zink: add shift ops
[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 UNOP(nir_op_f2i32, SpvOpConvertFToS)
790 UNOP(nir_op_f2u32, SpvOpConvertFToU)
791 #undef UNOP
792
793 #define BUILTIN_UNOP(nir_op, spirv_op) \
794 case nir_op: \
795 assert(nir_op_infos[alu->op].num_inputs == 1); \
796 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
797 break;
798
799 BUILTIN_UNOP(nir_op_fabs, GLSLstd450FAbs)
800 BUILTIN_UNOP(nir_op_fsqrt, GLSLstd450Sqrt)
801 BUILTIN_UNOP(nir_op_frsq, GLSLstd450InverseSqrt)
802 BUILTIN_UNOP(nir_op_flog2, GLSLstd450Log2)
803 BUILTIN_UNOP(nir_op_fexp2, GLSLstd450Exp2)
804 BUILTIN_UNOP(nir_op_ffract, GLSLstd450Fract)
805 BUILTIN_UNOP(nir_op_ffloor, GLSLstd450Floor)
806 BUILTIN_UNOP(nir_op_fceil, GLSLstd450Ceil)
807 BUILTIN_UNOP(nir_op_ftrunc, GLSLstd450Trunc)
808 BUILTIN_UNOP(nir_op_fround_even, GLSLstd450RoundEven)
809 BUILTIN_UNOP(nir_op_fsign, GLSLstd450FSign)
810 BUILTIN_UNOP(nir_op_fsin, GLSLstd450Sin)
811 BUILTIN_UNOP(nir_op_fcos, GLSLstd450Cos)
812 #undef BUILTIN_UNOP
813
814 case nir_op_frcp: {
815 assert(nir_op_infos[alu->op].num_inputs == 1);
816 float one[4] = { 1, 1, 1, 1 };
817 src[1] = src[0];
818 src[0] = get_fvec_constant(ctx, bit_size, num_components, one);
819 result = emit_binop(ctx, SpvOpFDiv, dest_type, src[0], src[1]);
820 }
821 break;
822
823 #define BINOP(nir_op, spirv_op) \
824 case nir_op: \
825 assert(nir_op_infos[alu->op].num_inputs == 2); \
826 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
827 break;
828
829 BINOP(nir_op_iadd, SpvOpIAdd)
830 BINOP(nir_op_isub, SpvOpISub)
831 BINOP(nir_op_imul, SpvOpIMul)
832 BINOP(nir_op_idiv, SpvOpSDiv)
833 BINOP(nir_op_udiv, SpvOpUDiv)
834 BINOP(nir_op_fadd, SpvOpFAdd)
835 BINOP(nir_op_fsub, SpvOpFSub)
836 BINOP(nir_op_fmul, SpvOpFMul)
837 BINOP(nir_op_fdiv, SpvOpFDiv)
838 BINOP(nir_op_fmod, SpvOpFMod)
839 BINOP(nir_op_flt, SpvOpFUnordLessThan)
840 BINOP(nir_op_fge, SpvOpFUnordGreaterThanEqual)
841 BINOP(nir_op_ishl, SpvOpShiftLeftLogical)
842 BINOP(nir_op_ishr, SpvOpShiftRightArithmetic)
843 BINOP(nir_op_ushr, SpvOpShiftRightLogical)
844 #undef BINOP
845
846 #define BUILTIN_BINOP(nir_op, spirv_op) \
847 case nir_op: \
848 assert(nir_op_infos[alu->op].num_inputs == 2); \
849 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
850 break;
851
852 BUILTIN_BINOP(nir_op_fmin, GLSLstd450FMin)
853 BUILTIN_BINOP(nir_op_fmax, GLSLstd450FMax)
854 #undef BUILTIN_BINOP
855
856 case nir_op_fdot2:
857 case nir_op_fdot3:
858 case nir_op_fdot4:
859 assert(nir_op_infos[alu->op].num_inputs == 2);
860 result = emit_binop(ctx, SpvOpDot, dest_type, src[0], src[1]);
861 break;
862
863 case nir_op_seq:
864 case nir_op_sne:
865 case nir_op_slt:
866 case nir_op_sge: {
867 assert(nir_op_infos[alu->op].num_inputs == 2);
868 int num_components = nir_dest_num_components(alu->dest.dest);
869 SpvId bool_type = get_bvec_type(ctx, num_components);
870
871 SpvId zero = spirv_builder_const_float(&ctx->builder, 32, 0.0f);
872 SpvId one = spirv_builder_const_float(&ctx->builder, 32, 1.0f);
873 if (num_components > 1) {
874 SpvId zero_comps[num_components], one_comps[num_components];
875 for (int i = 0; i < num_components; i++) {
876 zero_comps[i] = zero;
877 one_comps[i] = one;
878 }
879
880 zero = spirv_builder_const_composite(&ctx->builder, dest_type,
881 zero_comps, num_components);
882 one = spirv_builder_const_composite(&ctx->builder, dest_type,
883 one_comps, num_components);
884 }
885
886 SpvOp op;
887 switch (alu->op) {
888 case nir_op_seq: op = SpvOpFOrdEqual; break;
889 case nir_op_sne: op = SpvOpFOrdNotEqual; break;
890 case nir_op_slt: op = SpvOpFOrdLessThan; break;
891 case nir_op_sge: op = SpvOpFOrdGreaterThanEqual; break;
892 default: unreachable("unexpected op");
893 }
894
895 result = emit_binop(ctx, op, bool_type, src[0], src[1]);
896 result = emit_triop(ctx, SpvOpSelect, dest_type, result, one, zero);
897 }
898 break;
899
900 case nir_op_fcsel: {
901 assert(nir_op_infos[alu->op].num_inputs == 3);
902 int num_components = nir_dest_num_components(alu->dest.dest);
903 SpvId bool_type = get_bvec_type(ctx, num_components);
904
905 float zero[4] = { 0, 0, 0, 0 };
906 SpvId cmp = get_fvec_constant(ctx, nir_src_bit_size(alu->src[0].src),
907 num_components, zero);
908
909 result = emit_binop(ctx, SpvOpFOrdGreaterThan, bool_type, src[0], cmp);
910 result = emit_triop(ctx, SpvOpSelect, dest_type, result, src[1], src[2]);
911 }
912 break;
913
914 case nir_op_vec2:
915 case nir_op_vec3:
916 case nir_op_vec4: {
917 int num_inputs = nir_op_infos[alu->op].num_inputs;
918 assert(2 <= num_inputs && num_inputs <= 4);
919 result = spirv_builder_emit_composite_construct(&ctx->builder, dest_type,
920 src, num_inputs);
921 }
922 break;
923
924 default:
925 fprintf(stderr, "emit_alu: not implemented (%s)\n",
926 nir_op_infos[alu->op].name);
927
928 unreachable("unsupported opcode");
929 return;
930 }
931
932 store_alu_result(ctx, alu, result);
933 }
934
935 static void
936 emit_load_const(struct ntv_context *ctx, nir_load_const_instr *load_const)
937 {
938 uint32_t values[NIR_MAX_VEC_COMPONENTS];
939 for (int i = 0; i < load_const->def.num_components; ++i)
940 values[i] = load_const->value[i].u32;
941
942 SpvId constant = get_uvec_constant(ctx, load_const->def.bit_size,
943 load_const->def.num_components,
944 values);
945 store_ssa_def_uint(ctx, &load_const->def, constant);
946 }
947
948 static void
949 emit_load_ubo(struct ntv_context *ctx, nir_intrinsic_instr *intr)
950 {
951 nir_const_value *const_block_index = nir_src_as_const_value(intr->src[0]);
952 assert(const_block_index); // no dynamic indexing for now
953 assert(const_block_index->u32 == 0); // we only support the default UBO for now
954
955 nir_const_value *const_offset = nir_src_as_const_value(intr->src[1]);
956 if (const_offset) {
957 SpvId uvec4_type = get_uvec_type(ctx, 32, 4);
958 SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
959 SpvStorageClassUniform,
960 uvec4_type);
961
962 unsigned idx = const_offset->u32;
963 SpvId member = spirv_builder_const_uint(&ctx->builder, 32, 0);
964 SpvId offset = spirv_builder_const_uint(&ctx->builder, 32, idx);
965 SpvId offsets[] = { member, offset };
966 SpvId ptr = spirv_builder_emit_access_chain(&ctx->builder, pointer_type,
967 ctx->ubos[0], offsets,
968 ARRAY_SIZE(offsets));
969 SpvId result = spirv_builder_emit_load(&ctx->builder, uvec4_type, ptr);
970
971 SpvId type = get_dest_uvec_type(ctx, &intr->dest);
972 unsigned num_components = nir_dest_num_components(intr->dest);
973 if (num_components == 1) {
974 uint32_t components[] = { 0 };
975 result = spirv_builder_emit_composite_extract(&ctx->builder,
976 type,
977 result, components,
978 1);
979 } else if (num_components < 4) {
980 SpvId constituents[num_components];
981 SpvId uint_type = spirv_builder_type_uint(&ctx->builder, 32);
982 for (uint32_t i = 0; i < num_components; ++i)
983 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
984 uint_type,
985 result, &i,
986 1);
987
988 result = spirv_builder_emit_composite_construct(&ctx->builder,
989 type,
990 constituents,
991 num_components);
992 }
993
994 store_dest_uint(ctx, &intr->dest, result);
995 } else
996 unreachable("uniform-addressing not yet supported");
997 }
998
999 static void
1000 emit_discard(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1001 {
1002 assert(ctx->block_started);
1003 spirv_builder_emit_kill(&ctx->builder);
1004 /* discard is weird in NIR, so let's just create an unreachable block after
1005 it and hope that the vulkan driver will DCE any instructinos in it. */
1006 spirv_builder_label(&ctx->builder, spirv_builder_new_id(&ctx->builder));
1007 }
1008
1009 static void
1010 emit_load_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1011 {
1012 /* uint is a bit of a lie here; it's really just a pointer */
1013 SpvId ptr = get_src_uint(ctx, intr->src);
1014
1015 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1016 SpvId result = spirv_builder_emit_load(&ctx->builder,
1017 get_glsl_type(ctx, var->type),
1018 ptr);
1019 unsigned num_components = nir_dest_num_components(intr->dest);
1020 unsigned bit_size = nir_dest_bit_size(intr->dest);
1021 result = bitcast_to_uvec(ctx, result, bit_size, num_components);
1022 store_dest_uint(ctx, &intr->dest, result);
1023 }
1024
1025 static void
1026 emit_store_deref(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1027 {
1028 /* uint is a bit of a lie here; it's really just a pointer */
1029 SpvId ptr = get_src_uint(ctx, &intr->src[0]);
1030 SpvId src = get_src_uint(ctx, &intr->src[1]);
1031
1032 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1033 SpvId result = emit_unop(ctx, SpvOpBitcast,
1034 get_glsl_type(ctx, glsl_without_array(var->type)),
1035 src);
1036 spirv_builder_emit_store(&ctx->builder, ptr, result);
1037 }
1038
1039 static void
1040 emit_intrinsic(struct ntv_context *ctx, nir_intrinsic_instr *intr)
1041 {
1042 switch (intr->intrinsic) {
1043 case nir_intrinsic_load_ubo:
1044 emit_load_ubo(ctx, intr);
1045 break;
1046
1047 case nir_intrinsic_discard:
1048 emit_discard(ctx, intr);
1049 break;
1050
1051 case nir_intrinsic_load_deref:
1052 emit_load_deref(ctx, intr);
1053 break;
1054
1055 case nir_intrinsic_store_deref:
1056 emit_store_deref(ctx, intr);
1057 break;
1058
1059 default:
1060 fprintf(stderr, "emit_intrinsic: not implemented (%s)\n",
1061 nir_intrinsic_infos[intr->intrinsic].name);
1062 unreachable("unsupported intrinsic");
1063 }
1064 }
1065
1066 static void
1067 emit_undef(struct ntv_context *ctx, nir_ssa_undef_instr *undef)
1068 {
1069 SpvId type = get_uvec_type(ctx, undef->def.bit_size,
1070 undef->def.num_components);
1071
1072 store_ssa_def_uint(ctx, &undef->def,
1073 spirv_builder_emit_undef(&ctx->builder, type));
1074 }
1075
1076 static SpvId
1077 get_src_float(struct ntv_context *ctx, nir_src *src)
1078 {
1079 SpvId def = get_src_uint(ctx, src);
1080 unsigned num_components = nir_src_num_components(*src);
1081 unsigned bit_size = nir_src_bit_size(*src);
1082 return bitcast_to_fvec(ctx, def, bit_size, num_components);
1083 }
1084
1085 static void
1086 emit_tex(struct ntv_context *ctx, nir_tex_instr *tex)
1087 {
1088 assert(tex->op == nir_texop_tex);
1089 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
1090 assert(tex->texture_index == tex->sampler_index);
1091
1092 bool has_proj = false, has_lod = false;
1093 SpvId coord = 0, proj, lod;
1094 unsigned coord_components;
1095 for (unsigned i = 0; i < tex->num_srcs; i++) {
1096 switch (tex->src[i].src_type) {
1097 case nir_tex_src_coord:
1098 coord = get_src_float(ctx, &tex->src[i].src);
1099 coord_components = nir_src_num_components(tex->src[i].src);
1100 break;
1101
1102 case nir_tex_src_projector:
1103 has_proj = true;
1104 proj = get_src_float(ctx, &tex->src[i].src);
1105 assert(nir_src_num_components(tex->src[i].src) == 1);
1106 break;
1107
1108 case nir_tex_src_lod:
1109 has_lod = true;
1110 lod = get_src_float(ctx, &tex->src[i].src);
1111 assert(nir_src_num_components(tex->src[i].src) == 1);
1112 break;
1113
1114 default:
1115 fprintf(stderr, "texture source: %d\n", tex->src[i].src_type);
1116 unreachable("unknown texture source");
1117 }
1118 }
1119
1120 if (!has_lod && ctx->stage != MESA_SHADER_FRAGMENT) {
1121 has_lod = true;
1122 lod = spirv_builder_const_float(&ctx->builder, 32, 0);
1123 }
1124
1125 bool is_ms;
1126 SpvDim dimension = type_to_dim(tex->sampler_dim, &is_ms);
1127 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1128 SpvId image_type = spirv_builder_type_image(&ctx->builder, float_type,
1129 dimension, false, tex->is_array, is_ms, 1,
1130 SpvImageFormatUnknown);
1131 SpvId sampled_type = spirv_builder_type_sampled_image(&ctx->builder,
1132 image_type);
1133
1134 assert(tex->texture_index < ctx->num_samplers);
1135 SpvId load = spirv_builder_emit_load(&ctx->builder, sampled_type,
1136 ctx->samplers[tex->texture_index]);
1137
1138 SpvId dest_type = get_dest_type(ctx, &tex->dest, tex->dest_type);
1139
1140 SpvId result;
1141 if (has_proj) {
1142 SpvId constituents[coord_components + 1];
1143 SpvId float_type = spirv_builder_type_float(&ctx->builder, 32);
1144 for (uint32_t i = 0; i < coord_components; ++i)
1145 constituents[i] = spirv_builder_emit_composite_extract(&ctx->builder,
1146 float_type,
1147 coord,
1148 &i, 1);
1149
1150 constituents[coord_components++] = proj;
1151
1152 SpvId vec_type = get_fvec_type(ctx, 32, coord_components);
1153 SpvId merged = spirv_builder_emit_composite_construct(&ctx->builder,
1154 vec_type,
1155 constituents,
1156 coord_components);
1157
1158 if (has_lod)
1159 result = spirv_builder_emit_image_sample_proj_explicit_lod(&ctx->builder,
1160 dest_type,
1161 load,
1162 merged,
1163 lod);
1164 else
1165 result = spirv_builder_emit_image_sample_proj_implicit_lod(&ctx->builder,
1166 dest_type,
1167 load,
1168 merged);
1169 } else {
1170 if (has_lod)
1171 result = spirv_builder_emit_image_sample_explicit_lod(&ctx->builder,
1172 dest_type,
1173 load,
1174 coord, lod);
1175 else
1176 result = spirv_builder_emit_image_sample_implicit_lod(&ctx->builder,
1177 dest_type,
1178 load,
1179 coord);
1180 }
1181 spirv_builder_emit_decoration(&ctx->builder, result,
1182 SpvDecorationRelaxedPrecision);
1183
1184 store_dest(ctx, &tex->dest, result, tex->dest_type);
1185 }
1186
1187 static void
1188 start_block(struct ntv_context *ctx, SpvId label)
1189 {
1190 /* terminate previous block if needed */
1191 if (ctx->block_started)
1192 spirv_builder_emit_branch(&ctx->builder, label);
1193
1194 /* start new block */
1195 spirv_builder_label(&ctx->builder, label);
1196 ctx->block_started = true;
1197 }
1198
1199 static void
1200 branch(struct ntv_context *ctx, SpvId label)
1201 {
1202 assert(ctx->block_started);
1203 spirv_builder_emit_branch(&ctx->builder, label);
1204 ctx->block_started = false;
1205 }
1206
1207 static void
1208 branch_conditional(struct ntv_context *ctx, SpvId condition, SpvId then_id,
1209 SpvId else_id)
1210 {
1211 assert(ctx->block_started);
1212 spirv_builder_emit_branch_conditional(&ctx->builder, condition,
1213 then_id, else_id);
1214 ctx->block_started = false;
1215 }
1216
1217 static void
1218 emit_jump(struct ntv_context *ctx, nir_jump_instr *jump)
1219 {
1220 switch (jump->type) {
1221 case nir_jump_break:
1222 assert(ctx->loop_break);
1223 branch(ctx, ctx->loop_break);
1224 break;
1225
1226 case nir_jump_continue:
1227 assert(ctx->loop_cont);
1228 branch(ctx, ctx->loop_cont);
1229 break;
1230
1231 default:
1232 unreachable("Unsupported jump type\n");
1233 }
1234 }
1235
1236 static void
1237 emit_deref_var(struct ntv_context *ctx, nir_deref_instr *deref)
1238 {
1239 assert(deref->deref_type == nir_deref_type_var);
1240
1241 struct hash_entry *he = _mesa_hash_table_search(ctx->vars, deref->var);
1242 assert(he);
1243 SpvId result = (SpvId)(intptr_t)he->data;
1244 /* uint is a bit of a lie here, it's really just an opaque type */
1245 store_dest_uint(ctx, &deref->dest, result);
1246 }
1247
1248 static void
1249 emit_deref_array(struct ntv_context *ctx, nir_deref_instr *deref)
1250 {
1251 assert(deref->deref_type == nir_deref_type_array);
1252 nir_variable *var = nir_deref_instr_get_variable(deref);
1253
1254 SpvStorageClass storage_class;
1255 switch (var->data.mode) {
1256 case nir_var_shader_in:
1257 storage_class = SpvStorageClassInput;
1258 break;
1259
1260 case nir_var_shader_out:
1261 storage_class = SpvStorageClassOutput;
1262 break;
1263
1264 default:
1265 unreachable("Unsupported nir_variable_mode\n");
1266 }
1267
1268 SpvId index = get_src_uint(ctx, &deref->arr.index);
1269
1270 SpvId ptr_type = spirv_builder_type_pointer(&ctx->builder,
1271 storage_class,
1272 get_glsl_type(ctx, deref->type));
1273
1274 SpvId result = spirv_builder_emit_access_chain(&ctx->builder,
1275 ptr_type,
1276 get_src_uint(ctx, &deref->parent),
1277 &index, 1);
1278 /* uint is a bit of a lie here, it's really just an opaque type */
1279 store_dest_uint(ctx, &deref->dest, result);
1280 }
1281
1282 static void
1283 emit_deref(struct ntv_context *ctx, nir_deref_instr *deref)
1284 {
1285 switch (deref->deref_type) {
1286 case nir_deref_type_var:
1287 emit_deref_var(ctx, deref);
1288 break;
1289
1290 case nir_deref_type_array:
1291 emit_deref_array(ctx, deref);
1292 break;
1293
1294 default:
1295 unreachable("unexpected deref_type");
1296 }
1297 }
1298
1299 static void
1300 emit_block(struct ntv_context *ctx, struct nir_block *block)
1301 {
1302 start_block(ctx, block_label(ctx, block));
1303 nir_foreach_instr(instr, block) {
1304 switch (instr->type) {
1305 case nir_instr_type_alu:
1306 emit_alu(ctx, nir_instr_as_alu(instr));
1307 break;
1308 case nir_instr_type_intrinsic:
1309 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1310 break;
1311 case nir_instr_type_load_const:
1312 emit_load_const(ctx, nir_instr_as_load_const(instr));
1313 break;
1314 case nir_instr_type_ssa_undef:
1315 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1316 break;
1317 case nir_instr_type_tex:
1318 emit_tex(ctx, nir_instr_as_tex(instr));
1319 break;
1320 case nir_instr_type_phi:
1321 unreachable("nir_instr_type_phi not supported");
1322 break;
1323 case nir_instr_type_jump:
1324 emit_jump(ctx, nir_instr_as_jump(instr));
1325 break;
1326 case nir_instr_type_call:
1327 unreachable("nir_instr_type_call not supported");
1328 break;
1329 case nir_instr_type_parallel_copy:
1330 unreachable("nir_instr_type_parallel_copy not supported");
1331 break;
1332 case nir_instr_type_deref:
1333 emit_deref(ctx, nir_instr_as_deref(instr));
1334 break;
1335 }
1336 }
1337 }
1338
1339 static void
1340 emit_cf_list(struct ntv_context *ctx, struct exec_list *list);
1341
1342 static SpvId
1343 get_src_bool(struct ntv_context *ctx, nir_src *src)
1344 {
1345 SpvId def = get_src_uint(ctx, src);
1346 assert(nir_src_bit_size(*src) == 32);
1347 unsigned num_components = nir_src_num_components(*src);
1348 return uvec_to_bvec(ctx, def, num_components);
1349 }
1350
1351 static void
1352 emit_if(struct ntv_context *ctx, nir_if *if_stmt)
1353 {
1354 SpvId condition = get_src_bool(ctx, &if_stmt->condition);
1355
1356 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1357 SpvId then_id = block_label(ctx, nir_if_first_then_block(if_stmt));
1358 SpvId endif_id = spirv_builder_new_id(&ctx->builder);
1359 SpvId else_id = endif_id;
1360
1361 bool has_else = !exec_list_is_empty(&if_stmt->else_list);
1362 if (has_else) {
1363 assert(nir_if_first_else_block(if_stmt)->index < ctx->num_blocks);
1364 else_id = block_label(ctx, nir_if_first_else_block(if_stmt));
1365 }
1366
1367 /* create a header-block */
1368 start_block(ctx, header_id);
1369 spirv_builder_emit_selection_merge(&ctx->builder, endif_id,
1370 SpvSelectionControlMaskNone);
1371 branch_conditional(ctx, condition, then_id, else_id);
1372
1373 emit_cf_list(ctx, &if_stmt->then_list);
1374
1375 if (has_else) {
1376 if (ctx->block_started)
1377 branch(ctx, endif_id);
1378
1379 emit_cf_list(ctx, &if_stmt->else_list);
1380 }
1381
1382 start_block(ctx, endif_id);
1383 }
1384
1385 static void
1386 emit_loop(struct ntv_context *ctx, nir_loop *loop)
1387 {
1388 SpvId header_id = spirv_builder_new_id(&ctx->builder);
1389 SpvId begin_id = block_label(ctx, nir_loop_first_block(loop));
1390 SpvId break_id = spirv_builder_new_id(&ctx->builder);
1391 SpvId cont_id = spirv_builder_new_id(&ctx->builder);
1392
1393 /* create a header-block */
1394 start_block(ctx, header_id);
1395 spirv_builder_loop_merge(&ctx->builder, break_id, cont_id, SpvLoopControlMaskNone);
1396 branch(ctx, begin_id);
1397
1398 SpvId save_break = ctx->loop_break;
1399 SpvId save_cont = ctx->loop_cont;
1400 ctx->loop_break = break_id;
1401 ctx->loop_cont = cont_id;
1402
1403 emit_cf_list(ctx, &loop->body);
1404
1405 ctx->loop_break = save_break;
1406 ctx->loop_cont = save_cont;
1407
1408 branch(ctx, cont_id);
1409 start_block(ctx, cont_id);
1410 branch(ctx, header_id);
1411
1412 start_block(ctx, break_id);
1413 }
1414
1415 static void
1416 emit_cf_list(struct ntv_context *ctx, struct exec_list *list)
1417 {
1418 foreach_list_typed(nir_cf_node, node, node, list) {
1419 switch (node->type) {
1420 case nir_cf_node_block:
1421 emit_block(ctx, nir_cf_node_as_block(node));
1422 break;
1423
1424 case nir_cf_node_if:
1425 emit_if(ctx, nir_cf_node_as_if(node));
1426 break;
1427
1428 case nir_cf_node_loop:
1429 emit_loop(ctx, nir_cf_node_as_loop(node));
1430 break;
1431
1432 case nir_cf_node_function:
1433 unreachable("nir_cf_node_function not supported");
1434 break;
1435 }
1436 }
1437 }
1438
1439 struct spirv_shader *
1440 nir_to_spirv(struct nir_shader *s)
1441 {
1442 struct spirv_shader *ret = NULL;
1443
1444 struct ntv_context ctx = {};
1445
1446 switch (s->info.stage) {
1447 case MESA_SHADER_VERTEX:
1448 case MESA_SHADER_FRAGMENT:
1449 case MESA_SHADER_COMPUTE:
1450 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityShader);
1451 break;
1452
1453 case MESA_SHADER_TESS_CTRL:
1454 case MESA_SHADER_TESS_EVAL:
1455 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityTessellation);
1456 break;
1457
1458 case MESA_SHADER_GEOMETRY:
1459 spirv_builder_emit_cap(&ctx.builder, SpvCapabilityGeometry);
1460 break;
1461
1462 default:
1463 unreachable("invalid stage");
1464 }
1465
1466 ctx.stage = s->info.stage;
1467 ctx.GLSL_std_450 = spirv_builder_import(&ctx.builder, "GLSL.std.450");
1468 spirv_builder_emit_source(&ctx.builder, SpvSourceLanguageGLSL, 450);
1469
1470 spirv_builder_emit_mem_model(&ctx.builder, SpvAddressingModelLogical,
1471 SpvMemoryModelGLSL450);
1472
1473 SpvExecutionModel exec_model;
1474 switch (s->info.stage) {
1475 case MESA_SHADER_VERTEX:
1476 exec_model = SpvExecutionModelVertex;
1477 break;
1478 case MESA_SHADER_TESS_CTRL:
1479 exec_model = SpvExecutionModelTessellationControl;
1480 break;
1481 case MESA_SHADER_TESS_EVAL:
1482 exec_model = SpvExecutionModelTessellationEvaluation;
1483 break;
1484 case MESA_SHADER_GEOMETRY:
1485 exec_model = SpvExecutionModelGeometry;
1486 break;
1487 case MESA_SHADER_FRAGMENT:
1488 exec_model = SpvExecutionModelFragment;
1489 break;
1490 case MESA_SHADER_COMPUTE:
1491 exec_model = SpvExecutionModelGLCompute;
1492 break;
1493 default:
1494 unreachable("invalid stage");
1495 }
1496
1497 SpvId type_void = spirv_builder_type_void(&ctx.builder);
1498 SpvId type_main = spirv_builder_type_function(&ctx.builder, type_void,
1499 NULL, 0);
1500 SpvId entry_point = spirv_builder_new_id(&ctx.builder);
1501 spirv_builder_emit_name(&ctx.builder, entry_point, "main");
1502
1503 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1504 _mesa_key_pointer_equal);
1505
1506 nir_foreach_variable(var, &s->inputs)
1507 emit_input(&ctx, var);
1508
1509 nir_foreach_variable(var, &s->outputs)
1510 emit_output(&ctx, var);
1511
1512 nir_foreach_variable(var, &s->uniforms)
1513 emit_uniform(&ctx, var);
1514
1515 spirv_builder_emit_entry_point(&ctx.builder, exec_model, entry_point,
1516 "main", ctx.entry_ifaces,
1517 ctx.num_entry_ifaces);
1518 if (s->info.stage == MESA_SHADER_FRAGMENT)
1519 spirv_builder_emit_exec_mode(&ctx.builder, entry_point,
1520 SpvExecutionModeOriginUpperLeft);
1521
1522
1523 spirv_builder_function(&ctx.builder, entry_point, type_void,
1524 SpvFunctionControlMaskNone,
1525 type_main);
1526
1527 nir_function_impl *entry = nir_shader_get_entrypoint(s);
1528 nir_metadata_require(entry, nir_metadata_block_index);
1529
1530 ctx.defs = (SpvId *)malloc(sizeof(SpvId) * entry->ssa_alloc);
1531 if (!ctx.defs)
1532 goto fail;
1533 ctx.num_defs = entry->ssa_alloc;
1534
1535 nir_index_local_regs(entry);
1536 ctx.regs = malloc(sizeof(SpvId) * entry->reg_alloc);
1537 if (!ctx.regs)
1538 goto fail;
1539 ctx.num_regs = entry->reg_alloc;
1540
1541 SpvId *block_ids = (SpvId *)malloc(sizeof(SpvId) * entry->num_blocks);
1542 if (!block_ids)
1543 goto fail;
1544
1545 for (int i = 0; i < entry->num_blocks; ++i)
1546 block_ids[i] = spirv_builder_new_id(&ctx.builder);
1547
1548 ctx.block_ids = block_ids;
1549 ctx.num_blocks = entry->num_blocks;
1550
1551 /* emit a block only for the variable declarations */
1552 start_block(&ctx, spirv_builder_new_id(&ctx.builder));
1553 foreach_list_typed(nir_register, reg, node, &entry->registers) {
1554 SpvId type = get_uvec_type(&ctx, reg->bit_size, reg->num_components);
1555 SpvId pointer_type = spirv_builder_type_pointer(&ctx.builder,
1556 SpvStorageClassFunction,
1557 type);
1558 SpvId var = spirv_builder_emit_var(&ctx.builder, pointer_type,
1559 SpvStorageClassFunction);
1560
1561 ctx.regs[reg->index] = var;
1562 }
1563
1564 emit_cf_list(&ctx, &entry->body);
1565
1566 free(ctx.defs);
1567
1568 spirv_builder_return(&ctx.builder); // doesn't belong here, but whatevz
1569 spirv_builder_function_end(&ctx.builder);
1570
1571 size_t num_words = spirv_builder_get_num_words(&ctx.builder);
1572
1573 ret = CALLOC_STRUCT(spirv_shader);
1574 if (!ret)
1575 goto fail;
1576
1577 ret->words = MALLOC(sizeof(uint32_t) * num_words);
1578 if (!ret->words)
1579 goto fail;
1580
1581 ret->num_words = spirv_builder_get_words(&ctx.builder, ret->words, num_words);
1582 assert(ret->num_words == num_words);
1583
1584 return ret;
1585
1586 fail:
1587
1588 if (ret)
1589 spirv_shader_delete(ret);
1590
1591 if (ctx.vars)
1592 _mesa_hash_table_destroy(ctx.vars, NULL);
1593
1594 return NULL;
1595 }
1596
1597 void
1598 spirv_shader_delete(struct spirv_shader *s)
1599 {
1600 FREE(s->words);
1601 FREE(s);
1602 }