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_util.h"
44 #include "tgsi/tgsi_dump.h"
47 #include "si_shader.h"
52 static const char *scratch_rsrc_dword0_symbol
=
53 "SCRATCH_RSRC_DWORD0";
55 static const char *scratch_rsrc_dword1_symbol
=
56 "SCRATCH_RSRC_DWORD1";
58 struct si_shader_output_values
60 LLVMValueRef values
[4];
65 struct si_shader_context
67 struct radeon_llvm_context radeon_bld
;
68 struct si_shader
*shader
;
69 struct si_screen
*screen
;
71 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
72 bool is_gs_copy_shader
;
74 /* Whether to generate the optimized shader variant compiled as a whole
75 * (without a prolog and epilog)
79 int param_streamout_config
;
80 int param_streamout_write_index
;
81 int param_streamout_offset
[4];
83 int param_rel_auto_id
;
85 int param_instance_id
;
86 int param_vertex_index0
;
89 int param_tes_rel_patch_id
;
90 int param_tes_patch_id
;
91 int param_es2gs_offset
;
93 LLVMTargetMachineRef tm
;
95 LLVMValueRef const_md
;
96 LLVMValueRef const_buffers
[SI_NUM_CONST_BUFFERS
];
98 LLVMValueRef
*constants
[SI_NUM_CONST_BUFFERS
];
99 LLVMValueRef sampler_views
[SI_NUM_SAMPLERS
];
100 LLVMValueRef sampler_states
[SI_NUM_SAMPLERS
];
101 LLVMValueRef fmasks
[SI_NUM_USER_SAMPLERS
];
102 LLVMValueRef images
[SI_NUM_IMAGES
];
103 LLVMValueRef so_buffers
[4];
104 LLVMValueRef esgs_ring
;
105 LLVMValueRef gsvs_ring
[4];
106 LLVMValueRef gs_next_vertex
[4];
107 LLVMValueRef return_value
;
123 static struct si_shader_context
*si_shader_context(
124 struct lp_build_tgsi_context
*bld_base
)
126 return (struct si_shader_context
*)bld_base
;
129 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
130 struct si_screen
*sscreen
,
131 struct si_shader
*shader
,
132 LLVMTargetMachineRef tm
);
134 /* Ideally pass the sample mask input to the PS epilog as v13, which
135 * is its usual location, so that the shader doesn't have to add v_mov.
137 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
139 /* The VS location of the PrimitiveID input is the same in the epilog,
140 * so that the main shader part doesn't have to move it.
142 #define VS_EPILOG_PRIMID_LOC 2
144 #define PERSPECTIVE_BASE 0
145 #define LINEAR_BASE 9
147 #define SAMPLE_OFFSET 0
148 #define CENTER_OFFSET 2
149 #define CENTROID_OFSET 4
151 #define USE_SGPR_MAX_SUFFIX_LEN 5
152 #define CONST_ADDR_SPACE 2
153 #define LOCAL_ADDR_SPACE 3
154 #define USER_SGPR_ADDR_SPACE 8
158 #define SENDMSG_GS_DONE 3
160 #define SENDMSG_GS_OP_NOP (0 << 4)
161 #define SENDMSG_GS_OP_CUT (1 << 4)
162 #define SENDMSG_GS_OP_EMIT (2 << 4)
163 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
166 * Returns a unique index for a semantic name and index. The index must be
167 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
170 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
172 switch (semantic_name
) {
173 case TGSI_SEMANTIC_POSITION
:
175 case TGSI_SEMANTIC_PSIZE
:
177 case TGSI_SEMANTIC_CLIPDIST
:
180 case TGSI_SEMANTIC_GENERIC
:
184 /* same explanation as in the default statement,
185 * the only user hitting this is st/nine.
189 /* patch indices are completely separate and thus start from 0 */
190 case TGSI_SEMANTIC_TESSOUTER
:
192 case TGSI_SEMANTIC_TESSINNER
:
194 case TGSI_SEMANTIC_PATCH
:
198 /* Don't fail here. The result of this function is only used
199 * for LS, TCS, TES, and GS, where legacy GL semantics can't
200 * occur, but this function is called for all vertex shaders
201 * before it's known whether LS will be compiled or not.
208 * Get the value of a shader input parameter and extract a bitfield.
210 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
211 unsigned param
, unsigned rshift
,
214 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
215 LLVMValueRef value
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
218 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
219 value
= bitcast(&ctx
->radeon_bld
.soa
.bld_base
,
220 TGSI_TYPE_UNSIGNED
, value
);
223 value
= LLVMBuildLShr(gallivm
->builder
, value
,
224 lp_build_const_int32(gallivm
, rshift
), "");
226 if (rshift
+ bitwidth
< 32) {
227 unsigned mask
= (1 << bitwidth
) - 1;
228 value
= LLVMBuildAnd(gallivm
->builder
, value
,
229 lp_build_const_int32(gallivm
, mask
), "");
235 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
238 case TGSI_PROCESSOR_TESS_CTRL
:
239 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
241 case TGSI_PROCESSOR_TESS_EVAL
:
242 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
243 ctx
->param_tes_rel_patch_id
);
251 /* Tessellation shaders pass outputs to the next shader using LDS.
253 * LS outputs = TCS inputs
254 * TCS outputs = TES inputs
257 * - TCS inputs for patch 0
258 * - TCS inputs for patch 1
259 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
261 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
262 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
263 * - TCS outputs for patch 1
264 * - Per-patch TCS outputs for patch 1
265 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
266 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
269 * All three shaders VS(LS), TCS, TES share the same LDS space.
273 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
275 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
)
276 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
277 else if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
278 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
286 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
288 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
292 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
294 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
296 SI_PARAM_TCS_OUT_OFFSETS
,
302 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
304 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
306 SI_PARAM_TCS_OUT_OFFSETS
,
312 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
314 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
315 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
316 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
318 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
322 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
324 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
325 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
326 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
327 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
329 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
330 LLVMBuildMul(gallivm
->builder
, patch_stride
,
336 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
338 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
339 LLVMValueRef patch0_patch_data_offset
=
340 get_tcs_out_patch0_patch_data_offset(ctx
);
341 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
342 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
344 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
345 LLVMBuildMul(gallivm
->builder
, patch_stride
,
350 static void build_indexed_store(struct si_shader_context
*ctx
,
351 LLVMValueRef base_ptr
, LLVMValueRef index
,
354 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
355 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
356 LLVMValueRef indices
[2], pointer
;
358 indices
[0] = bld_base
->uint_bld
.zero
;
361 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
362 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
366 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
367 * It's equivalent to doing a load from &base_ptr[index].
369 * \param base_ptr Where the array starts.
370 * \param index The element index into the array.
372 static LLVMValueRef
build_indexed_load(struct si_shader_context
*ctx
,
373 LLVMValueRef base_ptr
, LLVMValueRef index
)
375 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
376 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
377 LLVMValueRef indices
[2], pointer
;
379 indices
[0] = bld_base
->uint_bld
.zero
;
382 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
383 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
387 * Do a load from &base_ptr[index], but also add a flag that it's loading
390 static LLVMValueRef
build_indexed_load_const(
391 struct si_shader_context
*ctx
,
392 LLVMValueRef base_ptr
, LLVMValueRef index
)
394 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
);
395 LLVMSetMetadata(result
, 1, ctx
->const_md
);
399 static LLVMValueRef
get_instance_index_for_fetch(
400 struct radeon_llvm_context
*radeon_bld
,
401 unsigned param_start_instance
, unsigned divisor
)
403 struct si_shader_context
*ctx
=
404 si_shader_context(&radeon_bld
->soa
.bld_base
);
405 struct gallivm_state
*gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
407 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
408 ctx
->param_instance_id
);
410 /* The division must be done before START_INSTANCE is added. */
412 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
413 lp_build_const_int32(gallivm
, divisor
), "");
415 return LLVMBuildAdd(gallivm
->builder
, result
,
416 LLVMGetParam(radeon_bld
->main_fn
, param_start_instance
), "");
419 static void declare_input_vs(
420 struct radeon_llvm_context
*radeon_bld
,
421 unsigned input_index
,
422 const struct tgsi_full_declaration
*decl
)
424 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
425 struct gallivm_state
*gallivm
= base
->gallivm
;
426 struct si_shader_context
*ctx
=
427 si_shader_context(&radeon_bld
->soa
.bld_base
);
429 ctx
->shader
->key
.vs
.prolog
.instance_divisors
[input_index
];
433 LLVMValueRef t_list_ptr
;
434 LLVMValueRef t_offset
;
436 LLVMValueRef attribute_offset
;
437 LLVMValueRef buffer_index
;
438 LLVMValueRef args
[3];
441 /* Load the T list */
442 t_list_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFERS
);
444 t_offset
= lp_build_const_int32(gallivm
, input_index
);
446 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
448 /* Build the attribute offset */
449 attribute_offset
= lp_build_const_int32(gallivm
, 0);
451 if (!ctx
->is_monolithic
) {
452 buffer_index
= LLVMGetParam(radeon_bld
->main_fn
,
453 ctx
->param_vertex_index0
+
455 } else if (divisor
) {
456 /* Build index from instance ID, start instance and divisor */
457 ctx
->shader
->info
.uses_instanceid
= true;
458 buffer_index
= get_instance_index_for_fetch(&ctx
->radeon_bld
,
459 SI_PARAM_START_INSTANCE
,
462 /* Load the buffer index for vertices. */
463 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
464 ctx
->param_vertex_id
);
465 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
466 SI_PARAM_BASE_VERTEX
);
467 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
471 args
[1] = attribute_offset
;
472 args
[2] = buffer_index
;
473 input
= lp_build_intrinsic(gallivm
->builder
,
474 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
475 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
477 /* Break up the vec4 into individual components */
478 for (chan
= 0; chan
< 4; chan
++) {
479 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
480 /* XXX: Use a helper function for this. There is one in
482 ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
483 LLVMBuildExtractElement(gallivm
->builder
,
484 input
, llvm_chan
, "");
488 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
491 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
494 return bld_base
->uint_bld
.zero
;
497 case TGSI_PROCESSOR_VERTEX
:
498 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
499 ctx
->param_vs_prim_id
);
500 case TGSI_PROCESSOR_TESS_CTRL
:
501 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
503 case TGSI_PROCESSOR_TESS_EVAL
:
504 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
505 ctx
->param_tes_patch_id
);
506 case TGSI_PROCESSOR_GEOMETRY
:
507 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
508 SI_PARAM_PRIMITIVE_ID
);
511 return bld_base
->uint_bld
.zero
;
516 * Return the value of tgsi_ind_register for indexing.
517 * This is the indirect index with the constant offset added to it.
519 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
520 const struct tgsi_ind_register
*ind
,
523 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
526 result
= ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
527 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
528 result
= LLVMBuildAdd(gallivm
->builder
, result
,
529 lp_build_const_int32(gallivm
, rel_index
), "");
534 * Calculate a dword address given an input or output register and a stride.
536 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
537 const struct tgsi_full_dst_register
*dst
,
538 const struct tgsi_full_src_register
*src
,
539 LLVMValueRef vertex_dw_stride
,
540 LLVMValueRef base_addr
)
542 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
543 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
544 ubyte
*name
, *index
, *array_first
;
546 struct tgsi_full_dst_register reg
;
548 /* Set the register description. The address computation is the same
549 * for sources and destinations. */
551 reg
.Register
.File
= src
->Register
.File
;
552 reg
.Register
.Index
= src
->Register
.Index
;
553 reg
.Register
.Indirect
= src
->Register
.Indirect
;
554 reg
.Register
.Dimension
= src
->Register
.Dimension
;
555 reg
.Indirect
= src
->Indirect
;
556 reg
.Dimension
= src
->Dimension
;
557 reg
.DimIndirect
= src
->DimIndirect
;
561 /* If the register is 2-dimensional (e.g. an array of vertices
562 * in a primitive), calculate the base address of the vertex. */
563 if (reg
.Register
.Dimension
) {
566 if (reg
.Dimension
.Indirect
)
567 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
568 reg
.Dimension
.Index
);
570 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
572 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
573 LLVMBuildMul(gallivm
->builder
, index
,
574 vertex_dw_stride
, ""), "");
577 /* Get information about the register. */
578 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
579 name
= info
->input_semantic_name
;
580 index
= info
->input_semantic_index
;
581 array_first
= info
->input_array_first
;
582 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
583 name
= info
->output_semantic_name
;
584 index
= info
->output_semantic_index
;
585 array_first
= info
->output_array_first
;
591 if (reg
.Register
.Indirect
) {
592 /* Add the relative address of the element. */
593 LLVMValueRef ind_index
;
595 if (reg
.Indirect
.ArrayID
)
596 first
= array_first
[reg
.Indirect
.ArrayID
];
598 first
= reg
.Register
.Index
;
600 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
601 reg
.Register
.Index
- first
);
603 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
604 LLVMBuildMul(gallivm
->builder
, ind_index
,
605 lp_build_const_int32(gallivm
, 4), ""), "");
607 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
609 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
610 index
[reg
.Register
.Index
]);
613 /* Add the base address of the element. */
614 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
615 lp_build_const_int32(gallivm
, param
* 4), "");
621 * \param type output value type
622 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
623 * \param dw_addr address in dwords
625 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
626 enum tgsi_opcode_type type
, unsigned swizzle
,
627 LLVMValueRef dw_addr
)
629 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
630 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
634 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
636 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
637 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
639 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
643 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
644 lp_build_const_int32(gallivm
, swizzle
));
646 value
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
647 if (type
== TGSI_TYPE_DOUBLE
) {
649 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
650 lp_build_const_int32(gallivm
, swizzle
+ 1));
651 value2
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
652 return radeon_llvm_emit_fetch_double(bld_base
, value
, value2
);
655 return LLVMBuildBitCast(gallivm
->builder
, value
,
656 tgsi2llvmtype(bld_base
, type
), "");
662 * \param swizzle offset (typically 0..3)
663 * \param dw_addr address in dwords
664 * \param value value to store
666 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
667 unsigned swizzle
, LLVMValueRef dw_addr
,
670 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
671 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
673 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
674 lp_build_const_int32(gallivm
, swizzle
));
676 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
677 build_indexed_store(ctx
, ctx
->lds
,
681 static LLVMValueRef
fetch_input_tcs(
682 struct lp_build_tgsi_context
*bld_base
,
683 const struct tgsi_full_src_register
*reg
,
684 enum tgsi_opcode_type type
, unsigned swizzle
)
686 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
687 LLVMValueRef dw_addr
, stride
;
689 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
690 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
691 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
693 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
696 static LLVMValueRef
fetch_output_tcs(
697 struct lp_build_tgsi_context
*bld_base
,
698 const struct tgsi_full_src_register
*reg
,
699 enum tgsi_opcode_type type
, unsigned swizzle
)
701 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
702 LLVMValueRef dw_addr
, stride
;
704 if (reg
->Register
.Dimension
) {
705 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
706 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
707 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
709 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
710 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
713 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
716 static LLVMValueRef
fetch_input_tes(
717 struct lp_build_tgsi_context
*bld_base
,
718 const struct tgsi_full_src_register
*reg
,
719 enum tgsi_opcode_type type
, unsigned swizzle
)
721 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
722 LLVMValueRef dw_addr
, stride
;
724 if (reg
->Register
.Dimension
) {
725 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
726 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
727 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
729 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
730 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
733 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
736 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
737 const struct tgsi_full_instruction
*inst
,
738 const struct tgsi_opcode_info
*info
,
741 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
742 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
744 LLVMValueRef dw_addr
, stride
;
746 /* Only handle per-patch and per-vertex outputs here.
747 * Vectors will be lowered to scalars and this function will be called again.
749 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
750 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
751 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
755 if (reg
->Register
.Dimension
) {
756 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
757 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
758 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
760 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
761 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
764 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
765 LLVMValueRef value
= dst
[chan_index
];
767 if (inst
->Instruction
.Saturate
)
768 value
= radeon_llvm_saturate(bld_base
, value
);
770 lds_store(bld_base
, chan_index
, dw_addr
, value
);
774 static LLVMValueRef
fetch_input_gs(
775 struct lp_build_tgsi_context
*bld_base
,
776 const struct tgsi_full_src_register
*reg
,
777 enum tgsi_opcode_type type
,
780 struct lp_build_context
*base
= &bld_base
->base
;
781 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
782 struct si_shader
*shader
= ctx
->shader
;
783 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
784 struct gallivm_state
*gallivm
= base
->gallivm
;
785 LLVMValueRef vtx_offset
;
786 LLVMValueRef args
[9];
787 unsigned vtx_offset_param
;
788 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
789 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
790 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
794 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
795 return get_primitive_id(bld_base
, swizzle
);
797 if (!reg
->Register
.Dimension
)
801 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
803 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
804 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
806 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
810 /* Get the vertex offset parameter */
811 vtx_offset_param
= reg
->Dimension
.Index
;
812 if (vtx_offset_param
< 2) {
813 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
815 assert(vtx_offset_param
< 6);
816 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
818 vtx_offset
= lp_build_mul_imm(uint
,
819 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
823 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
824 args
[0] = ctx
->esgs_ring
;
825 args
[1] = vtx_offset
;
826 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
827 args
[3] = uint
->zero
;
828 args
[4] = uint
->one
; /* OFFEN */
829 args
[5] = uint
->zero
; /* IDXEN */
830 args
[6] = uint
->one
; /* GLC */
831 args
[7] = uint
->zero
; /* SLC */
832 args
[8] = uint
->zero
; /* TFE */
834 value
= lp_build_intrinsic(gallivm
->builder
,
835 "llvm.SI.buffer.load.dword.i32.i32",
837 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
838 if (type
== TGSI_TYPE_DOUBLE
) {
840 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
841 value2
= lp_build_intrinsic(gallivm
->builder
,
842 "llvm.SI.buffer.load.dword.i32.i32",
844 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
845 return radeon_llvm_emit_fetch_double(bld_base
,
848 return LLVMBuildBitCast(gallivm
->builder
,
850 tgsi2llvmtype(bld_base
, type
), "");
853 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
855 switch (interpolate
) {
856 case TGSI_INTERPOLATE_CONSTANT
:
859 case TGSI_INTERPOLATE_LINEAR
:
860 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
861 return SI_PARAM_LINEAR_SAMPLE
;
862 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
863 return SI_PARAM_LINEAR_CENTROID
;
865 return SI_PARAM_LINEAR_CENTER
;
867 case TGSI_INTERPOLATE_COLOR
:
868 case TGSI_INTERPOLATE_PERSPECTIVE
:
869 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
870 return SI_PARAM_PERSP_SAMPLE
;
871 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
872 return SI_PARAM_PERSP_CENTROID
;
874 return SI_PARAM_PERSP_CENTER
;
877 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
882 /* This shouldn't be used by explicit INTERP opcodes. */
883 static unsigned select_interp_param(struct si_shader_context
*ctx
,
886 if (!ctx
->shader
->key
.ps
.prolog
.force_persample_interp
||
890 /* If the shader doesn't use center/centroid, just return the parameter.
892 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
893 * switch between center/centroid and sample without shader changes.
896 case SI_PARAM_PERSP_CENTROID
:
897 case SI_PARAM_PERSP_CENTER
:
898 return SI_PARAM_PERSP_SAMPLE
;
900 case SI_PARAM_LINEAR_CENTROID
:
901 case SI_PARAM_LINEAR_CENTER
:
902 return SI_PARAM_LINEAR_SAMPLE
;
910 * Interpolate a fragment shader input.
913 * @param input_index index of the input in hardware
914 * @param semantic_name TGSI_SEMANTIC_*
915 * @param semantic_index semantic index
916 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
917 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
918 * @param interp_param interpolation weights (i,j)
919 * @param prim_mask SI_PARAM_PRIM_MASK
920 * @param face SI_PARAM_FRONT_FACE
921 * @param result the return value (4 components)
923 static void interp_fs_input(struct si_shader_context
*ctx
,
924 unsigned input_index
,
925 unsigned semantic_name
,
926 unsigned semantic_index
,
927 unsigned num_interp_inputs
,
928 unsigned colors_read_mask
,
929 LLVMValueRef interp_param
,
930 LLVMValueRef prim_mask
,
932 LLVMValueRef result
[4])
934 struct lp_build_context
*base
= &ctx
->radeon_bld
.soa
.bld_base
.base
;
935 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
936 struct gallivm_state
*gallivm
= base
->gallivm
;
937 const char *intr_name
;
938 LLVMValueRef attr_number
;
942 attr_number
= lp_build_const_int32(gallivm
, input_index
);
944 /* fs.constant returns the param from the middle vertex, so it's not
945 * really useful for flat shading. It's meant to be used for custom
946 * interpolation (but the intrinsic can't fetch from the other two
949 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
950 * to do the right thing. The only reason we use fs.constant is that
951 * fs.interp cannot be used on integers, because they can be equal
954 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
956 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
957 ctx
->shader
->key
.ps
.prolog
.color_two_side
) {
958 LLVMValueRef args
[4];
959 LLVMValueRef is_face_positive
;
960 LLVMValueRef back_attr_number
;
962 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
963 * otherwise it's at offset "num_inputs".
965 unsigned back_attr_offset
= num_interp_inputs
;
966 if (semantic_index
== 1 && colors_read_mask
& 0xf)
967 back_attr_offset
+= 1;
969 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
971 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
972 face
, uint
->zero
, "");
975 args
[3] = interp_param
;
976 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
977 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
978 LLVMValueRef front
, back
;
981 args
[1] = attr_number
;
982 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
983 ctx
->f32
, args
, args
[3] ? 4 : 3,
984 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
986 args
[1] = back_attr_number
;
987 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
988 ctx
->f32
, args
, args
[3] ? 4 : 3,
989 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
991 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
997 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
998 LLVMValueRef args
[4];
1000 args
[0] = uint
->zero
;
1001 args
[1] = attr_number
;
1002 args
[2] = prim_mask
;
1003 args
[3] = interp_param
;
1004 result
[0] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1005 ctx
->f32
, args
, args
[3] ? 4 : 3,
1006 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1008 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1009 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1011 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1012 LLVMValueRef args
[4];
1013 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1015 args
[0] = llvm_chan
;
1016 args
[1] = attr_number
;
1017 args
[2] = prim_mask
;
1018 args
[3] = interp_param
;
1019 result
[chan
] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1020 ctx
->f32
, args
, args
[3] ? 4 : 3,
1021 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1026 static void declare_input_fs(
1027 struct radeon_llvm_context
*radeon_bld
,
1028 unsigned input_index
,
1029 const struct tgsi_full_declaration
*decl
)
1031 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
1032 struct si_shader_context
*ctx
=
1033 si_shader_context(&radeon_bld
->soa
.bld_base
);
1034 struct si_shader
*shader
= ctx
->shader
;
1035 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
1036 LLVMValueRef interp_param
= NULL
;
1037 int interp_param_idx
;
1039 /* Get colors from input VGPRs (set by the prolog). */
1040 if (!ctx
->is_monolithic
&&
1041 decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1042 unsigned i
= decl
->Semantic
.Index
;
1043 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1044 unsigned mask
= colors_read
>> (i
* 4);
1045 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1046 (i
? util_bitcount(colors_read
& 0xf) : 0);
1048 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
1049 mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1050 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
1051 mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1052 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
1053 mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1054 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
1055 mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1059 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1060 decl
->Interp
.Location
);
1061 if (interp_param_idx
== -1)
1063 else if (interp_param_idx
) {
1064 interp_param_idx
= select_interp_param(ctx
,
1066 interp_param
= LLVMGetParam(main_fn
, interp_param_idx
);
1069 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1070 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1071 shader
->selector
->info
.colors_read
, interp_param
,
1072 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1073 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1074 &radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)]);
1077 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1079 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1080 SI_PARAM_ANCILLARY
, 8, 4);
1084 * Load a dword from a constant buffer.
1086 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1087 LLVMValueRef offset
, LLVMTypeRef return_type
)
1089 LLVMValueRef args
[2] = {resource
, offset
};
1091 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1092 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1095 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1097 struct si_shader_context
*ctx
=
1098 si_shader_context(&radeon_bld
->soa
.bld_base
);
1099 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1100 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1101 LLVMBuilderRef builder
= gallivm
->builder
;
1102 LLVMValueRef desc
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1103 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1104 LLVMValueRef resource
= build_indexed_load_const(ctx
, desc
, buf_index
);
1106 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1107 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1108 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1110 LLVMValueRef pos
[4] = {
1111 buffer_load_const(builder
, resource
, offset0
, ctx
->f32
),
1112 buffer_load_const(builder
, resource
, offset1
, ctx
->f32
),
1113 lp_build_const_float(gallivm
, 0),
1114 lp_build_const_float(gallivm
, 0)
1117 return lp_build_gather_values(gallivm
, pos
, 4);
1120 static void declare_system_value(
1121 struct radeon_llvm_context
*radeon_bld
,
1123 const struct tgsi_full_declaration
*decl
)
1125 struct si_shader_context
*ctx
=
1126 si_shader_context(&radeon_bld
->soa
.bld_base
);
1127 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1128 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1129 LLVMValueRef value
= 0;
1131 switch (decl
->Semantic
.Name
) {
1132 case TGSI_SEMANTIC_INSTANCEID
:
1133 value
= LLVMGetParam(radeon_bld
->main_fn
,
1134 ctx
->param_instance_id
);
1137 case TGSI_SEMANTIC_VERTEXID
:
1138 value
= LLVMBuildAdd(gallivm
->builder
,
1139 LLVMGetParam(radeon_bld
->main_fn
,
1140 ctx
->param_vertex_id
),
1141 LLVMGetParam(radeon_bld
->main_fn
,
1142 SI_PARAM_BASE_VERTEX
), "");
1145 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1146 value
= LLVMGetParam(radeon_bld
->main_fn
,
1147 ctx
->param_vertex_id
);
1150 case TGSI_SEMANTIC_BASEVERTEX
:
1151 value
= LLVMGetParam(radeon_bld
->main_fn
,
1152 SI_PARAM_BASE_VERTEX
);
1155 case TGSI_SEMANTIC_INVOCATIONID
:
1156 if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1157 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1158 else if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1159 value
= LLVMGetParam(radeon_bld
->main_fn
,
1160 SI_PARAM_GS_INSTANCE_ID
);
1162 assert(!"INVOCATIONID not implemented");
1165 case TGSI_SEMANTIC_POSITION
:
1167 LLVMValueRef pos
[4] = {
1168 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1169 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1170 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1171 lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
, TGSI_OPCODE_RCP
,
1172 LLVMGetParam(radeon_bld
->main_fn
,
1173 SI_PARAM_POS_W_FLOAT
)),
1175 value
= lp_build_gather_values(gallivm
, pos
, 4);
1179 case TGSI_SEMANTIC_FACE
:
1180 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1183 case TGSI_SEMANTIC_SAMPLEID
:
1184 value
= get_sample_id(radeon_bld
);
1187 case TGSI_SEMANTIC_SAMPLEPOS
: {
1188 LLVMValueRef pos
[4] = {
1189 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1190 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1191 lp_build_const_float(gallivm
, 0),
1192 lp_build_const_float(gallivm
, 0)
1194 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1195 TGSI_OPCODE_FRC
, pos
[0]);
1196 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1197 TGSI_OPCODE_FRC
, pos
[1]);
1198 value
= lp_build_gather_values(gallivm
, pos
, 4);
1202 case TGSI_SEMANTIC_SAMPLEMASK
:
1203 /* This can only occur with the OpenGL Core profile, which
1204 * doesn't support smoothing.
1206 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1209 case TGSI_SEMANTIC_TESSCOORD
:
1211 LLVMValueRef coord
[4] = {
1212 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_u
),
1213 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_v
),
1218 /* For triangles, the vector should be (u, v, 1-u-v). */
1219 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1220 PIPE_PRIM_TRIANGLES
)
1221 coord
[2] = lp_build_sub(bld
, bld
->one
,
1222 lp_build_add(bld
, coord
[0], coord
[1]));
1224 value
= lp_build_gather_values(gallivm
, coord
, 4);
1228 case TGSI_SEMANTIC_VERTICESIN
:
1229 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1232 case TGSI_SEMANTIC_TESSINNER
:
1233 case TGSI_SEMANTIC_TESSOUTER
:
1235 LLVMValueRef dw_addr
;
1236 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1238 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1239 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1240 lp_build_const_int32(gallivm
, param
* 4), "");
1242 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1247 case TGSI_SEMANTIC_PRIMID
:
1248 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1252 assert(!"unknown system value");
1256 radeon_bld
->system_values
[index
] = value
;
1259 static LLVMValueRef
fetch_constant(
1260 struct lp_build_tgsi_context
*bld_base
,
1261 const struct tgsi_full_src_register
*reg
,
1262 enum tgsi_opcode_type type
,
1265 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1266 struct lp_build_context
*base
= &bld_base
->base
;
1267 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1270 LLVMValueRef addr
, bufp
;
1271 LLVMValueRef result
;
1273 if (swizzle
== LP_CHAN_ALL
) {
1275 LLVMValueRef values
[4];
1276 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1277 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1279 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1282 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1283 idx
= reg
->Register
.Index
* 4 + swizzle
;
1285 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1286 if (type
!= TGSI_TYPE_DOUBLE
)
1287 return bitcast(bld_base
, type
, ctx
->constants
[buf
][idx
]);
1289 return radeon_llvm_emit_fetch_double(bld_base
,
1290 ctx
->constants
[buf
][idx
],
1291 ctx
->constants
[buf
][idx
+ 1]);
1295 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1296 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1298 index
= get_indirect_index(ctx
, ®
->DimIndirect
,
1299 reg
->Dimension
.Index
);
1300 bufp
= build_indexed_load_const(ctx
, ptr
, index
);
1302 bufp
= ctx
->const_buffers
[buf
];
1304 addr
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1305 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1306 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1307 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1308 lp_build_const_int32(base
->gallivm
, idx
* 4));
1310 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1313 if (type
!= TGSI_TYPE_DOUBLE
)
1314 result
= bitcast(bld_base
, type
, result
);
1316 LLVMValueRef addr2
, result2
;
1317 addr2
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1318 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1319 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1320 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1321 lp_build_const_int32(base
->gallivm
, idx
* 4));
1323 result2
= buffer_load_const(base
->gallivm
->builder
, ctx
->const_buffers
[buf
],
1326 result
= radeon_llvm_emit_fetch_double(bld_base
,
1332 /* Upper 16 bits must be zero. */
1333 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1334 LLVMValueRef val
[2])
1336 return LLVMBuildOr(gallivm
->builder
, val
[0],
1337 LLVMBuildShl(gallivm
->builder
, val
[1],
1338 lp_build_const_int32(gallivm
, 16),
1342 /* Upper 16 bits are ignored and will be dropped. */
1343 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1344 LLVMValueRef val
[2])
1346 LLVMValueRef v
[2] = {
1347 LLVMBuildAnd(gallivm
->builder
, val
[0],
1348 lp_build_const_int32(gallivm
, 0xffff), ""),
1351 return si_llvm_pack_two_int16(gallivm
, v
);
1354 /* Initialize arguments for the shader export intrinsic */
1355 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1356 LLVMValueRef
*values
,
1360 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1361 struct lp_build_context
*uint
=
1362 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1363 struct lp_build_context
*base
= &bld_base
->base
;
1364 struct gallivm_state
*gallivm
= base
->gallivm
;
1365 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1366 LLVMValueRef val
[4];
1367 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1371 /* Default is 0xf. Adjusted below depending on the format. */
1372 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1374 /* Specify whether the EXEC mask represents the valid mask */
1375 args
[1] = uint
->zero
;
1377 /* Specify whether this is the last export */
1378 args
[2] = uint
->zero
;
1380 /* Specify the target we are exporting */
1381 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1383 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1384 const union si_shader_key
*key
= &ctx
->shader
->key
;
1385 unsigned col_formats
= key
->ps
.epilog
.spi_shader_col_format
;
1386 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1388 assert(cbuf
>= 0 && cbuf
< 8);
1389 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1390 is_int8
= (key
->ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1393 args
[4] = uint
->zero
; /* COMPR flag */
1394 args
[5] = base
->undef
;
1395 args
[6] = base
->undef
;
1396 args
[7] = base
->undef
;
1397 args
[8] = base
->undef
;
1399 switch (spi_shader_col_format
) {
1400 case V_028714_SPI_SHADER_ZERO
:
1401 args
[0] = uint
->zero
; /* writemask */
1402 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1405 case V_028714_SPI_SHADER_32_R
:
1406 args
[0] = uint
->one
; /* writemask */
1407 args
[5] = values
[0];
1410 case V_028714_SPI_SHADER_32_GR
:
1411 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1412 args
[5] = values
[0];
1413 args
[6] = values
[1];
1416 case V_028714_SPI_SHADER_32_AR
:
1417 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1418 args
[5] = values
[0];
1419 args
[8] = values
[3];
1422 case V_028714_SPI_SHADER_FP16_ABGR
:
1423 args
[4] = uint
->one
; /* COMPR flag */
1425 for (chan
= 0; chan
< 2; chan
++) {
1426 LLVMValueRef pack_args
[2] = {
1428 values
[2 * chan
+ 1]
1430 LLVMValueRef packed
;
1432 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1434 ctx
->i32
, pack_args
, 2,
1435 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1437 LLVMBuildBitCast(base
->gallivm
->builder
,
1438 packed
, ctx
->f32
, "");
1442 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1443 for (chan
= 0; chan
< 4; chan
++) {
1444 val
[chan
] = radeon_llvm_saturate(bld_base
, values
[chan
]);
1445 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1446 lp_build_const_float(gallivm
, 65535), "");
1447 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1448 lp_build_const_float(gallivm
, 0.5), "");
1449 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1453 args
[4] = uint
->one
; /* COMPR flag */
1454 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1455 si_llvm_pack_two_int16(gallivm
, val
));
1456 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1457 si_llvm_pack_two_int16(gallivm
, val
+2));
1460 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1461 for (chan
= 0; chan
< 4; chan
++) {
1462 /* Clamp between [-1, 1]. */
1463 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1465 lp_build_const_float(gallivm
, 1));
1466 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1468 lp_build_const_float(gallivm
, -1));
1469 /* Convert to a signed integer in [-32767, 32767]. */
1470 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1471 lp_build_const_float(gallivm
, 32767), "");
1472 /* If positive, add 0.5, else add -0.5. */
1473 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1474 LLVMBuildSelect(builder
,
1475 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1476 val
[chan
], base
->zero
, ""),
1477 lp_build_const_float(gallivm
, 0.5),
1478 lp_build_const_float(gallivm
, -0.5), ""), "");
1479 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1482 args
[4] = uint
->one
; /* COMPR flag */
1483 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1484 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1485 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1486 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1489 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1490 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1493 for (chan
= 0; chan
< 4; chan
++) {
1494 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1495 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1499 args
[4] = uint
->one
; /* COMPR flag */
1500 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1501 si_llvm_pack_two_int16(gallivm
, val
));
1502 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1503 si_llvm_pack_two_int16(gallivm
, val
+2));
1507 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1508 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1510 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
1513 for (chan
= 0; chan
< 4; chan
++) {
1514 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1515 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1518 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1523 args
[4] = uint
->one
; /* COMPR flag */
1524 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1525 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1526 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1527 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1531 case V_028714_SPI_SHADER_32_ABGR
:
1532 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1537 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1540 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1541 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1543 if (ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
1544 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1545 SI_PARAM_ALPHA_REF
);
1547 LLVMValueRef alpha_pass
=
1548 lp_build_cmp(&bld_base
->base
,
1549 ctx
->shader
->key
.ps
.epilog
.alpha_func
,
1552 lp_build_select(&bld_base
->base
,
1554 lp_build_const_float(gallivm
, 1.0f
),
1555 lp_build_const_float(gallivm
, -1.0f
));
1557 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
1558 ctx
->voidt
, &arg
, 1, 0);
1560 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kilp",
1561 ctx
->voidt
, NULL
, 0, 0);
1565 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1567 unsigned samplemask_param
)
1569 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1570 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1571 LLVMValueRef coverage
;
1573 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1574 coverage
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1576 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1578 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1580 &coverage
, 1, LLVMReadNoneAttribute
);
1582 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1585 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1586 lp_build_const_float(gallivm
,
1587 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1589 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1592 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
1593 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1595 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1596 struct lp_build_context
*base
= &bld_base
->base
;
1597 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1600 unsigned const_chan
;
1601 LLVMValueRef base_elt
;
1602 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1603 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1604 LLVMValueRef const_resource
= build_indexed_load_const(ctx
, ptr
, constbuf_index
);
1606 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1607 LLVMValueRef
*args
= pos
[2 + reg_index
];
1612 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1614 /* Compute dot products of position and user clip plane vectors */
1615 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1616 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1617 args
[1] = lp_build_const_int32(base
->gallivm
,
1618 ((reg_index
* 4 + chan
) * 4 +
1620 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1623 lp_build_add(base
, args
[5 + chan
],
1624 lp_build_mul(base
, base_elt
,
1625 out_elts
[const_chan
]));
1629 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1630 args
[1] = uint
->zero
;
1631 args
[2] = uint
->zero
;
1632 args
[3] = lp_build_const_int32(base
->gallivm
,
1633 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1634 args
[4] = uint
->zero
;
1638 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1642 if (so
->num_outputs
)
1643 fprintf(stderr
, "STREAMOUT\n");
1645 for (i
= 0; i
< so
->num_outputs
; i
++) {
1646 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1647 so
->output
[i
].start_component
;
1648 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1649 i
, so
->output
[i
].output_buffer
,
1650 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1651 so
->output
[i
].register_index
,
1652 mask
& 1 ? "x" : "",
1653 mask
& 2 ? "y" : "",
1654 mask
& 4 ? "z" : "",
1655 mask
& 8 ? "w" : "");
1659 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1660 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1661 * or v4i32 (num_channels=3,4). */
1662 static void build_tbuffer_store(struct si_shader_context
*ctx
,
1665 unsigned num_channels
,
1667 LLVMValueRef soffset
,
1668 unsigned inst_offset
,
1677 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1678 LLVMValueRef args
[] = {
1681 LLVMConstInt(ctx
->i32
, num_channels
, 0),
1684 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1685 LLVMConstInt(ctx
->i32
, dfmt
, 0),
1686 LLVMConstInt(ctx
->i32
, nfmt
, 0),
1687 LLVMConstInt(ctx
->i32
, offen
, 0),
1688 LLVMConstInt(ctx
->i32
, idxen
, 0),
1689 LLVMConstInt(ctx
->i32
, glc
, 0),
1690 LLVMConstInt(ctx
->i32
, slc
, 0),
1691 LLVMConstInt(ctx
->i32
, tfe
, 0)
1694 /* The instruction offset field has 12 bits */
1695 assert(offen
|| inst_offset
< (1 << 12));
1697 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1698 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1699 const char *types
[] = {"i32", "v2i32", "v4i32"};
1701 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1703 lp_build_intrinsic(gallivm
->builder
, name
, ctx
->voidt
,
1704 args
, Elements(args
), 0);
1707 static void build_tbuffer_store_dwords(struct si_shader_context
*ctx
,
1710 unsigned num_channels
,
1712 LLVMValueRef soffset
,
1713 unsigned inst_offset
)
1715 static unsigned dfmt
[] = {
1716 V_008F0C_BUF_DATA_FORMAT_32
,
1717 V_008F0C_BUF_DATA_FORMAT_32_32
,
1718 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1719 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1721 assert(num_channels
>= 1 && num_channels
<= 4);
1723 build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1724 inst_offset
, dfmt
[num_channels
-1],
1725 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1728 /* On SI, the vertex shader is responsible for writing streamout data
1730 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
1731 struct si_shader_output_values
*outputs
,
1734 struct pipe_stream_output_info
*so
= &ctx
->shader
->selector
->so
;
1735 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1736 LLVMBuilderRef builder
= gallivm
->builder
;
1738 struct lp_build_if_state if_ctx
;
1740 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1741 LLVMValueRef so_vtx_count
=
1742 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
1744 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", ctx
->i32
,
1745 NULL
, 0, LLVMReadNoneAttribute
);
1747 /* can_emit = tid < so_vtx_count; */
1748 LLVMValueRef can_emit
=
1749 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1751 LLVMValueRef stream_id
=
1752 unpack_param(ctx
, ctx
->param_streamout_config
, 24, 2);
1754 /* Emit the streamout code conditionally. This actually avoids
1755 * out-of-bounds buffer access. The hw tells us via the SGPR
1756 * (so_vtx_count) which threads are allowed to emit streamout data. */
1757 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1759 /* The buffer offset is computed as follows:
1760 * ByteOffset = streamout_offset[buffer_id]*4 +
1761 * (streamout_write_index + thread_id)*stride[buffer_id] +
1765 LLVMValueRef so_write_index
=
1766 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1767 ctx
->param_streamout_write_index
);
1769 /* Compute (streamout_write_index + thread_id). */
1770 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1772 /* Compute the write offset for each enabled buffer. */
1773 LLVMValueRef so_write_offset
[4] = {};
1774 for (i
= 0; i
< 4; i
++) {
1778 LLVMValueRef so_offset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1779 ctx
->param_streamout_offset
[i
]);
1780 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1782 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1783 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
1784 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1787 /* Write streamout data. */
1788 for (i
= 0; i
< so
->num_outputs
; i
++) {
1789 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1790 unsigned reg
= so
->output
[i
].register_index
;
1791 unsigned start
= so
->output
[i
].start_component
;
1792 unsigned num_comps
= so
->output
[i
].num_components
;
1793 unsigned stream
= so
->output
[i
].stream
;
1794 LLVMValueRef out
[4];
1795 struct lp_build_if_state if_ctx_stream
;
1797 assert(num_comps
&& num_comps
<= 4);
1798 if (!num_comps
|| num_comps
> 4)
1804 /* Load the output as int. */
1805 for (j
= 0; j
< num_comps
; j
++) {
1806 out
[j
] = LLVMBuildBitCast(builder
,
1807 outputs
[reg
].values
[start
+j
],
1811 /* Pack the output. */
1812 LLVMValueRef vdata
= NULL
;
1814 switch (num_comps
) {
1815 case 1: /* as i32 */
1818 case 2: /* as v2i32 */
1819 case 3: /* as v4i32 (aligned to 4) */
1820 case 4: /* as v4i32 */
1821 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
1822 for (j
= 0; j
< num_comps
; j
++) {
1823 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1824 LLVMConstInt(ctx
->i32
, j
, 0), "");
1829 LLVMValueRef can_emit_stream
=
1830 LLVMBuildICmp(builder
, LLVMIntEQ
,
1832 lp_build_const_int32(gallivm
, stream
), "");
1834 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1835 build_tbuffer_store_dwords(ctx
, ctx
->so_buffers
[buf_idx
],
1837 so_write_offset
[buf_idx
],
1838 LLVMConstInt(ctx
->i32
, 0, 0),
1839 so
->output
[i
].dst_offset
*4);
1840 lp_build_endif(&if_ctx_stream
);
1843 lp_build_endif(&if_ctx
);
1847 /* Generate export instructions for hardware VS shader stage */
1848 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1849 struct si_shader_output_values
*outputs
,
1852 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1853 struct si_shader
*shader
= ctx
->shader
;
1854 struct lp_build_context
*base
= &bld_base
->base
;
1855 struct lp_build_context
*uint
=
1856 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1857 LLVMValueRef args
[9];
1858 LLVMValueRef pos_args
[4][9] = { { 0 } };
1859 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1860 unsigned semantic_name
, semantic_index
;
1862 unsigned param_count
= 0;
1866 if (outputs
&& ctx
->shader
->selector
->so
.num_outputs
) {
1867 si_llvm_emit_streamout(ctx
, outputs
, noutput
);
1870 for (i
= 0; i
< noutput
; i
++) {
1871 semantic_name
= outputs
[i
].name
;
1872 semantic_index
= outputs
[i
].sid
;
1875 /* Select the correct target */
1876 switch(semantic_name
) {
1877 case TGSI_SEMANTIC_PSIZE
:
1878 psize_value
= outputs
[i
].values
[0];
1880 case TGSI_SEMANTIC_EDGEFLAG
:
1881 edgeflag_value
= outputs
[i
].values
[0];
1883 case TGSI_SEMANTIC_LAYER
:
1884 layer_value
= outputs
[i
].values
[0];
1885 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1886 goto handle_semantic
;
1887 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1888 viewport_index_value
= outputs
[i
].values
[0];
1889 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1890 goto handle_semantic
;
1891 case TGSI_SEMANTIC_POSITION
:
1892 target
= V_008DFC_SQ_EXP_POS
;
1894 case TGSI_SEMANTIC_COLOR
:
1895 case TGSI_SEMANTIC_BCOLOR
:
1896 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1897 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
1898 shader
->info
.vs_output_param_offset
[i
] = param_count
;
1901 case TGSI_SEMANTIC_CLIPDIST
:
1902 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1904 case TGSI_SEMANTIC_CLIPVERTEX
:
1905 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1907 case TGSI_SEMANTIC_PRIMID
:
1908 case TGSI_SEMANTIC_FOG
:
1909 case TGSI_SEMANTIC_TEXCOORD
:
1910 case TGSI_SEMANTIC_GENERIC
:
1911 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1912 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
1913 shader
->info
.vs_output_param_offset
[i
] = param_count
;
1919 "Warning: SI unhandled vs output type:%d\n",
1923 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1925 if (target
>= V_008DFC_SQ_EXP_POS
&&
1926 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1927 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1928 args
, sizeof(args
));
1930 lp_build_intrinsic(base
->gallivm
->builder
,
1931 "llvm.SI.export", ctx
->voidt
,
1935 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1936 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1937 goto handle_semantic
;
1941 shader
->info
.nr_param_exports
= param_count
;
1943 /* We need to add the position output manually if it's missing. */
1944 if (!pos_args
[0][0]) {
1945 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1946 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1947 pos_args
[0][2] = uint
->zero
; /* last export? */
1948 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1949 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1950 pos_args
[0][5] = base
->zero
; /* X */
1951 pos_args
[0][6] = base
->zero
; /* Y */
1952 pos_args
[0][7] = base
->zero
; /* Z */
1953 pos_args
[0][8] = base
->one
; /* W */
1956 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1957 if (shader
->selector
->info
.writes_psize
||
1958 shader
->selector
->info
.writes_edgeflag
||
1959 shader
->selector
->info
.writes_viewport_index
||
1960 shader
->selector
->info
.writes_layer
) {
1961 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1962 shader
->selector
->info
.writes_psize
|
1963 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1964 (shader
->selector
->info
.writes_layer
<< 2) |
1965 (shader
->selector
->info
.writes_viewport_index
<< 3));
1966 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1967 pos_args
[1][2] = uint
->zero
; /* last export? */
1968 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1969 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1970 pos_args
[1][5] = base
->zero
; /* X */
1971 pos_args
[1][6] = base
->zero
; /* Y */
1972 pos_args
[1][7] = base
->zero
; /* Z */
1973 pos_args
[1][8] = base
->zero
; /* W */
1975 if (shader
->selector
->info
.writes_psize
)
1976 pos_args
[1][5] = psize_value
;
1978 if (shader
->selector
->info
.writes_edgeflag
) {
1979 /* The output is a float, but the hw expects an integer
1980 * with the first bit containing the edge flag. */
1981 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1984 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1986 bld_base
->int_bld
.one
);
1988 /* The LLVM intrinsic expects a float. */
1989 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1994 if (shader
->selector
->info
.writes_layer
)
1995 pos_args
[1][7] = layer_value
;
1997 if (shader
->selector
->info
.writes_viewport_index
)
1998 pos_args
[1][8] = viewport_index_value
;
2001 for (i
= 0; i
< 4; i
++)
2003 shader
->info
.nr_pos_exports
++;
2006 for (i
= 0; i
< 4; i
++) {
2007 if (!pos_args
[i
][0])
2010 /* Specify the target we are exporting */
2011 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
2013 if (pos_idx
== shader
->info
.nr_pos_exports
)
2014 /* Specify that this is the last export */
2015 pos_args
[i
][2] = uint
->one
;
2017 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2018 ctx
->voidt
, pos_args
[i
], 9, 0);
2022 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2023 LLVMValueRef rel_patch_id
,
2024 LLVMValueRef invocation_id
,
2025 LLVMValueRef tcs_out_current_patch_data_offset
)
2027 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2028 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2029 struct si_shader
*shader
= ctx
->shader
;
2030 unsigned tess_inner_index
, tess_outer_index
;
2031 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2032 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
;
2033 unsigned stride
, outer_comps
, inner_comps
, i
;
2034 struct lp_build_if_state if_ctx
;
2036 /* Do this only for invocation 0, because the tess levels are per-patch,
2039 * This can't jump, because invocation 0 executes this. It should
2040 * at least mask out the loads and stores for other invocations.
2042 lp_build_if(&if_ctx
, gallivm
,
2043 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2044 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2046 /* Determine the layout of one tess factor element in the buffer. */
2047 switch (shader
->key
.tcs
.epilog
.prim_mode
) {
2048 case PIPE_PRIM_LINES
:
2049 stride
= 2; /* 2 dwords, 1 vec2 store */
2053 case PIPE_PRIM_TRIANGLES
:
2054 stride
= 4; /* 4 dwords, 1 vec4 store */
2058 case PIPE_PRIM_QUADS
:
2059 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2068 /* Load tess_inner and tess_outer from LDS.
2069 * Any invocation can write them, so we can't get them from a temporary.
2071 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2072 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2074 lds_base
= tcs_out_current_patch_data_offset
;
2075 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2076 lp_build_const_int32(gallivm
,
2077 tess_inner_index
* 4), "");
2078 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2079 lp_build_const_int32(gallivm
,
2080 tess_outer_index
* 4), "");
2082 for (i
= 0; i
< outer_comps
; i
++)
2083 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2084 for (i
= 0; i
< inner_comps
; i
++)
2085 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2087 /* Convert the outputs to vectors for stores. */
2088 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2092 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2094 /* Get the buffer. */
2095 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2096 SI_PARAM_RW_BUFFERS
);
2097 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
2098 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
2100 /* Get the offset. */
2101 tf_base
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2102 SI_PARAM_TESS_FACTOR_OFFSET
);
2103 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2104 lp_build_const_int32(gallivm
, 4 * stride
), "");
2106 /* Store the outputs. */
2107 build_tbuffer_store_dwords(ctx
, buffer
, vec0
,
2108 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
2110 build_tbuffer_store_dwords(ctx
, buffer
, vec1
,
2111 stride
- 4, byteoffset
, tf_base
, 16);
2112 lp_build_endif(&if_ctx
);
2115 /* This only writes the tessellation factor levels. */
2116 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2118 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2119 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2121 rel_patch_id
= get_rel_patch_id(ctx
);
2122 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2123 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2125 if (!ctx
->is_monolithic
) {
2126 /* Return epilog parameters from this function. */
2127 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2128 LLVMValueRef ret
= ctx
->return_value
;
2129 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2132 /* RW_BUFFERS pointer */
2133 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2134 SI_PARAM_RW_BUFFERS
);
2135 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2136 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2137 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2138 bld_base
->uint_bld
.zero
, "");
2139 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2140 bld_base
->uint_bld
.one
, "");
2141 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2142 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2144 /* Tess factor buffer soffset is after user SGPRs. */
2145 tf_soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2146 SI_PARAM_TESS_FACTOR_OFFSET
);
2147 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2148 SI_TCS_NUM_USER_SGPR
, "");
2151 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2152 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2153 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2155 vgpr
= SI_TCS_NUM_USER_SGPR
+ 1;
2156 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2157 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2158 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2159 ctx
->return_value
= ret
;
2163 si_write_tess_factors(bld_base
, rel_patch_id
, invocation_id
, tf_lds_offset
);
2166 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2168 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2169 struct si_shader
*shader
= ctx
->shader
;
2170 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2171 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2173 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2174 ctx
->param_rel_auto_id
);
2175 LLVMValueRef vertex_dw_stride
=
2176 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2177 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2178 vertex_dw_stride
, "");
2180 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2181 * its inputs from it. */
2182 for (i
= 0; i
< info
->num_outputs
; i
++) {
2183 LLVMValueRef
*out_ptr
= ctx
->radeon_bld
.soa
.outputs
[i
];
2184 unsigned name
= info
->output_semantic_name
[i
];
2185 unsigned index
= info
->output_semantic_index
[i
];
2186 int param
= si_shader_io_get_unique_index(name
, index
);
2187 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2188 lp_build_const_int32(gallivm
, param
* 4), "");
2190 for (chan
= 0; chan
< 4; chan
++) {
2191 lds_store(bld_base
, chan
, dw_addr
,
2192 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2197 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2199 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2200 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2201 struct si_shader
*es
= ctx
->shader
;
2202 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2203 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2204 ctx
->param_es2gs_offset
);
2208 for (i
= 0; i
< info
->num_outputs
; i
++) {
2209 LLVMValueRef
*out_ptr
=
2210 ctx
->radeon_bld
.soa
.outputs
[i
];
2213 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2214 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2217 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2218 info
->output_semantic_index
[i
]);
2220 for (chan
= 0; chan
< 4; chan
++) {
2221 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2222 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2224 build_tbuffer_store(ctx
,
2227 LLVMGetUndef(ctx
->i32
), soffset
,
2228 (4 * param_index
+ chan
) * 4,
2229 V_008F0C_BUF_DATA_FORMAT_32
,
2230 V_008F0C_BUF_NUM_FORMAT_UINT
,
2236 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2238 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2239 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2240 LLVMValueRef args
[2];
2242 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2243 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2244 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2245 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
2248 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2250 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2251 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2252 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2253 struct si_shader_output_values
*outputs
= NULL
;
2256 assert(!ctx
->is_gs_copy_shader
);
2258 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2260 /* Vertex color clamping.
2262 * This uses a state constant loaded in a user data SGPR and
2263 * an IF statement is added that clamps all colors if the constant
2266 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
2267 struct lp_build_if_state if_ctx
;
2268 LLVMValueRef cond
= NULL
;
2269 LLVMValueRef addr
, val
;
2271 for (i
= 0; i
< info
->num_outputs
; i
++) {
2272 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2273 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2276 /* We've found a color. */
2278 /* The state is in the first bit of the user SGPR. */
2279 cond
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2280 SI_PARAM_VS_STATE_BITS
);
2281 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2283 lp_build_if(&if_ctx
, gallivm
, cond
);
2286 for (j
= 0; j
< 4; j
++) {
2287 addr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2288 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2289 val
= radeon_llvm_saturate(bld_base
, val
);
2290 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2295 lp_build_endif(&if_ctx
);
2298 for (i
= 0; i
< info
->num_outputs
; i
++) {
2299 outputs
[i
].name
= info
->output_semantic_name
[i
];
2300 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2302 for (j
= 0; j
< 4; j
++)
2303 outputs
[i
].values
[j
] =
2304 LLVMBuildLoad(gallivm
->builder
,
2305 ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2309 if (ctx
->is_monolithic
) {
2310 /* Export PrimitiveID when PS needs it. */
2311 if (si_vs_exports_prim_id(ctx
->shader
)) {
2312 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2314 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2315 get_primitive_id(bld_base
, 0));
2316 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2317 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2318 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2322 /* Return the primitive ID from the LLVM function. */
2324 LLVMBuildInsertValue(gallivm
->builder
,
2326 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2327 get_primitive_id(bld_base
, 0)),
2328 VS_EPILOG_PRIMID_LOC
, "");
2331 si_llvm_export_vs(bld_base
, outputs
, i
);
2335 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2336 LLVMValueRef depth
, LLVMValueRef stencil
,
2337 LLVMValueRef samplemask
)
2339 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2340 struct lp_build_context
*base
= &bld_base
->base
;
2341 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2342 LLVMValueRef args
[9];
2345 assert(depth
|| stencil
|| samplemask
);
2347 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2348 args
[2] = uint
->one
; /* DONE bit */
2350 /* Specify the target we are exporting */
2351 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2353 args
[4] = uint
->zero
; /* COMP flag */
2354 args
[5] = base
->undef
; /* R, depth */
2355 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2356 args
[7] = base
->undef
; /* B, sample mask */
2357 args
[8] = base
->undef
; /* A, alpha to mask */
2370 args
[7] = samplemask
;
2374 /* SI (except OLAND) has a bug that it only looks
2375 * at the X writemask component. */
2376 if (ctx
->screen
->b
.chip_class
== SI
&&
2377 ctx
->screen
->b
.family
!= CHIP_OLAND
)
2380 /* Specify which components to enable */
2381 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2383 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2384 ctx
->voidt
, args
, 9, 0);
2387 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2388 LLVMValueRef
*color
, unsigned index
,
2389 unsigned samplemask_param
,
2392 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2393 struct lp_build_context
*base
= &bld_base
->base
;
2397 if (ctx
->shader
->key
.ps
.epilog
.clamp_color
)
2398 for (i
= 0; i
< 4; i
++)
2399 color
[i
] = radeon_llvm_saturate(bld_base
, color
[i
]);
2402 if (ctx
->shader
->key
.ps
.epilog
.alpha_to_one
)
2403 color
[3] = base
->one
;
2407 ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2408 si_alpha_test(bld_base
, color
[3]);
2410 /* Line & polygon smoothing */
2411 if (ctx
->shader
->key
.ps
.epilog
.poly_line_smoothing
)
2412 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
2415 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2416 if (ctx
->shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2417 LLVMValueRef args
[8][9];
2420 /* Get the export arguments, also find out what the last one is. */
2421 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2422 si_llvm_init_export_args(bld_base
, color
,
2423 V_008DFC_SQ_EXP_MRT
+ c
, args
[c
]);
2424 if (args
[c
][0] != bld_base
->uint_bld
.zero
)
2428 /* Emit all exports. */
2429 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2430 if (is_last
&& last
== c
) {
2431 args
[c
][1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2432 args
[c
][2] = bld_base
->uint_bld
.one
; /* DONE bit */
2433 } else if (args
[c
][0] == bld_base
->uint_bld
.zero
)
2434 continue; /* unnecessary NULL export */
2436 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2437 ctx
->voidt
, args
[c
], 9, 0);
2440 LLVMValueRef args
[9];
2443 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
2446 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2447 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
2448 } else if (args
[0] == bld_base
->uint_bld
.zero
)
2449 return; /* unnecessary NULL export */
2451 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2452 ctx
->voidt
, args
, 9, 0);
2456 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
2458 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2459 struct lp_build_context
*base
= &bld_base
->base
;
2460 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2461 LLVMValueRef args
[9];
2463 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
2464 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2465 args
[2] = uint
->one
; /* DONE bit */
2466 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
2467 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
2468 args
[5] = uint
->undef
; /* R */
2469 args
[6] = uint
->undef
; /* G */
2470 args
[7] = uint
->undef
; /* B */
2471 args
[8] = uint
->undef
; /* A */
2473 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2474 ctx
->voidt
, args
, 9, 0);
2477 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2479 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2480 struct si_shader
*shader
= ctx
->shader
;
2481 struct lp_build_context
*base
= &bld_base
->base
;
2482 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2483 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2484 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2485 int last_color_export
= -1;
2488 /* Determine the last export. If MRTZ is present, it's always last.
2489 * Otherwise, find the last color export.
2491 if (!info
->writes_z
&& !info
->writes_stencil
&& !info
->writes_samplemask
) {
2492 unsigned spi_format
= shader
->key
.ps
.epilog
.spi_shader_col_format
;
2494 /* Don't export NULL and return if alpha-test is enabled. */
2495 if (shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
&&
2496 shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
&&
2497 (spi_format
& 0xf) == 0)
2498 spi_format
|= V_028714_SPI_SHADER_32_AR
;
2500 for (i
= 0; i
< info
->num_outputs
; i
++) {
2501 unsigned index
= info
->output_semantic_index
[i
];
2503 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
)
2506 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2507 if (shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2508 /* Just set this if any of the colorbuffers are enabled. */
2510 ((1llu << (4 * (shader
->key
.ps
.epilog
.last_cbuf
+ 1))) - 1))
2511 last_color_export
= i
;
2515 if ((spi_format
>> (index
* 4)) & 0xf)
2516 last_color_export
= i
;
2519 /* If there are no outputs, export NULL. */
2520 if (last_color_export
== -1) {
2521 si_export_null(bld_base
);
2526 for (i
= 0; i
< info
->num_outputs
; i
++) {
2527 unsigned semantic_name
= info
->output_semantic_name
[i
];
2528 unsigned semantic_index
= info
->output_semantic_index
[i
];
2530 LLVMValueRef color
[4] = {};
2532 /* Select the correct target */
2533 switch (semantic_name
) {
2534 case TGSI_SEMANTIC_POSITION
:
2535 depth
= LLVMBuildLoad(builder
,
2536 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2538 case TGSI_SEMANTIC_STENCIL
:
2539 stencil
= LLVMBuildLoad(builder
,
2540 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2542 case TGSI_SEMANTIC_SAMPLEMASK
:
2543 samplemask
= LLVMBuildLoad(builder
,
2544 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2546 case TGSI_SEMANTIC_COLOR
:
2547 for (j
= 0; j
< 4; j
++)
2548 color
[j
] = LLVMBuildLoad(builder
,
2549 ctx
->radeon_bld
.soa
.outputs
[i
][j
], "");
2551 si_export_mrt_color(bld_base
, color
, semantic_index
,
2552 SI_PARAM_SAMPLE_COVERAGE
,
2553 last_color_export
== i
);
2557 "Warning: SI unhandled fs output type:%d\n",
2562 if (depth
|| stencil
|| samplemask
)
2563 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
2567 * Return PS outputs in this order:
2569 * v[0:3] = color0.xyzw
2570 * v[4:7] = color1.xyzw
2575 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
2577 * The alpha-ref SGPR is returned via its original location.
2579 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
2581 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2582 struct si_shader
*shader
= ctx
->shader
;
2583 struct lp_build_context
*base
= &bld_base
->base
;
2584 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2585 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2586 unsigned i
, j
, first_vgpr
, vgpr
;
2588 LLVMValueRef color
[8][4] = {};
2589 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2592 /* Read the output values. */
2593 for (i
= 0; i
< info
->num_outputs
; i
++) {
2594 unsigned semantic_name
= info
->output_semantic_name
[i
];
2595 unsigned semantic_index
= info
->output_semantic_index
[i
];
2597 switch (semantic_name
) {
2598 case TGSI_SEMANTIC_COLOR
:
2599 assert(semantic_index
< 8);
2600 for (j
= 0; j
< 4; j
++) {
2601 LLVMValueRef ptr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2602 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2603 color
[semantic_index
][j
] = result
;
2606 case TGSI_SEMANTIC_POSITION
:
2607 depth
= LLVMBuildLoad(builder
,
2608 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2610 case TGSI_SEMANTIC_STENCIL
:
2611 stencil
= LLVMBuildLoad(builder
,
2612 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2614 case TGSI_SEMANTIC_SAMPLEMASK
:
2615 samplemask
= LLVMBuildLoad(builder
,
2616 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2619 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
2624 /* Fill the return structure. */
2625 ret
= ctx
->return_value
;
2628 ret
= LLVMBuildInsertValue(builder
, ret
,
2629 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
2630 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2631 SI_PARAM_ALPHA_REF
)),
2632 SI_SGPR_ALPHA_REF
, "");
2635 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
2636 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
2640 for (j
= 0; j
< 4; j
++)
2641 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
2644 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
2646 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
2648 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
2650 /* Add the input sample mask for smoothing at the end. */
2651 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
2652 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
2653 ret
= LLVMBuildInsertValue(builder
, ret
,
2654 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2655 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
2657 ctx
->return_value
= ret
;
2661 * Given a v8i32 resource descriptor for a buffer, extract the size of the
2662 * buffer in number of elements and return it as an i32.
2664 static LLVMValueRef
get_buffer_size(
2665 struct lp_build_tgsi_context
*bld_base
,
2666 LLVMValueRef descriptor
)
2668 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2669 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2670 LLVMBuilderRef builder
= gallivm
->builder
;
2672 LLVMBuildExtractElement(builder
, descriptor
,
2673 lp_build_const_int32(gallivm
, 6), "");
2675 if (ctx
->screen
->b
.chip_class
>= VI
) {
2676 /* On VI, the descriptor contains the size in bytes,
2677 * but TXQ must return the size in elements.
2678 * The stride is always non-zero for resources using TXQ.
2680 LLVMValueRef stride
=
2681 LLVMBuildExtractElement(builder
, descriptor
,
2682 lp_build_const_int32(gallivm
, 5), "");
2683 stride
= LLVMBuildLShr(builder
, stride
,
2684 lp_build_const_int32(gallivm
, 16), "");
2685 stride
= LLVMBuildAnd(builder
, stride
,
2686 lp_build_const_int32(gallivm
, 0x3FFF), "");
2688 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2695 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
2698 static void build_int_type_name(
2700 char *buf
, unsigned bufsize
)
2702 assert(bufsize
>= 6);
2704 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
2705 snprintf(buf
, bufsize
, "v%ui32",
2706 LLVMGetVectorSize(type
));
2711 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
2712 struct lp_build_tgsi_context
*bld_base
,
2713 struct lp_build_emit_data
*emit_data
);
2715 static bool tgsi_is_array_sampler(unsigned target
)
2717 return target
== TGSI_TEXTURE_1D_ARRAY
||
2718 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2719 target
== TGSI_TEXTURE_2D_ARRAY
||
2720 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2721 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2722 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2723 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2726 static bool tgsi_is_array_image(unsigned target
)
2728 return target
== TGSI_TEXTURE_3D
||
2729 target
== TGSI_TEXTURE_CUBE
||
2730 target
== TGSI_TEXTURE_1D_ARRAY
||
2731 target
== TGSI_TEXTURE_2D_ARRAY
||
2732 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2733 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2737 * Load the resource descriptor for \p image.
2741 struct lp_build_tgsi_context
*bld_base
,
2742 const struct tgsi_full_src_register
*image
,
2745 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2747 assert(image
->Register
.File
== TGSI_FILE_IMAGE
);
2749 if (!image
->Register
.Indirect
) {
2750 /* Fast path: use preloaded resources */
2751 *rsrc
= ctx
->images
[image
->Register
.Index
];
2753 /* Indexing and manual load */
2754 LLVMValueRef ind_index
;
2755 LLVMValueRef rsrc_ptr
;
2757 ind_index
= get_indirect_index(ctx
, &image
->Indirect
, image
->Register
.Index
);
2759 rsrc_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_IMAGES
);
2760 *rsrc
= build_indexed_load_const(ctx
, rsrc_ptr
, ind_index
);
2764 static LLVMValueRef
image_fetch_coords(
2765 struct lp_build_tgsi_context
*bld_base
,
2766 const struct tgsi_full_instruction
*inst
)
2768 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2769 LLVMBuilderRef builder
= gallivm
->builder
;
2770 unsigned target
= inst
->Memory
.Texture
;
2772 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &sample
);
2773 LLVMValueRef coords
[4];
2777 for (chan
= 0; chan
< num_coords
; ++chan
) {
2778 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
2779 tmp
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
2783 if (num_coords
== 1)
2786 if (num_coords
== 3) {
2787 /* LLVM has difficulties lowering 3-element vectors. */
2788 coords
[3] = bld_base
->uint_bld
.undef
;
2792 return lp_build_gather_values(gallivm
, coords
, num_coords
);
2796 * Append the extra mode bits that are used by image load and store.
2798 static void image_append_args(
2799 struct si_shader_context
*ctx
,
2800 struct lp_build_emit_data
* emit_data
,
2803 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2804 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
2806 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* r128 */
2807 emit_data
->args
[emit_data
->arg_count
++] =
2808 tgsi_is_array_image(target
) ? i1true
: i1false
; /* da */
2809 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* glc */
2810 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
2814 * Append the resource and indexing arguments for buffer intrinsics.
2816 * \param rsrc the 256 bit resource
2817 * \param index index into the buffer
2819 static void buffer_append_args(
2820 struct si_shader_context
*ctx
,
2821 struct lp_build_emit_data
*emit_data
,
2825 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
2826 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
2827 LLVMTypeRef v2i128
= LLVMVectorType(ctx
->i128
, 2);
2828 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
2830 rsrc
= LLVMBuildBitCast(gallivm
->builder
, rsrc
, v2i128
, "");
2831 rsrc
= LLVMBuildExtractElement(gallivm
->builder
, rsrc
, bld_base
->uint_bld
.one
, "");
2832 rsrc
= LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v4i32
, "");
2834 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
2835 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
2836 emit_data
->args
[emit_data
->arg_count
++] = bld_base
->uint_bld
.zero
; /* voffset */
2837 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* glc */
2838 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
2841 static void load_fetch_args(
2842 struct lp_build_tgsi_context
* bld_base
,
2843 struct lp_build_emit_data
* emit_data
)
2845 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2846 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2847 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2848 unsigned target
= inst
->Memory
.Texture
;
2849 LLVMValueRef coords
;
2852 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2854 image_fetch_rsrc(bld_base
, &inst
->Src
[0], &rsrc
);
2855 coords
= image_fetch_coords(bld_base
, inst
);
2857 if (target
== TGSI_TEXTURE_BUFFER
) {
2858 buffer_append_args(ctx
, emit_data
, rsrc
, coords
);
2860 emit_data
->args
[0] = coords
;
2861 emit_data
->args
[1] = rsrc
;
2862 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
2863 emit_data
->arg_count
= 3;
2865 image_append_args(ctx
, emit_data
, target
);
2869 static void load_emit(
2870 const struct lp_build_tgsi_action
*action
,
2871 struct lp_build_tgsi_context
*bld_base
,
2872 struct lp_build_emit_data
*emit_data
)
2874 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2875 LLVMBuilderRef builder
= gallivm
->builder
;
2876 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2877 unsigned target
= inst
->Memory
.Texture
;
2878 char intrinsic_name
[32];
2879 char coords_type
[8];
2881 if (target
== TGSI_TEXTURE_BUFFER
) {
2882 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2883 builder
, "llvm.amdgcn.buffer.load.format.v4f32", emit_data
->dst_type
,
2884 emit_data
->args
, emit_data
->arg_count
,
2885 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
2887 build_int_type_name(LLVMTypeOf(emit_data
->args
[0]),
2888 coords_type
, sizeof(coords_type
));
2890 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
2891 "llvm.amdgcn.image.load.%s", coords_type
);
2893 emit_data
->output
[emit_data
->chan
] =
2895 builder
, intrinsic_name
, emit_data
->dst_type
,
2896 emit_data
->args
, emit_data
->arg_count
,
2897 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
2901 static void resq_fetch_args(
2902 struct lp_build_tgsi_context
* bld_base
,
2903 struct lp_build_emit_data
* emit_data
)
2905 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2906 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2907 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
2908 unsigned tex_target
= inst
->Memory
.Texture
;
2910 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2912 if (tex_target
== TGSI_TEXTURE_BUFFER
) {
2913 image_fetch_rsrc(bld_base
, reg
, &emit_data
->args
[0]);
2914 emit_data
->arg_count
= 1;
2916 emit_data
->args
[0] = bld_base
->uint_bld
.zero
; /* mip level */
2917 image_fetch_rsrc(bld_base
, reg
, &emit_data
->args
[1]);
2918 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
2919 emit_data
->args
[3] = bld_base
->uint_bld
.zero
; /* unorm */
2920 emit_data
->args
[4] = bld_base
->uint_bld
.zero
; /* r128 */
2921 emit_data
->args
[5] = tgsi_is_array_image(tex_target
) ?
2922 bld_base
->uint_bld
.one
: bld_base
->uint_bld
.zero
; /* da */
2923 emit_data
->args
[6] = bld_base
->uint_bld
.zero
; /* glc */
2924 emit_data
->args
[7] = bld_base
->uint_bld
.zero
; /* slc */
2925 emit_data
->args
[8] = bld_base
->uint_bld
.zero
; /* tfe */
2926 emit_data
->args
[9] = bld_base
->uint_bld
.zero
; /* lwe */
2927 emit_data
->arg_count
= 10;
2931 static void resq_emit(
2932 const struct lp_build_tgsi_action
*action
,
2933 struct lp_build_tgsi_context
*bld_base
,
2934 struct lp_build_emit_data
*emit_data
)
2936 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2937 LLVMBuilderRef builder
= gallivm
->builder
;
2938 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2939 unsigned target
= inst
->Memory
.Texture
;
2942 if (target
== TGSI_TEXTURE_BUFFER
) {
2943 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
2945 out
= lp_build_intrinsic(
2946 builder
, "llvm.SI.getresinfo.i32", emit_data
->dst_type
,
2947 emit_data
->args
, emit_data
->arg_count
,
2948 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2950 /* Divide the number of layers by 6 to get the number of cubes. */
2951 if (target
== TGSI_TEXTURE_CUBE_ARRAY
) {
2952 LLVMValueRef imm2
= lp_build_const_int32(gallivm
, 2);
2953 LLVMValueRef imm6
= lp_build_const_int32(gallivm
, 6);
2955 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
2956 z
= LLVMBuildBitCast(builder
, z
, bld_base
->uint_bld
.elem_type
, "");
2957 z
= LLVMBuildSDiv(builder
, z
, imm6
, "");
2958 z
= LLVMBuildBitCast(builder
, z
, bld_base
->base
.elem_type
, "");
2959 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
2963 emit_data
->output
[emit_data
->chan
] = out
;
2966 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
2967 struct lp_build_emit_data
*emit_data
,
2968 unsigned opcode
, unsigned target
,
2969 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
2970 LLVMValueRef
*param
, unsigned count
,
2973 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
2975 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
2977 /* Pad to power of two vector */
2978 while (count
< util_next_power_of_two(count
))
2979 param
[count
++] = LLVMGetUndef(ctx
->i32
);
2981 /* Texture coordinates. */
2983 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
2985 emit_data
->args
[0] = param
[0];
2988 emit_data
->args
[1] = res_ptr
;
2991 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
2992 emit_data
->dst_type
= ctx
->v4i32
;
2994 emit_data
->dst_type
= ctx
->v4f32
;
2996 emit_data
->args
[num_args
++] = samp_ptr
;
2999 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
3000 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
3001 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
3002 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
3003 tgsi_is_array_sampler(target
)); /* da */
3004 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
3005 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
3006 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
3007 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
3009 emit_data
->arg_count
= num_args
;
3012 static const struct lp_build_tgsi_action tex_action
;
3020 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3022 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3027 * Load an image view, fmask view. or sampler state descriptor.
3029 static LLVMValueRef
get_sampler_desc_custom(struct si_shader_context
*ctx
,
3030 LLVMValueRef list
, LLVMValueRef index
,
3031 enum desc_type type
)
3033 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
3034 LLVMBuilderRef builder
= gallivm
->builder
;
3038 /* The image is at [0:7]. */
3039 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
3042 /* The FMASK is at [8:15]. */
3043 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
3044 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
3047 /* The sampler state is at [12:15]. */
3048 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
3049 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
3050 list
= LLVMBuildPointerCast(builder
, list
,
3051 const_array(ctx
->v4i32
, 0), "");
3055 return build_indexed_load_const(ctx
, list
, index
);
3058 static LLVMValueRef
get_sampler_desc(struct si_shader_context
*ctx
,
3059 LLVMValueRef index
, enum desc_type type
)
3061 LLVMValueRef list
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
3064 return get_sampler_desc_custom(ctx
, list
, index
, type
);
3067 static void tex_fetch_ptrs(
3068 struct lp_build_tgsi_context
*bld_base
,
3069 struct lp_build_emit_data
*emit_data
,
3070 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
3072 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3073 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3074 unsigned target
= inst
->Texture
.Texture
;
3075 unsigned sampler_src
;
3076 unsigned sampler_index
;
3078 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
3079 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
3081 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
3082 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
3083 LLVMValueRef ind_index
;
3085 ind_index
= get_indirect_index(ctx
, ®
->Indirect
, reg
->Register
.Index
);
3087 *res_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_IMAGE
);
3089 if (target
== TGSI_TEXTURE_2D_MSAA
||
3090 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
3092 *fmask_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_FMASK
);
3094 *samp_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_SAMPLER
);
3098 *res_ptr
= ctx
->sampler_views
[sampler_index
];
3099 *samp_ptr
= ctx
->sampler_states
[sampler_index
];
3100 *fmask_ptr
= ctx
->fmasks
[sampler_index
];
3104 static void tex_fetch_args(
3105 struct lp_build_tgsi_context
*bld_base
,
3106 struct lp_build_emit_data
*emit_data
)
3108 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3109 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3110 LLVMBuilderRef builder
= gallivm
->builder
;
3111 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3112 unsigned opcode
= inst
->Instruction
.Opcode
;
3113 unsigned target
= inst
->Texture
.Texture
;
3114 LLVMValueRef coords
[5], derivs
[6];
3115 LLVMValueRef address
[16];
3117 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
3120 unsigned num_deriv_channels
= 0;
3121 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
3122 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
3123 unsigned dmask
= 0xf;
3125 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3127 if (opcode
== TGSI_OPCODE_TXQ
) {
3128 if (target
== TGSI_TEXTURE_BUFFER
) {
3129 /* Read the size from the buffer descriptor directly. */
3130 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
3131 emit_data
->args
[0] = get_buffer_size(bld_base
, res
);
3135 /* Textures - set the mip level. */
3136 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
3138 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
3139 NULL
, address
, count
, 0xf);
3143 if (target
== TGSI_TEXTURE_BUFFER
) {
3144 LLVMTypeRef v2i128
= LLVMVectorType(ctx
->i128
, 2);
3146 /* Bitcast and truncate v8i32 to v16i8. */
3147 LLVMValueRef res
= res_ptr
;
3148 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
3149 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
3150 res
= LLVMBuildBitCast(gallivm
->builder
, res
, ctx
->v16i8
, "");
3152 emit_data
->dst_type
= ctx
->v4f32
;
3153 emit_data
->args
[0] = res
;
3154 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
3155 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
3156 emit_data
->arg_count
= 3;
3160 /* Fetch and project texture coordinates */
3161 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
3162 for (chan
= 0; chan
< 3; chan
++ ) {
3163 coords
[chan
] = lp_build_emit_fetch(bld_base
,
3166 if (opcode
== TGSI_OPCODE_TXP
)
3167 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
3173 if (opcode
== TGSI_OPCODE_TXP
)
3174 coords
[3] = bld_base
->base
.one
;
3177 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
3178 /* The offsets are six-bit signed integers packed like this:
3179 * X=[5:0], Y=[13:8], and Z=[21:16].
3181 LLVMValueRef offset
[3], pack
;
3183 assert(inst
->Texture
.NumOffsets
== 1);
3185 for (chan
= 0; chan
< 3; chan
++) {
3186 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
3187 emit_data
->inst
, 0, chan
);
3188 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
3189 lp_build_const_int32(gallivm
, 0x3f), "");
3191 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
3192 lp_build_const_int32(gallivm
, chan
*8), "");
3195 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
3196 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
3197 address
[count
++] = pack
;
3200 /* Pack LOD bias value */
3201 if (opcode
== TGSI_OPCODE_TXB
)
3202 address
[count
++] = coords
[3];
3203 if (opcode
== TGSI_OPCODE_TXB2
)
3204 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3206 /* Pack depth comparison value */
3207 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
3208 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
3209 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3211 assert(ref_pos
>= 0);
3212 address
[count
++] = coords
[ref_pos
];
3216 /* Pack user derivatives */
3217 if (opcode
== TGSI_OPCODE_TXD
) {
3218 int param
, num_src_deriv_channels
;
3221 case TGSI_TEXTURE_3D
:
3222 num_src_deriv_channels
= 3;
3223 num_deriv_channels
= 3;
3225 case TGSI_TEXTURE_2D
:
3226 case TGSI_TEXTURE_SHADOW2D
:
3227 case TGSI_TEXTURE_RECT
:
3228 case TGSI_TEXTURE_SHADOWRECT
:
3229 case TGSI_TEXTURE_2D_ARRAY
:
3230 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3231 num_src_deriv_channels
= 2;
3232 num_deriv_channels
= 2;
3234 case TGSI_TEXTURE_CUBE
:
3235 case TGSI_TEXTURE_SHADOWCUBE
:
3236 case TGSI_TEXTURE_CUBE_ARRAY
:
3237 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
3238 /* Cube derivatives will be converted to 2D. */
3239 num_src_deriv_channels
= 3;
3240 num_deriv_channels
= 2;
3242 case TGSI_TEXTURE_1D
:
3243 case TGSI_TEXTURE_SHADOW1D
:
3244 case TGSI_TEXTURE_1D_ARRAY
:
3245 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3246 num_src_deriv_channels
= 1;
3247 num_deriv_channels
= 1;
3250 unreachable("invalid target");
3253 for (param
= 0; param
< 2; param
++)
3254 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
3255 derivs
[param
* num_src_deriv_channels
+ chan
] =
3256 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
3259 if (target
== TGSI_TEXTURE_CUBE
||
3260 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3261 target
== TGSI_TEXTURE_SHADOWCUBE
||
3262 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
3263 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
3265 if (opcode
== TGSI_OPCODE_TXD
)
3266 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
3267 address
[count
++] = derivs
[i
];
3269 /* Pack texture coordinates */
3270 address
[count
++] = coords
[0];
3272 address
[count
++] = coords
[1];
3274 address
[count
++] = coords
[2];
3276 /* Pack LOD or sample index */
3277 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
3278 address
[count
++] = coords
[3];
3279 else if (opcode
== TGSI_OPCODE_TXL2
)
3280 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
3283 assert(!"Cannot handle more than 16 texture address parameters");
3287 for (chan
= 0; chan
< count
; chan
++ ) {
3288 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
3289 address
[chan
], ctx
->i32
, "");
3292 /* Adjust the sample index according to FMASK.
3294 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3295 * which is the identity mapping. Each nibble says which physical sample
3296 * should be fetched to get that sample.
3298 * For example, 0x11111100 means there are only 2 samples stored and
3299 * the second sample covers 3/4 of the pixel. When reading samples 0
3300 * and 1, return physical sample 0 (determined by the first two 0s
3301 * in FMASK), otherwise return physical sample 1.
3303 * The sample index should be adjusted as follows:
3304 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
3306 if (target
== TGSI_TEXTURE_2D_MSAA
||
3307 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
3308 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3309 struct lp_build_emit_data txf_emit_data
= *emit_data
;
3310 LLVMValueRef txf_address
[4];
3311 unsigned txf_count
= count
;
3312 struct tgsi_full_instruction inst
= {};
3314 memcpy(txf_address
, address
, sizeof(txf_address
));
3316 if (target
== TGSI_TEXTURE_2D_MSAA
) {
3317 txf_address
[2] = bld_base
->uint_bld
.zero
;
3319 txf_address
[3] = bld_base
->uint_bld
.zero
;
3321 /* Read FMASK using TXF. */
3322 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
3323 inst
.Texture
.Texture
= target
;
3324 txf_emit_data
.inst
= &inst
;
3325 txf_emit_data
.chan
= 0;
3326 set_tex_fetch_args(ctx
, &txf_emit_data
, TGSI_OPCODE_TXF
,
3327 target
, fmask_ptr
, NULL
,
3328 txf_address
, txf_count
, 0xf);
3329 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
3331 /* Initialize some constants. */
3332 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
3333 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
3335 /* Apply the formula. */
3336 LLVMValueRef fmask
=
3337 LLVMBuildExtractElement(gallivm
->builder
,
3338 txf_emit_data
.output
[0],
3339 uint_bld
->zero
, "");
3341 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
3343 LLVMValueRef sample_index4
=
3344 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
3346 LLVMValueRef shifted_fmask
=
3347 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
3349 LLVMValueRef final_sample
=
3350 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
3352 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3353 * resource descriptor is 0 (invalid),
3355 LLVMValueRef fmask_desc
=
3356 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
3359 LLVMValueRef fmask_word1
=
3360 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
3363 LLVMValueRef word1_is_nonzero
=
3364 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
3365 fmask_word1
, uint_bld
->zero
, "");
3367 /* Replace the MSAA sample index. */
3368 address
[sample_chan
] =
3369 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
3370 final_sample
, address
[sample_chan
], "");
3373 if (opcode
== TGSI_OPCODE_TXF
) {
3374 /* add tex offsets */
3375 if (inst
->Texture
.NumOffsets
) {
3376 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3377 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
3378 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
3380 assert(inst
->Texture
.NumOffsets
== 1);
3383 case TGSI_TEXTURE_3D
:
3384 address
[2] = lp_build_add(uint_bld
, address
[2],
3385 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
3387 case TGSI_TEXTURE_2D
:
3388 case TGSI_TEXTURE_SHADOW2D
:
3389 case TGSI_TEXTURE_RECT
:
3390 case TGSI_TEXTURE_SHADOWRECT
:
3391 case TGSI_TEXTURE_2D_ARRAY
:
3392 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3394 lp_build_add(uint_bld
, address
[1],
3395 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
3397 case TGSI_TEXTURE_1D
:
3398 case TGSI_TEXTURE_SHADOW1D
:
3399 case TGSI_TEXTURE_1D_ARRAY
:
3400 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3402 lp_build_add(uint_bld
, address
[0],
3403 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
3405 /* texture offsets do not apply to other texture targets */
3410 if (opcode
== TGSI_OPCODE_TG4
) {
3411 unsigned gather_comp
= 0;
3413 /* DMASK was repurposed for GATHER4. 4 components are always
3414 * returned and DMASK works like a swizzle - it selects
3415 * the component to fetch. The only valid DMASK values are
3416 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3417 * (red,red,red,red) etc.) The ISA document doesn't mention
3421 /* Get the component index from src1.x for Gather4. */
3422 if (!tgsi_is_shadow_target(target
)) {
3423 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3424 LLVMValueRef comp_imm
;
3425 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
3427 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
3429 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
3430 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
3431 gather_comp
= CLAMP(gather_comp
, 0, 3);
3434 dmask
= 1 << gather_comp
;
3437 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
3438 samp_ptr
, address
, count
, dmask
);
3441 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3442 struct lp_build_tgsi_context
*bld_base
,
3443 struct lp_build_emit_data
*emit_data
)
3445 struct lp_build_context
*base
= &bld_base
->base
;
3446 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
3447 unsigned target
= emit_data
->inst
->Texture
.Texture
;
3448 char intr_name
[127];
3449 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
3450 bool is_shadow
= tgsi_is_shadow_target(target
);
3452 const char *name
= "llvm.SI.image.sample";
3453 const char *infix
= "";
3455 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
3456 /* Just return the buffer size. */
3457 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
3461 if (target
== TGSI_TEXTURE_BUFFER
) {
3462 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3463 base
->gallivm
->builder
,
3464 "llvm.SI.vs.load.input", emit_data
->dst_type
,
3465 emit_data
->args
, emit_data
->arg_count
,
3466 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3471 case TGSI_OPCODE_TXF
:
3472 name
= target
== TGSI_TEXTURE_2D_MSAA
||
3473 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
3474 "llvm.SI.image.load" :
3475 "llvm.SI.image.load.mip";
3479 case TGSI_OPCODE_TXQ
:
3480 name
= "llvm.SI.getresinfo";
3484 case TGSI_OPCODE_LODQ
:
3485 name
= "llvm.SI.getlod";
3489 case TGSI_OPCODE_TEX
:
3490 case TGSI_OPCODE_TEX2
:
3491 case TGSI_OPCODE_TXP
:
3493 case TGSI_OPCODE_TXB
:
3494 case TGSI_OPCODE_TXB2
:
3497 case TGSI_OPCODE_TXL
:
3498 case TGSI_OPCODE_TXL2
:
3501 case TGSI_OPCODE_TXD
:
3504 case TGSI_OPCODE_TG4
:
3505 name
= "llvm.SI.gather4";
3512 /* Add the type and suffixes .c, .o if needed. */
3513 build_int_type_name(LLVMTypeOf(emit_data
->args
[0]), type
, sizeof(type
));
3514 sprintf(intr_name
, "%s%s%s%s.%s",
3515 name
, is_shadow
? ".c" : "", infix
,
3516 has_offset
? ".o" : "", type
);
3518 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3519 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
3520 emit_data
->args
, emit_data
->arg_count
,
3521 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3523 /* Divide the number of layers by 6 to get the number of cubes. */
3524 if (opcode
== TGSI_OPCODE_TXQ
&&
3525 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
3526 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
3527 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3528 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
3529 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
3531 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
3532 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
3533 z
= LLVMBuildSDiv(builder
, z
, six
, "");
3535 emit_data
->output
[emit_data
->chan
] =
3536 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
3540 static void si_llvm_emit_txqs(
3541 const struct lp_build_tgsi_action
*action
,
3542 struct lp_build_tgsi_context
*bld_base
,
3543 struct lp_build_emit_data
*emit_data
)
3545 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3546 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3547 LLVMBuilderRef builder
= gallivm
->builder
;
3548 LLVMValueRef res
, samples
;
3549 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
3551 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3554 /* Read the samples from the descriptor directly. */
3555 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
3556 samples
= LLVMBuildExtractElement(
3558 lp_build_const_int32(gallivm
, 3), "");
3559 samples
= LLVMBuildLShr(builder
, samples
,
3560 lp_build_const_int32(gallivm
, 16), "");
3561 samples
= LLVMBuildAnd(builder
, samples
,
3562 lp_build_const_int32(gallivm
, 0xf), "");
3563 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
3566 emit_data
->output
[emit_data
->chan
] = samples
;
3570 * SI implements derivatives using the local data store (LDS)
3571 * All writes to the LDS happen in all executing threads at
3572 * the same time. TID is the Thread ID for the current
3573 * thread and is a value between 0 and 63, representing
3574 * the thread's position in the wavefront.
3576 * For the pixel shader threads are grouped into quads of four pixels.
3577 * The TIDs of the pixels of a quad are:
3585 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
3586 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
3587 * the current pixel's column, and masking with 0xfffffffe yields the TID
3588 * of the left pixel of the current pixel's row.
3590 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
3591 * adding 2 yields the TID of the pixel below the top pixel.
3593 /* masks for thread ID. */
3594 #define TID_MASK_TOP_LEFT 0xfffffffc
3595 #define TID_MASK_TOP 0xfffffffd
3596 #define TID_MASK_LEFT 0xfffffffe
3598 static void si_llvm_emit_ddxy(
3599 const struct lp_build_tgsi_action
*action
,
3600 struct lp_build_tgsi_context
*bld_base
,
3601 struct lp_build_emit_data
*emit_data
)
3603 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3604 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3605 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3606 unsigned opcode
= inst
->Instruction
.Opcode
;
3607 LLVMValueRef indices
[2];
3608 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
3609 LLVMValueRef tl
, trbl
, result
[4];
3610 unsigned swizzle
[4];
3615 indices
[0] = bld_base
->uint_bld
.zero
;
3616 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3617 NULL
, 0, LLVMReadNoneAttribute
);
3618 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3621 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3622 mask
= TID_MASK_LEFT
;
3623 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3624 mask
= TID_MASK_TOP
;
3626 mask
= TID_MASK_TOP_LEFT
;
3628 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
3629 lp_build_const_int32(gallivm
, mask
), "");
3630 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3633 /* for DDX we want to next X pixel, DDY next Y pixel. */
3634 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3635 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
3636 lp_build_const_int32(gallivm
, idx
), "");
3637 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3640 for (c
= 0; c
< 4; ++c
) {
3643 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
3644 for (i
= 0; i
< c
; ++i
) {
3645 if (swizzle
[i
] == swizzle
[c
]) {
3646 result
[c
] = result
[i
];
3653 LLVMBuildStore(gallivm
->builder
,
3654 LLVMBuildBitCast(gallivm
->builder
,
3655 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
3659 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
3660 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3662 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
3663 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, ctx
->f32
, "");
3665 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
3668 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
3672 * this takes an I,J coordinate pair,
3673 * and works out the X and Y derivatives.
3674 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3676 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3677 struct lp_build_tgsi_context
*bld_base
,
3678 LLVMValueRef interp_ij
)
3680 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3681 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3682 LLVMValueRef indices
[2];
3683 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3684 LLVMValueRef tl
, tr
, bl
, result
[4];
3687 indices
[0] = bld_base
->uint_bld
.zero
;
3688 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3689 NULL
, 0, LLVMReadNoneAttribute
);
3690 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3693 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3694 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3696 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3697 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3700 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3704 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3707 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3708 lp_build_const_int32(gallivm
, 1), "");
3709 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3712 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3713 lp_build_const_int32(gallivm
, 2), "");
3714 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3717 for (c
= 0; c
< 2; ++c
) {
3718 LLVMValueRef store_val
;
3719 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3721 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3722 interp_ij
, c_ll
, "");
3723 LLVMBuildStore(gallivm
->builder
,
3727 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3728 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3730 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3731 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, ctx
->f32
, "");
3733 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3735 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3736 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3738 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3739 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, ctx
->f32
, "");
3741 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3744 return lp_build_gather_values(gallivm
, result
, 4);
3747 static void interp_fetch_args(
3748 struct lp_build_tgsi_context
*bld_base
,
3749 struct lp_build_emit_data
*emit_data
)
3751 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3752 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3753 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3755 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3756 /* offset is in second src, first two channels */
3757 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3760 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3763 emit_data
->arg_count
= 2;
3764 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3765 LLVMValueRef sample_position
;
3766 LLVMValueRef sample_id
;
3767 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3769 /* fetch sample ID, then fetch its sample position,
3770 * and place into first two channels.
3772 sample_id
= lp_build_emit_fetch(bld_base
,
3773 emit_data
->inst
, 1, TGSI_CHAN_X
);
3774 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3776 sample_position
= load_sample_position(&ctx
->radeon_bld
, sample_id
);
3778 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3780 lp_build_const_int32(gallivm
, 0), "");
3782 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3783 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3785 lp_build_const_int32(gallivm
, 1), "");
3786 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3787 emit_data
->arg_count
= 2;
3791 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3792 struct lp_build_tgsi_context
*bld_base
,
3793 struct lp_build_emit_data
*emit_data
)
3795 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3796 struct si_shader
*shader
= ctx
->shader
;
3797 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3798 LLVMValueRef interp_param
;
3799 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3800 const char *intr_name
;
3801 int input_index
= inst
->Src
[0].Register
.Index
;
3804 LLVMValueRef attr_number
;
3805 LLVMValueRef params
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3806 int interp_param_idx
;
3807 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
3810 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3812 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3813 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3814 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3816 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3818 interp_param_idx
= lookup_interp_param_index(interp
, location
);
3819 if (interp_param_idx
== -1)
3821 else if (interp_param_idx
)
3822 interp_param
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3824 interp_param
= NULL
;
3826 attr_number
= lp_build_const_int32(gallivm
, input_index
);
3828 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3829 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3830 LLVMValueRef ij_out
[2];
3831 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3834 * take the I then J parameters, and the DDX/Y for it, and
3835 * calculate the IJ inputs for the interpolator.
3836 * temp1 = ddx * offset/sample.x + I;
3837 * interp_param.I = ddy * offset/sample.y + temp1;
3838 * temp1 = ddx * offset/sample.x + J;
3839 * interp_param.J = ddy * offset/sample.y + temp1;
3841 for (i
= 0; i
< 2; i
++) {
3842 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3843 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
3844 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3845 ddxy_out
, ix_ll
, "");
3846 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3847 ddxy_out
, iy_ll
, "");
3848 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3849 interp_param
, ix_ll
, "");
3850 LLVMValueRef temp1
, temp2
;
3852 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3855 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3857 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3859 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3861 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3863 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
3864 temp2
, ctx
->i32
, "");
3866 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
3869 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3870 for (chan
= 0; chan
< 2; chan
++) {
3871 LLVMValueRef args
[4];
3872 LLVMValueRef llvm_chan
;
3875 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3876 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
3878 args
[0] = llvm_chan
;
3879 args
[1] = attr_number
;
3881 args
[3] = interp_param
;
3883 emit_data
->output
[chan
] =
3884 lp_build_intrinsic(gallivm
->builder
, intr_name
,
3885 ctx
->f32
, args
, args
[3] ? 4 : 3,
3886 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3890 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3891 struct lp_build_emit_data
*emit_data
)
3893 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3894 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3897 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3899 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
3903 /* Emit one vertex from the geometry shader */
3904 static void si_llvm_emit_vertex(
3905 const struct lp_build_tgsi_action
*action
,
3906 struct lp_build_tgsi_context
*bld_base
,
3907 struct lp_build_emit_data
*emit_data
)
3909 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3910 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3911 struct si_shader
*shader
= ctx
->shader
;
3912 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3913 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3914 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
3915 SI_PARAM_GS2VS_OFFSET
);
3916 LLVMValueRef gs_next_vertex
;
3917 LLVMValueRef can_emit
, kill
;
3918 LLVMValueRef args
[2];
3923 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3925 /* Write vertex attribute values to GSVS ring */
3926 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3927 ctx
->gs_next_vertex
[stream
],
3930 /* If this thread has already emitted the declared maximum number of
3931 * vertices, kill it: excessive vertex emissions are not supposed to
3932 * have any effect, and GS threads have no externally observable
3933 * effects other than emitting vertices.
3935 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
3936 lp_build_const_int32(gallivm
,
3937 shader
->selector
->gs_max_out_vertices
), "");
3938 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3939 lp_build_const_float(gallivm
, 1.0f
),
3940 lp_build_const_float(gallivm
, -1.0f
));
3942 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
3943 ctx
->voidt
, &kill
, 1, 0);
3945 for (i
= 0; i
< info
->num_outputs
; i
++) {
3946 LLVMValueRef
*out_ptr
=
3947 ctx
->radeon_bld
.soa
.outputs
[i
];
3949 for (chan
= 0; chan
< 4; chan
++) {
3950 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3951 LLVMValueRef voffset
=
3952 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
3953 shader
->selector
->gs_max_out_vertices
);
3955 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3956 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3958 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
3960 build_tbuffer_store(ctx
,
3961 ctx
->gsvs_ring
[stream
],
3963 voffset
, soffset
, 0,
3964 V_008F0C_BUF_DATA_FORMAT_32
,
3965 V_008F0C_BUF_NUM_FORMAT_UINT
,
3969 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3970 lp_build_const_int32(gallivm
, 1));
3972 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
3974 /* Signal vertex emission */
3975 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
3976 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3977 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3978 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
3981 /* Cut one primitive from the geometry shader */
3982 static void si_llvm_emit_primitive(
3983 const struct lp_build_tgsi_action
*action
,
3984 struct lp_build_tgsi_context
*bld_base
,
3985 struct lp_build_emit_data
*emit_data
)
3987 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3988 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3989 LLVMValueRef args
[2];
3992 /* Signal primitive cut */
3993 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3994 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
3995 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3996 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3997 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
4000 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
4001 struct lp_build_tgsi_context
*bld_base
,
4002 struct lp_build_emit_data
*emit_data
)
4004 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4005 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4007 lp_build_intrinsic(gallivm
->builder
,
4008 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
4009 : "llvm.AMDGPU.barrier.local",
4010 ctx
->voidt
, NULL
, 0, LLVMNoUnwindAttribute
);
4013 static const struct lp_build_tgsi_action tex_action
= {
4014 .fetch_args
= tex_fetch_args
,
4015 .emit
= build_tex_intrinsic
,
4018 static const struct lp_build_tgsi_action interp_action
= {
4019 .fetch_args
= interp_fetch_args
,
4020 .emit
= build_interp_intrinsic
,
4023 static void si_create_function(struct si_shader_context
*ctx
,
4024 LLVMTypeRef
*returns
, unsigned num_returns
,
4025 LLVMTypeRef
*params
, unsigned num_params
,
4026 int last_array_pointer
, int last_sgpr
)
4030 radeon_llvm_create_func(&ctx
->radeon_bld
, returns
, num_returns
,
4031 params
, num_params
);
4032 radeon_llvm_shader_type(ctx
->radeon_bld
.main_fn
, ctx
->type
);
4033 ctx
->return_value
= LLVMGetUndef(ctx
->radeon_bld
.return_type
);
4035 for (i
= 0; i
<= last_sgpr
; ++i
) {
4036 LLVMValueRef P
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, i
);
4038 /* We tell llvm that array inputs are passed by value to allow Sinking pass
4039 * to move load. Inputs are constant so this is fine. */
4040 if (i
<= last_array_pointer
)
4041 LLVMAddAttribute(P
, LLVMByValAttribute
);
4043 LLVMAddAttribute(P
, LLVMInRegAttribute
);
4047 static void create_meta_data(struct si_shader_context
*ctx
)
4049 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
4050 LLVMValueRef args
[3];
4052 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
4054 args
[2] = lp_build_const_int32(gallivm
, 1);
4056 ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
4059 static void declare_streamout_params(struct si_shader_context
*ctx
,
4060 struct pipe_stream_output_info
*so
,
4061 LLVMTypeRef
*params
, LLVMTypeRef i32
,
4062 unsigned *num_params
)
4066 /* Streamout SGPRs. */
4067 if (so
->num_outputs
) {
4068 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
4069 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
4071 /* A streamout buffer offset is loaded if the stride is non-zero. */
4072 for (i
= 0; i
< 4; i
++) {
4076 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
4080 static unsigned llvm_get_type_size(LLVMTypeRef type
)
4082 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
4085 case LLVMIntegerTypeKind
:
4086 return LLVMGetIntTypeWidth(type
) / 8;
4087 case LLVMFloatTypeKind
:
4089 case LLVMPointerTypeKind
:
4091 case LLVMVectorTypeKind
:
4092 return LLVMGetVectorSize(type
) *
4093 llvm_get_type_size(LLVMGetElementType(type
));
4100 static void declare_tess_lds(struct si_shader_context
*ctx
)
4102 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
4103 LLVMTypeRef i32
= ctx
->radeon_bld
.soa
.bld_base
.uint_bld
.elem_type
;
4105 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
4106 unsigned vertex_data_dw_size
= 32*32*4;
4107 unsigned patch_data_dw_size
= 32*4;
4108 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
4109 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
4110 unsigned lds_dwords
= patch_dw_size
;
4112 /* The actual size is computed outside of the shader to reduce
4113 * the number of shader variants. */
4115 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4116 LLVMArrayType(i32
, lds_dwords
),
4121 static void create_function(struct si_shader_context
*ctx
)
4123 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4124 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4125 struct si_shader
*shader
= ctx
->shader
;
4126 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_NUM_VERTEX_BUFFERS
], v3i32
;
4127 LLVMTypeRef returns
[16+32*4];
4128 unsigned i
, last_array_pointer
, last_sgpr
, num_params
, num_return_sgprs
;
4129 unsigned num_returns
= 0;
4131 v3i32
= LLVMVectorType(ctx
->i32
, 3);
4133 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
4134 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
4135 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
4136 params
[SI_PARAM_IMAGES
] = const_array(ctx
->v8i32
, SI_NUM_IMAGES
);
4137 last_array_pointer
= SI_PARAM_IMAGES
;
4139 switch (ctx
->type
) {
4140 case TGSI_PROCESSOR_VERTEX
:
4141 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_VERTEX_BUFFERS
);
4142 last_array_pointer
= SI_PARAM_VERTEX_BUFFERS
;
4143 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
4144 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
4145 num_params
= SI_PARAM_START_INSTANCE
+1;
4147 if (shader
->key
.vs
.as_es
) {
4148 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4149 } else if (shader
->key
.vs
.as_ls
) {
4150 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
4151 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
4153 if (ctx
->is_gs_copy_shader
) {
4154 last_array_pointer
= SI_PARAM_CONST_BUFFERS
;
4155 num_params
= SI_PARAM_CONST_BUFFERS
+1;
4157 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
4158 num_params
= SI_PARAM_VS_STATE_BITS
+1;
4161 /* The locations of the other parameters are assigned dynamically. */
4162 declare_streamout_params(ctx
, &shader
->selector
->so
,
4163 params
, ctx
->i32
, &num_params
);
4166 last_sgpr
= num_params
-1;
4169 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
4170 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
4171 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
4172 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
4174 if (!ctx
->is_monolithic
&&
4175 !ctx
->is_gs_copy_shader
) {
4176 /* Vertex load indices. */
4177 ctx
->param_vertex_index0
= num_params
;
4179 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
4180 params
[num_params
++] = ctx
->i32
;
4182 /* PrimitiveID output. */
4183 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
)
4184 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
4185 returns
[num_returns
++] = ctx
->f32
;
4189 case TGSI_PROCESSOR_TESS_CTRL
:
4190 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
4191 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
4192 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
4193 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
4194 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
4197 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
4198 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
4199 num_params
= SI_PARAM_REL_IDS
+1;
4201 if (!ctx
->is_monolithic
) {
4202 /* PARAM_TESS_FACTOR_OFFSET is after user SGPRs. */
4203 for (i
= 0; i
<= SI_TCS_NUM_USER_SGPR
; i
++)
4204 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
4206 for (i
= 0; i
< 3; i
++)
4207 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
4211 case TGSI_PROCESSOR_TESS_EVAL
:
4212 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
4213 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
4214 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
4216 if (shader
->key
.tes
.as_es
) {
4217 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
4219 declare_streamout_params(ctx
, &shader
->selector
->so
,
4220 params
, ctx
->i32
, &num_params
);
4222 last_sgpr
= num_params
- 1;
4225 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
4226 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
4227 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
4228 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
4230 /* PrimitiveID output. */
4231 if (!ctx
->is_monolithic
&& !shader
->key
.tes
.as_es
)
4232 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
4233 returns
[num_returns
++] = ctx
->f32
;
4236 case TGSI_PROCESSOR_GEOMETRY
:
4237 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
4238 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
4239 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
4242 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
4243 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
4244 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
4245 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
4246 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
4247 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
4248 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
4249 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
4250 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
4253 case TGSI_PROCESSOR_FRAGMENT
:
4254 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
4255 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
4256 last_sgpr
= SI_PARAM_PRIM_MASK
;
4257 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
4258 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
4259 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
4260 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
4261 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
4262 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
4263 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
4264 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
4265 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
4266 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
4267 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
4268 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
4269 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
4270 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
4271 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
4272 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
4273 num_params
= SI_PARAM_POS_FIXED_PT
+1;
4275 if (!ctx
->is_monolithic
) {
4276 /* Color inputs from the prolog. */
4277 if (shader
->selector
->info
.colors_read
) {
4278 unsigned num_color_elements
=
4279 util_bitcount(shader
->selector
->info
.colors_read
);
4281 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
4282 for (i
= 0; i
< num_color_elements
; i
++)
4283 params
[num_params
++] = ctx
->f32
;
4286 /* Outputs for the epilog. */
4287 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
4290 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
4291 shader
->selector
->info
.writes_z
+
4292 shader
->selector
->info
.writes_stencil
+
4293 shader
->selector
->info
.writes_samplemask
+
4294 1 /* SampleMaskIn */;
4296 num_returns
= MAX2(num_returns
,
4298 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
4300 for (i
= 0; i
< num_return_sgprs
; i
++)
4301 returns
[i
] = ctx
->i32
;
4302 for (; i
< num_returns
; i
++)
4303 returns
[i
] = ctx
->f32
;
4308 assert(0 && "unimplemented shader");
4312 assert(num_params
<= Elements(params
));
4314 si_create_function(ctx
, returns
, num_returns
, params
,
4315 num_params
, last_array_pointer
, last_sgpr
);
4317 /* Reserve register locations for VGPR inputs the PS prolog may need. */
4318 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
&&
4319 !ctx
->is_monolithic
) {
4320 radeon_llvm_add_attribute(ctx
->radeon_bld
.main_fn
,
4321 "InitialPSInputAddr",
4322 S_0286D0_PERSP_SAMPLE_ENA(1) |
4323 S_0286D0_PERSP_CENTER_ENA(1) |
4324 S_0286D0_PERSP_CENTROID_ENA(1) |
4325 S_0286D0_LINEAR_SAMPLE_ENA(1) |
4326 S_0286D0_LINEAR_CENTER_ENA(1) |
4327 S_0286D0_LINEAR_CENTROID_ENA(1) |
4328 S_0286D0_FRONT_FACE_ENA(1) |
4329 S_0286D0_POS_FIXED_PT_ENA(1));
4332 shader
->info
.num_input_sgprs
= 0;
4333 shader
->info
.num_input_vgprs
= 0;
4335 for (i
= 0; i
<= last_sgpr
; ++i
)
4336 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
4338 /* Unused fragment shader inputs are eliminated by the compiler,
4339 * so we don't know yet how many there will be.
4341 if (ctx
->type
!= TGSI_PROCESSOR_FRAGMENT
)
4342 for (; i
< num_params
; ++i
)
4343 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
4345 if (bld_base
->info
&&
4346 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
4347 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
4348 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
4349 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
4350 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
4351 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
4353 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4354 LLVMArrayType(ctx
->i32
, 64),
4358 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
4359 ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
4360 ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
)
4361 declare_tess_lds(ctx
);
4364 static void preload_constants(struct si_shader_context
*ctx
)
4366 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4367 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4368 const struct tgsi_shader_info
*info
= bld_base
->info
;
4370 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
4372 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
4373 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
4378 /* Allocate space for the constant values */
4379 ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
4381 /* Load the resource descriptor */
4382 ctx
->const_buffers
[buf
] =
4383 build_indexed_load_const(ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
4385 /* Load the constants, we rely on the code sinking to do the rest */
4386 for (i
= 0; i
< num_const
* 4; ++i
) {
4387 ctx
->constants
[buf
][i
] =
4388 buffer_load_const(gallivm
->builder
,
4389 ctx
->const_buffers
[buf
],
4390 lp_build_const_int32(gallivm
, i
* 4),
4396 static void preload_samplers(struct si_shader_context
*ctx
)
4398 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4399 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4400 const struct tgsi_shader_info
*info
= bld_base
->info
;
4401 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
4402 LLVMValueRef offset
;
4404 if (num_samplers
== 0)
4407 /* Load the resources and samplers, we rely on the code sinking to do the rest */
4408 for (i
= 0; i
< num_samplers
; ++i
) {
4410 offset
= lp_build_const_int32(gallivm
, i
);
4411 ctx
->sampler_views
[i
] =
4412 get_sampler_desc(ctx
, offset
, DESC_IMAGE
);
4414 /* FMASK resource */
4415 if (info
->is_msaa_sampler
[i
])
4417 get_sampler_desc(ctx
, offset
, DESC_FMASK
);
4419 ctx
->sampler_states
[i
] =
4420 get_sampler_desc(ctx
, offset
, DESC_SAMPLER
);
4424 static void preload_images(struct si_shader_context
*ctx
)
4426 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4427 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4428 unsigned num_images
= bld_base
->info
->file_max
[TGSI_FILE_IMAGE
] + 1;
4429 LLVMValueRef res_ptr
;
4432 if (num_images
== 0)
4435 res_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_IMAGES
);
4437 for (i
= 0; i
< num_images
; ++i
) {
4438 /* Rely on LLVM to shrink the load for buffer resources. */
4440 build_indexed_load_const(ctx
, res_ptr
,
4441 lp_build_const_int32(gallivm
, i
));
4445 static void preload_streamout_buffers(struct si_shader_context
*ctx
)
4447 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4448 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4451 /* Streamout can only be used if the shader is compiled as VS. */
4452 if (!ctx
->shader
->selector
->so
.num_outputs
||
4453 (ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4454 (ctx
->shader
->key
.vs
.as_es
||
4455 ctx
->shader
->key
.vs
.as_ls
)) ||
4456 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4457 ctx
->shader
->key
.tes
.as_es
))
4460 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4461 SI_PARAM_RW_BUFFERS
);
4463 /* Load the resources, we rely on the code sinking to do the rest */
4464 for (i
= 0; i
< 4; ++i
) {
4465 if (ctx
->shader
->selector
->so
.stride
[i
]) {
4466 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
4467 SI_SO_BUF_OFFSET
+ i
);
4469 ctx
->so_buffers
[i
] = build_indexed_load_const(ctx
, buf_ptr
, offset
);
4475 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
4478 static void preload_ring_buffers(struct si_shader_context
*ctx
)
4480 struct gallivm_state
*gallivm
=
4481 ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
4483 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4484 SI_PARAM_RW_BUFFERS
);
4486 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4487 ctx
->shader
->key
.vs
.as_es
) ||
4488 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4489 ctx
->shader
->key
.tes
.as_es
) ||
4490 ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4491 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
4494 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4497 if (ctx
->is_gs_copy_shader
) {
4498 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
4501 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4503 if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4505 for (i
= 0; i
< 4; i
++) {
4506 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
4509 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4514 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
4515 LLVMValueRef param_sampler_views
,
4516 unsigned param_pos_fixed_pt
)
4518 struct lp_build_tgsi_context
*bld_base
=
4519 &ctx
->radeon_bld
.soa
.bld_base
;
4520 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4521 struct lp_build_emit_data result
= {};
4522 struct tgsi_full_instruction inst
= {};
4523 LLVMValueRef desc
, sampler_index
, address
[2], pix
;
4525 /* Use the fixed-point gl_FragCoord input.
4526 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
4527 * per coordinate to get the repeating effect.
4529 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
4530 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
4532 /* Load the sampler view descriptor. */
4533 sampler_index
= lp_build_const_int32(gallivm
, SI_POLY_STIPPLE_SAMPLER
);
4534 desc
= get_sampler_desc_custom(ctx
, param_sampler_views
,
4535 sampler_index
, DESC_IMAGE
);
4537 /* Load the texel. */
4538 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
4539 inst
.Texture
.Texture
= TGSI_TEXTURE_2D_MSAA
; /* = use load, not load_mip */
4540 result
.inst
= &inst
;
4541 set_tex_fetch_args(ctx
, &result
, TGSI_OPCODE_TXF
,
4542 inst
.Texture
.Texture
,
4543 desc
, NULL
, address
, ARRAY_SIZE(address
), 0xf);
4544 build_tex_intrinsic(&tex_action
, bld_base
, &result
);
4546 /* Kill the thread accordingly. */
4547 pix
= LLVMBuildExtractElement(gallivm
->builder
, result
.output
[0],
4548 lp_build_const_int32(gallivm
, 3), "");
4549 pix
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, pix
);
4550 pix
= LLVMBuildFNeg(gallivm
->builder
, pix
, "");
4552 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
4553 LLVMVoidTypeInContext(gallivm
->context
),
4557 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
4558 struct si_shader_config
*conf
,
4559 unsigned symbol_offset
)
4562 const unsigned char *config
=
4563 radeon_shader_binary_config_start(binary
, symbol_offset
);
4565 /* XXX: We may be able to emit some of these values directly rather than
4566 * extracting fields to be emitted later.
4569 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
4570 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
4571 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
4573 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
4574 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
4575 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
4576 case R_00B848_COMPUTE_PGM_RSRC1
:
4577 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
4578 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
4579 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
4580 conf
->rsrc1
= value
;
4582 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
4583 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
4585 case R_00B84C_COMPUTE_PGM_RSRC2
:
4586 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
4587 conf
->rsrc2
= value
;
4589 case R_0286CC_SPI_PS_INPUT_ENA
:
4590 conf
->spi_ps_input_ena
= value
;
4592 case R_0286D0_SPI_PS_INPUT_ADDR
:
4593 conf
->spi_ps_input_addr
= value
;
4595 case R_0286E8_SPI_TMPRING_SIZE
:
4596 case R_00B860_COMPUTE_TMPRING_SIZE
:
4597 /* WAVESIZE is in units of 256 dwords. */
4598 conf
->scratch_bytes_per_wave
=
4599 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
4603 static bool printed
;
4606 fprintf(stderr
, "Warning: LLVM emitted unknown "
4607 "config register: 0x%x\n", reg
);
4614 if (!conf
->spi_ps_input_addr
)
4615 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
4619 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
4620 struct si_shader
*shader
,
4621 uint64_t scratch_va
)
4624 uint32_t scratch_rsrc_dword0
= scratch_va
;
4625 uint32_t scratch_rsrc_dword1
=
4626 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
4627 | S_008F04_STRIDE(shader
->config
.scratch_bytes_per_wave
/ 64);
4629 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
4630 const struct radeon_shader_reloc
*reloc
=
4631 &shader
->binary
.relocs
[i
];
4632 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
4633 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4634 &scratch_rsrc_dword0
, 4);
4635 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
4636 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4637 &scratch_rsrc_dword1
, 4);
4642 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
4644 unsigned size
= shader
->binary
.code_size
;
4647 size
+= shader
->prolog
->binary
.code_size
;
4649 size
+= shader
->epilog
->binary
.code_size
;
4653 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
4655 const struct radeon_shader_binary
*prolog
=
4656 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
4657 const struct radeon_shader_binary
*epilog
=
4658 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
4659 const struct radeon_shader_binary
*mainb
= &shader
->binary
;
4660 unsigned bo_size
= si_get_shader_binary_size(shader
) +
4661 (!epilog
? mainb
->rodata_size
: 0);
4664 assert(!prolog
|| !prolog
->rodata_size
);
4665 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
4666 assert(!epilog
|| !epilog
->rodata_size
);
4668 r600_resource_reference(&shader
->bo
, NULL
);
4669 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
4670 PIPE_USAGE_IMMUTABLE
,
4676 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
4677 PIPE_TRANSFER_READ_WRITE
);
4680 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
4681 ptr
+= prolog
->code_size
;
4684 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
4685 ptr
+= mainb
->code_size
;
4688 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
4689 else if (mainb
->rodata_size
> 0)
4690 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
4692 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
4696 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
4697 struct pipe_debug_callback
*debug
,
4698 const char *name
, FILE *file
)
4703 if (binary
->disasm_string
) {
4704 fprintf(file
, "Shader %s disassembly:\n", name
);
4705 fprintf(file
, "%s", binary
->disasm_string
);
4707 if (debug
&& debug
->debug_message
) {
4708 /* Very long debug messages are cut off, so send the
4709 * disassembly one line at a time. This causes more
4710 * overhead, but on the plus side it simplifies
4711 * parsing of resulting logs.
4713 pipe_debug_message(debug
, SHADER_INFO
,
4714 "Shader Disassembly Begin");
4716 line
= binary
->disasm_string
;
4718 p
= strchrnul(line
, '\n');
4722 pipe_debug_message(debug
, SHADER_INFO
,
4723 "%.*s", count
, line
);
4731 pipe_debug_message(debug
, SHADER_INFO
,
4732 "Shader Disassembly End");
4735 fprintf(file
, "Shader %s binary:\n", name
);
4736 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
4737 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
4738 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
4739 binary
->code
[i
+ 1], binary
->code
[i
]);
4744 static void si_shader_dump_stats(struct si_screen
*sscreen
,
4745 struct si_shader_config
*conf
,
4746 unsigned num_inputs
,
4748 struct pipe_debug_callback
*debug
,
4752 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
4753 unsigned lds_per_wave
= 0;
4754 unsigned max_simd_waves
= 10;
4756 /* Compute LDS usage for PS. */
4757 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4758 /* The minimum usage per wave is (num_inputs * 36). The maximum
4759 * usage is (num_inputs * 36 * 16).
4760 * We can get anything in between and it varies between waves.
4762 * Other stages don't know the size at compile time or don't
4763 * allocate LDS per wave, but instead they do it per thread group.
4765 lds_per_wave
= conf
->lds_size
* lds_increment
+
4766 align(num_inputs
* 36, lds_increment
);
4769 /* Compute the per-SIMD wave counts. */
4770 if (conf
->num_sgprs
) {
4771 if (sscreen
->b
.chip_class
>= VI
)
4772 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
4774 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
4777 if (conf
->num_vgprs
)
4778 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
4780 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
4784 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
4786 if (file
!= stderr
||
4787 r600_can_dump_shader(&sscreen
->b
, processor
)) {
4788 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4789 fprintf(file
, "*** SHADER CONFIG ***\n"
4790 "SPI_PS_INPUT_ADDR = 0x%04x\n"
4791 "SPI_PS_INPUT_ENA = 0x%04x\n",
4792 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
4795 fprintf(file
, "*** SHADER STATS ***\n"
4798 "Code Size: %d bytes\n"
4800 "Scratch: %d bytes per wave\n"
4802 "********************\n",
4803 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4804 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4808 pipe_debug_message(debug
, SHADER_INFO
,
4809 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
4810 "LDS: %d Scratch: %d Max Waves: %d",
4811 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4812 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4816 static const char *si_get_shader_name(struct si_shader
*shader
,
4819 switch (processor
) {
4820 case TGSI_PROCESSOR_VERTEX
:
4821 if (shader
->key
.vs
.as_es
)
4822 return "Vertex Shader as ES";
4823 else if (shader
->key
.vs
.as_ls
)
4824 return "Vertex Shader as LS";
4826 return "Vertex Shader as VS";
4827 case TGSI_PROCESSOR_TESS_CTRL
:
4828 return "Tessellation Control Shader";
4829 case TGSI_PROCESSOR_TESS_EVAL
:
4830 if (shader
->key
.tes
.as_es
)
4831 return "Tessellation Evaluation Shader as ES";
4833 return "Tessellation Evaluation Shader as VS";
4834 case TGSI_PROCESSOR_GEOMETRY
:
4835 if (shader
->gs_copy_shader
== NULL
)
4836 return "GS Copy Shader as VS";
4838 return "Geometry Shader";
4839 case TGSI_PROCESSOR_FRAGMENT
:
4840 return "Pixel Shader";
4841 case TGSI_PROCESSOR_COMPUTE
:
4842 return "Compute Shader";
4844 return "Unknown Shader";
4848 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
4849 struct pipe_debug_callback
*debug
, unsigned processor
,
4852 if (file
!= stderr
||
4853 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
4854 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
4855 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
4858 si_shader_dump_disassembly(&shader
->prolog
->binary
,
4859 debug
, "prolog", file
);
4861 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
4864 si_shader_dump_disassembly(&shader
->epilog
->binary
,
4865 debug
, "epilog", file
);
4866 fprintf(file
, "\n");
4869 si_shader_dump_stats(sscreen
, &shader
->config
,
4870 shader
->selector
? shader
->selector
->info
.num_inputs
: 0,
4871 si_get_shader_binary_size(shader
), debug
, processor
,
4875 int si_compile_llvm(struct si_screen
*sscreen
,
4876 struct radeon_shader_binary
*binary
,
4877 struct si_shader_config
*conf
,
4878 LLVMTargetMachineRef tm
,
4880 struct pipe_debug_callback
*debug
,
4885 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
4887 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
4888 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
4890 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
4891 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
4892 LLVMDumpModule(mod
);
4893 fprintf(stderr
, "\n");
4897 if (!si_replace_shader(count
, binary
)) {
4898 r
= radeon_llvm_compile(mod
, binary
,
4899 r600_get_llvm_processor_name(sscreen
->b
.family
), tm
,
4905 si_shader_binary_read_config(binary
, conf
, 0);
4907 /* Enable 64-bit and 16-bit denormals, because there is no performance
4910 * If denormals are enabled, all floating-point output modifiers are
4913 * Don't enable denormals for 32-bit floats, because:
4914 * - Floating-point output modifiers would be ignored by the hw.
4915 * - Some opcodes don't support denormals, such as v_mad_f32. We would
4916 * have to stop using those.
4917 * - SI & CI would be very slow.
4919 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
4921 FREE(binary
->config
);
4922 FREE(binary
->global_symbol_offsets
);
4923 binary
->config
= NULL
;
4924 binary
->global_symbol_offsets
= NULL
;
4926 /* Some shaders can't have rodata because their binaries can be
4929 if (binary
->rodata_size
&&
4930 (processor
== TGSI_PROCESSOR_VERTEX
||
4931 processor
== TGSI_PROCESSOR_TESS_CTRL
||
4932 processor
== TGSI_PROCESSOR_TESS_EVAL
||
4933 processor
== TGSI_PROCESSOR_FRAGMENT
)) {
4934 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
4941 /* Generate code for the hardware VS shader stage to go with a geometry shader */
4942 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
4943 struct si_shader_context
*ctx
,
4944 struct si_shader
*gs
,
4945 struct pipe_debug_callback
*debug
)
4947 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
4948 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4949 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
4950 struct si_shader_output_values
*outputs
;
4951 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
4952 LLVMValueRef args
[9];
4955 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
4957 si_init_shader_ctx(ctx
, sscreen
, ctx
->shader
, ctx
->tm
);
4958 ctx
->type
= TGSI_PROCESSOR_VERTEX
;
4959 ctx
->is_gs_copy_shader
= true;
4961 create_meta_data(ctx
);
4962 create_function(ctx
);
4963 preload_streamout_buffers(ctx
);
4964 preload_ring_buffers(ctx
);
4966 args
[0] = ctx
->gsvs_ring
[0];
4967 args
[1] = lp_build_mul_imm(uint
,
4968 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4969 ctx
->param_vertex_id
),
4971 args
[3] = uint
->zero
;
4972 args
[4] = uint
->one
; /* OFFEN */
4973 args
[5] = uint
->zero
; /* IDXEN */
4974 args
[6] = uint
->one
; /* GLC */
4975 args
[7] = uint
->one
; /* SLC */
4976 args
[8] = uint
->zero
; /* TFE */
4978 /* Fetch vertex data from GSVS ring */
4979 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
4982 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
4983 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
4985 for (chan
= 0; chan
< 4; chan
++) {
4986 args
[2] = lp_build_const_int32(gallivm
,
4988 gs
->selector
->gs_max_out_vertices
* 16 * 4);
4990 outputs
[i
].values
[chan
] =
4991 LLVMBuildBitCast(gallivm
->builder
,
4992 lp_build_intrinsic(gallivm
->builder
,
4993 "llvm.SI.buffer.load.dword.i32.i32",
4995 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
5000 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
5002 LLVMBuildRet(gallivm
->builder
, ctx
->return_value
);
5004 /* Dump LLVM IR before any optimization passes */
5005 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
5006 r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
5007 LLVMDumpModule(bld_base
->base
.gallivm
->module
);
5009 radeon_llvm_finalize_module(&ctx
->radeon_bld
);
5011 r
= si_compile_llvm(sscreen
, &ctx
->shader
->binary
,
5012 &ctx
->shader
->config
, ctx
->tm
,
5013 bld_base
->base
.gallivm
->module
,
5014 debug
, TGSI_PROCESSOR_GEOMETRY
,
5017 if (r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
5018 fprintf(stderr
, "GS Copy Shader:\n");
5019 si_shader_dump(sscreen
, ctx
->shader
, debug
,
5020 TGSI_PROCESSOR_GEOMETRY
, stderr
);
5021 r
= si_shader_binary_upload(sscreen
, ctx
->shader
);
5024 radeon_llvm_dispose(&ctx
->radeon_bld
);
5030 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
5034 fprintf(f
, "SHADER KEY\n");
5037 case PIPE_SHADER_VERTEX
:
5038 fprintf(f
, " instance_divisors = {");
5039 for (i
= 0; i
< Elements(key
->vs
.prolog
.instance_divisors
); i
++)
5040 fprintf(f
, !i
? "%u" : ", %u",
5041 key
->vs
.prolog
.instance_divisors
[i
]);
5043 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
5044 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
5045 fprintf(f
, " export_prim_id = %u\n", key
->vs
.epilog
.export_prim_id
);
5048 case PIPE_SHADER_TESS_CTRL
:
5049 fprintf(f
, " prim_mode = %u\n", key
->tcs
.epilog
.prim_mode
);
5052 case PIPE_SHADER_TESS_EVAL
:
5053 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
5054 fprintf(f
, " export_prim_id = %u\n", key
->tes
.epilog
.export_prim_id
);
5057 case PIPE_SHADER_GEOMETRY
:
5060 case PIPE_SHADER_FRAGMENT
:
5061 fprintf(f
, " prolog.color_two_side = %u\n", key
->ps
.prolog
.color_two_side
);
5062 fprintf(f
, " prolog.poly_stipple = %u\n", key
->ps
.prolog
.poly_stipple
);
5063 fprintf(f
, " prolog.force_persample_interp = %u\n", key
->ps
.prolog
.force_persample_interp
);
5064 fprintf(f
, " epilog.spi_shader_col_format = 0x%x\n", key
->ps
.epilog
.spi_shader_col_format
);
5065 fprintf(f
, " epilog.color_is_int8 = 0x%X\n", key
->ps
.epilog
.color_is_int8
);
5066 fprintf(f
, " epilog.last_cbuf = %u\n", key
->ps
.epilog
.last_cbuf
);
5067 fprintf(f
, " epilog.alpha_func = %u\n", key
->ps
.epilog
.alpha_func
);
5068 fprintf(f
, " epilog.alpha_to_one = %u\n", key
->ps
.epilog
.alpha_to_one
);
5069 fprintf(f
, " epilog.poly_line_smoothing = %u\n", key
->ps
.epilog
.poly_line_smoothing
);
5070 fprintf(f
, " epilog.clamp_color = %u\n", key
->ps
.epilog
.clamp_color
);
5078 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
5079 struct si_screen
*sscreen
,
5080 struct si_shader
*shader
,
5081 LLVMTargetMachineRef tm
)
5083 struct lp_build_tgsi_context
*bld_base
;
5085 memset(ctx
, 0, sizeof(*ctx
));
5086 radeon_llvm_context_init(&ctx
->radeon_bld
, "amdgcn--");
5088 ctx
->screen
= sscreen
;
5089 if (shader
&& shader
->selector
)
5090 ctx
->type
= shader
->selector
->info
.processor
;
5093 ctx
->shader
= shader
;
5095 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5096 ctx
->i1
= LLVMInt1TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5097 ctx
->i8
= LLVMInt8TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5098 ctx
->i32
= LLVMInt32TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5099 ctx
->i64
= LLVMInt64TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5100 ctx
->i128
= LLVMIntTypeInContext(ctx
->radeon_bld
.gallivm
.context
, 128);
5101 ctx
->f32
= LLVMFloatTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
5102 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
5103 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
5104 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
5105 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
5106 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
5108 bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
5109 if (shader
&& shader
->selector
)
5110 bld_base
->info
= &shader
->selector
->info
;
5111 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
5113 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
5114 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
5115 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
5117 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
5118 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
5119 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
5120 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
5121 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
5122 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
5123 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
5124 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
5125 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
5126 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
5127 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
5128 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
5129 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
5131 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
5132 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
5133 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
5134 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
5136 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
5137 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
5138 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
5139 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
5141 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
5142 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
5143 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
5145 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
5146 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
5147 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
5148 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
5151 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
5152 LLVMTargetMachineRef tm
,
5153 struct si_shader
*shader
,
5155 struct pipe_debug_callback
*debug
)
5157 struct si_shader_selector
*sel
= shader
->selector
;
5158 struct si_shader_context ctx
;
5159 struct lp_build_tgsi_context
*bld_base
;
5163 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
5164 * conversion fails. */
5165 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
5166 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
5167 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
5168 tgsi_dump(sel
->tokens
, 0);
5169 si_dump_streamout(&sel
->so
);
5172 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
5173 ctx
.is_monolithic
= is_monolithic
;
5175 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
5177 bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5178 ctx
.radeon_bld
.load_system_value
= declare_system_value
;
5181 case TGSI_PROCESSOR_VERTEX
:
5182 ctx
.radeon_bld
.load_input
= declare_input_vs
;
5183 if (shader
->key
.vs
.as_ls
)
5184 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
5185 else if (shader
->key
.vs
.as_es
)
5186 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5188 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5190 case TGSI_PROCESSOR_TESS_CTRL
:
5191 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
5192 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
5193 bld_base
->emit_store
= store_output_tcs
;
5194 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
5196 case TGSI_PROCESSOR_TESS_EVAL
:
5197 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
5198 if (shader
->key
.tes
.as_es
)
5199 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
5201 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
5203 case TGSI_PROCESSOR_GEOMETRY
:
5204 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
5205 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
5207 case TGSI_PROCESSOR_FRAGMENT
:
5208 ctx
.radeon_bld
.load_input
= declare_input_fs
;
5210 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
5212 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
5215 assert(!"Unsupported shader type");
5219 create_meta_data(&ctx
);
5220 create_function(&ctx
);
5221 preload_constants(&ctx
);
5222 preload_samplers(&ctx
);
5223 preload_images(&ctx
);
5224 preload_streamout_buffers(&ctx
);
5225 preload_ring_buffers(&ctx
);
5227 if (ctx
.is_monolithic
&& sel
->type
== PIPE_SHADER_FRAGMENT
&&
5228 shader
->key
.ps
.prolog
.poly_stipple
) {
5229 LLVMValueRef views
= LLVMGetParam(ctx
.radeon_bld
.main_fn
,
5231 si_llvm_emit_polygon_stipple(&ctx
, views
,
5232 SI_PARAM_POS_FIXED_PT
);
5235 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
5237 for (i
= 0; i
< 4; i
++) {
5238 ctx
.gs_next_vertex
[i
] =
5239 lp_build_alloca(bld_base
->base
.gallivm
,
5244 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
5245 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
5249 LLVMBuildRet(bld_base
->base
.gallivm
->builder
, ctx
.return_value
);
5250 mod
= bld_base
->base
.gallivm
->module
;
5252 /* Dump LLVM IR before any optimization passes */
5253 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
5254 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
5255 LLVMDumpModule(mod
);
5257 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5259 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
5260 mod
, debug
, ctx
.type
, "TGSI shader");
5262 fprintf(stderr
, "LLVM failed to compile shader\n");
5266 radeon_llvm_dispose(&ctx
.radeon_bld
);
5268 /* Calculate the number of fragment input VGPRs. */
5269 if (ctx
.type
== TGSI_PROCESSOR_FRAGMENT
) {
5270 shader
->info
.num_input_vgprs
= 0;
5271 shader
->info
.face_vgpr_index
= -1;
5273 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
5274 shader
->info
.num_input_vgprs
+= 2;
5275 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
5276 shader
->info
.num_input_vgprs
+= 2;
5277 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
5278 shader
->info
.num_input_vgprs
+= 2;
5279 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
5280 shader
->info
.num_input_vgprs
+= 3;
5281 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
5282 shader
->info
.num_input_vgprs
+= 2;
5283 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
5284 shader
->info
.num_input_vgprs
+= 2;
5285 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
5286 shader
->info
.num_input_vgprs
+= 2;
5287 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
5288 shader
->info
.num_input_vgprs
+= 1;
5289 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5290 shader
->info
.num_input_vgprs
+= 1;
5291 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5292 shader
->info
.num_input_vgprs
+= 1;
5293 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5294 shader
->info
.num_input_vgprs
+= 1;
5295 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
5296 shader
->info
.num_input_vgprs
+= 1;
5297 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
5298 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
5299 shader
->info
.num_input_vgprs
+= 1;
5301 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
5302 shader
->info
.num_input_vgprs
+= 1;
5303 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
5304 shader
->info
.num_input_vgprs
+= 1;
5305 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
5306 shader
->info
.num_input_vgprs
+= 1;
5309 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
5310 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
5311 shader
->gs_copy_shader
->selector
= shader
->selector
;
5312 ctx
.shader
= shader
->gs_copy_shader
;
5313 if ((r
= si_generate_gs_copy_shader(sscreen
, &ctx
,
5315 free(shader
->gs_copy_shader
);
5316 shader
->gs_copy_shader
= NULL
;
5322 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
5323 FREE(ctx
.constants
[i
]);
5328 * Create, compile and return a shader part (prolog or epilog).
5330 * \param sscreen screen
5331 * \param list list of shader parts of the same category
5332 * \param key shader part key
5333 * \param tm LLVM target machine
5334 * \param debug debug callback
5335 * \param compile the callback responsible for compilation
5336 * \return non-NULL on success
5338 static struct si_shader_part
*
5339 si_get_shader_part(struct si_screen
*sscreen
,
5340 struct si_shader_part
**list
,
5341 union si_shader_part_key
*key
,
5342 LLVMTargetMachineRef tm
,
5343 struct pipe_debug_callback
*debug
,
5344 bool (*compile
)(struct si_screen
*,
5345 LLVMTargetMachineRef
,
5346 struct pipe_debug_callback
*,
5347 struct si_shader_part
*))
5349 struct si_shader_part
*result
;
5351 pipe_mutex_lock(sscreen
->shader_parts_mutex
);
5353 /* Find existing. */
5354 for (result
= *list
; result
; result
= result
->next
) {
5355 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
5356 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5361 /* Compile a new one. */
5362 result
= CALLOC_STRUCT(si_shader_part
);
5364 if (!compile(sscreen
, tm
, debug
, result
)) {
5366 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5370 result
->next
= *list
;
5372 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
5377 * Create a vertex shader prolog.
5379 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
5380 * All inputs are returned unmodified. The vertex load indices are
5381 * stored after them, which will used by the API VS for fetching inputs.
5383 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
5388 * (VertexID + BaseVertex),
5389 * (InstanceID + StartInstance),
5390 * (InstanceID / 2 + StartInstance)
5392 static bool si_compile_vs_prolog(struct si_screen
*sscreen
,
5393 LLVMTargetMachineRef tm
,
5394 struct pipe_debug_callback
*debug
,
5395 struct si_shader_part
*out
)
5397 union si_shader_part_key
*key
= &out
->key
;
5398 struct si_shader shader
= {};
5399 struct si_shader_context ctx
;
5400 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5401 LLVMTypeRef
*params
, *returns
;
5402 LLVMValueRef ret
, func
;
5403 int last_sgpr
, num_params
, num_returns
, i
;
5406 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5407 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
5408 ctx
.param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
5409 ctx
.param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
5411 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
5412 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
5413 sizeof(LLVMTypeRef
));
5414 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
5415 key
->vs_prolog
.last_input
+ 1) *
5416 sizeof(LLVMTypeRef
));
5420 /* Declare input and output SGPRs. */
5422 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
5423 params
[num_params
++] = ctx
.i32
;
5424 returns
[num_returns
++] = ctx
.i32
;
5426 last_sgpr
= num_params
- 1;
5428 /* 4 preloaded VGPRs (outputs must be floats) */
5429 for (i
= 0; i
< 4; i
++) {
5430 params
[num_params
++] = ctx
.i32
;
5431 returns
[num_returns
++] = ctx
.f32
;
5434 /* Vertex load indices. */
5435 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
5436 returns
[num_returns
++] = ctx
.f32
;
5438 /* Create the function. */
5439 si_create_function(&ctx
, returns
, num_returns
, params
,
5440 num_params
, -1, last_sgpr
);
5441 func
= ctx
.radeon_bld
.main_fn
;
5443 /* Copy inputs to outputs. This should be no-op, as the registers match,
5444 * but it will prevent the compiler from overwriting them unintentionally.
5446 ret
= ctx
.return_value
;
5447 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
5448 LLVMValueRef p
= LLVMGetParam(func
, i
);
5449 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5451 for (i
= num_params
- 4; i
< num_params
; i
++) {
5452 LLVMValueRef p
= LLVMGetParam(func
, i
);
5453 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
.f32
, "");
5454 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5457 /* Compute vertex load indices from instance divisors. */
5458 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
5459 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
5463 /* InstanceID / Divisor + StartInstance */
5464 index
= get_instance_index_for_fetch(&ctx
.radeon_bld
,
5465 SI_SGPR_START_INSTANCE
,
5468 /* VertexID + BaseVertex */
5469 index
= LLVMBuildAdd(gallivm
->builder
,
5470 LLVMGetParam(func
, ctx
.param_vertex_id
),
5471 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
5474 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
.f32
, "");
5475 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
5480 LLVMBuildRet(gallivm
->builder
, ret
);
5481 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5483 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5484 gallivm
->module
, debug
, ctx
.type
,
5485 "Vertex Shader Prolog"))
5488 radeon_llvm_dispose(&ctx
.radeon_bld
);
5493 * Compile the vertex shader epilog. This is also used by the tessellation
5494 * evaluation shader compiled as VS.
5496 * The input is PrimitiveID.
5498 * If PrimitiveID is required by the pixel shader, export it.
5499 * Otherwise, do nothing.
5501 static bool si_compile_vs_epilog(struct si_screen
*sscreen
,
5502 LLVMTargetMachineRef tm
,
5503 struct pipe_debug_callback
*debug
,
5504 struct si_shader_part
*out
)
5506 union si_shader_part_key
*key
= &out
->key
;
5507 struct si_shader_context ctx
;
5508 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5509 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5510 LLVMTypeRef params
[5];
5514 si_init_shader_ctx(&ctx
, sscreen
, NULL
, tm
);
5515 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
5517 /* Declare input VGPRs. */
5518 num_params
= key
->vs_epilog
.states
.export_prim_id
?
5519 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
5520 assert(num_params
<= ARRAY_SIZE(params
));
5522 for (i
= 0; i
< num_params
; i
++)
5523 params
[i
] = ctx
.f32
;
5525 /* Create the function. */
5526 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5530 if (key
->vs_epilog
.states
.export_prim_id
) {
5531 struct lp_build_context
*base
= &bld_base
->base
;
5532 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5533 LLVMValueRef args
[9];
5535 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
5536 args
[1] = uint
->zero
; /* whether the EXEC mask is valid */
5537 args
[2] = uint
->zero
; /* DONE bit */
5538 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_PARAM
+
5539 key
->vs_epilog
.prim_id_param_offset
);
5540 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
5541 args
[5] = LLVMGetParam(ctx
.radeon_bld
.main_fn
,
5542 VS_EPILOG_PRIMID_LOC
); /* X */
5543 args
[6] = uint
->undef
; /* Y */
5544 args
[7] = uint
->undef
; /* Z */
5545 args
[8] = uint
->undef
; /* W */
5547 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
5548 LLVMVoidTypeInContext(base
->gallivm
->context
),
5553 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5554 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5556 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5557 gallivm
->module
, debug
, ctx
.type
,
5558 "Vertex Shader Epilog"))
5561 radeon_llvm_dispose(&ctx
.radeon_bld
);
5566 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
5568 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
5569 LLVMTargetMachineRef tm
,
5570 struct si_shader
*shader
,
5571 struct pipe_debug_callback
*debug
,
5572 struct si_vs_epilog_bits
*states
)
5574 union si_shader_part_key epilog_key
;
5576 memset(&epilog_key
, 0, sizeof(epilog_key
));
5577 epilog_key
.vs_epilog
.states
= *states
;
5579 /* Set up the PrimitiveID output. */
5580 if (shader
->key
.vs
.epilog
.export_prim_id
) {
5581 unsigned index
= shader
->selector
->info
.num_outputs
;
5582 unsigned offset
= shader
->info
.nr_param_exports
++;
5584 epilog_key
.vs_epilog
.prim_id_param_offset
= offset
;
5585 assert(index
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
5586 shader
->info
.vs_output_param_offset
[index
] = offset
;
5589 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
5590 &epilog_key
, tm
, debug
,
5591 si_compile_vs_epilog
);
5592 return shader
->epilog
!= NULL
;
5596 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
5598 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
5599 LLVMTargetMachineRef tm
,
5600 struct si_shader
*shader
,
5601 struct pipe_debug_callback
*debug
)
5603 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5604 union si_shader_part_key prolog_key
;
5607 /* Get the prolog. */
5608 memset(&prolog_key
, 0, sizeof(prolog_key
));
5609 prolog_key
.vs_prolog
.states
= shader
->key
.vs
.prolog
;
5610 prolog_key
.vs_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
5611 prolog_key
.vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
5613 /* The prolog is a no-op if there are no inputs. */
5614 if (info
->num_inputs
) {
5616 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
5617 &prolog_key
, tm
, debug
,
5618 si_compile_vs_prolog
);
5619 if (!shader
->prolog
)
5623 /* Get the epilog. */
5624 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
&&
5625 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5626 &shader
->key
.vs
.epilog
))
5629 /* Set the instanceID flag. */
5630 for (i
= 0; i
< info
->num_inputs
; i
++)
5631 if (prolog_key
.vs_prolog
.states
.instance_divisors
[i
])
5632 shader
->info
.uses_instanceid
= true;
5638 * Select and compile (or reuse) TES parts (epilog).
5640 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
5641 LLVMTargetMachineRef tm
,
5642 struct si_shader
*shader
,
5643 struct pipe_debug_callback
*debug
)
5645 if (shader
->key
.tes
.as_es
)
5648 /* TES compiled as VS. */
5649 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5650 &shader
->key
.tes
.epilog
);
5654 * Compile the TCS epilog. This writes tesselation factors to memory based on
5655 * the output primitive type of the tesselator (determined by TES).
5657 static bool si_compile_tcs_epilog(struct si_screen
*sscreen
,
5658 LLVMTargetMachineRef tm
,
5659 struct pipe_debug_callback
*debug
,
5660 struct si_shader_part
*out
)
5662 union si_shader_part_key
*key
= &out
->key
;
5663 struct si_shader shader
= {};
5664 struct si_shader_context ctx
;
5665 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5666 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5667 LLVMTypeRef params
[16];
5669 int last_array_pointer
, last_sgpr
, num_params
;
5672 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5673 ctx
.type
= TGSI_PROCESSOR_TESS_CTRL
;
5674 shader
.key
.tcs
.epilog
= key
->tcs_epilog
.states
;
5676 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
5677 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
.v16i8
, SI_NUM_RW_BUFFERS
);
5678 last_array_pointer
= SI_PARAM_RW_BUFFERS
;
5679 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
5680 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
5681 params
[SI_PARAM_IMAGES
] = ctx
.i64
;
5682 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
.i32
;
5683 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
.i32
;
5684 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
.i32
;
5685 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
.i32
;
5686 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5687 num_params
= last_sgpr
+ 1;
5689 params
[num_params
++] = ctx
.i32
; /* patch index within the wave (REL_PATCH_ID) */
5690 params
[num_params
++] = ctx
.i32
; /* invocation ID within the patch */
5691 params
[num_params
++] = ctx
.i32
; /* LDS offset where tess factors should be loaded from */
5693 /* Create the function. */
5694 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5695 last_array_pointer
, last_sgpr
);
5696 declare_tess_lds(&ctx
);
5697 func
= ctx
.radeon_bld
.main_fn
;
5699 si_write_tess_factors(bld_base
,
5700 LLVMGetParam(func
, last_sgpr
+ 1),
5701 LLVMGetParam(func
, last_sgpr
+ 2),
5702 LLVMGetParam(func
, last_sgpr
+ 3));
5705 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5706 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5708 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5709 gallivm
->module
, debug
, ctx
.type
,
5710 "Tessellation Control Shader Epilog"))
5713 radeon_llvm_dispose(&ctx
.radeon_bld
);
5718 * Select and compile (or reuse) TCS parts (epilog).
5720 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
5721 LLVMTargetMachineRef tm
,
5722 struct si_shader
*shader
,
5723 struct pipe_debug_callback
*debug
)
5725 union si_shader_part_key epilog_key
;
5727 /* Get the epilog. */
5728 memset(&epilog_key
, 0, sizeof(epilog_key
));
5729 epilog_key
.tcs_epilog
.states
= shader
->key
.tcs
.epilog
;
5731 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
5732 &epilog_key
, tm
, debug
,
5733 si_compile_tcs_epilog
);
5734 return shader
->epilog
!= NULL
;
5738 * Compile the pixel shader prolog. This handles:
5739 * - two-side color selection and interpolation
5740 * - overriding interpolation parameters for the API PS
5741 * - polygon stippling
5743 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
5744 * overriden by other states. (e.g. per-sample interpolation)
5745 * Interpolated colors are stored after the preloaded VGPRs.
5747 static bool si_compile_ps_prolog(struct si_screen
*sscreen
,
5748 LLVMTargetMachineRef tm
,
5749 struct pipe_debug_callback
*debug
,
5750 struct si_shader_part
*out
)
5752 union si_shader_part_key
*key
= &out
->key
;
5753 struct si_shader shader
= {};
5754 struct si_shader_context ctx
;
5755 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5756 LLVMTypeRef
*params
;
5757 LLVMValueRef ret
, func
;
5758 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
5761 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5762 ctx
.type
= TGSI_PROCESSOR_FRAGMENT
;
5763 shader
.key
.ps
.prolog
= key
->ps_prolog
.states
;
5765 /* Number of inputs + 8 color elements. */
5766 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
5767 key
->ps_prolog
.num_input_vgprs
+ 8) *
5768 sizeof(LLVMTypeRef
));
5770 /* Declare inputs. */
5772 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
5773 params
[num_params
++] = ctx
.i32
;
5774 last_sgpr
= num_params
- 1;
5776 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
5777 params
[num_params
++] = ctx
.f32
;
5779 /* Declare outputs (same as inputs + add colors if needed) */
5780 num_returns
= num_params
;
5781 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
5782 for (i
= 0; i
< num_color_channels
; i
++)
5783 params
[num_returns
++] = ctx
.f32
;
5785 /* Create the function. */
5786 si_create_function(&ctx
, params
, num_returns
, params
,
5787 num_params
, -1, last_sgpr
);
5788 func
= ctx
.radeon_bld
.main_fn
;
5790 /* Copy inputs to outputs. This should be no-op, as the registers match,
5791 * but it will prevent the compiler from overwriting them unintentionally.
5793 ret
= ctx
.return_value
;
5794 for (i
= 0; i
< num_params
; i
++) {
5795 LLVMValueRef p
= LLVMGetParam(func
, i
);
5796 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5799 /* Polygon stippling. */
5800 if (key
->ps_prolog
.states
.poly_stipple
) {
5801 /* POS_FIXED_PT is always last. */
5802 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
5803 key
->ps_prolog
.num_input_vgprs
- 1;
5804 LLVMValueRef ptr
[2], views
;
5806 /* Get the pointer to sampler views. */
5807 ptr
[0] = LLVMGetParam(func
, SI_SGPR_SAMPLERS
);
5808 ptr
[1] = LLVMGetParam(func
, SI_SGPR_SAMPLERS
+1);
5809 views
= lp_build_gather_values(gallivm
, ptr
, 2);
5810 views
= LLVMBuildBitCast(gallivm
->builder
, views
, ctx
.i64
, "");
5811 views
= LLVMBuildIntToPtr(gallivm
->builder
, views
,
5812 const_array(ctx
.v8i32
, SI_NUM_SAMPLERS
), "");
5814 si_llvm_emit_polygon_stipple(&ctx
, views
, pos
);
5817 /* Interpolate colors. */
5818 for (i
= 0; i
< 2; i
++) {
5819 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
5820 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
5821 key
->ps_prolog
.face_vgpr_index
;
5822 LLVMValueRef interp
[2], color
[4];
5823 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
5828 /* If the interpolation qualifier is not CONSTANT (-1). */
5829 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
5830 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
5831 key
->ps_prolog
.color_interp_vgpr_index
[i
];
5833 interp
[0] = LLVMGetParam(func
, interp_vgpr
);
5834 interp
[1] = LLVMGetParam(func
, interp_vgpr
+ 1);
5835 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
5836 interp_ij
= LLVMBuildBitCast(gallivm
->builder
, interp_ij
,
5840 /* Use the absolute location of the input. */
5841 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
5843 if (key
->ps_prolog
.states
.color_two_side
) {
5844 face
= LLVMGetParam(func
, face_vgpr
);
5845 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
.i32
, "");
5848 interp_fs_input(&ctx
,
5849 key
->ps_prolog
.color_attr_index
[i
],
5850 TGSI_SEMANTIC_COLOR
, i
,
5851 key
->ps_prolog
.num_interp_inputs
,
5852 key
->ps_prolog
.colors_read
, interp_ij
,
5853 prim_mask
, face
, color
);
5856 unsigned chan
= u_bit_scan(&writemask
);
5857 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
5862 /* Force per-sample interpolation. */
5863 if (key
->ps_prolog
.states
.force_persample_interp
) {
5864 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
5865 LLVMValueRef persp_sample
[2], linear_sample
[2];
5867 /* Read PERSP_SAMPLE. */
5868 for (i
= 0; i
< 2; i
++)
5869 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
5870 /* Overwrite PERSP_CENTER. */
5871 for (i
= 0; i
< 2; i
++)
5872 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
5873 persp_sample
[i
], base
+ 2 + i
, "");
5874 /* Overwrite PERSP_CENTROID. */
5875 for (i
= 0; i
< 2; i
++)
5876 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
5877 persp_sample
[i
], base
+ 4 + i
, "");
5878 /* Read LINEAR_SAMPLE. */
5879 for (i
= 0; i
< 2; i
++)
5880 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
5881 /* Overwrite LINEAR_CENTER. */
5882 for (i
= 0; i
< 2; i
++)
5883 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
5884 linear_sample
[i
], base
+ 8 + i
, "");
5885 /* Overwrite LINEAR_CENTROID. */
5886 for (i
= 0; i
< 2; i
++)
5887 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
5888 linear_sample
[i
], base
+ 10 + i
, "");
5892 LLVMBuildRet(gallivm
->builder
, ret
);
5893 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5895 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5896 gallivm
->module
, debug
, ctx
.type
,
5897 "Fragment Shader Prolog"))
5900 radeon_llvm_dispose(&ctx
.radeon_bld
);
5905 * Compile the pixel shader epilog. This handles everything that must be
5906 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
5908 static bool si_compile_ps_epilog(struct si_screen
*sscreen
,
5909 LLVMTargetMachineRef tm
,
5910 struct pipe_debug_callback
*debug
,
5911 struct si_shader_part
*out
)
5913 union si_shader_part_key
*key
= &out
->key
;
5914 struct si_shader shader
= {};
5915 struct si_shader_context ctx
;
5916 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5917 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5918 LLVMTypeRef params
[16+8*4+3];
5919 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5920 int last_array_pointer
, last_sgpr
, num_params
, i
;
5923 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
5924 ctx
.type
= TGSI_PROCESSOR_FRAGMENT
;
5925 shader
.key
.ps
.epilog
= key
->ps_epilog
.states
;
5927 /* Declare input SGPRs. */
5928 params
[SI_PARAM_RW_BUFFERS
] = ctx
.i64
;
5929 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
5930 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
5931 params
[SI_PARAM_IMAGES
] = ctx
.i64
;
5932 params
[SI_PARAM_ALPHA_REF
] = ctx
.f32
;
5933 last_array_pointer
= -1;
5934 last_sgpr
= SI_PARAM_ALPHA_REF
;
5936 /* Declare input VGPRs. */
5937 num_params
= (last_sgpr
+ 1) +
5938 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
5939 key
->ps_epilog
.writes_z
+
5940 key
->ps_epilog
.writes_stencil
+
5941 key
->ps_epilog
.writes_samplemask
;
5943 num_params
= MAX2(num_params
,
5944 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5946 assert(num_params
<= ARRAY_SIZE(params
));
5948 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
5949 params
[i
] = ctx
.f32
;
5951 /* Create the function. */
5952 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5953 last_array_pointer
, last_sgpr
);
5954 /* Disable elimination of unused inputs. */
5955 radeon_llvm_add_attribute(ctx
.radeon_bld
.main_fn
,
5956 "InitialPSInputAddr", 0xffffff);
5958 /* Process colors. */
5959 unsigned vgpr
= last_sgpr
+ 1;
5960 unsigned colors_written
= key
->ps_epilog
.colors_written
;
5961 int last_color_export
= -1;
5963 /* Find the last color export. */
5964 if (!key
->ps_epilog
.writes_z
&&
5965 !key
->ps_epilog
.writes_stencil
&&
5966 !key
->ps_epilog
.writes_samplemask
) {
5967 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
5969 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
5970 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
5971 /* Just set this if any of the colorbuffers are enabled. */
5973 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
5974 last_color_export
= 0;
5976 for (i
= 0; i
< 8; i
++)
5977 if (colors_written
& (1 << i
) &&
5978 (spi_format
>> (i
* 4)) & 0xf)
5979 last_color_export
= i
;
5983 while (colors_written
) {
5984 LLVMValueRef color
[4];
5985 int mrt
= u_bit_scan(&colors_written
);
5987 for (i
= 0; i
< 4; i
++)
5988 color
[i
] = LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5990 si_export_mrt_color(bld_base
, color
, mrt
,
5992 mrt
== last_color_export
);
5995 /* Process depth, stencil, samplemask. */
5996 if (key
->ps_epilog
.writes_z
)
5997 depth
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5998 if (key
->ps_epilog
.writes_stencil
)
5999 stencil
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6000 if (key
->ps_epilog
.writes_samplemask
)
6001 samplemask
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
6003 if (depth
|| stencil
|| samplemask
)
6004 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
6005 else if (last_color_export
== -1)
6006 si_export_null(bld_base
);
6009 LLVMBuildRetVoid(gallivm
->builder
);
6010 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
6012 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
6013 gallivm
->module
, debug
, ctx
.type
,
6014 "Fragment Shader Epilog"))
6017 radeon_llvm_dispose(&ctx
.radeon_bld
);
6022 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
6024 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
6025 LLVMTargetMachineRef tm
,
6026 struct si_shader
*shader
,
6027 struct pipe_debug_callback
*debug
)
6029 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6030 union si_shader_part_key prolog_key
;
6031 union si_shader_part_key epilog_key
;
6034 /* Get the prolog. */
6035 memset(&prolog_key
, 0, sizeof(prolog_key
));
6036 prolog_key
.ps_prolog
.states
= shader
->key
.ps
.prolog
;
6037 prolog_key
.ps_prolog
.colors_read
= info
->colors_read
;
6038 prolog_key
.ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6039 prolog_key
.ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6041 if (info
->colors_read
) {
6042 unsigned *color
= shader
->selector
->color_attr_index
;
6044 if (shader
->key
.ps
.prolog
.color_two_side
) {
6045 /* BCOLORs are stored after the last input. */
6046 prolog_key
.ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6047 prolog_key
.ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6048 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6051 for (i
= 0; i
< 2; i
++) {
6052 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6054 if (!(info
->colors_read
& (0xf << i
*4)))
6057 prolog_key
.ps_prolog
.color_attr_index
[i
] = color
[i
];
6059 /* Force per-sample interpolation for the colors here. */
6060 if (shader
->key
.ps
.prolog
.force_persample_interp
)
6061 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6063 switch (info
->input_interpolate
[color
[i
]]) {
6064 case TGSI_INTERPOLATE_CONSTANT
:
6065 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6067 case TGSI_INTERPOLATE_PERSPECTIVE
:
6068 case TGSI_INTERPOLATE_COLOR
:
6070 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6071 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6072 shader
->config
.spi_ps_input_ena
|=
6073 S_0286CC_PERSP_SAMPLE_ENA(1);
6075 case TGSI_INTERPOLATE_LOC_CENTER
:
6076 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6077 shader
->config
.spi_ps_input_ena
|=
6078 S_0286CC_PERSP_CENTER_ENA(1);
6080 case TGSI_INTERPOLATE_LOC_CENTROID
:
6081 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6082 shader
->config
.spi_ps_input_ena
|=
6083 S_0286CC_PERSP_CENTROID_ENA(1);
6089 case TGSI_INTERPOLATE_LINEAR
:
6091 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6092 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 6;
6093 shader
->config
.spi_ps_input_ena
|=
6094 S_0286CC_LINEAR_SAMPLE_ENA(1);
6096 case TGSI_INTERPOLATE_LOC_CENTER
:
6097 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 8;
6098 shader
->config
.spi_ps_input_ena
|=
6099 S_0286CC_LINEAR_CENTER_ENA(1);
6101 case TGSI_INTERPOLATE_LOC_CENTROID
:
6102 prolog_key
.ps_prolog
.color_interp_vgpr_index
[i
] = 10;
6103 shader
->config
.spi_ps_input_ena
|=
6104 S_0286CC_LINEAR_CENTROID_ENA(1);
6116 /* The prolog is a no-op if these aren't set. */
6117 if (prolog_key
.ps_prolog
.colors_read
||
6118 prolog_key
.ps_prolog
.states
.force_persample_interp
||
6119 prolog_key
.ps_prolog
.states
.poly_stipple
) {
6121 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
6122 &prolog_key
, tm
, debug
,
6123 si_compile_ps_prolog
);
6124 if (!shader
->prolog
)
6128 /* Get the epilog. */
6129 memset(&epilog_key
, 0, sizeof(epilog_key
));
6130 epilog_key
.ps_epilog
.colors_written
= info
->colors_written
;
6131 epilog_key
.ps_epilog
.writes_z
= info
->writes_z
;
6132 epilog_key
.ps_epilog
.writes_stencil
= info
->writes_stencil
;
6133 epilog_key
.ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6134 epilog_key
.ps_epilog
.states
= shader
->key
.ps
.epilog
;
6137 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
6138 &epilog_key
, tm
, debug
,
6139 si_compile_ps_epilog
);
6140 if (!shader
->epilog
)
6143 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
6144 if (shader
->key
.ps
.prolog
.poly_stipple
) {
6145 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
6146 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
6149 /* Set up the enable bits for per-sample shading if needed. */
6150 if (shader
->key
.ps
.prolog
.force_persample_interp
) {
6151 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
6152 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
)) {
6153 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
6154 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
6155 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
6157 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
6158 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
)) {
6159 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
6160 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
6161 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
6165 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
6166 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
6167 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
6168 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
6169 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
6172 /* At least one pair of interpolation weights must be enabled. */
6173 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
6174 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
6175 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
6178 /* The sample mask input is always enabled, because the API shader always
6179 * passes it through to the epilog. Disable it here if it's unused.
6181 if (!shader
->key
.ps
.epilog
.poly_line_smoothing
&&
6182 !shader
->selector
->info
.reads_samplemask
)
6183 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
6188 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
6189 struct si_shader
*shader
,
6190 struct pipe_debug_callback
*debug
)
6192 struct si_shader
*mainp
= shader
->selector
->main_shader_part
;
6195 /* LS, ES, VS are compiled on demand if the main part hasn't been
6196 * compiled for that stage.
6199 (shader
->selector
->type
== PIPE_SHADER_VERTEX
&&
6200 (shader
->key
.vs
.as_es
!= mainp
->key
.vs
.as_es
||
6201 shader
->key
.vs
.as_ls
!= mainp
->key
.vs
.as_ls
)) ||
6202 (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
&&
6203 shader
->key
.tes
.as_es
!= mainp
->key
.tes
.as_es
)) {
6204 /* Monolithic shader (compiled as a whole, has many variants,
6205 * may take a long time to compile).
6207 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
6211 /* The shader consists of 2-3 parts:
6213 * - the middle part is the user shader, it has 1 variant only
6214 * and it was compiled during the creation of the shader
6216 * - the prolog part is inserted at the beginning
6217 * - the epilog part is inserted at the end
6219 * The prolog and epilog have many (but simple) variants.
6222 /* Copy the compiled TGSI shader data over. */
6223 shader
->is_binary_shared
= true;
6224 shader
->binary
= mainp
->binary
;
6225 shader
->config
= mainp
->config
;
6226 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
6227 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
6228 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
6229 memcpy(shader
->info
.vs_output_param_offset
,
6230 mainp
->info
.vs_output_param_offset
,
6231 sizeof(mainp
->info
.vs_output_param_offset
));
6232 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
6233 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
6234 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
6236 /* Select prologs and/or epilogs. */
6237 switch (shader
->selector
->type
) {
6238 case PIPE_SHADER_VERTEX
:
6239 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
6242 case PIPE_SHADER_TESS_CTRL
:
6243 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
6246 case PIPE_SHADER_TESS_EVAL
:
6247 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
6250 case PIPE_SHADER_FRAGMENT
:
6251 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
6254 /* Make sure we have at least as many VGPRs as there
6255 * are allocated inputs.
6257 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6258 shader
->info
.num_input_vgprs
);
6262 /* Update SGPR and VGPR counts. */
6263 if (shader
->prolog
) {
6264 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
6265 shader
->prolog
->config
.num_sgprs
);
6266 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6267 shader
->prolog
->config
.num_vgprs
);
6269 if (shader
->epilog
) {
6270 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
6271 shader
->epilog
->config
.num_sgprs
);
6272 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
6273 shader
->epilog
->config
.num_vgprs
);
6277 si_shader_dump(sscreen
, shader
, debug
, shader
->selector
->info
.processor
,
6281 r
= si_shader_binary_upload(sscreen
, shader
);
6283 fprintf(stderr
, "LLVM failed to upload shader\n");
6290 void si_shader_destroy(struct si_shader
*shader
)
6292 if (shader
->gs_copy_shader
) {
6293 si_shader_destroy(shader
->gs_copy_shader
);
6294 FREE(shader
->gs_copy_shader
);
6297 if (shader
->scratch_bo
)
6298 r600_resource_reference(&shader
->scratch_bo
, NULL
);
6300 r600_resource_reference(&shader
->bo
, NULL
);
6302 if (!shader
->is_binary_shared
)
6303 radeon_shader_binary_clean(&shader
->binary
);