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