56ba36246b5941c83aa7a8766e68a39730c0f892
[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 <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
61 /**
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
65  */
66 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
67 {
68   return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
69 }
70
71 } // namespace
72
73 // IMPLEMENTATION
74
75 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController)
76 {
77   size_t shaderHash = shaderData->GetHashValue();
78
79   Program* program = cache.GetProgram(shaderHash);
80
81   if(nullptr == program)
82   {
83     // program not found so create it
84     program = new Program(cache, shaderData, gfxController);
85     cache.AddProgram(shaderHash, program);
86   }
87
88   return program;
89 }
90
91 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller)
92 : mCache(cache),
93   mProjectionMatrix(nullptr),
94   mViewMatrix(nullptr),
95   mGfxProgram(nullptr),
96   mGfxController(controller),
97   mProgramData(shaderData)
98 {
99 }
100
101 Program::~Program() = default;
102
103 void Program::BuildReflection(const Graphics::Reflection& graphicsReflection)
104 {
105   mReflectionDefaultUniforms.clear();
106   mReflectionDefaultUniforms.resize(NUMBER_OF_DEFAULT_UNIFORMS);
107
108   auto uniformBlockCount = graphicsReflection.GetUniformBlockCount();
109
110   // add uniform block fields
111   for(auto i = 0u; i < uniformBlockCount; ++i)
112   {
113     Graphics::UniformBlockInfo uboInfo;
114     graphicsReflection.GetUniformBlock(i, uboInfo);
115
116     // for each member store data
117     for(const auto& item : uboInfo.members)
118     {
119       auto hashValue = CalculateHash(item.name);
120       mReflection.emplace_back(ReflectionUniformInfo{hashValue, false, item});
121
122       // update buffer index
123       mReflection.back().uniformInfo.bufferIndex = i;
124
125       // Update default uniforms
126       for(auto j = 0u; j < NUMBER_OF_DEFAULT_UNIFORMS; ++j)
127       {
128         if(hashValue == DEFAULT_UNIFORM_HASHTABLE[j])
129         {
130           mReflectionDefaultUniforms[j] = mReflection.back();
131           break;
132         }
133       }
134     }
135   }
136
137   // add samplers
138   auto samplers = graphicsReflection.GetSamplers();
139   for(const auto& sampler : samplers)
140   {
141     mReflection.emplace_back(ReflectionUniformInfo{CalculateHash(sampler.name), false, sampler});
142   }
143
144   // check for potential collisions
145   std::map<size_t, bool> hashTest;
146   bool                   hasCollisions(false);
147   for(auto&& item : mReflection)
148   {
149     if(hashTest.find(item.hashValue) == hashTest.end())
150     {
151       hashTest[item.hashValue] = false;
152     }
153     else
154     {
155       hashTest[item.hashValue] = true;
156       hasCollisions            = true;
157     }
158   }
159
160   // update collision flag for further use
161   if(hasCollisions)
162   {
163     for(auto&& item : mReflection)
164     {
165       item.hasCollision = hashTest[item.hashValue];
166     }
167   }
168
169   // Calculate size of memory for uniform blocks
170   mUniformBlockRequirements.totalSizeRequired = 0;
171   mUniformBlockRequirements.blockCount = graphicsReflection.GetUniformBlockCount();
172   for (auto i = 0u; i < mUniformBlockRequirements.blockCount; ++i)
173   {
174     auto blockSize = GetUniformBufferDataAlignment(graphicsReflection.GetUniformBlockSize(i));
175     mUniformBlockRequirements.totalSizeRequired += blockSize;
176   }
177 }
178
179 void Program::SetGraphicsProgram( Graphics::UniquePtr<Graphics::Program>&& program )
180 {
181   mGfxProgram = std::move(program);
182   BuildReflection(mGfxController.GetProgramReflection(*mGfxProgram.get()));
183 }
184
185
186 bool Program::GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const
187 {
188   if(mReflection.empty())
189   {
190     return false;
191   }
192
193   hashedName = !hashedName ? CalculateHash(name, '[') : hashedName;
194
195   for(const ReflectionUniformInfo& item : mReflection)
196   {
197     if(item.hashValue == hashedName)
198     {
199       if(!item.hasCollision || item.uniformInfo.name == name)
200       {
201         out = item.uniformInfo;
202         return true;
203       }
204       else
205       {
206         return false;
207       }
208     }
209   }
210   return false;
211 }
212
213 const Graphics::UniformInfo* Program::GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const
214 {
215   if(mReflectionDefaultUniforms.empty())
216   {
217     return nullptr;
218   }
219
220   const auto value = &mReflectionDefaultUniforms[static_cast<uint32_t>(defaultUniformIndex)];
221   return &value->uniformInfo;
222 }
223
224 } // namespace Internal
225
226 } // namespace Dali