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 static LLVMValueRef
get_instance_index_for_fetch(
432 struct si_shader_context
*ctx
,
433 unsigned param_start_instance
, LLVMValueRef divisor
)
435 LLVMValueRef result
= ctx
->abi
.instance_id
;
437 /* The division must be done before START_INSTANCE is added. */
438 if (divisor
!= ctx
->i32_1
)
439 result
= LLVMBuildUDiv(ctx
->ac
.builder
, result
, divisor
, "");
441 return LLVMBuildAdd(ctx
->ac
.builder
, result
,
442 LLVMGetParam(ctx
->main_fn
, param_start_instance
), "");
445 /* Bitcast <4 x float> to <2 x double>, extract the component, and convert
447 static LLVMValueRef
extract_double_to_float(struct si_shader_context
*ctx
,
449 unsigned double_index
)
451 LLVMBuilderRef builder
= ctx
->ac
.builder
;
452 LLVMTypeRef f64
= LLVMDoubleTypeInContext(ctx
->ac
.context
);
453 LLVMValueRef dvec2
= LLVMBuildBitCast(builder
, vec4
,
454 LLVMVectorType(f64
, 2), "");
455 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, double_index
, 0);
456 LLVMValueRef value
= LLVMBuildExtractElement(builder
, dvec2
, index
, "");
457 return LLVMBuildFPTrunc(builder
, value
, ctx
->f32
, "");
460 static LLVMValueRef
unpack_sint16(struct si_shader_context
*ctx
,
461 LLVMValueRef i32
, unsigned index
)
466 return LLVMBuildAShr(ctx
->ac
.builder
, i32
,
467 LLVMConstInt(ctx
->i32
, 16, 0), "");
469 return LLVMBuildSExt(ctx
->ac
.builder
,
470 LLVMBuildTrunc(ctx
->ac
.builder
, i32
,
475 void si_llvm_load_input_vs(
476 struct si_shader_context
*ctx
,
477 unsigned input_index
,
480 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
481 unsigned vs_blit_property
= info
->properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
483 if (vs_blit_property
) {
484 LLVMValueRef vertex_id
= ctx
->abi
.vertex_id
;
485 LLVMValueRef sel_x1
= LLVMBuildICmp(ctx
->ac
.builder
,
486 LLVMIntULE
, vertex_id
,
488 /* Use LLVMIntNE, because we have 3 vertices and only
489 * the middle one should use y2.
491 LLVMValueRef sel_y1
= LLVMBuildICmp(ctx
->ac
.builder
,
492 LLVMIntNE
, vertex_id
,
495 if (input_index
== 0) {
497 LLVMValueRef x1y1
= LLVMGetParam(ctx
->main_fn
,
498 ctx
->param_vs_blit_inputs
);
499 LLVMValueRef x2y2
= LLVMGetParam(ctx
->main_fn
,
500 ctx
->param_vs_blit_inputs
+ 1);
502 LLVMValueRef x1
= unpack_sint16(ctx
, x1y1
, 0);
503 LLVMValueRef y1
= unpack_sint16(ctx
, x1y1
, 1);
504 LLVMValueRef x2
= unpack_sint16(ctx
, x2y2
, 0);
505 LLVMValueRef y2
= unpack_sint16(ctx
, x2y2
, 1);
507 LLVMValueRef x
= LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
509 LLVMValueRef y
= LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
512 out
[0] = LLVMBuildSIToFP(ctx
->ac
.builder
, x
, ctx
->f32
, "");
513 out
[1] = LLVMBuildSIToFP(ctx
->ac
.builder
, y
, ctx
->f32
, "");
514 out
[2] = LLVMGetParam(ctx
->main_fn
,
515 ctx
->param_vs_blit_inputs
+ 2);
516 out
[3] = ctx
->ac
.f32_1
;
520 /* Color or texture coordinates: */
521 assert(input_index
== 1);
523 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
524 for (int i
= 0; i
< 4; i
++) {
525 out
[i
] = LLVMGetParam(ctx
->main_fn
,
526 ctx
->param_vs_blit_inputs
+ 3 + i
);
529 assert(vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
);
530 LLVMValueRef x1
= LLVMGetParam(ctx
->main_fn
,
531 ctx
->param_vs_blit_inputs
+ 3);
532 LLVMValueRef y1
= LLVMGetParam(ctx
->main_fn
,
533 ctx
->param_vs_blit_inputs
+ 4);
534 LLVMValueRef x2
= LLVMGetParam(ctx
->main_fn
,
535 ctx
->param_vs_blit_inputs
+ 5);
536 LLVMValueRef y2
= LLVMGetParam(ctx
->main_fn
,
537 ctx
->param_vs_blit_inputs
+ 6);
539 out
[0] = LLVMBuildSelect(ctx
->ac
.builder
, sel_x1
,
541 out
[1] = LLVMBuildSelect(ctx
->ac
.builder
, sel_y1
,
543 out
[2] = LLVMGetParam(ctx
->main_fn
,
544 ctx
->param_vs_blit_inputs
+ 7);
545 out
[3] = LLVMGetParam(ctx
->main_fn
,
546 ctx
->param_vs_blit_inputs
+ 8);
553 unsigned num_fetches
;
554 unsigned fetch_stride
;
555 unsigned num_channels
;
557 LLVMValueRef t_list_ptr
;
558 LLVMValueRef t_offset
;
560 LLVMValueRef vertex_index
;
561 LLVMValueRef input
[3];
563 /* Load the T list */
564 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_vertex_buffers
);
566 t_offset
= LLVMConstInt(ctx
->i32
, input_index
, 0);
568 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
570 vertex_index
= LLVMGetParam(ctx
->main_fn
,
571 ctx
->param_vertex_index0
+
574 fix_fetch
= ctx
->shader
->key
.mono
.vs_fix_fetch
[input_index
];
576 /* Do multiple loads for special formats. */
578 case SI_FIX_FETCH_RGB_64_FLOAT
:
579 num_fetches
= 3; /* 3 2-dword loads */
583 case SI_FIX_FETCH_RGBA_64_FLOAT
:
584 num_fetches
= 2; /* 2 4-dword loads */
588 case SI_FIX_FETCH_RGB_8
:
589 case SI_FIX_FETCH_RGB_8_INT
:
594 case SI_FIX_FETCH_RGB_16
:
595 case SI_FIX_FETCH_RGB_16_INT
:
603 num_channels
= util_last_bit(info
->input_usage_mask
[input_index
]);
606 for (unsigned i
= 0; i
< num_fetches
; i
++) {
607 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, fetch_stride
* i
, 0);
609 input
[i
] = ac_build_buffer_load_format(&ctx
->ac
, t_list
,
610 vertex_index
, voffset
,
611 num_channels
, false, true);
612 input
[i
] = ac_build_expand_to_vec4(&ctx
->ac
, input
[i
], num_channels
);
615 /* Break up the vec4 into individual components */
616 for (chan
= 0; chan
< 4; chan
++) {
617 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->i32
, chan
, 0);
618 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
619 input
[0], llvm_chan
, "");
623 case SI_FIX_FETCH_A2_SNORM
:
624 case SI_FIX_FETCH_A2_SSCALED
:
625 case SI_FIX_FETCH_A2_SINT
: {
626 /* The hardware returns an unsigned value; convert it to a
629 LLVMValueRef tmp
= out
[3];
630 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
632 /* First, recover the sign-extended signed integer value. */
633 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
634 tmp
= LLVMBuildFPToUI(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
636 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
638 /* For the integer-like cases, do a natural sign extension.
640 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
641 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
644 tmp
= LLVMBuildShl(ctx
->ac
.builder
, tmp
,
645 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
646 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
647 tmp
= LLVMBuildAShr(ctx
->ac
.builder
, tmp
, c30
, "");
649 /* Convert back to the right type. */
650 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
652 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
653 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
654 clamp
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealULT
, tmp
, neg_one
, "");
655 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, clamp
, neg_one
, tmp
, "");
656 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
657 tmp
= LLVMBuildSIToFP(ctx
->ac
.builder
, tmp
, ctx
->f32
, "");
663 case SI_FIX_FETCH_RGBA_32_UNORM
:
664 case SI_FIX_FETCH_RGBX_32_UNORM
:
665 for (chan
= 0; chan
< 4; chan
++) {
666 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
667 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
668 out
[chan
], ctx
->f32
, "");
669 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
670 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
672 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
673 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
674 out
[3] = LLVMConstReal(ctx
->f32
, 1);
676 case SI_FIX_FETCH_RGBA_32_SNORM
:
677 case SI_FIX_FETCH_RGBX_32_SNORM
:
678 case SI_FIX_FETCH_RGBA_32_FIXED
:
679 case SI_FIX_FETCH_RGBX_32_FIXED
: {
681 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
682 scale
= 1.0 / 0x10000;
684 scale
= 1.0 / INT_MAX
;
686 for (chan
= 0; chan
< 4; chan
++) {
687 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
688 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
689 out
[chan
], ctx
->f32
, "");
690 out
[chan
] = LLVMBuildFMul(ctx
->ac
.builder
, out
[chan
],
691 LLVMConstReal(ctx
->f32
, scale
), "");
693 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
694 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
695 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
696 out
[3] = LLVMConstReal(ctx
->f32
, 1);
699 case SI_FIX_FETCH_RGBA_32_USCALED
:
700 for (chan
= 0; chan
< 4; chan
++) {
701 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
702 out
[chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
703 out
[chan
], ctx
->f32
, "");
706 case SI_FIX_FETCH_RGBA_32_SSCALED
:
707 for (chan
= 0; chan
< 4; chan
++) {
708 out
[chan
] = ac_to_integer(&ctx
->ac
, out
[chan
]);
709 out
[chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
710 out
[chan
], ctx
->f32
, "");
713 case SI_FIX_FETCH_RG_64_FLOAT
:
714 for (chan
= 0; chan
< 2; chan
++)
715 out
[chan
] = extract_double_to_float(ctx
, input
[0], chan
);
717 out
[2] = LLVMConstReal(ctx
->f32
, 0);
718 out
[3] = LLVMConstReal(ctx
->f32
, 1);
720 case SI_FIX_FETCH_RGB_64_FLOAT
:
721 for (chan
= 0; chan
< 3; chan
++)
722 out
[chan
] = extract_double_to_float(ctx
, input
[chan
], 0);
724 out
[3] = LLVMConstReal(ctx
->f32
, 1);
726 case SI_FIX_FETCH_RGBA_64_FLOAT
:
727 for (chan
= 0; chan
< 4; chan
++) {
728 out
[chan
] = extract_double_to_float(ctx
, input
[chan
/ 2],
732 case SI_FIX_FETCH_RGB_8
:
733 case SI_FIX_FETCH_RGB_8_INT
:
734 case SI_FIX_FETCH_RGB_16
:
735 case SI_FIX_FETCH_RGB_16_INT
:
736 for (chan
= 0; chan
< 3; chan
++) {
737 out
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
741 if (fix_fetch
== SI_FIX_FETCH_RGB_8
||
742 fix_fetch
== SI_FIX_FETCH_RGB_16
) {
743 out
[3] = LLVMConstReal(ctx
->f32
, 1);
745 out
[3] = ac_to_float(&ctx
->ac
, ctx
->i32_1
);
751 static void declare_input_vs(
752 struct si_shader_context
*ctx
,
753 unsigned input_index
,
754 const struct tgsi_full_declaration
*decl
,
757 si_llvm_load_input_vs(ctx
, input_index
, out
);
760 static LLVMValueRef
get_primitive_id(struct si_shader_context
*ctx
,
767 case PIPE_SHADER_VERTEX
:
768 return LLVMGetParam(ctx
->main_fn
,
769 ctx
->param_vs_prim_id
);
770 case PIPE_SHADER_TESS_CTRL
:
771 return ctx
->abi
.tcs_patch_id
;
772 case PIPE_SHADER_TESS_EVAL
:
773 return ctx
->abi
.tes_patch_id
;
774 case PIPE_SHADER_GEOMETRY
:
775 return ctx
->abi
.gs_prim_id
;
783 * Return the value of tgsi_ind_register for indexing.
784 * This is the indirect index with the constant offset added to it.
786 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
787 const struct tgsi_ind_register
*ind
,
793 if (ind
->File
== TGSI_FILE_ADDRESS
) {
794 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
795 result
= LLVMBuildLoad(ctx
->ac
.builder
, result
, "");
797 struct tgsi_full_src_register src
= {};
799 src
.Register
.File
= ind
->File
;
800 src
.Register
.Index
= ind
->Index
;
802 /* Set the second index to 0 for constants. */
803 if (ind
->File
== TGSI_FILE_CONSTANT
)
804 src
.Register
.Dimension
= 1;
806 result
= ctx
->bld_base
.emit_fetch_funcs
[ind
->File
](&ctx
->bld_base
, &src
,
809 result
= ac_to_integer(&ctx
->ac
, result
);
812 return ac_build_imad(&ctx
->ac
, result
, LLVMConstInt(ctx
->i32
, addr_mul
, 0),
813 LLVMConstInt(ctx
->i32
, rel_index
, 0));
817 * Like si_get_indirect_index, but restricts the return value to a (possibly
818 * undefined) value inside [0..num).
820 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
821 const struct tgsi_ind_register
*ind
,
822 int rel_index
, unsigned num
)
824 LLVMValueRef result
= si_get_indirect_index(ctx
, ind
, 1, rel_index
);
826 return si_llvm_bound_index(ctx
, result
, num
);
829 static LLVMValueRef
get_dw_address_from_generic_indices(struct si_shader_context
*ctx
,
830 LLVMValueRef vertex_dw_stride
,
831 LLVMValueRef base_addr
,
832 LLVMValueRef vertex_index
,
833 LLVMValueRef param_index
,
834 unsigned input_index
,
839 if (vertex_dw_stride
) {
840 base_addr
= ac_build_imad(&ctx
->ac
, vertex_index
,
841 vertex_dw_stride
, base_addr
);
845 base_addr
= ac_build_imad(&ctx
->ac
, param_index
,
846 LLVMConstInt(ctx
->i32
, 4, 0), base_addr
);
849 int param
= is_patch
?
850 si_shader_io_get_unique_index_patch(name
[input_index
],
851 index
[input_index
]) :
852 si_shader_io_get_unique_index(name
[input_index
],
853 index
[input_index
], false);
855 /* Add the base address of the element. */
856 return LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
857 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
861 * Calculate a dword address given an input or output register and a stride.
863 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
864 const struct tgsi_full_dst_register
*dst
,
865 const struct tgsi_full_src_register
*src
,
866 LLVMValueRef vertex_dw_stride
,
867 LLVMValueRef base_addr
)
869 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
870 ubyte
*name
, *index
, *array_first
;
872 struct tgsi_full_dst_register reg
;
873 LLVMValueRef vertex_index
= NULL
;
874 LLVMValueRef ind_index
= NULL
;
876 /* Set the register description. The address computation is the same
877 * for sources and destinations. */
879 reg
.Register
.File
= src
->Register
.File
;
880 reg
.Register
.Index
= src
->Register
.Index
;
881 reg
.Register
.Indirect
= src
->Register
.Indirect
;
882 reg
.Register
.Dimension
= src
->Register
.Dimension
;
883 reg
.Indirect
= src
->Indirect
;
884 reg
.Dimension
= src
->Dimension
;
885 reg
.DimIndirect
= src
->DimIndirect
;
889 /* If the register is 2-dimensional (e.g. an array of vertices
890 * in a primitive), calculate the base address of the vertex. */
891 if (reg
.Register
.Dimension
) {
892 if (reg
.Dimension
.Indirect
)
893 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
894 1, reg
.Dimension
.Index
);
896 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
899 /* Get information about the register. */
900 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
901 name
= info
->input_semantic_name
;
902 index
= info
->input_semantic_index
;
903 array_first
= info
->input_array_first
;
904 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
905 name
= info
->output_semantic_name
;
906 index
= info
->output_semantic_index
;
907 array_first
= info
->output_array_first
;
913 if (reg
.Register
.Indirect
) {
914 /* Add the relative address of the element. */
915 if (reg
.Indirect
.ArrayID
)
916 input_index
= array_first
[reg
.Indirect
.ArrayID
];
918 input_index
= reg
.Register
.Index
;
920 ind_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
921 1, reg
.Register
.Index
- input_index
);
923 input_index
= reg
.Register
.Index
;
926 return get_dw_address_from_generic_indices(ctx
, vertex_dw_stride
,
927 base_addr
, vertex_index
,
928 ind_index
, input_index
,
930 !reg
.Register
.Dimension
);
933 /* The offchip buffer layout for TCS->TES is
935 * - attribute 0 of patch 0 vertex 0
936 * - attribute 0 of patch 0 vertex 1
937 * - attribute 0 of patch 0 vertex 2
939 * - attribute 0 of patch 1 vertex 0
940 * - attribute 0 of patch 1 vertex 1
942 * - attribute 1 of patch 0 vertex 0
943 * - attribute 1 of patch 0 vertex 1
945 * - per patch attribute 0 of patch 0
946 * - per patch attribute 0 of patch 1
949 * Note that every attribute has 4 components.
951 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
952 LLVMValueRef rel_patch_id
,
953 LLVMValueRef vertex_index
,
954 LLVMValueRef param_index
)
956 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
957 LLVMValueRef param_stride
, constant16
;
959 vertices_per_patch
= get_num_tcs_out_vertices(ctx
);
960 num_patches
= si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 0, 6);
961 total_vertices
= LLVMBuildMul(ctx
->ac
.builder
, vertices_per_patch
,
964 constant16
= LLVMConstInt(ctx
->i32
, 16, 0);
966 base_addr
= ac_build_imad(&ctx
->ac
, rel_patch_id
,
967 vertices_per_patch
, vertex_index
);
968 param_stride
= total_vertices
;
970 base_addr
= rel_patch_id
;
971 param_stride
= num_patches
;
974 base_addr
= ac_build_imad(&ctx
->ac
, param_index
, param_stride
, base_addr
);
975 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, base_addr
, constant16
, "");
978 LLVMValueRef patch_data_offset
=
979 si_unpack_param(ctx
, ctx
->param_tcs_offchip_layout
, 12, 20);
981 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
982 patch_data_offset
, "");
987 /* This is a generic helper that can be shared by the NIR and TGSI backends */
988 static LLVMValueRef
get_tcs_tes_buffer_address_from_generic_indices(
989 struct si_shader_context
*ctx
,
990 LLVMValueRef vertex_index
,
991 LLVMValueRef param_index
,
997 unsigned param_index_base
;
999 param_index_base
= is_patch
?
1000 si_shader_io_get_unique_index_patch(name
[param_base
], index
[param_base
]) :
1001 si_shader_io_get_unique_index(name
[param_base
], index
[param_base
], false);
1004 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1005 LLVMConstInt(ctx
->i32
, param_index_base
, 0),
1008 param_index
= LLVMConstInt(ctx
->i32
, param_index_base
, 0);
1011 return get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
),
1012 vertex_index
, param_index
);
1015 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
1016 struct si_shader_context
*ctx
,
1017 const struct tgsi_full_dst_register
*dst
,
1018 const struct tgsi_full_src_register
*src
)
1020 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1021 ubyte
*name
, *index
, *array_first
;
1022 struct tgsi_full_src_register reg
;
1023 LLVMValueRef vertex_index
= NULL
;
1024 LLVMValueRef param_index
= NULL
;
1025 unsigned param_base
;
1027 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
1029 if (reg
.Register
.Dimension
) {
1031 if (reg
.Dimension
.Indirect
)
1032 vertex_index
= si_get_indirect_index(ctx
, ®
.DimIndirect
,
1033 1, reg
.Dimension
.Index
);
1035 vertex_index
= LLVMConstInt(ctx
->i32
, reg
.Dimension
.Index
, 0);
1038 /* Get information about the register. */
1039 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
1040 name
= info
->input_semantic_name
;
1041 index
= info
->input_semantic_index
;
1042 array_first
= info
->input_array_first
;
1043 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
1044 name
= info
->output_semantic_name
;
1045 index
= info
->output_semantic_index
;
1046 array_first
= info
->output_array_first
;
1052 if (reg
.Register
.Indirect
) {
1053 if (reg
.Indirect
.ArrayID
)
1054 param_base
= array_first
[reg
.Indirect
.ArrayID
];
1056 param_base
= reg
.Register
.Index
;
1058 param_index
= si_get_indirect_index(ctx
, ®
.Indirect
,
1059 1, reg
.Register
.Index
- param_base
);
1062 param_base
= reg
.Register
.Index
;
1065 return get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1066 param_index
, param_base
,
1067 name
, index
, !reg
.Register
.Dimension
);
1070 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
1071 LLVMTypeRef type
, unsigned swizzle
,
1072 LLVMValueRef buffer
, LLVMValueRef offset
,
1073 LLVMValueRef base
, bool can_speculate
)
1075 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1076 LLVMValueRef value
, value2
;
1077 LLVMTypeRef vec_type
= LLVMVectorType(type
, 4);
1079 if (swizzle
== ~0) {
1080 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1081 0, 1, 0, can_speculate
, false);
1083 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1086 if (!llvm_type_is_64bit(ctx
, type
)) {
1087 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
1088 0, 1, 0, can_speculate
, false);
1090 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, vec_type
, "");
1091 return LLVMBuildExtractElement(ctx
->ac
.builder
, value
,
1092 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
1095 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1096 swizzle
* 4, 1, 0, can_speculate
, false);
1098 value2
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
1099 swizzle
* 4 + 4, 1, 0, can_speculate
, false);
1101 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1107 * \param type output value type
1108 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
1109 * \param dw_addr address in dwords
1111 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
1112 LLVMTypeRef type
, unsigned swizzle
,
1113 LLVMValueRef dw_addr
)
1115 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1118 if (swizzle
== ~0) {
1119 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1121 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
1122 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
1124 return ac_build_gather_values(&ctx
->ac
, values
,
1128 /* Split 64-bit loads. */
1129 if (llvm_type_is_64bit(ctx
, type
)) {
1130 LLVMValueRef lo
, hi
;
1132 lo
= lds_load(bld_base
, ctx
->i32
, swizzle
, dw_addr
);
1133 hi
= lds_load(bld_base
, ctx
->i32
, swizzle
+ 1, dw_addr
);
1134 return si_llvm_emit_fetch_64bit(bld_base
, type
, lo
, hi
);
1137 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1138 LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
1140 value
= ac_lds_load(&ctx
->ac
, dw_addr
);
1142 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1148 * \param swizzle offset (typically 0..3)
1149 * \param dw_addr address in dwords
1150 * \param value value to store
1152 static void lds_store(struct si_shader_context
*ctx
,
1153 unsigned dw_offset_imm
, LLVMValueRef dw_addr
,
1156 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1157 LLVMConstInt(ctx
->i32
, dw_offset_imm
, 0), "");
1159 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
1164 TESS_OFFCHIP_RING_TCS
,
1165 TESS_OFFCHIP_RING_TES
,
1168 static LLVMValueRef
get_tess_ring_descriptor(struct si_shader_context
*ctx
,
1169 enum si_tess_ring ring
)
1171 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1172 unsigned param
= ring
== TESS_OFFCHIP_RING_TES
? ctx
->param_tes_offchip_addr
:
1173 ctx
->param_tcs_out_lds_layout
;
1174 LLVMValueRef addr
= LLVMGetParam(ctx
->main_fn
, param
);
1176 /* TCS only receives high 13 bits of the address. */
1177 if (ring
== TESS_OFFCHIP_RING_TCS
|| ring
== TCS_FACTOR_RING
) {
1178 addr
= LLVMBuildAnd(builder
, addr
,
1179 LLVMConstInt(ctx
->i32
, 0xfff80000, 0), "");
1182 if (ring
== TCS_FACTOR_RING
) {
1183 unsigned tf_offset
= ctx
->screen
->tess_offchip_ring_size
;
1184 addr
= LLVMBuildAdd(builder
, addr
,
1185 LLVMConstInt(ctx
->i32
, tf_offset
, 0), "");
1188 LLVMValueRef desc
[4];
1190 desc
[1] = LLVMConstInt(ctx
->i32
,
1191 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
1192 desc
[2] = LLVMConstInt(ctx
->i32
, 0xffffffff, 0);
1193 desc
[3] = LLVMConstInt(ctx
->i32
,
1194 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1195 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1196 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1197 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1198 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1199 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0);
1201 return ac_build_gather_values(&ctx
->ac
, desc
, 4);
1204 static LLVMValueRef
fetch_input_tcs(
1205 struct lp_build_tgsi_context
*bld_base
,
1206 const struct tgsi_full_src_register
*reg
,
1207 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1209 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1210 LLVMValueRef dw_addr
, stride
;
1211 unsigned swizzle
= swizzle_in
& 0xffff;
1212 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1213 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1214 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1216 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1219 static LLVMValueRef
si_nir_load_tcs_varyings(struct ac_shader_abi
*abi
,
1221 LLVMValueRef vertex_index
,
1222 LLVMValueRef param_index
,
1223 unsigned const_index
,
1225 unsigned driver_location
,
1227 unsigned num_components
,
1232 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1233 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1234 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1235 LLVMValueRef dw_addr
, stride
;
1237 driver_location
= driver_location
/ 4;
1240 stride
= get_tcs_in_vertex_dw_stride(ctx
);
1241 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1245 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1247 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1248 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1253 /* Add the constant index to the indirect index */
1254 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1255 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1257 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1263 names
= info
->input_semantic_name
;
1264 indices
= info
->input_semantic_index
;
1266 names
= info
->output_semantic_name
;
1267 indices
= info
->output_semantic_index
;
1270 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1271 vertex_index
, param_index
,
1276 LLVMValueRef value
[4];
1277 for (unsigned i
= 0; i
< num_components
; i
++) {
1278 unsigned offset
= i
;
1279 if (llvm_type_is_64bit(ctx
, type
))
1282 offset
+= component
;
1283 value
[i
+ component
] = lds_load(bld_base
, type
, offset
, dw_addr
);
1286 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1289 static LLVMValueRef
fetch_output_tcs(
1290 struct lp_build_tgsi_context
*bld_base
,
1291 const struct tgsi_full_src_register
*reg
,
1292 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1294 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1295 LLVMValueRef dw_addr
, stride
;
1296 unsigned swizzle
= (swizzle_in
& 0xffff);
1298 if (reg
->Register
.Dimension
) {
1299 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1300 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1301 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1303 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1304 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
1307 return lds_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
, dw_addr
);
1310 static LLVMValueRef
fetch_input_tes(
1311 struct lp_build_tgsi_context
*bld_base
,
1312 const struct tgsi_full_src_register
*reg
,
1313 enum tgsi_opcode_type type
, unsigned swizzle_in
)
1315 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1316 LLVMValueRef base
, addr
;
1317 unsigned swizzle
= (swizzle_in
& 0xffff);
1319 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1320 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
1322 return buffer_load(bld_base
, tgsi2llvmtype(bld_base
, type
), swizzle
,
1323 ctx
->tess_offchip_ring
, base
, addr
, true);
1326 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
1328 LLVMValueRef vertex_index
,
1329 LLVMValueRef param_index
,
1330 unsigned const_index
,
1332 unsigned driver_location
,
1334 unsigned num_components
,
1339 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1340 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1341 LLVMValueRef base
, addr
;
1343 driver_location
= driver_location
/ 4;
1345 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1348 /* Add the constant index to the indirect index */
1349 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1350 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1352 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1355 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1356 param_index
, driver_location
,
1357 info
->input_semantic_name
,
1358 info
->input_semantic_index
,
1361 /* TODO: This will generate rather ordinary llvm code, although it
1362 * should be easy for the optimiser to fix up. In future we might want
1363 * to refactor buffer_load(), but for now this maximises code sharing
1364 * between the NIR and TGSI backends.
1366 LLVMValueRef value
[4];
1367 for (unsigned i
= 0; i
< num_components
; i
++) {
1368 unsigned offset
= i
;
1369 if (llvm_type_is_64bit(ctx
, type
))
1372 offset
+= component
;
1373 value
[i
+ component
] = buffer_load(&ctx
->bld_base
, type
, offset
,
1374 ctx
->tess_offchip_ring
, base
, addr
, true);
1377 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1380 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1381 const struct tgsi_full_instruction
*inst
,
1382 const struct tgsi_opcode_info
*info
,
1384 LLVMValueRef dst
[4])
1386 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1387 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
1388 const struct tgsi_shader_info
*sh_info
= &ctx
->shader
->selector
->info
;
1389 unsigned chan_index
;
1390 LLVMValueRef dw_addr
, stride
;
1391 LLVMValueRef buffer
, base
, buf_addr
;
1392 LLVMValueRef values
[4];
1393 bool skip_lds_store
;
1394 bool is_tess_factor
= false, is_tess_inner
= false;
1396 /* Only handle per-patch and per-vertex outputs here.
1397 * Vectors will be lowered to scalars and this function will be called again.
1399 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1400 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1401 si_llvm_emit_store(bld_base
, inst
, info
, index
, dst
);
1405 if (reg
->Register
.Dimension
) {
1406 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1407 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1408 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1409 skip_lds_store
= !sh_info
->reads_pervertex_outputs
;
1411 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1412 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1413 skip_lds_store
= !sh_info
->reads_perpatch_outputs
;
1415 if (!reg
->Register
.Indirect
) {
1416 int name
= sh_info
->output_semantic_name
[reg
->Register
.Index
];
1418 /* Always write tess factors into LDS for the TCS epilog. */
1419 if (name
== TGSI_SEMANTIC_TESSINNER
||
1420 name
== TGSI_SEMANTIC_TESSOUTER
) {
1421 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1422 skip_lds_store
= !sh_info
->reads_tessfactor_outputs
&&
1423 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1424 is_tess_factor
= true;
1425 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1430 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1432 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1433 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1435 uint32_t writemask
= reg
->Register
.WriteMask
;
1437 chan_index
= u_bit_scan(&writemask
);
1438 LLVMValueRef value
= dst
[chan_index
];
1440 if (inst
->Instruction
.Saturate
)
1441 value
= ac_build_clamp(&ctx
->ac
, value
);
1443 /* Skip LDS stores if there is no LDS read of this output. */
1444 if (!skip_lds_store
)
1445 lds_store(ctx
, chan_index
, dw_addr
, value
);
1447 value
= ac_to_integer(&ctx
->ac
, value
);
1448 values
[chan_index
] = value
;
1450 if (reg
->Register
.WriteMask
!= 0xF && !is_tess_factor
) {
1451 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1453 4 * chan_index
, 1, 0, true, false);
1456 /* Write tess factors into VGPRs for the epilog. */
1457 if (is_tess_factor
&&
1458 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1459 if (!is_tess_inner
) {
1460 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1461 ctx
->invoc0_tess_factors
[chan_index
]);
1462 } else if (chan_index
< 2) {
1463 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1464 ctx
->invoc0_tess_factors
[4 + chan_index
]);
1469 if (reg
->Register
.WriteMask
== 0xF && !is_tess_factor
) {
1470 LLVMValueRef value
= ac_build_gather_values(&ctx
->ac
,
1472 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buf_addr
,
1473 base
, 0, 1, 0, true, false);
1477 static void si_nir_store_output_tcs(struct ac_shader_abi
*abi
,
1478 const struct nir_variable
*var
,
1479 LLVMValueRef vertex_index
,
1480 LLVMValueRef param_index
,
1481 unsigned const_index
,
1485 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1486 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1487 const unsigned component
= var
->data
.location_frac
;
1488 const bool is_patch
= var
->data
.patch
;
1489 unsigned driver_location
= var
->data
.driver_location
;
1490 LLVMValueRef dw_addr
, stride
;
1491 LLVMValueRef buffer
, base
, addr
;
1492 LLVMValueRef values
[4];
1493 bool skip_lds_store
;
1494 bool is_tess_factor
= false, is_tess_inner
= false;
1496 driver_location
= driver_location
/ 4;
1499 /* Add the constant index to the indirect index */
1500 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, param_index
,
1501 LLVMConstInt(ctx
->i32
, const_index
, 0), "");
1503 if (const_index
!= 0)
1504 param_index
= LLVMConstInt(ctx
->i32
, const_index
, 0);
1508 stride
= get_tcs_out_vertex_dw_stride(ctx
);
1509 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1510 dw_addr
= get_dw_address_from_generic_indices(ctx
, stride
, dw_addr
,
1511 vertex_index
, param_index
,
1513 info
->output_semantic_name
,
1514 info
->output_semantic_index
,
1517 skip_lds_store
= !info
->reads_pervertex_outputs
;
1519 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1520 dw_addr
= get_dw_address_from_generic_indices(ctx
, NULL
, dw_addr
,
1521 vertex_index
, param_index
,
1523 info
->output_semantic_name
,
1524 info
->output_semantic_index
,
1527 skip_lds_store
= !info
->reads_perpatch_outputs
;
1530 int name
= info
->output_semantic_name
[driver_location
];
1532 /* Always write tess factors into LDS for the TCS epilog. */
1533 if (name
== TGSI_SEMANTIC_TESSINNER
||
1534 name
== TGSI_SEMANTIC_TESSOUTER
) {
1535 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
1536 skip_lds_store
= !info
->reads_tessfactor_outputs
&&
1537 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
;
1538 is_tess_factor
= true;
1539 is_tess_inner
= name
== TGSI_SEMANTIC_TESSINNER
;
1544 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
1546 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
1548 addr
= get_tcs_tes_buffer_address_from_generic_indices(ctx
, vertex_index
,
1549 param_index
, driver_location
,
1550 info
->output_semantic_name
,
1551 info
->output_semantic_index
,
1554 for (unsigned chan
= 0; chan
< 4; chan
++) {
1555 if (!(writemask
& (1 << chan
)))
1557 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
1559 /* Skip LDS stores if there is no LDS read of this output. */
1560 if (!skip_lds_store
)
1561 lds_store(ctx
, chan
, dw_addr
, value
);
1563 value
= ac_to_integer(&ctx
->ac
, value
);
1564 values
[chan
] = value
;
1566 if (writemask
!= 0xF && !is_tess_factor
) {
1567 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1569 4 * chan
, 1, 0, true, false);
1572 /* Write tess factors into VGPRs for the epilog. */
1573 if (is_tess_factor
&&
1574 ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
1575 if (!is_tess_inner
) {
1576 LLVMBuildStore(ctx
->ac
.builder
, value
, /* outer */
1577 ctx
->invoc0_tess_factors
[chan
]);
1578 } else if (chan
< 2) {
1579 LLVMBuildStore(ctx
->ac
.builder
, value
, /* inner */
1580 ctx
->invoc0_tess_factors
[4 + chan
]);
1585 if (writemask
== 0xF && !is_tess_factor
) {
1586 LLVMValueRef value
= ac_build_gather_values(&ctx
->ac
,
1588 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, addr
,
1589 base
, 0, 1, 0, true, false);
1593 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
1594 unsigned input_index
,
1595 unsigned vtx_offset_param
,
1599 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1600 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1601 struct si_shader
*shader
= ctx
->shader
;
1602 LLVMValueRef vtx_offset
, soffset
;
1603 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1604 unsigned semantic_name
= info
->input_semantic_name
[input_index
];
1605 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1609 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
, false);
1611 /* GFX9 has the ESGS ring in LDS. */
1612 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1613 unsigned index
= vtx_offset_param
;
1615 switch (index
/ 2) {
1617 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx01_offset
,
1618 index
% 2 ? 16 : 0, 16);
1621 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx23_offset
,
1622 index
% 2 ? 16 : 0, 16);
1625 vtx_offset
= si_unpack_param(ctx
, ctx
->param_gs_vtx45_offset
,
1626 index
% 2 ? 16 : 0, 16);
1633 vtx_offset
= LLVMBuildAdd(ctx
->ac
.builder
, vtx_offset
,
1634 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
1635 return lds_load(bld_base
, type
, swizzle
, vtx_offset
);
1638 /* GFX6: input load from the ESGS ring in memory. */
1639 if (swizzle
== ~0) {
1640 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1642 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1643 values
[chan
] = si_llvm_load_input_gs(abi
, input_index
, vtx_offset_param
,
1646 return ac_build_gather_values(&ctx
->ac
, values
,
1650 /* Get the vertex offset parameter on GFX6. */
1651 LLVMValueRef gs_vtx_offset
= ctx
->gs_vtx_offset
[vtx_offset_param
];
1653 vtx_offset
= LLVMBuildMul(ctx
->ac
.builder
, gs_vtx_offset
,
1654 LLVMConstInt(ctx
->i32
, 4, 0), "");
1656 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
) * 256, 0);
1658 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1, ctx
->i32_0
,
1659 vtx_offset
, soffset
, 0, 1, 0, true, false);
1660 if (llvm_type_is_64bit(ctx
, type
)) {
1661 LLVMValueRef value2
;
1662 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
+ 1) * 256, 0);
1664 value2
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1,
1665 ctx
->i32_0
, vtx_offset
, soffset
,
1666 0, 1, 0, true, false);
1667 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
1669 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, type
, "");
1672 static LLVMValueRef
si_nir_load_input_gs(struct ac_shader_abi
*abi
,
1674 unsigned driver_location
,
1676 unsigned num_components
,
1677 unsigned vertex_index
,
1678 unsigned const_index
,
1681 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1683 LLVMValueRef value
[4];
1684 for (unsigned i
= 0; i
< num_components
; i
++) {
1685 unsigned offset
= i
;
1686 if (llvm_type_is_64bit(ctx
, type
))
1689 offset
+= component
;
1690 value
[i
+ component
] = si_llvm_load_input_gs(&ctx
->abi
, driver_location
/ 4,
1691 vertex_index
, type
, offset
);
1694 return ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1697 static LLVMValueRef
fetch_input_gs(
1698 struct lp_build_tgsi_context
*bld_base
,
1699 const struct tgsi_full_src_register
*reg
,
1700 enum tgsi_opcode_type type
,
1701 unsigned swizzle_in
)
1703 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1704 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
1705 unsigned swizzle
= swizzle_in
& 0xffff;
1707 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1708 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1709 return get_primitive_id(ctx
, swizzle
);
1711 if (!reg
->Register
.Dimension
)
1714 return si_llvm_load_input_gs(&ctx
->abi
, reg
->Register
.Index
,
1715 reg
->Dimension
.Index
,
1716 tgsi2llvmtype(bld_base
, type
),
1720 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1722 switch (interpolate
) {
1723 case TGSI_INTERPOLATE_CONSTANT
:
1726 case TGSI_INTERPOLATE_LINEAR
:
1727 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1728 return SI_PARAM_LINEAR_SAMPLE
;
1729 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1730 return SI_PARAM_LINEAR_CENTROID
;
1732 return SI_PARAM_LINEAR_CENTER
;
1734 case TGSI_INTERPOLATE_COLOR
:
1735 case TGSI_INTERPOLATE_PERSPECTIVE
:
1736 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1737 return SI_PARAM_PERSP_SAMPLE
;
1738 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1739 return SI_PARAM_PERSP_CENTROID
;
1741 return SI_PARAM_PERSP_CENTER
;
1744 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1749 static LLVMValueRef
si_build_fs_interp(struct si_shader_context
*ctx
,
1750 unsigned attr_index
, unsigned chan
,
1751 LLVMValueRef prim_mask
,
1752 LLVMValueRef i
, LLVMValueRef j
)
1755 return ac_build_fs_interp(&ctx
->ac
,
1756 LLVMConstInt(ctx
->i32
, chan
, 0),
1757 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1760 return ac_build_fs_interp_mov(&ctx
->ac
,
1761 LLVMConstInt(ctx
->i32
, 2, 0), /* P0 */
1762 LLVMConstInt(ctx
->i32
, chan
, 0),
1763 LLVMConstInt(ctx
->i32
, attr_index
, 0),
1768 * Interpolate a fragment shader input.
1770 * @param ctx context
1771 * @param input_index index of the input in hardware
1772 * @param semantic_name TGSI_SEMANTIC_*
1773 * @param semantic_index semantic index
1774 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1775 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1776 * @param interp_param interpolation weights (i,j)
1777 * @param prim_mask SI_PARAM_PRIM_MASK
1778 * @param face SI_PARAM_FRONT_FACE
1779 * @param result the return value (4 components)
1781 static void interp_fs_input(struct si_shader_context
*ctx
,
1782 unsigned input_index
,
1783 unsigned semantic_name
,
1784 unsigned semantic_index
,
1785 unsigned num_interp_inputs
,
1786 unsigned colors_read_mask
,
1787 LLVMValueRef interp_param
,
1788 LLVMValueRef prim_mask
,
1790 LLVMValueRef result
[4])
1792 LLVMValueRef i
= NULL
, j
= NULL
;
1795 /* fs.constant returns the param from the middle vertex, so it's not
1796 * really useful for flat shading. It's meant to be used for custom
1797 * interpolation (but the intrinsic can't fetch from the other two
1800 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1801 * to do the right thing. The only reason we use fs.constant is that
1802 * fs.interp cannot be used on integers, because they can be equal
1805 * When interp is false we will use fs.constant or for newer llvm,
1806 * amdgcn.interp.mov.
1808 bool interp
= interp_param
!= NULL
;
1811 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
1812 LLVMVectorType(ctx
->f32
, 2), "");
1814 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1816 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
1820 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1821 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1822 LLVMValueRef is_face_positive
;
1824 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1825 * otherwise it's at offset "num_inputs".
1827 unsigned back_attr_offset
= num_interp_inputs
;
1828 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1829 back_attr_offset
+= 1;
1831 is_face_positive
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
1832 face
, ctx
->i32_0
, "");
1834 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1835 LLVMValueRef front
, back
;
1837 front
= si_build_fs_interp(ctx
,
1840 back
= si_build_fs_interp(ctx
,
1841 back_attr_offset
, chan
,
1844 result
[chan
] = LLVMBuildSelect(ctx
->ac
.builder
,
1850 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1851 result
[0] = si_build_fs_interp(ctx
, input_index
,
1852 0, prim_mask
, i
, j
);
1854 result
[2] = LLVMConstReal(ctx
->f32
, 0.0f
);
1855 result
[3] = LLVMConstReal(ctx
->f32
, 1.0f
);
1857 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1858 result
[chan
] = si_build_fs_interp(ctx
,
1865 void si_llvm_load_input_fs(
1866 struct si_shader_context
*ctx
,
1867 unsigned input_index
,
1868 LLVMValueRef out
[4])
1870 struct si_shader
*shader
= ctx
->shader
;
1871 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1872 LLVMValueRef main_fn
= ctx
->main_fn
;
1873 LLVMValueRef interp_param
= NULL
;
1874 int interp_param_idx
;
1875 enum tgsi_semantic semantic_name
= info
->input_semantic_name
[input_index
];
1876 unsigned semantic_index
= info
->input_semantic_index
[input_index
];
1877 enum tgsi_interpolate_mode interp_mode
= info
->input_interpolate
[input_index
];
1878 enum tgsi_interpolate_loc interp_loc
= info
->input_interpolate_loc
[input_index
];
1880 /* Get colors from input VGPRs (set by the prolog). */
1881 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1882 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1883 unsigned mask
= colors_read
>> (semantic_index
* 4);
1884 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1885 (semantic_index
? util_bitcount(colors_read
& 0xf) : 0);
1886 LLVMValueRef undef
= LLVMGetUndef(ctx
->f32
);
1888 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1889 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1890 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1891 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
1895 interp_param_idx
= lookup_interp_param_index(interp_mode
, interp_loc
);
1896 if (interp_param_idx
== -1)
1898 else if (interp_param_idx
) {
1899 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1902 interp_fs_input(ctx
, input_index
, semantic_name
,
1903 semantic_index
, 0, /* this param is unused */
1904 shader
->selector
->info
.colors_read
, interp_param
,
1906 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1910 static void declare_input_fs(
1911 struct si_shader_context
*ctx
,
1912 unsigned input_index
,
1913 const struct tgsi_full_declaration
*decl
,
1914 LLVMValueRef out
[4])
1916 si_llvm_load_input_fs(ctx
, input_index
, out
);
1919 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
)
1921 return si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 8, 4);
1924 static LLVMValueRef
get_base_vertex(struct ac_shader_abi
*abi
)
1926 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1928 /* For non-indexed draws, the base vertex set by the driver
1929 * (for direct draws) or the CP (for indirect draws) is the
1930 * first vertex ID, but GLSL expects 0 to be returned.
1932 LLVMValueRef vs_state
= LLVMGetParam(ctx
->main_fn
,
1933 ctx
->param_vs_state_bits
);
1934 LLVMValueRef indexed
;
1936 indexed
= LLVMBuildLShr(ctx
->ac
.builder
, vs_state
, ctx
->i32_1
, "");
1937 indexed
= LLVMBuildTrunc(ctx
->ac
.builder
, indexed
, ctx
->i1
, "");
1939 return LLVMBuildSelect(ctx
->ac
.builder
, indexed
, ctx
->abi
.base_vertex
,
1943 static LLVMValueRef
get_block_size(struct ac_shader_abi
*abi
)
1945 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1947 LLVMValueRef values
[3];
1948 LLVMValueRef result
;
1950 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1952 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1953 unsigned sizes
[3] = {
1954 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1955 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1956 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1959 for (i
= 0; i
< 3; ++i
)
1960 values
[i
] = LLVMConstInt(ctx
->i32
, sizes
[i
], 0);
1962 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
1964 result
= LLVMGetParam(ctx
->main_fn
, ctx
->param_block_size
);
1971 * Load a dword from a constant buffer.
1973 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1974 LLVMValueRef resource
,
1975 LLVMValueRef offset
)
1977 return ac_build_buffer_load(&ctx
->ac
, resource
, 1, NULL
, offset
, NULL
,
1978 0, 0, 0, true, true);
1981 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
, LLVMValueRef sample_id
)
1983 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1984 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1985 LLVMValueRef buf_index
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_SAMPLE_POSITIONS
, 0);
1986 LLVMValueRef resource
= ac_build_load_to_sgpr(&ctx
->ac
, desc
, buf_index
);
1988 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1989 LLVMValueRef offset0
= LLVMBuildMul(ctx
->ac
.builder
, sample_id
, LLVMConstInt(ctx
->i32
, 8, 0), "");
1990 LLVMValueRef offset1
= LLVMBuildAdd(ctx
->ac
.builder
, offset0
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1992 LLVMValueRef pos
[4] = {
1993 buffer_load_const(ctx
, resource
, offset0
),
1994 buffer_load_const(ctx
, resource
, offset1
),
1995 LLVMConstReal(ctx
->f32
, 0),
1996 LLVMConstReal(ctx
->f32
, 0)
1999 return ac_build_gather_values(&ctx
->ac
, pos
, 4);
2002 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
2004 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2005 return ac_to_integer(&ctx
->ac
, abi
->sample_coverage
);
2008 static LLVMValueRef
si_load_tess_coord(struct ac_shader_abi
*abi
)
2010 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2011 LLVMValueRef coord
[4] = {
2012 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_u
),
2013 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_v
),
2018 /* For triangles, the vector should be (u, v, 1-u-v). */
2019 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
2020 PIPE_PRIM_TRIANGLES
) {
2021 coord
[2] = LLVMBuildFSub(ctx
->ac
.builder
, ctx
->ac
.f32_1
,
2022 LLVMBuildFAdd(ctx
->ac
.builder
,
2023 coord
[0], coord
[1], ""), "");
2025 return ac_build_gather_values(&ctx
->ac
, coord
, 4);
2028 static LLVMValueRef
load_tess_level(struct si_shader_context
*ctx
,
2029 unsigned semantic_name
)
2031 LLVMValueRef base
, addr
;
2033 int param
= si_shader_io_get_unique_index_patch(semantic_name
, 0);
2035 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
2036 addr
= get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
), NULL
,
2037 LLVMConstInt(ctx
->i32
, param
, 0));
2039 return buffer_load(&ctx
->bld_base
, ctx
->f32
,
2040 ~0, ctx
->tess_offchip_ring
, base
, addr
, true);
2044 static LLVMValueRef
si_load_tess_level(struct ac_shader_abi
*abi
,
2045 unsigned varying_id
)
2047 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2048 unsigned semantic_name
;
2050 switch (varying_id
) {
2051 case VARYING_SLOT_TESS_LEVEL_INNER
:
2052 semantic_name
= TGSI_SEMANTIC_TESSINNER
;
2054 case VARYING_SLOT_TESS_LEVEL_OUTER
:
2055 semantic_name
= TGSI_SEMANTIC_TESSOUTER
;
2058 unreachable("unknown tess level");
2061 return load_tess_level(ctx
, semantic_name
);
2065 static LLVMValueRef
si_load_patch_vertices_in(struct ac_shader_abi
*abi
)
2067 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2068 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2069 return si_unpack_param(ctx
, ctx
->param_tcs_out_lds_layout
, 13, 6);
2070 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
2071 return get_num_tcs_out_vertices(ctx
);
2073 unreachable("invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
2076 void si_load_system_value(struct si_shader_context
*ctx
,
2078 const struct tgsi_full_declaration
*decl
)
2080 LLVMValueRef value
= 0;
2082 assert(index
< RADEON_LLVM_MAX_SYSTEM_VALUES
);
2084 switch (decl
->Semantic
.Name
) {
2085 case TGSI_SEMANTIC_INSTANCEID
:
2086 value
= ctx
->abi
.instance_id
;
2089 case TGSI_SEMANTIC_VERTEXID
:
2090 value
= LLVMBuildAdd(ctx
->ac
.builder
,
2092 ctx
->abi
.base_vertex
, "");
2095 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
2096 /* Unused. Clarify the meaning in indexed vs. non-indexed
2097 * draws if this is ever used again. */
2101 case TGSI_SEMANTIC_BASEVERTEX
:
2102 value
= get_base_vertex(&ctx
->abi
);
2105 case TGSI_SEMANTIC_BASEINSTANCE
:
2106 value
= ctx
->abi
.start_instance
;
2109 case TGSI_SEMANTIC_DRAWID
:
2110 value
= ctx
->abi
.draw_id
;
2113 case TGSI_SEMANTIC_INVOCATIONID
:
2114 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
2115 value
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
2116 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
2117 value
= ctx
->abi
.gs_invocation_id
;
2119 assert(!"INVOCATIONID not implemented");
2122 case TGSI_SEMANTIC_POSITION
:
2124 LLVMValueRef pos
[4] = {
2125 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2126 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2127 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
2128 ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
2129 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_W_FLOAT
)),
2131 value
= ac_build_gather_values(&ctx
->ac
, pos
, 4);
2135 case TGSI_SEMANTIC_FACE
:
2136 value
= ctx
->abi
.front_face
;
2139 case TGSI_SEMANTIC_SAMPLEID
:
2140 value
= si_get_sample_id(ctx
);
2143 case TGSI_SEMANTIC_SAMPLEPOS
: {
2144 LLVMValueRef pos
[4] = {
2145 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
2146 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
2147 LLVMConstReal(ctx
->f32
, 0),
2148 LLVMConstReal(ctx
->f32
, 0)
2150 pos
[0] = ac_build_fract(&ctx
->ac
, pos
[0], 32);
2151 pos
[1] = ac_build_fract(&ctx
->ac
, pos
[1], 32);
2152 value
= ac_build_gather_values(&ctx
->ac
, pos
, 4);
2156 case TGSI_SEMANTIC_SAMPLEMASK
:
2157 /* This can only occur with the OpenGL Core profile, which
2158 * doesn't support smoothing.
2160 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
2163 case TGSI_SEMANTIC_TESSCOORD
:
2164 value
= si_load_tess_coord(&ctx
->abi
);
2167 case TGSI_SEMANTIC_VERTICESIN
:
2168 value
= si_load_patch_vertices_in(&ctx
->abi
);
2171 case TGSI_SEMANTIC_TESSINNER
:
2172 case TGSI_SEMANTIC_TESSOUTER
:
2173 value
= load_tess_level(ctx
, decl
->Semantic
.Name
);
2176 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
2177 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
2179 LLVMValueRef buf
, slot
, val
[4];
2182 slot
= LLVMConstInt(ctx
->i32
, SI_HS_CONST_DEFAULT_TESS_LEVELS
, 0);
2183 buf
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2184 buf
= ac_build_load_to_sgpr(&ctx
->ac
, buf
, slot
);
2185 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
2187 for (i
= 0; i
< 4; i
++)
2188 val
[i
] = buffer_load_const(ctx
, buf
,
2189 LLVMConstInt(ctx
->i32
, (offset
+ i
) * 4, 0));
2190 value
= ac_build_gather_values(&ctx
->ac
, val
, 4);
2194 case TGSI_SEMANTIC_PRIMID
:
2195 value
= get_primitive_id(ctx
, 0);
2198 case TGSI_SEMANTIC_GRID_SIZE
:
2199 value
= ctx
->abi
.num_work_groups
;
2202 case TGSI_SEMANTIC_BLOCK_SIZE
:
2203 value
= get_block_size(&ctx
->abi
);
2206 case TGSI_SEMANTIC_BLOCK_ID
:
2208 LLVMValueRef values
[3];
2210 for (int i
= 0; i
< 3; i
++) {
2211 values
[i
] = ctx
->i32_0
;
2212 if (ctx
->abi
.workgroup_ids
[i
]) {
2213 values
[i
] = ctx
->abi
.workgroup_ids
[i
];
2216 value
= ac_build_gather_values(&ctx
->ac
, values
, 3);
2220 case TGSI_SEMANTIC_THREAD_ID
:
2221 value
= ctx
->abi
.local_invocation_ids
;
2224 case TGSI_SEMANTIC_HELPER_INVOCATION
:
2225 value
= ac_build_intrinsic(&ctx
->ac
,
2226 "llvm.amdgcn.ps.live",
2228 AC_FUNC_ATTR_READNONE
);
2229 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2230 value
= LLVMBuildSExt(ctx
->ac
.builder
, value
, ctx
->i32
, "");
2233 case TGSI_SEMANTIC_SUBGROUP_SIZE
:
2234 value
= LLVMConstInt(ctx
->i32
, 64, 0);
2237 case TGSI_SEMANTIC_SUBGROUP_INVOCATION
:
2238 value
= ac_get_thread_id(&ctx
->ac
);
2241 case TGSI_SEMANTIC_SUBGROUP_EQ_MASK
:
2243 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2244 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2245 value
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->i64
, 1, 0), id
, "");
2246 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2250 case TGSI_SEMANTIC_SUBGROUP_GE_MASK
:
2251 case TGSI_SEMANTIC_SUBGROUP_GT_MASK
:
2252 case TGSI_SEMANTIC_SUBGROUP_LE_MASK
:
2253 case TGSI_SEMANTIC_SUBGROUP_LT_MASK
:
2255 LLVMValueRef id
= ac_get_thread_id(&ctx
->ac
);
2256 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_GT_MASK
||
2257 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
) {
2258 /* All bits set except LSB */
2259 value
= LLVMConstInt(ctx
->i64
, -2, 0);
2262 value
= LLVMConstInt(ctx
->i64
, -1, 0);
2264 id
= LLVMBuildZExt(ctx
->ac
.builder
, id
, ctx
->i64
, "");
2265 value
= LLVMBuildShl(ctx
->ac
.builder
, value
, id
, "");
2266 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LE_MASK
||
2267 decl
->Semantic
.Name
== TGSI_SEMANTIC_SUBGROUP_LT_MASK
)
2268 value
= LLVMBuildNot(ctx
->ac
.builder
, value
, "");
2269 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->v2i32
, "");
2273 case TGSI_SEMANTIC_CS_USER_DATA
:
2274 value
= LLVMGetParam(ctx
->main_fn
, ctx
->param_cs_user_data
);
2278 assert(!"unknown system value");
2282 ctx
->system_values
[index
] = value
;
2285 void si_declare_compute_memory(struct si_shader_context
*ctx
)
2287 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2288 unsigned lds_size
= sel
->info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
];
2290 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, AC_LOCAL_ADDR_SPACE
);
2293 assert(!ctx
->ac
.lds
);
2295 var
= LLVMAddGlobalInAddressSpace(ctx
->ac
.module
,
2296 LLVMArrayType(ctx
->i8
, lds_size
),
2298 AC_LOCAL_ADDR_SPACE
);
2299 LLVMSetAlignment(var
, 4);
2301 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, var
, i8p
, "");
2304 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
2305 const struct tgsi_full_declaration
*decl
)
2307 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
2308 assert(decl
->Range
.First
== decl
->Range
.Last
);
2310 si_declare_compute_memory(ctx
);
2313 static LLVMValueRef
load_const_buffer_desc_fast_path(struct si_shader_context
*ctx
)
2316 LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2317 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2319 /* Do the bounds checking with a descriptor, because
2320 * doing computation and manual bounds checking of 64-bit
2321 * addresses generates horrible VALU code with very high
2322 * VGPR usage and very low SIMD occupancy.
2324 ptr
= LLVMBuildPtrToInt(ctx
->ac
.builder
, ptr
, ctx
->ac
.intptr
, "");
2326 LLVMValueRef desc0
, desc1
;
2327 if (HAVE_32BIT_POINTERS
) {
2329 desc1
= LLVMConstInt(ctx
->i32
,
2330 S_008F04_BASE_ADDRESS_HI(ctx
->screen
->info
.address32_hi
), 0);
2332 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
, ctx
->v2i32
, "");
2333 desc0
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_0
, "");
2334 desc1
= LLVMBuildExtractElement(ctx
->ac
.builder
, ptr
, ctx
->i32_1
, "");
2335 /* Mask out all bits except BASE_ADDRESS_HI. */
2336 desc1
= LLVMBuildAnd(ctx
->ac
.builder
, desc1
,
2337 LLVMConstInt(ctx
->i32
, ~C_008F04_BASE_ADDRESS_HI
, 0), "");
2340 LLVMValueRef desc_elems
[] = {
2343 LLVMConstInt(ctx
->i32
, (sel
->info
.const_file_max
[0] + 1) * 16, 0),
2344 LLVMConstInt(ctx
->i32
,
2345 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
2346 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
2347 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
2348 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
2349 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
2350 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
), 0)
2353 return ac_build_gather_values(&ctx
->ac
, desc_elems
, 4);
2356 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
2358 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
2359 ctx
->param_const_and_shader_buffers
);
2361 return ac_build_load_to_sgpr(&ctx
->ac
, list_ptr
,
2362 LLVMConstInt(ctx
->i32
, si_get_constbuf_slot(i
), 0));
2365 static LLVMValueRef
load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef index
)
2367 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2368 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2370 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2372 if (sel
->info
.const_buffers_declared
== 1 &&
2373 sel
->info
.shader_buffers_declared
== 0) {
2374 return load_const_buffer_desc_fast_path(ctx
);
2377 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_const_buffers
);
2378 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2379 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2381 return ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2385 load_ssbo(struct ac_shader_abi
*abi
, LLVMValueRef index
, bool write
)
2387 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
2388 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
2389 ctx
->param_const_and_shader_buffers
);
2391 index
= si_llvm_bound_index(ctx
, index
, ctx
->num_shader_buffers
);
2392 index
= LLVMBuildSub(ctx
->ac
.builder
,
2393 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
- 1, 0),
2396 return ac_build_load_to_sgpr(&ctx
->ac
, rsrc_ptr
, index
);
2399 static LLVMValueRef
fetch_constant(
2400 struct lp_build_tgsi_context
*bld_base
,
2401 const struct tgsi_full_src_register
*reg
,
2402 enum tgsi_opcode_type type
,
2403 unsigned swizzle_in
)
2405 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2406 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2407 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
2409 unsigned swizzle
= swizzle_in
& 0xffff;
2411 LLVMValueRef addr
, bufp
;
2413 if (swizzle_in
== LP_CHAN_ALL
) {
2415 LLVMValueRef values
[4];
2416 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
2417 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
2419 return ac_build_gather_values(&ctx
->ac
, values
, 4);
2422 /* Split 64-bit loads. */
2423 if (tgsi_type_is_64bit(type
)) {
2424 LLVMValueRef lo
, hi
;
2426 lo
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, swizzle
);
2427 hi
= fetch_constant(bld_base
, reg
, TGSI_TYPE_UNSIGNED
, (swizzle_in
>> 16));
2428 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
2432 idx
= reg
->Register
.Index
* 4 + swizzle
;
2433 if (reg
->Register
.Indirect
) {
2434 addr
= si_get_indirect_index(ctx
, ireg
, 16, idx
* 4);
2436 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
2439 /* Fast path when user data SGPRs point to constant buffer 0 directly. */
2440 if (sel
->info
.const_buffers_declared
== 1 &&
2441 sel
->info
.shader_buffers_declared
== 0) {
2442 LLVMValueRef desc
= load_const_buffer_desc_fast_path(ctx
);
2443 LLVMValueRef result
= buffer_load_const(ctx
, desc
, addr
);
2444 return bitcast(bld_base
, type
, result
);
2447 assert(reg
->Register
.Dimension
);
2448 buf
= reg
->Dimension
.Index
;
2450 if (reg
->Dimension
.Indirect
) {
2451 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_const_and_shader_buffers
);
2453 index
= si_get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
2454 reg
->Dimension
.Index
,
2455 ctx
->num_const_buffers
);
2456 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
2457 LLVMConstInt(ctx
->i32
, SI_NUM_SHADER_BUFFERS
, 0), "");
2458 bufp
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, index
);
2460 bufp
= load_const_buffer_desc(ctx
, buf
);
2462 return bitcast(bld_base
, type
, buffer_load_const(ctx
, bufp
, addr
));
2465 /* Initialize arguments for the shader export intrinsic */
2466 static void si_llvm_init_export_args(struct si_shader_context
*ctx
,
2467 LLVMValueRef
*values
,
2469 struct ac_export_args
*args
)
2471 LLVMValueRef f32undef
= LLVMGetUndef(ctx
->ac
.f32
);
2472 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
2474 bool is_int8
, is_int10
;
2476 /* Default is 0xf. Adjusted below depending on the format. */
2477 args
->enabled_channels
= 0xf; /* writemask */
2479 /* Specify whether the EXEC mask represents the valid mask */
2480 args
->valid_mask
= 0;
2482 /* Specify whether this is the last export */
2485 /* Specify the target we are exporting */
2486 args
->target
= target
;
2488 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
2489 const struct si_shader_key
*key
= &ctx
->shader
->key
;
2490 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
2491 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
2493 assert(cbuf
>= 0 && cbuf
< 8);
2494 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
2495 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
2496 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
2499 args
->compr
= false;
2500 args
->out
[0] = f32undef
;
2501 args
->out
[1] = f32undef
;
2502 args
->out
[2] = f32undef
;
2503 args
->out
[3] = f32undef
;
2505 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
2506 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
2507 unsigned bits
, bool hi
) = NULL
;
2509 switch (spi_shader_col_format
) {
2510 case V_028714_SPI_SHADER_ZERO
:
2511 args
->enabled_channels
= 0; /* writemask */
2512 args
->target
= V_008DFC_SQ_EXP_NULL
;
2515 case V_028714_SPI_SHADER_32_R
:
2516 args
->enabled_channels
= 1; /* writemask */
2517 args
->out
[0] = values
[0];
2520 case V_028714_SPI_SHADER_32_GR
:
2521 args
->enabled_channels
= 0x3; /* writemask */
2522 args
->out
[0] = values
[0];
2523 args
->out
[1] = values
[1];
2526 case V_028714_SPI_SHADER_32_AR
:
2527 args
->enabled_channels
= 0x9; /* writemask */
2528 args
->out
[0] = values
[0];
2529 args
->out
[3] = values
[3];
2532 case V_028714_SPI_SHADER_FP16_ABGR
:
2533 packf
= ac_build_cvt_pkrtz_f16
;
2536 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2537 packf
= ac_build_cvt_pknorm_u16
;
2540 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2541 packf
= ac_build_cvt_pknorm_i16
;
2544 case V_028714_SPI_SHADER_UINT16_ABGR
:
2545 packi
= ac_build_cvt_pk_u16
;
2548 case V_028714_SPI_SHADER_SINT16_ABGR
:
2549 packi
= ac_build_cvt_pk_i16
;
2552 case V_028714_SPI_SHADER_32_ABGR
:
2553 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2557 /* Pack f16 or norm_i16/u16. */
2559 for (chan
= 0; chan
< 2; chan
++) {
2560 LLVMValueRef pack_args
[2] = {
2562 values
[2 * chan
+ 1]
2564 LLVMValueRef packed
;
2566 packed
= packf(&ctx
->ac
, pack_args
);
2567 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2569 args
->compr
= 1; /* COMPR flag */
2573 for (chan
= 0; chan
< 2; chan
++) {
2574 LLVMValueRef pack_args
[2] = {
2575 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
2576 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
2578 LLVMValueRef packed
;
2580 packed
= packi(&ctx
->ac
, pack_args
,
2581 is_int8
? 8 : is_int10
? 10 : 16,
2583 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2585 args
->compr
= 1; /* COMPR flag */
2589 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2592 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2594 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2595 static LLVMRealPredicate cond_map
[PIPE_FUNC_ALWAYS
+ 1] = {
2596 [PIPE_FUNC_LESS
] = LLVMRealOLT
,
2597 [PIPE_FUNC_EQUAL
] = LLVMRealOEQ
,
2598 [PIPE_FUNC_LEQUAL
] = LLVMRealOLE
,
2599 [PIPE_FUNC_GREATER
] = LLVMRealOGT
,
2600 [PIPE_FUNC_NOTEQUAL
] = LLVMRealONE
,
2601 [PIPE_FUNC_GEQUAL
] = LLVMRealOGE
,
2603 LLVMRealPredicate cond
= cond_map
[ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
];
2606 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2607 SI_PARAM_ALPHA_REF
);
2608 LLVMValueRef alpha_pass
=
2609 LLVMBuildFCmp(ctx
->ac
.builder
, cond
, alpha
, alpha_ref
, "");
2610 ac_build_kill_if_false(&ctx
->ac
, alpha_pass
);
2612 ac_build_kill_if_false(&ctx
->ac
, LLVMConstInt(ctx
->i1
, 0, 0));
2616 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2618 unsigned samplemask_param
)
2620 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2621 LLVMValueRef coverage
;
2623 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2624 coverage
= LLVMGetParam(ctx
->main_fn
,
2626 coverage
= ac_to_integer(&ctx
->ac
, coverage
);
2628 coverage
= ac_build_intrinsic(&ctx
->ac
, "llvm.ctpop.i32",
2630 &coverage
, 1, AC_FUNC_ATTR_READNONE
);
2632 coverage
= LLVMBuildUIToFP(ctx
->ac
.builder
, coverage
,
2635 coverage
= LLVMBuildFMul(ctx
->ac
.builder
, coverage
,
2636 LLVMConstReal(ctx
->f32
,
2637 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2639 return LLVMBuildFMul(ctx
->ac
.builder
, alpha
, coverage
, "");
2642 static void si_llvm_emit_clipvertex(struct si_shader_context
*ctx
,
2643 struct ac_export_args
*pos
, LLVMValueRef
*out_elts
)
2647 unsigned const_chan
;
2648 LLVMValueRef base_elt
;
2649 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
2650 LLVMValueRef constbuf_index
= LLVMConstInt(ctx
->i32
,
2651 SI_VS_CONST_CLIP_PLANES
, 0);
2652 LLVMValueRef const_resource
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
, constbuf_index
);
2654 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2655 struct ac_export_args
*args
= &pos
[2 + reg_index
];
2660 args
->out
[3] = LLVMConstReal(ctx
->f32
, 0.0f
);
2662 /* Compute dot products of position and user clip plane vectors */
2663 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2664 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2666 LLVMConstInt(ctx
->i32
, ((reg_index
* 4 + chan
) * 4 +
2667 const_chan
) * 4, 0);
2668 base_elt
= buffer_load_const(ctx
, const_resource
,
2670 args
->out
[chan
] = ac_build_fmad(&ctx
->ac
, base_elt
,
2671 out_elts
[const_chan
], args
->out
[chan
]);
2675 args
->enabled_channels
= 0xf;
2676 args
->valid_mask
= 0;
2678 args
->target
= V_008DFC_SQ_EXP_POS
+ 2 + reg_index
;
2683 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2687 if (so
->num_outputs
)
2688 fprintf(stderr
, "STREAMOUT\n");
2690 for (i
= 0; i
< so
->num_outputs
; i
++) {
2691 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2692 so
->output
[i
].start_component
;
2693 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2694 i
, so
->output
[i
].output_buffer
,
2695 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2696 so
->output
[i
].register_index
,
2697 mask
& 1 ? "x" : "",
2698 mask
& 2 ? "y" : "",
2699 mask
& 4 ? "z" : "",
2700 mask
& 8 ? "w" : "");
2704 static void emit_streamout_output(struct si_shader_context
*ctx
,
2705 LLVMValueRef
const *so_buffers
,
2706 LLVMValueRef
const *so_write_offsets
,
2707 struct pipe_stream_output
*stream_out
,
2708 struct si_shader_output_values
*shader_out
)
2710 unsigned buf_idx
= stream_out
->output_buffer
;
2711 unsigned start
= stream_out
->start_component
;
2712 unsigned num_comps
= stream_out
->num_components
;
2713 LLVMValueRef out
[4];
2715 assert(num_comps
&& num_comps
<= 4);
2716 if (!num_comps
|| num_comps
> 4)
2719 /* Load the output as int. */
2720 for (int j
= 0; j
< num_comps
; j
++) {
2721 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2723 out
[j
] = ac_to_integer(&ctx
->ac
, shader_out
->values
[start
+ j
]);
2726 /* Pack the output. */
2727 LLVMValueRef vdata
= NULL
;
2729 switch (num_comps
) {
2730 case 1: /* as i32 */
2733 case 2: /* as v2i32 */
2734 case 3: /* as v4i32 (aligned to 4) */
2735 out
[3] = LLVMGetUndef(ctx
->i32
);
2737 case 4: /* as v4i32 */
2738 vdata
= ac_build_gather_values(&ctx
->ac
, out
, util_next_power_of_two(num_comps
));
2742 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf_idx
],
2744 so_write_offsets
[buf_idx
],
2746 stream_out
->dst_offset
* 4, 1, 1, true, false);
2750 * Write streamout data to buffers for vertex stream @p stream (different
2751 * vertex streams can occur for GS copy shaders).
2753 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2754 struct si_shader_output_values
*outputs
,
2755 unsigned noutput
, unsigned stream
)
2757 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2758 struct pipe_stream_output_info
*so
= &sel
->so
;
2759 LLVMBuilderRef builder
= ctx
->ac
.builder
;
2761 struct lp_build_if_state if_ctx
;
2763 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2764 LLVMValueRef so_vtx_count
=
2765 si_unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2767 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2769 /* can_emit = tid < so_vtx_count; */
2770 LLVMValueRef can_emit
=
2771 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2773 /* Emit the streamout code conditionally. This actually avoids
2774 * out-of-bounds buffer access. The hw tells us via the SGPR
2775 * (so_vtx_count) which threads are allowed to emit streamout data. */
2776 lp_build_if(&if_ctx
, &ctx
->gallivm
, can_emit
);
2778 /* The buffer offset is computed as follows:
2779 * ByteOffset = streamout_offset[buffer_id]*4 +
2780 * (streamout_write_index + thread_id)*stride[buffer_id] +
2784 LLVMValueRef so_write_index
=
2785 LLVMGetParam(ctx
->main_fn
,
2786 ctx
->param_streamout_write_index
);
2788 /* Compute (streamout_write_index + thread_id). */
2789 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2791 /* Load the descriptor and compute the write offset for each
2792 * enabled buffer. */
2793 LLVMValueRef so_write_offset
[4] = {};
2794 LLVMValueRef so_buffers
[4];
2795 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2796 ctx
->param_rw_buffers
);
2798 for (i
= 0; i
< 4; i
++) {
2802 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
,
2803 SI_VS_STREAMOUT_BUF0
+ i
, 0);
2805 so_buffers
[i
] = ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
2807 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2808 ctx
->param_streamout_offset
[i
]);
2809 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2811 so_write_offset
[i
] = ac_build_imad(&ctx
->ac
, so_write_index
,
2812 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0),
2816 /* Write streamout data. */
2817 for (i
= 0; i
< so
->num_outputs
; i
++) {
2818 unsigned reg
= so
->output
[i
].register_index
;
2823 if (stream
!= so
->output
[i
].stream
)
2826 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2827 &so
->output
[i
], &outputs
[reg
]);
2830 lp_build_endif(&if_ctx
);
2833 static void si_export_param(struct si_shader_context
*ctx
, unsigned index
,
2834 LLVMValueRef
*values
)
2836 struct ac_export_args args
;
2838 si_llvm_init_export_args(ctx
, values
,
2839 V_008DFC_SQ_EXP_PARAM
+ index
, &args
);
2840 ac_build_export(&ctx
->ac
, &args
);
2843 static void si_build_param_exports(struct si_shader_context
*ctx
,
2844 struct si_shader_output_values
*outputs
,
2847 struct si_shader
*shader
= ctx
->shader
;
2848 unsigned param_count
= 0;
2850 for (unsigned i
= 0; i
< noutput
; i
++) {
2851 unsigned semantic_name
= outputs
[i
].semantic_name
;
2852 unsigned semantic_index
= outputs
[i
].semantic_index
;
2854 if (outputs
[i
].vertex_stream
[0] != 0 &&
2855 outputs
[i
].vertex_stream
[1] != 0 &&
2856 outputs
[i
].vertex_stream
[2] != 0 &&
2857 outputs
[i
].vertex_stream
[3] != 0)
2860 switch (semantic_name
) {
2861 case TGSI_SEMANTIC_LAYER
:
2862 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2863 case TGSI_SEMANTIC_CLIPDIST
:
2864 case TGSI_SEMANTIC_COLOR
:
2865 case TGSI_SEMANTIC_BCOLOR
:
2866 case TGSI_SEMANTIC_PRIMID
:
2867 case TGSI_SEMANTIC_FOG
:
2868 case TGSI_SEMANTIC_TEXCOORD
:
2869 case TGSI_SEMANTIC_GENERIC
:
2875 if ((semantic_name
!= TGSI_SEMANTIC_GENERIC
||
2876 semantic_index
< SI_MAX_IO_GENERIC
) &&
2877 shader
->key
.opt
.kill_outputs
&
2878 (1ull << si_shader_io_get_unique_index(semantic_name
,
2879 semantic_index
, true)))
2882 si_export_param(ctx
, param_count
, outputs
[i
].values
);
2884 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2885 shader
->info
.vs_output_param_offset
[i
] = param_count
++;
2888 shader
->info
.nr_param_exports
= param_count
;
2891 /* Generate export instructions for hardware VS shader stage */
2892 static void si_llvm_export_vs(struct si_shader_context
*ctx
,
2893 struct si_shader_output_values
*outputs
,
2896 struct si_shader
*shader
= ctx
->shader
;
2897 struct ac_export_args pos_args
[4] = {};
2898 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2902 /* Build position exports. */
2903 for (i
= 0; i
< noutput
; i
++) {
2904 switch (outputs
[i
].semantic_name
) {
2905 case TGSI_SEMANTIC_POSITION
:
2906 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2907 V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
2909 case TGSI_SEMANTIC_PSIZE
:
2910 psize_value
= outputs
[i
].values
[0];
2912 case TGSI_SEMANTIC_LAYER
:
2913 layer_value
= outputs
[i
].values
[0];
2915 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2916 viewport_index_value
= outputs
[i
].values
[0];
2918 case TGSI_SEMANTIC_EDGEFLAG
:
2919 edgeflag_value
= outputs
[i
].values
[0];
2921 case TGSI_SEMANTIC_CLIPDIST
:
2922 if (!shader
->key
.opt
.clip_disable
) {
2923 unsigned index
= 2 + outputs
[i
].semantic_index
;
2924 si_llvm_init_export_args(ctx
, outputs
[i
].values
,
2925 V_008DFC_SQ_EXP_POS
+ index
,
2929 case TGSI_SEMANTIC_CLIPVERTEX
:
2930 if (!shader
->key
.opt
.clip_disable
) {
2931 si_llvm_emit_clipvertex(ctx
, pos_args
,
2938 /* We need to add the position output manually if it's missing. */
2939 if (!pos_args
[0].out
[0]) {
2940 pos_args
[0].enabled_channels
= 0xf; /* writemask */
2941 pos_args
[0].valid_mask
= 0; /* EXEC mask */
2942 pos_args
[0].done
= 0; /* last export? */
2943 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
2944 pos_args
[0].compr
= 0; /* COMPR flag */
2945 pos_args
[0].out
[0] = ctx
->ac
.f32_0
; /* X */
2946 pos_args
[0].out
[1] = ctx
->ac
.f32_0
; /* Y */
2947 pos_args
[0].out
[2] = ctx
->ac
.f32_0
; /* Z */
2948 pos_args
[0].out
[3] = ctx
->ac
.f32_1
; /* W */
2951 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2952 if (shader
->selector
->info
.writes_psize
||
2953 shader
->selector
->info
.writes_edgeflag
||
2954 shader
->selector
->info
.writes_viewport_index
||
2955 shader
->selector
->info
.writes_layer
) {
2956 pos_args
[1].enabled_channels
= shader
->selector
->info
.writes_psize
|
2957 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2958 (shader
->selector
->info
.writes_layer
<< 2);
2960 pos_args
[1].valid_mask
= 0; /* EXEC mask */
2961 pos_args
[1].done
= 0; /* last export? */
2962 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
2963 pos_args
[1].compr
= 0; /* COMPR flag */
2964 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
2965 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
2966 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
2967 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
2969 if (shader
->selector
->info
.writes_psize
)
2970 pos_args
[1].out
[0] = psize_value
;
2972 if (shader
->selector
->info
.writes_edgeflag
) {
2973 /* The output is a float, but the hw expects an integer
2974 * with the first bit containing the edge flag. */
2975 edgeflag_value
= LLVMBuildFPToUI(ctx
->ac
.builder
,
2978 edgeflag_value
= ac_build_umin(&ctx
->ac
,
2982 /* The LLVM intrinsic expects a float. */
2983 pos_args
[1].out
[1] = ac_to_float(&ctx
->ac
, edgeflag_value
);
2986 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
2987 /* GFX9 has the layer in out.z[10:0] and the viewport
2988 * index in out.z[19:16].
2990 if (shader
->selector
->info
.writes_layer
)
2991 pos_args
[1].out
[2] = layer_value
;
2993 if (shader
->selector
->info
.writes_viewport_index
) {
2994 LLVMValueRef v
= viewport_index_value
;
2996 v
= ac_to_integer(&ctx
->ac
, v
);
2997 v
= LLVMBuildShl(ctx
->ac
.builder
, v
,
2998 LLVMConstInt(ctx
->i32
, 16, 0), "");
2999 v
= LLVMBuildOr(ctx
->ac
.builder
, v
,
3000 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
3001 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
3002 pos_args
[1].enabled_channels
|= 1 << 2;
3005 if (shader
->selector
->info
.writes_layer
)
3006 pos_args
[1].out
[2] = layer_value
;
3008 if (shader
->selector
->info
.writes_viewport_index
) {
3009 pos_args
[1].out
[3] = viewport_index_value
;
3010 pos_args
[1].enabled_channels
|= 1 << 3;
3015 for (i
= 0; i
< 4; i
++)
3016 if (pos_args
[i
].out
[0])
3017 shader
->info
.nr_pos_exports
++;
3020 for (i
= 0; i
< 4; i
++) {
3021 if (!pos_args
[i
].out
[0])
3024 /* Specify the target we are exporting */
3025 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
3027 if (pos_idx
== shader
->info
.nr_pos_exports
)
3028 /* Specify that this is the last export */
3029 pos_args
[i
].done
= 1;
3031 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
3034 /* Build parameter exports. */
3035 si_build_param_exports(ctx
, outputs
, noutput
);
3039 * Forward all outputs from the vertex shader to the TES. This is only used
3040 * for the fixed function TCS.
3042 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
3044 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3045 LLVMValueRef invocation_id
, buffer
, buffer_offset
;
3046 LLVMValueRef lds_vertex_stride
, lds_base
;
3049 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3050 buffer
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3051 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3053 lds_vertex_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3054 lds_base
= get_tcs_in_current_patch_offset(ctx
);
3055 lds_base
= ac_build_imad(&ctx
->ac
, invocation_id
, lds_vertex_stride
,
3058 inputs
= ctx
->shader
->key
.mono
.u
.ff_tcs_inputs_to_copy
;
3060 unsigned i
= u_bit_scan64(&inputs
);
3062 LLVMValueRef lds_ptr
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3063 LLVMConstInt(ctx
->i32
, 4 * i
, 0),
3066 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
3067 get_rel_patch_id(ctx
),
3069 LLVMConstInt(ctx
->i32
, i
, 0));
3071 LLVMValueRef value
= lds_load(bld_base
, ctx
->ac
.i32
, ~0,
3074 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buffer_addr
,
3075 buffer_offset
, 0, 1, 0, true, false);
3079 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
3080 LLVMValueRef rel_patch_id
,
3081 LLVMValueRef invocation_id
,
3082 LLVMValueRef tcs_out_current_patch_data_offset
,
3083 LLVMValueRef invoc0_tf_outer
[4],
3084 LLVMValueRef invoc0_tf_inner
[2])
3086 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3087 struct si_shader
*shader
= ctx
->shader
;
3088 unsigned tess_inner_index
, tess_outer_index
;
3089 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
3090 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
3091 unsigned stride
, outer_comps
, inner_comps
, i
, offset
;
3092 struct lp_build_if_state if_ctx
, inner_if_ctx
;
3094 /* Add a barrier before loading tess factors from LDS. */
3095 if (!shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
)
3096 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
3098 /* Do this only for invocation 0, because the tess levels are per-patch,
3101 * This can't jump, because invocation 0 executes this. It should
3102 * at least mask out the loads and stores for other invocations.
3104 lp_build_if(&if_ctx
, &ctx
->gallivm
,
3105 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3106 invocation_id
, ctx
->i32_0
, ""));
3108 /* Determine the layout of one tess factor element in the buffer. */
3109 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
3110 case PIPE_PRIM_LINES
:
3111 stride
= 2; /* 2 dwords, 1 vec2 store */
3115 case PIPE_PRIM_TRIANGLES
:
3116 stride
= 4; /* 4 dwords, 1 vec4 store */
3120 case PIPE_PRIM_QUADS
:
3121 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
3130 for (i
= 0; i
< 4; i
++) {
3131 inner
[i
] = LLVMGetUndef(ctx
->i32
);
3132 outer
[i
] = LLVMGetUndef(ctx
->i32
);
3135 if (shader
->key
.part
.tcs
.epilog
.invoc0_tess_factors_are_def
) {
3136 /* Tess factors are in VGPRs. */
3137 for (i
= 0; i
< outer_comps
; i
++)
3138 outer
[i
] = out
[i
] = invoc0_tf_outer
[i
];
3139 for (i
= 0; i
< inner_comps
; i
++)
3140 inner
[i
] = out
[outer_comps
+i
] = invoc0_tf_inner
[i
];
3142 /* Load tess_inner and tess_outer from LDS.
3143 * Any invocation can write them, so we can't get them from a temporary.
3145 tess_inner_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER
, 0);
3146 tess_outer_index
= si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER
, 0);
3148 lds_base
= tcs_out_current_patch_data_offset
;
3149 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3150 LLVMConstInt(ctx
->i32
,
3151 tess_inner_index
* 4, 0), "");
3152 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3153 LLVMConstInt(ctx
->i32
,
3154 tess_outer_index
* 4, 0), "");
3156 for (i
= 0; i
< outer_comps
; i
++) {
3158 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_outer
);
3160 for (i
= 0; i
< inner_comps
; i
++) {
3161 inner
[i
] = out
[outer_comps
+i
] =
3162 lds_load(bld_base
, ctx
->ac
.i32
, i
, lds_inner
);
3166 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
3167 /* For isolines, the hardware expects tess factors in the
3168 * reverse order from what GLSL / TGSI specify.
3170 LLVMValueRef tmp
= out
[0];
3175 /* Convert the outputs to vectors for stores. */
3176 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
3180 vec1
= ac_build_gather_values(&ctx
->ac
, out
+4, stride
- 4);
3182 /* Get the buffer. */
3183 buffer
= get_tess_ring_descriptor(ctx
, TCS_FACTOR_RING
);
3185 /* Get the offset. */
3186 tf_base
= LLVMGetParam(ctx
->main_fn
,
3187 ctx
->param_tcs_factor_offset
);
3188 byteoffset
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
3189 LLVMConstInt(ctx
->i32
, 4 * stride
, 0), "");
3191 lp_build_if(&inner_if_ctx
, &ctx
->gallivm
,
3192 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3193 rel_patch_id
, ctx
->i32_0
, ""));
3195 /* Store the dynamic HS control word. */
3197 if (ctx
->screen
->info
.chip_class
<= VI
) {
3198 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
3199 LLVMConstInt(ctx
->i32
, 0x80000000, 0),
3200 1, ctx
->i32_0
, tf_base
,
3201 offset
, 1, 0, true, false);
3205 lp_build_endif(&inner_if_ctx
);
3207 /* Store the tessellation factors. */
3208 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
3209 MIN2(stride
, 4), byteoffset
, tf_base
,
3210 offset
, 1, 0, true, false);
3213 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
3214 stride
- 4, byteoffset
, tf_base
,
3215 offset
, 1, 0, true, false);
3217 /* Store the tess factors into the offchip buffer if TES reads them. */
3218 if (shader
->key
.part
.tcs
.epilog
.tes_reads_tess_factors
) {
3219 LLVMValueRef buf
, base
, inner_vec
, outer_vec
, tf_outer_offset
;
3220 LLVMValueRef tf_inner_offset
;
3221 unsigned param_outer
, param_inner
;
3223 buf
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TCS
);
3224 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_tcs_offchip_offset
);
3226 param_outer
= si_shader_io_get_unique_index_patch(
3227 TGSI_SEMANTIC_TESSOUTER
, 0);
3228 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3229 LLVMConstInt(ctx
->i32
, param_outer
, 0));
3231 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
3232 util_next_power_of_two(outer_comps
));
3234 ac_build_buffer_store_dword(&ctx
->ac
, buf
, outer_vec
,
3235 outer_comps
, tf_outer_offset
,
3236 base
, 0, 1, 0, true, false);
3238 param_inner
= si_shader_io_get_unique_index_patch(
3239 TGSI_SEMANTIC_TESSINNER
, 0);
3240 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
3241 LLVMConstInt(ctx
->i32
, param_inner
, 0));
3243 inner_vec
= inner_comps
== 1 ? inner
[0] :
3244 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
3245 ac_build_buffer_store_dword(&ctx
->ac
, buf
, inner_vec
,
3246 inner_comps
, tf_inner_offset
,
3247 base
, 0, 1, 0, true, false);
3251 lp_build_endif(&if_ctx
);
3255 si_insert_input_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3256 unsigned param
, unsigned return_index
)
3258 return LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3259 LLVMGetParam(ctx
->main_fn
, param
),
3264 si_insert_input_ret_float(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3265 unsigned param
, unsigned return_index
)
3267 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3268 LLVMValueRef p
= LLVMGetParam(ctx
->main_fn
, param
);
3270 return LLVMBuildInsertValue(builder
, ret
,
3271 ac_to_float(&ctx
->ac
, p
),
3276 si_insert_input_ptr(struct si_shader_context
*ctx
, LLVMValueRef ret
,
3277 unsigned param
, unsigned return_index
)
3279 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3280 LLVMValueRef ptr
, lo
, hi
;
3282 if (HAVE_32BIT_POINTERS
) {
3283 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3284 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i32
, "");
3285 return LLVMBuildInsertValue(builder
, ret
, ptr
, return_index
, "");
3288 ptr
= LLVMGetParam(ctx
->main_fn
, param
);
3289 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->i64
, "");
3290 ptr
= LLVMBuildBitCast(builder
, ptr
, ctx
->v2i32
, "");
3291 lo
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_0
, "");
3292 hi
= LLVMBuildExtractElement(builder
, ptr
, ctx
->i32_1
, "");
3293 ret
= LLVMBuildInsertValue(builder
, ret
, lo
, return_index
, "");
3294 return LLVMBuildInsertValue(builder
, ret
, hi
, return_index
+ 1, "");
3297 /* This only writes the tessellation factor levels. */
3298 static void si_llvm_emit_tcs_epilogue(struct ac_shader_abi
*abi
,
3299 unsigned max_outputs
,
3300 LLVMValueRef
*addrs
)
3302 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3303 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
3304 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3305 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
3307 si_copy_tcs_inputs(bld_base
);
3309 rel_patch_id
= get_rel_patch_id(ctx
);
3310 invocation_id
= unpack_llvm_param(ctx
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3311 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
3313 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3314 LLVMBasicBlockRef blocks
[2] = {
3315 LLVMGetInsertBlock(builder
),
3316 ctx
->merged_wrap_if_state
.entry_block
3318 LLVMValueRef values
[2];
3320 lp_build_endif(&ctx
->merged_wrap_if_state
);
3322 values
[0] = rel_patch_id
;
3323 values
[1] = LLVMGetUndef(ctx
->i32
);
3324 rel_patch_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3326 values
[0] = tf_lds_offset
;
3327 values
[1] = LLVMGetUndef(ctx
->i32
);
3328 tf_lds_offset
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3330 values
[0] = invocation_id
;
3331 values
[1] = ctx
->i32_1
; /* cause the epilog to skip threads */
3332 invocation_id
= ac_build_phi(&ctx
->ac
, ctx
->i32
, 2, values
, blocks
);
3335 /* Return epilog parameters from this function. */
3336 LLVMValueRef ret
= ctx
->return_value
;
3339 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3340 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3341 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3342 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3343 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3344 /* Tess offchip and tess factor offsets are at the beginning. */
3345 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3346 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3347 vgpr
= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
+ 1;
3349 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3350 GFX6_SGPR_TCS_OFFCHIP_LAYOUT
);
3351 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3352 GFX6_SGPR_TCS_OUT_LAYOUT
);
3353 /* Tess offchip and tess factor offsets are after user SGPRs. */
3354 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
,
3355 GFX6_TCS_NUM_USER_SGPR
);
3356 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
,
3357 GFX6_TCS_NUM_USER_SGPR
+ 1);
3358 vgpr
= GFX6_TCS_NUM_USER_SGPR
+ 2;
3362 rel_patch_id
= ac_to_float(&ctx
->ac
, rel_patch_id
);
3363 invocation_id
= ac_to_float(&ctx
->ac
, invocation_id
);
3364 tf_lds_offset
= ac_to_float(&ctx
->ac
, tf_lds_offset
);
3366 /* Leave a hole corresponding to the two input VGPRs. This ensures that
3367 * the invocation_id output does not alias the tcs_rel_ids input,
3368 * which saves a V_MOV on gfx9.
3372 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
3373 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
3375 if (ctx
->shader
->selector
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
3376 vgpr
++; /* skip the tess factor LDS offset */
3377 for (unsigned i
= 0; i
< 6; i
++) {
3378 LLVMValueRef value
=
3379 LLVMBuildLoad(builder
, ctx
->invoc0_tess_factors
[i
], "");
3380 value
= ac_to_float(&ctx
->ac
, value
);
3381 ret
= LLVMBuildInsertValue(builder
, ret
, value
, vgpr
++, "");
3384 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
3386 ctx
->return_value
= ret
;
3389 /* Pass TCS inputs from LS to TCS on GFX9. */
3390 static void si_set_ls_return_value_for_tcs(struct si_shader_context
*ctx
)
3392 LLVMValueRef ret
= ctx
->return_value
;
3394 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3395 if (HAVE_32BIT_POINTERS
)
3396 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3397 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_offset
, 2);
3398 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3399 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_factor_offset
, 4);
3400 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3402 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3403 8 + SI_SGPR_RW_BUFFERS
);
3404 ret
= si_insert_input_ptr(ctx
, ret
,
3405 ctx
->param_bindless_samplers_and_images
,
3406 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3408 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_vs_state_bits
,
3409 8 + SI_SGPR_VS_STATE_BITS
);
3411 #if !HAVE_32BIT_POINTERS
3412 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 4,
3413 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3416 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_offchip_layout
,
3417 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT
);
3418 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_offsets
,
3419 8 + GFX9_SGPR_TCS_OUT_OFFSETS
);
3420 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_tcs_out_lds_layout
,
3421 8 + GFX9_SGPR_TCS_OUT_LAYOUT
);
3423 unsigned vgpr
= 8 + GFX9_TCS_NUM_USER_SGPR
;
3424 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3425 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_patch_id
),
3427 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
3428 ac_to_float(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
),
3430 ctx
->return_value
= ret
;
3433 /* Pass GS inputs from ES to GS on GFX9. */
3434 static void si_set_es_return_value_for_gs(struct si_shader_context
*ctx
)
3436 LLVMValueRef ret
= ctx
->return_value
;
3438 ret
= si_insert_input_ptr(ctx
, ret
, 0, 0);
3439 if (HAVE_32BIT_POINTERS
)
3440 ret
= si_insert_input_ptr(ctx
, ret
, 1, 1);
3441 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_gs2vs_offset
, 2);
3442 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_wave_info
, 3);
3443 ret
= si_insert_input_ret(ctx
, ret
, ctx
->param_merged_scratch_offset
, 5);
3445 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_rw_buffers
,
3446 8 + SI_SGPR_RW_BUFFERS
);
3447 ret
= si_insert_input_ptr(ctx
, ret
,
3448 ctx
->param_bindless_samplers_and_images
,
3449 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
);
3451 #if !HAVE_32BIT_POINTERS
3452 ret
= si_insert_input_ptr(ctx
, ret
, ctx
->param_vs_state_bits
+ 4,
3453 8 + GFX9_SGPR_2ND_SAMPLERS_AND_IMAGES
);
3457 if (ctx
->type
== PIPE_SHADER_VERTEX
)
3458 vgpr
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
3460 vgpr
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
3462 for (unsigned i
= 0; i
< 5; i
++) {
3463 unsigned param
= ctx
->param_gs_vtx01_offset
+ i
;
3464 ret
= si_insert_input_ret_float(ctx
, ret
, param
, vgpr
++);
3466 ctx
->return_value
= ret
;
3469 static void si_llvm_emit_ls_epilogue(struct ac_shader_abi
*abi
,
3470 unsigned max_outputs
,
3471 LLVMValueRef
*addrs
)
3473 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3474 struct si_shader
*shader
= ctx
->shader
;
3475 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3477 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
3478 ctx
->param_rel_auto_id
);
3479 LLVMValueRef vertex_dw_stride
= get_tcs_in_vertex_dw_stride(ctx
);
3480 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->ac
.builder
, vertex_id
,
3481 vertex_dw_stride
, "");
3483 /* Write outputs to LDS. The next shader (TCS aka HS) will read
3484 * its inputs from it. */
3485 for (i
= 0; i
< info
->num_outputs
; i
++) {
3486 unsigned name
= info
->output_semantic_name
[i
];
3487 unsigned index
= info
->output_semantic_index
[i
];
3489 /* The ARB_shader_viewport_layer_array spec contains the
3492 * 2) What happens if gl_ViewportIndex or gl_Layer is
3493 * written in the vertex shader and a geometry shader is
3496 * RESOLVED: The value written by the last vertex processing
3497 * stage is used. If the last vertex processing stage
3498 * (vertex, tessellation evaluation or geometry) does not
3499 * statically assign to gl_ViewportIndex or gl_Layer, index
3500 * or layer zero is assumed.
3502 * So writes to those outputs in VS-as-LS are simply ignored.
3504 if (name
== TGSI_SEMANTIC_LAYER
||
3505 name
== TGSI_SEMANTIC_VIEWPORT_INDEX
)
3508 int param
= si_shader_io_get_unique_index(name
, index
, false);
3509 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_dw_addr
,
3510 LLVMConstInt(ctx
->i32
, param
* 4, 0), "");
3512 for (chan
= 0; chan
< 4; chan
++) {
3513 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3516 lds_store(ctx
, chan
, dw_addr
,
3517 LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], ""));
3521 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3522 si_set_ls_return_value_for_tcs(ctx
);
3525 static void si_llvm_emit_es_epilogue(struct ac_shader_abi
*abi
,
3526 unsigned max_outputs
,
3527 LLVMValueRef
*addrs
)
3529 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3530 struct si_shader
*es
= ctx
->shader
;
3531 struct tgsi_shader_info
*info
= &es
->selector
->info
;
3532 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
3533 ctx
->param_es2gs_offset
);
3534 LLVMValueRef lds_base
= NULL
;
3538 if (ctx
->screen
->info
.chip_class
>= GFX9
&& info
->num_outputs
) {
3539 unsigned itemsize_dw
= es
->selector
->esgs_itemsize
/ 4;
3540 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
3541 LLVMValueRef wave_idx
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 24, 4);
3542 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
3543 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
3544 LLVMConstInt(ctx
->i32
, 64, false), ""), "");
3545 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
3546 LLVMConstInt(ctx
->i32
, itemsize_dw
, 0), "");
3549 for (i
= 0; i
< info
->num_outputs
; i
++) {
3552 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
3553 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
3556 param
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
3557 info
->output_semantic_index
[i
], false);
3559 for (chan
= 0; chan
< 4; chan
++) {
3560 if (!(info
->output_usagemask
[i
] & (1 << chan
)))
3563 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
3564 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
3566 /* GFX9 has the ESGS ring in LDS. */
3567 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
3568 lds_store(ctx
, param
* 4 + chan
, lds_base
, out_val
);
3572 ac_build_buffer_store_dword(&ctx
->ac
,
3574 out_val
, 1, NULL
, soffset
,
3575 (4 * param
+ chan
) * 4,
3580 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3581 si_set_es_return_value_for_gs(ctx
);
3584 static LLVMValueRef
si_get_gs_wave_id(struct si_shader_context
*ctx
)
3586 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3587 return si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 16, 8);
3589 return LLVMGetParam(ctx
->main_fn
, ctx
->param_gs_wave_id
);
3592 static void emit_gs_epilogue(struct si_shader_context
*ctx
)
3594 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
,
3595 si_get_gs_wave_id(ctx
));
3597 if (ctx
->screen
->info
.chip_class
>= GFX9
)
3598 lp_build_endif(&ctx
->merged_wrap_if_state
);
3601 static void si_llvm_emit_gs_epilogue(struct ac_shader_abi
*abi
,
3602 unsigned max_outputs
,
3603 LLVMValueRef
*addrs
)
3605 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3606 struct tgsi_shader_info UNUSED
*info
= &ctx
->shader
->selector
->info
;
3608 assert(info
->num_outputs
<= max_outputs
);
3610 emit_gs_epilogue(ctx
);
3613 static void si_tgsi_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
3615 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3616 emit_gs_epilogue(ctx
);
3619 static void si_llvm_emit_vs_epilogue(struct ac_shader_abi
*abi
,
3620 unsigned max_outputs
,
3621 LLVMValueRef
*addrs
)
3623 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3624 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
3625 struct si_shader_output_values
*outputs
= NULL
;
3628 assert(!ctx
->shader
->is_gs_copy_shader
);
3629 assert(info
->num_outputs
<= max_outputs
);
3631 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
3633 /* Vertex color clamping.
3635 * This uses a state constant loaded in a user data SGPR and
3636 * an IF statement is added that clamps all colors if the constant
3639 struct lp_build_if_state if_ctx
;
3640 LLVMValueRef cond
= NULL
;
3641 LLVMValueRef addr
, val
;
3643 for (i
= 0; i
< info
->num_outputs
; i
++) {
3644 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
3645 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
3648 /* We've found a color. */
3650 /* The state is in the first bit of the user SGPR. */
3651 cond
= LLVMGetParam(ctx
->main_fn
,
3652 ctx
->param_vs_state_bits
);
3653 cond
= LLVMBuildTrunc(ctx
->ac
.builder
, cond
,
3655 lp_build_if(&if_ctx
, &ctx
->gallivm
, cond
);
3658 for (j
= 0; j
< 4; j
++) {
3659 addr
= addrs
[4 * i
+ j
];
3660 val
= LLVMBuildLoad(ctx
->ac
.builder
, addr
, "");
3661 val
= ac_build_clamp(&ctx
->ac
, val
);
3662 LLVMBuildStore(ctx
->ac
.builder
, val
, addr
);
3667 lp_build_endif(&if_ctx
);
3669 for (i
= 0; i
< info
->num_outputs
; i
++) {
3670 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
3671 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
3673 for (j
= 0; j
< 4; j
++) {
3674 outputs
[i
].values
[j
] =
3675 LLVMBuildLoad(ctx
->ac
.builder
,
3678 outputs
[i
].vertex_stream
[j
] =
3679 (info
->output_streams
[i
] >> (2 * j
)) & 3;
3683 if (ctx
->shader
->selector
->so
.num_outputs
)
3684 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
3686 /* Export PrimitiveID. */
3687 if (ctx
->shader
->key
.mono
.u
.vs_export_prim_id
) {
3688 outputs
[i
].semantic_name
= TGSI_SEMANTIC_PRIMID
;
3689 outputs
[i
].semantic_index
= 0;
3690 outputs
[i
].values
[0] = ac_to_float(&ctx
->ac
, get_primitive_id(ctx
, 0));
3691 for (j
= 1; j
< 4; j
++)
3692 outputs
[i
].values
[j
] = LLVMConstReal(ctx
->f32
, 0);
3694 memset(outputs
[i
].vertex_stream
, 0,
3695 sizeof(outputs
[i
].vertex_stream
));
3699 si_llvm_export_vs(ctx
, outputs
, i
);
3703 static void si_tgsi_emit_epilogue(struct lp_build_tgsi_context
*bld_base
)
3705 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3707 ctx
->abi
.emit_outputs(&ctx
->abi
, RADEON_LLVM_MAX_OUTPUTS
,
3708 &ctx
->outputs
[0][0]);
3711 struct si_ps_exports
{
3713 struct ac_export_args args
[10];
3716 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
3717 LLVMValueRef depth
, LLVMValueRef stencil
,
3718 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
3720 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3721 struct ac_export_args args
;
3723 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
3725 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3728 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
3729 LLVMValueRef
*color
, unsigned index
,
3730 unsigned samplemask_param
,
3731 bool is_last
, struct si_ps_exports
*exp
)
3733 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3737 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
3738 for (i
= 0; i
< 4; i
++)
3739 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
3742 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
3743 color
[3] = ctx
->ac
.f32_1
;
3747 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
3748 si_alpha_test(bld_base
, color
[3]);
3750 /* Line & polygon smoothing */
3751 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
3752 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
3755 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3756 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
3757 struct ac_export_args args
[8];
3760 /* Get the export arguments, also find out what the last one is. */
3761 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3762 si_llvm_init_export_args(ctx
, color
,
3763 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
3764 if (args
[c
].enabled_channels
)
3768 /* Emit all exports. */
3769 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3770 if (is_last
&& last
== c
) {
3771 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
3772 args
[c
].done
= 1; /* DONE bit */
3773 } else if (!args
[c
].enabled_channels
)
3774 continue; /* unnecessary NULL export */
3776 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
3779 struct ac_export_args args
;
3782 si_llvm_init_export_args(ctx
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3785 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
3786 args
.done
= 1; /* DONE bit */
3787 } else if (!args
.enabled_channels
)
3788 return; /* unnecessary NULL export */
3790 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
3794 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3795 struct si_ps_exports
*exp
)
3797 for (unsigned i
= 0; i
< exp
->num
; i
++)
3798 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
3802 * Return PS outputs in this order:
3804 * v[0:3] = color0.xyzw
3805 * v[4:7] = color1.xyzw
3810 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3812 * The alpha-ref SGPR is returned via its original location.
3814 static void si_llvm_return_fs_outputs(struct ac_shader_abi
*abi
,
3815 unsigned max_outputs
,
3816 LLVMValueRef
*addrs
)
3818 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
3819 struct si_shader
*shader
= ctx
->shader
;
3820 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3821 LLVMBuilderRef builder
= ctx
->ac
.builder
;
3822 unsigned i
, j
, first_vgpr
, vgpr
;
3824 LLVMValueRef color
[8][4] = {};
3825 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3828 if (ctx
->postponed_kill
)
3829 ac_build_kill_if_false(&ctx
->ac
, LLVMBuildLoad(builder
, ctx
->postponed_kill
, ""));
3831 /* Read the output values. */
3832 for (i
= 0; i
< info
->num_outputs
; i
++) {
3833 unsigned semantic_name
= info
->output_semantic_name
[i
];
3834 unsigned semantic_index
= info
->output_semantic_index
[i
];
3836 switch (semantic_name
) {
3837 case TGSI_SEMANTIC_COLOR
:
3838 assert(semantic_index
< 8);
3839 for (j
= 0; j
< 4; j
++) {
3840 LLVMValueRef ptr
= addrs
[4 * i
+ j
];
3841 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3842 color
[semantic_index
][j
] = result
;
3845 case TGSI_SEMANTIC_POSITION
:
3846 depth
= LLVMBuildLoad(builder
,
3847 addrs
[4 * i
+ 2], "");
3849 case TGSI_SEMANTIC_STENCIL
:
3850 stencil
= LLVMBuildLoad(builder
,
3851 addrs
[4 * i
+ 1], "");
3853 case TGSI_SEMANTIC_SAMPLEMASK
:
3854 samplemask
= LLVMBuildLoad(builder
,
3855 addrs
[4 * i
+ 0], "");
3858 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3863 /* Fill the return structure. */
3864 ret
= ctx
->return_value
;
3867 ret
= LLVMBuildInsertValue(builder
, ret
,
3868 ac_to_integer(&ctx
->ac
,
3869 LLVMGetParam(ctx
->main_fn
,
3870 SI_PARAM_ALPHA_REF
)),
3871 SI_SGPR_ALPHA_REF
, "");
3874 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3875 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3879 for (j
= 0; j
< 4; j
++)
3880 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3883 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3885 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3887 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3889 /* Add the input sample mask for smoothing at the end. */
3890 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3891 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3892 ret
= LLVMBuildInsertValue(builder
, ret
,
3893 LLVMGetParam(ctx
->main_fn
,
3894 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3896 ctx
->return_value
= ret
;
3899 static void membar_emit(
3900 const struct lp_build_tgsi_action
*action
,
3901 struct lp_build_tgsi_context
*bld_base
,
3902 struct lp_build_emit_data
*emit_data
)
3904 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3905 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3906 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3907 unsigned waitcnt
= NOOP_WAITCNT
;
3909 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3910 waitcnt
&= VM_CNT
& LGKM_CNT
;
3912 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3913 TGSI_MEMBAR_SHADER_BUFFER
|
3914 TGSI_MEMBAR_SHADER_IMAGE
))
3917 if (flags
& TGSI_MEMBAR_SHARED
)
3918 waitcnt
&= LGKM_CNT
;
3920 if (waitcnt
!= NOOP_WAITCNT
)
3921 ac_build_waitcnt(&ctx
->ac
, waitcnt
);
3924 static void clock_emit(
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 LLVMValueRef tmp
= ac_build_shader_clock(&ctx
->ac
);
3932 emit_data
->output
[0] =
3933 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_0
, "");
3934 emit_data
->output
[1] =
3935 LLVMBuildExtractElement(ctx
->ac
.builder
, tmp
, ctx
->i32_1
, "");
3938 static void si_llvm_emit_ddxy(
3939 const struct lp_build_tgsi_action
*action
,
3940 struct lp_build_tgsi_context
*bld_base
,
3941 struct lp_build_emit_data
*emit_data
)
3943 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3944 unsigned opcode
= emit_data
->info
->opcode
;
3949 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3950 mask
= AC_TID_MASK_LEFT
;
3951 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3952 mask
= AC_TID_MASK_TOP
;
3954 mask
= AC_TID_MASK_TOP_LEFT
;
3956 /* for DDX we want to next X pixel, DDY next Y pixel. */
3957 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3959 val
= ac_to_integer(&ctx
->ac
, emit_data
->args
[0]);
3960 val
= ac_build_ddxy(&ctx
->ac
, mask
, idx
, val
);
3961 emit_data
->output
[emit_data
->chan
] = val
;
3965 * this takes an I,J coordinate pair,
3966 * and works out the X and Y derivatives.
3967 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3969 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3970 struct lp_build_tgsi_context
*bld_base
,
3971 LLVMValueRef interp_ij
)
3973 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3974 LLVMValueRef result
[4], a
;
3977 for (i
= 0; i
< 2; i
++) {
3978 a
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_ij
,
3979 LLVMConstInt(ctx
->i32
, i
, 0), "");
3980 result
[i
] = ac_build_ddxy(&ctx
->ac
, AC_TID_MASK_TOP_LEFT
, 1,
3981 ac_to_integer(&ctx
->ac
, a
)); /* DDX */
3982 result
[2+i
] = ac_build_ddxy(&ctx
->ac
, AC_TID_MASK_TOP_LEFT
, 2,
3983 ac_to_integer(&ctx
->ac
, a
)); /* DDY */
3986 return ac_build_gather_values(&ctx
->ac
, result
, 4);
3989 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3990 struct lp_build_tgsi_context
*bld_base
,
3991 struct lp_build_emit_data
*emit_data
)
3993 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3994 struct si_shader
*shader
= ctx
->shader
;
3995 const struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3996 LLVMValueRef interp_param
;
3997 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3998 const struct tgsi_full_src_register
*input
= &inst
->Src
[0];
3999 int input_base
, input_array_size
;
4002 LLVMValueRef prim_mask
= ctx
->abi
.prim_mask
;
4003 LLVMValueRef array_idx
, offset_x
= NULL
, offset_y
= NULL
;
4004 int interp_param_idx
;
4008 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
4009 /* offset is in second src, first two channels */
4010 offset_x
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 1,
4012 offset_y
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 1,
4014 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4015 LLVMValueRef sample_position
;
4016 LLVMValueRef sample_id
;
4017 LLVMValueRef halfval
= LLVMConstReal(ctx
->f32
, 0.5f
);
4019 /* fetch sample ID, then fetch its sample position,
4020 * and place into first two channels.
4022 sample_id
= lp_build_emit_fetch(bld_base
,
4023 emit_data
->inst
, 1, TGSI_CHAN_X
);
4024 sample_id
= ac_to_integer(&ctx
->ac
, sample_id
);
4026 /* Section 8.13.2 (Interpolation Functions) of the OpenGL Shading
4027 * Language 4.50 spec says about interpolateAtSample:
4029 * "Returns the value of the input interpolant variable at
4030 * the location of sample number sample. If multisample
4031 * buffers are not available, the input variable will be
4032 * evaluated at the center of the pixel. If sample sample
4033 * does not exist, the position used to interpolate the
4034 * input variable is undefined."
4036 * This means that sample_id values outside of the valid are
4037 * in fact valid input, and the usual mechanism for loading the
4038 * sample position doesn't work.
4040 if (ctx
->shader
->key
.mono
.u
.ps
.interpolate_at_sample_force_center
) {
4041 LLVMValueRef center
[4] = {
4042 LLVMConstReal(ctx
->f32
, 0.5),
4043 LLVMConstReal(ctx
->f32
, 0.5),
4048 sample_position
= ac_build_gather_values(&ctx
->ac
, center
, 4);
4050 sample_position
= load_sample_position(&ctx
->abi
, sample_id
);
4053 offset_x
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
,
4056 offset_x
= LLVMBuildFSub(ctx
->ac
.builder
, offset_x
, halfval
, "");
4057 offset_y
= LLVMBuildExtractElement(ctx
->ac
.builder
, sample_position
,
4059 offset_y
= LLVMBuildFSub(ctx
->ac
.builder
, offset_y
, halfval
, "");
4062 assert(input
->Register
.File
== TGSI_FILE_INPUT
);
4064 if (input
->Register
.Indirect
) {
4065 unsigned array_id
= input
->Indirect
.ArrayID
;
4068 input_base
= info
->input_array_first
[array_id
];
4069 input_array_size
= info
->input_array_last
[array_id
] - input_base
+ 1;
4071 input_base
= inst
->Src
[0].Register
.Index
;
4072 input_array_size
= info
->num_inputs
- input_base
;
4075 array_idx
= si_get_indirect_index(ctx
, &input
->Indirect
,
4076 1, input
->Register
.Index
- input_base
);
4078 input_base
= inst
->Src
[0].Register
.Index
;
4079 input_array_size
= 1;
4080 array_idx
= ctx
->i32_0
;
4083 interp
= shader
->selector
->info
.input_interpolate
[input_base
];
4085 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4086 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
4087 location
= TGSI_INTERPOLATE_LOC_CENTER
;
4089 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
4091 interp_param_idx
= lookup_interp_param_index(interp
, location
);
4092 if (interp_param_idx
== -1)
4094 else if (interp_param_idx
)
4095 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
4097 interp_param
= NULL
;
4099 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
4100 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4101 LLVMValueRef ij_out
[2];
4102 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
4105 * take the I then J parameters, and the DDX/Y for it, and
4106 * calculate the IJ inputs for the interpolator.
4107 * temp1 = ddx * offset/sample.x + I;
4108 * interp_param.I = ddy * offset/sample.y + temp1;
4109 * temp1 = ddx * offset/sample.x + J;
4110 * interp_param.J = ddy * offset/sample.y + temp1;
4112 for (i
= 0; i
< 2; i
++) {
4113 LLVMValueRef ix_ll
= LLVMConstInt(ctx
->i32
, i
, 0);
4114 LLVMValueRef iy_ll
= LLVMConstInt(ctx
->i32
, i
+ 2, 0);
4115 LLVMValueRef ddx_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4116 ddxy_out
, ix_ll
, "");
4117 LLVMValueRef ddy_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4118 ddxy_out
, iy_ll
, "");
4119 LLVMValueRef interp_el
= LLVMBuildExtractElement(ctx
->ac
.builder
,
4120 interp_param
, ix_ll
, "");
4123 interp_el
= ac_to_float(&ctx
->ac
, interp_el
);
4125 temp
= ac_build_fmad(&ctx
->ac
, ddx_el
, offset_x
, interp_el
);
4126 ij_out
[i
] = ac_build_fmad(&ctx
->ac
, ddy_el
, offset_y
, temp
);
4128 interp_param
= ac_build_gather_values(&ctx
->ac
, ij_out
, 2);
4132 interp_param
= ac_to_float(&ctx
->ac
, interp_param
);
4134 for (chan
= 0; chan
< 4; chan
++) {
4135 LLVMValueRef gather
= LLVMGetUndef(LLVMVectorType(ctx
->f32
, input_array_size
));
4136 unsigned schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
4138 for (unsigned idx
= 0; idx
< input_array_size
; ++idx
) {
4139 LLVMValueRef v
, i
= NULL
, j
= NULL
;
4142 i
= LLVMBuildExtractElement(
4143 ctx
->ac
.builder
, interp_param
, ctx
->i32_0
, "");
4144 j
= LLVMBuildExtractElement(
4145 ctx
->ac
.builder
, interp_param
, ctx
->i32_1
, "");
4147 v
= si_build_fs_interp(ctx
, input_base
+ idx
, schan
,
4150 gather
= LLVMBuildInsertElement(ctx
->ac
.builder
,
4151 gather
, v
, LLVMConstInt(ctx
->i32
, idx
, false), "");
4154 emit_data
->output
[chan
] = LLVMBuildExtractElement(
4155 ctx
->ac
.builder
, gather
, array_idx
, "");
4159 static void vote_all_emit(
4160 const struct lp_build_tgsi_action
*action
,
4161 struct lp_build_tgsi_context
*bld_base
,
4162 struct lp_build_emit_data
*emit_data
)
4164 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4166 LLVMValueRef tmp
= ac_build_vote_all(&ctx
->ac
, emit_data
->args
[0]);
4167 emit_data
->output
[emit_data
->chan
] =
4168 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4171 static void vote_any_emit(
4172 const struct lp_build_tgsi_action
*action
,
4173 struct lp_build_tgsi_context
*bld_base
,
4174 struct lp_build_emit_data
*emit_data
)
4176 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4178 LLVMValueRef tmp
= ac_build_vote_any(&ctx
->ac
, emit_data
->args
[0]);
4179 emit_data
->output
[emit_data
->chan
] =
4180 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4183 static void vote_eq_emit(
4184 const struct lp_build_tgsi_action
*action
,
4185 struct lp_build_tgsi_context
*bld_base
,
4186 struct lp_build_emit_data
*emit_data
)
4188 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4190 LLVMValueRef tmp
= ac_build_vote_eq(&ctx
->ac
, emit_data
->args
[0]);
4191 emit_data
->output
[emit_data
->chan
] =
4192 LLVMBuildSExt(ctx
->ac
.builder
, tmp
, ctx
->i32
, "");
4195 static void ballot_emit(
4196 const struct lp_build_tgsi_action
*action
,
4197 struct lp_build_tgsi_context
*bld_base
,
4198 struct lp_build_emit_data
*emit_data
)
4200 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4201 LLVMBuilderRef builder
= ctx
->ac
.builder
;
4204 tmp
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4205 tmp
= ac_build_ballot(&ctx
->ac
, tmp
);
4206 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->v2i32
, "");
4208 emit_data
->output
[0] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_0
, "");
4209 emit_data
->output
[1] = LLVMBuildExtractElement(builder
, tmp
, ctx
->i32_1
, "");
4212 static void read_lane_emit(
4213 const struct lp_build_tgsi_action
*action
,
4214 struct lp_build_tgsi_context
*bld_base
,
4215 struct lp_build_emit_data
*emit_data
)
4217 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4219 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_READ_INVOC
) {
4220 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4221 0, emit_data
->src_chan
);
4223 /* Always read the source invocation (= lane) from the X channel. */
4224 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
4226 emit_data
->arg_count
= 2;
4229 /* We currently have no other way to prevent LLVM from lifting the icmp
4230 * calls to a dominating basic block.
4232 ac_build_optimization_barrier(&ctx
->ac
, &emit_data
->args
[0]);
4234 for (unsigned i
= 0; i
< emit_data
->arg_count
; ++i
)
4235 emit_data
->args
[i
] = ac_to_integer(&ctx
->ac
, emit_data
->args
[i
]);
4237 emit_data
->output
[emit_data
->chan
] =
4238 ac_build_intrinsic(&ctx
->ac
, action
->intr_name
,
4239 ctx
->i32
, emit_data
->args
, emit_data
->arg_count
,
4240 AC_FUNC_ATTR_READNONE
|
4241 AC_FUNC_ATTR_CONVERGENT
);
4244 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
4245 struct lp_build_emit_data
*emit_data
)
4247 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4248 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
4252 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
4254 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
4255 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
4259 /* Emit one vertex from the geometry shader */
4260 static void si_llvm_emit_vertex(struct ac_shader_abi
*abi
,
4262 LLVMValueRef
*addrs
)
4264 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4265 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4266 struct si_shader
*shader
= ctx
->shader
;
4267 struct lp_build_if_state if_state
;
4268 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
4269 ctx
->param_gs2vs_offset
);
4270 LLVMValueRef gs_next_vertex
;
4271 LLVMValueRef can_emit
;
4272 unsigned chan
, offset
;
4275 /* Write vertex attribute values to GSVS ring */
4276 gs_next_vertex
= LLVMBuildLoad(ctx
->ac
.builder
,
4277 ctx
->gs_next_vertex
[stream
],
4280 /* If this thread has already emitted the declared maximum number of
4281 * vertices, skip the write: excessive vertex emissions are not
4282 * supposed to have any effect.
4284 * If the shader has no writes to memory, kill it instead. This skips
4285 * further memory loads and may allow LLVM to skip to the end
4288 can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, gs_next_vertex
,
4289 LLVMConstInt(ctx
->i32
,
4290 shader
->selector
->gs_max_out_vertices
, 0), "");
4292 bool use_kill
= !info
->writes_memory
;
4294 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
4296 lp_build_if(&if_state
, &ctx
->gallivm
, can_emit
);
4300 for (i
= 0; i
< info
->num_outputs
; i
++) {
4301 for (chan
= 0; chan
< 4; chan
++) {
4302 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
4303 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
4306 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, addrs
[4 * i
+ chan
], "");
4307 LLVMValueRef voffset
=
4308 LLVMConstInt(ctx
->i32
, offset
*
4309 shader
->selector
->gs_max_out_vertices
, 0);
4312 voffset
= LLVMBuildAdd(ctx
->ac
.builder
, voffset
, gs_next_vertex
, "");
4313 voffset
= LLVMBuildMul(ctx
->ac
.builder
, voffset
,
4314 LLVMConstInt(ctx
->i32
, 4, 0), "");
4316 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
4318 ac_build_buffer_store_dword(&ctx
->ac
,
4319 ctx
->gsvs_ring
[stream
],
4321 voffset
, soffset
, 0,
4326 gs_next_vertex
= LLVMBuildAdd(ctx
->ac
.builder
, gs_next_vertex
, ctx
->i32_1
, "");
4327 LLVMBuildStore(ctx
->ac
.builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
4329 /* Signal vertex emission */
4330 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
4331 si_get_gs_wave_id(ctx
));
4333 lp_build_endif(&if_state
);
4336 /* Emit one vertex from the geometry shader */
4337 static void si_tgsi_emit_vertex(
4338 const struct lp_build_tgsi_action
*action
,
4339 struct lp_build_tgsi_context
*bld_base
,
4340 struct lp_build_emit_data
*emit_data
)
4342 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4343 unsigned stream
= si_llvm_get_stream(bld_base
, emit_data
);
4345 si_llvm_emit_vertex(&ctx
->abi
, stream
, ctx
->outputs
[0]);
4348 /* Cut one primitive from the geometry shader */
4349 static void si_llvm_emit_primitive(struct ac_shader_abi
*abi
,
4352 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4354 /* Signal primitive cut */
4355 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
4356 si_get_gs_wave_id(ctx
));
4359 /* Cut one primitive from the geometry shader */
4360 static void si_tgsi_emit_primitive(
4361 const struct lp_build_tgsi_action
*action
,
4362 struct lp_build_tgsi_context
*bld_base
,
4363 struct lp_build_emit_data
*emit_data
)
4365 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4367 si_llvm_emit_primitive(&ctx
->abi
, si_llvm_get_stream(bld_base
, emit_data
));
4370 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4371 struct lp_build_tgsi_context
*bld_base
,
4372 struct lp_build_emit_data
*emit_data
)
4374 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4376 /* SI only (thanks to a hw bug workaround):
4377 * The real barrier instruction isn’t needed, because an entire patch
4378 * always fits into a single wave.
4380 if (ctx
->screen
->info
.chip_class
== SI
&&
4381 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
4382 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
4386 ac_build_s_barrier(&ctx
->ac
);
4389 static void si_create_function(struct si_shader_context
*ctx
,
4391 LLVMTypeRef
*returns
, unsigned num_returns
,
4392 struct si_function_info
*fninfo
,
4393 unsigned max_workgroup_size
)
4397 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
4398 fninfo
->types
, fninfo
->num_params
);
4399 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
4401 for (i
= 0; i
< fninfo
->num_sgpr_params
; ++i
) {
4402 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
4404 /* The combination of:
4408 * allows the optimization passes to move loads and reduces
4409 * SGPR spilling significantly.
4411 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4412 AC_FUNC_ATTR_INREG
);
4414 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
4415 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4416 AC_FUNC_ATTR_NOALIAS
);
4417 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
4421 for (i
= 0; i
< fninfo
->num_params
; ++i
) {
4422 if (fninfo
->assign
[i
])
4423 *fninfo
->assign
[i
] = LLVMGetParam(ctx
->main_fn
, i
);
4426 if (ctx
->screen
->info
.address32_hi
) {
4427 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4428 "amdgpu-32bit-address-high-bits",
4429 ctx
->screen
->info
.address32_hi
);
4432 if (max_workgroup_size
) {
4433 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4434 "amdgpu-max-work-group-size",
4435 max_workgroup_size
);
4437 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4438 "no-signed-zeros-fp-math",
4441 if (ctx
->screen
->debug_flags
& DBG(UNSAFE_MATH
)) {
4442 /* These were copied from some LLVM test. */
4443 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4444 "less-precise-fpmad",
4446 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4449 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4452 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4458 static void declare_streamout_params(struct si_shader_context
*ctx
,
4459 struct pipe_stream_output_info
*so
,
4460 struct si_function_info
*fninfo
)
4464 /* Streamout SGPRs. */
4465 if (so
->num_outputs
) {
4466 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
4467 ctx
->param_streamout_config
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4469 ctx
->param_streamout_config
= fninfo
->num_params
- 1;
4471 ctx
->param_streamout_write_index
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4473 /* A streamout buffer offset is loaded if the stride is non-zero. */
4474 for (i
= 0; i
< 4; i
++) {
4478 ctx
->param_streamout_offset
[i
] = add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4482 static unsigned si_get_max_workgroup_size(const struct si_shader
*shader
)
4484 switch (shader
->selector
->type
) {
4485 case PIPE_SHADER_TESS_CTRL
:
4486 /* Return this so that LLVM doesn't remove s_barrier
4487 * instructions on chips where we use s_barrier. */
4488 return shader
->selector
->screen
->info
.chip_class
>= CIK
? 128 : 64;
4490 case PIPE_SHADER_GEOMETRY
:
4491 return shader
->selector
->screen
->info
.chip_class
>= GFX9
? 128 : 64;
4493 case PIPE_SHADER_COMPUTE
:
4494 break; /* see below */
4500 const unsigned *properties
= shader
->selector
->info
.properties
;
4501 unsigned max_work_group_size
=
4502 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
4503 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
4504 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
4506 if (!max_work_group_size
) {
4507 /* This is a variable group size compute shader,
4508 * compile it for the maximum possible group size.
4510 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
4512 return max_work_group_size
;
4515 static void declare_const_and_shader_buffers(struct si_shader_context
*ctx
,
4516 struct si_function_info
*fninfo
,
4519 LLVMTypeRef const_shader_buf_type
;
4521 if (ctx
->shader
->selector
->info
.const_buffers_declared
== 1 &&
4522 ctx
->shader
->selector
->info
.shader_buffers_declared
== 0)
4523 const_shader_buf_type
= ctx
->f32
;
4525 const_shader_buf_type
= ctx
->v4i32
;
4527 unsigned const_and_shader_buffers
=
4528 add_arg(fninfo
, ARG_SGPR
,
4529 ac_array_in_const32_addr_space(const_shader_buf_type
));
4532 ctx
->param_const_and_shader_buffers
= const_and_shader_buffers
;
4535 static void declare_samplers_and_images(struct si_shader_context
*ctx
,
4536 struct si_function_info
*fninfo
,
4539 unsigned samplers_and_images
=
4540 add_arg(fninfo
, ARG_SGPR
,
4541 ac_array_in_const32_addr_space(ctx
->v8i32
));
4544 ctx
->param_samplers_and_images
= samplers_and_images
;
4547 static void declare_per_stage_desc_pointers(struct si_shader_context
*ctx
,
4548 struct si_function_info
*fninfo
,
4551 declare_const_and_shader_buffers(ctx
, fninfo
, assign_params
);
4552 declare_samplers_and_images(ctx
, fninfo
, assign_params
);
4555 static void declare_global_desc_pointers(struct si_shader_context
*ctx
,
4556 struct si_function_info
*fninfo
)
4558 ctx
->param_rw_buffers
= add_arg(fninfo
, ARG_SGPR
,
4559 ac_array_in_const32_addr_space(ctx
->v4i32
));
4560 ctx
->param_bindless_samplers_and_images
= add_arg(fninfo
, ARG_SGPR
,
4561 ac_array_in_const32_addr_space(ctx
->v8i32
));
4564 static void declare_vs_specific_input_sgprs(struct si_shader_context
*ctx
,
4565 struct si_function_info
*fninfo
)
4567 ctx
->param_vs_state_bits
= add_arg(fninfo
, ARG_SGPR
, ctx
->i32
);
4568 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.base_vertex
);
4569 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.start_instance
);
4570 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.draw_id
);
4573 static void declare_vs_input_vgprs(struct si_shader_context
*ctx
,
4574 struct si_function_info
*fninfo
,
4575 unsigned *num_prolog_vgprs
)
4577 struct si_shader
*shader
= ctx
->shader
;
4579 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.vertex_id
);
4580 if (shader
->key
.as_ls
) {
4581 ctx
->param_rel_auto_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4582 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4584 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4585 ctx
->param_vs_prim_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4587 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
); /* unused */
4589 if (!shader
->is_gs_copy_shader
) {
4590 /* Vertex load indices. */
4591 ctx
->param_vertex_index0
= fninfo
->num_params
;
4592 for (unsigned i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4593 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4594 *num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
4598 static void declare_tes_input_vgprs(struct si_shader_context
*ctx
,
4599 struct si_function_info
*fninfo
)
4601 ctx
->param_tes_u
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4602 ctx
->param_tes_v
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4603 ctx
->param_tes_rel_patch_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4604 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tes_patch_id
);
4608 /* Convenient merged shader definitions. */
4609 SI_SHADER_MERGED_VERTEX_TESSCTRL
= PIPE_SHADER_TYPES
,
4610 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
,
4613 static void create_function(struct si_shader_context
*ctx
)
4615 struct si_shader
*shader
= ctx
->shader
;
4616 struct si_function_info fninfo
;
4617 LLVMTypeRef returns
[16+32*4];
4618 unsigned i
, num_return_sgprs
;
4619 unsigned num_returns
= 0;
4620 unsigned num_prolog_vgprs
= 0;
4621 unsigned type
= ctx
->type
;
4622 unsigned vs_blit_property
=
4623 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
4625 si_init_function_info(&fninfo
);
4627 /* Set MERGED shaders. */
4628 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
4629 if (shader
->key
.as_ls
|| type
== PIPE_SHADER_TESS_CTRL
)
4630 type
= SI_SHADER_MERGED_VERTEX_TESSCTRL
; /* LS or HS */
4631 else if (shader
->key
.as_es
|| type
== PIPE_SHADER_GEOMETRY
)
4632 type
= SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
;
4635 LLVMTypeRef v3i32
= LLVMVectorType(ctx
->i32
, 3);
4638 case PIPE_SHADER_VERTEX
:
4639 declare_global_desc_pointers(ctx
, &fninfo
);
4641 if (vs_blit_property
) {
4642 ctx
->param_vs_blit_inputs
= fninfo
.num_params
;
4643 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x1, y1 */
4644 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x2, y2 */
4645 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* depth */
4647 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
4648 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color0 */
4649 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color1 */
4650 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color2 */
4651 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color3 */
4652 } else if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
) {
4653 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x1 */
4654 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y1 */
4655 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x2 */
4656 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y2 */
4657 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.z */
4658 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.w */
4662 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4666 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4667 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4668 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4669 ac_array_in_const32_addr_space(ctx
->v4i32
));
4671 if (shader
->key
.as_es
) {
4672 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4673 } else if (shader
->key
.as_ls
) {
4674 /* no extra parameters */
4676 if (shader
->is_gs_copy_shader
) {
4677 fninfo
.num_params
= ctx
->param_vs_state_bits
+ 1;
4678 fninfo
.num_sgpr_params
= fninfo
.num_params
;
4681 /* The locations of the other parameters are assigned dynamically. */
4682 declare_streamout_params(ctx
, &shader
->selector
->so
,
4687 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4690 case PIPE_SHADER_TESS_CTRL
: /* SI-CI-VI */
4691 declare_global_desc_pointers(ctx
, &fninfo
);
4692 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4693 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4694 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4695 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4696 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4697 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4698 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4701 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4702 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4704 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4705 * placed after the user SGPRs.
4707 for (i
= 0; i
< GFX6_TCS_NUM_USER_SGPR
+ 2; i
++)
4708 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4709 for (i
= 0; i
< 11; i
++)
4710 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4713 case SI_SHADER_MERGED_VERTEX_TESSCTRL
:
4714 /* Merged stages have 8 system SGPRs at the beginning. */
4715 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
4716 if (HAVE_32BIT_POINTERS
) {
4717 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4718 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4720 declare_const_and_shader_buffers(ctx
, &fninfo
,
4721 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4723 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4724 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4725 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4726 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4727 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4728 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4730 declare_global_desc_pointers(ctx
, &fninfo
);
4731 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4732 ctx
->type
== PIPE_SHADER_VERTEX
);
4733 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4735 if (!HAVE_32BIT_POINTERS
) {
4736 declare_samplers_and_images(ctx
, &fninfo
,
4737 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4739 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4740 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4741 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4742 if (!HAVE_32BIT_POINTERS
) /* Align to 2 dwords. */
4743 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4744 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4745 ac_array_in_const32_addr_space(ctx
->v4i32
));
4747 /* VGPRs (first TCS, then VS) */
4748 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4749 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4751 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4752 declare_vs_input_vgprs(ctx
, &fninfo
,
4755 /* LS return values are inputs to the TCS main shader part. */
4756 for (i
= 0; i
< 8 + GFX9_TCS_NUM_USER_SGPR
; i
++)
4757 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4758 for (i
= 0; i
< 2; i
++)
4759 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4761 /* TCS return values are inputs to the TCS epilog.
4763 * param_tcs_offchip_offset, param_tcs_factor_offset,
4764 * param_tcs_offchip_layout, and param_rw_buffers
4765 * should be passed to the epilog.
4767 for (i
= 0; i
<= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
; i
++)
4768 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4769 for (i
= 0; i
< 11; i
++)
4770 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4774 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
:
4775 /* Merged stages have 8 system SGPRs at the beginning. */
4776 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
4777 if (HAVE_32BIT_POINTERS
) {
4778 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4779 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4781 declare_const_and_shader_buffers(ctx
, &fninfo
,
4782 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4784 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4785 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4786 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4787 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4788 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4789 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4791 declare_global_desc_pointers(ctx
, &fninfo
);
4792 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4793 (ctx
->type
== PIPE_SHADER_VERTEX
||
4794 ctx
->type
== PIPE_SHADER_TESS_EVAL
));
4795 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4796 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4798 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4799 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4800 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4801 /* Declare as many input SGPRs as the VS has. */
4802 if (!HAVE_32BIT_POINTERS
)
4803 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4806 if (!HAVE_32BIT_POINTERS
) {
4807 declare_samplers_and_images(ctx
, &fninfo
,
4808 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4810 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4811 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4812 ac_array_in_const32_addr_space(ctx
->v4i32
));
4815 /* VGPRs (first GS, then VS/TES) */
4816 ctx
->param_gs_vtx01_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4817 ctx
->param_gs_vtx23_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4818 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4819 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4820 ctx
->param_gs_vtx45_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4822 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4823 declare_vs_input_vgprs(ctx
, &fninfo
,
4825 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4826 declare_tes_input_vgprs(ctx
, &fninfo
);
4829 if (ctx
->type
== PIPE_SHADER_VERTEX
||
4830 ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4831 unsigned num_user_sgprs
;
4833 if (ctx
->type
== PIPE_SHADER_VERTEX
)
4834 num_user_sgprs
= GFX9_VSGS_NUM_USER_SGPR
;
4836 num_user_sgprs
= GFX9_TESGS_NUM_USER_SGPR
;
4838 /* ES return values are inputs to GS. */
4839 for (i
= 0; i
< 8 + num_user_sgprs
; i
++)
4840 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4841 for (i
= 0; i
< 5; i
++)
4842 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4846 case PIPE_SHADER_TESS_EVAL
:
4847 declare_global_desc_pointers(ctx
, &fninfo
);
4848 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4849 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4850 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4851 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4853 if (shader
->key
.as_es
) {
4854 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4855 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4856 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4858 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4859 declare_streamout_params(ctx
, &shader
->selector
->so
,
4861 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4865 declare_tes_input_vgprs(ctx
, &fninfo
);
4868 case PIPE_SHADER_GEOMETRY
:
4869 declare_global_desc_pointers(ctx
, &fninfo
);
4870 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4871 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4872 ctx
->param_gs_wave_id
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4875 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]);
4876 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]);
4877 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4878 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
4879 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
4880 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
4881 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
4882 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4885 case PIPE_SHADER_FRAGMENT
:
4886 declare_global_desc_pointers(ctx
, &fninfo
);
4887 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4888 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
4889 add_arg_assign_checked(&fninfo
, ARG_SGPR
, ctx
->i32
,
4890 &ctx
->abi
.prim_mask
, SI_PARAM_PRIM_MASK
);
4892 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_SAMPLE
);
4893 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTER
);
4894 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTROID
);
4895 add_arg_checked(&fninfo
, ARG_VGPR
, v3i32
, SI_PARAM_PERSP_PULL_MODEL
);
4896 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_SAMPLE
);
4897 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTER
);
4898 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTROID
);
4899 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->f32
, SI_PARAM_LINE_STIPPLE_TEX
);
4900 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4901 &ctx
->abi
.frag_pos
[0], SI_PARAM_POS_X_FLOAT
);
4902 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4903 &ctx
->abi
.frag_pos
[1], SI_PARAM_POS_Y_FLOAT
);
4904 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4905 &ctx
->abi
.frag_pos
[2], SI_PARAM_POS_Z_FLOAT
);
4906 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4907 &ctx
->abi
.frag_pos
[3], SI_PARAM_POS_W_FLOAT
);
4908 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4909 &ctx
->abi
.front_face
, SI_PARAM_FRONT_FACE
);
4910 shader
->info
.face_vgpr_index
= 20;
4911 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4912 &ctx
->abi
.ancillary
, SI_PARAM_ANCILLARY
);
4913 shader
->info
.ancillary_vgpr_index
= 21;
4914 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4915 &ctx
->abi
.sample_coverage
, SI_PARAM_SAMPLE_COVERAGE
);
4916 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->i32
, SI_PARAM_POS_FIXED_PT
);
4918 /* Color inputs from the prolog. */
4919 if (shader
->selector
->info
.colors_read
) {
4920 unsigned num_color_elements
=
4921 util_bitcount(shader
->selector
->info
.colors_read
);
4923 assert(fninfo
.num_params
+ num_color_elements
<= ARRAY_SIZE(fninfo
.types
));
4924 for (i
= 0; i
< num_color_elements
; i
++)
4925 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
4927 num_prolog_vgprs
+= num_color_elements
;
4930 /* Outputs for the epilog. */
4931 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4934 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4935 shader
->selector
->info
.writes_z
+
4936 shader
->selector
->info
.writes_stencil
+
4937 shader
->selector
->info
.writes_samplemask
+
4938 1 /* SampleMaskIn */;
4940 num_returns
= MAX2(num_returns
,
4942 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4944 for (i
= 0; i
< num_return_sgprs
; i
++)
4945 returns
[i
] = ctx
->i32
;
4946 for (; i
< num_returns
; i
++)
4947 returns
[i
] = ctx
->f32
;
4950 case PIPE_SHADER_COMPUTE
:
4951 declare_global_desc_pointers(ctx
, &fninfo
);
4952 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4953 if (shader
->selector
->info
.uses_grid_size
)
4954 add_arg_assign(&fninfo
, ARG_SGPR
, v3i32
, &ctx
->abi
.num_work_groups
);
4955 if (shader
->selector
->info
.uses_block_size
&&
4956 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
4957 ctx
->param_block_size
= add_arg(&fninfo
, ARG_SGPR
, v3i32
);
4959 unsigned cs_user_data_dwords
=
4960 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_USER_DATA_DWORDS
];
4961 if (cs_user_data_dwords
) {
4962 ctx
->param_cs_user_data
= add_arg(&fninfo
, ARG_SGPR
,
4963 LLVMVectorType(ctx
->i32
, cs_user_data_dwords
));
4966 for (i
= 0; i
< 3; i
++) {
4967 ctx
->abi
.workgroup_ids
[i
] = NULL
;
4968 if (shader
->selector
->info
.uses_block_id
[i
])
4969 add_arg_assign(&fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.workgroup_ids
[i
]);
4972 add_arg_assign(&fninfo
, ARG_VGPR
, v3i32
, &ctx
->abi
.local_invocation_ids
);
4975 assert(0 && "unimplemented shader");
4979 si_create_function(ctx
, "main", returns
, num_returns
, &fninfo
,
4980 si_get_max_workgroup_size(shader
));
4982 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4983 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&& !ctx
->shader
->is_monolithic
) {
4984 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4985 "InitialPSInputAddr",
4986 S_0286D0_PERSP_SAMPLE_ENA(1) |
4987 S_0286D0_PERSP_CENTER_ENA(1) |
4988 S_0286D0_PERSP_CENTROID_ENA(1) |
4989 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4990 S_0286D0_LINEAR_CENTER_ENA(1) |
4991 S_0286D0_LINEAR_CENTROID_ENA(1) |
4992 S_0286D0_FRONT_FACE_ENA(1) |
4993 S_0286D0_ANCILLARY_ENA(1) |
4994 S_0286D0_POS_FIXED_PT_ENA(1));
4997 shader
->info
.num_input_sgprs
= 0;
4998 shader
->info
.num_input_vgprs
= 0;
5000 for (i
= 0; i
< fninfo
.num_sgpr_params
; ++i
)
5001 shader
->info
.num_input_sgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5003 for (; i
< fninfo
.num_params
; ++i
)
5004 shader
->info
.num_input_vgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5006 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5007 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5009 if (shader
->key
.as_ls
||
5010 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5011 /* GFX9 has the ESGS ring buffer in LDS. */
5012 type
== SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
)
5013 ac_declare_lds_as_pointer(&ctx
->ac
);
5017 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5020 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5022 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5024 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5025 ctx
->param_rw_buffers
);
5027 if (ctx
->screen
->info
.chip_class
<= VI
&&
5028 (ctx
->shader
->key
.as_es
|| ctx
->type
== PIPE_SHADER_GEOMETRY
)) {
5030 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5032 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, ring
, 0);
5035 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5038 if (ctx
->shader
->is_gs_copy_shader
) {
5039 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5042 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5043 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5044 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5045 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5046 LLVMValueRef base_ring
;
5048 base_ring
= ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5050 /* The conceptual layout of the GSVS ring is
5051 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5052 * but the real memory layout is swizzled across
5054 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5056 * Override the buffer descriptor accordingly.
5058 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5059 uint64_t stream_offset
= 0;
5061 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5062 unsigned num_components
;
5064 unsigned num_records
;
5065 LLVMValueRef ring
, tmp
;
5067 num_components
= sel
->info
.num_stream_output_components
[stream
];
5068 if (!num_components
)
5071 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5073 /* Limit on the stride field for <= CIK. */
5074 assert(stride
< (1 << 14));
5078 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5079 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_0
, "");
5080 tmp
= LLVMBuildAdd(builder
, tmp
,
5081 LLVMConstInt(ctx
->i64
,
5082 stream_offset
, 0), "");
5083 stream_offset
+= stride
* 64;
5085 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_0
, "");
5086 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5087 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_1
, "");
5088 tmp
= LLVMBuildOr(builder
, tmp
,
5089 LLVMConstInt(ctx
->i32
,
5090 S_008F04_STRIDE(stride
) |
5091 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5092 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_1
, "");
5093 ring
= LLVMBuildInsertElement(builder
, ring
,
5094 LLVMConstInt(ctx
->i32
, num_records
, 0),
5095 LLVMConstInt(ctx
->i32
, 2, 0), "");
5096 ring
= LLVMBuildInsertElement(builder
, ring
,
5097 LLVMConstInt(ctx
->i32
,
5098 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5099 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5100 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5101 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5102 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5103 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5104 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5105 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5106 S_008F0C_ADD_TID_ENABLE(1),
5108 LLVMConstInt(ctx
->i32
, 3, 0), "");
5110 ctx
->gsvs_ring
[stream
] = ring
;
5112 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
5113 ctx
->tess_offchip_ring
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TES
);
5117 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5118 LLVMValueRef param_rw_buffers
,
5119 unsigned param_pos_fixed_pt
)
5121 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5122 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5124 /* Use the fixed-point gl_FragCoord input.
5125 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5126 * per coordinate to get the repeating effect.
5128 address
[0] = si_unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5129 address
[1] = si_unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5131 /* Load the buffer descriptor. */
5132 slot
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
5133 desc
= ac_build_load_to_sgpr(&ctx
->ac
, param_rw_buffers
, slot
);
5135 /* The stipple pattern is 32x32, each row has 32 bits. */
5136 offset
= LLVMBuildMul(builder
, address
[1],
5137 LLVMConstInt(ctx
->i32
, 4, 0), "");
5138 row
= buffer_load_const(ctx
, desc
, offset
);
5139 row
= ac_to_integer(&ctx
->ac
, row
);
5140 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5141 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5142 ac_build_kill_if_false(&ctx
->ac
, bit
);
5145 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
5146 struct si_shader_config
*conf
,
5147 unsigned symbol_offset
)
5150 const unsigned char *config
=
5151 ac_shader_binary_config_start(binary
, symbol_offset
);
5152 bool really_needs_scratch
= false;
5154 /* LLVM adds SGPR spills to the scratch size.
5155 * Find out if we really need the scratch buffer.
5157 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5158 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
5160 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5161 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5162 really_needs_scratch
= true;
5167 /* XXX: We may be able to emit some of these values directly rather than
5168 * extracting fields to be emitted later.
5171 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5172 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5173 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5175 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5176 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5177 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5178 case R_00B428_SPI_SHADER_PGM_RSRC1_HS
:
5179 case R_00B848_COMPUTE_PGM_RSRC1
:
5180 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5181 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5182 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5183 conf
->rsrc1
= value
;
5185 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5186 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5188 case R_00B84C_COMPUTE_PGM_RSRC2
:
5189 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5190 conf
->rsrc2
= value
;
5192 case R_0286CC_SPI_PS_INPUT_ENA
:
5193 conf
->spi_ps_input_ena
= value
;
5195 case R_0286D0_SPI_PS_INPUT_ADDR
:
5196 conf
->spi_ps_input_addr
= value
;
5198 case R_0286E8_SPI_TMPRING_SIZE
:
5199 case R_00B860_COMPUTE_TMPRING_SIZE
:
5200 /* WAVESIZE is in units of 256 dwords. */
5201 if (really_needs_scratch
)
5202 conf
->scratch_bytes_per_wave
=
5203 G_00B860_WAVESIZE(value
) * 256 * 4;
5205 case 0x4: /* SPILLED_SGPRS */
5206 conf
->spilled_sgprs
= value
;
5208 case 0x8: /* SPILLED_VGPRS */
5209 conf
->spilled_vgprs
= value
;
5213 static bool printed
;
5216 fprintf(stderr
, "Warning: LLVM emitted unknown "
5217 "config register: 0x%x\n", reg
);
5225 if (!conf
->spi_ps_input_addr
)
5226 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5229 void si_shader_apply_scratch_relocs(struct si_shader
*shader
,
5230 uint64_t scratch_va
)
5233 uint32_t scratch_rsrc_dword0
= scratch_va
;
5234 uint32_t scratch_rsrc_dword1
=
5235 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5237 /* Enable scratch coalescing. */
5238 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5240 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5241 const struct ac_shader_reloc
*reloc
=
5242 &shader
->binary
.relocs
[i
];
5243 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5244 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5245 &scratch_rsrc_dword0
, 4);
5246 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5247 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5248 &scratch_rsrc_dword1
, 4);
5253 /* For the UMR disassembler. */
5254 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
5255 #define DEBUGGER_NUM_MARKERS 5
5257 static unsigned si_get_shader_binary_size(const struct si_shader
*shader
)
5259 unsigned size
= shader
->binary
.code_size
;
5262 size
+= shader
->prolog
->binary
.code_size
;
5263 if (shader
->previous_stage
)
5264 size
+= shader
->previous_stage
->binary
.code_size
;
5265 if (shader
->prolog2
)
5266 size
+= shader
->prolog2
->binary
.code_size
;
5268 size
+= shader
->epilog
->binary
.code_size
;
5269 return size
+ DEBUGGER_NUM_MARKERS
* 4;
5272 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5274 const struct ac_shader_binary
*prolog
=
5275 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
5276 const struct ac_shader_binary
*previous_stage
=
5277 shader
->previous_stage
? &shader
->previous_stage
->binary
: NULL
;
5278 const struct ac_shader_binary
*prolog2
=
5279 shader
->prolog2
? &shader
->prolog2
->binary
: NULL
;
5280 const struct ac_shader_binary
*epilog
=
5281 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
5282 const struct ac_shader_binary
*mainb
= &shader
->binary
;
5283 unsigned bo_size
= si_get_shader_binary_size(shader
) +
5284 (!epilog
? mainb
->rodata_size
: 0);
5287 assert(!prolog
|| !prolog
->rodata_size
);
5288 assert(!previous_stage
|| !previous_stage
->rodata_size
);
5289 assert(!prolog2
|| !prolog2
->rodata_size
);
5290 assert((!prolog
&& !previous_stage
&& !prolog2
&& !epilog
) ||
5291 !mainb
->rodata_size
);
5292 assert(!epilog
|| !epilog
->rodata_size
);
5294 r600_resource_reference(&shader
->bo
, NULL
);
5295 shader
->bo
= si_aligned_buffer_create(&sscreen
->b
,
5296 sscreen
->cpdma_prefetch_writes_memory
?
5297 0 : SI_RESOURCE_FLAG_READ_ONLY
,
5298 PIPE_USAGE_IMMUTABLE
,
5299 align(bo_size
, SI_CPDMA_ALIGNMENT
),
5305 ptr
= sscreen
->ws
->buffer_map(shader
->bo
->buf
, NULL
,
5306 PIPE_TRANSFER_READ_WRITE
|
5307 PIPE_TRANSFER_UNSYNCHRONIZED
);
5309 /* Don't use util_memcpy_cpu_to_le32. LLVM binaries are
5310 * endian-independent. */
5312 memcpy(ptr
, prolog
->code
, prolog
->code_size
);
5313 ptr
+= prolog
->code_size
;
5315 if (previous_stage
) {
5316 memcpy(ptr
, previous_stage
->code
, previous_stage
->code_size
);
5317 ptr
+= previous_stage
->code_size
;
5320 memcpy(ptr
, prolog2
->code
, prolog2
->code_size
);
5321 ptr
+= prolog2
->code_size
;
5324 memcpy(ptr
, mainb
->code
, mainb
->code_size
);
5325 ptr
+= mainb
->code_size
;
5328 memcpy(ptr
, epilog
->code
, epilog
->code_size
);
5329 ptr
+= epilog
->code_size
;
5330 } else if (mainb
->rodata_size
> 0) {
5331 memcpy(ptr
, mainb
->rodata
, mainb
->rodata_size
);
5332 ptr
+= mainb
->rodata_size
;
5335 /* Add end-of-code markers for the UMR disassembler. */
5336 uint32_t *ptr32
= (uint32_t*)ptr
;
5337 for (unsigned i
= 0; i
< DEBUGGER_NUM_MARKERS
; i
++)
5338 ptr32
[i
] = DEBUGGER_END_OF_CODE_MARKER
;
5340 sscreen
->ws
->buffer_unmap(shader
->bo
->buf
);
5344 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
5345 struct pipe_debug_callback
*debug
,
5346 const char *name
, FILE *file
)
5351 if (binary
->disasm_string
) {
5352 fprintf(file
, "Shader %s disassembly:\n", name
);
5353 fprintf(file
, "%s", binary
->disasm_string
);
5355 if (debug
&& debug
->debug_message
) {
5356 /* Very long debug messages are cut off, so send the
5357 * disassembly one line at a time. This causes more
5358 * overhead, but on the plus side it simplifies
5359 * parsing of resulting logs.
5361 pipe_debug_message(debug
, SHADER_INFO
,
5362 "Shader Disassembly Begin");
5364 line
= binary
->disasm_string
;
5366 p
= util_strchrnul(line
, '\n');
5370 pipe_debug_message(debug
, SHADER_INFO
,
5371 "%.*s", count
, line
);
5379 pipe_debug_message(debug
, SHADER_INFO
,
5380 "Shader Disassembly End");
5383 fprintf(file
, "Shader %s binary:\n", name
);
5384 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
5385 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
5386 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
5387 binary
->code
[i
+ 1], binary
->code
[i
]);
5392 static void si_calculate_max_simd_waves(struct si_shader
*shader
)
5394 struct si_screen
*sscreen
= shader
->selector
->screen
;
5395 struct si_shader_config
*conf
= &shader
->config
;
5396 unsigned num_inputs
= shader
->selector
->info
.num_inputs
;
5397 unsigned lds_increment
= sscreen
->info
.chip_class
>= CIK
? 512 : 256;
5398 unsigned lds_per_wave
= 0;
5399 unsigned max_simd_waves
;
5401 max_simd_waves
= ac_get_max_simd_waves(sscreen
->info
.family
);
5403 /* Compute LDS usage for PS. */
5404 switch (shader
->selector
->type
) {
5405 case PIPE_SHADER_FRAGMENT
:
5406 /* The minimum usage per wave is (num_inputs * 48). The maximum
5407 * usage is (num_inputs * 48 * 16).
5408 * We can get anything in between and it varies between waves.
5410 * The 48 bytes per input for a single primitive is equal to
5411 * 4 bytes/component * 4 components/input * 3 points.
5413 * Other stages don't know the size at compile time or don't
5414 * allocate LDS per wave, but instead they do it per thread group.
5416 lds_per_wave
= conf
->lds_size
* lds_increment
+
5417 align(num_inputs
* 48, lds_increment
);
5419 case PIPE_SHADER_COMPUTE
:
5420 if (shader
->selector
) {
5421 unsigned max_workgroup_size
=
5422 si_get_max_workgroup_size(shader
);
5423 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
5424 DIV_ROUND_UP(max_workgroup_size
, 64);
5429 /* Compute the per-SIMD wave counts. */
5430 if (conf
->num_sgprs
) {
5431 if (sscreen
->info
.chip_class
>= VI
)
5432 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
5434 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
5437 if (conf
->num_vgprs
)
5438 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
5440 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
5441 * 16KB makes some SIMDs unoccupied). */
5443 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
5445 conf
->max_simd_waves
= max_simd_waves
;
5448 void si_shader_dump_stats_for_shader_db(const struct si_shader
*shader
,
5449 struct pipe_debug_callback
*debug
)
5451 const struct si_shader_config
*conf
= &shader
->config
;
5453 pipe_debug_message(debug
, SHADER_INFO
,
5454 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
5455 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
5456 "Spilled VGPRs: %d PrivMem VGPRs: %d",
5457 conf
->num_sgprs
, conf
->num_vgprs
,
5458 si_get_shader_binary_size(shader
),
5459 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5460 conf
->max_simd_waves
, conf
->spilled_sgprs
,
5461 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
5464 static void si_shader_dump_stats(struct si_screen
*sscreen
,
5465 const struct si_shader
*shader
,
5468 bool check_debug_option
)
5470 const struct si_shader_config
*conf
= &shader
->config
;
5472 if (!check_debug_option
||
5473 si_can_dump_shader(sscreen
, processor
)) {
5474 if (processor
== PIPE_SHADER_FRAGMENT
) {
5475 fprintf(file
, "*** SHADER CONFIG ***\n"
5476 "SPI_PS_INPUT_ADDR = 0x%04x\n"
5477 "SPI_PS_INPUT_ENA = 0x%04x\n",
5478 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
5481 fprintf(file
, "*** SHADER STATS ***\n"
5484 "Spilled SGPRs: %d\n"
5485 "Spilled VGPRs: %d\n"
5486 "Private memory VGPRs: %d\n"
5487 "Code Size: %d bytes\n"
5489 "Scratch: %d bytes per wave\n"
5491 "********************\n\n\n",
5492 conf
->num_sgprs
, conf
->num_vgprs
,
5493 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
5494 conf
->private_mem_vgprs
,
5495 si_get_shader_binary_size(shader
),
5496 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5497 conf
->max_simd_waves
);
5501 const char *si_get_shader_name(const struct si_shader
*shader
, unsigned processor
)
5503 switch (processor
) {
5504 case PIPE_SHADER_VERTEX
:
5505 if (shader
->key
.as_es
)
5506 return "Vertex Shader as ES";
5507 else if (shader
->key
.as_ls
)
5508 return "Vertex Shader as LS";
5510 return "Vertex Shader as VS";
5511 case PIPE_SHADER_TESS_CTRL
:
5512 return "Tessellation Control Shader";
5513 case PIPE_SHADER_TESS_EVAL
:
5514 if (shader
->key
.as_es
)
5515 return "Tessellation Evaluation Shader as ES";
5517 return "Tessellation Evaluation Shader as VS";
5518 case PIPE_SHADER_GEOMETRY
:
5519 if (shader
->is_gs_copy_shader
)
5520 return "GS Copy Shader as VS";
5522 return "Geometry Shader";
5523 case PIPE_SHADER_FRAGMENT
:
5524 return "Pixel Shader";
5525 case PIPE_SHADER_COMPUTE
:
5526 return "Compute Shader";
5528 return "Unknown Shader";
5532 void si_shader_dump(struct si_screen
*sscreen
, const struct si_shader
*shader
,
5533 struct pipe_debug_callback
*debug
, unsigned processor
,
5534 FILE *file
, bool check_debug_option
)
5536 if (!check_debug_option
||
5537 si_can_dump_shader(sscreen
, processor
))
5538 si_dump_shader_key(processor
, shader
, file
);
5540 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
5541 if (shader
->previous_stage
&&
5542 shader
->previous_stage
->binary
.llvm_ir_string
) {
5543 fprintf(file
, "\n%s - previous stage - LLVM IR:\n\n",
5544 si_get_shader_name(shader
, processor
));
5545 fprintf(file
, "%s\n", shader
->previous_stage
->binary
.llvm_ir_string
);
5548 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
5549 si_get_shader_name(shader
, processor
));
5550 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
5553 if (!check_debug_option
||
5554 (si_can_dump_shader(sscreen
, processor
) &&
5555 !(sscreen
->debug_flags
& DBG(NO_ASM
)))) {
5556 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5559 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5560 debug
, "prolog", file
);
5561 if (shader
->previous_stage
)
5562 si_shader_dump_disassembly(&shader
->previous_stage
->binary
,
5563 debug
, "previous stage", file
);
5564 if (shader
->prolog2
)
5565 si_shader_dump_disassembly(&shader
->prolog2
->binary
,
5566 debug
, "prolog2", file
);
5568 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5571 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5572 debug
, "epilog", file
);
5573 fprintf(file
, "\n");
5576 si_shader_dump_stats(sscreen
, shader
, processor
, file
,
5577 check_debug_option
);
5580 static int si_compile_llvm(struct si_screen
*sscreen
,
5581 struct ac_shader_binary
*binary
,
5582 struct si_shader_config
*conf
,
5583 struct ac_llvm_compiler
*compiler
,
5585 struct pipe_debug_callback
*debug
,
5588 bool less_optimized
)
5591 unsigned count
= p_atomic_inc_return(&sscreen
->num_compilations
);
5593 if (si_can_dump_shader(sscreen
, processor
)) {
5594 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5596 if (!(sscreen
->debug_flags
& (DBG(NO_IR
) | DBG(PREOPT_IR
)))) {
5597 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5598 ac_dump_module(mod
);
5599 fprintf(stderr
, "\n");
5603 if (sscreen
->record_llvm_ir
) {
5604 char *ir
= LLVMPrintModuleToString(mod
);
5605 binary
->llvm_ir_string
= strdup(ir
);
5606 LLVMDisposeMessage(ir
);
5609 if (!si_replace_shader(count
, binary
)) {
5610 r
= si_llvm_compile(mod
, binary
, compiler
, debug
,
5616 si_shader_binary_read_config(binary
, conf
, 0);
5618 /* Enable 64-bit and 16-bit denormals, because there is no performance
5621 * If denormals are enabled, all floating-point output modifiers are
5624 * Don't enable denormals for 32-bit floats, because:
5625 * - Floating-point output modifiers would be ignored by the hw.
5626 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5627 * have to stop using those.
5628 * - SI & CI would be very slow.
5630 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5632 FREE(binary
->config
);
5633 FREE(binary
->global_symbol_offsets
);
5634 binary
->config
= NULL
;
5635 binary
->global_symbol_offsets
= NULL
;
5637 /* Some shaders can't have rodata because their binaries can be
5640 if (binary
->rodata_size
&&
5641 (processor
== PIPE_SHADER_VERTEX
||
5642 processor
== PIPE_SHADER_TESS_CTRL
||
5643 processor
== PIPE_SHADER_TESS_EVAL
||
5644 processor
== PIPE_SHADER_FRAGMENT
)) {
5645 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5652 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
5654 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
5655 LLVMBuildRetVoid(ctx
->ac
.builder
);
5657 LLVMBuildRet(ctx
->ac
.builder
, ret
);
5660 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5662 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5663 struct ac_llvm_compiler
*compiler
,
5664 struct si_shader_selector
*gs_selector
,
5665 struct pipe_debug_callback
*debug
)
5667 struct si_shader_context ctx
;
5668 struct si_shader
*shader
;
5669 LLVMBuilderRef builder
;
5670 struct si_shader_output_values outputs
[SI_MAX_VS_OUTPUTS
];
5671 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
5675 shader
= CALLOC_STRUCT(si_shader
);
5679 /* We can leave the fence as permanently signaled because the GS copy
5680 * shader only becomes visible globally after it has been compiled. */
5681 util_queue_fence_init(&shader
->ready
);
5683 shader
->selector
= gs_selector
;
5684 shader
->is_gs_copy_shader
= true;
5686 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
5687 ctx
.shader
= shader
;
5688 ctx
.type
= PIPE_SHADER_VERTEX
;
5690 builder
= ctx
.ac
.builder
;
5692 create_function(&ctx
);
5693 preload_ring_buffers(&ctx
);
5695 LLVMValueRef voffset
=
5696 LLVMBuildMul(ctx
.ac
.builder
, ctx
.abi
.vertex_id
,
5697 LLVMConstInt(ctx
.i32
, 4, 0), "");
5699 /* Fetch the vertex stream ID.*/
5700 LLVMValueRef stream_id
;
5702 if (gs_selector
->so
.num_outputs
)
5703 stream_id
= si_unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
5705 stream_id
= ctx
.i32_0
;
5707 /* Fill in output information. */
5708 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5709 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
5710 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
5712 for (int chan
= 0; chan
< 4; chan
++) {
5713 outputs
[i
].vertex_stream
[chan
] =
5714 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
5718 LLVMBasicBlockRef end_bb
;
5719 LLVMValueRef switch_inst
;
5721 end_bb
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, ctx
.main_fn
, "end");
5722 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
5724 for (int stream
= 0; stream
< 4; stream
++) {
5725 LLVMBasicBlockRef bb
;
5728 if (!gsinfo
->num_stream_output_components
[stream
])
5731 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
5734 bb
= LLVMInsertBasicBlockInContext(ctx
.ac
.context
, end_bb
, "out");
5735 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
.i32
, stream
, 0), bb
);
5736 LLVMPositionBuilderAtEnd(builder
, bb
);
5738 /* Fetch vertex data from GSVS ring */
5740 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5741 for (unsigned chan
= 0; chan
< 4; chan
++) {
5742 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
5743 outputs
[i
].vertex_stream
[chan
] != stream
) {
5744 outputs
[i
].values
[chan
] = LLVMGetUndef(ctx
.f32
);
5748 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
5749 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
5752 outputs
[i
].values
[chan
] =
5753 ac_build_buffer_load(&ctx
.ac
,
5754 ctx
.gsvs_ring
[0], 1,
5761 /* Streamout and exports. */
5762 if (gs_selector
->so
.num_outputs
) {
5763 si_llvm_emit_streamout(&ctx
, outputs
,
5764 gsinfo
->num_outputs
,
5769 /* Vertex color clamping.
5771 * This uses a state constant loaded in a user data SGPR and
5772 * an IF statement is added that clamps all colors if the constant
5775 struct lp_build_if_state if_ctx
;
5776 LLVMValueRef v
[2], cond
= NULL
;
5777 LLVMBasicBlockRef blocks
[2];
5779 for (unsigned i
= 0; i
< gsinfo
->num_outputs
; i
++) {
5780 if (gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
5781 gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
5784 /* We've found a color. */
5786 /* The state is in the first bit of the user SGPR. */
5787 cond
= LLVMGetParam(ctx
.main_fn
,
5788 ctx
.param_vs_state_bits
);
5789 cond
= LLVMBuildTrunc(ctx
.ac
.builder
, cond
,
5791 lp_build_if(&if_ctx
, &ctx
.gallivm
, cond
);
5792 /* Remember blocks for Phi. */
5793 blocks
[0] = if_ctx
.true_block
;
5794 blocks
[1] = if_ctx
.entry_block
;
5797 for (unsigned j
= 0; j
< 4; j
++) {
5798 /* Insert clamp into the true block. */
5799 v
[0] = ac_build_clamp(&ctx
.ac
, outputs
[i
].values
[j
]);
5800 v
[1] = outputs
[i
].values
[j
];
5802 /* Insert Phi into the endif block. */
5803 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.merge_block
);
5804 outputs
[i
].values
[j
] = ac_build_phi(&ctx
.ac
, ctx
.f32
, 2, v
, blocks
);
5805 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.true_block
);
5809 lp_build_endif(&if_ctx
);
5811 si_llvm_export_vs(&ctx
, outputs
, gsinfo
->num_outputs
);
5814 LLVMBuildBr(builder
, end_bb
);
5817 LLVMPositionBuilderAtEnd(builder
, end_bb
);
5819 LLVMBuildRetVoid(ctx
.ac
.builder
);
5821 ctx
.type
= PIPE_SHADER_GEOMETRY
; /* override for shader dumping */
5822 si_llvm_optimize_module(&ctx
);
5824 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
5825 &ctx
.shader
->config
, ctx
.compiler
,
5827 debug
, PIPE_SHADER_GEOMETRY
,
5828 "GS Copy Shader", false);
5830 if (si_can_dump_shader(sscreen
, PIPE_SHADER_GEOMETRY
))
5831 fprintf(stderr
, "GS Copy Shader:\n");
5832 si_shader_dump(sscreen
, ctx
.shader
, debug
,
5833 PIPE_SHADER_GEOMETRY
, stderr
, true);
5834 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
5837 si_llvm_dispose(&ctx
);
5846 static void si_dump_shader_key_vs(const struct si_shader_key
*key
,
5847 const struct si_vs_prolog_bits
*prolog
,
5848 const char *prefix
, FILE *f
)
5850 fprintf(f
, " %s.instance_divisor_is_one = %u\n",
5851 prefix
, prolog
->instance_divisor_is_one
);
5852 fprintf(f
, " %s.instance_divisor_is_fetched = %u\n",
5853 prefix
, prolog
->instance_divisor_is_fetched
);
5854 fprintf(f
, " %s.ls_vgpr_fix = %u\n",
5855 prefix
, prolog
->ls_vgpr_fix
);
5857 fprintf(f
, " mono.vs.fix_fetch = {");
5858 for (int i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
5859 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs_fix_fetch
[i
]);
5863 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
5866 const struct si_shader_key
*key
= &shader
->key
;
5868 fprintf(f
, "SHADER KEY\n");
5870 switch (processor
) {
5871 case PIPE_SHADER_VERTEX
:
5872 si_dump_shader_key_vs(key
, &key
->part
.vs
.prolog
,
5873 "part.vs.prolog", f
);
5874 fprintf(f
, " as_es = %u\n", key
->as_es
);
5875 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
5876 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5877 key
->mono
.u
.vs_export_prim_id
);
5880 case PIPE_SHADER_TESS_CTRL
:
5881 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
) {
5882 si_dump_shader_key_vs(key
, &key
->part
.tcs
.ls_prolog
,
5883 "part.tcs.ls_prolog", f
);
5885 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
5886 fprintf(f
, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.u
.ff_tcs_inputs_to_copy
);
5889 case PIPE_SHADER_TESS_EVAL
:
5890 fprintf(f
, " as_es = %u\n", key
->as_es
);
5891 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5892 key
->mono
.u
.vs_export_prim_id
);
5895 case PIPE_SHADER_GEOMETRY
:
5896 if (shader
->is_gs_copy_shader
)
5899 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
&&
5900 key
->part
.gs
.es
->type
== PIPE_SHADER_VERTEX
) {
5901 si_dump_shader_key_vs(key
, &key
->part
.gs
.vs_prolog
,
5902 "part.gs.vs_prolog", f
);
5904 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
5907 case PIPE_SHADER_COMPUTE
:
5910 case PIPE_SHADER_FRAGMENT
:
5911 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
5912 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
5913 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
5914 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
5915 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
5916 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
5917 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
5918 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
5919 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
5920 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
5921 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
5922 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
5923 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
5924 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
5925 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
5926 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
5927 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
5934 if ((processor
== PIPE_SHADER_GEOMETRY
||
5935 processor
== PIPE_SHADER_TESS_EVAL
||
5936 processor
== PIPE_SHADER_VERTEX
) &&
5937 !key
->as_es
&& !key
->as_ls
) {
5938 fprintf(f
, " opt.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.kill_outputs
);
5939 fprintf(f
, " opt.clip_disable = %u\n", key
->opt
.clip_disable
);
5943 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5944 struct si_screen
*sscreen
,
5945 struct ac_llvm_compiler
*compiler
)
5947 struct lp_build_tgsi_context
*bld_base
;
5949 si_llvm_context_init(ctx
, sscreen
, compiler
);
5951 bld_base
= &ctx
->bld_base
;
5952 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5954 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
].emit
= build_interp_intrinsic
;
5955 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
].emit
= build_interp_intrinsic
;
5956 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
].emit
= build_interp_intrinsic
;
5958 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
5960 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
5962 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5963 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5964 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5965 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5967 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
5968 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
5969 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
5970 bld_base
->op_actions
[TGSI_OPCODE_BALLOT
].emit
= ballot_emit
;
5971 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].intr_name
= "llvm.amdgcn.readfirstlane";
5972 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].emit
= read_lane_emit
;
5973 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].intr_name
= "llvm.amdgcn.readlane";
5974 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].emit
= read_lane_emit
;
5976 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_tgsi_emit_vertex
;
5977 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_tgsi_emit_primitive
;
5978 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5981 static void si_optimize_vs_outputs(struct si_shader_context
*ctx
)
5983 struct si_shader
*shader
= ctx
->shader
;
5984 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5986 if ((ctx
->type
!= PIPE_SHADER_VERTEX
&&
5987 ctx
->type
!= PIPE_SHADER_TESS_EVAL
) ||
5988 shader
->key
.as_ls
||
5992 ac_optimize_vs_outputs(&ctx
->ac
,
5994 shader
->info
.vs_output_param_offset
,
5996 &shader
->info
.nr_param_exports
);
5999 static void si_init_exec_from_input(struct si_shader_context
*ctx
,
6000 unsigned param
, unsigned bitoffset
)
6002 LLVMValueRef args
[] = {
6003 LLVMGetParam(ctx
->main_fn
, param
),
6004 LLVMConstInt(ctx
->i32
, bitoffset
, 0),
6006 ac_build_intrinsic(&ctx
->ac
,
6007 "llvm.amdgcn.init.exec.from.input",
6008 ctx
->voidt
, args
, 2, AC_FUNC_ATTR_CONVERGENT
);
6011 static bool si_vs_needs_prolog(const struct si_shader_selector
*sel
,
6012 const struct si_vs_prolog_bits
*key
)
6014 /* VGPR initialization fixup for Vega10 and Raven is always done in the
6016 return sel
->vs_needs_prolog
|| key
->ls_vgpr_fix
;
6019 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
)
6021 struct si_shader
*shader
= ctx
->shader
;
6022 struct si_shader_selector
*sel
= shader
->selector
;
6023 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6025 // TODO clean all this up!
6026 switch (ctx
->type
) {
6027 case PIPE_SHADER_VERTEX
:
6028 ctx
->load_input
= declare_input_vs
;
6029 if (shader
->key
.as_ls
)
6030 ctx
->abi
.emit_outputs
= si_llvm_emit_ls_epilogue
;
6031 else if (shader
->key
.as_es
)
6032 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6034 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6035 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6036 ctx
->abi
.load_base_vertex
= get_base_vertex
;
6038 case PIPE_SHADER_TESS_CTRL
:
6039 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6040 ctx
->abi
.load_tess_varyings
= si_nir_load_tcs_varyings
;
6041 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6042 bld_base
->emit_store
= store_output_tcs
;
6043 ctx
->abi
.store_tcs_outputs
= si_nir_store_output_tcs
;
6044 ctx
->abi
.emit_outputs
= si_llvm_emit_tcs_epilogue
;
6045 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6046 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6048 case PIPE_SHADER_TESS_EVAL
:
6049 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6050 ctx
->abi
.load_tess_varyings
= si_nir_load_input_tes
;
6051 ctx
->abi
.load_tess_coord
= si_load_tess_coord
;
6052 ctx
->abi
.load_tess_level
= si_load_tess_level
;
6053 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6054 if (shader
->key
.as_es
)
6055 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6057 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6058 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6060 case PIPE_SHADER_GEOMETRY
:
6061 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6062 ctx
->abi
.load_inputs
= si_nir_load_input_gs
;
6063 ctx
->abi
.emit_vertex
= si_llvm_emit_vertex
;
6064 ctx
->abi
.emit_primitive
= si_llvm_emit_primitive
;
6065 ctx
->abi
.emit_outputs
= si_llvm_emit_gs_epilogue
;
6066 bld_base
->emit_epilogue
= si_tgsi_emit_gs_epilogue
;
6068 case PIPE_SHADER_FRAGMENT
:
6069 ctx
->load_input
= declare_input_fs
;
6070 ctx
->abi
.emit_outputs
= si_llvm_return_fs_outputs
;
6071 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6072 ctx
->abi
.lookup_interp_param
= si_nir_lookup_interp_param
;
6073 ctx
->abi
.load_sample_position
= load_sample_position
;
6074 ctx
->abi
.load_sample_mask_in
= load_sample_mask_in
;
6075 ctx
->abi
.emit_kill
= si_llvm_emit_kill
;
6077 case PIPE_SHADER_COMPUTE
:
6078 ctx
->abi
.load_local_group_size
= get_block_size
;
6081 assert(!"Unsupported shader type");
6085 ctx
->abi
.load_ubo
= load_ubo
;
6086 ctx
->abi
.load_ssbo
= load_ssbo
;
6088 create_function(ctx
);
6089 preload_ring_buffers(ctx
);
6091 /* For GFX9 merged shaders:
6092 * - Set EXEC for the first shader. If the prolog is present, set
6093 * EXEC there instead.
6094 * - Add a barrier before the second shader.
6095 * - In the second shader, reset EXEC to ~0 and wrap the main part in
6096 * an if-statement. This is required for correctness in geometry
6097 * shaders, to ensure that empty GS waves do not send GS_EMIT and
6100 * For monolithic merged shaders, the first shader is wrapped in an
6101 * if-block together with its prolog in si_build_wrapper_function.
6103 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6104 if (!shader
->is_monolithic
&&
6105 sel
->info
.num_instructions
> 1 && /* not empty shader */
6106 (shader
->key
.as_es
|| shader
->key
.as_ls
) &&
6107 (ctx
->type
== PIPE_SHADER_TESS_EVAL
||
6108 (ctx
->type
== PIPE_SHADER_VERTEX
&&
6109 !si_vs_needs_prolog(sel
, &shader
->key
.part
.vs
.prolog
)))) {
6110 si_init_exec_from_input(ctx
,
6111 ctx
->param_merged_wave_info
, 0);
6112 } else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
6113 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6114 if (!shader
->is_monolithic
)
6115 ac_init_exec_full_mask(&ctx
->ac
);
6117 LLVMValueRef num_threads
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 8, 8);
6119 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
6120 ac_get_thread_id(&ctx
->ac
), num_threads
, "");
6121 lp_build_if(&ctx
->merged_wrap_if_state
, &ctx
->gallivm
, ena
);
6123 /* The barrier must execute for all shaders in a
6126 * Execute the barrier inside the conditional block,
6127 * so that empty waves can jump directly to s_endpgm,
6128 * which will also signal the barrier.
6130 * If the shader is TCS and the TCS epilog is present
6131 * and contains a barrier, it will wait there and then
6134 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
6138 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&&
6139 sel
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
6140 for (unsigned i
= 0; i
< 6; i
++) {
6141 ctx
->invoc0_tess_factors
[i
] =
6142 ac_build_alloca_undef(&ctx
->ac
, ctx
->i32
, "");
6146 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6148 for (i
= 0; i
< 4; i
++) {
6149 ctx
->gs_next_vertex
[i
] =
6150 ac_build_alloca(&ctx
->ac
, ctx
->i32
, "");
6154 if (sel
->force_correct_derivs_after_kill
) {
6155 ctx
->postponed_kill
= ac_build_alloca_undef(&ctx
->ac
, ctx
->i1
, "");
6156 /* true = don't kill. */
6157 LLVMBuildStore(ctx
->ac
.builder
, LLVMConstInt(ctx
->i1
, 1, 0),
6158 ctx
->postponed_kill
);
6162 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6163 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6167 if (!si_nir_build_llvm(ctx
, sel
->nir
)) {
6168 fprintf(stderr
, "Failed to translate shader from NIR to LLVM\n");
6173 si_llvm_build_ret(ctx
, ctx
->return_value
);
6178 * Compute the VS prolog key, which contains all the information needed to
6179 * build the VS prolog function, and set shader->info bits where needed.
6181 * \param info Shader info of the vertex shader.
6182 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
6183 * \param prolog_key Key of the VS prolog
6184 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
6185 * \param key Output shader part key.
6187 static void si_get_vs_prolog_key(const struct tgsi_shader_info
*info
,
6188 unsigned num_input_sgprs
,
6189 const struct si_vs_prolog_bits
*prolog_key
,
6190 struct si_shader
*shader_out
,
6191 union si_shader_part_key
*key
)
6193 memset(key
, 0, sizeof(*key
));
6194 key
->vs_prolog
.states
= *prolog_key
;
6195 key
->vs_prolog
.num_input_sgprs
= num_input_sgprs
;
6196 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6197 key
->vs_prolog
.as_ls
= shader_out
->key
.as_ls
;
6198 key
->vs_prolog
.as_es
= shader_out
->key
.as_es
;
6200 if (shader_out
->selector
->type
== PIPE_SHADER_TESS_CTRL
) {
6201 key
->vs_prolog
.as_ls
= 1;
6202 key
->vs_prolog
.num_merged_next_stage_vgprs
= 2;
6203 } else if (shader_out
->selector
->type
== PIPE_SHADER_GEOMETRY
) {
6204 key
->vs_prolog
.as_es
= 1;
6205 key
->vs_prolog
.num_merged_next_stage_vgprs
= 5;
6208 /* Enable loading the InstanceID VGPR. */
6209 uint16_t input_mask
= u_bit_consecutive(0, info
->num_inputs
);
6211 if ((key
->vs_prolog
.states
.instance_divisor_is_one
|
6212 key
->vs_prolog
.states
.instance_divisor_is_fetched
) & input_mask
)
6213 shader_out
->info
.uses_instanceid
= true;
6217 * Compute the PS prolog key, which contains all the information needed to
6218 * build the PS prolog function, and set related bits in shader->config.
6220 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6221 union si_shader_part_key
*key
,
6222 bool separate_prolog
)
6224 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6226 memset(key
, 0, sizeof(*key
));
6227 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6228 key
->ps_prolog
.colors_read
= info
->colors_read
;
6229 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6230 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6231 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6232 (key
->ps_prolog
.colors_read
||
6233 key
->ps_prolog
.states
.force_persp_sample_interp
||
6234 key
->ps_prolog
.states
.force_linear_sample_interp
||
6235 key
->ps_prolog
.states
.force_persp_center_interp
||
6236 key
->ps_prolog
.states
.force_linear_center_interp
||
6237 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6238 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6239 key
->ps_prolog
.ancillary_vgpr_index
= shader
->info
.ancillary_vgpr_index
;
6241 if (info
->colors_read
) {
6242 unsigned *color
= shader
->selector
->color_attr_index
;
6244 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6245 /* BCOLORs are stored after the last input. */
6246 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6247 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6248 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6251 for (unsigned i
= 0; i
< 2; i
++) {
6252 unsigned interp
= info
->input_interpolate
[color
[i
]];
6253 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6255 if (!(info
->colors_read
& (0xf << i
*4)))
6258 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6260 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6261 interp
== TGSI_INTERPOLATE_COLOR
)
6262 interp
= TGSI_INTERPOLATE_CONSTANT
;
6265 case TGSI_INTERPOLATE_CONSTANT
:
6266 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6268 case TGSI_INTERPOLATE_PERSPECTIVE
:
6269 case TGSI_INTERPOLATE_COLOR
:
6270 /* Force the interpolation location for colors here. */
6271 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6272 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6273 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6274 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6277 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6278 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6279 shader
->config
.spi_ps_input_ena
|=
6280 S_0286CC_PERSP_SAMPLE_ENA(1);
6282 case TGSI_INTERPOLATE_LOC_CENTER
:
6283 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6284 shader
->config
.spi_ps_input_ena
|=
6285 S_0286CC_PERSP_CENTER_ENA(1);
6287 case TGSI_INTERPOLATE_LOC_CENTROID
:
6288 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6289 shader
->config
.spi_ps_input_ena
|=
6290 S_0286CC_PERSP_CENTROID_ENA(1);
6296 case TGSI_INTERPOLATE_LINEAR
:
6297 /* Force the interpolation location for colors here. */
6298 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
6299 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6300 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
6301 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6303 /* The VGPR assignment for non-monolithic shaders
6304 * works because InitialPSInputAddr is set on the
6305 * main shader and PERSP_PULL_MODEL is never used.
6308 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6309 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6310 separate_prolog
? 6 : 9;
6311 shader
->config
.spi_ps_input_ena
|=
6312 S_0286CC_LINEAR_SAMPLE_ENA(1);
6314 case TGSI_INTERPOLATE_LOC_CENTER
:
6315 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6316 separate_prolog
? 8 : 11;
6317 shader
->config
.spi_ps_input_ena
|=
6318 S_0286CC_LINEAR_CENTER_ENA(1);
6320 case TGSI_INTERPOLATE_LOC_CENTROID
:
6321 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6322 separate_prolog
? 10 : 13;
6323 shader
->config
.spi_ps_input_ena
|=
6324 S_0286CC_LINEAR_CENTROID_ENA(1);
6338 * Check whether a PS prolog is required based on the key.
6340 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
6342 return key
->ps_prolog
.colors_read
||
6343 key
->ps_prolog
.states
.force_persp_sample_interp
||
6344 key
->ps_prolog
.states
.force_linear_sample_interp
||
6345 key
->ps_prolog
.states
.force_persp_center_interp
||
6346 key
->ps_prolog
.states
.force_linear_center_interp
||
6347 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6348 key
->ps_prolog
.states
.bc_optimize_for_linear
||
6349 key
->ps_prolog
.states
.poly_stipple
||
6350 key
->ps_prolog
.states
.samplemask_log_ps_iter
;
6354 * Compute the PS epilog key, which contains all the information needed to
6355 * build the PS epilog function.
6357 static void si_get_ps_epilog_key(struct si_shader
*shader
,
6358 union si_shader_part_key
*key
)
6360 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6361 memset(key
, 0, sizeof(*key
));
6362 key
->ps_epilog
.colors_written
= info
->colors_written
;
6363 key
->ps_epilog
.writes_z
= info
->writes_z
;
6364 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
6365 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6366 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
6370 * Build the GS prolog function. Rotate the input vertices for triangle strips
6373 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
6374 union si_shader_part_key
*key
)
6376 unsigned num_sgprs
, num_vgprs
;
6377 struct si_function_info fninfo
;
6378 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6379 LLVMTypeRef returns
[48];
6380 LLVMValueRef func
, ret
;
6382 si_init_function_info(&fninfo
);
6384 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6385 if (key
->gs_prolog
.states
.gfx9_prev_is_vs
)
6386 num_sgprs
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
6388 num_sgprs
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
6389 num_vgprs
= 5; /* ES inputs are not needed by GS */
6391 num_sgprs
= GFX6_GS_NUM_USER_SGPR
+ 2;
6395 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
6396 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
6397 returns
[i
] = ctx
->i32
;
6400 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
6401 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
6402 returns
[num_sgprs
+ i
] = ctx
->f32
;
6405 /* Create the function. */
6406 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
6408 func
= ctx
->main_fn
;
6410 /* Set the full EXEC mask for the prolog, because we are only fiddling
6411 * with registers here. The main shader part will set the correct EXEC
6414 if (ctx
->screen
->info
.chip_class
>= GFX9
&& !key
->gs_prolog
.is_monolithic
)
6415 ac_init_exec_full_mask(&ctx
->ac
);
6417 /* Copy inputs to outputs. This should be no-op, as the registers match,
6418 * but it will prevent the compiler from overwriting them unintentionally.
6420 ret
= ctx
->return_value
;
6421 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
6422 LLVMValueRef p
= LLVMGetParam(func
, i
);
6423 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
6425 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
6426 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
6427 p
= ac_to_float(&ctx
->ac
, p
);
6428 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
6431 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
6432 /* Remap the input vertices for every other primitive. */
6433 const unsigned gfx6_vtx_params
[6] = {
6441 const unsigned gfx9_vtx_params
[3] = {
6446 LLVMValueRef vtx_in
[6], vtx_out
[6];
6447 LLVMValueRef prim_id
, rotate
;
6449 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6450 for (unsigned i
= 0; i
< 3; i
++) {
6451 vtx_in
[i
*2] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 0, 16);
6452 vtx_in
[i
*2+1] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 16, 16);
6455 for (unsigned i
= 0; i
< 6; i
++)
6456 vtx_in
[i
] = LLVMGetParam(func
, gfx6_vtx_params
[i
]);
6459 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
6460 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
6462 for (unsigned i
= 0; i
< 6; ++i
) {
6463 LLVMValueRef base
, rotated
;
6465 rotated
= vtx_in
[(i
+ 4) % 6];
6466 vtx_out
[i
] = LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
6469 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6470 for (unsigned i
= 0; i
< 3; i
++) {
6471 LLVMValueRef hi
, out
;
6473 hi
= LLVMBuildShl(builder
, vtx_out
[i
*2+1],
6474 LLVMConstInt(ctx
->i32
, 16, 0), "");
6475 out
= LLVMBuildOr(builder
, vtx_out
[i
*2], hi
, "");
6476 out
= ac_to_float(&ctx
->ac
, out
);
6477 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6478 gfx9_vtx_params
[i
], "");
6481 for (unsigned i
= 0; i
< 6; i
++) {
6484 out
= ac_to_float(&ctx
->ac
, vtx_out
[i
]);
6485 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6486 gfx6_vtx_params
[i
], "");
6491 LLVMBuildRet(builder
, ret
);
6495 * Given a list of shader part functions, build a wrapper function that
6496 * runs them in sequence to form a monolithic shader.
6498 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
6499 LLVMValueRef
*parts
,
6502 unsigned next_shader_first_part
)
6504 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6505 /* PS epilog has one arg per color component; gfx9 merged shader
6506 * prologs need to forward 32 user SGPRs.
6508 struct si_function_info fninfo
;
6509 LLVMValueRef initial
[64], out
[64];
6510 LLVMTypeRef function_type
;
6511 unsigned num_first_params
;
6512 unsigned num_out
, initial_num_out
;
6513 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
6514 MAYBE_UNUSED
unsigned initial_num_out_sgpr
; /* used in debug checks */
6515 unsigned num_sgprs
, num_vgprs
;
6517 struct lp_build_if_state if_state
;
6519 si_init_function_info(&fninfo
);
6521 for (unsigned i
= 0; i
< num_parts
; ++i
) {
6522 ac_add_function_attr(ctx
->ac
.context
, parts
[i
], -1,
6523 AC_FUNC_ATTR_ALWAYSINLINE
);
6524 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
6527 /* The parameters of the wrapper function correspond to those of the
6528 * first part in terms of SGPRs and VGPRs, but we use the types of the
6529 * main part to get the right types. This is relevant for the
6530 * dereferenceable attribute on descriptor table pointers.
6535 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
6536 num_first_params
= LLVMCountParamTypes(function_type
);
6538 for (unsigned i
= 0; i
< num_first_params
; ++i
) {
6539 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
6541 if (ac_is_sgpr_param(param
)) {
6542 assert(num_vgprs
== 0);
6543 num_sgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6545 num_vgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6550 while (gprs
< num_sgprs
+ num_vgprs
) {
6551 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], fninfo
.num_params
);
6552 LLVMTypeRef type
= LLVMTypeOf(param
);
6553 unsigned size
= ac_get_type_size(type
) / 4;
6555 add_arg(&fninfo
, gprs
< num_sgprs
? ARG_SGPR
: ARG_VGPR
, type
);
6557 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
6558 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
6559 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
6564 si_create_function(ctx
, "wrapper", NULL
, 0, &fninfo
,
6565 si_get_max_workgroup_size(ctx
->shader
));
6567 if (is_merged_shader(ctx
))
6568 ac_init_exec_full_mask(&ctx
->ac
);
6570 /* Record the arguments of the function as if they were an output of
6576 for (unsigned i
= 0; i
< fninfo
.num_params
; ++i
) {
6577 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
6578 LLVMTypeRef param_type
= LLVMTypeOf(param
);
6579 LLVMTypeRef out_type
= i
< fninfo
.num_sgpr_params
? ctx
->i32
: ctx
->f32
;
6580 unsigned size
= ac_get_type_size(param_type
) / 4;
6583 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6584 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i32
, "");
6585 param_type
= ctx
->i32
;
6588 if (param_type
!= out_type
)
6589 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
6590 out
[num_out
++] = param
;
6592 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
6594 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6595 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
6596 param_type
= ctx
->i64
;
6599 if (param_type
!= vector_type
)
6600 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
6602 for (unsigned j
= 0; j
< size
; ++j
)
6603 out
[num_out
++] = LLVMBuildExtractElement(
6604 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
6607 if (i
< fninfo
.num_sgpr_params
)
6608 num_out_sgpr
= num_out
;
6611 memcpy(initial
, out
, sizeof(out
));
6612 initial_num_out
= num_out
;
6613 initial_num_out_sgpr
= num_out_sgpr
;
6615 /* Now chain the parts. */
6616 for (unsigned part
= 0; part
< num_parts
; ++part
) {
6617 LLVMValueRef in
[48];
6619 LLVMTypeRef ret_type
;
6620 unsigned out_idx
= 0;
6621 unsigned num_params
= LLVMCountParams(parts
[part
]);
6623 /* Merged shaders are executed conditionally depending
6624 * on the number of enabled threads passed in the input SGPRs. */
6625 if (is_merged_shader(ctx
) && part
== 0) {
6626 LLVMValueRef ena
, count
= initial
[3];
6628 count
= LLVMBuildAnd(builder
, count
,
6629 LLVMConstInt(ctx
->i32
, 0x7f, 0), "");
6630 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
6631 ac_get_thread_id(&ctx
->ac
), count
, "");
6632 lp_build_if(&if_state
, &ctx
->gallivm
, ena
);
6635 /* Derive arguments for the next part from outputs of the
6638 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
6640 LLVMTypeRef param_type
;
6642 unsigned param_size
;
6643 LLVMValueRef arg
= NULL
;
6645 param
= LLVMGetParam(parts
[part
], param_idx
);
6646 param_type
= LLVMTypeOf(param
);
6647 param_size
= ac_get_type_size(param_type
) / 4;
6648 is_sgpr
= ac_is_sgpr_param(param
);
6651 ac_add_function_attr(ctx
->ac
.context
, parts
[part
],
6652 param_idx
+ 1, AC_FUNC_ATTR_INREG
);
6653 } else if (out_idx
< num_out_sgpr
) {
6654 /* Skip returned SGPRs the current part doesn't
6655 * declare on the input. */
6656 out_idx
= num_out_sgpr
;
6659 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
6661 if (param_size
== 1)
6664 arg
= ac_build_gather_values(&ctx
->ac
, &out
[out_idx
], param_size
);
6666 if (LLVMTypeOf(arg
) != param_type
) {
6667 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6668 if (LLVMGetPointerAddressSpace(param_type
) ==
6669 AC_CONST_32BIT_ADDR_SPACE
) {
6670 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
6671 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6673 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
6674 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6677 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
6681 in
[param_idx
] = arg
;
6682 out_idx
+= param_size
;
6685 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
6687 if (is_merged_shader(ctx
) &&
6688 part
+ 1 == next_shader_first_part
) {
6689 lp_build_endif(&if_state
);
6691 /* The second half of the merged shader should use
6692 * the inputs from the toplevel (wrapper) function,
6693 * not the return value from the last call.
6695 * That's because the last call was executed condi-
6696 * tionally, so we can't consume it in the main
6699 memcpy(out
, initial
, sizeof(initial
));
6700 num_out
= initial_num_out
;
6701 num_out_sgpr
= initial_num_out_sgpr
;
6705 /* Extract the returned GPRs. */
6706 ret_type
= LLVMTypeOf(ret
);
6710 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
6711 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
6713 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
6715 for (unsigned i
= 0; i
< ret_size
; ++i
) {
6717 LLVMBuildExtractValue(builder
, ret
, i
, "");
6719 assert(num_out
< ARRAY_SIZE(out
));
6720 out
[num_out
++] = val
;
6722 if (LLVMTypeOf(val
) == ctx
->i32
) {
6723 assert(num_out_sgpr
+ 1 == num_out
);
6724 num_out_sgpr
= num_out
;
6730 LLVMBuildRetVoid(builder
);
6733 static bool si_should_optimize_less(struct ac_llvm_compiler
*compiler
,
6734 struct si_shader_selector
*sel
)
6736 if (!compiler
->low_opt_passes
)
6739 /* Assume a slow CPU. */
6740 assert(!sel
->screen
->info
.has_dedicated_vram
&&
6741 sel
->screen
->info
.chip_class
<= VI
);
6743 /* For a crazy dEQP test containing 2597 memory opcodes, mostly
6745 return sel
->type
== PIPE_SHADER_COMPUTE
&&
6746 sel
->info
.num_memory_instructions
> 1000;
6749 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
6750 struct ac_llvm_compiler
*compiler
,
6751 struct si_shader
*shader
,
6752 struct pipe_debug_callback
*debug
)
6754 struct si_shader_selector
*sel
= shader
->selector
;
6755 struct si_shader_context ctx
;
6758 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6759 * conversion fails. */
6760 if (si_can_dump_shader(sscreen
, sel
->info
.processor
) &&
6761 !(sscreen
->debug_flags
& DBG(NO_TGSI
))) {
6763 tgsi_dump(sel
->tokens
, 0);
6765 nir_print_shader(sel
->nir
, stderr
);
6766 si_dump_streamout(&sel
->so
);
6769 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
6770 si_llvm_context_set_tgsi(&ctx
, shader
);
6772 memset(shader
->info
.vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6773 sizeof(shader
->info
.vs_output_param_offset
));
6775 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
6777 if (!si_compile_tgsi_main(&ctx
)) {
6778 si_llvm_dispose(&ctx
);
6782 if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
6783 LLVMValueRef parts
[2];
6784 bool need_prolog
= sel
->vs_needs_prolog
;
6786 parts
[1] = ctx
.main_fn
;
6789 union si_shader_part_key prolog_key
;
6790 si_get_vs_prolog_key(&sel
->info
,
6791 shader
->info
.num_input_sgprs
,
6792 &shader
->key
.part
.vs
.prolog
,
6793 shader
, &prolog_key
);
6794 si_build_vs_prolog_function(&ctx
, &prolog_key
);
6795 parts
[0] = ctx
.main_fn
;
6798 si_build_wrapper_function(&ctx
, parts
+ !need_prolog
,
6799 1 + need_prolog
, need_prolog
, 0);
6800 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
6801 if (sscreen
->info
.chip_class
>= GFX9
) {
6802 struct si_shader_selector
*ls
= shader
->key
.part
.tcs
.ls
;
6803 LLVMValueRef parts
[4];
6804 bool vs_needs_prolog
=
6805 si_vs_needs_prolog(ls
, &shader
->key
.part
.tcs
.ls_prolog
);
6808 parts
[2] = ctx
.main_fn
;
6811 union si_shader_part_key tcs_epilog_key
;
6812 memset(&tcs_epilog_key
, 0, sizeof(tcs_epilog_key
));
6813 tcs_epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6814 si_build_tcs_epilog_function(&ctx
, &tcs_epilog_key
);
6815 parts
[3] = ctx
.main_fn
;
6817 /* VS as LS main part */
6818 struct si_shader shader_ls
= {};
6819 shader_ls
.selector
= ls
;
6820 shader_ls
.key
.as_ls
= 1;
6821 shader_ls
.key
.mono
= shader
->key
.mono
;
6822 shader_ls
.key
.opt
= shader
->key
.opt
;
6823 shader_ls
.is_monolithic
= true;
6824 si_llvm_context_set_tgsi(&ctx
, &shader_ls
);
6826 if (!si_compile_tgsi_main(&ctx
)) {
6827 si_llvm_dispose(&ctx
);
6830 shader
->info
.uses_instanceid
|= ls
->info
.uses_instanceid
;
6831 parts
[1] = ctx
.main_fn
;
6834 if (vs_needs_prolog
) {
6835 union si_shader_part_key vs_prolog_key
;
6836 si_get_vs_prolog_key(&ls
->info
,
6837 shader_ls
.info
.num_input_sgprs
,
6838 &shader
->key
.part
.tcs
.ls_prolog
,
6839 shader
, &vs_prolog_key
);
6840 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6841 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6842 parts
[0] = ctx
.main_fn
;
6845 /* Reset the shader context. */
6846 ctx
.shader
= shader
;
6847 ctx
.type
= PIPE_SHADER_TESS_CTRL
;
6849 si_build_wrapper_function(&ctx
,
6850 parts
+ !vs_needs_prolog
,
6851 4 - !vs_needs_prolog
, vs_needs_prolog
,
6852 vs_needs_prolog
? 2 : 1);
6854 LLVMValueRef parts
[2];
6855 union si_shader_part_key epilog_key
;
6857 parts
[0] = ctx
.main_fn
;
6859 memset(&epilog_key
, 0, sizeof(epilog_key
));
6860 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6861 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
6862 parts
[1] = ctx
.main_fn
;
6864 si_build_wrapper_function(&ctx
, parts
, 2, 0, 0);
6866 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
6867 if (ctx
.screen
->info
.chip_class
>= GFX9
) {
6868 struct si_shader_selector
*es
= shader
->key
.part
.gs
.es
;
6869 LLVMValueRef es_prolog
= NULL
;
6870 LLVMValueRef es_main
= NULL
;
6871 LLVMValueRef gs_prolog
= NULL
;
6872 LLVMValueRef gs_main
= ctx
.main_fn
;
6875 union si_shader_part_key gs_prolog_key
;
6876 memset(&gs_prolog_key
, 0, sizeof(gs_prolog_key
));
6877 gs_prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6878 gs_prolog_key
.gs_prolog
.is_monolithic
= true;
6879 si_build_gs_prolog_function(&ctx
, &gs_prolog_key
);
6880 gs_prolog
= ctx
.main_fn
;
6883 struct si_shader shader_es
= {};
6884 shader_es
.selector
= es
;
6885 shader_es
.key
.as_es
= 1;
6886 shader_es
.key
.mono
= shader
->key
.mono
;
6887 shader_es
.key
.opt
= shader
->key
.opt
;
6888 shader_es
.is_monolithic
= true;
6889 si_llvm_context_set_tgsi(&ctx
, &shader_es
);
6891 if (!si_compile_tgsi_main(&ctx
)) {
6892 si_llvm_dispose(&ctx
);
6895 shader
->info
.uses_instanceid
|= es
->info
.uses_instanceid
;
6896 es_main
= ctx
.main_fn
;
6899 if (es
->vs_needs_prolog
) {
6900 union si_shader_part_key vs_prolog_key
;
6901 si_get_vs_prolog_key(&es
->info
,
6902 shader_es
.info
.num_input_sgprs
,
6903 &shader
->key
.part
.gs
.vs_prolog
,
6904 shader
, &vs_prolog_key
);
6905 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6906 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6907 es_prolog
= ctx
.main_fn
;
6910 /* Reset the shader context. */
6911 ctx
.shader
= shader
;
6912 ctx
.type
= PIPE_SHADER_GEOMETRY
;
6914 /* Prepare the array of shader parts. */
6915 LLVMValueRef parts
[4];
6916 unsigned num_parts
= 0, main_part
, next_first_part
;
6919 parts
[num_parts
++] = es_prolog
;
6921 parts
[main_part
= num_parts
++] = es_main
;
6922 parts
[next_first_part
= num_parts
++] = gs_prolog
;
6923 parts
[num_parts
++] = gs_main
;
6925 si_build_wrapper_function(&ctx
, parts
, num_parts
,
6926 main_part
, next_first_part
);
6928 LLVMValueRef parts
[2];
6929 union si_shader_part_key prolog_key
;
6931 parts
[1] = ctx
.main_fn
;
6933 memset(&prolog_key
, 0, sizeof(prolog_key
));
6934 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6935 si_build_gs_prolog_function(&ctx
, &prolog_key
);
6936 parts
[0] = ctx
.main_fn
;
6938 si_build_wrapper_function(&ctx
, parts
, 2, 1, 0);
6940 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6941 LLVMValueRef parts
[3];
6942 union si_shader_part_key prolog_key
;
6943 union si_shader_part_key epilog_key
;
6946 si_get_ps_prolog_key(shader
, &prolog_key
, false);
6947 need_prolog
= si_need_ps_prolog(&prolog_key
);
6949 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
6952 si_build_ps_prolog_function(&ctx
, &prolog_key
);
6953 parts
[0] = ctx
.main_fn
;
6956 si_get_ps_epilog_key(shader
, &epilog_key
);
6957 si_build_ps_epilog_function(&ctx
, &epilog_key
);
6958 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
6960 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2,
6961 need_prolog
? 1 : 0, 0);
6964 si_llvm_optimize_module(&ctx
);
6966 /* Post-optimization transformations and analysis. */
6967 si_optimize_vs_outputs(&ctx
);
6969 if ((debug
&& debug
->debug_message
) ||
6970 si_can_dump_shader(sscreen
, ctx
.type
)) {
6971 ctx
.shader
->config
.private_mem_vgprs
=
6972 ac_count_scratch_private_memory(ctx
.main_fn
);
6975 /* Make sure the input is a pointer and not integer followed by inttoptr. */
6976 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx
.main_fn
, 0))) ==
6977 LLVMPointerTypeKind
);
6979 /* Compile to bytecode. */
6980 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, compiler
,
6981 ctx
.ac
.module
, debug
, ctx
.type
,
6982 si_get_shader_name(shader
, ctx
.type
),
6983 si_should_optimize_less(compiler
, shader
->selector
));
6984 si_llvm_dispose(&ctx
);
6986 fprintf(stderr
, "LLVM failed to compile shader\n");
6990 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
6991 * LLVM 3.9svn has this bug.
6993 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
6994 unsigned wave_size
= 64;
6995 unsigned max_vgprs
= 256;
6996 unsigned max_sgprs
= sscreen
->info
.chip_class
>= VI
? 800 : 512;
6997 unsigned max_sgprs_per_wave
= 128;
6998 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
6999 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7000 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7002 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7003 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7005 if (shader
->config
.num_sgprs
> max_sgprs
||
7006 shader
->config
.num_vgprs
> max_vgprs
) {
7007 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7008 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7009 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7010 max_sgprs
, max_vgprs
);
7012 /* Just terminate the process, because dependent
7013 * shaders can hang due to bad input data, but use
7014 * the env var to allow shader-db to work.
7016 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7021 /* Add the scratch offset to input SGPRs. */
7022 if (shader
->config
.scratch_bytes_per_wave
&& !is_merged_shader(&ctx
))
7023 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7025 /* Calculate the number of fragment input VGPRs. */
7026 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7027 shader
->info
.num_input_vgprs
= 0;
7028 shader
->info
.face_vgpr_index
= -1;
7029 shader
->info
.ancillary_vgpr_index
= -1;
7031 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7032 shader
->info
.num_input_vgprs
+= 2;
7033 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7034 shader
->info
.num_input_vgprs
+= 2;
7035 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7036 shader
->info
.num_input_vgprs
+= 2;
7037 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7038 shader
->info
.num_input_vgprs
+= 3;
7039 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7040 shader
->info
.num_input_vgprs
+= 2;
7041 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7042 shader
->info
.num_input_vgprs
+= 2;
7043 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7044 shader
->info
.num_input_vgprs
+= 2;
7045 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7046 shader
->info
.num_input_vgprs
+= 1;
7047 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7048 shader
->info
.num_input_vgprs
+= 1;
7049 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7050 shader
->info
.num_input_vgprs
+= 1;
7051 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7052 shader
->info
.num_input_vgprs
+= 1;
7053 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7054 shader
->info
.num_input_vgprs
+= 1;
7055 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7056 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7057 shader
->info
.num_input_vgprs
+= 1;
7059 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
)) {
7060 shader
->info
.ancillary_vgpr_index
= shader
->info
.num_input_vgprs
;
7061 shader
->info
.num_input_vgprs
+= 1;
7063 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7064 shader
->info
.num_input_vgprs
+= 1;
7065 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7066 shader
->info
.num_input_vgprs
+= 1;
7069 si_calculate_max_simd_waves(shader
);
7070 si_shader_dump_stats_for_shader_db(shader
, debug
);
7075 * Create, compile and return a shader part (prolog or epilog).
7077 * \param sscreen screen
7078 * \param list list of shader parts of the same category
7079 * \param type shader type
7080 * \param key shader part key
7081 * \param prolog whether the part being requested is a prolog
7082 * \param tm LLVM target machine
7083 * \param debug debug callback
7084 * \param build the callback responsible for building the main function
7085 * \return non-NULL on success
7087 static struct si_shader_part
*
7088 si_get_shader_part(struct si_screen
*sscreen
,
7089 struct si_shader_part
**list
,
7090 enum pipe_shader_type type
,
7092 union si_shader_part_key
*key
,
7093 struct ac_llvm_compiler
*compiler
,
7094 struct pipe_debug_callback
*debug
,
7095 void (*build
)(struct si_shader_context
*,
7096 union si_shader_part_key
*),
7099 struct si_shader_part
*result
;
7101 mtx_lock(&sscreen
->shader_parts_mutex
);
7103 /* Find existing. */
7104 for (result
= *list
; result
; result
= result
->next
) {
7105 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7106 mtx_unlock(&sscreen
->shader_parts_mutex
);
7111 /* Compile a new one. */
7112 result
= CALLOC_STRUCT(si_shader_part
);
7115 struct si_shader shader
= {};
7116 struct si_shader_context ctx
;
7118 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
7119 ctx
.shader
= &shader
;
7123 case PIPE_SHADER_VERTEX
:
7124 shader
.key
.as_ls
= key
->vs_prolog
.as_ls
;
7125 shader
.key
.as_es
= key
->vs_prolog
.as_es
;
7127 case PIPE_SHADER_TESS_CTRL
:
7129 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7131 case PIPE_SHADER_GEOMETRY
:
7134 case PIPE_SHADER_FRAGMENT
:
7136 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7138 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7141 unreachable("bad shader part");
7147 si_llvm_optimize_module(&ctx
);
7149 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, compiler
,
7150 ctx
.ac
.module
, debug
, ctx
.type
, name
, false)) {
7156 result
->next
= *list
;
7160 si_llvm_dispose(&ctx
);
7161 mtx_unlock(&sscreen
->shader_parts_mutex
);
7165 static LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
7167 LLVMValueRef ptr
[2], list
;
7168 bool merged_shader
= is_merged_shader(ctx
);
7170 if (HAVE_32BIT_POINTERS
) {
7171 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7172 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
[0],
7173 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
7177 /* Get the pointer to rw buffers. */
7178 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7179 ptr
[1] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
+ 1);
7180 list
= ac_build_gather_values(&ctx
->ac
, ptr
, 2);
7181 list
= LLVMBuildBitCast(ctx
->ac
.builder
, list
, ctx
->i64
, "");
7182 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, list
,
7183 ac_array_in_const_addr_space(ctx
->v4i32
), "");
7188 * Build the vertex shader prolog function.
7190 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7191 * All inputs are returned unmodified. The vertex load indices are
7192 * stored after them, which will be used by the API VS for fetching inputs.
7194 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7199 * (VertexID + BaseVertex),
7200 * (InstanceID + StartInstance),
7201 * (InstanceID / 2 + StartInstance)
7203 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7204 union si_shader_part_key
*key
)
7206 struct si_function_info fninfo
;
7207 LLVMTypeRef
*returns
;
7208 LLVMValueRef ret
, func
;
7210 unsigned first_vs_vgpr
= key
->vs_prolog
.num_merged_next_stage_vgprs
;
7211 unsigned num_input_vgprs
= key
->vs_prolog
.num_merged_next_stage_vgprs
+ 4;
7212 LLVMValueRef input_vgprs
[9];
7213 unsigned num_all_input_regs
= key
->vs_prolog
.num_input_sgprs
+
7215 unsigned user_sgpr_base
= key
->vs_prolog
.num_merged_next_stage_vgprs
? 8 : 0;
7217 si_init_function_info(&fninfo
);
7219 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7220 returns
= alloca((num_all_input_regs
+ key
->vs_prolog
.last_input
+ 1) *
7221 sizeof(LLVMTypeRef
));
7224 /* Declare input and output SGPRs. */
7225 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7226 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7227 returns
[num_returns
++] = ctx
->i32
;
7230 /* Preloaded VGPRs (outputs must be floats) */
7231 for (i
= 0; i
< num_input_vgprs
; i
++) {
7232 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &input_vgprs
[i
]);
7233 returns
[num_returns
++] = ctx
->f32
;
7236 /* Vertex load indices. */
7237 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7238 returns
[num_returns
++] = ctx
->f32
;
7240 /* Create the function. */
7241 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, &fninfo
, 0);
7242 func
= ctx
->main_fn
;
7244 if (key
->vs_prolog
.num_merged_next_stage_vgprs
) {
7245 if (!key
->vs_prolog
.is_monolithic
)
7246 si_init_exec_from_input(ctx
, 3, 0);
7248 if (key
->vs_prolog
.as_ls
&&
7249 ctx
->screen
->has_ls_vgpr_init_bug
) {
7250 /* If there are no HS threads, SPI loads the LS VGPRs
7251 * starting at VGPR 0. Shift them back to where they
7254 LLVMValueRef has_hs_threads
=
7255 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
7256 si_unpack_param(ctx
, 3, 8, 8),
7259 for (i
= 4; i
> 0; --i
) {
7260 input_vgprs
[i
+ 1] =
7261 LLVMBuildSelect(ctx
->ac
.builder
, has_hs_threads
,
7263 input_vgprs
[i
- 1], "");
7268 ctx
->abi
.vertex_id
= input_vgprs
[first_vs_vgpr
];
7269 ctx
->abi
.instance_id
= input_vgprs
[first_vs_vgpr
+ (key
->vs_prolog
.as_ls
? 2 : 1)];
7271 /* Copy inputs to outputs. This should be no-op, as the registers match,
7272 * but it will prevent the compiler from overwriting them unintentionally.
7274 ret
= ctx
->return_value
;
7275 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7276 LLVMValueRef p
= LLVMGetParam(func
, i
);
7277 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7279 for (i
= 0; i
< num_input_vgprs
; i
++) {
7280 LLVMValueRef p
= input_vgprs
[i
];
7281 p
= ac_to_float(&ctx
->ac
, p
);
7282 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
,
7283 key
->vs_prolog
.num_input_sgprs
+ i
, "");
7286 /* Compute vertex load indices from instance divisors. */
7287 LLVMValueRef instance_divisor_constbuf
= NULL
;
7289 if (key
->vs_prolog
.states
.instance_divisor_is_fetched
) {
7290 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7291 LLVMValueRef buf_index
=
7292 LLVMConstInt(ctx
->i32
, SI_VS_CONST_INSTANCE_DIVISORS
, 0);
7293 instance_divisor_constbuf
=
7294 ac_build_load_to_sgpr(&ctx
->ac
, list
, buf_index
);
7297 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7298 bool divisor_is_one
=
7299 key
->vs_prolog
.states
.instance_divisor_is_one
& (1u << i
);
7300 bool divisor_is_fetched
=
7301 key
->vs_prolog
.states
.instance_divisor_is_fetched
& (1u << i
);
7304 if (divisor_is_one
|| divisor_is_fetched
) {
7305 LLVMValueRef divisor
= ctx
->i32_1
;
7307 if (divisor_is_fetched
) {
7308 divisor
= buffer_load_const(ctx
, instance_divisor_constbuf
,
7309 LLVMConstInt(ctx
->i32
, i
* 4, 0));
7310 divisor
= ac_to_integer(&ctx
->ac
, divisor
);
7313 /* InstanceID / Divisor + StartInstance */
7314 index
= get_instance_index_for_fetch(ctx
,
7316 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
);