Line data Source code
1 : /* Copyright (C) 2024 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
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.
8 : *
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.
13 : *
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/>.
16 : */
17 :
18 : #ifndef INCLUDED_RENDERER_BACKEND_PIPELINESTATE
19 : #define INCLUDED_RENDERER_BACKEND_PIPELINESTATE
20 :
21 : #include "graphics/Color.h"
22 : #include "ps/CStr.h"
23 : #include "renderer/backend/IDeviceObject.h"
24 :
25 : #include <cstdint>
26 :
27 : namespace Renderer::Backend { class IShaderProgram; }
28 : namespace Renderer::Backend { enum class CompareOp; }
29 :
30 : namespace Renderer
31 : {
32 :
33 : namespace Backend
34 : {
35 :
36 : enum class StencilOp
37 : {
38 : // Keeps the current value.
39 : KEEP,
40 : // Sets the value to zero.
41 : ZERO,
42 : // Sets the value to reference.
43 : REPLACE,
44 : // Increments the value and clamps to the maximum representable unsigned
45 : // value.
46 : INCREMENT_AND_CLAMP,
47 : // Decrements the value and clamps to zero.
48 : DECREMENT_AND_CLAMP,
49 : // Bitwise inverts the value.
50 : INVERT,
51 : // Increments the value and wraps it to zero when incrementing the maximum
52 : // representable unsigned value.
53 : INCREMENT_AND_WRAP,
54 : // Decrements the value and wraps it to the maximum representable unsigned
55 : // value when decrementing zero.
56 : DECREMENT_AND_WRAP
57 : };
58 :
59 : struct SStencilOpState
60 : {
61 : StencilOp failOp;
62 : StencilOp passOp;
63 : StencilOp depthFailOp;
64 : CompareOp compareOp;
65 : };
66 :
67 : struct SDepthStencilStateDesc
68 : {
69 : bool depthTestEnabled;
70 : CompareOp depthCompareOp;
71 : bool depthWriteEnabled;
72 : bool stencilTestEnabled;
73 : uint32_t stencilReadMask;
74 : uint32_t stencilWriteMask;
75 : uint32_t stencilReference;
76 : SStencilOpState stencilFrontFace;
77 : SStencilOpState stencilBackFace;
78 : };
79 :
80 : // TODO: add per constant description.
81 :
82 : enum class BlendFactor
83 : {
84 : ZERO,
85 : ONE,
86 : SRC_COLOR,
87 : ONE_MINUS_SRC_COLOR,
88 : DST_COLOR,
89 : ONE_MINUS_DST_COLOR,
90 : SRC_ALPHA,
91 : ONE_MINUS_SRC_ALPHA,
92 : DST_ALPHA,
93 : ONE_MINUS_DST_ALPHA,
94 : CONSTANT_COLOR,
95 : ONE_MINUS_CONSTANT_COLOR,
96 : CONSTANT_ALPHA,
97 : ONE_MINUS_CONSTANT_ALPHA,
98 : SRC_ALPHA_SATURATE,
99 : SRC1_COLOR,
100 : ONE_MINUS_SRC1_COLOR,
101 : SRC1_ALPHA,
102 : ONE_MINUS_SRC1_ALPHA,
103 : };
104 :
105 : enum class BlendOp
106 : {
107 : ADD,
108 : SUBTRACT,
109 : REVERSE_SUBTRACT,
110 : MIN,
111 : MAX
112 : };
113 :
114 : // Using a namespace instead of a enum allows using the same syntax while
115 : // avoiding adding operator overrides and additional checks on casts.
116 : namespace ColorWriteMask
117 : {
118 : constexpr uint8_t RED = 0x01;
119 : constexpr uint8_t GREEN = 0x02;
120 : constexpr uint8_t BLUE = 0x04;
121 : constexpr uint8_t ALPHA = 0x08;
122 : } // namespace ColorWriteMask
123 :
124 : struct SBlendStateDesc
125 : {
126 : bool enabled;
127 : BlendFactor srcColorBlendFactor;
128 : BlendFactor dstColorBlendFactor;
129 : BlendOp colorBlendOp;
130 : BlendFactor srcAlphaBlendFactor;
131 : BlendFactor dstAlphaBlendFactor;
132 : BlendOp alphaBlendOp;
133 : CColor constant;
134 : uint8_t colorWriteMask;
135 : };
136 :
137 : enum class PolygonMode
138 : {
139 : FILL,
140 : LINE
141 : };
142 :
143 : enum class CullMode
144 : {
145 : NONE,
146 : FRONT,
147 : BACK
148 : };
149 :
150 : enum class FrontFace
151 : {
152 : COUNTER_CLOCKWISE,
153 : CLOCKWISE
154 : };
155 :
156 : struct SRasterizationStateDesc
157 : {
158 : PolygonMode polygonMode;
159 : CullMode cullMode;
160 : FrontFace frontFace;
161 : bool depthBiasEnabled;
162 : float depthBiasConstantFactor;
163 : float depthBiasSlopeFactor;
164 : };
165 :
166 : struct SGraphicsPipelineStateDesc
167 : {
168 : // It's a backend client reponsibility to keep the shader program alive
169 : // while it's bound.
170 : IShaderProgram* shaderProgram;
171 : SDepthStencilStateDesc depthStencilState;
172 : SBlendStateDesc blendState;
173 : SRasterizationStateDesc rasterizationState;
174 : };
175 :
176 : struct SComputePipelineStateDesc
177 : {
178 : // It's a backend client reponsibility to keep the shader program alive
179 : // while it's bound.
180 : IShaderProgram* shaderProgram;
181 : };
182 :
183 : // We don't provide additional helpers intentionally because all custom states
184 : // should be described with a related shader and should be switched together.
185 : SGraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc();
186 :
187 : StencilOp ParseStencilOp(const CStr& str);
188 :
189 : BlendFactor ParseBlendFactor(const CStr& str);
190 : BlendOp ParseBlendOp(const CStr& str);
191 :
192 : PolygonMode ParsePolygonMode(const CStr& str);
193 : CullMode ParseCullMode(const CStr& str);
194 : FrontFace ParseFrontFace(const CStr& str);
195 :
196 : /**
197 : * A holder for precompiled graphics pipeline description.
198 : */
199 0 : class IGraphicsPipelineState : public IDeviceObject<IGraphicsPipelineState>
200 : {
201 : public:
202 : virtual IShaderProgram* GetShaderProgram() const = 0;
203 : };
204 :
205 : /**
206 : * A holder for precompiled compute pipeline description.
207 : */
208 0 : class IComputePipelineState : public IDeviceObject<IComputePipelineState>
209 : {
210 : public:
211 : virtual IShaderProgram* GetShaderProgram() const = 0;
212 : };
213 :
214 : } // namespace Backend
215 :
216 : } // namespace Renderer
217 :
218 : #endif // INCLUDED_RENDERER_BACKEND_PIPELINESTATE
|