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
70 * List of all default uniforms used for quicker lookup
72 std::array<size_t, 8> DEFAULT_UNIFORM_HASHTABLE =
74 CalculateHash(std::string("uModelMatrix")),
75 CalculateHash(std::string("uMvpMatrix")),
76 CalculateHash(std::string("uViewMatrix")),
77 CalculateHash(std::string("uModelView")),
78 CalculateHash(std::string("uNormalMatrix")),
79 CalculateHash(std::string("uProjection")),
80 CalculateHash(std::string("uSize")),
81 CalculateHash(std::string("uColor"))};
87 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
89 uint32_t programId{0u};
91 // Get program id and use it as hash for the cache
92 // in order to maintain current functionality as long as needed
93 gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
95 size_t shaderHash = programId;
97 Program* program = cache.GetProgram(shaderHash);
99 if(nullptr == program)
101 // program not found so create it
102 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram), modifiesGeometry);
103 program->GetActiveSamplerUniforms();
104 cache.AddProgram(shaderHash, program);
112 LOG_GL("UseProgram(%d)\n", mProgramId);
113 CHECK_GL(mGlAbstraction, mGlAbstraction.UseProgram(mProgramId));
114 mCache.SetCurrentProgram(this);
117 bool Program::IsUsed()
119 return (this == mCache.GetCurrentProgram());
122 GLint Program::GetAttribLocation(AttribType type)
124 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
126 return GetCustomAttributeLocation(type);
129 uint32_t Program::RegisterCustomAttribute(ConstString name)
132 // find the value from cache
133 for(; index < static_cast<uint32_t>(mAttributeLocations.size()); ++index)
135 if(mAttributeLocations[index].first == name)
137 // name found so return index
141 // if we get here, index is one past end so push back the new name
142 mAttributeLocations.push_back(std::make_pair(name, ATTRIB_UNKNOWN));
146 GLint Program::GetCustomAttributeLocation(uint32_t attributeIndex)
148 // debug check that index is within name cache
149 DALI_ASSERT_DEBUG(mAttributeLocations.size() > attributeIndex);
151 // check if we have already queried the location of the attribute
152 GLint location = mAttributeLocations[attributeIndex].second;
154 if(location == ATTRIB_UNKNOWN)
156 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetAttribLocation(mProgramId, mAttributeLocations[attributeIndex].first.GetCString()));
158 mAttributeLocations[attributeIndex].second = location;
159 LOG_GL("GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[attributeIndex].first.GetCString(), mAttributeLocations[attributeIndex].second);
165 uint32_t Program::RegisterUniform(ConstString name)
168 // find the value from cache
169 for(; index < static_cast<uint32_t>(mUniformLocations.size()); ++index)
171 if(mUniformLocations[index].first == name)
173 // name found so return index
177 // if we get here, index is one past end so push back the new name
178 mUniformLocations.push_back(std::make_pair(name, UNIFORM_NOT_QUERIED));
182 GLint Program::GetUniformLocation(uint32_t uniformIndex)
184 // debug check that index is within name cache
185 DALI_ASSERT_DEBUG(mUniformLocations.size() > uniformIndex);
187 // check if we have already queried the location of the uniform
188 GLint location = mUniformLocations[uniformIndex].second;
190 if(location == UNIFORM_NOT_QUERIED)
192 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetUniformLocation(mProgramId, mUniformLocations[uniformIndex].first.GetCString()));
194 mUniformLocations[uniformIndex].second = location;
195 LOG_GL("GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[uniformIndex].first.GetCString(), mUniformLocations[uniformIndex].second);
204 * This struct is used to record the position of a uniform declaration
205 * within the fragment shader source code.
207 struct LocationPosition
209 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
210 int32_t position; ///< the position of the uniform declaration
211 LocationPosition(GLint uniformLocation, int32_t position)
212 : uniformLocation(uniformLocation),
218 bool sortByPosition(LocationPosition a, LocationPosition b)
220 return a.position < b.position;
223 const char* const DELIMITERS = " \t\n";
227 const char* const mString;
228 const uint32_t mLength;
230 template<uint32_t kLength>
231 constexpr StringSize(const char (&string)[kLength])
233 mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
237 operator const char*() const
243 bool operator==(const StringSize& lhs, const char* rhs)
245 return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
248 constexpr StringSize UNIFORM{"uniform"};
249 constexpr StringSize SAMPLER_PREFIX{"sampler"};
250 constexpr StringSize SAMPLER_TYPES[] = {
255 constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
259 void Program::GetActiveSamplerUniforms()
261 GLint numberOfActiveUniforms = -1;
262 GLint uniformMaxNameLength = -1;
264 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms);
265 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength);
267 std::vector<std::string> samplerNames;
268 std::vector<char> name(uniformMaxNameLength + 1); // Allow for null terminator
269 std::vector<LocationPosition> samplerUniformLocations;
274 GLenum type = GL_ZERO;
276 for(int i = 0; i < numberOfActiveUniforms; ++i)
278 mGlAbstraction.GetActiveUniform(mProgramId, static_cast<GLuint>(i), uniformMaxNameLength, &nameLength, &number, &type, name.data());
280 if(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES)
282 GLuint location = mGlAbstraction.GetUniformLocation(mProgramId, name.data());
283 samplerNames.push_back(name.data());
284 samplerUniformLocations.push_back(LocationPosition(location, -1));
289 //Determine declaration order of each sampler
290 char* fragShader = strdup(mProgramData->GetFragmentShader());
291 char* uniform = strstr(fragShader, UNIFORM);
292 int samplerPosition = 0;
295 char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
297 char* nextPtr = nullptr;
298 char* token = strtok_r(outerToken, DELIMITERS, &nextPtr);
301 if(SAMPLER_PREFIX == token)
303 token += SAMPLER_PREFIX.mLength;
304 if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
307 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
308 for(uint32_t i = 0; i < static_cast<uint32_t>(samplerUniformLocations.size()); ++i)
310 if(samplerUniformLocations[i].position == -1 &&
311 strncmp(token, samplerNames[i].c_str(), samplerNames[i].size()) == 0)
313 samplerUniformLocations[i].position = samplerPosition++;
321 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token);
327 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
330 uniform = strstr(uniform, UNIFORM);
335 // Re-order according to declaration order in the fragment source.
336 uint32_t samplerUniformCount = static_cast<uint32_t>(samplerUniformLocations.size());
337 if(samplerUniformCount > 1)
339 std::sort(samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
342 mSamplerUniformLocations.resize(samplerUniformCount);
343 for(uint32_t i = 0; i < samplerUniformCount; ++i)
345 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
349 bool Program::GetSamplerUniformLocation(uint32_t index, GLint& location)
352 if(index < mSamplerUniformLocations.size())
354 location = mSamplerUniformLocations[index];
360 uint32_t Program::GetActiveSamplerCount() const
362 return static_cast<uint32_t>(mSamplerUniformLocations.size());
365 void Program::SetUniform1i(GLint location, GLint value0)
367 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
369 if(UNIFORM_UNKNOWN == location)
371 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
372 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
373 // specified uniform variable will not be changed.following opengl silently do nothing
377 // check if uniform location fits the cache
378 if(location >= MAX_UNIFORM_CACHE_SIZE)
380 // not cached, make the gl call
381 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
382 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
386 // check if the value is different from what's already been set
387 if(value0 != mUniformCacheInt[location])
390 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
391 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
393 mUniformCacheInt[location] = value0;
398 void Program::SetUniform4i(GLint location, GLint value0, GLint value1, GLint value2, GLint value3)
400 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
402 if(UNIFORM_UNKNOWN == location)
404 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
405 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
406 // specified uniform variable will not be changed.following opengl silently do nothing
410 // Not caching these as based on current analysis this is not called that often by our shaders
411 LOG_GL("Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3);
412 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4i(location, value0, value1, value2, value3));
415 void Program::SetUniform1f(GLint location, GLfloat value0)
417 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
419 if(UNIFORM_UNKNOWN == location)
421 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
422 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
423 // specified uniform variable will not be changed.following opengl silently do nothing
427 // check if uniform location fits the cache
428 if(location >= MAX_UNIFORM_CACHE_SIZE)
430 // not cached, make the gl call
431 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
432 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
436 // check if the same value has already been set, reset if it is different
437 if((fabsf(value0 - mUniformCacheFloat[location]) >= Math::MACHINE_EPSILON_1))
440 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
441 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
444 mUniformCacheFloat[location] = value0;
449 void Program::SetUniform2f(GLint location, GLfloat value0, GLfloat value1)
451 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
453 if(UNIFORM_UNKNOWN == location)
455 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
456 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
457 // specified uniform variable will not be changed.following opengl silently do nothing
461 // check if uniform location fits the cache
462 if(location >= MAX_UNIFORM_CACHE_SIZE)
464 // not cached, make the gl call
465 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
466 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
470 // check if the same value has already been set, reset if it is different
471 if((fabsf(value0 - mUniformCacheFloat2[location][0]) >= Math::MACHINE_EPSILON_1) ||
472 (fabsf(value1 - mUniformCacheFloat2[location][1]) >= Math::MACHINE_EPSILON_1))
475 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
476 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
479 mUniformCacheFloat2[location][0] = value0;
480 mUniformCacheFloat2[location][1] = value1;
485 void Program::SetSizeUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
487 if((fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1) ||
488 (fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1) ||
489 (fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1))
491 mSizeUniformCache.x = value0;
492 mSizeUniformCache.y = value1;
493 mSizeUniformCache.z = value2;
494 SetUniform3f(location, value0, value1, value2);
498 void Program::SetUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
500 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
502 if(UNIFORM_UNKNOWN == location)
504 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
505 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
506 // specified uniform variable will not be changed.following opengl silently do nothing
510 // Not caching these as based on current analysis this is not called that often by our shaders
511 LOG_GL("Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2);
512 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform3f(location, value0, value1, value2));
515 void Program::SetUniform4f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3)
517 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
519 if(UNIFORM_UNKNOWN == location)
521 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
522 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
523 // specified uniform variable will not be changed.following opengl silently do nothing
527 // check if uniform location fits the cache
528 if(location >= MAX_UNIFORM_CACHE_SIZE)
530 // not cached, make the gl call
531 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
532 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
536 // check if the same value has already been set, reset if any component is different
537 // checking index 3 first because we're often animating alpha (rgba)
538 if((fabsf(value3 - mUniformCacheFloat4[location][3]) >= Math::MACHINE_EPSILON_1) ||
539 (fabsf(value0 - mUniformCacheFloat4[location][0]) >= Math::MACHINE_EPSILON_1) ||
540 (fabsf(value1 - mUniformCacheFloat4[location][1]) >= Math::MACHINE_EPSILON_1) ||
541 (fabsf(value2 - mUniformCacheFloat4[location][2]) >= Math::MACHINE_EPSILON_1))
544 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
545 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
547 mUniformCacheFloat4[location][0] = value0;
548 mUniformCacheFloat4[location][1] = value1;
549 mUniformCacheFloat4[location][2] = value2;
550 mUniformCacheFloat4[location][3] = value3;
555 void Program::SetUniformMatrix4fv(GLint location, GLsizei count, const GLfloat* value)
557 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
559 if(UNIFORM_UNKNOWN == location)
561 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
562 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
563 // specified uniform variable will not be changed.following opengl silently do nothing
567 // Not caching these calls. Based on current analysis this is called very often
568 // but with different values (we're using this for MVP matrices)
569 // NOTE! we never want driver or GPU to transpose
570 LOG_GL("UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
571 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix4fv(location, count, GL_FALSE, value));
574 void Program::SetUniformMatrix3fv(GLint location, GLsizei count, const GLfloat* value)
576 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
578 if(UNIFORM_UNKNOWN == location)
580 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
581 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
582 // specified uniform variable will not be changed.following opengl silently do nothing
586 // Not caching these calls. Based on current analysis this is called very often
587 // but with different values (we're using this for MVP matrices)
588 // NOTE! we never want driver or GPU to transpose
589 LOG_GL("UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
590 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix3fv(location, count, GL_FALSE, value));
593 void Program::GlContextCreated()
597 void Program::GlContextDestroyed()
599 ResetAttribsUniformCache();
602 bool Program::ModifiesGeometry()
604 return mModifiesGeometry;
607 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
609 mGlAbstraction(mCache.GetGlAbstraction()),
610 mProjectionMatrix(nullptr),
611 mViewMatrix(nullptr),
613 mGfxProgram(std::move(gfxProgram)),
614 mGfxController(controller),
615 mProgramData(shaderData),
616 mModifiesGeometry(modifiesGeometry)
618 // reserve space for standard attributes
619 mAttributeLocations.reserve(ATTRIB_TYPE_LAST);
620 for(uint32_t i = 0; i < ATTRIB_TYPE_LAST; ++i)
622 RegisterCustomAttribute(ConstString(gStdAttribs[i]));
625 // reserve space for standard uniforms
626 mUniformLocations.reserve(UNIFORM_TYPE_LAST);
627 // reset built in uniform names in cache
628 for(uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i)
630 RegisterUniform(ConstString(gStdUniforms[i]));
634 ResetAttribsUniformCache();
636 // Get program id and use it as hash for the cache
637 // in order to maintain current functionality as long as needed
638 mGfxController.GetProgramParameter(*mGfxProgram, 1, &mProgramId);
640 BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
647 void Program::ResetAttribsUniformCache()
649 // reset attribute locations
650 for(uint32_t i = 0; i < mAttributeLocations.size(); ++i)
652 mAttributeLocations[i].second = ATTRIB_UNKNOWN;
655 // reset all gl uniform locations
656 for(uint32_t i = 0; i < mUniformLocations.size(); ++i)
658 // reset gl program locations and names
659 mUniformLocations[i].second = UNIFORM_NOT_QUERIED;
662 mSamplerUniformLocations.clear();
664 // reset uniform caches
665 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
667 for(uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i)
669 // GL initializes uniforms to 0
670 mUniformCacheInt[i] = 0;
671 mUniformCacheFloat[i] = 0.0f;
672 mUniformCacheFloat2[i][0] = 0.0f;
673 mUniformCacheFloat2[i][1] = 0.0f;
674 mUniformCacheFloat4[i][0] = 0.0f;
675 mUniformCacheFloat4[i][1] = 0.0f;
676 mUniformCacheFloat4[i][2] = 0.0f;
677 mUniformCacheFloat4[i][3] = 0.0f;
681 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
683 mReflectionDefaultUniforms.clear();
684 mReflectionDefaultUniforms.resize(DEFAULT_UNIFORM_HASHTABLE.size());
686 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
688 // add uniform block fields
689 for(auto i = 0u; i < uniformBlockCount; ++i)
691 Graphics::UniformBlockInfo uboInfo;
692 graphicsReflection.GetUniformBlock(i, uboInfo);
694 // for each member store data
695 for(const auto& item : uboInfo.members)
697 auto hashValue = CalculateHash(item.name);
698 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
700 // update buffer index
701 mReflection.back().uniformInfo.bufferIndex = i;
703 // Update default uniforms
704 for(auto i = 0u; i < DEFAULT_UNIFORM_HASHTABLE.size(); ++i)
706 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
708 mReflectionDefaultUniforms[i] = mReflection.back();
716 auto samplers = graphicsReflection.GetSamplers();
717 for(const auto& sampler : samplers)
719 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
722 // check for potential collisions
723 std::map<size_t, bool> hashTest;
724 bool hasCollisions(false);
725 for(auto&& item : mReflection)
727 if(hashTest.find(item.hashValue) == hashTest.end())
729 hashTest[item.hashValue] = false;
733 hashTest[item.hashValue] = true;
734 hasCollisions = true;
738 // update collision flag for further use
741 for(auto&& item : mReflection)
743 item.hasCollision = hashTest[item.hashValue];
748 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
750 if(mReflection.empty())
755 hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
757 for(const ReflectionUniformInfo& item : mReflection)
759 if(item.hashValue == hashedName)
761 if(!item.hasCollision || item.uniformInfo.name == name)
763 out = item.uniformInfo;
775 bool Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex, Graphics::UniformInfo& out) const
777 if(mReflectionDefaultUniforms.empty())
782 auto& value = mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
783 out = value.uniformInfo;
787 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
789 if(mReflectionDefaultUniforms.empty())
794 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
795 return &value->uniformInfo;
798 } // namespace Internal