Shader binary synchronous simplified IO
[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 Program;
52
53 namespace SceneGraph
54 {
55
56 class RenderQueue;
57 class UniformMeta;
58 class TextureCache;
59
60 /**
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
63  */
64 class Shader : public PropertyOwner
65 {
66 public:
67
68   /**
69    * Constructor
70    * @param hints Geometry hints
71    */
72   Shader( Dali::ShaderEffect::GeometryHints& hints );
73
74   /**
75    * Virtual destructor
76    */
77   virtual ~Shader();
78
79   /**
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.
83    */
84   void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
85
86   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87   // The following methods are called during UpdateManager::Update()
88   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89
90   /**
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.
95    */
96   bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
97   {
98     return mGeometryHints & hint;
99   }
100
101   /**
102    * Retrieve the set of geometry hints.
103    * @return The hints.
104    */
105   Dali::ShaderEffect::GeometryHints GetGeometryHints() const
106   {
107     return mGeometryHints;
108   }
109
110   /**
111    * Set the geometry hints.
112    * @param[in] hints The hints.
113    */
114   void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
115   {
116     mGeometryHints = hints;
117   }
118
119   /**
120    * @return True if the fragment shader outputs only 1.0 on the alpha channel
121    *
122    * @note Shaders that can output any value on the alpha channel
123    * including 1.0 should return false for this.
124    */
125   bool IsOutputOpaque();
126
127   /**
128    * @return True if the fragment shader can output any value but 1.0 on the alpha channel
129    *
130    * @note Shaders that can output any value on the alpha channel
131    * including 1.0 should return false for this
132    */
133   bool IsOutputTransparent();
134
135   /**
136    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
137    */
138   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
139   {
140     // no default properties
141   }
142
143   /**
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.
148    */
149   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
150
151   /**
152    * Gets the effect texture resource ID
153    * This is zero if there is effect texture
154    * @return the resource Id
155    */
156   Integration::ResourceId GetEffectTextureResourceId();
157
158   /**
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.
165    */
166   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
167
168   /**
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.
175    */
176   void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
177
178   /**
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.
183    */
184   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
185
186   /**
187    * Forwards hints.
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.
191    */
192   void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
193
194   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
195   // The following methods are called in Render thread
196   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
197
198   /**
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.
202    */
203   void SetTextureId( Integration::ResourceId textureId );
204
205   /**
206    * Get the texture id, that will be used in the next call to Shader::Apply()
207    * @return textureId The texture ID
208    */
209   Integration::ResourceId GetTextureIdToRender();
210
211   /**
212    * Sets grid density
213    * @pre This method is not thread-safe, and should only be called from the update thread.
214    * @param[in] value The grid density
215    */
216   void SetGridDensity(float value);
217
218   /**
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.
222    */
223   float GetGridDensity();
224
225   /**
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.
229    */
230   void InstallUniformMetaInRender( UniformMeta* meta );
231
232   /**
233    * Sets the uniform coordinate type
234    * @param index of the uniform
235    * @param type to set
236    */
237   void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
238
239   /**
240    * @brief Set the program for this shader.
241    * @param[in] resourceId        The resource ID for the program.
242    * @param[in] shaderData        The program's vertex/fragment source and optionally precompiled shader binary.
243    * @param[in] programCache      Owner of the Programs.
244    * @param[in] modifiesGeometry  True if the vertex shader changes the positions of vertexes such that
245    * they might exceed the bounding box of vertexes passing through the default transformation.
246    */
247   void SetProgram( Integration::ResourceId resourceId,
248                    Integration::ShaderDataPtr shaderData,
249                    ProgramCache* programCache,
250                    bool modifiesGeometry );
251
252   /**
253    * @brief Set the program for this shader.
254    * @param[in] shaderData        The program's vertex/fragment source and optionally precompiled shader binary.
255    * @param[in] programCache      Owner of the Programs.
256    * @param[in] modifiesGeometry  True if the vertex shader changes the positions of vertexes such that
257    * they might exceed the bounding box of vertexes passing through the default transformation.
258    */
259   void SetProgram( Integration::ShaderDataPtr shaderData,
260                    ProgramCache* programCache,
261                    bool modifiesGeometry );
262
263   /**
264    * Get the program built for this shader
265    * @return The program built from the shader sources.
266    */
267   Program* GetProgram();
268
269   /**
270    * Sets the shader specific uniforms including custom uniforms
271    * @pre The shader has been initialized.
272    * @pre This method is not thread-safe, and should only be called from the render-thread.
273    * @param[in] context The context used to render.
274    * @param[in] program to use.
275    * @param[in] bufferIndex The buffer to read shader properties from.
276    * @param[in] type        the type of the object (geometry) that is being rendered.
277    * @param[in] subType     Identifier for geometry types with specialised default shaders
278    */
279   void SetUniforms( Context& context,
280                     Program& program,
281                     BufferIndex bufferIndex );
282
283 private: // Data
284
285   Dali::ShaderEffect::GeometryHints mGeometryHints;    ///< shader geometry hints for building the geometry
286   float                          mGridDensity;      ///< grid density
287
288   Texture*                       mTexture;          ///< Raw Pointer to Texture
289   Integration::ResourceId        mRenderTextureId;  ///< Copy of the texture ID for the render thread
290   Integration::ResourceId        mUpdateTextureId;  ///< Copy of the texture ID for update thread
291
292   Program*                       mProgram;
293
294   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
295   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
296
297   // These members are only safe to access during UpdateManager::Update()
298   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
299
300   // These members are only safe to access in render thread
301   TextureCache*                  mTextureCache; // Used for retrieving textures in the render thread
302 };
303
304 // Messages for Shader, to be processed in Update thread.
305 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
306 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
307 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
308 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
309 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
310
311 } // namespace SceneGraph
312
313 } // namespace Internal
314
315 } // namespace Dali
316
317 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__