1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2017 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/render/data-providers/render-data-provider.h>
30 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
31 #include <dali/internal/render/renderers/render-geometry.h>
32 #include <dali/internal/update/manager/render-instruction-processor.h>
33 #include <dali/integration-api/debug.h>
46 class SceneController;
48 class NodeDataProvider;
55 * Renderers are used to render meshes
56 * These objects are used during RenderManager::Render(), so properties modified during
57 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
59 class Renderer : public GlResourceOwner
64 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
66 struct StencilParameters
68 StencilParameters( RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
69 int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
70 StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
71 : stencilFunctionMask ( stencilFunctionMask ),
72 stencilFunctionReference ( stencilFunctionReference ),
73 stencilMask ( stencilMask ),
74 renderMode ( renderMode ),
75 stencilFunction ( stencilFunction ),
76 stencilOperationOnFail ( stencilOperationOnFail ),
77 stencilOperationOnZFail ( stencilOperationOnZFail ),
78 stencilOperationOnZPass ( stencilOperationOnZPass )
82 int stencilFunctionMask; ///< The stencil function mask
83 int stencilFunctionReference; ///< The stencil function reference
84 int stencilMask; ///< The stencil mask
85 RenderMode::Type renderMode:3; ///< The render mode
86 StencilFunction::Type stencilFunction:3; ///< The stencil function
87 StencilOperation::Type stencilOperationOnFail:3; ///< The stencil operation for stencil test fail
88 StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
89 StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
93 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
95 void GlContextDestroyed();
98 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
103 * Create a new renderer instance
104 * @param[in] dataProviders The data providers for the renderer
105 * @param[in] geometry The geometry for the renderer
106 * @param[in] blendingBitmask A bitmask of blending options.
107 * @param[in] blendColor The blend color to pass to GL
108 * @param[in] faceCullingMode The face-culling mode.
109 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
110 * @param[in] depthWriteMode Depth buffer write mode
111 * @param[in] depthTestMode Depth buffer test mode
112 * @param[in] depthFunction Depth function
113 * @param[in] stencilParameters Struct containing all stencil related options
115 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
116 Render::Geometry* geometry,
117 unsigned int blendingBitmask,
118 const Vector4& blendColor,
119 FaceCullingMode::Type faceCullingMode,
120 bool preMultipliedAlphaEnabled,
121 DepthWriteMode::Type depthWriteMode,
122 DepthTestMode::Type depthTestMode,
123 DepthFunction::Type depthFunction,
124 StencilParameters& stencilParameters );
128 * @param[in] dataProviders The data providers for the renderer
129 * @param[in] geometry The geometry for the renderer
130 * @param[in] blendingBitmask A bitmask of blending options.
131 * @param[in] blendColor The blend color to pass to GL
132 * @param[in] faceCullingMode The face-culling mode.
133 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
134 * @param[in] depthWriteMode Depth buffer write mode
135 * @param[in] depthTestMode Depth buffer test mode
136 * @param[in] depthFunction Depth function
137 * @param[in] stencilParameters Struct containing all stencil related options
139 Renderer( SceneGraph::RenderDataProvider* dataProviders,
140 Render::Geometry* geometry,
141 unsigned int blendingBitmask,
142 const Vector4& blendColor,
143 FaceCullingMode::Type faceCullingMode,
144 bool preMultipliedAlphaEnabled,
145 DepthWriteMode::Type depthWriteMode,
146 DepthTestMode::Type depthTestMode,
147 DepthFunction::Type depthFunction,
148 StencilParameters& stencilParameters );
151 * Change the data providers of the renderer
152 * @param[in] dataProviders The data providers
154 void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
157 * Change the geometry used by the renderer
158 * @param[in] geometry The new geometry
160 void SetGeometry( Render::Geometry* geometry );
163 * Second-phase construction.
164 * This is called when the renderer is inside render thread
165 * @param[in] context Context used by the renderer
167 void Initialize( Context& context );
175 * Set the face-culling mode.
176 * @param[in] mode The face-culling mode.
178 void SetFaceCullingMode( FaceCullingMode::Type mode );
181 * Set the bitmask for blending options
182 * @param[in] bitmask A bitmask of blending options.
184 void SetBlendingBitMask( unsigned int bitmask );
187 * Set the blend color for blending options
188 * @param[in] blendColor The blend color to pass to GL
190 void SetBlendColor( const Vector4& color );
193 * Set the first element index to draw by the indexed draw
194 * @param[in] firstElement index of first element to draw
196 void SetIndexedDrawFirstElement( size_t firstElement );
199 * Set the number of elements to draw by the indexed draw
200 * @param[in] elementsCount number of elements to draw
202 void SetIndexedDrawElementsCount( size_t elementsCount );
205 * @brief Set whether the Pre-multiplied Alpha Blending is required
207 * @param[in] preMultipled whether alpha is pre-multiplied.
209 void EnablePreMultipliedAlpha( bool preMultipled );
212 * Sets the depth write mode
213 * @param[in] depthWriteMode The depth write mode
215 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
218 * Query the Renderer's depth write mode
219 * @return The renderer depth write mode
221 DepthWriteMode::Type GetDepthWriteMode() const;
224 * Sets the depth test mode
225 * @param[in] depthTestMode The depth test mode
227 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
230 * Query the Renderer's depth test mode
231 * @return The renderer depth test mode
233 DepthTestMode::Type GetDepthTestMode() const;
236 * Sets the depth function
237 * @param[in] depthFunction The depth function
239 void SetDepthFunction( DepthFunction::Type depthFunction );
242 * Query the Renderer's depth function
243 * @return The renderer depth function
245 DepthFunction::Type GetDepthFunction() const;
248 * Sets the render mode
249 * @param[in] renderMode The render mode
251 void SetRenderMode( RenderMode::Type mode );
254 * Gets the render mode
255 * @return The render mode
257 RenderMode::Type GetRenderMode() const;
260 * Sets the stencil function
261 * @param[in] stencilFunction The stencil function
263 void SetStencilFunction( StencilFunction::Type stencilFunction );
266 * Gets the stencil function
267 * @return The stencil function
269 StencilFunction::Type GetStencilFunction() const;
272 * Sets the stencil function mask
273 * @param[in] stencilFunctionMask The stencil function mask
275 void SetStencilFunctionMask( int stencilFunctionMask );
278 * Gets the stencil function mask
279 * @return The stencil function mask
281 int GetStencilFunctionMask() const;
284 * Sets the stencil function reference
285 * @param[in] stencilFunctionReference The stencil function reference
287 void SetStencilFunctionReference( int stencilFunctionReference );
290 * Gets the stencil function reference
291 * @return The stencil function reference
293 int GetStencilFunctionReference() const;
296 * Sets the stencil mask
297 * @param[in] stencilMask The stencil mask
299 void SetStencilMask( int stencilMask );
302 * Gets the stencil mask
303 * @return The stencil mask
305 int GetStencilMask() const;
308 * Sets the stencil operation for when the stencil test fails
309 * @param[in] stencilOperationOnFail The stencil operation
311 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
314 * Gets the stencil operation for when the stencil test fails
315 * @return The stencil operation
317 StencilOperation::Type GetStencilOperationOnFail() const;
320 * Sets the stencil operation for when the depth test fails
321 * @param[in] stencilOperationOnZFail The stencil operation
323 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
326 * Gets the stencil operation for when the depth test fails
327 * @return The stencil operation
329 StencilOperation::Type GetStencilOperationOnZFail() const;
332 * Sets the stencil operation for when the depth test passes
333 * @param[in] stencilOperationOnZPass The stencil operation
335 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
338 * Gets the stencil operation for when the depth test passes
339 * @return The stencil operation
341 StencilOperation::Type GetStencilOperationOnZPass() const;
344 * Called to render during RenderManager::Render().
345 * @param[in] context The context used for rendering
346 * @param[in] bufferIndex The index of the previous update buffer.
347 * @param[in] node The node using this renderer
348 * @param[in] defaultShader in case there is no custom shader
349 * @param[in] modelViewMatrix The model-view matrix.
350 * @param[in] viewMatrix The view matrix.
351 * @param[in] projectionMatrix The projection matrix.
352 * @param[in] size Size of the render item
353 * @param[in] blend If true, blending is enabled
355 void Render( Context& context,
356 BufferIndex bufferIndex,
357 const SceneGraph::NodeDataProvider& node,
358 SceneGraph::Shader& defaultShader,
359 const Matrix& modelMatrix,
360 const Matrix& modelViewMatrix,
361 const Matrix& viewMatrix,
362 const Matrix& projectionMatrix,
367 * Write the renderer's sort attributes to the passed in reference
369 * @param[in] bufferIndex The current update buffer index.
370 * @param[out] sortAttributes
372 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
376 struct UniformIndexMap;
379 Renderer( const Renderer& );
382 Renderer& operator=( const Renderer& rhs );
385 * Sets blending options
386 * @param context to use
387 * @param blend Wheter blending should be enabled or not
389 void SetBlending( Context& context, bool blend );
392 * Set the uniforms from properties according to the uniform map
393 * @param[in] bufferIndex The index of the previous update buffer.
394 * @param[in] node The node using the renderer
395 * @param[in] size The size of the renderer
396 * @param[in] program The shader program on which to set the uniforms.
398 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
401 * Set the program uniform in the map from the mapped property
402 * @param[in] bufferIndex The index of the previous update buffer.
403 * @param[in] program The shader program
404 * @param[in] map The uniform
406 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
409 * Bind the textures and setup the samplers
410 * @param[in] context The GL context
411 * @param[in] program The shader program
412 * @return False if create or bind failed, true if success.
414 bool BindTextures( Context& context, Program& program );
418 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
421 Render::Geometry* mGeometry;
423 struct UniformIndexMap
425 unsigned int uniformIndex; ///< The index of the cached location in the Program
426 const PropertyInputImpl* propertyValue;
429 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
431 UniformIndexMappings mUniformIndexMap;
432 Vector<GLint> mAttributesLocation;
434 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
435 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
437 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
438 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
440 DepthFunction::Type mDepthFunction:3; ///< The depth function
441 FaceCullingMode::Type mFaceCullingMode:2; ///< The mode of face culling
442 DepthWriteMode::Type mDepthWriteMode:2; ///< The depth write mode
443 DepthTestMode::Type mDepthTestMode:2; ///< The depth test mode
444 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
445 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
449 } // namespace SceneGraph
451 } // namespace Internal
455 #endif // DALI_INTERNAL_RENDER_RENDERER_H