Merge 'remotes/trunk'
[0ad.git] / source / renderer / backend / PipelineState.h
blob7cfacfdd999c497267974ad38aa072fff3f52b9e
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 #ifndef INCLUDED_RENDERER_BACKEND_PIPELINESTATE
19 #define INCLUDED_RENDERER_BACKEND_PIPELINESTATE
21 #include "graphics/Color.h"
22 #include "renderer/backend/CompareOp.h"
23 #include "renderer/backend/IDeviceObject.h"
24 #include "renderer/backend/IShaderProgram.h"
26 class CStr;
28 namespace Renderer
31 namespace Backend
34 enum class StencilOp
36 // Keeps the current value.
37 KEEP,
38 // Sets the value to zero.
39 ZERO,
40 // Sets the value to reference.
41 REPLACE,
42 // Increments the value and clamps to the maximum representable unsigned
43 // value.
44 INCREMENT_AND_CLAMP,
45 // Decrements the value and clamps to zero.
46 DECREMENT_AND_CLAMP,
47 // Bitwise inverts the value.
48 INVERT,
49 // Increments the value and wraps it to zero when incrementing the maximum
50 // representable unsigned value.
51 INCREMENT_AND_WRAP,
52 // Decrements the value and wraps it to the maximum representable unsigned
53 // value when decrementing zero.
54 DECREMENT_AND_WRAP
57 struct SStencilOpState
59 StencilOp failOp;
60 StencilOp passOp;
61 StencilOp depthFailOp;
62 CompareOp compareOp;
65 struct SDepthStencilStateDesc
67 bool depthTestEnabled;
68 CompareOp depthCompareOp;
69 bool depthWriteEnabled;
70 bool stencilTestEnabled;
71 uint32_t stencilReadMask;
72 uint32_t stencilWriteMask;
73 uint32_t stencilReference;
74 SStencilOpState stencilFrontFace;
75 SStencilOpState stencilBackFace;
78 // TODO: add per constant description.
80 enum class BlendFactor
82 ZERO,
83 ONE,
84 SRC_COLOR,
85 ONE_MINUS_SRC_COLOR,
86 DST_COLOR,
87 ONE_MINUS_DST_COLOR,
88 SRC_ALPHA,
89 ONE_MINUS_SRC_ALPHA,
90 DST_ALPHA,
91 ONE_MINUS_DST_ALPHA,
92 CONSTANT_COLOR,
93 ONE_MINUS_CONSTANT_COLOR,
94 CONSTANT_ALPHA,
95 ONE_MINUS_CONSTANT_ALPHA,
96 SRC_ALPHA_SATURATE,
97 SRC1_COLOR,
98 ONE_MINUS_SRC1_COLOR,
99 SRC1_ALPHA,
100 ONE_MINUS_SRC1_ALPHA,
103 enum class BlendOp
105 ADD,
106 SUBTRACT,
107 REVERSE_SUBTRACT,
108 MIN,
112 // Using a namespace instead of a enum allows using the same syntax while
113 // avoiding adding operator overrides and additional checks on casts.
114 namespace ColorWriteMask
116 constexpr uint8_t RED = 0x01;
117 constexpr uint8_t GREEN = 0x02;
118 constexpr uint8_t BLUE = 0x04;
119 constexpr uint8_t ALPHA = 0x08;
120 } // namespace ColorWriteMask
122 struct SBlendStateDesc
124 bool enabled;
125 BlendFactor srcColorBlendFactor;
126 BlendFactor dstColorBlendFactor;
127 BlendOp colorBlendOp;
128 BlendFactor srcAlphaBlendFactor;
129 BlendFactor dstAlphaBlendFactor;
130 BlendOp alphaBlendOp;
131 CColor constant;
132 uint8_t colorWriteMask;
135 enum class PolygonMode
137 FILL,
138 LINE
141 enum class CullMode
143 NONE,
144 FRONT,
145 BACK
148 enum class FrontFace
150 COUNTER_CLOCKWISE,
151 CLOCKWISE
154 struct SRasterizationStateDesc
156 PolygonMode polygonMode;
157 CullMode cullMode;
158 FrontFace frontFace;
159 bool depthBiasEnabled;
160 float depthBiasConstantFactor;
161 float depthBiasSlopeFactor;
164 struct SGraphicsPipelineStateDesc
166 // It's a backend client reponsibility to keep the shader program alive
167 // while it's bound.
168 IShaderProgram* shaderProgram;
169 SDepthStencilStateDesc depthStencilState;
170 SBlendStateDesc blendState;
171 SRasterizationStateDesc rasterizationState;
174 // We don't provide additional helpers intentionally because all custom states
175 // should be described with a related shader and should be switched together.
176 SGraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc();
178 StencilOp ParseStencilOp(const CStr& str);
180 BlendFactor ParseBlendFactor(const CStr& str);
181 BlendOp ParseBlendOp(const CStr& str);
183 PolygonMode ParsePolygonMode(const CStr& str);
184 CullMode ParseCullMode(const CStr& str);
185 FrontFace ParseFrontFace(const CStr& str);
188 * A holder for precompiled graphics pipeline description.
190 class IGraphicsPipelineState : public IDeviceObject<IGraphicsPipelineState>
192 public:
193 virtual IShaderProgram* GetShaderProgram() const = 0;
196 } // namespace Backend
198 } // namespace Renderer
200 #endif // INCLUDED_RENDERER_BACKEND_PIPELINESTATE