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