Add reflection API to retrieve uniform type for standalone uniforms
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-reflection.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "gles-graphics-reflection.h"
19 #include <dali/integration-api/gl-abstraction.h>
20 #include <dali/integration-api/gl-defines.h>
21
22 #include <vector>
23 #include "egl-graphics-controller.h"
24
25 #include <GLES3/gl3.h>
26 #include <GLES3/gl31.h>
27
28 #include "gles-graphics-program.h"
29
30 #include <iostream>
31
32 namespace
33 {
34 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
35 {
36   switch(type)
37   {
38     case GL_FLOAT:
39       return Dali::Graphics::VertexInputAttributeFormat::FLOAT;
40     case GL_FLOAT_VEC2:
41       return Dali::Graphics::VertexInputAttributeFormat::VEC2;
42     case GL_FLOAT_VEC3:
43       return Dali::Graphics::VertexInputAttributeFormat::VEC3;
44     case GL_FLOAT_VEC4:
45       return Dali::Graphics::VertexInputAttributeFormat::VEC4;
46     case GL_INT:
47       return Dali::Graphics::VertexInputAttributeFormat::INTEGER;
48     default:
49       return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
50   }
51 }
52
53 uint32_t GetGLDataTypeSize(GLenum type)
54 {
55   // There are many more types than what are covered here, but
56   // they are not supported in dali.
57   switch(type)
58   {
59     case GL_FLOAT: // "float", 1 float, 4 bytes
60       return 4;
61     case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
62       return 8;
63     case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
64       return 12;
65     case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
66       return 16;
67     case GL_INT: // "int", 1 integer, 4 bytes
68       return 4;
69     case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
70       return 16;
71     case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
72       return 36;
73     case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
74       return 64;
75     default:
76       return 0;
77   }
78 }
79
80 bool IsSampler(GLenum type)
81 {
82   return type == GL_SAMPLER_2D || type == GL_SAMPLER_3D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
83 }
84
85 bool SortUniformInfoByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
86 {
87   return a.location < b.location;
88 }
89
90 bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExtraInfo a, Dali::Graphics::GLES::Reflection::UniformExtraInfo b)
91 {
92   return a.location < b.location;
93 }
94
95 } // namespace
96
97 namespace Dali::Graphics::GLES
98 {
99 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
100 : Graphics::Reflection(),
101   mController(controller),
102   mProgram(program)
103 {
104 }
105
106 Reflection::~Reflection() = default;
107
108 void Reflection::BuildVertexAttributeReflection()
109 {
110   auto glProgram = mProgram.GetGlProgram();
111
112   int    written, size, location, maxLength, nAttribs;
113   GLenum type;
114   char*  name;
115
116   auto gl = mController.GetGL();
117
118   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
119   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
120
121   mVertexInputAttributes.clear();
122   mVertexInputAttributes.resize(nAttribs);
123
124   name = new GLchar[maxLength];
125   for(int i = 0; i < nAttribs; i++)
126   {
127     gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
128     location = gl->GetAttribLocation(glProgram, name);
129
130     if(location >= 0)
131     {
132       AttributeInfo attributeInfo;
133       attributeInfo.location = location;
134       attributeInfo.name     = name;
135       attributeInfo.format   = GetVertexAttributeTypeFormat(type);
136       mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
137     }
138   }
139   delete[] name;
140 }
141
142 void Reflection::BuildUniformReflection()
143 {
144   auto glProgram = mProgram.GetGlProgram();
145
146   int   maxLen;
147   char* name;
148
149   int numUniforms = 0;
150
151   auto gl = mController.GetGL();
152
153   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
154   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
155
156   mUniformBlocks.clear();
157   mDefaultUniformBlock.members.clear();
158   mUniformOpaques.clear();
159
160   name = new char[maxLen];
161
162   mStandaloneUniformExtraInfos.clear();
163
164   for(int i = 0; i < numUniforms; ++i)
165   {
166     int    size;
167     GLenum type;
168     int    written;
169     gl->GetActiveUniform(glProgram, i, maxLen, &written, &size, &type, name);
170     int location = gl->GetUniformLocation(glProgram, name);
171
172     Dali::Graphics::UniformInfo uniformInfo;
173     uniformInfo.name         = name;
174     uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
175     uniformInfo.location     = IsSampler(type) ? 0 : location;
176     uniformInfo.binding      = IsSampler(type) ? location : 0;
177     uniformInfo.bufferIndex  = 0;
178
179     if(IsSampler(type))
180     {
181       mUniformOpaques.push_back(uniformInfo);
182     }
183     else
184     {
185       mDefaultUniformBlock.members.push_back(uniformInfo);
186       mStandaloneUniformExtraInfos.push_back(UniformExtraInfo(location, GetGLDataTypeSize(type), type));
187     }
188   }
189
190   // Re-order according to uniform locations.
191   if(mDefaultUniformBlock.members.size() > 1)
192   {
193     std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
194   }
195
196   if(mUniformOpaques.size() > 1)
197   {
198     std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
199   }
200
201   // Calculate the uniform offset
202   for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
203   {
204     if(i == 0)
205     {
206       mDefaultUniformBlock.members[i].offset = 0;
207     }
208     else
209     {
210       uint32_t previousUniformLocation       = mDefaultUniformBlock.members[i - 1].location;
211       auto     previousUniform               = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
212       mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + previousUniform->size;
213     }
214   }
215
216   if(mDefaultUniformBlock.members.size() > 0)
217   {
218   uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
219   auto     lastUniform         = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
220   mDefaultUniformBlock.size    = mDefaultUniformBlock.members.back().offset + lastUniform->size;
221
222   mUniformBlocks.push_back(mDefaultUniformBlock);
223   }
224   else
225   {
226     mDefaultUniformBlock.size = 0;
227   }
228
229   // Re-order according to uniform locations.
230   if(mStandaloneUniformExtraInfos.size() > 1)
231   {
232     std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
233   }
234
235   delete[] name;
236 }
237
238 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
239 void Reflection::BuildUniformBlockReflection()
240 {
241   auto gl               = mController.GetGL();
242   auto glProgram        = mProgram.GetGlProgram();
243   int  numUniformBlocks = 0;
244   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
245
246   mUniformBlocks.clear();
247   mUniformBlocks.resize(numUniformBlocks);
248
249   int uniformBlockMaxLength = 0;
250   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
251
252   char* uniformBlockName = new char[uniformBlockMaxLength];
253   for(int i = 0; i < numUniformBlocks; i++)
254   {
255     int length;
256     int blockBinding;
257     int blockDataSize;
258     gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
259     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
260     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
261
262     Dali::Graphics::UniformBlockInfo uniformBlockInfo;
263     uniformBlockInfo.name    = uniformBlockName;
264     uniformBlockInfo.size    = blockDataSize;
265     uniformBlockInfo.binding = blockBinding;
266
267     int nUnis;
268     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
269     int* unifIndexes = new GLint[nUnis];
270     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
271     char* uniformName{};
272     int   maxUniLen;
273     gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
274
275     for(int unif = 0; unif < nUnis; ++unif)
276     {
277       int    uniIndex = unifIndexes[unif];
278       int    size;
279       GLenum type;
280
281       gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
282       int location = gl->GetUniformLocation(glProgram, uniformName);
283
284       Dali::Graphics::UniformInfo uniform;
285       uniform.name     = uniformName;
286       uniform.location = location;
287       uniformBlockInfo.members.push_back(uniform);
288     }
289
290     delete[] unifIndexes;
291
292     mUniformBlocks.push_back(uniformBlockInfo);
293   }
294   delete[] uniformBlockName;
295 }
296
297 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
298 {
299   for(auto&& attr : mVertexInputAttributes)
300   {
301     if(attr.name == name)
302     {
303       return attr.location;
304     }
305   }
306   return ERROR_ATTRIBUTE_NOT_FOUND;
307 }
308
309 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
310 {
311   if(location >= mVertexInputAttributes.size())
312   {
313     return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
314   }
315
316   return mVertexInputAttributes[location].format;
317 }
318
319 std::string Reflection::GetVertexAttributeName(uint32_t location) const
320 {
321   if(location >= mVertexInputAttributes.size())
322   {
323     return std::string();
324   }
325
326   return mVertexInputAttributes[location].name;
327 }
328
329 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
330 {
331   std::vector<uint32_t> locations;
332   for(auto&& attr : mVertexInputAttributes)
333   {
334     if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
335     {
336       locations.push_back(attr.location);
337     }
338   }
339
340   return locations;
341 }
342
343 uint32_t Reflection::GetUniformBlockCount() const
344 {
345   return mUniformBlocks.size();
346 }
347
348 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
349 {
350   return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
351 }
352
353 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
354 {
355   return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
356 }
357
358 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
359 {
360   if(index >= mUniformBlocks.size())
361   {
362     return false;
363   }
364
365   const auto& block = mUniformBlocks[index];
366
367   out.name          = block.name;
368   out.binding       = block.binding;
369   out.descriptorSet = block.descriptorSet;
370   auto membersSize  = block.members.size();
371   out.members.resize(membersSize);
372   out.size = block.size;
373   for(auto i = 0u; i < out.members.size(); ++i)
374   {
375     const auto& memberUniform   = block.members[i];
376     out.members[i].name         = memberUniform.name;
377     out.members[i].binding      = block.binding;
378     out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
379     out.members[i].offset       = memberUniform.offset;
380     out.members[i].location     = memberUniform.location;
381   }
382
383   return true;
384 }
385
386 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
387 {
388   std::vector<uint32_t> retval{};
389   for(auto&& ubo : mUniformBlocks)
390   {
391     retval.emplace_back(ubo.binding);
392   }
393   return retval;
394 }
395
396 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
397 {
398   if(blockIndex < mUniformBlocks.size())
399   {
400     return mUniformBlocks[blockIndex].name;
401   }
402   else
403   {
404     return std::string();
405   }
406 }
407
408 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
409 {
410   if(blockIndex < mUniformBlocks.size())
411   {
412     return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
413   }
414   else
415   {
416     return 0u;
417   }
418 }
419
420 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
421 {
422   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
423   {
424     return mUniformBlocks[blockIndex].members[memberLocation].name;
425   }
426   else
427   {
428     return std::string();
429   }
430 }
431
432 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
433 {
434   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
435   {
436     return mUniformBlocks[blockIndex].members[memberLocation].offset;
437   }
438   else
439   {
440     return 0u;
441   }
442 }
443
444 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
445 {
446   auto index = 0u;
447   for(auto&& ubo : mUniformBlocks)
448   {
449     for(auto&& member : ubo.members)
450     {
451       if(name == member.name || name == (ubo.name + "." + member.name))
452       {
453         out.name         = name;
454         out.location     = member.location;
455         out.binding      = ubo.binding;
456         out.bufferIndex  = index;
457         out.offset       = member.offset;
458         out.uniformClass = Graphics::UniformClass::UNIFORM;
459         return true;
460       }
461     }
462     index++;
463   }
464
465   // check samplers
466   for(auto&& uniform : mUniformOpaques)
467   {
468     if(uniform.name == name)
469     {
470       out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
471       out.binding      = uniform.binding;
472       out.name         = name;
473       out.offset       = 0;
474       out.location     = uniform.location;
475       return true;
476     }
477   }
478
479   return false;
480 }
481
482 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
483 {
484   std::vector<GLenum> retval{};
485   for(auto&& uniform : mStandaloneUniformExtraInfos)
486   {
487     retval.emplace_back(uniform.type);
488   }
489   return retval;
490 }
491
492 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
493 {
494   return mUniformOpaques;
495 }
496
497 Graphics::ShaderLanguage Reflection::GetLanguage() const
498 {
499   auto gl = mController.GetGL();
500
501   int majorVersion, minorVersion;
502   gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
503   gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
504   printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
505   printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
506
507   // TODO: the language version is hardcoded for now, but we may use what we get
508   // from GL_SHADING_LANGUAGE_VERSION?
509   return Graphics::ShaderLanguage::GLSL_3_2;
510 }
511
512 } // namespace Dali::Graphics::GLES