• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  /*
25   * See the DirectX Shader Compiler for documentation for DXIL details:
26   * https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst
27   */
28 
29 #ifndef DXIL_MODULE_H
30 #define DXIL_MODULE_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include "dxil_buffer.h"
37 #include "dxil_signature.h"
38 
39 #include "util/list.h"
40 
41 
42 #define DXIL_SHADER_MAX_IO_ROWS 80
43 
44 enum dxil_shader_kind {
45    DXIL_PIXEL_SHADER = 0,
46    DXIL_VERTEX_SHADER = 1,
47    DXIL_GEOMETRY_SHADER = 2,
48    DXIL_HULL_SHADER = 3,
49    DXIL_DOMAIN_SHADER = 4,
50    DXIL_COMPUTE_SHADER = 5,
51 };
52 
53 extern int debug_dxil;
54 
55 enum dxil_debug_flags {
56    DXIL_DEBUG_VERBOSE    = 1 << 0,
57    DXIL_DEBUG_DUMP_BLOB  = 1 << 1,
58    DXIL_DEBUG_TRACE      = 1 << 2,
59    DXIL_DEBUG_DUMP_MODULE = 1 << 3,
60 };
61 
62 enum dxil_bin_opcode {
63    DXIL_BINOP_ADD = 0,
64    DXIL_BINOP_SUB = 1,
65    DXIL_BINOP_MUL = 2,
66    DXIL_BINOP_UDIV = 3,
67    DXIL_BINOP_SDIV = 4,
68    DXIL_BINOP_UREM = 5,
69    DXIL_BINOP_SREM = 6,
70    DXIL_BINOP_SHL = 7,
71    DXIL_BINOP_LSHR = 8,
72    DXIL_BINOP_ASHR = 9,
73    DXIL_BINOP_AND = 10,
74    DXIL_BINOP_OR = 11,
75    DXIL_BINOP_XOR = 12,
76    DXIL_BINOP_INSTR_COUNT
77 };
78 
79 enum dxil_cast_opcode {
80    DXIL_CAST_TRUNC = 0,
81    DXIL_CAST_ZEXT = 1,
82    DXIL_CAST_SEXT = 2,
83    DXIL_CAST_FPTOUI = 3,
84    DXIL_CAST_FPTOSI = 4,
85    DXIL_CAST_UITOFP = 5,
86    DXIL_CAST_SITOFP = 6,
87    DXIL_CAST_FPTRUNC = 7,
88    DXIL_CAST_FPEXT = 8,
89    DXIL_CAST_PTRTOINT = 9,
90    DXIL_CAST_INTTOPTR = 10,
91    DXIL_CAST_BITCAST = 11,
92    DXIL_CAST_ADDRSPACECAST = 12,
93    DXIL_CAST_INSTR_COUNT
94 };
95 
96 enum dxil_cmp_pred {
97    DXIL_FCMP_FALSE = 0,
98    DXIL_FCMP_OEQ = 1,
99    DXIL_FCMP_OGT = 2,
100    DXIL_FCMP_OGE = 3,
101    DXIL_FCMP_OLT = 4,
102    DXIL_FCMP_OLE = 5,
103    DXIL_FCMP_ONE = 6,
104    DXIL_FCMP_ORD = 7,
105    DXIL_FCMP_UNO = 8,
106    DXIL_FCMP_UEQ = 9,
107    DXIL_FCMP_UGT = 10,
108    DXIL_FCMP_UGE = 11,
109    DXIL_FCMP_ULT = 12,
110    DXIL_FCMP_ULE = 13,
111    DXIL_FCMP_UNE = 14,
112    DXIL_FCMP_TRUE = 15,
113    DXIL_ICMP_EQ = 32,
114    DXIL_ICMP_NE = 33,
115    DXIL_ICMP_UGT = 34,
116    DXIL_ICMP_UGE = 35,
117    DXIL_ICMP_ULT = 36,
118    DXIL_ICMP_ULE = 37,
119    DXIL_ICMP_SGT = 38,
120    DXIL_ICMP_SGE = 39,
121    DXIL_ICMP_SLT = 40,
122    DXIL_ICMP_SLE = 41,
123    DXIL_CMP_INSTR_COUNT
124 };
125 
126 enum dxil_opt_flags {
127   DXIL_UNSAFE_ALGEBRA = (1 << 0),
128   DXIL_NO_NANS = (1 << 1),
129   DXIL_NO_INFS = (1 << 2),
130   DXIL_NO_SIGNED_ZEROS = (1 << 3),
131   DXIL_ALLOW_RECIPROCAL = (1 << 4)
132 };
133 
134 struct dxil_features {
135    unsigned doubles : 1,
136             cs_4x_raw_sb : 1,
137             uavs_at_every_stage : 1,
138             use_64uavs : 1,
139             min_precision : 1,
140             dx11_1_double_extensions : 1,
141             dx11_1_shader_extensions : 1,
142             dx9_comparison_filtering : 1,
143             tiled_resources : 1,
144             stencil_ref : 1,
145             inner_coverage : 1,
146             typed_uav_load_additional_formats : 1,
147             rovs : 1,
148             array_layer_from_vs_or_ds : 1,
149             wave_ops : 1,
150             int64_ops : 1,
151             view_id : 1,
152             barycentrics : 1,
153             native_low_precision : 1,
154             shading_rate : 1,
155             raytracing_tier_1_1 : 1,
156             sampler_feedback : 1;
157 };
158 
159 struct dxil_shader_info {
160    unsigned has_out_position:1;
161    unsigned has_out_depth:1;
162    unsigned has_per_sample_input:1;
163 };
164 
165 struct dxil_func_def {
166    struct list_head head;
167    const struct dxil_func *func;
168 
169    struct list_head instr_list;
170    int *basic_block_ids; /* maps from "user" ids to LLVM ids */
171    size_t num_basic_block_ids;
172    unsigned curr_block;
173 };
174 
175 struct dxil_module {
176    void *ralloc_ctx;
177    enum dxil_shader_kind shader_kind;
178    unsigned major_version, minor_version;
179    unsigned major_validator, minor_validator;
180    struct dxil_features feats;
181    unsigned raw_and_structured_buffers : 1;
182    struct dxil_shader_info info;
183 
184    struct dxil_buffer buf;
185 
186    /* The number of entries in the arrays below */
187    unsigned num_sig_inputs;
188    unsigned num_sig_outputs;
189    unsigned num_sig_patch_consts;
190 
191    /* The number of "vectors" of elements. This is used to determine the sizes
192     * of the dependency tables.
193     */
194    unsigned num_psv_inputs;
195    unsigned num_psv_outputs[4];
196    unsigned num_psv_patch_consts;
197 
198    struct dxil_signature_record inputs[DXIL_SHADER_MAX_IO_ROWS];
199    struct dxil_signature_record outputs[DXIL_SHADER_MAX_IO_ROWS];
200    struct dxil_signature_record patch_consts[DXIL_SHADER_MAX_IO_ROWS];
201 
202    /* This array is indexed using var->data.driver_location, which
203     * is not a direct match to IO rows, since a row is a vec4, and
204     * variables can occupy less than that, and several vars can
205     * be packed in a row. Hence the x4, but I doubt we can end up
206     * with more than 80x4 variables in practice. Maybe this array
207     * should be allocated dynamically based on on the maximum
208     * driver_location across all input vars.
209     */
210    unsigned input_mappings[DXIL_SHADER_MAX_IO_ROWS * 4];
211 
212    struct dxil_psv_signature_element psv_inputs[DXIL_SHADER_MAX_IO_ROWS];
213    struct dxil_psv_signature_element psv_outputs[DXIL_SHADER_MAX_IO_ROWS];
214    struct dxil_psv_signature_element psv_patch_consts[DXIL_SHADER_MAX_IO_ROWS];
215 
216    struct _mesa_string_buffer *sem_string_table;
217    struct dxil_psv_sem_index_table sem_index_table;
218 
219    struct {
220       unsigned abbrev_width;
221       intptr_t offset;
222    } blocks[16];
223    size_t num_blocks;
224 
225    struct list_head type_list;
226    struct list_head gvar_list;
227    struct list_head func_list;
228    struct list_head func_def_list;
229    struct list_head attr_set_list;
230    struct list_head const_list;
231    struct list_head mdnode_list;
232    struct list_head md_named_node_list;
233    const struct dxil_type *void_type;
234    const struct dxil_type *int1_type, *int8_type, *int16_type,
235                           *int32_type, *int64_type;
236    const struct dxil_type *float16_type, *float32_type, *float64_type;
237 
238    struct rb_tree *functions;
239 
240    struct dxil_func_def *cur_emitting_func;
241 };
242 
243 struct dxil_instr;
244 struct dxil_value;
245 
246 void
247 dxil_module_init(struct dxil_module *m, void *ralloc_ctx);
248 
249 void
250 dxil_module_release(struct dxil_module *m);
251 
252 const struct dxil_value *
253 dxil_add_global_var(struct dxil_module *m, const char *name,
254                     const struct dxil_type *type,
255                     enum dxil_address_space as, int align,
256                     const struct dxil_value *value);
257 
258 const struct dxil_value *
259 dxil_add_global_ptr_var(struct dxil_module *m, const char *name,
260                         const struct dxil_type *type,
261                         enum dxil_address_space as, int align,
262                         const struct dxil_value *value);
263 
264 struct dxil_func_def *
265 dxil_add_function_def(struct dxil_module *m, const char *name,
266                       const struct dxil_type *type, unsigned num_blocks);
267 
268 const struct dxil_func *
269 dxil_add_function_decl(struct dxil_module *m, const char *name,
270                        const struct dxil_type *type,
271                        enum dxil_attr_kind attr);
272 
273 const struct dxil_type *
274 dxil_module_get_void_type(struct dxil_module *m);
275 
276 const struct dxil_type *
277 dxil_module_get_int_type(struct dxil_module *m, unsigned bit_size);
278 
279 const struct dxil_type *
280 dxil_module_get_float_type(struct dxil_module *m, unsigned bit_size);
281 
282 const struct dxil_type *
283 dxil_module_get_pointer_type(struct dxil_module *m,
284                              const struct dxil_type *target);
285 
286 const struct dxil_type *
287 dxil_get_overload_type(struct dxil_module *mod, enum overload_type overload);
288 
289 const struct dxil_type *
290 dxil_module_get_handle_type(struct dxil_module *m);
291 
292 const struct dxil_type *
293 dxil_module_get_cbuf_ret_type(struct dxil_module *mod, enum overload_type overload);
294 
295 const struct dxil_type *
296 dxil_module_get_split_double_ret_type(struct dxil_module *mod);
297 
298 const struct dxil_type *
299 dxil_module_get_res_type(struct dxil_module *m, enum dxil_resource_kind kind,
300                          enum dxil_component_type comp_type, bool readwrite);
301 
302 const struct dxil_type *
303 dxil_module_get_resret_type(struct dxil_module *m, enum overload_type overload);
304 
305 const struct dxil_type *
306 dxil_module_get_dimret_type(struct dxil_module *m);
307 
308 const struct dxil_type *
309 dxil_module_get_samplepos_type(struct dxil_module *m);
310 
311 const struct dxil_type *
312 dxil_module_get_struct_type(struct dxil_module *m,
313                             const char *name,
314                             const struct dxil_type **elem_types,
315                             size_t num_elem_types);
316 
317 const struct dxil_type *
318 dxil_module_get_array_type(struct dxil_module *m,
319                            const struct dxil_type *elem_type,
320                            size_t num_elems);
321 
322 const struct dxil_type *
323 dxil_module_get_vector_type(struct dxil_module *m,
324                             const struct dxil_type *elem_type,
325                             size_t num_elems);
326 
327 const struct dxil_type *
328 dxil_module_add_function_type(struct dxil_module *m,
329                               const struct dxil_type *ret_type,
330                               const struct dxil_type **arg_types,
331                               size_t num_arg_types);
332 
333 nir_alu_type
334 dxil_type_to_nir_type(const struct dxil_type *type);
335 
336 bool
337 dxil_value_type_equal_to(const struct dxil_value *value,
338                          const struct dxil_type *lhs);
339 
340 bool
341 dxil_value_type_bitsize_equal_to(const struct dxil_value *value, unsigned bitsize);
342 
343 const struct dxil_type *
344 dxil_value_get_type(const struct dxil_value *value);
345 
346 const struct dxil_value *
347 dxil_module_get_int1_const(struct dxil_module *m, bool value);
348 
349 const struct dxil_value *
350 dxil_module_get_int8_const(struct dxil_module *m, int8_t value);
351 
352 const struct dxil_value *
353 dxil_module_get_int16_const(struct dxil_module *m, int16_t value);
354 
355 const struct dxil_value *
356 dxil_module_get_int32_const(struct dxil_module *m, int32_t value);
357 
358 const struct dxil_value *
359 dxil_module_get_int64_const(struct dxil_module *m, int64_t value);
360 
361 const struct dxil_value *
362 dxil_module_get_int_const(struct dxil_module *m, intmax_t value,
363                           unsigned bit_size);
364 
365 const struct dxil_value *
366 dxil_module_get_float16_const(struct dxil_module *m, uint16_t);
367 
368 const struct dxil_value *
369 dxil_module_get_float_const(struct dxil_module *m, float value);
370 
371 const struct dxil_value *
372 dxil_module_get_double_const(struct dxil_module *m, double value);
373 
374 const struct dxil_value *
375 dxil_module_get_array_const(struct dxil_module *m, const struct dxil_type *type,
376                             const struct dxil_value **values);
377 
378 const struct dxil_value *
379 dxil_module_get_undef(struct dxil_module *m, const struct dxil_type *type);
380 
381 const struct dxil_mdnode *
382 dxil_get_metadata_string(struct dxil_module *m, const char *str);
383 
384 const struct dxil_mdnode *
385 dxil_get_metadata_value(struct dxil_module *m, const struct dxil_type *type,
386                         const struct dxil_value *value);
387 
388 const struct dxil_mdnode *
389 dxil_get_metadata_func(struct dxil_module *m, const struct dxil_func *func);
390 
391 const struct dxil_mdnode *
392 dxil_get_metadata_int1(struct dxil_module *m, bool value);
393 
394 const struct dxil_mdnode *
395 dxil_get_metadata_int8(struct dxil_module *m, int8_t value);
396 
397 const struct dxil_mdnode *
398 dxil_get_metadata_int32(struct dxil_module *m, int32_t value);
399 
400 const struct dxil_mdnode *
401 dxil_get_metadata_int64(struct dxil_module *m, int64_t value);
402 
403 const struct dxil_mdnode *
404 dxil_get_metadata_float32(struct dxil_module *m, float value);
405 
406 const struct dxil_mdnode *
407 dxil_get_metadata_node(struct dxil_module *m,
408                        const struct dxil_mdnode *subnodes[],
409                        size_t num_subnodes);
410 
411 bool
412 dxil_add_metadata_named_node(struct dxil_module *m, const char *name,
413                              const struct dxil_mdnode *subnodes[],
414                              size_t num_subnodes);
415 
416 const struct dxil_value *
417 dxil_emit_binop(struct dxil_module *m, enum dxil_bin_opcode opcode,
418                 const struct dxil_value *op0, const struct dxil_value *op1,
419                 enum dxil_opt_flags flags);
420 
421 const struct dxil_value *
422 dxil_emit_cmp(struct dxil_module *m, enum dxil_cmp_pred pred,
423               const struct dxil_value *op0, const struct dxil_value *op1);
424 
425 const struct dxil_value *
426 dxil_emit_select(struct dxil_module *m,
427                 const struct dxil_value *op0,
428                 const struct dxil_value *op1,
429                 const struct dxil_value *op2);
430 
431 const struct dxil_value *
432 dxil_emit_extractval(struct dxil_module *m, const struct dxil_value *src,
433                      const unsigned int index);
434 
435 const struct dxil_value *
436 dxil_emit_cast(struct dxil_module *m, enum dxil_cast_opcode opcode,
437                const struct dxil_type *type,
438                const struct dxil_value *value);
439 
440 bool
441 dxil_emit_branch(struct dxil_module *m, const struct dxil_value *cond,
442                  unsigned true_block, unsigned false_block);
443 
444 const struct dxil_value *
445 dxil_instr_get_return_value(struct dxil_instr *instr);
446 
447 struct dxil_instr *
448 dxil_emit_phi(struct dxil_module *m, const struct dxil_type *type);
449 
450 bool
451 dxil_phi_add_incoming(struct dxil_instr *instr,
452                       const struct dxil_value *incoming_values[],
453                       const unsigned incoming_blocks[],
454                       size_t num_incoming);
455 
456 const struct dxil_value *
457 dxil_emit_call(struct dxil_module *m,
458                const struct dxil_func *func,
459                const struct dxil_value **args, size_t num_args);
460 
461 bool
462 dxil_emit_call_void(struct dxil_module *m,
463                     const struct dxil_func *func,
464                     const struct dxil_value **args, size_t num_args);
465 
466 bool
467 dxil_emit_ret_void(struct dxil_module *m);
468 
469 const struct dxil_value *
470 dxil_emit_alloca(struct dxil_module *m, const struct dxil_type *alloc_type,
471                  const struct dxil_type *size_type,
472                  const struct dxil_value *size,
473                  unsigned int align);
474 
475 const struct dxil_value *
476 dxil_emit_gep_inbounds(struct dxil_module *m,
477                        const struct dxil_value **operands,
478                        size_t num_operands);
479 
480 const struct dxil_value *
481 dxil_emit_load(struct dxil_module *m, const struct dxil_value *ptr,
482                unsigned align,
483                bool is_volatile);
484 
485 bool
486 dxil_emit_store(struct dxil_module *m, const struct dxil_value *value,
487                 const struct dxil_value *ptr, unsigned align,
488                 bool is_volatile);
489 
490 const struct dxil_value *
491 dxil_emit_cmpxchg(struct dxil_module *m, const struct dxil_value *cmpval,
492                   const struct dxil_value *newval,
493                   const struct dxil_value *ptr, bool is_volatile,
494                   enum dxil_atomic_ordering ordering,
495                   enum dxil_sync_scope syncscope);
496 
497 const struct dxil_value *
498 dxil_emit_atomicrmw(struct dxil_module *m, const struct dxil_value *value,
499                     const struct dxil_value *ptr, enum dxil_rmw_op op,
500                     bool is_volatile, enum dxil_atomic_ordering ordering,
501                     enum dxil_sync_scope syncscope);
502 
503 bool
504 dxil_emit_module(struct dxil_module *m);
505 
506 #ifdef __cplusplus
507 }
508 #endif
509 
510 #endif
511