| 1 | /**************************************************************************/ |
| 2 | /* rendering_shader_container_d3d12.cpp */ |
| 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 | #include "rendering_shader_container_d3d12.h" |
| 32 | |
| 33 | #include "core/templates/sort_array.h" |
| 34 | |
| 35 | #include "dxil_hash.h" |
| 36 | |
| 37 | #include <zlib.h> |
| 38 | |
| 39 | GODOT_GCC_WARNING_PUSH |
| 40 | GODOT_GCC_WARNING_IGNORE("-Wimplicit-fallthrough") |
| 41 | GODOT_GCC_WARNING_IGNORE("-Wlogical-not-parentheses") |
| 42 | GODOT_GCC_WARNING_IGNORE("-Wmissing-field-initializers") |
| 43 | GODOT_GCC_WARNING_IGNORE("-Wnon-virtual-dtor") |
| 44 | GODOT_GCC_WARNING_IGNORE("-Wshadow") |
| 45 | GODOT_GCC_WARNING_IGNORE("-Wswitch") |
| 46 | GODOT_CLANG_WARNING_PUSH |
| 47 | GODOT_CLANG_WARNING_IGNORE("-Wimplicit-fallthrough") |
| 48 | GODOT_CLANG_WARNING_IGNORE("-Wlogical-not-parentheses") |
| 49 | GODOT_CLANG_WARNING_IGNORE("-Wmissing-field-initializers") |
| 50 | GODOT_CLANG_WARNING_IGNORE("-Wnon-virtual-dtor") |
| 51 | GODOT_CLANG_WARNING_IGNORE("-Wstring-plus-int") |
| 52 | GODOT_CLANG_WARNING_IGNORE("-Wswitch") |
| 53 | GODOT_MSVC_WARNING_PUSH |
| 54 | GODOT_MSVC_WARNING_IGNORE(4200) // "nonstandard extension used: zero-sized array in struct/union". |
| 55 | GODOT_MSVC_WARNING_IGNORE(4806) // "'&': unsafe operation: no value of type 'bool' promoted to type 'uint32_t' can equal the given constant". |
| 56 | |
| 57 | #include <dxgi1_6.h> |
| 58 | #include <thirdparty/directx_headers/include/directx/d3dx12.h> |
| 59 | #define D3D12MA_D3D12_HEADERS_ALREADY_INCLUDED |
| 60 | #include <thirdparty/d3d12ma/D3D12MemAlloc.h> |
| 61 | |
| 62 | #include <wrl/client.h> |
| 63 | |
| 64 | #include <nir_spirv.h> |
| 65 | #include <nir_to_dxil.h> |
| 66 | #include <spirv_to_dxil.h> |
| 67 | extern "C" { |
| 68 | #include <dxil_spirv_nir.h> |
| 69 | |
| 70 | void dxil_reassign_driver_locations(nir_shader *s, nir_variable_mode modes, |
| 71 | uint64_t other_stage_mask, const BITSET_WORD *other_stage_frac_mask); |
| 72 | } |
| 73 | |
| 74 | GODOT_GCC_WARNING_POP |
| 75 | GODOT_CLANG_WARNING_POP |
| 76 | GODOT_MSVC_WARNING_POP |
| 77 | |
| 78 | // SPIR-V to DXIL does way too many allocations, which causes worker threads |
| 79 | // to bottleneck each other due to sharing the same global process heap. |
| 80 | // This can be solved by making each thread allocate from its own heap. |
| 81 | #define SPIRV_TO_DXIL_ENABLE_HEAP_PER_THREAD |
| 82 | |
| 83 | #ifdef SPIRV_TO_DXIL_ENABLE_HEAP_PER_THREAD |
| 84 | |
| 85 | namespace { |
| 86 | struct Win32Heap { |
| 87 | HANDLE handle; |
| 88 | SafeRefCount ref_count; |
| 89 | |
| 90 | Win32Heap() { |
| 91 | handle = HeapCreate(0, 0, 0); |
| 92 | ref_count.init(); |
| 93 | } |
| 94 | |
| 95 | ~Win32Heap() { |
| 96 | HeapDestroy(handle); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | constexpr size_t ALLOC_HEADER_SIZE = sizeof(Win32Heap *) * 2; |
| 101 | } //namespace |
| 102 | |
| 103 | extern "C" { |
| 104 | void *godot_nir_malloc(size_t p_size) { |
| 105 | // This RAII helper is for allowing the heap to be destroyed when the thread quits. |
| 106 | struct Win32HeapHolder { |
| 107 | Win32Heap *win32_heap = nullptr; |
| 108 | |
| 109 | Win32HeapHolder() { |
| 110 | win32_heap = memnew(Win32Heap); |
| 111 | } |
| 112 | |
| 113 | ~Win32HeapHolder() { |
| 114 | if (win32_heap->ref_count.unref()) { |
| 115 | memdelete(win32_heap); |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | thread_local Win32HeapHolder holder; |
| 121 | |
| 122 | void *block = HeapAlloc(holder.win32_heap->handle, 0, p_size + ALLOC_HEADER_SIZE); |
| 123 | |
| 124 | // Store the heap in the allocation for the realloc/free operations. |
| 125 | *(Win32Heap **)block = holder.win32_heap; |
| 126 | holder.win32_heap->ref_count.ref(); |
| 127 | |
| 128 | return (uint8_t *)block + ALLOC_HEADER_SIZE; |
| 129 | } |
| 130 | |
| 131 | void *godot_nir_realloc(void *p_block, size_t p_size) { |
| 132 | uint8_t *actual_block = (uint8_t *)p_block - ALLOC_HEADER_SIZE; |
| 133 | Win32Heap *win32_heap = *(Win32Heap **)actual_block; |
| 134 | return (uint8_t *)HeapReAlloc(win32_heap->handle, 0, actual_block, p_size + ALLOC_HEADER_SIZE) + ALLOC_HEADER_SIZE; |
| 135 | } |
| 136 | |
| 137 | void godot_nir_free(void *p_block) { |
| 138 | if (p_block != nullptr) { |
| 139 | uint8_t *actual_block = (uint8_t *)p_block - ALLOC_HEADER_SIZE; |
| 140 | Win32Heap *win32_heap = *(Win32Heap **)actual_block; |
| 141 | HeapFree(win32_heap->handle, 0, actual_block); |
| 142 | |
| 143 | // Allocations can outlive the threads they were created in if they were stored globally. |
| 144 | if (win32_heap->ref_count.unref()) { |
| 145 | memdelete(win32_heap); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #else |
| 152 | |
| 153 | extern "C" { |
| 154 | void *godot_nir_malloc(size_t p_size) { |
| 155 | return malloc(p_size); |
| 156 | } |
| 157 | |
| 158 | void *godot_nir_realloc(void *p_block, size_t p_size) { |
| 159 | return realloc(p_block, p_size); |
| 160 | } |
| 161 | |
| 162 | void godot_nir_free(void *p_block) { |
| 163 | return free(p_block); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | #endif |
| 168 | |
| 169 | static D3D12_SHADER_VISIBILITY stages_to_d3d12_visibility(uint32_t p_stages_mask) { |
| 170 | switch (p_stages_mask) { |
| 171 | case RenderingDeviceCommons::SHADER_STAGE_VERTEX_BIT: |
| 172 | return D3D12_SHADER_VISIBILITY_VERTEX; |
| 173 | case RenderingDeviceCommons::SHADER_STAGE_FRAGMENT_BIT: |
| 174 | return D3D12_SHADER_VISIBILITY_PIXEL; |
| 175 | default: |
| 176 | return D3D12_SHADER_VISIBILITY_ALL; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | uint32_t RenderingDXIL::patch_specialization_constant( |
| 181 | RenderingDeviceCommons::PipelineSpecializationConstantType p_type, |
| 182 | const void *p_value, |
| 183 | const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES], |
| 184 | HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_stages_bytecodes, |
| 185 | bool p_is_first_patch) { |
| 186 | int64_t patch_val = 0; |
| 187 | switch (p_type) { |
| 188 | case RenderingDeviceCommons::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT: { |
| 189 | patch_val = *((const int32_t *)p_value); |
| 190 | } break; |
| 191 | case RenderingDeviceCommons::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL: { |
| 192 | bool bool_value = *((const bool *)p_value); |
| 193 | patch_val = (int32_t)bool_value; |
| 194 | } break; |
| 195 | case RenderingDeviceCommons::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT: { |
| 196 | patch_val = *((const int32_t *)p_value); |
| 197 | } break; |
| 198 | } |
| 199 | |
| 200 | // Encode to signed VBR. |
| 201 | if (patch_val >= 0) { |
| 202 | patch_val <<= 1; |
| 203 | } else { |
| 204 | patch_val = ((-patch_val) << 1) | 1; |
| 205 | } |
| 206 | |
| 207 | auto tamper_bits = [](uint8_t *p_start, uint64_t p_bit_offset, uint64_t p_tb_value) -> uint64_t { |
| 208 | uint64_t original = 0; |
| 209 | uint32_t curr_input_byte = p_bit_offset / 8; |
| 210 | uint8_t curr_input_bit = p_bit_offset % 8; |
| 211 | auto get_curr_input_bit = [&]() -> bool { |
| 212 | return ((p_start[curr_input_byte] >> curr_input_bit) & 1); |
| 213 | }; |
| 214 | auto move_to_next_input_bit = [&]() { |
| 215 | if (curr_input_bit == 7) { |
| 216 | curr_input_bit = 0; |
| 217 | curr_input_byte++; |
| 218 | } else { |
| 219 | curr_input_bit++; |
| 220 | } |
| 221 | }; |
| 222 | auto tamper_input_bit = [&](bool p_new_bit) { |
| 223 | p_start[curr_input_byte] &= ~((uint8_t)1 << curr_input_bit); |
| 224 | if (p_new_bit) { |
| 225 | p_start[curr_input_byte] |= (uint8_t)1 << curr_input_bit; |
| 226 | } |
| 227 | }; |
| 228 | uint8_t value_bit_idx = 0; |
| 229 | for (uint32_t i = 0; i < 5; i++) { // 32 bits take 5 full bytes in VBR. |
| 230 | for (uint32_t j = 0; j < 7; j++) { |
| 231 | bool input_bit = get_curr_input_bit(); |
| 232 | original |= (uint64_t)(input_bit ? 1 : 0) << value_bit_idx; |
| 233 | tamper_input_bit((p_tb_value >> value_bit_idx) & 1); |
| 234 | move_to_next_input_bit(); |
| 235 | value_bit_idx++; |
| 236 | } |
| 237 | #ifdef DEV_ENABLED |
| 238 | bool input_bit = get_curr_input_bit(); |
| 239 | DEV_ASSERT((i < 4 && input_bit) || (i == 4 && !input_bit)); |
| 240 | #endif |
| 241 | move_to_next_input_bit(); |
| 242 | } |
| 243 | return original; |
| 244 | }; |
| 245 | uint32_t stages_patched_mask = 0; |
| 246 | for (int stage = 0; stage < RenderingDeviceCommons::SHADER_STAGE_MAX; stage++) { |
| 247 | if (!r_stages_bytecodes.has((RenderingDeviceCommons::ShaderStage)stage)) { |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | uint64_t offset = p_stages_bit_offsets[RenderingShaderContainerD3D12::SHADER_STAGES_BIT_OFFSET_INDICES[stage]]; |
| 252 | if (offset == 0) { |
| 253 | // This constant does not appear at this stage. |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | Vector<uint8_t> &bytecode = r_stages_bytecodes[(RenderingDeviceCommons::ShaderStage)stage]; |
| 258 | #ifdef DEV_ENABLED |
| 259 | uint64_t orig_patch_val = tamper_bits(bytecode.ptrw(), offset, (uint64_t)patch_val); |
| 260 | // Checking against the value the NIR patch should have set. |
| 261 | DEV_ASSERT(!p_is_first_patch || ((orig_patch_val >> 1) & GODOT_NIR_SC_SENTINEL_MAGIC_MASK) == GODOT_NIR_SC_SENTINEL_MAGIC); |
| 262 | uint64_t readback_patch_val = tamper_bits(bytecode.ptrw(), offset, (uint64_t)patch_val); |
| 263 | DEV_ASSERT(readback_patch_val == (uint64_t)patch_val); |
| 264 | #else |
| 265 | tamper_bits(bytecode.ptrw(), offset, (uint64_t)patch_val); |
| 266 | #endif |
| 267 | |
| 268 | stages_patched_mask |= (1 << stage); |
| 269 | } |
| 270 | |
| 271 | return stages_patched_mask; |
| 272 | } |
| 273 | |
| 274 | void RenderingDXIL::sign_bytecode(RenderingDeviceCommons::ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob) { |
| 275 | uint8_t *w = r_dxil_blob.ptrw(); |
| 276 | compute_dxil_hash(w + 20, r_dxil_blob.size() - 20, w + 4); |
| 277 | } |
| 278 | |
| 279 | // RenderingShaderContainerD3D12 |
| 280 | |
| 281 | uint32_t RenderingShaderContainerD3D12::_format() const { |
| 282 | return 0x43443344; |
| 283 | } |
| 284 | |
| 285 | uint32_t RenderingShaderContainerD3D12::_format_version() const { |
| 286 | return FORMAT_VERSION; |
| 287 | } |
| 288 | |
| 289 | uint32_t RenderingShaderContainerD3D12::_from_bytes_reflection_extra_data(const uint8_t *p_bytes) { |
| 290 | reflection_data_d3d12 = *(const ReflectionDataD3D12 *)(p_bytes); |
| 291 | reflection_binding_set_data_d3d12.resize(reflection_data.set_count); |
| 292 | for (uint32_t i = 0; i < reflection_binding_set_data_d3d12.size(); i++) { |
| 293 | reflection_binding_set_data_d3d12.ptrw()[i] = *(const ReflectionBindingSetDataD3D12 *)(p_bytes + sizeof(ReflectionDataD3D12) + (i * sizeof(ReflectionBindingSetDataD3D12))); |
| 294 | } |
| 295 | return sizeof(ReflectionDataD3D12) + (reflection_binding_set_data_d3d12.size() * sizeof(ReflectionBindingSetDataD3D12)); |
| 296 | } |
| 297 | |
| 298 | uint32_t RenderingShaderContainerD3D12::_from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) { |
| 299 | reflection_binding_set_uniforms_data_d3d12.resize(reflection_binding_set_uniforms_data.size()); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | uint32_t RenderingShaderContainerD3D12::_from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) { |
| 304 | reflection_binding_set_uniforms_data_d3d12.ptrw()[p_index] = *(const ReflectionBindingDataD3D12 *)(p_bytes); |
| 305 | return sizeof(ReflectionBindingDataD3D12); |
| 306 | } |
| 307 | |
| 308 | uint32_t RenderingShaderContainerD3D12::_from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) { |
| 309 | reflection_specialization_data_d3d12.resize(reflection_specialization_data.size()); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | uint32_t RenderingShaderContainerD3D12::_from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) { |
| 314 | reflection_specialization_data_d3d12.ptrw()[p_index] = *(const ReflectionSpecializationDataD3D12 *)(p_bytes); |
| 315 | return sizeof(ReflectionSpecializationDataD3D12); |
| 316 | } |
| 317 | |
| 318 | uint32_t RenderingShaderContainerD3D12::_from_bytes_footer_extra_data(const uint8_t *p_bytes) { |
| 319 | ContainerFooterD3D12 footer = *(const ContainerFooterD3D12 *)(p_bytes); |
| 320 | root_signature_crc = footer.root_signature_crc; |
| 321 | root_signature_bytes.resize(footer.root_signature_length); |
| 322 | memcpy(root_signature_bytes.ptrw(), p_bytes + sizeof(ContainerFooterD3D12), root_signature_bytes.size()); |
| 323 | return sizeof(ContainerFooterD3D12) + footer.root_signature_length; |
| 324 | } |
| 325 | |
| 326 | uint32_t RenderingShaderContainerD3D12::_to_bytes_reflection_extra_data(uint8_t *p_bytes) const { |
| 327 | if (p_bytes != nullptr) { |
| 328 | *(ReflectionDataD3D12 *)(p_bytes) = reflection_data_d3d12; |
| 329 | for (uint32_t i = 0; i < reflection_binding_set_data_d3d12.size(); i++) { |
| 330 | *(ReflectionBindingSetDataD3D12 *)(p_bytes + sizeof(ReflectionDataD3D12) + (i * sizeof(ReflectionBindingSetDataD3D12))) = reflection_binding_set_data_d3d12[i]; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return sizeof(ReflectionDataD3D12) + (reflection_binding_set_data_d3d12.size() * sizeof(ReflectionBindingSetDataD3D12)); |
| 335 | } |
| 336 | |
| 337 | uint32_t RenderingShaderContainerD3D12::_to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const { |
| 338 | if (p_bytes != nullptr) { |
| 339 | *(ReflectionBindingDataD3D12 *)(p_bytes) = reflection_binding_set_uniforms_data_d3d12[p_index]; |
| 340 | } |
| 341 | |
| 342 | return sizeof(ReflectionBindingDataD3D12); |
| 343 | } |
| 344 | |
| 345 | uint32_t RenderingShaderContainerD3D12::_to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const { |
| 346 | if (p_bytes != nullptr) { |
| 347 | *(ReflectionSpecializationDataD3D12 *)(p_bytes) = reflection_specialization_data_d3d12[p_index]; |
| 348 | } |
| 349 | |
| 350 | return sizeof(ReflectionSpecializationDataD3D12); |
| 351 | } |
| 352 | |
| 353 | uint32_t RenderingShaderContainerD3D12::_to_bytes_footer_extra_data(uint8_t *p_bytes) const { |
| 354 | if (p_bytes != nullptr) { |
| 355 | ContainerFooterD3D12 &footer = *(ContainerFooterD3D12 *)(p_bytes); |
| 356 | footer.root_signature_length = root_signature_bytes.size(); |
| 357 | footer.root_signature_crc = root_signature_crc; |
| 358 | memcpy(p_bytes + sizeof(ContainerFooterD3D12), root_signature_bytes.ptr(), root_signature_bytes.size()); |
| 359 | } |
| 360 | |
| 361 | return sizeof(ContainerFooterD3D12) + root_signature_bytes.size(); |
| 362 | } |
| 363 | |
| 364 | #if NIR_ENABLED |
| 365 | bool RenderingShaderContainerD3D12::_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) { |
| 366 | r_stages_processed.clear(); |
| 367 | |
| 368 | dxil_spirv_runtime_conf dxil_runtime_conf = {}; |
| 369 | dxil_runtime_conf.runtime_data_cbv.base_shader_register = RUNTIME_DATA_REGISTER; |
| 370 | dxil_runtime_conf.push_constant_cbv.base_shader_register = ROOT_CONSTANT_REGISTER; |
| 371 | dxil_runtime_conf.first_vertex_and_base_instance_mode = DXIL_SPIRV_SYSVAL_TYPE_ZERO; |
| 372 | dxil_runtime_conf.workgroup_id_mode = DXIL_SPIRV_SYSVAL_TYPE_ZERO; |
| 373 | |
| 374 | // Explicitly keeping these false because converting UAV descriptors to SRVs do not seem to have real performance benefits on desktop GPUs. |
| 375 | // It also makes it easier to implement descriptor heaps and enhanced barriers. |
| 376 | dxil_runtime_conf.declared_read_only_images_as_srvs = false; |
| 377 | dxil_runtime_conf.inferred_read_only_images_as_srvs = false; |
| 378 | |
| 379 | // Translate SPIR-V to NIR. |
| 380 | for (uint64_t i = 0; i < p_spirv.size(); i++) { |
| 381 | RenderingDeviceCommons::ShaderStage stage = p_spirv[i].shader_stage; |
| 382 | RenderingDeviceCommons::ShaderStage stage_flag = (RenderingDeviceCommons::ShaderStage)(1 << stage); |
| 383 | r_stages.push_back(stage); |
| 384 | r_stages_processed.set_flag(stage_flag); |
| 385 | |
| 386 | const char *entry_point = "main"; |
| 387 | static const mesa_shader_stage SPIRV_TO_MESA_STAGES[RenderingDeviceCommons::SHADER_STAGE_MAX] = { |
| 388 | MESA_SHADER_VERTEX, // SHADER_STAGE_VERTEX |
| 389 | MESA_SHADER_FRAGMENT, // SHADER_STAGE_FRAGMENT |
| 390 | MESA_SHADER_TESS_CTRL, // SHADER_STAGE_TESSELATION_CONTROL |
| 391 | MESA_SHADER_TESS_EVAL, // SHADER_STAGE_TESSELATION_EVALUATION |
| 392 | MESA_SHADER_COMPUTE, // SHADER_STAGE_COMPUTE |
| 393 | }; |
| 394 | |
| 395 | Span<uint32_t> code = p_spirv[i].spirv(); |
| 396 | nir_shader *shader = spirv_to_nir( |
| 397 | code.ptr(), |
| 398 | code.size(), |
| 399 | nullptr, |
| 400 | 0, |
| 401 | SPIRV_TO_MESA_STAGES[stage], |
| 402 | entry_point, |
| 403 | dxil_spirv_nir_get_spirv_options(), |
| 404 | p_compiler_options); |
| 405 | |
| 406 | ERR_FAIL_NULL_V_MSG(shader, false, "Shader translation (step 1) at stage " + String(RenderingDeviceCommons::SHADER_STAGE_NAMES[stage]) + " failed."); |
| 407 | |
| 408 | if (stage == RenderingDeviceCommons::SHADER_STAGE_VERTEX) { |
| 409 | dxil_runtime_conf.yz_flip.y_mask = 0xffff; |
| 410 | dxil_runtime_conf.yz_flip.mode = DXIL_SPIRV_Y_FLIP_UNCONDITIONAL; |
| 411 | } else { |
| 412 | dxil_runtime_conf.yz_flip.y_mask = 0; |
| 413 | dxil_runtime_conf.yz_flip.mode = DXIL_SPIRV_YZ_FLIP_NONE; |
| 414 | } |
| 415 | |
| 416 | dxil_spirv_nir_prep(shader); |
| 417 | dxil_spirv_metadata dxil_metadata = {}; |
| 418 | dxil_spirv_nir_passes(shader, &dxil_runtime_conf, &dxil_metadata); |
| 419 | |
| 420 | r_stages_nir_shaders[stage] = shader; |
| 421 | } |
| 422 | |
| 423 | // Link NIR shaders. |
| 424 | for (int i = RenderingDeviceCommons::SHADER_STAGE_MAX - 1; i >= 0; i--) { |
| 425 | if (!r_stages_nir_shaders.has(i)) { |
| 426 | continue; |
| 427 | } |
| 428 | nir_shader *shader = r_stages_nir_shaders[i]; |
| 429 | nir_shader *prev_shader = nullptr; |
| 430 | for (int j = i - 1; j >= 0; j--) { |
| 431 | if (r_stages_nir_shaders.has(j)) { |
| 432 | prev_shader = r_stages_nir_shaders[j]; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | if (prev_shader) { |
| 437 | dxil_spirv_metadata dxil_metadata = {}; |
| 438 | dxil_spirv_nir_link(shader, prev_shader, &dxil_runtime_conf, &dxil_metadata); |
| 439 | } |
| 440 | // There is a bug in the Direct3D runtime during creation of a PSO with view instancing. If a fragment |
| 441 | // shader uses front/back face detection (SV_IsFrontFace), its signature must include the pixel position |
| 442 | // builtin variable (SV_Position), otherwise an Internal Runtime error will occur. |
| 443 | if (i == RenderingDeviceCommons::SHADER_STAGE_FRAGMENT) { |
| 444 | const bool use_front_face = |
| 445 | nir_find_variable_with_location(shader, nir_var_shader_in, VARYING_SLOT_FACE) || |
| 446 | (shader->info.inputs_read & VARYING_BIT_FACE) || |
| 447 | nir_find_variable_with_location(shader, nir_var_system_value, SYSTEM_VALUE_FRONT_FACE) || |
| 448 | BITSET_TEST(shader->info.system_values_read, SYSTEM_VALUE_FRONT_FACE); |
| 449 | const bool use_position = |
| 450 | nir_find_variable_with_location(shader, nir_var_shader_in, VARYING_SLOT_POS) || |
| 451 | (shader->info.inputs_read & VARYING_BIT_POS) || |
| 452 | nir_find_variable_with_location(shader, nir_var_system_value, SYSTEM_VALUE_FRAG_COORD) || |
| 453 | BITSET_TEST(shader->info.system_values_read, SYSTEM_VALUE_FRAG_COORD); |
| 454 | if (use_front_face && !use_position) { |
| 455 | nir_variable *const pos = nir_variable_create(shader, nir_var_shader_in, glsl_vec4_type(), "gl_FragCoord"); |
| 456 | pos->data.location = VARYING_SLOT_POS; |
| 457 | shader->info.inputs_read |= VARYING_BIT_POS; |
| 458 | |
| 459 | if (prev_shader) { |
| 460 | dxil_reassign_driver_locations(shader, nir_var_shader_in, prev_shader->info.outputs_written, NULL); |
| 461 | dxil_reassign_driver_locations(prev_shader, nir_var_shader_out, shader->info.inputs_read, NULL); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | struct GodotNirCallbackUserData { |
| 471 | RenderingShaderContainerD3D12 *container; |
| 472 | RenderingDeviceCommons::ShaderStage stage; |
| 473 | }; |
| 474 | |
| 475 | static dxil_shader_model shader_model_d3d_to_dxil(D3D_SHADER_MODEL p_d3d_shader_model) { |
| 476 | static_assert(SHADER_MODEL_6_0 == 0x60000); |
| 477 | static_assert(SHADER_MODEL_6_3 == 0x60003); |
| 478 | static_assert(D3D_SHADER_MODEL_6_0 == 0x60); |
| 479 | static_assert(D3D_SHADER_MODEL_6_3 == 0x63); |
| 480 | return (dxil_shader_model)((p_d3d_shader_model >> 4) * 0x10000 + (p_d3d_shader_model & 0xf)); |
| 481 | } |
| 482 | |
| 483 | bool RenderingShaderContainerD3D12::_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) { |
| 484 | // Translate NIR to DXIL. |
| 485 | for (KeyValue<int, nir_shader *> it : p_stages_nir_shaders) { |
| 486 | RenderingDeviceCommons::ShaderStage stage = (RenderingDeviceCommons::ShaderStage)(it.key); |
| 487 | GodotNirCallbackUserData godot_nir_callback_user_data; |
| 488 | godot_nir_callback_user_data.container = this; |
| 489 | godot_nir_callback_user_data.stage = stage; |
| 490 | |
| 491 | GodotNirCallbacks godot_nir_callbacks = {}; |
| 492 | godot_nir_callbacks.data = &godot_nir_callback_user_data; |
| 493 | godot_nir_callbacks.report_resource = _nir_report_resource; |
| 494 | godot_nir_callbacks.report_sc_bit_offset_fn = _nir_report_sc_bit_offset; |
| 495 | godot_nir_callbacks.report_bitcode_bit_offset_fn = _nir_report_bitcode_bit_offset; |
| 496 | |
| 497 | nir_to_dxil_options nir_to_dxil_options = {}; |
| 498 | nir_to_dxil_options.environment = DXIL_ENVIRONMENT_VULKAN; |
| 499 | nir_to_dxil_options.shader_model_max = shader_model_d3d_to_dxil(D3D_SHADER_MODEL(REQUIRED_SHADER_MODEL)); |
| 500 | nir_to_dxil_options.validator_version_max = NO_DXIL_VALIDATION; |
| 501 | nir_to_dxil_options.godot_nir_callbacks = &godot_nir_callbacks; |
| 502 | |
| 503 | dxil_logger logger = {}; |
| 504 | logger.log = [](void *p_priv, const char *p_msg) { |
| 505 | #ifdef DEBUG_ENABLED |
| 506 | print_verbose(p_msg); |
| 507 | #endif |
| 508 | }; |
| 509 | |
| 510 | blob dxil_blob = {}; |
| 511 | bool ok = nir_to_dxil(it.value, &nir_to_dxil_options, &logger, &dxil_blob); |
| 512 | ERR_FAIL_COND_V_MSG(!ok, false, "Shader translation at stage " + String(RenderingDeviceCommons::SHADER_STAGE_NAMES[stage]) + " failed."); |
| 513 | |
| 514 | Vector<uint8_t> blob_copy; |
| 515 | blob_copy.resize(dxil_blob.size); |
| 516 | memcpy(blob_copy.ptrw(), dxil_blob.data, dxil_blob.size); |
| 517 | blob_finish(&dxil_blob); |
| 518 | r_dxil_blobs.insert(stage, blob_copy); |
| 519 | } |
| 520 | |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | bool RenderingShaderContainerD3D12::_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) { |
| 525 | r_dxil_blobs.clear(); |
| 526 | |
| 527 | HashMap<int, nir_shader *> stages_nir_shaders; |
| 528 | auto free_nir_shaders = [&]() { |
| 529 | for (KeyValue<int, nir_shader *> &E : stages_nir_shaders) { |
| 530 | ralloc_free(E.value); |
| 531 | } |
| 532 | stages_nir_shaders.clear(); |
| 533 | }; |
| 534 | |
| 535 | // This structure must live as long as the shaders are alive. |
| 536 | nir_shader_compiler_options compiler_options = {}; |
| 537 | const unsigned supported_bit_sizes = 16 | 32 | 64; |
| 538 | dxil_get_nir_compiler_options(&compiler_options, shader_model_d3d_to_dxil(D3D_SHADER_MODEL(REQUIRED_SHADER_MODEL)), supported_bit_sizes, supported_bit_sizes); |
| 539 | compiler_options.lower_base_vertex = false; |
| 540 | |
| 541 | // This is based on spirv2dxil.c. May need updates when it changes. |
| 542 | // Also, this has to stay around until after linking. |
| 543 | if (!_convert_spirv_to_nir(p_spirv, &compiler_options, stages_nir_shaders, r_stages, r_stages_processed)) { |
| 544 | free_nir_shaders(); |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | if (!_convert_nir_to_dxil(stages_nir_shaders, r_stages_processed, r_dxil_blobs)) { |
| 549 | free_nir_shaders(); |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | free_nir_shaders(); |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | bool RenderingShaderContainerD3D12::_generate_root_signature(BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed) { |
| 558 | // Root (push) constants. |
| 559 | LocalVector<D3D12_ROOT_PARAMETER1> root_params; |
| 560 | if (reflection_data_d3d12.dxil_push_constant_stages) { |
| 561 | CD3DX12_ROOT_PARAMETER1 push_constant; |
| 562 | push_constant.InitAsConstants( |
| 563 | reflection_data.push_constant_size / sizeof(uint32_t), |
| 564 | ROOT_CONSTANT_REGISTER, |
| 565 | 0, |
| 566 | stages_to_d3d12_visibility(reflection_data_d3d12.dxil_push_constant_stages)); |
| 567 | |
| 568 | root_params.push_back(push_constant); |
| 569 | } |
| 570 | |
| 571 | // NIR-DXIL runtime data. |
| 572 | if (reflection_data_d3d12.nir_runtime_data_root_param_idx == 1) { // Set above to 1 when discovering runtime data is needed. |
| 573 | DEV_ASSERT(!reflection_data.is_compute); // Could be supported if needed, but it's pointless as of now. |
| 574 | reflection_data_d3d12.nir_runtime_data_root_param_idx = root_params.size(); |
| 575 | CD3DX12_ROOT_PARAMETER1 nir_runtime_data; |
| 576 | nir_runtime_data.InitAsConstants( |
| 577 | sizeof(dxil_spirv_vertex_runtime_data) / sizeof(uint32_t), |
| 578 | RUNTIME_DATA_REGISTER, |
| 579 | 0, |
| 580 | D3D12_SHADER_VISIBILITY_VERTEX); |
| 581 | root_params.push_back(nir_runtime_data); |
| 582 | } |
| 583 | |
| 584 | // Descriptor tables (up to two per uniform set, for resources and/or samplers). |
| 585 | // These have to stay around until serialization! |
| 586 | struct TraceableDescriptorTable { |
| 587 | uint32_t stages_mask = {}; |
| 588 | Vector<D3D12_DESCRIPTOR_RANGE1> ranges; |
| 589 | uint32_t set = UINT_MAX; |
| 590 | }; |
| 591 | |
| 592 | uint32_t binding_start = 0; |
| 593 | Vector<TraceableDescriptorTable> resource_tables_maps; |
| 594 | Vector<TraceableDescriptorTable> sampler_tables_maps; |
| 595 | for (uint32_t i = 0; i < reflection_binding_set_uniforms_count.size(); i++) { |
| 596 | bool first_resource_in_set = true; |
| 597 | bool first_sampler_in_set = true; |
| 598 | uint32_t uniform_count = reflection_binding_set_uniforms_count[i]; |
| 599 | for (uint32_t j = 0; j < uniform_count; j++) { |
| 600 | const ReflectionBindingData &uniform = reflection_binding_set_uniforms_data[binding_start + j]; |
| 601 | ReflectionBindingDataD3D12 &uniform_d3d12 = reflection_binding_set_uniforms_data_d3d12.ptrw()[binding_start + j]; |
| 602 | #ifdef DEV_ENABLED |
| 603 | bool really_used = uniform_d3d12.dxil_stages != 0; |
| 604 | bool anybody_home = (ResourceClass)(uniform_d3d12.resource_class) != RES_CLASS_INVALID || uniform_d3d12.has_sampler; |
| 605 | DEV_ASSERT(anybody_home == really_used); |
| 606 | #endif |
| 607 | |
| 608 | auto insert_range = [i](D3D12_DESCRIPTOR_RANGE_TYPE p_range_type, |
| 609 | uint32_t p_num_descriptors, |
| 610 | uint32_t p_dxil_register, |
| 611 | uint32_t p_dxil_stages_mask, |
| 612 | uint32_t &r_descriptor_offset, |
| 613 | uint32_t &r_descriptor_count, |
| 614 | bool &r_first_in_set, |
| 615 | Vector<TraceableDescriptorTable> &r_tables) { |
| 616 | r_descriptor_offset = r_descriptor_count; |
| 617 | |
| 618 | if (r_first_in_set) { |
| 619 | r_tables.resize(r_tables.size() + 1); |
| 620 | r_first_in_set = false; |
| 621 | } |
| 622 | |
| 623 | TraceableDescriptorTable &table = r_tables.write[r_tables.size() - 1]; |
| 624 | DEV_ASSERT(table.set == UINT_MAX || table.set == i); |
| 625 | |
| 626 | table.stages_mask |= p_dxil_stages_mask; |
| 627 | table.set = i; |
| 628 | |
| 629 | CD3DX12_DESCRIPTOR_RANGE1 range; |
| 630 | |
| 631 | // Due to the aliasing hack for SRV-UAV of different families, |
| 632 | // we can be causing an unintended change of data (sometimes the validation layers catch it). |
| 633 | D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE; |
| 634 | if (p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_SRV || p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_UAV) { |
| 635 | flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE; |
| 636 | } else if (p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_CBV) { |
| 637 | flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE; |
| 638 | } |
| 639 | |
| 640 | range.Init(p_range_type, p_num_descriptors, p_dxil_register, 0, flags, r_descriptor_offset); |
| 641 | r_descriptor_count += p_num_descriptors; |
| 642 | table.ranges.push_back(range); |
| 643 | }; |
| 644 | |
| 645 | D3D12_DESCRIPTOR_RANGE_TYPE range_type = (D3D12_DESCRIPTOR_RANGE_TYPE)UINT_MAX; |
| 646 | bool has_sampler = false; |
| 647 | uint32_t num_descriptors = 1; |
| 648 | |
| 649 | switch (uniform.type) { |
| 650 | case RDC::UNIFORM_TYPE_SAMPLER: { |
| 651 | has_sampler = true; |
| 652 | num_descriptors = uniform.length; |
| 653 | } break; |
| 654 | case RDC::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE: { |
| 655 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 656 | has_sampler = true; |
| 657 | num_descriptors = MAX(1u, uniform.length); |
| 658 | } break; |
| 659 | case RDC::UNIFORM_TYPE_TEXTURE: { |
| 660 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 661 | num_descriptors = MAX(1u, uniform.length); |
| 662 | } break; |
| 663 | case RDC::UNIFORM_TYPE_IMAGE: { |
| 664 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_UAV; |
| 665 | num_descriptors = MAX(1u, uniform.length); |
| 666 | } break; |
| 667 | case RDC::UNIFORM_TYPE_TEXTURE_BUFFER: { |
| 668 | CRASH_NOW_MSG("Unimplemented!"); |
| 669 | } break; |
| 670 | case RDC::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER: { |
| 671 | CRASH_NOW_MSG("Unimplemented!"); |
| 672 | } break; |
| 673 | case RDC::UNIFORM_TYPE_IMAGE_BUFFER: { |
| 674 | CRASH_NOW_MSG("Unimplemented!"); |
| 675 | } break; |
| 676 | case RDC::UNIFORM_TYPE_UNIFORM_BUFFER: { |
| 677 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_CBV; |
| 678 | } break; |
| 679 | case RDC::UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC: { |
| 680 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_CBV; |
| 681 | } break; |
| 682 | case RDC::UNIFORM_TYPE_STORAGE_BUFFER: { |
| 683 | range_type = uniform.writable ? D3D12_DESCRIPTOR_RANGE_TYPE_UAV : D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 684 | } break; |
| 685 | case RDC::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC: { |
| 686 | range_type = uniform.writable ? D3D12_DESCRIPTOR_RANGE_TYPE_UAV : D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 687 | } break; |
| 688 | case RDC::UNIFORM_TYPE_INPUT_ATTACHMENT: { |
| 689 | range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 690 | } break; |
| 691 | default: { |
| 692 | DEV_ASSERT(false); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | uint32_t dxil_register = i * GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER + uniform.binding * GODOT_NIR_BINDING_MULTIPLIER; |
| 697 | if (range_type != (D3D12_DESCRIPTOR_RANGE_TYPE)UINT_MAX) { |
| 698 | // Dynamic buffers are converted to root descriptors to prevent copying descriptors during command recording. |
| 699 | // Out of bounds accesses are not a concern because that's already undefined behavior on Vulkan. |
| 700 | if (uniform.type == RDC::UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC || uniform.type == RDC::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC) { |
| 701 | CD3DX12_ROOT_PARAMETER1 root_param = {}; |
| 702 | D3D12_SHADER_VISIBILITY visibility = stages_to_d3d12_visibility(uniform.stages); |
| 703 | |
| 704 | switch (range_type) { |
| 705 | case D3D12_DESCRIPTOR_RANGE_TYPE_CBV: { |
| 706 | root_param.InitAsConstantBufferView(dxil_register, 0, D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE, visibility); |
| 707 | } break; |
| 708 | case D3D12_DESCRIPTOR_RANGE_TYPE_SRV: { |
| 709 | root_param.InitAsShaderResourceView(dxil_register, 0, D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE, visibility); |
| 710 | } break; |
| 711 | case D3D12_DESCRIPTOR_RANGE_TYPE_UAV: { |
| 712 | root_param.InitAsUnorderedAccessView(dxil_register, 0, D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE, visibility); |
| 713 | } break; |
| 714 | default: { |
| 715 | DEV_ASSERT(false && "Unrecognized range type."); |
| 716 | } break; |
| 717 | } |
| 718 | |
| 719 | uniform_d3d12.root_param_idx = root_params.size(); |
| 720 | root_params.push_back(root_param); |
| 721 | } else { |
| 722 | insert_range( |
| 723 | range_type, |
| 724 | num_descriptors, |
| 725 | dxil_register, |
| 726 | uniform.stages, |
| 727 | uniform_d3d12.resource_descriptor_offset, |
| 728 | reflection_binding_set_data_d3d12.ptrw()[i].resource_descriptor_count, |
| 729 | first_resource_in_set, |
| 730 | resource_tables_maps); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (has_sampler) { |
| 735 | insert_range( |
| 736 | D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, |
| 737 | num_descriptors, |
| 738 | dxil_register, |
| 739 | uniform.stages, |
| 740 | uniform_d3d12.sampler_descriptor_offset, |
| 741 | reflection_binding_set_data_d3d12.ptrw()[i].sampler_descriptor_count, |
| 742 | first_sampler_in_set, |
| 743 | sampler_tables_maps); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | binding_start += uniform_count; |
| 748 | } |
| 749 | |
| 750 | for (const TraceableDescriptorTable &table : resource_tables_maps) { |
| 751 | CD3DX12_ROOT_PARAMETER1 root_table = {}; |
| 752 | root_table.InitAsDescriptorTable(table.ranges.size(), table.ranges.ptr(), stages_to_d3d12_visibility(table.stages_mask)); |
| 753 | reflection_binding_set_data_d3d12.ptrw()[table.set].resource_root_param_idx = root_params.size(); |
| 754 | root_params.push_back(root_table); |
| 755 | } |
| 756 | |
| 757 | for (const TraceableDescriptorTable &table : sampler_tables_maps) { |
| 758 | CD3DX12_ROOT_PARAMETER1 root_table = {}; |
| 759 | root_table.InitAsDescriptorTable(table.ranges.size(), table.ranges.ptr(), stages_to_d3d12_visibility(table.stages_mask)); |
| 760 | reflection_binding_set_data_d3d12.ptrw()[table.set].sampler_root_param_idx = root_params.size(); |
| 761 | root_params.push_back(root_table); |
| 762 | } |
| 763 | |
| 764 | CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC root_sig_desc = {}; |
| 765 | D3D12_ROOT_SIGNATURE_FLAGS root_sig_flags = |
| 766 | D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | |
| 767 | D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | |
| 768 | D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS; |
| 769 | |
| 770 | if (!p_stages_processed.has_flag(RenderingDeviceCommons::SHADER_STAGE_VERTEX_BIT)) { |
| 771 | root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS; |
| 772 | } |
| 773 | |
| 774 | if (!p_stages_processed.has_flag(RenderingDeviceCommons::SHADER_STAGE_FRAGMENT_BIT)) { |
| 775 | root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS; |
| 776 | } |
| 777 | |
| 778 | if (reflection_data.vertex_input_mask) { |
| 779 | root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; |
| 780 | } |
| 781 | |
| 782 | root_sig_desc.Init_1_1(root_params.size(), root_params.ptr(), 0, nullptr, root_sig_flags); |
| 783 | |
| 784 | // Create and store the root signature and its CRC32. |
| 785 | ID3DBlob *error_blob = nullptr; |
| 786 | ID3DBlob *root_sig_blob = nullptr; |
| 787 | HRESULT res = D3DX12SerializeVersionedRootSignature(HMODULE(lib_d3d12), &root_sig_desc, D3D_ROOT_SIGNATURE_VERSION_1_1, &root_sig_blob, &error_blob); |
| 788 | if (SUCCEEDED(res)) { |
| 789 | root_signature_bytes.resize(root_sig_blob->GetBufferSize()); |
| 790 | memcpy(root_signature_bytes.ptrw(), root_sig_blob->GetBufferPointer(), root_sig_blob->GetBufferSize()); |
| 791 | |
| 792 | root_signature_crc = crc32(0, nullptr, 0); |
| 793 | root_signature_crc = crc32(root_signature_crc, (const Bytef *)root_sig_blob->GetBufferPointer(), root_sig_blob->GetBufferSize()); |
| 794 | |
| 795 | return true; |
| 796 | } else { |
| 797 | if (root_sig_blob != nullptr) { |
| 798 | root_sig_blob->Release(); |
| 799 | } |
| 800 | |
| 801 | String error_string; |
| 802 | if (error_blob != nullptr) { |
| 803 | error_string = vformat("Serialization of root signature failed with error 0x%08ux and the following message:\n%s", uint32_t(res), String::ascii(Span((char *)error_blob->GetBufferPointer(), error_blob->GetBufferSize()))); |
| 804 | error_blob->Release(); |
| 805 | } else { |
| 806 | error_string = vformat("Serialization of root signature failed with error 0x%08ux", uint32_t(res)); |
| 807 | } |
| 808 | |
| 809 | ERR_FAIL_V_MSG(false, error_string); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | void RenderingShaderContainerD3D12::_nir_report_resource(uint32_t p_register, uint32_t p_space, uint32_t p_dxil_type, void *p_data) { |
| 814 | const GodotNirCallbackUserData &user_data = *(GodotNirCallbackUserData *)p_data; |
| 815 | |
| 816 | // Types based on Mesa's dxil_container.h. |
| 817 | static const uint32_t DXIL_RES_SAMPLER = 1; |
| 818 | static const ResourceClass DXIL_TYPE_TO_CLASS[] = { |
| 819 | RES_CLASS_INVALID, // DXIL_RES_INVALID |
| 820 | RES_CLASS_INVALID, // DXIL_RES_SAMPLER |
| 821 | RES_CLASS_CBV, // DXIL_RES_CBV |
| 822 | RES_CLASS_SRV, // DXIL_RES_SRV_TYPED |
| 823 | RES_CLASS_SRV, // DXIL_RES_SRV_RAW |
| 824 | RES_CLASS_SRV, // DXIL_RES_SRV_STRUCTURED |
| 825 | RES_CLASS_UAV, // DXIL_RES_UAV_TYPED |
| 826 | RES_CLASS_UAV, // DXIL_RES_UAV_RAW |
| 827 | RES_CLASS_UAV, // DXIL_RES_UAV_STRUCTURED |
| 828 | RES_CLASS_INVALID, // DXIL_RES_UAV_STRUCTURED_WITH_COUNTER |
| 829 | }; |
| 830 | |
| 831 | DEV_ASSERT(p_dxil_type < ARRAY_SIZE(DXIL_TYPE_TO_CLASS)); |
| 832 | ResourceClass resource_class = DXIL_TYPE_TO_CLASS[p_dxil_type]; |
| 833 | |
| 834 | if (p_register == ROOT_CONSTANT_REGISTER && p_space == 0) { |
| 835 | DEV_ASSERT(resource_class == RES_CLASS_CBV); |
| 836 | user_data.container->reflection_data_d3d12.dxil_push_constant_stages |= (1 << user_data.stage); |
| 837 | } else if (p_register == RUNTIME_DATA_REGISTER && p_space == 0) { |
| 838 | DEV_ASSERT(resource_class == RES_CLASS_CBV); |
| 839 | user_data.container->reflection_data_d3d12.nir_runtime_data_root_param_idx = 1; // Temporary, to be determined later. |
| 840 | } else { |
| 841 | DEV_ASSERT(p_space == 0); |
| 842 | |
| 843 | uint32_t set = p_register / GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER; |
| 844 | uint32_t binding = (p_register % GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER) / GODOT_NIR_BINDING_MULTIPLIER; |
| 845 | |
| 846 | DEV_ASSERT(set < (uint32_t)user_data.container->reflection_binding_set_uniforms_count.size()); |
| 847 | |
| 848 | uint32_t binding_start = 0; |
| 849 | for (uint32_t i = 0; i < set; i++) { |
| 850 | binding_start += user_data.container->reflection_binding_set_uniforms_count[i]; |
| 851 | } |
| 852 | |
| 853 | [[maybe_unused]] bool found = false; |
| 854 | for (uint32_t i = 0; i < user_data.container->reflection_binding_set_uniforms_count[set]; i++) { |
| 855 | const ReflectionBindingData &uniform = user_data.container->reflection_binding_set_uniforms_data[binding_start + i]; |
| 856 | ReflectionBindingDataD3D12 &uniform_d3d12 = user_data.container->reflection_binding_set_uniforms_data_d3d12.ptrw()[binding_start + i]; |
| 857 | if (uniform.binding != binding) { |
| 858 | continue; |
| 859 | } |
| 860 | |
| 861 | uniform_d3d12.dxil_stages |= (1 << user_data.stage); |
| 862 | if (resource_class != RES_CLASS_INVALID) { |
| 863 | DEV_ASSERT(uniform_d3d12.resource_class == (uint32_t)RES_CLASS_INVALID || uniform_d3d12.resource_class == (uint32_t)resource_class); |
| 864 | uniform_d3d12.resource_class = resource_class; |
| 865 | } else if (p_dxil_type == DXIL_RES_SAMPLER) { |
| 866 | uniform_d3d12.has_sampler = (uint32_t)true; |
| 867 | } else { |
| 868 | DEV_ASSERT(false && "Unknown resource class."); |
| 869 | } |
| 870 | found = true; |
| 871 | } |
| 872 | |
| 873 | DEV_ASSERT(found); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | void RenderingShaderContainerD3D12::_nir_report_sc_bit_offset(uint32_t p_sc_id, uint64_t p_bit_offset, void *p_data) { |
| 878 | const GodotNirCallbackUserData &user_data = *(GodotNirCallbackUserData *)p_data; |
| 879 | [[maybe_unused]] bool found = false; |
| 880 | for (int64_t i = 0; i < user_data.container->reflection_specialization_data.size(); i++) { |
| 881 | const ReflectionSpecializationData &sc = user_data.container->reflection_specialization_data[i]; |
| 882 | ReflectionSpecializationDataD3D12 &sc_d3d12 = user_data.container->reflection_specialization_data_d3d12.ptrw()[i]; |
| 883 | if (sc.constant_id != p_sc_id) { |
| 884 | continue; |
| 885 | } |
| 886 | |
| 887 | uint32_t offset_idx = SHADER_STAGES_BIT_OFFSET_INDICES[user_data.stage]; |
| 888 | DEV_ASSERT(sc_d3d12.stages_bit_offsets[offset_idx] == 0); |
| 889 | sc_d3d12.stages_bit_offsets[offset_idx] = p_bit_offset; |
| 890 | found = true; |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | DEV_ASSERT(found); |
| 895 | } |
| 896 | |
| 897 | void RenderingShaderContainerD3D12::_nir_report_bitcode_bit_offset(uint64_t p_bit_offset, void *p_data) { |
| 898 | DEV_ASSERT(p_bit_offset % 8 == 0); |
| 899 | |
| 900 | const GodotNirCallbackUserData &user_data = *(GodotNirCallbackUserData *)p_data; |
| 901 | uint32_t offset_idx = SHADER_STAGES_BIT_OFFSET_INDICES[user_data.stage]; |
| 902 | for (int64_t i = 0; i < user_data.container->reflection_specialization_data.size(); i++) { |
| 903 | ReflectionSpecializationDataD3D12 &sc_d3d12 = user_data.container->reflection_specialization_data_d3d12.ptrw()[i]; |
| 904 | if (sc_d3d12.stages_bit_offsets[offset_idx] == 0) { |
| 905 | // This SC has been optimized out from this stage. |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | sc_d3d12.stages_bit_offsets[offset_idx] += p_bit_offset; |
| 910 | } |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | void RenderingShaderContainerD3D12::_set_from_shader_reflection_post(const ReflectShader &p_shader) { |
| 915 | reflection_binding_set_data_d3d12.resize(reflection_binding_set_uniforms_count.size()); |
| 916 | reflection_binding_set_uniforms_data_d3d12.resize(reflection_binding_set_uniforms_data.size()); |
| 917 | reflection_specialization_data_d3d12.resize(reflection_specialization_data.size()); |
| 918 | |
| 919 | // Sort bindings inside each uniform set. This guarantees the root signature will be generated in the correct order. |
| 920 | SortArray<ReflectionBindingData> sorter; |
| 921 | uint32_t binding_start = 0; |
| 922 | for (uint32_t i = 0; i < reflection_binding_set_uniforms_count.size(); i++) { |
| 923 | uint32_t uniform_count = reflection_binding_set_uniforms_count[i]; |
| 924 | if (uniform_count > 0) { |
| 925 | sorter.sort(&reflection_binding_set_uniforms_data.ptrw()[binding_start], uniform_count); |
| 926 | binding_start += uniform_count; |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | bool RenderingShaderContainerD3D12::_set_code_from_spirv(const ReflectShader &p_shader) { |
| 932 | #if NIR_ENABLED |
| 933 | const LocalVector<ReflectShaderStage> &p_spirv = p_shader.shader_stages; |
| 934 | reflection_data_d3d12.nir_runtime_data_root_param_idx = UINT32_MAX; |
| 935 | |
| 936 | for (int64_t i = 0; i < reflection_specialization_data.size(); i++) { |
| 937 | DEV_ASSERT(reflection_specialization_data[i].constant_id < (sizeof(reflection_data_d3d12.spirv_specialization_constants_ids_mask) * 8) && "Constant IDs with values above 31 are not supported."); |
| 938 | reflection_data_d3d12.spirv_specialization_constants_ids_mask |= (1 << reflection_specialization_data[i].constant_id); |
| 939 | } |
| 940 | |
| 941 | // Translate SPIR-V shaders to DXIL, and collect shader info from the new representation. |
| 942 | HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> dxil_blobs; |
| 943 | Vector<RenderingDeviceCommons::ShaderStage> stages; |
| 944 | BitField<RenderingDeviceCommons::ShaderStage> stages_processed = {}; |
| 945 | if (!_convert_spirv_to_dxil(p_spirv, dxil_blobs, stages, stages_processed)) { |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | // Patch with default values of specialization constants. |
| 950 | DEV_ASSERT(reflection_specialization_data.size() == reflection_specialization_data_d3d12.size()); |
| 951 | for (int32_t i = 0; i < reflection_specialization_data.size(); i++) { |
| 952 | const ReflectionSpecializationData &sc = reflection_specialization_data[i]; |
| 953 | const ReflectionSpecializationDataD3D12 &sc_d3d12 = reflection_specialization_data_d3d12[i]; |
| 954 | RenderingDXIL::patch_specialization_constant((RenderingDeviceCommons::PipelineSpecializationConstantType)(sc.type), &sc.int_value, sc_d3d12.stages_bit_offsets, dxil_blobs, true); |
| 955 | } |
| 956 | |
| 957 | // Sign. |
| 958 | uint32_t shader_index = 0; |
| 959 | for (KeyValue<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &E : dxil_blobs) { |
| 960 | RenderingDXIL::sign_bytecode(E.key, E.value); |
| 961 | } |
| 962 | |
| 963 | // Store compressed DXIL blobs as the shaders. |
| 964 | shaders.resize(p_spirv.size()); |
| 965 | for (int64_t i = 0; i < shaders.size(); i++) { |
| 966 | const PackedByteArray &dxil_bytes = dxil_blobs[stages[i]]; |
| 967 | RenderingShaderContainer::Shader &shader = shaders.ptrw()[i]; |
| 968 | uint32_t compressed_size = 0; |
| 969 | shader.shader_stage = stages[i]; |
| 970 | shader.code_decompressed_size = dxil_bytes.size(); |
| 971 | shader.code_compressed_bytes.resize(dxil_bytes.size()); |
| 972 | |
| 973 | bool compressed = compress_code(dxil_bytes.ptr(), dxil_bytes.size(), shader.code_compressed_bytes.ptrw(), &compressed_size, &shader.code_compression_flags); |
| 974 | ERR_FAIL_COND_V_MSG(!compressed, false, vformat("Failed to compress native code to native for SPIR-V #%d.", shader_index)); |
| 975 | |
| 976 | shader.code_compressed_bytes.resize(compressed_size); |
| 977 | } |
| 978 | |
| 979 | if (!_generate_root_signature(stages_processed)) { |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | return true; |
| 984 | #else |
| 985 | ERR_FAIL_V_MSG(false, "Shader compilation is not supported at runtime without NIR."); |
| 986 | #endif |
| 987 | } |
| 988 | |
| 989 | RenderingShaderContainerD3D12::RenderingShaderContainerD3D12() { |
| 990 | // Default empty constructor. |
| 991 | } |
| 992 | |
| 993 | RenderingShaderContainerD3D12::RenderingShaderContainerD3D12(void *p_lib_d3d12) { |
| 994 | lib_d3d12 = p_lib_d3d12; |
| 995 | } |
| 996 | |
| 997 | RenderingShaderContainerD3D12::ShaderReflectionD3D12 RenderingShaderContainerD3D12::get_shader_reflection_d3d12() const { |
| 998 | ShaderReflectionD3D12 reflection; |
| 999 | reflection.spirv_specialization_constants_ids_mask = reflection_data_d3d12.spirv_specialization_constants_ids_mask; |
| 1000 | reflection.dxil_push_constant_stages = reflection_data_d3d12.dxil_push_constant_stages; |
| 1001 | reflection.nir_runtime_data_root_param_idx = reflection_data_d3d12.nir_runtime_data_root_param_idx; |
| 1002 | reflection.reflection_binding_sets_d3d12 = reflection_binding_set_data_d3d12; |
| 1003 | reflection.reflection_specialization_data_d3d12 = reflection_specialization_data_d3d12; |
| 1004 | reflection.root_signature_bytes = root_signature_bytes; |
| 1005 | reflection.root_signature_crc = root_signature_crc; |
| 1006 | |
| 1007 | // Transform data vector into a vector of vectors that's easier to user. |
| 1008 | uint32_t uniform_index = 0; |
| 1009 | reflection.reflection_binding_set_uniforms_d3d12.resize(reflection_binding_set_uniforms_count.size()); |
| 1010 | for (int64_t i = 0; i < reflection.reflection_binding_set_uniforms_d3d12.size(); i++) { |
| 1011 | Vector<ReflectionBindingDataD3D12> &uniforms = reflection.reflection_binding_set_uniforms_d3d12.ptrw()[i]; |
| 1012 | uniforms.resize(reflection_binding_set_uniforms_count[i]); |
| 1013 | for (int64_t j = 0; j < uniforms.size(); j++) { |
| 1014 | uniforms.ptrw()[j] = reflection_binding_set_uniforms_data_d3d12[uniform_index]; |
| 1015 | uniform_index++; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | return reflection; |
| 1020 | } |
| 1021 | |
| 1022 | // RenderingShaderContainerFormatD3D12 |
| 1023 | |
| 1024 | void RenderingShaderContainerFormatD3D12::set_lib_d3d12(void *p_lib_d3d12) { |
| 1025 | lib_d3d12 = p_lib_d3d12; |
| 1026 | } |
| 1027 | |
| 1028 | Ref<RenderingShaderContainer> RenderingShaderContainerFormatD3D12::create_container() const { |
| 1029 | return memnew(RenderingShaderContainerD3D12(lib_d3d12)); |
| 1030 | } |
| 1031 | |
| 1032 | RenderingDeviceCommons::ShaderLanguageVersion RenderingShaderContainerFormatD3D12::get_shader_language_version() const { |
| 1033 | // NIR-DXIL is Vulkan 1.1-conformant. |
| 1034 | return SHADER_LANGUAGE_VULKAN_VERSION_1_1; |
| 1035 | } |
| 1036 | |
| 1037 | RenderingDeviceCommons::ShaderSpirvVersion RenderingShaderContainerFormatD3D12::get_shader_spirv_version() const { |
| 1038 | // The SPIR-V part of Mesa supports 1.6, but: |
| 1039 | // - SPIRV-Reflect won't be able to parse the compute workgroup size. |
| 1040 | // - We want to play it safe with NIR-DXIL. |
| 1041 | return SHADER_SPIRV_VERSION_1_5; |
| 1042 | } |
| 1043 | |
| 1044 | RenderingShaderContainerFormatD3D12::RenderingShaderContainerFormatD3D12() {} |
| 1045 | |
| 1046 | RenderingShaderContainerFormatD3D12::~RenderingShaderContainerFormatD3D12() {} |