root/OpenSceneGraph/trunk/include/osg/FrameBufferObject @ 11339

Revision 11339, 18.8 kB (checked in by robert, 4 years ago)

From Tim Moore, "This contains a couple of fixes to support changing FrameBufferObject? configurations on the fly; the user changes the camera attachments and calls Renderer::setCameraRequiresSetUp(). The major part of this submission is a comprehensive example of setting up floating point depth buffers. The user can change the near plane value and cycle through the available combinations of depth format and multisample buffer formats."

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14// initial FBO support written by Marco Jez, June 2005.
15
16#ifndef OSG_FRAMEBUFFEROBJECT
17#define OSG_FRAMEBUFFEROBJECT 1
18
19#include <osg/GL>
20#include <osg/Texture>
21#include <osg/buffered_value>
22#include <osg/Camera>
23
24#ifndef GL_EXT_framebuffer_object
25#define GL_EXT_framebuffer_object 1
26#define GL_FRAMEBUFFER_EXT                     0x8D40
27#define GL_RENDERBUFFER_EXT                    0x8D41
28// #define GL_STENCIL_INDEX_EXT                   0x8D45  // removed in rev. #114 of the spec
29#define GL_STENCIL_INDEX1_EXT                  0x8D46
30#define GL_STENCIL_INDEX4_EXT                  0x8D47
31#define GL_STENCIL_INDEX8_EXT                  0x8D48
32#define GL_STENCIL_INDEX16_EXT                 0x8D49
33#define GL_RENDERBUFFER_WIDTH_EXT              0x8D42
34#define GL_RENDERBUFFER_HEIGHT_EXT             0x8D43
35#define GL_RENDERBUFFER_INTERNAL_FORMAT_EXT    0x8D44
36#define GL_RENDERBUFFER_RED_SIZE_EXT           0x8D50
37#define GL_RENDERBUFFER_GREEN_SIZE_EXT         0x8D51
38#define GL_RENDERBUFFER_BLUE_SIZE_EXT          0x8D52
39#define GL_RENDERBUFFER_ALPHA_SIZE_EXT         0x8D53
40#define GL_RENDERBUFFER_DEPTH_SIZE_EXT         0x8D54
41#define GL_RENDERBUFFER_STENCIL_SIZE_EXT       0x8D55
42#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT            0x8CD0
43#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT            0x8CD1
44#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT          0x8CD2
45#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT  0x8CD3
46#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT     0x8CD4
47#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER_EXT          0x8CD4
48#define GL_COLOR_ATTACHMENT0_EXT                0x8CE0
49#define GL_COLOR_ATTACHMENT1_EXT                0x8CE1
50#define GL_COLOR_ATTACHMENT2_EXT                0x8CE2
51#define GL_COLOR_ATTACHMENT3_EXT                0x8CE3
52#define GL_COLOR_ATTACHMENT4_EXT                0x8CE4
53#define GL_COLOR_ATTACHMENT5_EXT                0x8CE5
54#define GL_COLOR_ATTACHMENT6_EXT                0x8CE6
55#define GL_COLOR_ATTACHMENT7_EXT                0x8CE7
56#define GL_COLOR_ATTACHMENT8_EXT                0x8CE8
57#define GL_COLOR_ATTACHMENT9_EXT                0x8CE9
58#define GL_COLOR_ATTACHMENT10_EXT               0x8CEA
59#define GL_COLOR_ATTACHMENT11_EXT               0x8CEB
60#define GL_COLOR_ATTACHMENT12_EXT               0x8CEC
61#define GL_COLOR_ATTACHMENT13_EXT               0x8CED
62#define GL_COLOR_ATTACHMENT14_EXT               0x8CEE
63#define GL_COLOR_ATTACHMENT15_EXT               0x8CEF
64#define GL_DEPTH_ATTACHMENT_EXT                 0x8D00
65#define GL_STENCIL_ATTACHMENT_EXT               0x8D20
66#define GL_FRAMEBUFFER_COMPLETE_EXT                          0x8CD5
67#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT             0x8CD6
68#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT     0x8CD7
69#define GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT   0x8CD8
70#define GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT             0x8CD9
71#define GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT                0x8CDA
72#define GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT            0x8CDB
73#define GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT            0x8CDC
74#define GL_FRAMEBUFFER_UNSUPPORTED_EXT                       0x8CDD
75#define GL_FRAMEBUFFER_BINDING_EXT             0x8CA6
76#define GL_RENDERBUFFER_BINDING_EXT            0x8CA7
77#define GL_MAX_COLOR_ATTACHMENTS_EXT           0x8CDF
78#define GL_MAX_RENDERBUFFER_SIZE_EXT           0x84E8
79#define GL_INVALID_FRAMEBUFFER_OPERATION_EXT   0x0506
80#endif
81
82#ifndef GL_EXT_framebuffer_blit
83#define GL_EXT_framebuffer_blit 1
84#define GL_DRAW_FRAMEBUFFER_BINDING_EXT 0x8CA6
85#define GL_READ_FRAMEBUFFER_EXT 0x8CA8
86#define GL_DRAW_FRAMEBUFFER_EXT 0x8CA9
87#define GL_READ_FRAMEBUFFER_BINDING_EXT 0x8CAA
88#endif
89
90#ifndef GL_EXT_framebuffer_multisample
91#define GL_EXT_framebuffer_multisample 1
92#define GL_RENDERBUFFER_SAMPLES_EXT                 0x8CAB
93#define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT   0x8D56
94#define GL_MAX_SAMPLES_EXT                          0x8D57
95#endif
96
97#ifndef GL_MAX_SAMPLES_EXT
98// Workaround for Centos 5 and other distros that define
99// GL_EXT_framebuffer_multisample but not GL_MAX_SAMPLES_EXT
100#define GL_MAX_SAMPLES_EXT 0x8D57
101#endif
102
103#ifndef GL_NV_framebuffer_multisample_coverage
104#define GL_NV_framebuffer_multisample_coverage 1
105#define GL_RENDERBUFFER_COVERAGE_SAMPLES_NV     0x8CAB
106#define GL_RENDERBUFFER_COLOR_SAMPLES_NV        0x8E10
107#define GL_MAX_MULTISAMPLE_COVERAGE_MODES_NV    0x8E11
108#define GL_MULTISAMPLE_COVERAGE_MODES_NV        0x8E12
109#endif
110
111#ifndef GL_DEPTH_COMPONENT
112#define GL_DEPTH_COMPONENT              0x1902
113#endif
114
115#ifndef GL_VERSION_1_4
116#define GL_DEPTH_COMPONENT16              0x81A5
117#define GL_DEPTH_COMPONENT24              0x81A6
118#define GL_DEPTH_COMPONENT32              0x81A7
119#endif
120
121#ifndef GL_EXT_packed_depth_stencil
122#define GL_EXT_packed_depth_stencil 1
123#define GL_DEPTH_STENCIL_EXT 0x84F9
124#define GL_UNSIGNED_INT_24_8_EXT 0x84FA
125#define GL_DEPTH24_STENCIL8_EXT 0x88F0
126#define GL_TEXTURE_STENCIL_SIZE_EXT 0x88F1
127#endif
128
129namespace osg
130{
131
132/**************************************************************************
133 * FBOExtensions
134 **************************************************************************/
135
136    class OSG_EXPORT FBOExtensions : public osg::Referenced
137    {
138    public:
139        typedef void APIENTRY TglBindRenderbuffer(GLenum, GLuint);
140        typedef void APIENTRY TglDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers);
141        typedef void APIENTRY TglGenRenderbuffers(GLsizei, GLuint *);
142        typedef void APIENTRY TglRenderbufferStorage(GLenum, GLenum, GLsizei, GLsizei);
143        typedef void APIENTRY TglRenderbufferStorageMultisample(GLenum, GLsizei, GLenum, GLsizei, GLsizei);
144        typedef void APIENTRY TglRenderbufferStorageMultisampleCoverageNV(GLenum, GLsizei, GLsizei, GLenum, GLsizei, GLsizei);
145        typedef void APIENTRY TglBindFramebuffer(GLenum, GLuint);
146        typedef void APIENTRY TglDeleteFramebuffers(GLsizei n, const GLuint *framebuffers);
147        typedef void APIENTRY TglGenFramebuffers(GLsizei, GLuint *);
148        typedef GLenum APIENTRY TglCheckFramebufferStatus(GLenum);
149        typedef void APIENTRY TglFramebufferTexture1D(GLenum, GLenum, GLenum, GLuint, GLint);
150        typedef void APIENTRY TglFramebufferTexture2D(GLenum, GLenum, GLenum, GLuint, GLint);
151        typedef void APIENTRY TglFramebufferTexture3D(GLenum, GLenum, GLenum, GLuint, GLint, GLint);
152        typedef void APIENTRY TglFramebufferTextureLayer(GLenum, GLenum, GLuint, GLint, GLint);
153        typedef void APIENTRY TglFramebufferRenderbuffer(GLenum, GLenum, GLenum, GLuint);
154        typedef void APIENTRY TglGenerateMipmap(GLenum);
155        typedef void APIENTRY TglBlitFramebuffer(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum);
156        typedef void APIENTRY TglGetRenderbufferParameteriv(GLenum, GLenum, GLint*);
157
158        TglBindRenderbuffer* glBindRenderbuffer;
159        TglGenRenderbuffers* glGenRenderbuffers;
160        TglDeleteRenderbuffers* glDeleteRenderbuffers;
161        TglRenderbufferStorage* glRenderbufferStorage;
162        TglRenderbufferStorageMultisample* glRenderbufferStorageMultisample;
163        TglRenderbufferStorageMultisampleCoverageNV* glRenderbufferStorageMultisampleCoverageNV;
164        TglBindFramebuffer* glBindFramebuffer;
165        TglDeleteFramebuffers* glDeleteFramebuffers;
166        TglGenFramebuffers* glGenFramebuffers;
167        TglCheckFramebufferStatus* glCheckFramebufferStatus;
168        TglFramebufferTexture1D* glFramebufferTexture1D;
169        TglFramebufferTexture2D* glFramebufferTexture2D;
170        TglFramebufferTexture3D* glFramebufferTexture3D;
171        TglFramebufferTextureLayer* glFramebufferTextureLayer;
172        TglFramebufferRenderbuffer* glFramebufferRenderbuffer;
173        TglGenerateMipmap* glGenerateMipmap;
174        TglBlitFramebuffer* glBlitFramebuffer;
175        TglGetRenderbufferParameteriv* glGetRenderbufferParameteriv;
176
177        static FBOExtensions* instance(unsigned contextID, bool createIfNotInitalized);
178
179        bool isSupported() const { return _supported; }
180        bool isMultisampleSupported() const { return glRenderbufferStorageMultisample != 0; }
181        bool isMultisampleCoverageSupported() const { return glRenderbufferStorageMultisampleCoverageNV != 0; }
182        bool isPackedDepthStencilSupported() const { return _packed_depth_stencil_supported; }
183
184    protected:
185        FBOExtensions(unsigned int contextID);
186
187        bool _supported;
188        bool _packed_depth_stencil_supported;
189    };
190
191/**************************************************************************
192 * RenderBuffer
193 **************************************************************************/
194
195    class OSG_EXPORT RenderBuffer: public Object
196    {
197    public:
198        RenderBuffer();
199        RenderBuffer(int width, int height, GLenum internalFormat, int samples=0, int colorSamples=0);
200        RenderBuffer(const RenderBuffer& copy, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
201
202        META_Object(osg, RenderBuffer);
203
204        inline int getWidth() const;
205        inline int getHeight() const;
206        inline void setWidth(int w);
207        inline void setHeight(int h);
208        inline void setSize(int w, int h);
209        inline GLenum getInternalFormat() const;
210        inline void setInternalFormat(GLenum format);
211        inline int getSamples() const;
212        inline int getColorSamples() const;
213        inline void setSamples(int samples);
214        inline void setColorSamples(int colorSamples);
215
216        GLuint getObjectID(unsigned int contextID, const FBOExtensions *ext) const;
217        inline int compare(const RenderBuffer &rb) const;
218
219        /** Mark internal RenderBuffer for deletion.
220          * Deletion requests are queued until they can be executed
221          * in the proper GL context. */
222        static void deleteRenderBuffer(unsigned int contextID, GLuint rb);
223
224        /** flush all the cached RenderBuffers which need to be deleted
225          * in the OpenGL context related to contextID.*/
226        static void flushDeletedRenderBuffers(unsigned int contextID,double currentTime, double& availableTime);
227
228        /** discard all the cached RenderBuffers which need to be deleted in the OpenGL context related to contextID.
229          * Note, unlike flush no OpenGL calls are made, instead the handles are all removed.
230          * this call is useful for when an OpenGL context has been destroyed. */
231        static void discardDeletedRenderBuffers(unsigned int contextID);
232
233        static int getMaxSamples(unsigned int contextID, const FBOExtensions *ext);
234
235    protected:
236        virtual ~RenderBuffer();
237        RenderBuffer &operator=(const RenderBuffer &) { return *this; }
238
239        inline void dirtyAll() const;
240
241    private:
242        mutable buffered_value<GLuint> _objectID;
243        mutable buffered_value<int> _dirty;
244
245        GLenum _internalFormat;
246        int _width;
247        int _height;
248        // "samples" in the framebuffer_multisample extension is equivalent to
249        // "coverageSamples" in the framebuffer_multisample_coverage extension.
250        int _samples;
251        int _colorSamples;
252    };
253
254    // INLINE METHODS
255
256    inline int RenderBuffer::getWidth() const
257    {
258        return _width;
259    }
260
261    inline int RenderBuffer::getHeight() const
262    {
263        return _height;
264    }
265
266    inline void RenderBuffer::setWidth(int w)
267    {
268        _width = w;
269        dirtyAll();
270    }
271
272    inline void RenderBuffer::setHeight(int h)
273    {
274        _height = h;
275        dirtyAll();
276    }
277
278    inline void RenderBuffer::setSize(int w, int h)
279    {
280        _width = w;
281        _height = h;
282        dirtyAll();
283    }
284
285    inline GLenum RenderBuffer::getInternalFormat() const
286    {
287        return _internalFormat;
288    }
289
290    inline void RenderBuffer::setInternalFormat(GLenum format)
291    {
292        _internalFormat = format;
293        dirtyAll();
294    }
295
296    inline int RenderBuffer::getSamples() const
297    {
298        return _samples;
299    }
300
301    inline int RenderBuffer::getColorSamples() const
302    {
303        return _colorSamples;
304    }
305
306    inline void RenderBuffer::setSamples(int samples)
307    {
308        _samples = samples;
309        dirtyAll();
310    }
311
312    inline void RenderBuffer::setColorSamples(int colorSamples)
313    {
314        _colorSamples = colorSamples;
315        dirtyAll();
316    }
317
318    inline void RenderBuffer::dirtyAll() const
319    {
320        _dirty.setAllElementsTo(1);
321    }
322
323    inline int RenderBuffer::compare(const RenderBuffer &rb) const
324    {
325        if (&rb == this) return 0;
326        if (_internalFormat < rb._internalFormat) return -1;
327        if (_internalFormat > rb._internalFormat) return 1;
328        if (_width < rb._width) return -1;
329        if (_width > rb._width) return 1;
330        if (_height < rb._height) return -1;
331        if (_height > rb._height) return 1;
332        return 0;
333    }
334
335/**************************************************************************
336 * FrameBufferAttachement
337 **************************************************************************/
338    class Texture1D;
339    class Texture2D;
340    class Texture3D;
341    class Texture2DArray;
342    class TextureCubeMap;
343    class TextureRectangle;
344
345    class OSG_EXPORT FrameBufferAttachment
346    {
347    public:
348        FrameBufferAttachment();
349        FrameBufferAttachment(const FrameBufferAttachment& copy);
350
351        explicit FrameBufferAttachment(RenderBuffer* target);
352        explicit FrameBufferAttachment(Texture1D* target, int level = 0);
353        explicit FrameBufferAttachment(Texture2D* target, int level = 0);
354        explicit FrameBufferAttachment(Texture3D* target, int zoffset, int level = 0);
355        explicit FrameBufferAttachment(Texture2DArray* target, int layer, int level = 0);
356        explicit FrameBufferAttachment(TextureCubeMap* target, int face, int level = 0);
357        explicit FrameBufferAttachment(TextureRectangle* target);
358        explicit FrameBufferAttachment(Camera::Attachment& attachment);
359       
360        ~FrameBufferAttachment();
361
362        FrameBufferAttachment&operator = (const FrameBufferAttachment& copy);       
363
364        bool isMultisample() const;
365        void createRequiredTexturesAndApplyGenerateMipMap(State& state, const FBOExtensions* ext) const;
366        void attach(State &state, GLenum target, GLenum attachment_point, const FBOExtensions* ext) const;
367        int compare(const FrameBufferAttachment &fa) const;
368
369        RenderBuffer* getRenderBuffer();
370        const RenderBuffer* getRenderBuffer() const;
371
372        Texture* getTexture();
373        const Texture* getTexture() const;
374
375        int getCubeMapFace() const;
376        int getTextureLevel() const;
377        int getTexture3DZOffset() const;
378        int getTextureArrayLayer() const;
379
380    private:
381        // use the Pimpl idiom to avoid dependency from
382        // all Texture* headers
383        struct Pimpl;
384        Pimpl* _ximpl;
385    };
386
387/**************************************************************************
388 * FrameBufferObject
389 **************************************************************************/
390    class OSG_EXPORT FrameBufferObject: public StateAttribute
391    {
392    public:
393        typedef std::map<Camera::BufferComponent, FrameBufferAttachment> AttachmentMap;
394        typedef std::vector<GLenum> MultipleRenderingTargets;
395       
396        typedef Camera::BufferComponent BufferComponent;
397
398        FrameBufferObject();
399        FrameBufferObject(const FrameBufferObject& copy, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
400
401        META_StateAttribute(osg, FrameBufferObject, (StateAttribute::Type)0x101010/*FrameBufferObject*/);
402
403        inline const AttachmentMap& getAttachmentMap() const;
404       
405
406        void setAttachment(BufferComponent attachment_point, const FrameBufferAttachment &attachment);
407        inline const FrameBufferAttachment& getAttachment(BufferComponent attachment_point) const;
408        inline bool hasAttachment(BufferComponent attachment_point) const;
409       
410        inline bool hasMultipleRenderingTargets() const { return !_drawBuffers.empty(); }
411        inline const MultipleRenderingTargets& getMultipleRenderingTargets() const { return _drawBuffers; }
412
413        bool isMultisample() const;
414
415        int compare(const StateAttribute &sa) const;
416       
417        void apply(State &state) const;
418
419        enum BindTarget
420        {
421            READ_FRAMEBUFFER = GL_READ_FRAMEBUFFER_EXT,
422            DRAW_FRAMEBUFFER = GL_DRAW_FRAMEBUFFER_EXT,
423            READ_DRAW_FRAMEBUFFER = GL_FRAMEBUFFER_EXT
424        };
425
426        /** Bind the FBO as either the read or draw target, or both. */
427        void apply(State &state, BindTarget target) const;
428
429        /** Mark internal FBO for deletion.
430          * Deletion requests are queued until they can be executed
431          * in the proper GL context. */
432        static void deleteFrameBufferObject(unsigned int contextID, GLuint program);
433
434        /** flush all the cached FBOs which need to be deleted
435          * in the OpenGL context related to contextID.*/
436        static void flushDeletedFrameBufferObjects(unsigned int contextID,double currentTime, double& availableTime);
437
438        /** discard all the cached FBOs which need to be deleted
439          * in the OpenGL context related to contextID.*/
440        static void discardDeletedFrameBufferObjects(unsigned int contextID);
441
442    protected:
443        virtual ~FrameBufferObject();
444        FrameBufferObject& operator = (const FrameBufferObject&) { return *this; }
445
446        void updateDrawBuffers();
447       
448        inline void dirtyAll();
449
450        GLenum convertBufferComponentToGLenum(BufferComponent attachment_point) const;
451
452    private:       
453        AttachmentMap               _attachments;
454
455        // Buffers passed to glDrawBuffers when using multiple render targets.
456        MultipleRenderingTargets    _drawBuffers;
457
458        mutable buffered_value<int>         _dirtyAttachmentList;
459        mutable buffered_value<int>         _unsupported;
460        mutable buffered_value<GLuint>      _fboID;
461       
462    };
463
464    // INLINE METHODS
465
466    inline const FrameBufferObject::AttachmentMap &FrameBufferObject::getAttachmentMap() const
467    {
468        return _attachments;
469    }
470
471    inline bool FrameBufferObject::hasAttachment(FrameBufferObject::BufferComponent attachment_point) const
472    {
473        return _attachments.find(attachment_point) != _attachments.end();
474    }
475
476    inline const FrameBufferAttachment &FrameBufferObject::getAttachment(FrameBufferObject::BufferComponent attachment_point) const
477    {
478        return _attachments.find(attachment_point)->second;
479    }
480
481    inline void FrameBufferObject::dirtyAll()
482    {
483        _dirtyAttachmentList.setAllElementsTo(1);
484    }
485
486
487}
488
489#endif
490
Note: See TracBrowser for help on using the browser.