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

Revision 11355, 19.0 kB (checked in by robert, 5 years ago)

From Mourad Boufarguine, "When compiling the example on VS9, the compiler complain about undefined GL_DEPTH_COMPONENT32F and GL_DEPTH_COMPONENT32F_NV. So I added them to include/osg/FrameBufferObject. The example builds fine and is working for me without crashs unless it is ran whithout argument. I added couple of lines to check for arguments number and print the example usage when needed."

  • 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_DEPTH_COMPONENT32F
122#define GL_DEPTH_COMPONENT32F             0x8CAC
123#endif
124
125#ifndef GL_DEPTH_COMPONENT32F_NV
126#define GL_DEPTH_COMPONENT32F_NV          0x8DAB
127#endif
128
129#ifndef GL_EXT_packed_depth_stencil
130#define GL_EXT_packed_depth_stencil 1
131#define GL_DEPTH_STENCIL_EXT 0x84F9
132#define GL_UNSIGNED_INT_24_8_EXT 0x84FA
133#define GL_DEPTH24_STENCIL8_EXT 0x88F0
134#define GL_TEXTURE_STENCIL_SIZE_EXT 0x88F1
135#endif
136
137namespace osg
138{
139
140/**************************************************************************
141 * FBOExtensions
142 **************************************************************************/
143
144    class OSG_EXPORT FBOExtensions : public osg::Referenced
145    {
146    public:
147        typedef void APIENTRY TglBindRenderbuffer(GLenum, GLuint);
148        typedef void APIENTRY TglDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers);
149        typedef void APIENTRY TglGenRenderbuffers(GLsizei, GLuint *);
150        typedef void APIENTRY TglRenderbufferStorage(GLenum, GLenum, GLsizei, GLsizei);
151        typedef void APIENTRY TglRenderbufferStorageMultisample(GLenum, GLsizei, GLenum, GLsizei, GLsizei);
152        typedef void APIENTRY TglRenderbufferStorageMultisampleCoverageNV(GLenum, GLsizei, GLsizei, GLenum, GLsizei, GLsizei);
153        typedef void APIENTRY TglBindFramebuffer(GLenum, GLuint);
154        typedef void APIENTRY TglDeleteFramebuffers(GLsizei n, const GLuint *framebuffers);
155        typedef void APIENTRY TglGenFramebuffers(GLsizei, GLuint *);
156        typedef GLenum APIENTRY TglCheckFramebufferStatus(GLenum);
157        typedef void APIENTRY TglFramebufferTexture1D(GLenum, GLenum, GLenum, GLuint, GLint);
158        typedef void APIENTRY TglFramebufferTexture2D(GLenum, GLenum, GLenum, GLuint, GLint);
159        typedef void APIENTRY TglFramebufferTexture3D(GLenum, GLenum, GLenum, GLuint, GLint, GLint);
160        typedef void APIENTRY TglFramebufferTextureLayer(GLenum, GLenum, GLuint, GLint, GLint);
161        typedef void APIENTRY TglFramebufferRenderbuffer(GLenum, GLenum, GLenum, GLuint);
162        typedef void APIENTRY TglGenerateMipmap(GLenum);
163        typedef void APIENTRY TglBlitFramebuffer(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum);
164        typedef void APIENTRY TglGetRenderbufferParameteriv(GLenum, GLenum, GLint*);
165
166        TglBindRenderbuffer* glBindRenderbuffer;
167        TglGenRenderbuffers* glGenRenderbuffers;
168        TglDeleteRenderbuffers* glDeleteRenderbuffers;
169        TglRenderbufferStorage* glRenderbufferStorage;
170        TglRenderbufferStorageMultisample* glRenderbufferStorageMultisample;
171        TglRenderbufferStorageMultisampleCoverageNV* glRenderbufferStorageMultisampleCoverageNV;
172        TglBindFramebuffer* glBindFramebuffer;
173        TglDeleteFramebuffers* glDeleteFramebuffers;
174        TglGenFramebuffers* glGenFramebuffers;
175        TglCheckFramebufferStatus* glCheckFramebufferStatus;
176        TglFramebufferTexture1D* glFramebufferTexture1D;
177        TglFramebufferTexture2D* glFramebufferTexture2D;
178        TglFramebufferTexture3D* glFramebufferTexture3D;
179        TglFramebufferTextureLayer* glFramebufferTextureLayer;
180        TglFramebufferRenderbuffer* glFramebufferRenderbuffer;
181        TglGenerateMipmap* glGenerateMipmap;
182        TglBlitFramebuffer* glBlitFramebuffer;
183        TglGetRenderbufferParameteriv* glGetRenderbufferParameteriv;
184
185        static FBOExtensions* instance(unsigned contextID, bool createIfNotInitalized);
186
187        bool isSupported() const { return _supported; }
188        bool isMultisampleSupported() const { return glRenderbufferStorageMultisample != 0; }
189        bool isMultisampleCoverageSupported() const { return glRenderbufferStorageMultisampleCoverageNV != 0; }
190        bool isPackedDepthStencilSupported() const { return _packed_depth_stencil_supported; }
191
192    protected:
193        FBOExtensions(unsigned int contextID);
194
195        bool _supported;
196        bool _packed_depth_stencil_supported;
197    };
198
199/**************************************************************************
200 * RenderBuffer
201 **************************************************************************/
202
203    class OSG_EXPORT RenderBuffer: public Object
204    {
205    public:
206        RenderBuffer();
207        RenderBuffer(int width, int height, GLenum internalFormat, int samples=0, int colorSamples=0);
208        RenderBuffer(const RenderBuffer& copy, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
209
210        META_Object(osg, RenderBuffer);
211
212        inline int getWidth() const;
213        inline int getHeight() const;
214        inline void setWidth(int w);
215        inline void setHeight(int h);
216        inline void setSize(int w, int h);
217        inline GLenum getInternalFormat() const;
218        inline void setInternalFormat(GLenum format);
219        inline int getSamples() const;
220        inline int getColorSamples() const;
221        inline void setSamples(int samples);
222        inline void setColorSamples(int colorSamples);
223
224        GLuint getObjectID(unsigned int contextID, const FBOExtensions *ext) const;
225        inline int compare(const RenderBuffer &rb) const;
226
227        /** Mark internal RenderBuffer for deletion.
228          * Deletion requests are queued until they can be executed
229          * in the proper GL context. */
230        static void deleteRenderBuffer(unsigned int contextID, GLuint rb);
231
232        /** flush all the cached RenderBuffers which need to be deleted
233          * in the OpenGL context related to contextID.*/
234        static void flushDeletedRenderBuffers(unsigned int contextID,double currentTime, double& availableTime);
235
236        /** discard all the cached RenderBuffers which need to be deleted in the OpenGL context related to contextID.
237          * Note, unlike flush no OpenGL calls are made, instead the handles are all removed.
238          * this call is useful for when an OpenGL context has been destroyed. */
239        static void discardDeletedRenderBuffers(unsigned int contextID);
240
241        static int getMaxSamples(unsigned int contextID, const FBOExtensions *ext);
242
243    protected:
244        virtual ~RenderBuffer();
245        RenderBuffer &operator=(const RenderBuffer &) { return *this; }
246
247        inline void dirtyAll() const;
248
249    private:
250        mutable buffered_value<GLuint> _objectID;
251        mutable buffered_value<int> _dirty;
252
253        GLenum _internalFormat;
254        int _width;
255        int _height;
256        // "samples" in the framebuffer_multisample extension is equivalent to
257        // "coverageSamples" in the framebuffer_multisample_coverage extension.
258        int _samples;
259        int _colorSamples;
260    };
261
262    // INLINE METHODS
263
264    inline int RenderBuffer::getWidth() const
265    {
266        return _width;
267    }
268
269    inline int RenderBuffer::getHeight() const
270    {
271        return _height;
272    }
273
274    inline void RenderBuffer::setWidth(int w)
275    {
276        _width = w;
277        dirtyAll();
278    }
279
280    inline void RenderBuffer::setHeight(int h)
281    {
282        _height = h;
283        dirtyAll();
284    }
285
286    inline void RenderBuffer::setSize(int w, int h)
287    {
288        _width = w;
289        _height = h;
290        dirtyAll();
291    }
292
293    inline GLenum RenderBuffer::getInternalFormat() const
294    {
295        return _internalFormat;
296    }
297
298    inline void RenderBuffer::setInternalFormat(GLenum format)
299    {
300        _internalFormat = format;
301        dirtyAll();
302    }
303
304    inline int RenderBuffer::getSamples() const
305    {
306        return _samples;
307    }
308
309    inline int RenderBuffer::getColorSamples() const
310    {
311        return _colorSamples;
312    }
313
314    inline void RenderBuffer::setSamples(int samples)
315    {
316        _samples = samples;
317        dirtyAll();
318    }
319
320    inline void RenderBuffer::setColorSamples(int colorSamples)
321    {
322        _colorSamples = colorSamples;
323        dirtyAll();
324    }
325
326    inline void RenderBuffer::dirtyAll() const
327    {
328        _dirty.setAllElementsTo(1);
329    }
330
331    inline int RenderBuffer::compare(const RenderBuffer &rb) const
332    {
333        if (&rb == this) return 0;
334        if (_internalFormat < rb._internalFormat) return -1;
335        if (_internalFormat > rb._internalFormat) return 1;
336        if (_width < rb._width) return -1;
337        if (_width > rb._width) return 1;
338        if (_height < rb._height) return -1;
339        if (_height > rb._height) return 1;
340        return 0;
341    }
342
343/**************************************************************************
344 * FrameBufferAttachement
345 **************************************************************************/
346    class Texture1D;
347    class Texture2D;
348    class Texture3D;
349    class Texture2DArray;
350    class TextureCubeMap;
351    class TextureRectangle;
352
353    class OSG_EXPORT FrameBufferAttachment
354    {
355    public:
356        FrameBufferAttachment();
357        FrameBufferAttachment(const FrameBufferAttachment& copy);
358
359        explicit FrameBufferAttachment(RenderBuffer* target);
360        explicit FrameBufferAttachment(Texture1D* target, int level = 0);
361        explicit FrameBufferAttachment(Texture2D* target, int level = 0);
362        explicit FrameBufferAttachment(Texture3D* target, int zoffset, int level = 0);
363        explicit FrameBufferAttachment(Texture2DArray* target, int layer, int level = 0);
364        explicit FrameBufferAttachment(TextureCubeMap* target, int face, int level = 0);
365        explicit FrameBufferAttachment(TextureRectangle* target);
366        explicit FrameBufferAttachment(Camera::Attachment& attachment);
367       
368        ~FrameBufferAttachment();
369
370        FrameBufferAttachment&operator = (const FrameBufferAttachment& copy);       
371
372        bool isMultisample() const;
373        void createRequiredTexturesAndApplyGenerateMipMap(State& state, const FBOExtensions* ext) const;
374        void attach(State &state, GLenum target, GLenum attachment_point, const FBOExtensions* ext) const;
375        int compare(const FrameBufferAttachment &fa) const;
376
377        RenderBuffer* getRenderBuffer();
378        const RenderBuffer* getRenderBuffer() const;
379
380        Texture* getTexture();
381        const Texture* getTexture() const;
382
383        int getCubeMapFace() const;
384        int getTextureLevel() const;
385        int getTexture3DZOffset() const;
386        int getTextureArrayLayer() const;
387
388    private:
389        // use the Pimpl idiom to avoid dependency from
390        // all Texture* headers
391        struct Pimpl;
392        Pimpl* _ximpl;
393    };
394
395/**************************************************************************
396 * FrameBufferObject
397 **************************************************************************/
398    class OSG_EXPORT FrameBufferObject: public StateAttribute
399    {
400    public:
401        typedef std::map<Camera::BufferComponent, FrameBufferAttachment> AttachmentMap;
402        typedef std::vector<GLenum> MultipleRenderingTargets;
403       
404        typedef Camera::BufferComponent BufferComponent;
405
406        FrameBufferObject();
407        FrameBufferObject(const FrameBufferObject& copy, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
408
409        META_StateAttribute(osg, FrameBufferObject, (StateAttribute::Type)0x101010/*FrameBufferObject*/);
410
411        inline const AttachmentMap& getAttachmentMap() const;
412       
413
414        void setAttachment(BufferComponent attachment_point, const FrameBufferAttachment &attachment);
415        inline const FrameBufferAttachment& getAttachment(BufferComponent attachment_point) const;
416        inline bool hasAttachment(BufferComponent attachment_point) const;
417       
418        inline bool hasMultipleRenderingTargets() const { return !_drawBuffers.empty(); }
419        inline const MultipleRenderingTargets& getMultipleRenderingTargets() const { return _drawBuffers; }
420
421        bool isMultisample() const;
422
423        int compare(const StateAttribute &sa) const;
424       
425        void apply(State &state) const;
426
427        enum BindTarget
428        {
429            READ_FRAMEBUFFER = GL_READ_FRAMEBUFFER_EXT,
430            DRAW_FRAMEBUFFER = GL_DRAW_FRAMEBUFFER_EXT,
431            READ_DRAW_FRAMEBUFFER = GL_FRAMEBUFFER_EXT
432        };
433
434        /** Bind the FBO as either the read or draw target, or both. */
435        void apply(State &state, BindTarget target) const;
436
437        /** Mark internal FBO for deletion.
438          * Deletion requests are queued until they can be executed
439          * in the proper GL context. */
440        static void deleteFrameBufferObject(unsigned int contextID, GLuint program);
441
442        /** flush all the cached FBOs which need to be deleted
443          * in the OpenGL context related to contextID.*/
444        static void flushDeletedFrameBufferObjects(unsigned int contextID,double currentTime, double& availableTime);
445
446        /** discard all the cached FBOs which need to be deleted
447          * in the OpenGL context related to contextID.*/
448        static void discardDeletedFrameBufferObjects(unsigned int contextID);
449
450    protected:
451        virtual ~FrameBufferObject();
452        FrameBufferObject& operator = (const FrameBufferObject&) { return *this; }
453
454        void updateDrawBuffers();
455       
456        inline void dirtyAll();
457
458        GLenum convertBufferComponentToGLenum(BufferComponent attachment_point) const;
459
460    private:       
461        AttachmentMap               _attachments;
462
463        // Buffers passed to glDrawBuffers when using multiple render targets.
464        MultipleRenderingTargets    _drawBuffers;
465
466        mutable buffered_value<int>         _dirtyAttachmentList;
467        mutable buffered_value<int>         _unsupported;
468        mutable buffered_value<GLuint>      _fboID;
469       
470    };
471
472    // INLINE METHODS
473
474    inline const FrameBufferObject::AttachmentMap &FrameBufferObject::getAttachmentMap() const
475    {
476        return _attachments;
477    }
478
479    inline bool FrameBufferObject::hasAttachment(FrameBufferObject::BufferComponent attachment_point) const
480    {
481        return _attachments.find(attachment_point) != _attachments.end();
482    }
483
484    inline const FrameBufferAttachment &FrameBufferObject::getAttachment(FrameBufferObject::BufferComponent attachment_point) const
485    {
486        return _attachments.find(attachment_point)->second;
487    }
488
489    inline void FrameBufferObject::dirtyAll()
490    {
491        _dirtyAttachmentList.setAllElementsTo(1);
492    }
493
494
495}
496
497#endif
498
Note: See TracBrowser for help on using the browser.