Blowbox 2017
A 3D Game Engine by Riko Ophorst using DirectX 12
graphics.h
1 #pragma once
2 
3 #include "root_signature.h"
4 
5 namespace blowbox
6 {
7  namespace Graphics
8  {
9  D3D12_STATIC_SAMPLER_DESC sampler_static_point_wrap;
10  D3D12_STATIC_SAMPLER_DESC sampler_static_point_clamp;
11  D3D12_STATIC_SAMPLER_DESC sampler_static_linear_wrap;
12  D3D12_STATIC_SAMPLER_DESC sampler_static_linear_clamp;
13  D3D12_STATIC_SAMPLER_DESC sampler_static_anisotropic_wrap;
14  D3D12_STATIC_SAMPLER_DESC sampler_static_anisotropic_clamp;
15 
16  D3D12_SAMPLER_DESC sampler_point_wrap;
17  D3D12_SAMPLER_DESC sampler_point_clamp;
18  D3D12_SAMPLER_DESC sampler_linear_wrap;
19  D3D12_SAMPLER_DESC sampler_linear_clamp;
20  D3D12_SAMPLER_DESC sampler_anisotropic_wrap;
21  D3D12_SAMPLER_DESC sampler_anisotropic_clamp;
22 
23  D3D12_RASTERIZER_DESC rasterizer_default;
24  D3D12_RASTERIZER_DESC rasterizer_default_cw;
25  D3D12_RASTERIZER_DESC rasterizer_no_culling;
26  D3D12_RASTERIZER_DESC rasterizer_wireframe;
27  D3D12_RASTERIZER_DESC rasterizer_no_culling_wireframe;
28 
29  D3D12_DEPTH_STENCIL_DESC depth_state_default;
30  D3D12_DEPTH_STENCIL_DESC depth_state_disabled;
31  D3D12_DEPTH_STENCIL_DESC depth_state_read_write;
32  D3D12_DEPTH_STENCIL_DESC depth_state_read_only;
33  D3D12_DEPTH_STENCIL_DESC depth_state_read_reversed;
34  D3D12_DEPTH_STENCIL_DESC depth_state_read_test_equal;
35 
36  D3D12_BLEND_DESC blend_state_disabled;
37  D3D12_BLEND_DESC blend_state_no_color_write;
38  D3D12_BLEND_DESC blend_state_pre_multiplied;
39  D3D12_BLEND_DESC blend_state_traditional;
40  D3D12_BLEND_DESC blend_state_additive;
41  D3D12_BLEND_DESC blend_state_traditional_additive;
42 
43  RootSignature root_signature_default;
44 
45  void Initialize()
46  {
47  sampler_static_point_wrap = CD3DX12_STATIC_SAMPLER_DESC(
48  0,
49  D3D12_FILTER_MIN_MAG_MIP_POINT,
50  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
51  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
52  D3D12_TEXTURE_ADDRESS_MODE_WRAP
53  );
54 
55  sampler_static_point_clamp = CD3DX12_STATIC_SAMPLER_DESC(
56  1,
57  D3D12_FILTER_MIN_MAG_MIP_POINT,
58  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
59  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
60  D3D12_TEXTURE_ADDRESS_MODE_CLAMP
61  );
62 
63  sampler_static_linear_wrap = CD3DX12_STATIC_SAMPLER_DESC(
64  2,
65  D3D12_FILTER_MIN_MAG_MIP_LINEAR,
66  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
67  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
68  D3D12_TEXTURE_ADDRESS_MODE_WRAP
69  );
70 
71  sampler_static_linear_clamp = CD3DX12_STATIC_SAMPLER_DESC(
72  3,
73  D3D12_FILTER_MIN_MAG_MIP_LINEAR,
74  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
75  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
76  D3D12_TEXTURE_ADDRESS_MODE_CLAMP
77  );
78 
79  sampler_static_anisotropic_wrap = CD3DX12_STATIC_SAMPLER_DESC(
80  4,
81  D3D12_FILTER_ANISOTROPIC,
82  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
83  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
84  D3D12_TEXTURE_ADDRESS_MODE_WRAP,
85  0.0f,
86  8
87  );
88 
89  sampler_static_anisotropic_clamp = CD3DX12_STATIC_SAMPLER_DESC(
90  5,
91  D3D12_FILTER_ANISOTROPIC,
92  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
93  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
94  D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
95  0.0f,
96  8
97  );
98 
99  sampler_point_wrap.BorderColor[0] = 0.0f;
100  sampler_point_wrap.BorderColor[1] = 0.0f;
101  sampler_point_wrap.BorderColor[2] = 0.0f;
102  sampler_point_wrap.BorderColor[3] = 0.0f;
103  sampler_point_wrap.ComparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
104  sampler_point_wrap.MinLOD = 0.0f;
105  sampler_point_wrap.MaxLOD = D3D12_FLOAT32_MAX;
106  sampler_point_wrap.MipLODBias = 0.0f;
107  sampler_point_wrap.MaxAnisotropy = 8;
108  sampler_point_wrap.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
109  sampler_point_wrap.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
110  sampler_point_wrap.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
111  sampler_point_wrap.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
112 
113  sampler_point_clamp = sampler_point_wrap;
114  sampler_point_clamp.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
115  sampler_point_clamp.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
116  sampler_point_clamp.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
117 
118  sampler_linear_wrap = sampler_point_wrap;
119  sampler_linear_wrap.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
120 
121  sampler_linear_clamp = sampler_point_clamp;
122  sampler_linear_clamp.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
123 
124  sampler_anisotropic_wrap = sampler_point_wrap;
125  sampler_anisotropic_wrap.Filter = D3D12_FILTER_ANISOTROPIC;
126 
127  sampler_anisotropic_clamp = sampler_point_clamp;
128  sampler_anisotropic_clamp.Filter = D3D12_FILTER_ANISOTROPIC;
129 
130  rasterizer_default.FillMode = D3D12_FILL_MODE_SOLID;
131  rasterizer_default.CullMode = D3D12_CULL_MODE_BACK;
132  rasterizer_default.FrontCounterClockwise = TRUE;
133  rasterizer_default.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
134  rasterizer_default.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
135  rasterizer_default.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
136  rasterizer_default.DepthClipEnable = TRUE;
137  rasterizer_default.MultisampleEnable = FALSE;
138  rasterizer_default.AntialiasedLineEnable = FALSE;
139  rasterizer_default.ForcedSampleCount = 0;
140  rasterizer_default.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
141 
142  rasterizer_default_cw = rasterizer_default;
143  rasterizer_default_cw.FrontCounterClockwise = FALSE;
144 
145  rasterizer_no_culling = rasterizer_default;
146  rasterizer_no_culling.CullMode = D3D12_CULL_MODE_NONE;
147  rasterizer_no_culling.DepthClipEnable = FALSE;
148 
149  rasterizer_wireframe = rasterizer_default;
150  rasterizer_wireframe.FillMode = D3D12_FILL_MODE_WIREFRAME;
151 
152  rasterizer_no_culling_wireframe = rasterizer_default;
153  rasterizer_no_culling_wireframe.CullMode = D3D12_CULL_MODE_NONE;
154  rasterizer_no_culling_wireframe.FillMode = D3D12_FILL_MODE_WIREFRAME;
155 
156  depth_state_default.DepthEnable = TRUE;
157  depth_state_default.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
158  depth_state_default.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
159  depth_state_default.StencilEnable = FALSE;
160  depth_state_default.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
161  depth_state_default.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
162 
163  depth_state_disabled.DepthEnable = FALSE;
164  depth_state_disabled.DepthEnable = FALSE;
165  depth_state_disabled.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
166  depth_state_disabled.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
167  depth_state_disabled.StencilEnable = FALSE;
168  depth_state_disabled.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
169  depth_state_disabled.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
170  depth_state_disabled.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
171  depth_state_disabled.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
172  depth_state_disabled.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
173  depth_state_disabled.FrontFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
174  depth_state_disabled.BackFace = depth_state_disabled.FrontFace;
175 
176  depth_state_read_write = depth_state_disabled;
177  depth_state_read_write.DepthEnable = TRUE;
178  depth_state_read_write.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
179  depth_state_read_write.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
180 
181  depth_state_read_only = depth_state_read_write;
182  depth_state_read_only.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
183 
184  D3D12_DEPTH_STENCIL_DESC prepass_depth_state;
185  prepass_depth_state.DepthEnable = TRUE;
186  prepass_depth_state.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
187  prepass_depth_state.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
188  prepass_depth_state.StencilEnable = FALSE;
189  prepass_depth_state.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
190  prepass_depth_state.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
191  prepass_depth_state.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
192  prepass_depth_state.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
193  prepass_depth_state.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
194  prepass_depth_state.FrontFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
195  prepass_depth_state.BackFace = prepass_depth_state.FrontFace;
196 
197  depth_state_read_reversed = depth_state_read_only;
198  depth_state_read_reversed.DepthFunc = D3D12_COMPARISON_FUNC_GREATER_EQUAL;
199 
200  depth_state_read_test_equal = depth_state_read_only;
201  depth_state_read_test_equal.DepthFunc = D3D12_COMPARISON_FUNC_EQUAL;
202 
203  depth_state_default = depth_state_read_write;
204 
205  D3D12_BLEND_DESC alpha_blend = {};
206  alpha_blend.IndependentBlendEnable = FALSE;
207  alpha_blend.RenderTarget[0].BlendEnable = FALSE;
208  alpha_blend.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
209  alpha_blend.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
210  alpha_blend.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
211  alpha_blend.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
212  alpha_blend.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
213  alpha_blend.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
214  alpha_blend.RenderTarget[0].RenderTargetWriteMask = 0;
215  blend_state_no_color_write = alpha_blend;
216 
217  alpha_blend.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
218  blend_state_disabled = alpha_blend;
219 
220  alpha_blend.RenderTarget[0].BlendEnable = TRUE;
221  blend_state_traditional = alpha_blend;
222 
223  alpha_blend.RenderTarget[0].SrcBlend = D3D12_BLEND_ONE;
224  blend_state_pre_multiplied = alpha_blend;
225 
226  alpha_blend.RenderTarget[0].DestBlend = D3D12_BLEND_ONE;
227  blend_state_additive = alpha_blend;
228 
229  alpha_blend.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
230  blend_state_traditional_additive = alpha_blend;
231 
232  root_signature_default.Create(3, 1);
233  root_signature_default[0].InitAsConstantBuffer(0);
234  root_signature_default[1].InitAsConstantBuffer(1);
235  root_signature_default[2].InitAsDescriptorRange(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 2); // diffuse
236  root_signature_default.InitStaticSampler(0, sampler_anisotropic_wrap);
237  root_signature_default.Finalize(L"RootSignatureDefault", D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
238  }
239  }
240 }
The main Blowbox namespace.
Definition: image.cc:8