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