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"
20 #include <dali/integration-api/debug.h>
21 #include <dali/integration-api/gl-abstraction.h>
22 #include <dali/integration-api/gl-defines.h>
25 #include "egl-graphics-controller.h"
27 #include <GLES3/gl3.h>
28 #include <GLES3/gl31.h>
30 #include "gles-graphics-program.h"
36 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
41 return Dali::Graphics::VertexInputAttributeFormat::FLOAT;
43 return Dali::Graphics::VertexInputAttributeFormat::VEC2;
45 return Dali::Graphics::VertexInputAttributeFormat::VEC3;
47 return Dali::Graphics::VertexInputAttributeFormat::VEC4;
49 return Dali::Graphics::VertexInputAttributeFormat::INTEGER;
51 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
55 uint32_t GetGLDataTypeSize(GLenum type)
57 // There are many more types than what are covered here, but
58 // they are not supported in dali.
61 case GL_FLOAT: // "float", 1 float, 4 bytes
63 case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
65 case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
67 case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
69 case GL_INT: // "int", 1 integer, 4 bytes
71 case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
73 case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
75 case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
82 bool IsSampler(GLenum type)
84 return type == GL_SAMPLER_2D || type == GL_SAMPLER_3D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
87 bool SortUniformInfoByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
89 return a.location < b.location;
92 bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExtraInfo a, Dali::Graphics::GLES::Reflection::UniformExtraInfo b)
94 return a.location < b.location;
99 namespace Dali::Graphics::GLES
101 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
102 : Graphics::Reflection(),
103 mController(controller),
108 Reflection::~Reflection() = default;
110 void Reflection::BuildVertexAttributeReflection()
112 auto glProgram = mProgram.GetGlProgram();
114 int written, size, location, maxLength, nAttribs;
118 auto gl = mController.GetGL();
121 // Do nothing during shutdown
125 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
126 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
128 mVertexInputAttributes.clear();
129 mVertexInputAttributes.resize(nAttribs);
131 name = new GLchar[maxLength];
132 for(int i = 0; i < nAttribs; i++)
134 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
135 location = gl->GetAttribLocation(glProgram, name);
139 AttributeInfo attributeInfo;
140 attributeInfo.location = location;
141 attributeInfo.name = name;
142 attributeInfo.format = GetVertexAttributeTypeFormat(type);
143 mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
149 void Reflection::BuildUniformReflection()
151 auto glProgram = mProgram.GetGlProgram();
158 auto gl = mController.GetGL();
161 // Do nothing during shutdown
165 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
166 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
168 mUniformBlocks.clear();
169 mDefaultUniformBlock.members.clear();
170 mUniformOpaques.clear();
172 name = new char[maxLen];
174 mStandaloneUniformExtraInfos.clear();
176 for(int i = 0; i < numUniforms; ++i)
181 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
182 int location = gl->GetUniformLocation(glProgram, name);
184 Dali::Graphics::UniformInfo uniformInfo;
186 uniformInfo.name = name;
189 auto iter = std::string(uniformInfo.name).find("[", 0);
190 if(iter != std::string::npos)
192 uniformInfo.name = std::string(name).substr(0, iter);
196 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
197 uniformInfo.location = IsSampler(type) ? 0 : location;
198 uniformInfo.binding = IsSampler(type) ? location : 0;
199 uniformInfo.bufferIndex = 0;
203 mUniformOpaques.push_back(uniformInfo);
207 mDefaultUniformBlock.members.push_back(uniformInfo);
208 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
212 // Re-order according to uniform locations.
214 if(mDefaultUniformBlock.members.size() > 1)
216 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
217 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
220 if(mUniformOpaques.size() > 1)
222 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
225 // Calculate the uniform offset
226 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
230 mDefaultUniformBlock.members[i].offset = 0;
234 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
235 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
236 if(previousUniform != mStandaloneUniformExtraInfos.end())
238 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
239 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
244 if(mDefaultUniformBlock.members.size() > 0)
246 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
247 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
248 if(lastUniform != mStandaloneUniformExtraInfos.end())
250 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
251 mUniformBlocks.push_back(mDefaultUniformBlock);
256 mDefaultUniformBlock.size = 0;
262 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
263 void Reflection::BuildUniformBlockReflection()
265 auto gl = mController.GetGL();
266 auto glProgram = mProgram.GetGlProgram();
267 int numUniformBlocks = 0;
271 // Do nothing during shutdown
275 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
277 mUniformBlocks.clear();
278 mUniformBlocks.resize(numUniformBlocks);
280 int uniformBlockMaxLength = 0;
281 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
283 char* uniformBlockName = new char[uniformBlockMaxLength];
284 for(int i = 0; i < numUniformBlocks; i++)
289 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
290 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
291 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
293 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
294 uniformBlockInfo.name = uniformBlockName;
295 uniformBlockInfo.size = blockDataSize;
296 uniformBlockInfo.binding = blockBinding;
299 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
300 int* unifIndexes = new GLint[nUnis];
301 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
304 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
306 for(int unif = 0; unif < nUnis; ++unif)
308 int uniIndex = unifIndexes[unif];
312 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
313 int location = gl->GetUniformLocation(glProgram, uniformName);
315 Dali::Graphics::UniformInfo uniform;
316 uniform.name = uniformName;
317 uniform.location = location;
318 uniformBlockInfo.members.push_back(uniform);
321 delete[] unifIndexes;
323 mUniformBlocks.push_back(uniformBlockInfo);
325 delete[] uniformBlockName;
328 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
330 for(auto&& attr : mVertexInputAttributes)
332 if(attr.name == name)
334 return attr.location;
337 return ERROR_ATTRIBUTE_NOT_FOUND;
340 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
342 if(location >= mVertexInputAttributes.size())
344 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
347 return mVertexInputAttributes[location].format;
350 std::string Reflection::GetVertexAttributeName(uint32_t location) const
352 if(location >= mVertexInputAttributes.size())
354 return std::string();
357 return mVertexInputAttributes[location].name;
360 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
362 std::vector<uint32_t> locations;
363 for(auto&& attr : mVertexInputAttributes)
365 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
367 locations.push_back(attr.location);
374 uint32_t Reflection::GetUniformBlockCount() const
376 return mUniformBlocks.size();
379 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
381 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
384 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
386 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
389 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
391 if(index >= mUniformBlocks.size())
396 const auto& block = mUniformBlocks[index];
398 out.name = block.name;
399 out.binding = block.binding;
400 out.descriptorSet = block.descriptorSet;
401 auto membersSize = block.members.size();
402 out.members.resize(membersSize);
403 out.size = block.size;
404 for(auto i = 0u; i < out.members.size(); ++i)
406 const auto& memberUniform = block.members[i];
407 out.members[i].name = memberUniform.name;
408 out.members[i].binding = block.binding;
409 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
410 out.members[i].offset = memberUniform.offset;
411 out.members[i].location = memberUniform.location;
417 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
419 std::vector<uint32_t> retval{};
420 for(auto&& ubo : mUniformBlocks)
422 retval.emplace_back(ubo.binding);
427 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
429 if(blockIndex < mUniformBlocks.size())
431 return mUniformBlocks[blockIndex].name;
435 return std::string();
439 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
441 if(blockIndex < mUniformBlocks.size())
443 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
451 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
453 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
455 return mUniformBlocks[blockIndex].members[memberLocation].name;
459 return std::string();
463 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
465 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
467 return mUniformBlocks[blockIndex].members[memberLocation].offset;
475 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
478 for(auto&& ubo : mUniformBlocks)
480 for(auto&& member : ubo.members)
482 if(name == member.name || name == (ubo.name + "." + member.name))
485 out.location = member.location;
486 out.binding = ubo.binding;
487 out.bufferIndex = index;
488 out.offset = member.offset;
489 out.uniformClass = Graphics::UniformClass::UNIFORM;
497 for(auto&& uniform : mUniformOpaques)
499 if(uniform.name == name)
501 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
502 out.binding = uniform.binding;
505 out.location = uniform.location;
513 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
515 std::vector<GLenum> retval{};
516 for(auto&& uniform : mStandaloneUniformExtraInfos)
518 retval.emplace_back(uniform.type);
524 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
526 return mStandaloneUniformExtraInfos;
529 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
531 return mUniformOpaques;
534 Graphics::ShaderLanguage Reflection::GetLanguage() const
536 auto version = Graphics::ShaderLanguage::GLSL_3_2;
538 auto gl = mController.GetGL();
541 // Do nothing during shutdown
545 int majorVersion, minorVersion;
546 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
547 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
548 DALI_LOG_RELEASE_INFO("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
549 DALI_LOG_RELEASE_INFO("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
551 // TODO: the language version is hardcoded for now, but we may use what we get
552 // from GL_SHADING_LANGUAGE_VERSION?
556 } // namespace Dali::Graphics::GLES