Lock uniform buffer only 1 times per each render + minor fixup of uniforms
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/shaders/program.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <map>
24
25 // INTERNAL INCLUDES
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>
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 // LOCAL STUFF
43 namespace
44 {
45 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
46
47 /**
48  * List of all default uniforms used for quicker lookup
49  */
50 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
51   {
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")),
60     CalculateHash(std::string("uActorColor"))};
61
62 /**
63  * Helper function to calculate the correct alignment of data for uniform buffers
64  * @param dataSize size of uniform buffer
65  * @return aligned offset of data
66  */
67 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
68 {
69   return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
70 }
71
72 } // namespace
73
74 // IMPLEMENTATION
75
76 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController)
77 {
78   size_t shaderHash = shaderData->GetHashValue();
79
80   Program* program = cache.GetProgram(shaderHash);
81
82   if(nullptr == program)
83   {
84     // program not found so create it
85     program = new Program(cache, shaderData, gfxController);
86     cache.AddProgram(shaderHash, program);
87   }
88
89   return program;
90 }
91
92 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller)
93 : mCache(cache),
94   mProjectionMatrix(nullptr),
95   mViewMatrix(nullptr),
96   mGfxProgram(nullptr),
97   mGfxController(controller),
98   mProgramData(shaderData)
99 {
100 }
101
102 Program::~Program() = default;
103
104 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
105 {
106   mReflectionDefaultUniforms.clear();
107   mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
108
109   auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
110
111   // add uniform block fields
112   for(auto i = 0u; i < uniformBlockCount; ++i)
113   {
114     Graphics::UniformBlockInfo uboInfo;
115     graphicsReflection.GetUniformBlock(i, uboInfo);
116
117     // for each member store data
118     for(const auto& item : uboInfo.members)
119     {
120       // Add a hash for the whole name.
121       //
122       // If the name represents an array of basic types, it won't contain an index
123       // operator "[",NN,"]".
124       //
125       // If the name represents an element in an array of structs, it will contain an
126       // index operator, but should be hashed in full.
127       auto hashValue = CalculateHash(item.name);
128       mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
129
130       // update buffer index
131       mReflection.back().uniformInfo.bufferIndex = i;
132
133       // Update default uniforms
134       for(auto j = 0u; j < NUMBER_OF_DEFAULT_UNIFORMS; ++j)
135       {
136         if(hashValue == DEFAULT_UNIFORM_HASHTABLE[j])
137         {
138           mReflectionDefaultUniforms[j] = mReflection.back();
139           break;
140         }
141       }
142     }
143   }
144
145   // add samplers
146   auto samplers = graphicsReflection.GetSamplers();
147   for(const auto& sampler : samplers)
148   {
149     mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
150   }
151
152   // check for potential collisions
153   std::map<size_t, bool> hashTest;
154   bool                   hasCollisions(false);
155   for(auto&& item : mReflection)
156   {
157     if(hashTest.find(item.hashValue) == hashTest.end())
158     {
159       hashTest[item.hashValue] = false;
160     }
161     else
162     {
163       hashTest[item.hashValue] = true;
164       hasCollisions            = true;
165     }
166   }
167
168   // update collision flag for further use
169   if(hasCollisions)
170   {
171     for(auto&& item : mReflection)
172     {
173       item.hasCollision = hashTest[item.hashValue];
174     }
175   }
176
177   // Calculate size of memory for uniform blocks
178   mUniformBlockRequirements.totalSizeRequired = 0;
179   mUniformBlockRequirements.blockCount        = graphicsReflection.GetUniformBlockCount();
180   for(auto i = 0u; i < mUniformBlockRequirements.blockCount; ++i)
181   {
182     auto blockSize = GetUniformBufferDataAlignment(graphicsReflection.GetUniformBlockSize(i));
183     mUniformBlockRequirements.totalSizeRequired += blockSize;
184   }
185 }
186
187 void Program::SetGraphicsProgram(Graphics::UniquePtr<Graphics::Program>&& program)
188 {
189   mGfxProgram = std::move(program);
190   BuildReflection(mGfxController.GetProgramReflection(*mGfxProgram.get()));
191 }
192
193 bool Program::GetUniform(const std::string_view& name, Hash hashedName, Hash hashedNameNoArray, Graphics::UniformInfo& out) const
194 {
195   if(mReflection.empty())
196   {
197     return false;
198   }
199   DALI_ASSERT_DEBUG(hashedName != 0 && "GetUniform() hash is not set");
200
201   // If name contains a "]", but has nothing after, it's an element in an array,
202   // The reflection doesn't contain such elements, it only contains the name without square brackets
203   // Use the hash without array subscript.
204
205   // If the name contains a "]" anywhere but the end, it's a structure element. The reflection
206   // does contain such elements, so use normal hash.
207   Hash             hash  = hashedName;
208   std::string_view match = name;
209
210   if(!name.empty() && name.back() == ']')
211   {
212     hash     = hashedNameNoArray;
213     auto pos = name.rfind("[");
214     match    = name.substr(0, pos - 1); // Remove subscript
215   }
216
217   for(const ReflectionUniformInfo& item : mReflection)
218   {
219     if(item.hashValue == hash)
220     {
221       if(!item.hasCollision || item.uniformInfo.name == match)
222       {
223         out = item.uniformInfo;
224         return true;
225       }
226       else
227       {
228         return false;
229       }
230     }
231   }
232   return false;
233 }
234
235 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
236 {
237   if(mReflectionDefaultUniforms.empty())
238   {
239     return nullptr;
240   }
241
242   const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
243   return &value->uniformInfo;
244 }
245
246 } // namespace Internal
247
248 } // namespace Dali