[dali_2.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / renderer-state.cpp
1 /*
2  * Copyright (c) 2021 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 #include "dali-scene-loader/public-api/renderer-state.h"
18 #include "dali-scene-loader/public-api/utils.h"
19
20 namespace Dali
21 {
22 namespace SceneLoader
23 {
24 namespace
25 {
26 // NOTE: values for BlendFactor aren't contiguous, hence we need a mapping.
27 const Dali::BlendFactor::Type kBlendFactors[] = {
28   Dali::BlendFactor::ZERO,
29   Dali::BlendFactor::ONE,
30   Dali::BlendFactor::SRC_COLOR,
31   Dali::BlendFactor::ONE_MINUS_SRC_COLOR,
32   Dali::BlendFactor::SRC_ALPHA,
33   Dali::BlendFactor::ONE_MINUS_SRC_ALPHA,
34   Dali::BlendFactor::DST_ALPHA,
35   Dali::BlendFactor::ONE_MINUS_DST_ALPHA,
36   Dali::BlendFactor::DST_COLOR,
37   Dali::BlendFactor::ONE_MINUS_DST_COLOR,
38   Dali::BlendFactor::SRC_ALPHA_SATURATE,
39   Dali::BlendFactor::CONSTANT_COLOR,
40   Dali::BlendFactor::ONE_MINUS_CONSTANT_COLOR,
41   Dali::BlendFactor::CONSTANT_ALPHA,
42   Dali::BlendFactor::ONE_MINUS_CONSTANT_ALPHA,
43 };
44 } // namespace
45
46 namespace RendererState
47 {
48 #define RENDERER_SET_PROPERTY(name, value) renderer.SetProperty(Renderer::Property::name, (value))
49
50 void Apply(Type rendererState, Renderer& renderer)
51 {
52   RENDERER_SET_PROPERTY(DEPTH_WRITE_MODE, MaskMatch(rendererState, DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF);
53   RENDERER_SET_PROPERTY(DEPTH_TEST_MODE, MaskMatch(rendererState, DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
54
55   RENDERER_SET_PROPERTY(BLEND_MODE, MaskMatch(rendererState, ALPHA_BLEND) ? BlendMode::ON : BlendMode::OFF);
56
57   const bool cullBack = MaskMatch(rendererState, CULL_BACK);
58   RENDERER_SET_PROPERTY(FACE_CULLING_MODE, MaskMatch(rendererState, CULL_FRONT) ? (cullBack ? FaceCullingMode::FRONT_AND_BACK : FaceCullingMode::FRONT) : (cullBack ? FaceCullingMode::BACK : FaceCullingMode::NONE));
59
60   if(auto depthFunc = (rendererState & DEPTH_FUNCTION_MASK) >> DEPTH_FUNCTION_SHIFT)
61   {
62     RENDERER_SET_PROPERTY(DEPTH_FUNCTION, static_cast<DepthFunction::Type>(depthFunc - 1));
63   }
64
65   if(auto blendFactors = (rendererState & BLEND_FACTOR_MASK) >> BLEND_FACTOR_BASE_SHIFT)
66   {
67     if(auto srcRgb = (blendFactors & BLEND_FACTOR_ITEM_MASK))
68     {
69       RENDERER_SET_PROPERTY(BLEND_FACTOR_SRC_RGB, kBlendFactors[static_cast<BlendFactor::Type>(srcRgb - 1)]);
70     }
71
72     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
73     if(auto dstRgb = (blendFactors & BLEND_FACTOR_ITEM_MASK))
74     {
75       RENDERER_SET_PROPERTY(BLEND_FACTOR_DEST_RGB, kBlendFactors[static_cast<BlendFactor::Type>(dstRgb - 1)]);
76     }
77
78     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
79     if(auto srcAlpha = (blendFactors & BLEND_FACTOR_ITEM_MASK))
80     {
81       RENDERER_SET_PROPERTY(BLEND_FACTOR_SRC_ALPHA, kBlendFactors[static_cast<BlendFactor::Type>(srcAlpha - 1)]);
82     }
83
84     blendFactors >>= BLEND_FACTOR_ITEM_BITS;
85     if(auto dstAlpha = (blendFactors & BLEND_FACTOR_ITEM_MASK))
86     {
87       RENDERER_SET_PROPERTY(BLEND_FACTOR_DEST_ALPHA, kBlendFactors[static_cast<BlendFactor::Type>(dstAlpha - 1)]);
88     }
89   }
90
91   if(auto bufferMode = (rendererState & BUFFER_MODE_MASK) >> BUFFER_MODE_SHIFT)
92   {
93     RENDERER_SET_PROPERTY(RENDER_MODE, static_cast<RenderMode::Type>(bufferMode - 1));
94   }
95 }
96
97 } // namespace RendererState
98
99 } // namespace SceneLoader
100 } // namespace Dali