0027198: OSD_Environment - use wide characters API on Windows
[occt.git] / src / OpenGl / OpenGl_ShaderProgram.cxx
CommitLineData
30f0ad28 1// Created on: 2013-09-19
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
30f0ad28 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
30f0ad28 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
30f0ad28 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
30f0ad28 15
16#include <OSD_File.hxx>
17#include <OSD_Protection.hxx>
18
7d3e64ef 19#include <Graphic3d_Buffer.hxx>
30f0ad28 20#include <Standard_Assert.hxx>
21#include <Standard_Atomic.hxx>
22#include <TCollection_ExtendedString.hxx>
23
24#include <OpenGl_Context.hxx>
25#include <OpenGl_ShaderProgram.hxx>
26#include <OpenGl_ShaderManager.hxx>
25ef750e 27#include <OpenGl_ArbTexBindless.hxx>
30f0ad28 28
47e9c178 29#include <OpenGl_GlCore32.hxx>
30f0ad28 30
92efcf78 31IMPLEMENT_STANDARD_RTTIEXT(OpenGl_ShaderProgram,OpenGl_Resource)
32
30f0ad28 33OpenGl_VariableSetterSelector OpenGl_ShaderProgram::mySetterSelector = OpenGl_VariableSetterSelector();
34
35// Declare OCCT-specific OpenGL/GLSL shader variables
36Standard_CString OpenGl_ShaderProgram::PredefinedKeywords[] =
37{
12381341 38 "occModelWorldMatrix", // OpenGl_OCC_MODEL_WORLD_MATRIX
39 "occWorldViewMatrix", // OpenGl_OCC_WORLD_VIEW_MATRIX
40 "occProjectionMatrix", // OpenGl_OCC_PROJECTION_MATRIX
41 "occModelWorldMatrixInverse", // OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE
42 "occWorldViewMatrixInverse", // OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE
43 "occProjectionMatrixInverse", // OpenGl_OCC_PROJECTION_MATRIX_INVERSE
44 "occModelWorldMatrixTranspose", // OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE
45 "occWorldViewMatrixTranspose", // OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE
46 "occProjectionMatrixTranspose", // OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE
47 "occModelWorldMatrixInverseTranspose", // OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE
48 "occWorldViewMatrixInverseTranspose", // OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE
49 "occProjectionMatrixInverseTranspose", // OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE
50
51 "occClipPlaneEquations", // OpenGl_OCC_CLIP_PLANE_EQUATIONS
52 "occClipPlaneSpaces", // OpenGl_OCC_CLIP_PLANE_SPACES
5495fa7e 53 "occClipPlaneCount", // OpenGl_OCC_CLIP_PLANE_COUNT
12381341 54
55 "occLightSourcesCount", // OpenGl_OCC_LIGHT_SOURCE_COUNT
56 "occLightSourcesTypes", // OpenGl_OCC_LIGHT_SOURCE_TYPES
57 "occLightSources", // OpenGl_OCC_LIGHT_SOURCE_PARAMS
58 "occLightAmbient", // OpenGl_OCC_LIGHT_AMBIENT
59
60 "occActiveSampler", // OpenGl_OCCT_ACTIVE_SAMPLER
61 "occTextureEnable", // OpenGl_OCCT_TEXTURE_ENABLE
62 "occDistinguishingMode", // OpenGl_OCCT_DISTINGUISH_MODE
63 "occFrontMaterial", // OpenGl_OCCT_FRONT_MATERIAL
8625ef7e 64 "occBackMaterial", // OpenGl_OCCT_BACK_MATERIAL
65 "occColor", // OpenGl_OCCT_COLOR
66
79f4f036 67 "occTexTrsf2d", // OpenGl_OCCT_TEXTURE_TRSF2D
8625ef7e 68 "occPointSize" // OpenGl_OCCT_POINT_SIZE
30f0ad28 69
70};
71
72// =======================================================================
73// function : OpenGl_VariableSetterSelector
74// purpose : Creates new variable setter selector
75// =======================================================================
76OpenGl_VariableSetterSelector::OpenGl_VariableSetterSelector()
77{
78 // Note: Add new variable setters here
79 mySetterList = OpenGl_HashMapInitializer::CreateListOf<size_t, OpenGl_SetterInterface*>
80 (Graphic3d_UniformValueTypeID<int>::ID, new OpenGl_VariableSetter<int>())
81 (Graphic3d_UniformValueTypeID<float>::ID, new OpenGl_VariableSetter<float>())
82 (Graphic3d_UniformValueTypeID<OpenGl_Vec2>::ID, new OpenGl_VariableSetter<OpenGl_Vec2>())
83 (Graphic3d_UniformValueTypeID<OpenGl_Vec3>::ID, new OpenGl_VariableSetter<OpenGl_Vec3>())
84 (Graphic3d_UniformValueTypeID<OpenGl_Vec4>::ID, new OpenGl_VariableSetter<OpenGl_Vec4>())
85 (Graphic3d_UniformValueTypeID<OpenGl_Vec2i>::ID, new OpenGl_VariableSetter<OpenGl_Vec2i>())
86 (Graphic3d_UniformValueTypeID<OpenGl_Vec3i>::ID, new OpenGl_VariableSetter<OpenGl_Vec3i>())
87 (Graphic3d_UniformValueTypeID<OpenGl_Vec4i>::ID, new OpenGl_VariableSetter<OpenGl_Vec4i>());
88}
89
90// =======================================================================
91// function : ~OpenGl_VariableSetterSelector
92// purpose : Releases memory resources of variable setter selector
93// =======================================================================
94OpenGl_VariableSetterSelector::~OpenGl_VariableSetterSelector()
95{
96 for (OpenGl_SetterList::Iterator anIt (mySetterList); anIt.More(); anIt.Next())
97 {
98 delete anIt.Value();
99 }
100
101 mySetterList.Clear();
102}
103
104// =======================================================================
105// function : Set
106// purpose : Sets generic variable to specified shader program
107// =======================================================================
108void OpenGl_VariableSetterSelector::Set (const Handle(OpenGl_Context)& theCtx,
109 const Handle(Graphic3d_ShaderVariable)& theVariable,
110 OpenGl_ShaderProgram* theProgram) const
111{
112 Standard_ASSERT_RETURN (mySetterList.IsBound (theVariable->Value()->TypeID()),
113 "The type of user-defined uniform variable is not supported...", );
114
115 mySetterList.Find (theVariable->Value()->TypeID())->Set (theCtx, theVariable, theProgram);
116}
117
118// =======================================================================
119// function : OpenGl_ShaderProgram
120// purpose : Creates uninitialized shader program
121// =======================================================================
122OpenGl_ShaderProgram::OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy)
123: myProgramID (NO_PROGRAM),
392ac980 124 myProxy (theProxy),
125 myShareCount(1)
30f0ad28 126{
127 memset (myCurrentState, 0, sizeof (myCurrentState));
128 for (GLint aVar = 0; aVar < OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES; ++aVar)
129 {
130 myStateLocations[aVar] = INVALID_LOCATION;
131 }
132}
133
134// =======================================================================
135// function : Initialize
136// purpose : Initializes program object with the list of shader objects
137// =======================================================================
138Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)& theCtx,
139 const Graphic3d_ShaderObjectList& theShaders)
140{
141 if (theCtx.IsNull() || !Create (theCtx))
142 {
143 return Standard_False;
144 }
145
12381341 146 OSD_File aDeclFile (Graphic3d_ShaderProgram::ShadersFolder() + "/Declarations.glsl");
147 OSD_File aDeclImplFile (Graphic3d_ShaderProgram::ShadersFolder() + "/DeclarationsImpl.glsl");
148 if (!aDeclFile.Exists()
149 || !aDeclImplFile.Exists())
30f0ad28 150 {
392ac980 151 const TCollection_ExtendedString aMsg = "Error! Failed to load OCCT shader declarations file";
3b523c4c 152 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
153 GL_DEBUG_TYPE_ERROR,
30f0ad28 154 0,
3b523c4c 155 GL_DEBUG_SEVERITY_HIGH,
30f0ad28 156 aMsg);
30f0ad28 157 return Standard_False;
158 }
159
b86bb3df 160 TCollection_AsciiString aHeader = !myProxy.IsNull() && !myProxy->Header().IsEmpty()
161 ? (myProxy->Header() + "\n")
162 : TCollection_AsciiString();
163
30f0ad28 164 TCollection_AsciiString aDeclarations;
30f0ad28 165 aDeclFile.Open (OSD_ReadOnly, OSD_Protection());
bd0b3e60 166 aDeclFile.Read (aDeclarations, (int)aDeclFile.Size());
30f0ad28 167 aDeclFile.Close();
168
12381341 169 TCollection_AsciiString aDeclImpl;
170 aDeclImplFile.Open (OSD_ReadOnly, OSD_Protection());
171 aDeclImplFile.Read (aDeclImpl, (int)aDeclImplFile.Size());
172 aDeclImplFile.Close();
173 aDeclarations += aDeclImpl;
174
30f0ad28 175 for (Graphic3d_ShaderObjectList::Iterator anIter (theShaders);
176 anIter.More(); anIter.Next())
177 {
178 if (!anIter.Value()->IsDone())
179 {
392ac980 180 const TCollection_ExtendedString aMsg = "Error! Failed to get shader source";
3b523c4c 181 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
182 GL_DEBUG_TYPE_ERROR,
30f0ad28 183 0,
3b523c4c 184 GL_DEBUG_SEVERITY_HIGH,
30f0ad28 185 aMsg);
30f0ad28 186 return Standard_False;
187 }
188
189 Handle(OpenGl_ShaderObject) aShader;
190
191 // Note: Add support of other shader types here
192 switch (anIter.Value()->Type())
193 {
194 case Graphic3d_TOS_VERTEX:
195 aShader = new OpenGl_ShaderObject (GL_VERTEX_SHADER);
196 break;
197 case Graphic3d_TOS_FRAGMENT:
198 aShader = new OpenGl_ShaderObject (GL_FRAGMENT_SHADER);
199 break;
200 }
201
202 // Is unsupported shader type?
203 if (aShader.IsNull())
204 {
205 TCollection_ExtendedString aMsg = "Error! Unsupported shader type";
3b523c4c 206 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
207 GL_DEBUG_TYPE_ERROR,
30f0ad28 208 0,
3b523c4c 209 GL_DEBUG_SEVERITY_HIGH,
30f0ad28 210 aMsg);
30f0ad28 211 return Standard_False;
212 }
213
214 if (!aShader->Create (theCtx))
215 {
392ac980 216 aShader->Release (theCtx.operator->());
30f0ad28 217 return Standard_False;
218 }
219
220 TCollection_AsciiString aSource = aDeclarations + anIter.Value()->Source();
8625ef7e 221 switch (anIter.Value()->Type())
30f0ad28 222 {
8625ef7e 223 case Graphic3d_TOS_VERTEX:
224 {
b86bb3df 225 aSource = aHeader + TCollection_AsciiString ("#define VERTEX_SHADER\n") + aSource;
8625ef7e 226 break;
227 }
228 case Graphic3d_TOS_FRAGMENT:
229 {
230 #if defined(GL_ES_VERSION_2_0)
231 TCollection_AsciiString aPrefix (theCtx->hasHighp
232 ? "precision highp float;\n"
8a53d1c4 233 "precision highp int;\n"
234 : "precision mediump float;\n"
235 "precision mediump int;\n");
b86bb3df 236 aSource = aHeader + aPrefix + aSource;
4e1523ef 237 #else
238 aSource = aHeader + aSource;
8625ef7e 239 #endif
240 break;
241 }
30f0ad28 242 }
243
244 if (!aShader->LoadSource (theCtx, aSource))
245 {
392ac980 246 const TCollection_ExtendedString aMsg = "Error! Failed to set shader source";
3b523c4c 247 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
248 GL_DEBUG_TYPE_ERROR,
30f0ad28 249 0,
3b523c4c 250 GL_DEBUG_SEVERITY_HIGH,
30f0ad28 251 aMsg);
392ac980 252 aShader->Release (theCtx.operator->());
30f0ad28 253 return Standard_False;
254 }
255
256 if (!aShader->Compile (theCtx))
257 {
392ac980 258 TCollection_AsciiString aLog;
259 aShader->FetchInfoLog (theCtx, aLog);
260 if (aLog.IsEmpty())
261 {
262 aLog = "Compilation log is empty.";
263 }
3b523c4c 264 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
265 GL_DEBUG_TYPE_ERROR,
30f0ad28 266 0,
3b523c4c 267 GL_DEBUG_SEVERITY_HIGH,
392ac980 268 TCollection_ExtendedString ("Failed to compile shader object. Compilation log:\n") + aLog);
269 aShader->Release (theCtx.operator->());
270 return Standard_False;
271 }
272 else if (theCtx->caps->glslWarnings)
273 {
274 TCollection_AsciiString aLog;
275 aShader->FetchInfoLog (theCtx, aLog);
276 if (!aLog.IsEmpty()
277 && !aLog.IsEqual ("No errors.\n"))
30f0ad28 278 {
3b523c4c 279 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
280 GL_DEBUG_TYPE_PORTABILITY,
392ac980 281 0,
3b523c4c 282 GL_DEBUG_SEVERITY_LOW,
392ac980 283 TCollection_ExtendedString ("Shader compilation log:\n") + aLog);
30f0ad28 284 }
30f0ad28 285 }
286
287 if (!AttachShader (theCtx, aShader))
288 {
392ac980 289 aShader->Release (theCtx.operator->());
30f0ad28 290 return Standard_False;
291 }
292 }
293
7d3e64ef 294 // bind locations for pre-defined Vertex Attributes
295 SetAttributeName (theCtx, Graphic3d_TOA_POS, "occVertex");
296 SetAttributeName (theCtx, Graphic3d_TOA_NORM, "occNormal");
297 SetAttributeName (theCtx, Graphic3d_TOA_UV, "occTexCoord");
8625ef7e 298 SetAttributeName (theCtx, Graphic3d_TOA_COLOR, "occVertColor");
7d3e64ef 299
4a535d3f 300 // bind custom Vertex Attributes
301 if (!myProxy.IsNull())
302 {
303 for (Graphic3d_ShaderAttributeList::Iterator anAttribIter (myProxy->VertexAttributes());
304 anAttribIter.More(); anAttribIter.Next())
305 {
306 SetAttributeName (theCtx, anAttribIter.Value()->Location(), anAttribIter.Value()->Name().ToCString());
307 }
308 }
309
30f0ad28 310 if (!Link (theCtx))
311 {
392ac980 312 TCollection_AsciiString aLog;
313 FetchInfoLog (theCtx, aLog);
314 if (aLog.IsEmpty())
315 {
316 aLog = "Linker log is empty.";
317 }
3b523c4c 318 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
319 GL_DEBUG_TYPE_ERROR,
30f0ad28 320 0,
3b523c4c 321 GL_DEBUG_SEVERITY_HIGH,
12381341 322 TCollection_ExtendedString ("Failed to link program object! Linker log:\n") + aLog);
392ac980 323 return Standard_False;
324 }
325 else if (theCtx->caps->glslWarnings)
326 {
327 TCollection_AsciiString aLog;
328 FetchInfoLog (theCtx, aLog);
329 if (!aLog.IsEmpty()
330 && !aLog.IsEqual ("No errors.\n"))
30f0ad28 331 {
3b523c4c 332 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
333 GL_DEBUG_TYPE_PORTABILITY,
392ac980 334 0,
3b523c4c 335 GL_DEBUG_SEVERITY_LOW,
392ac980 336 TCollection_ExtendedString ("GLSL linker log:\n") + aLog);
30f0ad28 337 }
30f0ad28 338 }
339
340 return Standard_True;
341}
342
343// =======================================================================
344// function : ~OpenGl_ShaderProgram
345// purpose : Releases resources of shader program
346// =======================================================================
347OpenGl_ShaderProgram::~OpenGl_ShaderProgram()
348{
349 Release (NULL);
350}
351
352// =======================================================================
353// function : AttachShader
354// purpose : Attaches shader object to the program object
355// =======================================================================
356Standard_Boolean OpenGl_ShaderProgram::AttachShader (const Handle(OpenGl_Context)& theCtx,
357 const Handle(OpenGl_ShaderObject)& theShader)
358{
359 if (myProgramID == NO_PROGRAM || theShader.IsNull())
360 {
361 return Standard_False;
362 }
363
364 for (OpenGl_ShaderList::Iterator anIter (myShaderObjects); anIter.More(); anIter.Next())
365 {
366 if (theShader == anIter.Value())
367 {
368 return Standard_False;
369 }
370 }
371
372 myShaderObjects.Append (theShader);
4e1523ef 373 theCtx->core20fwd->glAttachShader (myProgramID, theShader->myShaderID);
30f0ad28 374 return Standard_True;
375}
376
377// =======================================================================
378// function : DetachShader
379// purpose : Detaches shader object to the program object
380// =======================================================================
381Standard_Boolean OpenGl_ShaderProgram::DetachShader (const Handle(OpenGl_Context)& theCtx,
382 const Handle(OpenGl_ShaderObject)& theShader)
383{
384 if (myProgramID == NO_PROGRAM
385 || theShader.IsNull())
386 {
387 return Standard_False;
388 }
389
390 OpenGl_ShaderList::Iterator anIter (myShaderObjects);
391 while (anIter.More())
392 {
393 if (theShader == anIter.Value())
394 {
395 myShaderObjects.Remove (anIter);
396 break;
397 }
398
399 anIter.Next();
400 }
401
402 if (!anIter.More())
403 {
404 return Standard_False;
405 }
406
4e1523ef 407 theCtx->core20fwd->glDetachShader (myProgramID, theShader->myShaderID);
30f0ad28 408 return Standard_True;
409}
410
411// =======================================================================
412// function : Link
413// purpose : Links the program object
414// =======================================================================
415Standard_Boolean OpenGl_ShaderProgram::Link (const Handle(OpenGl_Context)& theCtx)
416{
417 if (myProgramID == NO_PROGRAM)
418 {
419 return Standard_False;
420 }
421
30f0ad28 422 GLint aStatus = GL_FALSE;
4e1523ef 423 theCtx->core20fwd->glLinkProgram (myProgramID);
424 theCtx->core20fwd->glGetProgramiv (myProgramID, GL_LINK_STATUS, &aStatus);
fc73a202 425 if (aStatus == GL_FALSE)
426 {
427 return Standard_False;
428 }
30f0ad28 429
430 for (GLint aVar = 0; aVar < OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES; ++aVar)
431 {
432 myStateLocations[aVar] = GetUniformLocation (theCtx, PredefinedKeywords[aVar]);
433 }
fc73a202 434 return Standard_True;
30f0ad28 435}
436
437// =======================================================================
438// function : FetchInfoLog
439// purpose : Fetches information log of the last link operation
440// =======================================================================
441Standard_Boolean OpenGl_ShaderProgram::FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
442 TCollection_AsciiString& theOutput)
443{
444 if (myProgramID == NO_PROGRAM)
445 {
446 return Standard_False;
447 }
448
449 GLint aLength = 0;
4e1523ef 450 theCtx->core20fwd->glGetProgramiv (myProgramID, GL_INFO_LOG_LENGTH, &aLength);
30f0ad28 451 if (aLength > 0)
452 {
453 GLchar* aLog = (GLchar*) alloca (aLength);
454 memset (aLog, 0, aLength);
4e1523ef 455 theCtx->core20fwd->glGetProgramInfoLog (myProgramID, aLength, NULL, aLog);
30f0ad28 456 theOutput = aLog;
457 }
458 return Standard_True;
459}
460
461// =======================================================================
30f0ad28 462// function : ApplyVariables
463// purpose : Fetches uniform variables from proxy shader program
464// =======================================================================
465Standard_Boolean OpenGl_ShaderProgram::ApplyVariables(const Handle(OpenGl_Context)& theCtx)
466{
467 if (myProxy.IsNull() || myProxy->Variables().IsEmpty())
468 {
469 return Standard_False;
470 }
471
472 for (Graphic3d_ShaderVariableList::Iterator anIter (myProxy->Variables()); anIter.More(); anIter.Next())
473 {
474 mySetterSelector.Set (theCtx, anIter.Value(), this);
475 }
476
477 myProxy->ClearVariables();
478 return Standard_True;
479}
480
481// =======================================================================
30f0ad28 482// function : ActiveState
483// purpose : Returns index of last modification for specified state type
484// =======================================================================
485Standard_Size OpenGl_ShaderProgram::ActiveState (const OpenGl_UniformStateType theType) const
486{
487 if (theType < MaxStateTypes)
488 {
489 return myCurrentState[theType];
490 }
491 return 0;
492}
493
494// =======================================================================
495// function : UpdateState
496// purpose : Updates index of last modification for specified state type
497// =======================================================================
498void OpenGl_ShaderProgram::UpdateState (const OpenGl_UniformStateType theType,
499 const Standard_Size theIndex)
500{
501 if (theType < MaxStateTypes)
502 {
503 myCurrentState[theType] = theIndex;
504 }
505}
506
507// =======================================================================
508// function : GetUniformLocation
509// purpose : Returns location (index) of the specific uniform variable
510// =======================================================================
511GLint OpenGl_ShaderProgram::GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
512 const GLchar* theName) const
513{
514 return myProgramID != NO_PROGRAM
4e1523ef 515 ? theCtx->core20fwd->glGetUniformLocation (myProgramID, theName)
30f0ad28 516 : INVALID_LOCATION;
517}
518
519// =======================================================================
520// function : GetAttributeLocation
521// purpose : Returns location (index) of the generic vertex attribute
522// =======================================================================
523GLint OpenGl_ShaderProgram::GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
524 const GLchar* theName) const
525{
526 return myProgramID != NO_PROGRAM
4e1523ef 527 ? theCtx->core20fwd->glGetAttribLocation (myProgramID, theName)
30f0ad28 528 : INVALID_LOCATION;
529}
530
531// =======================================================================
532// function : GetStateLocation
533// purpose : Returns location of the OCCT state uniform variable
534// =======================================================================
535GLint OpenGl_ShaderProgram::GetStateLocation (const GLuint theVariable) const
536{
537 if (theVariable < OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES)
538 {
539 return myStateLocations[theVariable];
540 }
541 return INVALID_LOCATION;
542}
543
544// =======================================================================
545// function : GetUniform
546// purpose : Returns the value of the integer uniform variable
547// =======================================================================
548Standard_Boolean OpenGl_ShaderProgram::GetUniform (const Handle(OpenGl_Context)& theCtx,
549 const GLchar* theName,
550 OpenGl_Vec4i& theValue) const
551{
552 return GetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
553}
554
555// =======================================================================
556// function : GetUniform
557// purpose : Returns the value of the integer uniform variable
558// =======================================================================
559Standard_Boolean OpenGl_ShaderProgram::GetUniform (const Handle(OpenGl_Context)& theCtx,
560 GLint theLocation,
561 OpenGl_Vec4i& theValue) const
562{
563 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
564 {
565 return Standard_False;
566 }
567
4e1523ef 568 theCtx->core20fwd->glGetUniformiv (myProgramID, theLocation, theValue);
30f0ad28 569 return Standard_True;
570}
571
572// =======================================================================
573// function : GetUniform
574// purpose : Returns the value of the floating-point uniform variable
575// =======================================================================
576Standard_Boolean OpenGl_ShaderProgram::GetUniform (const Handle(OpenGl_Context)& theCtx,
577 const GLchar* theName,
578 OpenGl_Vec4& theValue) const
579{
580 return GetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
581}
582
583// =======================================================================
584// function : GetUniform
585// purpose : Returns the value of the floating-point uniform variable
586// =======================================================================
587Standard_Boolean OpenGl_ShaderProgram::GetUniform (const Handle(OpenGl_Context)& theCtx,
588 GLint theLocation,
589 OpenGl_Vec4& theValue) const
590{
591 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
592 {
593 return Standard_False;
594 }
595
4e1523ef 596 theCtx->core20fwd->glGetUniformfv (myProgramID, theLocation, theValue);
30f0ad28 597 return Standard_True;
598}
599
600// =======================================================================
601// function : GetAttribute
602// purpose : Returns the integer vertex attribute
603// =======================================================================
604Standard_Boolean OpenGl_ShaderProgram::GetAttribute (const Handle(OpenGl_Context)& theCtx,
605 const GLchar* theName,
606 OpenGl_Vec4i& theValue) const
607{
608 return GetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
609}
610
611// =======================================================================
612// function : GetAttribute
613// purpose : Returns the integer vertex attribute
614// =======================================================================
615Standard_Boolean OpenGl_ShaderProgram::GetAttribute (const Handle(OpenGl_Context)& theCtx,
616 GLint theIndex,
617 OpenGl_Vec4i& theValue) const
618{
619 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
620 {
621 return Standard_False;
622 }
623
4e1523ef 624 theCtx->core20fwd->glGetVertexAttribiv (theIndex, GL_CURRENT_VERTEX_ATTRIB, theValue);
30f0ad28 625 return Standard_True;
626}
627
628// =======================================================================
629// function : GetAttribute
630// purpose : Returns the floating-point vertex attribute
631// =======================================================================
632Standard_Boolean OpenGl_ShaderProgram::GetAttribute (const Handle(OpenGl_Context)& theCtx,
633 const GLchar* theName,
634 OpenGl_Vec4& theValue) const
635{
636 return GetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
637}
638
639// =======================================================================
640// function : GetAttribute
641// purpose : Returns the floating-point vertex attribute
642// =======================================================================
643Standard_Boolean OpenGl_ShaderProgram::GetAttribute (const Handle(OpenGl_Context)& theCtx,
644 GLint theIndex,
645 OpenGl_Vec4& theValue) const
646{
647 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
648 {
649 return Standard_False;
650 }
651
4e1523ef 652 theCtx->core20fwd->glGetVertexAttribfv (theIndex, GL_CURRENT_VERTEX_ATTRIB, theValue);
30f0ad28 653 return Standard_True;
654}
655
656// =======================================================================
fc73a202 657// function : SetAttributeName
658// purpose :
659// =======================================================================
660Standard_Boolean OpenGl_ShaderProgram::SetAttributeName (const Handle(OpenGl_Context)& theCtx,
661 GLint theIndex,
662 const GLchar* theName)
663{
664 theCtx->core20fwd->glBindAttribLocation (myProgramID, theIndex, theName);
665 return Standard_True;
666}
b86bb3df 667
fc73a202 668// =======================================================================
669// function : SetAttribute
670// purpose :
671// =======================================================================
672Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
673 const GLchar* theName,
674 GLfloat theValue)
675{
676 return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
677}
678
679// =======================================================================
680// function : SetAttribute
681// purpose :
682// =======================================================================
683Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
684 GLint theIndex,
685 GLfloat theValue)
686{
687 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
688 {
689 return Standard_False;
690 }
691
692 theCtx->core20fwd->glVertexAttrib1f (theIndex, theValue);
693 return Standard_True;
694}
695
696// =======================================================================
697// function : SetAttribute
698// purpose :
699// =======================================================================
700Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
701 const GLchar* theName,
702 const OpenGl_Vec2& theValue)
703{
704 return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
705}
706
707// =======================================================================
708// function : SetAttribute
709// purpose :
710// =======================================================================
711Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
712 GLint theIndex,
713 const OpenGl_Vec2& theValue)
714{
715 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
716 {
717 return Standard_False;
718 }
719
720 theCtx->core20fwd->glVertexAttrib2fv (theIndex, theValue);
721 return Standard_True;
722}
723
724// =======================================================================
725// function : SetAttribute
726// purpose :
727// =======================================================================
728Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
729 const GLchar* theName,
730 const OpenGl_Vec3& theValue)
731{
732 return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
733}
734
735// =======================================================================
736// function : SetAttribute
737// purpose :
738// =======================================================================
739Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
740 GLint theIndex,
741 const OpenGl_Vec3& theValue)
742{
743 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
744 {
745 return Standard_False;
746 }
747
748 theCtx->core20fwd->glVertexAttrib3fv (theIndex, theValue);
749 return Standard_True;
750}
751
752// =======================================================================
753// function : SetAttribute
754// purpose :
755// =======================================================================
756Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
757 const GLchar* theName,
758 const OpenGl_Vec4& theValue)
759{
760 return SetAttribute (theCtx, GetAttributeLocation (theCtx, theName), theValue);
761}
762
763// =======================================================================
764// function : SetAttribute
765// purpose :
766// =======================================================================
767Standard_Boolean OpenGl_ShaderProgram::SetAttribute (const Handle(OpenGl_Context)& theCtx,
768 GLint theIndex,
769 const OpenGl_Vec4& theValue)
770{
771 if (myProgramID == NO_PROGRAM || theIndex == INVALID_LOCATION)
772 {
773 return Standard_False;
774 }
775
776 theCtx->core20fwd->glVertexAttrib4fv (theIndex, theValue);
777 return Standard_True;
778}
779
780// =======================================================================
30f0ad28 781// function : SetUniform
782// purpose : Specifies the value of the integer uniform variable
783// =======================================================================
784Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
785 const GLchar* theName,
786 GLint theValue)
787{
788 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
789}
790
791// =======================================================================
792// function : SetUniform
793// purpose : Specifies the value of the integer uniform variable
794// =======================================================================
795Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
796 GLint theLocation,
797 GLint theValue)
798{
799 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
800 {
801 return Standard_False;
802 }
803
4e1523ef 804 theCtx->core20fwd->glUniform1i (theLocation, theValue);
30f0ad28 805 return Standard_True;
806}
807
808// =======================================================================
809// function : SetUniform
47e9c178 810// purpose :
25ef750e 811// =======================================================================
812Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
813 const GLchar* theName,
47e9c178 814 const OpenGl_Vec2u& theValue)
25ef750e 815{
816 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
817}
818
819// =======================================================================
820// function : SetUniform
47e9c178 821// purpose :
25ef750e 822// =======================================================================
823Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
824 GLint theLocation,
47e9c178 825 const OpenGl_Vec2u& theValue)
25ef750e 826{
47e9c178 827 if (theCtx->core32 == NULL || myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
25ef750e 828 {
829 return Standard_False;
830 }
831
832#if !defined(GL_ES_VERSION_2_0)
47e9c178 833 theCtx->core32->glUniform2uiv (theLocation, 1, theValue.GetData());
25ef750e 834 return Standard_True;
20aeeb7b 835#else
836 (void )theValue;
837 return Standard_False;
838#endif
25ef750e 839}
840
841// =======================================================================
842// function : SetUniform
47e9c178 843// purpose :
25ef750e 844// =======================================================================
845Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
846 const GLchar* theName,
847 const GLsizei theCount,
47e9c178 848 const OpenGl_Vec2u* theValue)
25ef750e 849{
850 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theCount, theValue);
851}
852
853// =======================================================================
854// function : SetUniform
47e9c178 855// purpose :
25ef750e 856// =======================================================================
857Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
858 GLint theLocation,
859 const GLsizei theCount,
47e9c178 860 const OpenGl_Vec2u* theValue)
25ef750e 861{
47e9c178 862 if (theCtx->core32 == NULL || myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
25ef750e 863 {
864 return Standard_False;
865 }
866
867#if !defined(GL_ES_VERSION_2_0)
47e9c178 868 theCtx->core32->glUniform2uiv (theLocation, theCount, theValue->GetData());
25ef750e 869 return Standard_True;
20aeeb7b 870#else
871 (void )theCount;
872 (void )theValue;
873 return Standard_False;
874#endif
25ef750e 875}
876
877// =======================================================================
878// function : SetUniform
30f0ad28 879// purpose : Specifies the value of the floating-point uniform variable
880// =======================================================================
881Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
882 const GLchar* theName,
883 GLfloat theValue)
884{
885 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
886}
887
888// =======================================================================
889// function : SetUniform
890// purpose : Specifies the value of the floating-point uniform variable
891// =======================================================================
892Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
893 GLint theLocation,
894 GLfloat theValue)
895{
896 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
897 {
898 return Standard_False;
899 }
900
4e1523ef 901 theCtx->core20fwd->glUniform1f (theLocation, theValue);
30f0ad28 902 return Standard_True;
903}
904
905// =======================================================================
906// function : SetUniform
907// purpose : Specifies the value of the integer uniform 2D vector
908// =======================================================================
909Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
910 const GLchar* theName,
911 const OpenGl_Vec2i& theValue)
912{
913 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
914}
915
916// =======================================================================
917// function : SetUniform
918// purpose : Specifies the value of the integer uniform 2D vector
919// =======================================================================
920Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
921 GLint theLocation,
922 const OpenGl_Vec2i& theValue)
923{
924 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
925 {
926 return Standard_False;
927 }
928
4e1523ef 929 theCtx->core20fwd->glUniform2iv (theLocation, 1, theValue);
30f0ad28 930 return Standard_True;
931}
932
933// =======================================================================
934// function : SetUniform
935// purpose : Specifies the value of the integer uniform 3D vector
936// =======================================================================
937Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
938 const GLchar* theName,
939 const OpenGl_Vec3i& theValue)
940{
941 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
942}
943
944// =======================================================================
945// function : SetUniform
946// purpose : Specifies the value of the integer uniform 3D vector
947// =======================================================================
948Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
949 GLint theLocation,
950 const OpenGl_Vec3i& theValue)
951{
952 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
953 {
954 return Standard_False;
955 }
956
4e1523ef 957 theCtx->core20fwd->glUniform3iv (theLocation, 1, theValue);
30f0ad28 958 return Standard_True;
959}
960
961// =======================================================================
962// function : SetUniform
963// purpose : Specifies the value of the integer uniform 4D vector
964// =======================================================================
965Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
966 const GLchar* theName,
967 const OpenGl_Vec4i& theValue)
968{
969 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
970}
971
972// =======================================================================
973// function : SetUniform
974// purpose : Specifies the value of the integer uniform 4D vector
975// =======================================================================
976Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
977 GLint theLocation,
978 const OpenGl_Vec4i& theValue)
979{
980 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
981 {
982 return Standard_False;
983 }
984
4e1523ef 985 theCtx->core20fwd->glUniform4iv (theLocation, 1, theValue);
30f0ad28 986 return Standard_True;
987}
988
989// =======================================================================
990// function : SetUniform
991// purpose : Specifies the value of the floating-point uniform 2D vector
992// =======================================================================
993Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
994 const GLchar* theName,
995 const OpenGl_Vec2& theValue)
996{
997 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
998}
999
1000// =======================================================================
1001// function : SetUniform
1002// purpose : Specifies the value of the floating-point uniform 2D vector
1003// =======================================================================
1004Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1005 GLint theLocation,
1006 const OpenGl_Vec2& theValue)
1007{
1008 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1009 {
1010 return Standard_False;
1011 }
1012
4e1523ef 1013 theCtx->core20fwd->glUniform2fv (theLocation, 1, theValue);
30f0ad28 1014 return Standard_True;
1015}
1016
1017// =======================================================================
1018// function : SetUniform
1019// purpose : Specifies the value of the floating-point uniform 3D vector
1020// =======================================================================
1021Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1022 const GLchar* theName,
1023 const OpenGl_Vec3& theValue)
1024{
1025 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
1026}
1027
1028// =======================================================================
1029// function : SetUniform
1030// purpose : Specifies the value of the floating-point uniform 3D vector
1031// =======================================================================
1032Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1033 GLint theLocation,
1034 const OpenGl_Vec3& theValue)
1035{
1036 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1037 {
1038 return Standard_False;
1039 }
1040
4e1523ef 1041 theCtx->core20fwd->glUniform3fv (theLocation, 1, theValue);
30f0ad28 1042 return Standard_True;
1043}
1044
1045// =======================================================================
1046// function : SetUniform
1047// purpose : Specifies the value of the floating-point uniform 4D vector
1048// =======================================================================
1049Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1050 const GLchar* theName,
1051 const OpenGl_Vec4& theValue)
1052{
1053 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue);
1054}
1055
1056// =======================================================================
1057// function : SetUniform
1058// purpose : Specifies the value of the floating-point uniform 4D vector
1059// =======================================================================
1060Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1061 GLint theLocation,
1062 const OpenGl_Vec4& theValue)
1063{
1064 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1065 {
1066 return Standard_False;
1067 }
1068
4e1523ef 1069 theCtx->core20fwd->glUniform4fv (theLocation, 1, theValue);
30f0ad28 1070 return Standard_True;
1071}
1072
1073// =======================================================================
1074// function : SetUniform
1075// purpose : Specifies the value of the floating-point uniform 4x4 matrix
1076// =======================================================================
1077Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1078 const GLchar* theName,
25ef750e 1079 const OpenGl_Mat4& theValue,
1080 GLboolean theTranspose)
1081{
1082 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue, theTranspose);
1083}
1084
1085// =======================================================================
1086// function : SetUniform
1087// purpose : Specifies the value of the floating-point uniform 4x4 matrix
1088// =======================================================================
1089Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1090 GLint theLocation,
1091 const OpenGl_Mat4& theValue,
1092 GLboolean theTranspose)
1093{
1094 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1095 {
1096 return Standard_False;
1097 }
1098
4e1523ef 1099 theCtx->core20fwd->glUniformMatrix4fv (theLocation, 1, GL_FALSE, theTranspose ? theValue.Transposed() : theValue);
25ef750e 1100 return Standard_True;
1101}
1102
1103// =======================================================================
1104// function : SetUniform
1105// purpose : Specifies the value of the floating-point uniform 4x4 matrix
1106// =======================================================================
1107Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1108 const GLchar* theName,
30f0ad28 1109 const OpenGl_Matrix& theValue,
1110 GLboolean theTranspose)
1111{
1112 return SetUniform (theCtx, GetUniformLocation (theCtx, theName), theValue, theTranspose);
1113}
1114
1115// =======================================================================
1116// function : SetUniform
1117// purpose : Specifies the value of the floating-point uniform 4x4 matrix
1118// =======================================================================
1119Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1120 GLint theLocation,
1121 const OpenGl_Matrix& theValue,
1122 GLboolean theTranspose)
1123{
c827ea3a 1124 return SetUniform (theCtx, theLocation, OpenGl_Mat4::Map (*theValue.mat), theTranspose);
30f0ad28 1125}
1126
1127// =======================================================================
4fe9ad57 1128// function : SetUniform
1129// purpose : Specifies the value of the float uniform array
1130// =======================================================================
1131Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1132 GLint theLocation,
1133 GLuint theCount,
1134 const Standard_ShortReal* theData)
1135{
1136 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1137 {
1138 return Standard_False;
1139 }
1140
4e1523ef 1141 theCtx->core20fwd->glUniform1fv (theLocation, theCount, theData);
4fe9ad57 1142 return Standard_True;
1143}
1144
1145// =======================================================================
1146// function : SetUniform
1147// purpose : Specifies the value of the float2 uniform array
1148// =======================================================================
1149Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1150 GLint theLocation,
1151 GLuint theCount,
1152 const OpenGl_Vec2* theData)
1153{
1154 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1155 {
1156 return Standard_False;
1157 }
1158
4e1523ef 1159 theCtx->core20fwd->glUniform2fv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1160 return Standard_True;
1161}
1162
1163// =======================================================================
1164// function : SetUniform
1165// purpose : Specifies the value of the float3 uniform array
1166// =======================================================================
1167Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1168 GLint theLocation,
1169 GLuint theCount,
1170 const OpenGl_Vec3* theData)
1171{
1172 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1173 {
1174 return Standard_False;
1175 }
1176
4e1523ef 1177 theCtx->core20fwd->glUniform3fv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1178 return Standard_True;
1179}
1180
1181// =======================================================================
1182// function : SetUniform
1183// purpose : Specifies the value of the float4 uniform array
1184// =======================================================================
1185Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1186 GLint theLocation,
1187 GLuint theCount,
1188 const OpenGl_Vec4* theData)
1189{
1190 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1191 {
1192 return Standard_False;
1193 }
1194
4e1523ef 1195 theCtx->core20fwd->glUniform4fv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1196 return Standard_True;
1197}
1198
1199// =======================================================================
1200// function : SetUniform
1201// purpose : Specifies the value of the integer uniform array
1202// =======================================================================
1203Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1204 GLint theLocation,
1205 GLuint theCount,
1206 const Standard_Integer* theData)
1207{
1208 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1209 {
1210 return Standard_False;
1211 }
1212
4e1523ef 1213 theCtx->core20fwd->glUniform1iv (theLocation, theCount, theData);
4fe9ad57 1214 return Standard_True;
1215}
1216
1217// =======================================================================
1218// function : SetUniform
1219// purpose : Specifies the value of the int2 uniform array
1220// =======================================================================
1221Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1222 GLint theLocation,
1223 GLuint theCount,
1224 const OpenGl_Vec2i* theData)
1225{
1226 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1227 {
1228 return Standard_False;
1229 }
1230
4e1523ef 1231 theCtx->core20fwd->glUniform2iv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1232 return Standard_True;
1233}
1234
1235// =======================================================================
1236// function : SetUniform
1237// purpose : Specifies the value of the int3 uniform array
1238// =======================================================================
1239Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1240 GLint theLocation,
1241 GLuint theCount,
1242 const OpenGl_Vec3i* theData)
1243{
1244 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1245 {
1246 return Standard_False;
1247 }
1248
4e1523ef 1249 theCtx->core20fwd->glUniform3iv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1250 return Standard_True;
1251}
1252
1253// =======================================================================
1254// function : SetUniform
1255// purpose : Specifies the value of the int4 uniform array
1256// =======================================================================
1257Standard_Boolean OpenGl_ShaderProgram::SetUniform (const Handle(OpenGl_Context)& theCtx,
1258 GLint theLocation,
1259 GLuint theCount,
1260 const OpenGl_Vec4i* theData)
1261{
1262 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1263 {
1264 return Standard_False;
1265 }
1266
4e1523ef 1267 theCtx->core20fwd->glUniform4iv (theLocation, theCount, theData[0].GetData());
4fe9ad57 1268 return Standard_True;
1269}
1270
1271// =======================================================================
30f0ad28 1272// function : SetSampler
1273// purpose : Specifies the value of the sampler uniform variable
1274// =======================================================================
1275Standard_Boolean OpenGl_ShaderProgram::SetSampler (const Handle(OpenGl_Context)& theCtx,
1276 const GLchar* theName,
1277 const GLenum theTextureUnit)
1278{
1279 return SetSampler (theCtx, GetUniformLocation (theCtx, theName), theTextureUnit);
1280}
1281
1282// =======================================================================
1283// function : SetSampler
1284// purpose : Specifies the value of the sampler uniform variable
1285// =======================================================================
1286Standard_Boolean OpenGl_ShaderProgram::SetSampler (const Handle(OpenGl_Context)& theCtx,
1287 GLint theLocation,
1288 const GLenum theTextureUnit)
1289{
1290 if (myProgramID == NO_PROGRAM || theLocation == INVALID_LOCATION)
1291 {
1292 return Standard_False;
1293 }
1294
4e1523ef 1295 theCtx->core20fwd->glUniform1i (theLocation, theTextureUnit);
30f0ad28 1296 return Standard_True;
1297}
1298
1299// =======================================================================
1300// function : Create
1301// purpose : Creates new empty shader program of specified type
1302// =======================================================================
1303Standard_Boolean OpenGl_ShaderProgram::Create (const Handle(OpenGl_Context)& theCtx)
1304{
1305 if (myProgramID == NO_PROGRAM
4e1523ef 1306 && theCtx->core20fwd != NULL)
30f0ad28 1307 {
4e1523ef 1308 myProgramID = theCtx->core20fwd->glCreateProgram();
30f0ad28 1309 }
1310
1311 return myProgramID != NO_PROGRAM;
1312}
1313
1314// =======================================================================
1315// function : Release
1316// purpose : Destroys shader program
1317// =======================================================================
10b9c7df 1318void OpenGl_ShaderProgram::Release (OpenGl_Context* theCtx)
30f0ad28 1319{
1320 if (myProgramID == NO_PROGRAM)
1321 {
1322 return;
1323 }
1324
1325 Standard_ASSERT_RETURN (theCtx != NULL,
1326 "OpenGl_ShaderProgram destroyed without GL context! Possible GPU memory leakage...",);
1327
1328 for (OpenGl_ShaderList::Iterator anIter (myShaderObjects); anIter.More(); anIter.Next())
1329 {
fc73a202 1330 if (!anIter.Value().IsNull())
1331 {
1332 anIter.ChangeValue()->Release (theCtx);
1333 anIter.ChangeValue().Nullify();
1334 }
30f0ad28 1335 }
1336
4e1523ef 1337 if (theCtx->core20fwd != NULL
ec2eeb2d 1338 && theCtx->IsValid())
30f0ad28 1339 {
4e1523ef 1340 theCtx->core20fwd->glDeleteProgram (myProgramID);
30f0ad28 1341 }
1342
1343 myProgramID = NO_PROGRAM;
1344}