1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
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 //---------------------------------------------------------------------------
46 #include <boost/bind.hpp>
48 #include "OSGConfig.h"
53 #include "OSGWindow.h"
55 #include "OSGGLFuncProtos.h"
57 #include "OSGDrawEnv.h"
59 #include "OSGShaderStorageBufferObjStdLayoutChunk.h"
63 // Documentation for this class is emitted in the
64 // OSGShaderStorageBufferObjStdLayoutChunkBase.cpp file.
65 // To modify it, please change the .fcd file (OSGShaderStorageBufferObjStdLayoutChunk.fcd) and
66 // regenerate the base file.
68 /***************************************************************************\
70 \***************************************************************************/
72 typedef OSG::Window Win
;
74 UInt32
ShaderStorageBufferObjStdLayoutChunk::_extUniformBufferObject
= Win::invalidExtensionID
;
75 UInt32
ShaderStorageBufferObjStdLayoutChunk::_extVertexBufferObject
= Win::invalidExtensionID
;
76 UInt32
ShaderStorageBufferObjStdLayoutChunk::_extProgramInterfaceQuery
= Win::invalidExtensionID
;
77 UInt32
ShaderStorageBufferObjStdLayoutChunk::_extShaderStorageBufferObject
= Win::invalidExtensionID
;
79 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcBindBuffer
= Win::invalidFunctionID
;
80 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcMapBuffer
= Win::invalidFunctionID
;
81 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcUnmapBuffer
= Win::invalidFunctionID
;
82 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcBufferData
= Win::invalidFunctionID
;
83 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcBufferSubData
= Win::invalidFunctionID
;
84 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcGenBuffers
= Win::invalidFunctionID
;
85 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcDeleteBuffers
= Win::invalidFunctionID
;
86 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcBindBufferBase
= Win::invalidFunctionID
;
87 UInt32
ShaderStorageBufferObjStdLayoutChunk::_funcGetBufferParameteriv
= Win::invalidFunctionID
;
89 /***************************************************************************\
91 \***************************************************************************/
93 void ShaderStorageBufferObjStdLayoutChunk::initMethod(InitPhase ePhase
)
95 Inherited::initMethod(ePhase
);
97 if(ePhase
== TypeObject::SystemPost
)
99 _extVertexBufferObject
=
100 Window::registerExtension("GL_ARB_vertex_buffer_object");
102 _funcBindBuffer
= Window::registerFunction(
103 OSG_DLSYM_UNDERSCORE
"glBindBufferARB",
104 _extVertexBufferObject
);
106 _funcMapBuffer
= Window::registerFunction(
107 OSG_DLSYM_UNDERSCORE
"glMapBufferARB",
108 _extVertexBufferObject
);
110 _funcUnmapBuffer
= Window::registerFunction(
111 OSG_DLSYM_UNDERSCORE
"glUnmapBufferARB",
112 _extVertexBufferObject
);
114 _funcBufferData
= Window::registerFunction(
115 OSG_DLSYM_UNDERSCORE
"glBufferDataARB",
116 _extVertexBufferObject
);
118 _funcBufferSubData
= Window::registerFunction(
119 OSG_DLSYM_UNDERSCORE
"glBufferSubDataARB",
120 _extVertexBufferObject
);
122 _funcDeleteBuffers
= Window::registerFunction(
123 OSG_DLSYM_UNDERSCORE
"glDeleteBuffersARB",
124 _extVertexBufferObject
);
126 _funcGenBuffers
= Window::registerFunction(
127 OSG_DLSYM_UNDERSCORE
"glGenBuffersARB",
128 _extVertexBufferObject
);
130 _funcGetBufferParameteriv
= Window::registerFunction(
131 OSG_DLSYM_UNDERSCORE
"glGetBufferParameterivARB",
132 _extVertexBufferObject
);
135 _extUniformBufferObject
=
136 Window::registerExtension("GL_ARB_uniform_buffer_object" );
138 _funcBindBufferBase
= Window::registerFunction(
139 OSG_DLSYM_UNDERSCORE
"glBindBufferBase",
140 _extUniformBufferObject
);
143 _extProgramInterfaceQuery
=
144 Window::registerExtension("GL_ARB_program_interface_query");
146 _extShaderStorageBufferObject
=
147 Window::registerExtension("GL_ARB_shader_storage_buffer_object");
151 /***************************************************************************\
153 \***************************************************************************/
155 /*-------------------------------------------------------------------------*\
157 \*-------------------------------------------------------------------------*/
159 /*----------------------- constructors & destructors ----------------------*/
161 ShaderStorageBufferObjStdLayoutChunk::ShaderStorageBufferObjStdLayoutChunk(void) :
166 ShaderStorageBufferObjStdLayoutChunk::ShaderStorageBufferObjStdLayoutChunk(
167 const ShaderStorageBufferObjStdLayoutChunk
&source
) :
172 ShaderStorageBufferObjStdLayoutChunk::~ShaderStorageBufferObjStdLayoutChunk(void)
176 /*----------------------------- class specific ----------------------------*/
178 void ShaderStorageBufferObjStdLayoutChunk::changed(ConstFieldMaskArg whichField
,
182 GLenum id
= _sfGLId
.getValue();
184 if( (whichField
& (UsageFieldMask
| BufferFieldMask
)) != 0 )
186 Window::reinitializeGLObject(id
);
189 Inherited::changed(whichField
, origin
, details
);
192 /*----------------------------- onCreate --------------------------------*/
194 void ShaderStorageBufferObjStdLayoutChunk::onCreate(const ShaderStorageBufferObjStdLayoutChunk
*source
)
196 Inherited::onCreate(source
);
198 if(GlobalSystemState
== Startup
)
201 setGLId(Window::registerGLObject(
202 boost::bind(&ShaderStorageBufferObjStdLayoutChunk::handleGL
,
203 ShaderStorageBufferObjStdLayoutChunkMTUncountedPtr(this),
205 &ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL
));
208 void ShaderStorageBufferObjStdLayoutChunk::onCreateAspect(
209 const ShaderStorageBufferObjStdLayoutChunk
*createAspect
,
210 const ShaderStorageBufferObjStdLayoutChunk
*source
)
212 Inherited::onCreateAspect(createAspect
, source
);
215 void ShaderStorageBufferObjStdLayoutChunk::onDestroy(UInt32 uiContainerId
)
218 Window::destroyGLObject(getGLId(), 1);
220 Inherited::onDestroy(uiContainerId
);
223 /*------------------------------ Output ----------------------------------*/
225 void ShaderStorageBufferObjStdLayoutChunk::dump( UInt32 uiIndent
,
226 const BitVector bvFlags
) const
228 SLOG
<< "Dump ShaderStorageBufferObjStdLayoutChunk NI" << std::endl
;
230 Inherited::dump(uiIndent
, bvFlags
);
232 if((bvFlags
& UsageFieldMask
) != 0)
234 indentLog(uiIndent
, PLOG
);
235 PLOG
<< "usage " << _sfUsage
.getValue() << "\n";
237 if((bvFlags
& BufferFieldMask
) != 0)
239 indentLog(uiIndent
, PLOG
);
240 PLOG
<< "buffer " << "content changed" << "\n";// _mfBuffer.getValue() << "\n";
244 /*------------------------------ GL -----------------------------------------*/
246 /*! GL object handler */
247 UInt32
ShaderStorageBufferObjStdLayoutChunk::handleGL(
250 Window::GLObjectStatusE mode
,
253 Window
*pWin
= pEnv
->getWindow();
254 GLuint id
= pWin
->getGLObjectId(osgid
);
256 bool hasVBO
= pWin
->hasExtOrVersion(_extVertexBufferObject
, 0x0105, 0x0200);
260 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: vertex buffer objects not "
261 "supported for this window!\n"));
265 bool hasUBO
= pWin
->hasExtOrVersion(_extUniformBufferObject
, 0x0301);
269 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: uniform buffer objects not "
270 "supported for this window!\n"));
274 bool hasSSBO
= pWin
->hasExtOrVersion(_extShaderStorageBufferObject
, 0x0403);
278 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: GL_ARB_uniform_buffer_object not "
279 "supported for this window!\n"));
283 bool hasPIQ
= pWin
->hasExtOrVersion(_extProgramInterfaceQuery
, 0x0402);
287 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: GL_ARB_program_interface_query not "
288 "supported for this window!\n"));
294 case Window::initialize
:
296 OSGGETGLFUNCBYID_GL3_ES( glGenBuffers
,
301 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer
,
306 OSGGETGLFUNCBYID_GL3_ES( glBufferData
,
311 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase
,
316 osgGlGenBuffers(1, &id
);
317 pWin
->setGLObjectId(osgid
, id
);
319 std::size_t sz
= _mfBuffer
.size();
322 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, id
);
323 osgGlBufferData (GL_SHADER_STORAGE_BUFFER
, sz
, &_mfBuffer
[0], _sfUsage
.getValue());
324 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, 0);
327 glErr("ShaderStorageBufferObjStdLayoutChunk::handleGL initialize");
330 case Window::reinitialize
:
332 std::size_t sz
= _mfBuffer
.size();
335 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer
,
340 OSGGETGLFUNCBYID_GL3_ES( glGetBufferParameteriv
,
341 osgGlGetBufferParameteriv
,
342 _funcGetBufferParameteriv
,
345 OSGGETGLFUNCBYID_GL3_ES( glBufferData
,
350 OSGGETGLFUNCBYID_GL3_ES( glMapBuffer
,
355 OSGGETGLFUNCBYID_GL3_ES( glUnmapBuffer
,
360 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase
,
365 osgGlBindBuffer(GL_SHADER_STORAGE_BUFFER
, id
);
368 osgGlGetBufferParameteriv( GL_SHADER_STORAGE_BUFFER
,
373 osgGlGetBufferParameteriv( GL_SHADER_STORAGE_BUFFER
,
377 if ( sz
!= SizeT (curr_size
) ||
378 _sfUsage
.getValue() != GLenum(curr_usage
) )
380 osgGlBufferData( GL_SHADER_STORAGE_BUFFER
,
383 _sfUsage
.getValue());
387 GLubyte
* pBuffer
= static_cast<GLubyte
*>(
388 osgGlMapBuffer(GL_SHADER_STORAGE_BUFFER
,
391 UInt8
* p
= &_mfBuffer
[0];
392 memcpy(pBuffer
, p
, sizeof(UInt8
) * _mfBuffer
.size());
394 osgGlUnmapBuffer(GL_SHADER_STORAGE_BUFFER
);
397 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, 0);
399 glErr("ShaderStorageBufferObjStdLayoutChunk::handleGL reinitialize");
403 case Window::needrefresh
:
405 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer
,
410 OSGGETGLFUNCBYID_GL3_ES( glMapBuffer
,
415 OSGGETGLFUNCBYID_GL3_ES( glUnmapBuffer
,
420 osgGlBindBuffer(GL_UNIFORM_BUFFER
, id
);
421 GLubyte
* pBuffer
= static_cast<GLubyte
*>(
422 osgGlMapBuffer(GL_SHADER_STORAGE_BUFFER
, GL_WRITE_ONLY_ARB
));
424 UChar8
* p
= &_mfBuffer
[0];
425 memcpy(pBuffer
, p
, sizeof(UChar8
) * _mfBuffer
.size());
427 osgGlUnmapBuffer(GL_SHADER_STORAGE_BUFFER
);
428 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, 0);
430 glErr("ShaderStorageBufferObjStdLayoutChunk::handleGL needrefresh");
435 SWARNING
<< "ShaderStorageBufferObjStdLayoutChunk(" << this << "::handleGL: Illegal mode: "
436 << mode
<< " for id " << id
<< std::endl
;
443 /*! GL object handler
446 void ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL(
449 Window::GLObjectStatusE mode
)
451 Window
*pWin
= pEnv
->getWindow();
452 GLuint id
= pWin
->getGLObjectId(osgid
);
454 bool hasVBO
= pWin
->hasExtOrVersion(_extVertexBufferObject
, 0x0105, 0x0200);
458 ("ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL: vertex buffer objects not "
459 "supported for this window!\n"));
463 if(mode
== Window::destroy
)
465 OSGGETGLFUNCBYID_GL3_ES( glDeleteBuffers
,
470 osgGlDeleteBuffers(1, &id
);
471 pWin
->setGLObjectId(osgid
, 0);
473 glErr("ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL");
475 else if(mode
== Window::finaldestroy
)
477 //SWARNING << "Last buffer user destroyed" << std::endl;
481 SWARNING
<< "ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL: Illegal mode: "
482 << mode
<< " for id " << id
<< std::endl
;
486 /*------------------------------ tools --------------------------------------*/
488 void ShaderStorageBufferObjStdLayoutChunk::validate(DrawEnv
*pEnv
)
490 pEnv
->getWindow()->validateGLObject(this->getGLId(),
494 Int32
ShaderStorageBufferObjStdLayoutChunk::getOpenGLId(DrawEnv
*pEnv
)
496 return pEnv
->getWindow()->getGLObjectId(this->getGLId());
499 /*------------------------------ activate -----------------------------------*/
501 void ShaderStorageBufferObjStdLayoutChunk::activate(DrawEnv
*pEnv
, UInt32 idx
)
503 Window
*pWin
= pEnv
->getWindow();
505 pWin
->validateGLObject(getGLId(), pEnv
);
507 GLuint id
= pWin
->getGLObjectId(getGLId());
509 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer
,
514 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase
,
519 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, id
);
520 osgGlBindBufferBase(GL_SHADER_STORAGE_BUFFER
, idx
, id
);
522 glErr("ShaderStorageBufferObjStdLayoutChunk::activate");
525 /*------------------------------ deactivate ---------------------------------*/
527 void ShaderStorageBufferObjStdLayoutChunk::deactivate(DrawEnv
*pEnv
, UInt32 idx
)
529 Window
*pWin
= pEnv
->getWindow();
531 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer
,
536 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase
,
541 osgGlBindBufferBase(GL_SHADER_STORAGE_BUFFER
, idx
, 0);
542 osgGlBindBuffer (GL_SHADER_STORAGE_BUFFER
, 0);
544 glErr("ShaderStorageBufferObjStdLayoutChunk::deactivate");
547 /*------------------------------ changeFrom ---------------------------------*/
549 void ShaderStorageBufferObjStdLayoutChunk::changeFrom(DrawEnv
*pEnv
,
553 // change from me to me?
554 // this assumes I haven't changed in the meantime.
555 // is that a valid assumption?
559 old
->deactivate(pEnv
, idx
);
560 this->activate(pEnv
, idx
);