1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2016 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/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/type-abstraction-enums.h>
28 #include <dali/internal/event/common/property-input-impl.h>
29 #include <dali/internal/event/effects/shader-declarations.h>
30 #include <dali/internal/render/data-providers/render-data-provider.h>
31 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
32 #include <dali/internal/render/renderers/render-geometry.h>
33 #include <dali/internal/update/manager/prepare-render-instructions.h>
34 #include <dali/integration-api/debug.h>
47 class SceneController;
50 class NodeDataProvider;
56 class UniformNameCache;
59 * Renderers are used to render meshes
60 * These objects are used during RenderManager::Render(), so properties modified during
61 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
63 class Renderer : public GlResourceOwner
68 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
70 struct StencilParameters
72 StencilParameters( StencilMode::Type stencilMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
73 int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
74 StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
75 : stencilFunctionMask ( stencilFunctionMask ),
76 stencilFunctionReference ( stencilFunctionReference ),
77 stencilMask ( stencilMask ),
78 stencilFunction ( stencilFunction ),
79 stencilOperationOnFail ( stencilOperationOnFail ),
80 stencilOperationOnZFail ( stencilOperationOnZFail ),
81 stencilOperationOnZPass ( stencilOperationOnZPass ),
82 stencilMode ( stencilMode )
86 int stencilFunctionMask; ///< The stencil function mask
87 int stencilFunctionReference; ///< The stencil function reference
88 int stencilMask; ///< The stencil mask
89 StencilFunction::Type stencilFunction:3; ///< The stencil function
90 StencilOperation::Type stencilOperationOnFail:3; ///< The stencil operation for stencil test fail
91 StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
92 StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
93 StencilMode::Type stencilMode:2; ///< The stencil mode
97 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
99 void GlContextDestroyed();
102 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
107 * Create a new renderer instance
108 * @param[in] dataProviders The data providers for the renderer
109 * @param[in] geometry The geometry for the renderer
110 * @param[in] blendingBitmask A bitmask of blending options.
111 * @param[in] blendColor The blend color to pass to GL
112 * @param[in] faceCullingMode The face-culling mode.
113 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
114 * @param[in] depthWriteMode Depth buffer write mode
115 * @param[in] depthTestMode Depth buffer test mode
116 * @param[in] depthFunction Depth function
117 * @param[in] stencilParameters Struct containing all stencil related options
118 * @param[in] writeToColorBuffer Set to True to write to the color buffer
120 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
121 Render::Geometry* geometry,
122 unsigned int blendingBitmask,
123 const Vector4* blendColor,
124 FaceCullingMode::Type faceCullingMode,
125 bool preMultipliedAlphaEnabled,
126 DepthWriteMode::Type depthWriteMode,
127 DepthTestMode::Type depthTestMode,
128 DepthFunction::Type depthFunction,
129 StencilParameters& stencilParameters,
130 bool writeToColorBuffer );
134 * @param[in] dataProviders The data providers for the renderer
135 * @param[in] geometry The geometry for the renderer
136 * @param[in] blendingBitmask A bitmask of blending options.
137 * @param[in] blendColor The blend color to pass to GL
138 * @param[in] faceCullingMode The face-culling mode.
139 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
140 * @param[in] depthWriteMode Depth buffer write mode
141 * @param[in] depthTestMode Depth buffer test mode
142 * @param[in] depthFunction Depth function
143 * @param[in] stencilParameters Struct containing all stencil related options
144 * @param[in] writeToColorBuffer Set to True to write to the color buffer
146 Renderer( SceneGraph::RenderDataProvider* dataProviders,
147 Render::Geometry* geometry,
148 unsigned int blendingBitmask,
149 const Vector4* blendColor,
150 FaceCullingMode::Type faceCullingMode,
151 bool preMultipliedAlphaEnabled,
152 DepthWriteMode::Type depthWriteMode,
153 DepthTestMode::Type depthTestMode,
154 DepthFunction::Type depthFunction,
155 StencilParameters& stencilParameters,
156 bool writeToColorBuffer );
159 * Change the data providers of the renderer
160 * @param[in] dataProviders The data providers
162 void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
165 * Change the geometry used by the renderer
166 * @param[in] geometry The new geometry
168 void SetGeometry( Render::Geometry* geometry );
170 * Second-phase construction.
171 * This is called when the renderer is inside render thread
172 * @param[in] context to use
173 * @param[in] textureCache to use
174 * @param[in] uniformNameCache to use
176 void Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache );
184 * Set the face-culling mode.
185 * @param[in] mode The face-culling mode.
187 void SetFaceCullingMode( FaceCullingMode::Type mode );
190 * Set the bitmask for blending options
191 * @param[in] bitmask A bitmask of blending options.
193 void SetBlendingBitMask( unsigned int bitmask );
196 * Set the blend color for blending options
197 * @param[in] blendColor The blend color to pass to GL
199 void SetBlendColor( const Vector4* color );
202 * Set the first element index to draw by the indexed draw
203 * @param[in] firstElement index of first element to draw
205 void SetIndexedDrawFirstElement( size_t firstElement );
208 * Set the number of elements to draw by the indexed draw
209 * @param[in] elementsCount number of elements to draw
211 void SetIndexedDrawElementsCount( size_t elementsCount );
214 * @brief Set whether the Pre-multiplied Alpha Blending is required
216 * @param[in] preMultipled whether alpha is pre-multiplied.
218 void EnablePreMultipliedAlpha( bool preMultipled );
221 * Sets the depth write mode
222 * @param[in] depthWriteMode The depth write mode
224 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
227 * Query the Renderer's depth write mode
228 * @return The renderer depth write mode
230 DepthWriteMode::Type GetDepthWriteMode() const;
233 * Sets the depth test mode
234 * @param[in] depthTestMode The depth test mode
236 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
239 * Query the Renderer's depth test mode
240 * @return The renderer depth test mode
242 DepthTestMode::Type GetDepthTestMode() const;
245 * Sets the depth function
246 * @param[in] depthFunction The depth function
248 void SetDepthFunction( DepthFunction::Type depthFunction );
251 * Query the Renderer's depth function
252 * @return The renderer depth function
254 DepthFunction::Type GetDepthFunction() const;
257 * Sets the stencil mode
258 * @param[in] stencilMode The stencil function
260 void SetStencilMode( StencilMode::Type stencilMode );
263 * Gets the stencil mode
264 * @return The stencil function
266 StencilMode::Type GetStencilMode() const;
269 * Sets the stencil function
270 * @param[in] stencilFunction The stencil function
272 void SetStencilFunction( StencilFunction::Type stencilFunction );
275 * Gets the stencil function
276 * @return The stencil function
278 StencilFunction::Type GetStencilFunction() const;
281 * Sets the stencil function mask
282 * @param[in] stencilFunctionMask The stencil function mask
284 void SetStencilFunctionMask( int stencilFunctionMask );
287 * Gets the stencil function mask
288 * @return The stencil function mask
290 int GetStencilFunctionMask() const;
293 * Sets the stencil function reference
294 * @param[in] stencilFunctionReference The stencil function reference
296 void SetStencilFunctionReference( int stencilFunctionReference );
299 * Gets the stencil function reference
300 * @return The stencil function reference
302 int GetStencilFunctionReference() const;
305 * Sets the stencil mask
306 * @param[in] stencilMask The stencil mask
308 void SetStencilMask( int stencilMask );
311 * Gets the stencil mask
312 * @return The stencil mask
314 int GetStencilMask() const;
317 * Sets the stencil operation for when the stencil test fails
318 * @param[in] stencilOperationOnFail The stencil operation
320 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
323 * Gets the stencil operation for when the stencil test fails
324 * @return The stencil operation
326 StencilOperation::Type GetStencilOperationOnFail() const;
329 * Sets the stencil operation for when the depth test fails
330 * @param[in] stencilOperationOnZFail The stencil operation
332 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
335 * Gets the stencil operation for when the depth test fails
336 * @return The stencil operation
338 StencilOperation::Type GetStencilOperationOnZFail() const;
341 * Sets the stencil operation for when the depth test passes
342 * @param[in] stencilOperationOnZPass The stencil operation
344 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
347 * Gets the stencil operation for when the depth test passes
348 * @return The stencil operation
350 StencilOperation::Type GetStencilOperationOnZPass() const;
353 * Sets whether or not to write to the color buffer
354 * @param[in] writeToColorBuffer True to write to the color buffer
356 void SetWriteToColorBuffer( bool writeToColorBuffer );
359 * Gets whether or not to write to the color buffer
360 * @return True to write to the color buffer
362 bool GetWriteToColorBuffer() const;
365 * Called to render during RenderManager::Render().
366 * @param[in] context The context used for rendering
367 * @param[in] textureCache The texture cache used to get textures
368 * @param[in] bufferIndex The index of the previous update buffer.
369 * @param[in] node The node using this renderer
370 * @param[in] defaultShader in case there is no custom shader
371 * @param[in] modelViewMatrix The model-view matrix.
372 * @param[in] viewMatrix The view matrix.
373 * @param[in] projectionMatrix The projection matrix.
375 void Render( Context& context,
376 SceneGraph::TextureCache& textureCache,
377 BufferIndex bufferIndex,
378 const SceneGraph::NodeDataProvider& node,
379 SceneGraph::Shader& defaultShader,
380 const Matrix& modelMatrix,
381 const Matrix& modelViewMatrix,
382 const Matrix& viewMatrix,
383 const Matrix& projectionMatrix,
388 * Write the renderer's sort attributes to the passed in reference
390 * @param[in] bufferIndex The current update buffer index.
391 * @param[out] sortAttributes
393 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
397 struct UniformIndexMap;
400 Renderer( const Renderer& );
403 Renderer& operator=( const Renderer& rhs );
406 * Sets blending options
407 * @param context to use
408 * @param blend Wheter blending should be enabled or not
410 void SetBlending( Context& context, bool blend );
413 * Set the uniforms from properties according to the uniform map
414 * @param[in] bufferIndex The index of the previous update buffer.
415 * @param[in] node The node using the renderer
416 * @param[in] size The size of the renderer
417 * @param[in] program The shader program on which to set the uniforms.
419 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
422 * Set the program uniform in the map from the mapped property
423 * @param[in] bufferIndex The index of the previous update buffer.
424 * @param[in] program The shader program
425 * @param[in] map The uniform
427 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
430 * Bind the textures and setup the samplers
431 * @param[in] context The GL context
432 * @param[in] textureCache The texture cache
433 * @param[in] program The shader program
434 * @return False if create or bind failed, true if success.
436 bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
440 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
443 SceneGraph::TextureCache* mTextureCache;
444 Render::UniformNameCache* mUniformNameCache;
445 Render::Geometry* mGeometry;
447 struct UniformIndexMap
449 unsigned int uniformIndex; ///< The index of the cached location in the Program
450 const PropertyInputImpl* propertyValue;
453 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
454 UniformIndexMappings mUniformIndexMap;
455 Vector<GLint> mAttributesLocation;
457 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
458 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
460 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
461 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
463 DepthFunction::Type mDepthFunction:3; ///< The depth function
464 FaceCullingMode::Type mFaceCullingMode:2; ///< The mode of face culling
465 BlendMode::Type mBlendMode:2; ///< The mode of blending
466 DepthWriteMode::Type mDepthWriteMode:2; ///< The depth write mode
467 DepthTestMode::Type mDepthTestMode:2; ///< The depth test mode
468 bool mWriteToColorBuffer:1; ///< True if we are writing to the color buffer
469 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
470 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
473 } // namespace SceneGraph
475 } // namespace Internal
479 #endif // DALI_INTERNAL_RENDER_RENDERER_H