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 = 6,
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 };
113 
114 enum dxil_spirv_yz_flip_mode {
115    DXIL_SPIRV_YZ_FLIP_NONE = 0,
116    // Y-flip is unconditional: pos.y = -pos.y
117    // Z-flip is unconditional: pos.z = -pos.z + 1.0f
118    DXIL_SPIRV_Y_FLIP_UNCONDITIONAL = 1 << 0,
119    DXIL_SPIRV_Z_FLIP_UNCONDITIONAL = 1 << 1,
120    DXIL_SPIRV_YZ_FLIP_UNCONDITIONAL = DXIL_SPIRV_Y_FLIP_UNCONDITIONAL | DXIL_SPIRV_Z_FLIP_UNCONDITIONAL,
121    // Y-flip/Z-flip info are passed through a sysval
122    DXIL_SPIRV_Y_FLIP_CONDITIONAL = 1 << 2,
123    DXIL_SPIRV_Z_FLIP_CONDITIONAL = 1 << 3,
124    DXIL_SPIRV_YZ_FLIP_CONDITIONAL = DXIL_SPIRV_Y_FLIP_CONDITIONAL | DXIL_SPIRV_Z_FLIP_CONDITIONAL,
125 };
126 
127 struct dxil_spirv_vulkan_binding {
128    uint32_t base_register;
129 };
130 
131 struct dxil_spirv_vulkan_descriptor_set {
132    uint32_t binding_count;
133    struct dxil_spirv_vulkan_binding *bindings;
134 };
135 
136 #define DXIL_SPIRV_MAX_VIEWPORT 16
137 
138 struct dxil_spirv_runtime_conf {
139    struct {
140       uint32_t register_space;
141       uint32_t base_shader_register;
142    } runtime_data_cbv;
143 
144    struct {
145       uint32_t register_space;
146       uint32_t base_shader_register;
147    } push_constant_cbv;
148 
149    uint32_t descriptor_set_count;
150    struct dxil_spirv_vulkan_descriptor_set *descriptor_sets;
151 
152    // Set true if vertex and instance ids have already been converted to
153    // zero-based. Otherwise, runtime_data will be required to lower them.
154    bool zero_based_vertex_instance_id;
155 
156    struct {
157       // mode != DXIL_SPIRV_YZ_FLIP_NONE only valid on vertex/geometry stages.
158       enum dxil_spirv_yz_flip_mode mode;
159 
160       // Y/Z flip masks (one bit per viewport)
161       uint16_t y_mask;
162       uint16_t z_mask;
163    } yz_flip;
164 
165    // The caller supports read-only images to be turned into SRV accesses,
166    // which allows us to run the nir_opt_access() pass
167    bool read_only_images_as_srvs;
168 };
169 
170 struct dxil_spirv_debug_options {
171    bool dump_nir;
172 };
173 
174 /**
175  * Compile a SPIR-V module into DXIL.
176  * \param  words  SPIR-V module to compile
177  * \param  word_count  number of words in the SPIR-V module
178  * \param  specializations  specialization constants to compile with the shader
179  * \param  num_specializations  number of specialization constants
180  * \param  stage  shader stage
181  * \param  entry_point_name  name of shader entrypoint
182  * \param  conf  configuration for spriv_to_dxil
183  * \param  out_dxil  will contain the DXIL bytes on success (call spirv_to_dxil_free after use)
184  * \return  true if compilation succeeded
185  */
186 bool
187 spirv_to_dxil(const uint32_t *words, size_t word_count,
188               struct dxil_spirv_specialization *specializations,
189               unsigned int num_specializations, dxil_spirv_shader_stage stage,
190               const char *entry_point_name,
191               const struct dxil_spirv_debug_options *debug_options,
192               const struct dxil_spirv_runtime_conf *conf,
193               struct dxil_spirv_object *out_dxil);
194 
195 /**
196  * Free the buffer allocated by spirv_to_dxil.
197  */
198 void
199 spirv_to_dxil_free(struct dxil_spirv_object *dxil);
200 
201 uint64_t
202 spirv_to_dxil_get_version(void);
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif
209