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