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>
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/shaders/program-cache.h>
34 #include <dali/public-api/common/constants.h>
35 #include <dali/public-api/common/dali-common.h>
36 #include <dali/public-api/common/dali-vector.h>
45 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
48 * List of all default uniforms used for quicker lookup
50 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
52 CalculateHash(std::string("uModelMatrix")),
53 CalculateHash(std::string("uMvpMatrix")),
54 CalculateHash(std::string("uViewMatrix")),
55 CalculateHash(std::string("uModelView")),
56 CalculateHash(std::string("uNormalMatrix")),
57 CalculateHash(std::string("uProjection")),
58 CalculateHash(std::string("uSize")),
59 CalculateHash(std::string("uColor"))};
62 * Helper function to calculate the correct alignment of data for uniform buffers
63 * @param dataSize size of uniform buffer
64 * @return aligned offset of data
66 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
68 return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
75 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
77 uint32_t programId{0u};
79 // Get program id and use it as hash for the cache
80 // in order to maintain current functionality as long as needed
81 gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
83 size_t shaderHash = programId;
85 Program* program = cache.GetProgram(shaderHash);
87 if(nullptr == program)
89 // program not found so create it
90 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram));
91 cache.AddProgram(shaderHash, program);
97 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
99 mProjectionMatrix(nullptr),
100 mViewMatrix(nullptr),
101 mGfxProgram(std::move(gfxProgram)),
102 mGfxController(controller),
103 mProgramData(shaderData)
105 BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
108 Program::~Program() = default;
110 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
112 mReflectionDefaultUniforms.clear();
113 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
115 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
117 // add uniform block fields
118 for(auto i = 0u; i < uniformBlockCount; ++i)
120 Graphics::UniformBlockInfo uboInfo;
121 graphicsReflection.GetUniformBlock(i, uboInfo);
123 // for each member store data
124 for(const auto& item : uboInfo.members)
126 auto hashValue = CalculateHash(item.name);
127 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
129 // update buffer index
130 mReflection.back().uniformInfo.bufferIndex = i;
132 // Update default uniforms
133 for(auto j = 0u; j < NUMBER_OF_DEFAULT_UNIFORMS; ++j)
135 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[j])
137 mReflectionDefaultUniforms[j] = mReflection.back();
145 auto samplers = graphicsReflection.GetSamplers();
146 for(const auto& sampler : samplers)
148 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
151 // check for potential collisions
152 std::map<size_t, bool> hashTest;
153 bool hasCollisions(false);
154 for(auto&& item : mReflection)
156 if(hashTest.find(item.hashValue) == hashTest.end())
158 hashTest[item.hashValue] = false;
162 hashTest[item.hashValue] = true;
163 hasCollisions = true;
167 // update collision flag for further use
170 for(auto&& item : mReflection)
172 item.hasCollision = hashTest[item.hashValue];
176 // Calculate size of memory for uniform blocks
177 mUniformBlockRequirements.totalSizeRequired = 0;
178 mUniformBlockRequirements.blockCount = graphicsReflection.GetUniformBlockCount();
179 for (auto i = 0u; i < mUniformBlockRequirements.blockCount; ++i)
181 auto blockSize = GetUniformBufferDataAlignment(graphicsReflection.GetUniformBlockSize(i));
182 mUniformBlockRequirements.totalSizeRequired += blockSize;
186 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
188 if(mReflection.empty())
193 hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
195 for(const ReflectionUniformInfo& item : mReflection)
197 if(item.hashValue == hashedName)
199 if(!item.hasCollision || item.uniformInfo.name == name)
201 out = item.uniformInfo;
213 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
215 if(mReflectionDefaultUniforms.empty())
220 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
221 return &value->uniformInfo;
224 } // namespace Internal