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