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_bitarit.h"
35 #include "gallivm/lp_bld_flow.h"
36 #include "radeon/r600_cs.h"
37 #include "radeon/radeon_llvm.h"
38 #include "radeon/radeon_elf_util.h"
39 #include "radeon/radeon_llvm_emit.h"
40 #include "util/u_memory.h"
41 #include "util/u_pstipple.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_build.h"
44 #include "tgsi/tgsi_util.h"
45 #include "tgsi/tgsi_dump.h"
48 #include "si_shader.h"
53 static const char *scratch_rsrc_dword0_symbol
=
54 "SCRATCH_RSRC_DWORD0";
56 static const char *scratch_rsrc_dword1_symbol
=
57 "SCRATCH_RSRC_DWORD1";
59 struct si_shader_output_values
61 LLVMValueRef values
[4];
66 struct si_shader_context
68 struct radeon_llvm_context radeon_bld
;
69 struct si_shader
*shader
;
70 struct si_screen
*screen
;
72 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
73 bool is_gs_copy_shader
;
75 /* Whether to generate the optimized shader variant compiled as a whole
76 * (without a prolog and epilog)
80 int param_streamout_config
;
81 int param_streamout_write_index
;
82 int param_streamout_offset
[4];
84 int param_rel_auto_id
;
86 int param_instance_id
;
87 int param_vertex_index0
;
90 int param_tes_rel_patch_id
;
91 int param_tes_patch_id
;
92 int param_es2gs_offset
;
94 LLVMTargetMachineRef tm
;
96 LLVMValueRef const_md
;
97 LLVMValueRef const_buffers
[SI_NUM_CONST_BUFFERS
];
99 LLVMValueRef
*constants
[SI_NUM_CONST_BUFFERS
];
100 LLVMValueRef sampler_views
[SI_NUM_SAMPLERS
];
101 LLVMValueRef sampler_states
[SI_NUM_SAMPLERS
];
102 LLVMValueRef fmasks
[SI_NUM_USER_SAMPLERS
];
103 LLVMValueRef images
[SI_NUM_IMAGES
];
104 LLVMValueRef so_buffers
[4];
105 LLVMValueRef esgs_ring
;
106 LLVMValueRef gsvs_ring
[4];
107 LLVMValueRef gs_next_vertex
[4];
108 LLVMValueRef return_value
;
124 static struct si_shader_context
*si_shader_context(
125 struct lp_build_tgsi_context
*bld_base
)
127 return (struct si_shader_context
*)bld_base
;
130 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
131 struct si_screen
*sscreen
,
132 struct si_shader
*shader
,
133 LLVMTargetMachineRef tm
);
135 /* Ideally pass the sample mask input to the PS epilog as v13, which
136 * is its usual location, so that the shader doesn't have to add v_mov.
138 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
140 /* The VS location of the PrimitiveID input is the same in the epilog,
141 * so that the main shader part doesn't have to move it.
143 #define VS_EPILOG_PRIMID_LOC 2
145 #define PERSPECTIVE_BASE 0
146 #define LINEAR_BASE 9
148 #define SAMPLE_OFFSET 0
149 #define CENTER_OFFSET 2
150 #define CENTROID_OFSET 4
152 #define USE_SGPR_MAX_SUFFIX_LEN 5
153 #define CONST_ADDR_SPACE 2
154 #define LOCAL_ADDR_SPACE 3
155 #define USER_SGPR_ADDR_SPACE 8
159 #define SENDMSG_GS_DONE 3
161 #define SENDMSG_GS_OP_NOP (0 << 4)
162 #define SENDMSG_GS_OP_CUT (1 << 4)
163 #define SENDMSG_GS_OP_EMIT (2 << 4)
164 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
167 * Returns a unique index for a semantic name and index. The index must be
168 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
171 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
173 switch (semantic_name
) {
174 case TGSI_SEMANTIC_POSITION
:
176 case TGSI_SEMANTIC_PSIZE
:
178 case TGSI_SEMANTIC_CLIPDIST
:
181 case TGSI_SEMANTIC_GENERIC
:
185 /* same explanation as in the default statement,
186 * the only user hitting this is st/nine.
190 /* patch indices are completely separate and thus start from 0 */
191 case TGSI_SEMANTIC_TESSOUTER
:
193 case TGSI_SEMANTIC_TESSINNER
:
195 case TGSI_SEMANTIC_PATCH
:
199 /* Don't fail here. The result of this function is only used
200 * for LS, TCS, TES, and GS, where legacy GL semantics can't
201 * occur, but this function is called for all vertex shaders
202 * before it's known whether LS will be compiled or not.
209 * Get the value of a shader input parameter and extract a bitfield.
211 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
212 unsigned param
, unsigned rshift
,
215 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
216 LLVMValueRef value
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
219 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
220 value
= bitcast(&ctx
->radeon_bld
.soa
.bld_base
,
221 TGSI_TYPE_UNSIGNED
, value
);
224 value
= LLVMBuildLShr(gallivm
->builder
, value
,
225 lp_build_const_int32(gallivm
, rshift
), "");
227 if (rshift
+ bitwidth
< 32) {
228 unsigned mask
= (1 << bitwidth
) - 1;
229 value
= LLVMBuildAnd(gallivm
->builder
, value
,
230 lp_build_const_int32(gallivm
, mask
), "");
236 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
239 case TGSI_PROCESSOR_TESS_CTRL
:
240 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
242 case TGSI_PROCESSOR_TESS_EVAL
:
243 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
244 ctx
->param_tes_rel_patch_id
);
252 /* Tessellation shaders pass outputs to the next shader using LDS.
254 * LS outputs = TCS inputs
255 * TCS outputs = TES inputs
258 * - TCS inputs for patch 0
259 * - TCS inputs for patch 1
260 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
262 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
263 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
264 * - TCS outputs for patch 1
265 * - Per-patch TCS outputs for patch 1
266 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
267 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
270 * All three shaders VS(LS), TCS, TES share the same LDS space.
274 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
276 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
)
277 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
278 else if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
279 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
287 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
289 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
293 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
295 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
297 SI_PARAM_TCS_OUT_OFFSETS
,
303 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
305 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
307 SI_PARAM_TCS_OUT_OFFSETS
,
313 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
315 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
316 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
317 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
319 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
323 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
325 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
326 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
327 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
328 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
330 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
331 LLVMBuildMul(gallivm
->builder
, patch_stride
,
337 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
339 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
340 LLVMValueRef patch0_patch_data_offset
=
341 get_tcs_out_patch0_patch_data_offset(ctx
);
342 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
343 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
345 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
346 LLVMBuildMul(gallivm
->builder
, patch_stride
,
351 static void build_indexed_store(struct si_shader_context
*ctx
,
352 LLVMValueRef base_ptr
, LLVMValueRef index
,
355 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
356 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
357 LLVMValueRef indices
[2], pointer
;
359 indices
[0] = bld_base
->uint_bld
.zero
;
362 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
363 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
367 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
368 * It's equivalent to doing a load from &base_ptr[index].
370 * \param base_ptr Where the array starts.
371 * \param index The element index into the array.
373 static LLVMValueRef
build_indexed_load(struct si_shader_context
*ctx
,
374 LLVMValueRef base_ptr
, LLVMValueRef index
)
376 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
377 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
378 LLVMValueRef indices
[2], pointer
;
380 indices
[0] = bld_base
->uint_bld
.zero
;
383 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
384 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
388 * Do a load from &base_ptr[index], but also add a flag that it's loading
391 static LLVMValueRef
build_indexed_load_const(
392 struct si_shader_context
*ctx
,
393 LLVMValueRef base_ptr
, LLVMValueRef index
)
395 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
);
396 LLVMSetMetadata(result
, 1, ctx
->const_md
);
400 static LLVMValueRef
get_instance_index_for_fetch(
401 struct radeon_llvm_context
*radeon_bld
,
402 unsigned param_start_instance
, unsigned divisor
)
404 struct si_shader_context
*ctx
=
405 si_shader_context(&radeon_bld
->soa
.bld_base
);
406 struct gallivm_state
*gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
408 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
409 ctx
->param_instance_id
);
411 /* The division must be done before START_INSTANCE is added. */
413 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
414 lp_build_const_int32(gallivm
, divisor
), "");
416 return LLVMBuildAdd(gallivm
->builder
, result
,
417 LLVMGetParam(radeon_bld
->main_fn
, param_start_instance
), "");
420 static void declare_input_vs(
421 struct radeon_llvm_context
*radeon_bld
,
422 unsigned input_index
,
423 const struct tgsi_full_declaration
*decl
)
425 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
426 struct gallivm_state
*gallivm
= base
->gallivm
;
427 struct si_shader_context
*ctx
=
428 si_shader_context(&radeon_bld
->soa
.bld_base
);
430 ctx
->shader
->key
.vs
.prolog
.instance_divisors
[input_index
];
434 LLVMValueRef t_list_ptr
;
435 LLVMValueRef t_offset
;
437 LLVMValueRef attribute_offset
;
438 LLVMValueRef buffer_index
;
439 LLVMValueRef args
[3];
442 /* Load the T list */
443 t_list_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFERS
);
445 t_offset
= lp_build_const_int32(gallivm
, input_index
);
447 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
449 /* Build the attribute offset */
450 attribute_offset
= lp_build_const_int32(gallivm
, 0);
452 if (!ctx
->is_monolithic
) {
453 buffer_index
= LLVMGetParam(radeon_bld
->main_fn
,
454 ctx
->param_vertex_index0
+
456 } else if (divisor
) {
457 /* Build index from instance ID, start instance and divisor */
458 ctx
->shader
->info
.uses_instanceid
= true;
459 buffer_index
= get_instance_index_for_fetch(&ctx
->radeon_bld
,
460 SI_PARAM_START_INSTANCE
,
463 /* Load the buffer index for vertices. */
464 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
465 ctx
->param_vertex_id
);
466 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
467 SI_PARAM_BASE_VERTEX
);
468 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
472 args
[1] = attribute_offset
;
473 args
[2] = buffer_index
;
474 input
= lp_build_intrinsic(gallivm
->builder
,
475 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
476 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
478 /* Break up the vec4 into individual components */
479 for (chan
= 0; chan
< 4; chan
++) {
480 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
481 /* XXX: Use a helper function for this. There is one in
483 ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
484 LLVMBuildExtractElement(gallivm
->builder
,
485 input
, llvm_chan
, "");
489 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
492 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
495 return bld_base
->uint_bld
.zero
;
498 case TGSI_PROCESSOR_VERTEX
:
499 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
500 ctx
->param_vs_prim_id
);
501 case TGSI_PROCESSOR_TESS_CTRL
:
502 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
504 case TGSI_PROCESSOR_TESS_EVAL
:
505 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
506 ctx
->param_tes_patch_id
);
507 case TGSI_PROCESSOR_GEOMETRY
:
508 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
509 SI_PARAM_PRIMITIVE_ID
);
512 return bld_base
->uint_bld
.zero
;
517 * Return the value of tgsi_ind_register for indexing.
518 * This is the indirect index with the constant offset added to it.
520 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
521 const struct tgsi_ind_register
*ind
,
524 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
527 result
= ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
528 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
529 result
= LLVMBuildAdd(gallivm
->builder
, result
,
530 lp_build_const_int32(gallivm
, rel_index
), "");
535 * Calculate a dword address given an input or output register and a stride.
537 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
538 const struct tgsi_full_dst_register
*dst
,
539 const struct tgsi_full_src_register
*src
,
540 LLVMValueRef vertex_dw_stride
,
541 LLVMValueRef base_addr
)
543 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
544 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
545 ubyte
*name
, *index
, *array_first
;
547 struct tgsi_full_dst_register reg
;
549 /* Set the register description. The address computation is the same
550 * for sources and destinations. */
552 reg
.Register
.File
= src
->Register
.File
;
553 reg
.Register
.Index
= src
->Register
.Index
;
554 reg
.Register
.Indirect
= src
->Register
.Indirect
;
555 reg
.Register
.Dimension
= src
->Register
.Dimension
;
556 reg
.Indirect
= src
->Indirect
;
557 reg
.Dimension
= src
->Dimension
;
558 reg
.DimIndirect
= src
->DimIndirect
;
562 /* If the register is 2-dimensional (e.g. an array of vertices
563 * in a primitive), calculate the base address of the vertex. */
564 if (reg
.Register
.Dimension
) {
567 if (reg
.Dimension
.Indirect
)
568 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
569 reg
.Dimension
.Index
);
571 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
573 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
574 LLVMBuildMul(gallivm
->builder
, index
,
575 vertex_dw_stride
, ""), "");
578 /* Get information about the register. */
579 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
580 name
= info
->input_semantic_name
;
581 index
= info
->input_semantic_index
;
582 array_first
= info
->input_array_first
;
583 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
584 name
= info
->output_semantic_name
;
585 index
= info
->output_semantic_index
;
586 array_first
= info
->output_array_first
;
592 if (reg
.Register
.Indirect
) {
593 /* Add the relative address of the element. */
594 LLVMValueRef ind_index
;
596 if (reg
.Indirect
.ArrayID
)
597 first
= array_first
[reg
.Indirect
.ArrayID
];
599 first
= reg
.Register
.Index
;
601 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
602 reg
.Register
.Index
- first
);
604 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
605 LLVMBuildMul(gallivm
->builder
, ind_index
,
606 lp_build_const_int32(gallivm
, 4), ""), "");
608 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
610 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
611 index
[reg
.Register
.Index
]);
614 /* Add the base address of the element. */
615 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
616 lp_build_const_int32(gallivm
, param
* 4), "");
622 * \param type output value type
623 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
624 * \param dw_addr address in dwords
626 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
627 enum tgsi_opcode_type type
, unsigned swizzle
,
628 LLVMValueRef dw_addr
)
630 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
631 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
635 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
637 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
638 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
640 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
644 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
645 lp_build_const_int32(gallivm
, swizzle
));
647 value
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
648 if (type
== TGSI_TYPE_DOUBLE
) {
650 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
651 lp_build_const_int32(gallivm
, swizzle
+ 1));
652 value2
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
653 return radeon_llvm_emit_fetch_double(bld_base
, value
, value2
);
656 return LLVMBuildBitCast(gallivm
->builder
, value
,
657 tgsi2llvmtype(bld_base
, type
), "");
663 * \param swizzle offset (typically 0..3)
664 * \param dw_addr address in dwords
665 * \param value value to store
667 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
668 unsigned swizzle
, LLVMValueRef dw_addr
,
671 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
672 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
674 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
675 lp_build_const_int32(gallivm
, swizzle
));
677 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
678 build_indexed_store(ctx
, ctx
->lds
,
682 static LLVMValueRef
fetch_input_tcs(
683 struct lp_build_tgsi_context
*bld_base
,
684 const struct tgsi_full_src_register
*reg
,
685 enum tgsi_opcode_type type
, unsigned swizzle
)
687 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
688 LLVMValueRef dw_addr
, stride
;
690 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
691 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
692 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
694 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
697 static LLVMValueRef
fetch_output_tcs(
698 struct lp_build_tgsi_context
*bld_base
,
699 const struct tgsi_full_src_register
*reg
,
700 enum tgsi_opcode_type type
, unsigned swizzle
)
702 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
703 LLVMValueRef dw_addr
, stride
;
705 if (reg
->Register
.Dimension
) {
706 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
707 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
708 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
710 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
711 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
714 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
717 static LLVMValueRef
fetch_input_tes(
718 struct lp_build_tgsi_context
*bld_base
,
719 const struct tgsi_full_src_register
*reg
,
720 enum tgsi_opcode_type type
, unsigned swizzle
)
722 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
723 LLVMValueRef dw_addr
, stride
;
725 if (reg
->Register
.Dimension
) {
726 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
727 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
728 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
730 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
731 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
734 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
737 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
738 const struct tgsi_full_instruction
*inst
,
739 const struct tgsi_opcode_info
*info
,
742 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
743 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
745 LLVMValueRef dw_addr
, stride
;
747 /* Only handle per-patch and per-vertex outputs here.
748 * Vectors will be lowered to scalars and this function will be called again.
750 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
751 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
752 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
756 if (reg
->Register
.Dimension
) {
757 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
758 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
759 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
761 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
762 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
765 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
766 LLVMValueRef value
= dst
[chan_index
];
768 if (inst
->Instruction
.Saturate
)
769 value
= radeon_llvm_saturate(bld_base
, value
);
771 lds_store(bld_base
, chan_index
, dw_addr
, value
);
775 static LLVMValueRef
fetch_input_gs(
776 struct lp_build_tgsi_context
*bld_base
,
777 const struct tgsi_full_src_register
*reg
,
778 enum tgsi_opcode_type type
,
781 struct lp_build_context
*base
= &bld_base
->base
;
782 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
783 struct si_shader
*shader
= ctx
->shader
;
784 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
785 struct gallivm_state
*gallivm
= base
->gallivm
;
786 LLVMValueRef vtx_offset
;
787 LLVMValueRef args
[9];
788 unsigned vtx_offset_param
;
789 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
790 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
791 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
795 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
796 return get_primitive_id(bld_base
, swizzle
);
798 if (!reg
->Register
.Dimension
)
802 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
804 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
805 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
807 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
811 /* Get the vertex offset parameter */
812 vtx_offset_param
= reg
->Dimension
.Index
;
813 if (vtx_offset_param
< 2) {
814 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
816 assert(vtx_offset_param
< 6);
817 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
819 vtx_offset
= lp_build_mul_imm(uint
,
820 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
824 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
825 args
[0] = ctx
->esgs_ring
;
826 args
[1] = vtx_offset
;
827 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
828 args
[3] = uint
->zero
;
829 args
[4] = uint
->one
; /* OFFEN */
830 args
[5] = uint
->zero
; /* IDXEN */
831 args
[6] = uint
->one
; /* GLC */
832 args
[7] = uint
->zero
; /* SLC */
833 args
[8] = uint
->zero
; /* TFE */
835 value
= lp_build_intrinsic(gallivm
->builder
,
836 "llvm.SI.buffer.load.dword.i32.i32",
838 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
839 if (type
== TGSI_TYPE_DOUBLE
) {
841 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
842 value2
= lp_build_intrinsic(gallivm
->builder
,
843 "llvm.SI.buffer.load.dword.i32.i32",
845 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
846 return radeon_llvm_emit_fetch_double(bld_base
,
849 return LLVMBuildBitCast(gallivm
->builder
,
851 tgsi2llvmtype(bld_base
, type
), "");
854 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
856 switch (interpolate
) {
857 case TGSI_INTERPOLATE_CONSTANT
:
860 case TGSI_INTERPOLATE_LINEAR
:
861 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
862 return SI_PARAM_LINEAR_SAMPLE
;
863 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
864 return SI_PARAM_LINEAR_CENTROID
;
866 return SI_PARAM_LINEAR_CENTER
;
868 case TGSI_INTERPOLATE_COLOR
:
869 case TGSI_INTERPOLATE_PERSPECTIVE
:
870 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
871 return SI_PARAM_PERSP_SAMPLE
;
872 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
873 return SI_PARAM_PERSP_CENTROID
;
875 return SI_PARAM_PERSP_CENTER
;
878 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
883 /* This shouldn't be used by explicit INTERP opcodes. */
884 static unsigned select_interp_param(struct si_shader_context
*ctx
,
887 if (!ctx
->shader
->key
.ps
.prolog
.force_persample_interp
||
891 /* If the shader doesn't use center/centroid, just return the parameter.
893 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
894 * switch between center/centroid and sample without shader changes.
897 case SI_PARAM_PERSP_CENTROID
:
898 case SI_PARAM_PERSP_CENTER
:
899 return SI_PARAM_PERSP_SAMPLE
;
901 case SI_PARAM_LINEAR_CENTROID
:
902 case SI_PARAM_LINEAR_CENTER
:
903 return SI_PARAM_LINEAR_SAMPLE
;
911 * Interpolate a fragment shader input.
914 * @param input_index index of the input in hardware
915 * @param semantic_name TGSI_SEMANTIC_*
916 * @param semantic_index semantic index
917 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
918 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
919 * @param interp_param interpolation weights (i,j)
920 * @param prim_mask SI_PARAM_PRIM_MASK
921 * @param face SI_PARAM_FRONT_FACE
922 * @param result the return value (4 components)
924 static void interp_fs_input(struct si_shader_context
*ctx
,
925 unsigned input_index
,
926 unsigned semantic_name
,
927 unsigned semantic_index
,
928 unsigned num_interp_inputs
,
929 unsigned colors_read_mask
,
930 LLVMValueRef interp_param
,
931 LLVMValueRef prim_mask
,
933 LLVMValueRef result
[4])
935 struct lp_build_context
*base
= &ctx
->radeon_bld
.soa
.bld_base
.base
;
936 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
937 struct gallivm_state
*gallivm
= base
->gallivm
;
938 const char *intr_name
;
939 LLVMValueRef attr_number
;
943 attr_number
= lp_build_const_int32(gallivm
, input_index
);
945 /* fs.constant returns the param from the middle vertex, so it's not
946 * really useful for flat shading. It's meant to be used for custom
947 * interpolation (but the intrinsic can't fetch from the other two
950 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
951 * to do the right thing. The only reason we use fs.constant is that
952 * fs.interp cannot be used on integers, because they can be equal
955 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
957 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
958 ctx
->shader
->key
.ps
.prolog
.color_two_side
) {
959 LLVMValueRef args
[4];
960 LLVMValueRef is_face_positive
;
961 LLVMValueRef back_attr_number
;
963 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
964 * otherwise it's at offset "num_inputs".
966 unsigned back_attr_offset
= num_interp_inputs
;
967 if (semantic_index
== 1 && colors_read_mask
& 0xf)
968 back_attr_offset
+= 1;
970 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
972 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
973 face
, uint
->zero
, "");
976 args
[3] = interp_param
;
977 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
978 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
979 LLVMValueRef front
, back
;
982 args
[1] = attr_number
;
983 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
984 ctx
->f32
, args
, args
[3] ? 4 : 3,
985 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
987 args
[1] = back_attr_number
;
988 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
989 ctx
->f32
, args
, args
[3] ? 4 : 3,
990 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
992 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
998 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
999 LLVMValueRef args
[4];
1001 args
[0] = uint
->zero
;
1002 args
[1] = attr_number
;
1003 args
[2] = prim_mask
;
1004 args
[3] = interp_param
;
1005 result
[0] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1006 ctx
->f32
, args
, args
[3] ? 4 : 3,
1007 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1009 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1010 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1012 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1013 LLVMValueRef args
[4];
1014 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1016 args
[0] = llvm_chan
;
1017 args
[1] = attr_number
;
1018 args
[2] = prim_mask
;
1019 args
[3] = interp_param
;
1020 result
[chan
] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1021 ctx
->f32
, args
, args
[3] ? 4 : 3,
1022 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1027 static void declare_input_fs(
1028 struct radeon_llvm_context
*radeon_bld
,
1029 unsigned input_index
,
1030 const struct tgsi_full_declaration
*decl
)
1032 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
1033 struct si_shader_context
*ctx
=
1034 si_shader_context(&radeon_bld
->soa
.bld_base
);
1035 struct si_shader
*shader
= ctx
->shader
;
1036 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
1037 LLVMValueRef interp_param
= NULL
;
1038 int interp_param_idx
;
1040 /* Get colors from input VGPRs (set by the prolog). */
1041 if (!ctx
->is_monolithic
&&
1042 decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1043 unsigned i
= decl
->Semantic
.Index
;
1044 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1045 unsigned mask
= colors_read
>> (i
* 4);
1046 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1047 (i
? util_bitcount(colors_read
& 0xf) : 0);
1049 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
1050 mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1051 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
1052 mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1053 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
1054 mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1055 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
1056 mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1060 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1061 decl
->Interp
.Location
);
1062 if (interp_param_idx
== -1)
1064 else if (interp_param_idx
) {
1065 interp_param_idx
= select_interp_param(ctx
,
1067 interp_param
= LLVMGetParam(main_fn
, interp_param_idx
);
1070 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1071 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1072 shader
->selector
->info
.colors_read
, interp_param
,
1073 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1074 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1075 &radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)]);
1078 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1080 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1081 SI_PARAM_ANCILLARY
, 8, 4);
1085 * Load a dword from a constant buffer.
1087 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1088 LLVMValueRef offset
, LLVMTypeRef return_type
)
1090 LLVMValueRef args
[2] = {resource
, offset
};
1092 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1093 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1096 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1098 struct si_shader_context
*ctx
=
1099 si_shader_context(&radeon_bld
->soa
.bld_base
);
1100 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1101 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1102 LLVMBuilderRef builder
= gallivm
->builder
;
1103 LLVMValueRef desc
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1104 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1105 LLVMValueRef resource
= build_indexed_load_const(ctx
, desc
, buf_index
);
1107 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1108 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1109 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1111 LLVMValueRef pos
[4] = {
1112 buffer_load_const(builder
, resource
, offset0
, ctx
->f32
),
1113 buffer_load_const(builder
, resource
, offset1
, ctx
->f32
),
1114 lp_build_const_float(gallivm
, 0),
1115 lp_build_const_float(gallivm
, 0)
1118 return lp_build_gather_values(gallivm
, pos
, 4);
1121 static void declare_system_value(
1122 struct radeon_llvm_context
*radeon_bld
,
1124 const struct tgsi_full_declaration
*decl
)
1126 struct si_shader_context
*ctx
=
1127 si_shader_context(&radeon_bld
->soa
.bld_base
);
1128 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1129 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1130 LLVMValueRef value
= 0;
1132 switch (decl
->Semantic
.Name
) {
1133 case TGSI_SEMANTIC_INSTANCEID
:
1134 value
= LLVMGetParam(radeon_bld
->main_fn
,
1135 ctx
->param_instance_id
);
1138 case TGSI_SEMANTIC_VERTEXID
:
1139 value
= LLVMBuildAdd(gallivm
->builder
,
1140 LLVMGetParam(radeon_bld
->main_fn
,
1141 ctx
->param_vertex_id
),
1142 LLVMGetParam(radeon_bld
->main_fn
,
1143 SI_PARAM_BASE_VERTEX
), "");
1146 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1147 value
= LLVMGetParam(radeon_bld
->main_fn
,
1148 ctx
->param_vertex_id
);
1151 case TGSI_SEMANTIC_BASEVERTEX
:
1152 value
= LLVMGetParam(radeon_bld
->main_fn
,
1153 SI_PARAM_BASE_VERTEX
);
1156 case TGSI_SEMANTIC_INVOCATIONID
:
1157 if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1158 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1159 else if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1160 value
= LLVMGetParam(radeon_bld
->main_fn
,
1161 SI_PARAM_GS_INSTANCE_ID
);
1163 assert(!"INVOCATIONID not implemented");
1166 case TGSI_SEMANTIC_POSITION
:
1168 LLVMValueRef pos
[4] = {
1169 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1170 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1171 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1172 lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
, TGSI_OPCODE_RCP
,
1173 LLVMGetParam(radeon_bld
->main_fn
,
1174 SI_PARAM_POS_W_FLOAT
)),
1176 value
= lp_build_gather_values(gallivm
, pos
, 4);
1180 case TGSI_SEMANTIC_FACE
:
1181 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1184 case TGSI_SEMANTIC_SAMPLEID
:
1185 value
= get_sample_id(radeon_bld
);
1188 case TGSI_SEMANTIC_SAMPLEPOS
: {
1189 LLVMValueRef pos
[4] = {
1190 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1191 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1192 lp_build_const_float(gallivm
, 0),
1193 lp_build_const_float(gallivm
, 0)
1195 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1196 TGSI_OPCODE_FRC
, pos
[0]);
1197 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1198 TGSI_OPCODE_FRC
, pos
[1]);
1199 value
= lp_build_gather_values(gallivm
, pos
, 4);
1203 case TGSI_SEMANTIC_SAMPLEMASK
:
1204 /* This can only occur with the OpenGL Core profile, which
1205 * doesn't support smoothing.
1207 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1210 case TGSI_SEMANTIC_TESSCOORD
:
1212 LLVMValueRef coord
[4] = {
1213 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_u
),
1214 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_v
),
1219 /* For triangles, the vector should be (u, v, 1-u-v). */
1220 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1221 PIPE_PRIM_TRIANGLES
)
1222 coord
[2] = lp_build_sub(bld
, bld
->one
,
1223 lp_build_add(bld
, coord
[0], coord
[1]));
1225 value
= lp_build_gather_values(gallivm
, coord
, 4);
1229 case TGSI_SEMANTIC_VERTICESIN
:
1230 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1233 case TGSI_SEMANTIC_TESSINNER
:
1234 case TGSI_SEMANTIC_TESSOUTER
:
1236 LLVMValueRef dw_addr
;
1237 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1239 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1240 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1241 lp_build_const_int32(gallivm
, param
* 4), "");
1243 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1248 case TGSI_SEMANTIC_PRIMID
:
1249 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1253 assert(!"unknown system value");
1257 radeon_bld
->system_values
[index
] = value
;
1260 static LLVMValueRef
fetch_constant(
1261 struct lp_build_tgsi_context
*bld_base
,
1262 const struct tgsi_full_src_register
*reg
,
1263 enum tgsi_opcode_type type
,
1266 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1267 struct lp_build_context
*base
= &bld_base
->base
;
1268 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1271 LLVMValueRef addr
, bufp
;
1272 LLVMValueRef result
;
1274 if (swizzle
== LP_CHAN_ALL
) {
1276 LLVMValueRef values
[4];
1277 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1278 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1280 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1283 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1284 idx
= reg
->Register
.Index
* 4 + swizzle
;
1286 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1287 if (type
!= TGSI_TYPE_DOUBLE
)
1288 return bitcast(bld_base
, type
, ctx
->constants
[buf
][idx
]);
1290 return radeon_llvm_emit_fetch_double(bld_base
,
1291 ctx
->constants
[buf
][idx
],
1292 ctx
->constants
[buf
][idx
+ 1]);
1296 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1297 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1299 index
= get_indirect_index(ctx
, ®
->DimIndirect
,
1300 reg
->Dimension
.Index
);
1301 bufp
= build_indexed_load_const(ctx
, ptr
, index
);
1303 bufp
= ctx
->const_buffers
[buf
];
1305 addr
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1306 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1307 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1308 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1309 lp_build_const_int32(base
->gallivm
, idx
* 4));
1311 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1314 if (type
!= TGSI_TYPE_DOUBLE
)
1315 result
= bitcast(bld_base
, type
, result
);
1317 LLVMValueRef addr2
, result2
;
1318 addr2
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1319 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1320 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1321 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1322 lp_build_const_int32(base
->gallivm
, idx
* 4));
1324 result2
= buffer_load_const(base
->gallivm
->builder
, ctx
->const_buffers
[buf
],
1327 result
= radeon_llvm_emit_fetch_double(bld_base
,
1333 /* Upper 16 bits must be zero. */
1334 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1335 LLVMValueRef val
[2])
1337 return LLVMBuildOr(gallivm
->builder
, val
[0],
1338 LLVMBuildShl(gallivm
->builder
, val
[1],
1339 lp_build_const_int32(gallivm
, 16),
1343 /* Upper 16 bits are ignored and will be dropped. */
1344 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1345 LLVMValueRef val
[2])
1347 LLVMValueRef v
[2] = {
1348 LLVMBuildAnd(gallivm
->builder
, val
[0],
1349 lp_build_const_int32(gallivm
, 0xffff), ""),
1352 return si_llvm_pack_two_int16(gallivm
, v
);
1355 /* Initialize arguments for the shader export intrinsic */
1356 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1357 LLVMValueRef
*values
,
1361 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1362 struct lp_build_context
*uint
=
1363 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1364 struct lp_build_context
*base
= &bld_base
->base
;
1365 struct gallivm_state
*gallivm
= base
->gallivm
;
1366 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1367 LLVMValueRef val
[4];
1368 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1372 /* Default is 0xf. Adjusted below depending on the format. */
1373 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1375 /* Specify whether the EXEC mask represents the valid mask */
1376 args
[1] = uint
->zero
;
1378 /* Specify whether this is the last export */
1379 args
[2] = uint
->zero
;
1381 /* Specify the target we are exporting */
1382 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1384 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1385 const union si_shader_key
*key
= &ctx
->shader
->key
;
1386 unsigned col_formats
= key
->ps
.epilog
.spi_shader_col_format
;
1387 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1389 assert(cbuf
>= 0 && cbuf
< 8);
1390 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1391 is_int8
= (key
->ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1394 args
[4] = uint
->zero
; /* COMPR flag */
1395 args
[5] = base
->undef
;
1396 args
[6] = base
->undef
;
1397 args
[7] = base
->undef
;
1398 args
[8] = base
->undef
;
1400 switch (spi_shader_col_format
) {
1401 case V_028714_SPI_SHADER_ZERO
:
1402 args
[0] = uint
->zero
; /* writemask */
1403 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1406 case V_028714_SPI_SHADER_32_R
:
1407 args
[0] = uint
->one
; /* writemask */
1408 args
[5] = values
[0];
1411 case V_028714_SPI_SHADER_32_GR
:
1412 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1413 args
[5] = values
[0];
1414 args
[6] = values
[1];
1417 case V_028714_SPI_SHADER_32_AR
:
1418 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1419 args
[5] = values
[0];
1420 args
[8] = values
[3];
1423 case V_028714_SPI_SHADER_FP16_ABGR
:
1424 args
[4] = uint
->one
; /* COMPR flag */
1426 for (chan
= 0; chan
< 2; chan
++) {
1427 LLVMValueRef pack_args
[2] = {
1429 values
[2 * chan
+ 1]
1431 LLVMValueRef packed
;
1433 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1435 ctx
->i32
, pack_args
, 2,
1436 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1438 LLVMBuildBitCast(base
->gallivm
->builder
,
1439 packed
, ctx
->f32
, "");
1443 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1444 for (chan
= 0; chan
< 4; chan
++) {
1445 val
[chan
] = radeon_llvm_saturate(bld_base
, values
[chan
]);
1446 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1447 lp_build_const_float(gallivm
, 65535), "");
1448 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1449 lp_build_const_float(gallivm
, 0.5), "");
1450 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1454 args
[4] = uint
->one
; /* COMPR flag */
1455 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1456 si_llvm_pack_two_int16(gallivm
, val
));
1457 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1458 si_llvm_pack_two_int16(gallivm
, val
+2));
1461 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1462 for (chan
= 0; chan
< 4; chan
++) {
1463 /* Clamp between [-1, 1]. */
1464 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1466 lp_build_const_float(gallivm
, 1));
1467 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1469 lp_build_const_float(gallivm
, -1));
1470 /* Convert to a signed integer in [-32767, 32767]. */
1471 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1472 lp_build_const_float(gallivm
, 32767), "");
1473 /* If positive, add 0.5, else add -0.5. */
1474 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1475 LLVMBuildSelect(builder
,
1476 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1477 val
[chan
], base
->zero
, ""),
1478 lp_build_const_float(gallivm
, 0.5),
1479 lp_build_const_float(gallivm
, -0.5), ""), "");
1480 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1483 args
[4] = uint
->one
; /* COMPR flag */
1484 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1485 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1486 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1487 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1490 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1491 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1494 for (chan
= 0; chan
< 4; chan
++) {
1495 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1496 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1500 args
[4] = uint
->one
; /* COMPR flag */
1501 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1502 si_llvm_pack_two_int16(gallivm
, val
));
1503 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1504 si_llvm_pack_two_int16(gallivm
, val
+2));
1508 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1509 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1511 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
1514 for (chan
= 0; chan
< 4; chan
++) {
1515 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1516 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1519 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1524 args
[4] = uint
->one
; /* COMPR flag */
1525 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1526 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1527 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1528 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1532 case V_028714_SPI_SHADER_32_ABGR
:
1533 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1538 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1541 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1542 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1544 if (ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
1545 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1546 SI_PARAM_ALPHA_REF
);
1548 LLVMValueRef alpha_pass
=
1549 lp_build_cmp(&bld_base
->base
,
1550 ctx
->shader
->key
.ps
.epilog
.alpha_func
,
1553 lp_build_select(&bld_base
->base
,
1555 lp_build_const_float(gallivm
, 1.0f
),
1556 lp_build_const_float(gallivm
, -1.0f
));
1558 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
1559 ctx
->voidt
, &arg
, 1, 0);
1561 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kilp",
1562 ctx
->voidt
, NULL
, 0, 0);
1566 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1568 unsigned samplemask_param
)
1570 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1571 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1572 LLVMValueRef coverage
;
1574 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1575 coverage
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1577 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1579 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1581 &coverage
, 1, LLVMReadNoneAttribute
);
1583 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1586 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1587 lp_build_const_float(gallivm
,
1588 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1590 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1593 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
1594 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1596 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1597 struct lp_build_context
*base
= &bld_base
->base
;
1598 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1601 unsigned const_chan
;
1602 LLVMValueRef base_elt
;
1603 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1604 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1605 LLVMValueRef const_resource
= build_indexed_load_const(ctx
, ptr
, constbuf_index
);
1607 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1608 LLVMValueRef
*args
= pos
[2 + reg_index
];
1613 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1615 /* Compute dot products of position and user clip plane vectors */
1616 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1617 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1618 args
[1] = lp_build_const_int32(base
->gallivm
,
1619 ((reg_index
* 4 + chan
) * 4 +
1621 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1624 lp_build_add(base
, args
[5 + chan
],
1625 lp_build_mul(base
, base_elt
,
1626 out_elts
[const_chan
]));
1630 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1631 args
[1] = uint
->zero
;
1632 args
[2] = uint
->zero
;
1633 args
[3] = lp_build_const_int32(base
->gallivm
,
1634 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1635 args
[4] = uint
->zero
;
1639 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1643 if (so
->num_outputs
)
1644 fprintf(stderr
, "STREAMOUT\n");
1646 for (i
= 0; i
< so
->num_outputs
; i
++) {
1647 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1648 so
->output
[i
].start_component
;
1649 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1650 i
, so
->output
[i
].output_buffer
,
1651 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1652 so
->output
[i
].register_index
,
1653 mask
& 1 ? "x" : "",
1654 mask
& 2 ? "y" : "",
1655 mask
& 4 ? "z" : "",
1656 mask
& 8 ? "w" : "");
1660 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1661 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1662 * or v4i32 (num_channels=3,4). */
1663 static void build_tbuffer_store(struct si_shader_context
*ctx
,
1666 unsigned num_channels
,
1668 LLVMValueRef soffset
,
1669 unsigned inst_offset
,
1678 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1679 LLVMValueRef args
[] = {
1682 LLVMConstInt(ctx
->i32
, num_channels
, 0),
1685 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1686 LLVMConstInt(ctx
->i32
, dfmt
, 0),
1687 LLVMConstInt(ctx
->i32
, nfmt
, 0),
1688 LLVMConstInt(ctx
->i32
, offen
, 0),
1689 LLVMConstInt(ctx
->i32
, idxen
, 0),
1690 LLVMConstInt(ctx
->i32
, glc
, 0),
1691 LLVMConstInt(ctx
->i32
, slc
, 0),
1692 LLVMConstInt(ctx
->i32
, tfe
, 0)
1695 /* The instruction offset field has 12 bits */
1696 assert(offen
|| inst_offset
< (1 << 12));
1698 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1699 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1700 const char *types
[] = {"i32", "v2i32", "v4i32"};
1702 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1704 lp_build_intrinsic(gallivm
->builder
, name
, ctx
->voidt
,
1705 args
, Elements(args
), 0);
1708 static void build_tbuffer_store_dwords(struct si_shader_context
*ctx
,
1711 unsigned num_channels
,
1713 LLVMValueRef soffset
,
1714 unsigned inst_offset
)
1716 static unsigned dfmt
[] = {
1717 V_008F0C_BUF_DATA_FORMAT_32
,
1718 V_008F0C_BUF_DATA_FORMAT_32_32
,
1719 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1720 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1722 assert(num_channels
>= 1 && num_channels
<= 4);
1724 build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1725 inst_offset
, dfmt
[num_channels
-1],
1726 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1729 /* On SI, the vertex shader is responsible for writing streamout data
1731 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
1732 struct si_shader_output_values
*outputs
,
1735 struct pipe_stream_output_info
*so
= &ctx
->shader
->selector
->so
;
1736 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1737 LLVMBuilderRef builder
= gallivm
->builder
;
1739 struct lp_build_if_state if_ctx
;
1741 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1742 LLVMValueRef so_vtx_count
=
1743 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
1745 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", ctx
->i32
,
1746 NULL
, 0, LLVMReadNoneAttribute
);
1748 /* can_emit = tid < so_vtx_count; */
1749 LLVMValueRef can_emit
=
1750 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1752 LLVMValueRef stream_id
=
1753 unpack_param(ctx
, ctx
->param_streamout_config
, 24, 2);
1755 /* Emit the streamout code conditionally. This actually avoids
1756 * out-of-bounds buffer access. The hw tells us via the SGPR
1757 * (so_vtx_count) which threads are allowed to emit streamout data. */
1758 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1760 /* The buffer offset is computed as follows:
1761 * ByteOffset = streamout_offset[buffer_id]*4 +
1762 * (streamout_write_index + thread_id)*stride[buffer_id] +
1766 LLVMValueRef so_write_index
=
1767 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1768 ctx
->param_streamout_write_index
);
1770 /* Compute (streamout_write_index + thread_id). */
1771 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1773 /* Compute the write offset for each enabled buffer. */
1774 LLVMValueRef so_write_offset
[4] = {};
1775 for (i
= 0; i
< 4; i
++) {
1779 LLVMValueRef so_offset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1780 ctx
->param_streamout_offset
[i
]);
1781 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1783 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1784 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
1785 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1788 /* Write streamout data. */
1789 for (i
= 0; i
< so
->num_outputs
; i
++) {
1790 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1791 unsigned reg
= so
->output
[i
].register_index
;
1792 unsigned start
= so
->output
[i
].start_component
;
1793 unsigned num_comps
= so
->output
[i
].num_components
;
1794 unsigned stream
= so
->output
[i
].stream
;
1795 LLVMValueRef out
[4];
1796 struct lp_build_if_state if_ctx_stream
;
1798 assert(num_comps
&& num_comps
<= 4);
1799 if (!num_comps
|| num_comps
> 4)
1805 /* Load the output as int. */
1806 for (j
= 0; j
< num_comps
; j
++) {
1807 out
[j
] = LLVMBuildBitCast(builder
,
1808 outputs
[reg
].values
[start
+j
],
1812 /* Pack the output. */
1813 LLVMValueRef vdata
= NULL
;
1815 switch (num_comps
) {
1816 case 1: /* as i32 */
1819 case 2: /* as v2i32 */
1820 case 3: /* as v4i32 (aligned to 4) */
1821 case 4: /* as v4i32 */
1822 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
1823 for (j
= 0; j
< num_comps
; j
++) {
1824 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1825 LLVMConstInt(ctx
->i32
, j
, 0), "");
1830 LLVMValueRef can_emit_stream
=
1831 LLVMBuildICmp(builder
, LLVMIntEQ
,
1833 lp_build_const_int32(gallivm
, stream
), "");
1835 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1836 build_tbuffer_store_dwords(ctx
, ctx
->so_buffers
[buf_idx
],
1838 so_write_offset
[buf_idx
],
1839 LLVMConstInt(ctx
->i32
, 0, 0),
1840 so
->output
[i
].dst_offset
*4);
1841 lp_build_endif(&if_ctx_stream
);
1844 lp_build_endif(&if_ctx
);
1848 /* Generate export instructions for hardware VS shader stage */
1849 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1850 struct si_shader_output_values
*outputs
,
1853 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1854 struct si_shader
*shader
= ctx
->shader
;
1855 struct lp_build_context
*base
= &bld_base
->base
;
1856 struct lp_build_context
*uint
=
1857 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1858 LLVMValueRef args
[9];
1859 LLVMValueRef pos_args
[4][9] = { { 0 } };
1860 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1861 unsigned semantic_name
, semantic_index
;
1863 unsigned param_count
= 0;
1867 if (outputs
&& ctx
->shader
->selector
->so
.num_outputs
) {
1868 si_llvm_emit_streamout(ctx
, outputs
, noutput
);
1871 for (i
= 0; i
< noutput
; i
++) {
1872 semantic_name
= outputs
[i
].name
;
1873 semantic_index
= outputs
[i
].sid
;
1876 /* Select the correct target */
1877 switch(semantic_name
) {
1878 case TGSI_SEMANTIC_PSIZE
:
1879 psize_value
= outputs
[i
].values
[0];
1881 case TGSI_SEMANTIC_EDGEFLAG
:
1882 edgeflag_value
= outputs
[i
].values
[0];
1884 case TGSI_SEMANTIC_LAYER
:
1885 layer_value
= outputs
[i
].values
[0];
1886 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1887 goto handle_semantic
;
1888 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1889 viewport_index_value
= outputs
[i
].values
[0];
1890 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1891 goto handle_semantic
;
1892 case TGSI_SEMANTIC_POSITION
:
1893 target
= V_008DFC_SQ_EXP_POS
;
1895 case TGSI_SEMANTIC_COLOR
:
1896 case TGSI_SEMANTIC_BCOLOR
:
1897 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1898 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
1899 shader
->info
.vs_output_param_offset
[i
] = param_count
;
1902 case TGSI_SEMANTIC_CLIPDIST
:
1903 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1905 case TGSI_SEMANTIC_CLIPVERTEX
:
1906 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1908 case TGSI_SEMANTIC_PRIMID
:
1909 case TGSI_SEMANTIC_FOG
:
1910 case TGSI_SEMANTIC_TEXCOORD
:
1911 case TGSI_SEMANTIC_GENERIC
:
1912 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1913 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
1914 shader
->info
.vs_output_param_offset
[i
] = param_count
;
1920 "Warning: SI unhandled vs output type:%d\n",
1924 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1926 if (target
>= V_008DFC_SQ_EXP_POS
&&
1927 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1928 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1929 args
, sizeof(args
));
1931 lp_build_intrinsic(base
->gallivm
->builder
,
1932 "llvm.SI.export", ctx
->voidt
,
1936 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1937 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1938 goto handle_semantic
;
1942 shader
->info
.nr_param_exports
= param_count
;
1944 /* We need to add the position output manually if it's missing. */
1945 if (!pos_args
[0][0]) {
1946 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1947 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1948 pos_args
[0][2] = uint
->zero
; /* last export? */
1949 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1950 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1951 pos_args
[0][5] = base
->zero
; /* X */
1952 pos_args
[0][6] = base
->zero
; /* Y */
1953 pos_args
[0][7] = base
->zero
; /* Z */
1954 pos_args
[0][8] = base
->one
; /* W */
1957 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1958 if (shader
->selector
->info
.writes_psize
||
1959 shader
->selector
->info
.writes_edgeflag
||
1960 shader
->selector
->info
.writes_viewport_index
||
1961 shader
->selector
->info
.writes_layer
) {
1962 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1963 shader
->selector
->info
.writes_psize
|
1964 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1965 (shader
->selector
->info
.writes_layer
<< 2) |
1966 (shader
->selector
->info
.writes_viewport_index
<< 3));
1967 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1968 pos_args
[1][2] = uint
->zero
; /* last export? */
1969 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1970 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1971 pos_args
[1][5] = base
->zero
; /* X */
1972 pos_args
[1][6] = base
->zero
; /* Y */
1973 pos_args
[1][7] = base
->zero
; /* Z */
1974 pos_args
[1][8] = base
->zero
; /* W */
1976 if (shader
->selector
->info
.writes_psize
)
1977 pos_args
[1][5] = psize_value
;
1979 if (shader
->selector
->info
.writes_edgeflag
) {
1980 /* The output is a float, but the hw expects an integer
1981 * with the first bit containing the edge flag. */
1982 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1985 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1987 bld_base
->int_bld
.one
);
1989 /* The LLVM intrinsic expects a float. */
1990 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1995 if (shader
->selector
->info
.writes_layer
)
1996 pos_args
[1][7] = layer_value
;
1998 if (shader
->selector
->info
.writes_viewport_index
)
1999 pos_args
[1][8] = viewport_index_value
;
2002 for (i
= 0; i
< 4; i
++)
2004 shader
->info
.nr_pos_exports
++;
2007 for (i
= 0; i
< 4; i
++) {
2008 if (!pos_args
[i
][0])
2011 /* Specify the target we are exporting */
2012 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
2014 if (pos_idx
== shader
->info
.nr_pos_exports
)
2015 /* Specify that this is the last export */
2016 pos_args
[i
][2] = uint
->one
;
2018 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2019 ctx
->voidt
, pos_args
[i
], 9, 0);
2023 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2024 LLVMValueRef rel_patch_id
,
2025 LLVMValueRef invocation_id
,
2026 LLVMValueRef tcs_out_current_patch_data_offset
)
2028 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2029 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2030 struct si_shader
*shader
= ctx
->shader
;
2031 unsigned tess_inner_index
, tess_outer_index
;
2032 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2033 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
;
2034 unsigned stride
, outer_comps
, inner_comps
, i
;
2035 struct lp_build_if_state if_ctx
;
2037 /* Do this only for invocation 0, because the tess levels are per-patch,
2040 * This can't jump, because invocation 0 executes this. It should
2041 * at least mask out the loads and stores for other invocations.
2043 lp_build_if(&if_ctx
, gallivm
,
2044 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2045 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2047 /* Determine the layout of one tess factor element in the buffer. */
2048 switch (shader
->key
.tcs
.epilog
.prim_mode
) {
2049 case PIPE_PRIM_LINES
:
2050 stride
= 2; /* 2 dwords, 1 vec2 store */
2054 case PIPE_PRIM_TRIANGLES
:
2055 stride
= 4; /* 4 dwords, 1 vec4 store */
2059 case PIPE_PRIM_QUADS
:
2060 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2069 /* Load tess_inner and tess_outer from LDS.
2070 * Any invocation can write them, so we can't get them from a temporary.
2072 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2073 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2075 lds_base
= tcs_out_current_patch_data_offset
;
2076 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2077 lp_build_const_int32(gallivm
,
2078 tess_inner_index
* 4), "");
2079 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2080 lp_build_const_int32(gallivm
,
2081 tess_outer_index
* 4), "");
2083 for (i
= 0; i
< outer_comps
; i
++)
2084 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2085 for (i
= 0; i
< inner_comps
; i
++)
2086 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2088 /* Convert the outputs to vectors for stores. */
2089 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2093 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2095 /* Get the buffer. */
2096 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2097 SI_PARAM_RW_BUFFERS
);
2098 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
2099 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
2101 /* Get the offset. */
2102 tf_base
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2103 SI_PARAM_TESS_FACTOR_OFFSET
);
2104 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2105 lp_build_const_int32(gallivm
, 4 * stride
), "");
2107 /* Store the outputs. */
2108 build_tbuffer_store_dwords(ctx
, buffer
, vec0
,
2109 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
2111 build_tbuffer_store_dwords(ctx
, buffer
, vec1
,
2112 stride
- 4, byteoffset
, tf_base
, 16);
2113 lp_build_endif(&if_ctx
);
2116 /* This only writes the tessellation factor levels. */
2117 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2119 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2120 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2122 rel_patch_id
= get_rel_patch_id(ctx
);
2123 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2124 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2126 if (!ctx
->is_monolithic
) {
2127 /* Return epilog parameters from this function. */
2128 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2129 LLVMValueRef ret
= ctx
->return_value
;
2130 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2133 /* RW_BUFFERS pointer */
2134 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2135 SI_PARAM_RW_BUFFERS
);
2136 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2137 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2138 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2139 bld_base
->uint_bld
.zero
, "");
2140 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2141 bld_base
->uint_bld
.one
, "");
2142 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2143 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2145 /* Tess factor buffer soffset is after user SGPRs. */
2146 tf_soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2147 SI_PARAM_TESS_FACTOR_OFFSET
);
2148 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2149 SI_TCS_NUM_USER_SGPR
, "");
2152 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2153 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2154 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2156 vgpr
= SI_TCS_NUM_USER_SGPR
+ 1;
2157 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2158 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2159 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2160 ctx
->return_value
= ret
;
2164 si_write_tess_factors(bld_base
, rel_patch_id
, invocation_id
, tf_lds_offset
);
2167 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2169 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2170 struct si_shader
*shader
= ctx
->shader
;
2171 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2172 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2174 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2175 ctx
->param_rel_auto_id
);
2176 LLVMValueRef vertex_dw_stride
=
2177 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2178 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2179 vertex_dw_stride
, "");
2181 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2182 * its inputs from it. */
2183 for (i
= 0; i
< info
->num_outputs
; i
++) {
2184 LLVMValueRef
*out_ptr
= ctx
->radeon_bld
.soa
.outputs
[i
];
2185 unsigned name
= info
->output_semantic_name
[i
];
2186 unsigned index
= info
->output_semantic_index
[i
];
2187 int param
= si_shader_io_get_unique_index(name
, index
);
2188 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2189 lp_build_const_int32(gallivm
, param
* 4), "");
2191 for (chan
= 0; chan
< 4; chan
++) {
2192 lds_store(bld_base
, chan
, dw_addr
,
2193 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2198 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2200 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2201 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2202 struct si_shader
*es
= ctx
->shader
;
2203 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2204 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2205 ctx
->param_es2gs_offset
);
2209 for (i
= 0; i
< info
->num_outputs
; i
++) {
2210 LLVMValueRef
*out_ptr
=
2211 ctx
->radeon_bld
.soa
.outputs
[i
];
2214 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2215 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2218 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2219 info
->output_semantic_index
[i
]);
2221 for (chan
= 0; chan
< 4; chan
++) {
2222 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2223 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2225 build_tbuffer_store(ctx
,
2228 LLVMGetUndef(ctx
->i32
), soffset
,
2229 (4 * param_index
+ chan
) * 4,
2230 V_008F0C_BUF_DATA_FORMAT_32
,
2231 V_008F0C_BUF_NUM_FORMAT_UINT
,
2237 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2239 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2240 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2241 LLVMValueRef args
[2];
2243 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2244 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2245 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2246 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
2249 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2251 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2252 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2253 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2254 struct si_shader_output_values
*outputs
= NULL
;
2257 assert(!ctx
->is_gs_copy_shader
);
2259 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2261 /* Vertex color clamping.
2263 * This uses a state constant loaded in a user data SGPR and
2264 * an IF statement is added that clamps all colors if the constant
2267 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
2268 struct lp_build_if_state if_ctx
;
2269 LLVMValueRef cond
= NULL
;
2270 LLVMValueRef addr
, val
;
2272 for (i
= 0; i
< info
->num_outputs
; i
++) {
2273 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2274 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2277 /* We've found a color. */
2279 /* The state is in the first bit of the user SGPR. */
2280 cond
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2281 SI_PARAM_VS_STATE_BITS
);
2282 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2284 lp_build_if(&if_ctx
, gallivm
, cond
);
2287 for (j
= 0; j
< 4; j
++) {
2288 addr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2289 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2290 val
= radeon_llvm_saturate(bld_base
, val
);
2291 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2296 lp_build_endif(&if_ctx
);
2299 for (i
= 0; i
< info
->num_outputs
; i
++) {
2300 outputs
[i
].name
= info
->output_semantic_name
[i
];
2301 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2303 for (j
= 0; j
< 4; j
++)
2304 outputs
[i
].values
[j
] =
2305 LLVMBuildLoad(gallivm
->builder
,
2306 ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2310 if (ctx
->is_monolithic
) {
2311 /* Export PrimitiveID when PS needs it. */
2312 if (si_vs_exports_prim_id(ctx
->shader
)) {
2313 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2315 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2316 get_primitive_id(bld_base
, 0));
2317 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2318 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2319 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2323 /* Return the primitive ID from the LLVM function. */
2325 LLVMBuildInsertValue(gallivm
->builder
,
2327 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2328 get_primitive_id(bld_base
, 0)),
2329 VS_EPILOG_PRIMID_LOC
, "");
2332 si_llvm_export_vs(bld_base
, outputs
, i
);
2336 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2337 LLVMValueRef depth
, LLVMValueRef stencil
,
2338 LLVMValueRef samplemask
)
2340 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2341 struct lp_build_context
*base
= &bld_base
->base
;
2342 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2343 LLVMValueRef args
[9];
2346 assert(depth
|| stencil
|| samplemask
);
2348 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2349 args
[2] = uint
->one
; /* DONE bit */
2351 /* Specify the target we are exporting */
2352 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2354 args
[4] = uint
->zero
; /* COMP flag */
2355 args
[5] = base
->undef
; /* R, depth */
2356 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2357 args
[7] = base
->undef
; /* B, sample mask */
2358 args
[8] = base
->undef
; /* A, alpha to mask */
2371 args
[7] = samplemask
;
2375 /* SI (except OLAND) has a bug that it only looks
2376 * at the X writemask component. */
2377 if (ctx
->screen
->b
.chip_class
== SI
&&
2378 ctx
->screen
->b
.family
!= CHIP_OLAND
)
2381 /* Specify which components to enable */
2382 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2384 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2385 ctx
->voidt
, args
, 9, 0);
2388 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2389 LLVMValueRef
*color
, unsigned index
,
2390 unsigned samplemask_param
,
2393 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2394 struct lp_build_context
*base
= &bld_base
->base
;
2398 if (ctx
->shader
->key
.ps
.epilog
.clamp_color
)
2399 for (i
= 0; i
< 4; i
++)
2400 color
[i
] = radeon_llvm_saturate(bld_base
, color
[i
]);
2403 if (ctx
->shader
->key
.ps
.epilog
.alpha_to_one
)
2404 color
[3] = base
->one
;
2408 ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2409 si_alpha_test(bld_base
, color
[3]);
2411 /* Line & polygon smoothing */
2412 if (ctx
->shader
->key
.ps
.epilog
.poly_line_smoothing
)
2413 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
2416 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2417 if (ctx
->shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2418 LLVMValueRef args
[8][9];
2421 /* Get the export arguments, also find out what the last one is. */
2422 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2423 si_llvm_init_export_args(bld_base
, color
,
2424 V_008DFC_SQ_EXP_MRT
+ c
, args
[c
]);
2425 if (args
[c
][0] != bld_base
->uint_bld
.zero
)
2429 /* Emit all exports. */
2430 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2431 if (is_last
&& last
== c
) {
2432 args
[c
][1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2433 args
[c
][2] = bld_base
->uint_bld
.one
; /* DONE bit */
2434 } else if (args
[c
][0] == bld_base
->uint_bld
.zero
)
2435 continue; /* unnecessary NULL export */
2437 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2438 ctx
->voidt
, args
[c
], 9, 0);
2441 LLVMValueRef args
[9];
2444 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
2447 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2448 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
2449 } else if (args
[0] == bld_base
->uint_bld
.zero
)
2450 return; /* unnecessary NULL export */
2452 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2453 ctx
->voidt
, args
, 9, 0);
2457 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
2459 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2460 struct lp_build_context
*base
= &bld_base
->base
;
2461 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2462 LLVMValueRef args
[9];
2464 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
2465 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2466 args
[2] = uint
->one
; /* DONE bit */
2467 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
2468 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
2469 args
[5] = uint
->undef
; /* R */
2470 args
[6] = uint
->undef
; /* G */
2471 args
[7] = uint
->undef
; /* B */
2472 args
[8] = uint
->undef
; /* A */
2474 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2475 ctx
->voidt
, args
, 9, 0);
2478 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2480 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2481 struct si_shader
*shader
= ctx
->shader
;
2482 struct lp_build_context
*base
= &bld_base
->base
;
2483 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2484 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2485 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2486 int last_color_export
= -1;
2489 /* Determine the last export. If MRTZ is present, it's always last.
2490 * Otherwise, find the last color export.
2492 if (!info
->writes_z
&& !info
->writes_stencil
&& !info
->writes_samplemask
) {
2493 unsigned spi_format
= shader
->key
.ps
.epilog
.spi_shader_col_format
;
2495 /* Don't export NULL and return if alpha-test is enabled. */
2496 if (shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
&&
2497 shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
&&
2498 (spi_format
& 0xf) == 0)
2499 spi_format
|= V_028714_SPI_SHADER_32_AR
;
2501 for (i
= 0; i
< info
->num_outputs
; i
++) {
2502 unsigned index
= info
->output_semantic_index
[i
];
2504 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
)
2507 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2508 if (shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2509 /* Just set this if any of the colorbuffers are enabled. */
2511 ((1llu << (4 * (shader
->key
.ps
.epilog
.last_cbuf
+ 1))) - 1))
2512 last_color_export
= i
;
2516 if ((spi_format
>> (index
* 4)) & 0xf)
2517 last_color_export
= i
;
2520 /* If there are no outputs, export NULL. */
2521 if (last_color_export
== -1) {
2522 si_export_null(bld_base
);
2527 for (i
= 0; i
< info
->num_outputs
; i
++) {
2528 unsigned semantic_name
= info
->output_semantic_name
[i
];
2529 unsigned semantic_index
= info
->output_semantic_index
[i
];
2531 LLVMValueRef color
[4] = {};
2533 /* Select the correct target */
2534 switch (semantic_name
) {
2535 case TGSI_SEMANTIC_POSITION
:
2536 depth
= LLVMBuildLoad(builder
,
2537 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2539 case TGSI_SEMANTIC_STENCIL
:
2540 stencil
= LLVMBuildLoad(builder
,
2541 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2543 case TGSI_SEMANTIC_SAMPLEMASK
:
2544 samplemask
= LLVMBuildLoad(builder
,
2545 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2547 case TGSI_SEMANTIC_COLOR
:
2548 for (j
= 0; j
< 4; j
++)
2549 color
[j
] = LLVMBuildLoad(builder
,
2550 ctx
->radeon_bld
.soa
.outputs
[i
][j
], "");
2552 si_export_mrt_color(bld_base
, color
, semantic_index
,
2553 SI_PARAM_SAMPLE_COVERAGE
,
2554 last_color_export
== i
);
2558 "Warning: SI unhandled fs output type:%d\n",
2563 if (depth
|| stencil
|| samplemask
)
2564 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
2568 * Return PS outputs in this order:
2570 * v[0:3] = color0.xyzw
2571 * v[4:7] = color1.xyzw
2576 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
2578 * The alpha-ref SGPR is returned via its original location.
2580 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
2582 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2583 struct si_shader
*shader
= ctx
->shader
;
2584 struct lp_build_context
*base
= &bld_base
->base
;
2585 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2586 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2587 unsigned i
, j
, first_vgpr
, vgpr
;
2589 LLVMValueRef color
[8][4] = {};
2590 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2593 /* Read the output values. */
2594 for (i
= 0; i
< info
->num_outputs
; i
++) {
2595 unsigned semantic_name
= info
->output_semantic_name
[i
];
2596 unsigned semantic_index
= info
->output_semantic_index
[i
];
2598 switch (semantic_name
) {
2599 case TGSI_SEMANTIC_COLOR
:
2600 assert(semantic_index
< 8);
2601 for (j
= 0; j
< 4; j
++) {
2602 LLVMValueRef ptr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2603 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2604 color
[semantic_index
][j
] = result
;
2607 case TGSI_SEMANTIC_POSITION
:
2608 depth
= LLVMBuildLoad(builder
,
2609 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2611 case TGSI_SEMANTIC_STENCIL
:
2612 stencil
= LLVMBuildLoad(builder
,
2613 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2615 case TGSI_SEMANTIC_SAMPLEMASK
:
2616 samplemask
= LLVMBuildLoad(builder
,
2617 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2620 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
2625 /* Fill the return structure. */
2626 ret
= ctx
->return_value
;
2629 ret
= LLVMBuildInsertValue(builder
, ret
,
2630 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
2631 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2632 SI_PARAM_ALPHA_REF
)),
2633 SI_SGPR_ALPHA_REF
, "");
2636 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
2637 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
2641 for (j
= 0; j
< 4; j
++)
2642 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
2645 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
2647 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
2649 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
2651 /* Add the input sample mask for smoothing at the end. */
2652 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
2653 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
2654 ret
= LLVMBuildInsertValue(builder
, ret
,
2655 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2656 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
2658 ctx
->return_value
= ret
;
2662 * Given a v8i32 resource descriptor for a buffer, extract the size of the
2663 * buffer in number of elements and return it as an i32.
2665 static LLVMValueRef
get_buffer_size(
2666 struct lp_build_tgsi_context
*bld_base
,
2667 LLVMValueRef descriptor
)
2669 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2670 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2671 LLVMBuilderRef builder
= gallivm
->builder
;
2673 LLVMBuildExtractElement(builder
, descriptor
,
2674 lp_build_const_int32(gallivm
, 6), "");
2676 if (ctx
->screen
->b
.chip_class
>= VI
) {
2677 /* On VI, the descriptor contains the size in bytes,
2678 * but TXQ must return the size in elements.
2679 * The stride is always non-zero for resources using TXQ.
2681 LLVMValueRef stride
=
2682 LLVMBuildExtractElement(builder
, descriptor
,
2683 lp_build_const_int32(gallivm
, 5), "");
2684 stride
= LLVMBuildLShr(builder
, stride
,
2685 lp_build_const_int32(gallivm
, 16), "");
2686 stride
= LLVMBuildAnd(builder
, stride
,
2687 lp_build_const_int32(gallivm
, 0x3FFF), "");
2689 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2696 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2699 static void build_int_type_name(
2701 char *buf
, unsigned bufsize
)
2703 assert(bufsize
>= 6);
2705 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2706 snprintf(buf
, bufsize
, "v%ui32",
2707 LLVMGetVectorSize(type
));
2712 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
2713 struct lp_build_tgsi_context
*bld_base
,
2714 struct lp_build_emit_data
*emit_data
);
2716 static bool tgsi_is_array_sampler(unsigned target
)
2718 return target
== TGSI_TEXTURE_1D_ARRAY
||
2719 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2720 target
== TGSI_TEXTURE_2D_ARRAY
||
2721 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2722 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2723 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2724 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2727 static bool tgsi_is_array_image(unsigned target
)
2729 return target
== TGSI_TEXTURE_3D
||
2730 target
== TGSI_TEXTURE_CUBE
||
2731 target
== TGSI_TEXTURE_1D_ARRAY
||
2732 target
== TGSI_TEXTURE_2D_ARRAY
||
2733 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2734 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2738 * Load the resource descriptor for \p image.
2742 struct lp_build_tgsi_context
*bld_base
,
2743 const struct tgsi_full_src_register
*image
,
2746 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2748 assert(image
->Register
.File
== TGSI_FILE_IMAGE
);
2750 if (!image
->Register
.Indirect
) {
2751 /* Fast path: use preloaded resources */
2752 *rsrc
= ctx
->images
[image
->Register
.Index
];
2754 /* Indexing and manual load */
2755 LLVMValueRef ind_index
;
2756 LLVMValueRef rsrc_ptr
;
2758 ind_index
= get_indirect_index(ctx
, &image
->Indirect
, image
->Register
.Index
);
2760 rsrc_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_IMAGES
);
2761 *rsrc
= build_indexed_load_const(ctx
, rsrc_ptr
, ind_index
);
2765 static LLVMValueRef
image_fetch_coords(
2766 struct lp_build_tgsi_context
*bld_base
,
2767 const struct tgsi_full_instruction
*inst
,
2770 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2771 LLVMBuilderRef builder
= gallivm
->builder
;
2772 unsigned target
= inst
->Memory
.Texture
;
2774 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &sample
);
2775 LLVMValueRef coords
[4];
2779 for (chan
= 0; chan
< num_coords
; ++chan
) {
2780 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
2781 tmp
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
2785 if (num_coords
== 1)
2788 if (num_coords
== 3) {
2789 /* LLVM has difficulties lowering 3-element vectors. */
2790 coords
[3] = bld_base
->uint_bld
.undef
;
2794 return lp_build_gather_values(gallivm
, coords
, num_coords
);
2798 * Append the extra mode bits that are used by image load and store.
2800 static void image_append_args(
2801 struct si_shader_context
*ctx
,
2802 struct lp_build_emit_data
* emit_data
,
2806 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2807 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2809 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* r128 */
2810 emit_data
->args
[emit_data
->arg_count
++] =
2811 tgsi_is_array_image(target
) ? i1true
: i1false
; /* da */
2813 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* glc */
2814 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
2818 * Append the resource and indexing arguments for buffer intrinsics.
2820 * \param rsrc the 256 bit resource
2821 * \param index index into the buffer
2823 static void buffer_append_args(
2824 struct si_shader_context
*ctx
,
2825 struct lp_build_emit_data
*emit_data
,
2830 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
2831 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
2832 LLVMTypeRef v2i128
= LLVMVectorType(ctx
->i128
, 2);
2833 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2835 rsrc
= LLVMBuildBitCast(gallivm
->builder
, rsrc
, v2i128
, "");
2836 rsrc
= LLVMBuildExtractElement(gallivm
->builder
, rsrc
, bld_base
->uint_bld
.one
, "");
2837 rsrc
= LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v4i32
, "");
2839 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
2840 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
2841 emit_data
->args
[emit_data
->arg_count
++] = bld_base
->uint_bld
.zero
; /* voffset */
2843 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* glc */
2844 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
2847 static void load_fetch_args(
2848 struct lp_build_tgsi_context
* bld_base
,
2849 struct lp_build_emit_data
* emit_data
)
2851 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2852 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2853 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2854 unsigned target
= inst
->Memory
.Texture
;
2855 LLVMValueRef coords
;
2858 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2860 image_fetch_rsrc(bld_base
, &inst
->Src
[0], &rsrc
);
2861 coords
= image_fetch_coords(bld_base
, inst
, 1);
2863 if (target
== TGSI_TEXTURE_BUFFER
) {
2864 buffer_append_args(ctx
, emit_data
, rsrc
, coords
, false);
2866 emit_data
->args
[0] = coords
;
2867 emit_data
->args
[1] = rsrc
;
2868 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
2869 emit_data
->arg_count
= 3;
2871 image_append_args(ctx
, emit_data
, target
, false);
2875 static void load_emit(
2876 const struct lp_build_tgsi_action
*action
,
2877 struct lp_build_tgsi_context
*bld_base
,
2878 struct lp_build_emit_data
*emit_data
)
2880 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2881 LLVMBuilderRef builder
= gallivm
->builder
;
2882 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2883 unsigned target
= inst
->Memory
.Texture
;
2884 char intrinsic_name
[32];
2885 char coords_type
[8];
2887 if (target
== TGSI_TEXTURE_BUFFER
) {
2888 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2889 builder
, "llvm.amdgcn.buffer.load.format.v4f32", emit_data
->dst_type
,
2890 emit_data
->args
, emit_data
->arg_count
,
2891 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
2893 build_int_type_name(LLVMTypeOf(emit_data
->args
[0]),
2894 coords_type
, sizeof(coords_type
));
2896 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2897 "llvm.amdgcn.image.load.%s", coords_type
);
2899 emit_data
->output
[emit_data
->chan
] =
2901 builder
, intrinsic_name
, emit_data
->dst_type
,
2902 emit_data
->args
, emit_data
->arg_count
,
2903 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
2907 static void store_fetch_args(
2908 struct lp_build_tgsi_context
* bld_base
,
2909 struct lp_build_emit_data
* emit_data
)
2911 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2912 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2913 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2914 struct tgsi_full_src_register image
;
2915 unsigned target
= inst
->Memory
.Texture
;
2916 LLVMValueRef chans
[4];
2918 LLVMValueRef coords
;
2922 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
2924 image
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
2925 image_fetch_rsrc(bld_base
, &image
, &rsrc
);
2926 coords
= image_fetch_coords(bld_base
, inst
, 0);
2928 for (chan
= 0; chan
< 4; ++chan
) {
2929 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
2931 data
= lp_build_gather_values(gallivm
, chans
, 4);
2933 if (target
== TGSI_TEXTURE_BUFFER
) {
2934 emit_data
->args
[0] = data
;
2935 emit_data
->arg_count
= 1;
2937 buffer_append_args(ctx
, emit_data
, rsrc
, coords
, false);
2939 emit_data
->args
[0] = data
;
2940 emit_data
->args
[1] = coords
;
2941 emit_data
->args
[2] = rsrc
;
2942 emit_data
->args
[3] = lp_build_const_int32(gallivm
, 15); /* dmask */
2943 emit_data
->arg_count
= 4;
2945 image_append_args(ctx
, emit_data
, target
, false);
2949 static void store_emit(
2950 const struct lp_build_tgsi_action
*action
,
2951 struct lp_build_tgsi_context
*bld_base
,
2952 struct lp_build_emit_data
*emit_data
)
2954 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2955 LLVMBuilderRef builder
= gallivm
->builder
;
2956 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2957 unsigned target
= inst
->Memory
.Texture
;
2958 char intrinsic_name
[32];
2959 char coords_type
[8];
2961 if (target
== TGSI_TEXTURE_BUFFER
) {
2962 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2963 builder
, "llvm.amdgcn.buffer.store.format.v4f32",
2964 emit_data
->dst_type
, emit_data
->args
, emit_data
->arg_count
,
2965 LLVMNoUnwindAttribute
);
2967 build_int_type_name(LLVMTypeOf(emit_data
->args
[1]),
2968 coords_type
, sizeof(coords_type
));
2969 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2970 "llvm.amdgcn.image.store.%s", coords_type
);
2972 emit_data
->output
[emit_data
->chan
] =
2974 builder
, intrinsic_name
, emit_data
->dst_type
,
2975 emit_data
->args
, emit_data
->arg_count
,
2976 LLVMNoUnwindAttribute
);
2980 static void atomic_fetch_args(
2981 struct lp_build_tgsi_context
* bld_base
,
2982 struct lp_build_emit_data
* emit_data
)
2984 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2985 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2986 LLVMBuilderRef builder
= gallivm
->builder
;
2987 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2988 unsigned target
= inst
->Memory
.Texture
;
2989 LLVMValueRef data1
, data2
;
2990 LLVMValueRef coords
;
2994 emit_data
->dst_type
= bld_base
->base
.elem_type
;
2996 image_fetch_rsrc(bld_base
, &inst
->Src
[0], &rsrc
);
2997 coords
= image_fetch_coords(bld_base
, inst
, 1);
2999 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
3000 data1
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3002 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3003 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
3004 data2
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3007 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
3008 * of arguments, which is reversed relative to TGSI (and GLSL)
3010 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
3011 emit_data
->args
[emit_data
->arg_count
++] = data2
;
3012 emit_data
->args
[emit_data
->arg_count
++] = data1
;
3014 if (target
== TGSI_TEXTURE_BUFFER
) {
3015 buffer_append_args(ctx
, emit_data
, rsrc
, coords
, true);
3017 emit_data
->args
[emit_data
->arg_count
++] = coords
;
3018 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3020 image_append_args(ctx
, emit_data
, target
, true);
3024 static void atomic_emit(
3025 const struct lp_build_tgsi_action
*action
,
3026 struct lp_build_tgsi_context
*bld_base
,
3027 struct lp_build_emit_data
*emit_data
)
3029 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3030 LLVMBuilderRef builder
= gallivm
->builder
;
3031 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3032 unsigned target
= inst
->Memory
.Texture
;
3033 char intrinsic_name
[40];
3036 if (target
== TGSI_TEXTURE_BUFFER
) {
3037 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3038 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
3040 char coords_type
[8];
3042 build_int_type_name(LLVMTypeOf(emit_data
->args
[1]),
3043 coords_type
, sizeof(coords_type
));
3044 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
3045 "llvm.amdgcn.image.atomic.%s.%s",
3046 action
->intr_name
, coords_type
);
3049 tmp
= lp_build_intrinsic(
3050 builder
, intrinsic_name
, bld_base
->uint_bld
.elem_type
,
3051 emit_data
->args
, emit_data
->arg_count
,
3052 LLVMNoUnwindAttribute
);
3053 emit_data
->output
[emit_data
->chan
] =
3054 LLVMBuildBitCast(builder
, tmp
, bld_base
->base
.elem_type
, "");
3057 static void resq_fetch_args(
3058 struct lp_build_tgsi_context
* bld_base
,
3059 struct lp_build_emit_data
* emit_data
)
3061 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3062 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3063 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
3064 unsigned tex_target
= inst
->Memory
.Texture
;
3066 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
3068 if (tex_target
== TGSI_TEXTURE_BUFFER
) {
3069 image_fetch_rsrc(bld_base
, reg
, &emit_data
->args
[0]);
3070 emit_data
->arg_count
= 1;
3072 emit_data
->args
[0] = bld_base
->uint_bld
.zero
; /* mip level */
3073 image_fetch_rsrc(bld_base
, reg
, &emit_data
->args
[1]);
3074 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
3075 emit_data
->args
[3] = bld_base
->uint_bld
.zero
; /* unorm */
3076 emit_data
->args
[4] = bld_base
->uint_bld
.zero
; /* r128 */
3077 emit_data
->args
[5] = tgsi_is_array_image(tex_target
) ?
3078 bld_base
->uint_bld
.one
: bld_base
->uint_bld
.zero
; /* da */
3079 emit_data
->args
[6] = bld_base
->uint_bld
.zero
; /* glc */
3080 emit_data
->args
[7] = bld_base
->uint_bld
.zero
; /* slc */
3081 emit_data
->args
[8] = bld_base
->uint_bld
.zero
; /* tfe */
3082 emit_data
->args
[9] = bld_base
->uint_bld
.zero
; /* lwe */
3083 emit_data
->arg_count
= 10;
3087 static void resq_emit(
3088 const struct lp_build_tgsi_action
*action
,
3089 struct lp_build_tgsi_context
*bld_base
,
3090 struct lp_build_emit_data
*emit_data
)
3092 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3093 LLVMBuilderRef builder
= gallivm
->builder
;
3094 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3095 unsigned target
= inst
->Memory
.Texture
;
3098 if (target
== TGSI_TEXTURE_BUFFER
) {
3099 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
3101 out
= lp_build_intrinsic(
3102 builder
, "llvm.SI.getresinfo.i32", emit_data
->dst_type
,
3103 emit_data
->args
, emit_data
->arg_count
,
3104 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3106 /* Divide the number of layers by 6 to get the number of cubes. */
3107 if (target
== TGSI_TEXTURE_CUBE_ARRAY
) {
3108 LLVMValueRef imm2
= lp_build_const_int32(gallivm
, 2);
3109 LLVMValueRef imm6
= lp_build_const_int32(gallivm
, 6);
3111 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
3112 z
= LLVMBuildBitCast(builder
, z
, bld_base
->uint_bld
.elem_type
, "");
3113 z
= LLVMBuildSDiv(builder
, z
, imm6
, "");
3114 z
= LLVMBuildBitCast(builder
, z
, bld_base
->base
.elem_type
, "");
3115 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
3119 emit_data
->output
[emit_data
->chan
] = out
;
3122 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
3123 struct lp_build_emit_data
*emit_data
,
3124 unsigned opcode
, unsigned target
,
3125 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
3126 LLVMValueRef
*param
, unsigned count
,
3129 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
3131 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
3133 /* Pad to power of two vector */
3134 while (count
< util_next_power_of_two(count
))
3135 param
[count
++] = LLVMGetUndef(ctx
->i32
);
3137 /* Texture coordinates. */
3139 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
3141 emit_data
->args
[0] = param
[0];
3144 emit_data
->args
[1] = res_ptr
;
3147 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
3148 emit_data
->dst_type
= ctx
->v4i32
;
3150 emit_data
->dst_type
= ctx
->v4f32
;
3152 emit_data
->args
[num_args
++] = samp_ptr
;
3155 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
3156 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
3157 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
3158 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
3159 tgsi_is_array_sampler(target
)); /* da */
3160 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
3161 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
3162 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
3163 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
3165 emit_data
->arg_count
= num_args
;
3168 static const struct lp_build_tgsi_action tex_action
;
3176 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3178 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3183 * Load an image view, fmask view. or sampler state descriptor.
3185 static LLVMValueRef
get_sampler_desc_custom(struct si_shader_context
*ctx
,
3186 LLVMValueRef list
, LLVMValueRef index
,
3187 enum desc_type type
)
3189 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
3190 LLVMBuilderRef builder
= gallivm
->builder
;
3194 /* The image is at [0:7]. */
3195 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
3198 /* The FMASK is at [8:15]. */
3199 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
3200 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
3203 /* The sampler state is at [12:15]. */
3204 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
3205 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
3206 list
= LLVMBuildPointerCast(builder
, list
,
3207 const_array(ctx
->v4i32
, 0), "");
3211 return build_indexed_load_const(ctx
, list
, index
);
3214 static LLVMValueRef
get_sampler_desc(struct si_shader_context
*ctx
,
3215 LLVMValueRef index
, enum desc_type type
)
3217 LLVMValueRef list
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
3220 return get_sampler_desc_custom(ctx
, list
, index
, type
);
3223 static void tex_fetch_ptrs(
3224 struct lp_build_tgsi_context
*bld_base
,
3225 struct lp_build_emit_data
*emit_data
,
3226 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
3228 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3229 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3230 unsigned target
= inst
->Texture
.Texture
;
3231 unsigned sampler_src
;
3232 unsigned sampler_index
;
3234 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
3235 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
3237 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
3238 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
3239 LLVMValueRef ind_index
;
3241 ind_index
= get_indirect_index(ctx
, ®
->Indirect
, reg
->Register
.Index
);
3243 *res_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_IMAGE
);
3245 if (target
== TGSI_TEXTURE_2D_MSAA
||
3246 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
3248 *fmask_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_FMASK
);
3250 *samp_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_SAMPLER
);
3254 *res_ptr
= ctx
->sampler_views
[sampler_index
];
3255 *samp_ptr
= ctx
->sampler_states
[sampler_index
];
3256 *fmask_ptr
= ctx
->fmasks
[sampler_index
];
3260 static void tex_fetch_args(
3261 struct lp_build_tgsi_context
*bld_base
,
3262 struct lp_build_emit_data
*emit_data
)
3264 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3265 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3266 LLVMBuilderRef builder
= gallivm
->builder
;
3267 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3268 unsigned opcode
= inst
->Instruction
.Opcode
;
3269 unsigned target
= inst
->Texture
.Texture
;
3270 LLVMValueRef coords
[5], derivs
[6];
3271 LLVMValueRef address
[16];
3273 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
3276 unsigned num_deriv_channels
= 0;
3277 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
3278 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
3279 unsigned dmask
= 0xf;
3281 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3283 if (opcode
== TGSI_OPCODE_TXQ
) {
3284 if (target
== TGSI_TEXTURE_BUFFER
) {
3285 /* Read the size from the buffer descriptor directly. */
3286 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
3287 emit_data
->args
[0] = get_buffer_size(bld_base
, res
);
3291 /* Textures - set the mip level. */
3292 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
3294 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
3295 NULL
, address
, count
, 0xf);
3299 if (target
== TGSI_TEXTURE_BUFFER
) {
3300 LLVMTypeRef v2i128
= LLVMVectorType(ctx
->i128
, 2);
3302 /* Bitcast and truncate v8i32 to v16i8. */
3303 LLVMValueRef res
= res_ptr
;
3304 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
3305 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
3306 res
= LLVMBuildBitCast(gallivm
->builder
, res
, ctx
->v16i8
, "");
3308 emit_data
->dst_type
= ctx
->v4f32
;
3309 emit_data
->args
[0] = res
;
3310 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
3311 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
3312 emit_data
->arg_count
= 3;
3316 /* Fetch and project texture coordinates */
3317 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
3318 for (chan
= 0; chan
< 3; chan
++ ) {
3319 coords
[chan
] = lp_build_emit_fetch(bld_base
,
3322 if (opcode
== TGSI_OPCODE_TXP
)
3323 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
3329 if (opcode
== TGSI_OPCODE_TXP
)
3330 coords
[3] = bld_base
->base
.one
;
3333 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
3334 /* The offsets are six-bit signed integers packed like this:
3335 * X=[5:0], Y=[13:8], and Z=[21:16].
3337 LLVMValueRef offset
[3], pack
;
3339 assert(inst
->Texture
.NumOffsets
== 1);
3341 for (chan
= 0; chan
< 3; chan
++) {
3342 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
3343 emit_data
->inst
, 0, chan
);
3344 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
3345 lp_build_const_int32(gallivm
, 0x3f), "");
3347 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
3348 lp_build_const_int32(gallivm
, chan
*8), "");
3351 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
3352 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
3353 address
[count
++] = pack
;
3356 /* Pack LOD bias value */
3357 if (opcode
== TGSI_OPCODE_TXB
)
3358 address
[count
++] = coords
[3];
3359 if (opcode
== TGSI_OPCODE_TXB2
)
3360 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3362 /* Pack depth comparison value */
3363 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
3364 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
3365 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3367 assert(ref_pos
>= 0);
3368 address
[count
++] = coords
[ref_pos
];
3372 /* Pack user derivatives */
3373 if (opcode
== TGSI_OPCODE_TXD
) {
3374 int param
, num_src_deriv_channels
;
3377 case TGSI_TEXTURE_3D
:
3378 num_src_deriv_channels
= 3;
3379 num_deriv_channels
= 3;
3381 case TGSI_TEXTURE_2D
:
3382 case TGSI_TEXTURE_SHADOW2D
:
3383 case TGSI_TEXTURE_RECT
:
3384 case TGSI_TEXTURE_SHADOWRECT
:
3385 case TGSI_TEXTURE_2D_ARRAY
:
3386 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3387 num_src_deriv_channels
= 2;
3388 num_deriv_channels
= 2;
3390 case TGSI_TEXTURE_CUBE
:
3391 case TGSI_TEXTURE_SHADOWCUBE
:
3392 case TGSI_TEXTURE_CUBE_ARRAY
:
3393 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
3394 /* Cube derivatives will be converted to 2D. */
3395 num_src_deriv_channels
= 3;
3396 num_deriv_channels
= 2;
3398 case TGSI_TEXTURE_1D
:
3399 case TGSI_TEXTURE_SHADOW1D
:
3400 case TGSI_TEXTURE_1D_ARRAY
:
3401 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3402 num_src_deriv_channels
= 1;
3403 num_deriv_channels
= 1;
3406 unreachable("invalid target");
3409 for (param
= 0; param
< 2; param
++)
3410 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
3411 derivs
[param
* num_src_deriv_channels
+ chan
] =
3412 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
3415 if (target
== TGSI_TEXTURE_CUBE
||
3416 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3417 target
== TGSI_TEXTURE_SHADOWCUBE
||
3418 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
3419 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
3421 if (opcode
== TGSI_OPCODE_TXD
)
3422 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
3423 address
[count
++] = derivs
[i
];
3425 /* Pack texture coordinates */
3426 address
[count
++] = coords
[0];
3428 address
[count
++] = coords
[1];
3430 address
[count
++] = coords
[2];
3432 /* Pack LOD or sample index */
3433 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
3434 address
[count
++] = coords
[3];
3435 else if (opcode
== TGSI_OPCODE_TXL2
)
3436 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3439 assert(!"Cannot handle more than 16 texture address parameters");
3443 for (chan
= 0; chan
< count
; chan
++ ) {
3444 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
3445 address
[chan
], ctx
->i32
, "");
3448 /* Adjust the sample index according to FMASK.
3450 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3451 * which is the identity mapping. Each nibble says which physical sample
3452 * should be fetched to get that sample.
3454 * For example, 0x11111100 means there are only 2 samples stored and
3455 * the second sample covers 3/4 of the pixel. When reading samples 0
3456 * and 1, return physical sample 0 (determined by the first two 0s
3457 * in FMASK), otherwise return physical sample 1.
3459 * The sample index should be adjusted as follows:
3460 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3462 if (target
== TGSI_TEXTURE_2D_MSAA
||
3463 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
3464 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3465 struct lp_build_emit_data txf_emit_data
= *emit_data
;
3466 LLVMValueRef txf_address
[4];
3467 unsigned txf_count
= count
;
3468 struct tgsi_full_instruction inst
= {};
3470 memcpy(txf_address
, address
, sizeof(txf_address
));
3472 if (target
== TGSI_TEXTURE_2D_MSAA
) {
3473 txf_address
[2] = bld_base
->uint_bld
.zero
;
3475 txf_address
[3] = bld_base
->uint_bld
.zero
;
3477 /* Read FMASK using TXF. */
3478 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
3479 inst
.Texture
.Texture
= target
;
3480 txf_emit_data
.inst
= &inst
;
3481 txf_emit_data
.chan
= 0;
3482 set_tex_fetch_args(ctx
, &txf_emit_data
, TGSI_OPCODE_TXF
,
3483 target
, fmask_ptr
, NULL
,
3484 txf_address
, txf_count
, 0xf);
3485 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
3487 /* Initialize some constants. */
3488 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
3489 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
3491 /* Apply the formula. */
3492 LLVMValueRef fmask
=
3493 LLVMBuildExtractElement(gallivm
->builder
,
3494 txf_emit_data
.output
[0],
3495 uint_bld
->zero
, "");
3497 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
3499 LLVMValueRef sample_index4
=
3500 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
3502 LLVMValueRef shifted_fmask
=
3503 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
3505 LLVMValueRef final_sample
=
3506 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
3508 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3509 * resource descriptor is 0 (invalid),
3511 LLVMValueRef fmask_desc
=
3512 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
3515 LLVMValueRef fmask_word1
=
3516 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
3519 LLVMValueRef word1_is_nonzero
=
3520 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
3521 fmask_word1
, uint_bld
->zero
, "");
3523 /* Replace the MSAA sample index. */
3524 address
[sample_chan
] =
3525 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
3526 final_sample
, address
[sample_chan
], "");
3529 if (opcode
== TGSI_OPCODE_TXF
) {
3530 /* add tex offsets */
3531 if (inst
->Texture
.NumOffsets
) {
3532 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3533 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
3534 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
3536 assert(inst
->Texture
.NumOffsets
== 1);
3539 case TGSI_TEXTURE_3D
:
3540 address
[2] = lp_build_add(uint_bld
, address
[2],
3541 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
3543 case TGSI_TEXTURE_2D
:
3544 case TGSI_TEXTURE_SHADOW2D
:
3545 case TGSI_TEXTURE_RECT
:
3546 case TGSI_TEXTURE_SHADOWRECT
:
3547 case TGSI_TEXTURE_2D_ARRAY
:
3548 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3550 lp_build_add(uint_bld
, address
[1],
3551 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
3553 case TGSI_TEXTURE_1D
:
3554 case TGSI_TEXTURE_SHADOW1D
:
3555 case TGSI_TEXTURE_1D_ARRAY
:
3556 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3558 lp_build_add(uint_bld
, address
[0],
3559 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
3561 /* texture offsets do not apply to other texture targets */
3566 if (opcode
== TGSI_OPCODE_TG4
) {
3567 unsigned gather_comp
= 0;
3569 /* DMASK was repurposed for GATHER4. 4 components are always
3570 * returned and DMASK works like a swizzle - it selects
3571 * the component to fetch. The only valid DMASK values are
3572 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3573 * (red,red,red,red) etc.) The ISA document doesn't mention
3577 /* Get the component index from src1.x for Gather4. */
3578 if (!tgsi_is_shadow_target(target
)) {
3579 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3580 LLVMValueRef comp_imm
;
3581 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
3583 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
3585 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
3586 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
3587 gather_comp
= CLAMP(gather_comp
, 0, 3);
3590 dmask
= 1 << gather_comp
;
3593 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
3594 samp_ptr
, address
, count
, dmask
);
3597 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3598 struct lp_build_tgsi_context
*bld_base
,
3599 struct lp_build_emit_data
*emit_data
)
3601 struct lp_build_context
*base
= &bld_base
->base
;
3602 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
3603 unsigned target
= emit_data
->inst
->Texture
.Texture
;
3604 char intr_name
[127];
3605 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
3606 bool is_shadow
= tgsi_is_shadow_target(target
);
3608 const char *name
= "llvm.SI.image.sample";
3609 const char *infix
= "";
3611 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
3612 /* Just return the buffer size. */
3613 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
3617 if (target
== TGSI_TEXTURE_BUFFER
) {
3618 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3619 base
->gallivm
->builder
,
3620 "llvm.SI.vs.load.input", emit_data
->dst_type
,
3621 emit_data
->args
, emit_data
->arg_count
,
3622 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3627 case TGSI_OPCODE_TXF
:
3628 name
= target
== TGSI_TEXTURE_2D_MSAA
||
3629 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
3630 "llvm.SI.image.load" :
3631 "llvm.SI.image.load.mip";
3635 case TGSI_OPCODE_TXQ
:
3636 name
= "llvm.SI.getresinfo";
3640 case TGSI_OPCODE_LODQ
:
3641 name
= "llvm.SI.getlod";
3645 case TGSI_OPCODE_TEX
:
3646 case TGSI_OPCODE_TEX2
:
3647 case TGSI_OPCODE_TXP
:
3649 case TGSI_OPCODE_TXB
:
3650 case TGSI_OPCODE_TXB2
:
3653 case TGSI_OPCODE_TXL
:
3654 case TGSI_OPCODE_TXL2
:
3657 case TGSI_OPCODE_TXD
:
3660 case TGSI_OPCODE_TG4
:
3661 name
= "llvm.SI.gather4";
3668 /* Add the type and suffixes .c, .o if needed. */
3669 build_int_type_name(LLVMTypeOf(emit_data
->args
[0]), type
, sizeof(type
));
3670 sprintf(intr_name
, "%s%s%s%s.%s",
3671 name
, is_shadow
? ".c" : "", infix
,
3672 has_offset
? ".o" : "", type
);
3674 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3675 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
3676 emit_data
->args
, emit_data
->arg_count
,
3677 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3679 /* Divide the number of layers by 6 to get the number of cubes. */
3680 if (opcode
== TGSI_OPCODE_TXQ
&&
3681 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
3682 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
3683 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3684 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
3685 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
3687 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
3688 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
3689 z
= LLVMBuildSDiv(builder
, z
, six
, "");
3691 emit_data
->output
[emit_data
->chan
] =
3692 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
3696 static void si_llvm_emit_txqs(
3697 const struct lp_build_tgsi_action
*action
,
3698 struct lp_build_tgsi_context
*bld_base
,
3699 struct lp_build_emit_data
*emit_data
)
3701 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3702 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3703 LLVMBuilderRef builder
= gallivm
->builder
;
3704 LLVMValueRef res
, samples
;
3705 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
3707 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3710 /* Read the samples from the descriptor directly. */
3711 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
3712 samples
= LLVMBuildExtractElement(
3714 lp_build_const_int32(gallivm
, 3), "");
3715 samples
= LLVMBuildLShr(builder
, samples
,
3716 lp_build_const_int32(gallivm
, 16), "");
3717 samples
= LLVMBuildAnd(builder
, samples
,
3718 lp_build_const_int32(gallivm
, 0xf), "");
3719 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
3722 emit_data
->output
[emit_data
->chan
] = samples
;
3726 * SI implements derivatives using the local data store (LDS)
3727 * All writes to the LDS happen in all executing threads at
3728 * the same time. TID is the Thread ID for the current
3729 * thread and is a value between 0 and 63, representing
3730 * the thread's position in the wavefront.
3732 * For the pixel shader threads are grouped into quads of four pixels.
3733 * The TIDs of the pixels of a quad are:
3741 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
3742 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
3743 * the current pixel's column, and masking with 0xfffffffe yields the TID
3744 * of the left pixel of the current pixel's row.
3746 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
3747 * adding 2 yields the TID of the pixel below the top pixel.
3749 /* masks for thread ID. */
3750 #define TID_MASK_TOP_LEFT 0xfffffffc
3751 #define TID_MASK_TOP 0xfffffffd
3752 #define TID_MASK_LEFT 0xfffffffe
3754 static void si_llvm_emit_ddxy(
3755 const struct lp_build_tgsi_action
*action
,
3756 struct lp_build_tgsi_context
*bld_base
,
3757 struct lp_build_emit_data
*emit_data
)
3759 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3760 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3761 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3762 unsigned opcode
= inst
->Instruction
.Opcode
;
3763 LLVMValueRef indices
[2];
3764 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
3765 LLVMValueRef tl
, trbl
, result
[4];
3766 unsigned swizzle
[4];
3771 indices
[0] = bld_base
->uint_bld
.zero
;
3772 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3773 NULL
, 0, LLVMReadNoneAttribute
);
3774 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3777 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3778 mask
= TID_MASK_LEFT
;
3779 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3780 mask
= TID_MASK_TOP
;
3782 mask
= TID_MASK_TOP_LEFT
;
3784 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
3785 lp_build_const_int32(gallivm
, mask
), "");
3786 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3789 /* for DDX we want to next X pixel, DDY next Y pixel. */
3790 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3791 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
3792 lp_build_const_int32(gallivm
, idx
), "");
3793 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3796 for (c
= 0; c
< 4; ++c
) {
3799 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
3800 for (i
= 0; i
< c
; ++i
) {
3801 if (swizzle
[i
] == swizzle
[c
]) {
3802 result
[c
] = result
[i
];
3809 LLVMBuildStore(gallivm
->builder
,
3810 LLVMBuildBitCast(gallivm
->builder
,
3811 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
3815 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
3816 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3818 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
3819 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, ctx
->f32
, "");
3821 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
3824 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
3828 * this takes an I,J coordinate pair,
3829 * and works out the X and Y derivatives.
3830 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3832 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3833 struct lp_build_tgsi_context
*bld_base
,
3834 LLVMValueRef interp_ij
)
3836 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3837 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3838 LLVMValueRef indices
[2];
3839 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3840 LLVMValueRef tl
, tr
, bl
, result
[4];
3843 indices
[0] = bld_base
->uint_bld
.zero
;
3844 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3845 NULL
, 0, LLVMReadNoneAttribute
);
3846 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3849 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3850 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3852 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3853 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3856 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3860 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3863 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3864 lp_build_const_int32(gallivm
, 1), "");
3865 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3868 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3869 lp_build_const_int32(gallivm
, 2), "");
3870 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3873 for (c
= 0; c
< 2; ++c
) {
3874 LLVMValueRef store_val
;
3875 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3877 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3878 interp_ij
, c_ll
, "");
3879 LLVMBuildStore(gallivm
->builder
,
3883 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3884 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3886 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3887 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, ctx
->f32
, "");
3889 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3891 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3892 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3894 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3895 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, ctx
->f32
, "");
3897 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3900 return lp_build_gather_values(gallivm
, result
, 4);
3903 static void interp_fetch_args(
3904 struct lp_build_tgsi_context
*bld_base
,
3905 struct lp_build_emit_data
*emit_data
)
3907 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3908 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3909 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3911 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3912 /* offset is in second src, first two channels */
3913 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3916 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3919 emit_data
->arg_count
= 2;
3920 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3921 LLVMValueRef sample_position
;
3922 LLVMValueRef sample_id
;
3923 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3925 /* fetch sample ID, then fetch its sample position,
3926 * and place into first two channels.
3928 sample_id
= lp_build_emit_fetch(bld_base
,
3929 emit_data
->inst
, 1, TGSI_CHAN_X
);
3930 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3932 sample_position
= load_sample_position(&ctx
->radeon_bld
, sample_id
);
3934 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3936 lp_build_const_int32(gallivm
, 0), "");
3938 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3939 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3941 lp_build_const_int32(gallivm
, 1), "");
3942 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3943 emit_data
->arg_count
= 2;
3947 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3948 struct lp_build_tgsi_context
*bld_base
,
3949 struct lp_build_emit_data
*emit_data
)
3951 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3952 struct si_shader
*shader
= ctx
->shader
;
3953 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3954 LLVMValueRef interp_param
;
3955 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3956 const char *intr_name
;
3957 int input_index
= inst
->Src
[0].Register
.Index
;
3960 LLVMValueRef attr_number
;
3961 LLVMValueRef params
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3962 int interp_param_idx
;
3963 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
3966 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3968 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3969 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3970 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3972 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3974 interp_param_idx
= lookup_interp_param_index(interp
, location
);
3975 if (interp_param_idx
== -1)
3977 else if (interp_param_idx
)
3978 interp_param
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3980 interp_param
= NULL
;
3982 attr_number
= lp_build_const_int32(gallivm
, input_index
);
3984 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3985 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3986 LLVMValueRef ij_out
[2];
3987 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3990 * take the I then J parameters, and the DDX/Y for it, and
3991 * calculate the IJ inputs for the interpolator.
3992 * temp1 = ddx * offset/sample.x + I;
3993 * interp_param.I = ddy * offset/sample.y + temp1;
3994 * temp1 = ddx * offset/sample.x + J;
3995 * interp_param.J = ddy * offset/sample.y + temp1;
3997 for (i
= 0; i
< 2; i
++) {
3998 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3999 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
4000 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
4001 ddxy_out
, ix_ll
, "");
4002 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
4003 ddxy_out
, iy_ll
, "");
4004 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
4005 interp_param
, ix_ll
, "");
4006 LLVMValueRef temp1
, temp2
;
4008 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
4011 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
4013 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
4015 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
4017 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
4019 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
4020 temp2
, ctx
->i32
, "");
4022 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
4025 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
4026 for (chan
= 0; chan
< 2; chan
++) {
4027 LLVMValueRef args
[4];
4028 LLVMValueRef llvm_chan
;
4031 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
4032 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
4034 args
[0] = llvm_chan
;
4035 args
[1] = attr_number
;
4037 args
[3] = interp_param
;
4039 emit_data
->output
[chan
] =
4040 lp_build_intrinsic(gallivm
->builder
, intr_name
,
4041 ctx
->f32
, args
, args
[3] ? 4 : 3,
4042 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
4046 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
4047 struct lp_build_emit_data
*emit_data
)
4049 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
4050 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
4053 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
4055 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
4059 /* Emit one vertex from the geometry shader */
4060 static void si_llvm_emit_vertex(
4061 const struct lp_build_tgsi_action
*action
,
4062 struct lp_build_tgsi_context
*bld_base
,
4063 struct lp_build_emit_data
*emit_data
)
4065 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4066 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
4067 struct si_shader
*shader
= ctx
->shader
;
4068 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
4069 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4070 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4071 SI_PARAM_GS2VS_OFFSET
);
4072 LLVMValueRef gs_next_vertex
;
4073 LLVMValueRef can_emit
, kill
;
4074 LLVMValueRef args
[2];
4079 stream
= si_llvm_get_stream(bld_base
, emit_data
);
4081 /* Write vertex attribute values to GSVS ring */
4082 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
4083 ctx
->gs_next_vertex
[stream
],
4086 /* If this thread has already emitted the declared maximum number of
4087 * vertices, kill it: excessive vertex emissions are not supposed to
4088 * have any effect, and GS threads have no externally observable
4089 * effects other than emitting vertices.
4091 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
4092 lp_build_const_int32(gallivm
,
4093 shader
->selector
->gs_max_out_vertices
), "");
4094 kill
= lp_build_select(&bld_base
->base
, can_emit
,
4095 lp_build_const_float(gallivm
, 1.0f
),
4096 lp_build_const_float(gallivm
, -1.0f
));
4098 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
4099 ctx
->voidt
, &kill
, 1, 0);
4101 for (i
= 0; i
< info
->num_outputs
; i
++) {
4102 LLVMValueRef
*out_ptr
=
4103 ctx
->radeon_bld
.soa
.outputs
[i
];
4105 for (chan
= 0; chan
< 4; chan
++) {
4106 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
4107 LLVMValueRef voffset
=
4108 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
4109 shader
->selector
->gs_max_out_vertices
);
4111 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
4112 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
4114 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
4116 build_tbuffer_store(ctx
,
4117 ctx
->gsvs_ring
[stream
],
4119 voffset
, soffset
, 0,
4120 V_008F0C_BUF_DATA_FORMAT_32
,
4121 V_008F0C_BUF_NUM_FORMAT_UINT
,
4125 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
4126 lp_build_const_int32(gallivm
, 1));
4128 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
4130 /* Signal vertex emission */
4131 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
4132 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
4133 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
4134 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
4137 /* Cut one primitive from the geometry shader */
4138 static void si_llvm_emit_primitive(
4139 const struct lp_build_tgsi_action
*action
,
4140 struct lp_build_tgsi_context
*bld_base
,
4141 struct lp_build_emit_data
*emit_data
)
4143 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4144 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4145 LLVMValueRef args
[2];
4148 /* Signal primitive cut */
4149 stream
= si_llvm_get_stream(bld_base
, emit_data
);
4150 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
4151 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
4152 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
4153 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
4156 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4157 struct lp_build_tgsi_context
*bld_base
,
4158 struct lp_build_emit_data
*emit_data
)
4160 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4161 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4163 lp_build_intrinsic(gallivm
->builder
,
4164 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
4165 : "llvm.AMDGPU.barrier.local",
4166 ctx
->voidt
, NULL
, 0, LLVMNoUnwindAttribute
);
4169 static const struct lp_build_tgsi_action tex_action
= {
4170 .fetch_args
= tex_fetch_args
,
4171 .emit
= build_tex_intrinsic
,
4174 static const struct lp_build_tgsi_action interp_action
= {
4175 .fetch_args
= interp_fetch_args
,
4176 .emit
= build_interp_intrinsic
,
4179 static void si_create_function(struct si_shader_context
*ctx
,
4180 LLVMTypeRef
*returns
, unsigned num_returns
,
4181 LLVMTypeRef
*params
, unsigned num_params
,
4182 int last_array_pointer
, int last_sgpr
)
4186 radeon_llvm_create_func(&ctx
->radeon_bld
, returns
, num_returns
,
4187 params
, num_params
);
4188 radeon_llvm_shader_type(ctx
->radeon_bld
.main_fn
, ctx
->type
);
4189 ctx
->return_value
= LLVMGetUndef(ctx
->radeon_bld
.return_type
);
4191 for (i
= 0; i
<= last_sgpr
; ++i
) {
4192 LLVMValueRef P
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, i
);
4194 /* We tell llvm that array inputs are passed by value to allow Sinking pass
4195 * to move load. Inputs are constant so this is fine. */
4196 if (i
<= last_array_pointer
)
4197 LLVMAddAttribute(P
, LLVMByValAttribute
);
4199 LLVMAddAttribute(P
, LLVMInRegAttribute
);
4203 static void create_meta_data(struct si_shader_context
*ctx
)
4205 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
4206 LLVMValueRef args
[3];
4208 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
4210 args
[2] = lp_build_const_int32(gallivm
, 1);
4212 ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
4215 static void declare_streamout_params(struct si_shader_context
*ctx
,
4216 struct pipe_stream_output_info
*so
,
4217 LLVMTypeRef
*params
, LLVMTypeRef i32
,
4218 unsigned *num_params
)
4222 /* Streamout SGPRs. */
4223 if (so
->num_outputs
) {
4224 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
4225 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
4227 /* A streamout buffer offset is loaded if the stride is non-zero. */
4228 for (i
= 0; i
< 4; i
++) {
4232 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
4236 static unsigned llvm_get_type_size(LLVMTypeRef type
)
4238 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
4241 case LLVMIntegerTypeKind
:
4242 return LLVMGetIntTypeWidth(type
) / 8;
4243 case LLVMFloatTypeKind
:
4245 case LLVMPointerTypeKind
:
4247 case LLVMVectorTypeKind
:
4248 return LLVMGetVectorSize(type
) *
4249 llvm_get_type_size(LLVMGetElementType(type
));
4256 static void declare_tess_lds(struct si_shader_context
*ctx
)
4258 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
4259 LLVMTypeRef i32
= ctx
->radeon_bld
.soa
.bld_base
.uint_bld
.elem_type
;
4261 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
4262 unsigned vertex_data_dw_size
= 32*32*4;
4263 unsigned patch_data_dw_size
= 32*4;
4264 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
4265 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
4266 unsigned lds_dwords
= patch_dw_size
;
4268 /* The actual size is computed outside of the shader to reduce
4269 * the number of shader variants. */
4271 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4272 LLVMArrayType(i32
, lds_dwords
),
4277 static void create_function(struct si_shader_context
*ctx
)
4279 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4280 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4281 struct si_shader
*shader
= ctx
->shader
;
4282 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_NUM_VERTEX_BUFFERS
], v3i32
;
4283 LLVMTypeRef returns
[16+32*4];
4284 unsigned i
, last_array_pointer
, last_sgpr
, num_params
, num_return_sgprs
;
4285 unsigned num_returns
= 0;
4287 v3i32
= LLVMVectorType(ctx
->i32
, 3);
4289 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
4290 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
4291 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
4292 params
[SI_PARAM_IMAGES
] = const_array(ctx
->v8i32
, SI_NUM_IMAGES
);
4293 last_array_pointer
= SI_PARAM_IMAGES
;
4295 switch (ctx
->type
) {
4296 case TGSI_PROCESSOR_VERTEX
:
4297 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_VERTEX_BUFFERS
);
4298 last_array_pointer
= SI_PARAM_VERTEX_BUFFERS
;
4299 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
4300 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
4301 num_params
= SI_PARAM_START_INSTANCE
+1;
4303 if (shader
->key
.vs
.as_es
) {
4304 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4305 } else if (shader
->key
.vs
.as_ls
) {
4306 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
4307 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
4309 if (ctx
->is_gs_copy_shader
) {
4310 last_array_pointer
= SI_PARAM_CONST_BUFFERS
;
4311 num_params
= SI_PARAM_CONST_BUFFERS
+1;
4313 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
4314 num_params
= SI_PARAM_VS_STATE_BITS
+1;
4317 /* The locations of the other parameters are assigned dynamically. */
4318 declare_streamout_params(ctx
, &shader
->selector
->so
,
4319 params
, ctx
->i32
, &num_params
);
4322 last_sgpr
= num_params
-1;
4325 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
4326 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
4327 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
4328 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
4330 if (!ctx
->is_monolithic
&&
4331 !ctx
->is_gs_copy_shader
) {
4332 /* Vertex load indices. */
4333 ctx
->param_vertex_index0
= num_params
;
4335 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4336 params
[num_params
++] = ctx
->i32
;
4338 /* PrimitiveID output. */
4339 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
)
4340 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
4341 returns
[num_returns
++] = ctx
->f32
;
4345 case TGSI_PROCESSOR_TESS_CTRL
:
4346 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
4347 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
4348 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
4349 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
4350 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
4353 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
4354 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
4355 num_params
= SI_PARAM_REL_IDS
+1;
4357 if (!ctx
->is_monolithic
) {
4358 /* PARAM_TESS_FACTOR_OFFSET is after user SGPRs. */
4359 for (i
= 0; i
<= SI_TCS_NUM_USER_SGPR
; i
++)
4360 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4362 for (i
= 0; i
< 3; i
++)
4363 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4367 case TGSI_PROCESSOR_TESS_EVAL
:
4368 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
4369 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
4370 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
4372 if (shader
->key
.tes
.as_es
) {
4373 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4375 declare_streamout_params(ctx
, &shader
->selector
->so
,
4376 params
, ctx
->i32
, &num_params
);
4378 last_sgpr
= num_params
- 1;
4381 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
4382 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
4383 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
4384 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
4386 /* PrimitiveID output. */
4387 if (!ctx
->is_monolithic
&& !shader
->key
.tes
.as_es
)
4388 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
4389 returns
[num_returns
++] = ctx
->f32
;
4392 case TGSI_PROCESSOR_GEOMETRY
:
4393 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
4394 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
4395 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
4398 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
4399 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
4400 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
4401 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
4402 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
4403 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
4404 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
4405 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
4406 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
4409 case TGSI_PROCESSOR_FRAGMENT
:
4410 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
4411 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
4412 last_sgpr
= SI_PARAM_PRIM_MASK
;
4413 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
4414 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
4415 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
4416 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
4417 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
4418 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
4419 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
4420 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
4421 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
4422 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
4423 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
4424 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
4425 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
4426 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
4427 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
4428 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
4429 num_params
= SI_PARAM_POS_FIXED_PT
+1;
4431 if (!ctx
->is_monolithic
) {
4432 /* Color inputs from the prolog. */
4433 if (shader
->selector
->info
.colors_read
) {
4434 unsigned num_color_elements
=
4435 util_bitcount(shader
->selector
->info
.colors_read
);
4437 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
4438 for (i
= 0; i
< num_color_elements
; i
++)
4439 params
[num_params
++] = ctx
->f32
;
4442 /* Outputs for the epilog. */
4443 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4446 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4447 shader
->selector
->info
.writes_z
+
4448 shader
->selector
->info
.writes_stencil
+
4449 shader
->selector
->info
.writes_samplemask
+
4450 1 /* SampleMaskIn */;
4452 num_returns
= MAX2(num_returns
,
4454 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4456 for (i
= 0; i
< num_return_sgprs
; i
++)
4457 returns
[i
] = ctx
->i32
;
4458 for (; i
< num_returns
; i
++)
4459 returns
[i
] = ctx
->f32
;
4464 assert(0 && "unimplemented shader");
4468 assert(num_params
<= Elements(params
));
4470 si_create_function(ctx
, returns
, num_returns
, params
,
4471 num_params
, last_array_pointer
, last_sgpr
);
4473 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4474 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
&&
4475 !ctx
->is_monolithic
) {
4476 radeon_llvm_add_attribute(ctx
->radeon_bld
.main_fn
,
4477 "InitialPSInputAddr",
4478 S_0286D0_PERSP_SAMPLE_ENA(1) |
4479 S_0286D0_PERSP_CENTER_ENA(1) |
4480 S_0286D0_PERSP_CENTROID_ENA(1) |
4481 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4482 S_0286D0_LINEAR_CENTER_ENA(1) |
4483 S_0286D0_LINEAR_CENTROID_ENA(1) |
4484 S_0286D0_FRONT_FACE_ENA(1) |
4485 S_0286D0_POS_FIXED_PT_ENA(1));
4488 shader
->info
.num_input_sgprs
= 0;
4489 shader
->info
.num_input_vgprs
= 0;
4491 for (i
= 0; i
<= last_sgpr
; ++i
)
4492 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
4494 /* Unused fragment shader inputs are eliminated by the compiler,
4495 * so we don't know yet how many there will be.
4497 if (ctx
->type
!= TGSI_PROCESSOR_FRAGMENT
)
4498 for (; i
< num_params
; ++i
)
4499 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
4501 if (bld_base
->info
&&
4502 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
4503 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
4504 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
4505 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
4506 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
4507 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
4509 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4510 LLVMArrayType(ctx
->i32
, 64),
4514 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
4515 ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
4516 ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
)
4517 declare_tess_lds(ctx
);
4520 static void preload_constants(struct si_shader_context
*ctx
)
4522 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4523 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4524 const struct tgsi_shader_info
*info
= bld_base
->info
;
4526 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
4528 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
4529 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
4534 /* Allocate space for the constant values */
4535 ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
4537 /* Load the resource descriptor */
4538 ctx
->const_buffers
[buf
] =
4539 build_indexed_load_const(ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
4541 /* Load the constants, we rely on the code sinking to do the rest */
4542 for (i
= 0; i
< num_const
* 4; ++i
) {
4543 ctx
->constants
[buf
][i
] =
4544 buffer_load_const(gallivm
->builder
,
4545 ctx
->const_buffers
[buf
],
4546 lp_build_const_int32(gallivm
, i
* 4),
4552 static void preload_samplers(struct si_shader_context
*ctx
)
4554 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4555 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4556 const struct tgsi_shader_info
*info
= bld_base
->info
;
4557 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
4558 LLVMValueRef offset
;
4560 if (num_samplers
== 0)
4563 /* Load the resources and samplers, we rely on the code sinking to do the rest */
4564 for (i
= 0; i
< num_samplers
; ++i
) {
4566 offset
= lp_build_const_int32(gallivm
, i
);
4567 ctx
->sampler_views
[i
] =
4568 get_sampler_desc(ctx
, offset
, DESC_IMAGE
);
4570 /* FMASK resource */
4571 if (info
->is_msaa_sampler
[i
])
4573 get_sampler_desc(ctx
, offset
, DESC_FMASK
);
4575 ctx
->sampler_states
[i
] =
4576 get_sampler_desc(ctx
, offset
, DESC_SAMPLER
);
4580 static void preload_images(struct si_shader_context
*ctx
)
4582 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4583 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4584 unsigned num_images
= bld_base
->info
->file_max
[TGSI_FILE_IMAGE
] + 1;
4585 LLVMValueRef res_ptr
;
4588 if (num_images
== 0)
4591 res_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_IMAGES
);
4593 for (i
= 0; i
< num_images
; ++i
) {
4594 /* Rely on LLVM to shrink the load for buffer resources. */
4596 build_indexed_load_const(ctx
, res_ptr
,
4597 lp_build_const_int32(gallivm
, i
));
4601 static void preload_streamout_buffers(struct si_shader_context
*ctx
)
4603 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4604 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4607 /* Streamout can only be used if the shader is compiled as VS. */
4608 if (!ctx
->shader
->selector
->so
.num_outputs
||
4609 (ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4610 (ctx
->shader
->key
.vs
.as_es
||
4611 ctx
->shader
->key
.vs
.as_ls
)) ||
4612 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4613 ctx
->shader
->key
.tes
.as_es
))
4616 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4617 SI_PARAM_RW_BUFFERS
);
4619 /* Load the resources, we rely on the code sinking to do the rest */
4620 for (i
= 0; i
< 4; ++i
) {
4621 if (ctx
->shader
->selector
->so
.stride
[i
]) {
4622 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
4623 SI_SO_BUF_OFFSET
+ i
);
4625 ctx
->so_buffers
[i
] = build_indexed_load_const(ctx
, buf_ptr
, offset
);
4631 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
4634 static void preload_ring_buffers(struct si_shader_context
*ctx
)
4636 struct gallivm_state
*gallivm
=
4637 ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
4639 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4640 SI_PARAM_RW_BUFFERS
);
4642 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4643 ctx
->shader
->key
.vs
.as_es
) ||
4644 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4645 ctx
->shader
->key
.tes
.as_es
) ||
4646 ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4647 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
4650 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4653 if (ctx
->is_gs_copy_shader
) {
4654 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
4657 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4659 if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4661 for (i
= 0; i
< 4; i
++) {
4662 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
4665 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4670 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
4671 LLVMValueRef param_sampler_views
,
4672 unsigned param_pos_fixed_pt
)
4674 struct lp_build_tgsi_context
*bld_base
=
4675 &ctx
->radeon_bld
.soa
.bld_base
;
4676 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4677 struct lp_build_emit_data result
= {};
4678 struct tgsi_full_instruction inst
= {};
4679 LLVMValueRef desc
, sampler_index
, address
[2], pix
;
4681 /* Use the fixed-point gl_FragCoord input.
4682 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
4683 * per coordinate to get the repeating effect.
4685 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
4686 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
4688 /* Load the sampler view descriptor. */
4689 sampler_index
= lp_build_const_int32(gallivm
, SI_POLY_STIPPLE_SAMPLER
);
4690 desc
= get_sampler_desc_custom(ctx
, param_sampler_views
,
4691 sampler_index
, DESC_IMAGE
);
4693 /* Load the texel. */
4694 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
4695 inst
.Texture
.Texture
= TGSI_TEXTURE_2D_MSAA
; /* = use load, not load_mip */
4696 result
.inst
= &inst
;
4697 set_tex_fetch_args(ctx
, &result
, TGSI_OPCODE_TXF
,
4698 inst
.Texture
.Texture
,
4699 desc
, NULL
, address
, ARRAY_SIZE(address
), 0xf);
4700 build_tex_intrinsic(&tex_action
, bld_base
, &result
);
4702 /* Kill the thread accordingly. */
4703 pix
= LLVMBuildExtractElement(gallivm
->builder
, result
.output
[0],
4704 lp_build_const_int32(gallivm
, 3), "");
4705 pix
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, pix
);
4706 pix
= LLVMBuildFNeg(gallivm
->builder
, pix
, "");
4708 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
4709 LLVMVoidTypeInContext(gallivm
->context
),
4713 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
4714 struct si_shader_config
*conf
,
4715 unsigned symbol_offset
)
4718 const unsigned char *config
=
4719 radeon_shader_binary_config_start(binary
, symbol_offset
);
4721 /* XXX: We may be able to emit some of these values directly rather than
4722 * extracting fields to be emitted later.
4725 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
4726 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
4727 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
4729 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
4730 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
4731 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
4732 case R_00B848_COMPUTE_PGM_RSRC1
:
4733 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
4734 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
4735 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
4736 conf
->rsrc1
= value
;
4738 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
4739 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
4741 case R_00B84C_COMPUTE_PGM_RSRC2
:
4742 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
4743 conf
->rsrc2
= value
;
4745 case R_0286CC_SPI_PS_INPUT_ENA
:
4746 conf
->spi_ps_input_ena
= value
;
4748 case R_0286D0_SPI_PS_INPUT_ADDR
:
4749 conf
->spi_ps_input_addr
= value
;
4751 case R_0286E8_SPI_TMPRING_SIZE
:
4752 case R_00B860_COMPUTE_TMPRING_SIZE
:
4753 /* WAVESIZE is in units of 256 dwords. */
4754 conf
->scratch_bytes_per_wave
=
4755 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
4759 static bool printed
;
4762 fprintf(stderr
, "Warning: LLVM emitted unknown "
4763 "config register: 0x%x\n", reg
);
4770 if (!conf
->spi_ps_input_addr
)
4771 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
4775 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
4776 struct si_shader
*shader
,
4777 uint64_t scratch_va
)
4780 uint32_t scratch_rsrc_dword0
= scratch_va
;
4781 uint32_t scratch_rsrc_dword1
=
4782 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
4783 | S_008F04_STRIDE(shader
->config
.scratch_bytes_per_wave
/ 64);
4785 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
4786 const struct radeon_shader_reloc
*reloc
=
4787 &shader
->binary
.relocs
[i
];
4788 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
4789 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4790 &scratch_rsrc_dword0
, 4);
4791 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
4792 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4793 &scratch_rsrc_dword1
, 4);
4798 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
4800 unsigned size
= shader
->binary
.code_size
;
4803 size
+= shader
->prolog
->binary
.code_size
;
4805 size
+= shader
->epilog
->binary
.code_size
;
4809 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
4811 const struct radeon_shader_binary
*prolog
=
4812 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
4813 const struct radeon_shader_binary
*epilog
=
4814 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
4815 const struct radeon_shader_binary
*mainb
= &shader
->binary
;
4816 unsigned bo_size
= si_get_shader_binary_size(shader
) +
4817 (!epilog
? mainb
->rodata_size
: 0);
4820 assert(!prolog
|| !prolog
->rodata_size
);
4821 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
4822 assert(!epilog
|| !epilog
->rodata_size
);
4824 r600_resource_reference(&shader
->bo
, NULL
);
4825 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
4826 PIPE_USAGE_IMMUTABLE
,
4832 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
4833 PIPE_TRANSFER_READ_WRITE
);
4836 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
4837 ptr
+= prolog
->code_size
;
4840 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
4841 ptr
+= mainb
->code_size
;
4844 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
4845 else if (mainb
->rodata_size
> 0)
4846 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
4848 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
4852 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
4853 struct pipe_debug_callback
*debug
,
4854 const char *name
, FILE *file
)
4859 if (binary
->disasm_string
) {
4860 fprintf(file
, "Shader %s disassembly:\n", name
);
4861 fprintf(file
, "%s", binary
->disasm_string
);
4863 if (debug
&& debug
->debug_message
) {
4864 /* Very long debug messages are cut off, so send the
4865 * disassembly one line at a time. This causes more
4866 * overhead, but on the plus side it simplifies
4867 * parsing of resulting logs.
4869 pipe_debug_message(debug
, SHADER_INFO
,
4870 "Shader Disassembly Begin");
4872 line
= binary
->disasm_string
;
4874 p
= strchrnul(line
, '\n');
4878 pipe_debug_message(debug
, SHADER_INFO
,
4879 "%.*s", count
, line
);
4887 pipe_debug_message(debug
, SHADER_INFO
,
4888 "Shader Disassembly End");
4891 fprintf(file
, "Shader %s binary:\n", name
);
4892 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
4893 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
4894 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
4895 binary
->code
[i
+ 1], binary
->code
[i
]);
4900 static void si_shader_dump_stats(struct si_screen
*sscreen
,
4901 struct si_shader_config
*conf
,
4902 unsigned num_inputs
,
4904 struct pipe_debug_callback
*debug
,
4908 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
4909 unsigned lds_per_wave
= 0;
4910 unsigned max_simd_waves
= 10;
4912 /* Compute LDS usage for PS. */
4913 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4914 /* The minimum usage per wave is (num_inputs * 36). The maximum
4915 * usage is (num_inputs * 36 * 16).
4916 * We can get anything in between and it varies between waves.
4918 * Other stages don't know the size at compile time or don't
4919 * allocate LDS per wave, but instead they do it per thread group.
4921 lds_per_wave
= conf
->lds_size
* lds_increment
+
4922 align(num_inputs
* 36, lds_increment
);
4925 /* Compute the per-SIMD wave counts. */
4926 if (conf
->num_sgprs
) {
4927 if (sscreen
->b
.chip_class
>= VI
)
4928 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
4930 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
4933 if (conf
->num_vgprs
)
4934 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
4936 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
4940 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
4942 if (file
!= stderr
||
4943 r600_can_dump_shader(&sscreen
->b
, processor
)) {
4944 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4945 fprintf(file
, "*** SHADER CONFIG ***\n"
4946 "SPI_PS_INPUT_ADDR = 0x%04x\n"
4947 "SPI_PS_INPUT_ENA = 0x%04x\n",
4948 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
4951 fprintf(file
, "*** SHADER STATS ***\n"
4954 "Code Size: %d bytes\n"
4956 "Scratch: %d bytes per wave\n"
4958 "********************\n",
4959 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4960 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4964 pipe_debug_message(debug
, SHADER_INFO
,
4965 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
4966 "LDS: %d Scratch: %d Max Waves: %d",
4967 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4968 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4972 static const char *si_get_shader_name(struct si_shader
*shader
,
4975 switch (processor
) {
4976 case TGSI_PROCESSOR_VERTEX
:
4977 if (shader
->key
.vs
.as_es
)
4978 return "Vertex Shader as ES";
4979 else if (shader
->key
.vs
.as_ls
)
4980 return "Vertex Shader as LS";
4982 return "Vertex Shader as VS";
4983 case TGSI_PROCESSOR_TESS_CTRL
:
4984 return "Tessellation Control Shader";
4985 case TGSI_PROCESSOR_TESS_EVAL
:
4986 if (shader
->key
.tes
.as_es
)
4987 return "Tessellation Evaluation Shader as ES";
4989 return "Tessellation Evaluation Shader as VS";
4990 case TGSI_PROCESSOR_GEOMETRY
:
4991 if (shader
->gs_copy_shader
== NULL
)
4992 return "GS Copy Shader as VS";
4994 return "Geometry Shader";
4995 case TGSI_PROCESSOR_FRAGMENT
:
4996 return "Pixel Shader";
4997 case TGSI_PROCESSOR_COMPUTE
:
4998 return "Compute Shader";
5000 return "Unknown Shader";
5004 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
5005 struct pipe_debug_callback
*debug
, unsigned processor
,
5008 if (file
!= stderr
||
5009 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
5010 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
5011 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
5014 si_shader_dump_disassembly(&shader
->prolog
->binary
,
5015 debug
, "prolog", file
);
5017 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
5020 si_shader_dump_disassembly(&shader
->epilog
->binary
,
5021 debug
, "epilog", file
);
5022 fprintf(file
, "\n");
5025 si_shader_dump_stats(sscreen
, &shader
->config
,
5026 shader
->selector
? shader
->selector
->info
.num_inputs
: 0,
5027 si_get_shader_binary_size(shader
), debug
, processor
,
5031 int si_compile_llvm(struct si_screen
*sscreen
,
5032 struct radeon_shader_binary
*binary
,
5033 struct si_shader_config
*conf
,
5034 LLVMTargetMachineRef tm
,
5036 struct pipe_debug_callback
*debug
,
5041 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
5043 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
5044 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
5046 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
5047 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
5048 LLVMDumpModule(mod
);
5049 fprintf(stderr
, "\n");
5053 if (!si_replace_shader(count
, binary
)) {
5054 r
= radeon_llvm_compile(mod
, binary
,
5055 r600_get_llvm_processor_name(sscreen
->b
.family
), tm
,
5061 si_shader_binary_read_config(binary
, conf
, 0);
5063 /* Enable 64-bit and 16-bit denormals, because there is no performance
5066 * If denormals are enabled, all floating-point output modifiers are
5069 * Don't enable denormals for 32-bit floats, because:
5070 * - Floating-point output modifiers would be ignored by the hw.
5071 * - Some opcodes don't support denormals, such as v_mad_f32. We would
5072 * have to stop using those.
5073 * - SI & CI would be very slow.
5075 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
5077 FREE(binary
->config
);
5078 FREE(binary
->global_symbol_offsets
);
5079 binary
->config
= NULL
;
5080 binary
->global_symbol_offsets
= NULL
;
5082 /* Some shaders can't have rodata because their binaries can be
5085 if (binary
->rodata_size
&&
5086 (processor
== TGSI_PROCESSOR_VERTEX
||
5087 processor
== TGSI_PROCESSOR_TESS_CTRL
||
5088 processor
== TGSI_PROCESSOR_TESS_EVAL
||
5089 processor
== TGSI_PROCESSOR_FRAGMENT
)) {
5090 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
5097 /* Generate code for the hardware VS shader stage to go with a geometry shader */
5098 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
5099 struct si_shader_context
*ctx
,
5100 struct si_shader
*gs
,
5101 struct pipe_debug_callback
*debug
)
5103 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
5104 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
5105 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5106 struct si_shader_output_values
*outputs
;
5107 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
5108 LLVMValueRef args
[9];
5111 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
5113 si_init_shader_ctx(ctx
, sscreen
, ctx
->shader
, ctx
->tm
);
5114 ctx
->type
= TGSI_PROCESSOR_VERTEX
;
5115 ctx
->is_gs_copy_shader
= true;
5117 create_meta_data(ctx
);
5118 create_function(ctx
);
5119 preload_streamout_buffers(ctx
);
5120 preload_ring_buffers(ctx
);
5122 args
[0] = ctx
->gsvs_ring
[0];
5123 args
[1] = lp_build_mul_imm(uint
,
5124 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
5125 ctx
->param_vertex_id
),
5127 args
[3] = uint
->zero
;
5128 args
[4] = uint
->one
; /* OFFEN */
5129 args
[5] = uint
->zero
; /* IDXEN */
5130 args
[6] = uint
->one
; /* GLC */
5131 args
[7] = uint
->one
; /* SLC */
5132 args
[8] = uint
->zero
; /* TFE */
5134 /* Fetch vertex data from GSVS ring */
5135 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
5138 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
5139 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
5141 for (chan
= 0; chan
< 4; chan
++) {
5142 args
[2] = lp_build_const_int32(gallivm
,
5144 gs
->selector
->gs_max_out_vertices
* 16 * 4);
5146 outputs
[i
].values
[chan
] =
5147 LLVMBuildBitCast(gallivm
->builder
,
5148 lp_build_intrinsic(gallivm
->builder
,
5149 "llvm.SI.buffer.load.dword.i32.i32",
5151 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
5156 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
5158 LLVMBuildRet(gallivm
->builder
, ctx
->return_value
);
5160 /* Dump LLVM IR before any optimization passes */
5161 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
5162 r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
5163 LLVMDumpModule(bld_base
->base
.gallivm
->module
);
5165 radeon_llvm_finalize_module(&ctx
->radeon_bld
);
5167 r
= si_compile_llvm(sscreen
, &ctx
->shader
->binary
,
5168 &ctx
->shader
->config
, ctx
->tm
,
5169 bld_base
->base
.gallivm
->module
,
5170 debug
, TGSI_PROCESSOR_GEOMETRY
,
5173 if (r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
5174 fprintf(stderr
, "GS Copy Shader:\n");
5175 si_shader_dump(sscreen
, ctx
->shader
, debug
,
5176 TGSI_PROCESSOR_GEOMETRY
, stderr
);
5177 r
= si_shader_binary_upload(sscreen
, ctx
->shader
);
5180 radeon_llvm_dispose(&ctx
->radeon_bld
);
5186 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
5190 fprintf(f
, "SHADER KEY\n");
5193 case PIPE_SHADER_VERTEX
:
5194 fprintf(f
, " instance_divisors = {");
5195 for (i
= 0; i
< Elements(key
->vs
.prolog
.instance_divisors
); i
++)
5196 fprintf(f
, !i
? "%u" : ", %u",
5197 key
->vs
.prolog
.instance_divisors
[i
]);
5199 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
5200 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
5201 fprintf(f
, " export_prim_id = %u\n", key
->vs
.epilog
.export_prim_id
);
5204 case PIPE_SHADER_TESS_CTRL
:
5205 fprintf(f
, " prim_mode = %u\n", key
->tcs
.epilog
.prim_mode
);
5208 case PIPE_SHADER_TESS_EVAL
:
5209 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
5210 fprintf(f
, " export_prim_id = %u\n", key
->tes
.epilog
.export_prim_id
);
5213 case PIPE_SHADER_GEOMETRY
:
5216 case PIPE_SHADER_FRAGMENT
:
5217 fprintf(f
, " prolog.color_two_side = %u\n", key
->ps
.prolog
.color_two_side
);
5218 fprintf(f
, " prolog.poly_stipple = %u\n", key
->ps
.prolog
.poly_stipple
);
5219 fprintf(f
, " prolog.force_persample_interp = %u\n", key
->ps
.prolog
.force_persample_interp
);
5220 fprintf(f
, " epilog.spi_shader_col_format = 0x%x\n", key
->ps
.epilog
.spi_shader_col_format
);
5221 fprintf(f
, " epilog.color_is_int8 = 0x%X\n", key
->ps
.epilog
.color_is_int8
);
5222 fprintf(f
, " epilog.last_cbuf = %u\n", key
->ps
.epilog
.last_cbuf
);
5223 fprintf(f
, " epilog.alpha_func = %u\n", key
->ps
.epilog
.alpha_func
);
5224 fprintf(f
, " epilog.alpha_to_one = %u\n", key
->ps
.epilog
.alpha_to_one
);
5225 fprintf(f
, " epilog.poly_line_smoothing = %u\n", key
->ps
.epilog
.poly_line_smoothing
);
5226 fprintf(f
, " epilog.clamp_color = %u\n", key
->ps
.epilog
.clamp_color
);
5234 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5235 struct si_screen
*sscreen
,
5236 struct si_shader
*shader
,
5237 LLVMTargetMachineRef tm
)
5239 struct lp_build_tgsi_context
*bld_base
;
5240 struct lp_build_tgsi_action tmpl
;
5242 memset(ctx
, 0, sizeof(*ctx
));
5243 radeon_llvm_context_init(&ctx
->radeon_bld
, "amdgcn--");
5245 ctx
->screen
= sscreen
;
5246 if (shader
&& shader
->selector
)
5247 ctx
->type
= shader
->selector
->info
.processor
;
5250 ctx
->shader
= shader
;
5252 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5253 ctx
->i1
= LLVMInt1TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5254 ctx
->i8
= LLVMInt8TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5255 ctx
->i32
= LLVMInt32TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5256 ctx
->i64
= LLVMInt64TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5257 ctx
->i128
= LLVMIntTypeInContext(ctx
->radeon_bld
.gallivm
.context
, 128);
5258 ctx
->f32
= LLVMFloatTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5259 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
5260 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
5261 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
5262 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
5263 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
5265 bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
5266 if (shader
&& shader
->selector
)
5267 bld_base
->info
= &shader
->selector
->info
;
5268 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5270 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
5271 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
5272 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
5274 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
5275 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
5276 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
5277 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
5278 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
5279 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
5280 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
5281 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
5282 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
5283 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
5284 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
5285 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
5286 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
5288 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
5289 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
5290 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
5291 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
5292 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
5293 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
5295 tmpl
.fetch_args
= atomic_fetch_args
;
5296 tmpl
.emit
= atomic_emit
;
5297 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
5298 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
5299 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
5300 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
5301 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
5302 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
5303 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
5304 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
5305 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
5306 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
5307 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
5308 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
5309 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
5310 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
5311 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
5312 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
5313 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
5314 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
5315 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
5316 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
5318 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5319 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5320 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5321 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5323 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
5324 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
5325 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5327 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
5328 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
5329 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
5330 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
5333 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
5334 LLVMTargetMachineRef tm
,
5335 struct si_shader
*shader
,
5337 struct pipe_debug_callback
*debug
)
5339 struct si_shader_selector
*sel
= shader
->selector
;
5340 struct si_shader_context ctx
;
5341 struct lp_build_tgsi_context
*bld_base
;
5345 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
5346 * conversion fails. */
5347 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
5348 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
5349 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
5350 tgsi_dump(sel
->tokens
, 0);
5351 si_dump_streamout(&sel
->so
);
5354 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
5355 ctx
.is_monolithic
= is_monolithic
;
5357 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
5359 bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5360 ctx
.radeon_bld
.load_system_value
= declare_system_value
;
5363 case TGSI_PROCESSOR_VERTEX
:
5364 ctx
.radeon_bld
.load_input
= declare_input_vs
;
5365 if (shader
->key
.vs
.as_ls
)
5366 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
5367 else if (shader
->key
.vs
.as_es
)
5368 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5370 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5372 case TGSI_PROCESSOR_TESS_CTRL
:
5373 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
5374 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
5375 bld_base
->emit_store
= store_output_tcs
;
5376 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
5378 case TGSI_PROCESSOR_TESS_EVAL
:
5379 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
5380 if (shader
->key
.tes
.as_es
)
5381 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5383 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5385 case TGSI_PROCESSOR_GEOMETRY
:
5386 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
5387 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
5389 case TGSI_PROCESSOR_FRAGMENT
:
5390 ctx
.radeon_bld
.load_input
= declare_input_fs
;
5392 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
5394 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
5397 assert(!"Unsupported shader type");
5401 create_meta_data(&ctx
);
5402 create_function(&ctx
);
5403 preload_constants(&ctx
);
5404 preload_samplers(&ctx
);
5405 preload_images(&ctx
);
5406 preload_streamout_buffers(&ctx
);
5407 preload_ring_buffers(&ctx
);
5409 if (ctx
.is_monolithic
&& sel
->type
== PIPE_SHADER_FRAGMENT
&&
5410 shader
->key
.ps
.prolog
.poly_stipple
) {
5411 LLVMValueRef views
= LLVMGetParam(ctx
.radeon_bld
.main_fn
,
5413 si_llvm_emit_polygon_stipple(&ctx
, views
,
5414 SI_PARAM_POS_FIXED_PT
);
5417 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
5419 for (i
= 0; i
< 4; i
++) {
5420 ctx
.gs_next_vertex
[i
] =
5421 lp_build_alloca(bld_base
->base
.gallivm
,
5426 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
5427 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
5431 LLVMBuildRet(bld_base
->base
.gallivm
->builder
, ctx
.return_value
);
5432 mod
= bld_base
->base
.gallivm
->module
;
5434 /* Dump LLVM IR before any optimization passes */
5435 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
5436 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
5437 LLVMDumpModule(mod
);
5439 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5441 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
5442 mod
, debug
, ctx
.type
, "TGSI shader");
5444 fprintf(stderr
, "LLVM failed to compile shader\n");
5448 radeon_llvm_dispose(&ctx
.radeon_bld
);
5450 /* Calculate the number of fragment input VGPRs. */
5451 if (ctx
.type
== TGSI_PROCESSOR_FRAGMENT
) {
5452 shader
->info
.num_input_vgprs
= 0;
5453 shader
->info
.face_vgpr_index
= -1;
5455 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
5456 shader
->info
.num_input_vgprs
+= 2;
5457 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
5458 shader
->info
.num_input_vgprs
+= 2;
5459 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
5460 shader
->info
.num_input_vgprs
+= 2;
5461 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
5462 shader
->info
.num_input_vgprs
+= 3;
5463 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
5464 shader
->info
.num_input_vgprs
+= 2;
5465 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
5466 shader
->info
.num_input_vgprs
+= 2;
5467 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
5468 shader
->info
.num_input_vgprs
+= 2;
5469 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
5470 shader
->info
.num_input_vgprs
+= 1;
5471 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5472 shader
->info
.num_input_vgprs
+= 1;
5473 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5474 shader
->info
.num_input_vgprs
+= 1;
5475 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5476 shader
->info
.num_input_vgprs
+= 1;
5477 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5478 shader
->info
.num_input_vgprs
+= 1;
5479 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
5480 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
5481 shader
->info
.num_input_vgprs
+= 1;
5483 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
5484 shader
->info
.num_input_vgprs
+= 1;
5485 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
5486 shader
->info
.num_input_vgprs
+= 1;
5487 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
5488 shader
->info
.num_input_vgprs
+= 1;
5491 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
5492 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
5493 shader
->gs_copy_shader
->selector
= shader
->selector
;
5494 ctx
.shader
= shader
->gs_copy_shader
;
5495 if ((r
= si_generate_gs_copy_shader(sscreen
, &ctx
,
5497 free(shader
->gs_copy_shader
);
5498 shader
->gs_copy_shader
= NULL
;
5504 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
5505 FREE(ctx
.constants
[i
]);
5510 * Create, compile and return a shader part (prolog or epilog).
5512 * \param sscreen screen
5513 * \param list list of shader parts of the same category
5514 * \param key shader part key
5515 * \param tm LLVM target machine
5516 * \param debug debug callback
5517 * \param compile the callback responsible for compilation
5518 * \return non-NULL on success
5520 static struct si_shader_part
*
5521 si_get_shader_part(struct si_screen
*sscreen
,
5522 struct si_shader_part
**list
,
5523 union si_shader_part_key
*key
,
5524 LLVMTargetMachineRef tm
,
5525 struct pipe_debug_callback
*debug
,
5526 bool (*compile
)(struct si_screen
*,
5527 LLVMTargetMachineRef
,
5528 struct pipe_debug_callback
*,
5529 struct si_shader_part
*))
5531 struct si_shader_part
*result
;
5533 pipe_mutex_lock(sscreen
->shader_parts_mutex
);
5535 /* Find existing. */
5536 for (result
= *list
; result
; result
= result
->next
) {
5537 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
5538 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5543 /* Compile a new one. */
5544 result
= CALLOC_STRUCT(si_shader_part
);
5546 if (!compile(sscreen
, tm
, debug
, result
)) {
5548 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5552 result
->next
= *list
;
5554 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5559 * Create a vertex shader prolog.
5561 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
5562 * All inputs are returned unmodified. The vertex load indices are
5563 * stored after them, which will used by the API VS for fetching inputs.
5565 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
5570 * (VertexID + BaseVertex),
5571 * (InstanceID + StartInstance),
5572 * (InstanceID / 2 + StartInstance)
5574 static bool si_compile_vs_prolog(struct si_screen
*sscreen
,
5575 LLVMTargetMachineRef tm
,
5576 struct pipe_debug_callback
*debug
,
5577 struct si_shader_part
*out
)
5579 union si_shader_part_key
*key
= &out
->key
;
5580 struct si_shader shader
= {};
5581 struct si_shader_context ctx
;
5582 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5583 LLVMTypeRef
*params
, *returns
;
5584 LLVMValueRef ret
, func
;
5585 int last_sgpr
, num_params
, num_returns
, i
;
5588 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5589 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
5590 ctx
.param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
5591 ctx
.param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
5593 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
5594 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
5595 sizeof(LLVMTypeRef
));
5596 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
5597 key
->vs_prolog
.last_input
+ 1) *
5598 sizeof(LLVMTypeRef
));
5602 /* Declare input and output SGPRs. */
5604 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
5605 params
[num_params
++] = ctx
.i32
;
5606 returns
[num_returns
++] = ctx
.i32
;
5608 last_sgpr
= num_params
- 1;
5610 /* 4 preloaded VGPRs (outputs must be floats) */
5611 for (i
= 0; i
< 4; i
++) {
5612 params
[num_params
++] = ctx
.i32
;
5613 returns
[num_returns
++] = ctx
.f32
;
5616 /* Vertex load indices. */
5617 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
5618 returns
[num_returns
++] = ctx
.f32
;
5620 /* Create the function. */
5621 si_create_function(&ctx
, returns
, num_returns
, params
,
5622 num_params
, -1, last_sgpr
);
5623 func
= ctx
.radeon_bld
.main_fn
;
5625 /* Copy inputs to outputs. This should be no-op, as the registers match,
5626 * but it will prevent the compiler from overwriting them unintentionally.
5628 ret
= ctx
.return_value
;
5629 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
5630 LLVMValueRef p
= LLVMGetParam(func
, i
);
5631 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5633 for (i
= num_params
- 4; i
< num_params
; i
++) {
5634 LLVMValueRef p
= LLVMGetParam(func
, i
);
5635 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
.f32
, "");
5636 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5639 /* Compute vertex load indices from instance divisors. */
5640 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
5641 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
5645 /* InstanceID / Divisor + StartInstance */
5646 index
= get_instance_index_for_fetch(&ctx
.radeon_bld
,
5647 SI_SGPR_START_INSTANCE
,
5650 /* VertexID + BaseVertex */
5651 index
= LLVMBuildAdd(gallivm
->builder
,
5652 LLVMGetParam(func
, ctx
.param_vertex_id
),
5653 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
5656 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
.f32
, "");
5657 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
5662 LLVMBuildRet(gallivm
->builder
, ret
);
5663 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5665 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5666 gallivm
->module
, debug
, ctx
.type
,
5667 "Vertex Shader Prolog"))
5670 radeon_llvm_dispose(&ctx
.radeon_bld
);
5675 * Compile the vertex shader epilog. This is also used by the tessellation
5676 * evaluation shader compiled as VS.
5678 * The input is PrimitiveID.
5680 * If PrimitiveID is required by the pixel shader, export it.
5681 * Otherwise, do nothing.
5683 static bool si_compile_vs_epilog(struct si_screen
*sscreen
,
5684 LLVMTargetMachineRef tm
,
5685 struct pipe_debug_callback
*debug
,
5686 struct si_shader_part
*out
)
5688 union si_shader_part_key
*key
= &out
->key
;
5689 struct si_shader_context ctx
;
5690 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5691 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5692 LLVMTypeRef params
[5];
5696 si_init_shader_ctx(&ctx
, sscreen
, NULL
, tm
);
5697 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
5699 /* Declare input VGPRs. */
5700 num_params
= key
->vs_epilog
.states
.export_prim_id
?
5701 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
5702 assert(num_params
<= ARRAY_SIZE(params
));
5704 for (i
= 0; i
< num_params
; i
++)
5705 params
[i
] = ctx
.f32
;
5707 /* Create the function. */
5708 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5712 if (key
->vs_epilog
.states
.export_prim_id
) {
5713 struct lp_build_context
*base
= &bld_base
->base
;
5714 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5715 LLVMValueRef args
[9];
5717 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
5718 args
[1] = uint
->zero
; /* whether the EXEC mask is valid */
5719 args
[2] = uint
->zero
; /* DONE bit */
5720 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_PARAM
+
5721 key
->vs_epilog
.prim_id_param_offset
);
5722 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
5723 args
[5] = LLVMGetParam(ctx
.radeon_bld
.main_fn
,
5724 VS_EPILOG_PRIMID_LOC
); /* X */
5725 args
[6] = uint
->undef
; /* Y */
5726 args
[7] = uint
->undef
; /* Z */
5727 args
[8] = uint
->undef
; /* W */
5729 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
5730 LLVMVoidTypeInContext(base
->gallivm
->context
),
5735 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5736 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5738 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5739 gallivm
->module
, debug
, ctx
.type
,
5740 "Vertex Shader Epilog"))
5743 radeon_llvm_dispose(&ctx
.radeon_bld
);
5748 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
5750 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
5751 LLVMTargetMachineRef tm
,
5752 struct si_shader
*shader
,
5753 struct pipe_debug_callback
*debug
,
5754 struct si_vs_epilog_bits
*states
)
5756 union si_shader_part_key epilog_key
;
5758 memset(&epilog_key
, 0, sizeof(epilog_key
));
5759 epilog_key
.vs_epilog
.states
= *states
;
5761 /* Set up the PrimitiveID output. */
5762 if (shader
->key
.vs
.epilog
.export_prim_id
) {
5763 unsigned index
= shader
->selector
->info
.num_outputs
;
5764 unsigned offset
= shader
->info
.nr_param_exports
++;
5766 epilog_key
.vs_epilog
.prim_id_param_offset
= offset
;
5767 assert(index
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
5768 shader
->info
.vs_output_param_offset
[index
] = offset
;
5771 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
5772 &epilog_key
, tm
, debug
,
5773 si_compile_vs_epilog
);
5774 return shader
->epilog
!= NULL
;
5778 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
5780 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
5781 LLVMTargetMachineRef tm
,
5782 struct si_shader
*shader
,
5783 struct pipe_debug_callback
*debug
)
5785 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5786 union si_shader_part_key prolog_key
;
5789 /* Get the prolog. */
5790 memset(&prolog_key
, 0, sizeof(prolog_key
));
5791 prolog_key
.vs_prolog
.states
= shader
->key
.vs
.prolog
;
5792 prolog_key
.vs_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
5793 prolog_key
.vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
5795 /* The prolog is a no-op if there are no inputs. */
5796 if (info
->num_inputs
) {
5798 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
5799 &prolog_key
, tm
, debug
,
5800 si_compile_vs_prolog
);
5801 if (!shader
->prolog
)
5805 /* Get the epilog. */
5806 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
&&
5807 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5808 &shader
->key
.vs
.epilog
))
5811 /* Set the instanceID flag. */
5812 for (i
= 0; i
< info
->num_inputs
; i
++)
5813 if (prolog_key
.vs_prolog
.states
.instance_divisors
[i
])
5814 shader
->info
.uses_instanceid
= true;
5820 * Select and compile (or reuse) TES parts (epilog).
5822 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
5823 LLVMTargetMachineRef tm
,
5824 struct si_shader
*shader
,
5825 struct pipe_debug_callback
*debug
)
5827 if (shader
->key
.tes
.as_es
)
5830 /* TES compiled as VS. */
5831 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5832 &shader
->key
.tes
.epilog
);
5836 * Compile the TCS epilog. This writes tesselation factors to memory based on
5837 * the output primitive type of the tesselator (determined by TES).
5839 static bool si_compile_tcs_epilog(struct si_screen
*sscreen
,
5840 LLVMTargetMachineRef tm
,
5841 struct pipe_debug_callback
*debug
,
5842 struct si_shader_part
*out
)
5844 union si_shader_part_key
*key
= &out
->key
;
5845 struct si_shader shader
= {};
5846 struct si_shader_context ctx
;
5847 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5848 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5849 LLVMTypeRef params
[16];
5851 int last_array_pointer
, last_sgpr
, num_params
;
5854 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5855 ctx
.type
= TGSI_PROCESSOR_TESS_CTRL
;
5856 shader
.key
.tcs
.epilog
= key
->tcs_epilog
.states
;
5858 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
5859 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
.v16i8
, SI_NUM_RW_BUFFERS
);
5860 last_array_pointer
= SI_PARAM_RW_BUFFERS
;
5861 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
5862 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
5863 params
[SI_PARAM_IMAGES
] = ctx
.i64
;
5864 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
.i32
;
5865 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
.i32
;
5866 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
.i32
;
5867 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
.i32
;
5868 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5869 num_params
= last_sgpr
+ 1;
5871 params
[num_params
++] = ctx
.i32
; /* patch index within the wave (REL_PATCH_ID) */
5872 params
[num_params
++] = ctx
.i32
; /* invocation ID within the patch */
5873 params
[num_params
++] = ctx
.i32
; /* LDS offset where tess factors should be loaded from */
5875 /* Create the function. */
5876 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5877 last_array_pointer
, last_sgpr
);
5878 declare_tess_lds(&ctx
);
5879 func
= ctx
.radeon_bld
.main_fn
;
5881 si_write_tess_factors(bld_base
,
5882 LLVMGetParam(func
, last_sgpr
+ 1),
5883 LLVMGetParam(func
, last_sgpr
+ 2),
5884 LLVMGetParam(func
, last_sgpr
+ 3));
5887 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5888 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5890 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5891 gallivm
->module
, debug
, ctx
.type
,
5892 "Tessellation Control Shader Epilog"))
5895 radeon_llvm_dispose(&ctx
.radeon_bld
);
5900 * Select and compile (or reuse) TCS parts (epilog).
5902 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
5903 LLVMTargetMachineRef tm
,
5904 struct si_shader
*shader
,
5905 struct pipe_debug_callback
*debug
)
5907 union si_shader_part_key epilog_key
;
5909 /* Get the epilog. */
5910 memset(&epilog_key
, 0, sizeof(epilog_key
));
5911 epilog_key
.tcs_epilog
.states
= shader
->key
.tcs
.epilog
;
5913 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
5914 &epilog_key
, tm
, debug
,
5915 si_compile_tcs_epilog
);
5916 return shader
->epilog
!= NULL
;
5920 * Compile the pixel shader prolog. This handles:
5921 * - two-side color selection and interpolation
5922 * - overriding interpolation parameters for the API PS
5923 * - polygon stippling
5925 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
5926 * overriden by other states. (e.g. per-sample interpolation)
5927 * Interpolated colors are stored after the preloaded VGPRs.
5929 static bool si_compile_ps_prolog(struct si_screen
*sscreen
,
5930 LLVMTargetMachineRef tm
,
5931 struct pipe_debug_callback
*debug
,
5932 struct si_shader_part
*out
)
5934 union si_shader_part_key
*key
= &out
->key
;
5935 struct si_shader shader
= {};
5936 struct si_shader_context ctx
;
5937 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5938 LLVMTypeRef
*params
;
5939 LLVMValueRef ret
, func
;
5940 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
5943 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5944 ctx
.type
= TGSI_PROCESSOR_FRAGMENT
;
5945 shader
.key
.ps
.prolog
= key
->ps_prolog
.states
;
5947 /* Number of inputs + 8 color elements. */
5948 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
5949 key
->ps_prolog
.num_input_vgprs
+ 8) *
5950 sizeof(LLVMTypeRef
));
5952 /* Declare inputs. */
5954 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
5955 params
[num_params
++] = ctx
.i32
;
5956 last_sgpr
= num_params
- 1;
5958 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
5959 params
[num_params
++] = ctx
.f32
;
5961 /* Declare outputs (same as inputs + add colors if needed) */
5962 num_returns
= num_params
;
5963 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
5964 for (i
= 0; i
< num_color_channels
; i
++)
5965 params
[num_returns
++] = ctx
.f32
;
5967 /* Create the function. */
5968 si_create_function(&ctx
, params
, num_returns
, params
,
5969 num_params
, -1, last_sgpr
);
5970 func
= ctx
.radeon_bld
.main_fn
;
5972 /* Copy inputs to outputs. This should be no-op, as the registers match,
5973 * but it will prevent the compiler from overwriting them unintentionally.
5975 ret
= ctx
.return_value
;
5976 for (i
= 0; i
< num_params
; i
++) {
5977 LLVMValueRef p
= LLVMGetParam(func
, i
);
5978 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5981 /* Polygon stippling. */
5982 if (key
->ps_prolog
.states
.poly_stipple
) {
5983 /* POS_FIXED_PT is always last. */
5984 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
5985 key
->ps_prolog
.num_input_vgprs
- 1;
5986 LLVMValueRef ptr
[2], views
;
5988 /* Get the pointer to sampler views. */
5989 ptr
[0] = LLVMGetParam(func
, SI_SGPR_SAMPLERS
);
5990 ptr
[1] = LLVMGetParam(func
, SI_SGPR_SAMPLERS
+1);
5991 views
= lp_build_gather_values(gallivm
, ptr
, 2);
5992 views
= LLVMBuildBitCast(gallivm
->builder
, views
, ctx
.i64
, "");
5993 views
= LLVMBuildIntToPtr(gallivm
->builder
, views
,
5994 const_array(ctx
.v8i32
, SI_NUM_SAMPLERS
), "");
5996 si_llvm_emit_polygon_stipple(&ctx
, views
, pos
);
5999 /* Interpolate colors. */
6000 for (i
= 0; i
< 2; i
++) {
6001 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
6002 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
6003 key
->ps_prolog
.face_vgpr_index
;
6004 LLVMValueRef interp
[2], color
[4];
6005 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
6010 /* If the interpolation qualifier is not CONSTANT (-1). */
6011 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
6012 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
6013 key
->ps_prolog
.color_interp_vgpr_index
[i
];
6015 interp
[0] = LLVMGetParam(func
, interp_vgpr
);
6016 interp
[1] = LLVMGetParam(func
, interp_vgpr
+ 1);
6017 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
6018 interp_ij
= LLVMBuildBitCast(gallivm
->builder
, interp_ij
,
6022 /* Use the absolute location of the input. */
6023 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
6025 if (key
->ps_prolog
.states
.color_two_side
) {
6026 face
= LLVMGetParam(func
, face_vgpr
);
6027 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
.i32
, "");
6030 interp_fs_input(&ctx
,
6031 key
->ps_prolog
.color_attr_index
[i
],
6032 TGSI_SEMANTIC_COLOR
, i
,
6033 key
->ps_prolog
.num_interp_inputs
,
6034 key
->ps_prolog
.colors_read
, interp_ij
,
6035 prim_mask
, face
, color
);
6038 unsigned chan
= u_bit_scan(&writemask
);
6039 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
6044 /* Force per-sample interpolation. */
6045 if (key
->ps_prolog
.states
.force_persample_interp
) {
6046 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
6047 LLVMValueRef persp_sample
[2], linear_sample
[2];
6049 /* Read PERSP_SAMPLE. */
6050 for (i
= 0; i
< 2; i
++)
6051 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
6052 /* Overwrite PERSP_CENTER. */
6053 for (i
= 0; i
< 2; i
++)
6054 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6055 persp_sample
[i
], base
+ 2 + i
, "");
6056 /* Overwrite PERSP_CENTROID. */
6057 for (i
= 0; i
< 2; i
++)
6058 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6059 persp_sample
[i
], base
+ 4 + i
, "");
6060 /* Read LINEAR_SAMPLE. */
6061 for (i
= 0; i
< 2; i
++)
6062 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
6063 /* Overwrite LINEAR_CENTER. */
6064 for (i
= 0; i
< 2; i
++)
6065 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6066 linear_sample
[i
], base
+ 8 + i
, "");
6067 /* Overwrite LINEAR_CENTROID. */
6068 for (i
= 0; i
< 2; i
++)
6069 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
6070 linear_sample
[i
], base
+ 10 + i
, "");
6074 LLVMBuildRet(gallivm
->builder
, ret
);
6075 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
6077 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
6078 gallivm
->module
, debug
, ctx
.type
,
6079 "Fragment Shader Prolog"))
6082 radeon_llvm_dispose(&ctx
.radeon_bld
);
6087 * Compile the pixel shader epilog. This handles everything that must be
6088 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
6090 static bool si_compile_ps_epilog(struct si_screen
*sscreen
,
6091 LLVMTargetMachineRef tm
,
6092 struct pipe_debug_callback
*debug
,
6093 struct si_shader_part
*out
)
6095 union si_shader_part_key
*key
= &out
->key
;
6096 struct si_shader shader
= {};
6097 struct si_shader_context ctx
;
6098 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
6099 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
6100 LLVMTypeRef params
[16+8*4+3];
6101 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
6102 int last_array_pointer
, last_sgpr
, num_params
, i
;
6105 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
6106 ctx
.type
= TGSI_PROCESSOR_FRAGMENT
;
6107 shader
.key
.ps
.epilog
= key
->ps_epilog
.states
;
6109 /* Declare input SGPRs. */
6110 params
[SI_PARAM_RW_BUFFERS
] = ctx
.i64
;
6111 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
6112 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
6113 params
[SI_PARAM_IMAGES
] = ctx
.i64
;
6114 params
[SI_PARAM_ALPHA_REF
] = ctx
.f32
;
6115 last_array_pointer
= -1;
6116 last_sgpr
= SI_PARAM_ALPHA_REF
;
6118 /* Declare input VGPRs. */
6119 num_params
= (last_sgpr
+ 1) +
6120 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
6121 key
->ps_epilog
.writes_z
+
6122 key
->ps_epilog
.writes_stencil
+
6123 key
->ps_epilog
.writes_samplemask
;
6125 num_params
= MAX2(num_params
,
6126 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
6128 assert(num_params
<= ARRAY_SIZE(params
));
6130 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
6131 params
[i
] = ctx
.f32
;
6133 /* Create the function. */
6134 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
6135 last_array_pointer
, last_sgpr
);
6136 /* Disable elimination of unused inputs. */
6137 radeon_llvm_add_attribute(ctx
.radeon_bld
.main_fn
,
6138 "InitialPSInputAddr", 0xffffff);
6140 /* Process colors. */
6141 unsigned vgpr
= last_sgpr
+ 1;
6142 unsigned colors_written
= key
->ps_epilog
.colors_written
;
6143 int last_color_export
= -1;
6145 /* Find the last color export. */
6146 if (!key
->ps_epilog
.writes_z
&&
6147 !key
->ps_epilog
.writes_stencil
&&
6148 !key
->ps_epilog
.writes_samplemask
) {
6149 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
6151 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
6152 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
6153 /* Just set this if any of the colorbuffers are enabled. */
6155 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
6156 last_color_export
= 0;
6158 for (i
= 0; i
< 8; i
++)
6159 if (colors_written
& (1 << i
) &&
6160 (spi_format
>> (i
* 4)) & 0xf)
6161 last_color_export
= i
;
6165 while (colors_written
) {
6166 LLVMValueRef color
[4];
6167 int mrt
= u_bit_scan(&colors_written
);
6169 for (i
= 0; i
< 4; i
++)
6170 color
[i
] = LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6172 si_export_mrt_color(bld_base
, color
, mrt
,
6174 mrt
== last_color_export
);
6177 /* Process depth, stencil, samplemask. */
6178 if (key
->ps_epilog
.writes_z
)
6179 depth
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6180 if (key
->ps_epilog
.writes_stencil
)
6181 stencil
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6182 if (key
->ps_epilog
.writes_samplemask
)
6183 samplemask
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6185 if (depth
|| stencil
|| samplemask
)
6186 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
6187 else if (last_color_export
== -1)
6188 si_export_null(bld_base
);
6191 LLVMBuildRetVoid(gallivm
->builder
);
6192 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
6194 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
6195 gallivm
->module
, debug
, ctx
.type
,
6196 "Fragment Shader Epilog"))
6199 radeon_llvm_dispose(&ctx
.radeon_bld
);
6204 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
6206 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
6207 LLVMTargetMachineRef tm
,
6208 struct si_shader
*shader
,
6209 struct pipe_debug_callback
*debug
)
6211 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6212 union si_shader_part_key prolog_key
;
6213 union si_shader_part_key epilog_key
;
6216 /* Get the prolog. */
6217 memset(&prolog_key
, 0, sizeof(prolog_key
));
6218 prolog_key
.ps_prolog
.states
= shader
->key
.ps
.prolog
;
6219 prolog_key
.ps_prolog
.colors_read
= info
->colors_read
;
6220 prolog_key
.ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6221 prolog_key
.ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6223 if (info
->colors_read
) {
6224 unsigned *color
= shader
->selector
->color_attr_index
;
6226 if (shader
->key
.ps
.prolog
.color_two_side
) {
6227 /* BCOLORs are stored after the last input. */
6228 prolog_key
.ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6229 prolog_key
.ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6230 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6233 for (i
= 0; i
< 2; i
++) {
6234 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6236 if (!(info
->colors_read
& (0xf << i
*4)))
6239 prolog_key
.ps_prolog
.color_attr_index
[i
] = color
[i
];
6241 /* Force per-sample interpolation for the colors here. */
6242 if (shader
->key
.ps
.prolog
.force_persample_interp
)
6243 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6245 switch (info
->input_interpolate
[color
[i
]]) {
6246 case TGSI_INTERPOLATE_CONSTANT
:
6247 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6249 case TGSI_INTERPOLATE_PERSPECTIVE
:
6250 case TGSI_INTERPOLATE_COLOR
:
6252 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6253 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6254 shader
->config
.spi_ps_input_ena
|=
6255 S_0286CC_PERSP_SAMPLE_ENA(1);
6257 case TGSI_INTERPOLATE_LOC_CENTER
:
6258 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6259 shader
->config
.spi_ps_input_ena
|=
6260 S_0286CC_PERSP_CENTER_ENA(1);
6262 case TGSI_INTERPOLATE_LOC_CENTROID
:
6263 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6264 shader
->config
.spi_ps_input_ena
|=
6265 S_0286CC_PERSP_CENTROID_ENA(1);
6271 case TGSI_INTERPOLATE_LINEAR
:
6273 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6274 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 6;
6275 shader
->config
.spi_ps_input_ena
|=
6276 S_0286CC_LINEAR_SAMPLE_ENA(1);
6278 case TGSI_INTERPOLATE_LOC_CENTER
:
6279 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 8;
6280 shader
->config
.spi_ps_input_ena
|=
6281 S_0286CC_LINEAR_CENTER_ENA(1);
6283 case TGSI_INTERPOLATE_LOC_CENTROID
:
6284 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 10;
6285 shader
->config
.spi_ps_input_ena
|=
6286 S_0286CC_LINEAR_CENTROID_ENA(1);
6298 /* The prolog is a no-op if these aren't set. */
6299 if (prolog_key
.ps_prolog
.colors_read
||
6300 prolog_key
.ps_prolog
.states
.force_persample_interp
||
6301 prolog_key
.ps_prolog
.states
.poly_stipple
) {
6303 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
6304 &prolog_key
, tm
, debug
,
6305 si_compile_ps_prolog
);
6306 if (!shader
->prolog
)
6310 /* Get the epilog. */
6311 memset(&epilog_key
, 0, sizeof(epilog_key
));
6312 epilog_key
.ps_epilog
.colors_written
= info
->colors_written
;
6313 epilog_key
.ps_epilog
.writes_z
= info
->writes_z
;
6314 epilog_key
.ps_epilog
.writes_stencil
= info
->writes_stencil
;
6315 epilog_key
.ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6316 epilog_key
.ps_epilog
.states
= shader
->key
.ps
.epilog
;
6319 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
6320 &epilog_key
, tm
, debug
,
6321 si_compile_ps_epilog
);
6322 if (!shader
->epilog
)
6325 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
6326 if (shader
->key
.ps
.prolog
.poly_stipple
) {
6327 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
6328 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
6331 /* Set up the enable bits for per-sample shading if needed. */
6332 if (shader
->key
.ps
.prolog
.force_persample_interp
) {
6333 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
6334 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
)) {
6335 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
6336 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
6337 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
6339 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
6340 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
)) {
6341 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
6342 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
6343 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
6347 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
6348 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
6349 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
6350 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
6351 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
6354 /* At least one pair of interpolation weights must be enabled. */
6355 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
6356 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
6357 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
6360 /* The sample mask input is always enabled, because the API shader always
6361 * passes it through to the epilog. Disable it here if it's unused.
6363 if (!shader
->key
.ps
.epilog
.poly_line_smoothing
&&
6364 !shader
->selector
->info
.reads_samplemask
)
6365 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
6370 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
6371 struct si_shader
*shader
,
6372 struct pipe_debug_callback
*debug
)
6374 struct si_shader
*mainp
= shader
->selector
->main_shader_part
;
6377 /* LS, ES, VS are compiled on demand if the main part hasn't been
6378 * compiled for that stage.
6381 (shader
->selector
->type
== PIPE_SHADER_VERTEX
&&
6382 (shader
->key
.vs
.as_es
!= mainp
->key
.vs
.as_es
||
6383 shader
->key
.vs
.as_ls
!= mainp
->key
.vs
.as_ls
)) ||
6384 (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
&&
6385 shader
->key
.tes
.as_es
!= mainp
->key
.tes
.as_es
)) {
6386 /* Monolithic shader (compiled as a whole, has many variants,
6387 * may take a long time to compile).
6389 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
6393 /* The shader consists of 2-3 parts:
6395 * - the middle part is the user shader, it has 1 variant only
6396 * and it was compiled during the creation of the shader
6398 * - the prolog part is inserted at the beginning
6399 * - the epilog part is inserted at the end
6401 * The prolog and epilog have many (but simple) variants.
6404 /* Copy the compiled TGSI shader data over. */
6405 shader
->is_binary_shared
= true;
6406 shader
->binary
= mainp
->binary
;
6407 shader
->config
= mainp
->config
;
6408 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
6409 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
6410 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
6411 memcpy(shader
->info
.vs_output_param_offset
,
6412 mainp
->info
.vs_output_param_offset
,
6413 sizeof(mainp
->info
.vs_output_param_offset
));
6414 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
6415 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
6416 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
6418 /* Select prologs and/or epilogs. */
6419 switch (shader
->selector
->type
) {
6420 case PIPE_SHADER_VERTEX
:
6421 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
6424 case PIPE_SHADER_TESS_CTRL
:
6425 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
6428 case PIPE_SHADER_TESS_EVAL
:
6429 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
6432 case PIPE_SHADER_FRAGMENT
:
6433 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
6436 /* Make sure we have at least as many VGPRs as there
6437 * are allocated inputs.
6439 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6440 shader
->info
.num_input_vgprs
);
6444 /* Update SGPR and VGPR counts. */
6445 if (shader
->prolog
) {
6446 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
6447 shader
->prolog
->config
.num_sgprs
);
6448 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6449 shader
->prolog
->config
.num_vgprs
);
6451 if (shader
->epilog
) {
6452 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
6453 shader
->epilog
->config
.num_sgprs
);
6454 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6455 shader
->epilog
->config
.num_vgprs
);
6459 si_shader_dump(sscreen
, shader
, debug
, shader
->selector
->info
.processor
,
6463 r
= si_shader_binary_upload(sscreen
, shader
);
6465 fprintf(stderr
, "LLVM failed to upload shader\n");
6472 void si_shader_destroy(struct si_shader
*shader
)
6474 if (shader
->gs_copy_shader
) {
6475 si_shader_destroy(shader
->gs_copy_shader
);
6476 FREE(shader
->gs_copy_shader
);
6479 if (shader
->scratch_bo
)
6480 r600_resource_reference(&shader
->scratch_bo
, NULL
);
6482 r600_resource_reference(&shader
->bo
, NULL
);
6484 if (!shader
->is_binary_shared
)
6485 radeon_shader_binary_clean(&shader
->binary
);