Shader support
[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   // Sampler
176
177   /**
178    * @brief Gets all the sampler uniforms
179    *
180    * @return A vector of the sampler uniforms
181    */
182   [[nodiscard]] std::vector<Dali::Graphics::UniformInfo> GetSamplers() const override;
183
184   // Language
185
186   /**
187    * @brief Retrieves the language of the shader
188    *
189    * @return The language of the shader
190    */
191   [[nodiscard]] Graphics::ShaderLanguage GetLanguage() const override;
192
193 public:
194   /**
195    * @brief Build the reflection of vertex attributes
196    */
197   void BuildVertexAttributeReflection();
198
199   /**
200    * @brief Build the reflection of uniforms
201    */
202   void BuildUniformReflection();
203
204   /**
205    * @brief Build the reflection of uniform blocks
206    */
207   void BuildUniformBlockReflection();
208
209 protected:
210   Reflection(Reflection&&) = default;
211   Reflection& operator=(Reflection&&) = default;
212
213 private:
214   Graphics::EglGraphicsController& mController; ///< The Graphics controller
215   GLES::ProgramImpl&               mProgram;    ///< The Program object
216
217   struct AttributeInfo
218   {
219     uint32_t                                   location{};
220     std::string                                name{};
221     Dali::Graphics::VertexInputAttributeFormat format{};
222   };
223
224   std::vector<AttributeInfo>              mVertexInputAttributes; ///< List of vertex attributes
225   Graphics::UniformBlockInfo              mDefaultUniformBlock{}; ///< The emulated UBO containing all the standalone uniforms
226   std::vector<Graphics::UniformInfo>      mUniformOpaques{};      ///< List of opaque uniforms (i.e. samplers)
227   std::vector<Graphics::UniformBlockInfo> mUniformBlocks{};       ///< List of uniform blocks
228 };
229
230 } // namespace GLES
231 } // namespace Dali::Graphics
232
233 #endif // DALI_GRAPHICS_GLES_REFLECTION_H