[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / renderer-state.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/public-api/loader/renderer-state.h>
20
21 // INTENRAL INCLUDES
22 #include <dali-scene3d/public-api/loader/utils.h>
23
24 namespace Dali::Scene3D::Loader
25 {
26 namespace
27 {
28 // NOTE: values for BlendFactor aren't contiguous, hence we need a mapping.
29 const Dali::BlendFactor::Type kBlendFactors[] = {
30   Dali::BlendFactor::ZERO,
31   Dali::BlendFactor::ONE,
32   Dali::BlendFactor::SRC_COLOR,
33   Dali::BlendFactor::ONE_MINUS_SRC_COLOR,
34   Dali::BlendFactor::SRC_ALPHA,
35   Dali::BlendFactor::ONE_MINUS_SRC_ALPHA,
36   Dali::BlendFactor::DST_ALPHA,
37   Dali::BlendFactor::ONE_MINUS_DST_ALPHA,
38   Dali::BlendFactor::DST_COLOR,
39   Dali::BlendFactor::ONE_MINUS_DST_COLOR,
40   Dali::BlendFactor::SRC_ALPHA_SATURATE,
41   Dali::BlendFactor::CONSTANT_COLOR,
42   Dali::BlendFactor::ONE_MINUS_CONSTANT_COLOR,
43   Dali::BlendFactor::CONSTANT_ALPHA,
44   Dali::BlendFactor::ONE_MINUS_CONSTANT_ALPHA,
45 };
46 } // namespace
47
48 namespace RendererState
49 {
50 #define RENDERER_SET_PROPERTY(name, value) renderer.SetProperty(Renderer::Property::name, (value))
51
52 void Apply(Type rendererState, Renderer& renderer)
53 {
54   RENDERER_SET_PROPERTY(DEPTH_TEST_MODE, MaskMatch(rendererState, DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
55
56   RENDERER_SET_PROPERTY(BLEND_MODE, MaskMatch(rendererState, ALPHA_BLEND) ? BlendMode::ON : BlendMode::USE_ACTOR_OPACITY);
57
58   const bool cullBack = MaskMatch(rendererState, CULL_BACK);
59   RENDERER_SET_PROPERTY(FACE_CULLING_MODE, MaskMatch(rendererState, CULL_FRONT) ? (cullBack ? FaceCullingMode::FRONT_AND_BACK : FaceCullingMode::FRONT) : (cullBack ? FaceCullingMode::BACK : FaceCullingMode::NONE));
60
61   if(auto depthFunc = (rendererState & DEPTH_FUNCTION_MASK) >> DEPTH_FUNCTION_SHIFT)
62   {
63     RENDERER_SET_PROPERTY(DEPTH_FUNCTION, static_cast<DepthFunction::Type>(depthFunc - 1));
64   }
65
66   if(auto blendFactors = (rendererState & BLEND_FACTOR_MASK) >> BLEND_FACTOR_BASE_SHIFT)
67   {
68     if(auto srcRgb = (blendFactors & BLEND_FACTOR_ITEM_MASK))
69     {
70       RENDERER_SET_PROPERTY(BLEND_FACTOR_SRC_RGB, kBlendFactors[static_cast<BlendFactor::Type>(srcRgb - 1)]);
71     }
72
73     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
74     if(auto dstRgb = (blendFactors & BLEND_FACTOR_ITEM_MASK))
75     {
76       RENDERER_SET_PROPERTY(BLEND_FACTOR_DEST_RGB, kBlendFactors[static_cast<BlendFactor::Type>(dstRgb - 1)]);
77     }
78
79     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
80     if(auto srcAlpha = (blendFactors & BLEND_FACTOR_ITEM_MASK))
81     {
82       RENDERER_SET_PROPERTY(BLEND_FACTOR_SRC_ALPHA, kBlendFactors[static_cast<BlendFactor::Type>(srcAlpha - 1)]);
83     }
84
85     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
86     if(auto dstAlpha = (blendFactors & BLEND_FACTOR_ITEM_MASK))
87     {
88       RENDERER_SET_PROPERTY(BLEND_FACTOR_DEST_ALPHA, kBlendFactors[static_cast<BlendFactor::Type>(dstAlpha - 1)]);
89     }
90   }
91
92   if(auto bufferMode = (rendererState & BUFFER_MODE_MASK) >> BUFFER_MODE_SHIFT)
93   {
94     RENDERER_SET_PROPERTY(RENDER_MODE, static_cast<RenderMode::Type>(bufferMode - 1));
95   }
96 }
97
98 } // namespace RendererState
99
100 } // namespace Dali::Scene3D::Loader