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 name = new GLchar[maxLength];
224 for(int i = 0; i < nAttribs; i++)
226 gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
227 location = gl->GetAttribLocation(glProgram, name);
231 AttributeInfo attributeInfo;
232 attributeInfo.location = location;
233 attributeInfo.name = name;
234 attributeInfo.format = GetVertexAttributeTypeFormat(type);
235 mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
241 void Reflection::BuildUniformReflection()
243 auto glProgram = mProgram.GetGlProgram();
250 auto gl = mController.GetGL();
253 // Do nothing during shutdown
257 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
258 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
260 mUniformBlocks.clear();
261 mDefaultUniformBlock.members.clear();
262 mUniformOpaques.clear();
264 name = new char[maxLen];
266 mStandaloneUniformExtraInfos.clear();
268 for(int i = 0; i < numUniforms; ++i)
273 gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
274 int location = gl->GetUniformLocation(glProgram, name);
276 Dali::Graphics::UniformInfo uniformInfo;
278 uniformInfo.name = name;
281 auto iter = std::string(uniformInfo.name).find("[", 0);
282 if(iter != std::string::npos)
284 uniformInfo.name = std::string(name).substr(0, iter);
288 uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
289 uniformInfo.location = location; //IsSampler(type) ? 0 : location;
290 uniformInfo.binding = 0; // IsSampler(type) ? location : 0;
291 uniformInfo.bufferIndex = 0;
292 uniformInfo.offset = 0;
296 mUniformOpaques.push_back(uniformInfo);
300 mDefaultUniformBlock.members.push_back(uniformInfo);
301 mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
305 // Re-order according to uniform locations.
307 if(mDefaultUniformBlock.members.size() > 1)
309 std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
310 std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
313 if(mUniformOpaques.size() > 1)
318 // Calculate the uniform offset
319 for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
323 mDefaultUniformBlock.members[i].offset = 0;
327 uint32_t previousUniformLocation = mDefaultUniformBlock.members[i - 1].location;
328 auto previousUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
329 if(previousUniform != mStandaloneUniformExtraInfos.end())
331 mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
332 mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
337 if(mDefaultUniformBlock.members.size() > 0)
339 uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
340 auto lastUniform = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
341 if(lastUniform != mStandaloneUniformExtraInfos.end())
343 mDefaultUniformBlock.size = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
344 mUniformBlocks.push_back(mDefaultUniformBlock);
349 mDefaultUniformBlock.size = 0;
355 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
356 void Reflection::BuildUniformBlockReflection()
358 auto gl = mController.GetGL();
359 auto glProgram = mProgram.GetGlProgram();
360 int numUniformBlocks = 0;
364 // Do nothing during shutdown
368 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
370 mUniformBlocks.clear();
371 mUniformBlocks.resize(numUniformBlocks);
373 int uniformBlockMaxLength = 0;
374 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
376 char* uniformBlockName = new char[uniformBlockMaxLength];
377 for(int i = 0; i < numUniformBlocks; i++)
382 gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
383 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
384 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
386 Dali::Graphics::UniformBlockInfo uniformBlockInfo;
387 uniformBlockInfo.name = uniformBlockName;
388 uniformBlockInfo.size = blockDataSize;
389 uniformBlockInfo.binding = blockBinding;
392 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
393 int* unifIndexes = new GLint[nUnis];
394 gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
397 gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
399 for(int unif = 0; unif < nUnis; ++unif)
401 int uniIndex = unifIndexes[unif];
405 gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
406 int location = gl->GetUniformLocation(glProgram, uniformName);
408 Dali::Graphics::UniformInfo uniform;
409 uniform.name = uniformName;
410 uniform.location = location;
411 uniformBlockInfo.members.push_back(uniform);
414 delete[] unifIndexes;
416 mUniformBlocks.push_back(uniformBlockInfo);
418 delete[] uniformBlockName;
421 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
423 for(auto&& attr : mVertexInputAttributes)
425 if(attr.name == name)
427 return attr.location;
430 return ERROR_ATTRIBUTE_NOT_FOUND;
433 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
435 if(location >= mVertexInputAttributes.size())
437 return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
440 return mVertexInputAttributes[location].format;
443 std::string Reflection::GetVertexAttributeName(uint32_t location) const
445 if(location >= mVertexInputAttributes.size())
447 return std::string();
450 return mVertexInputAttributes[location].name;
453 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
455 std::vector<uint32_t> locations;
456 for(auto&& attr : mVertexInputAttributes)
458 if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
460 locations.push_back(attr.location);
467 uint32_t Reflection::GetUniformBlockCount() const
469 return mUniformBlocks.size();
472 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
474 return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
477 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
479 return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
482 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
484 if(index >= mUniformBlocks.size())
489 const auto& block = mUniformBlocks[index];
491 out.name = block.name;
492 out.binding = block.binding;
493 out.descriptorSet = block.descriptorSet;
494 auto membersSize = block.members.size();
495 out.members.resize(membersSize);
496 out.size = block.size;
497 for(auto i = 0u; i < out.members.size(); ++i)
499 const auto& memberUniform = block.members[i];
500 out.members[i].name = memberUniform.name;
501 out.members[i].binding = block.binding;
502 out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
503 out.members[i].offset = memberUniform.offset;
504 out.members[i].location = memberUniform.location;
510 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
512 std::vector<uint32_t> retval{};
513 for(auto&& ubo : mUniformBlocks)
515 retval.emplace_back(ubo.binding);
520 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
522 if(blockIndex < mUniformBlocks.size())
524 return mUniformBlocks[blockIndex].name;
528 return std::string();
532 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
534 if(blockIndex < mUniformBlocks.size())
536 return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
544 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
546 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
548 return mUniformBlocks[blockIndex].members[memberLocation].name;
552 return std::string();
556 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
558 if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
560 return mUniformBlocks[blockIndex].members[memberLocation].offset;
568 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
571 for(auto&& ubo : mUniformBlocks)
573 for(auto&& member : ubo.members)
575 if(name == member.name || name == (ubo.name + "." + member.name))
578 out.location = member.location;
579 out.binding = ubo.binding;
580 out.bufferIndex = index;
581 out.offset = member.offset;
582 out.uniformClass = Graphics::UniformClass::UNIFORM;
591 for(auto&& uniform : mUniformOpaques)
593 if(uniform.name == name)
595 out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
598 out.offset = index; // lexical location in shader
599 out.location = uniform.location; // uniform location mapping
608 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
610 std::vector<GLenum> retval{};
611 for(auto&& uniform : mStandaloneUniformExtraInfos)
613 retval.emplace_back(uniform.type);
619 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
621 return mStandaloneUniformExtraInfos;
624 const std::vector<Dali::Graphics::UniformInfo>& Reflection::GetSamplers() const
626 return mUniformOpaques;
629 Graphics::ShaderLanguage Reflection::GetLanguage() const
631 auto version = Graphics::ShaderLanguage::GLSL_3_2;
633 auto gl = mController.GetGL();
636 // Do nothing during shutdown
640 int majorVersion, minorVersion;
641 gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
642 gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
643 DALI_LOG_RELEASE_INFO("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
644 DALI_LOG_RELEASE_INFO("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
646 // TODO: the language version is hardcoded for now, but we may use what we get
647 // from GL_SHADING_LANGUAGE_VERSION?
651 void Reflection::SortOpaques()
653 //Determine declaration order of each sampler
654 auto& programCreateInfo = mProgram.GetCreateInfo();
656 std::vector<uint8_t> data;
657 std::string vertShader;
658 std::string fragShader;
660 for(auto& shaderState : *programCreateInfo.shaderState)
662 if(shaderState.pipelineStage == PipelineStage::VERTEX_SHADER)
664 vertShader = GetShaderSource(shaderState);
666 else if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
668 fragShader = GetShaderSource(shaderState);
672 int samplerPosition = 0;
673 std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
675 ParseShaderSamplers(vertShader, mUniformOpaques, samplerPosition, samplerPositions);
676 ParseShaderSamplers(fragShader, mUniformOpaques, samplerPosition, samplerPositions);
678 std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
681 } // namespace Dali::Graphics::GLES