1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
45 #include <boost/bind.hpp>
47 #include "OSGConfig.h"
50 #include "OSGGLFuncProtos.h"
52 #include "OSGRenderBuffer.h"
53 #include "OSGDrawEnv.h"
59 UInt32
RenderBuffer::_uiFramebufferObjectArb
=
60 Window::invalidExtensionID
;
62 UInt32
RenderBuffer::_uiFramebufferMultiSampleExt
=
63 Window::invalidExtensionID
;
65 UInt32
RenderBuffer::_uiFramebufferMultiSampleCoverageNV
=
66 Window::invalidExtensionID
;
69 UInt32
RenderBuffer::_uiFuncFramebufferRenderbuffer
=
70 Window::invalidFunctionID
;
72 UInt32
RenderBuffer::_uiFuncGenRenderbuffers
=
73 Window::invalidFunctionID
;
75 UInt32
RenderBuffer::_uiFuncDeleteRenderbuffers
=
76 Window::invalidFunctionID
;
78 UInt32
RenderBuffer::_uiFuncBindRenderbuffer
=
79 Window::invalidFunctionID
;
81 UInt32
RenderBuffer::_uiFuncRenderbufferStorage
=
82 Window::invalidFunctionID
;
84 UInt32
RenderBuffer::_uiFuncRenderbufferStorageMultiSample
=
85 Window::invalidFunctionID
;
87 UInt32
RenderBuffer::_uiFuncRenderbufferStorageMultiSampleCoverage
=
88 Window::invalidFunctionID
;
90 // Documentation for this class is emited in the
91 // OSGRenderBufferBase.cpp file.
92 // To modify it, please change the .fcd file (OSGRenderBuffer.fcd) and
93 // regenerate the base file.
95 void RenderBuffer::bind(DrawEnv
*pEnv
, UInt32 index
)
97 Window
*pWindow
= pEnv
->getWindow();
99 pWindow
->validateGLObject(getGLId(), pEnv
);
101 OSGGETGLFUNCBYID_GL3_ES( glFramebufferRenderbuffer
,
102 osgGlFramebufferRenderbuffer
,
103 _uiFuncFramebufferRenderbuffer
,
106 osgGlFramebufferRenderbuffer(GL_FRAMEBUFFER
,
109 pWindow
->getGLObjectId(getGLId()));
112 void RenderBuffer::validate(DrawEnv
*pEnv
)
114 Window
*pWindow
= pEnv
->getWindow();
116 pWindow
->validateGLObject(getGLId(), pEnv
);
120 /*----------------------- constructors & destructors ----------------------*/
122 RenderBuffer::RenderBuffer(void) :
127 RenderBuffer::RenderBuffer(const RenderBuffer
&source
) :
132 RenderBuffer::~RenderBuffer(void)
135 Window::destroyGLObject(getGLId(), 1);
138 /*----------------------------- class specific ----------------------------*/
140 void RenderBuffer::initMethod(InitPhase ePhase
)
142 Inherited::initMethod(ePhase
);
144 if(ePhase
== TypeObject::SystemPost
)
146 _uiFramebufferObjectArb
=
147 Window::registerExtension("GL_ARB_framebuffer_object");
149 _uiFramebufferMultiSampleExt
=
150 Window::registerExtension("GL_EXT_framebuffer_multisample");
152 _uiFramebufferMultiSampleCoverageNV
=
153 Window::registerExtension("GL_NV_framebuffer_multisample_coverage");
155 _uiFuncFramebufferRenderbuffer
=
156 Window::registerFunction (
157 OSG_DLSYM_UNDERSCORE
"glFramebufferRenderbuffer",
158 _uiFramebufferObjectArb
);
160 _uiFuncGenRenderbuffers
=
161 Window::registerFunction (
162 OSG_DLSYM_UNDERSCORE
"glGenRenderbuffers",
163 _uiFramebufferObjectArb
);
165 _uiFuncDeleteRenderbuffers
=
166 Window::registerFunction (
167 OSG_DLSYM_UNDERSCORE
"glDeleteRenderbuffers",
168 _uiFramebufferObjectArb
);
170 _uiFuncRenderbufferStorage
=
171 Window::registerFunction (
172 OSG_DLSYM_UNDERSCORE
"glRenderbufferStorage",
173 _uiFramebufferObjectArb
);
176 _uiFuncBindRenderbuffer
=
177 Window::registerFunction (
178 OSG_DLSYM_UNDERSCORE
"glBindRenderbuffer",
179 _uiFramebufferObjectArb
);
182 _uiFuncRenderbufferStorageMultiSample
=
183 Window::registerFunction (
184 OSG_DLSYM_UNDERSCORE
"glRenderbufferStorageMultisampleEXT",
185 _uiFramebufferMultiSampleExt
);
187 _uiFuncRenderbufferStorageMultiSampleCoverage
=
188 Window::registerFunction (
189 OSG_DLSYM_UNDERSCORE
"glRenderbufferStorageMultisampleCoverageNV",
190 _uiFramebufferMultiSampleCoverageNV
);
194 void RenderBuffer::onCreate(const RenderBuffer
*source
)
196 if(GlobalSystemState
== Startup
)
200 Window::registerGLObject(
201 boost::bind(&RenderBuffer::handleGL
,
202 RenderBufferMTUncountedPtr(this),
204 &RenderBuffer::handleDestroyGL
));
207 void RenderBuffer::changed(ConstFieldMaskArg whichField
,
211 Inherited::changed(whichField
, origin
, details
);
213 if(0x0000 != (whichField
& (WidthFieldMask
| HeightFieldMask
)))
215 Window::reinitializeGLObject(getGLId());
219 void RenderBuffer::dump( UInt32
,
220 const BitVector
) const
222 SLOG
<< "Dump RenderBuffer NI" << std::endl
;
225 void RenderBuffer::resizeBuffers(UInt32 uiWidth
, UInt32 uiHeight
)
227 Inherited::resize(uiWidth
, uiHeight
);
229 Window::reinitializeGLObject(getGLId());
232 GLenum
RenderBuffer::getBufferFormat(void) const
234 return _sfInternalFormat
.getValue();
237 UInt32
RenderBuffer::handleGL(DrawEnv
*pEnv
,
239 Window::GLObjectStatusE mode
,
242 Window
*pWindow
= pEnv
->getWindow();
244 if(mode
== Window::initialize
|| mode
== Window::reinitialize
||
245 mode
== Window::needrefresh
)
247 GLuint uiBufferId
= 0;
249 if(mode
== Window::initialize
)
251 OSGGETGLFUNCBYID_GL3_ES( glGenRenderbuffers
,
252 osgGlGenRenderbuffers
,
253 _uiFuncGenRenderbuffers
,
256 osgGlGenRenderbuffers(1, &uiBufferId
);
258 pWindow
->setGLObjectId(osgid
, uiBufferId
);
262 uiBufferId
= pWindow
->getGLObjectId(osgid
);
265 OSGGETGLFUNCBYID_GL3_ES( glBindRenderbuffer
,
266 osgGlBindRenderbuffer
,
267 _uiFuncBindRenderbuffer
,
270 OSGGETGLFUNCBYID_GL3_ES( glRenderbufferStorage
,
271 osgGlRenderbufferStorage
,
272 _uiFuncRenderbufferStorage
,
275 osgGlBindRenderbuffer(GL_RENDERBUFFER
, uiBufferId
);
277 GLenum internalFormat
= getInternalFormat();
279 if(internalFormat
== GL_NONE
&& getImage() != NULL
)
281 internalFormat
= getImage()->getPixelFormat();
285 if(pWindow
->hasExtension(_uiFramebufferMultiSampleCoverageNV
) == true &&
287 (_sfColorSamples
.getValue() != 0 ||
288 _sfCoverageSamples
.getValue() != 0 ) )
290 OSGGETGLFUNCBYID_EXT(
291 glRenderbufferStorageMultisampleCoverage
,
292 osgGlRenderbufferStorageMultisampleCoverage
,
293 _uiFuncRenderbufferStorageMultiSampleCoverage
,
296 osgGlRenderbufferStorageMultisampleCoverage(
298 _sfCoverageSamples
.getValue(),
299 _sfColorSamples
.getValue(),
304 else if(pWindow
->hasExtOrVersion(
305 _uiFramebufferMultiSampleExt
,
308 (_sfColorSamples
.getValue() != 0) )
310 OSGGETGLFUNCBYID_GL3( glRenderbufferStorageMultisample
,
311 osgGlRenderbufferStorageMultisample
,
312 _uiFuncRenderbufferStorageMultiSample
,
315 osgGlRenderbufferStorageMultisample(
317 _sfColorSamples
.getValue(),
324 osgGlRenderbufferStorage(GL_RENDERBUFFER
,
335 void RenderBuffer::handleDestroyGL(DrawEnv
*pEnv
,
337 Window::GLObjectStatusE mode
)
339 Window
*pWindow
= pEnv
->getWindow();
341 if(mode
== Window::destroy
)
343 GLuint uiBufferId
= pWindow
->getGLObjectId(osgid
);
345 OSGGETGLFUNCBYID_GL3_ES( glDeleteRenderbuffers
,
346 osgGlDeleteRenderbuffers
,
347 _uiFuncDeleteRenderbuffers
,
350 osgGlDeleteRenderbuffers(1, &uiBufferId
);
352 pWindow
->setGLObjectId(osgid
, 0);
354 else if(mode
== Window::finaldestroy
)
356 //SWARNING << "Last texture user destroyed" << std::endl;
360 void RenderBuffer::processPreDeactivate (DrawEnv
*pEnv
, UInt32 index
)
362 if(_sfReadBack
.getValue() == true)
364 Image
*pImg
= this->getImage();
369 if(pImg
->getData() == NULL
)
371 SINFO
<< "TextureBuffer::render: (Re)Allocating image "
375 pImg
->set(pImg
->getPixelFormat(),
379 pImg
->getMipMapCount(),
380 pImg
->getFrameCount (),
381 pImg
->getFrameDelay (),
383 pImg
->getDataType (),
385 pImg
->getSideCount () );
388 // select GL_COLORATTACHMENTn and read data into image
395 pImg
->getPixelFormat(),
396 pImg
->getDataType (),
400 glReadBuffer(GL_NONE
);
403 glErr("renderbuffer:predeactivate");
407 void RenderBuffer::processPostDeactivate(DrawEnv
*pEnv
)