2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "test-graphics-reflection.h"
18 #include "test-graphics-controller.h"
19 #include "test-graphics-shader.h"
21 #include <dali/public-api/object/property-map.h>
27 void tet_infoline(const char* str);
28 void tet_printf(const char* format, ...);
35 static const std::vector<UniformData> UNIFORMS =
37 UniformData("uColor", Property::Type::VECTOR4),
38 UniformData("uRendererColor", Property::Type::FLOAT),
39 UniformData("uCustom", Property::Type::INTEGER),
40 UniformData("uCustom3", Property::Type::VECTOR3),
41 UniformData("uFadeColor", Property::Type::VECTOR4),
42 UniformData("uUniform1", Property::Type::VECTOR4),
43 UniformData("uUniform2", Property::Type::VECTOR4),
44 UniformData("uUniform3", Property::Type::VECTOR4),
45 UniformData("uFadeProgress", Property::Type::FLOAT),
46 UniformData("uANormalMatrix", Property::Type::MATRIX3),
47 UniformData("sEffect", Property::Type::FLOAT),
48 UniformData("sTexture", Property::Type::FLOAT),
49 UniformData("sTextureRect", Property::Type::FLOAT),
50 UniformData("sGloss", Property::Type::FLOAT),
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("uScale", Property::Type::VECTOR3),
58 UniformData("uSize", Property::Type::VECTOR3),
59 UniformData("uViewMatrix", Property::Type::MATRIX),
60 UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
61 UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
63 // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
67 * Helper function that returns size of uniform datatypes based
70 constexpr int GetSizeForType(Property::Type type)
74 case Property::Type::BOOLEAN:
78 case Property::Type::FLOAT:
82 case Property::Type::INTEGER:
86 case Property::Type::VECTOR2:
88 return sizeof(Vector2);
90 case Property::Type::VECTOR3:
92 return sizeof(Vector3);
94 case Property::Type::VECTOR4:
96 return sizeof(Vector4);
98 case Property::Type::MATRIX3:
100 return sizeof(Matrix3);
102 case Property::Type::MATRIX:
104 return sizeof(Matrix);
115 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)
116 : mController(controller),
118 mCustomUniforms(customUniforms)
120 for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
122 Property::Map* vertexFormat = vfs[i].GetMap();
125 for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
127 auto key = vertexFormat->GetKeyAt(j);
128 if(key.type == Property::Key::STRING)
130 mAttributes.push_back(key.stringKey);
136 mDefaultUniformBlock.name = "";
137 mDefaultUniformBlock.members = {};
138 mDefaultUniformBlock.binding = 0;
139 mDefaultUniformBlock.descriptorSet = 0;
140 mDefaultUniformBlock.members.clear();
144 for(const auto& data : mCustomUniforms)
146 fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
148 auto iter = data.name.find("[", 0);
150 if(iter != std::string::npos)
152 auto baseName = data.name.substr(0, iter);
154 numElements = std::stoi(data.name.substr(iter));
159 iter = data.name.find("]");
161 if(iter != std::string::npos && iter + 1 != data.name.length())
163 suffix = data.name.substr(iter + 1); // If there is a suffix, it means it is an element of an array of struct
168 // Write multiple items
169 for(int i = 0; i < numElements; ++i)
171 std::stringstream elementNameStream;
172 elementNameStream << baseName << "[" << i << "]" << suffix;
173 mDefaultUniformBlock.members.emplace_back();
174 auto& item = mDefaultUniformBlock.members.back();
175 item.name = elementNameStream.str();
177 item.offsets.push_back(offset);
178 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
179 item.bufferIndex = 0;
180 item.uniformClass = Graphics::UniformClass::UNIFORM;
181 item.type = data.type;
182 offset += GetSizeForType(data.type);
187 // Write 1 item with multiple elements
188 mDefaultUniformBlock.members.emplace_back();
189 auto& item = mDefaultUniformBlock.members.back();
191 item.name = baseName;
193 item.bufferIndex = 0;
194 item.uniformClass = Graphics::UniformClass::UNIFORM;
195 item.type = data.type;
196 item.numElements = numElements;
198 for(int i = 0; i < numElements; ++i)
200 std::stringstream elementNameStream;
201 elementNameStream << baseName << "[" << i << "]";
202 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
203 item.offsets.push_back(offset);
204 offset += GetSizeForType(data.type);
210 // Write 1 item with 1 element
211 mDefaultUniformBlock.members.emplace_back();
212 auto& item = mDefaultUniformBlock.members.back();
213 item.name = data.name;
215 item.offsets.push_back(offset);
216 item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
217 item.bufferIndex = 0;
218 item.uniformClass = Graphics::UniformClass::UNIFORM;
219 item.type = data.type;
220 offset += GetSizeForType(data.type);
224 for(const auto& data : UNIFORMS)
226 mDefaultUniformBlock.members.emplace_back();
227 auto& item = mDefaultUniformBlock.members.back();
228 item.name = data.name;
230 item.offsets.push_back(offset);
231 item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
232 item.bufferIndex = 0;
233 item.uniformClass = Graphics::UniformClass::UNIFORM;
234 item.type = data.type;
235 offset += GetSizeForType(data.type);
238 mDefaultUniformBlock.size = offset;
240 mUniformBlocks.push_back(mDefaultUniformBlock);
241 for(auto& element : customUniformBlocks)
243 mUniformBlocks.push_back(element);
247 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
249 auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
250 if(iter != mAttributes.end())
252 return iter - mAttributes.begin();
254 else if(mController.AutoAttrCreation())
256 uint32_t location = mAttributes.size();
257 mAttributes.push_back(name);
264 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
266 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeFormat is unimplemented\n");
267 return Dali::Graphics::VertexInputAttributeFormat{};
270 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
272 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeName is unimplemented\n");
276 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
278 std::vector<uint32_t> locs;
279 for(uint32_t i = 0; i < mAttributes.size(); ++i)
286 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
288 return mUniformBlocks.size();
291 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
293 if(index >= mUniformBlocks.size())
297 return mUniformBlocks[index].binding;
300 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
302 if(index >= mUniformBlocks.size())
307 const auto& block = mUniformBlocks[index];
311 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
313 if(index >= mUniformBlocks.size())
318 const auto& block = mUniformBlocks[index];
320 out.name = block.name;
321 out.binding = block.binding;
322 out.descriptorSet = block.descriptorSet;
323 auto membersSize = block.members.size();
324 out.members.resize(membersSize);
325 out.size = block.size;
326 for(auto i = 0u; i < out.members.size(); ++i)
328 const auto& memberUniform = block.members[i];
329 out.members[i].name = memberUniform.name;
330 out.members[i].binding = block.binding;
331 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
332 out.members[i].offset = memberUniform.offsets[0];
333 out.members[i].location = memberUniform.locations[0];
334 out.members[i].elementCount = memberUniform.numElements;
340 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
342 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockLocations is unimplemented\n");
343 return std::vector<uint32_t>{};
346 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
348 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockName is unimplemented\n");
349 return std::string{};
352 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
354 if(blockIndex < mUniformBlocks.size())
356 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
364 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
366 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
368 return mUniformBlocks[blockIndex].members[memberLocation].name;
372 return std::string();
376 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
378 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
380 return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
388 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
390 tet_infoline("Warning, TestGraphicsReflection::GetNamedUniform is unimplemented\n");
394 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
396 tet_infoline("Warning, TestGraphicsReflection::GetSamplers is unimplemented\n");
397 static std::vector<Dali::Graphics::UniformInfo> samplers{};
401 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
403 return Graphics::ShaderLanguage::GLSL_3_1;