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/render-instruction-processor.h>
34 #include <dali/integration-api/debug.h>
47 class SceneController;
50 class NodeDataProvider;
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:3; ///< The render mode
88 StencilFunction::Type stencilFunction:3; ///< The stencil function
89 StencilOperation::Type stencilOperationOnFail:3; ///< The stencil operation for stencil test fail
90 StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
91 StencilOperation::Type stencilOperationOnZPass:3; ///< 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 unsigned int 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 unsigned int 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 data providers of the renderer
154 * @param[in] dataProviders The data providers
156 void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
159 * Change the geometry used by the renderer
160 * @param[in] geometry The new geometry
162 void SetGeometry( Render::Geometry* geometry );
164 * Second-phase construction.
165 * This is called when the renderer is inside render thread
166 * @param[in] context Context used by the renderer
167 * @param[in] textureCache The texture cache to use
169 void Initialize( Context& context, SceneGraph::TextureCache& textureCache );
177 * Set the face-culling mode.
178 * @param[in] mode The face-culling mode.
180 void SetFaceCullingMode( FaceCullingMode::Type mode );
183 * Set the bitmask for blending options
184 * @param[in] bitmask A bitmask of blending options.
186 void SetBlendingBitMask( unsigned int bitmask );
189 * Set the blend color for blending options
190 * @param[in] blendColor The blend color to pass to GL
192 void SetBlendColor( const Vector4* color );
195 * Set the first element index to draw by the indexed draw
196 * @param[in] firstElement index of first element to draw
198 void SetIndexedDrawFirstElement( size_t firstElement );
201 * Set the number of elements to draw by the indexed draw
202 * @param[in] elementsCount number of elements to draw
204 void SetIndexedDrawElementsCount( size_t elementsCount );
207 * @brief Set whether the Pre-multiplied Alpha Blending is required
209 * @param[in] preMultipled whether alpha is pre-multiplied.
211 void EnablePreMultipliedAlpha( bool preMultipled );
214 * Sets the depth write mode
215 * @param[in] depthWriteMode The depth write mode
217 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
220 * Query the Renderer's depth write mode
221 * @return The renderer depth write mode
223 DepthWriteMode::Type GetDepthWriteMode() const;
226 * Sets the depth test mode
227 * @param[in] depthTestMode The depth test mode
229 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
232 * Query the Renderer's depth test mode
233 * @return The renderer depth test mode
235 DepthTestMode::Type GetDepthTestMode() const;
238 * Sets the depth function
239 * @param[in] depthFunction The depth function
241 void SetDepthFunction( DepthFunction::Type depthFunction );
244 * Query the Renderer's depth function
245 * @return The renderer depth function
247 DepthFunction::Type GetDepthFunction() const;
250 * Sets the render mode
251 * @param[in] renderMode The render mode
253 void SetRenderMode( RenderMode::Type mode );
256 * Gets the render mode
257 * @return The render mode
259 RenderMode::Type GetRenderMode() const;
262 * Sets the stencil function
263 * @param[in] stencilFunction The stencil function
265 void SetStencilFunction( StencilFunction::Type stencilFunction );
268 * Gets the stencil function
269 * @return The stencil function
271 StencilFunction::Type GetStencilFunction() const;
274 * Sets the stencil function mask
275 * @param[in] stencilFunctionMask The stencil function mask
277 void SetStencilFunctionMask( int stencilFunctionMask );
280 * Gets the stencil function mask
281 * @return The stencil function mask
283 int GetStencilFunctionMask() const;
286 * Sets the stencil function reference
287 * @param[in] stencilFunctionReference The stencil function reference
289 void SetStencilFunctionReference( int stencilFunctionReference );
292 * Gets the stencil function reference
293 * @return The stencil function reference
295 int GetStencilFunctionReference() const;
298 * Sets the stencil mask
299 * @param[in] stencilMask The stencil mask
301 void SetStencilMask( int stencilMask );
304 * Gets the stencil mask
305 * @return The stencil mask
307 int GetStencilMask() const;
310 * Sets the stencil operation for when the stencil test fails
311 * @param[in] stencilOperationOnFail The stencil operation
313 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
316 * Gets the stencil operation for when the stencil test fails
317 * @return The stencil operation
319 StencilOperation::Type GetStencilOperationOnFail() const;
322 * Sets the stencil operation for when the depth test fails
323 * @param[in] stencilOperationOnZFail The stencil operation
325 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
328 * Gets the stencil operation for when the depth test fails
329 * @return The stencil operation
331 StencilOperation::Type GetStencilOperationOnZFail() const;
334 * Sets the stencil operation for when the depth test passes
335 * @param[in] stencilOperationOnZPass The stencil operation
337 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
340 * Gets the stencil operation for when the depth test passes
341 * @return The stencil operation
343 StencilOperation::Type GetStencilOperationOnZPass() const;
346 * Called to render during RenderManager::Render().
347 * @param[in] context The context used for rendering
348 * @param[in] textureCache The texture cache used to get textures
349 * @param[in] bufferIndex The index of the previous update buffer.
350 * @param[in] node The node using this renderer
351 * @param[in] defaultShader in case there is no custom shader
352 * @param[in] modelViewMatrix The model-view matrix.
353 * @param[in] viewMatrix The view matrix.
354 * @param[in] projectionMatrix The projection matrix.
356 void Render( Context& context,
357 SceneGraph::TextureCache& textureCache,
358 BufferIndex bufferIndex,
359 const SceneGraph::NodeDataProvider& node,
360 SceneGraph::Shader& defaultShader,
361 const Matrix& modelMatrix,
362 const Matrix& modelViewMatrix,
363 const Matrix& viewMatrix,
364 const Matrix& projectionMatrix,
369 * Write the renderer's sort attributes to the passed in reference
371 * @param[in] bufferIndex The current update buffer index.
372 * @param[out] sortAttributes
374 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
378 struct UniformIndexMap;
381 Renderer( const Renderer& );
384 Renderer& operator=( const Renderer& rhs );
387 * Sets blending options
388 * @param context to use
389 * @param blend Wheter blending should be enabled or not
391 void SetBlending( Context& context, bool blend );
394 * Set the uniforms from properties according to the uniform map
395 * @param[in] bufferIndex The index of the previous update buffer.
396 * @param[in] node The node using the renderer
397 * @param[in] size The size of the renderer
398 * @param[in] program The shader program on which to set the uniforms.
400 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
403 * Set the program uniform in the map from the mapped property
404 * @param[in] bufferIndex The index of the previous update buffer.
405 * @param[in] program The shader program
406 * @param[in] map The uniform
408 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
411 * Bind the textures and setup the samplers
412 * @param[in] context The GL context
413 * @param[in] textureCache The texture cache
414 * @param[in] program The shader program
415 * @return False if create or bind failed, true if success.
417 bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
421 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
424 SceneGraph::TextureCache* mTextureCache;
425 Render::Geometry* mGeometry;
427 struct UniformIndexMap
429 unsigned int uniformIndex; ///< The index of the cached location in the Program
430 const PropertyInputImpl* propertyValue;
433 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
435 UniformIndexMappings mUniformIndexMap;
436 Vector<GLint> mAttributesLocation;
438 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
439 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
441 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
442 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
444 DepthFunction::Type mDepthFunction:3; ///< The depth function
445 FaceCullingMode::Type mFaceCullingMode:2; ///< The mode of face culling
446 BlendMode::Type mBlendMode:2; ///< The mode of blending
447 DepthWriteMode::Type mDepthWriteMode:2; ///< The depth write mode
448 DepthTestMode::Type mDepthTestMode:2; ///< The depth test mode
449 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
450 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
454 } // namespace SceneGraph
456 } // namespace Internal
460 #endif // DALI_INTERNAL_RENDER_RENDERER_H