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);
127 AttributeInfo attributeInfo;
128 attributeInfo.location = location;
129 attributeInfo.name = name;
130 attributeInfo.format = GetVertexAttributeTypeFormat(type);
131 mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
137 void Reflection::BuildUniformReflection()
139 auto glProgram = mProgram.GetGlProgram();
146 auto gl = mController.GetGL();
148 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
149 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
151 mUniformBlocks.clear();
152 mDefaultUniformBlock.members.clear();
153 mUniformOpaques.clear();
155 name = new char[maxLen];
157 using UniformLocationSizePair = std::pair<uint32_t, uint32_t>;
158 std::vector<UniformLocationSizePair> uniformSizes;
160 uniformSizes.reserve(numUniforms);
162 for(int i = 0; i < numUniforms; ++i)
167 gl->GetActiveUniform(glProgram, i, maxLen, &written, &size, &type, name);
168 int location = gl->GetUniformLocation(glProgram, name);
169 uniformSizes.push_back(std::make_pair(location, GetGLDataTypeSize(type)));
171 Dali::Graphics::UniformInfo uniformInfo;
172 uniformInfo.name = name;
173 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
174 uniformInfo.location = IsSampler(type) ? 0 : location;
175 uniformInfo.binding = IsSampler(type) ? location : 0;
176 uniformInfo.bufferIndex = 0;
180 mUniformOpaques.push_back(uniformInfo);
184 mDefaultUniformBlock.members.push_back(uniformInfo);
188 // Re-order according to uniform locations.
189 if(mDefaultUniformBlock.members.size() > 1)
191 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortByLocation);
194 if(mUniformOpaques.size() > 1)
196 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortByLocation);
199 // Calculate the uniform offset
200 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
204 mDefaultUniformBlock.members[i].offset = 0;
208 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
209 auto previousUniform = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&previousUniformLocation](const UniformLocationSizePair& iter) { return iter.first == previousUniformLocation; });
210 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + previousUniform->second;
213 if(mDefaultUniformBlock.members.size() > 0)
215 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
216 auto lastUniform = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&lastUniformLocation](const UniformLocationSizePair& iter) { return iter.first == lastUniformLocation; });
217 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + lastUniform->second;
221 mDefaultUniformBlock.size = 0;
224 mUniformBlocks.push_back(mDefaultUniformBlock);
229 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
230 void Reflection::BuildUniformBlockReflection()
232 auto gl = mController.GetGL();
233 auto glProgram = mProgram.GetGlProgram();
234 int numUniformBlocks = 0;
235 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
237 mUniformBlocks.clear();
238 mUniformBlocks.resize(numUniformBlocks);
240 int uniformBlockMaxLength = 0;
241 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
243 char* uniformBlockName = new char[uniformBlockMaxLength];
244 for(int i = 0; i < numUniformBlocks; i++)
249 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
250 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
251 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
253 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
254 uniformBlockInfo.name = uniformBlockName;
255 uniformBlockInfo.size = blockDataSize;
256 uniformBlockInfo.binding = blockBinding;
259 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
260 int* unifIndexes = new GLint[nUnis];
261 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
264 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
266 for(int unif = 0; unif < nUnis; ++unif)
268 int uniIndex = unifIndexes[unif];
272 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
273 int location = gl->GetUniformLocation(glProgram, uniformName);
275 Dali::Graphics::UniformInfo uniform;
276 uniform.name = uniformName;
277 uniform.location = location;
278 uniformBlockInfo.members.push_back(uniform);
281 delete[] unifIndexes;
283 mUniformBlocks.push_back(uniformBlockInfo);
285 delete[] uniformBlockName;
288 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
290 for(auto&& attr : mVertexInputAttributes)
292 if(attr.name == name)
294 return attr.location;
297 return ERROR_ATTRIBUTE_NOT_FOUND;
300 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
302 if(location >= mVertexInputAttributes.size())
304 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
307 return mVertexInputAttributes[location].format;
310 std::string Reflection::GetVertexAttributeName(uint32_t location) const
312 if(location >= mVertexInputAttributes.size())
314 return std::string();
317 return mVertexInputAttributes[location].name;
320 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
322 std::vector<uint32_t> locations;
323 for(auto&& attr : mVertexInputAttributes)
325 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
327 locations.push_back(attr.location);
334 uint32_t Reflection::GetUniformBlockCount() const
336 return mUniformBlocks.size();
339 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
341 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
344 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
346 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
349 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
351 if(index >= mUniformBlocks.size())
356 const auto& block = mUniformBlocks[index];
358 out.name = block.name;
359 out.binding = block.binding;
360 out.descriptorSet = block.descriptorSet;
361 auto membersSize = block.members.size();
362 out.members.resize(membersSize);
363 out.size = block.size;
364 for(auto i = 0u; i < out.members.size(); ++i)
366 const auto& memberUniform = block.members[i];
367 out.members[i].name = memberUniform.name;
368 out.members[i].binding = block.binding;
369 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
370 out.members[i].offset = memberUniform.offset;
371 out.members[i].location = memberUniform.location;
377 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
379 std::vector<uint32_t> retval{};
380 for(auto&& ubo : mUniformBlocks)
382 retval.emplace_back(ubo.binding);
387 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
389 if(blockIndex < mUniformBlocks.size())
391 return mUniformBlocks[blockIndex].name;
395 return std::string();
399 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
401 if(blockIndex < mUniformBlocks.size())
403 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
411 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
413 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
415 return mUniformBlocks[blockIndex].members[memberLocation].name;
419 return std::string();
423 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
425 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
427 return mUniformBlocks[blockIndex].members[memberLocation].offset;
435 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
438 for(auto&& ubo : mUniformBlocks)
440 for(auto&& member : ubo.members)
442 if(name == member.name || name == (ubo.name + "." + member.name))
445 out.location = member.location;
446 out.binding = ubo.binding;
447 out.bufferIndex = index;
448 out.offset = member.offset;
449 out.uniformClass = Graphics::UniformClass::UNIFORM;
457 for(auto&& uniform : mUniformOpaques)
459 if(uniform.name == name)
461 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
462 out.binding = uniform.binding;
465 out.location = uniform.location;
473 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
475 return mUniformOpaques;
478 Graphics::ShaderLanguage Reflection::GetLanguage() const
480 auto gl = mController.GetGL();
482 int majorVersion, minorVersion;
483 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
484 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
485 printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
486 printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
488 // TODO: the language version is hardcoded for now, but we may use what we get
489 // from GL_SHADING_LANGUAGE_VERSION?
490 return Graphics::ShaderLanguage::GLSL_3_2;
493 } // namespace Dali::Graphics::GLES