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
, ctx
->i1false
);
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 if vertex data was written. */
4331 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
4332 si_get_gs_wave_id(ctx
));
4336 lp_build_endif(&if_state
);
4339 /* Emit one vertex from the geometry shader */
4340 static void si_tgsi_emit_vertex(
4341 const struct lp_build_tgsi_action
*action
,
4342 struct lp_build_tgsi_context
*bld_base
,
4343 struct lp_build_emit_data
*emit_data
)
4345 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4346 unsigned stream
= si_llvm_get_stream(bld_base
, emit_data
);
4348 si_llvm_emit_vertex(&ctx
->abi
, stream
, ctx
->outputs
[0]);
4351 /* Cut one primitive from the geometry shader */
4352 static void si_llvm_emit_primitive(struct ac_shader_abi
*abi
,
4355 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
4357 /* Signal primitive cut */
4358 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
4359 si_get_gs_wave_id(ctx
));
4362 /* Cut one primitive from the geometry shader */
4363 static void si_tgsi_emit_primitive(
4364 const struct lp_build_tgsi_action
*action
,
4365 struct lp_build_tgsi_context
*bld_base
,
4366 struct lp_build_emit_data
*emit_data
)
4368 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4370 si_llvm_emit_primitive(&ctx
->abi
, si_llvm_get_stream(bld_base
, emit_data
));
4373 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4374 struct lp_build_tgsi_context
*bld_base
,
4375 struct lp_build_emit_data
*emit_data
)
4377 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4379 /* SI only (thanks to a hw bug workaround):
4380 * The real barrier instruction isn’t needed, because an entire patch
4381 * always fits into a single wave.
4383 if (ctx
->screen
->info
.chip_class
== SI
&&
4384 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
4385 ac_build_waitcnt(&ctx
->ac
, LGKM_CNT
& VM_CNT
);
4389 ac_build_s_barrier(&ctx
->ac
);
4392 static void si_create_function(struct si_shader_context
*ctx
,
4394 LLVMTypeRef
*returns
, unsigned num_returns
,
4395 struct si_function_info
*fninfo
,
4396 unsigned max_workgroup_size
)
4400 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
4401 fninfo
->types
, fninfo
->num_params
);
4402 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
4404 for (i
= 0; i
< fninfo
->num_sgpr_params
; ++i
) {
4405 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
4407 /* The combination of:
4411 * allows the optimization passes to move loads and reduces
4412 * SGPR spilling significantly.
4414 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4415 AC_FUNC_ATTR_INREG
);
4417 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
4418 ac_add_function_attr(ctx
->ac
.context
, ctx
->main_fn
, i
+ 1,
4419 AC_FUNC_ATTR_NOALIAS
);
4420 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
4424 for (i
= 0; i
< fninfo
->num_params
; ++i
) {
4425 if (fninfo
->assign
[i
])
4426 *fninfo
->assign
[i
] = LLVMGetParam(ctx
->main_fn
, i
);
4429 if (ctx
->screen
->info
.address32_hi
) {
4430 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4431 "amdgpu-32bit-address-high-bits",
4432 ctx
->screen
->info
.address32_hi
);
4435 if (max_workgroup_size
) {
4436 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4437 "amdgpu-max-work-group-size",
4438 max_workgroup_size
);
4440 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4441 "no-signed-zeros-fp-math",
4444 if (ctx
->screen
->debug_flags
& DBG(UNSAFE_MATH
)) {
4445 /* These were copied from some LLVM test. */
4446 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4447 "less-precise-fpmad",
4449 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4452 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4455 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
4461 static void declare_streamout_params(struct si_shader_context
*ctx
,
4462 struct pipe_stream_output_info
*so
,
4463 struct si_function_info
*fninfo
)
4467 /* Streamout SGPRs. */
4468 if (so
->num_outputs
) {
4469 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
4470 ctx
->param_streamout_config
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4472 ctx
->param_streamout_config
= fninfo
->num_params
- 1;
4474 ctx
->param_streamout_write_index
= add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4476 /* A streamout buffer offset is loaded if the stride is non-zero. */
4477 for (i
= 0; i
< 4; i
++) {
4481 ctx
->param_streamout_offset
[i
] = add_arg(fninfo
, ARG_SGPR
, ctx
->ac
.i32
);
4485 static unsigned si_get_max_workgroup_size(const struct si_shader
*shader
)
4487 switch (shader
->selector
->type
) {
4488 case PIPE_SHADER_TESS_CTRL
:
4489 /* Return this so that LLVM doesn't remove s_barrier
4490 * instructions on chips where we use s_barrier. */
4491 return shader
->selector
->screen
->info
.chip_class
>= CIK
? 128 : 64;
4493 case PIPE_SHADER_GEOMETRY
:
4494 return shader
->selector
->screen
->info
.chip_class
>= GFX9
? 128 : 64;
4496 case PIPE_SHADER_COMPUTE
:
4497 break; /* see below */
4503 const unsigned *properties
= shader
->selector
->info
.properties
;
4504 unsigned max_work_group_size
=
4505 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
4506 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
4507 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
4509 if (!max_work_group_size
) {
4510 /* This is a variable group size compute shader,
4511 * compile it for the maximum possible group size.
4513 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
4515 return max_work_group_size
;
4518 static void declare_const_and_shader_buffers(struct si_shader_context
*ctx
,
4519 struct si_function_info
*fninfo
,
4522 LLVMTypeRef const_shader_buf_type
;
4524 if (ctx
->shader
->selector
->info
.const_buffers_declared
== 1 &&
4525 ctx
->shader
->selector
->info
.shader_buffers_declared
== 0)
4526 const_shader_buf_type
= ctx
->f32
;
4528 const_shader_buf_type
= ctx
->v4i32
;
4530 unsigned const_and_shader_buffers
=
4531 add_arg(fninfo
, ARG_SGPR
,
4532 ac_array_in_const32_addr_space(const_shader_buf_type
));
4535 ctx
->param_const_and_shader_buffers
= const_and_shader_buffers
;
4538 static void declare_samplers_and_images(struct si_shader_context
*ctx
,
4539 struct si_function_info
*fninfo
,
4542 unsigned samplers_and_images
=
4543 add_arg(fninfo
, ARG_SGPR
,
4544 ac_array_in_const32_addr_space(ctx
->v8i32
));
4547 ctx
->param_samplers_and_images
= samplers_and_images
;
4550 static void declare_per_stage_desc_pointers(struct si_shader_context
*ctx
,
4551 struct si_function_info
*fninfo
,
4554 declare_const_and_shader_buffers(ctx
, fninfo
, assign_params
);
4555 declare_samplers_and_images(ctx
, fninfo
, assign_params
);
4558 static void declare_global_desc_pointers(struct si_shader_context
*ctx
,
4559 struct si_function_info
*fninfo
)
4561 ctx
->param_rw_buffers
= add_arg(fninfo
, ARG_SGPR
,
4562 ac_array_in_const32_addr_space(ctx
->v4i32
));
4563 ctx
->param_bindless_samplers_and_images
= add_arg(fninfo
, ARG_SGPR
,
4564 ac_array_in_const32_addr_space(ctx
->v8i32
));
4567 static void declare_vs_specific_input_sgprs(struct si_shader_context
*ctx
,
4568 struct si_function_info
*fninfo
)
4570 ctx
->param_vs_state_bits
= add_arg(fninfo
, ARG_SGPR
, ctx
->i32
);
4571 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.base_vertex
);
4572 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.start_instance
);
4573 add_arg_assign(fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.draw_id
);
4576 static void declare_vs_input_vgprs(struct si_shader_context
*ctx
,
4577 struct si_function_info
*fninfo
,
4578 unsigned *num_prolog_vgprs
)
4580 struct si_shader
*shader
= ctx
->shader
;
4582 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.vertex_id
);
4583 if (shader
->key
.as_ls
) {
4584 ctx
->param_rel_auto_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4585 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4587 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.instance_id
);
4588 ctx
->param_vs_prim_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4590 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
); /* unused */
4592 if (!shader
->is_gs_copy_shader
) {
4593 /* Vertex load indices. */
4594 ctx
->param_vertex_index0
= fninfo
->num_params
;
4595 for (unsigned i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4596 add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4597 *num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
4601 static void declare_tes_input_vgprs(struct si_shader_context
*ctx
,
4602 struct si_function_info
*fninfo
)
4604 ctx
->param_tes_u
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4605 ctx
->param_tes_v
= add_arg(fninfo
, ARG_VGPR
, ctx
->f32
);
4606 ctx
->param_tes_rel_patch_id
= add_arg(fninfo
, ARG_VGPR
, ctx
->i32
);
4607 add_arg_assign(fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tes_patch_id
);
4611 /* Convenient merged shader definitions. */
4612 SI_SHADER_MERGED_VERTEX_TESSCTRL
= PIPE_SHADER_TYPES
,
4613 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
,
4616 static void create_function(struct si_shader_context
*ctx
)
4618 struct si_shader
*shader
= ctx
->shader
;
4619 struct si_function_info fninfo
;
4620 LLVMTypeRef returns
[16+32*4];
4621 unsigned i
, num_return_sgprs
;
4622 unsigned num_returns
= 0;
4623 unsigned num_prolog_vgprs
= 0;
4624 unsigned type
= ctx
->type
;
4625 unsigned vs_blit_property
=
4626 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_BLIT_SGPRS
];
4628 si_init_function_info(&fninfo
);
4630 /* Set MERGED shaders. */
4631 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
4632 if (shader
->key
.as_ls
|| type
== PIPE_SHADER_TESS_CTRL
)
4633 type
= SI_SHADER_MERGED_VERTEX_TESSCTRL
; /* LS or HS */
4634 else if (shader
->key
.as_es
|| type
== PIPE_SHADER_GEOMETRY
)
4635 type
= SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
;
4638 LLVMTypeRef v3i32
= LLVMVectorType(ctx
->i32
, 3);
4641 case PIPE_SHADER_VERTEX
:
4642 declare_global_desc_pointers(ctx
, &fninfo
);
4644 if (vs_blit_property
) {
4645 ctx
->param_vs_blit_inputs
= fninfo
.num_params
;
4646 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x1, y1 */
4647 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* i16 x2, y2 */
4648 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* depth */
4650 if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_COLOR
) {
4651 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color0 */
4652 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color1 */
4653 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color2 */
4654 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* color3 */
4655 } else if (vs_blit_property
== SI_VS_BLIT_SGPRS_POS_TEXCOORD
) {
4656 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x1 */
4657 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y1 */
4658 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.x2 */
4659 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.y2 */
4660 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.z */
4661 add_arg(&fninfo
, ARG_SGPR
, ctx
->f32
); /* texcoord.w */
4665 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4669 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4670 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4671 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4672 ac_array_in_const32_addr_space(ctx
->v4i32
));
4674 if (shader
->key
.as_es
) {
4675 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4676 } else if (shader
->key
.as_ls
) {
4677 /* no extra parameters */
4679 if (shader
->is_gs_copy_shader
) {
4680 fninfo
.num_params
= ctx
->param_vs_state_bits
+ 1;
4681 fninfo
.num_sgpr_params
= fninfo
.num_params
;
4684 /* The locations of the other parameters are assigned dynamically. */
4685 declare_streamout_params(ctx
, &shader
->selector
->so
,
4690 declare_vs_input_vgprs(ctx
, &fninfo
, &num_prolog_vgprs
);
4693 case PIPE_SHADER_TESS_CTRL
: /* SI-CI-VI */
4694 declare_global_desc_pointers(ctx
, &fninfo
);
4695 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4696 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4697 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4698 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4699 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4700 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4701 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4704 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4705 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4707 /* param_tcs_offchip_offset and param_tcs_factor_offset are
4708 * placed after the user SGPRs.
4710 for (i
= 0; i
< GFX6_TCS_NUM_USER_SGPR
+ 2; i
++)
4711 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4712 for (i
= 0; i
< 11; i
++)
4713 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4716 case SI_SHADER_MERGED_VERTEX_TESSCTRL
:
4717 /* Merged stages have 8 system SGPRs at the beginning. */
4718 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
4719 if (HAVE_32BIT_POINTERS
) {
4720 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4721 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4723 declare_const_and_shader_buffers(ctx
, &fninfo
,
4724 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4726 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4727 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4728 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4729 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4730 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4731 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4733 declare_global_desc_pointers(ctx
, &fninfo
);
4734 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4735 ctx
->type
== PIPE_SHADER_VERTEX
);
4736 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4738 if (!HAVE_32BIT_POINTERS
) {
4739 declare_samplers_and_images(ctx
, &fninfo
,
4740 ctx
->type
== PIPE_SHADER_TESS_CTRL
);
4742 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4743 ctx
->param_tcs_out_lds_offsets
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4744 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4745 if (!HAVE_32BIT_POINTERS
) /* Align to 2 dwords. */
4746 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4747 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4748 ac_array_in_const32_addr_space(ctx
->v4i32
));
4750 /* VGPRs (first TCS, then VS) */
4751 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_patch_id
);
4752 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.tcs_rel_ids
);
4754 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4755 declare_vs_input_vgprs(ctx
, &fninfo
,
4758 /* LS return values are inputs to the TCS main shader part. */
4759 for (i
= 0; i
< 8 + GFX9_TCS_NUM_USER_SGPR
; i
++)
4760 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4761 for (i
= 0; i
< 2; i
++)
4762 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4764 /* TCS return values are inputs to the TCS epilog.
4766 * param_tcs_offchip_offset, param_tcs_factor_offset,
4767 * param_tcs_offchip_layout, and param_rw_buffers
4768 * should be passed to the epilog.
4770 for (i
= 0; i
<= 8 + GFX9_SGPR_TCS_OUT_LAYOUT
; i
++)
4771 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4772 for (i
= 0; i
< 11; i
++)
4773 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4777 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
:
4778 /* Merged stages have 8 system SGPRs at the beginning. */
4779 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
4780 if (HAVE_32BIT_POINTERS
) {
4781 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4782 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4784 declare_const_and_shader_buffers(ctx
, &fninfo
,
4785 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4787 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4788 ctx
->param_merged_wave_info
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4789 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4790 ctx
->param_merged_scratch_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4791 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS << 8) */
4792 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
4794 declare_global_desc_pointers(ctx
, &fninfo
);
4795 declare_per_stage_desc_pointers(ctx
, &fninfo
,
4796 (ctx
->type
== PIPE_SHADER_VERTEX
||
4797 ctx
->type
== PIPE_SHADER_TESS_EVAL
));
4798 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4799 declare_vs_specific_input_sgprs(ctx
, &fninfo
);
4801 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4802 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4803 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4804 /* Declare as many input SGPRs as the VS has. */
4805 if (!HAVE_32BIT_POINTERS
)
4806 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* unused */
4809 if (!HAVE_32BIT_POINTERS
) {
4810 declare_samplers_and_images(ctx
, &fninfo
,
4811 ctx
->type
== PIPE_SHADER_GEOMETRY
);
4813 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4814 ctx
->param_vertex_buffers
= add_arg(&fninfo
, ARG_SGPR
,
4815 ac_array_in_const32_addr_space(ctx
->v4i32
));
4818 /* VGPRs (first GS, then VS/TES) */
4819 ctx
->param_gs_vtx01_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4820 ctx
->param_gs_vtx23_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4821 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4822 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4823 ctx
->param_gs_vtx45_offset
= add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
4825 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
4826 declare_vs_input_vgprs(ctx
, &fninfo
,
4828 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4829 declare_tes_input_vgprs(ctx
, &fninfo
);
4832 if (ctx
->type
== PIPE_SHADER_VERTEX
||
4833 ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
4834 unsigned num_user_sgprs
;
4836 if (ctx
->type
== PIPE_SHADER_VERTEX
)
4837 num_user_sgprs
= GFX9_VSGS_NUM_USER_SGPR
;
4839 num_user_sgprs
= GFX9_TESGS_NUM_USER_SGPR
;
4841 /* ES return values are inputs to GS. */
4842 for (i
= 0; i
< 8 + num_user_sgprs
; i
++)
4843 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4844 for (i
= 0; i
< 5; i
++)
4845 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4849 case PIPE_SHADER_TESS_EVAL
:
4850 declare_global_desc_pointers(ctx
, &fninfo
);
4851 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4852 ctx
->param_vs_state_bits
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4853 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4854 ctx
->param_tes_offchip_addr
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4856 if (shader
->key
.as_es
) {
4857 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4858 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4859 ctx
->param_es2gs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4861 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4862 declare_streamout_params(ctx
, &shader
->selector
->so
,
4864 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4868 declare_tes_input_vgprs(ctx
, &fninfo
);
4871 case PIPE_SHADER_GEOMETRY
:
4872 declare_global_desc_pointers(ctx
, &fninfo
);
4873 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4874 ctx
->param_gs2vs_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4875 ctx
->param_gs_wave_id
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
4878 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[0]);
4879 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[1]);
4880 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_prim_id
);
4881 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[2]);
4882 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[3]);
4883 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[4]);
4884 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->gs_vtx_offset
[5]);
4885 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &ctx
->abi
.gs_invocation_id
);
4888 case PIPE_SHADER_FRAGMENT
:
4889 declare_global_desc_pointers(ctx
, &fninfo
);
4890 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4891 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
4892 add_arg_assign_checked(&fninfo
, ARG_SGPR
, ctx
->i32
,
4893 &ctx
->abi
.prim_mask
, SI_PARAM_PRIM_MASK
);
4895 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_SAMPLE
);
4896 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTER
);
4897 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_PERSP_CENTROID
);
4898 add_arg_checked(&fninfo
, ARG_VGPR
, v3i32
, SI_PARAM_PERSP_PULL_MODEL
);
4899 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_SAMPLE
);
4900 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTER
);
4901 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->v2i32
, SI_PARAM_LINEAR_CENTROID
);
4902 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->f32
, SI_PARAM_LINE_STIPPLE_TEX
);
4903 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4904 &ctx
->abi
.frag_pos
[0], SI_PARAM_POS_X_FLOAT
);
4905 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4906 &ctx
->abi
.frag_pos
[1], SI_PARAM_POS_Y_FLOAT
);
4907 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4908 &ctx
->abi
.frag_pos
[2], SI_PARAM_POS_Z_FLOAT
);
4909 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4910 &ctx
->abi
.frag_pos
[3], SI_PARAM_POS_W_FLOAT
);
4911 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4912 &ctx
->abi
.front_face
, SI_PARAM_FRONT_FACE
);
4913 shader
->info
.face_vgpr_index
= 20;
4914 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->i32
,
4915 &ctx
->abi
.ancillary
, SI_PARAM_ANCILLARY
);
4916 shader
->info
.ancillary_vgpr_index
= 21;
4917 add_arg_assign_checked(&fninfo
, ARG_VGPR
, ctx
->f32
,
4918 &ctx
->abi
.sample_coverage
, SI_PARAM_SAMPLE_COVERAGE
);
4919 add_arg_checked(&fninfo
, ARG_VGPR
, ctx
->i32
, SI_PARAM_POS_FIXED_PT
);
4921 /* Color inputs from the prolog. */
4922 if (shader
->selector
->info
.colors_read
) {
4923 unsigned num_color_elements
=
4924 util_bitcount(shader
->selector
->info
.colors_read
);
4926 assert(fninfo
.num_params
+ num_color_elements
<= ARRAY_SIZE(fninfo
.types
));
4927 for (i
= 0; i
< num_color_elements
; i
++)
4928 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
4930 num_prolog_vgprs
+= num_color_elements
;
4933 /* Outputs for the epilog. */
4934 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4937 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4938 shader
->selector
->info
.writes_z
+
4939 shader
->selector
->info
.writes_stencil
+
4940 shader
->selector
->info
.writes_samplemask
+
4941 1 /* SampleMaskIn */;
4943 num_returns
= MAX2(num_returns
,
4945 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4947 for (i
= 0; i
< num_return_sgprs
; i
++)
4948 returns
[i
] = ctx
->i32
;
4949 for (; i
< num_returns
; i
++)
4950 returns
[i
] = ctx
->f32
;
4953 case PIPE_SHADER_COMPUTE
:
4954 declare_global_desc_pointers(ctx
, &fninfo
);
4955 declare_per_stage_desc_pointers(ctx
, &fninfo
, true);
4956 if (shader
->selector
->info
.uses_grid_size
)
4957 add_arg_assign(&fninfo
, ARG_SGPR
, v3i32
, &ctx
->abi
.num_work_groups
);
4958 if (shader
->selector
->info
.uses_block_size
&&
4959 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
4960 ctx
->param_block_size
= add_arg(&fninfo
, ARG_SGPR
, v3i32
);
4962 unsigned cs_user_data_dwords
=
4963 shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_USER_DATA_DWORDS
];
4964 if (cs_user_data_dwords
) {
4965 ctx
->param_cs_user_data
= add_arg(&fninfo
, ARG_SGPR
,
4966 LLVMVectorType(ctx
->i32
, cs_user_data_dwords
));
4969 for (i
= 0; i
< 3; i
++) {
4970 ctx
->abi
.workgroup_ids
[i
] = NULL
;
4971 if (shader
->selector
->info
.uses_block_id
[i
])
4972 add_arg_assign(&fninfo
, ARG_SGPR
, ctx
->i32
, &ctx
->abi
.workgroup_ids
[i
]);
4975 add_arg_assign(&fninfo
, ARG_VGPR
, v3i32
, &ctx
->abi
.local_invocation_ids
);
4978 assert(0 && "unimplemented shader");
4982 si_create_function(ctx
, "main", returns
, num_returns
, &fninfo
,
4983 si_get_max_workgroup_size(shader
));
4985 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4986 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&& !ctx
->shader
->is_monolithic
) {
4987 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
4988 "InitialPSInputAddr",
4989 S_0286D0_PERSP_SAMPLE_ENA(1) |
4990 S_0286D0_PERSP_CENTER_ENA(1) |
4991 S_0286D0_PERSP_CENTROID_ENA(1) |
4992 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4993 S_0286D0_LINEAR_CENTER_ENA(1) |
4994 S_0286D0_LINEAR_CENTROID_ENA(1) |
4995 S_0286D0_FRONT_FACE_ENA(1) |
4996 S_0286D0_ANCILLARY_ENA(1) |
4997 S_0286D0_POS_FIXED_PT_ENA(1));
5000 shader
->info
.num_input_sgprs
= 0;
5001 shader
->info
.num_input_vgprs
= 0;
5003 for (i
= 0; i
< fninfo
.num_sgpr_params
; ++i
)
5004 shader
->info
.num_input_sgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5006 for (; i
< fninfo
.num_params
; ++i
)
5007 shader
->info
.num_input_vgprs
+= ac_get_type_size(fninfo
.types
[i
]) / 4;
5009 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5010 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5012 if (shader
->key
.as_ls
||
5013 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5014 /* GFX9 has the ESGS ring buffer in LDS. */
5015 type
== SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY
)
5016 ac_declare_lds_as_pointer(&ctx
->ac
);
5020 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5023 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5025 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5027 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5028 ctx
->param_rw_buffers
);
5030 if (ctx
->screen
->info
.chip_class
<= VI
&&
5031 (ctx
->shader
->key
.as_es
|| ctx
->type
== PIPE_SHADER_GEOMETRY
)) {
5033 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5035 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, ring
, 0);
5038 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5041 if (ctx
->shader
->is_gs_copy_shader
) {
5042 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5045 ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5046 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5047 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5048 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, SI_RING_GSVS
, 0);
5049 LLVMValueRef base_ring
;
5051 base_ring
= ac_build_load_to_sgpr(&ctx
->ac
, buf_ptr
, offset
);
5053 /* The conceptual layout of the GSVS ring is
5054 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5055 * but the real memory layout is swizzled across
5057 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5059 * Override the buffer descriptor accordingly.
5061 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5062 uint64_t stream_offset
= 0;
5064 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5065 unsigned num_components
;
5067 unsigned num_records
;
5068 LLVMValueRef ring
, tmp
;
5070 num_components
= sel
->info
.num_stream_output_components
[stream
];
5071 if (!num_components
)
5074 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5076 /* Limit on the stride field for <= CIK. */
5077 assert(stride
< (1 << 14));
5081 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5082 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_0
, "");
5083 tmp
= LLVMBuildAdd(builder
, tmp
,
5084 LLVMConstInt(ctx
->i64
,
5085 stream_offset
, 0), "");
5086 stream_offset
+= stride
* 64;
5088 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_0
, "");
5089 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5090 tmp
= LLVMBuildExtractElement(builder
, ring
, ctx
->i32_1
, "");
5091 tmp
= LLVMBuildOr(builder
, tmp
,
5092 LLVMConstInt(ctx
->i32
,
5093 S_008F04_STRIDE(stride
) |
5094 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5095 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, ctx
->i32_1
, "");
5096 ring
= LLVMBuildInsertElement(builder
, ring
,
5097 LLVMConstInt(ctx
->i32
, num_records
, 0),
5098 LLVMConstInt(ctx
->i32
, 2, 0), "");
5099 ring
= LLVMBuildInsertElement(builder
, ring
,
5100 LLVMConstInt(ctx
->i32
,
5101 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5102 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5103 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5104 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5105 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5106 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5107 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5108 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5109 S_008F0C_ADD_TID_ENABLE(1),
5111 LLVMConstInt(ctx
->i32
, 3, 0), "");
5113 ctx
->gsvs_ring
[stream
] = ring
;
5115 } else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
) {
5116 ctx
->tess_offchip_ring
= get_tess_ring_descriptor(ctx
, TESS_OFFCHIP_RING_TES
);
5120 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5121 LLVMValueRef param_rw_buffers
,
5122 unsigned param_pos_fixed_pt
)
5124 LLVMBuilderRef builder
= ctx
->ac
.builder
;
5125 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5127 /* Use the fixed-point gl_FragCoord input.
5128 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5129 * per coordinate to get the repeating effect.
5131 address
[0] = si_unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5132 address
[1] = si_unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5134 /* Load the buffer descriptor. */
5135 slot
= LLVMConstInt(ctx
->i32
, SI_PS_CONST_POLY_STIPPLE
, 0);
5136 desc
= ac_build_load_to_sgpr(&ctx
->ac
, param_rw_buffers
, slot
);
5138 /* The stipple pattern is 32x32, each row has 32 bits. */
5139 offset
= LLVMBuildMul(builder
, address
[1],
5140 LLVMConstInt(ctx
->i32
, 4, 0), "");
5141 row
= buffer_load_const(ctx
, desc
, offset
);
5142 row
= ac_to_integer(&ctx
->ac
, row
);
5143 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5144 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5145 ac_build_kill_if_false(&ctx
->ac
, bit
);
5148 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
5149 struct si_shader_config
*conf
,
5150 unsigned symbol_offset
)
5153 const unsigned char *config
=
5154 ac_shader_binary_config_start(binary
, symbol_offset
);
5155 bool really_needs_scratch
= false;
5157 /* LLVM adds SGPR spills to the scratch size.
5158 * Find out if we really need the scratch buffer.
5160 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5161 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
5163 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5164 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5165 really_needs_scratch
= true;
5170 /* XXX: We may be able to emit some of these values directly rather than
5171 * extracting fields to be emitted later.
5174 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5175 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5176 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5178 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5179 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5180 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5181 case R_00B428_SPI_SHADER_PGM_RSRC1_HS
:
5182 case R_00B848_COMPUTE_PGM_RSRC1
:
5183 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5184 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5185 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5186 conf
->rsrc1
= value
;
5188 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5189 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5191 case R_00B84C_COMPUTE_PGM_RSRC2
:
5192 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5193 conf
->rsrc2
= value
;
5195 case R_0286CC_SPI_PS_INPUT_ENA
:
5196 conf
->spi_ps_input_ena
= value
;
5198 case R_0286D0_SPI_PS_INPUT_ADDR
:
5199 conf
->spi_ps_input_addr
= value
;
5201 case R_0286E8_SPI_TMPRING_SIZE
:
5202 case R_00B860_COMPUTE_TMPRING_SIZE
:
5203 /* WAVESIZE is in units of 256 dwords. */
5204 if (really_needs_scratch
)
5205 conf
->scratch_bytes_per_wave
=
5206 G_00B860_WAVESIZE(value
) * 256 * 4;
5208 case 0x4: /* SPILLED_SGPRS */
5209 conf
->spilled_sgprs
= value
;
5211 case 0x8: /* SPILLED_VGPRS */
5212 conf
->spilled_vgprs
= value
;
5216 static bool printed
;
5219 fprintf(stderr
, "Warning: LLVM emitted unknown "
5220 "config register: 0x%x\n", reg
);
5228 if (!conf
->spi_ps_input_addr
)
5229 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5232 void si_shader_apply_scratch_relocs(struct si_shader
*shader
,
5233 uint64_t scratch_va
)
5236 uint32_t scratch_rsrc_dword0
= scratch_va
;
5237 uint32_t scratch_rsrc_dword1
=
5238 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5240 /* Enable scratch coalescing. */
5241 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5243 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5244 const struct ac_shader_reloc
*reloc
=
5245 &shader
->binary
.relocs
[i
];
5246 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5247 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5248 &scratch_rsrc_dword0
, 4);
5249 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5250 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5251 &scratch_rsrc_dword1
, 4);
5256 /* For the UMR disassembler. */
5257 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
5258 #define DEBUGGER_NUM_MARKERS 5
5260 static unsigned si_get_shader_binary_size(const struct si_shader
*shader
)
5262 unsigned size
= shader
->binary
.code_size
;
5265 size
+= shader
->prolog
->binary
.code_size
;
5266 if (shader
->previous_stage
)
5267 size
+= shader
->previous_stage
->binary
.code_size
;
5268 if (shader
->prolog2
)
5269 size
+= shader
->prolog2
->binary
.code_size
;
5271 size
+= shader
->epilog
->binary
.code_size
;
5272 return size
+ DEBUGGER_NUM_MARKERS
* 4;
5275 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5277 const struct ac_shader_binary
*prolog
=
5278 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
5279 const struct ac_shader_binary
*previous_stage
=
5280 shader
->previous_stage
? &shader
->previous_stage
->binary
: NULL
;
5281 const struct ac_shader_binary
*prolog2
=
5282 shader
->prolog2
? &shader
->prolog2
->binary
: NULL
;
5283 const struct ac_shader_binary
*epilog
=
5284 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
5285 const struct ac_shader_binary
*mainb
= &shader
->binary
;
5286 unsigned bo_size
= si_get_shader_binary_size(shader
) +
5287 (!epilog
? mainb
->rodata_size
: 0);
5290 assert(!prolog
|| !prolog
->rodata_size
);
5291 assert(!previous_stage
|| !previous_stage
->rodata_size
);
5292 assert(!prolog2
|| !prolog2
->rodata_size
);
5293 assert((!prolog
&& !previous_stage
&& !prolog2
&& !epilog
) ||
5294 !mainb
->rodata_size
);
5295 assert(!epilog
|| !epilog
->rodata_size
);
5297 r600_resource_reference(&shader
->bo
, NULL
);
5298 shader
->bo
= si_aligned_buffer_create(&sscreen
->b
,
5299 sscreen
->cpdma_prefetch_writes_memory
?
5300 0 : SI_RESOURCE_FLAG_READ_ONLY
,
5301 PIPE_USAGE_IMMUTABLE
,
5302 align(bo_size
, SI_CPDMA_ALIGNMENT
),
5308 ptr
= sscreen
->ws
->buffer_map(shader
->bo
->buf
, NULL
,
5309 PIPE_TRANSFER_READ_WRITE
|
5310 PIPE_TRANSFER_UNSYNCHRONIZED
);
5312 /* Don't use util_memcpy_cpu_to_le32. LLVM binaries are
5313 * endian-independent. */
5315 memcpy(ptr
, prolog
->code
, prolog
->code_size
);
5316 ptr
+= prolog
->code_size
;
5318 if (previous_stage
) {
5319 memcpy(ptr
, previous_stage
->code
, previous_stage
->code_size
);
5320 ptr
+= previous_stage
->code_size
;
5323 memcpy(ptr
, prolog2
->code
, prolog2
->code_size
);
5324 ptr
+= prolog2
->code_size
;
5327 memcpy(ptr
, mainb
->code
, mainb
->code_size
);
5328 ptr
+= mainb
->code_size
;
5331 memcpy(ptr
, epilog
->code
, epilog
->code_size
);
5332 ptr
+= epilog
->code_size
;
5333 } else if (mainb
->rodata_size
> 0) {
5334 memcpy(ptr
, mainb
->rodata
, mainb
->rodata_size
);
5335 ptr
+= mainb
->rodata_size
;
5338 /* Add end-of-code markers for the UMR disassembler. */
5339 uint32_t *ptr32
= (uint32_t*)ptr
;
5340 for (unsigned i
= 0; i
< DEBUGGER_NUM_MARKERS
; i
++)
5341 ptr32
[i
] = DEBUGGER_END_OF_CODE_MARKER
;
5343 sscreen
->ws
->buffer_unmap(shader
->bo
->buf
);
5347 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
5348 struct pipe_debug_callback
*debug
,
5349 const char *name
, FILE *file
)
5354 if (binary
->disasm_string
) {
5355 fprintf(file
, "Shader %s disassembly:\n", name
);
5356 fprintf(file
, "%s", binary
->disasm_string
);
5358 if (debug
&& debug
->debug_message
) {
5359 /* Very long debug messages are cut off, so send the
5360 * disassembly one line at a time. This causes more
5361 * overhead, but on the plus side it simplifies
5362 * parsing of resulting logs.
5364 pipe_debug_message(debug
, SHADER_INFO
,
5365 "Shader Disassembly Begin");
5367 line
= binary
->disasm_string
;
5369 p
= util_strchrnul(line
, '\n');
5373 pipe_debug_message(debug
, SHADER_INFO
,
5374 "%.*s", count
, line
);
5382 pipe_debug_message(debug
, SHADER_INFO
,
5383 "Shader Disassembly End");
5386 fprintf(file
, "Shader %s binary:\n", name
);
5387 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
5388 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
5389 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
5390 binary
->code
[i
+ 1], binary
->code
[i
]);
5395 static void si_calculate_max_simd_waves(struct si_shader
*shader
)
5397 struct si_screen
*sscreen
= shader
->selector
->screen
;
5398 struct si_shader_config
*conf
= &shader
->config
;
5399 unsigned num_inputs
= shader
->selector
->info
.num_inputs
;
5400 unsigned lds_increment
= sscreen
->info
.chip_class
>= CIK
? 512 : 256;
5401 unsigned lds_per_wave
= 0;
5402 unsigned max_simd_waves
;
5404 max_simd_waves
= ac_get_max_simd_waves(sscreen
->info
.family
);
5406 /* Compute LDS usage for PS. */
5407 switch (shader
->selector
->type
) {
5408 case PIPE_SHADER_FRAGMENT
:
5409 /* The minimum usage per wave is (num_inputs * 48). The maximum
5410 * usage is (num_inputs * 48 * 16).
5411 * We can get anything in between and it varies between waves.
5413 * The 48 bytes per input for a single primitive is equal to
5414 * 4 bytes/component * 4 components/input * 3 points.
5416 * Other stages don't know the size at compile time or don't
5417 * allocate LDS per wave, but instead they do it per thread group.
5419 lds_per_wave
= conf
->lds_size
* lds_increment
+
5420 align(num_inputs
* 48, lds_increment
);
5422 case PIPE_SHADER_COMPUTE
:
5423 if (shader
->selector
) {
5424 unsigned max_workgroup_size
=
5425 si_get_max_workgroup_size(shader
);
5426 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
5427 DIV_ROUND_UP(max_workgroup_size
, 64);
5432 /* Compute the per-SIMD wave counts. */
5433 if (conf
->num_sgprs
) {
5434 if (sscreen
->info
.chip_class
>= VI
)
5435 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
5437 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
5440 if (conf
->num_vgprs
)
5441 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
5443 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
5444 * 16KB makes some SIMDs unoccupied). */
5446 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
5448 conf
->max_simd_waves
= max_simd_waves
;
5451 void si_shader_dump_stats_for_shader_db(const struct si_shader
*shader
,
5452 struct pipe_debug_callback
*debug
)
5454 const struct si_shader_config
*conf
= &shader
->config
;
5456 pipe_debug_message(debug
, SHADER_INFO
,
5457 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
5458 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
5459 "Spilled VGPRs: %d PrivMem VGPRs: %d",
5460 conf
->num_sgprs
, conf
->num_vgprs
,
5461 si_get_shader_binary_size(shader
),
5462 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5463 conf
->max_simd_waves
, conf
->spilled_sgprs
,
5464 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
5467 static void si_shader_dump_stats(struct si_screen
*sscreen
,
5468 const struct si_shader
*shader
,
5471 bool check_debug_option
)
5473 const struct si_shader_config
*conf
= &shader
->config
;
5475 if (!check_debug_option
||
5476 si_can_dump_shader(sscreen
, processor
)) {
5477 if (processor
== PIPE_SHADER_FRAGMENT
) {
5478 fprintf(file
, "*** SHADER CONFIG ***\n"
5479 "SPI_PS_INPUT_ADDR = 0x%04x\n"
5480 "SPI_PS_INPUT_ENA = 0x%04x\n",
5481 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
5484 fprintf(file
, "*** SHADER STATS ***\n"
5487 "Spilled SGPRs: %d\n"
5488 "Spilled VGPRs: %d\n"
5489 "Private memory VGPRs: %d\n"
5490 "Code Size: %d bytes\n"
5492 "Scratch: %d bytes per wave\n"
5494 "********************\n\n\n",
5495 conf
->num_sgprs
, conf
->num_vgprs
,
5496 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
5497 conf
->private_mem_vgprs
,
5498 si_get_shader_binary_size(shader
),
5499 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
5500 conf
->max_simd_waves
);
5504 const char *si_get_shader_name(const struct si_shader
*shader
, unsigned processor
)
5506 switch (processor
) {
5507 case PIPE_SHADER_VERTEX
:
5508 if (shader
->key
.as_es
)
5509 return "Vertex Shader as ES";
5510 else if (shader
->key
.as_ls
)
5511 return "Vertex Shader as LS";
5513 return "Vertex Shader as VS";
5514 case PIPE_SHADER_TESS_CTRL
:
5515 return "Tessellation Control Shader";
5516 case PIPE_SHADER_TESS_EVAL
:
5517 if (shader
->key
.as_es
)
5518 return "Tessellation Evaluation Shader as ES";
5520 return "Tessellation Evaluation Shader as VS";
5521 case PIPE_SHADER_GEOMETRY
:
5522 if (shader
->is_gs_copy_shader
)
5523 return "GS Copy Shader as VS";
5525 return "Geometry Shader";
5526 case PIPE_SHADER_FRAGMENT
:
5527 return "Pixel Shader";
5528 case PIPE_SHADER_COMPUTE
:
5529 return "Compute Shader";
5531 return "Unknown Shader";
5535 void si_shader_dump(struct si_screen
*sscreen
, const struct si_shader
*shader
,
5536 struct pipe_debug_callback
*debug
, unsigned processor
,
5537 FILE *file
, bool check_debug_option
)
5539 if (!check_debug_option
||
5540 si_can_dump_shader(sscreen
, processor
))
5541 si_dump_shader_key(processor
, shader
, file
);
5543 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
5544 if (shader
->previous_stage
&&
5545 shader
->previous_stage
->binary
.llvm_ir_string
) {
5546 fprintf(file
, "\n%s - previous stage - LLVM IR:\n\n",
5547 si_get_shader_name(shader
, processor
));
5548 fprintf(file
, "%s\n", shader
->previous_stage
->binary
.llvm_ir_string
);
5551 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
5552 si_get_shader_name(shader
, processor
));
5553 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
5556 if (!check_debug_option
||
5557 (si_can_dump_shader(sscreen
, processor
) &&
5558 !(sscreen
->debug_flags
& DBG(NO_ASM
)))) {
5559 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5562 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5563 debug
, "prolog", file
);
5564 if (shader
->previous_stage
)
5565 si_shader_dump_disassembly(&shader
->previous_stage
->binary
,
5566 debug
, "previous stage", file
);
5567 if (shader
->prolog2
)
5568 si_shader_dump_disassembly(&shader
->prolog2
->binary
,
5569 debug
, "prolog2", file
);
5571 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5574 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5575 debug
, "epilog", file
);
5576 fprintf(file
, "\n");
5579 si_shader_dump_stats(sscreen
, shader
, processor
, file
,
5580 check_debug_option
);
5583 static int si_compile_llvm(struct si_screen
*sscreen
,
5584 struct ac_shader_binary
*binary
,
5585 struct si_shader_config
*conf
,
5586 struct ac_llvm_compiler
*compiler
,
5588 struct pipe_debug_callback
*debug
,
5591 bool less_optimized
)
5594 unsigned count
= p_atomic_inc_return(&sscreen
->num_compilations
);
5596 if (si_can_dump_shader(sscreen
, processor
)) {
5597 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5599 if (!(sscreen
->debug_flags
& (DBG(NO_IR
) | DBG(PREOPT_IR
)))) {
5600 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5601 ac_dump_module(mod
);
5602 fprintf(stderr
, "\n");
5606 if (sscreen
->record_llvm_ir
) {
5607 char *ir
= LLVMPrintModuleToString(mod
);
5608 binary
->llvm_ir_string
= strdup(ir
);
5609 LLVMDisposeMessage(ir
);
5612 if (!si_replace_shader(count
, binary
)) {
5613 r
= si_llvm_compile(mod
, binary
, compiler
, debug
,
5619 si_shader_binary_read_config(binary
, conf
, 0);
5621 /* Enable 64-bit and 16-bit denormals, because there is no performance
5624 * If denormals are enabled, all floating-point output modifiers are
5627 * Don't enable denormals for 32-bit floats, because:
5628 * - Floating-point output modifiers would be ignored by the hw.
5629 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5630 * have to stop using those.
5631 * - SI & CI would be very slow.
5633 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5635 FREE(binary
->config
);
5636 FREE(binary
->global_symbol_offsets
);
5637 binary
->config
= NULL
;
5638 binary
->global_symbol_offsets
= NULL
;
5640 /* Some shaders can't have rodata because their binaries can be
5643 if (binary
->rodata_size
&&
5644 (processor
== PIPE_SHADER_VERTEX
||
5645 processor
== PIPE_SHADER_TESS_CTRL
||
5646 processor
== PIPE_SHADER_TESS_EVAL
||
5647 processor
== PIPE_SHADER_FRAGMENT
)) {
5648 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5655 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
5657 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
5658 LLVMBuildRetVoid(ctx
->ac
.builder
);
5660 LLVMBuildRet(ctx
->ac
.builder
, ret
);
5663 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5665 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5666 struct ac_llvm_compiler
*compiler
,
5667 struct si_shader_selector
*gs_selector
,
5668 struct pipe_debug_callback
*debug
)
5670 struct si_shader_context ctx
;
5671 struct si_shader
*shader
;
5672 LLVMBuilderRef builder
;
5673 struct si_shader_output_values outputs
[SI_MAX_VS_OUTPUTS
];
5674 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
5678 shader
= CALLOC_STRUCT(si_shader
);
5682 /* We can leave the fence as permanently signaled because the GS copy
5683 * shader only becomes visible globally after it has been compiled. */
5684 util_queue_fence_init(&shader
->ready
);
5686 shader
->selector
= gs_selector
;
5687 shader
->is_gs_copy_shader
= true;
5689 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
5690 ctx
.shader
= shader
;
5691 ctx
.type
= PIPE_SHADER_VERTEX
;
5693 builder
= ctx
.ac
.builder
;
5695 create_function(&ctx
);
5696 preload_ring_buffers(&ctx
);
5698 LLVMValueRef voffset
=
5699 LLVMBuildMul(ctx
.ac
.builder
, ctx
.abi
.vertex_id
,
5700 LLVMConstInt(ctx
.i32
, 4, 0), "");
5702 /* Fetch the vertex stream ID.*/
5703 LLVMValueRef stream_id
;
5705 if (gs_selector
->so
.num_outputs
)
5706 stream_id
= si_unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
5708 stream_id
= ctx
.i32_0
;
5710 /* Fill in output information. */
5711 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5712 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
5713 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
5715 for (int chan
= 0; chan
< 4; chan
++) {
5716 outputs
[i
].vertex_stream
[chan
] =
5717 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
5721 LLVMBasicBlockRef end_bb
;
5722 LLVMValueRef switch_inst
;
5724 end_bb
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, ctx
.main_fn
, "end");
5725 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
5727 for (int stream
= 0; stream
< 4; stream
++) {
5728 LLVMBasicBlockRef bb
;
5731 if (!gsinfo
->num_stream_output_components
[stream
])
5734 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
5737 bb
= LLVMInsertBasicBlockInContext(ctx
.ac
.context
, end_bb
, "out");
5738 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
.i32
, stream
, 0), bb
);
5739 LLVMPositionBuilderAtEnd(builder
, bb
);
5741 /* Fetch vertex data from GSVS ring */
5743 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5744 for (unsigned chan
= 0; chan
< 4; chan
++) {
5745 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
5746 outputs
[i
].vertex_stream
[chan
] != stream
) {
5747 outputs
[i
].values
[chan
] = LLVMGetUndef(ctx
.f32
);
5751 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
5752 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
5755 outputs
[i
].values
[chan
] =
5756 ac_build_buffer_load(&ctx
.ac
,
5757 ctx
.gsvs_ring
[0], 1,
5764 /* Streamout and exports. */
5765 if (gs_selector
->so
.num_outputs
) {
5766 si_llvm_emit_streamout(&ctx
, outputs
,
5767 gsinfo
->num_outputs
,
5772 /* Vertex color clamping.
5774 * This uses a state constant loaded in a user data SGPR and
5775 * an IF statement is added that clamps all colors if the constant
5778 struct lp_build_if_state if_ctx
;
5779 LLVMValueRef v
[2], cond
= NULL
;
5780 LLVMBasicBlockRef blocks
[2];
5782 for (unsigned i
= 0; i
< gsinfo
->num_outputs
; i
++) {
5783 if (gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
5784 gsinfo
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
5787 /* We've found a color. */
5789 /* The state is in the first bit of the user SGPR. */
5790 cond
= LLVMGetParam(ctx
.main_fn
,
5791 ctx
.param_vs_state_bits
);
5792 cond
= LLVMBuildTrunc(ctx
.ac
.builder
, cond
,
5794 lp_build_if(&if_ctx
, &ctx
.gallivm
, cond
);
5795 /* Remember blocks for Phi. */
5796 blocks
[0] = if_ctx
.true_block
;
5797 blocks
[1] = if_ctx
.entry_block
;
5800 for (unsigned j
= 0; j
< 4; j
++) {
5801 /* Insert clamp into the true block. */
5802 v
[0] = ac_build_clamp(&ctx
.ac
, outputs
[i
].values
[j
]);
5803 v
[1] = outputs
[i
].values
[j
];
5805 /* Insert Phi into the endif block. */
5806 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.merge_block
);
5807 outputs
[i
].values
[j
] = ac_build_phi(&ctx
.ac
, ctx
.f32
, 2, v
, blocks
);
5808 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, if_ctx
.true_block
);
5812 lp_build_endif(&if_ctx
);
5814 si_llvm_export_vs(&ctx
, outputs
, gsinfo
->num_outputs
);
5817 LLVMBuildBr(builder
, end_bb
);
5820 LLVMPositionBuilderAtEnd(builder
, end_bb
);
5822 LLVMBuildRetVoid(ctx
.ac
.builder
);
5824 ctx
.type
= PIPE_SHADER_GEOMETRY
; /* override for shader dumping */
5825 si_llvm_optimize_module(&ctx
);
5827 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
5828 &ctx
.shader
->config
, ctx
.compiler
,
5830 debug
, PIPE_SHADER_GEOMETRY
,
5831 "GS Copy Shader", false);
5833 if (si_can_dump_shader(sscreen
, PIPE_SHADER_GEOMETRY
))
5834 fprintf(stderr
, "GS Copy Shader:\n");
5835 si_shader_dump(sscreen
, ctx
.shader
, debug
,
5836 PIPE_SHADER_GEOMETRY
, stderr
, true);
5837 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
5840 si_llvm_dispose(&ctx
);
5849 static void si_dump_shader_key_vs(const struct si_shader_key
*key
,
5850 const struct si_vs_prolog_bits
*prolog
,
5851 const char *prefix
, FILE *f
)
5853 fprintf(f
, " %s.instance_divisor_is_one = %u\n",
5854 prefix
, prolog
->instance_divisor_is_one
);
5855 fprintf(f
, " %s.instance_divisor_is_fetched = %u\n",
5856 prefix
, prolog
->instance_divisor_is_fetched
);
5857 fprintf(f
, " %s.ls_vgpr_fix = %u\n",
5858 prefix
, prolog
->ls_vgpr_fix
);
5860 fprintf(f
, " mono.vs.fix_fetch = {");
5861 for (int i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
5862 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs_fix_fetch
[i
]);
5866 static void si_dump_shader_key(unsigned processor
, const struct si_shader
*shader
,
5869 const struct si_shader_key
*key
= &shader
->key
;
5871 fprintf(f
, "SHADER KEY\n");
5873 switch (processor
) {
5874 case PIPE_SHADER_VERTEX
:
5875 si_dump_shader_key_vs(key
, &key
->part
.vs
.prolog
,
5876 "part.vs.prolog", f
);
5877 fprintf(f
, " as_es = %u\n", key
->as_es
);
5878 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
5879 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5880 key
->mono
.u
.vs_export_prim_id
);
5883 case PIPE_SHADER_TESS_CTRL
:
5884 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
) {
5885 si_dump_shader_key_vs(key
, &key
->part
.tcs
.ls_prolog
,
5886 "part.tcs.ls_prolog", f
);
5888 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
5889 fprintf(f
, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.u
.ff_tcs_inputs_to_copy
);
5892 case PIPE_SHADER_TESS_EVAL
:
5893 fprintf(f
, " as_es = %u\n", key
->as_es
);
5894 fprintf(f
, " mono.u.vs_export_prim_id = %u\n",
5895 key
->mono
.u
.vs_export_prim_id
);
5898 case PIPE_SHADER_GEOMETRY
:
5899 if (shader
->is_gs_copy_shader
)
5902 if (shader
->selector
->screen
->info
.chip_class
>= GFX9
&&
5903 key
->part
.gs
.es
->type
== PIPE_SHADER_VERTEX
) {
5904 si_dump_shader_key_vs(key
, &key
->part
.gs
.vs_prolog
,
5905 "part.gs.vs_prolog", f
);
5907 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
5910 case PIPE_SHADER_COMPUTE
:
5913 case PIPE_SHADER_FRAGMENT
:
5914 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
5915 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
5916 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
5917 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
5918 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
5919 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
5920 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
5921 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
5922 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
5923 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
5924 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
5925 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
5926 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
5927 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
5928 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
5929 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
5930 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
5937 if ((processor
== PIPE_SHADER_GEOMETRY
||
5938 processor
== PIPE_SHADER_TESS_EVAL
||
5939 processor
== PIPE_SHADER_VERTEX
) &&
5940 !key
->as_es
&& !key
->as_ls
) {
5941 fprintf(f
, " opt.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.kill_outputs
);
5942 fprintf(f
, " opt.clip_disable = %u\n", key
->opt
.clip_disable
);
5946 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5947 struct si_screen
*sscreen
,
5948 struct ac_llvm_compiler
*compiler
)
5950 struct lp_build_tgsi_context
*bld_base
;
5952 si_llvm_context_init(ctx
, sscreen
, compiler
);
5954 bld_base
= &ctx
->bld_base
;
5955 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5957 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
].emit
= build_interp_intrinsic
;
5958 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
].emit
= build_interp_intrinsic
;
5959 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
].emit
= build_interp_intrinsic
;
5961 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
5963 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
5965 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5966 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5967 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5968 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5970 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
5971 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
5972 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
5973 bld_base
->op_actions
[TGSI_OPCODE_BALLOT
].emit
= ballot_emit
;
5974 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].intr_name
= "llvm.amdgcn.readfirstlane";
5975 bld_base
->op_actions
[TGSI_OPCODE_READ_FIRST
].emit
= read_lane_emit
;
5976 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].intr_name
= "llvm.amdgcn.readlane";
5977 bld_base
->op_actions
[TGSI_OPCODE_READ_INVOC
].emit
= read_lane_emit
;
5979 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_tgsi_emit_vertex
;
5980 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_tgsi_emit_primitive
;
5981 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5984 static void si_optimize_vs_outputs(struct si_shader_context
*ctx
)
5986 struct si_shader
*shader
= ctx
->shader
;
5987 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5989 if ((ctx
->type
!= PIPE_SHADER_VERTEX
&&
5990 ctx
->type
!= PIPE_SHADER_TESS_EVAL
) ||
5991 shader
->key
.as_ls
||
5995 ac_optimize_vs_outputs(&ctx
->ac
,
5997 shader
->info
.vs_output_param_offset
,
5999 &shader
->info
.nr_param_exports
);
6002 static void si_init_exec_from_input(struct si_shader_context
*ctx
,
6003 unsigned param
, unsigned bitoffset
)
6005 LLVMValueRef args
[] = {
6006 LLVMGetParam(ctx
->main_fn
, param
),
6007 LLVMConstInt(ctx
->i32
, bitoffset
, 0),
6009 ac_build_intrinsic(&ctx
->ac
,
6010 "llvm.amdgcn.init.exec.from.input",
6011 ctx
->voidt
, args
, 2, AC_FUNC_ATTR_CONVERGENT
);
6014 static bool si_vs_needs_prolog(const struct si_shader_selector
*sel
,
6015 const struct si_vs_prolog_bits
*key
)
6017 /* VGPR initialization fixup for Vega10 and Raven is always done in the
6019 return sel
->vs_needs_prolog
|| key
->ls_vgpr_fix
;
6022 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
)
6024 struct si_shader
*shader
= ctx
->shader
;
6025 struct si_shader_selector
*sel
= shader
->selector
;
6026 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6028 // TODO clean all this up!
6029 switch (ctx
->type
) {
6030 case PIPE_SHADER_VERTEX
:
6031 ctx
->load_input
= declare_input_vs
;
6032 if (shader
->key
.as_ls
)
6033 ctx
->abi
.emit_outputs
= si_llvm_emit_ls_epilogue
;
6034 else if (shader
->key
.as_es
)
6035 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6037 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6038 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6039 ctx
->abi
.load_base_vertex
= get_base_vertex
;
6041 case PIPE_SHADER_TESS_CTRL
:
6042 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6043 ctx
->abi
.load_tess_varyings
= si_nir_load_tcs_varyings
;
6044 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6045 bld_base
->emit_store
= store_output_tcs
;
6046 ctx
->abi
.store_tcs_outputs
= si_nir_store_output_tcs
;
6047 ctx
->abi
.emit_outputs
= si_llvm_emit_tcs_epilogue
;
6048 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6049 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6051 case PIPE_SHADER_TESS_EVAL
:
6052 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6053 ctx
->abi
.load_tess_varyings
= si_nir_load_input_tes
;
6054 ctx
->abi
.load_tess_coord
= si_load_tess_coord
;
6055 ctx
->abi
.load_tess_level
= si_load_tess_level
;
6056 ctx
->abi
.load_patch_vertices_in
= si_load_patch_vertices_in
;
6057 if (shader
->key
.as_es
)
6058 ctx
->abi
.emit_outputs
= si_llvm_emit_es_epilogue
;
6060 ctx
->abi
.emit_outputs
= si_llvm_emit_vs_epilogue
;
6061 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6063 case PIPE_SHADER_GEOMETRY
:
6064 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6065 ctx
->abi
.load_inputs
= si_nir_load_input_gs
;
6066 ctx
->abi
.emit_vertex
= si_llvm_emit_vertex
;
6067 ctx
->abi
.emit_primitive
= si_llvm_emit_primitive
;
6068 ctx
->abi
.emit_outputs
= si_llvm_emit_gs_epilogue
;
6069 bld_base
->emit_epilogue
= si_tgsi_emit_gs_epilogue
;
6071 case PIPE_SHADER_FRAGMENT
:
6072 ctx
->load_input
= declare_input_fs
;
6073 ctx
->abi
.emit_outputs
= si_llvm_return_fs_outputs
;
6074 bld_base
->emit_epilogue
= si_tgsi_emit_epilogue
;
6075 ctx
->abi
.lookup_interp_param
= si_nir_lookup_interp_param
;
6076 ctx
->abi
.load_sample_position
= load_sample_position
;
6077 ctx
->abi
.load_sample_mask_in
= load_sample_mask_in
;
6078 ctx
->abi
.emit_kill
= si_llvm_emit_kill
;
6080 case PIPE_SHADER_COMPUTE
:
6081 ctx
->abi
.load_local_group_size
= get_block_size
;
6084 assert(!"Unsupported shader type");
6088 ctx
->abi
.load_ubo
= load_ubo
;
6089 ctx
->abi
.load_ssbo
= load_ssbo
;
6091 create_function(ctx
);
6092 preload_ring_buffers(ctx
);
6094 /* For GFX9 merged shaders:
6095 * - Set EXEC for the first shader. If the prolog is present, set
6096 * EXEC there instead.
6097 * - Add a barrier before the second shader.
6098 * - In the second shader, reset EXEC to ~0 and wrap the main part in
6099 * an if-statement. This is required for correctness in geometry
6100 * shaders, to ensure that empty GS waves do not send GS_EMIT and
6103 * For monolithic merged shaders, the first shader is wrapped in an
6104 * if-block together with its prolog in si_build_wrapper_function.
6106 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6107 if (!shader
->is_monolithic
&&
6108 sel
->info
.num_instructions
> 1 && /* not empty shader */
6109 (shader
->key
.as_es
|| shader
->key
.as_ls
) &&
6110 (ctx
->type
== PIPE_SHADER_TESS_EVAL
||
6111 (ctx
->type
== PIPE_SHADER_VERTEX
&&
6112 !si_vs_needs_prolog(sel
, &shader
->key
.part
.vs
.prolog
)))) {
6113 si_init_exec_from_input(ctx
,
6114 ctx
->param_merged_wave_info
, 0);
6115 } else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
||
6116 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6117 if (!shader
->is_monolithic
)
6118 ac_init_exec_full_mask(&ctx
->ac
);
6120 LLVMValueRef num_threads
= si_unpack_param(ctx
, ctx
->param_merged_wave_info
, 8, 8);
6122 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
6123 ac_get_thread_id(&ctx
->ac
), num_threads
, "");
6124 lp_build_if(&ctx
->merged_wrap_if_state
, &ctx
->gallivm
, ena
);
6126 /* The barrier must execute for all shaders in a
6129 * Execute the barrier inside the conditional block,
6130 * so that empty waves can jump directly to s_endpgm,
6131 * which will also signal the barrier.
6133 * If the shader is TCS and the TCS epilog is present
6134 * and contains a barrier, it will wait there and then
6137 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
6141 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
&&
6142 sel
->tcs_info
.tessfactors_are_def_in_all_invocs
) {
6143 for (unsigned i
= 0; i
< 6; i
++) {
6144 ctx
->invoc0_tess_factors
[i
] =
6145 ac_build_alloca_undef(&ctx
->ac
, ctx
->i32
, "");
6149 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6151 for (i
= 0; i
< 4; i
++) {
6152 ctx
->gs_next_vertex
[i
] =
6153 ac_build_alloca(&ctx
->ac
, ctx
->i32
, "");
6157 if (sel
->force_correct_derivs_after_kill
) {
6158 ctx
->postponed_kill
= ac_build_alloca_undef(&ctx
->ac
, ctx
->i1
, "");
6159 /* true = don't kill. */
6160 LLVMBuildStore(ctx
->ac
.builder
, ctx
->i1true
,
6161 ctx
->postponed_kill
);
6165 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6166 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6170 if (!si_nir_build_llvm(ctx
, sel
->nir
)) {
6171 fprintf(stderr
, "Failed to translate shader from NIR to LLVM\n");
6176 si_llvm_build_ret(ctx
, ctx
->return_value
);
6181 * Compute the VS prolog key, which contains all the information needed to
6182 * build the VS prolog function, and set shader->info bits where needed.
6184 * \param info Shader info of the vertex shader.
6185 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
6186 * \param prolog_key Key of the VS prolog
6187 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
6188 * \param key Output shader part key.
6190 static void si_get_vs_prolog_key(const struct tgsi_shader_info
*info
,
6191 unsigned num_input_sgprs
,
6192 const struct si_vs_prolog_bits
*prolog_key
,
6193 struct si_shader
*shader_out
,
6194 union si_shader_part_key
*key
)
6196 memset(key
, 0, sizeof(*key
));
6197 key
->vs_prolog
.states
= *prolog_key
;
6198 key
->vs_prolog
.num_input_sgprs
= num_input_sgprs
;
6199 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6200 key
->vs_prolog
.as_ls
= shader_out
->key
.as_ls
;
6201 key
->vs_prolog
.as_es
= shader_out
->key
.as_es
;
6203 if (shader_out
->selector
->type
== PIPE_SHADER_TESS_CTRL
) {
6204 key
->vs_prolog
.as_ls
= 1;
6205 key
->vs_prolog
.num_merged_next_stage_vgprs
= 2;
6206 } else if (shader_out
->selector
->type
== PIPE_SHADER_GEOMETRY
) {
6207 key
->vs_prolog
.as_es
= 1;
6208 key
->vs_prolog
.num_merged_next_stage_vgprs
= 5;
6211 /* Enable loading the InstanceID VGPR. */
6212 uint16_t input_mask
= u_bit_consecutive(0, info
->num_inputs
);
6214 if ((key
->vs_prolog
.states
.instance_divisor_is_one
|
6215 key
->vs_prolog
.states
.instance_divisor_is_fetched
) & input_mask
)
6216 shader_out
->info
.uses_instanceid
= true;
6220 * Compute the PS prolog key, which contains all the information needed to
6221 * build the PS prolog function, and set related bits in shader->config.
6223 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6224 union si_shader_part_key
*key
,
6225 bool separate_prolog
)
6227 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6229 memset(key
, 0, sizeof(*key
));
6230 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6231 key
->ps_prolog
.colors_read
= info
->colors_read
;
6232 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6233 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6234 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6235 (key
->ps_prolog
.colors_read
||
6236 key
->ps_prolog
.states
.force_persp_sample_interp
||
6237 key
->ps_prolog
.states
.force_linear_sample_interp
||
6238 key
->ps_prolog
.states
.force_persp_center_interp
||
6239 key
->ps_prolog
.states
.force_linear_center_interp
||
6240 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6241 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6242 key
->ps_prolog
.ancillary_vgpr_index
= shader
->info
.ancillary_vgpr_index
;
6244 if (info
->colors_read
) {
6245 unsigned *color
= shader
->selector
->color_attr_index
;
6247 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6248 /* BCOLORs are stored after the last input. */
6249 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6250 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6251 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6254 for (unsigned i
= 0; i
< 2; i
++) {
6255 unsigned interp
= info
->input_interpolate
[color
[i
]];
6256 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6258 if (!(info
->colors_read
& (0xf << i
*4)))
6261 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6263 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6264 interp
== TGSI_INTERPOLATE_COLOR
)
6265 interp
= TGSI_INTERPOLATE_CONSTANT
;
6268 case TGSI_INTERPOLATE_CONSTANT
:
6269 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6271 case TGSI_INTERPOLATE_PERSPECTIVE
:
6272 case TGSI_INTERPOLATE_COLOR
:
6273 /* Force the interpolation location for colors here. */
6274 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6275 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6276 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6277 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6280 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6281 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6282 shader
->config
.spi_ps_input_ena
|=
6283 S_0286CC_PERSP_SAMPLE_ENA(1);
6285 case TGSI_INTERPOLATE_LOC_CENTER
:
6286 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6287 shader
->config
.spi_ps_input_ena
|=
6288 S_0286CC_PERSP_CENTER_ENA(1);
6290 case TGSI_INTERPOLATE_LOC_CENTROID
:
6291 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6292 shader
->config
.spi_ps_input_ena
|=
6293 S_0286CC_PERSP_CENTROID_ENA(1);
6299 case TGSI_INTERPOLATE_LINEAR
:
6300 /* Force the interpolation location for colors here. */
6301 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
6302 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6303 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
6304 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6306 /* The VGPR assignment for non-monolithic shaders
6307 * works because InitialPSInputAddr is set on the
6308 * main shader and PERSP_PULL_MODEL is never used.
6311 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6312 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6313 separate_prolog
? 6 : 9;
6314 shader
->config
.spi_ps_input_ena
|=
6315 S_0286CC_LINEAR_SAMPLE_ENA(1);
6317 case TGSI_INTERPOLATE_LOC_CENTER
:
6318 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6319 separate_prolog
? 8 : 11;
6320 shader
->config
.spi_ps_input_ena
|=
6321 S_0286CC_LINEAR_CENTER_ENA(1);
6323 case TGSI_INTERPOLATE_LOC_CENTROID
:
6324 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6325 separate_prolog
? 10 : 13;
6326 shader
->config
.spi_ps_input_ena
|=
6327 S_0286CC_LINEAR_CENTROID_ENA(1);
6341 * Check whether a PS prolog is required based on the key.
6343 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
6345 return key
->ps_prolog
.colors_read
||
6346 key
->ps_prolog
.states
.force_persp_sample_interp
||
6347 key
->ps_prolog
.states
.force_linear_sample_interp
||
6348 key
->ps_prolog
.states
.force_persp_center_interp
||
6349 key
->ps_prolog
.states
.force_linear_center_interp
||
6350 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6351 key
->ps_prolog
.states
.bc_optimize_for_linear
||
6352 key
->ps_prolog
.states
.poly_stipple
||
6353 key
->ps_prolog
.states
.samplemask_log_ps_iter
;
6357 * Compute the PS epilog key, which contains all the information needed to
6358 * build the PS epilog function.
6360 static void si_get_ps_epilog_key(struct si_shader
*shader
,
6361 union si_shader_part_key
*key
)
6363 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6364 memset(key
, 0, sizeof(*key
));
6365 key
->ps_epilog
.colors_written
= info
->colors_written
;
6366 key
->ps_epilog
.writes_z
= info
->writes_z
;
6367 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
6368 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6369 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
6373 * Build the GS prolog function. Rotate the input vertices for triangle strips
6376 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
6377 union si_shader_part_key
*key
)
6379 unsigned num_sgprs
, num_vgprs
;
6380 struct si_function_info fninfo
;
6381 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6382 LLVMTypeRef returns
[48];
6383 LLVMValueRef func
, ret
;
6385 si_init_function_info(&fninfo
);
6387 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6388 if (key
->gs_prolog
.states
.gfx9_prev_is_vs
)
6389 num_sgprs
= 8 + GFX9_VSGS_NUM_USER_SGPR
;
6391 num_sgprs
= 8 + GFX9_TESGS_NUM_USER_SGPR
;
6392 num_vgprs
= 5; /* ES inputs are not needed by GS */
6394 num_sgprs
= GFX6_GS_NUM_USER_SGPR
+ 2;
6398 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
6399 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
6400 returns
[i
] = ctx
->i32
;
6403 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
6404 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
);
6405 returns
[num_sgprs
+ i
] = ctx
->f32
;
6408 /* Create the function. */
6409 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
6411 func
= ctx
->main_fn
;
6413 /* Set the full EXEC mask for the prolog, because we are only fiddling
6414 * with registers here. The main shader part will set the correct EXEC
6417 if (ctx
->screen
->info
.chip_class
>= GFX9
&& !key
->gs_prolog
.is_monolithic
)
6418 ac_init_exec_full_mask(&ctx
->ac
);
6420 /* Copy inputs to outputs. This should be no-op, as the registers match,
6421 * but it will prevent the compiler from overwriting them unintentionally.
6423 ret
= ctx
->return_value
;
6424 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
6425 LLVMValueRef p
= LLVMGetParam(func
, i
);
6426 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
6428 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
6429 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
6430 p
= ac_to_float(&ctx
->ac
, p
);
6431 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
6434 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
6435 /* Remap the input vertices for every other primitive. */
6436 const unsigned gfx6_vtx_params
[6] = {
6444 const unsigned gfx9_vtx_params
[3] = {
6449 LLVMValueRef vtx_in
[6], vtx_out
[6];
6450 LLVMValueRef prim_id
, rotate
;
6452 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6453 for (unsigned i
= 0; i
< 3; i
++) {
6454 vtx_in
[i
*2] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 0, 16);
6455 vtx_in
[i
*2+1] = si_unpack_param(ctx
, gfx9_vtx_params
[i
], 16, 16);
6458 for (unsigned i
= 0; i
< 6; i
++)
6459 vtx_in
[i
] = LLVMGetParam(func
, gfx6_vtx_params
[i
]);
6462 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
6463 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
6465 for (unsigned i
= 0; i
< 6; ++i
) {
6466 LLVMValueRef base
, rotated
;
6468 rotated
= vtx_in
[(i
+ 4) % 6];
6469 vtx_out
[i
] = LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
6472 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
6473 for (unsigned i
= 0; i
< 3; i
++) {
6474 LLVMValueRef hi
, out
;
6476 hi
= LLVMBuildShl(builder
, vtx_out
[i
*2+1],
6477 LLVMConstInt(ctx
->i32
, 16, 0), "");
6478 out
= LLVMBuildOr(builder
, vtx_out
[i
*2], hi
, "");
6479 out
= ac_to_float(&ctx
->ac
, out
);
6480 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6481 gfx9_vtx_params
[i
], "");
6484 for (unsigned i
= 0; i
< 6; i
++) {
6487 out
= ac_to_float(&ctx
->ac
, vtx_out
[i
]);
6488 ret
= LLVMBuildInsertValue(builder
, ret
, out
,
6489 gfx6_vtx_params
[i
], "");
6494 LLVMBuildRet(builder
, ret
);
6498 * Given a list of shader part functions, build a wrapper function that
6499 * runs them in sequence to form a monolithic shader.
6501 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
6502 LLVMValueRef
*parts
,
6505 unsigned next_shader_first_part
)
6507 LLVMBuilderRef builder
= ctx
->ac
.builder
;
6508 /* PS epilog has one arg per color component; gfx9 merged shader
6509 * prologs need to forward 32 user SGPRs.
6511 struct si_function_info fninfo
;
6512 LLVMValueRef initial
[64], out
[64];
6513 LLVMTypeRef function_type
;
6514 unsigned num_first_params
;
6515 unsigned num_out
, initial_num_out
;
6516 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
6517 MAYBE_UNUSED
unsigned initial_num_out_sgpr
; /* used in debug checks */
6518 unsigned num_sgprs
, num_vgprs
;
6520 struct lp_build_if_state if_state
;
6522 si_init_function_info(&fninfo
);
6524 for (unsigned i
= 0; i
< num_parts
; ++i
) {
6525 ac_add_function_attr(ctx
->ac
.context
, parts
[i
], -1,
6526 AC_FUNC_ATTR_ALWAYSINLINE
);
6527 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
6530 /* The parameters of the wrapper function correspond to those of the
6531 * first part in terms of SGPRs and VGPRs, but we use the types of the
6532 * main part to get the right types. This is relevant for the
6533 * dereferenceable attribute on descriptor table pointers.
6538 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
6539 num_first_params
= LLVMCountParamTypes(function_type
);
6541 for (unsigned i
= 0; i
< num_first_params
; ++i
) {
6542 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
6544 if (ac_is_sgpr_param(param
)) {
6545 assert(num_vgprs
== 0);
6546 num_sgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6548 num_vgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
6553 while (gprs
< num_sgprs
+ num_vgprs
) {
6554 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], fninfo
.num_params
);
6555 LLVMTypeRef type
= LLVMTypeOf(param
);
6556 unsigned size
= ac_get_type_size(type
) / 4;
6558 add_arg(&fninfo
, gprs
< num_sgprs
? ARG_SGPR
: ARG_VGPR
, type
);
6560 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
6561 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
6562 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
6567 si_create_function(ctx
, "wrapper", NULL
, 0, &fninfo
,
6568 si_get_max_workgroup_size(ctx
->shader
));
6570 if (is_merged_shader(ctx
))
6571 ac_init_exec_full_mask(&ctx
->ac
);
6573 /* Record the arguments of the function as if they were an output of
6579 for (unsigned i
= 0; i
< fninfo
.num_params
; ++i
) {
6580 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
6581 LLVMTypeRef param_type
= LLVMTypeOf(param
);
6582 LLVMTypeRef out_type
= i
< fninfo
.num_sgpr_params
? ctx
->i32
: ctx
->f32
;
6583 unsigned size
= ac_get_type_size(param_type
) / 4;
6586 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6587 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i32
, "");
6588 param_type
= ctx
->i32
;
6591 if (param_type
!= out_type
)
6592 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
6593 out
[num_out
++] = param
;
6595 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
6597 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6598 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
6599 param_type
= ctx
->i64
;
6602 if (param_type
!= vector_type
)
6603 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
6605 for (unsigned j
= 0; j
< size
; ++j
)
6606 out
[num_out
++] = LLVMBuildExtractElement(
6607 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
6610 if (i
< fninfo
.num_sgpr_params
)
6611 num_out_sgpr
= num_out
;
6614 memcpy(initial
, out
, sizeof(out
));
6615 initial_num_out
= num_out
;
6616 initial_num_out_sgpr
= num_out_sgpr
;
6618 /* Now chain the parts. */
6619 for (unsigned part
= 0; part
< num_parts
; ++part
) {
6620 LLVMValueRef in
[48];
6622 LLVMTypeRef ret_type
;
6623 unsigned out_idx
= 0;
6624 unsigned num_params
= LLVMCountParams(parts
[part
]);
6626 /* Merged shaders are executed conditionally depending
6627 * on the number of enabled threads passed in the input SGPRs. */
6628 if (is_merged_shader(ctx
) && part
== 0) {
6629 LLVMValueRef ena
, count
= initial
[3];
6631 count
= LLVMBuildAnd(builder
, count
,
6632 LLVMConstInt(ctx
->i32
, 0x7f, 0), "");
6633 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
6634 ac_get_thread_id(&ctx
->ac
), count
, "");
6635 lp_build_if(&if_state
, &ctx
->gallivm
, ena
);
6638 /* Derive arguments for the next part from outputs of the
6641 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
6643 LLVMTypeRef param_type
;
6645 unsigned param_size
;
6646 LLVMValueRef arg
= NULL
;
6648 param
= LLVMGetParam(parts
[part
], param_idx
);
6649 param_type
= LLVMTypeOf(param
);
6650 param_size
= ac_get_type_size(param_type
) / 4;
6651 is_sgpr
= ac_is_sgpr_param(param
);
6654 ac_add_function_attr(ctx
->ac
.context
, parts
[part
],
6655 param_idx
+ 1, AC_FUNC_ATTR_INREG
);
6656 } else if (out_idx
< num_out_sgpr
) {
6657 /* Skip returned SGPRs the current part doesn't
6658 * declare on the input. */
6659 out_idx
= num_out_sgpr
;
6662 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
6664 if (param_size
== 1)
6667 arg
= ac_build_gather_values(&ctx
->ac
, &out
[out_idx
], param_size
);
6669 if (LLVMTypeOf(arg
) != param_type
) {
6670 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
6671 if (LLVMGetPointerAddressSpace(param_type
) ==
6672 AC_CONST_32BIT_ADDR_SPACE
) {
6673 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
6674 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6676 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
6677 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
6680 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
6684 in
[param_idx
] = arg
;
6685 out_idx
+= param_size
;
6688 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
6690 if (is_merged_shader(ctx
) &&
6691 part
+ 1 == next_shader_first_part
) {
6692 lp_build_endif(&if_state
);
6694 /* The second half of the merged shader should use
6695 * the inputs from the toplevel (wrapper) function,
6696 * not the return value from the last call.
6698 * That's because the last call was executed condi-
6699 * tionally, so we can't consume it in the main
6702 memcpy(out
, initial
, sizeof(initial
));
6703 num_out
= initial_num_out
;
6704 num_out_sgpr
= initial_num_out_sgpr
;
6708 /* Extract the returned GPRs. */
6709 ret_type
= LLVMTypeOf(ret
);
6713 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
6714 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
6716 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
6718 for (unsigned i
= 0; i
< ret_size
; ++i
) {
6720 LLVMBuildExtractValue(builder
, ret
, i
, "");
6722 assert(num_out
< ARRAY_SIZE(out
));
6723 out
[num_out
++] = val
;
6725 if (LLVMTypeOf(val
) == ctx
->i32
) {
6726 assert(num_out_sgpr
+ 1 == num_out
);
6727 num_out_sgpr
= num_out
;
6733 LLVMBuildRetVoid(builder
);
6736 static bool si_should_optimize_less(struct ac_llvm_compiler
*compiler
,
6737 struct si_shader_selector
*sel
)
6739 if (!compiler
->low_opt_passes
)
6742 /* Assume a slow CPU. */
6743 assert(!sel
->screen
->info
.has_dedicated_vram
&&
6744 sel
->screen
->info
.chip_class
<= VI
);
6746 /* For a crazy dEQP test containing 2597 memory opcodes, mostly
6748 return sel
->type
== PIPE_SHADER_COMPUTE
&&
6749 sel
->info
.num_memory_instructions
> 1000;
6752 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
6753 struct ac_llvm_compiler
*compiler
,
6754 struct si_shader
*shader
,
6755 struct pipe_debug_callback
*debug
)
6757 struct si_shader_selector
*sel
= shader
->selector
;
6758 struct si_shader_context ctx
;
6761 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6762 * conversion fails. */
6763 if (si_can_dump_shader(sscreen
, sel
->info
.processor
) &&
6764 !(sscreen
->debug_flags
& DBG(NO_TGSI
))) {
6766 tgsi_dump(sel
->tokens
, 0);
6768 nir_print_shader(sel
->nir
, stderr
);
6769 si_dump_streamout(&sel
->so
);
6772 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
6773 si_llvm_context_set_tgsi(&ctx
, shader
);
6775 memset(shader
->info
.vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
6776 sizeof(shader
->info
.vs_output_param_offset
));
6778 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
6780 if (!si_compile_tgsi_main(&ctx
)) {
6781 si_llvm_dispose(&ctx
);
6785 if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
6786 LLVMValueRef parts
[2];
6787 bool need_prolog
= sel
->vs_needs_prolog
;
6789 parts
[1] = ctx
.main_fn
;
6792 union si_shader_part_key prolog_key
;
6793 si_get_vs_prolog_key(&sel
->info
,
6794 shader
->info
.num_input_sgprs
,
6795 &shader
->key
.part
.vs
.prolog
,
6796 shader
, &prolog_key
);
6797 si_build_vs_prolog_function(&ctx
, &prolog_key
);
6798 parts
[0] = ctx
.main_fn
;
6801 si_build_wrapper_function(&ctx
, parts
+ !need_prolog
,
6802 1 + need_prolog
, need_prolog
, 0);
6803 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
6804 if (sscreen
->info
.chip_class
>= GFX9
) {
6805 struct si_shader_selector
*ls
= shader
->key
.part
.tcs
.ls
;
6806 LLVMValueRef parts
[4];
6807 bool vs_needs_prolog
=
6808 si_vs_needs_prolog(ls
, &shader
->key
.part
.tcs
.ls_prolog
);
6811 parts
[2] = ctx
.main_fn
;
6814 union si_shader_part_key tcs_epilog_key
;
6815 memset(&tcs_epilog_key
, 0, sizeof(tcs_epilog_key
));
6816 tcs_epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6817 si_build_tcs_epilog_function(&ctx
, &tcs_epilog_key
);
6818 parts
[3] = ctx
.main_fn
;
6820 /* VS as LS main part */
6821 struct si_shader shader_ls
= {};
6822 shader_ls
.selector
= ls
;
6823 shader_ls
.key
.as_ls
= 1;
6824 shader_ls
.key
.mono
= shader
->key
.mono
;
6825 shader_ls
.key
.opt
= shader
->key
.opt
;
6826 shader_ls
.is_monolithic
= true;
6827 si_llvm_context_set_tgsi(&ctx
, &shader_ls
);
6829 if (!si_compile_tgsi_main(&ctx
)) {
6830 si_llvm_dispose(&ctx
);
6833 shader
->info
.uses_instanceid
|= ls
->info
.uses_instanceid
;
6834 parts
[1] = ctx
.main_fn
;
6837 if (vs_needs_prolog
) {
6838 union si_shader_part_key vs_prolog_key
;
6839 si_get_vs_prolog_key(&ls
->info
,
6840 shader_ls
.info
.num_input_sgprs
,
6841 &shader
->key
.part
.tcs
.ls_prolog
,
6842 shader
, &vs_prolog_key
);
6843 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6844 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6845 parts
[0] = ctx
.main_fn
;
6848 /* Reset the shader context. */
6849 ctx
.shader
= shader
;
6850 ctx
.type
= PIPE_SHADER_TESS_CTRL
;
6852 si_build_wrapper_function(&ctx
,
6853 parts
+ !vs_needs_prolog
,
6854 4 - !vs_needs_prolog
, vs_needs_prolog
,
6855 vs_needs_prolog
? 2 : 1);
6857 LLVMValueRef parts
[2];
6858 union si_shader_part_key epilog_key
;
6860 parts
[0] = ctx
.main_fn
;
6862 memset(&epilog_key
, 0, sizeof(epilog_key
));
6863 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
6864 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
6865 parts
[1] = ctx
.main_fn
;
6867 si_build_wrapper_function(&ctx
, parts
, 2, 0, 0);
6869 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
6870 if (ctx
.screen
->info
.chip_class
>= GFX9
) {
6871 struct si_shader_selector
*es
= shader
->key
.part
.gs
.es
;
6872 LLVMValueRef es_prolog
= NULL
;
6873 LLVMValueRef es_main
= NULL
;
6874 LLVMValueRef gs_prolog
= NULL
;
6875 LLVMValueRef gs_main
= ctx
.main_fn
;
6878 union si_shader_part_key gs_prolog_key
;
6879 memset(&gs_prolog_key
, 0, sizeof(gs_prolog_key
));
6880 gs_prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6881 gs_prolog_key
.gs_prolog
.is_monolithic
= true;
6882 si_build_gs_prolog_function(&ctx
, &gs_prolog_key
);
6883 gs_prolog
= ctx
.main_fn
;
6886 struct si_shader shader_es
= {};
6887 shader_es
.selector
= es
;
6888 shader_es
.key
.as_es
= 1;
6889 shader_es
.key
.mono
= shader
->key
.mono
;
6890 shader_es
.key
.opt
= shader
->key
.opt
;
6891 shader_es
.is_monolithic
= true;
6892 si_llvm_context_set_tgsi(&ctx
, &shader_es
);
6894 if (!si_compile_tgsi_main(&ctx
)) {
6895 si_llvm_dispose(&ctx
);
6898 shader
->info
.uses_instanceid
|= es
->info
.uses_instanceid
;
6899 es_main
= ctx
.main_fn
;
6902 if (es
->vs_needs_prolog
) {
6903 union si_shader_part_key vs_prolog_key
;
6904 si_get_vs_prolog_key(&es
->info
,
6905 shader_es
.info
.num_input_sgprs
,
6906 &shader
->key
.part
.gs
.vs_prolog
,
6907 shader
, &vs_prolog_key
);
6908 vs_prolog_key
.vs_prolog
.is_monolithic
= true;
6909 si_build_vs_prolog_function(&ctx
, &vs_prolog_key
);
6910 es_prolog
= ctx
.main_fn
;
6913 /* Reset the shader context. */
6914 ctx
.shader
= shader
;
6915 ctx
.type
= PIPE_SHADER_GEOMETRY
;
6917 /* Prepare the array of shader parts. */
6918 LLVMValueRef parts
[4];
6919 unsigned num_parts
= 0, main_part
, next_first_part
;
6922 parts
[num_parts
++] = es_prolog
;
6924 parts
[main_part
= num_parts
++] = es_main
;
6925 parts
[next_first_part
= num_parts
++] = gs_prolog
;
6926 parts
[num_parts
++] = gs_main
;
6928 si_build_wrapper_function(&ctx
, parts
, num_parts
,
6929 main_part
, next_first_part
);
6931 LLVMValueRef parts
[2];
6932 union si_shader_part_key prolog_key
;
6934 parts
[1] = ctx
.main_fn
;
6936 memset(&prolog_key
, 0, sizeof(prolog_key
));
6937 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
6938 si_build_gs_prolog_function(&ctx
, &prolog_key
);
6939 parts
[0] = ctx
.main_fn
;
6941 si_build_wrapper_function(&ctx
, parts
, 2, 1, 0);
6943 } else if (shader
->is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
6944 LLVMValueRef parts
[3];
6945 union si_shader_part_key prolog_key
;
6946 union si_shader_part_key epilog_key
;
6949 si_get_ps_prolog_key(shader
, &prolog_key
, false);
6950 need_prolog
= si_need_ps_prolog(&prolog_key
);
6952 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
6955 si_build_ps_prolog_function(&ctx
, &prolog_key
);
6956 parts
[0] = ctx
.main_fn
;
6959 si_get_ps_epilog_key(shader
, &epilog_key
);
6960 si_build_ps_epilog_function(&ctx
, &epilog_key
);
6961 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
6963 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2,
6964 need_prolog
? 1 : 0, 0);
6967 si_llvm_optimize_module(&ctx
);
6969 /* Post-optimization transformations and analysis. */
6970 si_optimize_vs_outputs(&ctx
);
6972 if ((debug
&& debug
->debug_message
) ||
6973 si_can_dump_shader(sscreen
, ctx
.type
)) {
6974 ctx
.shader
->config
.private_mem_vgprs
=
6975 ac_count_scratch_private_memory(ctx
.main_fn
);
6978 /* Make sure the input is a pointer and not integer followed by inttoptr. */
6979 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx
.main_fn
, 0))) ==
6980 LLVMPointerTypeKind
);
6982 /* Compile to bytecode. */
6983 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, compiler
,
6984 ctx
.ac
.module
, debug
, ctx
.type
,
6985 si_get_shader_name(shader
, ctx
.type
),
6986 si_should_optimize_less(compiler
, shader
->selector
));
6987 si_llvm_dispose(&ctx
);
6989 fprintf(stderr
, "LLVM failed to compile shader\n");
6993 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
6994 * LLVM 3.9svn has this bug.
6996 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
6997 unsigned wave_size
= 64;
6998 unsigned max_vgprs
= 256;
6999 unsigned max_sgprs
= sscreen
->info
.chip_class
>= VI
? 800 : 512;
7000 unsigned max_sgprs_per_wave
= 128;
7001 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
7002 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7003 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7005 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7006 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7008 if (shader
->config
.num_sgprs
> max_sgprs
||
7009 shader
->config
.num_vgprs
> max_vgprs
) {
7010 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7011 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7012 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7013 max_sgprs
, max_vgprs
);
7015 /* Just terminate the process, because dependent
7016 * shaders can hang due to bad input data, but use
7017 * the env var to allow shader-db to work.
7019 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7024 /* Add the scratch offset to input SGPRs. */
7025 if (shader
->config
.scratch_bytes_per_wave
&& !is_merged_shader(&ctx
))
7026 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7028 /* Calculate the number of fragment input VGPRs. */
7029 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7030 shader
->info
.num_input_vgprs
= 0;
7031 shader
->info
.face_vgpr_index
= -1;
7032 shader
->info
.ancillary_vgpr_index
= -1;
7034 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7035 shader
->info
.num_input_vgprs
+= 2;
7036 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7037 shader
->info
.num_input_vgprs
+= 2;
7038 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7039 shader
->info
.num_input_vgprs
+= 2;
7040 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7041 shader
->info
.num_input_vgprs
+= 3;
7042 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7043 shader
->info
.num_input_vgprs
+= 2;
7044 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7045 shader
->info
.num_input_vgprs
+= 2;
7046 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7047 shader
->info
.num_input_vgprs
+= 2;
7048 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7049 shader
->info
.num_input_vgprs
+= 1;
7050 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7051 shader
->info
.num_input_vgprs
+= 1;
7052 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7053 shader
->info
.num_input_vgprs
+= 1;
7054 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7055 shader
->info
.num_input_vgprs
+= 1;
7056 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7057 shader
->info
.num_input_vgprs
+= 1;
7058 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7059 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7060 shader
->info
.num_input_vgprs
+= 1;
7062 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
)) {
7063 shader
->info
.ancillary_vgpr_index
= shader
->info
.num_input_vgprs
;
7064 shader
->info
.num_input_vgprs
+= 1;
7066 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7067 shader
->info
.num_input_vgprs
+= 1;
7068 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7069 shader
->info
.num_input_vgprs
+= 1;
7072 si_calculate_max_simd_waves(shader
);
7073 si_shader_dump_stats_for_shader_db(shader
, debug
);
7078 * Create, compile and return a shader part (prolog or epilog).
7080 * \param sscreen screen
7081 * \param list list of shader parts of the same category
7082 * \param type shader type
7083 * \param key shader part key
7084 * \param prolog whether the part being requested is a prolog
7085 * \param tm LLVM target machine
7086 * \param debug debug callback
7087 * \param build the callback responsible for building the main function
7088 * \return non-NULL on success
7090 static struct si_shader_part
*
7091 si_get_shader_part(struct si_screen
*sscreen
,
7092 struct si_shader_part
**list
,
7093 enum pipe_shader_type type
,
7095 union si_shader_part_key
*key
,
7096 struct ac_llvm_compiler
*compiler
,
7097 struct pipe_debug_callback
*debug
,
7098 void (*build
)(struct si_shader_context
*,
7099 union si_shader_part_key
*),
7102 struct si_shader_part
*result
;
7104 mtx_lock(&sscreen
->shader_parts_mutex
);
7106 /* Find existing. */
7107 for (result
= *list
; result
; result
= result
->next
) {
7108 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7109 mtx_unlock(&sscreen
->shader_parts_mutex
);
7114 /* Compile a new one. */
7115 result
= CALLOC_STRUCT(si_shader_part
);
7118 struct si_shader shader
= {};
7119 struct si_shader_context ctx
;
7121 si_init_shader_ctx(&ctx
, sscreen
, compiler
);
7122 ctx
.shader
= &shader
;
7126 case PIPE_SHADER_VERTEX
:
7127 shader
.key
.as_ls
= key
->vs_prolog
.as_ls
;
7128 shader
.key
.as_es
= key
->vs_prolog
.as_es
;
7130 case PIPE_SHADER_TESS_CTRL
:
7132 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7134 case PIPE_SHADER_GEOMETRY
:
7137 case PIPE_SHADER_FRAGMENT
:
7139 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7141 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7144 unreachable("bad shader part");
7150 si_llvm_optimize_module(&ctx
);
7152 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, compiler
,
7153 ctx
.ac
.module
, debug
, ctx
.type
, name
, false)) {
7159 result
->next
= *list
;
7163 si_llvm_dispose(&ctx
);
7164 mtx_unlock(&sscreen
->shader_parts_mutex
);
7168 static LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
7170 LLVMValueRef ptr
[2], list
;
7171 bool merged_shader
= is_merged_shader(ctx
);
7173 if (HAVE_32BIT_POINTERS
) {
7174 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7175 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
[0],
7176 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
7180 /* Get the pointer to rw buffers. */
7181 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
7182 ptr
[1] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
+ 1);
7183 list
= ac_build_gather_values(&ctx
->ac
, ptr
, 2);
7184 list
= LLVMBuildBitCast(ctx
->ac
.builder
, list
, ctx
->i64
, "");
7185 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, list
,
7186 ac_array_in_const_addr_space(ctx
->v4i32
), "");
7191 * Build the vertex shader prolog function.
7193 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7194 * All inputs are returned unmodified. The vertex load indices are
7195 * stored after them, which will be used by the API VS for fetching inputs.
7197 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7202 * (VertexID + BaseVertex),
7203 * (InstanceID + StartInstance),
7204 * (InstanceID / 2 + StartInstance)
7206 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7207 union si_shader_part_key
*key
)
7209 struct si_function_info fninfo
;
7210 LLVMTypeRef
*returns
;
7211 LLVMValueRef ret
, func
;
7213 unsigned first_vs_vgpr
= key
->vs_prolog
.num_merged_next_stage_vgprs
;
7214 unsigned num_input_vgprs
= key
->vs_prolog
.num_merged_next_stage_vgprs
+ 4;
7215 LLVMValueRef input_vgprs
[9];
7216 unsigned num_all_input_regs
= key
->vs_prolog
.num_input_sgprs
+
7218 unsigned user_sgpr_base
= key
->vs_prolog
.num_merged_next_stage_vgprs
? 8 : 0;
7220 si_init_function_info(&fninfo
);
7222 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7223 returns
= alloca((num_all_input_regs
+ key
->vs_prolog
.last_input
+ 1) *
7224 sizeof(LLVMTypeRef
));
7227 /* Declare input and output SGPRs. */
7228 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7229 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7230 returns
[num_returns
++] = ctx
->i32
;
7233 /* Preloaded VGPRs (outputs must be floats) */
7234 for (i
= 0; i
< num_input_vgprs
; i
++) {
7235 add_arg_assign(&fninfo
, ARG_VGPR
, ctx
->i32
, &input_vgprs
[i
]);
7236 returns
[num_returns
++] = ctx
->f32
;
7239 /* Vertex load indices. */
7240 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7241 returns
[num_returns
++] = ctx
->f32
;
7243 /* Create the function. */
7244 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, &fninfo
, 0);
7245 func
= ctx
->main_fn
;
7247 if (key
->vs_prolog
.num_merged_next_stage_vgprs
) {
7248 if (!key
->vs_prolog
.is_monolithic
)
7249 si_init_exec_from_input(ctx
, 3, 0);
7251 if (key
->vs_prolog
.as_ls
&&
7252 ctx
->screen
->has_ls_vgpr_init_bug
) {
7253 /* If there are no HS threads, SPI loads the LS VGPRs
7254 * starting at VGPR 0. Shift them back to where they
7257 LLVMValueRef has_hs_threads
=
7258 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
7259 si_unpack_param(ctx
, 3, 8, 8),
7262 for (i
= 4; i
> 0; --i
) {
7263 input_vgprs
[i
+ 1] =
7264 LLVMBuildSelect(ctx
->ac
.builder
, has_hs_threads
,
7266 input_vgprs
[i
- 1], "");
7271 ctx
->abi
.vertex_id
= input_vgprs
[first_vs_vgpr
];
7272 ctx
->abi
.instance_id
= input_vgprs
[first_vs_vgpr
+ (key
->vs_prolog
.as_ls
? 2 : 1)];
7274 /* Copy inputs to outputs. This should be no-op, as the registers match,
7275 * but it will prevent the compiler from overwriting them unintentionally.
7277 ret
= ctx
->return_value
;
7278 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7279 LLVMValueRef p
= LLVMGetParam(func
, i
);
7280 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7282 for (i
= 0; i
< num_input_vgprs
; i
++) {
7283 LLVMValueRef p
= input_vgprs
[i
];
7284 p
= ac_to_float(&ctx
->ac
, p
);
7285 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
,
7286 key
->vs_prolog
.num_input_sgprs
+ i
, "");
7289 /* Compute vertex load indices from instance divisors. */
7290 LLVMValueRef instance_divisor_constbuf
= NULL
;
7292 if (key
->vs_prolog
.states
.instance_divisor_is_fetched
) {
7293 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7294 LLVMValueRef buf_index
=
7295 LLVMConstInt(ctx
->i32
, SI_VS_CONST_INSTANCE_DIVISORS
, 0);
7296 instance_divisor_constbuf
=
7297 ac_build_load_to_sgpr(&ctx
->ac
, list
, buf_index
);
7300 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7301 bool divisor_is_one
=
7302 key
->vs_prolog
.states
.instance_divisor_is_one
& (1u << i
);
7303 bool divisor_is_fetched
=
7304 key
->vs_prolog
.states
.instance_divisor_is_fetched
& (1u << i
);
7307 if (divisor_is_one
|| divisor_is_fetched
) {
7308 LLVMValueRef divisor
= ctx
->i32_1
;
7310 if (divisor_is_fetched
) {
7311 divisor
= buffer_load_const(ctx
, instance_divisor_constbuf
,
7312 LLVMConstInt(ctx
->i32
, i
* 4, 0));
7313 divisor
= ac_to_integer(&ctx
->ac
, divisor
);
7316 /* InstanceID / Divisor + StartInstance */
7317 index
= get_instance_index_for_fetch(ctx
,
7319 SI_SGPR_START_INSTANCE
,
7322 /* VertexID + BaseVertex */
7323 index
= LLVMBuildAdd(ctx
->ac
.builder
,
7325 LLVMGetParam(func
, user_sgpr_base
+
7326 SI_SGPR_BASE_VERTEX
), "");
7329 index
= ac_to_float(&ctx
->ac
, index
);
7330 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, index
,
7331 fninfo
.num_params
+ i
, "");
7334 si_llvm_build_ret(ctx
, ret
);
7337 static bool si_get_vs_prolog(struct si_screen
*sscreen
,
7338 struct ac_llvm_compiler
*compiler
,
7339 struct si_shader
*shader
,
7340 struct pipe_debug_callback
*debug
,
7341 struct si_shader
*main_part
,
7342 const struct si_vs_prolog_bits
*key
)
7344 struct si_shader_selector
*vs
= main_part
->selector
;
7346 if (!si_vs_needs_prolog(vs
, key
))
7349 /* Get the prolog. */
7350 union si_shader_part_key prolog_key
;
7351 si_get_vs_prolog_key(&vs
->info
, main_part
->info
.num_input_sgprs
,
7352 key
, shader
, &prolog_key
);
7355 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7356 PIPE_SHADER_VERTEX
, true, &prolog_key
, compiler
,
7357 debug
, si_build_vs_prolog_function
,
7358 "Vertex Shader Prolog");
7359 return shader
->prolog
!= NULL
;
7363 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7365 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7366 struct ac_llvm_compiler
*compiler
,
7367 struct si_shader
*shader
,
7368 struct pipe_debug_callback
*debug
)
7370 return si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, shader
,
7371 &shader
->key
.part
.vs
.prolog
);
7375 * Compile the TCS epilog function. This writes tesselation factors to memory
7376 * based on the output primitive type of the tesselator (determined by TES).
7378 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7379 union si_shader_part_key
*key
)
7381 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7382 struct si_function_info fninfo
;
7385 si_init_function_info(&fninfo
);
7387 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
7388 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7389 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7390 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7391 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
); /* wave info */
7392 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7393 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7394 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7395 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7396 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7397 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7398 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7399 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7400 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7401 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7402 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7403 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7404 if (!HAVE_32BIT_POINTERS
)
7405 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7406 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7407 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7408 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7410 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7411 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7412 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7413 add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7414 ctx
->param_tcs_offchip_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7415 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7416 ctx
->param_tcs_out_lds_layout
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7417 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7418 ctx
->param_tcs_offchip_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7419 ctx
->param_tcs_factor_offset
= add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7422 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7423 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* VGPR gap */
7424 unsigned tess_factors_idx
=
7425 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* patch index within the wave (REL_PATCH_ID) */
7426 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* invocation ID within the patch */
7427 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* LDS offset where tess factors should be loaded from */
7429 for (unsigned i
= 0; i
< 6; i
++)
7430 add_arg(&fninfo
, ARG_VGPR
, ctx
->i32
); /* tess factors */
7432 /* Create the function. */
7433 si_create_function(ctx
, "tcs_epilog", NULL
, 0, &fninfo
,
7434 ctx
->screen
->info
.chip_class
>= CIK
? 128 : 64);
7435 ac_declare_lds_as_pointer(&ctx
->ac
);
7436 func
= ctx
->main_fn
;
7438 LLVMValueRef invoc0_tess_factors
[6];
7439 for (unsigned i
= 0; i
< 6; i
++)
7440 invoc0_tess_factors
[i
] = LLVMGetParam(func
, tess_factors_idx
+ 3 + i
);
7442 si_write_tess_factors(bld_base
,
7443 LLVMGetParam(func
, tess_factors_idx
),
7444 LLVMGetParam(func
, tess_factors_idx
+ 1),
7445 LLVMGetParam(func
, tess_factors_idx
+ 2),
7446 invoc0_tess_factors
, invoc0_tess_factors
+ 4);
7448 LLVMBuildRetVoid(ctx
->ac
.builder
);
7452 * Select and compile (or reuse) TCS parts (epilog).
7454 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7455 struct ac_llvm_compiler
*compiler
,
7456 struct si_shader
*shader
,
7457 struct pipe_debug_callback
*debug
)
7459 if (sscreen
->info
.chip_class
>= GFX9
) {
7460 struct si_shader
*ls_main_part
=
7461 shader
->key
.part
.tcs
.ls
->main_shader_part_ls
;
7463 if (!si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, ls_main_part
,
7464 &shader
->key
.part
.tcs
.ls_prolog
))
7467 shader
->previous_stage
= ls_main_part
;
7470 /* Get the epilog. */
7471 union si_shader_part_key epilog_key
;
7472 memset(&epilog_key
, 0, sizeof(epilog_key
));
7473 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7475 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7476 PIPE_SHADER_TESS_CTRL
, false,
7477 &epilog_key
, compiler
, debug
,
7478 si_build_tcs_epilog_function
,
7479 "Tessellation Control Shader Epilog");
7480 return shader
->epilog
!= NULL
;
7484 * Select and compile (or reuse) GS parts (prolog).
7486 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7487 struct ac_llvm_compiler
*compiler
,
7488 struct si_shader
*shader
,
7489 struct pipe_debug_callback
*debug
)
7491 if (sscreen
->info
.chip_class
>= GFX9
) {
7492 struct si_shader
*es_main_part
=
7493 shader
->key
.part
.gs
.es
->main_shader_part_es
;
7495 if (shader
->key
.part
.gs
.es
->type
== PIPE_SHADER_VERTEX
&&
7496 !si_get_vs_prolog(sscreen
, compiler
, shader
, debug
, es_main_part
,
7497 &shader
->key
.part
.gs
.vs_prolog
))
7500 shader
->previous_stage
= es_main_part
;
7503 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7506 union si_shader_part_key prolog_key
;
7507 memset(&prolog_key
, 0, sizeof(prolog_key
));
7508 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7510 shader
->prolog2
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7511 PIPE_SHADER_GEOMETRY
, true,
7512 &prolog_key
, compiler
, debug
,
7513 si_build_gs_prolog_function
,
7514 "Geometry Shader Prolog");
7515 return shader
->prolog2
!= NULL
;
7519 * Build the pixel shader prolog function. This handles:
7520 * - two-side color selection and interpolation
7521 * - overriding interpolation parameters for the API PS
7522 * - polygon stippling
7524 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7525 * overriden by other states. (e.g. per-sample interpolation)
7526 * Interpolated colors are stored after the preloaded VGPRs.
7528 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7529 union si_shader_part_key
*key
)
7531 struct si_function_info fninfo
;
7532 LLVMValueRef ret
, func
;
7533 int num_returns
, i
, num_color_channels
;
7535 assert(si_need_ps_prolog(key
));
7537 si_init_function_info(&fninfo
);
7539 /* Declare inputs. */
7540 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
7541 add_arg(&fninfo
, ARG_SGPR
, ctx
->i32
);
7543 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
7544 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7546 /* Declare outputs (same as inputs + add colors if needed) */
7547 num_returns
= fninfo
.num_params
;
7548 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
7549 for (i
= 0; i
< num_color_channels
; i
++)
7550 fninfo
.types
[num_returns
++] = ctx
->f32
;
7552 /* Create the function. */
7553 si_create_function(ctx
, "ps_prolog", fninfo
.types
, num_returns
,
7555 func
= ctx
->main_fn
;
7557 /* Copy inputs to outputs. This should be no-op, as the registers match,
7558 * but it will prevent the compiler from overwriting them unintentionally.
7560 ret
= ctx
->return_value
;
7561 for (i
= 0; i
< fninfo
.num_params
; i
++) {
7562 LLVMValueRef p
= LLVMGetParam(func
, i
);
7563 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, p
, i
, "");
7566 /* Polygon stippling. */
7567 if (key
->ps_prolog
.states
.poly_stipple
) {
7568 /* POS_FIXED_PT is always last. */
7569 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
7570 key
->ps_prolog
.num_input_vgprs
- 1;
7571 LLVMValueRef list
= si_prolog_get_rw_buffers(ctx
);
7573 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
7576 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
7577 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7578 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7579 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
7581 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7582 * The hw doesn't compute CENTROID if the whole wave only
7583 * contains fully-covered quads.
7585 * PRIM_MASK is after user SGPRs.
7587 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7588 bc_optimize
= LLVMBuildLShr(ctx
->ac
.builder
, bc_optimize
,
7589 LLVMConstInt(ctx
->i32
, 31, 0), "");
7590 bc_optimize
= LLVMBuildTrunc(ctx
->ac
.builder
, bc_optimize
,
7593 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
7594 /* Read PERSP_CENTER. */
7595 for (i
= 0; i
< 2; i
++)
7596 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7597 /* Read PERSP_CENTROID. */
7598 for (i
= 0; i
< 2; i
++)
7599 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
7600 /* Select PERSP_CENTROID. */
7601 for (i
= 0; i
< 2; i
++) {
7602 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7603 center
[i
], centroid
[i
], "");
7604 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7605 tmp
, base
+ 4 + i
, "");
7608 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7609 /* Read LINEAR_CENTER. */
7610 for (i
= 0; i
< 2; i
++)
7611 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7612 /* Read LINEAR_CENTROID. */
7613 for (i
= 0; i
< 2; i
++)
7614 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
7615 /* Select LINEAR_CENTROID. */
7616 for (i
= 0; i
< 2; i
++) {
7617 tmp
= LLVMBuildSelect(ctx
->ac
.builder
, bc_optimize
,
7618 center
[i
], centroid
[i
], "");
7619 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7620 tmp
, base
+ 10 + i
, "");
7625 /* Force per-sample interpolation. */
7626 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
7627 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7628 LLVMValueRef persp_sample
[2];
7630 /* Read PERSP_SAMPLE. */
7631 for (i
= 0; i
< 2; i
++)
7632 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
7633 /* Overwrite PERSP_CENTER. */
7634 for (i
= 0; i
< 2; i
++)
7635 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7636 persp_sample
[i
], base
+ 2 + i
, "");
7637 /* Overwrite PERSP_CENTROID. */
7638 for (i
= 0; i
< 2; i
++)
7639 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7640 persp_sample
[i
], base
+ 4 + i
, "");
7642 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
7643 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7644 LLVMValueRef linear_sample
[2];
7646 /* Read LINEAR_SAMPLE. */
7647 for (i
= 0; i
< 2; i
++)
7648 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
7649 /* Overwrite LINEAR_CENTER. */
7650 for (i
= 0; i
< 2; i
++)
7651 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7652 linear_sample
[i
], base
+ 8 + i
, "");
7653 /* Overwrite LINEAR_CENTROID. */
7654 for (i
= 0; i
< 2; i
++)
7655 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7656 linear_sample
[i
], base
+ 10 + i
, "");
7659 /* Force center interpolation. */
7660 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
7661 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7662 LLVMValueRef persp_center
[2];
7664 /* Read PERSP_CENTER. */
7665 for (i
= 0; i
< 2; i
++)
7666 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7667 /* Overwrite PERSP_SAMPLE. */
7668 for (i
= 0; i
< 2; i
++)
7669 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7670 persp_center
[i
], base
+ i
, "");
7671 /* Overwrite PERSP_CENTROID. */
7672 for (i
= 0; i
< 2; i
++)
7673 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7674 persp_center
[i
], base
+ 4 + i
, "");
7676 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
7677 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7678 LLVMValueRef linear_center
[2];
7680 /* Read LINEAR_CENTER. */
7681 for (i
= 0; i
< 2; i
++)
7682 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7683 /* Overwrite LINEAR_SAMPLE. */
7684 for (i
= 0; i
< 2; i
++)
7685 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7686 linear_center
[i
], base
+ 6 + i
, "");
7687 /* Overwrite LINEAR_CENTROID. */
7688 for (i
= 0; i
< 2; i
++)
7689 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
7690 linear_center
[i
], base
+ 10 + i
, "");
7693 /* Interpolate colors. */
7694 unsigned color_out_idx
= 0;
7695 for (i
= 0; i
< 2; i
++) {
7696 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
7697 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7698 key
->ps_prolog
.face_vgpr_index
;
7699 LLVMValueRef interp
[2], color
[4];
7700 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
7705 /* If the interpolation qualifier is not CONSTANT (-1). */
7706 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
7707 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7708 key
->ps_prolog
.color_interp_vgpr_index
[i
];
7710 /* Get the (i,j) updated by bc_optimize handling. */
7711 interp
[0] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7713 interp
[1] = LLVMBuildExtractValue(ctx
->ac
.builder
, ret
,
7714 interp_vgpr
+ 1, "");
7715 interp_ij
= ac_build_gather_values(&ctx
->ac
, interp
, 2);
7718 /* Use the absolute location of the input. */
7719 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7721 if (key
->ps_prolog
.states
.color_two_side
) {
7722 face
= LLVMGetParam(func
, face_vgpr
);
7723 face
= ac_to_integer(&ctx
->ac
, face
);
7726 interp_fs_input(ctx
,
7727 key
->ps_prolog
.color_attr_index
[i
],
7728 TGSI_SEMANTIC_COLOR
, i
,
7729 key
->ps_prolog
.num_interp_inputs
,
7730 key
->ps_prolog
.colors_read
, interp_ij
,
7731 prim_mask
, face
, color
);
7734 unsigned chan
= u_bit_scan(&writemask
);
7735 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, color
[chan
],
7736 fninfo
.num_params
+ color_out_idx
++, "");
7740 /* Section 15.2.2 (Shader Inputs) of the OpenGL 4.5 (Core Profile) spec
7743 * "When per-sample shading is active due to the use of a fragment
7744 * input qualified by sample or due to the use of the gl_SampleID
7745 * or gl_SamplePosition variables, only the bit for the current
7746 * sample is set in gl_SampleMaskIn. When state specifies multiple
7747 * fragment shader invocations for a given fragment, the sample
7748 * mask for any single fragment shader invocation may specify a
7749 * subset of the covered samples for the fragment. In this case,
7750 * the bit corresponding to each covered sample will be set in
7751 * exactly one fragment shader invocation."
7753 * The samplemask loaded by hardware is always the coverage of the
7754 * entire pixel/fragment, so mask bits out based on the sample ID.
7756 if (key
->ps_prolog
.states
.samplemask_log_ps_iter
) {
7757 /* The bit pattern matches that used by fixed function fragment
7759 static const uint16_t ps_iter_masks
[] = {
7760 0xffff, /* not used */
7766 assert(key
->ps_prolog
.states
.samplemask_log_ps_iter
< ARRAY_SIZE(ps_iter_masks
));
7768 uint32_t ps_iter_mask
= ps_iter_masks
[key
->ps_prolog
.states
.samplemask_log_ps_iter
];
7769 unsigned ancillary_vgpr
= key
->ps_prolog
.num_input_sgprs
+
7770 key
->ps_prolog
.ancillary_vgpr_index
;
7771 LLVMValueRef sampleid
= si_unpack_param(ctx
, ancillary_vgpr
, 8, 4);
7772 LLVMValueRef samplemask
= LLVMGetParam(func
, ancillary_vgpr
+ 1);
7774 samplemask
= ac_to_integer(&ctx
->ac
, samplemask
);
7775 samplemask
= LLVMBuildAnd(
7778 LLVMBuildShl(ctx
->ac
.builder
,
7779 LLVMConstInt(ctx
->i32
, ps_iter_mask
, false),
7782 samplemask
= ac_to_float(&ctx
->ac
, samplemask
);
7784 ret
= LLVMBuildInsertValue(ctx
->ac
.builder
, ret
, samplemask
,
7785 ancillary_vgpr
+ 1, "");
7788 /* Tell LLVM to insert WQM instruction sequence when needed. */
7789 if (key
->ps_prolog
.wqm
) {
7790 LLVMAddTargetDependentFunctionAttr(func
,
7791 "amdgpu-ps-wqm-outputs", "");
7794 si_llvm_build_ret(ctx
, ret
);
7798 * Build the pixel shader epilog function. This handles everything that must be
7799 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7801 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
7802 union si_shader_part_key
*key
)
7804 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7805 struct si_function_info fninfo
;
7806 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
7808 struct si_ps_exports exp
= {};
7810 si_init_function_info(&fninfo
);
7812 /* Declare input SGPRs. */
7813 ctx
->param_rw_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7814 ctx
->param_bindless_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7815 ctx
->param_const_and_shader_buffers
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7816 ctx
->param_samplers_and_images
= add_arg(&fninfo
, ARG_SGPR
, ctx
->ac
.intptr
);
7817 add_arg_checked(&fninfo
, ARG_SGPR
, ctx
->f32
, SI_PARAM_ALPHA_REF
);
7819 /* Declare input VGPRs. */
7820 unsigned required_num_params
=
7821 fninfo
.num_sgpr_params
+
7822 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
7823 key
->ps_epilog
.writes_z
+
7824 key
->ps_epilog
.writes_stencil
+
7825 key
->ps_epilog
.writes_samplemask
;
7827 required_num_params
= MAX2(required_num_params
,
7828 fninfo
.num_sgpr_params
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
7830 while (fninfo
.num_params
< required_num_params
)
7831 add_arg(&fninfo
, ARG_VGPR
, ctx
->f32
);
7833 /* Create the function. */
7834 si_create_function(ctx
, "ps_epilog", NULL
, 0, &fninfo
, 0);
7835 /* Disable elimination of unused inputs. */
7836 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
7837 "InitialPSInputAddr", 0xffffff);
7839 /* Process colors. */
7840 unsigned vgpr
= fninfo
.num_sgpr_params
;
7841 unsigned colors_written
= key
->ps_epilog
.colors_written
;
7842 int last_color_export
= -1;
7844 /* Find the last color export. */
7845 if (!key
->ps_epilog
.writes_z
&&
7846 !key
->ps_epilog
.writes_stencil
&&
7847 !key
->ps_epilog
.writes_samplemask
) {
7848 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
7850 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7851 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
7852 /* Just set this if any of the colorbuffers are enabled. */
7854 ((1ull << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
7855 last_color_export
= 0;
7857 for (i
= 0; i
< 8; i
++)
7858 if (colors_written
& (1 << i
) &&
7859 (spi_format
>> (i
* 4)) & 0xf)
7860 last_color_export
= i
;
7864 while (colors_written
) {
7865 LLVMValueRef color
[4];
7866 int mrt
= u_bit_scan(&colors_written
);
7868 for (i
= 0; i
< 4; i
++)
7869 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
7871 si_export_mrt_color(bld_base
, color
, mrt
,
7872 fninfo
.num_params
- 1,
7873 mrt
== last_color_export
, &exp
);
7876 /* Process depth, stencil, samplemask. */
7877 if (key
->ps_epilog
.writes_z
)
7878 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7879 if (key
->ps_epilog
.writes_stencil
)
7880 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7881 if (key
->ps_epilog
.writes_samplemask
)
7882 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
7884 if (depth
|| stencil
|| samplemask
)
7885 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
7886 else if (last_color_export
== -1)
7887 ac_build_export_null(&ctx
->ac
);
7890 si_emit_ps_exports(ctx
, &exp
);
7893 LLVMBuildRetVoid(ctx
->ac
.builder
);
7897 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7899 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
7900 struct ac_llvm_compiler
*compiler
,
7901 struct si_shader
*shader
,
7902 struct pipe_debug_callback
*debug
)
7904 union si_shader_part_key prolog_key
;
7905 union si_shader_part_key epilog_key
;
7907 /* Get the prolog. */
7908 si_get_ps_prolog_key(shader
, &prolog_key
, true);
7910 /* The prolog is a no-op if these aren't set. */
7911 if (si_need_ps_prolog(&prolog_key
)) {
7913 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
7914 PIPE_SHADER_FRAGMENT
, true,
7915 &prolog_key
, compiler
, debug
,
7916 si_build_ps_prolog_function
,
7917 "Fragment Shader Prolog");
7918 if (!shader
->prolog
)
7922 /* Get the epilog. */
7923 si_get_ps_epilog_key(shader
, &epilog_key
);
7926 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
7927 PIPE_SHADER_FRAGMENT
, false,
7928 &epilog_key
, compiler
, debug
,
7929 si_build_ps_epilog_function
,
7930 "Fragment Shader Epilog");
7931 if (!shader
->epilog
)
7934 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
7935 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
7936 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
7937 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
7940 /* Set up the enable bits for per-sample shading if needed. */
7941 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
7942 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7943 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7944 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
7945 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7946 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
7948 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
7949 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
7950 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7951 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
7952 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7953 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
7955 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
7956 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7957 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7958 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
7959 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
7960 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7962 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
7963 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
7964 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
7965 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
7966 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
7967 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7970 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
7971 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
7972 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
7973 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
7974 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7977 /* At least one pair of interpolation weights must be enabled. */
7978 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
7979 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
7980 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
7983 /* Samplemask fixup requires the sample ID. */
7984 if (shader
->key
.part
.ps
.prolog
.samplemask_log_ps_iter
) {
7985 shader
->config
.spi_ps_input_ena
|= S_0286CC_ANCILLARY_ENA(1);
7986 assert(G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
));
7989 /* The sample mask input is always enabled, because the API shader always
7990 * passes it through to the epilog. Disable it here if it's unused.
7992 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
7993 !shader
->selector
->info
.reads_samplemask
)
7994 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
7999 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
8002 /* If tessellation is all offchip and on-chip GS isn't used, this
8003 * workaround is not needed.
8007 /* SPI barrier management bug:
8008 * Make sure we have at least 4k of LDS in use to avoid the bug.
8009 * It applies to workgroup sizes of more than one wavefront.
8011 if (sscreen
->info
.family
== CHIP_BONAIRE
||
8012 sscreen
->info
.family
== CHIP_KABINI
||
8013 sscreen
->info
.family
== CHIP_MULLINS
)
8014 *lds_size
= MAX2(*lds_size
, 8);
8017 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8018 struct si_shader
*shader
)
8020 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8022 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8024 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8025 si_get_max_workgroup_size(shader
) > 64) {
8026 si_multiwave_lds_size_workaround(sscreen
,
8027 &shader
->config
.lds_size
);
8031 int si_shader_create(struct si_screen
*sscreen
, struct ac_llvm_compiler
*compiler
,
8032 struct si_shader
*shader
,
8033 struct pipe_debug_callback
*debug
)
8035 struct si_shader_selector
*sel
= shader
->selector
;
8036 struct si_shader
*mainp
= *si_get_main_shader_part(sel
, &shader
->key
);
8039 /* LS, ES, VS are compiled on demand if the main part hasn't been
8040 * compiled for that stage.
8042 * Vertex shaders are compiled on demand when a vertex fetch
8043 * workaround must be applied.
8045 if (shader
->is_monolithic
) {
8046 /* Monolithic shader (compiled as a whole, has many variants,
8047 * may take a long time to compile).
8049 r
= si_compile_tgsi_shader(sscreen
, compiler
, shader
, debug
);
8053 /* The shader consists of several parts:
8055 * - the middle part is the user shader, it has 1 variant only
8056 * and it was compiled during the creation of the shader
8058 * - the prolog part is inserted at the beginning
8059 * - the epilog part is inserted at the end
8061 * The prolog and epilog have many (but simple) variants.
8063 * Starting with gfx9, geometry and tessellation control
8064 * shaders also contain the prolog and user shader parts of
8065 * the previous shader stage.
8071 /* Copy the compiled TGSI shader data over. */
8072 shader
->is_binary_shared
= true;
8073 shader
->binary
= mainp
->binary
;
8074 shader
->config
= mainp
->config
;
8075 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8076 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8077 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8078 shader
->info
.ancillary_vgpr_index
= mainp
->info
.ancillary_vgpr_index
;
8079 memcpy(shader
->info
.vs_output_param_offset
,
8080 mainp
->info
.vs_output_param_offset
,
8081 sizeof(mainp
->info
.vs_output_param_offset
));
8082 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8083 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8084 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8086 /* Select prologs and/or epilogs. */
8087 switch (sel
->type
) {
8088 case PIPE_SHADER_VERTEX
:
8089 if (!si_shader_select_vs_parts(sscreen
, compiler
, shader
, debug
))
8092 case PIPE_SHADER_TESS_CTRL
:
8093 if (!si_shader_select_tcs_parts(sscreen
, compiler
, shader
, debug
))
8096 case PIPE_SHADER_TESS_EVAL
:
8098 case PIPE_SHADER_GEOMETRY
:
8099 if (!si_shader_select_gs_parts(sscreen
, compiler
, shader
, debug
))
8102 case PIPE_SHADER_FRAGMENT
:
8103 if (!si_shader_select_ps_parts(sscreen
, compiler
, shader
, debug
))
8106 /* Make sure we have at least as many VGPRs as there
8107 * are allocated inputs.
8109 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8110 shader
->info
.num_input_vgprs
);
8114 /* Update SGPR and VGPR counts. */
8115 if (shader
->prolog
) {
8116 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8117 shader
->prolog
->config
.num_sgprs
);
8118 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8119 shader
->prolog
->config
.num_vgprs
);
8121 if (shader
->previous_stage
) {
8122 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8123 shader
->previous_stage
->config
.num_sgprs
);
8124 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8125 shader
->previous_stage
->config
.num_vgprs
);
8126 shader
->config
.spilled_sgprs
=
8127 MAX2(shader
->config
.spilled_sgprs
,
8128 shader
->previous_stage
->config
.spilled_sgprs
);
8129 shader
->config
.spilled_vgprs
=
8130 MAX2(shader
->config
.spilled_vgprs
,
8131 shader
->previous_stage
->config
.spilled_vgprs
);
8132 shader
->config
.private_mem_vgprs
=
8133 MAX2(shader
->config
.private_mem_vgprs
,
8134 shader
->previous_stage
->config
.private_mem_vgprs
);
8135 shader
->config
.scratch_bytes_per_wave
=
8136 MAX2(shader
->config
.scratch_bytes_per_wave
,
8137 shader
->previous_stage
->config
.scratch_bytes_per_wave
);
8138 shader
->info
.uses_instanceid
|=
8139 shader
->previous_stage
->info
.uses_instanceid
;
8141 if (shader
->prolog2
) {
8142 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8143 shader
->prolog2
->config
.num_sgprs
);
8144 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8145 shader
->prolog2
->config
.num_vgprs
);
8147 if (shader
->epilog
) {
8148 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8149 shader
->epilog
->config
.num_sgprs
);
8150 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8151 shader
->epilog
->config
.num_vgprs
);
8153 si_calculate_max_simd_waves(shader
);
8156 si_fix_resource_usage(sscreen
, shader
);
8157 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8161 r
= si_shader_binary_upload(sscreen
, shader
);
8163 fprintf(stderr
, "LLVM failed to upload shader\n");
8170 void si_shader_destroy(struct si_shader
*shader
)
8172 if (shader
->scratch_bo
)
8173 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8175 r600_resource_reference(&shader
->bo
, NULL
);
8177 if (!shader
->is_binary_shared
)
8178 ac_shader_binary_clean(&shader
->binary
);
8180 free(shader
->shader_log
);