a0e2ba9367868f84031660049f088e39a0994585
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.h
1 #ifndef DALI_INTERNAL_PROGRAM_H
2 #define DALI_INTERNAL_PROGRAM_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 // EXTERNAL INCLUDES
22 #include <cstdint> // int32_t, uint32_t
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/common/const-string.h>
27 #include <dali/internal/common/shader-data.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/object/ref-object.h>
30
31 namespace Dali
32 {
33 namespace Graphics
34 {
35 class Controller;
36 class Program;
37 class Reflection;
38 } // namespace Graphics
39
40 class Matrix;
41
42 namespace Internal
43 {
44 class ProgramCache;
45
46 /**
47  * A program contains a vertex & fragment shader.
48  * It interfaces to the implementation program and it's reflection.
49  */
50 class Program
51 {
52 public:
53   /**
54    * Indices of default uniforms
55    */
56   enum class DefaultUniformIndex
57   {
58     MODEL_MATRIX = 0,
59     MVP_MATRIX,
60     VIEW_MATRIX,
61     MODEL_VIEW_MATRIX,
62     NORMAL_MATRIX,
63     PROJECTION_MATRIX,
64     SIZE,
65     COLOR,
66     ACTOR_COLOR,
67
68     COUNT
69   };
70
71   /**
72    * Creates a new program, or returns a copy of an existing program in the program cache
73    * @param[in] cache where the programs are stored
74    * @param[in] shaderData  A pointer to a data structure containing the program source
75    *                        and optionally precompiled binary. If the binary is empty the program bytecode
76    *                        is copied into it after compilation and linking)
77    * @param[in]  gfxController Reference to valid graphics Controller object
78    * @return pointer to the program
79    */
80  static Program* New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController);
81
82   /**
83    * Set the projection matrix that has currently been sent
84    * @param matrix to set
85    */
86   void SetProjectionMatrix(const Matrix* matrix)
87   {
88     mProjectionMatrix = matrix;
89   }
90
91   /**
92    * Get the projection matrix that has currently been sent
93    * @return the matrix that is set
94    */
95   const Matrix* GetProjectionMatrix()
96   {
97     return mProjectionMatrix;
98   }
99
100   /**
101    * Set the projection matrix that has currently been sent
102    * @param matrix to set
103    */
104   void SetViewMatrix(const Matrix* matrix)
105   {
106     mViewMatrix = matrix;
107   }
108
109   /**
110    * Get the projection matrix that has currently been sent
111    * @return the matrix that is set
112    */
113   const Matrix* GetViewMatrix()
114   {
115     return mViewMatrix;
116   }
117
118   [[nodiscard]] Graphics::Program& GetGraphicsProgram() const
119   {
120     return *mGfxProgram;
121   }
122
123   [[nodiscard]] Graphics::Program* GetGraphicsProgramPtr() const
124   {
125     return mGfxProgram.get();
126   }
127
128   void SetGraphicsProgram( Graphics::UniquePtr<Graphics::Program>&& program );
129
130   /**
131    * Retrieves uniform data.
132    * The lookup tries to minimise string comparisons. Ideally, when the hashedName is known
133    * and there are no hash collisions in the reflection it's the most optimal case.
134    *
135    * @param name Name of uniform
136    * @param hashedName Hash value from name or 0 if unknown
137    * @param out Reference to output structure
138    *
139    * @return False when uniform is not found or due to hash collision the result is ambiguous
140    */
141   bool GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const;
142
143   /**
144    * Retrieves default uniform
145    * @param[in] defaultUniformIndex index of the uniform
146    * @return Valid pointer to the UniformInfo object or nullptr
147    */
148   [[nodiscard]] const Graphics::UniformInfo* GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const;
149
150 private: // Implementation
151   /**
152    * Constructor, private so no direct instantiation
153    * @param[in] cache where the programs are stored
154    * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
155    * @param[in] gfxController Reference to Graphics Controller object
156    */
157   Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController);
158
159 public:
160   Program()               = delete;            ///< default constructor, not defined
161   Program(const Program&) = delete;            ///< copy constructor, not defined
162   Program& operator=(const Program&) = delete; ///< assignment operator, not defined
163
164   /**
165    * Destructor, non virtual as no virtual methods or inheritance
166    */
167   ~Program();
168
169 public:
170   /**
171    * Struct ReflectionUniformInfo
172    * Contains details of a single uniform buffer field and/or sampler.
173    */
174   struct ReflectionUniformInfo
175   {
176     size_t                hashValue{0};
177     bool                  hasCollision{false};
178     Graphics::UniformInfo uniformInfo{};
179   };
180
181   /**
182    * Build optimized shader reflection of uniforms
183    * @param graphicsReflection The graphics reflection
184    */
185   void BuildReflection(const Graphics::Reflection& graphicsReflection);
186
187   /**
188    * Struct UniformBlockMemoryRequirements
189    * Contains details of a uniform blocks memory requirements
190    */
191   struct UniformBlockMemoryRequirements
192   {
193     uint32_t blockCount;
194     uint32_t totalSizeRequired;
195   };
196
197   /**
198    * Retrieves uniform blocks requirements
199    *
200    * @return Reference to the valid UniformBlockMemoryRequirements struct
201    */
202   [[nodiscard]] const UniformBlockMemoryRequirements& GetUniformBlocksMemoryRequirements() const
203   {
204     return mUniformBlockRequirements;
205   }
206
207 private:                           // Data
208   ProgramCache& mCache;            ///< The program cache
209   const Matrix* mProjectionMatrix; ///< currently set projection matrix
210   const Matrix* mViewMatrix;       ///< currently set view matrix
211
212   Graphics::UniquePtr<Graphics::Program> mGfxProgram;    ///< Gfx program
213   Graphics::Controller&                  mGfxController; /// < Gfx controller
214   Internal::ShaderDataPtr                mProgramData;   ///< Shader program source and binary (when compiled & linked or loaded)
215
216   // uniform value caching
217   Vector3 mSizeUniformCache; ///< Cache value for size uniform
218
219   using UniformReflectionContainer = std::vector<ReflectionUniformInfo>;
220
221   UniformReflectionContainer mReflection{};                ///< Contains reflection build per program
222   UniformReflectionContainer mReflectionDefaultUniforms{}; ///< Contains default uniforms
223   UniformBlockMemoryRequirements mUniformBlockRequirements{}; ///< Memory requirements for uniform blocks
224 };
225
226 } // namespace Internal
227
228 } // namespace Dali
229
230 #endif // DALI_INTERNAL_PROGRAM_H