-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathspirv.c
11902 lines (10056 loc) · 478 KB
/
spirv.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 2017 Józef Kucia for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define VKD3D_DBG_CHANNEL VKD3D_DBG_CHANNEL_SHADER
#include "vkd3d_shader_private.h"
#include "vkd3d_d3d12.h"
#include "rbtree.h"
#include <stdarg.h>
#include <stdio.h>
#include <inttypes.h>
#include "spirv/unified1/spirv.h"
#include "spirv/unified1/GLSL.std.450.h"
#ifdef VKD3D_ENABLE_DESCRIPTOR_QA
#include "vkd3d_descriptor_qa_data.h"
#endif
static unsigned int vkd3d_shader_quirk_to_tess_factor_limit(uint32_t quirks)
{
if (quirks & VKD3D_SHADER_QUIRK_LIMIT_TESS_FACTORS_4)
return 4;
else if (quirks & VKD3D_SHADER_QUIRK_LIMIT_TESS_FACTORS_8)
return 8;
else if (quirks & VKD3D_SHADER_QUIRK_LIMIT_TESS_FACTORS_12)
return 12;
else if (quirks & VKD3D_SHADER_QUIRK_LIMIT_TESS_FACTORS_16)
return 16;
else if (quirks & VKD3D_SHADER_QUIRK_LIMIT_TESS_FACTORS_32)
return 32;
return 0;
}
static bool vkd3d_sysval_semantic_is_tessellation_factor(enum vkd3d_sysval_semantic sysval)
{
switch (sysval)
{
case VKD3D_SV_TESS_FACTOR_LINEDEN:
case VKD3D_SV_TESS_FACTOR_LINEDET:
case VKD3D_SV_TESS_FACTOR_QUADEDGE:
case VKD3D_SV_TESS_FACTOR_QUADINT:
case VKD3D_SV_TESS_FACTOR_TRIEDGE:
case VKD3D_SV_TESS_FACTOR_TRIINT:
return true;
default:
return false;
}
}
static enum vkd3d_shader_input_sysval_semantic vkd3d_siv_from_sysval_indexed(enum vkd3d_sysval_semantic sysval,
unsigned int index)
{
switch (sysval)
{
case VKD3D_SV_NONE:
return VKD3D_SIV_NONE;
case VKD3D_SV_POSITION:
return VKD3D_SIV_POSITION;
case VKD3D_SV_CLIP_DISTANCE:
return VKD3D_SIV_CLIP_DISTANCE;
case VKD3D_SV_CULL_DISTANCE:
return VKD3D_SIV_CULL_DISTANCE;
case VKD3D_SV_TESS_FACTOR_QUADEDGE:
return VKD3D_SIV_QUAD_U0_TESS_FACTOR + index;
case VKD3D_SV_TESS_FACTOR_QUADINT:
return VKD3D_SIV_QUAD_U_INNER_TESS_FACTOR + index;
case VKD3D_SV_TESS_FACTOR_TRIEDGE:
return VKD3D_SIV_TRIANGLE_U_TESS_FACTOR + index;
case VKD3D_SV_TESS_FACTOR_TRIINT:
return VKD3D_SIV_TRIANGLE_INNER_TESS_FACTOR;
case VKD3D_SV_TESS_FACTOR_LINEDET:
return VKD3D_SIV_LINE_DETAIL_TESS_FACTOR;
case VKD3D_SV_TESS_FACTOR_LINEDEN:
return VKD3D_SIV_LINE_DENSITY_TESS_FACTOR;
default:
FIXME("Unhandled sysval %#x, index %u.\n", sysval, index);
return VKD3D_SIV_NONE;
}
}
static enum vkd3d_shader_input_sysval_semantic vkd3d_siv_from_sysval(enum vkd3d_sysval_semantic sysval)
{
return vkd3d_siv_from_sysval_indexed(sysval, 0);
}
#define VKD3D_SPIRV_VERSION 0x00010000
#define VKD3D_SPIRV_GENERATOR_ID 18
#define VKD3D_SPIRV_GENERATOR_VERSION 1
#define VKD3D_SPIRV_GENERATOR_MAGIC ((VKD3D_SPIRV_GENERATOR_ID << 16) | VKD3D_SPIRV_GENERATOR_VERSION)
struct vkd3d_spirv_stream
{
uint32_t *words;
size_t capacity;
size_t word_count;
struct list inserted_chunks;
};
static void vkd3d_spirv_stream_init(struct vkd3d_spirv_stream *stream)
{
stream->capacity = 256;
if (!(stream->words = vkd3d_calloc(stream->capacity, sizeof(*stream->words))))
stream->capacity = 0;
stream->word_count = 0;
list_init(&stream->inserted_chunks);
}
struct vkd3d_spirv_chunk
{
struct list entry;
size_t location;
size_t word_count;
uint32_t words[];
};
static void vkd3d_spirv_stream_clear(struct vkd3d_spirv_stream *stream)
{
struct vkd3d_spirv_chunk *c1, *c2;
stream->word_count = 0;
LIST_FOR_EACH_ENTRY_SAFE(c1, c2, &stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
vkd3d_free(c1);
list_init(&stream->inserted_chunks);
}
static void vkd3d_spirv_stream_free(struct vkd3d_spirv_stream *stream)
{
vkd3d_free(stream->words);
vkd3d_spirv_stream_clear(stream);
}
static size_t vkd3d_spirv_stream_current_location(struct vkd3d_spirv_stream *stream)
{
return stream->word_count;
}
static void vkd3d_spirv_stream_insert(struct vkd3d_spirv_stream *stream,
size_t location, const uint32_t *words, unsigned int word_count)
{
struct vkd3d_spirv_chunk *chunk, *current;
if (!(chunk = vkd3d_malloc(offsetof(struct vkd3d_spirv_chunk, words[word_count]))))
return;
chunk->location = location;
chunk->word_count = word_count;
memcpy(chunk->words, words, word_count * sizeof(*words));
LIST_FOR_EACH_ENTRY(current, &stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
{
if (current->location > location)
{
list_add_before(¤t->entry, &chunk->entry);
return;
}
}
list_add_tail(&stream->inserted_chunks, &chunk->entry);
}
static bool vkd3d_spirv_stream_append(struct vkd3d_spirv_stream *dst_stream,
const struct vkd3d_spirv_stream *src_stream)
{
size_t word_count, src_word_count = src_stream->word_count;
struct vkd3d_spirv_chunk *chunk;
size_t src_location = 0;
assert(list_empty(&dst_stream->inserted_chunks));
LIST_FOR_EACH_ENTRY(chunk, &src_stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
src_word_count += chunk->word_count;
if (!vkd3d_array_reserve((void **)&dst_stream->words, &dst_stream->capacity,
dst_stream->word_count + src_word_count, sizeof(*dst_stream->words)))
return false;
assert(dst_stream->word_count + src_word_count <= dst_stream->capacity);
LIST_FOR_EACH_ENTRY(chunk, &src_stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
{
assert(src_location <= chunk->location);
word_count = chunk->location - src_location;
memcpy(&dst_stream->words[dst_stream->word_count], &src_stream->words[src_location],
word_count * sizeof(*src_stream->words));
dst_stream->word_count += word_count;
src_location += word_count;
assert(src_location == chunk->location);
memcpy(&dst_stream->words[dst_stream->word_count], chunk->words,
chunk->word_count * sizeof(*chunk->words));
dst_stream->word_count += chunk->word_count;
}
word_count = src_stream->word_count - src_location;
memcpy(&dst_stream->words[dst_stream->word_count], &src_stream->words[src_location],
word_count * sizeof(*src_stream->words));
dst_stream->word_count += word_count;
return true;
}
struct vkd3d_spirv_builder
{
SpvCapability *capabilities;
size_t capabilities_size;
size_t capability_count;
uint32_t ext_instr_set_glsl_450;
uint32_t invocation_count;
SpvExecutionModel execution_model;
uint32_t current_id;
uint32_t main_function_id;
struct rb_tree declarations;
uint32_t type_sampler_id;
uint32_t type_bool_id;
uint32_t type_void_id;
struct vkd3d_spirv_stream string_stream; /* OpString / OpSource instructions */
struct vkd3d_spirv_stream debug_stream; /* debug instructions */
struct vkd3d_spirv_stream annotation_stream; /* decoration instructions */
struct vkd3d_spirv_stream global_stream; /* types, constants, global variables */
struct vkd3d_spirv_stream function_stream; /* function definitions */
struct vkd3d_spirv_stream execution_mode_stream; /* execution mode instructions */
struct vkd3d_spirv_stream original_function_stream;
struct vkd3d_spirv_stream insertion_stream;
size_t insertion_location;
size_t main_function_location;
/* entry point interface */
uint32_t *iface;
size_t iface_capacity;
size_t iface_element_count;
};
static uint32_t vkd3d_spirv_alloc_id(struct vkd3d_spirv_builder *builder)
{
return builder->current_id++;
}
static bool vkd3d_spirv_has_capability(const struct vkd3d_spirv_builder *builder, SpvCapability cap)
{
unsigned int i;
for (i = 0; i < builder->capability_count; i++)
{
if (builder->capabilities[i] == cap)
return true;
}
return false;
}
static void vkd3d_spirv_enable_capability(struct vkd3d_spirv_builder *builder,
SpvCapability cap)
{
if (vkd3d_spirv_has_capability(builder, cap))
return;
if (!vkd3d_array_reserve((void **)&builder->capabilities, &builder->capabilities_size,
builder->capability_count + 1, sizeof(*builder->capabilities)))
{
ERR("Failed to enable capability %#x.\n", cap);
return;
}
builder->capabilities[builder->capability_count++] = cap;
}
static uint32_t vkd3d_spirv_get_glsl_std450_instr_set(struct vkd3d_spirv_builder *builder)
{
if (!builder->ext_instr_set_glsl_450)
builder->ext_instr_set_glsl_450 = vkd3d_spirv_alloc_id(builder);
return builder->ext_instr_set_glsl_450;
}
static void vkd3d_spirv_add_iface_variable(struct vkd3d_spirv_builder *builder,
uint32_t id)
{
if (!vkd3d_array_reserve((void **)&builder->iface, &builder->iface_capacity,
builder->iface_element_count + 1, sizeof(*builder->iface)))
return;
builder->iface[builder->iface_element_count++] = id;
}
static void vkd3d_spirv_set_execution_model(struct vkd3d_spirv_builder *builder,
SpvExecutionModel model)
{
builder->execution_model = model;
switch (model)
{
case SpvExecutionModelVertex:
case SpvExecutionModelFragment:
case SpvExecutionModelGLCompute:
vkd3d_spirv_enable_capability(builder, SpvCapabilityShader);
break;
case SpvExecutionModelTessellationControl:
case SpvExecutionModelTessellationEvaluation:
vkd3d_spirv_enable_capability(builder, SpvCapabilityTessellation);
break;
case SpvExecutionModelGeometry:
vkd3d_spirv_enable_capability(builder, SpvCapabilityGeometry);
break;
default:
ERR("Unhandled execution model %#x.\n", model);
}
}
static uint32_t vkd3d_spirv_opcode_word(SpvOp op, unsigned int word_count)
{
assert(!(op & ~SpvOpCodeMask));
return (word_count << SpvWordCountShift) | op;
}
static void vkd3d_spirv_build_word(struct vkd3d_spirv_stream *stream, uint32_t word)
{
if (!vkd3d_array_reserve((void **)&stream->words, &stream->capacity,
stream->word_count + 1, sizeof(*stream->words)))
return;
stream->words[stream->word_count++] = word;
}
static unsigned int vkd3d_spirv_string_word_count(const char *str)
{
return align(strlen(str) + 1, sizeof(uint32_t)) / sizeof(uint32_t);
}
static void vkd3d_spirv_build_string(struct vkd3d_spirv_stream *stream,
const char *str, unsigned int word_count)
{
unsigned int word_idx, i;
const char *ptr = str;
for (word_idx = 0; word_idx < word_count; ++word_idx)
{
uint32_t word = 0;
for (i = 0; i < sizeof(uint32_t) && *ptr; ++i)
word |= (uint32_t)*ptr++ << (8 * i);
vkd3d_spirv_build_word(stream, word);
}
}
typedef uint32_t (*vkd3d_spirv_build_pfn)(struct vkd3d_spirv_builder *builder);
typedef uint32_t (*vkd3d_spirv_build_v_pfn)(struct vkd3d_spirv_builder *builder,
const uint32_t *operands, unsigned int operand_count);
typedef uint32_t (*vkd3d_spirv_build1_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0);
typedef uint32_t (*vkd3d_spirv_build1v_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, const uint32_t *operands, unsigned int operand_count);
typedef uint32_t (*vkd3d_spirv_build2_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, uint32_t operand1);
typedef uint32_t (*vkd3d_spirv_build7_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, uint32_t operand1, uint32_t operand2, uint32_t operand3,
uint32_t operand4, uint32_t operand5, uint32_t operand6);
static uint32_t vkd3d_spirv_build_once(struct vkd3d_spirv_builder *builder,
uint32_t *id, vkd3d_spirv_build_pfn build_pfn)
{
if (!(*id))
*id = build_pfn(builder);
return *id;
}
#define MAX_SPIRV_DECLARATION_PARAMETER_COUNT 7
struct vkd3d_spirv_declaration
{
struct rb_entry entry;
SpvOp op;
unsigned int parameter_count;
uint32_t parameters[MAX_SPIRV_DECLARATION_PARAMETER_COUNT];
uint32_t id;
};
static int vkd3d_spirv_declaration_compare(const void *key, const struct rb_entry *e)
{
const struct vkd3d_spirv_declaration *a = key;
const struct vkd3d_spirv_declaration *b = RB_ENTRY_VALUE(e, const struct vkd3d_spirv_declaration, entry);
if (a->op != b->op)
return a->op - b->op;
if (a->parameter_count != b->parameter_count)
return a->parameter_count - b->parameter_count;
assert(a->parameter_count <= ARRAY_SIZE(a->parameters));
return memcmp(&a->parameters, &b->parameters, a->parameter_count * sizeof(*a->parameters));
}
static void vkd3d_spirv_declaration_free(struct rb_entry *entry, void *context)
{
struct vkd3d_spirv_declaration *d = RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry);
vkd3d_free(d);
}
static void vkd3d_spirv_insert_declaration(struct vkd3d_spirv_builder *builder,
const struct vkd3d_spirv_declaration *declaration)
{
struct vkd3d_spirv_declaration *d;
assert(declaration->parameter_count <= ARRAY_SIZE(declaration->parameters));
if (!(d = vkd3d_malloc(sizeof(*d))))
return;
memcpy(d, declaration, sizeof(*d));
if (rb_put(&builder->declarations, d, &d->entry) == -1)
{
ERR("Failed to insert declaration entry.\n");
vkd3d_free(d);
}
}
static uint32_t vkd3d_spirv_build_once_v(struct vkd3d_spirv_builder *builder,
SpvOp op, const uint32_t *operands, unsigned int operand_count,
vkd3d_spirv_build_v_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
unsigned int i, param_idx = 0;
struct rb_entry *entry;
if (operand_count > ARRAY_SIZE(declaration.parameters))
{
WARN("Unsupported parameter count %u (opcode %#x).\n", operand_count, op);
return build_pfn(builder, operands, operand_count);
}
declaration.op = op;
for (i = 0; i < operand_count; ++i)
declaration.parameters[param_idx++] = operands[i];
declaration.parameter_count = param_idx;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operands, operand_count);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once1(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, vkd3d_spirv_build1_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 1;
declaration.parameters[0] = operand0;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once1v(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, const uint32_t *operands, unsigned int operand_count,
vkd3d_spirv_build1v_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
unsigned int i, param_idx = 0;
struct rb_entry *entry;
if (operand_count >= ARRAY_SIZE(declaration.parameters))
{
WARN("Unsupported parameter count %u (opcode %#x).\n", operand_count + 1, op);
return build_pfn(builder, operand0, operands, operand_count);
}
declaration.op = op;
declaration.parameters[param_idx++] = operand0;
for (i = 0; i < operand_count; ++i)
declaration.parameters[param_idx++] = operands[i];
declaration.parameter_count = param_idx;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0, operands, operand_count);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once2(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, uint32_t operand1, vkd3d_spirv_build2_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 2;
declaration.parameters[0] = operand0;
declaration.parameters[1] = operand1;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0, operand1);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once7(struct vkd3d_spirv_builder *builder,
SpvOp op, const uint32_t *operands, vkd3d_spirv_build7_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 7;
memcpy(&declaration.parameters, operands, declaration.parameter_count * sizeof(*operands));
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operands[0], operands[1], operands[2],
operands[3], operands[4], operands[5], operands[6]);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
/*
* vkd3d_spirv_build_op[1-3][v]()
* vkd3d_spirv_build_op_[t][r][1-3][v]()
*
* t - result type
* r - result id
* 1-3 - the number of operands
* v - variable number of operands
*/
static void vkd3d_spirv_build_op(struct vkd3d_spirv_stream *stream, SpvOp op)
{
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 1));
}
static void vkd3d_spirv_build_op1(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand)
{
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2));
vkd3d_spirv_build_word(stream, operand);
}
static void vkd3d_spirv_build_op1v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op2v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1,
const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 3 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op3v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1, uint32_t operand2,
const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 4 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
vkd3d_spirv_build_word(stream, operand2);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op2(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1)
{
vkd3d_spirv_build_op2v(stream, op, operand0, operand1, NULL, 0);
}
static void vkd3d_spirv_build_op3(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1, uint32_t operand2)
{
vkd3d_spirv_build_op2v(stream, op, operand0, operand1, &operand2, 1);
}
static uint32_t vkd3d_spirv_build_op_rv(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op1v(stream, op, result_id, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_r(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op)
{
return vkd3d_spirv_build_op_rv(builder, stream, op, NULL, 0);
}
static uint32_t vkd3d_spirv_build_op_r1(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0)
{
return vkd3d_spirv_build_op_rv(builder, stream, op, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_r2(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0, uint32_t operand1)
{
uint32_t operands[] = {operand0, operand1};
return vkd3d_spirv_build_op_rv(builder, stream, op, operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_r1v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op2v(stream, op, result_id, operand0, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_trv(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op2v(stream, op, result_type, result_id, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_tr(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type)
{
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type, NULL, 0);
}
static uint32_t vkd3d_spirv_build_op_tr1(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0)
{
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_tr2(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1)
{
uint32_t operands[] = {operand0, operand1};
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type,
operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_tr3(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1, uint32_t operand2)
{
uint32_t operands[] = {operand0, operand1, operand2};
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type,
operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_tr1v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op3v(stream, op, result_type, result_id, operand0, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_tr2v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1, const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 5 + operand_count));
vkd3d_spirv_build_word(stream, result_type);
vkd3d_spirv_build_word(stream, result_id);
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
return result_id;
}
static void vkd3d_spirv_begin_function_stream_insertion(struct vkd3d_spirv_builder *builder,
size_t location)
{
assert(builder->insertion_location == ~(size_t)0);
if (vkd3d_spirv_stream_current_location(&builder->function_stream) == location)
return;
builder->original_function_stream = builder->function_stream;
builder->function_stream = builder->insertion_stream;
builder->insertion_location = location;
}
static void vkd3d_spirv_end_function_stream_insertion(struct vkd3d_spirv_builder *builder)
{
struct vkd3d_spirv_stream *insertion_stream = &builder->insertion_stream;
if (builder->insertion_location == ~(size_t)0)
return;
builder->insertion_stream = builder->function_stream;
builder->function_stream = builder->original_function_stream;
vkd3d_spirv_stream_insert(&builder->function_stream, builder->insertion_location,
insertion_stream->words, insertion_stream->word_count);
vkd3d_spirv_stream_clear(insertion_stream);
builder->insertion_location = ~(size_t)0;
}
struct vkd3d_spirv_op_branch_conditional
{
uint32_t opcode;
uint32_t condition_id;
uint32_t true_label;
uint32_t false_label;
};
static struct vkd3d_spirv_op_branch_conditional *vkd3d_spirv_as_op_branch_conditional(
struct vkd3d_spirv_stream *stream, size_t location)
{
return (struct vkd3d_spirv_op_branch_conditional *)&stream->words[location];
}
static void vkd3d_spirv_build_op_capability(struct vkd3d_spirv_stream *stream,
SpvCapability cap)
{
vkd3d_spirv_build_op1(stream, SpvOpCapability, cap);
}
static void vkd3d_spirv_build_op_extension(struct vkd3d_spirv_stream *stream,
const char *name)
{
unsigned int name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtension, 1 + name_size));
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_ext_inst_import(struct vkd3d_spirv_stream *stream,
uint32_t result_id, const char *name)
{
unsigned int name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtInstImport, 2 + name_size));
vkd3d_spirv_build_word(stream, result_id);
vkd3d_spirv_build_string(stream, name, name_size);
}
static uint32_t vkd3d_spirv_build_op_ext_inst(struct vkd3d_spirv_builder *builder,
uint32_t result_type, uint32_t inst_set, uint32_t inst_number,
uint32_t *operands, unsigned int operand_count)
{
return vkd3d_spirv_build_op_tr2v(builder, &builder->function_stream,
SpvOpExtInst, result_type, inst_set, inst_number, operands, operand_count);
}
static void vkd3d_spirv_build_op_memory_model(struct vkd3d_spirv_stream *stream,
SpvAddressingModel addressing_model, SpvMemoryModel memory_model)
{
vkd3d_spirv_build_op2(stream, SpvOpMemoryModel, addressing_model, memory_model);
}
static void vkd3d_spirv_build_op_entry_point(struct vkd3d_spirv_stream *stream,
SpvExecutionModel model, uint32_t function_id, const char *name,
uint32_t *interface_list, unsigned int interface_size)
{
unsigned int i, name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpEntryPoint, 3 + name_size + interface_size));
vkd3d_spirv_build_word(stream, model);
vkd3d_spirv_build_word(stream, function_id);
vkd3d_spirv_build_string(stream, name, name_size);
for (i = 0; i < interface_size; ++i)
vkd3d_spirv_build_word(stream, interface_list[i]);
}
static void vkd3d_spirv_build_op_execution_mode(struct vkd3d_spirv_stream *stream,
uint32_t entry_point, SpvExecutionMode mode, const uint32_t *literals, unsigned int literal_count)
{
vkd3d_spirv_build_op2v(stream, SpvOpExecutionMode, entry_point, mode, literals, literal_count);
}
static void vkd3d_spirv_build_op_source(struct vkd3d_spirv_builder *builder,
SpvSourceLanguage language, uint32_t version, uint32_t source_id)
{
struct vkd3d_spirv_stream *stream = &builder->string_stream;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpSource, 4));
vkd3d_spirv_build_word(stream, language);
vkd3d_spirv_build_word(stream, version);
vkd3d_spirv_build_word(stream, source_id);
}
static void vkd3d_spirv_build_op_string(struct vkd3d_spirv_builder *builder,
uint32_t id, const char *name)
{
struct vkd3d_spirv_stream *stream = &builder->string_stream;
unsigned int name_size;
name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpString, 2 + name_size));
vkd3d_spirv_build_word(stream, id);
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_name(struct vkd3d_spirv_builder *builder,
uint32_t id, const char *fmt, ...)
{
struct vkd3d_spirv_stream *stream = &builder->debug_stream;
unsigned int name_size;
char name[1024];
va_list args;
va_start(args, fmt);
vsnprintf(name, ARRAY_SIZE(name), fmt, args);
name[ARRAY_SIZE(name) - 1] = '\0';
va_end(args);
name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpName, 2 + name_size));
vkd3d_spirv_build_word(stream, id);
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_member_name(struct vkd3d_spirv_builder *builder,
uint32_t type_id, uint32_t member, const char *fmt, ...)
{
struct vkd3d_spirv_stream *stream = &builder->debug_stream;
unsigned int name_size;
char name[1024];
va_list args;
va_start(args, fmt);
vsnprintf(name, ARRAY_SIZE(name), fmt, args);
name[ARRAY_SIZE(name) - 1] = '\0';
va_end(args);
name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpMemberName, 3 + name_size));
vkd3d_spirv_build_word(stream, type_id);
vkd3d_spirv_build_word(stream, member);
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_decorate(struct vkd3d_spirv_builder *builder,
uint32_t target_id, SpvDecoration decoration,
uint32_t *literals, uint32_t literal_count)
{
vkd3d_spirv_build_op2v(&builder->annotation_stream,
SpvOpDecorate, target_id, decoration, literals, literal_count);
}
static void vkd3d_spirv_build_op_decorate1(struct vkd3d_spirv_builder *builder,
uint32_t target_id, SpvDecoration decoration, uint32_t operand0)
{
vkd3d_spirv_build_op_decorate(builder, target_id, decoration, &operand0, 1);
}
static void vkd3d_spirv_build_op_member_decorate(struct vkd3d_spirv_builder *builder,
uint32_t structure_type_id, uint32_t member_idx, SpvDecoration decoration,
uint32_t *literals, uint32_t literal_count)
{
vkd3d_spirv_build_op3v(&builder->annotation_stream, SpvOpMemberDecorate,
structure_type_id, member_idx, decoration, literals, literal_count);
}
static void vkd3d_spirv_build_op_member_decorate1(struct vkd3d_spirv_builder *builder,
uint32_t structure_type_id, uint32_t member_idx, SpvDecoration decoration, uint32_t operand0)
{
vkd3d_spirv_build_op_member_decorate(builder, structure_type_id, member_idx, decoration, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_type_void(struct vkd3d_spirv_builder *builder)
{
return vkd3d_spirv_build_op_r(builder, &builder->global_stream, SpvOpTypeVoid);
}
static uint32_t vkd3d_spirv_get_op_type_void(struct vkd3d_spirv_builder *builder)
{
return vkd3d_spirv_build_once(builder, &builder->type_void_id, vkd3d_spirv_build_op_type_void);
}
static uint32_t vkd3d_spirv_build_op_type_bool(struct vkd3d_spirv_builder *builder)
{
return vkd3d_spirv_build_op_r(builder, &builder->global_stream, SpvOpTypeBool);
}
static uint32_t vkd3d_spirv_get_op_type_bool(struct vkd3d_spirv_builder *builder)
{
return vkd3d_spirv_build_once(builder, &builder->type_bool_id, vkd3d_spirv_build_op_type_bool);
}
static uint32_t vkd3d_spirv_build_op_type_float(struct vkd3d_spirv_builder *builder,
uint32_t width)
{
return vkd3d_spirv_build_op_r1(builder, &builder->global_stream, SpvOpTypeFloat, width);
}
static uint32_t vkd3d_spirv_get_op_type_float(struct vkd3d_spirv_builder *builder,
uint32_t width)
{
return vkd3d_spirv_build_once1(builder, SpvOpTypeFloat, width, vkd3d_spirv_build_op_type_float);
}
static uint32_t vkd3d_spirv_build_op_type_int(struct vkd3d_spirv_builder *builder,
uint32_t width, uint32_t signedness)
{
return vkd3d_spirv_build_op_r2(builder, &builder->global_stream, SpvOpTypeInt, width, signedness);
}
static uint32_t vkd3d_spirv_get_op_type_int(struct vkd3d_spirv_builder *builder,
uint32_t width, uint32_t signedness)
{
if (width == 64)
vkd3d_spirv_enable_capability(builder, SpvCapabilityInt64);
return vkd3d_spirv_build_once2(builder, SpvOpTypeInt, width, signedness,
vkd3d_spirv_build_op_type_int);
}
static uint32_t vkd3d_spirv_build_op_type_vector(struct vkd3d_spirv_builder *builder,
uint32_t component_type, uint32_t component_count)
{
return vkd3d_spirv_build_op_r2(builder, &builder->global_stream,
SpvOpTypeVector, component_type, component_count);
}
static uint32_t vkd3d_spirv_get_op_type_vector(struct vkd3d_spirv_builder *builder,
uint32_t component_type, uint32_t component_count)
{
return vkd3d_spirv_build_once2(builder, SpvOpTypeVector, component_type, component_count,
vkd3d_spirv_build_op_type_vector);
}
static uint32_t vkd3d_spirv_build_op_type_array(struct vkd3d_spirv_builder *builder,
uint32_t element_type, uint32_t length_id)
{
return vkd3d_spirv_build_op_r2(builder, &builder->global_stream,
SpvOpTypeArray, element_type, length_id);
}
static uint32_t vkd3d_spirv_get_op_type_array(struct vkd3d_spirv_builder *builder,
uint32_t element_type, uint32_t length_id)
{
return vkd3d_spirv_build_once2(builder, SpvOpTypeArray, element_type, length_id,
vkd3d_spirv_build_op_type_array);
}
static uint32_t vkd3d_spirv_build_op_type_runtime_array(struct vkd3d_spirv_builder *builder,
uint32_t element_type)
{
return vkd3d_spirv_build_op_r1(builder, &builder->global_stream,
SpvOpTypeRuntimeArray, element_type);
}
static uint32_t vkd3d_spirv_get_op_type_runtime_array(struct vkd3d_spirv_builder *builder,
uint32_t element_type)
{
return vkd3d_spirv_build_once1(builder, SpvOpTypeRuntimeArray, element_type,
vkd3d_spirv_build_op_type_runtime_array);
}
static uint32_t vkd3d_spirv_build_op_type_struct(struct vkd3d_spirv_builder *builder,
const uint32_t *members, unsigned int member_count)