2 * Copyright (c) 2024 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>
26 #include <dali/devel-api/common/hash.h>
27 #include <dali/graphics-api/graphics-controller.h>
28 #include <dali/graphics-api/graphics-program.h>
29 #include <dali/graphics-api/graphics-reflection.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/internal/common/shader-data.h>
32 #include <dali/internal/render/common/performance-monitor.h>
33 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
34 #include <dali/internal/render/shaders/program-cache.h>
35 #include <dali/public-api/common/constants.h>
36 #include <dali/public-api/common/dali-common.h>
37 #include <dali/public-api/common/dali-vector.h>
46 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
49 * List of all default uniforms used for quicker lookup
51 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
53 CalculateHash(std::string("uModelMatrix")),
54 CalculateHash(std::string("uMvpMatrix")),
55 CalculateHash(std::string("uViewMatrix")),
56 CalculateHash(std::string("uModelView")),
57 CalculateHash(std::string("uNormalMatrix")),
58 CalculateHash(std::string("uProjection")),
59 CalculateHash(std::string("uScale")),
60 CalculateHash(std::string("uSize")),
61 CalculateHash(std::string("uColor")),
62 CalculateHash(std::string("uActorColor"))};
65 * Helper function to calculate the correct alignment of data for uniform buffers
66 * @param dataSize size of uniform buffer
67 * @return size of data aligned to given size
69 inline uint32_t AlignSize(uint32_t dataSize, uint32_t alignSize)
71 return ((dataSize / alignSize) + ((dataSize % alignSize) ? 1u : 0u)) * alignSize;
78 Program* Program::New(ProgramCache& cache, const Internal::ShaderDataPtr& shaderData, Graphics::Controller& gfxController)
80 size_t shaderHash = shaderData->GetHashValue();
82 Program* program = cache.GetProgram(shaderHash);
84 if(nullptr == program)
86 // program not found so create it
87 program = new Program(cache, shaderData, gfxController);
89 DALI_LOG_INFO(Debug::Filter::gShader, Debug::Verbose, "Program::New() created a unique program:\n VertexShader:\n%s\n\n FragShader:\n%s\n", shaderData->GetVertexShader(), shaderData->GetFragmentShader());
90 cache.AddProgram(shaderHash, program);
96 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller)
99 mGfxController(controller),
100 mProgramData(std::move(shaderData))
104 Program::~Program() = default;
106 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection, Render::UniformBufferManager& uniformBufferManager)
108 mReflectionDefaultUniforms.clear();
109 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
111 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
113 // add uniform block fields
114 for(auto i = 0u; i < uniformBlockCount; ++i)
116 Graphics::UniformBlockInfo uboInfo;
117 graphicsReflection.GetUniformBlock(i, uboInfo);
119 // for each member store data
120 for(const auto& item : uboInfo.members)
122 // Add a hash for the whole name.
124 // If the name represents an array of basic types, it won't contain an index
125 // operator "[",NN,"]".
127 // If the name represents an element in an array of structs, it will contain an
128 // index operator, but should be hashed in full.
129 auto hashValue = CalculateHash(item.name);
130 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
132 // update buffer index
133 mReflection.back().uniformInfo.bufferIndex = i;
135 // Update default uniforms
136 for(auto j = 0u; j < NUMBER_OF_DEFAULT_UNIFORMS; ++j)
138 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[j])
140 mReflectionDefaultUniforms[j] = mReflection.back();
148 auto samplers = graphicsReflection.GetSamplers(); // Only holds first element of arrays without [].
149 for(const auto& sampler : samplers)
151 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
154 // check for potential collisions
155 std::map<size_t, bool> hashTest;
156 bool hasCollisions(false);
157 for(auto&& item : mReflection)
159 if(hashTest.find(item.hashValue) == hashTest.end())
161 hashTest[item.hashValue] = false;
165 hashTest[item.hashValue] = true;
166 hasCollisions = true;
170 // update collision flag for further use
173 for(auto&& item : mReflection)
175 item.hasCollision = hashTest[item.hashValue];
179 mUniformBlockMemoryRequirements.blockSize.resize(uniformBlockCount);
180 mUniformBlockMemoryRequirements.blockSizeAligned.resize(uniformBlockCount);
181 mUniformBlockMemoryRequirements.blockCount = uniformBlockCount;
182 mUniformBlockMemoryRequirements.totalSizeRequired = 0u;
183 mUniformBlockMemoryRequirements.totalCpuSizeRequired = 0u;
184 mUniformBlockMemoryRequirements.totalGpuSizeRequired = 0u;
186 for(auto i = 0u; i < uniformBlockCount; ++i)
188 Graphics::UniformBlockInfo uboInfo;
189 graphicsReflection.GetUniformBlock(i, uboInfo);
190 bool standaloneUniformBlock = (i == 0);
192 auto blockSize = graphicsReflection.GetUniformBlockSize(i);
193 uint32_t blockAlignment = uniformBufferManager.GetUniformBlockAlignment(standaloneUniformBlock);
194 auto alignedBlockSize = AlignSize(blockSize, blockAlignment);
196 mUniformBlockMemoryRequirements.blockSize[i] = blockSize;
197 mUniformBlockMemoryRequirements.blockSizeAligned[i] = alignedBlockSize;
199 mUniformBlockMemoryRequirements.totalSizeRequired += alignedBlockSize;
200 mUniformBlockMemoryRequirements.totalCpuSizeRequired += (standaloneUniformBlock) ? alignedBlockSize : 0;
201 mUniformBlockMemoryRequirements.totalGpuSizeRequired += (standaloneUniformBlock) ? 0 : alignedBlockSize;
205 void Program::SetGraphicsProgram(Graphics::UniquePtr<Graphics::Program>&& program, Render::UniformBufferManager& uniformBufferManager)
207 mGfxProgram = std::move(program);
208 BuildReflection(mGfxController.GetProgramReflection(*mGfxProgram.get()), uniformBufferManager);
211 bool Program::GetUniform(const std::string_view& name, Hash hashedName, Hash hashedNameNoArray, Graphics::UniformInfo& out) const
213 if(mReflection.empty())
217 DALI_ASSERT_DEBUG(hashedName != 0 && "GetUniform() hash is not set");
219 // If name contains a "]", but has nothing after, it's an element in an array,
220 // The reflection doesn't contain such elements, it only contains the name without square brackets
221 // Use the hash without array subscript.
223 // If the name contains a "]" anywhere but the end, it's a structure element. The reflection
224 // does contain such elements, so use normal hash.
225 Hash hash = hashedName;
226 std::string_view match = name;
230 if(!name.empty() && name.back() == ']')
232 hash = hashedNameNoArray;
233 auto pos = name.rfind("[");
234 match = name.substr(0, pos); // Remove subscript
235 arrayIndex = atoi(&name[pos + 1]);
238 for(const ReflectionUniformInfo& item : mReflection)
240 if(item.hashValue == hash)
242 if(!item.hasCollision || item.uniformInfo.name == match)
244 out = item.uniformInfo;
246 // Array out of bounds
247 if(item.uniformInfo.elementCount > 0 && arrayIndex >= int(item.uniformInfo.elementCount))
249 DALI_LOG_ERROR("Uniform %s, array index out of bound [%d >= %d]!\n",
250 item.uniformInfo.name.c_str(),
252 int(item.uniformInfo.elementCount));
266 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
268 if(mReflectionDefaultUniforms.empty())
273 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
274 return &value->uniformInfo;
277 } // namespace Internal