Cleanup in Aisle #1
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
1 /*
2  * Copyright (c) 2021 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 <iomanip>
24 #include <map>
25
26 // INTERNAL INCLUDES
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>
39
40 namespace Dali
41 {
42 namespace Internal
43 {
44 // LOCAL STUFF
45 namespace
46 {
47 const char* const gStdUniforms[Program::UNIFORM_TYPE_LAST] =
48   {
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
60 };
61
62 const unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
63
64 /**
65  * List of all default uniforms used for quicker lookup
66  */
67 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
68   {
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"))};
77
78 } // namespace
79
80 // IMPLEMENTATION
81
82 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
83 {
84   uint32_t programId{0u};
85
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);
89
90   size_t shaderHash = programId;
91
92   Program* program = cache.GetProgram(shaderHash);
93
94   if(nullptr == program)
95   {
96     // program not found so create it
97     program = new Program(cache, shaderData, gfxController, std::move(gfxProgram), modifiesGeometry);
98     cache.AddProgram(shaderHash, program);
99   }
100
101   return program;
102 }
103
104 uint32_t Program::RegisterUniform(ConstString name)
105 {
106   uint32_t index = 0;
107   // find the value from cache
108   for(; index < static_cast<uint32_t>(mUniformLocations.size()); ++index)
109   {
110     if(mUniformLocations[index].first == name)
111     {
112       // name found so return index
113       return index;
114     }
115   }
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));
118   return index;
119 }
120
121 bool Program::ModifiesGeometry()
122 {
123   return mModifiesGeometry;
124 }
125
126 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
127 : mCache(cache),
128   mProjectionMatrix(nullptr),
129   mViewMatrix(nullptr),
130   mProgramId(0),
131   mGfxProgram(std::move(gfxProgram)),
132   mGfxController(controller),
133   mProgramData(shaderData),
134   mModifiesGeometry(modifiesGeometry)
135 {
136   // reserve space for standard uniforms
137   mUniformLocations.reserve(UNIFORM_TYPE_LAST);
138
139   // reset built in uniform names in cache
140   for(uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i)
141   {
142     RegisterUniform(ConstString(gStdUniforms[i]));
143   }
144
145   // reset values
146   ResetUniformCache();
147
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);
151
152   BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
153 }
154
155 Program::~Program()
156 {
157 }
158
159 void Program::ResetUniformCache()
160 {
161   // reset all gl uniform locations
162   for(uint32_t i = 0; i < mUniformLocations.size(); ++i)
163   {
164     // reset gl program locations and names
165     mUniformLocations[i].second = UNIFORM_NOT_QUERIED;
166   }
167
168   mSamplerUniformLocations.clear();
169
170   // reset uniform caches
171   mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
172
173   for(uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i)
174   {
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;
184   }
185 }
186
187 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
188 {
189   mReflectionDefaultUniforms.clear();
190   mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
191
192   auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
193
194   // add uniform block fields
195   for(auto i = 0u; i < uniformBlockCount; ++i)
196   {
197     Graphics::UniformBlockInfo uboInfo;
198     graphicsReflection.GetUniformBlock(i, uboInfo);
199
200     // for each member store data
201     for(const auto& item : uboInfo.members)
202     {
203       auto hashValue = CalculateHash(item.name);
204       mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
205
206       // update buffer index
207       mReflection.back().uniformInfo.bufferIndex = i;
208
209       // Update default uniforms
210       for(auto i = 0u; i < NUMBER_OF_DEFAULT_UNIFORMS; ++i)
211       {
212         if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
213         {
214           mReflectionDefaultUniforms[i] = mReflection.back();
215           break;
216         }
217       }
218     }
219   }
220
221   // add samplers
222   auto samplers = graphicsReflection.GetSamplers();
223   for(const auto& sampler : samplers)
224   {
225     mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
226   }
227
228   // check for potential collisions
229   std::map<size_t, bool> hashTest;
230   bool                   hasCollisions(false);
231   for(auto&& item : mReflection)
232   {
233     if(hashTest.find(item.hashValue) == hashTest.end())
234     {
235       hashTest[item.hashValue] = false;
236     }
237     else
238     {
239       hashTest[item.hashValue] = true;
240       hasCollisions            = true;
241     }
242   }
243
244   // update collision flag for further use
245   if(hasCollisions)
246   {
247     for(auto&& item : mReflection)
248     {
249       item.hasCollision = hashTest[item.hashValue];
250     }
251   }
252 }
253
254 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
255 {
256   if(mReflection.empty())
257   {
258     return false;
259   }
260
261   hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
262
263   for(const ReflectionUniformInfo& item : mReflection)
264   {
265     if(item.hashValue == hashedName)
266     {
267       if(!item.hasCollision || item.uniformInfo.name == name)
268       {
269         out = item.uniformInfo;
270         return true;
271       }
272       else
273       {
274         return false;
275       }
276     }
277   }
278   return false;
279 }
280
281 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
282 {
283   if(mReflectionDefaultUniforms.empty())
284   {
285     return nullptr;
286   }
287
288   const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
289   return &value->uniformInfo;
290 }
291
292 } // namespace Internal
293
294 } // namespace Dali