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