2 * Copyright (c) 2022 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>
27 static const std::vector<UniformData> UNIFORMS =
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),
54 // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
58 * Helper function that returns size of uniform datatypes based
61 constexpr int GetSizeForType(Property::Type type)
65 case Property::Type::BOOLEAN:
69 case Property::Type::FLOAT:
73 case Property::Type::INTEGER:
77 case Property::Type::VECTOR2:
79 return sizeof(Vector2);
81 case Property::Type::VECTOR3:
83 return sizeof(Vector3);
85 case Property::Type::VECTOR4:
87 return sizeof(Vector4);
89 case Property::Type::MATRIX3:
91 return sizeof(Matrix3);
93 case Property::Type::MATRIX:
95 return sizeof(Matrix);
106 TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, uint32_t programId, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
108 mCustomUniforms(customUniforms)
110 for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
112 Property::Map* vertexFormat = vfs[i].GetMap();
115 for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
117 auto key = vertexFormat->GetKeyAt(j);
118 if(key.type == Property::Key::STRING)
120 mAttributes.push_back(key.stringKey);
126 mDefaultUniformBlock.name = "";
127 mDefaultUniformBlock.members = {};
128 mDefaultUniformBlock.binding = 0;
129 mDefaultUniformBlock.descriptorSet = 0;
130 mDefaultUniformBlock.members.clear();
133 for(const auto& data : UNIFORMS)
135 mDefaultUniformBlock.members.emplace_back();
136 auto& item = mDefaultUniformBlock.members.back();
137 item.name = data.name;
139 item.offsets.push_back(offset);
140 item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
141 item.bufferIndex = 0;
142 item.uniformClass = Graphics::UniformClass::UNIFORM;
143 item.type = data.type;
144 offset += GetSizeForType(data.type);
147 for(const auto& data : mCustomUniforms)
149 fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
151 auto iter = data.name.find("[", 0);
153 if(iter != std::string::npos)
155 auto baseName = data.name.substr(0, iter);
157 numElements = std::stoi(data.name.substr(iter));
162 iter = data.name.find("]");
164 if(iter != std::string::npos && iter + 1 != data.name.length())
166 suffix = data.name.substr(iter + 1); // If there is a suffix, it means it is an element of an array of struct
171 // Write multiple items
172 for(int i = 0; i < numElements; ++i)
174 std::stringstream elementNameStream;
175 elementNameStream << baseName << "[" << i << "]" << suffix;
176 mDefaultUniformBlock.members.emplace_back();
177 auto& item = mDefaultUniformBlock.members.back();
178 item.name = elementNameStream.str();
180 item.offsets.push_back(offset);
181 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
182 item.bufferIndex = 0;
183 item.uniformClass = Graphics::UniformClass::UNIFORM;
184 item.type = data.type;
185 offset += GetSizeForType(data.type);
190 // Write 1 item with multiple elements
191 mDefaultUniformBlock.members.emplace_back();
192 auto& item = mDefaultUniformBlock.members.back();
194 item.name = baseName;
196 item.bufferIndex = 0;
197 item.uniformClass = Graphics::UniformClass::UNIFORM;
198 item.type = data.type;
199 item.numElements = numElements;
201 for(int i = 0; i < numElements; ++i)
203 std::stringstream elementNameStream;
204 elementNameStream << baseName << "[" << i << "]";
205 item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
206 item.offsets.push_back(offset);
207 offset += GetSizeForType(data.type);
213 // Write 1 item with 1 element
214 mDefaultUniformBlock.members.emplace_back();
215 auto& item = mDefaultUniformBlock.members.back();
216 item.name = data.name;
218 item.offsets.push_back(offset);
219 item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
220 item.bufferIndex = 0;
221 item.uniformClass = Graphics::UniformClass::UNIFORM;
222 item.type = data.type;
223 offset += GetSizeForType(data.type);
226 mDefaultUniformBlock.size = offset;
228 mUniformBlocks.push_back(mDefaultUniformBlock);
231 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
233 // Automatically assign locations to named attributes when requested
234 auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
235 if(iter != mAttributes.end())
237 return iter - mAttributes.begin();
241 uint32_t location = mAttributes.size();
242 mAttributes.push_back(name);
248 Dali::Graphics::VertexInputAttributeFormat TestGraphicsReflection::GetVertexAttributeFormat(uint32_t location) const
250 return Dali::Graphics::VertexInputAttributeFormat{};
253 std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) const
258 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
260 std::vector<uint32_t> locs;
261 for(uint32_t i = 0; i < mAttributes.size(); ++i)
268 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
270 return mUniformBlocks.size();
273 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
278 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
280 if(index >= mUniformBlocks.size())
285 const auto& block = mUniformBlocks[index];
289 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
291 if(index >= mUniformBlocks.size())
296 const auto& block = mUniformBlocks[index];
298 out.name = block.name;
299 out.binding = block.binding;
300 out.descriptorSet = block.descriptorSet;
301 auto membersSize = block.members.size();
302 out.members.resize(membersSize);
303 out.size = block.size;
304 for(auto i = 0u; i < out.members.size(); ++i)
306 const auto& memberUniform = block.members[i];
307 out.members[i].name = memberUniform.name;
308 out.members[i].binding = block.binding;
309 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
310 out.members[i].offset = memberUniform.offsets[0];
311 out.members[i].location = memberUniform.locations[0];
317 std::vector<uint32_t> TestGraphicsReflection::GetUniformBlockLocations() const
319 return std::vector<uint32_t>{};
322 std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) const
324 return std::string{};
327 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
329 if(blockIndex < mUniformBlocks.size())
331 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
339 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
341 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
343 return mUniformBlocks[blockIndex].members[memberLocation].name;
347 return std::string();
351 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
353 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
355 return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
363 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
368 const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
370 static std::vector<Dali::Graphics::UniformInfo> samplers{};
374 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
376 return Graphics::ShaderLanguage::GLSL_3_1;