Test harness for UBO
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-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 <dali/public-api/object/property-map.h>
19 #include <vector>
20 #include <string>
21 namespace Dali
22 {
23 namespace
24 {
25 // Add members
26 struct UniformData
27 {
28   std::string     name;
29   Property::Type  type;
30   UniformData( const std::string& name, Property::Type type = Property::Type::NONE)
31   : name(name), type(type)
32   {}
33 };
34 static const std::vector<UniformData> UNIFORMS =
35                                         {
36                                           UniformData("uRendererColor",Property::Type::FLOAT),
37                                           UniformData("uCustom", Property::Type::INTEGER),
38                                           UniformData("uCustom3", Property::Type::VECTOR3),
39                                           UniformData("uFadeColor", Property::Type::VECTOR4),
40                                           UniformData("uUniform1", Property::Type::VECTOR4),
41                                           UniformData("uUniform2", Property::Type::VECTOR4),
42                                           UniformData("uUniform3", Property::Type::VECTOR4),
43                                           UniformData("uFadeProgress", Property::Type::FLOAT),
44                                           UniformData("uANormalMatrix", Property::Type::MATRIX3),
45                                           UniformData("sEffect", Property::Type::FLOAT),
46                                           UniformData("sTexture", Property::Type::FLOAT),
47                                           UniformData("sTextureRect", Property::Type::FLOAT),
48                                           UniformData("sGloss", Property::Type::FLOAT),
49                                           UniformData("uColor", Property::Type::VECTOR4),
50                                           UniformData("uModelMatrix", Property::Type::MATRIX),
51                                           UniformData("uModelView", Property::Type::MATRIX),
52                                           UniformData("uMvpMatrix", Property::Type::MATRIX),
53                                           UniformData("uNormalMatrix", Property::Type::MATRIX3),
54                                           UniformData("uProjection", Property::Type::MATRIX),
55                                           UniformData("uSize", Property::Type::VECTOR3),
56                                           UniformData("uViewMatrix", Property::Type::MATRIX),
57                                           UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
58                                           UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
59
60                                         };
61 }
62
63 TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, Property::Array& vfs)
64   : mGl(gl)
65 {
66   for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
67   {
68     Property::Map* vertexFormat = vfs[i].GetMap();
69     if(vertexFormat)
70     {
71       for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
72       {
73         auto key = vertexFormat->GetKeyAt(j);
74         if(key.type == Property::Key::STRING)
75         {
76           mAttributes.push_back(key.stringKey);
77         }
78       }
79     }
80   }
81 }
82
83 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
84 {
85   // Automatically assign locations to named attributes when requested
86   auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
87   if(iter != mAttributes.end())
88   {
89     return iter - mAttributes.begin();
90   }
91   else
92   {
93     uint32_t location = mAttributes.size();
94     mAttributes.push_back(name);
95     return location;
96   }
97   return 0u;
98 }
99
100 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
101 {
102   return Dali::Graphics::VertexInputAttributeFormat{};
103 }
104
105 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
106 {
107   return 0u;
108 }
109
110 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
111 {
112   std::vector<uint32_t> locs;
113   for(uint32_t i = 0; i < mAttributes.size(); ++i)
114   {
115     locs.push_back(i);
116   }
117   return locs;
118 }
119
120 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
121 {
122   return 1u;
123 }
124
125 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
126 {
127   return 0u;
128 }
129
130 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
131 {
132   // 64 bytes per uniform (64 = 4x4 matrix)
133   // TODO: fix if array will be used
134   return 64 * UNIFORMS.size();
135 }
136
137 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
138 {
139   auto& info = out;
140   info.name = "";
141   info.members = {};
142   info.binding = 0;
143   info.size = 64 * UNIFORMS.size();
144   info.descriptorSet = 0;
145   info.members.clear();
146   int loc = 0;
147   for( const auto& data : UNIFORMS )
148   {
149     info.members.emplace_back();
150     auto& item = info.members.back();
151     item.name = data.name;
152     item.binding = 0;
153     item.offset = loc*64;
154     item.location = loc++;
155     item.bufferIndex = 0;
156     item.uniformClass = Graphics::UniformClass::UNIFORM;
157   }
158   return true;
159 }
160
161 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
162 {
163   return std::vector<uint32_t>{};
164 }
165
166 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
167 {
168   return std::string{};
169 }
170
171 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
172 {
173   return UNIFORMS.size();
174 }
175
176 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
177 {
178   return UNIFORMS[memberLocation].name;
179 }
180
181 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
182 {
183   return 0u;
184 }
185
186 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
187 {
188   return true;
189 }
190
191 std::vector<Dali::Graphics::UniformInfo> TestGraphicsReflection::GetSamplers() const
192 {
193   return std::vector<Dali::Graphics::UniformInfo>{};
194 }
195
196 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
197 {
198   return Graphics::ShaderLanguage::GLSL_3_1;
199 }
200
201 Dali::Property::Type TestGraphicsReflection::GetMemberType( int blockIndex, int location) const
202 {
203   return UNIFORMS[location].type;
204 }
205
206 } // namespace Dali