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