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 || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
85 bool SortUniformInfoByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
87 return a.location < b.location;
90 bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExtraInfo a, Dali::Graphics::GLES::Reflection::UniformExtraInfo b)
92 return a.location < b.location;
97 namespace Dali::Graphics::GLES
99 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
100 : Graphics::Reflection(),
101 mController(controller),
106 Reflection::~Reflection() = default;
108 void Reflection::BuildVertexAttributeReflection()
110 auto glProgram = mProgram.GetGlProgram();
112 int written, size, location, maxLength, nAttribs;
116 auto gl = mController.GetGL();
118 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
119 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
121 mVertexInputAttributes.clear();
122 mVertexInputAttributes.resize(nAttribs);
124 name = new GLchar[maxLength];
125 for(int i = 0; i < nAttribs; i++)
127 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
128 location = gl->GetAttribLocation(glProgram, name);
132 AttributeInfo attributeInfo;
133 attributeInfo.location = location;
134 attributeInfo.name = name;
135 attributeInfo.format = GetVertexAttributeTypeFormat(type);
136 mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
142 void Reflection::BuildUniformReflection()
144 auto glProgram = mProgram.GetGlProgram();
151 auto gl = mController.GetGL();
153 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
154 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
156 mUniformBlocks.clear();
157 mDefaultUniformBlock.members.clear();
158 mUniformOpaques.clear();
160 name = new char[maxLen];
162 mStandaloneUniformExtraInfos.clear();
164 for(int i = 0; i < numUniforms; ++i)
169 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
170 int location = gl->GetUniformLocation(glProgram, name);
172 Dali::Graphics::UniformInfo uniformInfo;
174 uniformInfo.name = name;
177 auto iter = std::string(uniformInfo.name).find("[", 0);
178 if(iter != std::string::npos)
180 uniformInfo.name = std::string(name).substr(0, iter);
184 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
185 uniformInfo.location = IsSampler(type) ? 0 : location;
186 uniformInfo.binding = IsSampler(type) ? location : 0;
187 uniformInfo.bufferIndex = 0;
191 mUniformOpaques.push_back(uniformInfo);
195 mDefaultUniformBlock.members.push_back(uniformInfo);
196 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
200 // Re-order according to uniform locations.
202 if(mDefaultUniformBlock.members.size() > 1)
204 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
205 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
208 if(mUniformOpaques.size() > 1)
210 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
213 // Calculate the uniform offset
214 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
218 mDefaultUniformBlock.members[i].offset = 0;
222 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
223 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
224 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
225 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
229 if(mDefaultUniformBlock.members.size() > 0)
231 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
232 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
233 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
234 mUniformBlocks.push_back(mDefaultUniformBlock);
238 mDefaultUniformBlock.size = 0;
244 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
245 void Reflection::BuildUniformBlockReflection()
247 auto gl = mController.GetGL();
248 auto glProgram = mProgram.GetGlProgram();
249 int numUniformBlocks = 0;
250 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
252 mUniformBlocks.clear();
253 mUniformBlocks.resize(numUniformBlocks);
255 int uniformBlockMaxLength = 0;
256 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
258 char* uniformBlockName = new char[uniformBlockMaxLength];
259 for(int i = 0; i < numUniformBlocks; i++)
264 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
265 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
266 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
268 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
269 uniformBlockInfo.name = uniformBlockName;
270 uniformBlockInfo.size = blockDataSize;
271 uniformBlockInfo.binding = blockBinding;
274 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
275 int* unifIndexes = new GLint[nUnis];
276 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
279 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
281 for(int unif = 0; unif < nUnis; ++unif)
283 int uniIndex = unifIndexes[unif];
287 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
288 int location = gl->GetUniformLocation(glProgram, uniformName);
290 Dali::Graphics::UniformInfo uniform;
291 uniform.name = uniformName;
292 uniform.location = location;
293 uniformBlockInfo.members.push_back(uniform);
296 delete[] unifIndexes;
298 mUniformBlocks.push_back(uniformBlockInfo);
300 delete[] uniformBlockName;
303 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
305 for(auto&& attr : mVertexInputAttributes)
307 if(attr.name == name)
309 return attr.location;
312 return ERROR_ATTRIBUTE_NOT_FOUND;
315 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
317 if(location >= mVertexInputAttributes.size())
319 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
322 return mVertexInputAttributes[location].format;
325 std::string Reflection::GetVertexAttributeName(uint32_t location) const
327 if(location >= mVertexInputAttributes.size())
329 return std::string();
332 return mVertexInputAttributes[location].name;
335 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
337 std::vector<uint32_t> locations;
338 for(auto&& attr : mVertexInputAttributes)
340 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
342 locations.push_back(attr.location);
349 uint32_t Reflection::GetUniformBlockCount() const
351 return mUniformBlocks.size();
354 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
356 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
359 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
361 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
364 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
366 if(index >= mUniformBlocks.size())
371 const auto& block = mUniformBlocks[index];
373 out.name = block.name;
374 out.binding = block.binding;
375 out.descriptorSet = block.descriptorSet;
376 auto membersSize = block.members.size();
377 out.members.resize(membersSize);
378 out.size = block.size;
379 for(auto i = 0u; i < out.members.size(); ++i)
381 const auto& memberUniform = block.members[i];
382 out.members[i].name = memberUniform.name;
383 out.members[i].binding = block.binding;
384 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
385 out.members[i].offset = memberUniform.offset;
386 out.members[i].location = memberUniform.location;
392 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
394 std::vector<uint32_t> retval{};
395 for(auto&& ubo : mUniformBlocks)
397 retval.emplace_back(ubo.binding);
402 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
404 if(blockIndex < mUniformBlocks.size())
406 return mUniformBlocks[blockIndex].name;
410 return std::string();
414 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
416 if(blockIndex < mUniformBlocks.size())
418 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
426 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
428 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
430 return mUniformBlocks[blockIndex].members[memberLocation].name;
434 return std::string();
438 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
440 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
442 return mUniformBlocks[blockIndex].members[memberLocation].offset;
450 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
453 for(auto&& ubo : mUniformBlocks)
455 for(auto&& member : ubo.members)
457 if(name == member.name || name == (ubo.name + "." + member.name))
460 out.location = member.location;
461 out.binding = ubo.binding;
462 out.bufferIndex = index;
463 out.offset = member.offset;
464 out.uniformClass = Graphics::UniformClass::UNIFORM;
472 for(auto&& uniform : mUniformOpaques)
474 if(uniform.name == name)
476 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
477 out.binding = uniform.binding;
480 out.location = uniform.location;
488 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
490 std::vector<GLenum> retval{};
491 for(auto&& uniform : mStandaloneUniformExtraInfos)
493 retval.emplace_back(uniform.type);
499 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
501 return mStandaloneUniformExtraInfos;
504 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
506 return mUniformOpaques;
509 Graphics::ShaderLanguage Reflection::GetLanguage() const
511 auto gl = mController.GetGL();
513 int majorVersion, minorVersion;
514 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
515 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
516 printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
517 printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
519 // TODO: the language version is hardcoded for now, but we may use what we get
520 // from GL_SHADING_LANGUAGE_VERSION?
521 return Graphics::ShaderLanguage::GLSL_3_2;
524 } // namespace Dali::Graphics::GLES