1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2019 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;
50 class RenderInstruction; //for relfection effect
57 * Renderers are used to render meshes
58 * These objects are used during RenderManager::Render(), so properties modified during
59 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
61 class Renderer : public GlResourceOwner
66 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
68 struct StencilParameters
70 StencilParameters( RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
71 int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
72 StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
73 : stencilFunctionMask ( stencilFunctionMask ),
74 stencilFunctionReference ( stencilFunctionReference ),
75 stencilMask ( stencilMask ),
76 renderMode ( renderMode ),
77 stencilFunction ( stencilFunction ),
78 stencilOperationOnFail ( stencilOperationOnFail ),
79 stencilOperationOnZFail ( stencilOperationOnZFail ),
80 stencilOperationOnZPass ( stencilOperationOnZPass )
84 int stencilFunctionMask; ///< The stencil function mask
85 int stencilFunctionReference; ///< The stencil function reference
86 int stencilMask; ///< The stencil mask
87 RenderMode::Type renderMode:4; ///< The render mode
88 StencilFunction::Type stencilFunction:4; ///< The stencil function
89 StencilOperation::Type stencilOperationOnFail:4; ///< The stencil operation for stencil test fail
90 StencilOperation::Type stencilOperationOnZFail:4; ///< The stencil operation for depth test fail
91 StencilOperation::Type stencilOperationOnZPass:4; ///< The stencil operation for depth test pass
95 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
97 void GlContextDestroyed();
100 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
105 * Create a new renderer instance
106 * @param[in] dataProviders The data providers for the renderer
107 * @param[in] geometry The geometry for the renderer
108 * @param[in] blendingBitmask A bitmask of blending options.
109 * @param[in] blendColor The blend color to pass to GL
110 * @param[in] faceCullingMode The face-culling mode.
111 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
112 * @param[in] depthWriteMode Depth buffer write mode
113 * @param[in] depthTestMode Depth buffer test mode
114 * @param[in] depthFunction Depth function
115 * @param[in] stencilParameters Struct containing all stencil related options
117 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
118 Render::Geometry* geometry,
119 uint32_t blendingBitmask,
120 const Vector4& blendColor,
121 FaceCullingMode::Type faceCullingMode,
122 bool preMultipliedAlphaEnabled,
123 DepthWriteMode::Type depthWriteMode,
124 DepthTestMode::Type depthTestMode,
125 DepthFunction::Type depthFunction,
126 StencilParameters& stencilParameters );
130 * @param[in] dataProviders The data providers for the renderer
131 * @param[in] geometry The geometry for the renderer
132 * @param[in] blendingBitmask A bitmask of blending options.
133 * @param[in] blendColor The blend color to pass to GL
134 * @param[in] faceCullingMode The face-culling mode.
135 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
136 * @param[in] depthWriteMode Depth buffer write mode
137 * @param[in] depthTestMode Depth buffer test mode
138 * @param[in] depthFunction Depth function
139 * @param[in] stencilParameters Struct containing all stencil related options
141 Renderer( SceneGraph::RenderDataProvider* dataProviders,
142 Render::Geometry* geometry,
143 uint32_t blendingBitmask,
144 const Vector4& blendColor,
145 FaceCullingMode::Type faceCullingMode,
146 bool preMultipliedAlphaEnabled,
147 DepthWriteMode::Type depthWriteMode,
148 DepthTestMode::Type depthTestMode,
149 DepthFunction::Type depthFunction,
150 StencilParameters& stencilParameters );
153 * Change the geometry used by the renderer
154 * @param[in] geometry The new geometry
156 void SetGeometry( Render::Geometry* geometry );
159 * Second-phase construction.
160 * This is called when the renderer is inside render thread
161 * @param[in] context Context used by the renderer
163 void Initialize( Context& context );
171 * Set the face-culling mode.
172 * @param[in] mode The face-culling mode.
174 void SetFaceCullingMode( FaceCullingMode::Type mode );
177 * Set the bitmask for blending options
178 * @param[in] bitmask A bitmask of blending options.
180 void SetBlendingBitMask( uint32_t bitmask );
183 * Set the blend color for blending options
184 * @param[in] blendColor The blend color to pass to GL
186 void SetBlendColor( const Vector4& color );
189 * Set the first element index to draw by the indexed draw
190 * @param[in] firstElement index of first element to draw
192 void SetIndexedDrawFirstElement( uint32_t firstElement );
195 * Set the number of elements to draw by the indexed draw
196 * @param[in] elementsCount number of elements to draw
198 void SetIndexedDrawElementsCount( uint32_t elementsCount );
201 * @brief Set whether the Pre-multiplied Alpha Blending is required
203 * @param[in] preMultipled whether alpha is pre-multiplied.
205 void EnablePreMultipliedAlpha( bool preMultipled );
208 * Sets the depth write mode
209 * @param[in] depthWriteMode The depth write mode
211 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
214 * Query the Renderer's depth write mode
215 * @return The renderer depth write mode
217 DepthWriteMode::Type GetDepthWriteMode() const;
220 * Sets the depth test mode
221 * @param[in] depthTestMode The depth test mode
223 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
226 * Query the Renderer's depth test mode
227 * @return The renderer depth test mode
229 DepthTestMode::Type GetDepthTestMode() const;
232 * Sets the depth function
233 * @param[in] depthFunction The depth function
235 void SetDepthFunction( DepthFunction::Type depthFunction );
238 * Query the Renderer's depth function
239 * @return The renderer depth function
241 DepthFunction::Type GetDepthFunction() const;
244 * Sets the render mode
245 * @param[in] renderMode The render mode
247 void SetRenderMode( RenderMode::Type mode );
250 * Gets the render mode
251 * @return The render mode
253 RenderMode::Type GetRenderMode() const;
256 * Sets the stencil function
257 * @param[in] stencilFunction The stencil function
259 void SetStencilFunction( StencilFunction::Type stencilFunction );
262 * Gets the stencil function
263 * @return The stencil function
265 StencilFunction::Type GetStencilFunction() const;
268 * Sets the stencil function mask
269 * @param[in] stencilFunctionMask The stencil function mask
271 void SetStencilFunctionMask( int stencilFunctionMask );
274 * Gets the stencil function mask
275 * @return The stencil function mask
277 int GetStencilFunctionMask() const;
280 * Sets the stencil function reference
281 * @param[in] stencilFunctionReference The stencil function reference
283 void SetStencilFunctionReference( int stencilFunctionReference );
286 * Gets the stencil function reference
287 * @return The stencil function reference
289 int GetStencilFunctionReference() const;
292 * Sets the stencil mask
293 * @param[in] stencilMask The stencil mask
295 void SetStencilMask( int stencilMask );
298 * Gets the stencil mask
299 * @return The stencil mask
301 int GetStencilMask() const;
304 * Sets the stencil operation for when the stencil test fails
305 * @param[in] stencilOperationOnFail The stencil operation
307 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
310 * Gets the stencil operation for when the stencil test fails
311 * @return The stencil operation
313 StencilOperation::Type GetStencilOperationOnFail() const;
316 * Sets the stencil operation for when the depth test fails
317 * @param[in] stencilOperationOnZFail The stencil operation
319 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
322 * Gets the stencil operation for when the depth test fails
323 * @return The stencil operation
325 StencilOperation::Type GetStencilOperationOnZFail() const;
328 * Sets the stencil operation for when the depth test passes
329 * @param[in] stencilOperationOnZPass The stencil operation
331 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
334 * Gets the stencil operation for when the depth test passes
335 * @return The stencil operation
337 StencilOperation::Type GetStencilOperationOnZPass() const;
340 * Called to upload during RenderManager::Render().
341 * @param[in] context The context used for uploading
343 void Upload( Context& context );
346 * Called to render during RenderManager::Render().
347 * @param[in] context The context used for rendering
348 * @param[in] bufferIndex The index of the previous update buffer.
349 * @param[in] node The node using this renderer
350 * @param[in] modelViewMatrix The model-view matrix.
351 * @param[in] viewMatrix The view matrix.
352 * @param[in] projectionMatrix The projection matrix.
353 * @param[in] size Size of the render item
354 * @param[in] blend If true, blending is enabled
355 * @param[in] boundTextures The textures bound for rendering
356 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
359 void Render( Context& context,
360 BufferIndex bufferIndex,
361 const SceneGraph::NodeDataProvider& node,
362 const Matrix& modelMatrix,
363 const Matrix& modelViewMatrix,
364 const Matrix& viewMatrix,
365 const Matrix& projectionMatrix,
368 Vector<GLuint>& boundTextures,
369 const Dali::Internal::SceneGraph::RenderInstruction& instruction //for reflection effect
373 * Write the renderer's sort attributes to the passed in reference
375 * @param[in] bufferIndex The current update buffer index.
376 * @param[out] sortAttributes
378 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
381 * Sets the flag indicating whether shader changed.
383 * @param[in] value True if shader changed
385 void SetShaderChanged( bool value );
389 struct UniformIndexMap;
392 Renderer( const Renderer& );
395 Renderer& operator=( const Renderer& rhs );
398 * Sets blending options
399 * @param context to use
400 * @param blend Wheter blending should be enabled or not
402 void SetBlending( Context& context, bool blend );
405 * Set the uniforms from properties according to the uniform map
406 * @param[in] bufferIndex The index of the previous update buffer.
407 * @param[in] node The node using the renderer
408 * @param[in] size The size of the renderer
409 * @param[in] program The shader program on which to set the uniforms.
411 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
414 * Set the program uniform in the map from the mapped property
415 * @param[in] bufferIndex The index of the previous update buffer.
416 * @param[in] program The shader program
417 * @param[in] map The uniform
419 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
422 * Bind the textures and setup the samplers
423 * @param[in] context The GL context
424 * @param[in] program The shader program
425 * @param[in] boundTextures The textures bound for rendering
426 * @return False if create or bind failed, true if success.
428 bool BindTextures( Context& context, Program& program, Vector<GLuint>& boundTextures );
432 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
435 Render::Geometry* mGeometry;
437 struct UniformIndexMap
439 uint32_t uniformIndex; ///< The index of the cached location in the Program
440 const PropertyInputImpl* propertyValue;
443 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
445 UniformIndexMappings mUniformIndexMap;
446 Vector<GLint> mAttributesLocation;
448 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
449 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
451 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
452 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
454 DepthFunction::Type mDepthFunction:4; ///< The depth function
455 FaceCullingMode::Type mFaceCullingMode:3; ///< The mode of face culling
456 DepthWriteMode::Type mDepthWriteMode:3; ///< The depth write mode
457 DepthTestMode::Type mDepthTestMode:3; ///< The depth test mode
458 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
459 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
460 bool mShaderChanged:1; ///< Flag indicating the shader changed and uniform maps have to be updated
465 } // namespace SceneGraph
467 } // namespace Internal
471 #endif // DALI_INTERNAL_RENDER_RENDERER_H