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