2 * Copyright 2012 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
29 #include "gallivm/lp_bld_const.h"
30 #include "gallivm/lp_bld_gather.h"
31 #include "gallivm/lp_bld_intr.h"
32 #include "gallivm/lp_bld_logic.h"
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_flow.h"
35 #include "gallivm/lp_bld_misc.h"
36 #include "util/u_memory.h"
37 #include "util/u_string.h"
38 #include "tgsi/tgsi_build.h"
39 #include "tgsi/tgsi_util.h"
40 #include "tgsi/tgsi_dump.h"
42 #include "ac_binary.h"
43 #include "ac_llvm_util.h"
44 #include "ac_exp_param.h"
45 #include "si_shader_internal.h"
50 static const char *scratch_rsrc_dword0_symbol
=
51 "SCRATCH_RSRC_DWORD0";
53 static const char *scratch_rsrc_dword1_symbol
=
54 "SCRATCH_RSRC_DWORD1";
56 struct si_shader_output_values
58 LLVMValueRef values
[4];
59 unsigned semantic_name
;
60 unsigned semantic_index
;
61 ubyte vertex_stream
[4];
64 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
65 struct si_screen
*sscreen
,
66 LLVMTargetMachineRef tm
);
68 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
69 struct lp_build_tgsi_context
*bld_base
,
70 struct lp_build_emit_data
*emit_data
);
72 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
75 static unsigned llvm_get_type_size(LLVMTypeRef type
);
77 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
78 union si_shader_part_key
*key
);
79 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
80 union si_shader_part_key
*key
);
81 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
82 union si_shader_part_key
*key
);
83 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
84 union si_shader_part_key
*key
);
86 /* Ideally pass the sample mask input to the PS epilog as v13, which
87 * is its usual location, so that the shader doesn't have to add v_mov.
89 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
96 static bool is_merged_shader(struct si_shader
*shader
)
98 if (shader
->selector
->screen
->b
.chip_class
<= VI
)
101 return shader
->key
.as_ls
||
103 shader
->selector
->type
== PIPE_SHADER_TESS_CTRL
||
104 shader
->selector
->type
== PIPE_SHADER_GEOMETRY
;
108 * Returns a unique index for a per-patch semantic name and index. The index
109 * must be less than 32, so that a 32-bit bitmask of used inputs or outputs
112 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name
, unsigned index
)
114 switch (semantic_name
) {
115 case TGSI_SEMANTIC_TESSOUTER
:
117 case TGSI_SEMANTIC_TESSINNER
:
119 case TGSI_SEMANTIC_PATCH
:
124 assert(!"invalid semantic name");
130 * Returns a unique index for a semantic name and index. The index must be
131 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
134 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
136 switch (semantic_name
) {
137 case TGSI_SEMANTIC_POSITION
:
139 case TGSI_SEMANTIC_GENERIC
:
140 /* Since some shader stages use the the highest used IO index
141 * to determine the size to allocate for inputs/outputs
142 * (in LDS, tess and GS rings). GENERIC should be placed right
143 * after POSITION to make that size as small as possible.
145 if (index
< SI_MAX_IO_GENERIC
)
148 assert(!"invalid generic index");
150 case TGSI_SEMANTIC_PSIZE
:
151 return SI_MAX_IO_GENERIC
+ 1;
152 case TGSI_SEMANTIC_CLIPDIST
:
154 return SI_MAX_IO_GENERIC
+ 2 + index
;
155 case TGSI_SEMANTIC_FOG
:
156 return SI_MAX_IO_GENERIC
+ 4;
157 case TGSI_SEMANTIC_LAYER
:
158 return SI_MAX_IO_GENERIC
+ 5;
159 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
160 return SI_MAX_IO_GENERIC
+ 6;
161 case TGSI_SEMANTIC_PRIMID
:
162 return SI_MAX_IO_GENERIC
+ 7;
163 case TGSI_SEMANTIC_COLOR
: /* these alias */
164 case TGSI_SEMANTIC_BCOLOR
:
166 return SI_MAX_IO_GENERIC
+ 8 + index
;
167 case TGSI_SEMANTIC_TEXCOORD
:
169 assert(SI_MAX_IO_GENERIC
+ 10 + index
< 64);
170 return SI_MAX_IO_GENERIC
+ 10 + index
;
172 assert(!"invalid semantic name");
178 * Get the value of a shader input parameter and extract a bitfield.
180 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
181 unsigned param
, unsigned rshift
,
184 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
185 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
,
188 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
189 value
= bitcast(&ctx
->bld_base
,
190 TGSI_TYPE_UNSIGNED
, value
);
193 value
= LLVMBuildLShr(gallivm
->builder
, value
,
194 LLVMConstInt(ctx
->i32
, rshift
, 0), "");
196 if (rshift
+ bitwidth
< 32) {
197 unsigned mask
= (1 << bitwidth
) - 1;
198 value
= LLVMBuildAnd(gallivm
->builder
, value
,
199 LLVMConstInt(ctx
->i32
, mask
, 0), "");
205 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
208 case PIPE_SHADER_TESS_CTRL
:
209 return unpack_param(ctx
, ctx
->param_tcs_rel_ids
, 0, 8);
211 case PIPE_SHADER_TESS_EVAL
:
212 return LLVMGetParam(ctx
->main_fn
,
213 ctx
->param_tes_rel_patch_id
);
221 /* Tessellation shaders pass outputs to the next shader using LDS.
223 * LS outputs = TCS inputs
224 * TCS outputs = TES inputs
227 * - TCS inputs for patch 0
228 * - TCS inputs for patch 1
229 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
231 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
232 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
233 * - TCS outputs for patch 1
234 * - Per-patch TCS outputs for patch 1
235 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
236 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
239 * All three shaders VS(LS), TCS, TES share the same LDS space.
243 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
245 return unpack_param(ctx
, ctx
->param_vs_state_bits
, 8, 13);
249 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
251 return unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 0, 13);
255 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
257 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
259 ctx
->param_tcs_out_lds_offsets
,
265 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
267 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
269 ctx
->param_tcs_out_lds_offsets
,
275 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
277 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
278 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
279 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
281 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
285 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
287 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
288 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
289 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
290 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
292 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
293 LLVMBuildMul(gallivm
->builder
, patch_stride
,
299 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
301 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
302 LLVMValueRef patch0_patch_data_offset
=
303 get_tcs_out_patch0_patch_data_offset(ctx
);
304 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
305 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
307 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
308 LLVMBuildMul(gallivm
->builder
, patch_stride
,
313 static LLVMValueRef
get_instance_index_for_fetch(
314 struct si_shader_context
*ctx
,
315 unsigned param_start_instance
, LLVMValueRef divisor
)
317 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
319 LLVMValueRef result
= LLVMGetParam(ctx
->main_fn
,
320 ctx
->param_instance_id
);
322 /* The division must be done before START_INSTANCE is added. */
323 if (divisor
!= ctx
->i32_1
)
324 result
= LLVMBuildUDiv(gallivm
->builder
, result
, divisor
, "");
326 return LLVMBuildAdd(gallivm
->builder
, result
,
327 LLVMGetParam(ctx
->main_fn
, param_start_instance
), "");
330 /* Bitcast <4 x float> to <2 x double>, extract the component, and convert
332 static LLVMValueRef
extract_double_to_float(struct si_shader_context
*ctx
,
334 unsigned double_index
)
336 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
337 LLVMTypeRef f64
= LLVMDoubleTypeInContext(ctx
->gallivm
.context
);
338 LLVMValueRef dvec2
= LLVMBuildBitCast(builder
, vec4
,
339 LLVMVectorType(f64
, 2), "");
340 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, double_index
, 0);
341 LLVMValueRef value
= LLVMBuildExtractElement(builder
, dvec2
, index
, "");
342 return LLVMBuildFPTrunc(builder
, value
, ctx
->f32
, "");
345 static void declare_input_vs(
346 struct si_shader_context
*ctx
,
347 unsigned input_index
,
348 const struct tgsi_full_declaration
*decl
,
351 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
355 unsigned num_fetches
;
356 unsigned fetch_stride
;
358 LLVMValueRef t_list_ptr
;
359 LLVMValueRef t_offset
;
361 LLVMValueRef vertex_index
;
362 LLVMValueRef input
[3];
364 /* Load the T list */
365 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_vertex_buffers
);
367 t_offset
= LLVMConstInt(ctx
->i32
, input_index
, 0);
369 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
371 vertex_index
= LLVMGetParam(ctx
->main_fn
,
372 ctx
->param_vertex_index0
+
375 fix_fetch
= ctx
->shader
->key
.mono
.vs_fix_fetch
[input_index
];
377 /* Do multiple loads for special formats. */
379 case SI_FIX_FETCH_RGB_64_FLOAT
:
380 num_fetches
= 3; /* 3 2-dword loads */
383 case SI_FIX_FETCH_RGBA_64_FLOAT
:
384 num_fetches
= 2; /* 2 4-dword loads */
387 case SI_FIX_FETCH_RGB_8
:
388 case SI_FIX_FETCH_RGB_8_INT
:
392 case SI_FIX_FETCH_RGB_16
:
393 case SI_FIX_FETCH_RGB_16_INT
:
402 for (unsigned i
= 0; i
< num_fetches
; i
++) {
403 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, fetch_stride
* i
, 0);
405 input
[i
] = ac_build_buffer_load_format(&ctx
->ac
, t_list
,
406 vertex_index
, voffset
,
410 /* Break up the vec4 into individual components */
411 for (chan
= 0; chan
< 4; chan
++) {
412 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
413 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
414 input
[0], llvm_chan
, "");
418 case SI_FIX_FETCH_A2_SNORM
:
419 case SI_FIX_FETCH_A2_SSCALED
:
420 case SI_FIX_FETCH_A2_SINT
: {
421 /* The hardware returns an unsigned value; convert it to a
424 LLVMValueRef tmp
= out
[3];
425 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
427 /* First, recover the sign-extended signed integer value. */
428 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
429 tmp
= LLVMBuildFPToUI(gallivm
->builder
, tmp
, ctx
->i32
, "");
431 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->i32
, "");
433 /* For the integer-like cases, do a natural sign extension.
435 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
436 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
439 tmp
= LLVMBuildShl(gallivm
->builder
, tmp
,
440 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
441 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
442 tmp
= LLVMBuildAShr(gallivm
->builder
, tmp
, c30
, "");
444 /* Convert back to the right type. */
445 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
447 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
448 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
449 clamp
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
450 tmp
= LLVMBuildSelect(gallivm
->builder
, clamp
, neg_one
, tmp
, "");
451 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
452 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
458 case SI_FIX_FETCH_RGBA_32_UNORM
:
459 case SI_FIX_FETCH_RGBX_32_UNORM
:
460 for (chan
= 0; chan
< 4; chan
++) {
461 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
463 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
464 out
[chan
], ctx
->f32
, "");
465 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
466 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
468 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
469 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
470 out
[3] = LLVMConstReal(ctx
->f32
, 1);
472 case SI_FIX_FETCH_RGBA_32_SNORM
:
473 case SI_FIX_FETCH_RGBX_32_SNORM
:
474 case SI_FIX_FETCH_RGBA_32_FIXED
:
475 case SI_FIX_FETCH_RGBX_32_FIXED
: {
477 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
478 scale
= 1.0 / 0x10000;
480 scale
= 1.0 / INT_MAX
;
482 for (chan
= 0; chan
< 4; chan
++) {
483 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
485 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
486 out
[chan
], ctx
->f32
, "");
487 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
488 LLVMConstReal(ctx
->f32
, scale
), "");
490 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
491 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
492 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
493 out
[3] = LLVMConstReal(ctx
->f32
, 1);
496 case SI_FIX_FETCH_RGBA_32_USCALED
:
497 for (chan
= 0; chan
< 4; chan
++) {
498 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
500 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
501 out
[chan
], ctx
->f32
, "");
504 case SI_FIX_FETCH_RGBA_32_SSCALED
:
505 for (chan
= 0; chan
< 4; chan
++) {
506 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
508 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
509 out
[chan
], ctx
->f32
, "");
512 case SI_FIX_FETCH_RG_64_FLOAT
:
513 for (chan
= 0; chan
< 2; chan
++)
514 out
[chan
] = extract_double_to_float(ctx
, input
[0], chan
);
516 out
[2] = LLVMConstReal(ctx
->f32
, 0);
517 out
[3] = LLVMConstReal(ctx
->f32
, 1);
519 case SI_FIX_FETCH_RGB_64_FLOAT
:
520 for (chan
= 0; chan
< 3; chan
++)
521 out
[chan
] = extract_double_to_float(ctx
, input
[chan
], 0);
523 out
[3] = LLVMConstReal(ctx
->f32
, 1);
525 case SI_FIX_FETCH_RGBA_64_FLOAT
:
526 for (chan
= 0; chan
< 4; chan
++) {
527 out
[chan
] = extract_double_to_float(ctx
, input
[chan
/ 2],
531 case SI_FIX_FETCH_RGB_8
:
532 case SI_FIX_FETCH_RGB_8_INT
:
533 case SI_FIX_FETCH_RGB_16
:
534 case SI_FIX_FETCH_RGB_16_INT
:
535 for (chan
= 0; chan
< 3; chan
++) {
536 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
540 if (fix_fetch
== SI_FIX_FETCH_RGB_8
||
541 fix_fetch
== SI_FIX_FETCH_RGB_16
) {
542 out
[3] = LLVMConstReal(ctx
->f32
, 1);
544 out
[3] = LLVMBuildBitCast(gallivm
->builder
, ctx
->i32_1
,
551 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
554 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
560 case PIPE_SHADER_VERTEX
:
561 return LLVMGetParam(ctx
->main_fn
,
562 ctx
->param_vs_prim_id
);
563 case PIPE_SHADER_TESS_CTRL
:
564 return LLVMGetParam(ctx
->main_fn
,
565 ctx
->param_tcs_patch_id
);
566 case PIPE_SHADER_TESS_EVAL
:
567 return LLVMGetParam(ctx
->main_fn
,
568 ctx
->param_tes_patch_id
);
569 case PIPE_SHADER_GEOMETRY
:
570 return LLVMGetParam(ctx
->main_fn
,
571 ctx
->param_gs_prim_id
);
579 * Return the value of tgsi_ind_register for indexing.
580 * This is the indirect index with the constant offset added to it.
582 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
583 const struct tgsi_ind_register
*ind
,
586 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
589 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
590 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
591 result
= LLVMBuildAdd(gallivm
->builder
, result
,
592 LLVMConstInt(ctx
->i32
, rel_index
, 0), "");
597 * Like get_indirect_index, but restricts the return value to a (possibly
598 * undefined) value inside [0..num).
600 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
601 const struct tgsi_ind_register
*ind
,
602 int rel_index
, unsigned num
)
604 LLVMValueRef result
= get_indirect_index(ctx
, ind
, rel_index
);
606 return si_llvm_bound_index(ctx
, result
, num
);
611 * Calculate a dword address given an input or output register and a stride.
613 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
614 const struct tgsi_full_dst_register
*dst
,
615 const struct tgsi_full_src_register
*src
,
616 LLVMValueRef vertex_dw_stride
,
617 LLVMValueRef base_addr
)
619 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
620 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
621 ubyte
*name
, *index
, *array_first
;
623 struct tgsi_full_dst_register reg
;
625 /* Set the register description. The address computation is the same
626 * for sources and destinations. */
628 reg
.Register
.File
= src
->Register
.File
;
629 reg
.Register
.Index
= src
->Register
.Index
;
630 reg
.Register
.Indirect
= src
->Register
.Indirect
;
631 reg
.Register
.Dimension
= src
->Register
.Dimension
;
632 reg
.Indirect
= src
->Indirect
;
633 reg
.Dimension
= src
->Dimension
;
634 reg
.DimIndirect
= src
->DimIndirect
;
638 /* If the register is 2-dimensional (e.g. an array of vertices
639 * in a primitive), calculate the base address of the vertex. */
640 if (reg
.Register
.Dimension
) {
643 if (reg
.Dimension
.Indirect
)
644 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
645 reg
.Dimension
.Index
);
647 index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
649 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
650 LLVMBuildMul(gallivm
->builder
, index
,
651 vertex_dw_stride
, ""), "");
654 /* Get information about the register. */
655 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
656 name
= info
->input_semantic_name
;
657 index
= info
->input_semantic_index
;
658 array_first
= info
->input_array_first
;
659 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
660 name
= info
->output_semantic_name
;
661 index
= info
->output_semantic_index
;
662 array_first
= info
->output_array_first
;
668 if (reg
.Register
.Indirect
) {
669 /* Add the relative address of the element. */
670 LLVMValueRef ind_index
;
672 if (reg
.Indirect
.ArrayID
)
673 first
= array_first
[reg
.Indirect
.ArrayID
];
675 first
= reg
.Register
.Index
;
677 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
678 reg
.Register
.Index
- first
);
680 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
681 LLVMBuildMul(gallivm
->builder
, ind_index
,
682 LLVMConstInt(ctx
->i32
, 4, 0), ""), "");
684 param
= reg
.Register
.Dimension
?
685 si_shader_io_get_unique_index(name
[first
], index
[first
]) :
686 si_shader_io_get_unique_index_patch(name
[first
], index
[first
]);
688 param
= reg
.Register
.Dimension
?
689 si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
690 index
[reg
.Register
.Index
]) :
691 si_shader_io_get_unique_index_patch(name
[reg
.Register
.Index
],
692 index
[reg
.Register
.Index
]);
695 /* Add the base address of the element. */
696 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
697 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
700 /* The offchip buffer layout for TCS->TES is
702 * - attribute 0 of patch 0 vertex 0
703 * - attribute 0 of patch 0 vertex 1
704 * - attribute 0 of patch 0 vertex 2
706 * - attribute 0 of patch 1 vertex 0
707 * - attribute 0 of patch 1 vertex 1
709 * - attribute 1 of patch 0 vertex 0
710 * - attribute 1 of patch 0 vertex 1
712 * - per patch attribute 0 of patch 0
713 * - per patch attribute 0 of patch 1
716 * Note that every attribute has 4 components.
718 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
719 LLVMValueRef rel_patch_id
,
720 LLVMValueRef vertex_index
,
721 LLVMValueRef param_index
)
723 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
724 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
725 LLVMValueRef param_stride
, constant16
;
727 vertices_per_patch
= unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 6, 6);
728 num_patches
= unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 0, 6);
729 total_vertices
= LLVMBuildMul(gallivm
->builder
, vertices_per_patch
,
732 constant16
= LLVMConstInt(ctx
->i32
, 16, 0);
734 base_addr
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
735 vertices_per_patch
, "");
737 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
740 param_stride
= total_vertices
;
742 base_addr
= rel_patch_id
;
743 param_stride
= num_patches
;
746 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
747 LLVMBuildMul(gallivm
->builder
, param_index
,
748 param_stride
, ""), "");
750 base_addr
= LLVMBuildMul(gallivm
->builder
, base_addr
, constant16
, "");
753 LLVMValueRef patch_data_offset
=
754 unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 12, 20);
756 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
757 patch_data_offset
, "");
762 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
763 struct si_shader_context
*ctx
,
764 const struct tgsi_full_dst_register
*dst
,
765 const struct tgsi_full_src_register
*src
)
767 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
768 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
769 ubyte
*name
, *index
, *array_first
;
770 struct tgsi_full_src_register reg
;
771 LLVMValueRef vertex_index
= NULL
;
772 LLVMValueRef param_index
= NULL
;
773 unsigned param_index_base
, param_base
;
775 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
777 if (reg
.Register
.Dimension
) {
779 if (reg
.Dimension
.Indirect
)
780 vertex_index
= get_indirect_index(ctx
, ®
.DimIndirect
,
781 reg
.Dimension
.Index
);
783 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
786 /* Get information about the register. */
787 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
788 name
= info
->input_semantic_name
;
789 index
= info
->input_semantic_index
;
790 array_first
= info
->input_array_first
;
791 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
792 name
= info
->output_semantic_name
;
793 index
= info
->output_semantic_index
;
794 array_first
= info
->output_array_first
;
800 if (reg
.Register
.Indirect
) {
801 if (reg
.Indirect
.ArrayID
)
802 param_base
= array_first
[reg
.Indirect
.ArrayID
];
804 param_base
= reg
.Register
.Index
;
806 param_index
= get_indirect_index(ctx
, ®
.Indirect
,
807 reg
.Register
.Index
- param_base
);
810 param_base
= reg
.Register
.Index
;
811 param_index
= ctx
->i32_0
;
814 param_index_base
= reg
.Register
.Dimension
?
815 si_shader_io_get_unique_index(name
[param_base
], index
[param_base
]) :
816 si_shader_io_get_unique_index_patch(name
[param_base
], index
[param_base
]);
818 param_index
= LLVMBuildAdd(gallivm
->builder
, param_index
,
819 LLVMConstInt(ctx
->i32
, param_index_base
, 0),
822 return get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
),
823 vertex_index
, param_index
);
826 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
827 enum tgsi_opcode_type type
, unsigned swizzle
,
828 LLVMValueRef buffer
, LLVMValueRef offset
,
829 LLVMValueRef base
, bool can_speculate
)
831 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
832 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
833 LLVMValueRef value
, value2
;
834 LLVMTypeRef llvm_type
= tgsi2llvmtype(bld_base
, type
);
835 LLVMTypeRef vec_type
= LLVMVectorType(llvm_type
, 4);
838 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
839 0, 1, 0, can_speculate
, false);
841 return LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
844 if (!tgsi_type_is_64bit(type
)) {
845 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
846 0, 1, 0, can_speculate
, false);
848 value
= LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
849 return LLVMBuildExtractElement(gallivm
->builder
, value
,
850 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
853 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
854 swizzle
* 4, 1, 0, can_speculate
, false);
856 value2
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
857 swizzle
* 4 + 4, 1, 0, can_speculate
, false);
859 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
865 * \param type output value type
866 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
867 * \param dw_addr address in dwords
869 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
870 enum tgsi_opcode_type type
, unsigned swizzle
,
871 LLVMValueRef dw_addr
)
873 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
874 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
878 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
880 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
881 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
883 return lp_build_gather_values(gallivm
, values
,
887 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
888 LLVMConstInt(ctx
->i32
, swizzle
, 0));
890 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
891 if (tgsi_type_is_64bit(type
)) {
893 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
895 value2
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
896 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
899 return LLVMBuildBitCast(gallivm
->builder
, value
,
900 tgsi2llvmtype(bld_base
, type
), "");
906 * \param swizzle offset (typically 0..3)
907 * \param dw_addr address in dwords
908 * \param value value to store
910 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
911 unsigned dw_offset_imm
, LLVMValueRef dw_addr
,
914 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
915 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
917 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
918 LLVMConstInt(ctx
->i32
, dw_offset_imm
, 0));
920 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
921 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
925 static LLVMValueRef
desc_from_addr_base64k(struct si_shader_context
*ctx
,
928 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
930 LLVMValueRef addr
= LLVMGetParam(ctx
->main_fn
, param
);
931 addr
= LLVMBuildZExt(builder
, addr
, ctx
->i64
, "");
932 addr
= LLVMBuildShl(builder
, addr
, LLVMConstInt(ctx
->i64
, 16, 0), "");
934 uint64_t desc2
= 0xffffffff;
935 uint64_t desc3
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
936 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
937 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
938 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
939 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
940 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
941 LLVMValueRef hi
= LLVMConstInt(ctx
->i64
, desc2
| (desc3
<< 32), 0);
943 LLVMValueRef desc
= LLVMGetUndef(LLVMVectorType(ctx
->i64
, 2));
944 desc
= LLVMBuildInsertElement(builder
, desc
, addr
, ctx
->i32_0
, "");
945 desc
= LLVMBuildInsertElement(builder
, desc
, hi
, ctx
->i32_1
, "");
946 return LLVMBuildBitCast(builder
, desc
, ctx
->v4i32
, "");
949 static LLVMValueRef
fetch_input_tcs(
950 struct lp_build_tgsi_context
*bld_base
,
951 const struct tgsi_full_src_register
*reg
,
952 enum tgsi_opcode_type type
, unsigned swizzle
)
954 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
955 LLVMValueRef dw_addr
, stride
;
957 stride
= unpack_param(ctx
, ctx
->param_vs_state_bits
, 24, 8);
958 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
959 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
961 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
964 static LLVMValueRef
fetch_output_tcs(
965 struct lp_build_tgsi_context
*bld_base
,
966 const struct tgsi_full_src_register
*reg
,
967 enum tgsi_opcode_type type
, unsigned swizzle
)
969 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
970 LLVMValueRef dw_addr
, stride
;
972 if (reg
->Register
.Dimension
) {
973 stride
= unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 13, 8);
974 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
975 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
977 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
978 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
981 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
984 static LLVMValueRef
fetch_input_tes(
985 struct lp_build_tgsi_context
*bld_base
,
986 const struct tgsi_full_src_register
*reg
,
987 enum tgsi_opcode_type type
, unsigned swizzle
)
989 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
990 LLVMValueRef buffer
, base
, addr
;
992 buffer
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_offchip_addr_base64k
);
994 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
995 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
997 return buffer_load(bld_base
, type
, swizzle
, buffer
, base
, addr
, true);
1000 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1001 const struct tgsi_full_instruction
*inst
,
1002 const struct tgsi_opcode_info
*info
,
1003 LLVMValueRef dst
[4])
1005 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1006 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1007 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
1008 const struct tgsi_shader_info
*sh_info
= &ctx
->shader
->selector
->info
;
1009 unsigned chan_index
;
1010 LLVMValueRef dw_addr
, stride
;
1011 LLVMValueRef buffer
, base
, buf_addr
;
1012 LLVMValueRef values
[4];
1013 bool skip_lds_store
;
1014 bool is_tess_factor
= false;
1016 /* Only handle per-patch and per-vertex outputs here.
1017 * Vectors will be lowered to scalars and this function will be called again.
1019 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1020 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1021 si_llvm_emit_store(bld_base
, inst
, info
, dst
);
1025 if (reg
->Register
.Dimension
) {
1026 stride
= unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 13, 8);
1027 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1028 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1029 skip_lds_store
= !sh_info
->reads_pervertex_outputs
;
1031 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1032 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1033 skip_lds_store
= !sh_info
->reads_perpatch_outputs
;
1035 if (!reg
->Register
.Indirect
) {
1036 int name
= sh_info
->output_semantic_name
[reg
->Register
.Index
];
1038 /* Always write tess factors into LDS for the TCS epilog. */
1039 if (name
== TGSI_SEMANTIC_TESSINNER
||
1040 name
== TGSI_SEMANTIC_TESSOUTER
) {
1041 skip_lds_store
= false;
1042 is_tess_factor
= true;
1047 buffer
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_offchip_addr_base64k
);
1049 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1050 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1053 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
1054 LLVMValueRef value
= dst
[chan_index
];
1056 if (inst
->Instruction
.Saturate
)
1057 value
= ac_build_clamp(&ctx
->ac
, value
);
1059 /* Skip LDS stores if there is no LDS read of this output. */
1060 if (!skip_lds_store
)
1061 lds_store(bld_base
, chan_index
, dw_addr
, value
);
1063 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
1064 values
[chan_index
] = value
;
1066 if (inst
->Dst
[0].Register
.WriteMask
!= 0xF && !is_tess_factor
) {
1067 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1069 4 * chan_index
, 1, 0, true, false);
1073 if (inst
->Dst
[0].Register
.WriteMask
== 0xF && !is_tess_factor
) {
1074 LLVMValueRef value
= lp_build_gather_values(gallivm
,
1076 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buf_addr
,
1077 base
, 0, 1, 0, true, false);
1081 static LLVMValueRef
fetch_input_gs(
1082 struct lp_build_tgsi_context
*bld_base
,
1083 const struct tgsi_full_src_register
*reg
,
1084 enum tgsi_opcode_type type
,
1087 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1088 struct si_shader
*shader
= ctx
->shader
;
1089 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
1090 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1091 LLVMValueRef vtx_offset
, soffset
;
1092 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1093 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1094 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
1098 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1099 return get_primitive_id(bld_base
, swizzle
);
1101 if (!reg
->Register
.Dimension
)
1104 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
1106 /* GFX9 has the ESGS ring in LDS. */
1107 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
1108 unsigned index
= reg
->Dimension
.Index
;
1110 switch (index
/ 2) {
1112 vtx_offset
= unpack_param(ctx
, ctx
->param_gs_vtx01_offset
,
1113 index
% 2 ? 16 : 0, 16);
1116 vtx_offset
= unpack_param(ctx
, ctx
->param_gs_vtx23_offset
,
1117 index
% 2 ? 16 : 0, 16);
1120 vtx_offset
= unpack_param(ctx
, ctx
->param_gs_vtx45_offset
,
1121 index
% 2 ? 16 : 0, 16);
1128 vtx_offset
= LLVMBuildAdd(gallivm
->builder
, vtx_offset
,
1129 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
1130 return lds_load(bld_base
, type
, swizzle
, vtx_offset
);
1133 /* GFX6: input load from the ESGS ring in memory. */
1134 if (swizzle
== ~0) {
1135 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1137 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1138 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
1140 return lp_build_gather_values(gallivm
, values
,
1144 /* Get the vertex offset parameter on GFX6. */
1145 unsigned vtx_offset_param
= reg
->Dimension
.Index
;
1146 if (vtx_offset_param
< 2) {
1147 vtx_offset_param
+= ctx
->param_gs_vtx0_offset
;
1149 assert(vtx_offset_param
< 6);
1150 vtx_offset_param
+= ctx
->param_gs_vtx2_offset
- 2;
1152 vtx_offset
= lp_build_mul_imm(uint
,
1153 LLVMGetParam(ctx
->main_fn
,
1157 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
) * 256, 0);
1159 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1, ctx
->i32_0
,
1160 vtx_offset
, soffset
, 0, 1, 0, true, false);
1161 if (tgsi_type_is_64bit(type
)) {
1162 LLVMValueRef value2
;
1163 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
+ 1) * 256, 0);
1165 value2
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1,
1166 ctx
->i32_0
, vtx_offset
, soffset
,
1167 0, 1, 0, true, false);
1168 return si_llvm_emit_fetch_64bit(bld_base
, type
,
1171 return LLVMBuildBitCast(gallivm
->builder
,
1173 tgsi2llvmtype(bld_base
, type
), "");
1176 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1178 switch (interpolate
) {
1179 case TGSI_INTERPOLATE_CONSTANT
:
1182 case TGSI_INTERPOLATE_LINEAR
:
1183 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1184 return SI_PARAM_LINEAR_SAMPLE
;
1185 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1186 return SI_PARAM_LINEAR_CENTROID
;
1188 return SI_PARAM_LINEAR_CENTER
;
1190 case TGSI_INTERPOLATE_COLOR
:
1191 case TGSI_INTERPOLATE_PERSPECTIVE
:
1192 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1193 return SI_PARAM_PERSP_SAMPLE
;
1194 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1195 return SI_PARAM_PERSP_CENTROID
;
1197 return SI_PARAM_PERSP_CENTER
;
1200 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1206 * Interpolate a fragment shader input.
1208 * @param ctx context
1209 * @param input_index index of the input in hardware
1210 * @param semantic_name TGSI_SEMANTIC_*
1211 * @param semantic_index semantic index
1212 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1213 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1214 * @param interp_param interpolation weights (i,j)
1215 * @param prim_mask SI_PARAM_PRIM_MASK
1216 * @param face SI_PARAM_FRONT_FACE
1217 * @param result the return value (4 components)
1219 static void interp_fs_input(struct si_shader_context
*ctx
,
1220 unsigned input_index
,
1221 unsigned semantic_name
,
1222 unsigned semantic_index
,
1223 unsigned num_interp_inputs
,
1224 unsigned colors_read_mask
,
1225 LLVMValueRef interp_param
,
1226 LLVMValueRef prim_mask
,
1228 LLVMValueRef result
[4])
1230 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1231 LLVMValueRef attr_number
;
1236 /* fs.constant returns the param from the middle vertex, so it's not
1237 * really useful for flat shading. It's meant to be used for custom
1238 * interpolation (but the intrinsic can't fetch from the other two
1241 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1242 * to do the right thing. The only reason we use fs.constant is that
1243 * fs.interp cannot be used on integers, because they can be equal
1246 * When interp is false we will use fs.constant or for newer llvm,
1247 * amdgcn.interp.mov.
1249 bool interp
= interp_param
!= NULL
;
1251 attr_number
= LLVMConstInt(ctx
->i32
, input_index
, 0);
1254 interp_param
= LLVMBuildBitCast(gallivm
->builder
, interp_param
,
1255 LLVMVectorType(ctx
->f32
, 2), "");
1257 i
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1259 j
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1263 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1264 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1265 LLVMValueRef is_face_positive
;
1266 LLVMValueRef back_attr_number
;
1268 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1269 * otherwise it's at offset "num_inputs".
1271 unsigned back_attr_offset
= num_interp_inputs
;
1272 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1273 back_attr_offset
+= 1;
1275 back_attr_number
= LLVMConstInt(ctx
->i32
, back_attr_offset
, 0);
1277 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1278 face
, ctx
->i32_0
, "");
1280 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1281 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
1282 LLVMValueRef front
, back
;
1285 front
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1286 attr_number
, prim_mask
,
1288 back
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1289 back_attr_number
, prim_mask
,
1292 front
= ac_build_fs_interp_mov(&ctx
->ac
,
1293 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1294 llvm_chan
, attr_number
, prim_mask
);
1295 back
= ac_build_fs_interp_mov(&ctx
->ac
,
1296 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1297 llvm_chan
, back_attr_number
, prim_mask
);
1300 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
1306 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1308 result
[0] = ac_build_fs_interp(&ctx
->ac
, ctx
->i32_0
,
1309 attr_number
, prim_mask
, i
, j
);
1311 result
[0] = ac_build_fs_interp_mov(&ctx
->ac
, ctx
->i32_0
,
1312 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1313 attr_number
, prim_mask
);
1316 result
[2] = LLVMConstReal(ctx
->f32
, 0.0f
);
1317 result
[3] = LLVMConstReal(ctx
->f32
, 1.0f
);
1319 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1320 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
1323 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
1324 llvm_chan
, attr_number
, prim_mask
, i
, j
);
1326 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
1327 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1328 llvm_chan
, attr_number
, prim_mask
);
1334 static void declare_input_fs(
1335 struct si_shader_context
*ctx
,
1336 unsigned input_index
,
1337 const struct tgsi_full_declaration
*decl
,
1338 LLVMValueRef out
[4])
1340 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
1341 struct si_shader
*shader
= ctx
->shader
;
1342 LLVMValueRef main_fn
= ctx
->main_fn
;
1343 LLVMValueRef interp_param
= NULL
;
1344 int interp_param_idx
;
1346 /* Get colors from input VGPRs (set by the prolog). */
1347 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1348 unsigned i
= decl
->Semantic
.Index
;
1349 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1350 unsigned mask
= colors_read
>> (i
* 4);
1351 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1352 (i
? util_bitcount(colors_read
& 0xf) : 0);
1354 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1355 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1356 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1357 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1361 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1362 decl
->Interp
.Location
);
1363 if (interp_param_idx
== -1)
1365 else if (interp_param_idx
) {
1366 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1369 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1370 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1371 shader
->selector
->info
.colors_read
, interp_param
,
1372 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1373 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1377 static LLVMValueRef
get_sample_id(struct si_shader_context
*ctx
)
1379 return unpack_param(ctx
, SI_PARAM_ANCILLARY
, 8, 4);
1384 * Load a dword from a constant buffer.
1386 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1387 LLVMValueRef resource
,
1388 LLVMValueRef offset
)
1390 return ac_build_buffer_load(&ctx
->ac
, resource
, 1, NULL
, offset
, NULL
,
1391 0, 0, 0, true, true);
1394 static LLVMValueRef
load_sample_position(struct si_shader_context
*ctx
, LLVMValueRef sample_id
)
1396 struct lp_build_context
*uint_bld
= &ctx
->bld_base
.uint_bld
;
1397 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1398 LLVMBuilderRef builder
= gallivm
->builder
;
1399 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1400 LLVMValueRef buf_index
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_SAMPLE_POSITIONS
, 0);
1401 LLVMValueRef resource
= ac_build_indexed_load_const(&ctx
->ac
, desc
, buf_index
);
1403 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1404 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1405 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1407 LLVMValueRef pos
[4] = {
1408 buffer_load_const(ctx
, resource
, offset0
),
1409 buffer_load_const(ctx
, resource
, offset1
),
1410 LLVMConstReal(ctx
->f32
, 0),
1411 LLVMConstReal(ctx
->f32
, 0)
1414 return lp_build_gather_values(gallivm
, pos
, 4);
1417 static void declare_system_value(struct si_shader_context
*ctx
,
1419 const struct tgsi_full_declaration
*decl
)
1421 struct lp_build_context
*bld
= &ctx
->bld_base
.base
;
1422 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1423 LLVMValueRef value
= 0;
1425 assert(index
< RADEON_LLVM_MAX_SYSTEM_VALUES
);
1427 switch (decl
->Semantic
.Name
) {
1428 case TGSI_SEMANTIC_INSTANCEID
:
1429 value
= LLVMGetParam(ctx
->main_fn
,
1430 ctx
->param_instance_id
);
1433 case TGSI_SEMANTIC_VERTEXID
:
1434 value
= LLVMBuildAdd(gallivm
->builder
,
1435 LLVMGetParam(ctx
->main_fn
,
1436 ctx
->param_vertex_id
),
1437 LLVMGetParam(ctx
->main_fn
,
1438 ctx
->param_base_vertex
), "");
1441 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1442 /* Unused. Clarify the meaning in indexed vs. non-indexed
1443 * draws if this is ever used again. */
1447 case TGSI_SEMANTIC_BASEVERTEX
:
1449 /* For non-indexed draws, the base vertex set by the driver
1450 * (for direct draws) or the CP (for indirect draws) is the
1451 * first vertex ID, but GLSL expects 0 to be returned.
1453 LLVMValueRef vs_state
= LLVMGetParam(ctx
->main_fn
, ctx
->param_vs_state_bits
);
1454 LLVMValueRef indexed
;
1456 indexed
= LLVMBuildLShr(gallivm
->builder
, vs_state
, ctx
->i32_1
, "");
1457 indexed
= LLVMBuildTrunc(gallivm
->builder
, indexed
, ctx
->i1
, "");
1459 value
= LLVMBuildSelect(gallivm
->builder
, indexed
,
1460 LLVMGetParam(ctx
->main_fn
, ctx
->param_base_vertex
),
1465 case TGSI_SEMANTIC_BASEINSTANCE
:
1466 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_start_instance
);
1469 case TGSI_SEMANTIC_DRAWID
:
1470 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_draw_id
);
1473 case TGSI_SEMANTIC_INVOCATIONID
:
1474 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1475 value
= unpack_param(ctx
, ctx
->param_tcs_rel_ids
, 8, 5);
1476 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
1477 value
= LLVMGetParam(ctx
->main_fn
,
1478 ctx
->param_gs_instance_id
);
1480 assert(!"INVOCATIONID not implemented");
1483 case TGSI_SEMANTIC_POSITION
:
1485 LLVMValueRef pos
[4] = {
1486 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1487 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1488 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1489 lp_build_emit_llvm_unary(&ctx
->bld_base
, TGSI_OPCODE_RCP
,
1490 LLVMGetParam(ctx
->main_fn
,
1491 SI_PARAM_POS_W_FLOAT
)),
1493 value
= lp_build_gather_values(gallivm
, pos
, 4);
1497 case TGSI_SEMANTIC_FACE
:
1498 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_FRONT_FACE
);
1501 case TGSI_SEMANTIC_SAMPLEID
:
1502 value
= get_sample_id(ctx
);
1505 case TGSI_SEMANTIC_SAMPLEPOS
: {
1506 LLVMValueRef pos
[4] = {
1507 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1508 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1509 LLVMConstReal(ctx
->f32
, 0),
1510 LLVMConstReal(ctx
->f32
, 0)
1512 pos
[0] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
1513 TGSI_OPCODE_FRC
, pos
[0]);
1514 pos
[1] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
1515 TGSI_OPCODE_FRC
, pos
[1]);
1516 value
= lp_build_gather_values(gallivm
, pos
, 4);
1520 case TGSI_SEMANTIC_SAMPLEMASK
:
1521 /* This can only occur with the OpenGL Core profile, which
1522 * doesn't support smoothing.
1524 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1527 case TGSI_SEMANTIC_TESSCOORD
:
1529 LLVMValueRef coord
[4] = {
1530 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_u
),
1531 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_v
),
1536 /* For triangles, the vector should be (u, v, 1-u-v). */
1537 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1538 PIPE_PRIM_TRIANGLES
)
1539 coord
[2] = lp_build_sub(bld
, bld
->one
,
1540 lp_build_add(bld
, coord
[0], coord
[1]));
1542 value
= lp_build_gather_values(gallivm
, coord
, 4);
1546 case TGSI_SEMANTIC_VERTICESIN
:
1547 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1548 value
= unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 26, 6);
1549 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
1550 value
= unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 6, 6);
1552 assert(!"invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
1555 case TGSI_SEMANTIC_TESSINNER
:
1556 case TGSI_SEMANTIC_TESSOUTER
:
1558 LLVMValueRef buffer
, base
, addr
;
1559 int param
= si_shader_io_get_unique_index_patch(decl
->Semantic
.Name
, 0);
1561 buffer
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_offchip_addr_base64k
);
1563 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1564 addr
= get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
), NULL
,
1565 LLVMConstInt(ctx
->i32
, param
, 0));
1567 value
= buffer_load(&ctx
->bld_base
, TGSI_TYPE_FLOAT
,
1568 ~0, buffer
, base
, addr
, true);
1573 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
1574 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
1576 LLVMValueRef buf
, slot
, val
[4];
1579 slot
= LLVMConstInt(ctx
->i32
, SI_HS_CONST_DEFAULT_TESS_LEVELS
, 0);
1580 buf
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1581 buf
= ac_build_indexed_load_const(&ctx
->ac
, buf
, slot
);
1582 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
1584 for (i
= 0; i
< 4; i
++)
1585 val
[i
] = buffer_load_const(ctx
, buf
,
1586 LLVMConstInt(ctx
->i32
, (offset
+ i
) * 4, 0));
1587 value
= lp_build_gather_values(gallivm
, val
, 4);
1591 case TGSI_SEMANTIC_PRIMID
:
1592 value
= get_primitive_id(&ctx
->bld_base
, 0);
1595 case TGSI_SEMANTIC_GRID_SIZE
:
1596 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_grid_size
);
1599 case TGSI_SEMANTIC_BLOCK_SIZE
:
1601 LLVMValueRef values
[3];
1603 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1605 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1606 unsigned sizes
[3] = {
1607 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1608 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1609 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1612 for (i
= 0; i
< 3; ++i
)
1613 values
[i
] = LLVMConstInt(ctx
->i32
, sizes
[i
], 0);
1615 value
= lp_build_gather_values(gallivm
, values
, 3);
1617 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_block_size
);
1622 case TGSI_SEMANTIC_BLOCK_ID
:
1624 LLVMValueRef values
[3];
1626 for (int i
= 0; i
< 3; i
++) {
1627 values
[i
] = ctx
->i32_0
;
1628 if (ctx
->param_block_id
[i
] >= 0) {
1629 values
[i
] = LLVMGetParam(ctx
->main_fn
,
1630 ctx
->param_block_id
[i
]);
1633 value
= lp_build_gather_values(gallivm
, values
, 3);
1637 case TGSI_SEMANTIC_THREAD_ID
:
1638 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_thread_id
);
1641 case TGSI_SEMANTIC_HELPER_INVOCATION
:
1642 value
= lp_build_intrinsic(gallivm
->builder
,
1643 "llvm.amdgcn.ps.live",
1645 LP_FUNC_ATTR_READNONE
);
1646 value
= LLVMBuildNot(gallivm
->builder
, value
, "");
1647 value
= LLVMBuildSExt(gallivm
->builder
, value
, ctx
->i32
, "");
1650 case TGSI_SEMANTIC_SUBGROUP_SIZE
:
1651 value
= LLVMConstInt(ctx
->i32
, 64, 0);
1654 case TGSI_SEMANTIC_SUBGROUP_INVOCATION
:
1655 value
= ac_get_thread_id(&ctx
->ac
);
1658 case TGSI_SEMANTIC_SUBGROUP_EQ_MASK
:
1660 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
1661 id
= LLVMBuildZExt(gallivm
->builder
, id
, ctx
->i64
, "");
1662 value
= LLVMBuildShl(gallivm
->builder
, LLVMConstInt(ctx
->i64
, 1, 0), id
, "");
1663 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->v2i32
, "");
1667 case TGSI_SEMANTIC_SUBGROUP_GE_MASK
:
1668 case TGSI_SEMANTIC_SUBGROUP_GT_MASK
:
1669 case TGSI_SEMANTIC_SUBGROUP_LE_MASK
:
1670 case TGSI_SEMANTIC_SUBGROUP_LT_MASK
:
1672 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
1673 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_GT_MASK
||
1674 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
) {
1675 /* All bits set except LSB */
1676 value
= LLVMConstInt(ctx
->i64
, -2, 0);
1679 value
= LLVMConstInt(ctx
->i64
, -1, 0);
1681 id
= LLVMBuildZExt(gallivm
->builder
, id
, ctx
->i64
, "");
1682 value
= LLVMBuildShl(gallivm
->builder
, value
, id
, "");
1683 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
||
1684 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LT_MASK
)
1685 value
= LLVMBuildNot(gallivm
->builder
, value
, "");
1686 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->v2i32
, "");
1691 assert(!"unknown system value");
1695 ctx
->system_values
[index
] = value
;
1698 static void declare_compute_memory(struct si_shader_context
*ctx
,
1699 const struct tgsi_full_declaration
*decl
)
1701 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
1702 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1704 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, LOCAL_ADDR_SPACE
);
1707 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
1708 assert(decl
->Range
.First
== decl
->Range
.Last
);
1709 assert(!ctx
->shared_memory
);
1711 var
= LLVMAddGlobalInAddressSpace(gallivm
->module
,
1712 LLVMArrayType(ctx
->i8
, sel
->local_size
),
1715 LLVMSetAlignment(var
, 4);
1717 ctx
->shared_memory
= LLVMBuildBitCast(gallivm
->builder
, var
, i8p
, "");
1720 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
1722 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
1723 ctx
->param_const_and_shader_buffers
);
1725 return ac_build_indexed_load_const(&ctx
->ac
, list_ptr
,
1726 LLVMConstInt(ctx
->i32
, si_get_constbuf_slot(i
), 0));
1729 static LLVMValueRef
fetch_constant(
1730 struct lp_build_tgsi_context
*bld_base
,
1731 const struct tgsi_full_src_register
*reg
,
1732 enum tgsi_opcode_type type
,
1735 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1736 struct lp_build_context
*base
= &bld_base
->base
;
1737 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1740 LLVMValueRef addr
, bufp
;
1741 LLVMValueRef result
;
1743 if (swizzle
== LP_CHAN_ALL
) {
1745 LLVMValueRef values
[4];
1746 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1747 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1749 return lp_build_gather_values(&ctx
->gallivm
, values
, 4);
1752 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1753 idx
= reg
->Register
.Index
* 4 + swizzle
;
1755 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1756 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
1758 index
= si_get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
1759 reg
->Dimension
.Index
,
1760 ctx
->num_const_buffers
);
1761 index
= LLVMBuildAdd(ctx
->gallivm
.builder
, index
,
1762 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
1763 bufp
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, index
);
1765 bufp
= load_const_buffer_desc(ctx
, buf
);
1767 if (reg
->Register
.Indirect
) {
1768 addr
= ctx
->addrs
[ireg
->Index
][ireg
->Swizzle
];
1769 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1770 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1771 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1772 LLVMConstInt(ctx
->i32
, idx
* 4, 0));
1774 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
1777 result
= buffer_load_const(ctx
, bufp
, addr
);
1779 if (!tgsi_type_is_64bit(type
))
1780 result
= bitcast(bld_base
, type
, result
);
1782 LLVMValueRef addr2
, result2
;
1784 addr2
= lp_build_add(&bld_base
->uint_bld
, addr
,
1785 LLVMConstInt(ctx
->i32
, 4, 0));
1786 result2
= buffer_load_const(ctx
, bufp
, addr2
);
1788 result
= si_llvm_emit_fetch_64bit(bld_base
, type
,
1794 /* Upper 16 bits must be zero. */
1795 static LLVMValueRef
si_llvm_pack_two_int16(struct si_shader_context
*ctx
,
1796 LLVMValueRef val
[2])
1798 return LLVMBuildOr(ctx
->gallivm
.builder
, val
[0],
1799 LLVMBuildShl(ctx
->gallivm
.builder
, val
[1],
1800 LLVMConstInt(ctx
->i32
, 16, 0),
1804 /* Upper 16 bits are ignored and will be dropped. */
1805 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct si_shader_context
*ctx
,
1806 LLVMValueRef val
[2])
1808 LLVMValueRef v
[2] = {
1809 LLVMBuildAnd(ctx
->gallivm
.builder
, val
[0],
1810 LLVMConstInt(ctx
->i32
, 0xffff, 0), ""),
1813 return si_llvm_pack_two_int16(ctx
, v
);
1816 /* Initialize arguments for the shader export intrinsic */
1817 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1818 LLVMValueRef
*values
,
1820 struct ac_export_args
*args
)
1822 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1823 struct lp_build_context
*base
= &bld_base
->base
;
1824 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
1825 LLVMValueRef val
[4];
1826 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1828 bool is_int8
, is_int10
;
1830 /* Default is 0xf. Adjusted below depending on the format. */
1831 args
->enabled_channels
= 0xf; /* writemask */
1833 /* Specify whether the EXEC mask represents the valid mask */
1834 args
->valid_mask
= 0;
1836 /* Specify whether this is the last export */
1839 /* Specify the target we are exporting */
1840 args
->target
= target
;
1842 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
1843 const struct si_shader_key
*key
= &ctx
->shader
->key
;
1844 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
1845 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1847 assert(cbuf
>= 0 && cbuf
< 8);
1848 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1849 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1850 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
1853 args
->compr
= false;
1854 args
->out
[0] = base
->undef
;
1855 args
->out
[1] = base
->undef
;
1856 args
->out
[2] = base
->undef
;
1857 args
->out
[3] = base
->undef
;
1859 switch (spi_shader_col_format
) {
1860 case V_028714_SPI_SHADER_ZERO
:
1861 args
->enabled_channels
= 0; /* writemask */
1862 args
->target
= V_008DFC_SQ_EXP_NULL
;
1865 case V_028714_SPI_SHADER_32_R
:
1866 args
->enabled_channels
= 1; /* writemask */
1867 args
->out
[0] = values
[0];
1870 case V_028714_SPI_SHADER_32_GR
:
1871 args
->enabled_channels
= 0x3; /* writemask */
1872 args
->out
[0] = values
[0];
1873 args
->out
[1] = values
[1];
1876 case V_028714_SPI_SHADER_32_AR
:
1877 args
->enabled_channels
= 0x9; /* writemask */
1878 args
->out
[0] = values
[0];
1879 args
->out
[3] = values
[3];
1882 case V_028714_SPI_SHADER_FP16_ABGR
:
1883 args
->compr
= 1; /* COMPR flag */
1885 for (chan
= 0; chan
< 2; chan
++) {
1886 LLVMValueRef pack_args
[2] = {
1888 values
[2 * chan
+ 1]
1890 LLVMValueRef packed
;
1892 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
1894 LLVMBuildBitCast(ctx
->gallivm
.builder
,
1895 packed
, ctx
->f32
, "");
1899 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1900 for (chan
= 0; chan
< 4; chan
++) {
1901 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
1902 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1903 LLVMConstReal(ctx
->f32
, 65535), "");
1904 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1905 LLVMConstReal(ctx
->f32
, 0.5), "");
1906 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1910 args
->compr
= 1; /* COMPR flag */
1911 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1912 si_llvm_pack_two_int16(ctx
, val
));
1913 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1914 si_llvm_pack_two_int16(ctx
, val
+2));
1917 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1918 for (chan
= 0; chan
< 4; chan
++) {
1919 /* Clamp between [-1, 1]. */
1920 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1922 LLVMConstReal(ctx
->f32
, 1));
1923 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1925 LLVMConstReal(ctx
->f32
, -1));
1926 /* Convert to a signed integer in [-32767, 32767]. */
1927 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1928 LLVMConstReal(ctx
->f32
, 32767), "");
1929 /* If positive, add 0.5, else add -0.5. */
1930 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1931 LLVMBuildSelect(builder
,
1932 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1933 val
[chan
], base
->zero
, ""),
1934 LLVMConstReal(ctx
->f32
, 0.5),
1935 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
1936 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1939 args
->compr
= 1; /* COMPR flag */
1940 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1941 si_llvm_pack_two_int32_as_int16(ctx
, val
));
1942 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1943 si_llvm_pack_two_int32_as_int16(ctx
, val
+2));
1946 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1947 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1948 is_int8
? 255 : is_int10
? 1023 : 65535, 0);
1949 LLVMValueRef max_alpha
=
1950 !is_int10
? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1953 for (chan
= 0; chan
< 4; chan
++) {
1954 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1955 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1957 chan
== 3 ? max_alpha
: max_rgb
);
1960 args
->compr
= 1; /* COMPR flag */
1961 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1962 si_llvm_pack_two_int16(ctx
, val
));
1963 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1964 si_llvm_pack_two_int16(ctx
, val
+2));
1968 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1969 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1970 is_int8
? 127 : is_int10
? 511 : 32767, 0);
1971 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1972 is_int8
? -128 : is_int10
? -512 : -32768, 0);
1973 LLVMValueRef max_alpha
=
1974 !is_int10
? max_rgb
: ctx
->i32_1
;
1975 LLVMValueRef min_alpha
=
1976 !is_int10
? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1979 for (chan
= 0; chan
< 4; chan
++) {
1980 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1981 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1983 val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
1984 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1986 val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
1989 args
->compr
= 1; /* COMPR flag */
1990 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1991 si_llvm_pack_two_int32_as_int16(ctx
, val
));
1992 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1993 si_llvm_pack_two_int32_as_int16(ctx
, val
+2));
1997 case V_028714_SPI_SHADER_32_ABGR
:
1998 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2003 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2006 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2008 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2009 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2010 SI_PARAM_ALPHA_REF
);
2012 LLVMValueRef alpha_pass
=
2013 lp_build_cmp(&bld_base
->base
,
2014 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
,
2017 lp_build_select(&bld_base
->base
,
2019 LLVMConstReal(ctx
->f32
, 1.0f
),
2020 LLVMConstReal(ctx
->f32
, -1.0f
));
2022 ac_build_kill(&ctx
->ac
, arg
);
2024 ac_build_kill(&ctx
->ac
, NULL
);
2028 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2030 unsigned samplemask_param
)
2032 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2033 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2034 LLVMValueRef coverage
;
2036 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2037 coverage
= LLVMGetParam(ctx
->main_fn
,
2039 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
2041 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
2043 &coverage
, 1, LP_FUNC_ATTR_READNONE
);
2045 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
2048 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
2049 LLVMConstReal(ctx
->f32
,
2050 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2052 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
2055 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
2056 struct ac_export_args
*pos
, LLVMValueRef
*out_elts
)
2058 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2059 struct lp_build_context
*base
= &bld_base
->base
;
2062 unsigned const_chan
;
2063 LLVMValueRef base_elt
;
2064 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2065 LLVMValueRef constbuf_index
= LLVMConstInt(ctx
->i32
,
2066 SI_VS_CONST_CLIP_PLANES
, 0);
2067 LLVMValueRef const_resource
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, constbuf_index
);
2069 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2070 struct ac_export_args
*args
= &pos
[2 + reg_index
];
2075 args
->out
[3] = LLVMConstReal(ctx
->f32
, 0.0f
);
2077 /* Compute dot products of position and user clip plane vectors */
2078 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2079 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2081 LLVMConstInt(ctx
->i32
, ((reg_index
* 4 + chan
) * 4 +
2082 const_chan
) * 4, 0);
2083 base_elt
= buffer_load_const(ctx
, const_resource
,
2086 lp_build_add(base
, args
->out
[chan
],
2087 lp_build_mul(base
, base_elt
,
2088 out_elts
[const_chan
]));
2092 args
->enabled_channels
= 0xf;
2093 args
->valid_mask
= 0;
2095 args
->target
= V_008DFC_SQ_EXP_POS
+ 2 + reg_index
;
2100 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2104 if (so
->num_outputs
)
2105 fprintf(stderr
, "STREAMOUT\n");
2107 for (i
= 0; i
< so
->num_outputs
; i
++) {
2108 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2109 so
->output
[i
].start_component
;
2110 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2111 i
, so
->output
[i
].output_buffer
,
2112 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2113 so
->output
[i
].register_index
,
2114 mask
& 1 ? "x" : "",
2115 mask
& 2 ? "y" : "",
2116 mask
& 4 ? "z" : "",
2117 mask
& 8 ? "w" : "");
2121 static void emit_streamout_output(struct si_shader_context
*ctx
,
2122 LLVMValueRef
const *so_buffers
,
2123 LLVMValueRef
const *so_write_offsets
,
2124 struct pipe_stream_output
*stream_out
,
2125 struct si_shader_output_values
*shader_out
)
2127 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2128 LLVMBuilderRef builder
= gallivm
->builder
;
2129 unsigned buf_idx
= stream_out
->output_buffer
;
2130 unsigned start
= stream_out
->start_component
;
2131 unsigned num_comps
= stream_out
->num_components
;
2132 LLVMValueRef out
[4];
2134 assert(num_comps
&& num_comps
<= 4);
2135 if (!num_comps
|| num_comps
> 4)
2138 /* Load the output as int. */
2139 for (int j
= 0; j
< num_comps
; j
++) {
2140 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2142 out
[j
] = LLVMBuildBitCast(builder
,
2143 shader_out
->values
[start
+ j
],
2147 /* Pack the output. */
2148 LLVMValueRef vdata
= NULL
;
2150 switch (num_comps
) {
2151 case 1: /* as i32 */
2154 case 2: /* as v2i32 */
2155 case 3: /* as v4i32 (aligned to 4) */
2156 case 4: /* as v4i32 */
2157 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
2158 for (int j
= 0; j
< num_comps
; j
++) {
2159 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
2160 LLVMConstInt(ctx
->i32
, j
, 0), "");
2165 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf_idx
],
2167 so_write_offsets
[buf_idx
],
2169 stream_out
->dst_offset
* 4, 1, 1, true, false);
2173 * Write streamout data to buffers for vertex stream @p stream (different
2174 * vertex streams can occur for GS copy shaders).
2176 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2177 struct si_shader_output_values
*outputs
,
2178 unsigned noutput
, unsigned stream
)
2180 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2181 struct pipe_stream_output_info
*so
= &sel
->so
;
2182 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2183 LLVMBuilderRef builder
= gallivm
->builder
;
2185 struct lp_build_if_state if_ctx
;
2187 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2188 LLVMValueRef so_vtx_count
=
2189 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2191 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2193 /* can_emit = tid < so_vtx_count; */
2194 LLVMValueRef can_emit
=
2195 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2197 /* Emit the streamout code conditionally. This actually avoids
2198 * out-of-bounds buffer access. The hw tells us via the SGPR
2199 * (so_vtx_count) which threads are allowed to emit streamout data. */
2200 lp_build_if(&if_ctx
, gallivm
, can_emit
);
2202 /* The buffer offset is computed as follows:
2203 * ByteOffset = streamout_offset[buffer_id]*4 +
2204 * (streamout_write_index + thread_id)*stride[buffer_id] +
2208 LLVMValueRef so_write_index
=
2209 LLVMGetParam(ctx
->main_fn
,
2210 ctx
->param_streamout_write_index
);
2212 /* Compute (streamout_write_index + thread_id). */
2213 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2215 /* Load the descriptor and compute the write offset for each
2216 * enabled buffer. */
2217 LLVMValueRef so_write_offset
[4] = {};
2218 LLVMValueRef so_buffers
[4];
2219 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2220 ctx
->param_rw_buffers
);
2222 for (i
= 0; i
< 4; i
++) {
2226 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
,
2227 SI_VS_STREAMOUT_BUF0
+ i
, 0);
2229 so_buffers
[i
] = ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
2231 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2232 ctx
->param_streamout_offset
[i
]);
2233 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2235 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
2236 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
2237 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
2240 /* Write streamout data. */
2241 for (i
= 0; i
< so
->num_outputs
; i
++) {
2242 unsigned reg
= so
->output
[i
].register_index
;
2247 if (stream
!= so
->output
[i
].stream
)
2250 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2251 &so
->output
[i
], &outputs
[reg
]);
2254 lp_build_endif(&if_ctx
);
2258 /* Generate export instructions for hardware VS shader stage */
2259 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
2260 struct si_shader_output_values
*outputs
,
2263 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2264 struct si_shader
*shader
= ctx
->shader
;
2265 struct lp_build_context
*base
= &bld_base
->base
;
2266 struct ac_export_args args
, pos_args
[4] = {};
2267 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2268 unsigned semantic_name
, semantic_index
;
2270 unsigned param_count
= 0;
2274 for (i
= 0; i
< noutput
; i
++) {
2275 semantic_name
= outputs
[i
].semantic_name
;
2276 semantic_index
= outputs
[i
].semantic_index
;
2277 bool export_param
= true;
2279 switch (semantic_name
) {
2280 case TGSI_SEMANTIC_POSITION
: /* ignore these */
2281 case TGSI_SEMANTIC_PSIZE
:
2282 case TGSI_SEMANTIC_CLIPVERTEX
:
2283 case TGSI_SEMANTIC_EDGEFLAG
:
2285 case TGSI_SEMANTIC_GENERIC
:
2286 /* don't process indices the function can't handle */
2287 if (semantic_index
>= SI_MAX_IO_GENERIC
)
2291 if (shader
->key
.opt
.kill_outputs
&
2292 (1ull << si_shader_io_get_unique_index(semantic_name
, semantic_index
)))
2293 export_param
= false;
2296 if (outputs
[i
].vertex_stream
[0] != 0 &&
2297 outputs
[i
].vertex_stream
[1] != 0 &&
2298 outputs
[i
].vertex_stream
[2] != 0 &&
2299 outputs
[i
].vertex_stream
[3] != 0)
2300 export_param
= false;
2303 /* Select the correct target */
2304 switch(semantic_name
) {
2305 case TGSI_SEMANTIC_PSIZE
:
2306 psize_value
= outputs
[i
].values
[0];
2308 case TGSI_SEMANTIC_EDGEFLAG
:
2309 edgeflag_value
= outputs
[i
].values
[0];
2311 case TGSI_SEMANTIC_LAYER
:
2312 layer_value
= outputs
[i
].values
[0];
2313 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2314 goto handle_semantic
;
2315 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2316 viewport_index_value
= outputs
[i
].values
[0];
2317 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2318 goto handle_semantic
;
2319 case TGSI_SEMANTIC_POSITION
:
2320 target
= V_008DFC_SQ_EXP_POS
;
2322 case TGSI_SEMANTIC_CLIPDIST
:
2323 if (shader
->key
.opt
.clip_disable
) {
2324 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2325 goto handle_semantic
;
2327 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
2329 case TGSI_SEMANTIC_CLIPVERTEX
:
2330 if (shader
->key
.opt
.clip_disable
)
2332 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
2334 case TGSI_SEMANTIC_COLOR
:
2335 case TGSI_SEMANTIC_BCOLOR
:
2336 case TGSI_SEMANTIC_PRIMID
:
2337 case TGSI_SEMANTIC_FOG
:
2338 case TGSI_SEMANTIC_TEXCOORD
:
2339 case TGSI_SEMANTIC_GENERIC
:
2342 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
2343 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2344 shader
->info
.vs_output_param_offset
[i
] = param_count
;
2350 "Warning: SI unhandled vs output type:%d\n",
2354 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, &args
);
2356 if (target
>= V_008DFC_SQ_EXP_POS
&&
2357 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
2358 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
2359 &args
, sizeof(args
));
2361 ac_build_export(&ctx
->ac
, &args
);
2364 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
2365 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2366 goto handle_semantic
;
2370 shader
->info
.nr_param_exports
= param_count
;
2372 /* We need to add the position output manually if it's missing. */
2373 if (!pos_args
[0].out
[0]) {
2374 pos_args
[0].enabled_channels
= 0xf; /* writemask */
2375 pos_args
[0].valid_mask
= 0; /* EXEC mask */
2376 pos_args
[0].done
= 0; /* last export? */
2377 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
2378 pos_args
[0].compr
= 0; /* COMPR flag */
2379 pos_args
[0].out
[0] = base
->zero
; /* X */
2380 pos_args
[0].out
[1] = base
->zero
; /* Y */
2381 pos_args
[0].out
[2] = base
->zero
; /* Z */
2382 pos_args
[0].out
[3] = base
->one
; /* W */
2385 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2386 if (shader
->selector
->info
.writes_psize
||
2387 shader
->selector
->info
.writes_edgeflag
||
2388 shader
->selector
->info
.writes_viewport_index
||
2389 shader
->selector
->info
.writes_layer
) {
2390 pos_args
[1].enabled_channels
= shader
->selector
->info
.writes_psize
|
2391 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2392 (shader
->selector
->info
.writes_layer
<< 2);
2394 pos_args
[1].valid_mask
= 0; /* EXEC mask */
2395 pos_args
[1].done
= 0; /* last export? */
2396 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
2397 pos_args
[1].compr
= 0; /* COMPR flag */
2398 pos_args
[1].out
[0] = base
->zero
; /* X */
2399 pos_args
[1].out
[1] = base
->zero
; /* Y */
2400 pos_args
[1].out
[2] = base
->zero
; /* Z */
2401 pos_args
[1].out
[3] = base
->zero
; /* W */
2403 if (shader
->selector
->info
.writes_psize
)
2404 pos_args
[1].out
[0] = psize_value
;
2406 if (shader
->selector
->info
.writes_edgeflag
) {
2407 /* The output is a float, but the hw expects an integer
2408 * with the first bit containing the edge flag. */
2409 edgeflag_value
= LLVMBuildFPToUI(ctx
->gallivm
.builder
,
2412 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
2416 /* The LLVM intrinsic expects a float. */
2417 pos_args
[1].out
[1] = LLVMBuildBitCast(ctx
->gallivm
.builder
,
2422 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
2423 /* GFX9 has the layer in out.z[10:0] and the viewport
2424 * index in out.z[19:16].
2426 if (shader
->selector
->info
.writes_layer
)
2427 pos_args
[1].out
[2] = layer_value
;
2429 if (shader
->selector
->info
.writes_viewport_index
) {
2430 LLVMValueRef v
= viewport_index_value
;
2432 v
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, v
);
2433 v
= LLVMBuildShl(ctx
->gallivm
.builder
, v
,
2434 LLVMConstInt(ctx
->i32
, 16, 0), "");
2435 v
= LLVMBuildOr(ctx
->gallivm
.builder
, v
,
2436 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
,
2437 pos_args
[1].out
[2]), "");
2438 pos_args
[1].out
[2] = bitcast(bld_base
, TGSI_TYPE_FLOAT
, v
);
2439 pos_args
[1].enabled_channels
|= 1 << 2;
2442 if (shader
->selector
->info
.writes_layer
)
2443 pos_args
[1].out
[2] = layer_value
;
2445 if (shader
->selector
->info
.writes_viewport_index
) {
2446 pos_args
[1].out
[3] = viewport_index_value
;
2447 pos_args
[1].enabled_channels
|= 1 << 3;
2452 for (i
= 0; i
< 4; i
++)
2453 if (pos_args
[i
].out
[0])
2454 shader
->info
.nr_pos_exports
++;
2457 for (i
= 0; i
< 4; i
++) {
2458 if (!pos_args
[i
].out
[0])
2461 /* Specify the target we are exporting */
2462 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
2464 if (pos_idx
== shader
->info
.nr_pos_exports
)
2465 /* Specify that this is the last export */
2466 pos_args
[i
].done
= 1;
2468 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
2473 * Forward all outputs from the vertex shader to the TES. This is only used
2474 * for the fixed function TCS.
2476 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
2478 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2479 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2480 LLVMValueRef invocation_id
, buffer
, buffer_offset
;
2481 LLVMValueRef lds_vertex_stride
, lds_vertex_offset
, lds_base
;
2484 invocation_id
= unpack_param(ctx
, ctx
->param_tcs_rel_ids
, 8, 5);
2485 buffer
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_offchip_addr_base64k
);
2486 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
2488 lds_vertex_stride
= unpack_param(ctx
, ctx
->param_vs_state_bits
, 24, 8);
2489 lds_vertex_offset
= LLVMBuildMul(gallivm
->builder
, invocation_id
,
2490 lds_vertex_stride
, "");
2491 lds_base
= get_tcs_in_current_patch_offset(ctx
);
2492 lds_base
= LLVMBuildAdd(gallivm
->builder
, lds_base
, lds_vertex_offset
, "");
2494 inputs
= ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
;
2496 unsigned i
= u_bit_scan64(&inputs
);
2498 LLVMValueRef lds_ptr
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2499 LLVMConstInt(ctx
->i32
, 4 * i
, 0),
2502 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
2503 get_rel_patch_id(ctx
),
2505 LLVMConstInt(ctx
->i32
, i
, 0));
2507 LLVMValueRef value
= lds_load(bld_base
, TGSI_TYPE_SIGNED
, ~0,
2510 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buffer_addr
,
2511 buffer_offset
, 0, 1, 0, true, false);
2515 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2516 LLVMValueRef rel_patch_id
,
2517 LLVMValueRef invocation_id
,
2518 LLVMValueRef tcs_out_current_patch_data_offset
)
2520 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2521 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2522 struct si_shader
*shader
= ctx
->shader
;
2523 unsigned tess_inner_index
, tess_outer_index
;
2524 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2525 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
2526 unsigned stride
, outer_comps
, inner_comps
, i
, offset
;
2527 struct lp_build_if_state if_ctx
, inner_if_ctx
;
2529 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
2531 /* Do this only for invocation 0, because the tess levels are per-patch,
2534 * This can't jump, because invocation 0 executes this. It should
2535 * at least mask out the loads and stores for other invocations.
2537 lp_build_if(&if_ctx
, gallivm
,
2538 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2539 invocation_id
, ctx
->i32_0
, ""));
2541 /* Determine the layout of one tess factor element in the buffer. */
2542 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
2543 case PIPE_PRIM_LINES
:
2544 stride
= 2; /* 2 dwords, 1 vec2 store */
2548 case PIPE_PRIM_TRIANGLES
:
2549 stride
= 4; /* 4 dwords, 1 vec4 store */
2553 case PIPE_PRIM_QUADS
:
2554 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2563 /* Load tess_inner and tess_outer from LDS.
2564 * Any invocation can write them, so we can't get them from a temporary.
2566 tess_inner_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER
, 0);
2567 tess_outer_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER
, 0);
2569 lds_base
= tcs_out_current_patch_data_offset
;
2570 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2571 LLVMConstInt(ctx
->i32
,
2572 tess_inner_index
* 4, 0), "");
2573 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2574 LLVMConstInt(ctx
->i32
,
2575 tess_outer_index
* 4, 0), "");
2577 for (i
= 0; i
< 4; i
++) {
2578 inner
[i
] = LLVMGetUndef(ctx
->i32
);
2579 outer
[i
] = LLVMGetUndef(ctx
->i32
);
2582 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
2583 /* For isolines, the hardware expects tess factors in the
2584 * reverse order from what GLSL / TGSI specify.
2586 outer
[0] = out
[1] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 0, lds_outer
);
2587 outer
[1] = out
[0] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 1, lds_outer
);
2589 for (i
= 0; i
< outer_comps
; i
++) {
2591 lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2593 for (i
= 0; i
< inner_comps
; i
++) {
2594 inner
[i
] = out
[outer_comps
+i
] =
2595 lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2599 /* Convert the outputs to vectors for stores. */
2600 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2604 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2606 /* Get the buffer. */
2607 buffer
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_factor_addr_base64k
);
2609 /* Get the offset. */
2610 tf_base
= LLVMGetParam(ctx
->main_fn
,
2611 ctx
->param_tcs_factor_offset
);
2612 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2613 LLVMConstInt(ctx
->i32
, 4 * stride
, 0), "");
2615 lp_build_if(&inner_if_ctx
, gallivm
,
2616 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2617 rel_patch_id
, ctx
->i32_0
, ""));
2619 /* Store the dynamic HS control word. */
2621 if (ctx
->screen
->b
.chip_class
<= VI
) {
2622 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
2623 LLVMConstInt(ctx
->i32
, 0x80000000, 0),
2624 1, ctx
->i32_0
, tf_base
,
2625 offset
, 1, 0, true, false);
2629 lp_build_endif(&inner_if_ctx
);
2631 /* Store the tessellation factors. */
2632 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
2633 MIN2(stride
, 4), byteoffset
, tf_base
,
2634 offset
, 1, 0, true, false);
2637 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
2638 stride
- 4, byteoffset
, tf_base
,
2639 offset
, 1, 0, true, false);
2641 /* Store the tess factors into the offchip buffer if TES reads them. */
2642 if (shader
->key
.part
.tcs
.epilog
.tes_reads_tess_factors
) {
2643 LLVMValueRef buf
, base
, inner_vec
, outer_vec
, tf_outer_offset
;
2644 LLVMValueRef tf_inner_offset
;
2645 unsigned param_outer
, param_inner
;
2647 buf
= desc_from_addr_base64k(ctx
, ctx
->param_tcs_offchip_addr_base64k
);
2648 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
2650 param_outer
= si_shader_io_get_unique_index_patch(
2651 TGSI_SEMANTIC_TESSOUTER
, 0);
2652 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
2653 LLVMConstInt(ctx
->i32
, param_outer
, 0));
2655 outer_vec
= lp_build_gather_values(gallivm
, outer
,
2656 util_next_power_of_two(outer_comps
));
2658 ac_build_buffer_store_dword(&ctx
->ac
, buf
, outer_vec
,
2659 outer_comps
, tf_outer_offset
,
2660 base
, 0, 1, 0, true, false);
2662 param_inner
= si_shader_io_get_unique_index_patch(
2663 TGSI_SEMANTIC_TESSINNER
, 0);
2664 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
2665 LLVMConstInt(ctx
->i32
, param_inner
, 0));
2667 inner_vec
= inner_comps
== 1 ? inner
[0] :
2668 lp_build_gather_values(gallivm
, inner
, inner_comps
);
2669 ac_build_buffer_store_dword(&ctx
->ac
, buf
, inner_vec
,
2670 inner_comps
, tf_inner_offset
,
2671 base
, 0, 1, 0, true, false);
2675 lp_build_endif(&if_ctx
);
2679 si_insert_input_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
,
2680 unsigned param
, unsigned return_index
)
2682 return LLVMBuildInsertValue(ctx
->gallivm
.builder
, ret
,
2683 LLVMGetParam(ctx
->main_fn
, param
),
2688 si_insert_input_ret_float(struct si_shader_context
*ctx
, LLVMValueRef ret
,
2689 unsigned param
, unsigned return_index
)
2691 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
2692 LLVMValueRef p
= LLVMGetParam(ctx
->main_fn
, param
);
2694 return LLVMBuildInsertValue(builder
, ret
,
2695 LLVMBuildBitCast(builder
, p
, ctx
->f32
, ""),
2700 si_insert_input_ptr_as_2xi32(struct si_shader_context
*ctx
, LLVMValueRef ret
,
2701 unsigned param
, unsigned return_index
)
2703 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
2704 LLVMValueRef ptr
, lo
, hi
;
2706 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
2707 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i64
, "");
2708 ptr
= LLVMBuildBitCast(builder
, ptr
, ctx
->v2i32
, "");
2709 lo
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_0
, "");
2710 hi
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_1
, "");
2711 ret
= LLVMBuildInsertValue(builder
, ret
, lo
, return_index
, "");
2712 return LLVMBuildInsertValue(builder
, ret
, hi
, return_index
+ 1, "");
2715 /* This only writes the tessellation factor levels. */
2716 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2718 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2719 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2721 si_copy_tcs_inputs(bld_base
);
2723 rel_patch_id
= get_rel_patch_id(ctx
);
2724 invocation_id
= unpack_param(ctx
, ctx
->param_tcs_rel_ids
, 8, 5);
2725 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2727 /* Return epilog parameters from this function. */
2728 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
2729 LLVMValueRef ret
= ctx
->return_value
;
2732 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
2733 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
2734 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
2735 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_addr_base64k
,
2736 8 + GFX9_SGPR_TCS_OFFCHIP_ADDR_BASE64K
);
2737 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_addr_base64k
,
2738 8 + GFX9_SGPR_TCS_FACTOR_ADDR_BASE64K
);
2739 /* Tess offchip and tess factor offsets are at the beginning. */
2740 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
2741 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
2742 vgpr
= 8 + GFX9_SGPR_TCS_FACTOR_ADDR_BASE64K
+ 1;
2744 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
2745 GFX6_SGPR_TCS_OFFCHIP_LAYOUT
);
2746 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_addr_base64k
,
2747 GFX6_SGPR_TCS_OFFCHIP_ADDR_BASE64K
);
2748 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_addr_base64k
,
2749 GFX6_SGPR_TCS_FACTOR_ADDR_BASE64K
);
2750 /* Tess offchip and tess factor offsets are after user SGPRs. */
2751 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
,
2752 GFX6_TCS_NUM_USER_SGPR
);
2753 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
,
2754 GFX6_TCS_NUM_USER_SGPR
+ 1);
2755 vgpr
= GFX6_TCS_NUM_USER_SGPR
+ 2;
2759 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2760 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2761 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2763 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2764 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2765 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2766 ctx
->return_value
= ret
;
2769 /* Pass TCS inputs from LS to TCS on GFX9. */
2770 static void si_set_ls_return_value_for_tcs(struct si_shader_context
*ctx
)
2772 LLVMValueRef ret
= ctx
->return_value
;
2774 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, ctx
->param_rw_buffers
, 0);
2775 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
2776 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
2777 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
2778 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
2780 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_vs_state_bits
,
2781 8 + SI_SGPR_VS_STATE_BITS
);
2782 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
2783 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
2784 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_offsets
,
2785 8 + GFX9_SGPR_TCS_OUT_OFFSETS
);
2786 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
2787 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
2788 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_addr_base64k
,
2789 8 + GFX9_SGPR_TCS_OFFCHIP_ADDR_BASE64K
);
2790 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_addr_base64k
,
2791 8 + GFX9_SGPR_TCS_FACTOR_ADDR_BASE64K
);
2793 unsigned desc_param
= ctx
->param_tcs_factor_addr_base64k
+ 2;
2794 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, desc_param
,
2795 8 + GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS
);
2796 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, desc_param
+ 1,
2797 8 + GFX9_SGPR_TCS_SAMPLERS_AND_IMAGES
);
2799 unsigned vgpr
= 8 + GFX9_TCS_NUM_USER_SGPR
;
2800 ret
= si_insert_input_ret_float(ctx
, ret
,
2801 ctx
->param_tcs_patch_id
, vgpr
++);
2802 ret
= si_insert_input_ret_float(ctx
, ret
,
2803 ctx
->param_tcs_rel_ids
, vgpr
++);
2804 ctx
->return_value
= ret
;
2807 /* Pass GS inputs from ES to GS on GFX9. */
2808 static void si_set_es_return_value_for_gs(struct si_shader_context
*ctx
)
2810 LLVMValueRef ret
= ctx
->return_value
;
2812 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, ctx
->param_rw_buffers
, 0);
2813 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_gs2vs_offset
, 2);
2814 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
2816 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
2818 unsigned desc_param
= ctx
->param_vs_state_bits
+ 1;
2819 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, desc_param
,
2820 8 + GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS
);
2821 ret
= si_insert_input_ptr_as_2xi32(ctx
, ret
, desc_param
+ 1,
2822 8 + GFX9_SGPR_GS_SAMPLERS_AND_IMAGES
);
2824 unsigned vgpr
= 8 + GFX9_GS_NUM_USER_SGPR
;
2825 for (unsigned i
= 0; i
< 5; i
++) {
2826 unsigned param
= ctx
->param_gs_vtx01_offset
+ i
;
2827 ret
= si_insert_input_ret_float(ctx
, ret
, param
, vgpr
++);
2829 ctx
->return_value
= ret
;
2832 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2834 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2835 struct si_shader
*shader
= ctx
->shader
;
2836 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2837 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2839 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
2840 ctx
->param_rel_auto_id
);
2841 LLVMValueRef vertex_dw_stride
=
2842 unpack_param(ctx
, ctx
->param_vs_state_bits
, 24, 8);
2843 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2844 vertex_dw_stride
, "");
2846 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2847 * its inputs from it. */
2848 for (i
= 0; i
< info
->num_outputs
; i
++) {
2849 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2850 unsigned name
= info
->output_semantic_name
[i
];
2851 unsigned index
= info
->output_semantic_index
[i
];
2853 /* The ARB_shader_viewport_layer_array spec contains the
2856 * 2) What happens if gl_ViewportIndex or gl_Layer is
2857 * written in the vertex shader and a geometry shader is
2860 * RESOLVED: The value written by the last vertex processing
2861 * stage is used. If the last vertex processing stage
2862 * (vertex, tessellation evaluation or geometry) does not
2863 * statically assign to gl_ViewportIndex or gl_Layer, index
2864 * or layer zero is assumed.
2866 * So writes to those outputs in VS-as-LS are simply ignored.
2868 if (name
== TGSI_SEMANTIC_LAYER
||
2869 name
== TGSI_SEMANTIC_VIEWPORT_INDEX
)
2872 int param
= si_shader_io_get_unique_index(name
, index
);
2873 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2874 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
2876 for (chan
= 0; chan
< 4; chan
++) {
2877 lds_store(bld_base
, chan
, dw_addr
,
2878 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2882 if (ctx
->screen
->b
.chip_class
>= GFX9
)
2883 si_set_ls_return_value_for_tcs(ctx
);
2886 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2888 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2889 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2890 struct si_shader
*es
= ctx
->shader
;
2891 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2892 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
2893 ctx
->param_es2gs_offset
);
2894 LLVMValueRef lds_base
= NULL
;
2898 if (ctx
->screen
->b
.chip_class
>= GFX9
&& info
->num_outputs
) {
2899 unsigned itemsize_dw
= es
->selector
->esgs_itemsize
/ 4;
2900 lds_base
= LLVMBuildMul(gallivm
->builder
, ac_get_thread_id(&ctx
->ac
),
2901 LLVMConstInt(ctx
->i32
, itemsize_dw
, 0), "");
2904 for (i
= 0; i
< info
->num_outputs
; i
++) {
2905 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2908 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2909 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2912 param
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2913 info
->output_semantic_index
[i
]);
2915 for (chan
= 0; chan
< 4; chan
++) {
2916 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2917 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2919 /* GFX9 has the ESGS ring in LDS. */
2920 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
2921 lds_store(bld_base
, param
* 4 + chan
, lds_base
, out_val
);
2925 ac_build_buffer_store_dword(&ctx
->ac
,
2927 out_val
, 1, NULL
, soffset
,
2928 (4 * param
+ chan
) * 4,
2933 if (ctx
->screen
->b
.chip_class
>= GFX9
)
2934 si_set_es_return_value_for_gs(ctx
);
2937 static LLVMValueRef
si_get_gs_wave_id(struct si_shader_context
*ctx
)
2939 if (ctx
->screen
->b
.chip_class
>= GFX9
)
2940 return unpack_param(ctx
, ctx
->param_merged_wave_info
, 16, 8);
2942 return LLVMGetParam(ctx
->main_fn
, ctx
->param_gs_wave_id
);
2945 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2947 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2949 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
,
2950 si_get_gs_wave_id(ctx
));
2953 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2955 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2956 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2957 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2958 struct si_shader_output_values
*outputs
= NULL
;
2961 assert(!ctx
->shader
->is_gs_copy_shader
);
2963 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2965 /* Vertex color clamping.
2967 * This uses a state constant loaded in a user data SGPR and
2968 * an IF statement is added that clamps all colors if the constant
2971 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
2972 struct lp_build_if_state if_ctx
;
2973 LLVMValueRef cond
= NULL
;
2974 LLVMValueRef addr
, val
;
2976 for (i
= 0; i
< info
->num_outputs
; i
++) {
2977 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2978 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2981 /* We've found a color. */
2983 /* The state is in the first bit of the user SGPR. */
2984 cond
= LLVMGetParam(ctx
->main_fn
,
2985 ctx
->param_vs_state_bits
);
2986 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2988 lp_build_if(&if_ctx
, gallivm
, cond
);
2991 for (j
= 0; j
< 4; j
++) {
2992 addr
= ctx
->outputs
[i
][j
];
2993 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2994 val
= ac_build_clamp(&ctx
->ac
, val
);
2995 LLVMBuildStore(gallivm
->builder
, val
, addr
);
3000 lp_build_endif(&if_ctx
);
3003 for (i
= 0; i
< info
->num_outputs
; i
++) {
3004 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
3005 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
3007 for (j
= 0; j
< 4; j
++) {
3008 outputs
[i
].values
[j
] =
3009 LLVMBuildLoad(gallivm
->builder
,
3012 outputs
[i
].vertex_stream
[j
] =
3013 (info
->output_streams
[i
] >> (2 * j
)) & 3;
3017 if (ctx
->shader
->selector
->so
.num_outputs
)
3018 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
3020 /* Export PrimitiveID. */
3021 if (ctx
->shader
->key
.mono
.u
.vs_export_prim_id
) {
3022 outputs
[i
].semantic_name
= TGSI_SEMANTIC_PRIMID
;
3023 outputs
[i
].semantic_index
= 0;
3024 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
3025 get_primitive_id(bld_base
, 0));
3026 for (j
= 1; j
< 4; j
++)
3027 outputs
[i
].values
[j
] = LLVMConstReal(ctx
->f32
, 0);
3029 memset(outputs
[i
].vertex_stream
, 0,
3030 sizeof(outputs
[i
].vertex_stream
));
3034 si_llvm_export_vs(bld_base
, outputs
, i
);
3038 struct si_ps_exports
{
3040 struct ac_export_args args
[10];
3043 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
3044 bool writes_samplemask
)
3047 /* Z needs 32 bits. */
3048 if (writes_samplemask
)
3049 return V_028710_SPI_SHADER_32_ABGR
;
3050 else if (writes_stencil
)
3051 return V_028710_SPI_SHADER_32_GR
;
3053 return V_028710_SPI_SHADER_32_R
;
3054 } else if (writes_stencil
|| writes_samplemask
) {
3055 /* Both stencil and sample mask need only 16 bits. */
3056 return V_028710_SPI_SHADER_UINT16_ABGR
;
3058 return V_028710_SPI_SHADER_ZERO
;
3062 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
3063 LLVMValueRef depth
, LLVMValueRef stencil
,
3064 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
3066 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3067 struct lp_build_context
*base
= &bld_base
->base
;
3068 struct ac_export_args args
;
3070 unsigned format
= si_get_spi_shader_z_format(depth
!= NULL
,
3072 samplemask
!= NULL
);
3074 assert(depth
|| stencil
|| samplemask
);
3076 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3077 args
.done
= 1; /* DONE bit */
3079 /* Specify the target we are exporting */
3080 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
3082 args
.compr
= 0; /* COMP flag */
3083 args
.out
[0] = base
->undef
; /* R, depth */
3084 args
.out
[1] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
3085 args
.out
[2] = base
->undef
; /* B, sample mask */
3086 args
.out
[3] = base
->undef
; /* A, alpha to mask */
3088 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
3090 args
.compr
= 1; /* COMPR flag */
3093 /* Stencil should be in X[23:16]. */
3094 stencil
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, stencil
);
3095 stencil
= LLVMBuildShl(ctx
->gallivm
.builder
, stencil
,
3096 LLVMConstInt(ctx
->i32
, 16, 0), "");
3097 args
.out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
, stencil
);
3101 /* SampleMask should be in Y[15:0]. */
3102 args
.out
[1] = samplemask
;
3107 args
.out
[0] = depth
;
3111 args
.out
[1] = stencil
;
3115 args
.out
[2] = samplemask
;
3120 /* SI (except OLAND and HAINAN) has a bug that it only looks
3121 * at the X writemask component. */
3122 if (ctx
->screen
->b
.chip_class
== SI
&&
3123 ctx
->screen
->b
.family
!= CHIP_OLAND
&&
3124 ctx
->screen
->b
.family
!= CHIP_HAINAN
)
3127 /* Specify which components to enable */
3128 args
.enabled_channels
= mask
;
3130 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3133 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
3134 LLVMValueRef
*color
, unsigned index
,
3135 unsigned samplemask_param
,
3136 bool is_last
, struct si_ps_exports
*exp
)
3138 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3139 struct lp_build_context
*base
= &bld_base
->base
;
3143 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
3144 for (i
= 0; i
< 4; i
++)
3145 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
3148 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
3149 color
[3] = base
->one
;
3153 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
3154 si_alpha_test(bld_base
, color
[3]);
3156 /* Line & polygon smoothing */
3157 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
3158 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
3161 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3162 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
3163 struct ac_export_args args
[8];
3166 /* Get the export arguments, also find out what the last one is. */
3167 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3168 si_llvm_init_export_args(bld_base
, color
,
3169 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
3170 if (args
[c
].enabled_channels
)
3174 /* Emit all exports. */
3175 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3176 if (is_last
&& last
== c
) {
3177 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
3178 args
[c
].done
= 1; /* DONE bit */
3179 } else if (!args
[c
].enabled_channels
)
3180 continue; /* unnecessary NULL export */
3182 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
3185 struct ac_export_args args
;
3188 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3191 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3192 args
.done
= 1; /* DONE bit */
3193 } else if (!args
.enabled_channels
)
3194 return; /* unnecessary NULL export */
3196 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3200 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3201 struct si_ps_exports
*exp
)
3203 for (unsigned i
= 0; i
< exp
->num
; i
++)
3204 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
3207 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
3209 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3210 struct lp_build_context
*base
= &bld_base
->base
;
3211 struct ac_export_args args
;
3213 args
.enabled_channels
= 0x0; /* enabled channels */
3214 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3215 args
.done
= 1; /* DONE bit */
3216 args
.target
= V_008DFC_SQ_EXP_NULL
;
3217 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
3218 args
.out
[0] = base
->undef
; /* R */
3219 args
.out
[1] = base
->undef
; /* G */
3220 args
.out
[2] = base
->undef
; /* B */
3221 args
.out
[3] = base
->undef
; /* A */
3223 ac_build_export(&ctx
->ac
, &args
);
3227 * Return PS outputs in this order:
3229 * v[0:3] = color0.xyzw
3230 * v[4:7] = color1.xyzw
3235 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3237 * The alpha-ref SGPR is returned via its original location.
3239 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
3241 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3242 struct si_shader
*shader
= ctx
->shader
;
3243 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3244 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3245 unsigned i
, j
, first_vgpr
, vgpr
;
3247 LLVMValueRef color
[8][4] = {};
3248 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3251 if (ctx
->postponed_kill
)
3252 ac_build_kill(&ctx
->ac
, LLVMBuildLoad(builder
, ctx
->postponed_kill
, ""));
3254 /* Read the output values. */
3255 for (i
= 0; i
< info
->num_outputs
; i
++) {
3256 unsigned semantic_name
= info
->output_semantic_name
[i
];
3257 unsigned semantic_index
= info
->output_semantic_index
[i
];
3259 switch (semantic_name
) {
3260 case TGSI_SEMANTIC_COLOR
:
3261 assert(semantic_index
< 8);
3262 for (j
= 0; j
< 4; j
++) {
3263 LLVMValueRef ptr
= ctx
->outputs
[i
][j
];
3264 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3265 color
[semantic_index
][j
] = result
;
3268 case TGSI_SEMANTIC_POSITION
:
3269 depth
= LLVMBuildLoad(builder
,
3270 ctx
->outputs
[i
][2], "");
3272 case TGSI_SEMANTIC_STENCIL
:
3273 stencil
= LLVMBuildLoad(builder
,
3274 ctx
->outputs
[i
][1], "");
3276 case TGSI_SEMANTIC_SAMPLEMASK
:
3277 samplemask
= LLVMBuildLoad(builder
,
3278 ctx
->outputs
[i
][0], "");
3281 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3286 /* Fill the return structure. */
3287 ret
= ctx
->return_value
;
3290 ret
= LLVMBuildInsertValue(builder
, ret
,
3291 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
3292 LLVMGetParam(ctx
->main_fn
,
3293 SI_PARAM_ALPHA_REF
)),
3294 SI_SGPR_ALPHA_REF
, "");
3297 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3298 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3302 for (j
= 0; j
< 4; j
++)
3303 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3306 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3308 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3310 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3312 /* Add the input sample mask for smoothing at the end. */
3313 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3314 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3315 ret
= LLVMBuildInsertValue(builder
, ret
,
3316 LLVMGetParam(ctx
->main_fn
,
3317 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3319 ctx
->return_value
= ret
;
3322 /* Prevent optimizations (at least of memory accesses) across the current
3323 * point in the program by emitting empty inline assembly that is marked as
3324 * having side effects.
3326 * Optionally, a value can be passed through the inline assembly to prevent
3327 * LLVM from hoisting calls to ReadNone functions.
3329 static void emit_optimization_barrier(struct si_shader_context
*ctx
,
3330 LLVMValueRef
*pvgpr
)
3332 static int counter
= 0;
3334 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3337 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
3340 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
3341 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
3342 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
3344 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
3345 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
3346 LLVMValueRef vgpr
= *pvgpr
;
3347 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
3348 unsigned vgpr_size
= llvm_get_type_size(vgpr_type
);
3351 assert(vgpr_size
% 4 == 0);
3353 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
3354 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
3355 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
3356 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
3357 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
3363 void si_emit_waitcnt(struct si_shader_context
*ctx
, unsigned simm16
)
3365 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3366 LLVMBuilderRef builder
= gallivm
->builder
;
3367 LLVMValueRef args
[1] = {
3368 LLVMConstInt(ctx
->i32
, simm16
, 0)
3370 lp_build_intrinsic(builder
, "llvm.amdgcn.s.waitcnt",
3371 ctx
->voidt
, args
, 1, 0);
3374 static void membar_emit(
3375 const struct lp_build_tgsi_action
*action
,
3376 struct lp_build_tgsi_context
*bld_base
,
3377 struct lp_build_emit_data
*emit_data
)
3379 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3380 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3381 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3382 unsigned waitcnt
= NOOP_WAITCNT
;
3384 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3385 waitcnt
&= VM_CNT
& LGKM_CNT
;
3387 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3388 TGSI_MEMBAR_SHADER_BUFFER
|
3389 TGSI_MEMBAR_SHADER_IMAGE
))
3392 if (flags
& TGSI_MEMBAR_SHARED
)
3393 waitcnt
&= LGKM_CNT
;
3395 if (waitcnt
!= NOOP_WAITCNT
)
3396 si_emit_waitcnt(ctx
, waitcnt
);
3399 static void clock_emit(
3400 const struct lp_build_tgsi_action
*action
,
3401 struct lp_build_tgsi_context
*bld_base
,
3402 struct lp_build_emit_data
*emit_data
)
3404 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3405 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3408 tmp
= lp_build_intrinsic(gallivm
->builder
, "llvm.readcyclecounter",
3409 ctx
->i64
, NULL
, 0, 0);
3410 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->v2i32
, "");
3412 emit_data
->output
[0] =
3413 LLVMBuildExtractElement(gallivm
->builder
, tmp
, ctx
->i32_0
, "");
3414 emit_data
->output
[1] =
3415 LLVMBuildExtractElement(gallivm
->builder
, tmp
, ctx
->i32_1
, "");
3418 LLVMTypeRef
si_const_array(LLVMTypeRef elem_type
, int num_elements
)
3420 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3424 static void si_llvm_emit_ddxy(
3425 const struct lp_build_tgsi_action
*action
,
3426 struct lp_build_tgsi_context
*bld_base
,
3427 struct lp_build_emit_data
*emit_data
)
3429 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3430 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3431 unsigned opcode
= emit_data
->info
->opcode
;
3436 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3437 mask
= AC_TID_MASK_LEFT
;
3438 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3439 mask
= AC_TID_MASK_TOP
;
3441 mask
= AC_TID_MASK_TOP_LEFT
;
3443 /* for DDX we want to next X pixel, DDY next Y pixel. */
3444 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3446 val
= LLVMBuildBitCast(gallivm
->builder
, emit_data
->args
[0], ctx
->i32
, "");
3447 val
= ac_build_ddxy(&ctx
->ac
, ctx
->screen
->has_ds_bpermute
,
3448 mask
, idx
, ctx
->lds
, val
);
3449 emit_data
->output
[emit_data
->chan
] = val
;
3453 * this takes an I,J coordinate pair,
3454 * and works out the X and Y derivatives.
3455 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3457 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3458 struct lp_build_tgsi_context
*bld_base
,
3459 LLVMValueRef interp_ij
)
3461 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3462 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3463 LLVMValueRef result
[4], a
;
3466 for (i
= 0; i
< 2; i
++) {
3467 a
= LLVMBuildExtractElement(gallivm
->builder
, interp_ij
,
3468 LLVMConstInt(ctx
->i32
, i
, 0), "");
3469 result
[i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDX
, a
);
3470 result
[2+i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDY
, a
);
3473 return lp_build_gather_values(gallivm
, result
, 4);
3476 static void interp_fetch_args(
3477 struct lp_build_tgsi_context
*bld_base
,
3478 struct lp_build_emit_data
*emit_data
)
3480 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3481 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3482 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3484 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3485 /* offset is in second src, first two channels */
3486 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3489 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3492 emit_data
->arg_count
= 2;
3493 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3494 LLVMValueRef sample_position
;
3495 LLVMValueRef sample_id
;
3496 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
3498 /* fetch sample ID, then fetch its sample position,
3499 * and place into first two channels.
3501 sample_id
= lp_build_emit_fetch(bld_base
,
3502 emit_data
->inst
, 1, TGSI_CHAN_X
);
3503 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3505 sample_position
= load_sample_position(ctx
, sample_id
);
3507 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3511 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3512 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3515 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3516 emit_data
->arg_count
= 2;
3520 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3521 struct lp_build_tgsi_context
*bld_base
,
3522 struct lp_build_emit_data
*emit_data
)
3524 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3525 struct si_shader
*shader
= ctx
->shader
;
3526 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3527 const struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3528 LLVMValueRef interp_param
;
3529 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3530 const struct tgsi_full_src_register
*input
= &inst
->Src
[0];
3531 int input_base
, input_array_size
;
3534 LLVMValueRef params
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_PRIM_MASK
);
3535 LLVMValueRef array_idx
;
3536 int interp_param_idx
;
3540 assert(input
->Register
.File
== TGSI_FILE_INPUT
);
3542 if (input
->Register
.Indirect
) {
3543 unsigned array_id
= input
->Indirect
.ArrayID
;
3546 input_base
= info
->input_array_first
[array_id
];
3547 input_array_size
= info
->input_array_last
[array_id
] - input_base
+ 1;
3549 input_base
= inst
->Src
[0].Register
.Index
;
3550 input_array_size
= info
->num_inputs
- input_base
;
3553 array_idx
= get_indirect_index(ctx
, &input
->Indirect
,
3554 input
->Register
.Index
- input_base
);
3556 input_base
= inst
->Src
[0].Register
.Index
;
3557 input_array_size
= 1;
3558 array_idx
= ctx
->i32_0
;
3561 interp
= shader
->selector
->info
.input_interpolate
[input_base
];
3563 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3564 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3565 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3567 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3569 interp_param_idx
= lookup_interp_param_index(interp
, location
);
3570 if (interp_param_idx
== -1)
3572 else if (interp_param_idx
)
3573 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
3575 interp_param
= NULL
;
3577 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3578 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3579 LLVMValueRef ij_out
[2];
3580 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3583 * take the I then J parameters, and the DDX/Y for it, and
3584 * calculate the IJ inputs for the interpolator.
3585 * temp1 = ddx * offset/sample.x + I;
3586 * interp_param.I = ddy * offset/sample.y + temp1;
3587 * temp1 = ddx * offset/sample.x + J;
3588 * interp_param.J = ddy * offset/sample.y + temp1;
3590 for (i
= 0; i
< 2; i
++) {
3591 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, 0);
3592 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, 0);
3593 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3594 ddxy_out
, ix_ll
, "");
3595 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3596 ddxy_out
, iy_ll
, "");
3597 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3598 interp_param
, ix_ll
, "");
3599 LLVMValueRef temp1
, temp2
;
3601 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3604 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3606 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3608 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3610 ij_out
[i
] = LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3612 interp_param
= lp_build_gather_values(gallivm
, ij_out
, 2);
3616 interp_param
= LLVMBuildBitCast(gallivm
->builder
,
3617 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3620 for (chan
= 0; chan
< 4; chan
++) {
3621 LLVMValueRef llvm_chan
;
3622 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->f32
, input_array_size
));
3625 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3626 llvm_chan
= LLVMConstInt(ctx
->i32
, schan
, 0);
3628 for (unsigned i
= 0; i
< input_array_size
; ++i
) {
3629 LLVMValueRef attr_number
= LLVMConstInt(ctx
->i32
, input_base
+ i
, false);
3633 interp_param
= LLVMBuildBitCast(gallivm
->builder
,
3634 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
3635 LLVMValueRef i
= LLVMBuildExtractElement(
3636 gallivm
->builder
, interp_param
, ctx
->i32_0
, "");
3637 LLVMValueRef j
= LLVMBuildExtractElement(
3638 gallivm
->builder
, interp_param
, ctx
->i32_1
, "");
3639 v
= ac_build_fs_interp(&ctx
->ac
,
3640 llvm_chan
, attr_number
, params
,
3643 v
= ac_build_fs_interp_mov(&ctx
->ac
,
3644 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
3645 llvm_chan
, attr_number
, params
);
3648 gather
= LLVMBuildInsertElement(gallivm
->builder
,
3649 gather
, v
, LLVMConstInt(ctx
->i32
, i
, false), "");
3652 emit_data
->output
[chan
] = LLVMBuildExtractElement(
3653 gallivm
->builder
, gather
, array_idx
, "");
3657 static LLVMValueRef
si_emit_ballot(struct si_shader_context
*ctx
,
3660 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3661 LLVMValueRef args
[3] = {
3664 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
3667 /* We currently have no other way to prevent LLVM from lifting the icmp
3668 * calls to a dominating basic block.
3670 emit_optimization_barrier(ctx
, &args
[0]);
3672 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
3673 args
[0] = LLVMBuildBitCast(gallivm
->builder
, args
[0], ctx
->i32
, "");
3675 return lp_build_intrinsic(gallivm
->builder
,
3676 "llvm.amdgcn.icmp.i32",
3678 LP_FUNC_ATTR_NOUNWIND
|
3679 LP_FUNC_ATTR_READNONE
|
3680 LP_FUNC_ATTR_CONVERGENT
);
3683 static void vote_all_emit(
3684 const struct lp_build_tgsi_action
*action
,
3685 struct lp_build_tgsi_context
*bld_base
,
3686 struct lp_build_emit_data
*emit_data
)
3688 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3689 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3690 LLVMValueRef active_set
, vote_set
;
3693 active_set
= si_emit_ballot(ctx
, ctx
->i32_1
);
3694 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
3696 tmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
3697 emit_data
->output
[emit_data
->chan
] =
3698 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
3701 static void vote_any_emit(
3702 const struct lp_build_tgsi_action
*action
,
3703 struct lp_build_tgsi_context
*bld_base
,
3704 struct lp_build_emit_data
*emit_data
)
3706 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3707 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3708 LLVMValueRef vote_set
;
3711 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
3713 tmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
3714 vote_set
, LLVMConstInt(ctx
->i64
, 0, 0), "");
3715 emit_data
->output
[emit_data
->chan
] =
3716 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
3719 static void vote_eq_emit(
3720 const struct lp_build_tgsi_action
*action
,
3721 struct lp_build_tgsi_context
*bld_base
,
3722 struct lp_build_emit_data
*emit_data
)
3724 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3725 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3726 LLVMValueRef active_set
, vote_set
;
3727 LLVMValueRef all
, none
, tmp
;
3729 active_set
= si_emit_ballot(ctx
, ctx
->i32_1
);
3730 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
3732 all
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
3733 none
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
3734 vote_set
, LLVMConstInt(ctx
->i64
, 0, 0), "");
3735 tmp
= LLVMBuildOr(gallivm
->builder
, all
, none
, "");
3736 emit_data
->output
[emit_data
->chan
] =
3737 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
3740 static void ballot_emit(
3741 const struct lp_build_tgsi_action
*action
,
3742 struct lp_build_tgsi_context
*bld_base
,
3743 struct lp_build_emit_data
*emit_data
)
3745 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3746 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3749 tmp
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
3750 tmp
= si_emit_ballot(ctx
, tmp
);
3751 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->v2i32
, "");
3753 emit_data
->output
[0] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_0
, "");
3754 emit_data
->output
[1] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_1
, "");
3757 static void read_invoc_fetch_args(
3758 struct lp_build_tgsi_context
*bld_base
,
3759 struct lp_build_emit_data
*emit_data
)
3761 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
3762 0, emit_data
->src_chan
);
3764 /* Always read the source invocation (= lane) from the X channel. */
3765 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
3767 emit_data
->arg_count
= 2;
3770 static void read_lane_emit(
3771 const struct lp_build_tgsi_action
*action
,
3772 struct lp_build_tgsi_context
*bld_base
,
3773 struct lp_build_emit_data
*emit_data
)
3775 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3776 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3778 /* We currently have no other way to prevent LLVM from lifting the icmp
3779 * calls to a dominating basic block.
3781 emit_optimization_barrier(ctx
, &emit_data
->args
[0]);
3783 for (unsigned i
= 0; i
< emit_data
->arg_count
; ++i
) {
3784 emit_data
->args
[i
] = LLVMBuildBitCast(builder
, emit_data
->args
[i
],
3788 emit_data
->output
[emit_data
->chan
] =
3789 ac_build_intrinsic(&ctx
->ac
, action
->intr_name
,
3790 ctx
->i32
, emit_data
->args
, emit_data
->arg_count
,
3791 AC_FUNC_ATTR_READNONE
|
3792 AC_FUNC_ATTR_CONVERGENT
);
3795 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3796 struct lp_build_emit_data
*emit_data
)
3798 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3799 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3803 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3805 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
3806 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
3810 /* Emit one vertex from the geometry shader */
3811 static void si_llvm_emit_vertex(
3812 const struct lp_build_tgsi_action
*action
,
3813 struct lp_build_tgsi_context
*bld_base
,
3814 struct lp_build_emit_data
*emit_data
)
3816 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3817 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3818 struct si_shader
*shader
= ctx
->shader
;
3819 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3820 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3821 struct lp_build_if_state if_state
;
3822 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
3823 ctx
->param_gs2vs_offset
);
3824 LLVMValueRef gs_next_vertex
;
3825 LLVMValueRef can_emit
, kill
;
3826 unsigned chan
, offset
;
3830 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3832 /* Write vertex attribute values to GSVS ring */
3833 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3834 ctx
->gs_next_vertex
[stream
],
3837 /* If this thread has already emitted the declared maximum number of
3838 * vertices, skip the write: excessive vertex emissions are not
3839 * supposed to have any effect.
3841 * If the shader has no writes to memory, kill it instead. This skips
3842 * further memory loads and may allow LLVM to skip to the end
3845 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULT
, gs_next_vertex
,
3846 LLVMConstInt(ctx
->i32
,
3847 shader
->selector
->gs_max_out_vertices
, 0), "");
3849 bool use_kill
= !info
->writes_memory
;
3851 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3852 LLVMConstReal(ctx
->f32
, 1.0f
),
3853 LLVMConstReal(ctx
->f32
, -1.0f
));
3855 ac_build_kill(&ctx
->ac
, kill
);
3857 lp_build_if(&if_state
, gallivm
, can_emit
);
3861 for (i
= 0; i
< info
->num_outputs
; i
++) {
3862 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
3864 for (chan
= 0; chan
< 4; chan
++) {
3865 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
3866 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
3869 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3870 LLVMValueRef voffset
=
3871 LLVMConstInt(ctx
->i32
, offset
*
3872 shader
->selector
->gs_max_out_vertices
, 0);
3875 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3876 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3878 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
3880 ac_build_buffer_store_dword(&ctx
->ac
,
3881 ctx
->gsvs_ring
[stream
],
3883 voffset
, soffset
, 0,
3888 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3891 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
3893 /* Signal vertex emission */
3894 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
3895 si_get_gs_wave_id(ctx
));
3897 lp_build_endif(&if_state
);
3900 /* Cut one primitive from the geometry shader */
3901 static void si_llvm_emit_primitive(
3902 const struct lp_build_tgsi_action
*action
,
3903 struct lp_build_tgsi_context
*bld_base
,
3904 struct lp_build_emit_data
*emit_data
)
3906 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3909 /* Signal primitive cut */
3910 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3911 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
3912 si_get_gs_wave_id(ctx
));
3915 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
3916 struct lp_build_tgsi_context
*bld_base
,
3917 struct lp_build_emit_data
*emit_data
)
3919 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3920 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3922 /* SI only (thanks to a hw bug workaround):
3923 * The real barrier instruction isn’t needed, because an entire patch
3924 * always fits into a single wave.
3926 if (ctx
->screen
->b
.chip_class
== SI
&&
3927 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
3928 si_emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
3932 lp_build_intrinsic(gallivm
->builder
,
3933 "llvm.amdgcn.s.barrier",
3934 ctx
->voidt
, NULL
, 0, LP_FUNC_ATTR_CONVERGENT
);
3937 static const struct lp_build_tgsi_action interp_action
= {
3938 .fetch_args
= interp_fetch_args
,
3939 .emit
= build_interp_intrinsic
,
3942 static void si_create_function(struct si_shader_context
*ctx
,
3944 LLVMTypeRef
*returns
, unsigned num_returns
,
3945 LLVMTypeRef
*params
, unsigned num_params
,
3946 int last_sgpr
, unsigned max_workgroup_size
)
3950 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
3951 params
, num_params
);
3952 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
3954 for (i
= 0; i
<= last_sgpr
; ++i
) {
3955 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
3957 /* The combination of:
3961 * allows the optimization passes to move loads and reduces
3962 * SGPR spilling significantly.
3964 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
3965 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_BYVAL
);
3966 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
3967 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
3969 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_INREG
);
3972 if (max_workgroup_size
) {
3973 si_llvm_add_attribute(ctx
->main_fn
, "amdgpu-max-work-group-size",
3974 max_workgroup_size
);
3976 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
3977 "no-signed-zeros-fp-math",
3980 if (ctx
->screen
->b
.debug_flags
& DBG_UNSAFE_MATH
) {
3981 /* These were copied from some LLVM test. */
3982 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
3983 "less-precise-fpmad",
3985 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
3988 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
3991 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
3997 static void declare_streamout_params(struct si_shader_context
*ctx
,
3998 struct pipe_stream_output_info
*so
,
3999 LLVMTypeRef
*params
, LLVMTypeRef i32
,
4000 unsigned *num_params
)
4004 /* Streamout SGPRs. */
4005 if (so
->num_outputs
) {
4006 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
4007 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
4009 ctx
->param_streamout_config
= *num_params
- 1;
4011 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
4013 /* A streamout buffer offset is loaded if the stride is non-zero. */
4014 for (i
= 0; i
< 4; i
++) {
4018 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
4022 static unsigned llvm_get_type_size(LLVMTypeRef type
)
4024 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
4027 case LLVMIntegerTypeKind
:
4028 return LLVMGetIntTypeWidth(type
) / 8;
4029 case LLVMFloatTypeKind
:
4031 case LLVMPointerTypeKind
:
4033 case LLVMVectorTypeKind
:
4034 return LLVMGetVectorSize(type
) *
4035 llvm_get_type_size(LLVMGetElementType(type
));
4036 case LLVMArrayTypeKind
:
4037 return LLVMGetArrayLength(type
) *
4038 llvm_get_type_size(LLVMGetElementType(type
));
4045 static void declare_lds_as_pointer(struct si_shader_context
*ctx
)
4047 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4049 unsigned lds_size
= ctx
->screen
->b
.chip_class
>= CIK
? 65536 : 32768;
4050 ctx
->lds
= LLVMBuildIntToPtr(gallivm
->builder
, ctx
->i32_0
,
4051 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
4055 static unsigned si_get_max_workgroup_size(const struct si_shader
*shader
)
4057 switch (shader
->selector
->type
) {
4058 case PIPE_SHADER_TESS_CTRL
:
4059 /* Return this so that LLVM doesn't remove s_barrier
4060 * instructions on chips where we use s_barrier. */
4061 return shader
->selector
->screen
->b
.chip_class
>= CIK
? 128 : 64;
4063 case PIPE_SHADER_GEOMETRY
:
4064 return shader
->selector
->screen
->b
.chip_class
>= GFX9
? 128 : 64;
4066 case PIPE_SHADER_COMPUTE
:
4067 break; /* see below */
4073 const unsigned *properties
= shader
->selector
->info
.properties
;
4074 unsigned max_work_group_size
=
4075 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
4076 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
4077 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
4079 if (!max_work_group_size
) {
4080 /* This is a variable group size compute shader,
4081 * compile it for the maximum possible group size.
4083 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
4085 return max_work_group_size
;
4088 static void declare_per_stage_desc_pointers(struct si_shader_context
*ctx
,
4089 LLVMTypeRef
*params
,
4090 unsigned *num_params
,
4093 params
[(*num_params
)++] = si_const_array(ctx
->v4i32
,
4094 SI_NUM_SHADER_BUFFERS
+ SI_NUM_CONST_BUFFERS
);
4095 params
[(*num_params
)++] = si_const_array(ctx
->v8i32
,
4096 SI_NUM_IMAGES
+ SI_NUM_SAMPLERS
* 2);
4098 if (assign_params
) {
4099 ctx
->param_const_and_shader_buffers
= *num_params
- 2;
4100 ctx
->param_samplers_and_images
= *num_params
- 1;
4104 static void declare_default_desc_pointers(struct si_shader_context
*ctx
,
4105 LLVMTypeRef
*params
,
4106 unsigned *num_params
)
4108 params
[ctx
->param_rw_buffers
= (*num_params
)++] =
4109 si_const_array(ctx
->v4i32
, SI_NUM_RW_BUFFERS
);
4110 declare_per_stage_desc_pointers(ctx
, params
, num_params
, true);
4113 static void declare_vs_specific_input_sgprs(struct si_shader_context
*ctx
,
4114 LLVMTypeRef
*params
,
4115 unsigned *num_params
)
4117 params
[ctx
->param_vertex_buffers
= (*num_params
)++] =
4118 si_const_array(ctx
->v4i32
, SI_NUM_VERTEX_BUFFERS
);
4119 params
[ctx
->param_base_vertex
= (*num_params
)++] = ctx
->i32
;
4120 params
[ctx
->param_start_instance
= (*num_params
)++] = ctx
->i32
;
4121 params
[ctx
->param_draw_id
= (*num_params
)++] = ctx
->i32
;
4122 params
[ctx
->param_vs_state_bits
= (*num_params
)++] = ctx
->i32
;
4125 static void declare_vs_input_vgprs(struct si_shader_context
*ctx
,
4126 LLVMTypeRef
*params
, unsigned *num_params
,
4127 unsigned *num_prolog_vgprs
)
4129 struct si_shader
*shader
= ctx
->shader
;
4131 params
[ctx
->param_vertex_id
= (*num_params
)++] = ctx
->i32
;
4132 if (shader
->key
.as_ls
) {
4133 params
[ctx
->param_rel_auto_id
= (*num_params
)++] = ctx
->i32
;
4134 params
[ctx
->param_instance_id
= (*num_params
)++] = ctx
->i32
;
4136 params
[ctx
->param_instance_id
= (*num_params
)++] = ctx
->i32
;
4137 params
[ctx
->param_vs_prim_id
= (*num_params
)++] = ctx
->i32
;
4139 params
[(*num_params
)++] = ctx
->i32
; /* unused */
4141 if (!shader
->is_gs_copy_shader
) {
4142 /* Vertex load indices. */
4143 ctx
->param_vertex_index0
= (*num_params
);
4144 for (unsigned i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4145 params
[(*num_params
)++] = ctx
->i32
;
4146 *num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
4150 static void declare_tes_input_vgprs(struct si_shader_context
*ctx
,
4151 LLVMTypeRef
*params
, unsigned *num_params
)
4153 params
[ctx
->param_tes_u
= (*num_params
)++] = ctx
->f32
;
4154 params
[ctx
->param_tes_v
= (*num_params
)++] = ctx
->f32
;
4155 params
[ctx
->param_tes_rel_patch_id
= (*num_params
)++] = ctx
->i32
;
4156 params
[ctx
->param_tes_patch_id
= (*num_params
)++] = ctx
->i32
;
4160 /* Convenient merged shader definitions. */
4161 SI_SHADER_MERGED_VERTEX_TESSCTRL
= PIPE_SHADER_TYPES
,
4162 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
,
4165 static void create_function(struct si_shader_context
*ctx
)
4167 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
4168 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4169 struct si_shader
*shader
= ctx
->shader
;
4170 LLVMTypeRef params
[100]; /* just make it large enough */
4171 LLVMTypeRef returns
[16+32*4];
4172 unsigned i
, last_sgpr
, num_params
= 0, num_return_sgprs
;
4173 unsigned num_returns
= 0;
4174 unsigned num_prolog_vgprs
= 0;
4175 unsigned type
= ctx
->type
;
4177 /* Set MERGED shaders. */
4178 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
4179 if (shader
->key
.as_ls
|| type
== PIPE_SHADER_TESS_CTRL
)
4180 type
= SI_SHADER_MERGED_VERTEX_TESSCTRL
; /* LS or HS */
4181 else if (shader
->key
.as_es
|| type
== PIPE_SHADER_GEOMETRY
)
4182 type
= SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
;
4185 LLVMTypeRef v3i32
= LLVMVectorType(ctx
->i32
, 3);
4188 case PIPE_SHADER_VERTEX
:
4189 declare_default_desc_pointers(ctx
, params
, &num_params
);
4190 declare_vs_specific_input_sgprs(ctx
, params
, &num_params
);
4192 if (shader
->key
.as_es
) {
4193 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4194 } else if (shader
->key
.as_ls
) {
4195 /* no extra parameters */
4197 if (shader
->is_gs_copy_shader
)
4198 num_params
= ctx
->param_rw_buffers
+ 1;
4200 /* The locations of the other parameters are assigned dynamically. */
4201 declare_streamout_params(ctx
, &shader
->selector
->so
,
4202 params
, ctx
->i32
, &num_params
);
4205 last_sgpr
= num_params
-1;
4208 declare_vs_input_vgprs(ctx
, params
, &num_params
,
4212 case PIPE_SHADER_TESS_CTRL
: /* SI-CI-VI */
4213 declare_default_desc_pointers(ctx
, params
, &num_params
);
4214 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
4215 params
[ctx
->param_tcs_out_lds_offsets
= num_params
++] = ctx
->i32
;
4216 params
[ctx
->param_tcs_out_lds_layout
= num_params
++] = ctx
->i32
;
4217 params
[ctx
->param_vs_state_bits
= num_params
++] = ctx
->i32
;
4218 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
4219 params
[ctx
->param_tcs_factor_addr_base64k
= num_params
++] = ctx
->i32
;
4220 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
4221 params
[ctx
->param_tcs_factor_offset
= num_params
++] = ctx
->i32
;
4222 last_sgpr
= num_params
- 1;
4225 params
[ctx
->param_tcs_patch_id
= num_params
++] = ctx
->i32
;
4226 params
[ctx
->param_tcs_rel_ids
= num_params
++] = ctx
->i32
;
4228 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4229 * placed after the user SGPRs.
4231 for (i
= 0; i
< GFX6_TCS_NUM_USER_SGPR
+ 2; i
++)
4232 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4233 for (i
= 0; i
< 3; i
++)
4234 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4237 case SI_SHADER_MERGED_VERTEX_TESSCTRL
:
4238 /* Merged stages have 8 system SGPRs at the beginning. */
4239 params
[ctx
->param_rw_buffers
= num_params
++] = /* SPI_SHADER_USER_DATA_ADDR_LO_HS */
4240 si_const_array(ctx
->v4i32
, SI_NUM_RW_BUFFERS
);
4241 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
4242 params
[ctx
->param_merged_wave_info
= num_params
++] = ctx
->i32
;
4243 params
[ctx
->param_tcs_factor_offset
= num_params
++] = ctx
->i32
;
4244 params
[ctx
->param_merged_scratch_offset
= num_params
++] = ctx
->i32
;
4245 params
[num_params
++] = ctx
->i32
; /* unused */
4246 params
[num_params
++] = ctx
->i32
; /* unused */
4248 params
[num_params
++] = ctx
->i32
; /* unused */
4249 params
[num_params
++] = ctx
->i32
; /* unused */
4250 declare_per_stage_desc_pointers(ctx
, params
, &num_params
,
4251 ctx
->type
== PIPE_SHADER_VERTEX
);
4252 declare_vs_specific_input_sgprs(ctx
, params
, &num_params
);
4254 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
4255 params
[ctx
->param_tcs_out_lds_offsets
= num_params
++] = ctx
->i32
;
4256 params
[ctx
->param_tcs_out_lds_layout
= num_params
++] = ctx
->i32
;
4257 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
4258 params
[ctx
->param_tcs_factor_addr_base64k
= num_params
++] = ctx
->i32
;
4259 params
[num_params
++] = ctx
->i32
; /* unused */
4261 declare_per_stage_desc_pointers(ctx
, params
, &num_params
,
4262 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4263 last_sgpr
= num_params
- 1;
4265 /* VGPRs (first TCS, then VS) */
4266 params
[ctx
->param_tcs_patch_id
= num_params
++] = ctx
->i32
;
4267 params
[ctx
->param_tcs_rel_ids
= num_params
++] = ctx
->i32
;
4269 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4270 declare_vs_input_vgprs(ctx
, params
, &num_params
,
4273 /* LS return values are inputs to the TCS main shader part. */
4274 for (i
= 0; i
< 8 + GFX9_TCS_NUM_USER_SGPR
; i
++)
4275 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4276 for (i
= 0; i
< 2; i
++)
4277 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4279 /* TCS return values are inputs to the TCS epilog.
4281 * param_tcs_offchip_offset, param_tcs_factor_offset,
4282 * param_tcs_offchip_layout, and param_rw_buffers
4283 * should be passed to the epilog.
4285 for (i
= 0; i
<= 8 + GFX9_SGPR_TCS_FACTOR_ADDR_BASE64K
; i
++)
4286 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4287 for (i
= 0; i
< 3; i
++)
4288 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4292 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
:
4293 /* Merged stages have 8 system SGPRs at the beginning. */
4294 params
[ctx
->param_rw_buffers
= num_params
++] = /* SPI_SHADER_USER_DATA_ADDR_LO_GS */
4295 si_const_array(ctx
->v4i32
, SI_NUM_RW_BUFFERS
);
4296 params
[ctx
->param_gs2vs_offset
= num_params
++] = ctx
->i32
;
4297 params
[ctx
->param_merged_wave_info
= num_params
++] = ctx
->i32
;
4298 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
4299 params
[ctx
->param_merged_scratch_offset
= num_params
++] = ctx
->i32
;
4300 params
[num_params
++] = ctx
->i32
; /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4301 params
[num_params
++] = ctx
->i32
; /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4303 params
[num_params
++] = ctx
->i32
; /* unused */
4304 params
[num_params
++] = ctx
->i32
; /* unused */
4305 declare_per_stage_desc_pointers(ctx
, params
, &num_params
,
4306 (ctx
->type
== PIPE_SHADER_VERTEX
||
4307 ctx
->type
== PIPE_SHADER_TESS_EVAL
));
4308 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4309 declare_vs_specific_input_sgprs(ctx
, params
, &num_params
);
4311 /* TESS_EVAL (and also GEOMETRY):
4312 * Declare as many input SGPRs as the VS has. */
4313 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
4314 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
4315 params
[num_params
++] = ctx
->i32
; /* unused */
4316 params
[num_params
++] = ctx
->i32
; /* unused */
4317 params
[num_params
++] = ctx
->i32
; /* unused */
4318 params
[ctx
->param_vs_state_bits
= num_params
++] = ctx
->i32
; /* unused */
4321 declare_per_stage_desc_pointers(ctx
, params
, &num_params
,
4322 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4323 last_sgpr
= num_params
- 1;
4325 /* VGPRs (first GS, then VS/TES) */
4326 params
[ctx
->param_gs_vtx01_offset
= num_params
++] = ctx
->i32
;
4327 params
[ctx
->param_gs_vtx23_offset
= num_params
++] = ctx
->i32
;
4328 params
[ctx
->param_gs_prim_id
= num_params
++] = ctx
->i32
;
4329 params
[ctx
->param_gs_instance_id
= num_params
++] = ctx
->i32
;
4330 params
[ctx
->param_gs_vtx45_offset
= num_params
++] = ctx
->i32
;
4332 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4333 declare_vs_input_vgprs(ctx
, params
, &num_params
,
4335 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4336 declare_tes_input_vgprs(ctx
, params
, &num_params
);
4339 if (ctx
->type
== PIPE_SHADER_VERTEX
||
4340 ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4341 /* ES return values are inputs to GS. */
4342 for (i
= 0; i
< 8 + GFX9_GS_NUM_USER_SGPR
; i
++)
4343 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4344 for (i
= 0; i
< 5; i
++)
4345 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4349 case PIPE_SHADER_TESS_EVAL
:
4350 declare_default_desc_pointers(ctx
, params
, &num_params
);
4351 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
4352 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
4354 if (shader
->key
.as_es
) {
4355 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
4356 params
[num_params
++] = ctx
->i32
;
4357 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4359 params
[num_params
++] = ctx
->i32
;
4360 declare_streamout_params(ctx
, &shader
->selector
->so
,
4361 params
, ctx
->i32
, &num_params
);
4362 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
4364 last_sgpr
= num_params
- 1;
4367 declare_tes_input_vgprs(ctx
, params
, &num_params
);
4370 case PIPE_SHADER_GEOMETRY
:
4371 declare_default_desc_pointers(ctx
, params
, &num_params
);
4372 params
[ctx
->param_gs2vs_offset
= num_params
++] = ctx
->i32
;
4373 params
[ctx
->param_gs_wave_id
= num_params
++] = ctx
->i32
;
4374 last_sgpr
= num_params
- 1;
4377 params
[ctx
->param_gs_vtx0_offset
= num_params
++] = ctx
->i32
;
4378 params
[ctx
->param_gs_vtx1_offset
= num_params
++] = ctx
->i32
;
4379 params
[ctx
->param_gs_prim_id
= num_params
++] = ctx
->i32
;
4380 params
[ctx
->param_gs_vtx2_offset
= num_params
++] = ctx
->i32
;
4381 params
[ctx
->param_gs_vtx3_offset
= num_params
++] = ctx
->i32
;
4382 params
[ctx
->param_gs_vtx4_offset
= num_params
++] = ctx
->i32
;
4383 params
[ctx
->param_gs_vtx5_offset
= num_params
++] = ctx
->i32
;
4384 params
[ctx
->param_gs_instance_id
= num_params
++] = ctx
->i32
;
4387 case PIPE_SHADER_FRAGMENT
:
4388 declare_default_desc_pointers(ctx
, params
, &num_params
);
4389 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
4390 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
4391 last_sgpr
= SI_PARAM_PRIM_MASK
;
4392 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
4393 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
4394 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
4395 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
4396 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
4397 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
4398 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
4399 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
4400 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
4401 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
4402 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
4403 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
4404 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
4405 shader
->info
.face_vgpr_index
= 20;
4406 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
4407 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
4408 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
4409 num_params
= SI_PARAM_POS_FIXED_PT
+1;
4411 /* Color inputs from the prolog. */
4412 if (shader
->selector
->info
.colors_read
) {
4413 unsigned num_color_elements
=
4414 util_bitcount(shader
->selector
->info
.colors_read
);
4416 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
4417 for (i
= 0; i
< num_color_elements
; i
++)
4418 params
[num_params
++] = ctx
->f32
;
4420 num_prolog_vgprs
+= num_color_elements
;
4423 /* Outputs for the epilog. */
4424 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4427 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4428 shader
->selector
->info
.writes_z
+
4429 shader
->selector
->info
.writes_stencil
+
4430 shader
->selector
->info
.writes_samplemask
+
4431 1 /* SampleMaskIn */;
4433 num_returns
= MAX2(num_returns
,
4435 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4437 for (i
= 0; i
< num_return_sgprs
; i
++)
4438 returns
[i
] = ctx
->i32
;
4439 for (; i
< num_returns
; i
++)
4440 returns
[i
] = ctx
->f32
;
4443 case PIPE_SHADER_COMPUTE
:
4444 declare_default_desc_pointers(ctx
, params
, &num_params
);
4445 if (shader
->selector
->info
.uses_grid_size
)
4446 params
[ctx
->param_grid_size
= num_params
++] = v3i32
;
4447 if (shader
->selector
->info
.uses_block_size
)
4448 params
[ctx
->param_block_size
= num_params
++] = v3i32
;
4450 for (i
= 0; i
< 3; i
++) {
4451 ctx
->param_block_id
[i
] = -1;
4452 if (shader
->selector
->info
.uses_block_id
[i
])
4453 params
[ctx
->param_block_id
[i
] = num_params
++] = ctx
->i32
;
4455 last_sgpr
= num_params
- 1;
4457 params
[ctx
->param_thread_id
= num_params
++] = v3i32
;
4460 assert(0 && "unimplemented shader");
4464 assert(num_params
<= ARRAY_SIZE(params
));
4466 si_create_function(ctx
, "main", returns
, num_returns
, params
,
4467 num_params
, last_sgpr
,
4468 si_get_max_workgroup_size(shader
));
4470 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4471 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&&
4472 ctx
->separate_prolog
) {
4473 si_llvm_add_attribute(ctx
->main_fn
,
4474 "InitialPSInputAddr",
4475 S_0286D0_PERSP_SAMPLE_ENA(1) |
4476 S_0286D0_PERSP_CENTER_ENA(1) |
4477 S_0286D0_PERSP_CENTROID_ENA(1) |
4478 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4479 S_0286D0_LINEAR_CENTER_ENA(1) |
4480 S_0286D0_LINEAR_CENTROID_ENA(1) |
4481 S_0286D0_FRONT_FACE_ENA(1) |
4482 S_0286D0_POS_FIXED_PT_ENA(1));
4485 shader
->info
.num_input_sgprs
= 0;
4486 shader
->info
.num_input_vgprs
= 0;
4488 for (i
= 0; i
<= last_sgpr
; ++i
)
4489 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
4491 for (; i
< num_params
; ++i
)
4492 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
4494 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
4495 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
4497 if (!ctx
->screen
->has_ds_bpermute
&&
4499 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
4500 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
4501 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
4502 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
4503 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
4504 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
4506 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4507 LLVMArrayType(ctx
->i32
, 64),
4511 if (shader
->key
.as_ls
||
4512 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
4513 /* GFX9 has the ESGS ring buffer in LDS. */
4514 (ctx
->screen
->b
.chip_class
>= GFX9
&&
4515 (shader
->key
.as_es
||
4516 ctx
->type
== PIPE_SHADER_GEOMETRY
)))
4517 declare_lds_as_pointer(ctx
);
4521 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
4524 static void preload_ring_buffers(struct si_shader_context
*ctx
)
4526 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4527 LLVMBuilderRef builder
= gallivm
->builder
;
4529 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
4530 ctx
->param_rw_buffers
);
4532 if (ctx
->screen
->b
.chip_class
<= VI
&&
4533 (ctx
->shader
->key
.as_es
|| ctx
->type
== PIPE_SHADER_GEOMETRY
)) {
4535 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
4537 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, ring
, 0);
4540 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
4543 if (ctx
->shader
->is_gs_copy_shader
) {
4544 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
4547 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
4548 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
4549 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
4550 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
4551 LLVMValueRef base_ring
;
4553 base_ring
= ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
4555 /* The conceptual layout of the GSVS ring is
4556 * v0c0 .. vLv0 v0c1 .. vLc1 ..
4557 * but the real memory layout is swizzled across
4559 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
4561 * Override the buffer descriptor accordingly.
4563 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
4564 uint64_t stream_offset
= 0;
4566 for (unsigned stream
= 0; stream
< 4; ++stream
) {
4567 unsigned num_components
;
4569 unsigned num_records
;
4570 LLVMValueRef ring
, tmp
;
4572 num_components
= sel
->info
.num_stream_output_components
[stream
];
4573 if (!num_components
)
4576 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
4578 /* Limit on the stride field for <= CIK. */
4579 assert(stride
< (1 << 14));
4583 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
4584 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_0
, "");
4585 tmp
= LLVMBuildAdd(builder
, tmp
,
4586 LLVMConstInt(ctx
->i64
,
4587 stream_offset
, 0), "");
4588 stream_offset
+= stride
* 64;
4590 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_0
, "");
4591 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
4592 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_1
, "");
4593 tmp
= LLVMBuildOr(builder
, tmp
,
4594 LLVMConstInt(ctx
->i32
,
4595 S_008F04_STRIDE(stride
) |
4596 S_008F04_SWIZZLE_ENABLE(1), 0), "");
4597 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_1
, "");
4598 ring
= LLVMBuildInsertElement(builder
, ring
,
4599 LLVMConstInt(ctx
->i32
, num_records
, 0),
4600 LLVMConstInt(ctx
->i32
, 2, 0), "");
4601 ring
= LLVMBuildInsertElement(builder
, ring
,
4602 LLVMConstInt(ctx
->i32
,
4603 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
4604 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
4605 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
4606 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
4607 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
4608 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
4609 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
4610 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
4611 S_008F0C_ADD_TID_ENABLE(1),
4613 LLVMConstInt(ctx
->i32
, 3, 0), "");
4615 ctx
->gsvs_ring
[stream
] = ring
;
4620 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
4621 LLVMValueRef param_rw_buffers
,
4622 unsigned param_pos_fixed_pt
)
4624 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4625 LLVMBuilderRef builder
= gallivm
->builder
;
4626 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
4628 /* Use the fixed-point gl_FragCoord input.
4629 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
4630 * per coordinate to get the repeating effect.
4632 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
4633 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
4635 /* Load the buffer descriptor. */
4636 slot
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
4637 desc
= ac_build_indexed_load_const(&ctx
->ac
, param_rw_buffers
, slot
);
4639 /* The stipple pattern is 32x32, each row has 32 bits. */
4640 offset
= LLVMBuildMul(builder
, address
[1],
4641 LLVMConstInt(ctx
->i32
, 4, 0), "");
4642 row
= buffer_load_const(ctx
, desc
, offset
);
4643 row
= LLVMBuildBitCast(builder
, row
, ctx
->i32
, "");
4644 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
4645 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
4647 /* The intrinsic kills the thread if arg < 0. */
4648 bit
= LLVMBuildSelect(builder
, bit
, LLVMConstReal(ctx
->f32
, 0),
4649 LLVMConstReal(ctx
->f32
, -1), "");
4650 ac_build_kill(&ctx
->ac
, bit
);
4653 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
4654 struct si_shader_config
*conf
,
4655 unsigned symbol_offset
)
4658 const unsigned char *config
=
4659 ac_shader_binary_config_start(binary
, symbol_offset
);
4660 bool really_needs_scratch
= false;
4662 /* LLVM adds SGPR spills to the scratch size.
4663 * Find out if we really need the scratch buffer.
4665 for (i
= 0; i
< binary
->reloc_count
; i
++) {
4666 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
4668 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
4669 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
4670 really_needs_scratch
= true;
4675 /* XXX: We may be able to emit some of these values directly rather than
4676 * extracting fields to be emitted later.
4679 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
4680 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
4681 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
4683 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
4684 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
4685 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
4686 case R_00B428_SPI_SHADER_PGM_RSRC1_HS
:
4687 case R_00B848_COMPUTE_PGM_RSRC1
:
4688 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
4689 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
4690 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
4691 conf
->rsrc1
= value
;
4693 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
4694 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
4696 case R_00B84C_COMPUTE_PGM_RSRC2
:
4697 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
4698 conf
->rsrc2
= value
;
4700 case R_0286CC_SPI_PS_INPUT_ENA
:
4701 conf
->spi_ps_input_ena
= value
;
4703 case R_0286D0_SPI_PS_INPUT_ADDR
:
4704 conf
->spi_ps_input_addr
= value
;
4706 case R_0286E8_SPI_TMPRING_SIZE
:
4707 case R_00B860_COMPUTE_TMPRING_SIZE
:
4708 /* WAVESIZE is in units of 256 dwords. */
4709 if (really_needs_scratch
)
4710 conf
->scratch_bytes_per_wave
=
4711 G_00B860_WAVESIZE(value
) * 256 * 4;
4713 case 0x4: /* SPILLED_SGPRS */
4714 conf
->spilled_sgprs
= value
;
4716 case 0x8: /* SPILLED_VGPRS */
4717 conf
->spilled_vgprs
= value
;
4721 static bool printed
;
4724 fprintf(stderr
, "Warning: LLVM emitted unknown "
4725 "config register: 0x%x\n", reg
);
4733 if (!conf
->spi_ps_input_addr
)
4734 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
4737 void si_shader_apply_scratch_relocs(struct si_shader
*shader
,
4738 uint64_t scratch_va
)
4741 uint32_t scratch_rsrc_dword0
= scratch_va
;
4742 uint32_t scratch_rsrc_dword1
=
4743 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
4745 /* Enable scratch coalescing. */
4746 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
4748 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
4749 const struct ac_shader_reloc
*reloc
=
4750 &shader
->binary
.relocs
[i
];
4751 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
4752 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4753 &scratch_rsrc_dword0
, 4);
4754 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
4755 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4756 &scratch_rsrc_dword1
, 4);
4761 static unsigned si_get_shader_binary_size(const struct si_shader
*shader
)
4763 unsigned size
= shader
->binary
.code_size
;
4766 size
+= shader
->prolog
->binary
.code_size
;
4767 if (shader
->previous_stage
)
4768 size
+= shader
->previous_stage
->binary
.code_size
;
4769 if (shader
->prolog2
)
4770 size
+= shader
->prolog2
->binary
.code_size
;
4772 size
+= shader
->epilog
->binary
.code_size
;
4776 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
4778 const struct ac_shader_binary
*prolog
=
4779 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
4780 const struct ac_shader_binary
*previous_stage
=
4781 shader
->previous_stage
? &shader
->previous_stage
->binary
: NULL
;
4782 const struct ac_shader_binary
*prolog2
=
4783 shader
->prolog2
? &shader
->prolog2
->binary
: NULL
;
4784 const struct ac_shader_binary
*epilog
=
4785 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
4786 const struct ac_shader_binary
*mainb
= &shader
->binary
;
4787 unsigned bo_size
= si_get_shader_binary_size(shader
) +
4788 (!epilog
? mainb
->rodata_size
: 0);
4791 assert(!prolog
|| !prolog
->rodata_size
);
4792 assert(!previous_stage
|| !previous_stage
->rodata_size
);
4793 assert(!prolog2
|| !prolog2
->rodata_size
);
4794 assert((!prolog
&& !previous_stage
&& !prolog2
&& !epilog
) ||
4795 !mainb
->rodata_size
);
4796 assert(!epilog
|| !epilog
->rodata_size
);
4798 r600_resource_reference(&shader
->bo
, NULL
);
4799 shader
->bo
= (struct r600_resource
*)
4800 pipe_buffer_create(&sscreen
->b
.b
, 0,
4801 PIPE_USAGE_IMMUTABLE
,
4802 align(bo_size
, SI_CPDMA_ALIGNMENT
));
4807 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
4808 PIPE_TRANSFER_READ_WRITE
|
4809 PIPE_TRANSFER_UNSYNCHRONIZED
);
4811 /* Don't use util_memcpy_cpu_to_le32. LLVM binaries are
4812 * endian-independent. */
4814 memcpy(ptr
, prolog
->code
, prolog
->code_size
);
4815 ptr
+= prolog
->code_size
;
4817 if (previous_stage
) {
4818 memcpy(ptr
, previous_stage
->code
, previous_stage
->code_size
);
4819 ptr
+= previous_stage
->code_size
;
4822 memcpy(ptr
, prolog2
->code
, prolog2
->code_size
);
4823 ptr
+= prolog2
->code_size
;
4826 memcpy(ptr
, mainb
->code
, mainb
->code_size
);
4827 ptr
+= mainb
->code_size
;
4830 memcpy(ptr
, epilog
->code
, epilog
->code_size
);
4831 else if (mainb
->rodata_size
> 0)
4832 memcpy(ptr
, mainb
->rodata
, mainb
->rodata_size
);
4834 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
4838 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
4839 struct pipe_debug_callback
*debug
,
4840 const char *name
, FILE *file
)
4845 if (binary
->disasm_string
) {
4846 fprintf(file
, "Shader %s disassembly:\n", name
);
4847 fprintf(file
, "%s", binary
->disasm_string
);
4849 if (debug
&& debug
->debug_message
) {
4850 /* Very long debug messages are cut off, so send the
4851 * disassembly one line at a time. This causes more
4852 * overhead, but on the plus side it simplifies
4853 * parsing of resulting logs.
4855 pipe_debug_message(debug
, SHADER_INFO
,
4856 "Shader Disassembly Begin");
4858 line
= binary
->disasm_string
;
4860 p
= util_strchrnul(line
, '\n');
4864 pipe_debug_message(debug
, SHADER_INFO
,
4865 "%.*s", count
, line
);
4873 pipe_debug_message(debug
, SHADER_INFO
,
4874 "Shader Disassembly End");
4877 fprintf(file
, "Shader %s binary:\n", name
);
4878 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
4879 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
4880 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
4881 binary
->code
[i
+ 1], binary
->code
[i
]);
4886 static void si_shader_dump_stats(struct si_screen
*sscreen
,
4887 const struct si_shader
*shader
,
4888 struct pipe_debug_callback
*debug
,
4891 bool check_debug_option
)
4893 const struct si_shader_config
*conf
= &shader
->config
;
4894 unsigned num_inputs
= shader
->selector
? shader
->selector
->info
.num_inputs
: 0;
4895 unsigned code_size
= si_get_shader_binary_size(shader
);
4896 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
4897 unsigned lds_per_wave
= 0;
4898 unsigned max_simd_waves
= 10;
4900 /* Compute LDS usage for PS. */
4901 switch (processor
) {
4902 case PIPE_SHADER_FRAGMENT
:
4903 /* The minimum usage per wave is (num_inputs * 48). The maximum
4904 * usage is (num_inputs * 48 * 16).
4905 * We can get anything in between and it varies between waves.
4907 * The 48 bytes per input for a single primitive is equal to
4908 * 4 bytes/component * 4 components/input * 3 points.
4910 * Other stages don't know the size at compile time or don't
4911 * allocate LDS per wave, but instead they do it per thread group.
4913 lds_per_wave
= conf
->lds_size
* lds_increment
+
4914 align(num_inputs
* 48, lds_increment
);
4916 case PIPE_SHADER_COMPUTE
:
4917 if (shader
->selector
) {
4918 unsigned max_workgroup_size
=
4919 si_get_max_workgroup_size(shader
);
4920 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
4921 DIV_ROUND_UP(max_workgroup_size
, 64);
4926 /* Compute the per-SIMD wave counts. */
4927 if (conf
->num_sgprs
) {
4928 if (sscreen
->b
.chip_class
>= VI
)
4929 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
4931 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
4934 if (conf
->num_vgprs
)
4935 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
4937 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
4938 * 16KB makes some SIMDs unoccupied). */
4940 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
4942 if (!check_debug_option
||
4943 r600_can_dump_shader(&sscreen
->b
, processor
)) {
4944 if (processor
== PIPE_SHADER_FRAGMENT
) {
4945 fprintf(file
, "*** SHADER CONFIG ***\n"
4946 "SPI_PS_INPUT_ADDR = 0x%04x\n"
4947 "SPI_PS_INPUT_ENA = 0x%04x\n",
4948 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
4951 fprintf(file
, "*** SHADER STATS ***\n"
4954 "Spilled SGPRs: %d\n"
4955 "Spilled VGPRs: %d\n"
4956 "Private memory VGPRs: %d\n"
4957 "Code Size: %d bytes\n"
4959 "Scratch: %d bytes per wave\n"
4961 "********************\n\n\n",
4962 conf
->num_sgprs
, conf
->num_vgprs
,
4963 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
4964 conf
->private_mem_vgprs
, code_size
,
4965 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4969 pipe_debug_message(debug
, SHADER_INFO
,
4970 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
4971 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
4972 "Spilled VGPRs: %d PrivMem VGPRs: %d",
4973 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4974 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4975 max_simd_waves
, conf
->spilled_sgprs
,
4976 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
4979 const char *si_get_shader_name(const struct si_shader
*shader
, unsigned processor
)
4981 switch (processor
) {
4982 case PIPE_SHADER_VERTEX
:
4983 if (shader
->key
.as_es
)
4984 return "Vertex Shader as ES";
4985 else if (shader
->key
.as_ls
)
4986 return "Vertex Shader as LS";
4988 return "Vertex Shader as VS";
4989 case PIPE_SHADER_TESS_CTRL
:
4990 return "Tessellation Control Shader";
4991 case PIPE_SHADER_TESS_EVAL
:
4992 if (shader
->key
.as_es
)
4993 return "Tessellation Evaluation Shader as ES";
4995 return "Tessellation Evaluation Shader as VS";
4996 case PIPE_SHADER_GEOMETRY
:
4997 if (shader
->is_gs_copy_shader
)
4998 return "GS Copy Shader as VS";
5000 return "Geometry Shader";
5001 case PIPE_SHADER_FRAGMENT
:
5002 return "Pixel Shader";
5003 case PIPE_SHADER_COMPUTE
:
5004 return "Compute Shader";
5006 return "Unknown Shader";
5010 void si_shader_dump(struct si_screen
*sscreen
, const struct si_shader
*shader
,
5011 struct pipe_debug_callback
*debug
, unsigned processor
,
5012 FILE *file
, bool check_debug_option
)
5014 if (!check_debug_option
||
5015 r600_can_dump_shader(&sscreen
->b
, processor
))
5016 si_dump_shader_key(processor
, shader
, file
);
5018 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
5019 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
5020 si_get_shader_name(shader
, processor
));
5021 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
5024 if (!check_debug_option
||
5025 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
5026 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
5027 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5030 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5031 debug
, "prolog", file
);
5032 if (shader
->previous_stage
)
5033 si_shader_dump_disassembly(&shader
->previous_stage
->binary
,
5034 debug
, "previous stage", file
);
5035 if (shader
->prolog2
)
5036 si_shader_dump_disassembly(&shader
->prolog2
->binary
,
5037 debug
, "prolog2", file
);
5039 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5042 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5043 debug
, "epilog", file
);
5044 fprintf(file
, "\n");
5047 si_shader_dump_stats(sscreen
, shader
, debug
, processor
, file
,
5048 check_debug_option
);
5051 static int si_compile_llvm(struct si_screen
*sscreen
,
5052 struct ac_shader_binary
*binary
,
5053 struct si_shader_config
*conf
,
5054 LLVMTargetMachineRef tm
,
5056 struct pipe_debug_callback
*debug
,
5061 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
5063 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
5064 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5066 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
5067 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5068 ac_dump_module(mod
);
5069 fprintf(stderr
, "\n");
5073 if (sscreen
->record_llvm_ir
) {
5074 char *ir
= LLVMPrintModuleToString(mod
);
5075 binary
->llvm_ir_string
= strdup(ir
);
5076 LLVMDisposeMessage(ir
);
5079 if (!si_replace_shader(count
, binary
)) {
5080 r
= si_llvm_compile(mod
, binary
, tm
, debug
);
5085 si_shader_binary_read_config(binary
, conf
, 0);
5087 /* Enable 64-bit and 16-bit denormals, because there is no performance
5090 * If denormals are enabled, all floating-point output modifiers are
5093 * Don't enable denormals for 32-bit floats, because:
5094 * - Floating-point output modifiers would be ignored by the hw.
5095 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5096 * have to stop using those.
5097 * - SI & CI would be very slow.
5099 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5101 FREE(binary
->config
);
5102 FREE(binary
->global_symbol_offsets
);
5103 binary
->config
= NULL
;
5104 binary
->global_symbol_offsets
= NULL
;
5106 /* Some shaders can't have rodata because their binaries can be
5109 if (binary
->rodata_size
&&
5110 (processor
== PIPE_SHADER_VERTEX
||
5111 processor
== PIPE_SHADER_TESS_CTRL
||
5112 processor
== PIPE_SHADER_TESS_EVAL
||
5113 processor
== PIPE_SHADER_FRAGMENT
)) {
5114 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5121 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
5123 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
5124 LLVMBuildRetVoid(ctx
->gallivm
.builder
);
5126 LLVMBuildRet(ctx
->gallivm
.builder
, ret
);
5129 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5131 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5132 LLVMTargetMachineRef tm
,
5133 struct si_shader_selector
*gs_selector
,
5134 struct pipe_debug_callback
*debug
)
5136 struct si_shader_context ctx
;
5137 struct si_shader
*shader
;
5138 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
5139 LLVMBuilderRef builder
;
5140 struct lp_build_tgsi_context
*bld_base
= &ctx
.bld_base
;
5141 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5142 struct si_shader_output_values
*outputs
;
5143 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
5146 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
5151 shader
= CALLOC_STRUCT(si_shader
);
5158 shader
->selector
= gs_selector
;
5159 shader
->is_gs_copy_shader
= true;
5161 si_init_shader_ctx(&ctx
, sscreen
, tm
);
5162 ctx
.shader
= shader
;
5163 ctx
.type
= PIPE_SHADER_VERTEX
;
5165 builder
= gallivm
->builder
;
5167 create_function(&ctx
);
5168 preload_ring_buffers(&ctx
);
5170 LLVMValueRef voffset
=
5171 lp_build_mul_imm(uint
, LLVMGetParam(ctx
.main_fn
,
5172 ctx
.param_vertex_id
), 4);
5174 /* Fetch the vertex stream ID.*/
5175 LLVMValueRef stream_id
;
5177 if (gs_selector
->so
.num_outputs
)
5178 stream_id
= unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
5180 stream_id
= ctx
.i32_0
;
5182 /* Fill in output information. */
5183 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5184 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
5185 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
5187 for (int chan
= 0; chan
< 4; chan
++) {
5188 outputs
[i
].vertex_stream
[chan
] =
5189 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
5193 LLVMBasicBlockRef end_bb
;
5194 LLVMValueRef switch_inst
;
5196 end_bb
= LLVMAppendBasicBlockInContext(gallivm
->context
, ctx
.main_fn
, "end");
5197 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
5199 for (int stream
= 0; stream
< 4; stream
++) {
5200 LLVMBasicBlockRef bb
;
5203 if (!gsinfo
->num_stream_output_components
[stream
])
5206 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
5209 bb
= LLVMInsertBasicBlockInContext(gallivm
->context
, end_bb
, "out");
5210 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
.i32
, stream
, 0), bb
);
5211 LLVMPositionBuilderAtEnd(builder
, bb
);
5213 /* Fetch vertex data from GSVS ring */
5215 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5216 for (unsigned chan
= 0; chan
< 4; chan
++) {
5217 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
5218 outputs
[i
].vertex_stream
[chan
] != stream
) {
5219 outputs
[i
].values
[chan
] = ctx
.bld_base
.base
.undef
;
5223 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
5224 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
5227 outputs
[i
].values
[chan
] =
5228 ac_build_buffer_load(&ctx
.ac
,
5229 ctx
.gsvs_ring
[0], 1,
5236 /* Streamout and exports. */
5237 if (gs_selector
->so
.num_outputs
) {
5238 si_llvm_emit_streamout(&ctx
, outputs
,
5239 gsinfo
->num_outputs
,
5244 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
5246 LLVMBuildBr(builder
, end_bb
);
5249 LLVMPositionBuilderAtEnd(builder
, end_bb
);
5251 LLVMBuildRetVoid(gallivm
->builder
);
5253 ctx
.type
= PIPE_SHADER_GEOMETRY
; /* override for shader dumping */
5254 si_llvm_optimize_module(&ctx
);
5256 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
5257 &ctx
.shader
->config
, ctx
.tm
,
5259 debug
, PIPE_SHADER_GEOMETRY
,
5262 if (r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
5263 fprintf(stderr
, "GS Copy Shader:\n");
5264 si_shader_dump(sscreen
, ctx
.shader
, debug
,
5265 PIPE_SHADER_GEOMETRY
, stderr
, true);
5266 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
5269 si_llvm_dispose(&ctx
);
5280 static void si_dump_shader_key_vs(const struct si_shader_key
*key
,
5281 const struct si_vs_prolog_bits
*prolog
,
5282 const char *prefix
, FILE *f
)
5284 fprintf(f
, " %s.instance_divisor_is_one = %u\n",
5285 prefix
, prolog
->instance_divisor_is_one
);
5286 fprintf(f
, " %s.instance_divisor_is_fetched = %u\n",
5287 prefix
, prolog
->instance_divisor_is_fetched
);
5289 fprintf(f
, " mono.vs.fix_fetch = {");
5290 for (int i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
5291 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs_fix_fetch
[i
]);
5295 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
5298 const struct si_shader_key
*key
= &shader
->key
;
5300 fprintf(f
, "SHADER KEY\n");
5302 switch (processor
) {
5303 case PIPE_SHADER_VERTEX
:
5304 si_dump_shader_key_vs(key
, &key
->part
.vs
.prolog
,
5305 "part.vs.prolog", f
);
5306 fprintf(f
, " as_es = %u\n", key
->as_es
);
5307 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
5308 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5309 key
->mono
.u
.vs_export_prim_id
);
5312 case PIPE_SHADER_TESS_CTRL
:
5313 if (shader
->selector
->screen
->b
.chip_class
>= GFX9
) {
5314 si_dump_shader_key_vs(key
, &key
->part
.tcs
.ls_prolog
,
5315 "part.tcs.ls_prolog", f
);
5317 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
5318 fprintf(f
, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.u
.ff_tcs_inputs_to_copy
);
5321 case PIPE_SHADER_TESS_EVAL
:
5322 fprintf(f
, " as_es = %u\n", key
->as_es
);
5323 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5324 key
->mono
.u
.vs_export_prim_id
);
5327 case PIPE_SHADER_GEOMETRY
:
5328 if (shader
->is_gs_copy_shader
)
5331 if (shader
->selector
->screen
->b
.chip_class
>= GFX9
&&
5332 key
->part
.gs
.es
->type
== PIPE_SHADER_VERTEX
) {
5333 si_dump_shader_key_vs(key
, &key
->part
.gs
.vs_prolog
,
5334 "part.gs.vs_prolog", f
);
5336 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
5339 case PIPE_SHADER_COMPUTE
:
5342 case PIPE_SHADER_FRAGMENT
:
5343 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
5344 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
5345 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
5346 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
5347 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
5348 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
5349 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
5350 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
5351 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
5352 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
5353 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
5354 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
5355 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
5356 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
5357 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
5358 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
5359 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
5366 if ((processor
== PIPE_SHADER_GEOMETRY
||
5367 processor
== PIPE_SHADER_TESS_EVAL
||
5368 processor
== PIPE_SHADER_VERTEX
) &&
5369 !key
->as_es
&& !key
->as_ls
) {
5370 fprintf(f
, " opt.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.kill_outputs
);
5371 fprintf(f
, " opt.clip_disable = %u\n", key
->opt
.clip_disable
);
5375 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5376 struct si_screen
*sscreen
,
5377 LLVMTargetMachineRef tm
)
5379 struct lp_build_tgsi_context
*bld_base
;
5381 si_llvm_context_init(ctx
, sscreen
, tm
);
5383 bld_base
= &ctx
->bld_base
;
5384 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5386 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
5387 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
5388 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
5390 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
5392 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
5394 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5395 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5396 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5397 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5399 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
5400 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
5401 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
5402 bld_base
->op_actions
[TGSI_OPCODE_BALLOT
].emit
= ballot_emit
;
5403 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].intr_name
= "llvm.amdgcn.readfirstlane";
5404 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].emit
= read_lane_emit
;
5405 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].intr_name
= "llvm.amdgcn.readlane";
5406 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].fetch_args
= read_invoc_fetch_args
;
5407 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].emit
= read_lane_emit
;
5409 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
5410 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
5411 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5414 static void si_optimize_vs_outputs(struct si_shader_context
*ctx
)
5416 struct si_shader
*shader
= ctx
->shader
;
5417 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5419 if ((ctx
->type
!= PIPE_SHADER_VERTEX
&&
5420 ctx
->type
!= PIPE_SHADER_TESS_EVAL
) ||
5421 shader
->key
.as_ls
||
5425 ac_optimize_vs_outputs(&ctx
->ac
,
5427 shader
->info
.vs_output_param_offset
,
5429 &shader
->info
.nr_param_exports
);
5432 static void si_count_scratch_private_memory(struct si_shader_context
*ctx
)
5434 ctx
->shader
->config
.private_mem_vgprs
= 0;
5436 /* Process all LLVM instructions. */
5437 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
5439 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
5442 LLVMValueRef inst
= next
;
5443 next
= LLVMGetNextInstruction(next
);
5445 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
5448 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
5449 /* No idea why LLVM aligns allocas to 4 elements. */
5450 unsigned alignment
= LLVMGetAlignment(inst
);
5451 unsigned dw_size
= align(llvm_get_type_size(type
) / 4, alignment
);
5452 ctx
->shader
->config
.private_mem_vgprs
+= dw_size
;
5454 bb
= LLVMGetNextBasicBlock(bb
);
5458 static void si_init_exec_full_mask(struct si_shader_context
*ctx
)
5460 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
5461 lp_build_intrinsic(ctx
->gallivm
.builder
,
5462 "llvm.amdgcn.init.exec", ctx
->voidt
,
5463 &full_mask
, 1, LP_FUNC_ATTR_CONVERGENT
);
5466 static void si_init_exec_from_input(struct si_shader_context
*ctx
,
5467 unsigned param
, unsigned bitoffset
)
5469 LLVMValueRef args
[] = {
5470 LLVMGetParam(ctx
->main_fn
, param
),
5471 LLVMConstInt(ctx
->i32
, bitoffset
, 0),
5473 lp_build_intrinsic(ctx
->gallivm
.builder
,
5474 "llvm.amdgcn.init.exec.from.input",
5475 ctx
->voidt
, args
, 2, LP_FUNC_ATTR_CONVERGENT
);
5478 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
,
5481 struct si_shader
*shader
= ctx
->shader
;
5482 struct si_shader_selector
*sel
= shader
->selector
;
5483 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5485 switch (ctx
->type
) {
5486 case PIPE_SHADER_VERTEX
:
5487 ctx
->load_input
= declare_input_vs
;
5488 if (shader
->key
.as_ls
)
5489 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
5490 else if (shader
->key
.as_es
)
5491 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5493 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5495 case PIPE_SHADER_TESS_CTRL
:
5496 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
5497 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
5498 bld_base
->emit_store
= store_output_tcs
;
5499 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
5501 case PIPE_SHADER_TESS_EVAL
:
5502 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
5503 if (shader
->key
.as_es
)
5504 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5506 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5508 case PIPE_SHADER_GEOMETRY
:
5509 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
5510 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
5512 case PIPE_SHADER_FRAGMENT
:
5513 ctx
->load_input
= declare_input_fs
;
5514 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
5516 case PIPE_SHADER_COMPUTE
:
5517 ctx
->declare_memory_region
= declare_compute_memory
;
5520 assert(!"Unsupported shader type");
5524 create_function(ctx
);
5525 preload_ring_buffers(ctx
);
5527 /* For GFX9 merged shaders:
5528 * - Set EXEC. If the prolog is present, set EXEC there instead.
5529 * - Add a barrier before the second shader.
5531 * The same thing for monolithic shaders is done in
5532 * si_build_wrapper_function.
5534 if (ctx
->screen
->b
.chip_class
>= GFX9
&& !is_monolithic
) {
5535 if (sel
->info
.num_instructions
> 1 && /* not empty shader */
5536 (shader
->key
.as_es
|| shader
->key
.as_ls
) &&
5537 (ctx
->type
== PIPE_SHADER_TESS_EVAL
||
5538 (ctx
->type
== PIPE_SHADER_VERTEX
&&
5539 !sel
->vs_needs_prolog
))) {
5540 si_init_exec_from_input(ctx
,
5541 ctx
->param_merged_wave_info
, 0);
5542 } else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5543 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5544 si_init_exec_from_input(ctx
,
5545 ctx
->param_merged_wave_info
, 8);
5546 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
5550 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5552 for (i
= 0; i
< 4; i
++) {
5553 ctx
->gs_next_vertex
[i
] =
5554 lp_build_alloca(&ctx
->gallivm
,
5559 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&& sel
->info
.uses_kill
&&
5560 ctx
->screen
->b
.debug_flags
& DBG_FS_CORRECT_DERIVS_AFTER_KILL
) {
5561 /* This is initialized to 0.0 = not kill. */
5562 ctx
->postponed_kill
= lp_build_alloca(&ctx
->gallivm
, ctx
->f32
, "");
5565 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
5566 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
5570 si_llvm_build_ret(ctx
, ctx
->return_value
);
5575 * Compute the VS prolog key, which contains all the information needed to
5576 * build the VS prolog function, and set shader->info bits where needed.
5578 * \param info Shader info of the vertex shader.
5579 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
5580 * \param prolog_key Key of the VS prolog
5581 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
5582 * \param key Output shader part key.
5584 static void si_get_vs_prolog_key(const struct tgsi_shader_info
*info
,
5585 unsigned num_input_sgprs
,
5586 const struct si_vs_prolog_bits
*prolog_key
,
5587 struct si_shader
*shader_out
,
5588 union si_shader_part_key
*key
)
5590 memset(key
, 0, sizeof(*key
));
5591 key
->vs_prolog
.states
= *prolog_key
;
5592 key
->vs_prolog
.num_input_sgprs
= num_input_sgprs
;
5593 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
5594 key
->vs_prolog
.as_ls
= shader_out
->key
.as_ls
;
5596 if (shader_out
->selector
->type
== PIPE_SHADER_TESS_CTRL
) {
5597 key
->vs_prolog
.as_ls
= 1;
5598 key
->vs_prolog
.num_merged_next_stage_vgprs
= 2;
5599 } else if (shader_out
->selector
->type
== PIPE_SHADER_GEOMETRY
) {
5600 key
->vs_prolog
.num_merged_next_stage_vgprs
= 5;
5603 /* Enable loading the InstanceID VGPR. */
5604 uint16_t input_mask
= u_bit_consecutive(0, info
->num_inputs
);
5606 if ((key
->vs_prolog
.states
.instance_divisor_is_one
|
5607 key
->vs_prolog
.states
.instance_divisor_is_fetched
) & input_mask
)
5608 shader_out
->info
.uses_instanceid
= true;
5612 * Compute the PS prolog key, which contains all the information needed to
5613 * build the PS prolog function, and set related bits in shader->config.
5615 static void si_get_ps_prolog_key(struct si_shader
*shader
,
5616 union si_shader_part_key
*key
,
5617 bool separate_prolog
)
5619 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5621 memset(key
, 0, sizeof(*key
));
5622 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
5623 key
->ps_prolog
.colors_read
= info
->colors_read
;
5624 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
5625 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
5626 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
5627 (key
->ps_prolog
.colors_read
||
5628 key
->ps_prolog
.states
.force_persp_sample_interp
||
5629 key
->ps_prolog
.states
.force_linear_sample_interp
||
5630 key
->ps_prolog
.states
.force_persp_center_interp
||
5631 key
->ps_prolog
.states
.force_linear_center_interp
||
5632 key
->ps_prolog
.states
.bc_optimize_for_persp
||
5633 key
->ps_prolog
.states
.bc_optimize_for_linear
);
5635 if (info
->colors_read
) {
5636 unsigned *color
= shader
->selector
->color_attr_index
;
5638 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
5639 /* BCOLORs are stored after the last input. */
5640 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
5641 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
5642 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
5645 for (unsigned i
= 0; i
< 2; i
++) {
5646 unsigned interp
= info
->input_interpolate
[color
[i
]];
5647 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
5649 if (!(info
->colors_read
& (0xf << i
*4)))
5652 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
5654 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
5655 interp
== TGSI_INTERPOLATE_COLOR
)
5656 interp
= TGSI_INTERPOLATE_CONSTANT
;
5659 case TGSI_INTERPOLATE_CONSTANT
:
5660 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
5662 case TGSI_INTERPOLATE_PERSPECTIVE
:
5663 case TGSI_INTERPOLATE_COLOR
:
5664 /* Force the interpolation location for colors here. */
5665 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
5666 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
5667 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
5668 location
= TGSI_INTERPOLATE_LOC_CENTER
;
5671 case TGSI_INTERPOLATE_LOC_SAMPLE
:
5672 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
5673 shader
->config
.spi_ps_input_ena
|=
5674 S_0286CC_PERSP_SAMPLE_ENA(1);
5676 case TGSI_INTERPOLATE_LOC_CENTER
:
5677 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
5678 shader
->config
.spi_ps_input_ena
|=
5679 S_0286CC_PERSP_CENTER_ENA(1);
5681 case TGSI_INTERPOLATE_LOC_CENTROID
:
5682 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
5683 shader
->config
.spi_ps_input_ena
|=
5684 S_0286CC_PERSP_CENTROID_ENA(1);
5690 case TGSI_INTERPOLATE_LINEAR
:
5691 /* Force the interpolation location for colors here. */
5692 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
5693 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
5694 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
5695 location
= TGSI_INTERPOLATE_LOC_CENTER
;
5697 /* The VGPR assignment for non-monolithic shaders
5698 * works because InitialPSInputAddr is set on the
5699 * main shader and PERSP_PULL_MODEL is never used.
5702 case TGSI_INTERPOLATE_LOC_SAMPLE
:
5703 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
5704 separate_prolog
? 6 : 9;
5705 shader
->config
.spi_ps_input_ena
|=
5706 S_0286CC_LINEAR_SAMPLE_ENA(1);
5708 case TGSI_INTERPOLATE_LOC_CENTER
:
5709 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
5710 separate_prolog
? 8 : 11;
5711 shader
->config
.spi_ps_input_ena
|=
5712 S_0286CC_LINEAR_CENTER_ENA(1);
5714 case TGSI_INTERPOLATE_LOC_CENTROID
:
5715 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
5716 separate_prolog
? 10 : 13;
5717 shader
->config
.spi_ps_input_ena
|=
5718 S_0286CC_LINEAR_CENTROID_ENA(1);
5732 * Check whether a PS prolog is required based on the key.
5734 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
5736 return key
->ps_prolog
.colors_read
||
5737 key
->ps_prolog
.states
.force_persp_sample_interp
||
5738 key
->ps_prolog
.states
.force_linear_sample_interp
||
5739 key
->ps_prolog
.states
.force_persp_center_interp
||
5740 key
->ps_prolog
.states
.force_linear_center_interp
||
5741 key
->ps_prolog
.states
.bc_optimize_for_persp
||
5742 key
->ps_prolog
.states
.bc_optimize_for_linear
||
5743 key
->ps_prolog
.states
.poly_stipple
;
5747 * Compute the PS epilog key, which contains all the information needed to
5748 * build the PS epilog function.
5750 static void si_get_ps_epilog_key(struct si_shader
*shader
,
5751 union si_shader_part_key
*key
)
5753 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5754 memset(key
, 0, sizeof(*key
));
5755 key
->ps_epilog
.colors_written
= info
->colors_written
;
5756 key
->ps_epilog
.writes_z
= info
->writes_z
;
5757 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
5758 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
5759 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
5763 * Build the GS prolog function. Rotate the input vertices for triangle strips
5766 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
5767 union si_shader_part_key
*key
)
5769 unsigned num_sgprs
, num_vgprs
;
5770 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5771 LLVMBuilderRef builder
= gallivm
->builder
;
5772 LLVMTypeRef params
[48]; /* 40 SGPRs (maximum) + some VGPRs */
5773 LLVMTypeRef returns
[48];
5774 LLVMValueRef func
, ret
;
5776 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
5777 num_sgprs
= 8 + GFX9_GS_NUM_USER_SGPR
;
5778 num_vgprs
= 5; /* ES inputs are not needed by GS */
5780 num_sgprs
= GFX6_GS_NUM_USER_SGPR
+ 2;
5784 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
5785 params
[i
] = ctx
->i32
;
5786 returns
[i
] = ctx
->i32
;
5789 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
5790 params
[num_sgprs
+ i
] = ctx
->i32
;
5791 returns
[num_sgprs
+ i
] = ctx
->f32
;
5794 /* Create the function. */
5795 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
5796 params
, num_sgprs
+ num_vgprs
, num_sgprs
- 1, 0);
5797 func
= ctx
->main_fn
;
5799 /* Set the full EXEC mask for the prolog, because we are only fiddling
5800 * with registers here. The main shader part will set the correct EXEC
5803 if (ctx
->screen
->b
.chip_class
>= GFX9
&& !key
->gs_prolog
.is_monolithic
)
5804 si_init_exec_full_mask(ctx
);
5806 /* Copy inputs to outputs. This should be no-op, as the registers match,
5807 * but it will prevent the compiler from overwriting them unintentionally.
5809 ret
= ctx
->return_value
;
5810 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
5811 LLVMValueRef p
= LLVMGetParam(func
, i
);
5812 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
5814 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
5815 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
5816 p
= LLVMBuildBitCast(builder
, p
, ctx
->f32
, "");
5817 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
5820 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
5821 /* Remap the input vertices for every other primitive. */
5822 const unsigned gfx6_vtx_params
[6] = {
5830 const unsigned gfx9_vtx_params
[3] = {
5835 LLVMValueRef vtx_in
[6], vtx_out
[6];
5836 LLVMValueRef prim_id
, rotate
;
5838 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
5839 for (unsigned i
= 0; i
< 3; i
++) {
5840 vtx_in
[i
*2] = unpack_param(ctx
, gfx9_vtx_params
[i
], 0, 16);
5841 vtx_in
[i
*2+1] = unpack_param(ctx
, gfx9_vtx_params
[i
], 16, 16);
5844 for (unsigned i
= 0; i
< 6; i
++)
5845 vtx_in
[i
] = LLVMGetParam(func
, gfx6_vtx_params
[i
]);
5848 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
5849 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
5851 for (unsigned i
= 0; i
< 6; ++i
) {
5852 LLVMValueRef base
, rotated
;
5854 rotated
= vtx_in
[(i
+ 4) % 6];
5855 vtx_out
[i
] = LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
5858 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
5859 for (unsigned i
= 0; i
< 3; i
++) {
5860 LLVMValueRef hi
, out
;
5862 hi
= LLVMBuildShl(builder
, vtx_out
[i
*2+1],
5863 LLVMConstInt(ctx
->i32
, 16, 0), "");
5864 out
= LLVMBuildOr(builder
, vtx_out
[i
*2], hi
, "");
5865 out
= LLVMBuildBitCast(builder
, out
, ctx
->f32
, "");
5866 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
5867 gfx9_vtx_params
[i
], "");
5870 for (unsigned i
= 0; i
< 6; i
++) {
5873 out
= LLVMBuildBitCast(builder
, vtx_out
[i
], ctx
->f32
, "");
5874 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
5875 gfx6_vtx_params
[i
], "");
5880 LLVMBuildRet(builder
, ret
);
5884 * Given a list of shader part functions, build a wrapper function that
5885 * runs them in sequence to form a monolithic shader.
5887 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
5888 LLVMValueRef
*parts
,
5891 unsigned next_shader_first_part
)
5893 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5894 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
5895 /* PS epilog has one arg per color component */
5896 LLVMTypeRef param_types
[48];
5897 LLVMValueRef initial
[48], out
[48];
5898 LLVMTypeRef function_type
;
5899 unsigned num_params
;
5900 unsigned num_out
, initial_num_out
;
5901 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
5902 MAYBE_UNUSED
unsigned initial_num_out_sgpr
; /* used in debug checks */
5903 unsigned num_sgprs
, num_vgprs
;
5904 unsigned last_sgpr_param
;
5906 struct lp_build_if_state if_state
;
5908 for (unsigned i
= 0; i
< num_parts
; ++i
) {
5909 lp_add_function_attr(parts
[i
], -1, LP_FUNC_ATTR_ALWAYSINLINE
);
5910 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
5913 /* The parameters of the wrapper function correspond to those of the
5914 * first part in terms of SGPRs and VGPRs, but we use the types of the
5915 * main part to get the right types. This is relevant for the
5916 * dereferenceable attribute on descriptor table pointers.
5921 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
5922 num_params
= LLVMCountParamTypes(function_type
);
5924 for (unsigned i
= 0; i
< num_params
; ++i
) {
5925 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
5927 if (ac_is_sgpr_param(param
)) {
5928 assert(num_vgprs
== 0);
5929 num_sgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
5931 num_vgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
5934 assert(num_vgprs
+ num_sgprs
<= ARRAY_SIZE(param_types
));
5937 last_sgpr_param
= 0;
5939 while (gprs
< num_sgprs
+ num_vgprs
) {
5940 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], num_params
);
5943 param_types
[num_params
] = LLVMTypeOf(param
);
5944 if (gprs
< num_sgprs
)
5945 last_sgpr_param
= num_params
;
5946 size
= llvm_get_type_size(param_types
[num_params
]) / 4;
5949 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
5950 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
5951 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
5956 si_create_function(ctx
, "wrapper", NULL
, 0, param_types
, num_params
,
5958 si_get_max_workgroup_size(ctx
->shader
));
5960 if (is_merged_shader(ctx
->shader
))
5961 si_init_exec_full_mask(ctx
);
5963 /* Record the arguments of the function as if they were an output of
5969 for (unsigned i
= 0; i
< num_params
; ++i
) {
5970 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
5971 LLVMTypeRef param_type
= LLVMTypeOf(param
);
5972 LLVMTypeRef out_type
= i
<= last_sgpr_param
? ctx
->i32
: ctx
->f32
;
5973 unsigned size
= llvm_get_type_size(param_type
) / 4;
5976 if (param_type
!= out_type
)
5977 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
5978 out
[num_out
++] = param
;
5980 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
5982 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
5983 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
5984 param_type
= ctx
->i64
;
5987 if (param_type
!= vector_type
)
5988 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
5990 for (unsigned j
= 0; j
< size
; ++j
)
5991 out
[num_out
++] = LLVMBuildExtractElement(
5992 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
5995 if (i
<= last_sgpr_param
)
5996 num_out_sgpr
= num_out
;
5999 memcpy(initial
, out
, sizeof(out
));
6000 initial_num_out
= num_out
;
6001 initial_num_out_sgpr
= num_out_sgpr
;
6003 /* Now chain the parts. */
6004 for (unsigned part
= 0; part
< num_parts
; ++part
) {
6005 LLVMValueRef in
[48];
6007 LLVMTypeRef ret_type
;
6008 unsigned out_idx
= 0;
6010 num_params
= LLVMCountParams(parts
[part
]);
6011 assert(num_params
<= ARRAY_SIZE(param_types
));
6013 /* Merged shaders are executed conditionally depending
6014 * on the number of enabled threads passed in the input SGPRs. */
6015 if (is_merged_shader(ctx
->shader
) &&
6016 (part
== 0 || part
== next_shader_first_part
)) {
6017 LLVMValueRef ena
, count
= initial
[3];
6019 /* The thread count for the 2nd shader is at bit-offset 8. */
6020 if (part
== next_shader_first_part
) {
6021 count
= LLVMBuildLShr(builder
, count
,
6022 LLVMConstInt(ctx
->i32
, 8, 0), "");
6024 count
= LLVMBuildAnd(builder
, count
,
6025 LLVMConstInt(ctx
->i32
, 0x7f, 0), "");
6026 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
6027 ac_get_thread_id(&ctx
->ac
), count
, "");
6028 lp_build_if(&if_state
, &ctx
->gallivm
, ena
);
6031 /* Derive arguments for the next part from outputs of the
6034 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
6036 LLVMTypeRef param_type
;
6038 unsigned param_size
;
6039 LLVMValueRef arg
= NULL
;
6041 param
= LLVMGetParam(parts
[part
], param_idx
);
6042 param_type
= LLVMTypeOf(param
);
6043 param_size
= llvm_get_type_size(param_type
) / 4;
6044 is_sgpr
= ac_is_sgpr_param(param
);
6047 #if HAVE_LLVM < 0x0400
6048 LLVMRemoveAttribute(param
, LLVMByValAttribute
);
6050 unsigned kind_id
= LLVMGetEnumAttributeKindForName("byval", 5);
6051 LLVMRemoveEnumAttributeAtIndex(parts
[part
], param_idx
+ 1, kind_id
);
6053 lp_add_function_attr(parts
[part
], param_idx
+ 1, LP_FUNC_ATTR_INREG
);
6056 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
6057 assert(is_sgpr
|| out_idx
>= num_out_sgpr
);
6059 if (param_size
== 1)
6062 arg
= lp_build_gather_values(gallivm
, &out
[out_idx
], param_size
);
6064 if (LLVMTypeOf(arg
) != param_type
) {
6065 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6066 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
6067 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6069 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
6073 in
[param_idx
] = arg
;
6074 out_idx
+= param_size
;
6077 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
6079 if (is_merged_shader(ctx
->shader
) &&
6080 (part
+ 1 == next_shader_first_part
||
6081 part
+ 1 == num_parts
)) {
6082 lp_build_endif(&if_state
);
6084 if (part
+ 1 == next_shader_first_part
) {
6085 /* A barrier is required between 2 merged shaders. */
6086 si_llvm_emit_barrier(NULL
, &ctx
->bld_base
, NULL
);
6088 /* The second half of the merged shader should use
6089 * the inputs from the toplevel (wrapper) function,
6090 * not the return value from the last call.
6092 * That's because the last call was executed condi-
6093 * tionally, so we can't consume it in the main
6096 memcpy(out
, initial
, sizeof(initial
));
6097 num_out
= initial_num_out
;
6098 num_out_sgpr
= initial_num_out_sgpr
;
6103 /* Extract the returned GPRs. */
6104 ret_type
= LLVMTypeOf(ret
);
6108 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
6109 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
6111 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
6113 for (unsigned i
= 0; i
< ret_size
; ++i
) {
6115 LLVMBuildExtractValue(builder
, ret
, i
, "");
6117 out
[num_out
++] = val
;
6119 if (LLVMTypeOf(val
) == ctx
->i32
) {
6120 assert(num_out_sgpr
+ 1 == num_out
);
6121 num_out_sgpr
= num_out
;
6127 LLVMBuildRetVoid(builder
);
6130 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
6131 LLVMTargetMachineRef tm
,
6132 struct si_shader
*shader
,
6134 struct pipe_debug_callback
*debug
)
6136 struct si_shader_selector
*sel
= shader
->selector
;
6137 struct si_shader_context ctx
;
6140 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6141 * conversion fails. */
6142 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
6143 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
6144 tgsi_dump(sel
->tokens
, 0);
6145 si_dump_streamout(&sel
->so
);
6148 si_init_shader_ctx(&ctx
, sscreen
, tm
);
6149 si_llvm_context_set_tgsi(&ctx
, shader
);
6150 ctx
.separate_prolog
= !is_monolithic
;
6152 memset(shader
->info
.vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6153 sizeof(shader
->info
.vs_output_param_offset
));
6155 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
6157 ctx
.load_system_value
= declare_system_value
;
6159 if (!si_compile_tgsi_main(&ctx
, is_monolithic
)) {
6160 si_llvm_dispose(&ctx
);
6164 if (is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
6165 LLVMValueRef parts
[2];
6166 bool need_prolog
= sel
->vs_needs_prolog
;
6168 parts
[1] = ctx
.main_fn
;
6171 union si_shader_part_key prolog_key
;
6172 si_get_vs_prolog_key(&sel
->info
,
6173 shader
->info
.num_input_sgprs
,
6174 &shader
->key
.part
.vs
.prolog
,
6175 shader
, &prolog_key
);
6176 si_build_vs_prolog_function(&ctx
, &prolog_key
);
6177 parts
[0] = ctx
.main_fn
;
6180 si_build_wrapper_function(&ctx
, parts
+ !need_prolog
,
6181 1 + need_prolog
, need_prolog
, 0);
6182 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
6183 if (sscreen
->b
.chip_class
>= GFX9
) {
6184 struct si_shader_selector
*ls
= shader
->key
.part
.tcs
.ls
;
6185 LLVMValueRef parts
[4];
6188 parts
[2] = ctx
.main_fn
;
6191 union si_shader_part_key tcs_epilog_key
;
6192 memset(&tcs_epilog_key
, 0, sizeof(tcs_epilog_key
));
6193 tcs_epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6194 si_build_tcs_epilog_function(&ctx
, &tcs_epilog_key
);
6195 parts
[3] = ctx
.main_fn
;
6198 if (ls
->vs_needs_prolog
) {
6199 union si_shader_part_key vs_prolog_key
;
6200 si_get_vs_prolog_key(&ls
->info
,
6201 shader
->info
.num_input_sgprs
,
6202 &shader
->key
.part
.tcs
.ls_prolog
,
6203 shader
, &vs_prolog_key
);
6204 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6205 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6206 parts
[0] = ctx
.main_fn
;
6209 /* VS as LS main part */
6210 struct si_shader shader_ls
= {};
6211 shader_ls
.selector
= ls
;
6212 shader_ls
.key
.as_ls
= 1;
6213 shader_ls
.key
.mono
= shader
->key
.mono
;
6214 shader_ls
.key
.opt
= shader
->key
.opt
;
6215 si_llvm_context_set_tgsi(&ctx
, &shader_ls
);
6217 if (!si_compile_tgsi_main(&ctx
, true)) {
6218 si_llvm_dispose(&ctx
);
6221 shader
->info
.uses_instanceid
|= ls
->info
.uses_instanceid
;
6222 parts
[1] = ctx
.main_fn
;
6224 /* Reset the shader context. */
6225 ctx
.shader
= shader
;
6226 ctx
.type
= PIPE_SHADER_TESS_CTRL
;
6228 si_build_wrapper_function(&ctx
,
6229 parts
+ !ls
->vs_needs_prolog
,
6230 4 - !ls
->vs_needs_prolog
, 0,
6231 ls
->vs_needs_prolog
? 2 : 1);
6233 LLVMValueRef parts
[2];
6234 union si_shader_part_key epilog_key
;
6236 parts
[0] = ctx
.main_fn
;
6238 memset(&epilog_key
, 0, sizeof(epilog_key
));
6239 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6240 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
6241 parts
[1] = ctx
.main_fn
;
6243 si_build_wrapper_function(&ctx
, parts
, 2, 0, 0);
6245 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
6246 if (ctx
.screen
->b
.chip_class
>= GFX9
) {
6247 struct si_shader_selector
*es
= shader
->key
.part
.gs
.es
;
6248 LLVMValueRef es_prolog
= NULL
;
6249 LLVMValueRef es_main
= NULL
;
6250 LLVMValueRef gs_prolog
= NULL
;
6251 LLVMValueRef gs_main
= ctx
.main_fn
;
6254 union si_shader_part_key gs_prolog_key
;
6255 memset(&gs_prolog_key
, 0, sizeof(gs_prolog_key
));
6256 gs_prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6257 gs_prolog_key
.gs_prolog
.is_monolithic
= true;
6258 si_build_gs_prolog_function(&ctx
, &gs_prolog_key
);
6259 gs_prolog
= ctx
.main_fn
;
6262 if (es
->vs_needs_prolog
) {
6263 union si_shader_part_key vs_prolog_key
;
6264 si_get_vs_prolog_key(&es
->info
,
6265 shader
->info
.num_input_sgprs
,
6266 &shader
->key
.part
.tcs
.ls_prolog
,
6267 shader
, &vs_prolog_key
);
6268 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6269 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6270 es_prolog
= ctx
.main_fn
;
6274 struct si_shader shader_es
= {};
6275 shader_es
.selector
= es
;
6276 shader_es
.key
.as_es
= 1;
6277 shader_es
.key
.mono
= shader
->key
.mono
;
6278 shader_es
.key
.opt
= shader
->key
.opt
;
6279 si_llvm_context_set_tgsi(&ctx
, &shader_es
);
6281 if (!si_compile_tgsi_main(&ctx
, true)) {
6282 si_llvm_dispose(&ctx
);
6285 shader
->info
.uses_instanceid
|= es
->info
.uses_instanceid
;
6286 es_main
= ctx
.main_fn
;
6288 /* Reset the shader context. */
6289 ctx
.shader
= shader
;
6290 ctx
.type
= PIPE_SHADER_GEOMETRY
;
6292 /* Prepare the array of shader parts. */
6293 LLVMValueRef parts
[4];
6294 unsigned num_parts
= 0, main_part
, next_first_part
;
6297 parts
[num_parts
++] = es_prolog
;
6299 parts
[main_part
= num_parts
++] = es_main
;
6300 parts
[next_first_part
= num_parts
++] = gs_prolog
;
6301 parts
[num_parts
++] = gs_main
;
6303 si_build_wrapper_function(&ctx
, parts
, num_parts
,
6304 main_part
, next_first_part
);
6306 LLVMValueRef parts
[2];
6307 union si_shader_part_key prolog_key
;
6309 parts
[1] = ctx
.main_fn
;
6311 memset(&prolog_key
, 0, sizeof(prolog_key
));
6312 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6313 si_build_gs_prolog_function(&ctx
, &prolog_key
);
6314 parts
[0] = ctx
.main_fn
;
6316 si_build_wrapper_function(&ctx
, parts
, 2, 1, 0);
6318 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6319 LLVMValueRef parts
[3];
6320 union si_shader_part_key prolog_key
;
6321 union si_shader_part_key epilog_key
;
6324 si_get_ps_prolog_key(shader
, &prolog_key
, false);
6325 need_prolog
= si_need_ps_prolog(&prolog_key
);
6327 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
6330 si_build_ps_prolog_function(&ctx
, &prolog_key
);
6331 parts
[0] = ctx
.main_fn
;
6334 si_get_ps_epilog_key(shader
, &epilog_key
);
6335 si_build_ps_epilog_function(&ctx
, &epilog_key
);
6336 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
6338 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2,
6339 need_prolog
? 1 : 0, 0);
6342 si_llvm_optimize_module(&ctx
);
6344 /* Post-optimization transformations and analysis. */
6345 si_optimize_vs_outputs(&ctx
);
6347 if ((debug
&& debug
->debug_message
) ||
6348 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
6349 si_count_scratch_private_memory(&ctx
);
6351 /* Compile to bytecode. */
6352 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
6353 ctx
.gallivm
.module
, debug
, ctx
.type
, "TGSI shader");
6354 si_llvm_dispose(&ctx
);
6356 fprintf(stderr
, "LLVM failed to compile shader\n");
6360 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
6361 * LLVM 3.9svn has this bug.
6363 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
6364 unsigned wave_size
= 64;
6365 unsigned max_vgprs
= 256;
6366 unsigned max_sgprs
= sscreen
->b
.chip_class
>= VI
? 800 : 512;
6367 unsigned max_sgprs_per_wave
= 128;
6368 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
6369 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
6370 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
6372 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
6373 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
6375 if (shader
->config
.num_sgprs
> max_sgprs
||
6376 shader
->config
.num_vgprs
> max_vgprs
) {
6377 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
6378 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
6379 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
6380 max_sgprs
, max_vgprs
);
6382 /* Just terminate the process, because dependent
6383 * shaders can hang due to bad input data, but use
6384 * the env var to allow shader-db to work.
6386 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
6391 /* Add the scratch offset to input SGPRs. */
6392 if (shader
->config
.scratch_bytes_per_wave
&& !is_merged_shader(shader
))
6393 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
6395 /* Calculate the number of fragment input VGPRs. */
6396 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6397 shader
->info
.num_input_vgprs
= 0;
6398 shader
->info
.face_vgpr_index
= -1;
6400 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
6401 shader
->info
.num_input_vgprs
+= 2;
6402 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
6403 shader
->info
.num_input_vgprs
+= 2;
6404 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
6405 shader
->info
.num_input_vgprs
+= 2;
6406 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
6407 shader
->info
.num_input_vgprs
+= 3;
6408 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
6409 shader
->info
.num_input_vgprs
+= 2;
6410 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
6411 shader
->info
.num_input_vgprs
+= 2;
6412 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
6413 shader
->info
.num_input_vgprs
+= 2;
6414 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
6415 shader
->info
.num_input_vgprs
+= 1;
6416 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
6417 shader
->info
.num_input_vgprs
+= 1;
6418 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
6419 shader
->info
.num_input_vgprs
+= 1;
6420 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
6421 shader
->info
.num_input_vgprs
+= 1;
6422 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
6423 shader
->info
.num_input_vgprs
+= 1;
6424 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
6425 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
6426 shader
->info
.num_input_vgprs
+= 1;
6428 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
6429 shader
->info
.num_input_vgprs
+= 1;
6430 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
6431 shader
->info
.num_input_vgprs
+= 1;
6432 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
6433 shader
->info
.num_input_vgprs
+= 1;
6440 * Create, compile and return a shader part (prolog or epilog).
6442 * \param sscreen screen
6443 * \param list list of shader parts of the same category
6444 * \param type shader type
6445 * \param key shader part key
6446 * \param prolog whether the part being requested is a prolog
6447 * \param tm LLVM target machine
6448 * \param debug debug callback
6449 * \param build the callback responsible for building the main function
6450 * \return non-NULL on success
6452 static struct si_shader_part
*
6453 si_get_shader_part(struct si_screen
*sscreen
,
6454 struct si_shader_part
**list
,
6455 enum pipe_shader_type type
,
6457 union si_shader_part_key
*key
,
6458 LLVMTargetMachineRef tm
,
6459 struct pipe_debug_callback
*debug
,
6460 void (*build
)(struct si_shader_context
*,
6461 union si_shader_part_key
*),
6464 struct si_shader_part
*result
;
6466 mtx_lock(&sscreen
->shader_parts_mutex
);
6468 /* Find existing. */
6469 for (result
= *list
; result
; result
= result
->next
) {
6470 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
6471 mtx_unlock(&sscreen
->shader_parts_mutex
);
6476 /* Compile a new one. */
6477 result
= CALLOC_STRUCT(si_shader_part
);
6480 struct si_shader shader
= {};
6481 struct si_shader_context ctx
;
6482 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
6484 si_init_shader_ctx(&ctx
, sscreen
, tm
);
6485 ctx
.shader
= &shader
;
6489 case PIPE_SHADER_VERTEX
:
6491 case PIPE_SHADER_TESS_CTRL
:
6493 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
6495 case PIPE_SHADER_GEOMETRY
:
6498 case PIPE_SHADER_FRAGMENT
:
6500 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
6502 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
6505 unreachable("bad shader part");
6511 si_llvm_optimize_module(&ctx
);
6513 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, tm
,
6514 gallivm
->module
, debug
, ctx
.type
, name
)) {
6520 result
->next
= *list
;
6524 si_llvm_dispose(&ctx
);
6525 mtx_unlock(&sscreen
->shader_parts_mutex
);
6529 static LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
6531 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
6532 LLVMValueRef ptr
[2], list
;
6534 /* Get the pointer to rw buffers. */
6535 ptr
[0] = LLVMGetParam(ctx
->main_fn
, SI_SGPR_RW_BUFFERS
);
6536 ptr
[1] = LLVMGetParam(ctx
->main_fn
, SI_SGPR_RW_BUFFERS_HI
);
6537 list
= lp_build_gather_values(gallivm
, ptr
, 2);
6538 list
= LLVMBuildBitCast(gallivm
->builder
, list
, ctx
->i64
, "");
6539 list
= LLVMBuildIntToPtr(gallivm
->builder
, list
,
6540 si_const_array(ctx
->v4i32
, SI_NUM_RW_BUFFERS
), "");
6545 * Build the vertex shader prolog function.
6547 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
6548 * All inputs are returned unmodified. The vertex load indices are
6549 * stored after them, which will be used by the API VS for fetching inputs.
6551 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
6556 * (VertexID + BaseVertex),
6557 * (InstanceID + StartInstance),
6558 * (InstanceID / 2 + StartInstance)
6560 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
6561 union si_shader_part_key
*key
)
6563 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
6564 LLVMTypeRef
*params
, *returns
;
6565 LLVMValueRef ret
, func
;
6566 int last_sgpr
, num_params
, num_returns
, i
;
6567 unsigned first_vs_vgpr
= key
->vs_prolog
.num_input_sgprs
+
6568 key
->vs_prolog
.num_merged_next_stage_vgprs
;
6569 unsigned num_input_vgprs
= key
->vs_prolog
.num_merged_next_stage_vgprs
+ 4;
6570 unsigned num_all_input_regs
= key
->vs_prolog
.num_input_sgprs
+
6572 unsigned user_sgpr_base
= key
->vs_prolog
.num_merged_next_stage_vgprs
? 8 : 0;
6574 ctx
->param_vertex_id
= first_vs_vgpr
;
6575 ctx
->param_instance_id
= first_vs_vgpr
+ (key
->vs_prolog
.as_ls
? 2 : 1);
6577 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
6578 params
= alloca(num_all_input_regs
* sizeof(LLVMTypeRef
));
6579 returns
= alloca((num_all_input_regs
+ key
->vs_prolog
.last_input
+ 1) *
6580 sizeof(LLVMTypeRef
));
6584 /* Declare input and output SGPRs. */
6586 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
6587 params
[num_params
++] = ctx
->i32
;
6588 returns
[num_returns
++] = ctx
->i32
;
6590 last_sgpr
= num_params
- 1;
6592 /* Preloaded VGPRs (outputs must be floats) */
6593 for (i
= 0; i
< num_input_vgprs
; i
++) {
6594 params
[num_params
++] = ctx
->i32
;
6595 returns
[num_returns
++] = ctx
->f32
;
6598 /* Vertex load indices. */
6599 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
6600 returns
[num_returns
++] = ctx
->f32
;
6602 /* Create the function. */
6603 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, params
,
6604 num_params
, last_sgpr
, 0);
6605 func
= ctx
->main_fn
;
6607 if (key
->vs_prolog
.num_merged_next_stage_vgprs
&&
6608 !key
->vs_prolog
.is_monolithic
)
6609 si_init_exec_from_input(ctx
, 3, 0);
6611 /* Copy inputs to outputs. This should be no-op, as the registers match,
6612 * but it will prevent the compiler from overwriting them unintentionally.
6614 ret
= ctx
->return_value
;
6615 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
6616 LLVMValueRef p
= LLVMGetParam(func
, i
);
6617 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
6619 for (; i
< num_params
; i
++) {
6620 LLVMValueRef p
= LLVMGetParam(func
, i
);
6621 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
->f32
, "");
6622 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
6625 /* Compute vertex load indices from instance divisors. */
6626 LLVMValueRef instance_divisor_constbuf
= NULL
;
6628 if (key
->vs_prolog
.states
.instance_divisor_is_fetched
) {
6629 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
6630 LLVMValueRef buf_index
=
6631 LLVMConstInt(ctx
->i32
, SI_VS_CONST_INSTANCE_DIVISORS
, 0);
6632 instance_divisor_constbuf
=
6633 ac_build_indexed_load_const(&ctx
->ac
, list
, buf_index
);
6636 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
6637 bool divisor_is_one
=
6638 key
->vs_prolog
.states
.instance_divisor_is_one
& (1u << i
);
6639 bool divisor_is_fetched
=
6640 key
->vs_prolog
.states
.instance_divisor_is_fetched
& (1u << i
);
6643 if (divisor_is_one
|| divisor_is_fetched
) {
6644 LLVMValueRef divisor
= ctx
->i32_1
;
6646 if (divisor_is_fetched
) {
6647 divisor
= buffer_load_const(ctx
, instance_divisor_constbuf
,
6648 LLVMConstInt(ctx
->i32
, i
* 4, 0));
6649 divisor
= LLVMBuildBitCast(gallivm
->builder
, divisor
,
6653 /* InstanceID / Divisor + StartInstance */
6654 index
= get_instance_index_for_fetch(ctx
,
6656 SI_SGPR_START_INSTANCE
,
6659 /* VertexID + BaseVertex */
6660 index
= LLVMBuildAdd(gallivm
->builder
,
6661 LLVMGetParam(func
, ctx
->param_vertex_id
),
6662 LLVMGetParam(func
, user_sgpr_base
+
6663 SI_SGPR_BASE_VERTEX
), "");
6666 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
->f32
, "");
6667 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
6671 si_llvm_build_ret(ctx
, ret
);
6674 static bool si_get_vs_prolog(struct si_screen
*sscreen
,
6675 LLVMTargetMachineRef tm
,
6676 struct si_shader
*shader
,
6677 struct pipe_debug_callback
*debug
,
6678 struct si_shader
*main_part
,
6679 const struct si_vs_prolog_bits
*key
)
6681 struct si_shader_selector
*vs
= main_part
->selector
;
6683 /* The prolog is a no-op if there are no inputs. */
6684 if (!vs
->vs_needs_prolog
)
6687 /* Get the prolog. */
6688 union si_shader_part_key prolog_key
;
6689 si_get_vs_prolog_key(&vs
->info
, main_part
->info
.num_input_sgprs
,
6690 key
, shader
, &prolog_key
);
6693 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
6694 PIPE_SHADER_VERTEX
, true, &prolog_key
, tm
,
6695 debug
, si_build_vs_prolog_function
,
6696 "Vertex Shader Prolog");
6697 return shader
->prolog
!= NULL
;
6701 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
6703 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
6704 LLVMTargetMachineRef tm
,
6705 struct si_shader
*shader
,
6706 struct pipe_debug_callback
*debug
)
6708 return si_get_vs_prolog(sscreen
, tm
, shader
, debug
, shader
,
6709 &shader
->key
.part
.vs
.prolog
);
6713 * Compile the TCS epilog function. This writes tesselation factors to memory
6714 * based on the output primitive type of the tesselator (determined by TES).
6716 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
6717 union si_shader_part_key
*key
)
6719 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
6720 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6721 LLVMTypeRef params
[32];
6723 int last_sgpr
, num_params
= 0;
6725 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
6726 params
[num_params
++] = ctx
->i64
;
6727 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
6728 params
[num_params
++] = ctx
->i32
; /* wave info */
6729 params
[ctx
->param_tcs_factor_offset
= num_params
++] = ctx
->i32
;
6730 params
[num_params
++] = ctx
->i32
;
6731 params
[num_params
++] = ctx
->i32
;
6732 params
[num_params
++] = ctx
->i32
;
6733 params
[num_params
++] = ctx
->i64
;
6734 params
[num_params
++] = ctx
->i64
;
6735 params
[num_params
++] = ctx
->i64
;
6736 params
[num_params
++] = ctx
->i64
;
6737 params
[num_params
++] = ctx
->i32
;
6738 params
[num_params
++] = ctx
->i32
;
6739 params
[num_params
++] = ctx
->i32
;
6740 params
[num_params
++] = ctx
->i32
;
6741 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
6742 params
[num_params
++] = ctx
->i32
;
6743 params
[num_params
++] = ctx
->i32
;
6744 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
6745 params
[ctx
->param_tcs_factor_addr_base64k
= num_params
++] = ctx
->i32
;
6747 params
[num_params
++] = ctx
->i64
;
6748 params
[num_params
++] = ctx
->i64
;
6749 params
[num_params
++] = ctx
->i64
;
6750 params
[ctx
->param_tcs_offchip_layout
= num_params
++] = ctx
->i32
;
6751 params
[num_params
++] = ctx
->i32
;
6752 params
[num_params
++] = ctx
->i32
;
6753 params
[num_params
++] = ctx
->i32
;
6754 params
[ctx
->param_tcs_offchip_addr_base64k
= num_params
++] = ctx
->i32
;
6755 params
[ctx
->param_tcs_factor_addr_base64k
= num_params
++] = ctx
->i32
;
6756 params
[ctx
->param_tcs_offchip_offset
= num_params
++] = ctx
->i32
;
6757 params
[ctx
->param_tcs_factor_offset
= num_params
++] = ctx
->i32
;
6759 last_sgpr
= num_params
- 1;
6761 params
[num_params
++] = ctx
->i32
; /* patch index within the wave (REL_PATCH_ID) */
6762 params
[num_params
++] = ctx
->i32
; /* invocation ID within the patch */
6763 params
[num_params
++] = ctx
->i32
; /* LDS offset where tess factors should be loaded from */
6765 /* Create the function. */
6766 si_create_function(ctx
, "tcs_epilog", NULL
, 0, params
, num_params
, last_sgpr
,
6767 ctx
->screen
->b
.chip_class
>= CIK
? 128 : 64);
6768 declare_lds_as_pointer(ctx
);
6769 func
= ctx
->main_fn
;
6771 si_write_tess_factors(bld_base
,
6772 LLVMGetParam(func
, last_sgpr
+ 1),
6773 LLVMGetParam(func
, last_sgpr
+ 2),
6774 LLVMGetParam(func
, last_sgpr
+ 3));
6776 LLVMBuildRetVoid(gallivm
->builder
);
6780 * Select and compile (or reuse) TCS parts (epilog).
6782 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
6783 LLVMTargetMachineRef tm
,
6784 struct si_shader
*shader
,
6785 struct pipe_debug_callback
*debug
)
6787 if (sscreen
->b
.chip_class
>= GFX9
) {
6788 struct si_shader
*ls_main_part
=
6789 shader
->key
.part
.tcs
.ls
->main_shader_part_ls
;
6791 if (!si_get_vs_prolog(sscreen
, tm
, shader
, debug
, ls_main_part
,
6792 &shader
->key
.part
.tcs
.ls_prolog
))
6795 shader
->previous_stage
= ls_main_part
;
6798 /* Get the epilog. */
6799 union si_shader_part_key epilog_key
;
6800 memset(&epilog_key
, 0, sizeof(epilog_key
));
6801 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6803 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
6804 PIPE_SHADER_TESS_CTRL
, false,
6805 &epilog_key
, tm
, debug
,
6806 si_build_tcs_epilog_function
,
6807 "Tessellation Control Shader Epilog");
6808 return shader
->epilog
!= NULL
;
6812 * Select and compile (or reuse) GS parts (prolog).
6814 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
6815 LLVMTargetMachineRef tm
,
6816 struct si_shader
*shader
,
6817 struct pipe_debug_callback
*debug
)
6819 if (sscreen
->b
.chip_class
>= GFX9
) {
6820 struct si_shader
*es_main_part
=
6821 shader
->key
.part
.gs
.es
->main_shader_part_es
;
6823 if (shader
->key
.part
.gs
.es
->type
== PIPE_SHADER_VERTEX
&&
6824 !si_get_vs_prolog(sscreen
, tm
, shader
, debug
, es_main_part
,
6825 &shader
->key
.part
.gs
.vs_prolog
))
6828 shader
->previous_stage
= es_main_part
;
6831 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
6834 union si_shader_part_key prolog_key
;
6835 memset(&prolog_key
, 0, sizeof(prolog_key
));
6836 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6838 shader
->prolog2
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
6839 PIPE_SHADER_GEOMETRY
, true,
6840 &prolog_key
, tm
, debug
,
6841 si_build_gs_prolog_function
,
6842 "Geometry Shader Prolog");
6843 return shader
->prolog2
!= NULL
;
6847 * Build the pixel shader prolog function. This handles:
6848 * - two-side color selection and interpolation
6849 * - overriding interpolation parameters for the API PS
6850 * - polygon stippling
6852 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
6853 * overriden by other states. (e.g. per-sample interpolation)
6854 * Interpolated colors are stored after the preloaded VGPRs.
6856 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
6857 union si_shader_part_key
*key
)
6859 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
6860 LLVMTypeRef
*params
;
6861 LLVMValueRef ret
, func
;
6862 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
6864 assert(si_need_ps_prolog(key
));
6866 /* Number of inputs + 8 color elements. */
6867 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
6868 key
->ps_prolog
.num_input_vgprs
+ 8) *
6869 sizeof(LLVMTypeRef
));
6871 /* Declare inputs. */
6873 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
6874 params
[num_params
++] = ctx
->i32
;
6875 last_sgpr
= num_params
- 1;
6877 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
6878 params
[num_params
++] = ctx
->f32
;
6880 /* Declare outputs (same as inputs + add colors if needed) */
6881 num_returns
= num_params
;
6882 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
6883 for (i
= 0; i
< num_color_channels
; i
++)
6884 params
[num_returns
++] = ctx
->f32
;
6886 /* Create the function. */
6887 si_create_function(ctx
, "ps_prolog", params
, num_returns
, params
,
6888 num_params
, last_sgpr
, 0);
6889 func
= ctx
->main_fn
;
6891 /* Copy inputs to outputs. This should be no-op, as the registers match,
6892 * but it will prevent the compiler from overwriting them unintentionally.
6894 ret
= ctx
->return_value
;
6895 for (i
= 0; i
< num_params
; i
++) {
6896 LLVMValueRef p
= LLVMGetParam(func
, i
);
6897 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
6900 /* Polygon stippling. */
6901 if (key
->ps_prolog
.states
.poly_stipple
) {
6902 /* POS_FIXED_PT is always last. */
6903 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
6904 key
->ps_prolog
.num_input_vgprs
- 1;
6905 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
6907 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
6910 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
6911 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
6912 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
6913 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
6915 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
6916 * The hw doesn't compute CENTROID if the whole wave only
6917 * contains fully-covered quads.
6919 * PRIM_MASK is after user SGPRs.
6921 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
6922 bc_optimize
= LLVMBuildLShr(gallivm
->builder
, bc_optimize
,
6923 LLVMConstInt(ctx
->i32
, 31, 0), "");
6924 bc_optimize
= LLVMBuildTrunc(gallivm
->builder
, bc_optimize
,
6927 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
6928 /* Read PERSP_CENTER. */
6929 for (i
= 0; i
< 2; i
++)
6930 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
6931 /* Read PERSP_CENTROID. */
6932 for (i
= 0; i
< 2; i
++)
6933 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
6934 /* Select PERSP_CENTROID. */
6935 for (i
= 0; i
< 2; i
++) {
6936 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
6937 center
[i
], centroid
[i
], "");
6938 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6939 tmp
, base
+ 4 + i
, "");
6942 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
6943 /* Read LINEAR_CENTER. */
6944 for (i
= 0; i
< 2; i
++)
6945 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
6946 /* Read LINEAR_CENTROID. */
6947 for (i
= 0; i
< 2; i
++)
6948 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
6949 /* Select LINEAR_CENTROID. */
6950 for (i
= 0; i
< 2; i
++) {
6951 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
6952 center
[i
], centroid
[i
], "");
6953 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6954 tmp
, base
+ 10 + i
, "");
6959 /* Force per-sample interpolation. */
6960 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
6961 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
6962 LLVMValueRef persp_sample
[2];
6964 /* Read PERSP_SAMPLE. */
6965 for (i
= 0; i
< 2; i
++)
6966 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
6967 /* Overwrite PERSP_CENTER. */
6968 for (i
= 0; i
< 2; i
++)
6969 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6970 persp_sample
[i
], base
+ 2 + i
, "");
6971 /* Overwrite PERSP_CENTROID. */
6972 for (i
= 0; i
< 2; i
++)
6973 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6974 persp_sample
[i
], base
+ 4 + i
, "");
6976 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
6977 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
6978 LLVMValueRef linear_sample
[2];
6980 /* Read LINEAR_SAMPLE. */
6981 for (i
= 0; i
< 2; i
++)
6982 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
6983 /* Overwrite LINEAR_CENTER. */
6984 for (i
= 0; i
< 2; i
++)
6985 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6986 linear_sample
[i
], base
+ 8 + i
, "");
6987 /* Overwrite LINEAR_CENTROID. */
6988 for (i
= 0; i
< 2; i
++)
6989 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6990 linear_sample
[i
], base
+ 10 + i
, "");
6993 /* Force center interpolation. */
6994 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
6995 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
6996 LLVMValueRef persp_center
[2];
6998 /* Read PERSP_CENTER. */
6999 for (i
= 0; i
< 2; i
++)
7000 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7001 /* Overwrite PERSP_SAMPLE. */
7002 for (i
= 0; i
< 2; i
++)
7003 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7004 persp_center
[i
], base
+ i
, "");
7005 /* Overwrite PERSP_CENTROID. */
7006 for (i
= 0; i
< 2; i
++)
7007 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7008 persp_center
[i
], base
+ 4 + i
, "");
7010 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
7011 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7012 LLVMValueRef linear_center
[2];
7014 /* Read LINEAR_CENTER. */
7015 for (i
= 0; i
< 2; i
++)
7016 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7017 /* Overwrite LINEAR_SAMPLE. */
7018 for (i
= 0; i
< 2; i
++)
7019 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7020 linear_center
[i
], base
+ 6 + i
, "");
7021 /* Overwrite LINEAR_CENTROID. */
7022 for (i
= 0; i
< 2; i
++)
7023 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7024 linear_center
[i
], base
+ 10 + i
, "");
7027 /* Interpolate colors. */
7028 for (i
= 0; i
< 2; i
++) {
7029 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
7030 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7031 key
->ps_prolog
.face_vgpr_index
;
7032 LLVMValueRef interp
[2], color
[4];
7033 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
7038 /* If the interpolation qualifier is not CONSTANT (-1). */
7039 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
7040 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7041 key
->ps_prolog
.color_interp_vgpr_index
[i
];
7043 /* Get the (i,j) updated by bc_optimize handling. */
7044 interp
[0] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
7046 interp
[1] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
7047 interp_vgpr
+ 1, "");
7048 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
7051 /* Use the absolute location of the input. */
7052 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7054 if (key
->ps_prolog
.states
.color_two_side
) {
7055 face
= LLVMGetParam(func
, face_vgpr
);
7056 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
->i32
, "");
7059 interp_fs_input(ctx
,
7060 key
->ps_prolog
.color_attr_index
[i
],
7061 TGSI_SEMANTIC_COLOR
, i
,
7062 key
->ps_prolog
.num_interp_inputs
,
7063 key
->ps_prolog
.colors_read
, interp_ij
,
7064 prim_mask
, face
, color
);
7067 unsigned chan
= u_bit_scan(&writemask
);
7068 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
7073 /* Tell LLVM to insert WQM instruction sequence when needed. */
7074 if (key
->ps_prolog
.wqm
) {
7075 LLVMAddTargetDependentFunctionAttr(func
,
7076 "amdgpu-ps-wqm-outputs", "");
7079 si_llvm_build_ret(ctx
, ret
);
7083 * Build the pixel shader epilog function. This handles everything that must be
7084 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7086 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
7087 union si_shader_part_key
*key
)
7089 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7090 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7091 LLVMTypeRef params
[16+8*4+3];
7092 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
7093 int last_sgpr
, num_params
= 0, i
;
7094 struct si_ps_exports exp
= {};
7096 /* Declare input SGPRs. */
7097 params
[ctx
->param_rw_buffers
= num_params
++] = ctx
->i64
;
7098 params
[ctx
->param_const_and_shader_buffers
= num_params
++] = ctx
->i64
;
7099 params
[ctx
->param_samplers_and_images
= num_params
++] = ctx
->i64
;
7100 assert(num_params
== SI_PARAM_ALPHA_REF
);
7101 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
7102 last_sgpr
= SI_PARAM_ALPHA_REF
;
7104 /* Declare input VGPRs. */
7105 num_params
= (last_sgpr
+ 1) +
7106 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
7107 key
->ps_epilog
.writes_z
+
7108 key
->ps_epilog
.writes_stencil
+
7109 key
->ps_epilog
.writes_samplemask
;
7111 num_params
= MAX2(num_params
,
7112 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
7114 assert(num_params
<= ARRAY_SIZE(params
));
7116 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
7117 params
[i
] = ctx
->f32
;
7119 /* Create the function. */
7120 si_create_function(ctx
, "ps_epilog", NULL
, 0, params
, num_params
,
7122 /* Disable elimination of unused inputs. */
7123 si_llvm_add_attribute(ctx
->main_fn
,
7124 "InitialPSInputAddr", 0xffffff);
7126 /* Process colors. */
7127 unsigned vgpr
= last_sgpr
+ 1;
7128 unsigned colors_written
= key
->ps_epilog
.colors_written
;
7129 int last_color_export
= -1;
7131 /* Find the last color export. */
7132 if (!key
->ps_epilog
.writes_z
&&
7133 !key
->ps_epilog
.writes_stencil
&&
7134 !key
->ps_epilog
.writes_samplemask
) {
7135 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
7137 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7138 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
7139 /* Just set this if any of the colorbuffers are enabled. */
7141 ((1ull << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
7142 last_color_export
= 0;
7144 for (i
= 0; i
< 8; i
++)
7145 if (colors_written
& (1 << i
) &&
7146 (spi_format
>> (i
* 4)) & 0xf)
7147 last_color_export
= i
;
7151 while (colors_written
) {
7152 LLVMValueRef color
[4];
7153 int mrt
= u_bit_scan(&colors_written
);
7155 for (i
= 0; i
< 4; i
++)
7156 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
7158 si_export_mrt_color(bld_base
, color
, mrt
,
7160 mrt
== last_color_export
, &exp
);
7163 /* Process depth, stencil, samplemask. */
7164 if (key
->ps_epilog
.writes_z
)
7165 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7166 if (key
->ps_epilog
.writes_stencil
)
7167 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7168 if (key
->ps_epilog
.writes_samplemask
)
7169 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7171 if (depth
|| stencil
|| samplemask
)
7172 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
7173 else if (last_color_export
== -1)
7174 si_export_null(bld_base
);
7177 si_emit_ps_exports(ctx
, &exp
);
7180 LLVMBuildRetVoid(gallivm
->builder
);
7184 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7186 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
7187 LLVMTargetMachineRef tm
,
7188 struct si_shader
*shader
,
7189 struct pipe_debug_callback
*debug
)
7191 union si_shader_part_key prolog_key
;
7192 union si_shader_part_key epilog_key
;
7194 /* Get the prolog. */
7195 si_get_ps_prolog_key(shader
, &prolog_key
, true);
7197 /* The prolog is a no-op if these aren't set. */
7198 if (si_need_ps_prolog(&prolog_key
)) {
7200 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
7201 PIPE_SHADER_FRAGMENT
, true,
7202 &prolog_key
, tm
, debug
,
7203 si_build_ps_prolog_function
,
7204 "Fragment Shader Prolog");
7205 if (!shader
->prolog
)
7209 /* Get the epilog. */
7210 si_get_ps_epilog_key(shader
, &epilog_key
);
7213 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
7214 PIPE_SHADER_FRAGMENT
, false,
7215 &epilog_key
, tm
, debug
,
7216 si_build_ps_epilog_function
,
7217 "Fragment Shader Epilog");
7218 if (!shader
->epilog
)
7221 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
7222 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
7223 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
7224 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
7227 /* Set up the enable bits for per-sample shading if needed. */
7228 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
7229 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7230 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7231 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
7232 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7233 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
7235 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
7236 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7237 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7238 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
7239 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7240 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
7242 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
7243 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7244 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7245 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
7246 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7247 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7249 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
7250 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7251 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7252 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
7253 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7254 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7257 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
7258 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
7259 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
7260 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7261 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7264 /* At least one pair of interpolation weights must be enabled. */
7265 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
7266 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7267 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7270 /* The sample mask input is always enabled, because the API shader always
7271 * passes it through to the epilog. Disable it here if it's unused.
7273 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
7274 !shader
->selector
->info
.reads_samplemask
)
7275 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
7280 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
7283 /* SPI barrier management bug:
7284 * Make sure we have at least 4k of LDS in use to avoid the bug.
7285 * It applies to workgroup sizes of more than one wavefront.
7287 if (sscreen
->b
.family
== CHIP_BONAIRE
||
7288 sscreen
->b
.family
== CHIP_KABINI
||
7289 sscreen
->b
.family
== CHIP_MULLINS
)
7290 *lds_size
= MAX2(*lds_size
, 8);
7293 static void si_fix_resource_usage(struct si_screen
*sscreen
,
7294 struct si_shader
*shader
)
7296 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
7298 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
7300 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
7301 si_get_max_workgroup_size(shader
) > 64) {
7302 si_multiwave_lds_size_workaround(sscreen
,
7303 &shader
->config
.lds_size
);
7307 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
7308 struct si_shader
*shader
,
7309 struct pipe_debug_callback
*debug
)
7311 struct si_shader_selector
*sel
= shader
->selector
;
7312 struct si_shader
*mainp
= *si_get_main_shader_part(sel
, &shader
->key
);
7315 /* LS, ES, VS are compiled on demand if the main part hasn't been
7316 * compiled for that stage.
7318 * Vertex shaders are compiled on demand when a vertex fetch
7319 * workaround must be applied.
7321 if (shader
->is_monolithic
) {
7322 /* Monolithic shader (compiled as a whole, has many variants,
7323 * may take a long time to compile).
7325 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
7329 /* The shader consists of 2-3 parts:
7331 * - the middle part is the user shader, it has 1 variant only
7332 * and it was compiled during the creation of the shader
7334 * - the prolog part is inserted at the beginning
7335 * - the epilog part is inserted at the end
7337 * The prolog and epilog have many (but simple) variants.
7340 /* Copy the compiled TGSI shader data over. */
7341 shader
->is_binary_shared
= true;
7342 shader
->binary
= mainp
->binary
;
7343 shader
->config
= mainp
->config
;
7344 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
7345 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
7346 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
7347 memcpy(shader
->info
.vs_output_param_offset
,
7348 mainp
->info
.vs_output_param_offset
,
7349 sizeof(mainp
->info
.vs_output_param_offset
));
7350 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
7351 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
7352 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
7354 /* Select prologs and/or epilogs. */
7355 switch (sel
->type
) {
7356 case PIPE_SHADER_VERTEX
:
7357 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
7360 case PIPE_SHADER_TESS_CTRL
:
7361 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
7364 case PIPE_SHADER_TESS_EVAL
:
7366 case PIPE_SHADER_GEOMETRY
:
7367 if (!si_shader_select_gs_parts(sscreen
, tm
, shader
, debug
))
7370 case PIPE_SHADER_FRAGMENT
:
7371 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
7374 /* Make sure we have at least as many VGPRs as there
7375 * are allocated inputs.
7377 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
7378 shader
->info
.num_input_vgprs
);
7382 /* Update SGPR and VGPR counts. */
7383 if (shader
->prolog
) {
7384 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
7385 shader
->prolog
->config
.num_sgprs
);
7386 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
7387 shader
->prolog
->config
.num_vgprs
);
7389 if (shader
->previous_stage
) {
7390 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
7391 shader
->previous_stage
->config
.num_sgprs
);
7392 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
7393 shader
->previous_stage
->config
.num_vgprs
);
7394 shader
->config
.spilled_sgprs
=
7395 MAX2(shader
->config
.spilled_sgprs
,
7396 shader
->previous_stage
->config
.spilled_sgprs
);
7397 shader
->config
.spilled_vgprs
=
7398 MAX2(shader
->config
.spilled_vgprs
,
7399 shader
->previous_stage
->config
.spilled_vgprs
);
7400 shader
->config
.private_mem_vgprs
=
7401 MAX2(shader
->config
.private_mem_vgprs
,
7402 shader
->previous_stage
->config
.private_mem_vgprs
);
7403 shader
->config
.scratch_bytes_per_wave
=
7404 MAX2(shader
->config
.scratch_bytes_per_wave
,
7405 shader
->previous_stage
->config
.scratch_bytes_per_wave
);
7406 shader
->info
.uses_instanceid
|=
7407 shader
->previous_stage
->info
.uses_instanceid
;
7409 if (shader
->prolog2
) {
7410 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
7411 shader
->prolog2
->config
.num_sgprs
);
7412 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
7413 shader
->prolog2
->config
.num_vgprs
);
7415 if (shader
->epilog
) {
7416 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
7417 shader
->epilog
->config
.num_sgprs
);
7418 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
7419 shader
->epilog
->config
.num_vgprs
);
7423 si_fix_resource_usage(sscreen
, shader
);
7424 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
7428 r
= si_shader_binary_upload(sscreen
, shader
);
7430 fprintf(stderr
, "LLVM failed to upload shader\n");
7437 void si_shader_destroy(struct si_shader
*shader
)
7439 if (shader
->scratch_bo
)
7440 r600_resource_reference(&shader
->scratch_bo
, NULL
);
7442 r600_resource_reference(&shader
->bo
, NULL
);
7444 if (!shader
->is_binary_shared
)
7445 radeon_shader_binary_clean(&shader
->binary
);
7447 free(shader
->shader_log
);