Skip to content

Latest commit

 

History

History
4273 lines (3603 loc) · 149 KB

mojoshader_profile_spirv.c

File metadata and controls

4273 lines (3603 loc) · 149 KB
 
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
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_profile.h"
#pragma GCC visibility push(hidden)
#if SUPPORT_PROFILE_SPIRV
#include "spirv/spirv.h"
#include "spirv/GLSL.std.450.h"
#include <float.h>
static const int SPV_NO_SWIZZLE = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
#define EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(op) \
void emit_SPIRV_##op(Context *ctx) { \
fail(ctx, #op " unimplemented in spirv profile"); \
}
typedef struct SpirvTexm3x3SetupResult
{
// vec4 load results
uint32 id_dst_pad0;
uint32 id_dst_pad1;
uint32 id_dst;
// float dot results
uint32 id_res_x;
uint32 id_res_y;
uint32 id_res_z;
} SpirvTexm3x3SetupResult;
static const char *spv_get_uniform_array_varname(Context *ctx,
const RegisterType regtype,
char *buf, const size_t len)
{
const char *shadertype = ctx->shader_type_str;
const char *type = "";
switch (regtype)
{
case REG_TYPE_CONST: type = "vec4"; break;
case REG_TYPE_CONSTINT: type = "ivec4"; break;
case REG_TYPE_CONSTBOOL: type = "bool"; break;
default: fail(ctx, "BUG: used a uniform we don't know how to define.");
} // switch
snprintf(buf, len, "%s_uniforms_%s", shadertype, type);
return buf;
} // spv_get_uniform_array_varname
static uint32 spv_bumpid(Context *ctx)
{
return (ctx->spirv.idmax += 1);
} // spv_bumpid
static RegisterList *spv_getreg(Context *ctx, const RegisterType regtype, const int regnum)
{
RegisterList *r = reglist_find(&ctx->used_registers, regtype, regnum);
if (!r)
{
failf(ctx, "register not found rt=%d, rn=%d", regtype, regnum);
return NULL;
} // if
return r;
} // spv_getreg
static void spv_componentlist_free(Context *ctx, ComponentList *cl)
{
ComponentList *next;
while (cl)
{
next = cl->next;
Free(ctx, cl);
cl = next;
} // while
} // spv_componentlist_free
static ComponentList *spv_componentlist_alloc(Context *ctx)
{
ComponentList *ret = (ComponentList *) Malloc(ctx, sizeof(ComponentList));
if (!ret) return NULL;
ret->id = 0;
ret->v.i = 0;
ret->next = NULL;
return ret;
} // spv_componentlist_alloc
static const char *get_SPIRV_varname_in_buf(Context *ctx, const RegisterType rt,
const int regnum, char *buf,
const size_t buflen)
{
// turns out these are identical at the moment.
return get_D3D_varname_in_buf(ctx, rt, regnum, buf, buflen);
} // get_SPIRV_varname_in_buf
const char *get_SPIRV_varname(Context *ctx, const RegisterType rt,
const int regnum)
{
// turns out these are identical at the moment.
return get_D3D_varname(ctx, rt, regnum);
} // get_SPIRV_varname
static inline const char *get_SPIRV_const_array_varname_in_buf(Context *ctx,
const int base, const int size,
char *buf, const size_t buflen)
{
snprintf(buf, buflen, "c_array_%d_%d", base, size);
return buf;
} // get_SPIRV_const_array_varname_in_buf
const char *get_SPIRV_const_array_varname(Context *ctx, int base, int size)
{
char buf[64];
get_SPIRV_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
return StrDup(ctx, buf);
} // get_SPIRV_const_array_varname
static uint32 spv_get_uniform_array_id(Context *ctx, const RegisterType regtype)
{
uint32 id;
switch (regtype)
{
case REG_TYPE_CONST:
id = ctx->spirv.uniform_arrays.idvec4;
if (id == 0)
{
id = spv_bumpid(ctx);
ctx->spirv.uniform_arrays.idvec4 = id;
} // if
break;
case REG_TYPE_CONSTINT:
id = ctx->spirv.uniform_arrays.idivec4;
if (id == 0)
{
id = spv_bumpid(ctx);
ctx->spirv.uniform_arrays.idivec4 = id;
} // if
break;
case REG_TYPE_CONSTBOOL:
id = ctx->spirv.uniform_arrays.idbool;
if (id == 0)
{
id = spv_bumpid(ctx);
ctx->spirv.uniform_arrays.idbool = id;
} // if
break;
default:
fail(ctx, "Unexpected register type used to access uniform array.");
id = 0;
} // switch
return id;
} // spv_get_uniform_array_id
static void spv_emit_part_va(Context* ctx, uint32 word_count, uint32 argc, SpvOp op, va_list args)
{
assert(ctx->output != NULL);
if (isfail(ctx))
return; // we failed previously, don't go on...
uint32 word = op | (word_count << 16);
buffer_append(ctx->output, &word, sizeof(word));
while (--argc)
{
word = va_arg(args, uint32);
buffer_append(ctx->output, &word, sizeof(word));
} // while
} // spv_emit_part_va
static void spv_emit_part(Context* ctx, uint32 word_count, uint32 argc, SpvOp op, ...)
{
va_list args;
va_start(args, op);
spv_emit_part_va(ctx, word_count, argc, op, args);
va_end(args);
} // spv_emit_part
static void spv_emit(Context *ctx, uint32 word_count, SpvOp op, ...)
{
va_list args;
va_start(args, op);
spv_emit_part_va(ctx, word_count, word_count, op, args);
va_end(args);
} // spv_emit
static void spv_emit_word(Context *ctx, uint32 word)
{
assert(ctx->output != NULL);
if (isfail(ctx))
return; // we failed previously, don't go on...
buffer_append(ctx->output, &word, sizeof(word));
} // spv_emit_word
static void spv_emit_str(Context *ctx, const char *str)
{
size_t len;
uint32 trail;
assert(ctx->output != NULL);
if (isfail(ctx))
return; // we failed previously, don't go on...
if (str == NULL)
return spv_emit_word(ctx, 0);
len = strlen(str) + 1;
buffer_append(ctx->output, str, len);
len = len % 4;
if (len)
{
trail = 0;
buffer_append(ctx->output, &trail, 4 - len);
} // if
} // spv_emit_str
// get the word count of a string
static uint32 spv_strlen(const char *str)
{
size_t len = strlen(str);
return (uint32) ((len / 4) + 1);
} // spv_strlen
// emits an OpName straight into ctx->globals
static void spv_output_name(Context *ctx, uint32 id, const char *str)
{
if (isfail(ctx))
return; // we failed previously, don't go on...
push_output(ctx, &ctx->globals);
spv_emit_part(ctx, 2 + spv_strlen(str), 2, SpvOpName, id);
spv_emit_str(ctx, str);
pop_output(ctx);
} // spv_output_name
// emit an OpName instruction to identify a register
static void spv_output_regname(Context *ctx, uint32 id, RegisterType regtype, int regnum)
{
char varname[64];
snprintf(varname, sizeof(varname), "%s_", ctx->shader_type_str);
size_t offset = strlen(varname);
get_SPIRV_varname_in_buf(ctx, regtype, regnum, varname + offset, sizeof(varname) - offset);
spv_output_name(ctx, id, varname);
} // spv_output_regname
// emits an OpDecorate BuiltIn straight into ctx->helpers
static void spv_output_builtin(Context *ctx, uint32 id, SpvBuiltIn builtin)
{
if (isfail(ctx))
return; // we failed previously, don't go on...
push_output(ctx, &ctx->helpers);
spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationBuiltIn, builtin);
pop_output(ctx);
} // spv_output_builtin
static uint32 spv_output_location(Context *ctx, uint32 id, uint32 loc)
{
push_output(ctx, &ctx->helpers);
spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationLocation, loc);
pop_output(ctx);
return (buffer_size(ctx->helpers) >> 2) - 1;
} // spv_output_location
May 3, 2020
May 3, 2020
274
static void spv_output_sampler_binding(Context *ctx, uint32 id, uint32 binding)
275
276
277
278
{
if (isfail(ctx))
return;
May 3, 2020
May 3, 2020
279
280
281
282
283
284
285
uint32 set = 0;
if (ctx->spirv.mode == SPIRV_MODE_VK)
{
set = shader_is_vertex(ctx) ? MOJOSHADER_SPIRV_VS_SAMPLER_SET
: MOJOSHADER_SPIRV_PS_SAMPLER_SET;
} // if
286
287
288
289
push_output(ctx, &ctx->helpers);
spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationDescriptorSet, set);
spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationBinding, binding);
pop_output(ctx);
May 3, 2020
May 3, 2020
290
} // spv_output_sampler_binding
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
static SpirvTypeIdx spv_change_base_type_vec_dim(SpirvTypeIdx sti, uint32 dim)
{
uint32 dimSub1 = dim - 1;
assert(STI_CORE_START_ <= sti && sti < STI_CORE_END_);
assert(dimSub1 < 4);
SpirvTypeIdx sti_base = (SpirvTypeIdx)(sti & ~0x3);
SpirvTypeIdx sti_new = (SpirvTypeIdx)(sti_base | dimSub1);
return sti_new;
} // spv_change_base_type_vec_dim
static uint32 spv_get_type(Context *ctx, SpirvTypeIdx tidx)
{
assert(((uint32)tidx) < ((uint32)STI_LENGTH_));
uint32 tid = ctx->spirv.tid[tidx];
if (tid)
return tid;
push_output(ctx, &ctx->mainline_intro);
if (STI_CORE_START_ <= tidx && tidx < STI_CORE_END_)
{
uint32 dim = tidx & 0x3;
SpirvType type = (SpirvType)((tidx >> 2) & 0x3);
if (dim)
{
uint32 tid_base = spv_get_type(ctx, (SpirvTypeIdx)(tidx - dim));
tid = spv_bumpid(ctx);
spv_emit(ctx, 4, SpvOpTypeVector, tid, tid_base, dim + 1);
} // if
else
{
tid = spv_bumpid(ctx);
switch (type)
{
case ST_FLOAT: spv_emit(ctx, 3, SpvOpTypeFloat, tid, 32); break;
case ST_SINT: spv_emit(ctx, 4, SpvOpTypeInt, tid, 32, 1); break;
case ST_UINT: spv_emit(ctx, 4, SpvOpTypeInt, tid, 32, 0); break;
case ST_BOOL: spv_emit(ctx, 2, SpvOpTypeBool, tid); break;
default: assert(!"Unexpected value of SpirvType."); break;
} // switch
} // else
} // if
else if (STI_IMAGE2D <= tidx && tidx <= STI_IMAGECUBE)
{
static const SpvDim dim_table[] = {SpvDim2D, SpvDim3D, SpvDimCube};
SpvDim dim = dim_table[tidx - STI_IMAGE2D];
uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
uint32 id_image = spv_bumpid(ctx);
tid = spv_bumpid(ctx);
spv_emit(ctx, 9, SpvOpTypeImage, id_image, tid_float, dim, 0, 0, 0, 1, SpvImageFormatUnknown);
spv_emit(ctx, 3, SpvOpTypeSampledImage, tid, id_image);
} // else if
else if (tidx == STI_VOID)
{
tid = spv_bumpid(ctx);
spv_emit(ctx, 2, SpvOpTypeVoid, tid);
} // else if
else if (tidx == STI_FUNC_VOID)
{
uint32 tid_void = spv_get_type(ctx, STI_VOID);
tid = spv_bumpid(ctx);
spv_emit(ctx, 3, SpvOpTypeFunction, tid, tid_void);
} // else if
else if (tidx == STI_FUNC_LIT)
{
uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
tid = spv_bumpid(ctx);
spv_emit(ctx, 3 + 1, SpvOpTypeFunction, tid, tid_vec4, tid_vec4);
} // else if
else if (STI_PTR_START_ <= tidx && tidx < STI_PTR_END_)
{
uint32 dim = (tidx & (1 << 4)) ? 3 : 0;
SpirvType type = (SpirvType)((tidx >> 2) & 0x3);
uint32 tid_base = spv_get_type(ctx, (SpirvTypeIdx)((1 << 4) | (type << 2) | dim));
static const SpvStorageClass sc_map[] = {
SpvStorageClassInput,
Apr 25, 2020
Apr 25, 2020
369
370
SpvStorageClassInput,
SpvStorageClassOutput,
371
372
SpvStorageClassOutput,
SpvStorageClassPrivate,
Apr 25, 2020
Apr 25, 2020
373
SpvStorageClassPrivate,
374
SpvStorageClassUniformConstant,
Apr 25, 2020
Apr 25, 2020
375
SpvStorageClassUniform,
376
};
Apr 25, 2020
Apr 25, 2020
377
SpvStorageClass sc = sc_map[((tidx & 0x3) << 1) | (ctx->spirv.mode == SPIRV_MODE_VK)];
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
tid = spv_bumpid(ctx);
spv_emit(ctx, 4, SpvOpTypePointer, tid, sc, tid_base);
} // else if
else if (STI_PTR_IMAGE2D <= tidx && tidx <= STI_PTR_IMAGECUBE)
{
uint32 tid_image = spv_get_type(ctx, (SpirvTypeIdx)(tidx - (STI_PTR_IMAGE2D - STI_IMAGE2D)));
tid = spv_bumpid(ctx);
spv_emit(ctx, 4, SpvOpTypePointer, tid, SpvStorageClassUniformConstant, tid_image);
} // else if
else
assert(!"Unexpected value of type index.");
pop_output(ctx);
ctx->spirv.tid[tidx] = tid;
return tid;
} // spv_get_type
static uint32 spv_gettrue(Context *ctx)
{
if (ctx->spirv.idtrue)
return ctx->spirv.idtrue;
uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
uint32 id = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline_intro);
spv_emit(ctx, 3, SpvOpConstantTrue, tid_bool, id);
pop_output(ctx);
return ctx->spirv.idtrue = id;
} // spv_gettrue
static uint32 spv_getfalse(Context *ctx)
{
if (ctx->spirv.idfalse)
return ctx->spirv.idfalse;
uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
uint32 id = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline_intro);
spv_emit(ctx, 3, SpvOpConstantFalse, tid_bool, id);
pop_output(ctx);
return ctx->spirv.idfalse = id;
} // spv_getfalse
static uint32 spv_getext(Context *ctx)
{
if (ctx->spirv.idext)
return ctx->spirv.idext;
return ctx->spirv.idext = spv_bumpid(ctx);
} // spv_getext
static uint32 spv_output_scalar(Context *ctx, ComponentList *cl,
MOJOSHADER_attributeType type)
{
uint32 idret, idtype;
if (type == MOJOSHADER_ATTRIBUTE_FLOAT)
idtype = spv_get_type(ctx, STI_FLOAT);
else if (type == MOJOSHADER_ATTRIBUTE_INT)
idtype = spv_get_type(ctx, STI_INT);
else if (type == MOJOSHADER_ATTRIBUTE_UINT)
idtype = spv_get_type(ctx, STI_UINT);
else
{
Mar 25, 2020
Mar 25, 2020
443
failf(ctx, "spv_output_scalar: invalid attribute type %d", type);
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
return 0;
} // else
idret = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline_intro);
spv_emit(ctx, 4, SpvOpConstant, idtype, idret, cl->v.u);
pop_output(ctx);
return idret;
} // spv_output_scalar
// The spv_getscalar* functions retrieve the result id of an OpConstant
// instruction with the corresponding value v, or generate a new one.
static uint32 spv_getscalarf(Context *ctx, float v)
{
ComponentList *prev = &(ctx->spirv.cl.f), *cl = ctx->spirv.cl.f.next;
while (cl)
{
if (v == cl->v.f)
return cl->id;
else if (v < cl->v.f)
break;
prev = cl;
cl = cl->next;
} // while
cl = spv_componentlist_alloc(ctx);
cl->next = prev->next;
prev->next = cl;
cl->v.f = v;
cl->id = spv_output_scalar(ctx, cl, MOJOSHADER_ATTRIBUTE_FLOAT);
return cl->id;
} // spv_getscalarf
static uint32 spv_getscalari(Context *ctx, int v)
{
ComponentList *prev = &(ctx->spirv.cl.i), *cl = ctx->spirv.cl.i.next;
while (cl)
{
if (v == cl->v.i)
return cl->id;
else if (v < cl->v.i)
break;
prev = cl;
cl = cl->next;
} // while
cl = spv_componentlist_alloc(ctx);
cl->next = prev->next;
prev->next = cl;
cl->v.i = v;
cl->id = spv_output_scalar(ctx, cl, MOJOSHADER_ATTRIBUTE_INT);
return cl->id;
} // spv_getscalari
static uint32 spv_get_constant_composite(Context *ctx, uint32 tid, uint32* cache, float scalar)
{
uint32 i;
assert(tid != 0);
uint32 dim =
(tid == ctx->spirv.tid[STI_VEC4]) ? 4 :
(tid == ctx->spirv.tid[STI_VEC3]) ? 3 :
(tid == ctx->spirv.tid[STI_VEC2]) ? 2 : 1;
uint32 id = cache[dim - 1];
if (id)
return id;
uint32 sid = spv_getscalarf(ctx, scalar);
if (dim == 1)
{
cache[0] = sid;
return sid;
} // if
id = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline_intro);
spv_emit_part(ctx, 3 + dim, 3, SpvOpConstantComposite, tid, id);
for (i = 0; i < dim; i++)
spv_emit_word(ctx, sid);
pop_output(ctx);
cache[dim - 1] = id;
return id;
} // spv_get_constant_composite
static uint32 spv_get_zero(Context *ctx, uint32 tid)
{
return spv_get_constant_composite(ctx, tid, ctx->spirv.id_0_0, 0.0f);
} // spv_get_zero
static uint32 spv_get_one(Context *ctx, uint32 tid)
{
return spv_get_constant_composite(ctx, tid, ctx->spirv.id_1_0, 1.0f);
} // spv_get_one
static uint32 spv_get_flt_max(Context *ctx, uint32 tid)
{
return spv_get_constant_composite(ctx, tid, ctx->spirv.id_flt_max, FLT_MAX);
} // spv_get_one
static uint32 spv_getvec4_zero(Context *ctx)
{
return spv_get_constant_composite(ctx, spv_get_type(ctx, STI_VEC4), ctx->spirv.id_0_0, 0.0f);
} // spv_getvec4_zero
static uint32 spv_getvec4_one(Context *ctx)
{
return spv_get_constant_composite(ctx, spv_get_type(ctx, STI_VEC4), ctx->spirv.id_1_0, 1.0f);
} // spv_getvec4_one
// Make a 4-channel vector with a value broadcast across all channels. Roughly equivalent to `vec4(value)` in GLSL
static uint32 spv_vectorbroadcast(Context *ctx, uint32 tid, uint32 value)
{
uint32 result = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline);
spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, tid, result, value, value, value, value);
pop_output(ctx);
return result;
} // spv_vectorbroadcast
static void spv_branch_push(Context *ctx, uint32 id_merge, uint32 patch_offset)
{
assert(((size_t)ctx->branch_labels_stack_index) < STATICARRAYLEN(ctx->branch_labels_stack));
int pos = ctx->branch_labels_stack_index++;
ctx->branch_labels_stack[pos] = id_merge;
ctx->branch_labels_patch_stack[pos] = patch_offset;
} // spv_branch_push
static void spv_branch_get(Context *ctx, uint32* out_id_merge, uint32* out_patch_offset)
{
assert(ctx->branch_labels_stack_index > 0);
int pos = ctx->branch_labels_stack_index - 1;
*out_id_merge = ctx->branch_labels_stack[pos];
*out_patch_offset = ctx->branch_labels_patch_stack[pos];
} // spv_branch_get
static void spv_branch_pop(Context *ctx, uint32* out_id_merge, uint32* out_patch_offset)
{
spv_branch_get(ctx, out_id_merge, out_patch_offset);
ctx->branch_labels_stack_index--;
} // spv_branch_pop
static void spv_loop_push(Context *ctx, const SpirvLoopInfo *loop)
{
assert(((size_t)ctx->spirv.loop_stack_idx) < STATICARRAYLEN(ctx->spirv.loop_stack));
int pos = ctx->spirv.loop_stack_idx++;
ctx->spirv.loop_stack[pos] = *loop;
} // spv_loop_push
static void spv_loop_get(Context *ctx, SpirvLoopInfo *loop)
{
assert(ctx->spirv.loop_stack_idx > 0);
int pos = ctx->spirv.loop_stack_idx - 1;
*loop = ctx->spirv.loop_stack[pos];
} // spv_loop_get
static void spv_loop_pop(Context *ctx, SpirvLoopInfo *loop)
{
spv_loop_get(ctx, loop);
ctx->spirv.loop_stack_idx--;
} // spv_loop_pop
static uint32 spv_loop_get_aL(Context *ctx)
{
int i;
// Find the first enclosing loop..endloop. There may be rep..endrep nested inside, so it might
// not be at the top of the stack.
for (i = ctx->spirv.loop_stack_idx - 1; i >= 0; i--)
{
uint32 id_aL = ctx->spirv.loop_stack[i].id_aL;
if (id_aL)
return id_aL;
} // for
assert(!"Referencing loop counter register aL in code not part of loop..endloop region.");
return 0;
} // spv_loop_get_aL
static SpvOp spv_get_comparison(Context *ctx)
{
static const SpvOp spv_cmp_ops[] = {
SpvOpUndef,
SpvOpFOrdGreaterThan,
SpvOpFOrdEqual,
SpvOpFOrdGreaterThanEqual,
SpvOpFOrdLessThan,
SpvOpFOrdNotEqual,
SpvOpFOrdLessThanEqual,
};
if (ctx->instruction_controls >= STATICARRAYLEN(spv_cmp_ops))
{
fail(ctx, "unknown comparison control");
return SpvOpUndef;
} // if
return spv_cmp_ops[ctx->instruction_controls];
} // spv_get_comparison
static void spv_check_read_reg_id(Context *ctx, RegisterList *r)
{
if (r->spirv.iddecl == 0)
{
assert(r->regtype != REG_TYPE_SAMPLER || (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 1, 4)));
assert(r->regtype != REG_TYPE_TEXTURE || (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 1, 4)));
switch (r->regtype)
{
case REG_TYPE_SAMPLER: // s# (only ps_1_1)
case REG_TYPE_TEXTURE: // t# (only ps_1_1)
case REG_TYPE_INPUT: // v#
case REG_TYPE_TEMP: // r#
case REG_TYPE_CONST: // c#
case REG_TYPE_CONSTINT: // i#
case REG_TYPE_CONSTBOOL: // b#
case REG_TYPE_LABEL: // l#
case REG_TYPE_PREDICATE: // p0
r->spirv.iddecl = spv_bumpid(ctx);
break;
case REG_TYPE_LOOP: // aL
r->spirv.iddecl = spv_loop_get_aL(ctx);
break;
default:
{
char varname[64];
get_SPIRV_varname_in_buf(ctx, r->regtype, r->regnum, varname, sizeof(varname));
failf(ctx, "register type %s is unimplemented\n", varname);
break;
} // default
} // switch
} // if
} // spv_check_read_reg_id
static void spv_check_write_reg_id(Context *ctx, RegisterList *r)
{
if (r->spirv.iddecl == 0)
{
switch (r->regtype)
{
// These registers require no declarations, so we can just create them as we see them
case REG_TYPE_ADDRESS:
case REG_TYPE_TEMP:
case REG_TYPE_RASTOUT:
case REG_TYPE_COLOROUT:
case REG_TYPE_TEXCRDOUT:
case REG_TYPE_DEPTHOUT:
case REG_TYPE_ATTROUT:
case REG_TYPE_PREDICATE:
r->spirv.iddecl = spv_bumpid(ctx);
break;
// Other register types should be explicitly declared, so it is an error for them to have iddecl == 0 by now
default:
{
char varname[64];
get_SPIRV_varname_in_buf(ctx, r->regtype, r->regnum, varname, sizeof(varname));
failf(ctx, "tried to write to undeclared register %s\n", varname);
break;
} // default
} // switch
} // if
} // spv_check_write_reg_id
static uint32 spv_ptrimage_from_texturetype(Context *ctx, TextureType ttype)
{
switch (ttype)
{
case TEXTURE_TYPE_2D:
return spv_get_type(ctx, STI_PTR_IMAGE2D);
case TEXTURE_TYPE_CUBE:
return spv_get_type(ctx, STI_PTR_IMAGECUBE);
case TEXTURE_TYPE_VOLUME:
return spv_get_type(ctx, STI_PTR_IMAGE3D);
default:
fail(ctx, "BUG: used a sampler we don't know how to define.");
return 0;
} // switch
} // spv_ptrimage_from_texturetype
static uint32 spv_image_from_texturetype(Context *ctx, TextureType ttype)
{
switch (ttype)
{
case TEXTURE_TYPE_2D:
return spv_get_type(ctx, STI_IMAGE2D);
case TEXTURE_TYPE_CUBE:
return spv_get_type(ctx, STI_IMAGECUBE);
case TEXTURE_TYPE_VOLUME:
return spv_get_type(ctx, STI_IMAGE3D);
default:
fail(ctx, "BUG: used a sampler we don't know how to define.");
return 0;
} // switch
} // spv_ptrimage_from_texturetype
static uint32 spv_access_uniform(Context *ctx, SpirvTypeIdx sti_ptr, RegisterType regtype, uint32 id_offset)
{
uint32 tid_ptr = spv_get_type(ctx, sti_ptr);
uint32 id_arr = spv_get_uniform_array_id(ctx, regtype);
uint32 id_access = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline);
Apr 25, 2020
Apr 25, 2020
745
746
747
748
749
750
751
752
753
754
755
756
757
758
if (ctx->spirv.mode == SPIRV_MODE_VK)
{
uint32 id_uniform_block = ctx->spirv.id_uniform_block;
if (id_uniform_block == 0)
{
id_uniform_block = spv_bumpid(ctx);
ctx->spirv.id_uniform_block = id_uniform_block;
} // if
spv_emit(ctx, 4+2, SpvOpAccessChain, tid_ptr, id_access, id_uniform_block, id_arr, id_offset);
} // if
else
{
spv_emit(ctx, 4+1, SpvOpAccessChain, tid_ptr, id_access, id_arr, id_offset);
} // else
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
pop_output(ctx);
return id_access;
} // spv_access_uniform
static SpirvResult spv_loadreg(Context *ctx, RegisterList *r)
{
const RegisterType regtype = r->regtype;
spv_check_read_reg_id(ctx, r);
uint32 id_src = r->spirv.iddecl;
SpirvResult result;
if (regtype == REG_TYPE_SAMPLER)
{
RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, r->regnum);
result.tid = spv_image_from_texturetype(ctx, (TextureType)sreg->index);
} // if
else if (regtype == REG_TYPE_CONSTBOOL)
{
if (!r->spirv.is_ssa)
id_src = spv_access_uniform(ctx, STI_PTR_INT_U, regtype, r->spirv.iddecl);
result.tid = spv_get_type(ctx, STI_INT);
} // else if
else if (regtype == REG_TYPE_CONSTINT)
{
if (!r->spirv.is_ssa)
id_src = spv_access_uniform(ctx, STI_PTR_IVEC4_U, regtype, r->spirv.iddecl);
result.tid = spv_get_type(ctx, STI_IVEC4);
} // else if
else if (regtype == REG_TYPE_CONST)
{
if (!r->spirv.is_ssa)
id_src = spv_access_uniform(ctx, STI_PTR_VEC4_U, regtype, r->spirv.iddecl);
result.tid = spv_get_type(ctx, STI_VEC4);
} // else if
else if (regtype == REG_TYPE_LOOP)
result.tid = spv_get_type(ctx, STI_INT);
else if (regtype == REG_TYPE_PREDICATE)
result.tid = spv_get_type(ctx, STI_BVEC4);
else
result.tid = spv_get_type(ctx, STI_VEC4);
// Constants can be used directly, no need to load them.
assert(r->spirv.is_ssa == 0 || r->spirv.is_ssa == 1);
if (r->spirv.is_ssa)
{
result.id = r->spirv.iddecl;
return result;
} // if
assert(id_src);
result.id = spv_bumpid(ctx);
push_output(ctx, &ctx->mainline);
spv_emit(ctx, 4, SpvOpLoad, result.tid, result.id, id_src);
pop_output(ctx);
return result;
} // spv_loadreg
static uint32 spv_emit_swizzle(Context *ctx, uint32 arg, uint32 rtid, const int swizzle, const int writemask)
{
uint32 result = spv_bumpid(ctx);
const int writemask0 = (writemask >> 0) & 0x1;
const int writemask1 = (writemask >> 1) & 0x1;
const int writemask2 = (writemask >> 2) & 0x1;
const int writemask3 = (writemask >> 3) & 0x1;
const uint32 swizzle_x = (swizzle >> 0) & 0x3;
const uint32 swizzle_y = (swizzle >> 2) & 0x3;
const uint32 swizzle_z = (swizzle >> 4) & 0x3;
const uint32 swizzle_w = (swizzle >> 6) & 0x3;
push_output(ctx, &ctx->mainline);
// OpVectorShuffle takes two vectors to shuffle, but to do a swizzle
// operation we can just ignore the second argument (meaning it can be
// anything, and I am just making it `arg` for convenience)
uint32 word_count = 5 + writemask0 + writemask1 + writemask2 + writemask3;
spv_emit_part(ctx, word_count, 5, SpvOpVectorShuffle, rtid, result, arg, arg);
if (writemask0) spv_emit_word(ctx, swizzle_x);
if (writemask1) spv_emit_word(ctx, swizzle_y);
if (writemask2) spv_emit_word(ctx, swizzle_z);
if (writemask3) spv_emit_word(ctx, swizzle_w);
pop_output(ctx);
return result;
} // spv_emit_swizzle
SpirvResult spv_swizzle(Context *ctx, SpirvResult arg, const int swizzle, const int writemask)
{
int i;
// Nothing to do, so return the same SSA value
if (no_swizzle(swizzle) && writemask_xyzw(writemask))
return arg;
assert(arg.tid != 0);
assert(writemask == 1
|| writemask == 3
|| writemask == 7
|| writemask == 15
);
SpirvTypeIdx sti_arg = STI_VOID;
for (i = STI_CORE_START_; i < STI_CORE_END_; i++)
{
if (ctx->spirv.tid[i] == arg.tid)
{
sti_arg = (SpirvTypeIdx)i;
break;
} // if
} // for
assert(sti_arg != STI_VOID);
// We should not leave any value undefined, as it may end up used (eg. dot
// product), which will make everything relying on it's result undefined.
// Therefore, we specifically determine true dimensionality of the result.
int resdim = 0;
switch (writemask)
{
case 1:
resdim = 1;
break;
case 3:
resdim = 2;
break;
case 7:
resdim = 3;
break;
case 15:
resdim = 4;
break;
default:
failf(ctx, "Unexpected write mask in swizzle: 0x%X");
assert(0);
break;
} // switch
SpirvTypeIdx sti_result = spv_change_base_type_vec_dim(sti_arg, resdim);
SpirvResult result = {0};
result.id = (resdim != 1 || sti_arg != sti_result) ? spv_bumpid(ctx) : arg.id;
result.tid = spv_get_type(ctx, sti_result);
assert(result.tid != 0);
push_output(ctx, &ctx->mainline);
if (resdim != 1)
{
// OpVectorShuffle takes two vectors to shuffle, but to do a swizzle
// operation we can just ignore the second argument (meaning it can be
// anything, and I am just making it `arg` for convenience)
spv_emit_part(ctx, 5 + resdim, 5, SpvOpVectorShuffle, result.tid, result.id, arg.id, arg.id);
for (i = 0; i < resdim; i++)
spv_emit_word(ctx, (swizzle >> (2*i)) & 0x3);
} // if
else if (sti_arg != sti_result)
{
// OpVectorShuffle may not produce a scalar. Instead we use OpCompositeExtract.
spv_emit(ctx, 5, SpvOpCompositeExtract, result.tid, result.id, arg.id, swizzle & 0x3);
} // else if
pop_output(ctx);
return result;
} // make_GLSL_swizzle_string
static SpirvResult spv_load_srcarg(Context *ctx, const size_t idx, const int writemask)
{
SpirvResult result = {0};
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
return result;
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
if (arg->relative)
{
if (arg->regtype == REG_TYPE_INPUT)
fail(ctx, "relative input array access is unimplemented");
else
{
assert(arg->regtype == REG_TYPE_CONST);
const int arrayidx = arg->relative_array->index;
const int offset = arg->regnum - arrayidx;
assert(offset >= 0);
int is_constant = (arg->relative_array->constant != NULL);
uint32 id_array = 0;
if (is_constant)
{
id_array = ctx->spirv.constant_arrays.idvec4;
if (id_array == 0)
{
id_array = spv_bumpid(ctx);
ctx->spirv.constant_arrays.idvec4 = id_array;
} // if
} // if
RegisterList *reg_rel = spv_getreg(ctx, arg->relative_regtype, arg->relative_regnum);
spv_check_read_reg_id(ctx, reg_rel);
spv_check_read_reg_id(ctx, reg);
uint32 id_int = spv_get_type(ctx, STI_INT);
uint32 id_offset;
if (reg_rel->regtype == REG_TYPE_LOOP)
id_offset = reg_rel->spirv.iddecl;
else
{
uint32 id_pint = spv_get_type(ctx, STI_PTR_INT_P);
uint32 id_compidx = spv_getscalari(ctx, arg->relative_component);
uint32 id_pcomp = spv_bumpid(ctx);
spv_emit(ctx, 5, SpvOpAccessChain, id_pint, id_pcomp, reg_rel->spirv.iddecl, id_compidx);
id_offset = spv_bumpid(ctx);
spv_emit(ctx, 4, SpvOpLoad, id_int, id_offset, id_pcomp);
} // else
if (!is_constant)
{
uint32 id_arraybase = reg->spirv.iddecl;
uint32 id_a = id_offset;
uint32 id_b = id_arraybase;
id_offset = spv_bumpid(ctx);
spv_emit(ctx, 5, SpvOpIAdd, id_int, id_offset, id_a, id_b);
} // if
if (offset)
{