rendering_shader_container_d3d12.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #pragma once
  31. #include "servers/rendering/rendering_shader_container.h"
  32. #define NIR_ENABLED 1
  33. #ifdef SHADER_BAKER_RUNTIME_ENABLED
  34. #undef NIR_ENABLED
  35. #endif
  36. #include "d3d12_godot_nir_bridge.h"
  37. #define D3D12_BITCODE_OFFSETS_NUM_STAGES 3
  38. #if NIR_ENABLED
  39. struct nir_shader;
  40. struct nir_shader_compiler_options;
  41. #endif
  42. enum RootSignatureLocationType {
  43. RS_LOC_TYPE_RESOURCE,
  44. RS_LOC_TYPE_SAMPLER,
  45. };
  46. enum ResourceClass {
  47. RES_CLASS_INVALID,
  48. RES_CLASS_CBV,
  49. RES_CLASS_SRV,
  50. RES_CLASS_UAV,
  51. };
  52. struct RenderingDXIL {
  53. static uint32_t patch_specialization_constant(
  54. RenderingDeviceCommons::PipelineSpecializationConstantType p_type,
  55. const void *p_value,
  56. const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES],
  57. HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_stages_bytecodes,
  58. bool p_is_first_patch);
  59. static void sign_bytecode(RenderingDeviceCommons::ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob);
  60. };
  61. class RenderingShaderContainerD3D12 : public RenderingShaderContainer {
  62. GDSOFTCLASS(RenderingShaderContainerD3D12, RenderingShaderContainer);
  63. public:
  64. static constexpr uint32_t REQUIRED_SHADER_MODEL = 0x62; // D3D_SHADER_MODEL_6_2
  65. static constexpr uint32_t ROOT_CONSTANT_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RenderingDeviceCommons::MAX_UNIFORM_SETS + 1);
  66. static constexpr uint32_t RUNTIME_DATA_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RenderingDeviceCommons::MAX_UNIFORM_SETS + 2);
  67. static constexpr uint32_t FORMAT_VERSION = 1;
  68. static constexpr uint32_t SHADER_STAGES_BIT_OFFSET_INDICES[RenderingDeviceCommons::SHADER_STAGE_MAX] = {
  69. 0, // SHADER_STAGE_VERTEX
  70. 1, // SHADER_STAGE_FRAGMENT
  71. UINT32_MAX, // SHADER_STAGE_TESSELATION_CONTROL
  72. UINT32_MAX, // SHADER_STAGE_TESSELATION_EVALUATION
  73. 2, // SHADER_STAGE_COMPUTE
  74. };
  75. struct RootSignatureLocation {
  76. uint32_t root_param_index = UINT32_MAX;
  77. uint32_t range_index = UINT32_MAX;
  78. };
  79. struct ReflectionBindingDataD3D12 {
  80. uint32_t resource_class = 0;
  81. uint32_t has_sampler = 0;
  82. uint32_t dxil_stages = 0;
  83. RootSignatureLocation root_signature_locations[2];
  84. };
  85. struct ReflectionSpecializationDataD3D12 {
  86. uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
  87. };
  88. protected:
  89. struct ReflectionDataD3D12 {
  90. uint32_t spirv_specialization_constants_ids_mask = 0;
  91. uint32_t dxil_push_constant_stages = 0;
  92. uint32_t nir_runtime_data_root_param_idx = 0;
  93. };
  94. struct ContainerFooterD3D12 {
  95. uint32_t root_signature_length = 0;
  96. uint32_t root_signature_crc = 0;
  97. };
  98. void *lib_d3d12 = nullptr;
  99. ReflectionDataD3D12 reflection_data_d3d12;
  100. Vector<ReflectionBindingDataD3D12> reflection_binding_set_uniforms_data_d3d12;
  101. Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12;
  102. Vector<uint8_t> root_signature_bytes;
  103. uint32_t root_signature_crc = 0;
  104. #if NIR_ENABLED
  105. 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);
  106. 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);
  107. 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);
  108. bool _generate_root_signature(BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed);
  109. // GodotNirCallbacks.
  110. static void _nir_report_resource(uint32_t p_register, uint32_t p_space, uint32_t p_dxil_type, void *p_data);
  111. static void _nir_report_sc_bit_offset(uint32_t p_sc_id, uint64_t p_bit_offset, void *p_data);
  112. static void _nir_report_bitcode_bit_offset(uint64_t p_bit_offset, void *p_data);
  113. #endif
  114. // RenderingShaderContainer overrides.
  115. virtual uint32_t _format() const override;
  116. virtual uint32_t _format_version() const override;
  117. virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes) override;
  118. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) override;
  119. virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
  120. virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) override;
  121. virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
  122. virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes) override;
  123. virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const override;
  124. virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
  125. virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
  126. virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const override;
  127. virtual void _set_from_shader_reflection_post(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection) override;
  128. virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) override;
  129. public:
  130. struct ShaderReflectionD3D12 {
  131. uint32_t spirv_specialization_constants_ids_mask = 0;
  132. uint32_t dxil_push_constant_stages = 0;
  133. uint32_t nir_runtime_data_root_param_idx = 0;
  134. Vector<Vector<ReflectionBindingDataD3D12>> reflection_binding_set_uniforms_d3d12;
  135. Vector<ReflectionSpecializationDataD3D12> reflection_specialization_data_d3d12;
  136. Vector<uint8_t> root_signature_bytes;
  137. uint32_t root_signature_crc = 0;
  138. };
  139. RenderingShaderContainerD3D12();
  140. RenderingShaderContainerD3D12(void *p_lib_d3d12);
  141. ShaderReflectionD3D12 get_shader_reflection_d3d12() const;
  142. };
  143. class RenderingShaderContainerFormatD3D12 : public RenderingShaderContainerFormat {
  144. protected:
  145. void *lib_d3d12 = nullptr;
  146. public:
  147. void set_lib_d3d12(void *p_lib_d3d12);
  148. virtual Ref<RenderingShaderContainer> create_container() const override;
  149. virtual ShaderLanguageVersion get_shader_language_version() const override;
  150. virtual ShaderSpirvVersion get_shader_spirv_version() const override;
  151. RenderingShaderContainerFormatD3D12();
  152. virtual ~RenderingShaderContainerFormatD3D12();
  153. };