Debian Codegraph @

src/kosmickrisp/compiler/msl_iomap.c

raw symbols SCIP
1 /*
2 * Copyright 2025 LunarG, Inc.
3 * Copyright 2025 Google LLC
4 * SPDX-License-Identifier: MIT
5 */
6
7 /* This file primarily concerns itself with mapping from the NIR (and Vulkan)
8 * model of I/O to the Metal one. */
9
10 #include "msl_private.h"
11
12 #include "nir_builder.h"
13
14 /* Mapping from alu type to Metal scalar type */
15 static const char *
16 alu_type_to_string(nir_alu_type type)
17 {
18 switch (type) {
19 case nir_type_uint8:
20 return "uchar";
21 case nir_type_uint16:
22 return "ushort";
23 case nir_type_uint32:
24 return "uint";
25 case nir_type_uint64:
26 return "ulong";
27 case nir_type_int8:
28 return "char";
29 case nir_type_int16:
30 return "short";
31 case nir_type_int32:
32 return "int";
33 case nir_type_int64:
34 return "long";
35 case nir_type_float16:
36 return "half";
37 case nir_type_float32:
38 return "float";
39 case nir_type_bool8:
40 return "bool";
41 default:
42 UNREACHABLE("Unsupported nir_alu_type");
43 }
44 };
45
46 /* Type suffix for a vector of a given size. */
47 static const char *vector_suffixes[] = {
48 [1] = "",
49 [2] = "2",
50 [3] = "3",
51 [4] = "4",
52 };
53
54 /* The type names of the generated output structs */
55 static const char *VERTEX_OUTPUT_TYPE = "VertexOut";
56 static const char *FRAGMENT_OUTPUT_TYPE = "FragmentOut";
57
58 /* Mapping from NIR's varying slots to the generated struct member name */
59 static const struct {
60 const char *name;
61 bool user;
62 bool scalarized;
63 } VARYING_SLOT_INFO[NUM_TOTAL_VARYING_SLOTS] = {
64 [VARYING_SLOT_POS] = {"position"},
65 [VARYING_SLOT_PSIZ] = {"point_size"},
66 [VARYING_SLOT_PRIMITIVE_ID] = {"primitive_id"},
67 [VARYING_SLOT_LAYER] = {"render_target_array_index"},
68 [VARYING_SLOT_VIEWPORT] = {"viewport_array_index"},
69 [VARYING_SLOT_CLIP_DIST0] = {"clip_0", .user = true, .scalarized = true},
70 [VARYING_SLOT_CLIP_DIST1] = {"clip_1", .user = true, .scalarized = true},
71 [VARYING_SLOT_VAR0] = {"vary_00", .user = true},
72 [VARYING_SLOT_VAR1] = {"vary_01", .user = true},
73 [VARYING_SLOT_VAR2] = {"vary_02", .user = true},
74 [VARYING_SLOT_VAR3] = {"vary_03", .user = true},
75 [VARYING_SLOT_VAR4] = {"vary_04", .user = true},
76 [VARYING_SLOT_VAR5] = {"vary_05", .user = true},
77 [VARYING_SLOT_VAR6] = {"vary_06", .user = true},
78 [VARYING_SLOT_VAR7] = {"vary_07", .user = true},
79 [VARYING_SLOT_VAR8] = {"vary_08", .user = true},
80 [VARYING_SLOT_VAR9] = {"vary_09", .user = true},
81 [VARYING_SLOT_VAR10] = {"vary_10", .user = true},
82 [VARYING_SLOT_VAR11] = {"vary_11", .user = true},
83 [VARYING_SLOT_VAR12] = {"vary_12", .user = true},
84 [VARYING_SLOT_VAR13] = {"vary_13", .user = true},
85 [VARYING_SLOT_VAR14] = {"vary_14", .user = true},
86 [VARYING_SLOT_VAR15] = {"vary_15", .user = true},
87 [VARYING_SLOT_VAR16] = {"vary_16", .user = true},
88 [VARYING_SLOT_VAR17] = {"vary_17", .user = true},
89 [VARYING_SLOT_VAR18] = {"vary_18", .user = true},
90 [VARYING_SLOT_VAR19] = {"vary_19", .user = true},
91 [VARYING_SLOT_VAR20] = {"vary_20", .user = true},
92 [VARYING_SLOT_VAR21] = {"vary_21", .user = true},
93 [VARYING_SLOT_VAR22] = {"vary_22", .user = true},
94 [VARYING_SLOT_VAR23] = {"vary_23", .user = true},
95 [VARYING_SLOT_VAR24] = {"vary_24", .user = true},
96 [VARYING_SLOT_VAR25] = {"vary_25", .user = true},
97 [VARYING_SLOT_VAR26] = {"vary_26", .user = true},
98 [VARYING_SLOT_VAR27] = {"vary_27", .user = true},
99 [VARYING_SLOT_VAR28] = {"vary_28", .user = true},
100 [VARYING_SLOT_VAR29] = {"vary_29", .user = true},
101 [VARYING_SLOT_VAR30] = {"vary_30", .user = true},
102 [VARYING_SLOT_VAR31] = {"vary_31", .user = true},
103 };
104
105 static void
106 varying_slot_name(struct nir_to_msl_ctx *ctx, unsigned location,
107 unsigned component)
108 {
109 if (VARYING_SLOT_INFO[location].scalarized) {
110 P(ctx, "%s_%c", VARYING_SLOT_INFO[location].name, "xyzw"[component]);
111 } else {
112 P(ctx, "%s", VARYING_SLOT_INFO[location].name);
113 }
114 }
115
116 static void
117 varying_slot_semantic(struct nir_to_msl_ctx *ctx, unsigned location,
118 unsigned component)
119 {
120 if (VARYING_SLOT_INFO[location].user) {
121 P(ctx, "[[user(");
122 varying_slot_name(ctx, location, component);
123 P(ctx, ")]]");
124 } else {
125 P(ctx, "[[");
126 varying_slot_name(ctx, location, component);
127 P(ctx, "]]");
128 }
129 }
130
131 /* Mapping from NIR fragment output slot to MSL struct member name */
132 static const char *FS_OUTPUT_NAME[] = {
133 [FRAG_RESULT_DEPTH] = "depth_out",
134 [FRAG_RESULT_STENCIL] = "stencil_out",
135 [FRAG_RESULT_SAMPLE_MASK] = "sample_mask_out",
136 [FRAG_RESULT_DATA0] = "color_0",
137 [FRAG_RESULT_DATA1] = "color_1",
138 [FRAG_RESULT_DATA2] = "color_2",
139 [FRAG_RESULT_DATA3] = "color_3",
140 [FRAG_RESULT_DATA4] = "color_4",
141 [FRAG_RESULT_DATA5] = "color_5",
142 [FRAG_RESULT_DATA6] = "color_6",
143 [FRAG_RESULT_DATA7] = "color_7",
144 };
145
146 /* Mapping from NIR fragment output slot to MSL struct member attribute */
147 static const char *FS_OUTPUT_SEMANTIC[] = {
148 [FRAG_RESULT_DEPTH] = "", // special case, depends on depth layout
149 [FRAG_RESULT_STENCIL] = "stencil", [FRAG_RESULT_SAMPLE_MASK] = "sample_mask",
150 [FRAG_RESULT_DATA0] = "color(0)", [FRAG_RESULT_DATA1] = "color(1)",
151 [FRAG_RESULT_DATA2] = "color(2)", [FRAG_RESULT_DATA3] = "color(3)",
152 [FRAG_RESULT_DATA4] = "color(4)", [FRAG_RESULT_DATA5] = "color(5)",
153 [FRAG_RESULT_DATA6] = "color(6)", [FRAG_RESULT_DATA7] = "color(7)",
154 };
155
156 const char *depth_layout_arg[8] = {
157 [FRAG_DEPTH_LAYOUT_ANY] = "any",
158 [FRAG_DEPTH_LAYOUT_GREATER] = "greater",
159 [FRAG_DEPTH_LAYOUT_LESS] = "less",
160 [FRAG_DEPTH_LAYOUT_UNCHANGED] = "any",
161 };
162
163 /* Generate the struct definition for the vertex shader return value */
164 static void
165 vs_output_block(nir_shader *shader, struct nir_to_msl_ctx *ctx)
166 {
167 P(ctx, "struct %s {\n", VERTEX_OUTPUT_TYPE);
168 ctx->indentlevel++;
169 u_foreach_bit64(location, shader->info.outputs_written) {
170 struct io_slot_info info = ctx->outputs_info[location];
171 bool scalarized = VARYING_SLOT_INFO[location].scalarized;
172 const char *type = alu_type_to_string(info.type);
173 const char *vector_suffix =
174 scalarized ? "" : vector_suffixes[info.num_components];
175 unsigned components = scalarized ? info.num_components : 1;
176 for (int c = 0; c < components; c++) {
177 P_IND(ctx, "%s%s ", type, vector_suffix);
178 varying_slot_name(ctx, location, c);
179 P(ctx, " ");
180 varying_slot_semantic(ctx, location, c);
181 P(ctx, ";\n");
182 }
183 }
184
185 if (shader->info.clip_distance_array_size)
186 P_IND(ctx, "float gl_ClipDistance [[clip_distance]] [%d];",
187 shader->info.clip_distance_array_size);
188 ctx->indentlevel--;
189 P(ctx, "};\n");
190 }
191
192 static void
193 fs_input_interpolant(struct nir_to_msl_ctx *ctx, uint64_t location)
194 {
195 struct io_slot_info info = ctx->inputs_info[location];
196 bool scalarized = VARYING_SLOT_INFO[location].scalarized;
197 const char *type = alu_type_to_string(info.type);
198 const char *vector_suffix =
199 scalarized ? "" : vector_suffixes[info.num_components];
200 unsigned components = scalarized ? info.num_components : 1;
201 const char *interp = "";
202 switch (info.interpolation) {
203 case INTERP_MODE_SMOOTH:
204 interp = "interpolation::perspective";
205 break;
206 case INTERP_MODE_NOPERSPECTIVE:
207 interp = "interpolation::no_perspective";
208 break;
209 default:
210 UNREACHABLE("Only perspective and no-perspective are allowed");
211 }
212 for (int c = 0; c < components; c++) {
213 P_IND(ctx, "interpolant<%s%s, %s> ", type, vector_suffix, interp);
214 varying_slot_name(ctx, location, c);
215 P(ctx, " ");
216 varying_slot_semantic(ctx, location, c);
217 P(ctx, ";\n");
218 }
219 }
220
221 static void
222 fs_input_no_interpolant(struct nir_to_msl_ctx *ctx, uint64_t location)
223 {
224 struct io_slot_info info = ctx->inputs_info[location];
225 bool scalarized = VARYING_SLOT_INFO[location].scalarized;
226 const char *type = alu_type_to_string(info.type);
227 const char *vector_suffix =
228 scalarized ? "" : vector_suffixes[info.num_components];
229 unsigned components = scalarized ? info.num_components : 1;
230 const char *interp = "";
231 switch (info.interpolation) {
232 case INTERP_MODE_NOPERSPECTIVE:
233 if (info.centroid)
234 interp = "[[centroid_no_perspective]]";
235 else if (info.sample)
236 interp = "[[sample_no_perspective]]";
237 else
238 interp = "[[center_no_perspective]]";
239 break;
240 case INTERP_MODE_FLAT:
241 interp = "[[flat]]";
242 break;
243 default:
244 if (info.centroid)
245 interp = "[[centroid_perspective]]";
246 else if (info.sample)
247 interp = "[[sample_perspective]]";
248 break;
249 }
250 for (int c = 0; c < components; c++) {
251 P_IND(ctx, "%s%s ", type, vector_suffix);
252 varying_slot_name(ctx, location, c);
253 P(ctx, " ");
254 varying_slot_semantic(ctx, location, c);
255 P(ctx, " %s;\n", interp);
256 }
257 }
258
259 /* Generate the struct definition for the fragment shader input argument */
260 static void
261 fs_input_block(nir_shader *shader, struct nir_to_msl_ctx *ctx)
262 {
263 P(ctx, "struct FragmentIn {\n");
264 ctx->indentlevel++;
265 u_foreach_bit64(location, shader->info.inputs_read) {
266 struct io_slot_info info = ctx->inputs_info[location];
267 if (info.uses_interpolant)
268 fs_input_interpolant(ctx, location);
269 else
270 fs_input_no_interpolant(ctx, location);
271 }
272
273 /* Enable reading from framebuffer */
274 u_foreach_bit64(location, shader->info.outputs_read) {
275 struct io_slot_info info = ctx->outputs_info[location];
276 const char *type = alu_type_to_string(info.type);
277 const char *vector_suffix = vector_suffixes[info.num_components];
278 P_IND(ctx, "%s%s ", type, vector_suffix);
279 P(ctx, "%s [[%s, raster_order_group(0)]];\n", FS_OUTPUT_NAME[location],
280 FS_OUTPUT_SEMANTIC[location]);
281 }
282
283 ctx->indentlevel--;
284 P(ctx, "};\n");
285 }
286
287 /* Generate the struct definition for the fragment shader return value */
288 static void
289 fs_output_block(nir_shader *shader, struct nir_to_msl_ctx *ctx)
290 {
291 P_IND(ctx, "struct %s {\n", FRAGMENT_OUTPUT_TYPE);
292 ctx->indentlevel++;
293 u_foreach_bit64(location, shader->info.outputs_written) {
294 struct io_slot_info info = ctx->outputs_info[location];
295 const char *type = alu_type_to_string(info.type);
296 const char *vector_suffix = vector_suffixes[info.num_components];
297 P_IND(ctx, "%s%s ", type, vector_suffix);
298 if (location == FRAG_RESULT_DEPTH) {
299 enum gl_frag_depth_layout depth_layout = shader->info.fs.depth_layout;
300 assert(depth_layout_arg[depth_layout]);
301 P(ctx, "%s [[depth(%s)]];\n", FS_OUTPUT_NAME[location],
302 depth_layout_arg[depth_layout]);
303 } else {
304 // TODO: scalarized fs outputs
305 P(ctx, "%s [[%s]];\n", FS_OUTPUT_NAME[location],
306 FS_OUTPUT_SEMANTIC[location]);
307 }
308 }
309 ctx->indentlevel--;
310 P_IND(ctx, "};\n")
311 }
312
313 struct gather_ctx {
314 struct io_slot_info *input;
315 struct io_slot_info *output;
316 };
317
318 static bool
319 msl_nir_gather_io_info(nir_builder *b, nir_intrinsic_instr *intrin, void *data)
320 {
321 struct gather_ctx *ctx = (struct gather_ctx *)data;
322 switch (intrin->intrinsic) {
323 case nir_intrinsic_load_interpolated_input: {
324 unsigned component = nir_intrinsic_component(intrin);
325 struct nir_io_semantics io = nir_intrinsic_io_semantics(intrin);
326 assert(io.num_slots == 1u && "We don't support arrays");
327
328 unsigned location = nir_src_as_uint(intrin->src[1u]) + io.location;
329 ctx->input[location].type = nir_intrinsic_dest_type(intrin);
330 ctx->input[location].num_components =
331 MAX2(ctx->input[location].num_components,
332 intrin->num_components + component);
333 assert(ctx->input[location].num_components <= 4u &&
334 "Cannot have more than a vec4");
335
336 nir_intrinsic_instr *interp_intrin =
337 nir_src_as_intrinsic(intrin->src[0u]);
338 ctx->input[location].interpolation =
339 nir_intrinsic_interp_mode(interp_intrin);
340 ctx->input[location].centroid =
341 interp_intrin->intrinsic == nir_intrinsic_load_barycentric_centroid;
342 ctx->input[location].sample =
343 interp_intrin->intrinsic == nir_intrinsic_load_barycentric_sample;
344 ctx->input[location].uses_interpolant |=
345 interp_intrin->intrinsic == nir_intrinsic_load_barycentric_at_sample ||
346 interp_intrin->intrinsic == nir_intrinsic_load_barycentric_at_offset;
347 break;
348 }
349 case nir_intrinsic_load_input: {
350 unsigned component = nir_intrinsic_component(intrin);
351 struct nir_io_semantics io = nir_intrinsic_io_semantics(intrin);
352 assert(io.num_slots == 1u && "We don't support arrays");
353
354 unsigned location = nir_src_as_uint(intrin->src[0u]) + io.location;
355 ctx->input[location].type = nir_intrinsic_dest_type(intrin);
356 ctx->input[location].interpolation = INTERP_MODE_FLAT;
357 ctx->input[location].num_components =
358 MAX2(ctx->input[location].num_components,
359 intrin->num_components + component);
360 assert(ctx->input[location].num_components <= 4u &&
361 "Cannot have more than a vec4");
362 break;
363 }
364 case nir_intrinsic_load_output: {
365 unsigned component = nir_intrinsic_component(intrin);
366 struct nir_io_semantics io = nir_intrinsic_io_semantics(intrin);
367 assert(io.num_slots == 1u && "We don't support arrays");
368
369 unsigned location = nir_src_as_uint(intrin->src[0u]) + io.location;
370 ctx->output[location].type = nir_intrinsic_dest_type(intrin);
371 ctx->output[location].num_components =
372 MAX2(ctx->output[location].num_components,
373 intrin->num_components + component);
374 assert(ctx->output[location].num_components <= 4u &&
375 "Cannot have more than a vec4");
376 break;
377 }
378 case nir_intrinsic_store_output: {
379 unsigned component = nir_intrinsic_component(intrin);
380 unsigned write_mask = nir_intrinsic_write_mask(intrin);
381 struct nir_io_semantics io = nir_intrinsic_io_semantics(intrin);
382 assert(io.num_slots == 1u && "We don't support arrays");
383
384 /* Due to nir_lower_blend that doesn't generate intrinsics with the same
385 * num_components as destination, we need to compute current store's
386 * num_components using offset and mask. */
387 unsigned num_components = component + 1u;
388 unsigned mask_left_most_index = 0u;
389 for (unsigned i = 0u; i < intrin->num_components; ++i) {
390 if ((write_mask >> i) & 1u)
391 mask_left_most_index = i;
392 }
393 num_components += mask_left_most_index;
394 unsigned location = nir_src_as_uint(intrin->src[1u]) + io.location;
395 ctx->output[location].type = nir_intrinsic_src_type(intrin);
396 ctx->output[location].num_components =
397 MAX3(ctx->output[location].num_components, num_components,
398 intrin->num_components);
399 assert(ctx->output[location].num_components <= 4u &&
400 "Cannot have more than a vec4");
401 break;
402 }
403 default:
404 break;
405 }
406
407 return false;
408 }
409
410 void
411 msl_gather_io_info(struct nir_to_msl_ctx *ctx,
412 struct io_slot_info *info_array_input,
413 struct io_slot_info *info_array_output)
414 {
415 struct gather_ctx gather_ctx = {
416 .input = info_array_input,
417 .output = info_array_output,
418 };
419 nir_shader_intrinsics_pass(ctx->shader, msl_nir_gather_io_info,
420 nir_metadata_all, &gather_ctx);
421 }
422
423 /* Generate all the struct definitions needed for shader I/O */
424 void
425 msl_emit_io_blocks(struct nir_to_msl_ctx *ctx, nir_shader *shader)
426 {
427 switch (ctx->shader->info.stage) {
428 case MESA_SHADER_VERTEX:
429 vs_output_block(shader, ctx);
430 break;
431 case MESA_SHADER_FRAGMENT:
432 fs_input_block(shader, ctx);
433 fs_output_block(shader, ctx);
434 break;
435 case MESA_SHADER_COMPUTE:
436 break;
437 default:
438 assert(0);
439 }
440 // TODO_KOSMICKRISP This should not exist. We need to create input structs in
441 // nir that will later be translated
442 P(ctx, "struct Buffer {\n");
443 ctx->indentlevel++;
444 P_IND(ctx, "uint64_t contents[1];\n"); // TODO_KOSMICKRISP This should not be
445 // a cpu pointer
446 ctx->indentlevel--;
447 P(ctx, "};\n")
448
449 P(ctx, "struct SamplerTable {\n");
450 ctx->indentlevel++;
451 P_IND(ctx, "sampler handles[1024];\n");
452 ctx->indentlevel--;
453 P(ctx, "};\n")
454 }
455
456 void
457 msl_emit_output_var(struct nir_to_msl_ctx *ctx, nir_shader *shader)
458 {
459 switch (shader->info.stage) {
460 case MESA_SHADER_VERTEX:
461 P_IND(ctx, "%s out = {};\n", VERTEX_OUTPUT_TYPE);
462 break;
463 case MESA_SHADER_FRAGMENT:
464 P_IND(ctx, "%s out = {};\n", FRAGMENT_OUTPUT_TYPE);
465
466 /* Load inputs to output */
467 u_foreach_bit64(location, shader->info.outputs_read) {
468 P_IND(ctx, "out.%s = in.%s;\n", FS_OUTPUT_NAME[location],
469 FS_OUTPUT_NAME[location]);
470 }
471 break;
472 default:
473 break;
474 }
475 }
476
477 void
478 msl_output_name(struct nir_to_msl_ctx *ctx, unsigned location,
479 unsigned component)
480 {
481 P(ctx, "out.")
482 switch (ctx->shader->info.stage) {
483 case MESA_SHADER_VERTEX:
484 varying_slot_name(ctx, location, component);
485 break;
486 case MESA_SHADER_FRAGMENT:
487 P(ctx, "%s", FS_OUTPUT_NAME[location]);
488 break;
489 default:
490 UNREACHABLE("Invalid shader stage");
491 }
492 }
493
494 void
495 msl_input_name(struct nir_to_msl_ctx *ctx, unsigned location,
496 unsigned component)
497 {
498 P(ctx, "in.");
499 switch (ctx->shader->info.stage) {
500 case MESA_SHADER_FRAGMENT:
501 varying_slot_name(ctx, location, component);
502 break;
503 default:
504 UNREACHABLE("Invalid shader stage");
505 }
506 }
507
508 uint32_t
509 msl_input_num_components(struct nir_to_msl_ctx *ctx, uint32_t location)
510 {
511 if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT &&
512 VARYING_SLOT_INFO[location].scalarized)
513 return 1;
514 else
515 return ctx->inputs_info[location].num_components;
516 }
517
518 uint32_t
519 msl_output_num_components(struct nir_to_msl_ctx *ctx, uint32_t location)
520 {
521 if (ctx->shader->info.stage == MESA_SHADER_VERTEX &&
522 VARYING_SLOT_INFO[location].scalarized)
523 return 1;
524 else
525 return ctx->outputs_info[location].num_components;
526 }