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
;
70 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
71 int param_streamout_config
;
72 int param_streamout_write_index
;
73 int param_streamout_offset
[4];
75 int param_rel_auto_id
;
77 int param_instance_id
;
80 int param_tes_rel_patch_id
;
81 int param_tes_patch_id
;
82 int param_es2gs_offset
;
83 LLVMTargetMachineRef tm
;
84 LLVMValueRef const_md
;
85 LLVMValueRef const_resource
[SI_NUM_CONST_BUFFERS
];
87 LLVMValueRef
*constants
[SI_NUM_CONST_BUFFERS
];
88 LLVMValueRef resources
[SI_NUM_SAMPLER_VIEWS
];
89 LLVMValueRef samplers
[SI_NUM_SAMPLER_STATES
];
90 LLVMValueRef so_buffers
[4];
91 LLVMValueRef esgs_ring
;
92 LLVMValueRef gsvs_ring
[4];
93 LLVMValueRef gs_next_vertex
[4];
96 static struct si_shader_context
* si_shader_context(
97 struct lp_build_tgsi_context
* bld_base
)
99 return (struct si_shader_context
*)bld_base
;
103 #define PERSPECTIVE_BASE 0
104 #define LINEAR_BASE 9
106 #define SAMPLE_OFFSET 0
107 #define CENTER_OFFSET 2
108 #define CENTROID_OFSET 4
110 #define USE_SGPR_MAX_SUFFIX_LEN 5
111 #define CONST_ADDR_SPACE 2
112 #define LOCAL_ADDR_SPACE 3
113 #define USER_SGPR_ADDR_SPACE 8
117 #define SENDMSG_GS_DONE 3
119 #define SENDMSG_GS_OP_NOP (0 << 4)
120 #define SENDMSG_GS_OP_CUT (1 << 4)
121 #define SENDMSG_GS_OP_EMIT (2 << 4)
122 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
125 * Returns a unique index for a semantic name and index. The index must be
126 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
129 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
131 switch (semantic_name
) {
132 case TGSI_SEMANTIC_POSITION
:
134 case TGSI_SEMANTIC_PSIZE
:
136 case TGSI_SEMANTIC_CLIPDIST
:
139 case TGSI_SEMANTIC_GENERIC
:
143 /* same explanation as in the default statement,
144 * the only user hitting this is st/nine.
148 /* patch indices are completely separate and thus start from 0 */
149 case TGSI_SEMANTIC_TESSOUTER
:
151 case TGSI_SEMANTIC_TESSINNER
:
153 case TGSI_SEMANTIC_PATCH
:
157 /* Don't fail here. The result of this function is only used
158 * for LS, TCS, TES, and GS, where legacy GL semantics can't
159 * occur, but this function is called for all vertex shaders
160 * before it's known whether LS will be compiled or not.
167 * Given a semantic name and index of a parameter and a mask of used parameters
168 * (inputs or outputs), return the index of the parameter in the list of all
171 * For example, assume this list of parameters:
172 * POSITION, PSIZE, GENERIC0, GENERIC2
173 * which has the mask:
176 * querying POSITION returns 0,
177 * querying PSIZE returns 1,
178 * querying GENERIC0 returns 2,
179 * querying GENERIC2 returns 3.
181 * Which can be used as an offset to a parameter buffer in units of vec4s.
183 static int get_param_index(unsigned semantic_name
, unsigned index
,
186 unsigned unique_index
= si_shader_io_get_unique_index(semantic_name
, index
);
187 int i
, param_index
= 0;
189 /* If not present... */
190 if (!((1llu << unique_index
) & mask
))
193 for (i
= 0; mask
; i
++) {
194 uint64_t bit
= 1llu << i
;
197 if (i
== unique_index
)
205 assert(!"unreachable");
210 * Get the value of a shader input parameter and extract a bitfield.
212 static LLVMValueRef
unpack_param(struct si_shader_context
*si_shader_ctx
,
213 unsigned param
, unsigned rshift
,
216 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
217 LLVMValueRef value
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
221 value
= LLVMBuildLShr(gallivm
->builder
, value
,
222 lp_build_const_int32(gallivm
, rshift
), "");
224 if (rshift
+ bitwidth
< 32) {
225 unsigned mask
= (1 << bitwidth
) - 1;
226 value
= LLVMBuildAnd(gallivm
->builder
, value
,
227 lp_build_const_int32(gallivm
, mask
), "");
233 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*si_shader_ctx
)
235 switch (si_shader_ctx
->type
) {
236 case TGSI_PROCESSOR_TESS_CTRL
:
237 return unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 0, 8);
239 case TGSI_PROCESSOR_TESS_EVAL
:
240 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
241 si_shader_ctx
->param_tes_rel_patch_id
);
249 /* Tessellation shaders pass outputs to the next shader using LDS.
251 * LS outputs = TCS inputs
252 * TCS outputs = TES inputs
255 * - TCS inputs for patch 0
256 * - TCS inputs for patch 1
257 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
259 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
260 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
261 * - TCS outputs for patch 1
262 * - Per-patch TCS outputs for patch 1
263 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
264 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
267 * All three shaders VS(LS), TCS, TES share the same LDS space.
271 get_tcs_in_patch_stride(struct si_shader_context
*si_shader_ctx
)
273 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
)
274 return unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
275 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
276 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
284 get_tcs_out_patch_stride(struct si_shader_context
*si_shader_ctx
)
286 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
290 get_tcs_out_patch0_offset(struct si_shader_context
*si_shader_ctx
)
292 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
293 unpack_param(si_shader_ctx
,
294 SI_PARAM_TCS_OUT_OFFSETS
,
300 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
302 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
303 unpack_param(si_shader_ctx
,
304 SI_PARAM_TCS_OUT_OFFSETS
,
310 get_tcs_in_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
312 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
313 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(si_shader_ctx
);
314 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
316 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
320 get_tcs_out_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
322 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
323 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(si_shader_ctx
);
324 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
325 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
327 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
328 LLVMBuildMul(gallivm
->builder
, patch_stride
,
334 get_tcs_out_current_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
336 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
337 LLVMValueRef patch0_patch_data_offset
=
338 get_tcs_out_patch0_patch_data_offset(si_shader_ctx
);
339 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
340 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
342 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
343 LLVMBuildMul(gallivm
->builder
, patch_stride
,
348 static void build_indexed_store(struct si_shader_context
*si_shader_ctx
,
349 LLVMValueRef base_ptr
, LLVMValueRef index
,
352 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
353 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
354 LLVMValueRef indices
[2], pointer
;
356 indices
[0] = bld_base
->uint_bld
.zero
;
359 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
360 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
364 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
365 * It's equivalent to doing a load from &base_ptr[index].
367 * \param base_ptr Where the array starts.
368 * \param index The element index into the array.
370 static LLVMValueRef
build_indexed_load(struct si_shader_context
*si_shader_ctx
,
371 LLVMValueRef base_ptr
, LLVMValueRef index
)
373 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
374 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
375 LLVMValueRef indices
[2], pointer
;
377 indices
[0] = bld_base
->uint_bld
.zero
;
380 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
381 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
385 * Do a load from &base_ptr[index], but also add a flag that it's loading
388 static LLVMValueRef
build_indexed_load_const(
389 struct si_shader_context
* si_shader_ctx
,
390 LLVMValueRef base_ptr
, LLVMValueRef index
)
392 LLVMValueRef result
= build_indexed_load(si_shader_ctx
, base_ptr
, index
);
393 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
397 static LLVMValueRef
get_instance_index_for_fetch(
398 struct radeon_llvm_context
* radeon_bld
,
401 struct si_shader_context
*si_shader_ctx
=
402 si_shader_context(&radeon_bld
->soa
.bld_base
);
403 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
405 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
406 si_shader_ctx
->param_instance_id
);
408 /* The division must be done before START_INSTANCE is added. */
410 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
411 lp_build_const_int32(gallivm
, divisor
), "");
413 return LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
414 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
417 static void declare_input_vs(
418 struct radeon_llvm_context
*radeon_bld
,
419 unsigned input_index
,
420 const struct tgsi_full_declaration
*decl
)
422 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
423 struct gallivm_state
*gallivm
= base
->gallivm
;
424 struct si_shader_context
*si_shader_ctx
=
425 si_shader_context(&radeon_bld
->soa
.bld_base
);
426 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
430 LLVMValueRef t_list_ptr
;
431 LLVMValueRef t_offset
;
433 LLVMValueRef attribute_offset
;
434 LLVMValueRef buffer_index
;
435 LLVMValueRef args
[3];
436 LLVMTypeRef vec4_type
;
439 /* Load the T list */
440 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
442 t_offset
= lp_build_const_int32(gallivm
, input_index
);
444 t_list
= build_indexed_load_const(si_shader_ctx
, t_list_ptr
, t_offset
);
446 /* Build the attribute offset */
447 attribute_offset
= lp_build_const_int32(gallivm
, 0);
450 /* Build index from instance ID, start instance and divisor */
451 si_shader_ctx
->shader
->uses_instanceid
= true;
452 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
454 /* Load the buffer index for vertices. */
455 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
456 si_shader_ctx
->param_vertex_id
);
457 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
458 SI_PARAM_BASE_VERTEX
);
459 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
462 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
464 args
[1] = attribute_offset
;
465 args
[2] = buffer_index
;
466 input
= lp_build_intrinsic(gallivm
->builder
,
467 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
468 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
470 /* Break up the vec4 into individual components */
471 for (chan
= 0; chan
< 4; chan
++) {
472 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
473 /* XXX: Use a helper function for this. There is one in
475 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
476 LLVMBuildExtractElement(gallivm
->builder
,
477 input
, llvm_chan
, "");
481 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
484 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
487 return bld_base
->uint_bld
.zero
;
489 switch (si_shader_ctx
->type
) {
490 case TGSI_PROCESSOR_VERTEX
:
491 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
492 si_shader_ctx
->param_vs_prim_id
);
493 case TGSI_PROCESSOR_TESS_CTRL
:
494 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
496 case TGSI_PROCESSOR_TESS_EVAL
:
497 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
498 si_shader_ctx
->param_tes_patch_id
);
499 case TGSI_PROCESSOR_GEOMETRY
:
500 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
501 SI_PARAM_PRIMITIVE_ID
);
504 return bld_base
->uint_bld
.zero
;
509 * Return the value of tgsi_ind_register for indexing.
510 * This is the indirect index with the constant offset added to it.
512 static LLVMValueRef
get_indirect_index(struct si_shader_context
*si_shader_ctx
,
513 const struct tgsi_ind_register
*ind
,
516 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
519 result
= si_shader_ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
520 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
521 result
= LLVMBuildAdd(gallivm
->builder
, result
,
522 lp_build_const_int32(gallivm
, rel_index
), "");
527 * Calculate a dword address given an input or output register and a stride.
529 static LLVMValueRef
get_dw_address(struct si_shader_context
*si_shader_ctx
,
530 const struct tgsi_full_dst_register
*dst
,
531 const struct tgsi_full_src_register
*src
,
532 LLVMValueRef vertex_dw_stride
,
533 LLVMValueRef base_addr
)
535 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
536 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
537 ubyte
*name
, *index
, *array_first
;
539 struct tgsi_full_dst_register reg
;
541 /* Set the register description. The address computation is the same
542 * for sources and destinations. */
544 reg
.Register
.File
= src
->Register
.File
;
545 reg
.Register
.Index
= src
->Register
.Index
;
546 reg
.Register
.Indirect
= src
->Register
.Indirect
;
547 reg
.Register
.Dimension
= src
->Register
.Dimension
;
548 reg
.Indirect
= src
->Indirect
;
549 reg
.Dimension
= src
->Dimension
;
550 reg
.DimIndirect
= src
->DimIndirect
;
554 /* If the register is 2-dimensional (e.g. an array of vertices
555 * in a primitive), calculate the base address of the vertex. */
556 if (reg
.Register
.Dimension
) {
559 if (reg
.Dimension
.Indirect
)
560 index
= get_indirect_index(si_shader_ctx
, ®
.DimIndirect
,
561 reg
.Dimension
.Index
);
563 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
565 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
566 LLVMBuildMul(gallivm
->builder
, index
,
567 vertex_dw_stride
, ""), "");
570 /* Get information about the register. */
571 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
572 name
= info
->input_semantic_name
;
573 index
= info
->input_semantic_index
;
574 array_first
= info
->input_array_first
;
575 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
576 name
= info
->output_semantic_name
;
577 index
= info
->output_semantic_index
;
578 array_first
= info
->output_array_first
;
584 if (reg
.Register
.Indirect
) {
585 /* Add the relative address of the element. */
586 LLVMValueRef ind_index
;
588 if (reg
.Indirect
.ArrayID
)
589 first
= array_first
[reg
.Indirect
.ArrayID
];
591 first
= reg
.Register
.Index
;
593 ind_index
= get_indirect_index(si_shader_ctx
, ®
.Indirect
,
594 reg
.Register
.Index
- first
);
596 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
597 LLVMBuildMul(gallivm
->builder
, ind_index
,
598 lp_build_const_int32(gallivm
, 4), ""), "");
600 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
602 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
603 index
[reg
.Register
.Index
]);
606 /* Add the base address of the element. */
607 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
608 lp_build_const_int32(gallivm
, param
* 4), "");
614 * \param type output value type
615 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
616 * \param dw_addr address in dwords
618 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
619 enum tgsi_opcode_type type
, unsigned swizzle
,
620 LLVMValueRef dw_addr
)
622 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
623 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
627 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
629 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
630 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
632 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
636 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
637 lp_build_const_int32(gallivm
, swizzle
));
639 value
= build_indexed_load(si_shader_ctx
, si_shader_ctx
->lds
, dw_addr
);
640 return LLVMBuildBitCast(gallivm
->builder
, value
,
641 tgsi2llvmtype(bld_base
, type
), "");
647 * \param swizzle offset (typically 0..3)
648 * \param dw_addr address in dwords
649 * \param value value to store
651 static void lds_store(struct lp_build_tgsi_context
* bld_base
,
652 unsigned swizzle
, LLVMValueRef dw_addr
,
655 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
656 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
658 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
659 lp_build_const_int32(gallivm
, swizzle
));
661 value
= LLVMBuildBitCast(gallivm
->builder
, value
,
662 LLVMInt32TypeInContext(gallivm
->context
), "");
663 build_indexed_store(si_shader_ctx
, si_shader_ctx
->lds
,
667 static LLVMValueRef
fetch_input_tcs(
668 struct lp_build_tgsi_context
*bld_base
,
669 const struct tgsi_full_src_register
*reg
,
670 enum tgsi_opcode_type type
, unsigned swizzle
)
672 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
673 LLVMValueRef dw_addr
, stride
;
675 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
676 dw_addr
= get_tcs_in_current_patch_offset(si_shader_ctx
);
677 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
679 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
682 static LLVMValueRef
fetch_output_tcs(
683 struct lp_build_tgsi_context
*bld_base
,
684 const struct tgsi_full_src_register
*reg
,
685 enum tgsi_opcode_type type
, unsigned swizzle
)
687 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
688 LLVMValueRef dw_addr
, stride
;
690 if (reg
->Register
.Dimension
) {
691 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
692 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
693 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
695 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
696 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
699 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
702 static LLVMValueRef
fetch_input_tes(
703 struct lp_build_tgsi_context
*bld_base
,
704 const struct tgsi_full_src_register
*reg
,
705 enum tgsi_opcode_type type
, unsigned swizzle
)
707 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
708 LLVMValueRef dw_addr
, stride
;
710 if (reg
->Register
.Dimension
) {
711 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
712 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
713 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
715 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
716 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
719 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
722 static void store_output_tcs(struct lp_build_tgsi_context
* bld_base
,
723 const struct tgsi_full_instruction
* inst
,
724 const struct tgsi_opcode_info
* info
,
727 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
728 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
730 LLVMValueRef dw_addr
, stride
;
732 /* Only handle per-patch and per-vertex outputs here.
733 * Vectors will be lowered to scalars and this function will be called again.
735 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
736 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
737 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
741 if (reg
->Register
.Dimension
) {
742 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
743 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
744 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, stride
, dw_addr
);
746 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
747 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, NULL
, dw_addr
);
750 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
751 LLVMValueRef value
= dst
[chan_index
];
753 if (inst
->Instruction
.Saturate
)
754 value
= radeon_llvm_saturate(bld_base
, value
);
756 lds_store(bld_base
, chan_index
, dw_addr
, value
);
760 static LLVMValueRef
fetch_input_gs(
761 struct lp_build_tgsi_context
*bld_base
,
762 const struct tgsi_full_src_register
*reg
,
763 enum tgsi_opcode_type type
,
766 struct lp_build_context
*base
= &bld_base
->base
;
767 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
768 struct si_shader
*shader
= si_shader_ctx
->shader
;
769 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
770 struct gallivm_state
*gallivm
= base
->gallivm
;
771 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
772 LLVMValueRef vtx_offset
;
773 LLVMValueRef args
[9];
774 unsigned vtx_offset_param
;
775 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
776 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
777 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
780 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
781 return get_primitive_id(bld_base
, swizzle
);
783 if (!reg
->Register
.Dimension
)
787 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
789 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
790 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
792 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
796 /* Get the vertex offset parameter */
797 vtx_offset_param
= reg
->Dimension
.Index
;
798 if (vtx_offset_param
< 2) {
799 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
801 assert(vtx_offset_param
< 6);
802 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
804 vtx_offset
= lp_build_mul_imm(uint
,
805 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
809 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
810 args
[0] = si_shader_ctx
->esgs_ring
;
811 args
[1] = vtx_offset
;
812 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
813 args
[3] = uint
->zero
;
814 args
[4] = uint
->one
; /* OFFEN */
815 args
[5] = uint
->zero
; /* IDXEN */
816 args
[6] = uint
->one
; /* GLC */
817 args
[7] = uint
->zero
; /* SLC */
818 args
[8] = uint
->zero
; /* TFE */
820 return LLVMBuildBitCast(gallivm
->builder
,
821 lp_build_intrinsic(gallivm
->builder
,
822 "llvm.SI.buffer.load.dword.i32.i32",
824 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
825 tgsi2llvmtype(bld_base
, type
), "");
828 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
830 switch (interpolate
) {
831 case TGSI_INTERPOLATE_CONSTANT
:
834 case TGSI_INTERPOLATE_LINEAR
:
835 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
836 return SI_PARAM_LINEAR_SAMPLE
;
837 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
838 return SI_PARAM_LINEAR_CENTROID
;
840 return SI_PARAM_LINEAR_CENTER
;
842 case TGSI_INTERPOLATE_COLOR
:
843 case TGSI_INTERPOLATE_PERSPECTIVE
:
844 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
845 return SI_PARAM_PERSP_SAMPLE
;
846 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
847 return SI_PARAM_PERSP_CENTROID
;
849 return SI_PARAM_PERSP_CENTER
;
852 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
857 /* This shouldn't be used by explicit INTERP opcodes. */
858 static LLVMValueRef
get_interp_param(struct si_shader_context
*si_shader_ctx
,
861 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
862 unsigned sample_param
= 0;
863 LLVMValueRef default_ij
, sample_ij
, force_sample
;
865 default_ij
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, param
);
867 /* If the shader doesn't use center/centroid, just return the parameter.
869 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
870 * switch between center/centroid and sample without shader changes.
873 case SI_PARAM_PERSP_CENTROID
:
874 case SI_PARAM_PERSP_CENTER
:
875 if (!si_shader_ctx
->shader
->selector
->forces_persample_interp_for_persp
)
878 sample_param
= SI_PARAM_PERSP_SAMPLE
;
881 case SI_PARAM_LINEAR_CENTROID
:
882 case SI_PARAM_LINEAR_CENTER
:
883 if (!si_shader_ctx
->shader
->selector
->forces_persample_interp_for_linear
)
886 sample_param
= SI_PARAM_LINEAR_SAMPLE
;
893 /* Otherwise, we have to select (i,j) based on a user data SGPR. */
894 sample_ij
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, sample_param
);
896 /* TODO: this can be done more efficiently by switching between
899 force_sample
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
900 SI_PARAM_PS_STATE_BITS
);
901 force_sample
= LLVMBuildTrunc(gallivm
->builder
, force_sample
,
902 LLVMInt1TypeInContext(gallivm
->context
), "");
903 return LLVMBuildSelect(gallivm
->builder
, force_sample
,
904 sample_ij
, default_ij
, "");
907 static void declare_input_fs(
908 struct radeon_llvm_context
*radeon_bld
,
909 unsigned input_index
,
910 const struct tgsi_full_declaration
*decl
)
912 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
913 struct si_shader_context
*si_shader_ctx
=
914 si_shader_context(&radeon_bld
->soa
.bld_base
);
915 struct si_shader
*shader
= si_shader_ctx
->shader
;
916 struct lp_build_context
*uint
= &radeon_bld
->soa
.bld_base
.uint_bld
;
917 struct gallivm_state
*gallivm
= base
->gallivm
;
918 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
919 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
921 LLVMValueRef interp_param
= NULL
;
922 int interp_param_idx
;
923 const char * intr_name
;
926 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
927 * quad begins a new primitive. Bit 0 always needs
929 * [32:16] ParamOffset
932 LLVMValueRef params
= LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
);
933 LLVMValueRef attr_number
;
937 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
938 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
940 radeon_llvm_reg_index_soa(input_index
, chan
);
941 radeon_bld
->inputs
[soa_index
] =
942 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
945 /* RCP for fragcoord.w */
946 radeon_bld
->inputs
[soa_index
] =
947 LLVMBuildFDiv(gallivm
->builder
,
948 lp_build_const_float(gallivm
, 1.0f
),
949 radeon_bld
->inputs
[soa_index
],
955 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
956 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
957 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
958 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
959 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
960 lp_build_const_float(gallivm
, 0.0f
);
961 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
962 lp_build_const_float(gallivm
, 1.0f
);
967 shader
->ps_input_param_offset
[input_index
] = shader
->nparam
++;
968 attr_number
= lp_build_const_int32(gallivm
,
969 shader
->ps_input_param_offset
[input_index
]);
971 shader
->ps_input_interpolate
[input_index
] = decl
->Interp
.Interpolate
;
972 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
973 decl
->Interp
.Location
);
974 if (interp_param_idx
== -1)
976 else if (interp_param_idx
)
977 interp_param
= get_interp_param(si_shader_ctx
, interp_param_idx
);
979 /* fs.constant returns the param from the middle vertex, so it's not
980 * really useful for flat shading. It's meant to be used for custom
981 * interpolation (but the intrinsic can't fetch from the other two
984 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
985 * to do the right thing. The only reason we use fs.constant is that
986 * fs.interp cannot be used on integers, because they can be equal
989 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
991 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
992 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
993 LLVMValueRef args
[4];
994 LLVMValueRef face
, is_face_positive
;
995 LLVMValueRef back_attr_number
=
996 lp_build_const_int32(gallivm
,
997 shader
->ps_input_param_offset
[input_index
] + 1);
999 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
1001 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
1003 lp_build_const_float(gallivm
, 0.0f
),
1007 args
[3] = interp_param
;
1008 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1009 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1010 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
1011 LLVMValueRef front
, back
;
1013 args
[0] = llvm_chan
;
1014 args
[1] = attr_number
;
1015 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
1016 input_type
, args
, args
[3] ? 4 : 3,
1017 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1019 args
[1] = back_attr_number
;
1020 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
1021 input_type
, args
, args
[3] ? 4 : 3,
1022 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1024 radeon_bld
->inputs
[soa_index
] =
1025 LLVMBuildSelect(gallivm
->builder
,
1033 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
1034 LLVMValueRef args
[4];
1036 args
[0] = uint
->zero
;
1037 args
[1] = attr_number
;
1039 args
[3] = interp_param
;
1040 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
1041 lp_build_intrinsic(gallivm
->builder
, intr_name
,
1042 input_type
, args
, args
[3] ? 4 : 3,
1043 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1044 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
1045 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
1046 lp_build_const_float(gallivm
, 0.0f
);
1047 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
1048 lp_build_const_float(gallivm
, 1.0f
);
1050 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1051 LLVMValueRef args
[4];
1052 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1053 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
1054 args
[0] = llvm_chan
;
1055 args
[1] = attr_number
;
1057 args
[3] = interp_param
;
1058 radeon_bld
->inputs
[soa_index
] =
1059 lp_build_intrinsic(gallivm
->builder
, intr_name
,
1060 input_type
, args
, args
[3] ? 4 : 3,
1061 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1066 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1068 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1069 SI_PARAM_ANCILLARY
, 8, 4);
1073 * Load a dword from a constant buffer.
1075 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1076 LLVMValueRef offset
, LLVMTypeRef return_type
)
1078 LLVMValueRef args
[2] = {resource
, offset
};
1080 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1081 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1084 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1086 struct si_shader_context
*si_shader_ctx
=
1087 si_shader_context(&radeon_bld
->soa
.bld_base
);
1088 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1089 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1090 LLVMBuilderRef builder
= gallivm
->builder
;
1091 LLVMValueRef desc
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1092 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1093 LLVMValueRef resource
= build_indexed_load_const(si_shader_ctx
, desc
, buf_index
);
1095 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1096 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1097 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1099 LLVMValueRef pos
[4] = {
1100 buffer_load_const(builder
, resource
, offset0
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1101 buffer_load_const(builder
, resource
, offset1
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1102 lp_build_const_float(gallivm
, 0),
1103 lp_build_const_float(gallivm
, 0)
1106 return lp_build_gather_values(gallivm
, pos
, 4);
1109 static void declare_system_value(
1110 struct radeon_llvm_context
* radeon_bld
,
1112 const struct tgsi_full_declaration
*decl
)
1114 struct si_shader_context
*si_shader_ctx
=
1115 si_shader_context(&radeon_bld
->soa
.bld_base
);
1116 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1117 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1118 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1119 LLVMValueRef value
= 0;
1121 switch (decl
->Semantic
.Name
) {
1122 case TGSI_SEMANTIC_INSTANCEID
:
1123 value
= LLVMGetParam(radeon_bld
->main_fn
,
1124 si_shader_ctx
->param_instance_id
);
1127 case TGSI_SEMANTIC_VERTEXID
:
1128 value
= LLVMBuildAdd(gallivm
->builder
,
1129 LLVMGetParam(radeon_bld
->main_fn
,
1130 si_shader_ctx
->param_vertex_id
),
1131 LLVMGetParam(radeon_bld
->main_fn
,
1132 SI_PARAM_BASE_VERTEX
), "");
1135 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1136 value
= LLVMGetParam(radeon_bld
->main_fn
,
1137 si_shader_ctx
->param_vertex_id
);
1140 case TGSI_SEMANTIC_BASEVERTEX
:
1141 value
= LLVMGetParam(radeon_bld
->main_fn
,
1142 SI_PARAM_BASE_VERTEX
);
1145 case TGSI_SEMANTIC_INVOCATIONID
:
1146 if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1147 value
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1148 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1149 value
= LLVMGetParam(radeon_bld
->main_fn
,
1150 SI_PARAM_GS_INSTANCE_ID
);
1152 assert(!"INVOCATIONID not implemented");
1155 case TGSI_SEMANTIC_SAMPLEID
:
1156 value
= get_sample_id(radeon_bld
);
1159 case TGSI_SEMANTIC_SAMPLEPOS
:
1160 value
= load_sample_position(radeon_bld
, get_sample_id(radeon_bld
));
1163 case TGSI_SEMANTIC_SAMPLEMASK
:
1164 /* Smoothing isn't MSAA in GL, but it's MSAA in hardware.
1165 * Therefore, force gl_SampleMaskIn to 1 for GL. */
1166 if (si_shader_ctx
->shader
->key
.ps
.poly_line_smoothing
)
1167 value
= uint_bld
->one
;
1169 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1172 case TGSI_SEMANTIC_TESSCOORD
:
1174 LLVMValueRef coord
[4] = {
1175 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_u
),
1176 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_v
),
1181 /* For triangles, the vector should be (u, v, 1-u-v). */
1182 if (si_shader_ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1183 PIPE_PRIM_TRIANGLES
)
1184 coord
[2] = lp_build_sub(bld
, bld
->one
,
1185 lp_build_add(bld
, coord
[0], coord
[1]));
1187 value
= lp_build_gather_values(gallivm
, coord
, 4);
1191 case TGSI_SEMANTIC_VERTICESIN
:
1192 value
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1195 case TGSI_SEMANTIC_TESSINNER
:
1196 case TGSI_SEMANTIC_TESSOUTER
:
1198 LLVMValueRef dw_addr
;
1199 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1201 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
1202 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1203 lp_build_const_int32(gallivm
, param
* 4), "");
1205 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1210 case TGSI_SEMANTIC_PRIMID
:
1211 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1215 assert(!"unknown system value");
1219 radeon_bld
->system_values
[index
] = value
;
1222 static LLVMValueRef
fetch_constant(
1223 struct lp_build_tgsi_context
* bld_base
,
1224 const struct tgsi_full_src_register
*reg
,
1225 enum tgsi_opcode_type type
,
1228 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1229 struct lp_build_context
* base
= &bld_base
->base
;
1230 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1233 LLVMValueRef addr
, bufp
;
1234 LLVMValueRef result
;
1236 if (swizzle
== LP_CHAN_ALL
) {
1238 LLVMValueRef values
[4];
1239 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1240 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1242 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1245 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1246 idx
= reg
->Register
.Index
* 4 + swizzle
;
1248 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1249 if (type
!= TGSI_TYPE_DOUBLE
)
1250 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
1252 return radeon_llvm_emit_fetch_double(bld_base
,
1253 si_shader_ctx
->constants
[buf
][idx
],
1254 si_shader_ctx
->constants
[buf
][idx
+ 1]);
1258 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1259 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1261 index
= get_indirect_index(si_shader_ctx
, ®
->DimIndirect
,
1262 reg
->Dimension
.Index
);
1263 bufp
= build_indexed_load_const(si_shader_ctx
, ptr
, index
);
1265 bufp
= si_shader_ctx
->const_resource
[buf
];
1267 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1268 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1269 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1270 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1271 lp_build_const_int32(base
->gallivm
, idx
* 4));
1273 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1274 addr
, bld_base
->base
.elem_type
);
1276 if (type
!= TGSI_TYPE_DOUBLE
)
1277 result
= bitcast(bld_base
, type
, result
);
1279 LLVMValueRef addr2
, result2
;
1280 addr2
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1281 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1282 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1283 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1284 lp_build_const_int32(base
->gallivm
, idx
* 4));
1286 result2
= buffer_load_const(base
->gallivm
->builder
, si_shader_ctx
->const_resource
[buf
],
1287 addr2
, bld_base
->base
.elem_type
);
1289 result
= radeon_llvm_emit_fetch_double(bld_base
,
1295 /* Initialize arguments for the shader export intrinsic */
1296 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1297 LLVMValueRef
*values
,
1301 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1302 struct lp_build_context
*uint
=
1303 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1304 struct lp_build_context
*base
= &bld_base
->base
;
1305 unsigned compressed
= 0;
1308 /* XXX: This controls which components of the output
1309 * registers actually get exported. (e.g bit 0 means export
1310 * X component, bit 1 means export Y component, etc.) I'm
1311 * hard coding this to 0xf for now. In the future, we might
1312 * want to do something else.
1314 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1316 /* Specify whether the EXEC mask represents the valid mask */
1317 args
[1] = uint
->zero
;
1319 /* Specify whether this is the last export */
1320 args
[2] = uint
->zero
;
1322 /* Specify the target we are exporting */
1323 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1325 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1326 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1328 if (cbuf
>= 0 && cbuf
< 8) {
1329 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
1332 si_shader_ctx
->shader
->spi_shader_col_format
|=
1333 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
1335 si_shader_ctx
->shader
->spi_shader_col_format
|=
1336 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
1338 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
1342 /* Set COMPR flag */
1343 args
[4] = compressed
? uint
->one
: uint
->zero
;
1346 /* Pixel shader needs to pack output values before export */
1347 for (chan
= 0; chan
< 2; chan
++) {
1348 LLVMValueRef pack_args
[2] = {
1350 values
[2 * chan
+ 1]
1352 LLVMValueRef packed
;
1354 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1356 LLVMInt32TypeInContext(base
->gallivm
->context
),
1358 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1359 args
[chan
+ 7] = args
[chan
+ 5] =
1360 LLVMBuildBitCast(base
->gallivm
->builder
,
1362 LLVMFloatTypeInContext(base
->gallivm
->context
),
1366 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1369 /* Load from output pointers and initialize arguments for the shader export intrinsic */
1370 static void si_llvm_init_export_args_load(struct lp_build_tgsi_context
*bld_base
,
1371 LLVMValueRef
*out_ptr
,
1375 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1376 LLVMValueRef values
[4];
1379 for (i
= 0; i
< 4; i
++)
1380 values
[i
] = LLVMBuildLoad(gallivm
->builder
, out_ptr
[i
], "");
1382 si_llvm_init_export_args(bld_base
, values
, target
, args
);
1385 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1386 LLVMValueRef alpha_ptr
)
1388 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1389 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1391 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
1392 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1393 SI_PARAM_ALPHA_REF
);
1395 LLVMValueRef alpha_pass
=
1396 lp_build_cmp(&bld_base
->base
,
1397 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
1398 LLVMBuildLoad(gallivm
->builder
, alpha_ptr
, ""),
1401 lp_build_select(&bld_base
->base
,
1403 lp_build_const_float(gallivm
, 1.0f
),
1404 lp_build_const_float(gallivm
, -1.0f
));
1406 lp_build_intrinsic(gallivm
->builder
,
1408 LLVMVoidTypeInContext(gallivm
->context
),
1411 lp_build_intrinsic(gallivm
->builder
,
1413 LLVMVoidTypeInContext(gallivm
->context
),
1417 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
1420 static void si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1421 LLVMValueRef alpha_ptr
)
1423 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1424 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1425 LLVMValueRef coverage
, alpha
;
1427 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1428 coverage
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1429 SI_PARAM_SAMPLE_COVERAGE
);
1430 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1432 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1433 bld_base
->int_bld
.elem_type
,
1434 &coverage
, 1, LLVMReadNoneAttribute
);
1436 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1437 bld_base
->base
.elem_type
, "");
1439 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1440 lp_build_const_float(gallivm
,
1441 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1443 alpha
= LLVMBuildLoad(gallivm
->builder
, alpha_ptr
, "");
1444 alpha
= LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1445 LLVMBuildStore(gallivm
->builder
, alpha
, alpha_ptr
);
1448 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
1449 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1451 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1452 struct lp_build_context
*base
= &bld_base
->base
;
1453 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1456 unsigned const_chan
;
1457 LLVMValueRef base_elt
;
1458 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1459 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1460 LLVMValueRef const_resource
= build_indexed_load_const(si_shader_ctx
, ptr
, constbuf_index
);
1462 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1463 LLVMValueRef
*args
= pos
[2 + reg_index
];
1468 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1470 /* Compute dot products of position and user clip plane vectors */
1471 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1472 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1473 args
[1] = lp_build_const_int32(base
->gallivm
,
1474 ((reg_index
* 4 + chan
) * 4 +
1476 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1477 args
[1], base
->elem_type
);
1479 lp_build_add(base
, args
[5 + chan
],
1480 lp_build_mul(base
, base_elt
,
1481 out_elts
[const_chan
]));
1485 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1486 args
[1] = uint
->zero
;
1487 args
[2] = uint
->zero
;
1488 args
[3] = lp_build_const_int32(base
->gallivm
,
1489 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1490 args
[4] = uint
->zero
;
1494 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1498 if (so
->num_outputs
)
1499 fprintf(stderr
, "STREAMOUT\n");
1501 for (i
= 0; i
< so
->num_outputs
; i
++) {
1502 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1503 so
->output
[i
].start_component
;
1504 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1505 i
, so
->output
[i
].output_buffer
,
1506 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1507 so
->output
[i
].register_index
,
1508 mask
& 1 ? "x" : "",
1509 mask
& 2 ? "y" : "",
1510 mask
& 4 ? "z" : "",
1511 mask
& 8 ? "w" : "");
1515 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1516 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1517 * or v4i32 (num_channels=3,4). */
1518 static void build_tbuffer_store(struct si_shader_context
*shader
,
1521 unsigned num_channels
,
1523 LLVMValueRef soffset
,
1524 unsigned inst_offset
,
1533 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1534 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1535 LLVMValueRef args
[] = {
1538 LLVMConstInt(i32
, num_channels
, 0),
1541 LLVMConstInt(i32
, inst_offset
, 0),
1542 LLVMConstInt(i32
, dfmt
, 0),
1543 LLVMConstInt(i32
, nfmt
, 0),
1544 LLVMConstInt(i32
, offen
, 0),
1545 LLVMConstInt(i32
, idxen
, 0),
1546 LLVMConstInt(i32
, glc
, 0),
1547 LLVMConstInt(i32
, slc
, 0),
1548 LLVMConstInt(i32
, tfe
, 0)
1551 /* The instruction offset field has 12 bits */
1552 assert(offen
|| inst_offset
< (1 << 12));
1554 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1555 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1556 const char *types
[] = {"i32", "v2i32", "v4i32"};
1558 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1560 lp_build_intrinsic(gallivm
->builder
, name
,
1561 LLVMVoidTypeInContext(gallivm
->context
),
1562 args
, Elements(args
), 0);
1565 static void build_tbuffer_store_dwords(struct si_shader_context
*shader
,
1568 unsigned num_channels
,
1570 LLVMValueRef soffset
,
1571 unsigned inst_offset
)
1573 static unsigned dfmt
[] = {
1574 V_008F0C_BUF_DATA_FORMAT_32
,
1575 V_008F0C_BUF_DATA_FORMAT_32_32
,
1576 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1577 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1579 assert(num_channels
>= 1 && num_channels
<= 4);
1581 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1582 inst_offset
, dfmt
[num_channels
-1],
1583 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1586 /* On SI, the vertex shader is responsible for writing streamout data
1588 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
1589 struct si_shader_output_values
*outputs
,
1592 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
1593 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1594 LLVMBuilderRef builder
= gallivm
->builder
;
1596 struct lp_build_if_state if_ctx
;
1598 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1600 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1601 LLVMValueRef so_vtx_count
=
1602 unpack_param(shader
, shader
->param_streamout_config
, 16, 7);
1604 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", i32
,
1605 NULL
, 0, LLVMReadNoneAttribute
);
1607 /* can_emit = tid < so_vtx_count; */
1608 LLVMValueRef can_emit
=
1609 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1611 LLVMValueRef stream_id
=
1612 unpack_param(shader
, shader
->param_streamout_config
, 24, 2);
1614 /* Emit the streamout code conditionally. This actually avoids
1615 * out-of-bounds buffer access. The hw tells us via the SGPR
1616 * (so_vtx_count) which threads are allowed to emit streamout data. */
1617 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1619 /* The buffer offset is computed as follows:
1620 * ByteOffset = streamout_offset[buffer_id]*4 +
1621 * (streamout_write_index + thread_id)*stride[buffer_id] +
1625 LLVMValueRef so_write_index
=
1626 LLVMGetParam(shader
->radeon_bld
.main_fn
,
1627 shader
->param_streamout_write_index
);
1629 /* Compute (streamout_write_index + thread_id). */
1630 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1632 /* Compute the write offset for each enabled buffer. */
1633 LLVMValueRef so_write_offset
[4] = {};
1634 for (i
= 0; i
< 4; i
++) {
1638 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
1639 shader
->param_streamout_offset
[i
]);
1640 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
1642 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1643 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
1644 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1647 /* Write streamout data. */
1648 for (i
= 0; i
< so
->num_outputs
; i
++) {
1649 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1650 unsigned reg
= so
->output
[i
].register_index
;
1651 unsigned start
= so
->output
[i
].start_component
;
1652 unsigned num_comps
= so
->output
[i
].num_components
;
1653 unsigned stream
= so
->output
[i
].stream
;
1654 LLVMValueRef out
[4];
1655 struct lp_build_if_state if_ctx_stream
;
1657 assert(num_comps
&& num_comps
<= 4);
1658 if (!num_comps
|| num_comps
> 4)
1664 /* Load the output as int. */
1665 for (j
= 0; j
< num_comps
; j
++) {
1666 out
[j
] = LLVMBuildBitCast(builder
,
1667 outputs
[reg
].values
[start
+j
],
1671 /* Pack the output. */
1672 LLVMValueRef vdata
= NULL
;
1674 switch (num_comps
) {
1675 case 1: /* as i32 */
1678 case 2: /* as v2i32 */
1679 case 3: /* as v4i32 (aligned to 4) */
1680 case 4: /* as v4i32 */
1681 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
1682 for (j
= 0; j
< num_comps
; j
++) {
1683 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1684 LLVMConstInt(i32
, j
, 0), "");
1689 LLVMValueRef can_emit_stream
=
1690 LLVMBuildICmp(builder
, LLVMIntEQ
,
1692 lp_build_const_int32(gallivm
, stream
), "");
1694 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1695 build_tbuffer_store_dwords(shader
, shader
->so_buffers
[buf_idx
],
1697 so_write_offset
[buf_idx
],
1698 LLVMConstInt(i32
, 0, 0),
1699 so
->output
[i
].dst_offset
*4);
1700 lp_build_endif(&if_ctx_stream
);
1703 lp_build_endif(&if_ctx
);
1707 /* Generate export instructions for hardware VS shader stage */
1708 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1709 struct si_shader_output_values
*outputs
,
1712 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1713 struct si_shader
* shader
= si_shader_ctx
->shader
;
1714 struct lp_build_context
* base
= &bld_base
->base
;
1715 struct lp_build_context
* uint
=
1716 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1717 LLVMValueRef args
[9];
1718 LLVMValueRef pos_args
[4][9] = { { 0 } };
1719 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1720 unsigned semantic_name
, semantic_index
;
1722 unsigned param_count
= 0;
1726 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1727 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1730 for (i
= 0; i
< noutput
; i
++) {
1731 semantic_name
= outputs
[i
].name
;
1732 semantic_index
= outputs
[i
].sid
;
1735 /* Select the correct target */
1736 switch(semantic_name
) {
1737 case TGSI_SEMANTIC_PSIZE
:
1738 psize_value
= outputs
[i
].values
[0];
1740 case TGSI_SEMANTIC_EDGEFLAG
:
1741 edgeflag_value
= outputs
[i
].values
[0];
1743 case TGSI_SEMANTIC_LAYER
:
1744 layer_value
= outputs
[i
].values
[0];
1745 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1746 goto handle_semantic
;
1747 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1748 viewport_index_value
= outputs
[i
].values
[0];
1749 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1750 goto handle_semantic
;
1751 case TGSI_SEMANTIC_POSITION
:
1752 target
= V_008DFC_SQ_EXP_POS
;
1754 case TGSI_SEMANTIC_COLOR
:
1755 case TGSI_SEMANTIC_BCOLOR
:
1756 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1757 shader
->vs_output_param_offset
[i
] = param_count
;
1760 case TGSI_SEMANTIC_CLIPDIST
:
1761 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1763 case TGSI_SEMANTIC_CLIPVERTEX
:
1764 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1766 case TGSI_SEMANTIC_PRIMID
:
1767 case TGSI_SEMANTIC_FOG
:
1768 case TGSI_SEMANTIC_TEXCOORD
:
1769 case TGSI_SEMANTIC_GENERIC
:
1770 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1771 shader
->vs_output_param_offset
[i
] = param_count
;
1777 "Warning: SI unhandled vs output type:%d\n",
1781 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1783 if (target
>= V_008DFC_SQ_EXP_POS
&&
1784 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1785 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1786 args
, sizeof(args
));
1788 lp_build_intrinsic(base
->gallivm
->builder
,
1790 LLVMVoidTypeInContext(base
->gallivm
->context
),
1794 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1795 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1796 goto handle_semantic
;
1800 shader
->nr_param_exports
= param_count
;
1802 /* We need to add the position output manually if it's missing. */
1803 if (!pos_args
[0][0]) {
1804 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1805 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1806 pos_args
[0][2] = uint
->zero
; /* last export? */
1807 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1808 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1809 pos_args
[0][5] = base
->zero
; /* X */
1810 pos_args
[0][6] = base
->zero
; /* Y */
1811 pos_args
[0][7] = base
->zero
; /* Z */
1812 pos_args
[0][8] = base
->one
; /* W */
1815 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1816 if (shader
->selector
->info
.writes_psize
||
1817 shader
->selector
->info
.writes_edgeflag
||
1818 shader
->selector
->info
.writes_viewport_index
||
1819 shader
->selector
->info
.writes_layer
) {
1820 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1821 shader
->selector
->info
.writes_psize
|
1822 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1823 (shader
->selector
->info
.writes_layer
<< 2) |
1824 (shader
->selector
->info
.writes_viewport_index
<< 3));
1825 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1826 pos_args
[1][2] = uint
->zero
; /* last export? */
1827 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1828 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1829 pos_args
[1][5] = base
->zero
; /* X */
1830 pos_args
[1][6] = base
->zero
; /* Y */
1831 pos_args
[1][7] = base
->zero
; /* Z */
1832 pos_args
[1][8] = base
->zero
; /* W */
1834 if (shader
->selector
->info
.writes_psize
)
1835 pos_args
[1][5] = psize_value
;
1837 if (shader
->selector
->info
.writes_edgeflag
) {
1838 /* The output is a float, but the hw expects an integer
1839 * with the first bit containing the edge flag. */
1840 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1842 bld_base
->uint_bld
.elem_type
, "");
1843 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1845 bld_base
->int_bld
.one
);
1847 /* The LLVM intrinsic expects a float. */
1848 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1850 base
->elem_type
, "");
1853 if (shader
->selector
->info
.writes_layer
)
1854 pos_args
[1][7] = layer_value
;
1856 if (shader
->selector
->info
.writes_viewport_index
)
1857 pos_args
[1][8] = viewport_index_value
;
1860 for (i
= 0; i
< 4; i
++)
1862 shader
->nr_pos_exports
++;
1865 for (i
= 0; i
< 4; i
++) {
1866 if (!pos_args
[i
][0])
1869 /* Specify the target we are exporting */
1870 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1872 if (pos_idx
== shader
->nr_pos_exports
)
1873 /* Specify that this is the last export */
1874 pos_args
[i
][2] = uint
->one
;
1876 lp_build_intrinsic(base
->gallivm
->builder
,
1878 LLVMVoidTypeInContext(base
->gallivm
->context
),
1883 /* This only writes the tessellation factor levels. */
1884 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1886 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1887 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1888 struct si_shader
*shader
= si_shader_ctx
->shader
;
1889 unsigned tess_inner_index
, tess_outer_index
;
1890 LLVMValueRef lds_base
, lds_inner
, lds_outer
;
1891 LLVMValueRef tf_base
, rel_patch_id
, byteoffset
, buffer
, rw_buffers
;
1892 LLVMValueRef out
[6], vec0
, vec1
, invocation_id
;
1893 unsigned stride
, outer_comps
, inner_comps
, i
;
1894 struct lp_build_if_state if_ctx
;
1896 invocation_id
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1898 /* Do this only for invocation 0, because the tess levels are per-patch,
1901 * This can't jump, because invocation 0 executes this. It should
1902 * at least mask out the loads and stores for other invocations.
1904 lp_build_if(&if_ctx
, gallivm
,
1905 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
1906 invocation_id
, bld_base
->uint_bld
.zero
, ""));
1908 /* Determine the layout of one tess factor element in the buffer. */
1909 switch (shader
->key
.tcs
.prim_mode
) {
1910 case PIPE_PRIM_LINES
:
1911 stride
= 2; /* 2 dwords, 1 vec2 store */
1915 case PIPE_PRIM_TRIANGLES
:
1916 stride
= 4; /* 4 dwords, 1 vec4 store */
1920 case PIPE_PRIM_QUADS
:
1921 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
1930 /* Load tess_inner and tess_outer from LDS.
1931 * Any invocation can write them, so we can't get them from a temporary.
1933 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
1934 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
1936 lds_base
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
1937 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
1938 lp_build_const_int32(gallivm
,
1939 tess_inner_index
* 4), "");
1940 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
1941 lp_build_const_int32(gallivm
,
1942 tess_outer_index
* 4), "");
1944 for (i
= 0; i
< outer_comps
; i
++)
1945 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
1946 for (i
= 0; i
< inner_comps
; i
++)
1947 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
1949 /* Convert the outputs to vectors for stores. */
1950 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
1954 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
1956 /* Get the buffer. */
1957 rw_buffers
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1958 SI_PARAM_RW_BUFFERS
);
1959 buffer
= build_indexed_load_const(si_shader_ctx
, rw_buffers
,
1960 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
1962 /* Get the offset. */
1963 tf_base
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1964 SI_PARAM_TESS_FACTOR_OFFSET
);
1965 rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
1966 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
1967 lp_build_const_int32(gallivm
, 4 * stride
), "");
1969 /* Store the outputs. */
1970 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec0
,
1971 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
1973 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec1
,
1974 stride
- 4, byteoffset
, tf_base
, 16);
1975 lp_build_endif(&if_ctx
);
1978 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
* bld_base
)
1980 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1981 struct si_shader
*shader
= si_shader_ctx
->shader
;
1982 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1983 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1985 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1986 si_shader_ctx
->param_rel_auto_id
);
1987 LLVMValueRef vertex_dw_stride
=
1988 unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
1989 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
1990 vertex_dw_stride
, "");
1992 /* Write outputs to LDS. The next shader (TCS aka HS) will read
1993 * its inputs from it. */
1994 for (i
= 0; i
< info
->num_outputs
; i
++) {
1995 LLVMValueRef
*out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
1996 unsigned name
= info
->output_semantic_name
[i
];
1997 unsigned index
= info
->output_semantic_index
[i
];
1998 int param
= si_shader_io_get_unique_index(name
, index
);
1999 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2000 lp_build_const_int32(gallivm
, param
* 4), "");
2002 for (chan
= 0; chan
< 4; chan
++) {
2003 lds_store(bld_base
, chan
, dw_addr
,
2004 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2009 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
2011 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2012 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2013 struct si_shader
*es
= si_shader_ctx
->shader
;
2014 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2015 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2016 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2017 si_shader_ctx
->param_es2gs_offset
);
2021 for (i
= 0; i
< info
->num_outputs
; i
++) {
2022 LLVMValueRef
*out_ptr
=
2023 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
2026 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2027 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2030 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2031 info
->output_semantic_index
[i
]);
2033 for (chan
= 0; chan
< 4; chan
++) {
2034 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2035 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
2037 build_tbuffer_store(si_shader_ctx
,
2038 si_shader_ctx
->esgs_ring
,
2040 LLVMGetUndef(i32
), soffset
,
2041 (4 * param_index
+ chan
) * 4,
2042 V_008F0C_BUF_DATA_FORMAT_32
,
2043 V_008F0C_BUF_NUM_FORMAT_UINT
,
2049 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2051 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2052 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2053 LLVMValueRef args
[2];
2055 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2056 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2057 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2058 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2059 LLVMNoUnwindAttribute
);
2062 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2064 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2065 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2066 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
2067 struct si_shader_output_values
*outputs
= NULL
;
2070 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2072 /* Vertex color clamping.
2074 * This uses a state constant loaded in a user data SGPR and
2075 * an IF statement is added that clamps all colors if the constant
2078 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
2079 !si_shader_ctx
->shader
->is_gs_copy_shader
) {
2080 struct lp_build_if_state if_ctx
;
2081 LLVMValueRef cond
= NULL
;
2082 LLVMValueRef addr
, val
;
2084 for (i
= 0; i
< info
->num_outputs
; i
++) {
2085 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2086 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2089 /* We've found a color. */
2091 /* The state is in the first bit of the user SGPR. */
2092 cond
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2093 SI_PARAM_VS_STATE_BITS
);
2094 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2095 LLVMInt1TypeInContext(gallivm
->context
), "");
2096 lp_build_if(&if_ctx
, gallivm
, cond
);
2099 for (j
= 0; j
< 4; j
++) {
2100 addr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2101 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2102 val
= radeon_llvm_saturate(bld_base
, val
);
2103 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2108 lp_build_endif(&if_ctx
);
2111 for (i
= 0; i
< info
->num_outputs
; i
++) {
2112 outputs
[i
].name
= info
->output_semantic_name
[i
];
2113 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2115 for (j
= 0; j
< 4; j
++)
2116 outputs
[i
].values
[j
] =
2117 LLVMBuildLoad(gallivm
->builder
,
2118 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2122 /* Export PrimitiveID when PS needs it. */
2123 if (si_vs_exports_prim_id(si_shader_ctx
->shader
)) {
2124 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2126 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2127 get_primitive_id(bld_base
, 0));
2128 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2129 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2130 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2134 si_llvm_export_vs(bld_base
, outputs
, i
);
2138 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2140 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
2141 struct si_shader
* shader
= si_shader_ctx
->shader
;
2142 struct lp_build_context
* base
= &bld_base
->base
;
2143 struct lp_build_context
* uint
= &bld_base
->uint_bld
;
2144 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2145 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2146 LLVMValueRef args
[9];
2147 LLVMValueRef last_args
[9] = { 0 };
2148 int depth_index
= -1, stencil_index
= -1, samplemask_index
= -1;
2151 for (i
= 0; i
< info
->num_outputs
; i
++) {
2152 unsigned semantic_name
= info
->output_semantic_name
[i
];
2153 unsigned semantic_index
= info
->output_semantic_index
[i
];
2155 LLVMValueRef alpha_ptr
;
2157 /* Select the correct target */
2158 switch (semantic_name
) {
2159 case TGSI_SEMANTIC_POSITION
:
2162 case TGSI_SEMANTIC_STENCIL
:
2165 case TGSI_SEMANTIC_SAMPLEMASK
:
2166 samplemask_index
= i
;
2168 case TGSI_SEMANTIC_COLOR
:
2169 target
= V_008DFC_SQ_EXP_MRT
+ semantic_index
;
2170 alpha_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][3];
2172 if (si_shader_ctx
->shader
->key
.ps
.clamp_color
) {
2173 for (int j
= 0; j
< 4; j
++) {
2174 LLVMValueRef ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2175 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2177 result
= radeon_llvm_saturate(bld_base
, result
);
2178 LLVMBuildStore(builder
, result
, ptr
);
2182 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
2183 LLVMBuildStore(base
->gallivm
->builder
,
2184 base
->one
, alpha_ptr
);
2186 if (semantic_index
== 0 &&
2187 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2188 si_alpha_test(bld_base
, alpha_ptr
);
2190 if (si_shader_ctx
->shader
->key
.ps
.poly_line_smoothing
)
2191 si_scale_alpha_by_sample_mask(bld_base
, alpha_ptr
);
2197 "Warning: SI unhandled fs output type:%d\n",
2201 si_llvm_init_export_args_load(bld_base
,
2202 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
2205 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
2206 /* If there is an export instruction waiting to be emitted, do so now. */
2208 lp_build_intrinsic(base
->gallivm
->builder
,
2210 LLVMVoidTypeInContext(base
->gallivm
->context
),
2214 /* This instruction will be emitted at the end of the shader. */
2215 memcpy(last_args
, args
, sizeof(args
));
2217 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
2218 if (shader
->selector
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
2219 semantic_index
== 0 &&
2220 si_shader_ctx
->shader
->key
.ps
.last_cbuf
> 0) {
2221 for (int c
= 1; c
<= si_shader_ctx
->shader
->key
.ps
.last_cbuf
; c
++) {
2222 si_llvm_init_export_args_load(bld_base
,
2223 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
2224 V_008DFC_SQ_EXP_MRT
+ c
, args
);
2225 lp_build_intrinsic(base
->gallivm
->builder
,
2227 LLVMVoidTypeInContext(base
->gallivm
->context
),
2232 lp_build_intrinsic(base
->gallivm
->builder
,
2234 LLVMVoidTypeInContext(base
->gallivm
->context
),
2239 if (depth_index
>= 0 || stencil_index
>= 0 || samplemask_index
>= 0) {
2240 LLVMValueRef out_ptr
;
2243 /* Specify the target we are exporting */
2244 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2246 args
[5] = base
->zero
; /* R, depth */
2247 args
[6] = base
->zero
; /* G, stencil test value[0:7], stencil op value[8:15] */
2248 args
[7] = base
->zero
; /* B, sample mask */
2249 args
[8] = base
->zero
; /* A, alpha to mask */
2251 if (depth_index
>= 0) {
2252 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
2253 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2255 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_Z_EXPORT_ENABLE(1);
2258 if (stencil_index
>= 0) {
2259 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
2260 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2262 si_shader_ctx
->shader
->db_shader_control
|=
2263 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
2266 if (samplemask_index
>= 0) {
2267 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[samplemask_index
][0];
2268 args
[7] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2270 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_MASK_EXPORT_ENABLE(1);
2273 /* SI (except OLAND) has a bug that it only looks
2274 * at the X writemask component. */
2275 if (si_shader_ctx
->screen
->b
.chip_class
== SI
&&
2276 si_shader_ctx
->screen
->b
.family
!= CHIP_OLAND
)
2279 if (samplemask_index
>= 0)
2280 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_ABGR
;
2281 else if (stencil_index
>= 0)
2282 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_GR
;
2284 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_R
;
2286 /* Specify which components to enable */
2287 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2291 args
[4] = uint
->zero
;
2294 lp_build_intrinsic(base
->gallivm
->builder
,
2296 LLVMVoidTypeInContext(base
->gallivm
->context
),
2299 memcpy(last_args
, args
, sizeof(args
));
2302 if (!last_args
[0]) {
2303 /* Specify which components to enable */
2304 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
2306 /* Specify the target we are exporting */
2307 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
2309 /* Set COMPR flag to zero to export data as 32-bit */
2310 last_args
[4] = uint
->zero
;
2313 last_args
[5]= uint
->zero
;
2314 last_args
[6]= uint
->zero
;
2315 last_args
[7]= uint
->zero
;
2316 last_args
[8]= uint
->zero
;
2319 /* Specify whether the EXEC mask represents the valid mask */
2320 last_args
[1] = uint
->one
;
2322 /* Specify that this is the last export */
2323 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
2325 lp_build_intrinsic(base
->gallivm
->builder
,
2327 LLVMVoidTypeInContext(base
->gallivm
->context
),
2331 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2332 struct lp_build_tgsi_context
* bld_base
,
2333 struct lp_build_emit_data
* emit_data
);
2335 static bool tgsi_is_array_sampler(unsigned target
)
2337 return target
== TGSI_TEXTURE_1D_ARRAY
||
2338 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2339 target
== TGSI_TEXTURE_2D_ARRAY
||
2340 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2341 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2342 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2343 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2346 static void set_tex_fetch_args(struct gallivm_state
*gallivm
,
2347 struct lp_build_emit_data
*emit_data
,
2348 unsigned opcode
, unsigned target
,
2349 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
2350 LLVMValueRef
*param
, unsigned count
,
2354 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
2355 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2357 /* Pad to power of two vector */
2358 while (count
< util_next_power_of_two(count
))
2359 param
[count
++] = LLVMGetUndef(i32
);
2361 /* Texture coordinates. */
2363 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
2365 emit_data
->args
[0] = param
[0];
2368 emit_data
->args
[1] = res_ptr
;
2371 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
2372 emit_data
->dst_type
= LLVMVectorType(i32
, 4);
2374 emit_data
->dst_type
= LLVMVectorType(
2375 LLVMFloatTypeInContext(gallivm
->context
), 4);
2377 emit_data
->args
[num_args
++] = samp_ptr
;
2380 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
2381 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
2382 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
2383 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
2384 tgsi_is_array_sampler(target
)); /* da */
2385 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
2386 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
2387 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
2388 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
2390 emit_data
->arg_count
= num_args
;
2393 static const struct lp_build_tgsi_action tex_action
;
2395 static void tex_fetch_ptrs(
2396 struct lp_build_tgsi_context
* bld_base
,
2397 struct lp_build_emit_data
* emit_data
,
2398 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
2400 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2401 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2402 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2403 unsigned target
= inst
->Texture
.Texture
;
2404 unsigned sampler_src
;
2405 unsigned sampler_index
;
2407 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
2408 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
2410 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
2411 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
2412 LLVMValueRef ind_index
;
2414 ind_index
= get_indirect_index(si_shader_ctx
, ®
->Indirect
, reg
->Register
.Index
);
2416 *res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2417 *res_ptr
= build_indexed_load_const(si_shader_ctx
, *res_ptr
, ind_index
);
2419 *samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2420 *samp_ptr
= build_indexed_load_const(si_shader_ctx
, *samp_ptr
, ind_index
);
2422 if (target
== TGSI_TEXTURE_2D_MSAA
||
2423 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2424 ind_index
= LLVMBuildAdd(gallivm
->builder
, ind_index
,
2425 lp_build_const_int32(gallivm
,
2426 SI_FMASK_TEX_OFFSET
), "");
2427 *fmask_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2428 *fmask_ptr
= build_indexed_load_const(si_shader_ctx
, *fmask_ptr
, ind_index
);
2431 *res_ptr
= si_shader_ctx
->resources
[sampler_index
];
2432 *samp_ptr
= si_shader_ctx
->samplers
[sampler_index
];
2433 *fmask_ptr
= si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ sampler_index
];
2437 static void tex_fetch_args(
2438 struct lp_build_tgsi_context
* bld_base
,
2439 struct lp_build_emit_data
* emit_data
)
2441 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2442 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2443 LLVMBuilderRef builder
= gallivm
->builder
;
2444 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2445 unsigned opcode
= inst
->Instruction
.Opcode
;
2446 unsigned target
= inst
->Texture
.Texture
;
2447 LLVMValueRef coords
[5], derivs
[6];
2448 LLVMValueRef address
[16];
2450 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
2453 unsigned num_deriv_channels
= 0;
2454 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
2455 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2456 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2457 unsigned dmask
= 0xf;
2459 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2461 if (opcode
== TGSI_OPCODE_TXQ
) {
2462 if (target
== TGSI_TEXTURE_BUFFER
) {
2463 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2465 /* Read the size from the buffer descriptor directly. */
2466 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2467 LLVMValueRef size
= LLVMBuildExtractElement(builder
, res
,
2468 lp_build_const_int32(gallivm
, 6), "");
2470 if (si_shader_ctx
->screen
->b
.chip_class
>= VI
) {
2471 /* On VI, the descriptor contains the size in bytes,
2472 * but TXQ must return the size in elements.
2473 * The stride is always non-zero for resources using TXQ.
2475 LLVMValueRef stride
=
2476 LLVMBuildExtractElement(builder
, res
,
2477 lp_build_const_int32(gallivm
, 5), "");
2478 stride
= LLVMBuildLShr(builder
, stride
,
2479 lp_build_const_int32(gallivm
, 16), "");
2480 stride
= LLVMBuildAnd(builder
, stride
,
2481 lp_build_const_int32(gallivm
, 0x3FFF), "");
2483 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2486 emit_data
->args
[0] = size
;
2490 /* Textures - set the mip level. */
2491 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
2493 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2494 NULL
, address
, count
, 0xf);
2498 if (target
== TGSI_TEXTURE_BUFFER
) {
2499 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
2500 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
2501 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
2502 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
2504 /* Bitcast and truncate v8i32 to v16i8. */
2505 LLVMValueRef res
= res_ptr
;
2506 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
2507 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
2508 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
2510 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2511 emit_data
->args
[0] = res
;
2512 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
2513 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
2514 emit_data
->arg_count
= 3;
2518 /* Fetch and project texture coordinates */
2519 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2520 for (chan
= 0; chan
< 3; chan
++ ) {
2521 coords
[chan
] = lp_build_emit_fetch(bld_base
,
2524 if (opcode
== TGSI_OPCODE_TXP
)
2525 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2531 if (opcode
== TGSI_OPCODE_TXP
)
2532 coords
[3] = bld_base
->base
.one
;
2535 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
2536 /* The offsets are six-bit signed integers packed like this:
2537 * X=[5:0], Y=[13:8], and Z=[21:16].
2539 LLVMValueRef offset
[3], pack
;
2541 assert(inst
->Texture
.NumOffsets
== 1);
2543 for (chan
= 0; chan
< 3; chan
++) {
2544 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
2545 emit_data
->inst
, 0, chan
);
2546 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
2547 lp_build_const_int32(gallivm
, 0x3f), "");
2549 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
2550 lp_build_const_int32(gallivm
, chan
*8), "");
2553 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
2554 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
2555 address
[count
++] = pack
;
2558 /* Pack LOD bias value */
2559 if (opcode
== TGSI_OPCODE_TXB
)
2560 address
[count
++] = coords
[3];
2561 if (opcode
== TGSI_OPCODE_TXB2
)
2562 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2564 /* Pack depth comparison value */
2565 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
2566 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
2567 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2569 assert(ref_pos
>= 0);
2570 address
[count
++] = coords
[ref_pos
];
2574 /* Pack user derivatives */
2575 if (opcode
== TGSI_OPCODE_TXD
) {
2576 int param
, num_src_deriv_channels
;
2579 case TGSI_TEXTURE_3D
:
2580 num_src_deriv_channels
= 3;
2581 num_deriv_channels
= 3;
2583 case TGSI_TEXTURE_2D
:
2584 case TGSI_TEXTURE_SHADOW2D
:
2585 case TGSI_TEXTURE_RECT
:
2586 case TGSI_TEXTURE_SHADOWRECT
:
2587 case TGSI_TEXTURE_2D_ARRAY
:
2588 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2589 num_src_deriv_channels
= 2;
2590 num_deriv_channels
= 2;
2592 case TGSI_TEXTURE_CUBE
:
2593 case TGSI_TEXTURE_SHADOWCUBE
:
2594 case TGSI_TEXTURE_CUBE_ARRAY
:
2595 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2596 /* Cube derivatives will be converted to 2D. */
2597 num_src_deriv_channels
= 3;
2598 num_deriv_channels
= 2;
2600 case TGSI_TEXTURE_1D
:
2601 case TGSI_TEXTURE_SHADOW1D
:
2602 case TGSI_TEXTURE_1D_ARRAY
:
2603 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2604 num_src_deriv_channels
= 1;
2605 num_deriv_channels
= 1;
2608 unreachable("invalid target");
2611 for (param
= 0; param
< 2; param
++)
2612 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
2613 derivs
[param
* num_src_deriv_channels
+ chan
] =
2614 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
2617 if (target
== TGSI_TEXTURE_CUBE
||
2618 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2619 target
== TGSI_TEXTURE_SHADOWCUBE
||
2620 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
2621 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
2623 if (opcode
== TGSI_OPCODE_TXD
)
2624 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
2625 address
[count
++] = derivs
[i
];
2627 /* Pack texture coordinates */
2628 address
[count
++] = coords
[0];
2630 address
[count
++] = coords
[1];
2632 address
[count
++] = coords
[2];
2634 /* Pack LOD or sample index */
2635 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
2636 address
[count
++] = coords
[3];
2637 else if (opcode
== TGSI_OPCODE_TXL2
)
2638 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2641 assert(!"Cannot handle more than 16 texture address parameters");
2645 for (chan
= 0; chan
< count
; chan
++ ) {
2646 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
2647 address
[chan
], i32
, "");
2650 /* Adjust the sample index according to FMASK.
2652 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2653 * which is the identity mapping. Each nibble says which physical sample
2654 * should be fetched to get that sample.
2656 * For example, 0x11111100 means there are only 2 samples stored and
2657 * the second sample covers 3/4 of the pixel. When reading samples 0
2658 * and 1, return physical sample 0 (determined by the first two 0s
2659 * in FMASK), otherwise return physical sample 1.
2661 * The sample index should be adjusted as follows:
2662 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2664 if (target
== TGSI_TEXTURE_2D_MSAA
||
2665 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2666 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2667 struct lp_build_emit_data txf_emit_data
= *emit_data
;
2668 LLVMValueRef txf_address
[4];
2669 unsigned txf_count
= count
;
2670 struct tgsi_full_instruction inst
= {};
2672 memcpy(txf_address
, address
, sizeof(txf_address
));
2674 if (target
== TGSI_TEXTURE_2D_MSAA
) {
2675 txf_address
[2] = bld_base
->uint_bld
.zero
;
2677 txf_address
[3] = bld_base
->uint_bld
.zero
;
2679 /* Read FMASK using TXF. */
2680 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
2681 inst
.Texture
.Texture
= target
;
2682 txf_emit_data
.inst
= &inst
;
2683 txf_emit_data
.chan
= 0;
2684 set_tex_fetch_args(gallivm
, &txf_emit_data
, TGSI_OPCODE_TXF
,
2685 target
, fmask_ptr
, NULL
,
2686 txf_address
, txf_count
, 0xf);
2687 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
2689 /* Initialize some constants. */
2690 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
2691 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
2693 /* Apply the formula. */
2694 LLVMValueRef fmask
=
2695 LLVMBuildExtractElement(gallivm
->builder
,
2696 txf_emit_data
.output
[0],
2697 uint_bld
->zero
, "");
2699 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
2701 LLVMValueRef sample_index4
=
2702 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
2704 LLVMValueRef shifted_fmask
=
2705 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
2707 LLVMValueRef final_sample
=
2708 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
2710 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2711 * resource descriptor is 0 (invalid),
2713 LLVMValueRef fmask_desc
=
2714 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
2715 LLVMVectorType(uint_bld
->elem_type
, 8), "");
2717 LLVMValueRef fmask_word1
=
2718 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
2721 LLVMValueRef word1_is_nonzero
=
2722 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
2723 fmask_word1
, uint_bld
->zero
, "");
2725 /* Replace the MSAA sample index. */
2726 address
[sample_chan
] =
2727 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
2728 final_sample
, address
[sample_chan
], "");
2731 if (opcode
== TGSI_OPCODE_TXF
) {
2732 /* add tex offsets */
2733 if (inst
->Texture
.NumOffsets
) {
2734 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2735 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2736 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
2738 assert(inst
->Texture
.NumOffsets
== 1);
2741 case TGSI_TEXTURE_3D
:
2742 address
[2] = lp_build_add(uint_bld
, address
[2],
2743 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
2745 case TGSI_TEXTURE_2D
:
2746 case TGSI_TEXTURE_SHADOW2D
:
2747 case TGSI_TEXTURE_RECT
:
2748 case TGSI_TEXTURE_SHADOWRECT
:
2749 case TGSI_TEXTURE_2D_ARRAY
:
2750 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2752 lp_build_add(uint_bld
, address
[1],
2753 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
2755 case TGSI_TEXTURE_1D
:
2756 case TGSI_TEXTURE_SHADOW1D
:
2757 case TGSI_TEXTURE_1D_ARRAY
:
2758 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2760 lp_build_add(uint_bld
, address
[0],
2761 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
2763 /* texture offsets do not apply to other texture targets */
2768 if (opcode
== TGSI_OPCODE_TG4
) {
2769 unsigned gather_comp
= 0;
2771 /* DMASK was repurposed for GATHER4. 4 components are always
2772 * returned and DMASK works like a swizzle - it selects
2773 * the component to fetch. The only valid DMASK values are
2774 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
2775 * (red,red,red,red) etc.) The ISA document doesn't mention
2779 /* Get the component index from src1.x for Gather4. */
2780 if (!tgsi_is_shadow_target(target
)) {
2781 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
2782 LLVMValueRef comp_imm
;
2783 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
2785 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
2787 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
2788 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
2789 gather_comp
= CLAMP(gather_comp
, 0, 3);
2792 dmask
= 1 << gather_comp
;
2795 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2796 samp_ptr
, address
, count
, dmask
);
2799 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2800 struct lp_build_tgsi_context
* bld_base
,
2801 struct lp_build_emit_data
* emit_data
)
2803 struct lp_build_context
* base
= &bld_base
->base
;
2804 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
2805 unsigned target
= emit_data
->inst
->Texture
.Texture
;
2806 char intr_name
[127];
2807 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
2808 bool is_shadow
= tgsi_is_shadow_target(target
);
2810 const char *name
= "llvm.SI.image.sample";
2811 const char *infix
= "";
2813 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
2814 /* Just return the buffer size. */
2815 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
2819 if (target
== TGSI_TEXTURE_BUFFER
) {
2820 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2821 base
->gallivm
->builder
,
2822 "llvm.SI.vs.load.input", emit_data
->dst_type
,
2823 emit_data
->args
, emit_data
->arg_count
,
2824 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2829 case TGSI_OPCODE_TXF
:
2830 name
= target
== TGSI_TEXTURE_2D_MSAA
||
2831 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
2832 "llvm.SI.image.load" :
2833 "llvm.SI.image.load.mip";
2837 case TGSI_OPCODE_TXQ
:
2838 name
= "llvm.SI.getresinfo";
2842 case TGSI_OPCODE_LODQ
:
2843 name
= "llvm.SI.getlod";
2847 case TGSI_OPCODE_TEX
:
2848 case TGSI_OPCODE_TEX2
:
2849 case TGSI_OPCODE_TXP
:
2851 case TGSI_OPCODE_TXB
:
2852 case TGSI_OPCODE_TXB2
:
2855 case TGSI_OPCODE_TXL
:
2856 case TGSI_OPCODE_TXL2
:
2859 case TGSI_OPCODE_TXD
:
2862 case TGSI_OPCODE_TG4
:
2863 name
= "llvm.SI.gather4";
2870 if (LLVMGetTypeKind(LLVMTypeOf(emit_data
->args
[0])) == LLVMVectorTypeKind
)
2871 sprintf(type
, ".v%ui32",
2872 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
2874 strcpy(type
, ".i32");
2876 /* Add the type and suffixes .c, .o if needed. */
2877 sprintf(intr_name
, "%s%s%s%s%s",
2878 name
, is_shadow
? ".c" : "", infix
,
2879 has_offset
? ".o" : "", type
);
2881 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2882 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
2883 emit_data
->args
, emit_data
->arg_count
,
2884 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2886 /* Divide the number of layers by 6 to get the number of cubes. */
2887 if (opcode
== TGSI_OPCODE_TXQ
&&
2888 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
2889 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
2890 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2891 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
2892 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
2894 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
2895 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
2896 z
= LLVMBuildSDiv(builder
, z
, six
, "");
2898 emit_data
->output
[emit_data
->chan
] =
2899 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
2903 static void si_llvm_emit_txqs(
2904 const struct lp_build_tgsi_action
* action
,
2905 struct lp_build_tgsi_context
* bld_base
,
2906 struct lp_build_emit_data
* emit_data
)
2908 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2909 LLVMBuilderRef builder
= gallivm
->builder
;
2910 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2911 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2912 LLVMValueRef res
, samples
;
2913 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2915 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2918 /* Read the samples from the descriptor directly. */
2919 res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2920 samples
= LLVMBuildExtractElement(
2922 lp_build_const_int32(gallivm
, 3), "");
2923 samples
= LLVMBuildLShr(builder
, samples
,
2924 lp_build_const_int32(gallivm
, 16), "");
2925 samples
= LLVMBuildAnd(builder
, samples
,
2926 lp_build_const_int32(gallivm
, 0xf), "");
2927 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
2930 emit_data
->output
[emit_data
->chan
] = samples
;
2934 * SI implements derivatives using the local data store (LDS)
2935 * All writes to the LDS happen in all executing threads at
2936 * the same time. TID is the Thread ID for the current
2937 * thread and is a value between 0 and 63, representing
2938 * the thread's position in the wavefront.
2940 * For the pixel shader threads are grouped into quads of four pixels.
2941 * The TIDs of the pixels of a quad are:
2949 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2950 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2951 * the current pixel's column, and masking with 0xfffffffe yields the TID
2952 * of the left pixel of the current pixel's row.
2954 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2955 * adding 2 yields the TID of the pixel below the top pixel.
2957 /* masks for thread ID. */
2958 #define TID_MASK_TOP_LEFT 0xfffffffc
2959 #define TID_MASK_TOP 0xfffffffd
2960 #define TID_MASK_LEFT 0xfffffffe
2962 static void si_llvm_emit_ddxy(
2963 const struct lp_build_tgsi_action
* action
,
2964 struct lp_build_tgsi_context
* bld_base
,
2965 struct lp_build_emit_data
* emit_data
)
2967 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2968 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2969 struct lp_build_context
* base
= &bld_base
->base
;
2970 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2971 unsigned opcode
= inst
->Instruction
.Opcode
;
2972 LLVMValueRef indices
[2];
2973 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
2974 LLVMValueRef tl
, trbl
, result
[4];
2976 unsigned swizzle
[4];
2981 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2983 indices
[0] = bld_base
->uint_bld
.zero
;
2984 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
2985 NULL
, 0, LLVMReadNoneAttribute
);
2986 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
2989 if (opcode
== TGSI_OPCODE_DDX_FINE
)
2990 mask
= TID_MASK_LEFT
;
2991 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
2992 mask
= TID_MASK_TOP
;
2994 mask
= TID_MASK_TOP_LEFT
;
2996 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
2997 lp_build_const_int32(gallivm
, mask
), "");
2998 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3001 /* for DDX we want to next X pixel, DDY next Y pixel. */
3002 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3003 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
3004 lp_build_const_int32(gallivm
, idx
), "");
3005 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3008 for (c
= 0; c
< 4; ++c
) {
3011 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
3012 for (i
= 0; i
< c
; ++i
) {
3013 if (swizzle
[i
] == swizzle
[c
]) {
3014 result
[c
] = result
[i
];
3021 LLVMBuildStore(gallivm
->builder
,
3022 LLVMBuildBitCast(gallivm
->builder
,
3023 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
3027 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
3028 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3030 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
3031 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
3033 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
3036 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
3040 * this takes an I,J coordinate pair,
3041 * and works out the X and Y derivatives.
3042 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3044 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3045 struct lp_build_tgsi_context
*bld_base
,
3046 LLVMValueRef interp_ij
)
3048 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3049 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3050 struct lp_build_context
*base
= &bld_base
->base
;
3051 LLVMValueRef indices
[2];
3052 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3053 LLVMValueRef tl
, tr
, bl
, result
[4];
3057 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3059 indices
[0] = bld_base
->uint_bld
.zero
;
3060 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
3061 NULL
, 0, LLVMReadNoneAttribute
);
3062 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3065 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3066 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3068 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3069 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3072 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3076 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3079 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3080 lp_build_const_int32(gallivm
, 1), "");
3081 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3084 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3085 lp_build_const_int32(gallivm
, 2), "");
3086 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3089 for (c
= 0; c
< 2; ++c
) {
3090 LLVMValueRef store_val
;
3091 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3093 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3094 interp_ij
, c_ll
, "");
3095 LLVMBuildStore(gallivm
->builder
,
3099 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3100 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3102 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3103 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, base
->elem_type
, "");
3105 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3107 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3108 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3110 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3111 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, base
->elem_type
, "");
3113 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3116 return lp_build_gather_values(gallivm
, result
, 4);
3119 static void interp_fetch_args(
3120 struct lp_build_tgsi_context
*bld_base
,
3121 struct lp_build_emit_data
*emit_data
)
3123 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3124 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3125 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3127 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3128 /* offset is in second src, first two channels */
3129 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3132 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3135 emit_data
->arg_count
= 2;
3136 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3137 LLVMValueRef sample_position
;
3138 LLVMValueRef sample_id
;
3139 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3141 /* fetch sample ID, then fetch its sample position,
3142 * and place into first two channels.
3144 sample_id
= lp_build_emit_fetch(bld_base
,
3145 emit_data
->inst
, 1, 0);
3146 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3147 LLVMInt32TypeInContext(gallivm
->context
),
3149 sample_position
= load_sample_position(&si_shader_ctx
->radeon_bld
, sample_id
);
3151 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3153 lp_build_const_int32(gallivm
, 0), "");
3155 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3156 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3158 lp_build_const_int32(gallivm
, 1), "");
3159 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3160 emit_data
->arg_count
= 2;
3164 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3165 struct lp_build_tgsi_context
*bld_base
,
3166 struct lp_build_emit_data
*emit_data
)
3168 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3169 struct si_shader
*shader
= si_shader_ctx
->shader
;
3170 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3171 LLVMValueRef interp_param
;
3172 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3173 const char *intr_name
;
3177 LLVMValueRef attr_number
;
3178 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
3179 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3180 int interp_param_idx
;
3183 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3184 input_index
= inst
->Src
[0].Register
.Index
;
3186 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3187 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3188 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3190 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3192 interp_param_idx
= lookup_interp_param_index(shader
->ps_input_interpolate
[input_index
],
3194 if (interp_param_idx
== -1)
3196 else if (interp_param_idx
)
3197 interp_param
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3199 interp_param
= NULL
;
3201 attr_number
= lp_build_const_int32(gallivm
,
3202 shader
->ps_input_param_offset
[input_index
]);
3204 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3205 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3206 LLVMValueRef ij_out
[2];
3207 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3210 * take the I then J parameters, and the DDX/Y for it, and
3211 * calculate the IJ inputs for the interpolator.
3212 * temp1 = ddx * offset/sample.x + I;
3213 * interp_param.I = ddy * offset/sample.y + temp1;
3214 * temp1 = ddx * offset/sample.x + J;
3215 * interp_param.J = ddy * offset/sample.y + temp1;
3217 for (i
= 0; i
< 2; i
++) {
3218 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3219 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
3220 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3221 ddxy_out
, ix_ll
, "");
3222 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3223 ddxy_out
, iy_ll
, "");
3224 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3225 interp_param
, ix_ll
, "");
3226 LLVMValueRef temp1
, temp2
;
3228 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3229 LLVMFloatTypeInContext(gallivm
->context
), "");
3231 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3233 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3235 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3237 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3239 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
3241 LLVMIntTypeInContext(gallivm
->context
, 32), "");
3243 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
3246 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3247 for (chan
= 0; chan
< 2; chan
++) {
3248 LLVMValueRef args
[4];
3249 LLVMValueRef llvm_chan
;
3252 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3253 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
3255 args
[0] = llvm_chan
;
3256 args
[1] = attr_number
;
3258 args
[3] = interp_param
;
3260 emit_data
->output
[chan
] =
3261 lp_build_intrinsic(gallivm
->builder
, intr_name
,
3262 input_type
, args
, args
[3] ? 4 : 3,
3263 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3267 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3268 struct lp_build_emit_data
*emit_data
)
3270 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3271 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3274 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3276 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
3280 /* Emit one vertex from the geometry shader */
3281 static void si_llvm_emit_vertex(
3282 const struct lp_build_tgsi_action
*action
,
3283 struct lp_build_tgsi_context
*bld_base
,
3284 struct lp_build_emit_data
*emit_data
)
3286 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3287 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3288 struct si_shader
*shader
= si_shader_ctx
->shader
;
3289 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3290 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3291 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
3292 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3293 SI_PARAM_GS2VS_OFFSET
);
3294 LLVMValueRef gs_next_vertex
;
3295 LLVMValueRef can_emit
, kill
;
3296 LLVMValueRef args
[2];
3301 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3303 /* Write vertex attribute values to GSVS ring */
3304 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3305 si_shader_ctx
->gs_next_vertex
[stream
],
3308 /* If this thread has already emitted the declared maximum number of
3309 * vertices, kill it: excessive vertex emissions are not supposed to
3310 * have any effect, and GS threads have no externally observable
3311 * effects other than emitting vertices.
3313 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
3314 lp_build_const_int32(gallivm
,
3315 shader
->selector
->gs_max_out_vertices
), "");
3316 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3317 lp_build_const_float(gallivm
, 1.0f
),
3318 lp_build_const_float(gallivm
, -1.0f
));
3320 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
3321 LLVMVoidTypeInContext(gallivm
->context
), &kill
, 1, 0);
3323 for (i
= 0; i
< info
->num_outputs
; i
++) {
3324 LLVMValueRef
*out_ptr
=
3325 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
3327 for (chan
= 0; chan
< 4; chan
++) {
3328 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3329 LLVMValueRef voffset
=
3330 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
3331 shader
->selector
->gs_max_out_vertices
);
3333 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3334 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3336 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
3338 build_tbuffer_store(si_shader_ctx
,
3339 si_shader_ctx
->gsvs_ring
[stream
],
3341 voffset
, soffset
, 0,
3342 V_008F0C_BUF_DATA_FORMAT_32
,
3343 V_008F0C_BUF_NUM_FORMAT_UINT
,
3347 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3348 lp_build_const_int32(gallivm
, 1));
3350 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
[stream
]);
3352 /* Signal vertex emission */
3353 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
3354 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3355 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3356 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3357 LLVMNoUnwindAttribute
);
3360 /* Cut one primitive from the geometry shader */
3361 static void si_llvm_emit_primitive(
3362 const struct lp_build_tgsi_action
*action
,
3363 struct lp_build_tgsi_context
*bld_base
,
3364 struct lp_build_emit_data
*emit_data
)
3366 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3367 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3368 LLVMValueRef args
[2];
3371 /* Signal primitive cut */
3372 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3373 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
3374 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3375 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3376 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3377 LLVMNoUnwindAttribute
);
3380 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
3381 struct lp_build_tgsi_context
*bld_base
,
3382 struct lp_build_emit_data
*emit_data
)
3384 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3386 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.barrier.local",
3387 LLVMVoidTypeInContext(gallivm
->context
), NULL
, 0,
3388 LLVMNoUnwindAttribute
);
3391 static const struct lp_build_tgsi_action tex_action
= {
3392 .fetch_args
= tex_fetch_args
,
3393 .emit
= build_tex_intrinsic
,
3396 static const struct lp_build_tgsi_action interp_action
= {
3397 .fetch_args
= interp_fetch_args
,
3398 .emit
= build_interp_intrinsic
,
3401 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
3403 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3404 LLVMValueRef args
[3];
3406 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
3408 args
[2] = lp_build_const_int32(gallivm
, 1);
3410 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
3413 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3415 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3419 static void declare_streamout_params(struct si_shader_context
*si_shader_ctx
,
3420 struct pipe_stream_output_info
*so
,
3421 LLVMTypeRef
*params
, LLVMTypeRef i32
,
3422 unsigned *num_params
)
3426 /* Streamout SGPRs. */
3427 if (so
->num_outputs
) {
3428 params
[si_shader_ctx
->param_streamout_config
= (*num_params
)++] = i32
;
3429 params
[si_shader_ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
3431 /* A streamout buffer offset is loaded if the stride is non-zero. */
3432 for (i
= 0; i
< 4; i
++) {
3436 params
[si_shader_ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
3440 static void create_function(struct si_shader_context
*si_shader_ctx
)
3442 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3443 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3444 struct si_shader
*shader
= si_shader_ctx
->shader
;
3445 LLVMTypeRef params
[SI_NUM_PARAMS
], f32
, i8
, i32
, v2i32
, v3i32
, v16i8
, v4i32
, v8i32
;
3446 unsigned i
, last_array_pointer
, last_sgpr
, num_params
;
3448 i8
= LLVMInt8TypeInContext(gallivm
->context
);
3449 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3450 f32
= LLVMFloatTypeInContext(gallivm
->context
);
3451 v2i32
= LLVMVectorType(i32
, 2);
3452 v3i32
= LLVMVectorType(i32
, 3);
3453 v4i32
= LLVMVectorType(i32
, 4);
3454 v8i32
= LLVMVectorType(i32
, 8);
3455 v16i8
= LLVMVectorType(i8
, 16);
3457 params
[SI_PARAM_RW_BUFFERS
] = const_array(v16i8
, SI_NUM_RW_BUFFERS
);
3458 params
[SI_PARAM_CONST
] = const_array(v16i8
, SI_NUM_CONST_BUFFERS
);
3459 params
[SI_PARAM_SAMPLER
] = const_array(v4i32
, SI_NUM_SAMPLER_STATES
);
3460 params
[SI_PARAM_RESOURCE
] = const_array(v8i32
, SI_NUM_SAMPLER_VIEWS
);
3461 last_array_pointer
= SI_PARAM_RESOURCE
;
3463 switch (si_shader_ctx
->type
) {
3464 case TGSI_PROCESSOR_VERTEX
:
3465 params
[SI_PARAM_VERTEX_BUFFER
] = const_array(v16i8
, SI_NUM_VERTEX_BUFFERS
);
3466 last_array_pointer
= SI_PARAM_VERTEX_BUFFER
;
3467 params
[SI_PARAM_BASE_VERTEX
] = i32
;
3468 params
[SI_PARAM_START_INSTANCE
] = i32
;
3469 num_params
= SI_PARAM_START_INSTANCE
+1;
3471 if (shader
->key
.vs
.as_es
) {
3472 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3473 } else if (shader
->key
.vs
.as_ls
) {
3474 params
[SI_PARAM_LS_OUT_LAYOUT
] = i32
;
3475 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
3477 if (shader
->is_gs_copy_shader
) {
3478 last_array_pointer
= SI_PARAM_CONST
;
3479 num_params
= SI_PARAM_CONST
+1;
3481 params
[SI_PARAM_VS_STATE_BITS
] = i32
;
3482 num_params
= SI_PARAM_VS_STATE_BITS
+1;
3485 /* The locations of the other parameters are assigned dynamically. */
3486 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3487 params
, i32
, &num_params
);
3490 last_sgpr
= num_params
-1;
3493 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
3494 params
[si_shader_ctx
->param_rel_auto_id
= num_params
++] = i32
;
3495 params
[si_shader_ctx
->param_vs_prim_id
= num_params
++] = i32
;
3496 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
3499 case TGSI_PROCESSOR_TESS_CTRL
:
3500 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3501 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3502 params
[SI_PARAM_TCS_IN_LAYOUT
] = i32
;
3503 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = i32
;
3504 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
3507 params
[SI_PARAM_PATCH_ID
] = i32
;
3508 params
[SI_PARAM_REL_IDS
] = i32
;
3509 num_params
= SI_PARAM_REL_IDS
+1;
3512 case TGSI_PROCESSOR_TESS_EVAL
:
3513 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3514 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3515 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
3517 if (shader
->key
.tes
.as_es
) {
3518 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3520 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3521 params
, i32
, &num_params
);
3523 last_sgpr
= num_params
- 1;
3526 params
[si_shader_ctx
->param_tes_u
= num_params
++] = f32
;
3527 params
[si_shader_ctx
->param_tes_v
= num_params
++] = f32
;
3528 params
[si_shader_ctx
->param_tes_rel_patch_id
= num_params
++] = i32
;
3529 params
[si_shader_ctx
->param_tes_patch_id
= num_params
++] = i32
;
3532 case TGSI_PROCESSOR_GEOMETRY
:
3533 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
3534 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
3535 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
3538 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
3539 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
3540 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
3541 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
3542 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
3543 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
3544 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
3545 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
3546 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
3549 case TGSI_PROCESSOR_FRAGMENT
:
3550 params
[SI_PARAM_ALPHA_REF
] = f32
;
3551 params
[SI_PARAM_PS_STATE_BITS
] = i32
;
3552 params
[SI_PARAM_PRIM_MASK
] = i32
;
3553 last_sgpr
= SI_PARAM_PRIM_MASK
;
3554 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
3555 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
3556 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
3557 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
3558 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
3559 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
3560 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
3561 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
3562 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
3563 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
3564 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
3565 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
3566 params
[SI_PARAM_FRONT_FACE
] = f32
;
3567 params
[SI_PARAM_ANCILLARY
] = i32
;
3568 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
3569 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
3570 num_params
= SI_PARAM_POS_FIXED_PT
+1;
3574 assert(0 && "unimplemented shader");
3578 assert(num_params
<= Elements(params
));
3579 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
3580 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
3582 if (shader
->dx10_clamp_mode
)
3583 LLVMAddTargetDependentFunctionAttr(si_shader_ctx
->radeon_bld
.main_fn
,
3584 "enable-no-nans-fp-math", "true");
3586 for (i
= 0; i
<= last_sgpr
; ++i
) {
3587 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
3589 /* We tell llvm that array inputs are passed by value to allow Sinking pass
3590 * to move load. Inputs are constant so this is fine. */
3591 if (i
<= last_array_pointer
)
3592 LLVMAddAttribute(P
, LLVMByValAttribute
);
3594 LLVMAddAttribute(P
, LLVMInRegAttribute
);
3597 if (bld_base
->info
&&
3598 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
3599 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
3600 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
3601 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
3602 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
3603 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
3604 si_shader_ctx
->lds
=
3605 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3606 LLVMArrayType(i32
, 64),
3610 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
3611 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
3612 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
) {
3613 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
3614 unsigned vertex_data_dw_size
= 32*32*4;
3615 unsigned patch_data_dw_size
= 32*4;
3616 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
3617 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
3618 unsigned lds_dwords
= patch_dw_size
;
3620 /* The actual size is computed outside of the shader to reduce
3621 * the number of shader variants. */
3622 si_shader_ctx
->lds
=
3623 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3624 LLVMArrayType(i32
, lds_dwords
),
3630 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
3632 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3633 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3634 const struct tgsi_shader_info
* info
= bld_base
->info
;
3636 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
3638 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
3639 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
3644 /* Allocate space for the constant values */
3645 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
3647 /* Load the resource descriptor */
3648 si_shader_ctx
->const_resource
[buf
] =
3649 build_indexed_load_const(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
3651 /* Load the constants, we rely on the code sinking to do the rest */
3652 for (i
= 0; i
< num_const
* 4; ++i
) {
3653 si_shader_ctx
->constants
[buf
][i
] =
3654 buffer_load_const(gallivm
->builder
,
3655 si_shader_ctx
->const_resource
[buf
],
3656 lp_build_const_int32(gallivm
, i
* 4),
3657 bld_base
->base
.elem_type
);
3662 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
3664 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3665 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3666 const struct tgsi_shader_info
* info
= bld_base
->info
;
3668 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
3670 LLVMValueRef res_ptr
, samp_ptr
;
3671 LLVMValueRef offset
;
3673 if (num_samplers
== 0)
3676 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
3677 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
3679 /* Load the resources and samplers, we rely on the code sinking to do the rest */
3680 for (i
= 0; i
< num_samplers
; ++i
) {
3682 offset
= lp_build_const_int32(gallivm
, i
);
3683 si_shader_ctx
->resources
[i
] = build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3686 offset
= lp_build_const_int32(gallivm
, i
);
3687 si_shader_ctx
->samplers
[i
] = build_indexed_load_const(si_shader_ctx
, samp_ptr
, offset
);
3689 /* FMASK resource */
3690 if (info
->is_msaa_sampler
[i
]) {
3691 offset
= lp_build_const_int32(gallivm
, SI_FMASK_TEX_OFFSET
+ i
);
3692 si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ i
] =
3693 build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3698 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
3700 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3701 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3704 /* Streamout can only be used if the shader is compiled as VS. */
3705 if (!si_shader_ctx
->shader
->selector
->so
.num_outputs
||
3706 (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3707 (si_shader_ctx
->shader
->key
.vs
.as_es
||
3708 si_shader_ctx
->shader
->key
.vs
.as_ls
)) ||
3709 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3710 si_shader_ctx
->shader
->key
.tes
.as_es
))
3713 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3714 SI_PARAM_RW_BUFFERS
);
3716 /* Load the resources, we rely on the code sinking to do the rest */
3717 for (i
= 0; i
< 4; ++i
) {
3718 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
3719 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
3720 SI_SO_BUF_OFFSET
+ i
);
3722 si_shader_ctx
->so_buffers
[i
] = build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3728 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
3731 static void preload_ring_buffers(struct si_shader_context
*si_shader_ctx
)
3733 struct gallivm_state
*gallivm
=
3734 si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3736 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3737 SI_PARAM_RW_BUFFERS
);
3739 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3740 si_shader_ctx
->shader
->key
.vs
.as_es
) ||
3741 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3742 si_shader_ctx
->shader
->key
.tes
.as_es
) ||
3743 si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3744 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
3746 si_shader_ctx
->esgs_ring
=
3747 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3750 if (si_shader_ctx
->shader
->is_gs_copy_shader
) {
3751 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
3753 si_shader_ctx
->gsvs_ring
[0] =
3754 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3756 if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3758 for (i
= 0; i
< 4; i
++) {
3759 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
3761 si_shader_ctx
->gsvs_ring
[i
] =
3762 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3767 void si_shader_binary_read_config(const struct si_screen
*sscreen
,
3768 struct si_shader
*shader
,
3769 unsigned symbol_offset
)
3772 const unsigned char *config
=
3773 radeon_shader_binary_config_start(&shader
->binary
,
3776 /* XXX: We may be able to emit some of these values directly rather than
3777 * extracting fields to be emitted later.
3780 for (i
= 0; i
< shader
->binary
.config_size_per_symbol
; i
+= 8) {
3781 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
3782 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
3784 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
3785 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
3786 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
3787 case R_00B848_COMPUTE_PGM_RSRC1
:
3788 shader
->num_sgprs
= MAX2(shader
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
3789 shader
->num_vgprs
= MAX2(shader
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
3790 shader
->float_mode
= G_00B028_FLOAT_MODE(value
);
3792 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
3793 shader
->lds_size
= MAX2(shader
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
3795 case R_00B84C_COMPUTE_PGM_RSRC2
:
3796 shader
->lds_size
= MAX2(shader
->lds_size
, G_00B84C_LDS_SIZE(value
));
3798 case R_0286CC_SPI_PS_INPUT_ENA
:
3799 shader
->spi_ps_input_ena
= value
;
3801 case R_0286E8_SPI_TMPRING_SIZE
:
3802 case R_00B860_COMPUTE_TMPRING_SIZE
:
3803 /* WAVESIZE is in units of 256 dwords. */
3804 shader
->scratch_bytes_per_wave
=
3805 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
3808 fprintf(stderr
, "Warning: Compiler emitted unknown "
3809 "config register: 0x%x\n", reg
);
3815 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
3816 struct si_shader
*shader
,
3817 uint64_t scratch_va
)
3820 uint32_t scratch_rsrc_dword0
= scratch_va
;
3821 uint32_t scratch_rsrc_dword1
=
3822 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
3823 | S_008F04_STRIDE(shader
->scratch_bytes_per_wave
/ 64);
3825 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
3826 const struct radeon_shader_reloc
*reloc
=
3827 &shader
->binary
.relocs
[i
];
3828 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
3829 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3830 &scratch_rsrc_dword0
, 4);
3831 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
3832 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3833 &scratch_rsrc_dword1
, 4);
3838 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
3840 const struct radeon_shader_binary
*binary
= &shader
->binary
;
3841 unsigned code_size
= binary
->code_size
+ binary
->rodata_size
;
3844 r600_resource_reference(&shader
->bo
, NULL
);
3845 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
3846 PIPE_USAGE_IMMUTABLE
,
3851 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, NULL
,
3852 PIPE_TRANSFER_READ_WRITE
);
3853 util_memcpy_cpu_to_le32(ptr
, binary
->code
, binary
->code_size
);
3854 if (binary
->rodata_size
> 0) {
3855 ptr
+= binary
->code_size
;
3856 util_memcpy_cpu_to_le32(ptr
, binary
->rodata
,
3857 binary
->rodata_size
);
3860 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
3864 int si_shader_binary_read(struct si_screen
*sscreen
, struct si_shader
*shader
)
3866 const struct radeon_shader_binary
*binary
= &shader
->binary
;
3869 bool dump
= r600_can_dump_shader(&sscreen
->b
,
3870 shader
->selector
? shader
->selector
->tokens
: NULL
);
3872 si_shader_binary_read_config(sscreen
, shader
, 0);
3873 r
= si_shader_binary_upload(sscreen
, shader
);
3878 if (!(sscreen
->b
.debug_flags
& DBG_NO_ASM
)) {
3879 if (binary
->disasm_string
) {
3880 fprintf(stderr
, "\nShader Disassembly:\n\n");
3881 fprintf(stderr
, "%s\n", binary
->disasm_string
);
3883 fprintf(stderr
, "SI CODE:\n");
3884 for (i
= 0; i
< binary
->code_size
; i
+=4 ) {
3885 fprintf(stderr
, "@0x%x: %02x%02x%02x%02x\n", i
, binary
->code
[i
+ 3],
3886 binary
->code
[i
+ 2], binary
->code
[i
+ 1],
3892 fprintf(stderr
, "*** SHADER STATS ***\n"
3893 "SGPRS: %d\nVGPRS: %d\nCode Size: %d bytes\nLDS: %d blocks\n"
3894 "Scratch: %d bytes per wave\n********************\n",
3895 shader
->num_sgprs
, shader
->num_vgprs
, binary
->code_size
,
3896 shader
->lds_size
, shader
->scratch_bytes_per_wave
);
3901 int si_compile_llvm(struct si_screen
*sscreen
, struct si_shader
*shader
,
3902 LLVMTargetMachineRef tm
, LLVMModuleRef mod
)
3905 bool dump_asm
= r600_can_dump_shader(&sscreen
->b
,
3906 shader
->selector
? shader
->selector
->tokens
: NULL
);
3907 bool dump_ir
= dump_asm
&& !(sscreen
->b
.debug_flags
& DBG_NO_IR
);
3909 r
= radeon_llvm_compile(mod
, &shader
->binary
,
3910 r600_get_llvm_processor_name(sscreen
->b
.family
), dump_ir
, dump_asm
, tm
);
3914 r
= si_shader_binary_read(sscreen
, shader
);
3916 FREE(shader
->binary
.config
);
3917 FREE(shader
->binary
.rodata
);
3918 FREE(shader
->binary
.global_symbol_offsets
);
3919 if (shader
->scratch_bytes_per_wave
== 0) {
3920 FREE(shader
->binary
.code
);
3921 FREE(shader
->binary
.relocs
);
3922 memset(&shader
->binary
, 0,
3923 offsetof(struct radeon_shader_binary
, disasm_string
));
3928 /* Generate code for the hardware VS shader stage to go with a geometry shader */
3929 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
3930 struct si_shader_context
*si_shader_ctx
,
3931 struct si_shader
*gs
, bool dump
)
3933 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
3934 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3935 struct lp_build_context
*base
= &bld_base
->base
;
3936 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3937 struct si_shader
*shader
= si_shader_ctx
->shader
;
3938 struct si_shader_output_values
*outputs
;
3939 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
3940 LLVMValueRef args
[9];
3943 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
3945 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
3946 shader
->is_gs_copy_shader
= true;
3948 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
3950 create_meta_data(si_shader_ctx
);
3951 create_function(si_shader_ctx
);
3952 preload_streamout_buffers(si_shader_ctx
);
3953 preload_ring_buffers(si_shader_ctx
);
3955 args
[0] = si_shader_ctx
->gsvs_ring
[0];
3956 args
[1] = lp_build_mul_imm(uint
,
3957 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3958 si_shader_ctx
->param_vertex_id
),
3960 args
[3] = uint
->zero
;
3961 args
[4] = uint
->one
; /* OFFEN */
3962 args
[5] = uint
->zero
; /* IDXEN */
3963 args
[6] = uint
->one
; /* GLC */
3964 args
[7] = uint
->one
; /* SLC */
3965 args
[8] = uint
->zero
; /* TFE */
3967 /* Fetch vertex data from GSVS ring */
3968 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
3971 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
3972 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
3974 for (chan
= 0; chan
< 4; chan
++) {
3975 args
[2] = lp_build_const_int32(gallivm
,
3977 gs
->selector
->gs_max_out_vertices
* 16 * 4);
3979 outputs
[i
].values
[chan
] =
3980 LLVMBuildBitCast(gallivm
->builder
,
3981 lp_build_intrinsic(gallivm
->builder
,
3982 "llvm.SI.buffer.load.dword.i32.i32",
3983 LLVMInt32TypeInContext(gallivm
->context
),
3985 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
3986 base
->elem_type
, "");
3990 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
3992 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
3995 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
3997 r
= si_compile_llvm(sscreen
, si_shader_ctx
->shader
,
3998 si_shader_ctx
->tm
, bld_base
->base
.gallivm
->module
);
4000 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
4006 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
4010 fprintf(f
, "SHADER KEY\n");
4013 case PIPE_SHADER_VERTEX
:
4014 fprintf(f
, " instance_divisors = {");
4015 for (i
= 0; i
< Elements(key
->vs
.instance_divisors
); i
++)
4016 fprintf(f
, !i
? "%u" : ", %u",
4017 key
->vs
.instance_divisors
[i
]);
4019 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
4020 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
4021 fprintf(f
, " export_prim_id = %u\n", key
->vs
.export_prim_id
);
4024 case PIPE_SHADER_TESS_CTRL
:
4025 fprintf(f
, " prim_mode = %u\n", key
->tcs
.prim_mode
);
4028 case PIPE_SHADER_TESS_EVAL
:
4029 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
4030 fprintf(f
, " export_prim_id = %u\n", key
->tes
.export_prim_id
);
4033 case PIPE_SHADER_GEOMETRY
:
4036 case PIPE_SHADER_FRAGMENT
:
4037 fprintf(f
, " export_16bpc = 0x%X\n", key
->ps
.export_16bpc
);
4038 fprintf(f
, " last_cbuf = %u\n", key
->ps
.last_cbuf
);
4039 fprintf(f
, " color_two_side = %u\n", key
->ps
.color_two_side
);
4040 fprintf(f
, " alpha_func = %u\n", key
->ps
.alpha_func
);
4041 fprintf(f
, " alpha_to_one = %u\n", key
->ps
.alpha_to_one
);
4042 fprintf(f
, " poly_stipple = %u\n", key
->ps
.poly_stipple
);
4043 fprintf(f
, " clamp_color = %u\n", key
->ps
.clamp_color
);
4051 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
4052 struct si_shader
*shader
)
4054 struct si_shader_selector
*sel
= shader
->selector
;
4055 struct tgsi_token
*tokens
= sel
->tokens
;
4056 struct si_shader_context si_shader_ctx
;
4057 struct lp_build_tgsi_context
* bld_base
;
4058 struct tgsi_shader_info stipple_shader_info
;
4061 bool poly_stipple
= sel
->type
== PIPE_SHADER_FRAGMENT
&&
4062 shader
->key
.ps
.poly_stipple
;
4063 bool dump
= r600_can_dump_shader(&sscreen
->b
, sel
->tokens
);
4066 tokens
= util_pstipple_create_fragment_shader(tokens
, NULL
,
4067 SI_POLY_STIPPLE_SAMPLER
);
4068 tgsi_scan_shader(tokens
, &stipple_shader_info
);
4071 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
4072 * conversion fails. */
4073 if (dump
&& !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
4074 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
4075 tgsi_dump(tokens
, 0);
4076 si_dump_streamout(&sel
->so
);
4079 assert(shader
->nparam
== 0);
4081 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
4082 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
4083 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
4085 if (sel
->type
!= PIPE_SHADER_COMPUTE
)
4086 shader
->dx10_clamp_mode
= true;
4088 if (sel
->info
.uses_kill
)
4089 shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
4091 shader
->uses_instanceid
= sel
->info
.uses_instanceid
;
4092 bld_base
->info
= poly_stipple
? &stipple_shader_info
: &sel
->info
;
4093 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
4095 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
4096 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
4097 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
4099 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
4100 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
4101 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
4102 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
4103 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
4104 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
4105 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
4106 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
4107 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
4108 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
4109 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
4110 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
4111 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
4113 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
4114 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
4115 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
4116 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
4118 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
4119 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
4120 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
4122 if (HAVE_LLVM
>= 0x0306) {
4123 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
4124 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
4125 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
4126 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
4129 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
4130 si_shader_ctx
.shader
= shader
;
4131 si_shader_ctx
.type
= tgsi_get_processor_type(tokens
);
4132 si_shader_ctx
.screen
= sscreen
;
4133 si_shader_ctx
.tm
= tm
;
4135 switch (si_shader_ctx
.type
) {
4136 case TGSI_PROCESSOR_VERTEX
:
4137 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
4138 if (shader
->key
.vs
.as_ls
)
4139 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
4140 else if (shader
->key
.vs
.as_es
)
4141 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4143 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4145 case TGSI_PROCESSOR_TESS_CTRL
:
4146 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
4147 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
4148 bld_base
->emit_store
= store_output_tcs
;
4149 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
4151 case TGSI_PROCESSOR_TESS_EVAL
:
4152 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
4153 if (shader
->key
.tes
.as_es
)
4154 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4156 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4158 case TGSI_PROCESSOR_GEOMETRY
:
4159 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
4160 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
4162 case TGSI_PROCESSOR_FRAGMENT
:
4163 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
4164 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
4166 switch (sel
->info
.properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
]) {
4167 case TGSI_FS_DEPTH_LAYOUT_GREATER
:
4168 shader
->db_shader_control
|=
4169 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z
);
4171 case TGSI_FS_DEPTH_LAYOUT_LESS
:
4172 shader
->db_shader_control
|=
4173 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z
);
4178 assert(!"Unsupported shader type");
4182 create_meta_data(&si_shader_ctx
);
4183 create_function(&si_shader_ctx
);
4184 preload_constants(&si_shader_ctx
);
4185 preload_samplers(&si_shader_ctx
);
4186 preload_streamout_buffers(&si_shader_ctx
);
4187 preload_ring_buffers(&si_shader_ctx
);
4189 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4191 for (i
= 0; i
< 4; i
++) {
4192 si_shader_ctx
.gs_next_vertex
[i
] =
4193 lp_build_alloca(bld_base
->base
.gallivm
,
4194 bld_base
->uint_bld
.elem_type
, "");
4198 if (!lp_build_tgsi_llvm(bld_base
, tokens
)) {
4199 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
4203 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
4205 mod
= bld_base
->base
.gallivm
->module
;
4206 r
= si_compile_llvm(sscreen
, shader
, tm
, mod
);
4208 fprintf(stderr
, "LLVM failed to compile shader\n");
4212 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
4214 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4215 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
4216 shader
->gs_copy_shader
->selector
= shader
->selector
;
4217 shader
->gs_copy_shader
->key
= shader
->key
;
4218 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
4219 if ((r
= si_generate_gs_copy_shader(sscreen
, &si_shader_ctx
,
4221 free(shader
->gs_copy_shader
);
4222 shader
->gs_copy_shader
= NULL
;
4228 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
4229 FREE(si_shader_ctx
.constants
[i
]);
4231 tgsi_free_tokens(tokens
);
4235 void si_shader_destroy(struct si_shader
*shader
)
4237 if (shader
->gs_copy_shader
) {
4238 si_shader_destroy(shader
->gs_copy_shader
);
4239 FREE(shader
->gs_copy_shader
);
4242 if (shader
->scratch_bo
)
4243 r600_resource_reference(&shader
->scratch_bo
, NULL
);
4245 r600_resource_reference(&shader
->bo
, NULL
);
4247 FREE(shader
->binary
.code
);
4248 FREE(shader
->binary
.relocs
);
4249 FREE(shader
->binary
.disasm_string
);