Use Mipmap level to sample specular texture
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / parse-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/parse-renderer-state.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/map-wrapper.h>
23 #include <cstring>
24
25 namespace Dali::Scene3D::Loader
26 {
27 namespace RendererState
28 {
29 namespace
30 {
31 std::map<std::string_view, Type> COMPARISONS{
32 #define DECL_COMPARISON(x) {#x, Comparison::x}
33   DECL_COMPARISON(NEVER),
34   DECL_COMPARISON(ALWAYS),
35   DECL_COMPARISON(LESS),
36   DECL_COMPARISON(GREATER),
37   DECL_COMPARISON(EQUAL),
38   DECL_COMPARISON(NOT_EQUAL),
39   DECL_COMPARISON(LESS_EQUAL),
40   DECL_COMPARISON(GREATER_EQUAL),
41 #undef DECL_COMPARISON
42 };
43
44 Type InterpretComparison(const std::string_view& str)
45 {
46   Type value = 0x0;
47   auto iFind = COMPARISONS.find(str);
48   if(iFind != COMPARISONS.end())
49   {
50     value = iFind->second;
51   }
52   return value;
53 }
54
55 std::map<std::string_view, Type> BLEND_FACTORS{
56 #define DECL_BLEND_FACTOR(x) {#x, Dali::Scene3D::Loader::BlendFactor::x}
57   DECL_BLEND_FACTOR(ZERO),
58   DECL_BLEND_FACTOR(ONE),
59   DECL_BLEND_FACTOR(SRC_COLOR),
60   DECL_BLEND_FACTOR(ONE_MINUS_SRC_COLOR),
61   DECL_BLEND_FACTOR(SRC_ALPHA),
62   DECL_BLEND_FACTOR(ONE_MINUS_SRC_ALPHA),
63   DECL_BLEND_FACTOR(DST_ALPHA),
64   DECL_BLEND_FACTOR(ONE_MINUS_DST_ALPHA),
65   DECL_BLEND_FACTOR(DST_COLOR),
66   DECL_BLEND_FACTOR(ONE_MINUS_DST_COLOR),
67   DECL_BLEND_FACTOR(SRC_ALPHA_SATURATE),
68   DECL_BLEND_FACTOR(CONSTANT_COLOR),
69   DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_COLOR),
70   DECL_BLEND_FACTOR(CONSTANT_ALPHA),
71   DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_ALPHA),
72 #undef DECL_BLEND_FACTOR
73 };
74
75 Type InterpretBlendFactor(const std::string_view& str, uint8_t item)
76 {
77   Type value = 0x0;
78   auto iFind = BLEND_FACTORS.find(str);
79   if(iFind != BLEND_FACTORS.end())
80   {
81     value = iFind->second << (BLEND_FACTOR_BASE_SHIFT + BLEND_FACTOR_ITEM_BITS * item);
82   }
83   return value;
84 }
85
86 std::map<std::string_view, Type> BUFFER_MODES{
87 #define DECL_BUFFER_MODE(x) {#x, BufferMode::x}
88   DECL_BUFFER_MODE(NONE),
89   DECL_BUFFER_MODE(AUTO),
90   DECL_BUFFER_MODE(COLOR),
91   DECL_BUFFER_MODE(STENCIL),
92   DECL_BUFFER_MODE(COLOR_STENCIL),
93 };
94
95 Type InterpretBufferMode(const std::string_view& str)
96 {
97   Type value = 0x0;
98   auto iFind = BUFFER_MODES.find(str);
99   if(iFind != BUFFER_MODES.end())
100   {
101     value = iFind->second << BUFFER_MODE_SHIFT;
102   }
103   return value;
104 }
105
106 std::map<std::string_view, Type (*)(const std::string_view&)> RENDERER_STATE_PROCESSORS{
107   {"DEPTH_WRITE", [](const std::string_view&) -> Type { return DEPTH_WRITE; }},
108   {"DEPTH_TEST", [](const std::string_view&) -> Type { return DEPTH_TEST; }},
109   {"CULL_FRONT", [](const std::string_view&) -> Type { return CULL_FRONT; }},
110   {"CULL_BACK", [](const std::string_view&) -> Type { return CULL_BACK; }},
111   {"ALPHA_BLEND", [](const std::string_view&) -> Type { return ALPHA_BLEND; }},
112   {"DEPTH_FUNC", [](const std::string_view& arg) -> Type {
113      Type value = (arg[0] == ':') ? (InterpretComparison(std::string_view(arg.data() + 1, arg.size() - 1)) << DEPTH_FUNCTION_SHIFT) : 0x0;
114      return value;
115    }},
116   {"BLEND_SRC_RGB", [](const std::string_view& arg) -> Type {
117      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 0) : 0x0;
118      return value;
119    }},
120   {"BLEND_DST_RGB", [](const std::string_view& arg) -> Type {
121      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 1) : 0x0;
122      return value;
123    }},
124   {"BLEND_SRC_ALPHA", [](const std::string_view& arg) -> Type {
125      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 2) : 0x0;
126      return value;
127    }},
128   {"BLEND_DST_ALPHA", [](const std::string_view& arg) -> Type {
129      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 3) : 0x0;
130      return value;
131    }},
132   {"BUFFER_MODE", [](const std::string_view& arg) -> Type {
133      Type value = (arg[0] == ':') ? InterpretBufferMode(std::string_view(arg.data() + 1, arg.size() - 1)) : 0x0;
134      return value;
135    }},
136 };
137
138 } // namespace
139
140 Type Parse(const char* string, size_t length, StringCallback onError)
141 {
142   if(length == 0)
143   {
144     length = strlen(string);
145   }
146
147   Type value = 0x0;
148   auto iEnd  = string + length;
149   while(string != iEnd)
150   {
151     auto iNextToken = std::find(string, iEnd, '|');
152     auto iColon     = std::find(string, iNextToken, ':');
153     auto i          = RENDERER_STATE_PROCESSORS.find(std::string_view(string, iColon - string));
154     if(i != RENDERER_STATE_PROCESSORS.end() && size_t(std::distance(string, iNextToken)) >= i->first.size())
155     {
156       value |= i->second(std::string_view(string + i->first.size(), iNextToken - iColon));
157     }
158     else
159     {
160       onError("Not a valid RendererState: " + std::string(string, iNextToken));
161     }
162
163     string = iNextToken + (iNextToken != iEnd);
164   }
165
166   return value;
167 }
168
169 } // namespace RendererState
170 } // namespace Dali::Scene3D::Loader