Reflection GetSamplers() returns const ref
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-reflection.h
1 #ifndef DALI_GRAPHICS_GLES_REFLECTION_H
2 #define DALI_GRAPHICS_GLES_REFLECTION_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/graphics-api/graphics-reflection.h>
22 #include <dali/graphics-api/graphics-types.h>
23 #include <dali/integration-api/gl-abstraction.h>
24
25 namespace Dali::Graphics
26 {
27 class EglGraphicsController;
28
29 namespace GLES
30 {
31 class ProgramImpl;
32 constexpr uint32_t ERROR_ATTRIBUTE_NOT_FOUND(-1u);
33
34 /**
35  * Reflection object represents a single full graphics reflection state.
36  *
37  * The state involves compiled and linked shaders as well as state parameters
38  * like blending, stencil, scissors, viewport etc.
39  *
40  * Some of the parameters can be modified by issuing commands but
41  * the Reflection must mark those states
42  * as dynamic.
43  *
44  */
45 class Reflection : public Dali::Graphics::Reflection
46 {
47 public:
48   explicit Reflection(GLES::ProgramImpl& program, Graphics::EglGraphicsController& controller);
49
50   ~Reflection() override;
51
52   // not copyable
53   Reflection(const Reflection&) = delete;
54   Reflection& operator=(const Reflection&) = delete;
55
56   /**
57    * @brief Gets the location of a vertex attribute.
58    *
59    * @param [in] name The name of vertex attribute
60    * @return The index of the vertex attribute in the shader
61    */
62   [[nodiscard]] uint32_t GetVertexAttributeLocation(const std::string& name) const override;
63
64   /**
65    * @brief Gets the format of a vertex attribute.
66    *
67    * @param [in] location The location of vertex attribute
68    * @return The format of a vertex attribute
69    */
70   [[nodiscard]] Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeFormat(uint32_t location) const override;
71
72   /**
73    * @brief Gets the name of a vertex attribute.
74    *
75    * @param [in] location The location of vertex attribute
76    * @return The name of the vertex attribute
77    */
78   [[nodiscard]] std::string GetVertexAttributeName(uint32_t location) const override;
79
80   /**
81    * @brief Gets the locations of all the vertex attribute in the shader.
82    *
83    * @return A vector of the locations of all the vertex attributes in the shader
84    */
85   [[nodiscard]] std::vector<uint32_t> GetVertexAttributeLocations() const override;
86
87   // Uniform blocks
88
89   /**
90    * @brief Gets the number of uniform blocks in the shader
91    *
92    * @return The number of uniform blocks
93    */
94   [[nodiscard]] uint32_t GetUniformBlockCount() const override;
95
96   /**
97    * @brief Gets the binding point to which the uniform block with the given index is binded.
98    *
99    * @param [in] index The index of the uniform block
100    * @return The binding point
101    */
102   [[nodiscard]] uint32_t GetUniformBlockBinding(uint32_t index) const override;
103
104   /**
105    * @brief Gets the size of the uniform block with the given index.
106    *
107    * @param [in] index The index of the uniform block
108    * @return The size of the uniform block
109    */
110   [[nodiscard]] uint32_t GetUniformBlockSize(uint32_t index) const override;
111
112   /**
113    * @brief Retrieves the information of the uniform block with the given index.
114    *
115    * The information includes the name, binding point, size, uniforms inside the uniform block, etc.
116    *
117    * @param [in] index The index of the uniform block
118    * @param [out] out A structure that contains the information of the uniform block
119    * @return Whether the uniform block exists or not
120    */
121   bool GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const override;
122
123   /**
124    * @brief Gets the binding points of all the uniform blocks in the shader.
125    *
126    * @return A vector of binding points
127    */
128   [[nodiscard]] std::vector<uint32_t> GetUniformBlockLocations() const override;
129
130   /**
131    * @brief Gets the name of uniform block with the given index.
132    *
133    * @param [in] blockIndex The index of the uniform block
134    * @return The name of the uniform block
135    */
136   [[nodiscard]] std::string GetUniformBlockName(uint32_t blockIndex) const override;
137
138   /**
139    * @brief Gets the number of uniforms in the uniform block with the given index.
140    *
141    * @param [in] blockIndex The index of the uniform block
142    * @return The number of uniforms in the uniform block
143    */
144   [[nodiscard]] uint32_t GetUniformBlockMemberCount(uint32_t blockIndex) const override;
145
146   /**
147    * @brief Gets the name of the uniform in the given location within the uniform block.
148    *
149    * @param [in] blockIndex The index of the uniform block
150    * @param [in] memberLocation The location of the uniform within the uniform block
151    * @return The name of the uniform
152    */
153   [[nodiscard]] std::string GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const override;
154
155   /**
156    * @brief Gets the byte offset of the uniform in the given location within the uniform block.
157    *
158    * @param [in] blockIndex The index of the uniform block
159    * @param [in] memberLocation The location of the uniform within the uniform block
160    * @return The byte offset of the uniform
161    */
162   [[nodiscard]] uint32_t GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const override;
163
164   // Named uniforms
165
166   /**
167    * @brief Gets the information of the uniform by its name.
168    *
169    * @param [in] name The name of the uniform
170    * @param [out] out The information of the uniform
171    * @return Whether the uniform exists or not
172    */
173   [[nodiscard]] bool GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const override;
174
175   /**
176    * @brief Gets the types of all the standalone uniforms within the default uniform block.
177    *
178    * @return A vector of uniform types sorted in the same order as the uniforms in the default uniform block.
179    */
180   [[nodiscard]] std::vector<GLenum> GetStandaloneUniformTypes() const;
181
182   // Sampler
183
184   /**
185    * @brief Gets all the sampler uniforms
186    *
187    * @return A vector of the sampler uniforms
188    */
189   const std::vector<Dali::Graphics::UniformInfo>& GetSamplers() const override;
190
191   // Language
192
193   /**
194    * @brief Retrieves the language of the shader
195    *
196    * @return The language of the shader
197    */
198   [[nodiscard]] Graphics::ShaderLanguage GetLanguage() const override;
199
200 public:
201   /**
202    * @brief Extra information of uniform
203    */
204   struct UniformExtraInfo
205   {
206     UniformExtraInfo(uint32_t location, uint32_t size, uint32_t offset, uint32_t arraySize, GLenum type)
207     : location(location),
208       size(size),
209       offset(offset),
210       arraySize(arraySize),
211       type(type){};
212
213     uint32_t location;  ///< Location of uniform
214     uint32_t size;      ///< size of uniform
215     uint32_t offset;    ///< offset of uniform within UBO
216     uint32_t arraySize; ///< number of array elements (1 for non-arrays)
217     GLenum   type;      ///< type of uniform
218   };
219
220   /**
221    * @brief Returns array of additional info about standalone uniforms
222    *
223    * @return Array of internal uniform data
224    */
225   [[nodiscard]] const std::vector<UniformExtraInfo>& GetStandaloneUniformExtraInfo() const;
226
227   /**
228    * @brief Build the reflection of vertex attributes
229    */
230   void BuildVertexAttributeReflection();
231
232   /**
233    * @brief Build the reflection of uniforms
234    */
235   void BuildUniformReflection();
236
237   /**
238    * @brief Build the reflection of uniform blocks
239    */
240   void BuildUniformBlockReflection();
241
242   /**
243    * Sort the samplers by their lexical location in the frag shader source code.
244    */
245   void SortOpaques();
246
247 protected:
248   Reflection(Reflection&&) = default;
249   Reflection& operator=(Reflection&&) = default;
250
251 private:
252   Graphics::EglGraphicsController& mController; ///< The Graphics controller
253   GLES::ProgramImpl&               mProgram;    ///< The Program object
254
255   struct AttributeInfo
256   {
257     uint32_t                                   location{};
258     std::string                                name{};
259     Dali::Graphics::VertexInputAttributeFormat format{};
260   };
261
262   std::vector<AttributeInfo>              mVertexInputAttributes;       ///< List of vertex attributes
263   Graphics::UniformBlockInfo              mDefaultUniformBlock{};       ///< The emulated UBO containing all the standalone uniforms
264   std::vector<Graphics::UniformInfo>      mUniformOpaques{};            ///< List of opaque uniforms (i.e. samplers)
265   std::vector<Graphics::UniformBlockInfo> mUniformBlocks{};             ///< List of uniform blocks
266   std::vector<UniformExtraInfo>           mStandaloneUniformExtraInfos; ///< List of extra information for standalone uniforms
267 };
268
269 } // namespace GLES
270 } // namespace Dali::Graphics
271
272 #endif // DALI_GRAPHICS_GLES_REFLECTION_H