Cleanup in Aisle #2
[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 unsigned int NUMBER_OF_DEFAULT_UNIFORMS = static_cast<unsigned int>(Program::DefaultUniformIndex::COUNT);
48
49 /**
50  * List of all default uniforms used for quicker lookup
51  */
52 size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
53   {
54     CalculateHash(std::string("uModelMatrix")),
55     CalculateHash(std::string("uMvpMatrix")),
56     CalculateHash(std::string("uViewMatrix")),
57     CalculateHash(std::string("uModelView")),
58     CalculateHash(std::string("uNormalMatrix")),
59     CalculateHash(std::string("uProjection")),
60     CalculateHash(std::string("uSize")),
61     CalculateHash(std::string("uColor"))};
62
63 } // namespace
64
65 // IMPLEMENTATION
66
67 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
68 {
69   uint32_t programId{0u};
70
71   // Get program id and use it as hash for the cache
72   // in order to maintain current functionality as long as needed
73   gfxController.GetProgramParameter(*gfxProgram, 1, &programId);
74
75   size_t shaderHash = programId;
76
77   Program* program = cache.GetProgram(shaderHash);
78
79   if(nullptr == program)
80   {
81     // program not found so create it
82     program = new Program(cache, shaderData, gfxController, std::move(gfxProgram));
83     cache.AddProgram(shaderHash, program);
84   }
85
86   return program;
87 }
88
89 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram)
90 : mCache(cache),
91   mProjectionMatrix(nullptr),
92   mViewMatrix(nullptr),
93   mGfxProgram(std::move(gfxProgram)),
94   mGfxController(controller),
95   mProgramData(shaderData)
96 {
97   BuildReflection(controller.GetProgramReflection(*mGfxProgram.get()));
98 }
99
100 Program::~Program()
101 {
102 }
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       auto hashValue = CalculateHash(item.name);
121       mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
122
123       // update buffer index
124       mReflection.back().uniformInfo.bufferIndex = i;
125
126       // Update default uniforms
127       for(auto i = 0u; i < NUMBER_OF_DEFAULT_UNIFORMS; ++i)
128       {
129         if(hashValue == DEFAULT_UNIFORM_HASHTABLE[i])
130         {
131           mReflectionDefaultUniforms[i] = mReflection.back();
132           break;
133         }
134       }
135     }
136   }
137
138   // add samplers
139   auto samplers = graphicsReflection.GetSamplers();
140   for(const auto& sampler : samplers)
141   {
142     mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
143   }
144
145   // check for potential collisions
146   std::map<size_t, bool> hashTest;
147   bool                   hasCollisions(false);
148   for(auto&& item : mReflection)
149   {
150     if(hashTest.find(item.hashValue) == hashTest.end())
151     {
152       hashTest[item.hashValue] = false;
153     }
154     else
155     {
156       hashTest[item.hashValue] = true;
157       hasCollisions            = true;
158     }
159   }
160
161   // update collision flag for further use
162   if(hasCollisions)
163   {
164     for(auto&& item : mReflection)
165     {
166       item.hasCollision = hashTest[item.hashValue];
167     }
168   }
169 }
170
171 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
172 {
173   if(mReflection.empty())
174   {
175     return false;
176   }
177
178   hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
179
180   for(const ReflectionUniformInfo& item : mReflection)
181   {
182     if(item.hashValue == hashedName)
183     {
184       if(!item.hasCollision || item.uniformInfo.name == name)
185       {
186         out = item.uniformInfo;
187         return true;
188       }
189       else
190       {
191         return false;
192       }
193     }
194   }
195   return false;
196 }
197
198 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
199 {
200   if(mReflectionDefaultUniforms.empty())
201   {
202     return nullptr;
203   }
204
205   const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
206   return &value->uniformInfo;
207 }
208
209 } // namespace Internal
210
211 } // namespace Dali