2 * Copyright (c) 2023 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("uSize")),
60 CalculateHash(std::string("uColor")),
61 CalculateHash(std::string("uActorColor"))};
64 * Helper function to calculate the correct alignment of data for uniform buffers
65 * @param dataSize size of uniform buffer
66 * @return size of data aligned to given size
68 inline uint32_t AlignSize(uint32_t dataSize, uint32_t alignSize)
70 return ((dataSize / alignSize) + ((dataSize % alignSize) ? 1u : 0u)) * alignSize;
77 Program* Program::New(ProgramCache& cache, const Internal::ShaderDataPtr& shaderData, Graphics::Controller& gfxController)
79 size_t shaderHash = shaderData->GetHashValue();
81 Program* program = cache.GetProgram(shaderHash);
83 if(nullptr == program)
85 // program not found so create it
86 program = new Program(cache, shaderData, gfxController);
88 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());
89 cache.AddProgram(shaderHash, program);
95 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller)
98 mGfxController(controller),
99 mProgramData(std::move(shaderData))
103 Program::~Program() = default;
105 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection, Render::UniformBufferManager& uniformBufferManager)
107 mReflectionDefaultUniforms.clear();
108 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
110 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
112 // add uniform block fields
113 for(auto i = 0u; i < uniformBlockCount; ++i)
115 Graphics::UniformBlockInfo uboInfo;
116 graphicsReflection.GetUniformBlock(i, uboInfo);
118 // for each member store data
119 for(const auto& item : uboInfo.members)
121 // Add a hash for the whole name.
123 // If the name represents an array of basic types, it won't contain an index
124 // operator "[",NN,"]".
126 // If the name represents an element in an array of structs, it will contain an
127 // index operator, but should be hashed in full.
128 auto hashValue = CalculateHash(item.name);
129 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
131 // update buffer index
132 mReflection.back().uniformInfo.bufferIndex = i;
134 // Update default uniforms
135 for(auto j = 0u; j < NUMBER_OF_DEFAULT_UNIFORMS; ++j)
137 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[j])
139 mReflectionDefaultUniforms[j] = mReflection.back();
147 auto samplers = graphicsReflection.GetSamplers(); // Only holds first element of arrays without [].
148 for(const auto& sampler : samplers)
150 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
153 // check for potential collisions
154 std::map<size_t, bool> hashTest;
155 bool hasCollisions(false);
156 for(auto&& item : mReflection)
158 if(hashTest.find(item.hashValue) == hashTest.end())
160 hashTest[item.hashValue] = false;
164 hashTest[item.hashValue] = true;
165 hasCollisions = true;
169 // update collision flag for further use
172 for(auto&& item : mReflection)
174 item.hasCollision = hashTest[item.hashValue];
178 mUniformBlockMemoryRequirements.blockSize.resize(uniformBlockCount);
179 mUniformBlockMemoryRequirements.blockSizeAligned.resize(uniformBlockCount);
180 mUniformBlockMemoryRequirements.blockCount = uniformBlockCount;
181 mUniformBlockMemoryRequirements.totalSizeRequired = 0u;
182 mUniformBlockMemoryRequirements.totalCpuSizeRequired = 0u;
183 mUniformBlockMemoryRequirements.totalGpuSizeRequired = 0u;
185 for(auto i = 0u; i < uniformBlockCount; ++i)
187 Graphics::UniformBlockInfo uboInfo;
188 graphicsReflection.GetUniformBlock(i, uboInfo);
189 bool standaloneUniformBlock = (i == 0);
191 auto blockSize = graphicsReflection.GetUniformBlockSize(i);
192 uint32_t blockAlignment = uniformBufferManager.GetUniformBlockAlignment(standaloneUniformBlock);
193 auto alignedBlockSize = AlignSize(blockSize, blockAlignment);
195 mUniformBlockMemoryRequirements.blockSize[i] = blockSize;
196 mUniformBlockMemoryRequirements.blockSizeAligned[i] = alignedBlockSize;
198 mUniformBlockMemoryRequirements.totalSizeRequired += alignedBlockSize;
199 mUniformBlockMemoryRequirements.totalCpuSizeRequired += (standaloneUniformBlock) ? alignedBlockSize : 0;
200 mUniformBlockMemoryRequirements.totalGpuSizeRequired += (standaloneUniformBlock) ? 0 : alignedBlockSize;
204 void Program::SetGraphicsProgram(Graphics::UniquePtr<Graphics::Program>&& program, Render::UniformBufferManager& uniformBufferManager)
206 mGfxProgram = std::move(program);
207 BuildReflection(mGfxController.GetProgramReflection(*mGfxProgram.get()), uniformBufferManager);
210 bool Program::GetUniform(const std::string_view& name, Hash hashedName, Hash hashedNameNoArray, Graphics::UniformInfo& out) const
212 if(mReflection.empty())
216 DALI_ASSERT_DEBUG(hashedName != 0 && "GetUniform() hash is not set");
218 // If name contains a "]", but has nothing after, it's an element in an array,
219 // The reflection doesn't contain such elements, it only contains the name without square brackets
220 // Use the hash without array subscript.
222 // If the name contains a "]" anywhere but the end, it's a structure element. The reflection
223 // does contain such elements, so use normal hash.
224 Hash hash = hashedName;
225 std::string_view match = name;
227 if(!name.empty() && name.back() == ']')
229 hash = hashedNameNoArray;
230 auto pos = name.rfind("[");
231 match = name.substr(0, pos - 1); // Remove subscript
234 for(const ReflectionUniformInfo& item : mReflection)
236 if(item.hashValue == hash)
238 if(!item.hasCollision || item.uniformInfo.name == match)
240 out = item.uniformInfo;
252 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
254 if(mReflectionDefaultUniforms.empty())
259 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
260 return &value->uniformInfo;
263 } // namespace Internal