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