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("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();
143 for(const auto& data : UNIFORMS)
145 mDefaultUniformBlock.members.emplace_back();
146 auto& item = mDefaultUniformBlock.members.back();
147 item.name = data.name;
149 item.offsets.push_back(offset);
150 item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
151 item.bufferIndex = 0;
152 item.uniformClass = Graphics::UniformClass::UNIFORM;
153 item.type = data.type;
154 offset += GetSizeForType(data.type);
157 for(const auto& data : mCustomUniforms)
159 fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
161 auto iter = data.name.find("[", 0);
163 if(iter != std::string::npos)
165 auto baseName = data.name.substr(0, iter);
167 numElements = std::stoi(data.name.substr(iter));
172 iter = data.name.find("]");
174 if(iter != std::string::npos && iter + 1 != data.name.length())
176 suffix = data.name.substr(iter + 1); // If there is a suffix, it means it is an element of an array of struct
181 // Write multiple items
182 for(int i = 0; i < numElements; ++i)
184 std::stringstream elementNameStream;
185 elementNameStream << baseName << "[" << i << "]" << suffix;
186 mDefaultUniformBlock.members.emplace_back();
187 auto& item = mDefaultUniformBlock.members.back();
188 item.name = elementNameStream.str();
190 item.offsets.push_back(offset);
191 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
192 item.bufferIndex = 0;
193 item.uniformClass = Graphics::UniformClass::UNIFORM;
194 item.type = data.type;
195 offset += GetSizeForType(data.type);
200 // Write 1 item with multiple elements
201 mDefaultUniformBlock.members.emplace_back();
202 auto& item = mDefaultUniformBlock.members.back();
204 item.name = baseName;
206 item.bufferIndex = 0;
207 item.uniformClass = Graphics::UniformClass::UNIFORM;
208 item.type = data.type;
209 item.numElements = numElements;
211 for(int i = 0; i < numElements; ++i)
213 std::stringstream elementNameStream;
214 elementNameStream << baseName << "[" << i << "]";
215 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
216 item.offsets.push_back(offset);
217 offset += GetSizeForType(data.type);
223 // Write 1 item with 1 element
224 mDefaultUniformBlock.members.emplace_back();
225 auto& item = mDefaultUniformBlock.members.back();
226 item.name = data.name;
228 item.offsets.push_back(offset);
229 item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
230 item.bufferIndex = 0;
231 item.uniformClass = Graphics::UniformClass::UNIFORM;
232 item.type = data.type;
233 offset += GetSizeForType(data.type);
236 mDefaultUniformBlock.size = offset;
238 mUniformBlocks.push_back(mDefaultUniformBlock);
239 for(auto& element : customUniformBlocks)
241 mUniformBlocks.push_back(element);
245 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
247 auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
248 if(iter != mAttributes.end())
250 return iter - mAttributes.begin();
252 else if(mController.AutoAttrCreation())
254 uint32_t location = mAttributes.size();
255 mAttributes.push_back(name);
262 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
264 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeFormat is unimplemented\n");
265 return Dali::Graphics::VertexInputAttributeFormat{};
268 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
270 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeName is unimplemented\n");
274 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
276 std::vector<uint32_t> locs;
277 for(uint32_t i = 0; i < mAttributes.size(); ++i)
284 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
286 return mUniformBlocks.size();
289 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
291 if(index >= mUniformBlocks.size())
295 return mUniformBlocks[index].binding;
298 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
300 if(index >= mUniformBlocks.size())
305 const auto& block = mUniformBlocks[index];
309 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
311 if(index >= mUniformBlocks.size())
316 const auto& block = mUniformBlocks[index];
318 out.name = block.name;
319 out.binding = block.binding;
320 out.descriptorSet = block.descriptorSet;
321 auto membersSize = block.members.size();
322 out.members.resize(membersSize);
323 out.size = block.size;
324 for(auto i = 0u; i < out.members.size(); ++i)
326 const auto& memberUniform = block.members[i];
327 out.members[i].name = memberUniform.name;
328 out.members[i].binding = block.binding;
329 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
330 out.members[i].offset = memberUniform.offsets[0];
331 out.members[i].location = memberUniform.locations[0];
337 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
339 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockLocations is unimplemented\n");
340 return std::vector<uint32_t>{};
343 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
345 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockName is unimplemented\n");
346 return std::string{};
349 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
351 if(blockIndex < mUniformBlocks.size())
353 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
361 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
363 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
365 return mUniformBlocks[blockIndex].members[memberLocation].name;
369 return std::string();
373 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
375 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
377 return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
385 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
387 tet_infoline("Warning, TestGraphicsReflection::GetNamedUniform is unimplemented\n");
391 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
393 tet_infoline("Warning, TestGraphicsReflection::GetSamplers is unimplemented\n");
394 static std::vector<Dali::Graphics::UniformInfo> samplers{};
398 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
400 return Graphics::ShaderLanguage::GLSL_3_1;