changed: gcc8 base update
[opensg.git] / Source / System / State / Base / OSGShaderStorageBufferObjStdLayoutChunk.cpp
blob76142d749ec5c2cd79313cee83d269b5ce7ff36b
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include <boost/bind.hpp>
48 #include "OSGConfig.h"
50 #include "OSGGL.h"
51 #include "OSGGLU.h"
52 #include "OSGGLEXT.h"
53 #include "OSGWindow.h"
55 #include "OSGGLFuncProtos.h"
57 #include "OSGDrawEnv.h"
59 #include "OSGShaderStorageBufferObjStdLayoutChunk.h"
61 OSG_BEGIN_NAMESPACE
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 /***************************************************************************\
69 * Class variables *
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 /***************************************************************************\
90 * Class methods *
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 /***************************************************************************\
152 * Instance methods *
153 \***************************************************************************/
155 /*-------------------------------------------------------------------------*\
156 - private -
157 \*-------------------------------------------------------------------------*/
159 /*----------------------- constructors & destructors ----------------------*/
161 ShaderStorageBufferObjStdLayoutChunk::ShaderStorageBufferObjStdLayoutChunk(void) :
162 Inherited()
166 ShaderStorageBufferObjStdLayoutChunk::ShaderStorageBufferObjStdLayoutChunk(
167 const ShaderStorageBufferObjStdLayoutChunk &source) :
168 Inherited(source)
172 ShaderStorageBufferObjStdLayoutChunk::~ShaderStorageBufferObjStdLayoutChunk(void)
176 /*----------------------------- class specific ----------------------------*/
178 void ShaderStorageBufferObjStdLayoutChunk::changed(ConstFieldMaskArg whichField,
179 UInt32 origin,
180 BitVector details)
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)
199 return;
201 setGLId(Window::registerGLObject(
202 boost::bind(&ShaderStorageBufferObjStdLayoutChunk::handleGL,
203 ShaderStorageBufferObjStdLayoutChunkMTUncountedPtr(this),
204 _1, _2, _3, _4),
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)
217 if(getGLId() > 0)
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(
248 DrawEnv *pEnv,
249 UInt32 osgid,
250 Window::GLObjectStatusE mode,
251 UInt64 uiOptions)
253 Window *pWin = pEnv->getWindow();
254 GLuint id = pWin->getGLObjectId(osgid);
256 bool hasVBO = pWin->hasExtOrVersion(_extVertexBufferObject, 0x0105, 0x0200);
257 if(!hasVBO)
259 FWARNING(
260 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: vertex buffer objects not "
261 "supported for this window!\n"));
262 return 0;
265 bool hasUBO = pWin->hasExtOrVersion(_extUniformBufferObject, 0x0301);
266 if(!hasUBO)
268 FWARNING(
269 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: uniform buffer objects not "
270 "supported for this window!\n"));
271 return 0;
274 bool hasSSBO = pWin->hasExtOrVersion(_extShaderStorageBufferObject, 0x0403);
275 if(!hasSSBO)
277 FWARNING(
278 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: GL_ARB_uniform_buffer_object not "
279 "supported for this window!\n"));
280 return 0;
283 bool hasPIQ = pWin->hasExtOrVersion(_extProgramInterfaceQuery, 0x0402);
284 if(!hasPIQ)
286 FWARNING(
287 ("ShaderStorageBufferObjStdLayoutChunk::handleGL: GL_ARB_program_interface_query not "
288 "supported for this window!\n"));
289 return 0;
292 switch (mode)
294 case Window::initialize:
296 OSGGETGLFUNCBYID_GL3_ES( glGenBuffers,
297 osgGlGenBuffers,
298 _funcGenBuffers,
299 pWin);
301 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer,
302 osgGlBindBuffer,
303 _funcBindBuffer,
304 pWin);
306 OSGGETGLFUNCBYID_GL3_ES( glBufferData,
307 osgGlBufferData,
308 _funcBufferData,
309 pWin);
311 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase,
312 osgGlBindBufferBase,
313 _funcBindBufferBase,
314 pWin);
316 osgGlGenBuffers(1, &id);
317 pWin->setGLObjectId(osgid, id);
319 std::size_t sz = _mfBuffer.size();
320 if (sz > 0)
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");
329 break;
330 case Window::reinitialize:
332 std::size_t sz = _mfBuffer.size();
333 if (sz > 0)
335 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer,
336 osgGlBindBuffer,
337 _funcBindBuffer,
338 pWin);
340 OSGGETGLFUNCBYID_GL3_ES( glGetBufferParameteriv,
341 osgGlGetBufferParameteriv,
342 _funcGetBufferParameteriv,
343 pWin);
345 OSGGETGLFUNCBYID_GL3_ES( glBufferData,
346 osgGlBufferData,
347 _funcBufferData,
348 pWin);
350 OSGGETGLFUNCBYID_GL3_ES( glMapBuffer,
351 osgGlMapBuffer,
352 _funcMapBuffer,
353 pWin);
355 OSGGETGLFUNCBYID_GL3_ES( glUnmapBuffer,
356 osgGlUnmapBuffer,
357 _funcUnmapBuffer,
358 pWin);
360 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase,
361 osgGlBindBufferBase,
362 _funcBindBufferBase,
363 pWin);
365 osgGlBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
367 GLint curr_size;
368 osgGlGetBufferParameteriv( GL_SHADER_STORAGE_BUFFER,
369 GL_BUFFER_SIZE,
370 &curr_size);
372 GLint curr_usage;
373 osgGlGetBufferParameteriv( GL_SHADER_STORAGE_BUFFER,
374 GL_BUFFER_USAGE,
375 &curr_usage);
377 if ( sz != SizeT (curr_size ) ||
378 _sfUsage.getValue() != GLenum(curr_usage) )
380 osgGlBufferData( GL_SHADER_STORAGE_BUFFER,
381 sz,
382 &_mfBuffer[0],
383 _sfUsage.getValue());
385 else
387 GLubyte* pBuffer = static_cast<GLubyte*>(
388 osgGlMapBuffer(GL_SHADER_STORAGE_BUFFER,
389 GL_WRITE_ONLY_ARB));
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");
402 break;
403 case Window::needrefresh:
405 OSGGETGLFUNCBYID_GL3_ES( glBindBuffer,
406 osgGlBindBuffer,
407 _funcBindBuffer,
408 pWin);
410 OSGGETGLFUNCBYID_GL3_ES( glMapBuffer,
411 osgGlMapBuffer,
412 _funcMapBuffer,
413 pWin);
415 OSGGETGLFUNCBYID_GL3_ES( glUnmapBuffer,
416 osgGlUnmapBuffer,
417 _funcUnmapBuffer,
418 pWin);
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");
432 break;
433 default:
435 SWARNING << "ShaderStorageBufferObjStdLayoutChunk(" << this << "::handleGL: Illegal mode: "
436 << mode << " for id " << id << std::endl;
440 return 0;
443 /*! GL object handler
444 destroy it
446 void ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL(
447 DrawEnv *pEnv,
448 UInt32 osgid,
449 Window::GLObjectStatusE mode)
451 Window *pWin = pEnv->getWindow();
452 GLuint id = pWin->getGLObjectId(osgid);
454 bool hasVBO = pWin->hasExtOrVersion(_extVertexBufferObject, 0x0105, 0x0200);
455 if(!hasVBO)
457 FWARNING(
458 ("ShaderStorageBufferObjStdLayoutChunk::handleDestroyGL: vertex buffer objects not "
459 "supported for this window!\n"));
460 return;
463 if(mode == Window::destroy)
465 OSGGETGLFUNCBYID_GL3_ES( glDeleteBuffers,
466 osgGlDeleteBuffers,
467 _funcDeleteBuffers,
468 pWin);
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;
479 else
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(),
491 pEnv );
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,
510 osgGlBindBuffer,
511 _funcBindBuffer,
512 pWin);
514 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase,
515 osgGlBindBufferBase,
516 _funcBindBufferBase,
517 pWin);
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,
532 osgGlBindBuffer,
533 _funcBindBuffer,
534 pWin);
536 OSGGETGLFUNCBYID_GL3_ES( glBindBufferBase,
537 osgGlBindBufferBase,
538 _funcBindBufferBase,
539 pWin);
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,
550 StateChunk *old,
551 UInt32 idx )
553 // change from me to me?
554 // this assumes I haven't changed in the meantime.
555 // is that a valid assumption?
556 if (old == this)
557 return;
559 old->deactivate(pEnv, idx);
560 this->activate(pEnv, idx);
563 OSG_END_NAMESPACE