forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnir_to_dxil.c
6173 lines (5269 loc) · 212 KB
/
nir_to_dxil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir_to_dxil.h"
#include "dxil_container.h"
#include "dxil_dump.h"
#include "dxil_enums.h"
#include "dxil_function.h"
#include "dxil_module.h"
#include "dxil_nir.h"
#include "dxil_signature.h"
#include "nir/nir_builder.h"
#include "util/ralloc.h"
#include "util/u_debug.h"
#include "util/u_dynarray.h"
#include "util/u_math.h"
#include "git_sha1.h"
#include "vulkan/vulkan_core.h"
#include <stdint.h>
int debug_dxil = 0;
static const struct debug_named_value
dxil_debug_options[] = {
{ "verbose", DXIL_DEBUG_VERBOSE, NULL },
{ "dump_blob", DXIL_DEBUG_DUMP_BLOB , "Write shader blobs" },
{ "trace", DXIL_DEBUG_TRACE , "Trace instruction conversion" },
{ "dump_module", DXIL_DEBUG_DUMP_MODULE, "dump module tree to stderr"},
DEBUG_NAMED_VALUE_END
};
DEBUG_GET_ONCE_FLAGS_OPTION(debug_dxil, "DXIL_DEBUG", dxil_debug_options, 0)
static void
log_nir_instr_unsupported(const struct dxil_logger *logger,
const char *message_prefix, const nir_instr *instr)
{
char *msg = NULL;
char *instr_str = nir_instr_as_str(instr, NULL);
asprintf(&msg, "%s: %s\n", message_prefix, instr_str);
ralloc_free(instr_str);
assert(msg);
logger->log(logger->priv, msg);
free(msg);
}
static void
default_logger_func(void *priv, const char *msg)
{
fprintf(stderr, "%s", msg);
unreachable("Unhandled error");
}
static const struct dxil_logger default_logger = { .priv = NULL, .log = default_logger_func };
#define TRACE_CONVERSION(instr) \
if (debug_dxil & DXIL_DEBUG_TRACE) \
do { \
fprintf(stderr, "Convert '"); \
nir_print_instr(instr, stderr); \
fprintf(stderr, "'\n"); \
} while (0)
static const nir_shader_compiler_options
nir_options = {
.lower_ineg = true,
.lower_fneg = true,
.lower_ffma16 = true,
.lower_ffma32 = true,
.lower_isign = true,
.lower_fsign = true,
.lower_iabs = true,
.lower_fmod = true,
.lower_fpow = true,
.lower_scmp = true,
.lower_ldexp = true,
.lower_flrp16 = true,
.lower_flrp32 = true,
.lower_flrp64 = true,
.lower_bitfield_extract = true,
.lower_find_msb_to_reverse = true,
.lower_extract_word = true,
.lower_extract_byte = true,
.lower_insert_word = true,
.lower_insert_byte = true,
.lower_all_io_to_elements = true,
.lower_all_io_to_temps = true,
.lower_hadd = true,
.lower_uadd_sat = true,
.lower_usub_sat = true,
.lower_iadd_sat = true,
.lower_uadd_carry = true,
.lower_mul_high = true,
.lower_rotate = true,
.lower_pack_64_2x32_split = true,
.lower_pack_32_2x16_split = true,
.lower_unpack_64_2x32_split = true,
.lower_unpack_32_2x16_split = true,
.lower_unpack_half_2x16 = true,
.lower_unpack_snorm_2x16 = true,
.lower_unpack_snorm_4x8 = true,
.lower_unpack_unorm_2x16 = true,
.lower_unpack_unorm_4x8 = true,
.lower_interpolate_at = true,
.has_fsub = true,
.has_isub = true,
.use_scoped_barrier = true,
.vertex_id_zero_based = true,
.lower_base_vertex = true,
.lower_helper_invocation = true,
.has_cs_global_id = true,
.has_txs = true,
.lower_mul_2x32_64 = true,
.lower_doubles_options =
nir_lower_drcp |
nir_lower_dsqrt |
nir_lower_drsq |
nir_lower_dfract |
nir_lower_dtrunc |
nir_lower_dfloor |
nir_lower_dceil |
nir_lower_dround_even,
.max_unroll_iterations = 32, /* arbitrary */
.force_indirect_unrolling = (nir_var_shader_in | nir_var_shader_out | nir_var_function_temp),
};
const nir_shader_compiler_options*
dxil_get_nir_compiler_options(void)
{
return &nir_options;
}
static bool
emit_llvm_ident(struct dxil_module *m)
{
const struct dxil_mdnode *compiler = dxil_get_metadata_string(m, "Mesa version " PACKAGE_VERSION MESA_GIT_SHA1);
if (!compiler)
return false;
const struct dxil_mdnode *llvm_ident = dxil_get_metadata_node(m, &compiler, 1);
return llvm_ident &&
dxil_add_metadata_named_node(m, "llvm.ident", &llvm_ident, 1);
}
static bool
emit_named_version(struct dxil_module *m, const char *name,
int major, int minor)
{
const struct dxil_mdnode *major_node = dxil_get_metadata_int32(m, major);
const struct dxil_mdnode *minor_node = dxil_get_metadata_int32(m, minor);
const struct dxil_mdnode *version_nodes[] = { major_node, minor_node };
const struct dxil_mdnode *version = dxil_get_metadata_node(m, version_nodes,
ARRAY_SIZE(version_nodes));
return dxil_add_metadata_named_node(m, name, &version, 1);
}
static const char *
get_shader_kind_str(enum dxil_shader_kind kind)
{
switch (kind) {
case DXIL_PIXEL_SHADER:
return "ps";
case DXIL_VERTEX_SHADER:
return "vs";
case DXIL_GEOMETRY_SHADER:
return "gs";
case DXIL_HULL_SHADER:
return "hs";
case DXIL_DOMAIN_SHADER:
return "ds";
case DXIL_COMPUTE_SHADER:
return "cs";
default:
unreachable("invalid shader kind");
}
}
static bool
emit_dx_shader_model(struct dxil_module *m)
{
const struct dxil_mdnode *type_node = dxil_get_metadata_string(m, get_shader_kind_str(m->shader_kind));
const struct dxil_mdnode *major_node = dxil_get_metadata_int32(m, m->major_version);
const struct dxil_mdnode *minor_node = dxil_get_metadata_int32(m, m->minor_version);
const struct dxil_mdnode *shader_model[] = { type_node, major_node,
minor_node };
const struct dxil_mdnode *dx_shader_model = dxil_get_metadata_node(m, shader_model, ARRAY_SIZE(shader_model));
return dxil_add_metadata_named_node(m, "dx.shaderModel",
&dx_shader_model, 1);
}
enum {
DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG = 0,
DXIL_STRUCTURED_BUFFER_ELEMENT_STRIDE_TAG = 1
};
enum dxil_intr {
DXIL_INTR_LOAD_INPUT = 4,
DXIL_INTR_STORE_OUTPUT = 5,
DXIL_INTR_FABS = 6,
DXIL_INTR_SATURATE = 7,
DXIL_INTR_ISFINITE = 10,
DXIL_INTR_ISNORMAL = 11,
DXIL_INTR_FCOS = 12,
DXIL_INTR_FSIN = 13,
DXIL_INTR_FEXP2 = 21,
DXIL_INTR_FRC = 22,
DXIL_INTR_FLOG2 = 23,
DXIL_INTR_SQRT = 24,
DXIL_INTR_RSQRT = 25,
DXIL_INTR_ROUND_NE = 26,
DXIL_INTR_ROUND_NI = 27,
DXIL_INTR_ROUND_PI = 28,
DXIL_INTR_ROUND_Z = 29,
DXIL_INTR_BFREV = 30,
DXIL_INTR_COUNTBITS = 31,
DXIL_INTR_FIRSTBIT_LO = 32,
DXIL_INTR_FIRSTBIT_HI = 33,
DXIL_INTR_FIRSTBIT_SHI = 34,
DXIL_INTR_FMAX = 35,
DXIL_INTR_FMIN = 36,
DXIL_INTR_IMAX = 37,
DXIL_INTR_IMIN = 38,
DXIL_INTR_UMAX = 39,
DXIL_INTR_UMIN = 40,
DXIL_INTR_FMA = 47,
DXIL_INTR_IBFE = 51,
DXIL_INTR_UBFE = 52,
DXIL_INTR_BFI = 53,
DXIL_INTR_CREATE_HANDLE = 57,
DXIL_INTR_CBUFFER_LOAD_LEGACY = 59,
DXIL_INTR_SAMPLE = 60,
DXIL_INTR_SAMPLE_BIAS = 61,
DXIL_INTR_SAMPLE_LEVEL = 62,
DXIL_INTR_SAMPLE_GRAD = 63,
DXIL_INTR_SAMPLE_CMP = 64,
DXIL_INTR_SAMPLE_CMP_LVL_ZERO = 65,
DXIL_INTR_TEXTURE_LOAD = 66,
DXIL_INTR_TEXTURE_STORE = 67,
DXIL_INTR_BUFFER_LOAD = 68,
DXIL_INTR_BUFFER_STORE = 69,
DXIL_INTR_TEXTURE_SIZE = 72,
DXIL_INTR_TEXTURE_GATHER = 73,
DXIL_INTR_TEXTURE_GATHER_CMP = 74,
DXIL_INTR_TEXTURE2DMS_GET_SAMPLE_POSITION = 75,
DXIL_INTR_RENDER_TARGET_GET_SAMPLE_POSITION = 76,
DXIL_INTR_RENDER_TARGET_GET_SAMPLE_COUNT = 77,
DXIL_INTR_ATOMIC_BINOP = 78,
DXIL_INTR_ATOMIC_CMPXCHG = 79,
DXIL_INTR_BARRIER = 80,
DXIL_INTR_TEXTURE_LOD = 81,
DXIL_INTR_DISCARD = 82,
DXIL_INTR_DDX_COARSE = 83,
DXIL_INTR_DDY_COARSE = 84,
DXIL_INTR_DDX_FINE = 85,
DXIL_INTR_DDY_FINE = 86,
DXIL_INTR_EVAL_SNAPPED = 87,
DXIL_INTR_EVAL_SAMPLE_INDEX = 88,
DXIL_INTR_EVAL_CENTROID = 89,
DXIL_INTR_SAMPLE_INDEX = 90,
DXIL_INTR_COVERAGE = 91,
DXIL_INTR_THREAD_ID = 93,
DXIL_INTR_GROUP_ID = 94,
DXIL_INTR_THREAD_ID_IN_GROUP = 95,
DXIL_INTR_FLATTENED_THREAD_ID_IN_GROUP = 96,
DXIL_INTR_EMIT_STREAM = 97,
DXIL_INTR_CUT_STREAM = 98,
DXIL_INTR_GS_INSTANCE_ID = 100,
DXIL_INTR_MAKE_DOUBLE = 101,
DXIL_INTR_SPLIT_DOUBLE = 102,
DXIL_INTR_LOAD_OUTPUT_CONTROL_POINT = 103,
DXIL_INTR_LOAD_PATCH_CONSTANT = 104,
DXIL_INTR_DOMAIN_LOCATION = 105,
DXIL_INTR_STORE_PATCH_CONSTANT = 106,
DXIL_INTR_OUTPUT_CONTROL_POINT_ID = 107,
DXIL_INTR_PRIMITIVE_ID = 108,
DXIL_INTR_LEGACY_F32TOF16 = 130,
DXIL_INTR_LEGACY_F16TOF32 = 131,
DXIL_INTR_ATTRIBUTE_AT_VERTEX = 137,
DXIL_INTR_ANNOTATE_HANDLE = 216,
DXIL_INTR_CREATE_HANDLE_FROM_BINDING = 217,
};
enum dxil_atomic_op {
DXIL_ATOMIC_ADD = 0,
DXIL_ATOMIC_AND = 1,
DXIL_ATOMIC_OR = 2,
DXIL_ATOMIC_XOR = 3,
DXIL_ATOMIC_IMIN = 4,
DXIL_ATOMIC_IMAX = 5,
DXIL_ATOMIC_UMIN = 6,
DXIL_ATOMIC_UMAX = 7,
DXIL_ATOMIC_EXCHANGE = 8,
};
typedef struct {
unsigned id;
unsigned binding;
unsigned size;
unsigned space;
} resource_array_layout;
static void
fill_resource_metadata(struct dxil_module *m, const struct dxil_mdnode **fields,
const struct dxil_type *struct_type,
const char *name, const resource_array_layout *layout)
{
const struct dxil_type *pointer_type = dxil_module_get_pointer_type(m, struct_type);
const struct dxil_value *pointer_undef = dxil_module_get_undef(m, pointer_type);
fields[0] = dxil_get_metadata_int32(m, layout->id); // resource ID
fields[1] = dxil_get_metadata_value(m, pointer_type, pointer_undef); // global constant symbol
fields[2] = dxil_get_metadata_string(m, name ? name : ""); // name
fields[3] = dxil_get_metadata_int32(m, layout->space); // space ID
fields[4] = dxil_get_metadata_int32(m, layout->binding); // lower bound
fields[5] = dxil_get_metadata_int32(m, layout->size); // range size
}
static const struct dxil_mdnode *
emit_srv_metadata(struct dxil_module *m, const struct dxil_type *elem_type,
const char *name, const resource_array_layout *layout,
enum dxil_component_type comp_type,
enum dxil_resource_kind res_kind)
{
const struct dxil_mdnode *fields[9];
const struct dxil_mdnode *metadata_tag_nodes[2];
fill_resource_metadata(m, fields, elem_type, name, layout);
fields[6] = dxil_get_metadata_int32(m, res_kind); // resource shape
fields[7] = dxil_get_metadata_int1(m, 0); // sample count
if (res_kind != DXIL_RESOURCE_KIND_RAW_BUFFER &&
res_kind != DXIL_RESOURCE_KIND_STRUCTURED_BUFFER) {
metadata_tag_nodes[0] = dxil_get_metadata_int32(m, DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG);
metadata_tag_nodes[1] = dxil_get_metadata_int32(m, comp_type);
fields[8] = dxil_get_metadata_node(m, metadata_tag_nodes, ARRAY_SIZE(metadata_tag_nodes)); // metadata
} else if (res_kind == DXIL_RESOURCE_KIND_RAW_BUFFER)
fields[8] = NULL;
else
unreachable("Structured buffers not supported yet");
return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}
static const struct dxil_mdnode *
emit_uav_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
const char *name, const resource_array_layout *layout,
enum dxil_component_type comp_type,
enum dxil_resource_kind res_kind)
{
const struct dxil_mdnode *fields[11];
const struct dxil_mdnode *metadata_tag_nodes[2];
fill_resource_metadata(m, fields, struct_type, name, layout);
fields[6] = dxil_get_metadata_int32(m, res_kind); // resource shape
fields[7] = dxil_get_metadata_int1(m, false); // globally-coherent
fields[8] = dxil_get_metadata_int1(m, false); // has counter
fields[9] = dxil_get_metadata_int1(m, false); // is ROV
if (res_kind != DXIL_RESOURCE_KIND_RAW_BUFFER &&
res_kind != DXIL_RESOURCE_KIND_STRUCTURED_BUFFER) {
metadata_tag_nodes[0] = dxil_get_metadata_int32(m, DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG);
metadata_tag_nodes[1] = dxil_get_metadata_int32(m, comp_type);
fields[10] = dxil_get_metadata_node(m, metadata_tag_nodes, ARRAY_SIZE(metadata_tag_nodes)); // metadata
} else if (res_kind == DXIL_RESOURCE_KIND_RAW_BUFFER)
fields[10] = NULL;
else
unreachable("Structured buffers not supported yet");
return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}
static const struct dxil_mdnode *
emit_cbv_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
const char *name, const resource_array_layout *layout,
unsigned size)
{
const struct dxil_mdnode *fields[8];
fill_resource_metadata(m, fields, struct_type, name, layout);
fields[6] = dxil_get_metadata_int32(m, size); // constant buffer size
fields[7] = NULL; // metadata
return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}
static const struct dxil_mdnode *
emit_sampler_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
nir_variable *var, const resource_array_layout *layout)
{
const struct dxil_mdnode *fields[8];
const struct glsl_type *type = glsl_without_array(var->type);
fill_resource_metadata(m, fields, struct_type, var->name, layout);
enum dxil_sampler_kind sampler_kind = glsl_sampler_type_is_shadow(type) ?
DXIL_SAMPLER_KIND_COMPARISON : DXIL_SAMPLER_KIND_DEFAULT;
fields[6] = dxil_get_metadata_int32(m, sampler_kind); // sampler kind
fields[7] = NULL; // metadata
return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}
#define MAX_SRVS 128
#define MAX_UAVS 64
#define MAX_CBVS 64 // ??
#define MAX_SAMPLERS 64 // ??
struct dxil_def {
const struct dxil_value *chans[NIR_MAX_VEC_COMPONENTS];
};
struct ntd_context {
void *ralloc_ctx;
const struct nir_to_dxil_options *opts;
struct nir_shader *shader;
struct dxil_module mod;
struct util_dynarray srv_metadata_nodes;
const struct dxil_value *srv_handles[MAX_SRVS];
struct util_dynarray uav_metadata_nodes;
const struct dxil_value *ssbo_handles[MAX_UAVS];
const struct dxil_value *image_handles[MAX_UAVS];
uint32_t num_uavs;
struct util_dynarray cbv_metadata_nodes;
const struct dxil_value *cbv_handles[MAX_CBVS];
struct util_dynarray sampler_metadata_nodes;
const struct dxil_value *sampler_handles[MAX_SAMPLERS];
struct util_dynarray resources;
const struct dxil_mdnode *shader_property_nodes[6];
size_t num_shader_property_nodes;
struct dxil_def *defs;
unsigned num_defs;
struct hash_table *phis;
const struct dxil_value *sharedvars;
const struct dxil_value *scratchvars;
struct hash_table *consts;
nir_variable *ps_front_face;
nir_variable *system_value[SYSTEM_VALUE_MAX];
nir_function *tess_ctrl_patch_constant_func;
unsigned tess_input_control_point_count;
struct dxil_func_def *main_func_def;
struct dxil_func_def *tess_ctrl_patch_constant_func_def;
unsigned unnamed_ubo_count;
const struct dxil_logger *logger;
};
static const char*
unary_func_name(enum dxil_intr intr)
{
switch (intr) {
case DXIL_INTR_COUNTBITS:
case DXIL_INTR_FIRSTBIT_HI:
case DXIL_INTR_FIRSTBIT_SHI:
case DXIL_INTR_FIRSTBIT_LO:
return "dx.op.unaryBits";
case DXIL_INTR_ISFINITE:
case DXIL_INTR_ISNORMAL:
return "dx.op.isSpecialFloat";
default:
return "dx.op.unary";
}
}
static const struct dxil_value *
emit_unary_call(struct ntd_context *ctx, enum overload_type overload,
enum dxil_intr intr,
const struct dxil_value *op0)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod,
unary_func_name(intr),
overload);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
op0
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_binary_call(struct ntd_context *ctx, enum overload_type overload,
enum dxil_intr intr,
const struct dxil_value *op0, const struct dxil_value *op1)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.binary", overload);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
op0,
op1
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_tertiary_call(struct ntd_context *ctx, enum overload_type overload,
enum dxil_intr intr,
const struct dxil_value *op0,
const struct dxil_value *op1,
const struct dxil_value *op2)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.tertiary", overload);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
op0,
op1,
op2
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_quaternary_call(struct ntd_context *ctx, enum overload_type overload,
enum dxil_intr intr,
const struct dxil_value *op0,
const struct dxil_value *op1,
const struct dxil_value *op2,
const struct dxil_value *op3)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.quaternary", overload);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
op0,
op1,
op2,
op3
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_threadid_call(struct ntd_context *ctx, const struct dxil_value *comp)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.threadId", DXIL_I32);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_THREAD_ID);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
comp
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_threadidingroup_call(struct ntd_context *ctx,
const struct dxil_value *comp)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.threadIdInGroup", DXIL_I32);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_THREAD_ID_IN_GROUP);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
comp
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_flattenedthreadidingroup_call(struct ntd_context *ctx)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.flattenedThreadIdInGroup", DXIL_I32);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_FLATTENED_THREAD_ID_IN_GROUP);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_groupid_call(struct ntd_context *ctx, const struct dxil_value *comp)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.groupId", DXIL_I32);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_GROUP_ID);
if (!opcode)
return NULL;
const struct dxil_value *args[] = {
opcode,
comp
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_bufferload_call(struct ntd_context *ctx,
const struct dxil_value *handle,
const struct dxil_value *coord[2],
enum overload_type overload)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.bufferLoad", overload);
if (!func)
return NULL;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_BUFFER_LOAD);
const struct dxil_value *args[] = { opcode, handle, coord[0], coord[1] };
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static bool
emit_bufferstore_call(struct ntd_context *ctx,
const struct dxil_value *handle,
const struct dxil_value *coord[2],
const struct dxil_value *value[4],
const struct dxil_value *write_mask,
enum overload_type overload)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.bufferStore", overload);
if (!func)
return false;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_BUFFER_STORE);
const struct dxil_value *args[] = {
opcode, handle, coord[0], coord[1],
value[0], value[1], value[2], value[3],
write_mask
};
return dxil_emit_call_void(&ctx->mod, func,
args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_textureload_call(struct ntd_context *ctx,
const struct dxil_value *handle,
const struct dxil_value *coord[3],
enum overload_type overload)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.textureLoad", overload);
if (!func)
return NULL;
const struct dxil_type *int_type = dxil_module_get_int_type(&ctx->mod, 32);
const struct dxil_value *int_undef = dxil_module_get_undef(&ctx->mod, int_type);
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_TEXTURE_LOAD);
const struct dxil_value *args[] = { opcode, handle,
/*lod_or_sample*/ int_undef,
coord[0], coord[1], coord[2],
/* offsets */ int_undef, int_undef, int_undef};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static bool
emit_texturestore_call(struct ntd_context *ctx,
const struct dxil_value *handle,
const struct dxil_value *coord[3],
const struct dxil_value *value[4],
const struct dxil_value *write_mask,
enum overload_type overload)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.textureStore", overload);
if (!func)
return false;
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
DXIL_INTR_TEXTURE_STORE);
const struct dxil_value *args[] = {
opcode, handle, coord[0], coord[1], coord[2],
value[0], value[1], value[2], value[3],
write_mask
};
return dxil_emit_call_void(&ctx->mod, func,
args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_atomic_binop(struct ntd_context *ctx,
const struct dxil_value *handle,
enum dxil_atomic_op atomic_op,
const struct dxil_value *coord[3],
const struct dxil_value *value)
{
const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.atomicBinOp", DXIL_I32);
if (!func)
return false;
const struct dxil_value *opcode =
dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_ATOMIC_BINOP);
const struct dxil_value *atomic_op_value =
dxil_module_get_int32_const(&ctx->mod, atomic_op);
const struct dxil_value *args[] = {
opcode, handle, atomic_op_value,
coord[0], coord[1], coord[2], value
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_atomic_cmpxchg(struct ntd_context *ctx,
const struct dxil_value *handle,
const struct dxil_value *coord[3],
const struct dxil_value *cmpval,
const struct dxil_value *newval)
{
const struct dxil_func *func =
dxil_get_function(&ctx->mod, "dx.op.atomicCompareExchange", DXIL_I32);
if (!func)
return false;
const struct dxil_value *opcode =
dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_ATOMIC_CMPXCHG);
const struct dxil_value *args[] = {
opcode, handle, coord[0], coord[1], coord[2], cmpval, newval
};
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_createhandle_call_pre_6_6(struct ntd_context *ctx,
enum dxil_resource_class resource_class,
unsigned lower_bound,
unsigned upper_bound,
unsigned space,
unsigned resource_range_id,
const struct dxil_value *resource_range_index,
bool non_uniform_resource_index)
{
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_CREATE_HANDLE);
const struct dxil_value *resource_class_value = dxil_module_get_int8_const(&ctx->mod, resource_class);
const struct dxil_value *resource_range_id_value = dxil_module_get_int32_const(&ctx->mod, resource_range_id);
const struct dxil_value *non_uniform_resource_index_value = dxil_module_get_int1_const(&ctx->mod, non_uniform_resource_index);
if (!opcode || !resource_class_value || !resource_range_id_value ||
!non_uniform_resource_index_value)
return NULL;
const struct dxil_value *args[] = {
opcode,
resource_class_value,
resource_range_id_value,
resource_range_index,
non_uniform_resource_index_value
};
const struct dxil_func *func =
dxil_get_function(&ctx->mod, "dx.op.createHandle", DXIL_NONE);
if (!func)
return NULL;
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_annotate_handle(struct ntd_context *ctx,
enum dxil_resource_class resource_class,
unsigned resource_range_id,
const struct dxil_value *unannotated_handle)
{
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_ANNOTATE_HANDLE);
if (!opcode)
return NULL;
const struct util_dynarray *mdnodes;
switch (resource_class) {
case DXIL_RESOURCE_CLASS_SRV:
mdnodes = &ctx->srv_metadata_nodes;
break;
case DXIL_RESOURCE_CLASS_UAV:
mdnodes = &ctx->uav_metadata_nodes;
break;
case DXIL_RESOURCE_CLASS_CBV:
mdnodes = &ctx->cbv_metadata_nodes;
break;
case DXIL_RESOURCE_CLASS_SAMPLER:
mdnodes = &ctx->sampler_metadata_nodes;
break;
default:
unreachable("Invalid resource class");
}
const struct dxil_mdnode *mdnode = *util_dynarray_element(mdnodes, const struct dxil_mdnode *, resource_range_id);
const struct dxil_value *res_props = dxil_module_get_res_props_const(&ctx->mod, resource_class, mdnode);
if (!res_props)
return NULL;
const struct dxil_value *args[] = {
opcode,
unannotated_handle,
res_props
};
const struct dxil_func *func =
dxil_get_function(&ctx->mod, "dx.op.annotateHandle", DXIL_NONE);
if (!func)
return NULL;
return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}
static const struct dxil_value *
emit_createhandle_and_annotate(struct ntd_context *ctx,
enum dxil_resource_class resource_class,
unsigned lower_bound,
unsigned upper_bound,
unsigned space,
unsigned resource_range_id,
const struct dxil_value *resource_range_index,
bool non_uniform_resource_index)
{
const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_CREATE_HANDLE_FROM_BINDING);
const struct dxil_value *res_bind = dxil_module_get_res_bind_const(&ctx->mod, lower_bound, upper_bound, space, resource_class);
const struct dxil_value *non_uniform_resource_index_value = dxil_module_get_int1_const(&ctx->mod, non_uniform_resource_index);
if (!opcode || !res_bind || !non_uniform_resource_index_value)
return NULL;
const struct dxil_value *args[] = {
opcode,
res_bind,
resource_range_index,
non_uniform_resource_index_value
};
const struct dxil_func *func =
dxil_get_function(&ctx->mod, "dx.op.createHandleFromBinding", DXIL_NONE);
if (!func)
return NULL;
const struct dxil_value *unannotated_handle = dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
if (!unannotated_handle)
return NULL;
return emit_annotate_handle(ctx, resource_class, resource_range_id, unannotated_handle);
}
static const struct dxil_value *
emit_createhandle_call(struct ntd_context *ctx,
enum dxil_resource_class resource_class,
unsigned lower_bound,
unsigned upper_bound,
unsigned space,
unsigned resource_range_id,
const struct dxil_value *resource_range_index,
bool non_uniform_resource_index)
{
if (ctx->mod.minor_version < 6)
return emit_createhandle_call_pre_6_6(ctx, resource_class, lower_bound, upper_bound, space, resource_range_id, resource_range_index, non_uniform_resource_index);
else
return emit_createhandle_and_annotate(ctx, resource_class, lower_bound, upper_bound, space, resource_range_id, resource_range_index, non_uniform_resource_index);
}
static const struct dxil_value *
emit_createhandle_call_const_index(struct ntd_context *ctx,
enum dxil_resource_class resource_class,
unsigned lower_bound,
unsigned upper_bound,
unsigned space,
unsigned resource_range_id,
unsigned resource_range_index,
bool non_uniform_resource_index)
{
const struct dxil_value *resource_range_index_value = dxil_module_get_int32_const(&ctx->mod, resource_range_index);
if (!resource_range_index_value)
return NULL;
return emit_createhandle_call(ctx, resource_class, lower_bound, upper_bound, space,
resource_range_id, resource_range_index_value,
non_uniform_resource_index);
}
static void
add_resource(struct ntd_context *ctx, enum dxil_resource_type type,
enum dxil_resource_kind kind,