nir_to_dxil.c 145 KB
Newer Older
1
/*
2
 * Copyright © Microsoft Corporation
3 4 5 6
 *
 * 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
7 8 9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * 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:
10 11 12 13 14 15 16
 *
 * 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,
17 18 19 20 21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
22 23 24 25 26 27
 */

#include "nir_to_dxil.h"

#include "dxil_module.h"
#include "dxil_container.h"
28
#include "dxil_function.h"
29
#include "dxil_signature.h"
30
#include "dxil_enums.h"
31
#include "dxil_dump.h"
32
#include "dxil_nir.h"
33 34

#include "util/u_debug.h"
35
#include "util/u_math.h"
36
#include "nir/nir_builder.h"
37 38 39 40 41

#include "git_sha1.h"

#include <stdint.h>

42 43 44 45 46 47 48
int debug_dxil = 0;

static const struct debug_named_value
debug_options[] = {
   { "verbose", DXIL_DEBUG_VERBOSE, NULL },
   { "dump_blob",  DXIL_DEBUG_DUMP_BLOB , "Write shader blobs" },
   { "trace",  DXIL_DEBUG_TRACE , "Trace instruction conversion" },
49
   { "dump_module", DXIL_DEBUG_DUMP_MODULE, "dump module tree to stderr"},
50 51 52 53
   DEBUG_NAMED_VALUE_END
};

DEBUG_GET_ONCE_FLAGS_OPTION(debug_dxil, "DXIL_DEBUG", debug_options, 0)
54 55

#define NIR_INSTR_UNSUPPORTED(instr) \
56
   if (debug_dxil & DXIL_DEBUG_VERBOSE) \
57 58 59 60 61
   do { \
      fprintf(stderr, "Unsupported instruction:"); \
      nir_print_instr(instr, stderr); \
      fprintf(stderr, "\n"); \
   } while (0)
62 63 64 65 66 67 68 69

#define TRACE_CONVERSION(instr) \
   if (debug_dxil & DXIL_DEBUG_TRACE) \
      do { \
         fprintf(stderr, "Convert '"); \
         nir_print_instr(instr, stderr); \
         fprintf(stderr, "'\n"); \
      } while (0)
70

71 72 73
static const nir_shader_compiler_options
nir_options = {
   .lower_negate = true,
74
   .lower_ffma = true,
75
   .lower_isign = true,
76
   .lower_fsign = true,
77 78
   .lower_iabs = true,
   .lower_fmod = true,
79 80
   .lower_fpow = true,
   .lower_scmp = true,
81
   .lower_ldexp = true,
82
   .lower_flrp16 = true,
Gert Wollny's avatar
Gert Wollny committed
83
   .lower_flrp32 = true,
84
   .lower_flrp64 = true,
85 86
   .lower_extract_word = true,
   .lower_extract_byte = true,
87
   .lower_all_io_to_elements = true,
88
   .lower_hadd = true,
89
   .lower_add_sat = true,
90
   .lower_uadd_carry = true,
91
   .lower_mul_high = true,
92 93 94 95 96
   .lower_rotate = true,
   .lower_pack_64_2x32_split = true,
   .lower_pack_32_2x16_split = true,
   .lower_unpack_64_2x32_split = true,
   .lower_unpack_32_2x16_split = true,
97 98
   .use_scoped_memory_barrier = true,
   .use_scoped_control_memory_barrier = true,
99 100
   .vertex_id_zero_based = true,
   .lower_base_vertex = true,
101
   .denorms_not_supported = true,
102
   .has_cs_global_id = true,
103 104 105 106 107 108 109
};

const nir_shader_compiler_options*
dxil_get_nir_compiler_options(void)
{
   return &nir_options;
}
110

111 112 113 114 115 116 117 118 119 120 121 122 123
static bool
emit_llvm_ident(struct dxil_module *m)
{
   const struct dxil_mdnode *compiler = dxil_get_metadata_string(m, "Mesa version " PACKAGE_VERSION MESA_GIT_SHA1);
   if (!compiler)
      return false;

   const struct dxil_mdnode *llvm_ident = dxil_get_metadata_node(m, &compiler, 1);
   return llvm_ident &&
          dxil_add_metadata_named_node(m, "llvm.ident", &llvm_ident, 1);
}

static bool
124 125
emit_named_version(struct dxil_module *m, const char *name,
                   int major, int minor)
126 127 128 129 130 131
{
   const struct dxil_mdnode *major_node = dxil_get_metadata_int32(m, major);
   const struct dxil_mdnode *minor_node = dxil_get_metadata_int32(m, minor);
   const struct dxil_mdnode *version_nodes[] = { major_node, minor_node };
   const struct dxil_mdnode *version = dxil_get_metadata_node(m, version_nodes,
                                                     ARRAY_SIZE(version_nodes));
132
   return dxil_add_metadata_named_node(m, name, &version, 1);
133 134 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 170 171 172 173 174
}

static const char *
get_shader_kind_str(enum dxil_shader_kind kind)
{
   switch (kind) {
   case DXIL_PIXEL_SHADER:
      return "ps";
   case DXIL_VERTEX_SHADER:
      return "vs";
   case DXIL_GEOMETRY_SHADER:
      return "gs";
   case DXIL_HULL_SHADER:
      return "hs";
   case DXIL_DOMAIN_SHADER:
      return "ds";
   case DXIL_COMPUTE_SHADER:
      return "cs";
   default:
      unreachable("invalid shader kind");
   }
}

static bool
emit_dx_shader_model(struct dxil_module *m)
{
   const struct dxil_mdnode *type_node = dxil_get_metadata_string(m, get_shader_kind_str(m->shader_kind));
   const struct dxil_mdnode *major_node = dxil_get_metadata_int32(m, m->major_version);
   const struct dxil_mdnode *minor_node = dxil_get_metadata_int32(m, m->minor_version);
   const struct dxil_mdnode *shader_model[] = { type_node, major_node,
                                                minor_node };
   const struct dxil_mdnode *dx_shader_model = dxil_get_metadata_node(m, shader_model, ARRAY_SIZE(shader_model));

   return dxil_add_metadata_named_node(m, "dx.shaderModel",
                                       &dx_shader_model, 1);
}

enum {
   DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG = 0,
   DXIL_STRUCTURED_BUFFER_ELEMENT_STRIDE_TAG = 1
};

175
enum dxil_intr {
176
   DXIL_INTR_LOAD_INPUT = 4,
177
   DXIL_INTR_STORE_OUTPUT = 5,
178
   DXIL_INTR_FABS = 6,
179
   DXIL_INTR_SATURATE = 7,
180

181
   DXIL_INTR_ISFINITE = 10,
182 183
   DXIL_INTR_ISNORMAL = 11,

184
   DXIL_INTR_FCOS = 12,
185
   DXIL_INTR_FSIN = 13,
186 187

   DXIL_INTR_FEXP2 = 21,
188
   DXIL_INTR_FRC = 22,
189
   DXIL_INTR_FLOG2 = 23,
190

191
   DXIL_INTR_SQRT = 24,
192
   DXIL_INTR_RSQRT = 25,
193 194 195 196 197
   DXIL_INTR_ROUND_NE = 26,
   DXIL_INTR_ROUND_NI = 27,
   DXIL_INTR_ROUND_PI = 28,
   DXIL_INTR_ROUND_Z = 29,

198
   DXIL_INTR_COUNTBITS = 31,
199
   DXIL_INTR_FIRSTBIT_HI = 33,
200

201
   DXIL_INTR_FMAX = 35,
202
   DXIL_INTR_FMIN = 36,
203 204
   DXIL_INTR_IMAX = 37,
   DXIL_INTR_IMIN = 38,
205 206
   DXIL_INTR_UMAX = 39,
   DXIL_INTR_UMIN = 40,
207

208 209
   DXIL_INTR_FFMA = 46,

210
   DXIL_INTR_CREATE_HANDLE = 57,
211
   DXIL_INTR_CBUFFER_LOAD_LEGACY = 59,
212

213 214 215 216 217 218 219 220 221 222
   DXIL_INTR_SAMPLE = 60,
   DXIL_INTR_SAMPLE_BIAS = 61,
   DXIL_INTR_SAMPLE_LEVEL = 62,
   DXIL_INTR_SAMPLE_GRAD = 63,
   DXIL_INTR_SAMPLE_CMP = 64,
   DXIL_INTR_SAMPLE_CMP_LVL_ZERO = 65,

   DXIL_INTR_TEXTURE_LOAD = 66,
   DXIL_INTR_TEXTURE_STORE = 67,

223
   DXIL_INTR_BUFFER_LOAD = 68,
224 225
   DXIL_INTR_BUFFER_STORE = 69,

226 227
   DXIL_INTR_TEXTURE_SIZE = 72,

228
   DXIL_INTR_ATOMIC_BINOP = 78,
229
   DXIL_INTR_ATOMIC_CMPXCHG = 79,
230
   DXIL_INTR_BARRIER = 80,
231
   DXIL_INTR_TEXTURE_LOD = 81,
232

233
   DXIL_INTR_DISCARD = 82,
234 235 236 237
   DXIL_INTR_DDX_COARSE = 83,
   DXIL_INTR_DDY_COARSE = 84,
   DXIL_INTR_DDX_FINE = 85,
   DXIL_INTR_DDY_FINE = 86,
238

239 240
   DXIL_INTR_THREAD_ID = 93,
   DXIL_INTR_GROUP_ID = 94,
241
   DXIL_INTR_THREAD_ID_IN_GROUP = 95,
242

243 244 245
   DXIL_INTR_EMIT_STREAM = 97,
   DXIL_INTR_CUT_STREAM = 98,

246 247
   DXIL_INTR_PRIMITIVE_ID = 108,

248 249 250
   DXIL_INTR_LEGACY_F32TOF16 = 130,
   DXIL_INTR_LEGACY_F16TOF32 = 131,

251
   DXIL_INTR_ATTRIBUTE_AT_VERTEX = 137,
252 253
};

254 255 256 257 258 259 260 261 262 263 264 265
enum dxil_atomic_op {
   DXIL_ATOMIC_ADD = 0,
   DXIL_ATOMIC_AND = 1,
   DXIL_ATOMIC_OR = 2,
   DXIL_ATOMIC_XOR = 3,
   DXIL_ATOMIC_IMIN = 4,
   DXIL_ATOMIC_IMAX = 5,
   DXIL_ATOMIC_UMIN = 6,
   DXIL_ATOMIC_UMAX = 7,
   DXIL_ATOMIC_EXCHANGE = 8,
};

266 267 268 269 270 271
typedef struct {
   unsigned id;
   unsigned binding;
   unsigned size;
} resource_array_layout;

272 273 274
static void
fill_resource_metadata(struct dxil_module *m, const struct dxil_mdnode **fields,
                       const struct dxil_type *struct_type,
275
                       const char *name, const resource_array_layout *layout)
276 277 278 279
{
   const struct dxil_type *pointer_type = dxil_module_get_pointer_type(m, struct_type);
   const struct dxil_value *pointer_undef = dxil_module_get_undef(m, pointer_type);

280
   fields[0] = dxil_get_metadata_int32(m, layout->id); // resource ID
281 282 283
   fields[1] = dxil_get_metadata_value(m, pointer_type, pointer_undef); // global constant symbol
   fields[2] = dxil_get_metadata_string(m, name ? name : ""); // name
   fields[3] = dxil_get_metadata_int32(m, 0); // space ID
284 285
   fields[4] = dxil_get_metadata_int32(m, layout->binding); // lower bound
   fields[5] = dxil_get_metadata_int32(m, layout->size); // range size
286 287
}

288 289
static const struct dxil_mdnode *
emit_srv_metadata(struct dxil_module *m, const struct dxil_type *elem_type,
290
                  const char *name, const resource_array_layout *layout,
291
                  enum dxil_component_type comp_type,
292 293 294 295 296 297 298 299 300 301
                  enum dxil_resource_kind res_kind)
{
   const struct dxil_mdnode *fields[9];

   const struct dxil_mdnode *buffer_element_type_tag = dxil_get_metadata_int32(m, DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG);
   const struct dxil_mdnode *element_type = dxil_get_metadata_int32(m, comp_type);
   const struct dxil_mdnode *metadata_tag_nodes[] = {
      buffer_element_type_tag, element_type
   };

302
   fill_resource_metadata(m, fields, elem_type, name, layout);
303 304 305 306 307 308 309
   fields[6] = dxil_get_metadata_int32(m, res_kind); // resource shape
   fields[7] = dxil_get_metadata_int1(m, 0); // sample count
   fields[8] = dxil_get_metadata_node(m, metadata_tag_nodes, ARRAY_SIZE(metadata_tag_nodes)); // metadata

   return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}

310 311
static const struct dxil_mdnode *
emit_uav_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
312
                  const char *name, const resource_array_layout *layout,
313 314
                  enum dxil_component_type comp_type,
                  enum dxil_resource_kind res_kind)
315
{
316
   const struct dxil_mdnode *fields[11];
317

318
   const struct dxil_mdnode *metadata_tag_nodes[2];
319

320
   fill_resource_metadata(m, fields, struct_type, name, layout);
321
   fields[6] = dxil_get_metadata_int32(m, res_kind); // resource shape
322 323 324
   fields[7] = dxil_get_metadata_int1(m, false); // globally-coherent
   fields[8] = dxil_get_metadata_int1(m, false); // has counter
   fields[9] = dxil_get_metadata_int1(m, false); // is ROV
325 326 327 328 329 330 331
   if (res_kind != DXIL_RESOURCE_KIND_RAW_BUFFER &&
       res_kind != DXIL_RESOURCE_KIND_STRUCTURED_BUFFER) {
      metadata_tag_nodes[0] = dxil_get_metadata_int32(m, DXIL_TYPED_BUFFER_ELEMENT_TYPE_TAG);
      metadata_tag_nodes[1] = dxil_get_metadata_int32(m, comp_type);
      fields[10] = dxil_get_metadata_node(m, metadata_tag_nodes, ARRAY_SIZE(metadata_tag_nodes)); // metadata
   } else
      fields[10] = NULL;
332

333 334 335
   return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}

336 337
static const struct dxil_mdnode *
emit_cbv_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
338 339
                  const char *name, const resource_array_layout *layout,
                  unsigned size)
340 341 342
{
   const struct dxil_mdnode *fields[8];

343
   fill_resource_metadata(m, fields, struct_type, name, layout);
344 345 346 347 348 349
   fields[6] = dxil_get_metadata_int32(m, size); // constant buffer size
   fields[7] = NULL; // metadata

   return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}

350 351
static const struct dxil_mdnode *
emit_sampler_metadata(struct dxil_module *m, const struct dxil_type *struct_type,
352
                      nir_variable *var, const resource_array_layout *layout)
353 354
{
   const struct dxil_mdnode *fields[8];
355
   const struct glsl_type *type = glsl_without_array(var->type);
356

357
   fill_resource_metadata(m, fields, struct_type, var->name, layout);
358
   fields[6] = dxil_get_metadata_int32(m, DXIL_SAMPLER_KIND_DEFAULT); // sampler kind
359
   enum dxil_sampler_kind sampler_kind = glsl_sampler_type_is_shadow(type) ?
360 361
          DXIL_SAMPLER_KIND_COMPARISON : DXIL_SAMPLER_KIND_DEFAULT;
   fields[6] = dxil_get_metadata_int32(m, sampler_kind); // sampler kind
362 363 364 365 366
   fields[7] = NULL; // metadata

   return dxil_get_metadata_node(m, fields, ARRAY_SIZE(fields));
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static const struct dxil_type *
get_glsl_basetype(struct dxil_module *m, enum glsl_base_type type)
{
   switch (type) {
   case GLSL_TYPE_BOOL:
      return dxil_module_get_int_type(m, 1);

   case GLSL_TYPE_UINT:
   case GLSL_TYPE_INT:
      return dxil_module_get_int_type(m, 32);

   default:
      debug_printf("type: %s\n", glsl_get_type_name(glsl_scalar_type(type)));
      unreachable("unexpected GLSL type");
   }
}

static const struct dxil_type *
get_glsl_type(struct dxil_module *m, const struct glsl_type *type)
{
   assert(type);

389 390 391 392 393 394 395
   if (glsl_type_is_scalar(type))
      return get_glsl_basetype(m, glsl_get_base_type(type));

   if (glsl_type_is_array(type))
      return dxil_module_get_array_type(m,
         get_glsl_type(m, glsl_get_array_element(type)),
         glsl_get_length(type));
396

397
   unreachable("unexpected glsl type");
398 399 400
}


401
#define MAX_SRVS 128
402
#define MAX_UAVS 64
403
#define MAX_CBVS 64 // ??
404
#define MAX_SAMPLERS 64 // ??
405 406 407 408 409 410

struct dxil_def {
   const struct dxil_value *chans[NIR_MAX_VEC_COMPONENTS];
};

struct ntd_context {
411
   void *ralloc_ctx;
412
   const struct nir_to_dxil_options *opts;
413

414 415
   struct dxil_module mod;

416 417
   const struct dxil_mdnode *srv_metadata_nodes[MAX_SRVS];
   const struct dxil_value *srv_handles[MAX_SRVS];
418
   uint64_t srvs_used[2];
419
   unsigned num_srv_arrays;
420

421 422 423 424
   const struct dxil_mdnode *uav_metadata_nodes[MAX_UAVS];
   const struct dxil_value *uav_handles[MAX_UAVS];
   unsigned num_uavs;
   unsigned num_uav_arrays;
425

426 427 428 429
   const struct dxil_mdnode *cbv_metadata_nodes[MAX_CBVS];
   const struct dxil_value *cbv_handles[MAX_CBVS];
   unsigned num_cbvs;

430 431
   const struct dxil_mdnode *sampler_metadata_nodes[MAX_SAMPLERS];
   const struct dxil_value *sampler_handles[MAX_SAMPLERS];
432
   uint64_t samplers_used : MAX_SAMPLERS;
433
   unsigned num_sampler_arrays;
434

435
   struct dxil_resource resources[MAX_SRVS + MAX_UAVS + MAX_CBVS];
436 437
   unsigned num_resources;

438 439 440
   const struct dxil_mdnode *shader_property_nodes[6];
   size_t num_shader_property_nodes;

441 442
   struct dxil_def *defs;
   unsigned num_defs;
443
   struct hash_table *phis;
444

445
   const struct dxil_value *sharedvars;
446
   const struct dxil_value *scratchvars;
447 448
   struct hash_table *consts;

449
   nir_variable *ps_front_face;
450
   nir_variable *system_value[SYSTEM_VALUE_MAX];
451 452
};

453 454 455 456 457
static const char*
unary_func_name(enum dxil_intr intr)
{
   switch (intr) {
   case DXIL_INTR_COUNTBITS:
458
   case DXIL_INTR_FIRSTBIT_HI:
459
      return "dx.op.unaryBits";
460
   case DXIL_INTR_ISFINITE:
461 462
   case DXIL_INTR_ISNORMAL:
      return "dx.op.isSpecialFloat";
463 464 465 466 467
   }

   return "dx.op.unary";
}

468 469 470 471 472
static const struct dxil_value *
emit_unary_call(struct ntd_context *ctx, enum overload_type overload,
                enum dxil_intr intr,
                const struct dxil_value *op0)
{
473 474 475
   const struct dxil_func *func = dxil_get_function(&ctx->mod,
                                                    unary_func_name(intr),
                                                    overload);
476 477
   if (!func)
      return NULL;
478 479 480 481 482 483 484 485 486 487

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     op0
   };

488
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
489 490
}

491
static const struct dxil_value *
492 493
emit_binary_call(struct ntd_context *ctx, enum overload_type overload,
                 enum dxil_intr intr,
494 495
                 const struct dxil_value *op0, const struct dxil_value *op1)
{
496 497 498
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.binary", overload);
   if (!func)
      return NULL;
499 500 501 502 503 504 505 506 507 508 509

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     op0,
     op1
   };

510
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
511 512
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
static const struct dxil_value *
emit_tertiary_call(struct ntd_context *ctx, enum overload_type overload,
                   enum dxil_intr intr,
                   const struct dxil_value *op0,
                   const struct dxil_value *op1,
                   const struct dxil_value *op2)
{
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.tertiary", overload);
   if (!func)
      return NULL;

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, intr);
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     op0,
     op1,
     op2
   };

   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}

538 539 540
static const struct dxil_value *
emit_threadid_call(struct ntd_context *ctx, const struct dxil_value *comp)
{
541 542 543
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.threadId", DXIL_I32);
   if (!func)
      return NULL;
544

545 546
   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
       DXIL_INTR_THREAD_ID);
547 548 549 550 551 552 553 554
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     comp
   };

555
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
556 557
}

558 559 560 561
static const struct dxil_value *
emit_threadidingroup_call(struct ntd_context *ctx,
                          const struct dxil_value *comp)
{
562
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.threadIdInGroup", DXIL_I32);
563

564 565
   if (!func)
      return NULL;
566 567 568 569 570 571 572 573 574 575 576

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
       DXIL_INTR_THREAD_ID_IN_GROUP);
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     comp
   };

577
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
578 579
}

580 581 582
static const struct dxil_value *
emit_groupid_call(struct ntd_context *ctx, const struct dxil_value *comp)
{
583
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.groupId", DXIL_I32);
584

585 586
   if (!func)
      return NULL;
587 588 589 590 591 592 593 594 595 596 597

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
       DXIL_INTR_GROUP_ID);
   if (!opcode)
      return NULL;

   const struct dxil_value *args[] = {
     opcode,
     comp
   };

598
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
599 600
}

601 602 603 604 605
static const struct dxil_value *
emit_bufferload_call(struct ntd_context *ctx,
                     const struct dxil_value *handle,
                     const struct dxil_value *coord[2])
{
606 607 608
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.bufferLoad", DXIL_I32);
   if (!func)
      return NULL;
609 610 611 612 613

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
      DXIL_INTR_BUFFER_LOAD);
   const struct dxil_value *args[] = { opcode, handle, coord[0], coord[1] };

614
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
615
}
616

617 618 619 620 621
static bool
emit_bufferstore_call(struct ntd_context *ctx,
                      const struct dxil_value *handle,
                      const struct dxil_value *coord[2],
                      const struct dxil_value *value[4],
622 623
                      const struct dxil_value *write_mask,
                      enum overload_type overload)
624
{
625
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.bufferStore", overload);
626

627 628
   if (!func)
      return false;
629

630 631
   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
      DXIL_INTR_BUFFER_STORE);
632 633 634 635 636 637
   const struct dxil_value *args[] = {
      opcode, handle, coord[0], coord[1],
      value[0], value[1], value[2], value[3],
      write_mask
   };

638
   return dxil_emit_call_void(&ctx->mod, func,
639 640 641
                              args, ARRAY_SIZE(args));
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
static bool
emit_texturestore_call(struct ntd_context *ctx,
                       const struct dxil_value *handle,
                       const struct dxil_value *coord[3],
                       const struct dxil_value *value[4],
                       const struct dxil_value *write_mask,
                       enum overload_type overload)
{
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.textureStore", overload);

   if (!func)
      return false;

   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod,
      DXIL_INTR_TEXTURE_STORE);
   const struct dxil_value *args[] = {
      opcode, handle, coord[0], coord[1], coord[2],
      value[0], value[1], value[2], value[3],
      write_mask
   };

   return dxil_emit_call_void(&ctx->mod, func,
                              args, ARRAY_SIZE(args));
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
static const struct dxil_value *
emit_atomic_binop(struct ntd_context *ctx,
                  const struct dxil_value *handle,
                  enum dxil_atomic_op atomic_op,
                  const struct dxil_value *coord[3],
                  const struct dxil_value *value)
{
   const struct dxil_func *func = dxil_get_function(&ctx->mod, "dx.op.atomicBinOp", DXIL_I32);

   if (!func)
      return false;

   const struct dxil_value *opcode =
      dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_ATOMIC_BINOP);
   const struct dxil_value *atomic_op_value =
      dxil_module_get_int32_const(&ctx->mod, atomic_op);
   const struct dxil_value *args[] = {
      opcode, handle, atomic_op_value,
      coord[0], coord[1], coord[2], value
   };

   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
static const struct dxil_value *
emit_atomic_cmpxchg(struct ntd_context *ctx,
                    const struct dxil_value *handle,
                    const struct dxil_value *coord[3],
                    const struct dxil_value *cmpval,
                    const struct dxil_value *newval)
{
   const struct dxil_func *func =
      dxil_get_function(&ctx->mod, "dx.op.atomicCompareExchange", DXIL_I32);

   if (!func)
      return false;

   const struct dxil_value *opcode =
      dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_ATOMIC_CMPXCHG);
   const struct dxil_value *args[] = {
      opcode, handle, coord[0], coord[1], coord[2], cmpval, newval
   };

   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
}

713 714
static const struct dxil_value *
emit_createhandle_call(struct ntd_context *ctx,
715 716
                       enum dxil_resource_class resource_class,
                       unsigned resource_range_id,
717
                       const struct dxil_value *resource_range_index,
718
                       bool non_uniform_resource_index)
719
{
720
   const struct dxil_value *opcode = dxil_module_get_int32_const(&ctx->mod, DXIL_INTR_CREATE_HANDLE);
721 722 723 724 725
   const struct dxil_value *resource_class_value = dxil_module_get_int8_const(&ctx->mod, resource_class);
   const struct dxil_value *resource_range_id_value = dxil_module_get_int32_const(&ctx->mod, resource_range_id);
   const struct dxil_value *non_uniform_resource_index_value = dxil_module_get_int1_const(&ctx->mod, non_uniform_resource_index);
   if (!opcode || !resource_class_value || !resource_range_id_value ||
       !non_uniform_resource_index_value)
726 727 728 729
      return NULL;

   const struct dxil_value *args[] = {
      opcode,
730 731
      resource_class_value,
      resource_range_id_value,
732
      resource_range_index,
733
      non_uniform_resource_index_value
734 735
   };

736 737 738 739 740 741
   const struct dxil_func *func =
         dxil_get_function(&ctx->mod, "dx.op.createHandle", DXIL_NONE);

   if (!func)
         return NULL;

742
   return dxil_emit_call(&ctx->mod, func, args, ARRAY_SIZE(args));
743 744
}

745
static const struct dxil_value *
746
emit_createhandle_call_const_index(struct ntd_context *ctx,
747 748 749 750 751 752 753
                                   enum dxil_resource_class resource_class,
                                   unsigned resource_range_id,
                                   unsigned resource_range_index,
                                   bool non_uniform_resource_index)
{

   const struct dxil_value *resource_range_index_value = dxil_module_get_int32_const(&ctx->mod, resource_range_index);
754
   if (!resource_range_index_value)
755 756
      return NULL;

757 758 759
   return emit_createhandle_call(ctx, resource_class, resource_range_id,
                                 resource_range_index_value,
                                 non_uniform_resource_index);
760 761
}

762
static void
763 764
add_resource(struct ntd_context *ctx, enum dxil_resource_type type,
             const resource_array_layout *layout)
765 766 767 768
{
   assert(ctx->num_resources < ARRAY_SIZE(ctx->resources));
   ctx->resources[ctx->num_resources].resource_type = type;
   ctx->resources[ctx->num_resources].space = 0;
769 770
   ctx->resources[ctx->num_resources].lower_bound = layout->binding;
   ctx->resources[ctx->num_resources].upper_bound = layout->binding + layout->size - 1;
771 772 773
   ctx->num_resources++;
}

774
static bool
775
emit_srv(struct ntd_context *ctx, nir_variable *var, unsigned binding, unsigned count)
776
{
777
   assert(ctx->num_srv_arrays < ARRAY_SIZE(ctx->srv_metadata_nodes));
778

779
   unsigned id = ctx->num_srv_arrays;
780
   resource_array_layout layout = {id, binding, count};
781

782
   enum dxil_component_type comp_type = dxil_get_comp_type(var->type);
783
   enum dxil_resource_kind res_kind = dxil_get_resource_kind(var->type);
784
   const struct dxil_type *res_type = dxil_module_get_res_type(&ctx->mod, res_kind, comp_type, false /* readwrite */);
785 786
   const struct dxil_mdnode *srv_meta = emit_srv_metadata(&ctx->mod, res_type, var->name,
                                                          &layout, comp_type, res_kind);
787 788 789 790

   if (!srv_meta)
      return false;

791
   ctx->srv_metadata_nodes[ctx->num_srv_arrays++] = srv_meta;
792
   add_resource(ctx, DXIL_RES_SRV_TYPED, &layout);
793

794
   for (unsigned i = 0; i < count; ++i) {
795 796
      const struct dxil_value *handle =
         emit_createhandle_call_const_index(ctx, DXIL_RESOURCE_CLASS_SRV,
797
                                            id, binding + i, false);
798 799
      if (!handle)
         return false;
800

801
      int idx = var->data.binding + i;
802 803
      uint64_t bit = 1ull << (idx % 64);
      assert(!(ctx->srvs_used[idx / 64] & bit));
804
      ctx->srv_handles[idx] = handle;
805
      ctx->srvs_used[idx / 64] |= bit;
806

807
   }
808 809 810 811

   return true;
}

812
static bool
813
emit_globals(struct ntd_context *ctx, nir_shader *s)
814
{
815 816 817 818 819 820 821 822 823 824
   unsigned size = util_bitcount64(s->info.cs.global_inputs);

   nir_foreach_variable(var, &s->uniforms) {
      if (var->data.mode == nir_var_mem_ssbo)
         size++;
   }

   if (!size)
      return true;

825
   const struct dxil_type *type = dxil_module_get_int_type(&ctx->mod, 32);
826
   if (!type)
827 828
      return false;

829 830
   const struct dxil_type *struct_type =
      dxil_module_get_struct_type(&ctx->mod, NULL, &type, 1);
831
   if (!struct_type)
832 833
      return false;

834 835 836 837
   const struct dxil_type *array_type =
      dxil_module_get_array_type(&ctx->mod, struct_type, size);
   if (!array_type)
      return false;
838

839
   resource_array_layout layout = {0, 0, size};
840 841
   const struct dxil_mdnode *uav_meta =
      emit_uav_metadata(&ctx->mod, array_type,
842
                                   "globals", &layout,
843 844
                                   DXIL_COMP_TYPE_INVALID,
                                   DXIL_RESOURCE_KIND_RAW_BUFFER);
845 846 847
   if (!uav_meta)
      return false;

848
   ctx->uav_metadata_nodes[ctx->num_uav_arrays++] = uav_meta;
849 850
   if (ctx->num_uav_arrays > 8)
      ctx->mod.feats.use_64uavs = 1;
851 852
   /* Handles to UAVs used for kernel globals are created on-demand */
   ctx->num_uavs += size;
853
   add_resource(ctx, DXIL_RES_UAV_RAW, &layout);
854
   ctx->mod.raw_and_structured_buffers = true;
855 856 857
   return true;
}

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
static bool
emit_uav(struct ntd_context *ctx, nir_variable *var, unsigned count)
{
   assert(ctx->num_uav_arrays < ARRAY_SIZE(ctx->uav_metadata_nodes));
   assert(ctx->num_uavs < ARRAY_SIZE(ctx->uav_handles));

   unsigned id = ctx->num_uav_arrays;
   unsigned idx = var->data.binding;
   resource_array_layout layout = { id, idx, count };

   enum dxil_component_type comp_type = dxil_get_comp_type(var->type);
   enum dxil_resource_kind res_kind = dxil_get_resource_kind(var->type);
   const struct dxil_type *res_type = dxil_module_get_res_type(&ctx->mod, res_kind, comp_type, true /* readwrite */);
   const struct dxil_mdnode *uav_meta = emit_uav_metadata(&ctx->mod, res_type, var->name,
                                                          &layout, comp_type, res_kind);

   if (!uav_meta)
      return false;

   ctx->uav_metadata_nodes[ctx->num_uav_arrays++] = uav_meta;
878 879
   if (ctx->num_uav_arrays > 8)
      ctx->mod.feats.use_64uavs = 1;
880 881 882 883 884 885 886 887 888 889 890 891 892 893
   add_resource(ctx, DXIL_RES_UAV_TYPED, &layout);

   for (unsigned i = 0; i < count; ++i) {
      const struct dxil_value *handle = emit_createhandle_call_const_index(ctx, DXIL_RESOURCE_CLASS_UAV,
                                                                           id, idx + i, false);
      if (!handle)
         return false;

      ctx->uav_handles[ctx->num_uavs++] = handle;
   }

   return true;
}

894 895 896 897 898 899 900 901 902 903
static unsigned get_dword_size(const struct glsl_type *type)
{
   unsigned factor = 1;
   if (glsl_type_is_array(type)) {
      factor = glsl_get_aoa_size(type);
      type = glsl_without_array(type);
   }
   return (factor * glsl_get_components(type));
}

904 905 906 907 908
static bool
var_fill_const_array_with_vector_or_scalar(struct ntd_context *ctx,
                                           const struct nir_constant *c,
                                           const struct glsl_type *type,
                                           void *const_vals,
909
                                           unsigned int offset)
910 911 912 913 914
{
   assert(glsl_type_is_vector_or_scalar(type));
   enum glsl_base_type base_type = glsl_get_base_type(type);
   unsigned int components = glsl_get_vector_elements(type);
   unsigned bit_size = glsl_get_bit_size(type);
915
   unsigned int increment = bit_size / 8;
916 917 918

   for (unsigned int comp = 0; comp < components; comp++) {
      uint8_t *dst = (uint8_t *)const_vals + offset;
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

      switch (bit_size) {
      case 64:
         memcpy(dst, &c->values[comp].u64, sizeof(c->values[0].u64));
         break;
      case 32:
         memcpy(dst, &c->values[comp].u32, sizeof(c->values[0].u32));
         break;
      case 16:
         memcpy(dst, &c->values[comp].u16, sizeof(c->values[0].u16));
         break;
      case 8:
         assert(glsl_base_type_is_integer(base_type));
         memcpy(dst, &c->values[comp].u8, sizeof(c->values[0].u8));
         break;
      default:
         unreachable("unexpeted bit-size");
936 937 938 939 940 941 942 943 944 945 946
      }

      offset += increment;
   }

   return true;
}

static bool
var_fill_const_array(struct ntd_context *ctx, const struct nir_constant *c,
                     const struct glsl_type *type, void *const_vals,
947
                     unsigned int offset)
948 949 950 951 952 953
{
   assert(!glsl_type_is_interface(type));

   if (glsl_type_is_vector_or_scalar(type)) {
      return var_fill_const_array_with_vector_or_scalar(ctx, c, type,
                                                        const_vals,
954
                                                        offset);
955 956 957
   } else if (glsl_type_is_array(type)) {
      assert(!glsl_type_is_unsized_array(type));
      const struct glsl_type *without = glsl_without_array(type);
958
      unsigned stride = glsl_get_explicit_stride(without);
959 960 961 962
      enum glsl_base_type without_base = glsl_get_base_type(without);

      for (unsigned elt = 0; elt < glsl_get_length(type); elt++) {
         if (!var_fill_const_array(ctx, c->elements[elt], without,
963
                                   const_vals, offset + (elt * stride))) {
964 965 966 967 968 969 970 971
            return false;
         }
         offset += glsl_get_cl_size(without);
      }
      return true;
   } else if (glsl_type_is_struct(type)) {
      for (unsigned int elt = 0; elt < glsl_get_length(type); elt++) {
         const struct glsl_type *elt_type = glsl_get_struct_field(type, elt);
972
         unsigned field_offset = glsl_get_struct_field_offset(type, elt);
973 974

         if (!var_fill_const_array(ctx, c->elements[elt],
975 976
                                   elt_type, const_vals,
                                   offset + field_offset)) {