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] resourceId The resource ID for the program.
243 * @param[in] shaderData The program's vertex/fragment source and optionally compiled bytecode
244 * @param[in] programCache Owner of the Programs
245 * @param[in] modifiesGeometry True if the vertex shader changes the positions of vertexes such that
246 * they might exceed the bounding box of vertexes passing through the default transformation.
248 void SetProgram( Integration::ResourceId resourceId,
249 Integration::ShaderDataPtr shaderData,
250 ProgramCache* programCache,
251 bool modifiesGeometry );
254 * Get the program built for this shader
255 * @return The program built from the shader sources.
257 Program* GetProgram();
260 * Sets the shader specific uniforms including custom uniforms
261 * @pre The shader has been initialized.
262 * @pre This method is not thread-safe, and should only be called from the render-thread.
263 * @param[in] context The context used to render.
264 * @param[in] program to use.
265 * @param[in] bufferIndex The buffer to read shader properties from.
266 * @param[in] type the type of the object (geometry) that is being rendered.
267 * @param[in] subType Identifier for geometry types with specialised default shaders
269 void SetUniforms( Context& context,
271 BufferIndex bufferIndex );
275 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
276 float mGridDensity; ///< grid density
278 Texture* mTexture; ///< Raw Pointer to Texture
279 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
280 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
284 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
285 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
287 // These members are only safe to access during UpdateManager::Update()
288 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
290 // These members are only safe to access in render thread
291 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
294 // Messages for Shader, to be processed in Update thread.
295 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
296 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
297 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
298 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
299 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
301 } // namespace SceneGraph
303 } // namespace Internal
307 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__