Remove multiple programs from SceneGraph::Shader
[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) 2014-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/integration-api/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 Integration
44 {
45 typedef unsigned int ResourceId;
46 } // namespace Integration
47
48 namespace Internal
49 {
50
51 class ProgramController;
52 class Program;
53
54 namespace SceneGraph
55 {
56
57 class RenderQueue;
58 class UniformMeta;
59 class TextureCache;
60
61 /**
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
64  */
65 class Shader : public PropertyOwner
66 {
67 public:
68
69   /**
70    * Constructor
71    * @param hints Geometry hints
72    */
73   Shader( Dali::ShaderEffect::GeometryHints& hints );
74
75   /**
76    * Virtual destructor
77    */
78   virtual ~Shader();
79
80   /**
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.
84    */
85   void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
86
87   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88   // The following methods are called during UpdateManager::Update()
89   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90
91   /**
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.
96    */
97   bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
98   {
99     return mGeometryHints & hint;
100   }
101
102   /**
103    * Retrieve the set of geometry hints.
104    * @return The hints.
105    */
106   Dali::ShaderEffect::GeometryHints GetGeometryHints() const
107   {
108     return mGeometryHints;
109   }
110
111   /**
112    * Set the geometry hints.
113    * @param[in] hints The hints.
114    */
115   void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
116   {
117     mGeometryHints = hints;
118   }
119
120   /**
121    * @return True if the fragment shader outputs only 1.0 on the alpha channel
122    *
123    * @note Shaders that can output any value on the alpha channel
124    * including 1.0 should return false for this.
125    */
126   bool IsOutputOpaque();
127
128   /**
129    * @return True if the fragment shader can output any value but 1.0 on the alpha channel
130    *
131    * @note Shaders that can output any value on the alpha channel
132    * including 1.0 should return false for this
133    */
134   bool IsOutputTransparent();
135
136   /**
137    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
138    */
139   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
140   {
141     // no default properties
142   }
143
144   /**
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.
149    */
150   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
151
152   /**
153    * Gets the effect texture resource ID
154    * This is zero if there is effect texture
155    * @return the resource Id
156    */
157   Integration::ResourceId GetEffectTextureResourceId();
158
159   /**
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.
166    */
167   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
168
169   /**
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.
176    */
177   void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
178
179   /**
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.
184    */
185   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
186
187   /**
188    * Forwards hints.
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.
192    */
193   void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
194
195   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196   // The following methods are called in Render thread
197   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
198
199   /**
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.
203    */
204   void SetTextureId( Integration::ResourceId textureId );
205
206   /**
207    * Get the texture id, that will be used in the next call to Shader::Apply()
208    * @return textureId The texture ID
209    */
210   Integration::ResourceId GetTextureIdToRender();
211
212   /**
213    * Sets grid density
214    * @pre This method is not thread-safe, and should only be called from the update thread.
215    * @param[in] value The grid density
216    */
217   void SetGridDensity(float value);
218
219   /**
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.
223    */
224   float GetGridDensity();
225
226   /**
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.
230    */
231   void InstallUniformMetaInRender( UniformMeta* meta );
232
233   /**
234    * Sets the uniform coordinate type
235    * @param index of the uniform
236    * @param type to set
237    */
238   void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
239
240   /**
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
247    */
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 );
254
255   /**
256    * Determine if subtypes are required for the given geometry type
257    * @deprecated
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
260    */
261   bool AreSubtypesRequired(GeometryType geometryType);
262
263   /**
264    * Get the program associated with the given type and subtype
265    * @deprecated
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.
271    */
272   Program* GetProgram( Context& context,
273                        GeometryType type,
274                        ShaderSubTypes subType,
275                        unsigned int& programIndex );
276
277   /**
278    * Get the program built for this shader
279    * @deprecated
280    * @return the program to use.
281    */
282   Program* GetProgram();
283
284   /**
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
293    */
294   void SetUniforms( Context& context,
295                     Program& program,
296                     BufferIndex bufferIndex,
297                     unsigned int programIndex,
298                     ShaderSubTypes subType = SHADER_DEFAULT );
299
300 private: // Data
301
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
307
308   Program*                       mProgram;
309
310   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
311   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
312
313   // These members are only safe to access during UpdateManager::Update()
314   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
315
316   // These members are only safe to access in render thread
317   TextureCache*                  mTextureCache; // Used for retrieving textures in the render thread
318 };
319
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 );
326
327 } // namespace SceneGraph
328
329 } // namespace Internal
330
331 } // namespace Dali
332
333 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__