0.2.0-dev Download ZIP
185 lines ·8.9 KB Raw
| 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 "drivers/d3d12/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 ReflectionBindingSetDataD3D12 { |
| 90 | uint32_t resource_root_param_idx = UINT32_MAX; |
| 91 | uint32_t resource_descriptor_count = 0; |
| 92 | uint32_t sampler_root_param_idx = UINT32_MAX; |
| 93 | uint32_t sampler_descriptor_count = 0; |
| 94 | }; |
| 95 | |
| 96 | struct ReflectionBindingDataD3D12 { |
| 97 | uint32_t resource_class = 0; |
| 98 | uint32_t has_sampler = 0; |
| 99 | uint32_t dxil_stages = 0; |
| 100 | uint32_t resource_descriptor_offset = UINT32_MAX; |
| 101 | uint32_t sampler_descriptor_offset = UINT32_MAX; |
| 102 | uint32_t root_param_idx = UINT32_MAX; // Root descriptor only. |
| 103 | }; |
| 104 | |
| 105 | struct ReflectionSpecializationDataD3D12 { |
| 106 | uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {}; |
| 107 | }; |
| 108 | |
| 109 | protected: |
| 110 | struct ReflectionDataD3D12 { |
| 111 | uint32_t spirv_specialization_constants_ids_mask = 0; |
| 112 | uint32_t dxil_push_constant_stages = 0; |
| 113 | uint32_t nir_runtime_data_root_param_idx = 0; |
| 114 | }; |
| 115 | |
| 116 | struct ContainerFooterD3D12 { |
| 117 | uint32_t root_signature_length = 0; |
| 118 | uint32_t root_signature_crc = 0; |
| 119 | }; |
| 120 | |
| 121 | void *lib_d3d12 = nullptr; |
| 122 | ReflectionDataD3D12 reflection_data_d3d12; |
| 123 | Vector<ReflectionBindingSetDataD3D12> reflection_binding_set_data_d3d12; |
| 124 | Vector<ReflectionBindingDataD3D12> reflection_binding_set_uniforms_data_d3d12; |
| 125 | Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12; |
| 126 | Vector<uint8_t> root_signature_bytes; |
| 127 | uint32_t root_signature_crc = 0; |
| 128 | |
| 129 | #if NIR_ENABLED |
| 130 | bool _convert_spirv_to_nir(Span<ReflectShaderStage> 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); |
| 131 | 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); |
| 132 | bool _convert_spirv_to_dxil(Span<ReflectShaderStage> p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed); |
| 133 | bool _generate_root_signature(BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed); |
| 134 | |
| 135 | // GodotNirCallbacks. |
| 136 | static void _nir_report_resource(uint32_t p_register, uint32_t p_space, uint32_t p_dxil_type, void *p_data); |
| 137 | static void _nir_report_sc_bit_offset(uint32_t p_sc_id, uint64_t p_bit_offset, void *p_data); |
| 138 | static void _nir_report_bitcode_bit_offset(uint64_t p_bit_offset, void *p_data); |
| 139 | #endif |
| 140 | |
| 141 | // RenderingShaderContainer overrides. |
| 142 | virtual uint32_t _format() const override; |
| 143 | virtual uint32_t _format_version() const override; |
| 144 | virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes) override; |
| 145 | virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) override; |
| 146 | virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) override; |
| 147 | virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) override; |
| 148 | virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) override; |
| 149 | virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes) override; |
| 150 | virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const override; |
| 151 | virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override; |
| 152 | virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override; |
| 153 | virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const override; |
| 154 | virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader) override; |
| 155 | virtual bool _set_code_from_spirv(const ReflectShader &p_shader) override; |
| 156 | |
| 157 | public: |
| 158 | struct ShaderReflectionD3D12 { |
| 159 | uint32_t spirv_specialization_constants_ids_mask = 0; |
| 160 | uint32_t dxil_push_constant_stages = 0; |
| 161 | uint32_t nir_runtime_data_root_param_idx = 0; |
| 162 | Vector<ReflectionBindingSetDataD3D12> reflection_binding_sets_d3d12; |
| 163 | Vector<Vector<ReflectionBindingDataD3D12>> reflection_binding_set_uniforms_d3d12; |
| 164 | Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12; |
| 165 | Vector<uint8_t> root_signature_bytes; |
| 166 | uint32_t root_signature_crc = 0; |
| 167 | }; |
| 168 | |
| 169 | RenderingShaderContainerD3D12(); |
| 170 | RenderingShaderContainerD3D12(void *p_lib_d3d12); |
| 171 | ShaderReflectionD3D12 get_shader_reflection_d3d12() const; |
| 172 | }; |
| 173 | |
| 174 | class RenderingShaderContainerFormatD3D12 : public RenderingShaderContainerFormat { |
| 175 | protected: |
| 176 | void *lib_d3d12 = nullptr; |
| 177 | |
| 178 | public: |
| 179 | void set_lib_d3d12(void *p_lib_d3d12); |
| 180 | virtual Ref<RenderingShaderContainer> create_container() const override; |
| 181 | virtual ShaderLanguageVersion get_shader_language_version() const override; |
| 182 | virtual ShaderSpirvVersion get_shader_spirv_version(ShaderStage p_stage) const override; |
| 183 | RenderingShaderContainerFormatD3D12(); |
| 184 | virtual ~RenderingShaderContainerFormatD3D12(); |
| 185 | }; |