2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-scene3d/public-api/loader/parse-renderer-state.h>
22 #include <dali/devel-api/common/map-wrapper.h>
25 namespace Dali::Scene3D::Loader
27 namespace RendererState
31 const std::map<std::string_view, Type>& GetComparisons()
33 static const std::map<std::string_view, Type> COMPARISONS{
34 #define DECL_COMPARISON(x) {#x, Comparison::x}
35 DECL_COMPARISON(NEVER),
36 DECL_COMPARISON(ALWAYS),
37 DECL_COMPARISON(LESS),
38 DECL_COMPARISON(GREATER),
39 DECL_COMPARISON(EQUAL),
40 DECL_COMPARISON(NOT_EQUAL),
41 DECL_COMPARISON(LESS_EQUAL),
42 DECL_COMPARISON(GREATER_EQUAL),
43 #undef DECL_COMPARISON
48 Type InterpretComparison(const std::string_view& str)
51 auto iFind = GetComparisons().find(str);
52 if(iFind != GetComparisons().end())
54 value = iFind->second;
59 const std::map<std::string_view, Type>& GetBlendFactors()
61 static const std::map<std::string_view, Type> BLEND_FACTORS{
62 #define DECL_BLEND_FACTOR(x) {#x, Dali::Scene3D::Loader::BlendFactor::x}
63 DECL_BLEND_FACTOR(ZERO),
64 DECL_BLEND_FACTOR(ONE),
65 DECL_BLEND_FACTOR(SRC_COLOR),
66 DECL_BLEND_FACTOR(ONE_MINUS_SRC_COLOR),
67 DECL_BLEND_FACTOR(SRC_ALPHA),
68 DECL_BLEND_FACTOR(ONE_MINUS_SRC_ALPHA),
69 DECL_BLEND_FACTOR(DST_ALPHA),
70 DECL_BLEND_FACTOR(ONE_MINUS_DST_ALPHA),
71 DECL_BLEND_FACTOR(DST_COLOR),
72 DECL_BLEND_FACTOR(ONE_MINUS_DST_COLOR),
73 DECL_BLEND_FACTOR(SRC_ALPHA_SATURATE),
74 DECL_BLEND_FACTOR(CONSTANT_COLOR),
75 DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_COLOR),
76 DECL_BLEND_FACTOR(CONSTANT_ALPHA),
77 DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_ALPHA),
78 #undef DECL_BLEND_FACTOR
83 Type InterpretBlendFactor(const std::string_view& str, uint8_t item)
86 auto iFind = GetBlendFactors().find(str);
87 if(iFind != GetBlendFactors().end())
89 value = iFind->second << (BLEND_FACTOR_BASE_SHIFT + BLEND_FACTOR_ITEM_BITS * item);
94 const std::map<std::string_view, Type>& GetBufferModes()
96 static const std::map<std::string_view, Type> BUFFER_MODES{
97 #define DECL_BUFFER_MODE(x) {#x, BufferMode::x}
98 DECL_BUFFER_MODE(NONE),
99 DECL_BUFFER_MODE(AUTO),
100 DECL_BUFFER_MODE(COLOR),
101 DECL_BUFFER_MODE(STENCIL),
102 DECL_BUFFER_MODE(COLOR_STENCIL),
103 #undef DECL_BUFFER_MODE
108 Type InterpretBufferMode(const std::string_view& str)
111 auto iFind = GetBufferModes().find(str);
112 if(iFind != GetBufferModes().end())
114 value = iFind->second << BUFFER_MODE_SHIFT;
119 const std::map<std::string_view, Type (*)(const std::string_view&)>& GetRendererStateProcessors()
121 static const std::map<std::string_view, Type (*)(const std::string_view&)> RENDERER_STATE_PROCESSORS{
122 {"DEPTH_WRITE", [](const std::string_view&) -> Type { return DEPTH_WRITE; }},
123 {"DEPTH_TEST", [](const std::string_view&) -> Type { return DEPTH_TEST; }},
124 {"CULL_FRONT", [](const std::string_view&) -> Type { return CULL_FRONT; }},
125 {"CULL_BACK", [](const std::string_view&) -> Type { return CULL_BACK; }},
126 {"ALPHA_BLEND", [](const std::string_view&) -> Type { return ALPHA_BLEND; }},
127 {"DEPTH_FUNC", [](const std::string_view& arg) -> Type {
128 Type value = (arg[0] == ':') ? (InterpretComparison(std::string_view(arg.data() + 1, arg.size() - 1)) << DEPTH_FUNCTION_SHIFT) : 0x0;
131 {"BLEND_SRC_RGB", [](const std::string_view& arg) -> Type {
132 Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 0) : 0x0;
135 {"BLEND_DST_RGB", [](const std::string_view& arg) -> Type {
136 Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 1) : 0x0;
139 {"BLEND_SRC_ALPHA", [](const std::string_view& arg) -> Type {
140 Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 2) : 0x0;
143 {"BLEND_DST_ALPHA", [](const std::string_view& arg) -> Type {
144 Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 3) : 0x0;
147 {"BUFFER_MODE", [](const std::string_view& arg) -> Type {
148 Type value = (arg[0] == ':') ? InterpretBufferMode(std::string_view(arg.data() + 1, arg.size() - 1)) : 0x0;
152 return RENDERER_STATE_PROCESSORS;
157 Type Parse(const char* string, size_t length, StringCallback onError)
161 length = strlen(string);
165 auto iEnd = string + length;
166 while(string != iEnd)
168 auto iNextToken = std::find(string, iEnd, '|');
169 auto iColon = std::find(string, iNextToken, ':');
170 auto i = GetRendererStateProcessors().find(std::string_view(string, iColon - string));
171 if(i != GetRendererStateProcessors().end() && size_t(std::distance(string, iNextToken)) >= i->first.size())
173 value |= i->second(std::string_view(string + i->first.size(), iNextToken - iColon));
177 onError("Not a valid RendererState: " + std::string(string, iNextToken));
180 string = iNextToken + (iNextToken != iEnd);
186 } // namespace RendererState
187 } // namespace Dali::Scene3D::Loader