fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Variables / OSGShaderVariables.inl
blobe2a3c2f08bd19159a82cf5c19e0bba6944ac64f9
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *               Copyright (C) 2000-2002 by the OpenSG Forum                 *
6  *                                                                           *
7  *                            www.opensg.org                                 *
8  *                                                                           *
9  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
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 OSG_BEGIN_NAMESPACE
45 inline
46 void osgUniformShaderVariableLocation(DrawEnv        *pEnv, 
47                                       ShaderVariable *pVar, 
48                                       Int32          &loc, 
49                                       UInt32          uiProgram)
51     if(loc == -1)
52     {
53         OSGGETGLFUNC_GL3_ES(glGetUniformLocation,
54                             osgGlGetUniformLocation,
55                             ShaderProgram::getFuncIdGetUniformLocation());
57         loc = osgGlGetUniformLocation(uiProgram, pVar->getName().c_str());
58 #ifdef OSG_MULTISHADER_VARCHUNK
59         if(loc == -1)
60             loc = -2;
61 #endif
62      }
65 inline
66 void osgUniformShaderVariableBool(DrawEnv        *pEnv,      
67                                   ShaderVariable *pVar,       
68                                   Int32          &loc,
69                                   UInt32          uiProgram, 
70                                   bool            warnUnknown)
72     ShaderVariableBool *p = dynamic_cast<ShaderVariableBool *>(pVar);
74     if(loc > -1)
75     {
76         OSGGETGLFUNC_GL3_ES(glUniform1i,
77                             osgGlUniform1i,
78                             ShaderProgram::getFuncIdUniform1i());
80         osgGlUniform1i(loc, GLint(p->getValue()));
81     }
82     else if(warnUnknown == true)
83     {
84         SWARNING << "Variable '" << p->getName() << "' type 'bool' "
85                  << "not found in active uniform "
86                  << "variables of shader program '" << uiProgram << "'"
87                  << std::endl;
88     }
91 inline
92 void osgUniformShaderVariableInt(DrawEnv        *pEnv,      
93                                  ShaderVariable *pVar,       
94                                  Int32          &loc,
95                                  UInt32          uiProgram, 
96                                  bool            warnUnknown)
98     ShaderVariableInt *p = dynamic_cast<ShaderVariableInt *>(pVar);
100     if(loc > -1)
101     {
102         OSGGETGLFUNC_GL3_ES(glUniform1i,
103                             osgGlUniform1i,
104                             ShaderProgram::getFuncIdUniform1i());
106         osgGlUniform1i(loc, p->getValue());
107     }
108     else if(warnUnknown == true)
109     {
110         SWARNING << "Variable '" << p->getName() << "' type 'int' "
111                  << "not found in active uniform "
112                  << "variables of shader program '" << uiProgram << "'"
113                  << std::endl;
114     }
117 inline
118 void osgUniformShaderVariableUInt(DrawEnv        *pEnv,
119                                   ShaderVariable *pVar,
120                                   Int32          &loc,
121                                   UInt32          uiProgram,
122                                   bool            warnUnknown)
124     if(!pEnv->getWindow()->hasExtOrVersion(
125         ShaderProgram::getExtIdGPUShader4(), 0x0303, 0x0200))
126     {
127         SWARNING << "Uniforms of type 'uint' are not supported, could not "
128                  << "find extension 'EXT_gpu_shader4'!"
129                  << std::endl;
130         return;
131     }
133     ShaderVariableUInt *p = dynamic_cast<ShaderVariableUInt *>(pVar);
135     if(loc > -1)
136     {
137         OSGGETGLFUNC_GL3_ES(glUniform1ui,
138                             osgGlUniform1ui,
139                             ShaderProgram::getFuncIdUniform1ui());
141         osgGlUniform1ui(loc, p->getValue());
142     }
143     else if(warnUnknown == true)
144     {
145         SWARNING << "Variable '" << p->getName() << "' type 'uint' "
146                  << "not found in active uniform "
147                  << "variables of shader program '" << uiProgram << "'"
148                  << std::endl;
149     }
152 inline
153 void osgUniformShaderVariableReal(DrawEnv        *pEnv,      
154                                   ShaderVariable *pVar,       
155                                   Int32          &loc,
156                                   UInt32          uiProgram, 
157                                   bool            warnUnknown)
159     ShaderVariableReal *p = dynamic_cast<ShaderVariableReal *>(pVar);
161     if(loc > -1)
162     {
163         OSGGETGLFUNC_GL3_ES(glUniform1f,
164                             osgGlUniform1f,
165                             ShaderProgram::getFuncIdUniform1f());
167         osgGlUniform1f(loc, p->getValue());
168     }
169     else if(warnUnknown == true)
170     {
171         SWARNING << "Variable '" << p->getName() << "' type 'float' "
172                  << "not found in active uniform "
173                  << "variables of shader program '" << uiProgram << "'"
174                  << std::endl;
175     }
178 inline
179 void osgUniformShaderVariableVec2f(DrawEnv        *pEnv,      
180                                    ShaderVariable *pVar,       
181                                    Int32          &loc,
182                                    UInt32          uiProgram, 
183                                    bool            warnUnknown)
185     ShaderVariableVec2f *p = dynamic_cast<ShaderVariableVec2f *>(pVar);
187     if(loc > -1)
188     {
189         OSGGETGLFUNC_GL3_ES(glUniform2fv,
190                             osgGlUniform2fv,
191                             ShaderProgram::getFuncIdUniform2fv());
193         osgGlUniform2fv(loc, 1, p->getValue().getValues());
194     }
195     else if(warnUnknown == true)
196     {
197         SWARNING << "Variable '" << p->getName() << "' type 'vec2f' "
198                  << "not found in active uniform "
199                  << "variables of shader program '" << uiProgram << "'"
200                  << std::endl;
201     }
204 inline
205 void osgUniformShaderVariableVec3f(DrawEnv        *pEnv,      
206                                    ShaderVariable *pVar,       
207                                    Int32          &loc,               
208                                    UInt32          uiProgram, 
209                                    bool            warnUnknown)
211     ShaderVariableVec3f *p = dynamic_cast<ShaderVariableVec3f *>(pVar);
213     if(loc > -1)
214     {
215         OSGGETGLFUNC_GL3_ES(glUniform3fv,
216                             osgGlUniform3fv,
217                             ShaderProgram::getFuncIdUniform3fv());
219         osgGlUniform3fv(loc, 1, p->getValue().getValues());
220     }
221     else if(warnUnknown == true)
222     {
223         SWARNING << "Variable '" << p->getName() << "' type 'vec3f' "
224                  << "not found in active uniform "
225                  << "variables of shader program '" << uiProgram << "'"
226                  << std::endl;
227     }
230 inline
231 void osgUniformShaderVariableVec4f(DrawEnv        *pEnv,      
232                                    ShaderVariable *pVar,       
233                                    Int32          &loc,
234                                    UInt32          uiProgram, 
235                                    bool            warnUnknown)
237     ShaderVariableVec4f *p = dynamic_cast<ShaderVariableVec4f *>(pVar);
239     if(loc > -1)
240     {
241         OSGGETGLFUNC_GL3_ES(glUniform4fv,
242                             osgGlUniform4fv,
243                             ShaderProgram::getFuncIdUniform4fv());
245         osgGlUniform4fv(loc, 1, p->getValue().getValues());
246     }
247     else if(warnUnknown == true)
248     {
249         SWARNING << "Variable '" << p->getName() << "' type 'vec4f' "
250                  << "not found in active uniform "
251                  << "variables of shader program '" << uiProgram << "'"
252                  << std::endl;
253     }
256 inline
257 void osgUniformShaderVariablePnt2f(DrawEnv        *pEnv,      
258                                    ShaderVariable *pVar,       
259                                    Int32          &loc,
260                                    UInt32          uiProgram, 
261                                    bool            warnUnknown)
263     ShaderVariablePnt2f *p = dynamic_cast<ShaderVariablePnt2f *>(pVar);
265     if(loc > -1)
266     {
267         OSGGETGLFUNC_GL3_ES(glUniform2fv,
268                             osgGlUniform2fv,
269                             ShaderProgram::getFuncIdUniform2fv());
271         osgGlUniform2fv(loc, 1, p->getValue().getValues());
272     }
273     else if(warnUnknown == true)
274     {
275         SWARNING << "Variable '" << p->getName() << "' type 'pnt2f' "
276                  << "not found in active uniform "
277                  << "variables of shader program '" << uiProgram << "'"
278                  << std::endl;
279     }
282 inline
283 void osgUniformShaderVariablePnt3f(DrawEnv        *pEnv,      
284                                    ShaderVariable *pVar,       
285                                    Int32          &loc,
286                                    UInt32          uiProgram, 
287                                    bool            warnUnknown)
289     ShaderVariablePnt3f *p = dynamic_cast<ShaderVariablePnt3f *>(pVar);
291     if(loc > -1)
292     {
293         OSGGETGLFUNC_GL3_ES(glUniform3fv,
294                             osgGlUniform3fv,
295                             ShaderProgram::getFuncIdUniform3fv());
297         osgGlUniform3fv(loc, 1, p->getValue().getValues());
298     }
299     else if(warnUnknown == true)
300     {
301         SWARNING << "Variable '" << p->getName() << "' type 'pnt3f' "
302                  << "not found in active uniform "
303                  << "variables of shader program '" << uiProgram << "'"
304                  << std::endl;
305     }
308 inline
309 void osgUniformShaderVariableMatrix(DrawEnv        *pEnv,      
310                                     ShaderVariable *pVar,       
311                                     Int32          &loc,
312                                     UInt32          uiProgram, 
313                                     bool            warnUnknown)
315     ShaderVariableMatrix *p = dynamic_cast<ShaderVariableMatrix *>(pVar);
317     if(loc > -1)
318     {
319         OSGGETGLFUNC_GL3_ES(glUniformMatrix4fv,
320                             osgGlUniformMatrix4fv,
321                             ShaderProgram::getFuncIdUniformMatrix4fv());
323         osgGlUniformMatrix4fv(loc,
324                               1,
325                               GL_FALSE,
326                               p->getValue().getValues());
327     }
328     else if(warnUnknown == true)
329     {
330         SWARNING << "Variable '" << p->getName() << "' type 'matrix' "
331                  << "not found in active uniform "
332                  << "variables of shader program '" << uiProgram << "'"
333                  << std::endl;
334     }
337 inline
338 void osgUniformShaderVariableMInt(DrawEnv        *pEnv,      
339                                   ShaderVariable *pVar,       
340                                   Int32          &loc,
341                                   UInt32          uiProgram, 
342                                   bool            warnUnknown)
344     ShaderVariableMInt *p = dynamic_cast<ShaderVariableMInt *>(pVar);
346     if(loc > -1 && !p->getMFValue()->empty())
347     {
348         OSGGETGLFUNC_GL3_ES(glUniform1iv,
349                             osgGlUniform1iv,
350                             ShaderProgram::getFuncIdUniform1iv());
351         
352         osgGlUniform1iv(  loc,
353                           p->getMFValue()->size32(),
354                         &(p->getMFValue()->front ()));
355     }
356     else if(warnUnknown == true)
357     {
358         SWARNING << "Variable '" << p->getName() << "' type 'int []' "
359                  << "not found in active uniform "
360                  << "variables of shader program '" << uiProgram << "'"
361                  << std::endl;
362     }
365 inline
366 void osgUniformShaderVariableMUInt(DrawEnv        *pEnv,
367                                    ShaderVariable *pVar,
368                                    Int32          &loc,
369                                    UInt32          uiProgram,
370                                    bool            warnUnknown)
372     if(!pEnv->getWindow()->hasExtOrVersion(
373         ShaderProgram::getExtIdGPUShader4(), 0x0303, 0x0200))
374     {
375         SWARNING << "Uniforms of type 'uint' are not supported, could not "
376                  << "find extension 'EXT_gpu_shader4'!"
377                  << std::endl;
378         return;
379     }
381     ShaderVariableMUInt *p = dynamic_cast<ShaderVariableMUInt *>(pVar);
383     if(loc > -1 && !p->getMFValue()->empty())
384     {
385         OSGGETGLFUNC_GL3_ES(glUniform1uiv,
386                             osgGlUniform1uiv,
387                             ShaderProgram::getFuncIdUniform1uiv());
389         osgGlUniform1uiv(  loc,
390                            p->getMFValue()->size32(),
391                          &(p->getMFValue()->front ()));
392     }
393     else if(warnUnknown == true)
394     {
395         SWARNING << "Variable '" << p->getName() << "' type 'uint []' "
396                  << "not found in active uniform "
397                  << "variables of shader program '" << uiProgram << "'"
398                  << std::endl;
399     }
402 inline
403 void osgUniformShaderVariableMReal(DrawEnv        *pEnv,      
404                                    ShaderVariable *pVar,       
405                                    Int32          &loc,
406                                    UInt32          uiProgram, 
407                                    bool            warnUnknown)
409     ShaderVariableMReal *p = dynamic_cast<ShaderVariableMReal *>(pVar);
411     if(loc > -1 && !p->getMFValue()->empty())
412     {
413         OSGGETGLFUNC_GL3_ES(glUniform1fv,
414                             osgGlUniform1fv,
415                             ShaderProgram::getFuncIdUniform1fv());
417         osgGlUniform1fv(  loc,
418                           p->getMFValue()->size32(),
419                         &(p->getMFValue()->front ()));
420     }
421     else if(warnUnknown == true)
422     {
423         SWARNING << "Variable '" << p->getName() << "' type 'real []' "
424                  << "not found in active uniform "
425                  << "variables of shader program '" << uiProgram << "'"
426                  << std::endl;
427     }
430 inline
431 void osgUniformShaderVariableMVec2f(DrawEnv        *pEnv,      
432                                     ShaderVariable *pVar,       
433                                     Int32          &loc,
434                                     UInt32          uiProgram, 
435                                     bool            warnUnknown)
437     ShaderVariableMVec2f *p = dynamic_cast<ShaderVariableMVec2f *>(pVar);
439     if(loc > -1 && !p->getMFValue()->empty())
440     {
441         OSGGETGLFUNC_GL3_ES(glUniform2fv,
442                             osgGlUniform2fv,
443                             ShaderProgram::getFuncIdUniform2fv());
445         osgGlUniform2fv(loc,
446                         p->getMFValue()->size32(),
447                         p->getMFValue()->front ().getValues());
448     }
449     else if(warnUnknown == true)
450     {
451         SWARNING << "Variable '" << p->getName() << "' type 'vec2f []' "
452                  << "not found in active uniform "
453                  << "variables of shader program '" << uiProgram << "'"
454                  << std::endl;
455     }
458 inline
459 void osgUniformShaderVariableMVec3f(DrawEnv        *pEnv,      
460                                     ShaderVariable *pVar,       
461                                     Int32          &loc,
462                                     UInt32          uiProgram, 
463                                     bool            warnUnknown)
465     ShaderVariableMVec3f *p = dynamic_cast<ShaderVariableMVec3f *>(pVar);
467     if(loc > -1 && !p->getMFValue()->empty())
468     {
469         OSGGETGLFUNC_GL3_ES(glUniform3fv,
470                             osgGlUniform3fv,
471                             ShaderProgram::getFuncIdUniform3fv());
473         osgGlUniform3fv(loc,
474                         p->getMFValue()->size32(),
475                         p->getMFValue()->front ().getValues());
476     }
477     else if(warnUnknown == true)
478     {
479         SWARNING << "Variable '" << p->getName() << "' type 'vec3f []' "
480                  << "not found in active uniform "
481                  << "variables of shader program '" << uiProgram << "'"
482                  << std::endl;
483     }
486 inline
487 void osgUniformShaderVariableMVec4f(DrawEnv        *pEnv,      
488                                     ShaderVariable *pVar,       
489                                     Int32          &loc,
490                                     UInt32          uiProgram, 
491                                     bool            warnUnknown)
493     ShaderVariableMVec4f *p = dynamic_cast<ShaderVariableMVec4f *>(pVar);
495     if(loc > -1 && !p->getMFValue()->empty())
496     {
497         OSGGETGLFUNC_GL3_ES(glUniform4fv,
498                             osgGlUniform4fv,
499                             ShaderProgram::getFuncIdUniform4fv());
501         osgGlUniform4fv(loc,
502                         p->getMFValue()->size32(),
503                         p->getMFValue()->front ().getValues());
504     }
505     else if(warnUnknown == true)
506     {
507         SWARNING << "Variable '" << p->getName() << "' type 'vec4f []' "
508                  << "not found in active uniform "
509                  << "variables of shader program '" << uiProgram << "'"
510                  << std::endl;
511     }
514 inline
515 void osgUniformShaderVariableMMatrix(DrawEnv        *pEnv,      
516                                      ShaderVariable *pVar,       
517                                      Int32          &loc,
518                                      UInt32          uiProgram, 
519                                      bool            warnUnknown)
521     ShaderVariableMMatrix *p = dynamic_cast<ShaderVariableMMatrix *>(pVar);
523     if(loc > -1 && !p->getMFValue()->empty())
524     {
525         OSGGETGLFUNC_GL3_ES(glUniformMatrix4fv,
526                             osgGlUniformMatrix4fv,
527                             ShaderProgram::getFuncIdUniformMatrix4fv());
529         osgGlUniformMatrix4fv(loc,
530                               p->getMFValue()->size32(),
531                               GL_FALSE,
532                               p->getMFValue()->front ().getValues());
533     }
534     else if(warnUnknown == true)
535     {
536         SWARNING << "Variable '" << p->getName() << "' type 'matrix []' "
537                  << "not found in active uniform "
538                  << "variables of shader program '" << uiProgram << "'"
539                  << std::endl;
540     }
543 inline
544 void osgUniformShaderUniformBlockLocation(DrawEnv        *pEnv, 
545                                           ShaderVariable *pVar, 
546                                           Int32          &loc, 
547                                           UInt32          uiProgram)
549     if(!pEnv->getWindow()->hasExtOrVersion(
550         ShaderProgram::getExtIdUniformBufferObject(), 0x0201, 0x0200))
551     {
552         SWARNING << "Uniform blocks are not supported, could not "
553                  << "find extension 'GL_ARB_uniform_buffer_object'!"
554                  << std::endl;
555         return;
556     }
558     if(loc == -1)
559     {
560         OSGGETGLFUNC_GL3_ES(glGetUniformBlockIndex,
561                             osgGlGetUniformBlockIndex,
562                             ShaderProgram::getFuncIdGetUniformBlockIndex());
564         loc = osgGlGetUniformBlockIndex(uiProgram, pVar->getName().c_str());
565 #ifdef OSG_MULTISHADER_VARCHUNK
566         if(loc == -1)
567             loc = -2;
568 #endif
569      }
572 inline
573 void osgUniformShaderUniformBlockBinding(DrawEnv        *pEnv,
574                                          ShaderVariable *pVar,
575                                          Int32          &loc,
576                                          UInt32          uiProgram,
577                                          bool            warnUnknown)
579     if(!pEnv->getWindow()->hasExtOrVersion(
580         ShaderProgram::getExtIdUniformBufferObject(), 0x0201, 0x0200))
581     {
582         SWARNING << "Uniform blocks are not supported, could not "
583                  << "find extension 'GL_ARB_uniform_buffer_object'!"
584                  << std::endl;
585         return;
586     }
588     ShaderVariableUniformBlock *p = dynamic_cast<ShaderVariableUniformBlock *>(pVar);
590     if(loc > -1)
591     {
592         OSGGETGLFUNC_GL3_ES(glUniformBlockBinding,
593                             osgGlUniformBlockBinding,
594                             ShaderProgram::getFuncIdUniformBlockBinding());
596         osgGlUniformBlockBinding(uiProgram, loc, p->getValue());
597     }
598     else if(warnUnknown == true)
599     {
600         SWARNING << "Block '" << p->getName() << "'" 
601                  << "not found in active shader program '" << uiProgram << "'" 
602                  << std::endl;
603     }
606 inline
607 void osgUniformShaderStorageBlockLocation(DrawEnv        *pEnv, 
608                                           ShaderVariable *pVar, 
609                                           Int32          &loc, 
610                                           UInt32          uiProgram)
612     if(!pEnv->getWindow()->hasExtOrVersion(
613         ShaderProgram::getExtIdProgramInterfaceQuery(), 0x0402))
614     {
615         SWARNING << "Shader Storage are not supported, could not "
616                  << "find extension 'GL_ARB_program_interface_query'!"
617                  << std::endl;
618         return;
619     }
621     if(!pEnv->getWindow()->hasExtOrVersion(
622         ShaderProgram::getExtIdShaderStorageBufferObject(), 0x0403))
623     {
624         SWARNING << "Shader Storage are not supported, could not "
625                  << "find extension 'GL_ARB_shader_storage_buffer_object'!"
626                  << std::endl;
627         return;
628     }
630     if(loc == -1)
631     {
632         OSGGETGLFUNC_GL3_ES(glGetProgramResourceIndex,
633                             osgGlGetProgramResourceIndex,
634                             ShaderProgram::getFuncIdGetProgramResourceIndex());
636         loc = osgGlGetProgramResourceIndex(uiProgram, GL_SHADER_STORAGE_BLOCK, pVar->getName().c_str());
637 #ifdef OSG_MULTISHADER_VARCHUNK
638         if(loc == -1)
639             loc = -2;
640 #endif
641      }
644 inline
645 void osgUniformShaderStorageBlockBinding(DrawEnv        *pEnv,
646                                          ShaderVariable *pVar,
647                                          Int32          &loc,
648                                          UInt32          uiProgram,
649                                          bool            warnUnknown)
651     if(!pEnv->getWindow()->hasExtOrVersion(
652         ShaderProgram::getExtIdShaderStorageBufferObject(), 0x0403))
653     {
654         SWARNING << "Shader Storage blocks are not supported, could not "
655                  << "find extension 'GL_ARB_shader_storage_buffer_object'!"
656                  << std::endl;
657         return;
658     }
660     ShaderVariableShaderStorageBlock *p = dynamic_cast<ShaderVariableShaderStorageBlock *>(pVar);
662     if(loc > -1)
663     {
664         OSGGETGLFUNC_GL3_ES(glShaderStorageBlockBinding,
665                             osgGlShaderStorageBlockBinding,
666                             ShaderProgram::getFuncIdShaderStorageBlockBinding());
668         osgGlShaderStorageBlockBinding(uiProgram, loc, p->getValue());
669     }
670     else if(warnUnknown == true)
671     {
672         SWARNING << "Block '" << p->getName() << "'" 
673                  << "not found in active shader program '" << uiProgram << "'" 
674                  << std::endl;
675     }
678 OSG_END_NAMESPACE