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/shaders/program-cache.h>
36 #include <dali/public-api/common/constants.h>
37 #include <dali/public-api/common/dali-common.h>
38 #include <dali/public-api/common/dali-vector.h>
47 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
50 * List of all default uniforms used for quicker lookup
52 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
54 CalculateHash(std::string("uModelMatrix")),
55 CalculateHash(std::string("uMvpMatrix")),
56 CalculateHash(std::string("uViewMatrix")),
57 CalculateHash(std::string("uModelView")),
58 CalculateHash(std::string("uNormalMatrix")),
59 CalculateHash(std::string("uProjection")),
60 CalculateHash(std::string("uSize")),
61 CalculateHash(std::string("uColor"))};
67 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
69 uint32_t programId{0u};
71 // Get program id and use it as hash for the cache
72 // in order to maintain current functionality as long as needed
73 gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
75 size_t shaderHash = programId;
77 Program* program = cache.GetProgram(shaderHash);
79 if(nullptr == program)
81 // program not found so create it
82 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram));
83 cache.AddProgram(shaderHash, program);
89 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
91 mProjectionMatrix(nullptr),
93 mGfxProgram(std::move(gfxProgram)),
94 mGfxController(controller),
95 mProgramData(shaderData)
97 BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
104 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
106 mReflectionDefaultUniforms.clear();
107 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
109 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
111 // add uniform block fields
112 for(auto i = 0u; i < uniformBlockCount; ++i)
114 Graphics::UniformBlockInfo uboInfo;
115 graphicsReflection.GetUniformBlock(i, uboInfo);
117 // for each member store data
118 for(const auto& item : uboInfo.members)
120 auto hashValue = CalculateHash(item.name);
121 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
123 // update buffer index
124 mReflection.back().uniformInfo.bufferIndex = i;
126 // Update default uniforms
127 for(auto i = 0u; i < NUMBER_OF_DEFAULT_UNIFORMS; ++i)
129 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
131 mReflectionDefaultUniforms[i] = mReflection.back();
139 auto samplers = graphicsReflection.GetSamplers();
140 for(const auto& sampler : samplers)
142 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
145 // check for potential collisions
146 std::map<size_t, bool> hashTest;
147 bool hasCollisions(false);
148 for(auto&& item : mReflection)
150 if(hashTest.find(item.hashValue) == hashTest.end())
152 hashTest[item.hashValue] = false;
156 hashTest[item.hashValue] = true;
157 hasCollisions = true;
161 // update collision flag for further use
164 for(auto&& item : mReflection)
166 item.hasCollision = hashTest[item.hashValue];
171 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
173 if(mReflection.empty())
178 hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
180 for(const ReflectionUniformInfo& item : mReflection)
182 if(item.hashValue == hashedName)
184 if(!item.hasCollision || item.uniformInfo.name == name)
186 out = item.uniformInfo;
198 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
200 if(mReflectionDefaultUniforms.empty())
205 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
206 return &value->uniformInfo;
209 } // namespace Internal