Merge "Add lock for image loading" into devel/master
[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    elementCount;
167     GLenum type;
168     int    written;
169     gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
170     int location = gl->GetUniformLocation(glProgram, name);
171
172     Dali::Graphics::UniformInfo uniformInfo;
173
174     uniformInfo.name = name;
175     if(elementCount > 1)
176     {
177       auto iter = std::string(uniformInfo.name).find("[", 0);
178       if(iter != std::string::npos)
179       {
180         uniformInfo.name = std::string(name).substr(0, iter);
181       }
182     }
183
184     uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
185     uniformInfo.location     = IsSampler(type) ? 0 : location;
186     uniformInfo.binding      = IsSampler(type) ? location : 0;
187     uniformInfo.bufferIndex  = 0;
188
189     if(IsSampler(type))
190     {
191       mUniformOpaques.push_back(uniformInfo);
192     }
193     else
194     {
195       mDefaultUniformBlock.members.push_back(uniformInfo);
196       mStandaloneUniformExtraInfos.emplace_back(location, GetGLDataTypeSize(type), uniformInfo.offset, elementCount, type);
197     }
198   }
199
200   // Re-order according to uniform locations.
201
202   if(mDefaultUniformBlock.members.size() > 1)
203   {
204     std::sort(mDefaultUniformBlock.members.begin(), mDefaultUniformBlock.members.end(), SortUniformInfoByLocation);
205     std::sort(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), SortUniformExtraInfoByLocation);
206   }
207
208   if(mUniformOpaques.size() > 1)
209   {
210     std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
211   }
212
213   // Calculate the uniform offset
214   for(unsigned int i = 0; i < mDefaultUniformBlock.members.size(); ++i)
215   {
216     if(i == 0)
217     {
218       mDefaultUniformBlock.members[i].offset = 0;
219     }
220     else
221     {
222       uint32_t previousUniformLocation       = mDefaultUniformBlock.members[i - 1].location;
223       auto     previousUniform               = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&previousUniformLocation](const UniformExtraInfo& iter) { return iter.location == previousUniformLocation; });
224       mDefaultUniformBlock.members[i].offset = mDefaultUniformBlock.members[i - 1].offset + (previousUniform->size * previousUniform->arraySize);
225       mStandaloneUniformExtraInfos[i].offset = mDefaultUniformBlock.members[i].offset;
226     }
227   }
228
229   if(mDefaultUniformBlock.members.size() > 0)
230   {
231   uint32_t lastUniformLocation = mDefaultUniformBlock.members.back().location;
232   auto     lastUniform         = std::find_if(mStandaloneUniformExtraInfos.begin(), mStandaloneUniformExtraInfos.end(), [&lastUniformLocation](const UniformExtraInfo& iter) { return iter.location == lastUniformLocation; });
233   mDefaultUniformBlock.size    = mDefaultUniformBlock.members.back().offset + (lastUniform->size * lastUniform->arraySize);
234   mUniformBlocks.push_back(mDefaultUniformBlock);
235   }
236   else
237   {
238     mDefaultUniformBlock.size = 0;
239   }
240
241   delete[] name;
242 }
243
244 // TODO: Maybe this is not needed if uniform block is not support by dali shaders?
245 void Reflection::BuildUniformBlockReflection()
246 {
247   auto gl               = mController.GetGL();
248   auto glProgram        = mProgram.GetGlProgram();
249   int  numUniformBlocks = 0;
250   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
251
252   mUniformBlocks.clear();
253   mUniformBlocks.resize(numUniformBlocks);
254
255   int uniformBlockMaxLength = 0;
256   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &uniformBlockMaxLength);
257
258   char* uniformBlockName = new char[uniformBlockMaxLength];
259   for(int i = 0; i < numUniformBlocks; i++)
260   {
261     int length;
262     int blockBinding;
263     int blockDataSize;
264     gl->GetActiveUniformBlockName(glProgram, i, uniformBlockMaxLength, &length, uniformBlockName);
265     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_BINDING, &blockBinding);
266     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &blockDataSize);
267
268     Dali::Graphics::UniformBlockInfo uniformBlockInfo;
269     uniformBlockInfo.name    = uniformBlockName;
270     uniformBlockInfo.size    = blockDataSize;
271     uniformBlockInfo.binding = blockBinding;
272
273     int nUnis;
274     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &nUnis);
275     int* unifIndexes = new GLint[nUnis];
276     gl->GetActiveUniformBlockiv(glProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, unifIndexes);
277     char* uniformName{};
278     int   maxUniLen;
279     gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniLen);
280
281     for(int unif = 0; unif < nUnis; ++unif)
282     {
283       int    uniIndex = unifIndexes[unif];
284       int    size;
285       GLenum type;
286
287       gl->GetActiveUniform(glProgram, uniIndex, maxUniLen, &length, &size, &type, uniformName);
288       int location = gl->GetUniformLocation(glProgram, uniformName);
289
290       Dali::Graphics::UniformInfo uniform;
291       uniform.name     = uniformName;
292       uniform.location = location;
293       uniformBlockInfo.members.push_back(uniform);
294     }
295
296     delete[] unifIndexes;
297
298     mUniformBlocks.push_back(uniformBlockInfo);
299   }
300   delete[] uniformBlockName;
301 }
302
303 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
304 {
305   for(auto&& attr : mVertexInputAttributes)
306   {
307     if(attr.name == name)
308     {
309       return attr.location;
310     }
311   }
312   return ERROR_ATTRIBUTE_NOT_FOUND;
313 }
314
315 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
316 {
317   if(location >= mVertexInputAttributes.size())
318   {
319     return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
320   }
321
322   return mVertexInputAttributes[location].format;
323 }
324
325 std::string Reflection::GetVertexAttributeName(uint32_t location) const
326 {
327   if(location >= mVertexInputAttributes.size())
328   {
329     return std::string();
330   }
331
332   return mVertexInputAttributes[location].name;
333 }
334
335 std::vector<uint32_t> Reflection::GetVertexAttributeLocations() const
336 {
337   std::vector<uint32_t> locations;
338   for(auto&& attr : mVertexInputAttributes)
339   {
340     if(attr.format != Dali::Graphics::VertexInputAttributeFormat::UNDEFINED)
341     {
342       locations.push_back(attr.location);
343     }
344   }
345
346   return locations;
347 }
348
349 uint32_t Reflection::GetUniformBlockCount() const
350 {
351   return mUniformBlocks.size();
352 }
353
354 uint32_t Reflection::GetUniformBlockBinding(uint32_t index) const
355 {
356   return index < mUniformBlocks.size() ? mUniformBlocks[index].binding : 0u;
357 }
358
359 uint32_t Reflection::GetUniformBlockSize(uint32_t index) const
360 {
361   return index < mUniformBlocks.size() ? mUniformBlocks[index].size : 0u;
362 }
363
364 bool Reflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
365 {
366   if(index >= mUniformBlocks.size())
367   {
368     return false;
369   }
370
371   const auto& block = mUniformBlocks[index];
372
373   out.name          = block.name;
374   out.binding       = block.binding;
375   out.descriptorSet = block.descriptorSet;
376   auto membersSize  = block.members.size();
377   out.members.resize(membersSize);
378   out.size = block.size;
379   for(auto i = 0u; i < out.members.size(); ++i)
380   {
381     const auto& memberUniform   = block.members[i];
382     out.members[i].name         = memberUniform.name;
383     out.members[i].binding      = block.binding;
384     out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
385     out.members[i].offset       = memberUniform.offset;
386     out.members[i].location     = memberUniform.location;
387   }
388
389   return true;
390 }
391
392 std::vector<uint32_t> Reflection::GetUniformBlockLocations() const
393 {
394   std::vector<uint32_t> retval{};
395   for(auto&& ubo : mUniformBlocks)
396   {
397     retval.emplace_back(ubo.binding);
398   }
399   return retval;
400 }
401
402 std::string Reflection::GetUniformBlockName(uint32_t blockIndex) const
403 {
404   if(blockIndex < mUniformBlocks.size())
405   {
406     return mUniformBlocks[blockIndex].name;
407   }
408   else
409   {
410     return std::string();
411   }
412 }
413
414 uint32_t Reflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
415 {
416   if(blockIndex < mUniformBlocks.size())
417   {
418     return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
419   }
420   else
421   {
422     return 0u;
423   }
424 }
425
426 std::string Reflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
427 {
428   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
429   {
430     return mUniformBlocks[blockIndex].members[memberLocation].name;
431   }
432   else
433   {
434     return std::string();
435   }
436 }
437
438 uint32_t Reflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
439 {
440   if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
441   {
442     return mUniformBlocks[blockIndex].members[memberLocation].offset;
443   }
444   else
445   {
446     return 0u;
447   }
448 }
449
450 bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
451 {
452   auto index = 0u;
453   for(auto&& ubo : mUniformBlocks)
454   {
455     for(auto&& member : ubo.members)
456     {
457       if(name == member.name || name == (ubo.name + "." + member.name))
458       {
459         out.name         = name;
460         out.location     = member.location;
461         out.binding      = ubo.binding;
462         out.bufferIndex  = index;
463         out.offset       = member.offset;
464         out.uniformClass = Graphics::UniformClass::UNIFORM;
465         return true;
466       }
467     }
468     index++;
469   }
470
471   // check samplers
472   for(auto&& uniform : mUniformOpaques)
473   {
474     if(uniform.name == name)
475     {
476       out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
477       out.binding      = uniform.binding;
478       out.name         = name;
479       out.offset       = 0;
480       out.location     = uniform.location;
481       return true;
482     }
483   }
484
485   return false;
486 }
487
488 std::vector<GLenum> Reflection::GetStandaloneUniformTypes() const
489 {
490   std::vector<GLenum> retval{};
491   for(auto&& uniform : mStandaloneUniformExtraInfos)
492   {
493     retval.emplace_back(uniform.type);
494   }
495
496   return retval;
497 }
498
499 const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUniformExtraInfo() const
500 {
501   return mStandaloneUniformExtraInfos;
502 }
503
504 std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
505 {
506   return mUniformOpaques;
507 }
508
509 Graphics::ShaderLanguage Reflection::GetLanguage() const
510 {
511   auto gl = mController.GetGL();
512
513   int majorVersion, minorVersion;
514   gl->GetIntegerv(GL_MAJOR_VERSION, &majorVersion);
515   gl->GetIntegerv(GL_MINOR_VERSION, &minorVersion);
516   printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
517   printf("GLSL Version : %s\n", gl->GetString(GL_SHADING_LANGUAGE_VERSION));
518
519   // TODO: the language version is hardcoded for now, but we may use what we get
520   // from GL_SHADING_LANGUAGE_VERSION?
521   return Graphics::ShaderLanguage::GLSL_3_2;
522 }
523
524 } // namespace Dali::Graphics::GLES