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