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