2 * Copyright 2012 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "gallivm/lp_bld_const.h"
26 #include "gallivm/lp_bld_gather.h"
27 #include "gallivm/lp_bld_intr.h"
28 #include "gallivm/lp_bld_logic.h"
29 #include "gallivm/lp_bld_arit.h"
30 #include "gallivm/lp_bld_flow.h"
31 #include "gallivm/lp_bld_misc.h"
32 #include "util/u_memory.h"
33 #include "util/u_string.h"
34 #include "tgsi/tgsi_build.h"
35 #include "tgsi/tgsi_util.h"
36 #include "tgsi/tgsi_dump.h"
38 #include "ac_binary.h"
39 #include "ac_llvm_util.h"
40 #include "ac_exp_param.h"
41 #include "ac_shader_util.h"
42 #include "si_shader_internal.h"
46 #include "compiler/nir/nir.h"
48 static const char *scratch_rsrc_dword0_symbol
=
49 "SCRATCH_RSRC_DWORD0";
51 static const char *scratch_rsrc_dword1_symbol
=
52 "SCRATCH_RSRC_DWORD1";
54 struct si_shader_output_values
56 LLVMValueRef values
[4];
57 unsigned semantic_name
;
58 unsigned semantic_index
;
59 ubyte vertex_stream
[4];
63 * Used to collect types and other info about arguments of the LLVM function
64 * before the function is created.
66 struct si_function_info
{
67 LLVMTypeRef types
[100];
68 LLVMValueRef
*assign
[100];
69 unsigned num_sgpr_params
;
78 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
79 struct si_screen
*sscreen
,
80 struct si_compiler
*compiler
);
82 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
83 struct lp_build_tgsi_context
*bld_base
,
84 struct lp_build_emit_data
*emit_data
);
86 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
89 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
90 union si_shader_part_key
*key
);
91 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
92 union si_shader_part_key
*key
);
93 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
94 union si_shader_part_key
*key
);
95 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
96 union si_shader_part_key
*key
);
98 /* Ideally pass the sample mask input to the PS epilog as v14, which
99 * is its usual location, so that the shader doesn't have to add v_mov.
101 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 14
103 static bool llvm_type_is_64bit(struct si_shader_context
*ctx
,
106 if (type
== ctx
->ac
.i64
|| type
== ctx
->ac
.f64
)
112 static bool is_merged_shader(struct si_shader
*shader
)
114 if (shader
->selector
->screen
->info
.chip_class
<= VI
)
117 return shader
->key
.as_ls
||
119 shader
->selector
->type
== PIPE_SHADER_TESS_CTRL
||
120 shader
->selector
->type
== PIPE_SHADER_GEOMETRY
;
123 static void si_init_function_info(struct si_function_info
*fninfo
)
125 fninfo
->num_params
= 0;
126 fninfo
->num_sgpr_params
= 0;
129 static unsigned add_arg_assign(struct si_function_info
*fninfo
,
130 enum si_arg_regfile regfile
, LLVMTypeRef type
,
131 LLVMValueRef
*assign
)
133 assert(regfile
!= ARG_SGPR
|| fninfo
->num_sgpr_params
== fninfo
->num_params
);
135 unsigned idx
= fninfo
->num_params
++;
136 assert(idx
< ARRAY_SIZE(fninfo
->types
));
138 if (regfile
== ARG_SGPR
)
139 fninfo
->num_sgpr_params
= fninfo
->num_params
;
141 fninfo
->types
[idx
] = type
;
142 fninfo
->assign
[idx
] = assign
;
146 static unsigned add_arg(struct si_function_info
*fninfo
,
147 enum si_arg_regfile regfile
, LLVMTypeRef type
)
149 return add_arg_assign(fninfo
, regfile
, type
, NULL
);
152 static void add_arg_assign_checked(struct si_function_info
*fninfo
,
153 enum si_arg_regfile regfile
, LLVMTypeRef type
,
154 LLVMValueRef
*assign
, unsigned idx
)
156 MAYBE_UNUSED
unsigned actual
= add_arg_assign(fninfo
, regfile
, type
, assign
);
157 assert(actual
== idx
);
160 static void add_arg_checked(struct si_function_info
*fninfo
,
161 enum si_arg_regfile regfile
, LLVMTypeRef type
,
164 add_arg_assign_checked(fninfo
, regfile
, type
, NULL
, idx
);
168 * Returns a unique index for a per-patch semantic name and index. The index
169 * must be less than 32, so that a 32-bit bitmask of used inputs or outputs
172 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name
, unsigned index
)
174 switch (semantic_name
) {
175 case TGSI_SEMANTIC_TESSOUTER
:
177 case TGSI_SEMANTIC_TESSINNER
:
179 case TGSI_SEMANTIC_PATCH
:
184 assert(!"invalid semantic name");
190 * Returns a unique index for a semantic name and index. The index must be
191 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
194 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
,
197 switch (semantic_name
) {
198 case TGSI_SEMANTIC_POSITION
:
200 case TGSI_SEMANTIC_GENERIC
:
201 /* Since some shader stages use the the highest used IO index
202 * to determine the size to allocate for inputs/outputs
203 * (in LDS, tess and GS rings). GENERIC should be placed right
204 * after POSITION to make that size as small as possible.
206 if (index
< SI_MAX_IO_GENERIC
)
209 assert(!"invalid generic index");
211 case TGSI_SEMANTIC_PSIZE
:
212 return SI_MAX_IO_GENERIC
+ 1;
213 case TGSI_SEMANTIC_CLIPDIST
:
215 return SI_MAX_IO_GENERIC
+ 2 + index
;
216 case TGSI_SEMANTIC_FOG
:
217 return SI_MAX_IO_GENERIC
+ 4;
218 case TGSI_SEMANTIC_LAYER
:
219 return SI_MAX_IO_GENERIC
+ 5;
220 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
221 return SI_MAX_IO_GENERIC
+ 6;
222 case TGSI_SEMANTIC_PRIMID
:
223 return SI_MAX_IO_GENERIC
+ 7;
224 case TGSI_SEMANTIC_COLOR
:
226 return SI_MAX_IO_GENERIC
+ 8 + index
;
227 case TGSI_SEMANTIC_BCOLOR
:
229 /* If it's a varying, COLOR and BCOLOR alias. */
231 return SI_MAX_IO_GENERIC
+ 8 + index
;
233 return SI_MAX_IO_GENERIC
+ 10 + index
;
234 case TGSI_SEMANTIC_TEXCOORD
:
236 STATIC_ASSERT(SI_MAX_IO_GENERIC
+ 12 + 8 <= 64);
237 return SI_MAX_IO_GENERIC
+ 12 + index
;
239 assert(!"invalid semantic name");
245 * Get the value of a shader input parameter and extract a bitfield.
247 static LLVMValueRef
unpack_llvm_param(struct si_shader_context
*ctx
,
248 LLVMValueRef value
, unsigned rshift
,
251 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
252 value
= ac_to_integer(&ctx
->ac
, value
);
255 value
= LLVMBuildLShr(ctx
->ac
.builder
, value
,
256 LLVMConstInt(ctx
->i32
, rshift
, 0), "");
258 if (rshift
+ bitwidth
< 32) {
259 unsigned mask
= (1 << bitwidth
) - 1;
260 value
= LLVMBuildAnd(ctx
->ac
.builder
, value
,
261 LLVMConstInt(ctx
->i32
, mask
, 0), "");
267 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
268 unsigned param
, unsigned rshift
,
271 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
, param
);
273 return unpack_llvm_param(ctx
, value
, rshift
, bitwidth
);
276 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
279 case PIPE_SHADER_TESS_CTRL
:
280 return unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 0, 8);
282 case PIPE_SHADER_TESS_EVAL
:
283 return LLVMGetParam(ctx
->main_fn
,
284 ctx
->param_tes_rel_patch_id
);
292 /* Tessellation shaders pass outputs to the next shader using LDS.
294 * LS outputs = TCS inputs
295 * TCS outputs = TES inputs
298 * - TCS inputs for patch 0
299 * - TCS inputs for patch 1
300 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
302 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
303 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
304 * - TCS outputs for patch 1
305 * - Per-patch TCS outputs for patch 1
306 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
307 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
310 * All three shaders VS(LS), TCS, TES share the same LDS space.
314 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
316 return si_unpack_param(ctx
, ctx
->param_vs_state_bits
, 8, 13);
319 static unsigned get_tcs_out_vertex_dw_stride_constant(struct si_shader_context
*ctx
)
321 assert(ctx
->type
== PIPE_SHADER_TESS_CTRL
);
323 if (ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
)
324 return util_last_bit64(ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
) * 4;
326 return util_last_bit64(ctx
->shader
->selector
->outputs_written
) * 4;
329 static LLVMValueRef
get_tcs_out_vertex_dw_stride(struct si_shader_context
*ctx
)
331 unsigned stride
= get_tcs_out_vertex_dw_stride_constant(ctx
);
333 return LLVMConstInt(ctx
->i32
, stride
, 0);
336 static LLVMValueRef
get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
338 if (ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
)
339 return si_unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 0, 13);
341 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
342 unsigned tcs_out_vertices
= info
->properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
];
343 unsigned vertex_dw_stride
= get_tcs_out_vertex_dw_stride_constant(ctx
);
344 unsigned num_patch_outputs
= util_last_bit64(ctx
->shader
->selector
->patch_outputs_written
);
345 unsigned patch_dw_stride
= tcs_out_vertices
* vertex_dw_stride
+
346 num_patch_outputs
* 4;
347 return LLVMConstInt(ctx
->i32
, patch_dw_stride
, 0);
351 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
353 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
355 ctx
->param_tcs_out_lds_offsets
,
361 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
363 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
365 ctx
->param_tcs_out_lds_offsets
,
371 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
373 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
374 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
376 return LLVMBuildMul(ctx
->ac
.builder
, patch_stride
, rel_patch_id
, "");
380 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
382 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
383 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
384 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
386 return LLVMBuildAdd(ctx
->ac
.builder
, patch0_offset
,
387 LLVMBuildMul(ctx
->ac
.builder
, patch_stride
,
393 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
395 LLVMValueRef patch0_patch_data_offset
=
396 get_tcs_out_patch0_patch_data_offset(ctx
);
397 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
398 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
400 return LLVMBuildAdd(ctx
->ac
.builder
, patch0_patch_data_offset
,
401 LLVMBuildMul(ctx
->ac
.builder
, patch_stride
,
406 static LLVMValueRef
get_num_tcs_out_vertices(struct si_shader_context
*ctx
)
408 unsigned tcs_out_vertices
=
409 ctx
->shader
->selector
?
410 ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
] : 0;
412 /* If !tcs_out_vertices, it's either the fixed-func TCS or the TCS epilog. */
413 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&& tcs_out_vertices
)
414 return LLVMConstInt(ctx
->i32
, tcs_out_vertices
, 0);
416 return si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 6, 6);
419 static LLVMValueRef
get_tcs_in_vertex_dw_stride(struct si_shader_context
*ctx
)
424 case PIPE_SHADER_VERTEX
:
425 stride
= util_last_bit64(ctx
->shader
->selector
->outputs_written
);
426 return LLVMConstInt(ctx
->i32
, stride
* 4, 0);
428 case PIPE_SHADER_TESS_CTRL
:
429 if (ctx
->screen
->info
.chip_class
>= GFX9
&&
430 ctx
->shader
->is_monolithic
) {
431 stride
= util_last_bit64(ctx
->shader
->key
.part
.tcs
.ls
->outputs_written
);
432 return LLVMConstInt(ctx
->i32
, stride
* 4, 0);
434 return si_unpack_param(ctx
, ctx
->param_vs_state_bits
, 24, 8);
442 static LLVMValueRef
get_instance_index_for_fetch(
443 struct si_shader_context
*ctx
,
444 unsigned param_start_instance
, LLVMValueRef divisor
)
446 LLVMValueRef result
= ctx
->abi
.instance_id
;
448 /* The division must be done before START_INSTANCE is added. */
449 if (divisor
!= ctx
->i32_1
)
450 result
= LLVMBuildUDiv(ctx
->ac
.builder
, result
, divisor
, "");
452 return LLVMBuildAdd(ctx
->ac
.builder
, result
,
453 LLVMGetParam(ctx
->main_fn
, param_start_instance
), "");
456 /* Bitcast <4 x float> to <2 x double>, extract the component, and convert
458 static LLVMValueRef
extract_double_to_float(struct si_shader_context
*ctx
,
460 unsigned double_index
)
462 LLVMBuilderRef builder
= ctx
->ac
.builder
;
463 LLVMTypeRef f64
= LLVMDoubleTypeInContext(ctx
->ac
.context
);
464 LLVMValueRef dvec2
= LLVMBuildBitCast(builder
, vec4
,
465 LLVMVectorType(f64
, 2), "");
466 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, double_index
, 0);
467 LLVMValueRef value
= LLVMBuildExtractElement(builder
, dvec2
, index
, "");
468 return LLVMBuildFPTrunc(builder
, value
, ctx
->f32
, "");
471 static LLVMValueRef
unpack_sint16(struct si_shader_context
*ctx
,
472 LLVMValueRef i32
, unsigned index
)
477 return LLVMBuildAShr(ctx
->ac
.builder
, i32
,
478 LLVMConstInt(ctx
->i32
, 16, 0), "");
480 return LLVMBuildSExt(ctx
->ac
.builder
,
481 LLVMBuildTrunc(ctx
->ac
.builder
, i32
,
486 void si_llvm_load_input_vs(
487 struct si_shader_context
*ctx
,
488 unsigned input_index
,
491 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
492 unsigned vs_blit_property
= info
->properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
494 if (vs_blit_property
) {
495 LLVMValueRef vertex_id
= ctx
->abi
.vertex_id
;
496 LLVMValueRef sel_x1
= LLVMBuildICmp(ctx
->ac
.builder
,
497 LLVMIntULE
, vertex_id
,
499 /* Use LLVMIntNE, because we have 3 vertices and only
500 * the middle one should use y2.
502 LLVMValueRef sel_y1
= LLVMBuildICmp(ctx
->ac
.builder
,
503 LLVMIntNE
, vertex_id
,
506 if (input_index
== 0) {
508 LLVMValueRef x1y1
= LLVMGetParam(ctx
->main_fn
,
509 ctx
->param_vs_blit_inputs
);
510 LLVMValueRef x2y2
= LLVMGetParam(ctx
->main_fn
,
511 ctx
->param_vs_blit_inputs
+ 1);
513 LLVMValueRef x1
= unpack_sint16(ctx
, x1y1
, 0);
514 LLVMValueRef y1
= unpack_sint16(ctx
, x1y1
, 1);
515 LLVMValueRef x2
= unpack_sint16(ctx
, x2y2
, 0);
516 LLVMValueRef y2
= unpack_sint16(ctx
, x2y2
, 1);
518 LLVMValueRef x
= LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
520 LLVMValueRef y
= LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
523 out
[0] = LLVMBuildSIToFP(ctx
->ac
.builder
, x
, ctx
->f32
, "");
524 out
[1] = LLVMBuildSIToFP(ctx
->ac
.builder
, y
, ctx
->f32
, "");
525 out
[2] = LLVMGetParam(ctx
->main_fn
,
526 ctx
->param_vs_blit_inputs
+ 2);
527 out
[3] = ctx
->ac
.f32_1
;
531 /* Color or texture coordinates: */
532 assert(input_index
== 1);
534 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
535 for (int i
= 0; i
< 4; i
++) {
536 out
[i
] = LLVMGetParam(ctx
->main_fn
,
537 ctx
->param_vs_blit_inputs
+ 3 + i
);
540 assert(vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
);
541 LLVMValueRef x1
= LLVMGetParam(ctx
->main_fn
,
542 ctx
->param_vs_blit_inputs
+ 3);
543 LLVMValueRef y1
= LLVMGetParam(ctx
->main_fn
,
544 ctx
->param_vs_blit_inputs
+ 4);
545 LLVMValueRef x2
= LLVMGetParam(ctx
->main_fn
,
546 ctx
->param_vs_blit_inputs
+ 5);
547 LLVMValueRef y2
= LLVMGetParam(ctx
->main_fn
,
548 ctx
->param_vs_blit_inputs
+ 6);
550 out
[0] = LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
552 out
[1] = LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
554 out
[2] = LLVMGetParam(ctx
->main_fn
,
555 ctx
->param_vs_blit_inputs
+ 7);
556 out
[3] = LLVMGetParam(ctx
->main_fn
,
557 ctx
->param_vs_blit_inputs
+ 8);
564 unsigned num_fetches
;
565 unsigned fetch_stride
;
566 unsigned num_channels
;
568 LLVMValueRef t_list_ptr
;
569 LLVMValueRef t_offset
;
571 LLVMValueRef vertex_index
;
572 LLVMValueRef input
[3];
574 /* Load the T list */
575 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_vertex_buffers
);
577 t_offset
= LLVMConstInt(ctx
->i32
, input_index
, 0);
579 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
581 vertex_index
= LLVMGetParam(ctx
->main_fn
,
582 ctx
->param_vertex_index0
+
585 fix_fetch
= ctx
->shader
->key
.mono
.vs_fix_fetch
[input_index
];
587 /* Do multiple loads for special formats. */
589 case SI_FIX_FETCH_RGB_64_FLOAT
:
590 num_fetches
= 3; /* 3 2-dword loads */
594 case SI_FIX_FETCH_RGBA_64_FLOAT
:
595 num_fetches
= 2; /* 2 4-dword loads */
599 case SI_FIX_FETCH_RGB_8
:
600 case SI_FIX_FETCH_RGB_8_INT
:
605 case SI_FIX_FETCH_RGB_16
:
606 case SI_FIX_FETCH_RGB_16_INT
:
614 num_channels
= util_last_bit(info
->input_usage_mask
[input_index
]);
617 for (unsigned i
= 0; i
< num_fetches
; i
++) {
618 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, fetch_stride
* i
, 0);
620 input
[i
] = ac_build_buffer_load_format(&ctx
->ac
, t_list
,
621 vertex_index
, voffset
,
622 num_channels
, false, true);
623 input
[i
] = ac_build_expand_to_vec4(&ctx
->ac
, input
[i
], num_channels
);
626 /* Break up the vec4 into individual components */
627 for (chan
= 0; chan
< 4; chan
++) {
628 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
629 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
630 input
[0], llvm_chan
, "");
634 case SI_FIX_FETCH_A2_SNORM
:
635 case SI_FIX_FETCH_A2_SSCALED
:
636 case SI_FIX_FETCH_A2_SINT
: {
637 /* The hardware returns an unsigned value; convert it to a
640 LLVMValueRef tmp
= out
[3];
641 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
643 /* First, recover the sign-extended signed integer value. */
644 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
645 tmp
= LLVMBuildFPToUI(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
647 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
649 /* For the integer-like cases, do a natural sign extension.
651 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
652 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
655 tmp
= LLVMBuildShl(ctx
->ac
.builder
, tmp
,
656 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
657 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
658 tmp
= LLVMBuildAShr(ctx
->ac
.builder
, tmp
, c30
, "");
660 /* Convert back to the right type. */
661 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
663 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
664 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
665 clamp
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealULT
, tmp
, neg_one
, "");
666 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, clamp
, neg_one
, tmp
, "");
667 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
668 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
674 case SI_FIX_FETCH_RGBA_32_UNORM
:
675 case SI_FIX_FETCH_RGBX_32_UNORM
:
676 for (chan
= 0; chan
< 4; chan
++) {
677 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
678 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
679 out
[chan
], ctx
->f32
, "");
680 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
681 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
683 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
684 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
685 out
[3] = LLVMConstReal(ctx
->f32
, 1);
687 case SI_FIX_FETCH_RGBA_32_SNORM
:
688 case SI_FIX_FETCH_RGBX_32_SNORM
:
689 case SI_FIX_FETCH_RGBA_32_FIXED
:
690 case SI_FIX_FETCH_RGBX_32_FIXED
: {
692 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
693 scale
= 1.0 / 0x10000;
695 scale
= 1.0 / INT_MAX
;
697 for (chan
= 0; chan
< 4; chan
++) {
698 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
699 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
700 out
[chan
], ctx
->f32
, "");
701 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
702 LLVMConstReal(ctx
->f32
, scale
), "");
704 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
705 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
706 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
707 out
[3] = LLVMConstReal(ctx
->f32
, 1);
710 case SI_FIX_FETCH_RGBA_32_USCALED
:
711 for (chan
= 0; chan
< 4; chan
++) {
712 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
713 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
714 out
[chan
], ctx
->f32
, "");
717 case SI_FIX_FETCH_RGBA_32_SSCALED
:
718 for (chan
= 0; chan
< 4; chan
++) {
719 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
720 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
721 out
[chan
], ctx
->f32
, "");
724 case SI_FIX_FETCH_RG_64_FLOAT
:
725 for (chan
= 0; chan
< 2; chan
++)
726 out
[chan
] = extract_double_to_float(ctx
, input
[0], chan
);
728 out
[2] = LLVMConstReal(ctx
->f32
, 0);
729 out
[3] = LLVMConstReal(ctx
->f32
, 1);
731 case SI_FIX_FETCH_RGB_64_FLOAT
:
732 for (chan
= 0; chan
< 3; chan
++)
733 out
[chan
] = extract_double_to_float(ctx
, input
[chan
], 0);
735 out
[3] = LLVMConstReal(ctx
->f32
, 1);
737 case SI_FIX_FETCH_RGBA_64_FLOAT
:
738 for (chan
= 0; chan
< 4; chan
++) {
739 out
[chan
] = extract_double_to_float(ctx
, input
[chan
/ 2],
743 case SI_FIX_FETCH_RGB_8
:
744 case SI_FIX_FETCH_RGB_8_INT
:
745 case SI_FIX_FETCH_RGB_16
:
746 case SI_FIX_FETCH_RGB_16_INT
:
747 for (chan
= 0; chan
< 3; chan
++) {
748 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
752 if (fix_fetch
== SI_FIX_FETCH_RGB_8
||
753 fix_fetch
== SI_FIX_FETCH_RGB_16
) {
754 out
[3] = LLVMConstReal(ctx
->f32
, 1);
756 out
[3] = ac_to_float(&ctx
->ac
, ctx
->i32_1
);
762 static void declare_input_vs(
763 struct si_shader_context
*ctx
,
764 unsigned input_index
,
765 const struct tgsi_full_declaration
*decl
,
768 si_llvm_load_input_vs(ctx
, input_index
, out
);
771 static LLVMValueRef
get_primitive_id(struct si_shader_context
*ctx
,
778 case PIPE_SHADER_VERTEX
:
779 return LLVMGetParam(ctx
->main_fn
,
780 ctx
->param_vs_prim_id
);
781 case PIPE_SHADER_TESS_CTRL
:
782 return ctx
->abi
.tcs_patch_id
;
783 case PIPE_SHADER_TESS_EVAL
:
784 return ctx
->abi
.tes_patch_id
;
785 case PIPE_SHADER_GEOMETRY
:
786 return ctx
->abi
.gs_prim_id
;
794 * Return the value of tgsi_ind_register for indexing.
795 * This is the indirect index with the constant offset added to it.
797 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
798 const struct tgsi_ind_register
*ind
,
804 if (ind
->File
== TGSI_FILE_ADDRESS
) {
805 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
806 result
= LLVMBuildLoad(ctx
->ac
.builder
, result
, "");
808 struct tgsi_full_src_register src
= {};
810 src
.Register
.File
= ind
->File
;
811 src
.Register
.Index
= ind
->Index
;
813 /* Set the second index to 0 for constants. */
814 if (ind
->File
== TGSI_FILE_CONSTANT
)
815 src
.Register
.Dimension
= 1;
817 result
= ctx
->bld_base
.emit_fetch_funcs
[ind
->File
](&ctx
->bld_base
, &src
,
820 result
= ac_to_integer(&ctx
->ac
, result
);
824 result
= LLVMBuildMul(ctx
->ac
.builder
, result
,
825 LLVMConstInt(ctx
->i32
, addr_mul
, 0), "");
826 result
= LLVMBuildAdd(ctx
->ac
.builder
, result
,
827 LLVMConstInt(ctx
->i32
, rel_index
, 0), "");
832 * Like si_get_indirect_index, but restricts the return value to a (possibly
833 * undefined) value inside [0..num).
835 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
836 const struct tgsi_ind_register
*ind
,
837 int rel_index
, unsigned num
)
839 LLVMValueRef result
= si_get_indirect_index(ctx
, ind
, 1, rel_index
);
841 return si_llvm_bound_index(ctx
, result
, num
);
844 static LLVMValueRef
get_dw_address_from_generic_indices(struct si_shader_context
*ctx
,
845 LLVMValueRef vertex_dw_stride
,
846 LLVMValueRef base_addr
,
847 LLVMValueRef vertex_index
,
848 LLVMValueRef param_index
,
849 unsigned input_index
,
854 if (vertex_dw_stride
) {
855 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
856 LLVMBuildMul(ctx
->ac
.builder
, vertex_index
,
857 vertex_dw_stride
, ""), "");
861 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
862 LLVMBuildMul(ctx
->ac
.builder
, param_index
,
863 LLVMConstInt(ctx
->i32
, 4, 0), ""), "");
866 int param
= is_patch
?
867 si_shader_io_get_unique_index_patch(name
[input_index
],
868 index
[input_index
]) :
869 si_shader_io_get_unique_index(name
[input_index
],
870 index
[input_index
], false);
872 /* Add the base address of the element. */
873 return LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
874 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
878 * Calculate a dword address given an input or output register and a stride.
880 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
881 const struct tgsi_full_dst_register
*dst
,
882 const struct tgsi_full_src_register
*src
,
883 LLVMValueRef vertex_dw_stride
,
884 LLVMValueRef base_addr
)
886 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
887 ubyte
*name
, *index
, *array_first
;
889 struct tgsi_full_dst_register reg
;
890 LLVMValueRef vertex_index
= NULL
;
891 LLVMValueRef ind_index
= NULL
;
893 /* Set the register description. The address computation is the same
894 * for sources and destinations. */
896 reg
.Register
.File
= src
->Register
.File
;
897 reg
.Register
.Index
= src
->Register
.Index
;
898 reg
.Register
.Indirect
= src
->Register
.Indirect
;
899 reg
.Register
.Dimension
= src
->Register
.Dimension
;
900 reg
.Indirect
= src
->Indirect
;
901 reg
.Dimension
= src
->Dimension
;
902 reg
.DimIndirect
= src
->DimIndirect
;
906 /* If the register is 2-dimensional (e.g. an array of vertices
907 * in a primitive), calculate the base address of the vertex. */
908 if (reg
.Register
.Dimension
) {
909 if (reg
.Dimension
.Indirect
)
910 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
911 1, reg
.Dimension
.Index
);
913 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
916 /* Get information about the register. */
917 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
918 name
= info
->input_semantic_name
;
919 index
= info
->input_semantic_index
;
920 array_first
= info
->input_array_first
;
921 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
922 name
= info
->output_semantic_name
;
923 index
= info
->output_semantic_index
;
924 array_first
= info
->output_array_first
;
930 if (reg
.Register
.Indirect
) {
931 /* Add the relative address of the element. */
932 if (reg
.Indirect
.ArrayID
)
933 input_index
= array_first
[reg
.Indirect
.ArrayID
];
935 input_index
= reg
.Register
.Index
;
937 ind_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
938 1, reg
.Register
.Index
- input_index
);
940 input_index
= reg
.Register
.Index
;
943 return get_dw_address_from_generic_indices(ctx
, vertex_dw_stride
,
944 base_addr
, vertex_index
,
945 ind_index
, input_index
,
947 !reg
.Register
.Dimension
);
950 /* The offchip buffer layout for TCS->TES is
952 * - attribute 0 of patch 0 vertex 0
953 * - attribute 0 of patch 0 vertex 1
954 * - attribute 0 of patch 0 vertex 2
956 * - attribute 0 of patch 1 vertex 0
957 * - attribute 0 of patch 1 vertex 1
959 * - attribute 1 of patch 0 vertex 0
960 * - attribute 1 of patch 0 vertex 1
962 * - per patch attribute 0 of patch 0
963 * - per patch attribute 0 of patch 1
966 * Note that every attribute has 4 components.
968 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
969 LLVMValueRef rel_patch_id
,
970 LLVMValueRef vertex_index
,
971 LLVMValueRef param_index
)
973 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
974 LLVMValueRef param_stride
, constant16
;
976 vertices_per_patch
= get_num_tcs_out_vertices(ctx
);
977 num_patches
= si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 0, 6);
978 total_vertices
= LLVMBuildMul(ctx
->ac
.builder
, vertices_per_patch
,
981 constant16
= LLVMConstInt(ctx
->i32
, 16, 0);
983 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
984 vertices_per_patch
, "");
986 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
989 param_stride
= total_vertices
;
991 base_addr
= rel_patch_id
;
992 param_stride
= num_patches
;
995 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
996 LLVMBuildMul(ctx
->ac
.builder
, param_index
,
997 param_stride
, ""), "");
999 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, base_addr
, constant16
, "");
1001 if (!vertex_index
) {
1002 LLVMValueRef patch_data_offset
=
1003 si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 12, 20);
1005 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
1006 patch_data_offset
, "");
1011 /* This is a generic helper that can be shared by the NIR and TGSI backends */
1012 static LLVMValueRef
get_tcs_tes_buffer_address_from_generic_indices(
1013 struct si_shader_context
*ctx
,
1014 LLVMValueRef vertex_index
,
1015 LLVMValueRef param_index
,
1016 unsigned param_base
,
1021 unsigned param_index_base
;
1023 param_index_base
= is_patch
?
1024 si_shader_io_get_unique_index_patch(name
[param_base
], index
[param_base
]) :
1025 si_shader_io_get_unique_index(name
[param_base
], index
[param_base
], false);
1028 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1029 LLVMConstInt(ctx
->i32
, param_index_base
, 0),
1032 param_index
= LLVMConstInt(ctx
->i32
, param_index_base
, 0);
1035 return get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
),
1036 vertex_index
, param_index
);
1039 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
1040 struct si_shader_context
*ctx
,
1041 const struct tgsi_full_dst_register
*dst
,
1042 const struct tgsi_full_src_register
*src
)
1044 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1045 ubyte
*name
, *index
, *array_first
;
1046 struct tgsi_full_src_register reg
;
1047 LLVMValueRef vertex_index
= NULL
;
1048 LLVMValueRef param_index
= NULL
;
1049 unsigned param_base
;
1051 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
1053 if (reg
.Register
.Dimension
) {
1055 if (reg
.Dimension
.Indirect
)
1056 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
1057 1, reg
.Dimension
.Index
);
1059 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
1062 /* Get information about the register. */
1063 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
1064 name
= info
->input_semantic_name
;
1065 index
= info
->input_semantic_index
;
1066 array_first
= info
->input_array_first
;
1067 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
1068 name
= info
->output_semantic_name
;
1069 index
= info
->output_semantic_index
;
1070 array_first
= info
->output_array_first
;
1076 if (reg
.Register
.Indirect
) {
1077 if (reg
.Indirect
.ArrayID
)
1078 param_base
= array_first
[reg
.Indirect
.ArrayID
];
1080 param_base
= reg
.Register
.Index
;
1082 param_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
1083 1, reg
.Register
.Index
- param_base
);
1086 param_base
= reg
.Register
.Index
;
1089 return get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1090 param_index
, param_base
,
1091 name
, index
, !reg
.Register
.Dimension
);
1094 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
1095 LLVMTypeRef type
, unsigned swizzle
,
1096 LLVMValueRef buffer
, LLVMValueRef offset
,
1097 LLVMValueRef base
, bool can_speculate
)
1099 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1100 LLVMValueRef value
, value2
;
1101 LLVMTypeRef vec_type
= LLVMVectorType(type
, 4);
1103 if (swizzle
== ~0) {
1104 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1105 0, 1, 0, can_speculate
, false);
1107 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1110 if (!llvm_type_is_64bit(ctx
, type
)) {
1111 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1112 0, 1, 0, can_speculate
, false);
1114 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1115 return LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1116 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
1119 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1120 swizzle
* 4, 1, 0, can_speculate
, false);
1122 value2
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1123 swizzle
* 4 + 4, 1, 0, can_speculate
, false);
1125 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1131 * \param type output value type
1132 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
1133 * \param dw_addr address in dwords
1135 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
1136 LLVMTypeRef type
, unsigned swizzle
,
1137 LLVMValueRef dw_addr
)
1139 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1142 if (swizzle
== ~0) {
1143 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1145 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
1146 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
1148 return lp_build_gather_values(&ctx
->gallivm
, values
,
1152 /* Split 64-bit loads. */
1153 if (llvm_type_is_64bit(ctx
, type
)) {
1154 LLVMValueRef lo
, hi
;
1156 lo
= lds_load(bld_base
, ctx
->i32
, swizzle
, dw_addr
);
1157 hi
= lds_load(bld_base
, ctx
->i32
, swizzle
+ 1, dw_addr
);
1158 return si_llvm_emit_fetch_64bit(bld_base
, type
, lo
, hi
);
1161 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
1162 LLVMConstInt(ctx
->i32
, swizzle
, 0));
1164 value
= ac_lds_load(&ctx
->ac
, dw_addr
);
1166 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1172 * \param swizzle offset (typically 0..3)
1173 * \param dw_addr address in dwords
1174 * \param value value to store
1176 static void lds_store(struct si_shader_context
*ctx
,
1177 unsigned dw_offset_imm
, LLVMValueRef dw_addr
,
1180 dw_addr
= lp_build_add(&ctx
->bld_base
.uint_bld
, dw_addr
,
1181 LLVMConstInt(ctx
->i32
, dw_offset_imm
, 0));
1183 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
1188 TESS_OFFCHIP_RING_TCS
,
1189 TESS_OFFCHIP_RING_TES
,
1192 static LLVMValueRef
get_tess_ring_descriptor(struct si_shader_context
*ctx
,
1193 enum si_tess_ring ring
)
1195 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1196 unsigned param
= ring
== TESS_OFFCHIP_RING_TES
? ctx
->param_tes_offchip_addr
:
1197 ctx
->param_tcs_out_lds_layout
;
1198 LLVMValueRef addr
= LLVMGetParam(ctx
->main_fn
, param
);
1200 /* TCS only receives high 13 bits of the address. */
1201 if (ring
== TESS_OFFCHIP_RING_TCS
|| ring
== TCS_FACTOR_RING
) {
1202 addr
= LLVMBuildAnd(builder
, addr
,
1203 LLVMConstInt(ctx
->i32
, 0xfff80000, 0), "");
1206 if (ring
== TCS_FACTOR_RING
) {
1207 unsigned tf_offset
= ctx
->screen
->tess_offchip_ring_size
;
1208 addr
= LLVMBuildAdd(builder
, addr
,
1209 LLVMConstInt(ctx
->i32
, tf_offset
, 0), "");
1212 LLVMValueRef desc
[4];
1214 desc
[1] = LLVMConstInt(ctx
->i32
,
1215 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
1216 desc
[2] = LLVMConstInt(ctx
->i32
, 0xffffffff, 0);
1217 desc
[3] = LLVMConstInt(ctx
->i32
,
1218 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1219 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1220 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1221 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1222 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1223 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0);
1225 return ac_build_gather_values(&ctx
->ac
, desc
, 4);
1228 static LLVMValueRef
fetch_input_tcs(
1229 struct lp_build_tgsi_context
*bld_base
,
1230 const struct tgsi_full_src_register
*reg
,
1231 enum tgsi_opcode_type type
, unsigned swizzle
)
1233 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1234 LLVMValueRef dw_addr
, stride
;
1236 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1237 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1238 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1240 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1243 static LLVMValueRef
si_nir_load_tcs_varyings(struct ac_shader_abi
*abi
,
1245 LLVMValueRef vertex_index
,
1246 LLVMValueRef param_index
,
1247 unsigned const_index
,
1249 unsigned driver_location
,
1251 unsigned num_components
,
1256 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1257 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1258 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1259 LLVMValueRef dw_addr
, stride
;
1261 driver_location
= driver_location
/ 4;
1264 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1265 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1269 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1271 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1272 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1277 /* Add the constant index to the indirect index */
1278 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1279 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1281 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1287 names
= info
->input_semantic_name
;
1288 indices
= info
->input_semantic_index
;
1290 names
= info
->output_semantic_name
;
1291 indices
= info
->output_semantic_index
;
1294 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1295 vertex_index
, param_index
,
1300 LLVMValueRef value
[4];
1301 for (unsigned i
= 0; i
< num_components
; i
++) {
1302 unsigned offset
= i
;
1303 if (llvm_type_is_64bit(ctx
, type
))
1306 offset
+= component
;
1307 value
[i
+ component
] = lds_load(bld_base
, type
, offset
, dw_addr
);
1310 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1313 static LLVMValueRef
fetch_output_tcs(
1314 struct lp_build_tgsi_context
*bld_base
,
1315 const struct tgsi_full_src_register
*reg
,
1316 enum tgsi_opcode_type type
, unsigned swizzle
)
1318 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1319 LLVMValueRef dw_addr
, stride
;
1321 if (reg
->Register
.Dimension
) {
1322 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1323 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1324 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1326 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1327 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
1330 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1333 static LLVMValueRef
fetch_input_tes(
1334 struct lp_build_tgsi_context
*bld_base
,
1335 const struct tgsi_full_src_register
*reg
,
1336 enum tgsi_opcode_type type
, unsigned swizzle
)
1338 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1339 LLVMValueRef base
, addr
;
1341 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1342 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
1344 return buffer_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
,
1345 ctx
->tess_offchip_ring
, base
, addr
, true);
1348 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
1350 LLVMValueRef vertex_index
,
1351 LLVMValueRef param_index
,
1352 unsigned const_index
,
1354 unsigned driver_location
,
1356 unsigned num_components
,
1361 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1362 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1363 LLVMValueRef base
, addr
;
1365 driver_location
= driver_location
/ 4;
1367 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1370 /* Add the constant index to the indirect index */
1371 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1372 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1374 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1377 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1378 param_index
, driver_location
,
1379 info
->input_semantic_name
,
1380 info
->input_semantic_index
,
1383 /* TODO: This will generate rather ordinary llvm code, although it
1384 * should be easy for the optimiser to fix up. In future we might want
1385 * to refactor buffer_load(), but for now this maximises code sharing
1386 * between the NIR and TGSI backends.
1388 LLVMValueRef value
[4];
1389 for (unsigned i
= 0; i
< num_components
; i
++) {
1390 unsigned offset
= i
;
1391 if (llvm_type_is_64bit(ctx
, type
))
1394 offset
+= component
;
1395 value
[i
+ component
] = buffer_load(&ctx
->bld_base
, type
, offset
,
1396 ctx
->tess_offchip_ring
, base
, addr
, true);
1399 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1402 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1403 const struct tgsi_full_instruction
*inst
,
1404 const struct tgsi_opcode_info
*info
,
1406 LLVMValueRef dst
[4])
1408 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1409 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1410 const struct tgsi_shader_info
*sh_info
= &ctx
->shader
->selector
->info
;
1411 unsigned chan_index
;
1412 LLVMValueRef dw_addr
, stride
;
1413 LLVMValueRef buffer
, base
, buf_addr
;
1414 LLVMValueRef values
[4];
1415 bool skip_lds_store
;
1416 bool is_tess_factor
= false, is_tess_inner
= false;
1418 /* Only handle per-patch and per-vertex outputs here.
1419 * Vectors will be lowered to scalars and this function will be called again.
1421 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1422 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1423 si_llvm_emit_store(bld_base
, inst
, info
, index
, dst
);
1427 if (reg
->Register
.Dimension
) {
1428 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1429 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1430 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1431 skip_lds_store
= !sh_info
->reads_pervertex_outputs
;
1433 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1434 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1435 skip_lds_store
= !sh_info
->reads_perpatch_outputs
;
1437 if (!reg
->Register
.Indirect
) {
1438 int name
= sh_info
->output_semantic_name
[reg
->Register
.Index
];
1440 /* Always write tess factors into LDS for the TCS epilog. */
1441 if (name
== TGSI_SEMANTIC_TESSINNER
||
1442 name
== TGSI_SEMANTIC_TESSOUTER
) {
1443 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1444 skip_lds_store
= !sh_info
->reads_tessfactor_outputs
&&
1445 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1446 is_tess_factor
= true;
1447 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1452 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1454 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1455 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1457 uint32_t writemask
= reg
->Register
.WriteMask
;
1459 chan_index
= u_bit_scan(&writemask
);
1460 LLVMValueRef value
= dst
[chan_index
];
1462 if (inst
->Instruction
.Saturate
)
1463 value
= ac_build_clamp(&ctx
->ac
, value
);
1465 /* Skip LDS stores if there is no LDS read of this output. */
1466 if (!skip_lds_store
)
1467 lds_store(ctx
, chan_index
, dw_addr
, value
);
1469 value
= ac_to_integer(&ctx
->ac
, value
);
1470 values
[chan_index
] = value
;
1472 if (reg
->Register
.WriteMask
!= 0xF && !is_tess_factor
) {
1473 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1475 4 * chan_index
, 1, 0, true, false);
1478 /* Write tess factors into VGPRs for the epilog. */
1479 if (is_tess_factor
&&
1480 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1481 if (!is_tess_inner
) {
1482 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1483 ctx
->invoc0_tess_factors
[chan_index
]);
1484 } else if (chan_index
< 2) {
1485 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1486 ctx
->invoc0_tess_factors
[4 + chan_index
]);
1491 if (reg
->Register
.WriteMask
== 0xF && !is_tess_factor
) {
1492 LLVMValueRef value
= lp_build_gather_values(&ctx
->gallivm
,
1494 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buf_addr
,
1495 base
, 0, 1, 0, true, false);
1499 static void si_nir_store_output_tcs(struct ac_shader_abi
*abi
,
1500 const struct nir_variable
*var
,
1501 LLVMValueRef vertex_index
,
1502 LLVMValueRef param_index
,
1503 unsigned const_index
,
1507 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1508 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1509 const unsigned component
= var
->data
.location_frac
;
1510 const bool is_patch
= var
->data
.patch
;
1511 unsigned driver_location
= var
->data
.driver_location
;
1512 LLVMValueRef dw_addr
, stride
;
1513 LLVMValueRef buffer
, base
, addr
;
1514 LLVMValueRef values
[4];
1515 bool skip_lds_store
;
1516 bool is_tess_factor
= false, is_tess_inner
= false;
1518 driver_location
= driver_location
/ 4;
1521 /* Add the constant index to the indirect index */
1522 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1523 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1525 if (const_index
!= 0)
1526 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1530 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1531 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1532 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1533 vertex_index
, param_index
,
1535 info
->output_semantic_name
,
1536 info
->output_semantic_index
,
1539 skip_lds_store
= !info
->reads_pervertex_outputs
;
1541 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1542 dw_addr
= get_dw_address_from_generic_indices(ctx
, NULL
, dw_addr
,
1543 vertex_index
, param_index
,
1545 info
->output_semantic_name
,
1546 info
->output_semantic_index
,
1549 skip_lds_store
= !info
->reads_perpatch_outputs
;
1552 int name
= info
->output_semantic_name
[driver_location
];
1554 /* Always write tess factors into LDS for the TCS epilog. */
1555 if (name
== TGSI_SEMANTIC_TESSINNER
||
1556 name
== TGSI_SEMANTIC_TESSOUTER
) {
1557 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1558 skip_lds_store
= !info
->reads_tessfactor_outputs
&&
1559 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1560 is_tess_factor
= true;
1561 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1566 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1568 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1570 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1571 param_index
, driver_location
,
1572 info
->output_semantic_name
,
1573 info
->output_semantic_index
,
1576 for (unsigned chan
= 0; chan
< 4; chan
++) {
1577 if (!(writemask
& (1 << chan
)))
1579 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
1581 /* Skip LDS stores if there is no LDS read of this output. */
1582 if (!skip_lds_store
)
1583 lds_store(ctx
, chan
, dw_addr
, value
);
1585 value
= ac_to_integer(&ctx
->ac
, value
);
1586 values
[chan
] = value
;
1588 if (writemask
!= 0xF && !is_tess_factor
) {
1589 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1591 4 * chan
, 1, 0, true, false);
1594 /* Write tess factors into VGPRs for the epilog. */
1595 if (is_tess_factor
&&
1596 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1597 if (!is_tess_inner
) {
1598 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1599 ctx
->invoc0_tess_factors
[chan
]);
1600 } else if (chan
< 2) {
1601 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1602 ctx
->invoc0_tess_factors
[4 + chan
]);
1607 if (writemask
== 0xF && !is_tess_factor
) {
1608 LLVMValueRef value
= lp_build_gather_values(&ctx
->gallivm
,
1610 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, addr
,
1611 base
, 0, 1, 0, true, false);
1615 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
1616 unsigned input_index
,
1617 unsigned vtx_offset_param
,
1621 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1622 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1623 struct si_shader
*shader
= ctx
->shader
;
1624 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
1625 LLVMValueRef vtx_offset
, soffset
;
1626 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1627 unsigned semantic_name
= info
->input_semantic_name
[input_index
];
1628 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1632 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
, false);
1634 /* GFX9 has the ESGS ring in LDS. */
1635 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1636 unsigned index
= vtx_offset_param
;
1638 switch (index
/ 2) {
1640 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx01_offset
,
1641 index
% 2 ? 16 : 0, 16);
1644 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx23_offset
,
1645 index
% 2 ? 16 : 0, 16);
1648 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx45_offset
,
1649 index
% 2 ? 16 : 0, 16);
1656 vtx_offset
= LLVMBuildAdd(ctx
->ac
.builder
, vtx_offset
,
1657 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
1658 return lds_load(bld_base
, type
, swizzle
, vtx_offset
);
1661 /* GFX6: input load from the ESGS ring in memory. */
1662 if (swizzle
== ~0) {
1663 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1665 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1666 values
[chan
] = si_llvm_load_input_gs(abi
, input_index
, vtx_offset_param
,
1669 return lp_build_gather_values(&ctx
->gallivm
, values
,
1673 /* Get the vertex offset parameter on GFX6. */
1674 LLVMValueRef gs_vtx_offset
= ctx
->gs_vtx_offset
[vtx_offset_param
];
1676 vtx_offset
= lp_build_mul_imm(uint
, gs_vtx_offset
, 4);
1678 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
) * 256, 0);
1680 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1, ctx
->i32_0
,
1681 vtx_offset
, soffset
, 0, 1, 0, true, false);
1682 if (llvm_type_is_64bit(ctx
, type
)) {
1683 LLVMValueRef value2
;
1684 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
+ 1) * 256, 0);
1686 value2
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1,
1687 ctx
->i32_0
, vtx_offset
, soffset
,
1688 0, 1, 0, true, false);
1689 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1691 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1694 static LLVMValueRef
si_nir_load_input_gs(struct ac_shader_abi
*abi
,
1696 unsigned driver_location
,
1698 unsigned num_components
,
1699 unsigned vertex_index
,
1700 unsigned const_index
,
1703 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1705 LLVMValueRef value
[4];
1706 for (unsigned i
= 0; i
< num_components
; i
++) {
1707 unsigned offset
= i
;
1708 if (llvm_type_is_64bit(ctx
, type
))
1711 offset
+= component
;
1712 value
[i
+ component
] = si_llvm_load_input_gs(&ctx
->abi
, driver_location
/ 4,
1713 vertex_index
, type
, offset
);
1716 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1719 static LLVMValueRef
fetch_input_gs(
1720 struct lp_build_tgsi_context
*bld_base
,
1721 const struct tgsi_full_src_register
*reg
,
1722 enum tgsi_opcode_type type
,
1725 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1726 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1728 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1729 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1730 return get_primitive_id(ctx
, swizzle
);
1732 if (!reg
->Register
.Dimension
)
1735 return si_llvm_load_input_gs(&ctx
->abi
, reg
->Register
.Index
,
1736 reg
->Dimension
.Index
,
1737 tgsi2llvmtype(bld_base
, type
),
1741 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1743 switch (interpolate
) {
1744 case TGSI_INTERPOLATE_CONSTANT
:
1747 case TGSI_INTERPOLATE_LINEAR
:
1748 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1749 return SI_PARAM_LINEAR_SAMPLE
;
1750 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1751 return SI_PARAM_LINEAR_CENTROID
;
1753 return SI_PARAM_LINEAR_CENTER
;
1755 case TGSI_INTERPOLATE_COLOR
:
1756 case TGSI_INTERPOLATE_PERSPECTIVE
:
1757 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1758 return SI_PARAM_PERSP_SAMPLE
;
1759 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1760 return SI_PARAM_PERSP_CENTROID
;
1762 return SI_PARAM_PERSP_CENTER
;
1765 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1770 static LLVMValueRef
si_build_fs_interp(struct si_shader_context
*ctx
,
1771 unsigned attr_index
, unsigned chan
,
1772 LLVMValueRef prim_mask
,
1773 LLVMValueRef i
, LLVMValueRef j
)
1776 return ac_build_fs_interp(&ctx
->ac
,
1777 LLVMConstInt(ctx
->i32
, chan
, 0),
1778 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1781 return ac_build_fs_interp_mov(&ctx
->ac
,
1782 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1783 LLVMConstInt(ctx
->i32
, chan
, 0),
1784 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1789 * Interpolate a fragment shader input.
1791 * @param ctx context
1792 * @param input_index index of the input in hardware
1793 * @param semantic_name TGSI_SEMANTIC_*
1794 * @param semantic_index semantic index
1795 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1796 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1797 * @param interp_param interpolation weights (i,j)
1798 * @param prim_mask SI_PARAM_PRIM_MASK
1799 * @param face SI_PARAM_FRONT_FACE
1800 * @param result the return value (4 components)
1802 static void interp_fs_input(struct si_shader_context
*ctx
,
1803 unsigned input_index
,
1804 unsigned semantic_name
,
1805 unsigned semantic_index
,
1806 unsigned num_interp_inputs
,
1807 unsigned colors_read_mask
,
1808 LLVMValueRef interp_param
,
1809 LLVMValueRef prim_mask
,
1811 LLVMValueRef result
[4])
1813 LLVMValueRef i
= NULL
, j
= NULL
;
1816 /* fs.constant returns the param from the middle vertex, so it's not
1817 * really useful for flat shading. It's meant to be used for custom
1818 * interpolation (but the intrinsic can't fetch from the other two
1821 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1822 * to do the right thing. The only reason we use fs.constant is that
1823 * fs.interp cannot be used on integers, because they can be equal
1826 * When interp is false we will use fs.constant or for newer llvm,
1827 * amdgcn.interp.mov.
1829 bool interp
= interp_param
!= NULL
;
1832 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
1833 LLVMVectorType(ctx
->f32
, 2), "");
1835 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1837 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1841 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1842 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1843 LLVMValueRef is_face_positive
;
1845 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1846 * otherwise it's at offset "num_inputs".
1848 unsigned back_attr_offset
= num_interp_inputs
;
1849 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1850 back_attr_offset
+= 1;
1852 is_face_positive
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
1853 face
, ctx
->i32_0
, "");
1855 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1856 LLVMValueRef front
, back
;
1858 front
= si_build_fs_interp(ctx
,
1861 back
= si_build_fs_interp(ctx
,
1862 back_attr_offset
, chan
,
1865 result
[chan
] = LLVMBuildSelect(ctx
->ac
.builder
,
1871 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1872 result
[0] = si_build_fs_interp(ctx
, input_index
,
1873 0, prim_mask
, i
, j
);
1875 result
[2] = LLVMConstReal(ctx
->f32
, 0.0f
);
1876 result
[3] = LLVMConstReal(ctx
->f32
, 1.0f
);
1878 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1879 result
[chan
] = si_build_fs_interp(ctx
,
1886 void si_llvm_load_input_fs(
1887 struct si_shader_context
*ctx
,
1888 unsigned input_index
,
1889 LLVMValueRef out
[4])
1891 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
1892 struct si_shader
*shader
= ctx
->shader
;
1893 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1894 LLVMValueRef main_fn
= ctx
->main_fn
;
1895 LLVMValueRef interp_param
= NULL
;
1896 int interp_param_idx
;
1897 enum tgsi_semantic semantic_name
= info
->input_semantic_name
[input_index
];
1898 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1899 enum tgsi_interpolate_mode interp_mode
= info
->input_interpolate
[input_index
];
1900 enum tgsi_interpolate_loc interp_loc
= info
->input_interpolate_loc
[input_index
];
1902 /* Get colors from input VGPRs (set by the prolog). */
1903 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1904 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1905 unsigned mask
= colors_read
>> (semantic_index
* 4);
1906 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1907 (semantic_index
? util_bitcount(colors_read
& 0xf) : 0);
1909 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1910 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1911 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1912 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1916 interp_param_idx
= lookup_interp_param_index(interp_mode
, interp_loc
);
1917 if (interp_param_idx
== -1)
1919 else if (interp_param_idx
) {
1920 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1923 interp_fs_input(ctx
, input_index
, semantic_name
,
1924 semantic_index
, 0, /* this param is unused */
1925 shader
->selector
->info
.colors_read
, interp_param
,
1927 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1931 static void declare_input_fs(
1932 struct si_shader_context
*ctx
,
1933 unsigned input_index
,
1934 const struct tgsi_full_declaration
*decl
,
1935 LLVMValueRef out
[4])
1937 si_llvm_load_input_fs(ctx
, input_index
, out
);
1940 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
)
1942 return si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 8, 4);
1945 static LLVMValueRef
get_base_vertex(struct ac_shader_abi
*abi
)
1947 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1949 /* For non-indexed draws, the base vertex set by the driver
1950 * (for direct draws) or the CP (for indirect draws) is the
1951 * first vertex ID, but GLSL expects 0 to be returned.
1953 LLVMValueRef vs_state
= LLVMGetParam(ctx
->main_fn
,
1954 ctx
->param_vs_state_bits
);
1955 LLVMValueRef indexed
;
1957 indexed
= LLVMBuildLShr(ctx
->ac
.builder
, vs_state
, ctx
->i32_1
, "");
1958 indexed
= LLVMBuildTrunc(ctx
->ac
.builder
, indexed
, ctx
->i1
, "");
1960 return LLVMBuildSelect(ctx
->ac
.builder
, indexed
, ctx
->abi
.base_vertex
,
1964 static LLVMValueRef
get_block_size(struct ac_shader_abi
*abi
)
1966 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1968 LLVMValueRef values
[3];
1969 LLVMValueRef result
;
1971 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1973 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1974 unsigned sizes
[3] = {
1975 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1976 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1977 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1980 for (i
= 0; i
< 3; ++i
)
1981 values
[i
] = LLVMConstInt(ctx
->i32
, sizes
[i
], 0);
1983 result
= lp_build_gather_values(&ctx
->gallivm
, values
, 3);
1985 result
= LLVMGetParam(ctx
->main_fn
, ctx
->param_block_size
);
1992 * Load a dword from a constant buffer.
1994 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1995 LLVMValueRef resource
,
1996 LLVMValueRef offset
)
1998 return ac_build_buffer_load(&ctx
->ac
, resource
, 1, NULL
, offset
, NULL
,
1999 0, 0, 0, true, true);
2002 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
, LLVMValueRef sample_id
)
2004 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2005 struct lp_build_context
*uint_bld
= &ctx
->bld_base
.uint_bld
;
2006 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2007 LLVMValueRef buf_index
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_SAMPLE_POSITIONS
, 0);
2008 LLVMValueRef resource
= ac_build_load_to_sgpr(&ctx
->ac
, desc
, buf_index
);
2010 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
2011 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
2012 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->ac
.builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2014 LLVMValueRef pos
[4] = {
2015 buffer_load_const(ctx
, resource
, offset0
),
2016 buffer_load_const(ctx
, resource
, offset1
),
2017 LLVMConstReal(ctx
->f32
, 0),
2018 LLVMConstReal(ctx
->f32
, 0)
2021 return lp_build_gather_values(&ctx
->gallivm
, pos
, 4);
2024 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
2026 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2027 return ac_to_integer(&ctx
->ac
, abi
->sample_coverage
);
2030 static LLVMValueRef
si_load_tess_coord(struct ac_shader_abi
*abi
)
2032 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2033 struct lp_build_context
*bld
= &ctx
->bld_base
.base
;
2035 LLVMValueRef coord
[4] = {
2036 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_u
),
2037 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_v
),
2042 /* For triangles, the vector should be (u, v, 1-u-v). */
2043 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
2044 PIPE_PRIM_TRIANGLES
)
2045 coord
[2] = lp_build_sub(bld
, ctx
->ac
.f32_1
,
2046 lp_build_add(bld
, coord
[0], coord
[1]));
2048 return lp_build_gather_values(&ctx
->gallivm
, coord
, 4);
2051 static LLVMValueRef
load_tess_level(struct si_shader_context
*ctx
,
2052 unsigned semantic_name
)
2054 LLVMValueRef base
, addr
;
2056 int param
= si_shader_io_get_unique_index_patch(semantic_name
, 0);
2058 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
2059 addr
= get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
), NULL
,
2060 LLVMConstInt(ctx
->i32
, param
, 0));
2062 return buffer_load(&ctx
->bld_base
, ctx
->f32
,
2063 ~0, ctx
->tess_offchip_ring
, base
, addr
, true);
2067 static LLVMValueRef
si_load_tess_level(struct ac_shader_abi
*abi
,
2068 unsigned varying_id
)
2070 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2071 unsigned semantic_name
;
2073 switch (varying_id
) {
2074 case VARYING_SLOT_TESS_LEVEL_INNER
:
2075 semantic_name
= TGSI_SEMANTIC_TESSINNER
;
2077 case VARYING_SLOT_TESS_LEVEL_OUTER
:
2078 semantic_name
= TGSI_SEMANTIC_TESSOUTER
;
2081 unreachable("unknown tess level");
2084 return load_tess_level(ctx
, semantic_name
);
2088 static LLVMValueRef
si_load_patch_vertices_in(struct ac_shader_abi
*abi
)
2090 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2091 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2092 return si_unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 13, 6);
2093 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
2094 return get_num_tcs_out_vertices(ctx
);
2096 unreachable("invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
2099 void si_load_system_value(struct si_shader_context
*ctx
,
2101 const struct tgsi_full_declaration
*decl
)
2103 LLVMValueRef value
= 0;
2105 assert(index
< RADEON_LLVM_MAX_SYSTEM_VALUES
);
2107 switch (decl
->Semantic
.Name
) {
2108 case TGSI_SEMANTIC_INSTANCEID
:
2109 value
= ctx
->abi
.instance_id
;
2112 case TGSI_SEMANTIC_VERTEXID
:
2113 value
= LLVMBuildAdd(ctx
->ac
.builder
,
2115 ctx
->abi
.base_vertex
, "");
2118 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
2119 /* Unused. Clarify the meaning in indexed vs. non-indexed
2120 * draws if this is ever used again. */
2124 case TGSI_SEMANTIC_BASEVERTEX
:
2125 value
= get_base_vertex(&ctx
->abi
);
2128 case TGSI_SEMANTIC_BASEINSTANCE
:
2129 value
= ctx
->abi
.start_instance
;
2132 case TGSI_SEMANTIC_DRAWID
:
2133 value
= ctx
->abi
.draw_id
;
2136 case TGSI_SEMANTIC_INVOCATIONID
:
2137 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2138 value
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
2139 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
2140 value
= ctx
->abi
.gs_invocation_id
;
2142 assert(!"INVOCATIONID not implemented");
2145 case TGSI_SEMANTIC_POSITION
:
2147 LLVMValueRef pos
[4] = {
2148 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2149 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2150 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
2151 lp_build_emit_llvm_unary(&ctx
->bld_base
, TGSI_OPCODE_RCP
,
2152 LLVMGetParam(ctx
->main_fn
,
2153 SI_PARAM_POS_W_FLOAT
)),
2155 value
= lp_build_gather_values(&ctx
->gallivm
, pos
, 4);
2159 case TGSI_SEMANTIC_FACE
:
2160 value
= ctx
->abi
.front_face
;
2163 case TGSI_SEMANTIC_SAMPLEID
:
2164 value
= si_get_sample_id(ctx
);
2167 case TGSI_SEMANTIC_SAMPLEPOS
: {
2168 LLVMValueRef pos
[4] = {
2169 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2170 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2171 LLVMConstReal(ctx
->f32
, 0),
2172 LLVMConstReal(ctx
->f32
, 0)
2174 pos
[0] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
2175 TGSI_OPCODE_FRC
, pos
[0]);
2176 pos
[1] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
2177 TGSI_OPCODE_FRC
, pos
[1]);
2178 value
= lp_build_gather_values(&ctx
->gallivm
, pos
, 4);
2182 case TGSI_SEMANTIC_SAMPLEMASK
:
2183 /* This can only occur with the OpenGL Core profile, which
2184 * doesn't support smoothing.
2186 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
2189 case TGSI_SEMANTIC_TESSCOORD
:
2190 value
= si_load_tess_coord(&ctx
->abi
);
2193 case TGSI_SEMANTIC_VERTICESIN
:
2194 value
= si_load_patch_vertices_in(&ctx
->abi
);
2197 case TGSI_SEMANTIC_TESSINNER
:
2198 case TGSI_SEMANTIC_TESSOUTER
:
2199 value
= load_tess_level(ctx
, decl
->Semantic
.Name
);
2202 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
2203 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
2205 LLVMValueRef buf
, slot
, val
[4];
2208 slot
= LLVMConstInt(ctx
->i32
, SI_HS_CONST_DEFAULT_TESS_LEVELS
, 0);
2209 buf
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2210 buf
= ac_build_load_to_sgpr(&ctx
->ac
, buf
, slot
);
2211 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
2213 for (i
= 0; i
< 4; i
++)
2214 val
[i
] = buffer_load_const(ctx
, buf
,
2215 LLVMConstInt(ctx
->i32
, (offset
+ i
) * 4, 0));
2216 value
= lp_build_gather_values(&ctx
->gallivm
, val
, 4);
2220 case TGSI_SEMANTIC_PRIMID
:
2221 value
= get_primitive_id(ctx
, 0);
2224 case TGSI_SEMANTIC_GRID_SIZE
:
2225 value
= ctx
->abi
.num_work_groups
;
2228 case TGSI_SEMANTIC_BLOCK_SIZE
:
2229 value
= get_block_size(&ctx
->abi
);
2232 case TGSI_SEMANTIC_BLOCK_ID
:
2234 LLVMValueRef values
[3];
2236 for (int i
= 0; i
< 3; i
++) {
2237 values
[i
] = ctx
->i32_0
;
2238 if (ctx
->abi
.workgroup_ids
[i
]) {
2239 values
[i
] = ctx
->abi
.workgroup_ids
[i
];
2242 value
= lp_build_gather_values(&ctx
->gallivm
, values
, 3);
2246 case TGSI_SEMANTIC_THREAD_ID
:
2247 value
= ctx
->abi
.local_invocation_ids
;
2250 case TGSI_SEMANTIC_HELPER_INVOCATION
:
2251 value
= lp_build_intrinsic(ctx
->ac
.builder
,
2252 "llvm.amdgcn.ps.live",
2254 LP_FUNC_ATTR_READNONE
);
2255 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2256 value
= LLVMBuildSExt(ctx
->ac
.builder
, value
, ctx
->i32
, "");
2259 case TGSI_SEMANTIC_SUBGROUP_SIZE
:
2260 value
= LLVMConstInt(ctx
->i32
, 64, 0);
2263 case TGSI_SEMANTIC_SUBGROUP_INVOCATION
:
2264 value
= ac_get_thread_id(&ctx
->ac
);
2267 case TGSI_SEMANTIC_SUBGROUP_EQ_MASK
:
2269 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2270 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2271 value
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->i64
, 1, 0), id
, "");
2272 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2276 case TGSI_SEMANTIC_SUBGROUP_GE_MASK
:
2277 case TGSI_SEMANTIC_SUBGROUP_GT_MASK
:
2278 case TGSI_SEMANTIC_SUBGROUP_LE_MASK
:
2279 case TGSI_SEMANTIC_SUBGROUP_LT_MASK
:
2281 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2282 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_GT_MASK
||
2283 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
) {
2284 /* All bits set except LSB */
2285 value
= LLVMConstInt(ctx
->i64
, -2, 0);
2288 value
= LLVMConstInt(ctx
->i64
, -1, 0);
2290 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2291 value
= LLVMBuildShl(ctx
->ac
.builder
, value
, id
, "");
2292 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
||
2293 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LT_MASK
)
2294 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2295 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2300 assert(!"unknown system value");
2304 ctx
->system_values
[index
] = value
;
2307 void si_declare_compute_memory(struct si_shader_context
*ctx
)
2309 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2311 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, AC_LOCAL_ADDR_SPACE
);
2314 assert(!ctx
->ac
.lds
);
2316 var
= LLVMAddGlobalInAddressSpace(ctx
->ac
.module
,
2317 LLVMArrayType(ctx
->i8
, sel
->local_size
),
2319 AC_LOCAL_ADDR_SPACE
);
2320 LLVMSetAlignment(var
, 4);
2322 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, var
, i8p
, "");
2325 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
2326 const struct tgsi_full_declaration
*decl
)
2328 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
2329 assert(decl
->Range
.First
== decl
->Range
.Last
);
2331 si_declare_compute_memory(ctx
);
2334 static LLVMValueRef
load_const_buffer_desc_fast_path(struct si_shader_context
*ctx
)
2337 LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2338 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2340 /* Do the bounds checking with a descriptor, because
2341 * doing computation and manual bounds checking of 64-bit
2342 * addresses generates horrible VALU code with very high
2343 * VGPR usage and very low SIMD occupancy.
2345 ptr
= LLVMBuildPtrToInt(ctx
->ac
.builder
, ptr
, ctx
->ac
.intptr
, "");
2347 LLVMValueRef desc0
, desc1
;
2348 if (HAVE_32BIT_POINTERS
) {
2350 desc1
= LLVMConstInt(ctx
->i32
,
2351 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
2353 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->v2i32
, "");
2354 desc0
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_0
, "");
2355 desc1
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_1
, "");
2356 /* Mask out all bits except BASE_ADDRESS_HI. */
2357 desc1
= LLVMBuildAnd(ctx
->ac
.builder
, desc1
,
2358 LLVMConstInt(ctx
->i32
, ~C_008F04_BASE_ADDRESS_HI
, 0), "");
2361 LLVMValueRef desc_elems
[] = {
2364 LLVMConstInt(ctx
->i32
, (sel
->info
.const_file_max
[0] + 1) * 16, 0),
2365 LLVMConstInt(ctx
->i32
,
2366 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
2367 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
2368 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
2369 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
2370 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
2371 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0)
2374 return ac_build_gather_values(&ctx
->ac
, desc_elems
, 4);
2377 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
2379 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
2380 ctx
->param_const_and_shader_buffers
);
2382 return ac_build_load_to_sgpr(&ctx
->ac
, list_ptr
,
2383 LLVMConstInt(ctx
->i32
, si_get_constbuf_slot(i
), 0));
2386 static LLVMValueRef
load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef index
)
2388 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2389 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2391 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2393 if (sel
->info
.const_buffers_declared
== 1 &&
2394 sel
->info
.shader_buffers_declared
== 0) {
2395 return load_const_buffer_desc_fast_path(ctx
);
2398 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_const_buffers
);
2399 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2400 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2402 return ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2406 load_ssbo(struct ac_shader_abi
*abi
, LLVMValueRef index
, bool write
)
2408 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2409 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
2410 ctx
->param_const_and_shader_buffers
);
2412 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_shader_buffers
);
2413 index
= LLVMBuildSub(ctx
->ac
.builder
,
2414 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
- 1, 0),
2417 return ac_build_load_to_sgpr(&ctx
->ac
, rsrc_ptr
, index
);
2420 static LLVMValueRef
fetch_constant(
2421 struct lp_build_tgsi_context
*bld_base
,
2422 const struct tgsi_full_src_register
*reg
,
2423 enum tgsi_opcode_type type
,
2426 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2427 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2428 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
2431 LLVMValueRef addr
, bufp
;
2433 if (swizzle
== LP_CHAN_ALL
) {
2435 LLVMValueRef values
[4];
2436 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
2437 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
2439 return lp_build_gather_values(&ctx
->gallivm
, values
, 4);
2442 /* Split 64-bit loads. */
2443 if (tgsi_type_is_64bit(type
)) {
2444 LLVMValueRef lo
, hi
;
2446 lo
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, swizzle
);
2447 hi
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, swizzle
+ 1);
2448 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
2452 idx
= reg
->Register
.Index
* 4 + swizzle
;
2453 if (reg
->Register
.Indirect
) {
2454 addr
= si_get_indirect_index(ctx
, ireg
, 16, idx
* 4);
2456 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
2459 /* Fast path when user data SGPRs point to constant buffer 0 directly. */
2460 if (sel
->info
.const_buffers_declared
== 1 &&
2461 sel
->info
.shader_buffers_declared
== 0) {
2463 /* This enables use of s_load_dword and flat_load_dword for const buffer 0
2464 * loads, and up to x4 load opcode merging. However, it leads to horrible
2465 * code reducing SIMD wave occupancy from 8 to 2 in many cases.
2467 * Using s_buffer_load_dword (x1) seems to be the best option right now.
2469 * LLVM 5.0 on SI doesn't insert a required s_nop between SALU setting
2470 * a descriptor and s_buffer_load_dword using it, so we can't expand
2471 * the pointer into a full descriptor like below. We have to use
2472 * s_load_dword instead. The only case when LLVM 5.0 would select
2473 * s_buffer_load_dword (that we have to prevent) is when we use use
2474 * a literal offset where we don't need bounds checking.
2476 if (ctx
->screen
->info
.chip_class
== SI
&& HAVE_LLVM
< 0x0600 &&
2477 !reg
->Register
.Indirect
) {
2479 LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2481 addr
= LLVMBuildLShr(ctx
->ac
.builder
, addr
, LLVMConstInt(ctx
->i32
, 2, 0), "");
2482 LLVMValueRef result
= ac_build_load_invariant(&ctx
->ac
, ptr
, addr
);
2483 return bitcast(bld_base
, type
, result
);
2486 LLVMValueRef desc
= load_const_buffer_desc_fast_path(ctx
);
2487 LLVMValueRef result
= buffer_load_const(ctx
, desc
, addr
);
2488 return bitcast(bld_base
, type
, result
);
2491 assert(reg
->Register
.Dimension
);
2492 buf
= reg
->Dimension
.Index
;
2494 if (reg
->Dimension
.Indirect
) {
2495 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2497 index
= si_get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
2498 reg
->Dimension
.Index
,
2499 ctx
->num_const_buffers
);
2500 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2501 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2502 bufp
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2504 bufp
= load_const_buffer_desc(ctx
, buf
);
2506 return bitcast(bld_base
, type
, buffer_load_const(ctx
, bufp
, addr
));
2509 /* Initialize arguments for the shader export intrinsic */
2510 static void si_llvm_init_export_args(struct si_shader_context
*ctx
,
2511 LLVMValueRef
*values
,
2513 struct ac_export_args
*args
)
2515 LLVMValueRef f32undef
= LLVMGetUndef(ctx
->ac
.f32
);
2516 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
2518 bool is_int8
, is_int10
;
2520 /* Default is 0xf. Adjusted below depending on the format. */
2521 args
->enabled_channels
= 0xf; /* writemask */
2523 /* Specify whether the EXEC mask represents the valid mask */
2524 args
->valid_mask
= 0;
2526 /* Specify whether this is the last export */
2529 /* Specify the target we are exporting */
2530 args
->target
= target
;
2532 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
2533 const struct si_shader_key
*key
= &ctx
->shader
->key
;
2534 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
2535 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
2537 assert(cbuf
>= 0 && cbuf
< 8);
2538 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
2539 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
2540 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
2543 args
->compr
= false;
2544 args
->out
[0] = f32undef
;
2545 args
->out
[1] = f32undef
;
2546 args
->out
[2] = f32undef
;
2547 args
->out
[3] = f32undef
;
2549 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
2550 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
2551 unsigned bits
, bool hi
) = NULL
;
2553 switch (spi_shader_col_format
) {
2554 case V_028714_SPI_SHADER_ZERO
:
2555 args
->enabled_channels
= 0; /* writemask */
2556 args
->target
= V_008DFC_SQ_EXP_NULL
;
2559 case V_028714_SPI_SHADER_32_R
:
2560 args
->enabled_channels
= 1; /* writemask */
2561 args
->out
[0] = values
[0];
2564 case V_028714_SPI_SHADER_32_GR
:
2565 args
->enabled_channels
= 0x3; /* writemask */
2566 args
->out
[0] = values
[0];
2567 args
->out
[1] = values
[1];
2570 case V_028714_SPI_SHADER_32_AR
:
2571 args
->enabled_channels
= 0x9; /* writemask */
2572 args
->out
[0] = values
[0];
2573 args
->out
[3] = values
[3];
2576 case V_028714_SPI_SHADER_FP16_ABGR
:
2577 packf
= ac_build_cvt_pkrtz_f16
;
2580 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2581 packf
= ac_build_cvt_pknorm_u16
;
2584 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2585 packf
= ac_build_cvt_pknorm_i16
;
2588 case V_028714_SPI_SHADER_UINT16_ABGR
:
2589 packi
= ac_build_cvt_pk_u16
;
2592 case V_028714_SPI_SHADER_SINT16_ABGR
:
2593 packi
= ac_build_cvt_pk_i16
;
2596 case V_028714_SPI_SHADER_32_ABGR
:
2597 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2601 /* Pack f16 or norm_i16/u16. */
2603 for (chan
= 0; chan
< 2; chan
++) {
2604 LLVMValueRef pack_args
[2] = {
2606 values
[2 * chan
+ 1]
2608 LLVMValueRef packed
;
2610 packed
= packf(&ctx
->ac
, pack_args
);
2611 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2613 args
->compr
= 1; /* COMPR flag */
2617 for (chan
= 0; chan
< 2; chan
++) {
2618 LLVMValueRef pack_args
[2] = {
2619 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
2620 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
2622 LLVMValueRef packed
;
2624 packed
= packi(&ctx
->ac
, pack_args
,
2625 is_int8
? 8 : is_int10
? 10 : 16,
2627 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2629 args
->compr
= 1; /* COMPR flag */
2633 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2636 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2638 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2639 static LLVMRealPredicate cond_map
[PIPE_FUNC_ALWAYS
+ 1] = {
2640 [PIPE_FUNC_LESS
] = LLVMRealOLT
,
2641 [PIPE_FUNC_EQUAL
] = LLVMRealOEQ
,
2642 [PIPE_FUNC_LEQUAL
] = LLVMRealOLE
,
2643 [PIPE_FUNC_GREATER
] = LLVMRealOGT
,
2644 [PIPE_FUNC_NOTEQUAL
] = LLVMRealONE
,
2645 [PIPE_FUNC_GEQUAL
] = LLVMRealOGE
,
2647 LLVMRealPredicate cond
= cond_map
[ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
];
2650 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2651 SI_PARAM_ALPHA_REF
);
2652 LLVMValueRef alpha_pass
=
2653 LLVMBuildFCmp(ctx
->ac
.builder
, cond
, alpha
, alpha_ref
, "");
2654 ac_build_kill_if_false(&ctx
->ac
, alpha_pass
);
2656 ac_build_kill_if_false(&ctx
->ac
, LLVMConstInt(ctx
->i1
, 0, 0));
2660 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2662 unsigned samplemask_param
)
2664 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2665 LLVMValueRef coverage
;
2667 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2668 coverage
= LLVMGetParam(ctx
->main_fn
,
2670 coverage
= ac_to_integer(&ctx
->ac
, coverage
);
2672 coverage
= lp_build_intrinsic(ctx
->ac
.builder
, "llvm.ctpop.i32",
2674 &coverage
, 1, LP_FUNC_ATTR_READNONE
);
2676 coverage
= LLVMBuildUIToFP(ctx
->ac
.builder
, coverage
,
2679 coverage
= LLVMBuildFMul(ctx
->ac
.builder
, coverage
,
2680 LLVMConstReal(ctx
->f32
,
2681 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2683 return LLVMBuildFMul(ctx
->ac
.builder
, alpha
, coverage
, "");
2686 static void si_llvm_emit_clipvertex(struct si_shader_context
*ctx
,
2687 struct ac_export_args
*pos
, LLVMValueRef
*out_elts
)
2691 unsigned const_chan
;
2692 LLVMValueRef base_elt
;
2693 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2694 LLVMValueRef constbuf_index
= LLVMConstInt(ctx
->i32
,
2695 SI_VS_CONST_CLIP_PLANES
, 0);
2696 LLVMValueRef const_resource
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, constbuf_index
);
2698 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2699 struct ac_export_args
*args
= &pos
[2 + reg_index
];
2704 args
->out
[3] = LLVMConstReal(ctx
->f32
, 0.0f
);
2706 /* Compute dot products of position and user clip plane vectors */
2707 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2708 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2710 LLVMConstInt(ctx
->i32
, ((reg_index
* 4 + chan
) * 4 +
2711 const_chan
) * 4, 0);
2712 base_elt
= buffer_load_const(ctx
, const_resource
,
2715 lp_build_add(&ctx
->bld_base
.base
, args
->out
[chan
],
2716 lp_build_mul(&ctx
->bld_base
.base
, base_elt
,
2717 out_elts
[const_chan
]));
2721 args
->enabled_channels
= 0xf;
2722 args
->valid_mask
= 0;
2724 args
->target
= V_008DFC_SQ_EXP_POS
+ 2 + reg_index
;
2729 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2733 if (so
->num_outputs
)
2734 fprintf(stderr
, "STREAMOUT\n");
2736 for (i
= 0; i
< so
->num_outputs
; i
++) {
2737 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2738 so
->output
[i
].start_component
;
2739 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2740 i
, so
->output
[i
].output_buffer
,
2741 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2742 so
->output
[i
].register_index
,
2743 mask
& 1 ? "x" : "",
2744 mask
& 2 ? "y" : "",
2745 mask
& 4 ? "z" : "",
2746 mask
& 8 ? "w" : "");
2750 static void emit_streamout_output(struct si_shader_context
*ctx
,
2751 LLVMValueRef
const *so_buffers
,
2752 LLVMValueRef
const *so_write_offsets
,
2753 struct pipe_stream_output
*stream_out
,
2754 struct si_shader_output_values
*shader_out
)
2756 unsigned buf_idx
= stream_out
->output_buffer
;
2757 unsigned start
= stream_out
->start_component
;
2758 unsigned num_comps
= stream_out
->num_components
;
2759 LLVMValueRef out
[4];
2761 assert(num_comps
&& num_comps
<= 4);
2762 if (!num_comps
|| num_comps
> 4)
2765 /* Load the output as int. */
2766 for (int j
= 0; j
< num_comps
; j
++) {
2767 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2769 out
[j
] = ac_to_integer(&ctx
->ac
, shader_out
->values
[start
+ j
]);
2772 /* Pack the output. */
2773 LLVMValueRef vdata
= NULL
;
2775 switch (num_comps
) {
2776 case 1: /* as i32 */
2779 case 2: /* as v2i32 */
2780 case 3: /* as v4i32 (aligned to 4) */
2781 case 4: /* as v4i32 */
2782 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
2783 for (int j
= 0; j
< num_comps
; j
++) {
2784 vdata
= LLVMBuildInsertElement(ctx
->ac
.builder
, vdata
, out
[j
],
2785 LLVMConstInt(ctx
->i32
, j
, 0), "");
2790 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf_idx
],
2792 so_write_offsets
[buf_idx
],
2794 stream_out
->dst_offset
* 4, 1, 1, true, false);
2798 * Write streamout data to buffers for vertex stream @p stream (different
2799 * vertex streams can occur for GS copy shaders).
2801 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2802 struct si_shader_output_values
*outputs
,
2803 unsigned noutput
, unsigned stream
)
2805 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2806 struct pipe_stream_output_info
*so
= &sel
->so
;
2807 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2809 struct lp_build_if_state if_ctx
;
2811 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2812 LLVMValueRef so_vtx_count
=
2813 si_unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2815 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2817 /* can_emit = tid < so_vtx_count; */
2818 LLVMValueRef can_emit
=
2819 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2821 /* Emit the streamout code conditionally. This actually avoids
2822 * out-of-bounds buffer access. The hw tells us via the SGPR
2823 * (so_vtx_count) which threads are allowed to emit streamout data. */
2824 lp_build_if(&if_ctx
, &ctx
->gallivm
, can_emit
);
2826 /* The buffer offset is computed as follows:
2827 * ByteOffset = streamout_offset[buffer_id]*4 +
2828 * (streamout_write_index + thread_id)*stride[buffer_id] +
2832 LLVMValueRef so_write_index
=
2833 LLVMGetParam(ctx
->main_fn
,
2834 ctx
->param_streamout_write_index
);
2836 /* Compute (streamout_write_index + thread_id). */
2837 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2839 /* Load the descriptor and compute the write offset for each
2840 * enabled buffer. */
2841 LLVMValueRef so_write_offset
[4] = {};
2842 LLVMValueRef so_buffers
[4];
2843 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2844 ctx
->param_rw_buffers
);
2846 for (i
= 0; i
< 4; i
++) {
2850 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
,
2851 SI_VS_STREAMOUT_BUF0
+ i
, 0);
2853 so_buffers
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
2855 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2856 ctx
->param_streamout_offset
[i
]);
2857 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2859 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
2860 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
2861 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
2864 /* Write streamout data. */
2865 for (i
= 0; i
< so
->num_outputs
; i
++) {
2866 unsigned reg
= so
->output
[i
].register_index
;
2871 if (stream
!= so
->output
[i
].stream
)
2874 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2875 &so
->output
[i
], &outputs
[reg
]);
2878 lp_build_endif(&if_ctx
);
2881 static void si_export_param(struct si_shader_context
*ctx
, unsigned index
,
2882 LLVMValueRef
*values
)
2884 struct ac_export_args args
;
2886 si_llvm_init_export_args(ctx
, values
,
2887 V_008DFC_SQ_EXP_PARAM
+ index
, &args
);
2888 ac_build_export(&ctx
->ac
, &args
);
2891 static void si_build_param_exports(struct si_shader_context
*ctx
,
2892 struct si_shader_output_values
*outputs
,
2895 struct si_shader
*shader
= ctx
->shader
;
2896 unsigned param_count
= 0;
2898 for (unsigned i
= 0; i
< noutput
; i
++) {
2899 unsigned semantic_name
= outputs
[i
].semantic_name
;
2900 unsigned semantic_index
= outputs
[i
].semantic_index
;
2902 if (outputs
[i
].vertex_stream
[0] != 0 &&
2903 outputs
[i
].vertex_stream
[1] != 0 &&
2904 outputs
[i
].vertex_stream
[2] != 0 &&
2905 outputs
[i
].vertex_stream
[3] != 0)
2908 switch (semantic_name
) {
2909 case TGSI_SEMANTIC_LAYER
:
2910 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2911 case TGSI_SEMANTIC_CLIPDIST
:
2912 case TGSI_SEMANTIC_COLOR
:
2913 case TGSI_SEMANTIC_BCOLOR
:
2914 case TGSI_SEMANTIC_PRIMID
:
2915 case TGSI_SEMANTIC_FOG
:
2916 case TGSI_SEMANTIC_TEXCOORD
:
2917 case TGSI_SEMANTIC_GENERIC
:
2923 if ((semantic_name
!= TGSI_SEMANTIC_GENERIC
||
2924 semantic_index
< SI_MAX_IO_GENERIC
) &&
2925 shader
->key
.opt
.kill_outputs
&
2926 (1ull << si_shader_io_get_unique_index(semantic_name
,
2927 semantic_index
, true)))
2930 si_export_param(ctx
, param_count
, outputs
[i
].values
);
2932 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2933 shader
->info
.vs_output_param_offset
[i
] = param_count
++;
2936 shader
->info
.nr_param_exports
= param_count
;
2939 /* Generate export instructions for hardware VS shader stage */
2940 static void si_llvm_export_vs(struct si_shader_context
*ctx
,
2941 struct si_shader_output_values
*outputs
,
2944 struct si_shader
*shader
= ctx
->shader
;
2945 struct ac_export_args pos_args
[4] = {};
2946 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2950 /* Build position exports. */
2951 for (i
= 0; i
< noutput
; i
++) {
2952 switch (outputs
[i
].semantic_name
) {
2953 case TGSI_SEMANTIC_POSITION
:
2954 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2955 V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
2957 case TGSI_SEMANTIC_PSIZE
:
2958 psize_value
= outputs
[i
].values
[0];
2960 case TGSI_SEMANTIC_LAYER
:
2961 layer_value
= outputs
[i
].values
[0];
2963 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2964 viewport_index_value
= outputs
[i
].values
[0];
2966 case TGSI_SEMANTIC_EDGEFLAG
:
2967 edgeflag_value
= outputs
[i
].values
[0];
2969 case TGSI_SEMANTIC_CLIPDIST
:
2970 if (!shader
->key
.opt
.clip_disable
) {
2971 unsigned index
= 2 + outputs
[i
].semantic_index
;
2972 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2973 V_008DFC_SQ_EXP_POS
+ index
,
2977 case TGSI_SEMANTIC_CLIPVERTEX
:
2978 if (!shader
->key
.opt
.clip_disable
) {
2979 si_llvm_emit_clipvertex(ctx
, pos_args
,
2986 /* We need to add the position output manually if it's missing. */
2987 if (!pos_args
[0].out
[0]) {
2988 pos_args
[0].enabled_channels
= 0xf; /* writemask */
2989 pos_args
[0].valid_mask
= 0; /* EXEC mask */
2990 pos_args
[0].done
= 0; /* last export? */
2991 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
2992 pos_args
[0].compr
= 0; /* COMPR flag */
2993 pos_args
[0].out
[0] = ctx
->ac
.f32_0
; /* X */
2994 pos_args
[0].out
[1] = ctx
->ac
.f32_0
; /* Y */
2995 pos_args
[0].out
[2] = ctx
->ac
.f32_0
; /* Z */
2996 pos_args
[0].out
[3] = ctx
->ac
.f32_1
; /* W */
2999 /* Write the misc vector (point size, edgeflag, layer, viewport). */
3000 if (shader
->selector
->info
.writes_psize
||
3001 shader
->selector
->info
.writes_edgeflag
||
3002 shader
->selector
->info
.writes_viewport_index
||
3003 shader
->selector
->info
.writes_layer
) {
3004 pos_args
[1].enabled_channels
= shader
->selector
->info
.writes_psize
|
3005 (shader
->selector
->info
.writes_edgeflag
<< 1) |
3006 (shader
->selector
->info
.writes_layer
<< 2);
3008 pos_args
[1].valid_mask
= 0; /* EXEC mask */
3009 pos_args
[1].done
= 0; /* last export? */
3010 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
3011 pos_args
[1].compr
= 0; /* COMPR flag */
3012 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
3013 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
3014 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
3015 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
3017 if (shader
->selector
->info
.writes_psize
)
3018 pos_args
[1].out
[0] = psize_value
;
3020 if (shader
->selector
->info
.writes_edgeflag
) {
3021 /* The output is a float, but the hw expects an integer
3022 * with the first bit containing the edge flag. */
3023 edgeflag_value
= LLVMBuildFPToUI(ctx
->ac
.builder
,
3026 edgeflag_value
= ac_build_umin(&ctx
->ac
,
3030 /* The LLVM intrinsic expects a float. */
3031 pos_args
[1].out
[1] = ac_to_float(&ctx
->ac
, edgeflag_value
);
3034 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3035 /* GFX9 has the layer in out.z[10:0] and the viewport
3036 * index in out.z[19:16].
3038 if (shader
->selector
->info
.writes_layer
)
3039 pos_args
[1].out
[2] = layer_value
;
3041 if (shader
->selector
->info
.writes_viewport_index
) {
3042 LLVMValueRef v
= viewport_index_value
;
3044 v
= ac_to_integer(&ctx
->ac
, v
);
3045 v
= LLVMBuildShl(ctx
->ac
.builder
, v
,
3046 LLVMConstInt(ctx
->i32
, 16, 0), "");
3047 v
= LLVMBuildOr(ctx
->ac
.builder
, v
,
3048 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
3049 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
3050 pos_args
[1].enabled_channels
|= 1 << 2;
3053 if (shader
->selector
->info
.writes_layer
)
3054 pos_args
[1].out
[2] = layer_value
;
3056 if (shader
->selector
->info
.writes_viewport_index
) {
3057 pos_args
[1].out
[3] = viewport_index_value
;
3058 pos_args
[1].enabled_channels
|= 1 << 3;
3063 for (i
= 0; i
< 4; i
++)
3064 if (pos_args
[i
].out
[0])
3065 shader
->info
.nr_pos_exports
++;
3068 for (i
= 0; i
< 4; i
++) {
3069 if (!pos_args
[i
].out
[0])
3072 /* Specify the target we are exporting */
3073 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
3075 if (pos_idx
== shader
->info
.nr_pos_exports
)
3076 /* Specify that this is the last export */
3077 pos_args
[i
].done
= 1;
3079 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
3082 /* Build parameter exports. */
3083 si_build_param_exports(ctx
, outputs
, noutput
);
3087 * Forward all outputs from the vertex shader to the TES. This is only used
3088 * for the fixed function TCS.
3090 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
3092 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3093 LLVMValueRef invocation_id
, buffer
, buffer_offset
;
3094 LLVMValueRef lds_vertex_stride
, lds_vertex_offset
, lds_base
;
3097 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3098 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3099 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3101 lds_vertex_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3102 lds_vertex_offset
= LLVMBuildMul(ctx
->ac
.builder
, invocation_id
,
3103 lds_vertex_stride
, "");
3104 lds_base
= get_tcs_in_current_patch_offset(ctx
);
3105 lds_base
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
, lds_vertex_offset
, "");
3107 inputs
= ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
;
3109 unsigned i
= u_bit_scan64(&inputs
);
3111 LLVMValueRef lds_ptr
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3112 LLVMConstInt(ctx
->i32
, 4 * i
, 0),
3115 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
3116 get_rel_patch_id(ctx
),
3118 LLVMConstInt(ctx
->i32
, i
, 0));
3120 LLVMValueRef value
= lds_load(bld_base
, ctx
->ac
.i32
, ~0,
3123 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buffer_addr
,
3124 buffer_offset
, 0, 1, 0, true, false);
3128 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
3129 LLVMValueRef rel_patch_id
,
3130 LLVMValueRef invocation_id
,
3131 LLVMValueRef tcs_out_current_patch_data_offset
,
3132 LLVMValueRef invoc0_tf_outer
[4],
3133 LLVMValueRef invoc0_tf_inner
[2])
3135 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3136 struct si_shader
*shader
= ctx
->shader
;
3137 unsigned tess_inner_index
, tess_outer_index
;
3138 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
3139 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
3140 unsigned stride
, outer_comps
, inner_comps
, i
, offset
;
3141 struct lp_build_if_state if_ctx
, inner_if_ctx
;
3143 /* Add a barrier before loading tess factors from LDS. */
3144 if (!shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
)
3145 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
3147 /* Do this only for invocation 0, because the tess levels are per-patch,
3150 * This can't jump, because invocation 0 executes this. It should
3151 * at least mask out the loads and stores for other invocations.
3153 lp_build_if(&if_ctx
, &ctx
->gallivm
,
3154 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3155 invocation_id
, ctx
->i32_0
, ""));
3157 /* Determine the layout of one tess factor element in the buffer. */
3158 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
3159 case PIPE_PRIM_LINES
:
3160 stride
= 2; /* 2 dwords, 1 vec2 store */
3164 case PIPE_PRIM_TRIANGLES
:
3165 stride
= 4; /* 4 dwords, 1 vec4 store */
3169 case PIPE_PRIM_QUADS
:
3170 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
3179 for (i
= 0; i
< 4; i
++) {
3180 inner
[i
] = LLVMGetUndef(ctx
->i32
);
3181 outer
[i
] = LLVMGetUndef(ctx
->i32
);
3184 if (shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
) {
3185 /* Tess factors are in VGPRs. */
3186 for (i
= 0; i
< outer_comps
; i
++)
3187 outer
[i
] = out
[i
] = invoc0_tf_outer
[i
];
3188 for (i
= 0; i
< inner_comps
; i
++)
3189 inner
[i
] = out
[outer_comps
+i
] = invoc0_tf_inner
[i
];
3191 /* Load tess_inner and tess_outer from LDS.
3192 * Any invocation can write them, so we can't get them from a temporary.
3194 tess_inner_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER
, 0);
3195 tess_outer_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER
, 0);
3197 lds_base
= tcs_out_current_patch_data_offset
;
3198 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3199 LLVMConstInt(ctx
->i32
,
3200 tess_inner_index
* 4, 0), "");
3201 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3202 LLVMConstInt(ctx
->i32
,
3203 tess_outer_index
* 4, 0), "");
3205 for (i
= 0; i
< outer_comps
; i
++) {
3207 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_outer
);
3209 for (i
= 0; i
< inner_comps
; i
++) {
3210 inner
[i
] = out
[outer_comps
+i
] =
3211 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_inner
);
3215 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
3216 /* For isolines, the hardware expects tess factors in the
3217 * reverse order from what GLSL / TGSI specify.
3219 LLVMValueRef tmp
= out
[0];
3224 /* Convert the outputs to vectors for stores. */
3225 vec0
= lp_build_gather_values(&ctx
->gallivm
, out
, MIN2(stride
, 4));
3229 vec1
= lp_build_gather_values(&ctx
->gallivm
, out
+4, stride
- 4);
3231 /* Get the buffer. */
3232 buffer
= get_tess_ring_descriptor(ctx
, TCS_FACTOR_RING
);
3234 /* Get the offset. */
3235 tf_base
= LLVMGetParam(ctx
->main_fn
,
3236 ctx
->param_tcs_factor_offset
);
3237 byteoffset
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
3238 LLVMConstInt(ctx
->i32
, 4 * stride
, 0), "");
3240 lp_build_if(&inner_if_ctx
, &ctx
->gallivm
,
3241 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3242 rel_patch_id
, ctx
->i32_0
, ""));
3244 /* Store the dynamic HS control word. */
3246 if (ctx
->screen
->info
.chip_class
<= VI
) {
3247 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
3248 LLVMConstInt(ctx
->i32
, 0x80000000, 0),
3249 1, ctx
->i32_0
, tf_base
,
3250 offset
, 1, 0, true, false);
3254 lp_build_endif(&inner_if_ctx
);
3256 /* Store the tessellation factors. */
3257 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
3258 MIN2(stride
, 4), byteoffset
, tf_base
,
3259 offset
, 1, 0, true, false);
3262 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
3263 stride
- 4, byteoffset
, tf_base
,
3264 offset
, 1, 0, true, false);
3266 /* Store the tess factors into the offchip buffer if TES reads them. */
3267 if (shader
->key
.part
.tcs
.epilog
.tes_reads_tess_factors
) {
3268 LLVMValueRef buf
, base
, inner_vec
, outer_vec
, tf_outer_offset
;
3269 LLVMValueRef tf_inner_offset
;
3270 unsigned param_outer
, param_inner
;
3272 buf
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3273 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3275 param_outer
= si_shader_io_get_unique_index_patch(
3276 TGSI_SEMANTIC_TESSOUTER
, 0);
3277 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3278 LLVMConstInt(ctx
->i32
, param_outer
, 0));
3280 outer_vec
= lp_build_gather_values(&ctx
->gallivm
, outer
,
3281 util_next_power_of_two(outer_comps
));
3283 ac_build_buffer_store_dword(&ctx
->ac
, buf
, outer_vec
,
3284 outer_comps
, tf_outer_offset
,
3285 base
, 0, 1, 0, true, false);
3287 param_inner
= si_shader_io_get_unique_index_patch(
3288 TGSI_SEMANTIC_TESSINNER
, 0);
3289 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3290 LLVMConstInt(ctx
->i32
, param_inner
, 0));
3292 inner_vec
= inner_comps
== 1 ? inner
[0] :
3293 lp_build_gather_values(&ctx
->gallivm
, inner
, inner_comps
);
3294 ac_build_buffer_store_dword(&ctx
->ac
, buf
, inner_vec
,
3295 inner_comps
, tf_inner_offset
,
3296 base
, 0, 1, 0, true, false);
3300 lp_build_endif(&if_ctx
);
3304 si_insert_input_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3305 unsigned param
, unsigned return_index
)
3307 return LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3308 LLVMGetParam(ctx
->main_fn
, param
),
3313 si_insert_input_ret_float(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3314 unsigned param
, unsigned return_index
)
3316 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3317 LLVMValueRef p
= LLVMGetParam(ctx
->main_fn
, param
);
3319 return LLVMBuildInsertValue(builder
, ret
,
3320 ac_to_float(&ctx
->ac
, p
),
3325 si_insert_input_ptr(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3326 unsigned param
, unsigned return_index
)
3328 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3329 LLVMValueRef ptr
, lo
, hi
;
3331 if (HAVE_32BIT_POINTERS
) {
3332 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3333 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i32
, "");
3334 return LLVMBuildInsertValue(builder
, ret
, ptr
, return_index
, "");
3337 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3338 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i64
, "");
3339 ptr
= LLVMBuildBitCast(builder
, ptr
, ctx
->v2i32
, "");
3340 lo
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_0
, "");
3341 hi
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_1
, "");
3342 ret
= LLVMBuildInsertValue(builder
, ret
, lo
, return_index
, "");
3343 return LLVMBuildInsertValue(builder
, ret
, hi
, return_index
+ 1, "");
3346 /* This only writes the tessellation factor levels. */
3347 static void si_llvm_emit_tcs_epilogue(struct ac_shader_abi
*abi
,
3348 unsigned max_outputs
,
3349 LLVMValueRef
*addrs
)
3351 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3352 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
3353 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3354 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
3356 si_copy_tcs_inputs(bld_base
);
3358 rel_patch_id
= get_rel_patch_id(ctx
);
3359 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3360 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
3362 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3363 LLVMBasicBlockRef blocks
[2] = {
3364 LLVMGetInsertBlock(builder
),
3365 ctx
->merged_wrap_if_state
.entry_block
3367 LLVMValueRef values
[2];
3369 lp_build_endif(&ctx
->merged_wrap_if_state
);
3371 values
[0] = rel_patch_id
;
3372 values
[1] = LLVMGetUndef(ctx
->i32
);
3373 rel_patch_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3375 values
[0] = tf_lds_offset
;
3376 values
[1] = LLVMGetUndef(ctx
->i32
);
3377 tf_lds_offset
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3379 values
[0] = invocation_id
;
3380 values
[1] = ctx
->i32_1
; /* cause the epilog to skip threads */
3381 invocation_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3384 /* Return epilog parameters from this function. */
3385 LLVMValueRef ret
= ctx
->return_value
;
3388 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3389 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3390 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3391 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3392 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3393 /* Tess offchip and tess factor offsets are at the beginning. */
3394 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3395 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3396 vgpr
= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
+ 1;
3398 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3399 GFX6_SGPR_TCS_OFFCHIP_LAYOUT
);
3400 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3401 GFX6_SGPR_TCS_OUT_LAYOUT
);
3402 /* Tess offchip and tess factor offsets are after user SGPRs. */
3403 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
,
3404 GFX6_TCS_NUM_USER_SGPR
);
3405 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
,
3406 GFX6_TCS_NUM_USER_SGPR
+ 1);
3407 vgpr
= GFX6_TCS_NUM_USER_SGPR
+ 2;
3411 rel_patch_id
= ac_to_float(&ctx
->ac
, rel_patch_id
);
3412 invocation_id
= ac_to_float(&ctx
->ac
, invocation_id
);
3413 tf_lds_offset
= ac_to_float(&ctx
->ac
, tf_lds_offset
);
3415 /* Leave a hole corresponding to the two input VGPRs. This ensures that
3416 * the invocation_id output does not alias the tcs_rel_ids input,
3417 * which saves a V_MOV on gfx9.
3421 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
3422 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
3424 if (ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
3425 vgpr
++; /* skip the tess factor LDS offset */
3426 for (unsigned i
= 0; i
< 6; i
++) {
3427 LLVMValueRef value
=
3428 LLVMBuildLoad(builder
, ctx
->invoc0_tess_factors
[i
], "");
3429 value
= ac_to_float(&ctx
->ac
, value
);
3430 ret
= LLVMBuildInsertValue(builder
, ret
, value
, vgpr
++, "");
3433 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
3435 ctx
->return_value
= ret
;
3438 /* Pass TCS inputs from LS to TCS on GFX9. */
3439 static void si_set_ls_return_value_for_tcs(struct si_shader_context
*ctx
)
3441 LLVMValueRef ret
= ctx
->return_value
;
3443 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3444 if (HAVE_32BIT_POINTERS
)
3445 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3446 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3447 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3448 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3449 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3451 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3452 8 + SI_SGPR_RW_BUFFERS
);
3453 ret
= si_insert_input_ptr(ctx
, ret
,
3454 ctx
->param_bindless_samplers_and_images
,
3455 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3457 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_vs_state_bits
,
3458 8 + SI_SGPR_VS_STATE_BITS
);
3460 #if !HAVE_32BIT_POINTERS
3461 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 1,
3462 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3465 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3466 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3467 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_offsets
,
3468 8 + GFX9_SGPR_TCS_OUT_OFFSETS
);
3469 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3470 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3472 unsigned vgpr
= 8 + GFX9_TCS_NUM_USER_SGPR
;
3473 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3474 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_patch_id
),
3476 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3477 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
),
3479 ctx
->return_value
= ret
;
3482 /* Pass GS inputs from ES to GS on GFX9. */
3483 static void si_set_es_return_value_for_gs(struct si_shader_context
*ctx
)
3485 LLVMValueRef ret
= ctx
->return_value
;
3487 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3488 if (HAVE_32BIT_POINTERS
)
3489 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3490 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_gs2vs_offset
, 2);
3491 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3492 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3494 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3495 8 + SI_SGPR_RW_BUFFERS
);
3496 ret
= si_insert_input_ptr(ctx
, ret
,
3497 ctx
->param_bindless_samplers_and_images
,
3498 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3500 #if !HAVE_32BIT_POINTERS
3501 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 1,
3502 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3506 if (ctx
->type
== PIPE_SHADER_VERTEX
)
3507 vgpr
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
3509 vgpr
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
3511 for (unsigned i
= 0; i
< 5; i
++) {
3512 unsigned param
= ctx
->param_gs_vtx01_offset
+ i
;
3513 ret
= si_insert_input_ret_float(ctx
, ret
, param
, vgpr
++);
3515 ctx
->return_value
= ret
;
3518 static void si_llvm_emit_ls_epilogue(struct ac_shader_abi
*abi
,
3519 unsigned max_outputs
,
3520 LLVMValueRef
*addrs
)
3522 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3523 struct si_shader
*shader
= ctx
->shader
;
3524 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3526 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
3527 ctx
->param_rel_auto_id
);
3528 LLVMValueRef vertex_dw_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3529 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->ac
.builder
, vertex_id
,
3530 vertex_dw_stride
, "");
3532 /* Write outputs to LDS. The next shader (TCS aka HS) will read
3533 * its inputs from it. */
3534 for (i
= 0; i
< info
->num_outputs
; i
++) {
3535 unsigned name
= info
->output_semantic_name
[i
];
3536 unsigned index
= info
->output_semantic_index
[i
];
3538 /* The ARB_shader_viewport_layer_array spec contains the
3541 * 2) What happens if gl_ViewportIndex or gl_Layer is
3542 * written in the vertex shader and a geometry shader is
3545 * RESOLVED: The value written by the last vertex processing
3546 * stage is used. If the last vertex processing stage
3547 * (vertex, tessellation evaluation or geometry) does not
3548 * statically assign to gl_ViewportIndex or gl_Layer, index
3549 * or layer zero is assumed.
3551 * So writes to those outputs in VS-as-LS are simply ignored.
3553 if (name
== TGSI_SEMANTIC_LAYER
||
3554 name
== TGSI_SEMANTIC_VIEWPORT_INDEX
)
3557 int param
= si_shader_io_get_unique_index(name
, index
, false);
3558 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_dw_addr
,
3559 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
3561 for (chan
= 0; chan
< 4; chan
++) {
3562 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3565 lds_store(ctx
, chan
, dw_addr
,
3566 LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], ""));
3570 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3571 si_set_ls_return_value_for_tcs(ctx
);
3574 static void si_llvm_emit_es_epilogue(struct ac_shader_abi
*abi
,
3575 unsigned max_outputs
,
3576 LLVMValueRef
*addrs
)
3578 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3579 struct si_shader
*es
= ctx
->shader
;
3580 struct tgsi_shader_info
*info
= &es
->selector
->info
;
3581 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
3582 ctx
->param_es2gs_offset
);
3583 LLVMValueRef lds_base
= NULL
;
3587 if (ctx
->screen
->info
.chip_class
>= GFX9
&& info
->num_outputs
) {
3588 unsigned itemsize_dw
= es
->selector
->esgs_itemsize
/ 4;
3589 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
3590 LLVMValueRef wave_idx
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 24, 4);
3591 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
3592 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
3593 LLVMConstInt(ctx
->i32
, 64, false), ""), "");
3594 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
3595 LLVMConstInt(ctx
->i32
, itemsize_dw
, 0), "");
3598 for (i
= 0; i
< info
->num_outputs
; i
++) {
3601 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
3602 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
3605 param
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
3606 info
->output_semantic_index
[i
], false);
3608 for (chan
= 0; chan
< 4; chan
++) {
3609 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3612 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
3613 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
3615 /* GFX9 has the ESGS ring in LDS. */
3616 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3617 lds_store(ctx
, param
* 4 + chan
, lds_base
, out_val
);
3621 ac_build_buffer_store_dword(&ctx
->ac
,
3623 out_val
, 1, NULL
, soffset
,
3624 (4 * param
+ chan
) * 4,
3629 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3630 si_set_es_return_value_for_gs(ctx
);
3633 static LLVMValueRef
si_get_gs_wave_id(struct si_shader_context
*ctx
)
3635 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3636 return si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 16, 8);
3638 return LLVMGetParam(ctx
->main_fn
, ctx
->param_gs_wave_id
);
3641 static void emit_gs_epilogue(struct si_shader_context
*ctx
)
3643 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
,
3644 si_get_gs_wave_id(ctx
));
3646 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3647 lp_build_endif(&ctx
->merged_wrap_if_state
);
3650 static void si_llvm_emit_gs_epilogue(struct ac_shader_abi
*abi
,
3651 unsigned max_outputs
,
3652 LLVMValueRef
*addrs
)
3654 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3655 struct tgsi_shader_info UNUSED
*info
= &ctx
->shader
->selector
->info
;
3657 assert(info
->num_outputs
<= max_outputs
);
3659 emit_gs_epilogue(ctx
);
3662 static void si_tgsi_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
3664 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3665 emit_gs_epilogue(ctx
);
3668 static void si_llvm_emit_vs_epilogue(struct ac_shader_abi
*abi
,
3669 unsigned max_outputs
,
3670 LLVMValueRef
*addrs
)
3672 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3673 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
3674 struct si_shader_output_values
*outputs
= NULL
;
3677 assert(!ctx
->shader
->is_gs_copy_shader
);
3678 assert(info
->num_outputs
<= max_outputs
);
3680 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
3682 /* Vertex color clamping.
3684 * This uses a state constant loaded in a user data SGPR and
3685 * an IF statement is added that clamps all colors if the constant
3688 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
3689 struct lp_build_if_state if_ctx
;
3690 LLVMValueRef cond
= NULL
;
3691 LLVMValueRef addr
, val
;
3693 for (i
= 0; i
< info
->num_outputs
; i
++) {
3694 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
3695 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
3698 /* We've found a color. */
3700 /* The state is in the first bit of the user SGPR. */
3701 cond
= LLVMGetParam(ctx
->main_fn
,
3702 ctx
->param_vs_state_bits
);
3703 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
,
3705 lp_build_if(&if_ctx
, &ctx
->gallivm
, cond
);
3708 for (j
= 0; j
< 4; j
++) {
3709 addr
= addrs
[4 * i
+ j
];
3710 val
= LLVMBuildLoad(ctx
->ac
.builder
, addr
, "");
3711 val
= ac_build_clamp(&ctx
->ac
, val
);
3712 LLVMBuildStore(ctx
->ac
.builder
, val
, addr
);
3717 lp_build_endif(&if_ctx
);
3720 for (i
= 0; i
< info
->num_outputs
; i
++) {
3721 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
3722 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
3724 for (j
= 0; j
< 4; j
++) {
3725 outputs
[i
].values
[j
] =
3726 LLVMBuildLoad(ctx
->ac
.builder
,
3729 outputs
[i
].vertex_stream
[j
] =
3730 (info
->output_streams
[i
] >> (2 * j
)) & 3;
3734 if (ctx
->shader
->selector
->so
.num_outputs
)
3735 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
3737 /* Export PrimitiveID. */
3738 if (ctx
->shader
->key
.mono
.u
.vs_export_prim_id
) {
3739 outputs
[i
].semantic_name
= TGSI_SEMANTIC_PRIMID
;
3740 outputs
[i
].semantic_index
= 0;
3741 outputs
[i
].values
[0] = ac_to_float(&ctx
->ac
, get_primitive_id(ctx
, 0));
3742 for (j
= 1; j
< 4; j
++)
3743 outputs
[i
].values
[j
] = LLVMConstReal(ctx
->f32
, 0);
3745 memset(outputs
[i
].vertex_stream
, 0,
3746 sizeof(outputs
[i
].vertex_stream
));
3750 si_llvm_export_vs(ctx
, outputs
, i
);
3754 static void si_tgsi_emit_epilogue(struct lp_build_tgsi_context
*bld_base
)
3756 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3758 ctx
->abi
.emit_outputs(&ctx
->abi
, RADEON_LLVM_MAX_OUTPUTS
,
3759 &ctx
->outputs
[0][0]);
3762 struct si_ps_exports
{
3764 struct ac_export_args args
[10];
3767 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
3768 LLVMValueRef depth
, LLVMValueRef stencil
,
3769 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
3771 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3772 struct ac_export_args args
;
3774 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
3776 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3779 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
3780 LLVMValueRef
*color
, unsigned index
,
3781 unsigned samplemask_param
,
3782 bool is_last
, struct si_ps_exports
*exp
)
3784 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3788 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
3789 for (i
= 0; i
< 4; i
++)
3790 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
3793 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
3794 color
[3] = ctx
->ac
.f32_1
;
3798 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
3799 si_alpha_test(bld_base
, color
[3]);
3801 /* Line & polygon smoothing */
3802 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
3803 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
3806 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3807 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
3808 struct ac_export_args args
[8];
3811 /* Get the export arguments, also find out what the last one is. */
3812 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3813 si_llvm_init_export_args(ctx
, color
,
3814 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
3815 if (args
[c
].enabled_channels
)
3819 /* Emit all exports. */
3820 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3821 if (is_last
&& last
== c
) {
3822 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
3823 args
[c
].done
= 1; /* DONE bit */
3824 } else if (!args
[c
].enabled_channels
)
3825 continue; /* unnecessary NULL export */
3827 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
3830 struct ac_export_args args
;
3833 si_llvm_init_export_args(ctx
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3836 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3837 args
.done
= 1; /* DONE bit */
3838 } else if (!args
.enabled_channels
)
3839 return; /* unnecessary NULL export */
3841 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3845 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3846 struct si_ps_exports
*exp
)
3848 for (unsigned i
= 0; i
< exp
->num
; i
++)
3849 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
3853 * Return PS outputs in this order:
3855 * v[0:3] = color0.xyzw
3856 * v[4:7] = color1.xyzw
3861 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3863 * The alpha-ref SGPR is returned via its original location.
3865 static void si_llvm_return_fs_outputs(struct ac_shader_abi
*abi
,
3866 unsigned max_outputs
,
3867 LLVMValueRef
*addrs
)
3869 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3870 struct si_shader
*shader
= ctx
->shader
;
3871 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3872 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3873 unsigned i
, j
, first_vgpr
, vgpr
;
3875 LLVMValueRef color
[8][4] = {};
3876 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3879 if (ctx
->postponed_kill
)
3880 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildLoad(builder
, ctx
->postponed_kill
, ""));
3882 /* Read the output values. */
3883 for (i
= 0; i
< info
->num_outputs
; i
++) {
3884 unsigned semantic_name
= info
->output_semantic_name
[i
];
3885 unsigned semantic_index
= info
->output_semantic_index
[i
];
3887 switch (semantic_name
) {
3888 case TGSI_SEMANTIC_COLOR
:
3889 assert(semantic_index
< 8);
3890 for (j
= 0; j
< 4; j
++) {
3891 LLVMValueRef ptr
= addrs
[4 * i
+ j
];
3892 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3893 color
[semantic_index
][j
] = result
;
3896 case TGSI_SEMANTIC_POSITION
:
3897 depth
= LLVMBuildLoad(builder
,
3898 addrs
[4 * i
+ 2], "");
3900 case TGSI_SEMANTIC_STENCIL
:
3901 stencil
= LLVMBuildLoad(builder
,
3902 addrs
[4 * i
+ 1], "");
3904 case TGSI_SEMANTIC_SAMPLEMASK
:
3905 samplemask
= LLVMBuildLoad(builder
,
3906 addrs
[4 * i
+ 0], "");
3909 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3914 /* Fill the return structure. */
3915 ret
= ctx
->return_value
;
3918 ret
= LLVMBuildInsertValue(builder
, ret
,
3919 ac_to_integer(&ctx
->ac
,
3920 LLVMGetParam(ctx
->main_fn
,
3921 SI_PARAM_ALPHA_REF
)),
3922 SI_SGPR_ALPHA_REF
, "");
3925 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3926 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3930 for (j
= 0; j
< 4; j
++)
3931 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3934 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3936 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3938 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3940 /* Add the input sample mask for smoothing at the end. */
3941 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3942 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3943 ret
= LLVMBuildInsertValue(builder
, ret
,
3944 LLVMGetParam(ctx
->main_fn
,
3945 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3947 ctx
->return_value
= ret
;
3950 static void membar_emit(
3951 const struct lp_build_tgsi_action
*action
,
3952 struct lp_build_tgsi_context
*bld_base
,
3953 struct lp_build_emit_data
*emit_data
)
3955 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3956 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3957 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3958 unsigned waitcnt
= NOOP_WAITCNT
;
3960 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3961 waitcnt
&= VM_CNT
& LGKM_CNT
;
3963 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3964 TGSI_MEMBAR_SHADER_BUFFER
|
3965 TGSI_MEMBAR_SHADER_IMAGE
))
3968 if (flags
& TGSI_MEMBAR_SHARED
)
3969 waitcnt
&= LGKM_CNT
;
3971 if (waitcnt
!= NOOP_WAITCNT
)
3972 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3975 static void clock_emit(
3976 const struct lp_build_tgsi_action
*action
,
3977 struct lp_build_tgsi_context
*bld_base
,
3978 struct lp_build_emit_data
*emit_data
)
3980 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3981 LLVMValueRef tmp
= ac_build_shader_clock(&ctx
->ac
);
3983 emit_data
->output
[0] =
3984 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_0
, "");
3985 emit_data
->output
[1] =
3986 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_1
, "");
3989 static void si_llvm_emit_ddxy(
3990 const struct lp_build_tgsi_action
*action
,
3991 struct lp_build_tgsi_context
*bld_base
,
3992 struct lp_build_emit_data
*emit_data
)
3994 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3995 unsigned opcode
= emit_data
->info
->opcode
;
4000 if (opcode
== TGSI_OPCODE_DDX_FINE
)
4001 mask
= AC_TID_MASK_LEFT
;
4002 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
4003 mask
= AC_TID_MASK_TOP
;
4005 mask
= AC_TID_MASK_TOP_LEFT
;
4007 /* for DDX we want to next X pixel, DDY next Y pixel. */
4008 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
4010 val
= ac_to_integer(&ctx
->ac
, emit_data
->args
[0]);
4011 val
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, val
);
4012 emit_data
->output
[emit_data
->chan
] = val
;
4016 * this takes an I,J coordinate pair,
4017 * and works out the X and Y derivatives.
4018 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4020 static LLVMValueRef
si_llvm_emit_ddxy_interp(
4021 struct lp_build_tgsi_context
*bld_base
,
4022 LLVMValueRef interp_ij
)
4024 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4025 LLVMValueRef result
[4], a
;
4028 for (i
= 0; i
< 2; i
++) {
4029 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
4030 LLVMConstInt(ctx
->i32
, i
, 0), "");
4031 result
[i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDX
, a
);
4032 result
[2+i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDY
, a
);
4035 return lp_build_gather_values(&ctx
->gallivm
, result
, 4);
4038 static void interp_fetch_args(
4039 struct lp_build_tgsi_context
*bld_base
,
4040 struct lp_build_emit_data
*emit_data
)
4042 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4043 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4045 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
4046 /* offset is in second src, first two channels */
4047 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
4050 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
4053 emit_data
->arg_count
= 2;
4054 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4055 LLVMValueRef sample_position
;
4056 LLVMValueRef sample_id
;
4057 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
4059 /* fetch sample ID, then fetch its sample position,
4060 * and place into first two channels.
4062 sample_id
= lp_build_emit_fetch(bld_base
,
4063 emit_data
->inst
, 1, TGSI_CHAN_X
);
4064 sample_id
= ac_to_integer(&ctx
->ac
, sample_id
);
4066 /* Section 8.13.2 (Interpolation Functions) of the OpenGL Shading
4067 * Language 4.50 spec says about interpolateAtSample:
4069 * "Returns the value of the input interpolant variable at
4070 * the location of sample number sample. If multisample
4071 * buffers are not available, the input variable will be
4072 * evaluated at the center of the pixel. If sample sample
4073 * does not exist, the position used to interpolate the
4074 * input variable is undefined."
4076 * This means that sample_id values outside of the valid are
4077 * in fact valid input, and the usual mechanism for loading the
4078 * sample position doesn't work.
4080 if (ctx
->shader
->key
.mono
.u
.ps
.interpolate_at_sample_force_center
) {
4081 LLVMValueRef center
[4] = {
4082 LLVMConstReal(ctx
->f32
, 0.5),
4083 LLVMConstReal(ctx
->f32
, 0.5),
4088 sample_position
= lp_build_gather_values(&ctx
->gallivm
, center
, 4);
4090 sample_position
= load_sample_position(&ctx
->abi
, sample_id
);
4093 emit_data
->args
[0] = LLVMBuildExtractElement(ctx
->ac
.builder
,
4097 emit_data
->args
[0] = LLVMBuildFSub(ctx
->ac
.builder
, emit_data
->args
[0], halfval
, "");
4098 emit_data
->args
[1] = LLVMBuildExtractElement(ctx
->ac
.builder
,
4101 emit_data
->args
[1] = LLVMBuildFSub(ctx
->ac
.builder
, emit_data
->args
[1], halfval
, "");
4102 emit_data
->arg_count
= 2;
4106 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
4107 struct lp_build_tgsi_context
*bld_base
,
4108 struct lp_build_emit_data
*emit_data
)
4110 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4111 struct si_shader
*shader
= ctx
->shader
;
4112 const struct tgsi_shader_info
*info
= &shader
->selector
->info
;
4113 LLVMValueRef interp_param
;
4114 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4115 const struct tgsi_full_src_register
*input
= &inst
->Src
[0];
4116 int input_base
, input_array_size
;
4119 LLVMValueRef prim_mask
= ctx
->abi
.prim_mask
;
4120 LLVMValueRef array_idx
;
4121 int interp_param_idx
;
4125 assert(input
->Register
.File
== TGSI_FILE_INPUT
);
4127 if (input
->Register
.Indirect
) {
4128 unsigned array_id
= input
->Indirect
.ArrayID
;
4131 input_base
= info
->input_array_first
[array_id
];
4132 input_array_size
= info
->input_array_last
[array_id
] - input_base
+ 1;
4134 input_base
= inst
->Src
[0].Register
.Index
;
4135 input_array_size
= info
->num_inputs
- input_base
;
4138 array_idx
= si_get_indirect_index(ctx
, &input
->Indirect
,
4139 1, input
->Register
.Index
- input_base
);
4141 input_base
= inst
->Src
[0].Register
.Index
;
4142 input_array_size
= 1;
4143 array_idx
= ctx
->i32_0
;
4146 interp
= shader
->selector
->info
.input_interpolate
[input_base
];
4148 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4149 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
4150 location
= TGSI_INTERPOLATE_LOC_CENTER
;
4152 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
4154 interp_param_idx
= lookup_interp_param_index(interp
, location
);
4155 if (interp_param_idx
== -1)
4157 else if (interp_param_idx
)
4158 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
4160 interp_param
= NULL
;
4162 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4163 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4164 LLVMValueRef ij_out
[2];
4165 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
4168 * take the I then J parameters, and the DDX/Y for it, and
4169 * calculate the IJ inputs for the interpolator.
4170 * temp1 = ddx * offset/sample.x + I;
4171 * interp_param.I = ddy * offset/sample.y + temp1;
4172 * temp1 = ddx * offset/sample.x + J;
4173 * interp_param.J = ddy * offset/sample.y + temp1;
4175 for (i
= 0; i
< 2; i
++) {
4176 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, 0);
4177 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, 0);
4178 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4179 ddxy_out
, ix_ll
, "");
4180 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4181 ddxy_out
, iy_ll
, "");
4182 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4183 interp_param
, ix_ll
, "");
4184 LLVMValueRef temp1
, temp2
;
4186 interp_el
= ac_to_float(&ctx
->ac
, interp_el
);
4188 temp1
= LLVMBuildFMul(ctx
->ac
.builder
, ddx_el
, emit_data
->args
[0], "");
4190 temp1
= LLVMBuildFAdd(ctx
->ac
.builder
, temp1
, interp_el
, "");
4192 temp2
= LLVMBuildFMul(ctx
->ac
.builder
, ddy_el
, emit_data
->args
[1], "");
4194 ij_out
[i
] = LLVMBuildFAdd(ctx
->ac
.builder
, temp2
, temp1
, "");
4196 interp_param
= lp_build_gather_values(&ctx
->gallivm
, ij_out
, 2);
4200 interp_param
= ac_to_float(&ctx
->ac
, interp_param
);
4202 for (chan
= 0; chan
< 4; chan
++) {
4203 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->f32
, input_array_size
));
4204 unsigned schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
4206 for (unsigned idx
= 0; idx
< input_array_size
; ++idx
) {
4207 LLVMValueRef v
, i
= NULL
, j
= NULL
;
4210 i
= LLVMBuildExtractElement(
4211 ctx
->ac
.builder
, interp_param
, ctx
->i32_0
, "");
4212 j
= LLVMBuildExtractElement(
4213 ctx
->ac
.builder
, interp_param
, ctx
->i32_1
, "");
4215 v
= si_build_fs_interp(ctx
, input_base
+ idx
, schan
,
4218 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
,
4219 gather
, v
, LLVMConstInt(ctx
->i32
, idx
, false), "");
4222 emit_data
->output
[chan
] = LLVMBuildExtractElement(
4223 ctx
->ac
.builder
, gather
, array_idx
, "");
4227 static void vote_all_emit(
4228 const struct lp_build_tgsi_action
*action
,
4229 struct lp_build_tgsi_context
*bld_base
,
4230 struct lp_build_emit_data
*emit_data
)
4232 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4234 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, emit_data
->args
[0]);
4235 emit_data
->output
[emit_data
->chan
] =
4236 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4239 static void vote_any_emit(
4240 const struct lp_build_tgsi_action
*action
,
4241 struct lp_build_tgsi_context
*bld_base
,
4242 struct lp_build_emit_data
*emit_data
)
4244 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4246 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, emit_data
->args
[0]);
4247 emit_data
->output
[emit_data
->chan
] =
4248 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4251 static void vote_eq_emit(
4252 const struct lp_build_tgsi_action
*action
,
4253 struct lp_build_tgsi_context
*bld_base
,
4254 struct lp_build_emit_data
*emit_data
)
4256 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4258 LLVMValueRef tmp
= ac_build_vote_eq(&ctx
->ac
, emit_data
->args
[0]);
4259 emit_data
->output
[emit_data
->chan
] =
4260 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4263 static void ballot_emit(
4264 const struct lp_build_tgsi_action
*action
,
4265 struct lp_build_tgsi_context
*bld_base
,
4266 struct lp_build_emit_data
*emit_data
)
4268 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4269 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4272 tmp
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4273 tmp
= ac_build_ballot(&ctx
->ac
, tmp
);
4274 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->v2i32
, "");
4276 emit_data
->output
[0] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_0
, "");
4277 emit_data
->output
[1] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_1
, "");
4280 static void read_invoc_fetch_args(
4281 struct lp_build_tgsi_context
*bld_base
,
4282 struct lp_build_emit_data
*emit_data
)
4284 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4285 0, emit_data
->src_chan
);
4287 /* Always read the source invocation (= lane) from the X channel. */
4288 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4290 emit_data
->arg_count
= 2;
4293 static void read_lane_emit(
4294 const struct lp_build_tgsi_action
*action
,
4295 struct lp_build_tgsi_context
*bld_base
,
4296 struct lp_build_emit_data
*emit_data
)
4298 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4300 /* We currently have no other way to prevent LLVM from lifting the icmp
4301 * calls to a dominating basic block.
4303 ac_build_optimization_barrier(&ctx
->ac
, &emit_data
->args
[0]);
4305 for (unsigned i
= 0; i
< emit_data
->arg_count
; ++i
)
4306 emit_data
->args
[i
] = ac_to_integer(&ctx
->ac
, emit_data
->args
[i
]);
4308 emit_data
->output
[emit_data
->chan
] =
4309 ac_build_intrinsic(&ctx
->ac
, action
->intr_name
,
4310 ctx
->i32
, emit_data
->args
, emit_data
->arg_count
,
4311 AC_FUNC_ATTR_READNONE
|
4312 AC_FUNC_ATTR_CONVERGENT
);
4315 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
4316 struct lp_build_emit_data
*emit_data
)
4318 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4319 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
4323 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
4325 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
4326 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
4330 /* Emit one vertex from the geometry shader */
4331 static void si_llvm_emit_vertex(struct ac_shader_abi
*abi
,
4333 LLVMValueRef
*addrs
)
4335 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4336 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4337 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
4338 struct si_shader
*shader
= ctx
->shader
;
4339 struct lp_build_if_state if_state
;
4340 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
4341 ctx
->param_gs2vs_offset
);
4342 LLVMValueRef gs_next_vertex
;
4343 LLVMValueRef can_emit
;
4344 unsigned chan
, offset
;
4347 /* Write vertex attribute values to GSVS ring */
4348 gs_next_vertex
= LLVMBuildLoad(ctx
->ac
.builder
,
4349 ctx
->gs_next_vertex
[stream
],
4352 /* If this thread has already emitted the declared maximum number of
4353 * vertices, skip the write: excessive vertex emissions are not
4354 * supposed to have any effect.
4356 * If the shader has no writes to memory, kill it instead. This skips
4357 * further memory loads and may allow LLVM to skip to the end
4360 can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, gs_next_vertex
,
4361 LLVMConstInt(ctx
->i32
,
4362 shader
->selector
->gs_max_out_vertices
, 0), "");
4364 bool use_kill
= !info
->writes_memory
;
4366 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4368 lp_build_if(&if_state
, &ctx
->gallivm
, can_emit
);
4372 for (i
= 0; i
< info
->num_outputs
; i
++) {
4373 for (chan
= 0; chan
< 4; chan
++) {
4374 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
4375 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
4378 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
4379 LLVMValueRef voffset
=
4380 LLVMConstInt(ctx
->i32
, offset
*
4381 shader
->selector
->gs_max_out_vertices
, 0);
4384 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
4385 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
4387 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
4389 ac_build_buffer_store_dword(&ctx
->ac
,
4390 ctx
->gsvs_ring
[stream
],
4392 voffset
, soffset
, 0,
4397 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
4400 LLVMBuildStore(ctx
->ac
.builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
4402 /* Signal vertex emission */
4403 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
4404 si_get_gs_wave_id(ctx
));
4406 lp_build_endif(&if_state
);
4409 /* Emit one vertex from the geometry shader */
4410 static void si_tgsi_emit_vertex(
4411 const struct lp_build_tgsi_action
*action
,
4412 struct lp_build_tgsi_context
*bld_base
,
4413 struct lp_build_emit_data
*emit_data
)
4415 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4416 unsigned stream
= si_llvm_get_stream(bld_base
, emit_data
);
4418 si_llvm_emit_vertex(&ctx
->abi
, stream
, ctx
->outputs
[0]);
4421 /* Cut one primitive from the geometry shader */
4422 static void si_llvm_emit_primitive(struct ac_shader_abi
*abi
,
4425 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4427 /* Signal primitive cut */
4428 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
4429 si_get_gs_wave_id(ctx
));
4432 /* Cut one primitive from the geometry shader */
4433 static void si_tgsi_emit_primitive(
4434 const struct lp_build_tgsi_action
*action
,
4435 struct lp_build_tgsi_context
*bld_base
,
4436 struct lp_build_emit_data
*emit_data
)
4438 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4440 si_llvm_emit_primitive(&ctx
->abi
, si_llvm_get_stream(bld_base
, emit_data
));
4443 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4444 struct lp_build_tgsi_context
*bld_base
,
4445 struct lp_build_emit_data
*emit_data
)
4447 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4449 /* SI only (thanks to a hw bug workaround):
4450 * The real barrier instruction isn’t needed, because an entire patch
4451 * always fits into a single wave.
4453 if (ctx
->screen
->info
.chip_class
== SI
&&
4454 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
4455 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
4459 lp_build_intrinsic(ctx
->ac
.builder
,
4460 "llvm.amdgcn.s.barrier",
4461 ctx
->voidt
, NULL
, 0, LP_FUNC_ATTR_CONVERGENT
);
4464 static const struct lp_build_tgsi_action interp_action
= {
4465 .fetch_args
= interp_fetch_args
,
4466 .emit
= build_interp_intrinsic
,
4469 static void si_create_function(struct si_shader_context
*ctx
,
4471 LLVMTypeRef
*returns
, unsigned num_returns
,
4472 struct si_function_info
*fninfo
,
4473 unsigned max_workgroup_size
)
4477 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
4478 fninfo
->types
, fninfo
->num_params
);
4479 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
4481 for (i
= 0; i
< fninfo
->num_sgpr_params
; ++i
) {
4482 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
4484 /* The combination of:
4488 * allows the optimization passes to move loads and reduces
4489 * SGPR spilling significantly.
4491 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_INREG
);
4493 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
4494 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
4495 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
4499 for (i
= 0; i
< fninfo
->num_params
; ++i
) {
4500 if (fninfo
->assign
[i
])
4501 *fninfo
->assign
[i
] = LLVMGetParam(ctx
->main_fn
, i
);
4504 if (ctx
->screen
->info
.address32_hi
) {
4505 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4506 "amdgpu-32bit-address-high-bits",
4507 ctx
->screen
->info
.address32_hi
);
4510 if (max_workgroup_size
) {
4511 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4512 "amdgpu-max-work-group-size",
4513 max_workgroup_size
);
4515 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4516 "no-signed-zeros-fp-math",
4519 if (ctx
->screen
->debug_flags
& DBG(UNSAFE_MATH
)) {
4520 /* These were copied from some LLVM test. */
4521 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4522 "less-precise-fpmad",
4524 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4527 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4530 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4536 static void declare_streamout_params(struct si_shader_context
*ctx
,
4537 struct pipe_stream_output_info
*so
,
4538 struct si_function_info
*fninfo
)
4542 /* Streamout SGPRs. */
4543 if (so
->num_outputs
) {
4544 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
4545 ctx
->param_streamout_config
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4547 ctx
->param_streamout_config
= fninfo
->num_params
- 1;
4549 ctx
->param_streamout_write_index
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4551 /* A streamout buffer offset is loaded if the stride is non-zero. */
4552 for (i
= 0; i
< 4; i
++) {
4556 ctx
->param_streamout_offset
[i
] = add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4560 static unsigned si_get_max_workgroup_size(const struct si_shader
*shader
)
4562 switch (shader
->selector
->type
) {
4563 case PIPE_SHADER_TESS_CTRL
:
4564 /* Return this so that LLVM doesn't remove s_barrier
4565 * instructions on chips where we use s_barrier. */
4566 return shader
->selector
->screen
->info
.chip_class
>= CIK
? 128 : 64;
4568 case PIPE_SHADER_GEOMETRY
:
4569 return shader
->selector
->screen
->info
.chip_class
>= GFX9
? 128 : 64;
4571 case PIPE_SHADER_COMPUTE
:
4572 break; /* see below */
4578 const unsigned *properties
= shader
->selector
->info
.properties
;
4579 unsigned max_work_group_size
=
4580 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
4581 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
4582 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
4584 if (!max_work_group_size
) {
4585 /* This is a variable group size compute shader,
4586 * compile it for the maximum possible group size.
4588 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
4590 return max_work_group_size
;
4593 static void declare_const_and_shader_buffers(struct si_shader_context
*ctx
,
4594 struct si_function_info
*fninfo
,
4597 LLVMTypeRef const_shader_buf_type
;
4599 if (ctx
->shader
->selector
->info
.const_buffers_declared
== 1 &&
4600 ctx
->shader
->selector
->info
.shader_buffers_declared
== 0)
4601 const_shader_buf_type
= ctx
->f32
;
4603 const_shader_buf_type
= ctx
->v4i32
;
4605 unsigned const_and_shader_buffers
=
4606 add_arg(fninfo
, ARG_SGPR
,
4607 ac_array_in_const32_addr_space(const_shader_buf_type
));
4610 ctx
->param_const_and_shader_buffers
= const_and_shader_buffers
;
4613 static void declare_samplers_and_images(struct si_shader_context
*ctx
,
4614 struct si_function_info
*fninfo
,
4617 unsigned samplers_and_images
=
4618 add_arg(fninfo
, ARG_SGPR
,
4619 ac_array_in_const32_addr_space(ctx
->v8i32
));
4622 ctx
->param_samplers_and_images
= samplers_and_images
;
4625 static void declare_per_stage_desc_pointers(struct si_shader_context
*ctx
,
4626 struct si_function_info
*fninfo
,
4629 declare_const_and_shader_buffers(ctx
, fninfo
, assign_params
);
4630 declare_samplers_and_images(ctx
, fninfo
, assign_params
);
4633 static void declare_global_desc_pointers(struct si_shader_context
*ctx
,
4634 struct si_function_info
*fninfo
)
4636 ctx
->param_rw_buffers
= add_arg(fninfo
, ARG_SGPR
,
4637 ac_array_in_const32_addr_space(ctx
->v4i32
));
4638 ctx
->param_bindless_samplers_and_images
= add_arg(fninfo
, ARG_SGPR
,
4639 ac_array_in_const32_addr_space(ctx
->v8i32
));
4642 static void declare_vs_specific_input_sgprs(struct si_shader_context
*ctx
,
4643 struct si_function_info
*fninfo
)
4645 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.base_vertex
);
4646 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.start_instance
);
4647 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.draw_id
);
4648 ctx
->param_vs_state_bits
= add_arg(fninfo
, ARG_SGPR
, ctx
->i32
);
4651 static void declare_vs_input_vgprs(struct si_shader_context
*ctx
,
4652 struct si_function_info
*fninfo
,
4653 unsigned *num_prolog_vgprs
)
4655 struct si_shader
*shader
= ctx
->shader
;
4657 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.vertex_id
);
4658 if (shader
->key
.as_ls
) {
4659 ctx
->param_rel_auto_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4660 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4662 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4663 ctx
->param_vs_prim_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4665 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
); /* unused */
4667 if (!shader
->is_gs_copy_shader
) {
4668 /* Vertex load indices. */
4669 ctx
->param_vertex_index0
= fninfo
->num_params
;
4670 for (unsigned i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4671 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4672 *num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
4676 static void declare_tes_input_vgprs(struct si_shader_context
*ctx
,
4677 struct si_function_info
*fninfo
)
4679 ctx
->param_tes_u
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4680 ctx
->param_tes_v
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4681 ctx
->param_tes_rel_patch_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4682 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tes_patch_id
);
4686 /* Convenient merged shader definitions. */
4687 SI_SHADER_MERGED_VERTEX_TESSCTRL
= PIPE_SHADER_TYPES
,
4688 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
,
4691 static void create_function(struct si_shader_context
*ctx
)
4693 struct si_shader
*shader
= ctx
->shader
;
4694 struct si_function_info fninfo
;
4695 LLVMTypeRef returns
[16+32*4];
4696 unsigned i
, num_return_sgprs
;
4697 unsigned num_returns
= 0;
4698 unsigned num_prolog_vgprs
= 0;
4699 unsigned type
= ctx
->type
;
4700 unsigned vs_blit_property
=
4701 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
4703 si_init_function_info(&fninfo
);
4705 /* Set MERGED shaders. */
4706 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
4707 if (shader
->key
.as_ls
|| type
== PIPE_SHADER_TESS_CTRL
)
4708 type
= SI_SHADER_MERGED_VERTEX_TESSCTRL
; /* LS or HS */
4709 else if (shader
->key
.as_es
|| type
== PIPE_SHADER_GEOMETRY
)
4710 type
= SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
;
4713 LLVMTypeRef v3i32
= LLVMVectorType(ctx
->i32
, 3);
4716 case PIPE_SHADER_VERTEX
:
4717 declare_global_desc_pointers(ctx
, &fninfo
);
4719 if (vs_blit_property
) {
4720 ctx
->param_vs_blit_inputs
= fninfo
.num_params
;
4721 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x1, y1 */
4722 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x2, y2 */
4723 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* depth */
4725 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
4726 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color0 */
4727 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color1 */
4728 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color2 */
4729 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color3 */
4730 } else if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
) {
4731 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x1 */
4732 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y1 */
4733 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x2 */
4734 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y2 */
4735 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.z */
4736 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.w */
4740 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4744 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4745 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4746 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4747 ac_array_in_const32_addr_space(ctx
->v4i32
));
4749 if (shader
->key
.as_es
) {
4750 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4751 } else if (shader
->key
.as_ls
) {
4752 /* no extra parameters */
4754 if (shader
->is_gs_copy_shader
) {
4755 fninfo
.num_params
= ctx
->param_rw_buffers
+ 1;
4756 fninfo
.num_sgpr_params
= fninfo
.num_params
;
4759 /* The locations of the other parameters are assigned dynamically. */
4760 declare_streamout_params(ctx
, &shader
->selector
->so
,
4765 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4768 case PIPE_SHADER_TESS_CTRL
: /* SI-CI-VI */
4769 declare_global_desc_pointers(ctx
, &fninfo
);
4770 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4771 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4772 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4773 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4774 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4775 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4776 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4779 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4780 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4782 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4783 * placed after the user SGPRs.
4785 for (i
= 0; i
< GFX6_TCS_NUM_USER_SGPR
+ 2; i
++)
4786 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4787 for (i
= 0; i
< 11; i
++)
4788 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4791 case SI_SHADER_MERGED_VERTEX_TESSCTRL
:
4792 /* Merged stages have 8 system SGPRs at the beginning. */
4793 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
4794 if (HAVE_32BIT_POINTERS
) {
4795 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4796 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4798 declare_const_and_shader_buffers(ctx
, &fninfo
,
4799 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4801 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4802 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4803 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4804 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4805 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4806 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4808 declare_global_desc_pointers(ctx
, &fninfo
);
4809 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4810 ctx
->type
== PIPE_SHADER_VERTEX
);
4811 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4813 if (!HAVE_32BIT_POINTERS
) {
4814 declare_samplers_and_images(ctx
, &fninfo
,
4815 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4817 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4818 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4819 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4820 if (!HAVE_32BIT_POINTERS
) /* Align to 2 dwords. */
4821 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4822 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4823 ac_array_in_const32_addr_space(ctx
->v4i32
));
4825 /* VGPRs (first TCS, then VS) */
4826 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4827 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4829 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4830 declare_vs_input_vgprs(ctx
, &fninfo
,
4833 /* LS return values are inputs to the TCS main shader part. */
4834 for (i
= 0; i
< 8 + GFX9_TCS_NUM_USER_SGPR
; i
++)
4835 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4836 for (i
= 0; i
< 2; i
++)
4837 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4839 /* TCS return values are inputs to the TCS epilog.
4841 * param_tcs_offchip_offset, param_tcs_factor_offset,
4842 * param_tcs_offchip_layout, and param_rw_buffers
4843 * should be passed to the epilog.
4845 for (i
= 0; i
<= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
; i
++)
4846 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4847 for (i
= 0; i
< 11; i
++)
4848 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4852 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
:
4853 /* Merged stages have 8 system SGPRs at the beginning. */
4854 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
4855 if (HAVE_32BIT_POINTERS
) {
4856 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4857 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4859 declare_const_and_shader_buffers(ctx
, &fninfo
,
4860 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4862 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4863 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4864 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4865 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4866 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4867 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4869 declare_global_desc_pointers(ctx
, &fninfo
);
4870 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4871 (ctx
->type
== PIPE_SHADER_VERTEX
||
4872 ctx
->type
== PIPE_SHADER_TESS_EVAL
));
4873 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4874 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4876 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4877 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4878 if (!HAVE_32BIT_POINTERS
) {
4879 /* Declare as many input SGPRs as the VS has. */
4880 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4881 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4885 if (!HAVE_32BIT_POINTERS
) {
4886 declare_samplers_and_images(ctx
, &fninfo
,
4887 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4889 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4890 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4891 ac_array_in_const32_addr_space(ctx
->v4i32
));
4894 /* VGPRs (first GS, then VS/TES) */
4895 ctx
->param_gs_vtx01_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4896 ctx
->param_gs_vtx23_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4897 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4898 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4899 ctx
->param_gs_vtx45_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4901 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4902 declare_vs_input_vgprs(ctx
, &fninfo
,
4904 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4905 declare_tes_input_vgprs(ctx
, &fninfo
);
4908 if (ctx
->type
== PIPE_SHADER_VERTEX
||
4909 ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4910 unsigned num_user_sgprs
;
4912 if (ctx
->type
== PIPE_SHADER_VERTEX
)
4913 num_user_sgprs
= GFX9_VSGS_NUM_USER_SGPR
;
4915 num_user_sgprs
= GFX9_TESGS_NUM_USER_SGPR
;
4917 /* ES return values are inputs to GS. */
4918 for (i
= 0; i
< 8 + num_user_sgprs
; i
++)
4919 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4920 for (i
= 0; i
< 5; i
++)
4921 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4925 case PIPE_SHADER_TESS_EVAL
:
4926 declare_global_desc_pointers(ctx
, &fninfo
);
4927 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4928 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4929 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4931 if (shader
->key
.as_es
) {
4932 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4933 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4934 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4936 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4937 declare_streamout_params(ctx
, &shader
->selector
->so
,
4939 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4943 declare_tes_input_vgprs(ctx
, &fninfo
);
4946 case PIPE_SHADER_GEOMETRY
:
4947 declare_global_desc_pointers(ctx
, &fninfo
);
4948 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4949 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4950 ctx
->param_gs_wave_id
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4953 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]);
4954 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]);
4955 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4956 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
4957 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
4958 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
4959 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
4960 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4963 case PIPE_SHADER_FRAGMENT
:
4964 declare_global_desc_pointers(ctx
, &fninfo
);
4965 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4966 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
4967 add_arg_assign_checked(&fninfo
, ARG_SGPR
, ctx
->i32
,
4968 &ctx
->abi
.prim_mask
, SI_PARAM_PRIM_MASK
);
4970 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_SAMPLE
);
4971 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTER
);
4972 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTROID
);
4973 add_arg_checked(&fninfo
, ARG_VGPR
, v3i32
, SI_PARAM_PERSP_PULL_MODEL
);
4974 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_SAMPLE
);
4975 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTER
);
4976 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTROID
);
4977 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->f32
, SI_PARAM_LINE_STIPPLE_TEX
);
4978 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4979 &ctx
->abi
.frag_pos
[0], SI_PARAM_POS_X_FLOAT
);
4980 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4981 &ctx
->abi
.frag_pos
[1], SI_PARAM_POS_Y_FLOAT
);
4982 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4983 &ctx
->abi
.frag_pos
[2], SI_PARAM_POS_Z_FLOAT
);
4984 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4985 &ctx
->abi
.frag_pos
[3], SI_PARAM_POS_W_FLOAT
);
4986 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4987 &ctx
->abi
.front_face
, SI_PARAM_FRONT_FACE
);
4988 shader
->info
.face_vgpr_index
= 20;
4989 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4990 &ctx
->abi
.ancillary
, SI_PARAM_ANCILLARY
);
4991 shader
->info
.ancillary_vgpr_index
= 21;
4992 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4993 &ctx
->abi
.sample_coverage
, SI_PARAM_SAMPLE_COVERAGE
);
4994 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->i32
, SI_PARAM_POS_FIXED_PT
);
4996 /* Color inputs from the prolog. */
4997 if (shader
->selector
->info
.colors_read
) {
4998 unsigned num_color_elements
=
4999 util_bitcount(shader
->selector
->info
.colors_read
);
5001 assert(fninfo
.num_params
+ num_color_elements
<= ARRAY_SIZE(fninfo
.types
));
5002 for (i
= 0; i
< num_color_elements
; i
++)
5003 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
5005 num_prolog_vgprs
+= num_color_elements
;
5008 /* Outputs for the epilog. */
5009 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
5012 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
5013 shader
->selector
->info
.writes_z
+
5014 shader
->selector
->info
.writes_stencil
+
5015 shader
->selector
->info
.writes_samplemask
+
5016 1 /* SampleMaskIn */;
5018 num_returns
= MAX2(num_returns
,
5020 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5022 for (i
= 0; i
< num_return_sgprs
; i
++)
5023 returns
[i
] = ctx
->i32
;
5024 for (; i
< num_returns
; i
++)
5025 returns
[i
] = ctx
->f32
;
5028 case PIPE_SHADER_COMPUTE
:
5029 declare_global_desc_pointers(ctx
, &fninfo
);
5030 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
5031 if (shader
->selector
->info
.uses_grid_size
)
5032 add_arg_assign(&fninfo
, ARG_SGPR
, v3i32
, &ctx
->abi
.num_work_groups
);
5033 if (shader
->selector
->info
.uses_block_size
)
5034 ctx
->param_block_size
= add_arg(&fninfo
, ARG_SGPR
, v3i32
);
5036 for (i
= 0; i
< 3; i
++) {
5037 ctx
->abi
.workgroup_ids
[i
] = NULL
;
5038 if (shader
->selector
->info
.uses_block_id
[i
])
5039 add_arg_assign(&fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.workgroup_ids
[i
]);
5042 add_arg_assign(&fninfo
, ARG_VGPR
, v3i32
, &ctx
->abi
.local_invocation_ids
);
5045 assert(0 && "unimplemented shader");
5049 si_create_function(ctx
, "main", returns
, num_returns
, &fninfo
,
5050 si_get_max_workgroup_size(shader
));
5052 /* Reserve register locations for VGPR inputs the PS prolog may need. */
5053 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&&
5054 ctx
->separate_prolog
) {
5055 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
5056 "InitialPSInputAddr",
5057 S_0286D0_PERSP_SAMPLE_ENA(1) |
5058 S_0286D0_PERSP_CENTER_ENA(1) |
5059 S_0286D0_PERSP_CENTROID_ENA(1) |
5060 S_0286D0_LINEAR_SAMPLE_ENA(1) |
5061 S_0286D0_LINEAR_CENTER_ENA(1) |
5062 S_0286D0_LINEAR_CENTROID_ENA(1) |
5063 S_0286D0_FRONT_FACE_ENA(1) |
5064 S_0286D0_ANCILLARY_ENA(1) |
5065 S_0286D0_POS_FIXED_PT_ENA(1));
5068 shader
->info
.num_input_sgprs
= 0;
5069 shader
->info
.num_input_vgprs
= 0;
5071 for (i
= 0; i
< fninfo
.num_sgpr_params
; ++i
)
5072 shader
->info
.num_input_sgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5074 for (; i
< fninfo
.num_params
; ++i
)
5075 shader
->info
.num_input_vgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5077 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5078 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5080 if (shader
->key
.as_ls
||
5081 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5082 /* GFX9 has the ESGS ring buffer in LDS. */
5083 type
== SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
)
5084 ac_declare_lds_as_pointer(&ctx
->ac
);
5088 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5091 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5093 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5095 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5096 ctx
->param_rw_buffers
);
5098 if (ctx
->screen
->info
.chip_class
<= VI
&&
5099 (ctx
->shader
->key
.as_es
|| ctx
->type
== PIPE_SHADER_GEOMETRY
)) {
5101 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5103 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, ring
, 0);
5106 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5109 if (ctx
->shader
->is_gs_copy_shader
) {
5110 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5113 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5114 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5115 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5116 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5117 LLVMValueRef base_ring
;
5119 base_ring
= ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5121 /* The conceptual layout of the GSVS ring is
5122 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5123 * but the real memory layout is swizzled across
5125 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5127 * Override the buffer descriptor accordingly.
5129 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5130 uint64_t stream_offset
= 0;
5132 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5133 unsigned num_components
;
5135 unsigned num_records
;
5136 LLVMValueRef ring
, tmp
;
5138 num_components
= sel
->info
.num_stream_output_components
[stream
];
5139 if (!num_components
)
5142 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5144 /* Limit on the stride field for <= CIK. */
5145 assert(stride
< (1 << 14));
5149 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5150 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_0
, "");
5151 tmp
= LLVMBuildAdd(builder
, tmp
,
5152 LLVMConstInt(ctx
->i64
,
5153 stream_offset
, 0), "");
5154 stream_offset
+= stride
* 64;
5156 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_0
, "");
5157 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5158 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_1
, "");
5159 tmp
= LLVMBuildOr(builder
, tmp
,
5160 LLVMConstInt(ctx
->i32
,
5161 S_008F04_STRIDE(stride
) |
5162 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5163 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_1
, "");
5164 ring
= LLVMBuildInsertElement(builder
, ring
,
5165 LLVMConstInt(ctx
->i32
, num_records
, 0),
5166 LLVMConstInt(ctx
->i32
, 2, 0), "");
5167 ring
= LLVMBuildInsertElement(builder
, ring
,
5168 LLVMConstInt(ctx
->i32
,
5169 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5170 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5171 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5172 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5173 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5174 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5175 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5176 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5177 S_008F0C_ADD_TID_ENABLE(1),
5179 LLVMConstInt(ctx
->i32
, 3, 0), "");
5181 ctx
->gsvs_ring
[stream
] = ring
;
5183 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
5184 ctx
->tess_offchip_ring
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TES
);
5188 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5189 LLVMValueRef param_rw_buffers
,
5190 unsigned param_pos_fixed_pt
)
5192 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5193 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5195 /* Use the fixed-point gl_FragCoord input.
5196 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5197 * per coordinate to get the repeating effect.
5199 address
[0] = si_unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5200 address
[1] = si_unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5202 /* Load the buffer descriptor. */
5203 slot
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
5204 desc
= ac_build_load_to_sgpr(&ctx
->ac
, param_rw_buffers
, slot
);
5206 /* The stipple pattern is 32x32, each row has 32 bits. */
5207 offset
= LLVMBuildMul(builder
, address
[1],
5208 LLVMConstInt(ctx
->i32
, 4, 0), "");
5209 row
= buffer_load_const(ctx
, desc
, offset
);
5210 row
= ac_to_integer(&ctx
->ac
, row
);
5211 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5212 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5213 ac_build_kill_if_false(&ctx
->ac
, bit
);
5216 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
5217 struct si_shader_config
*conf
,
5218 unsigned symbol_offset
)
5221 const unsigned char *config
=
5222 ac_shader_binary_config_start(binary
, symbol_offset
);
5223 bool really_needs_scratch
= false;
5225 /* LLVM adds SGPR spills to the scratch size.
5226 * Find out if we really need the scratch buffer.
5228 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5229 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
5231 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5232 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5233 really_needs_scratch
= true;
5238 /* XXX: We may be able to emit some of these values directly rather than
5239 * extracting fields to be emitted later.
5242 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5243 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5244 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5246 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5247 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5248 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5249 case R_00B428_SPI_SHADER_PGM_RSRC1_HS
:
5250 case R_00B848_COMPUTE_PGM_RSRC1
:
5251 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5252 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5253 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5254 conf
->rsrc1
= value
;
5256 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5257 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5259 case R_00B84C_COMPUTE_PGM_RSRC2
:
5260 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5261 conf
->rsrc2
= value
;
5263 case R_0286CC_SPI_PS_INPUT_ENA
:
5264 conf
->spi_ps_input_ena
= value
;
5266 case R_0286D0_SPI_PS_INPUT_ADDR
:
5267 conf
->spi_ps_input_addr
= value
;
5269 case R_0286E8_SPI_TMPRING_SIZE
:
5270 case R_00B860_COMPUTE_TMPRING_SIZE
:
5271 /* WAVESIZE is in units of 256 dwords. */
5272 if (really_needs_scratch
)
5273 conf
->scratch_bytes_per_wave
=
5274 G_00B860_WAVESIZE(value
) * 256 * 4;
5276 case 0x4: /* SPILLED_SGPRS */
5277 conf
->spilled_sgprs
= value
;
5279 case 0x8: /* SPILLED_VGPRS */
5280 conf
->spilled_vgprs
= value
;
5284 static bool printed
;
5287 fprintf(stderr
, "Warning: LLVM emitted unknown "
5288 "config register: 0x%x\n", reg
);
5296 if (!conf
->spi_ps_input_addr
)
5297 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5300 void si_shader_apply_scratch_relocs(struct si_shader
*shader
,
5301 uint64_t scratch_va
)
5304 uint32_t scratch_rsrc_dword0
= scratch_va
;
5305 uint32_t scratch_rsrc_dword1
=
5306 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5308 /* Enable scratch coalescing. */
5309 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5311 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5312 const struct ac_shader_reloc
*reloc
=
5313 &shader
->binary
.relocs
[i
];
5314 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5315 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5316 &scratch_rsrc_dword0
, 4);
5317 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5318 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5319 &scratch_rsrc_dword1
, 4);
5324 /* For the UMR disassembler. */
5325 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
5326 #define DEBUGGER_NUM_MARKERS 5
5328 static unsigned si_get_shader_binary_size(const struct si_shader
*shader
)
5330 unsigned size
= shader
->binary
.code_size
;
5333 size
+= shader
->prolog
->binary
.code_size
;
5334 if (shader
->previous_stage
)
5335 size
+= shader
->previous_stage
->binary
.code_size
;
5336 if (shader
->prolog2
)
5337 size
+= shader
->prolog2
->binary
.code_size
;
5339 size
+= shader
->epilog
->binary
.code_size
;
5340 return size
+ DEBUGGER_NUM_MARKERS
* 4;
5343 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5345 const struct ac_shader_binary
*prolog
=
5346 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
5347 const struct ac_shader_binary
*previous_stage
=
5348 shader
->previous_stage
? &shader
->previous_stage
->binary
: NULL
;
5349 const struct ac_shader_binary
*prolog2
=
5350 shader
->prolog2
? &shader
->prolog2
->binary
: NULL
;
5351 const struct ac_shader_binary
*epilog
=
5352 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
5353 const struct ac_shader_binary
*mainb
= &shader
->binary
;
5354 unsigned bo_size
= si_get_shader_binary_size(shader
) +
5355 (!epilog
? mainb
->rodata_size
: 0);
5358 assert(!prolog
|| !prolog
->rodata_size
);
5359 assert(!previous_stage
|| !previous_stage
->rodata_size
);
5360 assert(!prolog2
|| !prolog2
->rodata_size
);
5361 assert((!prolog
&& !previous_stage
&& !prolog2
&& !epilog
) ||
5362 !mainb
->rodata_size
);
5363 assert(!epilog
|| !epilog
->rodata_size
);
5365 r600_resource_reference(&shader
->bo
, NULL
);
5366 shader
->bo
= si_aligned_buffer_create(&sscreen
->b
,
5367 sscreen
->cpdma_prefetch_writes_memory
?
5368 0 : SI_RESOURCE_FLAG_READ_ONLY
,
5369 PIPE_USAGE_IMMUTABLE
,
5370 align(bo_size
, SI_CPDMA_ALIGNMENT
),
5376 ptr
= sscreen
->ws
->buffer_map(shader
->bo
->buf
, NULL
,
5377 PIPE_TRANSFER_READ_WRITE
|
5378 PIPE_TRANSFER_UNSYNCHRONIZED
);
5380 /* Don't use util_memcpy_cpu_to_le32. LLVM binaries are
5381 * endian-independent. */
5383 memcpy(ptr
, prolog
->code
, prolog
->code_size
);
5384 ptr
+= prolog
->code_size
;
5386 if (previous_stage
) {
5387 memcpy(ptr
, previous_stage
->code
, previous_stage
->code_size
);
5388 ptr
+= previous_stage
->code_size
;
5391 memcpy(ptr
, prolog2
->code
, prolog2
->code_size
);
5392 ptr
+= prolog2
->code_size
;
5395 memcpy(ptr
, mainb
->code
, mainb
->code_size
);
5396 ptr
+= mainb
->code_size
;
5399 memcpy(ptr
, epilog
->code
, epilog
->code_size
);
5400 ptr
+= epilog
->code_size
;
5401 } else if (mainb
->rodata_size
> 0) {
5402 memcpy(ptr
, mainb
->rodata
, mainb
->rodata_size
);
5403 ptr
+= mainb
->rodata_size
;
5406 /* Add end-of-code markers for the UMR disassembler. */
5407 uint32_t *ptr32
= (uint32_t*)ptr
;
5408 for (unsigned i
= 0; i
< DEBUGGER_NUM_MARKERS
; i
++)
5409 ptr32
[i
] = DEBUGGER_END_OF_CODE_MARKER
;
5411 sscreen
->ws
->buffer_unmap(shader
->bo
->buf
);
5415 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
5416 struct pipe_debug_callback
*debug
,
5417 const char *name
, FILE *file
)
5422 if (binary
->disasm_string
) {
5423 fprintf(file
, "Shader %s disassembly:\n", name
);
5424 fprintf(file
, "%s", binary
->disasm_string
);
5426 if (debug
&& debug
->debug_message
) {
5427 /* Very long debug messages are cut off, so send the
5428 * disassembly one line at a time. This causes more
5429 * overhead, but on the plus side it simplifies
5430 * parsing of resulting logs.
5432 pipe_debug_message(debug
, SHADER_INFO
,
5433 "Shader Disassembly Begin");
5435 line
= binary
->disasm_string
;
5437 p
= util_strchrnul(line
, '\n');
5441 pipe_debug_message(debug
, SHADER_INFO
,
5442 "%.*s", count
, line
);
5450 pipe_debug_message(debug
, SHADER_INFO
,
5451 "Shader Disassembly End");
5454 fprintf(file
, "Shader %s binary:\n", name
);
5455 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
5456 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
5457 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
5458 binary
->code
[i
+ 1], binary
->code
[i
]);
5463 static void si_calculate_max_simd_waves(struct si_shader
*shader
)
5465 struct si_screen
*sscreen
= shader
->selector
->screen
;
5466 struct si_shader_config
*conf
= &shader
->config
;
5467 unsigned num_inputs
= shader
->selector
->info
.num_inputs
;
5468 unsigned lds_increment
= sscreen
->info
.chip_class
>= CIK
? 512 : 256;
5469 unsigned lds_per_wave
= 0;
5470 unsigned max_simd_waves
;
5472 max_simd_waves
= ac_get_max_simd_waves(sscreen
->info
.family
);
5474 /* Compute LDS usage for PS. */
5475 switch (shader
->selector
->type
) {
5476 case PIPE_SHADER_FRAGMENT
:
5477 /* The minimum usage per wave is (num_inputs * 48). The maximum
5478 * usage is (num_inputs * 48 * 16).
5479 * We can get anything in between and it varies between waves.
5481 * The 48 bytes per input for a single primitive is equal to
5482 * 4 bytes/component * 4 components/input * 3 points.
5484 * Other stages don't know the size at compile time or don't
5485 * allocate LDS per wave, but instead they do it per thread group.
5487 lds_per_wave
= conf
->lds_size
* lds_increment
+
5488 align(num_inputs
* 48, lds_increment
);
5490 case PIPE_SHADER_COMPUTE
:
5491 if (shader
->selector
) {
5492 unsigned max_workgroup_size
=
5493 si_get_max_workgroup_size(shader
);
5494 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
5495 DIV_ROUND_UP(max_workgroup_size
, 64);
5500 /* Compute the per-SIMD wave counts. */
5501 if (conf
->num_sgprs
) {
5502 if (sscreen
->info
.chip_class
>= VI
)
5503 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
5505 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
5508 if (conf
->num_vgprs
)
5509 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
5511 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
5512 * 16KB makes some SIMDs unoccupied). */
5514 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
5516 conf
->max_simd_waves
= max_simd_waves
;
5519 void si_shader_dump_stats_for_shader_db(const struct si_shader
*shader
,
5520 struct pipe_debug_callback
*debug
)
5522 const struct si_shader_config
*conf
= &shader
->config
;
5524 pipe_debug_message(debug
, SHADER_INFO
,
5525 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
5526 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
5527 "Spilled VGPRs: %d PrivMem VGPRs: %d",
5528 conf
->num_sgprs
, conf
->num_vgprs
,
5529 si_get_shader_binary_size(shader
),
5530 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5531 conf
->max_simd_waves
, conf
->spilled_sgprs
,
5532 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
5535 static void si_shader_dump_stats(struct si_screen
*sscreen
,
5536 const struct si_shader
*shader
,
5539 bool check_debug_option
)
5541 const struct si_shader_config
*conf
= &shader
->config
;
5543 if (!check_debug_option
||
5544 si_can_dump_shader(sscreen
, processor
)) {
5545 if (processor
== PIPE_SHADER_FRAGMENT
) {
5546 fprintf(file
, "*** SHADER CONFIG ***\n"
5547 "SPI_PS_INPUT_ADDR = 0x%04x\n"
5548 "SPI_PS_INPUT_ENA = 0x%04x\n",
5549 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
5552 fprintf(file
, "*** SHADER STATS ***\n"
5555 "Spilled SGPRs: %d\n"
5556 "Spilled VGPRs: %d\n"
5557 "Private memory VGPRs: %d\n"
5558 "Code Size: %d bytes\n"
5560 "Scratch: %d bytes per wave\n"
5562 "********************\n\n\n",
5563 conf
->num_sgprs
, conf
->num_vgprs
,
5564 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
5565 conf
->private_mem_vgprs
,
5566 si_get_shader_binary_size(shader
),
5567 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5568 conf
->max_simd_waves
);
5572 const char *si_get_shader_name(const struct si_shader
*shader
, unsigned processor
)
5574 switch (processor
) {
5575 case PIPE_SHADER_VERTEX
:
5576 if (shader
->key
.as_es
)
5577 return "Vertex Shader as ES";
5578 else if (shader
->key
.as_ls
)
5579 return "Vertex Shader as LS";
5581 return "Vertex Shader as VS";
5582 case PIPE_SHADER_TESS_CTRL
:
5583 return "Tessellation Control Shader";
5584 case PIPE_SHADER_TESS_EVAL
:
5585 if (shader
->key
.as_es
)
5586 return "Tessellation Evaluation Shader as ES";
5588 return "Tessellation Evaluation Shader as VS";
5589 case PIPE_SHADER_GEOMETRY
:
5590 if (shader
->is_gs_copy_shader
)
5591 return "GS Copy Shader as VS";
5593 return "Geometry Shader";
5594 case PIPE_SHADER_FRAGMENT
:
5595 return "Pixel Shader";
5596 case PIPE_SHADER_COMPUTE
:
5597 return "Compute Shader";
5599 return "Unknown Shader";
5603 void si_shader_dump(struct si_screen
*sscreen
, const struct si_shader
*shader
,
5604 struct pipe_debug_callback
*debug
, unsigned processor
,
5605 FILE *file
, bool check_debug_option
)
5607 if (!check_debug_option
||
5608 si_can_dump_shader(sscreen
, processor
))
5609 si_dump_shader_key(processor
, shader
, file
);
5611 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
5612 if (shader
->previous_stage
&&
5613 shader
->previous_stage
->binary
.llvm_ir_string
) {
5614 fprintf(file
, "\n%s - previous stage - LLVM IR:\n\n",
5615 si_get_shader_name(shader
, processor
));
5616 fprintf(file
, "%s\n", shader
->previous_stage
->binary
.llvm_ir_string
);
5619 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
5620 si_get_shader_name(shader
, processor
));
5621 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
5624 if (!check_debug_option
||
5625 (si_can_dump_shader(sscreen
, processor
) &&
5626 !(sscreen
->debug_flags
& DBG(NO_ASM
)))) {
5627 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5630 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5631 debug
, "prolog", file
);
5632 if (shader
->previous_stage
)
5633 si_shader_dump_disassembly(&shader
->previous_stage
->binary
,
5634 debug
, "previous stage", file
);
5635 if (shader
->prolog2
)
5636 si_shader_dump_disassembly(&shader
->prolog2
->binary
,
5637 debug
, "prolog2", file
);
5639 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5642 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5643 debug
, "epilog", file
);
5644 fprintf(file
, "\n");
5647 si_shader_dump_stats(sscreen
, shader
, processor
, file
,
5648 check_debug_option
);
5651 static int si_compile_llvm(struct si_screen
*sscreen
,
5652 struct ac_shader_binary
*binary
,
5653 struct si_shader_config
*conf
,
5654 struct si_compiler
*compiler
,
5656 struct pipe_debug_callback
*debug
,
5661 unsigned count
= p_atomic_inc_return(&sscreen
->num_compilations
);
5663 if (si_can_dump_shader(sscreen
, processor
)) {
5664 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5666 if (!(sscreen
->debug_flags
& (DBG(NO_IR
) | DBG(PREOPT_IR
)))) {
5667 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5668 ac_dump_module(mod
);
5669 fprintf(stderr
, "\n");
5673 if (sscreen
->record_llvm_ir
) {
5674 char *ir
= LLVMPrintModuleToString(mod
);
5675 binary
->llvm_ir_string
= strdup(ir
);
5676 LLVMDisposeMessage(ir
);
5679 if (!si_replace_shader(count
, binary
)) {
5680 r
= si_llvm_compile(mod
, binary
, compiler
, debug
);
5685 si_shader_binary_read_config(binary
, conf
, 0);
5687 /* Enable 64-bit and 16-bit denormals, because there is no performance
5690 * If denormals are enabled, all floating-point output modifiers are
5693 * Don't enable denormals for 32-bit floats, because:
5694 * - Floating-point output modifiers would be ignored by the hw.
5695 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5696 * have to stop using those.
5697 * - SI & CI would be very slow.
5699 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5701 FREE(binary
->config
);
5702 FREE(binary
->global_symbol_offsets
);
5703 binary
->config
= NULL
;
5704 binary
->global_symbol_offsets
= NULL
;
5706 /* Some shaders can't have rodata because their binaries can be
5709 if (binary
->rodata_size
&&
5710 (processor
== PIPE_SHADER_VERTEX
||
5711 processor
== PIPE_SHADER_TESS_CTRL
||
5712 processor
== PIPE_SHADER_TESS_EVAL
||
5713 processor
== PIPE_SHADER_FRAGMENT
)) {
5714 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5721 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
5723 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
5724 LLVMBuildRetVoid(ctx
->ac
.builder
);
5726 LLVMBuildRet(ctx
->ac
.builder
, ret
);
5729 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5731 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5732 struct si_compiler
*compiler
,
5733 struct si_shader_selector
*gs_selector
,
5734 struct pipe_debug_callback
*debug
)
5736 struct si_shader_context ctx
;
5737 struct si_shader
*shader
;
5738 LLVMBuilderRef builder
;
5739 struct lp_build_tgsi_context
*bld_base
= &ctx
.bld_base
;
5740 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5741 struct si_shader_output_values
*outputs
;
5742 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
5745 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
5750 shader
= CALLOC_STRUCT(si_shader
);
5756 /* We can leave the fence as permanently signaled because the GS copy
5757 * shader only becomes visible globally after it has been compiled. */
5758 util_queue_fence_init(&shader
->ready
);
5760 shader
->selector
= gs_selector
;
5761 shader
->is_gs_copy_shader
= true;
5763 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
5764 ctx
.shader
= shader
;
5765 ctx
.type
= PIPE_SHADER_VERTEX
;
5767 builder
= ctx
.ac
.builder
;
5769 create_function(&ctx
);
5770 preload_ring_buffers(&ctx
);
5772 LLVMValueRef voffset
=
5773 lp_build_mul_imm(uint
, ctx
.abi
.vertex_id
, 4);
5775 /* Fetch the vertex stream ID.*/
5776 LLVMValueRef stream_id
;
5778 if (gs_selector
->so
.num_outputs
)
5779 stream_id
= si_unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
5781 stream_id
= ctx
.i32_0
;
5783 /* Fill in output information. */
5784 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5785 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
5786 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
5788 for (int chan
= 0; chan
< 4; chan
++) {
5789 outputs
[i
].vertex_stream
[chan
] =
5790 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
5794 LLVMBasicBlockRef end_bb
;
5795 LLVMValueRef switch_inst
;
5797 end_bb
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, ctx
.main_fn
, "end");
5798 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
5800 for (int stream
= 0; stream
< 4; stream
++) {
5801 LLVMBasicBlockRef bb
;
5804 if (!gsinfo
->num_stream_output_components
[stream
])
5807 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
5810 bb
= LLVMInsertBasicBlockInContext(ctx
.ac
.context
, end_bb
, "out");
5811 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
.i32
, stream
, 0), bb
);
5812 LLVMPositionBuilderAtEnd(builder
, bb
);
5814 /* Fetch vertex data from GSVS ring */
5816 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5817 for (unsigned chan
= 0; chan
< 4; chan
++) {
5818 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
5819 outputs
[i
].vertex_stream
[chan
] != stream
) {
5820 outputs
[i
].values
[chan
] = ctx
.bld_base
.base
.undef
;
5824 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
5825 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
5828 outputs
[i
].values
[chan
] =
5829 ac_build_buffer_load(&ctx
.ac
,
5830 ctx
.gsvs_ring
[0], 1,
5837 /* Streamout and exports. */
5838 if (gs_selector
->so
.num_outputs
) {
5839 si_llvm_emit_streamout(&ctx
, outputs
,
5840 gsinfo
->num_outputs
,
5845 si_llvm_export_vs(&ctx
, outputs
, gsinfo
->num_outputs
);
5847 LLVMBuildBr(builder
, end_bb
);
5850 LLVMPositionBuilderAtEnd(builder
, end_bb
);
5852 LLVMBuildRetVoid(ctx
.ac
.builder
);
5854 ctx
.type
= PIPE_SHADER_GEOMETRY
; /* override for shader dumping */
5855 si_llvm_optimize_module(&ctx
);
5857 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
5858 &ctx
.shader
->config
, ctx
.compiler
,
5860 debug
, PIPE_SHADER_GEOMETRY
,
5863 if (si_can_dump_shader(sscreen
, PIPE_SHADER_GEOMETRY
))
5864 fprintf(stderr
, "GS Copy Shader:\n");
5865 si_shader_dump(sscreen
, ctx
.shader
, debug
,
5866 PIPE_SHADER_GEOMETRY
, stderr
, true);
5867 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
5870 si_llvm_dispose(&ctx
);
5881 static void si_dump_shader_key_vs(const struct si_shader_key
*key
,
5882 const struct si_vs_prolog_bits
*prolog
,
5883 const char *prefix
, FILE *f
)
5885 fprintf(f
, " %s.instance_divisor_is_one = %u\n",
5886 prefix
, prolog
->instance_divisor_is_one
);
5887 fprintf(f
, " %s.instance_divisor_is_fetched = %u\n",
5888 prefix
, prolog
->instance_divisor_is_fetched
);
5889 fprintf(f
, " %s.ls_vgpr_fix = %u\n",
5890 prefix
, prolog
->ls_vgpr_fix
);
5892 fprintf(f
, " mono.vs.fix_fetch = {");
5893 for (int i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
5894 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs_fix_fetch
[i
]);
5898 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
5901 const struct si_shader_key
*key
= &shader
->key
;
5903 fprintf(f
, "SHADER KEY\n");
5905 switch (processor
) {
5906 case PIPE_SHADER_VERTEX
:
5907 si_dump_shader_key_vs(key
, &key
->part
.vs
.prolog
,
5908 "part.vs.prolog", f
);
5909 fprintf(f
, " as_es = %u\n", key
->as_es
);
5910 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
5911 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5912 key
->mono
.u
.vs_export_prim_id
);
5915 case PIPE_SHADER_TESS_CTRL
:
5916 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
) {
5917 si_dump_shader_key_vs(key
, &key
->part
.tcs
.ls_prolog
,
5918 "part.tcs.ls_prolog", f
);
5920 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
5921 fprintf(f
, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.u
.ff_tcs_inputs_to_copy
);
5924 case PIPE_SHADER_TESS_EVAL
:
5925 fprintf(f
, " as_es = %u\n", key
->as_es
);
5926 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5927 key
->mono
.u
.vs_export_prim_id
);
5930 case PIPE_SHADER_GEOMETRY
:
5931 if (shader
->is_gs_copy_shader
)
5934 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
&&
5935 key
->part
.gs
.es
->type
== PIPE_SHADER_VERTEX
) {
5936 si_dump_shader_key_vs(key
, &key
->part
.gs
.vs_prolog
,
5937 "part.gs.vs_prolog", f
);
5939 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
5942 case PIPE_SHADER_COMPUTE
:
5945 case PIPE_SHADER_FRAGMENT
:
5946 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
5947 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
5948 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
5949 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
5950 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
5951 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
5952 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
5953 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
5954 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
5955 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
5956 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
5957 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
5958 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
5959 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
5960 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
5961 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
5962 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
5969 if ((processor
== PIPE_SHADER_GEOMETRY
||
5970 processor
== PIPE_SHADER_TESS_EVAL
||
5971 processor
== PIPE_SHADER_VERTEX
) &&
5972 !key
->as_es
&& !key
->as_ls
) {
5973 fprintf(f
, " opt.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.kill_outputs
);
5974 fprintf(f
, " opt.clip_disable = %u\n", key
->opt
.clip_disable
);
5978 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5979 struct si_screen
*sscreen
,
5980 struct si_compiler
*compiler
)
5982 struct lp_build_tgsi_context
*bld_base
;
5984 si_llvm_context_init(ctx
, sscreen
, compiler
);
5986 bld_base
= &ctx
->bld_base
;
5987 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5989 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
5990 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
5991 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
5993 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
5995 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
5997 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5998 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5999 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
6000 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
6002 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
6003 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
6004 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
6005 bld_base
->op_actions
[TGSI_OPCODE_BALLOT
].emit
= ballot_emit
;
6006 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].intr_name
= "llvm.amdgcn.readfirstlane";
6007 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].emit
= read_lane_emit
;
6008 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].intr_name
= "llvm.amdgcn.readlane";
6009 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].fetch_args
= read_invoc_fetch_args
;
6010 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].emit
= read_lane_emit
;
6012 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_tgsi_emit_vertex
;
6013 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_tgsi_emit_primitive
;
6014 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
6017 static void si_optimize_vs_outputs(struct si_shader_context
*ctx
)
6019 struct si_shader
*shader
= ctx
->shader
;
6020 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6022 if ((ctx
->type
!= PIPE_SHADER_VERTEX
&&
6023 ctx
->type
!= PIPE_SHADER_TESS_EVAL
) ||
6024 shader
->key
.as_ls
||
6028 ac_optimize_vs_outputs(&ctx
->ac
,
6030 shader
->info
.vs_output_param_offset
,
6032 &shader
->info
.nr_param_exports
);
6035 static void si_init_exec_from_input(struct si_shader_context
*ctx
,
6036 unsigned param
, unsigned bitoffset
)
6038 LLVMValueRef args
[] = {
6039 LLVMGetParam(ctx
->main_fn
, param
),
6040 LLVMConstInt(ctx
->i32
, bitoffset
, 0),
6042 lp_build_intrinsic(ctx
->ac
.builder
,
6043 "llvm.amdgcn.init.exec.from.input",
6044 ctx
->voidt
, args
, 2, LP_FUNC_ATTR_CONVERGENT
);
6047 static bool si_vs_needs_prolog(const struct si_shader_selector
*sel
,
6048 const struct si_vs_prolog_bits
*key
)
6050 /* VGPR initialization fixup for Vega10 and Raven is always done in the
6052 return sel
->vs_needs_prolog
|| key
->ls_vgpr_fix
;
6055 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
,
6058 struct si_shader
*shader
= ctx
->shader
;
6059 struct si_shader_selector
*sel
= shader
->selector
;
6060 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6062 // TODO clean all this up!
6063 switch (ctx
->type
) {
6064 case PIPE_SHADER_VERTEX
:
6065 ctx
->load_input
= declare_input_vs
;
6066 if (shader
->key
.as_ls
)
6067 ctx
->abi
.emit_outputs
= si_llvm_emit_ls_epilogue
;
6068 else if (shader
->key
.as_es
)
6069 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6071 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6072 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6073 ctx
->abi
.load_base_vertex
= get_base_vertex
;
6075 case PIPE_SHADER_TESS_CTRL
:
6076 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6077 ctx
->abi
.load_tess_varyings
= si_nir_load_tcs_varyings
;
6078 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6079 bld_base
->emit_store
= store_output_tcs
;
6080 ctx
->abi
.store_tcs_outputs
= si_nir_store_output_tcs
;
6081 ctx
->abi
.emit_outputs
= si_llvm_emit_tcs_epilogue
;
6082 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6083 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6085 case PIPE_SHADER_TESS_EVAL
:
6086 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6087 ctx
->abi
.load_tess_varyings
= si_nir_load_input_tes
;
6088 ctx
->abi
.load_tess_coord
= si_load_tess_coord
;
6089 ctx
->abi
.load_tess_level
= si_load_tess_level
;
6090 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6091 if (shader
->key
.as_es
)
6092 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6094 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6095 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6097 case PIPE_SHADER_GEOMETRY
:
6098 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6099 ctx
->abi
.load_inputs
= si_nir_load_input_gs
;
6100 ctx
->abi
.emit_vertex
= si_llvm_emit_vertex
;
6101 ctx
->abi
.emit_primitive
= si_llvm_emit_primitive
;
6102 ctx
->abi
.emit_outputs
= si_llvm_emit_gs_epilogue
;
6103 bld_base
->emit_epilogue
= si_tgsi_emit_gs_epilogue
;
6105 case PIPE_SHADER_FRAGMENT
:
6106 ctx
->load_input
= declare_input_fs
;
6107 ctx
->abi
.emit_outputs
= si_llvm_return_fs_outputs
;
6108 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6109 ctx
->abi
.lookup_interp_param
= si_nir_lookup_interp_param
;
6110 ctx
->abi
.load_sample_position
= load_sample_position
;
6111 ctx
->abi
.load_sample_mask_in
= load_sample_mask_in
;
6112 ctx
->abi
.emit_kill
= si_llvm_emit_kill
;
6114 case PIPE_SHADER_COMPUTE
:
6115 ctx
->abi
.load_local_group_size
= get_block_size
;
6118 assert(!"Unsupported shader type");
6122 ctx
->abi
.load_ubo
= load_ubo
;
6123 ctx
->abi
.load_ssbo
= load_ssbo
;
6125 create_function(ctx
);
6126 preload_ring_buffers(ctx
);
6128 /* For GFX9 merged shaders:
6129 * - Set EXEC for the first shader. If the prolog is present, set
6130 * EXEC there instead.
6131 * - Add a barrier before the second shader.
6132 * - In the second shader, reset EXEC to ~0 and wrap the main part in
6133 * an if-statement. This is required for correctness in geometry
6134 * shaders, to ensure that empty GS waves do not send GS_EMIT and
6137 * For monolithic merged shaders, the first shader is wrapped in an
6138 * if-block together with its prolog in si_build_wrapper_function.
6140 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6141 if (!is_monolithic
&&
6142 sel
->info
.num_instructions
> 1 && /* not empty shader */
6143 (shader
->key
.as_es
|| shader
->key
.as_ls
) &&
6144 (ctx
->type
== PIPE_SHADER_TESS_EVAL
||
6145 (ctx
->type
== PIPE_SHADER_VERTEX
&&
6146 !si_vs_needs_prolog(sel
, &shader
->key
.part
.vs
.prolog
)))) {
6147 si_init_exec_from_input(ctx
,
6148 ctx
->param_merged_wave_info
, 0);
6149 } else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
6150 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6152 ac_init_exec_full_mask(&ctx
->ac
);
6154 /* The barrier must execute for all shaders in a
6157 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
6159 LLVMValueRef num_threads
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 8, 8);
6161 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
6162 ac_get_thread_id(&ctx
->ac
), num_threads
, "");
6163 lp_build_if(&ctx
->merged_wrap_if_state
, &ctx
->gallivm
, ena
);
6167 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&&
6168 sel
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
6169 for (unsigned i
= 0; i
< 6; i
++) {
6170 ctx
->invoc0_tess_factors
[i
] =
6171 lp_build_alloca_undef(&ctx
->gallivm
, ctx
->i32
, "");
6175 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6177 for (i
= 0; i
< 4; i
++) {
6178 ctx
->gs_next_vertex
[i
] =
6179 lp_build_alloca(&ctx
->gallivm
,
6184 if (sel
->force_correct_derivs_after_kill
) {
6185 ctx
->postponed_kill
= lp_build_alloca_undef(&ctx
->gallivm
, ctx
->i1
, "");
6186 /* true = don't kill. */
6187 LLVMBuildStore(ctx
->ac
.builder
, LLVMConstInt(ctx
->i1
, 1, 0),
6188 ctx
->postponed_kill
);
6192 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6193 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6197 if (!si_nir_build_llvm(ctx
, sel
->nir
)) {
6198 fprintf(stderr
, "Failed to translate shader from NIR to LLVM\n");
6203 si_llvm_build_ret(ctx
, ctx
->return_value
);
6208 * Compute the VS prolog key, which contains all the information needed to
6209 * build the VS prolog function, and set shader->info bits where needed.
6211 * \param info Shader info of the vertex shader.
6212 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
6213 * \param prolog_key Key of the VS prolog
6214 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
6215 * \param key Output shader part key.
6217 static void si_get_vs_prolog_key(const struct tgsi_shader_info
*info
,
6218 unsigned num_input_sgprs
,
6219 const struct si_vs_prolog_bits
*prolog_key
,
6220 struct si_shader
*shader_out
,
6221 union si_shader_part_key
*key
)
6223 memset(key
, 0, sizeof(*key
));
6224 key
->vs_prolog
.states
= *prolog_key
;
6225 key
->vs_prolog
.num_input_sgprs
= num_input_sgprs
;
6226 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6227 key
->vs_prolog
.as_ls
= shader_out
->key
.as_ls
;
6228 key
->vs_prolog
.as_es
= shader_out
->key
.as_es
;
6230 if (shader_out
->selector
->type
== PIPE_SHADER_TESS_CTRL
) {
6231 key
->vs_prolog
.as_ls
= 1;
6232 key
->vs_prolog
.num_merged_next_stage_vgprs
= 2;
6233 } else if (shader_out
->selector
->type
== PIPE_SHADER_GEOMETRY
) {
6234 key
->vs_prolog
.as_es
= 1;
6235 key
->vs_prolog
.num_merged_next_stage_vgprs
= 5;
6238 /* Enable loading the InstanceID VGPR. */
6239 uint16_t input_mask
= u_bit_consecutive(0, info
->num_inputs
);
6241 if ((key
->vs_prolog
.states
.instance_divisor_is_one
|
6242 key
->vs_prolog
.states
.instance_divisor_is_fetched
) & input_mask
)
6243 shader_out
->info
.uses_instanceid
= true;
6247 * Compute the PS prolog key, which contains all the information needed to
6248 * build the PS prolog function, and set related bits in shader->config.
6250 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6251 union si_shader_part_key
*key
,
6252 bool separate_prolog
)
6254 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6256 memset(key
, 0, sizeof(*key
));
6257 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6258 key
->ps_prolog
.colors_read
= info
->colors_read
;
6259 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6260 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6261 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6262 (key
->ps_prolog
.colors_read
||
6263 key
->ps_prolog
.states
.force_persp_sample_interp
||
6264 key
->ps_prolog
.states
.force_linear_sample_interp
||
6265 key
->ps_prolog
.states
.force_persp_center_interp
||
6266 key
->ps_prolog
.states
.force_linear_center_interp
||
6267 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6268 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6269 key
->ps_prolog
.ancillary_vgpr_index
= shader
->info
.ancillary_vgpr_index
;
6271 if (info
->colors_read
) {
6272 unsigned *color
= shader
->selector
->color_attr_index
;
6274 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6275 /* BCOLORs are stored after the last input. */
6276 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6277 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6278 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6281 for (unsigned i
= 0; i
< 2; i
++) {
6282 unsigned interp
= info
->input_interpolate
[color
[i
]];
6283 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6285 if (!(info
->colors_read
& (0xf << i
*4)))
6288 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6290 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6291 interp
== TGSI_INTERPOLATE_COLOR
)
6292 interp
= TGSI_INTERPOLATE_CONSTANT
;
6295 case TGSI_INTERPOLATE_CONSTANT
:
6296 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6298 case TGSI_INTERPOLATE_PERSPECTIVE
:
6299 case TGSI_INTERPOLATE_COLOR
:
6300 /* Force the interpolation location for colors here. */
6301 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6302 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6303 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6304 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6307 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6308 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6309 shader
->config
.spi_ps_input_ena
|=
6310 S_0286CC_PERSP_SAMPLE_ENA(1);
6312 case TGSI_INTERPOLATE_LOC_CENTER
:
6313 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6314 shader
->config
.spi_ps_input_ena
|=
6315 S_0286CC_PERSP_CENTER_ENA(1);
6317 case TGSI_INTERPOLATE_LOC_CENTROID
:
6318 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6319 shader
->config
.spi_ps_input_ena
|=
6320 S_0286CC_PERSP_CENTROID_ENA(1);
6326 case TGSI_INTERPOLATE_LINEAR
:
6327 /* Force the interpolation location for colors here. */
6328 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
6329 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6330 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
6331 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6333 /* The VGPR assignment for non-monolithic shaders
6334 * works because InitialPSInputAddr is set on the
6335 * main shader and PERSP_PULL_MODEL is never used.
6338 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6339 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6340 separate_prolog
? 6 : 9;
6341 shader
->config
.spi_ps_input_ena
|=
6342 S_0286CC_LINEAR_SAMPLE_ENA(1);
6344 case TGSI_INTERPOLATE_LOC_CENTER
:
6345 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6346 separate_prolog
? 8 : 11;
6347 shader
->config
.spi_ps_input_ena
|=
6348 S_0286CC_LINEAR_CENTER_ENA(1);
6350 case TGSI_INTERPOLATE_LOC_CENTROID
:
6351 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6352 separate_prolog
? 10 : 13;
6353 shader
->config
.spi_ps_input_ena
|=
6354 S_0286CC_LINEAR_CENTROID_ENA(1);
6368 * Check whether a PS prolog is required based on the key.
6370 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
6372 return key
->ps_prolog
.colors_read
||
6373 key
->ps_prolog
.states
.force_persp_sample_interp
||
6374 key
->ps_prolog
.states
.force_linear_sample_interp
||
6375 key
->ps_prolog
.states
.force_persp_center_interp
||
6376 key
->ps_prolog
.states
.force_linear_center_interp
||
6377 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6378 key
->ps_prolog
.states
.bc_optimize_for_linear
||
6379 key
->ps_prolog
.states
.poly_stipple
||
6380 key
->ps_prolog
.states
.samplemask_log_ps_iter
;
6384 * Compute the PS epilog key, which contains all the information needed to
6385 * build the PS epilog function.
6387 static void si_get_ps_epilog_key(struct si_shader
*shader
,
6388 union si_shader_part_key
*key
)
6390 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6391 memset(key
, 0, sizeof(*key
));
6392 key
->ps_epilog
.colors_written
= info
->colors_written
;
6393 key
->ps_epilog
.writes_z
= info
->writes_z
;
6394 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
6395 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6396 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
6400 * Build the GS prolog function. Rotate the input vertices for triangle strips
6403 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
6404 union si_shader_part_key
*key
)
6406 unsigned num_sgprs
, num_vgprs
;
6407 struct si_function_info fninfo
;
6408 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6409 LLVMTypeRef returns
[48];
6410 LLVMValueRef func
, ret
;
6412 si_init_function_info(&fninfo
);
6414 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6415 if (key
->gs_prolog
.states
.gfx9_prev_is_vs
)
6416 num_sgprs
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
6418 num_sgprs
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
6419 num_vgprs
= 5; /* ES inputs are not needed by GS */
6421 num_sgprs
= GFX6_GS_NUM_USER_SGPR
+ 2;
6425 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
6426 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
6427 returns
[i
] = ctx
->i32
;
6430 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
6431 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
6432 returns
[num_sgprs
+ i
] = ctx
->f32
;
6435 /* Create the function. */
6436 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
6438 func
= ctx
->main_fn
;
6440 /* Set the full EXEC mask for the prolog, because we are only fiddling
6441 * with registers here. The main shader part will set the correct EXEC
6444 if (ctx
->screen
->info
.chip_class
>= GFX9
&& !key
->gs_prolog
.is_monolithic
)
6445 ac_init_exec_full_mask(&ctx
->ac
);
6447 /* Copy inputs to outputs. This should be no-op, as the registers match,
6448 * but it will prevent the compiler from overwriting them unintentionally.
6450 ret
= ctx
->return_value
;
6451 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
6452 LLVMValueRef p
= LLVMGetParam(func
, i
);
6453 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
6455 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
6456 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
6457 p
= ac_to_float(&ctx
->ac
, p
);
6458 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
6461 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
6462 /* Remap the input vertices for every other primitive. */
6463 const unsigned gfx6_vtx_params
[6] = {
6471 const unsigned gfx9_vtx_params
[3] = {
6476 LLVMValueRef vtx_in
[6], vtx_out
[6];
6477 LLVMValueRef prim_id
, rotate
;
6479 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6480 for (unsigned i
= 0; i
< 3; i
++) {
6481 vtx_in
[i
*2] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 0, 16);
6482 vtx_in
[i
*2+1] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 16, 16);
6485 for (unsigned i
= 0; i
< 6; i
++)
6486 vtx_in
[i
] = LLVMGetParam(func
, gfx6_vtx_params
[i
]);
6489 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
6490 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
6492 for (unsigned i
= 0; i
< 6; ++i
) {
6493 LLVMValueRef base
, rotated
;
6495 rotated
= vtx_in
[(i
+ 4) % 6];
6496 vtx_out
[i
] = LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
6499 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6500 for (unsigned i
= 0; i
< 3; i
++) {
6501 LLVMValueRef hi
, out
;
6503 hi
= LLVMBuildShl(builder
, vtx_out
[i
*2+1],
6504 LLVMConstInt(ctx
->i32
, 16, 0), "");
6505 out
= LLVMBuildOr(builder
, vtx_out
[i
*2], hi
, "");
6506 out
= ac_to_float(&ctx
->ac
, out
);
6507 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6508 gfx9_vtx_params
[i
], "");
6511 for (unsigned i
= 0; i
< 6; i
++) {
6514 out
= ac_to_float(&ctx
->ac
, vtx_out
[i
]);
6515 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6516 gfx6_vtx_params
[i
], "");
6521 LLVMBuildRet(builder
, ret
);
6525 * Given a list of shader part functions, build a wrapper function that
6526 * runs them in sequence to form a monolithic shader.
6528 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
6529 LLVMValueRef
*parts
,
6532 unsigned next_shader_first_part
)
6534 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6535 /* PS epilog has one arg per color component; gfx9 merged shader
6536 * prologs need to forward 32 user SGPRs.
6538 struct si_function_info fninfo
;
6539 LLVMValueRef initial
[64], out
[64];
6540 LLVMTypeRef function_type
;
6541 unsigned num_first_params
;
6542 unsigned num_out
, initial_num_out
;
6543 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
6544 MAYBE_UNUSED
unsigned initial_num_out_sgpr
; /* used in debug checks */
6545 unsigned num_sgprs
, num_vgprs
;
6547 struct lp_build_if_state if_state
;
6549 si_init_function_info(&fninfo
);
6551 for (unsigned i
= 0; i
< num_parts
; ++i
) {
6552 lp_add_function_attr(parts
[i
], -1, LP_FUNC_ATTR_ALWAYSINLINE
);
6553 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
6556 /* The parameters of the wrapper function correspond to those of the
6557 * first part in terms of SGPRs and VGPRs, but we use the types of the
6558 * main part to get the right types. This is relevant for the
6559 * dereferenceable attribute on descriptor table pointers.
6564 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
6565 num_first_params
= LLVMCountParamTypes(function_type
);
6567 for (unsigned i
= 0; i
< num_first_params
; ++i
) {
6568 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
6570 if (ac_is_sgpr_param(param
)) {
6571 assert(num_vgprs
== 0);
6572 num_sgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6574 num_vgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6579 while (gprs
< num_sgprs
+ num_vgprs
) {
6580 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], fninfo
.num_params
);
6581 LLVMTypeRef type
= LLVMTypeOf(param
);
6582 unsigned size
= ac_get_type_size(type
) / 4;
6584 add_arg(&fninfo
, gprs
< num_sgprs
? ARG_SGPR
: ARG_VGPR
, type
);
6586 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
6587 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
6588 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
6593 si_create_function(ctx
, "wrapper", NULL
, 0, &fninfo
,
6594 si_get_max_workgroup_size(ctx
->shader
));
6596 if (is_merged_shader(ctx
->shader
))
6597 ac_init_exec_full_mask(&ctx
->ac
);
6599 /* Record the arguments of the function as if they were an output of
6605 for (unsigned i
= 0; i
< fninfo
.num_params
; ++i
) {
6606 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
6607 LLVMTypeRef param_type
= LLVMTypeOf(param
);
6608 LLVMTypeRef out_type
= i
< fninfo
.num_sgpr_params
? ctx
->i32
: ctx
->f32
;
6609 unsigned size
= ac_get_type_size(param_type
) / 4;
6612 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6613 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i32
, "");
6614 param_type
= ctx
->i32
;
6617 if (param_type
!= out_type
)
6618 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
6619 out
[num_out
++] = param
;
6621 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
6623 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6624 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
6625 param_type
= ctx
->i64
;
6628 if (param_type
!= vector_type
)
6629 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
6631 for (unsigned j
= 0; j
< size
; ++j
)
6632 out
[num_out
++] = LLVMBuildExtractElement(
6633 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
6636 if (i
< fninfo
.num_sgpr_params
)
6637 num_out_sgpr
= num_out
;
6640 memcpy(initial
, out
, sizeof(out
));
6641 initial_num_out
= num_out
;
6642 initial_num_out_sgpr
= num_out_sgpr
;
6644 /* Now chain the parts. */
6645 for (unsigned part
= 0; part
< num_parts
; ++part
) {
6646 LLVMValueRef in
[48];
6648 LLVMTypeRef ret_type
;
6649 unsigned out_idx
= 0;
6650 unsigned num_params
= LLVMCountParams(parts
[part
]);
6652 /* Merged shaders are executed conditionally depending
6653 * on the number of enabled threads passed in the input SGPRs. */
6654 if (is_merged_shader(ctx
->shader
) && part
== 0) {
6655 LLVMValueRef ena
, count
= initial
[3];
6657 count
= LLVMBuildAnd(builder
, count
,
6658 LLVMConstInt(ctx
->i32
, 0x7f, 0), "");
6659 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
6660 ac_get_thread_id(&ctx
->ac
), count
, "");
6661 lp_build_if(&if_state
, &ctx
->gallivm
, ena
);
6664 /* Derive arguments for the next part from outputs of the
6667 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
6669 LLVMTypeRef param_type
;
6671 unsigned param_size
;
6672 LLVMValueRef arg
= NULL
;
6674 param
= LLVMGetParam(parts
[part
], param_idx
);
6675 param_type
= LLVMTypeOf(param
);
6676 param_size
= ac_get_type_size(param_type
) / 4;
6677 is_sgpr
= ac_is_sgpr_param(param
);
6680 lp_add_function_attr(parts
[part
], param_idx
+ 1, LP_FUNC_ATTR_INREG
);
6681 else if (out_idx
< num_out_sgpr
) {
6682 /* Skip returned SGPRs the current part doesn't
6683 * declare on the input. */
6684 out_idx
= num_out_sgpr
;
6687 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
6689 if (param_size
== 1)
6692 arg
= lp_build_gather_values(&ctx
->gallivm
, &out
[out_idx
], param_size
);
6694 if (LLVMTypeOf(arg
) != param_type
) {
6695 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6696 if (LLVMGetPointerAddressSpace(param_type
) ==
6697 AC_CONST_32BIT_ADDR_SPACE
) {
6698 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
6699 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6701 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
6702 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6705 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
6709 in
[param_idx
] = arg
;
6710 out_idx
+= param_size
;
6713 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
6715 if (is_merged_shader(ctx
->shader
) &&
6716 part
+ 1 == next_shader_first_part
) {
6717 lp_build_endif(&if_state
);
6719 /* The second half of the merged shader should use
6720 * the inputs from the toplevel (wrapper) function,
6721 * not the return value from the last call.
6723 * That's because the last call was executed condi-
6724 * tionally, so we can't consume it in the main
6727 memcpy(out
, initial
, sizeof(initial
));
6728 num_out
= initial_num_out
;
6729 num_out_sgpr
= initial_num_out_sgpr
;
6733 /* Extract the returned GPRs. */
6734 ret_type
= LLVMTypeOf(ret
);
6738 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
6739 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
6741 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
6743 for (unsigned i
= 0; i
< ret_size
; ++i
) {
6745 LLVMBuildExtractValue(builder
, ret
, i
, "");
6747 assert(num_out
< ARRAY_SIZE(out
));
6748 out
[num_out
++] = val
;
6750 if (LLVMTypeOf(val
) == ctx
->i32
) {
6751 assert(num_out_sgpr
+ 1 == num_out
);
6752 num_out_sgpr
= num_out
;
6758 LLVMBuildRetVoid(builder
);
6761 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
6762 struct si_compiler
*compiler
,
6763 struct si_shader
*shader
,
6765 struct pipe_debug_callback
*debug
)
6767 struct si_shader_selector
*sel
= shader
->selector
;
6768 struct si_shader_context ctx
;
6771 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6772 * conversion fails. */
6773 if (si_can_dump_shader(sscreen
, sel
->info
.processor
) &&
6774 !(sscreen
->debug_flags
& DBG(NO_TGSI
))) {
6776 tgsi_dump(sel
->tokens
, 0);
6778 nir_print_shader(sel
->nir
, stderr
);
6779 si_dump_streamout(&sel
->so
);
6782 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
6783 si_llvm_context_set_tgsi(&ctx
, shader
);
6784 ctx
.separate_prolog
= !is_monolithic
;
6786 memset(shader
->info
.vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6787 sizeof(shader
->info
.vs_output_param_offset
));
6789 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
6791 if (!si_compile_tgsi_main(&ctx
, is_monolithic
)) {
6792 si_llvm_dispose(&ctx
);
6796 if (is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
6797 LLVMValueRef parts
[2];
6798 bool need_prolog
= sel
->vs_needs_prolog
;
6800 parts
[1] = ctx
.main_fn
;
6803 union si_shader_part_key prolog_key
;
6804 si_get_vs_prolog_key(&sel
->info
,
6805 shader
->info
.num_input_sgprs
,
6806 &shader
->key
.part
.vs
.prolog
,
6807 shader
, &prolog_key
);
6808 si_build_vs_prolog_function(&ctx
, &prolog_key
);
6809 parts
[0] = ctx
.main_fn
;
6812 si_build_wrapper_function(&ctx
, parts
+ !need_prolog
,
6813 1 + need_prolog
, need_prolog
, 0);
6814 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
6815 if (sscreen
->info
.chip_class
>= GFX9
) {
6816 struct si_shader_selector
*ls
= shader
->key
.part
.tcs
.ls
;
6817 LLVMValueRef parts
[4];
6818 bool vs_needs_prolog
=
6819 si_vs_needs_prolog(ls
, &shader
->key
.part
.tcs
.ls_prolog
);
6822 parts
[2] = ctx
.main_fn
;
6825 union si_shader_part_key tcs_epilog_key
;
6826 memset(&tcs_epilog_key
, 0, sizeof(tcs_epilog_key
));
6827 tcs_epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6828 si_build_tcs_epilog_function(&ctx
, &tcs_epilog_key
);
6829 parts
[3] = ctx
.main_fn
;
6831 /* VS as LS main part */
6832 struct si_shader shader_ls
= {};
6833 shader_ls
.selector
= ls
;
6834 shader_ls
.key
.as_ls
= 1;
6835 shader_ls
.key
.mono
= shader
->key
.mono
;
6836 shader_ls
.key
.opt
= shader
->key
.opt
;
6837 si_llvm_context_set_tgsi(&ctx
, &shader_ls
);
6839 if (!si_compile_tgsi_main(&ctx
, true)) {
6840 si_llvm_dispose(&ctx
);
6843 shader
->info
.uses_instanceid
|= ls
->info
.uses_instanceid
;
6844 parts
[1] = ctx
.main_fn
;
6847 if (vs_needs_prolog
) {
6848 union si_shader_part_key vs_prolog_key
;
6849 si_get_vs_prolog_key(&ls
->info
,
6850 shader_ls
.info
.num_input_sgprs
,
6851 &shader
->key
.part
.tcs
.ls_prolog
,
6852 shader
, &vs_prolog_key
);
6853 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6854 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6855 parts
[0] = ctx
.main_fn
;
6858 /* Reset the shader context. */
6859 ctx
.shader
= shader
;
6860 ctx
.type
= PIPE_SHADER_TESS_CTRL
;
6862 si_build_wrapper_function(&ctx
,
6863 parts
+ !vs_needs_prolog
,
6864 4 - !vs_needs_prolog
, vs_needs_prolog
,
6865 vs_needs_prolog
? 2 : 1);
6867 LLVMValueRef parts
[2];
6868 union si_shader_part_key epilog_key
;
6870 parts
[0] = ctx
.main_fn
;
6872 memset(&epilog_key
, 0, sizeof(epilog_key
));
6873 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6874 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
6875 parts
[1] = ctx
.main_fn
;
6877 si_build_wrapper_function(&ctx
, parts
, 2, 0, 0);
6879 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
6880 if (ctx
.screen
->info
.chip_class
>= GFX9
) {
6881 struct si_shader_selector
*es
= shader
->key
.part
.gs
.es
;
6882 LLVMValueRef es_prolog
= NULL
;
6883 LLVMValueRef es_main
= NULL
;
6884 LLVMValueRef gs_prolog
= NULL
;
6885 LLVMValueRef gs_main
= ctx
.main_fn
;
6888 union si_shader_part_key gs_prolog_key
;
6889 memset(&gs_prolog_key
, 0, sizeof(gs_prolog_key
));
6890 gs_prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6891 gs_prolog_key
.gs_prolog
.is_monolithic
= true;
6892 si_build_gs_prolog_function(&ctx
, &gs_prolog_key
);
6893 gs_prolog
= ctx
.main_fn
;
6896 struct si_shader shader_es
= {};
6897 shader_es
.selector
= es
;
6898 shader_es
.key
.as_es
= 1;
6899 shader_es
.key
.mono
= shader
->key
.mono
;
6900 shader_es
.key
.opt
= shader
->key
.opt
;
6901 si_llvm_context_set_tgsi(&ctx
, &shader_es
);
6903 if (!si_compile_tgsi_main(&ctx
, true)) {
6904 si_llvm_dispose(&ctx
);
6907 shader
->info
.uses_instanceid
|= es
->info
.uses_instanceid
;
6908 es_main
= ctx
.main_fn
;
6911 if (es
->vs_needs_prolog
) {
6912 union si_shader_part_key vs_prolog_key
;
6913 si_get_vs_prolog_key(&es
->info
,
6914 shader_es
.info
.num_input_sgprs
,
6915 &shader
->key
.part
.gs
.vs_prolog
,
6916 shader
, &vs_prolog_key
);
6917 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6918 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6919 es_prolog
= ctx
.main_fn
;
6922 /* Reset the shader context. */
6923 ctx
.shader
= shader
;
6924 ctx
.type
= PIPE_SHADER_GEOMETRY
;
6926 /* Prepare the array of shader parts. */
6927 LLVMValueRef parts
[4];
6928 unsigned num_parts
= 0, main_part
, next_first_part
;
6931 parts
[num_parts
++] = es_prolog
;
6933 parts
[main_part
= num_parts
++] = es_main
;
6934 parts
[next_first_part
= num_parts
++] = gs_prolog
;
6935 parts
[num_parts
++] = gs_main
;
6937 si_build_wrapper_function(&ctx
, parts
, num_parts
,
6938 main_part
, next_first_part
);
6940 LLVMValueRef parts
[2];
6941 union si_shader_part_key prolog_key
;
6943 parts
[1] = ctx
.main_fn
;
6945 memset(&prolog_key
, 0, sizeof(prolog_key
));
6946 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6947 si_build_gs_prolog_function(&ctx
, &prolog_key
);
6948 parts
[0] = ctx
.main_fn
;
6950 si_build_wrapper_function(&ctx
, parts
, 2, 1, 0);
6952 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6953 LLVMValueRef parts
[3];
6954 union si_shader_part_key prolog_key
;
6955 union si_shader_part_key epilog_key
;
6958 si_get_ps_prolog_key(shader
, &prolog_key
, false);
6959 need_prolog
= si_need_ps_prolog(&prolog_key
);
6961 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
6964 si_build_ps_prolog_function(&ctx
, &prolog_key
);
6965 parts
[0] = ctx
.main_fn
;
6968 si_get_ps_epilog_key(shader
, &epilog_key
);
6969 si_build_ps_epilog_function(&ctx
, &epilog_key
);
6970 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
6972 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2,
6973 need_prolog
? 1 : 0, 0);
6976 si_llvm_optimize_module(&ctx
);
6978 /* Post-optimization transformations and analysis. */
6979 si_optimize_vs_outputs(&ctx
);
6981 if ((debug
&& debug
->debug_message
) ||
6982 si_can_dump_shader(sscreen
, ctx
.type
)) {
6983 ctx
.shader
->config
.private_mem_vgprs
=
6984 ac_count_scratch_private_memory(ctx
.main_fn
);
6987 /* Make sure the input is a pointer and not integer followed by inttoptr. */
6988 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx
.main_fn
, 0))) ==
6989 LLVMPointerTypeKind
);
6991 /* Compile to bytecode. */
6992 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, compiler
,
6993 ctx
.gallivm
.module
, debug
, ctx
.type
, "TGSI shader");
6994 si_llvm_dispose(&ctx
);
6996 fprintf(stderr
, "LLVM failed to compile shader\n");
7000 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
7001 * LLVM 3.9svn has this bug.
7003 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
7004 unsigned wave_size
= 64;
7005 unsigned max_vgprs
= 256;
7006 unsigned max_sgprs
= sscreen
->info
.chip_class
>= VI
? 800 : 512;
7007 unsigned max_sgprs_per_wave
= 128;
7008 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
7009 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7010 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7012 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7013 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7015 if (shader
->config
.num_sgprs
> max_sgprs
||
7016 shader
->config
.num_vgprs
> max_vgprs
) {
7017 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7018 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7019 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7020 max_sgprs
, max_vgprs
);
7022 /* Just terminate the process, because dependent
7023 * shaders can hang due to bad input data, but use
7024 * the env var to allow shader-db to work.
7026 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7031 /* Add the scratch offset to input SGPRs. */
7032 if (shader
->config
.scratch_bytes_per_wave
&& !is_merged_shader(shader
))
7033 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7035 /* Calculate the number of fragment input VGPRs. */
7036 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7037 shader
->info
.num_input_vgprs
= 0;
7038 shader
->info
.face_vgpr_index
= -1;
7039 shader
->info
.ancillary_vgpr_index
= -1;
7041 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7042 shader
->info
.num_input_vgprs
+= 2;
7043 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7044 shader
->info
.num_input_vgprs
+= 2;
7045 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7046 shader
->info
.num_input_vgprs
+= 2;
7047 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7048 shader
->info
.num_input_vgprs
+= 3;
7049 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7050 shader
->info
.num_input_vgprs
+= 2;
7051 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7052 shader
->info
.num_input_vgprs
+= 2;
7053 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7054 shader
->info
.num_input_vgprs
+= 2;
7055 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7056 shader
->info
.num_input_vgprs
+= 1;
7057 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7058 shader
->info
.num_input_vgprs
+= 1;
7059 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7060 shader
->info
.num_input_vgprs
+= 1;
7061 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7062 shader
->info
.num_input_vgprs
+= 1;
7063 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7064 shader
->info
.num_input_vgprs
+= 1;
7065 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7066 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7067 shader
->info
.num_input_vgprs
+= 1;
7069 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
)) {
7070 shader
->info
.ancillary_vgpr_index
= shader
->info
.num_input_vgprs
;
7071 shader
->info
.num_input_vgprs
+= 1;
7073 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7074 shader
->info
.num_input_vgprs
+= 1;
7075 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7076 shader
->info
.num_input_vgprs
+= 1;
7079 si_calculate_max_simd_waves(shader
);
7080 si_shader_dump_stats_for_shader_db(shader
, debug
);
7085 * Create, compile and return a shader part (prolog or epilog).
7087 * \param sscreen screen
7088 * \param list list of shader parts of the same category
7089 * \param type shader type
7090 * \param key shader part key
7091 * \param prolog whether the part being requested is a prolog
7092 * \param tm LLVM target machine
7093 * \param debug debug callback
7094 * \param build the callback responsible for building the main function
7095 * \return non-NULL on success
7097 static struct si_shader_part
*
7098 si_get_shader_part(struct si_screen
*sscreen
,
7099 struct si_shader_part
**list
,
7100 enum pipe_shader_type type
,
7102 union si_shader_part_key
*key
,
7103 struct si_compiler
*compiler
,
7104 struct pipe_debug_callback
*debug
,
7105 void (*build
)(struct si_shader_context
*,
7106 union si_shader_part_key
*),
7109 struct si_shader_part
*result
;
7111 mtx_lock(&sscreen
->shader_parts_mutex
);
7113 /* Find existing. */
7114 for (result
= *list
; result
; result
= result
->next
) {
7115 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7116 mtx_unlock(&sscreen
->shader_parts_mutex
);
7121 /* Compile a new one. */
7122 result
= CALLOC_STRUCT(si_shader_part
);
7125 struct si_shader shader
= {};
7126 struct si_shader_context ctx
;
7128 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
7129 ctx
.shader
= &shader
;
7133 case PIPE_SHADER_VERTEX
:
7134 shader
.key
.as_ls
= key
->vs_prolog
.as_ls
;
7135 shader
.key
.as_es
= key
->vs_prolog
.as_es
;
7137 case PIPE_SHADER_TESS_CTRL
:
7139 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7141 case PIPE_SHADER_GEOMETRY
:
7144 case PIPE_SHADER_FRAGMENT
:
7146 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7148 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7151 unreachable("bad shader part");
7157 si_llvm_optimize_module(&ctx
);
7159 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, compiler
,
7160 ctx
.ac
.module
, debug
, ctx
.type
, name
)) {
7166 result
->next
= *list
;
7170 si_llvm_dispose(&ctx
);
7171 mtx_unlock(&sscreen
->shader_parts_mutex
);
7175 static LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
7177 LLVMValueRef ptr
[2], list
;
7178 bool is_merged_shader
=
7179 ctx
->screen
->info
.chip_class
>= GFX9
&&
7180 (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
7181 ctx
->type
== PIPE_SHADER_GEOMETRY
||
7182 ctx
->shader
->key
.as_ls
|| ctx
->shader
->key
.as_es
);
7184 if (HAVE_32BIT_POINTERS
) {
7185 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (is_merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7186 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
[0],
7187 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
7191 /* Get the pointer to rw buffers. */
7192 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (is_merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7193 ptr
[1] = LLVMGetParam(ctx
->main_fn
, (is_merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
+ 1);
7194 list
= lp_build_gather_values(&ctx
->gallivm
, ptr
, 2);
7195 list
= LLVMBuildBitCast(ctx
->ac
.builder
, list
, ctx
->i64
, "");
7196 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, list
,
7197 ac_array_in_const_addr_space(ctx
->v4i32
), "");
7202 * Build the vertex shader prolog function.
7204 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7205 * All inputs are returned unmodified. The vertex load indices are
7206 * stored after them, which will be used by the API VS for fetching inputs.
7208 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7213 * (VertexID + BaseVertex),
7214 * (InstanceID + StartInstance),
7215 * (InstanceID / 2 + StartInstance)
7217 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7218 union si_shader_part_key
*key
)
7220 struct si_function_info fninfo
;
7221 LLVMTypeRef
*returns
;
7222 LLVMValueRef ret
, func
;
7224 unsigned first_vs_vgpr
= key
->vs_prolog
.num_merged_next_stage_vgprs
;
7225 unsigned num_input_vgprs
= key
->vs_prolog
.num_merged_next_stage_vgprs
+ 4;
7226 LLVMValueRef input_vgprs
[9];
7227 unsigned num_all_input_regs
= key
->vs_prolog
.num_input_sgprs
+
7229 unsigned user_sgpr_base
= key
->vs_prolog
.num_merged_next_stage_vgprs
? 8 : 0;
7231 si_init_function_info(&fninfo
);
7233 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7234 returns
= alloca((num_all_input_regs
+ key
->vs_prolog
.last_input
+ 1) *
7235 sizeof(LLVMTypeRef
));
7238 /* Declare input and output SGPRs. */
7239 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7240 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7241 returns
[num_returns
++] = ctx
->i32
;
7244 /* Preloaded VGPRs (outputs must be floats) */
7245 for (i
= 0; i
< num_input_vgprs
; i
++) {
7246 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &input_vgprs
[i
]);
7247 returns
[num_returns
++] = ctx
->f32
;
7250 /* Vertex load indices. */
7251 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7252 returns
[num_returns
++] = ctx
->f32
;
7254 /* Create the function. */
7255 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, &fninfo
, 0);
7256 func
= ctx
->main_fn
;
7258 if (key
->vs_prolog
.num_merged_next_stage_vgprs
) {
7259 if (!key
->vs_prolog
.is_monolithic
)
7260 si_init_exec_from_input(ctx
, 3, 0);
7262 if (key
->vs_prolog
.as_ls
&&
7263 ctx
->screen
->has_ls_vgpr_init_bug
) {
7264 /* If there are no HS threads, SPI loads the LS VGPRs
7265 * starting at VGPR 0. Shift them back to where they
7268 LLVMValueRef has_hs_threads
=
7269 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
7270 si_unpack_param(ctx
, 3, 8, 8),
7273 for (i
= 4; i
> 0; --i
) {
7274 input_vgprs
[i
+ 1] =
7275 LLVMBuildSelect(ctx
->ac
.builder
, has_hs_threads
,
7277 input_vgprs
[i
- 1], "");
7282 ctx
->abi
.vertex_id
= input_vgprs
[first_vs_vgpr
];
7283 ctx
->abi
.instance_id
= input_vgprs
[first_vs_vgpr
+ (key
->vs_prolog
.as_ls
? 2 : 1)];
7285 /* Copy inputs to outputs. This should be no-op, as the registers match,
7286 * but it will prevent the compiler from overwriting them unintentionally.
7288 ret
= ctx
->return_value
;
7289 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7290 LLVMValueRef p
= LLVMGetParam(func
, i
);
7291 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7293 for (i
= 0; i
< num_input_vgprs
; i
++) {
7294 LLVMValueRef p
= input_vgprs
[i
];
7295 p
= ac_to_float(&ctx
->ac
, p
);
7296 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
,
7297 key
->vs_prolog
.num_input_sgprs
+ i
, "");
7300 /* Compute vertex load indices from instance divisors. */
7301 LLVMValueRef instance_divisor_constbuf
= NULL
;
7303 if (key
->vs_prolog
.states
.instance_divisor_is_fetched
) {
7304 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7305 LLVMValueRef buf_index
=
7306 LLVMConstInt(ctx
->i32
, SI_VS_CONST_INSTANCE_DIVISORS
, 0);
7307 instance_divisor_constbuf
=
7308 ac_build_load_to_sgpr(&ctx
->ac
, list
, buf_index
);
7311 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7312 bool divisor_is_one
=
7313 key
->vs_prolog
.states
.instance_divisor_is_one
& (1u << i
);
7314 bool divisor_is_fetched
=
7315 key
->vs_prolog
.states
.instance_divisor_is_fetched
& (1u << i
);
7318 if (divisor_is_one
|| divisor_is_fetched
) {
7319 LLVMValueRef divisor
= ctx
->i32_1
;
7321 if (divisor_is_fetched
) {
7322 divisor
= buffer_load_const(ctx
, instance_divisor_constbuf
,
7323 LLVMConstInt(ctx
->i32
, i
* 4, 0));
7324 divisor
= ac_to_integer(&ctx
->ac
, divisor
);
7327 /* InstanceID / Divisor + StartInstance */
7328 index
= get_instance_index_for_fetch(ctx
,
7330 SI_SGPR_START_INSTANCE
,
7333 /* VertexID + BaseVertex */
7334 index
= LLVMBuildAdd(ctx
->ac
.builder
,
7336 LLVMGetParam(func
, user_sgpr_base
+
7337 SI_SGPR_BASE_VERTEX
), "");
7340 index
= ac_to_float(&ctx
->ac
, index
);
7341 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, index
,
7342 fninfo
.num_params
+ i
, "");
7345 si_llvm_build_ret(ctx
, ret
);
7348 static bool si_get_vs_prolog(struct si_screen
*sscreen
,
7349 struct si_compiler
*compiler
,
7350 struct si_shader
*shader
,
7351 struct pipe_debug_callback
*debug
,
7352 struct si_shader
*main_part
,
7353 const struct si_vs_prolog_bits
*key
)
7355 struct si_shader_selector
*vs
= main_part
->selector
;
7357 if (!si_vs_needs_prolog(vs
, key
))
7360 /* Get the prolog. */
7361 union si_shader_part_key prolog_key
;
7362 si_get_vs_prolog_key(&vs
->info
, main_part
->info
.num_input_sgprs
,
7363 key
, shader
, &prolog_key
);
7366 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7367 PIPE_SHADER_VERTEX
, true, &prolog_key
, compiler
,
7368 debug
, si_build_vs_prolog_function
,
7369 "Vertex Shader Prolog");
7370 return shader
->prolog
!= NULL
;
7374 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7376 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7377 struct si_compiler
*compiler
,
7378 struct si_shader
*shader
,
7379 struct pipe_debug_callback
*debug
)
7381 return si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, shader
,
7382 &shader
->key
.part
.vs
.prolog
);
7386 * Compile the TCS epilog function. This writes tesselation factors to memory
7387 * based on the output primitive type of the tesselator (determined by TES).
7389 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7390 union si_shader_part_key
*key
)
7392 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7393 struct si_function_info fninfo
;
7396 si_init_function_info(&fninfo
);
7398 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
7399 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7400 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7401 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7402 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* wave info */
7403 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7404 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7405 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7406 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7407 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7408 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7409 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7410 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7411 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7412 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7413 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7414 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7415 if (!HAVE_32BIT_POINTERS
)
7416 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7417 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7418 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7419 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7421 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7422 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7423 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7424 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7425 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7426 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7427 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7428 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7429 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7430 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7433 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7434 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7435 unsigned tess_factors_idx
=
7436 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* patch index within the wave (REL_PATCH_ID) */
7437 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* invocation ID within the patch */
7438 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* LDS offset where tess factors should be loaded from */
7440 for (unsigned i
= 0; i
< 6; i
++)
7441 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* tess factors */
7443 /* Create the function. */
7444 si_create_function(ctx
, "tcs_epilog", NULL
, 0, &fninfo
,
7445 ctx
->screen
->info
.chip_class
>= CIK
? 128 : 64);
7446 ac_declare_lds_as_pointer(&ctx
->ac
);
7447 func
= ctx
->main_fn
;
7449 LLVMValueRef invoc0_tess_factors
[6];
7450 for (unsigned i
= 0; i
< 6; i
++)
7451 invoc0_tess_factors
[i
] = LLVMGetParam(func
, tess_factors_idx
+ 3 + i
);
7453 si_write_tess_factors(bld_base
,
7454 LLVMGetParam(func
, tess_factors_idx
),
7455 LLVMGetParam(func
, tess_factors_idx
+ 1),
7456 LLVMGetParam(func
, tess_factors_idx
+ 2),
7457 invoc0_tess_factors
, invoc0_tess_factors
+ 4);
7459 LLVMBuildRetVoid(ctx
->ac
.builder
);
7463 * Select and compile (or reuse) TCS parts (epilog).
7465 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7466 struct si_compiler
*compiler
,
7467 struct si_shader
*shader
,
7468 struct pipe_debug_callback
*debug
)
7470 if (sscreen
->info
.chip_class
>= GFX9
) {
7471 struct si_shader
*ls_main_part
=
7472 shader
->key
.part
.tcs
.ls
->main_shader_part_ls
;
7474 if (!si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, ls_main_part
,
7475 &shader
->key
.part
.tcs
.ls_prolog
))
7478 shader
->previous_stage
= ls_main_part
;
7481 /* Get the epilog. */
7482 union si_shader_part_key epilog_key
;
7483 memset(&epilog_key
, 0, sizeof(epilog_key
));
7484 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7486 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7487 PIPE_SHADER_TESS_CTRL
, false,
7488 &epilog_key
, compiler
, debug
,
7489 si_build_tcs_epilog_function
,
7490 "Tessellation Control Shader Epilog");
7491 return shader
->epilog
!= NULL
;
7495 * Select and compile (or reuse) GS parts (prolog).
7497 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7498 struct si_compiler
*compiler
,
7499 struct si_shader
*shader
,
7500 struct pipe_debug_callback
*debug
)
7502 if (sscreen
->info
.chip_class
>= GFX9
) {
7503 struct si_shader
*es_main_part
=
7504 shader
->key
.part
.gs
.es
->main_shader_part_es
;
7506 if (shader
->key
.part
.gs
.es
->type
== PIPE_SHADER_VERTEX
&&
7507 !si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, es_main_part
,
7508 &shader
->key
.part
.gs
.vs_prolog
))
7511 shader
->previous_stage
= es_main_part
;
7514 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7517 union si_shader_part_key prolog_key
;
7518 memset(&prolog_key
, 0, sizeof(prolog_key
));
7519 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7521 shader
->prolog2
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7522 PIPE_SHADER_GEOMETRY
, true,
7523 &prolog_key
, compiler
, debug
,
7524 si_build_gs_prolog_function
,
7525 "Geometry Shader Prolog");
7526 return shader
->prolog2
!= NULL
;
7530 * Build the pixel shader prolog function. This handles:
7531 * - two-side color selection and interpolation
7532 * - overriding interpolation parameters for the API PS
7533 * - polygon stippling
7535 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7536 * overriden by other states. (e.g. per-sample interpolation)
7537 * Interpolated colors are stored after the preloaded VGPRs.
7539 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7540 union si_shader_part_key
*key
)
7542 struct si_function_info fninfo
;
7543 LLVMValueRef ret
, func
;
7544 int num_returns
, i
, num_color_channels
;
7546 assert(si_need_ps_prolog(key
));
7548 si_init_function_info(&fninfo
);
7550 /* Declare inputs. */
7551 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
7552 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7554 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
7555 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7557 /* Declare outputs (same as inputs + add colors if needed) */
7558 num_returns
= fninfo
.num_params
;
7559 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
7560 for (i
= 0; i
< num_color_channels
; i
++)
7561 fninfo
.types
[num_returns
++] = ctx
->f32
;
7563 /* Create the function. */
7564 si_create_function(ctx
, "ps_prolog", fninfo
.types
, num_returns
,
7566 func
= ctx
->main_fn
;
7568 /* Copy inputs to outputs. This should be no-op, as the registers match,
7569 * but it will prevent the compiler from overwriting them unintentionally.
7571 ret
= ctx
->return_value
;
7572 for (i
= 0; i
< fninfo
.num_params
; i
++) {
7573 LLVMValueRef p
= LLVMGetParam(func
, i
);
7574 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7577 /* Polygon stippling. */
7578 if (key
->ps_prolog
.states
.poly_stipple
) {
7579 /* POS_FIXED_PT is always last. */
7580 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
7581 key
->ps_prolog
.num_input_vgprs
- 1;
7582 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7584 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
7587 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
7588 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7589 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7590 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
7592 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7593 * The hw doesn't compute CENTROID if the whole wave only
7594 * contains fully-covered quads.
7596 * PRIM_MASK is after user SGPRs.
7598 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7599 bc_optimize
= LLVMBuildLShr(ctx
->ac
.builder
, bc_optimize
,
7600 LLVMConstInt(ctx
->i32
, 31, 0), "");
7601 bc_optimize
= LLVMBuildTrunc(ctx
->ac
.builder
, bc_optimize
,
7604 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
7605 /* Read PERSP_CENTER. */
7606 for (i
= 0; i
< 2; i
++)
7607 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7608 /* Read PERSP_CENTROID. */
7609 for (i
= 0; i
< 2; i
++)
7610 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
7611 /* Select PERSP_CENTROID. */
7612 for (i
= 0; i
< 2; i
++) {
7613 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7614 center
[i
], centroid
[i
], "");
7615 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7616 tmp
, base
+ 4 + i
, "");
7619 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7620 /* Read LINEAR_CENTER. */
7621 for (i
= 0; i
< 2; i
++)
7622 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7623 /* Read LINEAR_CENTROID. */
7624 for (i
= 0; i
< 2; i
++)
7625 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
7626 /* Select LINEAR_CENTROID. */
7627 for (i
= 0; i
< 2; i
++) {
7628 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7629 center
[i
], centroid
[i
], "");
7630 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7631 tmp
, base
+ 10 + i
, "");
7636 /* Force per-sample interpolation. */
7637 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
7638 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7639 LLVMValueRef persp_sample
[2];
7641 /* Read PERSP_SAMPLE. */
7642 for (i
= 0; i
< 2; i
++)
7643 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
7644 /* Overwrite PERSP_CENTER. */
7645 for (i
= 0; i
< 2; i
++)
7646 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7647 persp_sample
[i
], base
+ 2 + i
, "");
7648 /* Overwrite PERSP_CENTROID. */
7649 for (i
= 0; i
< 2; i
++)
7650 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7651 persp_sample
[i
], base
+ 4 + i
, "");
7653 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
7654 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7655 LLVMValueRef linear_sample
[2];
7657 /* Read LINEAR_SAMPLE. */
7658 for (i
= 0; i
< 2; i
++)
7659 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
7660 /* Overwrite LINEAR_CENTER. */
7661 for (i
= 0; i
< 2; i
++)
7662 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7663 linear_sample
[i
], base
+ 8 + i
, "");
7664 /* Overwrite LINEAR_CENTROID. */
7665 for (i
= 0; i
< 2; i
++)
7666 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7667 linear_sample
[i
], base
+ 10 + i
, "");
7670 /* Force center interpolation. */
7671 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
7672 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7673 LLVMValueRef persp_center
[2];
7675 /* Read PERSP_CENTER. */
7676 for (i
= 0; i
< 2; i
++)
7677 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7678 /* Overwrite PERSP_SAMPLE. */
7679 for (i
= 0; i
< 2; i
++)
7680 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7681 persp_center
[i
], base
+ i
, "");
7682 /* Overwrite PERSP_CENTROID. */
7683 for (i
= 0; i
< 2; i
++)
7684 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7685 persp_center
[i
], base
+ 4 + i
, "");
7687 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
7688 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7689 LLVMValueRef linear_center
[2];
7691 /* Read LINEAR_CENTER. */
7692 for (i
= 0; i
< 2; i
++)
7693 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7694 /* Overwrite LINEAR_SAMPLE. */
7695 for (i
= 0; i
< 2; i
++)
7696 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7697 linear_center
[i
], base
+ 6 + i
, "");
7698 /* Overwrite LINEAR_CENTROID. */
7699 for (i
= 0; i
< 2; i
++)
7700 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7701 linear_center
[i
], base
+ 10 + i
, "");
7704 /* Interpolate colors. */
7705 unsigned color_out_idx
= 0;
7706 for (i
= 0; i
< 2; i
++) {
7707 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
7708 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7709 key
->ps_prolog
.face_vgpr_index
;
7710 LLVMValueRef interp
[2], color
[4];
7711 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
7716 /* If the interpolation qualifier is not CONSTANT (-1). */
7717 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
7718 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7719 key
->ps_prolog
.color_interp_vgpr_index
[i
];
7721 /* Get the (i,j) updated by bc_optimize handling. */
7722 interp
[0] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7724 interp
[1] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7725 interp_vgpr
+ 1, "");
7726 interp_ij
= lp_build_gather_values(&ctx
->gallivm
, interp
, 2);
7729 /* Use the absolute location of the input. */
7730 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7732 if (key
->ps_prolog
.states
.color_two_side
) {
7733 face
= LLVMGetParam(func
, face_vgpr
);
7734 face
= ac_to_integer(&ctx
->ac
, face
);
7737 interp_fs_input(ctx
,
7738 key
->ps_prolog
.color_attr_index
[i
],
7739 TGSI_SEMANTIC_COLOR
, i
,
7740 key
->ps_prolog
.num_interp_inputs
,
7741 key
->ps_prolog
.colors_read
, interp_ij
,
7742 prim_mask
, face
, color
);
7745 unsigned chan
= u_bit_scan(&writemask
);
7746 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, color
[chan
],
7747 fninfo
.num_params
+ color_out_idx
++, "");
7751 /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
7754 * "When per-sample shading is active due to the use of a fragment
7755 * input qualified by sample or due to the use of the gl_SampleID
7756 * or gl_SamplePosition variables, only the bit for the current
7757 * sample is set in gl_SampleMaskIn. When state specifies multiple
7758 * fragment shader invocations for a given fragment, the sample
7759 * mask for any single fragment shader invocation may specify a
7760 * subset of the covered samples for the fragment. In this case,
7761 * the bit corresponding to each covered sample will be set in
7762 * exactly one fragment shader invocation."
7764 * The samplemask loaded by hardware is always the coverage of the
7765 * entire pixel/fragment, so mask bits out based on the sample ID.
7767 if (key
->ps_prolog
.states
.samplemask_log_ps_iter
) {
7768 /* The bit pattern matches that used by fixed function fragment
7770 static const uint16_t ps_iter_masks
[] = {
7771 0xffff, /* not used */
7777 assert(key
->ps_prolog
.states
.samplemask_log_ps_iter
< ARRAY_SIZE(ps_iter_masks
));
7779 uint32_t ps_iter_mask
= ps_iter_masks
[key
->ps_prolog
.states
.samplemask_log_ps_iter
];
7780 unsigned ancillary_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7781 key
->ps_prolog
.ancillary_vgpr_index
;
7782 LLVMValueRef sampleid
= si_unpack_param(ctx
, ancillary_vgpr
, 8, 4);
7783 LLVMValueRef samplemask
= LLVMGetParam(func
, ancillary_vgpr
+ 1);
7785 samplemask
= ac_to_integer(&ctx
->ac
, samplemask
);
7786 samplemask
= LLVMBuildAnd(
7789 LLVMBuildShl(ctx
->ac
.builder
,
7790 LLVMConstInt(ctx
->i32
, ps_iter_mask
, false),
7793 samplemask
= ac_to_float(&ctx
->ac
, samplemask
);
7795 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, samplemask
,
7796 ancillary_vgpr
+ 1, "");
7799 /* Tell LLVM to insert WQM instruction sequence when needed. */
7800 if (key
->ps_prolog
.wqm
) {
7801 LLVMAddTargetDependentFunctionAttr(func
,
7802 "amdgpu-ps-wqm-outputs", "");
7805 si_llvm_build_ret(ctx
, ret
);
7809 * Build the pixel shader epilog function. This handles everything that must be
7810 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7812 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
7813 union si_shader_part_key
*key
)
7815 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7816 struct si_function_info fninfo
;
7817 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
7819 struct si_ps_exports exp
= {};
7821 si_init_function_info(&fninfo
);
7823 /* Declare input SGPRs. */
7824 ctx
->param_rw_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7825 ctx
->param_bindless_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7826 ctx
->param_const_and_shader_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7827 ctx
->param_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7828 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
7830 /* Declare input VGPRs. */
7831 unsigned required_num_params
=
7832 fninfo
.num_sgpr_params
+
7833 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
7834 key
->ps_epilog
.writes_z
+
7835 key
->ps_epilog
.writes_stencil
+
7836 key
->ps_epilog
.writes_samplemask
;
7838 required_num_params
= MAX2(required_num_params
,
7839 fninfo
.num_sgpr_params
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
7841 while (fninfo
.num_params
< required_num_params
)
7842 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7844 /* Create the function. */
7845 si_create_function(ctx
, "ps_epilog", NULL
, 0, &fninfo
, 0);
7846 /* Disable elimination of unused inputs. */
7847 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
7848 "InitialPSInputAddr", 0xffffff);
7850 /* Process colors. */
7851 unsigned vgpr
= fninfo
.num_sgpr_params
;
7852 unsigned colors_written
= key
->ps_epilog
.colors_written
;
7853 int last_color_export
= -1;
7855 /* Find the last color export. */
7856 if (!key
->ps_epilog
.writes_z
&&
7857 !key
->ps_epilog
.writes_stencil
&&
7858 !key
->ps_epilog
.writes_samplemask
) {
7859 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
7861 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7862 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
7863 /* Just set this if any of the colorbuffers are enabled. */
7865 ((1ull << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
7866 last_color_export
= 0;
7868 for (i
= 0; i
< 8; i
++)
7869 if (colors_written
& (1 << i
) &&
7870 (spi_format
>> (i
* 4)) & 0xf)
7871 last_color_export
= i
;
7875 while (colors_written
) {
7876 LLVMValueRef color
[4];
7877 int mrt
= u_bit_scan(&colors_written
);
7879 for (i
= 0; i
< 4; i
++)
7880 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
7882 si_export_mrt_color(bld_base
, color
, mrt
,
7883 fninfo
.num_params
- 1,
7884 mrt
== last_color_export
, &exp
);
7887 /* Process depth, stencil, samplemask. */
7888 if (key
->ps_epilog
.writes_z
)
7889 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7890 if (key
->ps_epilog
.writes_stencil
)
7891 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7892 if (key
->ps_epilog
.writes_samplemask
)
7893 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7895 if (depth
|| stencil
|| samplemask
)
7896 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
7897 else if (last_color_export
== -1)
7898 ac_build_export_null(&ctx
->ac
);
7901 si_emit_ps_exports(ctx
, &exp
);
7904 LLVMBuildRetVoid(ctx
->ac
.builder
);
7908 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7910 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
7911 struct si_compiler
*compiler
,
7912 struct si_shader
*shader
,
7913 struct pipe_debug_callback
*debug
)
7915 union si_shader_part_key prolog_key
;
7916 union si_shader_part_key epilog_key
;
7918 /* Get the prolog. */
7919 si_get_ps_prolog_key(shader
, &prolog_key
, true);
7921 /* The prolog is a no-op if these aren't set. */
7922 if (si_need_ps_prolog(&prolog_key
)) {
7924 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
7925 PIPE_SHADER_FRAGMENT
, true,
7926 &prolog_key
, compiler
, debug
,
7927 si_build_ps_prolog_function
,
7928 "Fragment Shader Prolog");
7929 if (!shader
->prolog
)
7933 /* Get the epilog. */
7934 si_get_ps_epilog_key(shader
, &epilog_key
);
7937 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
7938 PIPE_SHADER_FRAGMENT
, false,
7939 &epilog_key
, compiler
, debug
,
7940 si_build_ps_epilog_function
,
7941 "Fragment Shader Epilog");
7942 if (!shader
->epilog
)
7945 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
7946 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
7947 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
7948 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
7951 /* Set up the enable bits for per-sample shading if needed. */
7952 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
7953 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7954 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7955 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
7956 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7957 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
7959 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
7960 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7961 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7962 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
7963 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7964 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
7966 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
7967 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7968 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7969 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
7970 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7971 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7973 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
7974 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7975 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7976 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
7977 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7978 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7981 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
7982 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
7983 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
7984 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7985 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7988 /* At least one pair of interpolation weights must be enabled. */
7989 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
7990 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7991 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7994 /* Samplemask fixup requires the sample ID. */
7995 if (shader
->key
.part
.ps
.prolog
.samplemask_log_ps_iter
) {
7996 shader
->config
.spi_ps_input_ena
|= S_0286CC_ANCILLARY_ENA(1);
7997 assert(G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
));
8000 /* The sample mask input is always enabled, because the API shader always
8001 * passes it through to the epilog. Disable it here if it's unused.
8003 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
8004 !shader
->selector
->info
.reads_samplemask
)
8005 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
8010 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
8013 /* If tessellation is all offchip and on-chip GS isn't used, this
8014 * workaround is not needed.
8018 /* SPI barrier management bug:
8019 * Make sure we have at least 4k of LDS in use to avoid the bug.
8020 * It applies to workgroup sizes of more than one wavefront.
8022 if (sscreen
->info
.family
== CHIP_BONAIRE
||
8023 sscreen
->info
.family
== CHIP_KABINI
||
8024 sscreen
->info
.family
== CHIP_MULLINS
)
8025 *lds_size
= MAX2(*lds_size
, 8);
8028 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8029 struct si_shader
*shader
)
8031 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8033 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8035 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8036 si_get_max_workgroup_size(shader
) > 64) {
8037 si_multiwave_lds_size_workaround(sscreen
,
8038 &shader
->config
.lds_size
);
8042 int si_shader_create(struct si_screen
*sscreen
, struct si_compiler
*compiler
,
8043 struct si_shader
*shader
,
8044 struct pipe_debug_callback
*debug
)
8046 struct si_shader_selector
*sel
= shader
->selector
;
8047 struct si_shader
*mainp
= *si_get_main_shader_part(sel
, &shader
->key
);
8050 /* LS, ES, VS are compiled on demand if the main part hasn't been
8051 * compiled for that stage.
8053 * Vertex shaders are compiled on demand when a vertex fetch
8054 * workaround must be applied.
8056 if (shader
->is_monolithic
) {
8057 /* Monolithic shader (compiled as a whole, has many variants,
8058 * may take a long time to compile).
8060 r
= si_compile_tgsi_shader(sscreen
, compiler
, shader
, true, debug
);
8064 /* The shader consists of several parts:
8066 * - the middle part is the user shader, it has 1 variant only
8067 * and it was compiled during the creation of the shader
8069 * - the prolog part is inserted at the beginning
8070 * - the epilog part is inserted at the end
8072 * The prolog and epilog have many (but simple) variants.
8074 * Starting with gfx9, geometry and tessellation control
8075 * shaders also contain the prolog and user shader parts of
8076 * the previous shader stage.
8082 /* Copy the compiled TGSI shader data over. */
8083 shader
->is_binary_shared
= true;
8084 shader
->binary
= mainp
->binary
;
8085 shader
->config
= mainp
->config
;
8086 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8087 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8088 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8089 shader
->info
.ancillary_vgpr_index
= mainp
->info
.ancillary_vgpr_index
;
8090 memcpy(shader
->info
.vs_output_param_offset
,
8091 mainp
->info
.vs_output_param_offset
,
8092 sizeof(mainp
->info
.vs_output_param_offset
));
8093 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8094 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8095 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8097 /* Select prologs and/or epilogs. */
8098 switch (sel
->type
) {
8099 case PIPE_SHADER_VERTEX
:
8100 if (!si_shader_select_vs_parts(sscreen
, compiler
, shader
, debug
))
8103 case PIPE_SHADER_TESS_CTRL
:
8104 if (!si_shader_select_tcs_parts(sscreen
, compiler
, shader
, debug
))
8107 case PIPE_SHADER_TESS_EVAL
:
8109 case PIPE_SHADER_GEOMETRY
:
8110 if (!si_shader_select_gs_parts(sscreen
, compiler
, shader
, debug
))
8113 case PIPE_SHADER_FRAGMENT
:
8114 if (!si_shader_select_ps_parts(sscreen
, compiler
, shader
, debug
))
8117 /* Make sure we have at least as many VGPRs as there
8118 * are allocated inputs.
8120 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8121 shader
->info
.num_input_vgprs
);
8125 /* Update SGPR and VGPR counts. */
8126 if (shader
->prolog
) {
8127 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8128 shader
->prolog
->config
.num_sgprs
);
8129 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8130 shader
->prolog
->config
.num_vgprs
);
8132 if (shader
->previous_stage
) {
8133 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8134 shader
->previous_stage
->config
.num_sgprs
);
8135 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8136 shader
->previous_stage
->config
.num_vgprs
);
8137 shader
->config
.spilled_sgprs
=
8138 MAX2(shader
->config
.spilled_sgprs
,
8139 shader
->previous_stage
->config
.spilled_sgprs
);
8140 shader
->config
.spilled_vgprs
=
8141 MAX2(shader
->config
.spilled_vgprs
,
8142 shader
->previous_stage
->config
.spilled_vgprs
);
8143 shader
->config
.private_mem_vgprs
=
8144 MAX2(shader
->config
.private_mem_vgprs
,
8145 shader
->previous_stage
->config
.private_mem_vgprs
);
8146 shader
->config
.scratch_bytes_per_wave
=
8147 MAX2(shader
->config
.scratch_bytes_per_wave
,
8148 shader
->previous_stage
->config
.scratch_bytes_per_wave
);
8149 shader
->info
.uses_instanceid
|=
8150 shader
->previous_stage
->info
.uses_instanceid
;
8152 if (shader
->prolog2
) {
8153 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8154 shader
->prolog2
->config
.num_sgprs
);
8155 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8156 shader
->prolog2
->config
.num_vgprs
);
8158 if (shader
->epilog
) {
8159 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8160 shader
->epilog
->config
.num_sgprs
);
8161 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8162 shader
->epilog
->config
.num_vgprs
);
8164 si_calculate_max_simd_waves(shader
);
8167 si_fix_resource_usage(sscreen
, shader
);
8168 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8172 r
= si_shader_binary_upload(sscreen
, shader
);
8174 fprintf(stderr
, "LLVM failed to upload shader\n");
8181 void si_shader_destroy(struct si_shader
*shader
)
8183 if (shader
->scratch_bo
)
8184 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8186 r600_resource_reference(&shader
->bo
, NULL
);
8188 if (!shader
->is_binary_shared
)
8189 ac_shader_binary_clean(&shader
->binary
);
8191 free(shader
->shader_log
);