1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
5 * Copyright (c) 2014-2015 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 <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
25 #include <dali/integration-api/shader-data.h>
27 #include <dali/internal/common/buffer-index.h>
28 #include <dali/internal/common/type-abstraction-enums.h>
30 #include <dali/internal/event/common/event-thread-services.h>
31 #include <dali/internal/event/effects/shader-declarations.h>
33 #include <dali/internal/update/common/property-owner.h>
35 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
36 #include <dali/internal/render/gl-resources/texture-declarations.h>
37 #include <dali/internal/render/common/render-manager.h>
45 typedef unsigned int ResourceId;
46 } // namespace Integration
51 class ProgramController;
62 * A base class for a collection of shader programs, to apply an effect to different geometry types.
63 * This class is also the default shader so its easier to override default behaviour
65 class Shader : public PropertyOwner
71 * @param hints Geometry hints
73 Shader( Dali::ShaderEffect::GeometryHints& hints );
81 * Second stage initialization, called when added to the UpdateManager
82 * @param renderQueue Used to queue messages from update to render thread.
83 * @param textureCache Used to retrieve effect textures when rendering.
85 void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
87 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88 // The following methods are called during UpdateManager::Update()
89 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92 * Query whether a shader geometry hint is set.
93 * @pre The shader has been initialized.
94 * @param[in] hint The geometry hint to check.
95 * @return True if the given geometry hint is set.
97 bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
99 return mGeometryHints & hint;
103 * Retrieve the set of geometry hints.
106 Dali::ShaderEffect::GeometryHints GetGeometryHints() const
108 return mGeometryHints;
112 * Set the geometry hints.
113 * @param[in] hints The hints.
115 void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
117 mGeometryHints = hints;
121 * @return True if the fragment shader outputs only 1.0 on the alpha channel
123 * @note Shaders that can output any value on the alpha channel
124 * including 1.0 should return false for this.
126 bool IsOutputOpaque();
129 * @return True if the fragment shader can output any value but 1.0 on the alpha channel
131 * @note Shaders that can output any value on the alpha channel
132 * including 1.0 should return false for this
134 bool IsOutputTransparent();
137 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
139 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
141 // no default properties
145 * Set the ID used to access textures
146 * @pre This method is not thread-safe, and should only be called from the update-thread.
147 * @param[in] updateBufferIndex The current update buffer index.
148 * @param[in] textureId The texture ID.
150 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
153 * Gets the effect texture resource ID
154 * This is zero if there is effect texture
155 * @return the resource Id
157 Integration::ResourceId GetEffectTextureResourceId();
160 * Forwards the meta data from the update thread to the render thread for actual
161 * installation. (Installation is to a std::vector, which is not thread safe)
162 * @sa InstallUniformMetaInRender
163 * @pre This method should only be called from the update thread.
164 * @param[in] updateBufferIndex The current update buffer index.
165 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
167 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
170 * Forwards coordinate type to render
171 * @sa InstallUniformMetaInRender
172 * @pre This method should only be called from the update thread.
173 * @param[in] updateBufferIndex The current update buffer index.
174 * @param[in] index of the metadata.
175 * @param[in] type the coordinate type.
177 void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
180 * Forwards the grid density.
181 * @pre This method is not thread-safe, and should only be called from the update thread.
182 * @param[in] updateBufferIndex The current update buffer index.
183 * @param[in] density The grid density.
185 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
189 * @pre This method is not thread-safe, and should only be called from the update thread.
190 * @param[in] updateBufferIndex The current update buffer index.
191 * @param[in] hint The geometry hints.
193 void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
195 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196 // The following methods are called in Render thread
197 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
200 * Set the ID used to access textures
201 * @pre This method is not thread-safe, and should only be called from the render thread.
202 * @param[in] textureId The texture ID.
204 void SetTextureId( Integration::ResourceId textureId );
207 * Get the texture id, that will be used in the next call to Shader::Apply()
208 * @return textureId The texture ID
210 Integration::ResourceId GetTextureIdToRender();
214 * @pre This method is not thread-safe, and should only be called from the update thread.
215 * @param[in] value The grid density
217 void SetGridDensity(float value);
220 * Get the grid density ID.
221 * @pre This method is not thread-safe, and should only be called from the render thread.
222 * @return The grid density.
224 float GetGridDensity();
227 * Installs metadata related to a newly installed uniform property.
228 * @pre This method is not thread-safe, and should only be called from the render-thread.
229 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
231 void InstallUniformMetaInRender( UniformMeta* meta );
234 * Sets the uniform coordinate type
235 * @param index of the uniform
238 void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
241 * @brief Set the program for a geometry type.
242 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
243 * @param[in] resourceId The resource ID for the program.
244 * @param[in] shaderData The program's vertex/fragment source and optionally compiled bytecode
245 * @param[in] programCache Owner of the Programs
246 * @param[in] modifiesGeometry True if the vertex shader changes geometry
248 void SetProgram( /** @deprecated This is currently ignored and will soon disappear. */
249 GeometryType geometryType,
250 Integration::ResourceId resourceId,
251 Integration::ShaderDataPtr shaderData,
252 ProgramCache* programCache,
253 bool modifiesGeometry );
256 * Determine if subtypes are required for the given geometry type
258 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
259 * @return TRUE if subtypes are required, FALSE if there is only one subtype available
261 bool AreSubtypesRequired(GeometryType geometryType);
264 * Get the program associated with the given type and subtype
266 * @param[in] context the context used to render.
267 * @param[in] type the type of the object (geometry) that is being rendered.
268 * @param[in] subType Identifier for geometry types with specialised default shaders
269 * @param[out] programIndex returns the program index to be passed onto SetUniforms.
270 * @return the program to use.
272 Program* GetProgram( Context& context,
274 ShaderSubTypes subType,
275 unsigned int& programIndex );
278 * Get the program built for this shader
280 * @return the program to use.
282 Program* GetProgram();
285 * Sets the shader specific uniforms including custom uniforms
286 * @pre The shader has been initialized.
287 * @pre This method is not thread-safe, and should only be called from the render-thread.
288 * @param[in] context The context used to render.
289 * @param[in] program to use.
290 * @param[in] bufferIndex The buffer to read shader properties from.
291 * @param[in] type the type of the object (geometry) that is being rendered.
292 * @param[in] subType Identifier for geometry types with specialised default shaders
294 void SetUniforms( Context& context,
296 BufferIndex bufferIndex,
297 unsigned int programIndex,
298 ShaderSubTypes subType = SHADER_DEFAULT );
302 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
303 float mGridDensity; ///< grid density
304 Texture* mTexture; ///< Raw Pointer to Texture
305 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
306 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
310 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
311 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
313 // These members are only safe to access during UpdateManager::Update()
314 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
316 // These members are only safe to access in render thread
317 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
320 // Messages for Shader, to be processed in Update thread.
321 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
322 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
323 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
324 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
325 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
327 } // namespace SceneGraph
329 } // namespace Internal
333 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__