1 #ifndef DALI_INTERNAL_SHADER_DATA_H
2 #define DALI_INTERNAL_SHADER_DATA_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.
25 #include <dali/graphics-api/graphics-types.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/public-api/rendering/shader.h> // ShaderHints
35 using ShaderDataPtr = IntrusivePtr<ShaderData>;
39 static const std::vector<char> emptyShader;
41 inline std::vector<char> StringToVector(const std::string& str)
43 auto retval = std::vector<char>{};
44 retval.insert(retval.begin(), str.begin(), str.end());
45 retval.push_back('\0');
53 * A container for shader source code and compiled binary byte code.
55 class ShaderData : public Dali::RefObject
60 * @param[in] vertexSource Source code for vertex program
61 * @param[in] fragmentSource Source code for fragment program
62 * @param[in] hints Hints for rendering
64 ShaderData(std::string vertexSource, std::string fragmentSource, const Dali::Shader::Hint::Value hints)
66 mVertexShader(StringToVector(vertexSource)),
67 mFragmentShader(StringToVector(fragmentSource)),
69 mSourceMode(Graphics::ShaderSourceMode::TEXT)
74 * Creates a shader data containing binary content
75 * @param[in] vertexSource Source code for vertex program
76 * @param[in] fragmentSource Source code for fragment program
77 * @param[in] hints Hints for rendering
79 ShaderData(std::vector<char>& vertexSource, std::vector<char>& fragmentSource, const Dali::Shader::Hint::Value hints)
81 mVertexShader(vertexSource),
82 mFragmentShader(fragmentSource),
84 mSourceMode(Graphics::ShaderSourceMode::BINARY)
90 * Protected Destructor
91 * A reference counted object may only be deleted by calling Unreference()
93 ~ShaderData() override
95 // vector releases its data
100 * Set hash value which is created with vertex and fragment shader code
101 * @param [in] shaderHash hash key created with vertex and fragment shader code
103 void SetHashValue(size_t shaderHash)
105 DALI_ASSERT_DEBUG(shaderHash != size_t(-1));
106 mShaderHash = shaderHash;
110 * Get hash value which is created with vertex and fragment shader code
111 * @return shaderHash hash key created with vertex and fragment shader code
113 size_t GetHashValue() const
115 DALI_ASSERT_DEBUG(mShaderHash != size_t(-1));
120 * @return the vertex shader
122 const char* GetVertexShader() const
124 return &mVertexShader[0];
128 * @return the vertex shader
130 const char* GetFragmentShader() const
132 return &mFragmentShader[0];
136 * Returns a std::vector containing the shader code associated with particular pipeline stage
137 * @param[in] the graphics pipeline stage
138 * @return the shader code
140 const std::vector<char>& GetShaderForPipelineStage(Graphics::PipelineStage stage) const
142 if(stage == Graphics::PipelineStage::VERTEX_SHADER)
144 return mVertexShader;
146 else if(stage == Graphics::PipelineStage::FRAGMENT_SHADER)
148 return mFragmentShader;
152 // DALI_LOG_ERROR("Unsupported shader stage\n");
160 Dali::Shader::Hint::Value GetHints() const
165 * Check whether there is a compiled binary available
166 * @return true if this objects contains a compiled binary
168 bool HasBinary() const
170 return 0 != mBuffer.Size();
174 * Allocate a buffer for the compiled binary bytecode
175 * @param[in] size The size of the buffer in bytes
177 void AllocateBuffer(std::size_t size)
179 mBuffer.Resize(size);
183 * Get the program buffer
184 * @return reference to the buffer
186 std::size_t GetBufferSize() const
188 return mBuffer.Size();
192 * Get the data that the buffer points to
193 * @return raw pointer to the buffer data
195 uint8_t* GetBufferData()
197 DALI_ASSERT_DEBUG(mBuffer.Size() > 0);
202 * Get the data that the buffer points to
203 * @return raw pointer to the buffer data
205 Dali::Vector<uint8_t>& GetBuffer()
211 * Get the source mode of shader data
212 * @return the source mode of shader data ( text or binary )
214 Graphics::ShaderSourceMode GetSourceMode() const
219 private: // Not implemented
220 ShaderData(const ShaderData& other); ///< no copying of this object
221 ShaderData& operator=(const ShaderData& rhs); ///< no copying of this object
224 std::size_t mShaderHash; ///< hash key created with vertex and fragment shader code
225 std::vector<char> mVertexShader; ///< source code for vertex program
226 std::vector<char> mFragmentShader; ///< source code for fragment program
227 Dali::Shader::Hint::Value mHints; ///< take a hint
228 Dali::Vector<uint8_t> mBuffer; ///< buffer containing compiled binary bytecode
229 Graphics::ShaderSourceMode mSourceMode; ///< Source mode of shader data ( text or binary )
232 } // namespace Internal
236 #endif // DALI_INTERNAL_SHADER_DATA_H