Merge 'remotes/trunk'
[0ad.git] / source / renderer / backend / PipelineState.cpp
bloba606d9fe5bd6e56da2f892269e57853e2a5cb882
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "PipelineState.h"
22 #include <limits>
24 namespace Renderer
27 namespace Backend
30 SGraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc()
32 SGraphicsPipelineStateDesc desc{};
34 desc.shaderProgram = nullptr;
36 desc.depthStencilState.depthTestEnabled = true;
37 desc.depthStencilState.depthCompareOp = CompareOp::LESS_OR_EQUAL;
38 desc.depthStencilState.depthWriteEnabled = true;
39 desc.depthStencilState.stencilTestEnabled = false;
40 desc.depthStencilState.stencilFrontFace.failOp = StencilOp::KEEP;
41 desc.depthStencilState.stencilFrontFace.passOp = StencilOp::KEEP;
42 desc.depthStencilState.stencilFrontFace.depthFailOp = StencilOp::KEEP;
43 desc.depthStencilState.stencilFrontFace.compareOp = CompareOp::ALWAYS;
44 desc.depthStencilState.stencilBackFace = desc.depthStencilState.stencilFrontFace;
45 desc.depthStencilState.stencilReadMask = desc.depthStencilState.stencilWriteMask =
46 std::numeric_limits<uint32_t>::max();
47 desc.depthStencilState.stencilReference = 0;
49 desc.blendState.enabled = false;
50 desc.blendState.srcColorBlendFactor = desc.blendState.srcAlphaBlendFactor =
51 BlendFactor::ONE;
52 desc.blendState.dstColorBlendFactor = desc.blendState.dstAlphaBlendFactor =
53 BlendFactor::ZERO;
54 desc.blendState.colorBlendOp = desc.blendState.alphaBlendOp = BlendOp::ADD;
55 desc.blendState.constant = CColor(0.0f, 0.0f, 0.0f, 0.0f);
56 desc.blendState.colorWriteMask =
57 ColorWriteMask::RED | ColorWriteMask::GREEN | ColorWriteMask::BLUE | ColorWriteMask::ALPHA;
59 desc.rasterizationState.polygonMode = PolygonMode::FILL;
60 desc.rasterizationState.cullMode = CullMode::BACK;
61 desc.rasterizationState.frontFace = FrontFace::COUNTER_CLOCKWISE;
62 desc.rasterizationState.depthBiasEnabled = false;
63 desc.rasterizationState.depthBiasConstantFactor = 0.0f;
64 desc.rasterizationState.depthBiasSlopeFactor = 0.0f;
65 return desc;
68 StencilOp ParseStencilOp(const CStr& str)
70 #define CASE(NAME) if (str == #NAME) return StencilOp::NAME
71 CASE(KEEP);
72 CASE(ZERO);
73 CASE(REPLACE);
74 CASE(INCREMENT_AND_CLAMP);
75 CASE(DECREMENT_AND_CLAMP);
76 CASE(INVERT);
77 CASE(INCREMENT_AND_WRAP);
78 CASE(DECREMENT_AND_WRAP);
79 #undef CASE
80 debug_warn("Invalid stencil op");
81 return StencilOp::KEEP;
84 BlendFactor ParseBlendFactor(const CStr& str)
86 // TODO: it might make sense to use upper case in XML for consistency.
87 #define CASE(NAME, VALUE) if (str == NAME) return BlendFactor::VALUE
88 CASE("zero", ZERO);
89 CASE("one", ONE);
90 CASE("src_color", SRC_COLOR);
91 CASE("one_minus_src_color", ONE_MINUS_SRC_COLOR);
92 CASE("dst_color", DST_COLOR);
93 CASE("one_minus_dst_color", ONE_MINUS_DST_COLOR);
94 CASE("src_alpha", SRC_ALPHA);
95 CASE("one_minus_src_alpha", ONE_MINUS_SRC_ALPHA);
96 CASE("dst_alpha", DST_ALPHA);
97 CASE("one_minus_dst_alpha", ONE_MINUS_DST_ALPHA);
98 CASE("constant_color", CONSTANT_COLOR);
99 CASE("one_minus_constant_color", ONE_MINUS_CONSTANT_COLOR);
100 CASE("constant_alpha", CONSTANT_ALPHA);
101 CASE("one_minus_constant_alpha", ONE_MINUS_CONSTANT_ALPHA);
102 CASE("src_alpha_saturate", SRC_ALPHA_SATURATE);
103 CASE("src1_color", SRC1_COLOR);
104 CASE("one_minus_src1_color", ONE_MINUS_SRC1_COLOR);
105 CASE("src1_alpha", SRC1_ALPHA);
106 CASE("one_minus_src1_alpha", ONE_MINUS_SRC1_ALPHA);
107 #undef CASE
108 debug_warn("Invalid blend factor");
109 return BlendFactor::ZERO;
112 BlendOp ParseBlendOp(const CStr& str)
114 #define CASE(NAME) if (str == #NAME) return BlendOp::NAME
115 CASE(ADD);
116 CASE(SUBTRACT);
117 CASE(REVERSE_SUBTRACT);
118 CASE(MIN);
119 CASE(MAX);
120 #undef CASE
121 debug_warn("Invalid blend op");
122 return BlendOp::ADD;
125 PolygonMode ParsePolygonMode(const CStr& str)
127 if (str == "FILL")
128 return PolygonMode::FILL;
129 else if (str == "LINE")
130 return PolygonMode::LINE;
131 debug_warn("Invalid polygon mode");
132 return PolygonMode::FILL;
135 CullMode ParseCullMode(const CStr& str)
137 if (str == "NONE")
138 return CullMode::NONE;
139 else if (str == "FRONT")
140 return CullMode::FRONT;
141 else if (str == "BACK")
142 return CullMode::BACK;
143 debug_warn("Invalid cull mode");
144 return CullMode::BACK;
147 FrontFace ParseFrontFace(const CStr& str)
149 if (str == "CLOCKWISE")
150 return FrontFace::CLOCKWISE;
151 else if (str == "COUNTER_CLOCKWISE")
152 return FrontFace::COUNTER_CLOCKWISE;
153 debug_warn("Invalid front face");
154 return FrontFace::COUNTER_CLOCKWISE;
157 } // namespace Backend
159 } // namespace Renderer