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"
38 const char* const mString;
39 const uint32_t mLength;
41 template<uint32_t kLength>
42 constexpr StringSize(const char (&string)[kLength])
44 mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
48 operator const char*() const
54 bool operator==(const StringSize& lhs, const char* rhs)
56 return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
59 const char* const DELIMITERS = " \t\n";
60 constexpr StringSize UNIFORM{"uniform"};
61 constexpr StringSize SAMPLER_PREFIX{"sampler"};
62 constexpr StringSize SAMPLER_TYPES[] = {"2D", "Cube", "ExternalOES"};
63 constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
65 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
70 return Dali::Graphics::VertexInputAttributeFormat::FLOAT;
72 return Dali::Graphics::VertexInputAttributeFormat::VEC2;
74 return Dali::Graphics::VertexInputAttributeFormat::VEC3;
76 return Dali::Graphics::VertexInputAttributeFormat::VEC4;
78 return Dali::Graphics::VertexInputAttributeFormat::INTEGER;
80 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
84 uint32_t GetGLDataTypeSize(GLenum type)
86 // There are many more types than what are covered here, but
87 // they are not supported in dali.
90 case GL_FLOAT: // "float", 1 float, 4 bytes
92 case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
94 case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
96 case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
98 case GL_INT: // "int", 1 integer, 4 bytes
100 case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
102 case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
104 case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
111 bool IsSampler(GLenum type)
113 return type == GL_SAMPLER_2D || type == GL_SAMPLER_3D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
116 bool SortUniformInfoByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
118 return a.location < b.location;
121 bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExtraInfo a, Dali::Graphics::GLES::Reflection::UniformExtraInfo b)
123 return a.location < b.location;
126 std::string GetShaderSource(Dali::Graphics::ShaderState shaderState)
128 std::vector<uint8_t> data;
129 auto* shader = static_cast<const Dali::Graphics::GLES::Shader*>(shaderState.shader);
130 auto& shaderCreateInfo = shader->GetCreateInfo();
131 data.resize(shaderCreateInfo.sourceSize + 1);
132 std::memcpy(&data[0], shaderCreateInfo.sourceData, shaderCreateInfo.sourceSize);
133 data[shaderCreateInfo.sourceSize] = 0;
135 return std::string(reinterpret_cast<char*>(&data[0]));
138 void ParseShaderSamplers(std::string shaderSource, std::vector<Dali::Graphics::UniformInfo>& uniformOpaques, int& samplerPosition, std::vector<int>& samplerPositions)
140 if(!shaderSource.empty())
142 char* shaderStr = strdup(shaderSource.c_str());
143 char* uniform = strstr(shaderStr, UNIFORM);
147 char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
149 char* nextPtr = nullptr;
150 char* token = strtok_r(outerToken, DELIMITERS, &nextPtr);
153 if(SAMPLER_PREFIX == token)
155 token += SAMPLER_PREFIX.mLength;
156 if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
159 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
161 for(uint32_t i = 0; i < static_cast<uint32_t>(uniformOpaques.size()); ++i)
163 if(samplerPositions[i] == -1 &&
164 strncmp(token, uniformOpaques[i].name.c_str(), uniformOpaques[i].name.size()) == 0)
166 samplerPositions[i] = uniformOpaques[i].offset = samplerPosition++;
174 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token);
180 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
183 uniform = strstr(uniform, UNIFORM);
189 } // anonymous namespace
191 namespace Dali::Graphics::GLES
193 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
194 : Graphics::Reflection(),
195 mController(controller),
200 Reflection::~Reflection() = default;
202 void Reflection::BuildVertexAttributeReflection()
204 auto glProgram = mProgram.GetGlProgram();
206 int written, size, location, maxLength, nAttribs;
210 auto gl = mController.GetGL();
213 // Do nothing during shutdown
217 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
218 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
220 mVertexInputAttributes.clear();
221 mVertexInputAttributes.resize(nAttribs);
223 int maxLocationValue = nAttribs - 1;
225 name = new GLchar[maxLength];
226 for(int i = 0; i < nAttribs; i++)
228 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
229 location = gl->GetAttribLocation(glProgram, name);
233 if(maxLocationValue < location)
235 maxLocationValue = location;
236 mVertexInputAttributes.resize(maxLocationValue + 1u);
239 AttributeInfo attributeInfo;
240 attributeInfo.location = location;
241 attributeInfo.name = name;
242 attributeInfo.format = GetVertexAttributeTypeFormat(type);
243 mVertexInputAttributes[location] = std::move(attributeInfo);
250 void Reflection::BuildUniformReflection()
252 auto glProgram = mProgram.GetGlProgram();
259 auto gl = mController.GetGL();
262 // Do nothing during shutdown
266 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
267 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
269 mUniformBlocks.clear();
270 mDefaultUniformBlock.members.clear();
271 mUniformOpaques.clear();
273 name = new char[maxLen];
275 mStandaloneUniformExtraInfos.clear();
277 for(int i = 0; i < numUniforms; ++i)
282 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
283 int location = gl->GetUniformLocation(glProgram, name);
285 Dali::Graphics::UniformInfo uniformInfo;
287 uniformInfo.name = name;
290 auto iter = std::string(uniformInfo.name).find("[", 0);
291 if(iter != std::string::npos)
293 uniformInfo.name = std::string(name).substr(0, iter);
297 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
298 uniformInfo.location = location; //IsSampler(type) ? 0 : location;
299 uniformInfo.binding = 0; // IsSampler(type) ? location : 0;
300 uniformInfo.bufferIndex = 0;
301 uniformInfo.offset = 0;
305 mUniformOpaques.push_back(uniformInfo);
309 mDefaultUniformBlock.members.push_back(uniformInfo);
310 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
314 // Re-order according to uniform locations.
316 if(mDefaultUniformBlock.members.size() > 1)
318 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
319 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
322 if(mUniformOpaques.size() > 1)
327 // Calculate the uniform offset
328 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
332 mDefaultUniformBlock.members[i].offset = 0;
336 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
337 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
338 if(previousUniform != mStandaloneUniformExtraInfos.end())
340 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
341 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
346 if(mDefaultUniformBlock.members.size() > 0)
348 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
349 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
350 if(lastUniform != mStandaloneUniformExtraInfos.end())
352 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
353 mUniformBlocks.push_back(mDefaultUniformBlock);
358 mDefaultUniformBlock.size = 0;
364 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
365 void Reflection::BuildUniformBlockReflection()
367 auto gl = mController.GetGL();
368 auto glProgram = mProgram.GetGlProgram();
369 int numUniformBlocks = 0;
373 // Do nothing during shutdown
377 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
379 mUniformBlocks.clear();
380 mUniformBlocks.resize(numUniformBlocks);
382 int uniformBlockMaxLength = 0;
383 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
385 char* uniformBlockName = new char[uniformBlockMaxLength];
386 for(int i = 0; i < numUniformBlocks; i++)
391 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
392 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
393 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
395 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
396 uniformBlockInfo.name = uniformBlockName;
397 uniformBlockInfo.size = blockDataSize;
398 uniformBlockInfo.binding = blockBinding;
401 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
402 int* unifIndexes = new GLint[nUnis];
403 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
406 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
408 for(int unif = 0; unif < nUnis; ++unif)
410 int uniIndex = unifIndexes[unif];
414 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
415 int location = gl->GetUniformLocation(glProgram, uniformName);
417 Dali::Graphics::UniformInfo uniform;
418 uniform.name = uniformName;
419 uniform.location = location;
420 uniformBlockInfo.members.push_back(uniform);
423 delete[] unifIndexes;
425 mUniformBlocks.push_back(uniformBlockInfo);
427 delete[] uniformBlockName;
430 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
432 for(auto&& attr : mVertexInputAttributes)
434 if(attr.name == name)
436 return attr.location;
439 return ERROR_ATTRIBUTE_NOT_FOUND;
442 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
444 if(location >= mVertexInputAttributes.size())
446 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
449 return mVertexInputAttributes[location].format;
452 std::string Reflection::GetVertexAttributeName(uint32_t location) const
454 if(location >= mVertexInputAttributes.size())
456 return std::string();
459 return mVertexInputAttributes[location].name;
462 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
464 std::vector<uint32_t> locations;
465 for(auto&& attr : mVertexInputAttributes)
467 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
469 locations.push_back(attr.location);
476 uint32_t Reflection::GetUniformBlockCount() const
478 return mUniformBlocks.size();
481 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
483 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
486 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
488 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
491 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
493 if(index >= mUniformBlocks.size())
498 const auto& block = mUniformBlocks[index];
500 out.name = block.name;
501 out.binding = block.binding;
502 out.descriptorSet = block.descriptorSet;
503 auto membersSize = block.members.size();
504 out.members.resize(membersSize);
505 out.size = block.size;
506 for(auto i = 0u; i < out.members.size(); ++i)
508 const auto& memberUniform = block.members[i];
509 out.members[i].name = memberUniform.name;
510 out.members[i].binding = block.binding;
511 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
512 out.members[i].offset = memberUniform.offset;
513 out.members[i].location = memberUniform.location;
519 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
521 std::vector<uint32_t> retval{};
522 for(auto&& ubo : mUniformBlocks)
524 retval.emplace_back(ubo.binding);
529 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
531 if(blockIndex < mUniformBlocks.size())
533 return mUniformBlocks[blockIndex].name;
537 return std::string();
541 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
543 if(blockIndex < mUniformBlocks.size())
545 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
553 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
555 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
557 return mUniformBlocks[blockIndex].members[memberLocation].name;
561 return std::string();
565 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
567 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
569 return mUniformBlocks[blockIndex].members[memberLocation].offset;
577 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
580 for(auto&& ubo : mUniformBlocks)
582 for(auto&& member : ubo.members)
584 if(name == member.name || name == (ubo.name + "." + member.name))
587 out.location = member.location;
588 out.binding = ubo.binding;
589 out.bufferIndex = index;
590 out.offset = member.offset;
591 out.uniformClass = Graphics::UniformClass::UNIFORM;
600 for(auto&& uniform : mUniformOpaques)
602 if(uniform.name == name)
604 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
607 out.offset = index; // lexical location in shader
608 out.location = uniform.location; // uniform location mapping
617 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
619 std::vector<GLenum> retval{};
620 for(auto&& uniform : mStandaloneUniformExtraInfos)
622 retval.emplace_back(uniform.type);
628 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
630 return mStandaloneUniformExtraInfos;
633 const std::vector<Dali::Graphics::UniformInfo>& Reflection::GetSamplers() const
635 return mUniformOpaques;
638 Graphics::ShaderLanguage Reflection::GetLanguage() const
640 auto version = Graphics::ShaderLanguage::GLSL_3_2;
642 auto gl = mController.GetGL();
645 // Do nothing during shutdown
649 int majorVersion, minorVersion;
650 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
651 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
652 DALI_LOG_RELEASE_INFO("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
653 DALI_LOG_RELEASE_INFO("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
655 // TODO: the language version is hardcoded for now, but we may use what we get
656 // from GL_SHADING_LANGUAGE_VERSION?
660 void Reflection::SortOpaques()
662 //Determine declaration order of each sampler
663 auto& programCreateInfo = mProgram.GetCreateInfo();
665 std::vector<uint8_t> data;
666 std::string vertShader;
667 std::string fragShader;
669 for(auto& shaderState : *programCreateInfo.shaderState)
671 if(shaderState.pipelineStage == PipelineStage::VERTEX_SHADER)
673 vertShader = GetShaderSource(shaderState);
675 else if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
677 fragShader = GetShaderSource(shaderState);
681 int samplerPosition = 0;
682 std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
684 ParseShaderSamplers(vertShader, mUniformOpaques, samplerPosition, samplerPositions);
685 ParseShaderSamplers(fragShader, mUniformOpaques, samplerPosition, samplerPositions);
687 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
690 } // namespace Dali::Graphics::GLES