Change dali-scene-loader to dali-scene3d
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / renderer-state.h
1 #ifndef DALI_SCENE3D_LOADER_RENDERER_STATE_H
2 #define DALI_SCENE3D_LOADER_RENDERER_STATE_H
3 /*
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERAL INCLUDES
21 #include "dali-scene3d/public-api/api.h"
22
23 // EXTERNAL INCLUDES
24 #include "dali/public-api/rendering/renderer.h"
25
26 namespace Dali
27 {
28 namespace Scene3D
29 {
30 namespace Loader
31 {
32 /*
33  * @brief Contains values for comparison functions used in depth and stencil testing.
34  * @note Relative order of members must match DepthFunction::Type and StencilFunction::Type.
35  */
36 struct DALI_SCENE3D_API Comparison
37 {
38   enum Type
39   {
40     OMIT, // not specified; will not be set.
41     NEVER,
42     ALWAYS,
43     LESS,
44     GREATER,
45     EQUAL,
46     NOT_EQUAL,
47     LESS_EQUAL,
48     GREATER_EQUAL
49   };
50
51   Comparison() = delete;
52 };
53
54 /*
55  * @brief Determines the blend factor used.
56  * @note Relative order of members must match BlendFactor::Type.
57  */
58 struct DALI_SCENE3D_API BlendFactor
59 {
60   enum Type
61   {
62     OMIT, // not specified - will not be updated
63     ZERO,
64     ONE, // default for source alpha
65     SRC_COLOR,
66     ONE_MINUS_SRC_COLOR,
67     SRC_ALPHA,           // default for source RGB
68     ONE_MINUS_SRC_ALPHA, // default for destination RGB and destination alpha
69     DST_ALPHA,
70     ONE_MINUS_DST_ALPHA,
71     DST_COLOR,
72     ONE_MINUS_DST_COLOR,
73     SRC_ALPHA_SATURATE,
74     CONSTANT_COLOR,
75     ONE_MINUS_CONSTANT_COLOR,
76     CONSTANT_ALPHA,
77     ONE_MINUS_CONSTANT_ALPHA,
78   };
79
80   BlendFactor() = delete;
81 };
82
83 /*
84  * @brief Determines which buffers shall the Renderer write into.
85  * @note Relative order of members must match RenderMode::Type.
86  */
87 struct DALI_SCENE3D_API BufferMode
88 {
89   enum Type
90   {
91     OMIT,         ///< not specified - will not be updated
92     NONE,         ///< Don’t write to either color or stencil buffer (But will potentially render to depth buffer).
93     AUTO,         ///< Writes are managed by the Actor Clipping API. This is DALi's default.
94     COLOR,        ///< Ignore stencil properties.  Write to the color buffer.
95     STENCIL,      ///< Use the stencil properties. Do not write to the color buffer.
96     COLOR_STENCIL ///< Use the stencil properties AND Write to the color buffer.
97   };
98
99   BufferMode() = delete;
100 };
101
102 /*
103  * @brief Contains values and functionality for configuring Renderers.
104  */
105 namespace RendererState
106 {
107 using Type = uint32_t; // 8 bits reserved for flags, 4 * 4 bit for blend factors, 4 bits for depth function
108
109 enum DALI_SCENE3D_API Value : Type
110 {
111   NONE = 0x0,
112
113   DEPTH_WRITE = 0x01,
114   DEPTH_TEST  = 0x02,
115
116   CULL_FRONT = 0x04,
117   CULL_BACK  = 0x08,
118
119   ALPHA_BLEND = 0x10,
120
121   DEPTH_FUNCTION_SHIFT = 6,
122   DEPTH_FUNCTION_BITS  = 4,
123   DEPTH_FUNCTION_MASK  = ((1 << DEPTH_FUNCTION_BITS) - 1) << DEPTH_FUNCTION_SHIFT,
124
125   BLEND_FACTOR_BASE_SHIFT = DEPTH_FUNCTION_SHIFT + DEPTH_FUNCTION_BITS,
126   BLEND_FACTOR_ITEM_BITS  = 4,
127   BLEND_FACTOR_ITEMS      = 4,
128   BLEND_FACTOR_BITS       = BLEND_FACTOR_ITEM_BITS * BLEND_FACTOR_ITEMS,
129   BLEND_FACTOR_MASK       = ((1 << BLEND_FACTOR_BITS) - 1) << BLEND_FACTOR_BASE_SHIFT,
130   BLEND_FACTOR_ITEM_MASK  = (1 << BLEND_FACTOR_ITEM_BITS) - 1, // after rshifting by BLEND_FACTOR_BASE_SHIFT
131
132   // Buffer mode is DALi's RenderMode, just to avoid too much conflation.
133   BUFFER_MODE_BITS  = 3u,
134   BUFFER_MODE_SHIFT = 32u - BUFFER_MODE_BITS, // from end
135   BUFFER_MODE_MASK  = ((1u << BUFFER_MODE_BITS) - 1u) << BUFFER_MODE_SHIFT,
136
137   DEFAULT = DEPTH_WRITE | DEPTH_TEST | CULL_BACK | (Comparison::LESS_EQUAL << DEPTH_FUNCTION_SHIFT),
138 };
139
140 /*
141  * @brief Encodes the given blend factors into a RenderMode value, maskable into other options,
142  *  passable into ApplyRenderMode().
143  */
144 inline DALI_SCENE3D_API constexpr uint32_t FromBlendFactors(BlendFactor::Type srcRgb, BlendFactor::Type destRgb, BlendFactor::Type srcAlpha, BlendFactor::Type destAlpha)
145 {
146   return (srcRgb | (destRgb << BLEND_FACTOR_ITEM_BITS) | (srcAlpha << (BLEND_FACTOR_ITEM_BITS * 2)) |
147           (destAlpha << (BLEND_FACTOR_ITEM_BITS * 3)))
148          << BLEND_FACTOR_BASE_SHIFT;
149 }
150
151 /*
152  * @brief Applies the settings encoded in @a rendererState, to a @a renderer.
153  * @note Depth function is only set if not Comparison::OMIT.
154  * @note Blend factors are only set if not BlendFactor::OMIT.
155  * @note Buffer mode is only set is not BufferMode::OMIT.
156  */
157 DALI_SCENE3D_API void Apply(Type rendererState, Renderer& renderer);
158
159 } // namespace RendererState
160
161 } // namespace Loader
162 } // namespace Scene3D
163 } // namespace Dali
164
165 #endif //DALI_SCENE3D_LOADER_RENDERER_STATE_H