Line data Source code
1 : /* Copyright (C) 2022 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 "renderer/backend/CompareOp.h"
23 : #include "renderer/backend/IDeviceObject.h"
24 : #include "renderer/backend/IShaderProgram.h"
25 :
26 : class CStr;
27 :
28 : namespace Renderer
29 : {
30 :
31 : namespace Backend
32 : {
33 :
34 : enum class StencilOp
35 : {
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
55 : };
56 :
57 : struct SStencilOpState
58 : {
59 : StencilOp failOp;
60 : StencilOp passOp;
61 : StencilOp depthFailOp;
62 : CompareOp compareOp;
63 : };
64 :
65 : struct SDepthStencilStateDesc
66 : {
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;
76 : };
77 :
78 : // TODO: add per constant description.
79 :
80 : enum class BlendFactor
81 : {
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,
101 : };
102 :
103 : enum class BlendOp
104 : {
105 : ADD,
106 : SUBTRACT,
107 : REVERSE_SUBTRACT,
108 : MIN,
109 : MAX
110 : };
111 :
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
115 : {
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
121 :
122 : struct SBlendStateDesc
123 : {
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;
133 : };
134 :
135 : enum class PolygonMode
136 : {
137 : FILL,
138 : LINE
139 : };
140 :
141 : enum class CullMode
142 : {
143 : NONE,
144 : FRONT,
145 : BACK
146 : };
147 :
148 : enum class FrontFace
149 : {
150 : COUNTER_CLOCKWISE,
151 : CLOCKWISE
152 : };
153 :
154 : struct SRasterizationStateDesc
155 : {
156 : PolygonMode polygonMode;
157 : CullMode cullMode;
158 : FrontFace frontFace;
159 : bool depthBiasEnabled;
160 : float depthBiasConstantFactor;
161 : float depthBiasSlopeFactor;
162 : };
163 :
164 : struct SGraphicsPipelineStateDesc
165 : {
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;
172 : };
173 :
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();
177 :
178 : StencilOp ParseStencilOp(const CStr& str);
179 :
180 : BlendFactor ParseBlendFactor(const CStr& str);
181 : BlendOp ParseBlendOp(const CStr& str);
182 :
183 : PolygonMode ParsePolygonMode(const CStr& str);
184 : CullMode ParseCullMode(const CStr& str);
185 : FrontFace ParseFrontFace(const CStr& str);
186 :
187 : /**
188 : * A holder for precompiled graphics pipeline description.
189 : */
190 0 : class IGraphicsPipelineState : public IDeviceObject<IGraphicsPipelineState>
191 : {
192 : public:
193 : virtual IShaderProgram* GetShaderProgram() const = 0;
194 : };
195 :
196 : } // namespace Backend
197 :
198 : } // namespace Renderer
199 :
200 : #endif // INCLUDED_RENDERER_BACKEND_PIPELINESTATE
|