5b3da8c95b46705fe008290ad0c4ed791cc28541
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-reflection.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 #include "test-graphics-reflection.h"
18 #include "test-graphics-shader.h"
19
20 #include <dali/public-api/object/property-map.h>
21 #include <string>
22 #include <vector>
23 namespace Dali
24 {
25 namespace
26 {
27 static const std::vector<UniformData> UNIFORMS =
28   {
29     UniformData("uRendererColor", Property::Type::FLOAT),
30     UniformData("uCustom", Property::Type::INTEGER),
31     UniformData("uCustom3", Property::Type::VECTOR3),
32     UniformData("uFadeColor", Property::Type::VECTOR4),
33     UniformData("uUniform1", Property::Type::VECTOR4),
34     UniformData("uUniform2", Property::Type::VECTOR4),
35     UniformData("uUniform3", Property::Type::VECTOR4),
36     UniformData("uFadeProgress", Property::Type::FLOAT),
37     UniformData("uANormalMatrix", Property::Type::MATRIX3),
38     UniformData("sEffect", Property::Type::FLOAT),
39     UniformData("sTexture", Property::Type::FLOAT),
40     UniformData("sTextureRect", Property::Type::FLOAT),
41     UniformData("sGloss", Property::Type::FLOAT),
42     UniformData("uColor", Property::Type::VECTOR4),
43     UniformData("uActorColor", Property::Type::VECTOR4),
44     UniformData("uModelMatrix", Property::Type::MATRIX),
45     UniformData("uModelView", Property::Type::MATRIX),
46     UniformData("uMvpMatrix", Property::Type::MATRIX),
47     UniformData("uNormalMatrix", Property::Type::MATRIX3),
48     UniformData("uProjection", Property::Type::MATRIX),
49     UniformData("uSize", Property::Type::VECTOR3),
50     UniformData("uViewMatrix", Property::Type::MATRIX),
51     UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
52     UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
53
54     // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
55 };
56
57 /**
58  * Helper function that returns size of uniform datatypes based
59  * on property type.
60  */
61 constexpr int GetSizeForType(Property::Type type)
62 {
63   switch(type)
64   {
65     case Property::Type::BOOLEAN:
66     {
67       return sizeof(bool);
68     }
69     case Property::Type::FLOAT:
70     {
71       return sizeof(float);
72     }
73     case Property::Type::INTEGER:
74     {
75       return sizeof(int);
76     }
77     case Property::Type::VECTOR2:
78     {
79       return sizeof(Vector2);
80     }
81     case Property::Type::VECTOR3:
82     {
83       return sizeof(Vector3);
84     }
85     case Property::Type::VECTOR4:
86     {
87       return sizeof(Vector4);
88     }
89     case Property::Type::MATRIX3:
90     {
91       return sizeof(Matrix3);
92     }
93     case Property::Type::MATRIX:
94     {
95       return sizeof(Matrix);
96     }
97     default:
98     {
99       return 0;
100     }
101   };
102 }
103
104 } // namespace
105
106 TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, uint32_t programId, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
107 : mGl(gl),
108   mCustomUniforms(customUniforms)
109 {
110   for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
111   {
112     Property::Map* vertexFormat = vfs[i].GetMap();
113     if(vertexFormat)
114     {
115       for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
116       {
117         auto key = vertexFormat->GetKeyAt(j);
118         if(key.type == Property::Key::STRING)
119         {
120           mAttributes.push_back(key.stringKey);
121         }
122       }
123     }
124   }
125
126   mDefaultUniformBlock.name          = "";
127   mDefaultUniformBlock.members       = {};
128   mDefaultUniformBlock.binding       = 0;
129   mDefaultUniformBlock.descriptorSet = 0;
130   mDefaultUniformBlock.members.clear();
131
132   int offset = 0;
133   for(const auto& data : UNIFORMS)
134   {
135     mDefaultUniformBlock.members.emplace_back();
136     auto& item   = mDefaultUniformBlock.members.back();
137     item.name    = data.name;
138     item.binding = 0;
139     item.offsets.push_back(offset);
140     item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
141     item.bufferIndex  = 0;
142     item.uniformClass = Graphics::UniformClass::UNIFORM;
143     item.type         = data.type;
144     offset += GetSizeForType(data.type);
145   }
146
147   for(const auto& data : mCustomUniforms)
148   {
149     fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
150     mDefaultUniformBlock.members.emplace_back();
151     auto& item = mDefaultUniformBlock.members.back();
152
153     auto iter        = data.name.find("[", 0);
154     int  numElements = 1;
155     if(iter != std::string::npos)
156     {
157       auto baseName = data.name.substr(0, iter);
158       iter++;
159       numElements = std::stoi(data.name.substr(iter));
160       if(numElements == 0)
161       {
162         numElements = 1;
163       }
164
165       item.name         = baseName;
166       item.binding      = 0;
167       item.bufferIndex  = 0;
168       item.uniformClass = Graphics::UniformClass::UNIFORM;
169       item.type         = data.type;
170       item.numElements  = numElements;
171
172       for(int i = 0; i < numElements; ++i)
173       {
174         std::stringstream elementNameStream;
175         elementNameStream << baseName << "[" << i << "]";
176
177         item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
178         item.offsets.push_back(offset);
179         offset += GetSizeForType(data.type);
180       }
181     }
182     else
183     {
184       item.name    = data.name;
185       item.binding = 0;
186       item.offsets.push_back(offset);
187       item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
188       item.bufferIndex  = 0;
189       item.uniformClass = Graphics::UniformClass::UNIFORM;
190       item.type         = data.type;
191       offset += GetSizeForType(data.type);
192     }
193   }
194   mDefaultUniformBlock.size = offset;
195
196   mUniformBlocks.push_back(mDefaultUniformBlock);
197 }
198
199 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
200 {
201   // Automatically assign locations to named attributes when requested
202   auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
203   if(iter != mAttributes.end())
204   {
205     return iter - mAttributes.begin();
206   }
207   else
208   {
209     uint32_t location = mAttributes.size();
210     mAttributes.push_back(name);
211     return location;
212   }
213   return 0u;
214 }
215
216 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
217 {
218   return Dali::Graphics::VertexInputAttributeFormat{};
219 }
220
221 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
222 {
223   return 0u;
224 }
225
226 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
227 {
228   std::vector<uint32_t> locs;
229   for(uint32_t i = 0; i < mAttributes.size(); ++i)
230   {
231     locs.push_back(i);
232   }
233   return locs;
234 }
235
236 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
237 {
238   return mUniformBlocks.size();
239 }
240
241 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
242 {
243   return 0u;
244 }
245
246 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
247 {
248   if(index >= mUniformBlocks.size())
249   {
250     return 0;
251   }
252
253   const auto& block = mUniformBlocks[index];
254   return block.size;
255 }
256
257 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
258 {
259   if(index >= mUniformBlocks.size())
260   {
261     return false;
262   }
263
264   const auto& block = mUniformBlocks[index];
265
266   out.name          = block.name;
267   out.binding       = block.binding;
268   out.descriptorSet = block.descriptorSet;
269   auto membersSize  = block.members.size();
270   out.members.resize(membersSize);
271   out.size = block.size;
272   for(auto i = 0u; i < out.members.size(); ++i)
273   {
274     const auto& memberUniform   = block.members[i];
275     out.members[i].name         = memberUniform.name;
276     out.members[i].binding      = block.binding;
277     out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
278     out.members[i].offset       = memberUniform.offsets[0];
279     out.members[i].location     = memberUniform.locations[0];
280   }
281
282   return true;
283 }
284
285 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
286 {
287   return std::vector<uint32_t>{};
288 }
289
290 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
291 {
292   return std::string{};
293 }
294
295 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
296 {
297   if(blockIndex < mUniformBlocks.size())
298   {
299     return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
300   }
301   else
302   {
303     return 0u;
304   }
305 }
306
307 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
308 {
309   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
310   {
311     return mUniformBlocks[blockIndex].members[memberLocation].name;
312   }
313   else
314   {
315     return std::string();
316   }
317 }
318
319 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
320 {
321   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
322   {
323     return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
324   }
325   else
326   {
327     return 0u;
328   }
329 }
330
331 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
332 {
333   return true;
334 }
335
336 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
337 {
338   static std::vector<Dali::Graphics::UniformInfo> samplers{};
339   return samplers;
340 }
341
342 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
343 {
344   return Graphics::ShaderLanguage::GLSL_3_1;
345 }
346
347 } // namespace Dali