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;
214 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
215 auto lastUniform = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&lastUniformLocation](const UniformLocationSizePair& iter) { return iter.first == lastUniformLocation; });
216 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + lastUniform->second;
218 mUniformBlocks.push_back(mDefaultUniformBlock);
223 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
224 void Reflection::BuildUniformBlockReflection()
226 auto gl = mController.GetGL();
227 auto glProgram = mProgram.GetGlProgram();
228 int numUniformBlocks = 0;
229 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
231 mUniformBlocks.clear();
232 mUniformBlocks.resize(numUniformBlocks);
234 int uniformBlockMaxLength = 0;
235 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
237 char* uniformBlockName = new char[uniformBlockMaxLength];
238 for(int i = 0; i < numUniformBlocks; i++)
243 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
244 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
245 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
247 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
248 uniformBlockInfo.name = uniformBlockName;
249 uniformBlockInfo.size = blockDataSize;
250 uniformBlockInfo.binding = blockBinding;
253 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
254 int* unifIndexes = new GLint[nUnis];
255 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
258 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
260 for(int unif = 0; unif < nUnis; ++unif)
262 int uniIndex = unifIndexes[unif];
266 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
267 int location = gl->GetUniformLocation(glProgram, uniformName);
269 Dali::Graphics::UniformInfo uniform;
270 uniform.name = uniformName;
271 uniform.location = location;
272 uniformBlockInfo.members.push_back(uniform);
275 delete[] unifIndexes;
277 mUniformBlocks.push_back(uniformBlockInfo);
279 delete[] uniformBlockName;
282 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
284 for(auto&& attr : mVertexInputAttributes)
286 if(attr.name == name)
288 return attr.location;
291 return ERROR_ATTRIBUTE_NOT_FOUND;
294 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
296 if(location >= mVertexInputAttributes.size())
298 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
301 return mVertexInputAttributes[location].format;
304 std::string Reflection::GetVertexAttributeName(uint32_t location) const
306 if(location >= mVertexInputAttributes.size())
308 return std::string();
311 return mVertexInputAttributes[location].name;
314 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
316 std::vector<uint32_t> locations;
317 for(auto&& attr : mVertexInputAttributes)
319 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
321 locations.push_back(attr.location);
328 uint32_t Reflection::GetUniformBlockCount() const
330 return mUniformBlocks.size();
333 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
335 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
338 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
340 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
343 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
345 if(index >= mUniformBlocks.size())
350 const auto& block = mUniformBlocks[index];
352 out.name = block.name;
353 out.binding = block.binding;
354 out.descriptorSet = block.descriptorSet;
355 auto membersSize = block.members.size();
356 out.members.resize(membersSize);
357 out.size = block.size;
358 for(auto i = 0u; i < out.members.size(); ++i)
360 const auto& memberUniform = block.members[i];
361 out.members[i].name = memberUniform.name;
362 out.members[i].binding = block.binding;
363 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
364 out.members[i].offset = memberUniform.offset;
365 out.members[i].location = memberUniform.location;
371 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
373 std::vector<uint32_t> retval{};
374 for(auto&& ubo : mUniformBlocks)
376 retval.emplace_back(ubo.binding);
381 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
383 if(blockIndex < mUniformBlocks.size())
385 return mUniformBlocks[blockIndex].name;
389 return std::string();
393 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
395 if(blockIndex < mUniformBlocks.size())
397 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
405 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
407 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
409 return mUniformBlocks[blockIndex].members[memberLocation].name;
413 return std::string();
417 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
419 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
421 return mUniformBlocks[blockIndex].members[memberLocation].offset;
429 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
432 for(auto&& ubo : mUniformBlocks)
434 for(auto&& member : ubo.members)
436 if(name == member.name || name == (ubo.name + "." + member.name))
439 out.location = member.location;
440 out.binding = ubo.binding;
441 out.bufferIndex = index;
442 out.offset = member.offset;
443 out.uniformClass = Graphics::UniformClass::UNIFORM;
451 for(auto&& uniform : mUniformOpaques)
453 if(uniform.name == name)
455 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
456 out.binding = uniform.binding;
459 out.location = uniform.location;
467 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
469 return mUniformOpaques;
472 Graphics::ShaderLanguage Reflection::GetLanguage() const
474 auto gl = mController.GetGL();
476 int majorVersion, minorVersion;
477 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
478 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
479 printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
480 printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
482 // TODO: the language version is hardcoded for now, but we may use what we get
483 // from GL_SHADING_LANGUAGE_VERSION?
484 return Graphics::ShaderLanguage::GLSL_3_2;
487 } // namespace Dali::Graphics::GLES