1 #ifndef DALI_INTERNAL_PROGRAM_H
2 #define DALI_INTERNAL_PROGRAM_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cstdint> // int32_t, uint32_t
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>
38 } // namespace Graphics
47 * A program contains a vertex & fragment shader.
48 * It interfaces to the implementation program and it's reflection.
54 * Indices of default uniforms
56 enum class DefaultUniformIndex
71 * Creates a new program, or returns a copy of an existing program in the program cache
72 * @param[in] cache where the programs are stored
73 * @param[in] shaderData A pointer to a data structure containing the program source
74 * and optionally precompiled binary. If the binary is empty the program bytecode
75 * is copied into it after compilation and linking)
76 * @param[in] gfxController Reference to valid graphics Controller object
77 * @return pointer to the program
79 static Program* New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController);
82 * Set the projection matrix that has currently been sent
83 * @param matrix to set
85 void SetProjectionMatrix(const Matrix* matrix)
87 mProjectionMatrix = matrix;
91 * Get the projection matrix that has currently been sent
92 * @return the matrix that is set
94 const Matrix* GetProjectionMatrix()
96 return mProjectionMatrix;
100 * Set the projection matrix that has currently been sent
101 * @param matrix to set
103 void SetViewMatrix(const Matrix* matrix)
105 mViewMatrix = matrix;
109 * Get the projection matrix that has currently been sent
110 * @return the matrix that is set
112 const Matrix* GetViewMatrix()
117 [[nodiscard]] Graphics::Program& GetGraphicsProgram() const
122 [[nodiscard]] Graphics::Program* GetGraphicsProgramPtr() const
124 return mGfxProgram.get();
127 void SetGraphicsProgram( Graphics::UniquePtr<Graphics::Program>&& program );
130 * Retrieves uniform data.
131 * The lookup tries to minimise string comparisons. Ideally, when the hashedName is known
132 * and there are no hash collisions in the reflection it's the most optimal case.
134 * @param name Name of uniform
135 * @param hashedName Hash value from name or 0 if unknown
136 * @param out Reference to output structure
138 * @return False when uniform is not found or due to hash collision the result is ambiguous
140 bool GetUniform(const std::string& name, size_t hashedName, Graphics::UniformInfo& out) const;
143 * Retrieves default uniform
144 * @param[in] defaultUniformIndex index of the uniform
145 * @return Valid pointer to the UniformInfo object or nullptr
147 [[nodiscard]] const Graphics::UniformInfo* GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const;
149 private: // Implementation
151 * Constructor, private so no direct instantiation
152 * @param[in] cache where the programs are stored
153 * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
154 * @param[in] gfxController Reference to Graphics Controller object
156 Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController);
159 Program() = delete; ///< default constructor, not defined
160 Program(const Program&) = delete; ///< copy constructor, not defined
161 Program& operator=(const Program&) = delete; ///< assignment operator, not defined
164 * Destructor, non virtual as no virtual methods or inheritance
170 * Struct ReflectionUniformInfo
171 * Contains details of a single uniform buffer field and/or sampler.
173 struct ReflectionUniformInfo
176 bool hasCollision{false};
177 Graphics::UniformInfo uniformInfo{};
181 * Build optimized shader reflection of uniforms
182 * @param graphicsReflection The graphics reflection
184 void BuildReflection(const Graphics::Reflection& graphicsReflection);
187 * Struct UniformBlockMemoryRequirements
188 * Contains details of a uniform blocks memory requirements
190 struct UniformBlockMemoryRequirements
193 uint32_t totalSizeRequired;
197 * Retrieves uniform blocks requirements
199 * @return Reference to the valid UniformBlockMemoryRequirements struct
201 [[nodiscard]] const UniformBlockMemoryRequirements& GetUniformBlocksMemoryRequirements() const
203 return mUniformBlockRequirements;
207 ProgramCache& mCache; ///< The program cache
208 const Matrix* mProjectionMatrix; ///< currently set projection matrix
209 const Matrix* mViewMatrix; ///< currently set view matrix
211 Graphics::UniquePtr<Graphics::Program> mGfxProgram; ///< Gfx program
212 Graphics::Controller& mGfxController; /// < Gfx controller
213 Internal::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
215 // uniform value caching
216 Vector3 mSizeUniformCache; ///< Cache value for size uniform
218 using UniformReflectionContainer = std::vector<ReflectionUniformInfo>;
220 UniformReflectionContainer mReflection{}; ///< Contains reflection build per program
221 UniformReflectionContainer mReflectionDefaultUniforms{}; ///< Contains default uniforms
222 UniformBlockMemoryRequirements mUniformBlockRequirements{}; ///< Memory requirements for uniform blocks
225 } // namespace Internal
229 #endif // DALI_INTERNAL_PROGRAM_H