radeonsi/gfx10: change location of instance ID shader input
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "util/u_memory.h"
26 #include "util/u_string.h"
27 #include "tgsi/tgsi_build.h"
28 #include "tgsi/tgsi_strings.h"
29 #include "tgsi/tgsi_util.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "tgsi/tgsi_from_mesa.h"
32
33 #include "ac_binary.h"
34 #include "ac_exp_param.h"
35 #include "ac_shader_util.h"
36 #include "ac_rtld.h"
37 #include "ac_llvm_util.h"
38 #include "si_shader_internal.h"
39 #include "si_pipe.h"
40 #include "sid.h"
41
42 #include "compiler/nir/nir.h"
43
44 static const char scratch_rsrc_dword0_symbol[] =
45 "SCRATCH_RSRC_DWORD0";
46
47 static const char scratch_rsrc_dword1_symbol[] =
48 "SCRATCH_RSRC_DWORD1";
49
50 static void si_init_shader_ctx(struct si_shader_context *ctx,
51 struct si_screen *sscreen,
52 struct ac_llvm_compiler *compiler);
53
54 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action,
55 struct lp_build_tgsi_context *bld_base,
56 struct lp_build_emit_data *emit_data);
57
58 static void si_dump_shader_key(unsigned processor, const struct si_shader *shader,
59 FILE *f);
60
61 static void si_build_vs_prolog_function(struct si_shader_context *ctx,
62 union si_shader_part_key *key);
63 static void si_build_tcs_epilog_function(struct si_shader_context *ctx,
64 union si_shader_part_key *key);
65 static void si_build_ps_prolog_function(struct si_shader_context *ctx,
66 union si_shader_part_key *key);
67 static void si_build_ps_epilog_function(struct si_shader_context *ctx,
68 union si_shader_part_key *key);
69 static void si_fix_resource_usage(struct si_screen *sscreen,
70 struct si_shader *shader);
71
72 /* Ideally pass the sample mask input to the PS epilog as v14, which
73 * is its usual location, so that the shader doesn't have to add v_mov.
74 */
75 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 14
76
77 static bool llvm_type_is_64bit(struct si_shader_context *ctx,
78 LLVMTypeRef type)
79 {
80 if (type == ctx->ac.i64 || type == ctx->ac.f64)
81 return true;
82
83 return false;
84 }
85
86 /** Whether the shader runs as a combination of multiple API shaders */
87 static bool is_multi_part_shader(struct si_shader_context *ctx)
88 {
89 if (ctx->screen->info.chip_class <= GFX8)
90 return false;
91
92 return ctx->shader->key.as_ls ||
93 ctx->shader->key.as_es ||
94 ctx->type == PIPE_SHADER_TESS_CTRL ||
95 ctx->type == PIPE_SHADER_GEOMETRY;
96 }
97
98 /** Whether the shader runs on a merged HW stage (LSHS or ESGS) */
99 static bool is_merged_shader(struct si_shader_context *ctx)
100 {
101 return ctx->shader->key.as_ngg || is_multi_part_shader(ctx);
102 }
103
104 void si_init_function_info(struct si_function_info *fninfo)
105 {
106 fninfo->num_params = 0;
107 fninfo->num_sgpr_params = 0;
108 }
109
110 unsigned add_arg_assign(struct si_function_info *fninfo,
111 enum si_arg_regfile regfile, LLVMTypeRef type,
112 LLVMValueRef *assign)
113 {
114 assert(regfile != ARG_SGPR || fninfo->num_sgpr_params == fninfo->num_params);
115
116 unsigned idx = fninfo->num_params++;
117 assert(idx < ARRAY_SIZE(fninfo->types));
118
119 if (regfile == ARG_SGPR)
120 fninfo->num_sgpr_params = fninfo->num_params;
121
122 fninfo->types[idx] = type;
123 fninfo->assign[idx] = assign;
124 return idx;
125 }
126
127 static unsigned add_arg(struct si_function_info *fninfo,
128 enum si_arg_regfile regfile, LLVMTypeRef type)
129 {
130 return add_arg_assign(fninfo, regfile, type, NULL);
131 }
132
133 static void add_arg_assign_checked(struct si_function_info *fninfo,
134 enum si_arg_regfile regfile, LLVMTypeRef type,
135 LLVMValueRef *assign, unsigned idx)
136 {
137 MAYBE_UNUSED unsigned actual = add_arg_assign(fninfo, regfile, type, assign);
138 assert(actual == idx);
139 }
140
141 static void add_arg_checked(struct si_function_info *fninfo,
142 enum si_arg_regfile regfile, LLVMTypeRef type,
143 unsigned idx)
144 {
145 add_arg_assign_checked(fninfo, regfile, type, NULL, idx);
146 }
147
148 /**
149 * Returns a unique index for a per-patch semantic name and index. The index
150 * must be less than 32, so that a 32-bit bitmask of used inputs or outputs
151 * can be calculated.
152 */
153 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name, unsigned index)
154 {
155 switch (semantic_name) {
156 case TGSI_SEMANTIC_TESSOUTER:
157 return 0;
158 case TGSI_SEMANTIC_TESSINNER:
159 return 1;
160 case TGSI_SEMANTIC_PATCH:
161 assert(index < 30);
162 return 2 + index;
163
164 default:
165 assert(!"invalid semantic name");
166 return 0;
167 }
168 }
169
170 /**
171 * Returns a unique index for a semantic name and index. The index must be
172 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
173 * calculated.
174 */
175 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index,
176 unsigned is_varying)
177 {
178 switch (semantic_name) {
179 case TGSI_SEMANTIC_POSITION:
180 return 0;
181 case TGSI_SEMANTIC_GENERIC:
182 /* Since some shader stages use the the highest used IO index
183 * to determine the size to allocate for inputs/outputs
184 * (in LDS, tess and GS rings). GENERIC should be placed right
185 * after POSITION to make that size as small as possible.
186 */
187 if (index < SI_MAX_IO_GENERIC)
188 return 1 + index;
189
190 assert(!"invalid generic index");
191 return 0;
192 case TGSI_SEMANTIC_PSIZE:
193 return SI_MAX_IO_GENERIC + 1;
194 case TGSI_SEMANTIC_CLIPDIST:
195 assert(index <= 1);
196 return SI_MAX_IO_GENERIC + 2 + index;
197 case TGSI_SEMANTIC_FOG:
198 return SI_MAX_IO_GENERIC + 4;
199 case TGSI_SEMANTIC_LAYER:
200 return SI_MAX_IO_GENERIC + 5;
201 case TGSI_SEMANTIC_VIEWPORT_INDEX:
202 return SI_MAX_IO_GENERIC + 6;
203 case TGSI_SEMANTIC_PRIMID:
204 return SI_MAX_IO_GENERIC + 7;
205 case TGSI_SEMANTIC_COLOR:
206 assert(index < 2);
207 return SI_MAX_IO_GENERIC + 8 + index;
208 case TGSI_SEMANTIC_BCOLOR:
209 assert(index < 2);
210 /* If it's a varying, COLOR and BCOLOR alias. */
211 if (is_varying)
212 return SI_MAX_IO_GENERIC + 8 + index;
213 else
214 return SI_MAX_IO_GENERIC + 10 + index;
215 case TGSI_SEMANTIC_TEXCOORD:
216 assert(index < 8);
217 STATIC_ASSERT(SI_MAX_IO_GENERIC + 12 + 8 <= 63);
218 return SI_MAX_IO_GENERIC + 12 + index;
219 case TGSI_SEMANTIC_CLIPVERTEX:
220 return 63;
221 default:
222 fprintf(stderr, "invalid semantic name = %u\n", semantic_name);
223 assert(!"invalid semantic name");
224 return 0;
225 }
226 }
227
228 /**
229 * Get the value of a shader input parameter and extract a bitfield.
230 */
231 static LLVMValueRef unpack_llvm_param(struct si_shader_context *ctx,
232 LLVMValueRef value, unsigned rshift,
233 unsigned bitwidth)
234 {
235 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMFloatTypeKind)
236 value = ac_to_integer(&ctx->ac, value);
237
238 if (rshift)
239 value = LLVMBuildLShr(ctx->ac.builder, value,
240 LLVMConstInt(ctx->i32, rshift, 0), "");
241
242 if (rshift + bitwidth < 32) {
243 unsigned mask = (1 << bitwidth) - 1;
244 value = LLVMBuildAnd(ctx->ac.builder, value,
245 LLVMConstInt(ctx->i32, mask, 0), "");
246 }
247
248 return value;
249 }
250
251 LLVMValueRef si_unpack_param(struct si_shader_context *ctx,
252 unsigned param, unsigned rshift,
253 unsigned bitwidth)
254 {
255 LLVMValueRef value = LLVMGetParam(ctx->main_fn, param);
256
257 return unpack_llvm_param(ctx, value, rshift, bitwidth);
258 }
259
260 static LLVMValueRef get_rel_patch_id(struct si_shader_context *ctx)
261 {
262 switch (ctx->type) {
263 case PIPE_SHADER_TESS_CTRL:
264 return unpack_llvm_param(ctx, ctx->abi.tcs_rel_ids, 0, 8);
265
266 case PIPE_SHADER_TESS_EVAL:
267 return LLVMGetParam(ctx->main_fn,
268 ctx->param_tes_rel_patch_id);
269
270 default:
271 assert(0);
272 return NULL;
273 }
274 }
275
276 /* Tessellation shaders pass outputs to the next shader using LDS.
277 *
278 * LS outputs = TCS inputs
279 * TCS outputs = TES inputs
280 *
281 * The LDS layout is:
282 * - TCS inputs for patch 0
283 * - TCS inputs for patch 1
284 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
285 * - ...
286 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
287 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
288 * - TCS outputs for patch 1
289 * - Per-patch TCS outputs for patch 1
290 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
291 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
292 * - ...
293 *
294 * All three shaders VS(LS), TCS, TES share the same LDS space.
295 */
296
297 static LLVMValueRef
298 get_tcs_in_patch_stride(struct si_shader_context *ctx)
299 {
300 return si_unpack_param(ctx, ctx->param_vs_state_bits, 8, 13);
301 }
302
303 static unsigned get_tcs_out_vertex_dw_stride_constant(struct si_shader_context *ctx)
304 {
305 assert(ctx->type == PIPE_SHADER_TESS_CTRL);
306
307 if (ctx->shader->key.mono.u.ff_tcs_inputs_to_copy)
308 return util_last_bit64(ctx->shader->key.mono.u.ff_tcs_inputs_to_copy) * 4;
309
310 return util_last_bit64(ctx->shader->selector->outputs_written) * 4;
311 }
312
313 static LLVMValueRef get_tcs_out_vertex_dw_stride(struct si_shader_context *ctx)
314 {
315 unsigned stride = get_tcs_out_vertex_dw_stride_constant(ctx);
316
317 return LLVMConstInt(ctx->i32, stride, 0);
318 }
319
320 static LLVMValueRef get_tcs_out_patch_stride(struct si_shader_context *ctx)
321 {
322 if (ctx->shader->key.mono.u.ff_tcs_inputs_to_copy)
323 return si_unpack_param(ctx, ctx->param_tcs_out_lds_layout, 0, 13);
324
325 const struct tgsi_shader_info *info = &ctx->shader->selector->info;
326 unsigned tcs_out_vertices = info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
327 unsigned vertex_dw_stride = get_tcs_out_vertex_dw_stride_constant(ctx);
328 unsigned num_patch_outputs = util_last_bit64(ctx->shader->selector->patch_outputs_written);
329 unsigned patch_dw_stride = tcs_out_vertices * vertex_dw_stride +
330 num_patch_outputs * 4;
331 return LLVMConstInt(ctx->i32, patch_dw_stride, 0);
332 }
333
334 static LLVMValueRef
335 get_tcs_out_patch0_offset(struct si_shader_context *ctx)
336 {
337 return LLVMBuildMul(ctx->ac.builder,
338 si_unpack_param(ctx,
339 ctx->param_tcs_out_lds_offsets,
340 0, 16),
341 LLVMConstInt(ctx->i32, 4, 0), "");
342 }
343
344 static LLVMValueRef
345 get_tcs_out_patch0_patch_data_offset(struct si_shader_context *ctx)
346 {
347 return LLVMBuildMul(ctx->ac.builder,
348 si_unpack_param(ctx,
349 ctx->param_tcs_out_lds_offsets,
350 16, 16),
351 LLVMConstInt(ctx->i32, 4, 0), "");
352 }
353
354 static LLVMValueRef
355 get_tcs_in_current_patch_offset(struct si_shader_context *ctx)
356 {
357 LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
358 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
359
360 return LLVMBuildMul(ctx->ac.builder, patch_stride, rel_patch_id, "");
361 }
362
363 static LLVMValueRef
364 get_tcs_out_current_patch_offset(struct si_shader_context *ctx)
365 {
366 LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
367 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
368 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
369
370 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id, patch0_offset);
371 }
372
373 static LLVMValueRef
374 get_tcs_out_current_patch_data_offset(struct si_shader_context *ctx)
375 {
376 LLVMValueRef patch0_patch_data_offset =
377 get_tcs_out_patch0_patch_data_offset(ctx);
378 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
379 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
380
381 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id, patch0_patch_data_offset);
382 }
383
384 static LLVMValueRef get_num_tcs_out_vertices(struct si_shader_context *ctx)
385 {
386 unsigned tcs_out_vertices =
387 ctx->shader->selector ?
388 ctx->shader->selector->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT] : 0;
389
390 /* If !tcs_out_vertices, it's either the fixed-func TCS or the TCS epilog. */
391 if (ctx->type == PIPE_SHADER_TESS_CTRL && tcs_out_vertices)
392 return LLVMConstInt(ctx->i32, tcs_out_vertices, 0);
393
394 return si_unpack_param(ctx, ctx->param_tcs_offchip_layout, 6, 6);
395 }
396
397 static LLVMValueRef get_tcs_in_vertex_dw_stride(struct si_shader_context *ctx)
398 {
399 unsigned stride;
400
401 switch (ctx->type) {
402 case PIPE_SHADER_VERTEX:
403 stride = ctx->shader->selector->lshs_vertex_stride / 4;
404 return LLVMConstInt(ctx->i32, stride, 0);
405
406 case PIPE_SHADER_TESS_CTRL:
407 if (ctx->screen->info.chip_class >= GFX9 &&
408 ctx->shader->is_monolithic) {
409 stride = ctx->shader->key.part.tcs.ls->lshs_vertex_stride / 4;
410 return LLVMConstInt(ctx->i32, stride, 0);
411 }
412 return si_unpack_param(ctx, ctx->param_vs_state_bits, 24, 8);
413
414 default:
415 assert(0);
416 return NULL;
417 }
418 }
419
420 static LLVMValueRef unpack_sint16(struct si_shader_context *ctx,
421 LLVMValueRef i32, unsigned index)
422 {
423 assert(index <= 1);
424
425 if (index == 1)
426 return LLVMBuildAShr(ctx->ac.builder, i32,
427 LLVMConstInt(ctx->i32, 16, 0), "");
428
429 return LLVMBuildSExt(ctx->ac.builder,
430 LLVMBuildTrunc(ctx->ac.builder, i32,
431 ctx->ac.i16, ""),
432 ctx->i32, "");
433 }
434
435 void si_llvm_load_input_vs(
436 struct si_shader_context *ctx,
437 unsigned input_index,
438 LLVMValueRef out[4])
439 {
440 const struct tgsi_shader_info *info = &ctx->shader->selector->info;
441 unsigned vs_blit_property = info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS];
442
443 if (vs_blit_property) {
444 LLVMValueRef vertex_id = ctx->abi.vertex_id;
445 LLVMValueRef sel_x1 = LLVMBuildICmp(ctx->ac.builder,
446 LLVMIntULE, vertex_id,
447 ctx->i32_1, "");
448 /* Use LLVMIntNE, because we have 3 vertices and only
449 * the middle one should use y2.
450 */
451 LLVMValueRef sel_y1 = LLVMBuildICmp(ctx->ac.builder,
452 LLVMIntNE, vertex_id,
453 ctx->i32_1, "");
454
455 if (input_index == 0) {
456 /* Position: */
457 LLVMValueRef x1y1 = LLVMGetParam(ctx->main_fn,
458 ctx->param_vs_blit_inputs);
459 LLVMValueRef x2y2 = LLVMGetParam(ctx->main_fn,
460 ctx->param_vs_blit_inputs + 1);
461
462 LLVMValueRef x1 = unpack_sint16(ctx, x1y1, 0);
463 LLVMValueRef y1 = unpack_sint16(ctx, x1y1, 1);
464 LLVMValueRef x2 = unpack_sint16(ctx, x2y2, 0);
465 LLVMValueRef y2 = unpack_sint16(ctx, x2y2, 1);
466
467 LLVMValueRef x = LLVMBuildSelect(ctx->ac.builder, sel_x1,
468 x1, x2, "");
469 LLVMValueRef y = LLVMBuildSelect(ctx->ac.builder, sel_y1,
470 y1, y2, "");
471
472 out[0] = LLVMBuildSIToFP(ctx->ac.builder, x, ctx->f32, "");
473 out[1] = LLVMBuildSIToFP(ctx->ac.builder, y, ctx->f32, "");
474 out[2] = LLVMGetParam(ctx->main_fn,
475 ctx->param_vs_blit_inputs + 2);
476 out[3] = ctx->ac.f32_1;
477 return;
478 }
479
480 /* Color or texture coordinates: */
481 assert(input_index == 1);
482
483 if (vs_blit_property == SI_VS_BLIT_SGPRS_POS_COLOR) {
484 for (int i = 0; i < 4; i++) {
485 out[i] = LLVMGetParam(ctx->main_fn,
486 ctx->param_vs_blit_inputs + 3 + i);
487 }
488 } else {
489 assert(vs_blit_property == SI_VS_BLIT_SGPRS_POS_TEXCOORD);
490 LLVMValueRef x1 = LLVMGetParam(ctx->main_fn,
491 ctx->param_vs_blit_inputs + 3);
492 LLVMValueRef y1 = LLVMGetParam(ctx->main_fn,
493 ctx->param_vs_blit_inputs + 4);
494 LLVMValueRef x2 = LLVMGetParam(ctx->main_fn,
495 ctx->param_vs_blit_inputs + 5);
496 LLVMValueRef y2 = LLVMGetParam(ctx->main_fn,
497 ctx->param_vs_blit_inputs + 6);
498
499 out[0] = LLVMBuildSelect(ctx->ac.builder, sel_x1,
500 x1, x2, "");
501 out[1] = LLVMBuildSelect(ctx->ac.builder, sel_y1,
502 y1, y2, "");
503 out[2] = LLVMGetParam(ctx->main_fn,
504 ctx->param_vs_blit_inputs + 7);
505 out[3] = LLVMGetParam(ctx->main_fn,
506 ctx->param_vs_blit_inputs + 8);
507 }
508 return;
509 }
510
511 union si_vs_fix_fetch fix_fetch;
512 LLVMValueRef t_list_ptr;
513 LLVMValueRef t_offset;
514 LLVMValueRef t_list;
515 LLVMValueRef vertex_index;
516 LLVMValueRef tmp;
517
518 /* Load the T list */
519 t_list_ptr = LLVMGetParam(ctx->main_fn, ctx->param_vertex_buffers);
520
521 t_offset = LLVMConstInt(ctx->i32, input_index, 0);
522
523 t_list = ac_build_load_to_sgpr(&ctx->ac, t_list_ptr, t_offset);
524
525 vertex_index = LLVMGetParam(ctx->main_fn,
526 ctx->param_vertex_index0 +
527 input_index);
528
529 /* Use the open-coded implementation for all loads of doubles and
530 * of dword-sized data that needs fixups. We need to insert conversion
531 * code anyway, and the amd/common code does it for us.
532 *
533 * Note: On LLVM <= 8, we can only open-code formats with
534 * channel size >= 4 bytes.
535 */
536 bool opencode = ctx->shader->key.mono.vs_fetch_opencode & (1 << input_index);
537 fix_fetch.bits = ctx->shader->key.mono.vs_fix_fetch[input_index].bits;
538 if (opencode ||
539 (fix_fetch.u.log_size == 3 && fix_fetch.u.format == AC_FETCH_FORMAT_FLOAT) ||
540 (fix_fetch.u.log_size == 2)) {
541 tmp = ac_build_opencoded_load_format(
542 &ctx->ac, fix_fetch.u.log_size, fix_fetch.u.num_channels_m1 + 1,
543 fix_fetch.u.format, fix_fetch.u.reverse, !opencode,
544 t_list, vertex_index, ctx->ac.i32_0, ctx->ac.i32_0,
545 false, false, true);
546 for (unsigned i = 0; i < 4; ++i)
547 out[i] = LLVMBuildExtractElement(ctx->ac.builder, tmp, LLVMConstInt(ctx->i32, i, false), "");
548 return;
549 }
550
551 /* Do multiple loads for special formats. */
552 unsigned required_channels = util_last_bit(info->input_usage_mask[input_index]);
553 LLVMValueRef fetches[4];
554 unsigned num_fetches;
555 unsigned fetch_stride;
556 unsigned channels_per_fetch;
557
558 if (fix_fetch.u.log_size <= 1 && fix_fetch.u.num_channels_m1 == 2) {
559 num_fetches = MIN2(required_channels, 3);
560 fetch_stride = 1 << fix_fetch.u.log_size;
561 channels_per_fetch = 1;
562 } else {
563 num_fetches = 1;
564 fetch_stride = 0;
565 channels_per_fetch = required_channels;
566 }
567
568 for (unsigned i = 0; i < num_fetches; ++i) {
569 LLVMValueRef voffset = LLVMConstInt(ctx->i32, fetch_stride * i, 0);
570 fetches[i] = ac_build_buffer_load_format(&ctx->ac, t_list, vertex_index, voffset,
571 channels_per_fetch, false, true);
572 }
573
574 if (num_fetches == 1 && channels_per_fetch > 1) {
575 LLVMValueRef fetch = fetches[0];
576 for (unsigned i = 0; i < channels_per_fetch; ++i) {
577 tmp = LLVMConstInt(ctx->i32, i, false);
578 fetches[i] = LLVMBuildExtractElement(
579 ctx->ac.builder, fetch, tmp, "");
580 }
581 num_fetches = channels_per_fetch;
582 channels_per_fetch = 1;
583 }
584
585 for (unsigned i = num_fetches; i < 4; ++i)
586 fetches[i] = LLVMGetUndef(ctx->f32);
587
588 if (fix_fetch.u.log_size <= 1 && fix_fetch.u.num_channels_m1 == 2 &&
589 required_channels == 4) {
590 if (fix_fetch.u.format == AC_FETCH_FORMAT_UINT || fix_fetch.u.format == AC_FETCH_FORMAT_SINT)
591 fetches[3] = ctx->ac.i32_1;
592 else
593 fetches[3] = ctx->ac.f32_1;
594 } else if (fix_fetch.u.log_size == 3 &&
595 (fix_fetch.u.format == AC_FETCH_FORMAT_SNORM ||
596 fix_fetch.u.format == AC_FETCH_FORMAT_SSCALED ||
597 fix_fetch.u.format == AC_FETCH_FORMAT_SINT) &&
598 required_channels == 4) {
599 /* For 2_10_10_10, the hardware returns an unsigned value;
600 * convert it to a signed one.
601 */
602 LLVMValueRef tmp = fetches[3];
603 LLVMValueRef c30 = LLVMConstInt(ctx->i32, 30, 0);
604
605 /* First, recover the sign-extended signed integer value. */
606 if (fix_fetch.u.format == AC_FETCH_FORMAT_SSCALED)
607 tmp = LLVMBuildFPToUI(ctx->ac.builder, tmp, ctx->i32, "");
608 else
609 tmp = ac_to_integer(&ctx->ac, tmp);
610
611 /* For the integer-like cases, do a natural sign extension.
612 *
613 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
614 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
615 * exponent.
616 */
617 tmp = LLVMBuildShl(ctx->ac.builder, tmp,
618 fix_fetch.u.format == AC_FETCH_FORMAT_SNORM ?
619 LLVMConstInt(ctx->i32, 7, 0) : c30, "");
620 tmp = LLVMBuildAShr(ctx->ac.builder, tmp, c30, "");
621
622 /* Convert back to the right type. */
623 if (fix_fetch.u.format == AC_FETCH_FORMAT_SNORM) {
624 LLVMValueRef clamp;
625 LLVMValueRef neg_one = LLVMConstReal(ctx->f32, -1.0);
626 tmp = LLVMBuildSIToFP(ctx->ac.builder, tmp, ctx->f32, "");
627 clamp = LLVMBuildFCmp(ctx->ac.builder, LLVMRealULT, tmp, neg_one, "");
628 tmp = LLVMBuildSelect(ctx->ac.builder, clamp, neg_one, tmp, "");
629 } else if (fix_fetch.u.format == AC_FETCH_FORMAT_SSCALED) {
630 tmp = LLVMBuildSIToFP(ctx->ac.builder, tmp, ctx->f32, "");
631 }
632
633 fetches[3] = tmp;
634 }
635
636 for (unsigned i = 0; i < 4; ++i)
637 out[i] = ac_to_float(&ctx->ac, fetches[i]);
638 }
639
640 static void declare_input_vs(
641 struct si_shader_context *ctx,
642 unsigned input_index,
643 const struct tgsi_full_declaration *decl,
644 LLVMValueRef out[4])
645 {
646 si_llvm_load_input_vs(ctx, input_index, out);
647 }
648
649 LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx,
650 unsigned swizzle)
651 {
652 if (swizzle > 0)
653 return ctx->i32_0;
654
655 switch (ctx->type) {
656 case PIPE_SHADER_VERTEX:
657 return LLVMGetParam(ctx->main_fn,
658 ctx->param_vs_prim_id);
659 case PIPE_SHADER_TESS_CTRL:
660 return ctx->abi.tcs_patch_id;
661 case PIPE_SHADER_TESS_EVAL:
662 return ctx->abi.tes_patch_id;
663 case PIPE_SHADER_GEOMETRY:
664 return ctx->abi.gs_prim_id;
665 default:
666 assert(0);
667 return ctx->i32_0;
668 }
669 }
670
671 /**
672 * Return the value of tgsi_ind_register for indexing.
673 * This is the indirect index with the constant offset added to it.
674 */
675 LLVMValueRef si_get_indirect_index(struct si_shader_context *ctx,
676 const struct tgsi_ind_register *ind,
677 unsigned addr_mul,
678 int rel_index)
679 {
680 LLVMValueRef result;
681
682 if (ind->File == TGSI_FILE_ADDRESS) {
683 result = ctx->addrs[ind->Index][ind->Swizzle];
684 result = LLVMBuildLoad(ctx->ac.builder, result, "");
685 } else {
686 struct tgsi_full_src_register src = {};
687
688 src.Register.File = ind->File;
689 src.Register.Index = ind->Index;
690
691 /* Set the second index to 0 for constants. */
692 if (ind->File == TGSI_FILE_CONSTANT)
693 src.Register.Dimension = 1;
694
695 result = ctx->bld_base.emit_fetch_funcs[ind->File](&ctx->bld_base, &src,
696 TGSI_TYPE_SIGNED,
697 ind->Swizzle);
698 result = ac_to_integer(&ctx->ac, result);
699 }
700
701 return ac_build_imad(&ctx->ac, result, LLVMConstInt(ctx->i32, addr_mul, 0),
702 LLVMConstInt(ctx->i32, rel_index, 0));
703 }
704
705 /**
706 * Like si_get_indirect_index, but restricts the return value to a (possibly
707 * undefined) value inside [0..num).
708 */
709 LLVMValueRef si_get_bounded_indirect_index(struct si_shader_context *ctx,
710 const struct tgsi_ind_register *ind,
711 int rel_index, unsigned num)
712 {
713 LLVMValueRef result = si_get_indirect_index(ctx, ind, 1, rel_index);
714
715 return si_llvm_bound_index(ctx, result, num);
716 }
717
718 static LLVMValueRef get_dw_address_from_generic_indices(struct si_shader_context *ctx,
719 LLVMValueRef vertex_dw_stride,
720 LLVMValueRef base_addr,
721 LLVMValueRef vertex_index,
722 LLVMValueRef param_index,
723 unsigned input_index,
724 ubyte *name,
725 ubyte *index,
726 bool is_patch)
727 {
728 if (vertex_dw_stride) {
729 base_addr = ac_build_imad(&ctx->ac, vertex_index,
730 vertex_dw_stride, base_addr);
731 }
732
733 if (param_index) {
734 base_addr = ac_build_imad(&ctx->ac, param_index,
735 LLVMConstInt(ctx->i32, 4, 0), base_addr);
736 }
737
738 int param = is_patch ?
739 si_shader_io_get_unique_index_patch(name[input_index],
740 index[input_index]) :
741 si_shader_io_get_unique_index(name[input_index],
742 index[input_index], false);
743
744 /* Add the base address of the element. */
745 return LLVMBuildAdd(ctx->ac.builder, base_addr,
746 LLVMConstInt(ctx->i32, param * 4, 0), "");
747 }
748
749 /**
750 * Calculate a dword address given an input or output register and a stride.
751 */
752 static LLVMValueRef get_dw_address(struct si_shader_context *ctx,
753 const struct tgsi_full_dst_register *dst,
754 const struct tgsi_full_src_register *src,
755 LLVMValueRef vertex_dw_stride,
756 LLVMValueRef base_addr)
757 {
758 struct tgsi_shader_info *info = &ctx->shader->selector->info;
759 ubyte *name, *index, *array_first;
760 int input_index;
761 struct tgsi_full_dst_register reg;
762 LLVMValueRef vertex_index = NULL;
763 LLVMValueRef ind_index = NULL;
764
765 /* Set the register description. The address computation is the same
766 * for sources and destinations. */
767 if (src) {
768 reg.Register.File = src->Register.File;
769 reg.Register.Index = src->Register.Index;
770 reg.Register.Indirect = src->Register.Indirect;
771 reg.Register.Dimension = src->Register.Dimension;
772 reg.Indirect = src->Indirect;
773 reg.Dimension = src->Dimension;
774 reg.DimIndirect = src->DimIndirect;
775 } else
776 reg = *dst;
777
778 /* If the register is 2-dimensional (e.g. an array of vertices
779 * in a primitive), calculate the base address of the vertex. */
780 if (reg.Register.Dimension) {
781 if (reg.Dimension.Indirect)
782 vertex_index = si_get_indirect_index(ctx, &reg.DimIndirect,
783 1, reg.Dimension.Index);
784 else
785 vertex_index = LLVMConstInt(ctx->i32, reg.Dimension.Index, 0);
786 }
787
788 /* Get information about the register. */
789 if (reg.Register.File == TGSI_FILE_INPUT) {
790 name = info->input_semantic_name;
791 index = info->input_semantic_index;
792 array_first = info->input_array_first;
793 } else if (reg.Register.File == TGSI_FILE_OUTPUT) {
794 name = info->output_semantic_name;
795 index = info->output_semantic_index;
796 array_first = info->output_array_first;
797 } else {
798 assert(0);
799 return NULL;
800 }
801
802 if (reg.Register.Indirect) {
803 /* Add the relative address of the element. */
804 if (reg.Indirect.ArrayID)
805 input_index = array_first[reg.Indirect.ArrayID];
806 else
807 input_index = reg.Register.Index;
808
809 ind_index = si_get_indirect_index(ctx, &reg.Indirect,
810 1, reg.Register.Index - input_index);
811 } else {
812 input_index = reg.Register.Index;
813 }
814
815 return get_dw_address_from_generic_indices(ctx, vertex_dw_stride,
816 base_addr, vertex_index,
817 ind_index, input_index,
818 name, index,
819 !reg.Register.Dimension);
820 }
821
822 /* The offchip buffer layout for TCS->TES is
823 *
824 * - attribute 0 of patch 0 vertex 0
825 * - attribute 0 of patch 0 vertex 1
826 * - attribute 0 of patch 0 vertex 2
827 * ...
828 * - attribute 0 of patch 1 vertex 0
829 * - attribute 0 of patch 1 vertex 1
830 * ...
831 * - attribute 1 of patch 0 vertex 0
832 * - attribute 1 of patch 0 vertex 1
833 * ...
834 * - per patch attribute 0 of patch 0
835 * - per patch attribute 0 of patch 1
836 * ...
837 *
838 * Note that every attribute has 4 components.
839 */
840 static LLVMValueRef get_tcs_tes_buffer_address(struct si_shader_context *ctx,
841 LLVMValueRef rel_patch_id,
842 LLVMValueRef vertex_index,
843 LLVMValueRef param_index)
844 {
845 LLVMValueRef base_addr, vertices_per_patch, num_patches, total_vertices;
846 LLVMValueRef param_stride, constant16;
847
848 vertices_per_patch = get_num_tcs_out_vertices(ctx);
849 num_patches = si_unpack_param(ctx, ctx->param_tcs_offchip_layout, 0, 6);
850 total_vertices = LLVMBuildMul(ctx->ac.builder, vertices_per_patch,
851 num_patches, "");
852
853 constant16 = LLVMConstInt(ctx->i32, 16, 0);
854 if (vertex_index) {
855 base_addr = ac_build_imad(&ctx->ac, rel_patch_id,
856 vertices_per_patch, vertex_index);
857 param_stride = total_vertices;
858 } else {
859 base_addr = rel_patch_id;
860 param_stride = num_patches;
861 }
862
863 base_addr = ac_build_imad(&ctx->ac, param_index, param_stride, base_addr);
864 base_addr = LLVMBuildMul(ctx->ac.builder, base_addr, constant16, "");
865
866 if (!vertex_index) {
867 LLVMValueRef patch_data_offset =
868 si_unpack_param(ctx, ctx->param_tcs_offchip_layout, 12, 20);
869
870 base_addr = LLVMBuildAdd(ctx->ac.builder, base_addr,
871 patch_data_offset, "");
872 }
873 return base_addr;
874 }
875
876 /* This is a generic helper that can be shared by the NIR and TGSI backends */
877 static LLVMValueRef get_tcs_tes_buffer_address_from_generic_indices(
878 struct si_shader_context *ctx,
879 LLVMValueRef vertex_index,
880 LLVMValueRef param_index,
881 unsigned param_base,
882 ubyte *name,
883 ubyte *index,
884 bool is_patch)
885 {
886 unsigned param_index_base;
887
888 param_index_base = is_patch ?
889 si_shader_io_get_unique_index_patch(name[param_base], index[param_base]) :
890 si_shader_io_get_unique_index(name[param_base], index[param_base], false);
891
892 if (param_index) {
893 param_index = LLVMBuildAdd(ctx->ac.builder, param_index,
894 LLVMConstInt(ctx->i32, param_index_base, 0),
895 "");
896 } else {
897 param_index = LLVMConstInt(ctx->i32, param_index_base, 0);
898 }
899
900 return get_tcs_tes_buffer_address(ctx, get_rel_patch_id(ctx),
901 vertex_index, param_index);
902 }
903
904 static LLVMValueRef get_tcs_tes_buffer_address_from_reg(
905 struct si_shader_context *ctx,
906 const struct tgsi_full_dst_register *dst,
907 const struct tgsi_full_src_register *src)
908 {
909 struct tgsi_shader_info *info = &ctx->shader->selector->info;
910 ubyte *name, *index, *array_first;
911 struct tgsi_full_src_register reg;
912 LLVMValueRef vertex_index = NULL;
913 LLVMValueRef param_index = NULL;
914 unsigned param_base;
915
916 reg = src ? *src : tgsi_full_src_register_from_dst(dst);
917
918 if (reg.Register.Dimension) {
919
920 if (reg.Dimension.Indirect)
921 vertex_index = si_get_indirect_index(ctx, &reg.DimIndirect,
922 1, reg.Dimension.Index);
923 else
924 vertex_index = LLVMConstInt(ctx->i32, reg.Dimension.Index, 0);
925 }
926
927 /* Get information about the register. */
928 if (reg.Register.File == TGSI_FILE_INPUT) {
929 name = info->input_semantic_name;
930 index = info->input_semantic_index;
931 array_first = info->input_array_first;
932 } else if (reg.Register.File == TGSI_FILE_OUTPUT) {
933 name = info->output_semantic_name;
934 index = info->output_semantic_index;
935 array_first = info->output_array_first;
936 } else {
937 assert(0);
938 return NULL;
939 }
940
941 if (reg.Register.Indirect) {
942 if (reg.Indirect.ArrayID)
943 param_base = array_first[reg.Indirect.ArrayID];
944 else
945 param_base = reg.Register.Index;
946
947 param_index = si_get_indirect_index(ctx, &reg.Indirect,
948 1, reg.Register.Index - param_base);
949
950 } else {
951 param_base = reg.Register.Index;
952 }
953
954 return get_tcs_tes_buffer_address_from_generic_indices(ctx, vertex_index,
955 param_index, param_base,
956 name, index, !reg.Register.Dimension);
957 }
958
959 static LLVMValueRef buffer_load(struct lp_build_tgsi_context *bld_base,
960 LLVMTypeRef type, unsigned swizzle,
961 LLVMValueRef buffer, LLVMValueRef offset,
962 LLVMValueRef base, bool can_speculate)
963 {
964 struct si_shader_context *ctx = si_shader_context(bld_base);
965 LLVMValueRef value, value2;
966 LLVMTypeRef vec_type = LLVMVectorType(type, 4);
967
968 if (swizzle == ~0) {
969 value = ac_build_buffer_load(&ctx->ac, buffer, 4, NULL, base, offset,
970 0, 1, 0, can_speculate, false);
971
972 return LLVMBuildBitCast(ctx->ac.builder, value, vec_type, "");
973 }
974
975 if (!llvm_type_is_64bit(ctx, type)) {
976 value = ac_build_buffer_load(&ctx->ac, buffer, 4, NULL, base, offset,
977 0, 1, 0, can_speculate, false);
978
979 value = LLVMBuildBitCast(ctx->ac.builder, value, vec_type, "");
980 return LLVMBuildExtractElement(ctx->ac.builder, value,
981 LLVMConstInt(ctx->i32, swizzle, 0), "");
982 }
983
984 value = ac_build_buffer_load(&ctx->ac, buffer, 1, NULL, base, offset,
985 swizzle * 4, 1, 0, can_speculate, false);
986
987 value2 = ac_build_buffer_load(&ctx->ac, buffer, 1, NULL, base, offset,
988 swizzle * 4 + 4, 1, 0, can_speculate, false);
989
990 return si_llvm_emit_fetch_64bit(bld_base, type, value, value2);
991 }
992
993 /**
994 * Load from LSHS LDS storage.
995 *
996 * \param type output value type
997 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
998 * \param dw_addr address in dwords
999 */
1000 static LLVMValueRef lshs_lds_load(struct lp_build_tgsi_context *bld_base,
1001 LLVMTypeRef type, unsigned swizzle,
1002 LLVMValueRef dw_addr)
1003 {
1004 struct si_shader_context *ctx = si_shader_context(bld_base);
1005 LLVMValueRef value;
1006
1007 if (swizzle == ~0) {
1008 LLVMValueRef values[TGSI_NUM_CHANNELS];
1009
1010 for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; chan++)
1011 values[chan] = lshs_lds_load(bld_base, type, chan, dw_addr);
1012
1013 return ac_build_gather_values(&ctx->ac, values,
1014 TGSI_NUM_CHANNELS);
1015 }
1016
1017 /* Split 64-bit loads. */
1018 if (llvm_type_is_64bit(ctx, type)) {
1019 LLVMValueRef lo, hi;
1020
1021 lo = lshs_lds_load(bld_base, ctx->i32, swizzle, dw_addr);
1022 hi = lshs_lds_load(bld_base, ctx->i32, swizzle + 1, dw_addr);
1023 return si_llvm_emit_fetch_64bit(bld_base, type, lo, hi);
1024 }
1025
1026 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1027 LLVMConstInt(ctx->i32, swizzle, 0), "");
1028
1029 value = ac_lds_load(&ctx->ac, dw_addr);
1030
1031 return LLVMBuildBitCast(ctx->ac.builder, value, type, "");
1032 }
1033
1034 /**
1035 * Store to LSHS LDS storage.
1036 *
1037 * \param swizzle offset (typically 0..3)
1038 * \param dw_addr address in dwords
1039 * \param value value to store
1040 */
1041 static void lshs_lds_store(struct si_shader_context *ctx,
1042 unsigned dw_offset_imm, LLVMValueRef dw_addr,
1043 LLVMValueRef value)
1044 {
1045 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1046 LLVMConstInt(ctx->i32, dw_offset_imm, 0), "");
1047
1048 ac_lds_store(&ctx->ac, dw_addr, value);
1049 }
1050
1051 enum si_tess_ring {
1052 TCS_FACTOR_RING,
1053 TESS_OFFCHIP_RING_TCS,
1054 TESS_OFFCHIP_RING_TES,
1055 };
1056
1057 static LLVMValueRef get_tess_ring_descriptor(struct si_shader_context *ctx,
1058 enum si_tess_ring ring)
1059 {
1060 LLVMBuilderRef builder = ctx->ac.builder;
1061 unsigned param = ring == TESS_OFFCHIP_RING_TES ? ctx->param_tes_offchip_addr :
1062 ctx->param_tcs_out_lds_layout;
1063 LLVMValueRef addr = LLVMGetParam(ctx->main_fn, param);
1064
1065 /* TCS only receives high 13 bits of the address. */
1066 if (ring == TESS_OFFCHIP_RING_TCS || ring == TCS_FACTOR_RING) {
1067 addr = LLVMBuildAnd(builder, addr,
1068 LLVMConstInt(ctx->i32, 0xfff80000, 0), "");
1069 }
1070
1071 if (ring == TCS_FACTOR_RING) {
1072 unsigned tf_offset = ctx->screen->tess_offchip_ring_size;
1073 addr = LLVMBuildAdd(builder, addr,
1074 LLVMConstInt(ctx->i32, tf_offset, 0), "");
1075 }
1076
1077 LLVMValueRef desc[4];
1078 desc[0] = addr;
1079 desc[1] = LLVMConstInt(ctx->i32,
1080 S_008F04_BASE_ADDRESS_HI(ctx->screen->info.address32_hi), 0);
1081 desc[2] = LLVMConstInt(ctx->i32, 0xffffffff, 0);
1082 desc[3] = LLVMConstInt(ctx->i32,
1083 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1084 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1085 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1086 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1087 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1088 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32), 0);
1089
1090 return ac_build_gather_values(&ctx->ac, desc, 4);
1091 }
1092
1093 static LLVMValueRef fetch_input_tcs(
1094 struct lp_build_tgsi_context *bld_base,
1095 const struct tgsi_full_src_register *reg,
1096 enum tgsi_opcode_type type, unsigned swizzle_in)
1097 {
1098 struct si_shader_context *ctx = si_shader_context(bld_base);
1099 LLVMValueRef dw_addr, stride;
1100 unsigned swizzle = swizzle_in & 0xffff;
1101 stride = get_tcs_in_vertex_dw_stride(ctx);
1102 dw_addr = get_tcs_in_current_patch_offset(ctx);
1103 dw_addr = get_dw_address(ctx, NULL, reg, stride, dw_addr);
1104
1105 return lshs_lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
1106 }
1107
1108 static LLVMValueRef si_nir_load_tcs_varyings(struct ac_shader_abi *abi,
1109 LLVMTypeRef type,
1110 LLVMValueRef vertex_index,
1111 LLVMValueRef param_index,
1112 unsigned const_index,
1113 unsigned location,
1114 unsigned driver_location,
1115 unsigned component,
1116 unsigned num_components,
1117 bool is_patch,
1118 bool is_compact,
1119 bool load_input)
1120 {
1121 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1122 struct tgsi_shader_info *info = &ctx->shader->selector->info;
1123 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
1124 LLVMValueRef dw_addr, stride;
1125
1126 driver_location = driver_location / 4;
1127
1128 if (load_input) {
1129 stride = get_tcs_in_vertex_dw_stride(ctx);
1130 dw_addr = get_tcs_in_current_patch_offset(ctx);
1131 } else {
1132 if (is_patch) {
1133 stride = NULL;
1134 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1135 } else {
1136 stride = get_tcs_out_vertex_dw_stride(ctx);
1137 dw_addr = get_tcs_out_current_patch_offset(ctx);
1138 }
1139 }
1140
1141 if (param_index) {
1142 /* Add the constant index to the indirect index */
1143 param_index = LLVMBuildAdd(ctx->ac.builder, param_index,
1144 LLVMConstInt(ctx->i32, const_index, 0), "");
1145 } else {
1146 param_index = LLVMConstInt(ctx->i32, const_index, 0);
1147 }
1148
1149 ubyte *names;
1150 ubyte *indices;
1151 if (load_input) {
1152 names = info->input_semantic_name;
1153 indices = info->input_semantic_index;
1154 } else {
1155 names = info->output_semantic_name;
1156 indices = info->output_semantic_index;
1157 }
1158
1159 dw_addr = get_dw_address_from_generic_indices(ctx, stride, dw_addr,
1160 vertex_index, param_index,
1161 driver_location,
1162 names, indices,
1163 is_patch);
1164
1165 LLVMValueRef value[4];
1166 for (unsigned i = 0; i < num_components; i++) {
1167 unsigned offset = i;
1168 if (llvm_type_is_64bit(ctx, type))
1169 offset *= 2;
1170
1171 offset += component;
1172 value[i + component] = lshs_lds_load(bld_base, type, offset, dw_addr);
1173 }
1174
1175 return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
1176 }
1177
1178 static LLVMValueRef fetch_output_tcs(
1179 struct lp_build_tgsi_context *bld_base,
1180 const struct tgsi_full_src_register *reg,
1181 enum tgsi_opcode_type type, unsigned swizzle_in)
1182 {
1183 struct si_shader_context *ctx = si_shader_context(bld_base);
1184 LLVMValueRef dw_addr, stride;
1185 unsigned swizzle = (swizzle_in & 0xffff);
1186
1187 if (reg->Register.Dimension) {
1188 stride = get_tcs_out_vertex_dw_stride(ctx);
1189 dw_addr = get_tcs_out_current_patch_offset(ctx);
1190 dw_addr = get_dw_address(ctx, NULL, reg, stride, dw_addr);
1191 } else {
1192 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1193 dw_addr = get_dw_address(ctx, NULL, reg, NULL, dw_addr);
1194 }
1195
1196 return lshs_lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
1197 }
1198
1199 static LLVMValueRef fetch_input_tes(
1200 struct lp_build_tgsi_context *bld_base,
1201 const struct tgsi_full_src_register *reg,
1202 enum tgsi_opcode_type type, unsigned swizzle_in)
1203 {
1204 struct si_shader_context *ctx = si_shader_context(bld_base);
1205 LLVMValueRef base, addr;
1206 unsigned swizzle = (swizzle_in & 0xffff);
1207
1208 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
1209 addr = get_tcs_tes_buffer_address_from_reg(ctx, NULL, reg);
1210
1211 return buffer_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle,
1212 ctx->tess_offchip_ring, base, addr, true);
1213 }
1214
1215 LLVMValueRef si_nir_load_input_tes(struct ac_shader_abi *abi,
1216 LLVMTypeRef type,
1217 LLVMValueRef vertex_index,
1218 LLVMValueRef param_index,
1219 unsigned const_index,
1220 unsigned location,
1221 unsigned driver_location,
1222 unsigned component,
1223 unsigned num_components,
1224 bool is_patch,
1225 bool is_compact,
1226 bool load_input)
1227 {
1228 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1229 struct tgsi_shader_info *info = &ctx->shader->selector->info;
1230 LLVMValueRef base, addr;
1231
1232 driver_location = driver_location / 4;
1233
1234 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
1235
1236 if (param_index) {
1237 /* Add the constant index to the indirect index */
1238 param_index = LLVMBuildAdd(ctx->ac.builder, param_index,
1239 LLVMConstInt(ctx->i32, const_index, 0), "");
1240 } else {
1241 param_index = LLVMConstInt(ctx->i32, const_index, 0);
1242 }
1243
1244 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx, vertex_index,
1245 param_index, driver_location,
1246 info->input_semantic_name,
1247 info->input_semantic_index,
1248 is_patch);
1249
1250 /* TODO: This will generate rather ordinary llvm code, although it
1251 * should be easy for the optimiser to fix up. In future we might want
1252 * to refactor buffer_load(), but for now this maximises code sharing
1253 * between the NIR and TGSI backends.
1254 */
1255 LLVMValueRef value[4];
1256 for (unsigned i = 0; i < num_components; i++) {
1257 unsigned offset = i;
1258 if (llvm_type_is_64bit(ctx, type)) {
1259 offset *= 2;
1260 if (offset == 4) {
1261 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx,
1262 vertex_index,
1263 param_index,
1264 driver_location + 1,
1265 info->input_semantic_name,
1266 info->input_semantic_index,
1267 is_patch);
1268 }
1269
1270 offset = offset % 4;
1271 }
1272
1273 offset += component;
1274 value[i + component] = buffer_load(&ctx->bld_base, type, offset,
1275 ctx->tess_offchip_ring, base, addr, true);
1276 }
1277
1278 return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
1279 }
1280
1281 static void store_output_tcs(struct lp_build_tgsi_context *bld_base,
1282 const struct tgsi_full_instruction *inst,
1283 const struct tgsi_opcode_info *info,
1284 unsigned index,
1285 LLVMValueRef dst[4])
1286 {
1287 struct si_shader_context *ctx = si_shader_context(bld_base);
1288 const struct tgsi_full_dst_register *reg = &inst->Dst[index];
1289 const struct tgsi_shader_info *sh_info = &ctx->shader->selector->info;
1290 unsigned chan_index;
1291 LLVMValueRef dw_addr, stride;
1292 LLVMValueRef buffer, base, buf_addr;
1293 LLVMValueRef values[4];
1294 bool skip_lds_store;
1295 bool is_tess_factor = false, is_tess_inner = false;
1296
1297 /* Only handle per-patch and per-vertex outputs here.
1298 * Vectors will be lowered to scalars and this function will be called again.
1299 */
1300 if (reg->Register.File != TGSI_FILE_OUTPUT ||
1301 (dst[0] && LLVMGetTypeKind(LLVMTypeOf(dst[0])) == LLVMVectorTypeKind)) {
1302 si_llvm_emit_store(bld_base, inst, info, index, dst);
1303 return;
1304 }
1305
1306 if (reg->Register.Dimension) {
1307 stride = get_tcs_out_vertex_dw_stride(ctx);
1308 dw_addr = get_tcs_out_current_patch_offset(ctx);
1309 dw_addr = get_dw_address(ctx, reg, NULL, stride, dw_addr);
1310 skip_lds_store = !sh_info->reads_pervertex_outputs;
1311 } else {
1312 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1313 dw_addr = get_dw_address(ctx, reg, NULL, NULL, dw_addr);
1314 skip_lds_store = !sh_info->reads_perpatch_outputs;
1315
1316 if (!reg->Register.Indirect) {
1317 int name = sh_info->output_semantic_name[reg->Register.Index];
1318
1319 /* Always write tess factors into LDS for the TCS epilog. */
1320 if (name == TGSI_SEMANTIC_TESSINNER ||
1321 name == TGSI_SEMANTIC_TESSOUTER) {
1322 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1323 skip_lds_store = !sh_info->reads_tessfactor_outputs &&
1324 ctx->shader->selector->tcs_info.tessfactors_are_def_in_all_invocs;
1325 is_tess_factor = true;
1326 is_tess_inner = name == TGSI_SEMANTIC_TESSINNER;
1327 }
1328 }
1329 }
1330
1331 buffer = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
1332
1333 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
1334 buf_addr = get_tcs_tes_buffer_address_from_reg(ctx, reg, NULL);
1335
1336 uint32_t writemask = reg->Register.WriteMask;
1337 while (writemask) {
1338 chan_index = u_bit_scan(&writemask);
1339 LLVMValueRef value = dst[chan_index];
1340
1341 if (inst->Instruction.Saturate)
1342 value = ac_build_clamp(&ctx->ac, value);
1343
1344 /* Skip LDS stores if there is no LDS read of this output. */
1345 if (!skip_lds_store)
1346 lshs_lds_store(ctx, chan_index, dw_addr, value);
1347
1348 value = ac_to_integer(&ctx->ac, value);
1349 values[chan_index] = value;
1350
1351 if (reg->Register.WriteMask != 0xF && !is_tess_factor) {
1352 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 1,
1353 buf_addr, base,
1354 4 * chan_index, 1, 0, false);
1355 }
1356
1357 /* Write tess factors into VGPRs for the epilog. */
1358 if (is_tess_factor &&
1359 ctx->shader->selector->tcs_info.tessfactors_are_def_in_all_invocs) {
1360 if (!is_tess_inner) {
1361 LLVMBuildStore(ctx->ac.builder, value, /* outer */
1362 ctx->invoc0_tess_factors[chan_index]);
1363 } else if (chan_index < 2) {
1364 LLVMBuildStore(ctx->ac.builder, value, /* inner */
1365 ctx->invoc0_tess_factors[4 + chan_index]);
1366 }
1367 }
1368 }
1369
1370 if (reg->Register.WriteMask == 0xF && !is_tess_factor) {
1371 LLVMValueRef value = ac_build_gather_values(&ctx->ac,
1372 values, 4);
1373 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, buf_addr,
1374 base, 0, 1, 0, false);
1375 }
1376 }
1377
1378 static void si_nir_store_output_tcs(struct ac_shader_abi *abi,
1379 const struct nir_variable *var,
1380 LLVMValueRef vertex_index,
1381 LLVMValueRef param_index,
1382 unsigned const_index,
1383 LLVMValueRef src,
1384 unsigned writemask)
1385 {
1386 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1387 struct tgsi_shader_info *info = &ctx->shader->selector->info;
1388 const unsigned component = var->data.location_frac;
1389 const bool is_patch = var->data.patch;
1390 unsigned driver_location = var->data.driver_location;
1391 LLVMValueRef dw_addr, stride;
1392 LLVMValueRef buffer, base, addr;
1393 LLVMValueRef values[8];
1394 bool skip_lds_store;
1395 bool is_tess_factor = false, is_tess_inner = false;
1396
1397 driver_location = driver_location / 4;
1398
1399 if (param_index) {
1400 /* Add the constant index to the indirect index */
1401 param_index = LLVMBuildAdd(ctx->ac.builder, param_index,
1402 LLVMConstInt(ctx->i32, const_index, 0), "");
1403 } else {
1404 if (const_index != 0)
1405 param_index = LLVMConstInt(ctx->i32, const_index, 0);
1406 }
1407
1408 if (!is_patch) {
1409 stride = get_tcs_out_vertex_dw_stride(ctx);
1410 dw_addr = get_tcs_out_current_patch_offset(ctx);
1411 dw_addr = get_dw_address_from_generic_indices(ctx, stride, dw_addr,
1412 vertex_index, param_index,
1413 driver_location,
1414 info->output_semantic_name,
1415 info->output_semantic_index,
1416 is_patch);
1417
1418 skip_lds_store = !info->reads_pervertex_outputs;
1419 } else {
1420 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1421 dw_addr = get_dw_address_from_generic_indices(ctx, NULL, dw_addr,
1422 vertex_index, param_index,
1423 driver_location,
1424 info->output_semantic_name,
1425 info->output_semantic_index,
1426 is_patch);
1427
1428 skip_lds_store = !info->reads_perpatch_outputs;
1429
1430 if (!param_index) {
1431 int name = info->output_semantic_name[driver_location];
1432
1433 /* Always write tess factors into LDS for the TCS epilog. */
1434 if (name == TGSI_SEMANTIC_TESSINNER ||
1435 name == TGSI_SEMANTIC_TESSOUTER) {
1436 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1437 skip_lds_store = !info->reads_tessfactor_outputs &&
1438 ctx->shader->selector->tcs_info.tessfactors_are_def_in_all_invocs;
1439 is_tess_factor = true;
1440 is_tess_inner = name == TGSI_SEMANTIC_TESSINNER;
1441 }
1442 }
1443 }
1444
1445 buffer = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
1446
1447 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
1448
1449 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx, vertex_index,
1450 param_index, driver_location,
1451 info->output_semantic_name,
1452 info->output_semantic_index,
1453 is_patch);
1454
1455 for (unsigned chan = 0; chan < 8; chan++) {
1456 if (!(writemask & (1 << chan)))
1457 continue;
1458 LLVMValueRef value = ac_llvm_extract_elem(&ctx->ac, src, chan - component);
1459
1460 unsigned buffer_store_offset = chan % 4;
1461 if (chan == 4) {
1462 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx,
1463 vertex_index,
1464 param_index,
1465 driver_location + 1,
1466 info->output_semantic_name,
1467 info->output_semantic_index,
1468 is_patch);
1469 }
1470
1471 /* Skip LDS stores if there is no LDS read of this output. */
1472 if (!skip_lds_store)
1473 lshs_lds_store(ctx, chan, dw_addr, value);
1474
1475 value = ac_to_integer(&ctx->ac, value);
1476 values[chan] = value;
1477
1478 if (writemask != 0xF && !is_tess_factor) {
1479 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 1,
1480 addr, base,
1481 4 * buffer_store_offset,
1482 1, 0, false);
1483 }
1484
1485 /* Write tess factors into VGPRs for the epilog. */
1486 if (is_tess_factor &&
1487 ctx->shader->selector->tcs_info.tessfactors_are_def_in_all_invocs) {
1488 if (!is_tess_inner) {
1489 LLVMBuildStore(ctx->ac.builder, value, /* outer */
1490 ctx->invoc0_tess_factors[chan]);
1491 } else if (chan < 2) {
1492 LLVMBuildStore(ctx->ac.builder, value, /* inner */
1493 ctx->invoc0_tess_factors[4 + chan]);
1494 }
1495 }
1496 }
1497
1498 if (writemask == 0xF && !is_tess_factor) {
1499 LLVMValueRef value = ac_build_gather_values(&ctx->ac,
1500 values, 4);
1501 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, addr,
1502 base, 0, 1, 0, false);
1503 }
1504 }
1505
1506 LLVMValueRef si_llvm_load_input_gs(struct ac_shader_abi *abi,
1507 unsigned input_index,
1508 unsigned vtx_offset_param,
1509 LLVMTypeRef type,
1510 unsigned swizzle)
1511 {
1512 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1513 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
1514 struct si_shader *shader = ctx->shader;
1515 LLVMValueRef vtx_offset, soffset;
1516 struct tgsi_shader_info *info = &shader->selector->info;
1517 unsigned semantic_name = info->input_semantic_name[input_index];
1518 unsigned semantic_index = info->input_semantic_index[input_index];
1519 unsigned param;
1520 LLVMValueRef value;
1521
1522 param = si_shader_io_get_unique_index(semantic_name, semantic_index, false);
1523
1524 /* GFX9 has the ESGS ring in LDS. */
1525 if (ctx->screen->info.chip_class >= GFX9) {
1526 unsigned index = vtx_offset_param;
1527
1528 switch (index / 2) {
1529 case 0:
1530 vtx_offset = si_unpack_param(ctx, ctx->param_gs_vtx01_offset,
1531 index % 2 ? 16 : 0, 16);
1532 break;
1533 case 1:
1534 vtx_offset = si_unpack_param(ctx, ctx->param_gs_vtx23_offset,
1535 index % 2 ? 16 : 0, 16);
1536 break;
1537 case 2:
1538 vtx_offset = si_unpack_param(ctx, ctx->param_gs_vtx45_offset,
1539 index % 2 ? 16 : 0, 16);
1540 break;
1541 default:
1542 assert(0);
1543 return NULL;
1544 }
1545
1546 unsigned offset = param * 4 + swizzle;
1547 vtx_offset = LLVMBuildAdd(ctx->ac.builder, vtx_offset,
1548 LLVMConstInt(ctx->i32, offset, false), "");
1549
1550 LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->esgs_ring, vtx_offset);
1551 LLVMValueRef value = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1552 if (llvm_type_is_64bit(ctx, type)) {
1553 ptr = LLVMBuildGEP(ctx->ac.builder, ptr,
1554 &ctx->ac.i32_1, 1, "");
1555 LLVMValueRef values[2] = {
1556 value,
1557 LLVMBuildLoad(ctx->ac.builder, ptr, "")
1558 };
1559 value = ac_build_gather_values(&ctx->ac, values, 2);
1560 }
1561 return LLVMBuildBitCast(ctx->ac.builder, value, type, "");
1562 }
1563
1564 /* GFX6: input load from the ESGS ring in memory. */
1565 if (swizzle == ~0) {
1566 LLVMValueRef values[TGSI_NUM_CHANNELS];
1567 unsigned chan;
1568 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1569 values[chan] = si_llvm_load_input_gs(abi, input_index, vtx_offset_param,
1570 type, chan);
1571 }
1572 return ac_build_gather_values(&ctx->ac, values,
1573 TGSI_NUM_CHANNELS);
1574 }
1575
1576 /* Get the vertex offset parameter on GFX6. */
1577 LLVMValueRef gs_vtx_offset = ctx->gs_vtx_offset[vtx_offset_param];
1578
1579 vtx_offset = LLVMBuildMul(ctx->ac.builder, gs_vtx_offset,
1580 LLVMConstInt(ctx->i32, 4, 0), "");
1581
1582 soffset = LLVMConstInt(ctx->i32, (param * 4 + swizzle) * 256, 0);
1583
1584 value = ac_build_buffer_load(&ctx->ac, ctx->esgs_ring, 1, ctx->i32_0,
1585 vtx_offset, soffset, 0, 1, 0, true, false);
1586 if (llvm_type_is_64bit(ctx, type)) {
1587 LLVMValueRef value2;
1588 soffset = LLVMConstInt(ctx->i32, (param * 4 + swizzle + 1) * 256, 0);
1589
1590 value2 = ac_build_buffer_load(&ctx->ac, ctx->esgs_ring, 1,
1591 ctx->i32_0, vtx_offset, soffset,
1592 0, 1, 0, true, false);
1593 return si_llvm_emit_fetch_64bit(bld_base, type, value, value2);
1594 }
1595 return LLVMBuildBitCast(ctx->ac.builder, value, type, "");
1596 }
1597
1598 static LLVMValueRef si_nir_load_input_gs(struct ac_shader_abi *abi,
1599 unsigned location,
1600 unsigned driver_location,
1601 unsigned component,
1602 unsigned num_components,
1603 unsigned vertex_index,
1604 unsigned const_index,
1605 LLVMTypeRef type)
1606 {
1607 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1608
1609 LLVMValueRef value[4];
1610 for (unsigned i = 0; i < num_components; i++) {
1611 unsigned offset = i;
1612 if (llvm_type_is_64bit(ctx, type))
1613 offset *= 2;
1614
1615 offset += component;
1616 value[i + component] = si_llvm_load_input_gs(&ctx->abi, driver_location / 4,
1617 vertex_index, type, offset);
1618 }
1619
1620 return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
1621 }
1622
1623 static LLVMValueRef fetch_input_gs(
1624 struct lp_build_tgsi_context *bld_base,
1625 const struct tgsi_full_src_register *reg,
1626 enum tgsi_opcode_type type,
1627 unsigned swizzle_in)
1628 {
1629 struct si_shader_context *ctx = si_shader_context(bld_base);
1630 struct tgsi_shader_info *info = &ctx->shader->selector->info;
1631 unsigned swizzle = swizzle_in & 0xffff;
1632
1633 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
1634 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID)
1635 return si_get_primitive_id(ctx, swizzle);
1636
1637 if (!reg->Register.Dimension)
1638 return NULL;
1639
1640 return si_llvm_load_input_gs(&ctx->abi, reg->Register.Index,
1641 reg->Dimension.Index,
1642 tgsi2llvmtype(bld_base, type),
1643 swizzle);
1644 }
1645
1646 static int lookup_interp_param_index(unsigned interpolate, unsigned location)
1647 {
1648 switch (interpolate) {
1649 case TGSI_INTERPOLATE_CONSTANT:
1650 return 0;
1651
1652 case TGSI_INTERPOLATE_LINEAR:
1653 if (location == TGSI_INTERPOLATE_LOC_SAMPLE)
1654 return SI_PARAM_LINEAR_SAMPLE;
1655 else if (location == TGSI_INTERPOLATE_LOC_CENTROID)
1656 return SI_PARAM_LINEAR_CENTROID;
1657 else
1658 return SI_PARAM_LINEAR_CENTER;
1659 break;
1660 case TGSI_INTERPOLATE_COLOR:
1661 case TGSI_INTERPOLATE_PERSPECTIVE:
1662 if (location == TGSI_INTERPOLATE_LOC_SAMPLE)
1663 return SI_PARAM_PERSP_SAMPLE;
1664 else if (location == TGSI_INTERPOLATE_LOC_CENTROID)
1665 return SI_PARAM_PERSP_CENTROID;
1666 else
1667 return SI_PARAM_PERSP_CENTER;
1668 break;
1669 default:
1670 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
1671 return -1;
1672 }
1673 }
1674
1675 static LLVMValueRef si_build_fs_interp(struct si_shader_context *ctx,
1676 unsigned attr_index, unsigned chan,
1677 LLVMValueRef prim_mask,
1678 LLVMValueRef i, LLVMValueRef j)
1679 {
1680 if (i || j) {
1681 return ac_build_fs_interp(&ctx->ac,
1682 LLVMConstInt(ctx->i32, chan, 0),
1683 LLVMConstInt(ctx->i32, attr_index, 0),
1684 prim_mask, i, j);
1685 }
1686 return ac_build_fs_interp_mov(&ctx->ac,
1687 LLVMConstInt(ctx->i32, 2, 0), /* P0 */
1688 LLVMConstInt(ctx->i32, chan, 0),
1689 LLVMConstInt(ctx->i32, attr_index, 0),
1690 prim_mask);
1691 }
1692
1693 /**
1694 * Interpolate a fragment shader input.
1695 *
1696 * @param ctx context
1697 * @param input_index index of the input in hardware
1698 * @param semantic_name TGSI_SEMANTIC_*
1699 * @param semantic_index semantic index
1700 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1701 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1702 * @param interp_param interpolation weights (i,j)
1703 * @param prim_mask SI_PARAM_PRIM_MASK
1704 * @param face SI_PARAM_FRONT_FACE
1705 * @param result the return value (4 components)
1706 */
1707 static void interp_fs_input(struct si_shader_context *ctx,
1708 unsigned input_index,
1709 unsigned semantic_name,
1710 unsigned semantic_index,
1711 unsigned num_interp_inputs,
1712 unsigned colors_read_mask,
1713 LLVMValueRef interp_param,
1714 LLVMValueRef prim_mask,
1715 LLVMValueRef face,
1716 LLVMValueRef result[4])
1717 {
1718 LLVMValueRef i = NULL, j = NULL;
1719 unsigned chan;
1720
1721 /* fs.constant returns the param from the middle vertex, so it's not
1722 * really useful for flat shading. It's meant to be used for custom
1723 * interpolation (but the intrinsic can't fetch from the other two
1724 * vertices).
1725 *
1726 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1727 * to do the right thing. The only reason we use fs.constant is that
1728 * fs.interp cannot be used on integers, because they can be equal
1729 * to NaN.
1730 *
1731 * When interp is false we will use fs.constant or for newer llvm,
1732 * amdgcn.interp.mov.
1733 */
1734 bool interp = interp_param != NULL;
1735
1736 if (interp) {
1737 interp_param = LLVMBuildBitCast(ctx->ac.builder, interp_param,
1738 LLVMVectorType(ctx->f32, 2), "");
1739
1740 i = LLVMBuildExtractElement(ctx->ac.builder, interp_param,
1741 ctx->i32_0, "");
1742 j = LLVMBuildExtractElement(ctx->ac.builder, interp_param,
1743 ctx->i32_1, "");
1744 }
1745
1746 if (semantic_name == TGSI_SEMANTIC_COLOR &&
1747 ctx->shader->key.part.ps.prolog.color_two_side) {
1748 LLVMValueRef is_face_positive;
1749
1750 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1751 * otherwise it's at offset "num_inputs".
1752 */
1753 unsigned back_attr_offset = num_interp_inputs;
1754 if (semantic_index == 1 && colors_read_mask & 0xf)
1755 back_attr_offset += 1;
1756
1757 is_face_positive = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE,
1758 face, ctx->i32_0, "");
1759
1760 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1761 LLVMValueRef front, back;
1762
1763 front = si_build_fs_interp(ctx,
1764 input_index, chan,
1765 prim_mask, i, j);
1766 back = si_build_fs_interp(ctx,
1767 back_attr_offset, chan,
1768 prim_mask, i, j);
1769
1770 result[chan] = LLVMBuildSelect(ctx->ac.builder,
1771 is_face_positive,
1772 front,
1773 back,
1774 "");
1775 }
1776 } else if (semantic_name == TGSI_SEMANTIC_FOG) {
1777 result[0] = si_build_fs_interp(ctx, input_index,
1778 0, prim_mask, i, j);
1779 result[1] =
1780 result[2] = LLVMConstReal(ctx->f32, 0.0f);
1781 result[3] = LLVMConstReal(ctx->f32, 1.0f);
1782 } else {
1783 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1784 result[chan] = si_build_fs_interp(ctx,
1785 input_index, chan,
1786 prim_mask, i, j);
1787 }
1788 }
1789 }
1790
1791 void si_llvm_load_input_fs(
1792 struct si_shader_context *ctx,
1793 unsigned input_index,
1794 LLVMValueRef out[4])
1795 {
1796 struct si_shader *shader = ctx->shader;
1797 struct tgsi_shader_info *info = &shader->selector->info;
1798 LLVMValueRef main_fn = ctx->main_fn;
1799 LLVMValueRef interp_param = NULL;
1800 int interp_param_idx;
1801 enum tgsi_semantic semantic_name = info->input_semantic_name[input_index];
1802 unsigned semantic_index = info->input_semantic_index[input_index];
1803 enum tgsi_interpolate_mode interp_mode = info->input_interpolate[input_index];
1804 enum tgsi_interpolate_loc interp_loc = info->input_interpolate_loc[input_index];
1805
1806 /* Get colors from input VGPRs (set by the prolog). */
1807 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1808 unsigned colors_read = shader->selector->info.colors_read;
1809 unsigned mask = colors_read >> (semantic_index * 4);
1810 unsigned offset = SI_PARAM_POS_FIXED_PT + 1 +
1811 (semantic_index ? util_bitcount(colors_read & 0xf) : 0);
1812 LLVMValueRef undef = LLVMGetUndef(ctx->f32);
1813
1814 out[0] = mask & 0x1 ? LLVMGetParam(main_fn, offset++) : undef;
1815 out[1] = mask & 0x2 ? LLVMGetParam(main_fn, offset++) : undef;
1816 out[2] = mask & 0x4 ? LLVMGetParam(main_fn, offset++) : undef;
1817 out[3] = mask & 0x8 ? LLVMGetParam(main_fn, offset++) : undef;
1818 return;
1819 }
1820
1821 interp_param_idx = lookup_interp_param_index(interp_mode, interp_loc);
1822 if (interp_param_idx == -1)
1823 return;
1824 else if (interp_param_idx) {
1825 interp_param = LLVMGetParam(ctx->main_fn, interp_param_idx);
1826 }
1827
1828 interp_fs_input(ctx, input_index, semantic_name,
1829 semantic_index, 0, /* this param is unused */
1830 shader->selector->info.colors_read, interp_param,
1831 ctx->abi.prim_mask,
1832 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE),
1833 &out[0]);
1834 }
1835
1836 static void declare_input_fs(
1837 struct si_shader_context *ctx,
1838 unsigned input_index,
1839 const struct tgsi_full_declaration *decl,
1840 LLVMValueRef out[4])
1841 {
1842 si_llvm_load_input_fs(ctx, input_index, out);
1843 }
1844
1845 LLVMValueRef si_get_sample_id(struct si_shader_context *ctx)
1846 {
1847 return si_unpack_param(ctx, SI_PARAM_ANCILLARY, 8, 4);
1848 }
1849
1850 static LLVMValueRef get_base_vertex(struct ac_shader_abi *abi)
1851 {
1852 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1853
1854 /* For non-indexed draws, the base vertex set by the driver
1855 * (for direct draws) or the CP (for indirect draws) is the
1856 * first vertex ID, but GLSL expects 0 to be returned.
1857 */
1858 LLVMValueRef vs_state = LLVMGetParam(ctx->main_fn,
1859 ctx->param_vs_state_bits);
1860 LLVMValueRef indexed;
1861
1862 indexed = LLVMBuildLShr(ctx->ac.builder, vs_state, ctx->i32_1, "");
1863 indexed = LLVMBuildTrunc(ctx->ac.builder, indexed, ctx->i1, "");
1864
1865 return LLVMBuildSelect(ctx->ac.builder, indexed, ctx->abi.base_vertex,
1866 ctx->i32_0, "");
1867 }
1868
1869 static LLVMValueRef get_block_size(struct ac_shader_abi *abi)
1870 {
1871 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1872
1873 LLVMValueRef values[3];
1874 LLVMValueRef result;
1875 unsigned i;
1876 unsigned *properties = ctx->shader->selector->info.properties;
1877
1878 if (properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] != 0) {
1879 unsigned sizes[3] = {
1880 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH],
1881 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT],
1882 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH]
1883 };
1884
1885 for (i = 0; i < 3; ++i)
1886 values[i] = LLVMConstInt(ctx->i32, sizes[i], 0);
1887
1888 result = ac_build_gather_values(&ctx->ac, values, 3);
1889 } else {
1890 result = LLVMGetParam(ctx->main_fn, ctx->param_block_size);
1891 }
1892
1893 return result;
1894 }
1895
1896 /**
1897 * Load a dword from a constant buffer.
1898 */
1899 static LLVMValueRef buffer_load_const(struct si_shader_context *ctx,
1900 LLVMValueRef resource,
1901 LLVMValueRef offset)
1902 {
1903 return ac_build_buffer_load(&ctx->ac, resource, 1, NULL, offset, NULL,
1904 0, 0, 0, true, true);
1905 }
1906
1907 static LLVMValueRef load_sample_position(struct ac_shader_abi *abi, LLVMValueRef sample_id)
1908 {
1909 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1910 LLVMValueRef desc = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
1911 LLVMValueRef buf_index = LLVMConstInt(ctx->i32, SI_PS_CONST_SAMPLE_POSITIONS, 0);
1912 LLVMValueRef resource = ac_build_load_to_sgpr(&ctx->ac, desc, buf_index);
1913
1914 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1915 LLVMValueRef offset0 = LLVMBuildMul(ctx->ac.builder, sample_id, LLVMConstInt(ctx->i32, 8, 0), "");
1916 LLVMValueRef offset1 = LLVMBuildAdd(ctx->ac.builder, offset0, LLVMConstInt(ctx->i32, 4, 0), "");
1917
1918 LLVMValueRef pos[4] = {
1919 buffer_load_const(ctx, resource, offset0),
1920 buffer_load_const(ctx, resource, offset1),
1921 LLVMConstReal(ctx->f32, 0),
1922 LLVMConstReal(ctx->f32, 0)
1923 };
1924
1925 return ac_build_gather_values(&ctx->ac, pos, 4);
1926 }
1927
1928 static LLVMValueRef load_sample_mask_in(struct ac_shader_abi *abi)
1929 {
1930 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1931 return ac_to_integer(&ctx->ac, abi->sample_coverage);
1932 }
1933
1934 static LLVMValueRef si_load_tess_coord(struct ac_shader_abi *abi)
1935 {
1936 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1937 LLVMValueRef coord[4] = {
1938 LLVMGetParam(ctx->main_fn, ctx->param_tes_u),
1939 LLVMGetParam(ctx->main_fn, ctx->param_tes_v),
1940 ctx->ac.f32_0,
1941 ctx->ac.f32_0
1942 };
1943
1944 /* For triangles, the vector should be (u, v, 1-u-v). */
1945 if (ctx->shader->selector->info.properties[TGSI_PROPERTY_TES_PRIM_MODE] ==
1946 PIPE_PRIM_TRIANGLES) {
1947 coord[2] = LLVMBuildFSub(ctx->ac.builder, ctx->ac.f32_1,
1948 LLVMBuildFAdd(ctx->ac.builder,
1949 coord[0], coord[1], ""), "");
1950 }
1951 return ac_build_gather_values(&ctx->ac, coord, 4);
1952 }
1953
1954 static LLVMValueRef load_tess_level(struct si_shader_context *ctx,
1955 unsigned semantic_name)
1956 {
1957 LLVMValueRef base, addr;
1958
1959 int param = si_shader_io_get_unique_index_patch(semantic_name, 0);
1960
1961 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
1962 addr = get_tcs_tes_buffer_address(ctx, get_rel_patch_id(ctx), NULL,
1963 LLVMConstInt(ctx->i32, param, 0));
1964
1965 return buffer_load(&ctx->bld_base, ctx->f32,
1966 ~0, ctx->tess_offchip_ring, base, addr, true);
1967
1968 }
1969
1970 static LLVMValueRef si_load_tess_level(struct ac_shader_abi *abi,
1971 unsigned varying_id)
1972 {
1973 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1974 unsigned semantic_name;
1975
1976 switch (varying_id) {
1977 case VARYING_SLOT_TESS_LEVEL_INNER:
1978 semantic_name = TGSI_SEMANTIC_TESSINNER;
1979 break;
1980 case VARYING_SLOT_TESS_LEVEL_OUTER:
1981 semantic_name = TGSI_SEMANTIC_TESSOUTER;
1982 break;
1983 default:
1984 unreachable("unknown tess level");
1985 }
1986
1987 return load_tess_level(ctx, semantic_name);
1988
1989 }
1990
1991 static LLVMValueRef si_load_patch_vertices_in(struct ac_shader_abi *abi)
1992 {
1993 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1994 if (ctx->type == PIPE_SHADER_TESS_CTRL)
1995 return si_unpack_param(ctx, ctx->param_tcs_out_lds_layout, 13, 6);
1996 else if (ctx->type == PIPE_SHADER_TESS_EVAL)
1997 return get_num_tcs_out_vertices(ctx);
1998 else
1999 unreachable("invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
2000 }
2001
2002 void si_load_system_value(struct si_shader_context *ctx,
2003 unsigned index,
2004 const struct tgsi_full_declaration *decl)
2005 {
2006 LLVMValueRef value = 0;
2007
2008 assert(index < RADEON_LLVM_MAX_SYSTEM_VALUES);
2009
2010 switch (decl->Semantic.Name) {
2011 case TGSI_SEMANTIC_INSTANCEID:
2012 value = ctx->abi.instance_id;
2013 break;
2014
2015 case TGSI_SEMANTIC_VERTEXID:
2016 value = LLVMBuildAdd(ctx->ac.builder,
2017 ctx->abi.vertex_id,
2018 ctx->abi.base_vertex, "");
2019 break;
2020
2021 case TGSI_SEMANTIC_VERTEXID_NOBASE:
2022 /* Unused. Clarify the meaning in indexed vs. non-indexed
2023 * draws if this is ever used again. */
2024 assert(false);
2025 break;
2026
2027 case TGSI_SEMANTIC_BASEVERTEX:
2028 value = get_base_vertex(&ctx->abi);
2029 break;
2030
2031 case TGSI_SEMANTIC_BASEINSTANCE:
2032 value = ctx->abi.start_instance;
2033 break;
2034
2035 case TGSI_SEMANTIC_DRAWID:
2036 value = ctx->abi.draw_id;
2037 break;
2038
2039 case TGSI_SEMANTIC_INVOCATIONID:
2040 if (ctx->type == PIPE_SHADER_TESS_CTRL)
2041 value = unpack_llvm_param(ctx, ctx->abi.tcs_rel_ids, 8, 5);
2042 else if (ctx->type == PIPE_SHADER_GEOMETRY)
2043 value = ctx->abi.gs_invocation_id;
2044 else
2045 assert(!"INVOCATIONID not implemented");
2046 break;
2047
2048 case TGSI_SEMANTIC_POSITION:
2049 {
2050 LLVMValueRef pos[4] = {
2051 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_X_FLOAT),
2052 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_Y_FLOAT),
2053 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_Z_FLOAT),
2054 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1,
2055 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_W_FLOAT)),
2056 };
2057 value = ac_build_gather_values(&ctx->ac, pos, 4);
2058 break;
2059 }
2060
2061 case TGSI_SEMANTIC_FACE:
2062 value = ctx->abi.front_face;
2063 break;
2064
2065 case TGSI_SEMANTIC_SAMPLEID:
2066 value = si_get_sample_id(ctx);
2067 break;
2068
2069 case TGSI_SEMANTIC_SAMPLEPOS: {
2070 LLVMValueRef pos[4] = {
2071 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_X_FLOAT),
2072 LLVMGetParam(ctx->main_fn, SI_PARAM_POS_Y_FLOAT),
2073 LLVMConstReal(ctx->f32, 0),
2074 LLVMConstReal(ctx->f32, 0)
2075 };
2076 pos[0] = ac_build_fract(&ctx->ac, pos[0], 32);
2077 pos[1] = ac_build_fract(&ctx->ac, pos[1], 32);
2078 value = ac_build_gather_values(&ctx->ac, pos, 4);
2079 break;
2080 }
2081
2082 case TGSI_SEMANTIC_SAMPLEMASK:
2083 /* This can only occur with the OpenGL Core profile, which
2084 * doesn't support smoothing.
2085 */
2086 value = LLVMGetParam(ctx->main_fn, SI_PARAM_SAMPLE_COVERAGE);
2087 break;
2088
2089 case TGSI_SEMANTIC_TESSCOORD:
2090 value = si_load_tess_coord(&ctx->abi);
2091 break;
2092
2093 case TGSI_SEMANTIC_VERTICESIN:
2094 value = si_load_patch_vertices_in(&ctx->abi);
2095 break;
2096
2097 case TGSI_SEMANTIC_TESSINNER:
2098 case TGSI_SEMANTIC_TESSOUTER:
2099 value = load_tess_level(ctx, decl->Semantic.Name);
2100 break;
2101
2102 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI:
2103 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI:
2104 {
2105 LLVMValueRef buf, slot, val[4];
2106 int i, offset;
2107
2108 slot = LLVMConstInt(ctx->i32, SI_HS_CONST_DEFAULT_TESS_LEVELS, 0);
2109 buf = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
2110 buf = ac_build_load_to_sgpr(&ctx->ac, buf, slot);
2111 offset = decl->Semantic.Name == TGSI_SEMANTIC_DEFAULT_TESSINNER_SI ? 4 : 0;
2112
2113 for (i = 0; i < 4; i++)
2114 val[i] = buffer_load_const(ctx, buf,
2115 LLVMConstInt(ctx->i32, (offset + i) * 4, 0));
2116 value = ac_build_gather_values(&ctx->ac, val, 4);
2117 break;
2118 }
2119
2120 case TGSI_SEMANTIC_PRIMID:
2121 value = si_get_primitive_id(ctx, 0);
2122 break;
2123
2124 case TGSI_SEMANTIC_GRID_SIZE:
2125 value = ctx->abi.num_work_groups;
2126 break;
2127
2128 case TGSI_SEMANTIC_BLOCK_SIZE:
2129 value = get_block_size(&ctx->abi);
2130 break;
2131
2132 case TGSI_SEMANTIC_BLOCK_ID:
2133 {
2134 LLVMValueRef values[3];
2135
2136 for (int i = 0; i < 3; i++) {
2137 values[i] = ctx->i32_0;
2138 if (ctx->abi.workgroup_ids[i]) {
2139 values[i] = ctx->abi.workgroup_ids[i];
2140 }
2141 }
2142 value = ac_build_gather_values(&ctx->ac, values, 3);
2143 break;
2144 }
2145
2146 case TGSI_SEMANTIC_THREAD_ID:
2147 value = ctx->abi.local_invocation_ids;
2148 break;
2149
2150 case TGSI_SEMANTIC_HELPER_INVOCATION:
2151 value = ac_build_load_helper_invocation(&ctx->ac);
2152 break;
2153
2154 case TGSI_SEMANTIC_SUBGROUP_SIZE:
2155 value = LLVMConstInt(ctx->i32, 64, 0);
2156 break;
2157
2158 case TGSI_SEMANTIC_SUBGROUP_INVOCATION:
2159 value = ac_get_thread_id(&ctx->ac);
2160 break;
2161
2162 case TGSI_SEMANTIC_SUBGROUP_EQ_MASK:
2163 {
2164 LLVMValueRef id = ac_get_thread_id(&ctx->ac);
2165 id = LLVMBuildZExt(ctx->ac.builder, id, ctx->i64, "");
2166 value = LLVMBuildShl(ctx->ac.builder, LLVMConstInt(ctx->i64, 1, 0), id, "");
2167 value = LLVMBuildBitCast(ctx->ac.builder, value, ctx->v2i32, "");
2168 break;
2169 }
2170
2171 case TGSI_SEMANTIC_SUBGROUP_GE_MASK:
2172 case TGSI_SEMANTIC_SUBGROUP_GT_MASK:
2173 case TGSI_SEMANTIC_SUBGROUP_LE_MASK:
2174 case TGSI_SEMANTIC_SUBGROUP_LT_MASK:
2175 {
2176 LLVMValueRef id = ac_get_thread_id(&ctx->ac);
2177 if (decl->Semantic.Name == TGSI_SEMANTIC_SUBGROUP_GT_MASK ||
2178 decl->Semantic.Name == TGSI_SEMANTIC_SUBGROUP_LE_MASK) {
2179 /* All bits set except LSB */
2180 value = LLVMConstInt(ctx->i64, -2, 0);
2181 } else {
2182 /* All bits set */
2183 value = LLVMConstInt(ctx->i64, -1, 0);
2184 }
2185 id = LLVMBuildZExt(ctx->ac.builder, id, ctx->i64, "");
2186 value = LLVMBuildShl(ctx->ac.builder, value, id, "");
2187 if (decl->Semantic.Name == TGSI_SEMANTIC_SUBGROUP_LE_MASK ||
2188 decl->Semantic.Name == TGSI_SEMANTIC_SUBGROUP_LT_MASK)
2189 value = LLVMBuildNot(ctx->ac.builder, value, "");
2190 value = LLVMBuildBitCast(ctx->ac.builder, value, ctx->v2i32, "");
2191 break;
2192 }
2193
2194 case TGSI_SEMANTIC_CS_USER_DATA:
2195 value = LLVMGetParam(ctx->main_fn, ctx->param_cs_user_data);
2196 break;
2197
2198 default:
2199 assert(!"unknown system value");
2200 return;
2201 }
2202
2203 ctx->system_values[index] = value;
2204 }
2205
2206 void si_declare_compute_memory(struct si_shader_context *ctx)
2207 {
2208 struct si_shader_selector *sel = ctx->shader->selector;
2209 unsigned lds_size = sel->info.properties[TGSI_PROPERTY_CS_LOCAL_SIZE];
2210
2211 LLVMTypeRef i8p = LLVMPointerType(ctx->i8, AC_ADDR_SPACE_LDS);
2212 LLVMValueRef var;
2213
2214 assert(!ctx->ac.lds);
2215
2216 var = LLVMAddGlobalInAddressSpace(ctx->ac.module,
2217 LLVMArrayType(ctx->i8, lds_size),
2218 "compute_lds",
2219 AC_ADDR_SPACE_LDS);
2220 LLVMSetAlignment(var, 64 * 1024);
2221
2222 ctx->ac.lds = LLVMBuildBitCast(ctx->ac.builder, var, i8p, "");
2223 }
2224
2225 void si_tgsi_declare_compute_memory(struct si_shader_context *ctx,
2226 const struct tgsi_full_declaration *decl)
2227 {
2228 assert(decl->Declaration.MemType == TGSI_MEMORY_TYPE_SHARED);
2229 assert(decl->Range.First == decl->Range.Last);
2230
2231 si_declare_compute_memory(ctx);
2232 }
2233
2234 static LLVMValueRef load_const_buffer_desc_fast_path(struct si_shader_context *ctx)
2235 {
2236 LLVMValueRef ptr =
2237 LLVMGetParam(ctx->main_fn, ctx->param_const_and_shader_buffers);
2238 struct si_shader_selector *sel = ctx->shader->selector;
2239
2240 /* Do the bounds checking with a descriptor, because
2241 * doing computation and manual bounds checking of 64-bit
2242 * addresses generates horrible VALU code with very high
2243 * VGPR usage and very low SIMD occupancy.
2244 */
2245 ptr = LLVMBuildPtrToInt(ctx->ac.builder, ptr, ctx->ac.intptr, "");
2246
2247 LLVMValueRef desc0, desc1;
2248 desc0 = ptr;
2249 desc1 = LLVMConstInt(ctx->i32,
2250 S_008F04_BASE_ADDRESS_HI(ctx->screen->info.address32_hi), 0);
2251
2252 LLVMValueRef desc_elems[] = {
2253 desc0,
2254 desc1,
2255 LLVMConstInt(ctx->i32, (sel->info.const_file_max[0] + 1) * 16, 0),
2256 LLVMConstInt(ctx->i32,
2257 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2258 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2259 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2260 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
2261 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2262 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32), 0)
2263 };
2264
2265 return ac_build_gather_values(&ctx->ac, desc_elems, 4);
2266 }
2267
2268 static LLVMValueRef load_const_buffer_desc(struct si_shader_context *ctx, int i)
2269 {
2270 LLVMValueRef list_ptr = LLVMGetParam(ctx->main_fn,
2271 ctx->param_const_and_shader_buffers);
2272
2273 return ac_build_load_to_sgpr(&ctx->ac, list_ptr,
2274 LLVMConstInt(ctx->i32, si_get_constbuf_slot(i), 0));
2275 }
2276
2277 static LLVMValueRef load_ubo(struct ac_shader_abi *abi, LLVMValueRef index)
2278 {
2279 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
2280 struct si_shader_selector *sel = ctx->shader->selector;
2281
2282 LLVMValueRef ptr = LLVMGetParam(ctx->main_fn, ctx->param_const_and_shader_buffers);
2283
2284 if (sel->info.const_buffers_declared == 1 &&
2285 sel->info.shader_buffers_declared == 0) {
2286 return load_const_buffer_desc_fast_path(ctx);
2287 }
2288
2289 index = si_llvm_bound_index(ctx, index, ctx->num_const_buffers);
2290 index = LLVMBuildAdd(ctx->ac.builder, index,
2291 LLVMConstInt(ctx->i32, SI_NUM_SHADER_BUFFERS, 0), "");
2292
2293 return ac_build_load_to_sgpr(&ctx->ac, ptr, index);
2294 }
2295
2296 static LLVMValueRef
2297 load_ssbo(struct ac_shader_abi *abi, LLVMValueRef index, bool write)
2298 {
2299 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
2300 LLVMValueRef rsrc_ptr = LLVMGetParam(ctx->main_fn,
2301 ctx->param_const_and_shader_buffers);
2302
2303 index = si_llvm_bound_index(ctx, index, ctx->num_shader_buffers);
2304 index = LLVMBuildSub(ctx->ac.builder,
2305 LLVMConstInt(ctx->i32, SI_NUM_SHADER_BUFFERS - 1, 0),
2306 index, "");
2307
2308 return ac_build_load_to_sgpr(&ctx->ac, rsrc_ptr, index);
2309 }
2310
2311 static LLVMValueRef fetch_constant(
2312 struct lp_build_tgsi_context *bld_base,
2313 const struct tgsi_full_src_register *reg,
2314 enum tgsi_opcode_type type,
2315 unsigned swizzle_in)
2316 {
2317 struct si_shader_context *ctx = si_shader_context(bld_base);
2318 struct si_shader_selector *sel = ctx->shader->selector;
2319 const struct tgsi_ind_register *ireg = &reg->Indirect;
2320 unsigned buf, idx;
2321 unsigned swizzle = swizzle_in & 0xffff;
2322
2323 LLVMValueRef addr, bufp;
2324
2325 if (swizzle_in == LP_CHAN_ALL) {
2326 unsigned chan;
2327 LLVMValueRef values[4];
2328 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
2329 values[chan] = fetch_constant(bld_base, reg, type, chan);
2330
2331 return ac_build_gather_values(&ctx->ac, values, 4);
2332 }
2333
2334 /* Split 64-bit loads. */
2335 if (tgsi_type_is_64bit(type)) {
2336 LLVMValueRef lo, hi;
2337
2338 lo = fetch_constant(bld_base, reg, TGSI_TYPE_UNSIGNED, swizzle);
2339 hi = fetch_constant(bld_base, reg, TGSI_TYPE_UNSIGNED, (swizzle_in >> 16));
2340 return si_llvm_emit_fetch_64bit(bld_base, tgsi2llvmtype(bld_base, type),
2341 lo, hi);
2342 }
2343
2344 idx = reg->Register.Index * 4 + swizzle;
2345 if (reg->Register.Indirect) {
2346 addr = si_get_indirect_index(ctx, ireg, 16, idx * 4);
2347 } else {
2348 addr = LLVMConstInt(ctx->i32, idx * 4, 0);
2349 }
2350
2351 /* Fast path when user data SGPRs point to constant buffer 0 directly. */
2352 if (sel->info.const_buffers_declared == 1 &&
2353 sel->info.shader_buffers_declared == 0) {
2354 LLVMValueRef desc = load_const_buffer_desc_fast_path(ctx);
2355 LLVMValueRef result = buffer_load_const(ctx, desc, addr);
2356 return bitcast(bld_base, type, result);
2357 }
2358
2359 assert(reg->Register.Dimension);
2360 buf = reg->Dimension.Index;
2361
2362 if (reg->Dimension.Indirect) {
2363 LLVMValueRef ptr = LLVMGetParam(ctx->main_fn, ctx->param_const_and_shader_buffers);
2364 LLVMValueRef index;
2365 index = si_get_bounded_indirect_index(ctx, &reg->DimIndirect,
2366 reg->Dimension.Index,
2367 ctx->num_const_buffers);
2368 index = LLVMBuildAdd(ctx->ac.builder, index,
2369 LLVMConstInt(ctx->i32, SI_NUM_SHADER_BUFFERS, 0), "");
2370 bufp = ac_build_load_to_sgpr(&ctx->ac, ptr, index);
2371 } else
2372 bufp = load_const_buffer_desc(ctx, buf);
2373
2374 return bitcast(bld_base, type, buffer_load_const(ctx, bufp, addr));
2375 }
2376
2377 /* Initialize arguments for the shader export intrinsic */
2378 static void si_llvm_init_export_args(struct si_shader_context *ctx,
2379 LLVMValueRef *values,
2380 unsigned target,
2381 struct ac_export_args *args)
2382 {
2383 LLVMValueRef f32undef = LLVMGetUndef(ctx->ac.f32);
2384 unsigned spi_shader_col_format = V_028714_SPI_SHADER_32_ABGR;
2385 unsigned chan;
2386 bool is_int8, is_int10;
2387
2388 /* Default is 0xf. Adjusted below depending on the format. */
2389 args->enabled_channels = 0xf; /* writemask */
2390
2391 /* Specify whether the EXEC mask represents the valid mask */
2392 args->valid_mask = 0;
2393
2394 /* Specify whether this is the last export */
2395 args->done = 0;
2396
2397 /* Specify the target we are exporting */
2398 args->target = target;
2399
2400 if (ctx->type == PIPE_SHADER_FRAGMENT) {
2401 const struct si_shader_key *key = &ctx->shader->key;
2402 unsigned col_formats = key->part.ps.epilog.spi_shader_col_format;
2403 int cbuf = target - V_008DFC_SQ_EXP_MRT;
2404
2405 assert(cbuf >= 0 && cbuf < 8);
2406 spi_shader_col_format = (col_formats >> (cbuf * 4)) & 0xf;
2407 is_int8 = (key->part.ps.epilog.color_is_int8 >> cbuf) & 0x1;
2408 is_int10 = (key->part.ps.epilog.color_is_int10 >> cbuf) & 0x1;
2409 }
2410
2411 args->compr = false;
2412 args->out[0] = f32undef;
2413 args->out[1] = f32undef;
2414 args->out[2] = f32undef;
2415 args->out[3] = f32undef;
2416
2417 LLVMValueRef (*packf)(struct ac_llvm_context *ctx, LLVMValueRef args[2]) = NULL;
2418 LLVMValueRef (*packi)(struct ac_llvm_context *ctx, LLVMValueRef args[2],
2419 unsigned bits, bool hi) = NULL;
2420
2421 switch (spi_shader_col_format) {
2422 case V_028714_SPI_SHADER_ZERO:
2423 args->enabled_channels = 0; /* writemask */
2424 args->target = V_008DFC_SQ_EXP_NULL;
2425 break;
2426
2427 case V_028714_SPI_SHADER_32_R:
2428 args->enabled_channels = 1; /* writemask */
2429 args->out[0] = values[0];
2430 break;
2431
2432 case V_028714_SPI_SHADER_32_GR:
2433 args->enabled_channels = 0x3; /* writemask */
2434 args->out[0] = values[0];
2435 args->out[1] = values[1];
2436 break;
2437
2438 case V_028714_SPI_SHADER_32_AR:
2439 args->enabled_channels = 0x9; /* writemask */
2440 args->out[0] = values[0];
2441 args->out[3] = values[3];
2442 break;
2443
2444 case V_028714_SPI_SHADER_FP16_ABGR:
2445 packf = ac_build_cvt_pkrtz_f16;
2446 break;
2447
2448 case V_028714_SPI_SHADER_UNORM16_ABGR:
2449 packf = ac_build_cvt_pknorm_u16;
2450 break;
2451
2452 case V_028714_SPI_SHADER_SNORM16_ABGR:
2453 packf = ac_build_cvt_pknorm_i16;
2454 break;
2455
2456 case V_028714_SPI_SHADER_UINT16_ABGR:
2457 packi = ac_build_cvt_pk_u16;
2458 break;
2459
2460 case V_028714_SPI_SHADER_SINT16_ABGR:
2461 packi = ac_build_cvt_pk_i16;
2462 break;
2463
2464 case V_028714_SPI_SHADER_32_ABGR:
2465 memcpy(&args->out[0], values, sizeof(values[0]) * 4);
2466 break;
2467 }
2468
2469 /* Pack f16 or norm_i16/u16. */
2470 if (packf) {
2471 for (chan = 0; chan < 2; chan++) {
2472 LLVMValueRef pack_args[2] = {
2473 values[2 * chan],
2474 values[2 * chan + 1]
2475 };
2476 LLVMValueRef packed;
2477
2478 packed = packf(&ctx->ac, pack_args);
2479 args->out[chan] = ac_to_float(&ctx->ac, packed);
2480 }
2481 args->compr = 1; /* COMPR flag */
2482 }
2483 /* Pack i16/u16. */
2484 if (packi) {
2485 for (chan = 0; chan < 2; chan++) {
2486 LLVMValueRef pack_args[2] = {
2487 ac_to_integer(&ctx->ac, values[2 * chan]),
2488 ac_to_integer(&ctx->ac, values[2 * chan + 1])
2489 };
2490 LLVMValueRef packed;
2491
2492 packed = packi(&ctx->ac, pack_args,
2493 is_int8 ? 8 : is_int10 ? 10 : 16,
2494 chan == 1);
2495 args->out[chan] = ac_to_float(&ctx->ac, packed);
2496 }
2497 args->compr = 1; /* COMPR flag */
2498 }
2499 }
2500
2501 static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
2502 LLVMValueRef alpha)
2503 {
2504 struct si_shader_context *ctx = si_shader_context(bld_base);
2505
2506 if (ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_NEVER) {
2507 static LLVMRealPredicate cond_map[PIPE_FUNC_ALWAYS + 1] = {
2508 [PIPE_FUNC_LESS] = LLVMRealOLT,
2509 [PIPE_FUNC_EQUAL] = LLVMRealOEQ,
2510 [PIPE_FUNC_LEQUAL] = LLVMRealOLE,
2511 [PIPE_FUNC_GREATER] = LLVMRealOGT,
2512 [PIPE_FUNC_NOTEQUAL] = LLVMRealONE,
2513 [PIPE_FUNC_GEQUAL] = LLVMRealOGE,
2514 };
2515 LLVMRealPredicate cond = cond_map[ctx->shader->key.part.ps.epilog.alpha_func];
2516 assert(cond);
2517
2518 LLVMValueRef alpha_ref = LLVMGetParam(ctx->main_fn,
2519 SI_PARAM_ALPHA_REF);
2520 LLVMValueRef alpha_pass =
2521 LLVMBuildFCmp(ctx->ac.builder, cond, alpha, alpha_ref, "");
2522 ac_build_kill_if_false(&ctx->ac, alpha_pass);
2523 } else {
2524 ac_build_kill_if_false(&ctx->ac, ctx->i1false);
2525 }
2526 }
2527
2528 static LLVMValueRef si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context *bld_base,
2529 LLVMValueRef alpha,
2530 unsigned samplemask_param)
2531 {
2532 struct si_shader_context *ctx = si_shader_context(bld_base);
2533 LLVMValueRef coverage;
2534
2535 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2536 coverage = LLVMGetParam(ctx->main_fn,
2537 samplemask_param);
2538 coverage = ac_to_integer(&ctx->ac, coverage);
2539
2540 coverage = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32",
2541 ctx->i32,
2542 &coverage, 1, AC_FUNC_ATTR_READNONE);
2543
2544 coverage = LLVMBuildUIToFP(ctx->ac.builder, coverage,
2545 ctx->f32, "");
2546
2547 coverage = LLVMBuildFMul(ctx->ac.builder, coverage,
2548 LLVMConstReal(ctx->f32,
2549 1.0 / SI_NUM_SMOOTH_AA_SAMPLES), "");
2550
2551 return LLVMBuildFMul(ctx->ac.builder, alpha, coverage, "");
2552 }
2553
2554 static void si_llvm_emit_clipvertex(struct si_shader_context *ctx,
2555 struct ac_export_args *pos, LLVMValueRef *out_elts)
2556 {
2557 unsigned reg_index;
2558 unsigned chan;
2559 unsigned const_chan;
2560 LLVMValueRef base_elt;
2561 LLVMValueRef ptr = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
2562 LLVMValueRef constbuf_index = LLVMConstInt(ctx->i32,
2563 SI_VS_CONST_CLIP_PLANES, 0);
2564 LLVMValueRef const_resource = ac_build_load_to_sgpr(&ctx->ac, ptr, constbuf_index);
2565
2566 for (reg_index = 0; reg_index < 2; reg_index ++) {
2567 struct ac_export_args *args = &pos[2 + reg_index];
2568
2569 args->out[0] =
2570 args->out[1] =
2571 args->out[2] =
2572 args->out[3] = LLVMConstReal(ctx->f32, 0.0f);
2573
2574 /* Compute dot products of position and user clip plane vectors */
2575 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2576 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
2577 LLVMValueRef addr =
2578 LLVMConstInt(ctx->i32, ((reg_index * 4 + chan) * 4 +
2579 const_chan) * 4, 0);
2580 base_elt = buffer_load_const(ctx, const_resource,
2581 addr);
2582 args->out[chan] = ac_build_fmad(&ctx->ac, base_elt,
2583 out_elts[const_chan], args->out[chan]);
2584 }
2585 }
2586
2587 args->enabled_channels = 0xf;
2588 args->valid_mask = 0;
2589 args->done = 0;
2590 args->target = V_008DFC_SQ_EXP_POS + 2 + reg_index;
2591 args->compr = 0;
2592 }
2593 }
2594
2595 static void si_dump_streamout(struct pipe_stream_output_info *so)
2596 {
2597 unsigned i;
2598
2599 if (so->num_outputs)
2600 fprintf(stderr, "STREAMOUT\n");
2601
2602 for (i = 0; i < so->num_outputs; i++) {
2603 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
2604 so->output[i].start_component;
2605 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2606 i, so->output[i].output_buffer,
2607 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
2608 so->output[i].register_index,
2609 mask & 1 ? "x" : "",
2610 mask & 2 ? "y" : "",
2611 mask & 4 ? "z" : "",
2612 mask & 8 ? "w" : "");
2613 }
2614 }
2615
2616 void si_emit_streamout_output(struct si_shader_context *ctx,
2617 LLVMValueRef const *so_buffers,
2618 LLVMValueRef const *so_write_offsets,
2619 struct pipe_stream_output *stream_out,
2620 struct si_shader_output_values *shader_out)
2621 {
2622 unsigned buf_idx = stream_out->output_buffer;
2623 unsigned start = stream_out->start_component;
2624 unsigned num_comps = stream_out->num_components;
2625 LLVMValueRef out[4];
2626
2627 assert(num_comps && num_comps <= 4);
2628 if (!num_comps || num_comps > 4)
2629 return;
2630
2631 /* Load the output as int. */
2632 for (int j = 0; j < num_comps; j++) {
2633 assert(stream_out->stream == shader_out->vertex_stream[start + j]);
2634
2635 out[j] = ac_to_integer(&ctx->ac, shader_out->values[start + j]);
2636 }
2637
2638 /* Pack the output. */
2639 LLVMValueRef vdata = NULL;
2640
2641 switch (num_comps) {
2642 case 1: /* as i32 */
2643 vdata = out[0];
2644 break;
2645 case 2: /* as v2i32 */
2646 case 3: /* as v3i32 */
2647 if (ac_has_vec3_support(ctx->screen->info.chip_class, false)) {
2648 vdata = ac_build_gather_values(&ctx->ac, out, num_comps);
2649 break;
2650 }
2651 /* as v4i32 (aligned to 4) */
2652 out[3] = LLVMGetUndef(ctx->i32);
2653 /* fall through */
2654 case 4: /* as v4i32 */
2655 vdata = ac_build_gather_values(&ctx->ac, out, util_next_power_of_two(num_comps));
2656 break;
2657 }
2658
2659 ac_build_buffer_store_dword(&ctx->ac, so_buffers[buf_idx],
2660 vdata, num_comps,
2661 so_write_offsets[buf_idx],
2662 ctx->i32_0,
2663 stream_out->dst_offset * 4, 1, 1, false);
2664 }
2665
2666 /**
2667 * Write streamout data to buffers for vertex stream @p stream (different
2668 * vertex streams can occur for GS copy shaders).
2669 */
2670 static void si_llvm_emit_streamout(struct si_shader_context *ctx,
2671 struct si_shader_output_values *outputs,
2672 unsigned noutput, unsigned stream)
2673 {
2674 struct si_shader_selector *sel = ctx->shader->selector;
2675 struct pipe_stream_output_info *so = &sel->so;
2676 LLVMBuilderRef builder = ctx->ac.builder;
2677 int i;
2678 struct lp_build_if_state if_ctx;
2679
2680 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2681 LLVMValueRef so_vtx_count =
2682 si_unpack_param(ctx, ctx->param_streamout_config, 16, 7);
2683
2684 LLVMValueRef tid = ac_get_thread_id(&ctx->ac);
2685
2686 /* can_emit = tid < so_vtx_count; */
2687 LLVMValueRef can_emit =
2688 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
2689
2690 /* Emit the streamout code conditionally. This actually avoids
2691 * out-of-bounds buffer access. The hw tells us via the SGPR
2692 * (so_vtx_count) which threads are allowed to emit streamout data. */
2693 lp_build_if(&if_ctx, &ctx->gallivm, can_emit);
2694 {
2695 /* The buffer offset is computed as follows:
2696 * ByteOffset = streamout_offset[buffer_id]*4 +
2697 * (streamout_write_index + thread_id)*stride[buffer_id] +
2698 * attrib_offset
2699 */
2700
2701 LLVMValueRef so_write_index =
2702 LLVMGetParam(ctx->main_fn,
2703 ctx->param_streamout_write_index);
2704
2705 /* Compute (streamout_write_index + thread_id). */
2706 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
2707
2708 /* Load the descriptor and compute the write offset for each
2709 * enabled buffer. */
2710 LLVMValueRef so_write_offset[4] = {};
2711 LLVMValueRef so_buffers[4];
2712 LLVMValueRef buf_ptr = LLVMGetParam(ctx->main_fn,
2713 ctx->param_rw_buffers);
2714
2715 for (i = 0; i < 4; i++) {
2716 if (!so->stride[i])
2717 continue;
2718
2719 LLVMValueRef offset = LLVMConstInt(ctx->i32,
2720 SI_VS_STREAMOUT_BUF0 + i, 0);
2721
2722 so_buffers[i] = ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
2723
2724 LLVMValueRef so_offset = LLVMGetParam(ctx->main_fn,
2725 ctx->param_streamout_offset[i]);
2726 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(ctx->i32, 4, 0), "");
2727
2728 so_write_offset[i] = ac_build_imad(&ctx->ac, so_write_index,
2729 LLVMConstInt(ctx->i32, so->stride[i]*4, 0),
2730 so_offset);
2731 }
2732
2733 /* Write streamout data. */
2734 for (i = 0; i < so->num_outputs; i++) {
2735 unsigned reg = so->output[i].register_index;
2736
2737 if (reg >= noutput)
2738 continue;
2739
2740 if (stream != so->output[i].stream)
2741 continue;
2742
2743 si_emit_streamout_output(ctx, so_buffers, so_write_offset,
2744 &so->output[i], &outputs[reg]);
2745 }
2746 }
2747 lp_build_endif(&if_ctx);
2748 }
2749
2750 static void si_export_param(struct si_shader_context *ctx, unsigned index,
2751 LLVMValueRef *values)
2752 {
2753 struct ac_export_args args;
2754
2755 si_llvm_init_export_args(ctx, values,
2756 V_008DFC_SQ_EXP_PARAM + index, &args);
2757 ac_build_export(&ctx->ac, &args);
2758 }
2759
2760 static void si_build_param_exports(struct si_shader_context *ctx,
2761 struct si_shader_output_values *outputs,
2762 unsigned noutput)
2763 {
2764 struct si_shader *shader = ctx->shader;
2765 unsigned param_count = 0;
2766
2767 for (unsigned i = 0; i < noutput; i++) {
2768 unsigned semantic_name = outputs[i].semantic_name;
2769 unsigned semantic_index = outputs[i].semantic_index;
2770
2771 if (outputs[i].vertex_stream[0] != 0 &&
2772 outputs[i].vertex_stream[1] != 0 &&
2773 outputs[i].vertex_stream[2] != 0 &&
2774 outputs[i].vertex_stream[3] != 0)
2775 continue;
2776
2777 switch (semantic_name) {
2778 case TGSI_SEMANTIC_LAYER:
2779 case TGSI_SEMANTIC_VIEWPORT_INDEX:
2780 case TGSI_SEMANTIC_CLIPDIST:
2781 case TGSI_SEMANTIC_COLOR:
2782 case TGSI_SEMANTIC_BCOLOR:
2783 case TGSI_SEMANTIC_PRIMID:
2784 case TGSI_SEMANTIC_FOG:
2785 case TGSI_SEMANTIC_TEXCOORD:
2786 case TGSI_SEMANTIC_GENERIC:
2787 break;
2788 default:
2789 continue;
2790 }
2791
2792 if ((semantic_name != TGSI_SEMANTIC_GENERIC ||
2793 semantic_index < SI_MAX_IO_GENERIC) &&
2794 shader->key.opt.kill_outputs &
2795 (1ull << si_shader_io_get_unique_index(semantic_name,
2796 semantic_index, true)))
2797 continue;
2798
2799 si_export_param(ctx, param_count, outputs[i].values);
2800
2801 assert(i < ARRAY_SIZE(shader->info.vs_output_param_offset));
2802 shader->info.vs_output_param_offset[i] = param_count++;
2803 }
2804
2805 shader->info.nr_param_exports = param_count;
2806 }
2807
2808 /**
2809 * Vertex color clamping.
2810 *
2811 * This uses a state constant loaded in a user data SGPR and
2812 * an IF statement is added that clamps all colors if the constant
2813 * is true.
2814 */
2815 static void si_vertex_color_clamping(struct si_shader_context *ctx,
2816 struct si_shader_output_values *outputs,
2817 unsigned noutput)
2818 {
2819 LLVMValueRef addr[SI_MAX_VS_OUTPUTS][4];
2820 bool has_colors = false;
2821
2822 /* Store original colors to alloca variables. */
2823 for (unsigned i = 0; i < noutput; i++) {
2824 if (outputs[i].semantic_name != TGSI_SEMANTIC_COLOR &&
2825 outputs[i].semantic_name != TGSI_SEMANTIC_BCOLOR)
2826 continue;
2827
2828 for (unsigned j = 0; j < 4; j++) {
2829 addr[i][j] = ac_build_alloca_undef(&ctx->ac, ctx->f32, "");
2830 LLVMBuildStore(ctx->ac.builder, outputs[i].values[j], addr[i][j]);
2831 }
2832 has_colors = true;
2833 }
2834
2835 if (!has_colors)
2836 return;
2837
2838 /* The state is in the first bit of the user SGPR. */
2839 LLVMValueRef cond = LLVMGetParam(ctx->main_fn, ctx->param_vs_state_bits);
2840 cond = LLVMBuildTrunc(ctx->ac.builder, cond, ctx->i1, "");
2841
2842 struct lp_build_if_state if_ctx;
2843 lp_build_if(&if_ctx, &ctx->gallivm, cond);
2844
2845 /* Store clamped colors to alloca variables within the conditional block. */
2846 for (unsigned i = 0; i < noutput; i++) {
2847 if (outputs[i].semantic_name != TGSI_SEMANTIC_COLOR &&
2848 outputs[i].semantic_name != TGSI_SEMANTIC_BCOLOR)
2849 continue;
2850
2851 for (unsigned j = 0; j < 4; j++) {
2852 LLVMBuildStore(ctx->ac.builder,
2853 ac_build_clamp(&ctx->ac, outputs[i].values[j]),
2854 addr[i][j]);
2855 }
2856 }
2857 lp_build_endif(&if_ctx);
2858
2859 /* Load clamped colors */
2860 for (unsigned i = 0; i < noutput; i++) {
2861 if (outputs[i].semantic_name != TGSI_SEMANTIC_COLOR &&
2862 outputs[i].semantic_name != TGSI_SEMANTIC_BCOLOR)
2863 continue;
2864
2865 for (unsigned j = 0; j < 4; j++) {
2866 outputs[i].values[j] =
2867 LLVMBuildLoad(ctx->ac.builder, addr[i][j], "");
2868 }
2869 }
2870 }
2871
2872 /* Generate export instructions for hardware VS shader stage or NGG GS stage
2873 * (position and parameter data only).
2874 */
2875 void si_llvm_export_vs(struct si_shader_context *ctx,
2876 struct si_shader_output_values *outputs,
2877 unsigned noutput)
2878 {
2879 struct si_shader *shader = ctx->shader;
2880 struct ac_export_args pos_args[4] = {};
2881 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL, viewport_index_value = NULL;
2882 unsigned pos_idx;
2883 int i;
2884
2885 si_vertex_color_clamping(ctx, outputs, noutput);
2886
2887 /* Build position exports. */
2888 for (i = 0; i < noutput; i++) {
2889 switch (outputs[i].semantic_name) {
2890 case TGSI_SEMANTIC_POSITION:
2891 si_llvm_init_export_args(ctx, outputs[i].values,
2892 V_008DFC_SQ_EXP_POS, &pos_args[0]);
2893 break;
2894 case TGSI_SEMANTIC_PSIZE:
2895 psize_value = outputs[i].values[0];
2896 break;
2897 case TGSI_SEMANTIC_LAYER:
2898 layer_value = outputs[i].values[0];
2899 break;
2900 case TGSI_SEMANTIC_VIEWPORT_INDEX:
2901 viewport_index_value = outputs[i].values[0];
2902 break;
2903 case TGSI_SEMANTIC_EDGEFLAG:
2904 edgeflag_value = outputs[i].values[0];
2905 break;
2906 case TGSI_SEMANTIC_CLIPDIST:
2907 if (!shader->key.opt.clip_disable) {
2908 unsigned index = 2 + outputs[i].semantic_index;
2909 si_llvm_init_export_args(ctx, outputs[i].values,
2910 V_008DFC_SQ_EXP_POS + index,
2911 &pos_args[index]);
2912 }
2913 break;
2914 case TGSI_SEMANTIC_CLIPVERTEX:
2915 if (!shader->key.opt.clip_disable) {
2916 si_llvm_emit_clipvertex(ctx, pos_args,
2917 outputs[i].values);
2918 }
2919 break;
2920 }
2921 }
2922
2923 /* We need to add the position output manually if it's missing. */
2924 if (!pos_args[0].out[0]) {
2925 pos_args[0].enabled_channels = 0xf; /* writemask */
2926 pos_args[0].valid_mask = 0; /* EXEC mask */
2927 pos_args[0].done = 0; /* last export? */
2928 pos_args[0].target = V_008DFC_SQ_EXP_POS;
2929 pos_args[0].compr = 0; /* COMPR flag */
2930 pos_args[0].out[0] = ctx->ac.f32_0; /* X */
2931 pos_args[0].out[1] = ctx->ac.f32_0; /* Y */
2932 pos_args[0].out[2] = ctx->ac.f32_0; /* Z */
2933 pos_args[0].out[3] = ctx->ac.f32_1; /* W */
2934 }
2935
2936 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2937 if (shader->selector->info.writes_psize ||
2938 shader->selector->info.writes_edgeflag ||
2939 shader->selector->info.writes_viewport_index ||
2940 shader->selector->info.writes_layer) {
2941 pos_args[1].enabled_channels = shader->selector->info.writes_psize |
2942 (shader->selector->info.writes_edgeflag << 1) |
2943 (shader->selector->info.writes_layer << 2);
2944
2945 pos_args[1].valid_mask = 0; /* EXEC mask */
2946 pos_args[1].done = 0; /* last export? */
2947 pos_args[1].target = V_008DFC_SQ_EXP_POS + 1;
2948 pos_args[1].compr = 0; /* COMPR flag */
2949 pos_args[1].out[0] = ctx->ac.f32_0; /* X */
2950 pos_args[1].out[1] = ctx->ac.f32_0; /* Y */
2951 pos_args[1].out[2] = ctx->ac.f32_0; /* Z */
2952 pos_args[1].out[3] = ctx->ac.f32_0; /* W */
2953
2954 if (shader->selector->info.writes_psize)
2955 pos_args[1].out[0] = psize_value;
2956
2957 if (shader->selector->info.writes_edgeflag) {
2958 /* The output is a float, but the hw expects an integer
2959 * with the first bit containing the edge flag. */
2960 edgeflag_value = LLVMBuildFPToUI(ctx->ac.builder,
2961 edgeflag_value,
2962 ctx->i32, "");
2963 edgeflag_value = ac_build_umin(&ctx->ac,
2964 edgeflag_value,
2965 ctx->i32_1);
2966
2967 /* The LLVM intrinsic expects a float. */
2968 pos_args[1].out[1] = ac_to_float(&ctx->ac, edgeflag_value);
2969 }
2970
2971 if (ctx->screen->info.chip_class >= GFX9) {
2972 /* GFX9 has the layer in out.z[10:0] and the viewport
2973 * index in out.z[19:16].
2974 */
2975 if (shader->selector->info.writes_layer)
2976 pos_args[1].out[2] = layer_value;
2977
2978 if (shader->selector->info.writes_viewport_index) {
2979 LLVMValueRef v = viewport_index_value;
2980
2981 v = ac_to_integer(&ctx->ac, v);
2982 v = LLVMBuildShl(ctx->ac.builder, v,
2983 LLVMConstInt(ctx->i32, 16, 0), "");
2984 v = LLVMBuildOr(ctx->ac.builder, v,
2985 ac_to_integer(&ctx->ac, pos_args[1].out[2]), "");
2986 pos_args[1].out[2] = ac_to_float(&ctx->ac, v);
2987 pos_args[1].enabled_channels |= 1 << 2;
2988 }
2989 } else {
2990 if (shader->selector->info.writes_layer)
2991 pos_args[1].out[2] = layer_value;
2992
2993 if (shader->selector->info.writes_viewport_index) {
2994 pos_args[1].out[3] = viewport_index_value;
2995 pos_args[1].enabled_channels |= 1 << 3;
2996 }
2997 }
2998 }
2999
3000 for (i = 0; i < 4; i++)
3001 if (pos_args[i].out[0])
3002 shader->info.nr_pos_exports++;
3003
3004 pos_idx = 0;
3005 for (i = 0; i < 4; i++) {
3006 if (!pos_args[i].out[0])
3007 continue;
3008
3009 /* Specify the target we are exporting */
3010 pos_args[i].target = V_008DFC_SQ_EXP_POS + pos_idx++;
3011
3012 if (pos_idx == shader->info.nr_pos_exports)
3013 /* Specify that this is the last export */
3014 pos_args[i].done = 1;
3015
3016 ac_build_export(&ctx->ac, &pos_args[i]);
3017 }
3018
3019 /* Build parameter exports. */
3020 si_build_param_exports(ctx, outputs, noutput);
3021 }
3022
3023 /**
3024 * Forward all outputs from the vertex shader to the TES. This is only used
3025 * for the fixed function TCS.
3026 */
3027 static void si_copy_tcs_inputs(struct lp_build_tgsi_context *bld_base)
3028 {
3029 struct si_shader_context *ctx = si_shader_context(bld_base);
3030 LLVMValueRef invocation_id, buffer, buffer_offset;
3031 LLVMValueRef lds_vertex_stride, lds_base;
3032 uint64_t inputs;
3033
3034 invocation_id = unpack_llvm_param(ctx, ctx->abi.tcs_rel_ids, 8, 5);
3035 buffer = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
3036 buffer_offset = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
3037
3038 lds_vertex_stride = get_tcs_in_vertex_dw_stride(ctx);
3039 lds_base = get_tcs_in_current_patch_offset(ctx);
3040 lds_base = ac_build_imad(&ctx->ac, invocation_id, lds_vertex_stride,
3041 lds_base);
3042
3043 inputs = ctx->shader->key.mono.u.ff_tcs_inputs_to_copy;
3044 while (inputs) {
3045 unsigned i = u_bit_scan64(&inputs);
3046
3047 LLVMValueRef lds_ptr = LLVMBuildAdd(ctx->ac.builder, lds_base,
3048 LLVMConstInt(ctx->i32, 4 * i, 0),
3049 "");
3050
3051 LLVMValueRef buffer_addr = get_tcs_tes_buffer_address(ctx,
3052 get_rel_patch_id(ctx),
3053 invocation_id,
3054 LLVMConstInt(ctx->i32, i, 0));
3055
3056 LLVMValueRef value = lshs_lds_load(bld_base, ctx->ac.i32, ~0, lds_ptr);
3057
3058 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, buffer_addr,
3059 buffer_offset, 0, 1, 0, false);
3060 }
3061 }
3062
3063 static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
3064 LLVMValueRef rel_patch_id,
3065 LLVMValueRef invocation_id,
3066 LLVMValueRef tcs_out_current_patch_data_offset,
3067 LLVMValueRef invoc0_tf_outer[4],
3068 LLVMValueRef invoc0_tf_inner[2])
3069 {
3070 struct si_shader_context *ctx = si_shader_context(bld_base);
3071 struct si_shader *shader = ctx->shader;
3072 unsigned tess_inner_index, tess_outer_index;
3073 LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
3074 LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
3075 unsigned stride, outer_comps, inner_comps, i, offset;
3076 struct lp_build_if_state if_ctx, inner_if_ctx;
3077
3078 /* Add a barrier before loading tess factors from LDS. */
3079 if (!shader->key.part.tcs.epilog.invoc0_tess_factors_are_def)
3080 si_llvm_emit_barrier(NULL, bld_base, NULL);
3081
3082 /* Do this only for invocation 0, because the tess levels are per-patch,
3083 * not per-vertex.
3084 *
3085 * This can't jump, because invocation 0 executes this. It should
3086 * at least mask out the loads and stores for other invocations.
3087 */
3088 lp_build_if(&if_ctx, &ctx->gallivm,
3089 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3090 invocation_id, ctx->i32_0, ""));
3091
3092 /* Determine the layout of one tess factor element in the buffer. */
3093 switch (shader->key.part.tcs.epilog.prim_mode) {
3094 case PIPE_PRIM_LINES:
3095 stride = 2; /* 2 dwords, 1 vec2 store */
3096 outer_comps = 2;
3097 inner_comps = 0;
3098 break;
3099 case PIPE_PRIM_TRIANGLES:
3100 stride = 4; /* 4 dwords, 1 vec4 store */
3101 outer_comps = 3;
3102 inner_comps = 1;
3103 break;
3104 case PIPE_PRIM_QUADS:
3105 stride = 6; /* 6 dwords, 2 stores (vec4 + vec2) */
3106 outer_comps = 4;
3107 inner_comps = 2;
3108 break;
3109 default:
3110 assert(0);
3111 return;
3112 }
3113
3114 for (i = 0; i < 4; i++) {
3115 inner[i] = LLVMGetUndef(ctx->i32);
3116 outer[i] = LLVMGetUndef(ctx->i32);
3117 }
3118
3119 if (shader->key.part.tcs.epilog.invoc0_tess_factors_are_def) {
3120 /* Tess factors are in VGPRs. */
3121 for (i = 0; i < outer_comps; i++)
3122 outer[i] = out[i] = invoc0_tf_outer[i];
3123 for (i = 0; i < inner_comps; i++)
3124 inner[i] = out[outer_comps+i] = invoc0_tf_inner[i];
3125 } else {
3126 /* Load tess_inner and tess_outer from LDS.
3127 * Any invocation can write them, so we can't get them from a temporary.
3128 */
3129 tess_inner_index = si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER, 0);
3130 tess_outer_index = si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER, 0);
3131
3132 lds_base = tcs_out_current_patch_data_offset;
3133 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_base,
3134 LLVMConstInt(ctx->i32,
3135 tess_inner_index * 4, 0), "");
3136 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_base,
3137 LLVMConstInt(ctx->i32,
3138 tess_outer_index * 4, 0), "");
3139
3140 for (i = 0; i < outer_comps; i++) {
3141 outer[i] = out[i] =
3142 lshs_lds_load(bld_base, ctx->ac.i32, i, lds_outer);
3143 }
3144 for (i = 0; i < inner_comps; i++) {
3145 inner[i] = out[outer_comps+i] =
3146 lshs_lds_load(bld_base, ctx->ac.i32, i, lds_inner);
3147 }
3148 }
3149
3150 if (shader->key.part.tcs.epilog.prim_mode == PIPE_PRIM_LINES) {
3151 /* For isolines, the hardware expects tess factors in the
3152 * reverse order from what GLSL / TGSI specify.
3153 */
3154 LLVMValueRef tmp = out[0];
3155 out[0] = out[1];
3156 out[1] = tmp;
3157 }
3158
3159 /* Convert the outputs to vectors for stores. */
3160 vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
3161 vec1 = NULL;
3162
3163 if (stride > 4)
3164 vec1 = ac_build_gather_values(&ctx->ac, out+4, stride - 4);
3165
3166 /* Get the buffer. */
3167 buffer = get_tess_ring_descriptor(ctx, TCS_FACTOR_RING);
3168
3169 /* Get the offset. */
3170 tf_base = LLVMGetParam(ctx->main_fn,
3171 ctx->param_tcs_factor_offset);
3172 byteoffset = LLVMBuildMul(ctx->ac.builder, rel_patch_id,
3173 LLVMConstInt(ctx->i32, 4 * stride, 0), "");
3174
3175 lp_build_if(&inner_if_ctx, &ctx->gallivm,
3176 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3177 rel_patch_id, ctx->i32_0, ""));
3178
3179 /* Store the dynamic HS control word. */
3180 offset = 0;
3181 if (ctx->screen->info.chip_class <= GFX8) {
3182 ac_build_buffer_store_dword(&ctx->ac, buffer,
3183 LLVMConstInt(ctx->i32, 0x80000000, 0),
3184 1, ctx->i32_0, tf_base,
3185 offset, 1, 0, false);
3186 offset += 4;
3187 }
3188
3189 lp_build_endif(&inner_if_ctx);
3190
3191 /* Store the tessellation factors. */
3192 ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
3193 MIN2(stride, 4), byteoffset, tf_base,
3194 offset, 1, 0, false);
3195 offset += 16;
3196 if (vec1)
3197 ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
3198 stride - 4, byteoffset, tf_base,
3199 offset, 1, 0, false);
3200
3201 /* Store the tess factors into the offchip buffer if TES reads them. */
3202 if (shader->key.part.tcs.epilog.tes_reads_tess_factors) {
3203 LLVMValueRef buf, base, inner_vec, outer_vec, tf_outer_offset;
3204 LLVMValueRef tf_inner_offset;
3205 unsigned param_outer, param_inner;
3206
3207 buf = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
3208 base = LLVMGetParam(ctx->main_fn, ctx->param_tcs_offchip_offset);
3209
3210 param_outer = si_shader_io_get_unique_index_patch(
3211 TGSI_SEMANTIC_TESSOUTER, 0);
3212 tf_outer_offset = get_tcs_tes_buffer_address(ctx, rel_patch_id, NULL,
3213 LLVMConstInt(ctx->i32, param_outer, 0));
3214
3215 unsigned outer_vec_size =
3216 ac_has_vec3_support(ctx->screen->info.chip_class, false) ?
3217 outer_comps : util_next_power_of_two(outer_comps);
3218 outer_vec = ac_build_gather_values(&ctx->ac, outer, outer_vec_size);
3219
3220 ac_build_buffer_store_dword(&ctx->ac, buf, outer_vec,
3221 outer_comps, tf_outer_offset,
3222 base, 0, 1, 0, false);
3223 if (inner_comps) {
3224 param_inner = si_shader_io_get_unique_index_patch(
3225 TGSI_SEMANTIC_TESSINNER, 0);
3226 tf_inner_offset = get_tcs_tes_buffer_address(ctx, rel_patch_id, NULL,
3227 LLVMConstInt(ctx->i32, param_inner, 0));
3228
3229 inner_vec = inner_comps == 1 ? inner[0] :
3230 ac_build_gather_values(&ctx->ac, inner, inner_comps);
3231 ac_build_buffer_store_dword(&ctx->ac, buf, inner_vec,
3232 inner_comps, tf_inner_offset,
3233 base, 0, 1, 0, false);
3234 }
3235 }
3236
3237 lp_build_endif(&if_ctx);
3238 }
3239
3240 static LLVMValueRef
3241 si_insert_input_ret(struct si_shader_context *ctx, LLVMValueRef ret,
3242 unsigned param, unsigned return_index)
3243 {
3244 return LLVMBuildInsertValue(ctx->ac.builder, ret,
3245 LLVMGetParam(ctx->main_fn, param),
3246 return_index, "");
3247 }
3248
3249 static LLVMValueRef
3250 si_insert_input_ret_float(struct si_shader_context *ctx, LLVMValueRef ret,
3251 unsigned param, unsigned return_index)
3252 {
3253 LLVMBuilderRef builder = ctx->ac.builder;
3254 LLVMValueRef p = LLVMGetParam(ctx->main_fn, param);
3255
3256 return LLVMBuildInsertValue(builder, ret,
3257 ac_to_float(&ctx->ac, p),
3258 return_index, "");
3259 }
3260
3261 static LLVMValueRef
3262 si_insert_input_ptr(struct si_shader_context *ctx, LLVMValueRef ret,
3263 unsigned param, unsigned return_index)
3264 {
3265 LLVMBuilderRef builder = ctx->ac.builder;
3266 LLVMValueRef ptr = LLVMGetParam(ctx->main_fn, param);
3267 ptr = LLVMBuildPtrToInt(builder, ptr, ctx->i32, "");
3268 return LLVMBuildInsertValue(builder, ret, ptr, return_index, "");
3269 }
3270
3271 /* This only writes the tessellation factor levels. */
3272 static void si_llvm_emit_tcs_epilogue(struct ac_shader_abi *abi,
3273 unsigned max_outputs,
3274 LLVMValueRef *addrs)
3275 {
3276 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3277 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
3278 LLVMBuilderRef builder = ctx->ac.builder;
3279 LLVMValueRef rel_patch_id, invocation_id, tf_lds_offset;
3280
3281 si_copy_tcs_inputs(bld_base);
3282
3283 rel_patch_id = get_rel_patch_id(ctx);
3284 invocation_id = unpack_llvm_param(ctx, ctx->abi.tcs_rel_ids, 8, 5);
3285 tf_lds_offset = get_tcs_out_current_patch_data_offset(ctx);
3286
3287 if (ctx->screen->info.chip_class >= GFX9) {
3288 LLVMBasicBlockRef blocks[2] = {
3289 LLVMGetInsertBlock(builder),
3290 ctx->merged_wrap_if_state.entry_block
3291 };
3292 LLVMValueRef values[2];
3293
3294 lp_build_endif(&ctx->merged_wrap_if_state);
3295
3296 values[0] = rel_patch_id;
3297 values[1] = LLVMGetUndef(ctx->i32);
3298 rel_patch_id = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
3299
3300 values[0] = tf_lds_offset;
3301 values[1] = LLVMGetUndef(ctx->i32);
3302 tf_lds_offset = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
3303
3304 values[0] = invocation_id;
3305 values[1] = ctx->i32_1; /* cause the epilog to skip threads */
3306 invocation_id = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
3307 }
3308
3309 /* Return epilog parameters from this function. */
3310 LLVMValueRef ret = ctx->return_value;
3311 unsigned vgpr;
3312
3313 if (ctx->screen->info.chip_class >= GFX9) {
3314 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_layout,
3315 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT);
3316 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_out_lds_layout,
3317 8 + GFX9_SGPR_TCS_OUT_LAYOUT);
3318 /* Tess offchip and tess factor offsets are at the beginning. */
3319 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_offset, 2);
3320 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_factor_offset, 4);
3321 vgpr = 8 + GFX9_SGPR_TCS_OUT_LAYOUT + 1;
3322 } else {
3323 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_layout,
3324 GFX6_SGPR_TCS_OFFCHIP_LAYOUT);
3325 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_out_lds_layout,
3326 GFX6_SGPR_TCS_OUT_LAYOUT);
3327 /* Tess offchip and tess factor offsets are after user SGPRs. */
3328 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_offset,
3329 GFX6_TCS_NUM_USER_SGPR);
3330 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_factor_offset,
3331 GFX6_TCS_NUM_USER_SGPR + 1);
3332 vgpr = GFX6_TCS_NUM_USER_SGPR + 2;
3333 }
3334
3335 /* VGPRs */
3336 rel_patch_id = ac_to_float(&ctx->ac, rel_patch_id);
3337 invocation_id = ac_to_float(&ctx->ac, invocation_id);
3338 tf_lds_offset = ac_to_float(&ctx->ac, tf_lds_offset);
3339
3340 /* Leave a hole corresponding to the two input VGPRs. This ensures that
3341 * the invocation_id output does not alias the tcs_rel_ids input,
3342 * which saves a V_MOV on gfx9.
3343 */
3344 vgpr += 2;
3345
3346 ret = LLVMBuildInsertValue(builder, ret, rel_patch_id, vgpr++, "");
3347 ret = LLVMBuildInsertValue(builder, ret, invocation_id, vgpr++, "");
3348
3349 if (ctx->shader->selector->tcs_info.tessfactors_are_def_in_all_invocs) {
3350 vgpr++; /* skip the tess factor LDS offset */
3351 for (unsigned i = 0; i < 6; i++) {
3352 LLVMValueRef value =
3353 LLVMBuildLoad(builder, ctx->invoc0_tess_factors[i], "");
3354 value = ac_to_float(&ctx->ac, value);
3355 ret = LLVMBuildInsertValue(builder, ret, value, vgpr++, "");
3356 }
3357 } else {
3358 ret = LLVMBuildInsertValue(builder, ret, tf_lds_offset, vgpr++, "");
3359 }
3360 ctx->return_value = ret;
3361 }
3362
3363 /* Pass TCS inputs from LS to TCS on GFX9. */
3364 static void si_set_ls_return_value_for_tcs(struct si_shader_context *ctx)
3365 {
3366 LLVMValueRef ret = ctx->return_value;
3367
3368 ret = si_insert_input_ptr(ctx, ret, 0, 0);
3369 ret = si_insert_input_ptr(ctx, ret, 1, 1);
3370 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_offset, 2);
3371 ret = si_insert_input_ret(ctx, ret, ctx->param_merged_wave_info, 3);
3372 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_factor_offset, 4);
3373 ret = si_insert_input_ret(ctx, ret, ctx->param_merged_scratch_offset, 5);
3374
3375 ret = si_insert_input_ptr(ctx, ret, ctx->param_rw_buffers,
3376 8 + SI_SGPR_RW_BUFFERS);
3377 ret = si_insert_input_ptr(ctx, ret,
3378 ctx->param_bindless_samplers_and_images,
3379 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES);
3380
3381 ret = si_insert_input_ret(ctx, ret, ctx->param_vs_state_bits,
3382 8 + SI_SGPR_VS_STATE_BITS);
3383
3384 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_offchip_layout,
3385 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT);
3386 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_out_lds_offsets,
3387 8 + GFX9_SGPR_TCS_OUT_OFFSETS);
3388 ret = si_insert_input_ret(ctx, ret, ctx->param_tcs_out_lds_layout,
3389 8 + GFX9_SGPR_TCS_OUT_LAYOUT);
3390
3391 unsigned vgpr = 8 + GFX9_TCS_NUM_USER_SGPR;
3392 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
3393 ac_to_float(&ctx->ac, ctx->abi.tcs_patch_id),
3394 vgpr++, "");
3395 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
3396 ac_to_float(&ctx->ac, ctx->abi.tcs_rel_ids),
3397 vgpr++, "");
3398 ctx->return_value = ret;
3399 }
3400
3401 /* Pass GS inputs from ES to GS on GFX9. */
3402 static void si_set_es_return_value_for_gs(struct si_shader_context *ctx)
3403 {
3404 LLVMBuilderRef builder = ctx->ac.builder;
3405 LLVMValueRef ret = ctx->return_value;
3406
3407 ret = si_insert_input_ptr(ctx, ret, 0, 0);
3408 ret = si_insert_input_ptr(ctx, ret, 1, 1);
3409 if (ctx->shader->key.as_ngg)
3410 ret = LLVMBuildInsertValue(builder, ret, ctx->gs_tg_info, 2, "");
3411 else
3412 ret = si_insert_input_ret(ctx, ret, ctx->param_gs2vs_offset, 2);
3413 ret = si_insert_input_ret(ctx, ret, ctx->param_merged_wave_info, 3);
3414 ret = si_insert_input_ret(ctx, ret, ctx->param_merged_scratch_offset, 5);
3415
3416 ret = si_insert_input_ptr(ctx, ret, ctx->param_rw_buffers,
3417 8 + SI_SGPR_RW_BUFFERS);
3418 ret = si_insert_input_ptr(ctx, ret,
3419 ctx->param_bindless_samplers_and_images,
3420 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES);
3421
3422 unsigned vgpr;
3423 if (ctx->type == PIPE_SHADER_VERTEX)
3424 vgpr = 8 + GFX9_VSGS_NUM_USER_SGPR;
3425 else
3426 vgpr = 8 + GFX9_TESGS_NUM_USER_SGPR;
3427
3428 for (unsigned i = 0; i < 5; i++) {
3429 unsigned param = ctx->param_gs_vtx01_offset + i;
3430 ret = si_insert_input_ret_float(ctx, ret, param, vgpr++);
3431 }
3432 ctx->return_value = ret;
3433 }
3434
3435 static void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi,
3436 unsigned max_outputs,
3437 LLVMValueRef *addrs)
3438 {
3439 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3440 struct si_shader *shader = ctx->shader;
3441 struct tgsi_shader_info *info = &shader->selector->info;
3442 unsigned i, chan;
3443 LLVMValueRef vertex_id = LLVMGetParam(ctx->main_fn,
3444 ctx->param_rel_auto_id);
3445 LLVMValueRef vertex_dw_stride = get_tcs_in_vertex_dw_stride(ctx);
3446 LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->ac.builder, vertex_id,
3447 vertex_dw_stride, "");
3448
3449 /* Write outputs to LDS. The next shader (TCS aka HS) will read
3450 * its inputs from it. */
3451 for (i = 0; i < info->num_outputs; i++) {
3452 unsigned name = info->output_semantic_name[i];
3453 unsigned index = info->output_semantic_index[i];
3454
3455 /* The ARB_shader_viewport_layer_array spec contains the
3456 * following issue:
3457 *
3458 * 2) What happens if gl_ViewportIndex or gl_Layer is
3459 * written in the vertex shader and a geometry shader is
3460 * present?
3461 *
3462 * RESOLVED: The value written by the last vertex processing
3463 * stage is used. If the last vertex processing stage
3464 * (vertex, tessellation evaluation or geometry) does not
3465 * statically assign to gl_ViewportIndex or gl_Layer, index
3466 * or layer zero is assumed.
3467 *
3468 * So writes to those outputs in VS-as-LS are simply ignored.
3469 */
3470 if (name == TGSI_SEMANTIC_LAYER ||
3471 name == TGSI_SEMANTIC_VIEWPORT_INDEX)
3472 continue;
3473
3474 int param = si_shader_io_get_unique_index(name, index, false);
3475 LLVMValueRef dw_addr = LLVMBuildAdd(ctx->ac.builder, base_dw_addr,
3476 LLVMConstInt(ctx->i32, param * 4, 0), "");
3477
3478 for (chan = 0; chan < 4; chan++) {
3479 if (!(info->output_usagemask[i] & (1 << chan)))
3480 continue;
3481
3482 lshs_lds_store(ctx, chan, dw_addr,
3483 LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], ""));
3484 }
3485 }
3486
3487 if (ctx->screen->info.chip_class >= GFX9)
3488 si_set_ls_return_value_for_tcs(ctx);
3489 }
3490
3491 static void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi,
3492 unsigned max_outputs,
3493 LLVMValueRef *addrs)
3494 {
3495 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3496 struct si_shader *es = ctx->shader;
3497 struct tgsi_shader_info *info = &es->selector->info;
3498 LLVMValueRef soffset = LLVMGetParam(ctx->main_fn,
3499 ctx->param_es2gs_offset);
3500 LLVMValueRef lds_base = NULL;
3501 unsigned chan;
3502 int i;
3503
3504 if (ctx->screen->info.chip_class >= GFX9 && info->num_outputs) {
3505 unsigned itemsize_dw = es->selector->esgs_itemsize / 4;
3506 LLVMValueRef vertex_idx = ac_get_thread_id(&ctx->ac);
3507 LLVMValueRef wave_idx = si_unpack_param(ctx, ctx->param_merged_wave_info, 24, 4);
3508 vertex_idx = LLVMBuildOr(ctx->ac.builder, vertex_idx,
3509 LLVMBuildMul(ctx->ac.builder, wave_idx,
3510 LLVMConstInt(ctx->i32, 64, false), ""), "");
3511 lds_base = LLVMBuildMul(ctx->ac.builder, vertex_idx,
3512 LLVMConstInt(ctx->i32, itemsize_dw, 0), "");
3513 }
3514
3515 for (i = 0; i < info->num_outputs; i++) {
3516 int param;
3517
3518 if (info->output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX ||
3519 info->output_semantic_name[i] == TGSI_SEMANTIC_LAYER)
3520 continue;
3521
3522 param = si_shader_io_get_unique_index(info->output_semantic_name[i],
3523 info->output_semantic_index[i], false);
3524
3525 for (chan = 0; chan < 4; chan++) {
3526 if (!(info->output_usagemask[i] & (1 << chan)))
3527 continue;
3528
3529 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], "");
3530 out_val = ac_to_integer(&ctx->ac, out_val);
3531
3532 /* GFX9 has the ESGS ring in LDS. */
3533 if (ctx->screen->info.chip_class >= GFX9) {
3534 LLVMValueRef idx = LLVMConstInt(ctx->i32, param * 4 + chan, false);
3535 idx = LLVMBuildAdd(ctx->ac.builder, lds_base, idx, "");
3536 ac_build_indexed_store(&ctx->ac, ctx->esgs_ring, idx, out_val);
3537 continue;
3538 }
3539
3540 ac_build_buffer_store_dword(&ctx->ac,
3541 ctx->esgs_ring,
3542 out_val, 1, NULL, soffset,
3543 (4 * param + chan) * 4,
3544 1, 1, true);
3545 }
3546 }
3547
3548 if (ctx->screen->info.chip_class >= GFX9)
3549 si_set_es_return_value_for_gs(ctx);
3550 }
3551
3552 static LLVMValueRef si_get_gs_wave_id(struct si_shader_context *ctx)
3553 {
3554 if (ctx->screen->info.chip_class >= GFX9)
3555 return si_unpack_param(ctx, ctx->param_merged_wave_info, 16, 8);
3556 else
3557 return LLVMGetParam(ctx->main_fn, ctx->param_gs_wave_id);
3558 }
3559
3560 static void emit_gs_epilogue(struct si_shader_context *ctx)
3561 {
3562 if (ctx->shader->key.as_ngg) {
3563 gfx10_ngg_gs_emit_epilogue(ctx);
3564 return;
3565 }
3566
3567 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_NOP | AC_SENDMSG_GS_DONE,
3568 si_get_gs_wave_id(ctx));
3569
3570 if (ctx->screen->info.chip_class >= GFX9)
3571 lp_build_endif(&ctx->merged_wrap_if_state);
3572 }
3573
3574 static void si_llvm_emit_gs_epilogue(struct ac_shader_abi *abi,
3575 unsigned max_outputs,
3576 LLVMValueRef *addrs)
3577 {
3578 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3579 struct tgsi_shader_info UNUSED *info = &ctx->shader->selector->info;
3580
3581 assert(info->num_outputs <= max_outputs);
3582
3583 emit_gs_epilogue(ctx);
3584 }
3585
3586 static void si_tgsi_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
3587 {
3588 struct si_shader_context *ctx = si_shader_context(bld_base);
3589 emit_gs_epilogue(ctx);
3590 }
3591
3592 static void si_llvm_emit_vs_epilogue(struct ac_shader_abi *abi,
3593 unsigned max_outputs,
3594 LLVMValueRef *addrs)
3595 {
3596 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3597 struct tgsi_shader_info *info = &ctx->shader->selector->info;
3598 struct si_shader_output_values *outputs = NULL;
3599 int i,j;
3600
3601 assert(!ctx->shader->is_gs_copy_shader);
3602 assert(info->num_outputs <= max_outputs);
3603
3604 outputs = MALLOC((info->num_outputs + 1) * sizeof(outputs[0]));
3605
3606 for (i = 0; i < info->num_outputs; i++) {
3607 outputs[i].semantic_name = info->output_semantic_name[i];
3608 outputs[i].semantic_index = info->output_semantic_index[i];
3609
3610 for (j = 0; j < 4; j++) {
3611 outputs[i].values[j] =
3612 LLVMBuildLoad(ctx->ac.builder,
3613 addrs[4 * i + j],
3614 "");
3615 outputs[i].vertex_stream[j] =
3616 (info->output_streams[i] >> (2 * j)) & 3;
3617 }
3618 }
3619
3620 if (ctx->shader->selector->so.num_outputs)
3621 si_llvm_emit_streamout(ctx, outputs, i, 0);
3622
3623 /* Export PrimitiveID. */
3624 if (ctx->shader->key.mono.u.vs_export_prim_id) {
3625 outputs[i].semantic_name = TGSI_SEMANTIC_PRIMID;
3626 outputs[i].semantic_index = 0;
3627 outputs[i].values[0] = ac_to_float(&ctx->ac, si_get_primitive_id(ctx, 0));
3628 for (j = 1; j < 4; j++)
3629 outputs[i].values[j] = LLVMConstReal(ctx->f32, 0);
3630
3631 memset(outputs[i].vertex_stream, 0,
3632 sizeof(outputs[i].vertex_stream));
3633 i++;
3634 }
3635
3636 si_llvm_export_vs(ctx, outputs, i);
3637 FREE(outputs);
3638 }
3639
3640 static void si_llvm_emit_prim_discard_cs_epilogue(struct ac_shader_abi *abi,
3641 unsigned max_outputs,
3642 LLVMValueRef *addrs)
3643 {
3644 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3645 struct tgsi_shader_info *info = &ctx->shader->selector->info;
3646 LLVMValueRef pos[4] = {};
3647
3648 assert(info->num_outputs <= max_outputs);
3649
3650 for (unsigned i = 0; i < info->num_outputs; i++) {
3651 if (info->output_semantic_name[i] != TGSI_SEMANTIC_POSITION)
3652 continue;
3653
3654 for (unsigned chan = 0; chan < 4; chan++)
3655 pos[chan] = LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], "");
3656 break;
3657 }
3658 assert(pos[0] != NULL);
3659
3660 /* Return the position output. */
3661 LLVMValueRef ret = ctx->return_value;
3662 for (unsigned chan = 0; chan < 4; chan++)
3663 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, pos[chan], chan, "");
3664 ctx->return_value = ret;
3665 }
3666
3667 static void si_tgsi_emit_epilogue(struct lp_build_tgsi_context *bld_base)
3668 {
3669 struct si_shader_context *ctx = si_shader_context(bld_base);
3670
3671 ctx->abi.emit_outputs(&ctx->abi, RADEON_LLVM_MAX_OUTPUTS,
3672 &ctx->outputs[0][0]);
3673 }
3674
3675 struct si_ps_exports {
3676 unsigned num;
3677 struct ac_export_args args[10];
3678 };
3679
3680 static void si_export_mrt_z(struct lp_build_tgsi_context *bld_base,
3681 LLVMValueRef depth, LLVMValueRef stencil,
3682 LLVMValueRef samplemask, struct si_ps_exports *exp)
3683 {
3684 struct si_shader_context *ctx = si_shader_context(bld_base);
3685 struct ac_export_args args;
3686
3687 ac_export_mrt_z(&ctx->ac, depth, stencil, samplemask, &args);
3688
3689 memcpy(&exp->args[exp->num++], &args, sizeof(args));
3690 }
3691
3692 static void si_export_mrt_color(struct lp_build_tgsi_context *bld_base,
3693 LLVMValueRef *color, unsigned index,
3694 unsigned samplemask_param,
3695 bool is_last, struct si_ps_exports *exp)
3696 {
3697 struct si_shader_context *ctx = si_shader_context(bld_base);
3698 int i;
3699
3700 /* Clamp color */
3701 if (ctx->shader->key.part.ps.epilog.clamp_color)
3702 for (i = 0; i < 4; i++)
3703 color[i] = ac_build_clamp(&ctx->ac, color[i]);
3704
3705 /* Alpha to one */
3706 if (ctx->shader->key.part.ps.epilog.alpha_to_one)
3707 color[3] = ctx->ac.f32_1;
3708
3709 /* Alpha test */
3710 if (index == 0 &&
3711 ctx->shader->key.part.ps.epilog.alpha_func != PIPE_FUNC_ALWAYS)
3712 si_alpha_test(bld_base, color[3]);
3713
3714 /* Line & polygon smoothing */
3715 if (ctx->shader->key.part.ps.epilog.poly_line_smoothing)
3716 color[3] = si_scale_alpha_by_sample_mask(bld_base, color[3],
3717 samplemask_param);
3718
3719 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3720 if (ctx->shader->key.part.ps.epilog.last_cbuf > 0) {
3721 struct ac_export_args args[8];
3722 int c, last = -1;
3723
3724 /* Get the export arguments, also find out what the last one is. */
3725 for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {
3726 si_llvm_init_export_args(ctx, color,
3727 V_008DFC_SQ_EXP_MRT + c, &args[c]);
3728 if (args[c].enabled_channels)
3729 last = c;
3730 }
3731
3732 /* Emit all exports. */
3733 for (c = 0; c <= ctx->shader->key.part.ps.epilog.last_cbuf; c++) {
3734 if (is_last && last == c) {
3735 args[c].valid_mask = 1; /* whether the EXEC mask is valid */
3736 args[c].done = 1; /* DONE bit */
3737 } else if (!args[c].enabled_channels)
3738 continue; /* unnecessary NULL export */
3739
3740 memcpy(&exp->args[exp->num++], &args[c], sizeof(args[c]));
3741 }
3742 } else {
3743 struct ac_export_args args;
3744
3745 /* Export */
3746 si_llvm_init_export_args(ctx, color, V_008DFC_SQ_EXP_MRT + index,
3747 &args);
3748 if (is_last) {
3749 args.valid_mask = 1; /* whether the EXEC mask is valid */
3750 args.done = 1; /* DONE bit */
3751 } else if (!args.enabled_channels)
3752 return; /* unnecessary NULL export */
3753
3754 memcpy(&exp->args[exp->num++], &args, sizeof(args));
3755 }
3756 }
3757
3758 static void si_emit_ps_exports(struct si_shader_context *ctx,
3759 struct si_ps_exports *exp)
3760 {
3761 for (unsigned i = 0; i < exp->num; i++)
3762 ac_build_export(&ctx->ac, &exp->args[i]);
3763 }
3764
3765 /**
3766 * Return PS outputs in this order:
3767 *
3768 * v[0:3] = color0.xyzw
3769 * v[4:7] = color1.xyzw
3770 * ...
3771 * vN+0 = Depth
3772 * vN+1 = Stencil
3773 * vN+2 = SampleMask
3774 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3775 *
3776 * The alpha-ref SGPR is returned via its original location.
3777 */
3778 static void si_llvm_return_fs_outputs(struct ac_shader_abi *abi,
3779 unsigned max_outputs,
3780 LLVMValueRef *addrs)
3781 {
3782 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
3783 struct si_shader *shader = ctx->shader;
3784 struct tgsi_shader_info *info = &shader->selector->info;
3785 LLVMBuilderRef builder = ctx->ac.builder;
3786 unsigned i, j, first_vgpr, vgpr;
3787
3788 LLVMValueRef color[8][4] = {};
3789 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
3790 LLVMValueRef ret;
3791
3792 if (ctx->postponed_kill)
3793 ac_build_kill_if_false(&ctx->ac, LLVMBuildLoad(builder, ctx->postponed_kill, ""));
3794
3795 /* Read the output values. */
3796 for (i = 0; i < info->num_outputs; i++) {
3797 unsigned semantic_name = info->output_semantic_name[i];
3798 unsigned semantic_index = info->output_semantic_index[i];
3799
3800 switch (semantic_name) {
3801 case TGSI_SEMANTIC_COLOR:
3802 assert(semantic_index < 8);
3803 for (j = 0; j < 4; j++) {
3804 LLVMValueRef ptr = addrs[4 * i + j];
3805 LLVMValueRef result = LLVMBuildLoad(builder, ptr, "");
3806 color[semantic_index][j] = result;
3807 }
3808 break;
3809 case TGSI_SEMANTIC_POSITION:
3810 depth = LLVMBuildLoad(builder,
3811 addrs[4 * i + 2], "");
3812 break;
3813 case TGSI_SEMANTIC_STENCIL:
3814 stencil = LLVMBuildLoad(builder,
3815 addrs[4 * i + 1], "");
3816 break;
3817 case TGSI_SEMANTIC_SAMPLEMASK:
3818 samplemask = LLVMBuildLoad(builder,
3819 addrs[4 * i + 0], "");
3820 break;
3821 default:
3822 fprintf(stderr, "Warning: GFX6 unhandled fs output type:%d\n",
3823 semantic_name);
3824 }
3825 }
3826
3827 /* Fill the return structure. */
3828 ret = ctx->return_value;
3829
3830 /* Set SGPRs. */
3831 ret = LLVMBuildInsertValue(builder, ret,
3832 ac_to_integer(&ctx->ac,
3833 LLVMGetParam(ctx->main_fn,
3834 SI_PARAM_ALPHA_REF)),
3835 SI_SGPR_ALPHA_REF, "");
3836
3837 /* Set VGPRs */
3838 first_vgpr = vgpr = SI_SGPR_ALPHA_REF + 1;
3839 for (i = 0; i < ARRAY_SIZE(color); i++) {
3840 if (!color[i][0])
3841 continue;
3842
3843 for (j = 0; j < 4; j++)
3844 ret = LLVMBuildInsertValue(builder, ret, color[i][j], vgpr++, "");
3845 }
3846 if (depth)
3847 ret = LLVMBuildInsertValue(builder, ret, depth, vgpr++, "");
3848 if (stencil)
3849 ret = LLVMBuildInsertValue(builder, ret, stencil, vgpr++, "");
3850 if (samplemask)
3851 ret = LLVMBuildInsertValue(builder, ret, samplemask, vgpr++, "");
3852
3853 /* Add the input sample mask for smoothing at the end. */
3854 if (vgpr < first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC)
3855 vgpr = first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC;
3856 ret = LLVMBuildInsertValue(builder, ret,
3857 LLVMGetParam(ctx->main_fn,
3858 SI_PARAM_SAMPLE_COVERAGE), vgpr++, "");
3859
3860 ctx->return_value = ret;
3861 }
3862
3863 static void membar_emit(
3864 const struct lp_build_tgsi_action *action,
3865 struct lp_build_tgsi_context *bld_base,
3866 struct lp_build_emit_data *emit_data)
3867 {
3868 struct si_shader_context *ctx = si_shader_context(bld_base);
3869 LLVMValueRef src0 = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
3870 unsigned flags = LLVMConstIntGetZExtValue(src0);
3871 unsigned waitcnt = NOOP_WAITCNT;
3872
3873 if (flags & TGSI_MEMBAR_THREAD_GROUP)
3874 waitcnt &= VM_CNT & LGKM_CNT;
3875
3876 if (flags & (TGSI_MEMBAR_ATOMIC_BUFFER |
3877 TGSI_MEMBAR_SHADER_BUFFER |
3878 TGSI_MEMBAR_SHADER_IMAGE))
3879 waitcnt &= VM_CNT;
3880
3881 if (flags & TGSI_MEMBAR_SHARED)
3882 waitcnt &= LGKM_CNT;
3883
3884 if (waitcnt != NOOP_WAITCNT)
3885 ac_build_waitcnt(&ctx->ac, waitcnt);
3886 }
3887
3888 static void clock_emit(
3889 const struct lp_build_tgsi_action *action,
3890 struct lp_build_tgsi_context *bld_base,
3891 struct lp_build_emit_data *emit_data)
3892 {
3893 struct si_shader_context *ctx = si_shader_context(bld_base);
3894 LLVMValueRef tmp = ac_build_shader_clock(&ctx->ac);
3895
3896 emit_data->output[0] =
3897 LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->i32_0, "");
3898 emit_data->output[1] =
3899 LLVMBuildExtractElement(ctx->ac.builder, tmp, ctx->i32_1, "");
3900 }
3901
3902 static void si_llvm_emit_ddxy(
3903 const struct lp_build_tgsi_action *action,
3904 struct lp_build_tgsi_context *bld_base,
3905 struct lp_build_emit_data *emit_data)
3906 {
3907 struct si_shader_context *ctx = si_shader_context(bld_base);
3908 unsigned opcode = emit_data->info->opcode;
3909 LLVMValueRef val;
3910 int idx;
3911 unsigned mask;
3912
3913 if (opcode == TGSI_OPCODE_DDX_FINE)
3914 mask = AC_TID_MASK_LEFT;
3915 else if (opcode == TGSI_OPCODE_DDY_FINE)
3916 mask = AC_TID_MASK_TOP;
3917 else
3918 mask = AC_TID_MASK_TOP_LEFT;
3919
3920 /* for DDX we want to next X pixel, DDY next Y pixel. */
3921 idx = (opcode == TGSI_OPCODE_DDX || opcode == TGSI_OPCODE_DDX_FINE) ? 1 : 2;
3922
3923 val = ac_to_integer(&ctx->ac, emit_data->args[0]);
3924 val = ac_build_ddxy(&ctx->ac, mask, idx, val);
3925 emit_data->output[emit_data->chan] = val;
3926 }
3927
3928 static void build_interp_intrinsic(const struct lp_build_tgsi_action *action,
3929 struct lp_build_tgsi_context *bld_base,
3930 struct lp_build_emit_data *emit_data)
3931 {
3932 struct si_shader_context *ctx = si_shader_context(bld_base);
3933 struct si_shader *shader = ctx->shader;
3934 const struct tgsi_shader_info *info = &shader->selector->info;
3935 LLVMValueRef interp_param;
3936 const struct tgsi_full_instruction *inst = emit_data->inst;
3937 const struct tgsi_full_src_register *input = &inst->Src[0];
3938 int input_base, input_array_size;
3939 int chan;
3940 int i;
3941 LLVMValueRef prim_mask = ctx->abi.prim_mask;
3942 LLVMValueRef array_idx, offset_x = NULL, offset_y = NULL;
3943 int interp_param_idx;
3944 unsigned interp;
3945 unsigned location;
3946
3947 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
3948 /* offset is in second src, first two channels */
3949 offset_x = lp_build_emit_fetch(bld_base, emit_data->inst, 1,
3950 TGSI_CHAN_X);
3951 offset_y = lp_build_emit_fetch(bld_base, emit_data->inst, 1,
3952 TGSI_CHAN_Y);
3953 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
3954 LLVMValueRef sample_position;
3955 LLVMValueRef sample_id;
3956 LLVMValueRef halfval = LLVMConstReal(ctx->f32, 0.5f);
3957
3958 /* fetch sample ID, then fetch its sample position,
3959 * and place into first two channels.
3960 */
3961 sample_id = lp_build_emit_fetch(bld_base,
3962 emit_data->inst, 1, TGSI_CHAN_X);
3963 sample_id = ac_to_integer(&ctx->ac, sample_id);
3964
3965 /* Section 8.13.2 (Interpolation Functions) of the OpenGL Shading
3966 * Language 4.50 spec says about interpolateAtSample:
3967 *
3968 * "Returns the value of the input interpolant variable at
3969 * the location of sample number sample. If multisample
3970 * buffers are not available, the input variable will be
3971 * evaluated at the center of the pixel. If sample sample
3972 * does not exist, the position used to interpolate the
3973 * input variable is undefined."
3974 *
3975 * This means that sample_id values outside of the valid are
3976 * in fact valid input, and the usual mechanism for loading the
3977 * sample position doesn't work.
3978 */
3979 if (ctx->shader->key.mono.u.ps.interpolate_at_sample_force_center) {
3980 LLVMValueRef center[4] = {
3981 LLVMConstReal(ctx->f32, 0.5),
3982 LLVMConstReal(ctx->f32, 0.5),
3983 ctx->ac.f32_0,
3984 ctx->ac.f32_0,
3985 };
3986
3987 sample_position = ac_build_gather_values(&ctx->ac, center, 4);
3988 } else {
3989 sample_position = load_sample_position(&ctx->abi, sample_id);
3990 }
3991
3992 offset_x = LLVMBuildExtractElement(ctx->ac.builder, sample_position,
3993 ctx->i32_0, "");
3994
3995 offset_x = LLVMBuildFSub(ctx->ac.builder, offset_x, halfval, "");
3996 offset_y = LLVMBuildExtractElement(ctx->ac.builder, sample_position,
3997 ctx->i32_1, "");
3998 offset_y = LLVMBuildFSub(ctx->ac.builder, offset_y, halfval, "");
3999 }
4000
4001 assert(input->Register.File == TGSI_FILE_INPUT);
4002
4003 if (input->Register.Indirect) {
4004 unsigned array_id = input->Indirect.ArrayID;
4005
4006 if (array_id) {
4007 input_base = info->input_array_first[array_id];
4008 input_array_size = info->input_array_last[array_id] - input_base + 1;
4009 } else {
4010 input_base = inst->Src[0].Register.Index;
4011 input_array_size = info->num_inputs - input_base;
4012 }
4013
4014 array_idx = si_get_indirect_index(ctx, &input->Indirect,
4015 1, input->Register.Index - input_base);
4016 } else {
4017 input_base = inst->Src[0].Register.Index;
4018 input_array_size = 1;
4019 array_idx = ctx->i32_0;
4020 }
4021
4022 interp = shader->selector->info.input_interpolate[input_base];
4023
4024 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
4025 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE)
4026 location = TGSI_INTERPOLATE_LOC_CENTER;
4027 else
4028 location = TGSI_INTERPOLATE_LOC_CENTROID;
4029
4030 interp_param_idx = lookup_interp_param_index(interp, location);
4031 if (interp_param_idx == -1)
4032 return;
4033 else if (interp_param_idx)
4034 interp_param = LLVMGetParam(ctx->main_fn, interp_param_idx);
4035 else
4036 interp_param = NULL;
4037
4038 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
4039 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
4040 LLVMValueRef ij_out[2];
4041 LLVMValueRef ddxy_out = ac_build_ddxy_interp(&ctx->ac, interp_param);
4042
4043 /*
4044 * take the I then J parameters, and the DDX/Y for it, and
4045 * calculate the IJ inputs for the interpolator.
4046 * temp1 = ddx * offset/sample.x + I;
4047 * interp_param.I = ddy * offset/sample.y + temp1;
4048 * temp1 = ddx * offset/sample.x + J;
4049 * interp_param.J = ddy * offset/sample.y + temp1;
4050 */
4051 for (i = 0; i < 2; i++) {
4052 LLVMValueRef ix_ll = LLVMConstInt(ctx->i32, i, 0);
4053 LLVMValueRef iy_ll = LLVMConstInt(ctx->i32, i + 2, 0);
4054 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder,
4055 ddxy_out, ix_ll, "");
4056 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder,
4057 ddxy_out, iy_ll, "");
4058 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder,
4059 interp_param, ix_ll, "");
4060 LLVMValueRef temp;
4061
4062 interp_el = ac_to_float(&ctx->ac, interp_el);
4063
4064 temp = ac_build_fmad(&ctx->ac, ddx_el, offset_x, interp_el);
4065 ij_out[i] = ac_build_fmad(&ctx->ac, ddy_el, offset_y, temp);
4066 }
4067 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
4068 }
4069
4070 if (interp_param)
4071 interp_param = ac_to_float(&ctx->ac, interp_param);
4072
4073 for (chan = 0; chan < 4; chan++) {
4074 LLVMValueRef gather = LLVMGetUndef(LLVMVectorType(ctx->f32, input_array_size));
4075 unsigned schan = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], chan);
4076
4077 for (unsigned idx = 0; idx < input_array_size; ++idx) {
4078 LLVMValueRef v, i = NULL, j = NULL;
4079
4080 if (interp_param) {
4081 i = LLVMBuildExtractElement(
4082 ctx->ac.builder, interp_param, ctx->i32_0, "");
4083 j = LLVMBuildExtractElement(
4084 ctx->ac.builder, interp_param, ctx->i32_1, "");
4085 }
4086 v = si_build_fs_interp(ctx, input_base + idx, schan,
4087 prim_mask, i, j);
4088
4089 gather = LLVMBuildInsertElement(ctx->ac.builder,
4090 gather, v, LLVMConstInt(ctx->i32, idx, false), "");
4091 }
4092
4093 emit_data->output[chan] = LLVMBuildExtractElement(
4094 ctx->ac.builder, gather, array_idx, "");
4095 }
4096 }
4097
4098 static void vote_all_emit(
4099 const struct lp_build_tgsi_action *action,
4100 struct lp_build_tgsi_context *bld_base,
4101 struct lp_build_emit_data *emit_data)
4102 {
4103 struct si_shader_context *ctx = si_shader_context(bld_base);
4104
4105 LLVMValueRef tmp = ac_build_vote_all(&ctx->ac, emit_data->args[0]);
4106 emit_data->output[emit_data->chan] =
4107 LLVMBuildSExt(ctx->ac.builder, tmp, ctx->i32, "");
4108 }
4109
4110 static void vote_any_emit(
4111 const struct lp_build_tgsi_action *action,
4112 struct lp_build_tgsi_context *bld_base,
4113 struct lp_build_emit_data *emit_data)
4114 {
4115 struct si_shader_context *ctx = si_shader_context(bld_base);
4116
4117 LLVMValueRef tmp = ac_build_vote_any(&ctx->ac, emit_data->args[0]);
4118 emit_data->output[emit_data->chan] =
4119 LLVMBuildSExt(ctx->ac.builder, tmp, ctx->i32, "");
4120 }
4121
4122 static void vote_eq_emit(
4123 const struct lp_build_tgsi_action *action,
4124 struct lp_build_tgsi_context *bld_base,
4125 struct lp_build_emit_data *emit_data)
4126 {
4127 struct si_shader_context *ctx = si_shader_context(bld_base);
4128
4129 LLVMValueRef tmp = ac_build_vote_eq(&ctx->ac, emit_data->args[0]);
4130 emit_data->output[emit_data->chan] =
4131 LLVMBuildSExt(ctx->ac.builder, tmp, ctx->i32, "");
4132 }
4133
4134 static void ballot_emit(
4135 const struct lp_build_tgsi_action *action,
4136 struct lp_build_tgsi_context *bld_base,
4137 struct lp_build_emit_data *emit_data)
4138 {
4139 struct si_shader_context *ctx = si_shader_context(bld_base);
4140 LLVMBuilderRef builder = ctx->ac.builder;
4141 LLVMValueRef tmp;
4142
4143 tmp = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
4144 tmp = ac_build_ballot(&ctx->ac, tmp);
4145 tmp = LLVMBuildBitCast(builder, tmp, ctx->v2i32, "");
4146
4147 emit_data->output[0] = LLVMBuildExtractElement(builder, tmp, ctx->i32_0, "");
4148 emit_data->output[1] = LLVMBuildExtractElement(builder, tmp, ctx->i32_1, "");
4149 }
4150
4151 static void read_lane_emit(
4152 const struct lp_build_tgsi_action *action,
4153 struct lp_build_tgsi_context *bld_base,
4154 struct lp_build_emit_data *emit_data)
4155 {
4156 struct si_shader_context *ctx = si_shader_context(bld_base);
4157
4158 if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_READ_INVOC) {
4159 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
4160 0, emit_data->src_chan);
4161
4162 /* Always read the source invocation (= lane) from the X channel. */
4163 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
4164 1, TGSI_CHAN_X);
4165 emit_data->arg_count = 2;
4166 }
4167
4168 /* We currently have no other way to prevent LLVM from lifting the icmp
4169 * calls to a dominating basic block.
4170 */
4171 ac_build_optimization_barrier(&ctx->ac, &emit_data->args[0]);
4172
4173 for (unsigned i = 0; i < emit_data->arg_count; ++i)
4174 emit_data->args[i] = ac_to_integer(&ctx->ac, emit_data->args[i]);
4175
4176 emit_data->output[emit_data->chan] =
4177 ac_build_intrinsic(&ctx->ac, action->intr_name,
4178 ctx->i32, emit_data->args, emit_data->arg_count,
4179 AC_FUNC_ATTR_READNONE |
4180 AC_FUNC_ATTR_CONVERGENT);
4181 }
4182
4183 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context *bld_base,
4184 struct lp_build_emit_data *emit_data)
4185 {
4186 struct si_shader_context *ctx = si_shader_context(bld_base);
4187 struct tgsi_src_register src0 = emit_data->inst->Src[0].Register;
4188 LLVMValueRef imm;
4189 unsigned stream;
4190
4191 assert(src0.File == TGSI_FILE_IMMEDIATE);
4192
4193 imm = ctx->imms[src0.Index * TGSI_NUM_CHANNELS + src0.SwizzleX];
4194 stream = LLVMConstIntGetZExtValue(imm) & 0x3;
4195 return stream;
4196 }
4197
4198 /* Emit one vertex from the geometry shader */
4199 static void si_llvm_emit_vertex(struct ac_shader_abi *abi,
4200 unsigned stream,
4201 LLVMValueRef *addrs)
4202 {
4203 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
4204
4205 if (ctx->shader->key.as_ngg) {
4206 gfx10_ngg_gs_emit_vertex(ctx, stream, addrs);
4207 return;
4208 }
4209
4210 struct tgsi_shader_info *info = &ctx->shader->selector->info;
4211 struct si_shader *shader = ctx->shader;
4212 struct lp_build_if_state if_state;
4213 LLVMValueRef soffset = LLVMGetParam(ctx->main_fn,
4214 ctx->param_gs2vs_offset);
4215 LLVMValueRef gs_next_vertex;
4216 LLVMValueRef can_emit;
4217 unsigned chan, offset;
4218 int i;
4219
4220 /* Write vertex attribute values to GSVS ring */
4221 gs_next_vertex = LLVMBuildLoad(ctx->ac.builder,
4222 ctx->gs_next_vertex[stream],
4223 "");
4224
4225 /* If this thread has already emitted the declared maximum number of
4226 * vertices, skip the write: excessive vertex emissions are not
4227 * supposed to have any effect.
4228 *
4229 * If the shader has no writes to memory, kill it instead. This skips
4230 * further memory loads and may allow LLVM to skip to the end
4231 * altogether.
4232 */
4233 can_emit = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, gs_next_vertex,
4234 LLVMConstInt(ctx->i32,
4235 shader->selector->gs_max_out_vertices, 0), "");
4236
4237 bool use_kill = !info->writes_memory;
4238 if (use_kill) {
4239 ac_build_kill_if_false(&ctx->ac, can_emit);
4240 } else {
4241 lp_build_if(&if_state, &ctx->gallivm, can_emit);
4242 }
4243
4244 offset = 0;
4245 for (i = 0; i < info->num_outputs; i++) {
4246 for (chan = 0; chan < 4; chan++) {
4247 if (!(info->output_usagemask[i] & (1 << chan)) ||
4248 ((info->output_streams[i] >> (2 * chan)) & 3) != stream)
4249 continue;
4250
4251 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], "");
4252 LLVMValueRef voffset =
4253 LLVMConstInt(ctx->i32, offset *
4254 shader->selector->gs_max_out_vertices, 0);
4255 offset++;
4256
4257 voffset = LLVMBuildAdd(ctx->ac.builder, voffset, gs_next_vertex, "");
4258 voffset = LLVMBuildMul(ctx->ac.builder, voffset,
4259 LLVMConstInt(ctx->i32, 4, 0), "");
4260
4261 out_val = ac_to_integer(&ctx->ac, out_val);
4262
4263 ac_build_buffer_store_dword(&ctx->ac,
4264 ctx->gsvs_ring[stream],
4265 out_val, 1,
4266 voffset, soffset, 0,
4267 1, 1, true);
4268 }
4269 }
4270
4271 gs_next_vertex = LLVMBuildAdd(ctx->ac.builder, gs_next_vertex, ctx->i32_1, "");
4272 LLVMBuildStore(ctx->ac.builder, gs_next_vertex, ctx->gs_next_vertex[stream]);
4273
4274 /* Signal vertex emission if vertex data was written. */
4275 if (offset) {
4276 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_EMIT | AC_SENDMSG_GS | (stream << 8),
4277 si_get_gs_wave_id(ctx));
4278 }
4279
4280 if (!use_kill)
4281 lp_build_endif(&if_state);
4282 }
4283
4284 /* Emit one vertex from the geometry shader */
4285 static void si_tgsi_emit_vertex(
4286 const struct lp_build_tgsi_action *action,
4287 struct lp_build_tgsi_context *bld_base,
4288 struct lp_build_emit_data *emit_data)
4289 {
4290 struct si_shader_context *ctx = si_shader_context(bld_base);
4291 unsigned stream = si_llvm_get_stream(bld_base, emit_data);
4292
4293 si_llvm_emit_vertex(&ctx->abi, stream, ctx->outputs[0]);
4294 }
4295
4296 /* Cut one primitive from the geometry shader */
4297 static void si_llvm_emit_primitive(struct ac_shader_abi *abi,
4298 unsigned stream)
4299 {
4300 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
4301
4302 if (ctx->shader->key.as_ngg) {
4303 LLVMBuildStore(ctx->ac.builder, ctx->ac.i32_0, ctx->gs_curprim_verts[stream]);
4304 return;
4305 }
4306
4307 /* Signal primitive cut */
4308 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_CUT | AC_SENDMSG_GS | (stream << 8),
4309 si_get_gs_wave_id(ctx));
4310 }
4311
4312 /* Cut one primitive from the geometry shader */
4313 static void si_tgsi_emit_primitive(
4314 const struct lp_build_tgsi_action *action,
4315 struct lp_build_tgsi_context *bld_base,
4316 struct lp_build_emit_data *emit_data)
4317 {
4318 struct si_shader_context *ctx = si_shader_context(bld_base);
4319
4320 si_llvm_emit_primitive(&ctx->abi, si_llvm_get_stream(bld_base, emit_data));
4321 }
4322
4323 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action,
4324 struct lp_build_tgsi_context *bld_base,
4325 struct lp_build_emit_data *emit_data)
4326 {
4327 struct si_shader_context *ctx = si_shader_context(bld_base);
4328
4329 /* GFX6 only (thanks to a hw bug workaround):
4330 * The real barrier instruction isn’t needed, because an entire patch
4331 * always fits into a single wave.
4332 */
4333 if (ctx->screen->info.chip_class == GFX6 &&
4334 ctx->type == PIPE_SHADER_TESS_CTRL) {
4335 ac_build_waitcnt(&ctx->ac, LGKM_CNT & VM_CNT);
4336 return;
4337 }
4338
4339 ac_build_s_barrier(&ctx->ac);
4340 }
4341
4342 void si_create_function(struct si_shader_context *ctx,
4343 const char *name,
4344 LLVMTypeRef *returns, unsigned num_returns,
4345 struct si_function_info *fninfo,
4346 unsigned max_workgroup_size)
4347 {
4348 int i;
4349
4350 si_llvm_create_func(ctx, name, returns, num_returns,
4351 fninfo->types, fninfo->num_params);
4352 ctx->return_value = LLVMGetUndef(ctx->return_type);
4353
4354 for (i = 0; i < fninfo->num_sgpr_params; ++i) {
4355 LLVMValueRef P = LLVMGetParam(ctx->main_fn, i);
4356
4357 /* The combination of:
4358 * - noalias
4359 * - dereferenceable
4360 * - invariant.load
4361 * allows the optimization passes to move loads and reduces
4362 * SGPR spilling significantly.
4363 */
4364 ac_add_function_attr(ctx->ac.context, ctx->main_fn, i + 1,
4365 AC_FUNC_ATTR_INREG);
4366
4367 if (LLVMGetTypeKind(LLVMTypeOf(P)) == LLVMPointerTypeKind) {
4368 ac_add_function_attr(ctx->ac.context, ctx->main_fn, i + 1,
4369 AC_FUNC_ATTR_NOALIAS);
4370 ac_add_attr_dereferenceable(P, UINT64_MAX);
4371 }
4372 }
4373
4374 for (i = 0; i < fninfo->num_params; ++i) {
4375 if (fninfo->assign[i])
4376 *fninfo->assign[i] = LLVMGetParam(ctx->main_fn, i);
4377 }
4378
4379 if (ctx->screen->info.address32_hi) {
4380 ac_llvm_add_target_dep_function_attr(ctx->main_fn,
4381 "amdgpu-32bit-address-high-bits",
4382 ctx->screen->info.address32_hi);
4383 }
4384
4385 ac_llvm_set_workgroup_size(ctx->main_fn, max_workgroup_size);
4386
4387 LLVMAddTargetDependentFunctionAttr(ctx->main_fn,
4388 "no-signed-zeros-fp-math",
4389 "true");
4390
4391 if (ctx->screen->debug_flags & DBG(UNSAFE_MATH)) {
4392 /* These were copied from some LLVM test. */
4393 LLVMAddTargetDependentFunctionAttr(ctx->main_fn,
4394 "less-precise-fpmad",
4395 "true");
4396 LLVMAddTargetDependentFunctionAttr(ctx->main_fn,
4397 "no-infs-fp-math",
4398 "true");
4399 LLVMAddTargetDependentFunctionAttr(ctx->main_fn,
4400 "no-nans-fp-math",
4401 "true");
4402 LLVMAddTargetDependentFunctionAttr(ctx->main_fn,
4403 "unsafe-fp-math",
4404 "true");
4405 }
4406 }
4407
4408 static void declare_streamout_params(struct si_shader_context *ctx,
4409 struct pipe_stream_output_info *so,
4410 struct si_function_info *fninfo)
4411 {
4412 int i;
4413
4414 /* Streamout SGPRs. */
4415 if (so->num_outputs) {
4416 if (ctx->type != PIPE_SHADER_TESS_EVAL)
4417 ctx->param_streamout_config = add_arg(fninfo, ARG_SGPR, ctx->ac.i32);
4418 else
4419 ctx->param_streamout_config = fninfo->num_params - 1;
4420
4421 ctx->param_streamout_write_index = add_arg(fninfo, ARG_SGPR, ctx->ac.i32);
4422 }
4423 /* A streamout buffer offset is loaded if the stride is non-zero. */
4424 for (i = 0; i < 4; i++) {
4425 if (!so->stride[i])
4426 continue;
4427
4428 ctx->param_streamout_offset[i] = add_arg(fninfo, ARG_SGPR, ctx->ac.i32);
4429 }
4430 }
4431
4432 static unsigned si_get_max_workgroup_size(const struct si_shader *shader)
4433 {
4434 switch (shader->selector->type) {
4435 case PIPE_SHADER_VERTEX:
4436 case PIPE_SHADER_TESS_EVAL:
4437 return shader->key.as_ngg ? 128 : 0;
4438
4439 case PIPE_SHADER_TESS_CTRL:
4440 /* Return this so that LLVM doesn't remove s_barrier
4441 * instructions on chips where we use s_barrier. */
4442 return shader->selector->screen->info.chip_class >= GFX7 ? 128 : 64;
4443
4444 case PIPE_SHADER_GEOMETRY:
4445 return shader->selector->screen->info.chip_class >= GFX9 ? 128 : 64;
4446
4447 case PIPE_SHADER_COMPUTE:
4448 break; /* see below */
4449
4450 default:
4451 return 0;
4452 }
4453
4454 const unsigned *properties = shader->selector->info.properties;
4455 unsigned max_work_group_size =
4456 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] *
4457 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] *
4458 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH];
4459
4460 if (!max_work_group_size) {
4461 /* This is a variable group size compute shader,
4462 * compile it for the maximum possible group size.
4463 */
4464 max_work_group_size = SI_MAX_VARIABLE_THREADS_PER_BLOCK;
4465 }
4466 return max_work_group_size;
4467 }
4468
4469 static void declare_const_and_shader_buffers(struct si_shader_context *ctx,
4470 struct si_function_info *fninfo,
4471 bool assign_params)
4472 {
4473 LLVMTypeRef const_shader_buf_type;
4474
4475 if (ctx->shader->selector->info.const_buffers_declared == 1 &&
4476 ctx->shader->selector->info.shader_buffers_declared == 0)
4477 const_shader_buf_type = ctx->f32;
4478 else
4479 const_shader_buf_type = ctx->v4i32;
4480
4481 unsigned const_and_shader_buffers =
4482 add_arg(fninfo, ARG_SGPR,
4483 ac_array_in_const32_addr_space(const_shader_buf_type));
4484
4485 if (assign_params)
4486 ctx->param_const_and_shader_buffers = const_and_shader_buffers;
4487 }
4488
4489 static void declare_samplers_and_images(struct si_shader_context *ctx,
4490 struct si_function_info *fninfo,
4491 bool assign_params)
4492 {
4493 unsigned samplers_and_images =
4494 add_arg(fninfo, ARG_SGPR,
4495 ac_array_in_const32_addr_space(ctx->v8i32));
4496
4497 if (assign_params)
4498 ctx->param_samplers_and_images = samplers_and_images;
4499 }
4500
4501 static void declare_per_stage_desc_pointers(struct si_shader_context *ctx,
4502 struct si_function_info *fninfo,
4503 bool assign_params)
4504 {
4505 declare_const_and_shader_buffers(ctx, fninfo, assign_params);
4506 declare_samplers_and_images(ctx, fninfo, assign_params);
4507 }
4508
4509 static void declare_global_desc_pointers(struct si_shader_context *ctx,
4510 struct si_function_info *fninfo)
4511 {
4512 ctx->param_rw_buffers = add_arg(fninfo, ARG_SGPR,
4513 ac_array_in_const32_addr_space(ctx->v4i32));
4514 ctx->param_bindless_samplers_and_images = add_arg(fninfo, ARG_SGPR,
4515 ac_array_in_const32_addr_space(ctx->v8i32));
4516 }
4517
4518 static void declare_vs_specific_input_sgprs(struct si_shader_context *ctx,
4519 struct si_function_info *fninfo)
4520 {
4521 ctx->param_vs_state_bits = add_arg(fninfo, ARG_SGPR, ctx->i32);
4522 add_arg_assign(fninfo, ARG_SGPR, ctx->i32, &ctx->abi.base_vertex);
4523 add_arg_assign(fninfo, ARG_SGPR, ctx->i32, &ctx->abi.start_instance);
4524 add_arg_assign(fninfo, ARG_SGPR, ctx->i32, &ctx->abi.draw_id);
4525 }
4526
4527 static void declare_vs_input_vgprs(struct si_shader_context *ctx,
4528 struct si_function_info *fninfo,
4529 unsigned *num_prolog_vgprs)
4530 {
4531 struct si_shader *shader = ctx->shader;
4532
4533 add_arg_assign(fninfo, ARG_VGPR, ctx->i32, &ctx->abi.vertex_id);
4534 if (shader->key.as_ls) {
4535 ctx->param_rel_auto_id = add_arg(fninfo, ARG_VGPR, ctx->i32);
4536 add_arg_assign(fninfo, ARG_VGPR, ctx->i32, &ctx->abi.instance_id);
4537 add_arg(fninfo, ARG_VGPR, ctx->i32); /* unused */
4538 } else if (ctx->screen->info.chip_class == GFX10 &&
4539 !shader->is_gs_copy_shader) {
4540 add_arg(fninfo, ARG_VGPR, ctx->i32); /* user vgpr */
4541 add_arg(fninfo, ARG_VGPR, ctx->i32); /* user vgpr */
4542 add_arg_assign(fninfo, ARG_VGPR, ctx->i32, &ctx->abi.instance_id);
4543 } else {
4544 add_arg_assign(fninfo, ARG_VGPR, ctx->i32, &ctx->abi.instance_id);
4545 ctx->param_vs_prim_id = add_arg(fninfo, ARG_VGPR, ctx->i32);
4546 add_arg(fninfo, ARG_VGPR, ctx->i32); /* unused */
4547 }
4548
4549 if (!shader->is_gs_copy_shader) {
4550 /* Vertex load indices. */
4551 ctx->param_vertex_index0 = fninfo->num_params;
4552 for (unsigned i = 0; i < shader->selector->info.num_inputs; i++)
4553 add_arg(fninfo, ARG_VGPR, ctx->i32);
4554 *num_prolog_vgprs += shader->selector->info.num_inputs;
4555 }
4556 }
4557
4558 static void declare_vs_blit_inputs(struct si_shader_context *ctx,
4559 struct si_function_info *fninfo,
4560 unsigned vs_blit_property)
4561 {
4562 ctx->param_vs_blit_inputs = fninfo->num_params;
4563 add_arg(fninfo, ARG_SGPR, ctx->i32); /* i16 x1, y1 */
4564 add_arg(fninfo, ARG_SGPR, ctx->i32); /* i16 x2, y2 */
4565 add_arg(fninfo, ARG_SGPR, ctx->f32); /* depth */
4566
4567 if (vs_blit_property == SI_VS_BLIT_SGPRS_POS_COLOR) {
4568 add_arg(fninfo, ARG_SGPR, ctx->f32); /* color0 */
4569 add_arg(fninfo, ARG_SGPR, ctx->f32); /* color1 */
4570 add_arg(fninfo, ARG_SGPR, ctx->f32); /* color2 */
4571 add_arg(fninfo, ARG_SGPR, ctx->f32); /* color3 */
4572 } else if (vs_blit_property == SI_VS_BLIT_SGPRS_POS_TEXCOORD) {
4573 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.x1 */
4574 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.y1 */
4575 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.x2 */
4576 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.y2 */
4577 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.z */
4578 add_arg(fninfo, ARG_SGPR, ctx->f32); /* texcoord.w */
4579 }
4580 }
4581
4582 static void declare_tes_input_vgprs(struct si_shader_context *ctx,
4583 struct si_function_info *fninfo)
4584 {
4585 ctx->param_tes_u = add_arg(fninfo, ARG_VGPR, ctx->f32);
4586 ctx->param_tes_v = add_arg(fninfo, ARG_VGPR, ctx->f32);
4587 ctx->param_tes_rel_patch_id = add_arg(fninfo, ARG_VGPR, ctx->i32);
4588 add_arg_assign(fninfo, ARG_VGPR, ctx->i32, &ctx->abi.tes_patch_id);
4589 }
4590
4591 enum {
4592 /* Convenient merged shader definitions. */
4593 SI_SHADER_MERGED_VERTEX_TESSCTRL = PIPE_SHADER_TYPES,
4594 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY,
4595 };
4596
4597 static void create_function(struct si_shader_context *ctx)
4598 {
4599 struct si_shader *shader = ctx->shader;
4600 struct si_function_info fninfo;
4601 LLVMTypeRef returns[16+32*4];
4602 unsigned i, num_return_sgprs;
4603 unsigned num_returns = 0;
4604 unsigned num_prolog_vgprs = 0;
4605 unsigned type = ctx->type;
4606 unsigned vs_blit_property =
4607 shader->selector->info.properties[TGSI_PROPERTY_VS_BLIT_SGPRS];
4608
4609 si_init_function_info(&fninfo);
4610
4611 /* Set MERGED shaders. */
4612 if (ctx->screen->info.chip_class >= GFX9) {
4613 if (shader->key.as_ls || type == PIPE_SHADER_TESS_CTRL)
4614 type = SI_SHADER_MERGED_VERTEX_TESSCTRL; /* LS or HS */
4615 else if (shader->key.as_es || shader->key.as_ngg || type == PIPE_SHADER_GEOMETRY)
4616 type = SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY;
4617 }
4618
4619 LLVMTypeRef v3i32 = LLVMVectorType(ctx->i32, 3);
4620
4621 switch (type) {
4622 case PIPE_SHADER_VERTEX:
4623 declare_global_desc_pointers(ctx, &fninfo);
4624
4625 if (vs_blit_property) {
4626 declare_vs_blit_inputs(ctx, &fninfo, vs_blit_property);
4627
4628 /* VGPRs */
4629 declare_vs_input_vgprs(ctx, &fninfo, &num_prolog_vgprs);
4630 break;
4631 }
4632
4633 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4634 declare_vs_specific_input_sgprs(ctx, &fninfo);
4635 ctx->param_vertex_buffers = add_arg(&fninfo, ARG_SGPR,
4636 ac_array_in_const32_addr_space(ctx->v4i32));
4637
4638 if (shader->key.as_es) {
4639 ctx->param_es2gs_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4640 } else if (shader->key.as_ls) {
4641 /* no extra parameters */
4642 } else {
4643 if (shader->is_gs_copy_shader) {
4644 fninfo.num_params = ctx->param_vs_state_bits + 1;
4645 fninfo.num_sgpr_params = fninfo.num_params;
4646 }
4647
4648 /* The locations of the other parameters are assigned dynamically. */
4649 declare_streamout_params(ctx, &shader->selector->so,
4650 &fninfo);
4651 }
4652
4653 /* VGPRs */
4654 declare_vs_input_vgprs(ctx, &fninfo, &num_prolog_vgprs);
4655
4656 /* Return values */
4657 if (shader->key.opt.vs_as_prim_discard_cs) {
4658 for (i = 0; i < 4; i++)
4659 returns[num_returns++] = ctx->f32; /* VGPRs */
4660 }
4661 break;
4662
4663 case PIPE_SHADER_TESS_CTRL: /* GFX6-GFX8 */
4664 declare_global_desc_pointers(ctx, &fninfo);
4665 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4666 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4667 ctx->param_tcs_out_lds_offsets = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4668 ctx->param_tcs_out_lds_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4669 ctx->param_vs_state_bits = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4670 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4671 ctx->param_tcs_factor_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4672
4673 /* VGPRs */
4674 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.tcs_patch_id);
4675 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.tcs_rel_ids);
4676
4677 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4678 * placed after the user SGPRs.
4679 */
4680 for (i = 0; i < GFX6_TCS_NUM_USER_SGPR + 2; i++)
4681 returns[num_returns++] = ctx->i32; /* SGPRs */
4682 for (i = 0; i < 11; i++)
4683 returns[num_returns++] = ctx->f32; /* VGPRs */
4684 break;
4685
4686 case SI_SHADER_MERGED_VERTEX_TESSCTRL:
4687 /* Merged stages have 8 system SGPRs at the beginning. */
4688 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
4689 declare_per_stage_desc_pointers(ctx, &fninfo,
4690 ctx->type == PIPE_SHADER_TESS_CTRL);
4691 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4692 ctx->param_merged_wave_info = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4693 ctx->param_tcs_factor_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4694 ctx->param_merged_scratch_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4695 add_arg(&fninfo, ARG_SGPR, ctx->i32); /* unused */
4696 add_arg(&fninfo, ARG_SGPR, ctx->i32); /* unused */
4697
4698 declare_global_desc_pointers(ctx, &fninfo);
4699 declare_per_stage_desc_pointers(ctx, &fninfo,
4700 ctx->type == PIPE_SHADER_VERTEX);
4701 declare_vs_specific_input_sgprs(ctx, &fninfo);
4702
4703 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4704 ctx->param_tcs_out_lds_offsets = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4705 ctx->param_tcs_out_lds_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4706 ctx->param_vertex_buffers = add_arg(&fninfo, ARG_SGPR,
4707 ac_array_in_const32_addr_space(ctx->v4i32));
4708
4709 /* VGPRs (first TCS, then VS) */
4710 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.tcs_patch_id);
4711 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.tcs_rel_ids);
4712
4713 if (ctx->type == PIPE_SHADER_VERTEX) {
4714 declare_vs_input_vgprs(ctx, &fninfo,
4715 &num_prolog_vgprs);
4716
4717 /* LS return values are inputs to the TCS main shader part. */
4718 for (i = 0; i < 8 + GFX9_TCS_NUM_USER_SGPR; i++)
4719 returns[num_returns++] = ctx->i32; /* SGPRs */
4720 for (i = 0; i < 2; i++)
4721 returns[num_returns++] = ctx->f32; /* VGPRs */
4722 } else {
4723 /* TCS return values are inputs to the TCS epilog.
4724 *
4725 * param_tcs_offchip_offset, param_tcs_factor_offset,
4726 * param_tcs_offchip_layout, and param_rw_buffers
4727 * should be passed to the epilog.
4728 */
4729 for (i = 0; i <= 8 + GFX9_SGPR_TCS_OUT_LAYOUT; i++)
4730 returns[num_returns++] = ctx->i32; /* SGPRs */
4731 for (i = 0; i < 11; i++)
4732 returns[num_returns++] = ctx->f32; /* VGPRs */
4733 }
4734 break;
4735
4736 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY:
4737 /* Merged stages have 8 system SGPRs at the beginning. */
4738 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
4739 declare_per_stage_desc_pointers(ctx, &fninfo,
4740 ctx->type == PIPE_SHADER_GEOMETRY);
4741
4742 if (ctx->shader->key.as_ngg)
4743 add_arg_assign(&fninfo, ARG_SGPR, ctx->i32, &ctx->gs_tg_info);
4744 else
4745 ctx->param_gs2vs_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4746
4747 ctx->param_merged_wave_info = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4748 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4749 ctx->param_merged_scratch_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4750 add_arg(&fninfo, ARG_SGPR, ctx->i32); /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4751 add_arg(&fninfo, ARG_SGPR, ctx->i32); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4752
4753 declare_global_desc_pointers(ctx, &fninfo);
4754 if (ctx->type != PIPE_SHADER_VERTEX || !vs_blit_property) {
4755 declare_per_stage_desc_pointers(ctx, &fninfo,
4756 (ctx->type == PIPE_SHADER_VERTEX ||
4757 ctx->type == PIPE_SHADER_TESS_EVAL));
4758 }
4759
4760 if (ctx->type == PIPE_SHADER_VERTEX) {
4761 if (vs_blit_property)
4762 declare_vs_blit_inputs(ctx, &fninfo, vs_blit_property);
4763 else
4764 declare_vs_specific_input_sgprs(ctx, &fninfo);
4765 } else {
4766 ctx->param_vs_state_bits = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4767 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4768 ctx->param_tes_offchip_addr = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4769 /* Declare as many input SGPRs as the VS has. */
4770 }
4771
4772 if (ctx->type == PIPE_SHADER_VERTEX) {
4773 ctx->param_vertex_buffers = add_arg(&fninfo, ARG_SGPR,
4774 ac_array_in_const32_addr_space(ctx->v4i32));
4775 }
4776
4777 /* VGPRs (first GS, then VS/TES) */
4778 ctx->param_gs_vtx01_offset = add_arg(&fninfo, ARG_VGPR, ctx->i32);
4779 ctx->param_gs_vtx23_offset = add_arg(&fninfo, ARG_VGPR, ctx->i32);
4780 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.gs_prim_id);
4781 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.gs_invocation_id);
4782 ctx->param_gs_vtx45_offset = add_arg(&fninfo, ARG_VGPR, ctx->i32);
4783
4784 if (ctx->type == PIPE_SHADER_VERTEX) {
4785 declare_vs_input_vgprs(ctx, &fninfo,
4786 &num_prolog_vgprs);
4787 } else if (ctx->type == PIPE_SHADER_TESS_EVAL) {
4788 declare_tes_input_vgprs(ctx, &fninfo);
4789 }
4790
4791 if (ctx->shader->key.as_es &&
4792 (ctx->type == PIPE_SHADER_VERTEX ||
4793 ctx->type == PIPE_SHADER_TESS_EVAL)) {
4794 unsigned num_user_sgprs;
4795
4796 if (ctx->type == PIPE_SHADER_VERTEX)
4797 num_user_sgprs = GFX9_VSGS_NUM_USER_SGPR;
4798 else
4799 num_user_sgprs = GFX9_TESGS_NUM_USER_SGPR;
4800
4801 /* ES return values are inputs to GS. */
4802 for (i = 0; i < 8 + num_user_sgprs; i++)
4803 returns[num_returns++] = ctx->i32; /* SGPRs */
4804 for (i = 0; i < 5; i++)
4805 returns[num_returns++] = ctx->f32; /* VGPRs */
4806 }
4807 break;
4808
4809 case PIPE_SHADER_TESS_EVAL:
4810 declare_global_desc_pointers(ctx, &fninfo);
4811 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4812 ctx->param_vs_state_bits = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4813 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4814 ctx->param_tes_offchip_addr = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4815
4816 if (shader->key.as_es) {
4817 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4818 add_arg(&fninfo, ARG_SGPR, ctx->i32);
4819 ctx->param_es2gs_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4820 } else {
4821 add_arg(&fninfo, ARG_SGPR, ctx->i32);
4822 declare_streamout_params(ctx, &shader->selector->so,
4823 &fninfo);
4824 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4825 }
4826
4827 /* VGPRs */
4828 declare_tes_input_vgprs(ctx, &fninfo);
4829 break;
4830
4831 case PIPE_SHADER_GEOMETRY:
4832 declare_global_desc_pointers(ctx, &fninfo);
4833 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4834 ctx->param_gs2vs_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4835 ctx->param_gs_wave_id = add_arg(&fninfo, ARG_SGPR, ctx->i32);
4836
4837 /* VGPRs */
4838 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[0]);
4839 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[1]);
4840 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.gs_prim_id);
4841 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[2]);
4842 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[3]);
4843 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[4]);
4844 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->gs_vtx_offset[5]);
4845 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &ctx->abi.gs_invocation_id);
4846 break;
4847
4848 case PIPE_SHADER_FRAGMENT:
4849 declare_global_desc_pointers(ctx, &fninfo);
4850 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4851 add_arg_checked(&fninfo, ARG_SGPR, ctx->f32, SI_PARAM_ALPHA_REF);
4852 add_arg_assign_checked(&fninfo, ARG_SGPR, ctx->i32,
4853 &ctx->abi.prim_mask, SI_PARAM_PRIM_MASK);
4854
4855 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_PERSP_SAMPLE);
4856 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_PERSP_CENTER);
4857 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_PERSP_CENTROID);
4858 add_arg_checked(&fninfo, ARG_VGPR, v3i32, SI_PARAM_PERSP_PULL_MODEL);
4859 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_LINEAR_SAMPLE);
4860 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_LINEAR_CENTER);
4861 add_arg_checked(&fninfo, ARG_VGPR, ctx->v2i32, SI_PARAM_LINEAR_CENTROID);
4862 add_arg_checked(&fninfo, ARG_VGPR, ctx->f32, SI_PARAM_LINE_STIPPLE_TEX);
4863 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->f32,
4864 &ctx->abi.frag_pos[0], SI_PARAM_POS_X_FLOAT);
4865 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->f32,
4866 &ctx->abi.frag_pos[1], SI_PARAM_POS_Y_FLOAT);
4867 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->f32,
4868 &ctx->abi.frag_pos[2], SI_PARAM_POS_Z_FLOAT);
4869 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->f32,
4870 &ctx->abi.frag_pos[3], SI_PARAM_POS_W_FLOAT);
4871 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->i32,
4872 &ctx->abi.front_face, SI_PARAM_FRONT_FACE);
4873 shader->info.face_vgpr_index = 20;
4874 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->i32,
4875 &ctx->abi.ancillary, SI_PARAM_ANCILLARY);
4876 shader->info.ancillary_vgpr_index = 21;
4877 add_arg_assign_checked(&fninfo, ARG_VGPR, ctx->f32,
4878 &ctx->abi.sample_coverage, SI_PARAM_SAMPLE_COVERAGE);
4879 add_arg_checked(&fninfo, ARG_VGPR, ctx->i32, SI_PARAM_POS_FIXED_PT);
4880
4881 /* Color inputs from the prolog. */
4882 if (shader->selector->info.colors_read) {
4883 unsigned num_color_elements =
4884 util_bitcount(shader->selector->info.colors_read);
4885
4886 assert(fninfo.num_params + num_color_elements <= ARRAY_SIZE(fninfo.types));
4887 for (i = 0; i < num_color_elements; i++)
4888 add_arg(&fninfo, ARG_VGPR, ctx->f32);
4889
4890 num_prolog_vgprs += num_color_elements;
4891 }
4892
4893 /* Outputs for the epilog. */
4894 num_return_sgprs = SI_SGPR_ALPHA_REF + 1;
4895 num_returns =
4896 num_return_sgprs +
4897 util_bitcount(shader->selector->info.colors_written) * 4 +
4898 shader->selector->info.writes_z +
4899 shader->selector->info.writes_stencil +
4900 shader->selector->info.writes_samplemask +
4901 1 /* SampleMaskIn */;
4902
4903 num_returns = MAX2(num_returns,
4904 num_return_sgprs +
4905 PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
4906
4907 for (i = 0; i < num_return_sgprs; i++)
4908 returns[i] = ctx->i32;
4909 for (; i < num_returns; i++)
4910 returns[i] = ctx->f32;
4911 break;
4912
4913 case PIPE_SHADER_COMPUTE:
4914 declare_global_desc_pointers(ctx, &fninfo);
4915 declare_per_stage_desc_pointers(ctx, &fninfo, true);
4916 if (shader->selector->info.uses_grid_size)
4917 add_arg_assign(&fninfo, ARG_SGPR, v3i32, &ctx->abi.num_work_groups);
4918 if (shader->selector->info.uses_block_size &&
4919 shader->selector->info.properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
4920 ctx->param_block_size = add_arg(&fninfo, ARG_SGPR, v3i32);
4921
4922 unsigned cs_user_data_dwords =
4923 shader->selector->info.properties[TGSI_PROPERTY_CS_USER_DATA_DWORDS];
4924 if (cs_user_data_dwords) {
4925 ctx->param_cs_user_data = add_arg(&fninfo, ARG_SGPR,
4926 LLVMVectorType(ctx->i32, cs_user_data_dwords));
4927 }
4928
4929 for (i = 0; i < 3; i++) {
4930 ctx->abi.workgroup_ids[i] = NULL;
4931 if (shader->selector->info.uses_block_id[i])
4932 add_arg_assign(&fninfo, ARG_SGPR, ctx->i32, &ctx->abi.workgroup_ids[i]);
4933 }
4934
4935 add_arg_assign(&fninfo, ARG_VGPR, v3i32, &ctx->abi.local_invocation_ids);
4936 break;
4937 default:
4938 assert(0 && "unimplemented shader");
4939 return;
4940 }
4941
4942 si_create_function(ctx, "main", returns, num_returns, &fninfo,
4943 si_get_max_workgroup_size(shader));
4944
4945 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4946 if (ctx->type == PIPE_SHADER_FRAGMENT && !ctx->shader->is_monolithic) {
4947 ac_llvm_add_target_dep_function_attr(ctx->main_fn,
4948 "InitialPSInputAddr",
4949 S_0286D0_PERSP_SAMPLE_ENA(1) |
4950 S_0286D0_PERSP_CENTER_ENA(1) |
4951 S_0286D0_PERSP_CENTROID_ENA(1) |
4952 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4953 S_0286D0_LINEAR_CENTER_ENA(1) |
4954 S_0286D0_LINEAR_CENTROID_ENA(1) |
4955 S_0286D0_FRONT_FACE_ENA(1) |
4956 S_0286D0_ANCILLARY_ENA(1) |
4957 S_0286D0_POS_FIXED_PT_ENA(1));
4958 }
4959
4960 shader->info.num_input_sgprs = 0;
4961 shader->info.num_input_vgprs = 0;
4962
4963 for (i = 0; i < fninfo.num_sgpr_params; ++i)
4964 shader->info.num_input_sgprs += ac_get_type_size(fninfo.types[i]) / 4;
4965
4966 for (; i < fninfo.num_params; ++i)
4967 shader->info.num_input_vgprs += ac_get_type_size(fninfo.types[i]) / 4;
4968
4969 assert(shader->info.num_input_vgprs >= num_prolog_vgprs);
4970 shader->info.num_input_vgprs -= num_prolog_vgprs;
4971
4972 if (shader->key.as_ls || ctx->type == PIPE_SHADER_TESS_CTRL) {
4973 if (USE_LDS_SYMBOLS && HAVE_LLVM >= 0x0900) {
4974 /* The LSHS size is not known until draw time, so we append it
4975 * at the end of whatever LDS use there may be in the rest of
4976 * the shader (currently none, unless LLVM decides to do its
4977 * own LDS-based lowering).
4978 */
4979 ctx->ac.lds = LLVMAddGlobalInAddressSpace(
4980 ctx->ac.module, LLVMArrayType(ctx->i32, 0),
4981 "__lds_end", AC_ADDR_SPACE_LDS);
4982 LLVMSetAlignment(ctx->ac.lds, 256);
4983 } else {
4984 ac_declare_lds_as_pointer(&ctx->ac);
4985 }
4986 }
4987 }
4988
4989 /**
4990 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
4991 * for later use.
4992 */
4993 static void preload_ring_buffers(struct si_shader_context *ctx)
4994 {
4995 LLVMBuilderRef builder = ctx->ac.builder;
4996
4997 LLVMValueRef buf_ptr = LLVMGetParam(ctx->main_fn,
4998 ctx->param_rw_buffers);
4999
5000 if (ctx->shader->key.as_es || ctx->type == PIPE_SHADER_GEOMETRY) {
5001 if (ctx->screen->info.chip_class <= GFX8) {
5002 unsigned ring =
5003 ctx->type == PIPE_SHADER_GEOMETRY ? SI_GS_RING_ESGS
5004 : SI_ES_RING_ESGS;
5005 LLVMValueRef offset = LLVMConstInt(ctx->i32, ring, 0);
5006
5007 ctx->esgs_ring =
5008 ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
5009 } else {
5010 if (USE_LDS_SYMBOLS && HAVE_LLVM >= 0x0900) {
5011 /* Declare the ESGS ring as an explicit LDS symbol.
5012 * For monolithic shaders, we declare the ring only once.
5013 *
5014 * We declare it with 64KB alignment as a hint that the
5015 * pointer value will always be 0.
5016 */
5017 ctx->esgs_ring = LLVMAddGlobalInAddressSpace(
5018 ctx->ac.module, LLVMArrayType(ctx->i32, 0),
5019 "esgs_ring",
5020 AC_ADDR_SPACE_LDS);
5021 LLVMSetAlignment(ctx->esgs_ring, 64 * 1024);
5022 } else {
5023 ac_declare_lds_as_pointer(&ctx->ac);
5024 ctx->esgs_ring = ctx->ac.lds;
5025 }
5026 }
5027 }
5028
5029 if (ctx->shader->is_gs_copy_shader) {
5030 LLVMValueRef offset = LLVMConstInt(ctx->i32, SI_RING_GSVS, 0);
5031
5032 ctx->gsvs_ring[0] =
5033 ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
5034 } else if (ctx->type == PIPE_SHADER_GEOMETRY) {
5035 const struct si_shader_selector *sel = ctx->shader->selector;
5036 LLVMValueRef offset = LLVMConstInt(ctx->i32, SI_RING_GSVS, 0);
5037 LLVMValueRef base_ring;
5038
5039 base_ring = ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
5040
5041 /* The conceptual layout of the GSVS ring is
5042 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5043 * but the real memory layout is swizzled across
5044 * threads:
5045 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5046 * t16v0c0 ..
5047 * Override the buffer descriptor accordingly.
5048 */
5049 LLVMTypeRef v2i64 = LLVMVectorType(ctx->i64, 2);
5050 uint64_t stream_offset = 0;
5051
5052 for (unsigned stream = 0; stream < 4; ++stream) {
5053 unsigned num_components;
5054 unsigned stride;
5055 unsigned num_records;
5056 LLVMValueRef ring, tmp;
5057
5058 num_components = sel->info.num_stream_output_components[stream];
5059 if (!num_components)
5060 continue;
5061
5062 stride = 4 * num_components * sel->gs_max_out_vertices;
5063
5064 /* Limit on the stride field for <= GFX7. */
5065 assert(stride < (1 << 14));
5066
5067 num_records = 64;
5068
5069 ring = LLVMBuildBitCast(builder, base_ring, v2i64, "");
5070 tmp = LLVMBuildExtractElement(builder, ring, ctx->i32_0, "");
5071 tmp = LLVMBuildAdd(builder, tmp,
5072 LLVMConstInt(ctx->i64,
5073 stream_offset, 0), "");
5074 stream_offset += stride * 64;
5075
5076 ring = LLVMBuildInsertElement(builder, ring, tmp, ctx->i32_0, "");
5077 ring = LLVMBuildBitCast(builder, ring, ctx->v4i32, "");
5078 tmp = LLVMBuildExtractElement(builder, ring, ctx->i32_1, "");
5079 tmp = LLVMBuildOr(builder, tmp,
5080 LLVMConstInt(ctx->i32,
5081 S_008F04_STRIDE(stride) |
5082 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5083 ring = LLVMBuildInsertElement(builder, ring, tmp, ctx->i32_1, "");
5084 ring = LLVMBuildInsertElement(builder, ring,
5085 LLVMConstInt(ctx->i32, num_records, 0),
5086 LLVMConstInt(ctx->i32, 2, 0), "");
5087
5088 uint32_t rsrc3 =
5089 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
5090 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
5091 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
5092 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
5093 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5094 S_008F0C_ADD_TID_ENABLE(1);
5095
5096 if (ctx->ac.chip_class >= GFX10) {
5097 rsrc3 |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
5098 S_008F0C_OOB_SELECT(2) |
5099 S_008F0C_RESOURCE_LEVEL(1);
5100 } else {
5101 rsrc3 |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
5102 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
5103 S_008F0C_ELEMENT_SIZE(1); /* element_size = 4 (bytes) */
5104 }
5105
5106 ring = LLVMBuildInsertElement(builder, ring,
5107 LLVMConstInt(ctx->i32, rsrc3, false),
5108 LLVMConstInt(ctx->i32, 3, 0), "");
5109
5110 ctx->gsvs_ring[stream] = ring;
5111 }
5112 } else if (ctx->type == PIPE_SHADER_TESS_EVAL) {
5113 ctx->tess_offchip_ring = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TES);
5114 }
5115 }
5116
5117 static void si_llvm_emit_polygon_stipple(struct si_shader_context *ctx,
5118 LLVMValueRef param_rw_buffers,
5119 unsigned param_pos_fixed_pt)
5120 {
5121 LLVMBuilderRef builder = ctx->ac.builder;
5122 LLVMValueRef slot, desc, offset, row, bit, address[2];
5123
5124 /* Use the fixed-point gl_FragCoord input.
5125 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5126 * per coordinate to get the repeating effect.
5127 */
5128 address[0] = si_unpack_param(ctx, param_pos_fixed_pt, 0, 5);
5129 address[1] = si_unpack_param(ctx, param_pos_fixed_pt, 16, 5);
5130
5131 /* Load the buffer descriptor. */
5132 slot = LLVMConstInt(ctx->i32, SI_PS_CONST_POLY_STIPPLE, 0);
5133 desc = ac_build_load_to_sgpr(&ctx->ac, param_rw_buffers, slot);
5134
5135 /* The stipple pattern is 32x32, each row has 32 bits. */
5136 offset = LLVMBuildMul(builder, address[1],
5137 LLVMConstInt(ctx->i32, 4, 0), "");
5138 row = buffer_load_const(ctx, desc, offset);
5139 row = ac_to_integer(&ctx->ac, row);
5140 bit = LLVMBuildLShr(builder, row, address[0], "");
5141 bit = LLVMBuildTrunc(builder, bit, ctx->i1, "");
5142 ac_build_kill_if_false(&ctx->ac, bit);
5143 }
5144
5145 /* For the UMR disassembler. */
5146 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
5147 #define DEBUGGER_NUM_MARKERS 5
5148
5149 static bool si_shader_binary_open(struct si_screen *screen,
5150 struct si_shader *shader,
5151 struct ac_rtld_binary *rtld)
5152 {
5153 const struct si_shader_selector *sel = shader->selector;
5154 enum pipe_shader_type shader_type = sel ? sel->type : PIPE_SHADER_COMPUTE;
5155 const char *part_elfs[5];
5156 size_t part_sizes[5];
5157 unsigned num_parts = 0;
5158
5159 #define add_part(shader_or_part) \
5160 if (shader_or_part) { \
5161 part_elfs[num_parts] = (shader_or_part)->binary.elf_buffer; \
5162 part_sizes[num_parts] = (shader_or_part)->binary.elf_size; \
5163 num_parts++; \
5164 }
5165
5166 add_part(shader->prolog);
5167 add_part(shader->previous_stage);
5168 add_part(shader->prolog2);
5169 add_part(shader);
5170 add_part(shader->epilog);
5171
5172 #undef add_part
5173
5174 struct ac_rtld_symbol lds_symbols[2];
5175 unsigned num_lds_symbols = 0;
5176
5177 if (sel && screen->info.chip_class >= GFX9 &&
5178 sel->type == PIPE_SHADER_GEOMETRY && !shader->is_gs_copy_shader) {
5179 /* We add this symbol even on LLVM <= 8 to ensure that
5180 * shader->config.lds_size is set correctly below.
5181 */
5182 struct ac_rtld_symbol *sym = &lds_symbols[num_lds_symbols++];
5183 sym->name = "esgs_ring";
5184 sym->size = shader->gs_info.esgs_ring_size;
5185 sym->align = 64 * 1024;
5186 }
5187
5188 if (shader->key.as_ngg && sel->type == PIPE_SHADER_GEOMETRY) {
5189 struct ac_rtld_symbol *sym = &lds_symbols[num_lds_symbols++];
5190 sym->name = "ngg_emit";
5191 sym->size = shader->ngg.ngg_emit_size * 4;
5192 sym->align = 4;
5193 }
5194
5195 bool ok = ac_rtld_open(rtld, (struct ac_rtld_open_info){
5196 .info = &screen->info,
5197 .options = {
5198 .halt_at_entry = screen->options.halt_shaders,
5199 },
5200 .shader_type = tgsi_processor_to_shader_stage(shader_type),
5201 .num_parts = num_parts,
5202 .elf_ptrs = part_elfs,
5203 .elf_sizes = part_sizes,
5204 .num_shared_lds_symbols = num_lds_symbols,
5205 .shared_lds_symbols = lds_symbols });
5206
5207 if (rtld->lds_size > 0) {
5208 unsigned alloc_granularity = screen->info.chip_class >= GFX7 ? 512 : 256;
5209 shader->config.lds_size =
5210 align(rtld->lds_size, alloc_granularity) / alloc_granularity;
5211 }
5212
5213 return ok;
5214 }
5215
5216 static unsigned si_get_shader_binary_size(struct si_screen *screen, struct si_shader *shader)
5217 {
5218 struct ac_rtld_binary rtld;
5219 si_shader_binary_open(screen, shader, &rtld);
5220 return rtld.rx_size;
5221 }
5222
5223 static bool si_get_external_symbol(void *data, const char *name, uint64_t *value)
5224 {
5225 uint64_t *scratch_va = data;
5226
5227 if (!strcmp(scratch_rsrc_dword0_symbol, name)) {
5228 *value = (uint32_t)*scratch_va;
5229 return true;
5230 }
5231 if (!strcmp(scratch_rsrc_dword1_symbol, name)) {
5232 /* Enable scratch coalescing. */
5233 *value = S_008F04_BASE_ADDRESS_HI(*scratch_va >> 32) |
5234 S_008F04_SWIZZLE_ENABLE(1);
5235 if (HAVE_LLVM < 0x0800) {
5236 /* Old LLVM created an R_ABS32_HI relocation for
5237 * this symbol. */
5238 *value <<= 32;
5239 }
5240 return true;
5241 }
5242
5243 return false;
5244 }
5245
5246 bool si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader,
5247 uint64_t scratch_va)
5248 {
5249 struct ac_rtld_binary binary;
5250 if (!si_shader_binary_open(sscreen, shader, &binary))
5251 return false;
5252
5253 si_resource_reference(&shader->bo, NULL);
5254 shader->bo = si_aligned_buffer_create(&sscreen->b,
5255 sscreen->cpdma_prefetch_writes_memory ?
5256 0 : SI_RESOURCE_FLAG_READ_ONLY,
5257 PIPE_USAGE_IMMUTABLE,
5258 align(binary.rx_size, SI_CPDMA_ALIGNMENT),
5259 256);
5260 if (!shader->bo)
5261 return false;
5262
5263 /* Upload. */
5264 struct ac_rtld_upload_info u = {};
5265 u.binary = &binary;
5266 u.get_external_symbol = si_get_external_symbol;
5267 u.cb_data = &scratch_va;
5268 u.rx_va = shader->bo->gpu_address;
5269 u.rx_ptr = sscreen->ws->buffer_map(shader->bo->buf, NULL,
5270 PIPE_TRANSFER_READ_WRITE |
5271 PIPE_TRANSFER_UNSYNCHRONIZED |
5272 RADEON_TRANSFER_TEMPORARY);
5273 if (!u.rx_ptr)
5274 return false;
5275
5276 bool ok = ac_rtld_upload(&u);
5277
5278 sscreen->ws->buffer_unmap(shader->bo->buf);
5279 ac_rtld_close(&binary);
5280
5281 return ok;
5282 }
5283
5284 static void si_shader_dump_disassembly(struct si_screen *screen,
5285 const struct si_shader_binary *binary,
5286 struct pipe_debug_callback *debug,
5287 const char *name, FILE *file)
5288 {
5289 struct ac_rtld_binary rtld_binary;
5290
5291 if (!ac_rtld_open(&rtld_binary, (struct ac_rtld_open_info){
5292 .info = &screen->info,
5293 .num_parts = 1,
5294 .elf_ptrs = &binary->elf_buffer,
5295 .elf_sizes = &binary->elf_size }))
5296 return;
5297
5298 const char *disasm;
5299 size_t nbytes;
5300
5301 if (!ac_rtld_get_section_by_name(&rtld_binary, ".AMDGPU.disasm", &disasm, &nbytes))
5302 goto out;
5303
5304 if (nbytes > INT_MAX)
5305 goto out;
5306
5307 if (debug && debug->debug_message) {
5308 /* Very long debug messages are cut off, so send the
5309 * disassembly one line at a time. This causes more
5310 * overhead, but on the plus side it simplifies
5311 * parsing of resulting logs.
5312 */
5313 pipe_debug_message(debug, SHADER_INFO,
5314 "Shader Disassembly Begin");
5315
5316 uint64_t line = 0;
5317 while (line < nbytes) {
5318 int count = nbytes - line;
5319 const char *nl = memchr(disasm + line, '\n', nbytes - line);
5320 if (nl)
5321 count = nl - (disasm + line);
5322
5323 if (count) {
5324 pipe_debug_message(debug, SHADER_INFO,
5325 "%.*s", count, disasm + line);
5326 }
5327
5328 line += count + 1;
5329 }
5330
5331 pipe_debug_message(debug, SHADER_INFO,
5332 "Shader Disassembly End");
5333 }
5334
5335 if (file) {
5336 fprintf(file, "Shader %s disassembly:\n", name);
5337 fprintf(file, "%*s", (int)nbytes, disasm);
5338 }
5339
5340 out:
5341 ac_rtld_close(&rtld_binary);
5342 }
5343
5344 static void si_calculate_max_simd_waves(struct si_shader *shader)
5345 {
5346 struct si_screen *sscreen = shader->selector->screen;
5347 struct ac_shader_config *conf = &shader->config;
5348 unsigned num_inputs = shader->selector->info.num_inputs;
5349 unsigned lds_increment = sscreen->info.chip_class >= GFX7 ? 512 : 256;
5350 unsigned lds_per_wave = 0;
5351 unsigned max_simd_waves;
5352
5353 max_simd_waves = ac_get_max_simd_waves(sscreen->info.family);
5354
5355 /* Compute LDS usage for PS. */
5356 switch (shader->selector->type) {
5357 case PIPE_SHADER_FRAGMENT:
5358 /* The minimum usage per wave is (num_inputs * 48). The maximum
5359 * usage is (num_inputs * 48 * 16).
5360 * We can get anything in between and it varies between waves.
5361 *
5362 * The 48 bytes per input for a single primitive is equal to
5363 * 4 bytes/component * 4 components/input * 3 points.
5364 *
5365 * Other stages don't know the size at compile time or don't
5366 * allocate LDS per wave, but instead they do it per thread group.
5367 */
5368 lds_per_wave = conf->lds_size * lds_increment +
5369 align(num_inputs * 48, lds_increment);
5370 break;
5371 case PIPE_SHADER_COMPUTE:
5372 if (shader->selector) {
5373 unsigned max_workgroup_size =
5374 si_get_max_workgroup_size(shader);
5375 lds_per_wave = (conf->lds_size * lds_increment) /
5376 DIV_ROUND_UP(max_workgroup_size, 64);
5377 }
5378 break;
5379 }
5380
5381 /* Compute the per-SIMD wave counts. */
5382 if (conf->num_sgprs) {
5383 max_simd_waves =
5384 MIN2(max_simd_waves,
5385 ac_get_num_physical_sgprs(sscreen->info.chip_class) / conf->num_sgprs);
5386 }
5387
5388 if (conf->num_vgprs)
5389 max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs);
5390
5391 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
5392 * 16KB makes some SIMDs unoccupied). */
5393 if (lds_per_wave)
5394 max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
5395
5396 shader->info.max_simd_waves = max_simd_waves;
5397 }
5398
5399 void si_shader_dump_stats_for_shader_db(struct si_screen *screen,
5400 struct si_shader *shader,
5401 struct pipe_debug_callback *debug)
5402 {
5403 const struct ac_shader_config *conf = &shader->config;
5404
5405 if (screen->options.debug_disassembly)
5406 si_shader_dump_disassembly(screen, &shader->binary, debug, "main", NULL);
5407
5408 pipe_debug_message(debug, SHADER_INFO,
5409 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
5410 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
5411 "Spilled VGPRs: %d PrivMem VGPRs: %d",
5412 conf->num_sgprs, conf->num_vgprs,
5413 si_get_shader_binary_size(screen, shader),
5414 conf->lds_size, conf->scratch_bytes_per_wave,
5415 shader->info.max_simd_waves, conf->spilled_sgprs,
5416 conf->spilled_vgprs, shader->info.private_mem_vgprs);
5417 }
5418
5419 static void si_shader_dump_stats(struct si_screen *sscreen,
5420 struct si_shader *shader,
5421 unsigned processor,
5422 FILE *file,
5423 bool check_debug_option)
5424 {
5425 const struct ac_shader_config *conf = &shader->config;
5426
5427 if (!check_debug_option ||
5428 si_can_dump_shader(sscreen, processor)) {
5429 if (processor == PIPE_SHADER_FRAGMENT) {
5430 fprintf(file, "*** SHADER CONFIG ***\n"
5431 "SPI_PS_INPUT_ADDR = 0x%04x\n"
5432 "SPI_PS_INPUT_ENA = 0x%04x\n",
5433 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
5434 }
5435
5436 fprintf(file, "*** SHADER STATS ***\n"
5437 "SGPRS: %d\n"
5438 "VGPRS: %d\n"
5439 "Spilled SGPRs: %d\n"
5440 "Spilled VGPRs: %d\n"
5441 "Private memory VGPRs: %d\n"
5442 "Code Size: %d bytes\n"
5443 "LDS: %d blocks\n"
5444 "Scratch: %d bytes per wave\n"
5445 "Max Waves: %d\n"
5446 "********************\n\n\n",
5447 conf->num_sgprs, conf->num_vgprs,
5448 conf->spilled_sgprs, conf->spilled_vgprs,
5449 shader->info.private_mem_vgprs,
5450 si_get_shader_binary_size(sscreen, shader),
5451 conf->lds_size, conf->scratch_bytes_per_wave,
5452 shader->info.max_simd_waves);
5453 }
5454 }
5455
5456 const char *si_get_shader_name(const struct si_shader *shader, unsigned processor)
5457 {
5458 switch (processor) {
5459 case PIPE_SHADER_VERTEX:
5460 if (shader->key.as_es)
5461 return "Vertex Shader as ES";
5462 else if (shader->key.as_ls)
5463 return "Vertex Shader as LS";
5464 else if (shader->key.opt.vs_as_prim_discard_cs)
5465 return "Vertex Shader as Primitive Discard CS";
5466 else if (shader->key.as_ngg)
5467 return "Vertex Shader as ESGS";
5468 else
5469 return "Vertex Shader as VS";
5470 case PIPE_SHADER_TESS_CTRL:
5471 return "Tessellation Control Shader";
5472 case PIPE_SHADER_TESS_EVAL:
5473 if (shader->key.as_es)
5474 return "Tessellation Evaluation Shader as ES";
5475 else if (shader->key.as_ngg)
5476 return "Tessellation Evaluation Shader as ESGS";
5477 else
5478 return "Tessellation Evaluation Shader as VS";
5479 case PIPE_SHADER_GEOMETRY:
5480 if (shader->is_gs_copy_shader)
5481 return "GS Copy Shader as VS";
5482 else
5483 return "Geometry Shader";
5484 case PIPE_SHADER_FRAGMENT:
5485 return "Pixel Shader";
5486 case PIPE_SHADER_COMPUTE:
5487 return "Compute Shader";
5488 default:
5489 return "Unknown Shader";
5490 }
5491 }
5492
5493 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
5494 struct pipe_debug_callback *debug, unsigned processor,
5495 FILE *file, bool check_debug_option)
5496 {
5497 if (!check_debug_option ||
5498 si_can_dump_shader(sscreen, processor))
5499 si_dump_shader_key(processor, shader, file);
5500
5501 if (!check_debug_option && shader->binary.llvm_ir_string) {
5502 if (shader->previous_stage &&
5503 shader->previous_stage->binary.llvm_ir_string) {
5504 fprintf(file, "\n%s - previous stage - LLVM IR:\n\n",
5505 si_get_shader_name(shader, processor));
5506 fprintf(file, "%s\n", shader->previous_stage->binary.llvm_ir_string);
5507 }
5508
5509 fprintf(file, "\n%s - main shader part - LLVM IR:\n\n",
5510 si_get_shader_name(shader, processor));
5511 fprintf(file, "%s\n", shader->binary.llvm_ir_string);
5512 }
5513
5514 if (!check_debug_option ||
5515 (si_can_dump_shader(sscreen, processor) &&
5516 !(sscreen->debug_flags & DBG(NO_ASM)))) {
5517 fprintf(file, "\n%s:\n", si_get_shader_name(shader, processor));
5518
5519 if (shader->prolog)
5520 si_shader_dump_disassembly(sscreen, &shader->prolog->binary,
5521 debug, "prolog", file);
5522 if (shader->previous_stage)
5523 si_shader_dump_disassembly(sscreen, &shader->previous_stage->binary,
5524 debug, "previous stage", file);
5525 if (shader->prolog2)
5526 si_shader_dump_disassembly(sscreen, &shader->prolog2->binary,
5527 debug, "prolog2", file);
5528
5529 si_shader_dump_disassembly(sscreen, &shader->binary, debug, "main", file);
5530
5531 if (shader->epilog)
5532 si_shader_dump_disassembly(sscreen, &shader->epilog->binary,
5533 debug, "epilog", file);
5534 fprintf(file, "\n");
5535 }
5536
5537 si_shader_dump_stats(sscreen, shader, processor, file,
5538 check_debug_option);
5539 }
5540
5541 static int si_compile_llvm(struct si_screen *sscreen,
5542 struct si_shader_binary *binary,
5543 struct ac_shader_config *conf,
5544 struct ac_llvm_compiler *compiler,
5545 LLVMModuleRef mod,
5546 struct pipe_debug_callback *debug,
5547 unsigned processor,
5548 const char *name,
5549 bool less_optimized)
5550 {
5551 unsigned count = p_atomic_inc_return(&sscreen->num_compilations);
5552
5553 if (si_can_dump_shader(sscreen, processor)) {
5554 fprintf(stderr, "radeonsi: Compiling shader %d\n", count);
5555
5556 if (!(sscreen->debug_flags & (DBG(NO_IR) | DBG(PREOPT_IR)))) {
5557 fprintf(stderr, "%s LLVM IR:\n\n", name);
5558 ac_dump_module(mod);
5559 fprintf(stderr, "\n");
5560 }
5561 }
5562
5563 if (sscreen->record_llvm_ir) {
5564 char *ir = LLVMPrintModuleToString(mod);
5565 binary->llvm_ir_string = strdup(ir);
5566 LLVMDisposeMessage(ir);
5567 }
5568
5569 if (!si_replace_shader(count, binary)) {
5570 unsigned r = si_llvm_compile(mod, binary, compiler, debug,
5571 less_optimized);
5572 if (r)
5573 return r;
5574 }
5575
5576 struct ac_rtld_binary rtld;
5577 if (!ac_rtld_open(&rtld, (struct ac_rtld_open_info){
5578 .info = &sscreen->info,
5579 .num_parts = 1,
5580 .elf_ptrs = &binary->elf_buffer,
5581 .elf_sizes = &binary->elf_size }))
5582 return -1;
5583
5584 bool ok = ac_rtld_read_config(&rtld, conf);
5585 ac_rtld_close(&rtld);
5586 if (!ok)
5587 return -1;
5588
5589 /* Enable 64-bit and 16-bit denormals, because there is no performance
5590 * cost.
5591 *
5592 * If denormals are enabled, all floating-point output modifiers are
5593 * ignored.
5594 *
5595 * Don't enable denormals for 32-bit floats, because:
5596 * - Floating-point output modifiers would be ignored by the hw.
5597 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5598 * have to stop using those.
5599 * - GFX6 & GFX7 would be very slow.
5600 */
5601 conf->float_mode |= V_00B028_FP_64_DENORMS;
5602
5603 return 0;
5604 }
5605
5606 static void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret)
5607 {
5608 if (LLVMGetTypeKind(LLVMTypeOf(ret)) == LLVMVoidTypeKind)
5609 LLVMBuildRetVoid(ctx->ac.builder);
5610 else
5611 LLVMBuildRet(ctx->ac.builder, ret);
5612 }
5613
5614 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5615 struct si_shader *
5616 si_generate_gs_copy_shader(struct si_screen *sscreen,
5617 struct ac_llvm_compiler *compiler,
5618 struct si_shader_selector *gs_selector,
5619 struct pipe_debug_callback *debug)
5620 {
5621 struct si_shader_context ctx;
5622 struct si_shader *shader;
5623 LLVMBuilderRef builder;
5624 struct si_shader_output_values outputs[SI_MAX_VS_OUTPUTS];
5625 struct tgsi_shader_info *gsinfo = &gs_selector->info;
5626 int i;
5627
5628
5629 shader = CALLOC_STRUCT(si_shader);
5630 if (!shader)
5631 return NULL;
5632
5633 /* We can leave the fence as permanently signaled because the GS copy
5634 * shader only becomes visible globally after it has been compiled. */
5635 util_queue_fence_init(&shader->ready);
5636
5637 shader->selector = gs_selector;
5638 shader->is_gs_copy_shader = true;
5639
5640 si_init_shader_ctx(&ctx, sscreen, compiler);
5641 ctx.shader = shader;
5642 ctx.type = PIPE_SHADER_VERTEX;
5643
5644 builder = ctx.ac.builder;
5645
5646 create_function(&ctx);
5647 preload_ring_buffers(&ctx);
5648
5649 LLVMValueRef voffset =
5650 LLVMBuildMul(ctx.ac.builder, ctx.abi.vertex_id,
5651 LLVMConstInt(ctx.i32, 4, 0), "");
5652
5653 /* Fetch the vertex stream ID.*/
5654 LLVMValueRef stream_id;
5655
5656 if (gs_selector->so.num_outputs)
5657 stream_id = si_unpack_param(&ctx, ctx.param_streamout_config, 24, 2);
5658 else
5659 stream_id = ctx.i32_0;
5660
5661 /* Fill in output information. */
5662 for (i = 0; i < gsinfo->num_outputs; ++i) {
5663 outputs[i].semantic_name = gsinfo->output_semantic_name[i];
5664 outputs[i].semantic_index = gsinfo->output_semantic_index[i];
5665
5666 for (int chan = 0; chan < 4; chan++) {
5667 outputs[i].vertex_stream[chan] =
5668 (gsinfo->output_streams[i] >> (2 * chan)) & 3;
5669 }
5670 }
5671
5672 LLVMBasicBlockRef end_bb;
5673 LLVMValueRef switch_inst;
5674
5675 end_bb = LLVMAppendBasicBlockInContext(ctx.ac.context, ctx.main_fn, "end");
5676 switch_inst = LLVMBuildSwitch(builder, stream_id, end_bb, 4);
5677
5678 for (int stream = 0; stream < 4; stream++) {
5679 LLVMBasicBlockRef bb;
5680 unsigned offset;
5681
5682 if (!gsinfo->num_stream_output_components[stream])
5683 continue;
5684
5685 if (stream > 0 && !gs_selector->so.num_outputs)
5686 continue;
5687
5688 bb = LLVMInsertBasicBlockInContext(ctx.ac.context, end_bb, "out");
5689 LLVMAddCase(switch_inst, LLVMConstInt(ctx.i32, stream, 0), bb);
5690 LLVMPositionBuilderAtEnd(builder, bb);
5691
5692 /* Fetch vertex data from GSVS ring */
5693 offset = 0;
5694 for (i = 0; i < gsinfo->num_outputs; ++i) {
5695 for (unsigned chan = 0; chan < 4; chan++) {
5696 if (!(gsinfo->output_usagemask[i] & (1 << chan)) ||
5697 outputs[i].vertex_stream[chan] != stream) {
5698 outputs[i].values[chan] = LLVMGetUndef(ctx.f32);
5699 continue;
5700 }
5701
5702 LLVMValueRef soffset = LLVMConstInt(ctx.i32,
5703 offset * gs_selector->gs_max_out_vertices * 16 * 4, 0);
5704 offset++;
5705
5706 outputs[i].values[chan] =
5707 ac_build_buffer_load(&ctx.ac,
5708 ctx.gsvs_ring[0], 1,
5709 ctx.i32_0, voffset,
5710 soffset, 0, 1, 1,
5711 true, false);
5712 }
5713 }
5714
5715 /* Streamout and exports. */
5716 if (gs_selector->so.num_outputs) {
5717 si_llvm_emit_streamout(&ctx, outputs,
5718 gsinfo->num_outputs,
5719 stream);
5720 }
5721
5722 if (stream == 0)
5723 si_llvm_export_vs(&ctx, outputs, gsinfo->num_outputs);
5724
5725 LLVMBuildBr(builder, end_bb);
5726 }
5727
5728 LLVMPositionBuilderAtEnd(builder, end_bb);
5729
5730 LLVMBuildRetVoid(ctx.ac.builder);
5731
5732 ctx.type = PIPE_SHADER_GEOMETRY; /* override for shader dumping */
5733 si_llvm_optimize_module(&ctx);
5734
5735 bool ok = false;
5736 if (si_compile_llvm(sscreen, &ctx.shader->binary,
5737 &ctx.shader->config, ctx.compiler,
5738 ctx.ac.module,
5739 debug, PIPE_SHADER_GEOMETRY,
5740 "GS Copy Shader", false) == 0) {
5741 if (si_can_dump_shader(sscreen, PIPE_SHADER_GEOMETRY))
5742 fprintf(stderr, "GS Copy Shader:\n");
5743 si_shader_dump(sscreen, ctx.shader, debug,
5744 PIPE_SHADER_GEOMETRY, stderr, true);
5745
5746 if (!ctx.shader->config.scratch_bytes_per_wave)
5747 ok = si_shader_binary_upload(sscreen, ctx.shader, 0);
5748 else
5749 ok = true;
5750 }
5751
5752 si_llvm_dispose(&ctx);
5753
5754 if (!ok) {
5755 FREE(shader);
5756 shader = NULL;
5757 } else {
5758 si_fix_resource_usage(sscreen, shader);
5759 }
5760 return shader;
5761 }
5762
5763 static void si_dump_shader_key_vs(const struct si_shader_key *key,
5764 const struct si_vs_prolog_bits *prolog,
5765 const char *prefix, FILE *f)
5766 {
5767 fprintf(f, " %s.instance_divisor_is_one = %u\n",
5768 prefix, prolog->instance_divisor_is_one);
5769 fprintf(f, " %s.instance_divisor_is_fetched = %u\n",
5770 prefix, prolog->instance_divisor_is_fetched);
5771 fprintf(f, " %s.unpack_instance_id_from_vertex_id = %u\n",
5772 prefix, prolog->unpack_instance_id_from_vertex_id);
5773 fprintf(f, " %s.ls_vgpr_fix = %u\n",
5774 prefix, prolog->ls_vgpr_fix);
5775
5776 fprintf(f, " mono.vs.fetch_opencode = %x\n", key->mono.vs_fetch_opencode);
5777 fprintf(f, " mono.vs.fix_fetch = {");
5778 for (int i = 0; i < SI_MAX_ATTRIBS; i++) {
5779 union si_vs_fix_fetch fix = key->mono.vs_fix_fetch[i];
5780 if (i)
5781 fprintf(f, ", ");
5782 if (!fix.bits)
5783 fprintf(f, "0");
5784 else
5785 fprintf(f, "%u.%u.%u.%u", fix.u.reverse, fix.u.log_size,
5786 fix.u.num_channels_m1, fix.u.format);
5787 }
5788 fprintf(f, "}\n");
5789 }
5790
5791 static void si_dump_shader_key(unsigned processor, const struct si_shader *shader,
5792 FILE *f)
5793 {
5794 const struct si_shader_key *key = &shader->key;
5795
5796 fprintf(f, "SHADER KEY\n");
5797
5798 switch (processor) {
5799 case PIPE_SHADER_VERTEX:
5800 si_dump_shader_key_vs(key, &key->part.vs.prolog,
5801 "part.vs.prolog", f);
5802 fprintf(f, " as_es = %u\n", key->as_es);
5803 fprintf(f, " as_ls = %u\n", key->as_ls);
5804 fprintf(f, " mono.u.vs_export_prim_id = %u\n",
5805 key->mono.u.vs_export_prim_id);
5806 fprintf(f, " opt.vs_as_prim_discard_cs = %u\n",
5807 key->opt.vs_as_prim_discard_cs);
5808 fprintf(f, " opt.cs_prim_type = %s\n",
5809 tgsi_primitive_names[key->opt.cs_prim_type]);
5810 fprintf(f, " opt.cs_indexed = %u\n",
5811 key->opt.cs_indexed);
5812 fprintf(f, " opt.cs_instancing = %u\n",
5813 key->opt.cs_instancing);
5814 fprintf(f, " opt.cs_primitive_restart = %u\n",
5815 key->opt.cs_primitive_restart);
5816 fprintf(f, " opt.cs_provoking_vertex_first = %u\n",
5817 key->opt.cs_provoking_vertex_first);
5818 fprintf(f, " opt.cs_need_correct_orientation = %u\n",
5819 key->opt.cs_need_correct_orientation);
5820 fprintf(f, " opt.cs_cull_front = %u\n",
5821 key->opt.cs_cull_front);
5822 fprintf(f, " opt.cs_cull_back = %u\n",
5823 key->opt.cs_cull_back);
5824 fprintf(f, " opt.cs_cull_z = %u\n",
5825 key->opt.cs_cull_z);
5826 fprintf(f, " opt.cs_halfz_clip_space = %u\n",
5827 key->opt.cs_halfz_clip_space);
5828 break;
5829
5830 case PIPE_SHADER_TESS_CTRL:
5831 if (shader->selector->screen->info.chip_class >= GFX9) {
5832 si_dump_shader_key_vs(key, &key->part.tcs.ls_prolog,
5833 "part.tcs.ls_prolog", f);
5834 }
5835 fprintf(f, " part.tcs.epilog.prim_mode = %u\n", key->part.tcs.epilog.prim_mode);
5836 fprintf(f, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64"\n", key->mono.u.ff_tcs_inputs_to_copy);
5837 break;
5838
5839 case PIPE_SHADER_TESS_EVAL:
5840 fprintf(f, " as_es = %u\n", key->as_es);
5841 fprintf(f, " mono.u.vs_export_prim_id = %u\n",
5842 key->mono.u.vs_export_prim_id);
5843 break;
5844
5845 case PIPE_SHADER_GEOMETRY:
5846 if (shader->is_gs_copy_shader)
5847 break;
5848
5849 if (shader->selector->screen->info.chip_class >= GFX9 &&
5850 key->part.gs.es->type == PIPE_SHADER_VERTEX) {
5851 si_dump_shader_key_vs(key, &key->part.gs.vs_prolog,
5852 "part.gs.vs_prolog", f);
5853 }
5854 fprintf(f, " part.gs.prolog.tri_strip_adj_fix = %u\n", key->part.gs.prolog.tri_strip_adj_fix);
5855 break;
5856
5857 case PIPE_SHADER_COMPUTE:
5858 break;
5859
5860 case PIPE_SHADER_FRAGMENT:
5861 fprintf(f, " part.ps.prolog.color_two_side = %u\n", key->part.ps.prolog.color_two_side);
5862 fprintf(f, " part.ps.prolog.flatshade_colors = %u\n", key->part.ps.prolog.flatshade_colors);
5863 fprintf(f, " part.ps.prolog.poly_stipple = %u\n", key->part.ps.prolog.poly_stipple);
5864 fprintf(f, " part.ps.prolog.force_persp_sample_interp = %u\n", key->part.ps.prolog.force_persp_sample_interp);
5865 fprintf(f, " part.ps.prolog.force_linear_sample_interp = %u\n", key->part.ps.prolog.force_linear_sample_interp);
5866 fprintf(f, " part.ps.prolog.force_persp_center_interp = %u\n", key->part.ps.prolog.force_persp_center_interp);
5867 fprintf(f, " part.ps.prolog.force_linear_center_interp = %u\n", key->part.ps.prolog.force_linear_center_interp);
5868 fprintf(f, " part.ps.prolog.bc_optimize_for_persp = %u\n", key->part.ps.prolog.bc_optimize_for_persp);
5869 fprintf(f, " part.ps.prolog.bc_optimize_for_linear = %u\n", key->part.ps.prolog.bc_optimize_for_linear);
5870 fprintf(f, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key->part.ps.epilog.spi_shader_col_format);
5871 fprintf(f, " part.ps.epilog.color_is_int8 = 0x%X\n", key->part.ps.epilog.color_is_int8);
5872 fprintf(f, " part.ps.epilog.color_is_int10 = 0x%X\n", key->part.ps.epilog.color_is_int10);
5873 fprintf(f, " part.ps.epilog.last_cbuf = %u\n", key->part.ps.epilog.last_cbuf);
5874 fprintf(f, " part.ps.epilog.alpha_func = %u\n", key->part.ps.epilog.alpha_func);
5875 fprintf(f, " part.ps.epilog.alpha_to_one = %u\n", key->part.ps.epilog.alpha_to_one);
5876 fprintf(f, " part.ps.epilog.poly_line_smoothing = %u\n", key->part.ps.epilog.poly_line_smoothing);
5877 fprintf(f, " part.ps.epilog.clamp_color = %u\n", key->part.ps.epilog.clamp_color);
5878 break;
5879
5880 default:
5881 assert(0);
5882 }
5883
5884 if ((processor == PIPE_SHADER_GEOMETRY ||
5885 processor == PIPE_SHADER_TESS_EVAL ||
5886 processor == PIPE_SHADER_VERTEX) &&
5887 !key->as_es && !key->as_ls) {
5888 fprintf(f, " opt.kill_outputs = 0x%"PRIx64"\n", key->opt.kill_outputs);
5889 fprintf(f, " opt.clip_disable = %u\n", key->opt.clip_disable);
5890 }
5891 }
5892
5893 static void si_init_shader_ctx(struct si_shader_context *ctx,
5894 struct si_screen *sscreen,
5895 struct ac_llvm_compiler *compiler)
5896 {
5897 struct lp_build_tgsi_context *bld_base;
5898
5899 si_llvm_context_init(ctx, sscreen, compiler);
5900
5901 bld_base = &ctx->bld_base;
5902 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
5903
5904 bld_base->op_actions[TGSI_OPCODE_INTERP_CENTROID].emit = build_interp_intrinsic;
5905 bld_base->op_actions[TGSI_OPCODE_INTERP_SAMPLE].emit = build_interp_intrinsic;
5906 bld_base->op_actions[TGSI_OPCODE_INTERP_OFFSET].emit = build_interp_intrinsic;
5907
5908 bld_base->op_actions[TGSI_OPCODE_MEMBAR].emit = membar_emit;
5909
5910 bld_base->op_actions[TGSI_OPCODE_CLOCK].emit = clock_emit;
5911
5912 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
5913 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
5914 bld_base->op_actions[TGSI_OPCODE_DDX_FINE].emit = si_llvm_emit_ddxy;
5915 bld_base->op_actions[TGSI_OPCODE_DDY_FINE].emit = si_llvm_emit_ddxy;
5916
5917 bld_base->op_actions[TGSI_OPCODE_VOTE_ALL].emit = vote_all_emit;
5918 bld_base->op_actions[TGSI_OPCODE_VOTE_ANY].emit = vote_any_emit;
5919 bld_base->op_actions[TGSI_OPCODE_VOTE_EQ].emit = vote_eq_emit;
5920 bld_base->op_actions[TGSI_OPCODE_BALLOT].emit = ballot_emit;
5921 bld_base->op_actions[TGSI_OPCODE_READ_FIRST].intr_name = "llvm.amdgcn.readfirstlane";
5922 bld_base->op_actions[TGSI_OPCODE_READ_FIRST].emit = read_lane_emit;
5923 bld_base->op_actions[TGSI_OPCODE_READ_INVOC].intr_name = "llvm.amdgcn.readlane";
5924 bld_base->op_actions[TGSI_OPCODE_READ_INVOC].emit = read_lane_emit;
5925
5926 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_tgsi_emit_vertex;
5927 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_tgsi_emit_primitive;
5928 bld_base->op_actions[TGSI_OPCODE_BARRIER].emit = si_llvm_emit_barrier;
5929 }
5930
5931 static void si_optimize_vs_outputs(struct si_shader_context *ctx)
5932 {
5933 struct si_shader *shader = ctx->shader;
5934 struct tgsi_shader_info *info = &shader->selector->info;
5935
5936 if ((ctx->type != PIPE_SHADER_VERTEX &&
5937 ctx->type != PIPE_SHADER_TESS_EVAL) ||
5938 shader->key.as_ls ||
5939 shader->key.as_es)
5940 return;
5941
5942 ac_optimize_vs_outputs(&ctx->ac,
5943 ctx->main_fn,
5944 shader->info.vs_output_param_offset,
5945 info->num_outputs,
5946 &shader->info.nr_param_exports);
5947 }
5948
5949 static void si_init_exec_from_input(struct si_shader_context *ctx,
5950 unsigned param, unsigned bitoffset)
5951 {
5952 LLVMValueRef args[] = {
5953 LLVMGetParam(ctx->main_fn, param),
5954 LLVMConstInt(ctx->i32, bitoffset, 0),
5955 };
5956 ac_build_intrinsic(&ctx->ac,
5957 "llvm.amdgcn.init.exec.from.input",
5958 ctx->voidt, args, 2, AC_FUNC_ATTR_CONVERGENT);
5959 }
5960
5961 static bool si_vs_needs_prolog(const struct si_shader_selector *sel,
5962 const struct si_vs_prolog_bits *key)
5963 {
5964 /* VGPR initialization fixup for Vega10 and Raven is always done in the
5965 * VS prolog. */
5966 return sel->vs_needs_prolog || key->ls_vgpr_fix;
5967 }
5968
5969 static bool si_compile_tgsi_main(struct si_shader_context *ctx)
5970 {
5971 struct si_shader *shader = ctx->shader;
5972 struct si_shader_selector *sel = shader->selector;
5973 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
5974
5975 // TODO clean all this up!
5976 switch (ctx->type) {
5977 case PIPE_SHADER_VERTEX:
5978 ctx->load_input = declare_input_vs;
5979 if (shader->key.as_ls)
5980 ctx->abi.emit_outputs = si_llvm_emit_ls_epilogue;
5981 else if (shader->key.as_es)
5982 ctx->abi.emit_outputs = si_llvm_emit_es_epilogue;
5983 else if (shader->key.opt.vs_as_prim_discard_cs)
5984 ctx->abi.emit_outputs = si_llvm_emit_prim_discard_cs_epilogue;
5985 else if (shader->key.as_ngg)
5986 ctx->abi.emit_outputs = gfx10_emit_ngg_epilogue;
5987 else
5988 ctx->abi.emit_outputs = si_llvm_emit_vs_epilogue;
5989 bld_base->emit_epilogue = si_tgsi_emit_epilogue;
5990 ctx->abi.load_base_vertex = get_base_vertex;
5991 break;
5992 case PIPE_SHADER_TESS_CTRL:
5993 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_tcs;
5994 ctx->abi.load_tess_varyings = si_nir_load_tcs_varyings;
5995 bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = fetch_output_tcs;
5996 bld_base->emit_store = store_output_tcs;
5997 ctx->abi.store_tcs_outputs = si_nir_store_output_tcs;
5998 ctx->abi.emit_outputs = si_llvm_emit_tcs_epilogue;
5999 ctx->abi.load_patch_vertices_in = si_load_patch_vertices_in;
6000 bld_base->emit_epilogue = si_tgsi_emit_epilogue;
6001 break;
6002 case PIPE_SHADER_TESS_EVAL:
6003 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_tes;
6004 ctx->abi.load_tess_varyings = si_nir_load_input_tes;
6005 ctx->abi.load_tess_coord = si_load_tess_coord;
6006 ctx->abi.load_tess_level = si_load_tess_level;
6007 ctx->abi.load_patch_vertices_in = si_load_patch_vertices_in;
6008 if (shader->key.as_es)
6009 ctx->abi.emit_outputs = si_llvm_emit_es_epilogue;
6010 else {
6011 if (shader->key.as_ngg)
6012 ctx->abi.emit_outputs = gfx10_emit_ngg_epilogue;
6013 else
6014 ctx->abi.emit_outputs = si_llvm_emit_vs_epilogue;
6015 }
6016 bld_base->emit_epilogue = si_tgsi_emit_epilogue;
6017 break;
6018 case PIPE_SHADER_GEOMETRY:
6019 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
6020 ctx->abi.load_inputs = si_nir_load_input_gs;
6021 ctx->abi.emit_vertex = si_llvm_emit_vertex;
6022 ctx->abi.emit_primitive = si_llvm_emit_primitive;
6023 ctx->abi.emit_outputs = si_llvm_emit_gs_epilogue;
6024 bld_base->emit_epilogue = si_tgsi_emit_gs_epilogue;
6025 break;
6026 case PIPE_SHADER_FRAGMENT:
6027 ctx->load_input = declare_input_fs;
6028 ctx->abi.emit_outputs = si_llvm_return_fs_outputs;
6029 bld_base->emit_epilogue = si_tgsi_emit_epilogue;
6030 ctx->abi.lookup_interp_param = si_nir_lookup_interp_param;
6031 ctx->abi.load_sample_position = load_sample_position;
6032 ctx->abi.load_sample_mask_in = load_sample_mask_in;
6033 ctx->abi.emit_kill = si_llvm_emit_kill;
6034 break;
6035 case PIPE_SHADER_COMPUTE:
6036 ctx->abi.load_local_group_size = get_block_size;
6037 break;
6038 default:
6039 assert(!"Unsupported shader type");
6040 return false;
6041 }
6042
6043 ctx->abi.load_ubo = load_ubo;
6044 ctx->abi.load_ssbo = load_ssbo;
6045
6046 create_function(ctx);
6047 preload_ring_buffers(ctx);
6048
6049 /* For GFX9 merged shaders:
6050 * - Set EXEC for the first shader. If the prolog is present, set
6051 * EXEC there instead.
6052 * - Add a barrier before the second shader.
6053 * - In the second shader, reset EXEC to ~0 and wrap the main part in
6054 * an if-statement. This is required for correctness in geometry
6055 * shaders, to ensure that empty GS waves do not send GS_EMIT and
6056 * GS_CUT messages.
6057 *
6058 * For monolithic merged shaders, the first shader is wrapped in an
6059 * if-block together with its prolog in si_build_wrapper_function.
6060 *
6061 * NGG vertex and tess eval shaders running as the last
6062 * vertex/geometry stage handle execution explicitly using
6063 * if-statements.
6064 */
6065 if (ctx->screen->info.chip_class >= GFX9) {
6066 if (!shader->is_monolithic &&
6067 sel->info.num_instructions > 1 && /* not empty shader */
6068 (shader->key.as_es || shader->key.as_ls) &&
6069 (ctx->type == PIPE_SHADER_TESS_EVAL ||
6070 (ctx->type == PIPE_SHADER_VERTEX &&
6071 !si_vs_needs_prolog(sel, &shader->key.part.vs.prolog)))) {
6072 si_init_exec_from_input(ctx,
6073 ctx->param_merged_wave_info, 0);
6074 } else if (ctx->type == PIPE_SHADER_TESS_CTRL ||
6075 ctx->type == PIPE_SHADER_GEOMETRY ||
6076 shader->key.as_ngg) {
6077 LLVMValueRef num_threads;
6078 bool nested_barrier;
6079
6080 if (!shader->is_monolithic)
6081 ac_init_exec_full_mask(&ctx->ac);
6082
6083 if (ctx->type == PIPE_SHADER_TESS_CTRL ||
6084 ctx->type == PIPE_SHADER_GEOMETRY) {
6085 /* Number of patches / primitives */
6086 num_threads = si_unpack_param(ctx, ctx->param_merged_wave_info, 8, 8);
6087 nested_barrier = true;
6088 } else {
6089 /* Number of vertices */
6090 num_threads = si_unpack_param(ctx, ctx->param_merged_wave_info, 0, 8);
6091 nested_barrier = false;
6092 }
6093
6094 LLVMValueRef ena =
6095 LLVMBuildICmp(ctx->ac.builder, LLVMIntULT,
6096 ac_get_thread_id(&ctx->ac), num_threads, "");
6097 lp_build_if(&ctx->merged_wrap_if_state, &ctx->gallivm, ena);
6098
6099 if (nested_barrier) {
6100 /* Execute a barrier before the second shader in
6101 * a merged shader.
6102 *
6103 * Execute the barrier inside the conditional block,
6104 * so that empty waves can jump directly to s_endpgm,
6105 * which will also signal the barrier.
6106 *
6107 * This is possible in gfx9, because an empty wave
6108 * for the second shader does not participate in
6109 * the epilogue. With NGG, empty waves may still
6110 * be required to export data (e.g. GS output vertices),
6111 * so we cannot let them exit early.
6112 *
6113 * If the shader is TCS and the TCS epilog is present
6114 * and contains a barrier, it will wait there and then
6115 * reach s_endpgm.
6116 */
6117 si_llvm_emit_barrier(NULL, bld_base, NULL);
6118 }
6119 }
6120 }
6121
6122 if (ctx->type == PIPE_SHADER_TESS_CTRL &&
6123 sel->tcs_info.tessfactors_are_def_in_all_invocs) {
6124 for (unsigned i = 0; i < 6; i++) {
6125 ctx->invoc0_tess_factors[i] =
6126 ac_build_alloca_undef(&ctx->ac, ctx->i32, "");
6127 }
6128 }
6129
6130 if (ctx->type == PIPE_SHADER_GEOMETRY) {
6131 for (unsigned i = 0; i < 4; i++) {
6132 ctx->gs_next_vertex[i] =
6133 ac_build_alloca(&ctx->ac, ctx->i32, "");
6134 }
6135 if (shader->key.as_ngg) {
6136 for (unsigned i = 0; i < 4; ++i) {
6137 ctx->gs_curprim_verts[i] =
6138 lp_build_alloca(&ctx->gallivm, ctx->ac.i32, "");
6139 }
6140
6141 LLVMTypeRef a8i32 = LLVMArrayType(ctx->i32, 8);
6142 ctx->gs_ngg_scratch = LLVMAddGlobalInAddressSpace(ctx->ac.module,
6143 a8i32, "ngg_scratch", AC_ADDR_SPACE_LDS);
6144 LLVMSetInitializer(ctx->gs_ngg_scratch, LLVMGetUndef(a8i32));
6145 LLVMSetAlignment(ctx->gs_ngg_scratch, 4);
6146
6147 ctx->gs_ngg_emit = LLVMAddGlobalInAddressSpace(ctx->ac.module,
6148 LLVMArrayType(ctx->i32, 0), "ngg_emit", AC_ADDR_SPACE_LDS);
6149 LLVMSetLinkage(ctx->gs_ngg_emit, LLVMExternalLinkage);
6150 LLVMSetAlignment(ctx->gs_ngg_emit, 4);
6151 }
6152 }
6153
6154 if (sel->force_correct_derivs_after_kill) {
6155 ctx->postponed_kill = ac_build_alloca_undef(&ctx->ac, ctx->i1, "");
6156 /* true = don't kill. */
6157 LLVMBuildStore(ctx->ac.builder, ctx->i1true,
6158 ctx->postponed_kill);
6159 }
6160
6161 if (sel->tokens) {
6162 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
6163 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
6164 return false;
6165 }
6166 } else {
6167 if (!si_nir_build_llvm(ctx, sel->nir)) {
6168 fprintf(stderr, "Failed to translate shader from NIR to LLVM\n");
6169 return false;
6170 }
6171 }
6172
6173 si_llvm_build_ret(ctx, ctx->return_value);
6174 return true;
6175 }
6176
6177 /**
6178 * Compute the VS prolog key, which contains all the information needed to
6179 * build the VS prolog function, and set shader->info bits where needed.
6180 *
6181 * \param info Shader info of the vertex shader.
6182 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
6183 * \param prolog_key Key of the VS prolog
6184 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
6185 * \param key Output shader part key.
6186 */
6187 static void si_get_vs_prolog_key(const struct tgsi_shader_info *info,
6188 unsigned num_input_sgprs,
6189 const struct si_vs_prolog_bits *prolog_key,
6190 struct si_shader *shader_out,
6191 union si_shader_part_key *key)
6192 {
6193 memset(key, 0, sizeof(*key));
6194 key->vs_prolog.states = *prolog_key;
6195 key->vs_prolog.num_input_sgprs = num_input_sgprs;
6196 key->vs_prolog.last_input = MAX2(1, info->num_inputs) - 1;
6197 key->vs_prolog.as_ls = shader_out->key.as_ls;
6198 key->vs_prolog.as_es = shader_out->key.as_es;
6199 key->vs_prolog.as_ngg = shader_out->key.as_ngg;
6200
6201 if (shader_out->selector->type == PIPE_SHADER_TESS_CTRL) {
6202 key->vs_prolog.as_ls = 1;
6203 key->vs_prolog.num_merged_next_stage_vgprs = 2;
6204 } else if (shader_out->selector->type == PIPE_SHADER_GEOMETRY) {
6205 key->vs_prolog.as_es = 1;
6206 key->vs_prolog.num_merged_next_stage_vgprs = 5;
6207 } else if (shader_out->key.as_ngg) {
6208 key->vs_prolog.num_merged_next_stage_vgprs = 5;
6209 }
6210
6211 /* Enable loading the InstanceID VGPR. */
6212 uint16_t input_mask = u_bit_consecutive(0, info->num_inputs);
6213
6214 if ((key->vs_prolog.states.instance_divisor_is_one |
6215 key->vs_prolog.states.instance_divisor_is_fetched) & input_mask)
6216 shader_out->info.uses_instanceid = true;
6217 }
6218
6219 /**
6220 * Compute the PS prolog key, which contains all the information needed to
6221 * build the PS prolog function, and set related bits in shader->config.
6222 */
6223 static void si_get_ps_prolog_key(struct si_shader *shader,
6224 union si_shader_part_key *key,
6225 bool separate_prolog)
6226 {
6227 struct tgsi_shader_info *info = &shader->selector->info;
6228
6229 memset(key, 0, sizeof(*key));
6230 key->ps_prolog.states = shader->key.part.ps.prolog;
6231 key->ps_prolog.colors_read = info->colors_read;
6232 key->ps_prolog.num_input_sgprs = shader->info.num_input_sgprs;
6233 key->ps_prolog.num_input_vgprs = shader->info.num_input_vgprs;
6234 key->ps_prolog.wqm = info->uses_derivatives &&
6235 (key->ps_prolog.colors_read ||
6236 key->ps_prolog.states.force_persp_sample_interp ||
6237 key->ps_prolog.states.force_linear_sample_interp ||
6238 key->ps_prolog.states.force_persp_center_interp ||
6239 key->ps_prolog.states.force_linear_center_interp ||
6240 key->ps_prolog.states.bc_optimize_for_persp ||
6241 key->ps_prolog.states.bc_optimize_for_linear);
6242 key->ps_prolog.ancillary_vgpr_index = shader->info.ancillary_vgpr_index;
6243
6244 if (info->colors_read) {
6245 unsigned *color = shader->selector->color_attr_index;
6246
6247 if (shader->key.part.ps.prolog.color_two_side) {
6248 /* BCOLORs are stored after the last input. */
6249 key->ps_prolog.num_interp_inputs = info->num_inputs;
6250 key->ps_prolog.face_vgpr_index = shader->info.face_vgpr_index;
6251 if (separate_prolog)
6252 shader->config.spi_ps_input_ena |= S_0286CC_FRONT_FACE_ENA(1);
6253 }
6254
6255 for (unsigned i = 0; i < 2; i++) {
6256 unsigned interp = info->input_interpolate[color[i]];
6257 unsigned location = info->input_interpolate_loc[color[i]];
6258
6259 if (!(info->colors_read & (0xf << i*4)))
6260 continue;
6261
6262 key->ps_prolog.color_attr_index[i] = color[i];
6263
6264 if (shader->key.part.ps.prolog.flatshade_colors &&
6265 interp == TGSI_INTERPOLATE_COLOR)
6266 interp = TGSI_INTERPOLATE_CONSTANT;
6267
6268 switch (interp) {
6269 case TGSI_INTERPOLATE_CONSTANT:
6270 key->ps_prolog.color_interp_vgpr_index[i] = -1;
6271 break;
6272 case TGSI_INTERPOLATE_PERSPECTIVE:
6273 case TGSI_INTERPOLATE_COLOR:
6274 /* Force the interpolation location for colors here. */
6275 if (shader->key.part.ps.prolog.force_persp_sample_interp)
6276 location = TGSI_INTERPOLATE_LOC_SAMPLE;
6277 if (shader->key.part.ps.prolog.force_persp_center_interp)
6278 location = TGSI_INTERPOLATE_LOC_CENTER;
6279
6280 switch (location) {
6281 case TGSI_INTERPOLATE_LOC_SAMPLE:
6282 key->ps_prolog.color_interp_vgpr_index[i] = 0;
6283 if (separate_prolog) {
6284 shader->config.spi_ps_input_ena |=
6285 S_0286CC_PERSP_SAMPLE_ENA(1);
6286 }
6287 break;
6288 case TGSI_INTERPOLATE_LOC_CENTER:
6289 key->ps_prolog.color_interp_vgpr_index[i] = 2;
6290 if (separate_prolog) {
6291 shader->config.spi_ps_input_ena |=
6292 S_0286CC_PERSP_CENTER_ENA(1);
6293 }
6294 break;
6295 case TGSI_INTERPOLATE_LOC_CENTROID:
6296 key->ps_prolog.color_interp_vgpr_index[i] = 4;
6297 if (separate_prolog) {
6298 shader->config.spi_ps_input_ena |=
6299 S_0286CC_PERSP_CENTROID_ENA(1);
6300 }
6301 break;
6302 default:
6303 assert(0);
6304 }
6305 break;
6306 case TGSI_INTERPOLATE_LINEAR:
6307 /* Force the interpolation location for colors here. */
6308 if (shader->key.part.ps.prolog.force_linear_sample_interp)
6309 location = TGSI_INTERPOLATE_LOC_SAMPLE;
6310 if (shader->key.part.ps.prolog.force_linear_center_interp)
6311 location = TGSI_INTERPOLATE_LOC_CENTER;
6312
6313 /* The VGPR assignment for non-monolithic shaders
6314 * works because InitialPSInputAddr is set on the
6315 * main shader and PERSP_PULL_MODEL is never used.
6316 */
6317 switch (location) {
6318 case TGSI_INTERPOLATE_LOC_SAMPLE:
6319 key->ps_prolog.color_interp_vgpr_index[i] =
6320 separate_prolog ? 6 : 9;
6321 if (separate_prolog) {
6322 shader->config.spi_ps_input_ena |=
6323 S_0286CC_LINEAR_SAMPLE_ENA(1);
6324 }
6325 break;
6326 case TGSI_INTERPOLATE_LOC_CENTER:
6327 key->ps_prolog.color_interp_vgpr_index[i] =
6328 separate_prolog ? 8 : 11;
6329 if (separate_prolog) {
6330 shader->config.spi_ps_input_ena |=
6331 S_0286CC_LINEAR_CENTER_ENA(1);
6332 }
6333 break;
6334 case TGSI_INTERPOLATE_LOC_CENTROID:
6335 key->ps_prolog.color_interp_vgpr_index[i] =
6336 separate_prolog ? 10 : 13;
6337 if (separate_prolog) {
6338 shader->config.spi_ps_input_ena |=
6339 S_0286CC_LINEAR_CENTROID_ENA(1);
6340 }
6341 break;
6342 default:
6343 assert(0);
6344 }
6345 break;
6346 default:
6347 assert(0);
6348 }
6349 }
6350 }
6351 }
6352
6353 /**
6354 * Check whether a PS prolog is required based on the key.
6355 */
6356 static bool si_need_ps_prolog(const union si_shader_part_key *key)
6357 {
6358 return key->ps_prolog.colors_read ||
6359 key->ps_prolog.states.force_persp_sample_interp ||
6360 key->ps_prolog.states.force_linear_sample_interp ||
6361 key->ps_prolog.states.force_persp_center_interp ||
6362 key->ps_prolog.states.force_linear_center_interp ||
6363 key->ps_prolog.states.bc_optimize_for_persp ||
6364 key->ps_prolog.states.bc_optimize_for_linear ||
6365 key->ps_prolog.states.poly_stipple ||
6366 key->ps_prolog.states.samplemask_log_ps_iter;
6367 }
6368
6369 /**
6370 * Compute the PS epilog key, which contains all the information needed to
6371 * build the PS epilog function.
6372 */
6373 static void si_get_ps_epilog_key(struct si_shader *shader,
6374 union si_shader_part_key *key)
6375 {
6376 struct tgsi_shader_info *info = &shader->selector->info;
6377 memset(key, 0, sizeof(*key));
6378 key->ps_epilog.colors_written = info->colors_written;
6379 key->ps_epilog.writes_z = info->writes_z;
6380 key->ps_epilog.writes_stencil = info->writes_stencil;
6381 key->ps_epilog.writes_samplemask = info->writes_samplemask;
6382 key->ps_epilog.states = shader->key.part.ps.epilog;
6383 }
6384
6385 /**
6386 * Build the GS prolog function. Rotate the input vertices for triangle strips
6387 * with adjacency.
6388 */
6389 static void si_build_gs_prolog_function(struct si_shader_context *ctx,
6390 union si_shader_part_key *key)
6391 {
6392 unsigned num_sgprs, num_vgprs;
6393 struct si_function_info fninfo;
6394 LLVMBuilderRef builder = ctx->ac.builder;
6395 LLVMTypeRef returns[48];
6396 LLVMValueRef func, ret;
6397
6398 si_init_function_info(&fninfo);
6399
6400 if (ctx->screen->info.chip_class >= GFX9) {
6401 if (key->gs_prolog.states.gfx9_prev_is_vs)
6402 num_sgprs = 8 + GFX9_VSGS_NUM_USER_SGPR;
6403 else
6404 num_sgprs = 8 + GFX9_TESGS_NUM_USER_SGPR;
6405 num_vgprs = 5; /* ES inputs are not needed by GS */
6406 } else {
6407 num_sgprs = GFX6_GS_NUM_USER_SGPR + 2;
6408 num_vgprs = 8;
6409 }
6410
6411 for (unsigned i = 0; i < num_sgprs; ++i) {
6412 add_arg(&fninfo, ARG_SGPR, ctx->i32);
6413 returns[i] = ctx->i32;
6414 }
6415
6416 for (unsigned i = 0; i < num_vgprs; ++i) {
6417 add_arg(&fninfo, ARG_VGPR, ctx->i32);
6418 returns[num_sgprs + i] = ctx->f32;
6419 }
6420
6421 /* Create the function. */
6422 si_create_function(ctx, "gs_prolog", returns, num_sgprs + num_vgprs,
6423 &fninfo, 0);
6424 func = ctx->main_fn;
6425
6426 /* Set the full EXEC mask for the prolog, because we are only fiddling
6427 * with registers here. The main shader part will set the correct EXEC
6428 * mask.
6429 */
6430 if (ctx->screen->info.chip_class >= GFX9 && !key->gs_prolog.is_monolithic)
6431 ac_init_exec_full_mask(&ctx->ac);
6432
6433 /* Copy inputs to outputs. This should be no-op, as the registers match,
6434 * but it will prevent the compiler from overwriting them unintentionally.
6435 */
6436 ret = ctx->return_value;
6437 for (unsigned i = 0; i < num_sgprs; i++) {
6438 LLVMValueRef p = LLVMGetParam(func, i);
6439 ret = LLVMBuildInsertValue(builder, ret, p, i, "");
6440 }
6441 for (unsigned i = 0; i < num_vgprs; i++) {
6442 LLVMValueRef p = LLVMGetParam(func, num_sgprs + i);
6443 p = ac_to_float(&ctx->ac, p);
6444 ret = LLVMBuildInsertValue(builder, ret, p, num_sgprs + i, "");
6445 }
6446
6447 if (key->gs_prolog.states.tri_strip_adj_fix) {
6448 /* Remap the input vertices for every other primitive. */
6449 const unsigned gfx6_vtx_params[6] = {
6450 num_sgprs,
6451 num_sgprs + 1,
6452 num_sgprs + 3,
6453 num_sgprs + 4,
6454 num_sgprs + 5,
6455 num_sgprs + 6
6456 };
6457 const unsigned gfx9_vtx_params[3] = {
6458 num_sgprs,
6459 num_sgprs + 1,
6460 num_sgprs + 4,
6461 };
6462 LLVMValueRef vtx_in[6], vtx_out[6];
6463 LLVMValueRef prim_id, rotate;
6464
6465 if (ctx->screen->info.chip_class >= GFX9) {
6466 for (unsigned i = 0; i < 3; i++) {
6467 vtx_in[i*2] = si_unpack_param(ctx, gfx9_vtx_params[i], 0, 16);
6468 vtx_in[i*2+1] = si_unpack_param(ctx, gfx9_vtx_params[i], 16, 16);
6469 }
6470 } else {
6471 for (unsigned i = 0; i < 6; i++)
6472 vtx_in[i] = LLVMGetParam(func, gfx6_vtx_params[i]);
6473 }
6474
6475 prim_id = LLVMGetParam(func, num_sgprs + 2);
6476 rotate = LLVMBuildTrunc(builder, prim_id, ctx->i1, "");
6477
6478 for (unsigned i = 0; i < 6; ++i) {
6479 LLVMValueRef base, rotated;
6480 base = vtx_in[i];
6481 rotated = vtx_in[(i + 4) % 6];
6482 vtx_out[i] = LLVMBuildSelect(builder, rotate, rotated, base, "");
6483 }
6484
6485 if (ctx->screen->info.chip_class >= GFX9) {
6486 for (unsigned i = 0; i < 3; i++) {
6487 LLVMValueRef hi, out;
6488
6489 hi = LLVMBuildShl(builder, vtx_out[i*2+1],
6490 LLVMConstInt(ctx->i32, 16, 0), "");
6491 out = LLVMBuildOr(builder, vtx_out[i*2], hi, "");
6492 out = ac_to_float(&ctx->ac, out);
6493 ret = LLVMBuildInsertValue(builder, ret, out,
6494 gfx9_vtx_params[i], "");
6495 }
6496 } else {
6497 for (unsigned i = 0; i < 6; i++) {
6498 LLVMValueRef out;
6499
6500 out = ac_to_float(&ctx->ac, vtx_out[i]);
6501 ret = LLVMBuildInsertValue(builder, ret, out,
6502 gfx6_vtx_params[i], "");
6503 }
6504 }
6505 }
6506
6507 LLVMBuildRet(builder, ret);
6508 }
6509
6510 /**
6511 * Given a list of shader part functions, build a wrapper function that
6512 * runs them in sequence to form a monolithic shader.
6513 */
6514 static void si_build_wrapper_function(struct si_shader_context *ctx,
6515 LLVMValueRef *parts,
6516 unsigned num_parts,
6517 unsigned main_part,
6518 unsigned next_shader_first_part)
6519 {
6520 LLVMBuilderRef builder = ctx->ac.builder;
6521 /* PS epilog has one arg per color component; gfx9 merged shader
6522 * prologs need to forward 32 user SGPRs.
6523 */
6524 struct si_function_info fninfo;
6525 LLVMValueRef initial[64], out[64];
6526 LLVMTypeRef function_type;
6527 unsigned num_first_params;
6528 unsigned num_out, initial_num_out;
6529 MAYBE_UNUSED unsigned num_out_sgpr; /* used in debug checks */
6530 MAYBE_UNUSED unsigned initial_num_out_sgpr; /* used in debug checks */
6531 unsigned num_sgprs, num_vgprs;
6532 unsigned gprs;
6533 struct lp_build_if_state if_state;
6534
6535 si_init_function_info(&fninfo);
6536
6537 for (unsigned i = 0; i < num_parts; ++i) {
6538 ac_add_function_attr(ctx->ac.context, parts[i], -1,
6539 AC_FUNC_ATTR_ALWAYSINLINE);
6540 LLVMSetLinkage(parts[i], LLVMPrivateLinkage);
6541 }
6542
6543 /* The parameters of the wrapper function correspond to those of the
6544 * first part in terms of SGPRs and VGPRs, but we use the types of the
6545 * main part to get the right types. This is relevant for the
6546 * dereferenceable attribute on descriptor table pointers.
6547 */
6548 num_sgprs = 0;
6549 num_vgprs = 0;
6550
6551 function_type = LLVMGetElementType(LLVMTypeOf(parts[0]));
6552 num_first_params = LLVMCountParamTypes(function_type);
6553
6554 for (unsigned i = 0; i < num_first_params; ++i) {
6555 LLVMValueRef param = LLVMGetParam(parts[0], i);
6556
6557 if (ac_is_sgpr_param(param)) {
6558 assert(num_vgprs == 0);
6559 num_sgprs += ac_get_type_size(LLVMTypeOf(param)) / 4;
6560 } else {
6561 num_vgprs += ac_get_type_size(LLVMTypeOf(param)) / 4;
6562 }
6563 }
6564
6565 gprs = 0;
6566 while (gprs < num_sgprs + num_vgprs) {
6567 LLVMValueRef param = LLVMGetParam(parts[main_part], fninfo.num_params);
6568 LLVMTypeRef type = LLVMTypeOf(param);
6569 unsigned size = ac_get_type_size(type) / 4;
6570
6571 add_arg(&fninfo, gprs < num_sgprs ? ARG_SGPR : ARG_VGPR, type);
6572
6573 assert(ac_is_sgpr_param(param) == (gprs < num_sgprs));
6574 assert(gprs + size <= num_sgprs + num_vgprs &&
6575 (gprs >= num_sgprs || gprs + size <= num_sgprs));
6576
6577 gprs += size;
6578 }
6579
6580 /* Prepare the return type. */
6581 unsigned num_returns = 0;
6582 LLVMTypeRef returns[32], last_func_type, return_type;
6583
6584 last_func_type = LLVMGetElementType(LLVMTypeOf(parts[num_parts - 1]));
6585 return_type = LLVMGetReturnType(last_func_type);
6586
6587 switch (LLVMGetTypeKind(return_type)) {
6588 case LLVMStructTypeKind:
6589 num_returns = LLVMCountStructElementTypes(return_type);
6590 assert(num_returns <= ARRAY_SIZE(returns));
6591 LLVMGetStructElementTypes(return_type, returns);
6592 break;
6593 case LLVMVoidTypeKind:
6594 break;
6595 default:
6596 unreachable("unexpected type");
6597 }
6598
6599 si_create_function(ctx, "wrapper", returns, num_returns, &fninfo,
6600 si_get_max_workgroup_size(ctx->shader));
6601
6602 if (is_merged_shader(ctx))
6603 ac_init_exec_full_mask(&ctx->ac);
6604
6605 /* Record the arguments of the function as if they were an output of
6606 * a previous part.
6607 */
6608 num_out = 0;
6609 num_out_sgpr = 0;
6610
6611 for (unsigned i = 0; i < fninfo.num_params; ++i) {
6612 LLVMValueRef param = LLVMGetParam(ctx->main_fn, i);
6613 LLVMTypeRef param_type = LLVMTypeOf(param);
6614 LLVMTypeRef out_type = i < fninfo.num_sgpr_params ? ctx->i32 : ctx->f32;
6615 unsigned size = ac_get_type_size(param_type) / 4;
6616
6617 if (size == 1) {
6618 if (LLVMGetTypeKind(param_type) == LLVMPointerTypeKind) {
6619 param = LLVMBuildPtrToInt(builder, param, ctx->i32, "");
6620 param_type = ctx->i32;
6621 }
6622
6623 if (param_type != out_type)
6624 param = LLVMBuildBitCast(builder, param, out_type, "");
6625 out[num_out++] = param;
6626 } else {
6627 LLVMTypeRef vector_type = LLVMVectorType(out_type, size);
6628
6629 if (LLVMGetTypeKind(param_type) == LLVMPointerTypeKind) {
6630 param = LLVMBuildPtrToInt(builder, param, ctx->i64, "");
6631 param_type = ctx->i64;
6632 }
6633
6634 if (param_type != vector_type)
6635 param = LLVMBuildBitCast(builder, param, vector_type, "");
6636
6637 for (unsigned j = 0; j < size; ++j)
6638 out[num_out++] = LLVMBuildExtractElement(
6639 builder, param, LLVMConstInt(ctx->i32, j, 0), "");
6640 }
6641
6642 if (i < fninfo.num_sgpr_params)
6643 num_out_sgpr = num_out;
6644 }
6645
6646 memcpy(initial, out, sizeof(out));
6647 initial_num_out = num_out;
6648 initial_num_out_sgpr = num_out_sgpr;
6649
6650 /* Now chain the parts. */
6651 LLVMValueRef ret;
6652 for (unsigned part = 0; part < num_parts; ++part) {
6653 LLVMValueRef in[48];
6654 LLVMTypeRef ret_type;
6655 unsigned out_idx = 0;
6656 unsigned num_params = LLVMCountParams(parts[part]);
6657
6658 /* Merged shaders are executed conditionally depending
6659 * on the number of enabled threads passed in the input SGPRs. */
6660 if (is_multi_part_shader(ctx) && part == 0) {
6661 LLVMValueRef ena, count = initial[3];
6662
6663 count = LLVMBuildAnd(builder, count,
6664 LLVMConstInt(ctx->i32, 0x7f, 0), "");
6665 ena = LLVMBuildICmp(builder, LLVMIntULT,
6666 ac_get_thread_id(&ctx->ac), count, "");
6667 lp_build_if(&if_state, &ctx->gallivm, ena);
6668 }
6669
6670 /* Derive arguments for the next part from outputs of the
6671 * previous one.
6672 */
6673 for (unsigned param_idx = 0; param_idx < num_params; ++param_idx) {
6674 LLVMValueRef param;
6675 LLVMTypeRef param_type;
6676 bool is_sgpr;
6677 unsigned param_size;
6678 LLVMValueRef arg = NULL;
6679
6680 param = LLVMGetParam(parts[part], param_idx);
6681 param_type = LLVMTypeOf(param);
6682 param_size = ac_get_type_size(param_type) / 4;
6683 is_sgpr = ac_is_sgpr_param(param);
6684
6685 if (is_sgpr) {
6686 ac_add_function_attr(ctx->ac.context, parts[part],
6687 param_idx + 1, AC_FUNC_ATTR_INREG);
6688 } else if (out_idx < num_out_sgpr) {
6689 /* Skip returned SGPRs the current part doesn't
6690 * declare on the input. */
6691 out_idx = num_out_sgpr;
6692 }
6693
6694 assert(out_idx + param_size <= (is_sgpr ? num_out_sgpr : num_out));
6695
6696 if (param_size == 1)
6697 arg = out[out_idx];
6698 else
6699 arg = ac_build_gather_values(&ctx->ac, &out[out_idx], param_size);
6700
6701 if (LLVMTypeOf(arg) != param_type) {
6702 if (LLVMGetTypeKind(param_type) == LLVMPointerTypeKind) {
6703 if (LLVMGetPointerAddressSpace(param_type) ==
6704 AC_ADDR_SPACE_CONST_32BIT) {
6705 arg = LLVMBuildBitCast(builder, arg, ctx->i32, "");
6706 arg = LLVMBuildIntToPtr(builder, arg, param_type, "");
6707 } else {
6708 arg = LLVMBuildBitCast(builder, arg, ctx->i64, "");
6709 arg = LLVMBuildIntToPtr(builder, arg, param_type, "");
6710 }
6711 } else {
6712 arg = LLVMBuildBitCast(builder, arg, param_type, "");
6713 }
6714 }
6715
6716 in[param_idx] = arg;
6717 out_idx += param_size;
6718 }
6719
6720 ret = ac_build_call(&ctx->ac, parts[part], in, num_params);
6721
6722 if (is_multi_part_shader(ctx) &&
6723 part + 1 == next_shader_first_part) {
6724 lp_build_endif(&if_state);
6725
6726 /* The second half of the merged shader should use
6727 * the inputs from the toplevel (wrapper) function,
6728 * not the return value from the last call.
6729 *
6730 * That's because the last call was executed condi-
6731 * tionally, so we can't consume it in the main
6732 * block.
6733 */
6734 memcpy(out, initial, sizeof(initial));
6735 num_out = initial_num_out;
6736 num_out_sgpr = initial_num_out_sgpr;
6737 continue;
6738 }
6739
6740 /* Extract the returned GPRs. */
6741 ret_type = LLVMTypeOf(ret);
6742 num_out = 0;
6743 num_out_sgpr = 0;
6744
6745 if (LLVMGetTypeKind(ret_type) != LLVMVoidTypeKind) {
6746 assert(LLVMGetTypeKind(ret_type) == LLVMStructTypeKind);
6747
6748 unsigned ret_size = LLVMCountStructElementTypes(ret_type);
6749
6750 for (unsigned i = 0; i < ret_size; ++i) {
6751 LLVMValueRef val =
6752 LLVMBuildExtractValue(builder, ret, i, "");
6753
6754 assert(num_out < ARRAY_SIZE(out));
6755 out[num_out++] = val;
6756
6757 if (LLVMTypeOf(val) == ctx->i32) {
6758 assert(num_out_sgpr + 1 == num_out);
6759 num_out_sgpr = num_out;
6760 }
6761 }
6762 }
6763 }
6764
6765 /* Return the value from the last part. */
6766 if (LLVMGetTypeKind(LLVMTypeOf(ret)) == LLVMVoidTypeKind)
6767 LLVMBuildRetVoid(builder);
6768 else
6769 LLVMBuildRet(builder, ret);
6770 }
6771
6772 static bool si_should_optimize_less(struct ac_llvm_compiler *compiler,
6773 struct si_shader_selector *sel)
6774 {
6775 if (!compiler->low_opt_passes)
6776 return false;
6777
6778 /* Assume a slow CPU. */
6779 assert(!sel->screen->info.has_dedicated_vram &&
6780 sel->screen->info.chip_class <= GFX8);
6781
6782 /* For a crazy dEQP test containing 2597 memory opcodes, mostly
6783 * buffer stores. */
6784 return sel->type == PIPE_SHADER_COMPUTE &&
6785 sel->info.num_memory_instructions > 1000;
6786 }
6787
6788 int si_compile_tgsi_shader(struct si_screen *sscreen,
6789 struct ac_llvm_compiler *compiler,
6790 struct si_shader *shader,
6791 struct pipe_debug_callback *debug)
6792 {
6793 struct si_shader_selector *sel = shader->selector;
6794 struct si_shader_context ctx;
6795 int r = -1;
6796
6797 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6798 * conversion fails. */
6799 if (si_can_dump_shader(sscreen, sel->info.processor) &&
6800 !(sscreen->debug_flags & DBG(NO_TGSI))) {
6801 if (sel->tokens)
6802 tgsi_dump(sel->tokens, 0);
6803 else
6804 nir_print_shader(sel->nir, stderr);
6805 si_dump_streamout(&sel->so);
6806 }
6807
6808 si_init_shader_ctx(&ctx, sscreen, compiler);
6809 si_llvm_context_set_tgsi(&ctx, shader);
6810
6811 memset(shader->info.vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
6812 sizeof(shader->info.vs_output_param_offset));
6813
6814 shader->info.uses_instanceid = sel->info.uses_instanceid;
6815
6816 if (!si_compile_tgsi_main(&ctx)) {
6817 si_llvm_dispose(&ctx);
6818 return -1;
6819 }
6820
6821 if (shader->is_monolithic && ctx.type == PIPE_SHADER_VERTEX) {
6822 LLVMValueRef parts[2];
6823 bool need_prolog = sel->vs_needs_prolog;
6824
6825 parts[1] = ctx.main_fn;
6826
6827 if (need_prolog) {
6828 union si_shader_part_key prolog_key;
6829 si_get_vs_prolog_key(&sel->info,
6830 shader->info.num_input_sgprs,
6831 &shader->key.part.vs.prolog,
6832 shader, &prolog_key);
6833 si_build_vs_prolog_function(&ctx, &prolog_key);
6834 parts[0] = ctx.main_fn;
6835 }
6836
6837 si_build_wrapper_function(&ctx, parts + !need_prolog,
6838 1 + need_prolog, need_prolog, 0);
6839
6840 if (ctx.shader->key.opt.vs_as_prim_discard_cs)
6841 si_build_prim_discard_compute_shader(&ctx);
6842 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_TESS_CTRL) {
6843 if (sscreen->info.chip_class >= GFX9) {
6844 struct si_shader_selector *ls = shader->key.part.tcs.ls;
6845 LLVMValueRef parts[4];
6846 bool vs_needs_prolog =
6847 si_vs_needs_prolog(ls, &shader->key.part.tcs.ls_prolog);
6848
6849 /* TCS main part */
6850 parts[2] = ctx.main_fn;
6851
6852 /* TCS epilog */
6853 union si_shader_part_key tcs_epilog_key;
6854 memset(&tcs_epilog_key, 0, sizeof(tcs_epilog_key));
6855 tcs_epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
6856 si_build_tcs_epilog_function(&ctx, &tcs_epilog_key);
6857 parts[3] = ctx.main_fn;
6858
6859 /* VS as LS main part */
6860 struct si_shader shader_ls = {};
6861 shader_ls.selector = ls;
6862 shader_ls.key.as_ls = 1;
6863 shader_ls.key.mono = shader->key.mono;
6864 shader_ls.key.opt = shader->key.opt;
6865 shader_ls.is_monolithic = true;
6866 si_llvm_context_set_tgsi(&ctx, &shader_ls);
6867
6868 if (!si_compile_tgsi_main(&ctx)) {
6869 si_llvm_dispose(&ctx);
6870 return -1;
6871 }
6872 shader->info.uses_instanceid |= ls->info.uses_instanceid;
6873 parts[1] = ctx.main_fn;
6874
6875 /* LS prolog */
6876 if (vs_needs_prolog) {
6877 union si_shader_part_key vs_prolog_key;
6878 si_get_vs_prolog_key(&ls->info,
6879 shader_ls.info.num_input_sgprs,
6880 &shader->key.part.tcs.ls_prolog,
6881 shader, &vs_prolog_key);
6882 vs_prolog_key.vs_prolog.is_monolithic = true;
6883 si_build_vs_prolog_function(&ctx, &vs_prolog_key);
6884 parts[0] = ctx.main_fn;
6885 }
6886
6887 /* Reset the shader context. */
6888 ctx.shader = shader;
6889 ctx.type = PIPE_SHADER_TESS_CTRL;
6890
6891 si_build_wrapper_function(&ctx,
6892 parts + !vs_needs_prolog,
6893 4 - !vs_needs_prolog, vs_needs_prolog,
6894 vs_needs_prolog ? 2 : 1);
6895 } else {
6896 LLVMValueRef parts[2];
6897 union si_shader_part_key epilog_key;
6898
6899 parts[0] = ctx.main_fn;
6900
6901 memset(&epilog_key, 0, sizeof(epilog_key));
6902 epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
6903 si_build_tcs_epilog_function(&ctx, &epilog_key);
6904 parts[1] = ctx.main_fn;
6905
6906 si_build_wrapper_function(&ctx, parts, 2, 0, 0);
6907 }
6908 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_GEOMETRY) {
6909 if (ctx.screen->info.chip_class >= GFX9) {
6910 struct si_shader_selector *es = shader->key.part.gs.es;
6911 LLVMValueRef es_prolog = NULL;
6912 LLVMValueRef es_main = NULL;
6913 LLVMValueRef gs_prolog = NULL;
6914 LLVMValueRef gs_main = ctx.main_fn;
6915
6916 /* GS prolog */
6917 union si_shader_part_key gs_prolog_key;
6918 memset(&gs_prolog_key, 0, sizeof(gs_prolog_key));
6919 gs_prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
6920 gs_prolog_key.gs_prolog.is_monolithic = true;
6921 si_build_gs_prolog_function(&ctx, &gs_prolog_key);
6922 gs_prolog = ctx.main_fn;
6923
6924 /* ES main part */
6925 struct si_shader shader_es = {};
6926 shader_es.selector = es;
6927 shader_es.key.as_es = 1;
6928 shader_es.key.mono = shader->key.mono;
6929 shader_es.key.opt = shader->key.opt;
6930 shader_es.is_monolithic = true;
6931 si_llvm_context_set_tgsi(&ctx, &shader_es);
6932
6933 if (!si_compile_tgsi_main(&ctx)) {
6934 si_llvm_dispose(&ctx);
6935 return -1;
6936 }
6937 shader->info.uses_instanceid |= es->info.uses_instanceid;
6938 es_main = ctx.main_fn;
6939
6940 /* ES prolog */
6941 if (es->vs_needs_prolog) {
6942 union si_shader_part_key vs_prolog_key;
6943 si_get_vs_prolog_key(&es->info,
6944 shader_es.info.num_input_sgprs,
6945 &shader->key.part.gs.vs_prolog,
6946 shader, &vs_prolog_key);
6947 vs_prolog_key.vs_prolog.is_monolithic = true;
6948 si_build_vs_prolog_function(&ctx, &vs_prolog_key);
6949 es_prolog = ctx.main_fn;
6950 }
6951
6952 /* Reset the shader context. */
6953 ctx.shader = shader;
6954 ctx.type = PIPE_SHADER_GEOMETRY;
6955
6956 /* Prepare the array of shader parts. */
6957 LLVMValueRef parts[4];
6958 unsigned num_parts = 0, main_part, next_first_part;
6959
6960 if (es_prolog)
6961 parts[num_parts++] = es_prolog;
6962
6963 parts[main_part = num_parts++] = es_main;
6964 parts[next_first_part = num_parts++] = gs_prolog;
6965 parts[num_parts++] = gs_main;
6966
6967 si_build_wrapper_function(&ctx, parts, num_parts,
6968 main_part, next_first_part);
6969 } else {
6970 LLVMValueRef parts[2];
6971 union si_shader_part_key prolog_key;
6972
6973 parts[1] = ctx.main_fn;
6974
6975 memset(&prolog_key, 0, sizeof(prolog_key));
6976 prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
6977 si_build_gs_prolog_function(&ctx, &prolog_key);
6978 parts[0] = ctx.main_fn;
6979
6980 si_build_wrapper_function(&ctx, parts, 2, 1, 0);
6981 }
6982 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_FRAGMENT) {
6983 LLVMValueRef parts[3];
6984 union si_shader_part_key prolog_key;
6985 union si_shader_part_key epilog_key;
6986 bool need_prolog;
6987
6988 si_get_ps_prolog_key(shader, &prolog_key, false);
6989 need_prolog = si_need_ps_prolog(&prolog_key);
6990
6991 parts[need_prolog ? 1 : 0] = ctx.main_fn;
6992
6993 if (need_prolog) {
6994 si_build_ps_prolog_function(&ctx, &prolog_key);
6995 parts[0] = ctx.main_fn;
6996 }
6997
6998 si_get_ps_epilog_key(shader, &epilog_key);
6999 si_build_ps_epilog_function(&ctx, &epilog_key);
7000 parts[need_prolog ? 2 : 1] = ctx.main_fn;
7001
7002 si_build_wrapper_function(&ctx, parts, need_prolog ? 3 : 2,
7003 need_prolog ? 1 : 0, 0);
7004 }
7005
7006 si_llvm_optimize_module(&ctx);
7007
7008 /* Post-optimization transformations and analysis. */
7009 si_optimize_vs_outputs(&ctx);
7010
7011 if ((debug && debug->debug_message) ||
7012 si_can_dump_shader(sscreen, ctx.type)) {
7013 ctx.shader->info.private_mem_vgprs =
7014 ac_count_scratch_private_memory(ctx.main_fn);
7015 }
7016
7017 /* Make sure the input is a pointer and not integer followed by inttoptr. */
7018 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx.main_fn, 0))) ==
7019 LLVMPointerTypeKind);
7020
7021 /* Compile to bytecode. */
7022 r = si_compile_llvm(sscreen, &shader->binary, &shader->config, compiler,
7023 ctx.ac.module, debug, ctx.type,
7024 si_get_shader_name(shader, ctx.type),
7025 si_should_optimize_less(compiler, shader->selector));
7026 si_llvm_dispose(&ctx);
7027 if (r) {
7028 fprintf(stderr, "LLVM failed to compile shader\n");
7029 return r;
7030 }
7031
7032 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
7033 * LLVM 3.9svn has this bug.
7034 */
7035 if (sel->type == PIPE_SHADER_COMPUTE) {
7036 unsigned wave_size = 64;
7037 unsigned max_vgprs = 256;
7038 unsigned max_sgprs = sscreen->info.chip_class >= GFX8 ? 800 : 512;
7039 unsigned max_sgprs_per_wave = 128;
7040 unsigned max_block_threads = si_get_max_workgroup_size(shader);
7041 unsigned min_waves_per_cu = DIV_ROUND_UP(max_block_threads, wave_size);
7042 unsigned min_waves_per_simd = DIV_ROUND_UP(min_waves_per_cu, 4);
7043
7044 max_vgprs = max_vgprs / min_waves_per_simd;
7045 max_sgprs = MIN2(max_sgprs / min_waves_per_simd, max_sgprs_per_wave);
7046
7047 if (shader->config.num_sgprs > max_sgprs ||
7048 shader->config.num_vgprs > max_vgprs) {
7049 fprintf(stderr, "LLVM failed to compile a shader correctly: "
7050 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7051 shader->config.num_sgprs, shader->config.num_vgprs,
7052 max_sgprs, max_vgprs);
7053
7054 /* Just terminate the process, because dependent
7055 * shaders can hang due to bad input data, but use
7056 * the env var to allow shader-db to work.
7057 */
7058 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7059 abort();
7060 }
7061 }
7062
7063 /* Add the scratch offset to input SGPRs. */
7064 if (shader->config.scratch_bytes_per_wave && !is_merged_shader(&ctx))
7065 shader->info.num_input_sgprs += 1; /* scratch byte offset */
7066
7067 /* Calculate the number of fragment input VGPRs. */
7068 if (ctx.type == PIPE_SHADER_FRAGMENT) {
7069 shader->info.num_input_vgprs = 0;
7070 shader->info.face_vgpr_index = -1;
7071 shader->info.ancillary_vgpr_index = -1;
7072
7073 if (G_0286CC_PERSP_SAMPLE_ENA(shader->config.spi_ps_input_addr))
7074 shader->info.num_input_vgprs += 2;
7075 if (G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_addr))
7076 shader->info.num_input_vgprs += 2;
7077 if (G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_addr))
7078 shader->info.num_input_vgprs += 2;
7079 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader->config.spi_ps_input_addr))
7080 shader->info.num_input_vgprs += 3;
7081 if (G_0286CC_LINEAR_SAMPLE_ENA(shader->config.spi_ps_input_addr))
7082 shader->info.num_input_vgprs += 2;
7083 if (G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_addr))
7084 shader->info.num_input_vgprs += 2;
7085 if (G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_addr))
7086 shader->info.num_input_vgprs += 2;
7087 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader->config.spi_ps_input_addr))
7088 shader->info.num_input_vgprs += 1;
7089 if (G_0286CC_POS_X_FLOAT_ENA(shader->config.spi_ps_input_addr))
7090 shader->info.num_input_vgprs += 1;
7091 if (G_0286CC_POS_Y_FLOAT_ENA(shader->config.spi_ps_input_addr))
7092 shader->info.num_input_vgprs += 1;
7093 if (G_0286CC_POS_Z_FLOAT_ENA(shader->config.spi_ps_input_addr))
7094 shader->info.num_input_vgprs += 1;
7095 if (G_0286CC_POS_W_FLOAT_ENA(shader->config.spi_ps_input_addr))
7096 shader->info.num_input_vgprs += 1;
7097 if (G_0286CC_FRONT_FACE_ENA(shader->config.spi_ps_input_addr)) {
7098 shader->info.face_vgpr_index = shader->info.num_input_vgprs;
7099 shader->info.num_input_vgprs += 1;
7100 }
7101 if (G_0286CC_ANCILLARY_ENA(shader->config.spi_ps_input_addr)) {
7102 shader->info.ancillary_vgpr_index = shader->info.num_input_vgprs;
7103 shader->info.num_input_vgprs += 1;
7104 }
7105 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader->config.spi_ps_input_addr))
7106 shader->info.num_input_vgprs += 1;
7107 if (G_0286CC_POS_FIXED_PT_ENA(shader->config.spi_ps_input_addr))
7108 shader->info.num_input_vgprs += 1;
7109 }
7110
7111 si_calculate_max_simd_waves(shader);
7112 si_shader_dump_stats_for_shader_db(sscreen, shader, debug);
7113 return 0;
7114 }
7115
7116 /**
7117 * Create, compile and return a shader part (prolog or epilog).
7118 *
7119 * \param sscreen screen
7120 * \param list list of shader parts of the same category
7121 * \param type shader type
7122 * \param key shader part key
7123 * \param prolog whether the part being requested is a prolog
7124 * \param tm LLVM target machine
7125 * \param debug debug callback
7126 * \param build the callback responsible for building the main function
7127 * \return non-NULL on success
7128 */
7129 static struct si_shader_part *
7130 si_get_shader_part(struct si_screen *sscreen,
7131 struct si_shader_part **list,
7132 enum pipe_shader_type type,
7133 bool prolog,
7134 union si_shader_part_key *key,
7135 struct ac_llvm_compiler *compiler,
7136 struct pipe_debug_callback *debug,
7137 void (*build)(struct si_shader_context *,
7138 union si_shader_part_key *),
7139 const char *name)
7140 {
7141 struct si_shader_part *result;
7142
7143 mtx_lock(&sscreen->shader_parts_mutex);
7144
7145 /* Find existing. */
7146 for (result = *list; result; result = result->next) {
7147 if (memcmp(&result->key, key, sizeof(*key)) == 0) {
7148 mtx_unlock(&sscreen->shader_parts_mutex);
7149 return result;
7150 }
7151 }
7152
7153 /* Compile a new one. */
7154 result = CALLOC_STRUCT(si_shader_part);
7155 result->key = *key;
7156
7157 struct si_shader shader = {};
7158 struct si_shader_context ctx;
7159
7160 si_init_shader_ctx(&ctx, sscreen, compiler);
7161 ctx.shader = &shader;
7162 ctx.type = type;
7163
7164 switch (type) {
7165 case PIPE_SHADER_VERTEX:
7166 shader.key.as_ls = key->vs_prolog.as_ls;
7167 shader.key.as_es = key->vs_prolog.as_es;
7168 shader.key.as_ngg = key->vs_prolog.as_ngg;
7169 break;
7170 case PIPE_SHADER_TESS_CTRL:
7171 assert(!prolog);
7172 shader.key.part.tcs.epilog = key->tcs_epilog.states;
7173 break;
7174 case PIPE_SHADER_GEOMETRY:
7175 assert(prolog);
7176 break;
7177 case PIPE_SHADER_FRAGMENT:
7178 if (prolog)
7179 shader.key.part.ps.prolog = key->ps_prolog.states;
7180 else
7181 shader.key.part.ps.epilog = key->ps_epilog.states;
7182 break;
7183 default:
7184 unreachable("bad shader part");
7185 }
7186
7187 build(&ctx, key);
7188
7189 /* Compile. */
7190 si_llvm_optimize_module(&ctx);
7191
7192 if (si_compile_llvm(sscreen, &result->binary, &result->config, compiler,
7193 ctx.ac.module, debug, ctx.type, name, false)) {
7194 FREE(result);
7195 result = NULL;
7196 goto out;
7197 }
7198
7199 result->next = *list;
7200 *list = result;
7201
7202 out:
7203 si_llvm_dispose(&ctx);
7204 mtx_unlock(&sscreen->shader_parts_mutex);
7205 return result;
7206 }
7207
7208 static LLVMValueRef si_prolog_get_rw_buffers(struct si_shader_context *ctx)
7209 {
7210 LLVMValueRef ptr[2], list;
7211 bool merged_shader = is_merged_shader(ctx);
7212
7213 ptr[0] = LLVMGetParam(ctx->main_fn, (merged_shader ? 8 : 0) + SI_SGPR_RW_BUFFERS);
7214 list = LLVMBuildIntToPtr(ctx->ac.builder, ptr[0],
7215 ac_array_in_const32_addr_space(ctx->v4i32), "");
7216 return list;
7217 }
7218
7219 /**
7220 * Build the vertex shader prolog function.
7221 *
7222 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7223 * All inputs are returned unmodified. The vertex load indices are
7224 * stored after them, which will be used by the API VS for fetching inputs.
7225 *
7226 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7227 * input_v0,
7228 * input_v1,
7229 * input_v2,
7230 * input_v3,
7231 * (VertexID + BaseVertex),
7232 * (InstanceID + StartInstance),
7233 * (InstanceID / 2 + StartInstance)
7234 */
7235 static void si_build_vs_prolog_function(struct si_shader_context *ctx,
7236 union si_shader_part_key *key)
7237 {
7238 struct si_function_info fninfo;
7239 LLVMTypeRef *returns;
7240 LLVMValueRef ret, func;
7241 int num_returns, i;
7242 unsigned first_vs_vgpr = key->vs_prolog.num_merged_next_stage_vgprs;
7243 unsigned num_input_vgprs = key->vs_prolog.num_merged_next_stage_vgprs + 4;
7244 LLVMValueRef input_vgprs[9];
7245 unsigned num_all_input_regs = key->vs_prolog.num_input_sgprs +
7246 num_input_vgprs;
7247 unsigned user_sgpr_base = key->vs_prolog.num_merged_next_stage_vgprs ? 8 : 0;
7248
7249 si_init_function_info(&fninfo);
7250
7251 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7252 returns = alloca((num_all_input_regs + key->vs_prolog.last_input + 1) *
7253 sizeof(LLVMTypeRef));
7254 num_returns = 0;
7255
7256 /* Declare input and output SGPRs. */
7257 for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
7258 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7259 returns[num_returns++] = ctx->i32;
7260 }
7261
7262 /* Preloaded VGPRs (outputs must be floats) */
7263 for (i = 0; i < num_input_vgprs; i++) {
7264 add_arg_assign(&fninfo, ARG_VGPR, ctx->i32, &input_vgprs[i]);
7265 returns[num_returns++] = ctx->f32;
7266 }
7267
7268 /* Vertex load indices. */
7269 for (i = 0; i <= key->vs_prolog.last_input; i++)
7270 returns[num_returns++] = ctx->f32;
7271
7272 /* Create the function. */
7273 si_create_function(ctx, "vs_prolog", returns, num_returns, &fninfo, 0);
7274 func = ctx->main_fn;
7275
7276 if (key->vs_prolog.num_merged_next_stage_vgprs) {
7277 if (!key->vs_prolog.is_monolithic)
7278 si_init_exec_from_input(ctx, 3, 0);
7279
7280 if (key->vs_prolog.as_ls &&
7281 ctx->screen->has_ls_vgpr_init_bug) {
7282 /* If there are no HS threads, SPI loads the LS VGPRs
7283 * starting at VGPR 0. Shift them back to where they
7284 * belong.
7285 */
7286 LLVMValueRef has_hs_threads =
7287 LLVMBuildICmp(ctx->ac.builder, LLVMIntNE,
7288 si_unpack_param(ctx, 3, 8, 8),
7289 ctx->i32_0, "");
7290
7291 for (i = 4; i > 0; --i) {
7292 input_vgprs[i + 1] =
7293 LLVMBuildSelect(ctx->ac.builder, has_hs_threads,
7294 input_vgprs[i + 1],
7295 input_vgprs[i - 1], "");
7296 }
7297 }
7298 }
7299
7300 unsigned vertex_id_vgpr = first_vs_vgpr;
7301 unsigned instance_id_vgpr =
7302 ctx->screen->info.chip_class >= GFX10 ?
7303 first_vs_vgpr + 3 :
7304 first_vs_vgpr + (key->vs_prolog.as_ls ? 2 : 1);
7305
7306 ctx->abi.vertex_id = input_vgprs[vertex_id_vgpr];
7307 ctx->abi.instance_id = input_vgprs[instance_id_vgpr];
7308
7309 /* InstanceID = VertexID >> 16;
7310 * VertexID = VertexID & 0xffff;
7311 */
7312 if (key->vs_prolog.states.unpack_instance_id_from_vertex_id) {
7313 ctx->abi.instance_id = LLVMBuildLShr(ctx->ac.builder, ctx->abi.vertex_id,
7314 LLVMConstInt(ctx->i32, 16, 0), "");
7315 ctx->abi.vertex_id = LLVMBuildAnd(ctx->ac.builder, ctx->abi.vertex_id,
7316 LLVMConstInt(ctx->i32, 0xffff, 0), "");
7317 }
7318
7319 /* Copy inputs to outputs. This should be no-op, as the registers match,
7320 * but it will prevent the compiler from overwriting them unintentionally.
7321 */
7322 ret = ctx->return_value;
7323 for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
7324 LLVMValueRef p = LLVMGetParam(func, i);
7325 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, p, i, "");
7326 }
7327 for (i = 0; i < num_input_vgprs; i++) {
7328 LLVMValueRef p = input_vgprs[i];
7329
7330 if (i == vertex_id_vgpr)
7331 p = ctx->abi.vertex_id;
7332 else if (i == instance_id_vgpr)
7333 p = ctx->abi.instance_id;
7334
7335 p = ac_to_float(&ctx->ac, p);
7336 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, p,
7337 key->vs_prolog.num_input_sgprs + i, "");
7338 }
7339
7340 struct lp_build_if_state wrap_if_state;
7341 LLVMValueRef original_ret = ret;
7342 bool wrapped = false;
7343
7344 if (key->vs_prolog.is_monolithic && key->vs_prolog.as_ngg) {
7345 LLVMValueRef num_threads;
7346 LLVMValueRef ena;
7347
7348 num_threads = si_unpack_param(ctx, 3, 0, 8);
7349 ena = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT,
7350 ac_get_thread_id(&ctx->ac), num_threads, "");
7351 lp_build_if(&wrap_if_state, &ctx->gallivm, ena);
7352 wrapped = true;
7353 }
7354
7355 /* Compute vertex load indices from instance divisors. */
7356 LLVMValueRef instance_divisor_constbuf = NULL;
7357
7358 if (key->vs_prolog.states.instance_divisor_is_fetched) {
7359 LLVMValueRef list = si_prolog_get_rw_buffers(ctx);
7360 LLVMValueRef buf_index =
7361 LLVMConstInt(ctx->i32, SI_VS_CONST_INSTANCE_DIVISORS, 0);
7362 instance_divisor_constbuf =
7363 ac_build_load_to_sgpr(&ctx->ac, list, buf_index);
7364 }
7365
7366 for (i = 0; i <= key->vs_prolog.last_input; i++) {
7367 bool divisor_is_one =
7368 key->vs_prolog.states.instance_divisor_is_one & (1u << i);
7369 bool divisor_is_fetched =
7370 key->vs_prolog.states.instance_divisor_is_fetched & (1u << i);
7371 LLVMValueRef index = NULL;
7372
7373 if (divisor_is_one) {
7374 index = ctx->abi.instance_id;
7375 } else if (divisor_is_fetched) {
7376 LLVMValueRef udiv_factors[4];
7377
7378 for (unsigned j = 0; j < 4; j++) {
7379 udiv_factors[j] =
7380 buffer_load_const(ctx, instance_divisor_constbuf,
7381 LLVMConstInt(ctx->i32, i*16 + j*4, 0));
7382 udiv_factors[j] = ac_to_integer(&ctx->ac, udiv_factors[j]);
7383 }
7384 /* The faster NUW version doesn't work when InstanceID == UINT_MAX.
7385 * Such InstanceID might not be achievable in a reasonable time though.
7386 */
7387 index = ac_build_fast_udiv_nuw(&ctx->ac, ctx->abi.instance_id,
7388 udiv_factors[0], udiv_factors[1],
7389 udiv_factors[2], udiv_factors[3]);
7390 }
7391
7392 if (divisor_is_one || divisor_is_fetched) {
7393 /* Add StartInstance. */
7394 index = LLVMBuildAdd(ctx->ac.builder, index,
7395 LLVMGetParam(ctx->main_fn, user_sgpr_base +
7396 SI_SGPR_START_INSTANCE), "");
7397 } else {
7398 /* VertexID + BaseVertex */
7399 index = LLVMBuildAdd(ctx->ac.builder,
7400 ctx->abi.vertex_id,
7401 LLVMGetParam(func, user_sgpr_base +
7402 SI_SGPR_BASE_VERTEX), "");
7403 }
7404
7405 index = ac_to_float(&ctx->ac, index);
7406 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, index,
7407 fninfo.num_params + i, "");
7408 }
7409
7410 if (wrapped) {
7411 lp_build_endif(&wrap_if_state);
7412
7413 LLVMValueRef values[2] = {
7414 ret,
7415 original_ret
7416 };
7417 LLVMBasicBlockRef bbs[2] = {
7418 wrap_if_state.true_block,
7419 wrap_if_state.entry_block
7420 };
7421 ret = ac_build_phi(&ctx->ac, LLVMTypeOf(ret), 2, values, bbs);
7422 }
7423
7424 si_llvm_build_ret(ctx, ret);
7425 }
7426
7427 static bool si_get_vs_prolog(struct si_screen *sscreen,
7428 struct ac_llvm_compiler *compiler,
7429 struct si_shader *shader,
7430 struct pipe_debug_callback *debug,
7431 struct si_shader *main_part,
7432 const struct si_vs_prolog_bits *key)
7433 {
7434 struct si_shader_selector *vs = main_part->selector;
7435
7436 if (!si_vs_needs_prolog(vs, key))
7437 return true;
7438
7439 /* Get the prolog. */
7440 union si_shader_part_key prolog_key;
7441 si_get_vs_prolog_key(&vs->info, main_part->info.num_input_sgprs,
7442 key, shader, &prolog_key);
7443
7444 shader->prolog =
7445 si_get_shader_part(sscreen, &sscreen->vs_prologs,
7446 PIPE_SHADER_VERTEX, true, &prolog_key, compiler,
7447 debug, si_build_vs_prolog_function,
7448 "Vertex Shader Prolog");
7449 return shader->prolog != NULL;
7450 }
7451
7452 /**
7453 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7454 */
7455 static bool si_shader_select_vs_parts(struct si_screen *sscreen,
7456 struct ac_llvm_compiler *compiler,
7457 struct si_shader *shader,
7458 struct pipe_debug_callback *debug)
7459 {
7460 return si_get_vs_prolog(sscreen, compiler, shader, debug, shader,
7461 &shader->key.part.vs.prolog);
7462 }
7463
7464 /**
7465 * Compile the TCS epilog function. This writes tesselation factors to memory
7466 * based on the output primitive type of the tesselator (determined by TES).
7467 */
7468 static void si_build_tcs_epilog_function(struct si_shader_context *ctx,
7469 union si_shader_part_key *key)
7470 {
7471 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
7472 struct si_function_info fninfo;
7473 LLVMValueRef func;
7474
7475 si_init_function_info(&fninfo);
7476
7477 if (ctx->screen->info.chip_class >= GFX9) {
7478 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7479 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7480 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7481 add_arg(&fninfo, ARG_SGPR, ctx->i32); /* wave info */
7482 ctx->param_tcs_factor_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7483 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7484 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7485 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7486 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7487 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7488 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7489 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7490 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7491 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7492 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7493 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7494 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7495 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7496 ctx->param_tcs_out_lds_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7497 } else {
7498 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7499 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7500 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7501 add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7502 ctx->param_tcs_offchip_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7503 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7504 ctx->param_tcs_out_lds_layout = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7505 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7506 ctx->param_tcs_offchip_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7507 ctx->param_tcs_factor_offset = add_arg(&fninfo, ARG_SGPR, ctx->i32);
7508 }
7509
7510 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* VGPR gap */
7511 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* VGPR gap */
7512 unsigned tess_factors_idx =
7513 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* patch index within the wave (REL_PATCH_ID) */
7514 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* invocation ID within the patch */
7515 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* LDS offset where tess factors should be loaded from */
7516
7517 for (unsigned i = 0; i < 6; i++)
7518 add_arg(&fninfo, ARG_VGPR, ctx->i32); /* tess factors */
7519
7520 /* Create the function. */
7521 si_create_function(ctx, "tcs_epilog", NULL, 0, &fninfo,
7522 ctx->screen->info.chip_class >= GFX7 ? 128 : 64);
7523 ac_declare_lds_as_pointer(&ctx->ac);
7524 func = ctx->main_fn;
7525
7526 LLVMValueRef invoc0_tess_factors[6];
7527 for (unsigned i = 0; i < 6; i++)
7528 invoc0_tess_factors[i] = LLVMGetParam(func, tess_factors_idx + 3 + i);
7529
7530 si_write_tess_factors(bld_base,
7531 LLVMGetParam(func, tess_factors_idx),
7532 LLVMGetParam(func, tess_factors_idx + 1),
7533 LLVMGetParam(func, tess_factors_idx + 2),
7534 invoc0_tess_factors, invoc0_tess_factors + 4);
7535
7536 LLVMBuildRetVoid(ctx->ac.builder);
7537 }
7538
7539 /**
7540 * Select and compile (or reuse) TCS parts (epilog).
7541 */
7542 static bool si_shader_select_tcs_parts(struct si_screen *sscreen,
7543 struct ac_llvm_compiler *compiler,
7544 struct si_shader *shader,
7545 struct pipe_debug_callback *debug)
7546 {
7547 if (sscreen->info.chip_class >= GFX9) {
7548 struct si_shader *ls_main_part =
7549 shader->key.part.tcs.ls->main_shader_part_ls;
7550
7551 if (!si_get_vs_prolog(sscreen, compiler, shader, debug, ls_main_part,
7552 &shader->key.part.tcs.ls_prolog))
7553 return false;
7554
7555 shader->previous_stage = ls_main_part;
7556 }
7557
7558 /* Get the epilog. */
7559 union si_shader_part_key epilog_key;
7560 memset(&epilog_key, 0, sizeof(epilog_key));
7561 epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
7562
7563 shader->epilog = si_get_shader_part(sscreen, &sscreen->tcs_epilogs,
7564 PIPE_SHADER_TESS_CTRL, false,
7565 &epilog_key, compiler, debug,
7566 si_build_tcs_epilog_function,
7567 "Tessellation Control Shader Epilog");
7568 return shader->epilog != NULL;
7569 }
7570
7571 /**
7572 * Select and compile (or reuse) GS parts (prolog).
7573 */
7574 static bool si_shader_select_gs_parts(struct si_screen *sscreen,
7575 struct ac_llvm_compiler *compiler,
7576 struct si_shader *shader,
7577 struct pipe_debug_callback *debug)
7578 {
7579 if (sscreen->info.chip_class >= GFX9) {
7580 struct si_shader *es_main_part =
7581 shader->key.part.gs.es->main_shader_part_es;
7582
7583 if (shader->key.part.gs.es->type == PIPE_SHADER_VERTEX &&
7584 !si_get_vs_prolog(sscreen, compiler, shader, debug, es_main_part,
7585 &shader->key.part.gs.vs_prolog))
7586 return false;
7587
7588 shader->previous_stage = es_main_part;
7589 }
7590
7591 if (!shader->key.part.gs.prolog.tri_strip_adj_fix)
7592 return true;
7593
7594 union si_shader_part_key prolog_key;
7595 memset(&prolog_key, 0, sizeof(prolog_key));
7596 prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
7597
7598 shader->prolog2 = si_get_shader_part(sscreen, &sscreen->gs_prologs,
7599 PIPE_SHADER_GEOMETRY, true,
7600 &prolog_key, compiler, debug,
7601 si_build_gs_prolog_function,
7602 "Geometry Shader Prolog");
7603 return shader->prolog2 != NULL;
7604 }
7605
7606 /**
7607 * Build the pixel shader prolog function. This handles:
7608 * - two-side color selection and interpolation
7609 * - overriding interpolation parameters for the API PS
7610 * - polygon stippling
7611 *
7612 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7613 * overriden by other states. (e.g. per-sample interpolation)
7614 * Interpolated colors are stored after the preloaded VGPRs.
7615 */
7616 static void si_build_ps_prolog_function(struct si_shader_context *ctx,
7617 union si_shader_part_key *key)
7618 {
7619 struct si_function_info fninfo;
7620 LLVMValueRef ret, func;
7621 int num_returns, i, num_color_channels;
7622
7623 assert(si_need_ps_prolog(key));
7624
7625 si_init_function_info(&fninfo);
7626
7627 /* Declare inputs. */
7628 for (i = 0; i < key->ps_prolog.num_input_sgprs; i++)
7629 add_arg(&fninfo, ARG_SGPR, ctx->i32);
7630
7631 for (i = 0; i < key->ps_prolog.num_input_vgprs; i++)
7632 add_arg(&fninfo, ARG_VGPR, ctx->f32);
7633
7634 /* Declare outputs (same as inputs + add colors if needed) */
7635 num_returns = fninfo.num_params;
7636 num_color_channels = util_bitcount(key->ps_prolog.colors_read);
7637 for (i = 0; i < num_color_channels; i++)
7638 fninfo.types[num_returns++] = ctx->f32;
7639
7640 /* Create the function. */
7641 si_create_function(ctx, "ps_prolog", fninfo.types, num_returns,
7642 &fninfo, 0);
7643 func = ctx->main_fn;
7644
7645 /* Copy inputs to outputs. This should be no-op, as the registers match,
7646 * but it will prevent the compiler from overwriting them unintentionally.
7647 */
7648 ret = ctx->return_value;
7649 for (i = 0; i < fninfo.num_params; i++) {
7650 LLVMValueRef p = LLVMGetParam(func, i);
7651 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, p, i, "");
7652 }
7653
7654 /* Polygon stippling. */
7655 if (key->ps_prolog.states.poly_stipple) {
7656 /* POS_FIXED_PT is always last. */
7657 unsigned pos = key->ps_prolog.num_input_sgprs +
7658 key->ps_prolog.num_input_vgprs - 1;
7659 LLVMValueRef list = si_prolog_get_rw_buffers(ctx);
7660
7661 si_llvm_emit_polygon_stipple(ctx, list, pos);
7662 }
7663
7664 if (key->ps_prolog.states.bc_optimize_for_persp ||
7665 key->ps_prolog.states.bc_optimize_for_linear) {
7666 unsigned i, base = key->ps_prolog.num_input_sgprs;
7667 LLVMValueRef center[2], centroid[2], tmp, bc_optimize;
7668
7669 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7670 * The hw doesn't compute CENTROID if the whole wave only
7671 * contains fully-covered quads.
7672 *
7673 * PRIM_MASK is after user SGPRs.
7674 */
7675 bc_optimize = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
7676 bc_optimize = LLVMBuildLShr(ctx->ac.builder, bc_optimize,
7677 LLVMConstInt(ctx->i32, 31, 0), "");
7678 bc_optimize = LLVMBuildTrunc(ctx->ac.builder, bc_optimize,
7679 ctx->i1, "");
7680
7681 if (key->ps_prolog.states.bc_optimize_for_persp) {
7682 /* Read PERSP_CENTER. */
7683 for (i = 0; i < 2; i++)
7684 center[i] = LLVMGetParam(func, base + 2 + i);
7685 /* Read PERSP_CENTROID. */
7686 for (i = 0; i < 2; i++)
7687 centroid[i] = LLVMGetParam(func, base + 4 + i);
7688 /* Select PERSP_CENTROID. */
7689 for (i = 0; i < 2; i++) {
7690 tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize,
7691 center[i], centroid[i], "");
7692 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7693 tmp, base + 4 + i, "");
7694 }
7695 }
7696 if (key->ps_prolog.states.bc_optimize_for_linear) {
7697 /* Read LINEAR_CENTER. */
7698 for (i = 0; i < 2; i++)
7699 center[i] = LLVMGetParam(func, base + 8 + i);
7700 /* Read LINEAR_CENTROID. */
7701 for (i = 0; i < 2; i++)
7702 centroid[i] = LLVMGetParam(func, base + 10 + i);
7703 /* Select LINEAR_CENTROID. */
7704 for (i = 0; i < 2; i++) {
7705 tmp = LLVMBuildSelect(ctx->ac.builder, bc_optimize,
7706 center[i], centroid[i], "");
7707 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7708 tmp, base + 10 + i, "");
7709 }
7710 }
7711 }
7712
7713 /* Force per-sample interpolation. */
7714 if (key->ps_prolog.states.force_persp_sample_interp) {
7715 unsigned i, base = key->ps_prolog.num_input_sgprs;
7716 LLVMValueRef persp_sample[2];
7717
7718 /* Read PERSP_SAMPLE. */
7719 for (i = 0; i < 2; i++)
7720 persp_sample[i] = LLVMGetParam(func, base + i);
7721 /* Overwrite PERSP_CENTER. */
7722 for (i = 0; i < 2; i++)
7723 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7724 persp_sample[i], base + 2 + i, "");
7725 /* Overwrite PERSP_CENTROID. */
7726 for (i = 0; i < 2; i++)
7727 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7728 persp_sample[i], base + 4 + i, "");
7729 }
7730 if (key->ps_prolog.states.force_linear_sample_interp) {
7731 unsigned i, base = key->ps_prolog.num_input_sgprs;
7732 LLVMValueRef linear_sample[2];
7733
7734 /* Read LINEAR_SAMPLE. */
7735 for (i = 0; i < 2; i++)
7736 linear_sample[i] = LLVMGetParam(func, base + 6 + i);
7737 /* Overwrite LINEAR_CENTER. */
7738 for (i = 0; i < 2; i++)
7739 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7740 linear_sample[i], base + 8 + i, "");
7741 /* Overwrite LINEAR_CENTROID. */
7742 for (i = 0; i < 2; i++)
7743 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7744 linear_sample[i], base + 10 + i, "");
7745 }
7746
7747 /* Force center interpolation. */
7748 if (key->ps_prolog.states.force_persp_center_interp) {
7749 unsigned i, base = key->ps_prolog.num_input_sgprs;
7750 LLVMValueRef persp_center[2];
7751
7752 /* Read PERSP_CENTER. */
7753 for (i = 0; i < 2; i++)
7754 persp_center[i] = LLVMGetParam(func, base + 2 + i);
7755 /* Overwrite PERSP_SAMPLE. */
7756 for (i = 0; i < 2; i++)
7757 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7758 persp_center[i], base + i, "");
7759 /* Overwrite PERSP_CENTROID. */
7760 for (i = 0; i < 2; i++)
7761 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7762 persp_center[i], base + 4 + i, "");
7763 }
7764 if (key->ps_prolog.states.force_linear_center_interp) {
7765 unsigned i, base = key->ps_prolog.num_input_sgprs;
7766 LLVMValueRef linear_center[2];
7767
7768 /* Read LINEAR_CENTER. */
7769 for (i = 0; i < 2; i++)
7770 linear_center[i] = LLVMGetParam(func, base + 8 + i);
7771 /* Overwrite LINEAR_SAMPLE. */
7772 for (i = 0; i < 2; i++)
7773 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7774 linear_center[i], base + 6 + i, "");
7775 /* Overwrite LINEAR_CENTROID. */
7776 for (i = 0; i < 2; i++)
7777 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
7778 linear_center[i], base + 10 + i, "");
7779 }
7780
7781 /* Interpolate colors. */
7782 unsigned color_out_idx = 0;
7783 for (i = 0; i < 2; i++) {
7784 unsigned writemask = (key->ps_prolog.colors_read >> (i * 4)) & 0xf;
7785 unsigned face_vgpr = key->ps_prolog.num_input_sgprs +
7786 key->ps_prolog.face_vgpr_index;
7787 LLVMValueRef interp[2], color[4];
7788 LLVMValueRef interp_ij = NULL, prim_mask = NULL, face = NULL;
7789
7790 if (!writemask)
7791 continue;
7792
7793 /* If the interpolation qualifier is not CONSTANT (-1). */
7794 if (key->ps_prolog.color_interp_vgpr_index[i] != -1) {
7795 unsigned interp_vgpr = key->ps_prolog.num_input_sgprs +
7796 key->ps_prolog.color_interp_vgpr_index[i];
7797
7798 /* Get the (i,j) updated by bc_optimize handling. */
7799 interp[0] = LLVMBuildExtractValue(ctx->ac.builder, ret,
7800 interp_vgpr, "");
7801 interp[1] = LLVMBuildExtractValue(ctx->ac.builder, ret,
7802 interp_vgpr + 1, "");
7803 interp_ij = ac_build_gather_values(&ctx->ac, interp, 2);
7804 }
7805
7806 /* Use the absolute location of the input. */
7807 prim_mask = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
7808
7809 if (key->ps_prolog.states.color_two_side) {
7810 face = LLVMGetParam(func, face_vgpr);
7811 face = ac_to_integer(&ctx->ac, face);
7812 }
7813
7814 interp_fs_input(ctx,
7815 key->ps_prolog.color_attr_index[i],
7816 TGSI_SEMANTIC_COLOR, i,
7817 key->ps_prolog.num_interp_inputs,
7818 key->ps_prolog.colors_read, interp_ij,
7819 prim_mask, face, color);
7820
7821 while (writemask) {
7822 unsigned chan = u_bit_scan(&writemask);
7823 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, color[chan],
7824 fninfo.num_params + color_out_idx++, "");
7825 }
7826 }
7827
7828 /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
7829 * says:
7830 *
7831 * "When per-sample shading is active due to the use of a fragment
7832 * input qualified by sample or due to the use of the gl_SampleID
7833 * or gl_SamplePosition variables, only the bit for the current
7834 * sample is set in gl_SampleMaskIn. When state specifies multiple
7835 * fragment shader invocations for a given fragment, the sample
7836 * mask for any single fragment shader invocation may specify a
7837 * subset of the covered samples for the fragment. In this case,
7838 * the bit corresponding to each covered sample will be set in
7839 * exactly one fragment shader invocation."
7840 *
7841 * The samplemask loaded by hardware is always the coverage of the
7842 * entire pixel/fragment, so mask bits out based on the sample ID.
7843 */
7844 if (key->ps_prolog.states.samplemask_log_ps_iter) {
7845 /* The bit pattern matches that used by fixed function fragment
7846 * processing. */
7847 static const uint16_t ps_iter_masks[] = {
7848 0xffff, /* not used */
7849 0x5555,
7850 0x1111,
7851 0x0101,
7852 0x0001,
7853 };
7854 assert(key->ps_prolog.states.samplemask_log_ps_iter < ARRAY_SIZE(ps_iter_masks));
7855
7856 uint32_t ps_iter_mask = ps_iter_masks[key->ps_prolog.states.samplemask_log_ps_iter];
7857 unsigned ancillary_vgpr = key->ps_prolog.num_input_sgprs +
7858 key->ps_prolog.ancillary_vgpr_index;
7859 LLVMValueRef sampleid = si_unpack_param(ctx, ancillary_vgpr, 8, 4);
7860 LLVMValueRef samplemask = LLVMGetParam(func, ancillary_vgpr + 1);
7861
7862 samplemask = ac_to_integer(&ctx->ac, samplemask);
7863 samplemask = LLVMBuildAnd(
7864 ctx->ac.builder,
7865 samplemask,
7866 LLVMBuildShl(ctx->ac.builder,
7867 LLVMConstInt(ctx->i32, ps_iter_mask, false),
7868 sampleid, ""),
7869 "");
7870 samplemask = ac_to_float(&ctx->ac, samplemask);
7871
7872 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, samplemask,
7873 ancillary_vgpr + 1, "");
7874 }
7875
7876 /* Tell LLVM to insert WQM instruction sequence when needed. */
7877 if (key->ps_prolog.wqm) {
7878 LLVMAddTargetDependentFunctionAttr(func,
7879 "amdgpu-ps-wqm-outputs", "");
7880 }
7881
7882 si_llvm_build_ret(ctx, ret);
7883 }
7884
7885 /**
7886 * Build the pixel shader epilog function. This handles everything that must be
7887 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7888 */
7889 static void si_build_ps_epilog_function(struct si_shader_context *ctx,
7890 union si_shader_part_key *key)
7891 {
7892 struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
7893 struct si_function_info fninfo;
7894 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
7895 int i;
7896 struct si_ps_exports exp = {};
7897
7898 si_init_function_info(&fninfo);
7899
7900 /* Declare input SGPRs. */
7901 ctx->param_rw_buffers = add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7902 ctx->param_bindless_samplers_and_images = add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7903 ctx->param_const_and_shader_buffers = add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7904 ctx->param_samplers_and_images = add_arg(&fninfo, ARG_SGPR, ctx->ac.intptr);
7905 add_arg_checked(&fninfo, ARG_SGPR, ctx->f32, SI_PARAM_ALPHA_REF);
7906
7907 /* Declare input VGPRs. */
7908 unsigned required_num_params =
7909 fninfo.num_sgpr_params +
7910 util_bitcount(key->ps_epilog.colors_written) * 4 +
7911 key->ps_epilog.writes_z +
7912 key->ps_epilog.writes_stencil +
7913 key->ps_epilog.writes_samplemask;
7914
7915 required_num_params = MAX2(required_num_params,
7916 fninfo.num_sgpr_params + PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
7917
7918 while (fninfo.num_params < required_num_params)
7919 add_arg(&fninfo, ARG_VGPR, ctx->f32);
7920
7921 /* Create the function. */
7922 si_create_function(ctx, "ps_epilog", NULL, 0, &fninfo, 0);
7923 /* Disable elimination of unused inputs. */
7924 ac_llvm_add_target_dep_function_attr(ctx->main_fn,
7925 "InitialPSInputAddr", 0xffffff);
7926
7927 /* Process colors. */
7928 unsigned vgpr = fninfo.num_sgpr_params;
7929 unsigned colors_written = key->ps_epilog.colors_written;
7930 int last_color_export = -1;
7931
7932 /* Find the last color export. */
7933 if (!key->ps_epilog.writes_z &&
7934 !key->ps_epilog.writes_stencil &&
7935 !key->ps_epilog.writes_samplemask) {
7936 unsigned spi_format = key->ps_epilog.states.spi_shader_col_format;
7937
7938 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7939 if (colors_written == 0x1 && key->ps_epilog.states.last_cbuf > 0) {
7940 /* Just set this if any of the colorbuffers are enabled. */
7941 if (spi_format &
7942 ((1ull << (4 * (key->ps_epilog.states.last_cbuf + 1))) - 1))
7943 last_color_export = 0;
7944 } else {
7945 for (i = 0; i < 8; i++)
7946 if (colors_written & (1 << i) &&
7947 (spi_format >> (i * 4)) & 0xf)
7948 last_color_export = i;
7949 }
7950 }
7951
7952 while (colors_written) {
7953 LLVMValueRef color[4];
7954 int mrt = u_bit_scan(&colors_written);
7955
7956 for (i = 0; i < 4; i++)
7957 color[i] = LLVMGetParam(ctx->main_fn, vgpr++);
7958
7959 si_export_mrt_color(bld_base, color, mrt,
7960 fninfo.num_params - 1,
7961 mrt == last_color_export, &exp);
7962 }
7963
7964 /* Process depth, stencil, samplemask. */
7965 if (key->ps_epilog.writes_z)
7966 depth = LLVMGetParam(ctx->main_fn, vgpr++);
7967 if (key->ps_epilog.writes_stencil)
7968 stencil = LLVMGetParam(ctx->main_fn, vgpr++);
7969 if (key->ps_epilog.writes_samplemask)
7970 samplemask = LLVMGetParam(ctx->main_fn, vgpr++);
7971
7972 if (depth || stencil || samplemask)
7973 si_export_mrt_z(bld_base, depth, stencil, samplemask, &exp);
7974 else if (last_color_export == -1)
7975 ac_build_export_null(&ctx->ac);
7976
7977 if (exp.num)
7978 si_emit_ps_exports(ctx, &exp);
7979
7980 /* Compile. */
7981 LLVMBuildRetVoid(ctx->ac.builder);
7982 }
7983
7984 /**
7985 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7986 */
7987 static bool si_shader_select_ps_parts(struct si_screen *sscreen,
7988 struct ac_llvm_compiler *compiler,
7989 struct si_shader *shader,
7990 struct pipe_debug_callback *debug)
7991 {
7992 union si_shader_part_key prolog_key;
7993 union si_shader_part_key epilog_key;
7994
7995 /* Get the prolog. */
7996 si_get_ps_prolog_key(shader, &prolog_key, true);
7997
7998 /* The prolog is a no-op if these aren't set. */
7999 if (si_need_ps_prolog(&prolog_key)) {
8000 shader->prolog =
8001 si_get_shader_part(sscreen, &sscreen->ps_prologs,
8002 PIPE_SHADER_FRAGMENT, true,
8003 &prolog_key, compiler, debug,
8004 si_build_ps_prolog_function,
8005 "Fragment Shader Prolog");
8006 if (!shader->prolog)
8007 return false;
8008 }
8009
8010 /* Get the epilog. */
8011 si_get_ps_epilog_key(shader, &epilog_key);
8012
8013 shader->epilog =
8014 si_get_shader_part(sscreen, &sscreen->ps_epilogs,
8015 PIPE_SHADER_FRAGMENT, false,
8016 &epilog_key, compiler, debug,
8017 si_build_ps_epilog_function,
8018 "Fragment Shader Epilog");
8019 if (!shader->epilog)
8020 return false;
8021
8022 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
8023 if (shader->key.part.ps.prolog.poly_stipple) {
8024 shader->config.spi_ps_input_ena |= S_0286CC_POS_FIXED_PT_ENA(1);
8025 assert(G_0286CC_POS_FIXED_PT_ENA(shader->config.spi_ps_input_addr));
8026 }
8027
8028 /* Set up the enable bits for per-sample shading if needed. */
8029 if (shader->key.part.ps.prolog.force_persp_sample_interp &&
8030 (G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_ena) ||
8031 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
8032 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTER_ENA;
8033 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
8034 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_SAMPLE_ENA(1);
8035 }
8036 if (shader->key.part.ps.prolog.force_linear_sample_interp &&
8037 (G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_ena) ||
8038 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
8039 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTER_ENA;
8040 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
8041 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_SAMPLE_ENA(1);
8042 }
8043 if (shader->key.part.ps.prolog.force_persp_center_interp &&
8044 (G_0286CC_PERSP_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
8045 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
8046 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_SAMPLE_ENA;
8047 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
8048 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
8049 }
8050 if (shader->key.part.ps.prolog.force_linear_center_interp &&
8051 (G_0286CC_LINEAR_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
8052 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
8053 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_SAMPLE_ENA;
8054 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
8055 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
8056 }
8057
8058 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
8059 if (G_0286CC_POS_W_FLOAT_ENA(shader->config.spi_ps_input_ena) &&
8060 !(shader->config.spi_ps_input_ena & 0xf)) {
8061 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
8062 assert(G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_addr));
8063 }
8064
8065 /* At least one pair of interpolation weights must be enabled. */
8066 if (!(shader->config.spi_ps_input_ena & 0x7f)) {
8067 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
8068 assert(G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_addr));
8069 }
8070
8071 /* Samplemask fixup requires the sample ID. */
8072 if (shader->key.part.ps.prolog.samplemask_log_ps_iter) {
8073 shader->config.spi_ps_input_ena |= S_0286CC_ANCILLARY_ENA(1);
8074 assert(G_0286CC_ANCILLARY_ENA(shader->config.spi_ps_input_addr));
8075 }
8076
8077 /* The sample mask input is always enabled, because the API shader always
8078 * passes it through to the epilog. Disable it here if it's unused.
8079 */
8080 if (!shader->key.part.ps.epilog.poly_line_smoothing &&
8081 !shader->selector->info.reads_samplemask)
8082 shader->config.spi_ps_input_ena &= C_0286CC_SAMPLE_COVERAGE_ENA;
8083
8084 return true;
8085 }
8086
8087 void si_multiwave_lds_size_workaround(struct si_screen *sscreen,
8088 unsigned *lds_size)
8089 {
8090 /* If tessellation is all offchip and on-chip GS isn't used, this
8091 * workaround is not needed.
8092 */
8093 return;
8094
8095 /* SPI barrier management bug:
8096 * Make sure we have at least 4k of LDS in use to avoid the bug.
8097 * It applies to workgroup sizes of more than one wavefront.
8098 */
8099 if (sscreen->info.family == CHIP_BONAIRE ||
8100 sscreen->info.family == CHIP_KABINI)
8101 *lds_size = MAX2(*lds_size, 8);
8102 }
8103
8104 static void si_fix_resource_usage(struct si_screen *sscreen,
8105 struct si_shader *shader)
8106 {
8107 unsigned min_sgprs = shader->info.num_input_sgprs + 2; /* VCC */
8108
8109 shader->config.num_sgprs = MAX2(shader->config.num_sgprs, min_sgprs);
8110
8111 if (shader->selector->type == PIPE_SHADER_COMPUTE &&
8112 si_get_max_workgroup_size(shader) > 64) {
8113 si_multiwave_lds_size_workaround(sscreen,
8114 &shader->config.lds_size);
8115 }
8116 }
8117
8118 bool si_shader_create(struct si_screen *sscreen, struct ac_llvm_compiler *compiler,
8119 struct si_shader *shader,
8120 struct pipe_debug_callback *debug)
8121 {
8122 struct si_shader_selector *sel = shader->selector;
8123 struct si_shader *mainp = *si_get_main_shader_part(sel, &shader->key);
8124 int r;
8125
8126 /* LS, ES, VS are compiled on demand if the main part hasn't been
8127 * compiled for that stage.
8128 *
8129 * GS are compiled on demand if the main part hasn't been compiled
8130 * for the chosen NGG-ness.
8131 *
8132 * Vertex shaders are compiled on demand when a vertex fetch
8133 * workaround must be applied.
8134 */
8135 if (shader->is_monolithic) {
8136 /* Monolithic shader (compiled as a whole, has many variants,
8137 * may take a long time to compile).
8138 */
8139 r = si_compile_tgsi_shader(sscreen, compiler, shader, debug);
8140 if (r)
8141 return false;
8142 } else {
8143 /* The shader consists of several parts:
8144 *
8145 * - the middle part is the user shader, it has 1 variant only
8146 * and it was compiled during the creation of the shader
8147 * selector
8148 * - the prolog part is inserted at the beginning
8149 * - the epilog part is inserted at the end
8150 *
8151 * The prolog and epilog have many (but simple) variants.
8152 *
8153 * Starting with gfx9, geometry and tessellation control
8154 * shaders also contain the prolog and user shader parts of
8155 * the previous shader stage.
8156 */
8157
8158 if (!mainp)
8159 return false;
8160
8161 /* Copy the compiled TGSI shader data over. */
8162 shader->is_binary_shared = true;
8163 shader->binary = mainp->binary;
8164 shader->config = mainp->config;
8165 shader->info.num_input_sgprs = mainp->info.num_input_sgprs;
8166 shader->info.num_input_vgprs = mainp->info.num_input_vgprs;
8167 shader->info.face_vgpr_index = mainp->info.face_vgpr_index;
8168 shader->info.ancillary_vgpr_index = mainp->info.ancillary_vgpr_index;
8169 memcpy(shader->info.vs_output_param_offset,
8170 mainp->info.vs_output_param_offset,
8171 sizeof(mainp->info.vs_output_param_offset));
8172 shader->info.uses_instanceid = mainp->info.uses_instanceid;
8173 shader->info.nr_pos_exports = mainp->info.nr_pos_exports;
8174 shader->info.nr_param_exports = mainp->info.nr_param_exports;
8175
8176 /* Select prologs and/or epilogs. */
8177 switch (sel->type) {
8178 case PIPE_SHADER_VERTEX:
8179 if (!si_shader_select_vs_parts(sscreen, compiler, shader, debug))
8180 return false;
8181 break;
8182 case PIPE_SHADER_TESS_CTRL:
8183 if (!si_shader_select_tcs_parts(sscreen, compiler, shader, debug))
8184 return false;
8185 break;
8186 case PIPE_SHADER_TESS_EVAL:
8187 break;
8188 case PIPE_SHADER_GEOMETRY:
8189 if (!si_shader_select_gs_parts(sscreen, compiler, shader, debug))
8190 return false;
8191 break;
8192 case PIPE_SHADER_FRAGMENT:
8193 if (!si_shader_select_ps_parts(sscreen, compiler, shader, debug))
8194 return false;
8195
8196 /* Make sure we have at least as many VGPRs as there
8197 * are allocated inputs.
8198 */
8199 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
8200 shader->info.num_input_vgprs);
8201 break;
8202 }
8203
8204 /* Update SGPR and VGPR counts. */
8205 if (shader->prolog) {
8206 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
8207 shader->prolog->config.num_sgprs);
8208 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
8209 shader->prolog->config.num_vgprs);
8210 }
8211 if (shader->previous_stage) {
8212 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
8213 shader->previous_stage->config.num_sgprs);
8214 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
8215 shader->previous_stage->config.num_vgprs);
8216 shader->config.spilled_sgprs =
8217 MAX2(shader->config.spilled_sgprs,
8218 shader->previous_stage->config.spilled_sgprs);
8219 shader->config.spilled_vgprs =
8220 MAX2(shader->config.spilled_vgprs,
8221 shader->previous_stage->config.spilled_vgprs);
8222 shader->info.private_mem_vgprs =
8223 MAX2(shader->info.private_mem_vgprs,
8224 shader->previous_stage->info.private_mem_vgprs);
8225 shader->config.scratch_bytes_per_wave =
8226 MAX2(shader->config.scratch_bytes_per_wave,
8227 shader->previous_stage->config.scratch_bytes_per_wave);
8228 shader->info.uses_instanceid |=
8229 shader->previous_stage->info.uses_instanceid;
8230 }
8231 if (shader->prolog2) {
8232 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
8233 shader->prolog2->config.num_sgprs);
8234 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
8235 shader->prolog2->config.num_vgprs);
8236 }
8237 if (shader->epilog) {
8238 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
8239 shader->epilog->config.num_sgprs);
8240 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
8241 shader->epilog->config.num_vgprs);
8242 }
8243 si_calculate_max_simd_waves(shader);
8244 }
8245
8246 if (shader->key.as_ngg) {
8247 assert(!shader->key.as_es && !shader->key.as_ls);
8248 gfx10_ngg_calculate_subgroup_info(shader);
8249 } else if (sscreen->info.chip_class >= GFX9 && sel->type == PIPE_SHADER_GEOMETRY) {
8250 gfx9_get_gs_info(shader->previous_stage_sel, sel, &shader->gs_info);
8251 }
8252
8253 si_fix_resource_usage(sscreen, shader);
8254 si_shader_dump(sscreen, shader, debug, sel->info.processor,
8255 stderr, true);
8256
8257 /* Upload. */
8258 if (!si_shader_binary_upload(sscreen, shader, 0)) {
8259 fprintf(stderr, "LLVM failed to upload shader\n");
8260 return false;
8261 }
8262
8263 return true;
8264 }
8265
8266 void si_shader_destroy(struct si_shader *shader)
8267 {
8268 if (shader->scratch_bo)
8269 si_resource_reference(&shader->scratch_bo, NULL);
8270
8271 si_resource_reference(&shader->bo, NULL);
8272
8273 if (!shader->is_binary_shared)
8274 si_shader_binary_clean(&shader->binary);
8275
8276 free(shader->shader_log);
8277 }