Merge branch 'devel/master (1.1.1)' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / scene-graph-shader.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
24
25 #include <dali/internal/common/shader-data.h>
26
27 #include <dali/internal/common/buffer-index.h>
28 #include <dali/internal/common/type-abstraction-enums.h>
29
30 #include <dali/internal/event/common/event-thread-services.h>
31 #include <dali/internal/event/effects/shader-declarations.h>
32
33 #include <dali/internal/update/common/property-owner.h>
34
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>
38
39
40 namespace Dali
41 {
42
43 namespace Internal
44 {
45
46 class Program;
47
48 namespace SceneGraph
49 {
50
51 class RenderQueue;
52 class UniformMeta;
53 class TextureCache;
54
55 /**
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
58  */
59 class Shader : public PropertyOwner
60 {
61 public:
62
63   /**
64    * Constructor
65    * @param hints Geometry hints
66    */
67   Shader( Dali::ShaderEffect::GeometryHints& hints );
68
69   /**
70    * Virtual destructor
71    */
72   virtual ~Shader();
73
74   /**
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.
78    */
79   void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
80
81   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82   // The following methods are called during UpdateManager::Update()
83   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
84
85   /**
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.
90    */
91   bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
92   {
93     return mGeometryHints & hint;
94   }
95
96   /**
97    * Retrieve the set of geometry hints.
98    * @return The hints.
99    */
100   Dali::ShaderEffect::GeometryHints GetGeometryHints() const
101   {
102     return mGeometryHints;
103   }
104
105   /**
106    * Set the geometry hints.
107    * @param[in] hints The hints.
108    */
109   void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
110   {
111     mGeometryHints = hints;
112   }
113
114   /**
115    * @return True if the fragment shader outputs only 1.0 on the alpha channel
116    *
117    * @note Shaders that can output any value on the alpha channel
118    * including 1.0 should return false for this.
119    */
120   bool IsOutputOpaque();
121
122   /**
123    * @return True if the fragment shader can output any value but 1.0 on the alpha channel
124    *
125    * @note Shaders that can output any value on the alpha channel
126    * including 1.0 should return false for this
127    */
128   bool IsOutputTransparent();
129
130   /**
131    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
132    */
133   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
134   {
135     // no default properties
136   }
137
138   /**
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.
143    */
144   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
145
146   /**
147    * Gets the effect texture resource ID
148    * This is zero if there is effect texture
149    * @return the resource Id
150    */
151   Integration::ResourceId GetEffectTextureResourceId();
152
153   /**
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.
160    */
161   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
162
163   /**
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.
170    */
171   void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
172
173   /**
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.
178    */
179   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
180
181   /**
182    * Forwards hints.
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.
186    */
187   void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
188
189   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190   // The following methods are called in Render thread
191   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
192
193   /**
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.
197    */
198   void SetTextureId( Integration::ResourceId textureId );
199
200   /**
201    * Get the texture id, that will be used in the next call to Shader::Apply()
202    * @return textureId The texture ID
203    */
204   Integration::ResourceId GetTextureIdToRender();
205
206   /**
207    * Sets grid density
208    * @pre This method is not thread-safe, and should only be called from the update thread.
209    * @param[in] value The grid density
210    */
211   void SetGridDensity(float value);
212
213   /**
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.
217    */
218   float GetGridDensity();
219
220   /**
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.
224    */
225   void InstallUniformMetaInRender( UniformMeta* meta );
226
227   /**
228    * Sets the uniform coordinate type
229    * @param index of the uniform
230    * @param type to set
231    */
232   void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
233
234   /**
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.
240    */
241   void SetProgram( Internal::ShaderDataPtr shaderData,
242                    ProgramCache* programCache,
243                    bool modifiesGeometry );
244
245   /**
246    * Get the program built for this shader
247    * @return The program built from the shader sources.
248    */
249   Program* GetProgram();
250
251   /**
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
260    */
261   void SetUniforms( Context& context,
262                     Program& program,
263                     BufferIndex bufferIndex );
264
265 private: // Data
266
267   Dali::ShaderEffect::GeometryHints mGeometryHints;    ///< shader geometry hints for building the geometry
268   float                          mGridDensity;      ///< grid density
269
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
273
274   Program*                       mProgram;
275
276   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
277   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
278
279   // These members are only safe to access during UpdateManager::Update()
280   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
281
282   // These members are only safe to access in render thread
283   TextureCache*                  mTextureCache; // Used for retrieving textures in the render thread
284 };
285
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 );
292
293 } // namespace SceneGraph
294
295 } // namespace Internal
296
297 } // namespace Dali
298
299 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__