Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/d3d12_builders.h
7469 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "common/types.h"
7
#include "common/windows_headers.h"
8
9
#include "gpu_device.h"
10
11
#include <array>
12
#include <d3d12.h>
13
#include <string_view>
14
#include <wrl/client.h>
15
16
class Error;
17
18
namespace D3D12 {
19
class RootSignatureBuilder
20
{
21
public:
22
enum : u32
23
{
24
MAX_PARAMETERS = 16,
25
MAX_DESCRIPTOR_RANGES = 16,
26
MAX_STATIC_SAMPLERS = 1,
27
};
28
29
RootSignatureBuilder();
30
31
void Clear();
32
33
Microsoft::WRL::ComPtr<ID3D12RootSignature> Create(Error* error, bool clear);
34
35
void SetInputAssemblerFlag();
36
37
u32 Add32BitConstants(u32 shader_reg, u32 num_values, D3D12_SHADER_VISIBILITY visibility);
38
u32 AddCBVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
39
u32 AddSRVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
40
u32 AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE rt, u32 start_shader_reg, u32 num_shader_regs,
41
D3D12_SHADER_VISIBILITY visibility);
42
u32 AddStaticSampler(u32 shader_reg, const D3D12_SAMPLER_DESC& sampler_desc, D3D12_SHADER_VISIBILITY visibility);
43
44
private:
45
D3D12_ROOT_SIGNATURE_DESC m_desc{};
46
std::array<D3D12_ROOT_PARAMETER, MAX_PARAMETERS> m_params{};
47
std::array<D3D12_DESCRIPTOR_RANGE, MAX_DESCRIPTOR_RANGES> m_descriptor_ranges{};
48
std::array<D3D12_STATIC_SAMPLER_DESC, MAX_STATIC_SAMPLERS> m_static_samplers{};
49
u32 m_num_descriptor_ranges = 0;
50
};
51
52
class GraphicsPipelineBuilder
53
{
54
public:
55
enum : u32
56
{
57
MAX_VERTEX_ATTRIBUTES = 16,
58
};
59
60
GraphicsPipelineBuilder();
61
62
~GraphicsPipelineBuilder() = default;
63
64
ALWAYS_INLINE const D3D12_GRAPHICS_PIPELINE_STATE_DESC* GetDesc() const { return &m_desc; }
65
66
void Clear();
67
68
Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, Error* error, bool clear);
69
70
void SetRootSignature(ID3D12RootSignature* rs);
71
72
void SetVertexShader(const void* data, u32 data_size);
73
void SetGeometryShader(const void* data, u32 data_size);
74
void SetPixelShader(const void* data, u32 data_size);
75
76
void SetVertexShader(const ID3DBlob* blob);
77
void SetGeometryShader(const ID3DBlob* blob);
78
void SetPixelShader(const ID3DBlob* blob);
79
80
void AddVertexAttribute(const char* semantic_name, u32 semantic_index, DXGI_FORMAT format, u32 buffer, u32 offset);
81
82
void SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE type);
83
84
void SetRasterizationState(D3D12_FILL_MODE polygon_mode, D3D12_CULL_MODE cull_mode, bool front_face_ccw);
85
86
void SetMultisamples(u32 multisamples);
87
88
void SetDepthState(bool depth_test, bool depth_write, D3D12_COMPARISON_FUNC compare_op);
89
void SetStencilState(bool stencil_test, u8 read_mask, u8 write_mask, const D3D12_DEPTH_STENCILOP_DESC& front,
90
const D3D12_DEPTH_STENCILOP_DESC& back);
91
void SetNoStencilState();
92
93
void SetBlendState(u32 rt, bool blend_enable, D3D12_BLEND src_factor, D3D12_BLEND dst_factor, D3D12_BLEND_OP op,
94
D3D12_BLEND alpha_src_factor, D3D12_BLEND alpha_dst_factor, D3D12_BLEND_OP alpha_op,
95
u8 write_mask = D3D12_COLOR_WRITE_ENABLE_ALL);
96
97
void ClearRenderTargets();
98
99
void SetRenderTarget(u32 rt, DXGI_FORMAT format);
100
101
void ClearDepthStencilFormat();
102
103
void SetDepthStencilFormat(DXGI_FORMAT format);
104
105
private:
106
D3D12_GRAPHICS_PIPELINE_STATE_DESC m_desc;
107
std::array<D3D12_INPUT_ELEMENT_DESC, MAX_VERTEX_ATTRIBUTES> m_input_elements;
108
};
109
110
class ComputePipelineBuilder
111
{
112
public:
113
ComputePipelineBuilder();
114
~ComputePipelineBuilder() = default;
115
116
ALWAYS_INLINE const D3D12_COMPUTE_PIPELINE_STATE_DESC* GetDesc() const { return &m_desc; }
117
118
void Clear();
119
120
Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, Error* error, bool clear);
121
122
void SetRootSignature(ID3D12RootSignature* rs);
123
124
void SetShader(const void* data, u32 data_size);
125
126
private:
127
D3D12_COMPUTE_PIPELINE_STATE_DESC m_desc;
128
};
129
130
#ifdef ENABLE_GPU_OBJECT_NAMES
131
void SetObjectName(ID3D12Object* object, std::string_view name);
132
#else
133
inline void SetObjectName(ID3D12Object* object, std::string_view name)
134
{
135
}
136
#endif
137
} // namespace D3D12
138
139