1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "shader_state_loader_util.h"
17
18 #include <core/namespace.h>
19
20 #include "json_util.h"
21 #include "shader_state_loader.h"
22 #include "util/log.h"
23
24 using namespace BASE_NS;
25 using namespace CORE_NS;
26
27 RENDER_BEGIN_NAMESPACE()
28 // clang-format off
29 CORE_JSON_SERIALIZE_ENUM(PrimitiveTopology,
30 {
31 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, nullptr }, // default
32 { CORE_PRIMITIVE_TOPOLOGY_POINT_LIST, "point_list" },
33 { CORE_PRIMITIVE_TOPOLOGY_LINE_LIST, "line_list" },
34 { CORE_PRIMITIVE_TOPOLOGY_LINE_STRIP, "line_strip" },
35 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, "triangle_list" },
36 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, "triangle_strip" },
37 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN, "triangle_fan" },
38 { CORE_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY, "line_list_with_adjacency" },
39 { CORE_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY, "line_strip_with_adjacency" },
40 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY, "triangle_list_with_adjacency" },
41 { CORE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY, "triangle_strip_with_adjacency" },
42 { CORE_PRIMITIVE_TOPOLOGY_PATCH_LIST, "patch_list" },
43 })
44
45 CORE_JSON_SERIALIZE_ENUM(PolygonMode,
46 {
47 { CORE_POLYGON_MODE_FILL, "fill" },
48 { CORE_POLYGON_MODE_LINE, "line" },
49 { CORE_POLYGON_MODE_POINT, "point" },
50 })
51
52 CORE_JSON_SERIALIZE_ENUM(CullModeFlagBits,
53 {
54 { (CullModeFlagBits)0, nullptr },
55 { CORE_CULL_MODE_NONE, "none" },
56 { CORE_CULL_MODE_FRONT_BIT, "front" },
57 { CORE_CULL_MODE_BACK_BIT, "back" },
58 { CORE_CULL_MODE_FRONT_AND_BACK, "front_and_back" },
59 })
60
61 CORE_JSON_SERIALIZE_ENUM(FrontFace,
62 {
63 { CORE_FRONT_FACE_COUNTER_CLOCKWISE, "counter_clockwise" },
64 { CORE_FRONT_FACE_CLOCKWISE, "clockwise" },
65 })
66
67 CORE_JSON_SERIALIZE_ENUM(CompareOp,
68 {
69 { CORE_COMPARE_OP_NEVER, "never" },
70 { CORE_COMPARE_OP_LESS, "less" },
71 { CORE_COMPARE_OP_EQUAL, "equal" },
72 { CORE_COMPARE_OP_LESS_OR_EQUAL, "less_or_equal" },
73 { CORE_COMPARE_OP_GREATER, "greater" },
74 { CORE_COMPARE_OP_NOT_EQUAL, "not_equal" },
75 { CORE_COMPARE_OP_GREATER_OR_EQUAL, "greater_or_equal" },
76 { CORE_COMPARE_OP_ALWAYS, "always" },
77 })
78
79 CORE_JSON_SERIALIZE_ENUM(StencilOp,
80 {
81 { CORE_STENCIL_OP_KEEP, "keep" },
82 { CORE_STENCIL_OP_ZERO, "zero" },
83 { CORE_STENCIL_OP_REPLACE, "replace" },
84 { CORE_STENCIL_OP_INCREMENT_AND_CLAMP, "increment_and_clamp" },
85 { CORE_STENCIL_OP_DECREMENT_AND_CLAMP, "decrement_and_clamp" },
86 { CORE_STENCIL_OP_INVERT, "invert" },
87 { CORE_STENCIL_OP_INCREMENT_AND_WRAP, "increment_and_wrap" },
88 { CORE_STENCIL_OP_DECREMENT_AND_WRAP, "decrement_and_wrap" },
89 })
90
91 CORE_JSON_SERIALIZE_ENUM(BlendFactor,
92 {
93 { CORE_BLEND_FACTOR_ZERO, "zero" },
94 { CORE_BLEND_FACTOR_ONE, "one" },
95 { CORE_BLEND_FACTOR_SRC_COLOR, "src_color" },
96 { CORE_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, "one_minus_src_color" },
97 { CORE_BLEND_FACTOR_DST_COLOR, "dst_color" },
98 { CORE_BLEND_FACTOR_ONE_MINUS_DST_COLOR, "one_minus_dst_color" },
99 { CORE_BLEND_FACTOR_SRC_ALPHA, "src_alpha" },
100 { CORE_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, "one_minus_src_alpha" },
101 { CORE_BLEND_FACTOR_DST_ALPHA, "dst_alpha" },
102 { CORE_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, "one_minus_dst_alpha" },
103 { CORE_BLEND_FACTOR_CONSTANT_COLOR, "constant_color" },
104 { CORE_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, "one_minus_constant_color" },
105 { CORE_BLEND_FACTOR_CONSTANT_ALPHA, "constant_alpha" },
106 { CORE_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, "one_minus_constant_alpha" },
107 { CORE_BLEND_FACTOR_SRC_ALPHA_SATURATE, "src_alpha_saturate" },
108 { CORE_BLEND_FACTOR_SRC1_COLOR, "src1_color" },
109 { CORE_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, "one_minus_src1_color" },
110 { CORE_BLEND_FACTOR_SRC1_ALPHA, "src1_alpha" },
111 { CORE_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, "one_minus_src1_alpha" },
112 })
113
114 CORE_JSON_SERIALIZE_ENUM(BlendOp,
115 {
116 { CORE_BLEND_OP_ADD, "add" },
117 { CORE_BLEND_OP_SUBTRACT, "subtract" },
118 { CORE_BLEND_OP_REVERSE_SUBTRACT, "reverse_subtract" },
119 { CORE_BLEND_OP_MIN, "min" },
120 { CORE_BLEND_OP_MAX, "max" },
121 })
122
123 CORE_JSON_SERIALIZE_ENUM(ColorComponentFlagBits,
124 {
125 { (ColorComponentFlagBits)0, nullptr },
126 { CORE_COLOR_COMPONENT_R_BIT, "r_bit" },
127 { CORE_COLOR_COMPONENT_G_BIT, "g_bit" },
128 { CORE_COLOR_COMPONENT_B_BIT, "b_bit" },
129 { CORE_COLOR_COMPONENT_A_BIT, "a_bit" },
130 })
131
132 CORE_JSON_SERIALIZE_ENUM(GraphicsStateFlagBits,
133 {
134 { (GraphicsStateFlagBits)0, nullptr },
135 { CORE_GRAPHICS_STATE_INPUT_ASSEMBLY_BIT, "input_assembly_bit" },
136 { CORE_GRAPHICS_STATE_RASTERIZATION_STATE_BIT, "rasterization_state_bit" },
137 { CORE_GRAPHICS_STATE_DEPTH_STENCIL_STATE_BIT, "depth_stencil_state_bit" },
138 { CORE_GRAPHICS_STATE_COLOR_BLEND_STATE_BIT, "color_blend_state_bit" },
139 })
140
141 CORE_JSON_SERIALIZE_ENUM(LogicOp,
142 {
143 { CORE_LOGIC_OP_CLEAR, "clear" },
144 { CORE_LOGIC_OP_AND, "and" },
145 { CORE_LOGIC_OP_AND_REVERSE, "and_reverse" },
146 { CORE_LOGIC_OP_COPY, "copy" },
147 { CORE_LOGIC_OP_AND_INVERTED, "and_inverted" },
148 { CORE_LOGIC_OP_NO_OP, "no_op" },
149 { CORE_LOGIC_OP_XOR, "xor" },
150 { CORE_LOGIC_OP_OR, "or" },
151 { CORE_LOGIC_OP_NOR, "nor" },
152 { CORE_LOGIC_OP_EQUIVALENT, "equivalent" },
153 { CORE_LOGIC_OP_INVERT, "invert" },
154 { CORE_LOGIC_OP_OR_REVERSE, "or_reverse" },
155 { CORE_LOGIC_OP_COPY_INVERTED, "copy_inverted" },
156 { CORE_LOGIC_OP_OR_INVERTED, "or_inverted" },
157 { CORE_LOGIC_OP_NAND, "nand" },
158 { CORE_LOGIC_OP_SET, "set" },
159 })
160 // clang-format on
161 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::InputAssembly>& context)
162 {
163 SafeGetJsonValue(jsonData, "enablePrimitiveRestart", context.error, context.data.enablePrimitiveRestart);
164 SafeGetJsonEnum(jsonData, "primitiveTopology", context.error, context.data.primitiveTopology);
165 }
166
FromJson(const json::value & jsonData,JsonContext<GraphicsState::RasterizationState> & context)167 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::RasterizationState>& context)
168 {
169 SafeGetJsonValue(jsonData, "enableDepthClamp", context.error, context.data.enableDepthClamp);
170 SafeGetJsonValue(jsonData, "enableDepthBias", context.error, context.data.enableDepthBias);
171 SafeGetJsonValue(jsonData, "enableRasterizerDiscard", context.error, context.data.enableRasterizerDiscard);
172
173 SafeGetJsonEnum(jsonData, "polygonMode", context.error, context.data.polygonMode);
174 SafeGetJsonBitfield<CullModeFlagBits>(jsonData, "cullModeFlags", context.error, context.data.cullModeFlags);
175 SafeGetJsonEnum(jsonData, "frontFace", context.error, context.data.frontFace);
176
177 SafeGetJsonValue(jsonData, "depthBiasConstantFactor", context.error, context.data.depthBiasConstantFactor);
178 SafeGetJsonValue(jsonData, "depthBiasClamp", context.error, context.data.depthBiasClamp);
179 SafeGetJsonValue(jsonData, "depthBiasSlopeFactor", context.error, context.data.depthBiasSlopeFactor);
180
181 SafeGetJsonValue(jsonData, "lineWidth", context.error, context.data.lineWidth);
182 }
183
FromJson(const json::value & jsonData,JsonContext<GraphicsState::StencilOpState> & context)184 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::StencilOpState>& context)
185 {
186 SafeGetJsonEnum(jsonData, "failOp", context.error, context.data.failOp);
187 SafeGetJsonEnum(jsonData, "passOp", context.error, context.data.passOp);
188 SafeGetJsonEnum(jsonData, "depthFailOp", context.error, context.data.depthFailOp);
189 SafeGetJsonEnum(jsonData, "compareOp", context.error, context.data.compareOp);
190 SafeGetJsonMask(jsonData, "compareMask", context.error, context.data.compareMask);
191 SafeGetJsonMask(jsonData, "writeMask", context.error, context.data.writeMask);
192 SafeGetJsonMask(jsonData, "reference", context.error, context.data.reference);
193 }
194
FromJson(const json::value & jsonData,JsonContext<GraphicsState::DepthStencilState> & context)195 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::DepthStencilState>& context)
196 {
197 SafeGetJsonValue(jsonData, "enableDepthTest", context.error, context.data.enableDepthTest);
198 SafeGetJsonValue(jsonData, "enableDepthWrite", context.error, context.data.enableDepthWrite);
199 SafeGetJsonValue(jsonData, "enableDepthBoundsTest", context.error, context.data.enableDepthBoundsTest);
200 SafeGetJsonValue(jsonData, "enableStencilTest", context.error, context.data.enableStencilTest);
201
202 SafeGetJsonValue(jsonData, "minDepthBounds", context.error, context.data.minDepthBounds);
203 SafeGetJsonValue(jsonData, "maxDepthBounds", context.error, context.data.maxDepthBounds);
204
205 SafeGetJsonEnum(jsonData, "depthCompareOp", context.error, context.data.depthCompareOp);
206
207 if (const json::value* frontStencilStateIt = jsonData.find("frontStencilOpState"); frontStencilStateIt) {
208 JsonContext<GraphicsState::StencilOpState> stencilContext;
209 FromJson(*frontStencilStateIt, stencilContext);
210 context.data.frontStencilOpState = stencilContext.data;
211 if (!stencilContext.error.empty()) {
212 context.error = stencilContext.error;
213 }
214 }
215
216 if (const json::value* backStencilStateIt = jsonData.find("backStencilOpState"); backStencilStateIt) {
217 JsonContext<GraphicsState::StencilOpState> stencilContext;
218 FromJson(*backStencilStateIt, stencilContext);
219 context.data.backStencilOpState = stencilContext.data;
220 if (!stencilContext.error.empty()) {
221 context.error = stencilContext.error;
222 }
223 }
224 }
225
FromJson(const json::value & jsonData,JsonContext<GraphicsState::ColorBlendState::Attachment> & context)226 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::ColorBlendState::Attachment>& context)
227 {
228 SafeGetJsonValue(jsonData, "enableBlend", context.error, context.data.enableBlend);
229 SafeGetJsonBitfield<ColorComponentFlagBits>(jsonData, "colorWriteMask", context.error, context.data.colorWriteMask);
230
231 SafeGetJsonEnum(jsonData, "srcColorBlendFactor", context.error, context.data.srcColorBlendFactor);
232 SafeGetJsonEnum(jsonData, "dstColorBlendFactor", context.error, context.data.dstColorBlendFactor);
233 SafeGetJsonEnum(jsonData, "colorBlendOp", context.error, context.data.colorBlendOp);
234
235 SafeGetJsonEnum(jsonData, "srcAlphaBlendFactor", context.error, context.data.srcAlphaBlendFactor);
236 SafeGetJsonEnum(jsonData, "dstAlphaBlendFactor", context.error, context.data.dstAlphaBlendFactor);
237 SafeGetJsonEnum(jsonData, "alphaBlendOp", context.error, context.data.alphaBlendOp);
238 }
239
FromJson(const json::value & jsonData,JsonContext<GraphicsState::ColorBlendState> & context)240 void FromJson(const json::value& jsonData, JsonContext<GraphicsState::ColorBlendState>& context)
241 {
242 SafeGetJsonValue(jsonData, "enableLogicOp", context.error, context.data.enableLogicOp);
243 SafeGetJsonEnum(jsonData, "logicOp", context.error, context.data.logicOp);
244
245 if (const json::value* colorBlendConstantsIt = jsonData.find("colorBlendConstants"); colorBlendConstantsIt) {
246 FromJson(*colorBlendConstantsIt, context.data.colorBlendConstants);
247 }
248
249 if (const json::value* colorAttachmentsIt = jsonData.find("colorAttachments"); colorAttachmentsIt) {
250 vector<JsonContext<GraphicsState::ColorBlendState::Attachment>> colorContexts;
251 FromJson(*colorAttachmentsIt, colorContexts);
252 context.data.colorAttachmentCount = 0;
253 for (auto& colorContext : colorContexts) {
254 context.data.colorAttachments[context.data.colorAttachmentCount] = colorContext.data;
255 context.data.colorAttachmentCount++;
256
257 if (!colorContext.error.empty()) {
258 context.error = colorContext.error;
259 }
260 }
261 }
262 }
263
264 namespace ShaderStateLoaderUtil {
ParseStateFlags(const CORE_NS::json::value & jsonData,GraphicsStateFlags & stateFlags,ShaderStateResult & ssr)265 void ParseStateFlags(const CORE_NS::json::value& jsonData, GraphicsStateFlags& stateFlags, ShaderStateResult& ssr)
266 {
267 SafeGetJsonBitfield<GraphicsStateFlagBits>(jsonData, "stateFlags", ssr.res.error, stateFlags);
268 }
269
ParseSingleState(const json::value & jsonData,ShaderStateResult & ssr)270 void ParseSingleState(const json::value& jsonData, ShaderStateResult& ssr)
271 {
272 GraphicsState graphicsState;
273 // Read input assembly.
274 if (const json::value* inputAssemblyIt = jsonData.find("inputAssembly"); inputAssemblyIt) {
275 JsonContext<GraphicsState::InputAssembly> context;
276 FromJson(*inputAssemblyIt, context);
277 graphicsState.inputAssembly = context.data;
278
279 if (!context.error.empty()) {
280 ssr.res.error += context.error;
281 }
282 }
283
284 // Read rasterization state.
285 if (const json::value* rasterizationStateIt = jsonData.find("rasterizationState"); rasterizationStateIt) {
286 JsonContext<GraphicsState::RasterizationState> context;
287 FromJson(*rasterizationStateIt, context);
288 graphicsState.rasterizationState = context.data;
289
290 if (!context.error.empty()) {
291 ssr.res.error += context.error;
292 }
293 }
294
295 // Read depth stencil state
296 if (const json::value* depthStencilStateIt = jsonData.find("depthStencilState"); depthStencilStateIt) {
297 JsonContext<GraphicsState::DepthStencilState> context;
298 FromJson(*depthStencilStateIt, context);
299 graphicsState.depthStencilState = context.data;
300
301 if (!context.error.empty()) {
302 ssr.res.error += context.error;
303 }
304 }
305
306 // Read depth color blend state.
307 if (const json::value* colorBlendStateIt = jsonData.find("colorBlendState"); colorBlendStateIt) {
308 JsonContext<GraphicsState::ColorBlendState> context;
309 FromJson(*colorBlendStateIt, context);
310 graphicsState.colorBlendState = context.data;
311
312 if (!context.error.empty()) {
313 ssr.res.error += context.error;
314 }
315 }
316
317 ssr.res.success = ssr.res.error.empty();
318 if (ssr.res.success) {
319 ssr.states.states.push_back(move(graphicsState));
320 }
321 }
322
LoadStates(const json::value & jsonData)323 ShaderStateResult LoadStates(const json::value& jsonData)
324 {
325 ShaderStateResult ssr;
326 if (const json::value* iter = jsonData.find("shaderStates"); iter) {
327 const auto& allStates = iter->array_;
328 ssr.states.states.reserve(allStates.size());
329 for (auto const& state : allStates) {
330 ShaderStateLoaderVariantData variant;
331 SafeGetJsonValue(state, "variantName", ssr.res.error, variant.variantName);
332 if (variant.variantName.empty()) {
333 ssr.res.error += "graphics state variant name needs to be given\n";
334 }
335 if (ssr.res.error.empty()) {
336 if (const json::value* graphicsStateIt = state.find("state"); graphicsStateIt) {
337 ParseSingleState(*graphicsStateIt, ssr);
338 if (ssr.res.error.empty()) {
339 SafeGetJsonValue(state, "baseShaderState", ssr.res.error, variant.baseShaderState);
340 SafeGetJsonValue(state, "baseVariantName", ssr.res.error, variant.baseVariantName);
341 SafeGetJsonValue(state, "slot", ssr.res.error, variant.renderSlot);
342 SafeGetJsonValue(state, "renderSlot", ssr.res.error, variant.renderSlot);
343 SafeGetJsonValue(
344 state, "renderSlotDefaultShaderState", ssr.res.error, variant.renderSlotDefaultState);
345 SafeGetJsonBitfield<GraphicsStateFlagBits>(
346 state, "stateFlags", ssr.res.error, variant.stateFlags);
347 ssr.states.variantData.push_back(move(variant));
348 }
349 }
350 }
351 }
352 }
353
354 ssr.res.success = ssr.res.error.empty();
355 if (!ssr.res.success) {
356 ssr.res.error += "error loading shader states\n";
357 PLUGIN_LOG_E("error loading shader states: %s", ssr.res.error.c_str());
358 ssr.states.states.clear();
359 }
360 return ssr;
361 }
362 } // namespace ShaderStateLoaderUtil
363 RENDER_END_NAMESPACE()
364