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