Debian Codegraph @

Source/Core/VideoBackends/D3D12/DX12Pipeline.cpp

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/D3D12/DX12Pipeline.h"
5
6 #include "Common/Assert.h"
7 #include "Common/MsgHandler.h"
8
9 #include "VideoBackends/D3D12/Common.h"
10 #include "VideoBackends/D3D12/DX12Context.h"
11 #include "VideoBackends/D3D12/DX12Shader.h"
12 #include "VideoBackends/D3D12/DX12Texture.h"
13 #include "VideoBackends/D3D12/DX12VertexFormat.h"
14
15 namespace DX12
16 {
17 DXPipeline::DXPipeline(const AbstractPipelineConfig& config, ID3D12PipelineState* pipeline,
18 ID3D12RootSignature* root_signature, AbstractPipelineUsage usage,
19 D3D12_PRIMITIVE_TOPOLOGY primitive_topology, bool use_integer_rtv)
20 : AbstractPipeline(config), m_pipeline(pipeline), m_root_signature(root_signature),
21 m_usage(usage), m_primitive_topology(primitive_topology), m_use_integer_rtv(use_integer_rtv)
22 {
23 }
24
25 DXPipeline::~DXPipeline()
26 {
27 m_pipeline->Release();
28 }
29
30 static D3D12_PRIMITIVE_TOPOLOGY GetD3DTopology(const RasterizationState& state)
31 {
32 switch (state.primitive)
33 {
34 case PrimitiveType::Points:
35 return D3D_PRIMITIVE_TOPOLOGY_POINTLIST;
36 case PrimitiveType::Lines:
37 return D3D_PRIMITIVE_TOPOLOGY_LINELIST;
38 case PrimitiveType::Triangles:
39 return D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
40 case PrimitiveType::TriangleStrip:
41 default:
42 return D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
43 }
44 }
45
46 static D3D12_PRIMITIVE_TOPOLOGY_TYPE GetD3DTopologyType(const RasterizationState& state)
47 {
48 switch (state.primitive)
49 {
50 case PrimitiveType::Points:
51 return D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT;
52 case PrimitiveType::Lines:
53 return D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE;
54 case PrimitiveType::Triangles:
55 case PrimitiveType::TriangleStrip:
56 default:
57 return D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
58 }
59 }
60
61 static void GetD3DRasterizerDesc(D3D12_RASTERIZER_DESC* desc, const RasterizationState& rs_state,
62 const FramebufferState& fb_state)
63 {
64 // No CULL_ALL here.
65 static constexpr std::array<D3D12_CULL_MODE, 4> cull_modes = {
66 {D3D12_CULL_MODE_NONE, D3D12_CULL_MODE_BACK, D3D12_CULL_MODE_FRONT, D3D12_CULL_MODE_FRONT}};
67
68 desc->FillMode = D3D12_FILL_MODE_SOLID;
69 desc->CullMode = cull_modes[u32(rs_state.cull_mode.Value())];
70 desc->MultisampleEnable = fb_state.samples > 1;
71 }
72
73 static void GetD3DDepthDesc(D3D12_DEPTH_STENCIL_DESC* desc, const DepthState& state)
74 {
75 // Less/greater are swapped due to inverted depth.
76 static constexpr std::array<D3D12_COMPARISON_FUNC, 8> compare_funcs = {
77 {D3D12_COMPARISON_FUNC_NEVER, D3D12_COMPARISON_FUNC_GREATER, D3D12_COMPARISON_FUNC_EQUAL,
78 D3D12_COMPARISON_FUNC_GREATER_EQUAL, D3D12_COMPARISON_FUNC_LESS,
79 D3D12_COMPARISON_FUNC_NOT_EQUAL, D3D12_COMPARISON_FUNC_LESS_EQUAL,
80 D3D12_COMPARISON_FUNC_ALWAYS}};
81
82 desc->DepthEnable = state.test_enable;
83 desc->DepthFunc = compare_funcs[u32(state.func.Value())];
84 desc->DepthWriteMask =
85 state.update_enable ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
86 }
87
88 static void GetD3DBlendDesc(D3D12_BLEND_DESC* desc, const BlendingState& state,
89 u8 render_target_count)
90 {
91 static constexpr std::array<D3D12_BLEND, 8> src_dual_src_factors = {
92 {D3D12_BLEND_ZERO, D3D12_BLEND_ONE, D3D12_BLEND_DEST_COLOR, D3D12_BLEND_INV_DEST_COLOR,
93 D3D12_BLEND_SRC1_ALPHA, D3D12_BLEND_INV_SRC1_ALPHA, D3D12_BLEND_DEST_ALPHA,
94 D3D12_BLEND_INV_DEST_ALPHA}};
95 static constexpr std::array<D3D12_BLEND, 8> dst_dual_src_factors = {
96 {D3D12_BLEND_ZERO, D3D12_BLEND_ONE, D3D12_BLEND_SRC_COLOR, D3D12_BLEND_INV_SRC_COLOR,
97 D3D12_BLEND_SRC1_ALPHA, D3D12_BLEND_INV_SRC1_ALPHA, D3D12_BLEND_DEST_ALPHA,
98 D3D12_BLEND_INV_DEST_ALPHA}};
99 static constexpr std::array<D3D12_BLEND, 8> src_factors = {
100 {D3D12_BLEND_ZERO, D3D12_BLEND_ONE, D3D12_BLEND_DEST_COLOR, D3D12_BLEND_INV_DEST_COLOR,
101 D3D12_BLEND_SRC_ALPHA, D3D12_BLEND_INV_SRC_ALPHA, D3D12_BLEND_DEST_ALPHA,
102 D3D12_BLEND_INV_DEST_ALPHA}};
103
104 static constexpr std::array<D3D12_BLEND, 8> dst_factors = {
105 {D3D12_BLEND_ZERO, D3D12_BLEND_ONE, D3D12_BLEND_SRC_COLOR, D3D12_BLEND_INV_SRC_COLOR,
106 D3D12_BLEND_SRC_ALPHA, D3D12_BLEND_INV_SRC_ALPHA, D3D12_BLEND_DEST_ALPHA,
107 D3D12_BLEND_INV_DEST_ALPHA}};
108
109 static constexpr std::array<D3D12_LOGIC_OP, 16> logic_ops = {
110 {D3D12_LOGIC_OP_CLEAR, D3D12_LOGIC_OP_AND, D3D12_LOGIC_OP_AND_REVERSE, D3D12_LOGIC_OP_COPY,
111 D3D12_LOGIC_OP_AND_INVERTED, D3D12_LOGIC_OP_NOOP, D3D12_LOGIC_OP_XOR, D3D12_LOGIC_OP_OR,
112 D3D12_LOGIC_OP_NOR, D3D12_LOGIC_OP_EQUIV, D3D12_LOGIC_OP_INVERT, D3D12_LOGIC_OP_OR_REVERSE,
113 D3D12_LOGIC_OP_COPY_INVERTED, D3D12_LOGIC_OP_OR_INVERTED, D3D12_LOGIC_OP_NAND,
114 D3D12_LOGIC_OP_SET}};
115
116 desc->AlphaToCoverageEnable = FALSE;
117 desc->IndependentBlendEnable = FALSE;
118
119 for (u8 i = 0; i < render_target_count; i++)
120 {
121 D3D12_RENDER_TARGET_BLEND_DESC* rtblend = &desc->RenderTarget[i];
122 if (state.color_update)
123 {
124 rtblend->RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_RED |
125 D3D12_COLOR_WRITE_ENABLE_GREEN |
126 D3D12_COLOR_WRITE_ENABLE_BLUE;
127 }
128 if (state.alpha_update)
129 {
130 rtblend->RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_ALPHA;
131 }
132
133 // blend takes precedence over logic op
134 rtblend->BlendEnable = state.blend_enable;
135 if (state.blend_enable)
136 {
137 rtblend->BlendOp = state.subtract ? D3D12_BLEND_OP_REV_SUBTRACT : D3D12_BLEND_OP_ADD;
138 rtblend->BlendOpAlpha =
139 state.subtract_alpha ? D3D12_BLEND_OP_REV_SUBTRACT : D3D12_BLEND_OP_ADD;
140 if (state.use_dual_src)
141 {
142 rtblend->SrcBlend = src_dual_src_factors[u32(state.src_factor.Value())];
143 rtblend->SrcBlendAlpha = src_dual_src_factors[u32(state.src_factor_alpha.Value())];
144 rtblend->DestBlend = dst_dual_src_factors[u32(state.dst_factor.Value())];
145 rtblend->DestBlendAlpha = dst_dual_src_factors[u32(state.dst_factor_alpha.Value())];
146 }
147 else
148 {
149 rtblend->SrcBlend = src_factors[u32(state.src_factor.Value())];
150 rtblend->SrcBlendAlpha = src_factors[u32(state.src_factor_alpha.Value())];
151 rtblend->DestBlend = dst_factors[u32(state.dst_factor.Value())];
152 rtblend->DestBlendAlpha = dst_factors[u32(state.dst_factor_alpha.Value())];
153 }
154 }
155 else
156 {
157 rtblend->LogicOpEnable = state.logic_op_enable;
158 if (state.logic_op_enable)
159 rtblend->LogicOp = logic_ops[u32(state.logic_mode.Value())];
160 }
161 }
162 }
163
164 std::unique_ptr<DXPipeline> DXPipeline::Create(const AbstractPipelineConfig& config,
165 const void* cache_data, size_t cache_data_size)
166 {
167 DEBUG_ASSERT(config.vertex_shader && config.pixel_shader);
168
169 D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = {};
170 switch (config.usage)
171 {
172 case AbstractPipelineUsage::GX:
173 case AbstractPipelineUsage::GXUber:
174 desc.pRootSignature = g_dx_context->GetGXRootSignature();
175 break;
176 case AbstractPipelineUsage::Utility:
177 desc.pRootSignature = g_dx_context->GetUtilityRootSignature();
178 break;
179 default:
180 PanicAlertFmt("Unknown pipeline layout.");
181 return nullptr;
182 }
183
184 if (config.vertex_shader)
185 desc.VS = static_cast<const DXShader*>(config.vertex_shader)->GetD3DByteCode();
186 if (config.geometry_shader)
187 desc.GS = static_cast<const DXShader*>(config.geometry_shader)->GetD3DByteCode();
188 if (config.pixel_shader)
189 desc.PS = static_cast<const DXShader*>(config.pixel_shader)->GetD3DByteCode();
190
191 GetD3DBlendDesc(&desc.BlendState, config.blending_state,
192 config.framebuffer_state.additional_color_attachment_count + 1);
193 desc.SampleMask = 0xFFFFFFFF;
194 GetD3DRasterizerDesc(&desc.RasterizerState, config.rasterization_state, config.framebuffer_state);
195 GetD3DDepthDesc(&desc.DepthStencilState, config.depth_state);
196 if (config.vertex_format)
197 static_cast<const DXVertexFormat*>(config.vertex_format)->GetInputLayoutDesc(&desc.InputLayout);
198 desc.IBStripCutValue = config.rasterization_state.primitive == PrimitiveType::TriangleStrip ?
199 D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF :
200 D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED;
201 desc.PrimitiveTopologyType = GetD3DTopologyType(config.rasterization_state);
202 if (config.framebuffer_state.color_texture_format != AbstractTextureFormat::Undefined)
203 {
204 desc.NumRenderTargets =
205 static_cast<u8>(config.framebuffer_state.additional_color_attachment_count) + 1;
206 desc.RTVFormats[0] = D3DCommon::GetRTVFormatForAbstractFormat(
207 config.framebuffer_state.color_texture_format, config.blending_state.logic_op_enable);
208 for (u8 i = 0; i < static_cast<u8>(config.framebuffer_state.additional_color_attachment_count);
209 i++)
210 {
211 // For now set all formats to be the same
212 desc.RTVFormats[i + 1] = desc.RTVFormats[0];
213 }
214 }
215 if (config.framebuffer_state.depth_texture_format != AbstractTextureFormat::Undefined)
216 desc.DSVFormat =
217 D3DCommon::GetDSVFormatForAbstractFormat(config.framebuffer_state.depth_texture_format);
218 desc.SampleDesc.Count = config.framebuffer_state.samples;
219 desc.NodeMask = 1;
220 desc.CachedPSO.pCachedBlob = cache_data;
221 desc.CachedPSO.CachedBlobSizeInBytes = cache_data_size;
222
223 ID3D12PipelineState* pso;
224 HRESULT hr = g_dx_context->GetDevice()->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&pso));
225 if (FAILED(hr))
226 {
227 WARN_LOG_FMT(VIDEO, "CreateGraphicsPipelineState() {}failed: {}",
228 cache_data ? "with cache data " : "", DX12HRWrap(hr));
229 return nullptr;
230 }
231
232 const bool use_integer_rtv =
233 !config.blending_state.blend_enable && config.blending_state.logic_op_enable;
234 return std::make_unique<DXPipeline>(config, pso, desc.pRootSignature, config.usage,
235 GetD3DTopology(config.rasterization_state), use_integer_rtv);
236 }
237
238 AbstractPipeline::CacheData DXPipeline::GetCacheData() const
239 {
240 ComPtr<ID3DBlob> blob;
241 HRESULT hr = m_pipeline->GetCachedBlob(&blob);
242 if (FAILED(hr))
243 {
244 WARN_LOG_FMT(VIDEO, "ID3D12Pipeline::GetCachedBlob() failed: {}", DX12HRWrap(hr));
245 return {};
246 }
247
248 CacheData data(blob->GetBufferSize());
249 std::memcpy(data.data(), blob->GetBufferPointer(), blob->GetBufferSize());
250 return data;
251 }
252 } // namespace DX12