Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/d3d12/rendering_shader_container_d3d12.h
9973 views
1
/**************************************************************************/
2
/* rendering_shader_container_d3d12.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "servers/rendering/rendering_shader_container.h"
34
35
#define NIR_ENABLED 1
36
37
#ifdef SHADER_BAKER_RUNTIME_ENABLED
38
#undef NIR_ENABLED
39
#endif
40
41
#include "d3d12_godot_nir_bridge.h"
42
43
#define D3D12_BITCODE_OFFSETS_NUM_STAGES 3
44
45
#if NIR_ENABLED
46
struct nir_shader;
47
struct nir_shader_compiler_options;
48
#endif
49
50
enum RootSignatureLocationType {
51
RS_LOC_TYPE_RESOURCE,
52
RS_LOC_TYPE_SAMPLER,
53
};
54
55
enum ResourceClass {
56
RES_CLASS_INVALID,
57
RES_CLASS_CBV,
58
RES_CLASS_SRV,
59
RES_CLASS_UAV,
60
};
61
62
struct RenderingDXIL {
63
static uint32_t patch_specialization_constant(
64
RenderingDeviceCommons::PipelineSpecializationConstantType p_type,
65
const void *p_value,
66
const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES],
67
HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_stages_bytecodes,
68
bool p_is_first_patch);
69
70
static void sign_bytecode(RenderingDeviceCommons::ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob);
71
};
72
73
class RenderingShaderContainerD3D12 : public RenderingShaderContainer {
74
GDSOFTCLASS(RenderingShaderContainerD3D12, RenderingShaderContainer);
75
76
public:
77
static constexpr uint32_t REQUIRED_SHADER_MODEL = 0x62; // D3D_SHADER_MODEL_6_2
78
static constexpr uint32_t ROOT_CONSTANT_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RenderingDeviceCommons::MAX_UNIFORM_SETS + 1);
79
static constexpr uint32_t RUNTIME_DATA_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RenderingDeviceCommons::MAX_UNIFORM_SETS + 2);
80
static constexpr uint32_t FORMAT_VERSION = 1;
81
static constexpr uint32_t SHADER_STAGES_BIT_OFFSET_INDICES[RenderingDeviceCommons::SHADER_STAGE_MAX] = {
82
0, // SHADER_STAGE_VERTEX
83
1, // SHADER_STAGE_FRAGMENT
84
UINT32_MAX, // SHADER_STAGE_TESSELATION_CONTROL
85
UINT32_MAX, // SHADER_STAGE_TESSELATION_EVALUATION
86
2, // SHADER_STAGE_COMPUTE
87
};
88
89
struct RootSignatureLocation {
90
uint32_t root_param_index = UINT32_MAX;
91
uint32_t range_index = UINT32_MAX;
92
};
93
94
struct ReflectionBindingDataD3D12 {
95
uint32_t resource_class = 0;
96
uint32_t has_sampler = 0;
97
uint32_t dxil_stages = 0;
98
RootSignatureLocation root_signature_locations[2];
99
};
100
101
struct ReflectionSpecializationDataD3D12 {
102
uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
103
};
104
105
protected:
106
struct ReflectionDataD3D12 {
107
uint32_t spirv_specialization_constants_ids_mask = 0;
108
uint32_t dxil_push_constant_stages = 0;
109
uint32_t nir_runtime_data_root_param_idx = 0;
110
};
111
112
struct ContainerFooterD3D12 {
113
uint32_t root_signature_length = 0;
114
uint32_t root_signature_crc = 0;
115
};
116
117
void *lib_d3d12 = nullptr;
118
ReflectionDataD3D12 reflection_data_d3d12;
119
Vector<ReflectionBindingDataD3D12> reflection_binding_set_uniforms_data_d3d12;
120
Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12;
121
Vector<uint8_t> root_signature_bytes;
122
uint32_t root_signature_crc = 0;
123
124
#if NIR_ENABLED
125
bool _convert_spirv_to_nir(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv, const nir_shader_compiler_options *p_compiler_options, HashMap<int, nir_shader *> &r_stages_nir_shaders, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
126
bool _convert_nir_to_dxil(const HashMap<int, nir_shader *> &p_stages_nir_shaders, BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs);
127
bool _convert_spirv_to_dxil(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
128
bool _generate_root_signature(BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed);
129
130
// GodotNirCallbacks.
131
static void _nir_report_resource(uint32_t p_register, uint32_t p_space, uint32_t p_dxil_type, void *p_data);
132
static void _nir_report_sc_bit_offset(uint32_t p_sc_id, uint64_t p_bit_offset, void *p_data);
133
static void _nir_report_bitcode_bit_offset(uint64_t p_bit_offset, void *p_data);
134
#endif
135
136
// RenderingShaderContainer overrides.
137
virtual uint32_t _format() const override;
138
virtual uint32_t _format_version() const override;
139
virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes) override;
140
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) override;
141
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
142
virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) override;
143
virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
144
virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes) override;
145
virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const override;
146
virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
147
virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
148
virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const override;
149
virtual void _set_from_shader_reflection_post(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection) override;
150
virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) override;
151
152
public:
153
struct ShaderReflectionD3D12 {
154
uint32_t spirv_specialization_constants_ids_mask = 0;
155
uint32_t dxil_push_constant_stages = 0;
156
uint32_t nir_runtime_data_root_param_idx = 0;
157
Vector<Vector<ReflectionBindingDataD3D12>> reflection_binding_set_uniforms_d3d12;
158
Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12;
159
Vector<uint8_t> root_signature_bytes;
160
uint32_t root_signature_crc = 0;
161
};
162
163
RenderingShaderContainerD3D12();
164
RenderingShaderContainerD3D12(void *p_lib_d3d12);
165
ShaderReflectionD3D12 get_shader_reflection_d3d12() const;
166
};
167
168
class RenderingShaderContainerFormatD3D12 : public RenderingShaderContainerFormat {
169
protected:
170
void *lib_d3d12 = nullptr;
171
172
public:
173
void set_lib_d3d12(void *p_lib_d3d12);
174
virtual Ref<RenderingShaderContainer> create_container() const override;
175
virtual ShaderLanguageVersion get_shader_language_version() const override;
176
virtual ShaderSpirvVersion get_shader_spirv_version() const override;
177
RenderingShaderContainerFormatD3D12();
178
virtual ~RenderingShaderContainerFormatD3D12();
179
};
180
181