2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/render/shaders/program.h>
27 #include <dali/devel-api/common/hash.h>
28 #include <dali/graphics-api/graphics-controller.h>
29 #include <dali/graphics-api/graphics-program.h>
30 #include <dali/graphics-api/graphics-reflection.h>
31 #include <dali/integration-api/debug.h>
32 #include <dali/integration-api/gl-defines.h>
33 #include <dali/internal/common/shader-data.h>
34 #include <dali/internal/render/common/performance-monitor.h>
35 #include <dali/internal/render/gl-resources/gl-call-debug.h>
36 #include <dali/internal/render/shaders/program-cache.h>
37 #include <dali/public-api/common/constants.h>
38 #include <dali/public-api/common/dali-common.h>
39 #include <dali/public-api/common/dali-vector.h>
48 const char* const gStdAttribs[Program::ATTRIB_TYPE_LAST] =
50 "aPosition", // ATTRIB_POSITION
51 "aTexCoord", // ATTRIB_TEXCOORD
54 const char* const gStdUniforms[Program::UNIFORM_TYPE_LAST] =
56 "uMvpMatrix", // UNIFORM_MVP_MATRIX
57 "uModelView", // UNIFORM_MODELVIEW_MATRIX
58 "uProjection", // UNIFORM_PROJECTION_MATRIX
59 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
60 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
61 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
62 "uColor", // UNIFORM_COLOR
63 "sTexture", // UNIFORM_SAMPLER
64 "sTextureRect", // UNIFORM_SAMPLER_RECT
65 "sEffect", // UNIFORM_EFFECT_SAMPLER
66 "uSize" // UNIFORM_SIZE
69 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
72 * List of all default uniforms used for quicker lookup
74 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
76 CalculateHash(std::string("uModelMatrix")),
77 CalculateHash(std::string("uMvpMatrix")),
78 CalculateHash(std::string("uViewMatrix")),
79 CalculateHash(std::string("uModelView")),
80 CalculateHash(std::string("uNormalMatrix")),
81 CalculateHash(std::string("uProjection")),
82 CalculateHash(std::string("uSize")),
83 CalculateHash(std::string("uColor"))};
89 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
91 uint32_t programId{0u};
93 // Get program id and use it as hash for the cache
94 // in order to maintain current functionality as long as needed
95 gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
97 size_t shaderHash = programId;
99 Program* program = cache.GetProgram(shaderHash);
101 if(nullptr == program)
103 // program not found so create it
104 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram), modifiesGeometry);
105 program->GetActiveSamplerUniforms();
106 cache.AddProgram(shaderHash, program);
114 LOG_GL("UseProgram(%d)\n", mProgramId);
115 CHECK_GL(mGlAbstraction, mGlAbstraction.UseProgram(mProgramId));
116 mCache.SetCurrentProgram(this);
119 bool Program::IsUsed()
121 return (this == mCache.GetCurrentProgram());
124 GLint Program::GetAttribLocation(AttribType type)
126 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
128 return GetCustomAttributeLocation(type);
131 uint32_t Program::RegisterCustomAttribute(ConstString name)
134 // find the value from cache
135 for(; index < static_cast<uint32_t>(mAttributeLocations.size()); ++index)
137 if(mAttributeLocations[index].first == name)
139 // name found so return index
143 // if we get here, index is one past end so push back the new name
144 mAttributeLocations.push_back(std::make_pair(name, ATTRIB_UNKNOWN));
148 GLint Program::GetCustomAttributeLocation(uint32_t attributeIndex)
150 // debug check that index is within name cache
151 DALI_ASSERT_DEBUG(mAttributeLocations.size() > attributeIndex);
153 // check if we have already queried the location of the attribute
154 GLint location = mAttributeLocations[attributeIndex].second;
156 if(location == ATTRIB_UNKNOWN)
158 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetAttribLocation(mProgramId, mAttributeLocations[attributeIndex].first.GetCString()));
160 mAttributeLocations[attributeIndex].second = location;
161 LOG_GL("GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[attributeIndex].first.GetCString(), mAttributeLocations[attributeIndex].second);
167 uint32_t Program::RegisterUniform(ConstString name)
170 // find the value from cache
171 for(; index < static_cast<uint32_t>(mUniformLocations.size()); ++index)
173 if(mUniformLocations[index].first == name)
175 // name found so return index
179 // if we get here, index is one past end so push back the new name
180 mUniformLocations.push_back(std::make_pair(name, UNIFORM_NOT_QUERIED));
184 GLint Program::GetUniformLocation(uint32_t uniformIndex)
186 // debug check that index is within name cache
187 DALI_ASSERT_DEBUG(mUniformLocations.size() > uniformIndex);
189 // check if we have already queried the location of the uniform
190 GLint location = mUniformLocations[uniformIndex].second;
192 if(location == UNIFORM_NOT_QUERIED)
194 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetUniformLocation(mProgramId, mUniformLocations[uniformIndex].first.GetCString()));
196 mUniformLocations[uniformIndex].second = location;
197 LOG_GL("GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[uniformIndex].first.GetCString(), mUniformLocations[uniformIndex].second);
206 * This struct is used to record the position of a uniform declaration
207 * within the fragment shader source code.
209 struct LocationPosition
211 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
212 int32_t position; ///< the position of the uniform declaration
213 LocationPosition(GLint uniformLocation, int32_t position)
214 : uniformLocation(uniformLocation),
220 bool sortByPosition(LocationPosition a, LocationPosition b)
222 return a.position < b.position;
225 const char* const DELIMITERS = " \t\n";
229 const char* const mString;
230 const uint32_t mLength;
232 template<uint32_t kLength>
233 constexpr StringSize(const char (&string)[kLength])
235 mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
239 operator const char*() const
245 bool operator==(const StringSize& lhs, const char* rhs)
247 return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
250 constexpr StringSize UNIFORM{"uniform"};
251 constexpr StringSize SAMPLER_PREFIX{"sampler"};
252 constexpr StringSize SAMPLER_TYPES[] = {
257 constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
261 void Program::GetActiveSamplerUniforms()
263 GLint numberOfActiveUniforms = -1;
264 GLint uniformMaxNameLength = -1;
266 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms);
267 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength);
269 std::vector<std::string> samplerNames;
270 std::vector<char> name(uniformMaxNameLength + 1); // Allow for null terminator
271 std::vector<LocationPosition> samplerUniformLocations;
276 GLenum type = GL_ZERO;
278 for(int i = 0; i < numberOfActiveUniforms; ++i)
280 mGlAbstraction.GetActiveUniform(mProgramId, static_cast<GLuint>(i), uniformMaxNameLength, &nameLength, &number, &type, name.data());
282 if(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES)
284 GLuint location = mGlAbstraction.GetUniformLocation(mProgramId, name.data());
285 samplerNames.push_back(name.data());
286 samplerUniformLocations.push_back(LocationPosition(location, -1));
291 //Determine declaration order of each sampler
292 char* fragShader = strdup(mProgramData->GetFragmentShader());
293 char* uniform = strstr(fragShader, UNIFORM);
294 int samplerPosition = 0;
297 char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
299 char* nextPtr = nullptr;
300 char* token = strtok_r(outerToken, DELIMITERS, &nextPtr);
303 if(SAMPLER_PREFIX == token)
305 token += SAMPLER_PREFIX.mLength;
306 if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
309 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
310 for(uint32_t i = 0; i < static_cast<uint32_t>(samplerUniformLocations.size()); ++i)
312 if(samplerUniformLocations[i].position == -1 &&
313 strncmp(token, samplerNames[i].c_str(), samplerNames[i].size()) == 0)
315 samplerUniformLocations[i].position = samplerPosition++;
323 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token);
329 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
332 uniform = strstr(uniform, UNIFORM);
337 // Re-order according to declaration order in the fragment source.
338 uint32_t samplerUniformCount = static_cast<uint32_t>(samplerUniformLocations.size());
339 if(samplerUniformCount > 1)
341 std::sort(samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
344 mSamplerUniformLocations.resize(samplerUniformCount);
345 for(uint32_t i = 0; i < samplerUniformCount; ++i)
347 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
351 bool Program::GetSamplerUniformLocation(uint32_t index, GLint& location)
354 if(index < mSamplerUniformLocations.size())
356 location = mSamplerUniformLocations[index];
362 uint32_t Program::GetActiveSamplerCount() const
364 return static_cast<uint32_t>(mSamplerUniformLocations.size());
367 void Program::SetUniform1i(GLint location, GLint value0)
369 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
371 if(UNIFORM_UNKNOWN == location)
373 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
374 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
375 // specified uniform variable will not be changed.following opengl silently do nothing
379 // check if uniform location fits the cache
380 if(location >= MAX_UNIFORM_CACHE_SIZE)
382 // not cached, make the gl call
383 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
384 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
388 // check if the value is different from what's already been set
389 if(value0 != mUniformCacheInt[location])
392 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
393 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
395 mUniformCacheInt[location] = value0;
400 void Program::SetUniform4i(GLint location, GLint value0, GLint value1, GLint value2, GLint value3)
402 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
404 if(UNIFORM_UNKNOWN == location)
406 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
407 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
408 // specified uniform variable will not be changed.following opengl silently do nothing
412 // Not caching these as based on current analysis this is not called that often by our shaders
413 LOG_GL("Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3);
414 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4i(location, value0, value1, value2, value3));
417 void Program::SetUniform1f(GLint location, GLfloat value0)
419 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
421 if(UNIFORM_UNKNOWN == location)
423 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
424 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
425 // specified uniform variable will not be changed.following opengl silently do nothing
429 // check if uniform location fits the cache
430 if(location >= MAX_UNIFORM_CACHE_SIZE)
432 // not cached, make the gl call
433 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
434 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
438 // check if the same value has already been set, reset if it is different
439 if((fabsf(value0 - mUniformCacheFloat[location]) >= Math::MACHINE_EPSILON_1))
442 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
443 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
446 mUniformCacheFloat[location] = value0;
451 void Program::SetUniform2f(GLint location, GLfloat value0, GLfloat value1)
453 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
455 if(UNIFORM_UNKNOWN == location)
457 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
458 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
459 // specified uniform variable will not be changed.following opengl silently do nothing
463 // check if uniform location fits the cache
464 if(location >= MAX_UNIFORM_CACHE_SIZE)
466 // not cached, make the gl call
467 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
468 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
472 // check if the same value has already been set, reset if it is different
473 if((fabsf(value0 - mUniformCacheFloat2[location][0]) >= Math::MACHINE_EPSILON_1) ||
474 (fabsf(value1 - mUniformCacheFloat2[location][1]) >= Math::MACHINE_EPSILON_1))
477 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
478 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
481 mUniformCacheFloat2[location][0] = value0;
482 mUniformCacheFloat2[location][1] = value1;
487 void Program::SetSizeUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
489 if((fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1) ||
490 (fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1) ||
491 (fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1))
493 mSizeUniformCache.x = value0;
494 mSizeUniformCache.y = value1;
495 mSizeUniformCache.z = value2;
496 SetUniform3f(location, value0, value1, value2);
500 void Program::SetUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
502 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
504 if(UNIFORM_UNKNOWN == location)
506 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
507 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
508 // specified uniform variable will not be changed.following opengl silently do nothing
512 // Not caching these as based on current analysis this is not called that often by our shaders
513 LOG_GL("Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2);
514 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform3f(location, value0, value1, value2));
517 void Program::SetUniform4f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3)
519 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
521 if(UNIFORM_UNKNOWN == location)
523 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
524 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
525 // specified uniform variable will not be changed.following opengl silently do nothing
529 // check if uniform location fits the cache
530 if(location >= MAX_UNIFORM_CACHE_SIZE)
532 // not cached, make the gl call
533 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
534 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
538 // check if the same value has already been set, reset if any component is different
539 // checking index 3 first because we're often animating alpha (rgba)
540 if((fabsf(value3 - mUniformCacheFloat4[location][3]) >= Math::MACHINE_EPSILON_1) ||
541 (fabsf(value0 - mUniformCacheFloat4[location][0]) >= Math::MACHINE_EPSILON_1) ||
542 (fabsf(value1 - mUniformCacheFloat4[location][1]) >= Math::MACHINE_EPSILON_1) ||
543 (fabsf(value2 - mUniformCacheFloat4[location][2]) >= Math::MACHINE_EPSILON_1))
546 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
547 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
549 mUniformCacheFloat4[location][0] = value0;
550 mUniformCacheFloat4[location][1] = value1;
551 mUniformCacheFloat4[location][2] = value2;
552 mUniformCacheFloat4[location][3] = value3;
557 void Program::SetUniformMatrix4fv(GLint location, GLsizei count, const GLfloat* value)
559 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
561 if(UNIFORM_UNKNOWN == location)
563 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
564 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
565 // specified uniform variable will not be changed.following opengl silently do nothing
569 // Not caching these calls. Based on current analysis this is called very often
570 // but with different values (we're using this for MVP matrices)
571 // NOTE! we never want driver or GPU to transpose
572 LOG_GL("UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
573 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix4fv(location, count, GL_FALSE, value));
576 void Program::SetUniformMatrix3fv(GLint location, GLsizei count, const GLfloat* value)
578 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
580 if(UNIFORM_UNKNOWN == location)
582 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
583 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
584 // specified uniform variable will not be changed.following opengl silently do nothing
588 // Not caching these calls. Based on current analysis this is called very often
589 // but with different values (we're using this for MVP matrices)
590 // NOTE! we never want driver or GPU to transpose
591 LOG_GL("UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
592 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix3fv(location, count, GL_FALSE, value));
595 void Program::GlContextCreated()
599 void Program::GlContextDestroyed()
601 ResetAttribsUniformCache();
604 bool Program::ModifiesGeometry()
606 return mModifiesGeometry;
609 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
611 mGlAbstraction(mCache.GetGlAbstraction()),
612 mProjectionMatrix(nullptr),
613 mViewMatrix(nullptr),
615 mGfxProgram(std::move(gfxProgram)),
616 mGfxController(controller),
617 mProgramData(shaderData),
618 mModifiesGeometry(modifiesGeometry)
620 // reserve space for standard attributes
621 mAttributeLocations.reserve(ATTRIB_TYPE_LAST);
622 for(uint32_t i = 0; i < ATTRIB_TYPE_LAST; ++i)
624 RegisterCustomAttribute(ConstString(gStdAttribs[i]));
627 // reserve space for standard uniforms
628 mUniformLocations.reserve(UNIFORM_TYPE_LAST);
629 // reset built in uniform names in cache
630 for(uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i)
632 RegisterUniform(ConstString(gStdUniforms[i]));
636 ResetAttribsUniformCache();
638 // Get program id and use it as hash for the cache
639 // in order to maintain current functionality as long as needed
640 mGfxController.GetProgramParameter(*mGfxProgram, 1, &mProgramId);
642 BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
649 void Program::ResetAttribsUniformCache()
651 // reset attribute locations
652 for(uint32_t i = 0; i < mAttributeLocations.size(); ++i)
654 mAttributeLocations[i].second = ATTRIB_UNKNOWN;
657 // reset all gl uniform locations
658 for(uint32_t i = 0; i < mUniformLocations.size(); ++i)
660 // reset gl program locations and names
661 mUniformLocations[i].second = UNIFORM_NOT_QUERIED;
664 mSamplerUniformLocations.clear();
666 // reset uniform caches
667 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
669 for(uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i)
671 // GL initializes uniforms to 0
672 mUniformCacheInt[i] = 0;
673 mUniformCacheFloat[i] = 0.0f;
674 mUniformCacheFloat2[i][0] = 0.0f;
675 mUniformCacheFloat2[i][1] = 0.0f;
676 mUniformCacheFloat4[i][0] = 0.0f;
677 mUniformCacheFloat4[i][1] = 0.0f;
678 mUniformCacheFloat4[i][2] = 0.0f;
679 mUniformCacheFloat4[i][3] = 0.0f;
683 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
685 mReflectionDefaultUniforms.clear();
686 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
688 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
690 // add uniform block fields
691 for(auto i = 0u; i < uniformBlockCount; ++i)
693 Graphics::UniformBlockInfo uboInfo;
694 graphicsReflection.GetUniformBlock(i, uboInfo);
696 // for each member store data
697 for(const auto& item : uboInfo.members)
699 auto hashValue = CalculateHash(item.name);
700 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
702 // update buffer index
703 mReflection.back().uniformInfo.bufferIndex = i;
705 // Update default uniforms
706 for(auto i = 0u; i < NUMBER_OF_DEFAULT_UNIFORMS; ++i)
708 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
710 mReflectionDefaultUniforms[i] = mReflection.back();
718 auto samplers = graphicsReflection.GetSamplers();
719 for(const auto& sampler : samplers)
721 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
724 // check for potential collisions
725 std::map<size_t, bool> hashTest;
726 bool hasCollisions(false);
727 for(auto&& item : mReflection)
729 if(hashTest.find(item.hashValue) == hashTest.end())
731 hashTest[item.hashValue] = false;
735 hashTest[item.hashValue] = true;
736 hasCollisions = true;
740 // update collision flag for further use
743 for(auto&& item : mReflection)
745 item.hasCollision = hashTest[item.hashValue];
750 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
752 if(mReflection.empty())
757 hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
759 for(const ReflectionUniformInfo& item : mReflection)
761 if(item.hashValue == hashedName)
763 if(!item.hasCollision || item.uniformInfo.name == name)
765 out = item.uniformInfo;
777 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
779 if(mReflectionDefaultUniforms.empty())
784 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
785 return &value->uniformInfo;
788 } // namespace Internal