2 * Copyright (c) 2021 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.
18 #include "gles-graphics-reflection.h"
19 #include <dali/integration-api/gl-abstraction.h>
20 #include <dali/integration-api/gl-defines.h>
23 #include "egl-graphics-controller.h"
25 #include <GLES3/gl3.h>
26 #include <GLES3/gl31.h>
28 #include "gles-graphics-program.h"
34 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
39 return Dali::Graphics::VertexInputAttributeFormat::FLOAT;
41 return Dali::Graphics::VertexInputAttributeFormat::VEC2;
43 return Dali::Graphics::VertexInputAttributeFormat::VEC3;
45 return Dali::Graphics::VertexInputAttributeFormat::VEC4;
47 return Dali::Graphics::VertexInputAttributeFormat::INTEGER;
49 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
53 uint32_t GetGLDataTypeSize(GLenum type)
55 // There are many more types than what are covered here, but
56 // they are not supported in dali.
59 case GL_FLOAT: // "float", 1 float, 4 bytes
61 case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
63 case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
65 case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
67 case GL_INT: // "int", 1 integer, 4 bytes
69 case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
71 case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
73 case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
80 bool IsSampler(GLenum type)
82 return type == GL_SAMPLER_2D || type == GL_SAMPLER_3D;
85 bool SortByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
87 return a.location < b.location;
92 namespace Dali::Graphics::GLES
94 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
95 : Graphics::Reflection(),
96 mController(controller),
101 Reflection::~Reflection() = default;
103 void Reflection::BuildVertexAttributeReflection()
105 auto glProgram = mProgram.GetGlProgram();
107 int written, size, location, maxLength, nAttribs;
111 auto gl = mController.GetGL();
113 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
114 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
116 mVertexInputAttributes.clear();
117 mVertexInputAttributes.resize(nAttribs);
119 name = new GLchar[maxLength];
120 for(int i = 0; i < nAttribs; i++)
122 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
123 location = gl->GetAttribLocation(glProgram, name);
125 AttributeInfo attributeInfo;
126 attributeInfo.location = location;
127 attributeInfo.name = name;
128 attributeInfo.format = GetVertexAttributeTypeFormat(type);
130 mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
135 void Reflection::BuildUniformReflection()
137 auto glProgram = mProgram.GetGlProgram();
144 auto gl = mController.GetGL();
146 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
147 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
149 mUniformBlocks.clear();
150 mDefaultUniformBlock.members.clear();
151 mUniformOpaques.clear();
153 name = new char[maxLen];
155 using UniformLocationSizePair = std::pair<uint32_t, uint32_t>;
156 std::vector<UniformLocationSizePair> uniformSizes;
158 uniformSizes.reserve(numUniforms);
160 for(int i = 0; i < numUniforms; ++i)
165 gl->GetActiveUniform(glProgram, i, maxLen, &written, &size, &type, name);
166 int location = gl->GetUniformLocation(glProgram, name);
167 uniformSizes.push_back(std::make_pair(location, GetGLDataTypeSize(type)));
169 Dali::Graphics::UniformInfo uniformInfo;
170 uniformInfo.name = name;
171 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
172 uniformInfo.location = IsSampler(type) ? 0 : location;
173 uniformInfo.binding = IsSampler(type) ? location : 0;
174 uniformInfo.bufferIndex = 0;
178 mUniformOpaques.push_back(uniformInfo);
182 mDefaultUniformBlock.members.push_back(uniformInfo);
186 // Re-order according to uniform locations.
187 if(mDefaultUniformBlock.members.size() > 1)
189 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortByLocation);
192 if(mUniformOpaques.size() > 1)
194 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortByLocation);
197 // Calculate the uniform offset
198 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
202 mDefaultUniformBlock.members[i].offset = 0;
206 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
207 auto previousUniform = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&previousUniformLocation](const UniformLocationSizePair& iter) { return iter.first == previousUniformLocation; });
208 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + previousUniform->second;
212 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
213 auto lastUniform = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&lastUniformLocation](const UniformLocationSizePair& iter) { return iter.first == lastUniformLocation; });
214 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + lastUniform->second;
216 mUniformBlocks.push_back(mDefaultUniformBlock);
221 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
222 void Reflection::BuildUniformBlockReflection()
224 auto gl = mController.GetGL();
225 auto glProgram = mProgram.GetGlProgram();
226 int numUniformBlocks = 0;
227 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
229 mUniformBlocks.clear();
230 mUniformBlocks.resize(numUniformBlocks);
232 int uniformBlockMaxLength = 0;
233 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
235 char* uniformBlockName = new char[uniformBlockMaxLength];
236 for(int i = 0; i < numUniformBlocks; i++)
241 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
242 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
243 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
245 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
246 uniformBlockInfo.name = uniformBlockName;
247 uniformBlockInfo.size = blockDataSize;
248 uniformBlockInfo.binding = blockBinding;
251 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
252 int* unifIndexes = new GLint[nUnis];
253 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
256 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
258 for(int unif = 0; unif < nUnis; ++unif)
260 int uniIndex = unifIndexes[unif];
264 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
265 int location = gl->GetUniformLocation(glProgram, uniformName);
267 Dali::Graphics::UniformInfo uniform;
268 uniform.name = uniformName;
269 uniform.location = location;
270 uniformBlockInfo.members.push_back(uniform);
273 delete[] unifIndexes;
275 mUniformBlocks.push_back(uniformBlockInfo);
277 delete[] uniformBlockName;
280 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
282 for(auto&& attr : mVertexInputAttributes)
284 if(attr.name == name)
286 return attr.location;
289 return ERROR_ATTRIBUTE_NOT_FOUND;
292 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
294 if(location >= mVertexInputAttributes.size())
296 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
299 return mVertexInputAttributes[location].format;
302 std::string Reflection::GetVertexAttributeName(uint32_t location) const
304 if(location >= mVertexInputAttributes.size())
306 return std::string();
309 return mVertexInputAttributes[location].name;
312 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
314 std::vector<uint32_t> locations;
315 for(auto&& attr : mVertexInputAttributes)
317 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
319 locations.push_back(attr.location);
326 uint32_t Reflection::GetUniformBlockCount() const
328 return mUniformBlocks.size();
331 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
333 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
336 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
338 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
341 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
343 if(index >= mUniformBlocks.size())
348 const auto& block = mUniformBlocks[index];
350 out.name = block.name;
351 out.binding = block.binding;
352 out.descriptorSet = block.descriptorSet;
353 auto membersSize = block.members.size();
354 out.members.resize(membersSize);
355 out.size = block.size;
356 for(auto i = 0u; i < out.members.size(); ++i)
358 const auto& memberUniform = block.members[i];
359 out.members[i].name = memberUniform.name;
360 out.members[i].binding = block.binding;
361 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
362 out.members[i].offset = memberUniform.offset;
363 out.members[i].location = memberUniform.location;
369 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
371 std::vector<uint32_t> retval{};
372 for(auto&& ubo : mUniformBlocks)
374 retval.emplace_back(ubo.binding);
379 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
381 if(blockIndex < mUniformBlocks.size())
383 return mUniformBlocks[blockIndex].name;
387 return std::string();
391 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
393 if(blockIndex < mUniformBlocks.size())
395 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
403 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
405 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
407 return mUniformBlocks[blockIndex].members[memberLocation].name;
411 return std::string();
415 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
417 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
419 return mUniformBlocks[blockIndex].members[memberLocation].offset;
427 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
430 for(auto&& ubo : mUniformBlocks)
432 for(auto&& member : ubo.members)
434 if(name == member.name || name == (ubo.name + "." + member.name))
437 out.location = member.location;
438 out.binding = ubo.binding;
439 out.bufferIndex = index;
440 out.offset = member.offset;
441 out.uniformClass = Graphics::UniformClass::UNIFORM;
449 for(auto&& uniform : mUniformOpaques)
451 if(uniform.name == name)
453 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
454 out.binding = uniform.binding;
457 out.location = uniform.location;
465 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
467 return mUniformOpaques;
470 Graphics::ShaderLanguage Reflection::GetLanguage() const
472 auto gl = mController.GetGL();
474 int majorVersion, minorVersion;
475 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
476 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
477 printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
478 printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
480 // TODO: the language version is hardcoded for now, but we may use what we get
481 // from GL_SHADING_LANGUAGE_VERSION?
482 return Graphics::ShaderLanguage::GLSL_3_2;
485 } // namespace Dali::Graphics::GLES