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 "util/u_memory.h"
26 #include "util/u_string.h"
27 #include "tgsi/tgsi_build.h"
28 #include "tgsi/tgsi_util.h"
29 #include "tgsi/tgsi_dump.h"
31 #include "ac_exp_param.h"
32 #include "ac_shader_util.h"
33 #include "ac_llvm_util.h"
34 #include "si_shader_internal.h"
38 #include "compiler/nir/nir.h"
40 static const char *scratch_rsrc_dword0_symbol
=
41 "SCRATCH_RSRC_DWORD0";
43 static const char *scratch_rsrc_dword1_symbol
=
44 "SCRATCH_RSRC_DWORD1";
46 struct si_shader_output_values
48 LLVMValueRef values
[4];
49 unsigned semantic_name
;
50 unsigned semantic_index
;
51 ubyte vertex_stream
[4];
55 * Used to collect types and other info about arguments of the LLVM function
56 * before the function is created.
58 struct si_function_info
{
59 LLVMTypeRef types
[100];
60 LLVMValueRef
*assign
[100];
61 unsigned num_sgpr_params
;
70 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
71 struct si_screen
*sscreen
,
72 struct ac_llvm_compiler
*compiler
);
74 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
75 struct lp_build_tgsi_context
*bld_base
,
76 struct lp_build_emit_data
*emit_data
);
78 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
81 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
82 union si_shader_part_key
*key
);
83 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
84 union si_shader_part_key
*key
);
85 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
86 union si_shader_part_key
*key
);
87 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
88 union si_shader_part_key
*key
);
90 /* Ideally pass the sample mask input to the PS epilog as v14, which
91 * is its usual location, so that the shader doesn't have to add v_mov.
93 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 14
95 static bool llvm_type_is_64bit(struct si_shader_context
*ctx
,
98 if (type
== ctx
->ac
.i64
|| type
== ctx
->ac
.f64
)
104 static bool is_merged_shader(struct si_shader_context
*ctx
)
106 if (ctx
->screen
->info
.chip_class
<= VI
)
109 return ctx
->shader
->key
.as_ls
||
110 ctx
->shader
->key
.as_es
||
111 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
112 ctx
->type
== PIPE_SHADER_GEOMETRY
;
115 static void si_init_function_info(struct si_function_info
*fninfo
)
117 fninfo
->num_params
= 0;
118 fninfo
->num_sgpr_params
= 0;
121 static unsigned add_arg_assign(struct si_function_info
*fninfo
,
122 enum si_arg_regfile regfile
, LLVMTypeRef type
,
123 LLVMValueRef
*assign
)
125 assert(regfile
!= ARG_SGPR
|| fninfo
->num_sgpr_params
== fninfo
->num_params
);
127 unsigned idx
= fninfo
->num_params
++;
128 assert(idx
< ARRAY_SIZE(fninfo
->types
));
130 if (regfile
== ARG_SGPR
)
131 fninfo
->num_sgpr_params
= fninfo
->num_params
;
133 fninfo
->types
[idx
] = type
;
134 fninfo
->assign
[idx
] = assign
;
138 static unsigned add_arg(struct si_function_info
*fninfo
,
139 enum si_arg_regfile regfile
, LLVMTypeRef type
)
141 return add_arg_assign(fninfo
, regfile
, type
, NULL
);
144 static void add_arg_assign_checked(struct si_function_info
*fninfo
,
145 enum si_arg_regfile regfile
, LLVMTypeRef type
,
146 LLVMValueRef
*assign
, unsigned idx
)
148 MAYBE_UNUSED
unsigned actual
= add_arg_assign(fninfo
, regfile
, type
, assign
);
149 assert(actual
== idx
);
152 static void add_arg_checked(struct si_function_info
*fninfo
,
153 enum si_arg_regfile regfile
, LLVMTypeRef type
,
156 add_arg_assign_checked(fninfo
, regfile
, type
, NULL
, idx
);
160 * Returns a unique index for a per-patch semantic name and index. The index
161 * must be less than 32, so that a 32-bit bitmask of used inputs or outputs
164 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name
, unsigned index
)
166 switch (semantic_name
) {
167 case TGSI_SEMANTIC_TESSOUTER
:
169 case TGSI_SEMANTIC_TESSINNER
:
171 case TGSI_SEMANTIC_PATCH
:
176 assert(!"invalid semantic name");
182 * Returns a unique index for a semantic name and index. The index must be
183 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
186 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
,
189 switch (semantic_name
) {
190 case TGSI_SEMANTIC_POSITION
:
192 case TGSI_SEMANTIC_GENERIC
:
193 /* Since some shader stages use the the highest used IO index
194 * to determine the size to allocate for inputs/outputs
195 * (in LDS, tess and GS rings). GENERIC should be placed right
196 * after POSITION to make that size as small as possible.
198 if (index
< SI_MAX_IO_GENERIC
)
201 assert(!"invalid generic index");
203 case TGSI_SEMANTIC_PSIZE
:
204 return SI_MAX_IO_GENERIC
+ 1;
205 case TGSI_SEMANTIC_CLIPDIST
:
207 return SI_MAX_IO_GENERIC
+ 2 + index
;
208 case TGSI_SEMANTIC_FOG
:
209 return SI_MAX_IO_GENERIC
+ 4;
210 case TGSI_SEMANTIC_LAYER
:
211 return SI_MAX_IO_GENERIC
+ 5;
212 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
213 return SI_MAX_IO_GENERIC
+ 6;
214 case TGSI_SEMANTIC_PRIMID
:
215 return SI_MAX_IO_GENERIC
+ 7;
216 case TGSI_SEMANTIC_COLOR
:
218 return SI_MAX_IO_GENERIC
+ 8 + index
;
219 case TGSI_SEMANTIC_BCOLOR
:
221 /* If it's a varying, COLOR and BCOLOR alias. */
223 return SI_MAX_IO_GENERIC
+ 8 + index
;
225 return SI_MAX_IO_GENERIC
+ 10 + index
;
226 case TGSI_SEMANTIC_TEXCOORD
:
228 STATIC_ASSERT(SI_MAX_IO_GENERIC
+ 12 + 8 <= 63);
229 return SI_MAX_IO_GENERIC
+ 12 + index
;
230 case TGSI_SEMANTIC_CLIPVERTEX
:
233 fprintf(stderr
, "invalid semantic name = %u\n", semantic_name
);
234 assert(!"invalid semantic name");
240 * Get the value of a shader input parameter and extract a bitfield.
242 static LLVMValueRef
unpack_llvm_param(struct si_shader_context
*ctx
,
243 LLVMValueRef value
, unsigned rshift
,
246 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
247 value
= ac_to_integer(&ctx
->ac
, value
);
250 value
= LLVMBuildLShr(ctx
->ac
.builder
, value
,
251 LLVMConstInt(ctx
->i32
, rshift
, 0), "");
253 if (rshift
+ bitwidth
< 32) {
254 unsigned mask
= (1 << bitwidth
) - 1;
255 value
= LLVMBuildAnd(ctx
->ac
.builder
, value
,
256 LLVMConstInt(ctx
->i32
, mask
, 0), "");
262 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
263 unsigned param
, unsigned rshift
,
266 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
, param
);
268 return unpack_llvm_param(ctx
, value
, rshift
, bitwidth
);
271 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
274 case PIPE_SHADER_TESS_CTRL
:
275 return unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 0, 8);
277 case PIPE_SHADER_TESS_EVAL
:
278 return LLVMGetParam(ctx
->main_fn
,
279 ctx
->param_tes_rel_patch_id
);
287 /* Tessellation shaders pass outputs to the next shader using LDS.
289 * LS outputs = TCS inputs
290 * TCS outputs = TES inputs
293 * - TCS inputs for patch 0
294 * - TCS inputs for patch 1
295 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
297 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
298 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
299 * - TCS outputs for patch 1
300 * - Per-patch TCS outputs for patch 1
301 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
302 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
305 * All three shaders VS(LS), TCS, TES share the same LDS space.
309 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
311 return si_unpack_param(ctx
, ctx
->param_vs_state_bits
, 8, 13);
314 static unsigned get_tcs_out_vertex_dw_stride_constant(struct si_shader_context
*ctx
)
316 assert(ctx
->type
== PIPE_SHADER_TESS_CTRL
);
318 if (ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
)
319 return util_last_bit64(ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
) * 4;
321 return util_last_bit64(ctx
->shader
->selector
->outputs_written
) * 4;
324 static LLVMValueRef
get_tcs_out_vertex_dw_stride(struct si_shader_context
*ctx
)
326 unsigned stride
= get_tcs_out_vertex_dw_stride_constant(ctx
);
328 return LLVMConstInt(ctx
->i32
, stride
, 0);
331 static LLVMValueRef
get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
333 if (ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
)
334 return si_unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 0, 13);
336 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
337 unsigned tcs_out_vertices
= info
->properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
];
338 unsigned vertex_dw_stride
= get_tcs_out_vertex_dw_stride_constant(ctx
);
339 unsigned num_patch_outputs
= util_last_bit64(ctx
->shader
->selector
->patch_outputs_written
);
340 unsigned patch_dw_stride
= tcs_out_vertices
* vertex_dw_stride
+
341 num_patch_outputs
* 4;
342 return LLVMConstInt(ctx
->i32
, patch_dw_stride
, 0);
346 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
348 return LLVMBuildMul(ctx
->ac
.builder
,
350 ctx
->param_tcs_out_lds_offsets
,
352 LLVMConstInt(ctx
->i32
, 4, 0), "");
356 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
358 return LLVMBuildMul(ctx
->ac
.builder
,
360 ctx
->param_tcs_out_lds_offsets
,
362 LLVMConstInt(ctx
->i32
, 4, 0), "");
366 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
368 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
369 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
371 return LLVMBuildMul(ctx
->ac
.builder
, patch_stride
, rel_patch_id
, "");
375 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
377 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
378 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
379 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
381 return ac_build_imad(&ctx
->ac
, patch_stride
, rel_patch_id
, patch0_offset
);
385 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
387 LLVMValueRef patch0_patch_data_offset
=
388 get_tcs_out_patch0_patch_data_offset(ctx
);
389 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
390 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
392 return ac_build_imad(&ctx
->ac
, patch_stride
, rel_patch_id
, patch0_patch_data_offset
);
395 static LLVMValueRef
get_num_tcs_out_vertices(struct si_shader_context
*ctx
)
397 unsigned tcs_out_vertices
=
398 ctx
->shader
->selector
?
399 ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
] : 0;
401 /* If !tcs_out_vertices, it's either the fixed-func TCS or the TCS epilog. */
402 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&& tcs_out_vertices
)
403 return LLVMConstInt(ctx
->i32
, tcs_out_vertices
, 0);
405 return si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 6, 6);
408 static LLVMValueRef
get_tcs_in_vertex_dw_stride(struct si_shader_context
*ctx
)
413 case PIPE_SHADER_VERTEX
:
414 stride
= ctx
->shader
->selector
->lshs_vertex_stride
/ 4;
415 return LLVMConstInt(ctx
->i32
, stride
, 0);
417 case PIPE_SHADER_TESS_CTRL
:
418 if (ctx
->screen
->info
.chip_class
>= GFX9
&&
419 ctx
->shader
->is_monolithic
) {
420 stride
= ctx
->shader
->key
.part
.tcs
.ls
->lshs_vertex_stride
/ 4;
421 return LLVMConstInt(ctx
->i32
, stride
, 0);
423 return si_unpack_param(ctx
, ctx
->param_vs_state_bits
, 24, 8);
431 /* Bitcast <4 x float> to <2 x double>, extract the component, and convert
433 static LLVMValueRef
extract_double_to_float(struct si_shader_context
*ctx
,
435 unsigned double_index
)
437 LLVMBuilderRef builder
= ctx
->ac
.builder
;
438 LLVMTypeRef f64
= LLVMDoubleTypeInContext(ctx
->ac
.context
);
439 LLVMValueRef dvec2
= LLVMBuildBitCast(builder
, vec4
,
440 LLVMVectorType(f64
, 2), "");
441 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, double_index
, 0);
442 LLVMValueRef value
= LLVMBuildExtractElement(builder
, dvec2
, index
, "");
443 return LLVMBuildFPTrunc(builder
, value
, ctx
->f32
, "");
446 static LLVMValueRef
unpack_sint16(struct si_shader_context
*ctx
,
447 LLVMValueRef i32
, unsigned index
)
452 return LLVMBuildAShr(ctx
->ac
.builder
, i32
,
453 LLVMConstInt(ctx
->i32
, 16, 0), "");
455 return LLVMBuildSExt(ctx
->ac
.builder
,
456 LLVMBuildTrunc(ctx
->ac
.builder
, i32
,
461 void si_llvm_load_input_vs(
462 struct si_shader_context
*ctx
,
463 unsigned input_index
,
466 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
467 unsigned vs_blit_property
= info
->properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
469 if (vs_blit_property
) {
470 LLVMValueRef vertex_id
= ctx
->abi
.vertex_id
;
471 LLVMValueRef sel_x1
= LLVMBuildICmp(ctx
->ac
.builder
,
472 LLVMIntULE
, vertex_id
,
474 /* Use LLVMIntNE, because we have 3 vertices and only
475 * the middle one should use y2.
477 LLVMValueRef sel_y1
= LLVMBuildICmp(ctx
->ac
.builder
,
478 LLVMIntNE
, vertex_id
,
481 if (input_index
== 0) {
483 LLVMValueRef x1y1
= LLVMGetParam(ctx
->main_fn
,
484 ctx
->param_vs_blit_inputs
);
485 LLVMValueRef x2y2
= LLVMGetParam(ctx
->main_fn
,
486 ctx
->param_vs_blit_inputs
+ 1);
488 LLVMValueRef x1
= unpack_sint16(ctx
, x1y1
, 0);
489 LLVMValueRef y1
= unpack_sint16(ctx
, x1y1
, 1);
490 LLVMValueRef x2
= unpack_sint16(ctx
, x2y2
, 0);
491 LLVMValueRef y2
= unpack_sint16(ctx
, x2y2
, 1);
493 LLVMValueRef x
= LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
495 LLVMValueRef y
= LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
498 out
[0] = LLVMBuildSIToFP(ctx
->ac
.builder
, x
, ctx
->f32
, "");
499 out
[1] = LLVMBuildSIToFP(ctx
->ac
.builder
, y
, ctx
->f32
, "");
500 out
[2] = LLVMGetParam(ctx
->main_fn
,
501 ctx
->param_vs_blit_inputs
+ 2);
502 out
[3] = ctx
->ac
.f32_1
;
506 /* Color or texture coordinates: */
507 assert(input_index
== 1);
509 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
510 for (int i
= 0; i
< 4; i
++) {
511 out
[i
] = LLVMGetParam(ctx
->main_fn
,
512 ctx
->param_vs_blit_inputs
+ 3 + i
);
515 assert(vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
);
516 LLVMValueRef x1
= LLVMGetParam(ctx
->main_fn
,
517 ctx
->param_vs_blit_inputs
+ 3);
518 LLVMValueRef y1
= LLVMGetParam(ctx
->main_fn
,
519 ctx
->param_vs_blit_inputs
+ 4);
520 LLVMValueRef x2
= LLVMGetParam(ctx
->main_fn
,
521 ctx
->param_vs_blit_inputs
+ 5);
522 LLVMValueRef y2
= LLVMGetParam(ctx
->main_fn
,
523 ctx
->param_vs_blit_inputs
+ 6);
525 out
[0] = LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
527 out
[1] = LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
529 out
[2] = LLVMGetParam(ctx
->main_fn
,
530 ctx
->param_vs_blit_inputs
+ 7);
531 out
[3] = LLVMGetParam(ctx
->main_fn
,
532 ctx
->param_vs_blit_inputs
+ 8);
539 unsigned num_fetches
;
540 unsigned fetch_stride
;
541 unsigned num_channels
;
543 LLVMValueRef t_list_ptr
;
544 LLVMValueRef t_offset
;
546 LLVMValueRef vertex_index
;
547 LLVMValueRef input
[3];
549 /* Load the T list */
550 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_vertex_buffers
);
552 t_offset
= LLVMConstInt(ctx
->i32
, input_index
, 0);
554 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
556 vertex_index
= LLVMGetParam(ctx
->main_fn
,
557 ctx
->param_vertex_index0
+
560 fix_fetch
= ctx
->shader
->key
.mono
.vs_fix_fetch
[input_index
];
562 /* Do multiple loads for special formats. */
564 case SI_FIX_FETCH_RGB_64_FLOAT
:
565 num_fetches
= 3; /* 3 2-dword loads */
569 case SI_FIX_FETCH_RGBA_64_FLOAT
:
570 num_fetches
= 2; /* 2 4-dword loads */
574 case SI_FIX_FETCH_RGB_8
:
575 case SI_FIX_FETCH_RGB_8_INT
:
580 case SI_FIX_FETCH_RGB_16
:
581 case SI_FIX_FETCH_RGB_16_INT
:
589 num_channels
= util_last_bit(info
->input_usage_mask
[input_index
]);
592 for (unsigned i
= 0; i
< num_fetches
; i
++) {
593 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, fetch_stride
* i
, 0);
595 input
[i
] = ac_build_buffer_load_format(&ctx
->ac
, t_list
,
596 vertex_index
, voffset
,
597 num_channels
, false, true);
598 input
[i
] = ac_build_expand_to_vec4(&ctx
->ac
, input
[i
], num_channels
);
601 /* Break up the vec4 into individual components */
602 for (chan
= 0; chan
< 4; chan
++) {
603 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
604 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
605 input
[0], llvm_chan
, "");
609 case SI_FIX_FETCH_A2_SNORM
:
610 case SI_FIX_FETCH_A2_SSCALED
:
611 case SI_FIX_FETCH_A2_SINT
: {
612 /* The hardware returns an unsigned value; convert it to a
615 LLVMValueRef tmp
= out
[3];
616 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
618 /* First, recover the sign-extended signed integer value. */
619 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
620 tmp
= LLVMBuildFPToUI(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
622 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
624 /* For the integer-like cases, do a natural sign extension.
626 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
627 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
630 tmp
= LLVMBuildShl(ctx
->ac
.builder
, tmp
,
631 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
632 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
633 tmp
= LLVMBuildAShr(ctx
->ac
.builder
, tmp
, c30
, "");
635 /* Convert back to the right type. */
636 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
638 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
639 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
640 clamp
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealULT
, tmp
, neg_one
, "");
641 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, clamp
, neg_one
, tmp
, "");
642 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
643 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
649 case SI_FIX_FETCH_RGBA_32_UNORM
:
650 case SI_FIX_FETCH_RGBX_32_UNORM
:
651 for (chan
= 0; chan
< 4; chan
++) {
652 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
653 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
654 out
[chan
], ctx
->f32
, "");
655 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
656 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
658 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
659 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
660 out
[3] = LLVMConstReal(ctx
->f32
, 1);
662 case SI_FIX_FETCH_RGBA_32_SNORM
:
663 case SI_FIX_FETCH_RGBX_32_SNORM
:
664 case SI_FIX_FETCH_RGBA_32_FIXED
:
665 case SI_FIX_FETCH_RGBX_32_FIXED
: {
667 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
668 scale
= 1.0 / 0x10000;
670 scale
= 1.0 / INT_MAX
;
672 for (chan
= 0; chan
< 4; chan
++) {
673 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
674 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
675 out
[chan
], ctx
->f32
, "");
676 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
677 LLVMConstReal(ctx
->f32
, scale
), "");
679 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
680 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
681 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
682 out
[3] = LLVMConstReal(ctx
->f32
, 1);
685 case SI_FIX_FETCH_RGBA_32_USCALED
:
686 for (chan
= 0; chan
< 4; chan
++) {
687 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
688 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
689 out
[chan
], ctx
->f32
, "");
692 case SI_FIX_FETCH_RGBA_32_SSCALED
:
693 for (chan
= 0; chan
< 4; chan
++) {
694 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
695 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
696 out
[chan
], ctx
->f32
, "");
699 case SI_FIX_FETCH_RG_64_FLOAT
:
700 for (chan
= 0; chan
< 2; chan
++)
701 out
[chan
] = extract_double_to_float(ctx
, input
[0], chan
);
703 out
[2] = LLVMConstReal(ctx
->f32
, 0);
704 out
[3] = LLVMConstReal(ctx
->f32
, 1);
706 case SI_FIX_FETCH_RGB_64_FLOAT
:
707 for (chan
= 0; chan
< 3; chan
++)
708 out
[chan
] = extract_double_to_float(ctx
, input
[chan
], 0);
710 out
[3] = LLVMConstReal(ctx
->f32
, 1);
712 case SI_FIX_FETCH_RGBA_64_FLOAT
:
713 for (chan
= 0; chan
< 4; chan
++) {
714 out
[chan
] = extract_double_to_float(ctx
, input
[chan
/ 2],
718 case SI_FIX_FETCH_RGB_8
:
719 case SI_FIX_FETCH_RGB_8_INT
:
720 case SI_FIX_FETCH_RGB_16
:
721 case SI_FIX_FETCH_RGB_16_INT
:
722 for (chan
= 0; chan
< 3; chan
++) {
723 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
727 if (fix_fetch
== SI_FIX_FETCH_RGB_8
||
728 fix_fetch
== SI_FIX_FETCH_RGB_16
) {
729 out
[3] = LLVMConstReal(ctx
->f32
, 1);
731 out
[3] = ac_to_float(&ctx
->ac
, ctx
->i32_1
);
737 static void declare_input_vs(
738 struct si_shader_context
*ctx
,
739 unsigned input_index
,
740 const struct tgsi_full_declaration
*decl
,
743 si_llvm_load_input_vs(ctx
, input_index
, out
);
746 static LLVMValueRef
get_primitive_id(struct si_shader_context
*ctx
,
753 case PIPE_SHADER_VERTEX
:
754 return LLVMGetParam(ctx
->main_fn
,
755 ctx
->param_vs_prim_id
);
756 case PIPE_SHADER_TESS_CTRL
:
757 return ctx
->abi
.tcs_patch_id
;
758 case PIPE_SHADER_TESS_EVAL
:
759 return ctx
->abi
.tes_patch_id
;
760 case PIPE_SHADER_GEOMETRY
:
761 return ctx
->abi
.gs_prim_id
;
769 * Return the value of tgsi_ind_register for indexing.
770 * This is the indirect index with the constant offset added to it.
772 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
773 const struct tgsi_ind_register
*ind
,
779 if (ind
->File
== TGSI_FILE_ADDRESS
) {
780 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
781 result
= LLVMBuildLoad(ctx
->ac
.builder
, result
, "");
783 struct tgsi_full_src_register src
= {};
785 src
.Register
.File
= ind
->File
;
786 src
.Register
.Index
= ind
->Index
;
788 /* Set the second index to 0 for constants. */
789 if (ind
->File
== TGSI_FILE_CONSTANT
)
790 src
.Register
.Dimension
= 1;
792 result
= ctx
->bld_base
.emit_fetch_funcs
[ind
->File
](&ctx
->bld_base
, &src
,
795 result
= ac_to_integer(&ctx
->ac
, result
);
798 return ac_build_imad(&ctx
->ac
, result
, LLVMConstInt(ctx
->i32
, addr_mul
, 0),
799 LLVMConstInt(ctx
->i32
, rel_index
, 0));
803 * Like si_get_indirect_index, but restricts the return value to a (possibly
804 * undefined) value inside [0..num).
806 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
807 const struct tgsi_ind_register
*ind
,
808 int rel_index
, unsigned num
)
810 LLVMValueRef result
= si_get_indirect_index(ctx
, ind
, 1, rel_index
);
812 return si_llvm_bound_index(ctx
, result
, num
);
815 static LLVMValueRef
get_dw_address_from_generic_indices(struct si_shader_context
*ctx
,
816 LLVMValueRef vertex_dw_stride
,
817 LLVMValueRef base_addr
,
818 LLVMValueRef vertex_index
,
819 LLVMValueRef param_index
,
820 unsigned input_index
,
825 if (vertex_dw_stride
) {
826 base_addr
= ac_build_imad(&ctx
->ac
, vertex_index
,
827 vertex_dw_stride
, base_addr
);
831 base_addr
= ac_build_imad(&ctx
->ac
, param_index
,
832 LLVMConstInt(ctx
->i32
, 4, 0), base_addr
);
835 int param
= is_patch
?
836 si_shader_io_get_unique_index_patch(name
[input_index
],
837 index
[input_index
]) :
838 si_shader_io_get_unique_index(name
[input_index
],
839 index
[input_index
], false);
841 /* Add the base address of the element. */
842 return LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
843 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
847 * Calculate a dword address given an input or output register and a stride.
849 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
850 const struct tgsi_full_dst_register
*dst
,
851 const struct tgsi_full_src_register
*src
,
852 LLVMValueRef vertex_dw_stride
,
853 LLVMValueRef base_addr
)
855 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
856 ubyte
*name
, *index
, *array_first
;
858 struct tgsi_full_dst_register reg
;
859 LLVMValueRef vertex_index
= NULL
;
860 LLVMValueRef ind_index
= NULL
;
862 /* Set the register description. The address computation is the same
863 * for sources and destinations. */
865 reg
.Register
.File
= src
->Register
.File
;
866 reg
.Register
.Index
= src
->Register
.Index
;
867 reg
.Register
.Indirect
= src
->Register
.Indirect
;
868 reg
.Register
.Dimension
= src
->Register
.Dimension
;
869 reg
.Indirect
= src
->Indirect
;
870 reg
.Dimension
= src
->Dimension
;
871 reg
.DimIndirect
= src
->DimIndirect
;
875 /* If the register is 2-dimensional (e.g. an array of vertices
876 * in a primitive), calculate the base address of the vertex. */
877 if (reg
.Register
.Dimension
) {
878 if (reg
.Dimension
.Indirect
)
879 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
880 1, reg
.Dimension
.Index
);
882 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
885 /* Get information about the register. */
886 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
887 name
= info
->input_semantic_name
;
888 index
= info
->input_semantic_index
;
889 array_first
= info
->input_array_first
;
890 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
891 name
= info
->output_semantic_name
;
892 index
= info
->output_semantic_index
;
893 array_first
= info
->output_array_first
;
899 if (reg
.Register
.Indirect
) {
900 /* Add the relative address of the element. */
901 if (reg
.Indirect
.ArrayID
)
902 input_index
= array_first
[reg
.Indirect
.ArrayID
];
904 input_index
= reg
.Register
.Index
;
906 ind_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
907 1, reg
.Register
.Index
- input_index
);
909 input_index
= reg
.Register
.Index
;
912 return get_dw_address_from_generic_indices(ctx
, vertex_dw_stride
,
913 base_addr
, vertex_index
,
914 ind_index
, input_index
,
916 !reg
.Register
.Dimension
);
919 /* The offchip buffer layout for TCS->TES is
921 * - attribute 0 of patch 0 vertex 0
922 * - attribute 0 of patch 0 vertex 1
923 * - attribute 0 of patch 0 vertex 2
925 * - attribute 0 of patch 1 vertex 0
926 * - attribute 0 of patch 1 vertex 1
928 * - attribute 1 of patch 0 vertex 0
929 * - attribute 1 of patch 0 vertex 1
931 * - per patch attribute 0 of patch 0
932 * - per patch attribute 0 of patch 1
935 * Note that every attribute has 4 components.
937 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
938 LLVMValueRef rel_patch_id
,
939 LLVMValueRef vertex_index
,
940 LLVMValueRef param_index
)
942 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
943 LLVMValueRef param_stride
, constant16
;
945 vertices_per_patch
= get_num_tcs_out_vertices(ctx
);
946 num_patches
= si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 0, 6);
947 total_vertices
= LLVMBuildMul(ctx
->ac
.builder
, vertices_per_patch
,
950 constant16
= LLVMConstInt(ctx
->i32
, 16, 0);
952 base_addr
= ac_build_imad(&ctx
->ac
, rel_patch_id
,
953 vertices_per_patch
, vertex_index
);
954 param_stride
= total_vertices
;
956 base_addr
= rel_patch_id
;
957 param_stride
= num_patches
;
960 base_addr
= ac_build_imad(&ctx
->ac
, param_index
, param_stride
, base_addr
);
961 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, base_addr
, constant16
, "");
964 LLVMValueRef patch_data_offset
=
965 si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 12, 20);
967 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
968 patch_data_offset
, "");
973 /* This is a generic helper that can be shared by the NIR and TGSI backends */
974 static LLVMValueRef
get_tcs_tes_buffer_address_from_generic_indices(
975 struct si_shader_context
*ctx
,
976 LLVMValueRef vertex_index
,
977 LLVMValueRef param_index
,
983 unsigned param_index_base
;
985 param_index_base
= is_patch
?
986 si_shader_io_get_unique_index_patch(name
[param_base
], index
[param_base
]) :
987 si_shader_io_get_unique_index(name
[param_base
], index
[param_base
], false);
990 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
991 LLVMConstInt(ctx
->i32
, param_index_base
, 0),
994 param_index
= LLVMConstInt(ctx
->i32
, param_index_base
, 0);
997 return get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
),
998 vertex_index
, param_index
);
1001 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
1002 struct si_shader_context
*ctx
,
1003 const struct tgsi_full_dst_register
*dst
,
1004 const struct tgsi_full_src_register
*src
)
1006 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1007 ubyte
*name
, *index
, *array_first
;
1008 struct tgsi_full_src_register reg
;
1009 LLVMValueRef vertex_index
= NULL
;
1010 LLVMValueRef param_index
= NULL
;
1011 unsigned param_base
;
1013 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
1015 if (reg
.Register
.Dimension
) {
1017 if (reg
.Dimension
.Indirect
)
1018 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
1019 1, reg
.Dimension
.Index
);
1021 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
1024 /* Get information about the register. */
1025 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
1026 name
= info
->input_semantic_name
;
1027 index
= info
->input_semantic_index
;
1028 array_first
= info
->input_array_first
;
1029 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
1030 name
= info
->output_semantic_name
;
1031 index
= info
->output_semantic_index
;
1032 array_first
= info
->output_array_first
;
1038 if (reg
.Register
.Indirect
) {
1039 if (reg
.Indirect
.ArrayID
)
1040 param_base
= array_first
[reg
.Indirect
.ArrayID
];
1042 param_base
= reg
.Register
.Index
;
1044 param_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
1045 1, reg
.Register
.Index
- param_base
);
1048 param_base
= reg
.Register
.Index
;
1051 return get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1052 param_index
, param_base
,
1053 name
, index
, !reg
.Register
.Dimension
);
1056 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
1057 LLVMTypeRef type
, unsigned swizzle
,
1058 LLVMValueRef buffer
, LLVMValueRef offset
,
1059 LLVMValueRef base
, bool can_speculate
)
1061 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1062 LLVMValueRef value
, value2
;
1063 LLVMTypeRef vec_type
= LLVMVectorType(type
, 4);
1065 if (swizzle
== ~0) {
1066 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1067 0, 1, 0, can_speculate
, false);
1069 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1072 if (!llvm_type_is_64bit(ctx
, type
)) {
1073 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1074 0, 1, 0, can_speculate
, false);
1076 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1077 return LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1078 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
1081 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1082 swizzle
* 4, 1, 0, can_speculate
, false);
1084 value2
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1085 swizzle
* 4 + 4, 1, 0, can_speculate
, false);
1087 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1093 * \param type output value type
1094 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
1095 * \param dw_addr address in dwords
1097 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
1098 LLVMTypeRef type
, unsigned swizzle
,
1099 LLVMValueRef dw_addr
)
1101 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1104 if (swizzle
== ~0) {
1105 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1107 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
1108 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
1110 return ac_build_gather_values(&ctx
->ac
, values
,
1114 /* Split 64-bit loads. */
1115 if (llvm_type_is_64bit(ctx
, type
)) {
1116 LLVMValueRef lo
, hi
;
1118 lo
= lds_load(bld_base
, ctx
->i32
, swizzle
, dw_addr
);
1119 hi
= lds_load(bld_base
, ctx
->i32
, swizzle
+ 1, dw_addr
);
1120 return si_llvm_emit_fetch_64bit(bld_base
, type
, lo
, hi
);
1123 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1124 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
1126 value
= ac_lds_load(&ctx
->ac
, dw_addr
);
1128 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1134 * \param swizzle offset (typically 0..3)
1135 * \param dw_addr address in dwords
1136 * \param value value to store
1138 static void lds_store(struct si_shader_context
*ctx
,
1139 unsigned dw_offset_imm
, LLVMValueRef dw_addr
,
1142 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1143 LLVMConstInt(ctx
->i32
, dw_offset_imm
, 0), "");
1145 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
1150 TESS_OFFCHIP_RING_TCS
,
1151 TESS_OFFCHIP_RING_TES
,
1154 static LLVMValueRef
get_tess_ring_descriptor(struct si_shader_context
*ctx
,
1155 enum si_tess_ring ring
)
1157 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1158 unsigned param
= ring
== TESS_OFFCHIP_RING_TES
? ctx
->param_tes_offchip_addr
:
1159 ctx
->param_tcs_out_lds_layout
;
1160 LLVMValueRef addr
= LLVMGetParam(ctx
->main_fn
, param
);
1162 /* TCS only receives high 13 bits of the address. */
1163 if (ring
== TESS_OFFCHIP_RING_TCS
|| ring
== TCS_FACTOR_RING
) {
1164 addr
= LLVMBuildAnd(builder
, addr
,
1165 LLVMConstInt(ctx
->i32
, 0xfff80000, 0), "");
1168 if (ring
== TCS_FACTOR_RING
) {
1169 unsigned tf_offset
= ctx
->screen
->tess_offchip_ring_size
;
1170 addr
= LLVMBuildAdd(builder
, addr
,
1171 LLVMConstInt(ctx
->i32
, tf_offset
, 0), "");
1174 LLVMValueRef desc
[4];
1176 desc
[1] = LLVMConstInt(ctx
->i32
,
1177 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
1178 desc
[2] = LLVMConstInt(ctx
->i32
, 0xffffffff, 0);
1179 desc
[3] = LLVMConstInt(ctx
->i32
,
1180 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1181 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1182 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1183 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1184 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1185 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0);
1187 return ac_build_gather_values(&ctx
->ac
, desc
, 4);
1190 static LLVMValueRef
fetch_input_tcs(
1191 struct lp_build_tgsi_context
*bld_base
,
1192 const struct tgsi_full_src_register
*reg
,
1193 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1195 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1196 LLVMValueRef dw_addr
, stride
;
1197 unsigned swizzle
= swizzle_in
& 0xffff;
1198 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1199 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1200 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1202 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1205 static LLVMValueRef
si_nir_load_tcs_varyings(struct ac_shader_abi
*abi
,
1207 LLVMValueRef vertex_index
,
1208 LLVMValueRef param_index
,
1209 unsigned const_index
,
1211 unsigned driver_location
,
1213 unsigned num_components
,
1218 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1219 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1220 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1221 LLVMValueRef dw_addr
, stride
;
1223 driver_location
= driver_location
/ 4;
1226 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1227 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1231 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1233 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1234 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1239 /* Add the constant index to the indirect index */
1240 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1241 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1243 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1249 names
= info
->input_semantic_name
;
1250 indices
= info
->input_semantic_index
;
1252 names
= info
->output_semantic_name
;
1253 indices
= info
->output_semantic_index
;
1256 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1257 vertex_index
, param_index
,
1262 LLVMValueRef value
[4];
1263 for (unsigned i
= 0; i
< num_components
; i
++) {
1264 unsigned offset
= i
;
1265 if (llvm_type_is_64bit(ctx
, type
))
1268 offset
+= component
;
1269 value
[i
+ component
] = lds_load(bld_base
, type
, offset
, dw_addr
);
1272 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1275 static LLVMValueRef
fetch_output_tcs(
1276 struct lp_build_tgsi_context
*bld_base
,
1277 const struct tgsi_full_src_register
*reg
,
1278 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1280 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1281 LLVMValueRef dw_addr
, stride
;
1282 unsigned swizzle
= (swizzle_in
& 0xffff);
1284 if (reg
->Register
.Dimension
) {
1285 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1286 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1287 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1289 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1290 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
1293 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1296 static LLVMValueRef
fetch_input_tes(
1297 struct lp_build_tgsi_context
*bld_base
,
1298 const struct tgsi_full_src_register
*reg
,
1299 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1301 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1302 LLVMValueRef base
, addr
;
1303 unsigned swizzle
= (swizzle_in
& 0xffff);
1305 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1306 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
1308 return buffer_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
,
1309 ctx
->tess_offchip_ring
, base
, addr
, true);
1312 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
1314 LLVMValueRef vertex_index
,
1315 LLVMValueRef param_index
,
1316 unsigned const_index
,
1318 unsigned driver_location
,
1320 unsigned num_components
,
1325 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1326 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1327 LLVMValueRef base
, addr
;
1329 driver_location
= driver_location
/ 4;
1331 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1334 /* Add the constant index to the indirect index */
1335 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1336 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1338 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1341 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1342 param_index
, driver_location
,
1343 info
->input_semantic_name
,
1344 info
->input_semantic_index
,
1347 /* TODO: This will generate rather ordinary llvm code, although it
1348 * should be easy for the optimiser to fix up. In future we might want
1349 * to refactor buffer_load(), but for now this maximises code sharing
1350 * between the NIR and TGSI backends.
1352 LLVMValueRef value
[4];
1353 for (unsigned i
= 0; i
< num_components
; i
++) {
1354 unsigned offset
= i
;
1355 if (llvm_type_is_64bit(ctx
, type
))
1358 offset
+= component
;
1359 value
[i
+ component
] = buffer_load(&ctx
->bld_base
, type
, offset
,
1360 ctx
->tess_offchip_ring
, base
, addr
, true);
1363 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1366 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1367 const struct tgsi_full_instruction
*inst
,
1368 const struct tgsi_opcode_info
*info
,
1370 LLVMValueRef dst
[4])
1372 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1373 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1374 const struct tgsi_shader_info
*sh_info
= &ctx
->shader
->selector
->info
;
1375 unsigned chan_index
;
1376 LLVMValueRef dw_addr
, stride
;
1377 LLVMValueRef buffer
, base
, buf_addr
;
1378 LLVMValueRef values
[4];
1379 bool skip_lds_store
;
1380 bool is_tess_factor
= false, is_tess_inner
= false;
1382 /* Only handle per-patch and per-vertex outputs here.
1383 * Vectors will be lowered to scalars and this function will be called again.
1385 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1386 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1387 si_llvm_emit_store(bld_base
, inst
, info
, index
, dst
);
1391 if (reg
->Register
.Dimension
) {
1392 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1393 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1394 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1395 skip_lds_store
= !sh_info
->reads_pervertex_outputs
;
1397 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1398 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1399 skip_lds_store
= !sh_info
->reads_perpatch_outputs
;
1401 if (!reg
->Register
.Indirect
) {
1402 int name
= sh_info
->output_semantic_name
[reg
->Register
.Index
];
1404 /* Always write tess factors into LDS for the TCS epilog. */
1405 if (name
== TGSI_SEMANTIC_TESSINNER
||
1406 name
== TGSI_SEMANTIC_TESSOUTER
) {
1407 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1408 skip_lds_store
= !sh_info
->reads_tessfactor_outputs
&&
1409 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1410 is_tess_factor
= true;
1411 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1416 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1418 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1419 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1421 uint32_t writemask
= reg
->Register
.WriteMask
;
1423 chan_index
= u_bit_scan(&writemask
);
1424 LLVMValueRef value
= dst
[chan_index
];
1426 if (inst
->Instruction
.Saturate
)
1427 value
= ac_build_clamp(&ctx
->ac
, value
);
1429 /* Skip LDS stores if there is no LDS read of this output. */
1430 if (!skip_lds_store
)
1431 lds_store(ctx
, chan_index
, dw_addr
, value
);
1433 value
= ac_to_integer(&ctx
->ac
, value
);
1434 values
[chan_index
] = value
;
1436 if (reg
->Register
.WriteMask
!= 0xF && !is_tess_factor
) {
1437 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1439 4 * chan_index
, 1, 0, true, false);
1442 /* Write tess factors into VGPRs for the epilog. */
1443 if (is_tess_factor
&&
1444 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1445 if (!is_tess_inner
) {
1446 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1447 ctx
->invoc0_tess_factors
[chan_index
]);
1448 } else if (chan_index
< 2) {
1449 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1450 ctx
->invoc0_tess_factors
[4 + chan_index
]);
1455 if (reg
->Register
.WriteMask
== 0xF && !is_tess_factor
) {
1456 LLVMValueRef value
= ac_build_gather_values(&ctx
->ac
,
1458 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buf_addr
,
1459 base
, 0, 1, 0, true, false);
1463 static void si_nir_store_output_tcs(struct ac_shader_abi
*abi
,
1464 const struct nir_variable
*var
,
1465 LLVMValueRef vertex_index
,
1466 LLVMValueRef param_index
,
1467 unsigned const_index
,
1471 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1472 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1473 const unsigned component
= var
->data
.location_frac
;
1474 const bool is_patch
= var
->data
.patch
;
1475 unsigned driver_location
= var
->data
.driver_location
;
1476 LLVMValueRef dw_addr
, stride
;
1477 LLVMValueRef buffer
, base
, addr
;
1478 LLVMValueRef values
[4];
1479 bool skip_lds_store
;
1480 bool is_tess_factor
= false, is_tess_inner
= false;
1482 driver_location
= driver_location
/ 4;
1485 /* Add the constant index to the indirect index */
1486 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1487 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1489 if (const_index
!= 0)
1490 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1494 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1495 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1496 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1497 vertex_index
, param_index
,
1499 info
->output_semantic_name
,
1500 info
->output_semantic_index
,
1503 skip_lds_store
= !info
->reads_pervertex_outputs
;
1505 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1506 dw_addr
= get_dw_address_from_generic_indices(ctx
, NULL
, dw_addr
,
1507 vertex_index
, param_index
,
1509 info
->output_semantic_name
,
1510 info
->output_semantic_index
,
1513 skip_lds_store
= !info
->reads_perpatch_outputs
;
1516 int name
= info
->output_semantic_name
[driver_location
];
1518 /* Always write tess factors into LDS for the TCS epilog. */
1519 if (name
== TGSI_SEMANTIC_TESSINNER
||
1520 name
== TGSI_SEMANTIC_TESSOUTER
) {
1521 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1522 skip_lds_store
= !info
->reads_tessfactor_outputs
&&
1523 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1524 is_tess_factor
= true;
1525 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1530 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1532 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1534 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1535 param_index
, driver_location
,
1536 info
->output_semantic_name
,
1537 info
->output_semantic_index
,
1540 for (unsigned chan
= 0; chan
< 4; chan
++) {
1541 if (!(writemask
& (1 << chan
)))
1543 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
1545 /* Skip LDS stores if there is no LDS read of this output. */
1546 if (!skip_lds_store
)
1547 lds_store(ctx
, chan
, dw_addr
, value
);
1549 value
= ac_to_integer(&ctx
->ac
, value
);
1550 values
[chan
] = value
;
1552 if (writemask
!= 0xF && !is_tess_factor
) {
1553 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1555 4 * chan
, 1, 0, true, false);
1558 /* Write tess factors into VGPRs for the epilog. */
1559 if (is_tess_factor
&&
1560 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1561 if (!is_tess_inner
) {
1562 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1563 ctx
->invoc0_tess_factors
[chan
]);
1564 } else if (chan
< 2) {
1565 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1566 ctx
->invoc0_tess_factors
[4 + chan
]);
1571 if (writemask
== 0xF && !is_tess_factor
) {
1572 LLVMValueRef value
= ac_build_gather_values(&ctx
->ac
,
1574 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, addr
,
1575 base
, 0, 1, 0, true, false);
1579 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
1580 unsigned input_index
,
1581 unsigned vtx_offset_param
,
1585 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1586 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1587 struct si_shader
*shader
= ctx
->shader
;
1588 LLVMValueRef vtx_offset
, soffset
;
1589 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1590 unsigned semantic_name
= info
->input_semantic_name
[input_index
];
1591 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1595 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
, false);
1597 /* GFX9 has the ESGS ring in LDS. */
1598 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1599 unsigned index
= vtx_offset_param
;
1601 switch (index
/ 2) {
1603 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx01_offset
,
1604 index
% 2 ? 16 : 0, 16);
1607 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx23_offset
,
1608 index
% 2 ? 16 : 0, 16);
1611 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx45_offset
,
1612 index
% 2 ? 16 : 0, 16);
1619 vtx_offset
= LLVMBuildAdd(ctx
->ac
.builder
, vtx_offset
,
1620 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
1621 return lds_load(bld_base
, type
, swizzle
, vtx_offset
);
1624 /* GFX6: input load from the ESGS ring in memory. */
1625 if (swizzle
== ~0) {
1626 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1628 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1629 values
[chan
] = si_llvm_load_input_gs(abi
, input_index
, vtx_offset_param
,
1632 return ac_build_gather_values(&ctx
->ac
, values
,
1636 /* Get the vertex offset parameter on GFX6. */
1637 LLVMValueRef gs_vtx_offset
= ctx
->gs_vtx_offset
[vtx_offset_param
];
1639 vtx_offset
= LLVMBuildMul(ctx
->ac
.builder
, gs_vtx_offset
,
1640 LLVMConstInt(ctx
->i32
, 4, 0), "");
1642 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
) * 256, 0);
1644 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1, ctx
->i32_0
,
1645 vtx_offset
, soffset
, 0, 1, 0, true, false);
1646 if (llvm_type_is_64bit(ctx
, type
)) {
1647 LLVMValueRef value2
;
1648 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
+ 1) * 256, 0);
1650 value2
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1,
1651 ctx
->i32_0
, vtx_offset
, soffset
,
1652 0, 1, 0, true, false);
1653 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1655 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1658 static LLVMValueRef
si_nir_load_input_gs(struct ac_shader_abi
*abi
,
1660 unsigned driver_location
,
1662 unsigned num_components
,
1663 unsigned vertex_index
,
1664 unsigned const_index
,
1667 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1669 LLVMValueRef value
[4];
1670 for (unsigned i
= 0; i
< num_components
; i
++) {
1671 unsigned offset
= i
;
1672 if (llvm_type_is_64bit(ctx
, type
))
1675 offset
+= component
;
1676 value
[i
+ component
] = si_llvm_load_input_gs(&ctx
->abi
, driver_location
/ 4,
1677 vertex_index
, type
, offset
);
1680 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1683 static LLVMValueRef
fetch_input_gs(
1684 struct lp_build_tgsi_context
*bld_base
,
1685 const struct tgsi_full_src_register
*reg
,
1686 enum tgsi_opcode_type type
,
1687 unsigned swizzle_in
)
1689 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1690 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1691 unsigned swizzle
= swizzle_in
& 0xffff;
1693 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1694 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1695 return get_primitive_id(ctx
, swizzle
);
1697 if (!reg
->Register
.Dimension
)
1700 return si_llvm_load_input_gs(&ctx
->abi
, reg
->Register
.Index
,
1701 reg
->Dimension
.Index
,
1702 tgsi2llvmtype(bld_base
, type
),
1706 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1708 switch (interpolate
) {
1709 case TGSI_INTERPOLATE_CONSTANT
:
1712 case TGSI_INTERPOLATE_LINEAR
:
1713 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1714 return SI_PARAM_LINEAR_SAMPLE
;
1715 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1716 return SI_PARAM_LINEAR_CENTROID
;
1718 return SI_PARAM_LINEAR_CENTER
;
1720 case TGSI_INTERPOLATE_COLOR
:
1721 case TGSI_INTERPOLATE_PERSPECTIVE
:
1722 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1723 return SI_PARAM_PERSP_SAMPLE
;
1724 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1725 return SI_PARAM_PERSP_CENTROID
;
1727 return SI_PARAM_PERSP_CENTER
;
1730 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1735 static LLVMValueRef
si_build_fs_interp(struct si_shader_context
*ctx
,
1736 unsigned attr_index
, unsigned chan
,
1737 LLVMValueRef prim_mask
,
1738 LLVMValueRef i
, LLVMValueRef j
)
1741 return ac_build_fs_interp(&ctx
->ac
,
1742 LLVMConstInt(ctx
->i32
, chan
, 0),
1743 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1746 return ac_build_fs_interp_mov(&ctx
->ac
,
1747 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1748 LLVMConstInt(ctx
->i32
, chan
, 0),
1749 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1754 * Interpolate a fragment shader input.
1756 * @param ctx context
1757 * @param input_index index of the input in hardware
1758 * @param semantic_name TGSI_SEMANTIC_*
1759 * @param semantic_index semantic index
1760 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1761 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1762 * @param interp_param interpolation weights (i,j)
1763 * @param prim_mask SI_PARAM_PRIM_MASK
1764 * @param face SI_PARAM_FRONT_FACE
1765 * @param result the return value (4 components)
1767 static void interp_fs_input(struct si_shader_context
*ctx
,
1768 unsigned input_index
,
1769 unsigned semantic_name
,
1770 unsigned semantic_index
,
1771 unsigned num_interp_inputs
,
1772 unsigned colors_read_mask
,
1773 LLVMValueRef interp_param
,
1774 LLVMValueRef prim_mask
,
1776 LLVMValueRef result
[4])
1778 LLVMValueRef i
= NULL
, j
= NULL
;
1781 /* fs.constant returns the param from the middle vertex, so it's not
1782 * really useful for flat shading. It's meant to be used for custom
1783 * interpolation (but the intrinsic can't fetch from the other two
1786 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1787 * to do the right thing. The only reason we use fs.constant is that
1788 * fs.interp cannot be used on integers, because they can be equal
1791 * When interp is false we will use fs.constant or for newer llvm,
1792 * amdgcn.interp.mov.
1794 bool interp
= interp_param
!= NULL
;
1797 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
1798 LLVMVectorType(ctx
->f32
, 2), "");
1800 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1802 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1806 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1807 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1808 LLVMValueRef is_face_positive
;
1810 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1811 * otherwise it's at offset "num_inputs".
1813 unsigned back_attr_offset
= num_interp_inputs
;
1814 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1815 back_attr_offset
+= 1;
1817 is_face_positive
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
1818 face
, ctx
->i32_0
, "");
1820 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1821 LLVMValueRef front
, back
;
1823 front
= si_build_fs_interp(ctx
,
1826 back
= si_build_fs_interp(ctx
,
1827 back_attr_offset
, chan
,
1830 result
[chan
] = LLVMBuildSelect(ctx
->ac
.builder
,
1836 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1837 result
[0] = si_build_fs_interp(ctx
, input_index
,
1838 0, prim_mask
, i
, j
);
1840 result
[2] = LLVMConstReal(ctx
->f32
, 0.0f
);
1841 result
[3] = LLVMConstReal(ctx
->f32
, 1.0f
);
1843 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1844 result
[chan
] = si_build_fs_interp(ctx
,
1851 void si_llvm_load_input_fs(
1852 struct si_shader_context
*ctx
,
1853 unsigned input_index
,
1854 LLVMValueRef out
[4])
1856 struct si_shader
*shader
= ctx
->shader
;
1857 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1858 LLVMValueRef main_fn
= ctx
->main_fn
;
1859 LLVMValueRef interp_param
= NULL
;
1860 int interp_param_idx
;
1861 enum tgsi_semantic semantic_name
= info
->input_semantic_name
[input_index
];
1862 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1863 enum tgsi_interpolate_mode interp_mode
= info
->input_interpolate
[input_index
];
1864 enum tgsi_interpolate_loc interp_loc
= info
->input_interpolate_loc
[input_index
];
1866 /* Get colors from input VGPRs (set by the prolog). */
1867 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1868 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1869 unsigned mask
= colors_read
>> (semantic_index
* 4);
1870 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1871 (semantic_index
? util_bitcount(colors_read
& 0xf) : 0);
1872 LLVMValueRef undef
= LLVMGetUndef(ctx
->f32
);
1874 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1875 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1876 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1877 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1881 interp_param_idx
= lookup_interp_param_index(interp_mode
, interp_loc
);
1882 if (interp_param_idx
== -1)
1884 else if (interp_param_idx
) {
1885 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1888 interp_fs_input(ctx
, input_index
, semantic_name
,
1889 semantic_index
, 0, /* this param is unused */
1890 shader
->selector
->info
.colors_read
, interp_param
,
1892 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1896 static void declare_input_fs(
1897 struct si_shader_context
*ctx
,
1898 unsigned input_index
,
1899 const struct tgsi_full_declaration
*decl
,
1900 LLVMValueRef out
[4])
1902 si_llvm_load_input_fs(ctx
, input_index
, out
);
1905 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
)
1907 return si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 8, 4);
1910 static LLVMValueRef
get_base_vertex(struct ac_shader_abi
*abi
)
1912 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1914 /* For non-indexed draws, the base vertex set by the driver
1915 * (for direct draws) or the CP (for indirect draws) is the
1916 * first vertex ID, but GLSL expects 0 to be returned.
1918 LLVMValueRef vs_state
= LLVMGetParam(ctx
->main_fn
,
1919 ctx
->param_vs_state_bits
);
1920 LLVMValueRef indexed
;
1922 indexed
= LLVMBuildLShr(ctx
->ac
.builder
, vs_state
, ctx
->i32_1
, "");
1923 indexed
= LLVMBuildTrunc(ctx
->ac
.builder
, indexed
, ctx
->i1
, "");
1925 return LLVMBuildSelect(ctx
->ac
.builder
, indexed
, ctx
->abi
.base_vertex
,
1929 static LLVMValueRef
get_block_size(struct ac_shader_abi
*abi
)
1931 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1933 LLVMValueRef values
[3];
1934 LLVMValueRef result
;
1936 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1938 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1939 unsigned sizes
[3] = {
1940 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1941 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1942 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1945 for (i
= 0; i
< 3; ++i
)
1946 values
[i
] = LLVMConstInt(ctx
->i32
, sizes
[i
], 0);
1948 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
1950 result
= LLVMGetParam(ctx
->main_fn
, ctx
->param_block_size
);
1957 * Load a dword from a constant buffer.
1959 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1960 LLVMValueRef resource
,
1961 LLVMValueRef offset
)
1963 return ac_build_buffer_load(&ctx
->ac
, resource
, 1, NULL
, offset
, NULL
,
1964 0, 0, 0, true, true);
1967 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
, LLVMValueRef sample_id
)
1969 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1970 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1971 LLVMValueRef buf_index
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_SAMPLE_POSITIONS
, 0);
1972 LLVMValueRef resource
= ac_build_load_to_sgpr(&ctx
->ac
, desc
, buf_index
);
1974 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1975 LLVMValueRef offset0
= LLVMBuildMul(ctx
->ac
.builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, 0), "");
1976 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->ac
.builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1978 LLVMValueRef pos
[4] = {
1979 buffer_load_const(ctx
, resource
, offset0
),
1980 buffer_load_const(ctx
, resource
, offset1
),
1981 LLVMConstReal(ctx
->f32
, 0),
1982 LLVMConstReal(ctx
->f32
, 0)
1985 return ac_build_gather_values(&ctx
->ac
, pos
, 4);
1988 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
1990 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1991 return ac_to_integer(&ctx
->ac
, abi
->sample_coverage
);
1994 static LLVMValueRef
si_load_tess_coord(struct ac_shader_abi
*abi
)
1996 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1997 LLVMValueRef coord
[4] = {
1998 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_u
),
1999 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_v
),
2004 /* For triangles, the vector should be (u, v, 1-u-v). */
2005 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
2006 PIPE_PRIM_TRIANGLES
) {
2007 coord
[2] = LLVMBuildFSub(ctx
->ac
.builder
, ctx
->ac
.f32_1
,
2008 LLVMBuildFAdd(ctx
->ac
.builder
,
2009 coord
[0], coord
[1], ""), "");
2011 return ac_build_gather_values(&ctx
->ac
, coord
, 4);
2014 static LLVMValueRef
load_tess_level(struct si_shader_context
*ctx
,
2015 unsigned semantic_name
)
2017 LLVMValueRef base
, addr
;
2019 int param
= si_shader_io_get_unique_index_patch(semantic_name
, 0);
2021 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
2022 addr
= get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
), NULL
,
2023 LLVMConstInt(ctx
->i32
, param
, 0));
2025 return buffer_load(&ctx
->bld_base
, ctx
->f32
,
2026 ~0, ctx
->tess_offchip_ring
, base
, addr
, true);
2030 static LLVMValueRef
si_load_tess_level(struct ac_shader_abi
*abi
,
2031 unsigned varying_id
)
2033 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2034 unsigned semantic_name
;
2036 switch (varying_id
) {
2037 case VARYING_SLOT_TESS_LEVEL_INNER
:
2038 semantic_name
= TGSI_SEMANTIC_TESSINNER
;
2040 case VARYING_SLOT_TESS_LEVEL_OUTER
:
2041 semantic_name
= TGSI_SEMANTIC_TESSOUTER
;
2044 unreachable("unknown tess level");
2047 return load_tess_level(ctx
, semantic_name
);
2051 static LLVMValueRef
si_load_patch_vertices_in(struct ac_shader_abi
*abi
)
2053 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2054 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2055 return si_unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 13, 6);
2056 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
2057 return get_num_tcs_out_vertices(ctx
);
2059 unreachable("invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
2062 void si_load_system_value(struct si_shader_context
*ctx
,
2064 const struct tgsi_full_declaration
*decl
)
2066 LLVMValueRef value
= 0;
2068 assert(index
< RADEON_LLVM_MAX_SYSTEM_VALUES
);
2070 switch (decl
->Semantic
.Name
) {
2071 case TGSI_SEMANTIC_INSTANCEID
:
2072 value
= ctx
->abi
.instance_id
;
2075 case TGSI_SEMANTIC_VERTEXID
:
2076 value
= LLVMBuildAdd(ctx
->ac
.builder
,
2078 ctx
->abi
.base_vertex
, "");
2081 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
2082 /* Unused. Clarify the meaning in indexed vs. non-indexed
2083 * draws if this is ever used again. */
2087 case TGSI_SEMANTIC_BASEVERTEX
:
2088 value
= get_base_vertex(&ctx
->abi
);
2091 case TGSI_SEMANTIC_BASEINSTANCE
:
2092 value
= ctx
->abi
.start_instance
;
2095 case TGSI_SEMANTIC_DRAWID
:
2096 value
= ctx
->abi
.draw_id
;
2099 case TGSI_SEMANTIC_INVOCATIONID
:
2100 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2101 value
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
2102 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
2103 value
= ctx
->abi
.gs_invocation_id
;
2105 assert(!"INVOCATIONID not implemented");
2108 case TGSI_SEMANTIC_POSITION
:
2110 LLVMValueRef pos
[4] = {
2111 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2112 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2113 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
2114 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
2115 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_W_FLOAT
)),
2117 value
= ac_build_gather_values(&ctx
->ac
, pos
, 4);
2121 case TGSI_SEMANTIC_FACE
:
2122 value
= ctx
->abi
.front_face
;
2125 case TGSI_SEMANTIC_SAMPLEID
:
2126 value
= si_get_sample_id(ctx
);
2129 case TGSI_SEMANTIC_SAMPLEPOS
: {
2130 LLVMValueRef pos
[4] = {
2131 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2132 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2133 LLVMConstReal(ctx
->f32
, 0),
2134 LLVMConstReal(ctx
->f32
, 0)
2136 pos
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2137 pos
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2138 value
= ac_build_gather_values(&ctx
->ac
, pos
, 4);
2142 case TGSI_SEMANTIC_SAMPLEMASK
:
2143 /* This can only occur with the OpenGL Core profile, which
2144 * doesn't support smoothing.
2146 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
2149 case TGSI_SEMANTIC_TESSCOORD
:
2150 value
= si_load_tess_coord(&ctx
->abi
);
2153 case TGSI_SEMANTIC_VERTICESIN
:
2154 value
= si_load_patch_vertices_in(&ctx
->abi
);
2157 case TGSI_SEMANTIC_TESSINNER
:
2158 case TGSI_SEMANTIC_TESSOUTER
:
2159 value
= load_tess_level(ctx
, decl
->Semantic
.Name
);
2162 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
2163 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
2165 LLVMValueRef buf
, slot
, val
[4];
2168 slot
= LLVMConstInt(ctx
->i32
, SI_HS_CONST_DEFAULT_TESS_LEVELS
, 0);
2169 buf
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2170 buf
= ac_build_load_to_sgpr(&ctx
->ac
, buf
, slot
);
2171 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
2173 for (i
= 0; i
< 4; i
++)
2174 val
[i
] = buffer_load_const(ctx
, buf
,
2175 LLVMConstInt(ctx
->i32
, (offset
+ i
) * 4, 0));
2176 value
= ac_build_gather_values(&ctx
->ac
, val
, 4);
2180 case TGSI_SEMANTIC_PRIMID
:
2181 value
= get_primitive_id(ctx
, 0);
2184 case TGSI_SEMANTIC_GRID_SIZE
:
2185 value
= ctx
->abi
.num_work_groups
;
2188 case TGSI_SEMANTIC_BLOCK_SIZE
:
2189 value
= get_block_size(&ctx
->abi
);
2192 case TGSI_SEMANTIC_BLOCK_ID
:
2194 LLVMValueRef values
[3];
2196 for (int i
= 0; i
< 3; i
++) {
2197 values
[i
] = ctx
->i32_0
;
2198 if (ctx
->abi
.workgroup_ids
[i
]) {
2199 values
[i
] = ctx
->abi
.workgroup_ids
[i
];
2202 value
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2206 case TGSI_SEMANTIC_THREAD_ID
:
2207 value
= ctx
->abi
.local_invocation_ids
;
2210 case TGSI_SEMANTIC_HELPER_INVOCATION
:
2211 value
= ac_build_intrinsic(&ctx
->ac
,
2212 "llvm.amdgcn.ps.live",
2214 AC_FUNC_ATTR_READNONE
);
2215 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2216 value
= LLVMBuildSExt(ctx
->ac
.builder
, value
, ctx
->i32
, "");
2219 case TGSI_SEMANTIC_SUBGROUP_SIZE
:
2220 value
= LLVMConstInt(ctx
->i32
, 64, 0);
2223 case TGSI_SEMANTIC_SUBGROUP_INVOCATION
:
2224 value
= ac_get_thread_id(&ctx
->ac
);
2227 case TGSI_SEMANTIC_SUBGROUP_EQ_MASK
:
2229 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2230 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2231 value
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->i64
, 1, 0), id
, "");
2232 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2236 case TGSI_SEMANTIC_SUBGROUP_GE_MASK
:
2237 case TGSI_SEMANTIC_SUBGROUP_GT_MASK
:
2238 case TGSI_SEMANTIC_SUBGROUP_LE_MASK
:
2239 case TGSI_SEMANTIC_SUBGROUP_LT_MASK
:
2241 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2242 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_GT_MASK
||
2243 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
) {
2244 /* All bits set except LSB */
2245 value
= LLVMConstInt(ctx
->i64
, -2, 0);
2248 value
= LLVMConstInt(ctx
->i64
, -1, 0);
2250 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2251 value
= LLVMBuildShl(ctx
->ac
.builder
, value
, id
, "");
2252 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
||
2253 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LT_MASK
)
2254 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2255 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2259 case TGSI_SEMANTIC_CS_USER_DATA
:
2260 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_cs_user_data
);
2264 assert(!"unknown system value");
2268 ctx
->system_values
[index
] = value
;
2271 void si_declare_compute_memory(struct si_shader_context
*ctx
)
2273 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2274 unsigned lds_size
= sel
->info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
];
2276 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, AC_ADDR_SPACE_LDS
);
2279 assert(!ctx
->ac
.lds
);
2281 var
= LLVMAddGlobalInAddressSpace(ctx
->ac
.module
,
2282 LLVMArrayType(ctx
->i8
, lds_size
),
2285 LLVMSetAlignment(var
, 4);
2287 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, var
, i8p
, "");
2290 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
2291 const struct tgsi_full_declaration
*decl
)
2293 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
2294 assert(decl
->Range
.First
== decl
->Range
.Last
);
2296 si_declare_compute_memory(ctx
);
2299 static LLVMValueRef
load_const_buffer_desc_fast_path(struct si_shader_context
*ctx
)
2302 LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2303 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2305 /* Do the bounds checking with a descriptor, because
2306 * doing computation and manual bounds checking of 64-bit
2307 * addresses generates horrible VALU code with very high
2308 * VGPR usage and very low SIMD occupancy.
2310 ptr
= LLVMBuildPtrToInt(ctx
->ac
.builder
, ptr
, ctx
->ac
.intptr
, "");
2312 LLVMValueRef desc0
, desc1
;
2313 if (HAVE_32BIT_POINTERS
) {
2315 desc1
= LLVMConstInt(ctx
->i32
,
2316 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
2318 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->v2i32
, "");
2319 desc0
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_0
, "");
2320 desc1
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_1
, "");
2321 /* Mask out all bits except BASE_ADDRESS_HI. */
2322 desc1
= LLVMBuildAnd(ctx
->ac
.builder
, desc1
,
2323 LLVMConstInt(ctx
->i32
, ~C_008F04_BASE_ADDRESS_HI
, 0), "");
2326 LLVMValueRef desc_elems
[] = {
2329 LLVMConstInt(ctx
->i32
, (sel
->info
.const_file_max
[0] + 1) * 16, 0),
2330 LLVMConstInt(ctx
->i32
,
2331 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
2332 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
2333 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
2334 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
2335 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
2336 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0)
2339 return ac_build_gather_values(&ctx
->ac
, desc_elems
, 4);
2342 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
2344 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
2345 ctx
->param_const_and_shader_buffers
);
2347 return ac_build_load_to_sgpr(&ctx
->ac
, list_ptr
,
2348 LLVMConstInt(ctx
->i32
, si_get_constbuf_slot(i
), 0));
2351 static LLVMValueRef
load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef index
)
2353 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2354 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2356 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2358 if (sel
->info
.const_buffers_declared
== 1 &&
2359 sel
->info
.shader_buffers_declared
== 0) {
2360 return load_const_buffer_desc_fast_path(ctx
);
2363 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_const_buffers
);
2364 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2365 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2367 return ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2371 load_ssbo(struct ac_shader_abi
*abi
, LLVMValueRef index
, bool write
)
2373 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2374 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
2375 ctx
->param_const_and_shader_buffers
);
2377 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_shader_buffers
);
2378 index
= LLVMBuildSub(ctx
->ac
.builder
,
2379 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
- 1, 0),
2382 return ac_build_load_to_sgpr(&ctx
->ac
, rsrc_ptr
, index
);
2385 static LLVMValueRef
fetch_constant(
2386 struct lp_build_tgsi_context
*bld_base
,
2387 const struct tgsi_full_src_register
*reg
,
2388 enum tgsi_opcode_type type
,
2389 unsigned swizzle_in
)
2391 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2392 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2393 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
2395 unsigned swizzle
= swizzle_in
& 0xffff;
2397 LLVMValueRef addr
, bufp
;
2399 if (swizzle_in
== LP_CHAN_ALL
) {
2401 LLVMValueRef values
[4];
2402 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
2403 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
2405 return ac_build_gather_values(&ctx
->ac
, values
, 4);
2408 /* Split 64-bit loads. */
2409 if (tgsi_type_is_64bit(type
)) {
2410 LLVMValueRef lo
, hi
;
2412 lo
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, swizzle
);
2413 hi
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, (swizzle_in
>> 16));
2414 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
2418 idx
= reg
->Register
.Index
* 4 + swizzle
;
2419 if (reg
->Register
.Indirect
) {
2420 addr
= si_get_indirect_index(ctx
, ireg
, 16, idx
* 4);
2422 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
2425 /* Fast path when user data SGPRs point to constant buffer 0 directly. */
2426 if (sel
->info
.const_buffers_declared
== 1 &&
2427 sel
->info
.shader_buffers_declared
== 0) {
2428 LLVMValueRef desc
= load_const_buffer_desc_fast_path(ctx
);
2429 LLVMValueRef result
= buffer_load_const(ctx
, desc
, addr
);
2430 return bitcast(bld_base
, type
, result
);
2433 assert(reg
->Register
.Dimension
);
2434 buf
= reg
->Dimension
.Index
;
2436 if (reg
->Dimension
.Indirect
) {
2437 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2439 index
= si_get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
2440 reg
->Dimension
.Index
,
2441 ctx
->num_const_buffers
);
2442 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2443 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2444 bufp
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2446 bufp
= load_const_buffer_desc(ctx
, buf
);
2448 return bitcast(bld_base
, type
, buffer_load_const(ctx
, bufp
, addr
));
2451 /* Initialize arguments for the shader export intrinsic */
2452 static void si_llvm_init_export_args(struct si_shader_context
*ctx
,
2453 LLVMValueRef
*values
,
2455 struct ac_export_args
*args
)
2457 LLVMValueRef f32undef
= LLVMGetUndef(ctx
->ac
.f32
);
2458 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
2460 bool is_int8
, is_int10
;
2462 /* Default is 0xf. Adjusted below depending on the format. */
2463 args
->enabled_channels
= 0xf; /* writemask */
2465 /* Specify whether the EXEC mask represents the valid mask */
2466 args
->valid_mask
= 0;
2468 /* Specify whether this is the last export */
2471 /* Specify the target we are exporting */
2472 args
->target
= target
;
2474 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
2475 const struct si_shader_key
*key
= &ctx
->shader
->key
;
2476 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
2477 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
2479 assert(cbuf
>= 0 && cbuf
< 8);
2480 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
2481 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
2482 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
2485 args
->compr
= false;
2486 args
->out
[0] = f32undef
;
2487 args
->out
[1] = f32undef
;
2488 args
->out
[2] = f32undef
;
2489 args
->out
[3] = f32undef
;
2491 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
2492 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
2493 unsigned bits
, bool hi
) = NULL
;
2495 switch (spi_shader_col_format
) {
2496 case V_028714_SPI_SHADER_ZERO
:
2497 args
->enabled_channels
= 0; /* writemask */
2498 args
->target
= V_008DFC_SQ_EXP_NULL
;
2501 case V_028714_SPI_SHADER_32_R
:
2502 args
->enabled_channels
= 1; /* writemask */
2503 args
->out
[0] = values
[0];
2506 case V_028714_SPI_SHADER_32_GR
:
2507 args
->enabled_channels
= 0x3; /* writemask */
2508 args
->out
[0] = values
[0];
2509 args
->out
[1] = values
[1];
2512 case V_028714_SPI_SHADER_32_AR
:
2513 args
->enabled_channels
= 0x9; /* writemask */
2514 args
->out
[0] = values
[0];
2515 args
->out
[3] = values
[3];
2518 case V_028714_SPI_SHADER_FP16_ABGR
:
2519 packf
= ac_build_cvt_pkrtz_f16
;
2522 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2523 packf
= ac_build_cvt_pknorm_u16
;
2526 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2527 packf
= ac_build_cvt_pknorm_i16
;
2530 case V_028714_SPI_SHADER_UINT16_ABGR
:
2531 packi
= ac_build_cvt_pk_u16
;
2534 case V_028714_SPI_SHADER_SINT16_ABGR
:
2535 packi
= ac_build_cvt_pk_i16
;
2538 case V_028714_SPI_SHADER_32_ABGR
:
2539 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2543 /* Pack f16 or norm_i16/u16. */
2545 for (chan
= 0; chan
< 2; chan
++) {
2546 LLVMValueRef pack_args
[2] = {
2548 values
[2 * chan
+ 1]
2550 LLVMValueRef packed
;
2552 packed
= packf(&ctx
->ac
, pack_args
);
2553 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2555 args
->compr
= 1; /* COMPR flag */
2559 for (chan
= 0; chan
< 2; chan
++) {
2560 LLVMValueRef pack_args
[2] = {
2561 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
2562 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
2564 LLVMValueRef packed
;
2566 packed
= packi(&ctx
->ac
, pack_args
,
2567 is_int8
? 8 : is_int10
? 10 : 16,
2569 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2571 args
->compr
= 1; /* COMPR flag */
2575 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2578 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2580 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2581 static LLVMRealPredicate cond_map
[PIPE_FUNC_ALWAYS
+ 1] = {
2582 [PIPE_FUNC_LESS
] = LLVMRealOLT
,
2583 [PIPE_FUNC_EQUAL
] = LLVMRealOEQ
,
2584 [PIPE_FUNC_LEQUAL
] = LLVMRealOLE
,
2585 [PIPE_FUNC_GREATER
] = LLVMRealOGT
,
2586 [PIPE_FUNC_NOTEQUAL
] = LLVMRealONE
,
2587 [PIPE_FUNC_GEQUAL
] = LLVMRealOGE
,
2589 LLVMRealPredicate cond
= cond_map
[ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
];
2592 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2593 SI_PARAM_ALPHA_REF
);
2594 LLVMValueRef alpha_pass
=
2595 LLVMBuildFCmp(ctx
->ac
.builder
, cond
, alpha
, alpha_ref
, "");
2596 ac_build_kill_if_false(&ctx
->ac
, alpha_pass
);
2598 ac_build_kill_if_false(&ctx
->ac
, ctx
->i1false
);
2602 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2604 unsigned samplemask_param
)
2606 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2607 LLVMValueRef coverage
;
2609 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2610 coverage
= LLVMGetParam(ctx
->main_fn
,
2612 coverage
= ac_to_integer(&ctx
->ac
, coverage
);
2614 coverage
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32",
2616 &coverage
, 1, AC_FUNC_ATTR_READNONE
);
2618 coverage
= LLVMBuildUIToFP(ctx
->ac
.builder
, coverage
,
2621 coverage
= LLVMBuildFMul(ctx
->ac
.builder
, coverage
,
2622 LLVMConstReal(ctx
->f32
,
2623 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2625 return LLVMBuildFMul(ctx
->ac
.builder
, alpha
, coverage
, "");
2628 static void si_llvm_emit_clipvertex(struct si_shader_context
*ctx
,
2629 struct ac_export_args
*pos
, LLVMValueRef
*out_elts
)
2633 unsigned const_chan
;
2634 LLVMValueRef base_elt
;
2635 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2636 LLVMValueRef constbuf_index
= LLVMConstInt(ctx
->i32
,
2637 SI_VS_CONST_CLIP_PLANES
, 0);
2638 LLVMValueRef const_resource
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, constbuf_index
);
2640 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2641 struct ac_export_args
*args
= &pos
[2 + reg_index
];
2646 args
->out
[3] = LLVMConstReal(ctx
->f32
, 0.0f
);
2648 /* Compute dot products of position and user clip plane vectors */
2649 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2650 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2652 LLVMConstInt(ctx
->i32
, ((reg_index
* 4 + chan
) * 4 +
2653 const_chan
) * 4, 0);
2654 base_elt
= buffer_load_const(ctx
, const_resource
,
2656 args
->out
[chan
] = ac_build_fmad(&ctx
->ac
, base_elt
,
2657 out_elts
[const_chan
], args
->out
[chan
]);
2661 args
->enabled_channels
= 0xf;
2662 args
->valid_mask
= 0;
2664 args
->target
= V_008DFC_SQ_EXP_POS
+ 2 + reg_index
;
2669 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2673 if (so
->num_outputs
)
2674 fprintf(stderr
, "STREAMOUT\n");
2676 for (i
= 0; i
< so
->num_outputs
; i
++) {
2677 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2678 so
->output
[i
].start_component
;
2679 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2680 i
, so
->output
[i
].output_buffer
,
2681 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2682 so
->output
[i
].register_index
,
2683 mask
& 1 ? "x" : "",
2684 mask
& 2 ? "y" : "",
2685 mask
& 4 ? "z" : "",
2686 mask
& 8 ? "w" : "");
2690 static void emit_streamout_output(struct si_shader_context
*ctx
,
2691 LLVMValueRef
const *so_buffers
,
2692 LLVMValueRef
const *so_write_offsets
,
2693 struct pipe_stream_output
*stream_out
,
2694 struct si_shader_output_values
*shader_out
)
2696 unsigned buf_idx
= stream_out
->output_buffer
;
2697 unsigned start
= stream_out
->start_component
;
2698 unsigned num_comps
= stream_out
->num_components
;
2699 LLVMValueRef out
[4];
2701 assert(num_comps
&& num_comps
<= 4);
2702 if (!num_comps
|| num_comps
> 4)
2705 /* Load the output as int. */
2706 for (int j
= 0; j
< num_comps
; j
++) {
2707 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2709 out
[j
] = ac_to_integer(&ctx
->ac
, shader_out
->values
[start
+ j
]);
2712 /* Pack the output. */
2713 LLVMValueRef vdata
= NULL
;
2715 switch (num_comps
) {
2716 case 1: /* as i32 */
2719 case 2: /* as v2i32 */
2720 case 3: /* as v4i32 (aligned to 4) */
2721 out
[3] = LLVMGetUndef(ctx
->i32
);
2723 case 4: /* as v4i32 */
2724 vdata
= ac_build_gather_values(&ctx
->ac
, out
, util_next_power_of_two(num_comps
));
2728 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf_idx
],
2730 so_write_offsets
[buf_idx
],
2732 stream_out
->dst_offset
* 4, 1, 1, true, false);
2736 * Write streamout data to buffers for vertex stream @p stream (different
2737 * vertex streams can occur for GS copy shaders).
2739 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2740 struct si_shader_output_values
*outputs
,
2741 unsigned noutput
, unsigned stream
)
2743 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2744 struct pipe_stream_output_info
*so
= &sel
->so
;
2745 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2747 struct lp_build_if_state if_ctx
;
2749 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2750 LLVMValueRef so_vtx_count
=
2751 si_unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2753 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2755 /* can_emit = tid < so_vtx_count; */
2756 LLVMValueRef can_emit
=
2757 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2759 /* Emit the streamout code conditionally. This actually avoids
2760 * out-of-bounds buffer access. The hw tells us via the SGPR
2761 * (so_vtx_count) which threads are allowed to emit streamout data. */
2762 lp_build_if(&if_ctx
, &ctx
->gallivm
, can_emit
);
2764 /* The buffer offset is computed as follows:
2765 * ByteOffset = streamout_offset[buffer_id]*4 +
2766 * (streamout_write_index + thread_id)*stride[buffer_id] +
2770 LLVMValueRef so_write_index
=
2771 LLVMGetParam(ctx
->main_fn
,
2772 ctx
->param_streamout_write_index
);
2774 /* Compute (streamout_write_index + thread_id). */
2775 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2777 /* Load the descriptor and compute the write offset for each
2778 * enabled buffer. */
2779 LLVMValueRef so_write_offset
[4] = {};
2780 LLVMValueRef so_buffers
[4];
2781 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2782 ctx
->param_rw_buffers
);
2784 for (i
= 0; i
< 4; i
++) {
2788 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
,
2789 SI_VS_STREAMOUT_BUF0
+ i
, 0);
2791 so_buffers
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
2793 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2794 ctx
->param_streamout_offset
[i
]);
2795 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2797 so_write_offset
[i
] = ac_build_imad(&ctx
->ac
, so_write_index
,
2798 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0),
2802 /* Write streamout data. */
2803 for (i
= 0; i
< so
->num_outputs
; i
++) {
2804 unsigned reg
= so
->output
[i
].register_index
;
2809 if (stream
!= so
->output
[i
].stream
)
2812 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2813 &so
->output
[i
], &outputs
[reg
]);
2816 lp_build_endif(&if_ctx
);
2819 static void si_export_param(struct si_shader_context
*ctx
, unsigned index
,
2820 LLVMValueRef
*values
)
2822 struct ac_export_args args
;
2824 si_llvm_init_export_args(ctx
, values
,
2825 V_008DFC_SQ_EXP_PARAM
+ index
, &args
);
2826 ac_build_export(&ctx
->ac
, &args
);
2829 static void si_build_param_exports(struct si_shader_context
*ctx
,
2830 struct si_shader_output_values
*outputs
,
2833 struct si_shader
*shader
= ctx
->shader
;
2834 unsigned param_count
= 0;
2836 for (unsigned i
= 0; i
< noutput
; i
++) {
2837 unsigned semantic_name
= outputs
[i
].semantic_name
;
2838 unsigned semantic_index
= outputs
[i
].semantic_index
;
2840 if (outputs
[i
].vertex_stream
[0] != 0 &&
2841 outputs
[i
].vertex_stream
[1] != 0 &&
2842 outputs
[i
].vertex_stream
[2] != 0 &&
2843 outputs
[i
].vertex_stream
[3] != 0)
2846 switch (semantic_name
) {
2847 case TGSI_SEMANTIC_LAYER
:
2848 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2849 case TGSI_SEMANTIC_CLIPDIST
:
2850 case TGSI_SEMANTIC_COLOR
:
2851 case TGSI_SEMANTIC_BCOLOR
:
2852 case TGSI_SEMANTIC_PRIMID
:
2853 case TGSI_SEMANTIC_FOG
:
2854 case TGSI_SEMANTIC_TEXCOORD
:
2855 case TGSI_SEMANTIC_GENERIC
:
2861 if ((semantic_name
!= TGSI_SEMANTIC_GENERIC
||
2862 semantic_index
< SI_MAX_IO_GENERIC
) &&
2863 shader
->key
.opt
.kill_outputs
&
2864 (1ull << si_shader_io_get_unique_index(semantic_name
,
2865 semantic_index
, true)))
2868 si_export_param(ctx
, param_count
, outputs
[i
].values
);
2870 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2871 shader
->info
.vs_output_param_offset
[i
] = param_count
++;
2874 shader
->info
.nr_param_exports
= param_count
;
2877 /* Generate export instructions for hardware VS shader stage */
2878 static void si_llvm_export_vs(struct si_shader_context
*ctx
,
2879 struct si_shader_output_values
*outputs
,
2882 struct si_shader
*shader
= ctx
->shader
;
2883 struct ac_export_args pos_args
[4] = {};
2884 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2888 /* Build position exports. */
2889 for (i
= 0; i
< noutput
; i
++) {
2890 switch (outputs
[i
].semantic_name
) {
2891 case TGSI_SEMANTIC_POSITION
:
2892 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2893 V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
2895 case TGSI_SEMANTIC_PSIZE
:
2896 psize_value
= outputs
[i
].values
[0];
2898 case TGSI_SEMANTIC_LAYER
:
2899 layer_value
= outputs
[i
].values
[0];
2901 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2902 viewport_index_value
= outputs
[i
].values
[0];
2904 case TGSI_SEMANTIC_EDGEFLAG
:
2905 edgeflag_value
= outputs
[i
].values
[0];
2907 case TGSI_SEMANTIC_CLIPDIST
:
2908 if (!shader
->key
.opt
.clip_disable
) {
2909 unsigned index
= 2 + outputs
[i
].semantic_index
;
2910 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2911 V_008DFC_SQ_EXP_POS
+ index
,
2915 case TGSI_SEMANTIC_CLIPVERTEX
:
2916 if (!shader
->key
.opt
.clip_disable
) {
2917 si_llvm_emit_clipvertex(ctx
, pos_args
,
2924 /* We need to add the position output manually if it's missing. */
2925 if (!pos_args
[0].out
[0]) {
2926 pos_args
[0].enabled_channels
= 0xf; /* writemask */
2927 pos_args
[0].valid_mask
= 0; /* EXEC mask */
2928 pos_args
[0].done
= 0; /* last export? */
2929 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
2930 pos_args
[0].compr
= 0; /* COMPR flag */
2931 pos_args
[0].out
[0] = ctx
->ac
.f32_0
; /* X */
2932 pos_args
[0].out
[1] = ctx
->ac
.f32_0
; /* Y */
2933 pos_args
[0].out
[2] = ctx
->ac
.f32_0
; /* Z */
2934 pos_args
[0].out
[3] = ctx
->ac
.f32_1
; /* W */
2937 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2938 if (shader
->selector
->info
.writes_psize
||
2939 shader
->selector
->info
.writes_edgeflag
||
2940 shader
->selector
->info
.writes_viewport_index
||
2941 shader
->selector
->info
.writes_layer
) {
2942 pos_args
[1].enabled_channels
= shader
->selector
->info
.writes_psize
|
2943 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2944 (shader
->selector
->info
.writes_layer
<< 2);
2946 pos_args
[1].valid_mask
= 0; /* EXEC mask */
2947 pos_args
[1].done
= 0; /* last export? */
2948 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
2949 pos_args
[1].compr
= 0; /* COMPR flag */
2950 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
2951 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
2952 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
2953 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
2955 if (shader
->selector
->info
.writes_psize
)
2956 pos_args
[1].out
[0] = psize_value
;
2958 if (shader
->selector
->info
.writes_edgeflag
) {
2959 /* The output is a float, but the hw expects an integer
2960 * with the first bit containing the edge flag. */
2961 edgeflag_value
= LLVMBuildFPToUI(ctx
->ac
.builder
,
2964 edgeflag_value
= ac_build_umin(&ctx
->ac
,
2968 /* The LLVM intrinsic expects a float. */
2969 pos_args
[1].out
[1] = ac_to_float(&ctx
->ac
, edgeflag_value
);
2972 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
2973 /* GFX9 has the layer in out.z[10:0] and the viewport
2974 * index in out.z[19:16].
2976 if (shader
->selector
->info
.writes_layer
)
2977 pos_args
[1].out
[2] = layer_value
;
2979 if (shader
->selector
->info
.writes_viewport_index
) {
2980 LLVMValueRef v
= viewport_index_value
;
2982 v
= ac_to_integer(&ctx
->ac
, v
);
2983 v
= LLVMBuildShl(ctx
->ac
.builder
, v
,
2984 LLVMConstInt(ctx
->i32
, 16, 0), "");
2985 v
= LLVMBuildOr(ctx
->ac
.builder
, v
,
2986 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
2987 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
2988 pos_args
[1].enabled_channels
|= 1 << 2;
2991 if (shader
->selector
->info
.writes_layer
)
2992 pos_args
[1].out
[2] = layer_value
;
2994 if (shader
->selector
->info
.writes_viewport_index
) {
2995 pos_args
[1].out
[3] = viewport_index_value
;
2996 pos_args
[1].enabled_channels
|= 1 << 3;
3001 for (i
= 0; i
< 4; i
++)
3002 if (pos_args
[i
].out
[0])
3003 shader
->info
.nr_pos_exports
++;
3006 for (i
= 0; i
< 4; i
++) {
3007 if (!pos_args
[i
].out
[0])
3010 /* Specify the target we are exporting */
3011 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
3013 if (pos_idx
== shader
->info
.nr_pos_exports
)
3014 /* Specify that this is the last export */
3015 pos_args
[i
].done
= 1;
3017 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
3020 /* Build parameter exports. */
3021 si_build_param_exports(ctx
, outputs
, noutput
);
3025 * Forward all outputs from the vertex shader to the TES. This is only used
3026 * for the fixed function TCS.
3028 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
3030 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3031 LLVMValueRef invocation_id
, buffer
, buffer_offset
;
3032 LLVMValueRef lds_vertex_stride
, lds_base
;
3035 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3036 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3037 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3039 lds_vertex_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3040 lds_base
= get_tcs_in_current_patch_offset(ctx
);
3041 lds_base
= ac_build_imad(&ctx
->ac
, invocation_id
, lds_vertex_stride
,
3044 inputs
= ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
;
3046 unsigned i
= u_bit_scan64(&inputs
);
3048 LLVMValueRef lds_ptr
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3049 LLVMConstInt(ctx
->i32
, 4 * i
, 0),
3052 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
3053 get_rel_patch_id(ctx
),
3055 LLVMConstInt(ctx
->i32
, i
, 0));
3057 LLVMValueRef value
= lds_load(bld_base
, ctx
->ac
.i32
, ~0,
3060 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buffer_addr
,
3061 buffer_offset
, 0, 1, 0, true, false);
3065 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
3066 LLVMValueRef rel_patch_id
,
3067 LLVMValueRef invocation_id
,
3068 LLVMValueRef tcs_out_current_patch_data_offset
,
3069 LLVMValueRef invoc0_tf_outer
[4],
3070 LLVMValueRef invoc0_tf_inner
[2])
3072 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3073 struct si_shader
*shader
= ctx
->shader
;
3074 unsigned tess_inner_index
, tess_outer_index
;
3075 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
3076 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
3077 unsigned stride
, outer_comps
, inner_comps
, i
, offset
;
3078 struct lp_build_if_state if_ctx
, inner_if_ctx
;
3080 /* Add a barrier before loading tess factors from LDS. */
3081 if (!shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
)
3082 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
3084 /* Do this only for invocation 0, because the tess levels are per-patch,
3087 * This can't jump, because invocation 0 executes this. It should
3088 * at least mask out the loads and stores for other invocations.
3090 lp_build_if(&if_ctx
, &ctx
->gallivm
,
3091 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3092 invocation_id
, ctx
->i32_0
, ""));
3094 /* Determine the layout of one tess factor element in the buffer. */
3095 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
3096 case PIPE_PRIM_LINES
:
3097 stride
= 2; /* 2 dwords, 1 vec2 store */
3101 case PIPE_PRIM_TRIANGLES
:
3102 stride
= 4; /* 4 dwords, 1 vec4 store */
3106 case PIPE_PRIM_QUADS
:
3107 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
3116 for (i
= 0; i
< 4; i
++) {
3117 inner
[i
] = LLVMGetUndef(ctx
->i32
);
3118 outer
[i
] = LLVMGetUndef(ctx
->i32
);
3121 if (shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
) {
3122 /* Tess factors are in VGPRs. */
3123 for (i
= 0; i
< outer_comps
; i
++)
3124 outer
[i
] = out
[i
] = invoc0_tf_outer
[i
];
3125 for (i
= 0; i
< inner_comps
; i
++)
3126 inner
[i
] = out
[outer_comps
+i
] = invoc0_tf_inner
[i
];
3128 /* Load tess_inner and tess_outer from LDS.
3129 * Any invocation can write them, so we can't get them from a temporary.
3131 tess_inner_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER
, 0);
3132 tess_outer_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER
, 0);
3134 lds_base
= tcs_out_current_patch_data_offset
;
3135 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3136 LLVMConstInt(ctx
->i32
,
3137 tess_inner_index
* 4, 0), "");
3138 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3139 LLVMConstInt(ctx
->i32
,
3140 tess_outer_index
* 4, 0), "");
3142 for (i
= 0; i
< outer_comps
; i
++) {
3144 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_outer
);
3146 for (i
= 0; i
< inner_comps
; i
++) {
3147 inner
[i
] = out
[outer_comps
+i
] =
3148 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_inner
);
3152 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
3153 /* For isolines, the hardware expects tess factors in the
3154 * reverse order from what GLSL / TGSI specify.
3156 LLVMValueRef tmp
= out
[0];
3161 /* Convert the outputs to vectors for stores. */
3162 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
3166 vec1
= ac_build_gather_values(&ctx
->ac
, out
+4, stride
- 4);
3168 /* Get the buffer. */
3169 buffer
= get_tess_ring_descriptor(ctx
, TCS_FACTOR_RING
);
3171 /* Get the offset. */
3172 tf_base
= LLVMGetParam(ctx
->main_fn
,
3173 ctx
->param_tcs_factor_offset
);
3174 byteoffset
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
3175 LLVMConstInt(ctx
->i32
, 4 * stride
, 0), "");
3177 lp_build_if(&inner_if_ctx
, &ctx
->gallivm
,
3178 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3179 rel_patch_id
, ctx
->i32_0
, ""));
3181 /* Store the dynamic HS control word. */
3183 if (ctx
->screen
->info
.chip_class
<= VI
) {
3184 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
3185 LLVMConstInt(ctx
->i32
, 0x80000000, 0),
3186 1, ctx
->i32_0
, tf_base
,
3187 offset
, 1, 0, true, false);
3191 lp_build_endif(&inner_if_ctx
);
3193 /* Store the tessellation factors. */
3194 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
3195 MIN2(stride
, 4), byteoffset
, tf_base
,
3196 offset
, 1, 0, true, false);
3199 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
3200 stride
- 4, byteoffset
, tf_base
,
3201 offset
, 1, 0, true, false);
3203 /* Store the tess factors into the offchip buffer if TES reads them. */
3204 if (shader
->key
.part
.tcs
.epilog
.tes_reads_tess_factors
) {
3205 LLVMValueRef buf
, base
, inner_vec
, outer_vec
, tf_outer_offset
;
3206 LLVMValueRef tf_inner_offset
;
3207 unsigned param_outer
, param_inner
;
3209 buf
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3210 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3212 param_outer
= si_shader_io_get_unique_index_patch(
3213 TGSI_SEMANTIC_TESSOUTER
, 0);
3214 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3215 LLVMConstInt(ctx
->i32
, param_outer
, 0));
3217 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
3218 util_next_power_of_two(outer_comps
));
3220 ac_build_buffer_store_dword(&ctx
->ac
, buf
, outer_vec
,
3221 outer_comps
, tf_outer_offset
,
3222 base
, 0, 1, 0, true, false);
3224 param_inner
= si_shader_io_get_unique_index_patch(
3225 TGSI_SEMANTIC_TESSINNER
, 0);
3226 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3227 LLVMConstInt(ctx
->i32
, param_inner
, 0));
3229 inner_vec
= inner_comps
== 1 ? inner
[0] :
3230 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
3231 ac_build_buffer_store_dword(&ctx
->ac
, buf
, inner_vec
,
3232 inner_comps
, tf_inner_offset
,
3233 base
, 0, 1, 0, true, false);
3237 lp_build_endif(&if_ctx
);
3241 si_insert_input_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3242 unsigned param
, unsigned return_index
)
3244 return LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3245 LLVMGetParam(ctx
->main_fn
, param
),
3250 si_insert_input_ret_float(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3251 unsigned param
, unsigned return_index
)
3253 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3254 LLVMValueRef p
= LLVMGetParam(ctx
->main_fn
, param
);
3256 return LLVMBuildInsertValue(builder
, ret
,
3257 ac_to_float(&ctx
->ac
, p
),
3262 si_insert_input_ptr(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3263 unsigned param
, unsigned return_index
)
3265 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3266 LLVMValueRef ptr
, lo
, hi
;
3268 if (HAVE_32BIT_POINTERS
) {
3269 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3270 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i32
, "");
3271 return LLVMBuildInsertValue(builder
, ret
, ptr
, return_index
, "");
3274 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3275 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i64
, "");
3276 ptr
= LLVMBuildBitCast(builder
, ptr
, ctx
->v2i32
, "");
3277 lo
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_0
, "");
3278 hi
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_1
, "");
3279 ret
= LLVMBuildInsertValue(builder
, ret
, lo
, return_index
, "");
3280 return LLVMBuildInsertValue(builder
, ret
, hi
, return_index
+ 1, "");
3283 /* This only writes the tessellation factor levels. */
3284 static void si_llvm_emit_tcs_epilogue(struct ac_shader_abi
*abi
,
3285 unsigned max_outputs
,
3286 LLVMValueRef
*addrs
)
3288 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3289 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
3290 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3291 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
3293 si_copy_tcs_inputs(bld_base
);
3295 rel_patch_id
= get_rel_patch_id(ctx
);
3296 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3297 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
3299 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3300 LLVMBasicBlockRef blocks
[2] = {
3301 LLVMGetInsertBlock(builder
),
3302 ctx
->merged_wrap_if_state
.entry_block
3304 LLVMValueRef values
[2];
3306 lp_build_endif(&ctx
->merged_wrap_if_state
);
3308 values
[0] = rel_patch_id
;
3309 values
[1] = LLVMGetUndef(ctx
->i32
);
3310 rel_patch_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3312 values
[0] = tf_lds_offset
;
3313 values
[1] = LLVMGetUndef(ctx
->i32
);
3314 tf_lds_offset
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3316 values
[0] = invocation_id
;
3317 values
[1] = ctx
->i32_1
; /* cause the epilog to skip threads */
3318 invocation_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3321 /* Return epilog parameters from this function. */
3322 LLVMValueRef ret
= ctx
->return_value
;
3325 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3326 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3327 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3328 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3329 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3330 /* Tess offchip and tess factor offsets are at the beginning. */
3331 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3332 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3333 vgpr
= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
+ 1;
3335 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3336 GFX6_SGPR_TCS_OFFCHIP_LAYOUT
);
3337 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3338 GFX6_SGPR_TCS_OUT_LAYOUT
);
3339 /* Tess offchip and tess factor offsets are after user SGPRs. */
3340 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
,
3341 GFX6_TCS_NUM_USER_SGPR
);
3342 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
,
3343 GFX6_TCS_NUM_USER_SGPR
+ 1);
3344 vgpr
= GFX6_TCS_NUM_USER_SGPR
+ 2;
3348 rel_patch_id
= ac_to_float(&ctx
->ac
, rel_patch_id
);
3349 invocation_id
= ac_to_float(&ctx
->ac
, invocation_id
);
3350 tf_lds_offset
= ac_to_float(&ctx
->ac
, tf_lds_offset
);
3352 /* Leave a hole corresponding to the two input VGPRs. This ensures that
3353 * the invocation_id output does not alias the tcs_rel_ids input,
3354 * which saves a V_MOV on gfx9.
3358 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
3359 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
3361 if (ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
3362 vgpr
++; /* skip the tess factor LDS offset */
3363 for (unsigned i
= 0; i
< 6; i
++) {
3364 LLVMValueRef value
=
3365 LLVMBuildLoad(builder
, ctx
->invoc0_tess_factors
[i
], "");
3366 value
= ac_to_float(&ctx
->ac
, value
);
3367 ret
= LLVMBuildInsertValue(builder
, ret
, value
, vgpr
++, "");
3370 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
3372 ctx
->return_value
= ret
;
3375 /* Pass TCS inputs from LS to TCS on GFX9. */
3376 static void si_set_ls_return_value_for_tcs(struct si_shader_context
*ctx
)
3378 LLVMValueRef ret
= ctx
->return_value
;
3380 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3381 if (HAVE_32BIT_POINTERS
)
3382 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3383 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3384 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3385 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3386 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3388 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3389 8 + SI_SGPR_RW_BUFFERS
);
3390 ret
= si_insert_input_ptr(ctx
, ret
,
3391 ctx
->param_bindless_samplers_and_images
,
3392 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3394 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_vs_state_bits
,
3395 8 + SI_SGPR_VS_STATE_BITS
);
3397 #if !HAVE_32BIT_POINTERS
3398 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 4,
3399 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3402 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3403 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3404 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_offsets
,
3405 8 + GFX9_SGPR_TCS_OUT_OFFSETS
);
3406 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3407 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3409 unsigned vgpr
= 8 + GFX9_TCS_NUM_USER_SGPR
;
3410 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3411 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_patch_id
),
3413 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3414 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
),
3416 ctx
->return_value
= ret
;
3419 /* Pass GS inputs from ES to GS on GFX9. */
3420 static void si_set_es_return_value_for_gs(struct si_shader_context
*ctx
)
3422 LLVMValueRef ret
= ctx
->return_value
;
3424 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3425 if (HAVE_32BIT_POINTERS
)
3426 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3427 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_gs2vs_offset
, 2);
3428 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3429 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3431 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3432 8 + SI_SGPR_RW_BUFFERS
);
3433 ret
= si_insert_input_ptr(ctx
, ret
,
3434 ctx
->param_bindless_samplers_and_images
,
3435 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3437 #if !HAVE_32BIT_POINTERS
3438 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 4,
3439 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3443 if (ctx
->type
== PIPE_SHADER_VERTEX
)
3444 vgpr
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
3446 vgpr
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
3448 for (unsigned i
= 0; i
< 5; i
++) {
3449 unsigned param
= ctx
->param_gs_vtx01_offset
+ i
;
3450 ret
= si_insert_input_ret_float(ctx
, ret
, param
, vgpr
++);
3452 ctx
->return_value
= ret
;
3455 static void si_llvm_emit_ls_epilogue(struct ac_shader_abi
*abi
,
3456 unsigned max_outputs
,
3457 LLVMValueRef
*addrs
)
3459 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3460 struct si_shader
*shader
= ctx
->shader
;
3461 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3463 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
3464 ctx
->param_rel_auto_id
);
3465 LLVMValueRef vertex_dw_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3466 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->ac
.builder
, vertex_id
,
3467 vertex_dw_stride
, "");
3469 /* Write outputs to LDS. The next shader (TCS aka HS) will read
3470 * its inputs from it. */
3471 for (i
= 0; i
< info
->num_outputs
; i
++) {
3472 unsigned name
= info
->output_semantic_name
[i
];
3473 unsigned index
= info
->output_semantic_index
[i
];
3475 /* The ARB_shader_viewport_layer_array spec contains the
3478 * 2) What happens if gl_ViewportIndex or gl_Layer is
3479 * written in the vertex shader and a geometry shader is
3482 * RESOLVED: The value written by the last vertex processing
3483 * stage is used. If the last vertex processing stage
3484 * (vertex, tessellation evaluation or geometry) does not
3485 * statically assign to gl_ViewportIndex or gl_Layer, index
3486 * or layer zero is assumed.
3488 * So writes to those outputs in VS-as-LS are simply ignored.
3490 if (name
== TGSI_SEMANTIC_LAYER
||
3491 name
== TGSI_SEMANTIC_VIEWPORT_INDEX
)
3494 int param
= si_shader_io_get_unique_index(name
, index
, false);
3495 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_dw_addr
,
3496 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
3498 for (chan
= 0; chan
< 4; chan
++) {
3499 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3502 lds_store(ctx
, chan
, dw_addr
,
3503 LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], ""));
3507 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3508 si_set_ls_return_value_for_tcs(ctx
);
3511 static void si_llvm_emit_es_epilogue(struct ac_shader_abi
*abi
,
3512 unsigned max_outputs
,
3513 LLVMValueRef
*addrs
)
3515 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3516 struct si_shader
*es
= ctx
->shader
;
3517 struct tgsi_shader_info
*info
= &es
->selector
->info
;
3518 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
3519 ctx
->param_es2gs_offset
);
3520 LLVMValueRef lds_base
= NULL
;
3524 if (ctx
->screen
->info
.chip_class
>= GFX9
&& info
->num_outputs
) {
3525 unsigned itemsize_dw
= es
->selector
->esgs_itemsize
/ 4;
3526 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
3527 LLVMValueRef wave_idx
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 24, 4);
3528 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
3529 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
3530 LLVMConstInt(ctx
->i32
, 64, false), ""), "");
3531 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
3532 LLVMConstInt(ctx
->i32
, itemsize_dw
, 0), "");
3535 for (i
= 0; i
< info
->num_outputs
; i
++) {
3538 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
3539 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
3542 param
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
3543 info
->output_semantic_index
[i
], false);
3545 for (chan
= 0; chan
< 4; chan
++) {
3546 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3549 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
3550 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
3552 /* GFX9 has the ESGS ring in LDS. */
3553 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3554 lds_store(ctx
, param
* 4 + chan
, lds_base
, out_val
);
3558 ac_build_buffer_store_dword(&ctx
->ac
,
3560 out_val
, 1, NULL
, soffset
,
3561 (4 * param
+ chan
) * 4,
3566 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3567 si_set_es_return_value_for_gs(ctx
);
3570 static LLVMValueRef
si_get_gs_wave_id(struct si_shader_context
*ctx
)
3572 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3573 return si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 16, 8);
3575 return LLVMGetParam(ctx
->main_fn
, ctx
->param_gs_wave_id
);
3578 static void emit_gs_epilogue(struct si_shader_context
*ctx
)
3580 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
,
3581 si_get_gs_wave_id(ctx
));
3583 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3584 lp_build_endif(&ctx
->merged_wrap_if_state
);
3587 static void si_llvm_emit_gs_epilogue(struct ac_shader_abi
*abi
,
3588 unsigned max_outputs
,
3589 LLVMValueRef
*addrs
)
3591 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3592 struct tgsi_shader_info UNUSED
*info
= &ctx
->shader
->selector
->info
;
3594 assert(info
->num_outputs
<= max_outputs
);
3596 emit_gs_epilogue(ctx
);
3599 static void si_tgsi_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
3601 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3602 emit_gs_epilogue(ctx
);
3605 static void si_llvm_emit_vs_epilogue(struct ac_shader_abi
*abi
,
3606 unsigned max_outputs
,
3607 LLVMValueRef
*addrs
)
3609 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3610 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
3611 struct si_shader_output_values
*outputs
= NULL
;
3614 assert(!ctx
->shader
->is_gs_copy_shader
);
3615 assert(info
->num_outputs
<= max_outputs
);
3617 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
3619 /* Vertex color clamping.
3621 * This uses a state constant loaded in a user data SGPR and
3622 * an IF statement is added that clamps all colors if the constant
3625 struct lp_build_if_state if_ctx
;
3626 LLVMValueRef cond
= NULL
;
3627 LLVMValueRef addr
, val
;
3629 for (i
= 0; i
< info
->num_outputs
; i
++) {
3630 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
3631 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
3634 /* We've found a color. */
3636 /* The state is in the first bit of the user SGPR. */
3637 cond
= LLVMGetParam(ctx
->main_fn
,
3638 ctx
->param_vs_state_bits
);
3639 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
,
3641 lp_build_if(&if_ctx
, &ctx
->gallivm
, cond
);
3644 for (j
= 0; j
< 4; j
++) {
3645 addr
= addrs
[4 * i
+ j
];
3646 val
= LLVMBuildLoad(ctx
->ac
.builder
, addr
, "");
3647 val
= ac_build_clamp(&ctx
->ac
, val
);
3648 LLVMBuildStore(ctx
->ac
.builder
, val
, addr
);
3653 lp_build_endif(&if_ctx
);
3655 for (i
= 0; i
< info
->num_outputs
; i
++) {
3656 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
3657 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
3659 for (j
= 0; j
< 4; j
++) {
3660 outputs
[i
].values
[j
] =
3661 LLVMBuildLoad(ctx
->ac
.builder
,
3664 outputs
[i
].vertex_stream
[j
] =
3665 (info
->output_streams
[i
] >> (2 * j
)) & 3;
3669 if (ctx
->shader
->selector
->so
.num_outputs
)
3670 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
3672 /* Export PrimitiveID. */
3673 if (ctx
->shader
->key
.mono
.u
.vs_export_prim_id
) {
3674 outputs
[i
].semantic_name
= TGSI_SEMANTIC_PRIMID
;
3675 outputs
[i
].semantic_index
= 0;
3676 outputs
[i
].values
[0] = ac_to_float(&ctx
->ac
, get_primitive_id(ctx
, 0));
3677 for (j
= 1; j
< 4; j
++)
3678 outputs
[i
].values
[j
] = LLVMConstReal(ctx
->f32
, 0);
3680 memset(outputs
[i
].vertex_stream
, 0,
3681 sizeof(outputs
[i
].vertex_stream
));
3685 si_llvm_export_vs(ctx
, outputs
, i
);
3689 static void si_tgsi_emit_epilogue(struct lp_build_tgsi_context
*bld_base
)
3691 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3693 ctx
->abi
.emit_outputs(&ctx
->abi
, RADEON_LLVM_MAX_OUTPUTS
,
3694 &ctx
->outputs
[0][0]);
3697 struct si_ps_exports
{
3699 struct ac_export_args args
[10];
3702 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
3703 LLVMValueRef depth
, LLVMValueRef stencil
,
3704 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
3706 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3707 struct ac_export_args args
;
3709 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
3711 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3714 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
3715 LLVMValueRef
*color
, unsigned index
,
3716 unsigned samplemask_param
,
3717 bool is_last
, struct si_ps_exports
*exp
)
3719 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3723 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
3724 for (i
= 0; i
< 4; i
++)
3725 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
3728 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
3729 color
[3] = ctx
->ac
.f32_1
;
3733 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
3734 si_alpha_test(bld_base
, color
[3]);
3736 /* Line & polygon smoothing */
3737 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
3738 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
3741 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3742 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
3743 struct ac_export_args args
[8];
3746 /* Get the export arguments, also find out what the last one is. */
3747 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3748 si_llvm_init_export_args(ctx
, color
,
3749 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
3750 if (args
[c
].enabled_channels
)
3754 /* Emit all exports. */
3755 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3756 if (is_last
&& last
== c
) {
3757 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
3758 args
[c
].done
= 1; /* DONE bit */
3759 } else if (!args
[c
].enabled_channels
)
3760 continue; /* unnecessary NULL export */
3762 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
3765 struct ac_export_args args
;
3768 si_llvm_init_export_args(ctx
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3771 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3772 args
.done
= 1; /* DONE bit */
3773 } else if (!args
.enabled_channels
)
3774 return; /* unnecessary NULL export */
3776 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3780 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3781 struct si_ps_exports
*exp
)
3783 for (unsigned i
= 0; i
< exp
->num
; i
++)
3784 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
3788 * Return PS outputs in this order:
3790 * v[0:3] = color0.xyzw
3791 * v[4:7] = color1.xyzw
3796 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3798 * The alpha-ref SGPR is returned via its original location.
3800 static void si_llvm_return_fs_outputs(struct ac_shader_abi
*abi
,
3801 unsigned max_outputs
,
3802 LLVMValueRef
*addrs
)
3804 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3805 struct si_shader
*shader
= ctx
->shader
;
3806 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3807 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3808 unsigned i
, j
, first_vgpr
, vgpr
;
3810 LLVMValueRef color
[8][4] = {};
3811 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3814 if (ctx
->postponed_kill
)
3815 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildLoad(builder
, ctx
->postponed_kill
, ""));
3817 /* Read the output values. */
3818 for (i
= 0; i
< info
->num_outputs
; i
++) {
3819 unsigned semantic_name
= info
->output_semantic_name
[i
];
3820 unsigned semantic_index
= info
->output_semantic_index
[i
];
3822 switch (semantic_name
) {
3823 case TGSI_SEMANTIC_COLOR
:
3824 assert(semantic_index
< 8);
3825 for (j
= 0; j
< 4; j
++) {
3826 LLVMValueRef ptr
= addrs
[4 * i
+ j
];
3827 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3828 color
[semantic_index
][j
] = result
;
3831 case TGSI_SEMANTIC_POSITION
:
3832 depth
= LLVMBuildLoad(builder
,
3833 addrs
[4 * i
+ 2], "");
3835 case TGSI_SEMANTIC_STENCIL
:
3836 stencil
= LLVMBuildLoad(builder
,
3837 addrs
[4 * i
+ 1], "");
3839 case TGSI_SEMANTIC_SAMPLEMASK
:
3840 samplemask
= LLVMBuildLoad(builder
,
3841 addrs
[4 * i
+ 0], "");
3844 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3849 /* Fill the return structure. */
3850 ret
= ctx
->return_value
;
3853 ret
= LLVMBuildInsertValue(builder
, ret
,
3854 ac_to_integer(&ctx
->ac
,
3855 LLVMGetParam(ctx
->main_fn
,
3856 SI_PARAM_ALPHA_REF
)),
3857 SI_SGPR_ALPHA_REF
, "");
3860 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3861 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3865 for (j
= 0; j
< 4; j
++)
3866 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3869 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3871 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3873 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3875 /* Add the input sample mask for smoothing at the end. */
3876 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3877 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3878 ret
= LLVMBuildInsertValue(builder
, ret
,
3879 LLVMGetParam(ctx
->main_fn
,
3880 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3882 ctx
->return_value
= ret
;
3885 static void membar_emit(
3886 const struct lp_build_tgsi_action
*action
,
3887 struct lp_build_tgsi_context
*bld_base
,
3888 struct lp_build_emit_data
*emit_data
)
3890 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3891 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3892 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3893 unsigned waitcnt
= NOOP_WAITCNT
;
3895 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3896 waitcnt
&= VM_CNT
& LGKM_CNT
;
3898 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3899 TGSI_MEMBAR_SHADER_BUFFER
|
3900 TGSI_MEMBAR_SHADER_IMAGE
))
3903 if (flags
& TGSI_MEMBAR_SHARED
)
3904 waitcnt
&= LGKM_CNT
;
3906 if (waitcnt
!= NOOP_WAITCNT
)
3907 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3910 static void clock_emit(
3911 const struct lp_build_tgsi_action
*action
,
3912 struct lp_build_tgsi_context
*bld_base
,
3913 struct lp_build_emit_data
*emit_data
)
3915 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3916 LLVMValueRef tmp
= ac_build_shader_clock(&ctx
->ac
);
3918 emit_data
->output
[0] =
3919 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_0
, "");
3920 emit_data
->output
[1] =
3921 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_1
, "");
3924 static void si_llvm_emit_ddxy(
3925 const struct lp_build_tgsi_action
*action
,
3926 struct lp_build_tgsi_context
*bld_base
,
3927 struct lp_build_emit_data
*emit_data
)
3929 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3930 unsigned opcode
= emit_data
->info
->opcode
;
3935 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3936 mask
= AC_TID_MASK_LEFT
;
3937 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3938 mask
= AC_TID_MASK_TOP
;
3940 mask
= AC_TID_MASK_TOP_LEFT
;
3942 /* for DDX we want to next X pixel, DDY next Y pixel. */
3943 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3945 val
= ac_to_integer(&ctx
->ac
, emit_data
->args
[0]);
3946 val
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, val
);
3947 emit_data
->output
[emit_data
->chan
] = val
;
3951 * this takes an I,J coordinate pair,
3952 * and works out the X and Y derivatives.
3953 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3955 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3956 struct lp_build_tgsi_context
*bld_base
,
3957 LLVMValueRef interp_ij
)
3959 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3960 LLVMValueRef result
[4], a
;
3963 for (i
= 0; i
< 2; i
++) {
3964 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
3965 LLVMConstInt(ctx
->i32
, i
, 0), "");
3966 result
[i
] = ac_build_ddxy(&ctx
->ac
, AC_TID_MASK_TOP_LEFT
, 1,
3967 ac_to_integer(&ctx
->ac
, a
)); /* DDX */
3968 result
[2+i
] = ac_build_ddxy(&ctx
->ac
, AC_TID_MASK_TOP_LEFT
, 2,
3969 ac_to_integer(&ctx
->ac
, a
)); /* DDY */
3972 return ac_build_gather_values(&ctx
->ac
, result
, 4);
3975 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3976 struct lp_build_tgsi_context
*bld_base
,
3977 struct lp_build_emit_data
*emit_data
)
3979 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3980 struct si_shader
*shader
= ctx
->shader
;
3981 const struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3982 LLVMValueRef interp_param
;
3983 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3984 const struct tgsi_full_src_register
*input
= &inst
->Src
[0];
3985 int input_base
, input_array_size
;
3988 LLVMValueRef prim_mask
= ctx
->abi
.prim_mask
;
3989 LLVMValueRef array_idx
, offset_x
= NULL
, offset_y
= NULL
;
3990 int interp_param_idx
;
3994 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3995 /* offset is in second src, first two channels */
3996 offset_x
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 1,
3998 offset_y
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 1,
4000 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4001 LLVMValueRef sample_position
;
4002 LLVMValueRef sample_id
;
4003 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
4005 /* fetch sample ID, then fetch its sample position,
4006 * and place into first two channels.
4008 sample_id
= lp_build_emit_fetch(bld_base
,
4009 emit_data
->inst
, 1, TGSI_CHAN_X
);
4010 sample_id
= ac_to_integer(&ctx
->ac
, sample_id
);
4012 /* Section 8.13.2 (Interpolation Functions) of the OpenGL Shading
4013 * Language 4.50 spec says about interpolateAtSample:
4015 * "Returns the value of the input interpolant variable at
4016 * the location of sample number sample. If multisample
4017 * buffers are not available, the input variable will be
4018 * evaluated at the center of the pixel. If sample sample
4019 * does not exist, the position used to interpolate the
4020 * input variable is undefined."
4022 * This means that sample_id values outside of the valid are
4023 * in fact valid input, and the usual mechanism for loading the
4024 * sample position doesn't work.
4026 if (ctx
->shader
->key
.mono
.u
.ps
.interpolate_at_sample_force_center
) {
4027 LLVMValueRef center
[4] = {
4028 LLVMConstReal(ctx
->f32
, 0.5),
4029 LLVMConstReal(ctx
->f32
, 0.5),
4034 sample_position
= ac_build_gather_values(&ctx
->ac
, center
, 4);
4036 sample_position
= load_sample_position(&ctx
->abi
, sample_id
);
4039 offset_x
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
,
4042 offset_x
= LLVMBuildFSub(ctx
->ac
.builder
, offset_x
, halfval
, "");
4043 offset_y
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
,
4045 offset_y
= LLVMBuildFSub(ctx
->ac
.builder
, offset_y
, halfval
, "");
4048 assert(input
->Register
.File
== TGSI_FILE_INPUT
);
4050 if (input
->Register
.Indirect
) {
4051 unsigned array_id
= input
->Indirect
.ArrayID
;
4054 input_base
= info
->input_array_first
[array_id
];
4055 input_array_size
= info
->input_array_last
[array_id
] - input_base
+ 1;
4057 input_base
= inst
->Src
[0].Register
.Index
;
4058 input_array_size
= info
->num_inputs
- input_base
;
4061 array_idx
= si_get_indirect_index(ctx
, &input
->Indirect
,
4062 1, input
->Register
.Index
- input_base
);
4064 input_base
= inst
->Src
[0].Register
.Index
;
4065 input_array_size
= 1;
4066 array_idx
= ctx
->i32_0
;
4069 interp
= shader
->selector
->info
.input_interpolate
[input_base
];
4071 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4072 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
4073 location
= TGSI_INTERPOLATE_LOC_CENTER
;
4075 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
4077 interp_param_idx
= lookup_interp_param_index(interp
, location
);
4078 if (interp_param_idx
== -1)
4080 else if (interp_param_idx
)
4081 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
4083 interp_param
= NULL
;
4085 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4086 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4087 LLVMValueRef ij_out
[2];
4088 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
4091 * take the I then J parameters, and the DDX/Y for it, and
4092 * calculate the IJ inputs for the interpolator.
4093 * temp1 = ddx * offset/sample.x + I;
4094 * interp_param.I = ddy * offset/sample.y + temp1;
4095 * temp1 = ddx * offset/sample.x + J;
4096 * interp_param.J = ddy * offset/sample.y + temp1;
4098 for (i
= 0; i
< 2; i
++) {
4099 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, 0);
4100 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, 0);
4101 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4102 ddxy_out
, ix_ll
, "");
4103 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4104 ddxy_out
, iy_ll
, "");
4105 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4106 interp_param
, ix_ll
, "");
4109 interp_el
= ac_to_float(&ctx
->ac
, interp_el
);
4111 temp
= ac_build_fmad(&ctx
->ac
, ddx_el
, offset_x
, interp_el
);
4112 ij_out
[i
] = ac_build_fmad(&ctx
->ac
, ddy_el
, offset_y
, temp
);
4114 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4118 interp_param
= ac_to_float(&ctx
->ac
, interp_param
);
4120 for (chan
= 0; chan
< 4; chan
++) {
4121 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->f32
, input_array_size
));
4122 unsigned schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
4124 for (unsigned idx
= 0; idx
< input_array_size
; ++idx
) {
4125 LLVMValueRef v
, i
= NULL
, j
= NULL
;
4128 i
= LLVMBuildExtractElement(
4129 ctx
->ac
.builder
, interp_param
, ctx
->i32_0
, "");
4130 j
= LLVMBuildExtractElement(
4131 ctx
->ac
.builder
, interp_param
, ctx
->i32_1
, "");
4133 v
= si_build_fs_interp(ctx
, input_base
+ idx
, schan
,
4136 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
,
4137 gather
, v
, LLVMConstInt(ctx
->i32
, idx
, false), "");
4140 emit_data
->output
[chan
] = LLVMBuildExtractElement(
4141 ctx
->ac
.builder
, gather
, array_idx
, "");
4145 static void vote_all_emit(
4146 const struct lp_build_tgsi_action
*action
,
4147 struct lp_build_tgsi_context
*bld_base
,
4148 struct lp_build_emit_data
*emit_data
)
4150 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4152 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, emit_data
->args
[0]);
4153 emit_data
->output
[emit_data
->chan
] =
4154 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4157 static void vote_any_emit(
4158 const struct lp_build_tgsi_action
*action
,
4159 struct lp_build_tgsi_context
*bld_base
,
4160 struct lp_build_emit_data
*emit_data
)
4162 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4164 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, emit_data
->args
[0]);
4165 emit_data
->output
[emit_data
->chan
] =
4166 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4169 static void vote_eq_emit(
4170 const struct lp_build_tgsi_action
*action
,
4171 struct lp_build_tgsi_context
*bld_base
,
4172 struct lp_build_emit_data
*emit_data
)
4174 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4176 LLVMValueRef tmp
= ac_build_vote_eq(&ctx
->ac
, emit_data
->args
[0]);
4177 emit_data
->output
[emit_data
->chan
] =
4178 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4181 static void ballot_emit(
4182 const struct lp_build_tgsi_action
*action
,
4183 struct lp_build_tgsi_context
*bld_base
,
4184 struct lp_build_emit_data
*emit_data
)
4186 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4187 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4190 tmp
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4191 tmp
= ac_build_ballot(&ctx
->ac
, tmp
);
4192 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->v2i32
, "");
4194 emit_data
->output
[0] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_0
, "");
4195 emit_data
->output
[1] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_1
, "");
4198 static void read_lane_emit(
4199 const struct lp_build_tgsi_action
*action
,
4200 struct lp_build_tgsi_context
*bld_base
,
4201 struct lp_build_emit_data
*emit_data
)
4203 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4205 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_READ_INVOC
) {
4206 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4207 0, emit_data
->src_chan
);
4209 /* Always read the source invocation (= lane) from the X channel. */
4210 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4212 emit_data
->arg_count
= 2;
4215 /* We currently have no other way to prevent LLVM from lifting the icmp
4216 * calls to a dominating basic block.
4218 ac_build_optimization_barrier(&ctx
->ac
, &emit_data
->args
[0]);
4220 for (unsigned i
= 0; i
< emit_data
->arg_count
; ++i
)
4221 emit_data
->args
[i
] = ac_to_integer(&ctx
->ac
, emit_data
->args
[i
]);
4223 emit_data
->output
[emit_data
->chan
] =
4224 ac_build_intrinsic(&ctx
->ac
, action
->intr_name
,
4225 ctx
->i32
, emit_data
->args
, emit_data
->arg_count
,
4226 AC_FUNC_ATTR_READNONE
|
4227 AC_FUNC_ATTR_CONVERGENT
);
4230 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
4231 struct lp_build_emit_data
*emit_data
)
4233 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4234 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
4238 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
4240 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
4241 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
4245 /* Emit one vertex from the geometry shader */
4246 static void si_llvm_emit_vertex(struct ac_shader_abi
*abi
,
4248 LLVMValueRef
*addrs
)
4250 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4251 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4252 struct si_shader
*shader
= ctx
->shader
;
4253 struct lp_build_if_state if_state
;
4254 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
4255 ctx
->param_gs2vs_offset
);
4256 LLVMValueRef gs_next_vertex
;
4257 LLVMValueRef can_emit
;
4258 unsigned chan
, offset
;
4261 /* Write vertex attribute values to GSVS ring */
4262 gs_next_vertex
= LLVMBuildLoad(ctx
->ac
.builder
,
4263 ctx
->gs_next_vertex
[stream
],
4266 /* If this thread has already emitted the declared maximum number of
4267 * vertices, skip the write: excessive vertex emissions are not
4268 * supposed to have any effect.
4270 * If the shader has no writes to memory, kill it instead. This skips
4271 * further memory loads and may allow LLVM to skip to the end
4274 can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, gs_next_vertex
,
4275 LLVMConstInt(ctx
->i32
,
4276 shader
->selector
->gs_max_out_vertices
, 0), "");
4278 bool use_kill
= !info
->writes_memory
;
4280 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4282 lp_build_if(&if_state
, &ctx
->gallivm
, can_emit
);
4286 for (i
= 0; i
< info
->num_outputs
; i
++) {
4287 for (chan
= 0; chan
< 4; chan
++) {
4288 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
4289 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
4292 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
4293 LLVMValueRef voffset
=
4294 LLVMConstInt(ctx
->i32
, offset
*
4295 shader
->selector
->gs_max_out_vertices
, 0);
4298 voffset
= LLVMBuildAdd(ctx
->ac
.builder
, voffset
, gs_next_vertex
, "");
4299 voffset
= LLVMBuildMul(ctx
->ac
.builder
, voffset
,
4300 LLVMConstInt(ctx
->i32
, 4, 0), "");
4302 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
4304 ac_build_buffer_store_dword(&ctx
->ac
,
4305 ctx
->gsvs_ring
[stream
],
4307 voffset
, soffset
, 0,
4312 gs_next_vertex
= LLVMBuildAdd(ctx
->ac
.builder
, gs_next_vertex
, ctx
->i32_1
, "");
4313 LLVMBuildStore(ctx
->ac
.builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
4315 /* Signal vertex emission if vertex data was written. */
4317 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
4318 si_get_gs_wave_id(ctx
));
4322 lp_build_endif(&if_state
);
4325 /* Emit one vertex from the geometry shader */
4326 static void si_tgsi_emit_vertex(
4327 const struct lp_build_tgsi_action
*action
,
4328 struct lp_build_tgsi_context
*bld_base
,
4329 struct lp_build_emit_data
*emit_data
)
4331 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4332 unsigned stream
= si_llvm_get_stream(bld_base
, emit_data
);
4334 si_llvm_emit_vertex(&ctx
->abi
, stream
, ctx
->outputs
[0]);
4337 /* Cut one primitive from the geometry shader */
4338 static void si_llvm_emit_primitive(struct ac_shader_abi
*abi
,
4341 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4343 /* Signal primitive cut */
4344 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
4345 si_get_gs_wave_id(ctx
));
4348 /* Cut one primitive from the geometry shader */
4349 static void si_tgsi_emit_primitive(
4350 const struct lp_build_tgsi_action
*action
,
4351 struct lp_build_tgsi_context
*bld_base
,
4352 struct lp_build_emit_data
*emit_data
)
4354 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4356 si_llvm_emit_primitive(&ctx
->abi
, si_llvm_get_stream(bld_base
, emit_data
));
4359 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4360 struct lp_build_tgsi_context
*bld_base
,
4361 struct lp_build_emit_data
*emit_data
)
4363 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4365 /* SI only (thanks to a hw bug workaround):
4366 * The real barrier instruction isn’t needed, because an entire patch
4367 * always fits into a single wave.
4369 if (ctx
->screen
->info
.chip_class
== SI
&&
4370 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
4371 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
4375 ac_build_s_barrier(&ctx
->ac
);
4378 static void si_create_function(struct si_shader_context
*ctx
,
4380 LLVMTypeRef
*returns
, unsigned num_returns
,
4381 struct si_function_info
*fninfo
,
4382 unsigned max_workgroup_size
)
4386 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
4387 fninfo
->types
, fninfo
->num_params
);
4388 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
4390 for (i
= 0; i
< fninfo
->num_sgpr_params
; ++i
) {
4391 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
4393 /* The combination of:
4397 * allows the optimization passes to move loads and reduces
4398 * SGPR spilling significantly.
4400 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4401 AC_FUNC_ATTR_INREG
);
4403 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
4404 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4405 AC_FUNC_ATTR_NOALIAS
);
4406 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
4410 for (i
= 0; i
< fninfo
->num_params
; ++i
) {
4411 if (fninfo
->assign
[i
])
4412 *fninfo
->assign
[i
] = LLVMGetParam(ctx
->main_fn
, i
);
4415 if (ctx
->screen
->info
.address32_hi
) {
4416 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4417 "amdgpu-32bit-address-high-bits",
4418 ctx
->screen
->info
.address32_hi
);
4421 if (max_workgroup_size
) {
4422 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4423 "amdgpu-max-work-group-size",
4424 max_workgroup_size
);
4426 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4427 "no-signed-zeros-fp-math",
4430 if (ctx
->screen
->debug_flags
& DBG(UNSAFE_MATH
)) {
4431 /* These were copied from some LLVM test. */
4432 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4433 "less-precise-fpmad",
4435 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4438 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4441 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4447 static void declare_streamout_params(struct si_shader_context
*ctx
,
4448 struct pipe_stream_output_info
*so
,
4449 struct si_function_info
*fninfo
)
4453 /* Streamout SGPRs. */
4454 if (so
->num_outputs
) {
4455 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
4456 ctx
->param_streamout_config
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4458 ctx
->param_streamout_config
= fninfo
->num_params
- 1;
4460 ctx
->param_streamout_write_index
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4462 /* A streamout buffer offset is loaded if the stride is non-zero. */
4463 for (i
= 0; i
< 4; i
++) {
4467 ctx
->param_streamout_offset
[i
] = add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4471 static unsigned si_get_max_workgroup_size(const struct si_shader
*shader
)
4473 switch (shader
->selector
->type
) {
4474 case PIPE_SHADER_TESS_CTRL
:
4475 /* Return this so that LLVM doesn't remove s_barrier
4476 * instructions on chips where we use s_barrier. */
4477 return shader
->selector
->screen
->info
.chip_class
>= CIK
? 128 : 64;
4479 case PIPE_SHADER_GEOMETRY
:
4480 return shader
->selector
->screen
->info
.chip_class
>= GFX9
? 128 : 64;
4482 case PIPE_SHADER_COMPUTE
:
4483 break; /* see below */
4489 const unsigned *properties
= shader
->selector
->info
.properties
;
4490 unsigned max_work_group_size
=
4491 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
4492 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
4493 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
4495 if (!max_work_group_size
) {
4496 /* This is a variable group size compute shader,
4497 * compile it for the maximum possible group size.
4499 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
4501 return max_work_group_size
;
4504 static void declare_const_and_shader_buffers(struct si_shader_context
*ctx
,
4505 struct si_function_info
*fninfo
,
4508 LLVMTypeRef const_shader_buf_type
;
4510 if (ctx
->shader
->selector
->info
.const_buffers_declared
== 1 &&
4511 ctx
->shader
->selector
->info
.shader_buffers_declared
== 0)
4512 const_shader_buf_type
= ctx
->f32
;
4514 const_shader_buf_type
= ctx
->v4i32
;
4516 unsigned const_and_shader_buffers
=
4517 add_arg(fninfo
, ARG_SGPR
,
4518 ac_array_in_const32_addr_space(const_shader_buf_type
));
4521 ctx
->param_const_and_shader_buffers
= const_and_shader_buffers
;
4524 static void declare_samplers_and_images(struct si_shader_context
*ctx
,
4525 struct si_function_info
*fninfo
,
4528 unsigned samplers_and_images
=
4529 add_arg(fninfo
, ARG_SGPR
,
4530 ac_array_in_const32_addr_space(ctx
->v8i32
));
4533 ctx
->param_samplers_and_images
= samplers_and_images
;
4536 static void declare_per_stage_desc_pointers(struct si_shader_context
*ctx
,
4537 struct si_function_info
*fninfo
,
4540 declare_const_and_shader_buffers(ctx
, fninfo
, assign_params
);
4541 declare_samplers_and_images(ctx
, fninfo
, assign_params
);
4544 static void declare_global_desc_pointers(struct si_shader_context
*ctx
,
4545 struct si_function_info
*fninfo
)
4547 ctx
->param_rw_buffers
= add_arg(fninfo
, ARG_SGPR
,
4548 ac_array_in_const32_addr_space(ctx
->v4i32
));
4549 ctx
->param_bindless_samplers_and_images
= add_arg(fninfo
, ARG_SGPR
,
4550 ac_array_in_const32_addr_space(ctx
->v8i32
));
4553 static void declare_vs_specific_input_sgprs(struct si_shader_context
*ctx
,
4554 struct si_function_info
*fninfo
)
4556 ctx
->param_vs_state_bits
= add_arg(fninfo
, ARG_SGPR
, ctx
->i32
);
4557 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.base_vertex
);
4558 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.start_instance
);
4559 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.draw_id
);
4562 static void declare_vs_input_vgprs(struct si_shader_context
*ctx
,
4563 struct si_function_info
*fninfo
,
4564 unsigned *num_prolog_vgprs
)
4566 struct si_shader
*shader
= ctx
->shader
;
4568 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.vertex_id
);
4569 if (shader
->key
.as_ls
) {
4570 ctx
->param_rel_auto_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4571 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4573 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4574 ctx
->param_vs_prim_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4576 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
); /* unused */
4578 if (!shader
->is_gs_copy_shader
) {
4579 /* Vertex load indices. */
4580 ctx
->param_vertex_index0
= fninfo
->num_params
;
4581 for (unsigned i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4582 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4583 *num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
4587 static void declare_tes_input_vgprs(struct si_shader_context
*ctx
,
4588 struct si_function_info
*fninfo
)
4590 ctx
->param_tes_u
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4591 ctx
->param_tes_v
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4592 ctx
->param_tes_rel_patch_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4593 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tes_patch_id
);
4597 /* Convenient merged shader definitions. */
4598 SI_SHADER_MERGED_VERTEX_TESSCTRL
= PIPE_SHADER_TYPES
,
4599 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
,
4602 static void create_function(struct si_shader_context
*ctx
)
4604 struct si_shader
*shader
= ctx
->shader
;
4605 struct si_function_info fninfo
;
4606 LLVMTypeRef returns
[16+32*4];
4607 unsigned i
, num_return_sgprs
;
4608 unsigned num_returns
= 0;
4609 unsigned num_prolog_vgprs
= 0;
4610 unsigned type
= ctx
->type
;
4611 unsigned vs_blit_property
=
4612 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
4614 si_init_function_info(&fninfo
);
4616 /* Set MERGED shaders. */
4617 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
4618 if (shader
->key
.as_ls
|| type
== PIPE_SHADER_TESS_CTRL
)
4619 type
= SI_SHADER_MERGED_VERTEX_TESSCTRL
; /* LS or HS */
4620 else if (shader
->key
.as_es
|| type
== PIPE_SHADER_GEOMETRY
)
4621 type
= SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
;
4624 LLVMTypeRef v3i32
= LLVMVectorType(ctx
->i32
, 3);
4627 case PIPE_SHADER_VERTEX
:
4628 declare_global_desc_pointers(ctx
, &fninfo
);
4630 if (vs_blit_property
) {
4631 ctx
->param_vs_blit_inputs
= fninfo
.num_params
;
4632 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x1, y1 */
4633 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x2, y2 */
4634 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* depth */
4636 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
4637 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color0 */
4638 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color1 */
4639 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color2 */
4640 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color3 */
4641 } else if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
) {
4642 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x1 */
4643 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y1 */
4644 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x2 */
4645 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y2 */
4646 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.z */
4647 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.w */
4651 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4655 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4656 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4657 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4658 ac_array_in_const32_addr_space(ctx
->v4i32
));
4660 if (shader
->key
.as_es
) {
4661 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4662 } else if (shader
->key
.as_ls
) {
4663 /* no extra parameters */
4665 if (shader
->is_gs_copy_shader
) {
4666 fninfo
.num_params
= ctx
->param_vs_state_bits
+ 1;
4667 fninfo
.num_sgpr_params
= fninfo
.num_params
;
4670 /* The locations of the other parameters are assigned dynamically. */
4671 declare_streamout_params(ctx
, &shader
->selector
->so
,
4676 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4679 case PIPE_SHADER_TESS_CTRL
: /* SI-CI-VI */
4680 declare_global_desc_pointers(ctx
, &fninfo
);
4681 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4682 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4683 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4684 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4685 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4686 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4687 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4690 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4691 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4693 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4694 * placed after the user SGPRs.
4696 for (i
= 0; i
< GFX6_TCS_NUM_USER_SGPR
+ 2; i
++)
4697 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4698 for (i
= 0; i
< 11; i
++)
4699 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4702 case SI_SHADER_MERGED_VERTEX_TESSCTRL
:
4703 /* Merged stages have 8 system SGPRs at the beginning. */
4704 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
4705 if (HAVE_32BIT_POINTERS
) {
4706 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4707 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4709 declare_const_and_shader_buffers(ctx
, &fninfo
,
4710 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4712 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4713 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4714 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4715 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4716 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4717 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4719 declare_global_desc_pointers(ctx
, &fninfo
);
4720 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4721 ctx
->type
== PIPE_SHADER_VERTEX
);
4722 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4724 if (!HAVE_32BIT_POINTERS
) {
4725 declare_samplers_and_images(ctx
, &fninfo
,
4726 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4728 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4729 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4730 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4731 if (!HAVE_32BIT_POINTERS
) /* Align to 2 dwords. */
4732 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4733 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4734 ac_array_in_const32_addr_space(ctx
->v4i32
));
4736 /* VGPRs (first TCS, then VS) */
4737 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4738 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4740 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4741 declare_vs_input_vgprs(ctx
, &fninfo
,
4744 /* LS return values are inputs to the TCS main shader part. */
4745 for (i
= 0; i
< 8 + GFX9_TCS_NUM_USER_SGPR
; i
++)
4746 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4747 for (i
= 0; i
< 2; i
++)
4748 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4750 /* TCS return values are inputs to the TCS epilog.
4752 * param_tcs_offchip_offset, param_tcs_factor_offset,
4753 * param_tcs_offchip_layout, and param_rw_buffers
4754 * should be passed to the epilog.
4756 for (i
= 0; i
<= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
; i
++)
4757 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4758 for (i
= 0; i
< 11; i
++)
4759 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4763 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
:
4764 /* Merged stages have 8 system SGPRs at the beginning. */
4765 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
4766 if (HAVE_32BIT_POINTERS
) {
4767 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4768 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4770 declare_const_and_shader_buffers(ctx
, &fninfo
,
4771 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4773 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4774 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4775 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4776 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4777 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4778 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4780 declare_global_desc_pointers(ctx
, &fninfo
);
4781 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4782 (ctx
->type
== PIPE_SHADER_VERTEX
||
4783 ctx
->type
== PIPE_SHADER_TESS_EVAL
));
4784 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4785 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4787 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4788 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4789 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4790 /* Declare as many input SGPRs as the VS has. */
4791 if (!HAVE_32BIT_POINTERS
)
4792 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4795 if (!HAVE_32BIT_POINTERS
) {
4796 declare_samplers_and_images(ctx
, &fninfo
,
4797 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4799 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4800 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4801 ac_array_in_const32_addr_space(ctx
->v4i32
));
4804 /* VGPRs (first GS, then VS/TES) */
4805 ctx
->param_gs_vtx01_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4806 ctx
->param_gs_vtx23_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4807 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4808 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4809 ctx
->param_gs_vtx45_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4811 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4812 declare_vs_input_vgprs(ctx
, &fninfo
,
4814 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4815 declare_tes_input_vgprs(ctx
, &fninfo
);
4818 if (ctx
->type
== PIPE_SHADER_VERTEX
||
4819 ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4820 unsigned num_user_sgprs
;
4822 if (ctx
->type
== PIPE_SHADER_VERTEX
)
4823 num_user_sgprs
= GFX9_VSGS_NUM_USER_SGPR
;
4825 num_user_sgprs
= GFX9_TESGS_NUM_USER_SGPR
;
4827 /* ES return values are inputs to GS. */
4828 for (i
= 0; i
< 8 + num_user_sgprs
; i
++)
4829 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4830 for (i
= 0; i
< 5; i
++)
4831 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4835 case PIPE_SHADER_TESS_EVAL
:
4836 declare_global_desc_pointers(ctx
, &fninfo
);
4837 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4838 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4839 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4840 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4842 if (shader
->key
.as_es
) {
4843 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4844 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4845 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4847 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4848 declare_streamout_params(ctx
, &shader
->selector
->so
,
4850 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4854 declare_tes_input_vgprs(ctx
, &fninfo
);
4857 case PIPE_SHADER_GEOMETRY
:
4858 declare_global_desc_pointers(ctx
, &fninfo
);
4859 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4860 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4861 ctx
->param_gs_wave_id
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4864 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]);
4865 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]);
4866 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4867 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
4868 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
4869 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
4870 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
4871 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4874 case PIPE_SHADER_FRAGMENT
:
4875 declare_global_desc_pointers(ctx
, &fninfo
);
4876 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4877 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
4878 add_arg_assign_checked(&fninfo
, ARG_SGPR
, ctx
->i32
,
4879 &ctx
->abi
.prim_mask
, SI_PARAM_PRIM_MASK
);
4881 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_SAMPLE
);
4882 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTER
);
4883 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTROID
);
4884 add_arg_checked(&fninfo
, ARG_VGPR
, v3i32
, SI_PARAM_PERSP_PULL_MODEL
);
4885 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_SAMPLE
);
4886 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTER
);
4887 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTROID
);
4888 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->f32
, SI_PARAM_LINE_STIPPLE_TEX
);
4889 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4890 &ctx
->abi
.frag_pos
[0], SI_PARAM_POS_X_FLOAT
);
4891 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4892 &ctx
->abi
.frag_pos
[1], SI_PARAM_POS_Y_FLOAT
);
4893 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4894 &ctx
->abi
.frag_pos
[2], SI_PARAM_POS_Z_FLOAT
);
4895 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4896 &ctx
->abi
.frag_pos
[3], SI_PARAM_POS_W_FLOAT
);
4897 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4898 &ctx
->abi
.front_face
, SI_PARAM_FRONT_FACE
);
4899 shader
->info
.face_vgpr_index
= 20;
4900 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4901 &ctx
->abi
.ancillary
, SI_PARAM_ANCILLARY
);
4902 shader
->info
.ancillary_vgpr_index
= 21;
4903 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4904 &ctx
->abi
.sample_coverage
, SI_PARAM_SAMPLE_COVERAGE
);
4905 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->i32
, SI_PARAM_POS_FIXED_PT
);
4907 /* Color inputs from the prolog. */
4908 if (shader
->selector
->info
.colors_read
) {
4909 unsigned num_color_elements
=
4910 util_bitcount(shader
->selector
->info
.colors_read
);
4912 assert(fninfo
.num_params
+ num_color_elements
<= ARRAY_SIZE(fninfo
.types
));
4913 for (i
= 0; i
< num_color_elements
; i
++)
4914 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
4916 num_prolog_vgprs
+= num_color_elements
;
4919 /* Outputs for the epilog. */
4920 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4923 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4924 shader
->selector
->info
.writes_z
+
4925 shader
->selector
->info
.writes_stencil
+
4926 shader
->selector
->info
.writes_samplemask
+
4927 1 /* SampleMaskIn */;
4929 num_returns
= MAX2(num_returns
,
4931 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4933 for (i
= 0; i
< num_return_sgprs
; i
++)
4934 returns
[i
] = ctx
->i32
;
4935 for (; i
< num_returns
; i
++)
4936 returns
[i
] = ctx
->f32
;
4939 case PIPE_SHADER_COMPUTE
:
4940 declare_global_desc_pointers(ctx
, &fninfo
);
4941 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4942 if (shader
->selector
->info
.uses_grid_size
)
4943 add_arg_assign(&fninfo
, ARG_SGPR
, v3i32
, &ctx
->abi
.num_work_groups
);
4944 if (shader
->selector
->info
.uses_block_size
&&
4945 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
4946 ctx
->param_block_size
= add_arg(&fninfo
, ARG_SGPR
, v3i32
);
4948 unsigned cs_user_data_dwords
=
4949 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_USER_DATA_DWORDS
];
4950 if (cs_user_data_dwords
) {
4951 ctx
->param_cs_user_data
= add_arg(&fninfo
, ARG_SGPR
,
4952 LLVMVectorType(ctx
->i32
, cs_user_data_dwords
));
4955 for (i
= 0; i
< 3; i
++) {
4956 ctx
->abi
.workgroup_ids
[i
] = NULL
;
4957 if (shader
->selector
->info
.uses_block_id
[i
])
4958 add_arg_assign(&fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.workgroup_ids
[i
]);
4961 add_arg_assign(&fninfo
, ARG_VGPR
, v3i32
, &ctx
->abi
.local_invocation_ids
);
4964 assert(0 && "unimplemented shader");
4968 si_create_function(ctx
, "main", returns
, num_returns
, &fninfo
,
4969 si_get_max_workgroup_size(shader
));
4971 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4972 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&& !ctx
->shader
->is_monolithic
) {
4973 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4974 "InitialPSInputAddr",
4975 S_0286D0_PERSP_SAMPLE_ENA(1) |
4976 S_0286D0_PERSP_CENTER_ENA(1) |
4977 S_0286D0_PERSP_CENTROID_ENA(1) |
4978 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4979 S_0286D0_LINEAR_CENTER_ENA(1) |
4980 S_0286D0_LINEAR_CENTROID_ENA(1) |
4981 S_0286D0_FRONT_FACE_ENA(1) |
4982 S_0286D0_ANCILLARY_ENA(1) |
4983 S_0286D0_POS_FIXED_PT_ENA(1));
4986 shader
->info
.num_input_sgprs
= 0;
4987 shader
->info
.num_input_vgprs
= 0;
4989 for (i
= 0; i
< fninfo
.num_sgpr_params
; ++i
)
4990 shader
->info
.num_input_sgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
4992 for (; i
< fninfo
.num_params
; ++i
)
4993 shader
->info
.num_input_vgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
4995 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
4996 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
4998 if (shader
->key
.as_ls
||
4999 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5000 /* GFX9 has the ESGS ring buffer in LDS. */
5001 type
== SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
)
5002 ac_declare_lds_as_pointer(&ctx
->ac
);
5006 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5009 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5011 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5013 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5014 ctx
->param_rw_buffers
);
5016 if (ctx
->screen
->info
.chip_class
<= VI
&&
5017 (ctx
->shader
->key
.as_es
|| ctx
->type
== PIPE_SHADER_GEOMETRY
)) {
5019 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5021 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, ring
, 0);
5024 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5027 if (ctx
->shader
->is_gs_copy_shader
) {
5028 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5031 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5032 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5033 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5034 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5035 LLVMValueRef base_ring
;
5037 base_ring
= ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5039 /* The conceptual layout of the GSVS ring is
5040 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5041 * but the real memory layout is swizzled across
5043 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5045 * Override the buffer descriptor accordingly.
5047 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5048 uint64_t stream_offset
= 0;
5050 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5051 unsigned num_components
;
5053 unsigned num_records
;
5054 LLVMValueRef ring
, tmp
;
5056 num_components
= sel
->info
.num_stream_output_components
[stream
];
5057 if (!num_components
)
5060 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5062 /* Limit on the stride field for <= CIK. */
5063 assert(stride
< (1 << 14));
5067 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5068 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_0
, "");
5069 tmp
= LLVMBuildAdd(builder
, tmp
,
5070 LLVMConstInt(ctx
->i64
,
5071 stream_offset
, 0), "");
5072 stream_offset
+= stride
* 64;
5074 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_0
, "");
5075 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5076 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_1
, "");
5077 tmp
= LLVMBuildOr(builder
, tmp
,
5078 LLVMConstInt(ctx
->i32
,
5079 S_008F04_STRIDE(stride
) |
5080 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5081 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_1
, "");
5082 ring
= LLVMBuildInsertElement(builder
, ring
,
5083 LLVMConstInt(ctx
->i32
, num_records
, 0),
5084 LLVMConstInt(ctx
->i32
, 2, 0), "");
5085 ring
= LLVMBuildInsertElement(builder
, ring
,
5086 LLVMConstInt(ctx
->i32
,
5087 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5088 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5089 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5090 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5091 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5092 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5093 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5094 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5095 S_008F0C_ADD_TID_ENABLE(1),
5097 LLVMConstInt(ctx
->i32
, 3, 0), "");
5099 ctx
->gsvs_ring
[stream
] = ring
;
5101 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
5102 ctx
->tess_offchip_ring
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TES
);
5106 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5107 LLVMValueRef param_rw_buffers
,
5108 unsigned param_pos_fixed_pt
)
5110 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5111 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5113 /* Use the fixed-point gl_FragCoord input.
5114 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5115 * per coordinate to get the repeating effect.
5117 address
[0] = si_unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5118 address
[1] = si_unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5120 /* Load the buffer descriptor. */
5121 slot
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
5122 desc
= ac_build_load_to_sgpr(&ctx
->ac
, param_rw_buffers
, slot
);
5124 /* The stipple pattern is 32x32, each row has 32 bits. */
5125 offset
= LLVMBuildMul(builder
, address
[1],
5126 LLVMConstInt(ctx
->i32
, 4, 0), "");
5127 row
= buffer_load_const(ctx
, desc
, offset
);
5128 row
= ac_to_integer(&ctx
->ac
, row
);
5129 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5130 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5131 ac_build_kill_if_false(&ctx
->ac
, bit
);
5134 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
5135 struct si_shader_config
*conf
,
5136 unsigned symbol_offset
)
5139 const unsigned char *config
=
5140 ac_shader_binary_config_start(binary
, symbol_offset
);
5141 bool really_needs_scratch
= false;
5143 /* LLVM adds SGPR spills to the scratch size.
5144 * Find out if we really need the scratch buffer.
5146 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5147 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
5149 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5150 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5151 really_needs_scratch
= true;
5156 /* XXX: We may be able to emit some of these values directly rather than
5157 * extracting fields to be emitted later.
5160 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5161 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5162 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5164 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5165 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5166 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5167 case R_00B428_SPI_SHADER_PGM_RSRC1_HS
:
5168 case R_00B848_COMPUTE_PGM_RSRC1
:
5169 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5170 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5171 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5172 conf
->rsrc1
= value
;
5174 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5175 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5177 case R_00B84C_COMPUTE_PGM_RSRC2
:
5178 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5179 conf
->rsrc2
= value
;
5181 case R_0286CC_SPI_PS_INPUT_ENA
:
5182 conf
->spi_ps_input_ena
= value
;
5184 case R_0286D0_SPI_PS_INPUT_ADDR
:
5185 conf
->spi_ps_input_addr
= value
;
5187 case R_0286E8_SPI_TMPRING_SIZE
:
5188 case R_00B860_COMPUTE_TMPRING_SIZE
:
5189 /* WAVESIZE is in units of 256 dwords. */
5190 if (really_needs_scratch
)
5191 conf
->scratch_bytes_per_wave
=
5192 G_00B860_WAVESIZE(value
) * 256 * 4;
5194 case 0x4: /* SPILLED_SGPRS */
5195 conf
->spilled_sgprs
= value
;
5197 case 0x8: /* SPILLED_VGPRS */
5198 conf
->spilled_vgprs
= value
;
5202 static bool printed
;
5205 fprintf(stderr
, "Warning: LLVM emitted unknown "
5206 "config register: 0x%x\n", reg
);
5214 if (!conf
->spi_ps_input_addr
)
5215 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5218 void si_shader_apply_scratch_relocs(struct si_shader
*shader
,
5219 uint64_t scratch_va
)
5222 uint32_t scratch_rsrc_dword0
= scratch_va
;
5223 uint32_t scratch_rsrc_dword1
=
5224 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5226 /* Enable scratch coalescing. */
5227 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5229 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5230 const struct ac_shader_reloc
*reloc
=
5231 &shader
->binary
.relocs
[i
];
5232 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5233 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5234 &scratch_rsrc_dword0
, 4);
5235 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5236 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5237 &scratch_rsrc_dword1
, 4);
5242 /* For the UMR disassembler. */
5243 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
5244 #define DEBUGGER_NUM_MARKERS 5
5246 static unsigned si_get_shader_binary_size(const struct si_shader
*shader
)
5248 unsigned size
= shader
->binary
.code_size
;
5251 size
+= shader
->prolog
->binary
.code_size
;
5252 if (shader
->previous_stage
)
5253 size
+= shader
->previous_stage
->binary
.code_size
;
5254 if (shader
->prolog2
)
5255 size
+= shader
->prolog2
->binary
.code_size
;
5257 size
+= shader
->epilog
->binary
.code_size
;
5258 return size
+ DEBUGGER_NUM_MARKERS
* 4;
5261 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5263 const struct ac_shader_binary
*prolog
=
5264 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
5265 const struct ac_shader_binary
*previous_stage
=
5266 shader
->previous_stage
? &shader
->previous_stage
->binary
: NULL
;
5267 const struct ac_shader_binary
*prolog2
=
5268 shader
->prolog2
? &shader
->prolog2
->binary
: NULL
;
5269 const struct ac_shader_binary
*epilog
=
5270 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
5271 const struct ac_shader_binary
*mainb
= &shader
->binary
;
5272 unsigned bo_size
= si_get_shader_binary_size(shader
) +
5273 (!epilog
? mainb
->rodata_size
: 0);
5276 assert(!prolog
|| !prolog
->rodata_size
);
5277 assert(!previous_stage
|| !previous_stage
->rodata_size
);
5278 assert(!prolog2
|| !prolog2
->rodata_size
);
5279 assert((!prolog
&& !previous_stage
&& !prolog2
&& !epilog
) ||
5280 !mainb
->rodata_size
);
5281 assert(!epilog
|| !epilog
->rodata_size
);
5283 r600_resource_reference(&shader
->bo
, NULL
);
5284 shader
->bo
= si_aligned_buffer_create(&sscreen
->b
,
5285 sscreen
->cpdma_prefetch_writes_memory
?
5286 0 : SI_RESOURCE_FLAG_READ_ONLY
,
5287 PIPE_USAGE_IMMUTABLE
,
5288 align(bo_size
, SI_CPDMA_ALIGNMENT
),
5294 ptr
= sscreen
->ws
->buffer_map(shader
->bo
->buf
, NULL
,
5295 PIPE_TRANSFER_READ_WRITE
|
5296 PIPE_TRANSFER_UNSYNCHRONIZED
|
5297 RADEON_TRANSFER_TEMPORARY
);
5299 /* Don't use util_memcpy_cpu_to_le32. LLVM binaries are
5300 * endian-independent. */
5302 memcpy(ptr
, prolog
->code
, prolog
->code_size
);
5303 ptr
+= prolog
->code_size
;
5305 if (previous_stage
) {
5306 memcpy(ptr
, previous_stage
->code
, previous_stage
->code_size
);
5307 ptr
+= previous_stage
->code_size
;
5310 memcpy(ptr
, prolog2
->code
, prolog2
->code_size
);
5311 ptr
+= prolog2
->code_size
;
5314 memcpy(ptr
, mainb
->code
, mainb
->code_size
);
5315 ptr
+= mainb
->code_size
;
5318 memcpy(ptr
, epilog
->code
, epilog
->code_size
);
5319 ptr
+= epilog
->code_size
;
5320 } else if (mainb
->rodata_size
> 0) {
5321 memcpy(ptr
, mainb
->rodata
, mainb
->rodata_size
);
5322 ptr
+= mainb
->rodata_size
;
5325 /* Add end-of-code markers for the UMR disassembler. */
5326 uint32_t *ptr32
= (uint32_t*)ptr
;
5327 for (unsigned i
= 0; i
< DEBUGGER_NUM_MARKERS
; i
++)
5328 ptr32
[i
] = DEBUGGER_END_OF_CODE_MARKER
;
5330 sscreen
->ws
->buffer_unmap(shader
->bo
->buf
);
5334 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
5335 struct pipe_debug_callback
*debug
,
5336 const char *name
, FILE *file
)
5341 if (binary
->disasm_string
) {
5342 fprintf(file
, "Shader %s disassembly:\n", name
);
5343 fprintf(file
, "%s", binary
->disasm_string
);
5345 if (debug
&& debug
->debug_message
) {
5346 /* Very long debug messages are cut off, so send the
5347 * disassembly one line at a time. This causes more
5348 * overhead, but on the plus side it simplifies
5349 * parsing of resulting logs.
5351 pipe_debug_message(debug
, SHADER_INFO
,
5352 "Shader Disassembly Begin");
5354 line
= binary
->disasm_string
;
5356 p
= util_strchrnul(line
, '\n');
5360 pipe_debug_message(debug
, SHADER_INFO
,
5361 "%.*s", count
, line
);
5369 pipe_debug_message(debug
, SHADER_INFO
,
5370 "Shader Disassembly End");
5373 fprintf(file
, "Shader %s binary:\n", name
);
5374 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
5375 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
5376 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
5377 binary
->code
[i
+ 1], binary
->code
[i
]);
5382 static void si_calculate_max_simd_waves(struct si_shader
*shader
)
5384 struct si_screen
*sscreen
= shader
->selector
->screen
;
5385 struct si_shader_config
*conf
= &shader
->config
;
5386 unsigned num_inputs
= shader
->selector
->info
.num_inputs
;
5387 unsigned lds_increment
= sscreen
->info
.chip_class
>= CIK
? 512 : 256;
5388 unsigned lds_per_wave
= 0;
5389 unsigned max_simd_waves
;
5391 max_simd_waves
= ac_get_max_simd_waves(sscreen
->info
.family
);
5393 /* Compute LDS usage for PS. */
5394 switch (shader
->selector
->type
) {
5395 case PIPE_SHADER_FRAGMENT
:
5396 /* The minimum usage per wave is (num_inputs * 48). The maximum
5397 * usage is (num_inputs * 48 * 16).
5398 * We can get anything in between and it varies between waves.
5400 * The 48 bytes per input for a single primitive is equal to
5401 * 4 bytes/component * 4 components/input * 3 points.
5403 * Other stages don't know the size at compile time or don't
5404 * allocate LDS per wave, but instead they do it per thread group.
5406 lds_per_wave
= conf
->lds_size
* lds_increment
+
5407 align(num_inputs
* 48, lds_increment
);
5409 case PIPE_SHADER_COMPUTE
:
5410 if (shader
->selector
) {
5411 unsigned max_workgroup_size
=
5412 si_get_max_workgroup_size(shader
);
5413 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
5414 DIV_ROUND_UP(max_workgroup_size
, 64);
5419 /* Compute the per-SIMD wave counts. */
5420 if (conf
->num_sgprs
) {
5421 if (sscreen
->info
.chip_class
>= VI
)
5422 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
5424 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
5427 if (conf
->num_vgprs
)
5428 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
5430 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
5431 * 16KB makes some SIMDs unoccupied). */
5433 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
5435 conf
->max_simd_waves
= max_simd_waves
;
5438 void si_shader_dump_stats_for_shader_db(const struct si_shader
*shader
,
5439 struct pipe_debug_callback
*debug
)
5441 const struct si_shader_config
*conf
= &shader
->config
;
5443 pipe_debug_message(debug
, SHADER_INFO
,
5444 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
5445 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
5446 "Spilled VGPRs: %d PrivMem VGPRs: %d",
5447 conf
->num_sgprs
, conf
->num_vgprs
,
5448 si_get_shader_binary_size(shader
),
5449 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5450 conf
->max_simd_waves
, conf
->spilled_sgprs
,
5451 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
5454 static void si_shader_dump_stats(struct si_screen
*sscreen
,
5455 const struct si_shader
*shader
,
5458 bool check_debug_option
)
5460 const struct si_shader_config
*conf
= &shader
->config
;
5462 if (!check_debug_option
||
5463 si_can_dump_shader(sscreen
, processor
)) {
5464 if (processor
== PIPE_SHADER_FRAGMENT
) {
5465 fprintf(file
, "*** SHADER CONFIG ***\n"
5466 "SPI_PS_INPUT_ADDR = 0x%04x\n"
5467 "SPI_PS_INPUT_ENA = 0x%04x\n",
5468 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
5471 fprintf(file
, "*** SHADER STATS ***\n"
5474 "Spilled SGPRs: %d\n"
5475 "Spilled VGPRs: %d\n"
5476 "Private memory VGPRs: %d\n"
5477 "Code Size: %d bytes\n"
5479 "Scratch: %d bytes per wave\n"
5481 "********************\n\n\n",
5482 conf
->num_sgprs
, conf
->num_vgprs
,
5483 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
5484 conf
->private_mem_vgprs
,
5485 si_get_shader_binary_size(shader
),
5486 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5487 conf
->max_simd_waves
);
5491 const char *si_get_shader_name(const struct si_shader
*shader
, unsigned processor
)
5493 switch (processor
) {
5494 case PIPE_SHADER_VERTEX
:
5495 if (shader
->key
.as_es
)
5496 return "Vertex Shader as ES";
5497 else if (shader
->key
.as_ls
)
5498 return "Vertex Shader as LS";
5500 return "Vertex Shader as VS";
5501 case PIPE_SHADER_TESS_CTRL
:
5502 return "Tessellation Control Shader";
5503 case PIPE_SHADER_TESS_EVAL
:
5504 if (shader
->key
.as_es
)
5505 return "Tessellation Evaluation Shader as ES";
5507 return "Tessellation Evaluation Shader as VS";
5508 case PIPE_SHADER_GEOMETRY
:
5509 if (shader
->is_gs_copy_shader
)
5510 return "GS Copy Shader as VS";
5512 return "Geometry Shader";
5513 case PIPE_SHADER_FRAGMENT
:
5514 return "Pixel Shader";
5515 case PIPE_SHADER_COMPUTE
:
5516 return "Compute Shader";
5518 return "Unknown Shader";
5522 void si_shader_dump(struct si_screen
*sscreen
, const struct si_shader
*shader
,
5523 struct pipe_debug_callback
*debug
, unsigned processor
,
5524 FILE *file
, bool check_debug_option
)
5526 if (!check_debug_option
||
5527 si_can_dump_shader(sscreen
, processor
))
5528 si_dump_shader_key(processor
, shader
, file
);
5530 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
5531 if (shader
->previous_stage
&&
5532 shader
->previous_stage
->binary
.llvm_ir_string
) {
5533 fprintf(file
, "\n%s - previous stage - LLVM IR:\n\n",
5534 si_get_shader_name(shader
, processor
));
5535 fprintf(file
, "%s\n", shader
->previous_stage
->binary
.llvm_ir_string
);
5538 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
5539 si_get_shader_name(shader
, processor
));
5540 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
5543 if (!check_debug_option
||
5544 (si_can_dump_shader(sscreen
, processor
) &&
5545 !(sscreen
->debug_flags
& DBG(NO_ASM
)))) {
5546 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5549 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5550 debug
, "prolog", file
);
5551 if (shader
->previous_stage
)
5552 si_shader_dump_disassembly(&shader
->previous_stage
->binary
,
5553 debug
, "previous stage", file
);
5554 if (shader
->prolog2
)
5555 si_shader_dump_disassembly(&shader
->prolog2
->binary
,
5556 debug
, "prolog2", file
);
5558 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5561 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5562 debug
, "epilog", file
);
5563 fprintf(file
, "\n");
5566 si_shader_dump_stats(sscreen
, shader
, processor
, file
,
5567 check_debug_option
);
5570 static int si_compile_llvm(struct si_screen
*sscreen
,
5571 struct ac_shader_binary
*binary
,
5572 struct si_shader_config
*conf
,
5573 struct ac_llvm_compiler
*compiler
,
5575 struct pipe_debug_callback
*debug
,
5578 bool less_optimized
)
5581 unsigned count
= p_atomic_inc_return(&sscreen
->num_compilations
);
5583 if (si_can_dump_shader(sscreen
, processor
)) {
5584 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5586 if (!(sscreen
->debug_flags
& (DBG(NO_IR
) | DBG(PREOPT_IR
)))) {
5587 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5588 ac_dump_module(mod
);
5589 fprintf(stderr
, "\n");
5593 if (sscreen
->record_llvm_ir
) {
5594 char *ir
= LLVMPrintModuleToString(mod
);
5595 binary
->llvm_ir_string
= strdup(ir
);
5596 LLVMDisposeMessage(ir
);
5599 if (!si_replace_shader(count
, binary
)) {
5600 r
= si_llvm_compile(mod
, binary
, compiler
, debug
,
5606 si_shader_binary_read_config(binary
, conf
, 0);
5608 /* Enable 64-bit and 16-bit denormals, because there is no performance
5611 * If denormals are enabled, all floating-point output modifiers are
5614 * Don't enable denormals for 32-bit floats, because:
5615 * - Floating-point output modifiers would be ignored by the hw.
5616 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5617 * have to stop using those.
5618 * - SI & CI would be very slow.
5620 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5622 FREE(binary
->config
);
5623 FREE(binary
->global_symbol_offsets
);
5624 binary
->config
= NULL
;
5625 binary
->global_symbol_offsets
= NULL
;
5627 /* Some shaders can't have rodata because their binaries can be
5630 if (binary
->rodata_size
&&
5631 (processor
== PIPE_SHADER_VERTEX
||
5632 processor
== PIPE_SHADER_TESS_CTRL
||
5633 processor
== PIPE_SHADER_TESS_EVAL
||
5634 processor
== PIPE_SHADER_FRAGMENT
)) {
5635 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5642 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
5644 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
5645 LLVMBuildRetVoid(ctx
->ac
.builder
);
5647 LLVMBuildRet(ctx
->ac
.builder
, ret
);
5650 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5652 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5653 struct ac_llvm_compiler
*compiler
,
5654 struct si_shader_selector
*gs_selector
,
5655 struct pipe_debug_callback
*debug
)
5657 struct si_shader_context ctx
;
5658 struct si_shader
*shader
;
5659 LLVMBuilderRef builder
;
5660 struct si_shader_output_values outputs
[SI_MAX_VS_OUTPUTS
];
5661 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
5665 shader
= CALLOC_STRUCT(si_shader
);
5669 /* We can leave the fence as permanently signaled because the GS copy
5670 * shader only becomes visible globally after it has been compiled. */
5671 util_queue_fence_init(&shader
->ready
);
5673 shader
->selector
= gs_selector
;
5674 shader
->is_gs_copy_shader
= true;
5676 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
5677 ctx
.shader
= shader
;
5678 ctx
.type
= PIPE_SHADER_VERTEX
;
5680 builder
= ctx
.ac
.builder
;
5682 create_function(&ctx
);
5683 preload_ring_buffers(&ctx
);
5685 LLVMValueRef voffset
=
5686 LLVMBuildMul(ctx
.ac
.builder
, ctx
.abi
.vertex_id
,
5687 LLVMConstInt(ctx
.i32
, 4, 0), "");
5689 /* Fetch the vertex stream ID.*/
5690 LLVMValueRef stream_id
;
5692 if (gs_selector
->so
.num_outputs
)
5693 stream_id
= si_unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
5695 stream_id
= ctx
.i32_0
;
5697 /* Fill in output information. */
5698 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5699 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
5700 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
5702 for (int chan
= 0; chan
< 4; chan
++) {
5703 outputs
[i
].vertex_stream
[chan
] =
5704 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
5708 LLVMBasicBlockRef end_bb
;
5709 LLVMValueRef switch_inst
;
5711 end_bb
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, ctx
.main_fn
, "end");
5712 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
5714 for (int stream
= 0; stream
< 4; stream
++) {
5715 LLVMBasicBlockRef bb
;
5718 if (!gsinfo
->num_stream_output_components
[stream
])
5721 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
5724 bb
= LLVMInsertBasicBlockInContext(ctx
.ac
.context
, end_bb
, "out");
5725 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
.i32
, stream
, 0), bb
);
5726 LLVMPositionBuilderAtEnd(builder
, bb
);
5728 /* Fetch vertex data from GSVS ring */
5730 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5731 for (unsigned chan
= 0; chan
< 4; chan
++) {
5732 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
5733 outputs
[i
].vertex_stream
[chan
] != stream
) {
5734 outputs
[i
].values
[chan
] = LLVMGetUndef(ctx
.f32
);
5738 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
5739 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
5742 outputs
[i
].values
[chan
] =
5743 ac_build_buffer_load(&ctx
.ac
,
5744 ctx
.gsvs_ring
[0], 1,
5751 /* Streamout and exports. */
5752 if (gs_selector
->so
.num_outputs
) {
5753 si_llvm_emit_streamout(&ctx
, outputs
,
5754 gsinfo
->num_outputs
,
5759 /* Vertex color clamping.
5761 * This uses a state constant loaded in a user data SGPR and
5762 * an IF statement is added that clamps all colors if the constant
5765 struct lp_build_if_state if_ctx
;
5766 LLVMValueRef v
[2], cond
= NULL
;
5767 LLVMBasicBlockRef blocks
[2];
5769 for (unsigned i
= 0; i
< gsinfo
->num_outputs
; i
++) {
5770 if (gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
5771 gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
5774 /* We've found a color. */
5776 /* The state is in the first bit of the user SGPR. */
5777 cond
= LLVMGetParam(ctx
.main_fn
,
5778 ctx
.param_vs_state_bits
);
5779 cond
= LLVMBuildTrunc(ctx
.ac
.builder
, cond
,
5781 lp_build_if(&if_ctx
, &ctx
.gallivm
, cond
);
5782 /* Remember blocks for Phi. */
5783 blocks
[0] = if_ctx
.true_block
;
5784 blocks
[1] = if_ctx
.entry_block
;
5787 for (unsigned j
= 0; j
< 4; j
++) {
5788 /* Insert clamp into the true block. */
5789 v
[0] = ac_build_clamp(&ctx
.ac
, outputs
[i
].values
[j
]);
5790 v
[1] = outputs
[i
].values
[j
];
5792 /* Insert Phi into the endif block. */
5793 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.merge_block
);
5794 outputs
[i
].values
[j
] = ac_build_phi(&ctx
.ac
, ctx
.f32
, 2, v
, blocks
);
5795 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.true_block
);
5799 lp_build_endif(&if_ctx
);
5801 si_llvm_export_vs(&ctx
, outputs
, gsinfo
->num_outputs
);
5804 LLVMBuildBr(builder
, end_bb
);
5807 LLVMPositionBuilderAtEnd(builder
, end_bb
);
5809 LLVMBuildRetVoid(ctx
.ac
.builder
);
5811 ctx
.type
= PIPE_SHADER_GEOMETRY
; /* override for shader dumping */
5812 si_llvm_optimize_module(&ctx
);
5814 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
5815 &ctx
.shader
->config
, ctx
.compiler
,
5817 debug
, PIPE_SHADER_GEOMETRY
,
5818 "GS Copy Shader", false);
5820 if (si_can_dump_shader(sscreen
, PIPE_SHADER_GEOMETRY
))
5821 fprintf(stderr
, "GS Copy Shader:\n");
5822 si_shader_dump(sscreen
, ctx
.shader
, debug
,
5823 PIPE_SHADER_GEOMETRY
, stderr
, true);
5824 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
5827 si_llvm_dispose(&ctx
);
5836 static void si_dump_shader_key_vs(const struct si_shader_key
*key
,
5837 const struct si_vs_prolog_bits
*prolog
,
5838 const char *prefix
, FILE *f
)
5840 fprintf(f
, " %s.instance_divisor_is_one = %u\n",
5841 prefix
, prolog
->instance_divisor_is_one
);
5842 fprintf(f
, " %s.instance_divisor_is_fetched = %u\n",
5843 prefix
, prolog
->instance_divisor_is_fetched
);
5844 fprintf(f
, " %s.ls_vgpr_fix = %u\n",
5845 prefix
, prolog
->ls_vgpr_fix
);
5847 fprintf(f
, " mono.vs.fix_fetch = {");
5848 for (int i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
5849 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs_fix_fetch
[i
]);
5853 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
5856 const struct si_shader_key
*key
= &shader
->key
;
5858 fprintf(f
, "SHADER KEY\n");
5860 switch (processor
) {
5861 case PIPE_SHADER_VERTEX
:
5862 si_dump_shader_key_vs(key
, &key
->part
.vs
.prolog
,
5863 "part.vs.prolog", f
);
5864 fprintf(f
, " as_es = %u\n", key
->as_es
);
5865 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
5866 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5867 key
->mono
.u
.vs_export_prim_id
);
5870 case PIPE_SHADER_TESS_CTRL
:
5871 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
) {
5872 si_dump_shader_key_vs(key
, &key
->part
.tcs
.ls_prolog
,
5873 "part.tcs.ls_prolog", f
);
5875 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
5876 fprintf(f
, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.u
.ff_tcs_inputs_to_copy
);
5879 case PIPE_SHADER_TESS_EVAL
:
5880 fprintf(f
, " as_es = %u\n", key
->as_es
);
5881 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5882 key
->mono
.u
.vs_export_prim_id
);
5885 case PIPE_SHADER_GEOMETRY
:
5886 if (shader
->is_gs_copy_shader
)
5889 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
&&
5890 key
->part
.gs
.es
->type
== PIPE_SHADER_VERTEX
) {
5891 si_dump_shader_key_vs(key
, &key
->part
.gs
.vs_prolog
,
5892 "part.gs.vs_prolog", f
);
5894 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
5897 case PIPE_SHADER_COMPUTE
:
5900 case PIPE_SHADER_FRAGMENT
:
5901 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
5902 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
5903 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
5904 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
5905 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
5906 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
5907 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
5908 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
5909 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
5910 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
5911 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
5912 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
5913 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
5914 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
5915 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
5916 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
5917 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
5924 if ((processor
== PIPE_SHADER_GEOMETRY
||
5925 processor
== PIPE_SHADER_TESS_EVAL
||
5926 processor
== PIPE_SHADER_VERTEX
) &&
5927 !key
->as_es
&& !key
->as_ls
) {
5928 fprintf(f
, " opt.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.kill_outputs
);
5929 fprintf(f
, " opt.clip_disable = %u\n", key
->opt
.clip_disable
);
5933 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5934 struct si_screen
*sscreen
,
5935 struct ac_llvm_compiler
*compiler
)
5937 struct lp_build_tgsi_context
*bld_base
;
5939 si_llvm_context_init(ctx
, sscreen
, compiler
);
5941 bld_base
= &ctx
->bld_base
;
5942 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5944 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
].emit
= build_interp_intrinsic
;
5945 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
].emit
= build_interp_intrinsic
;
5946 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
].emit
= build_interp_intrinsic
;
5948 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
5950 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
5952 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5953 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5954 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5955 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5957 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
5958 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
5959 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
5960 bld_base
->op_actions
[TGSI_OPCODE_BALLOT
].emit
= ballot_emit
;
5961 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].intr_name
= "llvm.amdgcn.readfirstlane";
5962 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].emit
= read_lane_emit
;
5963 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].intr_name
= "llvm.amdgcn.readlane";
5964 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].emit
= read_lane_emit
;
5966 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_tgsi_emit_vertex
;
5967 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_tgsi_emit_primitive
;
5968 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5971 static void si_optimize_vs_outputs(struct si_shader_context
*ctx
)
5973 struct si_shader
*shader
= ctx
->shader
;
5974 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5976 if ((ctx
->type
!= PIPE_SHADER_VERTEX
&&
5977 ctx
->type
!= PIPE_SHADER_TESS_EVAL
) ||
5978 shader
->key
.as_ls
||
5982 ac_optimize_vs_outputs(&ctx
->ac
,
5984 shader
->info
.vs_output_param_offset
,
5986 &shader
->info
.nr_param_exports
);
5989 static void si_init_exec_from_input(struct si_shader_context
*ctx
,
5990 unsigned param
, unsigned bitoffset
)
5992 LLVMValueRef args
[] = {
5993 LLVMGetParam(ctx
->main_fn
, param
),
5994 LLVMConstInt(ctx
->i32
, bitoffset
, 0),
5996 ac_build_intrinsic(&ctx
->ac
,
5997 "llvm.amdgcn.init.exec.from.input",
5998 ctx
->voidt
, args
, 2, AC_FUNC_ATTR_CONVERGENT
);
6001 static bool si_vs_needs_prolog(const struct si_shader_selector
*sel
,
6002 const struct si_vs_prolog_bits
*key
)
6004 /* VGPR initialization fixup for Vega10 and Raven is always done in the
6006 return sel
->vs_needs_prolog
|| key
->ls_vgpr_fix
;
6009 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
)
6011 struct si_shader
*shader
= ctx
->shader
;
6012 struct si_shader_selector
*sel
= shader
->selector
;
6013 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6015 // TODO clean all this up!
6016 switch (ctx
->type
) {
6017 case PIPE_SHADER_VERTEX
:
6018 ctx
->load_input
= declare_input_vs
;
6019 if (shader
->key
.as_ls
)
6020 ctx
->abi
.emit_outputs
= si_llvm_emit_ls_epilogue
;
6021 else if (shader
->key
.as_es
)
6022 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6024 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6025 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6026 ctx
->abi
.load_base_vertex
= get_base_vertex
;
6028 case PIPE_SHADER_TESS_CTRL
:
6029 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6030 ctx
->abi
.load_tess_varyings
= si_nir_load_tcs_varyings
;
6031 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6032 bld_base
->emit_store
= store_output_tcs
;
6033 ctx
->abi
.store_tcs_outputs
= si_nir_store_output_tcs
;
6034 ctx
->abi
.emit_outputs
= si_llvm_emit_tcs_epilogue
;
6035 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6036 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6038 case PIPE_SHADER_TESS_EVAL
:
6039 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6040 ctx
->abi
.load_tess_varyings
= si_nir_load_input_tes
;
6041 ctx
->abi
.load_tess_coord
= si_load_tess_coord
;
6042 ctx
->abi
.load_tess_level
= si_load_tess_level
;
6043 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6044 if (shader
->key
.as_es
)
6045 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6047 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6048 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6050 case PIPE_SHADER_GEOMETRY
:
6051 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6052 ctx
->abi
.load_inputs
= si_nir_load_input_gs
;
6053 ctx
->abi
.emit_vertex
= si_llvm_emit_vertex
;
6054 ctx
->abi
.emit_primitive
= si_llvm_emit_primitive
;
6055 ctx
->abi
.emit_outputs
= si_llvm_emit_gs_epilogue
;
6056 bld_base
->emit_epilogue
= si_tgsi_emit_gs_epilogue
;
6058 case PIPE_SHADER_FRAGMENT
:
6059 ctx
->load_input
= declare_input_fs
;
6060 ctx
->abi
.emit_outputs
= si_llvm_return_fs_outputs
;
6061 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6062 ctx
->abi
.lookup_interp_param
= si_nir_lookup_interp_param
;
6063 ctx
->abi
.load_sample_position
= load_sample_position
;
6064 ctx
->abi
.load_sample_mask_in
= load_sample_mask_in
;
6065 ctx
->abi
.emit_kill
= si_llvm_emit_kill
;
6067 case PIPE_SHADER_COMPUTE
:
6068 ctx
->abi
.load_local_group_size
= get_block_size
;
6071 assert(!"Unsupported shader type");
6075 ctx
->abi
.load_ubo
= load_ubo
;
6076 ctx
->abi
.load_ssbo
= load_ssbo
;
6078 create_function(ctx
);
6079 preload_ring_buffers(ctx
);
6081 /* For GFX9 merged shaders:
6082 * - Set EXEC for the first shader. If the prolog is present, set
6083 * EXEC there instead.
6084 * - Add a barrier before the second shader.
6085 * - In the second shader, reset EXEC to ~0 and wrap the main part in
6086 * an if-statement. This is required for correctness in geometry
6087 * shaders, to ensure that empty GS waves do not send GS_EMIT and
6090 * For monolithic merged shaders, the first shader is wrapped in an
6091 * if-block together with its prolog in si_build_wrapper_function.
6093 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6094 if (!shader
->is_monolithic
&&
6095 sel
->info
.num_instructions
> 1 && /* not empty shader */
6096 (shader
->key
.as_es
|| shader
->key
.as_ls
) &&
6097 (ctx
->type
== PIPE_SHADER_TESS_EVAL
||
6098 (ctx
->type
== PIPE_SHADER_VERTEX
&&
6099 !si_vs_needs_prolog(sel
, &shader
->key
.part
.vs
.prolog
)))) {
6100 si_init_exec_from_input(ctx
,
6101 ctx
->param_merged_wave_info
, 0);
6102 } else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
6103 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6104 if (!shader
->is_monolithic
)
6105 ac_init_exec_full_mask(&ctx
->ac
);
6107 LLVMValueRef num_threads
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 8, 8);
6109 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
6110 ac_get_thread_id(&ctx
->ac
), num_threads
, "");
6111 lp_build_if(&ctx
->merged_wrap_if_state
, &ctx
->gallivm
, ena
);
6113 /* The barrier must execute for all shaders in a
6116 * Execute the barrier inside the conditional block,
6117 * so that empty waves can jump directly to s_endpgm,
6118 * which will also signal the barrier.
6120 * If the shader is TCS and the TCS epilog is present
6121 * and contains a barrier, it will wait there and then
6124 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
6128 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&&
6129 sel
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
6130 for (unsigned i
= 0; i
< 6; i
++) {
6131 ctx
->invoc0_tess_factors
[i
] =
6132 ac_build_alloca_undef(&ctx
->ac
, ctx
->i32
, "");
6136 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6138 for (i
= 0; i
< 4; i
++) {
6139 ctx
->gs_next_vertex
[i
] =
6140 ac_build_alloca(&ctx
->ac
, ctx
->i32
, "");
6144 if (sel
->force_correct_derivs_after_kill
) {
6145 ctx
->postponed_kill
= ac_build_alloca_undef(&ctx
->ac
, ctx
->i1
, "");
6146 /* true = don't kill. */
6147 LLVMBuildStore(ctx
->ac
.builder
, ctx
->i1true
,
6148 ctx
->postponed_kill
);
6152 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6153 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6157 if (!si_nir_build_llvm(ctx
, sel
->nir
)) {
6158 fprintf(stderr
, "Failed to translate shader from NIR to LLVM\n");
6163 si_llvm_build_ret(ctx
, ctx
->return_value
);
6168 * Compute the VS prolog key, which contains all the information needed to
6169 * build the VS prolog function, and set shader->info bits where needed.
6171 * \param info Shader info of the vertex shader.
6172 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
6173 * \param prolog_key Key of the VS prolog
6174 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
6175 * \param key Output shader part key.
6177 static void si_get_vs_prolog_key(const struct tgsi_shader_info
*info
,
6178 unsigned num_input_sgprs
,
6179 const struct si_vs_prolog_bits
*prolog_key
,
6180 struct si_shader
*shader_out
,
6181 union si_shader_part_key
*key
)
6183 memset(key
, 0, sizeof(*key
));
6184 key
->vs_prolog
.states
= *prolog_key
;
6185 key
->vs_prolog
.num_input_sgprs
= num_input_sgprs
;
6186 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6187 key
->vs_prolog
.as_ls
= shader_out
->key
.as_ls
;
6188 key
->vs_prolog
.as_es
= shader_out
->key
.as_es
;
6190 if (shader_out
->selector
->type
== PIPE_SHADER_TESS_CTRL
) {
6191 key
->vs_prolog
.as_ls
= 1;
6192 key
->vs_prolog
.num_merged_next_stage_vgprs
= 2;
6193 } else if (shader_out
->selector
->type
== PIPE_SHADER_GEOMETRY
) {
6194 key
->vs_prolog
.as_es
= 1;
6195 key
->vs_prolog
.num_merged_next_stage_vgprs
= 5;
6198 /* Enable loading the InstanceID VGPR. */
6199 uint16_t input_mask
= u_bit_consecutive(0, info
->num_inputs
);
6201 if ((key
->vs_prolog
.states
.instance_divisor_is_one
|
6202 key
->vs_prolog
.states
.instance_divisor_is_fetched
) & input_mask
)
6203 shader_out
->info
.uses_instanceid
= true;
6207 * Compute the PS prolog key, which contains all the information needed to
6208 * build the PS prolog function, and set related bits in shader->config.
6210 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6211 union si_shader_part_key
*key
,
6212 bool separate_prolog
)
6214 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6216 memset(key
, 0, sizeof(*key
));
6217 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6218 key
->ps_prolog
.colors_read
= info
->colors_read
;
6219 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6220 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6221 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6222 (key
->ps_prolog
.colors_read
||
6223 key
->ps_prolog
.states
.force_persp_sample_interp
||
6224 key
->ps_prolog
.states
.force_linear_sample_interp
||
6225 key
->ps_prolog
.states
.force_persp_center_interp
||
6226 key
->ps_prolog
.states
.force_linear_center_interp
||
6227 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6228 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6229 key
->ps_prolog
.ancillary_vgpr_index
= shader
->info
.ancillary_vgpr_index
;
6231 if (info
->colors_read
) {
6232 unsigned *color
= shader
->selector
->color_attr_index
;
6234 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6235 /* BCOLORs are stored after the last input. */
6236 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6237 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6238 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6241 for (unsigned i
= 0; i
< 2; i
++) {
6242 unsigned interp
= info
->input_interpolate
[color
[i
]];
6243 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6245 if (!(info
->colors_read
& (0xf << i
*4)))
6248 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6250 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6251 interp
== TGSI_INTERPOLATE_COLOR
)
6252 interp
= TGSI_INTERPOLATE_CONSTANT
;
6255 case TGSI_INTERPOLATE_CONSTANT
:
6256 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6258 case TGSI_INTERPOLATE_PERSPECTIVE
:
6259 case TGSI_INTERPOLATE_COLOR
:
6260 /* Force the interpolation location for colors here. */
6261 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6262 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6263 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6264 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6267 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6268 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6269 shader
->config
.spi_ps_input_ena
|=
6270 S_0286CC_PERSP_SAMPLE_ENA(1);
6272 case TGSI_INTERPOLATE_LOC_CENTER
:
6273 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6274 shader
->config
.spi_ps_input_ena
|=
6275 S_0286CC_PERSP_CENTER_ENA(1);
6277 case TGSI_INTERPOLATE_LOC_CENTROID
:
6278 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6279 shader
->config
.spi_ps_input_ena
|=
6280 S_0286CC_PERSP_CENTROID_ENA(1);
6286 case TGSI_INTERPOLATE_LINEAR
:
6287 /* Force the interpolation location for colors here. */
6288 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
6289 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6290 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
6291 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6293 /* The VGPR assignment for non-monolithic shaders
6294 * works because InitialPSInputAddr is set on the
6295 * main shader and PERSP_PULL_MODEL is never used.
6298 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6299 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6300 separate_prolog
? 6 : 9;
6301 shader
->config
.spi_ps_input_ena
|=
6302 S_0286CC_LINEAR_SAMPLE_ENA(1);
6304 case TGSI_INTERPOLATE_LOC_CENTER
:
6305 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6306 separate_prolog
? 8 : 11;
6307 shader
->config
.spi_ps_input_ena
|=
6308 S_0286CC_LINEAR_CENTER_ENA(1);
6310 case TGSI_INTERPOLATE_LOC_CENTROID
:
6311 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6312 separate_prolog
? 10 : 13;
6313 shader
->config
.spi_ps_input_ena
|=
6314 S_0286CC_LINEAR_CENTROID_ENA(1);
6328 * Check whether a PS prolog is required based on the key.
6330 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
6332 return key
->ps_prolog
.colors_read
||
6333 key
->ps_prolog
.states
.force_persp_sample_interp
||
6334 key
->ps_prolog
.states
.force_linear_sample_interp
||
6335 key
->ps_prolog
.states
.force_persp_center_interp
||
6336 key
->ps_prolog
.states
.force_linear_center_interp
||
6337 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6338 key
->ps_prolog
.states
.bc_optimize_for_linear
||
6339 key
->ps_prolog
.states
.poly_stipple
||
6340 key
->ps_prolog
.states
.samplemask_log_ps_iter
;
6344 * Compute the PS epilog key, which contains all the information needed to
6345 * build the PS epilog function.
6347 static void si_get_ps_epilog_key(struct si_shader
*shader
,
6348 union si_shader_part_key
*key
)
6350 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6351 memset(key
, 0, sizeof(*key
));
6352 key
->ps_epilog
.colors_written
= info
->colors_written
;
6353 key
->ps_epilog
.writes_z
= info
->writes_z
;
6354 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
6355 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6356 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
6360 * Build the GS prolog function. Rotate the input vertices for triangle strips
6363 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
6364 union si_shader_part_key
*key
)
6366 unsigned num_sgprs
, num_vgprs
;
6367 struct si_function_info fninfo
;
6368 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6369 LLVMTypeRef returns
[48];
6370 LLVMValueRef func
, ret
;
6372 si_init_function_info(&fninfo
);
6374 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6375 if (key
->gs_prolog
.states
.gfx9_prev_is_vs
)
6376 num_sgprs
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
6378 num_sgprs
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
6379 num_vgprs
= 5; /* ES inputs are not needed by GS */
6381 num_sgprs
= GFX6_GS_NUM_USER_SGPR
+ 2;
6385 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
6386 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
6387 returns
[i
] = ctx
->i32
;
6390 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
6391 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
6392 returns
[num_sgprs
+ i
] = ctx
->f32
;
6395 /* Create the function. */
6396 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
6398 func
= ctx
->main_fn
;
6400 /* Set the full EXEC mask for the prolog, because we are only fiddling
6401 * with registers here. The main shader part will set the correct EXEC
6404 if (ctx
->screen
->info
.chip_class
>= GFX9
&& !key
->gs_prolog
.is_monolithic
)
6405 ac_init_exec_full_mask(&ctx
->ac
);
6407 /* Copy inputs to outputs. This should be no-op, as the registers match,
6408 * but it will prevent the compiler from overwriting them unintentionally.
6410 ret
= ctx
->return_value
;
6411 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
6412 LLVMValueRef p
= LLVMGetParam(func
, i
);
6413 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
6415 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
6416 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
6417 p
= ac_to_float(&ctx
->ac
, p
);
6418 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
6421 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
6422 /* Remap the input vertices for every other primitive. */
6423 const unsigned gfx6_vtx_params
[6] = {
6431 const unsigned gfx9_vtx_params
[3] = {
6436 LLVMValueRef vtx_in
[6], vtx_out
[6];
6437 LLVMValueRef prim_id
, rotate
;
6439 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6440 for (unsigned i
= 0; i
< 3; i
++) {
6441 vtx_in
[i
*2] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 0, 16);
6442 vtx_in
[i
*2+1] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 16, 16);
6445 for (unsigned i
= 0; i
< 6; i
++)
6446 vtx_in
[i
] = LLVMGetParam(func
, gfx6_vtx_params
[i
]);
6449 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
6450 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
6452 for (unsigned i
= 0; i
< 6; ++i
) {
6453 LLVMValueRef base
, rotated
;
6455 rotated
= vtx_in
[(i
+ 4) % 6];
6456 vtx_out
[i
] = LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
6459 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6460 for (unsigned i
= 0; i
< 3; i
++) {
6461 LLVMValueRef hi
, out
;
6463 hi
= LLVMBuildShl(builder
, vtx_out
[i
*2+1],
6464 LLVMConstInt(ctx
->i32
, 16, 0), "");
6465 out
= LLVMBuildOr(builder
, vtx_out
[i
*2], hi
, "");
6466 out
= ac_to_float(&ctx
->ac
, out
);
6467 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6468 gfx9_vtx_params
[i
], "");
6471 for (unsigned i
= 0; i
< 6; i
++) {
6474 out
= ac_to_float(&ctx
->ac
, vtx_out
[i
]);
6475 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6476 gfx6_vtx_params
[i
], "");
6481 LLVMBuildRet(builder
, ret
);
6485 * Given a list of shader part functions, build a wrapper function that
6486 * runs them in sequence to form a monolithic shader.
6488 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
6489 LLVMValueRef
*parts
,
6492 unsigned next_shader_first_part
)
6494 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6495 /* PS epilog has one arg per color component; gfx9 merged shader
6496 * prologs need to forward 32 user SGPRs.
6498 struct si_function_info fninfo
;
6499 LLVMValueRef initial
[64], out
[64];
6500 LLVMTypeRef function_type
;
6501 unsigned num_first_params
;
6502 unsigned num_out
, initial_num_out
;
6503 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
6504 MAYBE_UNUSED
unsigned initial_num_out_sgpr
; /* used in debug checks */
6505 unsigned num_sgprs
, num_vgprs
;
6507 struct lp_build_if_state if_state
;
6509 si_init_function_info(&fninfo
);
6511 for (unsigned i
= 0; i
< num_parts
; ++i
) {
6512 ac_add_function_attr(ctx
->ac
.context
, parts
[i
], -1,
6513 AC_FUNC_ATTR_ALWAYSINLINE
);
6514 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
6517 /* The parameters of the wrapper function correspond to those of the
6518 * first part in terms of SGPRs and VGPRs, but we use the types of the
6519 * main part to get the right types. This is relevant for the
6520 * dereferenceable attribute on descriptor table pointers.
6525 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
6526 num_first_params
= LLVMCountParamTypes(function_type
);
6528 for (unsigned i
= 0; i
< num_first_params
; ++i
) {
6529 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
6531 if (ac_is_sgpr_param(param
)) {
6532 assert(num_vgprs
== 0);
6533 num_sgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6535 num_vgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6540 while (gprs
< num_sgprs
+ num_vgprs
) {
6541 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], fninfo
.num_params
);
6542 LLVMTypeRef type
= LLVMTypeOf(param
);
6543 unsigned size
= ac_get_type_size(type
) / 4;
6545 add_arg(&fninfo
, gprs
< num_sgprs
? ARG_SGPR
: ARG_VGPR
, type
);
6547 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
6548 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
6549 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
6554 si_create_function(ctx
, "wrapper", NULL
, 0, &fninfo
,
6555 si_get_max_workgroup_size(ctx
->shader
));
6557 if (is_merged_shader(ctx
))
6558 ac_init_exec_full_mask(&ctx
->ac
);
6560 /* Record the arguments of the function as if they were an output of
6566 for (unsigned i
= 0; i
< fninfo
.num_params
; ++i
) {
6567 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
6568 LLVMTypeRef param_type
= LLVMTypeOf(param
);
6569 LLVMTypeRef out_type
= i
< fninfo
.num_sgpr_params
? ctx
->i32
: ctx
->f32
;
6570 unsigned size
= ac_get_type_size(param_type
) / 4;
6573 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6574 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i32
, "");
6575 param_type
= ctx
->i32
;
6578 if (param_type
!= out_type
)
6579 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
6580 out
[num_out
++] = param
;
6582 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
6584 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6585 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
6586 param_type
= ctx
->i64
;
6589 if (param_type
!= vector_type
)
6590 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
6592 for (unsigned j
= 0; j
< size
; ++j
)
6593 out
[num_out
++] = LLVMBuildExtractElement(
6594 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
6597 if (i
< fninfo
.num_sgpr_params
)
6598 num_out_sgpr
= num_out
;
6601 memcpy(initial
, out
, sizeof(out
));
6602 initial_num_out
= num_out
;
6603 initial_num_out_sgpr
= num_out_sgpr
;
6605 /* Now chain the parts. */
6606 for (unsigned part
= 0; part
< num_parts
; ++part
) {
6607 LLVMValueRef in
[48];
6609 LLVMTypeRef ret_type
;
6610 unsigned out_idx
= 0;
6611 unsigned num_params
= LLVMCountParams(parts
[part
]);
6613 /* Merged shaders are executed conditionally depending
6614 * on the number of enabled threads passed in the input SGPRs. */
6615 if (is_merged_shader(ctx
) && part
== 0) {
6616 LLVMValueRef ena
, count
= initial
[3];
6618 count
= LLVMBuildAnd(builder
, count
,
6619 LLVMConstInt(ctx
->i32
, 0x7f, 0), "");
6620 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
6621 ac_get_thread_id(&ctx
->ac
), count
, "");
6622 lp_build_if(&if_state
, &ctx
->gallivm
, ena
);
6625 /* Derive arguments for the next part from outputs of the
6628 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
6630 LLVMTypeRef param_type
;
6632 unsigned param_size
;
6633 LLVMValueRef arg
= NULL
;
6635 param
= LLVMGetParam(parts
[part
], param_idx
);
6636 param_type
= LLVMTypeOf(param
);
6637 param_size
= ac_get_type_size(param_type
) / 4;
6638 is_sgpr
= ac_is_sgpr_param(param
);
6641 ac_add_function_attr(ctx
->ac
.context
, parts
[part
],
6642 param_idx
+ 1, AC_FUNC_ATTR_INREG
);
6643 } else if (out_idx
< num_out_sgpr
) {
6644 /* Skip returned SGPRs the current part doesn't
6645 * declare on the input. */
6646 out_idx
= num_out_sgpr
;
6649 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
6651 if (param_size
== 1)
6654 arg
= ac_build_gather_values(&ctx
->ac
, &out
[out_idx
], param_size
);
6656 if (LLVMTypeOf(arg
) != param_type
) {
6657 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6658 if (LLVMGetPointerAddressSpace(param_type
) ==
6659 AC_ADDR_SPACE_CONST_32BIT
) {
6660 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
6661 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6663 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
6664 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6667 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
6671 in
[param_idx
] = arg
;
6672 out_idx
+= param_size
;
6675 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
6677 if (is_merged_shader(ctx
) &&
6678 part
+ 1 == next_shader_first_part
) {
6679 lp_build_endif(&if_state
);
6681 /* The second half of the merged shader should use
6682 * the inputs from the toplevel (wrapper) function,
6683 * not the return value from the last call.
6685 * That's because the last call was executed condi-
6686 * tionally, so we can't consume it in the main
6689 memcpy(out
, initial
, sizeof(initial
));
6690 num_out
= initial_num_out
;
6691 num_out_sgpr
= initial_num_out_sgpr
;
6695 /* Extract the returned GPRs. */
6696 ret_type
= LLVMTypeOf(ret
);
6700 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
6701 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
6703 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
6705 for (unsigned i
= 0; i
< ret_size
; ++i
) {
6707 LLVMBuildExtractValue(builder
, ret
, i
, "");
6709 assert(num_out
< ARRAY_SIZE(out
));
6710 out
[num_out
++] = val
;
6712 if (LLVMTypeOf(val
) == ctx
->i32
) {
6713 assert(num_out_sgpr
+ 1 == num_out
);
6714 num_out_sgpr
= num_out
;
6720 LLVMBuildRetVoid(builder
);
6723 static bool si_should_optimize_less(struct ac_llvm_compiler
*compiler
,
6724 struct si_shader_selector
*sel
)
6726 if (!compiler
->low_opt_passes
)
6729 /* Assume a slow CPU. */
6730 assert(!sel
->screen
->info
.has_dedicated_vram
&&
6731 sel
->screen
->info
.chip_class
<= VI
);
6733 /* For a crazy dEQP test containing 2597 memory opcodes, mostly
6735 return sel
->type
== PIPE_SHADER_COMPUTE
&&
6736 sel
->info
.num_memory_instructions
> 1000;
6739 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
6740 struct ac_llvm_compiler
*compiler
,
6741 struct si_shader
*shader
,
6742 struct pipe_debug_callback
*debug
)
6744 struct si_shader_selector
*sel
= shader
->selector
;
6745 struct si_shader_context ctx
;
6748 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6749 * conversion fails. */
6750 if (si_can_dump_shader(sscreen
, sel
->info
.processor
) &&
6751 !(sscreen
->debug_flags
& DBG(NO_TGSI
))) {
6753 tgsi_dump(sel
->tokens
, 0);
6755 nir_print_shader(sel
->nir
, stderr
);
6756 si_dump_streamout(&sel
->so
);
6759 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
6760 si_llvm_context_set_tgsi(&ctx
, shader
);
6762 memset(shader
->info
.vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6763 sizeof(shader
->info
.vs_output_param_offset
));
6765 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
6767 if (!si_compile_tgsi_main(&ctx
)) {
6768 si_llvm_dispose(&ctx
);
6772 if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
6773 LLVMValueRef parts
[2];
6774 bool need_prolog
= sel
->vs_needs_prolog
;
6776 parts
[1] = ctx
.main_fn
;
6779 union si_shader_part_key prolog_key
;
6780 si_get_vs_prolog_key(&sel
->info
,
6781 shader
->info
.num_input_sgprs
,
6782 &shader
->key
.part
.vs
.prolog
,
6783 shader
, &prolog_key
);
6784 si_build_vs_prolog_function(&ctx
, &prolog_key
);
6785 parts
[0] = ctx
.main_fn
;
6788 si_build_wrapper_function(&ctx
, parts
+ !need_prolog
,
6789 1 + need_prolog
, need_prolog
, 0);
6790 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
6791 if (sscreen
->info
.chip_class
>= GFX9
) {
6792 struct si_shader_selector
*ls
= shader
->key
.part
.tcs
.ls
;
6793 LLVMValueRef parts
[4];
6794 bool vs_needs_prolog
=
6795 si_vs_needs_prolog(ls
, &shader
->key
.part
.tcs
.ls_prolog
);
6798 parts
[2] = ctx
.main_fn
;
6801 union si_shader_part_key tcs_epilog_key
;
6802 memset(&tcs_epilog_key
, 0, sizeof(tcs_epilog_key
));
6803 tcs_epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6804 si_build_tcs_epilog_function(&ctx
, &tcs_epilog_key
);
6805 parts
[3] = ctx
.main_fn
;
6807 /* VS as LS main part */
6808 struct si_shader shader_ls
= {};
6809 shader_ls
.selector
= ls
;
6810 shader_ls
.key
.as_ls
= 1;
6811 shader_ls
.key
.mono
= shader
->key
.mono
;
6812 shader_ls
.key
.opt
= shader
->key
.opt
;
6813 shader_ls
.is_monolithic
= true;
6814 si_llvm_context_set_tgsi(&ctx
, &shader_ls
);
6816 if (!si_compile_tgsi_main(&ctx
)) {
6817 si_llvm_dispose(&ctx
);
6820 shader
->info
.uses_instanceid
|= ls
->info
.uses_instanceid
;
6821 parts
[1] = ctx
.main_fn
;
6824 if (vs_needs_prolog
) {
6825 union si_shader_part_key vs_prolog_key
;
6826 si_get_vs_prolog_key(&ls
->info
,
6827 shader_ls
.info
.num_input_sgprs
,
6828 &shader
->key
.part
.tcs
.ls_prolog
,
6829 shader
, &vs_prolog_key
);
6830 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6831 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6832 parts
[0] = ctx
.main_fn
;
6835 /* Reset the shader context. */
6836 ctx
.shader
= shader
;
6837 ctx
.type
= PIPE_SHADER_TESS_CTRL
;
6839 si_build_wrapper_function(&ctx
,
6840 parts
+ !vs_needs_prolog
,
6841 4 - !vs_needs_prolog
, vs_needs_prolog
,
6842 vs_needs_prolog
? 2 : 1);
6844 LLVMValueRef parts
[2];
6845 union si_shader_part_key epilog_key
;
6847 parts
[0] = ctx
.main_fn
;
6849 memset(&epilog_key
, 0, sizeof(epilog_key
));
6850 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6851 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
6852 parts
[1] = ctx
.main_fn
;
6854 si_build_wrapper_function(&ctx
, parts
, 2, 0, 0);
6856 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
6857 if (ctx
.screen
->info
.chip_class
>= GFX9
) {
6858 struct si_shader_selector
*es
= shader
->key
.part
.gs
.es
;
6859 LLVMValueRef es_prolog
= NULL
;
6860 LLVMValueRef es_main
= NULL
;
6861 LLVMValueRef gs_prolog
= NULL
;
6862 LLVMValueRef gs_main
= ctx
.main_fn
;
6865 union si_shader_part_key gs_prolog_key
;
6866 memset(&gs_prolog_key
, 0, sizeof(gs_prolog_key
));
6867 gs_prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6868 gs_prolog_key
.gs_prolog
.is_monolithic
= true;
6869 si_build_gs_prolog_function(&ctx
, &gs_prolog_key
);
6870 gs_prolog
= ctx
.main_fn
;
6873 struct si_shader shader_es
= {};
6874 shader_es
.selector
= es
;
6875 shader_es
.key
.as_es
= 1;
6876 shader_es
.key
.mono
= shader
->key
.mono
;
6877 shader_es
.key
.opt
= shader
->key
.opt
;
6878 shader_es
.is_monolithic
= true;
6879 si_llvm_context_set_tgsi(&ctx
, &shader_es
);
6881 if (!si_compile_tgsi_main(&ctx
)) {
6882 si_llvm_dispose(&ctx
);
6885 shader
->info
.uses_instanceid
|= es
->info
.uses_instanceid
;
6886 es_main
= ctx
.main_fn
;
6889 if (es
->vs_needs_prolog
) {
6890 union si_shader_part_key vs_prolog_key
;
6891 si_get_vs_prolog_key(&es
->info
,
6892 shader_es
.info
.num_input_sgprs
,
6893 &shader
->key
.part
.gs
.vs_prolog
,
6894 shader
, &vs_prolog_key
);
6895 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6896 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6897 es_prolog
= ctx
.main_fn
;
6900 /* Reset the shader context. */
6901 ctx
.shader
= shader
;
6902 ctx
.type
= PIPE_SHADER_GEOMETRY
;
6904 /* Prepare the array of shader parts. */
6905 LLVMValueRef parts
[4];
6906 unsigned num_parts
= 0, main_part
, next_first_part
;
6909 parts
[num_parts
++] = es_prolog
;
6911 parts
[main_part
= num_parts
++] = es_main
;
6912 parts
[next_first_part
= num_parts
++] = gs_prolog
;
6913 parts
[num_parts
++] = gs_main
;
6915 si_build_wrapper_function(&ctx
, parts
, num_parts
,
6916 main_part
, next_first_part
);
6918 LLVMValueRef parts
[2];
6919 union si_shader_part_key prolog_key
;
6921 parts
[1] = ctx
.main_fn
;
6923 memset(&prolog_key
, 0, sizeof(prolog_key
));
6924 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6925 si_build_gs_prolog_function(&ctx
, &prolog_key
);
6926 parts
[0] = ctx
.main_fn
;
6928 si_build_wrapper_function(&ctx
, parts
, 2, 1, 0);
6930 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6931 LLVMValueRef parts
[3];
6932 union si_shader_part_key prolog_key
;
6933 union si_shader_part_key epilog_key
;
6936 si_get_ps_prolog_key(shader
, &prolog_key
, false);
6937 need_prolog
= si_need_ps_prolog(&prolog_key
);
6939 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
6942 si_build_ps_prolog_function(&ctx
, &prolog_key
);
6943 parts
[0] = ctx
.main_fn
;
6946 si_get_ps_epilog_key(shader
, &epilog_key
);
6947 si_build_ps_epilog_function(&ctx
, &epilog_key
);
6948 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
6950 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2,
6951 need_prolog
? 1 : 0, 0);
6954 si_llvm_optimize_module(&ctx
);
6956 /* Post-optimization transformations and analysis. */
6957 si_optimize_vs_outputs(&ctx
);
6959 if ((debug
&& debug
->debug_message
) ||
6960 si_can_dump_shader(sscreen
, ctx
.type
)) {
6961 ctx
.shader
->config
.private_mem_vgprs
=
6962 ac_count_scratch_private_memory(ctx
.main_fn
);
6965 /* Make sure the input is a pointer and not integer followed by inttoptr. */
6966 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx
.main_fn
, 0))) ==
6967 LLVMPointerTypeKind
);
6969 /* Compile to bytecode. */
6970 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, compiler
,
6971 ctx
.ac
.module
, debug
, ctx
.type
,
6972 si_get_shader_name(shader
, ctx
.type
),
6973 si_should_optimize_less(compiler
, shader
->selector
));
6974 si_llvm_dispose(&ctx
);
6976 fprintf(stderr
, "LLVM failed to compile shader\n");
6980 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
6981 * LLVM 3.9svn has this bug.
6983 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
6984 unsigned wave_size
= 64;
6985 unsigned max_vgprs
= 256;
6986 unsigned max_sgprs
= sscreen
->info
.chip_class
>= VI
? 800 : 512;
6987 unsigned max_sgprs_per_wave
= 128;
6988 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
6989 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
6990 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
6992 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
6993 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
6995 if (shader
->config
.num_sgprs
> max_sgprs
||
6996 shader
->config
.num_vgprs
> max_vgprs
) {
6997 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
6998 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
6999 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7000 max_sgprs
, max_vgprs
);
7002 /* Just terminate the process, because dependent
7003 * shaders can hang due to bad input data, but use
7004 * the env var to allow shader-db to work.
7006 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7011 /* Add the scratch offset to input SGPRs. */
7012 if (shader
->config
.scratch_bytes_per_wave
&& !is_merged_shader(&ctx
))
7013 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7015 /* Calculate the number of fragment input VGPRs. */
7016 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7017 shader
->info
.num_input_vgprs
= 0;
7018 shader
->info
.face_vgpr_index
= -1;
7019 shader
->info
.ancillary_vgpr_index
= -1;
7021 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7022 shader
->info
.num_input_vgprs
+= 2;
7023 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7024 shader
->info
.num_input_vgprs
+= 2;
7025 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7026 shader
->info
.num_input_vgprs
+= 2;
7027 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7028 shader
->info
.num_input_vgprs
+= 3;
7029 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7030 shader
->info
.num_input_vgprs
+= 2;
7031 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7032 shader
->info
.num_input_vgprs
+= 2;
7033 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7034 shader
->info
.num_input_vgprs
+= 2;
7035 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7036 shader
->info
.num_input_vgprs
+= 1;
7037 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7038 shader
->info
.num_input_vgprs
+= 1;
7039 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7040 shader
->info
.num_input_vgprs
+= 1;
7041 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7042 shader
->info
.num_input_vgprs
+= 1;
7043 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7044 shader
->info
.num_input_vgprs
+= 1;
7045 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7046 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7047 shader
->info
.num_input_vgprs
+= 1;
7049 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
)) {
7050 shader
->info
.ancillary_vgpr_index
= shader
->info
.num_input_vgprs
;
7051 shader
->info
.num_input_vgprs
+= 1;
7053 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7054 shader
->info
.num_input_vgprs
+= 1;
7055 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7056 shader
->info
.num_input_vgprs
+= 1;
7059 si_calculate_max_simd_waves(shader
);
7060 si_shader_dump_stats_for_shader_db(shader
, debug
);
7065 * Create, compile and return a shader part (prolog or epilog).
7067 * \param sscreen screen
7068 * \param list list of shader parts of the same category
7069 * \param type shader type
7070 * \param key shader part key
7071 * \param prolog whether the part being requested is a prolog
7072 * \param tm LLVM target machine
7073 * \param debug debug callback
7074 * \param build the callback responsible for building the main function
7075 * \return non-NULL on success
7077 static struct si_shader_part
*
7078 si_get_shader_part(struct si_screen
*sscreen
,
7079 struct si_shader_part
**list
,
7080 enum pipe_shader_type type
,
7082 union si_shader_part_key
*key
,
7083 struct ac_llvm_compiler
*compiler
,
7084 struct pipe_debug_callback
*debug
,
7085 void (*build
)(struct si_shader_context
*,
7086 union si_shader_part_key
*),
7089 struct si_shader_part
*result
;
7091 mtx_lock(&sscreen
->shader_parts_mutex
);
7093 /* Find existing. */
7094 for (result
= *list
; result
; result
= result
->next
) {
7095 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7096 mtx_unlock(&sscreen
->shader_parts_mutex
);
7101 /* Compile a new one. */
7102 result
= CALLOC_STRUCT(si_shader_part
);
7105 struct si_shader shader
= {};
7106 struct si_shader_context ctx
;
7108 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
7109 ctx
.shader
= &shader
;
7113 case PIPE_SHADER_VERTEX
:
7114 shader
.key
.as_ls
= key
->vs_prolog
.as_ls
;
7115 shader
.key
.as_es
= key
->vs_prolog
.as_es
;
7117 case PIPE_SHADER_TESS_CTRL
:
7119 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7121 case PIPE_SHADER_GEOMETRY
:
7124 case PIPE_SHADER_FRAGMENT
:
7126 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7128 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7131 unreachable("bad shader part");
7137 si_llvm_optimize_module(&ctx
);
7139 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, compiler
,
7140 ctx
.ac
.module
, debug
, ctx
.type
, name
, false)) {
7146 result
->next
= *list
;
7150 si_llvm_dispose(&ctx
);
7151 mtx_unlock(&sscreen
->shader_parts_mutex
);
7155 static LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
7157 LLVMValueRef ptr
[2], list
;
7158 bool merged_shader
= is_merged_shader(ctx
);
7160 if (HAVE_32BIT_POINTERS
) {
7161 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7162 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
[0],
7163 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
7167 /* Get the pointer to rw buffers. */
7168 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7169 ptr
[1] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
+ 1);
7170 list
= ac_build_gather_values(&ctx
->ac
, ptr
, 2);
7171 list
= LLVMBuildBitCast(ctx
->ac
.builder
, list
, ctx
->i64
, "");
7172 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, list
,
7173 ac_array_in_const_addr_space(ctx
->v4i32
), "");
7178 * Build the vertex shader prolog function.
7180 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7181 * All inputs are returned unmodified. The vertex load indices are
7182 * stored after them, which will be used by the API VS for fetching inputs.
7184 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7189 * (VertexID + BaseVertex),
7190 * (InstanceID + StartInstance),
7191 * (InstanceID / 2 + StartInstance)
7193 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7194 union si_shader_part_key
*key
)
7196 struct si_function_info fninfo
;
7197 LLVMTypeRef
*returns
;
7198 LLVMValueRef ret
, func
;
7200 unsigned first_vs_vgpr
= key
->vs_prolog
.num_merged_next_stage_vgprs
;
7201 unsigned num_input_vgprs
= key
->vs_prolog
.num_merged_next_stage_vgprs
+ 4;
7202 LLVMValueRef input_vgprs
[9];
7203 unsigned num_all_input_regs
= key
->vs_prolog
.num_input_sgprs
+
7205 unsigned user_sgpr_base
= key
->vs_prolog
.num_merged_next_stage_vgprs
? 8 : 0;
7207 si_init_function_info(&fninfo
);
7209 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7210 returns
= alloca((num_all_input_regs
+ key
->vs_prolog
.last_input
+ 1) *
7211 sizeof(LLVMTypeRef
));
7214 /* Declare input and output SGPRs. */
7215 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7216 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7217 returns
[num_returns
++] = ctx
->i32
;
7220 /* Preloaded VGPRs (outputs must be floats) */
7221 for (i
= 0; i
< num_input_vgprs
; i
++) {
7222 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &input_vgprs
[i
]);
7223 returns
[num_returns
++] = ctx
->f32
;
7226 /* Vertex load indices. */
7227 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7228 returns
[num_returns
++] = ctx
->f32
;
7230 /* Create the function. */
7231 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, &fninfo
, 0);
7232 func
= ctx
->main_fn
;
7234 if (key
->vs_prolog
.num_merged_next_stage_vgprs
) {
7235 if (!key
->vs_prolog
.is_monolithic
)
7236 si_init_exec_from_input(ctx
, 3, 0);
7238 if (key
->vs_prolog
.as_ls
&&
7239 ctx
->screen
->has_ls_vgpr_init_bug
) {
7240 /* If there are no HS threads, SPI loads the LS VGPRs
7241 * starting at VGPR 0. Shift them back to where they
7244 LLVMValueRef has_hs_threads
=
7245 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
7246 si_unpack_param(ctx
, 3, 8, 8),
7249 for (i
= 4; i
> 0; --i
) {
7250 input_vgprs
[i
+ 1] =
7251 LLVMBuildSelect(ctx
->ac
.builder
, has_hs_threads
,
7253 input_vgprs
[i
- 1], "");
7258 ctx
->abi
.vertex_id
= input_vgprs
[first_vs_vgpr
];
7259 ctx
->abi
.instance_id
= input_vgprs
[first_vs_vgpr
+ (key
->vs_prolog
.as_ls
? 2 : 1)];
7261 /* Copy inputs to outputs. This should be no-op, as the registers match,
7262 * but it will prevent the compiler from overwriting them unintentionally.
7264 ret
= ctx
->return_value
;
7265 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7266 LLVMValueRef p
= LLVMGetParam(func
, i
);
7267 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7269 for (i
= 0; i
< num_input_vgprs
; i
++) {
7270 LLVMValueRef p
= input_vgprs
[i
];
7271 p
= ac_to_float(&ctx
->ac
, p
);
7272 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
,
7273 key
->vs_prolog
.num_input_sgprs
+ i
, "");
7276 /* Compute vertex load indices from instance divisors. */
7277 LLVMValueRef instance_divisor_constbuf
= NULL
;
7279 if (key
->vs_prolog
.states
.instance_divisor_is_fetched
) {
7280 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7281 LLVMValueRef buf_index
=
7282 LLVMConstInt(ctx
->i32
, SI_VS_CONST_INSTANCE_DIVISORS
, 0);
7283 instance_divisor_constbuf
=
7284 ac_build_load_to_sgpr(&ctx
->ac
, list
, buf_index
);
7287 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7288 bool divisor_is_one
=
7289 key
->vs_prolog
.states
.instance_divisor_is_one
& (1u << i
);
7290 bool divisor_is_fetched
=
7291 key
->vs_prolog
.states
.instance_divisor_is_fetched
& (1u << i
);
7292 LLVMValueRef index
= NULL
;
7294 if (divisor_is_one
) {
7295 index
= ctx
->abi
.instance_id
;
7296 } else if (divisor_is_fetched
) {
7297 LLVMValueRef udiv_factors
[4];
7299 for (unsigned j
= 0; j
< 4; j
++) {
7301 buffer_load_const(ctx
, instance_divisor_constbuf
,
7302 LLVMConstInt(ctx
->i32
, i
*16 + j
*4, 0));
7303 udiv_factors
[j
] = ac_to_integer(&ctx
->ac
, udiv_factors
[j
]);
7305 /* The faster NUW version doesn't work when InstanceID == UINT_MAX.
7306 * Such InstanceID might not be achievable in a reasonable time though.
7308 index
= ac_build_fast_udiv_nuw(&ctx
->ac
, ctx
->abi
.instance_id
,
7309 udiv_factors
[0], udiv_factors
[1],
7310 udiv_factors
[2], udiv_factors
[3]);
7313 if (divisor_is_one
|| divisor_is_fetched
) {
7314 /* Add StartInstance. */
7315 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
7316 LLVMGetParam(ctx
->main_fn
, user_sgpr_base
+
7317 SI_SGPR_START_INSTANCE
), "");
7319 /* VertexID + BaseVertex */
7320 index
= LLVMBuildAdd(ctx
->ac
.builder
,
7322 LLVMGetParam(func
, user_sgpr_base
+
7323 SI_SGPR_BASE_VERTEX
), "");
7326 index
= ac_to_float(&ctx
->ac
, index
);
7327 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, index
,
7328 fninfo
.num_params
+ i
, "");
7331 si_llvm_build_ret(ctx
, ret
);
7334 static bool si_get_vs_prolog(struct si_screen
*sscreen
,
7335 struct ac_llvm_compiler
*compiler
,
7336 struct si_shader
*shader
,
7337 struct pipe_debug_callback
*debug
,
7338 struct si_shader
*main_part
,
7339 const struct si_vs_prolog_bits
*key
)
7341 struct si_shader_selector
*vs
= main_part
->selector
;
7343 if (!si_vs_needs_prolog(vs
, key
))
7346 /* Get the prolog. */
7347 union si_shader_part_key prolog_key
;
7348 si_get_vs_prolog_key(&vs
->info
, main_part
->info
.num_input_sgprs
,
7349 key
, shader
, &prolog_key
);
7352 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7353 PIPE_SHADER_VERTEX
, true, &prolog_key
, compiler
,
7354 debug
, si_build_vs_prolog_function
,
7355 "Vertex Shader Prolog");
7356 return shader
->prolog
!= NULL
;
7360 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7362 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7363 struct ac_llvm_compiler
*compiler
,
7364 struct si_shader
*shader
,
7365 struct pipe_debug_callback
*debug
)
7367 return si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, shader
,
7368 &shader
->key
.part
.vs
.prolog
);
7372 * Compile the TCS epilog function. This writes tesselation factors to memory
7373 * based on the output primitive type of the tesselator (determined by TES).
7375 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7376 union si_shader_part_key
*key
)
7378 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7379 struct si_function_info fninfo
;
7382 si_init_function_info(&fninfo
);
7384 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
7385 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7386 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7387 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7388 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* wave info */
7389 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7390 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7391 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7392 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7393 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7394 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7395 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7396 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7397 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7398 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7399 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7400 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7401 if (!HAVE_32BIT_POINTERS
)
7402 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7403 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7404 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7405 ctx
->param_tcs_out_lds_layout
= 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 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7412 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7413 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7414 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7415 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7416 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7419 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7420 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7421 unsigned tess_factors_idx
=
7422 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* patch index within the wave (REL_PATCH_ID) */
7423 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* invocation ID within the patch */
7424 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* LDS offset where tess factors should be loaded from */
7426 for (unsigned i
= 0; i
< 6; i
++)
7427 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* tess factors */
7429 /* Create the function. */
7430 si_create_function(ctx
, "tcs_epilog", NULL
, 0, &fninfo
,
7431 ctx
->screen
->info
.chip_class
>= CIK
? 128 : 64);
7432 ac_declare_lds_as_pointer(&ctx
->ac
);
7433 func
= ctx
->main_fn
;
7435 LLVMValueRef invoc0_tess_factors
[6];
7436 for (unsigned i
= 0; i
< 6; i
++)
7437 invoc0_tess_factors
[i
] = LLVMGetParam(func
, tess_factors_idx
+ 3 + i
);
7439 si_write_tess_factors(bld_base
,
7440 LLVMGetParam(func
, tess_factors_idx
),
7441 LLVMGetParam(func
, tess_factors_idx
+ 1),
7442 LLVMGetParam(func
, tess_factors_idx
+ 2),
7443 invoc0_tess_factors
, invoc0_tess_factors
+ 4);
7445 LLVMBuildRetVoid(ctx
->ac
.builder
);
7449 * Select and compile (or reuse) TCS parts (epilog).
7451 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7452 struct ac_llvm_compiler
*compiler
,
7453 struct si_shader
*shader
,
7454 struct pipe_debug_callback
*debug
)
7456 if (sscreen
->info
.chip_class
>= GFX9
) {
7457 struct si_shader
*ls_main_part
=
7458 shader
->key
.part
.tcs
.ls
->main_shader_part_ls
;
7460 if (!si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, ls_main_part
,
7461 &shader
->key
.part
.tcs
.ls_prolog
))
7464 shader
->previous_stage
= ls_main_part
;
7467 /* Get the epilog. */
7468 union si_shader_part_key epilog_key
;
7469 memset(&epilog_key
, 0, sizeof(epilog_key
));
7470 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7472 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7473 PIPE_SHADER_TESS_CTRL
, false,
7474 &epilog_key
, compiler
, debug
,
7475 si_build_tcs_epilog_function
,
7476 "Tessellation Control Shader Epilog");
7477 return shader
->epilog
!= NULL
;
7481 * Select and compile (or reuse) GS parts (prolog).
7483 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7484 struct ac_llvm_compiler
*compiler
,
7485 struct si_shader
*shader
,
7486 struct pipe_debug_callback
*debug
)
7488 if (sscreen
->info
.chip_class
>= GFX9
) {
7489 struct si_shader
*es_main_part
=
7490 shader
->key
.part
.gs
.es
->main_shader_part_es
;
7492 if (shader
->key
.part
.gs
.es
->type
== PIPE_SHADER_VERTEX
&&
7493 !si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, es_main_part
,
7494 &shader
->key
.part
.gs
.vs_prolog
))
7497 shader
->previous_stage
= es_main_part
;
7500 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7503 union si_shader_part_key prolog_key
;
7504 memset(&prolog_key
, 0, sizeof(prolog_key
));
7505 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7507 shader
->prolog2
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7508 PIPE_SHADER_GEOMETRY
, true,
7509 &prolog_key
, compiler
, debug
,
7510 si_build_gs_prolog_function
,
7511 "Geometry Shader Prolog");
7512 return shader
->prolog2
!= NULL
;
7516 * Build the pixel shader prolog function. This handles:
7517 * - two-side color selection and interpolation
7518 * - overriding interpolation parameters for the API PS
7519 * - polygon stippling
7521 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7522 * overriden by other states. (e.g. per-sample interpolation)
7523 * Interpolated colors are stored after the preloaded VGPRs.
7525 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7526 union si_shader_part_key
*key
)
7528 struct si_function_info fninfo
;
7529 LLVMValueRef ret
, func
;
7530 int num_returns
, i
, num_color_channels
;
7532 assert(si_need_ps_prolog(key
));
7534 si_init_function_info(&fninfo
);
7536 /* Declare inputs. */
7537 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
7538 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7540 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
7541 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7543 /* Declare outputs (same as inputs + add colors if needed) */
7544 num_returns
= fninfo
.num_params
;
7545 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
7546 for (i
= 0; i
< num_color_channels
; i
++)
7547 fninfo
.types
[num_returns
++] = ctx
->f32
;
7549 /* Create the function. */
7550 si_create_function(ctx
, "ps_prolog", fninfo
.types
, num_returns
,
7552 func
= ctx
->main_fn
;
7554 /* Copy inputs to outputs. This should be no-op, as the registers match,
7555 * but it will prevent the compiler from overwriting them unintentionally.
7557 ret
= ctx
->return_value
;
7558 for (i
= 0; i
< fninfo
.num_params
; i
++) {
7559 LLVMValueRef p
= LLVMGetParam(func
, i
);
7560 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7563 /* Polygon stippling. */
7564 if (key
->ps_prolog
.states
.poly_stipple
) {
7565 /* POS_FIXED_PT is always last. */
7566 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
7567 key
->ps_prolog
.num_input_vgprs
- 1;
7568 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7570 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
7573 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
7574 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7575 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7576 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
7578 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7579 * The hw doesn't compute CENTROID if the whole wave only
7580 * contains fully-covered quads.
7582 * PRIM_MASK is after user SGPRs.
7584 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7585 bc_optimize
= LLVMBuildLShr(ctx
->ac
.builder
, bc_optimize
,
7586 LLVMConstInt(ctx
->i32
, 31, 0), "");
7587 bc_optimize
= LLVMBuildTrunc(ctx
->ac
.builder
, bc_optimize
,
7590 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
7591 /* Read PERSP_CENTER. */
7592 for (i
= 0; i
< 2; i
++)
7593 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7594 /* Read PERSP_CENTROID. */
7595 for (i
= 0; i
< 2; i
++)
7596 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
7597 /* Select PERSP_CENTROID. */
7598 for (i
= 0; i
< 2; i
++) {
7599 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7600 center
[i
], centroid
[i
], "");
7601 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7602 tmp
, base
+ 4 + i
, "");
7605 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7606 /* Read LINEAR_CENTER. */
7607 for (i
= 0; i
< 2; i
++)
7608 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7609 /* Read LINEAR_CENTROID. */
7610 for (i
= 0; i
< 2; i
++)
7611 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
7612 /* Select LINEAR_CENTROID. */
7613 for (i
= 0; i
< 2; i
++) {
7614 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7615 center
[i
], centroid
[i
], "");
7616 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7617 tmp
, base
+ 10 + i
, "");
7622 /* Force per-sample interpolation. */
7623 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
7624 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7625 LLVMValueRef persp_sample
[2];
7627 /* Read PERSP_SAMPLE. */
7628 for (i
= 0; i
< 2; i
++)
7629 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
7630 /* Overwrite PERSP_CENTER. */
7631 for (i
= 0; i
< 2; i
++)
7632 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7633 persp_sample
[i
], base
+ 2 + i
, "");
7634 /* Overwrite PERSP_CENTROID. */
7635 for (i
= 0; i
< 2; i
++)
7636 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7637 persp_sample
[i
], base
+ 4 + i
, "");
7639 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
7640 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7641 LLVMValueRef linear_sample
[2];
7643 /* Read LINEAR_SAMPLE. */
7644 for (i
= 0; i
< 2; i
++)
7645 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
7646 /* Overwrite LINEAR_CENTER. */
7647 for (i
= 0; i
< 2; i
++)
7648 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7649 linear_sample
[i
], base
+ 8 + i
, "");
7650 /* Overwrite LINEAR_CENTROID. */
7651 for (i
= 0; i
< 2; i
++)
7652 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7653 linear_sample
[i
], base
+ 10 + i
, "");
7656 /* Force center interpolation. */
7657 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
7658 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7659 LLVMValueRef persp_center
[2];
7661 /* Read PERSP_CENTER. */
7662 for (i
= 0; i
< 2; i
++)
7663 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7664 /* Overwrite PERSP_SAMPLE. */
7665 for (i
= 0; i
< 2; i
++)
7666 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7667 persp_center
[i
], base
+ i
, "");
7668 /* Overwrite PERSP_CENTROID. */
7669 for (i
= 0; i
< 2; i
++)
7670 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7671 persp_center
[i
], base
+ 4 + i
, "");
7673 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
7674 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7675 LLVMValueRef linear_center
[2];
7677 /* Read LINEAR_CENTER. */
7678 for (i
= 0; i
< 2; i
++)
7679 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7680 /* Overwrite LINEAR_SAMPLE. */
7681 for (i
= 0; i
< 2; i
++)
7682 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7683 linear_center
[i
], base
+ 6 + i
, "");
7684 /* Overwrite LINEAR_CENTROID. */
7685 for (i
= 0; i
< 2; i
++)
7686 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7687 linear_center
[i
], base
+ 10 + i
, "");
7690 /* Interpolate colors. */
7691 unsigned color_out_idx
= 0;
7692 for (i
= 0; i
< 2; i
++) {
7693 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
7694 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7695 key
->ps_prolog
.face_vgpr_index
;
7696 LLVMValueRef interp
[2], color
[4];
7697 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
7702 /* If the interpolation qualifier is not CONSTANT (-1). */
7703 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
7704 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7705 key
->ps_prolog
.color_interp_vgpr_index
[i
];
7707 /* Get the (i,j) updated by bc_optimize handling. */
7708 interp
[0] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7710 interp
[1] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7711 interp_vgpr
+ 1, "");
7712 interp_ij
= ac_build_gather_values(&ctx
->ac
, interp
, 2);
7715 /* Use the absolute location of the input. */
7716 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7718 if (key
->ps_prolog
.states
.color_two_side
) {
7719 face
= LLVMGetParam(func
, face_vgpr
);
7720 face
= ac_to_integer(&ctx
->ac
, face
);
7723 interp_fs_input(ctx
,
7724 key
->ps_prolog
.color_attr_index
[i
],
7725 TGSI_SEMANTIC_COLOR
, i
,
7726 key
->ps_prolog
.num_interp_inputs
,
7727 key
->ps_prolog
.colors_read
, interp_ij
,
7728 prim_mask
, face
, color
);
7731 unsigned chan
= u_bit_scan(&writemask
);
7732 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, color
[chan
],
7733 fninfo
.num_params
+ color_out_idx
++, "");
7737 /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
7740 * "When per-sample shading is active due to the use of a fragment
7741 * input qualified by sample or due to the use of the gl_SampleID
7742 * or gl_SamplePosition variables, only the bit for the current
7743 * sample is set in gl_SampleMaskIn. When state specifies multiple
7744 * fragment shader invocations for a given fragment, the sample
7745 * mask for any single fragment shader invocation may specify a
7746 * subset of the covered samples for the fragment. In this case,
7747 * the bit corresponding to each covered sample will be set in
7748 * exactly one fragment shader invocation."
7750 * The samplemask loaded by hardware is always the coverage of the
7751 * entire pixel/fragment, so mask bits out based on the sample ID.
7753 if (key
->ps_prolog
.states
.samplemask_log_ps_iter
) {
7754 /* The bit pattern matches that used by fixed function fragment
7756 static const uint16_t ps_iter_masks
[] = {
7757 0xffff, /* not used */
7763 assert(key
->ps_prolog
.states
.samplemask_log_ps_iter
< ARRAY_SIZE(ps_iter_masks
));
7765 uint32_t ps_iter_mask
= ps_iter_masks
[key
->ps_prolog
.states
.samplemask_log_ps_iter
];
7766 unsigned ancillary_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7767 key
->ps_prolog
.ancillary_vgpr_index
;
7768 LLVMValueRef sampleid
= si_unpack_param(ctx
, ancillary_vgpr
, 8, 4);
7769 LLVMValueRef samplemask
= LLVMGetParam(func
, ancillary_vgpr
+ 1);
7771 samplemask
= ac_to_integer(&ctx
->ac
, samplemask
);
7772 samplemask
= LLVMBuildAnd(
7775 LLVMBuildShl(ctx
->ac
.builder
,
7776 LLVMConstInt(ctx
->i32
, ps_iter_mask
, false),
7779 samplemask
= ac_to_float(&ctx
->ac
, samplemask
);
7781 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, samplemask
,
7782 ancillary_vgpr
+ 1, "");
7785 /* Tell LLVM to insert WQM instruction sequence when needed. */
7786 if (key
->ps_prolog
.wqm
) {
7787 LLVMAddTargetDependentFunctionAttr(func
,
7788 "amdgpu-ps-wqm-outputs", "");
7791 si_llvm_build_ret(ctx
, ret
);
7795 * Build the pixel shader epilog function. This handles everything that must be
7796 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7798 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
7799 union si_shader_part_key
*key
)
7801 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7802 struct si_function_info fninfo
;
7803 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
7805 struct si_ps_exports exp
= {};
7807 si_init_function_info(&fninfo
);
7809 /* Declare input SGPRs. */
7810 ctx
->param_rw_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7811 ctx
->param_bindless_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7812 ctx
->param_const_and_shader_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7813 ctx
->param_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7814 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
7816 /* Declare input VGPRs. */
7817 unsigned required_num_params
=
7818 fninfo
.num_sgpr_params
+
7819 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
7820 key
->ps_epilog
.writes_z
+
7821 key
->ps_epilog
.writes_stencil
+
7822 key
->ps_epilog
.writes_samplemask
;
7824 required_num_params
= MAX2(required_num_params
,
7825 fninfo
.num_sgpr_params
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
7827 while (fninfo
.num_params
< required_num_params
)
7828 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7830 /* Create the function. */
7831 si_create_function(ctx
, "ps_epilog", NULL
, 0, &fninfo
, 0);
7832 /* Disable elimination of unused inputs. */
7833 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
7834 "InitialPSInputAddr", 0xffffff);
7836 /* Process colors. */
7837 unsigned vgpr
= fninfo
.num_sgpr_params
;
7838 unsigned colors_written
= key
->ps_epilog
.colors_written
;
7839 int last_color_export
= -1;
7841 /* Find the last color export. */
7842 if (!key
->ps_epilog
.writes_z
&&
7843 !key
->ps_epilog
.writes_stencil
&&
7844 !key
->ps_epilog
.writes_samplemask
) {
7845 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
7847 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7848 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
7849 /* Just set this if any of the colorbuffers are enabled. */
7851 ((1ull << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
7852 last_color_export
= 0;
7854 for (i
= 0; i
< 8; i
++)
7855 if (colors_written
& (1 << i
) &&
7856 (spi_format
>> (i
* 4)) & 0xf)
7857 last_color_export
= i
;
7861 while (colors_written
) {
7862 LLVMValueRef color
[4];
7863 int mrt
= u_bit_scan(&colors_written
);
7865 for (i
= 0; i
< 4; i
++)
7866 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
7868 si_export_mrt_color(bld_base
, color
, mrt
,
7869 fninfo
.num_params
- 1,
7870 mrt
== last_color_export
, &exp
);
7873 /* Process depth, stencil, samplemask. */
7874 if (key
->ps_epilog
.writes_z
)
7875 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7876 if (key
->ps_epilog
.writes_stencil
)
7877 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7878 if (key
->ps_epilog
.writes_samplemask
)
7879 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7881 if (depth
|| stencil
|| samplemask
)
7882 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
7883 else if (last_color_export
== -1)
7884 ac_build_export_null(&ctx
->ac
);
7887 si_emit_ps_exports(ctx
, &exp
);
7890 LLVMBuildRetVoid(ctx
->ac
.builder
);
7894 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7896 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
7897 struct ac_llvm_compiler
*compiler
,
7898 struct si_shader
*shader
,
7899 struct pipe_debug_callback
*debug
)
7901 union si_shader_part_key prolog_key
;
7902 union si_shader_part_key epilog_key
;
7904 /* Get the prolog. */
7905 si_get_ps_prolog_key(shader
, &prolog_key
, true);
7907 /* The prolog is a no-op if these aren't set. */
7908 if (si_need_ps_prolog(&prolog_key
)) {
7910 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
7911 PIPE_SHADER_FRAGMENT
, true,
7912 &prolog_key
, compiler
, debug
,
7913 si_build_ps_prolog_function
,
7914 "Fragment Shader Prolog");
7915 if (!shader
->prolog
)
7919 /* Get the epilog. */
7920 si_get_ps_epilog_key(shader
, &epilog_key
);
7923 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
7924 PIPE_SHADER_FRAGMENT
, false,
7925 &epilog_key
, compiler
, debug
,
7926 si_build_ps_epilog_function
,
7927 "Fragment Shader Epilog");
7928 if (!shader
->epilog
)
7931 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
7932 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
7933 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
7934 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
7937 /* Set up the enable bits for per-sample shading if needed. */
7938 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
7939 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7940 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7941 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
7942 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7943 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
7945 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
7946 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7947 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7948 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
7949 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7950 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
7952 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
7953 (G_0286CC_PERSP_SAMPLE_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_SAMPLE_ENA
;
7956 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7957 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7959 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
7960 (G_0286CC_LINEAR_SAMPLE_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_SAMPLE_ENA
;
7963 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7964 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7967 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
7968 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
7969 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
7970 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7971 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7974 /* At least one pair of interpolation weights must be enabled. */
7975 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
7976 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7977 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7980 /* Samplemask fixup requires the sample ID. */
7981 if (shader
->key
.part
.ps
.prolog
.samplemask_log_ps_iter
) {
7982 shader
->config
.spi_ps_input_ena
|= S_0286CC_ANCILLARY_ENA(1);
7983 assert(G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
));
7986 /* The sample mask input is always enabled, because the API shader always
7987 * passes it through to the epilog. Disable it here if it's unused.
7989 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
7990 !shader
->selector
->info
.reads_samplemask
)
7991 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
7996 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
7999 /* If tessellation is all offchip and on-chip GS isn't used, this
8000 * workaround is not needed.
8004 /* SPI barrier management bug:
8005 * Make sure we have at least 4k of LDS in use to avoid the bug.
8006 * It applies to workgroup sizes of more than one wavefront.
8008 if (sscreen
->info
.family
== CHIP_BONAIRE
||
8009 sscreen
->info
.family
== CHIP_KABINI
||
8010 sscreen
->info
.family
== CHIP_MULLINS
)
8011 *lds_size
= MAX2(*lds_size
, 8);
8014 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8015 struct si_shader
*shader
)
8017 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8019 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8021 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8022 si_get_max_workgroup_size(shader
) > 64) {
8023 si_multiwave_lds_size_workaround(sscreen
,
8024 &shader
->config
.lds_size
);
8028 int si_shader_create(struct si_screen
*sscreen
, struct ac_llvm_compiler
*compiler
,
8029 struct si_shader
*shader
,
8030 struct pipe_debug_callback
*debug
)
8032 struct si_shader_selector
*sel
= shader
->selector
;
8033 struct si_shader
*mainp
= *si_get_main_shader_part(sel
, &shader
->key
);
8036 /* LS, ES, VS are compiled on demand if the main part hasn't been
8037 * compiled for that stage.
8039 * Vertex shaders are compiled on demand when a vertex fetch
8040 * workaround must be applied.
8042 if (shader
->is_monolithic
) {
8043 /* Monolithic shader (compiled as a whole, has many variants,
8044 * may take a long time to compile).
8046 r
= si_compile_tgsi_shader(sscreen
, compiler
, shader
, debug
);
8050 /* The shader consists of several parts:
8052 * - the middle part is the user shader, it has 1 variant only
8053 * and it was compiled during the creation of the shader
8055 * - the prolog part is inserted at the beginning
8056 * - the epilog part is inserted at the end
8058 * The prolog and epilog have many (but simple) variants.
8060 * Starting with gfx9, geometry and tessellation control
8061 * shaders also contain the prolog and user shader parts of
8062 * the previous shader stage.
8068 /* Copy the compiled TGSI shader data over. */
8069 shader
->is_binary_shared
= true;
8070 shader
->binary
= mainp
->binary
;
8071 shader
->config
= mainp
->config
;
8072 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8073 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8074 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8075 shader
->info
.ancillary_vgpr_index
= mainp
->info
.ancillary_vgpr_index
;
8076 memcpy(shader
->info
.vs_output_param_offset
,
8077 mainp
->info
.vs_output_param_offset
,
8078 sizeof(mainp
->info
.vs_output_param_offset
));
8079 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8080 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8081 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8083 /* Select prologs and/or epilogs. */
8084 switch (sel
->type
) {
8085 case PIPE_SHADER_VERTEX
:
8086 if (!si_shader_select_vs_parts(sscreen
, compiler
, shader
, debug
))
8089 case PIPE_SHADER_TESS_CTRL
:
8090 if (!si_shader_select_tcs_parts(sscreen
, compiler
, shader
, debug
))
8093 case PIPE_SHADER_TESS_EVAL
:
8095 case PIPE_SHADER_GEOMETRY
:
8096 if (!si_shader_select_gs_parts(sscreen
, compiler
, shader
, debug
))
8099 case PIPE_SHADER_FRAGMENT
:
8100 if (!si_shader_select_ps_parts(sscreen
, compiler
, shader
, debug
))
8103 /* Make sure we have at least as many VGPRs as there
8104 * are allocated inputs.
8106 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8107 shader
->info
.num_input_vgprs
);
8111 /* Update SGPR and VGPR counts. */
8112 if (shader
->prolog
) {
8113 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8114 shader
->prolog
->config
.num_sgprs
);
8115 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8116 shader
->prolog
->config
.num_vgprs
);
8118 if (shader
->previous_stage
) {
8119 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8120 shader
->previous_stage
->config
.num_sgprs
);
8121 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8122 shader
->previous_stage
->config
.num_vgprs
);
8123 shader
->config
.spilled_sgprs
=
8124 MAX2(shader
->config
.spilled_sgprs
,
8125 shader
->previous_stage
->config
.spilled_sgprs
);
8126 shader
->config
.spilled_vgprs
=
8127 MAX2(shader
->config
.spilled_vgprs
,
8128 shader
->previous_stage
->config
.spilled_vgprs
);
8129 shader
->config
.private_mem_vgprs
=
8130 MAX2(shader
->config
.private_mem_vgprs
,
8131 shader
->previous_stage
->config
.private_mem_vgprs
);
8132 shader
->config
.scratch_bytes_per_wave
=
8133 MAX2(shader
->config
.scratch_bytes_per_wave
,
8134 shader
->previous_stage
->config
.scratch_bytes_per_wave
);
8135 shader
->info
.uses_instanceid
|=
8136 shader
->previous_stage
->info
.uses_instanceid
;
8138 if (shader
->prolog2
) {
8139 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8140 shader
->prolog2
->config
.num_sgprs
);
8141 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8142 shader
->prolog2
->config
.num_vgprs
);
8144 if (shader
->epilog
) {
8145 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8146 shader
->epilog
->config
.num_sgprs
);
8147 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8148 shader
->epilog
->config
.num_vgprs
);
8150 si_calculate_max_simd_waves(shader
);
8153 si_fix_resource_usage(sscreen
, shader
);
8154 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8158 r
= si_shader_binary_upload(sscreen
, shader
);
8160 fprintf(stderr
, "LLVM failed to upload shader\n");
8167 void si_shader_destroy(struct si_shader
*shader
)
8169 if (shader
->scratch_bo
)
8170 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8172 r600_resource_reference(&shader
->bo
, NULL
);
8174 if (!shader
->is_binary_shared
)
8175 ac_shader_binary_clean(&shader
->binary
);
8177 free(shader
->shader_log
);