2 * Copyright (c) 2023 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("uRendererColor", Property::Type::FLOAT),
38 UniformData("uCustom", Property::Type::INTEGER),
39 UniformData("uCustom3", Property::Type::VECTOR3),
40 UniformData("uFadeColor", Property::Type::VECTOR4),
41 UniformData("uUniform1", Property::Type::VECTOR4),
42 UniformData("uUniform2", Property::Type::VECTOR4),
43 UniformData("uUniform3", Property::Type::VECTOR4),
44 UniformData("uFadeProgress", Property::Type::FLOAT),
45 UniformData("uANormalMatrix", Property::Type::MATRIX3),
46 UniformData("sEffect", Property::Type::FLOAT),
47 UniformData("sTexture", Property::Type::FLOAT),
48 UniformData("sTextureRect", Property::Type::FLOAT),
49 UniformData("sGloss", Property::Type::FLOAT),
50 UniformData("uColor", Property::Type::VECTOR4),
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("uSize", Property::Type::VECTOR3),
58 UniformData("uViewMatrix", Property::Type::MATRIX),
59 UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
60 UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
62 // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
66 * Helper function that returns size of uniform datatypes based
69 constexpr int GetSizeForType(Property::Type type)
73 case Property::Type::BOOLEAN:
77 case Property::Type::FLOAT:
81 case Property::Type::INTEGER:
85 case Property::Type::VECTOR2:
87 return sizeof(Vector2);
89 case Property::Type::VECTOR3:
91 return sizeof(Vector3);
93 case Property::Type::VECTOR4:
95 return sizeof(Vector4);
97 case Property::Type::MATRIX3:
99 return sizeof(Matrix3);
101 case Property::Type::MATRIX:
103 return sizeof(Matrix);
114 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)
115 : mController(controller),
117 mCustomUniforms(customUniforms)
119 for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
121 Property::Map* vertexFormat = vfs[i].GetMap();
124 for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
126 auto key = vertexFormat->GetKeyAt(j);
127 if(key.type == Property::Key::STRING)
129 mAttributes.push_back(key.stringKey);
135 mDefaultUniformBlock.name = "";
136 mDefaultUniformBlock.members = {};
137 mDefaultUniformBlock.binding = 0;
138 mDefaultUniformBlock.descriptorSet = 0;
139 mDefaultUniformBlock.members.clear();
142 for(const auto& data : UNIFORMS)
144 mDefaultUniformBlock.members.emplace_back();
145 auto& item = mDefaultUniformBlock.members.back();
146 item.name = data.name;
148 item.offsets.push_back(offset);
149 item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
150 item.bufferIndex = 0;
151 item.uniformClass = Graphics::UniformClass::UNIFORM;
152 item.type = data.type;
153 offset += GetSizeForType(data.type);
156 for(const auto& data : mCustomUniforms)
158 fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
160 auto iter = data.name.find("[", 0);
162 if(iter != std::string::npos)
164 auto baseName = data.name.substr(0, iter);
166 numElements = std::stoi(data.name.substr(iter));
171 iter = data.name.find("]");
173 if(iter != std::string::npos && iter + 1 != data.name.length())
175 suffix = data.name.substr(iter + 1); // If there is a suffix, it means it is an element of an array of struct
180 // Write multiple items
181 for(int i = 0; i < numElements; ++i)
183 std::stringstream elementNameStream;
184 elementNameStream << baseName << "[" << i << "]" << suffix;
185 mDefaultUniformBlock.members.emplace_back();
186 auto& item = mDefaultUniformBlock.members.back();
187 item.name = elementNameStream.str();
189 item.offsets.push_back(offset);
190 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
191 item.bufferIndex = 0;
192 item.uniformClass = Graphics::UniformClass::UNIFORM;
193 item.type = data.type;
194 offset += GetSizeForType(data.type);
199 // Write 1 item with multiple elements
200 mDefaultUniformBlock.members.emplace_back();
201 auto& item = mDefaultUniformBlock.members.back();
203 item.name = baseName;
205 item.bufferIndex = 0;
206 item.uniformClass = Graphics::UniformClass::UNIFORM;
207 item.type = data.type;
208 item.numElements = numElements;
210 for(int i = 0; i < numElements; ++i)
212 std::stringstream elementNameStream;
213 elementNameStream << baseName << "[" << i << "]";
214 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
215 item.offsets.push_back(offset);
216 offset += GetSizeForType(data.type);
222 // Write 1 item with 1 element
223 mDefaultUniformBlock.members.emplace_back();
224 auto& item = mDefaultUniformBlock.members.back();
225 item.name = data.name;
227 item.offsets.push_back(offset);
228 item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
229 item.bufferIndex = 0;
230 item.uniformClass = Graphics::UniformClass::UNIFORM;
231 item.type = data.type;
232 offset += GetSizeForType(data.type);
235 mDefaultUniformBlock.size = offset;
237 mUniformBlocks.push_back(mDefaultUniformBlock);
238 for(auto& element : customUniformBlocks)
240 mUniformBlocks.push_back(element);
244 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
246 auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
247 if(iter != mAttributes.end())
249 return iter - mAttributes.begin();
251 else if(mController.AutoAttrCreation())
253 uint32_t location = mAttributes.size();
254 mAttributes.push_back(name);
261 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
263 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeFormat is unimplemented\n");
264 return Dali::Graphics::VertexInputAttributeFormat{};
267 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
269 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeName is unimplemented\n");
273 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
275 std::vector<uint32_t> locs;
276 for(uint32_t i = 0; i < mAttributes.size(); ++i)
283 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
285 return mUniformBlocks.size();
288 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
290 if(index >= mUniformBlocks.size())
294 return mUniformBlocks[index].binding;
297 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
299 if(index >= mUniformBlocks.size())
304 const auto& block = mUniformBlocks[index];
308 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
310 if(index >= mUniformBlocks.size())
315 const auto& block = mUniformBlocks[index];
317 out.name = block.name;
318 out.binding = block.binding;
319 out.descriptorSet = block.descriptorSet;
320 auto membersSize = block.members.size();
321 out.members.resize(membersSize);
322 out.size = block.size;
323 for(auto i = 0u; i < out.members.size(); ++i)
325 const auto& memberUniform = block.members[i];
326 out.members[i].name = memberUniform.name;
327 out.members[i].binding = block.binding;
328 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
329 out.members[i].offset = memberUniform.offsets[0];
330 out.members[i].location = memberUniform.locations[0];
336 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
338 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockLocations is unimplemented\n");
339 return std::vector<uint32_t>{};
342 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
344 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockName is unimplemented\n");
345 return std::string{};
348 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
350 if(blockIndex < mUniformBlocks.size())
352 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
360 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
362 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
364 return mUniformBlocks[blockIndex].members[memberLocation].name;
368 return std::string();
372 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
374 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
376 return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
384 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
386 tet_infoline("Warning, TestGraphicsReflection::GetNamedUniform is unimplemented\n");
390 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
392 tet_infoline("Warning, TestGraphicsReflection::GetSamplers is unimplemented\n");
393 static std::vector<Dali::Graphics::UniformInfo> samplers{};
397 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
399 return Graphics::ShaderLanguage::GLSL_3_1;