zink: set ExecutionModeDepthReplacing when depth is written
[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
488 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, bit_size);
489 if (used_channels == 1) {
490 uint32_t indices[] = { alu->src[src].swizzle[0] };
491 return spirv_builder_emit_composite_extract(&ctx->builder, uint_type,
492 def, indices,
493 ARRAY_SIZE(indices));
494 } else if (live_channels == 1) {
495 SpvId uvec_type = spirv_builder_type_vector(&ctx->builder, uint_type,
496 used_channels);
497
498 SpvId constituents[NIR_MAX_VEC_COMPONENTS];
499 for (unsigned i = 0; i < used_channels; ++i)
500 constituents[i] = def;
501
502 return spirv_builder_emit_composite_construct(&ctx->builder, uvec_type,
503 constituents,
504 used_channels);
505 } else {
506 SpvId uvec_type = spirv_builder_type_vector(&ctx->builder, uint_type,
507 used_channels);
508
509 uint32_t components[NIR_MAX_VEC_COMPONENTS];
510 size_t num_components = 0;
511 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
512 if (!nir_alu_instr_channel_used(alu, src, i))
513 continue;
514
515 components[num_components++] = alu->src[src].swizzle[i];
516 }
517
518 return spirv_builder_emit_vector_shuffle(&ctx->builder, uvec_type,
519 def, def, components, num_components);
520 }
521 }
522
523 static void
524 store_ssa_def_uint(struct ntv_context *ctx, nir_ssa_def *ssa, SpvId result)
525 {
526 assert(result != 0);
527 assert(ssa->index < ctx->num_defs);
528 ctx->defs[ssa->index] = result;
529 }
530
531 static SpvId
532 bvec_to_uvec(struct ntv_context *ctx, SpvId value, unsigned num_components)
533 {
534 SpvId otype = get_uvec_type(ctx, 32, num_components);
535 uint32_t zeros[4] = { 0, 0, 0, 0 };
536 uint32_t ones[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
537 SpvId zero = get_uvec_constant(ctx, 32, num_components, zeros);
538 SpvId one = get_uvec_constant(ctx, 32, num_components, ones);
539 return emit_triop(ctx, SpvOpSelect, otype, value, one, zero);
540 }
541
542 static SpvId
543 uvec_to_bvec(struct ntv_context *ctx, SpvId value, unsigned num_components)
544 {
545 SpvId type = get_bvec_type(ctx, num_components);
546
547 uint32_t zeros[NIR_MAX_VEC_COMPONENTS] = { 0 };
548 SpvId zero = get_uvec_constant(ctx, 32, num_components, zeros);
549
550 return emit_binop(ctx, SpvOpINotEqual, type, value, zero);
551 }
552
553 static SpvId
554 bitcast_to_uvec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
555 unsigned num_components)
556 {
557 SpvId type = get_uvec_type(ctx, bit_size, num_components);
558 return emit_unop(ctx, SpvOpBitcast, type, value);
559 }
560
561 static SpvId
562 bitcast_to_ivec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
563 unsigned num_components)
564 {
565 SpvId type = get_ivec_type(ctx, bit_size, num_components);
566 return emit_unop(ctx, SpvOpBitcast, type, value);
567 }
568
569 static SpvId
570 bitcast_to_fvec(struct ntv_context *ctx, SpvId value, unsigned bit_size,
571 unsigned num_components)
572 {
573 SpvId type = get_fvec_type(ctx, bit_size, num_components);
574 return emit_unop(ctx, SpvOpBitcast, type, value);
575 }
576
577 static void
578 store_reg_def(struct ntv_context *ctx, nir_reg_dest *reg, SpvId result)
579 {
580 SpvId var = get_var_from_reg(ctx, reg->reg);
581 assert(var);
582 spirv_builder_emit_store(&ctx->builder, var, result);
583 }
584
585 static void
586 store_dest_uint(struct ntv_context *ctx, nir_dest *dest, SpvId result)
587 {
588 if (dest->is_ssa)
589 store_ssa_def_uint(ctx, &dest->ssa, result);
590 else
591 store_reg_def(ctx, &dest->reg, result);
592 }
593
594 static void
595 store_dest(struct ntv_context *ctx, nir_dest *dest, SpvId result, nir_alu_type type)
596 {
597 unsigned num_components = nir_dest_num_components(*dest);
598 unsigned bit_size = nir_dest_bit_size(*dest);
599
600 switch (nir_alu_type_get_base_type(type)) {
601 case nir_type_bool:
602 assert(bit_size == 1);
603 result = bvec_to_uvec(ctx, result, num_components);
604 break;
605
606 case nir_type_uint:
607 break; /* nothing to do! */
608
609 case nir_type_int:
610 case nir_type_float:
611 result = bitcast_to_uvec(ctx, result, bit_size, num_components);
612 break;
613
614 default:
615 unreachable("unsupported nir_alu_type");
616 }
617
618 store_dest_uint(ctx, dest, result);
619 }
620
621 static SpvId
622 emit_unop(struct ntv_context *ctx, SpvOp op, SpvId type, SpvId src)
623 {
624 return spirv_builder_emit_unop(&ctx->builder, op, type, src);
625 }
626
627 static SpvId
628 emit_binop(struct ntv_context *ctx, SpvOp op, SpvId type,
629 SpvId src0, SpvId src1)
630 {
631 return spirv_builder_emit_binop(&ctx->builder, op, type, src0, src1);
632 }
633
634 static SpvId
635 emit_triop(struct ntv_context *ctx, SpvOp op, SpvId type,
636 SpvId src0, SpvId src1, SpvId src2)
637 {
638 return spirv_builder_emit_triop(&ctx->builder, op, type, src0, src1, src2);
639 }
640
641 static SpvId
642 emit_builtin_unop(struct ntv_context *ctx, enum GLSLstd450 op, SpvId type,
643 SpvId src)
644 {
645 SpvId args[] = { src };
646 return spirv_builder_emit_ext_inst(&ctx->builder, type, ctx->GLSL_std_450,
647 op, args, ARRAY_SIZE(args));
648 }
649
650 static SpvId
651 emit_builtin_binop(struct ntv_context *ctx, enum GLSLstd450 op, SpvId type,
652 SpvId src0, SpvId src1)
653 {
654 SpvId args[] = { src0, src1 };
655 return spirv_builder_emit_ext_inst(&ctx->builder, type, ctx->GLSL_std_450,
656 op, args, ARRAY_SIZE(args));
657 }
658
659 static SpvId
660 get_fvec_constant(struct ntv_context *ctx, int bit_size, int num_components,
661 const float values[])
662 {
663 assert(bit_size == 32);
664
665 if (num_components > 1) {
666 SpvId components[num_components];
667 for (int i = 0; i < num_components; i++)
668 components[i] = spirv_builder_const_float(&ctx->builder, bit_size,
669 values[i]);
670
671 SpvId type = get_fvec_type(ctx, bit_size, num_components);
672 return spirv_builder_const_composite(&ctx->builder, type, components,
673 num_components);
674 }
675
676 assert(num_components == 1);
677 return spirv_builder_const_float(&ctx->builder, bit_size, values[0]);
678 }
679
680 static SpvId
681 get_uvec_constant(struct ntv_context *ctx, int bit_size, int num_components,
682 const uint32_t values[])
683 {
684 assert(bit_size == 32);
685
686 if (num_components > 1) {
687 SpvId components[num_components];
688 for (int i = 0; i < num_components; i++)
689 components[i] = spirv_builder_const_uint(&ctx->builder, bit_size,
690 values[i]);
691
692 SpvId type = get_uvec_type(ctx, bit_size, num_components);
693 return spirv_builder_const_composite(&ctx->builder, type, components,
694 num_components);
695 }
696
697 assert(num_components == 1);
698 return spirv_builder_const_uint(&ctx->builder, bit_size, values[0]);
699 }
700
701 static inline unsigned
702 alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
703 {
704 if (nir_op_infos[instr->op].input_sizes[src] > 0)
705 return nir_op_infos[instr->op].input_sizes[src];
706
707 if (instr->dest.dest.is_ssa)
708 return instr->dest.dest.ssa.num_components;
709 else
710 return instr->dest.dest.reg.reg->num_components;
711 }
712
713 static SpvId
714 get_alu_src(struct ntv_context *ctx, nir_alu_instr *alu, unsigned src)
715 {
716 SpvId uint_value = get_alu_src_uint(ctx, alu, src);
717
718 unsigned num_components = alu_instr_src_components(alu, src);
719 unsigned bit_size = nir_src_bit_size(alu->src[src].src);
720 nir_alu_type type = nir_op_infos[alu->op].input_types[src];
721
722 switch (nir_alu_type_get_base_type(type)) {
723 case nir_type_bool:
724 assert(bit_size == 1);
725 return uvec_to_bvec(ctx, uint_value, num_components);
726
727 case nir_type_int:
728 return bitcast_to_ivec(ctx, uint_value, bit_size, num_components);
729
730 case nir_type_uint:
731 return uint_value;
732
733 case nir_type_float:
734 return bitcast_to_fvec(ctx, uint_value, bit_size, num_components);
735
736 default:
737 unreachable("unknown nir_alu_type");
738 }
739 }
740
741 static void
742 store_alu_result(struct ntv_context *ctx, nir_alu_instr *alu, SpvId result)
743 {
744 assert(!alu->dest.saturate);
745 return store_dest(ctx, &alu->dest.dest, result, nir_op_infos[alu->op].output_type);
746 }
747
748 static SpvId
749 get_dest_type(struct ntv_context *ctx, nir_dest *dest, nir_alu_type type)
750 {
751 unsigned num_components = nir_dest_num_components(*dest);
752 unsigned bit_size = nir_dest_bit_size(*dest);
753
754 switch (nir_alu_type_get_base_type(type)) {
755 case nir_type_bool:
756 return get_bvec_type(ctx, num_components);
757
758 case nir_type_int:
759 return get_ivec_type(ctx, bit_size, num_components);
760
761 case nir_type_uint:
762 return get_uvec_type(ctx, bit_size, num_components);
763
764 case nir_type_float:
765 return get_fvec_type(ctx, bit_size, num_components);
766
767 default:
768 unreachable("unsupported nir_alu_type");
769 }
770 }
771
772 static void
773 emit_alu(struct ntv_context *ctx, nir_alu_instr *alu)
774 {
775 SpvId src[nir_op_infos[alu->op].num_inputs];
776 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++)
777 src[i] = get_alu_src(ctx, alu, i);
778
779 SpvId dest_type = get_dest_type(ctx, &alu->dest.dest,
780 nir_op_infos[alu->op].output_type);
781 unsigned bit_size = nir_dest_bit_size(alu->dest.dest);
782 unsigned num_components = nir_dest_num_components(alu->dest.dest);
783
784 SpvId result = 0;
785 switch (alu->op) {
786 case nir_op_mov:
787 assert(nir_op_infos[alu->op].num_inputs == 1);
788 result = src[0];
789 break;
790
791 #define UNOP(nir_op, spirv_op) \
792 case nir_op: \
793 assert(nir_op_infos[alu->op].num_inputs == 1); \
794 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
795 break;
796
797 UNOP(nir_op_ineg, SpvOpSNegate)
798 UNOP(nir_op_fneg, SpvOpFNegate)
799 UNOP(nir_op_fddx, SpvOpDPdx)
800 UNOP(nir_op_fddy, SpvOpDPdy)
801 UNOP(nir_op_f2i32, SpvOpConvertFToS)
802 UNOP(nir_op_f2u32, SpvOpConvertFToU)
803 UNOP(nir_op_i2f32, SpvOpConvertSToF)
804 UNOP(nir_op_u2f32, SpvOpConvertUToF)
805 #undef UNOP
806
807 #define BUILTIN_UNOP(nir_op, spirv_op) \
808 case nir_op: \
809 assert(nir_op_infos[alu->op].num_inputs == 1); \
810 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
811 break;
812
813 BUILTIN_UNOP(nir_op_fabs, GLSLstd450FAbs)
814 BUILTIN_UNOP(nir_op_fsqrt, GLSLstd450Sqrt)
815 BUILTIN_UNOP(nir_op_frsq, GLSLstd450InverseSqrt)
816 BUILTIN_UNOP(nir_op_flog2, GLSLstd450Log2)
817 BUILTIN_UNOP(nir_op_fexp2, GLSLstd450Exp2)
818 BUILTIN_UNOP(nir_op_ffract, GLSLstd450Fract)
819 BUILTIN_UNOP(nir_op_ffloor, GLSLstd450Floor)
820 BUILTIN_UNOP(nir_op_fceil, GLSLstd450Ceil)
821 BUILTIN_UNOP(nir_op_ftrunc, GLSLstd450Trunc)
822 BUILTIN_UNOP(nir_op_fround_even, GLSLstd450RoundEven)
823 BUILTIN_UNOP(nir_op_fsign, GLSLstd450FSign)
824 BUILTIN_UNOP(nir_op_fsin, GLSLstd450Sin)
825 BUILTIN_UNOP(nir_op_fcos, GLSLstd450Cos)
826 #undef BUILTIN_UNOP
827
828 case nir_op_frcp: {
829 assert(nir_op_infos[alu->op].num_inputs == 1);
830 float one[4] = { 1, 1, 1, 1 };
831 src[1] = src[0];
832 src[0] = get_fvec_constant(ctx, bit_size, num_components, one);
833 result = emit_binop(ctx, SpvOpFDiv, dest_type, src[0], src[1]);
834 }
835 break;
836
837 #define BINOP(nir_op, spirv_op) \
838 case nir_op: \
839 assert(nir_op_infos[alu->op].num_inputs == 2); \
840 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
841 break;
842
843 BINOP(nir_op_iadd, SpvOpIAdd)
844 BINOP(nir_op_isub, SpvOpISub)
845 BINOP(nir_op_imul, SpvOpIMul)
846 BINOP(nir_op_idiv, SpvOpSDiv)
847 BINOP(nir_op_udiv, SpvOpUDiv)
848 BINOP(nir_op_fadd, SpvOpFAdd)
849 BINOP(nir_op_fsub, SpvOpFSub)
850 BINOP(nir_op_fmul, SpvOpFMul)
851 BINOP(nir_op_fdiv, SpvOpFDiv)
852 BINOP(nir_op_fmod, SpvOpFMod)
853 BINOP(nir_op_ilt, SpvOpSLessThan)
854 BINOP(nir_op_ige, SpvOpSGreaterThanEqual)
855 BINOP(nir_op_ieq, SpvOpIEqual)
856 BINOP(nir_op_ine, SpvOpINotEqual)
857 BINOP(nir_op_flt, SpvOpFUnordLessThan)
858 BINOP(nir_op_fge, SpvOpFUnordGreaterThanEqual)
859 BINOP(nir_op_feq, SpvOpFOrdEqual)
860 BINOP(nir_op_fne, SpvOpFOrdNotEqual)
861 BINOP(nir_op_ishl, SpvOpShiftLeftLogical)
862 BINOP(nir_op_ishr, SpvOpShiftRightArithmetic)
863 BINOP(nir_op_ushr, SpvOpShiftRightLogical)
864 #undef BINOP
865
866 #define BUILTIN_BINOP(nir_op, spirv_op) \
867 case nir_op: \
868 assert(nir_op_infos[alu->op].num_inputs == 2); \
869 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
870 break;
871
872 BUILTIN_BINOP(nir_op_fmin, GLSLstd450FMin)
873 BUILTIN_BINOP(nir_op_fmax, GLSLstd450FMax)
874 #undef BUILTIN_BINOP
875
876 case nir_op_fdot2:
877 case nir_op_fdot3:
878 case nir_op_fdot4:
879 assert(nir_op_infos[alu->op].num_inputs == 2);
880 result = emit_binop(ctx, SpvOpDot, dest_type, src[0], src[1]);
881 break;
882
883 case nir_op_seq:
884 case nir_op_sne:
885 case nir_op_slt:
886 case nir_op_sge: {
887 assert(nir_op_infos[alu->op].num_inputs == 2);
888 int num_components = nir_dest_num_components(alu->dest.dest);
889 SpvId bool_type = get_bvec_type(ctx, num_components);
890
891 SpvId zero = spirv_builder_const_float(&ctx->builder, 32, 0.0f);
892 SpvId one = spirv_builder_const_float(&ctx->builder, 32, 1.0f);
893 if (num_components > 1) {
894 SpvId zero_comps[num_components], one_comps[num_components];
895 for (int i = 0; i < num_components; i++) {
896 zero_comps[i] = zero;
897 one_comps[i] = one;
898 }
899
900 zero = spirv_builder_const_composite(&ctx->builder, dest_type,
901 zero_comps, num_components);
902 one = spirv_builder_const_composite(&ctx->builder, dest_type,
903 one_comps, num_components);
904 }
905
906 SpvOp op;
907 switch (alu->op) {
908 case nir_op_seq: op = SpvOpFOrdEqual; break;
909 case nir_op_sne: op = SpvOpFOrdNotEqual; break;
910 case nir_op_slt: op = SpvOpFOrdLessThan; break;
911 case nir_op_sge: op = SpvOpFOrdGreaterThanEqual; break;
912 default: unreachable("unexpected op");
913 }
914
915 result = emit_binop(ctx, op, bool_type, src[0], src[1]);
916 result = emit_triop(ctx, SpvOpSelect, dest_type, result, one, zero);
917 }
918 break;
919
920 case nir_op_fcsel: {
921 assert(nir_op_infos[alu->op].num_inputs == 3);
922 int num_components = nir_dest_num_components(alu->dest.dest);
923 SpvId bool_type = get_bvec_type(ctx, num_components);
924
925 float zero[4] = { 0, 0, 0, 0 };
926 SpvId cmp = get_fvec_constant(ctx, nir_src_bit_size(alu->src[0].src),
927 num_components, zero);
928
929 result = emit_binop(ctx, SpvOpFOrdGreaterThan, bool_type, src[0], cmp);
930 result = emit_triop(ctx, SpvOpSelect, dest_type, result, src[1], src[2]);
931 }
932 break;
933
934 case nir_op_vec2:
935 case nir_op_vec3:
936 case nir_op_vec4: {
937 int num_inputs = nir_op_infos[alu->op].num_inputs;
938 assert(2 <= num_inputs && num_inputs <= 4);
939 result = spirv_builder_emit_composite_construct(&ctx->builder, dest_type,
940 src, num_inputs);
941 }
942 break;
943
944 default:
945 fprintf(stderr, "emit_alu: not implemented (%s)\n",
946 nir_op_infos[alu->op].name);
947
948 unreachable("unsupported opcode");
949 return;
950 }
951
952 store_alu_result(ctx, alu, result);
953 }
954
955 static void
956 emit_load_const(struct ntv_context *ctx, nir_load_const_instr *load_const)
957 {
958 uint32_t values[NIR_MAX_VEC_COMPONENTS];
959 for (int i = 0; i < load_const->def.num_components; ++i)
960 values[i] = load_const->value[i].u32;
961
962 SpvId constant = get_uvec_constant(ctx, load_const->def.bit_size,
963 load_const->def.num_components,
964 values);
965 store_ssa_def_uint(ctx, &load_const->def, constant);
966 }
967
968 static void
969 emit_load_ubo(struct ntv_context *ctx, nir_intrinsic_instr *intr)
970 {
971 nir_const_value *const_block_index = nir_src_as_const_value(intr->src[0]);
972 assert(const_block_index); // no dynamic indexing for now
973 assert(const_block_index->u32 == 0); // we only support the default UBO for now
974
975 nir_const_value *const_offset = nir_src_as_const_value(intr->src[1]);
976 if (const_offset) {
977 SpvId uvec4_type = get_uvec_type(ctx, 32, 4);
978 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
979 SpvStorageClassUniform,
980 uvec4_type);
981
982 unsigned idx = const_offset->u32;
983 SpvId member = spirv_builder_const_uint(&ctx->builder, 32, 0);
984 SpvId offset = spirv_builder_const_uint(&ctx->builder, 32, idx);
985 SpvId offsets[] = { member, offset };
986 SpvId ptr = spirv_builder_emit_access_chain(&ctx->builder, pointer_type,
987 ctx->ubos[0], offsets,
988 ARRAY_SIZE(offsets));
989 SpvId result = spirv_builder_emit_load(&ctx->builder, uvec4_type, ptr);
990
991 SpvId type = get_dest_uvec_type(ctx, &intr->dest);
992 unsigned num_components = nir_dest_num_components(intr->dest);
993 if (num_components == 1) {
994 uint32_t components[] = { 0 };
995 result = spirv_builder_emit_composite_extract(&ctx->builder,
996 type,
997 result, components,
998 1);
999 } else if (num_components < 4) {
1000 SpvId constituents[num_components];
1001 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, 32);
1002 for (uint32_t i = 0; i < num_components; ++i)
1003 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
1004 uint_type,
1005 result, &i,
1006 1);
1007
1008 result = spirv_builder_emit_composite_construct(&ctx->builder,
1009 type,
1010 constituents,
1011 num_components);
1012 }
1013
1014 store_dest_uint(ctx, &intr->dest, result);
1015 } else
1016 unreachable("uniform-addressing not yet supported");
1017 }
1018
1019 static void
1020 emit_discard(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1021 {
1022 assert(ctx->block_started);
1023 spirv_builder_emit_kill(&ctx->builder);
1024 /* discard is weird in NIR, so let's just create an unreachable block after
1025 it and hope that the vulkan driver will DCE any instructinos in it. */
1026 spirv_builder_label(&ctx->builder, spirv_builder_new_id(&ctx->builder));
1027 }
1028
1029 static void
1030 emit_load_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1031 {
1032 /* uint is a bit of a lie here; it's really just a pointer */
1033 SpvId ptr = get_src_uint(ctx, intr->src);
1034
1035 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1036 SpvId result = spirv_builder_emit_load(&ctx->builder,
1037 get_glsl_type(ctx, var->type),
1038 ptr);
1039 unsigned num_components = nir_dest_num_components(intr->dest);
1040 unsigned bit_size = nir_dest_bit_size(intr->dest);
1041 result = bitcast_to_uvec(ctx, result, bit_size, num_components);
1042 store_dest_uint(ctx, &intr->dest, result);
1043 }
1044
1045 static void
1046 emit_store_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1047 {
1048 /* uint is a bit of a lie here; it's really just a pointer */
1049 SpvId ptr = get_src_uint(ctx, &intr->src[0]);
1050 SpvId src = get_src_uint(ctx, &intr->src[1]);
1051
1052 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1053 SpvId result = emit_unop(ctx, SpvOpBitcast,
1054 get_glsl_type(ctx, glsl_without_array(var->type)),
1055 src);
1056 spirv_builder_emit_store(&ctx->builder, ptr, result);
1057 }
1058
1059 static void
1060 emit_intrinsic(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1061 {
1062 switch (intr->intrinsic) {
1063 case nir_intrinsic_load_ubo:
1064 emit_load_ubo(ctx, intr);
1065 break;
1066
1067 case nir_intrinsic_discard:
1068 emit_discard(ctx, intr);
1069 break;
1070
1071 case nir_intrinsic_load_deref:
1072 emit_load_deref(ctx, intr);
1073 break;
1074
1075 case nir_intrinsic_store_deref:
1076 emit_store_deref(ctx, intr);
1077 break;
1078
1079 default:
1080 fprintf(stderr, "emit_intrinsic: not implemented (%s)\n",
1081 nir_intrinsic_infos[intr->intrinsic].name);
1082 unreachable("unsupported intrinsic");
1083 }
1084 }
1085
1086 static void
1087 emit_undef(struct ntv_context *ctx, nir_ssa_undef_instr *undef)
1088 {
1089 SpvId type = get_uvec_type(ctx, undef->def.bit_size,
1090 undef->def.num_components);
1091
1092 store_ssa_def_uint(ctx, &undef->def,
1093 spirv_builder_emit_undef(&ctx->builder, type));
1094 }
1095
1096 static SpvId
1097 get_src_float(struct ntv_context *ctx, nir_src *src)
1098 {
1099 SpvId def = get_src_uint(ctx, src);
1100 unsigned num_components = nir_src_num_components(*src);
1101 unsigned bit_size = nir_src_bit_size(*src);
1102 return bitcast_to_fvec(ctx, def, bit_size, num_components);
1103 }
1104
1105 static void
1106 emit_tex(struct ntv_context *ctx, nir_tex_instr *tex)
1107 {
1108 assert(tex->op == nir_texop_tex ||
1109 tex->op == nir_texop_txb);
1110 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
1111 assert(tex->texture_index == tex->sampler_index);
1112
1113 SpvId coord = 0, proj = 0, bias = 0, lod = 0;
1114 unsigned coord_components;
1115 for (unsigned i = 0; i < tex->num_srcs; i++) {
1116 switch (tex->src[i].src_type) {
1117 case nir_tex_src_coord:
1118 coord = get_src_float(ctx, &tex->src[i].src);
1119 coord_components = nir_src_num_components(tex->src[i].src);
1120 break;
1121
1122 case nir_tex_src_projector:
1123 assert(nir_src_num_components(tex->src[i].src) == 1);
1124 proj = get_src_float(ctx, &tex->src[i].src);
1125 assert(proj != 0);
1126 break;
1127
1128 case nir_tex_src_bias:
1129 assert(tex->op == nir_texop_txb);
1130 bias = get_src_float(ctx, &tex->src[i].src);
1131 assert(bias != 0);
1132 break;
1133
1134 case nir_tex_src_lod:
1135 assert(nir_src_num_components(tex->src[i].src) == 1);
1136 lod = get_src_float(ctx, &tex->src[i].src);
1137 assert(lod != 0);
1138 break;
1139
1140 default:
1141 fprintf(stderr, "texture source: %d\n", tex->src[i].src_type);
1142 unreachable("unknown texture source");
1143 }
1144 }
1145
1146 if (lod == 0 && ctx->stage != MESA_SHADER_FRAGMENT) {
1147 lod = spirv_builder_const_float(&ctx->builder, 32, 0);
1148 assert(lod != 0);
1149 }
1150
1151 bool is_ms;
1152 SpvDim dimension = type_to_dim(tex->sampler_dim, &is_ms);
1153 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1154 SpvId image_type = spirv_builder_type_image(&ctx->builder, float_type,
1155 dimension, false, tex->is_array, is_ms, 1,
1156 SpvImageFormatUnknown);
1157 SpvId sampled_type = spirv_builder_type_sampled_image(&ctx->builder,
1158 image_type);
1159
1160 assert(tex->texture_index < ctx->num_samplers);
1161 SpvId load = spirv_builder_emit_load(&ctx->builder, sampled_type,
1162 ctx->samplers[tex->texture_index]);
1163
1164 SpvId dest_type = get_dest_type(ctx, &tex->dest, tex->dest_type);
1165
1166 if (proj) {
1167 SpvId constituents[coord_components + 1];
1168 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1169 for (uint32_t i = 0; i < coord_components; ++i)
1170 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
1171 float_type,
1172 coord,
1173 &i, 1);
1174
1175 constituents[coord_components++] = proj;
1176
1177 SpvId vec_type = get_fvec_type(ctx, 32, coord_components);
1178 coord = spirv_builder_emit_composite_construct(&ctx->builder,
1179 vec_type,
1180 constituents,
1181 coord_components);
1182 }
1183
1184 SpvId result = spirv_builder_emit_image_sample(&ctx->builder,
1185 dest_type, load,
1186 coord,
1187 proj != 0,
1188 lod, bias);
1189 spirv_builder_emit_decoration(&ctx->builder, result,
1190 SpvDecorationRelaxedPrecision);
1191
1192 store_dest(ctx, &tex->dest, result, tex->dest_type);
1193 }
1194
1195 static void
1196 start_block(struct ntv_context *ctx, SpvId label)
1197 {
1198 /* terminate previous block if needed */
1199 if (ctx->block_started)
1200 spirv_builder_emit_branch(&ctx->builder, label);
1201
1202 /* start new block */
1203 spirv_builder_label(&ctx->builder, label);
1204 ctx->block_started = true;
1205 }
1206
1207 static void
1208 branch(struct ntv_context *ctx, SpvId label)
1209 {
1210 assert(ctx->block_started);
1211 spirv_builder_emit_branch(&ctx->builder, label);
1212 ctx->block_started = false;
1213 }
1214
1215 static void
1216 branch_conditional(struct ntv_context *ctx, SpvId condition, SpvId then_id,
1217 SpvId else_id)
1218 {
1219 assert(ctx->block_started);
1220 spirv_builder_emit_branch_conditional(&ctx->builder, condition,
1221 then_id, else_id);
1222 ctx->block_started = false;
1223 }
1224
1225 static void
1226 emit_jump(struct ntv_context *ctx, nir_jump_instr *jump)
1227 {
1228 switch (jump->type) {
1229 case nir_jump_break:
1230 assert(ctx->loop_break);
1231 branch(ctx, ctx->loop_break);
1232 break;
1233
1234 case nir_jump_continue:
1235 assert(ctx->loop_cont);
1236 branch(ctx, ctx->loop_cont);
1237 break;
1238
1239 default:
1240 unreachable("Unsupported jump type\n");
1241 }
1242 }
1243
1244 static void
1245 emit_deref_var(struct ntv_context *ctx, nir_deref_instr *deref)
1246 {
1247 assert(deref->deref_type == nir_deref_type_var);
1248
1249 struct hash_entry *he = _mesa_hash_table_search(ctx->vars, deref->var);
1250 assert(he);
1251 SpvId result = (SpvId)(intptr_t)he->data;
1252 /* uint is a bit of a lie here, it's really just an opaque type */
1253 store_dest_uint(ctx, &deref->dest, result);
1254 }
1255
1256 static void
1257 emit_deref_array(struct ntv_context *ctx, nir_deref_instr *deref)
1258 {
1259 assert(deref->deref_type == nir_deref_type_array);
1260 nir_variable *var = nir_deref_instr_get_variable(deref);
1261
1262 SpvStorageClass storage_class;
1263 switch (var->data.mode) {
1264 case nir_var_shader_in:
1265 storage_class = SpvStorageClassInput;
1266 break;
1267
1268 case nir_var_shader_out:
1269 storage_class = SpvStorageClassOutput;
1270 break;
1271
1272 default:
1273 unreachable("Unsupported nir_variable_mode\n");
1274 }
1275
1276 SpvId index = get_src_uint(ctx, &deref->arr.index);
1277
1278 SpvId ptr_type = spirv_builder_type_pointer(&ctx->builder,
1279 storage_class,
1280 get_glsl_type(ctx, deref->type));
1281
1282 SpvId result = spirv_builder_emit_access_chain(&ctx->builder,
1283 ptr_type,
1284 get_src_uint(ctx, &deref->parent),
1285 &index, 1);
1286 /* uint is a bit of a lie here, it's really just an opaque type */
1287 store_dest_uint(ctx, &deref->dest, result);
1288 }
1289
1290 static void
1291 emit_deref(struct ntv_context *ctx, nir_deref_instr *deref)
1292 {
1293 switch (deref->deref_type) {
1294 case nir_deref_type_var:
1295 emit_deref_var(ctx, deref);
1296 break;
1297
1298 case nir_deref_type_array:
1299 emit_deref_array(ctx, deref);
1300 break;
1301
1302 default:
1303 unreachable("unexpected deref_type");
1304 }
1305 }
1306
1307 static void
1308 emit_block(struct ntv_context *ctx, struct nir_block *block)
1309 {
1310 start_block(ctx, block_label(ctx, block));
1311 nir_foreach_instr(instr, block) {
1312 switch (instr->type) {
1313 case nir_instr_type_alu:
1314 emit_alu(ctx, nir_instr_as_alu(instr));
1315 break;
1316 case nir_instr_type_intrinsic:
1317 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1318 break;
1319 case nir_instr_type_load_const:
1320 emit_load_const(ctx, nir_instr_as_load_const(instr));
1321 break;
1322 case nir_instr_type_ssa_undef:
1323 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1324 break;
1325 case nir_instr_type_tex:
1326 emit_tex(ctx, nir_instr_as_tex(instr));
1327 break;
1328 case nir_instr_type_phi:
1329 unreachable("nir_instr_type_phi not supported");
1330 break;
1331 case nir_instr_type_jump:
1332 emit_jump(ctx, nir_instr_as_jump(instr));
1333 break;
1334 case nir_instr_type_call:
1335 unreachable("nir_instr_type_call not supported");
1336 break;
1337 case nir_instr_type_parallel_copy:
1338 unreachable("nir_instr_type_parallel_copy not supported");
1339 break;
1340 case nir_instr_type_deref:
1341 emit_deref(ctx, nir_instr_as_deref(instr));
1342 break;
1343 }
1344 }
1345 }
1346
1347 static void
1348 emit_cf_list(struct ntv_context *ctx, struct exec_list *list);
1349
1350 static SpvId
1351 get_src_bool(struct ntv_context *ctx, nir_src *src)
1352 {
1353 SpvId def = get_src_uint(ctx, src);
1354 assert(nir_src_bit_size(*src) == 32);
1355 unsigned num_components = nir_src_num_components(*src);
1356 return uvec_to_bvec(ctx, def, num_components);
1357 }
1358
1359 static void
1360 emit_if(struct ntv_context *ctx, nir_if *if_stmt)
1361 {
1362 SpvId condition = get_src_bool(ctx, &if_stmt->condition);
1363
1364 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1365 SpvId then_id = block_label(ctx, nir_if_first_then_block(if_stmt));
1366 SpvId endif_id = spirv_builder_new_id(&ctx->builder);
1367 SpvId else_id = endif_id;
1368
1369 bool has_else = !exec_list_is_empty(&if_stmt->else_list);
1370 if (has_else) {
1371 assert(nir_if_first_else_block(if_stmt)->index < ctx->num_blocks);
1372 else_id = block_label(ctx, nir_if_first_else_block(if_stmt));
1373 }
1374
1375 /* create a header-block */
1376 start_block(ctx, header_id);
1377 spirv_builder_emit_selection_merge(&ctx->builder, endif_id,
1378 SpvSelectionControlMaskNone);
1379 branch_conditional(ctx, condition, then_id, else_id);
1380
1381 emit_cf_list(ctx, &if_stmt->then_list);
1382
1383 if (has_else) {
1384 if (ctx->block_started)
1385 branch(ctx, endif_id);
1386
1387 emit_cf_list(ctx, &if_stmt->else_list);
1388 }
1389
1390 start_block(ctx, endif_id);
1391 }
1392
1393 static void
1394 emit_loop(struct ntv_context *ctx, nir_loop *loop)
1395 {
1396 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1397 SpvId begin_id = block_label(ctx, nir_loop_first_block(loop));
1398 SpvId break_id = spirv_builder_new_id(&ctx->builder);
1399 SpvId cont_id = spirv_builder_new_id(&ctx->builder);
1400
1401 /* create a header-block */
1402 start_block(ctx, header_id);
1403 spirv_builder_loop_merge(&ctx->builder, break_id, cont_id, SpvLoopControlMaskNone);
1404 branch(ctx, begin_id);
1405
1406 SpvId save_break = ctx->loop_break;
1407 SpvId save_cont = ctx->loop_cont;
1408 ctx->loop_break = break_id;
1409 ctx->loop_cont = cont_id;
1410
1411 emit_cf_list(ctx, &loop->body);
1412
1413 ctx->loop_break = save_break;
1414 ctx->loop_cont = save_cont;
1415
1416 branch(ctx, cont_id);
1417 start_block(ctx, cont_id);
1418 branch(ctx, header_id);
1419
1420 start_block(ctx, break_id);
1421 }
1422
1423 static void
1424 emit_cf_list(struct ntv_context *ctx, struct exec_list *list)
1425 {
1426 foreach_list_typed(nir_cf_node, node, node, list) {
1427 switch (node->type) {
1428 case nir_cf_node_block:
1429 emit_block(ctx, nir_cf_node_as_block(node));
1430 break;
1431
1432 case nir_cf_node_if:
1433 emit_if(ctx, nir_cf_node_as_if(node));
1434 break;
1435
1436 case nir_cf_node_loop:
1437 emit_loop(ctx, nir_cf_node_as_loop(node));
1438 break;
1439
1440 case nir_cf_node_function:
1441 unreachable("nir_cf_node_function not supported");
1442 break;
1443 }
1444 }
1445 }
1446
1447 struct spirv_shader *
1448 nir_to_spirv(struct nir_shader *s)
1449 {
1450 struct spirv_shader *ret = NULL;
1451
1452 struct ntv_context ctx = {};
1453
1454 switch (s->info.stage) {
1455 case MESA_SHADER_VERTEX:
1456 case MESA_SHADER_FRAGMENT:
1457 case MESA_SHADER_COMPUTE:
1458 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityShader);
1459 break;
1460
1461 case MESA_SHADER_TESS_CTRL:
1462 case MESA_SHADER_TESS_EVAL:
1463 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityTessellation);
1464 break;
1465
1466 case MESA_SHADER_GEOMETRY:
1467 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityGeometry);
1468 break;
1469
1470 default:
1471 unreachable("invalid stage");
1472 }
1473
1474 ctx.stage = s->info.stage;
1475 ctx.GLSL_std_450 = spirv_builder_import(&ctx.builder, "GLSL.std.450");
1476 spirv_builder_emit_source(&ctx.builder, SpvSourceLanguageGLSL, 450);
1477
1478 spirv_builder_emit_mem_model(&ctx.builder, SpvAddressingModelLogical,
1479 SpvMemoryModelGLSL450);
1480
1481 SpvExecutionModel exec_model;
1482 switch (s->info.stage) {
1483 case MESA_SHADER_VERTEX:
1484 exec_model = SpvExecutionModelVertex;
1485 break;
1486 case MESA_SHADER_TESS_CTRL:
1487 exec_model = SpvExecutionModelTessellationControl;
1488 break;
1489 case MESA_SHADER_TESS_EVAL:
1490 exec_model = SpvExecutionModelTessellationEvaluation;
1491 break;
1492 case MESA_SHADER_GEOMETRY:
1493 exec_model = SpvExecutionModelGeometry;
1494 break;
1495 case MESA_SHADER_FRAGMENT:
1496 exec_model = SpvExecutionModelFragment;
1497 break;
1498 case MESA_SHADER_COMPUTE:
1499 exec_model = SpvExecutionModelGLCompute;
1500 break;
1501 default:
1502 unreachable("invalid stage");
1503 }
1504
1505 SpvId type_void = spirv_builder_type_void(&ctx.builder);
1506 SpvId type_main = spirv_builder_type_function(&ctx.builder, type_void,
1507 NULL, 0);
1508 SpvId entry_point = spirv_builder_new_id(&ctx.builder);
1509 spirv_builder_emit_name(&ctx.builder, entry_point, "main");
1510
1511 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1512 _mesa_key_pointer_equal);
1513
1514 nir_foreach_variable(var, &s->inputs)
1515 emit_input(&ctx, var);
1516
1517 nir_foreach_variable(var, &s->outputs)
1518 emit_output(&ctx, var);
1519
1520 nir_foreach_variable(var, &s->uniforms)
1521 emit_uniform(&ctx, var);
1522
1523 spirv_builder_emit_entry_point(&ctx.builder, exec_model, entry_point,
1524 "main", ctx.entry_ifaces,
1525 ctx.num_entry_ifaces);
1526 if (s->info.stage == MESA_SHADER_FRAGMENT) {
1527 spirv_builder_emit_exec_mode(&ctx.builder, entry_point,
1528 SpvExecutionModeOriginUpperLeft);
1529 if (s->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
1530 spirv_builder_emit_exec_mode(&ctx.builder, entry_point,
1531 SpvExecutionModeDepthReplacing);
1532 }
1533
1534
1535 spirv_builder_function(&ctx.builder, entry_point, type_void,
1536 SpvFunctionControlMaskNone,
1537 type_main);
1538
1539 nir_function_impl *entry = nir_shader_get_entrypoint(s);
1540 nir_metadata_require(entry, nir_metadata_block_index);
1541
1542 ctx.defs = (SpvId *)malloc(sizeof(SpvId) * entry->ssa_alloc);
1543 if (!ctx.defs)
1544 goto fail;
1545 ctx.num_defs = entry->ssa_alloc;
1546
1547 nir_index_local_regs(entry);
1548 ctx.regs = malloc(sizeof(SpvId) * entry->reg_alloc);
1549 if (!ctx.regs)
1550 goto fail;
1551 ctx.num_regs = entry->reg_alloc;
1552
1553 SpvId *block_ids = (SpvId *)malloc(sizeof(SpvId) * entry->num_blocks);
1554 if (!block_ids)
1555 goto fail;
1556
1557 for (int i = 0; i < entry->num_blocks; ++i)
1558 block_ids[i] = spirv_builder_new_id(&ctx.builder);
1559
1560 ctx.block_ids = block_ids;
1561 ctx.num_blocks = entry->num_blocks;
1562
1563 /* emit a block only for the variable declarations */
1564 start_block(&ctx, spirv_builder_new_id(&ctx.builder));
1565 foreach_list_typed(nir_register, reg, node, &entry->registers) {
1566 SpvId type = get_uvec_type(&ctx, reg->bit_size, reg->num_components);
1567 SpvId pointer_type = spirv_builder_type_pointer(&ctx.builder,
1568 SpvStorageClassFunction,
1569 type);
1570 SpvId var = spirv_builder_emit_var(&ctx.builder, pointer_type,
1571 SpvStorageClassFunction);
1572
1573 ctx.regs[reg->index] = var;
1574 }
1575
1576 emit_cf_list(&ctx, &entry->body);
1577
1578 free(ctx.defs);
1579
1580 spirv_builder_return(&ctx.builder); // doesn't belong here, but whatevz
1581 spirv_builder_function_end(&ctx.builder);
1582
1583 size_t num_words = spirv_builder_get_num_words(&ctx.builder);
1584
1585 ret = CALLOC_STRUCT(spirv_shader);
1586 if (!ret)
1587 goto fail;
1588
1589 ret->words = MALLOC(sizeof(uint32_t) * num_words);
1590 if (!ret->words)
1591 goto fail;
1592
1593 ret->num_words = spirv_builder_get_words(&ctx.builder, ret->words, num_words);
1594 assert(ret->num_words == num_words);
1595
1596 return ret;
1597
1598 fail:
1599
1600 if (ret)
1601 spirv_shader_delete(ret);
1602
1603 if (ctx.vars)
1604 _mesa_hash_table_destroy(ctx.vars, NULL);
1605
1606 return NULL;
1607 }
1608
1609 void
1610 spirv_shader_delete(struct spirv_shader *s)
1611 {
1612 FREE(s->words);
1613 FREE(s);
1614 }