2 * Copyright 2012 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
29 #include "gallivm/lp_bld_const.h"
30 #include "gallivm/lp_bld_gather.h"
31 #include "gallivm/lp_bld_intr.h"
32 #include "gallivm/lp_bld_logic.h"
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_flow.h"
35 #include "gallivm/lp_bld_misc.h"
36 #include "util/u_memory.h"
37 #include "util/u_string.h"
38 #include "tgsi/tgsi_build.h"
39 #include "tgsi/tgsi_util.h"
40 #include "tgsi/tgsi_dump.h"
42 #include "ac_binary.h"
43 #include "ac_llvm_util.h"
44 #include "si_shader_internal.h"
49 static const char *scratch_rsrc_dword0_symbol
=
50 "SCRATCH_RSRC_DWORD0";
52 static const char *scratch_rsrc_dword1_symbol
=
53 "SCRATCH_RSRC_DWORD1";
55 struct si_shader_output_values
57 LLVMValueRef values
[4];
58 unsigned semantic_name
;
59 unsigned semantic_index
;
60 ubyte vertex_stream
[4];
63 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
64 struct si_screen
*sscreen
,
65 struct si_shader
*shader
,
66 LLVMTargetMachineRef tm
);
68 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
69 struct lp_build_tgsi_context
*bld_base
,
70 struct lp_build_emit_data
*emit_data
);
72 static void si_dump_shader_key(unsigned shader
, struct si_shader_key
*key
,
75 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
76 union si_shader_part_key
*key
);
77 static void si_build_vs_epilog_function(struct si_shader_context
*ctx
,
78 union si_shader_part_key
*key
);
79 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
80 union si_shader_part_key
*key
);
81 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
82 union si_shader_part_key
*key
);
83 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
84 union si_shader_part_key
*key
);
86 /* Ideally pass the sample mask input to the PS epilog as v13, which
87 * is its usual location, so that the shader doesn't have to add v_mov.
89 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
91 /* The VS location of the PrimitiveID input is the same in the epilog,
92 * so that the main shader part doesn't have to move it.
94 #define VS_EPILOG_PRIMID_LOC 2
102 * Returns a unique index for a semantic name and index. The index must be
103 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
106 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
108 switch (semantic_name
) {
109 case TGSI_SEMANTIC_POSITION
:
111 case TGSI_SEMANTIC_PSIZE
:
113 case TGSI_SEMANTIC_CLIPDIST
:
116 case TGSI_SEMANTIC_GENERIC
:
120 assert(!"invalid generic index");
123 /* patch indices are completely separate and thus start from 0 */
124 case TGSI_SEMANTIC_TESSOUTER
:
126 case TGSI_SEMANTIC_TESSINNER
:
128 case TGSI_SEMANTIC_PATCH
:
132 assert(!"invalid semantic name");
137 unsigned si_shader_io_get_unique_index2(unsigned name
, unsigned index
)
140 case TGSI_SEMANTIC_FOG
:
142 case TGSI_SEMANTIC_LAYER
:
144 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
146 case TGSI_SEMANTIC_PRIMID
:
148 case TGSI_SEMANTIC_COLOR
: /* these alias */
149 case TGSI_SEMANTIC_BCOLOR
:
151 case TGSI_SEMANTIC_TEXCOORD
:
154 assert(!"invalid semantic name");
160 * Get the value of a shader input parameter and extract a bitfield.
162 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
163 unsigned param
, unsigned rshift
,
166 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
167 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
,
170 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
171 value
= bitcast(&ctx
->bld_base
,
172 TGSI_TYPE_UNSIGNED
, value
);
175 value
= LLVMBuildLShr(gallivm
->builder
, value
,
176 lp_build_const_int32(gallivm
, rshift
), "");
178 if (rshift
+ bitwidth
< 32) {
179 unsigned mask
= (1 << bitwidth
) - 1;
180 value
= LLVMBuildAnd(gallivm
->builder
, value
,
181 lp_build_const_int32(gallivm
, mask
), "");
187 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
190 case PIPE_SHADER_TESS_CTRL
:
191 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
193 case PIPE_SHADER_TESS_EVAL
:
194 return LLVMGetParam(ctx
->main_fn
,
195 ctx
->param_tes_rel_patch_id
);
203 /* Tessellation shaders pass outputs to the next shader using LDS.
205 * LS outputs = TCS inputs
206 * TCS outputs = TES inputs
209 * - TCS inputs for patch 0
210 * - TCS inputs for patch 1
211 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
213 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
214 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
215 * - TCS outputs for patch 1
216 * - Per-patch TCS outputs for patch 1
217 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
218 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
221 * All three shaders VS(LS), TCS, TES share the same LDS space.
225 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
227 if (ctx
->type
== PIPE_SHADER_VERTEX
)
228 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
229 else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
230 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
238 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
240 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
244 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
246 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
248 SI_PARAM_TCS_OUT_OFFSETS
,
254 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
256 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
258 SI_PARAM_TCS_OUT_OFFSETS
,
264 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
266 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
267 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
268 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
270 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
274 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
276 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
277 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
278 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
279 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
281 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
282 LLVMBuildMul(gallivm
->builder
, patch_stride
,
288 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
290 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
291 LLVMValueRef patch0_patch_data_offset
=
292 get_tcs_out_patch0_patch_data_offset(ctx
);
293 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
294 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
296 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
297 LLVMBuildMul(gallivm
->builder
, patch_stride
,
302 static LLVMValueRef
get_instance_index_for_fetch(
303 struct si_shader_context
*ctx
,
304 unsigned param_start_instance
, unsigned divisor
)
306 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
308 LLVMValueRef result
= LLVMGetParam(ctx
->main_fn
,
309 ctx
->param_instance_id
);
311 /* The division must be done before START_INSTANCE is added. */
313 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
314 lp_build_const_int32(gallivm
, divisor
), "");
316 return LLVMBuildAdd(gallivm
->builder
, result
,
317 LLVMGetParam(ctx
->main_fn
, param_start_instance
), "");
320 /* Bitcast <4 x float> to <2 x double>, extract the component, and convert
322 static LLVMValueRef
extract_double_to_float(struct si_shader_context
*ctx
,
324 unsigned double_index
)
326 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
327 LLVMTypeRef f64
= LLVMDoubleTypeInContext(ctx
->gallivm
.context
);
328 LLVMValueRef dvec2
= LLVMBuildBitCast(builder
, vec4
,
329 LLVMVectorType(f64
, 2), "");
330 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, double_index
, 0);
331 LLVMValueRef value
= LLVMBuildExtractElement(builder
, dvec2
, index
, "");
332 return LLVMBuildFPTrunc(builder
, value
, ctx
->f32
, "");
335 static void declare_input_vs(
336 struct si_shader_context
*ctx
,
337 unsigned input_index
,
338 const struct tgsi_full_declaration
*decl
,
341 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
342 struct gallivm_state
*gallivm
= base
->gallivm
;
346 unsigned num_fetches
;
347 unsigned fetch_stride
;
349 LLVMValueRef t_list_ptr
;
350 LLVMValueRef t_offset
;
352 LLVMValueRef vertex_index
;
353 LLVMValueRef input
[3];
355 /* Load the T list */
356 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_VERTEX_BUFFERS
);
358 t_offset
= lp_build_const_int32(gallivm
, input_index
);
360 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
362 vertex_index
= LLVMGetParam(ctx
->main_fn
,
363 ctx
->param_vertex_index0
+
366 fix_fetch
= ctx
->shader
->key
.mono
.vs
.fix_fetch
[input_index
];
368 /* Do multiple loads for special formats. */
370 case SI_FIX_FETCH_RGB_64_FLOAT
:
371 num_fetches
= 3; /* 3 2-dword loads */
374 case SI_FIX_FETCH_RGBA_64_FLOAT
:
375 num_fetches
= 2; /* 2 4-dword loads */
378 case SI_FIX_FETCH_RGB_8
:
379 case SI_FIX_FETCH_RGB_8_INT
:
383 case SI_FIX_FETCH_RGB_16
:
384 case SI_FIX_FETCH_RGB_16_INT
:
393 for (unsigned i
= 0; i
< num_fetches
; i
++) {
394 LLVMValueRef voffset
= LLVMConstInt(ctx
->i32
, fetch_stride
* i
, 0);
396 input
[i
] = ac_build_buffer_load_format(&ctx
->ac
, t_list
,
397 vertex_index
, voffset
,
401 /* Break up the vec4 into individual components */
402 for (chan
= 0; chan
< 4; chan
++) {
403 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
404 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
405 input
[0], llvm_chan
, "");
409 case SI_FIX_FETCH_A2_SNORM
:
410 case SI_FIX_FETCH_A2_SSCALED
:
411 case SI_FIX_FETCH_A2_SINT
: {
412 /* The hardware returns an unsigned value; convert it to a
415 LLVMValueRef tmp
= out
[3];
416 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
418 /* First, recover the sign-extended signed integer value. */
419 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
420 tmp
= LLVMBuildFPToUI(gallivm
->builder
, tmp
, ctx
->i32
, "");
422 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->i32
, "");
424 /* For the integer-like cases, do a natural sign extension.
426 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
427 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
430 tmp
= LLVMBuildShl(gallivm
->builder
, tmp
,
431 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
432 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
433 tmp
= LLVMBuildAShr(gallivm
->builder
, tmp
, c30
, "");
435 /* Convert back to the right type. */
436 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
438 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
439 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
440 clamp
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
441 tmp
= LLVMBuildSelect(gallivm
->builder
, clamp
, neg_one
, tmp
, "");
442 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
443 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
449 case SI_FIX_FETCH_RGBA_32_UNORM
:
450 case SI_FIX_FETCH_RGBX_32_UNORM
:
451 for (chan
= 0; chan
< 4; chan
++) {
452 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
454 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
455 out
[chan
], ctx
->f32
, "");
456 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
457 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
459 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
460 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
461 out
[3] = LLVMConstReal(ctx
->f32
, 1);
463 case SI_FIX_FETCH_RGBA_32_SNORM
:
464 case SI_FIX_FETCH_RGBX_32_SNORM
:
465 case SI_FIX_FETCH_RGBA_32_FIXED
:
466 case SI_FIX_FETCH_RGBX_32_FIXED
: {
468 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
469 scale
= 1.0 / 0x10000;
471 scale
= 1.0 / INT_MAX
;
473 for (chan
= 0; chan
< 4; chan
++) {
474 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
476 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
477 out
[chan
], ctx
->f32
, "");
478 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
479 LLVMConstReal(ctx
->f32
, scale
), "");
481 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
482 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
483 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
484 out
[3] = LLVMConstReal(ctx
->f32
, 1);
487 case SI_FIX_FETCH_RGBA_32_USCALED
:
488 for (chan
= 0; chan
< 4; chan
++) {
489 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
491 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
492 out
[chan
], ctx
->f32
, "");
495 case SI_FIX_FETCH_RGBA_32_SSCALED
:
496 for (chan
= 0; chan
< 4; chan
++) {
497 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
499 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
500 out
[chan
], ctx
->f32
, "");
503 case SI_FIX_FETCH_RG_64_FLOAT
:
504 for (chan
= 0; chan
< 2; chan
++)
505 out
[chan
] = extract_double_to_float(ctx
, input
[0], chan
);
507 out
[2] = LLVMConstReal(ctx
->f32
, 0);
508 out
[3] = LLVMConstReal(ctx
->f32
, 1);
510 case SI_FIX_FETCH_RGB_64_FLOAT
:
511 for (chan
= 0; chan
< 3; chan
++)
512 out
[chan
] = extract_double_to_float(ctx
, input
[chan
], 0);
514 out
[3] = LLVMConstReal(ctx
->f32
, 1);
516 case SI_FIX_FETCH_RGBA_64_FLOAT
:
517 for (chan
= 0; chan
< 4; chan
++) {
518 out
[chan
] = extract_double_to_float(ctx
, input
[chan
/ 2],
522 case SI_FIX_FETCH_RGB_8
:
523 case SI_FIX_FETCH_RGB_8_INT
:
524 case SI_FIX_FETCH_RGB_16
:
525 case SI_FIX_FETCH_RGB_16_INT
:
526 for (chan
= 0; chan
< 3; chan
++) {
527 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
531 if (fix_fetch
== SI_FIX_FETCH_RGB_8
||
532 fix_fetch
== SI_FIX_FETCH_RGB_16
) {
533 out
[3] = LLVMConstReal(ctx
->f32
, 1);
535 out
[3] = LLVMBuildBitCast(gallivm
->builder
, ctx
->i32_1
,
542 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
545 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
548 return bld_base
->uint_bld
.zero
;
551 case PIPE_SHADER_VERTEX
:
552 return LLVMGetParam(ctx
->main_fn
,
553 ctx
->param_vs_prim_id
);
554 case PIPE_SHADER_TESS_CTRL
:
555 return LLVMGetParam(ctx
->main_fn
,
557 case PIPE_SHADER_TESS_EVAL
:
558 return LLVMGetParam(ctx
->main_fn
,
559 ctx
->param_tes_patch_id
);
560 case PIPE_SHADER_GEOMETRY
:
561 return LLVMGetParam(ctx
->main_fn
,
562 SI_PARAM_PRIMITIVE_ID
);
565 return bld_base
->uint_bld
.zero
;
570 * Return the value of tgsi_ind_register for indexing.
571 * This is the indirect index with the constant offset added to it.
573 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
574 const struct tgsi_ind_register
*ind
,
577 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
580 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
581 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
582 result
= LLVMBuildAdd(gallivm
->builder
, result
,
583 lp_build_const_int32(gallivm
, rel_index
), "");
588 * Like get_indirect_index, but restricts the return value to a (possibly
589 * undefined) value inside [0..num).
591 static LLVMValueRef
get_bounded_indirect_index(struct si_shader_context
*ctx
,
592 const struct tgsi_ind_register
*ind
,
593 int rel_index
, unsigned num
)
595 LLVMValueRef result
= get_indirect_index(ctx
, ind
, rel_index
);
597 /* LLVM 3.8: If indirect resource indexing is used:
601 if (HAVE_LLVM
== 0x0308)
602 return LLVMGetUndef(ctx
->i32
);
604 return si_llvm_bound_index(ctx
, result
, num
);
609 * Calculate a dword address given an input or output register and a stride.
611 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
612 const struct tgsi_full_dst_register
*dst
,
613 const struct tgsi_full_src_register
*src
,
614 LLVMValueRef vertex_dw_stride
,
615 LLVMValueRef base_addr
)
617 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
618 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
619 ubyte
*name
, *index
, *array_first
;
621 struct tgsi_full_dst_register reg
;
623 /* Set the register description. The address computation is the same
624 * for sources and destinations. */
626 reg
.Register
.File
= src
->Register
.File
;
627 reg
.Register
.Index
= src
->Register
.Index
;
628 reg
.Register
.Indirect
= src
->Register
.Indirect
;
629 reg
.Register
.Dimension
= src
->Register
.Dimension
;
630 reg
.Indirect
= src
->Indirect
;
631 reg
.Dimension
= src
->Dimension
;
632 reg
.DimIndirect
= src
->DimIndirect
;
636 /* If the register is 2-dimensional (e.g. an array of vertices
637 * in a primitive), calculate the base address of the vertex. */
638 if (reg
.Register
.Dimension
) {
641 if (reg
.Dimension
.Indirect
)
642 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
643 reg
.Dimension
.Index
);
645 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
647 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
648 LLVMBuildMul(gallivm
->builder
, index
,
649 vertex_dw_stride
, ""), "");
652 /* Get information about the register. */
653 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
654 name
= info
->input_semantic_name
;
655 index
= info
->input_semantic_index
;
656 array_first
= info
->input_array_first
;
657 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
658 name
= info
->output_semantic_name
;
659 index
= info
->output_semantic_index
;
660 array_first
= info
->output_array_first
;
666 if (reg
.Register
.Indirect
) {
667 /* Add the relative address of the element. */
668 LLVMValueRef ind_index
;
670 if (reg
.Indirect
.ArrayID
)
671 first
= array_first
[reg
.Indirect
.ArrayID
];
673 first
= reg
.Register
.Index
;
675 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
676 reg
.Register
.Index
- first
);
678 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
679 LLVMBuildMul(gallivm
->builder
, ind_index
,
680 lp_build_const_int32(gallivm
, 4), ""), "");
682 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
684 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
685 index
[reg
.Register
.Index
]);
688 /* Add the base address of the element. */
689 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
690 lp_build_const_int32(gallivm
, param
* 4), "");
693 /* The offchip buffer layout for TCS->TES is
695 * - attribute 0 of patch 0 vertex 0
696 * - attribute 0 of patch 0 vertex 1
697 * - attribute 0 of patch 0 vertex 2
699 * - attribute 0 of patch 1 vertex 0
700 * - attribute 0 of patch 1 vertex 1
702 * - attribute 1 of patch 0 vertex 0
703 * - attribute 1 of patch 0 vertex 1
705 * - per patch attribute 0 of patch 0
706 * - per patch attribute 0 of patch 1
709 * Note that every attribute has 4 components.
711 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
712 LLVMValueRef rel_patch_id
,
713 LLVMValueRef vertex_index
,
714 LLVMValueRef param_index
)
716 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
717 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
718 LLVMValueRef param_stride
, constant16
;
720 vertices_per_patch
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 6);
721 num_patches
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 0, 9);
722 total_vertices
= LLVMBuildMul(gallivm
->builder
, vertices_per_patch
,
725 constant16
= lp_build_const_int32(gallivm
, 16);
727 base_addr
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
728 vertices_per_patch
, "");
730 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
733 param_stride
= total_vertices
;
735 base_addr
= rel_patch_id
;
736 param_stride
= num_patches
;
739 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
740 LLVMBuildMul(gallivm
->builder
, param_index
,
741 param_stride
, ""), "");
743 base_addr
= LLVMBuildMul(gallivm
->builder
, base_addr
, constant16
, "");
746 LLVMValueRef patch_data_offset
=
747 unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 16, 16);
749 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
750 patch_data_offset
, "");
755 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
756 struct si_shader_context
*ctx
,
757 const struct tgsi_full_dst_register
*dst
,
758 const struct tgsi_full_src_register
*src
)
760 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
761 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
762 ubyte
*name
, *index
, *array_first
;
763 struct tgsi_full_src_register reg
;
764 LLVMValueRef vertex_index
= NULL
;
765 LLVMValueRef param_index
= NULL
;
766 unsigned param_index_base
, param_base
;
768 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
770 if (reg
.Register
.Dimension
) {
772 if (reg
.Dimension
.Indirect
)
773 vertex_index
= get_indirect_index(ctx
, ®
.DimIndirect
,
774 reg
.Dimension
.Index
);
776 vertex_index
= lp_build_const_int32(gallivm
,
777 reg
.Dimension
.Index
);
780 /* Get information about the register. */
781 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
782 name
= info
->input_semantic_name
;
783 index
= info
->input_semantic_index
;
784 array_first
= info
->input_array_first
;
785 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
786 name
= info
->output_semantic_name
;
787 index
= info
->output_semantic_index
;
788 array_first
= info
->output_array_first
;
794 if (reg
.Register
.Indirect
) {
795 if (reg
.Indirect
.ArrayID
)
796 param_base
= array_first
[reg
.Indirect
.ArrayID
];
798 param_base
= reg
.Register
.Index
;
800 param_index
= get_indirect_index(ctx
, ®
.Indirect
,
801 reg
.Register
.Index
- param_base
);
804 param_base
= reg
.Register
.Index
;
805 param_index
= lp_build_const_int32(gallivm
, 0);
808 param_index_base
= si_shader_io_get_unique_index(name
[param_base
],
811 param_index
= LLVMBuildAdd(gallivm
->builder
, param_index
,
812 lp_build_const_int32(gallivm
, param_index_base
),
815 return get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
),
816 vertex_index
, param_index
);
819 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
820 enum tgsi_opcode_type type
, unsigned swizzle
,
821 LLVMValueRef buffer
, LLVMValueRef offset
,
822 LLVMValueRef base
, bool readonly_memory
)
824 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
825 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
826 LLVMValueRef value
, value2
;
827 LLVMTypeRef llvm_type
= tgsi2llvmtype(bld_base
, type
);
828 LLVMTypeRef vec_type
= LLVMVectorType(llvm_type
, 4);
831 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
832 0, 1, 0, readonly_memory
);
834 return LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
837 if (!tgsi_type_is_64bit(type
)) {
838 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 4, NULL
, base
, offset
,
839 0, 1, 0, readonly_memory
);
841 value
= LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
842 return LLVMBuildExtractElement(gallivm
->builder
, value
,
843 lp_build_const_int32(gallivm
, swizzle
), "");
846 value
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
847 swizzle
* 4, 1, 0, readonly_memory
);
849 value2
= ac_build_buffer_load(&ctx
->ac
, buffer
, 1, NULL
, base
, offset
,
850 swizzle
* 4 + 4, 1, 0, readonly_memory
);
852 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
858 * \param type output value type
859 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
860 * \param dw_addr address in dwords
862 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
863 enum tgsi_opcode_type type
, unsigned swizzle
,
864 LLVMValueRef dw_addr
)
866 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
867 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
871 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
873 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
874 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
876 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
880 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
881 lp_build_const_int32(gallivm
, swizzle
));
883 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
884 if (tgsi_type_is_64bit(type
)) {
886 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
887 lp_build_const_int32(gallivm
, 1));
888 value2
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
889 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
892 return LLVMBuildBitCast(gallivm
->builder
, value
,
893 tgsi2llvmtype(bld_base
, type
), "");
899 * \param swizzle offset (typically 0..3)
900 * \param dw_addr address in dwords
901 * \param value value to store
903 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
904 unsigned swizzle
, LLVMValueRef dw_addr
,
907 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
908 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
910 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
911 lp_build_const_int32(gallivm
, swizzle
));
913 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
914 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
918 static LLVMValueRef
fetch_input_tcs(
919 struct lp_build_tgsi_context
*bld_base
,
920 const struct tgsi_full_src_register
*reg
,
921 enum tgsi_opcode_type type
, unsigned swizzle
)
923 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
924 LLVMValueRef dw_addr
, stride
;
926 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
927 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
928 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
930 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
933 static LLVMValueRef
fetch_output_tcs(
934 struct lp_build_tgsi_context
*bld_base
,
935 const struct tgsi_full_src_register
*reg
,
936 enum tgsi_opcode_type type
, unsigned swizzle
)
938 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
939 LLVMValueRef dw_addr
, stride
;
941 if (reg
->Register
.Dimension
) {
942 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
943 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
944 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
946 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
947 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
950 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
953 static LLVMValueRef
fetch_input_tes(
954 struct lp_build_tgsi_context
*bld_base
,
955 const struct tgsi_full_src_register
*reg
,
956 enum tgsi_opcode_type type
, unsigned swizzle
)
958 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
959 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
960 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
962 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
963 SI_PARAM_RW_BUFFERS
);
964 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
965 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
967 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
968 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
970 return buffer_load(bld_base
, type
, swizzle
, buffer
, base
, addr
, true);
973 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
974 const struct tgsi_full_instruction
*inst
,
975 const struct tgsi_opcode_info
*info
,
978 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
979 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
980 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
981 const struct tgsi_shader_info
*sh_info
= &ctx
->shader
->selector
->info
;
983 LLVMValueRef dw_addr
, stride
;
984 LLVMValueRef rw_buffers
, buffer
, base
, buf_addr
;
985 LLVMValueRef values
[4];
987 bool is_tess_factor
= false;
989 /* Only handle per-patch and per-vertex outputs here.
990 * Vectors will be lowered to scalars and this function will be called again.
992 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
993 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
994 si_llvm_emit_store(bld_base
, inst
, info
, dst
);
998 if (reg
->Register
.Dimension
) {
999 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
1000 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1001 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1002 skip_lds_store
= !sh_info
->reads_pervertex_outputs
;
1004 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1005 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1006 skip_lds_store
= !sh_info
->reads_perpatch_outputs
;
1008 if (!reg
->Register
.Indirect
) {
1009 int name
= sh_info
->output_semantic_name
[reg
->Register
.Index
];
1011 /* Always write tess factors into LDS for the TCS epilog. */
1012 if (name
== TGSI_SEMANTIC_TESSINNER
||
1013 name
== TGSI_SEMANTIC_TESSOUTER
) {
1014 skip_lds_store
= false;
1015 is_tess_factor
= true;
1020 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1021 SI_PARAM_RW_BUFFERS
);
1022 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
1023 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1025 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1026 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1029 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
1030 LLVMValueRef value
= dst
[chan_index
];
1032 if (inst
->Instruction
.Saturate
)
1033 value
= ac_build_clamp(&ctx
->ac
, value
);
1035 /* Skip LDS stores if there is no LDS read of this output. */
1036 if (!skip_lds_store
)
1037 lds_store(bld_base
, chan_index
, dw_addr
, value
);
1039 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
1040 values
[chan_index
] = value
;
1042 if (inst
->Dst
[0].Register
.WriteMask
!= 0xF && !is_tess_factor
) {
1043 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 1,
1045 4 * chan_index
, 1, 0, true, false);
1049 if (inst
->Dst
[0].Register
.WriteMask
== 0xF && !is_tess_factor
) {
1050 LLVMValueRef value
= lp_build_gather_values(bld_base
->base
.gallivm
,
1052 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buf_addr
,
1053 base
, 0, 1, 0, true, false);
1057 static LLVMValueRef
fetch_input_gs(
1058 struct lp_build_tgsi_context
*bld_base
,
1059 const struct tgsi_full_src_register
*reg
,
1060 enum tgsi_opcode_type type
,
1063 struct lp_build_context
*base
= &bld_base
->base
;
1064 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1065 struct si_shader
*shader
= ctx
->shader
;
1066 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
1067 struct gallivm_state
*gallivm
= base
->gallivm
;
1068 LLVMValueRef vtx_offset
, soffset
;
1069 unsigned vtx_offset_param
;
1070 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1071 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1072 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
1076 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1077 return get_primitive_id(bld_base
, swizzle
);
1079 if (!reg
->Register
.Dimension
)
1082 if (swizzle
== ~0) {
1083 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1085 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1086 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
1088 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
1092 /* Get the vertex offset parameter */
1093 vtx_offset_param
= reg
->Dimension
.Index
;
1094 if (vtx_offset_param
< 2) {
1095 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
1097 assert(vtx_offset_param
< 6);
1098 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
1100 vtx_offset
= lp_build_mul_imm(uint
,
1101 LLVMGetParam(ctx
->main_fn
,
1105 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
1106 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
) * 256, 0);
1108 value
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1, uint
->zero
,
1109 vtx_offset
, soffset
, 0, 1, 0, true);
1110 if (tgsi_type_is_64bit(type
)) {
1111 LLVMValueRef value2
;
1112 soffset
= LLVMConstInt(ctx
->i32
, (param
* 4 + swizzle
+ 1) * 256, 0);
1114 value2
= ac_build_buffer_load(&ctx
->ac
, ctx
->esgs_ring
, 1,
1115 uint
->zero
, vtx_offset
, soffset
,
1117 return si_llvm_emit_fetch_64bit(bld_base
, type
,
1120 return LLVMBuildBitCast(gallivm
->builder
,
1122 tgsi2llvmtype(bld_base
, type
), "");
1125 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1127 switch (interpolate
) {
1128 case TGSI_INTERPOLATE_CONSTANT
:
1131 case TGSI_INTERPOLATE_LINEAR
:
1132 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1133 return SI_PARAM_LINEAR_SAMPLE
;
1134 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1135 return SI_PARAM_LINEAR_CENTROID
;
1137 return SI_PARAM_LINEAR_CENTER
;
1139 case TGSI_INTERPOLATE_COLOR
:
1140 case TGSI_INTERPOLATE_PERSPECTIVE
:
1141 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1142 return SI_PARAM_PERSP_SAMPLE
;
1143 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1144 return SI_PARAM_PERSP_CENTROID
;
1146 return SI_PARAM_PERSP_CENTER
;
1149 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1155 * Interpolate a fragment shader input.
1157 * @param ctx context
1158 * @param input_index index of the input in hardware
1159 * @param semantic_name TGSI_SEMANTIC_*
1160 * @param semantic_index semantic index
1161 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1162 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1163 * @param interp_param interpolation weights (i,j)
1164 * @param prim_mask SI_PARAM_PRIM_MASK
1165 * @param face SI_PARAM_FRONT_FACE
1166 * @param result the return value (4 components)
1168 static void interp_fs_input(struct si_shader_context
*ctx
,
1169 unsigned input_index
,
1170 unsigned semantic_name
,
1171 unsigned semantic_index
,
1172 unsigned num_interp_inputs
,
1173 unsigned colors_read_mask
,
1174 LLVMValueRef interp_param
,
1175 LLVMValueRef prim_mask
,
1177 LLVMValueRef result
[4])
1179 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1180 struct lp_build_context
*base
= &bld_base
->base
;
1181 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1182 struct gallivm_state
*gallivm
= base
->gallivm
;
1183 LLVMValueRef attr_number
;
1188 /* fs.constant returns the param from the middle vertex, so it's not
1189 * really useful for flat shading. It's meant to be used for custom
1190 * interpolation (but the intrinsic can't fetch from the other two
1193 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1194 * to do the right thing. The only reason we use fs.constant is that
1195 * fs.interp cannot be used on integers, because they can be equal
1198 * When interp is false we will use fs.constant or for newer llvm,
1199 * amdgcn.interp.mov.
1201 bool interp
= interp_param
!= NULL
;
1203 attr_number
= lp_build_const_int32(gallivm
, input_index
);
1206 interp_param
= LLVMBuildBitCast(gallivm
->builder
, interp_param
,
1207 LLVMVectorType(ctx
->f32
, 2), "");
1209 i
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1211 j
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1215 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1216 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1217 LLVMValueRef is_face_positive
;
1218 LLVMValueRef back_attr_number
;
1220 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1221 * otherwise it's at offset "num_inputs".
1223 unsigned back_attr_offset
= num_interp_inputs
;
1224 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1225 back_attr_offset
+= 1;
1227 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
1229 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1230 face
, uint
->zero
, "");
1232 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1233 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1234 LLVMValueRef front
, back
;
1237 front
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1238 attr_number
, prim_mask
,
1240 back
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1241 back_attr_number
, prim_mask
,
1244 front
= ac_build_fs_interp_mov(&ctx
->ac
,
1245 lp_build_const_int32(gallivm
, 2), /* P0 */
1246 llvm_chan
, attr_number
, prim_mask
);
1247 back
= ac_build_fs_interp_mov(&ctx
->ac
,
1248 lp_build_const_int32(gallivm
, 2), /* P0 */
1249 llvm_chan
, back_attr_number
, prim_mask
);
1252 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
1258 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1260 result
[0] = ac_build_fs_interp(&ctx
->ac
, uint
->zero
,
1261 attr_number
, prim_mask
, i
, j
);
1263 result
[0] = ac_build_fs_interp_mov(&ctx
->ac
, uint
->zero
,
1264 lp_build_const_int32(gallivm
, 2), /* P0 */
1265 attr_number
, prim_mask
);
1268 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1269 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1271 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1272 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1275 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
1276 llvm_chan
, attr_number
, prim_mask
, i
, j
);
1278 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
1279 lp_build_const_int32(gallivm
, 2), /* P0 */
1280 llvm_chan
, attr_number
, prim_mask
);
1286 static void declare_input_fs(
1287 struct si_shader_context
*ctx
,
1288 unsigned input_index
,
1289 const struct tgsi_full_declaration
*decl
,
1290 LLVMValueRef out
[4])
1292 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
1293 struct si_shader
*shader
= ctx
->shader
;
1294 LLVMValueRef main_fn
= ctx
->main_fn
;
1295 LLVMValueRef interp_param
= NULL
;
1296 int interp_param_idx
;
1298 /* Get colors from input VGPRs (set by the prolog). */
1299 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1300 unsigned i
= decl
->Semantic
.Index
;
1301 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1302 unsigned mask
= colors_read
>> (i
* 4);
1303 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1304 (i
? util_bitcount(colors_read
& 0xf) : 0);
1306 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1307 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1308 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1309 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1313 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1314 decl
->Interp
.Location
);
1315 if (interp_param_idx
== -1)
1317 else if (interp_param_idx
) {
1318 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1321 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
1322 decl
->Interp
.Interpolate
== TGSI_INTERPOLATE_COLOR
&&
1323 ctx
->shader
->key
.part
.ps
.prolog
.flatshade_colors
)
1324 interp_param
= NULL
; /* load the constant color */
1326 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1327 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1328 shader
->selector
->info
.colors_read
, interp_param
,
1329 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1330 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1334 static LLVMValueRef
get_sample_id(struct si_shader_context
*ctx
)
1336 return unpack_param(ctx
, SI_PARAM_ANCILLARY
, 8, 4);
1341 * Load a dword from a constant buffer.
1343 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1344 LLVMValueRef resource
,
1345 LLVMValueRef offset
)
1347 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
1348 LLVMValueRef args
[2] = {resource
, offset
};
1350 return lp_build_intrinsic(builder
, "llvm.SI.load.const", ctx
->f32
, args
, 2,
1351 LP_FUNC_ATTR_READNONE
|
1352 LP_FUNC_ATTR_LEGACY
);
1355 static LLVMValueRef
load_sample_position(struct si_shader_context
*ctx
, LLVMValueRef sample_id
)
1357 struct lp_build_context
*uint_bld
= &ctx
->bld_base
.uint_bld
;
1358 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1359 LLVMBuilderRef builder
= gallivm
->builder
;
1360 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1361 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_PS_CONST_SAMPLE_POSITIONS
);
1362 LLVMValueRef resource
= ac_build_indexed_load_const(&ctx
->ac
, desc
, buf_index
);
1364 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1365 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1366 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1368 LLVMValueRef pos
[4] = {
1369 buffer_load_const(ctx
, resource
, offset0
),
1370 buffer_load_const(ctx
, resource
, offset1
),
1371 lp_build_const_float(gallivm
, 0),
1372 lp_build_const_float(gallivm
, 0)
1375 return lp_build_gather_values(gallivm
, pos
, 4);
1378 static void declare_system_value(struct si_shader_context
*ctx
,
1380 const struct tgsi_full_declaration
*decl
)
1382 struct lp_build_context
*bld
= &ctx
->bld_base
.base
;
1383 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1384 LLVMValueRef value
= 0;
1386 switch (decl
->Semantic
.Name
) {
1387 case TGSI_SEMANTIC_INSTANCEID
:
1388 value
= LLVMGetParam(ctx
->main_fn
,
1389 ctx
->param_instance_id
);
1392 case TGSI_SEMANTIC_VERTEXID
:
1393 value
= LLVMBuildAdd(gallivm
->builder
,
1394 LLVMGetParam(ctx
->main_fn
,
1395 ctx
->param_vertex_id
),
1396 LLVMGetParam(ctx
->main_fn
,
1397 SI_PARAM_BASE_VERTEX
), "");
1400 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1401 value
= LLVMGetParam(ctx
->main_fn
,
1402 ctx
->param_vertex_id
);
1405 case TGSI_SEMANTIC_BASEVERTEX
:
1406 value
= LLVMGetParam(ctx
->main_fn
,
1407 SI_PARAM_BASE_VERTEX
);
1410 case TGSI_SEMANTIC_BASEINSTANCE
:
1411 value
= LLVMGetParam(ctx
->main_fn
,
1412 SI_PARAM_START_INSTANCE
);
1415 case TGSI_SEMANTIC_DRAWID
:
1416 value
= LLVMGetParam(ctx
->main_fn
,
1420 case TGSI_SEMANTIC_INVOCATIONID
:
1421 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1422 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1423 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
1424 value
= LLVMGetParam(ctx
->main_fn
,
1425 SI_PARAM_GS_INSTANCE_ID
);
1427 assert(!"INVOCATIONID not implemented");
1430 case TGSI_SEMANTIC_POSITION
:
1432 LLVMValueRef pos
[4] = {
1433 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1434 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1435 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1436 lp_build_emit_llvm_unary(&ctx
->bld_base
, TGSI_OPCODE_RCP
,
1437 LLVMGetParam(ctx
->main_fn
,
1438 SI_PARAM_POS_W_FLOAT
)),
1440 value
= lp_build_gather_values(gallivm
, pos
, 4);
1444 case TGSI_SEMANTIC_FACE
:
1445 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_FRONT_FACE
);
1448 case TGSI_SEMANTIC_SAMPLEID
:
1449 value
= get_sample_id(ctx
);
1452 case TGSI_SEMANTIC_SAMPLEPOS
: {
1453 LLVMValueRef pos
[4] = {
1454 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1455 LLVMGetParam(ctx
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1456 lp_build_const_float(gallivm
, 0),
1457 lp_build_const_float(gallivm
, 0)
1459 pos
[0] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
1460 TGSI_OPCODE_FRC
, pos
[0]);
1461 pos
[1] = lp_build_emit_llvm_unary(&ctx
->bld_base
,
1462 TGSI_OPCODE_FRC
, pos
[1]);
1463 value
= lp_build_gather_values(gallivm
, pos
, 4);
1467 case TGSI_SEMANTIC_SAMPLEMASK
:
1468 /* This can only occur with the OpenGL Core profile, which
1469 * doesn't support smoothing.
1471 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1474 case TGSI_SEMANTIC_TESSCOORD
:
1476 LLVMValueRef coord
[4] = {
1477 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_u
),
1478 LLVMGetParam(ctx
->main_fn
, ctx
->param_tes_v
),
1483 /* For triangles, the vector should be (u, v, 1-u-v). */
1484 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1485 PIPE_PRIM_TRIANGLES
)
1486 coord
[2] = lp_build_sub(bld
, bld
->one
,
1487 lp_build_add(bld
, coord
[0], coord
[1]));
1489 value
= lp_build_gather_values(gallivm
, coord
, 4);
1493 case TGSI_SEMANTIC_VERTICESIN
:
1494 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1495 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1496 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
1497 value
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 7);
1499 assert(!"invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
1502 case TGSI_SEMANTIC_TESSINNER
:
1503 case TGSI_SEMANTIC_TESSOUTER
:
1505 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
1506 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1508 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1509 SI_PARAM_RW_BUFFERS
);
1510 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
1511 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1513 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1514 addr
= get_tcs_tes_buffer_address(ctx
, get_rel_patch_id(ctx
), NULL
,
1515 lp_build_const_int32(gallivm
, param
));
1517 value
= buffer_load(&ctx
->bld_base
, TGSI_TYPE_FLOAT
,
1518 ~0, buffer
, base
, addr
, true);
1523 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
1524 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
1526 LLVMValueRef buf
, slot
, val
[4];
1529 slot
= lp_build_const_int32(gallivm
, SI_HS_CONST_DEFAULT_TESS_LEVELS
);
1530 buf
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1531 buf
= ac_build_indexed_load_const(&ctx
->ac
, buf
, slot
);
1532 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
1534 for (i
= 0; i
< 4; i
++)
1535 val
[i
] = buffer_load_const(ctx
, buf
,
1536 lp_build_const_int32(gallivm
, (offset
+ i
) * 4));
1537 value
= lp_build_gather_values(gallivm
, val
, 4);
1541 case TGSI_SEMANTIC_PRIMID
:
1542 value
= get_primitive_id(&ctx
->bld_base
, 0);
1545 case TGSI_SEMANTIC_GRID_SIZE
:
1546 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_GRID_SIZE
);
1549 case TGSI_SEMANTIC_BLOCK_SIZE
:
1551 LLVMValueRef values
[3];
1553 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1555 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1556 unsigned sizes
[3] = {
1557 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1558 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1559 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1562 for (i
= 0; i
< 3; ++i
)
1563 values
[i
] = lp_build_const_int32(gallivm
, sizes
[i
]);
1565 value
= lp_build_gather_values(gallivm
, values
, 3);
1567 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_BLOCK_SIZE
);
1572 case TGSI_SEMANTIC_BLOCK_ID
:
1573 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_BLOCK_ID
);
1576 case TGSI_SEMANTIC_THREAD_ID
:
1577 value
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_THREAD_ID
);
1580 case TGSI_SEMANTIC_HELPER_INVOCATION
:
1581 if (HAVE_LLVM
>= 0x0309) {
1582 value
= lp_build_intrinsic(gallivm
->builder
,
1583 "llvm.amdgcn.ps.live",
1585 LP_FUNC_ATTR_READNONE
);
1586 value
= LLVMBuildNot(gallivm
->builder
, value
, "");
1587 value
= LLVMBuildSExt(gallivm
->builder
, value
, ctx
->i32
, "");
1589 assert(!"TGSI_SEMANTIC_HELPER_INVOCATION unsupported");
1595 assert(!"unknown system value");
1599 ctx
->system_values
[index
] = value
;
1602 static void declare_compute_memory(struct si_shader_context
*ctx
,
1603 const struct tgsi_full_declaration
*decl
)
1605 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
1606 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1608 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, LOCAL_ADDR_SPACE
);
1611 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
1612 assert(decl
->Range
.First
== decl
->Range
.Last
);
1613 assert(!ctx
->shared_memory
);
1615 var
= LLVMAddGlobalInAddressSpace(gallivm
->module
,
1616 LLVMArrayType(ctx
->i8
, sel
->local_size
),
1619 LLVMSetAlignment(var
, 4);
1621 ctx
->shared_memory
= LLVMBuildBitCast(gallivm
->builder
, var
, i8p
, "");
1624 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
1626 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
1627 SI_PARAM_CONST_BUFFERS
);
1629 return ac_build_indexed_load_const(&ctx
->ac
, list_ptr
,
1630 LLVMConstInt(ctx
->i32
, i
, 0));
1633 static LLVMValueRef
fetch_constant(
1634 struct lp_build_tgsi_context
*bld_base
,
1635 const struct tgsi_full_src_register
*reg
,
1636 enum tgsi_opcode_type type
,
1639 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1640 struct lp_build_context
*base
= &bld_base
->base
;
1641 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1644 LLVMValueRef addr
, bufp
;
1645 LLVMValueRef result
;
1647 if (swizzle
== LP_CHAN_ALL
) {
1649 LLVMValueRef values
[4];
1650 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1651 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1653 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1656 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1657 idx
= reg
->Register
.Index
* 4 + swizzle
;
1659 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1660 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_CONST_BUFFERS
);
1662 index
= get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
1663 reg
->Dimension
.Index
,
1664 SI_NUM_CONST_BUFFERS
);
1665 bufp
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, index
);
1667 bufp
= load_const_buffer_desc(ctx
, buf
);
1669 if (reg
->Register
.Indirect
) {
1670 addr
= ctx
->addrs
[ireg
->Index
][ireg
->Swizzle
];
1671 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1672 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1673 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1674 lp_build_const_int32(base
->gallivm
, idx
* 4));
1676 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
1679 result
= buffer_load_const(ctx
, bufp
, addr
);
1681 if (!tgsi_type_is_64bit(type
))
1682 result
= bitcast(bld_base
, type
, result
);
1684 LLVMValueRef addr2
, result2
;
1686 addr2
= lp_build_add(&bld_base
->uint_bld
, addr
,
1687 LLVMConstInt(ctx
->i32
, 4, 0));
1688 result2
= buffer_load_const(ctx
, bufp
, addr2
);
1690 result
= si_llvm_emit_fetch_64bit(bld_base
, type
,
1696 /* Upper 16 bits must be zero. */
1697 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1698 LLVMValueRef val
[2])
1700 return LLVMBuildOr(gallivm
->builder
, val
[0],
1701 LLVMBuildShl(gallivm
->builder
, val
[1],
1702 lp_build_const_int32(gallivm
, 16),
1706 /* Upper 16 bits are ignored and will be dropped. */
1707 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1708 LLVMValueRef val
[2])
1710 LLVMValueRef v
[2] = {
1711 LLVMBuildAnd(gallivm
->builder
, val
[0],
1712 lp_build_const_int32(gallivm
, 0xffff), ""),
1715 return si_llvm_pack_two_int16(gallivm
, v
);
1718 /* Initialize arguments for the shader export intrinsic */
1719 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1720 LLVMValueRef
*values
,
1722 struct ac_export_args
*args
)
1724 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1725 struct lp_build_context
*base
= &bld_base
->base
;
1726 struct gallivm_state
*gallivm
= base
->gallivm
;
1727 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1728 LLVMValueRef val
[4];
1729 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1731 bool is_int8
, is_int10
;
1733 /* Default is 0xf. Adjusted below depending on the format. */
1734 args
->enabled_channels
= 0xf; /* writemask */
1736 /* Specify whether the EXEC mask represents the valid mask */
1737 args
->valid_mask
= 0;
1739 /* Specify whether this is the last export */
1742 /* Specify the target we are exporting */
1743 args
->target
= target
;
1745 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
1746 const struct si_shader_key
*key
= &ctx
->shader
->key
;
1747 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
1748 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1750 assert(cbuf
>= 0 && cbuf
< 8);
1751 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1752 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1753 is_int10
= (key
->part
.ps
.epilog
.color_is_int10
>> cbuf
) & 0x1;
1756 args
->compr
= false;
1757 args
->out
[0] = base
->undef
;
1758 args
->out
[1] = base
->undef
;
1759 args
->out
[2] = base
->undef
;
1760 args
->out
[3] = base
->undef
;
1762 switch (spi_shader_col_format
) {
1763 case V_028714_SPI_SHADER_ZERO
:
1764 args
->enabled_channels
= 0; /* writemask */
1765 args
->target
= V_008DFC_SQ_EXP_NULL
;
1768 case V_028714_SPI_SHADER_32_R
:
1769 args
->enabled_channels
= 1; /* writemask */
1770 args
->out
[0] = values
[0];
1773 case V_028714_SPI_SHADER_32_GR
:
1774 args
->enabled_channels
= 0x3; /* writemask */
1775 args
->out
[0] = values
[0];
1776 args
->out
[1] = values
[1];
1779 case V_028714_SPI_SHADER_32_AR
:
1780 args
->enabled_channels
= 0x9; /* writemask */
1781 args
->out
[0] = values
[0];
1782 args
->out
[3] = values
[3];
1785 case V_028714_SPI_SHADER_FP16_ABGR
:
1786 args
->compr
= 1; /* COMPR flag */
1788 for (chan
= 0; chan
< 2; chan
++) {
1789 LLVMValueRef pack_args
[2] = {
1791 values
[2 * chan
+ 1]
1793 LLVMValueRef packed
;
1795 packed
= ac_build_cvt_pkrtz_f16(&ctx
->ac
, pack_args
);
1797 LLVMBuildBitCast(base
->gallivm
->builder
,
1798 packed
, ctx
->f32
, "");
1802 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1803 for (chan
= 0; chan
< 4; chan
++) {
1804 val
[chan
] = ac_build_clamp(&ctx
->ac
, values
[chan
]);
1805 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1806 lp_build_const_float(gallivm
, 65535), "");
1807 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1808 lp_build_const_float(gallivm
, 0.5), "");
1809 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1813 args
->compr
= 1; /* COMPR flag */
1814 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1815 si_llvm_pack_two_int16(gallivm
, val
));
1816 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1817 si_llvm_pack_two_int16(gallivm
, val
+2));
1820 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1821 for (chan
= 0; chan
< 4; chan
++) {
1822 /* Clamp between [-1, 1]. */
1823 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1825 lp_build_const_float(gallivm
, 1));
1826 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1828 lp_build_const_float(gallivm
, -1));
1829 /* Convert to a signed integer in [-32767, 32767]. */
1830 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1831 lp_build_const_float(gallivm
, 32767), "");
1832 /* If positive, add 0.5, else add -0.5. */
1833 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1834 LLVMBuildSelect(builder
,
1835 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1836 val
[chan
], base
->zero
, ""),
1837 lp_build_const_float(gallivm
, 0.5),
1838 lp_build_const_float(gallivm
, -0.5), ""), "");
1839 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1842 args
->compr
= 1; /* COMPR flag */
1843 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1844 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1845 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1846 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1849 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1850 LLVMValueRef max_rgb
= lp_build_const_int32(gallivm
,
1851 is_int8
? 255 : is_int10
? 1023 : 65535);
1852 LLVMValueRef max_alpha
=
1853 !is_int10
? max_rgb
: lp_build_const_int32(gallivm
, 3);
1856 for (chan
= 0; chan
< 4; chan
++) {
1857 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1858 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1860 chan
== 3 ? max_alpha
: max_rgb
);
1863 args
->compr
= 1; /* COMPR flag */
1864 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1865 si_llvm_pack_two_int16(gallivm
, val
));
1866 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1867 si_llvm_pack_two_int16(gallivm
, val
+2));
1871 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1872 LLVMValueRef max_rgb
= lp_build_const_int32(gallivm
,
1873 is_int8
? 127 : is_int10
? 511 : 32767);
1874 LLVMValueRef min_rgb
= lp_build_const_int32(gallivm
,
1875 is_int8
? -128 : is_int10
? -512 : -32768);
1876 LLVMValueRef max_alpha
=
1877 !is_int10
? max_rgb
: lp_build_const_int32(gallivm
, 1);
1878 LLVMValueRef min_alpha
=
1879 !is_int10
? min_rgb
: lp_build_const_int32(gallivm
, -2);
1882 for (chan
= 0; chan
< 4; chan
++) {
1883 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1884 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1886 val
[chan
], chan
== 3 ? max_alpha
: max_rgb
);
1887 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1889 val
[chan
], chan
== 3 ? min_alpha
: min_rgb
);
1892 args
->compr
= 1; /* COMPR flag */
1893 args
->out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1894 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1895 args
->out
[1] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1896 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1900 case V_028714_SPI_SHADER_32_ABGR
:
1901 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
1906 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1909 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1910 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1912 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
1913 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
1914 SI_PARAM_ALPHA_REF
);
1916 LLVMValueRef alpha_pass
=
1917 lp_build_cmp(&bld_base
->base
,
1918 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
,
1921 lp_build_select(&bld_base
->base
,
1923 lp_build_const_float(gallivm
, 1.0f
),
1924 lp_build_const_float(gallivm
, -1.0f
));
1926 ac_build_kill(&ctx
->ac
, arg
);
1928 ac_build_kill(&ctx
->ac
, NULL
);
1932 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1934 unsigned samplemask_param
)
1936 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1937 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1938 LLVMValueRef coverage
;
1940 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1941 coverage
= LLVMGetParam(ctx
->main_fn
,
1943 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1945 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1947 &coverage
, 1, LP_FUNC_ATTR_READNONE
);
1949 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1952 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1953 lp_build_const_float(gallivm
,
1954 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1956 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1959 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
1960 struct ac_export_args
*pos
, LLVMValueRef
*out_elts
)
1962 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1963 struct lp_build_context
*base
= &bld_base
->base
;
1966 unsigned const_chan
;
1967 LLVMValueRef base_elt
;
1968 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1969 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
,
1970 SI_VS_CONST_CLIP_PLANES
);
1971 LLVMValueRef const_resource
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, constbuf_index
);
1973 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1974 struct ac_export_args
*args
= &pos
[2 + reg_index
];
1979 args
->out
[3] = lp_build_const_float(base
->gallivm
, 0.0f
);
1981 /* Compute dot products of position and user clip plane vectors */
1982 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1983 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1985 LLVMConstInt(ctx
->i32
, ((reg_index
* 4 + chan
) * 4 +
1986 const_chan
) * 4, 0);
1987 base_elt
= buffer_load_const(ctx
, const_resource
,
1990 lp_build_add(base
, args
->out
[chan
],
1991 lp_build_mul(base
, base_elt
,
1992 out_elts
[const_chan
]));
1996 args
->enabled_channels
= 0xf;
1997 args
->valid_mask
= 0;
1999 args
->target
= V_008DFC_SQ_EXP_POS
+ 2 + reg_index
;
2004 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2008 if (so
->num_outputs
)
2009 fprintf(stderr
, "STREAMOUT\n");
2011 for (i
= 0; i
< so
->num_outputs
; i
++) {
2012 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2013 so
->output
[i
].start_component
;
2014 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2015 i
, so
->output
[i
].output_buffer
,
2016 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2017 so
->output
[i
].register_index
,
2018 mask
& 1 ? "x" : "",
2019 mask
& 2 ? "y" : "",
2020 mask
& 4 ? "z" : "",
2021 mask
& 8 ? "w" : "");
2025 static void emit_streamout_output(struct si_shader_context
*ctx
,
2026 LLVMValueRef
const *so_buffers
,
2027 LLVMValueRef
const *so_write_offsets
,
2028 struct pipe_stream_output
*stream_out
,
2029 struct si_shader_output_values
*shader_out
)
2031 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2032 LLVMBuilderRef builder
= gallivm
->builder
;
2033 unsigned buf_idx
= stream_out
->output_buffer
;
2034 unsigned start
= stream_out
->start_component
;
2035 unsigned num_comps
= stream_out
->num_components
;
2036 LLVMValueRef out
[4];
2038 assert(num_comps
&& num_comps
<= 4);
2039 if (!num_comps
|| num_comps
> 4)
2042 /* Load the output as int. */
2043 for (int j
= 0; j
< num_comps
; j
++) {
2044 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2046 out
[j
] = LLVMBuildBitCast(builder
,
2047 shader_out
->values
[start
+ j
],
2051 /* Pack the output. */
2052 LLVMValueRef vdata
= NULL
;
2054 switch (num_comps
) {
2055 case 1: /* as i32 */
2058 case 2: /* as v2i32 */
2059 case 3: /* as v4i32 (aligned to 4) */
2060 case 4: /* as v4i32 */
2061 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
2062 for (int j
= 0; j
< num_comps
; j
++) {
2063 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
2064 LLVMConstInt(ctx
->i32
, j
, 0), "");
2069 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf_idx
],
2071 so_write_offsets
[buf_idx
],
2072 LLVMConstInt(ctx
->i32
, 0, 0),
2073 stream_out
->dst_offset
* 4, 1, 1, true, false);
2077 * Write streamout data to buffers for vertex stream @p stream (different
2078 * vertex streams can occur for GS copy shaders).
2080 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2081 struct si_shader_output_values
*outputs
,
2082 unsigned noutput
, unsigned stream
)
2084 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2085 struct pipe_stream_output_info
*so
= &sel
->so
;
2086 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2087 LLVMBuilderRef builder
= gallivm
->builder
;
2089 struct lp_build_if_state if_ctx
;
2091 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2092 LLVMValueRef so_vtx_count
=
2093 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2095 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2097 /* can_emit = tid < so_vtx_count; */
2098 LLVMValueRef can_emit
=
2099 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2101 /* Emit the streamout code conditionally. This actually avoids
2102 * out-of-bounds buffer access. The hw tells us via the SGPR
2103 * (so_vtx_count) which threads are allowed to emit streamout data. */
2104 lp_build_if(&if_ctx
, gallivm
, can_emit
);
2106 /* The buffer offset is computed as follows:
2107 * ByteOffset = streamout_offset[buffer_id]*4 +
2108 * (streamout_write_index + thread_id)*stride[buffer_id] +
2112 LLVMValueRef so_write_index
=
2113 LLVMGetParam(ctx
->main_fn
,
2114 ctx
->param_streamout_write_index
);
2116 /* Compute (streamout_write_index + thread_id). */
2117 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2119 /* Load the descriptor and compute the write offset for each
2120 * enabled buffer. */
2121 LLVMValueRef so_write_offset
[4] = {};
2122 LLVMValueRef so_buffers
[4];
2123 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2124 SI_PARAM_RW_BUFFERS
);
2126 for (i
= 0; i
< 4; i
++) {
2130 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
2131 SI_VS_STREAMOUT_BUF0
+ i
);
2133 so_buffers
[i
] = ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
2135 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2136 ctx
->param_streamout_offset
[i
]);
2137 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2139 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
2140 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
2141 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
2144 /* Write streamout data. */
2145 for (i
= 0; i
< so
->num_outputs
; i
++) {
2146 unsigned reg
= so
->output
[i
].register_index
;
2151 if (stream
!= so
->output
[i
].stream
)
2154 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2155 &so
->output
[i
], &outputs
[reg
]);
2158 lp_build_endif(&if_ctx
);
2162 /* Generate export instructions for hardware VS shader stage */
2163 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
2164 struct si_shader_output_values
*outputs
,
2167 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2168 struct si_shader
*shader
= ctx
->shader
;
2169 struct lp_build_context
*base
= &bld_base
->base
;
2170 struct ac_export_args args
, pos_args
[4] = {};
2171 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2172 unsigned semantic_name
, semantic_index
;
2174 unsigned param_count
= 0;
2178 for (i
= 0; i
< noutput
; i
++) {
2179 semantic_name
= outputs
[i
].semantic_name
;
2180 semantic_index
= outputs
[i
].semantic_index
;
2181 bool export_param
= true;
2183 switch (semantic_name
) {
2184 case TGSI_SEMANTIC_POSITION
: /* ignore these */
2185 case TGSI_SEMANTIC_PSIZE
:
2186 case TGSI_SEMANTIC_CLIPVERTEX
:
2187 case TGSI_SEMANTIC_EDGEFLAG
:
2189 case TGSI_SEMANTIC_GENERIC
:
2190 case TGSI_SEMANTIC_CLIPDIST
:
2191 if (shader
->key
.opt
.hw_vs
.kill_outputs
&
2192 (1ull << si_shader_io_get_unique_index(semantic_name
, semantic_index
)))
2193 export_param
= false;
2196 if (shader
->key
.opt
.hw_vs
.kill_outputs2
&
2197 (1u << si_shader_io_get_unique_index2(semantic_name
, semantic_index
)))
2198 export_param
= false;
2202 if (outputs
[i
].vertex_stream
[0] != 0 &&
2203 outputs
[i
].vertex_stream
[1] != 0 &&
2204 outputs
[i
].vertex_stream
[2] != 0 &&
2205 outputs
[i
].vertex_stream
[3] != 0)
2206 export_param
= false;
2209 /* Select the correct target */
2210 switch(semantic_name
) {
2211 case TGSI_SEMANTIC_PSIZE
:
2212 psize_value
= outputs
[i
].values
[0];
2214 case TGSI_SEMANTIC_EDGEFLAG
:
2215 edgeflag_value
= outputs
[i
].values
[0];
2217 case TGSI_SEMANTIC_LAYER
:
2218 layer_value
= outputs
[i
].values
[0];
2219 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2220 goto handle_semantic
;
2221 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2222 viewport_index_value
= outputs
[i
].values
[0];
2223 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2224 goto handle_semantic
;
2225 case TGSI_SEMANTIC_POSITION
:
2226 target
= V_008DFC_SQ_EXP_POS
;
2228 case TGSI_SEMANTIC_CLIPDIST
:
2229 if (shader
->key
.opt
.hw_vs
.clip_disable
) {
2230 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2231 goto handle_semantic
;
2233 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
2235 case TGSI_SEMANTIC_CLIPVERTEX
:
2236 if (shader
->key
.opt
.hw_vs
.clip_disable
)
2238 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
2240 case TGSI_SEMANTIC_COLOR
:
2241 case TGSI_SEMANTIC_BCOLOR
:
2242 case TGSI_SEMANTIC_PRIMID
:
2243 case TGSI_SEMANTIC_FOG
:
2244 case TGSI_SEMANTIC_TEXCOORD
:
2245 case TGSI_SEMANTIC_GENERIC
:
2248 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
2249 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2250 shader
->info
.vs_output_param_offset
[i
] = param_count
;
2256 "Warning: SI unhandled vs output type:%d\n",
2260 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, &args
);
2262 if (target
>= V_008DFC_SQ_EXP_POS
&&
2263 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
2264 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
2265 &args
, sizeof(args
));
2267 ac_build_export(&ctx
->ac
, &args
);
2270 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
2271 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2272 goto handle_semantic
;
2276 shader
->info
.nr_param_exports
= param_count
;
2278 /* We need to add the position output manually if it's missing. */
2279 if (!pos_args
[0].out
[0]) {
2280 pos_args
[0].enabled_channels
= 0xf; /* writemask */
2281 pos_args
[0].valid_mask
= 0; /* EXEC mask */
2282 pos_args
[0].done
= 0; /* last export? */
2283 pos_args
[0].target
= V_008DFC_SQ_EXP_POS
;
2284 pos_args
[0].compr
= 0; /* COMPR flag */
2285 pos_args
[0].out
[0] = base
->zero
; /* X */
2286 pos_args
[0].out
[1] = base
->zero
; /* Y */
2287 pos_args
[0].out
[2] = base
->zero
; /* Z */
2288 pos_args
[0].out
[3] = base
->one
; /* W */
2291 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2292 if (shader
->selector
->info
.writes_psize
||
2293 shader
->selector
->info
.writes_edgeflag
||
2294 shader
->selector
->info
.writes_viewport_index
||
2295 shader
->selector
->info
.writes_layer
) {
2296 pos_args
[1].enabled_channels
= shader
->selector
->info
.writes_psize
|
2297 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2298 (shader
->selector
->info
.writes_layer
<< 2) |
2299 (shader
->selector
->info
.writes_viewport_index
<< 3);
2300 pos_args
[1].valid_mask
= 0; /* EXEC mask */
2301 pos_args
[1].done
= 0; /* last export? */
2302 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
2303 pos_args
[1].compr
= 0; /* COMPR flag */
2304 pos_args
[1].out
[0] = base
->zero
; /* X */
2305 pos_args
[1].out
[1] = base
->zero
; /* Y */
2306 pos_args
[1].out
[2] = base
->zero
; /* Z */
2307 pos_args
[1].out
[3] = base
->zero
; /* W */
2309 if (shader
->selector
->info
.writes_psize
)
2310 pos_args
[1].out
[0] = psize_value
;
2312 if (shader
->selector
->info
.writes_edgeflag
) {
2313 /* The output is a float, but the hw expects an integer
2314 * with the first bit containing the edge flag. */
2315 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
2318 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
2320 bld_base
->int_bld
.one
);
2322 /* The LLVM intrinsic expects a float. */
2323 pos_args
[1].out
[1] = LLVMBuildBitCast(base
->gallivm
->builder
,
2328 if (shader
->selector
->info
.writes_layer
)
2329 pos_args
[1].out
[2] = layer_value
;
2331 if (shader
->selector
->info
.writes_viewport_index
)
2332 pos_args
[1].out
[3] = viewport_index_value
;
2335 for (i
= 0; i
< 4; i
++)
2336 if (pos_args
[i
].out
[0])
2337 shader
->info
.nr_pos_exports
++;
2340 for (i
= 0; i
< 4; i
++) {
2341 if (!pos_args
[i
].out
[0])
2344 /* Specify the target we are exporting */
2345 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
2347 if (pos_idx
== shader
->info
.nr_pos_exports
)
2348 /* Specify that this is the last export */
2349 pos_args
[i
].done
= 1;
2351 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
2356 * Forward all outputs from the vertex shader to the TES. This is only used
2357 * for the fixed function TCS.
2359 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
2361 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2362 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2363 LLVMValueRef invocation_id
, rw_buffers
, buffer
, buffer_offset
;
2364 LLVMValueRef lds_vertex_stride
, lds_vertex_offset
, lds_base
;
2367 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2369 rw_buffers
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
2370 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
2371 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
2373 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
2375 lds_vertex_stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
2376 lds_vertex_offset
= LLVMBuildMul(gallivm
->builder
, invocation_id
,
2377 lds_vertex_stride
, "");
2378 lds_base
= get_tcs_in_current_patch_offset(ctx
);
2379 lds_base
= LLVMBuildAdd(gallivm
->builder
, lds_base
, lds_vertex_offset
, "");
2381 inputs
= ctx
->shader
->key
.mono
.tcs
.inputs_to_copy
;
2383 unsigned i
= u_bit_scan64(&inputs
);
2385 LLVMValueRef lds_ptr
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2386 lp_build_const_int32(gallivm
, 4 * i
),
2389 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
2390 get_rel_patch_id(ctx
),
2392 lp_build_const_int32(gallivm
, i
));
2394 LLVMValueRef value
= lds_load(bld_base
, TGSI_TYPE_SIGNED
, ~0,
2397 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, value
, 4, buffer_addr
,
2398 buffer_offset
, 0, 1, 0, true, false);
2402 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2403 LLVMValueRef rel_patch_id
,
2404 LLVMValueRef invocation_id
,
2405 LLVMValueRef tcs_out_current_patch_data_offset
)
2407 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2408 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2409 struct si_shader
*shader
= ctx
->shader
;
2410 unsigned tess_inner_index
, tess_outer_index
;
2411 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2412 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
, inner
[4], outer
[4];
2413 unsigned stride
, outer_comps
, inner_comps
, i
;
2414 struct lp_build_if_state if_ctx
, inner_if_ctx
;
2416 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
2418 /* Do this only for invocation 0, because the tess levels are per-patch,
2421 * This can't jump, because invocation 0 executes this. It should
2422 * at least mask out the loads and stores for other invocations.
2424 lp_build_if(&if_ctx
, gallivm
,
2425 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2426 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2428 /* Determine the layout of one tess factor element in the buffer. */
2429 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
2430 case PIPE_PRIM_LINES
:
2431 stride
= 2; /* 2 dwords, 1 vec2 store */
2435 case PIPE_PRIM_TRIANGLES
:
2436 stride
= 4; /* 4 dwords, 1 vec4 store */
2440 case PIPE_PRIM_QUADS
:
2441 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2450 /* Load tess_inner and tess_outer from LDS.
2451 * Any invocation can write them, so we can't get them from a temporary.
2453 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2454 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2456 lds_base
= tcs_out_current_patch_data_offset
;
2457 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2458 lp_build_const_int32(gallivm
,
2459 tess_inner_index
* 4), "");
2460 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2461 lp_build_const_int32(gallivm
,
2462 tess_outer_index
* 4), "");
2464 for (i
= 0; i
< 4; i
++) {
2465 inner
[i
] = LLVMGetUndef(ctx
->i32
);
2466 outer
[i
] = LLVMGetUndef(ctx
->i32
);
2469 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
2470 /* For isolines, the hardware expects tess factors in the
2471 * reverse order from what GLSL / TGSI specify.
2473 outer
[0] = out
[1] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 0, lds_outer
);
2474 outer
[1] = out
[0] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 1, lds_outer
);
2476 for (i
= 0; i
< outer_comps
; i
++) {
2478 lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2480 for (i
= 0; i
< inner_comps
; i
++) {
2481 inner
[i
] = out
[outer_comps
+i
] =
2482 lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2486 /* Convert the outputs to vectors for stores. */
2487 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2491 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2493 /* Get the buffer. */
2494 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2495 SI_PARAM_RW_BUFFERS
);
2496 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
2497 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_FACTOR
));
2499 /* Get the offset. */
2500 tf_base
= LLVMGetParam(ctx
->main_fn
,
2501 SI_PARAM_TESS_FACTOR_OFFSET
);
2502 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2503 lp_build_const_int32(gallivm
, 4 * stride
), "");
2505 lp_build_if(&inner_if_ctx
, gallivm
,
2506 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2507 rel_patch_id
, bld_base
->uint_bld
.zero
, ""));
2509 /* Store the dynamic HS control word. */
2510 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
2511 lp_build_const_int32(gallivm
, 0x80000000),
2512 1, lp_build_const_int32(gallivm
, 0), tf_base
,
2513 0, 1, 0, true, false);
2515 lp_build_endif(&inner_if_ctx
);
2517 /* Store the tessellation factors. */
2518 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
2519 MIN2(stride
, 4), byteoffset
, tf_base
,
2520 4, 1, 0, true, false);
2522 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
2523 stride
- 4, byteoffset
, tf_base
,
2524 20, 1, 0, true, false);
2526 /* Store the tess factors into the offchip buffer if TES reads them. */
2527 if (shader
->key
.part
.tcs
.epilog
.tes_reads_tess_factors
) {
2528 LLVMValueRef buf
, base
, inner_vec
, outer_vec
, tf_outer_offset
;
2529 LLVMValueRef tf_inner_offset
;
2530 unsigned param_outer
, param_inner
;
2532 buf
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
2533 LLVMConstInt(ctx
->i32
, SI_HS_RING_TESS_OFFCHIP
, 0));
2534 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
2536 param_outer
= si_shader_io_get_unique_index(
2537 TGSI_SEMANTIC_TESSOUTER
, 0);
2538 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
2539 LLVMConstInt(ctx
->i32
, param_outer
, 0));
2541 outer_vec
= lp_build_gather_values(gallivm
, outer
,
2542 util_next_power_of_two(outer_comps
));
2544 ac_build_buffer_store_dword(&ctx
->ac
, buf
, outer_vec
,
2545 outer_comps
, tf_outer_offset
,
2546 base
, 0, 1, 0, true, false);
2548 param_inner
= si_shader_io_get_unique_index(
2549 TGSI_SEMANTIC_TESSINNER
, 0);
2550 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, rel_patch_id
, NULL
,
2551 LLVMConstInt(ctx
->i32
, param_inner
, 0));
2553 inner_vec
= inner_comps
== 1 ? inner
[0] :
2554 lp_build_gather_values(gallivm
, inner
, inner_comps
);
2555 ac_build_buffer_store_dword(&ctx
->ac
, buf
, inner_vec
,
2556 inner_comps
, tf_inner_offset
,
2557 base
, 0, 1, 0, true, false);
2561 lp_build_endif(&if_ctx
);
2564 /* This only writes the tessellation factor levels. */
2565 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2567 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2568 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2569 LLVMValueRef offchip_soffset
, offchip_layout
;
2571 si_copy_tcs_inputs(bld_base
);
2573 rel_patch_id
= get_rel_patch_id(ctx
);
2574 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2575 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2577 /* Return epilog parameters from this function. */
2578 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2579 LLVMValueRef ret
= ctx
->return_value
;
2580 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2583 /* RW_BUFFERS pointer */
2584 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2585 SI_PARAM_RW_BUFFERS
);
2586 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2587 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2588 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2589 bld_base
->uint_bld
.zero
, "");
2590 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2591 bld_base
->uint_bld
.one
, "");
2592 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2593 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2595 /* Tess offchip and factor buffer soffset are after user SGPRs. */
2596 offchip_layout
= LLVMGetParam(ctx
->main_fn
,
2597 SI_PARAM_TCS_OFFCHIP_LAYOUT
);
2598 offchip_soffset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
2599 tf_soffset
= LLVMGetParam(ctx
->main_fn
,
2600 SI_PARAM_TESS_FACTOR_OFFSET
);
2601 ret
= LLVMBuildInsertValue(builder
, ret
, offchip_layout
,
2602 SI_SGPR_TCS_OFFCHIP_LAYOUT
, "");
2603 ret
= LLVMBuildInsertValue(builder
, ret
, offchip_soffset
,
2604 SI_TCS_NUM_USER_SGPR
, "");
2605 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2606 SI_TCS_NUM_USER_SGPR
+ 1, "");
2609 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2610 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2611 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2613 vgpr
= SI_TCS_NUM_USER_SGPR
+ 2;
2614 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2615 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2616 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2617 ctx
->return_value
= ret
;
2620 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2622 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2623 struct si_shader
*shader
= ctx
->shader
;
2624 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2625 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2627 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
2628 ctx
->param_rel_auto_id
);
2629 LLVMValueRef vertex_dw_stride
=
2630 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2631 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2632 vertex_dw_stride
, "");
2634 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2635 * its inputs from it. */
2636 for (i
= 0; i
< info
->num_outputs
; i
++) {
2637 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2638 unsigned name
= info
->output_semantic_name
[i
];
2639 unsigned index
= info
->output_semantic_index
[i
];
2640 int param
= si_shader_io_get_unique_index(name
, index
);
2641 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2642 lp_build_const_int32(gallivm
, param
* 4), "");
2644 for (chan
= 0; chan
< 4; chan
++) {
2645 lds_store(bld_base
, chan
, dw_addr
,
2646 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2651 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2653 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2654 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2655 struct si_shader
*es
= ctx
->shader
;
2656 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2657 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
2658 ctx
->param_es2gs_offset
);
2662 for (i
= 0; i
< info
->num_outputs
; i
++) {
2663 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2666 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2667 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2670 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2671 info
->output_semantic_index
[i
]);
2673 for (chan
= 0; chan
< 4; chan
++) {
2674 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2675 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2677 ac_build_buffer_store_dword(&ctx
->ac
,
2679 out_val
, 1, NULL
, soffset
,
2680 (4 * param_index
+ chan
) * 4,
2686 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2688 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2690 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
,
2691 LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
));
2694 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2696 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2697 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2698 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2699 struct si_shader_output_values
*outputs
= NULL
;
2702 assert(!ctx
->shader
->is_gs_copy_shader
);
2704 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2706 /* Vertex color clamping.
2708 * This uses a state constant loaded in a user data SGPR and
2709 * an IF statement is added that clamps all colors if the constant
2712 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
2713 struct lp_build_if_state if_ctx
;
2714 LLVMValueRef cond
= NULL
;
2715 LLVMValueRef addr
, val
;
2717 for (i
= 0; i
< info
->num_outputs
; i
++) {
2718 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2719 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2722 /* We've found a color. */
2724 /* The state is in the first bit of the user SGPR. */
2725 cond
= LLVMGetParam(ctx
->main_fn
,
2726 SI_PARAM_VS_STATE_BITS
);
2727 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2729 lp_build_if(&if_ctx
, gallivm
, cond
);
2732 for (j
= 0; j
< 4; j
++) {
2733 addr
= ctx
->outputs
[i
][j
];
2734 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2735 val
= ac_build_clamp(&ctx
->ac
, val
);
2736 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2741 lp_build_endif(&if_ctx
);
2744 for (i
= 0; i
< info
->num_outputs
; i
++) {
2745 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
2746 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
2748 for (j
= 0; j
< 4; j
++) {
2749 outputs
[i
].values
[j
] =
2750 LLVMBuildLoad(gallivm
->builder
,
2753 outputs
[i
].vertex_stream
[j
] =
2754 (info
->output_streams
[i
] >> (2 * j
)) & 3;
2759 /* Return the primitive ID from the LLVM function. */
2761 LLVMBuildInsertValue(gallivm
->builder
,
2763 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2764 get_primitive_id(bld_base
, 0)),
2765 VS_EPILOG_PRIMID_LOC
, "");
2767 if (ctx
->shader
->selector
->so
.num_outputs
)
2768 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
2769 si_llvm_export_vs(bld_base
, outputs
, i
);
2773 struct si_ps_exports
{
2775 struct ac_export_args args
[10];
2778 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
2779 bool writes_samplemask
)
2782 /* Z needs 32 bits. */
2783 if (writes_samplemask
)
2784 return V_028710_SPI_SHADER_32_ABGR
;
2785 else if (writes_stencil
)
2786 return V_028710_SPI_SHADER_32_GR
;
2788 return V_028710_SPI_SHADER_32_R
;
2789 } else if (writes_stencil
|| writes_samplemask
) {
2790 /* Both stencil and sample mask need only 16 bits. */
2791 return V_028710_SPI_SHADER_UINT16_ABGR
;
2793 return V_028710_SPI_SHADER_ZERO
;
2797 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2798 LLVMValueRef depth
, LLVMValueRef stencil
,
2799 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
2801 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2802 struct lp_build_context
*base
= &bld_base
->base
;
2803 struct ac_export_args args
;
2805 unsigned format
= si_get_spi_shader_z_format(depth
!= NULL
,
2807 samplemask
!= NULL
);
2809 assert(depth
|| stencil
|| samplemask
);
2811 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2812 args
.done
= 1; /* DONE bit */
2814 /* Specify the target we are exporting */
2815 args
.target
= V_008DFC_SQ_EXP_MRTZ
;
2817 args
.compr
= 0; /* COMP flag */
2818 args
.out
[0] = base
->undef
; /* R, depth */
2819 args
.out
[1] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2820 args
.out
[2] = base
->undef
; /* B, sample mask */
2821 args
.out
[3] = base
->undef
; /* A, alpha to mask */
2823 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
2825 args
.compr
= 1; /* COMPR flag */
2828 /* Stencil should be in X[23:16]. */
2829 stencil
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, stencil
);
2830 stencil
= LLVMBuildShl(base
->gallivm
->builder
, stencil
,
2831 LLVMConstInt(ctx
->i32
, 16, 0), "");
2832 args
.out
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
, stencil
);
2836 /* SampleMask should be in Y[15:0]. */
2837 args
.out
[1] = samplemask
;
2842 args
.out
[0] = depth
;
2846 args
.out
[1] = stencil
;
2850 args
.out
[2] = samplemask
;
2855 /* SI (except OLAND and HAINAN) has a bug that it only looks
2856 * at the X writemask component. */
2857 if (ctx
->screen
->b
.chip_class
== SI
&&
2858 ctx
->screen
->b
.family
!= CHIP_OLAND
&&
2859 ctx
->screen
->b
.family
!= CHIP_HAINAN
)
2862 /* Specify which components to enable */
2863 args
.enabled_channels
= mask
;
2865 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
2868 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2869 LLVMValueRef
*color
, unsigned index
,
2870 unsigned samplemask_param
,
2871 bool is_last
, struct si_ps_exports
*exp
)
2873 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2874 struct lp_build_context
*base
= &bld_base
->base
;
2878 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
2879 for (i
= 0; i
< 4; i
++)
2880 color
[i
] = ac_build_clamp(&ctx
->ac
, color
[i
]);
2883 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
2884 color
[3] = base
->one
;
2888 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2889 si_alpha_test(bld_base
, color
[3]);
2891 /* Line & polygon smoothing */
2892 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
2893 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
2896 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2897 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
2898 struct ac_export_args args
[8];
2901 /* Get the export arguments, also find out what the last one is. */
2902 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
2903 si_llvm_init_export_args(bld_base
, color
,
2904 V_008DFC_SQ_EXP_MRT
+ c
, &args
[c
]);
2905 if (args
[c
].enabled_channels
)
2909 /* Emit all exports. */
2910 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
2911 if (is_last
&& last
== c
) {
2912 args
[c
].valid_mask
= 1; /* whether the EXEC mask is valid */
2913 args
[c
].done
= 1; /* DONE bit */
2914 } else if (!args
[c
].enabled_channels
)
2915 continue; /* unnecessary NULL export */
2917 memcpy(&exp
->args
[exp
->num
++], &args
[c
], sizeof(args
[c
]));
2920 struct ac_export_args args
;
2923 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
2926 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2927 args
.done
= 1; /* DONE bit */
2928 } else if (!args
.enabled_channels
)
2929 return; /* unnecessary NULL export */
2931 memcpy(&exp
->args
[exp
->num
++], &args
, sizeof(args
));
2935 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
2936 struct si_ps_exports
*exp
)
2938 for (unsigned i
= 0; i
< exp
->num
; i
++)
2939 ac_build_export(&ctx
->ac
, &exp
->args
[i
]);
2942 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
2944 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2945 struct lp_build_context
*base
= &bld_base
->base
;
2946 struct ac_export_args args
;
2948 args
.enabled_channels
= 0x0; /* enabled channels */
2949 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2950 args
.done
= 1; /* DONE bit */
2951 args
.target
= V_008DFC_SQ_EXP_NULL
;
2952 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2953 args
.out
[0] = base
->undef
; /* R */
2954 args
.out
[1] = base
->undef
; /* G */
2955 args
.out
[2] = base
->undef
; /* B */
2956 args
.out
[3] = base
->undef
; /* A */
2958 ac_build_export(&ctx
->ac
, &args
);
2962 * Return PS outputs in this order:
2964 * v[0:3] = color0.xyzw
2965 * v[4:7] = color1.xyzw
2970 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
2972 * The alpha-ref SGPR is returned via its original location.
2974 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
2976 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2977 struct si_shader
*shader
= ctx
->shader
;
2978 struct lp_build_context
*base
= &bld_base
->base
;
2979 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2980 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2981 unsigned i
, j
, first_vgpr
, vgpr
;
2983 LLVMValueRef color
[8][4] = {};
2984 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2987 /* Read the output values. */
2988 for (i
= 0; i
< info
->num_outputs
; i
++) {
2989 unsigned semantic_name
= info
->output_semantic_name
[i
];
2990 unsigned semantic_index
= info
->output_semantic_index
[i
];
2992 switch (semantic_name
) {
2993 case TGSI_SEMANTIC_COLOR
:
2994 assert(semantic_index
< 8);
2995 for (j
= 0; j
< 4; j
++) {
2996 LLVMValueRef ptr
= ctx
->outputs
[i
][j
];
2997 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2998 color
[semantic_index
][j
] = result
;
3001 case TGSI_SEMANTIC_POSITION
:
3002 depth
= LLVMBuildLoad(builder
,
3003 ctx
->outputs
[i
][2], "");
3005 case TGSI_SEMANTIC_STENCIL
:
3006 stencil
= LLVMBuildLoad(builder
,
3007 ctx
->outputs
[i
][1], "");
3009 case TGSI_SEMANTIC_SAMPLEMASK
:
3010 samplemask
= LLVMBuildLoad(builder
,
3011 ctx
->outputs
[i
][0], "");
3014 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3019 /* Fill the return structure. */
3020 ret
= ctx
->return_value
;
3023 ret
= LLVMBuildInsertValue(builder
, ret
,
3024 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
3025 LLVMGetParam(ctx
->main_fn
,
3026 SI_PARAM_ALPHA_REF
)),
3027 SI_SGPR_ALPHA_REF
, "");
3030 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3031 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3035 for (j
= 0; j
< 4; j
++)
3036 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3039 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3041 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3043 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3045 /* Add the input sample mask for smoothing at the end. */
3046 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3047 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3048 ret
= LLVMBuildInsertValue(builder
, ret
,
3049 LLVMGetParam(ctx
->main_fn
,
3050 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3052 ctx
->return_value
= ret
;
3056 * Given a v8i32 resource descriptor for a buffer, extract the size of the
3057 * buffer in number of elements and return it as an i32.
3059 static LLVMValueRef
get_buffer_size(
3060 struct lp_build_tgsi_context
*bld_base
,
3061 LLVMValueRef descriptor
)
3063 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3064 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3065 LLVMBuilderRef builder
= gallivm
->builder
;
3067 LLVMBuildExtractElement(builder
, descriptor
,
3068 lp_build_const_int32(gallivm
, 2), "");
3070 if (ctx
->screen
->b
.chip_class
== VI
) {
3071 /* On VI, the descriptor contains the size in bytes,
3072 * but TXQ must return the size in elements.
3073 * The stride is always non-zero for resources using TXQ.
3075 LLVMValueRef stride
=
3076 LLVMBuildExtractElement(builder
, descriptor
,
3077 lp_build_const_int32(gallivm
, 1), "");
3078 stride
= LLVMBuildLShr(builder
, stride
,
3079 lp_build_const_int32(gallivm
, 16), "");
3080 stride
= LLVMBuildAnd(builder
, stride
,
3081 lp_build_const_int32(gallivm
, 0x3FFF), "");
3083 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
3089 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3090 struct lp_build_tgsi_context
*bld_base
,
3091 struct lp_build_emit_data
*emit_data
);
3093 /* Prevent optimizations (at least of memory accesses) across the current
3094 * point in the program by emitting empty inline assembly that is marked as
3095 * having side effects.
3097 #if 0 /* unused currently */
3098 static void emit_optimization_barrier(struct si_shader_context
*ctx
)
3100 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3101 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
3102 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, "", "", true, false);
3103 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
3107 /* Combine these with & instead of |. */
3108 #define NOOP_WAITCNT 0xf7f
3109 #define LGKM_CNT 0x07f
3110 #define VM_CNT 0xf70
3112 static void emit_waitcnt(struct si_shader_context
*ctx
, unsigned simm16
)
3114 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3115 LLVMBuilderRef builder
= gallivm
->builder
;
3116 LLVMValueRef args
[1] = {
3117 lp_build_const_int32(gallivm
, simm16
)
3119 lp_build_intrinsic(builder
, "llvm.amdgcn.s.waitcnt",
3120 ctx
->voidt
, args
, 1, 0);
3123 static void membar_emit(
3124 const struct lp_build_tgsi_action
*action
,
3125 struct lp_build_tgsi_context
*bld_base
,
3126 struct lp_build_emit_data
*emit_data
)
3128 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3129 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3130 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3131 unsigned waitcnt
= NOOP_WAITCNT
;
3133 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3134 waitcnt
&= VM_CNT
& LGKM_CNT
;
3136 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3137 TGSI_MEMBAR_SHADER_BUFFER
|
3138 TGSI_MEMBAR_SHADER_IMAGE
))
3141 if (flags
& TGSI_MEMBAR_SHARED
)
3142 waitcnt
&= LGKM_CNT
;
3144 if (waitcnt
!= NOOP_WAITCNT
)
3145 emit_waitcnt(ctx
, waitcnt
);
3148 static void clock_emit(
3149 const struct lp_build_tgsi_action
*action
,
3150 struct lp_build_tgsi_context
*bld_base
,
3151 struct lp_build_emit_data
*emit_data
)
3153 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3154 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3157 tmp
= lp_build_intrinsic(gallivm
->builder
, "llvm.readcyclecounter",
3158 ctx
->i64
, NULL
, 0, 0);
3159 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->v2i32
, "");
3161 emit_data
->output
[0] =
3162 LLVMBuildExtractElement(gallivm
->builder
, tmp
, ctx
->i32_0
, "");
3163 emit_data
->output
[1] =
3164 LLVMBuildExtractElement(gallivm
->builder
, tmp
, ctx
->i32_1
, "");
3168 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
3169 const struct tgsi_full_src_register
*reg
)
3172 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3173 SI_PARAM_SHADER_BUFFERS
);
3175 if (!reg
->Register
.Indirect
)
3176 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, 0);
3178 index
= get_bounded_indirect_index(ctx
, ®
->Indirect
,
3179 reg
->Register
.Index
,
3180 SI_NUM_SHADER_BUFFERS
);
3182 return ac_build_indexed_load_const(&ctx
->ac
, rsrc_ptr
, index
);
3185 static bool tgsi_is_array_sampler(unsigned target
)
3187 return target
== TGSI_TEXTURE_1D_ARRAY
||
3188 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
3189 target
== TGSI_TEXTURE_2D_ARRAY
||
3190 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
3191 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3192 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
3193 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3196 static bool tgsi_is_array_image(unsigned target
)
3198 return target
== TGSI_TEXTURE_3D
||
3199 target
== TGSI_TEXTURE_CUBE
||
3200 target
== TGSI_TEXTURE_1D_ARRAY
||
3201 target
== TGSI_TEXTURE_2D_ARRAY
||
3202 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3203 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3207 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
3209 * At least on Tonga, executing image stores on images with DCC enabled and
3210 * non-trivial can eventually lead to lockups. This can occur when an
3211 * application binds an image as read-only but then uses a shader that writes
3212 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
3213 * program termination) in this case, but it doesn't cost much to be a bit
3214 * nicer: disabling DCC in the shader still leads to undefined results but
3215 * avoids the lockup.
3217 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
3220 if (ctx
->screen
->b
.chip_class
<= CIK
) {
3223 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3224 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
3225 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
3228 tmp
= LLVMBuildExtractElement(builder
, rsrc
, i32_6
, "");
3229 tmp
= LLVMBuildAnd(builder
, tmp
, i32_C
, "");
3230 return LLVMBuildInsertElement(builder
, rsrc
, tmp
, i32_6
, "");
3234 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3236 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3240 static LLVMValueRef
load_image_desc(struct si_shader_context
*ctx
,
3241 LLVMValueRef list
, LLVMValueRef index
,
3244 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3246 if (target
== TGSI_TEXTURE_BUFFER
) {
3247 index
= LLVMBuildMul(builder
, index
,
3248 LLVMConstInt(ctx
->i32
, 2, 0), "");
3249 index
= LLVMBuildAdd(builder
, index
,
3250 LLVMConstInt(ctx
->i32
, 1, 0), "");
3251 list
= LLVMBuildPointerCast(builder
, list
,
3252 const_array(ctx
->v4i32
, 0), "");
3255 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
3259 * Load the resource descriptor for \p image.
3263 struct lp_build_tgsi_context
*bld_base
,
3264 const struct tgsi_full_src_register
*image
,
3265 bool is_store
, unsigned target
,
3268 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3269 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3272 bool dcc_off
= is_store
;
3274 assert(image
->Register
.File
== TGSI_FILE_IMAGE
);
3276 if (!image
->Register
.Indirect
) {
3277 const struct tgsi_shader_info
*info
= bld_base
->info
;
3278 unsigned images_writemask
= info
->images_store
|
3279 info
->images_atomic
;
3281 index
= LLVMConstInt(ctx
->i32
, image
->Register
.Index
, 0);
3283 if (images_writemask
& (1 << image
->Register
.Index
))
3286 /* From the GL_ARB_shader_image_load_store extension spec:
3288 * If a shader performs an image load, store, or atomic
3289 * operation using an image variable declared as an array,
3290 * and if the index used to select an individual element is
3291 * negative or greater than or equal to the size of the
3292 * array, the results of the operation are undefined but may
3293 * not lead to termination.
3295 index
= get_bounded_indirect_index(ctx
, &image
->Indirect
,
3296 image
->Register
.Index
,
3300 *rsrc
= load_image_desc(ctx
, rsrc_ptr
, index
, target
);
3301 if (dcc_off
&& target
!= TGSI_TEXTURE_BUFFER
)
3302 *rsrc
= force_dcc_off(ctx
, *rsrc
);
3305 static LLVMValueRef
image_fetch_coords(
3306 struct lp_build_tgsi_context
*bld_base
,
3307 const struct tgsi_full_instruction
*inst
,
3310 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3311 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3312 LLVMBuilderRef builder
= gallivm
->builder
;
3313 unsigned target
= inst
->Memory
.Texture
;
3314 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
3315 LLVMValueRef coords
[4];
3319 for (chan
= 0; chan
< num_coords
; ++chan
) {
3320 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
3321 tmp
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3325 /* 1D textures are allocated and used as 2D on GFX9. */
3326 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
3327 if (target
== TGSI_TEXTURE_1D
) {
3328 coords
[1] = bld_base
->uint_bld
.zero
;
3330 } else if (target
== TGSI_TEXTURE_1D_ARRAY
) {
3331 coords
[2] = coords
[1];
3332 coords
[1] = bld_base
->uint_bld
.zero
;
3336 if (num_coords
== 1)
3339 if (num_coords
== 3) {
3340 /* LLVM has difficulties lowering 3-element vectors. */
3341 coords
[3] = bld_base
->uint_bld
.undef
;
3345 return lp_build_gather_values(gallivm
, coords
, num_coords
);
3349 * Append the extra mode bits that are used by image load and store.
3351 static void image_append_args(
3352 struct si_shader_context
*ctx
,
3353 struct lp_build_emit_data
* emit_data
,
3358 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3359 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3360 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3361 LLVMValueRef r128
= i1false
;
3362 LLVMValueRef da
= tgsi_is_array_image(target
) ? i1true
: i1false
;
3365 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3367 LLVMValueRef slc
= i1false
;
3368 LLVMValueRef lwe
= i1false
;
3370 if (atomic
|| (HAVE_LLVM
<= 0x0309)) {
3371 emit_data
->args
[emit_data
->arg_count
++] = r128
;
3372 emit_data
->args
[emit_data
->arg_count
++] = da
;
3374 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3376 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3380 /* HAVE_LLVM >= 0x0400 */
3381 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3382 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3383 emit_data
->args
[emit_data
->arg_count
++] = lwe
;
3384 emit_data
->args
[emit_data
->arg_count
++] = da
;
3388 * Append the resource and indexing arguments for buffer intrinsics.
3390 * \param rsrc the v4i32 buffer resource
3391 * \param index index into the buffer (stride-based)
3392 * \param offset byte offset into the buffer
3394 static void buffer_append_args(
3395 struct si_shader_context
*ctx
,
3396 struct lp_build_emit_data
*emit_data
,
3399 LLVMValueRef offset
,
3403 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3404 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3405 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3407 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3408 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
3409 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
3411 emit_data
->args
[emit_data
->arg_count
++] =
3413 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3414 i1true
: i1false
; /* glc */
3416 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
3419 static void load_fetch_args(
3420 struct lp_build_tgsi_context
* bld_base
,
3421 struct lp_build_emit_data
* emit_data
)
3423 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3424 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3425 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3426 unsigned target
= inst
->Memory
.Texture
;
3429 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
3431 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3432 LLVMBuilderRef builder
= gallivm
->builder
;
3433 LLVMValueRef offset
;
3436 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3438 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3439 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3441 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3442 offset
, false, false);
3443 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3444 LLVMValueRef coords
;
3446 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
3447 coords
= image_fetch_coords(bld_base
, inst
, 1);
3449 if (target
== TGSI_TEXTURE_BUFFER
) {
3450 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3451 bld_base
->uint_bld
.zero
, false, false);
3453 emit_data
->args
[0] = coords
;
3454 emit_data
->args
[1] = rsrc
;
3455 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
3456 emit_data
->arg_count
= 3;
3458 image_append_args(ctx
, emit_data
, target
, false, false);
3463 static unsigned get_load_intr_attribs(bool readonly_memory
)
3465 /* READNONE means writes can't affect it, while READONLY means that
3466 * writes can affect it. */
3467 return readonly_memory
&& HAVE_LLVM
>= 0x0400 ?
3468 LP_FUNC_ATTR_READNONE
:
3469 LP_FUNC_ATTR_READONLY
;
3472 static unsigned get_store_intr_attribs(bool writeonly_memory
)
3474 return writeonly_memory
&& HAVE_LLVM
>= 0x0400 ?
3475 LP_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
3476 LP_FUNC_ATTR_WRITEONLY
;
3479 static void load_emit_buffer(struct si_shader_context
*ctx
,
3480 struct lp_build_emit_data
*emit_data
,
3481 bool readonly_memory
)
3483 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3484 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3485 LLVMBuilderRef builder
= gallivm
->builder
;
3486 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
3487 uint count
= util_last_bit(writemask
);
3488 const char *intrinsic_name
;
3489 LLVMTypeRef dst_type
;
3493 intrinsic_name
= "llvm.amdgcn.buffer.load.f32";
3494 dst_type
= ctx
->f32
;
3497 intrinsic_name
= "llvm.amdgcn.buffer.load.v2f32";
3498 dst_type
= LLVMVectorType(ctx
->f32
, 2);
3501 intrinsic_name
= "llvm.amdgcn.buffer.load.v4f32";
3502 dst_type
= ctx
->v4f32
;
3506 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3507 builder
, intrinsic_name
, dst_type
,
3508 emit_data
->args
, emit_data
->arg_count
,
3509 get_load_intr_attribs(readonly_memory
));
3512 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
3513 const struct tgsi_full_instruction
*inst
,
3514 LLVMTypeRef type
, int arg
)
3516 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3517 LLVMBuilderRef builder
= gallivm
->builder
;
3518 LLVMValueRef offset
, ptr
;
3521 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
3522 offset
= LLVMBuildBitCast(builder
, offset
, ctx
->i32
, "");
3524 ptr
= ctx
->shared_memory
;
3525 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
3526 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3527 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
3532 static void load_emit_memory(
3533 struct si_shader_context
*ctx
,
3534 struct lp_build_emit_data
*emit_data
)
3536 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3537 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
3538 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3539 LLVMBuilderRef builder
= gallivm
->builder
;
3540 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3541 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
3544 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 1);
3546 for (chan
= 0; chan
< 4; ++chan
) {
3547 if (!(writemask
& (1 << chan
))) {
3548 channels
[chan
] = LLVMGetUndef(base
->elem_type
);
3552 index
= lp_build_const_int32(gallivm
, chan
);
3553 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3554 channels
[chan
] = LLVMBuildLoad(builder
, derived_ptr
, "");
3556 emit_data
->output
[emit_data
->chan
] = lp_build_gather_values(gallivm
, channels
, 4);
3560 * Return true if the memory accessed by a LOAD or STORE instruction is
3561 * read-only or write-only, respectively.
3563 * \param shader_buffers_reverse_access_mask
3564 * For LOAD, set this to (store | atomic) slot usage in the shader.
3565 * For STORE, set this to (load | atomic) slot usage in the shader.
3566 * \param images_reverse_access_mask Same as above, but for images.
3568 static bool is_oneway_access_only(const struct tgsi_full_instruction
*inst
,
3569 const struct tgsi_shader_info
*info
,
3570 unsigned shader_buffers_reverse_access_mask
,
3571 unsigned images_reverse_access_mask
)
3573 /* RESTRICT means NOALIAS.
3574 * If there are no writes, we can assume the accessed memory is read-only.
3575 * If there are no reads, we can assume the accessed memory is write-only.
3577 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_RESTRICT
) {
3578 unsigned reverse_access_mask
;
3580 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3581 reverse_access_mask
= shader_buffers_reverse_access_mask
;
3582 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
3583 reverse_access_mask
= info
->images_buffers
&
3584 images_reverse_access_mask
;
3586 reverse_access_mask
= ~info
->images_buffers
&
3587 images_reverse_access_mask
;
3590 if (inst
->Src
[0].Register
.Indirect
) {
3591 if (!reverse_access_mask
)
3594 if (!(reverse_access_mask
&
3595 (1u << inst
->Src
[0].Register
.Index
)))
3600 /* If there are no buffer writes (for both shader buffers & image
3601 * buffers), it implies that buffer memory is read-only.
3602 * If there are no buffer reads (for both shader buffers & image
3603 * buffers), it implies that buffer memory is write-only.
3605 * Same for the case when there are no writes/reads for non-buffer
3608 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
3609 (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
&&
3610 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
)) {
3611 if (!shader_buffers_reverse_access_mask
&&
3612 !(info
->images_buffers
& images_reverse_access_mask
))
3615 if (!(~info
->images_buffers
& images_reverse_access_mask
))
3621 static void load_emit(
3622 const struct lp_build_tgsi_action
*action
,
3623 struct lp_build_tgsi_context
*bld_base
,
3624 struct lp_build_emit_data
*emit_data
)
3626 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3627 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3628 LLVMBuilderRef builder
= gallivm
->builder
;
3629 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3630 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
3631 char intrinsic_name
[64];
3632 bool readonly_memory
= false;
3634 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3635 load_emit_memory(ctx
, emit_data
);
3639 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3640 emit_waitcnt(ctx
, VM_CNT
);
3642 readonly_memory
= !(inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
) &&
3643 is_oneway_access_only(inst
, info
,
3644 info
->shader_buffers_store
|
3645 info
->shader_buffers_atomic
,
3646 info
->images_store
|
3647 info
->images_atomic
);
3649 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3650 load_emit_buffer(ctx
, emit_data
, readonly_memory
);
3654 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
3655 emit_data
->output
[emit_data
->chan
] =
3657 builder
, "llvm.amdgcn.buffer.load.format.v4f32", emit_data
->dst_type
,
3658 emit_data
->args
, emit_data
->arg_count
,
3659 get_load_intr_attribs(readonly_memory
));
3661 ac_get_image_intr_name("llvm.amdgcn.image.load",
3662 emit_data
->dst_type
, /* vdata */
3663 LLVMTypeOf(emit_data
->args
[0]), /* coords */
3664 LLVMTypeOf(emit_data
->args
[1]), /* rsrc */
3665 intrinsic_name
, sizeof(intrinsic_name
));
3667 emit_data
->output
[emit_data
->chan
] =
3669 builder
, intrinsic_name
, emit_data
->dst_type
,
3670 emit_data
->args
, emit_data
->arg_count
,
3671 get_load_intr_attribs(readonly_memory
));
3675 static void store_fetch_args(
3676 struct lp_build_tgsi_context
* bld_base
,
3677 struct lp_build_emit_data
* emit_data
)
3679 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3680 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3681 LLVMBuilderRef builder
= gallivm
->builder
;
3682 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3683 struct tgsi_full_src_register memory
;
3684 LLVMValueRef chans
[4];
3689 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
3691 for (chan
= 0; chan
< 4; ++chan
) {
3692 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
3694 data
= lp_build_gather_values(gallivm
, chans
, 4);
3696 emit_data
->args
[emit_data
->arg_count
++] = data
;
3698 memory
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
3700 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3701 LLVMValueRef offset
;
3704 rsrc
= shader_buffer_fetch_rsrc(ctx
, &memory
);
3706 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
3707 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3709 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3710 offset
, false, false);
3711 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3712 unsigned target
= inst
->Memory
.Texture
;
3713 LLVMValueRef coords
;
3715 /* 8bit/16bit TC L1 write corruption bug on SI.
3716 * All store opcodes not aligned to a dword are affected.
3718 * The only way to get unaligned stores in radeonsi is through
3721 bool force_glc
= ctx
->screen
->b
.chip_class
== SI
;
3723 coords
= image_fetch_coords(bld_base
, inst
, 0);
3725 if (target
== TGSI_TEXTURE_BUFFER
) {
3726 image_fetch_rsrc(bld_base
, &memory
, true, target
, &rsrc
);
3727 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3728 bld_base
->uint_bld
.zero
, false, force_glc
);
3730 emit_data
->args
[1] = coords
;
3731 image_fetch_rsrc(bld_base
, &memory
, true, target
,
3732 &emit_data
->args
[2]);
3733 emit_data
->args
[3] = lp_build_const_int32(gallivm
, 15); /* dmask */
3734 emit_data
->arg_count
= 4;
3736 image_append_args(ctx
, emit_data
, target
, false, force_glc
);
3741 static void store_emit_buffer(
3742 struct si_shader_context
*ctx
,
3743 struct lp_build_emit_data
*emit_data
,
3744 bool writeonly_memory
)
3746 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3747 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3748 LLVMBuilderRef builder
= gallivm
->builder
;
3749 struct lp_build_context
*uint_bld
= &ctx
->bld_base
.uint_bld
;
3750 LLVMValueRef base_data
= emit_data
->args
[0];
3751 LLVMValueRef base_offset
= emit_data
->args
[3];
3752 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3756 const char *intrinsic_name
;
3758 LLVMValueRef offset
;
3761 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
3763 /* Due to an LLVM limitation, split 3-element writes
3764 * into a 2-element and a 1-element write. */
3766 writemask
|= 1 << (start
+ 2);
3772 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
3773 } else if (count
== 2) {
3774 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
3776 tmp
= LLVMBuildExtractElement(
3778 lp_build_const_int32(gallivm
, start
), "");
3779 data
= LLVMBuildInsertElement(
3780 builder
, LLVMGetUndef(v2f32
), tmp
,
3781 uint_bld
->zero
, "");
3783 tmp
= LLVMBuildExtractElement(
3785 lp_build_const_int32(gallivm
, start
+ 1), "");
3786 data
= LLVMBuildInsertElement(
3787 builder
, data
, tmp
, uint_bld
->one
, "");
3789 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
3792 data
= LLVMBuildExtractElement(
3794 lp_build_const_int32(gallivm
, start
), "");
3795 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
3798 offset
= base_offset
;
3800 offset
= LLVMBuildAdd(
3802 lp_build_const_int32(gallivm
, start
* 4), "");
3805 emit_data
->args
[0] = data
;
3806 emit_data
->args
[3] = offset
;
3809 builder
, intrinsic_name
, emit_data
->dst_type
,
3810 emit_data
->args
, emit_data
->arg_count
,
3811 get_store_intr_attribs(writeonly_memory
));
3815 static void store_emit_memory(
3816 struct si_shader_context
*ctx
,
3817 struct lp_build_emit_data
*emit_data
)
3819 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3820 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3821 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
3822 LLVMBuilderRef builder
= gallivm
->builder
;
3823 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3824 LLVMValueRef ptr
, derived_ptr
, data
, index
;
3827 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 0);
3829 for (chan
= 0; chan
< 4; ++chan
) {
3830 if (!(writemask
& (1 << chan
))) {
3833 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
3834 index
= lp_build_const_int32(gallivm
, chan
);
3835 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3836 LLVMBuildStore(builder
, data
, derived_ptr
);
3840 static void store_emit(
3841 const struct lp_build_tgsi_action
*action
,
3842 struct lp_build_tgsi_context
*bld_base
,
3843 struct lp_build_emit_data
*emit_data
)
3845 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3846 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3847 LLVMBuilderRef builder
= gallivm
->builder
;
3848 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3849 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
3850 unsigned target
= inst
->Memory
.Texture
;
3851 char intrinsic_name
[64];
3852 bool writeonly_memory
= false;
3854 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3855 store_emit_memory(ctx
, emit_data
);
3859 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3860 emit_waitcnt(ctx
, VM_CNT
);
3862 writeonly_memory
= is_oneway_access_only(inst
, info
,
3863 info
->shader_buffers_load
|
3864 info
->shader_buffers_atomic
,
3866 info
->images_atomic
);
3868 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3869 store_emit_buffer(ctx
, emit_data
, writeonly_memory
);
3873 if (target
== TGSI_TEXTURE_BUFFER
) {
3874 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3875 builder
, "llvm.amdgcn.buffer.store.format.v4f32",
3876 emit_data
->dst_type
, emit_data
->args
,
3877 emit_data
->arg_count
,
3878 get_store_intr_attribs(writeonly_memory
));
3880 ac_get_image_intr_name("llvm.amdgcn.image.store",
3881 LLVMTypeOf(emit_data
->args
[0]), /* vdata */
3882 LLVMTypeOf(emit_data
->args
[1]), /* coords */
3883 LLVMTypeOf(emit_data
->args
[2]), /* rsrc */
3884 intrinsic_name
, sizeof(intrinsic_name
));
3886 emit_data
->output
[emit_data
->chan
] =
3888 builder
, intrinsic_name
, emit_data
->dst_type
,
3889 emit_data
->args
, emit_data
->arg_count
,
3890 get_store_intr_attribs(writeonly_memory
));
3894 static void atomic_fetch_args(
3895 struct lp_build_tgsi_context
* bld_base
,
3896 struct lp_build_emit_data
* emit_data
)
3898 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3899 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3900 LLVMBuilderRef builder
= gallivm
->builder
;
3901 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3902 LLVMValueRef data1
, data2
;
3906 emit_data
->dst_type
= bld_base
->base
.elem_type
;
3908 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
3909 data1
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3911 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3912 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
3913 data2
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3916 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
3917 * of arguments, which is reversed relative to TGSI (and GLSL)
3919 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
3920 emit_data
->args
[emit_data
->arg_count
++] = data2
;
3921 emit_data
->args
[emit_data
->arg_count
++] = data1
;
3923 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3924 LLVMValueRef offset
;
3926 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3928 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3929 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3931 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3932 offset
, true, false);
3933 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3934 unsigned target
= inst
->Memory
.Texture
;
3935 LLVMValueRef coords
;
3937 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
3938 coords
= image_fetch_coords(bld_base
, inst
, 1);
3940 if (target
== TGSI_TEXTURE_BUFFER
) {
3941 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3942 bld_base
->uint_bld
.zero
, true, false);
3944 emit_data
->args
[emit_data
->arg_count
++] = coords
;
3945 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3947 image_append_args(ctx
, emit_data
, target
, true, false);
3952 static void atomic_emit_memory(struct si_shader_context
*ctx
,
3953 struct lp_build_emit_data
*emit_data
) {
3954 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3955 LLVMBuilderRef builder
= gallivm
->builder
;
3956 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3957 LLVMValueRef ptr
, result
, arg
;
3959 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
3961 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
3962 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
3964 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3965 LLVMValueRef new_data
;
3966 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
3969 new_data
= LLVMBuildBitCast(builder
, new_data
, ctx
->i32
, "");
3971 #if HAVE_LLVM >= 0x309
3972 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
3973 LLVMAtomicOrderingSequentiallyConsistent
,
3974 LLVMAtomicOrderingSequentiallyConsistent
,
3978 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
3980 LLVMAtomicRMWBinOp op
;
3982 switch(inst
->Instruction
.Opcode
) {
3983 case TGSI_OPCODE_ATOMUADD
:
3984 op
= LLVMAtomicRMWBinOpAdd
;
3986 case TGSI_OPCODE_ATOMXCHG
:
3987 op
= LLVMAtomicRMWBinOpXchg
;
3989 case TGSI_OPCODE_ATOMAND
:
3990 op
= LLVMAtomicRMWBinOpAnd
;
3992 case TGSI_OPCODE_ATOMOR
:
3993 op
= LLVMAtomicRMWBinOpOr
;
3995 case TGSI_OPCODE_ATOMXOR
:
3996 op
= LLVMAtomicRMWBinOpXor
;
3998 case TGSI_OPCODE_ATOMUMIN
:
3999 op
= LLVMAtomicRMWBinOpUMin
;
4001 case TGSI_OPCODE_ATOMUMAX
:
4002 op
= LLVMAtomicRMWBinOpUMax
;
4004 case TGSI_OPCODE_ATOMIMIN
:
4005 op
= LLVMAtomicRMWBinOpMin
;
4007 case TGSI_OPCODE_ATOMIMAX
:
4008 op
= LLVMAtomicRMWBinOpMax
;
4011 unreachable("unknown atomic opcode");
4014 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
4015 LLVMAtomicOrderingSequentiallyConsistent
,
4018 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
4021 static void atomic_emit(
4022 const struct lp_build_tgsi_action
*action
,
4023 struct lp_build_tgsi_context
*bld_base
,
4024 struct lp_build_emit_data
*emit_data
)
4026 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4027 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4028 LLVMBuilderRef builder
= gallivm
->builder
;
4029 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
4030 char intrinsic_name
[40];
4033 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
4034 atomic_emit_memory(ctx
, emit_data
);
4038 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
4039 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4040 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4041 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
4043 LLVMValueRef coords
;
4044 char coords_type
[8];
4046 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
4047 coords
= emit_data
->args
[2];
4049 coords
= emit_data
->args
[1];
4051 ac_build_type_name_for_intr(LLVMTypeOf(coords
), coords_type
, sizeof(coords_type
));
4052 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4053 "llvm.amdgcn.image.atomic.%s.%s",
4054 action
->intr_name
, coords_type
);
4057 tmp
= lp_build_intrinsic(
4058 builder
, intrinsic_name
, bld_base
->uint_bld
.elem_type
,
4059 emit_data
->args
, emit_data
->arg_count
, 0);
4060 emit_data
->output
[emit_data
->chan
] =
4061 LLVMBuildBitCast(builder
, tmp
, bld_base
->base
.elem_type
, "");
4064 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
4065 struct lp_build_emit_data
*emit_data
,
4067 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4068 LLVMValueRef
*param
, unsigned count
,
4071 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4072 struct ac_image_args args
= {};
4074 /* Pad to power of two vector */
4075 while (count
< util_next_power_of_two(count
))
4076 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4079 args
.addr
= lp_build_gather_values(gallivm
, param
, count
);
4081 args
.addr
= param
[0];
4083 args
.resource
= res_ptr
;
4084 args
.sampler
= samp_ptr
;
4086 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
4087 target
== TGSI_TEXTURE_SHADOWRECT
;
4088 args
.da
= tgsi_is_array_sampler(target
);
4090 /* Ugly, but we seem to have no other choice right now. */
4091 STATIC_ASSERT(sizeof(args
) <= sizeof(emit_data
->args
));
4092 memcpy(emit_data
->args
, &args
, sizeof(args
));
4095 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
4096 unsigned target
, LLVMValueRef out
)
4098 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4100 /* 1D textures are allocated and used as 2D on GFX9. */
4101 if (ctx
->screen
->b
.chip_class
>= GFX9
&&
4102 (target
== TGSI_TEXTURE_1D_ARRAY
||
4103 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
4104 LLVMValueRef layers
=
4105 LLVMBuildExtractElement(builder
, out
,
4106 LLVMConstInt(ctx
->i32
, 2, 0), "");
4107 out
= LLVMBuildInsertElement(builder
, out
, layers
,
4108 LLVMConstInt(ctx
->i32
, 1, 0), "");
4111 /* Divide the number of layers by 6 to get the number of cubes. */
4112 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
4113 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4114 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
4116 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
4117 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
4119 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
4124 static void resq_fetch_args(
4125 struct lp_build_tgsi_context
* bld_base
,
4126 struct lp_build_emit_data
* emit_data
)
4128 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4129 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4130 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
4132 emit_data
->dst_type
= ctx
->v4i32
;
4134 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
4135 emit_data
->args
[0] = shader_buffer_fetch_rsrc(ctx
, reg
);
4136 emit_data
->arg_count
= 1;
4137 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4138 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4139 &emit_data
->args
[0]);
4140 emit_data
->arg_count
= 1;
4142 LLVMValueRef res_ptr
;
4143 unsigned image_target
;
4145 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
4146 image_target
= TGSI_TEXTURE_2D_ARRAY
;
4148 image_target
= inst
->Memory
.Texture
;
4150 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4152 set_tex_fetch_args(ctx
, emit_data
, image_target
,
4153 res_ptr
, NULL
, &bld_base
->uint_bld
.zero
, 1,
4158 static void resq_emit(
4159 const struct lp_build_tgsi_action
*action
,
4160 struct lp_build_tgsi_context
*bld_base
,
4161 struct lp_build_emit_data
*emit_data
)
4163 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4164 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4165 LLVMBuilderRef builder
= gallivm
->builder
;
4166 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4169 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
4170 out
= LLVMBuildExtractElement(builder
, emit_data
->args
[0],
4171 lp_build_const_int32(gallivm
, 2), "");
4172 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4173 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
4175 struct ac_image_args args
;
4177 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
4178 args
.opcode
= ac_image_get_resinfo
;
4179 out
= ac_build_image_opcode(&ctx
->ac
, &args
);
4181 out
= fix_resinfo(ctx
, inst
->Memory
.Texture
, out
);
4184 emit_data
->output
[emit_data
->chan
] = out
;
4187 static const struct lp_build_tgsi_action tex_action
;
4197 * Load an image view, fmask view. or sampler state descriptor.
4199 static LLVMValueRef
load_sampler_desc(struct si_shader_context
*ctx
,
4200 LLVMValueRef list
, LLVMValueRef index
,
4201 enum desc_type type
)
4203 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4204 LLVMBuilderRef builder
= gallivm
->builder
;
4208 /* The image is at [0:7]. */
4209 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4212 /* The buffer is in [4:7]. */
4213 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4214 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4215 list
= LLVMBuildPointerCast(builder
, list
,
4216 const_array(ctx
->v4i32
, 0), "");
4219 /* The FMASK is at [8:15]. */
4220 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4221 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4224 /* The sampler state is at [12:15]. */
4225 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4226 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
4227 list
= LLVMBuildPointerCast(builder
, list
,
4228 const_array(ctx
->v4i32
, 0), "");
4232 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4235 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4238 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4239 * filtering manually. The driver sets img7 to a mask clearing
4240 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4241 * s_and_b32 samp0, samp0, img7
4244 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4246 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
4247 LLVMValueRef res
, LLVMValueRef samp
)
4249 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4250 LLVMValueRef img7
, samp0
;
4252 if (ctx
->screen
->b
.chip_class
>= VI
)
4255 img7
= LLVMBuildExtractElement(builder
, res
,
4256 LLVMConstInt(ctx
->i32
, 7, 0), "");
4257 samp0
= LLVMBuildExtractElement(builder
, samp
,
4258 LLVMConstInt(ctx
->i32
, 0, 0), "");
4259 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4260 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4261 LLVMConstInt(ctx
->i32
, 0, 0), "");
4264 static void tex_fetch_ptrs(
4265 struct lp_build_tgsi_context
*bld_base
,
4266 struct lp_build_emit_data
*emit_data
,
4267 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
4269 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4270 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_SAMPLERS
);
4271 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4272 const struct tgsi_full_src_register
*reg
;
4273 unsigned target
= inst
->Texture
.Texture
;
4274 unsigned sampler_src
;
4277 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
4278 reg
= &emit_data
->inst
->Src
[sampler_src
];
4280 if (reg
->Register
.Indirect
) {
4281 index
= get_bounded_indirect_index(ctx
,
4283 reg
->Register
.Index
,
4286 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, 0);
4289 if (target
== TGSI_TEXTURE_BUFFER
)
4290 *res_ptr
= load_sampler_desc(ctx
, list
, index
, DESC_BUFFER
);
4292 *res_ptr
= load_sampler_desc(ctx
, list
, index
, DESC_IMAGE
);
4299 if (target
== TGSI_TEXTURE_2D_MSAA
||
4300 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4302 *fmask_ptr
= load_sampler_desc(ctx
, list
, index
,
4304 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
4306 *samp_ptr
= load_sampler_desc(ctx
, list
, index
,
4308 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4313 static void txq_fetch_args(
4314 struct lp_build_tgsi_context
*bld_base
,
4315 struct lp_build_emit_data
*emit_data
)
4317 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4318 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4319 unsigned target
= inst
->Texture
.Texture
;
4320 LLVMValueRef res_ptr
;
4321 LLVMValueRef address
;
4323 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, NULL
, NULL
);
4325 if (target
== TGSI_TEXTURE_BUFFER
) {
4326 /* Read the size from the buffer descriptor directly. */
4327 emit_data
->args
[0] = get_buffer_size(bld_base
, res_ptr
);
4331 /* Textures - set the mip level. */
4332 address
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
4334 set_tex_fetch_args(ctx
, emit_data
, target
, res_ptr
,
4335 NULL
, &address
, 1, 0xf);
4338 static void txq_emit(const struct lp_build_tgsi_action
*action
,
4339 struct lp_build_tgsi_context
*bld_base
,
4340 struct lp_build_emit_data
*emit_data
)
4342 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4343 struct ac_image_args args
;
4344 unsigned target
= emit_data
->inst
->Texture
.Texture
;
4346 if (target
== TGSI_TEXTURE_BUFFER
) {
4347 /* Just return the buffer size. */
4348 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
4352 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
4354 args
.opcode
= ac_image_get_resinfo
;
4355 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
4357 emit_data
->output
[emit_data
->chan
] = fix_resinfo(ctx
, target
, result
);
4360 static void tex_fetch_args(
4361 struct lp_build_tgsi_context
*bld_base
,
4362 struct lp_build_emit_data
*emit_data
)
4364 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4365 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4366 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4367 unsigned opcode
= inst
->Instruction
.Opcode
;
4368 unsigned target
= inst
->Texture
.Texture
;
4369 LLVMValueRef coords
[5], derivs
[6];
4370 LLVMValueRef address
[16];
4371 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
4372 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
4375 unsigned num_deriv_channels
= 0;
4376 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
4377 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4378 unsigned dmask
= 0xf;
4380 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4382 if (target
== TGSI_TEXTURE_BUFFER
) {
4383 emit_data
->dst_type
= ctx
->v4f32
;
4384 emit_data
->args
[0] = LLVMBuildBitCast(gallivm
->builder
, res_ptr
,
4386 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
4387 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4388 emit_data
->arg_count
= 3;
4392 /* Fetch and project texture coordinates */
4393 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
4394 for (chan
= 0; chan
< 3; chan
++ ) {
4395 coords
[chan
] = lp_build_emit_fetch(bld_base
,
4398 if (opcode
== TGSI_OPCODE_TXP
)
4399 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
4405 if (opcode
== TGSI_OPCODE_TXP
)
4406 coords
[3] = bld_base
->base
.one
;
4410 opcode
!= TGSI_OPCODE_TXF
&&
4411 opcode
!= TGSI_OPCODE_TXF_LZ
) {
4412 /* The offsets are six-bit signed integers packed like this:
4413 * X=[5:0], Y=[13:8], and Z=[21:16].
4415 LLVMValueRef offset
[3], pack
;
4417 assert(inst
->Texture
.NumOffsets
== 1);
4419 for (chan
= 0; chan
< 3; chan
++) {
4420 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
4421 emit_data
->inst
, 0, chan
);
4422 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
4423 lp_build_const_int32(gallivm
, 0x3f), "");
4425 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
4426 lp_build_const_int32(gallivm
, chan
*8), "");
4429 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
4430 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
4431 address
[count
++] = pack
;
4434 /* Pack LOD bias value */
4435 if (opcode
== TGSI_OPCODE_TXB
)
4436 address
[count
++] = coords
[3];
4437 if (opcode
== TGSI_OPCODE_TXB2
)
4438 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4440 /* Pack depth comparison value */
4441 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
4444 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4445 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4447 assert(ref_pos
>= 0);
4448 z
= coords
[ref_pos
];
4451 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4452 * so the depth comparison value isn't clamped for Z16 and
4453 * Z24 anymore. Do it manually here.
4455 * It's unnecessary if the original texture format was
4456 * Z32_FLOAT, but we don't know that here.
4458 if (ctx
->screen
->b
.chip_class
== VI
)
4459 z
= ac_build_clamp(&ctx
->ac
, z
);
4461 address
[count
++] = z
;
4464 /* Pack user derivatives */
4465 if (opcode
== TGSI_OPCODE_TXD
) {
4466 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
4469 case TGSI_TEXTURE_3D
:
4470 num_src_deriv_channels
= 3;
4471 num_dst_deriv_channels
= 3;
4472 num_deriv_channels
= 3;
4474 case TGSI_TEXTURE_2D
:
4475 case TGSI_TEXTURE_SHADOW2D
:
4476 case TGSI_TEXTURE_RECT
:
4477 case TGSI_TEXTURE_SHADOWRECT
:
4478 case TGSI_TEXTURE_2D_ARRAY
:
4479 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4480 num_src_deriv_channels
= 2;
4481 num_dst_deriv_channels
= 2;
4482 num_deriv_channels
= 2;
4484 case TGSI_TEXTURE_CUBE
:
4485 case TGSI_TEXTURE_SHADOWCUBE
:
4486 case TGSI_TEXTURE_CUBE_ARRAY
:
4487 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
4488 /* Cube derivatives will be converted to 2D. */
4489 num_src_deriv_channels
= 3;
4490 num_dst_deriv_channels
= 3;
4491 num_deriv_channels
= 2;
4493 case TGSI_TEXTURE_1D
:
4494 case TGSI_TEXTURE_SHADOW1D
:
4495 case TGSI_TEXTURE_1D_ARRAY
:
4496 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4497 num_src_deriv_channels
= 1;
4499 /* 1D textures are allocated and used as 2D on GFX9. */
4500 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
4501 num_dst_deriv_channels
= 2;
4502 num_deriv_channels
= 2;
4504 num_dst_deriv_channels
= 1;
4505 num_deriv_channels
= 1;
4509 unreachable("invalid target");
4512 for (param
= 0; param
< 2; param
++) {
4513 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
4514 derivs
[param
* num_dst_deriv_channels
+ chan
] =
4515 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
4517 /* Fill in the rest with zeros. */
4518 for (chan
= num_src_deriv_channels
;
4519 chan
< num_dst_deriv_channels
; chan
++)
4520 derivs
[param
* num_dst_deriv_channels
+ chan
] =
4521 bld_base
->base
.zero
;
4525 if (target
== TGSI_TEXTURE_CUBE
||
4526 target
== TGSI_TEXTURE_CUBE_ARRAY
||
4527 target
== TGSI_TEXTURE_SHADOWCUBE
||
4528 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
4529 ac_prepare_cube_coords(&ctx
->ac
,
4530 opcode
== TGSI_OPCODE_TXD
,
4531 target
== TGSI_TEXTURE_CUBE_ARRAY
||
4532 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
4535 if (opcode
== TGSI_OPCODE_TXD
)
4536 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
4537 address
[count
++] = derivs
[i
];
4539 /* Pack texture coordinates */
4540 address
[count
++] = coords
[0];
4542 address
[count
++] = coords
[1];
4544 address
[count
++] = coords
[2];
4546 /* 1D textures are allocated and used as 2D on GFX9. */
4547 if (ctx
->screen
->b
.chip_class
>= GFX9
) {
4548 LLVMValueRef filler
;
4550 /* Use 0.5, so that we don't sample the border color. */
4551 if (opcode
== TGSI_OPCODE_TXF
)
4552 filler
= bld_base
->uint_bld
.zero
;
4554 filler
= LLVMConstReal(ctx
->f32
, 0.5);
4556 if (target
== TGSI_TEXTURE_1D
||
4557 target
== TGSI_TEXTURE_SHADOW1D
) {
4558 address
[count
++] = filler
;
4559 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
4560 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
4561 address
[count
] = address
[count
- 1];
4562 address
[count
- 1] = filler
;
4567 /* Pack LOD or sample index */
4568 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
4569 address
[count
++] = coords
[3];
4570 else if (opcode
== TGSI_OPCODE_TXL2
)
4571 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4574 assert(!"Cannot handle more than 16 texture address parameters");
4578 for (chan
= 0; chan
< count
; chan
++ ) {
4579 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
4580 address
[chan
], ctx
->i32
, "");
4583 /* Adjust the sample index according to FMASK.
4585 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4586 * which is the identity mapping. Each nibble says which physical sample
4587 * should be fetched to get that sample.
4589 * For example, 0x11111100 means there are only 2 samples stored and
4590 * the second sample covers 3/4 of the pixel. When reading samples 0
4591 * and 1, return physical sample 0 (determined by the first two 0s
4592 * in FMASK), otherwise return physical sample 1.
4594 * The sample index should be adjusted as follows:
4595 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4597 if (target
== TGSI_TEXTURE_2D_MSAA
||
4598 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4599 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4600 struct lp_build_emit_data txf_emit_data
= *emit_data
;
4601 LLVMValueRef txf_address
[4];
4602 /* We only need .xy for non-arrays, and .xyz for arrays. */
4603 unsigned txf_count
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
4604 struct tgsi_full_instruction inst
= {};
4606 memcpy(txf_address
, address
, sizeof(txf_address
));
4608 /* Read FMASK using TXF_LZ. */
4609 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF_LZ
;
4610 inst
.Texture
.Texture
= target
;
4611 txf_emit_data
.inst
= &inst
;
4612 txf_emit_data
.chan
= 0;
4613 set_tex_fetch_args(ctx
, &txf_emit_data
,
4614 target
, fmask_ptr
, NULL
,
4615 txf_address
, txf_count
, 0xf);
4616 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
4618 /* Initialize some constants. */
4619 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
4620 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
4622 /* Apply the formula. */
4623 LLVMValueRef fmask
=
4624 LLVMBuildExtractElement(gallivm
->builder
,
4625 txf_emit_data
.output
[0],
4626 uint_bld
->zero
, "");
4628 unsigned sample_chan
= txf_count
; /* the sample index is last */
4630 LLVMValueRef sample_index4
=
4631 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
4633 LLVMValueRef shifted_fmask
=
4634 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
4636 LLVMValueRef final_sample
=
4637 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
4639 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4640 * resource descriptor is 0 (invalid),
4642 LLVMValueRef fmask_desc
=
4643 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
4646 LLVMValueRef fmask_word1
=
4647 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
4650 LLVMValueRef word1_is_nonzero
=
4651 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
4652 fmask_word1
, uint_bld
->zero
, "");
4654 /* Replace the MSAA sample index. */
4655 address
[sample_chan
] =
4656 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
4657 final_sample
, address
[sample_chan
], "");
4660 if (opcode
== TGSI_OPCODE_TXF
||
4661 opcode
== TGSI_OPCODE_TXF_LZ
) {
4662 /* add tex offsets */
4663 if (inst
->Texture
.NumOffsets
) {
4664 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4665 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
4667 assert(inst
->Texture
.NumOffsets
== 1);
4670 case TGSI_TEXTURE_3D
:
4671 address
[2] = lp_build_add(uint_bld
, address
[2],
4672 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
]);
4674 case TGSI_TEXTURE_2D
:
4675 case TGSI_TEXTURE_SHADOW2D
:
4676 case TGSI_TEXTURE_RECT
:
4677 case TGSI_TEXTURE_SHADOWRECT
:
4678 case TGSI_TEXTURE_2D_ARRAY
:
4679 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4681 lp_build_add(uint_bld
, address
[1],
4682 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
]);
4684 case TGSI_TEXTURE_1D
:
4685 case TGSI_TEXTURE_SHADOW1D
:
4686 case TGSI_TEXTURE_1D_ARRAY
:
4687 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4689 lp_build_add(uint_bld
, address
[0],
4690 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
]);
4692 /* texture offsets do not apply to other texture targets */
4697 if (opcode
== TGSI_OPCODE_TG4
) {
4698 unsigned gather_comp
= 0;
4700 /* DMASK was repurposed for GATHER4. 4 components are always
4701 * returned and DMASK works like a swizzle - it selects
4702 * the component to fetch. The only valid DMASK values are
4703 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4704 * (red,red,red,red) etc.) The ISA document doesn't mention
4708 /* Get the component index from src1.x for Gather4. */
4709 if (!tgsi_is_shadow_target(target
)) {
4710 LLVMValueRef comp_imm
;
4711 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
4713 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
4715 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
4716 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
4717 gather_comp
= CLAMP(gather_comp
, 0, 3);
4720 dmask
= 1 << gather_comp
;
4723 set_tex_fetch_args(ctx
, emit_data
, target
, res_ptr
,
4724 samp_ptr
, address
, count
, dmask
);
4727 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
4728 * incorrectly forces nearest filtering if the texture format is integer.
4729 * The only effect it has on Gather4, which always returns 4 texels for
4730 * bilinear filtering, is that the final coordinates are off by 0.5 of
4733 * The workaround is to subtract 0.5 from the unnormalized coordinates,
4734 * or (0.5 / size) from the normalized coordinates.
4736 static void si_lower_gather4_integer(struct si_shader_context
*ctx
,
4737 struct ac_image_args
*args
,
4740 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4741 LLVMValueRef coord
= args
->addr
;
4742 LLVMValueRef half_texel
[2];
4743 /* Texture coordinates start after:
4744 * {offset, bias, z-compare, derivatives}
4745 * Only the offset and z-compare can occur here.
4747 unsigned coord_vgpr_index
= (int)args
->offset
+ (int)args
->compare
;
4750 if (target
== TGSI_TEXTURE_RECT
||
4751 target
== TGSI_TEXTURE_SHADOWRECT
) {
4752 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
4754 struct tgsi_full_instruction txq_inst
= {};
4755 struct lp_build_emit_data txq_emit_data
= {};
4757 /* Query the texture size. */
4758 txq_inst
.Texture
.Texture
= target
;
4759 txq_emit_data
.inst
= &txq_inst
;
4760 txq_emit_data
.dst_type
= ctx
->v4i32
;
4761 set_tex_fetch_args(ctx
, &txq_emit_data
, target
,
4762 args
->resource
, NULL
,
4763 &ctx
->bld_base
.uint_bld
.zero
,
4765 txq_emit(NULL
, &ctx
->bld_base
, &txq_emit_data
);
4767 /* Compute -0.5 / size. */
4768 for (c
= 0; c
< 2; c
++) {
4770 LLVMBuildExtractElement(builder
, txq_emit_data
.output
[0],
4771 LLVMConstInt(ctx
->i32
, c
, 0), "");
4772 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
4774 lp_build_emit_llvm_unary(&ctx
->bld_base
,
4775 TGSI_OPCODE_RCP
, half_texel
[c
]);
4776 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
4777 LLVMConstReal(ctx
->f32
, -0.5), "");
4781 for (c
= 0; c
< 2; c
++) {
4783 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
4785 tmp
= LLVMBuildExtractElement(builder
, coord
, index
, "");
4786 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->f32
, "");
4787 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
4788 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->i32
, "");
4789 coord
= LLVMBuildInsertElement(builder
, coord
, tmp
, index
, "");
4795 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
4796 struct lp_build_tgsi_context
*bld_base
,
4797 struct lp_build_emit_data
*emit_data
)
4799 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4800 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4801 struct ac_image_args args
;
4802 unsigned opcode
= inst
->Instruction
.Opcode
;
4803 unsigned target
= inst
->Texture
.Texture
;
4805 if (target
== TGSI_TEXTURE_BUFFER
) {
4806 emit_data
->output
[emit_data
->chan
] =
4807 ac_build_buffer_load_format(&ctx
->ac
,
4815 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
4817 args
.opcode
= ac_image_sample
;
4818 args
.compare
= tgsi_is_shadow_target(target
);
4819 args
.offset
= inst
->Texture
.NumOffsets
> 0;
4822 case TGSI_OPCODE_TXF
:
4823 case TGSI_OPCODE_TXF_LZ
:
4824 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
4825 target
== TGSI_TEXTURE_2D_MSAA
||
4826 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
4827 ac_image_load
: ac_image_load_mip
;
4828 args
.compare
= false;
4829 args
.offset
= false;
4831 case TGSI_OPCODE_LODQ
:
4832 args
.opcode
= ac_image_get_lod
;
4833 args
.compare
= false;
4834 args
.offset
= false;
4836 case TGSI_OPCODE_TEX
:
4837 case TGSI_OPCODE_TEX2
:
4838 case TGSI_OPCODE_TXP
:
4839 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
4840 args
.level_zero
= true;
4842 case TGSI_OPCODE_TEX_LZ
:
4843 args
.level_zero
= true;
4845 case TGSI_OPCODE_TXB
:
4846 case TGSI_OPCODE_TXB2
:
4847 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
4850 case TGSI_OPCODE_TXL
:
4851 case TGSI_OPCODE_TXL2
:
4854 case TGSI_OPCODE_TXD
:
4857 case TGSI_OPCODE_TG4
:
4858 args
.opcode
= ac_image_gather4
;
4859 args
.level_zero
= true;
4866 /* The hardware needs special lowering for Gather4 with integer formats. */
4867 if (ctx
->screen
->b
.chip_class
<= VI
&&
4868 opcode
== TGSI_OPCODE_TG4
) {
4869 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4870 /* This will also work with non-constant indexing because of how
4871 * glsl_to_tgsi works and we intent to preserve that behavior.
4873 const unsigned src_idx
= 2;
4874 unsigned sampler
= inst
->Src
[src_idx
].Register
.Index
;
4876 assert(inst
->Src
[src_idx
].Register
.File
== TGSI_FILE_SAMPLER
);
4878 if (info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_SINT
||
4879 info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_UINT
)
4880 si_lower_gather4_integer(ctx
, &args
, target
);
4883 emit_data
->output
[emit_data
->chan
] =
4884 ac_build_image_opcode(&ctx
->ac
, &args
);
4887 static void si_llvm_emit_txqs(
4888 const struct lp_build_tgsi_action
*action
,
4889 struct lp_build_tgsi_context
*bld_base
,
4890 struct lp_build_emit_data
*emit_data
)
4892 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4893 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4894 LLVMBuilderRef builder
= gallivm
->builder
;
4895 LLVMValueRef res
, samples
;
4896 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4898 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4901 /* Read the samples from the descriptor directly. */
4902 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
4903 samples
= LLVMBuildExtractElement(
4905 lp_build_const_int32(gallivm
, 3), "");
4906 samples
= LLVMBuildLShr(builder
, samples
,
4907 lp_build_const_int32(gallivm
, 16), "");
4908 samples
= LLVMBuildAnd(builder
, samples
,
4909 lp_build_const_int32(gallivm
, 0xf), "");
4910 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
4913 emit_data
->output
[emit_data
->chan
] = samples
;
4916 static void si_llvm_emit_ddxy(
4917 const struct lp_build_tgsi_action
*action
,
4918 struct lp_build_tgsi_context
*bld_base
,
4919 struct lp_build_emit_data
*emit_data
)
4921 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4922 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4923 unsigned opcode
= emit_data
->info
->opcode
;
4928 if (opcode
== TGSI_OPCODE_DDX_FINE
)
4929 mask
= AC_TID_MASK_LEFT
;
4930 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
4931 mask
= AC_TID_MASK_TOP
;
4933 mask
= AC_TID_MASK_TOP_LEFT
;
4935 /* for DDX we want to next X pixel, DDY next Y pixel. */
4936 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
4938 val
= LLVMBuildBitCast(gallivm
->builder
, emit_data
->args
[0], ctx
->i32
, "");
4939 val
= ac_build_ddxy(&ctx
->ac
, ctx
->screen
->has_ds_bpermute
,
4940 mask
, idx
, ctx
->lds
, val
);
4941 emit_data
->output
[emit_data
->chan
] = val
;
4945 * this takes an I,J coordinate pair,
4946 * and works out the X and Y derivatives.
4947 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4949 static LLVMValueRef
si_llvm_emit_ddxy_interp(
4950 struct lp_build_tgsi_context
*bld_base
,
4951 LLVMValueRef interp_ij
)
4953 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4954 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4955 LLVMValueRef result
[4], a
;
4958 for (i
= 0; i
< 2; i
++) {
4959 a
= LLVMBuildExtractElement(gallivm
->builder
, interp_ij
,
4960 LLVMConstInt(ctx
->i32
, i
, 0), "");
4961 result
[i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDX
, a
);
4962 result
[2+i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDY
, a
);
4965 return lp_build_gather_values(gallivm
, result
, 4);
4968 static void interp_fetch_args(
4969 struct lp_build_tgsi_context
*bld_base
,
4970 struct lp_build_emit_data
*emit_data
)
4972 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4973 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4974 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4976 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
4977 /* offset is in second src, first two channels */
4978 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
4981 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
4984 emit_data
->arg_count
= 2;
4985 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
4986 LLVMValueRef sample_position
;
4987 LLVMValueRef sample_id
;
4988 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
4990 /* fetch sample ID, then fetch its sample position,
4991 * and place into first two channels.
4993 sample_id
= lp_build_emit_fetch(bld_base
,
4994 emit_data
->inst
, 1, TGSI_CHAN_X
);
4995 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
4997 sample_position
= load_sample_position(ctx
, sample_id
);
4999 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
5001 lp_build_const_int32(gallivm
, 0), "");
5003 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
5004 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
5006 lp_build_const_int32(gallivm
, 1), "");
5007 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
5008 emit_data
->arg_count
= 2;
5012 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
5013 struct lp_build_tgsi_context
*bld_base
,
5014 struct lp_build_emit_data
*emit_data
)
5016 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5017 struct si_shader
*shader
= ctx
->shader
;
5018 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5019 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5020 LLVMValueRef interp_param
;
5021 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
5022 int input_index
= inst
->Src
[0].Register
.Index
;
5025 LLVMValueRef attr_number
;
5026 LLVMValueRef params
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_PRIM_MASK
);
5027 int interp_param_idx
;
5028 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
5031 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
5033 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5034 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
5035 location
= TGSI_INTERPOLATE_LOC_CENTER
;
5037 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
5039 interp_param_idx
= lookup_interp_param_index(interp
, location
);
5040 if (interp_param_idx
== -1)
5042 else if (interp_param_idx
)
5043 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
5045 interp_param
= NULL
;
5047 attr_number
= lp_build_const_int32(gallivm
, input_index
);
5049 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5050 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
5051 LLVMValueRef ij_out
[2];
5052 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
5055 * take the I then J parameters, and the DDX/Y for it, and
5056 * calculate the IJ inputs for the interpolator.
5057 * temp1 = ddx * offset/sample.x + I;
5058 * interp_param.I = ddy * offset/sample.y + temp1;
5059 * temp1 = ddx * offset/sample.x + J;
5060 * interp_param.J = ddy * offset/sample.y + temp1;
5062 for (i
= 0; i
< 2; i
++) {
5063 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
5064 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
5065 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
5066 ddxy_out
, ix_ll
, "");
5067 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
5068 ddxy_out
, iy_ll
, "");
5069 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
5070 interp_param
, ix_ll
, "");
5071 LLVMValueRef temp1
, temp2
;
5073 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
5076 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
5078 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
5080 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
5082 ij_out
[i
] = LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
5084 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
5087 for (chan
= 0; chan
< 4; chan
++) {
5088 LLVMValueRef llvm_chan
;
5091 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
5092 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
5095 interp_param
= LLVMBuildBitCast(gallivm
->builder
,
5096 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
5097 LLVMValueRef i
= LLVMBuildExtractElement(
5098 gallivm
->builder
, interp_param
, uint
->zero
, "");
5099 LLVMValueRef j
= LLVMBuildExtractElement(
5100 gallivm
->builder
, interp_param
, uint
->one
, "");
5101 emit_data
->output
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5102 llvm_chan
, attr_number
, params
,
5105 emit_data
->output
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5106 lp_build_const_int32(gallivm
, 2), /* P0 */
5107 llvm_chan
, attr_number
, params
);
5112 static LLVMValueRef
si_emit_ballot(struct si_shader_context
*ctx
,
5115 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5116 LLVMValueRef args
[3] = {
5119 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
5122 if (LLVMTypeOf(value
) != ctx
->i32
)
5123 args
[0] = LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
5125 return lp_build_intrinsic(gallivm
->builder
,
5126 "llvm.amdgcn.icmp.i32",
5128 LP_FUNC_ATTR_NOUNWIND
|
5129 LP_FUNC_ATTR_READNONE
|
5130 LP_FUNC_ATTR_CONVERGENT
);
5133 static void vote_all_emit(
5134 const struct lp_build_tgsi_action
*action
,
5135 struct lp_build_tgsi_context
*bld_base
,
5136 struct lp_build_emit_data
*emit_data
)
5138 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5139 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5140 LLVMValueRef active_set
, vote_set
;
5143 active_set
= si_emit_ballot(ctx
, ctx
->i32_1
);
5144 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
5146 tmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
5147 emit_data
->output
[emit_data
->chan
] =
5148 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
5151 static void vote_any_emit(
5152 const struct lp_build_tgsi_action
*action
,
5153 struct lp_build_tgsi_context
*bld_base
,
5154 struct lp_build_emit_data
*emit_data
)
5156 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5157 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5158 LLVMValueRef vote_set
;
5161 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
5163 tmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
5164 vote_set
, LLVMConstInt(ctx
->i64
, 0, 0), "");
5165 emit_data
->output
[emit_data
->chan
] =
5166 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
5169 static void vote_eq_emit(
5170 const struct lp_build_tgsi_action
*action
,
5171 struct lp_build_tgsi_context
*bld_base
,
5172 struct lp_build_emit_data
*emit_data
)
5174 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5175 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5176 LLVMValueRef active_set
, vote_set
;
5177 LLVMValueRef all
, none
, tmp
;
5179 active_set
= si_emit_ballot(ctx
, ctx
->i32_1
);
5180 vote_set
= si_emit_ballot(ctx
, emit_data
->args
[0]);
5182 all
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
5183 none
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
5184 vote_set
, LLVMConstInt(ctx
->i64
, 0, 0), "");
5185 tmp
= LLVMBuildOr(gallivm
->builder
, all
, none
, "");
5186 emit_data
->output
[emit_data
->chan
] =
5187 LLVMBuildSExt(gallivm
->builder
, tmp
, ctx
->i32
, "");
5190 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
5191 struct lp_build_emit_data
*emit_data
)
5193 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5194 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
5198 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
5200 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
5201 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
5205 /* Emit one vertex from the geometry shader */
5206 static void si_llvm_emit_vertex(
5207 const struct lp_build_tgsi_action
*action
,
5208 struct lp_build_tgsi_context
*bld_base
,
5209 struct lp_build_emit_data
*emit_data
)
5211 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5212 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5213 struct si_shader
*shader
= ctx
->shader
;
5214 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5215 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5216 struct lp_build_if_state if_state
;
5217 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
5218 SI_PARAM_GS2VS_OFFSET
);
5219 LLVMValueRef gs_next_vertex
;
5220 LLVMValueRef can_emit
, kill
;
5221 unsigned chan
, offset
;
5225 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5227 /* Write vertex attribute values to GSVS ring */
5228 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
5229 ctx
->gs_next_vertex
[stream
],
5232 /* If this thread has already emitted the declared maximum number of
5233 * vertices, skip the write: excessive vertex emissions are not
5234 * supposed to have any effect.
5236 * If the shader has no writes to memory, kill it instead. This skips
5237 * further memory loads and may allow LLVM to skip to the end
5240 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULT
, gs_next_vertex
,
5241 lp_build_const_int32(gallivm
,
5242 shader
->selector
->gs_max_out_vertices
), "");
5244 bool use_kill
= !info
->writes_memory
;
5246 kill
= lp_build_select(&bld_base
->base
, can_emit
,
5247 lp_build_const_float(gallivm
, 1.0f
),
5248 lp_build_const_float(gallivm
, -1.0f
));
5250 ac_build_kill(&ctx
->ac
, kill
);
5252 lp_build_if(&if_state
, gallivm
, can_emit
);
5256 for (i
= 0; i
< info
->num_outputs
; i
++) {
5257 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
5259 for (chan
= 0; chan
< 4; chan
++) {
5260 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
5261 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
5264 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
5265 LLVMValueRef voffset
=
5266 lp_build_const_int32(gallivm
, offset
*
5267 shader
->selector
->gs_max_out_vertices
);
5270 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
5271 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
5273 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
5275 ac_build_buffer_store_dword(&ctx
->ac
,
5276 ctx
->gsvs_ring
[stream
],
5278 voffset
, soffset
, 0,
5283 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
5284 lp_build_const_int32(gallivm
, 1));
5286 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
5288 /* Signal vertex emission */
5289 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
5290 LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
));
5292 lp_build_endif(&if_state
);
5295 /* Cut one primitive from the geometry shader */
5296 static void si_llvm_emit_primitive(
5297 const struct lp_build_tgsi_action
*action
,
5298 struct lp_build_tgsi_context
*bld_base
,
5299 struct lp_build_emit_data
*emit_data
)
5301 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5304 /* Signal primitive cut */
5305 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5306 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8),
5307 LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
));
5310 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
5311 struct lp_build_tgsi_context
*bld_base
,
5312 struct lp_build_emit_data
*emit_data
)
5314 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5315 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5317 /* SI only (thanks to a hw bug workaround):
5318 * The real barrier instruction isn’t needed, because an entire patch
5319 * always fits into a single wave.
5321 if (HAVE_LLVM
>= 0x0309 &&
5322 ctx
->screen
->b
.chip_class
== SI
&&
5323 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
5324 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
5328 lp_build_intrinsic(gallivm
->builder
,
5329 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
5330 : "llvm.AMDGPU.barrier.local",
5331 ctx
->voidt
, NULL
, 0, LP_FUNC_ATTR_CONVERGENT
);
5334 static const struct lp_build_tgsi_action tex_action
= {
5335 .fetch_args
= tex_fetch_args
,
5336 .emit
= build_tex_intrinsic
,
5339 static const struct lp_build_tgsi_action interp_action
= {
5340 .fetch_args
= interp_fetch_args
,
5341 .emit
= build_interp_intrinsic
,
5344 static void si_create_function(struct si_shader_context
*ctx
,
5346 LLVMTypeRef
*returns
, unsigned num_returns
,
5347 LLVMTypeRef
*params
, unsigned num_params
,
5352 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
5353 params
, num_params
);
5354 si_llvm_shader_type(ctx
->main_fn
, ctx
->type
);
5355 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
5357 for (i
= 0; i
<= last_sgpr
; ++i
) {
5358 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
5360 /* The combination of:
5364 * allows the optimization passes to move loads and reduces
5365 * SGPR spilling significantly.
5367 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
5368 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_BYVAL
);
5369 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
5370 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
5372 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_INREG
);
5375 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5376 "no-signed-zeros-fp-math",
5379 if (ctx
->screen
->b
.debug_flags
& DBG_UNSAFE_MATH
) {
5380 /* These were copied from some LLVM test. */
5381 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5382 "less-precise-fpmad",
5384 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5387 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5390 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5396 static void declare_streamout_params(struct si_shader_context
*ctx
,
5397 struct pipe_stream_output_info
*so
,
5398 LLVMTypeRef
*params
, LLVMTypeRef i32
,
5399 unsigned *num_params
)
5403 /* Streamout SGPRs. */
5404 if (so
->num_outputs
) {
5405 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
5406 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
5408 ctx
->param_streamout_config
= *num_params
- 1;
5410 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
5412 /* A streamout buffer offset is loaded if the stride is non-zero. */
5413 for (i
= 0; i
< 4; i
++) {
5417 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
5421 static unsigned llvm_get_type_size(LLVMTypeRef type
)
5423 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
5426 case LLVMIntegerTypeKind
:
5427 return LLVMGetIntTypeWidth(type
) / 8;
5428 case LLVMFloatTypeKind
:
5430 case LLVMPointerTypeKind
:
5432 case LLVMVectorTypeKind
:
5433 return LLVMGetVectorSize(type
) *
5434 llvm_get_type_size(LLVMGetElementType(type
));
5435 case LLVMArrayTypeKind
:
5436 return LLVMGetArrayLength(type
) *
5437 llvm_get_type_size(LLVMGetElementType(type
));
5444 static void declare_tess_lds(struct si_shader_context
*ctx
)
5446 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5447 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5448 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5450 unsigned lds_size
= ctx
->screen
->b
.chip_class
>= CIK
? 65536 : 32768;
5451 ctx
->lds
= LLVMBuildIntToPtr(gallivm
->builder
, uint
->zero
,
5452 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
5456 static unsigned si_get_max_workgroup_size(struct si_shader
*shader
)
5458 const unsigned *properties
= shader
->selector
->info
.properties
;
5459 unsigned max_work_group_size
=
5460 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
5461 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
5462 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
5464 if (!max_work_group_size
) {
5465 /* This is a variable group size compute shader,
5466 * compile it for the maximum possible group size.
5468 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
5470 return max_work_group_size
;
5473 static void create_function(struct si_shader_context
*ctx
)
5475 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5476 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5477 struct si_shader
*shader
= ctx
->shader
;
5478 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_MAX_ATTRIBS
], v3i32
;
5479 LLVMTypeRef returns
[16+32*4];
5480 unsigned i
, last_sgpr
, num_params
, num_return_sgprs
;
5481 unsigned num_returns
= 0;
5482 unsigned num_prolog_vgprs
= 0;
5484 v3i32
= LLVMVectorType(ctx
->i32
, 3);
5486 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
5487 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
5488 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
5489 params
[SI_PARAM_IMAGES
] = const_array(ctx
->v8i32
, SI_NUM_IMAGES
);
5490 params
[SI_PARAM_SHADER_BUFFERS
] = const_array(ctx
->v4i32
, SI_NUM_SHADER_BUFFERS
);
5492 switch (ctx
->type
) {
5493 case PIPE_SHADER_VERTEX
:
5494 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_MAX_ATTRIBS
);
5495 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
5496 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
5497 params
[SI_PARAM_DRAWID
] = ctx
->i32
;
5498 num_params
= SI_PARAM_DRAWID
+1;
5500 if (shader
->key
.as_es
) {
5501 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5502 } else if (shader
->key
.as_ls
) {
5503 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
5504 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
5506 if (shader
->is_gs_copy_shader
) {
5507 num_params
= SI_PARAM_RW_BUFFERS
+1;
5509 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
5510 num_params
= SI_PARAM_VS_STATE_BITS
+1;
5513 /* The locations of the other parameters are assigned dynamically. */
5514 declare_streamout_params(ctx
, &shader
->selector
->so
,
5515 params
, ctx
->i32
, &num_params
);
5518 last_sgpr
= num_params
-1;
5521 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
5522 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
5523 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
5524 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
5526 if (!shader
->is_gs_copy_shader
) {
5527 /* Vertex load indices. */
5528 ctx
->param_vertex_index0
= num_params
;
5530 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
5531 params
[num_params
++] = ctx
->i32
;
5533 num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
5535 /* PrimitiveID output. */
5536 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
)
5537 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5538 returns
[num_returns
++] = ctx
->f32
;
5542 case PIPE_SHADER_TESS_CTRL
:
5543 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5544 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
5545 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
5546 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
5547 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
5548 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
5549 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5552 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
5553 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
5554 num_params
= SI_PARAM_REL_IDS
+1;
5556 /* SI_PARAM_TCS_OC_LDS and PARAM_TESS_FACTOR_OFFSET are
5557 * placed after the user SGPRs.
5559 for (i
= 0; i
< SI_TCS_NUM_USER_SGPR
+ 2; i
++)
5560 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
5562 for (i
= 0; i
< 3; i
++)
5563 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
5566 case PIPE_SHADER_TESS_EVAL
:
5567 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5568 num_params
= SI_PARAM_TCS_OFFCHIP_LAYOUT
+1;
5570 if (shader
->key
.as_es
) {
5571 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5572 params
[num_params
++] = ctx
->i32
;
5573 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5575 params
[num_params
++] = ctx
->i32
;
5576 declare_streamout_params(ctx
, &shader
->selector
->so
,
5577 params
, ctx
->i32
, &num_params
);
5578 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5580 last_sgpr
= num_params
- 1;
5583 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
5584 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
5585 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
5586 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
5588 /* PrimitiveID output. */
5589 if (!shader
->key
.as_es
)
5590 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5591 returns
[num_returns
++] = ctx
->f32
;
5594 case PIPE_SHADER_GEOMETRY
:
5595 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
5596 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
5597 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
5600 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
5601 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
5602 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
5603 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
5604 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
5605 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
5606 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
5607 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
5608 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
5611 case PIPE_SHADER_FRAGMENT
:
5612 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
5613 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
5614 last_sgpr
= SI_PARAM_PRIM_MASK
;
5615 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
5616 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
5617 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
5618 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
5619 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
5620 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
5621 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
5622 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
5623 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
5624 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
5625 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
5626 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
5627 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
5628 shader
->info
.face_vgpr_index
= 20;
5629 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
5630 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
5631 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
5632 num_params
= SI_PARAM_POS_FIXED_PT
+1;
5634 /* Color inputs from the prolog. */
5635 if (shader
->selector
->info
.colors_read
) {
5636 unsigned num_color_elements
=
5637 util_bitcount(shader
->selector
->info
.colors_read
);
5639 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
5640 for (i
= 0; i
< num_color_elements
; i
++)
5641 params
[num_params
++] = ctx
->f32
;
5643 num_prolog_vgprs
+= num_color_elements
;
5646 /* Outputs for the epilog. */
5647 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
5650 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
5651 shader
->selector
->info
.writes_z
+
5652 shader
->selector
->info
.writes_stencil
+
5653 shader
->selector
->info
.writes_samplemask
+
5654 1 /* SampleMaskIn */;
5656 num_returns
= MAX2(num_returns
,
5658 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5660 for (i
= 0; i
< num_return_sgprs
; i
++)
5661 returns
[i
] = ctx
->i32
;
5662 for (; i
< num_returns
; i
++)
5663 returns
[i
] = ctx
->f32
;
5666 case PIPE_SHADER_COMPUTE
:
5667 params
[SI_PARAM_GRID_SIZE
] = v3i32
;
5668 params
[SI_PARAM_BLOCK_SIZE
] = v3i32
;
5669 params
[SI_PARAM_BLOCK_ID
] = v3i32
;
5670 last_sgpr
= SI_PARAM_BLOCK_ID
;
5672 params
[SI_PARAM_THREAD_ID
] = v3i32
;
5673 num_params
= SI_PARAM_THREAD_ID
+ 1;
5676 assert(0 && "unimplemented shader");
5680 assert(num_params
<= ARRAY_SIZE(params
));
5682 si_create_function(ctx
, "main", returns
, num_returns
, params
,
5683 num_params
, last_sgpr
);
5685 /* Reserve register locations for VGPR inputs the PS prolog may need. */
5686 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&&
5687 ctx
->separate_prolog
) {
5688 si_llvm_add_attribute(ctx
->main_fn
,
5689 "InitialPSInputAddr",
5690 S_0286D0_PERSP_SAMPLE_ENA(1) |
5691 S_0286D0_PERSP_CENTER_ENA(1) |
5692 S_0286D0_PERSP_CENTROID_ENA(1) |
5693 S_0286D0_LINEAR_SAMPLE_ENA(1) |
5694 S_0286D0_LINEAR_CENTER_ENA(1) |
5695 S_0286D0_LINEAR_CENTROID_ENA(1) |
5696 S_0286D0_FRONT_FACE_ENA(1) |
5697 S_0286D0_POS_FIXED_PT_ENA(1));
5698 } else if (ctx
->type
== PIPE_SHADER_COMPUTE
) {
5699 si_llvm_add_attribute(ctx
->main_fn
,
5700 "amdgpu-max-work-group-size",
5701 si_get_max_workgroup_size(shader
));
5704 shader
->info
.num_input_sgprs
= 0;
5705 shader
->info
.num_input_vgprs
= 0;
5707 for (i
= 0; i
<= last_sgpr
; ++i
)
5708 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
5710 for (; i
< num_params
; ++i
)
5711 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
5713 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5714 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5716 if (!ctx
->screen
->has_ds_bpermute
&&
5718 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
5719 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
5720 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
5721 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
5722 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
5723 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
5725 LLVMAddGlobalInAddressSpace(gallivm
->module
,
5726 LLVMArrayType(ctx
->i32
, 64),
5730 if ((ctx
->type
== PIPE_SHADER_VERTEX
&& shader
->key
.as_ls
) ||
5731 ctx
->type
== PIPE_SHADER_TESS_CTRL
)
5732 declare_tess_lds(ctx
);
5736 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5739 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5741 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
5742 LLVMBuilderRef builder
= gallivm
->builder
;
5744 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5745 SI_PARAM_RW_BUFFERS
);
5747 if ((ctx
->type
== PIPE_SHADER_VERTEX
&&
5748 ctx
->shader
->key
.as_es
) ||
5749 (ctx
->type
== PIPE_SHADER_TESS_EVAL
&&
5750 ctx
->shader
->key
.as_es
) ||
5751 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5753 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5755 LLVMValueRef offset
= lp_build_const_int32(gallivm
, ring
);
5758 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5761 if (ctx
->shader
->is_gs_copy_shader
) {
5762 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
5765 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5766 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5767 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5768 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
5769 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
5770 LLVMValueRef base_ring
;
5772 base_ring
= ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5774 /* The conceptual layout of the GSVS ring is
5775 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5776 * but the real memory layout is swizzled across
5778 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5780 * Override the buffer descriptor accordingly.
5782 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5783 uint64_t stream_offset
= 0;
5785 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5786 unsigned num_components
;
5788 unsigned num_records
;
5789 LLVMValueRef ring
, tmp
;
5791 num_components
= sel
->info
.num_stream_output_components
[stream
];
5792 if (!num_components
)
5795 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5797 /* Limit on the stride field for <= CIK. */
5798 assert(stride
< (1 << 14));
5802 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5803 tmp
= LLVMBuildExtractElement(builder
, ring
, uint
->zero
, "");
5804 tmp
= LLVMBuildAdd(builder
, tmp
,
5805 LLVMConstInt(ctx
->i64
,
5806 stream_offset
, 0), "");
5807 stream_offset
+= stride
* 64;
5809 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, uint
->zero
, "");
5810 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5811 tmp
= LLVMBuildExtractElement(builder
, ring
, uint
->one
, "");
5812 tmp
= LLVMBuildOr(builder
, tmp
,
5813 LLVMConstInt(ctx
->i32
,
5814 S_008F04_STRIDE(stride
) |
5815 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5816 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, uint
->one
, "");
5817 ring
= LLVMBuildInsertElement(builder
, ring
,
5818 LLVMConstInt(ctx
->i32
, num_records
, 0),
5819 LLVMConstInt(ctx
->i32
, 2, 0), "");
5820 ring
= LLVMBuildInsertElement(builder
, ring
,
5821 LLVMConstInt(ctx
->i32
,
5822 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5823 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5824 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5825 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5826 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5827 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5828 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5829 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5830 S_008F0C_ADD_TID_ENABLE(1),
5832 LLVMConstInt(ctx
->i32
, 3, 0), "");
5833 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v16i8
, "");
5835 ctx
->gsvs_ring
[stream
] = ring
;
5840 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5841 LLVMValueRef param_rw_buffers
,
5842 unsigned param_pos_fixed_pt
)
5844 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5845 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5846 LLVMBuilderRef builder
= gallivm
->builder
;
5847 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5849 /* Use the fixed-point gl_FragCoord input.
5850 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5851 * per coordinate to get the repeating effect.
5853 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5854 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5856 /* Load the buffer descriptor. */
5857 slot
= lp_build_const_int32(gallivm
, SI_PS_CONST_POLY_STIPPLE
);
5858 desc
= ac_build_indexed_load_const(&ctx
->ac
, param_rw_buffers
, slot
);
5860 /* The stipple pattern is 32x32, each row has 32 bits. */
5861 offset
= LLVMBuildMul(builder
, address
[1],
5862 LLVMConstInt(ctx
->i32
, 4, 0), "");
5863 row
= buffer_load_const(ctx
, desc
, offset
);
5864 row
= LLVMBuildBitCast(builder
, row
, ctx
->i32
, "");
5865 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5866 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5868 /* The intrinsic kills the thread if arg < 0. */
5869 bit
= LLVMBuildSelect(builder
, bit
, LLVMConstReal(ctx
->f32
, 0),
5870 LLVMConstReal(ctx
->f32
, -1), "");
5871 ac_build_kill(&ctx
->ac
, bit
);
5874 void si_shader_binary_read_config(struct ac_shader_binary
*binary
,
5875 struct si_shader_config
*conf
,
5876 unsigned symbol_offset
)
5879 const unsigned char *config
=
5880 ac_shader_binary_config_start(binary
, symbol_offset
);
5881 bool really_needs_scratch
= false;
5883 /* LLVM adds SGPR spills to the scratch size.
5884 * Find out if we really need the scratch buffer.
5886 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5887 const struct ac_shader_reloc
*reloc
= &binary
->relocs
[i
];
5889 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5890 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5891 really_needs_scratch
= true;
5896 /* XXX: We may be able to emit some of these values directly rather than
5897 * extracting fields to be emitted later.
5900 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5901 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5902 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5904 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5905 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5906 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5907 case R_00B848_COMPUTE_PGM_RSRC1
:
5908 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5909 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5910 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5911 conf
->rsrc1
= value
;
5913 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5914 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5916 case R_00B84C_COMPUTE_PGM_RSRC2
:
5917 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5918 conf
->rsrc2
= value
;
5920 case R_0286CC_SPI_PS_INPUT_ENA
:
5921 conf
->spi_ps_input_ena
= value
;
5923 case R_0286D0_SPI_PS_INPUT_ADDR
:
5924 conf
->spi_ps_input_addr
= value
;
5926 case R_0286E8_SPI_TMPRING_SIZE
:
5927 case R_00B860_COMPUTE_TMPRING_SIZE
:
5928 /* WAVESIZE is in units of 256 dwords. */
5929 if (really_needs_scratch
)
5930 conf
->scratch_bytes_per_wave
=
5931 G_00B860_WAVESIZE(value
) * 256 * 4;
5933 case 0x4: /* SPILLED_SGPRS */
5934 conf
->spilled_sgprs
= value
;
5936 case 0x8: /* SPILLED_VGPRS */
5937 conf
->spilled_vgprs
= value
;
5941 static bool printed
;
5944 fprintf(stderr
, "Warning: LLVM emitted unknown "
5945 "config register: 0x%x\n", reg
);
5953 if (!conf
->spi_ps_input_addr
)
5954 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5957 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
5958 struct si_shader
*shader
,
5959 struct si_shader_config
*config
,
5960 uint64_t scratch_va
)
5963 uint32_t scratch_rsrc_dword0
= scratch_va
;
5964 uint32_t scratch_rsrc_dword1
=
5965 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5967 /* Enable scratch coalescing if LLVM sets ELEMENT_SIZE & INDEX_STRIDE
5970 if (HAVE_LLVM
>= 0x0309)
5971 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5973 scratch_rsrc_dword1
|=
5974 S_008F04_STRIDE(config
->scratch_bytes_per_wave
/ 64);
5976 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5977 const struct ac_shader_reloc
*reloc
=
5978 &shader
->binary
.relocs
[i
];
5979 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5980 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5981 &scratch_rsrc_dword0
, 4);
5982 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5983 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5984 &scratch_rsrc_dword1
, 4);
5989 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
5991 unsigned size
= shader
->binary
.code_size
;
5994 size
+= shader
->prolog
->binary
.code_size
;
5996 size
+= shader
->epilog
->binary
.code_size
;
6000 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
6002 const struct ac_shader_binary
*prolog
=
6003 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
6004 const struct ac_shader_binary
*epilog
=
6005 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
6006 const struct ac_shader_binary
*mainb
= &shader
->binary
;
6007 unsigned bo_size
= si_get_shader_binary_size(shader
) +
6008 (!epilog
? mainb
->rodata_size
: 0);
6011 assert(!prolog
|| !prolog
->rodata_size
);
6012 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
6013 assert(!epilog
|| !epilog
->rodata_size
);
6015 /* GFX9 can fetch at most 128 bytes past the end of the shader.
6016 * Prevent VM faults.
6018 if (sscreen
->b
.chip_class
>= GFX9
)
6021 r600_resource_reference(&shader
->bo
, NULL
);
6022 shader
->bo
= (struct r600_resource
*)
6023 pipe_buffer_create(&sscreen
->b
.b
, 0,
6024 PIPE_USAGE_IMMUTABLE
,
6025 align(bo_size
, SI_CPDMA_ALIGNMENT
));
6030 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
6031 PIPE_TRANSFER_READ_WRITE
);
6034 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
6035 ptr
+= prolog
->code_size
;
6038 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
6039 ptr
+= mainb
->code_size
;
6042 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
6043 else if (mainb
->rodata_size
> 0)
6044 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
6046 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
6050 static void si_shader_dump_disassembly(const struct ac_shader_binary
*binary
,
6051 struct pipe_debug_callback
*debug
,
6052 const char *name
, FILE *file
)
6057 if (binary
->disasm_string
) {
6058 fprintf(file
, "Shader %s disassembly:\n", name
);
6059 fprintf(file
, "%s", binary
->disasm_string
);
6061 if (debug
&& debug
->debug_message
) {
6062 /* Very long debug messages are cut off, so send the
6063 * disassembly one line at a time. This causes more
6064 * overhead, but on the plus side it simplifies
6065 * parsing of resulting logs.
6067 pipe_debug_message(debug
, SHADER_INFO
,
6068 "Shader Disassembly Begin");
6070 line
= binary
->disasm_string
;
6072 p
= util_strchrnul(line
, '\n');
6076 pipe_debug_message(debug
, SHADER_INFO
,
6077 "%.*s", count
, line
);
6085 pipe_debug_message(debug
, SHADER_INFO
,
6086 "Shader Disassembly End");
6089 fprintf(file
, "Shader %s binary:\n", name
);
6090 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
6091 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
6092 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
6093 binary
->code
[i
+ 1], binary
->code
[i
]);
6098 static void si_shader_dump_stats(struct si_screen
*sscreen
,
6099 struct si_shader
*shader
,
6100 struct pipe_debug_callback
*debug
,
6103 bool check_debug_option
)
6105 struct si_shader_config
*conf
= &shader
->config
;
6106 unsigned num_inputs
= shader
->selector
? shader
->selector
->info
.num_inputs
: 0;
6107 unsigned code_size
= si_get_shader_binary_size(shader
);
6108 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
6109 unsigned lds_per_wave
= 0;
6110 unsigned max_simd_waves
= 10;
6112 /* Compute LDS usage for PS. */
6113 switch (processor
) {
6114 case PIPE_SHADER_FRAGMENT
:
6115 /* The minimum usage per wave is (num_inputs * 48). The maximum
6116 * usage is (num_inputs * 48 * 16).
6117 * We can get anything in between and it varies between waves.
6119 * The 48 bytes per input for a single primitive is equal to
6120 * 4 bytes/component * 4 components/input * 3 points.
6122 * Other stages don't know the size at compile time or don't
6123 * allocate LDS per wave, but instead they do it per thread group.
6125 lds_per_wave
= conf
->lds_size
* lds_increment
+
6126 align(num_inputs
* 48, lds_increment
);
6128 case PIPE_SHADER_COMPUTE
:
6129 if (shader
->selector
) {
6130 unsigned max_workgroup_size
=
6131 si_get_max_workgroup_size(shader
);
6132 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
6133 DIV_ROUND_UP(max_workgroup_size
, 64);
6138 /* Compute the per-SIMD wave counts. */
6139 if (conf
->num_sgprs
) {
6140 if (sscreen
->b
.chip_class
>= VI
)
6141 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
6143 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
6146 if (conf
->num_vgprs
)
6147 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
6149 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
6150 * 16KB makes some SIMDs unoccupied). */
6152 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
6154 if (!check_debug_option
||
6155 r600_can_dump_shader(&sscreen
->b
, processor
)) {
6156 if (processor
== PIPE_SHADER_FRAGMENT
) {
6157 fprintf(file
, "*** SHADER CONFIG ***\n"
6158 "SPI_PS_INPUT_ADDR = 0x%04x\n"
6159 "SPI_PS_INPUT_ENA = 0x%04x\n",
6160 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
6163 fprintf(file
, "*** SHADER STATS ***\n"
6166 "Spilled SGPRs: %d\n"
6167 "Spilled VGPRs: %d\n"
6168 "Private memory VGPRs: %d\n"
6169 "Code Size: %d bytes\n"
6171 "Scratch: %d bytes per wave\n"
6173 "********************\n\n\n",
6174 conf
->num_sgprs
, conf
->num_vgprs
,
6175 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
6176 conf
->private_mem_vgprs
, code_size
,
6177 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6181 pipe_debug_message(debug
, SHADER_INFO
,
6182 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
6183 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
6184 "Spilled VGPRs: %d PrivMem VGPRs: %d",
6185 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
6186 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6187 max_simd_waves
, conf
->spilled_sgprs
,
6188 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
6191 const char *si_get_shader_name(struct si_shader
*shader
, unsigned processor
)
6193 switch (processor
) {
6194 case PIPE_SHADER_VERTEX
:
6195 if (shader
->key
.as_es
)
6196 return "Vertex Shader as ES";
6197 else if (shader
->key
.as_ls
)
6198 return "Vertex Shader as LS";
6200 return "Vertex Shader as VS";
6201 case PIPE_SHADER_TESS_CTRL
:
6202 return "Tessellation Control Shader";
6203 case PIPE_SHADER_TESS_EVAL
:
6204 if (shader
->key
.as_es
)
6205 return "Tessellation Evaluation Shader as ES";
6207 return "Tessellation Evaluation Shader as VS";
6208 case PIPE_SHADER_GEOMETRY
:
6209 if (shader
->is_gs_copy_shader
)
6210 return "GS Copy Shader as VS";
6212 return "Geometry Shader";
6213 case PIPE_SHADER_FRAGMENT
:
6214 return "Pixel Shader";
6215 case PIPE_SHADER_COMPUTE
:
6216 return "Compute Shader";
6218 return "Unknown Shader";
6222 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
6223 struct pipe_debug_callback
*debug
, unsigned processor
,
6224 FILE *file
, bool check_debug_option
)
6226 if (!check_debug_option
||
6227 r600_can_dump_shader(&sscreen
->b
, processor
))
6228 si_dump_shader_key(processor
, &shader
->key
, file
);
6230 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
6231 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
6232 si_get_shader_name(shader
, processor
));
6233 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
6236 if (!check_debug_option
||
6237 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
6238 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
6239 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
6242 si_shader_dump_disassembly(&shader
->prolog
->binary
,
6243 debug
, "prolog", file
);
6245 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
6248 si_shader_dump_disassembly(&shader
->epilog
->binary
,
6249 debug
, "epilog", file
);
6250 fprintf(file
, "\n");
6253 si_shader_dump_stats(sscreen
, shader
, debug
, processor
, file
,
6254 check_debug_option
);
6257 int si_compile_llvm(struct si_screen
*sscreen
,
6258 struct ac_shader_binary
*binary
,
6259 struct si_shader_config
*conf
,
6260 LLVMTargetMachineRef tm
,
6262 struct pipe_debug_callback
*debug
,
6267 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
6269 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
6270 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
6272 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
6273 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
6274 ac_dump_module(mod
);
6275 fprintf(stderr
, "\n");
6279 if (sscreen
->record_llvm_ir
) {
6280 char *ir
= LLVMPrintModuleToString(mod
);
6281 binary
->llvm_ir_string
= strdup(ir
);
6282 LLVMDisposeMessage(ir
);
6285 if (!si_replace_shader(count
, binary
)) {
6286 r
= si_llvm_compile(mod
, binary
, tm
, debug
);
6291 si_shader_binary_read_config(binary
, conf
, 0);
6293 /* Enable 64-bit and 16-bit denormals, because there is no performance
6296 * If denormals are enabled, all floating-point output modifiers are
6299 * Don't enable denormals for 32-bit floats, because:
6300 * - Floating-point output modifiers would be ignored by the hw.
6301 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6302 * have to stop using those.
6303 * - SI & CI would be very slow.
6305 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
6307 FREE(binary
->config
);
6308 FREE(binary
->global_symbol_offsets
);
6309 binary
->config
= NULL
;
6310 binary
->global_symbol_offsets
= NULL
;
6312 /* Some shaders can't have rodata because their binaries can be
6315 if (binary
->rodata_size
&&
6316 (processor
== PIPE_SHADER_VERTEX
||
6317 processor
== PIPE_SHADER_TESS_CTRL
||
6318 processor
== PIPE_SHADER_TESS_EVAL
||
6319 processor
== PIPE_SHADER_FRAGMENT
)) {
6320 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
6327 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
6329 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
6330 LLVMBuildRetVoid(ctx
->gallivm
.builder
);
6332 LLVMBuildRet(ctx
->gallivm
.builder
, ret
);
6335 /* Generate code for the hardware VS shader stage to go with a geometry shader */
6337 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
6338 LLVMTargetMachineRef tm
,
6339 struct si_shader_selector
*gs_selector
,
6340 struct pipe_debug_callback
*debug
)
6342 struct si_shader_context ctx
;
6343 struct si_shader
*shader
;
6344 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
6345 LLVMBuilderRef builder
;
6346 struct lp_build_tgsi_context
*bld_base
= &ctx
.bld_base
;
6347 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
6348 struct si_shader_output_values
*outputs
;
6349 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
6352 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
6357 shader
= CALLOC_STRUCT(si_shader
);
6364 shader
->selector
= gs_selector
;
6365 shader
->is_gs_copy_shader
= true;
6367 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
6368 ctx
.type
= PIPE_SHADER_VERTEX
;
6370 builder
= gallivm
->builder
;
6372 create_function(&ctx
);
6373 preload_ring_buffers(&ctx
);
6375 LLVMValueRef voffset
=
6376 lp_build_mul_imm(uint
, LLVMGetParam(ctx
.main_fn
,
6377 ctx
.param_vertex_id
), 4);
6379 /* Fetch the vertex stream ID.*/
6380 LLVMValueRef stream_id
;
6382 if (gs_selector
->so
.num_outputs
)
6383 stream_id
= unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
6385 stream_id
= uint
->zero
;
6387 /* Fill in output information. */
6388 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
6389 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
6390 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
6392 for (int chan
= 0; chan
< 4; chan
++) {
6393 outputs
[i
].vertex_stream
[chan
] =
6394 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
6398 LLVMBasicBlockRef end_bb
;
6399 LLVMValueRef switch_inst
;
6401 end_bb
= LLVMAppendBasicBlockInContext(gallivm
->context
, ctx
.main_fn
, "end");
6402 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
6404 for (int stream
= 0; stream
< 4; stream
++) {
6405 LLVMBasicBlockRef bb
;
6408 if (!gsinfo
->num_stream_output_components
[stream
])
6411 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
6414 bb
= LLVMInsertBasicBlockInContext(gallivm
->context
, end_bb
, "out");
6415 LLVMAddCase(switch_inst
, lp_build_const_int32(gallivm
, stream
), bb
);
6416 LLVMPositionBuilderAtEnd(builder
, bb
);
6418 /* Fetch vertex data from GSVS ring */
6420 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
6421 for (unsigned chan
= 0; chan
< 4; chan
++) {
6422 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
6423 outputs
[i
].vertex_stream
[chan
] != stream
) {
6424 outputs
[i
].values
[chan
] = ctx
.bld_base
.base
.undef
;
6428 LLVMValueRef soffset
= LLVMConstInt(ctx
.i32
,
6429 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4, 0);
6432 outputs
[i
].values
[chan
] =
6433 ac_build_buffer_load(&ctx
.ac
,
6434 ctx
.gsvs_ring
[0], 1,
6435 uint
->zero
, voffset
,
6436 soffset
, 0, 1, 1, true);
6440 /* Streamout and exports. */
6441 if (gs_selector
->so
.num_outputs
) {
6442 si_llvm_emit_streamout(&ctx
, outputs
,
6443 gsinfo
->num_outputs
,
6448 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
6450 LLVMBuildBr(builder
, end_bb
);
6453 LLVMPositionBuilderAtEnd(builder
, end_bb
);
6455 LLVMBuildRetVoid(gallivm
->builder
);
6457 /* Dump LLVM IR before any optimization passes */
6458 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
6459 r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6460 ac_dump_module(bld_base
->base
.gallivm
->module
);
6462 si_llvm_finalize_module(&ctx
,
6463 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_GEOMETRY
));
6465 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
6466 &ctx
.shader
->config
, ctx
.tm
,
6467 bld_base
->base
.gallivm
->module
,
6468 debug
, PIPE_SHADER_GEOMETRY
,
6471 if (r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6472 fprintf(stderr
, "GS Copy Shader:\n");
6473 si_shader_dump(sscreen
, ctx
.shader
, debug
,
6474 PIPE_SHADER_GEOMETRY
, stderr
, true);
6475 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
6478 si_llvm_dispose(&ctx
);
6489 static void si_dump_shader_key(unsigned shader
, struct si_shader_key
*key
,
6494 fprintf(f
, "SHADER KEY\n");
6497 case PIPE_SHADER_VERTEX
:
6498 fprintf(f
, " part.vs.prolog.instance_divisors = {");
6499 for (i
= 0; i
< ARRAY_SIZE(key
->part
.vs
.prolog
.instance_divisors
); i
++)
6500 fprintf(f
, !i
? "%u" : ", %u",
6501 key
->part
.vs
.prolog
.instance_divisors
[i
]);
6503 fprintf(f
, " part.vs.epilog.export_prim_id = %u\n", key
->part
.vs
.epilog
.export_prim_id
);
6504 fprintf(f
, " as_es = %u\n", key
->as_es
);
6505 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
6507 fprintf(f
, " mono.vs.fix_fetch = {");
6508 for (i
= 0; i
< SI_MAX_ATTRIBS
; i
++)
6509 fprintf(f
, !i
? "%u" : ", %u", key
->mono
.vs
.fix_fetch
[i
]);
6513 case PIPE_SHADER_TESS_CTRL
:
6514 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
6515 fprintf(f
, " mono.tcs.inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.tcs
.inputs_to_copy
);
6518 case PIPE_SHADER_TESS_EVAL
:
6519 fprintf(f
, " part.tes.epilog.export_prim_id = %u\n", key
->part
.tes
.epilog
.export_prim_id
);
6520 fprintf(f
, " as_es = %u\n", key
->as_es
);
6523 case PIPE_SHADER_GEOMETRY
:
6524 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
6527 case PIPE_SHADER_COMPUTE
:
6530 case PIPE_SHADER_FRAGMENT
:
6531 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
6532 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
6533 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
6534 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
6535 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
6536 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
6537 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
6538 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
6539 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
6540 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
6541 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
6542 fprintf(f
, " part.ps.epilog.color_is_int10 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int10
);
6543 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
6544 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
6545 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
6546 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
6547 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
6554 if ((shader
== PIPE_SHADER_GEOMETRY
||
6555 shader
== PIPE_SHADER_TESS_EVAL
||
6556 shader
== PIPE_SHADER_VERTEX
) &&
6557 !key
->as_es
&& !key
->as_ls
) {
6558 fprintf(f
, " opt.hw_vs.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.hw_vs
.kill_outputs
);
6559 fprintf(f
, " opt.hw_vs.kill_outputs2 = 0x%x\n", key
->opt
.hw_vs
.kill_outputs2
);
6560 fprintf(f
, " opt.hw_vs.clip_disable = %u\n", key
->opt
.hw_vs
.clip_disable
);
6564 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
6565 struct si_screen
*sscreen
,
6566 struct si_shader
*shader
,
6567 LLVMTargetMachineRef tm
)
6569 struct lp_build_tgsi_context
*bld_base
;
6570 struct lp_build_tgsi_action tmpl
= {};
6572 si_llvm_context_init(ctx
, sscreen
, shader
, tm
,
6573 (shader
&& shader
->selector
) ? &shader
->selector
->info
: NULL
,
6574 (shader
&& shader
->selector
) ? shader
->selector
->tokens
: NULL
);
6576 bld_base
= &ctx
->bld_base
;
6577 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
6579 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
6580 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
6581 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
6583 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
6584 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
] = tex_action
;
6585 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
6586 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
6587 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
6588 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
6589 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
6590 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
] = tex_action
;
6591 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
6592 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
6593 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
6594 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= txq_fetch_args
;
6595 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
6596 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
6597 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
6598 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
6600 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
6601 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
6602 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
6603 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
6604 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
6605 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
6607 tmpl
.fetch_args
= atomic_fetch_args
;
6608 tmpl
.emit
= atomic_emit
;
6609 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
6610 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
6611 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
6612 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
6613 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
6614 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
6615 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
6616 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
6617 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
6618 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
6619 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
6620 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
6621 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
6622 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
6623 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
6624 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
6625 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
6626 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
6627 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
6628 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
6630 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
6632 bld_base
->op_actions
[TGSI_OPCODE_CLOCK
].emit
= clock_emit
;
6634 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
6635 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
6636 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
6637 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
6639 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ALL
].emit
= vote_all_emit
;
6640 bld_base
->op_actions
[TGSI_OPCODE_VOTE_ANY
].emit
= vote_any_emit
;
6641 bld_base
->op_actions
[TGSI_OPCODE_VOTE_EQ
].emit
= vote_eq_emit
;
6643 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
6644 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
6645 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
6648 #define EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
6649 #define EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
6651 /* Return true if the PARAM export has been eliminated. */
6652 static bool si_eliminate_const_output(struct si_shader_context
*ctx
,
6653 LLVMValueRef inst
, unsigned offset
)
6655 struct si_shader
*shader
= ctx
->shader
;
6656 unsigned num_outputs
= shader
->selector
->info
.num_outputs
;
6657 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
6658 bool is_zero
[4] = {}, is_one
[4] = {};
6660 for (i
= 0; i
< 4; i
++) {
6661 LLVMBool loses_info
;
6662 LLVMValueRef p
= LLVMGetOperand(inst
, EXP_OUT0
+ i
);
6664 /* It's a constant expression. Undef outputs are eliminated too. */
6665 if (LLVMIsUndef(p
)) {
6668 } else if (LLVMIsAConstantFP(p
)) {
6669 double a
= LLVMConstRealGetDouble(p
, &loses_info
);
6676 return false; /* other constant */
6681 /* Only certain combinations of 0 and 1 can be eliminated. */
6682 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
6683 default_val
= is_zero
[3] ? 0 : 1;
6684 else if (is_one
[0] && is_one
[1] && is_one
[2])
6685 default_val
= is_zero
[3] ? 2 : 3;
6689 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
6690 LLVMInstructionEraseFromParent(inst
);
6692 /* Change OFFSET to DEFAULT_VAL. */
6693 for (i
= 0; i
< num_outputs
; i
++) {
6694 if (shader
->info
.vs_output_param_offset
[i
] == offset
) {
6695 shader
->info
.vs_output_param_offset
[i
] =
6696 EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
6703 struct si_vs_exports
{
6705 unsigned offset
[SI_MAX_VS_OUTPUTS
];
6706 LLVMValueRef inst
[SI_MAX_VS_OUTPUTS
];
6709 static void si_eliminate_const_vs_outputs(struct si_shader_context
*ctx
)
6711 struct si_shader
*shader
= ctx
->shader
;
6712 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6713 LLVMBasicBlockRef bb
;
6714 struct si_vs_exports exports
;
6715 bool removed_any
= false;
6719 if (ctx
->type
== PIPE_SHADER_FRAGMENT
||
6720 ctx
->type
== PIPE_SHADER_COMPUTE
||
6721 shader
->key
.as_es
||
6725 /* Process all LLVM instructions. */
6726 bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6728 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
6731 LLVMValueRef cur
= inst
;
6732 inst
= LLVMGetNextInstruction(inst
);
6734 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
6737 LLVMValueRef callee
= lp_get_called_value(cur
);
6739 if (!lp_is_function(callee
))
6742 const char *name
= LLVMGetValueName(callee
);
6743 unsigned num_args
= LLVMCountParams(callee
);
6745 /* Check if this is an export instruction. */
6746 if ((num_args
!= 9 && num_args
!= 8) ||
6747 (strcmp(name
, "llvm.SI.export") &&
6748 strcmp(name
, "llvm.amdgcn.exp.f32")))
6751 LLVMValueRef arg
= LLVMGetOperand(cur
, EXP_TARGET
);
6752 unsigned target
= LLVMConstIntGetZExtValue(arg
);
6754 if (target
< V_008DFC_SQ_EXP_PARAM
)
6757 target
-= V_008DFC_SQ_EXP_PARAM
;
6759 /* Eliminate constant value PARAM exports. */
6760 if (si_eliminate_const_output(ctx
, cur
, target
)) {
6763 exports
.offset
[exports
.num
] = target
;
6764 exports
.inst
[exports
.num
] = cur
;
6768 bb
= LLVMGetNextBasicBlock(bb
);
6771 /* Remove holes in export memory due to removed PARAM exports.
6772 * This is done by renumbering all PARAM exports.
6775 ubyte current_offset
[SI_MAX_VS_OUTPUTS
];
6776 unsigned new_count
= 0;
6779 /* Make a copy of the offsets. We need the old version while
6780 * we are modifying some of them. */
6781 assert(sizeof(current_offset
) ==
6782 sizeof(shader
->info
.vs_output_param_offset
));
6783 memcpy(current_offset
, shader
->info
.vs_output_param_offset
,
6784 sizeof(current_offset
));
6786 for (i
= 0; i
< exports
.num
; i
++) {
6787 unsigned offset
= exports
.offset
[i
];
6789 for (out
= 0; out
< info
->num_outputs
; out
++) {
6790 if (current_offset
[out
] != offset
)
6793 LLVMSetOperand(exports
.inst
[i
], EXP_TARGET
,
6794 LLVMConstInt(ctx
->i32
,
6795 V_008DFC_SQ_EXP_PARAM
+ new_count
, 0));
6796 shader
->info
.vs_output_param_offset
[out
] = new_count
;
6801 shader
->info
.nr_param_exports
= new_count
;
6805 static void si_count_scratch_private_memory(struct si_shader_context
*ctx
)
6807 ctx
->shader
->config
.private_mem_vgprs
= 0;
6809 /* Process all LLVM instructions. */
6810 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6812 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
6815 LLVMValueRef inst
= next
;
6816 next
= LLVMGetNextInstruction(next
);
6818 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
6821 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
6822 /* No idea why LLVM aligns allocas to 4 elements. */
6823 unsigned alignment
= LLVMGetAlignment(inst
);
6824 unsigned dw_size
= align(llvm_get_type_size(type
) / 4, alignment
);
6825 ctx
->shader
->config
.private_mem_vgprs
+= dw_size
;
6827 bb
= LLVMGetNextBasicBlock(bb
);
6831 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
,
6832 struct si_shader
*shader
)
6834 struct si_shader_selector
*sel
= shader
->selector
;
6835 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6837 switch (ctx
->type
) {
6838 case PIPE_SHADER_VERTEX
:
6839 ctx
->load_input
= declare_input_vs
;
6840 if (shader
->key
.as_ls
)
6841 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
6842 else if (shader
->key
.as_es
)
6843 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6845 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6847 case PIPE_SHADER_TESS_CTRL
:
6848 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6849 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6850 bld_base
->emit_store
= store_output_tcs
;
6851 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
6853 case PIPE_SHADER_TESS_EVAL
:
6854 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6855 if (shader
->key
.as_es
)
6856 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6858 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6860 case PIPE_SHADER_GEOMETRY
:
6861 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6862 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
6864 case PIPE_SHADER_FRAGMENT
:
6865 ctx
->load_input
= declare_input_fs
;
6866 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
6868 case PIPE_SHADER_COMPUTE
:
6869 ctx
->declare_memory_region
= declare_compute_memory
;
6872 assert(!"Unsupported shader type");
6876 create_function(ctx
);
6877 preload_ring_buffers(ctx
);
6879 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6881 for (i
= 0; i
< 4; i
++) {
6882 ctx
->gs_next_vertex
[i
] =
6883 lp_build_alloca(bld_base
->base
.gallivm
,
6888 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6889 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6893 si_llvm_build_ret(ctx
, ctx
->return_value
);
6898 * Compute the VS prolog key, which contains all the information needed to
6899 * build the VS prolog function, and set shader->info bits where needed.
6901 static void si_get_vs_prolog_key(struct si_shader
*shader
,
6902 union si_shader_part_key
*key
)
6904 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6906 memset(key
, 0, sizeof(*key
));
6907 key
->vs_prolog
.states
= shader
->key
.part
.vs
.prolog
;
6908 key
->vs_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6909 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6911 /* Set the instanceID flag. */
6912 for (unsigned i
= 0; i
< info
->num_inputs
; i
++)
6913 if (key
->vs_prolog
.states
.instance_divisors
[i
])
6914 shader
->info
.uses_instanceid
= true;
6918 * Compute the VS epilog key, which contains all the information needed to
6919 * build the VS epilog function, and set the PrimitiveID output offset.
6921 static void si_get_vs_epilog_key(struct si_shader
*shader
,
6922 struct si_vs_epilog_bits
*states
,
6923 union si_shader_part_key
*key
)
6925 memset(key
, 0, sizeof(*key
));
6926 key
->vs_epilog
.states
= *states
;
6928 /* Set up the PrimitiveID output. */
6929 if (shader
->key
.part
.vs
.epilog
.export_prim_id
) {
6930 unsigned index
= shader
->selector
->info
.num_outputs
;
6931 unsigned offset
= shader
->info
.nr_param_exports
++;
6933 key
->vs_epilog
.prim_id_param_offset
= offset
;
6934 assert(index
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
6935 shader
->info
.vs_output_param_offset
[index
] = offset
;
6940 * Compute the PS prolog key, which contains all the information needed to
6941 * build the PS prolog function, and set related bits in shader->config.
6943 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6944 union si_shader_part_key
*key
,
6945 bool separate_prolog
)
6947 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6949 memset(key
, 0, sizeof(*key
));
6950 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6951 key
->ps_prolog
.colors_read
= info
->colors_read
;
6952 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6953 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6954 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6955 (key
->ps_prolog
.colors_read
||
6956 key
->ps_prolog
.states
.force_persp_sample_interp
||
6957 key
->ps_prolog
.states
.force_linear_sample_interp
||
6958 key
->ps_prolog
.states
.force_persp_center_interp
||
6959 key
->ps_prolog
.states
.force_linear_center_interp
||
6960 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6961 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6963 if (info
->colors_read
) {
6964 unsigned *color
= shader
->selector
->color_attr_index
;
6966 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6967 /* BCOLORs are stored after the last input. */
6968 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6969 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6970 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6973 for (unsigned i
= 0; i
< 2; i
++) {
6974 unsigned interp
= info
->input_interpolate
[color
[i
]];
6975 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6977 if (!(info
->colors_read
& (0xf << i
*4)))
6980 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6982 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6983 interp
== TGSI_INTERPOLATE_COLOR
)
6984 interp
= TGSI_INTERPOLATE_CONSTANT
;
6987 case TGSI_INTERPOLATE_CONSTANT
:
6988 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6990 case TGSI_INTERPOLATE_PERSPECTIVE
:
6991 case TGSI_INTERPOLATE_COLOR
:
6992 /* Force the interpolation location for colors here. */
6993 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6994 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6995 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6996 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6999 case TGSI_INTERPOLATE_LOC_SAMPLE
:
7000 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
7001 shader
->config
.spi_ps_input_ena
|=
7002 S_0286CC_PERSP_SAMPLE_ENA(1);
7004 case TGSI_INTERPOLATE_LOC_CENTER
:
7005 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
7006 shader
->config
.spi_ps_input_ena
|=
7007 S_0286CC_PERSP_CENTER_ENA(1);
7009 case TGSI_INTERPOLATE_LOC_CENTROID
:
7010 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
7011 shader
->config
.spi_ps_input_ena
|=
7012 S_0286CC_PERSP_CENTROID_ENA(1);
7018 case TGSI_INTERPOLATE_LINEAR
:
7019 /* Force the interpolation location for colors here. */
7020 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
7021 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
7022 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
7023 location
= TGSI_INTERPOLATE_LOC_CENTER
;
7025 /* The VGPR assignment for non-monolithic shaders
7026 * works because InitialPSInputAddr is set on the
7027 * main shader and PERSP_PULL_MODEL is never used.
7030 case TGSI_INTERPOLATE_LOC_SAMPLE
:
7031 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7032 separate_prolog
? 6 : 9;
7033 shader
->config
.spi_ps_input_ena
|=
7034 S_0286CC_LINEAR_SAMPLE_ENA(1);
7036 case TGSI_INTERPOLATE_LOC_CENTER
:
7037 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7038 separate_prolog
? 8 : 11;
7039 shader
->config
.spi_ps_input_ena
|=
7040 S_0286CC_LINEAR_CENTER_ENA(1);
7042 case TGSI_INTERPOLATE_LOC_CENTROID
:
7043 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7044 separate_prolog
? 10 : 13;
7045 shader
->config
.spi_ps_input_ena
|=
7046 S_0286CC_LINEAR_CENTROID_ENA(1);
7060 * Check whether a PS prolog is required based on the key.
7062 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
7064 return key
->ps_prolog
.colors_read
||
7065 key
->ps_prolog
.states
.force_persp_sample_interp
||
7066 key
->ps_prolog
.states
.force_linear_sample_interp
||
7067 key
->ps_prolog
.states
.force_persp_center_interp
||
7068 key
->ps_prolog
.states
.force_linear_center_interp
||
7069 key
->ps_prolog
.states
.bc_optimize_for_persp
||
7070 key
->ps_prolog
.states
.bc_optimize_for_linear
||
7071 key
->ps_prolog
.states
.poly_stipple
;
7075 * Compute the PS epilog key, which contains all the information needed to
7076 * build the PS epilog function.
7078 static void si_get_ps_epilog_key(struct si_shader
*shader
,
7079 union si_shader_part_key
*key
)
7081 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
7082 memset(key
, 0, sizeof(*key
));
7083 key
->ps_epilog
.colors_written
= info
->colors_written
;
7084 key
->ps_epilog
.writes_z
= info
->writes_z
;
7085 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
7086 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
7087 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
7091 * Build the GS prolog function. Rotate the input vertices for triangle strips
7094 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
7095 union si_shader_part_key
*key
)
7097 const unsigned num_sgprs
= SI_GS_NUM_USER_SGPR
+ 2;
7098 const unsigned num_vgprs
= 8;
7099 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7100 LLVMBuilderRef builder
= gallivm
->builder
;
7101 LLVMTypeRef params
[32];
7102 LLVMTypeRef returns
[32];
7103 LLVMValueRef func
, ret
;
7105 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
7106 params
[i
] = ctx
->i32
;
7107 returns
[i
] = ctx
->i32
;
7110 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
7111 params
[num_sgprs
+ i
] = ctx
->i32
;
7112 returns
[num_sgprs
+ i
] = ctx
->f32
;
7115 /* Create the function. */
7116 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
7117 params
, num_sgprs
+ num_vgprs
, num_sgprs
- 1);
7118 func
= ctx
->main_fn
;
7120 /* Copy inputs to outputs. This should be no-op, as the registers match,
7121 * but it will prevent the compiler from overwriting them unintentionally.
7123 ret
= ctx
->return_value
;
7124 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
7125 LLVMValueRef p
= LLVMGetParam(func
, i
);
7126 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
7128 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
7129 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
7130 p
= LLVMBuildBitCast(builder
, p
, ctx
->f32
, "");
7131 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
7134 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
7135 /* Remap the input vertices for every other primitive. */
7136 const unsigned vtx_params
[6] = {
7144 LLVMValueRef prim_id
, rotate
;
7146 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
7147 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
7149 for (unsigned i
= 0; i
< 6; ++i
) {
7150 LLVMValueRef base
, rotated
, actual
;
7151 base
= LLVMGetParam(func
, vtx_params
[i
]);
7152 rotated
= LLVMGetParam(func
, vtx_params
[(i
+ 4) % 6]);
7153 actual
= LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
7154 actual
= LLVMBuildBitCast(builder
, actual
, ctx
->f32
, "");
7155 ret
= LLVMBuildInsertValue(builder
, ret
, actual
, vtx_params
[i
], "");
7159 LLVMBuildRet(builder
, ret
);
7163 * Given a list of shader part functions, build a wrapper function that
7164 * runs them in sequence to form a monolithic shader.
7166 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
7167 LLVMValueRef
*parts
,
7171 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7172 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
7173 /* PS epilog has one arg per color component */
7174 LLVMTypeRef param_types
[48];
7175 LLVMValueRef out
[48];
7176 LLVMTypeRef function_type
;
7177 unsigned num_params
;
7179 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
7180 unsigned num_sgprs
, num_vgprs
;
7181 unsigned last_sgpr_param
;
7184 for (unsigned i
= 0; i
< num_parts
; ++i
) {
7185 lp_add_function_attr(parts
[i
], -1, LP_FUNC_ATTR_ALWAYSINLINE
);
7186 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
7189 /* The parameters of the wrapper function correspond to those of the
7190 * first part in terms of SGPRs and VGPRs, but we use the types of the
7191 * main part to get the right types. This is relevant for the
7192 * dereferenceable attribute on descriptor table pointers.
7197 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
7198 num_params
= LLVMCountParamTypes(function_type
);
7200 for (unsigned i
= 0; i
< num_params
; ++i
) {
7201 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
7203 if (ac_is_sgpr_param(param
)) {
7204 assert(num_vgprs
== 0);
7205 num_sgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7207 num_vgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7210 assert(num_vgprs
+ num_sgprs
<= ARRAY_SIZE(param_types
));
7213 last_sgpr_param
= 0;
7215 while (gprs
< num_sgprs
+ num_vgprs
) {
7216 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], num_params
);
7219 param_types
[num_params
] = LLVMTypeOf(param
);
7220 if (gprs
< num_sgprs
)
7221 last_sgpr_param
= num_params
;
7222 size
= llvm_get_type_size(param_types
[num_params
]) / 4;
7225 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
7226 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
7227 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
7232 si_create_function(ctx
, "wrapper", NULL
, 0, param_types
, num_params
, last_sgpr_param
);
7234 /* Record the arguments of the function as if they were an output of
7240 for (unsigned i
= 0; i
< num_params
; ++i
) {
7241 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
7242 LLVMTypeRef param_type
= LLVMTypeOf(param
);
7243 LLVMTypeRef out_type
= i
<= last_sgpr_param
? ctx
->i32
: ctx
->f32
;
7244 unsigned size
= llvm_get_type_size(param_type
) / 4;
7247 if (param_type
!= out_type
)
7248 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
7249 out
[num_out
++] = param
;
7251 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
7253 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7254 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
7255 param_type
= ctx
->i64
;
7258 if (param_type
!= vector_type
)
7259 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
7261 for (unsigned j
= 0; j
< size
; ++j
)
7262 out
[num_out
++] = LLVMBuildExtractElement(
7263 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
7266 if (i
<= last_sgpr_param
)
7267 num_out_sgpr
= num_out
;
7270 /* Now chain the parts. */
7271 for (unsigned part
= 0; part
< num_parts
; ++part
) {
7272 LLVMValueRef in
[48];
7274 LLVMTypeRef ret_type
;
7275 unsigned out_idx
= 0;
7277 num_params
= LLVMCountParams(parts
[part
]);
7278 assert(num_params
<= ARRAY_SIZE(param_types
));
7280 /* Derive arguments for the next part from outputs of the
7283 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
7285 LLVMTypeRef param_type
;
7287 unsigned param_size
;
7288 LLVMValueRef arg
= NULL
;
7290 param
= LLVMGetParam(parts
[part
], param_idx
);
7291 param_type
= LLVMTypeOf(param
);
7292 param_size
= llvm_get_type_size(param_type
) / 4;
7293 is_sgpr
= ac_is_sgpr_param(param
);
7296 #if HAVE_LLVM < 0x0400
7297 LLVMRemoveAttribute(param
, LLVMByValAttribute
);
7299 unsigned kind_id
= LLVMGetEnumAttributeKindForName("byval", 5);
7300 LLVMRemoveEnumAttributeAtIndex(parts
[part
], param_idx
+ 1, kind_id
);
7302 lp_add_function_attr(parts
[part
], param_idx
+ 1, LP_FUNC_ATTR_INREG
);
7305 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
7306 assert(is_sgpr
|| out_idx
>= num_out_sgpr
);
7308 if (param_size
== 1)
7311 arg
= lp_build_gather_values(gallivm
, &out
[out_idx
], param_size
);
7313 if (LLVMTypeOf(arg
) != param_type
) {
7314 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7315 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
7316 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
7318 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
7322 in
[param_idx
] = arg
;
7323 out_idx
+= param_size
;
7326 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
7327 ret_type
= LLVMTypeOf(ret
);
7329 /* Extract the returned GPRs. */
7333 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
7334 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
7336 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
7338 for (unsigned i
= 0; i
< ret_size
; ++i
) {
7340 LLVMBuildExtractValue(builder
, ret
, i
, "");
7342 out
[num_out
++] = val
;
7344 if (LLVMTypeOf(val
) == ctx
->i32
) {
7345 assert(num_out_sgpr
+ 1 == num_out
);
7346 num_out_sgpr
= num_out
;
7352 LLVMBuildRetVoid(builder
);
7355 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
7356 LLVMTargetMachineRef tm
,
7357 struct si_shader
*shader
,
7359 struct pipe_debug_callback
*debug
)
7361 struct si_shader_selector
*sel
= shader
->selector
;
7362 struct si_shader_context ctx
;
7363 struct lp_build_tgsi_context
*bld_base
;
7367 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
7368 * conversion fails. */
7369 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
7370 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
7371 tgsi_dump(sel
->tokens
, 0);
7372 si_dump_streamout(&sel
->so
);
7375 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
7376 ctx
.separate_prolog
= !is_monolithic
;
7378 memset(shader
->info
.vs_output_param_offset
, EXP_PARAM_UNDEFINED
,
7379 sizeof(shader
->info
.vs_output_param_offset
));
7381 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
7383 bld_base
= &ctx
.bld_base
;
7384 ctx
.load_system_value
= declare_system_value
;
7386 if (!si_compile_tgsi_main(&ctx
, shader
)) {
7387 si_llvm_dispose(&ctx
);
7391 if (is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
7392 LLVMValueRef parts
[3];
7396 need_prolog
= sel
->info
.num_inputs
;
7397 need_epilog
= !shader
->key
.as_es
&& !shader
->key
.as_ls
;
7399 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7402 union si_shader_part_key prolog_key
;
7403 si_get_vs_prolog_key(shader
, &prolog_key
);
7404 si_build_vs_prolog_function(&ctx
, &prolog_key
);
7405 parts
[0] = ctx
.main_fn
;
7409 union si_shader_part_key epilog_key
;
7410 si_get_vs_epilog_key(shader
, &shader
->key
.part
.vs
.epilog
, &epilog_key
);
7411 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7412 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7415 si_build_wrapper_function(&ctx
, parts
, 1 + need_prolog
+ need_epilog
,
7416 need_prolog
? 1 : 0);
7417 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
7418 LLVMValueRef parts
[2];
7419 union si_shader_part_key epilog_key
;
7421 parts
[0] = ctx
.main_fn
;
7423 memset(&epilog_key
, 0, sizeof(epilog_key
));
7424 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7425 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
7426 parts
[1] = ctx
.main_fn
;
7428 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7429 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_EVAL
&&
7430 !shader
->key
.as_es
) {
7431 LLVMValueRef parts
[2];
7432 union si_shader_part_key epilog_key
;
7434 parts
[0] = ctx
.main_fn
;
7436 si_get_vs_epilog_key(shader
, &shader
->key
.part
.tes
.epilog
, &epilog_key
);
7437 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7438 parts
[1] = ctx
.main_fn
;
7440 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7441 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
7442 LLVMValueRef parts
[2];
7443 union si_shader_part_key prolog_key
;
7445 parts
[1] = ctx
.main_fn
;
7447 memset(&prolog_key
, 0, sizeof(prolog_key
));
7448 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7449 si_build_gs_prolog_function(&ctx
, &prolog_key
);
7450 parts
[0] = ctx
.main_fn
;
7452 si_build_wrapper_function(&ctx
, parts
, 2, 1);
7453 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7454 LLVMValueRef parts
[3];
7455 union si_shader_part_key prolog_key
;
7456 union si_shader_part_key epilog_key
;
7459 si_get_ps_prolog_key(shader
, &prolog_key
, false);
7460 need_prolog
= si_need_ps_prolog(&prolog_key
);
7462 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7465 si_build_ps_prolog_function(&ctx
, &prolog_key
);
7466 parts
[0] = ctx
.main_fn
;
7469 si_get_ps_epilog_key(shader
, &epilog_key
);
7470 si_build_ps_epilog_function(&ctx
, &epilog_key
);
7471 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7473 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2, need_prolog
? 1 : 0);
7476 mod
= bld_base
->base
.gallivm
->module
;
7478 /* Dump LLVM IR before any optimization passes */
7479 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
7480 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7481 ac_dump_module(mod
);
7483 si_llvm_finalize_module(&ctx
,
7484 r600_extra_shader_checks(&sscreen
->b
, ctx
.type
));
7486 /* Post-optimization transformations and analysis. */
7487 si_eliminate_const_vs_outputs(&ctx
);
7489 if ((debug
&& debug
->debug_message
) ||
7490 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7491 si_count_scratch_private_memory(&ctx
);
7493 /* Compile to bytecode. */
7494 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
7495 mod
, debug
, ctx
.type
, "TGSI shader");
7496 si_llvm_dispose(&ctx
);
7498 fprintf(stderr
, "LLVM failed to compile shader\n");
7502 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
7503 * LLVM 3.9svn has this bug.
7505 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
7506 unsigned wave_size
= 64;
7507 unsigned max_vgprs
= 256;
7508 unsigned max_sgprs
= sscreen
->b
.chip_class
>= VI
? 800 : 512;
7509 unsigned max_sgprs_per_wave
= 128;
7510 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
7511 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7512 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7514 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7515 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7517 if (shader
->config
.num_sgprs
> max_sgprs
||
7518 shader
->config
.num_vgprs
> max_vgprs
) {
7519 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7520 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7521 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7522 max_sgprs
, max_vgprs
);
7524 /* Just terminate the process, because dependent
7525 * shaders can hang due to bad input data, but use
7526 * the env var to allow shader-db to work.
7528 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7533 /* Add the scratch offset to input SGPRs. */
7534 if (shader
->config
.scratch_bytes_per_wave
)
7535 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7537 /* Calculate the number of fragment input VGPRs. */
7538 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7539 shader
->info
.num_input_vgprs
= 0;
7540 shader
->info
.face_vgpr_index
= -1;
7542 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7543 shader
->info
.num_input_vgprs
+= 2;
7544 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7545 shader
->info
.num_input_vgprs
+= 2;
7546 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7547 shader
->info
.num_input_vgprs
+= 2;
7548 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7549 shader
->info
.num_input_vgprs
+= 3;
7550 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7551 shader
->info
.num_input_vgprs
+= 2;
7552 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7553 shader
->info
.num_input_vgprs
+= 2;
7554 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7555 shader
->info
.num_input_vgprs
+= 2;
7556 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7557 shader
->info
.num_input_vgprs
+= 1;
7558 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7559 shader
->info
.num_input_vgprs
+= 1;
7560 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7561 shader
->info
.num_input_vgprs
+= 1;
7562 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7563 shader
->info
.num_input_vgprs
+= 1;
7564 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7565 shader
->info
.num_input_vgprs
+= 1;
7566 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7567 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7568 shader
->info
.num_input_vgprs
+= 1;
7570 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
7571 shader
->info
.num_input_vgprs
+= 1;
7572 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7573 shader
->info
.num_input_vgprs
+= 1;
7574 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7575 shader
->info
.num_input_vgprs
+= 1;
7582 * Create, compile and return a shader part (prolog or epilog).
7584 * \param sscreen screen
7585 * \param list list of shader parts of the same category
7586 * \param type shader type
7587 * \param key shader part key
7588 * \param prolog whether the part being requested is a prolog
7589 * \param tm LLVM target machine
7590 * \param debug debug callback
7591 * \param build the callback responsible for building the main function
7592 * \return non-NULL on success
7594 static struct si_shader_part
*
7595 si_get_shader_part(struct si_screen
*sscreen
,
7596 struct si_shader_part
**list
,
7597 enum pipe_shader_type type
,
7599 union si_shader_part_key
*key
,
7600 LLVMTargetMachineRef tm
,
7601 struct pipe_debug_callback
*debug
,
7602 void (*build
)(struct si_shader_context
*,
7603 union si_shader_part_key
*),
7606 struct si_shader_part
*result
;
7608 mtx_lock(&sscreen
->shader_parts_mutex
);
7610 /* Find existing. */
7611 for (result
= *list
; result
; result
= result
->next
) {
7612 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7613 mtx_unlock(&sscreen
->shader_parts_mutex
);
7618 /* Compile a new one. */
7619 result
= CALLOC_STRUCT(si_shader_part
);
7622 struct si_shader shader
= {};
7623 struct si_shader_context ctx
;
7624 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
7626 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
7630 case PIPE_SHADER_VERTEX
:
7632 case PIPE_SHADER_TESS_CTRL
:
7634 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7636 case PIPE_SHADER_GEOMETRY
:
7639 case PIPE_SHADER_FRAGMENT
:
7641 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7643 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7646 unreachable("bad shader part");
7652 si_llvm_finalize_module(&ctx
,
7653 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_FRAGMENT
));
7655 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, tm
,
7656 gallivm
->module
, debug
, ctx
.type
, name
)) {
7662 result
->next
= *list
;
7666 si_llvm_dispose(&ctx
);
7667 mtx_unlock(&sscreen
->shader_parts_mutex
);
7672 * Build the vertex shader prolog function.
7674 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7675 * All inputs are returned unmodified. The vertex load indices are
7676 * stored after them, which will be used by the API VS for fetching inputs.
7678 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7683 * (VertexID + BaseVertex),
7684 * (InstanceID + StartInstance),
7685 * (InstanceID / 2 + StartInstance)
7687 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7688 union si_shader_part_key
*key
)
7690 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7691 LLVMTypeRef
*params
, *returns
;
7692 LLVMValueRef ret
, func
;
7693 int last_sgpr
, num_params
, num_returns
, i
;
7695 ctx
->param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
7696 ctx
->param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
7698 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7699 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
7700 sizeof(LLVMTypeRef
));
7701 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
7702 key
->vs_prolog
.last_input
+ 1) *
7703 sizeof(LLVMTypeRef
));
7707 /* Declare input and output SGPRs. */
7709 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7710 params
[num_params
++] = ctx
->i32
;
7711 returns
[num_returns
++] = ctx
->i32
;
7713 last_sgpr
= num_params
- 1;
7715 /* 4 preloaded VGPRs (outputs must be floats) */
7716 for (i
= 0; i
< 4; i
++) {
7717 params
[num_params
++] = ctx
->i32
;
7718 returns
[num_returns
++] = ctx
->f32
;
7721 /* Vertex load indices. */
7722 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7723 returns
[num_returns
++] = ctx
->f32
;
7725 /* Create the function. */
7726 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, params
,
7727 num_params
, last_sgpr
);
7728 func
= ctx
->main_fn
;
7730 /* Copy inputs to outputs. This should be no-op, as the registers match,
7731 * but it will prevent the compiler from overwriting them unintentionally.
7733 ret
= ctx
->return_value
;
7734 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7735 LLVMValueRef p
= LLVMGetParam(func
, i
);
7736 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7738 for (i
= num_params
- 4; i
< num_params
; i
++) {
7739 LLVMValueRef p
= LLVMGetParam(func
, i
);
7740 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
->f32
, "");
7741 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7744 /* Compute vertex load indices from instance divisors. */
7745 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7746 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
7750 /* InstanceID / Divisor + StartInstance */
7751 index
= get_instance_index_for_fetch(ctx
,
7752 SI_SGPR_START_INSTANCE
,
7755 /* VertexID + BaseVertex */
7756 index
= LLVMBuildAdd(gallivm
->builder
,
7757 LLVMGetParam(func
, ctx
->param_vertex_id
),
7758 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
7761 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
->f32
, "");
7762 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
7766 si_llvm_build_ret(ctx
, ret
);
7770 * Build the vertex shader epilog function. This is also used by the tessellation
7771 * evaluation shader compiled as VS.
7773 * The input is PrimitiveID.
7775 * If PrimitiveID is required by the pixel shader, export it.
7776 * Otherwise, do nothing.
7778 static void si_build_vs_epilog_function(struct si_shader_context
*ctx
,
7779 union si_shader_part_key
*key
)
7781 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7782 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7783 LLVMTypeRef params
[5];
7786 /* Declare input VGPRs. */
7787 num_params
= key
->vs_epilog
.states
.export_prim_id
?
7788 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
7789 assert(num_params
<= ARRAY_SIZE(params
));
7791 for (i
= 0; i
< num_params
; i
++)
7792 params
[i
] = ctx
->f32
;
7794 /* Create the function. */
7795 si_create_function(ctx
, "vs_epilog", NULL
, 0, params
, num_params
, -1);
7798 if (key
->vs_epilog
.states
.export_prim_id
) {
7799 struct lp_build_context
*base
= &bld_base
->base
;
7800 struct ac_export_args args
;
7802 args
.enabled_channels
= 0x1; /* enabled channels */
7803 args
.valid_mask
= 0; /* whether the EXEC mask is valid */
7804 args
.done
= 0; /* DONE bit */
7805 args
.target
= V_008DFC_SQ_EXP_PARAM
+
7806 key
->vs_epilog
.prim_id_param_offset
;
7807 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
7808 args
.out
[0] = LLVMGetParam(ctx
->main_fn
,
7809 VS_EPILOG_PRIMID_LOC
); /* X */
7810 args
.out
[1] = base
->undef
; /* Y */
7811 args
.out
[2] = base
->undef
; /* Z */
7812 args
.out
[3] = base
->undef
; /* W */
7814 ac_build_export(&ctx
->ac
, &args
);
7817 LLVMBuildRetVoid(gallivm
->builder
);
7821 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
7823 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
7824 LLVMTargetMachineRef tm
,
7825 struct si_shader
*shader
,
7826 struct pipe_debug_callback
*debug
,
7827 struct si_vs_epilog_bits
*states
)
7829 union si_shader_part_key epilog_key
;
7831 si_get_vs_epilog_key(shader
, states
, &epilog_key
);
7833 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
7834 PIPE_SHADER_VERTEX
, true,
7835 &epilog_key
, tm
, debug
,
7836 si_build_vs_epilog_function
,
7837 "Vertex Shader Epilog");
7838 return shader
->epilog
!= NULL
;
7842 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7844 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7845 LLVMTargetMachineRef tm
,
7846 struct si_shader
*shader
,
7847 struct pipe_debug_callback
*debug
)
7849 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
7850 union si_shader_part_key prolog_key
;
7852 /* Get the prolog. */
7853 si_get_vs_prolog_key(shader
, &prolog_key
);
7855 /* The prolog is a no-op if there are no inputs. */
7856 if (info
->num_inputs
) {
7858 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7859 PIPE_SHADER_VERTEX
, true,
7860 &prolog_key
, tm
, debug
,
7861 si_build_vs_prolog_function
,
7862 "Vertex Shader Prolog");
7863 if (!shader
->prolog
)
7867 /* Get the epilog. */
7868 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
&&
7869 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7870 &shader
->key
.part
.vs
.epilog
))
7877 * Select and compile (or reuse) TES parts (epilog).
7879 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
7880 LLVMTargetMachineRef tm
,
7881 struct si_shader
*shader
,
7882 struct pipe_debug_callback
*debug
)
7884 if (shader
->key
.as_es
)
7887 /* TES compiled as VS. */
7888 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7889 &shader
->key
.part
.tes
.epilog
);
7893 * Compile the TCS epilog function. This writes tesselation factors to memory
7894 * based on the output primitive type of the tesselator (determined by TES).
7896 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7897 union si_shader_part_key
*key
)
7899 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7900 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7901 LLVMTypeRef params
[16];
7903 int last_sgpr
, num_params
;
7905 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
7906 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
7907 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
7908 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
7909 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
7910 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
7911 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
7912 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
7913 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
7914 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
7915 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
7916 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
7917 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
7918 num_params
= last_sgpr
+ 1;
7920 params
[num_params
++] = ctx
->i32
; /* patch index within the wave (REL_PATCH_ID) */
7921 params
[num_params
++] = ctx
->i32
; /* invocation ID within the patch */
7922 params
[num_params
++] = ctx
->i32
; /* LDS offset where tess factors should be loaded from */
7924 /* Create the function. */
7925 si_create_function(ctx
, "tcs_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
7926 declare_tess_lds(ctx
);
7927 func
= ctx
->main_fn
;
7929 si_write_tess_factors(bld_base
,
7930 LLVMGetParam(func
, last_sgpr
+ 1),
7931 LLVMGetParam(func
, last_sgpr
+ 2),
7932 LLVMGetParam(func
, last_sgpr
+ 3));
7934 LLVMBuildRetVoid(gallivm
->builder
);
7938 * Select and compile (or reuse) TCS parts (epilog).
7940 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7941 LLVMTargetMachineRef tm
,
7942 struct si_shader
*shader
,
7943 struct pipe_debug_callback
*debug
)
7945 union si_shader_part_key epilog_key
;
7947 /* Get the epilog. */
7948 memset(&epilog_key
, 0, sizeof(epilog_key
));
7949 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7951 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7952 PIPE_SHADER_TESS_CTRL
, false,
7953 &epilog_key
, tm
, debug
,
7954 si_build_tcs_epilog_function
,
7955 "Tessellation Control Shader Epilog");
7956 return shader
->epilog
!= NULL
;
7960 * Select and compile (or reuse) GS parts (prolog).
7962 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7963 LLVMTargetMachineRef tm
,
7964 struct si_shader
*shader
,
7965 struct pipe_debug_callback
*debug
)
7967 union si_shader_part_key prolog_key
;
7969 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7972 memset(&prolog_key
, 0, sizeof(prolog_key
));
7973 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7975 shader
->prolog
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7976 PIPE_SHADER_GEOMETRY
, true,
7977 &prolog_key
, tm
, debug
,
7978 si_build_gs_prolog_function
,
7979 "Geometry Shader Prolog");
7980 return shader
->prolog
!= NULL
;
7984 * Build the pixel shader prolog function. This handles:
7985 * - two-side color selection and interpolation
7986 * - overriding interpolation parameters for the API PS
7987 * - polygon stippling
7989 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7990 * overriden by other states. (e.g. per-sample interpolation)
7991 * Interpolated colors are stored after the preloaded VGPRs.
7993 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7994 union si_shader_part_key
*key
)
7996 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7997 LLVMTypeRef
*params
;
7998 LLVMValueRef ret
, func
;
7999 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
8001 assert(si_need_ps_prolog(key
));
8003 /* Number of inputs + 8 color elements. */
8004 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
8005 key
->ps_prolog
.num_input_vgprs
+ 8) *
8006 sizeof(LLVMTypeRef
));
8008 /* Declare inputs. */
8010 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
8011 params
[num_params
++] = ctx
->i32
;
8012 last_sgpr
= num_params
- 1;
8014 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
8015 params
[num_params
++] = ctx
->f32
;
8017 /* Declare outputs (same as inputs + add colors if needed) */
8018 num_returns
= num_params
;
8019 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
8020 for (i
= 0; i
< num_color_channels
; i
++)
8021 params
[num_returns
++] = ctx
->f32
;
8023 /* Create the function. */
8024 si_create_function(ctx
, "ps_prolog", params
, num_returns
, params
,
8025 num_params
, last_sgpr
);
8026 func
= ctx
->main_fn
;
8028 /* Copy inputs to outputs. This should be no-op, as the registers match,
8029 * but it will prevent the compiler from overwriting them unintentionally.
8031 ret
= ctx
->return_value
;
8032 for (i
= 0; i
< num_params
; i
++) {
8033 LLVMValueRef p
= LLVMGetParam(func
, i
);
8034 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
8037 /* Polygon stippling. */
8038 if (key
->ps_prolog
.states
.poly_stipple
) {
8039 /* POS_FIXED_PT is always last. */
8040 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
8041 key
->ps_prolog
.num_input_vgprs
- 1;
8042 LLVMValueRef ptr
[2], list
;
8044 /* Get the pointer to rw buffers. */
8045 ptr
[0] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS
);
8046 ptr
[1] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS_HI
);
8047 list
= lp_build_gather_values(gallivm
, ptr
, 2);
8048 list
= LLVMBuildBitCast(gallivm
->builder
, list
, ctx
->i64
, "");
8049 list
= LLVMBuildIntToPtr(gallivm
->builder
, list
,
8050 const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
), "");
8052 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
8055 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
8056 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
8057 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8058 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
8060 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
8061 * The hw doesn't compute CENTROID if the whole wave only
8062 * contains fully-covered quads.
8064 * PRIM_MASK is after user SGPRs.
8066 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
8067 bc_optimize
= LLVMBuildLShr(gallivm
->builder
, bc_optimize
,
8068 LLVMConstInt(ctx
->i32
, 31, 0), "");
8069 bc_optimize
= LLVMBuildTrunc(gallivm
->builder
, bc_optimize
,
8072 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
8073 /* Read PERSP_CENTER. */
8074 for (i
= 0; i
< 2; i
++)
8075 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
8076 /* Read PERSP_CENTROID. */
8077 for (i
= 0; i
< 2; i
++)
8078 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
8079 /* Select PERSP_CENTROID. */
8080 for (i
= 0; i
< 2; i
++) {
8081 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
8082 center
[i
], centroid
[i
], "");
8083 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8084 tmp
, base
+ 4 + i
, "");
8087 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
8088 /* Read LINEAR_CENTER. */
8089 for (i
= 0; i
< 2; i
++)
8090 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
8091 /* Read LINEAR_CENTROID. */
8092 for (i
= 0; i
< 2; i
++)
8093 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
8094 /* Select LINEAR_CENTROID. */
8095 for (i
= 0; i
< 2; i
++) {
8096 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
8097 center
[i
], centroid
[i
], "");
8098 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8099 tmp
, base
+ 10 + i
, "");
8104 /* Force per-sample interpolation. */
8105 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
8106 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8107 LLVMValueRef persp_sample
[2];
8109 /* Read PERSP_SAMPLE. */
8110 for (i
= 0; i
< 2; i
++)
8111 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
8112 /* Overwrite PERSP_CENTER. */
8113 for (i
= 0; i
< 2; i
++)
8114 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8115 persp_sample
[i
], base
+ 2 + i
, "");
8116 /* Overwrite PERSP_CENTROID. */
8117 for (i
= 0; i
< 2; i
++)
8118 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8119 persp_sample
[i
], base
+ 4 + i
, "");
8121 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
8122 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8123 LLVMValueRef linear_sample
[2];
8125 /* Read LINEAR_SAMPLE. */
8126 for (i
= 0; i
< 2; i
++)
8127 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
8128 /* Overwrite LINEAR_CENTER. */
8129 for (i
= 0; i
< 2; i
++)
8130 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8131 linear_sample
[i
], base
+ 8 + i
, "");
8132 /* Overwrite LINEAR_CENTROID. */
8133 for (i
= 0; i
< 2; i
++)
8134 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8135 linear_sample
[i
], base
+ 10 + i
, "");
8138 /* Force center interpolation. */
8139 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
8140 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8141 LLVMValueRef persp_center
[2];
8143 /* Read PERSP_CENTER. */
8144 for (i
= 0; i
< 2; i
++)
8145 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
8146 /* Overwrite PERSP_SAMPLE. */
8147 for (i
= 0; i
< 2; i
++)
8148 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8149 persp_center
[i
], base
+ i
, "");
8150 /* Overwrite PERSP_CENTROID. */
8151 for (i
= 0; i
< 2; i
++)
8152 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8153 persp_center
[i
], base
+ 4 + i
, "");
8155 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
8156 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8157 LLVMValueRef linear_center
[2];
8159 /* Read LINEAR_CENTER. */
8160 for (i
= 0; i
< 2; i
++)
8161 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
8162 /* Overwrite LINEAR_SAMPLE. */
8163 for (i
= 0; i
< 2; i
++)
8164 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8165 linear_center
[i
], base
+ 6 + i
, "");
8166 /* Overwrite LINEAR_CENTROID. */
8167 for (i
= 0; i
< 2; i
++)
8168 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8169 linear_center
[i
], base
+ 10 + i
, "");
8172 /* Interpolate colors. */
8173 for (i
= 0; i
< 2; i
++) {
8174 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
8175 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8176 key
->ps_prolog
.face_vgpr_index
;
8177 LLVMValueRef interp
[2], color
[4];
8178 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
8183 /* If the interpolation qualifier is not CONSTANT (-1). */
8184 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
8185 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8186 key
->ps_prolog
.color_interp_vgpr_index
[i
];
8188 /* Get the (i,j) updated by bc_optimize handling. */
8189 interp
[0] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8191 interp
[1] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8192 interp_vgpr
+ 1, "");
8193 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
8196 /* Use the absolute location of the input. */
8197 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
8199 if (key
->ps_prolog
.states
.color_two_side
) {
8200 face
= LLVMGetParam(func
, face_vgpr
);
8201 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
->i32
, "");
8204 interp_fs_input(ctx
,
8205 key
->ps_prolog
.color_attr_index
[i
],
8206 TGSI_SEMANTIC_COLOR
, i
,
8207 key
->ps_prolog
.num_interp_inputs
,
8208 key
->ps_prolog
.colors_read
, interp_ij
,
8209 prim_mask
, face
, color
);
8212 unsigned chan
= u_bit_scan(&writemask
);
8213 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
8218 /* Tell LLVM to insert WQM instruction sequence when needed. */
8219 if (key
->ps_prolog
.wqm
) {
8220 LLVMAddTargetDependentFunctionAttr(func
,
8221 "amdgpu-ps-wqm-outputs", "");
8224 si_llvm_build_ret(ctx
, ret
);
8228 * Build the pixel shader epilog function. This handles everything that must be
8229 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
8231 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
8232 union si_shader_part_key
*key
)
8234 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
8235 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
8236 LLVMTypeRef params
[16+8*4+3];
8237 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
8238 int last_sgpr
, num_params
, i
;
8239 struct si_ps_exports exp
= {};
8241 /* Declare input SGPRs. */
8242 params
[SI_PARAM_RW_BUFFERS
] = ctx
->i64
;
8243 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
8244 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
8245 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
8246 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
8247 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
8248 last_sgpr
= SI_PARAM_ALPHA_REF
;
8250 /* Declare input VGPRs. */
8251 num_params
= (last_sgpr
+ 1) +
8252 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
8253 key
->ps_epilog
.writes_z
+
8254 key
->ps_epilog
.writes_stencil
+
8255 key
->ps_epilog
.writes_samplemask
;
8257 num_params
= MAX2(num_params
,
8258 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
8260 assert(num_params
<= ARRAY_SIZE(params
));
8262 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
8263 params
[i
] = ctx
->f32
;
8265 /* Create the function. */
8266 si_create_function(ctx
, "ps_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
8267 /* Disable elimination of unused inputs. */
8268 si_llvm_add_attribute(ctx
->main_fn
,
8269 "InitialPSInputAddr", 0xffffff);
8271 /* Process colors. */
8272 unsigned vgpr
= last_sgpr
+ 1;
8273 unsigned colors_written
= key
->ps_epilog
.colors_written
;
8274 int last_color_export
= -1;
8276 /* Find the last color export. */
8277 if (!key
->ps_epilog
.writes_z
&&
8278 !key
->ps_epilog
.writes_stencil
&&
8279 !key
->ps_epilog
.writes_samplemask
) {
8280 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
8282 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
8283 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
8284 /* Just set this if any of the colorbuffers are enabled. */
8286 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
8287 last_color_export
= 0;
8289 for (i
= 0; i
< 8; i
++)
8290 if (colors_written
& (1 << i
) &&
8291 (spi_format
>> (i
* 4)) & 0xf)
8292 last_color_export
= i
;
8296 while (colors_written
) {
8297 LLVMValueRef color
[4];
8298 int mrt
= u_bit_scan(&colors_written
);
8300 for (i
= 0; i
< 4; i
++)
8301 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
8303 si_export_mrt_color(bld_base
, color
, mrt
,
8305 mrt
== last_color_export
, &exp
);
8308 /* Process depth, stencil, samplemask. */
8309 if (key
->ps_epilog
.writes_z
)
8310 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8311 if (key
->ps_epilog
.writes_stencil
)
8312 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8313 if (key
->ps_epilog
.writes_samplemask
)
8314 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8316 if (depth
|| stencil
|| samplemask
)
8317 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
8318 else if (last_color_export
== -1)
8319 si_export_null(bld_base
);
8322 si_emit_ps_exports(ctx
, &exp
);
8325 LLVMBuildRetVoid(gallivm
->builder
);
8329 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
8331 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
8332 LLVMTargetMachineRef tm
,
8333 struct si_shader
*shader
,
8334 struct pipe_debug_callback
*debug
)
8336 union si_shader_part_key prolog_key
;
8337 union si_shader_part_key epilog_key
;
8339 /* Get the prolog. */
8340 si_get_ps_prolog_key(shader
, &prolog_key
, true);
8342 /* The prolog is a no-op if these aren't set. */
8343 if (si_need_ps_prolog(&prolog_key
)) {
8345 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
8346 PIPE_SHADER_FRAGMENT
, true,
8347 &prolog_key
, tm
, debug
,
8348 si_build_ps_prolog_function
,
8349 "Fragment Shader Prolog");
8350 if (!shader
->prolog
)
8354 /* Get the epilog. */
8355 si_get_ps_epilog_key(shader
, &epilog_key
);
8358 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
8359 PIPE_SHADER_FRAGMENT
, false,
8360 &epilog_key
, tm
, debug
,
8361 si_build_ps_epilog_function
,
8362 "Fragment Shader Epilog");
8363 if (!shader
->epilog
)
8366 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
8367 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
8368 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
8369 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
8372 /* Set up the enable bits for per-sample shading if needed. */
8373 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
8374 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8375 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8376 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
8377 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8378 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
8380 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
8381 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8382 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8383 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
8384 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8385 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
8387 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
8388 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8389 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8390 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
8391 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8392 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8394 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
8395 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8396 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8397 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
8398 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8399 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8402 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
8403 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
8404 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
8405 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8406 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8409 /* At least one pair of interpolation weights must be enabled. */
8410 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
8411 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8412 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8415 /* The sample mask input is always enabled, because the API shader always
8416 * passes it through to the epilog. Disable it here if it's unused.
8418 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
8419 !shader
->selector
->info
.reads_samplemask
)
8420 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
8425 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
8428 /* SPI barrier management bug:
8429 * Make sure we have at least 4k of LDS in use to avoid the bug.
8430 * It applies to workgroup sizes of more than one wavefront.
8432 if (sscreen
->b
.family
== CHIP_BONAIRE
||
8433 sscreen
->b
.family
== CHIP_KABINI
||
8434 sscreen
->b
.family
== CHIP_MULLINS
)
8435 *lds_size
= MAX2(*lds_size
, 8);
8438 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8439 struct si_shader
*shader
)
8441 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8443 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8445 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8446 si_get_max_workgroup_size(shader
) > 64) {
8447 si_multiwave_lds_size_workaround(sscreen
,
8448 &shader
->config
.lds_size
);
8452 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
8453 struct si_shader
*shader
,
8454 struct pipe_debug_callback
*debug
)
8456 struct si_shader_selector
*sel
= shader
->selector
;
8457 struct si_shader
*mainp
= *si_get_main_shader_part(sel
, &shader
->key
);
8460 /* LS, ES, VS are compiled on demand if the main part hasn't been
8461 * compiled for that stage.
8463 * Vertex shaders are compiled on demand when a vertex fetch
8464 * workaround must be applied.
8466 if (shader
->is_monolithic
) {
8467 /* Monolithic shader (compiled as a whole, has many variants,
8468 * may take a long time to compile).
8470 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
8474 /* The shader consists of 2-3 parts:
8476 * - the middle part is the user shader, it has 1 variant only
8477 * and it was compiled during the creation of the shader
8479 * - the prolog part is inserted at the beginning
8480 * - the epilog part is inserted at the end
8482 * The prolog and epilog have many (but simple) variants.
8485 /* Copy the compiled TGSI shader data over. */
8486 shader
->is_binary_shared
= true;
8487 shader
->binary
= mainp
->binary
;
8488 shader
->config
= mainp
->config
;
8489 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8490 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8491 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8492 memcpy(shader
->info
.vs_output_param_offset
,
8493 mainp
->info
.vs_output_param_offset
,
8494 sizeof(mainp
->info
.vs_output_param_offset
));
8495 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8496 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8497 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8499 /* Select prologs and/or epilogs. */
8500 switch (sel
->type
) {
8501 case PIPE_SHADER_VERTEX
:
8502 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
8505 case PIPE_SHADER_TESS_CTRL
:
8506 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
8509 case PIPE_SHADER_TESS_EVAL
:
8510 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
8513 case PIPE_SHADER_GEOMETRY
:
8514 if (!si_shader_select_gs_parts(sscreen
, tm
, shader
, debug
))
8517 case PIPE_SHADER_FRAGMENT
:
8518 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
8521 /* Make sure we have at least as many VGPRs as there
8522 * are allocated inputs.
8524 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8525 shader
->info
.num_input_vgprs
);
8529 /* Update SGPR and VGPR counts. */
8530 if (shader
->prolog
) {
8531 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8532 shader
->prolog
->config
.num_sgprs
);
8533 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8534 shader
->prolog
->config
.num_vgprs
);
8536 if (shader
->epilog
) {
8537 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8538 shader
->epilog
->config
.num_sgprs
);
8539 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8540 shader
->epilog
->config
.num_vgprs
);
8544 si_fix_resource_usage(sscreen
, shader
);
8545 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8549 r
= si_shader_binary_upload(sscreen
, shader
);
8551 fprintf(stderr
, "LLVM failed to upload shader\n");
8558 void si_shader_destroy(struct si_shader
*shader
)
8560 if (shader
->scratch_bo
)
8561 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8563 r600_resource_reference(&shader
->bo
, NULL
);
8565 if (!shader
->is_binary_shared
)
8566 radeon_shader_binary_clean(&shader
->binary
);
8568 free(shader
->shader_log
);