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-shader.h"
20 #include <dali/public-api/object/property-map.h>
26 void tet_infoline(const char* str);
27 void tet_printf(const char* format, ...);
34 static const std::vector<UniformData> UNIFORMS =
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("uActorColor", Property::Type::VECTOR4),
51 UniformData("uModelMatrix", Property::Type::MATRIX),
52 UniformData("uModelView", Property::Type::MATRIX),
53 UniformData("uMvpMatrix", Property::Type::MATRIX),
54 UniformData("uNormalMatrix", Property::Type::MATRIX3),
55 UniformData("uProjection", Property::Type::MATRIX),
56 UniformData("uSize", Property::Type::VECTOR3),
57 UniformData("uViewMatrix", Property::Type::MATRIX),
58 UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
59 UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
61 // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
65 * Helper function that returns size of uniform datatypes based
68 constexpr int GetSizeForType(Property::Type type)
72 case Property::Type::BOOLEAN:
76 case Property::Type::FLOAT:
80 case Property::Type::INTEGER:
84 case Property::Type::VECTOR2:
86 return sizeof(Vector2);
88 case Property::Type::VECTOR3:
90 return sizeof(Vector3);
92 case Property::Type::VECTOR4:
94 return sizeof(Vector4);
96 case Property::Type::MATRIX3:
98 return sizeof(Matrix3);
100 case Property::Type::MATRIX:
102 return sizeof(Matrix);
113 TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, uint32_t programId, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms, std::vector<TestGraphicsReflection::TestUniformBlockInfo>& customUniformBlocks)
115 mCustomUniforms(customUniforms)
117 for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
119 Property::Map* vertexFormat = vfs[i].GetMap();
122 for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
124 auto key = vertexFormat->GetKeyAt(j);
125 if(key.type == Property::Key::STRING)
127 mAttributes.push_back(key.stringKey);
133 mDefaultUniformBlock.name = "";
134 mDefaultUniformBlock.members = {};
135 mDefaultUniformBlock.binding = 0;
136 mDefaultUniformBlock.descriptorSet = 0;
137 mDefaultUniformBlock.members.clear();
140 for(const auto& data : UNIFORMS)
142 mDefaultUniformBlock.members.emplace_back();
143 auto& item = mDefaultUniformBlock.members.back();
144 item.name = data.name;
146 item.offsets.push_back(offset);
147 item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
148 item.bufferIndex = 0;
149 item.uniformClass = Graphics::UniformClass::UNIFORM;
150 item.type = data.type;
151 offset += GetSizeForType(data.type);
154 for(const auto& data : mCustomUniforms)
156 fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
158 auto iter = data.name.find("[", 0);
160 if(iter != std::string::npos)
162 auto baseName = data.name.substr(0, iter);
164 numElements = std::stoi(data.name.substr(iter));
169 iter = data.name.find("]");
171 if(iter != std::string::npos && iter + 1 != data.name.length())
173 suffix = data.name.substr(iter + 1); // If there is a suffix, it means it is an element of an array of struct
178 // Write multiple items
179 for(int i = 0; i < numElements; ++i)
181 std::stringstream elementNameStream;
182 elementNameStream << baseName << "[" << i << "]" << suffix;
183 mDefaultUniformBlock.members.emplace_back();
184 auto& item = mDefaultUniformBlock.members.back();
185 item.name = elementNameStream.str();
187 item.offsets.push_back(offset);
188 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
189 item.bufferIndex = 0;
190 item.uniformClass = Graphics::UniformClass::UNIFORM;
191 item.type = data.type;
192 offset += GetSizeForType(data.type);
197 // Write 1 item with multiple elements
198 mDefaultUniformBlock.members.emplace_back();
199 auto& item = mDefaultUniformBlock.members.back();
201 item.name = baseName;
203 item.bufferIndex = 0;
204 item.uniformClass = Graphics::UniformClass::UNIFORM;
205 item.type = data.type;
206 item.numElements = numElements;
208 for(int i = 0; i < numElements; ++i)
210 std::stringstream elementNameStream;
211 elementNameStream << baseName << "[" << i << "]";
212 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
213 item.offsets.push_back(offset);
214 offset += GetSizeForType(data.type);
220 // Write 1 item with 1 element
221 mDefaultUniformBlock.members.emplace_back();
222 auto& item = mDefaultUniformBlock.members.back();
223 item.name = data.name;
225 item.offsets.push_back(offset);
226 item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
227 item.bufferIndex = 0;
228 item.uniformClass = Graphics::UniformClass::UNIFORM;
229 item.type = data.type;
230 offset += GetSizeForType(data.type);
233 mDefaultUniformBlock.size = offset;
235 mUniformBlocks.push_back(mDefaultUniformBlock);
236 for(auto& element : customUniformBlocks)
238 mUniformBlocks.push_back(element);
242 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
244 // Automatically assign locations to named attributes when requested
245 auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
246 if(iter != mAttributes.end())
248 return iter - mAttributes.begin();
252 uint32_t location = mAttributes.size();
253 mAttributes.push_back(name);
259 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
261 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeFormat is unimplemented\n");
262 return Dali::Graphics::VertexInputAttributeFormat{};
265 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
267 tet_infoline("Warning, TestGraphicsReflection::GetVertexAttributeName is unimplemented\n");
271 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
273 std::vector<uint32_t> locs;
274 for(uint32_t i = 0; i < mAttributes.size(); ++i)
281 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
283 return mUniformBlocks.size();
286 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
288 if(index >= mUniformBlocks.size())
292 return mUniformBlocks[index].binding;
295 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
297 if(index >= mUniformBlocks.size())
302 const auto& block = mUniformBlocks[index];
306 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
308 if(index >= mUniformBlocks.size())
313 const auto& block = mUniformBlocks[index];
315 out.name = block.name;
316 out.binding = block.binding;
317 out.descriptorSet = block.descriptorSet;
318 auto membersSize = block.members.size();
319 out.members.resize(membersSize);
320 out.size = block.size;
321 for(auto i = 0u; i < out.members.size(); ++i)
323 const auto& memberUniform = block.members[i];
324 out.members[i].name = memberUniform.name;
325 out.members[i].binding = block.binding;
326 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
327 out.members[i].offset = memberUniform.offsets[0];
328 out.members[i].location = memberUniform.locations[0];
334 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
336 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockLocations is unimplemented\n");
337 return std::vector<uint32_t>{};
340 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
342 tet_infoline("Warning, TestGraphicsReflection::GetUniformBlockName is unimplemented\n");
343 return std::string{};
346 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
348 if(blockIndex < mUniformBlocks.size())
350 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
358 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
360 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
362 return mUniformBlocks[blockIndex].members[memberLocation].name;
366 return std::string();
370 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
372 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
374 return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
382 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
384 tet_infoline("Warning, TestGraphicsReflection::GetNamedUniform is unimplemented\n");
388 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
390 tet_infoline("Warning, TestGraphicsReflection::GetSamplers is unimplemented\n");
391 static std::vector<Dali::Graphics::UniformInfo> samplers{};
395 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
397 return Graphics::ShaderLanguage::GLSL_3_1;