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 char* const gStdUniforms[Program::UNIFORM_TYPE_LAST] =
49 "uMvpMatrix", // UNIFORM_MVP_MATRIX
50 "uModelView", // UNIFORM_MODELVIEW_MATRIX
51 "uProjection", // UNIFORM_PROJECTION_MATRIX
52 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
53 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
54 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
55 "uColor", // UNIFORM_COLOR
56 "sTexture", // UNIFORM_SAMPLER
57 "sTextureRect", // UNIFORM_SAMPLER_RECT
58 "sEffect", // UNIFORM_EFFECT_SAMPLER
59 "uSize" // UNIFORM_SIZE
62 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
65 * List of all default uniforms used for quicker lookup
67 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
69 CalculateHash(std::string("uModelMatrix")),
70 CalculateHash(std::string("uMvpMatrix")),
71 CalculateHash(std::string("uViewMatrix")),
72 CalculateHash(std::string("uModelView")),
73 CalculateHash(std::string("uNormalMatrix")),
74 CalculateHash(std::string("uProjection")),
75 CalculateHash(std::string("uSize")),
76 CalculateHash(std::string("uColor"))};
82 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
84 uint32_t programId{0u};
86 // Get program id and use it as hash for the cache
87 // in order to maintain current functionality as long as needed
88 gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
90 size_t shaderHash = programId;
92 Program* program = cache.GetProgram(shaderHash);
94 if(nullptr == program)
96 // program not found so create it
97 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram), modifiesGeometry);
98 cache.AddProgram(shaderHash, program);
104 uint32_t Program::RegisterUniform(ConstString name)
107 // find the value from cache
108 for(; index < static_cast<uint32_t>(mUniformLocations.size()); ++index)
110 if(mUniformLocations[index].first == name)
112 // name found so return index
116 // if we get here, index is one past end so push back the new name
117 mUniformLocations.push_back(std::make_pair(name, UNIFORM_NOT_QUERIED));
121 bool Program::ModifiesGeometry()
123 return mModifiesGeometry;
126 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
128 mProjectionMatrix(nullptr),
129 mViewMatrix(nullptr),
131 mGfxProgram(std::move(gfxProgram)),
132 mGfxController(controller),
133 mProgramData(shaderData),
134 mModifiesGeometry(modifiesGeometry)
136 // reserve space for standard uniforms
137 mUniformLocations.reserve(UNIFORM_TYPE_LAST);
139 // reset built in uniform names in cache
140 for(uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i)
142 RegisterUniform(ConstString(gStdUniforms[i]));
148 // Get program id and use it as hash for the cache
149 // in order to maintain current functionality as long as needed
150 mGfxController.GetProgramParameter(*mGfxProgram, 1, &mProgramId);
152 BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
159 void Program::ResetUniformCache()
161 // reset all gl uniform locations
162 for(uint32_t i = 0; i < mUniformLocations.size(); ++i)
164 // reset gl program locations and names
165 mUniformLocations[i].second = UNIFORM_NOT_QUERIED;
168 mSamplerUniformLocations.clear();
170 // reset uniform caches
171 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
173 for(uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i)
175 // GL initializes uniforms to 0
176 mUniformCacheInt[i] = 0;
177 mUniformCacheFloat[i] = 0.0f;
178 mUniformCacheFloat2[i][0] = 0.0f;
179 mUniformCacheFloat2[i][1] = 0.0f;
180 mUniformCacheFloat4[i][0] = 0.0f;
181 mUniformCacheFloat4[i][1] = 0.0f;
182 mUniformCacheFloat4[i][2] = 0.0f;
183 mUniformCacheFloat4[i][3] = 0.0f;
187 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
189 mReflectionDefaultUniforms.clear();
190 mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
192 auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
194 // add uniform block fields
195 for(auto i = 0u; i < uniformBlockCount; ++i)
197 Graphics::UniformBlockInfo uboInfo;
198 graphicsReflection.GetUniformBlock(i, uboInfo);
200 // for each member store data
201 for(const auto& item : uboInfo.members)
203 auto hashValue = CalculateHash(item.name);
204 mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
206 // update buffer index
207 mReflection.back().uniformInfo.bufferIndex = i;
209 // Update default uniforms
210 for(auto i = 0u; i < NUMBER_OF_DEFAULT_UNIFORMS; ++i)
212 if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
214 mReflectionDefaultUniforms[i] = mReflection.back();
222 auto samplers = graphicsReflection.GetSamplers();
223 for(const auto& sampler : samplers)
225 mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
228 // check for potential collisions
229 std::map<size_t, bool> hashTest;
230 bool hasCollisions(false);
231 for(auto&& item : mReflection)
233 if(hashTest.find(item.hashValue) == hashTest.end())
235 hashTest[item.hashValue] = false;
239 hashTest[item.hashValue] = true;
240 hasCollisions = true;
244 // update collision flag for further use
247 for(auto&& item : mReflection)
249 item.hasCollision = hashTest[item.hashValue];
254 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
256 if(mReflection.empty())
261 hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
263 for(const ReflectionUniformInfo& item : mReflection)
265 if(item.hashValue == hashedName)
267 if(!item.hasCollision || item.uniformInfo.name == name)
269 out = item.uniformInfo;
281 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
283 if(mReflectionDefaultUniforms.empty())
288 const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
289 return &value->uniformInfo;
292 } // namespace Internal