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