1 #ifndef DALI_INTERNAL_PROGRAM_H
2 #define DALI_INTERNAL_PROGRAM_H
5 * Copyright (c) 2023 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
48 class UniformBufferManager;
52 * A program contains a vertex & fragment shader.
53 * It interfaces to the implementation program and it's reflection.
58 using Hash = std::size_t;
61 * Indices of default uniforms
63 enum class DefaultUniformIndex
80 * Creates a new program, or returns a copy of an existing program in the program cache
81 * @param[in] cache where the programs are stored
82 * @param[in] shaderData A pointer to a data structure containing the program source
83 * and optionally precompiled binary. If the binary is empty the program bytecode
84 * is copied into it after compilation and linking)
85 * @param[in] gfxController Reference to valid graphics Controller object
86 * @return pointer to the program
88 static Program* New(ProgramCache& cache, const Internal::ShaderDataPtr& shaderData, Graphics::Controller& gfxController);
90 Internal::ShaderDataPtr GetShaderData()
95 [[nodiscard]] Graphics::Program& GetGraphicsProgram() const
100 [[nodiscard]] Graphics::Program* GetGraphicsProgramPtr() const
102 return mGfxProgram.get();
106 * Setup the actual program, and ensure that it's reflection is generated.
108 void SetGraphicsProgram(Graphics::UniquePtr<Graphics::Program>&& program, Render::UniformBufferManager& uniformBufferManager);
111 * Retrieves uniform data.
112 * The lookup tries to minimise string comparisons. Ideally, when the hashedName is known
113 * and there are no hash collisions in the reflection it's the most optimal case.
115 * @param name Name of uniform
116 * @param hashedName Hash value from name or 0 if unknown
117 * @param hashedNameNoArray Hash value from name without array index & trailing string, or 0 if unknown
118 * @param out Reference to output structure
120 * @return False when uniform is not found or due to hash collision the result is ambiguous
122 bool GetUniform(const std::string_view& name, Hash hashedName, Hash hashedNameNoArray, Graphics::UniformInfo& out) const;
125 * Retrieves default uniform
126 * @param[in] defaultUniformIndex index of the uniform
127 * @return Valid pointer to the UniformInfo object or nullptr
129 [[nodiscard]] const Graphics::UniformInfo* GetDefaultUniform(DefaultUniformIndex defaultUniformIndex) const;
131 private: // Implementation
133 * Constructor, private so no direct instantiation
134 * @param[in] cache where the programs are stored
135 * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
136 * @param[in] gfxController Reference to Graphics Controller object
138 Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController);
141 Program() = delete; ///< default constructor, not defined
142 Program(const Program&) = delete; ///< copy constructor, not defined
143 Program& operator=(const Program&) = delete; ///< assignment operator, not defined
146 * Destructor, non virtual as no virtual methods or inheritance
152 * Struct ReflectionUniformInfo
153 * Contains details of a single uniform buffer field and/or sampler.
155 struct ReflectionUniformInfo
158 bool hasCollision{false};
159 Graphics::UniformInfo uniformInfo{};
163 * Build optimized shader reflection of uniforms
164 * @param graphicsReflection The graphics reflection
166 void BuildReflection(const Graphics::Reflection& graphicsReflection, Render::UniformBufferManager& uniformBufferManager);
169 * Struct UniformBlockMemoryRequirements
170 * Contains details of a uniform blocks memory requirements
172 struct UniformBlockMemoryRequirements
174 uint32_t blockCount{0u};
175 uint32_t totalSizeRequired{0u};
176 uint32_t totalCpuSizeRequired{0u}; ///< requirements for CPU memory
177 uint32_t totalGpuSizeRequired{0u}; ///< requirements of hardware buffer
180 std::vector<uint32_t> blockSize{};
181 std::vector<uint32_t> blockSizeAligned{};
184 * Retrieves uniform blocks requirements
186 * @return Reference to the valid UniformBlockMemoryRequirements struct
188 [[nodiscard]] const UniformBlockMemoryRequirements& GetUniformBlocksMemoryRequirements() const
190 return mUniformBlockMemoryRequirements;
194 ProgramCache& mCache; ///< The program cache
195 Graphics::UniquePtr<Graphics::Program> mGfxProgram; ///< Gfx program
196 Graphics::Controller& mGfxController; /// < Gfx controller
197 Internal::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
199 // uniform value caching
200 Vector3 mSizeUniformCache; ///< Cache value for size uniform
202 using UniformReflectionContainer = std::vector<ReflectionUniformInfo>;
204 UniformReflectionContainer mReflection{}; ///< Contains reflection build per program
205 UniformReflectionContainer mReflectionDefaultUniforms{}; ///< Contains default uniforms
207 UniformBlockMemoryRequirements mUniformBlockMemoryRequirements; ///< Memory requirements per each block, block 0 = standalone/emulated
210 } // namespace Internal
214 #endif // DALI_INTERNAL_PROGRAM_H