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