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