1 /* 2 * Copyright © Microsoft Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #ifndef SPIRV_TO_DXIL_H 25 #define SPIRV_TO_DXIL_H 26 27 #include <stdbool.h> 28 #include <stddef.h> 29 #include <stdint.h> 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 35 // NB: I've copy and pasted some types into this header so we don't have to 36 // include other headers. This will surely break if any of these types change. 37 38 // Copy of gl_shader_stage 39 typedef enum { 40 DXIL_SPIRV_SHADER_NONE = -1, 41 DXIL_SPIRV_SHADER_VERTEX = 0, 42 DXIL_SPIRV_SHADER_TESS_CTRL = 1, 43 DXIL_SPIRV_SHADER_TESS_EVAL = 2, 44 DXIL_SPIRV_SHADER_GEOMETRY = 3, 45 DXIL_SPIRV_SHADER_FRAGMENT = 4, 46 DXIL_SPIRV_SHADER_COMPUTE = 5, 47 DXIL_SPIRV_SHADER_KERNEL = 14, 48 } dxil_spirv_shader_stage; 49 50 // Copy of nir_spirv_const_value 51 typedef union { 52 bool b; 53 float f32; 54 double f64; 55 int8_t i8; 56 uint8_t u8; 57 int16_t i16; 58 uint16_t u16; 59 int32_t i32; 60 uint32_t u32; 61 int64_t i64; 62 uint64_t u64; 63 } dxil_spirv_const_value; 64 65 // Copy of nir_spirv_specialization 66 struct dxil_spirv_specialization { 67 uint32_t id; 68 dxil_spirv_const_value value; 69 bool defined_on_module; 70 }; 71 72 struct dxil_spirv_metadata { 73 bool requires_runtime_data; 74 }; 75 76 struct dxil_spirv_object { 77 struct dxil_spirv_metadata metadata; 78 struct { 79 void *buffer; 80 size_t size; 81 } binary; 82 }; 83 84 /* This struct describes the layout of data expected in the CB bound to 85 * runtime_data_cbv during compute shader execution */ 86 struct dxil_spirv_compute_runtime_data { 87 /* Total number of groups dispatched (i.e. value passed to Dispatch()) */ 88 uint32_t group_count_x; 89 uint32_t group_count_y; 90 uint32_t group_count_z; 91 }; 92 93 #define DXIL_SPIRV_Y_FLIP_MASK BITFIELD_MASK(DXIL_SPIRV_MAX_VIEWPORT) 94 #define DXIL_SPIRV_Z_FLIP_SHIFT DXIL_SPIRV_MAX_VIEWPORT 95 #define DXIL_SPIRV_Z_FLIP_MASK BITFIELD_RANGE(DXIL_SPIRV_Z_FLIP_SHIFT, DXIL_SPIRV_MAX_VIEWPORT) 96 97 /* This struct describes the layout of data expected in the CB bound to 98 * runtime_data_cbv during vertex stages */ 99 struct dxil_spirv_vertex_runtime_data { 100 uint32_t first_vertex; 101 uint32_t base_instance; 102 bool is_indexed_draw; 103 // The lower 16bits of this mask encode Y-flips (one bit per viewport) 104 // The higher 16bits of this maks encode Z-flips (one bit per viewport) 105 union { 106 uint32_t yz_flip_mask; 107 struct { 108 uint16_t y_flip_mask; 109 uint16_t z_flip_mask; 110 }; 111 }; 112 uint32_t draw_id; 113 }; 114 115 enum dxil_spirv_yz_flip_mode { 116 DXIL_SPIRV_YZ_FLIP_NONE = 0, 117 // Y-flip is unconditional: pos.y = -pos.y 118 // Z-flip is unconditional: pos.z = -pos.z + 1.0f 119 DXIL_SPIRV_Y_FLIP_UNCONDITIONAL = 1 << 0, 120 DXIL_SPIRV_Z_FLIP_UNCONDITIONAL = 1 << 1, 121 DXIL_SPIRV_YZ_FLIP_UNCONDITIONAL = DXIL_SPIRV_Y_FLIP_UNCONDITIONAL | DXIL_SPIRV_Z_FLIP_UNCONDITIONAL, 122 // Y-flip/Z-flip info are passed through a sysval 123 DXIL_SPIRV_Y_FLIP_CONDITIONAL = 1 << 2, 124 DXIL_SPIRV_Z_FLIP_CONDITIONAL = 1 << 3, 125 DXIL_SPIRV_YZ_FLIP_CONDITIONAL = DXIL_SPIRV_Y_FLIP_CONDITIONAL | DXIL_SPIRV_Z_FLIP_CONDITIONAL, 126 }; 127 128 #define DXIL_SPIRV_MAX_VIEWPORT 16 129 130 struct dxil_spirv_runtime_conf { 131 struct { 132 uint32_t register_space; 133 uint32_t base_shader_register; 134 } runtime_data_cbv; 135 136 struct { 137 uint32_t register_space; 138 uint32_t base_shader_register; 139 } push_constant_cbv; 140 141 // Set true if vertex and instance ids have already been converted to 142 // zero-based. Otherwise, runtime_data will be required to lower them. 143 bool zero_based_vertex_instance_id; 144 145 struct { 146 // mode != DXIL_SPIRV_YZ_FLIP_NONE only valid on vertex/geometry stages. 147 enum dxil_spirv_yz_flip_mode mode; 148 149 // Y/Z flip masks (one bit per viewport) 150 uint16_t y_mask; 151 uint16_t z_mask; 152 } yz_flip; 153 154 // The caller supports read-only images to be turned into SRV accesses, 155 // which allows us to run the nir_opt_access() pass 156 bool read_only_images_as_srvs; 157 158 // Force sample rate shading on a fragment shader 159 bool force_sample_rate_shading; 160 }; 161 162 struct dxil_spirv_debug_options { 163 bool dump_nir; 164 }; 165 166 /** 167 * Compile a SPIR-V module into DXIL. 168 * \param words SPIR-V module to compile 169 * \param word_count number of words in the SPIR-V module 170 * \param specializations specialization constants to compile with the shader 171 * \param num_specializations number of specialization constants 172 * \param stage shader stage 173 * \param entry_point_name name of shader entrypoint 174 * \param conf configuration for spriv_to_dxil 175 * \param out_dxil will contain the DXIL bytes on success (call spirv_to_dxil_free after use) 176 * \return true if compilation succeeded 177 */ 178 bool 179 spirv_to_dxil(const uint32_t *words, size_t word_count, 180 struct dxil_spirv_specialization *specializations, 181 unsigned int num_specializations, dxil_spirv_shader_stage stage, 182 const char *entry_point_name, 183 const struct dxil_spirv_debug_options *debug_options, 184 const struct dxil_spirv_runtime_conf *conf, 185 struct dxil_spirv_object *out_dxil); 186 187 /** 188 * Free the buffer allocated by spirv_to_dxil. 189 */ 190 void 191 spirv_to_dxil_free(struct dxil_spirv_object *dxil); 192 193 uint64_t 194 spirv_to_dxil_get_version(void); 195 196 #ifdef __cplusplus 197 } 198 #endif 199 200 #endif 201