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 "radeon/radeon_elf_util.h"
37 #include "util/u_memory.h"
38 #include "util/u_string.h"
39 #include "tgsi/tgsi_build.h"
40 #include "tgsi/tgsi_util.h"
41 #include "tgsi/tgsi_dump.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 #define SENDMSG_GS_DONE 3
104 #define SENDMSG_GS_OP_NOP (0 << 4)
105 #define SENDMSG_GS_OP_CUT (1 << 4)
106 #define SENDMSG_GS_OP_EMIT (2 << 4)
107 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
110 * Returns a unique index for a semantic name and index. The index must be
111 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
114 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
116 switch (semantic_name
) {
117 case TGSI_SEMANTIC_POSITION
:
119 case TGSI_SEMANTIC_PSIZE
:
121 case TGSI_SEMANTIC_CLIPDIST
:
124 case TGSI_SEMANTIC_GENERIC
:
128 assert(!"invalid generic index");
131 /* patch indices are completely separate and thus start from 0 */
132 case TGSI_SEMANTIC_TESSOUTER
:
134 case TGSI_SEMANTIC_TESSINNER
:
136 case TGSI_SEMANTIC_PATCH
:
140 assert(!"invalid semantic name");
145 unsigned si_shader_io_get_unique_index2(unsigned name
, unsigned index
)
148 case TGSI_SEMANTIC_FOG
:
150 case TGSI_SEMANTIC_LAYER
:
152 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
154 case TGSI_SEMANTIC_PRIMID
:
156 case TGSI_SEMANTIC_COLOR
: /* these alias */
157 case TGSI_SEMANTIC_BCOLOR
:
159 case TGSI_SEMANTIC_TEXCOORD
:
162 assert(!"invalid semantic name");
168 * Get the value of a shader input parameter and extract a bitfield.
170 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
171 unsigned param
, unsigned rshift
,
174 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
175 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
,
178 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
179 value
= bitcast(&ctx
->bld_base
,
180 TGSI_TYPE_UNSIGNED
, value
);
183 value
= LLVMBuildLShr(gallivm
->builder
, value
,
184 lp_build_const_int32(gallivm
, rshift
), "");
186 if (rshift
+ bitwidth
< 32) {
187 unsigned mask
= (1 << bitwidth
) - 1;
188 value
= LLVMBuildAnd(gallivm
->builder
, value
,
189 lp_build_const_int32(gallivm
, mask
), "");
195 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
198 case PIPE_SHADER_TESS_CTRL
:
199 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
201 case PIPE_SHADER_TESS_EVAL
:
202 return LLVMGetParam(ctx
->main_fn
,
203 ctx
->param_tes_rel_patch_id
);
211 /* Tessellation shaders pass outputs to the next shader using LDS.
213 * LS outputs = TCS inputs
214 * TCS outputs = TES inputs
217 * - TCS inputs for patch 0
218 * - TCS inputs for patch 1
219 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
221 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
222 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
223 * - TCS outputs for patch 1
224 * - Per-patch TCS outputs for patch 1
225 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
226 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
229 * All three shaders VS(LS), TCS, TES share the same LDS space.
233 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
235 if (ctx
->type
== PIPE_SHADER_VERTEX
)
236 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
237 else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
238 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
246 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
248 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
252 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
254 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
256 SI_PARAM_TCS_OUT_OFFSETS
,
262 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
264 return lp_build_mul_imm(&ctx
->bld_base
.uint_bld
,
266 SI_PARAM_TCS_OUT_OFFSETS
,
272 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
274 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
275 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
276 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
278 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
282 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
284 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
285 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
286 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
287 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
289 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
290 LLVMBuildMul(gallivm
->builder
, patch_stride
,
296 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
298 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
299 LLVMValueRef patch0_patch_data_offset
=
300 get_tcs_out_patch0_patch_data_offset(ctx
);
301 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
302 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
304 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
305 LLVMBuildMul(gallivm
->builder
, patch_stride
,
310 static LLVMValueRef
get_instance_index_for_fetch(
311 struct si_shader_context
*radeon_bld
,
312 unsigned param_start_instance
, unsigned divisor
)
314 struct si_shader_context
*ctx
=
315 si_shader_context(&radeon_bld
->bld_base
);
316 struct gallivm_state
*gallivm
= radeon_bld
->bld_base
.base
.gallivm
;
318 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
319 ctx
->param_instance_id
);
321 /* The division must be done before START_INSTANCE is added. */
323 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
324 lp_build_const_int32(gallivm
, divisor
), "");
326 return LLVMBuildAdd(gallivm
->builder
, result
,
327 LLVMGetParam(radeon_bld
->main_fn
, param_start_instance
), "");
330 static void declare_input_vs(
331 struct si_shader_context
*ctx
,
332 unsigned input_index
,
333 const struct tgsi_full_declaration
*decl
,
336 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
337 struct gallivm_state
*gallivm
= base
->gallivm
;
342 LLVMValueRef t_list_ptr
;
343 LLVMValueRef t_offset
;
345 LLVMValueRef attribute_offset
;
346 LLVMValueRef buffer_index
;
347 LLVMValueRef args
[3];
350 /* Load the T list */
351 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_VERTEX_BUFFERS
);
353 t_offset
= lp_build_const_int32(gallivm
, input_index
);
355 t_list
= ac_build_indexed_load_const(&ctx
->ac
, t_list_ptr
, t_offset
);
357 /* Build the attribute offset */
358 attribute_offset
= lp_build_const_int32(gallivm
, 0);
360 buffer_index
= LLVMGetParam(ctx
->main_fn
,
361 ctx
->param_vertex_index0
+
365 args
[1] = attribute_offset
;
366 args
[2] = buffer_index
;
367 input
= lp_build_intrinsic(gallivm
->builder
,
368 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
369 LP_FUNC_ATTR_READNONE
);
371 /* Break up the vec4 into individual components */
372 for (chan
= 0; chan
< 4; chan
++) {
373 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
374 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
375 input
, llvm_chan
, "");
378 fix_fetch
= (ctx
->shader
->key
.mono
.vs
.fix_fetch
>> (4 * input_index
)) & 0xf;
381 case SI_FIX_FETCH_A2_SNORM
:
382 case SI_FIX_FETCH_A2_SSCALED
:
383 case SI_FIX_FETCH_A2_SINT
: {
384 /* The hardware returns an unsigned value; convert it to a
387 LLVMValueRef tmp
= out
[3];
388 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
390 /* First, recover the sign-extended signed integer value. */
391 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
392 tmp
= LLVMBuildFPToUI(gallivm
->builder
, tmp
, ctx
->i32
, "");
394 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->i32
, "");
396 /* For the integer-like cases, do a natural sign extension.
398 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
399 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
402 tmp
= LLVMBuildShl(gallivm
->builder
, tmp
,
403 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
404 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
405 tmp
= LLVMBuildAShr(gallivm
->builder
, tmp
, c30
, "");
407 /* Convert back to the right type. */
408 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
410 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
411 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
412 clamp
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
413 tmp
= LLVMBuildSelect(gallivm
->builder
, clamp
, neg_one
, tmp
, "");
414 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
415 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
421 case SI_FIX_FETCH_RGBA_32_UNORM
:
422 case SI_FIX_FETCH_RGBX_32_UNORM
:
423 for (chan
= 0; chan
< 4; chan
++) {
424 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
426 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
427 out
[chan
], ctx
->f32
, "");
428 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
429 LLVMConstReal(ctx
->f32
, 1.0 / UINT_MAX
), "");
431 /* RGBX UINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
432 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_UNORM
)
433 out
[3] = LLVMConstReal(ctx
->f32
, 1);
435 case SI_FIX_FETCH_RGBA_32_SNORM
:
436 case SI_FIX_FETCH_RGBX_32_SNORM
:
437 case SI_FIX_FETCH_RGBA_32_FIXED
:
438 case SI_FIX_FETCH_RGBX_32_FIXED
: {
440 if (fix_fetch
>= SI_FIX_FETCH_RGBA_32_FIXED
)
441 scale
= 1.0 / 0x10000;
443 scale
= 1.0 / INT_MAX
;
445 for (chan
= 0; chan
< 4; chan
++) {
446 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
448 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
449 out
[chan
], ctx
->f32
, "");
450 out
[chan
] = LLVMBuildFMul(gallivm
->builder
, out
[chan
],
451 LLVMConstReal(ctx
->f32
, scale
), "");
453 /* RGBX SINT returns 1 in alpha, which would be rounded to 0 by normalizing. */
454 if (fix_fetch
== SI_FIX_FETCH_RGBX_32_SNORM
||
455 fix_fetch
== SI_FIX_FETCH_RGBX_32_FIXED
)
456 out
[3] = LLVMConstReal(ctx
->f32
, 1);
459 case SI_FIX_FETCH_RGBA_32_USCALED
:
460 for (chan
= 0; chan
< 4; chan
++) {
461 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
463 out
[chan
] = LLVMBuildUIToFP(gallivm
->builder
,
464 out
[chan
], ctx
->f32
, "");
467 case SI_FIX_FETCH_RGBA_32_SSCALED
:
468 for (chan
= 0; chan
< 4; chan
++) {
469 out
[chan
] = LLVMBuildBitCast(gallivm
->builder
, out
[chan
],
471 out
[chan
] = LLVMBuildSIToFP(gallivm
->builder
,
472 out
[chan
], ctx
->f32
, "");
478 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
481 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
484 return bld_base
->uint_bld
.zero
;
487 case PIPE_SHADER_VERTEX
:
488 return LLVMGetParam(ctx
->main_fn
,
489 ctx
->param_vs_prim_id
);
490 case PIPE_SHADER_TESS_CTRL
:
491 return LLVMGetParam(ctx
->main_fn
,
493 case PIPE_SHADER_TESS_EVAL
:
494 return LLVMGetParam(ctx
->main_fn
,
495 ctx
->param_tes_patch_id
);
496 case PIPE_SHADER_GEOMETRY
:
497 return LLVMGetParam(ctx
->main_fn
,
498 SI_PARAM_PRIMITIVE_ID
);
501 return bld_base
->uint_bld
.zero
;
506 * Return the value of tgsi_ind_register for indexing.
507 * This is the indirect index with the constant offset added to it.
509 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
510 const struct tgsi_ind_register
*ind
,
513 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
516 result
= ctx
->addrs
[ind
->Index
][ind
->Swizzle
];
517 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
518 result
= LLVMBuildAdd(gallivm
->builder
, result
,
519 lp_build_const_int32(gallivm
, rel_index
), "");
524 * Like get_indirect_index, but restricts the return value to a (possibly
525 * undefined) value inside [0..num).
527 static LLVMValueRef
get_bounded_indirect_index(struct si_shader_context
*ctx
,
528 const struct tgsi_ind_register
*ind
,
529 int rel_index
, unsigned num
)
531 LLVMValueRef result
= get_indirect_index(ctx
, ind
, rel_index
);
533 /* LLVM 3.8: If indirect resource indexing is used:
537 if (HAVE_LLVM
<= 0x0308)
538 return LLVMGetUndef(ctx
->i32
);
540 return si_llvm_bound_index(ctx
, result
, num
);
545 * Calculate a dword address given an input or output register and a stride.
547 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
548 const struct tgsi_full_dst_register
*dst
,
549 const struct tgsi_full_src_register
*src
,
550 LLVMValueRef vertex_dw_stride
,
551 LLVMValueRef base_addr
)
553 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
554 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
555 ubyte
*name
, *index
, *array_first
;
557 struct tgsi_full_dst_register reg
;
559 /* Set the register description. The address computation is the same
560 * for sources and destinations. */
562 reg
.Register
.File
= src
->Register
.File
;
563 reg
.Register
.Index
= src
->Register
.Index
;
564 reg
.Register
.Indirect
= src
->Register
.Indirect
;
565 reg
.Register
.Dimension
= src
->Register
.Dimension
;
566 reg
.Indirect
= src
->Indirect
;
567 reg
.Dimension
= src
->Dimension
;
568 reg
.DimIndirect
= src
->DimIndirect
;
572 /* If the register is 2-dimensional (e.g. an array of vertices
573 * in a primitive), calculate the base address of the vertex. */
574 if (reg
.Register
.Dimension
) {
577 if (reg
.Dimension
.Indirect
)
578 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
579 reg
.Dimension
.Index
);
581 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
583 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
584 LLVMBuildMul(gallivm
->builder
, index
,
585 vertex_dw_stride
, ""), "");
588 /* Get information about the register. */
589 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
590 name
= info
->input_semantic_name
;
591 index
= info
->input_semantic_index
;
592 array_first
= info
->input_array_first
;
593 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
594 name
= info
->output_semantic_name
;
595 index
= info
->output_semantic_index
;
596 array_first
= info
->output_array_first
;
602 if (reg
.Register
.Indirect
) {
603 /* Add the relative address of the element. */
604 LLVMValueRef ind_index
;
606 if (reg
.Indirect
.ArrayID
)
607 first
= array_first
[reg
.Indirect
.ArrayID
];
609 first
= reg
.Register
.Index
;
611 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
612 reg
.Register
.Index
- first
);
614 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
615 LLVMBuildMul(gallivm
->builder
, ind_index
,
616 lp_build_const_int32(gallivm
, 4), ""), "");
618 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
620 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
621 index
[reg
.Register
.Index
]);
624 /* Add the base address of the element. */
625 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
626 lp_build_const_int32(gallivm
, param
* 4), "");
629 /* The offchip buffer layout for TCS->TES is
631 * - attribute 0 of patch 0 vertex 0
632 * - attribute 0 of patch 0 vertex 1
633 * - attribute 0 of patch 0 vertex 2
635 * - attribute 0 of patch 1 vertex 0
636 * - attribute 0 of patch 1 vertex 1
638 * - attribute 1 of patch 0 vertex 0
639 * - attribute 1 of patch 0 vertex 1
641 * - per patch attribute 0 of patch 0
642 * - per patch attribute 0 of patch 1
645 * Note that every attribute has 4 components.
647 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
648 LLVMValueRef vertex_index
,
649 LLVMValueRef param_index
)
651 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
652 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
653 LLVMValueRef param_stride
, constant16
;
655 vertices_per_patch
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 6);
656 num_patches
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 0, 9);
657 total_vertices
= LLVMBuildMul(gallivm
->builder
, vertices_per_patch
,
660 constant16
= lp_build_const_int32(gallivm
, 16);
662 base_addr
= LLVMBuildMul(gallivm
->builder
, get_rel_patch_id(ctx
),
663 vertices_per_patch
, "");
665 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
668 param_stride
= total_vertices
;
670 base_addr
= get_rel_patch_id(ctx
);
671 param_stride
= num_patches
;
674 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
675 LLVMBuildMul(gallivm
->builder
, param_index
,
676 param_stride
, ""), "");
678 base_addr
= LLVMBuildMul(gallivm
->builder
, base_addr
, constant16
, "");
681 LLVMValueRef patch_data_offset
=
682 unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 16, 16);
684 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
685 patch_data_offset
, "");
690 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
691 struct si_shader_context
*ctx
,
692 const struct tgsi_full_dst_register
*dst
,
693 const struct tgsi_full_src_register
*src
)
695 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
696 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
697 ubyte
*name
, *index
, *array_first
;
698 struct tgsi_full_src_register reg
;
699 LLVMValueRef vertex_index
= NULL
;
700 LLVMValueRef param_index
= NULL
;
701 unsigned param_index_base
, param_base
;
703 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
705 if (reg
.Register
.Dimension
) {
707 if (reg
.Dimension
.Indirect
)
708 vertex_index
= get_indirect_index(ctx
, ®
.DimIndirect
,
709 reg
.Dimension
.Index
);
711 vertex_index
= lp_build_const_int32(gallivm
,
712 reg
.Dimension
.Index
);
715 /* Get information about the register. */
716 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
717 name
= info
->input_semantic_name
;
718 index
= info
->input_semantic_index
;
719 array_first
= info
->input_array_first
;
720 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
721 name
= info
->output_semantic_name
;
722 index
= info
->output_semantic_index
;
723 array_first
= info
->output_array_first
;
729 if (reg
.Register
.Indirect
) {
730 if (reg
.Indirect
.ArrayID
)
731 param_base
= array_first
[reg
.Indirect
.ArrayID
];
733 param_base
= reg
.Register
.Index
;
735 param_index
= get_indirect_index(ctx
, ®
.Indirect
,
736 reg
.Register
.Index
- param_base
);
739 param_base
= reg
.Register
.Index
;
740 param_index
= lp_build_const_int32(gallivm
, 0);
743 param_index_base
= si_shader_io_get_unique_index(name
[param_base
],
746 param_index
= LLVMBuildAdd(gallivm
->builder
, param_index
,
747 lp_build_const_int32(gallivm
, param_index_base
),
750 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
753 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
754 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
755 * or v4i32 (num_channels=3,4). */
756 static void build_tbuffer_store(struct si_shader_context
*ctx
,
759 unsigned num_channels
,
761 LLVMValueRef soffset
,
762 unsigned inst_offset
,
771 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
772 LLVMValueRef args
[] = {
775 LLVMConstInt(ctx
->i32
, num_channels
, 0),
778 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
779 LLVMConstInt(ctx
->i32
, dfmt
, 0),
780 LLVMConstInt(ctx
->i32
, nfmt
, 0),
781 LLVMConstInt(ctx
->i32
, offen
, 0),
782 LLVMConstInt(ctx
->i32
, idxen
, 0),
783 LLVMConstInt(ctx
->i32
, glc
, 0),
784 LLVMConstInt(ctx
->i32
, slc
, 0),
785 LLVMConstInt(ctx
->i32
, tfe
, 0)
788 /* The instruction offset field has 12 bits */
789 assert(offen
|| inst_offset
< (1 << 12));
791 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
792 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
793 const char *types
[] = {"i32", "v2i32", "v4i32"};
795 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
797 lp_build_intrinsic(gallivm
->builder
, name
, ctx
->voidt
,
798 args
, ARRAY_SIZE(args
), 0);
801 static void build_tbuffer_store_dwords(struct si_shader_context
*ctx
,
804 unsigned num_channels
,
806 LLVMValueRef soffset
,
807 unsigned inst_offset
)
809 static unsigned dfmt
[] = {
810 V_008F0C_BUF_DATA_FORMAT_32
,
811 V_008F0C_BUF_DATA_FORMAT_32_32
,
812 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
813 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
815 assert(num_channels
>= 1 && num_channels
<= 4);
817 build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
818 inst_offset
, dfmt
[num_channels
-1],
819 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
822 static LLVMValueRef
build_buffer_load(struct si_shader_context
*ctx
,
826 LLVMValueRef voffset
,
827 LLVMValueRef soffset
,
828 unsigned inst_offset
,
832 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
833 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
835 if (HAVE_LLVM
>= 0x309) {
836 LLVMValueRef args
[] = {
837 LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v4i32
, ""),
838 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
839 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
840 LLVMConstInt(ctx
->i1
, glc
, 0),
841 LLVMConstInt(ctx
->i1
, slc
, 0)
844 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
846 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
850 args
[2] = LLVMBuildAdd(gallivm
->builder
, args
[2], voffset
,
855 args
[2] = LLVMBuildAdd(gallivm
->builder
, args
[2], soffset
,
859 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
862 return lp_build_intrinsic(gallivm
->builder
, name
, types
[func
], args
,
863 ARRAY_SIZE(args
), LP_FUNC_ATTR_READONLY
);
865 LLVMValueRef args
[] = {
866 LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v16i8
, ""),
867 voffset
? voffset
: vindex
,
869 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
870 LLVMConstInt(ctx
->i32
, voffset
? 1 : 0, 0), // offen
871 LLVMConstInt(ctx
->i32
, vindex
? 1 : 0, 0), //idxen
872 LLVMConstInt(ctx
->i32
, glc
, 0),
873 LLVMConstInt(ctx
->i32
, slc
, 0),
874 LLVMConstInt(ctx
->i32
, 0, 0), // TFE
877 LLVMTypeRef types
[] = {ctx
->i32
, LLVMVectorType(ctx
->i32
, 2),
879 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
880 const char *arg_type
= "i32";
883 if (voffset
&& vindex
) {
884 LLVMValueRef vaddr
[] = {vindex
, voffset
};
887 args
[1] = lp_build_gather_values(gallivm
, vaddr
, 2);
890 snprintf(name
, sizeof(name
), "llvm.SI.buffer.load.dword.%s.%s",
891 type_names
[func
], arg_type
);
893 return lp_build_intrinsic(gallivm
->builder
, name
, types
[func
], args
,
894 ARRAY_SIZE(args
), LP_FUNC_ATTR_READONLY
);
898 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
899 enum tgsi_opcode_type type
, unsigned swizzle
,
900 LLVMValueRef buffer
, LLVMValueRef offset
,
903 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
904 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
905 LLVMValueRef value
, value2
;
906 LLVMTypeRef llvm_type
= tgsi2llvmtype(bld_base
, type
);
907 LLVMTypeRef vec_type
= LLVMVectorType(llvm_type
, 4);
910 value
= build_buffer_load(ctx
, buffer
, 4, NULL
, base
, offset
,
913 return LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
916 if (!tgsi_type_is_64bit(type
)) {
917 value
= build_buffer_load(ctx
, buffer
, 4, NULL
, base
, offset
,
920 value
= LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
921 return LLVMBuildExtractElement(gallivm
->builder
, value
,
922 lp_build_const_int32(gallivm
, swizzle
), "");
925 value
= build_buffer_load(ctx
, buffer
, 1, NULL
, base
, offset
,
928 value2
= build_buffer_load(ctx
, buffer
, 1, NULL
, base
, offset
,
929 swizzle
* 4 + 4, 1, 0);
931 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
937 * \param type output value type
938 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
939 * \param dw_addr address in dwords
941 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
942 enum tgsi_opcode_type type
, unsigned swizzle
,
943 LLVMValueRef dw_addr
)
945 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
946 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
950 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
952 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
953 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
955 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
959 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
960 lp_build_const_int32(gallivm
, swizzle
));
962 value
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
963 if (tgsi_type_is_64bit(type
)) {
965 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
966 lp_build_const_int32(gallivm
, 1));
967 value2
= ac_build_indexed_load(&ctx
->ac
, ctx
->lds
, dw_addr
, false);
968 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
971 return LLVMBuildBitCast(gallivm
->builder
, value
,
972 tgsi2llvmtype(bld_base
, type
), "");
978 * \param swizzle offset (typically 0..3)
979 * \param dw_addr address in dwords
980 * \param value value to store
982 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
983 unsigned swizzle
, LLVMValueRef dw_addr
,
986 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
987 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
989 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
990 lp_build_const_int32(gallivm
, swizzle
));
992 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
993 ac_build_indexed_store(&ctx
->ac
, ctx
->lds
,
997 static LLVMValueRef
fetch_input_tcs(
998 struct lp_build_tgsi_context
*bld_base
,
999 const struct tgsi_full_src_register
*reg
,
1000 enum tgsi_opcode_type type
, unsigned swizzle
)
1002 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1003 LLVMValueRef dw_addr
, stride
;
1005 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
1006 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1007 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1009 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
1012 static LLVMValueRef
fetch_output_tcs(
1013 struct lp_build_tgsi_context
*bld_base
,
1014 const struct tgsi_full_src_register
*reg
,
1015 enum tgsi_opcode_type type
, unsigned swizzle
)
1017 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1018 LLVMValueRef dw_addr
, stride
;
1020 if (reg
->Register
.Dimension
) {
1021 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
1022 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1023 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1025 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1026 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
1029 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
1032 static LLVMValueRef
fetch_input_tes(
1033 struct lp_build_tgsi_context
*bld_base
,
1034 const struct tgsi_full_src_register
*reg
,
1035 enum tgsi_opcode_type type
, unsigned swizzle
)
1037 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1038 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1039 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
1041 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1042 SI_PARAM_RW_BUFFERS
);
1043 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
1044 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1046 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1047 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
1049 return buffer_load(bld_base
, type
, swizzle
, buffer
, base
, addr
);
1052 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1053 const struct tgsi_full_instruction
*inst
,
1054 const struct tgsi_opcode_info
*info
,
1055 LLVMValueRef dst
[4])
1057 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1058 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1059 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
1060 unsigned chan_index
;
1061 LLVMValueRef dw_addr
, stride
;
1062 LLVMValueRef rw_buffers
, buffer
, base
, buf_addr
;
1063 LLVMValueRef values
[4];
1065 /* Only handle per-patch and per-vertex outputs here.
1066 * Vectors will be lowered to scalars and this function will be called again.
1068 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1069 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1070 si_llvm_emit_store(bld_base
, inst
, info
, dst
);
1074 if (reg
->Register
.Dimension
) {
1075 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
1076 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1077 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1079 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1080 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1083 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1084 SI_PARAM_RW_BUFFERS
);
1085 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
1086 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1088 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1089 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1092 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
1093 LLVMValueRef value
= dst
[chan_index
];
1095 if (inst
->Instruction
.Saturate
)
1096 value
= si_llvm_saturate(bld_base
, value
);
1098 lds_store(bld_base
, chan_index
, dw_addr
, value
);
1100 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
1101 values
[chan_index
] = value
;
1103 if (inst
->Dst
[0].Register
.WriteMask
!= 0xF) {
1104 build_tbuffer_store_dwords(ctx
, buffer
, value
, 1,
1110 if (inst
->Dst
[0].Register
.WriteMask
== 0xF) {
1111 LLVMValueRef value
= lp_build_gather_values(bld_base
->base
.gallivm
,
1113 build_tbuffer_store_dwords(ctx
, buffer
, value
, 4, buf_addr
,
1118 static LLVMValueRef
fetch_input_gs(
1119 struct lp_build_tgsi_context
*bld_base
,
1120 const struct tgsi_full_src_register
*reg
,
1121 enum tgsi_opcode_type type
,
1124 struct lp_build_context
*base
= &bld_base
->base
;
1125 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1126 struct si_shader
*shader
= ctx
->shader
;
1127 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
1128 struct gallivm_state
*gallivm
= base
->gallivm
;
1129 LLVMValueRef vtx_offset
;
1130 LLVMValueRef args
[9];
1131 unsigned vtx_offset_param
;
1132 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1133 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1134 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
1138 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1139 return get_primitive_id(bld_base
, swizzle
);
1141 if (!reg
->Register
.Dimension
)
1144 if (swizzle
== ~0) {
1145 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1147 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1148 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
1150 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
1154 /* Get the vertex offset parameter */
1155 vtx_offset_param
= reg
->Dimension
.Index
;
1156 if (vtx_offset_param
< 2) {
1157 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
1159 assert(vtx_offset_param
< 6);
1160 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
1162 vtx_offset
= lp_build_mul_imm(uint
,
1163 LLVMGetParam(ctx
->main_fn
,
1167 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
1168 args
[0] = ctx
->esgs_ring
;
1169 args
[1] = vtx_offset
;
1170 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
1171 args
[3] = uint
->zero
;
1172 args
[4] = uint
->one
; /* OFFEN */
1173 args
[5] = uint
->zero
; /* IDXEN */
1174 args
[6] = uint
->one
; /* GLC */
1175 args
[7] = uint
->zero
; /* SLC */
1176 args
[8] = uint
->zero
; /* TFE */
1178 value
= lp_build_intrinsic(gallivm
->builder
,
1179 "llvm.SI.buffer.load.dword.i32.i32",
1181 LP_FUNC_ATTR_READONLY
);
1182 if (tgsi_type_is_64bit(type
)) {
1183 LLVMValueRef value2
;
1184 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
1185 value2
= lp_build_intrinsic(gallivm
->builder
,
1186 "llvm.SI.buffer.load.dword.i32.i32",
1188 LP_FUNC_ATTR_READONLY
);
1189 return si_llvm_emit_fetch_64bit(bld_base
, type
,
1192 return LLVMBuildBitCast(gallivm
->builder
,
1194 tgsi2llvmtype(bld_base
, type
), "");
1197 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1199 switch (interpolate
) {
1200 case TGSI_INTERPOLATE_CONSTANT
:
1203 case TGSI_INTERPOLATE_LINEAR
:
1204 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1205 return SI_PARAM_LINEAR_SAMPLE
;
1206 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1207 return SI_PARAM_LINEAR_CENTROID
;
1209 return SI_PARAM_LINEAR_CENTER
;
1211 case TGSI_INTERPOLATE_COLOR
:
1212 case TGSI_INTERPOLATE_PERSPECTIVE
:
1213 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1214 return SI_PARAM_PERSP_SAMPLE
;
1215 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1216 return SI_PARAM_PERSP_CENTROID
;
1218 return SI_PARAM_PERSP_CENTER
;
1221 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1227 * Interpolate a fragment shader input.
1229 * @param ctx context
1230 * @param input_index index of the input in hardware
1231 * @param semantic_name TGSI_SEMANTIC_*
1232 * @param semantic_index semantic index
1233 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1234 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1235 * @param interp_param interpolation weights (i,j)
1236 * @param prim_mask SI_PARAM_PRIM_MASK
1237 * @param face SI_PARAM_FRONT_FACE
1238 * @param result the return value (4 components)
1240 static void interp_fs_input(struct si_shader_context
*ctx
,
1241 unsigned input_index
,
1242 unsigned semantic_name
,
1243 unsigned semantic_index
,
1244 unsigned num_interp_inputs
,
1245 unsigned colors_read_mask
,
1246 LLVMValueRef interp_param
,
1247 LLVMValueRef prim_mask
,
1249 LLVMValueRef result
[4])
1251 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1252 struct lp_build_context
*base
= &bld_base
->base
;
1253 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1254 struct gallivm_state
*gallivm
= base
->gallivm
;
1255 LLVMValueRef attr_number
;
1260 /* fs.constant returns the param from the middle vertex, so it's not
1261 * really useful for flat shading. It's meant to be used for custom
1262 * interpolation (but the intrinsic can't fetch from the other two
1265 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1266 * to do the right thing. The only reason we use fs.constant is that
1267 * fs.interp cannot be used on integers, because they can be equal
1270 * When interp is false we will use fs.constant or for newer llvm,
1271 * amdgcn.interp.mov.
1273 bool interp
= interp_param
!= NULL
;
1275 attr_number
= lp_build_const_int32(gallivm
, input_index
);
1278 interp_param
= LLVMBuildBitCast(gallivm
->builder
, interp_param
,
1279 LLVMVectorType(ctx
->f32
, 2), "");
1281 i
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1283 j
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1287 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1288 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1289 LLVMValueRef is_face_positive
;
1290 LLVMValueRef back_attr_number
;
1292 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1293 * otherwise it's at offset "num_inputs".
1295 unsigned back_attr_offset
= num_interp_inputs
;
1296 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1297 back_attr_offset
+= 1;
1299 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
1301 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1302 face
, uint
->zero
, "");
1304 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1305 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1306 LLVMValueRef front
, back
;
1309 front
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1310 attr_number
, prim_mask
,
1312 back
= ac_build_fs_interp(&ctx
->ac
, llvm_chan
,
1313 back_attr_number
, prim_mask
,
1316 front
= ac_build_fs_interp_mov(&ctx
->ac
,
1317 lp_build_const_int32(gallivm
, 2), /* P0 */
1318 llvm_chan
, attr_number
, prim_mask
);
1319 back
= ac_build_fs_interp_mov(&ctx
->ac
,
1320 lp_build_const_int32(gallivm
, 2), /* P0 */
1321 llvm_chan
, back_attr_number
, prim_mask
);
1324 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
1330 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1332 result
[0] = ac_build_fs_interp(&ctx
->ac
, uint
->zero
,
1333 attr_number
, prim_mask
, i
, j
);
1335 result
[0] = ac_build_fs_interp_mov(&ctx
->ac
, uint
->zero
,
1336 lp_build_const_int32(gallivm
, 2), /* P0 */
1337 attr_number
, prim_mask
);
1340 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1341 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1343 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1344 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1347 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
1348 llvm_chan
, attr_number
, prim_mask
, i
, j
);
1350 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
1351 lp_build_const_int32(gallivm
, 2), /* P0 */
1352 llvm_chan
, attr_number
, prim_mask
);
1358 static void declare_input_fs(
1359 struct si_shader_context
*radeon_bld
,
1360 unsigned input_index
,
1361 const struct tgsi_full_declaration
*decl
,
1362 LLVMValueRef out
[4])
1364 struct lp_build_context
*base
= &radeon_bld
->bld_base
.base
;
1365 struct si_shader_context
*ctx
=
1366 si_shader_context(&radeon_bld
->bld_base
);
1367 struct si_shader
*shader
= ctx
->shader
;
1368 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
1369 LLVMValueRef interp_param
= NULL
;
1370 int interp_param_idx
;
1372 /* Get colors from input VGPRs (set by the prolog). */
1373 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1374 unsigned i
= decl
->Semantic
.Index
;
1375 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1376 unsigned mask
= colors_read
>> (i
* 4);
1377 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1378 (i
? util_bitcount(colors_read
& 0xf) : 0);
1380 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1381 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1382 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1383 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1387 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1388 decl
->Interp
.Location
);
1389 if (interp_param_idx
== -1)
1391 else if (interp_param_idx
) {
1392 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1395 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
1396 decl
->Interp
.Interpolate
== TGSI_INTERPOLATE_COLOR
&&
1397 ctx
->shader
->key
.part
.ps
.prolog
.flatshade_colors
)
1398 interp_param
= NULL
; /* load the constant color */
1400 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1401 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1402 shader
->selector
->info
.colors_read
, interp_param
,
1403 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1404 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1408 static LLVMValueRef
get_sample_id(struct si_shader_context
*radeon_bld
)
1410 return unpack_param(si_shader_context(&radeon_bld
->bld_base
),
1411 SI_PARAM_ANCILLARY
, 8, 4);
1415 * Set range metadata on an instruction. This can only be used on load and
1416 * call instructions. If you know an instruction can only produce the values
1417 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1418 * \p lo is the minimum value inclusive.
1419 * \p hi is the maximum value exclusive.
1421 static void set_range_metadata(struct si_shader_context
*ctx
,
1422 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1424 LLVMValueRef range_md
, md_args
[2];
1425 LLVMTypeRef type
= LLVMTypeOf(value
);
1426 LLVMContextRef context
= LLVMGetTypeContext(type
);
1428 md_args
[0] = LLVMConstInt(type
, lo
, false);
1429 md_args
[1] = LLVMConstInt(type
, hi
, false);
1430 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1431 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1434 static LLVMValueRef
get_thread_id(struct si_shader_context
*ctx
)
1436 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1439 if (HAVE_LLVM
< 0x0308) {
1440 tid
= lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid",
1441 ctx
->i32
, NULL
, 0, LP_FUNC_ATTR_READNONE
);
1443 LLVMValueRef tid_args
[2];
1444 tid_args
[0] = lp_build_const_int32(gallivm
, 0xffffffff);
1445 tid_args
[1] = lp_build_const_int32(gallivm
, 0);
1446 tid_args
[1] = lp_build_intrinsic(gallivm
->builder
,
1447 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1448 tid_args
, 2, LP_FUNC_ATTR_READNONE
);
1450 tid
= lp_build_intrinsic(gallivm
->builder
,
1451 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1452 tid_args
, 2, LP_FUNC_ATTR_READNONE
);
1454 set_range_metadata(ctx
, tid
, 0, 64);
1459 * Load a dword from a constant buffer.
1461 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1462 LLVMValueRef resource
,
1463 LLVMValueRef offset
)
1465 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
1466 LLVMValueRef args
[2] = {resource
, offset
};
1468 return lp_build_intrinsic(builder
, "llvm.SI.load.const", ctx
->f32
, args
, 2,
1469 LP_FUNC_ATTR_READNONE
);
1472 static LLVMValueRef
load_sample_position(struct si_shader_context
*radeon_bld
, LLVMValueRef sample_id
)
1474 struct si_shader_context
*ctx
=
1475 si_shader_context(&radeon_bld
->bld_base
);
1476 struct lp_build_context
*uint_bld
= &radeon_bld
->bld_base
.uint_bld
;
1477 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1478 LLVMBuilderRef builder
= gallivm
->builder
;
1479 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1480 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_PS_CONST_SAMPLE_POSITIONS
);
1481 LLVMValueRef resource
= ac_build_indexed_load_const(&ctx
->ac
, desc
, buf_index
);
1483 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1484 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1485 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1487 LLVMValueRef pos
[4] = {
1488 buffer_load_const(ctx
, resource
, offset0
),
1489 buffer_load_const(ctx
, resource
, offset1
),
1490 lp_build_const_float(gallivm
, 0),
1491 lp_build_const_float(gallivm
, 0)
1494 return lp_build_gather_values(gallivm
, pos
, 4);
1497 static void declare_system_value(
1498 struct si_shader_context
*radeon_bld
,
1500 const struct tgsi_full_declaration
*decl
)
1502 struct si_shader_context
*ctx
=
1503 si_shader_context(&radeon_bld
->bld_base
);
1504 struct lp_build_context
*bld
= &radeon_bld
->bld_base
.base
;
1505 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1506 LLVMValueRef value
= 0;
1508 switch (decl
->Semantic
.Name
) {
1509 case TGSI_SEMANTIC_INSTANCEID
:
1510 value
= LLVMGetParam(radeon_bld
->main_fn
,
1511 ctx
->param_instance_id
);
1514 case TGSI_SEMANTIC_VERTEXID
:
1515 value
= LLVMBuildAdd(gallivm
->builder
,
1516 LLVMGetParam(radeon_bld
->main_fn
,
1517 ctx
->param_vertex_id
),
1518 LLVMGetParam(radeon_bld
->main_fn
,
1519 SI_PARAM_BASE_VERTEX
), "");
1522 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1523 value
= LLVMGetParam(radeon_bld
->main_fn
,
1524 ctx
->param_vertex_id
);
1527 case TGSI_SEMANTIC_BASEVERTEX
:
1528 value
= LLVMGetParam(radeon_bld
->main_fn
,
1529 SI_PARAM_BASE_VERTEX
);
1532 case TGSI_SEMANTIC_BASEINSTANCE
:
1533 value
= LLVMGetParam(radeon_bld
->main_fn
,
1534 SI_PARAM_START_INSTANCE
);
1537 case TGSI_SEMANTIC_DRAWID
:
1538 value
= LLVMGetParam(radeon_bld
->main_fn
,
1542 case TGSI_SEMANTIC_INVOCATIONID
:
1543 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1544 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1545 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
1546 value
= LLVMGetParam(radeon_bld
->main_fn
,
1547 SI_PARAM_GS_INSTANCE_ID
);
1549 assert(!"INVOCATIONID not implemented");
1552 case TGSI_SEMANTIC_POSITION
:
1554 LLVMValueRef pos
[4] = {
1555 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1556 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1557 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1558 lp_build_emit_llvm_unary(&radeon_bld
->bld_base
, TGSI_OPCODE_RCP
,
1559 LLVMGetParam(radeon_bld
->main_fn
,
1560 SI_PARAM_POS_W_FLOAT
)),
1562 value
= lp_build_gather_values(gallivm
, pos
, 4);
1566 case TGSI_SEMANTIC_FACE
:
1567 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1570 case TGSI_SEMANTIC_SAMPLEID
:
1571 value
= get_sample_id(radeon_bld
);
1574 case TGSI_SEMANTIC_SAMPLEPOS
: {
1575 LLVMValueRef pos
[4] = {
1576 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1577 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1578 lp_build_const_float(gallivm
, 0),
1579 lp_build_const_float(gallivm
, 0)
1581 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->bld_base
,
1582 TGSI_OPCODE_FRC
, pos
[0]);
1583 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->bld_base
,
1584 TGSI_OPCODE_FRC
, pos
[1]);
1585 value
= lp_build_gather_values(gallivm
, pos
, 4);
1589 case TGSI_SEMANTIC_SAMPLEMASK
:
1590 /* This can only occur with the OpenGL Core profile, which
1591 * doesn't support smoothing.
1593 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1596 case TGSI_SEMANTIC_TESSCOORD
:
1598 LLVMValueRef coord
[4] = {
1599 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_u
),
1600 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_v
),
1605 /* For triangles, the vector should be (u, v, 1-u-v). */
1606 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1607 PIPE_PRIM_TRIANGLES
)
1608 coord
[2] = lp_build_sub(bld
, bld
->one
,
1609 lp_build_add(bld
, coord
[0], coord
[1]));
1611 value
= lp_build_gather_values(gallivm
, coord
, 4);
1615 case TGSI_SEMANTIC_VERTICESIN
:
1616 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1617 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1618 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
1619 value
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 7);
1621 assert(!"invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
1624 case TGSI_SEMANTIC_TESSINNER
:
1625 case TGSI_SEMANTIC_TESSOUTER
:
1627 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
1628 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1630 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1631 SI_PARAM_RW_BUFFERS
);
1632 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
1633 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1635 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1636 addr
= get_tcs_tes_buffer_address(ctx
, NULL
,
1637 lp_build_const_int32(gallivm
, param
));
1639 value
= buffer_load(&radeon_bld
->bld_base
, TGSI_TYPE_FLOAT
,
1640 ~0, buffer
, base
, addr
);
1645 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
1646 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
1648 LLVMValueRef buf
, slot
, val
[4];
1651 slot
= lp_build_const_int32(gallivm
, SI_HS_CONST_DEFAULT_TESS_LEVELS
);
1652 buf
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1653 buf
= ac_build_indexed_load_const(&ctx
->ac
, buf
, slot
);
1654 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
1656 for (i
= 0; i
< 4; i
++)
1657 val
[i
] = buffer_load_const(ctx
, buf
,
1658 lp_build_const_int32(gallivm
, (offset
+ i
) * 4));
1659 value
= lp_build_gather_values(gallivm
, val
, 4);
1663 case TGSI_SEMANTIC_PRIMID
:
1664 value
= get_primitive_id(&radeon_bld
->bld_base
, 0);
1667 case TGSI_SEMANTIC_GRID_SIZE
:
1668 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_GRID_SIZE
);
1671 case TGSI_SEMANTIC_BLOCK_SIZE
:
1673 LLVMValueRef values
[3];
1675 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1677 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1678 unsigned sizes
[3] = {
1679 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1680 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1681 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1684 for (i
= 0; i
< 3; ++i
)
1685 values
[i
] = lp_build_const_int32(gallivm
, sizes
[i
]);
1687 value
= lp_build_gather_values(gallivm
, values
, 3);
1689 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_BLOCK_SIZE
);
1694 case TGSI_SEMANTIC_BLOCK_ID
:
1695 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_BLOCK_ID
);
1698 case TGSI_SEMANTIC_THREAD_ID
:
1699 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_THREAD_ID
);
1702 case TGSI_SEMANTIC_HELPER_INVOCATION
:
1703 if (HAVE_LLVM
>= 0x0309) {
1704 value
= lp_build_intrinsic(gallivm
->builder
,
1705 "llvm.amdgcn.ps.live",
1707 LP_FUNC_ATTR_READNONE
);
1708 value
= LLVMBuildNot(gallivm
->builder
, value
, "");
1709 value
= LLVMBuildSExt(gallivm
->builder
, value
, ctx
->i32
, "");
1711 assert(!"TGSI_SEMANTIC_HELPER_INVOCATION unsupported");
1717 assert(!"unknown system value");
1721 radeon_bld
->system_values
[index
] = value
;
1724 static void declare_compute_memory(struct si_shader_context
*radeon_bld
,
1725 const struct tgsi_full_declaration
*decl
)
1727 struct si_shader_context
*ctx
=
1728 si_shader_context(&radeon_bld
->bld_base
);
1729 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
1730 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1732 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, LOCAL_ADDR_SPACE
);
1735 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
1736 assert(decl
->Range
.First
== decl
->Range
.Last
);
1737 assert(!ctx
->shared_memory
);
1739 var
= LLVMAddGlobalInAddressSpace(gallivm
->module
,
1740 LLVMArrayType(ctx
->i8
, sel
->local_size
),
1743 LLVMSetAlignment(var
, 4);
1745 ctx
->shared_memory
= LLVMBuildBitCast(gallivm
->builder
, var
, i8p
, "");
1748 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
1750 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
1751 SI_PARAM_CONST_BUFFERS
);
1753 return ac_build_indexed_load_const(&ctx
->ac
, list_ptr
,
1754 LLVMConstInt(ctx
->i32
, i
, 0));
1757 static LLVMValueRef
fetch_constant(
1758 struct lp_build_tgsi_context
*bld_base
,
1759 const struct tgsi_full_src_register
*reg
,
1760 enum tgsi_opcode_type type
,
1763 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1764 struct lp_build_context
*base
= &bld_base
->base
;
1765 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1768 LLVMValueRef addr
, bufp
;
1769 LLVMValueRef result
;
1771 if (swizzle
== LP_CHAN_ALL
) {
1773 LLVMValueRef values
[4];
1774 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1775 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1777 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1780 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1781 idx
= reg
->Register
.Index
* 4 + swizzle
;
1783 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1784 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_CONST_BUFFERS
);
1786 index
= get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
1787 reg
->Dimension
.Index
,
1788 SI_NUM_CONST_BUFFERS
);
1789 bufp
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, index
);
1791 bufp
= load_const_buffer_desc(ctx
, buf
);
1793 if (reg
->Register
.Indirect
) {
1794 addr
= ctx
->addrs
[ireg
->Index
][ireg
->Swizzle
];
1795 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1796 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1797 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1798 lp_build_const_int32(base
->gallivm
, idx
* 4));
1800 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
1803 result
= buffer_load_const(ctx
, bufp
, addr
);
1805 if (!tgsi_type_is_64bit(type
))
1806 result
= bitcast(bld_base
, type
, result
);
1808 LLVMValueRef addr2
, result2
;
1810 addr2
= lp_build_add(&bld_base
->uint_bld
, addr
,
1811 LLVMConstInt(ctx
->i32
, 4, 0));
1812 result2
= buffer_load_const(ctx
, bufp
, addr2
);
1814 result
= si_llvm_emit_fetch_64bit(bld_base
, type
,
1820 /* Upper 16 bits must be zero. */
1821 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1822 LLVMValueRef val
[2])
1824 return LLVMBuildOr(gallivm
->builder
, val
[0],
1825 LLVMBuildShl(gallivm
->builder
, val
[1],
1826 lp_build_const_int32(gallivm
, 16),
1830 /* Upper 16 bits are ignored and will be dropped. */
1831 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1832 LLVMValueRef val
[2])
1834 LLVMValueRef v
[2] = {
1835 LLVMBuildAnd(gallivm
->builder
, val
[0],
1836 lp_build_const_int32(gallivm
, 0xffff), ""),
1839 return si_llvm_pack_two_int16(gallivm
, v
);
1842 /* Initialize arguments for the shader export intrinsic */
1843 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1844 LLVMValueRef
*values
,
1848 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1849 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
1850 struct lp_build_context
*base
= &bld_base
->base
;
1851 struct gallivm_state
*gallivm
= base
->gallivm
;
1852 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1853 LLVMValueRef val
[4];
1854 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1858 /* Default is 0xf. Adjusted below depending on the format. */
1859 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1861 /* Specify whether the EXEC mask represents the valid mask */
1862 args
[1] = uint
->zero
;
1864 /* Specify whether this is the last export */
1865 args
[2] = uint
->zero
;
1867 /* Specify the target we are exporting */
1868 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1870 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
1871 const struct si_shader_key
*key
= &ctx
->shader
->key
;
1872 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
1873 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1875 assert(cbuf
>= 0 && cbuf
< 8);
1876 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1877 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1880 args
[4] = uint
->zero
; /* COMPR flag */
1881 args
[5] = base
->undef
;
1882 args
[6] = base
->undef
;
1883 args
[7] = base
->undef
;
1884 args
[8] = base
->undef
;
1886 switch (spi_shader_col_format
) {
1887 case V_028714_SPI_SHADER_ZERO
:
1888 args
[0] = uint
->zero
; /* writemask */
1889 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1892 case V_028714_SPI_SHADER_32_R
:
1893 args
[0] = uint
->one
; /* writemask */
1894 args
[5] = values
[0];
1897 case V_028714_SPI_SHADER_32_GR
:
1898 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1899 args
[5] = values
[0];
1900 args
[6] = values
[1];
1903 case V_028714_SPI_SHADER_32_AR
:
1904 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1905 args
[5] = values
[0];
1906 args
[8] = values
[3];
1909 case V_028714_SPI_SHADER_FP16_ABGR
:
1910 args
[4] = uint
->one
; /* COMPR flag */
1912 for (chan
= 0; chan
< 2; chan
++) {
1913 LLVMValueRef pack_args
[2] = {
1915 values
[2 * chan
+ 1]
1917 LLVMValueRef packed
;
1919 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1921 ctx
->i32
, pack_args
, 2,
1922 LP_FUNC_ATTR_READNONE
);
1924 LLVMBuildBitCast(base
->gallivm
->builder
,
1925 packed
, ctx
->f32
, "");
1929 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1930 for (chan
= 0; chan
< 4; chan
++) {
1931 val
[chan
] = si_llvm_saturate(bld_base
, values
[chan
]);
1932 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1933 lp_build_const_float(gallivm
, 65535), "");
1934 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1935 lp_build_const_float(gallivm
, 0.5), "");
1936 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1940 args
[4] = uint
->one
; /* COMPR flag */
1941 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1942 si_llvm_pack_two_int16(gallivm
, val
));
1943 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1944 si_llvm_pack_two_int16(gallivm
, val
+2));
1947 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1948 for (chan
= 0; chan
< 4; chan
++) {
1949 /* Clamp between [-1, 1]. */
1950 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1952 lp_build_const_float(gallivm
, 1));
1953 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1955 lp_build_const_float(gallivm
, -1));
1956 /* Convert to a signed integer in [-32767, 32767]. */
1957 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1958 lp_build_const_float(gallivm
, 32767), "");
1959 /* If positive, add 0.5, else add -0.5. */
1960 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1961 LLVMBuildSelect(builder
,
1962 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1963 val
[chan
], base
->zero
, ""),
1964 lp_build_const_float(gallivm
, 0.5),
1965 lp_build_const_float(gallivm
, -0.5), ""), "");
1966 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1969 args
[4] = uint
->one
; /* COMPR flag */
1970 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1971 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1972 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1973 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1976 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1977 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1980 for (chan
= 0; chan
< 4; chan
++) {
1981 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1982 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1986 args
[4] = uint
->one
; /* COMPR flag */
1987 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1988 si_llvm_pack_two_int16(gallivm
, val
));
1989 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1990 si_llvm_pack_two_int16(gallivm
, val
+2));
1994 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1995 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1997 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
2000 for (chan
= 0; chan
< 4; chan
++) {
2001 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
2002 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2005 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2010 args
[4] = uint
->one
; /* COMPR flag */
2011 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2012 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
2013 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2014 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
2018 case V_028714_SPI_SHADER_32_ABGR
:
2019 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
2024 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2027 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2028 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2030 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2031 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2032 SI_PARAM_ALPHA_REF
);
2034 LLVMValueRef alpha_pass
=
2035 lp_build_cmp(&bld_base
->base
,
2036 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
,
2039 lp_build_select(&bld_base
->base
,
2041 lp_build_const_float(gallivm
, 1.0f
),
2042 lp_build_const_float(gallivm
, -1.0f
));
2044 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
2045 ctx
->voidt
, &arg
, 1, 0);
2047 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kilp",
2048 ctx
->voidt
, NULL
, 0, 0);
2052 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2054 unsigned samplemask_param
)
2056 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2057 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2058 LLVMValueRef coverage
;
2060 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2061 coverage
= LLVMGetParam(ctx
->main_fn
,
2063 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
2065 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
2067 &coverage
, 1, LP_FUNC_ATTR_READNONE
);
2069 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
2072 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
2073 lp_build_const_float(gallivm
,
2074 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2076 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
2079 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
2080 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
2082 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2083 struct lp_build_context
*base
= &bld_base
->base
;
2084 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
2087 unsigned const_chan
;
2088 LLVMValueRef base_elt
;
2089 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
2090 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
,
2091 SI_VS_CONST_CLIP_PLANES
);
2092 LLVMValueRef const_resource
= ac_build_indexed_load_const(&ctx
->ac
, ptr
, constbuf_index
);
2094 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2095 LLVMValueRef
*args
= pos
[2 + reg_index
];
2100 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
2102 /* Compute dot products of position and user clip plane vectors */
2103 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2104 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2105 args
[1] = lp_build_const_int32(base
->gallivm
,
2106 ((reg_index
* 4 + chan
) * 4 +
2108 base_elt
= buffer_load_const(ctx
, const_resource
,
2111 lp_build_add(base
, args
[5 + chan
],
2112 lp_build_mul(base
, base_elt
,
2113 out_elts
[const_chan
]));
2117 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
2118 args
[1] = uint
->zero
;
2119 args
[2] = uint
->zero
;
2120 args
[3] = lp_build_const_int32(base
->gallivm
,
2121 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
2122 args
[4] = uint
->zero
;
2126 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2130 if (so
->num_outputs
)
2131 fprintf(stderr
, "STREAMOUT\n");
2133 for (i
= 0; i
< so
->num_outputs
; i
++) {
2134 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2135 so
->output
[i
].start_component
;
2136 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2137 i
, so
->output
[i
].output_buffer
,
2138 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2139 so
->output
[i
].register_index
,
2140 mask
& 1 ? "x" : "",
2141 mask
& 2 ? "y" : "",
2142 mask
& 4 ? "z" : "",
2143 mask
& 8 ? "w" : "");
2147 static void emit_streamout_output(struct si_shader_context
*ctx
,
2148 LLVMValueRef
const *so_buffers
,
2149 LLVMValueRef
const *so_write_offsets
,
2150 struct pipe_stream_output
*stream_out
,
2151 struct si_shader_output_values
*shader_out
)
2153 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2154 LLVMBuilderRef builder
= gallivm
->builder
;
2155 unsigned buf_idx
= stream_out
->output_buffer
;
2156 unsigned start
= stream_out
->start_component
;
2157 unsigned num_comps
= stream_out
->num_components
;
2158 LLVMValueRef out
[4];
2160 assert(num_comps
&& num_comps
<= 4);
2161 if (!num_comps
|| num_comps
> 4)
2164 /* Load the output as int. */
2165 for (int j
= 0; j
< num_comps
; j
++) {
2166 assert(stream_out
->stream
== shader_out
->vertex_stream
[start
+ j
]);
2168 out
[j
] = LLVMBuildBitCast(builder
,
2169 shader_out
->values
[start
+ j
],
2173 /* Pack the output. */
2174 LLVMValueRef vdata
= NULL
;
2176 switch (num_comps
) {
2177 case 1: /* as i32 */
2180 case 2: /* as v2i32 */
2181 case 3: /* as v4i32 (aligned to 4) */
2182 case 4: /* as v4i32 */
2183 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
2184 for (int j
= 0; j
< num_comps
; j
++) {
2185 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
2186 LLVMConstInt(ctx
->i32
, j
, 0), "");
2191 build_tbuffer_store_dwords(ctx
, so_buffers
[buf_idx
],
2193 so_write_offsets
[buf_idx
],
2194 LLVMConstInt(ctx
->i32
, 0, 0),
2195 stream_out
->dst_offset
* 4);
2199 * Write streamout data to buffers for vertex stream @p stream (different
2200 * vertex streams can occur for GS copy shaders).
2202 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2203 struct si_shader_output_values
*outputs
,
2204 unsigned noutput
, unsigned stream
)
2206 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
2207 struct pipe_stream_output_info
*so
= &sel
->so
;
2208 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2209 LLVMBuilderRef builder
= gallivm
->builder
;
2211 struct lp_build_if_state if_ctx
;
2213 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2214 LLVMValueRef so_vtx_count
=
2215 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2217 LLVMValueRef tid
= get_thread_id(ctx
);
2219 /* can_emit = tid < so_vtx_count; */
2220 LLVMValueRef can_emit
=
2221 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2223 /* Emit the streamout code conditionally. This actually avoids
2224 * out-of-bounds buffer access. The hw tells us via the SGPR
2225 * (so_vtx_count) which threads are allowed to emit streamout data. */
2226 lp_build_if(&if_ctx
, gallivm
, can_emit
);
2228 /* The buffer offset is computed as follows:
2229 * ByteOffset = streamout_offset[buffer_id]*4 +
2230 * (streamout_write_index + thread_id)*stride[buffer_id] +
2234 LLVMValueRef so_write_index
=
2235 LLVMGetParam(ctx
->main_fn
,
2236 ctx
->param_streamout_write_index
);
2238 /* Compute (streamout_write_index + thread_id). */
2239 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2241 /* Load the descriptor and compute the write offset for each
2242 * enabled buffer. */
2243 LLVMValueRef so_write_offset
[4] = {};
2244 LLVMValueRef so_buffers
[4];
2245 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2246 SI_PARAM_RW_BUFFERS
);
2248 for (i
= 0; i
< 4; i
++) {
2252 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
2253 SI_VS_STREAMOUT_BUF0
+ i
);
2255 so_buffers
[i
] = ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
2257 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2258 ctx
->param_streamout_offset
[i
]);
2259 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2261 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
2262 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
2263 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
2266 /* Write streamout data. */
2267 for (i
= 0; i
< so
->num_outputs
; i
++) {
2268 unsigned reg
= so
->output
[i
].register_index
;
2273 if (stream
!= so
->output
[i
].stream
)
2276 emit_streamout_output(ctx
, so_buffers
, so_write_offset
,
2277 &so
->output
[i
], &outputs
[reg
]);
2280 lp_build_endif(&if_ctx
);
2284 /* Generate export instructions for hardware VS shader stage */
2285 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
2286 struct si_shader_output_values
*outputs
,
2289 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2290 struct si_shader
*shader
= ctx
->shader
;
2291 struct lp_build_context
*base
= &bld_base
->base
;
2292 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
2293 LLVMValueRef args
[9];
2294 LLVMValueRef pos_args
[4][9] = { { 0 } };
2295 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2296 unsigned semantic_name
, semantic_index
;
2298 unsigned param_count
= 0;
2302 for (i
= 0; i
< noutput
; i
++) {
2303 semantic_name
= outputs
[i
].semantic_name
;
2304 semantic_index
= outputs
[i
].semantic_index
;
2305 bool export_param
= true;
2307 switch (semantic_name
) {
2308 case TGSI_SEMANTIC_POSITION
: /* ignore these */
2309 case TGSI_SEMANTIC_PSIZE
:
2310 case TGSI_SEMANTIC_CLIPVERTEX
:
2311 case TGSI_SEMANTIC_EDGEFLAG
:
2313 case TGSI_SEMANTIC_GENERIC
:
2314 case TGSI_SEMANTIC_CLIPDIST
:
2315 if (shader
->key
.opt
.hw_vs
.kill_outputs
&
2316 (1ull << si_shader_io_get_unique_index(semantic_name
, semantic_index
)))
2317 export_param
= false;
2320 if (shader
->key
.opt
.hw_vs
.kill_outputs2
&
2321 (1u << si_shader_io_get_unique_index2(semantic_name
, semantic_index
)))
2322 export_param
= false;
2326 if (outputs
[i
].vertex_stream
[0] != 0 &&
2327 outputs
[i
].vertex_stream
[1] != 0 &&
2328 outputs
[i
].vertex_stream
[2] != 0 &&
2329 outputs
[i
].vertex_stream
[3] != 0)
2330 export_param
= false;
2333 /* Select the correct target */
2334 switch(semantic_name
) {
2335 case TGSI_SEMANTIC_PSIZE
:
2336 psize_value
= outputs
[i
].values
[0];
2338 case TGSI_SEMANTIC_EDGEFLAG
:
2339 edgeflag_value
= outputs
[i
].values
[0];
2341 case TGSI_SEMANTIC_LAYER
:
2342 layer_value
= outputs
[i
].values
[0];
2343 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2344 goto handle_semantic
;
2345 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2346 viewport_index_value
= outputs
[i
].values
[0];
2347 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2348 goto handle_semantic
;
2349 case TGSI_SEMANTIC_POSITION
:
2350 target
= V_008DFC_SQ_EXP_POS
;
2352 case TGSI_SEMANTIC_CLIPDIST
:
2353 if (shader
->key
.opt
.hw_vs
.clip_disable
) {
2354 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2355 goto handle_semantic
;
2357 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
2359 case TGSI_SEMANTIC_CLIPVERTEX
:
2360 if (shader
->key
.opt
.hw_vs
.clip_disable
)
2362 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
2364 case TGSI_SEMANTIC_COLOR
:
2365 case TGSI_SEMANTIC_BCOLOR
:
2366 case TGSI_SEMANTIC_PRIMID
:
2367 case TGSI_SEMANTIC_FOG
:
2368 case TGSI_SEMANTIC_TEXCOORD
:
2369 case TGSI_SEMANTIC_GENERIC
:
2372 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
2373 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2374 shader
->info
.vs_output_param_offset
[i
] = param_count
;
2380 "Warning: SI unhandled vs output type:%d\n",
2384 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
2386 if (target
>= V_008DFC_SQ_EXP_POS
&&
2387 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
2388 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
2389 args
, sizeof(args
));
2391 lp_build_intrinsic(base
->gallivm
->builder
,
2392 "llvm.SI.export", ctx
->voidt
,
2396 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
2397 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2398 goto handle_semantic
;
2402 shader
->info
.nr_param_exports
= param_count
;
2404 /* We need to add the position output manually if it's missing. */
2405 if (!pos_args
[0][0]) {
2406 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
2407 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
2408 pos_args
[0][2] = uint
->zero
; /* last export? */
2409 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
2410 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
2411 pos_args
[0][5] = base
->zero
; /* X */
2412 pos_args
[0][6] = base
->zero
; /* Y */
2413 pos_args
[0][7] = base
->zero
; /* Z */
2414 pos_args
[0][8] = base
->one
; /* W */
2417 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2418 if (shader
->selector
->info
.writes_psize
||
2419 shader
->selector
->info
.writes_edgeflag
||
2420 shader
->selector
->info
.writes_viewport_index
||
2421 shader
->selector
->info
.writes_layer
) {
2422 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
2423 shader
->selector
->info
.writes_psize
|
2424 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2425 (shader
->selector
->info
.writes_layer
<< 2) |
2426 (shader
->selector
->info
.writes_viewport_index
<< 3));
2427 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
2428 pos_args
[1][2] = uint
->zero
; /* last export? */
2429 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
2430 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
2431 pos_args
[1][5] = base
->zero
; /* X */
2432 pos_args
[1][6] = base
->zero
; /* Y */
2433 pos_args
[1][7] = base
->zero
; /* Z */
2434 pos_args
[1][8] = base
->zero
; /* W */
2436 if (shader
->selector
->info
.writes_psize
)
2437 pos_args
[1][5] = psize_value
;
2439 if (shader
->selector
->info
.writes_edgeflag
) {
2440 /* The output is a float, but the hw expects an integer
2441 * with the first bit containing the edge flag. */
2442 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
2445 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
2447 bld_base
->int_bld
.one
);
2449 /* The LLVM intrinsic expects a float. */
2450 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
2455 if (shader
->selector
->info
.writes_layer
)
2456 pos_args
[1][7] = layer_value
;
2458 if (shader
->selector
->info
.writes_viewport_index
)
2459 pos_args
[1][8] = viewport_index_value
;
2462 for (i
= 0; i
< 4; i
++)
2464 shader
->info
.nr_pos_exports
++;
2467 for (i
= 0; i
< 4; i
++) {
2468 if (!pos_args
[i
][0])
2471 /* Specify the target we are exporting */
2472 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
2474 if (pos_idx
== shader
->info
.nr_pos_exports
)
2475 /* Specify that this is the last export */
2476 pos_args
[i
][2] = uint
->one
;
2478 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2479 ctx
->voidt
, pos_args
[i
], 9, 0);
2484 * Forward all outputs from the vertex shader to the TES. This is only used
2485 * for the fixed function TCS.
2487 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
2489 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2490 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2491 LLVMValueRef invocation_id
, rw_buffers
, buffer
, buffer_offset
;
2492 LLVMValueRef lds_vertex_stride
, lds_vertex_offset
, lds_base
;
2495 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2497 rw_buffers
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
2498 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
2499 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
2501 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
2503 lds_vertex_stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
2504 lds_vertex_offset
= LLVMBuildMul(gallivm
->builder
, invocation_id
,
2505 lds_vertex_stride
, "");
2506 lds_base
= get_tcs_in_current_patch_offset(ctx
);
2507 lds_base
= LLVMBuildAdd(gallivm
->builder
, lds_base
, lds_vertex_offset
, "");
2509 inputs
= ctx
->shader
->key
.mono
.tcs
.inputs_to_copy
;
2511 unsigned i
= u_bit_scan64(&inputs
);
2513 LLVMValueRef lds_ptr
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2514 lp_build_const_int32(gallivm
, 4 * i
),
2517 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
2519 lp_build_const_int32(gallivm
, i
));
2521 LLVMValueRef value
= lds_load(bld_base
, TGSI_TYPE_SIGNED
, ~0,
2524 build_tbuffer_store_dwords(ctx
, buffer
, value
, 4, buffer_addr
,
2529 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2530 LLVMValueRef rel_patch_id
,
2531 LLVMValueRef invocation_id
,
2532 LLVMValueRef tcs_out_current_patch_data_offset
)
2534 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2535 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2536 struct si_shader
*shader
= ctx
->shader
;
2537 unsigned tess_inner_index
, tess_outer_index
;
2538 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2539 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
;
2540 unsigned stride
, outer_comps
, inner_comps
, i
;
2541 struct lp_build_if_state if_ctx
, inner_if_ctx
;
2543 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
2545 /* Do this only for invocation 0, because the tess levels are per-patch,
2548 * This can't jump, because invocation 0 executes this. It should
2549 * at least mask out the loads and stores for other invocations.
2551 lp_build_if(&if_ctx
, gallivm
,
2552 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2553 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2555 /* Determine the layout of one tess factor element in the buffer. */
2556 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
2557 case PIPE_PRIM_LINES
:
2558 stride
= 2; /* 2 dwords, 1 vec2 store */
2562 case PIPE_PRIM_TRIANGLES
:
2563 stride
= 4; /* 4 dwords, 1 vec4 store */
2567 case PIPE_PRIM_QUADS
:
2568 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2577 /* Load tess_inner and tess_outer from LDS.
2578 * Any invocation can write them, so we can't get them from a temporary.
2580 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2581 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2583 lds_base
= tcs_out_current_patch_data_offset
;
2584 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2585 lp_build_const_int32(gallivm
,
2586 tess_inner_index
* 4), "");
2587 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2588 lp_build_const_int32(gallivm
,
2589 tess_outer_index
* 4), "");
2591 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
2592 /* For isolines, the hardware expects tess factors in the
2593 * reverse order from what GLSL / TGSI specify.
2595 out
[0] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 1, lds_outer
);
2596 out
[1] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 0, lds_outer
);
2598 for (i
= 0; i
< outer_comps
; i
++)
2599 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2600 for (i
= 0; i
< inner_comps
; i
++)
2601 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2604 /* Convert the outputs to vectors for stores. */
2605 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2609 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2611 /* Get the buffer. */
2612 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2613 SI_PARAM_RW_BUFFERS
);
2614 buffer
= ac_build_indexed_load_const(&ctx
->ac
, rw_buffers
,
2615 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_FACTOR
));
2617 /* Get the offset. */
2618 tf_base
= LLVMGetParam(ctx
->main_fn
,
2619 SI_PARAM_TESS_FACTOR_OFFSET
);
2620 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2621 lp_build_const_int32(gallivm
, 4 * stride
), "");
2623 lp_build_if(&inner_if_ctx
, gallivm
,
2624 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2625 rel_patch_id
, bld_base
->uint_bld
.zero
, ""));
2627 /* Store the dynamic HS control word. */
2628 build_tbuffer_store_dwords(ctx
, buffer
,
2629 lp_build_const_int32(gallivm
, 0x80000000),
2630 1, lp_build_const_int32(gallivm
, 0), tf_base
, 0);
2632 lp_build_endif(&inner_if_ctx
);
2634 /* Store the tessellation factors. */
2635 build_tbuffer_store_dwords(ctx
, buffer
, vec0
,
2636 MIN2(stride
, 4), byteoffset
, tf_base
, 4);
2638 build_tbuffer_store_dwords(ctx
, buffer
, vec1
,
2639 stride
- 4, byteoffset
, tf_base
, 20);
2640 lp_build_endif(&if_ctx
);
2643 /* This only writes the tessellation factor levels. */
2644 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2646 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2647 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2649 si_copy_tcs_inputs(bld_base
);
2651 rel_patch_id
= get_rel_patch_id(ctx
);
2652 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2653 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2655 /* Return epilog parameters from this function. */
2656 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2657 LLVMValueRef ret
= ctx
->return_value
;
2658 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2661 /* RW_BUFFERS pointer */
2662 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2663 SI_PARAM_RW_BUFFERS
);
2664 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2665 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2666 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2667 bld_base
->uint_bld
.zero
, "");
2668 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2669 bld_base
->uint_bld
.one
, "");
2670 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2671 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2673 /* Tess factor buffer soffset is after user SGPRs. */
2674 tf_soffset
= LLVMGetParam(ctx
->main_fn
,
2675 SI_PARAM_TESS_FACTOR_OFFSET
);
2676 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2677 SI_TCS_NUM_USER_SGPR
+ 1, "");
2680 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2681 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2682 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2684 vgpr
= SI_TCS_NUM_USER_SGPR
+ 2;
2685 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2686 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2687 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2688 ctx
->return_value
= ret
;
2691 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2693 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2694 struct si_shader
*shader
= ctx
->shader
;
2695 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2696 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2698 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
2699 ctx
->param_rel_auto_id
);
2700 LLVMValueRef vertex_dw_stride
=
2701 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2702 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2703 vertex_dw_stride
, "");
2705 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2706 * its inputs from it. */
2707 for (i
= 0; i
< info
->num_outputs
; i
++) {
2708 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2709 unsigned name
= info
->output_semantic_name
[i
];
2710 unsigned index
= info
->output_semantic_index
[i
];
2711 int param
= si_shader_io_get_unique_index(name
, index
);
2712 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2713 lp_build_const_int32(gallivm
, param
* 4), "");
2715 for (chan
= 0; chan
< 4; chan
++) {
2716 lds_store(bld_base
, chan
, dw_addr
,
2717 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2722 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2724 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2725 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2726 struct si_shader
*es
= ctx
->shader
;
2727 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2728 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
2729 ctx
->param_es2gs_offset
);
2733 for (i
= 0; i
< info
->num_outputs
; i
++) {
2734 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
2737 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2738 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2741 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2742 info
->output_semantic_index
[i
]);
2744 for (chan
= 0; chan
< 4; chan
++) {
2745 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2746 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2748 build_tbuffer_store(ctx
,
2751 LLVMGetUndef(ctx
->i32
), soffset
,
2752 (4 * param_index
+ chan
) * 4,
2753 V_008F0C_BUF_DATA_FORMAT_32
,
2754 V_008F0C_BUF_NUM_FORMAT_UINT
,
2760 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2762 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2763 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2764 LLVMValueRef args
[2];
2766 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2767 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
2768 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2769 ctx
->voidt
, args
, 2, 0);
2772 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2774 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2775 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2776 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2777 struct si_shader_output_values
*outputs
= NULL
;
2780 assert(!ctx
->shader
->is_gs_copy_shader
);
2782 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2784 /* Vertex color clamping.
2786 * This uses a state constant loaded in a user data SGPR and
2787 * an IF statement is added that clamps all colors if the constant
2790 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
2791 struct lp_build_if_state if_ctx
;
2792 LLVMValueRef cond
= NULL
;
2793 LLVMValueRef addr
, val
;
2795 for (i
= 0; i
< info
->num_outputs
; i
++) {
2796 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2797 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2800 /* We've found a color. */
2802 /* The state is in the first bit of the user SGPR. */
2803 cond
= LLVMGetParam(ctx
->main_fn
,
2804 SI_PARAM_VS_STATE_BITS
);
2805 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2807 lp_build_if(&if_ctx
, gallivm
, cond
);
2810 for (j
= 0; j
< 4; j
++) {
2811 addr
= ctx
->outputs
[i
][j
];
2812 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2813 val
= si_llvm_saturate(bld_base
, val
);
2814 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2819 lp_build_endif(&if_ctx
);
2822 for (i
= 0; i
< info
->num_outputs
; i
++) {
2823 outputs
[i
].semantic_name
= info
->output_semantic_name
[i
];
2824 outputs
[i
].semantic_index
= info
->output_semantic_index
[i
];
2826 for (j
= 0; j
< 4; j
++) {
2827 outputs
[i
].values
[j
] =
2828 LLVMBuildLoad(gallivm
->builder
,
2831 outputs
[i
].vertex_stream
[j
] =
2832 (info
->output_streams
[i
] >> (2 * j
)) & 3;
2837 /* Return the primitive ID from the LLVM function. */
2839 LLVMBuildInsertValue(gallivm
->builder
,
2841 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2842 get_primitive_id(bld_base
, 0)),
2843 VS_EPILOG_PRIMID_LOC
, "");
2845 if (ctx
->shader
->selector
->so
.num_outputs
)
2846 si_llvm_emit_streamout(ctx
, outputs
, i
, 0);
2847 si_llvm_export_vs(bld_base
, outputs
, i
);
2851 struct si_ps_exports
{
2853 LLVMValueRef args
[10][9];
2856 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
2857 bool writes_samplemask
)
2860 /* Z needs 32 bits. */
2861 if (writes_samplemask
)
2862 return V_028710_SPI_SHADER_32_ABGR
;
2863 else if (writes_stencil
)
2864 return V_028710_SPI_SHADER_32_GR
;
2866 return V_028710_SPI_SHADER_32_R
;
2867 } else if (writes_stencil
|| writes_samplemask
) {
2868 /* Both stencil and sample mask need only 16 bits. */
2869 return V_028710_SPI_SHADER_UINT16_ABGR
;
2871 return V_028710_SPI_SHADER_ZERO
;
2875 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2876 LLVMValueRef depth
, LLVMValueRef stencil
,
2877 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
2879 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2880 struct lp_build_context
*base
= &bld_base
->base
;
2881 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2882 LLVMValueRef args
[9];
2884 unsigned format
= si_get_spi_shader_z_format(depth
!= NULL
,
2886 samplemask
!= NULL
);
2888 assert(depth
|| stencil
|| samplemask
);
2890 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2891 args
[2] = uint
->one
; /* DONE bit */
2893 /* Specify the target we are exporting */
2894 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2896 args
[4] = uint
->zero
; /* COMP flag */
2897 args
[5] = base
->undef
; /* R, depth */
2898 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2899 args
[7] = base
->undef
; /* B, sample mask */
2900 args
[8] = base
->undef
; /* A, alpha to mask */
2902 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
2904 args
[4] = uint
->one
; /* COMPR flag */
2907 /* Stencil should be in X[23:16]. */
2908 stencil
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, stencil
);
2909 stencil
= LLVMBuildShl(base
->gallivm
->builder
, stencil
,
2910 LLVMConstInt(ctx
->i32
, 16, 0), "");
2911 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
, stencil
);
2915 /* SampleMask should be in Y[15:0]. */
2916 args
[6] = samplemask
;
2929 args
[7] = samplemask
;
2934 /* SI (except OLAND and HAINAN) has a bug that it only looks
2935 * at the X writemask component. */
2936 if (ctx
->screen
->b
.chip_class
== SI
&&
2937 ctx
->screen
->b
.family
!= CHIP_OLAND
&&
2938 ctx
->screen
->b
.family
!= CHIP_HAINAN
)
2941 /* Specify which components to enable */
2942 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2944 memcpy(exp
->args
[exp
->num
++], args
, sizeof(args
));
2947 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2948 LLVMValueRef
*color
, unsigned index
,
2949 unsigned samplemask_param
,
2950 bool is_last
, struct si_ps_exports
*exp
)
2952 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2953 struct lp_build_context
*base
= &bld_base
->base
;
2957 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
2958 for (i
= 0; i
< 4; i
++)
2959 color
[i
] = si_llvm_saturate(bld_base
, color
[i
]);
2962 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
2963 color
[3] = base
->one
;
2967 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2968 si_alpha_test(bld_base
, color
[3]);
2970 /* Line & polygon smoothing */
2971 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
2972 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
2975 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2976 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
2977 LLVMValueRef args
[8][9];
2980 /* Get the export arguments, also find out what the last one is. */
2981 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
2982 si_llvm_init_export_args(bld_base
, color
,
2983 V_008DFC_SQ_EXP_MRT
+ c
, args
[c
]);
2984 if (args
[c
][0] != bld_base
->uint_bld
.zero
)
2988 /* Emit all exports. */
2989 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
2990 if (is_last
&& last
== c
) {
2991 args
[c
][1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2992 args
[c
][2] = bld_base
->uint_bld
.one
; /* DONE bit */
2993 } else if (args
[c
][0] == bld_base
->uint_bld
.zero
)
2994 continue; /* unnecessary NULL export */
2996 memcpy(exp
->args
[exp
->num
++], args
[c
], sizeof(args
[c
]));
2999 LLVMValueRef args
[9];
3002 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3005 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
3006 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
3007 } else if (args
[0] == bld_base
->uint_bld
.zero
)
3008 return; /* unnecessary NULL export */
3010 memcpy(exp
->args
[exp
->num
++], args
, sizeof(args
));
3014 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3015 struct si_ps_exports
*exp
)
3017 for (unsigned i
= 0; i
< exp
->num
; i
++)
3018 lp_build_intrinsic(ctx
->gallivm
.builder
,
3019 "llvm.SI.export", ctx
->voidt
,
3020 exp
->args
[i
], 9, 0);
3023 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
3025 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3026 struct lp_build_context
*base
= &bld_base
->base
;
3027 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3028 LLVMValueRef args
[9];
3030 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
3031 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
3032 args
[2] = uint
->one
; /* DONE bit */
3033 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
3034 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
3035 args
[5] = base
->undef
; /* R */
3036 args
[6] = base
->undef
; /* G */
3037 args
[7] = base
->undef
; /* B */
3038 args
[8] = base
->undef
; /* A */
3040 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
3041 ctx
->voidt
, args
, 9, 0);
3045 * Return PS outputs in this order:
3047 * v[0:3] = color0.xyzw
3048 * v[4:7] = color1.xyzw
3053 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3055 * The alpha-ref SGPR is returned via its original location.
3057 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
3059 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3060 struct si_shader
*shader
= ctx
->shader
;
3061 struct lp_build_context
*base
= &bld_base
->base
;
3062 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3063 LLVMBuilderRef builder
= base
->gallivm
->builder
;
3064 unsigned i
, j
, first_vgpr
, vgpr
;
3066 LLVMValueRef color
[8][4] = {};
3067 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3070 /* Read the output values. */
3071 for (i
= 0; i
< info
->num_outputs
; i
++) {
3072 unsigned semantic_name
= info
->output_semantic_name
[i
];
3073 unsigned semantic_index
= info
->output_semantic_index
[i
];
3075 switch (semantic_name
) {
3076 case TGSI_SEMANTIC_COLOR
:
3077 assert(semantic_index
< 8);
3078 for (j
= 0; j
< 4; j
++) {
3079 LLVMValueRef ptr
= ctx
->outputs
[i
][j
];
3080 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3081 color
[semantic_index
][j
] = result
;
3084 case TGSI_SEMANTIC_POSITION
:
3085 depth
= LLVMBuildLoad(builder
,
3086 ctx
->outputs
[i
][2], "");
3088 case TGSI_SEMANTIC_STENCIL
:
3089 stencil
= LLVMBuildLoad(builder
,
3090 ctx
->outputs
[i
][1], "");
3092 case TGSI_SEMANTIC_SAMPLEMASK
:
3093 samplemask
= LLVMBuildLoad(builder
,
3094 ctx
->outputs
[i
][0], "");
3097 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3102 /* Fill the return structure. */
3103 ret
= ctx
->return_value
;
3106 ret
= LLVMBuildInsertValue(builder
, ret
,
3107 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
3108 LLVMGetParam(ctx
->main_fn
,
3109 SI_PARAM_ALPHA_REF
)),
3110 SI_SGPR_ALPHA_REF
, "");
3113 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3114 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3118 for (j
= 0; j
< 4; j
++)
3119 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3122 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3124 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3126 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3128 /* Add the input sample mask for smoothing at the end. */
3129 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3130 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3131 ret
= LLVMBuildInsertValue(builder
, ret
,
3132 LLVMGetParam(ctx
->main_fn
,
3133 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3135 ctx
->return_value
= ret
;
3139 * Given a v8i32 resource descriptor for a buffer, extract the size of the
3140 * buffer in number of elements and return it as an i32.
3142 static LLVMValueRef
get_buffer_size(
3143 struct lp_build_tgsi_context
*bld_base
,
3144 LLVMValueRef descriptor
)
3146 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3147 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3148 LLVMBuilderRef builder
= gallivm
->builder
;
3150 LLVMBuildExtractElement(builder
, descriptor
,
3151 lp_build_const_int32(gallivm
, 2), "");
3153 if (ctx
->screen
->b
.chip_class
>= VI
) {
3154 /* On VI, the descriptor contains the size in bytes,
3155 * but TXQ must return the size in elements.
3156 * The stride is always non-zero for resources using TXQ.
3158 LLVMValueRef stride
=
3159 LLVMBuildExtractElement(builder
, descriptor
,
3160 lp_build_const_int32(gallivm
, 1), "");
3161 stride
= LLVMBuildLShr(builder
, stride
,
3162 lp_build_const_int32(gallivm
, 16), "");
3163 stride
= LLVMBuildAnd(builder
, stride
,
3164 lp_build_const_int32(gallivm
, 0x3FFF), "");
3166 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
3173 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
3176 static void build_type_name_for_intr(
3178 char *buf
, unsigned bufsize
)
3180 LLVMTypeRef elem_type
= type
;
3182 assert(bufsize
>= 8);
3184 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
3185 int ret
= snprintf(buf
, bufsize
, "v%u",
3186 LLVMGetVectorSize(type
));
3188 char *type_name
= LLVMPrintTypeToString(type
);
3189 fprintf(stderr
, "Error building type name for: %s\n",
3193 elem_type
= LLVMGetElementType(type
);
3197 switch (LLVMGetTypeKind(elem_type
)) {
3199 case LLVMIntegerTypeKind
:
3200 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
3202 case LLVMFloatTypeKind
:
3203 snprintf(buf
, bufsize
, "f32");
3205 case LLVMDoubleTypeKind
:
3206 snprintf(buf
, bufsize
, "f64");
3211 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3212 struct lp_build_tgsi_context
*bld_base
,
3213 struct lp_build_emit_data
*emit_data
);
3215 /* Prevent optimizations (at least of memory accesses) across the current
3216 * point in the program by emitting empty inline assembly that is marked as
3217 * having side effects.
3219 #if 0 /* unused currently */
3220 static void emit_optimization_barrier(struct si_shader_context
*ctx
)
3222 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3223 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
3224 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, "", "", true, false);
3225 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
3229 /* Combine these with & instead of |. */
3230 #define NOOP_WAITCNT 0xf7f
3231 #define LGKM_CNT 0x07f
3232 #define VM_CNT 0xf70
3234 static void emit_waitcnt(struct si_shader_context
*ctx
, unsigned simm16
)
3236 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3237 LLVMBuilderRef builder
= gallivm
->builder
;
3238 LLVMValueRef args
[1] = {
3239 lp_build_const_int32(gallivm
, simm16
)
3241 lp_build_intrinsic(builder
, "llvm.amdgcn.s.waitcnt",
3242 ctx
->voidt
, args
, 1, 0);
3245 static void membar_emit(
3246 const struct lp_build_tgsi_action
*action
,
3247 struct lp_build_tgsi_context
*bld_base
,
3248 struct lp_build_emit_data
*emit_data
)
3250 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3251 LLVMValueRef src0
= lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
3252 unsigned flags
= LLVMConstIntGetZExtValue(src0
);
3253 unsigned waitcnt
= NOOP_WAITCNT
;
3255 if (flags
& TGSI_MEMBAR_THREAD_GROUP
)
3256 waitcnt
&= VM_CNT
& LGKM_CNT
;
3258 if (flags
& (TGSI_MEMBAR_ATOMIC_BUFFER
|
3259 TGSI_MEMBAR_SHADER_BUFFER
|
3260 TGSI_MEMBAR_SHADER_IMAGE
))
3263 if (flags
& TGSI_MEMBAR_SHARED
)
3264 waitcnt
&= LGKM_CNT
;
3266 if (waitcnt
!= NOOP_WAITCNT
)
3267 emit_waitcnt(ctx
, waitcnt
);
3271 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
3272 const struct tgsi_full_src_register
*reg
)
3275 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3276 SI_PARAM_SHADER_BUFFERS
);
3278 if (!reg
->Register
.Indirect
)
3279 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, 0);
3281 index
= get_bounded_indirect_index(ctx
, ®
->Indirect
,
3282 reg
->Register
.Index
,
3283 SI_NUM_SHADER_BUFFERS
);
3285 return ac_build_indexed_load_const(&ctx
->ac
, rsrc_ptr
, index
);
3288 static bool tgsi_is_array_sampler(unsigned target
)
3290 return target
== TGSI_TEXTURE_1D_ARRAY
||
3291 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
3292 target
== TGSI_TEXTURE_2D_ARRAY
||
3293 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
3294 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3295 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
3296 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3299 static bool tgsi_is_array_image(unsigned target
)
3301 return target
== TGSI_TEXTURE_3D
||
3302 target
== TGSI_TEXTURE_CUBE
||
3303 target
== TGSI_TEXTURE_1D_ARRAY
||
3304 target
== TGSI_TEXTURE_2D_ARRAY
||
3305 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3306 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3310 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
3312 * At least on Tonga, executing image stores on images with DCC enabled and
3313 * non-trivial can eventually lead to lockups. This can occur when an
3314 * application binds an image as read-only but then uses a shader that writes
3315 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
3316 * program termination) in this case, but it doesn't cost much to be a bit
3317 * nicer: disabling DCC in the shader still leads to undefined results but
3318 * avoids the lockup.
3320 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
3323 if (ctx
->screen
->b
.chip_class
<= CIK
) {
3326 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3327 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
3328 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
3331 tmp
= LLVMBuildExtractElement(builder
, rsrc
, i32_6
, "");
3332 tmp
= LLVMBuildAnd(builder
, tmp
, i32_C
, "");
3333 return LLVMBuildInsertElement(builder
, rsrc
, tmp
, i32_6
, "");
3337 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3339 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3344 * Load the resource descriptor for \p image.
3348 struct lp_build_tgsi_context
*bld_base
,
3349 const struct tgsi_full_src_register
*image
,
3350 bool is_store
, unsigned target
,
3353 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3354 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3356 LLVMValueRef index
, tmp
;
3357 bool dcc_off
= target
!= TGSI_TEXTURE_BUFFER
&& is_store
;
3359 assert(image
->Register
.File
== TGSI_FILE_IMAGE
);
3361 if (!image
->Register
.Indirect
) {
3362 const struct tgsi_shader_info
*info
= bld_base
->info
;
3364 index
= LLVMConstInt(ctx
->i32
, image
->Register
.Index
, 0);
3366 if (info
->images_writemask
& (1 << image
->Register
.Index
) &&
3367 target
!= TGSI_TEXTURE_BUFFER
)
3370 /* From the GL_ARB_shader_image_load_store extension spec:
3372 * If a shader performs an image load, store, or atomic
3373 * operation using an image variable declared as an array,
3374 * and if the index used to select an individual element is
3375 * negative or greater than or equal to the size of the
3376 * array, the results of the operation are undefined but may
3377 * not lead to termination.
3379 index
= get_bounded_indirect_index(ctx
, &image
->Indirect
,
3380 image
->Register
.Index
,
3384 if (target
== TGSI_TEXTURE_BUFFER
) {
3385 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3387 rsrc_ptr
= LLVMBuildPointerCast(builder
, rsrc_ptr
,
3388 const_array(ctx
->v4i32
, 0), "");
3389 index
= LLVMBuildMul(builder
, index
,
3390 LLVMConstInt(ctx
->i32
, 2, 0), "");
3391 index
= LLVMBuildAdd(builder
, index
,
3392 LLVMConstInt(ctx
->i32
, 1, 0), "");
3393 *rsrc
= ac_build_indexed_load_const(&ctx
->ac
, rsrc_ptr
, index
);
3397 tmp
= ac_build_indexed_load_const(&ctx
->ac
, rsrc_ptr
, index
);
3399 tmp
= force_dcc_off(ctx
, tmp
);
3403 static LLVMValueRef
image_fetch_coords(
3404 struct lp_build_tgsi_context
*bld_base
,
3405 const struct tgsi_full_instruction
*inst
,
3408 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3409 LLVMBuilderRef builder
= gallivm
->builder
;
3410 unsigned target
= inst
->Memory
.Texture
;
3411 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
3412 LLVMValueRef coords
[4];
3416 for (chan
= 0; chan
< num_coords
; ++chan
) {
3417 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
3418 tmp
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3422 if (num_coords
== 1)
3425 if (num_coords
== 3) {
3426 /* LLVM has difficulties lowering 3-element vectors. */
3427 coords
[3] = bld_base
->uint_bld
.undef
;
3431 return lp_build_gather_values(gallivm
, coords
, num_coords
);
3435 * Append the extra mode bits that are used by image load and store.
3437 static void image_append_args(
3438 struct si_shader_context
*ctx
,
3439 struct lp_build_emit_data
* emit_data
,
3444 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3445 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3446 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3447 LLVMValueRef r128
= i1false
;
3448 LLVMValueRef da
= tgsi_is_array_image(target
) ? i1true
: i1false
;
3451 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3453 LLVMValueRef slc
= i1false
;
3454 LLVMValueRef lwe
= i1false
;
3456 if (atomic
|| (HAVE_LLVM
<= 0x0309)) {
3457 emit_data
->args
[emit_data
->arg_count
++] = r128
;
3458 emit_data
->args
[emit_data
->arg_count
++] = da
;
3460 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3462 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3466 /* HAVE_LLVM >= 0x0400 */
3467 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3468 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3469 emit_data
->args
[emit_data
->arg_count
++] = lwe
;
3470 emit_data
->args
[emit_data
->arg_count
++] = da
;
3474 * Append the resource and indexing arguments for buffer intrinsics.
3476 * \param rsrc the v4i32 buffer resource
3477 * \param index index into the buffer (stride-based)
3478 * \param offset byte offset into the buffer
3480 static void buffer_append_args(
3481 struct si_shader_context
*ctx
,
3482 struct lp_build_emit_data
*emit_data
,
3485 LLVMValueRef offset
,
3489 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3490 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3491 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3493 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3494 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
3495 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
3497 emit_data
->args
[emit_data
->arg_count
++] =
3499 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3500 i1true
: i1false
; /* glc */
3502 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
3505 static void load_fetch_args(
3506 struct lp_build_tgsi_context
* bld_base
,
3507 struct lp_build_emit_data
* emit_data
)
3509 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3510 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3511 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3512 unsigned target
= inst
->Memory
.Texture
;
3515 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
3517 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3518 LLVMBuilderRef builder
= gallivm
->builder
;
3519 LLVMValueRef offset
;
3522 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3524 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3525 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3527 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3528 offset
, false, false);
3529 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3530 LLVMValueRef coords
;
3532 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
3533 coords
= image_fetch_coords(bld_base
, inst
, 1);
3535 if (target
== TGSI_TEXTURE_BUFFER
) {
3536 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3537 bld_base
->uint_bld
.zero
, false, false);
3539 emit_data
->args
[0] = coords
;
3540 emit_data
->args
[1] = rsrc
;
3541 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
3542 emit_data
->arg_count
= 3;
3544 image_append_args(ctx
, emit_data
, target
, false, false);
3549 static void load_emit_buffer(struct si_shader_context
*ctx
,
3550 struct lp_build_emit_data
*emit_data
)
3552 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3553 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3554 LLVMBuilderRef builder
= gallivm
->builder
;
3555 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
3556 uint count
= util_last_bit(writemask
);
3557 const char *intrinsic_name
;
3558 LLVMTypeRef dst_type
;
3562 intrinsic_name
= "llvm.amdgcn.buffer.load.f32";
3563 dst_type
= ctx
->f32
;
3566 intrinsic_name
= "llvm.amdgcn.buffer.load.v2f32";
3567 dst_type
= LLVMVectorType(ctx
->f32
, 2);
3570 intrinsic_name
= "llvm.amdgcn.buffer.load.v4f32";
3571 dst_type
= ctx
->v4f32
;
3575 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3576 builder
, intrinsic_name
, dst_type
,
3577 emit_data
->args
, emit_data
->arg_count
,
3578 LP_FUNC_ATTR_READONLY
);
3581 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
3582 const struct tgsi_full_instruction
*inst
,
3583 LLVMTypeRef type
, int arg
)
3585 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3586 LLVMBuilderRef builder
= gallivm
->builder
;
3587 LLVMValueRef offset
, ptr
;
3590 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
3591 offset
= LLVMBuildBitCast(builder
, offset
, ctx
->i32
, "");
3593 ptr
= ctx
->shared_memory
;
3594 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
3595 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3596 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
3601 static void load_emit_memory(
3602 struct si_shader_context
*ctx
,
3603 struct lp_build_emit_data
*emit_data
)
3605 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3606 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
3607 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3608 LLVMBuilderRef builder
= gallivm
->builder
;
3609 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3610 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
3613 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 1);
3615 for (chan
= 0; chan
< 4; ++chan
) {
3616 if (!(writemask
& (1 << chan
))) {
3617 channels
[chan
] = LLVMGetUndef(base
->elem_type
);
3621 index
= lp_build_const_int32(gallivm
, chan
);
3622 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3623 channels
[chan
] = LLVMBuildLoad(builder
, derived_ptr
, "");
3625 emit_data
->output
[emit_data
->chan
] = lp_build_gather_values(gallivm
, channels
, 4);
3628 static void get_image_intr_name(const char *base_name
,
3629 LLVMTypeRef data_type
,
3630 LLVMTypeRef coords_type
,
3631 LLVMTypeRef rsrc_type
,
3632 char *out_name
, unsigned out_len
)
3634 char coords_type_name
[8];
3636 build_type_name_for_intr(coords_type
, coords_type_name
,
3637 sizeof(coords_type_name
));
3639 if (HAVE_LLVM
<= 0x0309) {
3640 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
3642 char data_type_name
[8];
3643 char rsrc_type_name
[8];
3645 build_type_name_for_intr(data_type
, data_type_name
,
3646 sizeof(data_type_name
));
3647 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
3648 sizeof(rsrc_type_name
));
3649 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
3650 data_type_name
, coords_type_name
, rsrc_type_name
);
3654 static void load_emit(
3655 const struct lp_build_tgsi_action
*action
,
3656 struct lp_build_tgsi_context
*bld_base
,
3657 struct lp_build_emit_data
*emit_data
)
3659 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3660 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3661 LLVMBuilderRef builder
= gallivm
->builder
;
3662 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3663 char intrinsic_name
[64];
3665 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3666 load_emit_memory(ctx
, emit_data
);
3670 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3671 emit_waitcnt(ctx
, VM_CNT
);
3673 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3674 load_emit_buffer(ctx
, emit_data
);
3678 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
3679 emit_data
->output
[emit_data
->chan
] =
3681 builder
, "llvm.amdgcn.buffer.load.format.v4f32", emit_data
->dst_type
,
3682 emit_data
->args
, emit_data
->arg_count
,
3683 LP_FUNC_ATTR_READONLY
);
3685 get_image_intr_name("llvm.amdgcn.image.load",
3686 emit_data
->dst_type
, /* vdata */
3687 LLVMTypeOf(emit_data
->args
[0]), /* coords */
3688 LLVMTypeOf(emit_data
->args
[1]), /* rsrc */
3689 intrinsic_name
, sizeof(intrinsic_name
));
3691 emit_data
->output
[emit_data
->chan
] =
3693 builder
, intrinsic_name
, emit_data
->dst_type
,
3694 emit_data
->args
, emit_data
->arg_count
,
3695 LP_FUNC_ATTR_READONLY
);
3699 static void store_fetch_args(
3700 struct lp_build_tgsi_context
* bld_base
,
3701 struct lp_build_emit_data
* emit_data
)
3703 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3704 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3705 LLVMBuilderRef builder
= gallivm
->builder
;
3706 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3707 struct tgsi_full_src_register memory
;
3708 LLVMValueRef chans
[4];
3713 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
3715 for (chan
= 0; chan
< 4; ++chan
) {
3716 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
3718 data
= lp_build_gather_values(gallivm
, chans
, 4);
3720 emit_data
->args
[emit_data
->arg_count
++] = data
;
3722 memory
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
3724 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3725 LLVMValueRef offset
;
3728 rsrc
= shader_buffer_fetch_rsrc(ctx
, &memory
);
3730 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
3731 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3733 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3734 offset
, false, false);
3735 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3736 unsigned target
= inst
->Memory
.Texture
;
3737 LLVMValueRef coords
;
3739 /* 8bit/16bit TC L1 write corruption bug on SI.
3740 * All store opcodes not aligned to a dword are affected.
3742 * The only way to get unaligned stores in radeonsi is through
3745 bool force_glc
= ctx
->screen
->b
.chip_class
== SI
;
3747 coords
= image_fetch_coords(bld_base
, inst
, 0);
3749 if (target
== TGSI_TEXTURE_BUFFER
) {
3750 image_fetch_rsrc(bld_base
, &memory
, true, target
, &rsrc
);
3751 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3752 bld_base
->uint_bld
.zero
, false, force_glc
);
3754 emit_data
->args
[1] = coords
;
3755 image_fetch_rsrc(bld_base
, &memory
, true, target
,
3756 &emit_data
->args
[2]);
3757 emit_data
->args
[3] = lp_build_const_int32(gallivm
, 15); /* dmask */
3758 emit_data
->arg_count
= 4;
3760 image_append_args(ctx
, emit_data
, target
, false, force_glc
);
3765 static void store_emit_buffer(
3766 struct si_shader_context
*ctx
,
3767 struct lp_build_emit_data
*emit_data
)
3769 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3770 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3771 LLVMBuilderRef builder
= gallivm
->builder
;
3772 struct lp_build_context
*uint_bld
= &ctx
->bld_base
.uint_bld
;
3773 LLVMValueRef base_data
= emit_data
->args
[0];
3774 LLVMValueRef base_offset
= emit_data
->args
[3];
3775 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3779 const char *intrinsic_name
;
3781 LLVMValueRef offset
;
3784 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
3786 /* Due to an LLVM limitation, split 3-element writes
3787 * into a 2-element and a 1-element write. */
3789 writemask
|= 1 << (start
+ 2);
3795 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
3796 } else if (count
== 2) {
3797 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
3799 tmp
= LLVMBuildExtractElement(
3801 lp_build_const_int32(gallivm
, start
), "");
3802 data
= LLVMBuildInsertElement(
3803 builder
, LLVMGetUndef(v2f32
), tmp
,
3804 uint_bld
->zero
, "");
3806 tmp
= LLVMBuildExtractElement(
3808 lp_build_const_int32(gallivm
, start
+ 1), "");
3809 data
= LLVMBuildInsertElement(
3810 builder
, data
, tmp
, uint_bld
->one
, "");
3812 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
3815 data
= LLVMBuildExtractElement(
3817 lp_build_const_int32(gallivm
, start
), "");
3818 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
3821 offset
= base_offset
;
3823 offset
= LLVMBuildAdd(
3825 lp_build_const_int32(gallivm
, start
* 4), "");
3828 emit_data
->args
[0] = data
;
3829 emit_data
->args
[3] = offset
;
3832 builder
, intrinsic_name
, emit_data
->dst_type
,
3833 emit_data
->args
, emit_data
->arg_count
, 0);
3837 static void store_emit_memory(
3838 struct si_shader_context
*ctx
,
3839 struct lp_build_emit_data
*emit_data
)
3841 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3842 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3843 struct lp_build_context
*base
= &ctx
->bld_base
.base
;
3844 LLVMBuilderRef builder
= gallivm
->builder
;
3845 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3846 LLVMValueRef ptr
, derived_ptr
, data
, index
;
3849 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 0);
3851 for (chan
= 0; chan
< 4; ++chan
) {
3852 if (!(writemask
& (1 << chan
))) {
3855 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
3856 index
= lp_build_const_int32(gallivm
, chan
);
3857 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3858 LLVMBuildStore(builder
, data
, derived_ptr
);
3862 static void store_emit(
3863 const struct lp_build_tgsi_action
*action
,
3864 struct lp_build_tgsi_context
*bld_base
,
3865 struct lp_build_emit_data
*emit_data
)
3867 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3868 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3869 LLVMBuilderRef builder
= gallivm
->builder
;
3870 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3871 unsigned target
= inst
->Memory
.Texture
;
3872 char intrinsic_name
[64];
3874 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3875 store_emit_memory(ctx
, emit_data
);
3879 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3880 emit_waitcnt(ctx
, VM_CNT
);
3882 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3883 store_emit_buffer(ctx
, emit_data
);
3887 if (target
== TGSI_TEXTURE_BUFFER
) {
3888 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3889 builder
, "llvm.amdgcn.buffer.store.format.v4f32",
3890 emit_data
->dst_type
, emit_data
->args
,
3891 emit_data
->arg_count
, 0);
3893 get_image_intr_name("llvm.amdgcn.image.store",
3894 LLVMTypeOf(emit_data
->args
[0]), /* vdata */
3895 LLVMTypeOf(emit_data
->args
[1]), /* coords */
3896 LLVMTypeOf(emit_data
->args
[2]), /* rsrc */
3897 intrinsic_name
, sizeof(intrinsic_name
));
3899 emit_data
->output
[emit_data
->chan
] =
3901 builder
, intrinsic_name
, emit_data
->dst_type
,
3902 emit_data
->args
, emit_data
->arg_count
, 0);
3906 static void atomic_fetch_args(
3907 struct lp_build_tgsi_context
* bld_base
,
3908 struct lp_build_emit_data
* emit_data
)
3910 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3911 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3912 LLVMBuilderRef builder
= gallivm
->builder
;
3913 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3914 LLVMValueRef data1
, data2
;
3918 emit_data
->dst_type
= bld_base
->base
.elem_type
;
3920 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
3921 data1
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3923 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3924 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
3925 data2
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3928 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
3929 * of arguments, which is reversed relative to TGSI (and GLSL)
3931 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
3932 emit_data
->args
[emit_data
->arg_count
++] = data2
;
3933 emit_data
->args
[emit_data
->arg_count
++] = data1
;
3935 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3936 LLVMValueRef offset
;
3938 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3940 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3941 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3943 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3944 offset
, true, false);
3945 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3946 unsigned target
= inst
->Memory
.Texture
;
3947 LLVMValueRef coords
;
3949 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
3950 coords
= image_fetch_coords(bld_base
, inst
, 1);
3952 if (target
== TGSI_TEXTURE_BUFFER
) {
3953 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3954 bld_base
->uint_bld
.zero
, true, false);
3956 emit_data
->args
[emit_data
->arg_count
++] = coords
;
3957 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3959 image_append_args(ctx
, emit_data
, target
, true, false);
3964 static void atomic_emit_memory(struct si_shader_context
*ctx
,
3965 struct lp_build_emit_data
*emit_data
) {
3966 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3967 LLVMBuilderRef builder
= gallivm
->builder
;
3968 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3969 LLVMValueRef ptr
, result
, arg
;
3971 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
3973 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
3974 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
3976 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3977 LLVMValueRef new_data
;
3978 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
3981 new_data
= LLVMBuildBitCast(builder
, new_data
, ctx
->i32
, "");
3983 #if HAVE_LLVM >= 0x309
3984 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
3985 LLVMAtomicOrderingSequentiallyConsistent
,
3986 LLVMAtomicOrderingSequentiallyConsistent
,
3990 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
3992 LLVMAtomicRMWBinOp op
;
3994 switch(inst
->Instruction
.Opcode
) {
3995 case TGSI_OPCODE_ATOMUADD
:
3996 op
= LLVMAtomicRMWBinOpAdd
;
3998 case TGSI_OPCODE_ATOMXCHG
:
3999 op
= LLVMAtomicRMWBinOpXchg
;
4001 case TGSI_OPCODE_ATOMAND
:
4002 op
= LLVMAtomicRMWBinOpAnd
;
4004 case TGSI_OPCODE_ATOMOR
:
4005 op
= LLVMAtomicRMWBinOpOr
;
4007 case TGSI_OPCODE_ATOMXOR
:
4008 op
= LLVMAtomicRMWBinOpXor
;
4010 case TGSI_OPCODE_ATOMUMIN
:
4011 op
= LLVMAtomicRMWBinOpUMin
;
4013 case TGSI_OPCODE_ATOMUMAX
:
4014 op
= LLVMAtomicRMWBinOpUMax
;
4016 case TGSI_OPCODE_ATOMIMIN
:
4017 op
= LLVMAtomicRMWBinOpMin
;
4019 case TGSI_OPCODE_ATOMIMAX
:
4020 op
= LLVMAtomicRMWBinOpMax
;
4023 unreachable("unknown atomic opcode");
4026 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
4027 LLVMAtomicOrderingSequentiallyConsistent
,
4030 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
4033 static void atomic_emit(
4034 const struct lp_build_tgsi_action
*action
,
4035 struct lp_build_tgsi_context
*bld_base
,
4036 struct lp_build_emit_data
*emit_data
)
4038 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4039 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4040 LLVMBuilderRef builder
= gallivm
->builder
;
4041 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
4042 char intrinsic_name
[40];
4045 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
4046 atomic_emit_memory(ctx
, emit_data
);
4050 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
4051 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4052 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4053 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
4055 LLVMValueRef coords
;
4056 char coords_type
[8];
4058 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
4059 coords
= emit_data
->args
[2];
4061 coords
= emit_data
->args
[1];
4063 build_type_name_for_intr(LLVMTypeOf(coords
), coords_type
, sizeof(coords_type
));
4064 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4065 "llvm.amdgcn.image.atomic.%s.%s",
4066 action
->intr_name
, coords_type
);
4069 tmp
= lp_build_intrinsic(
4070 builder
, intrinsic_name
, bld_base
->uint_bld
.elem_type
,
4071 emit_data
->args
, emit_data
->arg_count
, 0);
4072 emit_data
->output
[emit_data
->chan
] =
4073 LLVMBuildBitCast(builder
, tmp
, bld_base
->base
.elem_type
, "");
4076 static void resq_fetch_args(
4077 struct lp_build_tgsi_context
* bld_base
,
4078 struct lp_build_emit_data
* emit_data
)
4080 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4081 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4082 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4083 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
4085 emit_data
->dst_type
= ctx
->v4i32
;
4087 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
4088 emit_data
->args
[0] = shader_buffer_fetch_rsrc(ctx
, reg
);
4089 emit_data
->arg_count
= 1;
4090 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4091 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4092 &emit_data
->args
[0]);
4093 emit_data
->arg_count
= 1;
4095 emit_data
->args
[0] = bld_base
->uint_bld
.zero
; /* mip level */
4096 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4097 &emit_data
->args
[1]);
4098 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
4099 emit_data
->args
[3] = bld_base
->uint_bld
.zero
; /* unorm */
4100 emit_data
->args
[4] = bld_base
->uint_bld
.zero
; /* r128 */
4101 emit_data
->args
[5] = tgsi_is_array_image(inst
->Memory
.Texture
) ?
4102 bld_base
->uint_bld
.one
: bld_base
->uint_bld
.zero
; /* da */
4103 emit_data
->args
[6] = bld_base
->uint_bld
.zero
; /* glc */
4104 emit_data
->args
[7] = bld_base
->uint_bld
.zero
; /* slc */
4105 emit_data
->args
[8] = bld_base
->uint_bld
.zero
; /* tfe */
4106 emit_data
->args
[9] = bld_base
->uint_bld
.zero
; /* lwe */
4107 emit_data
->arg_count
= 10;
4111 static void resq_emit(
4112 const struct lp_build_tgsi_action
*action
,
4113 struct lp_build_tgsi_context
*bld_base
,
4114 struct lp_build_emit_data
*emit_data
)
4116 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4117 LLVMBuilderRef builder
= gallivm
->builder
;
4118 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4121 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
4122 out
= LLVMBuildExtractElement(builder
, emit_data
->args
[0],
4123 lp_build_const_int32(gallivm
, 2), "");
4124 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4125 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
4127 out
= lp_build_intrinsic(
4128 builder
, "llvm.SI.getresinfo.i32", emit_data
->dst_type
,
4129 emit_data
->args
, emit_data
->arg_count
,
4130 LP_FUNC_ATTR_READNONE
);
4132 /* Divide the number of layers by 6 to get the number of cubes. */
4133 if (inst
->Memory
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
) {
4134 LLVMValueRef imm2
= lp_build_const_int32(gallivm
, 2);
4135 LLVMValueRef imm6
= lp_build_const_int32(gallivm
, 6);
4137 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
4138 z
= LLVMBuildSDiv(builder
, z
, imm6
, "");
4139 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
4143 emit_data
->output
[emit_data
->chan
] = out
;
4146 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
4147 struct lp_build_emit_data
*emit_data
,
4148 unsigned opcode
, unsigned target
,
4149 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4150 LLVMValueRef
*param
, unsigned count
,
4153 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4155 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
4157 /* Pad to power of two vector */
4158 while (count
< util_next_power_of_two(count
))
4159 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4161 /* Texture coordinates. */
4163 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
4165 emit_data
->args
[0] = param
[0];
4168 emit_data
->args
[1] = res_ptr
;
4171 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
4172 emit_data
->dst_type
= ctx
->v4i32
;
4174 emit_data
->dst_type
= ctx
->v4f32
;
4176 emit_data
->args
[num_args
++] = samp_ptr
;
4179 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
4180 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
4181 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
4182 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
4183 tgsi_is_array_sampler(target
)); /* da */
4184 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
4185 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
4186 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
4187 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
4189 emit_data
->arg_count
= num_args
;
4192 static const struct lp_build_tgsi_action tex_action
;
4202 * Load an image view, fmask view. or sampler state descriptor.
4204 static LLVMValueRef
load_sampler_desc_custom(struct si_shader_context
*ctx
,
4205 LLVMValueRef list
, LLVMValueRef index
,
4206 enum desc_type type
)
4208 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4209 LLVMBuilderRef builder
= gallivm
->builder
;
4213 /* The image is at [0:7]. */
4214 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4217 /* The buffer is in [4:7]. */
4218 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4219 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4220 list
= LLVMBuildPointerCast(builder
, list
,
4221 const_array(ctx
->v4i32
, 0), "");
4224 /* The FMASK is at [8:15]. */
4225 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4226 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4229 /* The sampler state is at [12:15]. */
4230 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4231 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
4232 list
= LLVMBuildPointerCast(builder
, list
,
4233 const_array(ctx
->v4i32
, 0), "");
4237 return ac_build_indexed_load_const(&ctx
->ac
, list
, index
);
4240 static LLVMValueRef
load_sampler_desc(struct si_shader_context
*ctx
,
4241 LLVMValueRef index
, enum desc_type type
)
4243 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
,
4246 return load_sampler_desc_custom(ctx
, list
, index
, type
);
4249 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4252 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4253 * filtering manually. The driver sets img7 to a mask clearing
4254 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4255 * s_and_b32 samp0, samp0, img7
4258 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4260 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
4261 LLVMValueRef res
, LLVMValueRef samp
)
4263 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4264 LLVMValueRef img7
, samp0
;
4266 if (ctx
->screen
->b
.chip_class
>= VI
)
4269 img7
= LLVMBuildExtractElement(builder
, res
,
4270 LLVMConstInt(ctx
->i32
, 7, 0), "");
4271 samp0
= LLVMBuildExtractElement(builder
, samp
,
4272 LLVMConstInt(ctx
->i32
, 0, 0), "");
4273 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4274 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4275 LLVMConstInt(ctx
->i32
, 0, 0), "");
4278 static void tex_fetch_ptrs(
4279 struct lp_build_tgsi_context
*bld_base
,
4280 struct lp_build_emit_data
*emit_data
,
4281 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
4283 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4284 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4285 unsigned target
= inst
->Texture
.Texture
;
4286 unsigned sampler_src
;
4287 unsigned sampler_index
;
4290 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
4291 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
4293 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
4294 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
4296 index
= get_bounded_indirect_index(ctx
,
4298 reg
->Register
.Index
,
4301 index
= LLVMConstInt(ctx
->i32
, sampler_index
, 0);
4304 if (target
== TGSI_TEXTURE_BUFFER
)
4305 *res_ptr
= load_sampler_desc(ctx
, index
, DESC_BUFFER
);
4307 *res_ptr
= load_sampler_desc(ctx
, index
, DESC_IMAGE
);
4314 if (target
== TGSI_TEXTURE_2D_MSAA
||
4315 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4317 *fmask_ptr
= load_sampler_desc(ctx
, index
, DESC_FMASK
);
4318 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
4320 *samp_ptr
= load_sampler_desc(ctx
, index
, DESC_SAMPLER
);
4321 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4326 static void txq_fetch_args(
4327 struct lp_build_tgsi_context
*bld_base
,
4328 struct lp_build_emit_data
*emit_data
)
4330 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4331 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4332 unsigned target
= inst
->Texture
.Texture
;
4333 LLVMValueRef res_ptr
;
4334 LLVMValueRef address
;
4336 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, NULL
, NULL
);
4338 if (target
== TGSI_TEXTURE_BUFFER
) {
4339 /* Read the size from the buffer descriptor directly. */
4340 emit_data
->args
[0] = get_buffer_size(bld_base
, res_ptr
);
4344 /* Textures - set the mip level. */
4345 address
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
4347 set_tex_fetch_args(ctx
, emit_data
, TGSI_OPCODE_TXQ
, target
, res_ptr
,
4348 NULL
, &address
, 1, 0xf);
4351 static void txq_emit(const struct lp_build_tgsi_action
*action
,
4352 struct lp_build_tgsi_context
*bld_base
,
4353 struct lp_build_emit_data
*emit_data
)
4355 struct lp_build_context
*base
= &bld_base
->base
;
4356 unsigned target
= emit_data
->inst
->Texture
.Texture
;
4358 if (target
== TGSI_TEXTURE_BUFFER
) {
4359 /* Just return the buffer size. */
4360 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
4364 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4365 base
->gallivm
->builder
, "llvm.SI.getresinfo.i32",
4366 emit_data
->dst_type
, emit_data
->args
, emit_data
->arg_count
,
4367 LP_FUNC_ATTR_READNONE
);
4369 /* Divide the number of layers by 6 to get the number of cubes. */
4370 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
4371 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4372 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
4373 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
4374 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
4376 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
4377 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
4378 z
= LLVMBuildSDiv(builder
, z
, six
, "");
4380 emit_data
->output
[emit_data
->chan
] =
4381 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
4385 static void tex_fetch_args(
4386 struct lp_build_tgsi_context
*bld_base
,
4387 struct lp_build_emit_data
*emit_data
)
4389 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4390 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4391 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4392 unsigned opcode
= inst
->Instruction
.Opcode
;
4393 unsigned target
= inst
->Texture
.Texture
;
4394 LLVMValueRef coords
[5], derivs
[6];
4395 LLVMValueRef address
[16];
4396 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
4397 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
4400 unsigned num_deriv_channels
= 0;
4401 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
4402 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4403 unsigned dmask
= 0xf;
4405 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4407 if (target
== TGSI_TEXTURE_BUFFER
) {
4408 emit_data
->dst_type
= ctx
->v4f32
;
4409 emit_data
->args
[0] = LLVMBuildBitCast(gallivm
->builder
, res_ptr
,
4411 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
4412 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4413 emit_data
->arg_count
= 3;
4417 /* Fetch and project texture coordinates */
4418 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
4419 for (chan
= 0; chan
< 3; chan
++ ) {
4420 coords
[chan
] = lp_build_emit_fetch(bld_base
,
4423 if (opcode
== TGSI_OPCODE_TXP
)
4424 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
4430 if (opcode
== TGSI_OPCODE_TXP
)
4431 coords
[3] = bld_base
->base
.one
;
4434 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
4435 /* The offsets are six-bit signed integers packed like this:
4436 * X=[5:0], Y=[13:8], and Z=[21:16].
4438 LLVMValueRef offset
[3], pack
;
4440 assert(inst
->Texture
.NumOffsets
== 1);
4442 for (chan
= 0; chan
< 3; chan
++) {
4443 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
4444 emit_data
->inst
, 0, chan
);
4445 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
4446 lp_build_const_int32(gallivm
, 0x3f), "");
4448 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
4449 lp_build_const_int32(gallivm
, chan
*8), "");
4452 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
4453 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
4454 address
[count
++] = pack
;
4457 /* Pack LOD bias value */
4458 if (opcode
== TGSI_OPCODE_TXB
)
4459 address
[count
++] = coords
[3];
4460 if (opcode
== TGSI_OPCODE_TXB2
)
4461 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4463 /* Pack depth comparison value */
4464 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
4467 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4468 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4470 assert(ref_pos
>= 0);
4471 z
= coords
[ref_pos
];
4474 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4475 * so the depth comparison value isn't clamped for Z16 and
4476 * Z24 anymore. Do it manually here.
4478 * It's unnecessary if the original texture format was
4479 * Z32_FLOAT, but we don't know that here.
4481 if (ctx
->screen
->b
.chip_class
== VI
)
4482 z
= si_llvm_saturate(bld_base
, z
);
4484 address
[count
++] = z
;
4487 /* Pack user derivatives */
4488 if (opcode
== TGSI_OPCODE_TXD
) {
4489 int param
, num_src_deriv_channels
;
4492 case TGSI_TEXTURE_3D
:
4493 num_src_deriv_channels
= 3;
4494 num_deriv_channels
= 3;
4496 case TGSI_TEXTURE_2D
:
4497 case TGSI_TEXTURE_SHADOW2D
:
4498 case TGSI_TEXTURE_RECT
:
4499 case TGSI_TEXTURE_SHADOWRECT
:
4500 case TGSI_TEXTURE_2D_ARRAY
:
4501 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4502 num_src_deriv_channels
= 2;
4503 num_deriv_channels
= 2;
4505 case TGSI_TEXTURE_CUBE
:
4506 case TGSI_TEXTURE_SHADOWCUBE
:
4507 case TGSI_TEXTURE_CUBE_ARRAY
:
4508 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
4509 /* Cube derivatives will be converted to 2D. */
4510 num_src_deriv_channels
= 3;
4511 num_deriv_channels
= 2;
4513 case TGSI_TEXTURE_1D
:
4514 case TGSI_TEXTURE_SHADOW1D
:
4515 case TGSI_TEXTURE_1D_ARRAY
:
4516 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4517 num_src_deriv_channels
= 1;
4518 num_deriv_channels
= 1;
4521 unreachable("invalid target");
4524 for (param
= 0; param
< 2; param
++)
4525 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
4526 derivs
[param
* num_src_deriv_channels
+ chan
] =
4527 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
4530 if (target
== TGSI_TEXTURE_CUBE
||
4531 target
== TGSI_TEXTURE_CUBE_ARRAY
||
4532 target
== TGSI_TEXTURE_SHADOWCUBE
||
4533 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
4534 ac_prepare_cube_coords(&ctx
->ac
,
4535 opcode
== TGSI_OPCODE_TXD
,
4536 target
== TGSI_TEXTURE_CUBE_ARRAY
||
4537 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
4540 if (opcode
== TGSI_OPCODE_TXD
)
4541 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
4542 address
[count
++] = derivs
[i
];
4544 /* Pack texture coordinates */
4545 address
[count
++] = coords
[0];
4547 address
[count
++] = coords
[1];
4549 address
[count
++] = coords
[2];
4551 /* Pack LOD or sample index */
4552 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
4553 address
[count
++] = coords
[3];
4554 else if (opcode
== TGSI_OPCODE_TXL2
)
4555 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4558 assert(!"Cannot handle more than 16 texture address parameters");
4562 for (chan
= 0; chan
< count
; chan
++ ) {
4563 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
4564 address
[chan
], ctx
->i32
, "");
4567 /* Adjust the sample index according to FMASK.
4569 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4570 * which is the identity mapping. Each nibble says which physical sample
4571 * should be fetched to get that sample.
4573 * For example, 0x11111100 means there are only 2 samples stored and
4574 * the second sample covers 3/4 of the pixel. When reading samples 0
4575 * and 1, return physical sample 0 (determined by the first two 0s
4576 * in FMASK), otherwise return physical sample 1.
4578 * The sample index should be adjusted as follows:
4579 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4581 if (target
== TGSI_TEXTURE_2D_MSAA
||
4582 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4583 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4584 struct lp_build_emit_data txf_emit_data
= *emit_data
;
4585 LLVMValueRef txf_address
[4];
4586 unsigned txf_count
= count
;
4587 struct tgsi_full_instruction inst
= {};
4589 memcpy(txf_address
, address
, sizeof(txf_address
));
4591 if (target
== TGSI_TEXTURE_2D_MSAA
) {
4592 txf_address
[2] = bld_base
->uint_bld
.zero
;
4594 txf_address
[3] = bld_base
->uint_bld
.zero
;
4596 /* Read FMASK using TXF. */
4597 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
4598 inst
.Texture
.Texture
= target
;
4599 txf_emit_data
.inst
= &inst
;
4600 txf_emit_data
.chan
= 0;
4601 set_tex_fetch_args(ctx
, &txf_emit_data
, TGSI_OPCODE_TXF
,
4602 target
, fmask_ptr
, NULL
,
4603 txf_address
, txf_count
, 0xf);
4604 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
4606 /* Initialize some constants. */
4607 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
4608 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
4610 /* Apply the formula. */
4611 LLVMValueRef fmask
=
4612 LLVMBuildExtractElement(gallivm
->builder
,
4613 txf_emit_data
.output
[0],
4614 uint_bld
->zero
, "");
4616 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
4618 LLVMValueRef sample_index4
=
4619 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
4621 LLVMValueRef shifted_fmask
=
4622 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
4624 LLVMValueRef final_sample
=
4625 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
4627 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4628 * resource descriptor is 0 (invalid),
4630 LLVMValueRef fmask_desc
=
4631 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
4634 LLVMValueRef fmask_word1
=
4635 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
4638 LLVMValueRef word1_is_nonzero
=
4639 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
4640 fmask_word1
, uint_bld
->zero
, "");
4642 /* Replace the MSAA sample index. */
4643 address
[sample_chan
] =
4644 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
4645 final_sample
, address
[sample_chan
], "");
4648 if (opcode
== TGSI_OPCODE_TXF
) {
4649 /* add tex offsets */
4650 if (inst
->Texture
.NumOffsets
) {
4651 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4652 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
4654 assert(inst
->Texture
.NumOffsets
== 1);
4657 case TGSI_TEXTURE_3D
:
4658 address
[2] = lp_build_add(uint_bld
, address
[2],
4659 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
]);
4661 case TGSI_TEXTURE_2D
:
4662 case TGSI_TEXTURE_SHADOW2D
:
4663 case TGSI_TEXTURE_RECT
:
4664 case TGSI_TEXTURE_SHADOWRECT
:
4665 case TGSI_TEXTURE_2D_ARRAY
:
4666 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4668 lp_build_add(uint_bld
, address
[1],
4669 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
]);
4671 case TGSI_TEXTURE_1D
:
4672 case TGSI_TEXTURE_SHADOW1D
:
4673 case TGSI_TEXTURE_1D_ARRAY
:
4674 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4676 lp_build_add(uint_bld
, address
[0],
4677 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
]);
4679 /* texture offsets do not apply to other texture targets */
4684 if (opcode
== TGSI_OPCODE_TG4
) {
4685 unsigned gather_comp
= 0;
4687 /* DMASK was repurposed for GATHER4. 4 components are always
4688 * returned and DMASK works like a swizzle - it selects
4689 * the component to fetch. The only valid DMASK values are
4690 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4691 * (red,red,red,red) etc.) The ISA document doesn't mention
4695 /* Get the component index from src1.x for Gather4. */
4696 if (!tgsi_is_shadow_target(target
)) {
4697 LLVMValueRef comp_imm
;
4698 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
4700 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
4702 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
4703 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
4704 gather_comp
= CLAMP(gather_comp
, 0, 3);
4707 dmask
= 1 << gather_comp
;
4710 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
4711 samp_ptr
, address
, count
, dmask
);
4714 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
4715 * incorrectly forces nearest filtering if the texture format is integer.
4716 * The only effect it has on Gather4, which always returns 4 texels for
4717 * bilinear filtering, is that the final coordinates are off by 0.5 of
4720 * The workaround is to subtract 0.5 from the unnormalized coordinates,
4721 * or (0.5 / size) from the normalized coordinates.
4723 static void si_lower_gather4_integer(struct si_shader_context
*ctx
,
4724 struct lp_build_emit_data
*emit_data
,
4725 const char *intr_name
,
4726 unsigned coord_vgpr_index
)
4728 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4729 LLVMValueRef coord
= emit_data
->args
[0];
4730 LLVMValueRef half_texel
[2];
4733 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
||
4734 emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWRECT
) {
4735 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
4737 struct tgsi_full_instruction txq_inst
= {};
4738 struct lp_build_emit_data txq_emit_data
= {};
4740 /* Query the texture size. */
4741 txq_inst
.Texture
.Texture
= emit_data
->inst
->Texture
.Texture
;
4742 txq_emit_data
.inst
= &txq_inst
;
4743 txq_emit_data
.dst_type
= ctx
->v4i32
;
4744 set_tex_fetch_args(ctx
, &txq_emit_data
, TGSI_OPCODE_TXQ
,
4745 txq_inst
.Texture
.Texture
,
4746 emit_data
->args
[1], NULL
,
4747 &ctx
->bld_base
.uint_bld
.zero
,
4749 txq_emit(NULL
, &ctx
->bld_base
, &txq_emit_data
);
4751 /* Compute -0.5 / size. */
4752 for (c
= 0; c
< 2; c
++) {
4754 LLVMBuildExtractElement(builder
, txq_emit_data
.output
[0],
4755 LLVMConstInt(ctx
->i32
, c
, 0), "");
4756 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
4758 lp_build_emit_llvm_unary(&ctx
->bld_base
,
4759 TGSI_OPCODE_RCP
, half_texel
[c
]);
4760 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
4761 LLVMConstReal(ctx
->f32
, -0.5), "");
4765 for (c
= 0; c
< 2; c
++) {
4767 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
4769 tmp
= LLVMBuildExtractElement(builder
, coord
, index
, "");
4770 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->f32
, "");
4771 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
4772 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->i32
, "");
4773 coord
= LLVMBuildInsertElement(builder
, coord
, tmp
, index
, "");
4776 emit_data
->args
[0] = coord
;
4777 emit_data
->output
[emit_data
->chan
] =
4778 lp_build_intrinsic(builder
, intr_name
, emit_data
->dst_type
,
4779 emit_data
->args
, emit_data
->arg_count
,
4780 LP_FUNC_ATTR_READNONE
);
4783 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
4784 struct lp_build_tgsi_context
*bld_base
,
4785 struct lp_build_emit_data
*emit_data
)
4787 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4788 struct lp_build_context
*base
= &bld_base
->base
;
4789 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4790 unsigned opcode
= inst
->Instruction
.Opcode
;
4791 unsigned target
= inst
->Texture
.Texture
;
4792 char intr_name
[127];
4793 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
4794 bool is_shadow
= tgsi_is_shadow_target(target
);
4796 const char *name
= "llvm.SI.image.sample";
4797 const char *infix
= "";
4799 if (target
== TGSI_TEXTURE_BUFFER
) {
4800 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4801 base
->gallivm
->builder
,
4802 "llvm.SI.vs.load.input", emit_data
->dst_type
,
4803 emit_data
->args
, emit_data
->arg_count
,
4804 LP_FUNC_ATTR_READNONE
);
4809 case TGSI_OPCODE_TXF
:
4810 name
= target
== TGSI_TEXTURE_2D_MSAA
||
4811 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
4812 "llvm.SI.image.load" :
4813 "llvm.SI.image.load.mip";
4817 case TGSI_OPCODE_LODQ
:
4818 name
= "llvm.SI.getlod";
4822 case TGSI_OPCODE_TEX
:
4823 case TGSI_OPCODE_TEX2
:
4824 case TGSI_OPCODE_TXP
:
4825 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
4828 case TGSI_OPCODE_TXB
:
4829 case TGSI_OPCODE_TXB2
:
4830 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
4833 case TGSI_OPCODE_TXL
:
4834 case TGSI_OPCODE_TXL2
:
4837 case TGSI_OPCODE_TXD
:
4840 case TGSI_OPCODE_TG4
:
4841 name
= "llvm.SI.gather4";
4849 /* Add the type and suffixes .c, .o if needed. */
4850 build_type_name_for_intr(LLVMTypeOf(emit_data
->args
[0]), type
, sizeof(type
));
4851 sprintf(intr_name
, "%s%s%s%s.%s",
4852 name
, is_shadow
? ".c" : "", infix
,
4853 has_offset
? ".o" : "", type
);
4855 /* The hardware needs special lowering for Gather4 with integer formats. */
4856 if (opcode
== TGSI_OPCODE_TG4
) {
4857 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4858 /* This will also work with non-constant indexing because of how
4859 * glsl_to_tgsi works and we intent to preserve that behavior.
4861 const unsigned src_idx
= 2;
4862 unsigned sampler
= inst
->Src
[src_idx
].Register
.Index
;
4864 assert(inst
->Src
[src_idx
].Register
.File
== TGSI_FILE_SAMPLER
);
4866 if (info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_SINT
||
4867 info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_UINT
) {
4868 /* Texture coordinates start after:
4869 * {offset, bias, z-compare, derivatives}
4870 * Only the offset and z-compare can occur here.
4872 si_lower_gather4_integer(ctx
, emit_data
, intr_name
,
4873 (int)has_offset
+ (int)is_shadow
);
4878 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4879 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
4880 emit_data
->args
, emit_data
->arg_count
,
4881 LP_FUNC_ATTR_READNONE
);
4884 static void si_llvm_emit_txqs(
4885 const struct lp_build_tgsi_action
*action
,
4886 struct lp_build_tgsi_context
*bld_base
,
4887 struct lp_build_emit_data
*emit_data
)
4889 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4890 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4891 LLVMBuilderRef builder
= gallivm
->builder
;
4892 LLVMValueRef res
, samples
;
4893 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4895 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4898 /* Read the samples from the descriptor directly. */
4899 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
4900 samples
= LLVMBuildExtractElement(
4902 lp_build_const_int32(gallivm
, 3), "");
4903 samples
= LLVMBuildLShr(builder
, samples
,
4904 lp_build_const_int32(gallivm
, 16), "");
4905 samples
= LLVMBuildAnd(builder
, samples
,
4906 lp_build_const_int32(gallivm
, 0xf), "");
4907 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
4910 emit_data
->output
[emit_data
->chan
] = samples
;
4914 * SI implements derivatives using the local data store (LDS)
4915 * All writes to the LDS happen in all executing threads at
4916 * the same time. TID is the Thread ID for the current
4917 * thread and is a value between 0 and 63, representing
4918 * the thread's position in the wavefront.
4920 * For the pixel shader threads are grouped into quads of four pixels.
4921 * The TIDs of the pixels of a quad are:
4929 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
4930 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
4931 * the current pixel's column, and masking with 0xfffffffe yields the TID
4932 * of the left pixel of the current pixel's row.
4934 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
4935 * adding 2 yields the TID of the pixel below the top pixel.
4937 /* masks for thread ID. */
4938 #define TID_MASK_TOP_LEFT 0xfffffffc
4939 #define TID_MASK_TOP 0xfffffffd
4940 #define TID_MASK_LEFT 0xfffffffe
4942 static void si_llvm_emit_ddxy(
4943 const struct lp_build_tgsi_action
*action
,
4944 struct lp_build_tgsi_context
*bld_base
,
4945 struct lp_build_emit_data
*emit_data
)
4947 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4948 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4949 unsigned opcode
= emit_data
->info
->opcode
;
4950 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, val
, args
[2];
4954 thread_id
= get_thread_id(ctx
);
4956 if (opcode
== TGSI_OPCODE_DDX_FINE
)
4957 mask
= TID_MASK_LEFT
;
4958 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
4959 mask
= TID_MASK_TOP
;
4961 mask
= TID_MASK_TOP_LEFT
;
4963 tl_tid
= LLVMBuildAnd(gallivm
->builder
, thread_id
,
4964 lp_build_const_int32(gallivm
, mask
), "");
4966 /* for DDX we want to next X pixel, DDY next Y pixel. */
4967 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
4968 trbl_tid
= LLVMBuildAdd(gallivm
->builder
, tl_tid
,
4969 lp_build_const_int32(gallivm
, idx
), "");
4971 val
= LLVMBuildBitCast(gallivm
->builder
, emit_data
->args
[0], ctx
->i32
, "");
4973 if (ctx
->screen
->has_ds_bpermute
) {
4974 args
[0] = LLVMBuildMul(gallivm
->builder
, tl_tid
,
4975 lp_build_const_int32(gallivm
, 4), "");
4977 tl
= lp_build_intrinsic(gallivm
->builder
,
4978 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4979 args
, 2, LP_FUNC_ATTR_READNONE
);
4981 args
[0] = LLVMBuildMul(gallivm
->builder
, trbl_tid
,
4982 lp_build_const_int32(gallivm
, 4), "");
4983 trbl
= lp_build_intrinsic(gallivm
->builder
,
4984 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4985 args
, 2, LP_FUNC_ATTR_READNONE
);
4987 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
4989 store_ptr
= ac_build_gep0(&ctx
->ac
, ctx
->lds
, thread_id
);
4990 load_ptr0
= ac_build_gep0(&ctx
->ac
, ctx
->lds
, tl_tid
);
4991 load_ptr1
= ac_build_gep0(&ctx
->ac
, ctx
->lds
, trbl_tid
);
4993 LLVMBuildStore(gallivm
->builder
, val
, store_ptr
);
4994 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
4995 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
4998 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
4999 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, ctx
->f32
, "");
5001 emit_data
->output
[emit_data
->chan
] =
5002 LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
5006 * this takes an I,J coordinate pair,
5007 * and works out the X and Y derivatives.
5008 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
5010 static LLVMValueRef
si_llvm_emit_ddxy_interp(
5011 struct lp_build_tgsi_context
*bld_base
,
5012 LLVMValueRef interp_ij
)
5014 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5015 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5016 LLVMValueRef result
[4], a
;
5019 for (i
= 0; i
< 2; i
++) {
5020 a
= LLVMBuildExtractElement(gallivm
->builder
, interp_ij
,
5021 LLVMConstInt(ctx
->i32
, i
, 0), "");
5022 result
[i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDX
, a
);
5023 result
[2+i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDY
, a
);
5026 return lp_build_gather_values(gallivm
, result
, 4);
5029 static void interp_fetch_args(
5030 struct lp_build_tgsi_context
*bld_base
,
5031 struct lp_build_emit_data
*emit_data
)
5033 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5034 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5035 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
5037 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
5038 /* offset is in second src, first two channels */
5039 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
5042 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
5045 emit_data
->arg_count
= 2;
5046 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
5047 LLVMValueRef sample_position
;
5048 LLVMValueRef sample_id
;
5049 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
5051 /* fetch sample ID, then fetch its sample position,
5052 * and place into first two channels.
5054 sample_id
= lp_build_emit_fetch(bld_base
,
5055 emit_data
->inst
, 1, TGSI_CHAN_X
);
5056 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
5058 sample_position
= load_sample_position(ctx
, sample_id
);
5060 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
5062 lp_build_const_int32(gallivm
, 0), "");
5064 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
5065 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
5067 lp_build_const_int32(gallivm
, 1), "");
5068 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
5069 emit_data
->arg_count
= 2;
5073 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
5074 struct lp_build_tgsi_context
*bld_base
,
5075 struct lp_build_emit_data
*emit_data
)
5077 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5078 struct si_shader
*shader
= ctx
->shader
;
5079 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5080 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5081 LLVMValueRef interp_param
;
5082 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
5083 int input_index
= inst
->Src
[0].Register
.Index
;
5086 LLVMValueRef attr_number
;
5087 LLVMValueRef params
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_PRIM_MASK
);
5088 int interp_param_idx
;
5089 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
5092 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
5094 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5095 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
5096 location
= TGSI_INTERPOLATE_LOC_CENTER
;
5098 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
5100 interp_param_idx
= lookup_interp_param_index(interp
, location
);
5101 if (interp_param_idx
== -1)
5103 else if (interp_param_idx
)
5104 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
5106 interp_param
= NULL
;
5108 attr_number
= lp_build_const_int32(gallivm
, input_index
);
5110 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5111 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
5112 LLVMValueRef ij_out
[2];
5113 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
5116 * take the I then J parameters, and the DDX/Y for it, and
5117 * calculate the IJ inputs for the interpolator.
5118 * temp1 = ddx * offset/sample.x + I;
5119 * interp_param.I = ddy * offset/sample.y + temp1;
5120 * temp1 = ddx * offset/sample.x + J;
5121 * interp_param.J = ddy * offset/sample.y + temp1;
5123 for (i
= 0; i
< 2; i
++) {
5124 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
5125 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
5126 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
5127 ddxy_out
, ix_ll
, "");
5128 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
5129 ddxy_out
, iy_ll
, "");
5130 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
5131 interp_param
, ix_ll
, "");
5132 LLVMValueRef temp1
, temp2
;
5134 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
5137 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
5139 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
5141 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
5143 ij_out
[i
] = LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
5145 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
5148 for (chan
= 0; chan
< 4; chan
++) {
5149 LLVMValueRef llvm_chan
;
5152 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
5153 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
5156 interp_param
= LLVMBuildBitCast(gallivm
->builder
,
5157 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
5158 LLVMValueRef i
= LLVMBuildExtractElement(
5159 gallivm
->builder
, interp_param
, uint
->zero
, "");
5160 LLVMValueRef j
= LLVMBuildExtractElement(
5161 gallivm
->builder
, interp_param
, uint
->one
, "");
5162 emit_data
->output
[chan
] = ac_build_fs_interp(&ctx
->ac
,
5163 llvm_chan
, attr_number
, params
,
5166 emit_data
->output
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
5167 lp_build_const_int32(gallivm
, 2), /* P0 */
5168 llvm_chan
, attr_number
, params
);
5173 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
5174 struct lp_build_emit_data
*emit_data
)
5176 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5177 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
5181 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
5183 imm
= ctx
->imms
[src0
.Index
* TGSI_NUM_CHANNELS
+ src0
.SwizzleX
];
5184 stream
= LLVMConstIntGetZExtValue(imm
) & 0x3;
5188 /* Emit one vertex from the geometry shader */
5189 static void si_llvm_emit_vertex(
5190 const struct lp_build_tgsi_action
*action
,
5191 struct lp_build_tgsi_context
*bld_base
,
5192 struct lp_build_emit_data
*emit_data
)
5194 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5195 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5196 struct si_shader
*shader
= ctx
->shader
;
5197 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5198 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5199 struct lp_build_if_state if_state
;
5200 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
5201 SI_PARAM_GS2VS_OFFSET
);
5202 LLVMValueRef gs_next_vertex
;
5203 LLVMValueRef can_emit
, kill
;
5204 LLVMValueRef args
[2];
5205 unsigned chan
, offset
;
5209 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5211 /* Write vertex attribute values to GSVS ring */
5212 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
5213 ctx
->gs_next_vertex
[stream
],
5216 /* If this thread has already emitted the declared maximum number of
5217 * vertices, skip the write: excessive vertex emissions are not
5218 * supposed to have any effect.
5220 * If the shader has no writes to memory, kill it instead. This skips
5221 * further memory loads and may allow LLVM to skip to the end
5224 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULT
, gs_next_vertex
,
5225 lp_build_const_int32(gallivm
,
5226 shader
->selector
->gs_max_out_vertices
), "");
5228 bool use_kill
= !info
->writes_memory
;
5230 kill
= lp_build_select(&bld_base
->base
, can_emit
,
5231 lp_build_const_float(gallivm
, 1.0f
),
5232 lp_build_const_float(gallivm
, -1.0f
));
5234 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
5235 ctx
->voidt
, &kill
, 1, 0);
5237 lp_build_if(&if_state
, gallivm
, can_emit
);
5241 for (i
= 0; i
< info
->num_outputs
; i
++) {
5242 LLVMValueRef
*out_ptr
= ctx
->outputs
[i
];
5244 for (chan
= 0; chan
< 4; chan
++) {
5245 if (!(info
->output_usagemask
[i
] & (1 << chan
)) ||
5246 ((info
->output_streams
[i
] >> (2 * chan
)) & 3) != stream
)
5249 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
5250 LLVMValueRef voffset
=
5251 lp_build_const_int32(gallivm
, offset
*
5252 shader
->selector
->gs_max_out_vertices
);
5255 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
5256 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
5258 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
5260 build_tbuffer_store(ctx
,
5261 ctx
->gsvs_ring
[stream
],
5263 voffset
, soffset
, 0,
5264 V_008F0C_BUF_DATA_FORMAT_32
,
5265 V_008F0C_BUF_NUM_FORMAT_UINT
,
5270 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
5271 lp_build_const_int32(gallivm
, 1));
5273 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
5275 /* Signal vertex emission */
5276 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
5277 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
5278 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
5279 ctx
->voidt
, args
, 2, 0);
5282 lp_build_endif(&if_state
);
5285 /* Cut one primitive from the geometry shader */
5286 static void si_llvm_emit_primitive(
5287 const struct lp_build_tgsi_action
*action
,
5288 struct lp_build_tgsi_context
*bld_base
,
5289 struct lp_build_emit_data
*emit_data
)
5291 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5292 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5293 LLVMValueRef args
[2];
5296 /* Signal primitive cut */
5297 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5298 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
5299 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
5300 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
5301 ctx
->voidt
, args
, 2, 0);
5304 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
5305 struct lp_build_tgsi_context
*bld_base
,
5306 struct lp_build_emit_data
*emit_data
)
5308 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5309 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5311 /* SI only (thanks to a hw bug workaround):
5312 * The real barrier instruction isn’t needed, because an entire patch
5313 * always fits into a single wave.
5315 if (HAVE_LLVM
>= 0x0309 &&
5316 ctx
->screen
->b
.chip_class
== SI
&&
5317 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
5318 emit_waitcnt(ctx
, LGKM_CNT
& VM_CNT
);
5322 lp_build_intrinsic(gallivm
->builder
,
5323 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
5324 : "llvm.AMDGPU.barrier.local",
5325 ctx
->voidt
, NULL
, 0, 0);
5328 static const struct lp_build_tgsi_action tex_action
= {
5329 .fetch_args
= tex_fetch_args
,
5330 .emit
= build_tex_intrinsic
,
5333 static const struct lp_build_tgsi_action interp_action
= {
5334 .fetch_args
= interp_fetch_args
,
5335 .emit
= build_interp_intrinsic
,
5338 static void si_create_function(struct si_shader_context
*ctx
,
5340 LLVMTypeRef
*returns
, unsigned num_returns
,
5341 LLVMTypeRef
*params
, unsigned num_params
,
5346 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
5347 params
, num_params
);
5348 si_llvm_shader_type(ctx
->main_fn
, ctx
->type
);
5349 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
5351 for (i
= 0; i
<= last_sgpr
; ++i
) {
5352 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
5354 /* The combination of:
5358 * allows the optimization passes to move loads and reduces
5359 * SGPR spilling significantly.
5361 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
5362 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_BYVAL
);
5363 lp_add_attr_dereferenceable(P
, UINT64_MAX
);
5365 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_INREG
);
5368 if (ctx
->screen
->b
.debug_flags
& DBG_UNSAFE_MATH
) {
5369 /* These were copied from some LLVM test. */
5370 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5371 "less-precise-fpmad",
5373 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5376 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5379 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5385 static void create_meta_data(struct si_shader_context
*ctx
)
5387 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
5389 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(gallivm
->context
,
5393 static void declare_streamout_params(struct si_shader_context
*ctx
,
5394 struct pipe_stream_output_info
*so
,
5395 LLVMTypeRef
*params
, LLVMTypeRef i32
,
5396 unsigned *num_params
)
5400 /* Streamout SGPRs. */
5401 if (so
->num_outputs
) {
5402 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
5403 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
5405 ctx
->param_streamout_config
= *num_params
- 1;
5407 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
5409 /* A streamout buffer offset is loaded if the stride is non-zero. */
5410 for (i
= 0; i
< 4; i
++) {
5414 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
5418 static unsigned llvm_get_type_size(LLVMTypeRef type
)
5420 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
5423 case LLVMIntegerTypeKind
:
5424 return LLVMGetIntTypeWidth(type
) / 8;
5425 case LLVMFloatTypeKind
:
5427 case LLVMPointerTypeKind
:
5429 case LLVMVectorTypeKind
:
5430 return LLVMGetVectorSize(type
) *
5431 llvm_get_type_size(LLVMGetElementType(type
));
5432 case LLVMArrayTypeKind
:
5433 return LLVMGetArrayLength(type
) *
5434 llvm_get_type_size(LLVMGetElementType(type
));
5441 static void declare_tess_lds(struct si_shader_context
*ctx
)
5443 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5444 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5445 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5447 unsigned lds_size
= ctx
->screen
->b
.chip_class
>= CIK
? 65536 : 32768;
5448 ctx
->lds
= LLVMBuildIntToPtr(gallivm
->builder
, uint
->zero
,
5449 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
5453 static unsigned si_get_max_workgroup_size(struct si_shader
*shader
)
5455 const unsigned *properties
= shader
->selector
->info
.properties
;
5456 unsigned max_work_group_size
=
5457 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
5458 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
5459 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
5461 if (!max_work_group_size
) {
5462 /* This is a variable group size compute shader,
5463 * compile it for the maximum possible group size.
5465 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
5467 return max_work_group_size
;
5470 static void create_function(struct si_shader_context
*ctx
)
5472 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5473 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5474 struct si_shader
*shader
= ctx
->shader
;
5475 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_NUM_VERTEX_BUFFERS
], v3i32
;
5476 LLVMTypeRef returns
[16+32*4];
5477 unsigned i
, last_sgpr
, num_params
, num_return_sgprs
;
5478 unsigned num_returns
= 0;
5479 unsigned num_prolog_vgprs
= 0;
5481 v3i32
= LLVMVectorType(ctx
->i32
, 3);
5483 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
5484 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
5485 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
5486 params
[SI_PARAM_IMAGES
] = const_array(ctx
->v8i32
, SI_NUM_IMAGES
);
5487 params
[SI_PARAM_SHADER_BUFFERS
] = const_array(ctx
->v4i32
, SI_NUM_SHADER_BUFFERS
);
5489 switch (ctx
->type
) {
5490 case PIPE_SHADER_VERTEX
:
5491 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_VERTEX_BUFFERS
);
5492 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
5493 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
5494 params
[SI_PARAM_DRAWID
] = ctx
->i32
;
5495 num_params
= SI_PARAM_DRAWID
+1;
5497 if (shader
->key
.as_es
) {
5498 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5499 } else if (shader
->key
.as_ls
) {
5500 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
5501 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
5503 if (shader
->is_gs_copy_shader
) {
5504 num_params
= SI_PARAM_RW_BUFFERS
+1;
5506 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
5507 num_params
= SI_PARAM_VS_STATE_BITS
+1;
5510 /* The locations of the other parameters are assigned dynamically. */
5511 declare_streamout_params(ctx
, &shader
->selector
->so
,
5512 params
, ctx
->i32
, &num_params
);
5515 last_sgpr
= num_params
-1;
5518 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
5519 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
5520 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
5521 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
5523 if (!shader
->is_gs_copy_shader
) {
5524 /* Vertex load indices. */
5525 ctx
->param_vertex_index0
= num_params
;
5527 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
5528 params
[num_params
++] = ctx
->i32
;
5530 num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
5532 /* PrimitiveID output. */
5533 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
)
5534 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5535 returns
[num_returns
++] = ctx
->f32
;
5539 case PIPE_SHADER_TESS_CTRL
:
5540 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5541 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
5542 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
5543 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
5544 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
5545 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
5546 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5549 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
5550 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
5551 num_params
= SI_PARAM_REL_IDS
+1;
5553 /* SI_PARAM_TCS_OC_LDS and PARAM_TESS_FACTOR_OFFSET are
5554 * placed after the user SGPRs.
5556 for (i
= 0; i
< SI_TCS_NUM_USER_SGPR
+ 2; i
++)
5557 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
5559 for (i
= 0; i
< 3; i
++)
5560 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
5563 case PIPE_SHADER_TESS_EVAL
:
5564 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5565 num_params
= SI_PARAM_TCS_OFFCHIP_LAYOUT
+1;
5567 if (shader
->key
.as_es
) {
5568 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5569 params
[num_params
++] = ctx
->i32
;
5570 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5572 params
[num_params
++] = ctx
->i32
;
5573 declare_streamout_params(ctx
, &shader
->selector
->so
,
5574 params
, ctx
->i32
, &num_params
);
5575 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5577 last_sgpr
= num_params
- 1;
5580 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
5581 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
5582 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
5583 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
5585 /* PrimitiveID output. */
5586 if (!shader
->key
.as_es
)
5587 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5588 returns
[num_returns
++] = ctx
->f32
;
5591 case PIPE_SHADER_GEOMETRY
:
5592 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
5593 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
5594 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
5597 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
5598 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
5599 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
5600 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
5601 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
5602 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
5603 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
5604 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
5605 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
5608 case PIPE_SHADER_FRAGMENT
:
5609 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
5610 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
5611 last_sgpr
= SI_PARAM_PRIM_MASK
;
5612 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
5613 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
5614 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
5615 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
5616 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
5617 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
5618 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
5619 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
5620 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
5621 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
5622 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
5623 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
5624 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
5625 shader
->info
.face_vgpr_index
= 20;
5626 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
5627 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
5628 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
5629 num_params
= SI_PARAM_POS_FIXED_PT
+1;
5631 /* Color inputs from the prolog. */
5632 if (shader
->selector
->info
.colors_read
) {
5633 unsigned num_color_elements
=
5634 util_bitcount(shader
->selector
->info
.colors_read
);
5636 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
5637 for (i
= 0; i
< num_color_elements
; i
++)
5638 params
[num_params
++] = ctx
->f32
;
5640 num_prolog_vgprs
+= num_color_elements
;
5643 /* Outputs for the epilog. */
5644 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
5647 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
5648 shader
->selector
->info
.writes_z
+
5649 shader
->selector
->info
.writes_stencil
+
5650 shader
->selector
->info
.writes_samplemask
+
5651 1 /* SampleMaskIn */;
5653 num_returns
= MAX2(num_returns
,
5655 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5657 for (i
= 0; i
< num_return_sgprs
; i
++)
5658 returns
[i
] = ctx
->i32
;
5659 for (; i
< num_returns
; i
++)
5660 returns
[i
] = ctx
->f32
;
5663 case PIPE_SHADER_COMPUTE
:
5664 params
[SI_PARAM_GRID_SIZE
] = v3i32
;
5665 params
[SI_PARAM_BLOCK_SIZE
] = v3i32
;
5666 params
[SI_PARAM_BLOCK_ID
] = v3i32
;
5667 last_sgpr
= SI_PARAM_BLOCK_ID
;
5669 params
[SI_PARAM_THREAD_ID
] = v3i32
;
5670 num_params
= SI_PARAM_THREAD_ID
+ 1;
5673 assert(0 && "unimplemented shader");
5677 assert(num_params
<= ARRAY_SIZE(params
));
5679 si_create_function(ctx
, "main", returns
, num_returns
, params
,
5680 num_params
, last_sgpr
);
5682 /* Reserve register locations for VGPR inputs the PS prolog may need. */
5683 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&&
5684 ctx
->separate_prolog
) {
5685 si_llvm_add_attribute(ctx
->main_fn
,
5686 "InitialPSInputAddr",
5687 S_0286D0_PERSP_SAMPLE_ENA(1) |
5688 S_0286D0_PERSP_CENTER_ENA(1) |
5689 S_0286D0_PERSP_CENTROID_ENA(1) |
5690 S_0286D0_LINEAR_SAMPLE_ENA(1) |
5691 S_0286D0_LINEAR_CENTER_ENA(1) |
5692 S_0286D0_LINEAR_CENTROID_ENA(1) |
5693 S_0286D0_FRONT_FACE_ENA(1) |
5694 S_0286D0_POS_FIXED_PT_ENA(1));
5695 } else if (ctx
->type
== PIPE_SHADER_COMPUTE
) {
5696 si_llvm_add_attribute(ctx
->main_fn
,
5697 "amdgpu-max-work-group-size",
5698 si_get_max_workgroup_size(shader
));
5701 shader
->info
.num_input_sgprs
= 0;
5702 shader
->info
.num_input_vgprs
= 0;
5704 for (i
= 0; i
<= last_sgpr
; ++i
)
5705 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
5707 for (; i
< num_params
; ++i
)
5708 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
5710 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5711 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5713 if (!ctx
->screen
->has_ds_bpermute
&&
5715 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
5716 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
5717 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
5718 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
5719 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
5720 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
5722 LLVMAddGlobalInAddressSpace(gallivm
->module
,
5723 LLVMArrayType(ctx
->i32
, 64),
5727 if ((ctx
->type
== PIPE_SHADER_VERTEX
&& shader
->key
.as_ls
) ||
5728 ctx
->type
== PIPE_SHADER_TESS_CTRL
)
5729 declare_tess_lds(ctx
);
5733 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5736 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5738 struct gallivm_state
*gallivm
= ctx
->bld_base
.base
.gallivm
;
5739 LLVMBuilderRef builder
= gallivm
->builder
;
5741 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5742 SI_PARAM_RW_BUFFERS
);
5744 if ((ctx
->type
== PIPE_SHADER_VERTEX
&&
5745 ctx
->shader
->key
.as_es
) ||
5746 (ctx
->type
== PIPE_SHADER_TESS_EVAL
&&
5747 ctx
->shader
->key
.as_es
) ||
5748 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5750 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5752 LLVMValueRef offset
= lp_build_const_int32(gallivm
, ring
);
5755 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5758 if (ctx
->shader
->is_gs_copy_shader
) {
5759 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
5762 ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5763 } else if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5764 const struct si_shader_selector
*sel
= ctx
->shader
->selector
;
5765 struct lp_build_context
*uint
= &ctx
->bld_base
.uint_bld
;
5766 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
5767 LLVMValueRef base_ring
;
5769 base_ring
= ac_build_indexed_load_const(&ctx
->ac
, buf_ptr
, offset
);
5771 /* The conceptual layout of the GSVS ring is
5772 * v0c0 .. vLv0 v0c1 .. vLc1 ..
5773 * but the real memory layout is swizzled across
5775 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
5777 * Override the buffer descriptor accordingly.
5779 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->i64
, 2);
5780 uint64_t stream_offset
= 0;
5782 for (unsigned stream
= 0; stream
< 4; ++stream
) {
5783 unsigned num_components
;
5785 unsigned num_records
;
5786 LLVMValueRef ring
, tmp
;
5788 num_components
= sel
->info
.num_stream_output_components
[stream
];
5789 if (!num_components
)
5792 stride
= 4 * num_components
* sel
->gs_max_out_vertices
;
5794 /* Limit on the stride field for <= CIK. */
5795 assert(stride
< (1 << 14));
5799 ring
= LLVMBuildBitCast(builder
, base_ring
, v2i64
, "");
5800 tmp
= LLVMBuildExtractElement(builder
, ring
, uint
->zero
, "");
5801 tmp
= LLVMBuildAdd(builder
, tmp
,
5802 LLVMConstInt(ctx
->i64
,
5803 stream_offset
, 0), "");
5804 stream_offset
+= stride
* 64;
5806 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, uint
->zero
, "");
5807 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v4i32
, "");
5808 tmp
= LLVMBuildExtractElement(builder
, ring
, uint
->one
, "");
5809 tmp
= LLVMBuildOr(builder
, tmp
,
5810 LLVMConstInt(ctx
->i32
,
5811 S_008F04_STRIDE(stride
) |
5812 S_008F04_SWIZZLE_ENABLE(1), 0), "");
5813 ring
= LLVMBuildInsertElement(builder
, ring
, tmp
, uint
->one
, "");
5814 ring
= LLVMBuildInsertElement(builder
, ring
,
5815 LLVMConstInt(ctx
->i32
, num_records
, 0),
5816 LLVMConstInt(ctx
->i32
, 2, 0), "");
5817 ring
= LLVMBuildInsertElement(builder
, ring
,
5818 LLVMConstInt(ctx
->i32
,
5819 S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
5820 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
5821 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
5822 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
5823 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
5824 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
5825 S_008F0C_ELEMENT_SIZE(1) | /* element_size = 4 (bytes) */
5826 S_008F0C_INDEX_STRIDE(1) | /* index_stride = 16 (elements) */
5827 S_008F0C_ADD_TID_ENABLE(1),
5829 LLVMConstInt(ctx
->i32
, 3, 0), "");
5830 ring
= LLVMBuildBitCast(builder
, ring
, ctx
->v16i8
, "");
5832 ctx
->gsvs_ring
[stream
] = ring
;
5837 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5838 LLVMValueRef param_rw_buffers
,
5839 unsigned param_pos_fixed_pt
)
5841 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
5842 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5843 LLVMBuilderRef builder
= gallivm
->builder
;
5844 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5846 /* Use the fixed-point gl_FragCoord input.
5847 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5848 * per coordinate to get the repeating effect.
5850 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5851 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5853 /* Load the buffer descriptor. */
5854 slot
= lp_build_const_int32(gallivm
, SI_PS_CONST_POLY_STIPPLE
);
5855 desc
= ac_build_indexed_load_const(&ctx
->ac
, param_rw_buffers
, slot
);
5857 /* The stipple pattern is 32x32, each row has 32 bits. */
5858 offset
= LLVMBuildMul(builder
, address
[1],
5859 LLVMConstInt(ctx
->i32
, 4, 0), "");
5860 row
= buffer_load_const(ctx
, desc
, offset
);
5861 row
= LLVMBuildBitCast(builder
, row
, ctx
->i32
, "");
5862 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5863 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5865 /* The intrinsic kills the thread if arg < 0. */
5866 bit
= LLVMBuildSelect(builder
, bit
, LLVMConstReal(ctx
->f32
, 0),
5867 LLVMConstReal(ctx
->f32
, -1), "");
5868 lp_build_intrinsic(builder
, "llvm.AMDGPU.kill", ctx
->voidt
, &bit
, 1, 0);
5871 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
5872 struct si_shader_config
*conf
,
5873 unsigned symbol_offset
)
5876 const unsigned char *config
=
5877 radeon_shader_binary_config_start(binary
, symbol_offset
);
5878 bool really_needs_scratch
= false;
5880 /* LLVM adds SGPR spills to the scratch size.
5881 * Find out if we really need the scratch buffer.
5883 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5884 const struct radeon_shader_reloc
*reloc
= &binary
->relocs
[i
];
5886 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5887 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5888 really_needs_scratch
= true;
5893 /* XXX: We may be able to emit some of these values directly rather than
5894 * extracting fields to be emitted later.
5897 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5898 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5899 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5901 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5902 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5903 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5904 case R_00B848_COMPUTE_PGM_RSRC1
:
5905 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5906 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5907 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5908 conf
->rsrc1
= value
;
5910 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5911 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5913 case R_00B84C_COMPUTE_PGM_RSRC2
:
5914 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5915 conf
->rsrc2
= value
;
5917 case R_0286CC_SPI_PS_INPUT_ENA
:
5918 conf
->spi_ps_input_ena
= value
;
5920 case R_0286D0_SPI_PS_INPUT_ADDR
:
5921 conf
->spi_ps_input_addr
= value
;
5923 case R_0286E8_SPI_TMPRING_SIZE
:
5924 case R_00B860_COMPUTE_TMPRING_SIZE
:
5925 /* WAVESIZE is in units of 256 dwords. */
5926 if (really_needs_scratch
)
5927 conf
->scratch_bytes_per_wave
=
5928 G_00B860_WAVESIZE(value
) * 256 * 4;
5930 case 0x4: /* SPILLED_SGPRS */
5931 conf
->spilled_sgprs
= value
;
5933 case 0x8: /* SPILLED_VGPRS */
5934 conf
->spilled_vgprs
= value
;
5938 static bool printed
;
5941 fprintf(stderr
, "Warning: LLVM emitted unknown "
5942 "config register: 0x%x\n", reg
);
5950 if (!conf
->spi_ps_input_addr
)
5951 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5954 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
5955 struct si_shader
*shader
,
5956 struct si_shader_config
*config
,
5957 uint64_t scratch_va
)
5960 uint32_t scratch_rsrc_dword0
= scratch_va
;
5961 uint32_t scratch_rsrc_dword1
=
5962 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5964 /* Enable scratch coalescing if LLVM sets ELEMENT_SIZE & INDEX_STRIDE
5967 if (HAVE_LLVM
>= 0x0309)
5968 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5970 scratch_rsrc_dword1
|=
5971 S_008F04_STRIDE(config
->scratch_bytes_per_wave
/ 64);
5973 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5974 const struct radeon_shader_reloc
*reloc
=
5975 &shader
->binary
.relocs
[i
];
5976 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5977 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5978 &scratch_rsrc_dword0
, 4);
5979 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5980 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5981 &scratch_rsrc_dword1
, 4);
5986 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
5988 unsigned size
= shader
->binary
.code_size
;
5991 size
+= shader
->prolog
->binary
.code_size
;
5993 size
+= shader
->epilog
->binary
.code_size
;
5997 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5999 const struct radeon_shader_binary
*prolog
=
6000 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
6001 const struct radeon_shader_binary
*epilog
=
6002 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
6003 const struct radeon_shader_binary
*mainb
= &shader
->binary
;
6004 unsigned bo_size
= si_get_shader_binary_size(shader
) +
6005 (!epilog
? mainb
->rodata_size
: 0);
6008 assert(!prolog
|| !prolog
->rodata_size
);
6009 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
6010 assert(!epilog
|| !epilog
->rodata_size
);
6012 r600_resource_reference(&shader
->bo
, NULL
);
6013 shader
->bo
= (struct r600_resource
*)
6014 pipe_buffer_create(&sscreen
->b
.b
, 0,
6015 PIPE_USAGE_IMMUTABLE
, bo_size
);
6020 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
6021 PIPE_TRANSFER_READ_WRITE
);
6024 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
6025 ptr
+= prolog
->code_size
;
6028 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
6029 ptr
+= mainb
->code_size
;
6032 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
6033 else if (mainb
->rodata_size
> 0)
6034 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
6036 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
6040 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
6041 struct pipe_debug_callback
*debug
,
6042 const char *name
, FILE *file
)
6047 if (binary
->disasm_string
) {
6048 fprintf(file
, "Shader %s disassembly:\n", name
);
6049 fprintf(file
, "%s", binary
->disasm_string
);
6051 if (debug
&& debug
->debug_message
) {
6052 /* Very long debug messages are cut off, so send the
6053 * disassembly one line at a time. This causes more
6054 * overhead, but on the plus side it simplifies
6055 * parsing of resulting logs.
6057 pipe_debug_message(debug
, SHADER_INFO
,
6058 "Shader Disassembly Begin");
6060 line
= binary
->disasm_string
;
6062 p
= util_strchrnul(line
, '\n');
6066 pipe_debug_message(debug
, SHADER_INFO
,
6067 "%.*s", count
, line
);
6075 pipe_debug_message(debug
, SHADER_INFO
,
6076 "Shader Disassembly End");
6079 fprintf(file
, "Shader %s binary:\n", name
);
6080 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
6081 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
6082 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
6083 binary
->code
[i
+ 1], binary
->code
[i
]);
6088 static void si_shader_dump_stats(struct si_screen
*sscreen
,
6089 struct si_shader
*shader
,
6090 struct pipe_debug_callback
*debug
,
6093 bool check_debug_option
)
6095 struct si_shader_config
*conf
= &shader
->config
;
6096 unsigned num_inputs
= shader
->selector
? shader
->selector
->info
.num_inputs
: 0;
6097 unsigned code_size
= si_get_shader_binary_size(shader
);
6098 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
6099 unsigned lds_per_wave
= 0;
6100 unsigned max_simd_waves
= 10;
6102 /* Compute LDS usage for PS. */
6103 switch (processor
) {
6104 case PIPE_SHADER_FRAGMENT
:
6105 /* The minimum usage per wave is (num_inputs * 48). The maximum
6106 * usage is (num_inputs * 48 * 16).
6107 * We can get anything in between and it varies between waves.
6109 * The 48 bytes per input for a single primitive is equal to
6110 * 4 bytes/component * 4 components/input * 3 points.
6112 * Other stages don't know the size at compile time or don't
6113 * allocate LDS per wave, but instead they do it per thread group.
6115 lds_per_wave
= conf
->lds_size
* lds_increment
+
6116 align(num_inputs
* 48, lds_increment
);
6118 case PIPE_SHADER_COMPUTE
:
6119 if (shader
->selector
) {
6120 unsigned max_workgroup_size
=
6121 si_get_max_workgroup_size(shader
);
6122 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
6123 DIV_ROUND_UP(max_workgroup_size
, 64);
6128 /* Compute the per-SIMD wave counts. */
6129 if (conf
->num_sgprs
) {
6130 if (sscreen
->b
.chip_class
>= VI
)
6131 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
6133 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
6136 if (conf
->num_vgprs
)
6137 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
6139 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
6140 * 16KB makes some SIMDs unoccupied). */
6142 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
6144 if (!check_debug_option
||
6145 r600_can_dump_shader(&sscreen
->b
, processor
)) {
6146 if (processor
== PIPE_SHADER_FRAGMENT
) {
6147 fprintf(file
, "*** SHADER CONFIG ***\n"
6148 "SPI_PS_INPUT_ADDR = 0x%04x\n"
6149 "SPI_PS_INPUT_ENA = 0x%04x\n",
6150 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
6153 fprintf(file
, "*** SHADER STATS ***\n"
6156 "Spilled SGPRs: %d\n"
6157 "Spilled VGPRs: %d\n"
6158 "Private memory VGPRs: %d\n"
6159 "Code Size: %d bytes\n"
6161 "Scratch: %d bytes per wave\n"
6163 "********************\n\n\n",
6164 conf
->num_sgprs
, conf
->num_vgprs
,
6165 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
6166 conf
->private_mem_vgprs
, code_size
,
6167 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6171 pipe_debug_message(debug
, SHADER_INFO
,
6172 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
6173 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
6174 "Spilled VGPRs: %d PrivMem VGPRs: %d",
6175 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
6176 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6177 max_simd_waves
, conf
->spilled_sgprs
,
6178 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
6181 static const char *si_get_shader_name(struct si_shader
*shader
,
6184 switch (processor
) {
6185 case PIPE_SHADER_VERTEX
:
6186 if (shader
->key
.as_es
)
6187 return "Vertex Shader as ES";
6188 else if (shader
->key
.as_ls
)
6189 return "Vertex Shader as LS";
6191 return "Vertex Shader as VS";
6192 case PIPE_SHADER_TESS_CTRL
:
6193 return "Tessellation Control Shader";
6194 case PIPE_SHADER_TESS_EVAL
:
6195 if (shader
->key
.as_es
)
6196 return "Tessellation Evaluation Shader as ES";
6198 return "Tessellation Evaluation Shader as VS";
6199 case PIPE_SHADER_GEOMETRY
:
6200 if (shader
->is_gs_copy_shader
)
6201 return "GS Copy Shader as VS";
6203 return "Geometry Shader";
6204 case PIPE_SHADER_FRAGMENT
:
6205 return "Pixel Shader";
6206 case PIPE_SHADER_COMPUTE
:
6207 return "Compute Shader";
6209 return "Unknown Shader";
6213 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
6214 struct pipe_debug_callback
*debug
, unsigned processor
,
6215 FILE *file
, bool check_debug_option
)
6217 if (!check_debug_option
||
6218 r600_can_dump_shader(&sscreen
->b
, processor
))
6219 si_dump_shader_key(processor
, &shader
->key
, file
);
6221 if (!check_debug_option
&& shader
->binary
.llvm_ir_string
) {
6222 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
6223 si_get_shader_name(shader
, processor
));
6224 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
6227 if (!check_debug_option
||
6228 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
6229 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
6230 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
6233 si_shader_dump_disassembly(&shader
->prolog
->binary
,
6234 debug
, "prolog", file
);
6236 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
6239 si_shader_dump_disassembly(&shader
->epilog
->binary
,
6240 debug
, "epilog", file
);
6241 fprintf(file
, "\n");
6244 si_shader_dump_stats(sscreen
, shader
, debug
, processor
, file
,
6245 check_debug_option
);
6248 int si_compile_llvm(struct si_screen
*sscreen
,
6249 struct radeon_shader_binary
*binary
,
6250 struct si_shader_config
*conf
,
6251 LLVMTargetMachineRef tm
,
6253 struct pipe_debug_callback
*debug
,
6258 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
6260 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
6261 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
6263 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
6264 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
6265 ac_dump_module(mod
);
6266 fprintf(stderr
, "\n");
6270 if (sscreen
->record_llvm_ir
) {
6271 char *ir
= LLVMPrintModuleToString(mod
);
6272 binary
->llvm_ir_string
= strdup(ir
);
6273 LLVMDisposeMessage(ir
);
6276 if (!si_replace_shader(count
, binary
)) {
6277 r
= si_llvm_compile(mod
, binary
, tm
, debug
);
6282 si_shader_binary_read_config(binary
, conf
, 0);
6284 /* Enable 64-bit and 16-bit denormals, because there is no performance
6287 * If denormals are enabled, all floating-point output modifiers are
6290 * Don't enable denormals for 32-bit floats, because:
6291 * - Floating-point output modifiers would be ignored by the hw.
6292 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6293 * have to stop using those.
6294 * - SI & CI would be very slow.
6296 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
6298 FREE(binary
->config
);
6299 FREE(binary
->global_symbol_offsets
);
6300 binary
->config
= NULL
;
6301 binary
->global_symbol_offsets
= NULL
;
6303 /* Some shaders can't have rodata because their binaries can be
6306 if (binary
->rodata_size
&&
6307 (processor
== PIPE_SHADER_VERTEX
||
6308 processor
== PIPE_SHADER_TESS_CTRL
||
6309 processor
== PIPE_SHADER_TESS_EVAL
||
6310 processor
== PIPE_SHADER_FRAGMENT
)) {
6311 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
6318 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
6320 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
6321 LLVMBuildRetVoid(ctx
->gallivm
.builder
);
6323 LLVMBuildRet(ctx
->gallivm
.builder
, ret
);
6326 /* Generate code for the hardware VS shader stage to go with a geometry shader */
6328 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
6329 LLVMTargetMachineRef tm
,
6330 struct si_shader_selector
*gs_selector
,
6331 struct pipe_debug_callback
*debug
)
6333 struct si_shader_context ctx
;
6334 struct si_shader
*shader
;
6335 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
6336 LLVMBuilderRef builder
;
6337 struct lp_build_tgsi_context
*bld_base
= &ctx
.bld_base
;
6338 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
6339 struct si_shader_output_values
*outputs
;
6340 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
6341 LLVMValueRef args
[9];
6344 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
6349 shader
= CALLOC_STRUCT(si_shader
);
6356 shader
->selector
= gs_selector
;
6357 shader
->is_gs_copy_shader
= true;
6359 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
6360 ctx
.type
= PIPE_SHADER_VERTEX
;
6362 builder
= gallivm
->builder
;
6364 create_meta_data(&ctx
);
6365 create_function(&ctx
);
6366 preload_ring_buffers(&ctx
);
6368 args
[0] = ctx
.gsvs_ring
[0];
6369 args
[1] = lp_build_mul_imm(uint
,
6370 LLVMGetParam(ctx
.main_fn
,
6371 ctx
.param_vertex_id
),
6373 args
[3] = uint
->zero
;
6374 args
[4] = uint
->one
; /* OFFEN */
6375 args
[5] = uint
->zero
; /* IDXEN */
6376 args
[6] = uint
->one
; /* GLC */
6377 args
[7] = uint
->one
; /* SLC */
6378 args
[8] = uint
->zero
; /* TFE */
6380 /* Fetch the vertex stream ID.*/
6381 LLVMValueRef stream_id
;
6383 if (gs_selector
->so
.num_outputs
)
6384 stream_id
= unpack_param(&ctx
, ctx
.param_streamout_config
, 24, 2);
6386 stream_id
= uint
->zero
;
6388 /* Fill in output information. */
6389 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
6390 outputs
[i
].semantic_name
= gsinfo
->output_semantic_name
[i
];
6391 outputs
[i
].semantic_index
= gsinfo
->output_semantic_index
[i
];
6393 for (int chan
= 0; chan
< 4; chan
++) {
6394 outputs
[i
].vertex_stream
[chan
] =
6395 (gsinfo
->output_streams
[i
] >> (2 * chan
)) & 3;
6399 LLVMBasicBlockRef end_bb
;
6400 LLVMValueRef switch_inst
;
6402 end_bb
= LLVMAppendBasicBlockInContext(gallivm
->context
, ctx
.main_fn
, "end");
6403 switch_inst
= LLVMBuildSwitch(builder
, stream_id
, end_bb
, 4);
6405 for (int stream
= 0; stream
< 4; stream
++) {
6406 LLVMBasicBlockRef bb
;
6409 if (!gsinfo
->num_stream_output_components
[stream
])
6412 if (stream
> 0 && !gs_selector
->so
.num_outputs
)
6415 bb
= LLVMInsertBasicBlockInContext(gallivm
->context
, end_bb
, "out");
6416 LLVMAddCase(switch_inst
, lp_build_const_int32(gallivm
, stream
), bb
);
6417 LLVMPositionBuilderAtEnd(builder
, bb
);
6419 /* Fetch vertex data from GSVS ring */
6421 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
6422 for (unsigned chan
= 0; chan
< 4; chan
++) {
6423 if (!(gsinfo
->output_usagemask
[i
] & (1 << chan
)) ||
6424 outputs
[i
].vertex_stream
[chan
] != stream
) {
6425 outputs
[i
].values
[chan
] = ctx
.bld_base
.base
.undef
;
6429 args
[2] = lp_build_const_int32(
6431 offset
* gs_selector
->gs_max_out_vertices
* 16 * 4);
6434 outputs
[i
].values
[chan
] =
6435 LLVMBuildBitCast(gallivm
->builder
,
6436 lp_build_intrinsic(gallivm
->builder
,
6437 "llvm.SI.buffer.load.dword.i32.i32",
6439 LP_FUNC_ATTR_READONLY
),
6444 /* Streamout and exports. */
6445 if (gs_selector
->so
.num_outputs
) {
6446 si_llvm_emit_streamout(&ctx
, outputs
,
6447 gsinfo
->num_outputs
,
6452 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
6454 LLVMBuildBr(builder
, end_bb
);
6457 LLVMPositionBuilderAtEnd(builder
, end_bb
);
6459 LLVMBuildRetVoid(gallivm
->builder
);
6461 /* Dump LLVM IR before any optimization passes */
6462 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
6463 r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6464 ac_dump_module(bld_base
->base
.gallivm
->module
);
6466 si_llvm_finalize_module(&ctx
,
6467 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_GEOMETRY
));
6469 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
6470 &ctx
.shader
->config
, ctx
.tm
,
6471 bld_base
->base
.gallivm
->module
,
6472 debug
, PIPE_SHADER_GEOMETRY
,
6475 if (r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6476 fprintf(stderr
, "GS Copy Shader:\n");
6477 si_shader_dump(sscreen
, ctx
.shader
, debug
,
6478 PIPE_SHADER_GEOMETRY
, stderr
, true);
6479 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
6482 si_llvm_dispose(&ctx
);
6493 static void si_dump_shader_key(unsigned shader
, struct si_shader_key
*key
,
6498 fprintf(f
, "SHADER KEY\n");
6501 case PIPE_SHADER_VERTEX
:
6502 fprintf(f
, " part.vs.prolog.instance_divisors = {");
6503 for (i
= 0; i
< ARRAY_SIZE(key
->part
.vs
.prolog
.instance_divisors
); i
++)
6504 fprintf(f
, !i
? "%u" : ", %u",
6505 key
->part
.vs
.prolog
.instance_divisors
[i
]);
6507 fprintf(f
, " part.vs.epilog.export_prim_id = %u\n", key
->part
.vs
.epilog
.export_prim_id
);
6508 fprintf(f
, " as_es = %u\n", key
->as_es
);
6509 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
6510 fprintf(f
, " mono.vs.fix_fetch = 0x%"PRIx64
"\n", key
->mono
.vs
.fix_fetch
);
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.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
6543 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
6544 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
6545 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
6546 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
6553 if ((shader
== PIPE_SHADER_GEOMETRY
||
6554 shader
== PIPE_SHADER_TESS_EVAL
||
6555 shader
== PIPE_SHADER_VERTEX
) &&
6556 !key
->as_es
&& !key
->as_ls
) {
6557 fprintf(f
, " opt.hw_vs.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.hw_vs
.kill_outputs
);
6558 fprintf(f
, " opt.hw_vs.kill_outputs2 = 0x%x\n", key
->opt
.hw_vs
.kill_outputs2
);
6559 fprintf(f
, " opt.hw_vs.clip_disable = %u\n", key
->opt
.hw_vs
.clip_disable
);
6563 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
6564 struct si_screen
*sscreen
,
6565 struct si_shader
*shader
,
6566 LLVMTargetMachineRef tm
)
6568 struct lp_build_tgsi_context
*bld_base
;
6569 struct lp_build_tgsi_action tmpl
= {};
6571 si_llvm_context_init(ctx
, sscreen
, shader
, tm
,
6572 (shader
&& shader
->selector
) ? &shader
->selector
->info
: NULL
,
6573 (shader
&& shader
->selector
) ? shader
->selector
->tokens
: NULL
);
6575 bld_base
= &ctx
->bld_base
;
6576 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
6578 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
6579 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
6580 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
6582 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
6583 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
6584 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
6585 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
6586 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
6587 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
6588 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
6589 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
6590 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
6591 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= txq_fetch_args
;
6592 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
6593 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
6594 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
6595 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
6597 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
6598 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
6599 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
6600 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
6601 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
6602 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
6604 tmpl
.fetch_args
= atomic_fetch_args
;
6605 tmpl
.emit
= atomic_emit
;
6606 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
6607 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
6608 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
6609 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
6610 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
6611 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
6612 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
6613 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
6614 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
6615 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
6616 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
6617 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
6618 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
6619 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
6620 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
6621 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
6622 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
6623 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
6624 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
6625 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
6627 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
6629 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
6630 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
6631 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
6632 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
6634 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
6635 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
6636 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
6639 /* Return true if the PARAM export has been eliminated. */
6640 static bool si_eliminate_const_output(struct si_shader_context
*ctx
,
6641 LLVMValueRef inst
, unsigned offset
)
6643 struct si_shader
*shader
= ctx
->shader
;
6644 unsigned num_outputs
= shader
->selector
->info
.num_outputs
;
6645 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
6646 bool is_zero
[4] = {}, is_one
[4] = {};
6648 for (i
= 0; i
< 4; i
++) {
6649 LLVMBool loses_info
;
6650 LLVMValueRef p
= LLVMGetOperand(inst
, 5 + i
);
6652 /* It's a constant expression. Undef outputs are eliminated too. */
6653 if (LLVMIsUndef(p
)) {
6656 } else if (LLVMIsAConstantFP(p
)) {
6657 double a
= LLVMConstRealGetDouble(p
, &loses_info
);
6664 return false; /* other constant */
6669 /* Only certain combinations of 0 and 1 can be eliminated. */
6670 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
6671 default_val
= is_zero
[3] ? 0 : 1;
6672 else if (is_one
[0] && is_one
[1] && is_one
[2])
6673 default_val
= is_zero
[3] ? 2 : 3;
6677 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
6678 LLVMInstructionEraseFromParent(inst
);
6680 /* Change OFFSET to DEFAULT_VAL. */
6681 for (i
= 0; i
< num_outputs
; i
++) {
6682 if (shader
->info
.vs_output_param_offset
[i
] == offset
) {
6683 shader
->info
.vs_output_param_offset
[i
] =
6684 EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
6691 struct si_vs_exports
{
6693 unsigned offset
[SI_MAX_VS_OUTPUTS
];
6694 LLVMValueRef inst
[SI_MAX_VS_OUTPUTS
];
6697 static void si_eliminate_const_vs_outputs(struct si_shader_context
*ctx
)
6699 struct si_shader
*shader
= ctx
->shader
;
6700 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6701 LLVMBasicBlockRef bb
;
6702 struct si_vs_exports exports
;
6703 bool removed_any
= false;
6707 if (ctx
->type
== PIPE_SHADER_FRAGMENT
||
6708 ctx
->type
== PIPE_SHADER_COMPUTE
||
6709 shader
->key
.as_es
||
6713 /* Process all LLVM instructions. */
6714 bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6716 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
6719 LLVMValueRef cur
= inst
;
6720 inst
= LLVMGetNextInstruction(inst
);
6722 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
6725 LLVMValueRef callee
= lp_get_called_value(cur
);
6727 if (!lp_is_function(callee
))
6730 const char *name
= LLVMGetValueName(callee
);
6731 unsigned num_args
= LLVMCountParams(callee
);
6733 /* Check if this is an export instruction. */
6734 if (num_args
!= 9 || strcmp(name
, "llvm.SI.export"))
6737 LLVMValueRef arg
= LLVMGetOperand(cur
, 3);
6738 unsigned target
= LLVMConstIntGetZExtValue(arg
);
6740 if (target
< V_008DFC_SQ_EXP_PARAM
)
6743 target
-= V_008DFC_SQ_EXP_PARAM
;
6745 /* Eliminate constant value PARAM exports. */
6746 if (si_eliminate_const_output(ctx
, cur
, target
)) {
6749 exports
.offset
[exports
.num
] = target
;
6750 exports
.inst
[exports
.num
] = cur
;
6754 bb
= LLVMGetNextBasicBlock(bb
);
6757 /* Remove holes in export memory due to removed PARAM exports.
6758 * This is done by renumbering all PARAM exports.
6761 ubyte current_offset
[SI_MAX_VS_OUTPUTS
];
6762 unsigned new_count
= 0;
6765 /* Make a copy of the offsets. We need the old version while
6766 * we are modifying some of them. */
6767 assert(sizeof(current_offset
) ==
6768 sizeof(shader
->info
.vs_output_param_offset
));
6769 memcpy(current_offset
, shader
->info
.vs_output_param_offset
,
6770 sizeof(current_offset
));
6772 for (i
= 0; i
< exports
.num
; i
++) {
6773 unsigned offset
= exports
.offset
[i
];
6775 for (out
= 0; out
< info
->num_outputs
; out
++) {
6776 if (current_offset
[out
] != offset
)
6779 LLVMSetOperand(exports
.inst
[i
], 3,
6780 LLVMConstInt(ctx
->i32
,
6781 V_008DFC_SQ_EXP_PARAM
+ new_count
, 0));
6782 shader
->info
.vs_output_param_offset
[out
] = new_count
;
6787 shader
->info
.nr_param_exports
= new_count
;
6791 static void si_count_scratch_private_memory(struct si_shader_context
*ctx
)
6793 ctx
->shader
->config
.private_mem_vgprs
= 0;
6795 /* Process all LLVM instructions. */
6796 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6798 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
6801 LLVMValueRef inst
= next
;
6802 next
= LLVMGetNextInstruction(next
);
6804 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
6807 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
6808 /* No idea why LLVM aligns allocas to 4 elements. */
6809 unsigned alignment
= LLVMGetAlignment(inst
);
6810 unsigned dw_size
= align(llvm_get_type_size(type
) / 4, alignment
);
6811 ctx
->shader
->config
.private_mem_vgprs
+= dw_size
;
6813 bb
= LLVMGetNextBasicBlock(bb
);
6817 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
,
6818 struct si_shader
*shader
)
6820 struct si_shader_selector
*sel
= shader
->selector
;
6821 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
6823 switch (ctx
->type
) {
6824 case PIPE_SHADER_VERTEX
:
6825 ctx
->load_input
= declare_input_vs
;
6826 if (shader
->key
.as_ls
)
6827 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
6828 else if (shader
->key
.as_es
)
6829 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6831 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6833 case PIPE_SHADER_TESS_CTRL
:
6834 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6835 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6836 bld_base
->emit_store
= store_output_tcs
;
6837 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
6839 case PIPE_SHADER_TESS_EVAL
:
6840 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6841 if (shader
->key
.as_es
)
6842 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6844 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6846 case PIPE_SHADER_GEOMETRY
:
6847 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6848 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
6850 case PIPE_SHADER_FRAGMENT
:
6851 ctx
->load_input
= declare_input_fs
;
6852 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
6854 case PIPE_SHADER_COMPUTE
:
6855 ctx
->declare_memory_region
= declare_compute_memory
;
6858 assert(!"Unsupported shader type");
6862 create_meta_data(ctx
);
6863 create_function(ctx
);
6864 preload_ring_buffers(ctx
);
6866 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6868 for (i
= 0; i
< 4; i
++) {
6869 ctx
->gs_next_vertex
[i
] =
6870 lp_build_alloca(bld_base
->base
.gallivm
,
6875 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6876 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6880 si_llvm_build_ret(ctx
, ctx
->return_value
);
6885 * Compute the VS prolog key, which contains all the information needed to
6886 * build the VS prolog function, and set shader->info bits where needed.
6888 static void si_get_vs_prolog_key(struct si_shader
*shader
,
6889 union si_shader_part_key
*key
)
6891 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6893 memset(key
, 0, sizeof(*key
));
6894 key
->vs_prolog
.states
= shader
->key
.part
.vs
.prolog
;
6895 key
->vs_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6896 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6898 /* Set the instanceID flag. */
6899 for (unsigned i
= 0; i
< info
->num_inputs
; i
++)
6900 if (key
->vs_prolog
.states
.instance_divisors
[i
])
6901 shader
->info
.uses_instanceid
= true;
6905 * Compute the VS epilog key, which contains all the information needed to
6906 * build the VS epilog function, and set the PrimitiveID output offset.
6908 static void si_get_vs_epilog_key(struct si_shader
*shader
,
6909 struct si_vs_epilog_bits
*states
,
6910 union si_shader_part_key
*key
)
6912 memset(key
, 0, sizeof(*key
));
6913 key
->vs_epilog
.states
= *states
;
6915 /* Set up the PrimitiveID output. */
6916 if (shader
->key
.part
.vs
.epilog
.export_prim_id
) {
6917 unsigned index
= shader
->selector
->info
.num_outputs
;
6918 unsigned offset
= shader
->info
.nr_param_exports
++;
6920 key
->vs_epilog
.prim_id_param_offset
= offset
;
6921 assert(index
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
6922 shader
->info
.vs_output_param_offset
[index
] = offset
;
6927 * Compute the PS prolog key, which contains all the information needed to
6928 * build the PS prolog function, and set related bits in shader->config.
6930 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6931 union si_shader_part_key
*key
,
6932 bool separate_prolog
)
6934 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6936 memset(key
, 0, sizeof(*key
));
6937 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6938 key
->ps_prolog
.colors_read
= info
->colors_read
;
6939 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6940 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6941 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6942 (key
->ps_prolog
.colors_read
||
6943 key
->ps_prolog
.states
.force_persp_sample_interp
||
6944 key
->ps_prolog
.states
.force_linear_sample_interp
||
6945 key
->ps_prolog
.states
.force_persp_center_interp
||
6946 key
->ps_prolog
.states
.force_linear_center_interp
||
6947 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6948 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6950 if (info
->colors_read
) {
6951 unsigned *color
= shader
->selector
->color_attr_index
;
6953 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6954 /* BCOLORs are stored after the last input. */
6955 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6956 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6957 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6960 for (unsigned i
= 0; i
< 2; i
++) {
6961 unsigned interp
= info
->input_interpolate
[color
[i
]];
6962 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6964 if (!(info
->colors_read
& (0xf << i
*4)))
6967 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6969 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6970 interp
== TGSI_INTERPOLATE_COLOR
)
6971 interp
= TGSI_INTERPOLATE_CONSTANT
;
6974 case TGSI_INTERPOLATE_CONSTANT
:
6975 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6977 case TGSI_INTERPOLATE_PERSPECTIVE
:
6978 case TGSI_INTERPOLATE_COLOR
:
6979 /* Force the interpolation location for colors here. */
6980 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6981 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6982 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6983 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6986 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6987 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6988 shader
->config
.spi_ps_input_ena
|=
6989 S_0286CC_PERSP_SAMPLE_ENA(1);
6991 case TGSI_INTERPOLATE_LOC_CENTER
:
6992 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6993 shader
->config
.spi_ps_input_ena
|=
6994 S_0286CC_PERSP_CENTER_ENA(1);
6996 case TGSI_INTERPOLATE_LOC_CENTROID
:
6997 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6998 shader
->config
.spi_ps_input_ena
|=
6999 S_0286CC_PERSP_CENTROID_ENA(1);
7005 case TGSI_INTERPOLATE_LINEAR
:
7006 /* Force the interpolation location for colors here. */
7007 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
7008 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
7009 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
7010 location
= TGSI_INTERPOLATE_LOC_CENTER
;
7012 /* The VGPR assignment for non-monolithic shaders
7013 * works because InitialPSInputAddr is set on the
7014 * main shader and PERSP_PULL_MODEL is never used.
7017 case TGSI_INTERPOLATE_LOC_SAMPLE
:
7018 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7019 separate_prolog
? 6 : 9;
7020 shader
->config
.spi_ps_input_ena
|=
7021 S_0286CC_LINEAR_SAMPLE_ENA(1);
7023 case TGSI_INTERPOLATE_LOC_CENTER
:
7024 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7025 separate_prolog
? 8 : 11;
7026 shader
->config
.spi_ps_input_ena
|=
7027 S_0286CC_LINEAR_CENTER_ENA(1);
7029 case TGSI_INTERPOLATE_LOC_CENTROID
:
7030 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
7031 separate_prolog
? 10 : 13;
7032 shader
->config
.spi_ps_input_ena
|=
7033 S_0286CC_LINEAR_CENTROID_ENA(1);
7047 * Check whether a PS prolog is required based on the key.
7049 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
7051 return key
->ps_prolog
.colors_read
||
7052 key
->ps_prolog
.states
.force_persp_sample_interp
||
7053 key
->ps_prolog
.states
.force_linear_sample_interp
||
7054 key
->ps_prolog
.states
.force_persp_center_interp
||
7055 key
->ps_prolog
.states
.force_linear_center_interp
||
7056 key
->ps_prolog
.states
.bc_optimize_for_persp
||
7057 key
->ps_prolog
.states
.bc_optimize_for_linear
||
7058 key
->ps_prolog
.states
.poly_stipple
;
7062 * Compute the PS epilog key, which contains all the information needed to
7063 * build the PS epilog function.
7065 static void si_get_ps_epilog_key(struct si_shader
*shader
,
7066 union si_shader_part_key
*key
)
7068 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
7069 memset(key
, 0, sizeof(*key
));
7070 key
->ps_epilog
.colors_written
= info
->colors_written
;
7071 key
->ps_epilog
.writes_z
= info
->writes_z
;
7072 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
7073 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
7074 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
7078 * Build the GS prolog function. Rotate the input vertices for triangle strips
7081 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
7082 union si_shader_part_key
*key
)
7084 const unsigned num_sgprs
= SI_GS_NUM_USER_SGPR
+ 2;
7085 const unsigned num_vgprs
= 8;
7086 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7087 LLVMBuilderRef builder
= gallivm
->builder
;
7088 LLVMTypeRef params
[32];
7089 LLVMTypeRef returns
[32];
7090 LLVMValueRef func
, ret
;
7092 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
7093 params
[i
] = ctx
->i32
;
7094 returns
[i
] = ctx
->i32
;
7097 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
7098 params
[num_sgprs
+ i
] = ctx
->i32
;
7099 returns
[num_sgprs
+ i
] = ctx
->f32
;
7102 /* Create the function. */
7103 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
7104 params
, num_sgprs
+ num_vgprs
, num_sgprs
- 1);
7105 func
= ctx
->main_fn
;
7107 /* Copy inputs to outputs. This should be no-op, as the registers match,
7108 * but it will prevent the compiler from overwriting them unintentionally.
7110 ret
= ctx
->return_value
;
7111 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
7112 LLVMValueRef p
= LLVMGetParam(func
, i
);
7113 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
7115 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
7116 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
7117 p
= LLVMBuildBitCast(builder
, p
, ctx
->f32
, "");
7118 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
7121 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
7122 /* Remap the input vertices for every other primitive. */
7123 const unsigned vtx_params
[6] = {
7131 LLVMValueRef prim_id
, rotate
;
7133 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
7134 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
7136 for (unsigned i
= 0; i
< 6; ++i
) {
7137 LLVMValueRef base
, rotated
, actual
;
7138 base
= LLVMGetParam(func
, vtx_params
[i
]);
7139 rotated
= LLVMGetParam(func
, vtx_params
[(i
+ 4) % 6]);
7140 actual
= LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
7141 actual
= LLVMBuildBitCast(builder
, actual
, ctx
->f32
, "");
7142 ret
= LLVMBuildInsertValue(builder
, ret
, actual
, vtx_params
[i
], "");
7146 LLVMBuildRet(builder
, ret
);
7150 * Given a list of shader part functions, build a wrapper function that
7151 * runs them in sequence to form a monolithic shader.
7153 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
7154 LLVMValueRef
*parts
,
7158 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7159 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
7160 /* PS epilog has one arg per color component */
7161 LLVMTypeRef param_types
[48];
7162 LLVMValueRef out
[48];
7163 LLVMTypeRef function_type
;
7164 unsigned num_params
;
7166 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
7167 unsigned num_sgprs
, num_vgprs
;
7168 unsigned last_sgpr_param
;
7171 for (unsigned i
= 0; i
< num_parts
; ++i
) {
7172 lp_add_function_attr(parts
[i
], -1, LP_FUNC_ATTR_ALWAYSINLINE
);
7173 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
7176 /* The parameters of the wrapper function correspond to those of the
7177 * first part in terms of SGPRs and VGPRs, but we use the types of the
7178 * main part to get the right types. This is relevant for the
7179 * dereferenceable attribute on descriptor table pointers.
7184 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
7185 num_params
= LLVMCountParamTypes(function_type
);
7187 for (unsigned i
= 0; i
< num_params
; ++i
) {
7188 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
7190 if (ac_is_sgpr_param(param
)) {
7191 assert(num_vgprs
== 0);
7192 num_sgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7194 num_vgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7197 assert(num_vgprs
+ num_sgprs
<= ARRAY_SIZE(param_types
));
7200 last_sgpr_param
= 0;
7202 while (gprs
< num_sgprs
+ num_vgprs
) {
7203 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], num_params
);
7206 param_types
[num_params
] = LLVMTypeOf(param
);
7207 if (gprs
< num_sgprs
)
7208 last_sgpr_param
= num_params
;
7209 size
= llvm_get_type_size(param_types
[num_params
]) / 4;
7212 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
7213 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
7214 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
7219 si_create_function(ctx
, "wrapper", NULL
, 0, param_types
, num_params
, last_sgpr_param
);
7221 /* Record the arguments of the function as if they were an output of
7227 for (unsigned i
= 0; i
< num_params
; ++i
) {
7228 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
7229 LLVMTypeRef param_type
= LLVMTypeOf(param
);
7230 LLVMTypeRef out_type
= i
<= last_sgpr_param
? ctx
->i32
: ctx
->f32
;
7231 unsigned size
= llvm_get_type_size(param_type
) / 4;
7234 if (param_type
!= out_type
)
7235 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
7236 out
[num_out
++] = param
;
7238 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
7240 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7241 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
7242 param_type
= ctx
->i64
;
7245 if (param_type
!= vector_type
)
7246 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
7248 for (unsigned j
= 0; j
< size
; ++j
)
7249 out
[num_out
++] = LLVMBuildExtractElement(
7250 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
7253 if (i
<= last_sgpr_param
)
7254 num_out_sgpr
= num_out
;
7257 /* Now chain the parts. */
7258 for (unsigned part
= 0; part
< num_parts
; ++part
) {
7259 LLVMValueRef in
[48];
7261 LLVMTypeRef ret_type
;
7262 unsigned out_idx
= 0;
7264 num_params
= LLVMCountParams(parts
[part
]);
7265 assert(num_params
<= ARRAY_SIZE(param_types
));
7267 /* Derive arguments for the next part from outputs of the
7270 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
7272 LLVMTypeRef param_type
;
7274 unsigned param_size
;
7275 LLVMValueRef arg
= NULL
;
7277 param
= LLVMGetParam(parts
[part
], param_idx
);
7278 param_type
= LLVMTypeOf(param
);
7279 param_size
= llvm_get_type_size(param_type
) / 4;
7280 is_sgpr
= ac_is_sgpr_param(param
);
7283 #if HAVE_LLVM < 0x0400
7284 LLVMRemoveAttribute(param
, LLVMByValAttribute
);
7286 unsigned kind_id
= LLVMGetEnumAttributeKindForName("byval", 5);
7287 LLVMRemoveEnumAttributeAtIndex(parts
[part
], param_idx
+ 1, kind_id
);
7289 lp_add_function_attr(parts
[part
], param_idx
+ 1, LP_FUNC_ATTR_INREG
);
7292 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
7293 assert(is_sgpr
|| out_idx
>= num_out_sgpr
);
7295 if (param_size
== 1)
7298 arg
= lp_build_gather_values(gallivm
, &out
[out_idx
], param_size
);
7300 if (LLVMTypeOf(arg
) != param_type
) {
7301 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7302 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
7303 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
7305 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
7309 in
[param_idx
] = arg
;
7310 out_idx
+= param_size
;
7313 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
7314 ret_type
= LLVMTypeOf(ret
);
7316 /* Extract the returned GPRs. */
7320 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
7321 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
7323 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
7325 for (unsigned i
= 0; i
< ret_size
; ++i
) {
7327 LLVMBuildExtractValue(builder
, ret
, i
, "");
7329 out
[num_out
++] = val
;
7331 if (LLVMTypeOf(val
) == ctx
->i32
) {
7332 assert(num_out_sgpr
+ 1 == num_out
);
7333 num_out_sgpr
= num_out
;
7339 LLVMBuildRetVoid(builder
);
7342 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
7343 LLVMTargetMachineRef tm
,
7344 struct si_shader
*shader
,
7346 struct pipe_debug_callback
*debug
)
7348 struct si_shader_selector
*sel
= shader
->selector
;
7349 struct si_shader_context ctx
;
7350 struct lp_build_tgsi_context
*bld_base
;
7354 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
7355 * conversion fails. */
7356 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
7357 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
7358 tgsi_dump(sel
->tokens
, 0);
7359 si_dump_streamout(&sel
->so
);
7362 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
7363 ctx
.separate_prolog
= !is_monolithic
;
7365 memset(shader
->info
.vs_output_param_offset
, EXP_PARAM_UNDEFINED
,
7366 sizeof(shader
->info
.vs_output_param_offset
));
7368 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
7370 bld_base
= &ctx
.bld_base
;
7371 ctx
.load_system_value
= declare_system_value
;
7373 if (!si_compile_tgsi_main(&ctx
, shader
)) {
7374 si_llvm_dispose(&ctx
);
7378 if (is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
7379 LLVMValueRef parts
[3];
7383 need_prolog
= sel
->info
.num_inputs
;
7384 need_epilog
= !shader
->key
.as_es
&& !shader
->key
.as_ls
;
7386 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7389 union si_shader_part_key prolog_key
;
7390 si_get_vs_prolog_key(shader
, &prolog_key
);
7391 si_build_vs_prolog_function(&ctx
, &prolog_key
);
7392 parts
[0] = ctx
.main_fn
;
7396 union si_shader_part_key epilog_key
;
7397 si_get_vs_epilog_key(shader
, &shader
->key
.part
.vs
.epilog
, &epilog_key
);
7398 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7399 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7402 si_build_wrapper_function(&ctx
, parts
, 1 + need_prolog
+ need_epilog
,
7403 need_prolog
? 1 : 0);
7404 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
7405 LLVMValueRef parts
[2];
7406 union si_shader_part_key epilog_key
;
7408 parts
[0] = ctx
.main_fn
;
7410 memset(&epilog_key
, 0, sizeof(epilog_key
));
7411 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7412 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
7413 parts
[1] = ctx
.main_fn
;
7415 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7416 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_EVAL
&&
7417 !shader
->key
.as_es
) {
7418 LLVMValueRef parts
[2];
7419 union si_shader_part_key epilog_key
;
7421 parts
[0] = ctx
.main_fn
;
7423 si_get_vs_epilog_key(shader
, &shader
->key
.part
.tes
.epilog
, &epilog_key
);
7424 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7425 parts
[1] = ctx
.main_fn
;
7427 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7428 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
7429 LLVMValueRef parts
[2];
7430 union si_shader_part_key prolog_key
;
7432 parts
[1] = ctx
.main_fn
;
7434 memset(&prolog_key
, 0, sizeof(prolog_key
));
7435 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7436 si_build_gs_prolog_function(&ctx
, &prolog_key
);
7437 parts
[0] = ctx
.main_fn
;
7439 si_build_wrapper_function(&ctx
, parts
, 2, 1);
7440 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7441 LLVMValueRef parts
[3];
7442 union si_shader_part_key prolog_key
;
7443 union si_shader_part_key epilog_key
;
7446 si_get_ps_prolog_key(shader
, &prolog_key
, false);
7447 need_prolog
= si_need_ps_prolog(&prolog_key
);
7449 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7452 si_build_ps_prolog_function(&ctx
, &prolog_key
);
7453 parts
[0] = ctx
.main_fn
;
7456 si_get_ps_epilog_key(shader
, &epilog_key
);
7457 si_build_ps_epilog_function(&ctx
, &epilog_key
);
7458 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7460 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2, need_prolog
? 1 : 0);
7463 mod
= bld_base
->base
.gallivm
->module
;
7465 /* Dump LLVM IR before any optimization passes */
7466 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
7467 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7468 ac_dump_module(mod
);
7470 si_llvm_finalize_module(&ctx
,
7471 r600_extra_shader_checks(&sscreen
->b
, ctx
.type
));
7473 /* Post-optimization transformations and analysis. */
7474 si_eliminate_const_vs_outputs(&ctx
);
7476 if ((debug
&& debug
->debug_message
) ||
7477 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7478 si_count_scratch_private_memory(&ctx
);
7480 /* Compile to bytecode. */
7481 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
7482 mod
, debug
, ctx
.type
, "TGSI shader");
7483 si_llvm_dispose(&ctx
);
7485 fprintf(stderr
, "LLVM failed to compile shader\n");
7489 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
7490 * LLVM 3.9svn has this bug.
7492 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
7493 unsigned wave_size
= 64;
7494 unsigned max_vgprs
= 256;
7495 unsigned max_sgprs
= sscreen
->b
.chip_class
>= VI
? 800 : 512;
7496 unsigned max_sgprs_per_wave
= 128;
7497 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
7498 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7499 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7501 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7502 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7504 if (shader
->config
.num_sgprs
> max_sgprs
||
7505 shader
->config
.num_vgprs
> max_vgprs
) {
7506 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7507 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7508 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7509 max_sgprs
, max_vgprs
);
7511 /* Just terminate the process, because dependent
7512 * shaders can hang due to bad input data, but use
7513 * the env var to allow shader-db to work.
7515 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7520 /* Add the scratch offset to input SGPRs. */
7521 if (shader
->config
.scratch_bytes_per_wave
)
7522 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7524 /* Calculate the number of fragment input VGPRs. */
7525 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7526 shader
->info
.num_input_vgprs
= 0;
7527 shader
->info
.face_vgpr_index
= -1;
7529 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7530 shader
->info
.num_input_vgprs
+= 2;
7531 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7532 shader
->info
.num_input_vgprs
+= 2;
7533 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7534 shader
->info
.num_input_vgprs
+= 2;
7535 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7536 shader
->info
.num_input_vgprs
+= 3;
7537 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7538 shader
->info
.num_input_vgprs
+= 2;
7539 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7540 shader
->info
.num_input_vgprs
+= 2;
7541 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7542 shader
->info
.num_input_vgprs
+= 2;
7543 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7544 shader
->info
.num_input_vgprs
+= 1;
7545 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7546 shader
->info
.num_input_vgprs
+= 1;
7547 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7548 shader
->info
.num_input_vgprs
+= 1;
7549 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7550 shader
->info
.num_input_vgprs
+= 1;
7551 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7552 shader
->info
.num_input_vgprs
+= 1;
7553 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7554 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7555 shader
->info
.num_input_vgprs
+= 1;
7557 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
7558 shader
->info
.num_input_vgprs
+= 1;
7559 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7560 shader
->info
.num_input_vgprs
+= 1;
7561 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7562 shader
->info
.num_input_vgprs
+= 1;
7569 * Create, compile and return a shader part (prolog or epilog).
7571 * \param sscreen screen
7572 * \param list list of shader parts of the same category
7573 * \param type shader type
7574 * \param key shader part key
7575 * \param prolog whether the part being requested is a prolog
7576 * \param tm LLVM target machine
7577 * \param debug debug callback
7578 * \param build the callback responsible for building the main function
7579 * \return non-NULL on success
7581 static struct si_shader_part
*
7582 si_get_shader_part(struct si_screen
*sscreen
,
7583 struct si_shader_part
**list
,
7584 enum pipe_shader_type type
,
7586 union si_shader_part_key
*key
,
7587 LLVMTargetMachineRef tm
,
7588 struct pipe_debug_callback
*debug
,
7589 void (*build
)(struct si_shader_context
*,
7590 union si_shader_part_key
*),
7593 struct si_shader_part
*result
;
7595 pipe_mutex_lock(sscreen
->shader_parts_mutex
);
7597 /* Find existing. */
7598 for (result
= *list
; result
; result
= result
->next
) {
7599 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7600 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
7605 /* Compile a new one. */
7606 result
= CALLOC_STRUCT(si_shader_part
);
7609 struct si_shader shader
= {};
7610 struct si_shader_context ctx
;
7611 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
7613 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
7617 case PIPE_SHADER_VERTEX
:
7619 case PIPE_SHADER_TESS_CTRL
:
7621 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7623 case PIPE_SHADER_GEOMETRY
:
7626 case PIPE_SHADER_FRAGMENT
:
7628 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7630 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7633 unreachable("bad shader part");
7639 si_llvm_finalize_module(&ctx
,
7640 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_FRAGMENT
));
7642 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, tm
,
7643 gallivm
->module
, debug
, ctx
.type
, name
)) {
7649 result
->next
= *list
;
7653 si_llvm_dispose(&ctx
);
7654 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
7659 * Build the vertex shader prolog function.
7661 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7662 * All inputs are returned unmodified. The vertex load indices are
7663 * stored after them, which will be used by the API VS for fetching inputs.
7665 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7670 * (VertexID + BaseVertex),
7671 * (InstanceID + StartInstance),
7672 * (InstanceID / 2 + StartInstance)
7674 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7675 union si_shader_part_key
*key
)
7677 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7678 LLVMTypeRef
*params
, *returns
;
7679 LLVMValueRef ret
, func
;
7680 int last_sgpr
, num_params
, num_returns
, i
;
7682 ctx
->param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
7683 ctx
->param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
7685 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7686 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
7687 sizeof(LLVMTypeRef
));
7688 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
7689 key
->vs_prolog
.last_input
+ 1) *
7690 sizeof(LLVMTypeRef
));
7694 /* Declare input and output SGPRs. */
7696 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7697 params
[num_params
++] = ctx
->i32
;
7698 returns
[num_returns
++] = ctx
->i32
;
7700 last_sgpr
= num_params
- 1;
7702 /* 4 preloaded VGPRs (outputs must be floats) */
7703 for (i
= 0; i
< 4; i
++) {
7704 params
[num_params
++] = ctx
->i32
;
7705 returns
[num_returns
++] = ctx
->f32
;
7708 /* Vertex load indices. */
7709 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7710 returns
[num_returns
++] = ctx
->f32
;
7712 /* Create the function. */
7713 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, params
,
7714 num_params
, last_sgpr
);
7715 func
= ctx
->main_fn
;
7717 /* Copy inputs to outputs. This should be no-op, as the registers match,
7718 * but it will prevent the compiler from overwriting them unintentionally.
7720 ret
= ctx
->return_value
;
7721 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7722 LLVMValueRef p
= LLVMGetParam(func
, i
);
7723 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7725 for (i
= num_params
- 4; i
< num_params
; i
++) {
7726 LLVMValueRef p
= LLVMGetParam(func
, i
);
7727 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
->f32
, "");
7728 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7731 /* Compute vertex load indices from instance divisors. */
7732 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7733 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
7737 /* InstanceID / Divisor + StartInstance */
7738 index
= get_instance_index_for_fetch(ctx
,
7739 SI_SGPR_START_INSTANCE
,
7742 /* VertexID + BaseVertex */
7743 index
= LLVMBuildAdd(gallivm
->builder
,
7744 LLVMGetParam(func
, ctx
->param_vertex_id
),
7745 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
7748 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
->f32
, "");
7749 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
7753 si_llvm_build_ret(ctx
, ret
);
7757 * Build the vertex shader epilog function. This is also used by the tessellation
7758 * evaluation shader compiled as VS.
7760 * The input is PrimitiveID.
7762 * If PrimitiveID is required by the pixel shader, export it.
7763 * Otherwise, do nothing.
7765 static void si_build_vs_epilog_function(struct si_shader_context
*ctx
,
7766 union si_shader_part_key
*key
)
7768 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7769 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7770 LLVMTypeRef params
[5];
7773 /* Declare input VGPRs. */
7774 num_params
= key
->vs_epilog
.states
.export_prim_id
?
7775 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
7776 assert(num_params
<= ARRAY_SIZE(params
));
7778 for (i
= 0; i
< num_params
; i
++)
7779 params
[i
] = ctx
->f32
;
7781 /* Create the function. */
7782 si_create_function(ctx
, "vs_epilog", NULL
, 0, params
, num_params
, -1);
7785 if (key
->vs_epilog
.states
.export_prim_id
) {
7786 struct lp_build_context
*base
= &bld_base
->base
;
7787 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
7788 LLVMValueRef args
[9];
7790 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
7791 args
[1] = uint
->zero
; /* whether the EXEC mask is valid */
7792 args
[2] = uint
->zero
; /* DONE bit */
7793 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_PARAM
+
7794 key
->vs_epilog
.prim_id_param_offset
);
7795 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
7796 args
[5] = LLVMGetParam(ctx
->main_fn
,
7797 VS_EPILOG_PRIMID_LOC
); /* X */
7798 args
[6] = base
->undef
; /* Y */
7799 args
[7] = base
->undef
; /* Z */
7800 args
[8] = base
->undef
; /* W */
7802 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
7803 LLVMVoidTypeInContext(base
->gallivm
->context
),
7807 LLVMBuildRetVoid(gallivm
->builder
);
7811 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
7813 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
7814 LLVMTargetMachineRef tm
,
7815 struct si_shader
*shader
,
7816 struct pipe_debug_callback
*debug
,
7817 struct si_vs_epilog_bits
*states
)
7819 union si_shader_part_key epilog_key
;
7821 si_get_vs_epilog_key(shader
, states
, &epilog_key
);
7823 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
7824 PIPE_SHADER_VERTEX
, true,
7825 &epilog_key
, tm
, debug
,
7826 si_build_vs_epilog_function
,
7827 "Vertex Shader Epilog");
7828 return shader
->epilog
!= NULL
;
7832 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7834 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7835 LLVMTargetMachineRef tm
,
7836 struct si_shader
*shader
,
7837 struct pipe_debug_callback
*debug
)
7839 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
7840 union si_shader_part_key prolog_key
;
7842 /* Get the prolog. */
7843 si_get_vs_prolog_key(shader
, &prolog_key
);
7845 /* The prolog is a no-op if there are no inputs. */
7846 if (info
->num_inputs
) {
7848 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7849 PIPE_SHADER_VERTEX
, true,
7850 &prolog_key
, tm
, debug
,
7851 si_build_vs_prolog_function
,
7852 "Vertex Shader Prolog");
7853 if (!shader
->prolog
)
7857 /* Get the epilog. */
7858 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
&&
7859 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7860 &shader
->key
.part
.vs
.epilog
))
7867 * Select and compile (or reuse) TES parts (epilog).
7869 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
7870 LLVMTargetMachineRef tm
,
7871 struct si_shader
*shader
,
7872 struct pipe_debug_callback
*debug
)
7874 if (shader
->key
.as_es
)
7877 /* TES compiled as VS. */
7878 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7879 &shader
->key
.part
.tes
.epilog
);
7883 * Compile the TCS epilog function. This writes tesselation factors to memory
7884 * based on the output primitive type of the tesselator (determined by TES).
7886 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7887 union si_shader_part_key
*key
)
7889 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7890 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
7891 LLVMTypeRef params
[16];
7893 int last_sgpr
, num_params
;
7895 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
7896 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
7897 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
7898 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
7899 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
7900 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
7901 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
7902 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
7903 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
7904 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
7905 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
7906 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
7907 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
7908 num_params
= last_sgpr
+ 1;
7910 params
[num_params
++] = ctx
->i32
; /* patch index within the wave (REL_PATCH_ID) */
7911 params
[num_params
++] = ctx
->i32
; /* invocation ID within the patch */
7912 params
[num_params
++] = ctx
->i32
; /* LDS offset where tess factors should be loaded from */
7914 /* Create the function. */
7915 si_create_function(ctx
, "tcs_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
7916 declare_tess_lds(ctx
);
7917 func
= ctx
->main_fn
;
7919 si_write_tess_factors(bld_base
,
7920 LLVMGetParam(func
, last_sgpr
+ 1),
7921 LLVMGetParam(func
, last_sgpr
+ 2),
7922 LLVMGetParam(func
, last_sgpr
+ 3));
7924 LLVMBuildRetVoid(gallivm
->builder
);
7928 * Select and compile (or reuse) TCS parts (epilog).
7930 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7931 LLVMTargetMachineRef tm
,
7932 struct si_shader
*shader
,
7933 struct pipe_debug_callback
*debug
)
7935 union si_shader_part_key epilog_key
;
7937 /* Get the epilog. */
7938 memset(&epilog_key
, 0, sizeof(epilog_key
));
7939 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7941 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7942 PIPE_SHADER_TESS_CTRL
, false,
7943 &epilog_key
, tm
, debug
,
7944 si_build_tcs_epilog_function
,
7945 "Tessellation Control Shader Epilog");
7946 return shader
->epilog
!= NULL
;
7950 * Select and compile (or reuse) GS parts (prolog).
7952 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7953 LLVMTargetMachineRef tm
,
7954 struct si_shader
*shader
,
7955 struct pipe_debug_callback
*debug
)
7957 union si_shader_part_key prolog_key
;
7959 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7962 memset(&prolog_key
, 0, sizeof(prolog_key
));
7963 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7965 shader
->prolog
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7966 PIPE_SHADER_GEOMETRY
, true,
7967 &prolog_key
, tm
, debug
,
7968 si_build_gs_prolog_function
,
7969 "Geometry Shader Prolog");
7970 return shader
->prolog
!= NULL
;
7974 * Build the pixel shader prolog function. This handles:
7975 * - two-side color selection and interpolation
7976 * - overriding interpolation parameters for the API PS
7977 * - polygon stippling
7979 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7980 * overriden by other states. (e.g. per-sample interpolation)
7981 * Interpolated colors are stored after the preloaded VGPRs.
7983 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7984 union si_shader_part_key
*key
)
7986 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7987 LLVMTypeRef
*params
;
7988 LLVMValueRef ret
, func
;
7989 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
7991 assert(si_need_ps_prolog(key
));
7993 /* Number of inputs + 8 color elements. */
7994 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
7995 key
->ps_prolog
.num_input_vgprs
+ 8) *
7996 sizeof(LLVMTypeRef
));
7998 /* Declare inputs. */
8000 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
8001 params
[num_params
++] = ctx
->i32
;
8002 last_sgpr
= num_params
- 1;
8004 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
8005 params
[num_params
++] = ctx
->f32
;
8007 /* Declare outputs (same as inputs + add colors if needed) */
8008 num_returns
= num_params
;
8009 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
8010 for (i
= 0; i
< num_color_channels
; i
++)
8011 params
[num_returns
++] = ctx
->f32
;
8013 /* Create the function. */
8014 si_create_function(ctx
, "ps_prolog", params
, num_returns
, params
,
8015 num_params
, last_sgpr
);
8016 func
= ctx
->main_fn
;
8018 /* Copy inputs to outputs. This should be no-op, as the registers match,
8019 * but it will prevent the compiler from overwriting them unintentionally.
8021 ret
= ctx
->return_value
;
8022 for (i
= 0; i
< num_params
; i
++) {
8023 LLVMValueRef p
= LLVMGetParam(func
, i
);
8024 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
8027 /* Polygon stippling. */
8028 if (key
->ps_prolog
.states
.poly_stipple
) {
8029 /* POS_FIXED_PT is always last. */
8030 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
8031 key
->ps_prolog
.num_input_vgprs
- 1;
8032 LLVMValueRef ptr
[2], list
;
8034 /* Get the pointer to rw buffers. */
8035 ptr
[0] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS
);
8036 ptr
[1] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS_HI
);
8037 list
= lp_build_gather_values(gallivm
, ptr
, 2);
8038 list
= LLVMBuildBitCast(gallivm
->builder
, list
, ctx
->i64
, "");
8039 list
= LLVMBuildIntToPtr(gallivm
->builder
, list
,
8040 const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
), "");
8042 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
8045 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
8046 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
8047 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8048 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
8050 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
8051 * The hw doesn't compute CENTROID if the whole wave only
8052 * contains fully-covered quads.
8054 * PRIM_MASK is after user SGPRs.
8056 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
8057 bc_optimize
= LLVMBuildLShr(gallivm
->builder
, bc_optimize
,
8058 LLVMConstInt(ctx
->i32
, 31, 0), "");
8059 bc_optimize
= LLVMBuildTrunc(gallivm
->builder
, bc_optimize
,
8062 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
8063 /* Read PERSP_CENTER. */
8064 for (i
= 0; i
< 2; i
++)
8065 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
8066 /* Read PERSP_CENTROID. */
8067 for (i
= 0; i
< 2; i
++)
8068 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
8069 /* Select PERSP_CENTROID. */
8070 for (i
= 0; i
< 2; i
++) {
8071 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
8072 center
[i
], centroid
[i
], "");
8073 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8074 tmp
, base
+ 4 + i
, "");
8077 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
8078 /* Read LINEAR_CENTER. */
8079 for (i
= 0; i
< 2; i
++)
8080 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
8081 /* Read LINEAR_CENTROID. */
8082 for (i
= 0; i
< 2; i
++)
8083 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
8084 /* Select LINEAR_CENTROID. */
8085 for (i
= 0; i
< 2; i
++) {
8086 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
8087 center
[i
], centroid
[i
], "");
8088 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8089 tmp
, base
+ 10 + i
, "");
8094 /* Force per-sample interpolation. */
8095 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
8096 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8097 LLVMValueRef persp_sample
[2];
8099 /* Read PERSP_SAMPLE. */
8100 for (i
= 0; i
< 2; i
++)
8101 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
8102 /* Overwrite PERSP_CENTER. */
8103 for (i
= 0; i
< 2; i
++)
8104 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8105 persp_sample
[i
], base
+ 2 + i
, "");
8106 /* Overwrite PERSP_CENTROID. */
8107 for (i
= 0; i
< 2; i
++)
8108 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8109 persp_sample
[i
], base
+ 4 + i
, "");
8111 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
8112 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8113 LLVMValueRef linear_sample
[2];
8115 /* Read LINEAR_SAMPLE. */
8116 for (i
= 0; i
< 2; i
++)
8117 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
8118 /* Overwrite LINEAR_CENTER. */
8119 for (i
= 0; i
< 2; i
++)
8120 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8121 linear_sample
[i
], base
+ 8 + i
, "");
8122 /* Overwrite LINEAR_CENTROID. */
8123 for (i
= 0; i
< 2; i
++)
8124 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8125 linear_sample
[i
], base
+ 10 + i
, "");
8128 /* Force center interpolation. */
8129 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
8130 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8131 LLVMValueRef persp_center
[2];
8133 /* Read PERSP_CENTER. */
8134 for (i
= 0; i
< 2; i
++)
8135 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
8136 /* Overwrite PERSP_SAMPLE. */
8137 for (i
= 0; i
< 2; i
++)
8138 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8139 persp_center
[i
], base
+ i
, "");
8140 /* Overwrite PERSP_CENTROID. */
8141 for (i
= 0; i
< 2; i
++)
8142 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8143 persp_center
[i
], base
+ 4 + i
, "");
8145 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
8146 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8147 LLVMValueRef linear_center
[2];
8149 /* Read LINEAR_CENTER. */
8150 for (i
= 0; i
< 2; i
++)
8151 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
8152 /* Overwrite LINEAR_SAMPLE. */
8153 for (i
= 0; i
< 2; i
++)
8154 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8155 linear_center
[i
], base
+ 6 + i
, "");
8156 /* Overwrite LINEAR_CENTROID. */
8157 for (i
= 0; i
< 2; i
++)
8158 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8159 linear_center
[i
], base
+ 10 + i
, "");
8162 /* Interpolate colors. */
8163 for (i
= 0; i
< 2; i
++) {
8164 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
8165 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8166 key
->ps_prolog
.face_vgpr_index
;
8167 LLVMValueRef interp
[2], color
[4];
8168 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
8173 /* If the interpolation qualifier is not CONSTANT (-1). */
8174 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
8175 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8176 key
->ps_prolog
.color_interp_vgpr_index
[i
];
8178 /* Get the (i,j) updated by bc_optimize handling. */
8179 interp
[0] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8181 interp
[1] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8182 interp_vgpr
+ 1, "");
8183 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
8186 /* Use the absolute location of the input. */
8187 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
8189 if (key
->ps_prolog
.states
.color_two_side
) {
8190 face
= LLVMGetParam(func
, face_vgpr
);
8191 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
->i32
, "");
8194 interp_fs_input(ctx
,
8195 key
->ps_prolog
.color_attr_index
[i
],
8196 TGSI_SEMANTIC_COLOR
, i
,
8197 key
->ps_prolog
.num_interp_inputs
,
8198 key
->ps_prolog
.colors_read
, interp_ij
,
8199 prim_mask
, face
, color
);
8202 unsigned chan
= u_bit_scan(&writemask
);
8203 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
8208 /* Tell LLVM to insert WQM instruction sequence when needed. */
8209 if (key
->ps_prolog
.wqm
) {
8210 LLVMAddTargetDependentFunctionAttr(func
,
8211 "amdgpu-ps-wqm-outputs", "");
8214 si_llvm_build_ret(ctx
, ret
);
8218 * Build the pixel shader epilog function. This handles everything that must be
8219 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
8221 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
8222 union si_shader_part_key
*key
)
8224 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
8225 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
8226 LLVMTypeRef params
[16+8*4+3];
8227 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
8228 int last_sgpr
, num_params
, i
;
8229 struct si_ps_exports exp
= {};
8231 /* Declare input SGPRs. */
8232 params
[SI_PARAM_RW_BUFFERS
] = ctx
->i64
;
8233 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
8234 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
8235 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
8236 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
8237 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
8238 last_sgpr
= SI_PARAM_ALPHA_REF
;
8240 /* Declare input VGPRs. */
8241 num_params
= (last_sgpr
+ 1) +
8242 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
8243 key
->ps_epilog
.writes_z
+
8244 key
->ps_epilog
.writes_stencil
+
8245 key
->ps_epilog
.writes_samplemask
;
8247 num_params
= MAX2(num_params
,
8248 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
8250 assert(num_params
<= ARRAY_SIZE(params
));
8252 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
8253 params
[i
] = ctx
->f32
;
8255 /* Create the function. */
8256 si_create_function(ctx
, "ps_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
8257 /* Disable elimination of unused inputs. */
8258 si_llvm_add_attribute(ctx
->main_fn
,
8259 "InitialPSInputAddr", 0xffffff);
8261 /* Process colors. */
8262 unsigned vgpr
= last_sgpr
+ 1;
8263 unsigned colors_written
= key
->ps_epilog
.colors_written
;
8264 int last_color_export
= -1;
8266 /* Find the last color export. */
8267 if (!key
->ps_epilog
.writes_z
&&
8268 !key
->ps_epilog
.writes_stencil
&&
8269 !key
->ps_epilog
.writes_samplemask
) {
8270 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
8272 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
8273 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
8274 /* Just set this if any of the colorbuffers are enabled. */
8276 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
8277 last_color_export
= 0;
8279 for (i
= 0; i
< 8; i
++)
8280 if (colors_written
& (1 << i
) &&
8281 (spi_format
>> (i
* 4)) & 0xf)
8282 last_color_export
= i
;
8286 while (colors_written
) {
8287 LLVMValueRef color
[4];
8288 int mrt
= u_bit_scan(&colors_written
);
8290 for (i
= 0; i
< 4; i
++)
8291 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
8293 si_export_mrt_color(bld_base
, color
, mrt
,
8295 mrt
== last_color_export
, &exp
);
8298 /* Process depth, stencil, samplemask. */
8299 if (key
->ps_epilog
.writes_z
)
8300 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8301 if (key
->ps_epilog
.writes_stencil
)
8302 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8303 if (key
->ps_epilog
.writes_samplemask
)
8304 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8306 if (depth
|| stencil
|| samplemask
)
8307 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
8308 else if (last_color_export
== -1)
8309 si_export_null(bld_base
);
8312 si_emit_ps_exports(ctx
, &exp
);
8315 LLVMBuildRetVoid(gallivm
->builder
);
8319 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
8321 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
8322 LLVMTargetMachineRef tm
,
8323 struct si_shader
*shader
,
8324 struct pipe_debug_callback
*debug
)
8326 union si_shader_part_key prolog_key
;
8327 union si_shader_part_key epilog_key
;
8329 /* Get the prolog. */
8330 si_get_ps_prolog_key(shader
, &prolog_key
, true);
8332 /* The prolog is a no-op if these aren't set. */
8333 if (si_need_ps_prolog(&prolog_key
)) {
8335 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
8336 PIPE_SHADER_FRAGMENT
, true,
8337 &prolog_key
, tm
, debug
,
8338 si_build_ps_prolog_function
,
8339 "Fragment Shader Prolog");
8340 if (!shader
->prolog
)
8344 /* Get the epilog. */
8345 si_get_ps_epilog_key(shader
, &epilog_key
);
8348 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
8349 PIPE_SHADER_FRAGMENT
, false,
8350 &epilog_key
, tm
, debug
,
8351 si_build_ps_epilog_function
,
8352 "Fragment Shader Epilog");
8353 if (!shader
->epilog
)
8356 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
8357 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
8358 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
8359 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
8362 /* Set up the enable bits for per-sample shading if needed. */
8363 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
8364 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8365 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8366 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
8367 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8368 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
8370 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
8371 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8372 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8373 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
8374 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8375 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
8377 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
8378 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8379 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8380 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
8381 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8382 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8384 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
8385 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8386 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8387 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
8388 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8389 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8392 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
8393 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
8394 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
8395 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8396 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8399 /* At least one pair of interpolation weights must be enabled. */
8400 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
8401 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8402 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8405 /* The sample mask input is always enabled, because the API shader always
8406 * passes it through to the epilog. Disable it here if it's unused.
8408 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
8409 !shader
->selector
->info
.reads_samplemask
)
8410 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
8415 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
8418 /* SPI barrier management bug:
8419 * Make sure we have at least 4k of LDS in use to avoid the bug.
8420 * It applies to workgroup sizes of more than one wavefront.
8422 if (sscreen
->b
.family
== CHIP_BONAIRE
||
8423 sscreen
->b
.family
== CHIP_KABINI
||
8424 sscreen
->b
.family
== CHIP_MULLINS
)
8425 *lds_size
= MAX2(*lds_size
, 8);
8428 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8429 struct si_shader
*shader
)
8431 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8433 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8435 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8436 si_get_max_workgroup_size(shader
) > 64) {
8437 si_multiwave_lds_size_workaround(sscreen
,
8438 &shader
->config
.lds_size
);
8442 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
8443 struct si_shader
*shader
,
8444 struct pipe_debug_callback
*debug
)
8446 struct si_shader_selector
*sel
= shader
->selector
;
8447 struct si_shader
*mainp
= sel
->main_shader_part
;
8450 /* LS, ES, VS are compiled on demand if the main part hasn't been
8451 * compiled for that stage.
8453 * Vertex shaders are compiled on demand when a vertex fetch
8454 * workaround must be applied.
8456 if (shader
->is_monolithic
) {
8457 /* Monolithic shader (compiled as a whole, has many variants,
8458 * may take a long time to compile).
8460 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
8464 /* The shader consists of 2-3 parts:
8466 * - the middle part is the user shader, it has 1 variant only
8467 * and it was compiled during the creation of the shader
8469 * - the prolog part is inserted at the beginning
8470 * - the epilog part is inserted at the end
8472 * The prolog and epilog have many (but simple) variants.
8475 /* Copy the compiled TGSI shader data over. */
8476 shader
->is_binary_shared
= true;
8477 shader
->binary
= mainp
->binary
;
8478 shader
->config
= mainp
->config
;
8479 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8480 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8481 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8482 memcpy(shader
->info
.vs_output_param_offset
,
8483 mainp
->info
.vs_output_param_offset
,
8484 sizeof(mainp
->info
.vs_output_param_offset
));
8485 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8486 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8487 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8489 /* Select prologs and/or epilogs. */
8490 switch (sel
->type
) {
8491 case PIPE_SHADER_VERTEX
:
8492 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
8495 case PIPE_SHADER_TESS_CTRL
:
8496 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
8499 case PIPE_SHADER_TESS_EVAL
:
8500 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
8503 case PIPE_SHADER_GEOMETRY
:
8504 if (!si_shader_select_gs_parts(sscreen
, tm
, shader
, debug
))
8507 case PIPE_SHADER_FRAGMENT
:
8508 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
8511 /* Make sure we have at least as many VGPRs as there
8512 * are allocated inputs.
8514 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8515 shader
->info
.num_input_vgprs
);
8519 /* Update SGPR and VGPR counts. */
8520 if (shader
->prolog
) {
8521 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8522 shader
->prolog
->config
.num_sgprs
);
8523 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8524 shader
->prolog
->config
.num_vgprs
);
8526 if (shader
->epilog
) {
8527 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8528 shader
->epilog
->config
.num_sgprs
);
8529 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8530 shader
->epilog
->config
.num_vgprs
);
8534 si_fix_resource_usage(sscreen
, shader
);
8535 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8539 r
= si_shader_binary_upload(sscreen
, shader
);
8541 fprintf(stderr
, "LLVM failed to upload shader\n");
8548 void si_shader_destroy(struct si_shader
*shader
)
8550 if (shader
->scratch_bo
)
8551 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8553 r600_resource_reference(&shader
->bo
, NULL
);
8555 if (!shader
->is_binary_shared
)
8556 radeon_shader_binary_clean(&shader
->binary
);
8558 free(shader
->shader_log
);