zink/spirv: implement ball_iequal[2-4]
[mesa.git] / src / gallium / drivers / zink / nir_to_spirv / nir_to_spirv.c
1 /*
2 * Copyright 2018 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "nir_to_spirv.h"
25 #include "spirv_builder.h"
26
27 #include "nir.h"
28 #include "pipe/p_state.h"
29 #include "util/u_memory.h"
30 #include "util/hash_table.h"
31
32 struct ntv_context {
33 struct spirv_builder builder;
34
35 SpvId GLSL_std_450;
36
37 gl_shader_stage stage;
38
39 SpvId ubos[128];
40 size_t num_ubos;
41 SpvId samplers[PIPE_MAX_SAMPLERS];
42 size_t num_samplers;
43 SpvId entry_ifaces[PIPE_MAX_SHADER_INPUTS * 4 + PIPE_MAX_SHADER_OUTPUTS * 4];
44 size_t num_entry_ifaces;
45
46 SpvId *defs;
47 size_t num_defs;
48
49 SpvId *regs;
50 size_t num_regs;
51
52 struct hash_table *vars; /* nir_variable -> SpvId */
53
54 const SpvId *block_ids;
55 size_t num_blocks;
56 bool block_started;
57 SpvId loop_break, loop_cont;
58
59 SpvId front_face_var;
60 };
61
62 static SpvId
63 get_fvec_constant(struct ntv_context *ctx, unsigned bit_size,
64 unsigned num_components, float value);
65
66 static SpvId
67 get_uvec_constant(struct ntv_context *ctx, unsigned bit_size,
68 unsigned num_components, uint32_t value);
69
70 static SpvId
71 get_ivec_constant(struct ntv_context *ctx, unsigned bit_size,
72 unsigned num_components, int32_t value);
73
74 static SpvId
75 emit_unop(struct ntv_context *ctx, SpvOp op, SpvId type, SpvId src);
76
77 static SpvId
78 emit_binop(struct ntv_context *ctx, SpvOp op, SpvId type,
79 SpvId src0, SpvId src1);
80
81 static SpvId
82 emit_triop(struct ntv_context *ctx, SpvOp op, SpvId type,
83 SpvId src0, SpvId src1, SpvId src2);
84
85 static SpvId
86 get_bvec_type(struct ntv_context *ctx, int num_components)
87 {
88 SpvId bool_type = spirv_builder_type_bool(&ctx->builder);
89 if (num_components > 1)
90 return spirv_builder_type_vector(&ctx->builder, bool_type,
91 num_components);
92
93 assert(num_components == 1);
94 return bool_type;
95 }
96
97 static SpvId
98 block_label(struct ntv_context *ctx, nir_block *block)
99 {
100 assert(block->index < ctx->num_blocks);
101 return ctx->block_ids[block->index];
102 }
103
104 static SpvId
105 emit_float_const(struct ntv_context *ctx, int bit_size, float value)
106 {
107 assert(bit_size == 32);
108 return spirv_builder_const_float(&ctx->builder, bit_size, value);
109 }
110
111 static SpvId
112 emit_uint_const(struct ntv_context *ctx, int bit_size, uint32_t value)
113 {
114 assert(bit_size == 32);
115 return spirv_builder_const_uint(&ctx->builder, bit_size, value);
116 }
117
118 static SpvId
119 emit_int_const(struct ntv_context *ctx, int bit_size, int32_t value)
120 {
121 assert(bit_size == 32);
122 return spirv_builder_const_int(&ctx->builder, bit_size, value);
123 }
124
125 static SpvId
126 get_fvec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
127 {
128 assert(bit_size == 32); // only 32-bit floats supported so far
129
130 SpvId float_type = spirv_builder_type_float(&ctx->builder, bit_size);
131 if (num_components > 1)
132 return spirv_builder_type_vector(&ctx->builder, float_type,
133 num_components);
134
135 assert(num_components == 1);
136 return float_type;
137 }
138
139 static SpvId
140 get_ivec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
141 {
142 assert(bit_size == 1 || bit_size == 32); // only 32-bit ints supported so far
143
144 SpvId int_type = spirv_builder_type_int(&ctx->builder, MAX2(bit_size, 32));
145 if (num_components > 1)
146 return spirv_builder_type_vector(&ctx->builder, int_type,
147 num_components);
148
149 assert(num_components == 1);
150 return int_type;
151 }
152
153 static SpvId
154 get_uvec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
155 {
156 assert(bit_size == 1 || bit_size == 32); // only 32-bit uints supported so far
157
158 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, MAX2(bit_size, 32));
159 if (num_components > 1)
160 return spirv_builder_type_vector(&ctx->builder, uint_type,
161 num_components);
162
163 assert(num_components == 1);
164 return uint_type;
165 }
166
167 static SpvId
168 get_dest_uvec_type(struct ntv_context *ctx, nir_dest *dest)
169 {
170 return get_uvec_type(ctx, nir_dest_bit_size(*dest),
171 nir_dest_num_components(*dest));
172 }
173
174 static SpvId
175 get_glsl_basetype(struct ntv_context *ctx, enum glsl_base_type type)
176 {
177 switch (type) {
178 case GLSL_TYPE_BOOL:
179 return spirv_builder_type_bool(&ctx->builder);
180
181 case GLSL_TYPE_FLOAT:
182 return spirv_builder_type_float(&ctx->builder, 32);
183
184 case GLSL_TYPE_INT:
185 return spirv_builder_type_int(&ctx->builder, 32);
186
187 case GLSL_TYPE_UINT:
188 return spirv_builder_type_uint(&ctx->builder, 32);
189 /* TODO: handle more types */
190
191 default:
192 unreachable("unknown GLSL type");
193 }
194 }
195
196 static SpvId
197 get_glsl_type(struct ntv_context *ctx, const struct glsl_type *type)
198 {
199 assert(type);
200 if (glsl_type_is_scalar(type))
201 return get_glsl_basetype(ctx, glsl_get_base_type(type));
202
203 if (glsl_type_is_vector(type))
204 return spirv_builder_type_vector(&ctx->builder,
205 get_glsl_basetype(ctx, glsl_get_base_type(type)),
206 glsl_get_vector_elements(type));
207
208 if (glsl_type_is_array(type)) {
209 SpvId ret = spirv_builder_type_array(&ctx->builder,
210 get_glsl_type(ctx, glsl_get_array_element(type)),
211 emit_uint_const(ctx, 32, glsl_get_length(type)));
212 uint32_t stride = glsl_get_explicit_stride(type);
213 if (stride)
214 spirv_builder_emit_array_stride(&ctx->builder, ret, stride);
215 return ret;
216 }
217
218
219 unreachable("we shouldn't get here, I think...");
220 }
221
222 static void
223 emit_input(struct ntv_context *ctx, struct nir_variable *var)
224 {
225 SpvId var_type = get_glsl_type(ctx, var->type);
226 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
227 SpvStorageClassInput,
228 var_type);
229 SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
230 SpvStorageClassInput);
231
232 if (var->name)
233 spirv_builder_emit_name(&ctx->builder, var_id, var->name);
234
235 if (ctx->stage == MESA_SHADER_FRAGMENT) {
236 if (var->data.location >= VARYING_SLOT_VAR0)
237 spirv_builder_emit_location(&ctx->builder, var_id,
238 var->data.location -
239 VARYING_SLOT_VAR0 +
240 VARYING_SLOT_TEX0);
241 else if ((var->data.location >= VARYING_SLOT_COL0 &&
242 var->data.location <= VARYING_SLOT_TEX7) ||
243 var->data.location == VARYING_SLOT_BFC0 ||
244 var->data.location == VARYING_SLOT_BFC1) {
245 spirv_builder_emit_location(&ctx->builder, var_id,
246 var->data.location);
247 } else {
248 switch (var->data.location) {
249 case VARYING_SLOT_POS:
250 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInFragCoord);
251 break;
252
253 case VARYING_SLOT_PNTC:
254 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInPointCoord);
255 break;
256
257 default:
258 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var->data.location));
259 unreachable("unexpected varying slot");
260 }
261 }
262 } else {
263 spirv_builder_emit_location(&ctx->builder, var_id,
264 var->data.driver_location);
265 }
266
267 if (var->data.location_frac)
268 spirv_builder_emit_component(&ctx->builder, var_id,
269 var->data.location_frac);
270
271 if (var->data.interpolation == INTERP_MODE_FLAT)
272 spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationFlat);
273
274 _mesa_hash_table_insert(ctx->vars, var, (void *)(intptr_t)var_id);
275
276 assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
277 ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
278 }
279
280 static void
281 emit_output(struct ntv_context *ctx, struct nir_variable *var)
282 {
283 SpvId var_type = get_glsl_type(ctx, var->type);
284 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
285 SpvStorageClassOutput,
286 var_type);
287 SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
288 SpvStorageClassOutput);
289 if (var->name)
290 spirv_builder_emit_name(&ctx->builder, var_id, var->name);
291
292
293 if (ctx->stage == MESA_SHADER_VERTEX) {
294 if (var->data.location >= VARYING_SLOT_VAR0)
295 spirv_builder_emit_location(&ctx->builder, var_id,
296 var->data.location -
297 VARYING_SLOT_VAR0 +
298 VARYING_SLOT_TEX0);
299 else if ((var->data.location >= VARYING_SLOT_COL0 &&
300 var->data.location <= VARYING_SLOT_TEX7) ||
301 var->data.location == VARYING_SLOT_BFC0 ||
302 var->data.location == VARYING_SLOT_BFC1) {
303 spirv_builder_emit_location(&ctx->builder, var_id,
304 var->data.location);
305 } else {
306 switch (var->data.location) {
307 case VARYING_SLOT_POS:
308 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInPosition);
309 break;
310
311 case VARYING_SLOT_PSIZ:
312 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInPointSize);
313 break;
314
315 case VARYING_SLOT_CLIP_DIST0:
316 assert(glsl_type_is_array(var->type));
317 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInClipDistance);
318 break;
319
320 default:
321 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var->data.location));
322 unreachable("unexpected varying slot");
323 }
324 }
325 } else if (ctx->stage == MESA_SHADER_FRAGMENT) {
326 if (var->data.location >= FRAG_RESULT_DATA0)
327 spirv_builder_emit_location(&ctx->builder, var_id,
328 var->data.location - FRAG_RESULT_DATA0);
329 else {
330 switch (var->data.location) {
331 case FRAG_RESULT_COLOR:
332 spirv_builder_emit_location(&ctx->builder, var_id, 0);
333 break;
334
335 case FRAG_RESULT_DEPTH:
336 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInFragDepth);
337 break;
338
339 default:
340 spirv_builder_emit_location(&ctx->builder, var_id,
341 var->data.driver_location);
342 }
343 }
344 }
345
346 if (var->data.location_frac)
347 spirv_builder_emit_component(&ctx->builder, var_id,
348 var->data.location_frac);
349
350 _mesa_hash_table_insert(ctx->vars, var, (void *)(intptr_t)var_id);
351
352 assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
353 ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
354 }
355
356 static SpvDim
357 type_to_dim(enum glsl_sampler_dim gdim, bool *is_ms)
358 {
359 *is_ms = false;
360 switch (gdim) {
361 case GLSL_SAMPLER_DIM_1D:
362 return SpvDim1D;
363 case GLSL_SAMPLER_DIM_2D:
364 return SpvDim2D;
365 case GLSL_SAMPLER_DIM_3D:
366 return SpvDim3D;
367 case GLSL_SAMPLER_DIM_CUBE:
368 return SpvDimCube;
369 case GLSL_SAMPLER_DIM_RECT:
370 return SpvDimRect;
371 case GLSL_SAMPLER_DIM_BUF:
372 return SpvDimBuffer;
373 case GLSL_SAMPLER_DIM_EXTERNAL:
374 return SpvDim2D; /* seems dodgy... */
375 case GLSL_SAMPLER_DIM_MS:
376 *is_ms = true;
377 return SpvDim2D;
378 default:
379 fprintf(stderr, "unknown sampler type %d\n", gdim);
380 break;
381 }
382 return SpvDim2D;
383 }
384
385 static void
386 emit_sampler(struct ntv_context *ctx, struct nir_variable *var)
387 {
388 bool is_ms;
389 SpvDim dimension = type_to_dim(glsl_get_sampler_dim(var->type), &is_ms);
390 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
391 SpvId image_type = spirv_builder_type_image(&ctx->builder, float_type,
392 dimension, false, glsl_sampler_type_is_array(var->type), is_ms, 1,
393 SpvImageFormatUnknown);
394
395 SpvId sampled_type = spirv_builder_type_sampled_image(&ctx->builder,
396 image_type);
397 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
398 SpvStorageClassUniformConstant,
399 sampled_type);
400 SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
401 SpvStorageClassUniformConstant);
402
403 if (var->name)
404 spirv_builder_emit_name(&ctx->builder, var_id, var->name);
405
406 assert(ctx->num_samplers < ARRAY_SIZE(ctx->samplers));
407 ctx->samplers[ctx->num_samplers++] = var_id;
408
409 spirv_builder_emit_descriptor_set(&ctx->builder, var_id,
410 var->data.descriptor_set);
411 spirv_builder_emit_binding(&ctx->builder, var_id, var->data.binding);
412 }
413
414 static void
415 emit_ubo(struct ntv_context *ctx, struct nir_variable *var)
416 {
417 uint32_t size = glsl_count_attribute_slots(var->type, false);
418 SpvId vec4_type = get_uvec_type(ctx, 32, 4);
419 SpvId array_length = emit_uint_const(ctx, 32, size);
420 SpvId array_type = spirv_builder_type_array(&ctx->builder, vec4_type,
421 array_length);
422 spirv_builder_emit_array_stride(&ctx->builder, array_type, 16);
423
424 // wrap UBO-array in a struct
425 SpvId struct_type = spirv_builder_type_struct(&ctx->builder, &array_type, 1);
426 if (var->name) {
427 char struct_name[100];
428 snprintf(struct_name, sizeof(struct_name), "struct_%s", var->name);
429 spirv_builder_emit_name(&ctx->builder, struct_type, struct_name);
430 }
431
432 spirv_builder_emit_decoration(&ctx->builder, struct_type,
433 SpvDecorationBlock);
434 spirv_builder_emit_member_offset(&ctx->builder, struct_type, 0, 0);
435
436
437 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
438 SpvStorageClassUniform,
439 struct_type);
440
441 SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
442 SpvStorageClassUniform);
443 if (var->name)
444 spirv_builder_emit_name(&ctx->builder, var_id, var->name);
445
446 assert(ctx->num_ubos < ARRAY_SIZE(ctx->ubos));
447 ctx->ubos[ctx->num_ubos++] = var_id;
448
449 spirv_builder_emit_descriptor_set(&ctx->builder, var_id,
450 var->data.descriptor_set);
451 spirv_builder_emit_binding(&ctx->builder, var_id, var->data.binding);
452 }
453
454 static void
455 emit_uniform(struct ntv_context *ctx, struct nir_variable *var)
456 {
457 if (var->data.mode == nir_var_mem_ubo)
458 emit_ubo(ctx, var);
459 else {
460 assert(var->data.mode == nir_var_uniform);
461 if (glsl_type_is_sampler(var->type))
462 emit_sampler(ctx, var);
463 }
464 }
465
466 static SpvId
467 get_src_uint_ssa(struct ntv_context *ctx, const nir_ssa_def *ssa)
468 {
469 assert(ssa->index < ctx->num_defs);
470 assert(ctx->defs[ssa->index] != 0);
471 return ctx->defs[ssa->index];
472 }
473
474 static SpvId
475 get_var_from_reg(struct ntv_context *ctx, nir_register *reg)
476 {
477 assert(reg->index < ctx->num_regs);
478 assert(ctx->regs[reg->index] != 0);
479 return ctx->regs[reg->index];
480 }
481
482 static SpvId
483 get_src_uint_reg(struct ntv_context *ctx, const nir_reg_src *reg)
484 {
485 assert(reg->reg);
486 assert(!reg->indirect);
487 assert(!reg->base_offset);
488
489 SpvId var = get_var_from_reg(ctx, reg->reg);
490 SpvId type = get_uvec_type(ctx, reg->reg->bit_size, reg->reg->num_components);
491 return spirv_builder_emit_load(&ctx->builder, type, var);
492 }
493
494 static SpvId
495 get_src_uint(struct ntv_context *ctx, nir_src *src)
496 {
497 if (src->is_ssa)
498 return get_src_uint_ssa(ctx, src->ssa);
499 else
500 return get_src_uint_reg(ctx, &src->reg);
501 }
502
503 static SpvId
504 get_alu_src_uint(struct ntv_context *ctx, nir_alu_instr *alu, unsigned src)
505 {
506 assert(!alu->src[src].negate);
507 assert(!alu->src[src].abs);
508
509 SpvId def = get_src_uint(ctx, &alu->src[src].src);
510
511 unsigned used_channels = 0;
512 bool need_swizzle = false;
513 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
514 if (!nir_alu_instr_channel_used(alu, src, i))
515 continue;
516
517 used_channels++;
518
519 if (alu->src[src].swizzle[i] != i)
520 need_swizzle = true;
521 }
522 assert(used_channels != 0);
523
524 unsigned live_channels = nir_src_num_components(alu->src[src].src);
525 if (used_channels != live_channels)
526 need_swizzle = true;
527
528 if (!need_swizzle)
529 return def;
530
531 int bit_size = nir_src_bit_size(alu->src[src].src);
532 assert(bit_size == 1 || bit_size == 32);
533
534 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, MAX2(bit_size, 32));
535 if (used_channels == 1) {
536 uint32_t indices[] = { alu->src[src].swizzle[0] };
537 return spirv_builder_emit_composite_extract(&ctx->builder, uint_type,
538 def, indices,
539 ARRAY_SIZE(indices));
540 } else if (live_channels == 1) {
541 SpvId uvec_type = spirv_builder_type_vector(&ctx->builder, uint_type,
542 used_channels);
543
544 SpvId constituents[NIR_MAX_VEC_COMPONENTS];
545 for (unsigned i = 0; i < used_channels; ++i)
546 constituents[i] = def;
547
548 return spirv_builder_emit_composite_construct(&ctx->builder, uvec_type,
549 constituents,
550 used_channels);
551 } else {
552 SpvId uvec_type = spirv_builder_type_vector(&ctx->builder, uint_type,
553 used_channels);
554
555 uint32_t components[NIR_MAX_VEC_COMPONENTS];
556 size_t num_components = 0;
557 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
558 if (!nir_alu_instr_channel_used(alu, src, i))
559 continue;
560
561 components[num_components++] = alu->src[src].swizzle[i];
562 }
563
564 return spirv_builder_emit_vector_shuffle(&ctx->builder, uvec_type,
565 def, def, components, num_components);
566 }
567 }
568
569 static void
570 store_ssa_def_uint(struct ntv_context *ctx, nir_ssa_def *ssa, SpvId result)
571 {
572 assert(result != 0);
573 assert(ssa->index < ctx->num_defs);
574 ctx->defs[ssa->index] = result;
575 }
576
577 static SpvId
578 emit_select(struct ntv_context *ctx, SpvId type, SpvId cond,
579 SpvId if_true, SpvId if_false)
580 {
581 return emit_triop(ctx, SpvOpSelect, type, cond, if_true, if_false);
582 }
583
584 static SpvId
585 bvec_to_uvec(struct ntv_context *ctx, SpvId value, unsigned num_components)
586 {
587 SpvId otype = get_uvec_type(ctx, 32, num_components);
588 SpvId zero = get_uvec_constant(ctx, 32, num_components, 0);
589 SpvId one = get_uvec_constant(ctx, 32, num_components, UINT32_MAX);
590 return emit_select(ctx, otype, value, one, zero);
591 }
592
593 static SpvId
594 uvec_to_bvec(struct ntv_context *ctx, SpvId value, unsigned num_components)
595 {
596 SpvId type = get_bvec_type(ctx, num_components);
597 SpvId zero = get_uvec_constant(ctx, 32, num_components, 0);
598 return emit_binop(ctx, SpvOpINotEqual, type, value, zero);
599 }
600
601 static SpvId
602 emit_bitcast(struct ntv_context *ctx, SpvId type, SpvId value)
603 {
604 return emit_unop(ctx, SpvOpBitcast, type, value);
605 }
606
607 static SpvId
608 bitcast_to_uvec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
609 unsigned num_components)
610 {
611 SpvId type = get_uvec_type(ctx, bit_size, num_components);
612 return emit_bitcast(ctx, type, value);
613 }
614
615 static SpvId
616 bitcast_to_ivec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
617 unsigned num_components)
618 {
619 SpvId type = get_ivec_type(ctx, bit_size, num_components);
620 return emit_bitcast(ctx, type, value);
621 }
622
623 static SpvId
624 bitcast_to_fvec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
625 unsigned num_components)
626 {
627 SpvId type = get_fvec_type(ctx, bit_size, num_components);
628 return emit_bitcast(ctx, type, value);
629 }
630
631 static void
632 store_reg_def(struct ntv_context *ctx, nir_reg_dest *reg, SpvId result)
633 {
634 SpvId var = get_var_from_reg(ctx, reg->reg);
635 assert(var);
636 spirv_builder_emit_store(&ctx->builder, var, result);
637 }
638
639 static void
640 store_dest_uint(struct ntv_context *ctx, nir_dest *dest, SpvId result)
641 {
642 if (dest->is_ssa)
643 store_ssa_def_uint(ctx, &dest->ssa, result);
644 else
645 store_reg_def(ctx, &dest->reg, result);
646 }
647
648 static void
649 store_dest(struct ntv_context *ctx, nir_dest *dest, SpvId result, nir_alu_type type)
650 {
651 unsigned num_components = nir_dest_num_components(*dest);
652 unsigned bit_size = nir_dest_bit_size(*dest);
653
654 switch (nir_alu_type_get_base_type(type)) {
655 case nir_type_bool:
656 assert(bit_size == 1);
657 result = bvec_to_uvec(ctx, result, num_components);
658 break;
659
660 case nir_type_uint:
661 break; /* nothing to do! */
662
663 case nir_type_int:
664 case nir_type_float:
665 result = bitcast_to_uvec(ctx, result, bit_size, num_components);
666 break;
667
668 default:
669 unreachable("unsupported nir_alu_type");
670 }
671
672 store_dest_uint(ctx, dest, result);
673 }
674
675 static SpvId
676 emit_unop(struct ntv_context *ctx, SpvOp op, SpvId type, SpvId src)
677 {
678 return spirv_builder_emit_unop(&ctx->builder, op, type, src);
679 }
680
681 static SpvId
682 emit_binop(struct ntv_context *ctx, SpvOp op, SpvId type,
683 SpvId src0, SpvId src1)
684 {
685 return spirv_builder_emit_binop(&ctx->builder, op, type, src0, src1);
686 }
687
688 static SpvId
689 emit_triop(struct ntv_context *ctx, SpvOp op, SpvId type,
690 SpvId src0, SpvId src1, SpvId src2)
691 {
692 return spirv_builder_emit_triop(&ctx->builder, op, type, src0, src1, src2);
693 }
694
695 static SpvId
696 emit_builtin_unop(struct ntv_context *ctx, enum GLSLstd450 op, SpvId type,
697 SpvId src)
698 {
699 SpvId args[] = { src };
700 return spirv_builder_emit_ext_inst(&ctx->builder, type, ctx->GLSL_std_450,
701 op, args, ARRAY_SIZE(args));
702 }
703
704 static SpvId
705 emit_builtin_binop(struct ntv_context *ctx, enum GLSLstd450 op, SpvId type,
706 SpvId src0, SpvId src1)
707 {
708 SpvId args[] = { src0, src1 };
709 return spirv_builder_emit_ext_inst(&ctx->builder, type, ctx->GLSL_std_450,
710 op, args, ARRAY_SIZE(args));
711 }
712
713 static SpvId
714 get_fvec_constant(struct ntv_context *ctx, unsigned bit_size,
715 unsigned num_components, float value)
716 {
717 assert(bit_size == 32);
718
719 SpvId result = emit_float_const(ctx, bit_size, value);
720 if (num_components == 1)
721 return result;
722
723 assert(num_components > 1);
724 SpvId components[num_components];
725 for (int i = 0; i < num_components; i++)
726 components[i] = result;
727
728 SpvId type = get_fvec_type(ctx, bit_size, num_components);
729 return spirv_builder_const_composite(&ctx->builder, type, components,
730 num_components);
731 }
732
733 static SpvId
734 get_uvec_constant(struct ntv_context *ctx, unsigned bit_size,
735 unsigned num_components, uint32_t value)
736 {
737 assert(bit_size == 32);
738
739 SpvId result = emit_uint_const(ctx, bit_size, value);
740 if (num_components == 1)
741 return result;
742
743 assert(num_components > 1);
744 SpvId components[num_components];
745 for (int i = 0; i < num_components; i++)
746 components[i] = result;
747
748 SpvId type = get_uvec_type(ctx, bit_size, num_components);
749 return spirv_builder_const_composite(&ctx->builder, type, components,
750 num_components);
751 }
752
753 static SpvId
754 get_ivec_constant(struct ntv_context *ctx, unsigned bit_size,
755 unsigned num_components, int32_t value)
756 {
757 assert(bit_size == 32);
758
759 SpvId result = emit_int_const(ctx, bit_size, value);
760 if (num_components == 1)
761 return result;
762
763 assert(num_components > 1);
764 SpvId components[num_components];
765 for (int i = 0; i < num_components; i++)
766 components[i] = result;
767
768 SpvId type = get_ivec_type(ctx, bit_size, num_components);
769 return spirv_builder_const_composite(&ctx->builder, type, components,
770 num_components);
771 }
772
773 static inline unsigned
774 alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
775 {
776 if (nir_op_infos[instr->op].input_sizes[src] > 0)
777 return nir_op_infos[instr->op].input_sizes[src];
778
779 if (instr->dest.dest.is_ssa)
780 return instr->dest.dest.ssa.num_components;
781 else
782 return instr->dest.dest.reg.reg->num_components;
783 }
784
785 static SpvId
786 get_alu_src(struct ntv_context *ctx, nir_alu_instr *alu, unsigned src)
787 {
788 SpvId uint_value = get_alu_src_uint(ctx, alu, src);
789
790 unsigned num_components = alu_instr_src_components(alu, src);
791 unsigned bit_size = nir_src_bit_size(alu->src[src].src);
792 nir_alu_type type = nir_op_infos[alu->op].input_types[src];
793
794 switch (nir_alu_type_get_base_type(type)) {
795 case nir_type_bool:
796 assert(bit_size == 1);
797 return uvec_to_bvec(ctx, uint_value, num_components);
798
799 case nir_type_int:
800 return bitcast_to_ivec(ctx, uint_value, bit_size, num_components);
801
802 case nir_type_uint:
803 return uint_value;
804
805 case nir_type_float:
806 return bitcast_to_fvec(ctx, uint_value, bit_size, num_components);
807
808 default:
809 unreachable("unknown nir_alu_type");
810 }
811 }
812
813 static void
814 store_alu_result(struct ntv_context *ctx, nir_alu_instr *alu, SpvId result)
815 {
816 assert(!alu->dest.saturate);
817 return store_dest(ctx, &alu->dest.dest, result, nir_op_infos[alu->op].output_type);
818 }
819
820 static SpvId
821 get_dest_type(struct ntv_context *ctx, nir_dest *dest, nir_alu_type type)
822 {
823 unsigned num_components = nir_dest_num_components(*dest);
824 unsigned bit_size = nir_dest_bit_size(*dest);
825
826 switch (nir_alu_type_get_base_type(type)) {
827 case nir_type_bool:
828 return get_bvec_type(ctx, num_components);
829
830 case nir_type_int:
831 return get_ivec_type(ctx, bit_size, num_components);
832
833 case nir_type_uint:
834 return get_uvec_type(ctx, bit_size, num_components);
835
836 case nir_type_float:
837 return get_fvec_type(ctx, bit_size, num_components);
838
839 default:
840 unreachable("unsupported nir_alu_type");
841 }
842 }
843
844 static void
845 emit_alu(struct ntv_context *ctx, nir_alu_instr *alu)
846 {
847 SpvId src[nir_op_infos[alu->op].num_inputs];
848 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++)
849 src[i] = get_alu_src(ctx, alu, i);
850
851 SpvId dest_type = get_dest_type(ctx, &alu->dest.dest,
852 nir_op_infos[alu->op].output_type);
853 unsigned bit_size = nir_dest_bit_size(alu->dest.dest);
854 unsigned num_components = nir_dest_num_components(alu->dest.dest);
855
856 SpvId result = 0;
857 switch (alu->op) {
858 case nir_op_mov:
859 assert(nir_op_infos[alu->op].num_inputs == 1);
860 result = src[0];
861 break;
862
863 #define UNOP(nir_op, spirv_op) \
864 case nir_op: \
865 assert(nir_op_infos[alu->op].num_inputs == 1); \
866 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
867 break;
868
869 UNOP(nir_op_ineg, SpvOpSNegate)
870 UNOP(nir_op_fneg, SpvOpFNegate)
871 UNOP(nir_op_fddx, SpvOpDPdx)
872 UNOP(nir_op_fddy, SpvOpDPdy)
873 UNOP(nir_op_f2i32, SpvOpConvertFToS)
874 UNOP(nir_op_f2u32, SpvOpConvertFToU)
875 UNOP(nir_op_i2f32, SpvOpConvertSToF)
876 UNOP(nir_op_u2f32, SpvOpConvertUToF)
877 UNOP(nir_op_inot, SpvOpNot)
878 #undef UNOP
879
880 case nir_op_b2i32:
881 assert(nir_op_infos[alu->op].num_inputs == 1);
882 result = emit_select(ctx, dest_type, src[0],
883 get_ivec_constant(ctx, 32, num_components, 1),
884 get_ivec_constant(ctx, 32, num_components, 0));
885 break;
886
887 case nir_op_b2f32:
888 assert(nir_op_infos[alu->op].num_inputs == 1);
889 result = emit_select(ctx, dest_type, src[0],
890 get_fvec_constant(ctx, 32, num_components, 1),
891 get_fvec_constant(ctx, 32, num_components, 0));
892 break;
893
894 #define BUILTIN_UNOP(nir_op, spirv_op) \
895 case nir_op: \
896 assert(nir_op_infos[alu->op].num_inputs == 1); \
897 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
898 break;
899
900 BUILTIN_UNOP(nir_op_fabs, GLSLstd450FAbs)
901 BUILTIN_UNOP(nir_op_fsqrt, GLSLstd450Sqrt)
902 BUILTIN_UNOP(nir_op_frsq, GLSLstd450InverseSqrt)
903 BUILTIN_UNOP(nir_op_flog2, GLSLstd450Log2)
904 BUILTIN_UNOP(nir_op_fexp2, GLSLstd450Exp2)
905 BUILTIN_UNOP(nir_op_ffract, GLSLstd450Fract)
906 BUILTIN_UNOP(nir_op_ffloor, GLSLstd450Floor)
907 BUILTIN_UNOP(nir_op_fceil, GLSLstd450Ceil)
908 BUILTIN_UNOP(nir_op_ftrunc, GLSLstd450Trunc)
909 BUILTIN_UNOP(nir_op_fround_even, GLSLstd450RoundEven)
910 BUILTIN_UNOP(nir_op_fsign, GLSLstd450FSign)
911 BUILTIN_UNOP(nir_op_fsin, GLSLstd450Sin)
912 BUILTIN_UNOP(nir_op_fcos, GLSLstd450Cos)
913 #undef BUILTIN_UNOP
914
915 case nir_op_frcp:
916 assert(nir_op_infos[alu->op].num_inputs == 1);
917 result = emit_binop(ctx, SpvOpFDiv, dest_type,
918 get_fvec_constant(ctx, bit_size, num_components, 1),
919 src[0]);
920 break;
921
922 case nir_op_f2b1:
923 assert(nir_op_infos[alu->op].num_inputs == 1);
924 result = emit_binop(ctx, SpvOpFOrdNotEqual, dest_type, src[0],
925 get_fvec_constant(ctx,
926 nir_src_bit_size(alu->src[0].src),
927 num_components, 0));
928 break;
929
930
931 #define BINOP(nir_op, spirv_op) \
932 case nir_op: \
933 assert(nir_op_infos[alu->op].num_inputs == 2); \
934 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
935 break;
936
937 BINOP(nir_op_iadd, SpvOpIAdd)
938 BINOP(nir_op_isub, SpvOpISub)
939 BINOP(nir_op_imul, SpvOpIMul)
940 BINOP(nir_op_idiv, SpvOpSDiv)
941 BINOP(nir_op_udiv, SpvOpUDiv)
942 BINOP(nir_op_fadd, SpvOpFAdd)
943 BINOP(nir_op_fsub, SpvOpFSub)
944 BINOP(nir_op_fmul, SpvOpFMul)
945 BINOP(nir_op_fdiv, SpvOpFDiv)
946 BINOP(nir_op_fmod, SpvOpFMod)
947 BINOP(nir_op_ilt, SpvOpSLessThan)
948 BINOP(nir_op_ige, SpvOpSGreaterThanEqual)
949 BINOP(nir_op_ieq, SpvOpIEqual)
950 BINOP(nir_op_ine, SpvOpINotEqual)
951 BINOP(nir_op_flt, SpvOpFOrdLessThan)
952 BINOP(nir_op_fge, SpvOpFOrdGreaterThanEqual)
953 BINOP(nir_op_feq, SpvOpFOrdEqual)
954 BINOP(nir_op_fne, SpvOpFOrdNotEqual)
955 BINOP(nir_op_ishl, SpvOpShiftLeftLogical)
956 BINOP(nir_op_ishr, SpvOpShiftRightArithmetic)
957 BINOP(nir_op_ushr, SpvOpShiftRightLogical)
958 BINOP(nir_op_iand, SpvOpBitwiseAnd)
959 BINOP(nir_op_ior, SpvOpBitwiseOr)
960 #undef BINOP
961
962 #define BUILTIN_BINOP(nir_op, spirv_op) \
963 case nir_op: \
964 assert(nir_op_infos[alu->op].num_inputs == 2); \
965 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
966 break;
967
968 BUILTIN_BINOP(nir_op_fmin, GLSLstd450FMin)
969 BUILTIN_BINOP(nir_op_fmax, GLSLstd450FMax)
970 #undef BUILTIN_BINOP
971
972 case nir_op_fdot2:
973 case nir_op_fdot3:
974 case nir_op_fdot4:
975 assert(nir_op_infos[alu->op].num_inputs == 2);
976 result = emit_binop(ctx, SpvOpDot, dest_type, src[0], src[1]);
977 break;
978
979 case nir_op_seq:
980 case nir_op_sne:
981 case nir_op_slt:
982 case nir_op_sge: {
983 assert(nir_op_infos[alu->op].num_inputs == 2);
984 int num_components = nir_dest_num_components(alu->dest.dest);
985 SpvId bool_type = get_bvec_type(ctx, num_components);
986
987 SpvId zero = emit_float_const(ctx, bit_size, 0.0f);
988 SpvId one = emit_float_const(ctx, bit_size, 1.0f);
989 if (num_components > 1) {
990 SpvId zero_comps[num_components], one_comps[num_components];
991 for (int i = 0; i < num_components; i++) {
992 zero_comps[i] = zero;
993 one_comps[i] = one;
994 }
995
996 zero = spirv_builder_const_composite(&ctx->builder, dest_type,
997 zero_comps, num_components);
998 one = spirv_builder_const_composite(&ctx->builder, dest_type,
999 one_comps, num_components);
1000 }
1001
1002 SpvOp op;
1003 switch (alu->op) {
1004 case nir_op_seq: op = SpvOpFOrdEqual; break;
1005 case nir_op_sne: op = SpvOpFOrdNotEqual; break;
1006 case nir_op_slt: op = SpvOpFOrdLessThan; break;
1007 case nir_op_sge: op = SpvOpFOrdGreaterThanEqual; break;
1008 default: unreachable("unexpected op");
1009 }
1010
1011 result = emit_binop(ctx, op, bool_type, src[0], src[1]);
1012 result = emit_select(ctx, dest_type, result, one, zero);
1013 }
1014 break;
1015
1016 case nir_op_fcsel:
1017 result = emit_binop(ctx, SpvOpFOrdGreaterThan,
1018 get_bvec_type(ctx, num_components),
1019 src[0],
1020 get_fvec_constant(ctx,
1021 nir_src_bit_size(alu->src[0].src),
1022 num_components, 0));
1023 result = emit_select(ctx, dest_type, result, src[1], src[2]);
1024 break;
1025
1026 case nir_op_bcsel:
1027 assert(nir_op_infos[alu->op].num_inputs == 3);
1028 result = emit_select(ctx, dest_type, src[0], src[1], src[2]);
1029 break;
1030
1031 case nir_op_bany_fnequal2:
1032 case nir_op_bany_fnequal3:
1033 case nir_op_bany_fnequal4:
1034 assert(nir_op_infos[alu->op].num_inputs == 2);
1035 assert(alu_instr_src_components(alu, 0) ==
1036 alu_instr_src_components(alu, 1));
1037 result = emit_binop(ctx, SpvOpFOrdNotEqual,
1038 get_bvec_type(ctx, alu_instr_src_components(alu, 0)),
1039 src[0], src[1]);
1040 result = emit_unop(ctx, SpvOpAny, dest_type, result);
1041 break;
1042
1043 case nir_op_bany_inequal2:
1044 case nir_op_bany_inequal3:
1045 case nir_op_bany_inequal4:
1046 assert(nir_op_infos[alu->op].num_inputs == 2);
1047 assert(alu_instr_src_components(alu, 0) ==
1048 alu_instr_src_components(alu, 1));
1049 result = emit_binop(ctx, SpvOpINotEqual,
1050 get_bvec_type(ctx, alu_instr_src_components(alu, 0)),
1051 src[0], src[1]);
1052 result = emit_unop(ctx, SpvOpAny, dest_type, result);
1053 break;
1054
1055 case nir_op_ball_iequal2:
1056 case nir_op_ball_iequal3:
1057 case nir_op_ball_iequal4:
1058 assert(nir_op_infos[alu->op].num_inputs == 2);
1059 assert(alu_instr_src_components(alu, 0) ==
1060 alu_instr_src_components(alu, 1));
1061 result = emit_binop(ctx, SpvOpIEqual,
1062 get_bvec_type(ctx, alu_instr_src_components(alu, 0)),
1063 src[0], src[1]);
1064 result = emit_unop(ctx, SpvOpAll, dest_type, result);
1065 break;
1066
1067 case nir_op_vec2:
1068 case nir_op_vec3:
1069 case nir_op_vec4: {
1070 int num_inputs = nir_op_infos[alu->op].num_inputs;
1071 assert(2 <= num_inputs && num_inputs <= 4);
1072 result = spirv_builder_emit_composite_construct(&ctx->builder, dest_type,
1073 src, num_inputs);
1074 }
1075 break;
1076
1077 default:
1078 fprintf(stderr, "emit_alu: not implemented (%s)\n",
1079 nir_op_infos[alu->op].name);
1080
1081 unreachable("unsupported opcode");
1082 return;
1083 }
1084
1085 store_alu_result(ctx, alu, result);
1086 }
1087
1088 static void
1089 emit_load_const(struct ntv_context *ctx, nir_load_const_instr *load_const)
1090 {
1091 unsigned bit_size = load_const->def.bit_size;
1092 unsigned num_components = load_const->def.num_components;
1093
1094 SpvId constant;
1095 if (num_components > 1) {
1096 SpvId components[num_components];
1097 SpvId type;
1098 if (bit_size == 1) {
1099 for (int i = 0; i < num_components; i++)
1100 components[i] = spirv_builder_const_bool(&ctx->builder,
1101 load_const->value[i].b);
1102
1103 type = get_bvec_type(ctx, num_components);
1104 } else {
1105 for (int i = 0; i < num_components; i++)
1106 components[i] = emit_uint_const(ctx, bit_size,
1107 load_const->value[i].u32);
1108
1109 type = get_uvec_type(ctx, bit_size, num_components);
1110 }
1111 constant = spirv_builder_const_composite(&ctx->builder, type,
1112 components, num_components);
1113 } else {
1114 assert(num_components == 1);
1115 if (bit_size == 1)
1116 constant = spirv_builder_const_bool(&ctx->builder,
1117 load_const->value[0].b);
1118 else
1119 constant = emit_uint_const(ctx, bit_size, load_const->value[0].u32);
1120 }
1121
1122 if (bit_size == 1)
1123 constant = bvec_to_uvec(ctx, constant, num_components);
1124
1125 store_ssa_def_uint(ctx, &load_const->def, constant);
1126 }
1127
1128 static void
1129 emit_load_ubo(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1130 {
1131 nir_const_value *const_block_index = nir_src_as_const_value(intr->src[0]);
1132 assert(const_block_index); // no dynamic indexing for now
1133 assert(const_block_index->u32 == 0); // we only support the default UBO for now
1134
1135 nir_const_value *const_offset = nir_src_as_const_value(intr->src[1]);
1136 if (const_offset) {
1137 SpvId uvec4_type = get_uvec_type(ctx, 32, 4);
1138 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
1139 SpvStorageClassUniform,
1140 uvec4_type);
1141
1142 unsigned idx = const_offset->u32;
1143 SpvId member = emit_uint_const(ctx, 32, 0);
1144 SpvId offset = emit_uint_const(ctx, 32, idx);
1145 SpvId offsets[] = { member, offset };
1146 SpvId ptr = spirv_builder_emit_access_chain(&ctx->builder, pointer_type,
1147 ctx->ubos[0], offsets,
1148 ARRAY_SIZE(offsets));
1149 SpvId result = spirv_builder_emit_load(&ctx->builder, uvec4_type, ptr);
1150
1151 SpvId type = get_dest_uvec_type(ctx, &intr->dest);
1152 unsigned num_components = nir_dest_num_components(intr->dest);
1153 if (num_components == 1) {
1154 uint32_t components[] = { 0 };
1155 result = spirv_builder_emit_composite_extract(&ctx->builder,
1156 type,
1157 result, components,
1158 1);
1159 } else if (num_components < 4) {
1160 SpvId constituents[num_components];
1161 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, 32);
1162 for (uint32_t i = 0; i < num_components; ++i)
1163 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
1164 uint_type,
1165 result, &i,
1166 1);
1167
1168 result = spirv_builder_emit_composite_construct(&ctx->builder,
1169 type,
1170 constituents,
1171 num_components);
1172 }
1173
1174 store_dest_uint(ctx, &intr->dest, result);
1175 } else
1176 unreachable("uniform-addressing not yet supported");
1177 }
1178
1179 static void
1180 emit_discard(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1181 {
1182 assert(ctx->block_started);
1183 spirv_builder_emit_kill(&ctx->builder);
1184 /* discard is weird in NIR, so let's just create an unreachable block after
1185 it and hope that the vulkan driver will DCE any instructinos in it. */
1186 spirv_builder_label(&ctx->builder, spirv_builder_new_id(&ctx->builder));
1187 }
1188
1189 static void
1190 emit_load_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1191 {
1192 /* uint is a bit of a lie here; it's really just a pointer */
1193 SpvId ptr = get_src_uint(ctx, intr->src);
1194
1195 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1196 SpvId result = spirv_builder_emit_load(&ctx->builder,
1197 get_glsl_type(ctx, var->type),
1198 ptr);
1199 unsigned num_components = nir_dest_num_components(intr->dest);
1200 unsigned bit_size = nir_dest_bit_size(intr->dest);
1201 result = bitcast_to_uvec(ctx, result, bit_size, num_components);
1202 store_dest_uint(ctx, &intr->dest, result);
1203 }
1204
1205 static void
1206 emit_store_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1207 {
1208 /* uint is a bit of a lie here; it's really just a pointer */
1209 SpvId ptr = get_src_uint(ctx, &intr->src[0]);
1210 SpvId src = get_src_uint(ctx, &intr->src[1]);
1211
1212 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1213 SpvId type = get_glsl_type(ctx, glsl_without_array(var->type));
1214 SpvId result = emit_bitcast(ctx, type, src);
1215 spirv_builder_emit_store(&ctx->builder, ptr, result);
1216 }
1217
1218 static void
1219 emit_load_front_face(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1220 {
1221 SpvId var_type = get_glsl_type(ctx, glsl_bool_type());
1222 if (!ctx->front_face_var) {
1223 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
1224 SpvStorageClassInput,
1225 var_type);
1226 ctx->front_face_var = spirv_builder_emit_var(&ctx->builder,
1227 pointer_type,
1228 SpvStorageClassInput);
1229 spirv_builder_emit_name(&ctx->builder, ctx->front_face_var,
1230 "gl_FrontFacing");
1231 spirv_builder_emit_builtin(&ctx->builder, ctx->front_face_var,
1232 SpvBuiltInFrontFacing);
1233
1234 assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
1235 ctx->entry_ifaces[ctx->num_entry_ifaces++] = ctx->front_face_var;
1236 }
1237
1238 SpvId result = spirv_builder_emit_load(&ctx->builder, var_type,
1239 ctx->front_face_var);
1240 assert(1 == nir_dest_num_components(intr->dest));
1241 result = bvec_to_uvec(ctx, result, 1);
1242 store_dest_uint(ctx, &intr->dest, result);
1243 }
1244
1245 static void
1246 emit_intrinsic(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1247 {
1248 switch (intr->intrinsic) {
1249 case nir_intrinsic_load_ubo:
1250 emit_load_ubo(ctx, intr);
1251 break;
1252
1253 case nir_intrinsic_discard:
1254 emit_discard(ctx, intr);
1255 break;
1256
1257 case nir_intrinsic_load_deref:
1258 emit_load_deref(ctx, intr);
1259 break;
1260
1261 case nir_intrinsic_store_deref:
1262 emit_store_deref(ctx, intr);
1263 break;
1264
1265 case nir_intrinsic_load_front_face:
1266 emit_load_front_face(ctx, intr);
1267 break;
1268
1269 default:
1270 fprintf(stderr, "emit_intrinsic: not implemented (%s)\n",
1271 nir_intrinsic_infos[intr->intrinsic].name);
1272 unreachable("unsupported intrinsic");
1273 }
1274 }
1275
1276 static void
1277 emit_undef(struct ntv_context *ctx, nir_ssa_undef_instr *undef)
1278 {
1279 SpvId type = get_uvec_type(ctx, undef->def.bit_size,
1280 undef->def.num_components);
1281
1282 store_ssa_def_uint(ctx, &undef->def,
1283 spirv_builder_emit_undef(&ctx->builder, type));
1284 }
1285
1286 static SpvId
1287 get_src_float(struct ntv_context *ctx, nir_src *src)
1288 {
1289 SpvId def = get_src_uint(ctx, src);
1290 unsigned num_components = nir_src_num_components(*src);
1291 unsigned bit_size = nir_src_bit_size(*src);
1292 return bitcast_to_fvec(ctx, def, bit_size, num_components);
1293 }
1294
1295 static void
1296 emit_tex(struct ntv_context *ctx, nir_tex_instr *tex)
1297 {
1298 assert(tex->op == nir_texop_tex ||
1299 tex->op == nir_texop_txb ||
1300 tex->op == nir_texop_txl);
1301 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
1302 assert(tex->texture_index == tex->sampler_index);
1303
1304 SpvId coord = 0, proj = 0, bias = 0, lod = 0, dref = 0;
1305 unsigned coord_components;
1306 for (unsigned i = 0; i < tex->num_srcs; i++) {
1307 switch (tex->src[i].src_type) {
1308 case nir_tex_src_coord:
1309 coord = get_src_float(ctx, &tex->src[i].src);
1310 coord_components = nir_src_num_components(tex->src[i].src);
1311 break;
1312
1313 case nir_tex_src_projector:
1314 assert(nir_src_num_components(tex->src[i].src) == 1);
1315 proj = get_src_float(ctx, &tex->src[i].src);
1316 assert(proj != 0);
1317 break;
1318
1319 case nir_tex_src_bias:
1320 assert(tex->op == nir_texop_txb);
1321 bias = get_src_float(ctx, &tex->src[i].src);
1322 assert(bias != 0);
1323 break;
1324
1325 case nir_tex_src_lod:
1326 assert(nir_src_num_components(tex->src[i].src) == 1);
1327 lod = get_src_float(ctx, &tex->src[i].src);
1328 assert(lod != 0);
1329 break;
1330
1331 case nir_tex_src_comparator:
1332 assert(nir_src_num_components(tex->src[i].src) == 1);
1333 dref = get_src_float(ctx, &tex->src[i].src);
1334 assert(dref != 0);
1335 break;
1336
1337 default:
1338 fprintf(stderr, "texture source: %d\n", tex->src[i].src_type);
1339 unreachable("unknown texture source");
1340 }
1341 }
1342
1343 if (lod == 0 && ctx->stage != MESA_SHADER_FRAGMENT) {
1344 lod = emit_float_const(ctx, 32, 0.0f);
1345 assert(lod != 0);
1346 }
1347
1348 bool is_ms;
1349 SpvDim dimension = type_to_dim(tex->sampler_dim, &is_ms);
1350 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1351 SpvId image_type = spirv_builder_type_image(&ctx->builder, float_type,
1352 dimension, false, tex->is_array, is_ms, 1,
1353 SpvImageFormatUnknown);
1354 SpvId sampled_type = spirv_builder_type_sampled_image(&ctx->builder,
1355 image_type);
1356
1357 assert(tex->texture_index < ctx->num_samplers);
1358 SpvId load = spirv_builder_emit_load(&ctx->builder, sampled_type,
1359 ctx->samplers[tex->texture_index]);
1360
1361 SpvId dest_type = get_dest_type(ctx, &tex->dest, tex->dest_type);
1362
1363 if (proj) {
1364 SpvId constituents[coord_components + 1];
1365 if (coord_components == 1)
1366 constituents[0] = coord;
1367 else {
1368 assert(coord_components > 1);
1369 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1370 for (uint32_t i = 0; i < coord_components; ++i)
1371 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
1372 float_type,
1373 coord,
1374 &i, 1);
1375 }
1376
1377 constituents[coord_components++] = proj;
1378
1379 SpvId vec_type = get_fvec_type(ctx, 32, coord_components);
1380 coord = spirv_builder_emit_composite_construct(&ctx->builder,
1381 vec_type,
1382 constituents,
1383 coord_components);
1384 }
1385
1386 SpvId actual_dest_type = dest_type;
1387 if (dref)
1388 actual_dest_type = float_type;
1389
1390 SpvId result = spirv_builder_emit_image_sample(&ctx->builder,
1391 actual_dest_type, load,
1392 coord,
1393 proj != 0,
1394 lod, bias, dref);
1395 spirv_builder_emit_decoration(&ctx->builder, result,
1396 SpvDecorationRelaxedPrecision);
1397
1398 if (dref) {
1399 SpvId components[4] = { result, result, result, result };
1400 result = spirv_builder_emit_composite_construct(&ctx->builder,
1401 dest_type,
1402 components,
1403 4);
1404 }
1405
1406 store_dest(ctx, &tex->dest, result, tex->dest_type);
1407 }
1408
1409 static void
1410 start_block(struct ntv_context *ctx, SpvId label)
1411 {
1412 /* terminate previous block if needed */
1413 if (ctx->block_started)
1414 spirv_builder_emit_branch(&ctx->builder, label);
1415
1416 /* start new block */
1417 spirv_builder_label(&ctx->builder, label);
1418 ctx->block_started = true;
1419 }
1420
1421 static void
1422 branch(struct ntv_context *ctx, SpvId label)
1423 {
1424 assert(ctx->block_started);
1425 spirv_builder_emit_branch(&ctx->builder, label);
1426 ctx->block_started = false;
1427 }
1428
1429 static void
1430 branch_conditional(struct ntv_context *ctx, SpvId condition, SpvId then_id,
1431 SpvId else_id)
1432 {
1433 assert(ctx->block_started);
1434 spirv_builder_emit_branch_conditional(&ctx->builder, condition,
1435 then_id, else_id);
1436 ctx->block_started = false;
1437 }
1438
1439 static void
1440 emit_jump(struct ntv_context *ctx, nir_jump_instr *jump)
1441 {
1442 switch (jump->type) {
1443 case nir_jump_break:
1444 assert(ctx->loop_break);
1445 branch(ctx, ctx->loop_break);
1446 break;
1447
1448 case nir_jump_continue:
1449 assert(ctx->loop_cont);
1450 branch(ctx, ctx->loop_cont);
1451 break;
1452
1453 default:
1454 unreachable("Unsupported jump type\n");
1455 }
1456 }
1457
1458 static void
1459 emit_deref_var(struct ntv_context *ctx, nir_deref_instr *deref)
1460 {
1461 assert(deref->deref_type == nir_deref_type_var);
1462
1463 struct hash_entry *he = _mesa_hash_table_search(ctx->vars, deref->var);
1464 assert(he);
1465 SpvId result = (SpvId)(intptr_t)he->data;
1466 /* uint is a bit of a lie here, it's really just an opaque type */
1467 store_dest_uint(ctx, &deref->dest, result);
1468 }
1469
1470 static void
1471 emit_deref_array(struct ntv_context *ctx, nir_deref_instr *deref)
1472 {
1473 assert(deref->deref_type == nir_deref_type_array);
1474 nir_variable *var = nir_deref_instr_get_variable(deref);
1475
1476 SpvStorageClass storage_class;
1477 switch (var->data.mode) {
1478 case nir_var_shader_in:
1479 storage_class = SpvStorageClassInput;
1480 break;
1481
1482 case nir_var_shader_out:
1483 storage_class = SpvStorageClassOutput;
1484 break;
1485
1486 default:
1487 unreachable("Unsupported nir_variable_mode\n");
1488 }
1489
1490 SpvId index = get_src_uint(ctx, &deref->arr.index);
1491
1492 SpvId ptr_type = spirv_builder_type_pointer(&ctx->builder,
1493 storage_class,
1494 get_glsl_type(ctx, deref->type));
1495
1496 SpvId result = spirv_builder_emit_access_chain(&ctx->builder,
1497 ptr_type,
1498 get_src_uint(ctx, &deref->parent),
1499 &index, 1);
1500 /* uint is a bit of a lie here, it's really just an opaque type */
1501 store_dest_uint(ctx, &deref->dest, result);
1502 }
1503
1504 static void
1505 emit_deref(struct ntv_context *ctx, nir_deref_instr *deref)
1506 {
1507 switch (deref->deref_type) {
1508 case nir_deref_type_var:
1509 emit_deref_var(ctx, deref);
1510 break;
1511
1512 case nir_deref_type_array:
1513 emit_deref_array(ctx, deref);
1514 break;
1515
1516 default:
1517 unreachable("unexpected deref_type");
1518 }
1519 }
1520
1521 static void
1522 emit_block(struct ntv_context *ctx, struct nir_block *block)
1523 {
1524 start_block(ctx, block_label(ctx, block));
1525 nir_foreach_instr(instr, block) {
1526 switch (instr->type) {
1527 case nir_instr_type_alu:
1528 emit_alu(ctx, nir_instr_as_alu(instr));
1529 break;
1530 case nir_instr_type_intrinsic:
1531 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1532 break;
1533 case nir_instr_type_load_const:
1534 emit_load_const(ctx, nir_instr_as_load_const(instr));
1535 break;
1536 case nir_instr_type_ssa_undef:
1537 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1538 break;
1539 case nir_instr_type_tex:
1540 emit_tex(ctx, nir_instr_as_tex(instr));
1541 break;
1542 case nir_instr_type_phi:
1543 unreachable("nir_instr_type_phi not supported");
1544 break;
1545 case nir_instr_type_jump:
1546 emit_jump(ctx, nir_instr_as_jump(instr));
1547 break;
1548 case nir_instr_type_call:
1549 unreachable("nir_instr_type_call not supported");
1550 break;
1551 case nir_instr_type_parallel_copy:
1552 unreachable("nir_instr_type_parallel_copy not supported");
1553 break;
1554 case nir_instr_type_deref:
1555 emit_deref(ctx, nir_instr_as_deref(instr));
1556 break;
1557 }
1558 }
1559 }
1560
1561 static void
1562 emit_cf_list(struct ntv_context *ctx, struct exec_list *list);
1563
1564 static SpvId
1565 get_src_bool(struct ntv_context *ctx, nir_src *src)
1566 {
1567 SpvId def = get_src_uint(ctx, src);
1568 assert(nir_src_bit_size(*src) == 1);
1569 unsigned num_components = nir_src_num_components(*src);
1570 return uvec_to_bvec(ctx, def, num_components);
1571 }
1572
1573 static void
1574 emit_if(struct ntv_context *ctx, nir_if *if_stmt)
1575 {
1576 SpvId condition = get_src_bool(ctx, &if_stmt->condition);
1577
1578 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1579 SpvId then_id = block_label(ctx, nir_if_first_then_block(if_stmt));
1580 SpvId endif_id = spirv_builder_new_id(&ctx->builder);
1581 SpvId else_id = endif_id;
1582
1583 bool has_else = !exec_list_is_empty(&if_stmt->else_list);
1584 if (has_else) {
1585 assert(nir_if_first_else_block(if_stmt)->index < ctx->num_blocks);
1586 else_id = block_label(ctx, nir_if_first_else_block(if_stmt));
1587 }
1588
1589 /* create a header-block */
1590 start_block(ctx, header_id);
1591 spirv_builder_emit_selection_merge(&ctx->builder, endif_id,
1592 SpvSelectionControlMaskNone);
1593 branch_conditional(ctx, condition, then_id, else_id);
1594
1595 emit_cf_list(ctx, &if_stmt->then_list);
1596
1597 if (has_else) {
1598 if (ctx->block_started)
1599 branch(ctx, endif_id);
1600
1601 emit_cf_list(ctx, &if_stmt->else_list);
1602 }
1603
1604 start_block(ctx, endif_id);
1605 }
1606
1607 static void
1608 emit_loop(struct ntv_context *ctx, nir_loop *loop)
1609 {
1610 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1611 SpvId begin_id = block_label(ctx, nir_loop_first_block(loop));
1612 SpvId break_id = spirv_builder_new_id(&ctx->builder);
1613 SpvId cont_id = spirv_builder_new_id(&ctx->builder);
1614
1615 /* create a header-block */
1616 start_block(ctx, header_id);
1617 spirv_builder_loop_merge(&ctx->builder, break_id, cont_id, SpvLoopControlMaskNone);
1618 branch(ctx, begin_id);
1619
1620 SpvId save_break = ctx->loop_break;
1621 SpvId save_cont = ctx->loop_cont;
1622 ctx->loop_break = break_id;
1623 ctx->loop_cont = cont_id;
1624
1625 emit_cf_list(ctx, &loop->body);
1626
1627 ctx->loop_break = save_break;
1628 ctx->loop_cont = save_cont;
1629
1630 branch(ctx, cont_id);
1631 start_block(ctx, cont_id);
1632 branch(ctx, header_id);
1633
1634 start_block(ctx, break_id);
1635 }
1636
1637 static void
1638 emit_cf_list(struct ntv_context *ctx, struct exec_list *list)
1639 {
1640 foreach_list_typed(nir_cf_node, node, node, list) {
1641 switch (node->type) {
1642 case nir_cf_node_block:
1643 emit_block(ctx, nir_cf_node_as_block(node));
1644 break;
1645
1646 case nir_cf_node_if:
1647 emit_if(ctx, nir_cf_node_as_if(node));
1648 break;
1649
1650 case nir_cf_node_loop:
1651 emit_loop(ctx, nir_cf_node_as_loop(node));
1652 break;
1653
1654 case nir_cf_node_function:
1655 unreachable("nir_cf_node_function not supported");
1656 break;
1657 }
1658 }
1659 }
1660
1661 struct spirv_shader *
1662 nir_to_spirv(struct nir_shader *s)
1663 {
1664 struct spirv_shader *ret = NULL;
1665
1666 struct ntv_context ctx = {};
1667
1668 switch (s->info.stage) {
1669 case MESA_SHADER_VERTEX:
1670 case MESA_SHADER_FRAGMENT:
1671 case MESA_SHADER_COMPUTE:
1672 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityShader);
1673 break;
1674
1675 case MESA_SHADER_TESS_CTRL:
1676 case MESA_SHADER_TESS_EVAL:
1677 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityTessellation);
1678 break;
1679
1680 case MESA_SHADER_GEOMETRY:
1681 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityGeometry);
1682 break;
1683
1684 default:
1685 unreachable("invalid stage");
1686 }
1687
1688 // TODO: only enable when needed
1689 if (s->info.stage == MESA_SHADER_FRAGMENT)
1690 spirv_builder_emit_cap(&ctx.builder, SpvCapabilitySampled1D);
1691
1692 ctx.stage = s->info.stage;
1693 ctx.GLSL_std_450 = spirv_builder_import(&ctx.builder, "GLSL.std.450");
1694 spirv_builder_emit_source(&ctx.builder, SpvSourceLanguageGLSL, 450);
1695
1696 spirv_builder_emit_mem_model(&ctx.builder, SpvAddressingModelLogical,
1697 SpvMemoryModelGLSL450);
1698
1699 SpvExecutionModel exec_model;
1700 switch (s->info.stage) {
1701 case MESA_SHADER_VERTEX:
1702 exec_model = SpvExecutionModelVertex;
1703 break;
1704 case MESA_SHADER_TESS_CTRL:
1705 exec_model = SpvExecutionModelTessellationControl;
1706 break;
1707 case MESA_SHADER_TESS_EVAL:
1708 exec_model = SpvExecutionModelTessellationEvaluation;
1709 break;
1710 case MESA_SHADER_GEOMETRY:
1711 exec_model = SpvExecutionModelGeometry;
1712 break;
1713 case MESA_SHADER_FRAGMENT:
1714 exec_model = SpvExecutionModelFragment;
1715 break;
1716 case MESA_SHADER_COMPUTE:
1717 exec_model = SpvExecutionModelGLCompute;
1718 break;
1719 default:
1720 unreachable("invalid stage");
1721 }
1722
1723 SpvId type_void = spirv_builder_type_void(&ctx.builder);
1724 SpvId type_main = spirv_builder_type_function(&ctx.builder, type_void,
1725 NULL, 0);
1726 SpvId entry_point = spirv_builder_new_id(&ctx.builder);
1727 spirv_builder_emit_name(&ctx.builder, entry_point, "main");
1728
1729 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1730 _mesa_key_pointer_equal);
1731
1732 nir_foreach_variable(var, &s->inputs)
1733 emit_input(&ctx, var);
1734
1735 nir_foreach_variable(var, &s->outputs)
1736 emit_output(&ctx, var);
1737
1738 nir_foreach_variable(var, &s->uniforms)
1739 emit_uniform(&ctx, var);
1740
1741 if (s->info.stage == MESA_SHADER_FRAGMENT) {
1742 spirv_builder_emit_exec_mode(&ctx.builder, entry_point,
1743 SpvExecutionModeOriginUpperLeft);
1744 if (s->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
1745 spirv_builder_emit_exec_mode(&ctx.builder, entry_point,
1746 SpvExecutionModeDepthReplacing);
1747 }
1748
1749
1750 spirv_builder_function(&ctx.builder, entry_point, type_void,
1751 SpvFunctionControlMaskNone,
1752 type_main);
1753
1754 nir_function_impl *entry = nir_shader_get_entrypoint(s);
1755 nir_metadata_require(entry, nir_metadata_block_index);
1756
1757 ctx.defs = (SpvId *)malloc(sizeof(SpvId) * entry->ssa_alloc);
1758 if (!ctx.defs)
1759 goto fail;
1760 ctx.num_defs = entry->ssa_alloc;
1761
1762 nir_index_local_regs(entry);
1763 ctx.regs = malloc(sizeof(SpvId) * entry->reg_alloc);
1764 if (!ctx.regs)
1765 goto fail;
1766 ctx.num_regs = entry->reg_alloc;
1767
1768 SpvId *block_ids = (SpvId *)malloc(sizeof(SpvId) * entry->num_blocks);
1769 if (!block_ids)
1770 goto fail;
1771
1772 for (int i = 0; i < entry->num_blocks; ++i)
1773 block_ids[i] = spirv_builder_new_id(&ctx.builder);
1774
1775 ctx.block_ids = block_ids;
1776 ctx.num_blocks = entry->num_blocks;
1777
1778 /* emit a block only for the variable declarations */
1779 start_block(&ctx, spirv_builder_new_id(&ctx.builder));
1780 foreach_list_typed(nir_register, reg, node, &entry->registers) {
1781 SpvId type = get_uvec_type(&ctx, reg->bit_size, reg->num_components);
1782 SpvId pointer_type = spirv_builder_type_pointer(&ctx.builder,
1783 SpvStorageClassFunction,
1784 type);
1785 SpvId var = spirv_builder_emit_var(&ctx.builder, pointer_type,
1786 SpvStorageClassFunction);
1787
1788 ctx.regs[reg->index] = var;
1789 }
1790
1791 emit_cf_list(&ctx, &entry->body);
1792
1793 free(ctx.defs);
1794
1795 spirv_builder_return(&ctx.builder); // doesn't belong here, but whatevz
1796 spirv_builder_function_end(&ctx.builder);
1797
1798 spirv_builder_emit_entry_point(&ctx.builder, exec_model, entry_point,
1799 "main", ctx.entry_ifaces,
1800 ctx.num_entry_ifaces);
1801
1802 size_t num_words = spirv_builder_get_num_words(&ctx.builder);
1803
1804 ret = CALLOC_STRUCT(spirv_shader);
1805 if (!ret)
1806 goto fail;
1807
1808 ret->words = MALLOC(sizeof(uint32_t) * num_words);
1809 if (!ret->words)
1810 goto fail;
1811
1812 ret->num_words = spirv_builder_get_words(&ctx.builder, ret->words, num_words);
1813 assert(ret->num_words == num_words);
1814
1815 return ret;
1816
1817 fail:
1818
1819 if (ret)
1820 spirv_shader_delete(ret);
1821
1822 if (ctx.vars)
1823 _mesa_hash_table_destroy(ctx.vars, NULL);
1824
1825 return NULL;
1826 }
1827
1828 void
1829 spirv_shader_delete(struct spirv_shader *s)
1830 {
1831 FREE(s->words);
1832 FREE(s);
1833 }