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 * Get the value of a shader input parameter and extract a bitfield.
169 static LLVMValueRef
unpack_param(struct si_shader_context
*si_shader_ctx
,
170 unsigned param
, unsigned rshift
,
173 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
174 LLVMValueRef value
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
178 value
= LLVMBuildLShr(gallivm
->builder
, value
,
179 lp_build_const_int32(gallivm
, rshift
), "");
181 if (rshift
+ bitwidth
< 32) {
182 unsigned mask
= (1 << bitwidth
) - 1;
183 value
= LLVMBuildAnd(gallivm
->builder
, value
,
184 lp_build_const_int32(gallivm
, mask
), "");
190 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*si_shader_ctx
)
192 switch (si_shader_ctx
->type
) {
193 case TGSI_PROCESSOR_TESS_CTRL
:
194 return unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 0, 8);
196 case TGSI_PROCESSOR_TESS_EVAL
:
197 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
198 si_shader_ctx
->param_tes_rel_patch_id
);
206 /* Tessellation shaders pass outputs to the next shader using LDS.
208 * LS outputs = TCS inputs
209 * TCS outputs = TES inputs
212 * - TCS inputs for patch 0
213 * - TCS inputs for patch 1
214 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
216 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
217 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
218 * - TCS outputs for patch 1
219 * - Per-patch TCS outputs for patch 1
220 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
221 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
224 * All three shaders VS(LS), TCS, TES share the same LDS space.
228 get_tcs_in_patch_stride(struct si_shader_context
*si_shader_ctx
)
230 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
)
231 return unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
232 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
233 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
241 get_tcs_out_patch_stride(struct si_shader_context
*si_shader_ctx
)
243 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
247 get_tcs_out_patch0_offset(struct si_shader_context
*si_shader_ctx
)
249 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
250 unpack_param(si_shader_ctx
,
251 SI_PARAM_TCS_OUT_OFFSETS
,
257 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
259 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
260 unpack_param(si_shader_ctx
,
261 SI_PARAM_TCS_OUT_OFFSETS
,
267 get_tcs_in_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
269 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
270 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(si_shader_ctx
);
271 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
273 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
277 get_tcs_out_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
279 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
280 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(si_shader_ctx
);
281 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
282 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
284 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
285 LLVMBuildMul(gallivm
->builder
, patch_stride
,
291 get_tcs_out_current_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
293 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
294 LLVMValueRef patch0_patch_data_offset
=
295 get_tcs_out_patch0_patch_data_offset(si_shader_ctx
);
296 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
297 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
299 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
300 LLVMBuildMul(gallivm
->builder
, patch_stride
,
305 static void build_indexed_store(struct si_shader_context
*si_shader_ctx
,
306 LLVMValueRef base_ptr
, LLVMValueRef index
,
309 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
310 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
311 LLVMValueRef indices
[2], pointer
;
313 indices
[0] = bld_base
->uint_bld
.zero
;
316 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
317 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
321 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
322 * It's equivalent to doing a load from &base_ptr[index].
324 * \param base_ptr Where the array starts.
325 * \param index The element index into the array.
327 static LLVMValueRef
build_indexed_load(struct si_shader_context
*si_shader_ctx
,
328 LLVMValueRef base_ptr
, LLVMValueRef index
)
330 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
331 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
332 LLVMValueRef indices
[2], pointer
;
334 indices
[0] = bld_base
->uint_bld
.zero
;
337 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
338 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
342 * Do a load from &base_ptr[index], but also add a flag that it's loading
345 static LLVMValueRef
build_indexed_load_const(
346 struct si_shader_context
* si_shader_ctx
,
347 LLVMValueRef base_ptr
, LLVMValueRef index
)
349 LLVMValueRef result
= build_indexed_load(si_shader_ctx
, base_ptr
, index
);
350 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
354 static LLVMValueRef
get_instance_index_for_fetch(
355 struct radeon_llvm_context
* radeon_bld
,
358 struct si_shader_context
*si_shader_ctx
=
359 si_shader_context(&radeon_bld
->soa
.bld_base
);
360 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
362 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
363 si_shader_ctx
->param_instance_id
);
365 /* The division must be done before START_INSTANCE is added. */
367 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
368 lp_build_const_int32(gallivm
, divisor
), "");
370 return LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
371 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
374 static void declare_input_vs(
375 struct radeon_llvm_context
*radeon_bld
,
376 unsigned input_index
,
377 const struct tgsi_full_declaration
*decl
)
379 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
380 struct gallivm_state
*gallivm
= base
->gallivm
;
381 struct si_shader_context
*si_shader_ctx
=
382 si_shader_context(&radeon_bld
->soa
.bld_base
);
383 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
387 LLVMValueRef t_list_ptr
;
388 LLVMValueRef t_offset
;
390 LLVMValueRef attribute_offset
;
391 LLVMValueRef buffer_index
;
392 LLVMValueRef args
[3];
393 LLVMTypeRef vec4_type
;
396 /* Load the T list */
397 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
399 t_offset
= lp_build_const_int32(gallivm
, input_index
);
401 t_list
= build_indexed_load_const(si_shader_ctx
, t_list_ptr
, t_offset
);
403 /* Build the attribute offset */
404 attribute_offset
= lp_build_const_int32(gallivm
, 0);
407 /* Build index from instance ID, start instance and divisor */
408 si_shader_ctx
->shader
->uses_instanceid
= true;
409 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
411 /* Load the buffer index for vertices. */
412 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
413 si_shader_ctx
->param_vertex_id
);
414 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
415 SI_PARAM_BASE_VERTEX
);
416 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
419 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
421 args
[1] = attribute_offset
;
422 args
[2] = buffer_index
;
423 input
= lp_build_intrinsic(gallivm
->builder
,
424 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
425 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
427 /* Break up the vec4 into individual components */
428 for (chan
= 0; chan
< 4; chan
++) {
429 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
430 /* XXX: Use a helper function for this. There is one in
432 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
433 LLVMBuildExtractElement(gallivm
->builder
,
434 input
, llvm_chan
, "");
438 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
441 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
444 return bld_base
->uint_bld
.zero
;
446 switch (si_shader_ctx
->type
) {
447 case TGSI_PROCESSOR_VERTEX
:
448 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
449 si_shader_ctx
->param_vs_prim_id
);
450 case TGSI_PROCESSOR_TESS_CTRL
:
451 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
453 case TGSI_PROCESSOR_TESS_EVAL
:
454 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
455 si_shader_ctx
->param_tes_patch_id
);
456 case TGSI_PROCESSOR_GEOMETRY
:
457 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
458 SI_PARAM_PRIMITIVE_ID
);
461 return bld_base
->uint_bld
.zero
;
466 * Return the value of tgsi_ind_register for indexing.
467 * This is the indirect index with the constant offset added to it.
469 static LLVMValueRef
get_indirect_index(struct si_shader_context
*si_shader_ctx
,
470 const struct tgsi_ind_register
*ind
,
473 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
476 result
= si_shader_ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
477 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
478 result
= LLVMBuildAdd(gallivm
->builder
, result
,
479 lp_build_const_int32(gallivm
, rel_index
), "");
484 * Calculate a dword address given an input or output register and a stride.
486 static LLVMValueRef
get_dw_address(struct si_shader_context
*si_shader_ctx
,
487 const struct tgsi_full_dst_register
*dst
,
488 const struct tgsi_full_src_register
*src
,
489 LLVMValueRef vertex_dw_stride
,
490 LLVMValueRef base_addr
)
492 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
493 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
494 ubyte
*name
, *index
, *array_first
;
496 struct tgsi_full_dst_register reg
;
498 /* Set the register description. The address computation is the same
499 * for sources and destinations. */
501 reg
.Register
.File
= src
->Register
.File
;
502 reg
.Register
.Index
= src
->Register
.Index
;
503 reg
.Register
.Indirect
= src
->Register
.Indirect
;
504 reg
.Register
.Dimension
= src
->Register
.Dimension
;
505 reg
.Indirect
= src
->Indirect
;
506 reg
.Dimension
= src
->Dimension
;
507 reg
.DimIndirect
= src
->DimIndirect
;
511 /* If the register is 2-dimensional (e.g. an array of vertices
512 * in a primitive), calculate the base address of the vertex. */
513 if (reg
.Register
.Dimension
) {
516 if (reg
.Dimension
.Indirect
)
517 index
= get_indirect_index(si_shader_ctx
, ®
.DimIndirect
,
518 reg
.Dimension
.Index
);
520 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
522 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
523 LLVMBuildMul(gallivm
->builder
, index
,
524 vertex_dw_stride
, ""), "");
527 /* Get information about the register. */
528 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
529 name
= info
->input_semantic_name
;
530 index
= info
->input_semantic_index
;
531 array_first
= info
->input_array_first
;
532 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
533 name
= info
->output_semantic_name
;
534 index
= info
->output_semantic_index
;
535 array_first
= info
->output_array_first
;
541 if (reg
.Register
.Indirect
) {
542 /* Add the relative address of the element. */
543 LLVMValueRef ind_index
;
545 if (reg
.Indirect
.ArrayID
)
546 first
= array_first
[reg
.Indirect
.ArrayID
];
548 first
= reg
.Register
.Index
;
550 ind_index
= get_indirect_index(si_shader_ctx
, ®
.Indirect
,
551 reg
.Register
.Index
- first
);
553 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
554 LLVMBuildMul(gallivm
->builder
, ind_index
,
555 lp_build_const_int32(gallivm
, 4), ""), "");
557 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
559 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
560 index
[reg
.Register
.Index
]);
563 /* Add the base address of the element. */
564 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
565 lp_build_const_int32(gallivm
, param
* 4), "");
571 * \param type output value type
572 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
573 * \param dw_addr address in dwords
575 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
576 enum tgsi_opcode_type type
, unsigned swizzle
,
577 LLVMValueRef dw_addr
)
579 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
580 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
584 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
586 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
587 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
589 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
593 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
594 lp_build_const_int32(gallivm
, swizzle
));
596 value
= build_indexed_load(si_shader_ctx
, si_shader_ctx
->lds
, dw_addr
);
597 return LLVMBuildBitCast(gallivm
->builder
, value
,
598 tgsi2llvmtype(bld_base
, type
), "");
604 * \param swizzle offset (typically 0..3)
605 * \param dw_addr address in dwords
606 * \param value value to store
608 static void lds_store(struct lp_build_tgsi_context
* bld_base
,
609 unsigned swizzle
, LLVMValueRef dw_addr
,
612 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
613 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
615 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
616 lp_build_const_int32(gallivm
, swizzle
));
618 value
= LLVMBuildBitCast(gallivm
->builder
, value
,
619 LLVMInt32TypeInContext(gallivm
->context
), "");
620 build_indexed_store(si_shader_ctx
, si_shader_ctx
->lds
,
624 static LLVMValueRef
fetch_input_tcs(
625 struct lp_build_tgsi_context
*bld_base
,
626 const struct tgsi_full_src_register
*reg
,
627 enum tgsi_opcode_type type
, unsigned swizzle
)
629 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
630 LLVMValueRef dw_addr
, stride
;
632 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
633 dw_addr
= get_tcs_in_current_patch_offset(si_shader_ctx
);
634 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
636 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
639 static LLVMValueRef
fetch_output_tcs(
640 struct lp_build_tgsi_context
*bld_base
,
641 const struct tgsi_full_src_register
*reg
,
642 enum tgsi_opcode_type type
, unsigned swizzle
)
644 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
645 LLVMValueRef dw_addr
, stride
;
647 if (reg
->Register
.Dimension
) {
648 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
649 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
650 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
652 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
653 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
656 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
659 static LLVMValueRef
fetch_input_tes(
660 struct lp_build_tgsi_context
*bld_base
,
661 const struct tgsi_full_src_register
*reg
,
662 enum tgsi_opcode_type type
, unsigned swizzle
)
664 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
665 LLVMValueRef dw_addr
, stride
;
667 if (reg
->Register
.Dimension
) {
668 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
669 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
670 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
672 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
673 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
676 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
679 static void store_output_tcs(struct lp_build_tgsi_context
* bld_base
,
680 const struct tgsi_full_instruction
* inst
,
681 const struct tgsi_opcode_info
* info
,
684 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
685 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
687 LLVMValueRef dw_addr
, stride
;
689 /* Only handle per-patch and per-vertex outputs here.
690 * Vectors will be lowered to scalars and this function will be called again.
692 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
693 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
694 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
698 if (reg
->Register
.Dimension
) {
699 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
700 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
701 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, stride
, dw_addr
);
703 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
704 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, NULL
, dw_addr
);
707 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
708 LLVMValueRef value
= dst
[chan_index
];
710 if (inst
->Instruction
.Saturate
)
711 value
= radeon_llvm_saturate(bld_base
, value
);
713 lds_store(bld_base
, chan_index
, dw_addr
, value
);
717 static LLVMValueRef
fetch_input_gs(
718 struct lp_build_tgsi_context
*bld_base
,
719 const struct tgsi_full_src_register
*reg
,
720 enum tgsi_opcode_type type
,
723 struct lp_build_context
*base
= &bld_base
->base
;
724 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
725 struct si_shader
*shader
= si_shader_ctx
->shader
;
726 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
727 struct gallivm_state
*gallivm
= base
->gallivm
;
728 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
729 LLVMValueRef vtx_offset
;
730 LLVMValueRef args
[9];
731 unsigned vtx_offset_param
;
732 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
733 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
734 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
737 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
738 return get_primitive_id(bld_base
, swizzle
);
740 if (!reg
->Register
.Dimension
)
744 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
746 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
747 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
749 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
753 /* Get the vertex offset parameter */
754 vtx_offset_param
= reg
->Dimension
.Index
;
755 if (vtx_offset_param
< 2) {
756 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
758 assert(vtx_offset_param
< 6);
759 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
761 vtx_offset
= lp_build_mul_imm(uint
,
762 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
766 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
767 args
[0] = si_shader_ctx
->esgs_ring
;
768 args
[1] = vtx_offset
;
769 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
770 args
[3] = uint
->zero
;
771 args
[4] = uint
->one
; /* OFFEN */
772 args
[5] = uint
->zero
; /* IDXEN */
773 args
[6] = uint
->one
; /* GLC */
774 args
[7] = uint
->zero
; /* SLC */
775 args
[8] = uint
->zero
; /* TFE */
777 return LLVMBuildBitCast(gallivm
->builder
,
778 lp_build_intrinsic(gallivm
->builder
,
779 "llvm.SI.buffer.load.dword.i32.i32",
781 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
782 tgsi2llvmtype(bld_base
, type
), "");
785 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
787 switch (interpolate
) {
788 case TGSI_INTERPOLATE_CONSTANT
:
791 case TGSI_INTERPOLATE_LINEAR
:
792 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
793 return SI_PARAM_LINEAR_SAMPLE
;
794 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
795 return SI_PARAM_LINEAR_CENTROID
;
797 return SI_PARAM_LINEAR_CENTER
;
799 case TGSI_INTERPOLATE_COLOR
:
800 case TGSI_INTERPOLATE_PERSPECTIVE
:
801 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
802 return SI_PARAM_PERSP_SAMPLE
;
803 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
804 return SI_PARAM_PERSP_CENTROID
;
806 return SI_PARAM_PERSP_CENTER
;
809 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
814 /* This shouldn't be used by explicit INTERP opcodes. */
815 static LLVMValueRef
get_interp_param(struct si_shader_context
*si_shader_ctx
,
818 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
819 unsigned sample_param
= 0;
820 LLVMValueRef default_ij
, sample_ij
, force_sample
;
822 default_ij
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, param
);
824 /* If the shader doesn't use center/centroid, just return the parameter.
826 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
827 * switch between center/centroid and sample without shader changes.
830 case SI_PARAM_PERSP_CENTROID
:
831 case SI_PARAM_PERSP_CENTER
:
832 if (!si_shader_ctx
->shader
->selector
->forces_persample_interp_for_persp
)
835 sample_param
= SI_PARAM_PERSP_SAMPLE
;
838 case SI_PARAM_LINEAR_CENTROID
:
839 case SI_PARAM_LINEAR_CENTER
:
840 if (!si_shader_ctx
->shader
->selector
->forces_persample_interp_for_linear
)
843 sample_param
= SI_PARAM_LINEAR_SAMPLE
;
850 /* Otherwise, we have to select (i,j) based on a user data SGPR. */
851 sample_ij
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, sample_param
);
853 /* TODO: this can be done more efficiently by switching between
856 force_sample
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
857 SI_PARAM_PS_STATE_BITS
);
858 force_sample
= LLVMBuildTrunc(gallivm
->builder
, force_sample
,
859 LLVMInt1TypeInContext(gallivm
->context
), "");
860 return LLVMBuildSelect(gallivm
->builder
, force_sample
,
861 sample_ij
, default_ij
, "");
864 static void declare_input_fs(
865 struct radeon_llvm_context
*radeon_bld
,
866 unsigned input_index
,
867 const struct tgsi_full_declaration
*decl
)
869 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
870 struct si_shader_context
*si_shader_ctx
=
871 si_shader_context(&radeon_bld
->soa
.bld_base
);
872 struct si_shader
*shader
= si_shader_ctx
->shader
;
873 struct lp_build_context
*uint
= &radeon_bld
->soa
.bld_base
.uint_bld
;
874 struct gallivm_state
*gallivm
= base
->gallivm
;
875 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
876 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
878 LLVMValueRef interp_param
= NULL
;
879 int interp_param_idx
;
880 const char * intr_name
;
883 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
884 * quad begins a new primitive. Bit 0 always needs
886 * [32:16] ParamOffset
889 LLVMValueRef params
= LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
);
890 LLVMValueRef attr_number
;
894 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
895 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
897 radeon_llvm_reg_index_soa(input_index
, chan
);
898 radeon_bld
->inputs
[soa_index
] =
899 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
902 /* RCP for fragcoord.w */
903 radeon_bld
->inputs
[soa_index
] =
904 LLVMBuildFDiv(gallivm
->builder
,
905 lp_build_const_float(gallivm
, 1.0f
),
906 radeon_bld
->inputs
[soa_index
],
912 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
913 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
914 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
915 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
916 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
917 lp_build_const_float(gallivm
, 0.0f
);
918 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
919 lp_build_const_float(gallivm
, 1.0f
);
924 shader
->ps_input_param_offset
[input_index
] = shader
->nparam
++;
925 attr_number
= lp_build_const_int32(gallivm
,
926 shader
->ps_input_param_offset
[input_index
]);
928 shader
->ps_input_interpolate
[input_index
] = decl
->Interp
.Interpolate
;
929 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
930 decl
->Interp
.Location
);
931 if (interp_param_idx
== -1)
933 else if (interp_param_idx
)
934 interp_param
= get_interp_param(si_shader_ctx
, interp_param_idx
);
936 /* fs.constant returns the param from the middle vertex, so it's not
937 * really useful for flat shading. It's meant to be used for custom
938 * interpolation (but the intrinsic can't fetch from the other two
941 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
942 * to do the right thing. The only reason we use fs.constant is that
943 * fs.interp cannot be used on integers, because they can be equal
946 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
948 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
949 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
950 LLVMValueRef args
[4];
951 LLVMValueRef face
, is_face_positive
;
952 LLVMValueRef back_attr_number
=
953 lp_build_const_int32(gallivm
,
954 shader
->ps_input_param_offset
[input_index
] + 1);
956 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
958 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
960 lp_build_const_float(gallivm
, 0.0f
),
964 args
[3] = interp_param
;
965 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
966 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
967 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
968 LLVMValueRef front
, back
;
971 args
[1] = attr_number
;
972 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
973 input_type
, args
, args
[3] ? 4 : 3,
974 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
976 args
[1] = back_attr_number
;
977 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
978 input_type
, args
, args
[3] ? 4 : 3,
979 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
981 radeon_bld
->inputs
[soa_index
] =
982 LLVMBuildSelect(gallivm
->builder
,
990 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
991 LLVMValueRef args
[4];
993 args
[0] = uint
->zero
;
994 args
[1] = attr_number
;
996 args
[3] = interp_param
;
997 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
998 lp_build_intrinsic(gallivm
->builder
, intr_name
,
999 input_type
, args
, args
[3] ? 4 : 3,
1000 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1001 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
1002 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
1003 lp_build_const_float(gallivm
, 0.0f
);
1004 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
1005 lp_build_const_float(gallivm
, 1.0f
);
1007 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1008 LLVMValueRef args
[4];
1009 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1010 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
1011 args
[0] = llvm_chan
;
1012 args
[1] = attr_number
;
1014 args
[3] = interp_param
;
1015 radeon_bld
->inputs
[soa_index
] =
1016 lp_build_intrinsic(gallivm
->builder
, intr_name
,
1017 input_type
, args
, args
[3] ? 4 : 3,
1018 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1023 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1025 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1026 SI_PARAM_ANCILLARY
, 8, 4);
1030 * Load a dword from a constant buffer.
1032 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1033 LLVMValueRef offset
, LLVMTypeRef return_type
)
1035 LLVMValueRef args
[2] = {resource
, offset
};
1037 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1038 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1041 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1043 struct si_shader_context
*si_shader_ctx
=
1044 si_shader_context(&radeon_bld
->soa
.bld_base
);
1045 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1046 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1047 LLVMBuilderRef builder
= gallivm
->builder
;
1048 LLVMValueRef desc
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1049 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1050 LLVMValueRef resource
= build_indexed_load_const(si_shader_ctx
, desc
, buf_index
);
1052 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1053 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1054 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1056 LLVMValueRef pos
[4] = {
1057 buffer_load_const(builder
, resource
, offset0
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1058 buffer_load_const(builder
, resource
, offset1
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1059 lp_build_const_float(gallivm
, 0),
1060 lp_build_const_float(gallivm
, 0)
1063 return lp_build_gather_values(gallivm
, pos
, 4);
1066 static void declare_system_value(
1067 struct radeon_llvm_context
* radeon_bld
,
1069 const struct tgsi_full_declaration
*decl
)
1071 struct si_shader_context
*si_shader_ctx
=
1072 si_shader_context(&radeon_bld
->soa
.bld_base
);
1073 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1074 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1075 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1076 LLVMValueRef value
= 0;
1078 switch (decl
->Semantic
.Name
) {
1079 case TGSI_SEMANTIC_INSTANCEID
:
1080 value
= LLVMGetParam(radeon_bld
->main_fn
,
1081 si_shader_ctx
->param_instance_id
);
1084 case TGSI_SEMANTIC_VERTEXID
:
1085 value
= LLVMBuildAdd(gallivm
->builder
,
1086 LLVMGetParam(radeon_bld
->main_fn
,
1087 si_shader_ctx
->param_vertex_id
),
1088 LLVMGetParam(radeon_bld
->main_fn
,
1089 SI_PARAM_BASE_VERTEX
), "");
1092 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1093 value
= LLVMGetParam(radeon_bld
->main_fn
,
1094 si_shader_ctx
->param_vertex_id
);
1097 case TGSI_SEMANTIC_BASEVERTEX
:
1098 value
= LLVMGetParam(radeon_bld
->main_fn
,
1099 SI_PARAM_BASE_VERTEX
);
1102 case TGSI_SEMANTIC_INVOCATIONID
:
1103 if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1104 value
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1105 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1106 value
= LLVMGetParam(radeon_bld
->main_fn
,
1107 SI_PARAM_GS_INSTANCE_ID
);
1109 assert(!"INVOCATIONID not implemented");
1112 case TGSI_SEMANTIC_SAMPLEID
:
1113 value
= get_sample_id(radeon_bld
);
1116 case TGSI_SEMANTIC_SAMPLEPOS
:
1117 value
= load_sample_position(radeon_bld
, get_sample_id(radeon_bld
));
1120 case TGSI_SEMANTIC_SAMPLEMASK
:
1121 /* Smoothing isn't MSAA in GL, but it's MSAA in hardware.
1122 * Therefore, force gl_SampleMaskIn to 1 for GL. */
1123 if (si_shader_ctx
->shader
->key
.ps
.poly_line_smoothing
)
1124 value
= uint_bld
->one
;
1126 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1129 case TGSI_SEMANTIC_TESSCOORD
:
1131 LLVMValueRef coord
[4] = {
1132 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_u
),
1133 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_v
),
1138 /* For triangles, the vector should be (u, v, 1-u-v). */
1139 if (si_shader_ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1140 PIPE_PRIM_TRIANGLES
)
1141 coord
[2] = lp_build_sub(bld
, bld
->one
,
1142 lp_build_add(bld
, coord
[0], coord
[1]));
1144 value
= lp_build_gather_values(gallivm
, coord
, 4);
1148 case TGSI_SEMANTIC_VERTICESIN
:
1149 value
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1152 case TGSI_SEMANTIC_TESSINNER
:
1153 case TGSI_SEMANTIC_TESSOUTER
:
1155 LLVMValueRef dw_addr
;
1156 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1158 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
1159 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1160 lp_build_const_int32(gallivm
, param
* 4), "");
1162 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1167 case TGSI_SEMANTIC_PRIMID
:
1168 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1172 assert(!"unknown system value");
1176 radeon_bld
->system_values
[index
] = value
;
1179 static LLVMValueRef
fetch_constant(
1180 struct lp_build_tgsi_context
* bld_base
,
1181 const struct tgsi_full_src_register
*reg
,
1182 enum tgsi_opcode_type type
,
1185 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1186 struct lp_build_context
* base
= &bld_base
->base
;
1187 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1190 LLVMValueRef addr
, bufp
;
1191 LLVMValueRef result
;
1193 if (swizzle
== LP_CHAN_ALL
) {
1195 LLVMValueRef values
[4];
1196 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1197 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1199 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1202 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1203 idx
= reg
->Register
.Index
* 4 + swizzle
;
1205 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1206 if (type
!= TGSI_TYPE_DOUBLE
)
1207 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
1209 return radeon_llvm_emit_fetch_double(bld_base
,
1210 si_shader_ctx
->constants
[buf
][idx
],
1211 si_shader_ctx
->constants
[buf
][idx
+ 1]);
1215 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1216 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1218 index
= get_indirect_index(si_shader_ctx
, ®
->DimIndirect
,
1219 reg
->Dimension
.Index
);
1220 bufp
= build_indexed_load_const(si_shader_ctx
, ptr
, index
);
1222 bufp
= si_shader_ctx
->const_resource
[buf
];
1224 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1225 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1226 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1227 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1228 lp_build_const_int32(base
->gallivm
, idx
* 4));
1230 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1231 addr
, bld_base
->base
.elem_type
);
1233 if (type
!= TGSI_TYPE_DOUBLE
)
1234 result
= bitcast(bld_base
, type
, result
);
1236 LLVMValueRef addr2
, result2
;
1237 addr2
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1238 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1239 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1240 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1241 lp_build_const_int32(base
->gallivm
, idx
* 4));
1243 result2
= buffer_load_const(base
->gallivm
->builder
, si_shader_ctx
->const_resource
[buf
],
1244 addr2
, bld_base
->base
.elem_type
);
1246 result
= radeon_llvm_emit_fetch_double(bld_base
,
1252 /* Initialize arguments for the shader export intrinsic */
1253 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1254 LLVMValueRef
*values
,
1258 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1259 struct lp_build_context
*uint
=
1260 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1261 struct lp_build_context
*base
= &bld_base
->base
;
1262 unsigned compressed
= 0;
1265 /* XXX: This controls which components of the output
1266 * registers actually get exported. (e.g bit 0 means export
1267 * X component, bit 1 means export Y component, etc.) I'm
1268 * hard coding this to 0xf for now. In the future, we might
1269 * want to do something else.
1271 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1273 /* Specify whether the EXEC mask represents the valid mask */
1274 args
[1] = uint
->zero
;
1276 /* Specify whether this is the last export */
1277 args
[2] = uint
->zero
;
1279 /* Specify the target we are exporting */
1280 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1282 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1283 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1285 if (cbuf
>= 0 && cbuf
< 8) {
1286 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
1289 si_shader_ctx
->shader
->spi_shader_col_format
|=
1290 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
1292 si_shader_ctx
->shader
->spi_shader_col_format
|=
1293 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
1295 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
1299 /* Set COMPR flag */
1300 args
[4] = compressed
? uint
->one
: uint
->zero
;
1303 /* Pixel shader needs to pack output values before export */
1304 for (chan
= 0; chan
< 2; chan
++) {
1305 LLVMValueRef pack_args
[2] = {
1307 values
[2 * chan
+ 1]
1309 LLVMValueRef packed
;
1311 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1313 LLVMInt32TypeInContext(base
->gallivm
->context
),
1315 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1316 args
[chan
+ 7] = args
[chan
+ 5] =
1317 LLVMBuildBitCast(base
->gallivm
->builder
,
1319 LLVMFloatTypeInContext(base
->gallivm
->context
),
1323 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1326 /* Load from output pointers and initialize arguments for the shader export intrinsic */
1327 static void si_llvm_init_export_args_load(struct lp_build_tgsi_context
*bld_base
,
1328 LLVMValueRef
*out_ptr
,
1332 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1333 LLVMValueRef values
[4];
1336 for (i
= 0; i
< 4; i
++)
1337 values
[i
] = LLVMBuildLoad(gallivm
->builder
, out_ptr
[i
], "");
1339 si_llvm_init_export_args(bld_base
, values
, target
, args
);
1342 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1343 LLVMValueRef alpha_ptr
)
1345 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1346 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1348 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
1349 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1350 SI_PARAM_ALPHA_REF
);
1352 LLVMValueRef alpha_pass
=
1353 lp_build_cmp(&bld_base
->base
,
1354 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
1355 LLVMBuildLoad(gallivm
->builder
, alpha_ptr
, ""),
1358 lp_build_select(&bld_base
->base
,
1360 lp_build_const_float(gallivm
, 1.0f
),
1361 lp_build_const_float(gallivm
, -1.0f
));
1363 lp_build_intrinsic(gallivm
->builder
,
1365 LLVMVoidTypeInContext(gallivm
->context
),
1368 lp_build_intrinsic(gallivm
->builder
,
1370 LLVMVoidTypeInContext(gallivm
->context
),
1374 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
1377 static void si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1378 LLVMValueRef alpha_ptr
)
1380 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1381 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1382 LLVMValueRef coverage
, alpha
;
1384 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1385 coverage
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1386 SI_PARAM_SAMPLE_COVERAGE
);
1387 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1389 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1390 bld_base
->int_bld
.elem_type
,
1391 &coverage
, 1, LLVMReadNoneAttribute
);
1393 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1394 bld_base
->base
.elem_type
, "");
1396 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1397 lp_build_const_float(gallivm
,
1398 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1400 alpha
= LLVMBuildLoad(gallivm
->builder
, alpha_ptr
, "");
1401 alpha
= LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1402 LLVMBuildStore(gallivm
->builder
, alpha
, alpha_ptr
);
1405 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
1406 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1408 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1409 struct lp_build_context
*base
= &bld_base
->base
;
1410 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1413 unsigned const_chan
;
1414 LLVMValueRef base_elt
;
1415 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1416 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1417 LLVMValueRef const_resource
= build_indexed_load_const(si_shader_ctx
, ptr
, constbuf_index
);
1419 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1420 LLVMValueRef
*args
= pos
[2 + reg_index
];
1425 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1427 /* Compute dot products of position and user clip plane vectors */
1428 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1429 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1430 args
[1] = lp_build_const_int32(base
->gallivm
,
1431 ((reg_index
* 4 + chan
) * 4 +
1433 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1434 args
[1], base
->elem_type
);
1436 lp_build_add(base
, args
[5 + chan
],
1437 lp_build_mul(base
, base_elt
,
1438 out_elts
[const_chan
]));
1442 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1443 args
[1] = uint
->zero
;
1444 args
[2] = uint
->zero
;
1445 args
[3] = lp_build_const_int32(base
->gallivm
,
1446 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1447 args
[4] = uint
->zero
;
1451 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1455 if (so
->num_outputs
)
1456 fprintf(stderr
, "STREAMOUT\n");
1458 for (i
= 0; i
< so
->num_outputs
; i
++) {
1459 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1460 so
->output
[i
].start_component
;
1461 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1462 i
, so
->output
[i
].output_buffer
,
1463 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1464 so
->output
[i
].register_index
,
1465 mask
& 1 ? "x" : "",
1466 mask
& 2 ? "y" : "",
1467 mask
& 4 ? "z" : "",
1468 mask
& 8 ? "w" : "");
1472 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1473 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1474 * or v4i32 (num_channels=3,4). */
1475 static void build_tbuffer_store(struct si_shader_context
*shader
,
1478 unsigned num_channels
,
1480 LLVMValueRef soffset
,
1481 unsigned inst_offset
,
1490 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1491 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1492 LLVMValueRef args
[] = {
1495 LLVMConstInt(i32
, num_channels
, 0),
1498 LLVMConstInt(i32
, inst_offset
, 0),
1499 LLVMConstInt(i32
, dfmt
, 0),
1500 LLVMConstInt(i32
, nfmt
, 0),
1501 LLVMConstInt(i32
, offen
, 0),
1502 LLVMConstInt(i32
, idxen
, 0),
1503 LLVMConstInt(i32
, glc
, 0),
1504 LLVMConstInt(i32
, slc
, 0),
1505 LLVMConstInt(i32
, tfe
, 0)
1508 /* The instruction offset field has 12 bits */
1509 assert(offen
|| inst_offset
< (1 << 12));
1511 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1512 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1513 const char *types
[] = {"i32", "v2i32", "v4i32"};
1515 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1517 lp_build_intrinsic(gallivm
->builder
, name
,
1518 LLVMVoidTypeInContext(gallivm
->context
),
1519 args
, Elements(args
), 0);
1522 static void build_tbuffer_store_dwords(struct si_shader_context
*shader
,
1525 unsigned num_channels
,
1527 LLVMValueRef soffset
,
1528 unsigned inst_offset
)
1530 static unsigned dfmt
[] = {
1531 V_008F0C_BUF_DATA_FORMAT_32
,
1532 V_008F0C_BUF_DATA_FORMAT_32_32
,
1533 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1534 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1536 assert(num_channels
>= 1 && num_channels
<= 4);
1538 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1539 inst_offset
, dfmt
[num_channels
-1],
1540 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1543 /* On SI, the vertex shader is responsible for writing streamout data
1545 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
1546 struct si_shader_output_values
*outputs
,
1549 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
1550 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1551 LLVMBuilderRef builder
= gallivm
->builder
;
1553 struct lp_build_if_state if_ctx
;
1555 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1557 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1558 LLVMValueRef so_vtx_count
=
1559 unpack_param(shader
, shader
->param_streamout_config
, 16, 7);
1561 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", i32
,
1562 NULL
, 0, LLVMReadNoneAttribute
);
1564 /* can_emit = tid < so_vtx_count; */
1565 LLVMValueRef can_emit
=
1566 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1568 LLVMValueRef stream_id
=
1569 unpack_param(shader
, shader
->param_streamout_config
, 24, 2);
1571 /* Emit the streamout code conditionally. This actually avoids
1572 * out-of-bounds buffer access. The hw tells us via the SGPR
1573 * (so_vtx_count) which threads are allowed to emit streamout data. */
1574 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1576 /* The buffer offset is computed as follows:
1577 * ByteOffset = streamout_offset[buffer_id]*4 +
1578 * (streamout_write_index + thread_id)*stride[buffer_id] +
1582 LLVMValueRef so_write_index
=
1583 LLVMGetParam(shader
->radeon_bld
.main_fn
,
1584 shader
->param_streamout_write_index
);
1586 /* Compute (streamout_write_index + thread_id). */
1587 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1589 /* Compute the write offset for each enabled buffer. */
1590 LLVMValueRef so_write_offset
[4] = {};
1591 for (i
= 0; i
< 4; i
++) {
1595 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
1596 shader
->param_streamout_offset
[i
]);
1597 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
1599 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1600 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
1601 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1604 /* Write streamout data. */
1605 for (i
= 0; i
< so
->num_outputs
; i
++) {
1606 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1607 unsigned reg
= so
->output
[i
].register_index
;
1608 unsigned start
= so
->output
[i
].start_component
;
1609 unsigned num_comps
= so
->output
[i
].num_components
;
1610 unsigned stream
= so
->output
[i
].stream
;
1611 LLVMValueRef out
[4];
1612 struct lp_build_if_state if_ctx_stream
;
1614 assert(num_comps
&& num_comps
<= 4);
1615 if (!num_comps
|| num_comps
> 4)
1621 /* Load the output as int. */
1622 for (j
= 0; j
< num_comps
; j
++) {
1623 out
[j
] = LLVMBuildBitCast(builder
,
1624 outputs
[reg
].values
[start
+j
],
1628 /* Pack the output. */
1629 LLVMValueRef vdata
= NULL
;
1631 switch (num_comps
) {
1632 case 1: /* as i32 */
1635 case 2: /* as v2i32 */
1636 case 3: /* as v4i32 (aligned to 4) */
1637 case 4: /* as v4i32 */
1638 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
1639 for (j
= 0; j
< num_comps
; j
++) {
1640 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1641 LLVMConstInt(i32
, j
, 0), "");
1646 LLVMValueRef can_emit_stream
=
1647 LLVMBuildICmp(builder
, LLVMIntEQ
,
1649 lp_build_const_int32(gallivm
, stream
), "");
1651 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1652 build_tbuffer_store_dwords(shader
, shader
->so_buffers
[buf_idx
],
1654 so_write_offset
[buf_idx
],
1655 LLVMConstInt(i32
, 0, 0),
1656 so
->output
[i
].dst_offset
*4);
1657 lp_build_endif(&if_ctx_stream
);
1660 lp_build_endif(&if_ctx
);
1664 /* Generate export instructions for hardware VS shader stage */
1665 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1666 struct si_shader_output_values
*outputs
,
1669 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1670 struct si_shader
* shader
= si_shader_ctx
->shader
;
1671 struct lp_build_context
* base
= &bld_base
->base
;
1672 struct lp_build_context
* uint
=
1673 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1674 LLVMValueRef args
[9];
1675 LLVMValueRef pos_args
[4][9] = { { 0 } };
1676 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1677 unsigned semantic_name
, semantic_index
;
1679 unsigned param_count
= 0;
1683 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1684 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1687 for (i
= 0; i
< noutput
; i
++) {
1688 semantic_name
= outputs
[i
].name
;
1689 semantic_index
= outputs
[i
].sid
;
1692 /* Select the correct target */
1693 switch(semantic_name
) {
1694 case TGSI_SEMANTIC_PSIZE
:
1695 psize_value
= outputs
[i
].values
[0];
1697 case TGSI_SEMANTIC_EDGEFLAG
:
1698 edgeflag_value
= outputs
[i
].values
[0];
1700 case TGSI_SEMANTIC_LAYER
:
1701 layer_value
= outputs
[i
].values
[0];
1702 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1703 goto handle_semantic
;
1704 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1705 viewport_index_value
= outputs
[i
].values
[0];
1706 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1707 goto handle_semantic
;
1708 case TGSI_SEMANTIC_POSITION
:
1709 target
= V_008DFC_SQ_EXP_POS
;
1711 case TGSI_SEMANTIC_COLOR
:
1712 case TGSI_SEMANTIC_BCOLOR
:
1713 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1714 shader
->vs_output_param_offset
[i
] = param_count
;
1717 case TGSI_SEMANTIC_CLIPDIST
:
1718 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1720 case TGSI_SEMANTIC_CLIPVERTEX
:
1721 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1723 case TGSI_SEMANTIC_PRIMID
:
1724 case TGSI_SEMANTIC_FOG
:
1725 case TGSI_SEMANTIC_TEXCOORD
:
1726 case TGSI_SEMANTIC_GENERIC
:
1727 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1728 shader
->vs_output_param_offset
[i
] = param_count
;
1734 "Warning: SI unhandled vs output type:%d\n",
1738 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1740 if (target
>= V_008DFC_SQ_EXP_POS
&&
1741 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1742 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1743 args
, sizeof(args
));
1745 lp_build_intrinsic(base
->gallivm
->builder
,
1747 LLVMVoidTypeInContext(base
->gallivm
->context
),
1751 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1752 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1753 goto handle_semantic
;
1757 shader
->nr_param_exports
= param_count
;
1759 /* We need to add the position output manually if it's missing. */
1760 if (!pos_args
[0][0]) {
1761 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1762 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1763 pos_args
[0][2] = uint
->zero
; /* last export? */
1764 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1765 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1766 pos_args
[0][5] = base
->zero
; /* X */
1767 pos_args
[0][6] = base
->zero
; /* Y */
1768 pos_args
[0][7] = base
->zero
; /* Z */
1769 pos_args
[0][8] = base
->one
; /* W */
1772 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1773 if (shader
->selector
->info
.writes_psize
||
1774 shader
->selector
->info
.writes_edgeflag
||
1775 shader
->selector
->info
.writes_viewport_index
||
1776 shader
->selector
->info
.writes_layer
) {
1777 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1778 shader
->selector
->info
.writes_psize
|
1779 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1780 (shader
->selector
->info
.writes_layer
<< 2) |
1781 (shader
->selector
->info
.writes_viewport_index
<< 3));
1782 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1783 pos_args
[1][2] = uint
->zero
; /* last export? */
1784 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1785 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1786 pos_args
[1][5] = base
->zero
; /* X */
1787 pos_args
[1][6] = base
->zero
; /* Y */
1788 pos_args
[1][7] = base
->zero
; /* Z */
1789 pos_args
[1][8] = base
->zero
; /* W */
1791 if (shader
->selector
->info
.writes_psize
)
1792 pos_args
[1][5] = psize_value
;
1794 if (shader
->selector
->info
.writes_edgeflag
) {
1795 /* The output is a float, but the hw expects an integer
1796 * with the first bit containing the edge flag. */
1797 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1799 bld_base
->uint_bld
.elem_type
, "");
1800 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1802 bld_base
->int_bld
.one
);
1804 /* The LLVM intrinsic expects a float. */
1805 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1807 base
->elem_type
, "");
1810 if (shader
->selector
->info
.writes_layer
)
1811 pos_args
[1][7] = layer_value
;
1813 if (shader
->selector
->info
.writes_viewport_index
)
1814 pos_args
[1][8] = viewport_index_value
;
1817 for (i
= 0; i
< 4; i
++)
1819 shader
->nr_pos_exports
++;
1822 for (i
= 0; i
< 4; i
++) {
1823 if (!pos_args
[i
][0])
1826 /* Specify the target we are exporting */
1827 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1829 if (pos_idx
== shader
->nr_pos_exports
)
1830 /* Specify that this is the last export */
1831 pos_args
[i
][2] = uint
->one
;
1833 lp_build_intrinsic(base
->gallivm
->builder
,
1835 LLVMVoidTypeInContext(base
->gallivm
->context
),
1840 /* This only writes the tessellation factor levels. */
1841 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1843 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1844 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1845 struct si_shader
*shader
= si_shader_ctx
->shader
;
1846 unsigned tess_inner_index
, tess_outer_index
;
1847 LLVMValueRef lds_base
, lds_inner
, lds_outer
;
1848 LLVMValueRef tf_base
, rel_patch_id
, byteoffset
, buffer
, rw_buffers
;
1849 LLVMValueRef out
[6], vec0
, vec1
, invocation_id
;
1850 unsigned stride
, outer_comps
, inner_comps
, i
;
1851 struct lp_build_if_state if_ctx
;
1853 invocation_id
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1855 /* Do this only for invocation 0, because the tess levels are per-patch,
1858 * This can't jump, because invocation 0 executes this. It should
1859 * at least mask out the loads and stores for other invocations.
1861 lp_build_if(&if_ctx
, gallivm
,
1862 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
1863 invocation_id
, bld_base
->uint_bld
.zero
, ""));
1865 /* Determine the layout of one tess factor element in the buffer. */
1866 switch (shader
->key
.tcs
.prim_mode
) {
1867 case PIPE_PRIM_LINES
:
1868 stride
= 2; /* 2 dwords, 1 vec2 store */
1872 case PIPE_PRIM_TRIANGLES
:
1873 stride
= 4; /* 4 dwords, 1 vec4 store */
1877 case PIPE_PRIM_QUADS
:
1878 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
1887 /* Load tess_inner and tess_outer from LDS.
1888 * Any invocation can write them, so we can't get them from a temporary.
1890 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
1891 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
1893 lds_base
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
1894 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
1895 lp_build_const_int32(gallivm
,
1896 tess_inner_index
* 4), "");
1897 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
1898 lp_build_const_int32(gallivm
,
1899 tess_outer_index
* 4), "");
1901 for (i
= 0; i
< outer_comps
; i
++)
1902 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
1903 for (i
= 0; i
< inner_comps
; i
++)
1904 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
1906 /* Convert the outputs to vectors for stores. */
1907 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
1911 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
1913 /* Get the buffer. */
1914 rw_buffers
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1915 SI_PARAM_RW_BUFFERS
);
1916 buffer
= build_indexed_load_const(si_shader_ctx
, rw_buffers
,
1917 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
1919 /* Get the offset. */
1920 tf_base
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1921 SI_PARAM_TESS_FACTOR_OFFSET
);
1922 rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
1923 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
1924 lp_build_const_int32(gallivm
, 4 * stride
), "");
1926 /* Store the outputs. */
1927 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec0
,
1928 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
1930 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec1
,
1931 stride
- 4, byteoffset
, tf_base
, 16);
1932 lp_build_endif(&if_ctx
);
1935 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
* bld_base
)
1937 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1938 struct si_shader
*shader
= si_shader_ctx
->shader
;
1939 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1940 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1942 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1943 si_shader_ctx
->param_rel_auto_id
);
1944 LLVMValueRef vertex_dw_stride
=
1945 unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
1946 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
1947 vertex_dw_stride
, "");
1949 /* Write outputs to LDS. The next shader (TCS aka HS) will read
1950 * its inputs from it. */
1951 for (i
= 0; i
< info
->num_outputs
; i
++) {
1952 LLVMValueRef
*out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
1953 unsigned name
= info
->output_semantic_name
[i
];
1954 unsigned index
= info
->output_semantic_index
[i
];
1955 int param
= si_shader_io_get_unique_index(name
, index
);
1956 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
1957 lp_build_const_int32(gallivm
, param
* 4), "");
1959 for (chan
= 0; chan
< 4; chan
++) {
1960 lds_store(bld_base
, chan
, dw_addr
,
1961 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
1966 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
1968 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1969 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1970 struct si_shader
*es
= si_shader_ctx
->shader
;
1971 struct tgsi_shader_info
*info
= &es
->selector
->info
;
1972 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1973 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1974 si_shader_ctx
->param_es2gs_offset
);
1978 for (i
= 0; i
< info
->num_outputs
; i
++) {
1979 LLVMValueRef
*out_ptr
=
1980 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
1983 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
1984 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
1987 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
1988 info
->output_semantic_index
[i
]);
1990 for (chan
= 0; chan
< 4; chan
++) {
1991 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1992 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1994 build_tbuffer_store(si_shader_ctx
,
1995 si_shader_ctx
->esgs_ring
,
1997 LLVMGetUndef(i32
), soffset
,
1998 (4 * param_index
+ chan
) * 4,
1999 V_008F0C_BUF_DATA_FORMAT_32
,
2000 V_008F0C_BUF_NUM_FORMAT_UINT
,
2006 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2008 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2009 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2010 LLVMValueRef args
[2];
2012 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2013 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2014 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2015 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2016 LLVMNoUnwindAttribute
);
2019 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2021 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2022 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2023 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
2024 struct si_shader_output_values
*outputs
= NULL
;
2027 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2029 /* Vertex color clamping.
2031 * This uses a state constant loaded in a user data SGPR and
2032 * an IF statement is added that clamps all colors if the constant
2035 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
2036 !si_shader_ctx
->shader
->is_gs_copy_shader
) {
2037 struct lp_build_if_state if_ctx
;
2038 LLVMValueRef cond
= NULL
;
2039 LLVMValueRef addr
, val
;
2041 for (i
= 0; i
< info
->num_outputs
; i
++) {
2042 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2043 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2046 /* We've found a color. */
2048 /* The state is in the first bit of the user SGPR. */
2049 cond
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2050 SI_PARAM_VS_STATE_BITS
);
2051 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2052 LLVMInt1TypeInContext(gallivm
->context
), "");
2053 lp_build_if(&if_ctx
, gallivm
, cond
);
2056 for (j
= 0; j
< 4; j
++) {
2057 addr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2058 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2059 val
= radeon_llvm_saturate(bld_base
, val
);
2060 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2065 lp_build_endif(&if_ctx
);
2068 for (i
= 0; i
< info
->num_outputs
; i
++) {
2069 outputs
[i
].name
= info
->output_semantic_name
[i
];
2070 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2072 for (j
= 0; j
< 4; j
++)
2073 outputs
[i
].values
[j
] =
2074 LLVMBuildLoad(gallivm
->builder
,
2075 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2079 /* Export PrimitiveID when PS needs it. */
2080 if (si_vs_exports_prim_id(si_shader_ctx
->shader
)) {
2081 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2083 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2084 get_primitive_id(bld_base
, 0));
2085 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2086 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2087 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2091 si_llvm_export_vs(bld_base
, outputs
, i
);
2095 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2097 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
2098 struct si_shader
* shader
= si_shader_ctx
->shader
;
2099 struct lp_build_context
* base
= &bld_base
->base
;
2100 struct lp_build_context
* uint
= &bld_base
->uint_bld
;
2101 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2102 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2103 LLVMValueRef args
[9];
2104 LLVMValueRef last_args
[9] = { 0 };
2105 int depth_index
= -1, stencil_index
= -1, samplemask_index
= -1;
2108 for (i
= 0; i
< info
->num_outputs
; i
++) {
2109 unsigned semantic_name
= info
->output_semantic_name
[i
];
2110 unsigned semantic_index
= info
->output_semantic_index
[i
];
2112 LLVMValueRef alpha_ptr
;
2114 /* Select the correct target */
2115 switch (semantic_name
) {
2116 case TGSI_SEMANTIC_POSITION
:
2119 case TGSI_SEMANTIC_STENCIL
:
2122 case TGSI_SEMANTIC_SAMPLEMASK
:
2123 samplemask_index
= i
;
2125 case TGSI_SEMANTIC_COLOR
:
2126 target
= V_008DFC_SQ_EXP_MRT
+ semantic_index
;
2127 alpha_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][3];
2129 if (si_shader_ctx
->shader
->key
.ps
.clamp_color
) {
2130 for (int j
= 0; j
< 4; j
++) {
2131 LLVMValueRef ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2132 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
2134 result
= radeon_llvm_saturate(bld_base
, result
);
2135 LLVMBuildStore(builder
, result
, ptr
);
2139 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
2140 LLVMBuildStore(base
->gallivm
->builder
,
2141 base
->one
, alpha_ptr
);
2143 if (semantic_index
== 0 &&
2144 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2145 si_alpha_test(bld_base
, alpha_ptr
);
2147 if (si_shader_ctx
->shader
->key
.ps
.poly_line_smoothing
)
2148 si_scale_alpha_by_sample_mask(bld_base
, alpha_ptr
);
2154 "Warning: SI unhandled fs output type:%d\n",
2158 si_llvm_init_export_args_load(bld_base
,
2159 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
2162 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
2163 /* If there is an export instruction waiting to be emitted, do so now. */
2165 lp_build_intrinsic(base
->gallivm
->builder
,
2167 LLVMVoidTypeInContext(base
->gallivm
->context
),
2171 /* This instruction will be emitted at the end of the shader. */
2172 memcpy(last_args
, args
, sizeof(args
));
2174 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
2175 if (shader
->selector
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
2176 semantic_index
== 0 &&
2177 si_shader_ctx
->shader
->key
.ps
.last_cbuf
> 0) {
2178 for (int c
= 1; c
<= si_shader_ctx
->shader
->key
.ps
.last_cbuf
; c
++) {
2179 si_llvm_init_export_args_load(bld_base
,
2180 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
2181 V_008DFC_SQ_EXP_MRT
+ c
, args
);
2182 lp_build_intrinsic(base
->gallivm
->builder
,
2184 LLVMVoidTypeInContext(base
->gallivm
->context
),
2189 lp_build_intrinsic(base
->gallivm
->builder
,
2191 LLVMVoidTypeInContext(base
->gallivm
->context
),
2196 if (depth_index
>= 0 || stencil_index
>= 0 || samplemask_index
>= 0) {
2197 LLVMValueRef out_ptr
;
2200 /* Specify the target we are exporting */
2201 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2203 args
[5] = base
->zero
; /* R, depth */
2204 args
[6] = base
->zero
; /* G, stencil test value[0:7], stencil op value[8:15] */
2205 args
[7] = base
->zero
; /* B, sample mask */
2206 args
[8] = base
->zero
; /* A, alpha to mask */
2208 if (depth_index
>= 0) {
2209 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
2210 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2212 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_Z_EXPORT_ENABLE(1);
2215 if (stencil_index
>= 0) {
2216 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
2217 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2219 si_shader_ctx
->shader
->db_shader_control
|=
2220 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
2223 if (samplemask_index
>= 0) {
2224 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[samplemask_index
][0];
2225 args
[7] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
2227 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_MASK_EXPORT_ENABLE(1);
2230 /* SI (except OLAND) has a bug that it only looks
2231 * at the X writemask component. */
2232 if (si_shader_ctx
->screen
->b
.chip_class
== SI
&&
2233 si_shader_ctx
->screen
->b
.family
!= CHIP_OLAND
)
2236 if (samplemask_index
>= 0)
2237 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_ABGR
;
2238 else if (stencil_index
>= 0)
2239 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_GR
;
2241 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_R
;
2243 /* Specify which components to enable */
2244 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2248 args
[4] = uint
->zero
;
2251 lp_build_intrinsic(base
->gallivm
->builder
,
2253 LLVMVoidTypeInContext(base
->gallivm
->context
),
2256 memcpy(last_args
, args
, sizeof(args
));
2259 if (!last_args
[0]) {
2260 /* Specify which components to enable */
2261 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
2263 /* Specify the target we are exporting */
2264 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
2266 /* Set COMPR flag to zero to export data as 32-bit */
2267 last_args
[4] = uint
->zero
;
2270 last_args
[5]= uint
->zero
;
2271 last_args
[6]= uint
->zero
;
2272 last_args
[7]= uint
->zero
;
2273 last_args
[8]= uint
->zero
;
2276 /* Specify whether the EXEC mask represents the valid mask */
2277 last_args
[1] = uint
->one
;
2279 /* Specify that this is the last export */
2280 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
2282 lp_build_intrinsic(base
->gallivm
->builder
,
2284 LLVMVoidTypeInContext(base
->gallivm
->context
),
2288 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2289 struct lp_build_tgsi_context
* bld_base
,
2290 struct lp_build_emit_data
* emit_data
);
2292 static bool tgsi_is_array_sampler(unsigned target
)
2294 return target
== TGSI_TEXTURE_1D_ARRAY
||
2295 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2296 target
== TGSI_TEXTURE_2D_ARRAY
||
2297 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2298 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2299 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2300 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2303 static void set_tex_fetch_args(struct gallivm_state
*gallivm
,
2304 struct lp_build_emit_data
*emit_data
,
2305 unsigned opcode
, unsigned target
,
2306 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
2307 LLVMValueRef
*param
, unsigned count
,
2311 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
2312 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2314 /* Pad to power of two vector */
2315 while (count
< util_next_power_of_two(count
))
2316 param
[count
++] = LLVMGetUndef(i32
);
2318 /* Texture coordinates. */
2320 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
2322 emit_data
->args
[0] = param
[0];
2325 emit_data
->args
[1] = res_ptr
;
2328 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
2329 emit_data
->dst_type
= LLVMVectorType(i32
, 4);
2331 emit_data
->dst_type
= LLVMVectorType(
2332 LLVMFloatTypeInContext(gallivm
->context
), 4);
2334 emit_data
->args
[num_args
++] = samp_ptr
;
2337 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
2338 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
2339 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
2340 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
2341 tgsi_is_array_sampler(target
)); /* da */
2342 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
2343 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
2344 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
2345 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
2347 emit_data
->arg_count
= num_args
;
2350 static const struct lp_build_tgsi_action tex_action
;
2352 static void tex_fetch_ptrs(
2353 struct lp_build_tgsi_context
* bld_base
,
2354 struct lp_build_emit_data
* emit_data
,
2355 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
2357 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2358 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2359 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2360 unsigned target
= inst
->Texture
.Texture
;
2361 unsigned sampler_src
;
2362 unsigned sampler_index
;
2364 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
2365 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
2367 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
2368 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
2369 LLVMValueRef ind_index
;
2371 ind_index
= get_indirect_index(si_shader_ctx
, ®
->Indirect
, reg
->Register
.Index
);
2373 *res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2374 *res_ptr
= build_indexed_load_const(si_shader_ctx
, *res_ptr
, ind_index
);
2376 *samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2377 *samp_ptr
= build_indexed_load_const(si_shader_ctx
, *samp_ptr
, ind_index
);
2379 if (target
== TGSI_TEXTURE_2D_MSAA
||
2380 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2381 ind_index
= LLVMBuildAdd(gallivm
->builder
, ind_index
,
2382 lp_build_const_int32(gallivm
,
2383 SI_FMASK_TEX_OFFSET
), "");
2384 *fmask_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2385 *fmask_ptr
= build_indexed_load_const(si_shader_ctx
, *fmask_ptr
, ind_index
);
2388 *res_ptr
= si_shader_ctx
->resources
[sampler_index
];
2389 *samp_ptr
= si_shader_ctx
->samplers
[sampler_index
];
2390 *fmask_ptr
= si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ sampler_index
];
2394 static void tex_fetch_args(
2395 struct lp_build_tgsi_context
* bld_base
,
2396 struct lp_build_emit_data
* emit_data
)
2398 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2399 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2400 LLVMBuilderRef builder
= gallivm
->builder
;
2401 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2402 unsigned opcode
= inst
->Instruction
.Opcode
;
2403 unsigned target
= inst
->Texture
.Texture
;
2404 LLVMValueRef coords
[5], derivs
[6];
2405 LLVMValueRef address
[16];
2407 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
2410 unsigned num_deriv_channels
= 0;
2411 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
2412 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2413 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2414 unsigned dmask
= 0xf;
2416 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2418 if (opcode
== TGSI_OPCODE_TXQ
) {
2419 if (target
== TGSI_TEXTURE_BUFFER
) {
2420 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2422 /* Read the size from the buffer descriptor directly. */
2423 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2424 LLVMValueRef size
= LLVMBuildExtractElement(builder
, res
,
2425 lp_build_const_int32(gallivm
, 6), "");
2427 if (si_shader_ctx
->screen
->b
.chip_class
>= VI
) {
2428 /* On VI, the descriptor contains the size in bytes,
2429 * but TXQ must return the size in elements.
2430 * The stride is always non-zero for resources using TXQ.
2432 LLVMValueRef stride
=
2433 LLVMBuildExtractElement(builder
, res
,
2434 lp_build_const_int32(gallivm
, 5), "");
2435 stride
= LLVMBuildLShr(builder
, stride
,
2436 lp_build_const_int32(gallivm
, 16), "");
2437 stride
= LLVMBuildAnd(builder
, stride
,
2438 lp_build_const_int32(gallivm
, 0x3FFF), "");
2440 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2443 emit_data
->args
[0] = size
;
2447 /* Textures - set the mip level. */
2448 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
2450 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2451 NULL
, address
, count
, 0xf);
2455 if (target
== TGSI_TEXTURE_BUFFER
) {
2456 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
2457 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
2458 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
2459 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
2461 /* Bitcast and truncate v8i32 to v16i8. */
2462 LLVMValueRef res
= res_ptr
;
2463 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
2464 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
2465 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
2467 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2468 emit_data
->args
[0] = res
;
2469 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
2470 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
2471 emit_data
->arg_count
= 3;
2475 /* Fetch and project texture coordinates */
2476 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2477 for (chan
= 0; chan
< 3; chan
++ ) {
2478 coords
[chan
] = lp_build_emit_fetch(bld_base
,
2481 if (opcode
== TGSI_OPCODE_TXP
)
2482 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2488 if (opcode
== TGSI_OPCODE_TXP
)
2489 coords
[3] = bld_base
->base
.one
;
2492 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
2493 /* The offsets are six-bit signed integers packed like this:
2494 * X=[5:0], Y=[13:8], and Z=[21:16].
2496 LLVMValueRef offset
[3], pack
;
2498 assert(inst
->Texture
.NumOffsets
== 1);
2500 for (chan
= 0; chan
< 3; chan
++) {
2501 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
2502 emit_data
->inst
, 0, chan
);
2503 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
2504 lp_build_const_int32(gallivm
, 0x3f), "");
2506 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
2507 lp_build_const_int32(gallivm
, chan
*8), "");
2510 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
2511 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
2512 address
[count
++] = pack
;
2515 /* Pack LOD bias value */
2516 if (opcode
== TGSI_OPCODE_TXB
)
2517 address
[count
++] = coords
[3];
2518 if (opcode
== TGSI_OPCODE_TXB2
)
2519 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2521 /* Pack depth comparison value */
2522 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
2523 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
2524 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2526 assert(ref_pos
>= 0);
2527 address
[count
++] = coords
[ref_pos
];
2531 /* Pack user derivatives */
2532 if (opcode
== TGSI_OPCODE_TXD
) {
2533 int param
, num_src_deriv_channels
;
2536 case TGSI_TEXTURE_3D
:
2537 num_src_deriv_channels
= 3;
2538 num_deriv_channels
= 3;
2540 case TGSI_TEXTURE_2D
:
2541 case TGSI_TEXTURE_SHADOW2D
:
2542 case TGSI_TEXTURE_RECT
:
2543 case TGSI_TEXTURE_SHADOWRECT
:
2544 case TGSI_TEXTURE_2D_ARRAY
:
2545 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2546 num_src_deriv_channels
= 2;
2547 num_deriv_channels
= 2;
2549 case TGSI_TEXTURE_CUBE
:
2550 case TGSI_TEXTURE_SHADOWCUBE
:
2551 case TGSI_TEXTURE_CUBE_ARRAY
:
2552 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2553 /* Cube derivatives will be converted to 2D. */
2554 num_src_deriv_channels
= 3;
2555 num_deriv_channels
= 2;
2557 case TGSI_TEXTURE_1D
:
2558 case TGSI_TEXTURE_SHADOW1D
:
2559 case TGSI_TEXTURE_1D_ARRAY
:
2560 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2561 num_src_deriv_channels
= 1;
2562 num_deriv_channels
= 1;
2565 unreachable("invalid target");
2568 for (param
= 0; param
< 2; param
++)
2569 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
2570 derivs
[param
* num_src_deriv_channels
+ chan
] =
2571 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
2574 if (target
== TGSI_TEXTURE_CUBE
||
2575 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2576 target
== TGSI_TEXTURE_SHADOWCUBE
||
2577 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
2578 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
2580 if (opcode
== TGSI_OPCODE_TXD
)
2581 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
2582 address
[count
++] = derivs
[i
];
2584 /* Pack texture coordinates */
2585 address
[count
++] = coords
[0];
2587 address
[count
++] = coords
[1];
2589 address
[count
++] = coords
[2];
2591 /* Pack LOD or sample index */
2592 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
2593 address
[count
++] = coords
[3];
2594 else if (opcode
== TGSI_OPCODE_TXL2
)
2595 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
2598 assert(!"Cannot handle more than 16 texture address parameters");
2602 for (chan
= 0; chan
< count
; chan
++ ) {
2603 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
2604 address
[chan
], i32
, "");
2607 /* Adjust the sample index according to FMASK.
2609 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2610 * which is the identity mapping. Each nibble says which physical sample
2611 * should be fetched to get that sample.
2613 * For example, 0x11111100 means there are only 2 samples stored and
2614 * the second sample covers 3/4 of the pixel. When reading samples 0
2615 * and 1, return physical sample 0 (determined by the first two 0s
2616 * in FMASK), otherwise return physical sample 1.
2618 * The sample index should be adjusted as follows:
2619 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2621 if (target
== TGSI_TEXTURE_2D_MSAA
||
2622 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2623 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2624 struct lp_build_emit_data txf_emit_data
= *emit_data
;
2625 LLVMValueRef txf_address
[4];
2626 unsigned txf_count
= count
;
2627 struct tgsi_full_instruction inst
= {};
2629 memcpy(txf_address
, address
, sizeof(txf_address
));
2631 if (target
== TGSI_TEXTURE_2D_MSAA
) {
2632 txf_address
[2] = bld_base
->uint_bld
.zero
;
2634 txf_address
[3] = bld_base
->uint_bld
.zero
;
2636 /* Read FMASK using TXF. */
2637 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
2638 inst
.Texture
.Texture
= target
;
2639 txf_emit_data
.inst
= &inst
;
2640 txf_emit_data
.chan
= 0;
2641 set_tex_fetch_args(gallivm
, &txf_emit_data
, TGSI_OPCODE_TXF
,
2642 target
, fmask_ptr
, NULL
,
2643 txf_address
, txf_count
, 0xf);
2644 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
2646 /* Initialize some constants. */
2647 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
2648 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
2650 /* Apply the formula. */
2651 LLVMValueRef fmask
=
2652 LLVMBuildExtractElement(gallivm
->builder
,
2653 txf_emit_data
.output
[0],
2654 uint_bld
->zero
, "");
2656 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
2658 LLVMValueRef sample_index4
=
2659 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
2661 LLVMValueRef shifted_fmask
=
2662 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
2664 LLVMValueRef final_sample
=
2665 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
2667 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2668 * resource descriptor is 0 (invalid),
2670 LLVMValueRef fmask_desc
=
2671 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
2672 LLVMVectorType(uint_bld
->elem_type
, 8), "");
2674 LLVMValueRef fmask_word1
=
2675 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
2678 LLVMValueRef word1_is_nonzero
=
2679 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
2680 fmask_word1
, uint_bld
->zero
, "");
2682 /* Replace the MSAA sample index. */
2683 address
[sample_chan
] =
2684 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
2685 final_sample
, address
[sample_chan
], "");
2688 if (opcode
== TGSI_OPCODE_TXF
) {
2689 /* add tex offsets */
2690 if (inst
->Texture
.NumOffsets
) {
2691 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2692 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2693 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
2695 assert(inst
->Texture
.NumOffsets
== 1);
2698 case TGSI_TEXTURE_3D
:
2699 address
[2] = lp_build_add(uint_bld
, address
[2],
2700 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
2702 case TGSI_TEXTURE_2D
:
2703 case TGSI_TEXTURE_SHADOW2D
:
2704 case TGSI_TEXTURE_RECT
:
2705 case TGSI_TEXTURE_SHADOWRECT
:
2706 case TGSI_TEXTURE_2D_ARRAY
:
2707 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2709 lp_build_add(uint_bld
, address
[1],
2710 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
2712 case TGSI_TEXTURE_1D
:
2713 case TGSI_TEXTURE_SHADOW1D
:
2714 case TGSI_TEXTURE_1D_ARRAY
:
2715 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2717 lp_build_add(uint_bld
, address
[0],
2718 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
2720 /* texture offsets do not apply to other texture targets */
2725 if (opcode
== TGSI_OPCODE_TG4
) {
2726 unsigned gather_comp
= 0;
2728 /* DMASK was repurposed for GATHER4. 4 components are always
2729 * returned and DMASK works like a swizzle - it selects
2730 * the component to fetch. The only valid DMASK values are
2731 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
2732 * (red,red,red,red) etc.) The ISA document doesn't mention
2736 /* Get the component index from src1.x for Gather4. */
2737 if (!tgsi_is_shadow_target(target
)) {
2738 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
2739 LLVMValueRef comp_imm
;
2740 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
2742 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
2744 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
2745 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
2746 gather_comp
= CLAMP(gather_comp
, 0, 3);
2749 dmask
= 1 << gather_comp
;
2752 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2753 samp_ptr
, address
, count
, dmask
);
2756 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2757 struct lp_build_tgsi_context
* bld_base
,
2758 struct lp_build_emit_data
* emit_data
)
2760 struct lp_build_context
* base
= &bld_base
->base
;
2761 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
2762 unsigned target
= emit_data
->inst
->Texture
.Texture
;
2763 char intr_name
[127];
2764 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
2765 bool is_shadow
= tgsi_is_shadow_target(target
);
2767 const char *name
= "llvm.SI.image.sample";
2768 const char *infix
= "";
2770 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
2771 /* Just return the buffer size. */
2772 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
2776 if (target
== TGSI_TEXTURE_BUFFER
) {
2777 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2778 base
->gallivm
->builder
,
2779 "llvm.SI.vs.load.input", emit_data
->dst_type
,
2780 emit_data
->args
, emit_data
->arg_count
,
2781 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2786 case TGSI_OPCODE_TXF
:
2787 name
= target
== TGSI_TEXTURE_2D_MSAA
||
2788 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
2789 "llvm.SI.image.load" :
2790 "llvm.SI.image.load.mip";
2794 case TGSI_OPCODE_TXQ
:
2795 name
= "llvm.SI.getresinfo";
2799 case TGSI_OPCODE_LODQ
:
2800 name
= "llvm.SI.getlod";
2804 case TGSI_OPCODE_TEX
:
2805 case TGSI_OPCODE_TEX2
:
2806 case TGSI_OPCODE_TXP
:
2808 case TGSI_OPCODE_TXB
:
2809 case TGSI_OPCODE_TXB2
:
2812 case TGSI_OPCODE_TXL
:
2813 case TGSI_OPCODE_TXL2
:
2816 case TGSI_OPCODE_TXD
:
2819 case TGSI_OPCODE_TG4
:
2820 name
= "llvm.SI.gather4";
2827 if (LLVMGetTypeKind(LLVMTypeOf(emit_data
->args
[0])) == LLVMVectorTypeKind
)
2828 sprintf(type
, ".v%ui32",
2829 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
2831 strcpy(type
, ".i32");
2833 /* Add the type and suffixes .c, .o if needed. */
2834 sprintf(intr_name
, "%s%s%s%s%s",
2835 name
, is_shadow
? ".c" : "", infix
,
2836 has_offset
? ".o" : "", type
);
2838 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2839 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
2840 emit_data
->args
, emit_data
->arg_count
,
2841 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2843 /* Divide the number of layers by 6 to get the number of cubes. */
2844 if (opcode
== TGSI_OPCODE_TXQ
&&
2845 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
2846 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
2847 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2848 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
2849 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
2851 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
2852 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
2853 z
= LLVMBuildSDiv(builder
, z
, six
, "");
2855 emit_data
->output
[emit_data
->chan
] =
2856 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
2860 static void si_llvm_emit_txqs(
2861 const struct lp_build_tgsi_action
* action
,
2862 struct lp_build_tgsi_context
* bld_base
,
2863 struct lp_build_emit_data
* emit_data
)
2865 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2866 LLVMBuilderRef builder
= gallivm
->builder
;
2867 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2868 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2869 LLVMValueRef res
, samples
;
2870 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2872 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2875 /* Read the samples from the descriptor directly. */
2876 res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2877 samples
= LLVMBuildExtractElement(
2879 lp_build_const_int32(gallivm
, 3), "");
2880 samples
= LLVMBuildLShr(builder
, samples
,
2881 lp_build_const_int32(gallivm
, 16), "");
2882 samples
= LLVMBuildAnd(builder
, samples
,
2883 lp_build_const_int32(gallivm
, 0xf), "");
2884 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
2887 emit_data
->output
[emit_data
->chan
] = samples
;
2891 * SI implements derivatives using the local data store (LDS)
2892 * All writes to the LDS happen in all executing threads at
2893 * the same time. TID is the Thread ID for the current
2894 * thread and is a value between 0 and 63, representing
2895 * the thread's position in the wavefront.
2897 * For the pixel shader threads are grouped into quads of four pixels.
2898 * The TIDs of the pixels of a quad are:
2906 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2907 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2908 * the current pixel's column, and masking with 0xfffffffe yields the TID
2909 * of the left pixel of the current pixel's row.
2911 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2912 * adding 2 yields the TID of the pixel below the top pixel.
2914 /* masks for thread ID. */
2915 #define TID_MASK_TOP_LEFT 0xfffffffc
2916 #define TID_MASK_TOP 0xfffffffd
2917 #define TID_MASK_LEFT 0xfffffffe
2919 static void si_llvm_emit_ddxy(
2920 const struct lp_build_tgsi_action
* action
,
2921 struct lp_build_tgsi_context
* bld_base
,
2922 struct lp_build_emit_data
* emit_data
)
2924 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2925 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2926 struct lp_build_context
* base
= &bld_base
->base
;
2927 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2928 unsigned opcode
= inst
->Instruction
.Opcode
;
2929 LLVMValueRef indices
[2];
2930 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
2931 LLVMValueRef tl
, trbl
, result
[4];
2933 unsigned swizzle
[4];
2938 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2940 indices
[0] = bld_base
->uint_bld
.zero
;
2941 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
2942 NULL
, 0, LLVMReadNoneAttribute
);
2943 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
2946 if (opcode
== TGSI_OPCODE_DDX_FINE
)
2947 mask
= TID_MASK_LEFT
;
2948 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
2949 mask
= TID_MASK_TOP
;
2951 mask
= TID_MASK_TOP_LEFT
;
2953 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
2954 lp_build_const_int32(gallivm
, mask
), "");
2955 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
2958 /* for DDX we want to next X pixel, DDY next Y pixel. */
2959 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
2960 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
2961 lp_build_const_int32(gallivm
, idx
), "");
2962 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
2965 for (c
= 0; c
< 4; ++c
) {
2968 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
2969 for (i
= 0; i
< c
; ++i
) {
2970 if (swizzle
[i
] == swizzle
[c
]) {
2971 result
[c
] = result
[i
];
2978 LLVMBuildStore(gallivm
->builder
,
2979 LLVMBuildBitCast(gallivm
->builder
,
2980 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
2984 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
2985 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
2987 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
2988 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
2990 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
2993 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
2997 * this takes an I,J coordinate pair,
2998 * and works out the X and Y derivatives.
2999 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3001 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3002 struct lp_build_tgsi_context
*bld_base
,
3003 LLVMValueRef interp_ij
)
3005 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3006 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3007 struct lp_build_context
*base
= &bld_base
->base
;
3008 LLVMValueRef indices
[2];
3009 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3010 LLVMValueRef tl
, tr
, bl
, result
[4];
3014 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3016 indices
[0] = bld_base
->uint_bld
.zero
;
3017 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
3018 NULL
, 0, LLVMReadNoneAttribute
);
3019 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3022 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3023 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3025 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3026 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3029 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3033 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3036 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3037 lp_build_const_int32(gallivm
, 1), "");
3038 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3041 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3042 lp_build_const_int32(gallivm
, 2), "");
3043 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3046 for (c
= 0; c
< 2; ++c
) {
3047 LLVMValueRef store_val
;
3048 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3050 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3051 interp_ij
, c_ll
, "");
3052 LLVMBuildStore(gallivm
->builder
,
3056 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3057 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3059 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3060 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, base
->elem_type
, "");
3062 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3064 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3065 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3067 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3068 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, base
->elem_type
, "");
3070 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3073 return lp_build_gather_values(gallivm
, result
, 4);
3076 static void interp_fetch_args(
3077 struct lp_build_tgsi_context
*bld_base
,
3078 struct lp_build_emit_data
*emit_data
)
3080 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3081 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3082 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3084 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3085 /* offset is in second src, first two channels */
3086 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3089 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3092 emit_data
->arg_count
= 2;
3093 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3094 LLVMValueRef sample_position
;
3095 LLVMValueRef sample_id
;
3096 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3098 /* fetch sample ID, then fetch its sample position,
3099 * and place into first two channels.
3101 sample_id
= lp_build_emit_fetch(bld_base
,
3102 emit_data
->inst
, 1, 0);
3103 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3104 LLVMInt32TypeInContext(gallivm
->context
),
3106 sample_position
= load_sample_position(&si_shader_ctx
->radeon_bld
, sample_id
);
3108 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3110 lp_build_const_int32(gallivm
, 0), "");
3112 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3113 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3115 lp_build_const_int32(gallivm
, 1), "");
3116 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3117 emit_data
->arg_count
= 2;
3121 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3122 struct lp_build_tgsi_context
*bld_base
,
3123 struct lp_build_emit_data
*emit_data
)
3125 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3126 struct si_shader
*shader
= si_shader_ctx
->shader
;
3127 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3128 LLVMValueRef interp_param
;
3129 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3130 const char *intr_name
;
3134 LLVMValueRef attr_number
;
3135 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
3136 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3137 int interp_param_idx
;
3140 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3141 input_index
= inst
->Src
[0].Register
.Index
;
3143 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3144 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3145 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3147 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3149 interp_param_idx
= lookup_interp_param_index(shader
->ps_input_interpolate
[input_index
],
3151 if (interp_param_idx
== -1)
3153 else if (interp_param_idx
)
3154 interp_param
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3156 interp_param
= NULL
;
3158 attr_number
= lp_build_const_int32(gallivm
,
3159 shader
->ps_input_param_offset
[input_index
]);
3161 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3162 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3163 LLVMValueRef ij_out
[2];
3164 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3167 * take the I then J parameters, and the DDX/Y for it, and
3168 * calculate the IJ inputs for the interpolator.
3169 * temp1 = ddx * offset/sample.x + I;
3170 * interp_param.I = ddy * offset/sample.y + temp1;
3171 * temp1 = ddx * offset/sample.x + J;
3172 * interp_param.J = ddy * offset/sample.y + temp1;
3174 for (i
= 0; i
< 2; i
++) {
3175 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3176 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
3177 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3178 ddxy_out
, ix_ll
, "");
3179 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3180 ddxy_out
, iy_ll
, "");
3181 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3182 interp_param
, ix_ll
, "");
3183 LLVMValueRef temp1
, temp2
;
3185 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3186 LLVMFloatTypeInContext(gallivm
->context
), "");
3188 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3190 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3192 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3194 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3196 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
3198 LLVMIntTypeInContext(gallivm
->context
, 32), "");
3200 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
3203 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3204 for (chan
= 0; chan
< 2; chan
++) {
3205 LLVMValueRef args
[4];
3206 LLVMValueRef llvm_chan
;
3209 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3210 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
3212 args
[0] = llvm_chan
;
3213 args
[1] = attr_number
;
3215 args
[3] = interp_param
;
3217 emit_data
->output
[chan
] =
3218 lp_build_intrinsic(gallivm
->builder
, intr_name
,
3219 input_type
, args
, args
[3] ? 4 : 3,
3220 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3224 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3225 struct lp_build_emit_data
*emit_data
)
3227 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3228 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3231 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3233 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
3237 /* Emit one vertex from the geometry shader */
3238 static void si_llvm_emit_vertex(
3239 const struct lp_build_tgsi_action
*action
,
3240 struct lp_build_tgsi_context
*bld_base
,
3241 struct lp_build_emit_data
*emit_data
)
3243 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3244 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3245 struct si_shader
*shader
= si_shader_ctx
->shader
;
3246 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3247 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3248 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
3249 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3250 SI_PARAM_GS2VS_OFFSET
);
3251 LLVMValueRef gs_next_vertex
;
3252 LLVMValueRef can_emit
, kill
;
3253 LLVMValueRef args
[2];
3258 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3260 /* Write vertex attribute values to GSVS ring */
3261 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3262 si_shader_ctx
->gs_next_vertex
[stream
],
3265 /* If this thread has already emitted the declared maximum number of
3266 * vertices, kill it: excessive vertex emissions are not supposed to
3267 * have any effect, and GS threads have no externally observable
3268 * effects other than emitting vertices.
3270 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
3271 lp_build_const_int32(gallivm
,
3272 shader
->selector
->gs_max_out_vertices
), "");
3273 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3274 lp_build_const_float(gallivm
, 1.0f
),
3275 lp_build_const_float(gallivm
, -1.0f
));
3277 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
3278 LLVMVoidTypeInContext(gallivm
->context
), &kill
, 1, 0);
3280 for (i
= 0; i
< info
->num_outputs
; i
++) {
3281 LLVMValueRef
*out_ptr
=
3282 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
3284 for (chan
= 0; chan
< 4; chan
++) {
3285 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3286 LLVMValueRef voffset
=
3287 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
3288 shader
->selector
->gs_max_out_vertices
);
3290 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3291 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3293 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
3295 build_tbuffer_store(si_shader_ctx
,
3296 si_shader_ctx
->gsvs_ring
[stream
],
3298 voffset
, soffset
, 0,
3299 V_008F0C_BUF_DATA_FORMAT_32
,
3300 V_008F0C_BUF_NUM_FORMAT_UINT
,
3304 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3305 lp_build_const_int32(gallivm
, 1));
3307 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
[stream
]);
3309 /* Signal vertex emission */
3310 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
3311 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3312 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3313 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3314 LLVMNoUnwindAttribute
);
3317 /* Cut one primitive from the geometry shader */
3318 static void si_llvm_emit_primitive(
3319 const struct lp_build_tgsi_action
*action
,
3320 struct lp_build_tgsi_context
*bld_base
,
3321 struct lp_build_emit_data
*emit_data
)
3323 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3324 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3325 LLVMValueRef args
[2];
3328 /* Signal primitive cut */
3329 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3330 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
3331 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3332 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3333 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3334 LLVMNoUnwindAttribute
);
3337 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
3338 struct lp_build_tgsi_context
*bld_base
,
3339 struct lp_build_emit_data
*emit_data
)
3341 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3343 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.barrier.local",
3344 LLVMVoidTypeInContext(gallivm
->context
), NULL
, 0,
3345 LLVMNoUnwindAttribute
);
3348 static const struct lp_build_tgsi_action tex_action
= {
3349 .fetch_args
= tex_fetch_args
,
3350 .emit
= build_tex_intrinsic
,
3353 static const struct lp_build_tgsi_action interp_action
= {
3354 .fetch_args
= interp_fetch_args
,
3355 .emit
= build_interp_intrinsic
,
3358 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
3360 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3361 LLVMValueRef args
[3];
3363 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
3365 args
[2] = lp_build_const_int32(gallivm
, 1);
3367 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
3370 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3372 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3376 static void declare_streamout_params(struct si_shader_context
*si_shader_ctx
,
3377 struct pipe_stream_output_info
*so
,
3378 LLVMTypeRef
*params
, LLVMTypeRef i32
,
3379 unsigned *num_params
)
3383 /* Streamout SGPRs. */
3384 if (so
->num_outputs
) {
3385 params
[si_shader_ctx
->param_streamout_config
= (*num_params
)++] = i32
;
3386 params
[si_shader_ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
3388 /* A streamout buffer offset is loaded if the stride is non-zero. */
3389 for (i
= 0; i
< 4; i
++) {
3393 params
[si_shader_ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
3397 static void create_function(struct si_shader_context
*si_shader_ctx
)
3399 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3400 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3401 struct si_shader
*shader
= si_shader_ctx
->shader
;
3402 LLVMTypeRef params
[SI_NUM_PARAMS
], f32
, i8
, i32
, v2i32
, v3i32
, v16i8
, v4i32
, v8i32
;
3403 unsigned i
, last_array_pointer
, last_sgpr
, num_params
;
3405 i8
= LLVMInt8TypeInContext(gallivm
->context
);
3406 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3407 f32
= LLVMFloatTypeInContext(gallivm
->context
);
3408 v2i32
= LLVMVectorType(i32
, 2);
3409 v3i32
= LLVMVectorType(i32
, 3);
3410 v4i32
= LLVMVectorType(i32
, 4);
3411 v8i32
= LLVMVectorType(i32
, 8);
3412 v16i8
= LLVMVectorType(i8
, 16);
3414 params
[SI_PARAM_RW_BUFFERS
] = const_array(v16i8
, SI_NUM_RW_BUFFERS
);
3415 params
[SI_PARAM_CONST
] = const_array(v16i8
, SI_NUM_CONST_BUFFERS
);
3416 params
[SI_PARAM_SAMPLER
] = const_array(v4i32
, SI_NUM_SAMPLER_STATES
);
3417 params
[SI_PARAM_RESOURCE
] = const_array(v8i32
, SI_NUM_SAMPLER_VIEWS
);
3418 last_array_pointer
= SI_PARAM_RESOURCE
;
3420 switch (si_shader_ctx
->type
) {
3421 case TGSI_PROCESSOR_VERTEX
:
3422 params
[SI_PARAM_VERTEX_BUFFER
] = const_array(v16i8
, SI_NUM_VERTEX_BUFFERS
);
3423 last_array_pointer
= SI_PARAM_VERTEX_BUFFER
;
3424 params
[SI_PARAM_BASE_VERTEX
] = i32
;
3425 params
[SI_PARAM_START_INSTANCE
] = i32
;
3426 num_params
= SI_PARAM_START_INSTANCE
+1;
3428 if (shader
->key
.vs
.as_es
) {
3429 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3430 } else if (shader
->key
.vs
.as_ls
) {
3431 params
[SI_PARAM_LS_OUT_LAYOUT
] = i32
;
3432 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
3434 if (shader
->is_gs_copy_shader
) {
3435 last_array_pointer
= SI_PARAM_CONST
;
3436 num_params
= SI_PARAM_CONST
+1;
3438 params
[SI_PARAM_VS_STATE_BITS
] = i32
;
3439 num_params
= SI_PARAM_VS_STATE_BITS
+1;
3442 /* The locations of the other parameters are assigned dynamically. */
3443 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3444 params
, i32
, &num_params
);
3447 last_sgpr
= num_params
-1;
3450 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
3451 params
[si_shader_ctx
->param_rel_auto_id
= num_params
++] = i32
;
3452 params
[si_shader_ctx
->param_vs_prim_id
= num_params
++] = i32
;
3453 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
3456 case TGSI_PROCESSOR_TESS_CTRL
:
3457 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3458 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3459 params
[SI_PARAM_TCS_IN_LAYOUT
] = i32
;
3460 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = i32
;
3461 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
3464 params
[SI_PARAM_PATCH_ID
] = i32
;
3465 params
[SI_PARAM_REL_IDS
] = i32
;
3466 num_params
= SI_PARAM_REL_IDS
+1;
3469 case TGSI_PROCESSOR_TESS_EVAL
:
3470 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3471 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3472 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
3474 if (shader
->key
.tes
.as_es
) {
3475 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3477 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3478 params
, i32
, &num_params
);
3480 last_sgpr
= num_params
- 1;
3483 params
[si_shader_ctx
->param_tes_u
= num_params
++] = f32
;
3484 params
[si_shader_ctx
->param_tes_v
= num_params
++] = f32
;
3485 params
[si_shader_ctx
->param_tes_rel_patch_id
= num_params
++] = i32
;
3486 params
[si_shader_ctx
->param_tes_patch_id
= num_params
++] = i32
;
3489 case TGSI_PROCESSOR_GEOMETRY
:
3490 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
3491 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
3492 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
3495 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
3496 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
3497 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
3498 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
3499 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
3500 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
3501 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
3502 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
3503 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
3506 case TGSI_PROCESSOR_FRAGMENT
:
3507 params
[SI_PARAM_ALPHA_REF
] = f32
;
3508 params
[SI_PARAM_PS_STATE_BITS
] = i32
;
3509 params
[SI_PARAM_PRIM_MASK
] = i32
;
3510 last_sgpr
= SI_PARAM_PRIM_MASK
;
3511 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
3512 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
3513 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
3514 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
3515 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
3516 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
3517 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
3518 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
3519 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
3520 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
3521 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
3522 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
3523 params
[SI_PARAM_FRONT_FACE
] = f32
;
3524 params
[SI_PARAM_ANCILLARY
] = i32
;
3525 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
3526 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
3527 num_params
= SI_PARAM_POS_FIXED_PT
+1;
3531 assert(0 && "unimplemented shader");
3535 assert(num_params
<= Elements(params
));
3536 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
3537 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
3539 if (shader
->dx10_clamp_mode
)
3540 LLVMAddTargetDependentFunctionAttr(si_shader_ctx
->radeon_bld
.main_fn
,
3541 "enable-no-nans-fp-math", "true");
3543 for (i
= 0; i
<= last_sgpr
; ++i
) {
3544 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
3546 /* We tell llvm that array inputs are passed by value to allow Sinking pass
3547 * to move load. Inputs are constant so this is fine. */
3548 if (i
<= last_array_pointer
)
3549 LLVMAddAttribute(P
, LLVMByValAttribute
);
3551 LLVMAddAttribute(P
, LLVMInRegAttribute
);
3554 if (bld_base
->info
&&
3555 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
3556 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
3557 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
3558 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
3559 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
3560 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
3561 si_shader_ctx
->lds
=
3562 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3563 LLVMArrayType(i32
, 64),
3567 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
3568 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
3569 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
) {
3570 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
3571 unsigned vertex_data_dw_size
= 32*32*4;
3572 unsigned patch_data_dw_size
= 32*4;
3573 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
3574 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
3575 unsigned lds_dwords
= patch_dw_size
;
3577 /* The actual size is computed outside of the shader to reduce
3578 * the number of shader variants. */
3579 si_shader_ctx
->lds
=
3580 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3581 LLVMArrayType(i32
, lds_dwords
),
3587 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
3589 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3590 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3591 const struct tgsi_shader_info
* info
= bld_base
->info
;
3593 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
3595 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
3596 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
3601 /* Allocate space for the constant values */
3602 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
3604 /* Load the resource descriptor */
3605 si_shader_ctx
->const_resource
[buf
] =
3606 build_indexed_load_const(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
3608 /* Load the constants, we rely on the code sinking to do the rest */
3609 for (i
= 0; i
< num_const
* 4; ++i
) {
3610 si_shader_ctx
->constants
[buf
][i
] =
3611 buffer_load_const(gallivm
->builder
,
3612 si_shader_ctx
->const_resource
[buf
],
3613 lp_build_const_int32(gallivm
, i
* 4),
3614 bld_base
->base
.elem_type
);
3619 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
3621 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3622 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3623 const struct tgsi_shader_info
* info
= bld_base
->info
;
3625 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
3627 LLVMValueRef res_ptr
, samp_ptr
;
3628 LLVMValueRef offset
;
3630 if (num_samplers
== 0)
3633 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
3634 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
3636 /* Load the resources and samplers, we rely on the code sinking to do the rest */
3637 for (i
= 0; i
< num_samplers
; ++i
) {
3639 offset
= lp_build_const_int32(gallivm
, i
);
3640 si_shader_ctx
->resources
[i
] = build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3643 offset
= lp_build_const_int32(gallivm
, i
);
3644 si_shader_ctx
->samplers
[i
] = build_indexed_load_const(si_shader_ctx
, samp_ptr
, offset
);
3646 /* FMASK resource */
3647 if (info
->is_msaa_sampler
[i
]) {
3648 offset
= lp_build_const_int32(gallivm
, SI_FMASK_TEX_OFFSET
+ i
);
3649 si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ i
] =
3650 build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3655 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
3657 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3658 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3661 /* Streamout can only be used if the shader is compiled as VS. */
3662 if (!si_shader_ctx
->shader
->selector
->so
.num_outputs
||
3663 (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3664 (si_shader_ctx
->shader
->key
.vs
.as_es
||
3665 si_shader_ctx
->shader
->key
.vs
.as_ls
)) ||
3666 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3667 si_shader_ctx
->shader
->key
.tes
.as_es
))
3670 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3671 SI_PARAM_RW_BUFFERS
);
3673 /* Load the resources, we rely on the code sinking to do the rest */
3674 for (i
= 0; i
< 4; ++i
) {
3675 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
3676 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
3677 SI_SO_BUF_OFFSET
+ i
);
3679 si_shader_ctx
->so_buffers
[i
] = build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3685 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
3688 static void preload_ring_buffers(struct si_shader_context
*si_shader_ctx
)
3690 struct gallivm_state
*gallivm
=
3691 si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3693 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3694 SI_PARAM_RW_BUFFERS
);
3696 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3697 si_shader_ctx
->shader
->key
.vs
.as_es
) ||
3698 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3699 si_shader_ctx
->shader
->key
.tes
.as_es
) ||
3700 si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3701 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
3703 si_shader_ctx
->esgs_ring
=
3704 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3707 if (si_shader_ctx
->shader
->is_gs_copy_shader
) {
3708 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
3710 si_shader_ctx
->gsvs_ring
[0] =
3711 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3713 if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3715 for (i
= 0; i
< 4; i
++) {
3716 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
3718 si_shader_ctx
->gsvs_ring
[i
] =
3719 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3724 void si_shader_binary_read_config(const struct si_screen
*sscreen
,
3725 struct si_shader
*shader
,
3726 unsigned symbol_offset
)
3729 const unsigned char *config
=
3730 radeon_shader_binary_config_start(&shader
->binary
,
3733 /* XXX: We may be able to emit some of these values directly rather than
3734 * extracting fields to be emitted later.
3737 for (i
= 0; i
< shader
->binary
.config_size_per_symbol
; i
+= 8) {
3738 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
3739 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
3741 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
3742 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
3743 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
3744 case R_00B848_COMPUTE_PGM_RSRC1
:
3745 shader
->num_sgprs
= MAX2(shader
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
3746 shader
->num_vgprs
= MAX2(shader
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
3747 shader
->float_mode
= G_00B028_FLOAT_MODE(value
);
3749 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
3750 shader
->lds_size
= MAX2(shader
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
3752 case R_00B84C_COMPUTE_PGM_RSRC2
:
3753 shader
->lds_size
= MAX2(shader
->lds_size
, G_00B84C_LDS_SIZE(value
));
3755 case R_0286CC_SPI_PS_INPUT_ENA
:
3756 shader
->spi_ps_input_ena
= value
;
3758 case R_0286E8_SPI_TMPRING_SIZE
:
3759 case R_00B860_COMPUTE_TMPRING_SIZE
:
3760 /* WAVESIZE is in units of 256 dwords. */
3761 shader
->scratch_bytes_per_wave
=
3762 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
3765 fprintf(stderr
, "Warning: Compiler emitted unknown "
3766 "config register: 0x%x\n", reg
);
3772 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
3773 struct si_shader
*shader
,
3774 uint64_t scratch_va
)
3777 uint32_t scratch_rsrc_dword0
= scratch_va
;
3778 uint32_t scratch_rsrc_dword1
=
3779 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
3780 | S_008F04_STRIDE(shader
->scratch_bytes_per_wave
/ 64);
3782 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
3783 const struct radeon_shader_reloc
*reloc
=
3784 &shader
->binary
.relocs
[i
];
3785 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
3786 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3787 &scratch_rsrc_dword0
, 4);
3788 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
3789 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3790 &scratch_rsrc_dword1
, 4);
3795 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
3797 const struct radeon_shader_binary
*binary
= &shader
->binary
;
3798 unsigned code_size
= binary
->code_size
+ binary
->rodata_size
;
3801 r600_resource_reference(&shader
->bo
, NULL
);
3802 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
3803 PIPE_USAGE_IMMUTABLE
,
3808 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, NULL
,
3809 PIPE_TRANSFER_READ_WRITE
);
3810 util_memcpy_cpu_to_le32(ptr
, binary
->code
, binary
->code_size
);
3811 if (binary
->rodata_size
> 0) {
3812 ptr
+= binary
->code_size
;
3813 util_memcpy_cpu_to_le32(ptr
, binary
->rodata
,
3814 binary
->rodata_size
);
3817 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
3821 int si_shader_binary_read(struct si_screen
*sscreen
, struct si_shader
*shader
)
3823 const struct radeon_shader_binary
*binary
= &shader
->binary
;
3826 bool dump
= r600_can_dump_shader(&sscreen
->b
,
3827 shader
->selector
? shader
->selector
->tokens
: NULL
);
3829 si_shader_binary_read_config(sscreen
, shader
, 0);
3830 r
= si_shader_binary_upload(sscreen
, shader
);
3835 if (!(sscreen
->b
.debug_flags
& DBG_NO_ASM
)) {
3836 if (binary
->disasm_string
) {
3837 fprintf(stderr
, "\nShader Disassembly:\n\n");
3838 fprintf(stderr
, "%s\n", binary
->disasm_string
);
3840 fprintf(stderr
, "SI CODE:\n");
3841 for (i
= 0; i
< binary
->code_size
; i
+=4 ) {
3842 fprintf(stderr
, "@0x%x: %02x%02x%02x%02x\n", i
, binary
->code
[i
+ 3],
3843 binary
->code
[i
+ 2], binary
->code
[i
+ 1],
3849 fprintf(stderr
, "*** SHADER STATS ***\n"
3850 "SGPRS: %d\nVGPRS: %d\nCode Size: %d bytes\nLDS: %d blocks\n"
3851 "Scratch: %d bytes per wave\n********************\n",
3852 shader
->num_sgprs
, shader
->num_vgprs
, binary
->code_size
,
3853 shader
->lds_size
, shader
->scratch_bytes_per_wave
);
3858 int si_compile_llvm(struct si_screen
*sscreen
, struct si_shader
*shader
,
3859 LLVMTargetMachineRef tm
, LLVMModuleRef mod
)
3862 bool dump_asm
= r600_can_dump_shader(&sscreen
->b
,
3863 shader
->selector
? shader
->selector
->tokens
: NULL
);
3864 bool dump_ir
= dump_asm
&& !(sscreen
->b
.debug_flags
& DBG_NO_IR
);
3866 r
= radeon_llvm_compile(mod
, &shader
->binary
,
3867 r600_get_llvm_processor_name(sscreen
->b
.family
), dump_ir
, dump_asm
, tm
);
3871 r
= si_shader_binary_read(sscreen
, shader
);
3873 FREE(shader
->binary
.config
);
3874 FREE(shader
->binary
.rodata
);
3875 FREE(shader
->binary
.global_symbol_offsets
);
3876 if (shader
->scratch_bytes_per_wave
== 0) {
3877 FREE(shader
->binary
.code
);
3878 FREE(shader
->binary
.relocs
);
3879 memset(&shader
->binary
, 0,
3880 offsetof(struct radeon_shader_binary
, disasm_string
));
3885 /* Generate code for the hardware VS shader stage to go with a geometry shader */
3886 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
3887 struct si_shader_context
*si_shader_ctx
,
3888 struct si_shader
*gs
, bool dump
)
3890 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
3891 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3892 struct lp_build_context
*base
= &bld_base
->base
;
3893 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3894 struct si_shader
*shader
= si_shader_ctx
->shader
;
3895 struct si_shader_output_values
*outputs
;
3896 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
3897 LLVMValueRef args
[9];
3900 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
3902 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
3903 shader
->is_gs_copy_shader
= true;
3905 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
3907 create_meta_data(si_shader_ctx
);
3908 create_function(si_shader_ctx
);
3909 preload_streamout_buffers(si_shader_ctx
);
3910 preload_ring_buffers(si_shader_ctx
);
3912 args
[0] = si_shader_ctx
->gsvs_ring
[0];
3913 args
[1] = lp_build_mul_imm(uint
,
3914 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3915 si_shader_ctx
->param_vertex_id
),
3917 args
[3] = uint
->zero
;
3918 args
[4] = uint
->one
; /* OFFEN */
3919 args
[5] = uint
->zero
; /* IDXEN */
3920 args
[6] = uint
->one
; /* GLC */
3921 args
[7] = uint
->one
; /* SLC */
3922 args
[8] = uint
->zero
; /* TFE */
3924 /* Fetch vertex data from GSVS ring */
3925 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
3928 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
3929 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
3931 for (chan
= 0; chan
< 4; chan
++) {
3932 args
[2] = lp_build_const_int32(gallivm
,
3934 gs
->selector
->gs_max_out_vertices
* 16 * 4);
3936 outputs
[i
].values
[chan
] =
3937 LLVMBuildBitCast(gallivm
->builder
,
3938 lp_build_intrinsic(gallivm
->builder
,
3939 "llvm.SI.buffer.load.dword.i32.i32",
3940 LLVMInt32TypeInContext(gallivm
->context
),
3942 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
3943 base
->elem_type
, "");
3947 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
3949 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
3952 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
3954 r
= si_compile_llvm(sscreen
, si_shader_ctx
->shader
,
3955 si_shader_ctx
->tm
, bld_base
->base
.gallivm
->module
);
3957 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
3963 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
3967 fprintf(f
, "SHADER KEY\n");
3970 case PIPE_SHADER_VERTEX
:
3971 fprintf(f
, " instance_divisors = {");
3972 for (i
= 0; i
< Elements(key
->vs
.instance_divisors
); i
++)
3973 fprintf(f
, !i
? "%u" : ", %u",
3974 key
->vs
.instance_divisors
[i
]);
3976 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
3977 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
3978 fprintf(f
, " export_prim_id = %u\n", key
->vs
.export_prim_id
);
3981 case PIPE_SHADER_TESS_CTRL
:
3982 fprintf(f
, " prim_mode = %u\n", key
->tcs
.prim_mode
);
3985 case PIPE_SHADER_TESS_EVAL
:
3986 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
3987 fprintf(f
, " export_prim_id = %u\n", key
->tes
.export_prim_id
);
3990 case PIPE_SHADER_GEOMETRY
:
3993 case PIPE_SHADER_FRAGMENT
:
3994 fprintf(f
, " export_16bpc = 0x%X\n", key
->ps
.export_16bpc
);
3995 fprintf(f
, " last_cbuf = %u\n", key
->ps
.last_cbuf
);
3996 fprintf(f
, " color_two_side = %u\n", key
->ps
.color_two_side
);
3997 fprintf(f
, " alpha_func = %u\n", key
->ps
.alpha_func
);
3998 fprintf(f
, " alpha_to_one = %u\n", key
->ps
.alpha_to_one
);
3999 fprintf(f
, " poly_stipple = %u\n", key
->ps
.poly_stipple
);
4000 fprintf(f
, " clamp_color = %u\n", key
->ps
.clamp_color
);
4008 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
4009 struct si_shader
*shader
)
4011 struct si_shader_selector
*sel
= shader
->selector
;
4012 struct tgsi_token
*tokens
= sel
->tokens
;
4013 struct si_shader_context si_shader_ctx
;
4014 struct lp_build_tgsi_context
* bld_base
;
4015 struct tgsi_shader_info stipple_shader_info
;
4018 bool poly_stipple
= sel
->type
== PIPE_SHADER_FRAGMENT
&&
4019 shader
->key
.ps
.poly_stipple
;
4020 bool dump
= r600_can_dump_shader(&sscreen
->b
, sel
->tokens
);
4023 tokens
= util_pstipple_create_fragment_shader(tokens
, NULL
,
4024 SI_POLY_STIPPLE_SAMPLER
);
4025 tgsi_scan_shader(tokens
, &stipple_shader_info
);
4028 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
4029 * conversion fails. */
4030 if (dump
&& !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
4031 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
4032 tgsi_dump(tokens
, 0);
4033 si_dump_streamout(&sel
->so
);
4036 assert(shader
->nparam
== 0);
4038 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
4039 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
4040 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
4042 if (sel
->type
!= PIPE_SHADER_COMPUTE
)
4043 shader
->dx10_clamp_mode
= true;
4045 if (sel
->info
.uses_kill
)
4046 shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
4048 shader
->uses_instanceid
= sel
->info
.uses_instanceid
;
4049 bld_base
->info
= poly_stipple
? &stipple_shader_info
: &sel
->info
;
4050 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
4052 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
4053 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
4054 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
4056 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
4057 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
4058 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
4059 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
4060 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
4061 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
4062 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
4063 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
4064 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
4065 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
4066 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
4067 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
4068 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
4070 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
4071 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
4072 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
4073 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
4075 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
4076 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
4077 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
4079 if (HAVE_LLVM
>= 0x0306) {
4080 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
4081 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
4082 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
4083 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
4086 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
4087 si_shader_ctx
.shader
= shader
;
4088 si_shader_ctx
.type
= tgsi_get_processor_type(tokens
);
4089 si_shader_ctx
.screen
= sscreen
;
4090 si_shader_ctx
.tm
= tm
;
4092 switch (si_shader_ctx
.type
) {
4093 case TGSI_PROCESSOR_VERTEX
:
4094 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
4095 if (shader
->key
.vs
.as_ls
)
4096 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
4097 else if (shader
->key
.vs
.as_es
)
4098 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4100 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4102 case TGSI_PROCESSOR_TESS_CTRL
:
4103 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
4104 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
4105 bld_base
->emit_store
= store_output_tcs
;
4106 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
4108 case TGSI_PROCESSOR_TESS_EVAL
:
4109 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
4110 if (shader
->key
.tes
.as_es
)
4111 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4113 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4115 case TGSI_PROCESSOR_GEOMETRY
:
4116 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
4117 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
4119 case TGSI_PROCESSOR_FRAGMENT
:
4120 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
4121 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
4123 switch (sel
->info
.properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
]) {
4124 case TGSI_FS_DEPTH_LAYOUT_GREATER
:
4125 shader
->db_shader_control
|=
4126 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z
);
4128 case TGSI_FS_DEPTH_LAYOUT_LESS
:
4129 shader
->db_shader_control
|=
4130 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z
);
4135 assert(!"Unsupported shader type");
4139 create_meta_data(&si_shader_ctx
);
4140 create_function(&si_shader_ctx
);
4141 preload_constants(&si_shader_ctx
);
4142 preload_samplers(&si_shader_ctx
);
4143 preload_streamout_buffers(&si_shader_ctx
);
4144 preload_ring_buffers(&si_shader_ctx
);
4146 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4148 for (i
= 0; i
< 4; i
++) {
4149 si_shader_ctx
.gs_next_vertex
[i
] =
4150 lp_build_alloca(bld_base
->base
.gallivm
,
4151 bld_base
->uint_bld
.elem_type
, "");
4155 if (!lp_build_tgsi_llvm(bld_base
, tokens
)) {
4156 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
4160 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
4162 mod
= bld_base
->base
.gallivm
->module
;
4163 r
= si_compile_llvm(sscreen
, shader
, tm
, mod
);
4165 fprintf(stderr
, "LLVM failed to compile shader\n");
4169 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
4171 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4172 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
4173 shader
->gs_copy_shader
->selector
= shader
->selector
;
4174 shader
->gs_copy_shader
->key
= shader
->key
;
4175 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
4176 if ((r
= si_generate_gs_copy_shader(sscreen
, &si_shader_ctx
,
4178 free(shader
->gs_copy_shader
);
4179 shader
->gs_copy_shader
= NULL
;
4185 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
4186 FREE(si_shader_ctx
.constants
[i
]);
4188 tgsi_free_tokens(tokens
);
4192 void si_shader_destroy(struct si_shader
*shader
)
4194 if (shader
->gs_copy_shader
) {
4195 si_shader_destroy(shader
->gs_copy_shader
);
4196 FREE(shader
->gs_copy_shader
);
4199 if (shader
->scratch_bo
)
4200 r600_resource_reference(&shader
->scratch_bo
, NULL
);
4202 r600_resource_reference(&shader
->bo
, NULL
);
4204 FREE(shader
->binary
.code
);
4205 FREE(shader
->binary
.relocs
);
4206 FREE(shader
->binary
.disasm_string
);