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