Updated visuals to use VisualRenderer
[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 TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
59 : mGl(gl),
60   mCustomUniforms(customUniforms)
61 {
62   for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
63   {
64     Property::Map* vertexFormat = vfs[i].GetMap();
65     if(vertexFormat)
66     {
67       for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
68       {
69         auto key = vertexFormat->GetKeyAt(j);
70         if(key.type == Property::Key::STRING)
71         {
72           mAttributes.push_back(key.stringKey);
73         }
74       }
75     }
76   }
77
78   mDefaultUniformBlock.name          = "";
79   mDefaultUniformBlock.members       = {};
80   mDefaultUniformBlock.binding       = 0;
81   mDefaultUniformBlock.size          = 64 * (UNIFORMS.size() + mCustomUniforms.size());
82   mDefaultUniformBlock.descriptorSet = 0;
83   mDefaultUniformBlock.members.clear();
84   int loc = 0;
85   for(const auto& data : UNIFORMS)
86   {
87     mDefaultUniformBlock.members.emplace_back();
88     auto& item        = mDefaultUniformBlock.members.back();
89     item.name         = data.name;
90     item.binding      = 0;
91     item.offset       = loc * 64;
92     item.location     = loc++;
93     item.bufferIndex  = 0;
94     item.uniformClass = Graphics::UniformClass::UNIFORM;
95   }
96
97   for(const auto& data : mCustomUniforms)
98   {
99     fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
100     mDefaultUniformBlock.members.emplace_back();
101     auto& item        = mDefaultUniformBlock.members.back();
102     item.name         = data.name;
103     item.binding      = 0;
104     item.offset       = loc * 64;
105     item.location     = loc++;
106     item.bufferIndex  = 0;
107     item.uniformClass = Graphics::UniformClass::UNIFORM;
108   }
109
110   mUniformBlocks.push_back(mDefaultUniformBlock);
111 }
112
113 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
114 {
115   // Automatically assign locations to named attributes when requested
116   auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
117   if(iter != mAttributes.end())
118   {
119     return iter - mAttributes.begin();
120   }
121   else
122   {
123     uint32_t location = mAttributes.size();
124     mAttributes.push_back(name);
125     return location;
126   }
127   return 0u;
128 }
129
130 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
131 {
132   return Dali::Graphics::VertexInputAttributeFormat{};
133 }
134
135 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
136 {
137   return 0u;
138 }
139
140 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
141 {
142   std::vector<uint32_t> locs;
143   for(uint32_t i = 0; i < mAttributes.size(); ++i)
144   {
145     locs.push_back(i);
146   }
147   return locs;
148 }
149
150 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
151 {
152   return mUniformBlocks.size();
153 }
154
155 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
156 {
157   return 0u;
158 }
159
160 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
161 {
162   // 64 bytes per uniform (64 = 4x4 matrix)
163   // TODO: fix if array will be used
164   return 64 * (UNIFORMS.size() + mCustomUniforms.size());
165 }
166
167 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
168 {
169   if(index >= mUniformBlocks.size())
170   {
171     return false;
172   }
173
174   const auto& block = mUniformBlocks[index];
175
176   out.name          = block.name;
177   out.binding       = block.binding;
178   out.descriptorSet = block.descriptorSet;
179   auto membersSize  = block.members.size();
180   out.members.resize(membersSize);
181   out.size = block.size;
182   for(auto i = 0u; i < out.members.size(); ++i)
183   {
184     const auto& memberUniform   = block.members[i];
185     out.members[i].name         = memberUniform.name;
186     out.members[i].binding      = block.binding;
187     out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
188     out.members[i].offset       = memberUniform.offset;
189     out.members[i].location     = memberUniform.location;
190   }
191
192   return true;
193 }
194
195 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
196 {
197   return std::vector<uint32_t>{};
198 }
199
200 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
201 {
202   return std::string{};
203 }
204
205 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
206 {
207   if(blockIndex < mUniformBlocks.size())
208   {
209     return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
210   }
211   else
212   {
213     return 0u;
214   }
215 }
216
217 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
218 {
219   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
220   {
221     return mUniformBlocks[blockIndex].members[memberLocation].name;
222   }
223   else
224   {
225     return std::string();
226   }
227 }
228
229 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
230 {
231   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
232   {
233     return mUniformBlocks[blockIndex].members[memberLocation].offset;
234   }
235   else
236   {
237     return 0u;
238   }
239 }
240
241 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
242 {
243   return true;
244 }
245
246 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
247 {
248   static std::vector<Dali::Graphics::UniformInfo> samplers{};
249   return samplers;
250 }
251
252 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
253 {
254   return Graphics::ShaderLanguage::GLSL_3_1;
255 }
256
257 Dali::Property::Type TestGraphicsReflection::GetMemberType(int blockIndex, int location) const
258 {
259   return location < static_cast<int>(UNIFORMS.size()) ? UNIFORMS[location].type : mCustomUniforms[location - UNIFORMS.size()].type;
260 }
261
262 } // namespace Dali