nir_to_spirv.c 142 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright 2018 Collabora Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "nir_to_spirv.h"
#include "spirv_builder.h"

#include "nir.h"
#include "pipe/p_state.h"
29
#include "util/u_math.h"
30
#include "util/u_memory.h"
Erik Faye-Lund's avatar
Erik Faye-Lund committed
31
#include "util/hash_table.h"
32

33
#define SLOT_UNSET ((unsigned char) -1)
34

35
struct ntv_context {
36
37
   void *mem_ctx;

38
39
40
41
42
   struct spirv_builder builder;

   SpvId GLSL_std_450;

   gl_shader_stage stage;
43
   const struct zink_so_info *so_info;
44

45
46
   SpvId ubos[2];
   SpvId ubo_type;
47

48
49
   SpvId ssbo;
   SpvId ssbo_type;
50
   SpvId image_types[PIPE_MAX_SAMPLERS];
51
   SpvId images[PIPE_MAX_SAMPLERS];
52
   SpvId sampler_types[PIPE_MAX_SAMPLERS];
53
   SpvId samplers[PIPE_MAX_SAMPLERS];
54
   unsigned char sampler_array_sizes[PIPE_MAX_SAMPLERS];
Erik Faye-Lund's avatar
Erik Faye-Lund committed
55
   unsigned samplers_used : PIPE_MAX_SAMPLERS;
56
57
58
59
60
   SpvId entry_ifaces[PIPE_MAX_SHADER_INPUTS * 4 + PIPE_MAX_SHADER_OUTPUTS * 4];
   size_t num_entry_ifaces;

   SpvId *defs;
   size_t num_defs;
61

Erik Faye-Lund's avatar
Erik Faye-Lund committed
62
63
   SpvId *regs;
   size_t num_regs;
64

Erik Faye-Lund's avatar
Erik Faye-Lund committed
65
   struct hash_table *vars; /* nir_variable -> SpvId */
66
   struct hash_table *image_vars; /* SpvId -> nir_variable */
67
   struct hash_table *so_outputs; /* pipe_stream_output -> SpvId */
68
69
70
   unsigned outputs[VARYING_SLOT_MAX * 4];
   const struct glsl_type *so_output_gl_types[VARYING_SLOT_MAX * 4];
   SpvId so_output_types[VARYING_SLOT_MAX * 4];
Erik Faye-Lund's avatar
Erik Faye-Lund committed
71

72
73
74
   const SpvId *block_ids;
   size_t num_blocks;
   bool block_started;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
75
   SpvId loop_break, loop_cont;
76

77
78
79
   unsigned char *shader_slot_map;
   unsigned char shader_slots_reserved;

80
   SpvId front_face_var, instance_id_var, vertex_id_var,
81
         primitive_id_var, invocation_id_var, // geometry
82
         sample_mask_type, sample_id_var, sample_pos_var, sample_mask_in_var,
83
         tess_patch_vertices_in, tess_coord_var, // tess
84
         push_const_var,
85
86
         workgroup_id_var, num_workgroups_var,
         local_invocation_id_var, global_invocation_id_var,
87
         local_invocation_index_var, helper_invocation_var,
88
         local_group_size_var,
89
90
         shared_block_var,
         base_vertex_var, base_instance_var, draw_id_var;
91
92
};

93
static SpvId
94
get_fvec_constant(struct ntv_context *ctx, unsigned bit_size,
95
                  unsigned num_components, double value);
96
97

static SpvId
98
get_uvec_constant(struct ntv_context *ctx, unsigned bit_size,
99
                  unsigned num_components, uint64_t value);
100

Erik Faye-Lund's avatar
Erik Faye-Lund committed
101
102
static SpvId
get_ivec_constant(struct ntv_context *ctx, unsigned bit_size,
103
                  unsigned num_components, int64_t value);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
104

105
106
107
108
109
110
111
112
113
114
115
static SpvId
emit_unop(struct ntv_context *ctx, SpvOp op, SpvId type, SpvId src);

static SpvId
emit_binop(struct ntv_context *ctx, SpvOp op, SpvId type,
           SpvId src0, SpvId src1);

static SpvId
emit_triop(struct ntv_context *ctx, SpvOp op, SpvId type,
           SpvId src0, SpvId src1, SpvId src2);

116
117
118
119
120
121
122
123
124
125
126
127
static SpvId
get_bvec_type(struct ntv_context *ctx, int num_components)
{
   SpvId bool_type = spirv_builder_type_bool(&ctx->builder);
   if (num_components > 1)
      return spirv_builder_type_vector(&ctx->builder, bool_type,
                                       num_components);

   assert(num_components == 1);
   return bool_type;
}

128
129
130
131
132
133
134
static SpvId
block_label(struct ntv_context *ctx, nir_block *block)
{
   assert(block->index < ctx->num_blocks);
   return ctx->block_ids[block->index];
}

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
static void
emit_access_decorations(struct ntv_context *ctx, nir_variable *var, SpvId var_id)
{
    enum gl_access_qualifier access = var->data.access;
    while (access) {
       unsigned bit = u_bit_scan(&access);
       switch (1 << bit) {
       case ACCESS_COHERENT:
          /* SpvDecorationCoherent can't be used with vulkan memory model */
          break;
       case ACCESS_RESTRICT:
          spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationRestrict);
          break;
       case ACCESS_VOLATILE:
          /* SpvDecorationVolatile can't be used with vulkan memory model */
          break;
       case ACCESS_NON_READABLE:
          spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationNonReadable);
          break;
       case ACCESS_NON_WRITEABLE:
          spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationNonWritable);
          break;
       case ACCESS_NON_UNIFORM:
          spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationNonUniform);
          break;
       case ACCESS_CAN_REORDER:
       case ACCESS_STREAM_CACHE_POLICY:
          /* no equivalent */
          break;
       default:
          unreachable("unknown access bit");
       }
    }
}

170
171
172
173
static SpvOp
get_atomic_op(nir_intrinsic_op op)
{
   switch (op) {
174
175
#define CASE_ATOMIC_OP(type) \
   case nir_intrinsic_ssbo_atomic_##type: \
176
177
   case nir_intrinsic_image_deref_atomic_##type: \
   case nir_intrinsic_shared_atomic_##type
178
179

   CASE_ATOMIC_OP(add):
180
      return SpvOpAtomicIAdd;
181
   CASE_ATOMIC_OP(umin):
182
      return SpvOpAtomicUMin;
183
   CASE_ATOMIC_OP(imin):
184
      return SpvOpAtomicSMin;
185
   CASE_ATOMIC_OP(umax):
186
      return SpvOpAtomicUMax;
187
   CASE_ATOMIC_OP(imax):
188
      return SpvOpAtomicSMax;
189
   CASE_ATOMIC_OP(and):
190
      return SpvOpAtomicAnd;
191
   CASE_ATOMIC_OP(or):
192
      return SpvOpAtomicOr;
193
   CASE_ATOMIC_OP(xor):
194
      return SpvOpAtomicXor;
195
   CASE_ATOMIC_OP(exchange):
196
      return SpvOpAtomicExchange;
197
   CASE_ATOMIC_OP(comp_swap):
198
199
      return SpvOpAtomicCompareExchange;
   default:
200
      debug_printf("%s - ", nir_intrinsic_infos[op].name);
201
202
203
204
      unreachable("unhandled atomic op");
   }
   return 0;
}
205
#undef CASE_ATOMIC_OP
206
static SpvId
207
emit_float_const(struct ntv_context *ctx, int bit_size, double value)
208
{
209
   assert(bit_size == 32 || bit_size == 64);
210
211
212
213
   return spirv_builder_const_float(&ctx->builder, bit_size, value);
}

static SpvId
214
emit_uint_const(struct ntv_context *ctx, int bit_size, uint64_t value)
215
{
216
   assert(bit_size == 32 || bit_size == 64);
217
218
219
   return spirv_builder_const_uint(&ctx->builder, bit_size, value);
}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
220
static SpvId
221
emit_int_const(struct ntv_context *ctx, int bit_size, int64_t value)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
222
{
223
   assert(bit_size == 32 || bit_size == 64);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
224
225
226
   return spirv_builder_const_int(&ctx->builder, bit_size, value);
}

227
228
229
static SpvId
get_fvec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
{
230
   assert(bit_size == 32 || bit_size == 64);
231
232
233
234
235
236
237
238
239
240
241

   SpvId float_type = spirv_builder_type_float(&ctx->builder, bit_size);
   if (num_components > 1)
      return spirv_builder_type_vector(&ctx->builder, float_type,
                                       num_components);

   assert(num_components == 1);
   return float_type;
}

static SpvId
242
243
get_ivec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
{
244
   assert(bit_size == 32 || bit_size == 64);
245

246
   SpvId int_type = spirv_builder_type_int(&ctx->builder, bit_size);
247
248
249
250
251
252
253
254
255
256
257
   if (num_components > 1)
      return spirv_builder_type_vector(&ctx->builder, int_type,
                                       num_components);

   assert(num_components == 1);
   return int_type;
}

static SpvId
get_uvec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components)
{
258
   assert(bit_size == 32 || bit_size == 64);
259

260
   SpvId uint_type = spirv_builder_type_uint(&ctx->builder, bit_size);
261
262
263
264
265
266
267
268
   if (num_components > 1)
      return spirv_builder_type_vector(&ctx->builder, uint_type,
                                       num_components);

   assert(num_components == 1);
   return uint_type;
}

269
270
271
272
static SpvStorageClass
get_storage_class(struct nir_variable *var)
{
   switch (var->data.mode) {
273
274
   case nir_var_mem_push_const:
      return SpvStorageClassPushConstant;
275
276
277
278
   case nir_var_shader_in:
      return SpvStorageClassInput;
   case nir_var_shader_out:
      return SpvStorageClassOutput;
279
280
   case nir_var_uniform:
      return SpvStorageClassUniformConstant;
281
282
283
284
285
286
   default:
      unreachable("Unsupported nir_variable_mode");
   }
   return 0;
}

287
288
static SpvId
get_dest_uvec_type(struct ntv_context *ctx, nir_dest *dest)
289
{
290
   unsigned bit_size = nir_dest_bit_size(*dest);
291
   return get_uvec_type(ctx, bit_size, nir_dest_num_components(*dest));
292
293
294
295
296
297
}

static SpvId
get_glsl_basetype(struct ntv_context *ctx, enum glsl_base_type type)
{
   switch (type) {
298
299
300
   case GLSL_TYPE_BOOL:
      return spirv_builder_type_bool(&ctx->builder);

301
302
303
304
305
306
307
308
   case GLSL_TYPE_FLOAT:
      return spirv_builder_type_float(&ctx->builder, 32);

   case GLSL_TYPE_INT:
      return spirv_builder_type_int(&ctx->builder, 32);

   case GLSL_TYPE_UINT:
      return spirv_builder_type_uint(&ctx->builder, 32);
309
310
311
312
313
314
315
316
317

   case GLSL_TYPE_DOUBLE:
      return spirv_builder_type_float(&ctx->builder, 64);

   case GLSL_TYPE_INT64:
      return spirv_builder_type_int(&ctx->builder, 64);

   case GLSL_TYPE_UINT64:
      return spirv_builder_type_uint(&ctx->builder, 64);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
   /* TODO: handle more types */

   default:
      unreachable("unknown GLSL type");
   }
}

static SpvId
get_glsl_type(struct ntv_context *ctx, const struct glsl_type *type)
{
   assert(type);
   if (glsl_type_is_scalar(type))
      return get_glsl_basetype(ctx, glsl_get_base_type(type));

   if (glsl_type_is_vector(type))
      return spirv_builder_type_vector(&ctx->builder,
         get_glsl_basetype(ctx, glsl_get_base_type(type)),
         glsl_get_vector_elements(type));

Erik Faye-Lund's avatar
Erik Faye-Lund committed
337
   if (glsl_type_is_array(type)) {
338
339
340
341
342
343
344
345
      SpvId ret;
      SpvId element_type = get_glsl_type(ctx, glsl_get_array_element(type));
      if (glsl_type_is_unsized_array(type))
         ret = spirv_builder_type_runtime_array(&ctx->builder, element_type);
      else
         ret = spirv_builder_type_array(&ctx->builder,
                                        element_type,
                                        emit_uint_const(ctx, 32, glsl_get_length(type)));
Erik Faye-Lund's avatar
Erik Faye-Lund committed
346
      uint32_t stride = glsl_get_explicit_stride(type);
347
348
349
      if (!stride && glsl_type_is_scalar(glsl_get_array_element(type))) {
         stride = MAX2(glsl_get_bit_size(glsl_get_array_element(type)) / 8, 1);
      }
Erik Faye-Lund's avatar
Erik Faye-Lund committed
350
351
352
353
      if (stride)
         spirv_builder_emit_array_stride(&ctx->builder, ret, stride);
      return ret;
   }
354
355
356
357
358
359
360
361
362
363
364
   if (glsl_type_is_struct_or_ifc(type)) {
      SpvId types[glsl_get_length(type)];
      for (unsigned i = 0; i < glsl_get_length(type); i++)
         types[i] = get_glsl_type(ctx, glsl_get_struct_field(type, i));
      SpvId ret = spirv_builder_type_struct(&ctx->builder,
                                           types,
                                           glsl_get_length(type));
      for (unsigned i = 0; i < glsl_get_length(type); i++)
         spirv_builder_emit_member_offset(&ctx->builder, ret, i, glsl_get_struct_field_offset(type, i));
      return ret;
   }
Erik Faye-Lund's avatar
Erik Faye-Lund committed
365

366
367
368
369
370
371
   if (glsl_type_is_matrix(type))
      return spirv_builder_type_matrix(&ctx->builder,
                                       spirv_builder_type_vector(&ctx->builder,
                                                                 get_glsl_basetype(ctx, glsl_get_base_type(type)),
                                                                 glsl_get_vector_elements(type)),
                                       glsl_get_matrix_columns(type));
Erik Faye-Lund's avatar
Erik Faye-Lund committed
372

373
374
375
   unreachable("we shouldn't get here, I think...");
}

376
377
378
379
380
381
382
383
384
385
386
387
388
389
static void
create_shared_block(struct ntv_context *ctx, unsigned shared_size)
{
   SpvId type = spirv_builder_type_uint(&ctx->builder, 32);
   SpvId array = spirv_builder_type_array(&ctx->builder, type, emit_uint_const(ctx, 32, shared_size / 4));
   spirv_builder_emit_array_stride(&ctx->builder, array, 4);
   SpvId ptr_type = spirv_builder_type_pointer(&ctx->builder,
                                               SpvStorageClassWorkgroup,
                                               array);
   ctx->shared_block_var = spirv_builder_emit_var(&ctx->builder, ptr_type, SpvStorageClassWorkgroup);
   assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
   ctx->entry_ifaces[ctx->num_entry_ifaces++] = ctx->shared_block_var;
}

390
static inline unsigned char
391
reserve_slot(struct ntv_context *ctx, unsigned num_slots)
392
393
394
395
396
{
   /* TODO: this should actually be clamped to the limits value as in the table
    * in 14.1.4 of the vulkan spec, though there's not really any recourse
    * other than aborting if we do hit it...
    */
397
398
399
400
   assert(ctx->shader_slots_reserved + num_slots <= MAX_VARYING);
   unsigned ret = ctx->shader_slots_reserved;
   ctx->shader_slots_reserved += num_slots;
   return ret;
401
402
}

403
static inline unsigned
404
handle_slot(struct ntv_context *ctx, unsigned slot, unsigned num_slots)
405
{
406
   assert(num_slots);
407
   if (ctx->shader_slot_map[slot] == SLOT_UNSET)
408
      ctx->shader_slot_map[slot] = reserve_slot(ctx, num_slots);
409
410
   slot = ctx->shader_slot_map[slot];
   assert(slot < MAX_VARYING);
411
412
413
   return slot;
}

414
415
416
417
418
419
#define HANDLE_EMIT_BUILTIN(SLOT, BUILTIN) \
      case VARYING_SLOT_##SLOT: \
         spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltIn##BUILTIN); \
         break


420
static inline unsigned
421
handle_handle_slot(struct ntv_context *ctx, struct nir_variable *var, bool output)
422
423
424
425
{
   if (var->data.patch) {
      assert(var->data.location >= VARYING_SLOT_PATCH0);
      return var->data.location - VARYING_SLOT_PATCH0;
426
427
428
   } else if (var->data.location >= VARYING_SLOT_VAR0 &&
              ((output && ctx->stage == MESA_SHADER_TESS_CTRL) ||
              (!output && ctx->stage == MESA_SHADER_TESS_EVAL))) {
429
430
      return var->data.location - VARYING_SLOT_VAR0;
   }
431
   return handle_slot(ctx, var->data.location, glsl_count_vec4_slots(var->type, false, false));
432
433
}

434
435
static SpvId
input_var_init(struct ntv_context *ctx, struct nir_variable *var)
436
{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
437
   SpvId var_type = get_glsl_type(ctx, var->type);
438
439
440
   SpvStorageClass sc = get_storage_class(var);
   if (sc == SpvStorageClassPushConstant)
      spirv_builder_emit_decoration(&ctx->builder, var_type, SpvDecorationBlock);
441
   SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
442
443
                                                   sc, var_type);
   SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type, sc);
444
445
446

   if (var->name)
      spirv_builder_emit_name(&ctx->builder, var_id, var->name);
447
   if (var->data.mode == nir_var_mem_push_const) {
448
      ctx->push_const_var = var_id;
449
450
451
452

      assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
      ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
   }
453
454
   return var_id;
}
455

456
457
458
459
static void
emit_input(struct ntv_context *ctx, struct nir_variable *var)
{
   SpvId var_id = input_var_init(ctx, var);
460
461
462
463
464
   unsigned slot = var->data.location;
   if (ctx->stage == MESA_SHADER_VERTEX)
      spirv_builder_emit_location(&ctx->builder, var_id,
                                  var->data.driver_location);
   else if (ctx->stage == MESA_SHADER_FRAGMENT) {
465
466
467
468
469
470
471
472
473
474
475
      switch (slot) {
      HANDLE_EMIT_BUILTIN(POS, FragCoord);
      HANDLE_EMIT_BUILTIN(PNTC, PointCoord);
      HANDLE_EMIT_BUILTIN(LAYER, Layer);
      HANDLE_EMIT_BUILTIN(PRIMITIVE_ID, PrimitiveId);
      HANDLE_EMIT_BUILTIN(CLIP_DIST0, ClipDistance);
      HANDLE_EMIT_BUILTIN(CULL_DIST0, CullDistance);
      HANDLE_EMIT_BUILTIN(VIEWPORT, ViewportIndex);
      HANDLE_EMIT_BUILTIN(FACE, FrontFacing);

      default:
476
         slot = handle_slot(ctx, slot, glsl_count_vec4_slots(var->type, false, false));
477
         spirv_builder_emit_location(&ctx->builder, var_id, slot);
478
      }
479
480
      if (var->data.centroid)
         spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationCentroid);
481
482
      else if (var->data.sample) {
         spirv_builder_emit_cap(&ctx->builder, SpvCapabilitySampleRateShading);
483
         spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationSample);
484
      }
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
   } else if (ctx->stage < MESA_SHADER_FRAGMENT) {
      switch (slot) {
      HANDLE_EMIT_BUILTIN(POS, Position);
      HANDLE_EMIT_BUILTIN(PSIZ, PointSize);
      HANDLE_EMIT_BUILTIN(LAYER, Layer);
      HANDLE_EMIT_BUILTIN(PRIMITIVE_ID, PrimitiveId);
      HANDLE_EMIT_BUILTIN(CULL_DIST0, CullDistance);
      HANDLE_EMIT_BUILTIN(VIEWPORT, ViewportIndex);
      HANDLE_EMIT_BUILTIN(TESS_LEVEL_OUTER, TessLevelOuter);
      HANDLE_EMIT_BUILTIN(TESS_LEVEL_INNER, TessLevelInner);

      case VARYING_SLOT_CLIP_DIST0:
         assert(glsl_type_is_array(var->type));
         spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInClipDistance);
         break;

      default:
502
         slot = handle_handle_slot(ctx, var, false);
503
504
         spirv_builder_emit_location(&ctx->builder, var_id, slot);
      }
505
506
507
508
509
510
   }

   if (var->data.location_frac)
      spirv_builder_emit_component(&ctx->builder, var_id,
                                   var->data.location_frac);

511
512
513
   if (var->data.patch)
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationPatch);

514
515
516
   if (var->data.interpolation == INTERP_MODE_FLAT)
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationFlat);

Erik Faye-Lund's avatar
Erik Faye-Lund committed
517
   _mesa_hash_table_insert(ctx->vars, var, (void *)(intptr_t)var_id);
518
519
520
521
522
523
524
525

   assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
   ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
}

static void
emit_output(struct ntv_context *ctx, struct nir_variable *var)
{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
526
   SpvId var_type = get_glsl_type(ctx, var->type);
527
528
529
530

   /* SampleMask is always an array in spirv */
   if (ctx->stage == MESA_SHADER_FRAGMENT && var->data.location == FRAG_RESULT_SAMPLE_MASK)
      ctx->sample_mask_type = var_type = spirv_builder_type_array(&ctx->builder, var_type, emit_uint_const(ctx, 32, 1));
531
532
   SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
                                                   SpvStorageClassOutput,
Erik Faye-Lund's avatar
Erik Faye-Lund committed
533
                                                   var_type);
534
535
536
537
538
   SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
                                         SpvStorageClassOutput);
   if (var->name)
      spirv_builder_emit_name(&ctx->builder, var_id, var->name);

539
540
   unsigned slot = var->data.location;
   if (ctx->stage != MESA_SHADER_FRAGMENT) {
541
542
543
544
545
546
547
548
549
550
551
552
553
      switch (slot) {
      HANDLE_EMIT_BUILTIN(POS, Position);
      HANDLE_EMIT_BUILTIN(PSIZ, PointSize);
      HANDLE_EMIT_BUILTIN(LAYER, Layer);
      HANDLE_EMIT_BUILTIN(PRIMITIVE_ID, PrimitiveId);
      HANDLE_EMIT_BUILTIN(CULL_DIST0, CullDistance);
      HANDLE_EMIT_BUILTIN(VIEWPORT, ViewportIndex);
      HANDLE_EMIT_BUILTIN(TESS_LEVEL_OUTER, TessLevelOuter);
      HANDLE_EMIT_BUILTIN(TESS_LEVEL_INNER, TessLevelInner);

      case VARYING_SLOT_CLIP_DIST0:
         assert(glsl_type_is_array(var->type));
         spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInClipDistance);
554
555
556
557
         /* this can be as large as 2x vec4, which requires 2 slots */
         ctx->outputs[VARYING_SLOT_CLIP_DIST1] = var_id;
         ctx->so_output_gl_types[VARYING_SLOT_CLIP_DIST1] = var->type;
         ctx->so_output_types[VARYING_SLOT_CLIP_DIST1] = var_type;
558
559
560
         break;

      default:
561
         slot = handle_handle_slot(ctx, var, true);
562
         spirv_builder_emit_location(&ctx->builder, var_id, slot);
563
      }
564
565
      /* tcs can't do xfb */
      if (ctx->stage != MESA_SHADER_TESS_CTRL) {
566
567
568
569
         unsigned idx = var->data.location << 2 | var->data.location_frac;
         ctx->outputs[idx] = var_id;
         ctx->so_output_gl_types[idx] = var->type;
         ctx->so_output_types[idx] = var_type;
570
      }
571
   } else {
572
      if (var->data.location >= FRAG_RESULT_DATA0) {
573
         spirv_builder_emit_location(&ctx->builder, var_id,
574
                                     var->data.location - FRAG_RESULT_DATA0);
575
576
         spirv_builder_emit_index(&ctx->builder, var_id, var->data.index);
      } else {
577
578
         switch (var->data.location) {
         case FRAG_RESULT_COLOR:
579
            unreachable("gl_FragColor should be lowered by now");
580
581
582
583
584

         case FRAG_RESULT_DEPTH:
            spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInFragDepth);
            break;

585
586
587
588
         case FRAG_RESULT_SAMPLE_MASK:
            spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInSampleMask);
            break;

589
590
591
592
         case FRAG_RESULT_STENCIL:
            spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltInFragStencilRefEXT);
            break;

593
         default:
594
            spirv_builder_emit_location(&ctx->builder, var_id, slot);
595
            spirv_builder_emit_index(&ctx->builder, var_id, var->data.index);
596
         }
597
      }
598
599
      if (var->data.sample) {
         spirv_builder_emit_cap(&ctx->builder, SpvCapabilitySampleRateShading);
600
         spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationSample);
601
      }
602
603
604
605
606
607
   }

   if (var->data.location_frac)
      spirv_builder_emit_component(&ctx->builder, var_id,
                                   var->data.location_frac);

608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
   switch (var->data.interpolation) {
   case INTERP_MODE_NONE:
   case INTERP_MODE_SMOOTH: /* XXX spirv doesn't seem to have anything for this */
      break;
   case INTERP_MODE_FLAT:
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationFlat);
      break;
   case INTERP_MODE_EXPLICIT:
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationExplicitInterpAMD);
      break;
   case INTERP_MODE_NOPERSPECTIVE:
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationNoPerspective);
      break;
   default:
      unreachable("unknown interpolation value");
   }

625
626
627
   if (var->data.patch)
      spirv_builder_emit_decoration(&ctx->builder, var_id, SpvDecorationPatch);

628
629
630
631
632
633
634
635
   if (var->data.explicit_xfb_buffer) {
      spirv_builder_emit_offset(&ctx->builder, var_id, var->data.offset);
      spirv_builder_emit_xfb_buffer(&ctx->builder, var_id, var->data.xfb.buffer);
      spirv_builder_emit_xfb_stride(&ctx->builder, var_id, var->data.xfb.stride);
      if (var->data.stream)
         spirv_builder_emit_stream(&ctx->builder, var_id, var->data.stream);
   }

Erik Faye-Lund's avatar
Erik Faye-Lund committed
636
   _mesa_hash_table_insert(ctx->vars, var, (void *)(intptr_t)var_id);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

   assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
   ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
}

static SpvDim
type_to_dim(enum glsl_sampler_dim gdim, bool *is_ms)
{
   *is_ms = false;
   switch (gdim) {
   case GLSL_SAMPLER_DIM_1D:
      return SpvDim1D;
   case GLSL_SAMPLER_DIM_2D:
      return SpvDim2D;
   case GLSL_SAMPLER_DIM_3D:
      return SpvDim3D;
653
654
655
   case GLSL_SAMPLER_DIM_CUBE:
      return SpvDimCube;
   case GLSL_SAMPLER_DIM_RECT:
Erik Faye-Lund's avatar
Erik Faye-Lund committed
656
      return SpvDim2D;
657
658
659
660
   case GLSL_SAMPLER_DIM_BUF:
      return SpvDimBuffer;
   case GLSL_SAMPLER_DIM_EXTERNAL:
      return SpvDim2D; /* seems dodgy... */
661
662
663
664
665
666
667
668
669
670
   case GLSL_SAMPLER_DIM_MS:
      *is_ms = true;
      return SpvDim2D;
   default:
      fprintf(stderr, "unknown sampler type %d\n", gdim);
      break;
   }
   return SpvDim2D;
}

671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
static inline SpvImageFormat
get_image_format(enum pipe_format format)
{
   switch (format) {
   case PIPE_FORMAT_NONE:
      return SpvImageFormatUnknown;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      return SpvImageFormatRgba32f;
   case PIPE_FORMAT_R16G16B16A16_FLOAT:
      return SpvImageFormatRgba16f;
   case PIPE_FORMAT_R32_FLOAT:
      return SpvImageFormatR32f;
   case PIPE_FORMAT_R8G8B8A8_UNORM:
      return SpvImageFormatRgba8;
   case PIPE_FORMAT_R8G8B8A8_SNORM:
      return SpvImageFormatRgba8Snorm;
   case PIPE_FORMAT_R32G32_FLOAT:
      return SpvImageFormatRg32f;
   case PIPE_FORMAT_R16G16_FLOAT:
      return SpvImageFormatRg16f;
   case PIPE_FORMAT_R11G11B10_FLOAT:
      return SpvImageFormatR11fG11fB10f;
   case PIPE_FORMAT_R16_FLOAT:
      return SpvImageFormatR16f;
   case PIPE_FORMAT_R16G16B16A16_UNORM:
      return SpvImageFormatRgba16;
   case PIPE_FORMAT_R10G10B10A2_UNORM:
      return SpvImageFormatRgb10A2;
   case PIPE_FORMAT_R16G16_UNORM:
      return SpvImageFormatRg16;
   case PIPE_FORMAT_R8G8_UNORM:
      return SpvImageFormatRg8;
   case PIPE_FORMAT_R16_UNORM:
      return SpvImageFormatR16;
   case PIPE_FORMAT_R8_UNORM:
      return SpvImageFormatR8;
   case PIPE_FORMAT_R16G16B16A16_SNORM:
      return SpvImageFormatRgba16Snorm;
   case PIPE_FORMAT_R16G16_SNORM:
      return SpvImageFormatRg16Snorm;
   case PIPE_FORMAT_R8G8_SNORM:
      return SpvImageFormatRg8Snorm;
   case PIPE_FORMAT_R16_SNORM:
      return SpvImageFormatR16Snorm;
   case PIPE_FORMAT_R8_SNORM:
      return SpvImageFormatR8Snorm;
   case PIPE_FORMAT_R32G32B32A32_SINT:
      return SpvImageFormatRgba32i;
   case PIPE_FORMAT_R16G16B16A16_SINT:
      return SpvImageFormatRgba16i;
   case PIPE_FORMAT_R8G8B8A8_SINT:
      return SpvImageFormatRgba8i;
   case PIPE_FORMAT_R32_SINT:
      return SpvImageFormatR32i;
   case PIPE_FORMAT_R32G32_SINT:
      return SpvImageFormatRg32i;
   case PIPE_FORMAT_R16G16_SINT:
      return SpvImageFormatRg16i;
   case PIPE_FORMAT_R8G8_SINT:
      return SpvImageFormatRg8i;
   case PIPE_FORMAT_R16_SINT:
      return SpvImageFormatR16i;
   case PIPE_FORMAT_R8_SINT:
      return SpvImageFormatR8i;
   case PIPE_FORMAT_R32G32B32A32_UINT:
      return SpvImageFormatRgba32ui;
   case PIPE_FORMAT_R16G16B16A16_UINT:
      return SpvImageFormatRgba16ui;
   case PIPE_FORMAT_R8G8B8A8_UINT:
      return SpvImageFormatRgba8ui;
   case PIPE_FORMAT_R32_UINT:
      return SpvImageFormatR32ui;
   case PIPE_FORMAT_R10G10B10A2_UINT:
      return SpvImageFormatRgb10a2ui;
   case PIPE_FORMAT_R32G32_UINT:
      return SpvImageFormatRg32ui;
   case PIPE_FORMAT_R16G16_UINT:
      return SpvImageFormatRg16ui;
   case PIPE_FORMAT_R8G8_UINT:
      return SpvImageFormatRg8ui;
   case PIPE_FORMAT_R16_UINT:
      return SpvImageFormatR16ui;
   case PIPE_FORMAT_R8_UINT:
      return SpvImageFormatR8ui;
   default:
      break;
   }
   unreachable("unknown format");
   return SpvImageFormatUnknown;
}

762
static void
763
emit_image(struct ntv_context *ctx, struct nir_variable *var)
764
{
765
766
   const struct glsl_type *type = glsl_without_array(var->type);

767
   bool is_ms;
768
   bool is_sampler = glsl_type_is_sampler(type);
769
770
771
772
773
774

   if (!is_sampler && !var->data.image.format) {
      spirv_builder_emit_cap(&ctx->builder, SpvCapabilityStorageImageWriteWithoutFormat);
      spirv_builder_emit_cap(&ctx->builder, SpvCapabilityStorageImageReadWithoutFormat);
   }

775
   SpvDim dimension = type_to_dim(glsl_get_sampler_dim(type), &is_ms);
776

777
   SpvId result_type = get_glsl_basetype(ctx, glsl_get_sampler_result_type(type));
778
779
   SpvId image_type = spirv_builder_type_image(&ctx->builder, result_type,
                                               dimension, false,
780
                                               glsl_sampler_type_is_array(type),
781
782
                                               is_ms, is_sampler ? 1 : 2,
                                               get_image_format(var->data.image.format));
783
784
785

   SpvId sampled_type = spirv_builder_type_sampled_image(&ctx->builder,
                                                         image_type);
786
   SpvId var_type = is_sampler ? sampled_type : image_type;
787

788
   int index = var->data.driver_location;
789
790
791
   assert(!is_sampler || (!(ctx->samplers_used & (1 << index))));
   assert(!is_sampler || !ctx->sampler_types[index]);
   assert(is_sampler || !ctx->image_types[index]);
792
793

   if (glsl_type_is_array(var->type)) {
794
      var_type = spirv_builder_type_array(&ctx->builder, var_type,
795
                                              emit_uint_const(ctx, 32, glsl_get_aoa_size(var->type)));
796
      spirv_builder_emit_array_stride(&ctx->builder, var_type, sizeof(void*));
797
798
      ctx->sampler_array_sizes[index] = glsl_get_aoa_size(var->type);
   }
799
800
   SpvId pointer_type = spirv_builder_type_pointer(&ctx->builder,
                                                   SpvStorageClassUniformConstant,
801
                                                   var_type);
802

803
804
   SpvId var_id = spirv_builder_emit_var(&ctx->builder, pointer_type,
                                         SpvStorageClassUniformConstant);
805

806
807
808
   if (var->name)
      spirv_builder_emit_name(&ctx->builder, var_id, var->name);

809
810
811
812
813
814
815
816
817
818
819
820
821
   if (is_sampler) {
      ctx->sampler_types[index] = image_type;
      ctx->samplers[index] = var_id;
      ctx->samplers_used |= 1 << index;
   } else {
      ctx->image_types[index] = image_type;
      ctx->images[index] = var_id;
      _mesa_hash_table_insert(ctx->vars, var, (void *)(intptr_t)var_id);
      uint32_t *key = ralloc_size(ctx->mem_ctx, sizeof(uint32_t));
      *key = var_id;
      _mesa_hash_table_insert(ctx->image_vars, key, var);
      emit_access_decorations(ctx, var, var_id);
   }
822
823
   assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
   ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;
824

825
   spirv_builder_emit_descriptor_set(&ctx->builder, var_id, var->data.descriptor_set);
826
   spirv_builder_emit_binding(&ctx->builder, var_id, var->data.binding);
827
828
}

829
830
831
832
833
834
835
836
837
838
static SpvId
get_sized_uint_array_type(struct ntv_context *ctx, unsigned array_size)
{
   SpvId array_length = emit_uint_const(ctx, 32, array_size);
   SpvId array_type = spirv_builder_type_array(&ctx->builder, get_uvec_type(ctx, 32, 1),
                                            array_length);
   spirv_builder_emit_array_stride(&ctx->builder, array_type, 4);
   return array_type;
}

839
840
static SpvId
get_bo_array_type(struct ntv_context *ctx, struct nir_variable *var)
841
{
842
   SpvId array_type;
843
844
   uint32_t array_size = glsl_get_length(glsl_get_struct_field(glsl_without_array(var->interface_type), 0));
   array_type = get_sized_uint_array_type(ctx, array_size);
845
846
847
   return array_type;
}

848
849
static SpvId
get_bo_struct_type(struct ntv_context *ctx, struct nir_variable *var)
850
851
{
   SpvId array_type = get_bo_array_type(ctx, var);
852
   bool ssbo = var->data.mode == nir_var_mem_ssbo;
853
854

   // wrap UBO-array in a struct
855
856
   SpvId runtime_array = 0;
   if (ssbo) {
857
858
       runtime_array = spirv_builder_type_runtime_array(&ctx->builder, get_uvec_type(ctx, 32, 1));
       spirv_builder_emit_array_stride(&ctx->builder, runtime_array, 4);
859
860
   }
   SpvId types[] = {array_type, runtime_array};
861
   SpvId struct_type = spirv_builder_type_struct(&ctx->builder, types, ssbo ? 2 : 1);
862
863
864
865
866
867
868
869
870
   if (var->name) {
      char struct_name[100];
      snprintf(struct_name, sizeof(struct_name), "struct_%s", var->name);
      spirv_builder_emit_name(&ctx->builder, struct_type, struct_name);
   }

   spirv_builder_emit_decoration(&ctx->builder, struct_type,
                                 SpvDecorationBlock);
   spirv_builder_emit_member_offset(&ctx->builder, struct_type, 0, 0);
871
872
   if (ssbo)
      spirv_builder_emit_member_offset(&ctx->builder, struct_type, 1, 0);
873

874
   return struct_type;
875
876
877
878
879
880
881
}

static void
emit_bo(struct ntv_context *ctx, struct nir_variable *var)
{
   bool ssbo = var->data.mode == nir_var_mem_ssbo;

882
883
884
885
886
887
888
889
   SpvId pointer_type = ssbo ? ctx->ssbo_type : ctx->ubo_type;
   /* ensure we don't get annoying validation warnings from type mismatches */
   if (!pointer_type)
      pointer_type = get_bo_struct_type(ctx, var);
   SpvId array_type = spirv_builder_type_array(&ctx->builder, pointer_type, emit_uint_const(ctx, 32, glsl_get_length(var->type)));
   SpvId array_ptr_type = spirv_builder_type_pointer(&ctx->builder,
                                                     ssbo ? SpvStorageClassStorageBuffer : SpvStorageClassUniform,
                                                     array_type);
890

891
   SpvId var_id = spirv_builder_emit_var(&ctx->builder, array_ptr_type,
892
893
894
                                         ssbo ? SpvStorageClassStorageBuffer : SpvStorageClassUniform);
   if (var->name)
      spirv_builder_emit_name(&ctx->builder, var_id, var->name);
895

896
   if (ssbo) {
897
898
899
      assert(!ctx->ssbo);
      ctx->ssbo = var_id;
      ctx->ssbo_type = pointer_type;
900
   } else {
901
      assert(var->data.driver_location < ARRAY_SIZE(ctx->ubos));
902
903
      assert(!ctx->ubos[var->data.driver_location]);
      ctx->ubos[var->data.driver_location] = var_id;
904
      ctx->ubo_type = pointer_type;
905
   }
906
907
908
   assert(ctx->num_entry_ifaces < ARRAY_SIZE(ctx->entry_ifaces));
   ctx->entry_ifaces[ctx->num_entry_ifaces++] = var_id;

909
   spirv_builder_emit_descriptor_set(&ctx->builder, var_id, var->data.descriptor_set);
910
   spirv_builder_emit_binding(&ctx->builder, var_id, var->data.binding);
911
912
913
914
915
}

static void
emit_uniform(struct ntv_context *ctx, struct nir_variable *var)
{
916
917
   if (var->data.mode == nir_var_mem_ubo || var->data.mode == nir_var_mem_ssbo)
      emit_bo(ctx, var);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
918
919
   else {
      assert(var->data.mode == nir_var_uniform);
920
921
922
      const struct glsl_type *type = glsl_without_array(var->type);
      if (glsl_type_is_sampler(type) || glsl_type_is_image(type))
         emit_image(ctx, var);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
923
   }
924
925
}

926
927
928
929
930
static SpvId
get_vec_from_bit_size(struct ntv_context *ctx, uint32_t bit_size, uint32_t num_components)
{
   if (bit_size == 1)
      return get_bvec_type(ctx, num_components);
931
   if (bit_size == 32 || bit_size == 64)
932
933
934
935
936
      return get_uvec_type(ctx, bit_size, num_components);
   unreachable("unhandled register bit size");
   return 0;
}

937
static SpvId
938
get_src_ssa(struct ntv_context *ctx, const nir_ssa_def *ssa)
939
940
941
942
943
944
{
   assert(ssa->index < ctx->num_defs);
   assert(ctx->defs[ssa->index] != 0);
   return ctx->defs[ssa->index];
}

945
946
947
static SpvId
get_var_from_reg(struct ntv_context *ctx, nir_register *reg)
{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
948
949
950
   assert(reg->index < ctx->num_regs);
   assert(ctx->regs[reg->index] != 0);
   return ctx->regs[reg->index];
951
952
953
}

static SpvId
954
get_src_reg(struct ntv_context *ctx, const nir_reg_src *reg)
955
956
957
958
959
960
{
   assert(reg->reg);
   assert(!reg->indirect);
   assert(!reg->base_offset);

   SpvId var = get_var_from_reg(ctx, reg->reg);
961
   SpvId type = get_vec_from_bit_size(ctx, reg->reg->bit_size, reg->reg->num_components);
962
963
964
   return spirv_builder_emit_load(&ctx->builder, type, var);
}

965
static SpvId
966
get_src(struct ntv_context *ctx, nir_src *src)
967
{
968
   if (src->is_ssa)
969
      return get_src_ssa(ctx, src->ssa);
970
   else
971
      return get_src_reg(ctx, &src->reg);
972
973
974
}

static SpvId
975
get_alu_src_raw(struct ntv_context *ctx, nir_alu_instr *alu, unsigned src)
976
977
978
979
{
   assert(!alu->src[src].negate);
   assert(!alu->src[src].abs);

980
   SpvId def = get_src(ctx, &alu->src[src].src);
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

   unsigned used_channels = 0;
   bool need_swizzle = false;
   for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
      if (!nir_alu_instr_channel_used(alu, src, i))
         continue;

      used_channels++;

      if (alu->src[src].swizzle[i] != i)
         need_swizzle = true;
   }
   assert(used_channels != 0);

   unsigned live_channels = nir_src_num_components(alu->src[src].src);
   if (used_channels != live_channels)
      need_swizzle = true;

   if (!need_swizzle)
      return def;