Debian Codegraph @

profiles/mojoshader_profile_spirv.c

raw symbols SCIP
1 /**
2 * MojoShader; generate shader programs from bytecode of compiled
3 * Direct3D shaders.
4 *
5 * Please see the file LICENSE.txt in the source's root directory.
6 *
7 * This file written by Ryan C. Gordon.
8 */
9
10 #define __MOJOSHADER_INTERNAL__ 1
11 #include "mojoshader_profile.h"
12
13 #pragma GCC visibility push(hidden)
14
15 #if SUPPORT_PROFILE_SPIRV
16 #include "spirv/spirv.h"
17 #include "spirv/GLSL.std.450.h"
18 #include <float.h>
19
20 static const int SPV_NO_SWIZZLE = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
21
22 #define EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(op) \
23 void emit_SPIRV_##op(Context *ctx) { \
24 fail(ctx, #op " unimplemented in spirv profile"); \
25 }
26
27 typedef struct SpirvTexm3x3SetupResult
28 {
29 // vec4 load results
30 uint32 id_dst_pad0;
31 uint32 id_dst_pad1;
32 uint32 id_dst;
33
34 // float dot results
35 uint32 id_res_x;
36 uint32 id_res_y;
37 uint32 id_res_z;
38 } SpirvTexm3x3SetupResult;
39
40 static const char *spv_get_uniform_array_varname(Context *ctx,
41 const RegisterType regtype,
42 char *buf, const size_t len)
43 {
44 const char *shadertype = ctx->shader_type_str;
45 const char *type = "";
46 switch (regtype)
47 {
48 case REG_TYPE_CONST: type = "vec4"; break;
49 case REG_TYPE_CONSTINT: type = "ivec4"; break;
50 case REG_TYPE_CONSTBOOL: type = "bool"; break;
51 default: fail(ctx, "BUG: used a uniform we don't know how to define.");
52 } // switch
53 snprintf(buf, len, "%s_uniforms_%s", shadertype, type);
54 return buf;
55 } // spv_get_uniform_array_varname
56
57 static uint32 spv_bumpid(Context *ctx)
58 {
59 return (ctx->spirv.idmax += 1);
60 } // spv_bumpid
61
62 static RegisterList *spv_getreg(Context *ctx, const RegisterType regtype, const int regnum)
63 {
64 RegisterList *r = reglist_find(&ctx->used_registers, regtype, regnum);
65 if (!r)
66 {
67 failf(ctx, "register not found rt=%d, rn=%d", regtype, regnum);
68 return NULL;
69 } // if
70 return r;
71 } // spv_getreg
72
73 static void spv_componentlist_free(Context *ctx, ComponentList *cl)
74 {
75 ComponentList *next;
76 while (cl)
77 {
78 next = cl->next;
79 Free(ctx, cl);
80 cl = next;
81 } // while
82 } // spv_componentlist_free
83
84 static ComponentList *spv_componentlist_alloc(Context *ctx)
85 {
86 ComponentList *ret = (ComponentList *) Malloc(ctx, sizeof(ComponentList));
87 if (!ret) return NULL;
88 ret->id = 0;
89 ret->v.i = 0;
90 ret->next = NULL;
91 return ret;
92 } // spv_componentlist_alloc
93
94 static const char *get_SPIRV_varname_in_buf(Context *ctx, const RegisterType rt,
95 const int regnum, char *buf,
96 const size_t buflen)
97 {
98 // turns out these are identical at the moment.
99 return get_D3D_varname_in_buf(ctx, rt, regnum, buf, buflen);
100 } // get_SPIRV_varname_in_buf
101
102 const char *get_SPIRV_varname(Context *ctx, const RegisterType rt,
103 const int regnum)
104 {
105 // turns out these are identical at the moment.
106 return get_D3D_varname(ctx, rt, regnum);
107 } // get_SPIRV_varname
108
109
110 static inline const char *get_SPIRV_const_array_varname_in_buf(Context *ctx,
111 const int base, const int size,
112 char *buf, const size_t buflen)
113 {
114 snprintf(buf, buflen, "c_array_%d_%d", base, size);
115 return buf;
116 } // get_SPIRV_const_array_varname_in_buf
117
118
119 const char *get_SPIRV_const_array_varname(Context *ctx, int base, int size)
120 {
121 char buf[64];
122 get_SPIRV_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
123 return StrDup(ctx, buf);
124 } // get_SPIRV_const_array_varname
125
126 static uint32 spv_get_uniform_array_id(Context *ctx, const RegisterType regtype)
127 {
128 uint32 id;
129 switch (regtype)
130 {
131 case REG_TYPE_CONST:
132 id = ctx->spirv.uniform_arrays.idvec4;
133 if (id == 0)
134 {
135 id = spv_bumpid(ctx);
136 ctx->spirv.uniform_arrays.idvec4 = id;
137 } // if
138 break;
139
140 case REG_TYPE_CONSTINT:
141 id = ctx->spirv.uniform_arrays.idivec4;
142 if (id == 0)
143 {
144 id = spv_bumpid(ctx);
145 ctx->spirv.uniform_arrays.idivec4 = id;
146 } // if
147 break;
148
149 case REG_TYPE_CONSTBOOL:
150 id = ctx->spirv.uniform_arrays.idbool;
151 if (id == 0)
152 {
153 id = spv_bumpid(ctx);
154 ctx->spirv.uniform_arrays.idbool = id;
155 } // if
156 break;
157
158 default:
159 fail(ctx, "Unexpected register type used to access uniform array.");
160 id = 0;
161 } // switch
162
163 return id;
164 } // spv_get_uniform_array_id
165
166 static void spv_emit_part_va(Context* ctx, uint32 word_count, uint32 argc, SpvOp op, va_list args)
167 {
168 assert(ctx->output != NULL);
169 if (isfail(ctx))
170 return; // we failed previously, don't go on...
171
172 uint32 word = op | (word_count << 16);
173 buffer_append(ctx->output, &word, sizeof(word));
174 while (--argc)
175 {
176 word = va_arg(args, uint32);
177 buffer_append(ctx->output, &word, sizeof(word));
178 } // while
179 } // spv_emit_part_va
180
181 static void spv_emit_part(Context* ctx, uint32 word_count, uint32 argc, SpvOp op, ...)
182 {
183 va_list args;
184 va_start(args, op);
185 spv_emit_part_va(ctx, word_count, argc, op, args);
186 va_end(args);
187 } // spv_emit_part
188
189 static void spv_emit(Context *ctx, uint32 word_count, SpvOp op, ...)
190 {
191 va_list args;
192 va_start(args, op);
193 spv_emit_part_va(ctx, word_count, word_count, op, args);
194 va_end(args);
195 } // spv_emit
196
197 static void spv_emit_word(Context *ctx, uint32 word)
198 {
199 assert(ctx->output != NULL);
200 if (isfail(ctx))
201 return; // we failed previously, don't go on...
202
203 buffer_append(ctx->output, &word, sizeof(word));
204 } // spv_emit_word
205
206 static void spv_emit_str(Context *ctx, const char *str)
207 {
208 size_t len;
209 uint32 trail;
210 assert(ctx->output != NULL);
211 if (isfail(ctx))
212 return; // we failed previously, don't go on...
213
214 if (str == NULL)
215 return spv_emit_word(ctx, 0);
216 len = strlen(str) + 1;
217 buffer_append(ctx->output, str, len);
218 len = len % 4;
219 if (len)
220 {
221 trail = 0;
222 buffer_append(ctx->output, &trail, 4 - len);
223 } // if
224 } // spv_emit_str
225
226 // get the word count of a string
227 static uint32 spv_strlen(const char *str)
228 {
229 size_t len = strlen(str);
230 return (uint32) ((len / 4) + 1);
231 } // spv_strlen
232
233 // emits an OpName straight into ctx->globals
234 static void spv_output_name(Context *ctx, uint32 id, const char *str)
235 {
236 if (isfail(ctx))
237 return; // we failed previously, don't go on...
238
239 push_output(ctx, &ctx->globals);
240 spv_emit_part(ctx, 2 + spv_strlen(str), 2, SpvOpName, id);
241 spv_emit_str(ctx, str);
242 pop_output(ctx);
243 } // spv_output_name
244
245 // emit an OpName instruction to identify a register
246 static void spv_output_regname(Context *ctx, uint32 id, RegisterType regtype, int regnum)
247 {
248 char varname[64];
249 snprintf(varname, sizeof(varname), "%s_", ctx->shader_type_str);
250 size_t offset = strlen(varname);
251 get_SPIRV_varname_in_buf(ctx, regtype, regnum, varname + offset, sizeof(varname) - offset);
252 spv_output_name(ctx, id, varname);
253 } // spv_output_regname
254
255 // emits an OpDecorate BuiltIn straight into ctx->helpers
256 static void spv_output_builtin(Context *ctx, uint32 id, SpvBuiltIn builtin)
257 {
258 if (isfail(ctx))
259 return; // we failed previously, don't go on...
260
261 push_output(ctx, &ctx->helpers);
262 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationBuiltIn, builtin);
263 pop_output(ctx);
264 } // spv_output_builtin
265
266 static uint32 spv_output_location(Context *ctx, uint32 id, uint32 loc)
267 {
268 push_output(ctx, &ctx->helpers);
269 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationLocation, loc);
270 pop_output(ctx);
271 return (buffer_size(ctx->helpers) >> 2) - 1;
272 } // spv_output_location
273
274 static void spv_output_color_location(Context *ctx, uint32 id, uint32 index)
275 {
276 SpirvPatchTable* table = &ctx->spirv.patch_table;
277 push_output(ctx, &ctx->helpers);
278 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationLocation, index);
279 pop_output(ctx);
280 table->output_offsets[index] = (buffer_size(ctx->helpers) >> 2) - 1;
281 } // spv_output_color_location
282
283 static void spv_output_attrib_location(Context *ctx, uint32 id,
284 MOJOSHADER_usage usage, uint32 index)
285 {
286 SpirvPatchTable* table = &ctx->spirv.patch_table;
287 push_output(ctx, &ctx->helpers);
288 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationLocation, 0xDEADBEEF);
289 pop_output(ctx);
290 table->attrib_offsets[usage][index] = (buffer_size(ctx->helpers) >> 2) - 1;
291 } // spv_output_attrib_location
292
293 static void spv_output_sampler_binding(Context *ctx, uint32 id, uint32 binding)
294 {
295 if (isfail(ctx))
296 return;
297
298 uint32 set = 0;
299 if (ctx->spirv.mode == SPIRV_MODE_VK)
300 {
301 set = shader_is_vertex(ctx) ? MOJOSHADER_SPIRV_VS_SAMPLER_SET
302 : MOJOSHADER_SPIRV_PS_SAMPLER_SET;
303 } // if
304
305 push_output(ctx, &ctx->helpers);
306 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationDescriptorSet, set);
307 spv_emit(ctx, 4, SpvOpDecorate, id, SpvDecorationBinding, binding);
308 pop_output(ctx);
309 } // spv_output_sampler_binding
310
311 static SpirvTypeIdx spv_change_base_type_vec_dim(SpirvTypeIdx sti, uint32 dim)
312 {
313 uint32 dimSub1 = dim - 1;
314 assert(STI_CORE_START_ <= sti && sti < STI_CORE_END_);
315 assert(dimSub1 < 4);
316
317 SpirvTypeIdx sti_base = (SpirvTypeIdx)(sti & ~0x3);
318 SpirvTypeIdx sti_new = (SpirvTypeIdx)(sti_base | dimSub1);
319 return sti_new;
320 } // spv_change_base_type_vec_dim
321
322 static uint32 spv_get_type(Context *ctx, SpirvTypeIdx tidx)
323 {
324 assert(((uint32)tidx) < ((uint32)STI_LENGTH_));
325
326 uint32 tid = ctx->spirv.tid[tidx];
327 if (tid)
328 return tid;
329
330 push_output(ctx, &ctx->mainline_intro);
331 if (STI_CORE_START_ <= tidx && tidx < STI_CORE_END_)
332 {
333 uint32 dim = tidx & 0x3;
334 SpirvType type = (SpirvType)((tidx >> 2) & 0x3);
335 if (dim)
336 {
337 uint32 tid_base = spv_get_type(ctx, (SpirvTypeIdx)(tidx - dim));
338 tid = spv_bumpid(ctx);
339 spv_emit(ctx, 4, SpvOpTypeVector, tid, tid_base, dim + 1);
340 } // if
341 else
342 {
343 tid = spv_bumpid(ctx);
344 switch (type)
345 {
346 case ST_FLOAT: spv_emit(ctx, 3, SpvOpTypeFloat, tid, 32); break;
347 case ST_SINT: spv_emit(ctx, 4, SpvOpTypeInt, tid, 32, 1); break;
348 case ST_UINT: spv_emit(ctx, 4, SpvOpTypeInt, tid, 32, 0); break;
349 case ST_BOOL: spv_emit(ctx, 2, SpvOpTypeBool, tid); break;
350 default: assert(!"Unexpected value of SpirvType."); break;
351 } // switch
352 } // else
353 } // if
354 else if (STI_IMAGE2D <= tidx && tidx <= STI_IMAGECUBE)
355 {
356 static const SpvDim dim_table[] = {SpvDim2D, SpvDim3D, SpvDimCube};
357 SpvDim dim = dim_table[tidx - STI_IMAGE2D];
358 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
359 uint32 id_image = spv_bumpid(ctx);
360 tid = spv_bumpid(ctx);
361 spv_emit(ctx, 9, SpvOpTypeImage, id_image, tid_float, dim, 0, 0, 0, 1, SpvImageFormatUnknown);
362 spv_emit(ctx, 3, SpvOpTypeSampledImage, tid, id_image);
363 } // else if
364 else if (tidx == STI_VOID)
365 {
366 tid = spv_bumpid(ctx);
367 spv_emit(ctx, 2, SpvOpTypeVoid, tid);
368 } // else if
369 else if (tidx == STI_FUNC_VOID)
370 {
371 uint32 tid_void = spv_get_type(ctx, STI_VOID);
372 tid = spv_bumpid(ctx);
373 spv_emit(ctx, 3, SpvOpTypeFunction, tid, tid_void);
374 } // else if
375 else if (tidx == STI_FUNC_LIT)
376 {
377 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
378 tid = spv_bumpid(ctx);
379 spv_emit(ctx, 3 + 1, SpvOpTypeFunction, tid, tid_vec4, tid_vec4);
380 } // else if
381 else if (STI_PTR_START_ <= tidx && tidx < STI_PTR_END_)
382 {
383 uint32 dim = (tidx & (1 << 4)) ? 3 : 0;
384 SpirvType type = (SpirvType)((tidx >> 2) & 0x3);
385 uint32 tid_base = spv_get_type(ctx, (SpirvTypeIdx)((1 << 4) | (type << 2) | dim));
386 static const SpvStorageClass sc_map[] = {
387 SpvStorageClassInput,
388 SpvStorageClassInput,
389 SpvStorageClassOutput,
390 SpvStorageClassOutput,
391 SpvStorageClassPrivate,
392 SpvStorageClassPrivate,
393 SpvStorageClassUniformConstant,
394 SpvStorageClassUniform,
395 };
396 SpvStorageClass sc = sc_map[((tidx & 0x3) << 1) | (ctx->spirv.mode == SPIRV_MODE_VK)];
397 tid = spv_bumpid(ctx);
398 spv_emit(ctx, 4, SpvOpTypePointer, tid, sc, tid_base);
399 } // else if
400 else if (STI_PTR_IMAGE2D <= tidx && tidx <= STI_PTR_IMAGECUBE)
401 {
402 uint32 tid_image = spv_get_type(ctx, (SpirvTypeIdx)(tidx - (STI_PTR_IMAGE2D - STI_IMAGE2D)));
403 tid = spv_bumpid(ctx);
404 spv_emit(ctx, 4, SpvOpTypePointer, tid, SpvStorageClassUniformConstant, tid_image);
405 } // else if
406 else if (tidx == STI_PTR_VEC2_I)
407 {
408 uint32 tid_base = spv_get_type(ctx, STI_VEC2);
409 tid = spv_bumpid(ctx);
410 spv_emit(ctx, 4, SpvOpTypePointer, tid, SpvStorageClassInput, tid_base);
411 } // else if
412 else
413 assert(!"Unexpected value of type index.");
414 pop_output(ctx);
415
416 ctx->spirv.tid[tidx] = tid;
417 return tid;
418 } // spv_get_type
419
420 static uint32 spv_gettrue(Context *ctx)
421 {
422 if (ctx->spirv.idtrue)
423 return ctx->spirv.idtrue;
424
425 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
426 uint32 id = spv_bumpid(ctx);
427
428 push_output(ctx, &ctx->mainline_intro);
429 spv_emit(ctx, 3, SpvOpConstantTrue, tid_bool, id);
430 pop_output(ctx);
431 return ctx->spirv.idtrue = id;
432 } // spv_gettrue
433
434 static uint32 spv_getfalse(Context *ctx)
435 {
436 if (ctx->spirv.idfalse)
437 return ctx->spirv.idfalse;
438
439 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
440 uint32 id = spv_bumpid(ctx);
441
442 push_output(ctx, &ctx->mainline_intro);
443 spv_emit(ctx, 3, SpvOpConstantFalse, tid_bool, id);
444 pop_output(ctx);
445 return ctx->spirv.idfalse = id;
446 } // spv_getfalse
447
448 static uint32 spv_getext(Context *ctx)
449 {
450 if (ctx->spirv.idext)
451 return ctx->spirv.idext;
452
453 return ctx->spirv.idext = spv_bumpid(ctx);
454 } // spv_getext
455
456 static uint32 spv_output_scalar(Context *ctx, ComponentList *cl,
457 MOJOSHADER_attributeType type)
458 {
459 uint32 idret, idtype;
460 if (type == MOJOSHADER_ATTRIBUTE_FLOAT)
461 idtype = spv_get_type(ctx, STI_FLOAT);
462 else if (type == MOJOSHADER_ATTRIBUTE_INT)
463 idtype = spv_get_type(ctx, STI_INT);
464 else if (type == MOJOSHADER_ATTRIBUTE_UINT)
465 idtype = spv_get_type(ctx, STI_UINT);
466 else
467 {
468 failf(ctx, "spv_output_scalar: invalid attribute type %d", type);
469 return 0;
470 } // else
471 idret = spv_bumpid(ctx);
472
473 push_output(ctx, &ctx->mainline_intro);
474 spv_emit(ctx, 4, SpvOpConstant, idtype, idret, cl->v.u);
475 pop_output(ctx);
476 return idret;
477 } // spv_output_scalar
478
479 // The spv_getscalar* functions retrieve the result id of an OpConstant
480 // instruction with the corresponding value v, or generate a new one.
481 static uint32 spv_getscalarf(Context *ctx, float v)
482 {
483 ComponentList *prev = &(ctx->spirv.cl.f), *cl = ctx->spirv.cl.f.next;
484 while (cl)
485 {
486 if (v == cl->v.f)
487 return cl->id;
488 else if (v < cl->v.f)
489 break;
490 prev = cl;
491 cl = cl->next;
492 } // while
493 cl = spv_componentlist_alloc(ctx);
494 cl->next = prev->next;
495 prev->next = cl;
496 cl->v.f = v;
497 cl->id = spv_output_scalar(ctx, cl, MOJOSHADER_ATTRIBUTE_FLOAT);
498 return cl->id;
499 } // spv_getscalarf
500
501 static uint32 spv_getscalari(Context *ctx, int v)
502 {
503 ComponentList *prev = &(ctx->spirv.cl.i), *cl = ctx->spirv.cl.i.next;
504 while (cl)
505 {
506 if (v == cl->v.i)
507 return cl->id;
508 else if (v < cl->v.i)
509 break;
510 prev = cl;
511 cl = cl->next;
512 } // while
513 cl = spv_componentlist_alloc(ctx);
514 cl->next = prev->next;
515 prev->next = cl;
516 cl->v.i = v;
517 cl->id = spv_output_scalar(ctx, cl, MOJOSHADER_ATTRIBUTE_INT);
518 return cl->id;
519 } // spv_getscalari
520
521 static uint32 spv_get_constant_composite(Context *ctx, uint32 tid, uint32* cache, float scalar)
522 {
523 uint32 i;
524
525 assert(tid != 0);
526 uint32 dim =
527 (tid == ctx->spirv.tid[STI_VEC4]) ? 4 :
528 (tid == ctx->spirv.tid[STI_VEC3]) ? 3 :
529 (tid == ctx->spirv.tid[STI_VEC2]) ? 2 : 1;
530
531 uint32 id = cache[dim - 1];
532 if (id)
533 return id;
534
535 uint32 sid = spv_getscalarf(ctx, scalar);
536 if (dim == 1)
537 {
538 cache[0] = sid;
539 return sid;
540 } // if
541
542 id = spv_bumpid(ctx);
543 push_output(ctx, &ctx->mainline_intro);
544 spv_emit_part(ctx, 3 + dim, 3, SpvOpConstantComposite, tid, id);
545 for (i = 0; i < dim; i++)
546 spv_emit_word(ctx, sid);
547 pop_output(ctx);
548 cache[dim - 1] = id;
549 return id;
550 } // spv_get_constant_composite
551
552 static uint32 spv_get_zero(Context *ctx, uint32 tid)
553 {
554 return spv_get_constant_composite(ctx, tid, ctx->spirv.id_0_0, 0.0f);
555 } // spv_get_zero
556
557 static uint32 spv_get_one(Context *ctx, uint32 tid)
558 {
559 return spv_get_constant_composite(ctx, tid, ctx->spirv.id_1_0, 1.0f);
560 } // spv_get_one
561
562 static uint32 spv_get_flt_max(Context *ctx, uint32 tid)
563 {
564 return spv_get_constant_composite(ctx, tid, ctx->spirv.id_flt_max, FLT_MAX);
565 } // spv_get_one
566
567 static uint32 spv_getvec4_zero(Context *ctx)
568 {
569 return spv_get_constant_composite(ctx, spv_get_type(ctx, STI_VEC4), ctx->spirv.id_0_0, 0.0f);
570 } // spv_getvec4_zero
571
572 static uint32 spv_getvec4_one(Context *ctx)
573 {
574 return spv_get_constant_composite(ctx, spv_get_type(ctx, STI_VEC4), ctx->spirv.id_1_0, 1.0f);
575 } // spv_getvec4_one
576
577 // Make a 4-channel vector with a value broadcast across all channels. Roughly equivalent to `vec4(value)` in GLSL
578 static uint32 spv_vectorbroadcast(Context *ctx, uint32 tid, uint32 value)
579 {
580 uint32 result = spv_bumpid(ctx);
581 push_output(ctx, &ctx->mainline);
582 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, tid, result, value, value, value, value);
583 pop_output(ctx);
584 return result;
585 } // spv_vectorbroadcast
586
587 static void spv_branch_push(Context *ctx, uint32 id_merge, uint32 patch_offset)
588 {
589 assert(((size_t)ctx->branch_labels_stack_index) < STATICARRAYLEN(ctx->branch_labels_stack));
590 int pos = ctx->branch_labels_stack_index++;
591 ctx->branch_labels_stack[pos] = id_merge;
592 ctx->branch_labels_patch_stack[pos] = patch_offset;
593 } // spv_branch_push
594
595 static void spv_branch_get(Context *ctx, uint32* out_id_merge, uint32* out_patch_offset)
596 {
597 assert(ctx->branch_labels_stack_index > 0);
598 int pos = ctx->branch_labels_stack_index - 1;
599 *out_id_merge = ctx->branch_labels_stack[pos];
600 *out_patch_offset = ctx->branch_labels_patch_stack[pos];
601 } // spv_branch_get
602
603 static void spv_branch_pop(Context *ctx, uint32* out_id_merge, uint32* out_patch_offset)
604 {
605 spv_branch_get(ctx, out_id_merge, out_patch_offset);
606 ctx->branch_labels_stack_index--;
607 } // spv_branch_pop
608
609 static void spv_loop_push(Context *ctx, const SpirvLoopInfo *loop)
610 {
611 assert(((size_t)ctx->spirv.loop_stack_idx) < STATICARRAYLEN(ctx->spirv.loop_stack));
612 int pos = ctx->spirv.loop_stack_idx++;
613 ctx->spirv.loop_stack[pos] = *loop;
614 } // spv_loop_push
615
616 static void spv_loop_get(Context *ctx, SpirvLoopInfo *loop)
617 {
618 assert(ctx->spirv.loop_stack_idx > 0);
619 int pos = ctx->spirv.loop_stack_idx - 1;
620 *loop = ctx->spirv.loop_stack[pos];
621 } // spv_loop_get
622
623 static void spv_loop_pop(Context *ctx, SpirvLoopInfo *loop)
624 {
625 spv_loop_get(ctx, loop);
626 ctx->spirv.loop_stack_idx--;
627 } // spv_loop_pop
628
629 static uint32 spv_loop_get_aL(Context *ctx)
630 {
631 int i;
632
633 // Find the first enclosing loop..endloop. There may be rep..endrep nested inside, so it might
634 // not be at the top of the stack.
635 for (i = ctx->spirv.loop_stack_idx - 1; i >= 0; i--)
636 {
637 uint32 id_aL = ctx->spirv.loop_stack[i].id_aL;
638 if (id_aL)
639 return id_aL;
640 } // for
641
642 assert(!"Referencing loop counter register aL in code not part of loop..endloop region.");
643 return 0;
644 } // spv_loop_get_aL
645
646 static SpvOp spv_get_comparison(Context *ctx)
647 {
648 static const SpvOp spv_cmp_ops[] = {
649 SpvOpUndef,
650 SpvOpFOrdGreaterThan,
651 SpvOpFOrdEqual,
652 SpvOpFOrdGreaterThanEqual,
653 SpvOpFOrdLessThan,
654 SpvOpFOrdNotEqual,
655 SpvOpFOrdLessThanEqual,
656 };
657
658 if (ctx->instruction_controls >= STATICARRAYLEN(spv_cmp_ops))
659 {
660 fail(ctx, "unknown comparison control");
661 return SpvOpUndef;
662 } // if
663
664 return spv_cmp_ops[ctx->instruction_controls];
665 } // spv_get_comparison
666
667 static void spv_check_read_reg_id(Context *ctx, RegisterList *r)
668 {
669 if (r->spirv.iddecl == 0)
670 {
671 assert(r->regtype != REG_TYPE_SAMPLER || (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 1, 4)));
672 assert(r->regtype != REG_TYPE_TEXTURE || (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 1, 4)));
673 switch (r->regtype)
674 {
675 case REG_TYPE_SAMPLER: // s# (only ps_1_1)
676 case REG_TYPE_TEXTURE: // t# (only ps_1_1)
677 case REG_TYPE_INPUT: // v#
678 case REG_TYPE_TEMP: // r#
679 case REG_TYPE_CONST: // c#
680 case REG_TYPE_CONSTINT: // i#
681 case REG_TYPE_CONSTBOOL: // b#
682 case REG_TYPE_LABEL: // l#
683 case REG_TYPE_PREDICATE: // p0
684 r->spirv.iddecl = spv_bumpid(ctx);
685 break;
686
687 case REG_TYPE_LOOP: // aL
688 r->spirv.iddecl = spv_loop_get_aL(ctx);
689 break;
690
691 default:
692 {
693 char varname[64];
694 get_SPIRV_varname_in_buf(ctx, r->regtype, r->regnum, varname, sizeof(varname));
695 failf(ctx, "register type %s is unimplemented\n", varname);
696 break;
697 } // default
698 } // switch
699 } // if
700 } // spv_check_read_reg_id
701
702 static void spv_check_write_reg_id(Context *ctx, RegisterList *r)
703 {
704 if (r->spirv.iddecl == 0)
705 {
706 switch (r->regtype)
707 {
708 // These registers require no declarations, so we can just create them as we see them
709 case REG_TYPE_ADDRESS:
710 case REG_TYPE_TEMP:
711 case REG_TYPE_RASTOUT:
712 case REG_TYPE_COLOROUT:
713 case REG_TYPE_TEXCRDOUT:
714 case REG_TYPE_DEPTHOUT:
715 case REG_TYPE_ATTROUT:
716 case REG_TYPE_PREDICATE:
717 r->spirv.iddecl = spv_bumpid(ctx);
718 break;
719
720 // Other register types should be explicitly declared, so it is an error for them to have iddecl == 0 by now
721 default:
722 {
723 char varname[64];
724 get_SPIRV_varname_in_buf(ctx, r->regtype, r->regnum, varname, sizeof(varname));
725 failf(ctx, "tried to write to undeclared register %s\n", varname);
726 break;
727 } // default
728 } // switch
729 } // if
730 } // spv_check_write_reg_id
731
732 static uint32 spv_ptrimage_from_texturetype(Context *ctx, TextureType ttype)
733 {
734 switch (ttype)
735 {
736 case TEXTURE_TYPE_2D:
737 return spv_get_type(ctx, STI_PTR_IMAGE2D);
738 case TEXTURE_TYPE_CUBE:
739 return spv_get_type(ctx, STI_PTR_IMAGECUBE);
740 case TEXTURE_TYPE_VOLUME:
741 return spv_get_type(ctx, STI_PTR_IMAGE3D);
742 default:
743 fail(ctx, "BUG: used a sampler we don't know how to define.");
744 return 0;
745 } // switch
746 } // spv_ptrimage_from_texturetype
747
748 static uint32 spv_image_from_texturetype(Context *ctx, TextureType ttype)
749 {
750 switch (ttype)
751 {
752 case TEXTURE_TYPE_2D:
753 return spv_get_type(ctx, STI_IMAGE2D);
754 case TEXTURE_TYPE_CUBE:
755 return spv_get_type(ctx, STI_IMAGECUBE);
756 case TEXTURE_TYPE_VOLUME:
757 return spv_get_type(ctx, STI_IMAGE3D);
758 default:
759 fail(ctx, "BUG: used a sampler we don't know how to define.");
760 return 0;
761 } // switch
762 } // spv_ptrimage_from_texturetype
763
764 static uint32 spv_access_uniform(Context *ctx, SpirvTypeIdx sti_ptr, RegisterType regtype, uint32 id_offset)
765 {
766 uint32 tid_ptr = spv_get_type(ctx, sti_ptr);
767 uint32 id_arr = spv_get_uniform_array_id(ctx, regtype);
768 uint32 id_access = spv_bumpid(ctx);
769 push_output(ctx, &ctx->mainline);
770 if (ctx->spirv.mode == SPIRV_MODE_VK)
771 {
772 uint32 id_uniform_block = ctx->spirv.id_uniform_block;
773 if (id_uniform_block == 0)
774 {
775 id_uniform_block = spv_bumpid(ctx);
776 ctx->spirv.id_uniform_block = id_uniform_block;
777 } // if
778 spv_emit(ctx, 4+2, SpvOpAccessChain, tid_ptr, id_access, id_uniform_block, id_arr, id_offset);
779 } // if
780 else
781 {
782 spv_emit(ctx, 4+1, SpvOpAccessChain, tid_ptr, id_access, id_arr, id_offset);
783 } // else
784 pop_output(ctx);
785 return id_access;
786 } // spv_access_uniform
787
788 static void spv_add_attrib_fixup(Context *ctx, RegisterList *r, unsigned int type_offset, unsigned int opcode_offset)
789 {
790 uint32* next_types;
791 uint32* next_opcodes;
792
793 assert(r != NULL);
794 #define TYPE_LOAD_OFFSET ctx->spirv.patch_table.attrib_type_load_offsets[r->usage][r->index]
795
796 next_types = (uint32*) Malloc(ctx, sizeof(uint32) * (TYPE_LOAD_OFFSET.num_loads + 1));
797 next_opcodes = (uint32*) Malloc(ctx, sizeof(uint32) * (TYPE_LOAD_OFFSET.num_loads + 1));
798
799 memcpy(next_types, TYPE_LOAD_OFFSET.load_types, sizeof(uint32) * TYPE_LOAD_OFFSET.num_loads);
800 memcpy(next_opcodes, TYPE_LOAD_OFFSET.load_opcodes, sizeof(uint32) * TYPE_LOAD_OFFSET.num_loads);
801
802 Free(ctx, TYPE_LOAD_OFFSET.load_types);
803 Free(ctx, TYPE_LOAD_OFFSET.load_opcodes);
804
805 TYPE_LOAD_OFFSET.load_types = next_types;
806 TYPE_LOAD_OFFSET.load_opcodes = next_opcodes;
807
808 TYPE_LOAD_OFFSET.load_types[TYPE_LOAD_OFFSET.num_loads] = type_offset;
809 TYPE_LOAD_OFFSET.load_opcodes[TYPE_LOAD_OFFSET.num_loads] = opcode_offset;
810
811 TYPE_LOAD_OFFSET.num_loads += 1;
812 #undef TYPE_LOAD_OFFSET
813 } // spv_add_attrib_fixup
814
815 static SpirvResult spv_loadreg(Context *ctx, RegisterList *r)
816 {
817 const RegisterType regtype = r->regtype;
818 uint32 copy_id;
819
820 spv_check_read_reg_id(ctx, r);
821
822 uint32 id_src = r->spirv.iddecl;
823 SpirvResult result;
824 if (regtype == REG_TYPE_SAMPLER)
825 {
826 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, r->regnum);
827 result.tid = spv_image_from_texturetype(ctx, (TextureType)sreg->index);
828 } // if
829 else if (regtype == REG_TYPE_CONSTBOOL)
830 {
831 if (!r->spirv.is_ssa)
832 id_src = spv_access_uniform(ctx, STI_PTR_INT_U, regtype, r->spirv.iddecl);
833
834 result.tid = spv_get_type(ctx, STI_INT);
835 } // else if
836 else if (regtype == REG_TYPE_CONSTINT)
837 {
838 if (!r->spirv.is_ssa)
839 id_src = spv_access_uniform(ctx, STI_PTR_IVEC4_U, regtype, r->spirv.iddecl);
840
841 result.tid = spv_get_type(ctx, STI_IVEC4);
842 } // else if
843 else if (regtype == REG_TYPE_CONST)
844 {
845 if (!r->spirv.is_ssa)
846 id_src = spv_access_uniform(ctx, STI_PTR_VEC4_U, regtype, r->spirv.iddecl);
847
848 result.tid = spv_get_type(ctx, STI_VEC4);
849 } // else if
850 else if (regtype == REG_TYPE_LOOP)
851 result.tid = spv_get_type(ctx, STI_INT);
852 else if (regtype == REG_TYPE_PREDICATE)
853 result.tid = spv_get_type(ctx, STI_BVEC4);
854 else
855 result.tid = spv_get_type(ctx, STI_VEC4);
856
857 // Constants can be used directly, no need to load them.
858 assert(r->spirv.is_ssa == 0 || r->spirv.is_ssa == 1);
859 if (r->spirv.is_ssa)
860 {
861 result.id = r->spirv.iddecl;
862 return result;
863 } // if
864
865 assert(id_src);
866 result.id = spv_bumpid(ctx);
867
868 push_output(ctx, &ctx->mainline);
869 spv_emit(ctx, 4, SpvOpLoad, result.tid, result.id, id_src);
870 if (shader_is_vertex(ctx) && regtype == REG_TYPE_INPUT)
871 {
872 copy_id = spv_bumpid(ctx);
873 spv_emit(ctx, 4, SpvOpCopyObject, result.tid, copy_id, result.id);
874 result.id = copy_id;
875 pop_output(ctx);
876
877 // Store the offsets of:
878 // - OpLoad's type id, to change the input type
879 // - OpCopyObject's opcode, to change to OpConvert if needed
880 spv_add_attrib_fixup(ctx,
881 reglist_find(&ctx->attributes, r->regtype, r->regnum),
882 (buffer_size(ctx->mainline) >> 2) - 7,
883 (buffer_size(ctx->mainline) >> 2) - 4);
884 } // if
885 else
886 {
887 // Nothing left to do for this register
888 pop_output(ctx);
889 } // else
890
891 return result;
892 } // spv_loadreg
893
894 static uint32 spv_emit_swizzle(Context *ctx, uint32 arg, uint32 rtid, const int swizzle, const int writemask)
895 {
896 uint32 result = spv_bumpid(ctx);
897
898 const int writemask0 = (writemask >> 0) & 0x1;
899 const int writemask1 = (writemask >> 1) & 0x1;
900 const int writemask2 = (writemask >> 2) & 0x1;
901 const int writemask3 = (writemask >> 3) & 0x1;
902
903 const uint32 swizzle_x = (swizzle >> 0) & 0x3;
904 const uint32 swizzle_y = (swizzle >> 2) & 0x3;
905 const uint32 swizzle_z = (swizzle >> 4) & 0x3;
906 const uint32 swizzle_w = (swizzle >> 6) & 0x3;
907
908 push_output(ctx, &ctx->mainline);
909 // OpVectorShuffle takes two vectors to shuffle, but to do a swizzle
910 // operation we can just ignore the second argument (meaning it can be
911 // anything, and I am just making it `arg` for convenience)
912 uint32 word_count = 5 + writemask0 + writemask1 + writemask2 + writemask3;
913 spv_emit_part(ctx, word_count, 5, SpvOpVectorShuffle, rtid, result, arg, arg);
914 if (writemask0) spv_emit_word(ctx, swizzle_x);
915 if (writemask1) spv_emit_word(ctx, swizzle_y);
916 if (writemask2) spv_emit_word(ctx, swizzle_z);
917 if (writemask3) spv_emit_word(ctx, swizzle_w);
918 pop_output(ctx);
919
920 return result;
921 } // spv_emit_swizzle
922
923 SpirvResult spv_swizzle(Context *ctx, SpirvResult arg, const int swizzle, const int writemask)
924 {
925 int i;
926
927 // Nothing to do, so return the same SSA value
928 if (no_swizzle(swizzle) && writemask_xyzw(writemask))
929 return arg;
930
931 assert(arg.tid != 0);
932 assert(writemask == 1
933 || writemask == 3
934 || writemask == 7
935 || writemask == 15
936 );
937
938 SpirvTypeIdx sti_arg = STI_VOID;
939 for (i = STI_CORE_START_; i < STI_CORE_END_; i++)
940 {
941 if (ctx->spirv.tid[i] == arg.tid)
942 {
943 sti_arg = (SpirvTypeIdx)i;
944 break;
945 } // if
946 } // for
947 assert(sti_arg != STI_VOID);
948
949 // We should not leave any value undefined, as it may end up used (eg. dot
950 // product), which will make everything relying on it's result undefined.
951 // Therefore, we specifically determine true dimensionality of the result.
952 int resdim = 0;
953 switch (writemask)
954 {
955 case 1:
956 resdim = 1;
957 break;
958
959 case 3:
960 resdim = 2;
961 break;
962
963 case 7:
964 resdim = 3;
965 break;
966
967 case 15:
968 resdim = 4;
969 break;
970
971 default:
972 failf(ctx, "Unexpected write mask in swizzle: 0x%X");
973 assert(0);
974 break;
975 } // switch
976
977 SpirvTypeIdx sti_result = spv_change_base_type_vec_dim(sti_arg, resdim);
978
979 SpirvResult result = {0};
980 result.id = (resdim != 1 || sti_arg != sti_result) ? spv_bumpid(ctx) : arg.id;
981 result.tid = spv_get_type(ctx, sti_result);
982 assert(result.tid != 0);
983
984 push_output(ctx, &ctx->mainline);
985 if (resdim != 1)
986 {
987 // OpVectorShuffle takes two vectors to shuffle, but to do a swizzle
988 // operation we can just ignore the second argument (meaning it can be
989 // anything, and I am just making it `arg` for convenience)
990 spv_emit_part(ctx, 5 + resdim, 5, SpvOpVectorShuffle, result.tid, result.id, arg.id, arg.id);
991
992 for (i = 0; i < resdim; i++)
993 spv_emit_word(ctx, (swizzle >> (2*i)) & 0x3);
994 } // if
995 else if (sti_arg != sti_result)
996 {
997 // OpVectorShuffle may not produce a scalar. Instead we use OpCompositeExtract.
998 spv_emit(ctx, 5, SpvOpCompositeExtract, result.tid, result.id, arg.id, swizzle & 0x3);
999 } // else if
1000
1001 pop_output(ctx);
1002
1003 return result;
1004 } // make_GLSL_swizzle_string
1005
1006 static SpirvResult spv_load_srcarg(Context *ctx, const size_t idx, const int writemask)
1007 {
1008 SpirvResult result = {0};
1009 if (idx >= STATICARRAYLEN(ctx->source_args))
1010 {
1011 fail(ctx, "Too many source args");
1012 return result;
1013 } // if
1014
1015 const SourceArgInfo *arg = &ctx->source_args[idx];
1016
1017 RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
1018
1019 if (arg->relative)
1020 {
1021 if (arg->regtype == REG_TYPE_INPUT)
1022 fail(ctx, "relative input array access is unimplemented");
1023 else
1024 {
1025 assert(arg->regtype == REG_TYPE_CONST);
1026 const int arrayidx = arg->relative_array->index;
1027 const int offset = arg->regnum - arrayidx;
1028 assert(offset >= 0);
1029
1030 int is_constant = (arg->relative_array->constant != NULL);
1031 uint32 id_array = 0;
1032 if (is_constant)
1033 {
1034 id_array = ctx->spirv.constant_arrays.idvec4;
1035 if (id_array == 0)
1036 {
1037 id_array = spv_bumpid(ctx);
1038 ctx->spirv.constant_arrays.idvec4 = id_array;
1039 } // if
1040 } // if
1041
1042 RegisterList *reg_rel = spv_getreg(ctx, arg->relative_regtype, arg->relative_regnum);
1043
1044 spv_check_read_reg_id(ctx, reg_rel);
1045 spv_check_read_reg_id(ctx, reg);
1046
1047 uint32 id_int = spv_get_type(ctx, STI_INT);
1048 uint32 id_offset;
1049 if (reg_rel->regtype == REG_TYPE_LOOP)
1050 id_offset = reg_rel->spirv.iddecl;
1051 else
1052 {
1053 uint32 id_pint = spv_get_type(ctx, STI_PTR_INT_P);
1054 uint32 id_compidx = spv_getscalari(ctx, arg->relative_component);
1055 uint32 id_pcomp = spv_bumpid(ctx);
1056 spv_emit(ctx, 5, SpvOpAccessChain, id_pint, id_pcomp, reg_rel->spirv.iddecl, id_compidx);
1057
1058 id_offset = spv_bumpid(ctx);
1059 spv_emit(ctx, 4, SpvOpLoad, id_int, id_offset, id_pcomp);
1060 } // else
1061
1062 if (!is_constant)
1063 {
1064 uint32 id_arraybase = reg->spirv.iddecl;
1065 uint32 id_a = id_offset;
1066 uint32 id_b = id_arraybase;
1067 id_offset = spv_bumpid(ctx);
1068 spv_emit(ctx, 5, SpvOpIAdd, id_int, id_offset, id_a, id_b);
1069 } // if
1070
1071 if (offset)
1072 {
1073 uint32 id_a = id_offset;
1074 uint32 id_b = spv_getscalari(ctx, offset);
1075 id_offset = spv_bumpid(ctx);
1076 spv_emit(ctx, 5, SpvOpIAdd, id_int, id_offset, id_a, id_b);
1077 } // if
1078
1079 uint32 id_pvalue;
1080 if (is_constant)
1081 {
1082 uint32 id_pvec4 = spv_get_type(ctx, STI_PTR_VEC4_P);
1083 id_pvalue = spv_bumpid(ctx);
1084 spv_emit(ctx, 4+1, SpvOpAccessChain, id_pvec4, id_pvalue, id_array, id_offset);
1085 } // if
1086 else
1087 {
1088 id_pvalue = spv_access_uniform(ctx, STI_PTR_VEC4_U, arg->regtype, id_offset);
1089 } // else
1090
1091 result.tid = spv_get_type(ctx, STI_VEC4);
1092 result.id = spv_bumpid(ctx);
1093 spv_emit(ctx, 4, SpvOpLoad, result.tid, result.id, id_pvalue);
1094 } // else
1095 } // if
1096 else
1097 result = spv_loadreg(ctx, reg);
1098
1099 result = spv_swizzle(ctx, result, arg->swizzle, writemask);
1100
1101 switch (arg->src_mod)
1102 {
1103 case SRCMOD_NEGATE:
1104 {
1105 uint32 id_neg = spv_bumpid(ctx);
1106 spv_emit(ctx, 4, SpvOpFNegate, result.tid, id_neg, result.id);
1107 result.id = id_neg;
1108 break;
1109 } // case
1110
1111 case SRCMOD_BIASNEGATE:
1112 {
1113 uint32 id_half = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_0_5, 0.5f);
1114 uint32 id_tmp = spv_bumpid(ctx);
1115 uint32 id_new = spv_bumpid(ctx);
1116 spv_emit(ctx, 5, SpvOpFSub, result.tid, id_tmp, result.id, id_half);
1117 spv_emit(ctx, 4, SpvOpFNegate, result.tid, id_new, id_tmp);
1118 result.id = id_new;
1119 break;
1120 } // case
1121
1122 case SRCMOD_BIAS:
1123 {
1124 uint32 id_half = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_0_5, 0.5f);
1125 uint32 id_new = spv_bumpid(ctx);
1126 spv_emit(ctx, 5, SpvOpFSub, result.tid, id_new, result.id, id_half);
1127 result.id = id_new;
1128 break;
1129 } // case
1130
1131 case SRCMOD_SIGNNEGATE:
1132 {
1133 uint32 id_half = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_0_5, 0.5f);
1134 uint32 id_two = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_2_0, 2.0f);
1135 uint32 id_tmp0 = spv_bumpid(ctx);
1136 uint32 id_tmp1 = spv_bumpid(ctx);
1137 uint32 id_new = spv_bumpid(ctx);
1138 spv_emit(ctx, 5, SpvOpFSub, result.tid, id_tmp0, result.id, id_half);
1139 spv_emit(ctx, 5, SpvOpFMul, result.tid, id_tmp1, id_tmp0, id_two);
1140 spv_emit(ctx, 4, SpvOpFNegate, result.tid, id_new, id_tmp1);
1141 result.id = id_new;
1142 break;
1143 } // case
1144
1145 case SRCMOD_SIGN:
1146 {
1147 uint32 id_half = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_0_5, 0.5f);
1148 uint32 id_two = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_2_0, 2.0f);
1149 uint32 id_tmp = spv_bumpid(ctx);
1150 uint32 id_new = spv_bumpid(ctx);
1151 spv_emit(ctx, 5, SpvOpFSub, result.tid, id_tmp, result.id, id_half);
1152 spv_emit(ctx, 5, SpvOpFMul, result.tid, id_new, id_tmp, id_two);
1153 result.id = id_new;
1154 break;
1155 } // case
1156
1157 case SRCMOD_COMPLEMENT:
1158 {
1159 uint32 id_one = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_1_0, 1.0f);
1160 uint32 id_new = spv_bumpid(ctx);
1161 spv_emit(ctx, 5, SpvOpFSub, result.tid, id_new, id_one, result.id);
1162 result.id = id_new;
1163 break;
1164 } // case
1165
1166 case SRCMOD_X2NEGATE:
1167 {
1168 uint32 id_two = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_2_0, 2.0f);
1169 uint32 id_tmp = spv_bumpid(ctx);
1170 uint32 id_new = spv_bumpid(ctx);
1171 spv_emit(ctx, 5, SpvOpFMul, result.tid, id_tmp, result.id, id_two);
1172 spv_emit(ctx, 4, SpvOpFNegate, result.tid, id_new, id_tmp);
1173 result.id = id_new;
1174 break;
1175 } // case
1176
1177 case SRCMOD_X2:
1178 {
1179 uint32 id_two = spv_get_constant_composite(ctx, result.tid, ctx->spirv.id_2_0, 2.0f);
1180 uint32 id_new = spv_bumpid(ctx);
1181 spv_emit(ctx, 5, SpvOpFMul, result.tid, id_new, result.id, id_two);
1182 result.id = id_new;
1183 break;
1184 } // case
1185
1186 // case SRCMOD_DZ:
1187 // fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME
1188 // postmod_str = "_dz";
1189 // break;
1190
1191 // case SRCMOD_DW:
1192 // fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME
1193 // postmod_str = "_dw";
1194 // break;
1195
1196 case SRCMOD_ABSNEGATE:
1197 {
1198 uint32 id_abs = spv_bumpid(ctx);
1199 uint32 id_neg = spv_bumpid(ctx);
1200 spv_emit(ctx, 5 + 1, SpvOpExtInst, result.tid, id_abs, spv_getext(ctx), GLSLstd450FAbs, result.id);
1201 spv_emit(ctx, 4, SpvOpFNegate, result.tid, id_neg, id_abs);
1202 result.id = id_neg;
1203 break;
1204 } // case
1205
1206 case SRCMOD_ABS:
1207 {
1208 uint32 id_abs = spv_bumpid(ctx);
1209 spv_emit(ctx, 5 + 1, SpvOpExtInst, result.tid, id_abs, spv_getext(ctx), GLSLstd450FAbs, result.id);
1210 result.id = id_abs;
1211 break;
1212 } // case
1213
1214 case SRCMOD_NOT:
1215 {
1216 // We can't do OpLogicalNot on ints, so do (x ^ 1) instead
1217 uint32 id_one = spv_getscalari(ctx, 1);
1218 uint32 id_not = spv_bumpid(ctx);
1219 spv_emit(ctx, 5, SpvOpBitwiseXor, result.tid, id_not, result.id, id_one);
1220 result.id = id_not;
1221 break;
1222 } // case
1223
1224 case SRCMOD_NONE:
1225 case SRCMOD_TOTAL:
1226 break; // stop compiler whining.
1227
1228 default:
1229 failf(ctx, "unsupported source modifier %d", arg->src_mod);
1230 return result;
1231 } // switch
1232
1233 return result;
1234 } // spv_load_srcarg
1235
1236 static inline SpirvResult spv_load_srcarg_full(Context *ctx, const size_t idx)
1237 {
1238 return spv_load_srcarg(ctx, idx, 0xF);
1239 } // spv_load_srcarg_full
1240
1241 static void spv_assign_destarg(Context *ctx, SpirvResult value)
1242 {
1243 const DestArgInfo *arg = &ctx->dest_arg;
1244 RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
1245
1246 spv_check_write_reg_id(ctx, reg);
1247
1248 if (arg->writemask == 0)
1249 {
1250 // Return without updating the reg->spirv.iddecl (all-zero writemask = no-op)
1251 return;
1252 } // if
1253
1254 if (arg->result_mod & MOD_SATURATE)
1255 {
1256 uint32 ext, zero, one, new_value;
1257
1258 // Don't inline these, compilers will run the varargs in different orders
1259 new_value = spv_bumpid(ctx);
1260 one = spv_get_one(ctx, value.tid);
1261 zero = spv_get_zero(ctx, value.tid);
1262 ext = spv_getext(ctx);
1263
1264 push_output(ctx, &ctx->mainline);
1265 spv_emit(ctx, 5 + 3, SpvOpExtInst,
1266 value.tid, new_value, ext, GLSLstd450FClamp, value.id, zero, one
1267 );
1268 pop_output(ctx);
1269 value.id = new_value;
1270 } // if
1271
1272 // MSDN says MOD_PP is a hint and many implementations ignore it. So do we.
1273
1274 // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
1275 assert((arg->result_mod & MOD_CENTROID) == 0);
1276
1277 if (ctx->predicated)
1278 {
1279 fail(ctx, "predicated destinations unsupported"); // !!! FIXME
1280 return;
1281 } // if
1282
1283 if (arg->result_shift)
1284 {
1285 float factor = 1.0f;
1286 uint32* cache = ctx->spirv.id_1_0;
1287 switch (arg->result_shift)
1288 {
1289 case 0x1: factor = 2.0f; cache = ctx->spirv.id_2_0; break;
1290 case 0x2: factor = 4.0f; cache = ctx->spirv.id_4_0; break;
1291 case 0x3: factor = 8.0f; cache = ctx->spirv.id_8_0; break;
1292 case 0xD: factor = 0.125f; cache = ctx->spirv.id_0_125; break;
1293 case 0xE: factor = 0.25f; cache = ctx->spirv.id_0_25; break;
1294 case 0xF: factor = 0.5f; cache = ctx->spirv.id_0_5; break;
1295 default:
1296 failf(ctx, "unexpected result shift %d", arg->result_shift);
1297 } // switch
1298
1299 uint32 id_factor = spv_get_constant_composite(ctx, value.tid, cache, factor);
1300 push_output(ctx, &ctx->mainline);
1301 uint32 id_new = spv_bumpid(ctx);
1302 spv_emit(ctx, 5, SpvOpFMul, value.tid, id_new, value.id, id_factor);
1303 pop_output(ctx);
1304 value.id = id_new;
1305 } // if
1306
1307 if (reg->regtype == REG_TYPE_DEPTHOUT
1308 || isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum))
1309 {
1310 assert(arg->writemask == 0x1);
1311 SpirvTypeIdx sti_reg = STI_FLOAT;
1312 uint32 rtid = spv_get_type(ctx, sti_reg);
1313 uint32 new_value = spv_bumpid(ctx);
1314 push_output(ctx, &ctx->mainline);
1315 spv_emit(ctx, 5, SpvOpCompositeExtract, rtid, new_value, value.id, 0);
1316 pop_output(ctx);
1317 value.tid = rtid;
1318 value.id = new_value;
1319 } // if
1320 else if (!writemask_xyzw(arg->writemask))
1321 {
1322 SpirvTypeIdx sti_reg;
1323 switch (reg->regtype)
1324 {
1325 case REG_TYPE_ADDRESS: sti_reg = STI_IVEC4; break;
1326 case REG_TYPE_PREDICATE: sti_reg = STI_BVEC4; break;
1327 default: sti_reg = STI_VEC4; break;
1328 } // switch
1329
1330 uint32 rtid = spv_get_type(ctx, sti_reg);
1331 uint32 new_value = spv_bumpid(ctx);
1332 uint32 current_value = spv_bumpid(ctx);
1333
1334 push_output(ctx, &ctx->mainline);
1335
1336 spv_emit(ctx, 4, SpvOpLoad, rtid, current_value, reg->spirv.iddecl);
1337
1338 // output id is new_value
1339 // select between current value and new value based on writemask
1340 // in the shuffle, components [0, 3] are the new value, and components
1341 // [4, 7] are the existing value
1342 spv_emit_part(ctx, 5 + 4, 5, SpvOpVectorShuffle, rtid, new_value, value.id, current_value);
1343 if (arg->writemask0) spv_emit_word(ctx, 0); else spv_emit_word(ctx, 4);
1344 if (arg->writemask1) spv_emit_word(ctx, 1); else spv_emit_word(ctx, 5);
1345 if (arg->writemask2) spv_emit_word(ctx, 2); else spv_emit_word(ctx, 6);
1346 if (arg->writemask3) spv_emit_word(ctx, 3); else spv_emit_word(ctx, 7);
1347
1348 pop_output(ctx);
1349
1350 value.tid = rtid;
1351 value.id = new_value;
1352 } // if
1353
1354 switch (reg->regtype)
1355 {
1356 case REG_TYPE_OUTPUT:
1357 case REG_TYPE_ADDRESS:
1358 case REG_TYPE_TEMP:
1359 case REG_TYPE_DEPTHOUT:
1360 case REG_TYPE_COLOROUT:
1361 case REG_TYPE_RASTOUT:
1362 case REG_TYPE_ATTROUT:
1363 case REG_TYPE_PREDICATE:
1364 push_output(ctx, &ctx->mainline);
1365 spv_emit(ctx, 3, SpvOpStore, reg->spirv.iddecl, value.id);
1366 pop_output(ctx);
1367 break;
1368
1369 default:
1370 {
1371 char varname[64];
1372 get_SPIRV_varname_in_buf(ctx, reg->regtype, reg->regnum, varname, sizeof(varname));
1373 failf(ctx, "register %s is unimplemented for storing", varname);
1374 break;
1375 } // default
1376 } // switch
1377 } // spv_assign_destarg
1378
1379 static void spv_emit_vs_main_end(Context* ctx)
1380 {
1381 #if SUPPORT_PROFILE_GLSPIRV
1382 #if defined(MOJOSHADER_DEPTH_CLIPPING) || defined(MOJOSHADER_FLIP_RENDERTARGET)
1383 if (!ctx->profile_supports_glspirv || !shader_is_vertex(ctx))
1384 return;
1385
1386 uint32 tid_void = spv_get_type(ctx, STI_VOID);
1387 uint32 tid_func = spv_get_type(ctx, STI_FUNC_VOID);
1388 uint32 id_func = ctx->spirv.id_vs_main_end;
1389 uint32 id_label = spv_bumpid(ctx);
1390 assert(id_func != 0);
1391
1392 push_output(ctx, &ctx->mainline);
1393 spv_emit(ctx, 5, SpvOpFunction, tid_void, id_func, SpvFunctionControlMaskNone, tid_func);
1394 spv_emit(ctx, 2, SpvOpLabel, id_label);
1395
1396 RegisterList *reg;
1397 for (reg = ctx->used_registers.next; reg != NULL; reg = reg->next)
1398 {
1399 if (reg->usage == MOJOSHADER_USAGE_POSITION &&
1400 (reg->regtype == REG_TYPE_RASTOUT || reg->regtype == REG_TYPE_OUTPUT))
1401 break;
1402 } // for
1403 SpirvResult output = spv_loadreg(ctx, reg);
1404 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
1405 uint32 id_new_output;
1406
1407 #ifdef MOJOSHADER_FLIP_RENDERTARGET
1408 // gl_Position.y = gl_Position.y * vpFlip;
1409 uint32 tid_pvpflip = spv_bumpid(ctx);
1410 uint32 id_old_y = spv_bumpid(ctx);
1411 uint32 id_pvpflip = spv_bumpid(ctx);
1412 uint32 id_vpflip = spv_bumpid(ctx);
1413 uint32 id_new_y = spv_bumpid(ctx);
1414 id_new_output = spv_bumpid(ctx);
1415
1416 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_old_y, output.id, 1);
1417 spv_emit(ctx, 4, SpvOpLoad, tid_float, id_vpflip, id_pvpflip);
1418 spv_emit(ctx, 5, SpvOpFMul, tid_float, id_new_y, id_old_y, id_vpflip);
1419 spv_emit(ctx, 6, SpvOpCompositeInsert, output.tid, id_new_output, id_new_y, output.id, 1);
1420 output.id = id_new_output;
1421
1422 push_output(ctx, &ctx->mainline_intro);
1423 spv_emit(ctx, 4, SpvOpTypePointer, tid_pvpflip, SpvStorageClassUniformConstant, tid_float);
1424 spv_emit(ctx, 4, SpvOpVariable, tid_pvpflip, id_pvpflip, SpvStorageClassUniformConstant);
1425 pop_output(ctx);
1426
1427 spv_output_name(ctx, id_pvpflip, "vpFlip");
1428 ctx->spirv.patch_table.vpflip.offset = spv_output_location(ctx, id_pvpflip, ~0u);
1429 #endif // MOJOSHADER_FLIP_RENDERTARGET
1430
1431 #ifdef MOJOSHADER_DEPTH_CLIPPING
1432 // gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;
1433 uint32 id_2 = spv_getscalarf(ctx, 2.0f);
1434 uint32 id_old_z = spv_bumpid(ctx);
1435 uint32 id_old_w = spv_bumpid(ctx);
1436 uint32 id_2z = spv_bumpid(ctx);
1437 uint32 id_new_z = spv_bumpid(ctx);
1438 id_new_output = spv_bumpid(ctx);
1439
1440 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_old_z, output.id, 2);
1441 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_old_w, output.id, 3);
1442 spv_emit(ctx, 5, SpvOpFMul, tid_float, id_2z, id_old_z, id_2);
1443 spv_emit(ctx, 5, SpvOpFSub, tid_float, id_new_z, id_2z, id_old_w);
1444 spv_emit(ctx, 6, SpvOpCompositeInsert, output.tid, id_new_output, id_new_z, output.id, 2);
1445 output.id = id_new_output;
1446 #endif // MOJOSHADER_DEPTH_CLIPPING
1447
1448 spv_emit(ctx, 3, SpvOpStore, reg->spirv.iddecl, output.id);
1449 spv_emit(ctx, 1, SpvOpReturn);
1450 spv_emit(ctx, 1, SpvOpFunctionEnd);
1451 pop_output(ctx);
1452
1453 spv_output_name(ctx, id_func, "vs_epilogue");
1454 #endif // defined(MOJOSHADER_DEPTH_CLIPPING) || defined(MOJOSHADER_FLIP_RENDERTARGET)
1455 #endif // SUPPORT_PROFILE_GLSPIRV
1456 } // spv_emit_vs_main_end
1457
1458 static void spv_emit_func_lit(Context *ctx)
1459 {
1460 if (!ctx->spirv.id_func_lit)
1461 return;
1462
1463 // vec4 LIT(const vec4 src)
1464 // {
1465 // float retval_y, retval_z;
1466 // if (src.x > 0.0) {
1467 // retval_y = src.x;
1468 // if (src.y > 0.0) {
1469 // float power = clamp(src.w, -127.9961, 127.9961);
1470 // retval_z = pow(src.y, power);
1471 // } else {
1472 // retval_z = 0.0;
1473 // }
1474 // } else {
1475 // retval_y = 0.0;
1476 // retval_z = 0.0;
1477 // }
1478 // vec4 retval = vec4(1.0, retval_y, retval_z, 1.0);
1479 // return retval;
1480 // }
1481
1482 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
1483 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
1484 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1485 uint32 tid_func = spv_get_type(ctx, STI_FUNC_LIT);
1486 uint32 id_func = ctx->spirv.id_func_lit;
1487 uint32 id_src = spv_bumpid(ctx);
1488 uint32 id_block_start = spv_bumpid(ctx);
1489 uint32 id_src_x = spv_bumpid(ctx);
1490 uint32 id_src_x_pos = spv_bumpid(ctx);
1491 uint32 id_0_0 = spv_get_zero(ctx, tid_float);
1492 uint32 id_branch0_true = spv_bumpid(ctx);
1493 uint32 id_src_y = spv_bumpid(ctx);
1494 uint32 id_src_y_pos = spv_bumpid(ctx);
1495 uint32 id_branch1_true = spv_bumpid(ctx);
1496 uint32 id_src_w = spv_bumpid(ctx);
1497 uint32 id_maxp = spv_getscalarf(ctx, 127.9961f);
1498 uint32 id_maxp_neg = spv_getscalarf(ctx, -127.9961f);
1499 uint32 id_power = spv_bumpid(ctx);
1500 uint32 id_pow_result = spv_bumpid(ctx);
1501 uint32 id_branch1_merge = spv_bumpid(ctx);
1502 uint32 id_branch1_result = spv_bumpid(ctx);
1503 uint32 id_branch0_merge = spv_bumpid(ctx);
1504 uint32 id_result_y = spv_bumpid(ctx);
1505 uint32 id_result_z = spv_bumpid(ctx);
1506 uint32 id_1_0 = spv_get_one(ctx, tid_float);
1507 uint32 id_result = spv_bumpid(ctx);
1508
1509 push_output(ctx, &ctx->mainline);
1510 spv_emit(ctx, 5, SpvOpFunction, tid_vec4, id_func, SpvFunctionControlMaskNone, tid_func);
1511 spv_emit(ctx, 3, SpvOpFunctionParameter, tid_vec4, id_src);
1512
1513 // id_block_start
1514 spv_emit(ctx, 2, SpvOpLabel, id_block_start);
1515 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src_x, id_src, 0);
1516 spv_emit(ctx, 5, SpvOpFOrdGreaterThan, tid_bool, id_src_x_pos, id_src_x, id_0_0);
1517 spv_emit(ctx, 3, SpvOpSelectionMerge, id_branch0_merge, 0);
1518 spv_emit(ctx, 4, SpvOpBranchConditional, id_src_x_pos, id_branch0_true, id_branch0_merge);
1519
1520 // id_branch0_true
1521 spv_emit(ctx, 2, SpvOpLabel, id_branch0_true);
1522 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src_y, id_src, 1);
1523 spv_emit(ctx, 5, SpvOpFOrdGreaterThan, tid_bool, id_src_y_pos, id_src_y, id_0_0);
1524 spv_emit(ctx, 3, SpvOpSelectionMerge, id_branch1_merge, 0);
1525 spv_emit(ctx, 4, SpvOpBranchConditional, id_src_y_pos, id_branch1_true, id_branch1_merge);
1526
1527 // id_branch1_true
1528 spv_emit(ctx, 2, SpvOpLabel, id_branch1_true);
1529 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src_w, id_src, 3);
1530 spv_emit(ctx, 5 + 3, SpvOpExtInst,
1531 tid_float, id_power, spv_getext(ctx), GLSLstd450FClamp, id_src_w, id_maxp_neg, id_maxp
1532 );
1533 spv_emit(ctx, 5 + 2, SpvOpExtInst,
1534 tid_float, id_pow_result, spv_getext(ctx), GLSLstd450Pow, id_src_y, id_power
1535 );
1536 spv_emit(ctx, 2, SpvOpBranch, id_branch1_merge);
1537
1538 // id_branch1_merge
1539 spv_emit(ctx, 2, SpvOpLabel, id_branch1_merge);
1540 spv_emit(ctx, 7, SpvOpPhi, tid_float, id_branch1_result,
1541 id_pow_result, id_branch1_true,
1542 id_0_0, id_branch0_true
1543 );
1544 spv_emit(ctx, 2, SpvOpBranch, id_branch0_merge);
1545
1546 // id_branch0_merge
1547 spv_emit(ctx, 2, SpvOpLabel, id_branch0_merge);
1548 spv_emit(ctx, 7, SpvOpPhi, tid_float, id_result_y,
1549 id_src_x, id_branch1_merge,
1550 id_0_0, id_block_start
1551 );
1552 spv_emit(ctx, 7, SpvOpPhi, tid_float, id_result_z,
1553 id_branch1_result, id_branch1_merge,
1554 id_0_0, id_block_start
1555 );
1556 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, tid_vec4, id_result,
1557 id_1_0, id_result_y, id_result_z, id_1_0
1558 );
1559 spv_emit(ctx, 2, SpvOpReturnValue, id_result);
1560 spv_emit(ctx, 1, SpvOpFunctionEnd);
1561
1562 pop_output(ctx);
1563
1564 spv_output_name(ctx, ctx->spirv.id_func_lit, "LIT");
1565 } // spv_emit_func_lit
1566
1567 static void spv_emit_func_end(Context *ctx)
1568 {
1569 push_output(ctx, &ctx->mainline);
1570
1571 #if SUPPORT_PROFILE_GLSPIRV
1572 #if defined(MOJOSHADER_DEPTH_CLIPPING) || defined(MOJOSHADER_FLIP_RENDERTARGET)
1573 if (ctx->profile_supports_glspirv
1574 && shader_is_vertex(ctx)
1575 && ctx->spirv.id_vs_main_end == 0)
1576 {
1577 ctx->spirv.id_vs_main_end = spv_bumpid(ctx);
1578 uint32 tid_void = spv_get_type(ctx, STI_VOID);
1579 uint32 id_res = spv_bumpid(ctx);
1580
1581 push_output(ctx, &ctx->mainline);
1582 spv_emit(ctx, 4, SpvOpFunctionCall, tid_void, id_res, ctx->spirv.id_vs_main_end);
1583 pop_output(ctx);
1584 } // if
1585 #endif // defined(MOJOSHADER_DEPTH_CLIPPING) || defined(MOJOSHADER_FLIP_RENDERTARGET)
1586 #endif // SUPPORT_PROFILE_GLSPIRV
1587
1588 spv_emit(ctx, 1, SpvOpReturn);
1589 spv_emit(ctx, 1, SpvOpFunctionEnd);
1590 pop_output(ctx);
1591 } // spv_emit_func_end
1592
1593 static void spv_emit_vpos_glmode(Context *ctx, uint32 id)
1594 {
1595 // In SM3.0 vPos only has x and y defined, but we should be
1596 // fine to leave the z and w attributes in that
1597 // SpvBuiltInFragCoord gives.
1598
1599 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
1600 uint32 tid_vec2 = spv_get_type(ctx, STI_VEC2);
1601 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1602 uint32 tid_pvec4i = spv_get_type(ctx, STI_PTR_VEC4_I);
1603 uint32 tid_pvec2u = spv_bumpid(ctx);
1604 uint32 tid_pvec4p = spv_get_type(ctx, STI_PTR_VEC4_P);
1605
1606 uint32 id_var_fragcoord = spv_bumpid(ctx);
1607 uint32 id_var_vposflip = spv_bumpid(ctx);
1608 uint32 id_var_vpos = id;
1609
1610 uint32 id_fragcoord = spv_bumpid(ctx);
1611 uint32 id_fragcoord_y = spv_bumpid(ctx);
1612 uint32 id_vposflip = spv_bumpid(ctx);
1613 uint32 id_vposflip_x = spv_bumpid(ctx);
1614 uint32 id_vposflip_y = spv_bumpid(ctx);
1615 uint32 id_tmp = spv_bumpid(ctx);
1616 uint32 id_vpos_y = spv_bumpid(ctx);
1617 uint32 id_vpos = spv_bumpid(ctx);
1618
1619 // vec4 gl_FragCoord = <compiler magic builtin>;
1620 // uniform vec2 vposFlip;
1621 // vec4 ps_vPos = vec4(
1622 // gl_FragCoord.x,
1623 // (gl_FragCoord.y * vposFlip.x) + vposFlip.y,
1624 // gl_FragCoord.z,
1625 // gl_FragCoord.w
1626 // );
1627
1628 push_output(ctx, &ctx->mainline_intro);
1629 // Define uniform vec2*. This is the only place that uses it right now.
1630 spv_emit(ctx, 4, SpvOpTypePointer, tid_pvec2u, SpvStorageClassUniformConstant, tid_vec2);
1631 // Define all variables involved.
1632 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4i, id_var_fragcoord, SpvStorageClassInput);
1633 spv_emit(ctx, 4, SpvOpVariable, tid_pvec2u, id_var_vposflip, SpvStorageClassUniformConstant);
1634 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4p, id_var_vpos, SpvStorageClassPrivate);
1635 pop_output(ctx);
1636
1637 spv_output_builtin(ctx, id_var_fragcoord, SpvBuiltInFragCoord);
1638 spv_output_name(ctx, id_var_vposflip, "vposFlip");
1639
1640 // Initialize vPos using vPosFlip and built in FragCoord.
1641 push_output(ctx, &ctx->mainline_top);
1642 spv_emit(ctx, 4, SpvOpLoad, tid_vec4, id_fragcoord, id_var_fragcoord);
1643 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_fragcoord_y, id_fragcoord, 1);
1644 spv_emit(ctx, 4, SpvOpLoad, tid_vec2, id_vposflip, id_var_vposflip);
1645 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_vposflip_x, id_vposflip, 0);
1646 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_vposflip_y, id_vposflip, 1);
1647 spv_emit(ctx, 5, SpvOpFMul, tid_float, id_tmp, id_fragcoord_y, id_vposflip_x);
1648 spv_emit(ctx, 5, SpvOpFAdd, tid_float, id_vpos_y, id_tmp, id_vposflip_y);
1649 spv_emit(ctx, 6, SpvOpCompositeInsert, tid_vec4, id_vpos, id_vpos_y, id_fragcoord, 1);
1650 spv_emit(ctx, 3, SpvOpStore, id_var_vpos, id_vpos);
1651 pop_output(ctx);
1652
1653 ctx->spirv.id_var_fragcoord = id_var_fragcoord;
1654 ctx->spirv.id_var_vpos = id_var_vpos;
1655 ctx->spirv.patch_table.vpflip.offset = spv_output_location(ctx, id_var_vposflip, ~0u);
1656 } // spv_emit_vpos_glmode
1657
1658 static void spv_emit_vpos_vkmode(Context *ctx, uint32 id)
1659 {
1660 // In SM3.0 vPos only has x and y defined, but we should be
1661 // fine to leave the z and w attributes in that
1662 // SpvBuiltInFragCoord gives.
1663
1664 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1665 uint32 tid_pvec4i = spv_get_type(ctx, STI_PTR_VEC4_I);
1666 uint32 tid_pvec4p = spv_get_type(ctx, STI_PTR_VEC4_P);
1667
1668 uint32 id_var_fragcoord = spv_bumpid(ctx);
1669 uint32 id_var_vpos = id;
1670
1671 uint32 id_fragcoord = spv_bumpid(ctx);
1672 uint32 id_vpos = spv_bumpid(ctx);
1673
1674 // vec4 gl_FragCoord = <compiler magic builtin>;
1675 // vec4 ps_vPos = gl_FragCoord;
1676
1677 push_output(ctx, &ctx->mainline_intro);
1678 // Define all variables involved.
1679 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4i, id_var_fragcoord, SpvStorageClassInput);
1680 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4p, id_var_vpos, SpvStorageClassPrivate);
1681 pop_output(ctx);
1682
1683 spv_output_builtin(ctx, id_var_fragcoord, SpvBuiltInFragCoord);
1684
1685 // Initialize vPos using built in FragCoord.
1686 push_output(ctx, &ctx->mainline_top);
1687 spv_emit(ctx, 4, SpvOpLoad, tid_vec4, id_fragcoord, id_var_fragcoord);
1688 spv_emit(ctx, 3, SpvOpStore, id_var_vpos, id_fragcoord);
1689 pop_output(ctx);
1690
1691 ctx->spirv.id_var_fragcoord = id_var_fragcoord;
1692 ctx->spirv.id_var_vpos = id_var_vpos;
1693 } // spv_emit_vpos_vkmode
1694
1695 static void spv_link_vs_attributes(Context *ctx, uint32 id,
1696 MOJOSHADER_usage usage, int index)
1697 {
1698 if (usage == MOJOSHADER_USAGE_POSITION && index == 0)
1699 spv_output_builtin(ctx, id, SpvBuiltInPosition);
1700 else if (usage == MOJOSHADER_USAGE_POINTSIZE && index == 0)
1701 {
1702 spv_output_builtin(ctx, id, SpvBuiltInPointSize);
1703 ctx->spirv.patch_table.attrib_offsets[usage][index] = 1;
1704 } // else if
1705 else
1706 spv_output_attrib_location(ctx, id, usage, index);
1707 } // spv_link_vs_attributes
1708
1709 static void spv_link_ps_attributes(Context *ctx, uint32 id, RegisterType regtype,
1710 MOJOSHADER_usage usage, int index)
1711 {
1712 switch (regtype)
1713 {
1714 case REG_TYPE_COLOROUT:
1715 // Per KHR_glsl_shader:
1716 // The fragment-stage built-in gl_FragColor, which implies a broadcast to all
1717 // outputs, is not present in SPIR-V. Shaders where writing to gl_FragColor
1718 // is allowed can still write to it, but it only means to write to an output:
1719 // - of the same type as gl_FragColor
1720 // - decorated with location 0
1721 // - not decorated as a built-in variable.
1722 // There is no implicit broadcast.
1723 spv_output_color_location(ctx, id, index);
1724 break;
1725 case REG_TYPE_INPUT: // v# (MOJOSHADER_USAGE_COLOR aka `oC#` in vertex shader)
1726 spv_output_attrib_location(ctx, id, usage, index);
1727 break;
1728 case REG_TYPE_TEXTURE: // t# (MOJOSHADER_USAGE_TEXCOORD aka `oT#` in vertex shader)
1729 spv_output_attrib_location(ctx, id, MOJOSHADER_USAGE_TEXCOORD, index);
1730 break;
1731 case REG_TYPE_DEPTHOUT:
1732 spv_output_builtin(ctx, id, SpvBuiltInFragDepth);
1733 ctx->spirv.hasdepth = 1;
1734 break;
1735 case REG_TYPE_MISCTYPE:
1736 // inputs
1737 switch ((MiscTypeType)index)
1738 {
1739 case MISCTYPE_TYPE_POSITION: // vPos
1740 {
1741 if (ctx->spirv.mode == SPIRV_MODE_GL)
1742 spv_emit_vpos_glmode(ctx, id);
1743 else
1744 spv_emit_vpos_vkmode(ctx, id);
1745 break;
1746 } // case
1747
1748 case MISCTYPE_TYPE_FACE: // vFace
1749 {
1750 // The much more wordy equivalent of:
1751 // bool gl_FrontFacing = <compiler magic builtin>;
1752 // vec4 vFace;
1753 // vFace = vec4(gl_FrontFacing ? 1.0 : 0.0);
1754
1755 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
1756 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
1757 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1758 uint32 tid_pbooli = spv_get_type(ctx, STI_PTR_BOOL_I);
1759 uint32 tid_pvec4p = spv_get_type(ctx, STI_PTR_VEC4_P);
1760
1761 uint32 id_1_0 = spv_getscalarf(ctx, 1.0f);
1762 uint32 id_n1_0 = spv_getscalarf(ctx, -1.0f);
1763
1764 uint32 id_var_frontfacing = spv_bumpid(ctx);
1765 uint32 id_var_vface = id;
1766
1767 uint32 id_frontfacing = spv_bumpid(ctx);
1768 uint32 id_tmp = spv_bumpid(ctx);
1769 uint32 id_vface = spv_bumpid(ctx);
1770
1771 push_output(ctx, &ctx->mainline_intro);
1772 spv_emit(ctx, 4, SpvOpVariable, tid_pbooli, id_var_frontfacing, SpvStorageClassInput);
1773 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4p, id_var_vface, SpvStorageClassPrivate);
1774 pop_output(ctx);
1775
1776 spv_output_builtin(ctx, id_var_frontfacing, SpvBuiltInFrontFacing);
1777
1778 push_output(ctx, &ctx->mainline_top);
1779 spv_emit(ctx, 4, SpvOpLoad, tid_bool, id_frontfacing, id_var_frontfacing);
1780 spv_emit(ctx, 6, SpvOpSelect, tid_float, id_tmp, id_frontfacing, id_1_0, id_n1_0);
1781 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, tid_vec4, id_vface, id_tmp, id_tmp, id_tmp, id_tmp);
1782 spv_emit(ctx, 3, SpvOpStore, id_var_vface, id_vface);
1783 pop_output(ctx);
1784
1785 ctx->spirv.id_var_frontfacing = id_var_frontfacing;
1786 ctx->spirv.id_var_vface = id_var_vface;
1787 break;
1788 } // case
1789 } // switch
1790 break;
1791 default:
1792 fail(ctx, "unknown pixel shader attribute register");
1793 } // switch
1794 } // spv_link_ps_attributes
1795
1796 static void spv_texbem(Context* ctx, int luminanceCorrection)
1797 {
1798 DestArgInfo *info = &ctx->dest_arg;
1799 uint32 sampler_idx = info->regnum;
1800 RegisterList *pSReg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, sampler_idx);
1801 RegisterList *pSrc = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum);
1802 RegisterList *pDst = spv_getreg(ctx, info->regtype, sampler_idx);
1803
1804 push_output(ctx, &ctx->mainline);
1805
1806 SpirvResult sampler = spv_loadreg(ctx, pSReg);
1807 SpirvResult src0 = spv_loadreg(ctx, pSrc);
1808 SpirvResult src1 = spv_loadreg(ctx, pDst);
1809
1810 // <dst> = texture(
1811 // <sampler>,
1812 // vec2(
1813 // (<sampler>_texbem.x * <src0>.x) + (<sampler>_texbem.z * <src0>.y) + <src1>.x,
1814 // (<sampler>_texbem.y * <src0>.x) + (<sampler>_texbem.w * <src0>.y) + <src1>.y
1815 // )
1816 // );
1817
1818 // Load 2x2 transform matrix from uniform data (stored as vec4).
1819 assert(sampler_idx < 4);
1820 uint32 id_offset = ctx->spirv.sampler_extras[sampler_idx].idtexbem;
1821 if (!id_offset)
1822 {
1823 id_offset = spv_bumpid(ctx);
1824 ctx->spirv.sampler_extras[sampler_idx].idtexbem = id_offset;
1825 } // if
1826 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1827 uint32 id_pmatrix = spv_access_uniform(ctx, STI_PTR_VEC4_U, REG_TYPE_CONST, id_offset);
1828 SpirvResult matrix;
1829 matrix.tid = tid_vec4;
1830 matrix.id = spv_bumpid(ctx);
1831 spv_emit(ctx, 4, SpvOpLoad, matrix.tid, matrix.id, id_pmatrix);
1832
1833 // transform src0 using matrix and translate result using src1
1834 // ie. src0 * matrix + src1
1835 SpirvResult matrix_xy = spv_swizzle(ctx, matrix, 0x4, 0x3);
1836 SpirvResult matrix_zw = spv_swizzle(ctx, matrix, 0xE, 0x3);
1837 SpirvResult src0_xx = spv_swizzle(ctx, src0, 0x0, 0x3);
1838 SpirvResult src0_yy = spv_swizzle(ctx, src0, 0x5, 0x3);
1839 SpirvResult src1_xy = spv_swizzle(ctx, src1, 0x4, 0x3);
1840 uint32 tid_vec2 = src0_xx.tid;
1841 uint32 id_a = spv_bumpid(ctx);
1842 uint32 id_b = spv_bumpid(ctx);
1843 uint32 id_c = spv_bumpid(ctx);
1844 uint32 id_d = spv_bumpid(ctx);
1845 spv_emit(ctx, 5, SpvOpFMul, tid_vec2, id_a, matrix_xy.id, src0_xx.id);
1846 spv_emit(ctx, 5, SpvOpFMul, tid_vec2, id_b, matrix_zw.id, src0_yy.id);
1847 spv_emit(ctx, 5, SpvOpFAdd, tid_vec2, id_c, id_a, id_b);
1848 spv_emit(ctx, 5, SpvOpFAdd, tid_vec2, id_d, id_c, src1_xy.id);
1849
1850 // sample texture
1851 SpirvResult result;
1852 result.tid = tid_vec4;
1853 result.id = spv_bumpid(ctx);
1854 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id, sampler.id, id_d);
1855 if (luminanceCorrection)
1856 {
1857 uint32 id_l_offset = ctx->spirv.sampler_extras[sampler_idx].idtexbeml;
1858 if (!id_l_offset)
1859 {
1860 id_l_offset = spv_bumpid(ctx);
1861 ctx->spirv.sampler_extras[sampler_idx].idtexbeml = id_l_offset;
1862 } // if
1863
1864 // <dst> = <dst> * ((<src0>.z * <sampler>_texbeml.x) + <sampler>_texbeml.y)
1865 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
1866
1867 SpirvResult src0_z = spv_swizzle(ctx, src0, 0x2, 0x1);
1868 uint32 id_l_ptr = spv_access_uniform(ctx, STI_PTR_VEC4_U, REG_TYPE_CONST, id_l_offset);
1869
1870 SpirvResult l;
1871 l.tid = tid_vec4;
1872 l.id = spv_bumpid(ctx);
1873
1874 spv_emit(ctx, 4, SpvOpLoad, l.tid, l.id, id_l_ptr);
1875
1876 SpirvResult l_x = spv_swizzle(ctx, l, 0x0, 0x1);
1877 SpirvResult l_y = spv_swizzle(ctx, l, 0x1, 0x1);
1878 assert(tid_float == l_x.tid);
1879 assert(tid_float == l_y.tid);
1880 assert(tid_float == src0_z.tid);
1881
1882 uint32 id_e = spv_bumpid(ctx);
1883 uint32 id_f = spv_bumpid(ctx);
1884 uint32 id_ffff = spv_bumpid(ctx);
1885 uint32 id_new = spv_bumpid(ctx);
1886 spv_emit(ctx, 5, SpvOpFMul, tid_float, id_e, src0_z.id, l_x.id);
1887 spv_emit(ctx, 5, SpvOpFAdd, tid_float, id_f, id_e, l_y.id);
1888 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, tid_vec4, id_ffff,
1889 id_f, id_f, id_f, id_f
1890 );
1891 spv_emit(ctx, 5, SpvOpFMul, tid_vec4, id_new, result.id, id_ffff);
1892 result.id = id_new;
1893 } // if
1894
1895 pop_output(ctx);
1896
1897 spv_assign_destarg(ctx, result);
1898 }
1899
1900 void emit_SPIRV_start(Context *ctx, const char *profilestr)
1901 {
1902 if (!(shader_is_vertex(ctx) || shader_is_pixel(ctx)))
1903 {
1904 failf(ctx, "Shader type %u unsupported in this profile.",
1905 (uint) ctx->shader_type);
1906 return;
1907 } // if
1908
1909 memset(&(ctx->spirv), '\0', sizeof(ctx->spirv));
1910
1911 #if SUPPORT_PROFILE_GLSPIRV
1912 if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSPIRV) == 0)
1913 {
1914 ctx->profile_supports_glspirv = 1;
1915 ctx->spirv.mode = SPIRV_MODE_GL;
1916 } // if
1917 else
1918 #endif // SUPPORT_PROFILE_GLSPIRV
1919 {
1920 ctx->spirv.mode = SPIRV_MODE_VK;
1921 if (strcmp(profilestr, MOJOSHADER_PROFILE_SPIRV) != 0)
1922 failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
1923 } // else
1924
1925 ctx->spirv.idmain = spv_bumpid(ctx);
1926
1927 // calls spv_getvoid as well
1928 uint32 tid_void = spv_get_type(ctx, STI_VOID);
1929 uint32 tid_func = spv_get_type(ctx, STI_FUNC_VOID);
1930
1931 // slap the function declaration itself in mainline_top, so we can do type
1932 // declaration in mainline_intro (= before this in the output)
1933 push_output(ctx, &ctx->mainline_top);
1934 spv_emit(ctx, 5, SpvOpFunction, tid_void, ctx->spirv.idmain, SpvFunctionControlMaskNone, tid_func);
1935 spv_emit(ctx, 2, SpvOpLabel, spv_bumpid(ctx));
1936 pop_output(ctx);
1937
1938 // also emit the name for the function
1939 spv_output_name(ctx, ctx->spirv.idmain, ctx->mainfn);
1940
1941 set_output(ctx, &ctx->mainline);
1942 } // emit_SPIRV_start
1943
1944 void emit_SPIRV_end(Context *ctx)
1945 {
1946 if (ctx->previous_opcode != OPCODE_RET)
1947 spv_emit_func_end(ctx);
1948 } // emit_SPIRV_end
1949
1950 void emit_SPIRV_phase(Context *ctx)
1951 {
1952 // no-op
1953 } // emit_SPIRV_phase
1954
1955 void emit_SPIRV_global(Context *ctx, RegisterType regtype, int regnum)
1956 {
1957 RegisterList *r = reglist_find(&ctx->used_registers, regtype, regnum);
1958
1959 SpvStorageClass sc = SpvStorageClassPrivate;
1960 uint32 tid = 0;
1961 switch (regtype)
1962 {
1963 case REG_TYPE_LABEL:
1964 failf(ctx, "unimplemented regtype %d", regtype);
1965 return;
1966
1967 case REG_TYPE_LOOP:
1968 // Using SSA id to represent loop counters, instead of a variable.
1969 return;
1970
1971 case REG_TYPE_PREDICATE:
1972 tid = spv_get_type(ctx, STI_PTR_BVEC4_P);
1973 break;
1974
1975 case REG_TYPE_ADDRESS:
1976 if (shader_is_vertex(ctx))
1977 tid = spv_get_type(ctx, STI_PTR_IVEC4_P);
1978 else if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE
1979 {
1980 if (!shader_version_atleast(ctx, 1, 4))
1981 {
1982 // ps_1_1 texture/address registers work like temporaries. They are initialized
1983 // with tex coords and TEX instruction then reads tex coords from it and writes
1984 // sampling result back into it. Because Input storage class is read-only, we
1985 // create private variable that is initialized to value of input.
1986
1987 uint32 tid_pvec4_i = spv_get_type(ctx, STI_PTR_VEC4_I);
1988 uint32 tid_pvec4_p = spv_get_type(ctx, STI_PTR_VEC4_P);
1989 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
1990 uint32 id_input_var = spv_bumpid(ctx);
1991 uint32 id_private_var = r->spirv.iddecl;
1992 uint32 id_tmp = spv_bumpid(ctx);
1993
1994 // Create one Input and one Private variable. Input variable is linked to prev stage.
1995 push_output(ctx, &ctx->mainline_intro);
1996 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4_i, id_input_var, SpvStorageClassInput);
1997 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4_p, id_private_var, SpvStorageClassPrivate);
1998 pop_output(ctx);
1999 spv_link_ps_attributes(ctx, id_input_var, regtype, MOJOSHADER_USAGE_TEXCOORD, regnum);
2000
2001 // Initialize Private variable with Input variable.
2002 push_output(ctx, &ctx->mainline_top);
2003 spv_emit(ctx, 4, SpvOpLoad, tid_vec4, id_tmp, id_input_var);
2004 spv_emit(ctx, 3, SpvOpStore, id_private_var, id_tmp);
2005 pop_output(ctx);
2006
2007 // TEX instruction have already been emitted that work with Private variable.
2008
2009 // Overwrite Private variable with Input variable, so emit_SPIRV_finalize outputs
2010 // OpEntryPoint with correct references to Input and Output variables.
2011 ctx->spirv.id_implicit_input[regnum] = id_input_var;
2012 r->spirv.iddecl = id_input_var;
2013 spv_output_regname(ctx, id_input_var, regtype, regnum);
2014 return;
2015 } // if
2016 tid = spv_get_type(ctx, STI_PTR_VEC4_P);
2017 } // else if
2018 break;
2019
2020 case REG_TYPE_TEMP:
2021 if (regnum == 0 && shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
2022 {
2023 // Value of r0 is at the end of shader execution is color output.
2024 sc = SpvStorageClassOutput;
2025 tid = spv_get_type(ctx, STI_PTR_VEC4_O);
2026 }
2027 else
2028 tid = spv_get_type(ctx, STI_PTR_VEC4_P);
2029 break;
2030
2031 default:
2032 fail(ctx, "BUG: Unexpected regtype in emit_SPIRV_global");
2033 return;
2034 } // switch
2035
2036 // TODO: If the SSA id for this register is still 0 by this point, that
2037 // means no instructions actually loaded from/stored to this variable...
2038
2039 if (r->spirv.iddecl == 0)
2040 r->spirv.iddecl = spv_bumpid(ctx);
2041
2042 push_output(ctx, &ctx->mainline_intro);
2043 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, sc);
2044 pop_output(ctx);
2045
2046 spv_output_regname(ctx, r->spirv.iddecl, regtype, regnum);
2047 } // emit_SPIRV_global
2048
2049 void emit_SPIRV_array(Context *ctx, VariableList *var)
2050 {
2051 var->emit_position = ctx->uniform_float4_count;
2052 } // emit_SPIRV_array
2053
2054 void emit_SPIRV_const_array(Context *ctx,
2055 const struct ConstantsList *clist,
2056 int base, int size)
2057 {
2058 int i;
2059
2060 assert(ctx->spirv.constant_arrays.idvec4 != 0);
2061
2062 push_output(ctx, &ctx->mainline_intro);
2063
2064 // FIXME: This code potentially duplicates constants defined using DEF ops.
2065 // FIXME: Multiple constant arrays probably won't work. Are those even possible?
2066 // Maybe it would be better to do this in emit_SPIRV_finalize and use used_registers for it?
2067 uint32 *constituents = (uint32 *)Malloc(ctx, size * sizeof(uint32));
2068 uint32 tid_constituent = spv_get_type(ctx, STI_VEC4);
2069 for (i = 0; i < size; i++)
2070 {
2071 while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
2072 clist = clist->next;
2073 assert(clist->constant.index == (base + i));
2074
2075 uint32 id_x = spv_getscalarf(ctx, clist->constant.value.f[0]);
2076 uint32 id_y = spv_getscalarf(ctx, clist->constant.value.f[1]);
2077 uint32 id_z = spv_getscalarf(ctx, clist->constant.value.f[2]);
2078 uint32 id_w = spv_getscalarf(ctx, clist->constant.value.f[3]);
2079
2080 uint32 id = spv_bumpid(ctx);
2081 spv_emit(ctx, 3 + 4, SpvOpConstantComposite, tid_constituent, id, id_x, id_y, id_z, id_w);
2082 constituents[i] = id;
2083
2084 clist = clist->next;
2085 } // for
2086
2087 uint32 id_array_len = spv_getscalari(ctx, size);
2088
2089 uint32 tid_array = spv_bumpid(ctx);
2090 spv_emit(ctx, 4, SpvOpTypeArray, tid_array, tid_constituent, id_array_len);
2091
2092 uint32 id_array = spv_bumpid(ctx);
2093 spv_emit_part(ctx, 3+size, 3, SpvOpConstantComposite, tid_array, id_array);
2094 for (i = 0; i < size; i++)
2095 spv_emit_word(ctx, constituents[i]);
2096
2097 uint32 tid_parray = spv_bumpid(ctx);
2098 spv_emit(ctx, 4, SpvOpTypePointer, tid_parray, SpvStorageClassPrivate, tid_array);
2099
2100 uint32 id_array_var = ctx->spirv.constant_arrays.idvec4;
2101 spv_emit(ctx, 5, SpvOpVariable, tid_parray, id_array_var, SpvStorageClassPrivate, id_array);
2102
2103 Free(ctx, constituents);
2104 pop_output(ctx);
2105 } // emit_SPIRV_const_array
2106
2107 void emit_SPIRV_uniform(Context *ctx, RegisterType regtype, int regnum,
2108 const VariableList *var)
2109 {
2110 RegisterList *r = reglist_find(&ctx->uniforms, regtype, regnum);
2111
2112 // TODO: If the SSA id for this register is still 0 by this point, that means no instructions actually
2113 // loaded from/stored to this variable...
2114
2115 if (r->spirv.iddecl == 0)
2116 r->spirv.iddecl = spv_bumpid(ctx);
2117
2118 if (var == NULL)
2119 {
2120 uint32 tid = spv_get_type(ctx, STI_INT);
2121 int offset = 0;
2122 switch (regtype)
2123 {
2124 case REG_TYPE_CONST:
2125 offset = ctx->uniform_float4_count;
2126 break;
2127
2128 case REG_TYPE_CONSTINT:
2129 offset = ctx->uniform_int4_count;
2130 break;
2131
2132 case REG_TYPE_CONSTBOOL:
2133 offset = ctx->uniform_bool_count;
2134 break;
2135
2136 default:
2137 fail(ctx, "BUG: used a uniform we don't know how to define.");
2138 return;
2139 } // switch
2140
2141 push_output(ctx, &ctx->mainline_intro);
2142 spv_emit(ctx, 4, SpvOpConstant, tid, r->spirv.iddecl, offset);
2143 pop_output(ctx);
2144
2145 char varname[64];
2146 get_SPIRV_varname_in_buf(ctx, regtype, regnum, varname, sizeof(varname));
2147 spv_output_name(ctx, r->spirv.iddecl, varname);
2148 } // if
2149 else
2150 {
2151 if (var->constant)
2152 fail(ctx, "const array not implemented");
2153 else
2154 {
2155 // Instructions needed to reference this constant before its value was known, so unique
2156 // id had to be generated. Unfortunately, this prevents reusing already emitted
2157 // constants.
2158 assert(var->emit_position != -1);
2159 push_output(ctx, &ctx->mainline_intro);
2160 spv_emit(ctx, 4, SpvOpConstant, spv_get_type(ctx, STI_INT), r->spirv.iddecl, var->emit_position);
2161 pop_output(ctx);
2162
2163 char varname[64];
2164 get_SPIRV_varname_in_buf(ctx, regtype, regnum, varname, sizeof(varname));
2165 spv_output_name(ctx, r->spirv.iddecl, varname);
2166 } // else
2167 } // else
2168 } // emit_SPIRV_uniform
2169
2170 void emit_SPIRV_sampler(Context *ctx, int stage, TextureType ttype, int texbem)
2171 {
2172 uint32 type = spv_ptrimage_from_texturetype(ctx, ttype);
2173
2174 RegisterList *sampler_reg;
2175 // Pre ps_2_0 samplers were not dcl-ed, so we won't find them using spv_getreg().
2176 if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
2177 sampler_reg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
2178 else
2179 sampler_reg = spv_getreg(ctx, REG_TYPE_SAMPLER, stage);
2180
2181 uint32 result = sampler_reg->spirv.iddecl;
2182
2183 push_output(ctx, &ctx->mainline_intro);
2184 spv_emit(ctx, 4, SpvOpVariable, type, result, SpvStorageClassUniformConstant);
2185 if (texbem) // This sampler used a ps_1_1 TEXBEM opcode?
2186 {
2187 uint32 tid_int = spv_get_type(ctx, STI_INT);
2188 uint32 id_texbem = ctx->spirv.sampler_extras[stage].idtexbem;
2189 uint32 id_texbeml = ctx->spirv.sampler_extras[stage].idtexbeml;
2190 const int offset = ctx->uniform_float4_count;
2191 ctx->uniform_float4_count += 2;
2192 if (id_texbem)
2193 spv_emit(ctx, 4, SpvOpConstant, tid_int, id_texbem, offset);
2194 if (id_texbeml)
2195 spv_emit(ctx, 4, SpvOpConstant, tid_int, id_texbeml, offset + 1);
2196 } // if
2197 pop_output(ctx);
2198
2199 // hnn: specify uniform location for SPIR-V shaders (required per gl_arb_spirv spec)
2200 spv_output_sampler_binding(ctx, result, sampler_reg->regnum);
2201
2202 if (ctx->spirv.mode == SPIRV_MODE_GL)
2203 {
2204 assert(sampler_reg->regnum < STATICARRAYLEN(ctx->spirv.patch_table.samplers));
2205 uint32 location_offset = spv_output_location(ctx, result, ~0u);
2206 ctx->spirv.patch_table.samplers[sampler_reg->regnum].offset = location_offset;
2207 }
2208
2209 spv_output_regname(ctx, result, REG_TYPE_SAMPLER, stage);
2210 } // emit_SPIRV_sampler
2211
2212 void emit_SPIRV_attribute(Context *ctx, RegisterType regtype, int regnum,
2213 MOJOSHADER_usage usage, int index, int wmask,
2214 int flags)
2215 {
2216 uint32 tid;
2217 RegisterList *r = spv_getreg(ctx, regtype, regnum);
2218
2219 ctx->spirv.inoutcount += 1;
2220
2221 spv_output_regname(ctx, r->spirv.iddecl, regtype, regnum);
2222
2223 if (shader_is_vertex(ctx))
2224 {
2225 // pre-vs3 output registers.
2226 // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
2227 // output registers.
2228 if (!shader_version_atleast(ctx, 3, 0))
2229 {
2230 if (regtype == REG_TYPE_RASTOUT)
2231 {
2232 regtype = REG_TYPE_OUTPUT;
2233 index = regnum;
2234 switch ((const RastOutType) regnum)
2235 {
2236 case RASTOUT_TYPE_POSITION:
2237 usage = MOJOSHADER_USAGE_POSITION;
2238 break;
2239 case RASTOUT_TYPE_FOG:
2240 usage = MOJOSHADER_USAGE_FOG;
2241 break;
2242 case RASTOUT_TYPE_POINT_SIZE:
2243 usage = MOJOSHADER_USAGE_POINTSIZE;
2244 break;
2245 } // switch
2246 } // if
2247
2248 else if (regtype == REG_TYPE_ATTROUT)
2249 {
2250 regtype = REG_TYPE_OUTPUT;
2251 usage = MOJOSHADER_USAGE_COLOR;
2252 index = regnum;
2253 } // else if
2254
2255 else if (regtype == REG_TYPE_TEXCRDOUT)
2256 {
2257 regtype = REG_TYPE_OUTPUT;
2258 usage = MOJOSHADER_USAGE_TEXCOORD;
2259 index = regnum;
2260 } // else if
2261 } // if
2262 assert(r->usage == MOJOSHADER_USAGE_UNKNOWN);
2263 r->usage = usage;
2264
2265 switch (regtype)
2266 {
2267 case REG_TYPE_INPUT:
2268 {
2269 ctx->spirv.patch_table.tid_vec4_p = spv_get_type(ctx, STI_PTR_VEC4_I);
2270 ctx->spirv.patch_table.tid_ivec4_p = spv_get_type(ctx, STI_PTR_IVEC4_I);
2271 ctx->spirv.patch_table.tid_uvec4_p = spv_get_type(ctx, STI_PTR_UVEC4_I);
2272 ctx->spirv.patch_table.tid_vec4 = spv_get_type(ctx, STI_VEC4);
2273 ctx->spirv.patch_table.tid_ivec4 = spv_get_type(ctx, STI_IVEC4);
2274 ctx->spirv.patch_table.tid_uvec4 = spv_get_type(ctx, STI_UVEC4);
2275
2276 push_output(ctx, &ctx->mainline_intro);
2277 tid = spv_get_type(ctx, STI_PTR_VEC4_I);
2278 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, SpvStorageClassInput);
2279 pop_output(ctx);
2280
2281 ctx->spirv.patch_table.attrib_type_offsets[usage][index] = (buffer_size(ctx->mainline_intro) >> 2) - 3;
2282
2283 // hnn: generate location decorators for the input
2284 spv_output_location(ctx, r->spirv.iddecl, regnum);
2285 break;
2286 }
2287
2288 case REG_TYPE_OUTPUT:
2289 {
2290 push_output(ctx, &ctx->mainline_intro);
2291 SpirvTypeIdx sti = STI_PTR_VEC4_O;
2292 if (usage == MOJOSHADER_USAGE_POINTSIZE
2293 || usage == MOJOSHADER_USAGE_FOG)
2294 {
2295 sti = STI_PTR_FLOAT_O;
2296 } // if
2297
2298 tid = spv_get_type(ctx, sti);
2299 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, SpvStorageClassOutput);
2300 pop_output(ctx);
2301
2302 spv_link_vs_attributes(ctx, r->spirv.iddecl, usage, index);
2303 break;
2304 } // case
2305
2306 default:
2307 fail(ctx, "unknown vertex shader attribute register");
2308 } // switch
2309 } // if
2310
2311 else if (shader_is_pixel(ctx))
2312 {
2313 // samplers DCLs get handled in emit_SPIRV_sampler().
2314
2315 if (flags & MOD_CENTROID) // !!! FIXME
2316 {
2317 failf(ctx, "centroid unsupported in %s profile", ctx->profile->name);
2318 return;
2319 } // if
2320
2321 switch (regtype)
2322 {
2323 case REG_TYPE_COLOROUT:
2324 spv_link_ps_attributes(ctx, r->spirv.iddecl, regtype, usage, regnum);
2325 push_output(ctx, &ctx->mainline_intro);
2326 tid = spv_get_type(ctx, STI_PTR_VEC4_O);
2327 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, SpvStorageClassOutput);
2328 pop_output(ctx);
2329 break;
2330 case REG_TYPE_DEPTHOUT:
2331 // maps to BuiltIn FragDepth
2332 spv_link_ps_attributes(ctx, r->spirv.iddecl, regtype, usage, index);
2333 push_output(ctx, &ctx->mainline_intro);
2334 tid = spv_get_type(ctx, STI_PTR_FLOAT_O);
2335 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, SpvStorageClassOutput);
2336 pop_output(ctx);
2337 break;
2338 case REG_TYPE_MISCTYPE:
2339 assert((MiscTypeType)regnum == MISCTYPE_TYPE_FACE || (MiscTypeType)regnum == MISCTYPE_TYPE_POSITION);
2340 // SpvBuiltInFrontFacing is a input bool, and for the DX bytecode
2341 // we need to map it to a float that's either -1.0 or 1.0.
2342 // SpvBuiltInFragCoord needs to be modified using vposFlip uniform
2343 // to match vPos.
2344 // Both of these take place in spv_link_ps_attributes() so don't
2345 // create an input variable for it here.
2346 spv_link_ps_attributes(ctx, r->spirv.iddecl, regtype, usage, regnum);
2347 break;
2348
2349 case REG_TYPE_TEXTURE:
2350 case REG_TYPE_INPUT:
2351 // ps_1_1 is dealt with in emit_SPIRV_global().
2352 if (usage != MOJOSHADER_USAGE_TEXCOORD || shader_version_atleast(ctx, 1, 4))
2353 {
2354 if (usage == MOJOSHADER_USAGE_TEXCOORD && index == 0)
2355 {
2356 // This can be either BuiltInPointCoord (vec2) or normal TEXCOORD0 input (vec4).
2357 // To determine correct type, we need to wait until link-time when we can see
2358 // vertex shader outputs and then patch in correct types. To avoid having to
2359 // fix all loads from the input variable, we never access it directly, but
2360 // instead go through private variable that is always vec4.
2361 // Here we generate input and private variables and helper code that gets
2362 // patched at link-time. See SpirvPatchTable for details on patching.
2363 SpirvPatchTable* table = &ctx->spirv.patch_table;
2364
2365 uint32 tid_pvec2i = spv_get_type(ctx, STI_PTR_VEC2_I);
2366 uint32 tid_pvec4i = spv_get_type(ctx, STI_PTR_VEC4_I);
2367 uint32 tid_pvec4p = spv_get_type(ctx, STI_PTR_VEC4_P);
2368 uint32 tid_vec2 = spv_get_type(ctx, STI_VEC2);
2369 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
2370
2371 table->tid_pvec2i = tid_pvec2i;
2372 table->tid_vec2 = tid_vec2;
2373 table->tid_pvec4i = tid_pvec4i;
2374 table->tid_vec4 = tid_vec4;
2375
2376 push_output(ctx, &ctx->mainline_intro);
2377 ctx->spirv.id_var_texcoord0_private = r->spirv.iddecl;
2378 ctx->spirv.id_var_texcoord0_input = spv_bumpid(ctx);
2379 table->pointcoord_var_offset = buffer_size(ctx->mainline_intro) >> 2;
2380 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4i, ctx->spirv.id_var_texcoord0_input, SpvStorageClassInput);
2381 spv_emit(ctx, 4, SpvOpVariable, tid_pvec4p, ctx->spirv.id_var_texcoord0_private, SpvStorageClassPrivate);
2382 pop_output(ctx);
2383
2384 spv_link_ps_attributes(ctx, ctx->spirv.id_var_texcoord0_input, regtype, usage, index);
2385 spv_output_name(ctx, ctx->spirv.id_var_texcoord0_input, "ps_PointCoordOrTexCoord0");
2386
2387 push_output(ctx, &ctx->mainline_top);
2388 uint32 id_loaded = spv_bumpid(ctx);
2389 uint32 id_shuffled = spv_bumpid(ctx);
2390 table->pointcoord_load_offset = buffer_size(ctx->mainline_top) >> 2;
2391 spv_emit(ctx, 4, SpvOpLoad, tid_vec4, id_loaded, ctx->spirv.id_var_texcoord0_input);
2392 spv_emit(ctx, 9, SpvOpVectorShuffle, tid_vec4, id_shuffled, id_loaded, id_loaded, 0, 1, 2, 3);
2393 spv_emit(ctx, 3, SpvOpStore, ctx->spirv.id_var_texcoord0_private, id_shuffled);
2394 pop_output(ctx);
2395 } // if
2396 else
2397 {
2398 spv_link_ps_attributes(ctx, r->spirv.iddecl, regtype, usage, index);
2399 push_output(ctx, &ctx->mainline_intro);
2400 tid = spv_get_type(ctx, STI_PTR_VEC4_I);
2401 spv_emit(ctx, 4, SpvOpVariable, tid, r->spirv.iddecl, SpvStorageClassInput);
2402 pop_output(ctx);
2403 } // else
2404 } // if
2405 break;
2406 default:
2407 fail(ctx, "unknown pixel shader attribute register");
2408 } // switch
2409 } // else if
2410
2411 else
2412 fail(ctx, "Unknown shader type"); // state machine should catch this.
2413 } // emit_SPIRV_attribute
2414
2415 static void spv_emit_uniform_constant_array(Context *ctx,
2416 const RegisterType regtype,
2417 const int size, uint32 id_var,
2418 uint32 id_type_base,
2419 uint32* dst_location_offset)
2420 {
2421 assert(size > 0);
2422 assert(id_var != 0);
2423 assert(ctx->spirv.mode == SPIRV_MODE_GL);
2424
2425 uint32 id_size = spv_getscalari(ctx, size);
2426 uint32 id_type = spv_bumpid(ctx);
2427 uint32 id_type_ptr = spv_bumpid(ctx);
2428 push_output(ctx, &ctx->mainline_intro);
2429 spv_emit(ctx, 4, SpvOpTypeArray, id_type, id_type_base, id_size);
2430 spv_emit(ctx, 4, SpvOpTypePointer, id_type_ptr, SpvStorageClassUniformConstant, id_type);
2431 spv_emit(ctx, 4, SpvOpVariable, id_type_ptr, id_var, SpvStorageClassUniformConstant);
2432 pop_output(ctx);
2433
2434 char buf[64];
2435 spv_get_uniform_array_varname(ctx, regtype, buf, sizeof(buf));
2436 spv_output_name(ctx, id_var, buf);
2437
2438 *dst_location_offset = spv_output_location(ctx, id_var, ~0u);
2439 } // spv_emit_uniform_constant_array
2440
2441 void emit_SPIRV_finalize(Context *ctx)
2442 {
2443 size_t i, j, k, max;
2444
2445 /* The generator's magic number, this could be registered with Khronos
2446 * if we wanted to. 0 is fine though, so use that for now. */
2447 uint32 genmagic = 0x00000000;
2448
2449 /* Vertex shader main() function may need to do some position adjustments. However,
2450 position may be written in subroutines, so we can't write position adjust code
2451 at the end of main(), because output register might not be in ctx->used_registers
2452 yet. Instead, we do adjust in a subroutine generated here and called at the
2453 end of main(). */
2454 spv_emit_vs_main_end(ctx);
2455 spv_emit_func_lit(ctx);
2456
2457 uint8 emit_vec4 = ctx->uniform_float4_count > 0 && ctx->spirv.uniform_arrays.idvec4;
2458 uint8 emit_ivec4 = ctx->uniform_int4_count > 0 && ctx->spirv.uniform_arrays.idivec4;
2459 uint8 emit_bool = ctx->uniform_bool_count > 0 && ctx->spirv.uniform_arrays.idbool;
2460 uint8 emit_any = emit_vec4 | emit_ivec4 | emit_bool;
2461 if (ctx->spirv.mode == SPIRV_MODE_GL)
2462 {
2463 if (emit_vec4)
2464 spv_emit_uniform_constant_array(ctx, REG_TYPE_CONST,
2465 ctx->uniform_float4_count,
2466 ctx->spirv.uniform_arrays.idvec4,
2467 spv_get_type(ctx, STI_VEC4),
2468 &ctx->spirv.patch_table.array_vec4.offset
2469 );
2470
2471 if (emit_ivec4)
2472 spv_emit_uniform_constant_array(ctx, REG_TYPE_CONSTINT,
2473 ctx->uniform_int4_count,
2474 ctx->spirv.uniform_arrays.idivec4,
2475 spv_get_type(ctx, STI_IVEC4),
2476 &ctx->spirv.patch_table.array_ivec4.offset
2477 );
2478
2479 if (emit_bool)
2480 spv_emit_uniform_constant_array(ctx, REG_TYPE_CONSTBOOL,
2481 ctx->uniform_bool_count,
2482 ctx->spirv.uniform_arrays.idbool,
2483 spv_get_type(ctx, STI_INT),
2484 &ctx->spirv.patch_table.array_bool.offset
2485 );
2486 } // if
2487 else if (emit_any)
2488 {
2489 assert(ctx->spirv.mode == SPIRV_MODE_VK);
2490 uint32 member_tid[3];
2491 uint32 member_offset[3];
2492 uint32 member_count = 0;
2493 uint32 struct_size = 0;
2494
2495 uint32 tid_arr_idx = spv_get_type(ctx, STI_INT);
2496
2497 push_output(ctx, &ctx->mainline_intro);
2498
2499 if (emit_vec4)
2500 {
2501 int size = ctx->uniform_float4_count;
2502 uint32 id_size = spv_getscalari(ctx, size);
2503 uint32 tid_type_base = spv_get_type(ctx, STI_VEC4);
2504 uint32 tid_array = spv_bumpid(ctx);
2505 spv_emit(ctx, 4, SpvOpTypeArray, tid_array, tid_type_base, id_size);
2506 uint32 i = member_count++;
2507 spv_emit(ctx, 4, SpvOpConstant, tid_arr_idx, ctx->spirv.uniform_arrays.idvec4, i);
2508 member_tid[i] = tid_array;
2509 member_offset[i] = struct_size;
2510 struct_size += size * 16;
2511 } // if
2512
2513 if (emit_ivec4)
2514 {
2515 int size = ctx->uniform_int4_count;
2516 uint32 id_size = spv_getscalari(ctx, size);
2517 uint32 tid_type_base = spv_get_type(ctx, STI_IVEC4);
2518 uint32 tid_array = spv_bumpid(ctx);
2519 spv_emit(ctx, 4, SpvOpTypeArray, tid_array, tid_type_base, id_size);
2520 uint32 i = member_count++;
2521 spv_emit(ctx, 4, SpvOpConstant, tid_arr_idx, ctx->spirv.uniform_arrays.idivec4, i);
2522 member_tid[i] = tid_array;
2523 member_offset[i] = struct_size;
2524 struct_size += size * 16;
2525 } // if
2526
2527 if (emit_bool)
2528 {
2529 int size = ctx->uniform_bool_count;
2530 uint32 id_size = spv_getscalari(ctx, size);
2531 uint32 tid_type_base = spv_get_type(ctx, STI_INT);
2532 uint32 tid_array = spv_bumpid(ctx);
2533 spv_emit(ctx, 4, SpvOpTypeArray, tid_array, tid_type_base, id_size);
2534 uint32 i = member_count++;
2535 spv_emit(ctx, 4, SpvOpConstant, tid_arr_idx, ctx->spirv.uniform_arrays.idbool, i);
2536 member_tid[i] = tid_array;
2537 member_offset[i] = struct_size;
2538 struct_size += size * 16;
2539 } // if
2540
2541 uint32 tid_struct = spv_bumpid(ctx);
2542 uint32 tid_pstruct = spv_bumpid(ctx);
2543 uint32 id_pstruct = ctx->spirv.id_uniform_block;
2544 spv_emit_part(ctx, 2 + member_count, 2, SpvOpTypeStruct, tid_struct);
2545 for (i = 0; i < member_count; i++)
2546 spv_emit_word(ctx, member_tid[i]);
2547 spv_emit(ctx, 4, SpvOpTypePointer, tid_pstruct, SpvStorageClassUniform, tid_struct);
2548 spv_emit(ctx, 4, SpvOpVariable, tid_pstruct, id_pstruct, SpvStorageClassUniform);
2549
2550 pop_output(ctx);
2551
2552 char buf[64];
2553 snprintf(buf, sizeof(buf), "%s_uniforms", ctx->shader_type_str);
2554 spv_output_name(ctx, id_pstruct, buf);
2555
2556 uint32 set = shader_is_vertex(ctx) ? MOJOSHADER_SPIRV_VS_UNIFORM_SET
2557 : MOJOSHADER_SPIRV_PS_UNIFORM_SET;
2558 push_output(ctx, &ctx->helpers);
2559 spv_emit(ctx, 3+0, SpvOpDecorate, tid_struct, SpvDecorationBlock);
2560 spv_emit(ctx, 3+1, SpvOpDecorate, id_pstruct, SpvDecorationDescriptorSet, set);
2561 spv_emit(ctx, 3+1, SpvOpDecorate, id_pstruct, SpvDecorationBinding, 0);
2562
2563 for (uint32 i = 0; i < member_count; i++)
2564 {
2565 spv_emit(ctx, 3+1, SpvOpDecorate, member_tid[i], SpvDecorationArrayStride, 16);
2566 spv_emit(ctx, 4+1, SpvOpMemberDecorate, tid_struct, i, SpvDecorationOffset, member_offset[i]);
2567 } // for
2568
2569 pop_output(ctx);
2570 } // else if
2571
2572 push_output(ctx, &ctx->preflight);
2573
2574 spv_emit_word(ctx, SpvMagicNumber);
2575 spv_emit_word(ctx, SpvVersion);
2576 spv_emit_word(ctx, genmagic);
2577 // "Bound: where all <id>s in this module are guaranteed to satisfy 0 < id < Bound"
2578 // `idmax` holds the last id that was given out, so we need to emit `idmax + 1`
2579 spv_emit_word(ctx, ctx->spirv.idmax + 1);
2580 spv_emit_word(ctx, 0);
2581
2582 spv_emit(ctx, 2, SpvOpCapability, SpvCapabilityShader);
2583
2584 // only non-zero when actually needed
2585 if (ctx->spirv.idext)
2586 {
2587 const char *extstr = "GLSL.std.450";
2588 spv_emit_part(ctx, 2 + spv_strlen(extstr), 2, SpvOpExtInstImport, ctx->spirv.idext);
2589 spv_emit_str(ctx, extstr);
2590 } // if
2591
2592 spv_emit(ctx, 3, SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelSimple);
2593
2594 assert(shader_is_vertex(ctx) || shader_is_pixel(ctx));
2595 SpvExecutionModel model = SpvExecutionModelVertex;
2596 if (shader_is_pixel(ctx))
2597 model = SpvExecutionModelFragment;
2598
2599 /* 3 is for opcode + exec. model + idmain */
2600 uint32 inoutcount = ctx->spirv.inoutcount;
2601
2602 uint32 implicit_input_count = sizeof(ctx->spirv.id_implicit_input) / sizeof(uint32);
2603 if (shader_is_pixel(ctx))
2604 {
2605 if (!shader_version_atleast(ctx, 1, 4))
2606 {
2607 for (uint32 i = 0; i < implicit_input_count; i++)
2608 {
2609 if (ctx->spirv.id_implicit_input[i])
2610 inoutcount += 1;
2611 } // for
2612 } // if
2613
2614 if (!shader_version_atleast(ctx, 2, 0))
2615 inoutcount += 1;
2616 } // if
2617
2618 spv_emit_part(ctx, 3 + spv_strlen(ctx->mainfn) + inoutcount, 3, SpvOpEntryPoint,
2619 model, ctx->spirv.idmain
2620 );
2621 spv_emit_str(ctx, ctx->mainfn);
2622
2623 RegisterList *p = &ctx->attributes, *r = NULL;
2624 // !!! FIXME: The first element of the list is always empty and I don't know why!
2625 p = p->next;
2626 while (p)
2627 {
2628 r = spv_getreg(ctx, p->regtype, p->regnum);
2629 if (r)
2630 {
2631 if (r->spirv.iddecl == ctx->spirv.id_var_vpos)
2632 spv_emit_word(ctx, ctx->spirv.id_var_fragcoord);
2633 else if (r->spirv.iddecl == ctx->spirv.id_var_vface)
2634 spv_emit_word(ctx, ctx->spirv.id_var_frontfacing);
2635 else if (r->spirv.iddecl == ctx->spirv.id_var_texcoord0_private)
2636 spv_emit_word(ctx, ctx->spirv.id_var_texcoord0_input);
2637 else
2638 spv_emit_word(ctx, r->spirv.iddecl);
2639 } // if
2640 else
2641 {
2642 char varname[64];
2643 get_SPIRV_varname_in_buf(ctx, p->regtype, p->regnum, varname, sizeof (varname));
2644 failf(
2645 ctx,
2646 "missing attribute register %s (rt=%u, rn=%u, u=%u)",
2647 varname, p->regtype, p->regnum, p->usage
2648 );
2649 } // else
2650 p = p->next;
2651 } // while
2652
2653 // only applies to pixel shaders
2654 if (shader_is_pixel(ctx))
2655 {
2656 if (!shader_version_atleast(ctx, 1, 4))
2657 {
2658 for (uint32 i = 0; i < implicit_input_count; i++)
2659 {
2660 uint32 id = ctx->spirv.id_implicit_input[i];
2661 if (id)
2662 spv_emit_word(ctx, id);
2663 } // for
2664 } // if
2665
2666 if (!shader_version_atleast(ctx, 2, 0))
2667 {
2668 // r0 is used as color output.
2669 r = spv_getreg(ctx, REG_TYPE_TEMP, 0);
2670 spv_emit_word(ctx, r->spirv.iddecl);
2671 } // if
2672
2673 // vk semantics = default origin is upper left
2674 // gl semantics = default origin is lower left
2675 spv_emit(ctx, 3, SpvOpExecutionMode, ctx->spirv.idmain, SpvExecutionModeOriginUpperLeft);
2676
2677 // This must be explicitly marked when FragDepth is in use!
2678 if (ctx->spirv.hasdepth)
2679 spv_emit(ctx, 3, SpvOpExecutionMode, ctx->spirv.idmain, SpvExecutionModeDepthReplacing);
2680 } // if
2681
2682 pop_output(ctx);
2683
2684 // Generate final patch table.
2685
2686 uint32 base_offset = 0;
2687 if (ctx->preflight) base_offset += buffer_size(ctx->preflight);
2688 if (ctx->globals) base_offset += buffer_size(ctx->globals);
2689 if (ctx->inputs) base_offset += buffer_size(ctx->inputs);
2690 if (ctx->outputs) base_offset += buffer_size(ctx->outputs);
2691 base_offset >>= 2;
2692
2693 int32 location_count = 0;
2694 SpirvPatchTable* table = &ctx->spirv.patch_table;
2695 if (table->vpflip.offset)
2696 {
2697 table->vpflip.offset += base_offset;
2698 table->vpflip.location = location_count;
2699 location_count += 1;
2700 } // if
2701 else
2702 table->vpflip.location = -1;
2703
2704 if (table->array_vec4.offset)
2705 {
2706 table->array_vec4.offset += base_offset;
2707 table->array_vec4.location = location_count;
2708 location_count += ctx->uniform_float4_count;
2709 } // if
2710 else
2711 table->array_vec4.location = -1;
2712
2713 if (table->array_ivec4.offset)
2714 {
2715 table->array_ivec4.offset += base_offset;
2716 table->array_ivec4.location = location_count;
2717 location_count += ctx->uniform_int4_count;
2718 } // if
2719 else
2720 table->array_ivec4.location = -1;
2721
2722 if (table->array_bool.offset)
2723 {
2724 table->array_bool.offset += base_offset;
2725 table->array_bool.location = location_count;
2726 location_count += ctx->uniform_bool_count;
2727 } // if
2728 else
2729 table->array_bool.location = -1;
2730
2731 for (i = 0, max = STATICARRAYLEN(table->samplers); i < max; i++)
2732 {
2733 SpirvPatchEntry* entry = &table->samplers[i];
2734 if (entry->offset)
2735 {
2736 entry->offset += base_offset;
2737 entry->location = location_count;
2738 location_count++;
2739 } // if
2740 else
2741 entry->location = -1;
2742 } // for
2743
2744 table->location_count = location_count;
2745
2746 for (i = 0; i < MOJOSHADER_USAGE_TOTAL; i++)
2747 for (j = 0; j < 16; j++)
2748 if (table->attrib_offsets[i][j])
2749 table->attrib_offsets[i][j] += base_offset;
2750 for (i = 0; i < 16; i++)
2751 if (table->output_offsets[i])
2752 table->output_offsets[i] += base_offset;
2753
2754 base_offset <<= 2;
2755 if (ctx->helpers) base_offset += buffer_size(ctx->helpers);
2756 if (ctx->subroutines) base_offset += buffer_size(ctx->subroutines);
2757 base_offset >>= 2;
2758
2759 if (table->pointcoord_var_offset)
2760 table->pointcoord_var_offset += base_offset;
2761
2762 for (i = 0; i < MOJOSHADER_USAGE_TOTAL; i++)
2763 for (j = 0; j < 16; j++)
2764 if (table->attrib_type_offsets[i][j])
2765 table->attrib_type_offsets[i][j] += base_offset;
2766
2767 base_offset <<= 2;
2768 if (ctx->mainline_intro) base_offset += buffer_size(ctx->mainline_intro);
2769 if (ctx->mainline_arguments) base_offset += buffer_size(ctx->mainline_arguments);
2770 base_offset >>= 2;
2771
2772 if (table->pointcoord_load_offset)
2773 table->pointcoord_load_offset += base_offset;
2774
2775 base_offset <<= 2;
2776 if (ctx->mainline_top) base_offset += buffer_size(ctx->mainline_top);
2777 base_offset >>= 2;
2778
2779 for (i = 0; i < MOJOSHADER_USAGE_TOTAL; i++)
2780 for (j = 0; j < 16; j++)
2781 if (table->attrib_type_offsets[i][j])
2782 for (k = 0; k < table->attrib_type_load_offsets[i][j].num_loads; k++)
2783 {
2784 table->attrib_type_load_offsets[i][j].load_types[k] += base_offset;
2785 table->attrib_type_load_offsets[i][j].load_opcodes[k] += base_offset;
2786 } // for
2787
2788 push_output(ctx, &ctx->postflight);
2789 buffer_append(ctx->output, &ctx->spirv.patch_table, sizeof(ctx->spirv.patch_table));
2790 pop_output(ctx);
2791
2792 spv_componentlist_free(ctx, ctx->spirv.cl.f.next);
2793 spv_componentlist_free(ctx, ctx->spirv.cl.i.next);
2794 spv_componentlist_free(ctx, ctx->spirv.cl.u.next);
2795 } // emit_SPIRV_finalize
2796
2797 void emit_SPIRV_NOP(Context *ctx)
2798 {
2799 // no-op is a no-op. :)
2800 // TODO: (hnn) SPIR-V has OpNop :O
2801 } // emit_SPIRV_NOP
2802
2803 void emit_SPIRV_DEF(Context *ctx)
2804 {
2805 RegisterList *rl;
2806 uint32 val0, val1, val2, val3, idv4;
2807 const float *raw = (const float *) ctx->dwords;
2808
2809 rl = spv_getreg(ctx, ctx->dest_arg.regtype, ctx->dest_arg.regnum);
2810 rl->spirv.iddecl = spv_bumpid(ctx);
2811 rl->spirv.is_ssa = 1;
2812
2813 val0 = spv_getscalarf(ctx, raw[0]);
2814 val1 = spv_getscalarf(ctx, raw[1]);
2815 val2 = spv_getscalarf(ctx, raw[2]);
2816 val3 = spv_getscalarf(ctx, raw[3]);
2817
2818 idv4 = spv_get_type(ctx, STI_VEC4);
2819
2820 push_output(ctx, &ctx->mainline_intro);
2821 spv_emit(ctx, 3 + 4, SpvOpConstantComposite, idv4, rl->spirv.iddecl, val0, val1, val2, val3);
2822 pop_output(ctx);
2823 } // emit_SPIRV_DEF
2824
2825 void emit_SPIRV_DEFI(Context *ctx)
2826 {
2827 RegisterList *rl;
2828 uint32 val0, val1, val2, val3, idiv4;
2829 const int *raw = (const int *) ctx->dwords;
2830
2831 rl = spv_getreg(ctx, ctx->dest_arg.regtype, ctx->dest_arg.regnum);
2832 rl->spirv.iddecl = spv_bumpid(ctx);
2833 rl->spirv.is_ssa = 1;
2834
2835 val0 = spv_getscalari(ctx, raw[0]);
2836 val1 = spv_getscalari(ctx, raw[1]);
2837 val2 = spv_getscalari(ctx, raw[2]);
2838 val3 = spv_getscalari(ctx, raw[3]);
2839
2840 idiv4 = spv_get_type(ctx, STI_IVEC4);
2841
2842 push_output(ctx, &ctx->mainline_intro);
2843 spv_emit(ctx, 3 + 4, SpvOpConstantComposite, idiv4, rl->spirv.iddecl, val0, val1, val2, val3);
2844 pop_output(ctx);
2845 } // emit_SPIRV_DEFI
2846
2847 void emit_SPIRV_DEFB(Context *ctx)
2848 {
2849 RegisterList *rl = spv_getreg(ctx, ctx->dest_arg.regtype, ctx->dest_arg.regnum);
2850 rl->spirv.iddecl = ctx->dwords[0] ? spv_gettrue(ctx) : spv_getfalse(ctx);
2851 rl->spirv.is_ssa = 1;
2852 } // emit_SPIRV_DEFB
2853
2854 void emit_SPIRV_DCL(Context *ctx)
2855 {
2856 // state_DCL handles checking if the registers are valid for this
2857 // instruction, and collecting samplers and attribs
2858 RegisterList *reg = spv_getreg(ctx, ctx->dest_arg.regtype, ctx->dest_arg.regnum);
2859
2860 // This id will be assigned to in emit_SPIRV_attribute, but
2861 // emit_SPIRV_attribute is called after instructions are emitted,
2862 // so we generate the id here so it can be used in instructions
2863 reg->spirv.iddecl = spv_bumpid(ctx);
2864 } // emit_SPIRV_DCL
2865
2866 static void emit_SPIRV_dotproduct(Context *ctx, SpirvResult src0, SpirvResult src1)
2867 {
2868 SpirvResult result;
2869
2870 assert(src0.tid == src1.tid);
2871
2872 result.tid = spv_get_type(ctx, STI_FLOAT);
2873 result.id = spv_bumpid(ctx);
2874
2875 push_output(ctx, &ctx->mainline);
2876 spv_emit(ctx, 5, SpvOpDot, result.tid, result.id, src0.id, src1.id);
2877
2878 // Broadcast scalar result across all channels of a vec4
2879 result.tid = spv_get_type(ctx, STI_VEC4);
2880 result.id = spv_vectorbroadcast(ctx, result.tid, result.id);
2881 pop_output(ctx);
2882
2883 spv_assign_destarg(ctx, result);
2884 } // emit_SPIRV_dotproduct
2885
2886 void emit_SPIRV_DP4(Context *ctx)
2887 {
2888 SpirvResult src0 = spv_load_srcarg_full(ctx, 0);
2889 SpirvResult src1 = spv_load_srcarg_full(ctx, 1);
2890
2891 emit_SPIRV_dotproduct(ctx, src0, src1);
2892 } // emit_SPIRV_DP4
2893
2894 void emit_SPIRV_DP3(Context *ctx)
2895 {
2896 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x7);
2897 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x7);
2898
2899 emit_SPIRV_dotproduct(ctx, src0, src1);
2900 } // emit_SPIRV_DP3
2901
2902 static void spv_emit_begin_ds(Context *ctx, SpirvResult* dst, SpirvResult* src)
2903 {
2904 *src = spv_load_srcarg_full(ctx, 0);
2905 dst->tid = spv_get_type(ctx, STI_VEC4);
2906 dst->id = spv_bumpid(ctx);
2907 push_output(ctx, &ctx->mainline);
2908 } // spv_emit_begin_ds
2909
2910 static void spv_emit_begin_dss(Context *ctx, SpirvResult* dst, SpirvResult* src0, SpirvResult* src1)
2911 {
2912 *src0 = spv_load_srcarg_full(ctx, 0);
2913 *src1 = spv_load_srcarg_full(ctx, 1);
2914 dst->tid = spv_get_type(ctx, STI_VEC4);
2915 dst->id = spv_bumpid(ctx);
2916 push_output(ctx, &ctx->mainline);
2917 } // spv_emit_begin_dss
2918
2919 static void spv_emit_begin_dsss(Context *ctx, SpirvResult* dst,
2920 SpirvResult* src0, SpirvResult* src1, SpirvResult* src2)
2921 {
2922 *src0 = spv_load_srcarg_full(ctx, 0);
2923 *src1 = spv_load_srcarg_full(ctx, 1);
2924 *src2 = spv_load_srcarg_full(ctx, 2);
2925 dst->tid = spv_get_type(ctx, STI_VEC4);
2926 dst->id = spv_bumpid(ctx);
2927 push_output(ctx, &ctx->mainline);
2928 } // spv_emit_begin_dsss
2929
2930 static void spv_emit_end(Context *ctx, SpirvResult dst)
2931 {
2932 pop_output(ctx);
2933 spv_assign_destarg(ctx, dst);
2934 } // spv_emit_end
2935
2936 static SpirvTexm3x3SetupResult spv_texm3x3_setup(Context *ctx)
2937 {
2938 SpirvTexm3x3SetupResult result;
2939
2940 DestArgInfo *pDstInfo = &ctx->dest_arg;
2941
2942 RegisterList *pSrc0 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0);
2943 RegisterList *pSrc1 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0);
2944 RegisterList *pSrc2 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1);
2945 RegisterList *pSrc3 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1);
2946 RegisterList *pSrc4 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum);
2947 RegisterList *pDst = spv_getreg(ctx, pDstInfo->regtype, pDstInfo->regnum);
2948
2949 SpirvResult src0 = spv_loadreg(ctx, pSrc0);
2950 SpirvResult src1 = spv_loadreg(ctx, pSrc1);
2951 SpirvResult src2 = spv_loadreg(ctx, pSrc2);
2952 SpirvResult src3 = spv_loadreg(ctx, pSrc3);
2953 SpirvResult src4 = spv_loadreg(ctx, pSrc4);
2954 SpirvResult dst = spv_loadreg(ctx, pDst);
2955
2956 result.id_dst_pad0 = src0.id;
2957 result.id_dst_pad1 = src2.id;
2958 result.id_dst = dst.id;
2959
2960 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
2961 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
2962
2963 uint32 id_src0_xyz = spv_bumpid(ctx);
2964 uint32 id_src1_xyz = spv_bumpid(ctx);
2965 uint32 id_src2_xyz = spv_bumpid(ctx);
2966 uint32 id_src3_xyz = spv_bumpid(ctx);
2967 uint32 id_src4_xyz = spv_bumpid(ctx);
2968 uint32 id_dst_xyz = spv_bumpid(ctx);
2969 uint32 id_res_x = spv_bumpid(ctx);
2970 uint32 id_res_y = spv_bumpid(ctx);
2971 uint32 id_res_z = spv_bumpid(ctx);
2972
2973 push_output(ctx, &ctx->mainline);
2974
2975 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src0_xyz, src0.id, src0.id, 0, 1, 2);
2976 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src1_xyz, src1.id, src1.id, 0, 1, 2);
2977 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src2_xyz, src2.id, src2.id, 0, 1, 2);
2978 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src3_xyz, src3.id, src3.id, 0, 1, 2);
2979 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src4_xyz, src4.id, src4.id, 0, 1, 2);
2980 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_dst_xyz, dst.id, dst.id, 0, 1, 2);
2981
2982 spv_emit(ctx, 5, SpvOpDot, tid_float, id_res_x, id_src0_xyz, id_src1_xyz);
2983 spv_emit(ctx, 5, SpvOpDot, tid_float, id_res_y, id_src2_xyz, id_src3_xyz);
2984 spv_emit(ctx, 5, SpvOpDot, tid_float, id_res_z, id_dst_xyz, id_src4_xyz);
2985
2986 pop_output(ctx);
2987
2988 result.id_res_x = id_res_x;
2989 result.id_res_y = id_res_y;
2990 result.id_res_z = id_res_z;
2991
2992 return result;
2993 } // spv_texm3x3_setup
2994
2995 static uint32 spv_reflect(Context *ctx, uint32 id_normal, uint32 id_eyeray)
2996 {
2997 // reflect(E : vec3 = eyeray, N : vec3 = normal) -> vec3
2998 // 2 * [(N*E) / (N*N)] * N - E
2999
3000 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
3001 uint32 id_2 = spv_getscalarf(ctx, 2.0f);
3002 uint32 id_2_v3 = spv_bumpid(ctx);
3003 uint32 id_refl_0 = spv_bumpid(ctx);
3004 uint32 id_refl_1 = spv_bumpid(ctx);
3005 uint32 id_refl_2 = spv_bumpid(ctx);
3006 uint32 id_refl_3 = spv_bumpid(ctx);
3007 uint32 id_refl_4 = spv_bumpid(ctx);
3008 uint32 id_reflected = spv_bumpid(ctx);
3009
3010 spv_emit(ctx, 3 + 3, SpvOpCompositeConstruct, tid_vec3, id_2_v3, id_2, id_2, id_2);
3011 spv_emit(ctx, 5, SpvOpFMul, tid_vec3, id_refl_0, id_normal, id_eyeray);
3012 spv_emit(ctx, 5, SpvOpFMul, tid_vec3, id_refl_1, id_normal, id_normal);
3013 spv_emit(ctx, 5, SpvOpFDiv, tid_vec3, id_refl_2, id_refl_0, id_refl_1);
3014 spv_emit(ctx, 5, SpvOpFMul, tid_vec3, id_refl_3, id_refl_2, id_normal);
3015 spv_emit(ctx, 5, SpvOpFMul, tid_vec3, id_refl_4, id_refl_3, id_2_v3);
3016 spv_emit(ctx, 5, SpvOpFSub, tid_vec3, id_reflected, id_refl_4, id_eyeray);
3017
3018 return id_reflected;
3019 } // spv_reflect
3020
3021 void emit_SPIRV_ADD(Context *ctx)
3022 {
3023 SpirvResult dst, src0, src1;
3024 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3025 spv_emit(ctx, 5, SpvOpFAdd, dst.tid, dst.id, src0.id, src1.id);
3026 spv_emit_end(ctx, dst);
3027 } // emit_SPIRV_ADD
3028
3029 void emit_SPIRV_SUB(Context *ctx)
3030 {
3031 SpirvResult dst, src0, src1;
3032 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3033 spv_emit(ctx, 5, SpvOpFSub, dst.tid, dst.id, src0.id, src1.id);
3034 spv_emit_end(ctx, dst);
3035 } // emit_SPIRV_SUB
3036
3037 void emit_SPIRV_MUL(Context *ctx)
3038 {
3039 SpirvResult dst, src0, src1;
3040 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3041 spv_emit(ctx, 5, SpvOpFMul, dst.tid, dst.id, src0.id, src1.id);
3042 spv_emit_end(ctx, dst);
3043 } // emit_SPIRV_MUL
3044
3045 void emit_SPIRV_SLT(Context *ctx)
3046 {
3047 SpirvResult dst, src0, src1;
3048 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3049
3050 // https://msdn.microsoft.com/en-us/library/windows/desktop/cc308050(v=vs.85).aspx
3051 // "The comparisons EQ, GT, GE, LT, and LE, when either or both operands is NaN returns FALSE"
3052 uint32 bool_result = spv_bumpid(ctx);
3053 spv_emit(ctx, 5, SpvOpFOrdLessThan, spv_get_type(ctx, STI_BVEC4), bool_result, src0.id, src1.id);
3054
3055 uint32 ones = spv_getvec4_one(ctx);
3056 uint32 zeros = spv_getvec4_zero(ctx);
3057 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, bool_result, ones, zeros);
3058
3059 spv_emit_end(ctx, dst);
3060 } // emit_SPIRV_SLT
3061
3062 void emit_SPIRV_SGE(Context *ctx)
3063 {
3064 SpirvResult dst, src0, src1;
3065 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3066
3067 // https://msdn.microsoft.com/en-us/library/windows/desktop/cc308050(v=vs.85).aspx
3068 // "The comparisons EQ, GT, GE, LT, and LE, when either or both operands is NaN returns FALSE"
3069 uint32 bool_result = spv_bumpid(ctx);
3070 spv_emit(ctx, 5, SpvOpFOrdGreaterThanEqual, spv_get_type(ctx, STI_BVEC4), bool_result, src0.id, src1.id);
3071
3072 uint32 ones = spv_getvec4_one(ctx);
3073 uint32 zeros = spv_getvec4_zero(ctx);
3074
3075 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, bool_result, ones, zeros);
3076 spv_emit_end(ctx, dst);
3077 } // emit_SPIRV_SGE
3078
3079 void emit_SPIRV_MIN(Context *ctx)
3080 {
3081 SpirvResult dst, src0, src1;
3082 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3083 spv_emit(ctx, 5 + 2, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450FMin, src0.id, src1.id);
3084 spv_emit_end(ctx, dst);
3085 } // emit_SPIRV_MIN
3086
3087 void emit_SPIRV_MAX(Context *ctx)
3088 {
3089 SpirvResult dst, src0, src1;
3090 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3091 spv_emit(ctx, 5 + 2, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450FMax, src0.id, src1.id);
3092 spv_emit_end(ctx, dst);
3093 } // emit_SPIRV_MAX
3094
3095 void emit_SPIRV_POW(Context *ctx)
3096 {
3097 SpirvResult dst, src0, src1;
3098 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3099 uint32 id_abs = spv_bumpid(ctx);
3100 spv_emit(ctx, 5 + 1, SpvOpExtInst, src0.tid, id_abs, spv_getext(ctx), GLSLstd450FAbs, src0.id);
3101 spv_emit(ctx, 5 + 2, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450Pow, id_abs, src1.id);
3102 spv_emit_end(ctx, dst);
3103 } // emit_SPIRV_POW
3104
3105 static uint32 spv_extract_vec3(Context *ctx, uint32 input)
3106 {
3107 uint32 vec3 = spv_get_type(ctx, STI_VEC3);
3108 uint32 result = spv_bumpid(ctx);
3109
3110 push_output(ctx, &ctx->mainline);
3111 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, vec3, result, input, input, 0, 1, 2);
3112 pop_output(ctx);
3113
3114 return result;
3115 } // spv_extract_vec3
3116
3117 void emit_SPIRV_CRS(Context *ctx)
3118 {
3119 SpirvResult dst, src0, src1;
3120 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3121
3122 uint32 vec3 = spv_get_type(ctx, STI_VEC3);
3123 uint32 src0_vec3 = spv_extract_vec3(ctx, src0.id);
3124 uint32 src1_vec3 = spv_extract_vec3(ctx, src1.id);
3125 uint32 result_vec3 = spv_bumpid(ctx);
3126
3127 spv_emit(ctx, 5 + 2, SpvOpExtInst, vec3, result_vec3, spv_getext(ctx),
3128 GLSLstd450Cross, src0_vec3, src1_vec3);
3129
3130 // According to DirectX docs, CRS doesn't allow `w` in its writemask, so we
3131 // can make this component anything and the code generated by
3132 // `spv_assign_destarg()` will just throw it away.
3133 spv_emit(ctx, 5 + 4, SpvOpVectorShuffle, dst.tid, dst.id,
3134 result_vec3, result_vec3, 0, 1, 2, 0xFFFFFFFF);
3135
3136 spv_emit_end(ctx, dst);
3137 } // emit_SPIRV_CRS
3138
3139 void emit_SPIRV_MAD(Context *ctx)
3140 {
3141 SpirvResult src0 = spv_load_srcarg_full(ctx, 0);
3142 SpirvResult src1 = spv_load_srcarg_full(ctx, 1);
3143 SpirvResult src2 = spv_load_srcarg_full(ctx, 2);
3144 assert(src0.tid == src1.tid);
3145 assert(src0.tid == src2.tid);
3146 uint32 mul_result = spv_bumpid(ctx);
3147 SpirvResult result;
3148 result.tid = src0.tid;
3149 result.id = spv_bumpid(ctx);
3150
3151 push_output(ctx, &ctx->mainline);
3152 spv_emit(ctx, 5, SpvOpFMul, src0.tid, mul_result, src0.id, src1.id);
3153 spv_emit(ctx, 5, SpvOpFAdd, src0.tid, result.id, mul_result, src2.id);
3154 pop_output(ctx);
3155
3156 spv_assign_destarg(ctx, result);
3157 } // emit_SPIRV_MAD
3158
3159 void emit_SPIRV_TEXKILL(Context *ctx)
3160 {
3161 const DestArgInfo *pDstInfo = &ctx->dest_arg;
3162 RegisterList *pDst = spv_getreg(ctx, pDstInfo->regtype, pDstInfo->regnum);
3163 SpirvResult dst = spv_loadreg(ctx, pDst);
3164
3165 uint32 vec3 = spv_get_type(ctx, STI_VEC3);
3166 uint32 bvec3 = spv_get_type(ctx, STI_BVEC3);
3167
3168 uint32 zeros = spv_get_zero(ctx, vec3);
3169
3170 push_output(ctx, &ctx->mainline);
3171 uint32 res_swiz = spv_emit_swizzle(ctx, dst.id, vec3, (0 << 0) | (1 << 2) | (2 << 4), 0x7);
3172 uint32 res_lt = spv_bumpid(ctx);
3173 uint32 res_any = spv_bumpid(ctx);
3174 uint32 label_true = spv_bumpid(ctx);
3175 uint32 label_merge = spv_bumpid(ctx);
3176 spv_emit(ctx, 5, SpvOpFOrdLessThan, bvec3, res_lt, res_swiz, zeros);
3177 spv_emit(ctx, 4, SpvOpAny, spv_get_type(ctx, STI_BOOL), res_any, res_lt);
3178 spv_emit(ctx, 3, SpvOpSelectionMerge, label_merge, 0);
3179 spv_emit(ctx, 4, SpvOpBranchConditional, res_any, label_true, label_merge);
3180 spv_emit(ctx, 2, SpvOpLabel, label_true);
3181 spv_emit(ctx, 1, SpvOpKill);
3182 spv_emit(ctx, 2, SpvOpLabel, label_merge);
3183 pop_output(ctx);
3184 } // emit_SPIRV_TEXKILL
3185
3186 void emit_SPIRV_DP2ADD(Context *ctx)
3187 {
3188 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x3);
3189 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x3);
3190 SpirvResult src2 = spv_load_srcarg(ctx, 2, 0x1);
3191
3192 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
3193 uint32 id_dot = spv_bumpid(ctx);
3194 uint32 id_add = spv_bumpid(ctx);
3195
3196 push_output(ctx, &ctx->mainline);
3197 spv_emit(ctx, 5, SpvOpDot, tid_float, id_dot, src0.id, src1.id);
3198 spv_emit(ctx, 5, SpvOpFAdd, tid_float, id_add, id_dot, src2.id);
3199 SpirvResult result;
3200 result.tid = spv_get_type(ctx, STI_VEC4);
3201 result.id = spv_vectorbroadcast(ctx, result.tid, id_add);
3202 pop_output(ctx);
3203
3204 spv_assign_destarg(ctx, result);
3205 } // emit_SPIRV_DP2ADD
3206
3207 void emit_SPIRV_MOV(Context *ctx)
3208 {
3209 SpirvResult src0 = spv_load_srcarg_full(ctx, 0);
3210 spv_assign_destarg(ctx, src0);
3211 } // emit_SPIRV_MOV
3212
3213 void emit_SPIRV_RCP(Context *ctx)
3214 {
3215 /*
3216 if (src != 0.0f)
3217 dst = 1.0f / src;
3218 else
3219 dst = FLT_MAX;
3220 */
3221
3222 SpirvResult dst, src;
3223 spv_emit_begin_ds(ctx, &dst, &src);
3224
3225 SpirvTypeIdx sti_bvec =
3226 (src.tid == ctx->spirv.tid[STI_VEC4]) ? STI_BVEC4 :
3227 (src.tid == ctx->spirv.tid[STI_VEC3]) ? STI_BVEC3 :
3228 (src.tid == ctx->spirv.tid[STI_VEC2]) ? STI_BVEC2 : STI_BOOL;
3229
3230 uint32 tid_bvec = spv_get_type(ctx, sti_bvec);
3231 uint32 id_one = spv_get_one(ctx, src.tid);
3232 uint32 id_zero = spv_get_zero(ctx, src.tid);
3233 uint32 id_flt_max = spv_get_flt_max(ctx, src.tid);
3234 uint32 id_mask = spv_bumpid(ctx);
3235 uint32 id_div = spv_bumpid(ctx);
3236
3237 spv_emit(ctx, 5, SpvOpFOrdNotEqual, tid_bvec, id_mask, src.id, id_zero);
3238 spv_emit(ctx, 5, SpvOpFDiv, dst.tid, id_div, id_one, src.id);
3239 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, id_mask, id_div, id_flt_max);
3240
3241 spv_emit_end(ctx, dst);
3242 } // emit_SPIRV_RCP
3243
3244 void emit_SPIRV_RSQ(Context *ctx)
3245 {
3246 /*
3247 if (src != 0.0f)
3248 dst = 1.0f / abs(src);
3249 else
3250 dst = FLT_MAX;
3251 */
3252 SpirvResult dst, src;
3253 spv_emit_begin_ds(ctx, &dst, &src);
3254
3255 SpirvTypeIdx sti_bvec =
3256 (src.tid == ctx->spirv.tid[STI_VEC4]) ? STI_BVEC4 :
3257 (src.tid == ctx->spirv.tid[STI_VEC3]) ? STI_BVEC3 :
3258 (src.tid == ctx->spirv.tid[STI_VEC2]) ? STI_BVEC2 : STI_BOOL;
3259
3260 uint32 tid_bvec = spv_get_type(ctx, sti_bvec);
3261 uint32 id_zero = spv_get_zero(ctx, src.tid);
3262 uint32 id_flt_max = spv_get_flt_max(ctx, src.tid);
3263 uint32 id_mask = spv_bumpid(ctx);
3264 uint32 id_abs = spv_bumpid(ctx);
3265 uint32 id_rsq = spv_bumpid(ctx);
3266
3267 spv_emit(ctx, 5, SpvOpFOrdNotEqual, tid_bvec, id_mask, src.id, id_zero);
3268 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, id_abs, spv_getext(ctx), GLSLstd450FAbs, src.id);
3269 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, id_rsq, spv_getext(ctx), GLSLstd450InverseSqrt, id_abs);
3270 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, id_mask, id_rsq, id_flt_max);
3271
3272 spv_emit_end(ctx, dst);
3273 } // emit_SPIRV_RSQ
3274
3275 void emit_SPIRV_EXP(Context *ctx)
3276 {
3277 SpirvResult dst, src;
3278 spv_emit_begin_ds(ctx, &dst, &src);
3279 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450Exp2, src.id);
3280 spv_emit_end(ctx, dst);
3281 } // emit_SPIRV_EXP
3282
3283 void emit_SPIRV_SGN(Context *ctx)
3284 {
3285 SpirvResult dst, src;
3286 spv_emit_begin_ds(ctx, &dst, &src);
3287
3288 // SGN also takes a src1 and src2 to use for intermediate results, they are
3289 // left undefined after the instruction executes, and as such it is
3290 // perfectly valid for us to not touch those registers in our implementation
3291 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450FSign, src.id);
3292
3293 spv_emit_end(ctx, dst);
3294 } // emit_SPIRV_SGN
3295
3296 void emit_SPIRV_ABS(Context *ctx)
3297 {
3298 SpirvResult dst, src;
3299 spv_emit_begin_ds(ctx, &dst, &src);
3300 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450FAbs, src.id);
3301 spv_emit_end(ctx, dst);
3302 } // emit_SPIRV_ABS
3303
3304 void emit_SPIRV_NRM(Context *ctx)
3305 {
3306 /*
3307 float dot = dot(src, src);
3308
3309 float f;
3310 if (dot != 0)
3311 f = (float)(1/sqrt(dot));
3312 else
3313 f = FLT_MAX;
3314
3315 dst = src0*f;
3316 */
3317
3318 SpirvResult src = spv_load_srcarg_full(ctx, 0);
3319 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
3320 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
3321 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3322 uint32 id_zero = spv_getscalarf(ctx, 0.0f);
3323 uint32 id_flt_max = spv_getscalarf(ctx, FLT_MAX);
3324 uint32 id_src_xyz = spv_bumpid(ctx);
3325 uint32 id_dot = spv_bumpid(ctx);
3326 uint32 id_dot_valid = spv_bumpid(ctx);
3327 uint32 id_f = spv_bumpid(ctx);
3328 uint32 id_f_sane = spv_bumpid(ctx);
3329 uint32 id_f_vec = spv_bumpid(ctx);
3330
3331 SpirvResult dst;
3332 dst.tid = src.tid;
3333 dst.id = spv_bumpid(ctx);
3334
3335 push_output(ctx, &ctx->mainline);
3336 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_src_xyz, src.id, src.id, 0, 1, 2);
3337 spv_emit(ctx, 5, SpvOpDot, tid_float, id_dot, id_src_xyz, id_src_xyz);
3338 spv_emit(ctx, 5, SpvOpFOrdNotEqual, tid_bool, id_dot_valid, id_dot, id_zero);
3339 spv_emit(ctx, 5 + 1, SpvOpExtInst, tid_float, id_f, spv_getext(ctx), GLSLstd450InverseSqrt, id_dot);
3340 spv_emit(ctx, 6, SpvOpSelect, tid_float, id_f_sane, id_dot_valid, id_f, id_flt_max);
3341 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, dst.tid, id_f_vec, id_f_sane, id_f_sane, id_f_sane, id_f_sane);
3342 spv_emit(ctx, 5, SpvOpFMul, dst.tid, dst.id, src.id, id_f_vec);
3343 pop_output(ctx);
3344 spv_assign_destarg(ctx, dst);
3345 } // emit_SPIRV_NRM
3346
3347 void emit_SPIRV_FRC(Context *ctx)
3348 {
3349 SpirvResult dst, src;
3350 spv_emit_begin_ds(ctx, &dst, &src);
3351 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, dst.id, spv_getext(ctx), GLSLstd450Fract, src.id);
3352 spv_emit_end(ctx, dst);
3353 } // emit_SPIRV_FRC
3354
3355 void emit_SPIRV_LOG(Context *ctx)
3356 {
3357 SpirvResult dst, src;
3358 spv_emit_begin_ds(ctx, &dst, &src);
3359
3360 // LOG(x) := (x == vec4(0.0)) ? vec4(-FLT_MAX) : log2(abs(x))
3361
3362 // abs(x)
3363 uint32 abs_src0 = spv_bumpid(ctx);
3364 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, abs_src0, spv_getext(ctx), GLSLstd450FAbs, src.id);
3365
3366 // vec4(0.0)
3367 uint32 vec4_zero = spv_vectorbroadcast(ctx, dst.tid, spv_getscalarf(ctx, 0.0f));
3368
3369 // x == vec4(0.0)
3370 uint32 is_zero = spv_bumpid(ctx);
3371 spv_emit(ctx, 5, SpvOpFOrdEqual, spv_get_type(ctx, STI_BVEC4), is_zero, abs_src0, vec4_zero);
3372
3373 // log2(abs(x))
3374 uint32 log2_of_nonzero = spv_bumpid(ctx);
3375 spv_emit(ctx, 5 + 1, SpvOpExtInst, dst.tid, log2_of_nonzero, spv_getext(ctx), GLSLstd450Log2, abs_src0);
3376
3377 // vec4(-FLT_MAX)
3378 uint32 vec4_neg_flt_max = spv_vectorbroadcast(ctx, dst.tid, spv_getscalarf(ctx, -FLT_MAX));
3379
3380 // (x == vec4(0.0)) ? vec4(-FLT_MAX) : log2(abs(x))
3381 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, is_zero, vec4_neg_flt_max, log2_of_nonzero);
3382
3383 spv_emit_end(ctx, dst);
3384 } // emit_SPIRV_LOG
3385
3386 void emit_SPIRV_SINCOS(Context *ctx)
3387 {
3388 SpirvResult src = spv_load_srcarg(ctx, 0, 0x1);
3389
3390 // For vs_2_0 and vs_2_x this instruction also has a src1 and src2 which provide a couple of constants
3391 // We just ignore these in any case
3392
3393 // float V = src0.x;
3394
3395 int writemask = ctx->dest_arg.writemask;
3396 uint32 id_zero = spv_get_zero(ctx, src.tid);
3397
3398 uint32 id_cos;
3399 if (writemask & 1) // .x = cos(V)
3400 {
3401 id_cos = spv_bumpid(ctx);
3402 spv_emit(ctx, 5 + 1, SpvOpExtInst, src.tid, id_cos, spv_getext(ctx), GLSLstd450Cos, src.id);
3403 } // if
3404 else
3405 id_cos = id_zero;
3406
3407 uint32 id_sin;
3408 if (writemask & 2) // .y = sin(V)
3409 {
3410 id_sin = spv_bumpid(ctx);
3411 spv_emit(ctx, 5 + 1, SpvOpExtInst, src.tid, id_sin, spv_getext(ctx), GLSLstd450Sin, src.id);
3412 } // if
3413 else
3414 id_sin = id_zero;
3415
3416 SpirvResult dst;
3417 dst.tid = spv_get_type(ctx, STI_VEC4);
3418 dst.id = spv_bumpid(ctx);
3419 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, dst.tid, dst.id, id_cos, id_sin, id_zero, id_zero);
3420
3421 spv_assign_destarg(ctx, dst);
3422 } // emit_SPIRV_SINCOS
3423
3424 void emit_SPIRV_MOVA(Context *ctx)
3425 {
3426 SpirvResult src = spv_load_srcarg_full(ctx, 0);
3427 assert(src.tid == spv_get_type(ctx, STI_VEC4));
3428
3429 uint32 id_rounded = spv_bumpid(ctx);
3430
3431 SpirvResult dst;
3432 dst.tid = spv_get_type(ctx, STI_IVEC4);
3433 dst.id = spv_bumpid(ctx);
3434
3435 push_output(ctx, &ctx->mainline);
3436 spv_emit(ctx, 5 + 1, SpvOpExtInst, spv_get_type(ctx, STI_VEC4), id_rounded,
3437 spv_getext(ctx), GLSLstd450Round, src.id);
3438 spv_emit(ctx, 4, SpvOpConvertFToS, dst.tid, dst.id, id_rounded);
3439 pop_output(ctx);
3440
3441 spv_assign_destarg(ctx, dst);
3442 } // emit_SPIRV_MOVA
3443
3444 void emit_SPIRV_CMP(Context *ctx)
3445 {
3446 SpirvResult dst, src0, src1, src2;
3447 spv_emit_begin_dsss(ctx, &dst, &src0, &src1, &src2);
3448 uint32 id_0_0 = spv_get_zero(ctx, src0.tid);
3449
3450 uint32 id_cmp = spv_bumpid(ctx);
3451 spv_emit(ctx, 5, SpvOpFUnordGreaterThanEqual, spv_get_type(ctx, STI_BVEC4), id_cmp, src0.id, id_0_0);
3452 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, id_cmp, src1.id, src2.id);
3453 spv_emit_end(ctx, dst);
3454 } // emit_SPIRV_CMP
3455
3456 void emit_SPIRV_CND(Context *ctx)
3457 {
3458 SpirvResult dst, src0, src1, src2;
3459 spv_emit_begin_dsss(ctx, &dst, &src0, &src1, &src2);
3460 uint32 id_0_5 = spv_get_constant_composite(ctx, src0.tid, ctx->spirv.id_0_5, 0.5f);
3461
3462 uint32 id_cmp = spv_bumpid(ctx);
3463 spv_emit(ctx, 5, SpvOpFUnordGreaterThan, spv_get_type(ctx, STI_BVEC4), id_cmp, src0.id, id_0_5);
3464 spv_emit(ctx, 6, SpvOpSelect, dst.tid, dst.id, id_cmp, src1.id, src2.id);
3465 spv_emit_end(ctx, dst);
3466 } // emit_SPIRV_CND
3467
3468 void emit_SPIRV_LIT(Context *ctx)
3469 {
3470 SpirvResult dst, src;
3471 spv_emit_begin_ds(ctx, &dst, &src);
3472
3473 if (!ctx->spirv.id_func_lit)
3474 ctx->spirv.id_func_lit = spv_bumpid(ctx);
3475
3476 spv_emit(ctx, 5, SpvOpFunctionCall, dst.tid, dst.id, ctx->spirv.id_func_lit, src.id);
3477
3478 spv_emit_end(ctx, dst);
3479 } // emit_SPIRV_LIT
3480
3481 void emit_SPIRV_DST(Context *ctx)
3482 {
3483 SpirvResult dst, src0, src1;
3484 spv_emit_begin_dss(ctx, &dst, &src0, &src1);
3485
3486 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
3487 dst.tid = spv_get_type(ctx, STI_VEC4);
3488 uint32 id_1_0 = spv_getscalarf(ctx, 1.0f);
3489 uint32 id_src0_y = spv_bumpid(ctx);
3490 uint32 id_src1_y = spv_bumpid(ctx);
3491 uint32 id_src0_z = spv_bumpid(ctx);
3492 uint32 id_src1_w = spv_bumpid(ctx);
3493 uint32 id_dst_y = spv_bumpid(ctx);
3494 dst.id = spv_bumpid(ctx);
3495
3496 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src0_y, src0.id, 1);
3497 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src1_y, src1.id, 1);
3498 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src0_z, src0.id, 2);
3499 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_src1_w, src1.id, 3);
3500 spv_emit(ctx, 5, SpvOpFMul, tid_float, id_dst_y, id_src0_y, id_src1_y);
3501 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, dst.tid, dst.id, id_1_0, id_dst_y, id_src0_z, id_src1_w);
3502
3503 spv_emit_end(ctx, dst);
3504 } // emit_SPIRV_DST
3505
3506 void emit_SPIRV_LRP(Context *ctx)
3507 {
3508 // lerp(x, y, a) = x + a*(y - x)
3509 // = x*(1 - a) + y*a
3510 SpirvResult a = spv_load_srcarg_full(ctx, 0); // 'scale'
3511 SpirvResult y = spv_load_srcarg_full(ctx, 1); // 'end'
3512 SpirvResult x = spv_load_srcarg_full(ctx, 2); // 'start'
3513 assert(x.tid == y.tid);
3514 SpirvResult result;
3515 result.id = spv_bumpid(ctx);
3516 result.tid = x.tid;
3517
3518 push_output(ctx, &ctx->mainline);
3519 spv_emit(ctx, 5 + 3, SpvOpExtInst, result.tid, result.id, spv_getext(ctx), GLSLstd450FMix, x.id, y.id, a.id);
3520 pop_output(ctx);
3521
3522 spv_assign_destarg(ctx, result);
3523 } // emit_SPIRV_LRP
3524
3525 static void spv_emit_vecXmatrix(Context *ctx, int rows, int writemask)
3526 {
3527 int i;
3528
3529 assert(rows <= 4);
3530 assert(writemask == 0x7 || writemask == 0xF);
3531
3532 uint32 src0 = spv_load_srcarg(ctx, 0, writemask).id;
3533 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
3534
3535 RegisterType src1type = ctx->source_args[1].regtype;
3536 int src1num = ctx->source_args[1].regnum;
3537
3538 uint32 result_components[4];
3539 for (i = 0; i < rows; i++)
3540 {
3541 SpirvResult row = spv_loadreg(ctx, spv_getreg(ctx, src1type, src1num + i));
3542 row = spv_swizzle(ctx, row, SPV_NO_SWIZZLE, writemask);
3543 uint32 dot_result = spv_bumpid(ctx);
3544
3545 push_output(ctx, &ctx->mainline);
3546 spv_emit(ctx, 5, SpvOpDot, tid_float, dot_result, src0, row.id);
3547 pop_output(ctx);
3548
3549 result_components[i] = dot_result;
3550 } // for
3551
3552 SpirvResult r;
3553 r.tid = spv_get_type(ctx, STI_VEC4);
3554 r.id = spv_bumpid(ctx);
3555
3556 uint32 id_zero = 0;
3557 if (rows < 4)
3558 id_zero = spv_getscalarf(ctx, 0.0f);
3559
3560 push_output(ctx, &ctx->mainline);
3561 spv_emit_part(ctx, 3 + 4, 3, SpvOpCompositeConstruct, r.tid, r.id);
3562 for (i = 0; i < rows; i++) spv_emit_word(ctx, result_components[i]);
3563 for (i = rows; i < 4; i++) spv_emit_word(ctx, id_zero);
3564 pop_output(ctx);
3565
3566 spv_assign_destarg(ctx, r);
3567 } // spv_emit_vecXmatrix
3568
3569 void emit_SPIRV_M4X4(Context *ctx)
3570 {
3571 // float4 * (4 columns, 4 rows) -> float4
3572 spv_emit_vecXmatrix(ctx, 4, 0xF);
3573 } // emit_SPIRV_M4X4
3574
3575 void emit_SPIRV_M4X3(Context *ctx)
3576 {
3577 // float4 * (4 columns, 3 rows) -> float3
3578 spv_emit_vecXmatrix(ctx, 3, 0xF);
3579 } // emit_SPIRV_M4X3
3580
3581 void emit_SPIRV_M3X4(Context *ctx)
3582 {
3583 // float3 * (3 columns, 4 rows) -> float4
3584 spv_emit_vecXmatrix(ctx, 4, 0x7);
3585 } // emit_SPIRV_M3X4
3586
3587 void emit_SPIRV_M3X3(Context *ctx)
3588 {
3589 // float3 * (3 columns, 3 rows) -> float3
3590 spv_emit_vecXmatrix(ctx, 3, 0x7);
3591 } // emit_SPIRV_M3X3
3592
3593 void emit_SPIRV_M3X2(Context *ctx)
3594 {
3595 // float3 * (3 columns, 2 rows) -> float2
3596 spv_emit_vecXmatrix(ctx, 2, 0x7);
3597 } // emit_SPIRV_M3X2
3598
3599 void emit_SPIRV_TEXLD(Context *ctx)
3600 {
3601 if (!shader_version_atleast(ctx, 1, 4))
3602 {
3603 DestArgInfo *dst_info = &ctx->dest_arg;
3604
3605 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, dst_info->regnum);
3606 RegisterList *treg = spv_getreg(ctx, dst_info->regtype, dst_info->regnum);
3607
3608 // Variables are not declared using dcl opcodes, so handle it in this instruction.
3609 assert(sreg->spirv.iddecl == 0);
3610 assert(treg->spirv.iddecl == 0);
3611
3612 // Prep the result
3613 SpirvResult result;
3614 result.tid = spv_get_type(ctx, STI_VEC4);
3615 result.id = spv_bumpid(ctx);
3616 SpirvResult sampler = spv_loadreg(ctx, sreg);
3617 // OpImageSampleImplicitLod should ignore the components of this argument that
3618 // it doesn't need, so we don't need to mask it
3619 SpirvResult texcoord = spv_loadreg(ctx, treg);
3620
3621 // Generate the instruction.
3622 // OpImageSampleImplicitLod should ignore the components of the
3623 // texcoord that it doesn't need, so we don't need to mask it.
3624 push_output(ctx, &ctx->mainline);
3625 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id,
3626 sampler.id, texcoord.id);
3627 pop_output(ctx);
3628
3629 // Emit the result, finally.
3630 assert(!isscalar(ctx, ctx->shader_type, sreg->regtype, sreg->regnum));
3631 spv_assign_destarg(ctx, result);
3632 } // if
3633
3634 else if (!shader_version_atleast(ctx, 2, 0))
3635 {
3636 // ps_1_4 is different, too!
3637 fail(ctx, "TEXLD == Shader Model 1.4 unimplemented."); // !!! FIXME
3638 return;
3639 } // else if
3640
3641 else
3642 {
3643 const SourceArgInfo *samp_arg = &ctx->source_args[1];
3644 RegisterList *sampler_reg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, samp_arg->regnum);
3645 const SourceArgInfo *texcoord_arg = &ctx->source_args[0];
3646 RegisterList *texcoord_reg = spv_getreg(ctx, texcoord_arg->regtype, texcoord_arg->regnum);
3647
3648 if (sampler_reg == NULL)
3649 {
3650 fail(ctx, "TEXLD using undeclared sampler");
3651 return;
3652 } // if
3653
3654 // OpImageSampleImplicitLod should ignore the components of this argument that
3655 // it doesn't need, so we don't need to mask it
3656 uint32 texcoord = spv_load_srcarg_full(ctx, 0).id;
3657 uint32 sampler = spv_load_srcarg_full(ctx, 1).id;
3658
3659 // Special case for TEXLDB
3660 // !!! FIXME: does the d3d bias value map directly to GLSL?
3661 uint32 bias;
3662 uint32 instruction_length;
3663 if (ctx->instruction_controls == CONTROL_TEXLDB)
3664 {
3665 uint32 float_tid = spv_get_type(ctx, STI_FLOAT);
3666 bias = spv_bumpid(ctx);
3667 instruction_length = 7;
3668
3669 // The w component of texcoord_reg specifies the bias. Extract it from texcoord_reg
3670 push_output(ctx, &ctx->mainline);
3671 spv_emit(ctx, 4 + 1, SpvOpCompositeExtract, float_tid, bias, texcoord, 3);
3672 pop_output(ctx);
3673 } // if
3674 else
3675 {
3676 bias = 0;
3677 instruction_length = 5;
3678 } // else
3679
3680 // Determine the opcode
3681 SpvOp opcode;
3682 if (ctx->instruction_controls == CONTROL_TEXLDP)
3683 {
3684 if ((TextureType) sampler_reg->index == TEXTURE_TYPE_CUBE)
3685 fail(ctx, "TEXLDP on a cubemap"); // !!! FIXME: is this legal?
3686 opcode = SpvOpImageSampleProjImplicitLod;
3687 } // if
3688 else
3689 opcode = SpvOpImageSampleImplicitLod;
3690
3691 // Prep the result
3692 uint32 vec4_tid = spv_get_type(ctx, STI_VEC4);
3693 uint32 result = spv_bumpid(ctx);
3694
3695 // Generate the instruction.
3696 // OpImageSampleImplicitLod should ignore the components of the
3697 // texcoord that it doesn't need, so we don't need to mask it.
3698 push_output(ctx, &ctx->mainline);
3699 spv_emit_part(ctx, instruction_length, 5, opcode, vec4_tid, result,
3700 sampler, texcoord);
3701 if (ctx->instruction_controls == CONTROL_TEXLDB)
3702 {
3703 // ... include the bias operand, if applicable
3704 spv_emit_word(ctx, SpvImageOperandsBiasMask);
3705 spv_emit_word(ctx, bias);
3706 } // if
3707 pop_output(ctx);
3708
3709 // Emit the result, finally.
3710 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
3711 SpirvResult r;
3712 r.id = result;
3713 r.tid = vec4_tid;
3714 spv_assign_destarg(ctx, r);
3715 } // else
3716 } // emit_SPIRV_TEXLD
3717
3718 void emit_SPIRV_IF(Context *ctx)
3719 {
3720 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x1);
3721 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3722 uint32 id_cond = src0.id;
3723
3724 // Predicate register is already boolean so no need to convert.
3725 if (src0.tid != tid_bool)
3726 {
3727 uint32 id_zero = spv_getscalari(ctx, 0);
3728 id_cond = spv_bumpid(ctx);
3729 spv_emit(ctx, 5, SpvOpINotEqual, tid_bool, id_cond, src0.id, id_zero);
3730 } // if
3731
3732 uint32 id_label_branch = spv_bumpid(ctx);
3733 uint32 id_label_merge = spv_bumpid(ctx);
3734 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
3735 spv_emit(ctx, 4, SpvOpBranchConditional, id_cond, id_label_branch, id_label_merge);
3736 spv_branch_push(ctx, id_label_merge, buffer_size(ctx->output) - 4);
3737 spv_emit(ctx, 2, SpvOpLabel, id_label_branch);
3738 } // emit_SPIRV_IF
3739
3740 void emit_SPIRV_IFC(Context *ctx)
3741 {
3742 SpvOp cmp_op = spv_get_comparison(ctx);
3743 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x1);
3744 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x1);
3745 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3746 uint32 id_cond = spv_bumpid(ctx);
3747 uint32 id_label_branch = spv_bumpid(ctx);
3748 uint32 id_label_merge = spv_bumpid(ctx);
3749
3750 spv_emit(ctx, 5, cmp_op, tid_bool, id_cond, src0.id, src1.id);
3751 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
3752 spv_emit(ctx, 4, SpvOpBranchConditional, id_cond, id_label_branch, id_label_merge);
3753 spv_branch_push(ctx, id_label_merge, buffer_size(ctx->output) - 4);
3754 spv_emit(ctx, 2, SpvOpLabel, id_label_branch);
3755 } // emit_SPIRV_IFC
3756
3757 void emit_SPIRV_ELSE(Context *ctx)
3758 {
3759 uint32 id_label_merge, patch_offset;
3760 spv_branch_get(ctx, &id_label_merge, &patch_offset);
3761 uint32 id_label_else = spv_bumpid(ctx);
3762
3763 buffer_patch(ctx->output, patch_offset, &id_label_else, sizeof(id_label_else));
3764 spv_emit(ctx, 2, SpvOpBranch, id_label_merge);
3765 spv_emit(ctx, 2, SpvOpLabel, id_label_else);
3766 } // emit_SPIRV_ELSE
3767
3768 void emit_SPIRV_ENDIF(Context *ctx)
3769 {
3770 uint32 id_label_merge, patch_offset;
3771 spv_branch_pop(ctx, &id_label_merge, &patch_offset);
3772
3773 spv_emit(ctx, 2, SpvOpBranch, id_label_merge);
3774 spv_emit(ctx, 2, SpvOpLabel, id_label_merge);
3775 } // emit_SPIRV_ENDIF
3776
3777 void emit_SPIRV_REP(Context *ctx)
3778 {
3779 SpirvLoopInfo loop = {0};
3780 uint32 id_label_init = spv_bumpid(ctx);
3781 loop.id_label_header = spv_bumpid(ctx);
3782 uint32 id_label_cond = spv_bumpid(ctx);
3783 uint32 id_label_body = spv_bumpid(ctx);
3784 loop.id_label_continue = spv_bumpid(ctx);
3785 loop.id_label_merge = spv_bumpid(ctx);
3786
3787 // emit end of previous block
3788 spv_emit(ctx, 2, SpvOpBranch, id_label_init);
3789
3790 // emit loop init block
3791 spv_emit(ctx, 2, SpvOpLabel, id_label_init);
3792 // This block only exists to allow use of SpvOpPhi in loop header block.
3793 // SpvOpPhi needs to refer to predecessor by it's label ID, so insert dummy
3794 // block just so we know what the ID is.
3795 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x1);
3796
3797 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3798 loop.tid_counter = src0.tid;
3799 loop.id_counter = spv_bumpid(ctx);
3800 loop.id_counter_next = spv_bumpid(ctx);
3801
3802 uint32 id_cond = spv_bumpid(ctx);
3803 uint32 id_zero = spv_getscalari(ctx, 0);
3804 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_header);
3805
3806 // emit loop header block
3807 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_header);
3808 spv_emit(ctx, 7, SpvOpPhi, loop.tid_counter, loop.id_counter,
3809 src0.id, id_label_init,
3810 loop.id_counter_next, loop.id_label_continue
3811 );
3812 spv_emit(ctx, 4, SpvOpLoopMerge, loop.id_label_merge, loop.id_label_continue, 0);
3813 spv_emit(ctx, 2, SpvOpBranch, id_label_cond);
3814
3815 // emit loop condition block
3816 spv_emit(ctx, 2, SpvOpLabel, id_label_cond);
3817 spv_emit(ctx, 5, SpvOpINotEqual, tid_bool, id_cond, loop.id_counter, id_zero);
3818 spv_emit(ctx, 4, SpvOpBranchConditional, id_cond, id_label_body, loop.id_label_merge);
3819
3820 // emit start of loop body block
3821 spv_emit(ctx, 2, SpvOpLabel, id_label_body);
3822
3823 spv_loop_push(ctx, &loop);
3824 } // emit_SPIRV_REP
3825
3826 void emit_SPIRV_ENDREP(Context *ctx)
3827 {
3828 uint32 id_one = spv_getscalari(ctx, 1);
3829 SpirvLoopInfo loop;
3830 spv_loop_pop(ctx, &loop);
3831
3832 // emit end of loop body block
3833 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_continue);
3834
3835 // emit loop continue block
3836 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_continue);
3837 spv_emit(ctx, 5, SpvOpISub, loop.tid_counter, loop.id_counter_next, loop.id_counter, id_one);
3838 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_header);
3839
3840 // emit start of next block
3841 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_merge);
3842 } // emit_SPIRV_ENDREP
3843
3844 void emit_SPIRV_LOOP(Context *ctx)
3845 {
3846 SpirvLoopInfo loop = {0};
3847 uint32 id_label_init = spv_bumpid(ctx);
3848 loop.id_label_header = spv_bumpid(ctx);
3849 uint32 id_label_cond = spv_bumpid(ctx);
3850 uint32 id_label_body = spv_bumpid(ctx);
3851 loop.id_label_continue = spv_bumpid(ctx);
3852 loop.id_label_merge = spv_bumpid(ctx);
3853
3854 /*
3855 i#.x = iteration count; every round we decrement it and terminate on 0.
3856 i#.y = aL initial value; every round we subtract aL step from it.
3857 i#.z = aL step value;
3858
3859 We use copy of i# as iteration variable. Compared to rep loop, we only
3860 need to add single instruction for extracting current aL value as single
3861 int.
3862
3863 rep i0
3864 for (int i = i0.x; i; i--)
3865
3866 loop aL, i0
3867 for (int3 i = i0, int aL = i.y; i.x; i.x--, aL += i.z)
3868 */
3869
3870 // emit end of previous block
3871 spv_emit(ctx, 2, SpvOpBranch, id_label_init);
3872
3873 // emit loop init block
3874 spv_emit(ctx, 2, SpvOpLabel, id_label_init);
3875 // This block only exists to allow use of SpvOpPhi in loop header block.
3876 // SpvOpPhi needs to refer to predecessor by it's label ID, so insert dummy block just so we
3877 // know what the ID is.
3878
3879 // src0 has aL register. Does it hold any interesting information?
3880 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x7);
3881 uint32 tid_int = spv_get_type(ctx, STI_INT);
3882 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3883
3884 loop.tid_counter = src1.tid;
3885 loop.id_counter = spv_bumpid(ctx);
3886 loop.id_counter_next = spv_bumpid(ctx);
3887 loop.id_aL = spv_bumpid(ctx);
3888 uint32 id_counter_x = spv_bumpid(ctx);
3889
3890 uint32 id_cond = spv_bumpid(ctx);
3891 uint32 id_zero = spv_getscalari(ctx, 0);
3892 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_header);
3893
3894 // emit loop header block
3895 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_header);
3896 spv_emit(ctx, 7, SpvOpPhi, loop.tid_counter, loop.id_counter,
3897 src1.id, id_label_init,
3898 loop.id_counter_next, loop.id_label_continue
3899 );
3900 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_int, loop.id_aL, loop.id_counter, 1);
3901 spv_emit(ctx, 4, SpvOpLoopMerge, loop.id_label_merge, loop.id_label_continue, 0);
3902 spv_emit(ctx, 2, SpvOpBranch, id_label_cond);
3903
3904 // emit loop condition block
3905 spv_emit(ctx, 2, SpvOpLabel, id_label_cond);
3906 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_int, id_counter_x, loop.id_counter, 0);
3907 spv_emit(ctx, 5, SpvOpINotEqual, tid_bool, id_cond, id_counter_x, id_zero);
3908 spv_emit(ctx, 4, SpvOpBranchConditional, id_cond, id_label_body, loop.id_label_merge);
3909
3910 // emit start of loop body block
3911 spv_emit(ctx, 2, SpvOpLabel, id_label_body);
3912
3913 spv_loop_push(ctx, &loop);
3914 } // emit_SPIRV_LOOP
3915
3916 void emit_SPIRV_ENDLOOP(Context *ctx)
3917 {
3918 uint32 tid_int = spv_get_type(ctx, STI_INT);
3919 uint32 tid_ivec2 = spv_get_type(ctx, STI_IVEC2);
3920
3921 uint32 id_minus_one = spv_getscalari(ctx, -1);
3922 uint32 id_counter_z = spv_bumpid(ctx);
3923 uint32 id_inc = spv_bumpid(ctx);
3924 uint32 id_counter_xy = spv_bumpid(ctx);
3925 uint32 id_counter_next_xy = spv_bumpid(ctx);
3926
3927 SpirvLoopInfo loop;
3928 spv_loop_pop(ctx, &loop);
3929
3930 // emit end of loop body block
3931 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_continue);
3932
3933 // emit loop continue block
3934 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_continue);
3935 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_int, id_counter_z, loop.id_counter, 2);
3936 spv_emit(ctx, 5, SpvOpCompositeConstruct, tid_ivec2, id_inc, id_minus_one, id_counter_z);
3937 spv_emit(ctx, 7, SpvOpVectorShuffle, tid_ivec2, id_counter_xy, loop.id_counter, loop.id_counter, 0, 1);
3938 spv_emit(ctx, 5, SpvOpIAdd, tid_ivec2, id_counter_next_xy, id_counter_xy, id_inc);
3939 spv_emit(ctx, 5, SpvOpCompositeConstruct, loop.tid_counter, loop.id_counter_next, id_counter_next_xy, id_counter_z);
3940 spv_emit(ctx, 2, SpvOpBranch, loop.id_label_header);
3941
3942 // emit start of next block
3943 spv_emit(ctx, 2, SpvOpLabel, loop.id_label_merge);
3944 } // emit_SPIRV_ENDLOOP
3945
3946 void emit_SPIRV_BREAKC(Context *ctx)
3947 {
3948 SpirvLoopInfo loop;
3949 spv_loop_get(ctx, &loop);
3950
3951 SpvOp cmp_op = spv_get_comparison(ctx);
3952 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x1);
3953 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x1);
3954 uint32 tid_bool = spv_get_type(ctx, STI_BOOL);
3955 uint32 id_cond = spv_bumpid(ctx);
3956 uint32 id_label_merge = spv_bumpid(ctx);
3957
3958 // emit branch to merge target
3959 spv_emit(ctx, 5, cmp_op, tid_bool, id_cond, src0.id, src1.id);
3960 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
3961 spv_emit(ctx, 4, SpvOpBranchConditional, id_cond, loop.id_label_merge, id_label_merge);
3962 spv_emit(ctx, 2, SpvOpLabel, id_label_merge);
3963 } // emit_SPIRV_BREAKC
3964
3965 void emit_SPIRV_BREAKP(Context *ctx)
3966 {
3967 SpirvLoopInfo loop;
3968 spv_loop_get(ctx, &loop);
3969
3970 SpirvResult src0 = spv_load_srcarg(ctx, 0, 0x1);
3971
3972 uint32 id_label_merge = spv_bumpid(ctx);
3973
3974 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
3975 spv_emit(ctx, 4, SpvOpBranchConditional, src0.id, loop.id_label_merge, id_label_merge);
3976 spv_emit(ctx, 2, SpvOpLabel, id_label_merge);
3977 } // emit_SPIRV_BREAKP
3978
3979 void emit_SPIRV_LABEL(Context *ctx)
3980 {
3981 const SourceArgInfo* arg = &ctx->source_args[0];
3982 RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
3983 spv_check_read_reg_id(ctx, reg);
3984
3985 uint32 tid_void = spv_get_type(ctx, STI_VOID);
3986 uint32 tid_func = spv_get_type(ctx, STI_FUNC_VOID);
3987 uint32 id_func = reg->spirv.iddecl;
3988 uint32 id_label = spv_bumpid(ctx);
3989
3990 push_output(ctx, &ctx->mainline);
3991 spv_emit(ctx, 5, SpvOpFunction, tid_void, id_func, 0, tid_func);
3992 spv_emit(ctx, 2, SpvOpLabel, id_label);
3993 pop_output(ctx);
3994 } // emit_SPIRV_LABEL
3995
3996 void emit_SPIRV_RET(Context *ctx)
3997 {
3998 spv_emit_func_end(ctx);
3999 } // emit_SPIRV_RET
4000
4001 void emit_SPIRV_CALL(Context *ctx)
4002 {
4003 const SourceArgInfo* arg = &ctx->source_args[0];
4004 RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
4005 spv_check_read_reg_id(ctx, reg);
4006
4007 uint32 tid_void = spv_get_type(ctx, STI_VOID);
4008 uint32 id_res = spv_bumpid(ctx);
4009 uint32 id_func = reg->spirv.iddecl;
4010
4011 push_output(ctx, &ctx->mainline);
4012 if (ctx->loops > 0)
4013 failf(ctx, "Function calls referencing aL not implemented.");
4014 else
4015 spv_emit(ctx, 4, SpvOpFunctionCall, tid_void, id_res, id_func);
4016
4017 pop_output(ctx);
4018 } // emit_SPIRV_CALL
4019
4020 void emit_SPIRV_CALLNZ(Context *ctx)
4021 {
4022 const SourceArgInfo* arg = &ctx->source_args[0];
4023 RegisterList *reg = spv_getreg(ctx, arg->regtype, arg->regnum);
4024 spv_check_read_reg_id(ctx, reg);
4025
4026 SpirvResult src1 = spv_load_srcarg(ctx, 1, 0x1);
4027
4028 uint32 tid_void = spv_get_type(ctx, STI_VOID);
4029 uint32 id_label_then = spv_bumpid(ctx);
4030 uint32 id_func = reg->spirv.iddecl;
4031 uint32 id_call_res = spv_bumpid(ctx);
4032 uint32 id_label_merge = spv_bumpid(ctx);
4033
4034 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
4035 spv_emit(ctx, 4, SpvOpBranchConditional, src1.id, id_label_then, id_label_merge);
4036
4037 spv_emit(ctx, 2, SpvOpLabel, id_label_then);
4038 if (ctx->loops > 0)
4039 failf(ctx, "Function calls referencing aL not implemented.");
4040 else
4041 spv_emit(ctx, 4, SpvOpFunctionCall, tid_void, id_call_res, id_func);
4042 spv_emit(ctx, 2, SpvOpBranch, id_label_merge);
4043
4044 spv_emit(ctx, 2, SpvOpLabel, id_label_merge);
4045 } // emit_SPIRV_CALLNZ
4046
4047 void emit_SPIRV_TEXLDD(Context *ctx)
4048 {
4049 const SourceArgInfo *samp_arg = &ctx->source_args[1];
4050 if (!reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, samp_arg->regnum))
4051 {
4052 fail(ctx, "TEXLDD using undeclared sampler");
4053 return;
4054 } // if
4055
4056 // Prep the result
4057 SpirvResult result;
4058 result.tid = spv_get_type(ctx, STI_VEC4);
4059 result.id = spv_bumpid(ctx);
4060
4061 SpirvResult texcoord = spv_load_srcarg_full(ctx, 0);
4062 SpirvResult sampler = spv_load_srcarg_full(ctx, 1);
4063 SpirvResult grad_x = spv_load_srcarg_full(ctx, 2);
4064 SpirvResult grad_y = spv_load_srcarg_full(ctx, 3);
4065
4066 // Generate the instruction.
4067 // SpvOpImageSampleExplicitLod should ignore the components of the
4068 // texcoord that it doesn't need, so we don't need to mask it.
4069 push_output(ctx, &ctx->mainline);
4070 spv_emit(ctx, 8, SpvOpImageSampleExplicitLod, result.tid, result.id, sampler.id,
4071 texcoord.id, SpvImageOperandsGradMask, grad_x.id, grad_y.id);
4072 pop_output(ctx);
4073
4074 // Emit the result, finally.
4075 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
4076 spv_assign_destarg(ctx, result);
4077 } // emit_SPIRV_TEXLDD
4078
4079 void emit_SPIRV_SETP(Context *ctx)
4080 {
4081 SpirvResult src0 = spv_load_srcarg_full(ctx, 0);
4082 SpirvResult src1 = spv_load_srcarg_full(ctx, 1);
4083
4084 SpirvResult dst;
4085 dst.tid = spv_get_type(ctx, STI_BVEC4);
4086 dst.id = spv_bumpid(ctx);
4087
4088 SpvOp cmp_op = spv_get_comparison(ctx);
4089
4090 push_output(ctx, &ctx->mainline);
4091 spv_emit(ctx, 5, cmp_op, dst.tid, dst.id, src0.id, src1.id);
4092 pop_output(ctx);
4093
4094 spv_assign_destarg(ctx, dst);
4095 } // emit_SPIRV_SETP
4096
4097 void emit_SPIRV_TEXLDL(Context *ctx)
4098 {
4099 const SourceArgInfo *samp_arg = &ctx->source_args[1];
4100 RegisterList *sampler_reg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, samp_arg->regnum);
4101 if (sampler_reg == NULL)
4102 {
4103 fail(ctx, "TEXLDL using undeclared sampler");
4104 return;
4105 } // if
4106 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
4107
4108 // Prep the result
4109 SpirvResult result;
4110 result.tid = spv_get_type(ctx, STI_VEC4);
4111 result.id = spv_bumpid(ctx);
4112
4113 SpirvResult sampler = spv_load_srcarg_full(ctx, 1);
4114 SpirvResult texcoord = spv_load_srcarg_full(ctx, 0);
4115
4116 // The w component of texcoord_reg specifies the LOD. Extract it from texcoord_reg
4117 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
4118 uint32 id_lod = spv_bumpid(ctx);
4119
4120 // Generate the instruction.
4121 // SpvOpImageSampleExplicitLod should ignore the components of the
4122 // texcoord that it doesn't need, so we don't need to mask it.
4123 push_output(ctx, &ctx->mainline);
4124 spv_emit(ctx, 4 + 1, SpvOpCompositeExtract, tid_float, id_lod, texcoord.id, 3);
4125 spv_emit(ctx, 7, SpvOpImageSampleExplicitLod, result.tid, result.id, sampler.id,
4126 texcoord.id, SpvImageOperandsLodMask, id_lod);
4127 pop_output(ctx);
4128
4129 // Emit the result, finally.
4130 spv_assign_destarg(ctx, result);
4131 } // emit_SPIRV_TEXLDL
4132
4133 void emit_SPIRV_BREAK(Context *ctx)
4134 {
4135 uint32 id_label_merge = spv_bumpid(ctx);
4136 spv_emit(ctx, 3, SpvOpSelectionMerge, id_label_merge, 0);
4137 spv_emit(ctx, 2, SpvOpBranch, id_label_merge);
4138 spv_emit(ctx, 2, SpvOpLabel, id_label_merge);
4139 } // emit_SPIRV_BREAK
4140
4141 void emit_SPIRV_TEXM3X2PAD(Context *ctx)
4142 {
4143 // no-op ... work happens in emit_SPIRV_TEXM3X2TEX().
4144 } // emit_SPIRV_TEXM3X2PAD
4145
4146 void emit_SPIRV_TEXM3X2TEX(Context *ctx)
4147 {
4148 if (ctx->texm3x2pad_src0 == -1)
4149 return;
4150
4151 DestArgInfo *pDstInfo = &ctx->dest_arg;
4152
4153 RegisterList *pSReg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, pDstInfo->regnum);
4154 RegisterList *pSrc0 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0);
4155 RegisterList *pSrc1 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0);
4156 RegisterList *pSrc2 = spv_getreg(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum);
4157 RegisterList *pDst = spv_getreg(ctx, pDstInfo->regtype, pDstInfo->regnum);
4158
4159 SpirvResult sampler = spv_loadreg(ctx, pSReg);
4160 SpirvResult src0 = spv_loadreg(ctx, pSrc0);
4161 SpirvResult src1 = spv_loadreg(ctx, pSrc1);
4162 SpirvResult src2 = spv_loadreg(ctx, pSrc2);
4163 SpirvResult src3 = spv_loadreg(ctx, pDst);
4164
4165 src0 = spv_swizzle(ctx, src0, SPV_NO_SWIZZLE, 0x7);
4166 src1 = spv_swizzle(ctx, src1, SPV_NO_SWIZZLE, 0x7);
4167 src2 = spv_swizzle(ctx, src2, SPV_NO_SWIZZLE, 0x7);
4168 src3 = spv_swizzle(ctx, src3, SPV_NO_SWIZZLE, 0x7);
4169
4170 SpirvResult result;
4171 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
4172 uint32 tid_vec2 = spv_get_type(ctx, STI_VEC2);
4173 result.tid = spv_get_type(ctx, STI_VEC4);
4174 uint32 id_x = spv_bumpid(ctx);
4175 uint32 id_y = spv_bumpid(ctx);
4176 uint32 id_texcoord = spv_bumpid(ctx);
4177 result.id = spv_bumpid(ctx);
4178 push_output(ctx, &ctx->mainline);
4179 spv_emit(ctx, 5, SpvOpDot, tid_float, id_x, src0.id, src1.id);
4180 spv_emit(ctx, 5, SpvOpDot, tid_float, id_y, src2.id, src3.id);
4181 spv_emit(ctx, 3+2, SpvOpCompositeConstruct, tid_vec2, id_texcoord, id_x, id_y);
4182 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id, sampler.id, id_texcoord);
4183 pop_output(ctx);
4184 spv_assign_destarg(ctx, result);
4185 } // emit_SPIRV_TEXM3X2TEX
4186
4187 void emit_SPIRV_TEXM3X3PAD(Context *ctx)
4188 {
4189 // no-op ... work happens in emit_SPIRV_TEXM3X3*().
4190 } // emit_SPIRV_TEXM3X3PAD
4191
4192 void emit_SPIRV_TEXM3X3(Context *ctx)
4193 {
4194 if (ctx->texm3x3pad_src1 == -1)
4195 return;
4196
4197 // vec4(
4198 // dot({src0}.xyz, {src1}.xyz),
4199 // dot({src2}.xyz, {src3}.xyz),
4200 // dot({dst}.xyz, {src4}.xyz),
4201 // 1
4202 // )
4203
4204 uint32 id_1 = spv_getscalarf(ctx, 1.0f);
4205
4206 SpirvTexm3x3SetupResult setup = spv_texm3x3_setup(ctx);
4207
4208 SpirvResult result;
4209 result.tid = spv_get_type(ctx, STI_VEC4);
4210 result.id = spv_bumpid(ctx);
4211
4212 push_output(ctx, &ctx->mainline);
4213 spv_emit(ctx, 3 + 4, SpvOpCompositeConstruct, result.tid, result.id,
4214 setup.id_res_x, setup.id_res_y, setup.id_res_z, id_1
4215 );
4216 pop_output(ctx);
4217
4218 spv_assign_destarg(ctx, result);
4219 } // emit_SPIRV_TEXM3X3
4220
4221 void emit_SPIRV_TEXM3X3TEX(Context *ctx)
4222 {
4223 if (ctx->texm3x3pad_src1 == -1)
4224 return;
4225
4226 RegisterList *pSReg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, ctx->dest_arg.regnum);
4227
4228 // texture{ttypestr}({sampler},
4229 // vec3(
4230 // dot({src0}.xyz, {src1}.xyz),
4231 // dot({src2}.xyz, {src3}.xyz),
4232 // dot({dst}.xyz, {src4}.xyz)
4233 // ),
4234 // )
4235
4236 SpirvResult sampler = spv_loadreg(ctx, pSReg);
4237
4238 SpirvTexm3x3SetupResult setup = spv_texm3x3_setup(ctx);
4239
4240 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
4241 uint32 tid_vec4 = spv_get_type(ctx, STI_VEC4);
4242 uint32 id_tc = spv_bumpid(ctx);
4243
4244 SpirvResult result;
4245 result.tid = tid_vec4;
4246 result.id = spv_bumpid(ctx);
4247
4248 push_output(ctx, &ctx->mainline);
4249 spv_emit(ctx, 3 + 3, SpvOpCompositeConstruct, tid_vec3, id_tc,
4250 setup.id_res_x, setup.id_res_y, setup.id_res_z
4251 );
4252 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id, sampler.id, id_tc);
4253 pop_output(ctx);
4254
4255 spv_assign_destarg(ctx, result);
4256 } // emit_SPIRV_TEXM3X3TEX
4257
4258 void emit_SPIRV_TEXM3X3SPEC(Context *ctx)
4259 {
4260 if (ctx->texm3x3pad_src1 == -1)
4261 return;
4262
4263 RegisterList *pSReg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, ctx->dest_arg.regnum);
4264 RegisterList *pSrc5 = spv_getreg(ctx, ctx->source_args[1].regtype, ctx->source_args[1].regnum);
4265
4266 SpirvTexm3x3SetupResult setup = spv_texm3x3_setup(ctx);
4267
4268 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
4269
4270 push_output(ctx, &ctx->mainline);
4271
4272 uint32 id_normal = spv_bumpid(ctx);
4273 spv_emit(ctx, 3 + 3, SpvOpCompositeConstruct, tid_vec3, id_normal,
4274 setup.id_res_x, setup.id_res_y, setup.id_res_z
4275 );
4276
4277 SpirvResult src5 = spv_loadreg(ctx, pSrc5);
4278
4279 uint32 id_eyeray = spv_bumpid(ctx);
4280 spv_emit(ctx, 5 + 3, SpvOpVectorShuffle, tid_vec3, id_eyeray, src5.id, src5.id, 0, 1, 2);
4281
4282 uint32 id_reflected = spv_reflect(ctx, id_normal, id_eyeray);
4283
4284 SpirvResult sampler = spv_loadreg(ctx, pSReg);
4285
4286 SpirvResult result;
4287 result.tid = spv_get_type(ctx, STI_VEC4);
4288 result.id = spv_bumpid(ctx);
4289 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id, sampler.id, id_reflected);
4290
4291 pop_output(ctx);
4292
4293 spv_assign_destarg(ctx, result);
4294 } // emit_SPIRV_TEXM3X3SPEC
4295
4296 void emit_SPIRV_TEXM3X3VSPEC(Context *ctx)
4297 {
4298 if (ctx->texm3x3pad_src1 == -1)
4299 return;
4300
4301 RegisterList *pSReg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, ctx->dest_arg.regnum);
4302
4303 SpirvTexm3x3SetupResult setup = spv_texm3x3_setup(ctx);
4304
4305 uint32 tid_float = spv_get_type(ctx, STI_FLOAT);
4306 uint32 tid_vec3 = spv_get_type(ctx, STI_VEC3);
4307
4308 push_output(ctx, &ctx->mainline);
4309
4310 uint32 id_normal = spv_bumpid(ctx);
4311 spv_emit(ctx, 3 + 3, SpvOpCompositeConstruct, tid_vec3, id_normal,
4312 setup.id_res_x, setup.id_res_y, setup.id_res_z
4313 );
4314
4315 uint32 id_eyeray_x = spv_bumpid(ctx);
4316 uint32 id_eyeray_y = spv_bumpid(ctx);
4317 uint32 id_eyeray_z = spv_bumpid(ctx);
4318 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_eyeray_x, setup.id_dst_pad0, 3);
4319 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_eyeray_y, setup.id_dst_pad1, 3);
4320 spv_emit(ctx, 5, SpvOpCompositeExtract, tid_float, id_eyeray_z, setup.id_dst, 3);
4321
4322 uint32 id_eyeray = spv_bumpid(ctx);
4323 spv_emit(ctx, 3 + 3, SpvOpCompositeConstruct, tid_vec3, id_eyeray,
4324 id_eyeray_x, id_eyeray_y, id_eyeray_z
4325 );
4326
4327 uint32 id_reflected = spv_reflect(ctx, id_normal, id_eyeray);
4328
4329 SpirvResult sampler = spv_loadreg(ctx, pSReg);
4330
4331 SpirvResult result;
4332 result.tid = spv_get_type(ctx, STI_VEC4);
4333 result.id = spv_bumpid(ctx);
4334 spv_emit(ctx, 5, SpvOpImageSampleImplicitLod, result.tid, result.id, sampler.id, id_reflected);
4335
4336 pop_output(ctx);
4337
4338 spv_assign_destarg(ctx, result);
4339 } // emit_SPIRV_TEXM3X3VSPEC
4340
4341 void emit_SPIRV_TEXBEM(Context *ctx)
4342 {
4343 spv_texbem(ctx, 0);
4344 } // emit_SPIRV_TEXBEM
4345
4346 void emit_SPIRV_TEXBEML(Context *ctx)
4347 {
4348 spv_texbem(ctx, 1);
4349 } // emit_SPIRV_TEXBEML
4350
4351 void emit_SPIRV_EXPP(Context *ctx)
4352 {
4353 // !!! FIXME: msdn's asm docs don't list this opcode, I'll have to check the driver documentation.
4354 emit_SPIRV_EXP(ctx); // I guess this is just partial precision EXP?
4355 } // emit_SPIRV_EXPP
4356
4357 void emit_SPIRV_LOGP(Context *ctx)
4358 {
4359 // LOGP is just low-precision LOG, but we'll take the higher precision.
4360 emit_SPIRV_LOG(ctx);
4361 } // emit_SPIRV_LOGP
4362
4363 void emit_SPIRV_DSX(Context *ctx)
4364 {
4365 SpirvResult dst, src;
4366 spv_emit_begin_ds(ctx, &dst, &src);
4367 spv_emit(ctx, 4, SpvOpDPdx, dst.tid, dst.id, src.id);
4368 spv_emit_end(ctx, dst);
4369 } // emit_SPIRV_DSX
4370
4371 void emit_SPIRV_DSY(Context *ctx)
4372 {
4373 SpirvResult dst, src;
4374 spv_emit_begin_ds(ctx, &dst, &src);
4375 spv_emit(ctx, 4, SpvOpDPdy, dst.tid, dst.id, src.id);
4376 spv_emit_end(ctx, dst);
4377 } // emit_SPIRV_DSY
4378
4379 void emit_SPIRV_RESERVED(Context *ctx)
4380 {
4381 // do nothing; fails in the state machine.
4382 } // emit_SPIRV_RESERVED
4383
4384 // !!! FIXME: The following are unimplemented even in the GLSL emitter.
4385 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD)
4386 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR)
4387 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB)
4388 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB)
4389 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX)
4390 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH)
4391 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3)
4392 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH)
4393 EMIT_SPIRV_OPCODE_UNIMPLEMENTED_FUNC(BEM)
4394
4395 #endif // SUPPORT_PROFILE_SPIRV
4396
4397 #pragma GCC visibility pop