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 so_buffers
[4];
103 LLVMValueRef esgs_ring
;
104 LLVMValueRef gsvs_ring
[4];
105 LLVMValueRef gs_next_vertex
[4];
106 LLVMValueRef return_value
;
122 static struct si_shader_context
*si_shader_context(
123 struct lp_build_tgsi_context
*bld_base
)
125 return (struct si_shader_context
*)bld_base
;
128 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
129 struct si_screen
*sscreen
,
130 struct si_shader
*shader
,
131 LLVMTargetMachineRef tm
,
132 struct tgsi_shader_info
*info
);
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
,
219 value
= LLVMBuildLShr(gallivm
->builder
, value
,
220 lp_build_const_int32(gallivm
, rshift
), "");
222 if (rshift
+ bitwidth
< 32) {
223 unsigned mask
= (1 << bitwidth
) - 1;
224 value
= LLVMBuildAnd(gallivm
->builder
, value
,
225 lp_build_const_int32(gallivm
, mask
), "");
231 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
234 case TGSI_PROCESSOR_TESS_CTRL
:
235 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
237 case TGSI_PROCESSOR_TESS_EVAL
:
238 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
239 ctx
->param_tes_rel_patch_id
);
247 /* Tessellation shaders pass outputs to the next shader using LDS.
249 * LS outputs = TCS inputs
250 * TCS outputs = TES inputs
253 * - TCS inputs for patch 0
254 * - TCS inputs for patch 1
255 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
257 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
258 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
259 * - TCS outputs for patch 1
260 * - Per-patch TCS outputs for patch 1
261 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
262 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
265 * All three shaders VS(LS), TCS, TES share the same LDS space.
269 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
271 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
)
272 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
273 else if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
274 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
282 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
284 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
288 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
290 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
292 SI_PARAM_TCS_OUT_OFFSETS
,
298 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
300 return lp_build_mul_imm(&ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
302 SI_PARAM_TCS_OUT_OFFSETS
,
308 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
310 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
311 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
312 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
314 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
318 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
320 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
321 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
322 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
323 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
325 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
326 LLVMBuildMul(gallivm
->builder
, patch_stride
,
332 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
334 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
335 LLVMValueRef patch0_patch_data_offset
=
336 get_tcs_out_patch0_patch_data_offset(ctx
);
337 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
338 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
340 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
341 LLVMBuildMul(gallivm
->builder
, patch_stride
,
346 static void build_indexed_store(struct si_shader_context
*ctx
,
347 LLVMValueRef base_ptr
, LLVMValueRef index
,
350 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
351 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
352 LLVMValueRef indices
[2], pointer
;
354 indices
[0] = bld_base
->uint_bld
.zero
;
357 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
358 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
362 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
363 * It's equivalent to doing a load from &base_ptr[index].
365 * \param base_ptr Where the array starts.
366 * \param index The element index into the array.
368 static LLVMValueRef
build_indexed_load(struct si_shader_context
*ctx
,
369 LLVMValueRef base_ptr
, LLVMValueRef index
)
371 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
372 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
373 LLVMValueRef indices
[2], pointer
;
375 indices
[0] = bld_base
->uint_bld
.zero
;
378 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
379 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
383 * Do a load from &base_ptr[index], but also add a flag that it's loading
386 static LLVMValueRef
build_indexed_load_const(
387 struct si_shader_context
*ctx
,
388 LLVMValueRef base_ptr
, LLVMValueRef index
)
390 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
);
391 LLVMSetMetadata(result
, 1, ctx
->const_md
);
395 static LLVMValueRef
get_instance_index_for_fetch(
396 struct radeon_llvm_context
*radeon_bld
,
397 unsigned param_start_instance
, unsigned divisor
)
399 struct si_shader_context
*ctx
=
400 si_shader_context(&radeon_bld
->soa
.bld_base
);
401 struct gallivm_state
*gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
403 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
404 ctx
->param_instance_id
);
406 /* The division must be done before START_INSTANCE is added. */
408 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
409 lp_build_const_int32(gallivm
, divisor
), "");
411 return LLVMBuildAdd(gallivm
->builder
, result
,
412 LLVMGetParam(radeon_bld
->main_fn
, param_start_instance
), "");
415 static void declare_input_vs(
416 struct radeon_llvm_context
*radeon_bld
,
417 unsigned input_index
,
418 const struct tgsi_full_declaration
*decl
)
420 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
421 struct gallivm_state
*gallivm
= base
->gallivm
;
422 struct si_shader_context
*ctx
=
423 si_shader_context(&radeon_bld
->soa
.bld_base
);
425 ctx
->shader
->key
.vs
.prolog
.instance_divisors
[input_index
];
429 LLVMValueRef t_list_ptr
;
430 LLVMValueRef t_offset
;
432 LLVMValueRef attribute_offset
;
433 LLVMValueRef buffer_index
;
434 LLVMValueRef args
[3];
437 /* Load the T list */
438 t_list_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFERS
);
440 t_offset
= lp_build_const_int32(gallivm
, input_index
);
442 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
444 /* Build the attribute offset */
445 attribute_offset
= lp_build_const_int32(gallivm
, 0);
447 if (!ctx
->is_monolithic
) {
448 buffer_index
= LLVMGetParam(radeon_bld
->main_fn
,
449 ctx
->param_vertex_index0
+
451 } else if (divisor
) {
452 /* Build index from instance ID, start instance and divisor */
453 ctx
->shader
->uses_instanceid
= true;
454 buffer_index
= get_instance_index_for_fetch(&ctx
->radeon_bld
,
455 SI_PARAM_START_INSTANCE
,
458 /* Load the buffer index for vertices. */
459 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
460 ctx
->param_vertex_id
);
461 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
462 SI_PARAM_BASE_VERTEX
);
463 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
467 args
[1] = attribute_offset
;
468 args
[2] = buffer_index
;
469 input
= lp_build_intrinsic(gallivm
->builder
,
470 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
471 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
473 /* Break up the vec4 into individual components */
474 for (chan
= 0; chan
< 4; chan
++) {
475 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
476 /* XXX: Use a helper function for this. There is one in
478 ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
479 LLVMBuildExtractElement(gallivm
->builder
,
480 input
, llvm_chan
, "");
484 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
487 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
490 return bld_base
->uint_bld
.zero
;
493 case TGSI_PROCESSOR_VERTEX
:
494 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
495 ctx
->param_vs_prim_id
);
496 case TGSI_PROCESSOR_TESS_CTRL
:
497 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
499 case TGSI_PROCESSOR_TESS_EVAL
:
500 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
501 ctx
->param_tes_patch_id
);
502 case TGSI_PROCESSOR_GEOMETRY
:
503 return LLVMGetParam(ctx
->radeon_bld
.main_fn
,
504 SI_PARAM_PRIMITIVE_ID
);
507 return bld_base
->uint_bld
.zero
;
512 * Return the value of tgsi_ind_register for indexing.
513 * This is the indirect index with the constant offset added to it.
515 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
516 const struct tgsi_ind_register
*ind
,
519 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
522 result
= ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
523 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
524 result
= LLVMBuildAdd(gallivm
->builder
, result
,
525 lp_build_const_int32(gallivm
, rel_index
), "");
530 * Calculate a dword address given an input or output register and a stride.
532 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
533 const struct tgsi_full_dst_register
*dst
,
534 const struct tgsi_full_src_register
*src
,
535 LLVMValueRef vertex_dw_stride
,
536 LLVMValueRef base_addr
)
538 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
539 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
540 ubyte
*name
, *index
, *array_first
;
542 struct tgsi_full_dst_register reg
;
544 /* Set the register description. The address computation is the same
545 * for sources and destinations. */
547 reg
.Register
.File
= src
->Register
.File
;
548 reg
.Register
.Index
= src
->Register
.Index
;
549 reg
.Register
.Indirect
= src
->Register
.Indirect
;
550 reg
.Register
.Dimension
= src
->Register
.Dimension
;
551 reg
.Indirect
= src
->Indirect
;
552 reg
.Dimension
= src
->Dimension
;
553 reg
.DimIndirect
= src
->DimIndirect
;
557 /* If the register is 2-dimensional (e.g. an array of vertices
558 * in a primitive), calculate the base address of the vertex. */
559 if (reg
.Register
.Dimension
) {
562 if (reg
.Dimension
.Indirect
)
563 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
564 reg
.Dimension
.Index
);
566 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
568 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
569 LLVMBuildMul(gallivm
->builder
, index
,
570 vertex_dw_stride
, ""), "");
573 /* Get information about the register. */
574 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
575 name
= info
->input_semantic_name
;
576 index
= info
->input_semantic_index
;
577 array_first
= info
->input_array_first
;
578 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
579 name
= info
->output_semantic_name
;
580 index
= info
->output_semantic_index
;
581 array_first
= info
->output_array_first
;
587 if (reg
.Register
.Indirect
) {
588 /* Add the relative address of the element. */
589 LLVMValueRef ind_index
;
591 if (reg
.Indirect
.ArrayID
)
592 first
= array_first
[reg
.Indirect
.ArrayID
];
594 first
= reg
.Register
.Index
;
596 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
597 reg
.Register
.Index
- first
);
599 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
600 LLVMBuildMul(gallivm
->builder
, ind_index
,
601 lp_build_const_int32(gallivm
, 4), ""), "");
603 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
605 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
606 index
[reg
.Register
.Index
]);
609 /* Add the base address of the element. */
610 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
611 lp_build_const_int32(gallivm
, param
* 4), "");
617 * \param type output value type
618 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
619 * \param dw_addr address in dwords
621 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
622 enum tgsi_opcode_type type
, unsigned swizzle
,
623 LLVMValueRef dw_addr
)
625 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
626 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
630 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
632 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
633 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
635 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
639 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
640 lp_build_const_int32(gallivm
, swizzle
));
642 value
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
643 if (type
== TGSI_TYPE_DOUBLE
) {
645 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
646 lp_build_const_int32(gallivm
, swizzle
+ 1));
647 value2
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
);
648 return radeon_llvm_emit_fetch_double(bld_base
, value
, value2
);
651 return LLVMBuildBitCast(gallivm
->builder
, value
,
652 tgsi2llvmtype(bld_base
, type
), "");
658 * \param swizzle offset (typically 0..3)
659 * \param dw_addr address in dwords
660 * \param value value to store
662 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
663 unsigned swizzle
, LLVMValueRef dw_addr
,
666 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
667 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
669 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
670 lp_build_const_int32(gallivm
, swizzle
));
672 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
673 build_indexed_store(ctx
, ctx
->lds
,
677 static LLVMValueRef
fetch_input_tcs(
678 struct lp_build_tgsi_context
*bld_base
,
679 const struct tgsi_full_src_register
*reg
,
680 enum tgsi_opcode_type type
, unsigned swizzle
)
682 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
683 LLVMValueRef dw_addr
, stride
;
685 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
686 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
687 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
689 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
692 static LLVMValueRef
fetch_output_tcs(
693 struct lp_build_tgsi_context
*bld_base
,
694 const struct tgsi_full_src_register
*reg
,
695 enum tgsi_opcode_type type
, unsigned swizzle
)
697 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
698 LLVMValueRef dw_addr
, stride
;
700 if (reg
->Register
.Dimension
) {
701 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
702 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
703 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
705 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
706 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
709 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
712 static LLVMValueRef
fetch_input_tes(
713 struct lp_build_tgsi_context
*bld_base
,
714 const struct tgsi_full_src_register
*reg
,
715 enum tgsi_opcode_type type
, unsigned swizzle
)
717 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
718 LLVMValueRef dw_addr
, stride
;
720 if (reg
->Register
.Dimension
) {
721 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
722 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
723 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
725 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
726 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
729 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
732 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
733 const struct tgsi_full_instruction
*inst
,
734 const struct tgsi_opcode_info
*info
,
737 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
738 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
740 LLVMValueRef dw_addr
, stride
;
742 /* Only handle per-patch and per-vertex outputs here.
743 * Vectors will be lowered to scalars and this function will be called again.
745 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
746 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
747 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
751 if (reg
->Register
.Dimension
) {
752 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
753 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
754 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
756 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
757 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
760 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
761 LLVMValueRef value
= dst
[chan_index
];
763 if (inst
->Instruction
.Saturate
)
764 value
= radeon_llvm_saturate(bld_base
, value
);
766 lds_store(bld_base
, chan_index
, dw_addr
, value
);
770 static LLVMValueRef
fetch_input_gs(
771 struct lp_build_tgsi_context
*bld_base
,
772 const struct tgsi_full_src_register
*reg
,
773 enum tgsi_opcode_type type
,
776 struct lp_build_context
*base
= &bld_base
->base
;
777 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
778 struct si_shader
*shader
= ctx
->shader
;
779 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
780 struct gallivm_state
*gallivm
= base
->gallivm
;
781 LLVMValueRef vtx_offset
;
782 LLVMValueRef args
[9];
783 unsigned vtx_offset_param
;
784 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
785 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
786 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
790 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
791 return get_primitive_id(bld_base
, swizzle
);
793 if (!reg
->Register
.Dimension
)
797 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
799 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
800 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
802 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
806 /* Get the vertex offset parameter */
807 vtx_offset_param
= reg
->Dimension
.Index
;
808 if (vtx_offset_param
< 2) {
809 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
811 assert(vtx_offset_param
< 6);
812 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
814 vtx_offset
= lp_build_mul_imm(uint
,
815 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
819 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
820 args
[0] = ctx
->esgs_ring
;
821 args
[1] = vtx_offset
;
822 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
823 args
[3] = uint
->zero
;
824 args
[4] = uint
->one
; /* OFFEN */
825 args
[5] = uint
->zero
; /* IDXEN */
826 args
[6] = uint
->one
; /* GLC */
827 args
[7] = uint
->zero
; /* SLC */
828 args
[8] = uint
->zero
; /* TFE */
830 value
= lp_build_intrinsic(gallivm
->builder
,
831 "llvm.SI.buffer.load.dword.i32.i32",
833 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
834 if (type
== TGSI_TYPE_DOUBLE
) {
836 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
837 value2
= lp_build_intrinsic(gallivm
->builder
,
838 "llvm.SI.buffer.load.dword.i32.i32",
840 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
841 return radeon_llvm_emit_fetch_double(bld_base
,
844 return LLVMBuildBitCast(gallivm
->builder
,
846 tgsi2llvmtype(bld_base
, type
), "");
849 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
851 switch (interpolate
) {
852 case TGSI_INTERPOLATE_CONSTANT
:
855 case TGSI_INTERPOLATE_LINEAR
:
856 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
857 return SI_PARAM_LINEAR_SAMPLE
;
858 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
859 return SI_PARAM_LINEAR_CENTROID
;
861 return SI_PARAM_LINEAR_CENTER
;
863 case TGSI_INTERPOLATE_COLOR
:
864 case TGSI_INTERPOLATE_PERSPECTIVE
:
865 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
866 return SI_PARAM_PERSP_SAMPLE
;
867 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
868 return SI_PARAM_PERSP_CENTROID
;
870 return SI_PARAM_PERSP_CENTER
;
873 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
878 /* This shouldn't be used by explicit INTERP opcodes. */
879 static unsigned select_interp_param(struct si_shader_context
*ctx
,
882 if (!ctx
->shader
->key
.ps
.prolog
.force_persample_interp
)
885 /* If the shader doesn't use center/centroid, just return the parameter.
887 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
888 * switch between center/centroid and sample without shader changes.
891 case SI_PARAM_PERSP_CENTROID
:
892 case SI_PARAM_PERSP_CENTER
:
893 return SI_PARAM_PERSP_SAMPLE
;
895 case SI_PARAM_LINEAR_CENTROID
:
896 case SI_PARAM_LINEAR_CENTER
:
897 return SI_PARAM_LINEAR_SAMPLE
;
905 * Interpolate a fragment shader input.
908 * @param input_index index of the input in hardware
909 * @param semantic_name TGSI_SEMANTIC_*
910 * @param semantic_index semantic index
911 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
912 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
913 * @param interp_param interpolation weights (i,j)
914 * @param prim_mask SI_PARAM_PRIM_MASK
915 * @param face SI_PARAM_FRONT_FACE
916 * @param result the return value (4 components)
918 static void interp_fs_input(struct si_shader_context
*ctx
,
919 unsigned input_index
,
920 unsigned semantic_name
,
921 unsigned semantic_index
,
922 unsigned num_interp_inputs
,
923 unsigned colors_read_mask
,
924 LLVMValueRef interp_param
,
925 LLVMValueRef prim_mask
,
927 LLVMValueRef result
[4])
929 struct lp_build_context
*base
= &ctx
->radeon_bld
.soa
.bld_base
.base
;
930 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
931 struct gallivm_state
*gallivm
= base
->gallivm
;
932 const char *intr_name
;
933 LLVMValueRef attr_number
;
937 attr_number
= lp_build_const_int32(gallivm
, input_index
);
939 /* fs.constant returns the param from the middle vertex, so it's not
940 * really useful for flat shading. It's meant to be used for custom
941 * interpolation (but the intrinsic can't fetch from the other two
944 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
945 * to do the right thing. The only reason we use fs.constant is that
946 * fs.interp cannot be used on integers, because they can be equal
949 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
951 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
952 ctx
->shader
->key
.ps
.prolog
.color_two_side
) {
953 LLVMValueRef args
[4];
954 LLVMValueRef is_face_positive
;
955 LLVMValueRef back_attr_number
;
957 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
958 * otherwise it's at offset "num_inputs".
960 unsigned back_attr_offset
= num_interp_inputs
;
961 if (semantic_index
== 1 && colors_read_mask
& 0xf)
962 back_attr_offset
+= 1;
964 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
966 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
967 face
, uint
->zero
, "");
970 args
[3] = interp_param
;
971 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
972 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
973 LLVMValueRef front
, back
;
976 args
[1] = attr_number
;
977 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
978 ctx
->f32
, args
, args
[3] ? 4 : 3,
979 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
981 args
[1] = back_attr_number
;
982 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
983 ctx
->f32
, args
, args
[3] ? 4 : 3,
984 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
986 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
992 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
993 LLVMValueRef args
[4];
995 args
[0] = uint
->zero
;
996 args
[1] = attr_number
;
998 args
[3] = interp_param
;
999 result
[0] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1000 ctx
->f32
, args
, args
[3] ? 4 : 3,
1001 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1003 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1004 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1006 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1007 LLVMValueRef args
[4];
1008 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1010 args
[0] = llvm_chan
;
1011 args
[1] = attr_number
;
1012 args
[2] = prim_mask
;
1013 args
[3] = interp_param
;
1014 result
[chan
] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
1015 ctx
->f32
, args
, args
[3] ? 4 : 3,
1016 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1021 static void declare_input_fs(
1022 struct radeon_llvm_context
*radeon_bld
,
1023 unsigned input_index
,
1024 const struct tgsi_full_declaration
*decl
)
1026 struct si_shader_context
*ctx
=
1027 si_shader_context(&radeon_bld
->soa
.bld_base
);
1028 struct si_shader
*shader
= ctx
->shader
;
1029 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
1030 LLVMValueRef interp_param
= NULL
;
1031 int interp_param_idx
;
1033 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1034 decl
->Interp
.Location
);
1035 if (interp_param_idx
== -1)
1037 else if (interp_param_idx
) {
1038 interp_param_idx
= select_interp_param(ctx
,
1040 interp_param
= LLVMGetParam(main_fn
, interp_param_idx
);
1043 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1044 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1045 shader
->selector
->info
.colors_read
, interp_param
,
1046 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1047 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1048 &radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)]);
1051 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1053 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1054 SI_PARAM_ANCILLARY
, 8, 4);
1058 * Load a dword from a constant buffer.
1060 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1061 LLVMValueRef offset
, LLVMTypeRef return_type
)
1063 LLVMValueRef args
[2] = {resource
, offset
};
1065 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1066 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1069 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1071 struct si_shader_context
*ctx
=
1072 si_shader_context(&radeon_bld
->soa
.bld_base
);
1073 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1074 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1075 LLVMBuilderRef builder
= gallivm
->builder
;
1076 LLVMValueRef desc
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1077 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1078 LLVMValueRef resource
= build_indexed_load_const(ctx
, desc
, buf_index
);
1080 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1081 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1082 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1084 LLVMValueRef pos
[4] = {
1085 buffer_load_const(builder
, resource
, offset0
, ctx
->f32
),
1086 buffer_load_const(builder
, resource
, offset1
, ctx
->f32
),
1087 lp_build_const_float(gallivm
, 0),
1088 lp_build_const_float(gallivm
, 0)
1091 return lp_build_gather_values(gallivm
, pos
, 4);
1094 static void declare_system_value(
1095 struct radeon_llvm_context
*radeon_bld
,
1097 const struct tgsi_full_declaration
*decl
)
1099 struct si_shader_context
*ctx
=
1100 si_shader_context(&radeon_bld
->soa
.bld_base
);
1101 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1102 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1103 LLVMValueRef value
= 0;
1105 switch (decl
->Semantic
.Name
) {
1106 case TGSI_SEMANTIC_INSTANCEID
:
1107 value
= LLVMGetParam(radeon_bld
->main_fn
,
1108 ctx
->param_instance_id
);
1111 case TGSI_SEMANTIC_VERTEXID
:
1112 value
= LLVMBuildAdd(gallivm
->builder
,
1113 LLVMGetParam(radeon_bld
->main_fn
,
1114 ctx
->param_vertex_id
),
1115 LLVMGetParam(radeon_bld
->main_fn
,
1116 SI_PARAM_BASE_VERTEX
), "");
1119 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1120 value
= LLVMGetParam(radeon_bld
->main_fn
,
1121 ctx
->param_vertex_id
);
1124 case TGSI_SEMANTIC_BASEVERTEX
:
1125 value
= LLVMGetParam(radeon_bld
->main_fn
,
1126 SI_PARAM_BASE_VERTEX
);
1129 case TGSI_SEMANTIC_INVOCATIONID
:
1130 if (ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1131 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1132 else if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1133 value
= LLVMGetParam(radeon_bld
->main_fn
,
1134 SI_PARAM_GS_INSTANCE_ID
);
1136 assert(!"INVOCATIONID not implemented");
1139 case TGSI_SEMANTIC_POSITION
:
1141 LLVMValueRef pos
[4] = {
1142 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1143 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1144 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1145 lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
, TGSI_OPCODE_RCP
,
1146 LLVMGetParam(radeon_bld
->main_fn
,
1147 SI_PARAM_POS_W_FLOAT
)),
1149 value
= lp_build_gather_values(gallivm
, pos
, 4);
1153 case TGSI_SEMANTIC_FACE
:
1154 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1157 case TGSI_SEMANTIC_SAMPLEID
:
1158 value
= get_sample_id(radeon_bld
);
1161 case TGSI_SEMANTIC_SAMPLEPOS
: {
1162 LLVMValueRef pos
[4] = {
1163 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1164 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1165 lp_build_const_float(gallivm
, 0),
1166 lp_build_const_float(gallivm
, 0)
1168 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1169 TGSI_OPCODE_FRC
, pos
[0]);
1170 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1171 TGSI_OPCODE_FRC
, pos
[1]);
1172 value
= lp_build_gather_values(gallivm
, pos
, 4);
1176 case TGSI_SEMANTIC_SAMPLEMASK
:
1177 /* This can only occur with the OpenGL Core profile, which
1178 * doesn't support smoothing.
1180 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1183 case TGSI_SEMANTIC_TESSCOORD
:
1185 LLVMValueRef coord
[4] = {
1186 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_u
),
1187 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_v
),
1192 /* For triangles, the vector should be (u, v, 1-u-v). */
1193 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1194 PIPE_PRIM_TRIANGLES
)
1195 coord
[2] = lp_build_sub(bld
, bld
->one
,
1196 lp_build_add(bld
, coord
[0], coord
[1]));
1198 value
= lp_build_gather_values(gallivm
, coord
, 4);
1202 case TGSI_SEMANTIC_VERTICESIN
:
1203 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1206 case TGSI_SEMANTIC_TESSINNER
:
1207 case TGSI_SEMANTIC_TESSOUTER
:
1209 LLVMValueRef dw_addr
;
1210 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1212 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1213 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1214 lp_build_const_int32(gallivm
, param
* 4), "");
1216 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1221 case TGSI_SEMANTIC_PRIMID
:
1222 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1226 assert(!"unknown system value");
1230 radeon_bld
->system_values
[index
] = value
;
1233 static LLVMValueRef
fetch_constant(
1234 struct lp_build_tgsi_context
*bld_base
,
1235 const struct tgsi_full_src_register
*reg
,
1236 enum tgsi_opcode_type type
,
1239 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1240 struct lp_build_context
*base
= &bld_base
->base
;
1241 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1244 LLVMValueRef addr
, bufp
;
1245 LLVMValueRef result
;
1247 if (swizzle
== LP_CHAN_ALL
) {
1249 LLVMValueRef values
[4];
1250 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1251 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1253 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1256 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1257 idx
= reg
->Register
.Index
* 4 + swizzle
;
1259 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1260 if (type
!= TGSI_TYPE_DOUBLE
)
1261 return bitcast(bld_base
, type
, ctx
->constants
[buf
][idx
]);
1263 return radeon_llvm_emit_fetch_double(bld_base
,
1264 ctx
->constants
[buf
][idx
],
1265 ctx
->constants
[buf
][idx
+ 1]);
1269 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1270 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1272 index
= get_indirect_index(ctx
, ®
->DimIndirect
,
1273 reg
->Dimension
.Index
);
1274 bufp
= build_indexed_load_const(ctx
, ptr
, index
);
1276 bufp
= ctx
->const_buffers
[buf
];
1278 addr
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1279 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1280 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1281 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1282 lp_build_const_int32(base
->gallivm
, idx
* 4));
1284 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1287 if (type
!= TGSI_TYPE_DOUBLE
)
1288 result
= bitcast(bld_base
, type
, result
);
1290 LLVMValueRef addr2
, result2
;
1291 addr2
= ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1292 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1293 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1294 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1295 lp_build_const_int32(base
->gallivm
, idx
* 4));
1297 result2
= buffer_load_const(base
->gallivm
->builder
, ctx
->const_buffers
[buf
],
1300 result
= radeon_llvm_emit_fetch_double(bld_base
,
1306 /* Upper 16 bits must be zero. */
1307 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1308 LLVMValueRef val
[2])
1310 return LLVMBuildOr(gallivm
->builder
, val
[0],
1311 LLVMBuildShl(gallivm
->builder
, val
[1],
1312 lp_build_const_int32(gallivm
, 16),
1316 /* Upper 16 bits are ignored and will be dropped. */
1317 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1318 LLVMValueRef val
[2])
1320 LLVMValueRef v
[2] = {
1321 LLVMBuildAnd(gallivm
->builder
, val
[0],
1322 lp_build_const_int32(gallivm
, 0xffff), ""),
1325 return si_llvm_pack_two_int16(gallivm
, v
);
1328 /* Initialize arguments for the shader export intrinsic */
1329 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1330 LLVMValueRef
*values
,
1334 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1335 struct lp_build_context
*uint
=
1336 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1337 struct lp_build_context
*base
= &bld_base
->base
;
1338 struct gallivm_state
*gallivm
= base
->gallivm
;
1339 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1340 LLVMValueRef val
[4];
1341 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1345 /* Default is 0xf. Adjusted below depending on the format. */
1346 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1348 /* Specify whether the EXEC mask represents the valid mask */
1349 args
[1] = uint
->zero
;
1351 /* Specify whether this is the last export */
1352 args
[2] = uint
->zero
;
1354 /* Specify the target we are exporting */
1355 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1357 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1358 const union si_shader_key
*key
= &ctx
->shader
->key
;
1359 unsigned col_formats
= key
->ps
.epilog
.spi_shader_col_format
;
1360 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1362 assert(cbuf
>= 0 && cbuf
< 8);
1363 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1364 is_int8
= (key
->ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1367 args
[4] = uint
->zero
; /* COMPR flag */
1368 args
[5] = base
->undef
;
1369 args
[6] = base
->undef
;
1370 args
[7] = base
->undef
;
1371 args
[8] = base
->undef
;
1373 switch (spi_shader_col_format
) {
1374 case V_028714_SPI_SHADER_ZERO
:
1375 args
[0] = uint
->zero
; /* writemask */
1376 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1379 case V_028714_SPI_SHADER_32_R
:
1380 args
[0] = uint
->one
; /* writemask */
1381 args
[5] = values
[0];
1384 case V_028714_SPI_SHADER_32_GR
:
1385 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1386 args
[5] = values
[0];
1387 args
[6] = values
[1];
1390 case V_028714_SPI_SHADER_32_AR
:
1391 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1392 args
[5] = values
[0];
1393 args
[8] = values
[3];
1396 case V_028714_SPI_SHADER_FP16_ABGR
:
1397 args
[4] = uint
->one
; /* COMPR flag */
1399 for (chan
= 0; chan
< 2; chan
++) {
1400 LLVMValueRef pack_args
[2] = {
1402 values
[2 * chan
+ 1]
1404 LLVMValueRef packed
;
1406 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1408 ctx
->i32
, pack_args
, 2,
1409 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1411 LLVMBuildBitCast(base
->gallivm
->builder
,
1412 packed
, ctx
->f32
, "");
1416 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1417 for (chan
= 0; chan
< 4; chan
++) {
1418 val
[chan
] = radeon_llvm_saturate(bld_base
, values
[chan
]);
1419 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1420 lp_build_const_float(gallivm
, 65535), "");
1421 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1422 lp_build_const_float(gallivm
, 0.5), "");
1423 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1427 args
[4] = uint
->one
; /* COMPR flag */
1428 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1429 si_llvm_pack_two_int16(gallivm
, val
));
1430 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1431 si_llvm_pack_two_int16(gallivm
, val
+2));
1434 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1435 for (chan
= 0; chan
< 4; chan
++) {
1436 /* Clamp between [-1, 1]. */
1437 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1439 lp_build_const_float(gallivm
, 1));
1440 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1442 lp_build_const_float(gallivm
, -1));
1443 /* Convert to a signed integer in [-32767, 32767]. */
1444 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1445 lp_build_const_float(gallivm
, 32767), "");
1446 /* If positive, add 0.5, else add -0.5. */
1447 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1448 LLVMBuildSelect(builder
,
1449 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1450 val
[chan
], base
->zero
, ""),
1451 lp_build_const_float(gallivm
, 0.5),
1452 lp_build_const_float(gallivm
, -0.5), ""), "");
1453 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
1456 args
[4] = uint
->one
; /* COMPR flag */
1457 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1458 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1459 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1460 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1463 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1464 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1467 for (chan
= 0; chan
< 4; chan
++) {
1468 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1469 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1473 args
[4] = uint
->one
; /* COMPR flag */
1474 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1475 si_llvm_pack_two_int16(gallivm
, val
));
1476 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1477 si_llvm_pack_two_int16(gallivm
, val
+2));
1481 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1482 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1484 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
1487 for (chan
= 0; chan
< 4; chan
++) {
1488 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1489 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1492 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1497 args
[4] = uint
->one
; /* COMPR flag */
1498 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1499 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1500 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1501 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1505 case V_028714_SPI_SHADER_32_ABGR
:
1506 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1511 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1514 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1515 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1517 if (ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
1518 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1519 SI_PARAM_ALPHA_REF
);
1521 LLVMValueRef alpha_pass
=
1522 lp_build_cmp(&bld_base
->base
,
1523 ctx
->shader
->key
.ps
.epilog
.alpha_func
,
1526 lp_build_select(&bld_base
->base
,
1528 lp_build_const_float(gallivm
, 1.0f
),
1529 lp_build_const_float(gallivm
, -1.0f
));
1531 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
1532 ctx
->voidt
, &arg
, 1, 0);
1534 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kilp",
1535 ctx
->voidt
, NULL
, 0, 0);
1539 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1541 unsigned samplemask_param
)
1543 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1544 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1545 LLVMValueRef coverage
;
1547 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1548 coverage
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1550 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1552 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1554 &coverage
, 1, LLVMReadNoneAttribute
);
1556 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1559 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1560 lp_build_const_float(gallivm
,
1561 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1563 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1566 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
1567 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1569 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1570 struct lp_build_context
*base
= &bld_base
->base
;
1571 struct lp_build_context
*uint
= &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1574 unsigned const_chan
;
1575 LLVMValueRef base_elt
;
1576 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1577 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1578 LLVMValueRef const_resource
= build_indexed_load_const(ctx
, ptr
, constbuf_index
);
1580 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1581 LLVMValueRef
*args
= pos
[2 + reg_index
];
1586 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1588 /* Compute dot products of position and user clip plane vectors */
1589 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1590 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1591 args
[1] = lp_build_const_int32(base
->gallivm
,
1592 ((reg_index
* 4 + chan
) * 4 +
1594 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1597 lp_build_add(base
, args
[5 + chan
],
1598 lp_build_mul(base
, base_elt
,
1599 out_elts
[const_chan
]));
1603 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1604 args
[1] = uint
->zero
;
1605 args
[2] = uint
->zero
;
1606 args
[3] = lp_build_const_int32(base
->gallivm
,
1607 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1608 args
[4] = uint
->zero
;
1612 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1616 if (so
->num_outputs
)
1617 fprintf(stderr
, "STREAMOUT\n");
1619 for (i
= 0; i
< so
->num_outputs
; i
++) {
1620 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1621 so
->output
[i
].start_component
;
1622 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1623 i
, so
->output
[i
].output_buffer
,
1624 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1625 so
->output
[i
].register_index
,
1626 mask
& 1 ? "x" : "",
1627 mask
& 2 ? "y" : "",
1628 mask
& 4 ? "z" : "",
1629 mask
& 8 ? "w" : "");
1633 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1634 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1635 * or v4i32 (num_channels=3,4). */
1636 static void build_tbuffer_store(struct si_shader_context
*ctx
,
1639 unsigned num_channels
,
1641 LLVMValueRef soffset
,
1642 unsigned inst_offset
,
1651 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1652 LLVMValueRef args
[] = {
1655 LLVMConstInt(ctx
->i32
, num_channels
, 0),
1658 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1659 LLVMConstInt(ctx
->i32
, dfmt
, 0),
1660 LLVMConstInt(ctx
->i32
, nfmt
, 0),
1661 LLVMConstInt(ctx
->i32
, offen
, 0),
1662 LLVMConstInt(ctx
->i32
, idxen
, 0),
1663 LLVMConstInt(ctx
->i32
, glc
, 0),
1664 LLVMConstInt(ctx
->i32
, slc
, 0),
1665 LLVMConstInt(ctx
->i32
, tfe
, 0)
1668 /* The instruction offset field has 12 bits */
1669 assert(offen
|| inst_offset
< (1 << 12));
1671 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1672 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1673 const char *types
[] = {"i32", "v2i32", "v4i32"};
1675 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1677 lp_build_intrinsic(gallivm
->builder
, name
, ctx
->voidt
,
1678 args
, Elements(args
), 0);
1681 static void build_tbuffer_store_dwords(struct si_shader_context
*ctx
,
1684 unsigned num_channels
,
1686 LLVMValueRef soffset
,
1687 unsigned inst_offset
)
1689 static unsigned dfmt
[] = {
1690 V_008F0C_BUF_DATA_FORMAT_32
,
1691 V_008F0C_BUF_DATA_FORMAT_32_32
,
1692 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1693 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1695 assert(num_channels
>= 1 && num_channels
<= 4);
1697 build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1698 inst_offset
, dfmt
[num_channels
-1],
1699 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1702 /* On SI, the vertex shader is responsible for writing streamout data
1704 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
1705 struct si_shader_output_values
*outputs
,
1708 struct pipe_stream_output_info
*so
= &ctx
->shader
->selector
->so
;
1709 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
1710 LLVMBuilderRef builder
= gallivm
->builder
;
1712 struct lp_build_if_state if_ctx
;
1714 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1715 LLVMValueRef so_vtx_count
=
1716 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
1718 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", ctx
->i32
,
1719 NULL
, 0, LLVMReadNoneAttribute
);
1721 /* can_emit = tid < so_vtx_count; */
1722 LLVMValueRef can_emit
=
1723 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1725 LLVMValueRef stream_id
=
1726 unpack_param(ctx
, ctx
->param_streamout_config
, 24, 2);
1728 /* Emit the streamout code conditionally. This actually avoids
1729 * out-of-bounds buffer access. The hw tells us via the SGPR
1730 * (so_vtx_count) which threads are allowed to emit streamout data. */
1731 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1733 /* The buffer offset is computed as follows:
1734 * ByteOffset = streamout_offset[buffer_id]*4 +
1735 * (streamout_write_index + thread_id)*stride[buffer_id] +
1739 LLVMValueRef so_write_index
=
1740 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1741 ctx
->param_streamout_write_index
);
1743 /* Compute (streamout_write_index + thread_id). */
1744 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1746 /* Compute the write offset for each enabled buffer. */
1747 LLVMValueRef so_write_offset
[4] = {};
1748 for (i
= 0; i
< 4; i
++) {
1752 LLVMValueRef so_offset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
1753 ctx
->param_streamout_offset
[i
]);
1754 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1756 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1757 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
1758 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1761 /* Write streamout data. */
1762 for (i
= 0; i
< so
->num_outputs
; i
++) {
1763 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1764 unsigned reg
= so
->output
[i
].register_index
;
1765 unsigned start
= so
->output
[i
].start_component
;
1766 unsigned num_comps
= so
->output
[i
].num_components
;
1767 unsigned stream
= so
->output
[i
].stream
;
1768 LLVMValueRef out
[4];
1769 struct lp_build_if_state if_ctx_stream
;
1771 assert(num_comps
&& num_comps
<= 4);
1772 if (!num_comps
|| num_comps
> 4)
1778 /* Load the output as int. */
1779 for (j
= 0; j
< num_comps
; j
++) {
1780 out
[j
] = LLVMBuildBitCast(builder
,
1781 outputs
[reg
].values
[start
+j
],
1785 /* Pack the output. */
1786 LLVMValueRef vdata
= NULL
;
1788 switch (num_comps
) {
1789 case 1: /* as i32 */
1792 case 2: /* as v2i32 */
1793 case 3: /* as v4i32 (aligned to 4) */
1794 case 4: /* as v4i32 */
1795 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
1796 for (j
= 0; j
< num_comps
; j
++) {
1797 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1798 LLVMConstInt(ctx
->i32
, j
, 0), "");
1803 LLVMValueRef can_emit_stream
=
1804 LLVMBuildICmp(builder
, LLVMIntEQ
,
1806 lp_build_const_int32(gallivm
, stream
), "");
1808 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1809 build_tbuffer_store_dwords(ctx
, ctx
->so_buffers
[buf_idx
],
1811 so_write_offset
[buf_idx
],
1812 LLVMConstInt(ctx
->i32
, 0, 0),
1813 so
->output
[i
].dst_offset
*4);
1814 lp_build_endif(&if_ctx_stream
);
1817 lp_build_endif(&if_ctx
);
1821 /* Generate export instructions for hardware VS shader stage */
1822 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1823 struct si_shader_output_values
*outputs
,
1826 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1827 struct si_shader
*shader
= ctx
->shader
;
1828 struct lp_build_context
*base
= &bld_base
->base
;
1829 struct lp_build_context
*uint
=
1830 &ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1831 LLVMValueRef args
[9];
1832 LLVMValueRef pos_args
[4][9] = { { 0 } };
1833 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1834 unsigned semantic_name
, semantic_index
;
1836 unsigned param_count
= 0;
1840 if (outputs
&& ctx
->shader
->selector
->so
.num_outputs
) {
1841 si_llvm_emit_streamout(ctx
, outputs
, noutput
);
1844 for (i
= 0; i
< noutput
; i
++) {
1845 semantic_name
= outputs
[i
].name
;
1846 semantic_index
= outputs
[i
].sid
;
1849 /* Select the correct target */
1850 switch(semantic_name
) {
1851 case TGSI_SEMANTIC_PSIZE
:
1852 psize_value
= outputs
[i
].values
[0];
1854 case TGSI_SEMANTIC_EDGEFLAG
:
1855 edgeflag_value
= outputs
[i
].values
[0];
1857 case TGSI_SEMANTIC_LAYER
:
1858 layer_value
= outputs
[i
].values
[0];
1859 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1860 goto handle_semantic
;
1861 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1862 viewport_index_value
= outputs
[i
].values
[0];
1863 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1864 goto handle_semantic
;
1865 case TGSI_SEMANTIC_POSITION
:
1866 target
= V_008DFC_SQ_EXP_POS
;
1868 case TGSI_SEMANTIC_COLOR
:
1869 case TGSI_SEMANTIC_BCOLOR
:
1870 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1871 shader
->vs_output_param_offset
[i
] = param_count
;
1874 case TGSI_SEMANTIC_CLIPDIST
:
1875 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1877 case TGSI_SEMANTIC_CLIPVERTEX
:
1878 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1880 case TGSI_SEMANTIC_PRIMID
:
1881 case TGSI_SEMANTIC_FOG
:
1882 case TGSI_SEMANTIC_TEXCOORD
:
1883 case TGSI_SEMANTIC_GENERIC
:
1884 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1885 shader
->vs_output_param_offset
[i
] = param_count
;
1891 "Warning: SI unhandled vs output type:%d\n",
1895 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1897 if (target
>= V_008DFC_SQ_EXP_POS
&&
1898 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1899 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1900 args
, sizeof(args
));
1902 lp_build_intrinsic(base
->gallivm
->builder
,
1903 "llvm.SI.export", ctx
->voidt
,
1907 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1908 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1909 goto handle_semantic
;
1913 shader
->nr_param_exports
= param_count
;
1915 /* We need to add the position output manually if it's missing. */
1916 if (!pos_args
[0][0]) {
1917 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1918 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1919 pos_args
[0][2] = uint
->zero
; /* last export? */
1920 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1921 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1922 pos_args
[0][5] = base
->zero
; /* X */
1923 pos_args
[0][6] = base
->zero
; /* Y */
1924 pos_args
[0][7] = base
->zero
; /* Z */
1925 pos_args
[0][8] = base
->one
; /* W */
1928 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1929 if (shader
->selector
->info
.writes_psize
||
1930 shader
->selector
->info
.writes_edgeflag
||
1931 shader
->selector
->info
.writes_viewport_index
||
1932 shader
->selector
->info
.writes_layer
) {
1933 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1934 shader
->selector
->info
.writes_psize
|
1935 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1936 (shader
->selector
->info
.writes_layer
<< 2) |
1937 (shader
->selector
->info
.writes_viewport_index
<< 3));
1938 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1939 pos_args
[1][2] = uint
->zero
; /* last export? */
1940 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1941 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1942 pos_args
[1][5] = base
->zero
; /* X */
1943 pos_args
[1][6] = base
->zero
; /* Y */
1944 pos_args
[1][7] = base
->zero
; /* Z */
1945 pos_args
[1][8] = base
->zero
; /* W */
1947 if (shader
->selector
->info
.writes_psize
)
1948 pos_args
[1][5] = psize_value
;
1950 if (shader
->selector
->info
.writes_edgeflag
) {
1951 /* The output is a float, but the hw expects an integer
1952 * with the first bit containing the edge flag. */
1953 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1956 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1958 bld_base
->int_bld
.one
);
1960 /* The LLVM intrinsic expects a float. */
1961 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1966 if (shader
->selector
->info
.writes_layer
)
1967 pos_args
[1][7] = layer_value
;
1969 if (shader
->selector
->info
.writes_viewport_index
)
1970 pos_args
[1][8] = viewport_index_value
;
1973 for (i
= 0; i
< 4; i
++)
1975 shader
->nr_pos_exports
++;
1978 for (i
= 0; i
< 4; i
++) {
1979 if (!pos_args
[i
][0])
1982 /* Specify the target we are exporting */
1983 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1985 if (pos_idx
== shader
->nr_pos_exports
)
1986 /* Specify that this is the last export */
1987 pos_args
[i
][2] = uint
->one
;
1989 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
1990 ctx
->voidt
, pos_args
[i
], 9, 0);
1994 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
1995 LLVMValueRef rel_patch_id
,
1996 LLVMValueRef invocation_id
,
1997 LLVMValueRef tcs_out_current_patch_data_offset
)
1999 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2000 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2001 struct si_shader
*shader
= ctx
->shader
;
2002 unsigned tess_inner_index
, tess_outer_index
;
2003 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2004 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
;
2005 unsigned stride
, outer_comps
, inner_comps
, i
;
2006 struct lp_build_if_state if_ctx
;
2008 /* Do this only for invocation 0, because the tess levels are per-patch,
2011 * This can't jump, because invocation 0 executes this. It should
2012 * at least mask out the loads and stores for other invocations.
2014 lp_build_if(&if_ctx
, gallivm
,
2015 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2016 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2018 /* Determine the layout of one tess factor element in the buffer. */
2019 switch (shader
->key
.tcs
.epilog
.prim_mode
) {
2020 case PIPE_PRIM_LINES
:
2021 stride
= 2; /* 2 dwords, 1 vec2 store */
2025 case PIPE_PRIM_TRIANGLES
:
2026 stride
= 4; /* 4 dwords, 1 vec4 store */
2030 case PIPE_PRIM_QUADS
:
2031 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2040 /* Load tess_inner and tess_outer from LDS.
2041 * Any invocation can write them, so we can't get them from a temporary.
2043 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2044 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2046 lds_base
= tcs_out_current_patch_data_offset
;
2047 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2048 lp_build_const_int32(gallivm
,
2049 tess_inner_index
* 4), "");
2050 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2051 lp_build_const_int32(gallivm
,
2052 tess_outer_index
* 4), "");
2054 for (i
= 0; i
< outer_comps
; i
++)
2055 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2056 for (i
= 0; i
< inner_comps
; i
++)
2057 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2059 /* Convert the outputs to vectors for stores. */
2060 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2064 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2066 /* Get the buffer. */
2067 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2068 SI_PARAM_RW_BUFFERS
);
2069 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
2070 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
2072 /* Get the offset. */
2073 tf_base
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2074 SI_PARAM_TESS_FACTOR_OFFSET
);
2075 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2076 lp_build_const_int32(gallivm
, 4 * stride
), "");
2078 /* Store the outputs. */
2079 build_tbuffer_store_dwords(ctx
, buffer
, vec0
,
2080 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
2082 build_tbuffer_store_dwords(ctx
, buffer
, vec1
,
2083 stride
- 4, byteoffset
, tf_base
, 16);
2084 lp_build_endif(&if_ctx
);
2087 /* This only writes the tessellation factor levels. */
2088 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2090 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2091 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2093 rel_patch_id
= get_rel_patch_id(ctx
);
2094 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2095 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2097 if (!ctx
->is_monolithic
) {
2098 /* Return epilog parameters from this function. */
2099 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2100 LLVMValueRef ret
= ctx
->return_value
;
2101 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2104 /* RW_BUFFERS pointer */
2105 rw_buffers
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2106 SI_PARAM_RW_BUFFERS
);
2107 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2108 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2109 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2110 bld_base
->uint_bld
.zero
, "");
2111 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2112 bld_base
->uint_bld
.one
, "");
2113 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2114 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2116 /* Tess factor buffer soffset is after user SGPRs. */
2117 tf_soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2118 SI_PARAM_TESS_FACTOR_OFFSET
);
2119 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2120 SI_TCS_NUM_USER_SGPR
, "");
2123 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2124 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2125 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2127 vgpr
= SI_TCS_NUM_USER_SGPR
+ 1;
2128 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2129 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2130 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2131 ctx
->return_value
= ret
;
2135 si_write_tess_factors(bld_base
, rel_patch_id
, invocation_id
, tf_lds_offset
);
2138 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2140 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2141 struct si_shader
*shader
= ctx
->shader
;
2142 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2143 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2145 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2146 ctx
->param_rel_auto_id
);
2147 LLVMValueRef vertex_dw_stride
=
2148 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2149 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2150 vertex_dw_stride
, "");
2152 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2153 * its inputs from it. */
2154 for (i
= 0; i
< info
->num_outputs
; i
++) {
2155 LLVMValueRef
*out_ptr
= ctx
->radeon_bld
.soa
.outputs
[i
];
2156 unsigned name
= info
->output_semantic_name
[i
];
2157 unsigned index
= info
->output_semantic_index
[i
];
2158 int param
= si_shader_io_get_unique_index(name
, index
);
2159 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2160 lp_build_const_int32(gallivm
, param
* 4), "");
2162 for (chan
= 0; chan
< 4; chan
++) {
2163 lds_store(bld_base
, chan
, dw_addr
,
2164 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2169 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2171 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2172 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2173 struct si_shader
*es
= ctx
->shader
;
2174 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2175 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2176 ctx
->param_es2gs_offset
);
2180 for (i
= 0; i
< info
->num_outputs
; i
++) {
2181 LLVMValueRef
*out_ptr
=
2182 ctx
->radeon_bld
.soa
.outputs
[i
];
2185 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2186 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2189 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2190 info
->output_semantic_index
[i
]);
2192 for (chan
= 0; chan
< 4; chan
++) {
2193 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2194 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2196 build_tbuffer_store(ctx
,
2199 LLVMGetUndef(ctx
->i32
), soffset
,
2200 (4 * param_index
+ chan
) * 4,
2201 V_008F0C_BUF_DATA_FORMAT_32
,
2202 V_008F0C_BUF_NUM_FORMAT_UINT
,
2208 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2210 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2211 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2212 LLVMValueRef args
[2];
2214 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2215 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2216 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2217 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
2220 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2222 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2223 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2224 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2225 struct si_shader_output_values
*outputs
= NULL
;
2228 assert(!ctx
->is_gs_copy_shader
);
2230 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2232 /* Vertex color clamping.
2234 * This uses a state constant loaded in a user data SGPR and
2235 * an IF statement is added that clamps all colors if the constant
2238 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
2239 struct lp_build_if_state if_ctx
;
2240 LLVMValueRef cond
= NULL
;
2241 LLVMValueRef addr
, val
;
2243 for (i
= 0; i
< info
->num_outputs
; i
++) {
2244 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2245 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2248 /* We've found a color. */
2250 /* The state is in the first bit of the user SGPR. */
2251 cond
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2252 SI_PARAM_VS_STATE_BITS
);
2253 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2255 lp_build_if(&if_ctx
, gallivm
, cond
);
2258 for (j
= 0; j
< 4; j
++) {
2259 addr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2260 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2261 val
= radeon_llvm_saturate(bld_base
, val
);
2262 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2267 lp_build_endif(&if_ctx
);
2270 for (i
= 0; i
< info
->num_outputs
; i
++) {
2271 outputs
[i
].name
= info
->output_semantic_name
[i
];
2272 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2274 for (j
= 0; j
< 4; j
++)
2275 outputs
[i
].values
[j
] =
2276 LLVMBuildLoad(gallivm
->builder
,
2277 ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2281 if (ctx
->is_monolithic
) {
2282 /* Export PrimitiveID when PS needs it. */
2283 if (si_vs_exports_prim_id(ctx
->shader
)) {
2284 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2286 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2287 get_primitive_id(bld_base
, 0));
2288 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2289 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2290 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2294 /* Return the primitive ID from the LLVM function. */
2296 LLVMBuildInsertValue(gallivm
->builder
,
2298 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2299 get_primitive_id(bld_base
, 0)),
2300 VS_EPILOG_PRIMID_LOC
, "");
2303 si_llvm_export_vs(bld_base
, outputs
, i
);
2307 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2308 LLVMValueRef depth
, LLVMValueRef stencil
,
2309 LLVMValueRef samplemask
)
2311 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2312 struct lp_build_context
*base
= &bld_base
->base
;
2313 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2314 LLVMValueRef args
[9];
2317 assert(depth
|| stencil
|| samplemask
);
2319 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2320 args
[2] = uint
->one
; /* DONE bit */
2322 /* Specify the target we are exporting */
2323 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2325 args
[4] = uint
->zero
; /* COMP flag */
2326 args
[5] = base
->undef
; /* R, depth */
2327 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2328 args
[7] = base
->undef
; /* B, sample mask */
2329 args
[8] = base
->undef
; /* A, alpha to mask */
2342 args
[7] = samplemask
;
2346 /* SI (except OLAND) has a bug that it only looks
2347 * at the X writemask component. */
2348 if (ctx
->screen
->b
.chip_class
== SI
&&
2349 ctx
->screen
->b
.family
!= CHIP_OLAND
)
2352 /* Specify which components to enable */
2353 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2355 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2356 ctx
->voidt
, args
, 9, 0);
2359 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2360 LLVMValueRef
*color
, unsigned index
,
2361 unsigned samplemask_param
,
2364 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2365 struct lp_build_context
*base
= &bld_base
->base
;
2369 if (ctx
->shader
->key
.ps
.epilog
.clamp_color
)
2370 for (i
= 0; i
< 4; i
++)
2371 color
[i
] = radeon_llvm_saturate(bld_base
, color
[i
]);
2374 if (ctx
->shader
->key
.ps
.epilog
.alpha_to_one
)
2375 color
[3] = base
->one
;
2379 ctx
->shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2380 si_alpha_test(bld_base
, color
[3]);
2382 /* Line & polygon smoothing */
2383 if (ctx
->shader
->key
.ps
.epilog
.poly_line_smoothing
)
2384 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
2387 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2388 if (ctx
->shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2389 LLVMValueRef args
[8][9];
2392 /* Get the export arguments, also find out what the last one is. */
2393 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2394 si_llvm_init_export_args(bld_base
, color
,
2395 V_008DFC_SQ_EXP_MRT
+ c
, args
[c
]);
2396 if (args
[c
][0] != bld_base
->uint_bld
.zero
)
2400 /* Emit all exports. */
2401 for (c
= 0; c
<= ctx
->shader
->key
.ps
.epilog
.last_cbuf
; c
++) {
2402 if (is_last
&& last
== c
) {
2403 args
[c
][1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2404 args
[c
][2] = bld_base
->uint_bld
.one
; /* DONE bit */
2405 } else if (args
[c
][0] == bld_base
->uint_bld
.zero
)
2406 continue; /* unnecessary NULL export */
2408 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2409 ctx
->voidt
, args
[c
], 9, 0);
2412 LLVMValueRef args
[9];
2415 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
2418 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2419 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
2420 } else if (args
[0] == bld_base
->uint_bld
.zero
)
2421 return; /* unnecessary NULL export */
2423 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2424 ctx
->voidt
, args
, 9, 0);
2428 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
2430 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2431 struct lp_build_context
*base
= &bld_base
->base
;
2432 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2433 LLVMValueRef args
[9];
2435 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
2436 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2437 args
[2] = uint
->one
; /* DONE bit */
2438 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
2439 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
2440 args
[5] = uint
->undef
; /* R */
2441 args
[6] = uint
->undef
; /* G */
2442 args
[7] = uint
->undef
; /* B */
2443 args
[8] = uint
->undef
; /* A */
2445 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2446 ctx
->voidt
, args
, 9, 0);
2449 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2451 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2452 struct si_shader
*shader
= ctx
->shader
;
2453 struct lp_build_context
*base
= &bld_base
->base
;
2454 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2455 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2456 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2457 int last_color_export
= -1;
2460 /* Determine the last export. If MRTZ is present, it's always last.
2461 * Otherwise, find the last color export.
2463 if (!info
->writes_z
&& !info
->writes_stencil
&& !info
->writes_samplemask
) {
2464 unsigned spi_format
= shader
->key
.ps
.epilog
.spi_shader_col_format
;
2466 /* Don't export NULL and return if alpha-test is enabled. */
2467 if (shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
&&
2468 shader
->key
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
&&
2469 (spi_format
& 0xf) == 0)
2470 spi_format
|= V_028714_SPI_SHADER_32_AR
;
2472 for (i
= 0; i
< info
->num_outputs
; i
++) {
2473 unsigned index
= info
->output_semantic_index
[i
];
2475 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
)
2478 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2479 if (shader
->key
.ps
.epilog
.last_cbuf
> 0) {
2480 /* Just set this if any of the colorbuffers are enabled. */
2482 ((1llu << (4 * (shader
->key
.ps
.epilog
.last_cbuf
+ 1))) - 1))
2483 last_color_export
= i
;
2487 if ((spi_format
>> (index
* 4)) & 0xf)
2488 last_color_export
= i
;
2491 /* If there are no outputs, export NULL. */
2492 if (last_color_export
== -1) {
2493 si_export_null(bld_base
);
2498 for (i
= 0; i
< info
->num_outputs
; i
++) {
2499 unsigned semantic_name
= info
->output_semantic_name
[i
];
2500 unsigned semantic_index
= info
->output_semantic_index
[i
];
2502 LLVMValueRef color
[4] = {};
2504 /* Select the correct target */
2505 switch (semantic_name
) {
2506 case TGSI_SEMANTIC_POSITION
:
2507 depth
= LLVMBuildLoad(builder
,
2508 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2510 case TGSI_SEMANTIC_STENCIL
:
2511 stencil
= LLVMBuildLoad(builder
,
2512 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2514 case TGSI_SEMANTIC_SAMPLEMASK
:
2515 samplemask
= LLVMBuildLoad(builder
,
2516 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2518 case TGSI_SEMANTIC_COLOR
:
2519 for (j
= 0; j
< 4; j
++)
2520 color
[j
] = LLVMBuildLoad(builder
,
2521 ctx
->radeon_bld
.soa
.outputs
[i
][j
], "");
2523 si_export_mrt_color(bld_base
, color
, semantic_index
,
2524 SI_PARAM_SAMPLE_COVERAGE
,
2525 last_color_export
== i
);
2529 "Warning: SI unhandled fs output type:%d\n",
2534 if (depth
|| stencil
|| samplemask
)
2535 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
2539 * Return PS outputs in this order:
2541 * v[0:3] = color0.xyzw
2542 * v[4:7] = color1.xyzw
2547 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
2549 * The alpha-ref SGPR is returned via its original location.
2551 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
2553 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2554 struct si_shader
*shader
= ctx
->shader
;
2555 struct lp_build_context
*base
= &bld_base
->base
;
2556 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2557 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2558 unsigned i
, j
, first_vgpr
, vgpr
;
2560 LLVMValueRef color
[8][4] = {};
2561 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2564 /* Read the output values. */
2565 for (i
= 0; i
< info
->num_outputs
; i
++) {
2566 unsigned semantic_name
= info
->output_semantic_name
[i
];
2567 unsigned semantic_index
= info
->output_semantic_index
[i
];
2569 switch (semantic_name
) {
2570 case TGSI_SEMANTIC_COLOR
:
2571 assert(semantic_index
< 8);
2572 for (j
= 0; j
< 4; j
++) {
2573 LLVMValueRef ptr
= ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2574 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2575 color
[semantic_index
][j
] = result
;
2578 case TGSI_SEMANTIC_POSITION
:
2579 depth
= LLVMBuildLoad(builder
,
2580 ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2582 case TGSI_SEMANTIC_STENCIL
:
2583 stencil
= LLVMBuildLoad(builder
,
2584 ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2586 case TGSI_SEMANTIC_SAMPLEMASK
:
2587 samplemask
= LLVMBuildLoad(builder
,
2588 ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2591 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
2596 /* Fill the return structure. */
2597 ret
= ctx
->return_value
;
2600 ret
= LLVMBuildInsertValue(builder
, ret
,
2601 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
2602 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2603 SI_PARAM_ALPHA_REF
)),
2604 SI_SGPR_ALPHA_REF
, "");
2607 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
2608 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
2612 for (j
= 0; j
< 4; j
++)
2613 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
2616 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
2618 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
2620 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
2622 /* Add the input sample mask for smoothing at the end. */
2623 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
2624 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
2625 ret
= LLVMBuildInsertValue(builder
, ret
,
2626 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2627 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
2629 ctx
->return_value
= ret
;
2632 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
2633 struct lp_build_tgsi_context
*bld_base
,
2634 struct lp_build_emit_data
*emit_data
);
2636 static bool tgsi_is_array_sampler(unsigned target
)
2638 return target
== TGSI_TEXTURE_1D_ARRAY
||
2639 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2640 target
== TGSI_TEXTURE_2D_ARRAY
||
2641 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2642 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2643 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2644 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2647 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
2648 struct lp_build_emit_data
*emit_data
,
2649 unsigned opcode
, unsigned target
,
2650 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
2651 LLVMValueRef
*param
, unsigned count
,
2654 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
2656 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
2658 /* Pad to power of two vector */
2659 while (count
< util_next_power_of_two(count
))
2660 param
[count
++] = LLVMGetUndef(ctx
->i32
);
2662 /* Texture coordinates. */
2664 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
2666 emit_data
->args
[0] = param
[0];
2669 emit_data
->args
[1] = res_ptr
;
2672 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
2673 emit_data
->dst_type
= ctx
->v4i32
;
2675 emit_data
->dst_type
= ctx
->v4f32
;
2677 emit_data
->args
[num_args
++] = samp_ptr
;
2680 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
2681 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
2682 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
2683 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
2684 tgsi_is_array_sampler(target
)); /* da */
2685 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
2686 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
2687 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
2688 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
2690 emit_data
->arg_count
= num_args
;
2693 static const struct lp_build_tgsi_action tex_action
;
2701 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
2703 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
2708 * Load an image view, fmask view. or sampler state descriptor.
2710 static LLVMValueRef
get_sampler_desc(struct si_shader_context
*ctx
,
2711 LLVMValueRef index
, enum desc_type type
)
2713 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
2714 LLVMBuilderRef builder
= gallivm
->builder
;
2715 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
2720 /* The image is at [0:7]. */
2721 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
2724 /* The FMASK is at [8:15]. */
2725 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
2726 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
2729 /* The sampler state is at [12:15]. */
2730 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2731 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
2732 ptr
= LLVMBuildPointerCast(builder
, ptr
,
2733 const_array(ctx
->v4i32
, 0), "");
2737 return build_indexed_load_const(ctx
, ptr
, index
);
2740 static void tex_fetch_ptrs(
2741 struct lp_build_tgsi_context
*bld_base
,
2742 struct lp_build_emit_data
*emit_data
,
2743 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
2745 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2746 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2747 unsigned target
= inst
->Texture
.Texture
;
2748 unsigned sampler_src
;
2749 unsigned sampler_index
;
2751 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
2752 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
2754 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
2755 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
2756 LLVMValueRef ind_index
;
2758 ind_index
= get_indirect_index(ctx
, ®
->Indirect
, reg
->Register
.Index
);
2760 *res_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_IMAGE
);
2762 if (target
== TGSI_TEXTURE_2D_MSAA
||
2763 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2765 *fmask_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_FMASK
);
2767 *samp_ptr
= get_sampler_desc(ctx
, ind_index
, DESC_SAMPLER
);
2771 *res_ptr
= ctx
->sampler_views
[sampler_index
];
2772 *samp_ptr
= ctx
->sampler_states
[sampler_index
];
2773 *fmask_ptr
= ctx
->fmasks
[sampler_index
];
2777 static void tex_fetch_args(
2778 struct lp_build_tgsi_context
*bld_base
,
2779 struct lp_build_emit_data
*emit_data
)
2781 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2782 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2783 LLVMBuilderRef builder
= gallivm
->builder
;
2784 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2785 unsigned opcode
= inst
->Instruction
.Opcode
;
2786 unsigned target
= inst
->Texture
.Texture
;
2787 LLVMValueRef coords
[5], derivs
[6];
2788 LLVMValueRef address
[16];
2790 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
2793 unsigned num_deriv_channels
= 0;
2794 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
2795 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2796 unsigned dmask
= 0xf;
2798 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2800 if (opcode
== TGSI_OPCODE_TXQ
) {
2801 if (target
== TGSI_TEXTURE_BUFFER
) {
2802 /* Read the size from the buffer descriptor directly. */
2803 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
2804 LLVMValueRef size
= LLVMBuildExtractElement(builder
, res
,
2805 lp_build_const_int32(gallivm
, 6), "");
2807 if (ctx
->screen
->b
.chip_class
>= VI
) {
2808 /* On VI, the descriptor contains the size in bytes,
2809 * but TXQ must return the size in elements.
2810 * The stride is always non-zero for resources using TXQ.
2812 LLVMValueRef stride
=
2813 LLVMBuildExtractElement(builder
, res
,
2814 lp_build_const_int32(gallivm
, 5), "");
2815 stride
= LLVMBuildLShr(builder
, stride
,
2816 lp_build_const_int32(gallivm
, 16), "");
2817 stride
= LLVMBuildAnd(builder
, stride
,
2818 lp_build_const_int32(gallivm
, 0x3FFF), "");
2820 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2823 emit_data
->args
[0] = size
;
2827 /* Textures - set the mip level. */
2828 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
2830 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
2831 NULL
, address
, count
, 0xf);
2835 if (target
== TGSI_TEXTURE_BUFFER
) {
2836 LLVMTypeRef v2i128
= LLVMVectorType(ctx
->i128
, 2);
2838 /* Bitcast and truncate v8i32 to v16i8. */
2839 LLVMValueRef res
= res_ptr
;
2840 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
2841 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
2842 res
= LLVMBuildBitCast(gallivm
->builder
, res
, ctx
->v16i8
, "");
2844 emit_data
->dst_type
= ctx
->v4f32
;
2845 emit_data
->args
[0] = res
;
2846 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
2847 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
2848 emit_data
->arg_count
= 3;
2852 /* Fetch and project texture coordinates */
2853 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2854 for (chan
= 0; chan
< 3; chan
++ ) {
2855 coords
[chan
] = lp_build_emit_fetch(bld_base
,
2858 if (opcode
== TGSI_OPCODE_TXP
)
2859 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2865 if (opcode
== TGSI_OPCODE_TXP
)
2866 coords
[3] = bld_base
->base
.one
;
2869 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
2870 /* The offsets are six-bit signed integers packed like this:
2871 * X=[5:0], Y=[13:8], and Z=[21:16].
2873 LLVMValueRef offset
[3], pack
;
2875 assert(inst
->Texture
.NumOffsets
== 1);
2877 for (chan
= 0; chan
< 3; chan
++) {
2878 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
2879 emit_data
->inst
, 0, chan
);
2880 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
2881 lp_build_const_int32(gallivm
, 0x3f), "");
2883 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
2884 lp_build_const_int32(gallivm
, chan
*8), "");
2887 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
2888 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
2889 address
[count
++] = pack
;
2892 /* Pack LOD bias value */
2893 if (opcode
== TGSI_OPCODE_TXB
)
2894 address
[count
++] = coords
[3];
2895 if (opcode
== TGSI_OPCODE_TXB2
)
2896 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2898 /* Pack depth comparison value */
2899 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
2900 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
2901 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2903 assert(ref_pos
>= 0);
2904 address
[count
++] = coords
[ref_pos
];
2908 /* Pack user derivatives */
2909 if (opcode
== TGSI_OPCODE_TXD
) {
2910 int param
, num_src_deriv_channels
;
2913 case TGSI_TEXTURE_3D
:
2914 num_src_deriv_channels
= 3;
2915 num_deriv_channels
= 3;
2917 case TGSI_TEXTURE_2D
:
2918 case TGSI_TEXTURE_SHADOW2D
:
2919 case TGSI_TEXTURE_RECT
:
2920 case TGSI_TEXTURE_SHADOWRECT
:
2921 case TGSI_TEXTURE_2D_ARRAY
:
2922 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2923 num_src_deriv_channels
= 2;
2924 num_deriv_channels
= 2;
2926 case TGSI_TEXTURE_CUBE
:
2927 case TGSI_TEXTURE_SHADOWCUBE
:
2928 case TGSI_TEXTURE_CUBE_ARRAY
:
2929 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2930 /* Cube derivatives will be converted to 2D. */
2931 num_src_deriv_channels
= 3;
2932 num_deriv_channels
= 2;
2934 case TGSI_TEXTURE_1D
:
2935 case TGSI_TEXTURE_SHADOW1D
:
2936 case TGSI_TEXTURE_1D_ARRAY
:
2937 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2938 num_src_deriv_channels
= 1;
2939 num_deriv_channels
= 1;
2942 unreachable("invalid target");
2945 for (param
= 0; param
< 2; param
++)
2946 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
2947 derivs
[param
* num_src_deriv_channels
+ chan
] =
2948 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
2951 if (target
== TGSI_TEXTURE_CUBE
||
2952 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2953 target
== TGSI_TEXTURE_SHADOWCUBE
||
2954 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
2955 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
2957 if (opcode
== TGSI_OPCODE_TXD
)
2958 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
2959 address
[count
++] = derivs
[i
];
2961 /* Pack texture coordinates */
2962 address
[count
++] = coords
[0];
2964 address
[count
++] = coords
[1];
2966 address
[count
++] = coords
[2];
2968 /* Pack LOD or sample index */
2969 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
2970 address
[count
++] = coords
[3];
2971 else if (opcode
== TGSI_OPCODE_TXL2
)
2972 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2975 assert(!"Cannot handle more than 16 texture address parameters");
2979 for (chan
= 0; chan
< count
; chan
++ ) {
2980 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
2981 address
[chan
], ctx
->i32
, "");
2984 /* Adjust the sample index according to FMASK.
2986 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2987 * which is the identity mapping. Each nibble says which physical sample
2988 * should be fetched to get that sample.
2990 * For example, 0x11111100 means there are only 2 samples stored and
2991 * the second sample covers 3/4 of the pixel. When reading samples 0
2992 * and 1, return physical sample 0 (determined by the first two 0s
2993 * in FMASK), otherwise return physical sample 1.
2995 * The sample index should be adjusted as follows:
2996 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2998 if (target
== TGSI_TEXTURE_2D_MSAA
||
2999 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
3000 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3001 struct lp_build_emit_data txf_emit_data
= *emit_data
;
3002 LLVMValueRef txf_address
[4];
3003 unsigned txf_count
= count
;
3004 struct tgsi_full_instruction inst
= {};
3006 memcpy(txf_address
, address
, sizeof(txf_address
));
3008 if (target
== TGSI_TEXTURE_2D_MSAA
) {
3009 txf_address
[2] = bld_base
->uint_bld
.zero
;
3011 txf_address
[3] = bld_base
->uint_bld
.zero
;
3013 /* Read FMASK using TXF. */
3014 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
3015 inst
.Texture
.Texture
= target
;
3016 txf_emit_data
.inst
= &inst
;
3017 txf_emit_data
.chan
= 0;
3018 set_tex_fetch_args(ctx
, &txf_emit_data
, TGSI_OPCODE_TXF
,
3019 target
, fmask_ptr
, NULL
,
3020 txf_address
, txf_count
, 0xf);
3021 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
3023 /* Initialize some constants. */
3024 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
3025 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
3027 /* Apply the formula. */
3028 LLVMValueRef fmask
=
3029 LLVMBuildExtractElement(gallivm
->builder
,
3030 txf_emit_data
.output
[0],
3031 uint_bld
->zero
, "");
3033 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
3035 LLVMValueRef sample_index4
=
3036 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
3038 LLVMValueRef shifted_fmask
=
3039 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
3041 LLVMValueRef final_sample
=
3042 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
3044 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3045 * resource descriptor is 0 (invalid),
3047 LLVMValueRef fmask_desc
=
3048 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
3051 LLVMValueRef fmask_word1
=
3052 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
3055 LLVMValueRef word1_is_nonzero
=
3056 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
3057 fmask_word1
, uint_bld
->zero
, "");
3059 /* Replace the MSAA sample index. */
3060 address
[sample_chan
] =
3061 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
3062 final_sample
, address
[sample_chan
], "");
3065 if (opcode
== TGSI_OPCODE_TXF
) {
3066 /* add tex offsets */
3067 if (inst
->Texture
.NumOffsets
) {
3068 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
3069 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
3070 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
3072 assert(inst
->Texture
.NumOffsets
== 1);
3075 case TGSI_TEXTURE_3D
:
3076 address
[2] = lp_build_add(uint_bld
, address
[2],
3077 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
3079 case TGSI_TEXTURE_2D
:
3080 case TGSI_TEXTURE_SHADOW2D
:
3081 case TGSI_TEXTURE_RECT
:
3082 case TGSI_TEXTURE_SHADOWRECT
:
3083 case TGSI_TEXTURE_2D_ARRAY
:
3084 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
3086 lp_build_add(uint_bld
, address
[1],
3087 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
3089 case TGSI_TEXTURE_1D
:
3090 case TGSI_TEXTURE_SHADOW1D
:
3091 case TGSI_TEXTURE_1D_ARRAY
:
3092 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
3094 lp_build_add(uint_bld
, address
[0],
3095 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
3097 /* texture offsets do not apply to other texture targets */
3102 if (opcode
== TGSI_OPCODE_TG4
) {
3103 unsigned gather_comp
= 0;
3105 /* DMASK was repurposed for GATHER4. 4 components are always
3106 * returned and DMASK works like a swizzle - it selects
3107 * the component to fetch. The only valid DMASK values are
3108 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
3109 * (red,red,red,red) etc.) The ISA document doesn't mention
3113 /* Get the component index from src1.x for Gather4. */
3114 if (!tgsi_is_shadow_target(target
)) {
3115 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3116 LLVMValueRef comp_imm
;
3117 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
3119 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
3121 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
3122 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
3123 gather_comp
= CLAMP(gather_comp
, 0, 3);
3126 dmask
= 1 << gather_comp
;
3129 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
3130 samp_ptr
, address
, count
, dmask
);
3133 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3134 struct lp_build_tgsi_context
*bld_base
,
3135 struct lp_build_emit_data
*emit_data
)
3137 struct lp_build_context
*base
= &bld_base
->base
;
3138 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
3139 unsigned target
= emit_data
->inst
->Texture
.Texture
;
3140 char intr_name
[127];
3141 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
3142 bool is_shadow
= tgsi_is_shadow_target(target
);
3144 const char *name
= "llvm.SI.image.sample";
3145 const char *infix
= "";
3147 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
3148 /* Just return the buffer size. */
3149 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
3153 if (target
== TGSI_TEXTURE_BUFFER
) {
3154 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3155 base
->gallivm
->builder
,
3156 "llvm.SI.vs.load.input", emit_data
->dst_type
,
3157 emit_data
->args
, emit_data
->arg_count
,
3158 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3163 case TGSI_OPCODE_TXF
:
3164 name
= target
== TGSI_TEXTURE_2D_MSAA
||
3165 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
3166 "llvm.SI.image.load" :
3167 "llvm.SI.image.load.mip";
3171 case TGSI_OPCODE_TXQ
:
3172 name
= "llvm.SI.getresinfo";
3176 case TGSI_OPCODE_LODQ
:
3177 name
= "llvm.SI.getlod";
3181 case TGSI_OPCODE_TEX
:
3182 case TGSI_OPCODE_TEX2
:
3183 case TGSI_OPCODE_TXP
:
3185 case TGSI_OPCODE_TXB
:
3186 case TGSI_OPCODE_TXB2
:
3189 case TGSI_OPCODE_TXL
:
3190 case TGSI_OPCODE_TXL2
:
3193 case TGSI_OPCODE_TXD
:
3196 case TGSI_OPCODE_TG4
:
3197 name
= "llvm.SI.gather4";
3204 if (LLVMGetTypeKind(LLVMTypeOf(emit_data
->args
[0])) == LLVMVectorTypeKind
)
3205 sprintf(type
, ".v%ui32",
3206 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
3208 strcpy(type
, ".i32");
3210 /* Add the type and suffixes .c, .o if needed. */
3211 sprintf(intr_name
, "%s%s%s%s%s",
3212 name
, is_shadow
? ".c" : "", infix
,
3213 has_offset
? ".o" : "", type
);
3215 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3216 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
3217 emit_data
->args
, emit_data
->arg_count
,
3218 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3220 /* Divide the number of layers by 6 to get the number of cubes. */
3221 if (opcode
== TGSI_OPCODE_TXQ
&&
3222 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
3223 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
3224 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
3225 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
3226 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
3228 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
3229 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
3230 z
= LLVMBuildSDiv(builder
, z
, six
, "");
3232 emit_data
->output
[emit_data
->chan
] =
3233 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
3237 static void si_llvm_emit_txqs(
3238 const struct lp_build_tgsi_action
*action
,
3239 struct lp_build_tgsi_context
*bld_base
,
3240 struct lp_build_emit_data
*emit_data
)
3242 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3243 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3244 LLVMBuilderRef builder
= gallivm
->builder
;
3245 LLVMValueRef res
, samples
;
3246 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
3248 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
3251 /* Read the samples from the descriptor directly. */
3252 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
3253 samples
= LLVMBuildExtractElement(
3255 lp_build_const_int32(gallivm
, 3), "");
3256 samples
= LLVMBuildLShr(builder
, samples
,
3257 lp_build_const_int32(gallivm
, 16), "");
3258 samples
= LLVMBuildAnd(builder
, samples
,
3259 lp_build_const_int32(gallivm
, 0xf), "");
3260 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
3263 emit_data
->output
[emit_data
->chan
] = samples
;
3267 * SI implements derivatives using the local data store (LDS)
3268 * All writes to the LDS happen in all executing threads at
3269 * the same time. TID is the Thread ID for the current
3270 * thread and is a value between 0 and 63, representing
3271 * the thread's position in the wavefront.
3273 * For the pixel shader threads are grouped into quads of four pixels.
3274 * The TIDs of the pixels of a quad are:
3282 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
3283 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
3284 * the current pixel's column, and masking with 0xfffffffe yields the TID
3285 * of the left pixel of the current pixel's row.
3287 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
3288 * adding 2 yields the TID of the pixel below the top pixel.
3290 /* masks for thread ID. */
3291 #define TID_MASK_TOP_LEFT 0xfffffffc
3292 #define TID_MASK_TOP 0xfffffffd
3293 #define TID_MASK_LEFT 0xfffffffe
3295 static void si_llvm_emit_ddxy(
3296 const struct lp_build_tgsi_action
*action
,
3297 struct lp_build_tgsi_context
*bld_base
,
3298 struct lp_build_emit_data
*emit_data
)
3300 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3301 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3302 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3303 unsigned opcode
= inst
->Instruction
.Opcode
;
3304 LLVMValueRef indices
[2];
3305 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
3306 LLVMValueRef tl
, trbl
, result
[4];
3307 unsigned swizzle
[4];
3312 indices
[0] = bld_base
->uint_bld
.zero
;
3313 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3314 NULL
, 0, LLVMReadNoneAttribute
);
3315 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3318 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3319 mask
= TID_MASK_LEFT
;
3320 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3321 mask
= TID_MASK_TOP
;
3323 mask
= TID_MASK_TOP_LEFT
;
3325 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
3326 lp_build_const_int32(gallivm
, mask
), "");
3327 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3330 /* for DDX we want to next X pixel, DDY next Y pixel. */
3331 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3332 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
3333 lp_build_const_int32(gallivm
, idx
), "");
3334 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3337 for (c
= 0; c
< 4; ++c
) {
3340 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
3341 for (i
= 0; i
< c
; ++i
) {
3342 if (swizzle
[i
] == swizzle
[c
]) {
3343 result
[c
] = result
[i
];
3350 LLVMBuildStore(gallivm
->builder
,
3351 LLVMBuildBitCast(gallivm
->builder
,
3352 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
3356 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
3357 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3359 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
3360 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, ctx
->f32
, "");
3362 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
3365 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
3369 * this takes an I,J coordinate pair,
3370 * and works out the X and Y derivatives.
3371 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3373 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3374 struct lp_build_tgsi_context
*bld_base
,
3375 LLVMValueRef interp_ij
)
3377 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3378 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3379 LLVMValueRef indices
[2];
3380 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3381 LLVMValueRef tl
, tr
, bl
, result
[4];
3384 indices
[0] = bld_base
->uint_bld
.zero
;
3385 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", ctx
->i32
,
3386 NULL
, 0, LLVMReadNoneAttribute
);
3387 store_ptr
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3390 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3391 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3393 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3394 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3397 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3401 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3404 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3405 lp_build_const_int32(gallivm
, 1), "");
3406 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3409 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3410 lp_build_const_int32(gallivm
, 2), "");
3411 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, ctx
->lds
,
3414 for (c
= 0; c
< 2; ++c
) {
3415 LLVMValueRef store_val
;
3416 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3418 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3419 interp_ij
, c_ll
, "");
3420 LLVMBuildStore(gallivm
->builder
,
3424 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3425 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3427 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3428 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, ctx
->f32
, "");
3430 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3432 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3433 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
3435 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3436 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, ctx
->f32
, "");
3438 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3441 return lp_build_gather_values(gallivm
, result
, 4);
3444 static void interp_fetch_args(
3445 struct lp_build_tgsi_context
*bld_base
,
3446 struct lp_build_emit_data
*emit_data
)
3448 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3449 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3450 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3452 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3453 /* offset is in second src, first two channels */
3454 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3457 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3460 emit_data
->arg_count
= 2;
3461 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3462 LLVMValueRef sample_position
;
3463 LLVMValueRef sample_id
;
3464 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3466 /* fetch sample ID, then fetch its sample position,
3467 * and place into first two channels.
3469 sample_id
= lp_build_emit_fetch(bld_base
,
3470 emit_data
->inst
, 1, TGSI_CHAN_X
);
3471 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3473 sample_position
= load_sample_position(&ctx
->radeon_bld
, sample_id
);
3475 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3477 lp_build_const_int32(gallivm
, 0), "");
3479 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3480 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3482 lp_build_const_int32(gallivm
, 1), "");
3483 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3484 emit_data
->arg_count
= 2;
3488 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3489 struct lp_build_tgsi_context
*bld_base
,
3490 struct lp_build_emit_data
*emit_data
)
3492 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3493 struct si_shader
*shader
= ctx
->shader
;
3494 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3495 LLVMValueRef interp_param
;
3496 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3497 const char *intr_name
;
3498 int input_index
= inst
->Src
[0].Register
.Index
;
3501 LLVMValueRef attr_number
;
3502 LLVMValueRef params
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3503 int interp_param_idx
;
3504 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
3507 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3509 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3510 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3511 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3513 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3515 interp_param_idx
= lookup_interp_param_index(interp
, location
);
3516 if (interp_param_idx
== -1)
3518 else if (interp_param_idx
)
3519 interp_param
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3521 interp_param
= NULL
;
3523 attr_number
= lp_build_const_int32(gallivm
, input_index
);
3525 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3526 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3527 LLVMValueRef ij_out
[2];
3528 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3531 * take the I then J parameters, and the DDX/Y for it, and
3532 * calculate the IJ inputs for the interpolator.
3533 * temp1 = ddx * offset/sample.x + I;
3534 * interp_param.I = ddy * offset/sample.y + temp1;
3535 * temp1 = ddx * offset/sample.x + J;
3536 * interp_param.J = ddy * offset/sample.y + temp1;
3538 for (i
= 0; i
< 2; i
++) {
3539 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3540 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
3541 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3542 ddxy_out
, ix_ll
, "");
3543 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3544 ddxy_out
, iy_ll
, "");
3545 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3546 interp_param
, ix_ll
, "");
3547 LLVMValueRef temp1
, temp2
;
3549 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3552 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3554 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3556 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3558 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3560 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
3561 temp2
, ctx
->i32
, "");
3563 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
3566 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3567 for (chan
= 0; chan
< 2; chan
++) {
3568 LLVMValueRef args
[4];
3569 LLVMValueRef llvm_chan
;
3572 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3573 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
3575 args
[0] = llvm_chan
;
3576 args
[1] = attr_number
;
3578 args
[3] = interp_param
;
3580 emit_data
->output
[chan
] =
3581 lp_build_intrinsic(gallivm
->builder
, intr_name
,
3582 ctx
->f32
, args
, args
[3] ? 4 : 3,
3583 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3587 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3588 struct lp_build_emit_data
*emit_data
)
3590 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3591 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3594 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3596 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
3600 /* Emit one vertex from the geometry shader */
3601 static void si_llvm_emit_vertex(
3602 const struct lp_build_tgsi_action
*action
,
3603 struct lp_build_tgsi_context
*bld_base
,
3604 struct lp_build_emit_data
*emit_data
)
3606 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3607 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3608 struct si_shader
*shader
= ctx
->shader
;
3609 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3610 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3611 LLVMValueRef soffset
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
3612 SI_PARAM_GS2VS_OFFSET
);
3613 LLVMValueRef gs_next_vertex
;
3614 LLVMValueRef can_emit
, kill
;
3615 LLVMValueRef args
[2];
3620 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3622 /* Write vertex attribute values to GSVS ring */
3623 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3624 ctx
->gs_next_vertex
[stream
],
3627 /* If this thread has already emitted the declared maximum number of
3628 * vertices, kill it: excessive vertex emissions are not supposed to
3629 * have any effect, and GS threads have no externally observable
3630 * effects other than emitting vertices.
3632 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
3633 lp_build_const_int32(gallivm
,
3634 shader
->selector
->gs_max_out_vertices
), "");
3635 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3636 lp_build_const_float(gallivm
, 1.0f
),
3637 lp_build_const_float(gallivm
, -1.0f
));
3639 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
3640 ctx
->voidt
, &kill
, 1, 0);
3642 for (i
= 0; i
< info
->num_outputs
; i
++) {
3643 LLVMValueRef
*out_ptr
=
3644 ctx
->radeon_bld
.soa
.outputs
[i
];
3646 for (chan
= 0; chan
< 4; chan
++) {
3647 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3648 LLVMValueRef voffset
=
3649 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
3650 shader
->selector
->gs_max_out_vertices
);
3652 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3653 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3655 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
3657 build_tbuffer_store(ctx
,
3658 ctx
->gsvs_ring
[stream
],
3660 voffset
, soffset
, 0,
3661 V_008F0C_BUF_DATA_FORMAT_32
,
3662 V_008F0C_BUF_NUM_FORMAT_UINT
,
3666 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3667 lp_build_const_int32(gallivm
, 1));
3669 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
3671 /* Signal vertex emission */
3672 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
3673 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3674 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3675 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
3678 /* Cut one primitive from the geometry shader */
3679 static void si_llvm_emit_primitive(
3680 const struct lp_build_tgsi_action
*action
,
3681 struct lp_build_tgsi_context
*bld_base
,
3682 struct lp_build_emit_data
*emit_data
)
3684 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3685 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3686 LLVMValueRef args
[2];
3689 /* Signal primitive cut */
3690 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3691 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
3692 args
[1] = LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3693 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3694 ctx
->voidt
, args
, 2, LLVMNoUnwindAttribute
);
3697 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
3698 struct lp_build_tgsi_context
*bld_base
,
3699 struct lp_build_emit_data
*emit_data
)
3701 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3702 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3704 lp_build_intrinsic(gallivm
->builder
,
3705 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
3706 : "llvm.AMDGPU.barrier.local",
3707 ctx
->voidt
, NULL
, 0, LLVMNoUnwindAttribute
);
3710 static const struct lp_build_tgsi_action tex_action
= {
3711 .fetch_args
= tex_fetch_args
,
3712 .emit
= build_tex_intrinsic
,
3715 static const struct lp_build_tgsi_action interp_action
= {
3716 .fetch_args
= interp_fetch_args
,
3717 .emit
= build_interp_intrinsic
,
3720 static void si_create_function(struct si_shader_context
*ctx
,
3721 LLVMTypeRef
*returns
, unsigned num_returns
,
3722 LLVMTypeRef
*params
, unsigned num_params
,
3723 int last_array_pointer
, int last_sgpr
)
3727 radeon_llvm_create_func(&ctx
->radeon_bld
, returns
, num_returns
,
3728 params
, num_params
);
3729 radeon_llvm_shader_type(ctx
->radeon_bld
.main_fn
, ctx
->type
);
3730 ctx
->return_value
= LLVMGetUndef(ctx
->radeon_bld
.return_type
);
3732 for (i
= 0; i
<= last_sgpr
; ++i
) {
3733 LLVMValueRef P
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, i
);
3735 /* We tell llvm that array inputs are passed by value to allow Sinking pass
3736 * to move load. Inputs are constant so this is fine. */
3737 if (i
<= last_array_pointer
)
3738 LLVMAddAttribute(P
, LLVMByValAttribute
);
3740 LLVMAddAttribute(P
, LLVMInRegAttribute
);
3744 static void create_meta_data(struct si_shader_context
*ctx
)
3746 struct gallivm_state
*gallivm
= ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3747 LLVMValueRef args
[3];
3749 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
3751 args
[2] = lp_build_const_int32(gallivm
, 1);
3753 ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
3756 static void declare_streamout_params(struct si_shader_context
*ctx
,
3757 struct pipe_stream_output_info
*so
,
3758 LLVMTypeRef
*params
, LLVMTypeRef i32
,
3759 unsigned *num_params
)
3763 /* Streamout SGPRs. */
3764 if (so
->num_outputs
) {
3765 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
3766 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
3768 /* A streamout buffer offset is loaded if the stride is non-zero. */
3769 for (i
= 0; i
< 4; i
++) {
3773 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
3777 static unsigned llvm_get_type_size(LLVMTypeRef type
)
3779 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
3782 case LLVMIntegerTypeKind
:
3783 return LLVMGetIntTypeWidth(type
) / 8;
3784 case LLVMFloatTypeKind
:
3786 case LLVMPointerTypeKind
:
3788 case LLVMVectorTypeKind
:
3789 return LLVMGetVectorSize(type
) *
3790 llvm_get_type_size(LLVMGetElementType(type
));
3797 static void declare_tess_lds(struct si_shader_context
*ctx
)
3799 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
3800 LLVMTypeRef i32
= ctx
->radeon_bld
.soa
.bld_base
.uint_bld
.elem_type
;
3802 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
3803 unsigned vertex_data_dw_size
= 32*32*4;
3804 unsigned patch_data_dw_size
= 32*4;
3805 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
3806 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
3807 unsigned lds_dwords
= patch_dw_size
;
3809 /* The actual size is computed outside of the shader to reduce
3810 * the number of shader variants. */
3812 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3813 LLVMArrayType(i32
, lds_dwords
),
3818 static void create_function(struct si_shader_context
*ctx
)
3820 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
3821 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3822 struct si_shader
*shader
= ctx
->shader
;
3823 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_NUM_VERTEX_BUFFERS
], v3i32
;
3824 LLVMTypeRef returns
[16+32*4];
3825 unsigned i
, last_array_pointer
, last_sgpr
, num_params
, num_return_sgprs
;
3826 unsigned num_returns
= 0;
3828 v3i32
= LLVMVectorType(ctx
->i32
, 3);
3830 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
3831 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
3832 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
3833 params
[SI_PARAM_UNUSED
] = LLVMPointerType(ctx
->i32
, CONST_ADDR_SPACE
);
3834 last_array_pointer
= SI_PARAM_UNUSED
;
3836 switch (ctx
->type
) {
3837 case TGSI_PROCESSOR_VERTEX
:
3838 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_VERTEX_BUFFERS
);
3839 last_array_pointer
= SI_PARAM_VERTEX_BUFFERS
;
3840 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
3841 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
3842 num_params
= SI_PARAM_START_INSTANCE
+1;
3844 if (shader
->key
.vs
.as_es
) {
3845 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
3846 } else if (shader
->key
.vs
.as_ls
) {
3847 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
3848 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
3850 if (ctx
->is_gs_copy_shader
) {
3851 last_array_pointer
= SI_PARAM_CONST_BUFFERS
;
3852 num_params
= SI_PARAM_CONST_BUFFERS
+1;
3854 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
3855 num_params
= SI_PARAM_VS_STATE_BITS
+1;
3858 /* The locations of the other parameters are assigned dynamically. */
3859 declare_streamout_params(ctx
, &shader
->selector
->so
,
3860 params
, ctx
->i32
, &num_params
);
3863 last_sgpr
= num_params
-1;
3866 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
3867 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
3868 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
3869 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
3871 if (!ctx
->is_monolithic
&&
3872 !ctx
->is_gs_copy_shader
) {
3873 /* Vertex load indices. */
3874 ctx
->param_vertex_index0
= num_params
;
3876 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
3877 params
[num_params
++] = ctx
->i32
;
3879 /* PrimitiveID output. */
3880 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
)
3881 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
3882 returns
[num_returns
++] = ctx
->f32
;
3886 case TGSI_PROCESSOR_TESS_CTRL
:
3887 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
3888 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
3889 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
3890 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
3891 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
3894 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
3895 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
3896 num_params
= SI_PARAM_REL_IDS
+1;
3898 if (!ctx
->is_monolithic
) {
3899 /* PARAM_TESS_FACTOR_OFFSET is after user SGPRs. */
3900 for (i
= 0; i
<= SI_TCS_NUM_USER_SGPR
; i
++)
3901 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
3903 for (i
= 0; i
< 3; i
++)
3904 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
3908 case TGSI_PROCESSOR_TESS_EVAL
:
3909 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
3910 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
3911 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
3913 if (shader
->key
.tes
.as_es
) {
3914 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
3916 declare_streamout_params(ctx
, &shader
->selector
->so
,
3917 params
, ctx
->i32
, &num_params
);
3919 last_sgpr
= num_params
- 1;
3922 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
3923 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
3924 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
3925 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
3927 /* PrimitiveID output. */
3928 if (!ctx
->is_monolithic
&& !shader
->key
.tes
.as_es
)
3929 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
3930 returns
[num_returns
++] = ctx
->f32
;
3933 case TGSI_PROCESSOR_GEOMETRY
:
3934 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
3935 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
3936 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
3939 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
3940 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
3941 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
3942 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
3943 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
3944 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
3945 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
3946 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
3947 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
3950 case TGSI_PROCESSOR_FRAGMENT
:
3951 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
3952 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
3953 last_sgpr
= SI_PARAM_PRIM_MASK
;
3954 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
3955 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
3956 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
3957 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
3958 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
3959 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
3960 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
3961 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
3962 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
3963 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
3964 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
3965 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
3966 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
3967 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
3968 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
3969 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->f32
;
3970 num_params
= SI_PARAM_POS_FIXED_PT
+1;
3972 if (!ctx
->is_monolithic
) {
3973 /* Outputs for the epilog. */
3974 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
3977 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
3978 shader
->selector
->info
.writes_z
+
3979 shader
->selector
->info
.writes_stencil
+
3980 shader
->selector
->info
.writes_samplemask
+
3981 1 /* SampleMaskIn */;
3983 num_returns
= MAX2(num_returns
,
3985 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
3987 for (i
= 0; i
< num_return_sgprs
; i
++)
3988 returns
[i
] = ctx
->i32
;
3989 for (; i
< num_returns
; i
++)
3990 returns
[i
] = ctx
->f32
;
3995 assert(0 && "unimplemented shader");
3999 assert(num_params
<= Elements(params
));
4001 si_create_function(ctx
, returns
, num_returns
, params
,
4002 num_params
, last_array_pointer
, last_sgpr
);
4004 shader
->num_input_sgprs
= 0;
4005 shader
->num_input_vgprs
= 0;
4007 for (i
= 0; i
<= last_sgpr
; ++i
)
4008 shader
->num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
4010 /* Unused fragment shader inputs are eliminated by the compiler,
4011 * so we don't know yet how many there will be.
4013 if (ctx
->type
!= TGSI_PROCESSOR_FRAGMENT
)
4014 for (; i
< num_params
; ++i
)
4015 shader
->num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
4017 if (bld_base
->info
&&
4018 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
4019 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
4020 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
4021 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
4022 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
4023 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
4025 LLVMAddGlobalInAddressSpace(gallivm
->module
,
4026 LLVMArrayType(ctx
->i32
, 64),
4030 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
4031 ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
4032 ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
)
4033 declare_tess_lds(ctx
);
4036 static void preload_constants(struct si_shader_context
*ctx
)
4038 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4039 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4040 const struct tgsi_shader_info
*info
= bld_base
->info
;
4042 LLVMValueRef ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
4044 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
4045 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
4050 /* Allocate space for the constant values */
4051 ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
4053 /* Load the resource descriptor */
4054 ctx
->const_buffers
[buf
] =
4055 build_indexed_load_const(ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
4057 /* Load the constants, we rely on the code sinking to do the rest */
4058 for (i
= 0; i
< num_const
* 4; ++i
) {
4059 ctx
->constants
[buf
][i
] =
4060 buffer_load_const(gallivm
->builder
,
4061 ctx
->const_buffers
[buf
],
4062 lp_build_const_int32(gallivm
, i
* 4),
4068 static void preload_samplers(struct si_shader_context
*ctx
)
4070 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4071 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4072 const struct tgsi_shader_info
*info
= bld_base
->info
;
4073 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
4074 LLVMValueRef offset
;
4076 if (num_samplers
== 0)
4079 /* Load the resources and samplers, we rely on the code sinking to do the rest */
4080 for (i
= 0; i
< num_samplers
; ++i
) {
4082 offset
= lp_build_const_int32(gallivm
, i
);
4083 ctx
->sampler_views
[i
] =
4084 get_sampler_desc(ctx
, offset
, DESC_IMAGE
);
4086 /* FMASK resource */
4087 if (info
->is_msaa_sampler
[i
])
4089 get_sampler_desc(ctx
, offset
, DESC_FMASK
);
4091 ctx
->sampler_states
[i
] =
4092 get_sampler_desc(ctx
, offset
, DESC_SAMPLER
);
4096 static void preload_streamout_buffers(struct si_shader_context
*ctx
)
4098 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4099 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4102 /* Streamout can only be used if the shader is compiled as VS. */
4103 if (!ctx
->shader
->selector
->so
.num_outputs
||
4104 (ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4105 (ctx
->shader
->key
.vs
.as_es
||
4106 ctx
->shader
->key
.vs
.as_ls
)) ||
4107 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4108 ctx
->shader
->key
.tes
.as_es
))
4111 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4112 SI_PARAM_RW_BUFFERS
);
4114 /* Load the resources, we rely on the code sinking to do the rest */
4115 for (i
= 0; i
< 4; ++i
) {
4116 if (ctx
->shader
->selector
->so
.stride
[i
]) {
4117 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
4118 SI_SO_BUF_OFFSET
+ i
);
4120 ctx
->so_buffers
[i
] = build_indexed_load_const(ctx
, buf_ptr
, offset
);
4126 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
4129 static void preload_ring_buffers(struct si_shader_context
*ctx
)
4131 struct gallivm_state
*gallivm
=
4132 ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
4134 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4135 SI_PARAM_RW_BUFFERS
);
4137 if ((ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
4138 ctx
->shader
->key
.vs
.as_es
) ||
4139 (ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
4140 ctx
->shader
->key
.tes
.as_es
) ||
4141 ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4142 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
4145 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4148 if (ctx
->is_gs_copy_shader
) {
4149 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
4152 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4154 if (ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
4156 for (i
= 0; i
< 4; i
++) {
4157 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
4160 build_indexed_load_const(ctx
, buf_ptr
, offset
);
4165 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
4166 struct si_shader_config
*conf
,
4167 unsigned symbol_offset
)
4170 const unsigned char *config
=
4171 radeon_shader_binary_config_start(binary
, symbol_offset
);
4173 /* XXX: We may be able to emit some of these values directly rather than
4174 * extracting fields to be emitted later.
4177 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
4178 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
4179 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
4181 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
4182 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
4183 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
4184 case R_00B848_COMPUTE_PGM_RSRC1
:
4185 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
4186 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
4187 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
4188 conf
->rsrc1
= value
;
4190 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
4191 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
4193 case R_00B84C_COMPUTE_PGM_RSRC2
:
4194 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
4195 conf
->rsrc2
= value
;
4197 case R_0286CC_SPI_PS_INPUT_ENA
:
4198 conf
->spi_ps_input_ena
= value
;
4200 case R_0286D0_SPI_PS_INPUT_ADDR
:
4201 conf
->spi_ps_input_addr
= value
;
4203 case R_0286E8_SPI_TMPRING_SIZE
:
4204 case R_00B860_COMPUTE_TMPRING_SIZE
:
4205 /* WAVESIZE is in units of 256 dwords. */
4206 conf
->scratch_bytes_per_wave
=
4207 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
4211 static bool printed
;
4214 fprintf(stderr
, "Warning: LLVM emitted unknown "
4215 "config register: 0x%x\n", reg
);
4222 if (!conf
->spi_ps_input_addr
)
4223 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
4227 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
4228 struct si_shader
*shader
,
4229 uint64_t scratch_va
)
4232 uint32_t scratch_rsrc_dword0
= scratch_va
;
4233 uint32_t scratch_rsrc_dword1
=
4234 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
4235 | S_008F04_STRIDE(shader
->config
.scratch_bytes_per_wave
/ 64);
4237 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
4238 const struct radeon_shader_reloc
*reloc
=
4239 &shader
->binary
.relocs
[i
];
4240 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
4241 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4242 &scratch_rsrc_dword0
, 4);
4243 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
4244 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
4245 &scratch_rsrc_dword1
, 4);
4250 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
4252 unsigned size
= shader
->binary
.code_size
;
4255 size
+= shader
->prolog
->binary
.code_size
;
4257 size
+= shader
->epilog
->binary
.code_size
;
4261 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
4263 const struct radeon_shader_binary
*prolog
=
4264 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
4265 const struct radeon_shader_binary
*epilog
=
4266 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
4267 const struct radeon_shader_binary
*mainb
= &shader
->binary
;
4268 unsigned bo_size
= si_get_shader_binary_size(shader
) +
4269 (!epilog
? mainb
->rodata_size
: 0);
4272 assert(!prolog
|| !prolog
->rodata_size
);
4273 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
4274 assert(!epilog
|| !epilog
->rodata_size
);
4276 r600_resource_reference(&shader
->bo
, NULL
);
4277 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
4278 PIPE_USAGE_IMMUTABLE
,
4284 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
4285 PIPE_TRANSFER_READ_WRITE
);
4288 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
4289 ptr
+= prolog
->code_size
;
4292 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
4293 ptr
+= mainb
->code_size
;
4296 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
4297 else if (mainb
->rodata_size
> 0)
4298 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
4300 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
4304 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
4305 struct pipe_debug_callback
*debug
,
4311 if (binary
->disasm_string
) {
4312 fprintf(stderr
, "Shader %s disassembly:\n", name
);
4313 fprintf(stderr
, "%s", binary
->disasm_string
);
4315 if (debug
&& debug
->debug_message
) {
4316 /* Very long debug messages are cut off, so send the
4317 * disassembly one line at a time. This causes more
4318 * overhead, but on the plus side it simplifies
4319 * parsing of resulting logs.
4321 pipe_debug_message(debug
, SHADER_INFO
,
4322 "Shader Disassembly Begin");
4324 line
= binary
->disasm_string
;
4326 p
= strchrnul(line
, '\n');
4330 pipe_debug_message(debug
, SHADER_INFO
,
4331 "%.*s", count
, line
);
4339 pipe_debug_message(debug
, SHADER_INFO
,
4340 "Shader Disassembly End");
4343 fprintf(stderr
, "Shader %s binary:\n", name
);
4344 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
4345 fprintf(stderr
, "@0x%x: %02x%02x%02x%02x\n", i
,
4346 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
4347 binary
->code
[i
+ 1], binary
->code
[i
]);
4352 static void si_shader_dump_stats(struct si_screen
*sscreen
,
4353 struct si_shader_config
*conf
,
4354 unsigned num_inputs
,
4356 struct pipe_debug_callback
*debug
,
4359 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
4360 unsigned lds_per_wave
= 0;
4361 unsigned max_simd_waves
= 10;
4363 /* Compute LDS usage for PS. */
4364 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4365 /* The minimum usage per wave is (num_inputs * 36). The maximum
4366 * usage is (num_inputs * 36 * 16).
4367 * We can get anything in between and it varies between waves.
4369 * Other stages don't know the size at compile time or don't
4370 * allocate LDS per wave, but instead they do it per thread group.
4372 lds_per_wave
= conf
->lds_size
* lds_increment
+
4373 align(num_inputs
* 36, lds_increment
);
4376 /* Compute the per-SIMD wave counts. */
4377 if (conf
->num_sgprs
) {
4378 if (sscreen
->b
.chip_class
>= VI
)
4379 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
4381 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
4384 if (conf
->num_vgprs
)
4385 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
4387 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
4391 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
4393 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
4394 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4395 fprintf(stderr
, "*** SHADER CONFIG ***\n"
4396 "SPI_PS_INPUT_ADDR = 0x%04x\n"
4397 "SPI_PS_INPUT_ENA = 0x%04x\n",
4398 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
4401 fprintf(stderr
, "*** SHADER STATS ***\n"
4404 "Code Size: %d bytes\n"
4406 "Scratch: %d bytes per wave\n"
4408 "********************\n",
4409 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4410 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4414 pipe_debug_message(debug
, SHADER_INFO
,
4415 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
4416 "LDS: %d Scratch: %d Max Waves: %d",
4417 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4418 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4422 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
4423 struct pipe_debug_callback
*debug
, unsigned processor
)
4425 if (r600_can_dump_shader(&sscreen
->b
, processor
) &&
4426 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
)) {
4427 fprintf(stderr
, "\n");
4430 si_shader_dump_disassembly(&shader
->prolog
->binary
,
4433 si_shader_dump_disassembly(&shader
->binary
, debug
, "main");
4436 si_shader_dump_disassembly(&shader
->epilog
->binary
,
4438 fprintf(stderr
, "\n");
4441 si_shader_dump_stats(sscreen
, &shader
->config
,
4442 shader
->selector
? shader
->selector
->info
.num_inputs
: 0,
4443 si_get_shader_binary_size(shader
), debug
, processor
);
4446 int si_compile_llvm(struct si_screen
*sscreen
,
4447 struct radeon_shader_binary
*binary
,
4448 struct si_shader_config
*conf
,
4449 LLVMTargetMachineRef tm
,
4451 struct pipe_debug_callback
*debug
,
4456 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
4458 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
4459 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
4461 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
4462 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
4463 LLVMDumpModule(mod
);
4464 fprintf(stderr
, "\n");
4468 if (!si_replace_shader(count
, binary
)) {
4469 r
= radeon_llvm_compile(mod
, binary
,
4470 r600_get_llvm_processor_name(sscreen
->b
.family
), tm
,
4476 si_shader_binary_read_config(binary
, conf
, 0);
4478 /* Enable 64-bit and 16-bit denormals, because there is no performance
4481 * If denormals are enabled, all floating-point output modifiers are
4484 * Don't enable denormals for 32-bit floats, because:
4485 * - Floating-point output modifiers would be ignored by the hw.
4486 * - Some opcodes don't support denormals, such as v_mad_f32. We would
4487 * have to stop using those.
4488 * - SI & CI would be very slow.
4490 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
4492 FREE(binary
->config
);
4493 FREE(binary
->global_symbol_offsets
);
4494 binary
->config
= NULL
;
4495 binary
->global_symbol_offsets
= NULL
;
4497 /* Some shaders can't have rodata because their binaries can be
4500 if (binary
->rodata_size
&&
4501 (processor
== TGSI_PROCESSOR_VERTEX
||
4502 processor
== TGSI_PROCESSOR_TESS_CTRL
||
4503 processor
== TGSI_PROCESSOR_TESS_EVAL
||
4504 processor
== TGSI_PROCESSOR_FRAGMENT
)) {
4505 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
4512 /* Generate code for the hardware VS shader stage to go with a geometry shader */
4513 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
4514 struct si_shader_context
*ctx
,
4515 struct si_shader
*gs
,
4516 struct pipe_debug_callback
*debug
)
4518 struct gallivm_state
*gallivm
= &ctx
->radeon_bld
.gallivm
;
4519 struct lp_build_tgsi_context
*bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4520 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
4521 struct si_shader_output_values
*outputs
;
4522 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
4523 LLVMValueRef args
[9];
4526 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
4528 si_init_shader_ctx(ctx
, sscreen
, ctx
->shader
, ctx
->tm
, gsinfo
);
4529 ctx
->type
= TGSI_PROCESSOR_VERTEX
;
4530 ctx
->is_gs_copy_shader
= true;
4532 create_meta_data(ctx
);
4533 create_function(ctx
);
4534 preload_streamout_buffers(ctx
);
4535 preload_ring_buffers(ctx
);
4537 args
[0] = ctx
->gsvs_ring
[0];
4538 args
[1] = lp_build_mul_imm(uint
,
4539 LLVMGetParam(ctx
->radeon_bld
.main_fn
,
4540 ctx
->param_vertex_id
),
4542 args
[3] = uint
->zero
;
4543 args
[4] = uint
->one
; /* OFFEN */
4544 args
[5] = uint
->zero
; /* IDXEN */
4545 args
[6] = uint
->one
; /* GLC */
4546 args
[7] = uint
->one
; /* SLC */
4547 args
[8] = uint
->zero
; /* TFE */
4549 /* Fetch vertex data from GSVS ring */
4550 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
4553 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
4554 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
4556 for (chan
= 0; chan
< 4; chan
++) {
4557 args
[2] = lp_build_const_int32(gallivm
,
4559 gs
->selector
->gs_max_out_vertices
* 16 * 4);
4561 outputs
[i
].values
[chan
] =
4562 LLVMBuildBitCast(gallivm
->builder
,
4563 lp_build_intrinsic(gallivm
->builder
,
4564 "llvm.SI.buffer.load.dword.i32.i32",
4566 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
4571 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
4573 LLVMBuildRet(gallivm
->builder
, ctx
->return_value
);
4575 /* Dump LLVM IR before any optimization passes */
4576 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
4577 r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
4578 LLVMDumpModule(bld_base
->base
.gallivm
->module
);
4580 radeon_llvm_finalize_module(&ctx
->radeon_bld
);
4582 r
= si_compile_llvm(sscreen
, &ctx
->shader
->binary
,
4583 &ctx
->shader
->config
, ctx
->tm
,
4584 bld_base
->base
.gallivm
->module
,
4585 debug
, TGSI_PROCESSOR_GEOMETRY
,
4588 if (r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
4589 fprintf(stderr
, "GS Copy Shader:\n");
4590 si_shader_dump(sscreen
, ctx
->shader
, debug
,
4591 TGSI_PROCESSOR_GEOMETRY
);
4592 r
= si_shader_binary_upload(sscreen
, ctx
->shader
);
4595 radeon_llvm_dispose(&ctx
->radeon_bld
);
4601 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
4605 fprintf(f
, "SHADER KEY\n");
4608 case PIPE_SHADER_VERTEX
:
4609 fprintf(f
, " instance_divisors = {");
4610 for (i
= 0; i
< Elements(key
->vs
.prolog
.instance_divisors
); i
++)
4611 fprintf(f
, !i
? "%u" : ", %u",
4612 key
->vs
.prolog
.instance_divisors
[i
]);
4614 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
4615 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
4616 fprintf(f
, " export_prim_id = %u\n", key
->vs
.epilog
.export_prim_id
);
4619 case PIPE_SHADER_TESS_CTRL
:
4620 fprintf(f
, " prim_mode = %u\n", key
->tcs
.epilog
.prim_mode
);
4623 case PIPE_SHADER_TESS_EVAL
:
4624 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
4625 fprintf(f
, " export_prim_id = %u\n", key
->tes
.epilog
.export_prim_id
);
4628 case PIPE_SHADER_GEOMETRY
:
4631 case PIPE_SHADER_FRAGMENT
:
4632 fprintf(f
, " prolog.color_two_side = %u\n", key
->ps
.prolog
.color_two_side
);
4633 fprintf(f
, " prolog.poly_stipple = %u\n", key
->ps
.prolog
.poly_stipple
);
4634 fprintf(f
, " prolog.force_persample_interp = %u\n", key
->ps
.prolog
.force_persample_interp
);
4635 fprintf(f
, " epilog.spi_shader_col_format = 0x%x\n", key
->ps
.epilog
.spi_shader_col_format
);
4636 fprintf(f
, " epilog.color_is_int8 = 0x%X\n", key
->ps
.epilog
.color_is_int8
);
4637 fprintf(f
, " epilog.last_cbuf = %u\n", key
->ps
.epilog
.last_cbuf
);
4638 fprintf(f
, " epilog.alpha_func = %u\n", key
->ps
.epilog
.alpha_func
);
4639 fprintf(f
, " epilog.alpha_to_one = %u\n", key
->ps
.epilog
.alpha_to_one
);
4640 fprintf(f
, " epilog.poly_line_smoothing = %u\n", key
->ps
.epilog
.poly_line_smoothing
);
4641 fprintf(f
, " epilog.clamp_color = %u\n", key
->ps
.epilog
.clamp_color
);
4649 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
4650 struct si_screen
*sscreen
,
4651 struct si_shader
*shader
,
4652 LLVMTargetMachineRef tm
,
4653 struct tgsi_shader_info
*info
)
4655 struct lp_build_tgsi_context
*bld_base
;
4657 memset(ctx
, 0, sizeof(*ctx
));
4658 radeon_llvm_context_init(&ctx
->radeon_bld
, "amdgcn--");
4660 ctx
->screen
= sscreen
;
4661 if (shader
&& shader
->selector
)
4662 ctx
->type
= shader
->selector
->info
.processor
;
4665 ctx
->shader
= shader
;
4667 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4668 ctx
->i1
= LLVMInt1TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4669 ctx
->i8
= LLVMInt8TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4670 ctx
->i32
= LLVMInt32TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4671 ctx
->i64
= LLVMInt64TypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4672 ctx
->i128
= LLVMIntTypeInContext(ctx
->radeon_bld
.gallivm
.context
, 128);
4673 ctx
->f32
= LLVMFloatTypeInContext(ctx
->radeon_bld
.gallivm
.context
);
4674 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
4675 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
4676 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
4677 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
4678 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
4680 bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4681 bld_base
->info
= info
;
4682 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
4684 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
4685 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
4686 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
4688 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
4689 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
4690 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
4691 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
4692 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
4693 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
4694 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
4695 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
4696 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
4697 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
4698 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
4699 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
4700 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
4702 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
4703 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
4704 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
4705 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
4707 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
4708 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
4709 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
4711 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
4712 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
4713 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
4714 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
4717 static int si_compile_tgsi_shader(struct si_screen
*sscreen
,
4718 LLVMTargetMachineRef tm
,
4719 struct si_shader
*shader
,
4721 struct pipe_debug_callback
*debug
)
4723 struct si_shader_selector
*sel
= shader
->selector
;
4724 struct tgsi_token
*tokens
= sel
->tokens
;
4725 struct si_shader_context ctx
;
4726 struct lp_build_tgsi_context
*bld_base
;
4727 struct tgsi_shader_info stipple_shader_info
;
4730 bool poly_stipple
= sel
->type
== PIPE_SHADER_FRAGMENT
&&
4731 shader
->key
.ps
.prolog
.poly_stipple
;
4734 tokens
= util_pstipple_create_fragment_shader(tokens
, NULL
,
4735 SI_POLY_STIPPLE_SAMPLER
,
4736 TGSI_FILE_SYSTEM_VALUE
);
4737 tgsi_scan_shader(tokens
, &stipple_shader_info
);
4740 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
4741 * conversion fails. */
4742 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
4743 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
4744 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
4745 tgsi_dump(tokens
, 0);
4746 si_dump_streamout(&sel
->so
);
4749 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
,
4750 poly_stipple
? &stipple_shader_info
: &sel
->info
);
4751 ctx
.is_monolithic
= is_monolithic
;
4753 shader
->uses_instanceid
= sel
->info
.uses_instanceid
;
4755 bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
4756 ctx
.radeon_bld
.load_system_value
= declare_system_value
;
4759 case TGSI_PROCESSOR_VERTEX
:
4760 ctx
.radeon_bld
.load_input
= declare_input_vs
;
4761 if (shader
->key
.vs
.as_ls
)
4762 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
4763 else if (shader
->key
.vs
.as_es
)
4764 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4766 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4768 case TGSI_PROCESSOR_TESS_CTRL
:
4769 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
4770 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
4771 bld_base
->emit_store
= store_output_tcs
;
4772 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
4774 case TGSI_PROCESSOR_TESS_EVAL
:
4775 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
4776 if (shader
->key
.tes
.as_es
)
4777 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4779 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4781 case TGSI_PROCESSOR_GEOMETRY
:
4782 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
4783 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
4785 case TGSI_PROCESSOR_FRAGMENT
:
4786 ctx
.radeon_bld
.load_input
= declare_input_fs
;
4788 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
4790 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
4793 assert(!"Unsupported shader type");
4797 create_meta_data(&ctx
);
4798 create_function(&ctx
);
4799 preload_constants(&ctx
);
4800 preload_samplers(&ctx
);
4801 preload_streamout_buffers(&ctx
);
4802 preload_ring_buffers(&ctx
);
4804 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4806 for (i
= 0; i
< 4; i
++) {
4807 ctx
.gs_next_vertex
[i
] =
4808 lp_build_alloca(bld_base
->base
.gallivm
,
4813 if (!lp_build_tgsi_llvm(bld_base
, tokens
)) {
4814 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
4818 LLVMBuildRet(bld_base
->base
.gallivm
->builder
, ctx
.return_value
);
4819 mod
= bld_base
->base
.gallivm
->module
;
4821 /* Dump LLVM IR before any optimization passes */
4822 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
4823 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
4824 LLVMDumpModule(mod
);
4826 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
4828 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
4829 mod
, debug
, ctx
.type
, "TGSI shader");
4831 fprintf(stderr
, "LLVM failed to compile shader\n");
4835 radeon_llvm_dispose(&ctx
.radeon_bld
);
4837 /* Calculate the number of fragment input VGPRs. */
4838 if (ctx
.type
== TGSI_PROCESSOR_FRAGMENT
) {
4839 shader
->num_input_vgprs
= 0;
4840 shader
->face_vgpr_index
= -1;
4842 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
4843 shader
->num_input_vgprs
+= 2;
4844 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
4845 shader
->num_input_vgprs
+= 2;
4846 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
4847 shader
->num_input_vgprs
+= 2;
4848 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
4849 shader
->num_input_vgprs
+= 3;
4850 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
4851 shader
->num_input_vgprs
+= 2;
4852 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
4853 shader
->num_input_vgprs
+= 2;
4854 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
4855 shader
->num_input_vgprs
+= 2;
4856 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
4857 shader
->num_input_vgprs
+= 1;
4858 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
4859 shader
->num_input_vgprs
+= 1;
4860 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
4861 shader
->num_input_vgprs
+= 1;
4862 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
4863 shader
->num_input_vgprs
+= 1;
4864 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
4865 shader
->num_input_vgprs
+= 1;
4866 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
4867 shader
->face_vgpr_index
= shader
->num_input_vgprs
;
4868 shader
->num_input_vgprs
+= 1;
4870 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
4871 shader
->num_input_vgprs
+= 1;
4872 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
4873 shader
->num_input_vgprs
+= 1;
4874 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
4875 shader
->num_input_vgprs
+= 1;
4878 if (ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4879 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
4880 shader
->gs_copy_shader
->selector
= shader
->selector
;
4881 ctx
.shader
= shader
->gs_copy_shader
;
4882 if ((r
= si_generate_gs_copy_shader(sscreen
, &ctx
,
4884 free(shader
->gs_copy_shader
);
4885 shader
->gs_copy_shader
= NULL
;
4891 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
4892 FREE(ctx
.constants
[i
]);
4894 tgsi_free_tokens(tokens
);
4899 * Create, compile and return a shader part (prolog or epilog).
4901 * \param sscreen screen
4902 * \param list list of shader parts of the same category
4903 * \param key shader part key
4904 * \param tm LLVM target machine
4905 * \param debug debug callback
4906 * \param compile the callback responsible for compilation
4907 * \return non-NULL on success
4909 static struct si_shader_part
*
4910 si_get_shader_part(struct si_screen
*sscreen
,
4911 struct si_shader_part
**list
,
4912 union si_shader_part_key
*key
,
4913 LLVMTargetMachineRef tm
,
4914 struct pipe_debug_callback
*debug
,
4915 bool (*compile
)(struct si_screen
*,
4916 LLVMTargetMachineRef
,
4917 struct pipe_debug_callback
*,
4918 struct si_shader_part
*))
4920 struct si_shader_part
*result
;
4922 pipe_mutex_lock(sscreen
->shader_parts_mutex
);
4924 /* Find existing. */
4925 for (result
= *list
; result
; result
= result
->next
) {
4926 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
4927 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
4932 /* Compile a new one. */
4933 result
= CALLOC_STRUCT(si_shader_part
);
4935 if (!compile(sscreen
, tm
, debug
, result
)) {
4937 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
4941 result
->next
= *list
;
4943 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
4948 * Create a vertex shader prolog.
4950 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
4951 * All inputs are returned unmodified. The vertex load indices are
4952 * stored after them, which will used by the API VS for fetching inputs.
4954 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
4959 * (VertexID + BaseVertex),
4960 * (InstanceID + StartInstance),
4961 * (InstanceID / 2 + StartInstance)
4963 static bool si_compile_vs_prolog(struct si_screen
*sscreen
,
4964 LLVMTargetMachineRef tm
,
4965 struct pipe_debug_callback
*debug
,
4966 struct si_shader_part
*out
)
4968 union si_shader_part_key
*key
= &out
->key
;
4969 struct si_shader shader
= {};
4970 struct si_shader_context ctx
;
4971 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
4972 LLVMTypeRef
*params
, *returns
;
4973 LLVMValueRef ret
, func
;
4974 int last_sgpr
, num_params
, num_returns
, i
;
4977 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
, NULL
);
4978 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
4979 ctx
.param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
4980 ctx
.param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
4982 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
4983 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
4984 sizeof(LLVMTypeRef
));
4985 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
4986 key
->vs_prolog
.last_input
+ 1) *
4987 sizeof(LLVMTypeRef
));
4991 /* Declare input and output SGPRs. */
4993 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
4994 params
[num_params
++] = ctx
.i32
;
4995 returns
[num_returns
++] = ctx
.i32
;
4997 last_sgpr
= num_params
- 1;
4999 /* 4 preloaded VGPRs (outputs must be floats) */
5000 for (i
= 0; i
< 4; i
++) {
5001 params
[num_params
++] = ctx
.i32
;
5002 returns
[num_returns
++] = ctx
.f32
;
5005 /* Vertex load indices. */
5006 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
5007 returns
[num_returns
++] = ctx
.f32
;
5009 /* Create the function. */
5010 si_create_function(&ctx
, returns
, num_returns
, params
,
5011 num_params
, -1, last_sgpr
);
5012 func
= ctx
.radeon_bld
.main_fn
;
5014 /* Copy inputs to outputs. This should be no-op, as the registers match,
5015 * but it will prevent the compiler from overwriting them unintentionally.
5017 ret
= ctx
.return_value
;
5018 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
5019 LLVMValueRef p
= LLVMGetParam(func
, i
);
5020 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5022 for (i
= num_params
- 4; i
< num_params
; i
++) {
5023 LLVMValueRef p
= LLVMGetParam(func
, i
);
5024 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
.f32
, "");
5025 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
5028 /* Compute vertex load indices from instance divisors. */
5029 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
5030 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
5034 /* InstanceID / Divisor + StartInstance */
5035 index
= get_instance_index_for_fetch(&ctx
.radeon_bld
,
5036 SI_SGPR_START_INSTANCE
,
5039 /* VertexID + BaseVertex */
5040 index
= LLVMBuildAdd(gallivm
->builder
,
5041 LLVMGetParam(func
, ctx
.param_vertex_id
),
5042 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
5045 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
.f32
, "");
5046 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
5051 LLVMBuildRet(gallivm
->builder
, ret
);
5052 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5054 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5055 gallivm
->module
, debug
, ctx
.type
,
5056 "Vertex Shader Prolog"))
5059 radeon_llvm_dispose(&ctx
.radeon_bld
);
5064 * Compile the vertex shader epilog. This is also used by the tessellation
5065 * evaluation shader compiled as VS.
5067 * The input is PrimitiveID.
5069 * If PrimitiveID is required by the pixel shader, export it.
5070 * Otherwise, do nothing.
5072 static bool si_compile_vs_epilog(struct si_screen
*sscreen
,
5073 LLVMTargetMachineRef tm
,
5074 struct pipe_debug_callback
*debug
,
5075 struct si_shader_part
*out
)
5077 union si_shader_part_key
*key
= &out
->key
;
5078 struct si_shader_context ctx
;
5079 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5080 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5081 LLVMTypeRef params
[5];
5085 si_init_shader_ctx(&ctx
, sscreen
, NULL
, tm
, NULL
);
5086 ctx
.type
= TGSI_PROCESSOR_VERTEX
;
5088 /* Declare input VGPRs. */
5089 num_params
= key
->vs_epilog
.states
.export_prim_id
?
5090 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
5091 assert(num_params
<= ARRAY_SIZE(params
));
5093 for (i
= 0; i
< num_params
; i
++)
5094 params
[i
] = ctx
.f32
;
5096 /* Create the function. */
5097 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5101 if (key
->vs_epilog
.states
.export_prim_id
) {
5102 struct lp_build_context
*base
= &bld_base
->base
;
5103 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5104 LLVMValueRef args
[9];
5106 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
5107 args
[1] = uint
->zero
; /* whether the EXEC mask is valid */
5108 args
[2] = uint
->zero
; /* DONE bit */
5109 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_PARAM
+
5110 key
->vs_epilog
.prim_id_param_offset
);
5111 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
5112 args
[5] = LLVMGetParam(ctx
.radeon_bld
.main_fn
,
5113 VS_EPILOG_PRIMID_LOC
); /* X */
5114 args
[6] = uint
->undef
; /* Y */
5115 args
[7] = uint
->undef
; /* Z */
5116 args
[8] = uint
->undef
; /* W */
5118 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
5119 LLVMVoidTypeInContext(base
->gallivm
->context
),
5124 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5125 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5127 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5128 gallivm
->module
, debug
, ctx
.type
,
5129 "Vertex Shader Epilog"))
5132 radeon_llvm_dispose(&ctx
.radeon_bld
);
5137 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
5139 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
5140 LLVMTargetMachineRef tm
,
5141 struct si_shader
*shader
,
5142 struct pipe_debug_callback
*debug
,
5143 struct si_vs_epilog_bits
*states
)
5145 union si_shader_part_key epilog_key
;
5147 memset(&epilog_key
, 0, sizeof(epilog_key
));
5148 epilog_key
.vs_epilog
.states
= *states
;
5150 /* Set up the PrimitiveID output. */
5151 if (shader
->key
.vs
.epilog
.export_prim_id
) {
5152 unsigned index
= shader
->selector
->info
.num_outputs
;
5153 unsigned offset
= shader
->nr_param_exports
++;
5155 epilog_key
.vs_epilog
.prim_id_param_offset
= offset
;
5156 shader
->vs_output_param_offset
[index
] = offset
;
5159 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
5160 &epilog_key
, tm
, debug
,
5161 si_compile_vs_epilog
);
5162 return shader
->epilog
!= NULL
;
5166 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
5168 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
5169 LLVMTargetMachineRef tm
,
5170 struct si_shader
*shader
,
5171 struct pipe_debug_callback
*debug
)
5173 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5174 union si_shader_part_key prolog_key
;
5177 /* Get the prolog. */
5178 memset(&prolog_key
, 0, sizeof(prolog_key
));
5179 prolog_key
.vs_prolog
.states
= shader
->key
.vs
.prolog
;
5180 prolog_key
.vs_prolog
.num_input_sgprs
= shader
->num_input_sgprs
;
5181 prolog_key
.vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
5183 /* The prolog is a no-op if there are no inputs. */
5184 if (info
->num_inputs
) {
5186 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
5187 &prolog_key
, tm
, debug
,
5188 si_compile_vs_prolog
);
5189 if (!shader
->prolog
)
5193 /* Get the epilog. */
5194 if (!shader
->key
.vs
.as_es
&& !shader
->key
.vs
.as_ls
&&
5195 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5196 &shader
->key
.vs
.epilog
))
5199 /* Set the instanceID flag. */
5200 for (i
= 0; i
< info
->num_inputs
; i
++)
5201 if (prolog_key
.vs_prolog
.states
.instance_divisors
[i
])
5202 shader
->uses_instanceid
= true;
5208 * Select and compile (or reuse) TES parts (epilog).
5210 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
5211 LLVMTargetMachineRef tm
,
5212 struct si_shader
*shader
,
5213 struct pipe_debug_callback
*debug
)
5215 if (shader
->key
.tes
.as_es
)
5218 /* TES compiled as VS. */
5219 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
5220 &shader
->key
.tes
.epilog
);
5224 * Compile the TCS epilog. This writes tesselation factors to memory based on
5225 * the output primitive type of the tesselator (determined by TES).
5227 static bool si_compile_tcs_epilog(struct si_screen
*sscreen
,
5228 LLVMTargetMachineRef tm
,
5229 struct pipe_debug_callback
*debug
,
5230 struct si_shader_part
*out
)
5232 union si_shader_part_key
*key
= &out
->key
;
5233 struct si_shader shader
= {};
5234 struct si_shader_context ctx
;
5235 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5236 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5237 LLVMTypeRef params
[16];
5239 int last_array_pointer
, last_sgpr
, num_params
;
5242 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
, NULL
);
5243 ctx
.type
= TGSI_PROCESSOR_TESS_CTRL
;
5244 shader
.key
.tcs
.epilog
= key
->tcs_epilog
.states
;
5246 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
5247 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
.v16i8
, SI_NUM_RW_BUFFERS
);
5248 last_array_pointer
= SI_PARAM_RW_BUFFERS
;
5249 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
5250 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
5251 params
[SI_PARAM_UNUSED
] = ctx
.i64
;
5252 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
.i32
;
5253 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
.i32
;
5254 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
.i32
;
5255 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
.i32
;
5256 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5257 num_params
= last_sgpr
+ 1;
5259 params
[num_params
++] = ctx
.i32
; /* patch index within the wave (REL_PATCH_ID) */
5260 params
[num_params
++] = ctx
.i32
; /* invocation ID within the patch */
5261 params
[num_params
++] = ctx
.i32
; /* LDS offset where tess factors should be loaded from */
5263 /* Create the function. */
5264 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5265 last_array_pointer
, last_sgpr
);
5266 declare_tess_lds(&ctx
);
5267 func
= ctx
.radeon_bld
.main_fn
;
5269 si_write_tess_factors(bld_base
,
5270 LLVMGetParam(func
, last_sgpr
+ 1),
5271 LLVMGetParam(func
, last_sgpr
+ 2),
5272 LLVMGetParam(func
, last_sgpr
+ 3));
5275 LLVMBuildRet(gallivm
->builder
, ctx
.return_value
);
5276 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5278 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5279 gallivm
->module
, debug
, ctx
.type
,
5280 "Tessellation Control Shader Epilog"))
5283 radeon_llvm_dispose(&ctx
.radeon_bld
);
5288 * Select and compile (or reuse) TCS parts (epilog).
5290 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
5291 LLVMTargetMachineRef tm
,
5292 struct si_shader
*shader
,
5293 struct pipe_debug_callback
*debug
)
5295 union si_shader_part_key epilog_key
;
5297 /* Get the epilog. */
5298 memset(&epilog_key
, 0, sizeof(epilog_key
));
5299 epilog_key
.tcs_epilog
.states
= shader
->key
.tcs
.epilog
;
5301 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
5302 &epilog_key
, tm
, debug
,
5303 si_compile_tcs_epilog
);
5304 return shader
->epilog
!= NULL
;
5308 * Compile the pixel shader epilog. This handles everything that must be
5309 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
5311 static bool si_compile_ps_epilog(struct si_screen
*sscreen
,
5312 LLVMTargetMachineRef tm
,
5313 struct pipe_debug_callback
*debug
,
5314 struct si_shader_part
*out
)
5316 union si_shader_part_key
*key
= &out
->key
;
5317 struct si_shader shader
= {};
5318 struct si_shader_context ctx
;
5319 struct gallivm_state
*gallivm
= &ctx
.radeon_bld
.gallivm
;
5320 struct lp_build_tgsi_context
*bld_base
= &ctx
.radeon_bld
.soa
.bld_base
;
5321 LLVMTypeRef params
[16+8*4+3];
5322 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
5323 int last_array_pointer
, last_sgpr
, num_params
, i
;
5326 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
, NULL
);
5327 ctx
.type
= TGSI_PROCESSOR_FRAGMENT
;
5328 shader
.key
.ps
.epilog
= key
->ps_epilog
.states
;
5330 /* Declare input SGPRs. */
5331 params
[SI_PARAM_RW_BUFFERS
] = ctx
.i64
;
5332 params
[SI_PARAM_CONST_BUFFERS
] = ctx
.i64
;
5333 params
[SI_PARAM_SAMPLERS
] = ctx
.i64
;
5334 params
[SI_PARAM_UNUSED
] = ctx
.i64
;
5335 params
[SI_PARAM_ALPHA_REF
] = ctx
.f32
;
5336 last_array_pointer
= -1;
5337 last_sgpr
= SI_PARAM_ALPHA_REF
;
5339 /* Declare input VGPRs. */
5340 num_params
= (last_sgpr
+ 1) +
5341 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
5342 key
->ps_epilog
.writes_z
+
5343 key
->ps_epilog
.writes_stencil
+
5344 key
->ps_epilog
.writes_samplemask
;
5346 num_params
= MAX2(num_params
,
5347 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5349 assert(num_params
<= ARRAY_SIZE(params
));
5351 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
5352 params
[i
] = ctx
.f32
;
5354 /* Create the function. */
5355 si_create_function(&ctx
, NULL
, 0, params
, num_params
,
5356 last_array_pointer
, last_sgpr
);
5357 /* Disable elimination of unused inputs. */
5358 radeon_llvm_add_attribute(ctx
.radeon_bld
.main_fn
,
5359 "InitialPSInputAddr", 0xffffff);
5361 /* Process colors. */
5362 unsigned vgpr
= last_sgpr
+ 1;
5363 unsigned colors_written
= key
->ps_epilog
.colors_written
;
5364 int last_color_export
= -1;
5366 /* Find the last color export. */
5367 if (!key
->ps_epilog
.writes_z
&&
5368 !key
->ps_epilog
.writes_stencil
&&
5369 !key
->ps_epilog
.writes_samplemask
) {
5370 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
5372 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
5373 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
5374 /* Just set this if any of the colorbuffers are enabled. */
5376 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
5377 last_color_export
= 0;
5379 for (i
= 0; i
< 8; i
++)
5380 if (colors_written
& (1 << i
) &&
5381 (spi_format
>> (i
* 4)) & 0xf)
5382 last_color_export
= i
;
5386 while (colors_written
) {
5387 LLVMValueRef color
[4];
5388 int mrt
= u_bit_scan(&colors_written
);
5390 for (i
= 0; i
< 4; i
++)
5391 color
[i
] = LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5393 si_export_mrt_color(bld_base
, color
, mrt
,
5395 mrt
== last_color_export
);
5398 /* Process depth, stencil, samplemask. */
5399 if (key
->ps_epilog
.writes_z
)
5400 depth
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5401 if (key
->ps_epilog
.writes_stencil
)
5402 stencil
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5403 if (key
->ps_epilog
.writes_samplemask
)
5404 samplemask
= LLVMGetParam(ctx
.radeon_bld
.main_fn
, vgpr
++);
5406 if (depth
|| stencil
|| samplemask
)
5407 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
5408 else if (last_color_export
== -1)
5409 si_export_null(bld_base
);
5412 LLVMBuildRetVoid(gallivm
->builder
);
5413 radeon_llvm_finalize_module(&ctx
.radeon_bld
);
5415 if (si_compile_llvm(sscreen
, &out
->binary
, &out
->config
, tm
,
5416 gallivm
->module
, debug
, ctx
.type
,
5417 "Fragment Shader Epilog"))
5420 radeon_llvm_dispose(&ctx
.radeon_bld
);
5425 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
5427 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
5428 LLVMTargetMachineRef tm
,
5429 struct si_shader
*shader
,
5430 struct pipe_debug_callback
*debug
)
5432 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5433 union si_shader_part_key epilog_key
;
5435 /* Get the epilog. */
5436 memset(&epilog_key
, 0, sizeof(epilog_key
));
5437 epilog_key
.ps_epilog
.colors_written
= info
->colors_written
;
5438 epilog_key
.ps_epilog
.writes_z
= info
->writes_z
;
5439 epilog_key
.ps_epilog
.writes_stencil
= info
->writes_stencil
;
5440 epilog_key
.ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
5441 epilog_key
.ps_epilog
.states
= shader
->key
.ps
.epilog
;
5444 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
5445 &epilog_key
, tm
, debug
,
5446 si_compile_ps_epilog
);
5447 if (!shader
->epilog
)
5450 /* The sample mask input is always enabled, because the API shader always
5451 * passes it through to the epilog. Disable it here if it's unused.
5453 if (!shader
->key
.ps
.epilog
.poly_line_smoothing
&&
5454 !shader
->selector
->info
.reads_samplemask
)
5455 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
5460 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
5461 struct si_shader
*shader
,
5462 struct pipe_debug_callback
*debug
)
5467 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
,
5468 sscreen
->use_monolithic_shaders
, debug
);
5472 if (!sscreen
->use_monolithic_shaders
) {
5473 switch (shader
->selector
->type
) {
5474 case PIPE_SHADER_VERTEX
:
5475 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
5478 case PIPE_SHADER_TESS_CTRL
:
5479 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
5482 case PIPE_SHADER_TESS_EVAL
:
5483 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
5486 case PIPE_SHADER_FRAGMENT
:
5487 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
5490 /* Make sure we have at least as many VGPRs as there
5491 * are allocated inputs.
5493 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
5494 shader
->num_input_vgprs
);
5498 /* Update SGPR and VGPR counts. */
5499 if (shader
->prolog
) {
5500 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
5501 shader
->prolog
->config
.num_sgprs
);
5502 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
5503 shader
->prolog
->config
.num_vgprs
);
5505 if (shader
->epilog
) {
5506 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
5507 shader
->epilog
->config
.num_sgprs
);
5508 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
5509 shader
->epilog
->config
.num_vgprs
);
5513 si_shader_dump(sscreen
, shader
, debug
, shader
->selector
->info
.processor
);
5516 r
= si_shader_binary_upload(sscreen
, shader
);
5518 fprintf(stderr
, "LLVM failed to upload shader\n");
5525 void si_shader_destroy(struct si_shader
*shader
)
5527 if (shader
->gs_copy_shader
) {
5528 si_shader_destroy(shader
->gs_copy_shader
);
5529 FREE(shader
->gs_copy_shader
);
5532 if (shader
->scratch_bo
)
5533 r600_resource_reference(&shader
->scratch_bo
, NULL
);
5535 r600_resource_reference(&shader
->bo
, NULL
);
5537 radeon_shader_binary_clean(&shader
->binary
);