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 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gGraphicsReflectionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_GRAPHICS_REFLECTION");
42 const char* const mString;
43 const uint32_t mLength;
45 template<uint32_t kLength>
46 constexpr StringSize(const char (&string)[kLength])
48 mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
52 operator const char*() const
58 bool operator==(const StringSize& lhs, const char* rhs)
60 return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
63 const char* const DELIMITERS = " \t\n";
64 constexpr StringSize UNIFORM{"uniform"};
65 constexpr StringSize SAMPLER_PREFIX{"sampler"};
66 constexpr StringSize SAMPLER_TYPES[] = {"2D", "Cube", "ExternalOES"};
67 constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
69 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
74 return Dali::Graphics::VertexInputAttributeFormat::FLOAT;
76 return Dali::Graphics::VertexInputAttributeFormat::VEC2;
78 return Dali::Graphics::VertexInputAttributeFormat::VEC3;
80 return Dali::Graphics::VertexInputAttributeFormat::VEC4;
82 return Dali::Graphics::VertexInputAttributeFormat::INTEGER;
84 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
88 uint32_t GetGLDataTypeSize(GLenum type)
90 // There are many more types than what are covered here, but
91 // they are not supported in dali.
94 case GL_FLOAT: // "float", 1 float, 4 bytes
96 case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
98 case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
100 case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
102 case GL_INT: // "int", 1 integer, 4 bytes
104 case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
106 case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
108 case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
115 bool IsSampler(GLenum type)
117 return type == GL_SAMPLER_2D || type == GL_SAMPLER_3D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
120 bool SortUniformInfoByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
122 return a.location < b.location;
125 bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExtraInfo a, Dali::Graphics::GLES::Reflection::UniformExtraInfo b)
127 return a.location < b.location;
130 std::string GetShaderSource(Dali::Graphics::ShaderState shaderState)
132 std::vector<uint8_t> data;
133 auto* shader = static_cast<const Dali::Graphics::GLES::Shader*>(shaderState.shader);
134 auto& shaderCreateInfo = shader->GetCreateInfo();
135 data.resize(shaderCreateInfo.sourceSize + 1);
136 std::memcpy(&data[0], shaderCreateInfo.sourceData, shaderCreateInfo.sourceSize);
137 data[shaderCreateInfo.sourceSize] = 0;
139 return std::string(reinterpret_cast<char*>(&data[0]));
142 void ParseShaderSamplers(std::string shaderSource, std::vector<Dali::Graphics::UniformInfo>& uniformOpaques, int& samplerPosition, std::vector<int>& samplerPositions)
144 if(!shaderSource.empty())
146 char* shaderStr = strdup(shaderSource.c_str());
147 char* uniform = strstr(shaderStr, UNIFORM);
151 char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
153 char* nextPtr = nullptr;
154 char* token = strtok_r(outerToken, DELIMITERS, &nextPtr);
157 if(SAMPLER_PREFIX == token)
159 token += SAMPLER_PREFIX.mLength;
160 if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
163 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
165 for(uint32_t i = 0; i < static_cast<uint32_t>(uniformOpaques.size()); ++i)
167 if(samplerPositions[i] == -1 &&
168 strncmp(token, uniformOpaques[i].name.c_str(), uniformOpaques[i].name.size()) == 0)
170 samplerPositions[i] = uniformOpaques[i].offset = samplerPosition++;
178 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Sampler uniform %s declared but not used in the shader\n", token);
184 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
187 uniform = strstr(uniform, UNIFORM);
193 } // anonymous namespace
195 namespace Dali::Graphics::GLES
197 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
198 : Graphics::Reflection(),
199 mController(controller),
204 Reflection::~Reflection() = default;
206 void Reflection::BuildVertexAttributeReflection()
208 auto glProgram = mProgram.GetGlProgram();
210 int written, size, location, maxLength, nAttribs;
214 auto gl = mController.GetGL();
217 // Do nothing during shutdown
221 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build vertex attribute reflection for glProgram : %u\n", glProgram);
223 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
224 gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
226 mVertexInputAttributes.clear();
227 mVertexInputAttributes.resize(nAttribs);
229 int maxLocationValue = nAttribs - 1;
231 name = new GLchar[maxLength];
232 for(int i = 0; i < nAttribs; i++)
234 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
235 location = gl->GetAttribLocation(glProgram, name);
239 if(maxLocationValue < location)
241 maxLocationValue = location;
242 mVertexInputAttributes.resize(maxLocationValue + 1u);
245 AttributeInfo attributeInfo;
246 attributeInfo.location = location;
247 attributeInfo.name = name;
248 attributeInfo.format = GetVertexAttributeTypeFormat(type);
249 mVertexInputAttributes[location] = std::move(attributeInfo);
256 void Reflection::BuildUniformReflection()
258 auto glProgram = mProgram.GetGlProgram();
265 auto gl = mController.GetGL();
268 // Do nothing during shutdown
272 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build uniform reflection for glProgram : %u\n", glProgram);
274 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
275 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
277 mUniformBlocks.clear();
278 mDefaultUniformBlock.members.clear();
279 mUniformOpaques.clear();
281 name = new char[maxLen];
283 mStandaloneUniformExtraInfos.clear();
285 for(int i = 0; i < numUniforms; ++i)
290 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
291 int location = gl->GetUniformLocation(glProgram, name);
293 Dali::Graphics::UniformInfo uniformInfo;
295 uniformInfo.name = name;
298 auto iter = std::string(uniformInfo.name).find("[", 0);
299 if(iter != std::string::npos)
301 uniformInfo.name = std::string(name).substr(0, iter);
305 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
306 uniformInfo.location = location; //IsSampler(type) ? 0 : location;
307 uniformInfo.binding = 0; // IsSampler(type) ? location : 0;
308 uniformInfo.bufferIndex = 0;
309 uniformInfo.offset = 0;
313 mUniformOpaques.push_back(uniformInfo);
317 mDefaultUniformBlock.members.push_back(uniformInfo);
318 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
322 // Re-order according to uniform locations.
324 if(mDefaultUniformBlock.members.size() > 1)
326 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
327 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
330 if(mUniformOpaques.size() > 1)
335 // Calculate the uniform offset
336 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
340 mDefaultUniformBlock.members[i].offset = 0;
344 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
345 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
346 if(previousUniform != mStandaloneUniformExtraInfos.end())
348 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
349 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
354 if(mDefaultUniformBlock.members.size() > 0)
356 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
357 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
358 if(lastUniform != mStandaloneUniformExtraInfos.end())
360 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
361 mUniformBlocks.push_back(mDefaultUniformBlock);
366 mDefaultUniformBlock.size = 0;
372 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
373 void Reflection::BuildUniformBlockReflection()
375 auto gl = mController.GetGL();
376 auto glProgram = mProgram.GetGlProgram();
377 int numUniformBlocks = 0;
381 // Do nothing during shutdown
385 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build uniform block reflection for glProgram : %u\n", glProgram);
387 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
389 mUniformBlocks.clear();
390 mUniformBlocks.resize(numUniformBlocks);
392 int uniformBlockMaxLength = 0;
393 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
395 char* uniformBlockName = new char[uniformBlockMaxLength];
396 for(int i = 0; i < numUniformBlocks; i++)
401 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
402 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
403 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
405 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
406 uniformBlockInfo.name = uniformBlockName;
407 uniformBlockInfo.size = blockDataSize;
408 uniformBlockInfo.binding = blockBinding;
411 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
412 int* unifIndexes = new GLint[nUnis];
413 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
416 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
418 for(int unif = 0; unif < nUnis; ++unif)
420 int uniIndex = unifIndexes[unif];
424 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
425 int location = gl->GetUniformLocation(glProgram, uniformName);
427 Dali::Graphics::UniformInfo uniform;
428 uniform.name = uniformName;
429 uniform.location = location;
430 uniformBlockInfo.members.push_back(uniform);
433 delete[] unifIndexes;
435 mUniformBlocks.push_back(uniformBlockInfo);
437 delete[] uniformBlockName;
440 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
442 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "name : %s\n", name.c_str());
443 for(auto&& attr : mVertexInputAttributes)
445 if(attr.name == name)
447 return attr.location;
450 return ERROR_ATTRIBUTE_NOT_FOUND;
453 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
455 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "location : %u\n", location);
456 if(location >= mVertexInputAttributes.size())
458 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
461 return mVertexInputAttributes[location].format;
464 std::string Reflection::GetVertexAttributeName(uint32_t location) const
466 DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "location : %u\n", location);
467 if(location >= mVertexInputAttributes.size())
469 return std::string();
472 return mVertexInputAttributes[location].name;
475 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
477 std::vector<uint32_t> locations;
478 for(auto&& attr : mVertexInputAttributes)
480 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
482 locations.push_back(attr.location);
489 uint32_t Reflection::GetUniformBlockCount() const
491 return mUniformBlocks.size();
494 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
496 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
499 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
501 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
504 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
506 if(index >= mUniformBlocks.size())
511 const auto& block = mUniformBlocks[index];
513 out.name = block.name;
514 out.binding = block.binding;
515 out.descriptorSet = block.descriptorSet;
516 auto membersSize = block.members.size();
517 out.members.resize(membersSize);
518 out.size = block.size;
519 for(auto i = 0u; i < out.members.size(); ++i)
521 const auto& memberUniform = block.members[i];
522 out.members[i].name = memberUniform.name;
523 out.members[i].binding = block.binding;
524 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
525 out.members[i].offset = memberUniform.offset;
526 out.members[i].location = memberUniform.location;
532 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
534 std::vector<uint32_t> retval{};
535 for(auto&& ubo : mUniformBlocks)
537 retval.emplace_back(ubo.binding);
542 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
544 if(blockIndex < mUniformBlocks.size())
546 return mUniformBlocks[blockIndex].name;
550 return std::string();
554 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
556 if(blockIndex < mUniformBlocks.size())
558 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
566 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
568 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
570 return mUniformBlocks[blockIndex].members[memberLocation].name;
574 return std::string();
578 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
580 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
582 return mUniformBlocks[blockIndex].members[memberLocation].offset;
590 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
593 for(auto&& ubo : mUniformBlocks)
595 for(auto&& member : ubo.members)
597 if(name == member.name || name == (ubo.name + "." + member.name))
600 out.location = member.location;
601 out.binding = ubo.binding;
602 out.bufferIndex = index;
603 out.offset = member.offset;
604 out.uniformClass = Graphics::UniformClass::UNIFORM;
613 for(auto&& uniform : mUniformOpaques)
615 if(uniform.name == name)
617 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
620 out.offset = index; // lexical location in shader
621 out.location = uniform.location; // uniform location mapping
630 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
632 std::vector<GLenum> retval{};
633 for(auto&& uniform : mStandaloneUniformExtraInfos)
635 retval.emplace_back(uniform.type);
641 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
643 return mStandaloneUniformExtraInfos;
646 const std::vector<Dali::Graphics::UniformInfo>& Reflection::GetSamplers() const
648 return mUniformOpaques;
651 Graphics::ShaderLanguage Reflection::GetLanguage() const
653 auto version = Graphics::ShaderLanguage::GLSL_3_2;
655 auto gl = mController.GetGL();
658 // Do nothing during shutdown
662 int majorVersion, minorVersion;
663 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
664 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
665 DALI_LOG_RELEASE_INFO("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
666 DALI_LOG_RELEASE_INFO("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
668 // TODO: the language version is hardcoded for now, but we may use what we get
669 // from GL_SHADING_LANGUAGE_VERSION?
673 void Reflection::SortOpaques()
675 //Determine declaration order of each sampler
676 auto& programCreateInfo = mProgram.GetCreateInfo();
678 std::vector<uint8_t> data;
679 std::string vertShader;
680 std::string fragShader;
682 for(auto& shaderState : *programCreateInfo.shaderState)
684 if(shaderState.pipelineStage == PipelineStage::VERTEX_SHADER)
686 vertShader = GetShaderSource(shaderState);
688 else if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
690 fragShader = GetShaderSource(shaderState);
694 int samplerPosition = 0;
695 std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
697 ParseShaderSamplers(vertShader, mUniformOpaques, samplerPosition, samplerPositions);
698 ParseShaderSamplers(fragShader, mUniformOpaques, samplerPosition, samplerPositions);
700 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
703 } // namespace Dali::Graphics::GLES