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/internal/common/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
61 * A base class for a collection of shader programs, to apply an effect to different geometry types.
62 * This class is also the default shader so its easier to override default behaviour
64 class Shader : public PropertyOwner
70 * @param hints Geometry hints
72 Shader( Dali::ShaderEffect::GeometryHints& hints );
80 * Second stage initialization, called when added to the UpdateManager
81 * @param renderQueue Used to queue messages from update to render thread.
82 * @param textureCache Used to retrieve effect textures when rendering.
84 void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
86 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87 // The following methods are called during UpdateManager::Update()
88 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
91 * Query whether a shader geometry hint is set.
92 * @pre The shader has been initialized.
93 * @param[in] hint The geometry hint to check.
94 * @return True if the given geometry hint is set.
96 bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
98 return mGeometryHints & hint;
102 * Retrieve the set of geometry hints.
105 Dali::ShaderEffect::GeometryHints GetGeometryHints() const
107 return mGeometryHints;
111 * Set the geometry hints.
112 * @param[in] hints The hints.
114 void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
116 mGeometryHints = hints;
120 * @return True if the fragment shader outputs only 1.0 on the alpha channel
122 * @note Shaders that can output any value on the alpha channel
123 * including 1.0 should return false for this.
125 bool IsOutputOpaque();
128 * @return True if the fragment shader can output any value but 1.0 on the alpha channel
130 * @note Shaders that can output any value on the alpha channel
131 * including 1.0 should return false for this
133 bool IsOutputTransparent();
136 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
138 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
140 // no default properties
144 * Set the ID used to access textures
145 * @pre This method is not thread-safe, and should only be called from the update-thread.
146 * @param[in] updateBufferIndex The current update buffer index.
147 * @param[in] textureId The texture ID.
149 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
152 * Gets the effect texture resource ID
153 * This is zero if there is effect texture
154 * @return the resource Id
156 Integration::ResourceId GetEffectTextureResourceId();
159 * Forwards the meta data from the update thread to the render thread for actual
160 * installation. (Installation is to a std::vector, which is not thread safe)
161 * @sa InstallUniformMetaInRender
162 * @pre This method should only be called from the update thread.
163 * @param[in] updateBufferIndex The current update buffer index.
164 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
166 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
169 * Forwards coordinate type to render
170 * @sa InstallUniformMetaInRender
171 * @pre This method should only be called from the update thread.
172 * @param[in] updateBufferIndex The current update buffer index.
173 * @param[in] index of the metadata.
174 * @param[in] type the coordinate type.
176 void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
179 * Forwards the grid density.
180 * @pre This method is not thread-safe, and should only be called from the update thread.
181 * @param[in] updateBufferIndex The current update buffer index.
182 * @param[in] density The grid density.
184 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
188 * @pre This method is not thread-safe, and should only be called from the update thread.
189 * @param[in] updateBufferIndex The current update buffer index.
190 * @param[in] hint The geometry hints.
192 void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
194 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
195 // The following methods are called in Render thread
196 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
199 * Set the ID used to access textures
200 * @pre This method is not thread-safe, and should only be called from the render thread.
201 * @param[in] textureId The texture ID.
203 void SetTextureId( Integration::ResourceId textureId );
206 * Get the texture id, that will be used in the next call to Shader::Apply()
207 * @return textureId The texture ID
209 Integration::ResourceId GetTextureIdToRender();
213 * @pre This method is not thread-safe, and should only be called from the update thread.
214 * @param[in] value The grid density
216 void SetGridDensity(float value);
219 * Get the grid density ID.
220 * @pre This method is not thread-safe, and should only be called from the render thread.
221 * @return The grid density.
223 float GetGridDensity();
226 * Installs metadata related to a newly installed uniform property.
227 * @pre This method is not thread-safe, and should only be called from the render-thread.
228 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
230 void InstallUniformMetaInRender( UniformMeta* meta );
233 * Sets the uniform coordinate type
234 * @param index of the uniform
237 void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
240 * @brief Set the program for this shader.
241 * @param[in] shaderData The program's vertex/fragment source and optionally precompiled shader binary.
242 * @param[in] programCache Owner of the Programs.
243 * @param[in] modifiesGeometry True if the vertex shader changes the positions of vertexes such that
244 * they might exceed the bounding box of vertexes passing through the default transformation.
246 void SetProgram( Internal::ShaderDataPtr shaderData,
247 ProgramCache* programCache,
248 bool modifiesGeometry );
251 * Get the program built for this shader
252 * @return The program built from the shader sources.
254 Program* GetProgram();
257 * Sets the shader specific uniforms including custom uniforms
258 * @pre The shader has been initialized.
259 * @pre This method is not thread-safe, and should only be called from the render-thread.
260 * @param[in] context The context used to render.
261 * @param[in] program to use.
262 * @param[in] bufferIndex The buffer to read shader properties from.
263 * @param[in] type the type of the object (geometry) that is being rendered.
264 * @param[in] subType Identifier for geometry types with specialised default shaders
266 void SetUniforms( Context& context,
268 BufferIndex bufferIndex );
272 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
273 float mGridDensity; ///< grid density
275 Texture* mTexture; ///< Raw Pointer to Texture
276 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
277 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
281 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
282 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
284 // These members are only safe to access during UpdateManager::Update()
285 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
287 // These members are only safe to access in render thread
288 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
291 // Messages for Shader, to be processed in Update thread.
292 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
293 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
294 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
295 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
296 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
298 } // namespace SceneGraph
300 } // namespace Internal
304 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__