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