Merge branch 'devel/master' into devel/graphics
[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;
83 }
84
85 bool SortByLocation(Dali::Graphics::UniformInfo a, Dali::Graphics::UniformInfo b)
86 {
87   return a.location < b.location;
88 }
89
90 } // namespace
91
92 namespace Dali::Graphics::GLES
93 {
94 Reflection::Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller)
95 : Graphics::Reflection(),
96   mController(controller),
97   mProgram(program)
98 {
99 }
100
101 Reflection::~Reflection() = default;
102
103 void Reflection::BuildVertexAttributeReflection()
104 {
105   auto glProgram = mProgram.GetGlProgram();
106
107   int    written, size, location, maxLength, nAttribs;
108   GLenum type;
109   char*  name;
110
111   auto gl = mController.GetGL();
112
113   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
114   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
115
116   mVertexInputAttributes.clear();
117   mVertexInputAttributes.resize(nAttribs);
118
119   name = new GLchar[maxLength];
120   for(int i = 0; i < nAttribs; i++)
121   {
122     gl->GetActiveAttrib(glProgram, i, maxLength, &written, &size, &type, name);
123     location = gl->GetAttribLocation(glProgram, name);
124
125     if(location >= 0)
126     {
127       AttributeInfo attributeInfo;
128       attributeInfo.location = location;
129       attributeInfo.name     = name;
130       attributeInfo.format   = GetVertexAttributeTypeFormat(type);
131       mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
132     }
133   }
134   delete[] name;
135 }
136
137 void Reflection::BuildUniformReflection()
138 {
139   auto glProgram = mProgram.GetGlProgram();
140
141   int   maxLen;
142   char* name;
143
144   int numUniforms = 0;
145
146   auto gl = mController.GetGL();
147
148   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
149   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
150
151   mUniformBlocks.clear();
152   mDefaultUniformBlock.members.clear();
153   mUniformOpaques.clear();
154
155   name = new char[maxLen];
156
157   using UniformLocationSizePair = std::pair<uint32_t, uint32_t>;
158   std::vector<UniformLocationSizePair> uniformSizes;
159
160   uniformSizes.reserve(numUniforms);
161
162   for(int i = 0; i < numUniforms; ++i)
163   {
164     int    size;
165     GLenum type;
166     int    written;
167     gl->GetActiveUniform(glProgram, i, maxLen, &written, &size, &type, name);
168     int location = gl->GetUniformLocation(glProgram, name);
169     uniformSizes.push_back(std::make_pair(location, GetGLDataTypeSize(type)));
170
171     Dali::Graphics::UniformInfo uniformInfo;
172     uniformInfo.name         = name;
173     uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
174     uniformInfo.location     = IsSampler(type) ? 0 : location;
175     uniformInfo.binding      = IsSampler(type) ? location : 0;
176     uniformInfo.bufferIndex  = 0;
177
178     if(IsSampler(type))
179     {
180       mUniformOpaques.push_back(uniformInfo);
181     }
182     else
183     {
184       mDefaultUniformBlock.members.push_back(uniformInfo);
185     }
186   }
187
188   // Re-order according to uniform locations.
189   if(mDefaultUniformBlock.members.size() > 1)
190   {
191     std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortByLocation);
192   }
193
194   if(mUniformOpaques.size() > 1)
195   {
196     std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortByLocation);
197   }
198
199   // Calculate the uniform offset
200   for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
201   {
202     if(i == 0)
203     {
204       mDefaultUniformBlock.members[i].offset = 0;
205     }
206     else
207     {
208       uint32_t previousUniformLocation       = mDefaultUniformBlock.members[i - 1].location;
209       auto     previousUniform               = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&previousUniformLocation](const UniformLocationSizePair& iter) { return iter.first == previousUniformLocation; });
210       mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + previousUniform->second;
211     }
212   }
213
214   uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
215   auto     lastUniform         = std::find_if(uniformSizes.begin(), uniformSizes.end(), [&lastUniformLocation](const UniformLocationSizePair& iter) { return iter.first == lastUniformLocation; });
216   mDefaultUniformBlock.size    = mDefaultUniformBlock.members.back().offset + lastUniform->second;
217
218   mUniformBlocks.push_back(mDefaultUniformBlock);
219
220   delete[] name;
221 }
222
223 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
224 void Reflection::BuildUniformBlockReflection()
225 {
226   auto gl               = mController.GetGL();
227   auto glProgram        = mProgram.GetGlProgram();
228   int  numUniformBlocks = 0;
229   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
230
231   mUniformBlocks.clear();
232   mUniformBlocks.resize(numUniformBlocks);
233
234   int uniformBlockMaxLength = 0;
235   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
236
237   char* uniformBlockName = new char[uniformBlockMaxLength];
238   for(int i = 0; i < numUniformBlocks; i++)
239   {
240     int length;
241     int blockBinding;
242     int blockDataSize;
243     gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
244     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
245     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
246
247     Dali::Graphics::UniformBlockInfo uniformBlockInfo;
248     uniformBlockInfo.name    = uniformBlockName;
249     uniformBlockInfo.size    = blockDataSize;
250     uniformBlockInfo.binding = blockBinding;
251
252     int nUnis;
253     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
254     int* unifIndexes = new GLint[nUnis];
255     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
256     char* uniformName{};
257     int   maxUniLen;
258     gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
259
260     for(int unif = 0; unif < nUnis; ++unif)
261     {
262       int    uniIndex = unifIndexes[unif];
263       int    size;
264       GLenum type;
265
266       gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
267       int location = gl->GetUniformLocation(glProgram, uniformName);
268
269       Dali::Graphics::UniformInfo uniform;
270       uniform.name     = uniformName;
271       uniform.location = location;
272       uniformBlockInfo.members.push_back(uniform);
273     }
274
275     delete[] unifIndexes;
276
277     mUniformBlocks.push_back(uniformBlockInfo);
278   }
279   delete[] uniformBlockName;
280 }
281
282 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
283 {
284   for(auto&& attr : mVertexInputAttributes)
285   {
286     if(attr.name == name)
287     {
288       return attr.location;
289     }
290   }
291   return ERROR_ATTRIBUTE_NOT_FOUND;
292 }
293
294 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
295 {
296   if(location >= mVertexInputAttributes.size())
297   {
298     return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
299   }
300
301   return mVertexInputAttributes[location].format;
302 }
303
304 std::string Reflection::GetVertexAttributeName(uint32_t location) const
305 {
306   if(location >= mVertexInputAttributes.size())
307   {
308     return std::string();
309   }
310
311   return mVertexInputAttributes[location].name;
312 }
313
314 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
315 {
316   std::vector<uint32_t> locations;
317   for(auto&& attr : mVertexInputAttributes)
318   {
319     if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
320     {
321       locations.push_back(attr.location);
322     }
323   }
324
325   return locations;
326 }
327
328 uint32_t Reflection::GetUniformBlockCount() const
329 {
330   return mUniformBlocks.size();
331 }
332
333 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
334 {
335   return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
336 }
337
338 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
339 {
340   return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
341 }
342
343 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
344 {
345   if(index >= mUniformBlocks.size())
346   {
347     return false;
348   }
349
350   const auto& block = mUniformBlocks[index];
351
352   out.name          = block.name;
353   out.binding       = block.binding;
354   out.descriptorSet = block.descriptorSet;
355   auto membersSize  = block.members.size();
356   out.members.resize(membersSize);
357   out.size = block.size;
358   for(auto i = 0u; i < out.members.size(); ++i)
359   {
360     const auto& memberUniform   = block.members[i];
361     out.members[i].name         = memberUniform.name;
362     out.members[i].binding      = block.binding;
363     out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
364     out.members[i].offset       = memberUniform.offset;
365     out.members[i].location     = memberUniform.location;
366   }
367
368   return true;
369 }
370
371 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
372 {
373   std::vector<uint32_t> retval{};
374   for(auto&& ubo : mUniformBlocks)
375   {
376     retval.emplace_back(ubo.binding);
377   }
378   return retval;
379 }
380
381 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
382 {
383   if(blockIndex < mUniformBlocks.size())
384   {
385     return mUniformBlocks[blockIndex].name;
386   }
387   else
388   {
389     return std::string();
390   }
391 }
392
393 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
394 {
395   if(blockIndex < mUniformBlocks.size())
396   {
397     return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
398   }
399   else
400   {
401     return 0u;
402   }
403 }
404
405 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
406 {
407   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
408   {
409     return mUniformBlocks[blockIndex].members[memberLocation].name;
410   }
411   else
412   {
413     return std::string();
414   }
415 }
416
417 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
418 {
419   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
420   {
421     return mUniformBlocks[blockIndex].members[memberLocation].offset;
422   }
423   else
424   {
425     return 0u;
426   }
427 }
428
429 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
430 {
431   auto index = 0u;
432   for(auto&& ubo : mUniformBlocks)
433   {
434     for(auto&& member : ubo.members)
435     {
436       if(name == member.name || name == (ubo.name + "." + member.name))
437       {
438         out.name         = name;
439         out.location     = member.location;
440         out.binding      = ubo.binding;
441         out.bufferIndex  = index;
442         out.offset       = member.offset;
443         out.uniformClass = Graphics::UniformClass::UNIFORM;
444         return true;
445       }
446     }
447     index++;
448   }
449
450   // check samplers
451   for(auto&& uniform : mUniformOpaques)
452   {
453     if(uniform.name == name)
454     {
455       out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
456       out.binding      = uniform.binding;
457       out.name         = name;
458       out.offset       = 0;
459       out.location     = uniform.location;
460       return true;
461     }
462   }
463
464   return false;
465 }
466
467 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
468 {
469   return mUniformOpaques;
470 }
471
472 Graphics::ShaderLanguage Reflection::GetLanguage() const
473 {
474   auto gl = mController.GetGL();
475
476   int majorVersion, minorVersion;
477   gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
478   gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
479   printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
480   printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
481
482   // TODO: the language version is hardcoded for now, but we may use what we get
483   // from GL_SHADING_LANGUAGE_VERSION?
484   return Graphics::ShaderLanguage::GLSL_3_2;
485 }
486
487 } // namespace Dali::Graphics::GLES