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 maximumLocation = 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(maximumLocation < location)
235 maximumLocation = location;
236 // Increate continer size s.t. we can use maximumLocation as index.
237 mVertexInputAttributes.resize(maximumLocation + 1u);
240 AttributeInfo attributeInfo;
241 attributeInfo.location = location;
242 attributeInfo.name = name;
243 attributeInfo.format = GetVertexAttributeTypeFormat(type);
244 mVertexInputAttributes[location] = std::move(attributeInfo);
251 void Reflection::BuildUniformReflection()
253 auto glProgram = mProgram.GetGlProgram();
260 auto gl = mController.GetGL();
263 // Do nothing during shutdown
267 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
268 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
270 mUniformBlocks.clear();
271 mDefaultUniformBlock.members.clear();
272 mUniformOpaques.clear();
274 name = new char[maxLen];
276 mStandaloneUniformExtraInfos.clear();
278 for(int i = 0; i < numUniforms; ++i)
283 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
284 int location = gl->GetUniformLocation(glProgram, name);
286 Dali::Graphics::UniformInfo uniformInfo;
288 uniformInfo.name = name;
291 auto iter = std::string(uniformInfo.name).find("[", 0);
292 if(iter != std::string::npos)
294 uniformInfo.name = std::string(name).substr(0, iter);
298 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
299 uniformInfo.location = location; //IsSampler(type) ? 0 : location;
300 uniformInfo.binding = 0; // IsSampler(type) ? location : 0;
301 uniformInfo.bufferIndex = 0;
302 uniformInfo.offset = 0;
306 mUniformOpaques.push_back(uniformInfo);
310 mDefaultUniformBlock.members.push_back(uniformInfo);
311 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
315 // Re-order according to uniform locations.
317 if(mDefaultUniformBlock.members.size() > 1)
319 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
320 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
323 if(mUniformOpaques.size() > 1)
328 // Calculate the uniform offset
329 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
333 mDefaultUniformBlock.members[i].offset = 0;
337 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
338 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
339 if(previousUniform != mStandaloneUniformExtraInfos.end())
341 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
342 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
347 if(mDefaultUniformBlock.members.size() > 0)
349 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
350 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
351 if(lastUniform != mStandaloneUniformExtraInfos.end())
353 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
354 mUniformBlocks.push_back(mDefaultUniformBlock);
359 mDefaultUniformBlock.size = 0;
365 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
366 void Reflection::BuildUniformBlockReflection()
368 auto gl = mController.GetGL();
369 auto glProgram = mProgram.GetGlProgram();
370 int numUniformBlocks = 0;
374 // Do nothing during shutdown
378 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
380 mUniformBlocks.clear();
381 mUniformBlocks.resize(numUniformBlocks);
383 int uniformBlockMaxLength = 0;
384 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
386 char* uniformBlockName = new char[uniformBlockMaxLength];
387 for(int i = 0; i < numUniformBlocks; i++)
392 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
393 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
394 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
396 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
397 uniformBlockInfo.name = uniformBlockName;
398 uniformBlockInfo.size = blockDataSize;
399 uniformBlockInfo.binding = blockBinding;
402 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
403 int* unifIndexes = new GLint[nUnis];
404 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
407 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
409 for(int unif = 0; unif < nUnis; ++unif)
411 int uniIndex = unifIndexes[unif];
415 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
416 int location = gl->GetUniformLocation(glProgram, uniformName);
418 Dali::Graphics::UniformInfo uniform;
419 uniform.name = uniformName;
420 uniform.location = location;
421 uniformBlockInfo.members.push_back(uniform);
424 delete[] unifIndexes;
426 mUniformBlocks.push_back(uniformBlockInfo);
428 delete[] uniformBlockName;
431 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
433 for(auto&& attr : mVertexInputAttributes)
435 if(attr.name == name)
437 return attr.location;
440 return ERROR_ATTRIBUTE_NOT_FOUND;
443 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
445 if(location >= mVertexInputAttributes.size())
447 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
450 return mVertexInputAttributes[location].format;
453 std::string Reflection::GetVertexAttributeName(uint32_t location) const
455 if(location >= mVertexInputAttributes.size())
457 return std::string();
460 return mVertexInputAttributes[location].name;
463 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
465 std::vector<uint32_t> locations;
466 for(auto&& attr : mVertexInputAttributes)
468 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
470 locations.push_back(attr.location);
477 uint32_t Reflection::GetUniformBlockCount() const
479 return mUniformBlocks.size();
482 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
484 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
487 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
489 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
492 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
494 if(index >= mUniformBlocks.size())
499 const auto& block = mUniformBlocks[index];
501 out.name = block.name;
502 out.binding = block.binding;
503 out.descriptorSet = block.descriptorSet;
504 auto membersSize = block.members.size();
505 out.members.resize(membersSize);
506 out.size = block.size;
507 for(auto i = 0u; i < out.members.size(); ++i)
509 const auto& memberUniform = block.members[i];
510 out.members[i].name = memberUniform.name;
511 out.members[i].binding = block.binding;
512 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
513 out.members[i].offset = memberUniform.offset;
514 out.members[i].location = memberUniform.location;
520 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
522 std::vector<uint32_t> retval{};
523 for(auto&& ubo : mUniformBlocks)
525 retval.emplace_back(ubo.binding);
530 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
532 if(blockIndex < mUniformBlocks.size())
534 return mUniformBlocks[blockIndex].name;
538 return std::string();
542 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
544 if(blockIndex < mUniformBlocks.size())
546 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
554 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
556 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
558 return mUniformBlocks[blockIndex].members[memberLocation].name;
562 return std::string();
566 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
568 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
570 return mUniformBlocks[blockIndex].members[memberLocation].offset;
578 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
581 for(auto&& ubo : mUniformBlocks)
583 for(auto&& member : ubo.members)
585 if(name == member.name || name == (ubo.name + "." + member.name))
588 out.location = member.location;
589 out.binding = ubo.binding;
590 out.bufferIndex = index;
591 out.offset = member.offset;
592 out.uniformClass = Graphics::UniformClass::UNIFORM;
601 for(auto&& uniform : mUniformOpaques)
603 if(uniform.name == name)
605 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
608 out.offset = index; // lexical location in shader
609 out.location = uniform.location; // uniform location mapping
618 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
620 std::vector<GLenum> retval{};
621 for(auto&& uniform : mStandaloneUniformExtraInfos)
623 retval.emplace_back(uniform.type);
629 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
631 return mStandaloneUniformExtraInfos;
634 const std::vector<Dali::Graphics::UniformInfo>& Reflection::GetSamplers() const
636 return mUniformOpaques;
639 Graphics::ShaderLanguage Reflection::GetLanguage() const
641 auto version = Graphics::ShaderLanguage::GLSL_3_2;
643 auto gl = mController.GetGL();
646 // Do nothing during shutdown
650 int majorVersion, minorVersion;
651 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
652 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
653 DALI_LOG_RELEASE_INFO("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
654 DALI_LOG_RELEASE_INFO("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
656 // TODO: the language version is hardcoded for now, but we may use what we get
657 // from GL_SHADING_LANGUAGE_VERSION?
661 void Reflection::SortOpaques()
663 //Determine declaration order of each sampler
664 auto& programCreateInfo = mProgram.GetCreateInfo();
666 std::vector<uint8_t> data;
667 std::string vertShader;
668 std::string fragShader;
670 for(auto& shaderState : *programCreateInfo.shaderState)
672 if(shaderState.pipelineStage == PipelineStage::VERTEX_SHADER)
674 vertShader = GetShaderSource(shaderState);
676 else if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
678 fragShader = GetShaderSource(shaderState);
682 int samplerPosition = 0;
683 std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
685 ParseShaderSamplers(vertShader, mUniformOpaques, samplerPosition, samplerPositions);
686 ParseShaderSamplers(fragShader, mUniformOpaques, samplerPosition, samplerPositions);
688 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
691 } // namespace Dali::Graphics::GLES