1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
5 * Copyright (c) 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>
56 * A base class for a collection of shader programs, to apply an effect to different geometry types.
57 * This class is also the default shader so its easier to override default behaviour
59 class Shader : public PropertyOwner
65 * @param hints Geometry hints
67 Shader( Dali::ShaderEffect::GeometryHints& hints );
75 * Second stage initialization, called when added to the UpdateManager
76 * @param renderQueue Used to queue messages from update to render thread.
77 * @param textureCache Used to retrieve effect textures when rendering.
79 void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
81 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82 // The following methods are called during UpdateManager::Update()
83 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86 * Query whether a shader geometry hint is set.
87 * @pre The shader has been initialized.
88 * @param[in] hint The geometry hint to check.
89 * @return True if the given geometry hint is set.
91 bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
93 return mGeometryHints & hint;
97 * Retrieve the set of geometry hints.
100 Dali::ShaderEffect::GeometryHints GetGeometryHints() const
102 return mGeometryHints;
106 * Set the geometry hints.
107 * @param[in] hints The hints.
109 void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
111 mGeometryHints = hints;
115 * @return True if the fragment shader outputs only 1.0 on the alpha channel
117 * @note Shaders that can output any value on the alpha channel
118 * including 1.0 should return false for this.
120 bool IsOutputOpaque();
123 * @return True if the fragment shader can output any value but 1.0 on the alpha channel
125 * @note Shaders that can output any value on the alpha channel
126 * including 1.0 should return false for this
128 bool IsOutputTransparent();
131 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
133 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
135 // no default properties
139 * Set the ID used to access textures
140 * @pre This method is not thread-safe, and should only be called from the update-thread.
141 * @param[in] updateBufferIndex The current update buffer index.
142 * @param[in] textureId The texture ID.
144 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
147 * Gets the effect texture resource ID
148 * This is zero if there is effect texture
149 * @return the resource Id
151 Integration::ResourceId GetEffectTextureResourceId();
154 * Forwards the meta data from the update thread to the render thread for actual
155 * installation. (Installation is to a std::vector, which is not thread safe)
156 * @sa InstallUniformMetaInRender
157 * @pre This method should only be called from the update thread.
158 * @param[in] updateBufferIndex The current update buffer index.
159 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
161 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
164 * Forwards coordinate type to render
165 * @sa InstallUniformMetaInRender
166 * @pre This method should only be called from the update thread.
167 * @param[in] updateBufferIndex The current update buffer index.
168 * @param[in] index of the metadata.
169 * @param[in] type the coordinate type.
171 void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
174 * Forwards the grid density.
175 * @pre This method is not thread-safe, and should only be called from the update thread.
176 * @param[in] updateBufferIndex The current update buffer index.
177 * @param[in] density The grid density.
179 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
183 * @pre This method is not thread-safe, and should only be called from the update thread.
184 * @param[in] updateBufferIndex The current update buffer index.
185 * @param[in] hint The geometry hints.
187 void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
189 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190 // The following methods are called in Render thread
191 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194 * Set the ID used to access textures
195 * @pre This method is not thread-safe, and should only be called from the render thread.
196 * @param[in] textureId The texture ID.
198 void SetTextureId( Integration::ResourceId textureId );
201 * Get the texture id, that will be used in the next call to Shader::Apply()
202 * @return textureId The texture ID
204 Integration::ResourceId GetTextureIdToRender();
208 * @pre This method is not thread-safe, and should only be called from the update thread.
209 * @param[in] value The grid density
211 void SetGridDensity(float value);
214 * Get the grid density ID.
215 * @pre This method is not thread-safe, and should only be called from the render thread.
216 * @return The grid density.
218 float GetGridDensity();
221 * Installs metadata related to a newly installed uniform property.
222 * @pre This method is not thread-safe, and should only be called from the render-thread.
223 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
225 void InstallUniformMetaInRender( UniformMeta* meta );
228 * Sets the uniform coordinate type
229 * @param index of the uniform
232 void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
235 * @brief Set the program for this shader.
236 * @param[in] shaderData The program's vertex/fragment source and optionally precompiled shader binary.
237 * @param[in] programCache Owner of the Programs.
238 * @param[in] modifiesGeometry True if the vertex shader changes the positions of vertexes such that
239 * they might exceed the bounding box of vertexes passing through the default transformation.
241 void SetProgram( Internal::ShaderDataPtr shaderData,
242 ProgramCache* programCache,
243 bool modifiesGeometry );
246 * Get the program built for this shader
247 * @return The program built from the shader sources.
249 Program* GetProgram();
252 * Sets the shader specific uniforms including custom uniforms
253 * @pre The shader has been initialized.
254 * @pre This method is not thread-safe, and should only be called from the render-thread.
255 * @param[in] context The context used to render.
256 * @param[in] program to use.
257 * @param[in] bufferIndex The buffer to read shader properties from.
258 * @param[in] type the type of the object (geometry) that is being rendered.
259 * @param[in] subType Identifier for geometry types with specialised default shaders
261 void SetUniforms( Context& context,
263 BufferIndex bufferIndex );
267 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
268 float mGridDensity; ///< grid density
270 Texture* mTexture; ///< Raw Pointer to Texture
271 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
272 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
276 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
277 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
279 // These members are only safe to access during UpdateManager::Update()
280 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
282 // These members are only safe to access in render thread
283 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
286 // Messages for Shader, to be processed in Update thread.
287 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
288 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
289 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
290 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
291 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
293 } // namespace SceneGraph
295 } // namespace Internal
299 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__