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 bool is_gs_copy_shader
;
72 int param_streamout_config
;
73 int param_streamout_write_index
;
74 int param_streamout_offset
[4];
76 int param_rel_auto_id
;
78 int param_instance_id
;
81 int param_tes_rel_patch_id
;
82 int param_tes_patch_id
;
83 int param_es2gs_offset
;
84 LLVMTargetMachineRef tm
;
85 LLVMValueRef const_md
;
86 LLVMValueRef const_buffers
[SI_NUM_CONST_BUFFERS
];
88 LLVMValueRef
*constants
[SI_NUM_CONST_BUFFERS
];
89 LLVMValueRef sampler_views
[SI_NUM_SAMPLER_VIEWS
];
90 LLVMValueRef sampler_states
[SI_NUM_SAMPLER_STATES
];
91 LLVMValueRef so_buffers
[4];
92 LLVMValueRef esgs_ring
;
93 LLVMValueRef gsvs_ring
[4];
94 LLVMValueRef gs_next_vertex
[4];
97 static struct si_shader_context
* si_shader_context(
98 struct lp_build_tgsi_context
* bld_base
)
100 return (struct si_shader_context
*)bld_base
;
104 #define PERSPECTIVE_BASE 0
105 #define LINEAR_BASE 9
107 #define SAMPLE_OFFSET 0
108 #define CENTER_OFFSET 2
109 #define CENTROID_OFSET 4
111 #define USE_SGPR_MAX_SUFFIX_LEN 5
112 #define CONST_ADDR_SPACE 2
113 #define LOCAL_ADDR_SPACE 3
114 #define USER_SGPR_ADDR_SPACE 8
118 #define SENDMSG_GS_DONE 3
120 #define SENDMSG_GS_OP_NOP (0 << 4)
121 #define SENDMSG_GS_OP_CUT (1 << 4)
122 #define SENDMSG_GS_OP_EMIT (2 << 4)
123 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
126 * Returns a unique index for a semantic name and index. The index must be
127 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
130 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
132 switch (semantic_name
) {
133 case TGSI_SEMANTIC_POSITION
:
135 case TGSI_SEMANTIC_PSIZE
:
137 case TGSI_SEMANTIC_CLIPDIST
:
140 case TGSI_SEMANTIC_GENERIC
:
144 /* same explanation as in the default statement,
145 * the only user hitting this is st/nine.
149 /* patch indices are completely separate and thus start from 0 */
150 case TGSI_SEMANTIC_TESSOUTER
:
152 case TGSI_SEMANTIC_TESSINNER
:
154 case TGSI_SEMANTIC_PATCH
:
158 /* Don't fail here. The result of this function is only used
159 * for LS, TCS, TES, and GS, where legacy GL semantics can't
160 * occur, but this function is called for all vertex shaders
161 * before it's known whether LS will be compiled or not.
168 * Get the value of a shader input parameter and extract a bitfield.
170 static LLVMValueRef
unpack_param(struct si_shader_context
*si_shader_ctx
,
171 unsigned param
, unsigned rshift
,
174 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
175 LLVMValueRef value
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
179 value
= LLVMBuildLShr(gallivm
->builder
, value
,
180 lp_build_const_int32(gallivm
, rshift
), "");
182 if (rshift
+ bitwidth
< 32) {
183 unsigned mask
= (1 << bitwidth
) - 1;
184 value
= LLVMBuildAnd(gallivm
->builder
, value
,
185 lp_build_const_int32(gallivm
, mask
), "");
191 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*si_shader_ctx
)
193 switch (si_shader_ctx
->type
) {
194 case TGSI_PROCESSOR_TESS_CTRL
:
195 return unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 0, 8);
197 case TGSI_PROCESSOR_TESS_EVAL
:
198 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
199 si_shader_ctx
->param_tes_rel_patch_id
);
207 /* Tessellation shaders pass outputs to the next shader using LDS.
209 * LS outputs = TCS inputs
210 * TCS outputs = TES inputs
213 * - TCS inputs for patch 0
214 * - TCS inputs for patch 1
215 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
217 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
218 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
219 * - TCS outputs for patch 1
220 * - Per-patch TCS outputs for patch 1
221 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
222 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
225 * All three shaders VS(LS), TCS, TES share the same LDS space.
229 get_tcs_in_patch_stride(struct si_shader_context
*si_shader_ctx
)
231 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
)
232 return unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
233 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
234 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
242 get_tcs_out_patch_stride(struct si_shader_context
*si_shader_ctx
)
244 return unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
248 get_tcs_out_patch0_offset(struct si_shader_context
*si_shader_ctx
)
250 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
251 unpack_param(si_shader_ctx
,
252 SI_PARAM_TCS_OUT_OFFSETS
,
258 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
260 return lp_build_mul_imm(&si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
,
261 unpack_param(si_shader_ctx
,
262 SI_PARAM_TCS_OUT_OFFSETS
,
268 get_tcs_in_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
270 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
271 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(si_shader_ctx
);
272 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
274 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
278 get_tcs_out_current_patch_offset(struct si_shader_context
*si_shader_ctx
)
280 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
281 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(si_shader_ctx
);
282 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
283 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
285 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
286 LLVMBuildMul(gallivm
->builder
, patch_stride
,
292 get_tcs_out_current_patch_data_offset(struct si_shader_context
*si_shader_ctx
)
294 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
295 LLVMValueRef patch0_patch_data_offset
=
296 get_tcs_out_patch0_patch_data_offset(si_shader_ctx
);
297 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(si_shader_ctx
);
298 LLVMValueRef rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
300 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
301 LLVMBuildMul(gallivm
->builder
, patch_stride
,
306 static void build_indexed_store(struct si_shader_context
*si_shader_ctx
,
307 LLVMValueRef base_ptr
, LLVMValueRef index
,
310 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
311 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
312 LLVMValueRef indices
[2], pointer
;
314 indices
[0] = bld_base
->uint_bld
.zero
;
317 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
318 LLVMBuildStore(gallivm
->builder
, value
, pointer
);
322 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
323 * It's equivalent to doing a load from &base_ptr[index].
325 * \param base_ptr Where the array starts.
326 * \param index The element index into the array.
328 static LLVMValueRef
build_indexed_load(struct si_shader_context
*si_shader_ctx
,
329 LLVMValueRef base_ptr
, LLVMValueRef index
)
331 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
332 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
333 LLVMValueRef indices
[2], pointer
;
335 indices
[0] = bld_base
->uint_bld
.zero
;
338 pointer
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, indices
, 2, "");
339 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
343 * Do a load from &base_ptr[index], but also add a flag that it's loading
346 static LLVMValueRef
build_indexed_load_const(
347 struct si_shader_context
* si_shader_ctx
,
348 LLVMValueRef base_ptr
, LLVMValueRef index
)
350 LLVMValueRef result
= build_indexed_load(si_shader_ctx
, base_ptr
, index
);
351 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
355 static LLVMValueRef
get_instance_index_for_fetch(
356 struct radeon_llvm_context
* radeon_bld
,
359 struct si_shader_context
*si_shader_ctx
=
360 si_shader_context(&radeon_bld
->soa
.bld_base
);
361 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
363 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
364 si_shader_ctx
->param_instance_id
);
366 /* The division must be done before START_INSTANCE is added. */
368 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
369 lp_build_const_int32(gallivm
, divisor
), "");
371 return LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
372 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
375 static void declare_input_vs(
376 struct radeon_llvm_context
*radeon_bld
,
377 unsigned input_index
,
378 const struct tgsi_full_declaration
*decl
)
380 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
381 struct gallivm_state
*gallivm
= base
->gallivm
;
382 struct si_shader_context
*si_shader_ctx
=
383 si_shader_context(&radeon_bld
->soa
.bld_base
);
384 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
388 LLVMValueRef t_list_ptr
;
389 LLVMValueRef t_offset
;
391 LLVMValueRef attribute_offset
;
392 LLVMValueRef buffer_index
;
393 LLVMValueRef args
[3];
394 LLVMTypeRef vec4_type
;
397 /* Load the T list */
398 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFERS
);
400 t_offset
= lp_build_const_int32(gallivm
, input_index
);
402 t_list
= build_indexed_load_const(si_shader_ctx
, t_list_ptr
, t_offset
);
404 /* Build the attribute offset */
405 attribute_offset
= lp_build_const_int32(gallivm
, 0);
408 /* Build index from instance ID, start instance and divisor */
409 si_shader_ctx
->shader
->uses_instanceid
= true;
410 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
412 /* Load the buffer index for vertices. */
413 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
414 si_shader_ctx
->param_vertex_id
);
415 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
416 SI_PARAM_BASE_VERTEX
);
417 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
420 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
422 args
[1] = attribute_offset
;
423 args
[2] = buffer_index
;
424 input
= lp_build_intrinsic(gallivm
->builder
,
425 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
426 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
428 /* Break up the vec4 into individual components */
429 for (chan
= 0; chan
< 4; chan
++) {
430 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
431 /* XXX: Use a helper function for this. There is one in
433 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
434 LLVMBuildExtractElement(gallivm
->builder
,
435 input
, llvm_chan
, "");
439 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
442 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
445 return bld_base
->uint_bld
.zero
;
447 switch (si_shader_ctx
->type
) {
448 case TGSI_PROCESSOR_VERTEX
:
449 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
450 si_shader_ctx
->param_vs_prim_id
);
451 case TGSI_PROCESSOR_TESS_CTRL
:
452 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
454 case TGSI_PROCESSOR_TESS_EVAL
:
455 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
456 si_shader_ctx
->param_tes_patch_id
);
457 case TGSI_PROCESSOR_GEOMETRY
:
458 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
459 SI_PARAM_PRIMITIVE_ID
);
462 return bld_base
->uint_bld
.zero
;
467 * Return the value of tgsi_ind_register for indexing.
468 * This is the indirect index with the constant offset added to it.
470 static LLVMValueRef
get_indirect_index(struct si_shader_context
*si_shader_ctx
,
471 const struct tgsi_ind_register
*ind
,
474 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
477 result
= si_shader_ctx
->radeon_bld
.soa
.addr
[ind
->Index
][ind
->Swizzle
];
478 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
479 result
= LLVMBuildAdd(gallivm
->builder
, result
,
480 lp_build_const_int32(gallivm
, rel_index
), "");
485 * Calculate a dword address given an input or output register and a stride.
487 static LLVMValueRef
get_dw_address(struct si_shader_context
*si_shader_ctx
,
488 const struct tgsi_full_dst_register
*dst
,
489 const struct tgsi_full_src_register
*src
,
490 LLVMValueRef vertex_dw_stride
,
491 LLVMValueRef base_addr
)
493 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
494 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
495 ubyte
*name
, *index
, *array_first
;
497 struct tgsi_full_dst_register reg
;
499 /* Set the register description. The address computation is the same
500 * for sources and destinations. */
502 reg
.Register
.File
= src
->Register
.File
;
503 reg
.Register
.Index
= src
->Register
.Index
;
504 reg
.Register
.Indirect
= src
->Register
.Indirect
;
505 reg
.Register
.Dimension
= src
->Register
.Dimension
;
506 reg
.Indirect
= src
->Indirect
;
507 reg
.Dimension
= src
->Dimension
;
508 reg
.DimIndirect
= src
->DimIndirect
;
512 /* If the register is 2-dimensional (e.g. an array of vertices
513 * in a primitive), calculate the base address of the vertex. */
514 if (reg
.Register
.Dimension
) {
517 if (reg
.Dimension
.Indirect
)
518 index
= get_indirect_index(si_shader_ctx
, ®
.DimIndirect
,
519 reg
.Dimension
.Index
);
521 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
523 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
524 LLVMBuildMul(gallivm
->builder
, index
,
525 vertex_dw_stride
, ""), "");
528 /* Get information about the register. */
529 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
530 name
= info
->input_semantic_name
;
531 index
= info
->input_semantic_index
;
532 array_first
= info
->input_array_first
;
533 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
534 name
= info
->output_semantic_name
;
535 index
= info
->output_semantic_index
;
536 array_first
= info
->output_array_first
;
542 if (reg
.Register
.Indirect
) {
543 /* Add the relative address of the element. */
544 LLVMValueRef ind_index
;
546 if (reg
.Indirect
.ArrayID
)
547 first
= array_first
[reg
.Indirect
.ArrayID
];
549 first
= reg
.Register
.Index
;
551 ind_index
= get_indirect_index(si_shader_ctx
, ®
.Indirect
,
552 reg
.Register
.Index
- first
);
554 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
555 LLVMBuildMul(gallivm
->builder
, ind_index
,
556 lp_build_const_int32(gallivm
, 4), ""), "");
558 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
560 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
561 index
[reg
.Register
.Index
]);
564 /* Add the base address of the element. */
565 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
566 lp_build_const_int32(gallivm
, param
* 4), "");
572 * \param type output value type
573 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
574 * \param dw_addr address in dwords
576 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
577 enum tgsi_opcode_type type
, unsigned swizzle
,
578 LLVMValueRef dw_addr
)
580 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
581 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
585 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
587 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
588 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
590 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
594 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
595 lp_build_const_int32(gallivm
, swizzle
));
597 value
= build_indexed_load(si_shader_ctx
, si_shader_ctx
->lds
, dw_addr
);
598 if (type
== TGSI_TYPE_DOUBLE
) {
600 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
601 lp_build_const_int32(gallivm
, swizzle
+ 1));
602 value2
= build_indexed_load(si_shader_ctx
, si_shader_ctx
->lds
, dw_addr
);
603 return radeon_llvm_emit_fetch_double(bld_base
, value
, value2
);
606 return LLVMBuildBitCast(gallivm
->builder
, value
,
607 tgsi2llvmtype(bld_base
, type
), "");
613 * \param swizzle offset (typically 0..3)
614 * \param dw_addr address in dwords
615 * \param value value to store
617 static void lds_store(struct lp_build_tgsi_context
* bld_base
,
618 unsigned swizzle
, LLVMValueRef dw_addr
,
621 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
622 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
624 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
625 lp_build_const_int32(gallivm
, swizzle
));
627 value
= LLVMBuildBitCast(gallivm
->builder
, value
,
628 LLVMInt32TypeInContext(gallivm
->context
), "");
629 build_indexed_store(si_shader_ctx
, si_shader_ctx
->lds
,
633 static LLVMValueRef
fetch_input_tcs(
634 struct lp_build_tgsi_context
*bld_base
,
635 const struct tgsi_full_src_register
*reg
,
636 enum tgsi_opcode_type type
, unsigned swizzle
)
638 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
639 LLVMValueRef dw_addr
, stride
;
641 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
642 dw_addr
= get_tcs_in_current_patch_offset(si_shader_ctx
);
643 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
645 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
648 static LLVMValueRef
fetch_output_tcs(
649 struct lp_build_tgsi_context
*bld_base
,
650 const struct tgsi_full_src_register
*reg
,
651 enum tgsi_opcode_type type
, unsigned swizzle
)
653 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
654 LLVMValueRef dw_addr
, stride
;
656 if (reg
->Register
.Dimension
) {
657 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
658 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
659 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
661 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
662 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
665 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
668 static LLVMValueRef
fetch_input_tes(
669 struct lp_build_tgsi_context
*bld_base
,
670 const struct tgsi_full_src_register
*reg
,
671 enum tgsi_opcode_type type
, unsigned swizzle
)
673 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
674 LLVMValueRef dw_addr
, stride
;
676 if (reg
->Register
.Dimension
) {
677 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
678 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
679 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, stride
, dw_addr
);
681 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
682 dw_addr
= get_dw_address(si_shader_ctx
, NULL
, reg
, NULL
, dw_addr
);
685 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
688 static void store_output_tcs(struct lp_build_tgsi_context
* bld_base
,
689 const struct tgsi_full_instruction
* inst
,
690 const struct tgsi_opcode_info
* info
,
693 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
694 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
696 LLVMValueRef dw_addr
, stride
;
698 /* Only handle per-patch and per-vertex outputs here.
699 * Vectors will be lowered to scalars and this function will be called again.
701 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
702 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
703 radeon_llvm_emit_store(bld_base
, inst
, info
, dst
);
707 if (reg
->Register
.Dimension
) {
708 stride
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
709 dw_addr
= get_tcs_out_current_patch_offset(si_shader_ctx
);
710 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, stride
, dw_addr
);
712 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
713 dw_addr
= get_dw_address(si_shader_ctx
, reg
, NULL
, NULL
, dw_addr
);
716 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
717 LLVMValueRef value
= dst
[chan_index
];
719 if (inst
->Instruction
.Saturate
)
720 value
= radeon_llvm_saturate(bld_base
, value
);
722 lds_store(bld_base
, chan_index
, dw_addr
, value
);
726 static LLVMValueRef
fetch_input_gs(
727 struct lp_build_tgsi_context
*bld_base
,
728 const struct tgsi_full_src_register
*reg
,
729 enum tgsi_opcode_type type
,
732 struct lp_build_context
*base
= &bld_base
->base
;
733 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
734 struct si_shader
*shader
= si_shader_ctx
->shader
;
735 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
736 struct gallivm_state
*gallivm
= base
->gallivm
;
737 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
738 LLVMValueRef vtx_offset
;
739 LLVMValueRef args
[9];
740 unsigned vtx_offset_param
;
741 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
742 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
743 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
747 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
748 return get_primitive_id(bld_base
, swizzle
);
750 if (!reg
->Register
.Dimension
)
754 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
756 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
757 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
759 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
763 /* Get the vertex offset parameter */
764 vtx_offset_param
= reg
->Dimension
.Index
;
765 if (vtx_offset_param
< 2) {
766 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
768 assert(vtx_offset_param
< 6);
769 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
771 vtx_offset
= lp_build_mul_imm(uint
,
772 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
776 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
777 args
[0] = si_shader_ctx
->esgs_ring
;
778 args
[1] = vtx_offset
;
779 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
780 args
[3] = uint
->zero
;
781 args
[4] = uint
->one
; /* OFFEN */
782 args
[5] = uint
->zero
; /* IDXEN */
783 args
[6] = uint
->one
; /* GLC */
784 args
[7] = uint
->zero
; /* SLC */
785 args
[8] = uint
->zero
; /* TFE */
787 value
= lp_build_intrinsic(gallivm
->builder
,
788 "llvm.SI.buffer.load.dword.i32.i32",
790 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
791 if (type
== TGSI_TYPE_DOUBLE
) {
793 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
794 value2
= lp_build_intrinsic(gallivm
->builder
,
795 "llvm.SI.buffer.load.dword.i32.i32",
797 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
798 return radeon_llvm_emit_fetch_double(bld_base
,
801 return LLVMBuildBitCast(gallivm
->builder
,
803 tgsi2llvmtype(bld_base
, type
), "");
806 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
808 switch (interpolate
) {
809 case TGSI_INTERPOLATE_CONSTANT
:
812 case TGSI_INTERPOLATE_LINEAR
:
813 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
814 return SI_PARAM_LINEAR_SAMPLE
;
815 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
816 return SI_PARAM_LINEAR_CENTROID
;
818 return SI_PARAM_LINEAR_CENTER
;
820 case TGSI_INTERPOLATE_COLOR
:
821 case TGSI_INTERPOLATE_PERSPECTIVE
:
822 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
823 return SI_PARAM_PERSP_SAMPLE
;
824 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
825 return SI_PARAM_PERSP_CENTROID
;
827 return SI_PARAM_PERSP_CENTER
;
830 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
835 /* This shouldn't be used by explicit INTERP opcodes. */
836 static unsigned select_interp_param(struct si_shader_context
*si_shader_ctx
,
839 if (!si_shader_ctx
->shader
->key
.ps
.force_persample_interp
)
842 /* If the shader doesn't use center/centroid, just return the parameter.
844 * If the shader only uses one set of (i,j), "si_emit_spi_ps_input" can
845 * switch between center/centroid and sample without shader changes.
848 case SI_PARAM_PERSP_CENTROID
:
849 case SI_PARAM_PERSP_CENTER
:
850 return SI_PARAM_PERSP_SAMPLE
;
852 case SI_PARAM_LINEAR_CENTROID
:
853 case SI_PARAM_LINEAR_CENTER
:
854 return SI_PARAM_LINEAR_SAMPLE
;
862 * Interpolate a fragment shader input.
864 * @param si_shader_ctx context
865 * @param input_index index of the input in hardware
866 * @param semantic_name TGSI_SEMANTIC_*
867 * @param semantic_index semantic index
868 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
869 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
870 * @param interp_param interpolation weights (i,j)
871 * @param prim_mask SI_PARAM_PRIM_MASK
872 * @param face SI_PARAM_FRONT_FACE
873 * @param result the return value (4 components)
875 static void interp_fs_input(struct si_shader_context
*si_shader_ctx
,
876 unsigned input_index
,
877 unsigned semantic_name
,
878 unsigned semantic_index
,
879 unsigned num_interp_inputs
,
880 unsigned colors_read_mask
,
881 LLVMValueRef interp_param
,
882 LLVMValueRef prim_mask
,
884 LLVMValueRef result
[4])
886 struct lp_build_context
*base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
887 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
888 struct gallivm_state
*gallivm
= base
->gallivm
;
889 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
890 const char * intr_name
;
891 LLVMValueRef attr_number
;
895 attr_number
= lp_build_const_int32(gallivm
, input_index
);
897 /* fs.constant returns the param from the middle vertex, so it's not
898 * really useful for flat shading. It's meant to be used for custom
899 * interpolation (but the intrinsic can't fetch from the other two
902 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
903 * to do the right thing. The only reason we use fs.constant is that
904 * fs.interp cannot be used on integers, because they can be equal
907 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
909 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
910 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
911 LLVMValueRef args
[4];
912 LLVMValueRef is_face_positive
;
913 LLVMValueRef back_attr_number
;
915 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
916 * otherwise it's at offset "num_inputs".
918 unsigned back_attr_offset
= num_interp_inputs
;
919 if (semantic_index
== 1 && colors_read_mask
& 0xf)
920 back_attr_offset
+= 1;
922 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
924 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
925 face
, uint
->zero
, "");
928 args
[3] = interp_param
;
929 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
930 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
931 LLVMValueRef front
, back
;
934 args
[1] = attr_number
;
935 front
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
936 input_type
, args
, args
[3] ? 4 : 3,
937 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
939 args
[1] = back_attr_number
;
940 back
= lp_build_intrinsic(gallivm
->builder
, intr_name
,
941 input_type
, args
, args
[3] ? 4 : 3,
942 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
944 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
950 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
951 LLVMValueRef args
[4];
953 args
[0] = uint
->zero
;
954 args
[1] = attr_number
;
956 args
[3] = interp_param
;
957 result
[0] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
958 input_type
, args
, args
[3] ? 4 : 3,
959 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
961 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
962 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
964 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
965 LLVMValueRef args
[4];
966 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
969 args
[1] = attr_number
;
971 args
[3] = interp_param
;
972 result
[chan
] = lp_build_intrinsic(gallivm
->builder
, intr_name
,
973 input_type
, args
, args
[3] ? 4 : 3,
974 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
979 static void declare_input_fs(
980 struct radeon_llvm_context
*radeon_bld
,
981 unsigned input_index
,
982 const struct tgsi_full_declaration
*decl
)
984 struct si_shader_context
*si_shader_ctx
=
985 si_shader_context(&radeon_bld
->soa
.bld_base
);
986 struct si_shader
*shader
= si_shader_ctx
->shader
;
987 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
988 LLVMValueRef interp_param
= NULL
;
989 int interp_param_idx
;
991 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
992 decl
->Interp
.Location
);
993 if (interp_param_idx
== -1)
995 else if (interp_param_idx
) {
996 interp_param_idx
= select_interp_param(si_shader_ctx
,
998 interp_param
= LLVMGetParam(main_fn
, interp_param_idx
);
1001 interp_fs_input(si_shader_ctx
, input_index
, decl
->Semantic
.Name
,
1002 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1003 shader
->selector
->info
.colors_read
, interp_param
,
1004 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1005 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1006 &radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)]);
1009 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
1011 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1012 SI_PARAM_ANCILLARY
, 8, 4);
1016 * Load a dword from a constant buffer.
1018 static LLVMValueRef
buffer_load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
1019 LLVMValueRef offset
, LLVMTypeRef return_type
)
1021 LLVMValueRef args
[2] = {resource
, offset
};
1023 return lp_build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
1024 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1027 static LLVMValueRef
load_sample_position(struct radeon_llvm_context
*radeon_bld
, LLVMValueRef sample_id
)
1029 struct si_shader_context
*si_shader_ctx
=
1030 si_shader_context(&radeon_bld
->soa
.bld_base
);
1031 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1032 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1033 LLVMBuilderRef builder
= gallivm
->builder
;
1034 LLVMValueRef desc
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1035 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1036 LLVMValueRef resource
= build_indexed_load_const(si_shader_ctx
, desc
, buf_index
);
1038 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1039 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1040 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1042 LLVMValueRef pos
[4] = {
1043 buffer_load_const(builder
, resource
, offset0
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1044 buffer_load_const(builder
, resource
, offset1
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
1045 lp_build_const_float(gallivm
, 0),
1046 lp_build_const_float(gallivm
, 0)
1049 return lp_build_gather_values(gallivm
, pos
, 4);
1052 static void declare_system_value(
1053 struct radeon_llvm_context
* radeon_bld
,
1055 const struct tgsi_full_declaration
*decl
)
1057 struct si_shader_context
*si_shader_ctx
=
1058 si_shader_context(&radeon_bld
->soa
.bld_base
);
1059 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1060 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1061 LLVMValueRef value
= 0;
1063 switch (decl
->Semantic
.Name
) {
1064 case TGSI_SEMANTIC_INSTANCEID
:
1065 value
= LLVMGetParam(radeon_bld
->main_fn
,
1066 si_shader_ctx
->param_instance_id
);
1069 case TGSI_SEMANTIC_VERTEXID
:
1070 value
= LLVMBuildAdd(gallivm
->builder
,
1071 LLVMGetParam(radeon_bld
->main_fn
,
1072 si_shader_ctx
->param_vertex_id
),
1073 LLVMGetParam(radeon_bld
->main_fn
,
1074 SI_PARAM_BASE_VERTEX
), "");
1077 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1078 value
= LLVMGetParam(radeon_bld
->main_fn
,
1079 si_shader_ctx
->param_vertex_id
);
1082 case TGSI_SEMANTIC_BASEVERTEX
:
1083 value
= LLVMGetParam(radeon_bld
->main_fn
,
1084 SI_PARAM_BASE_VERTEX
);
1087 case TGSI_SEMANTIC_INVOCATIONID
:
1088 if (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
)
1089 value
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1090 else if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
)
1091 value
= LLVMGetParam(radeon_bld
->main_fn
,
1092 SI_PARAM_GS_INSTANCE_ID
);
1094 assert(!"INVOCATIONID not implemented");
1097 case TGSI_SEMANTIC_POSITION
:
1099 LLVMValueRef pos
[4] = {
1100 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1101 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1102 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1103 lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
, TGSI_OPCODE_RCP
,
1104 LLVMGetParam(radeon_bld
->main_fn
,
1105 SI_PARAM_POS_W_FLOAT
)),
1107 value
= lp_build_gather_values(gallivm
, pos
, 4);
1111 case TGSI_SEMANTIC_FACE
:
1112 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1115 case TGSI_SEMANTIC_SAMPLEID
:
1116 value
= get_sample_id(radeon_bld
);
1119 case TGSI_SEMANTIC_SAMPLEPOS
: {
1120 LLVMValueRef pos
[4] = {
1121 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1122 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1123 lp_build_const_float(gallivm
, 0),
1124 lp_build_const_float(gallivm
, 0)
1126 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1127 TGSI_OPCODE_FRC
, pos
[0]);
1128 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1129 TGSI_OPCODE_FRC
, pos
[1]);
1130 value
= lp_build_gather_values(gallivm
, pos
, 4);
1134 case TGSI_SEMANTIC_SAMPLEMASK
:
1135 /* This can only occur with the OpenGL Core profile, which
1136 * doesn't support smoothing.
1138 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1141 case TGSI_SEMANTIC_TESSCOORD
:
1143 LLVMValueRef coord
[4] = {
1144 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_u
),
1145 LLVMGetParam(radeon_bld
->main_fn
, si_shader_ctx
->param_tes_v
),
1150 /* For triangles, the vector should be (u, v, 1-u-v). */
1151 if (si_shader_ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1152 PIPE_PRIM_TRIANGLES
)
1153 coord
[2] = lp_build_sub(bld
, bld
->one
,
1154 lp_build_add(bld
, coord
[0], coord
[1]));
1156 value
= lp_build_gather_values(gallivm
, coord
, 4);
1160 case TGSI_SEMANTIC_VERTICESIN
:
1161 value
= unpack_param(si_shader_ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1164 case TGSI_SEMANTIC_TESSINNER
:
1165 case TGSI_SEMANTIC_TESSOUTER
:
1167 LLVMValueRef dw_addr
;
1168 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1170 dw_addr
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
1171 dw_addr
= LLVMBuildAdd(gallivm
->builder
, dw_addr
,
1172 lp_build_const_int32(gallivm
, param
* 4), "");
1174 value
= lds_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1179 case TGSI_SEMANTIC_PRIMID
:
1180 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1184 assert(!"unknown system value");
1188 radeon_bld
->system_values
[index
] = value
;
1191 static LLVMValueRef
fetch_constant(
1192 struct lp_build_tgsi_context
* bld_base
,
1193 const struct tgsi_full_src_register
*reg
,
1194 enum tgsi_opcode_type type
,
1197 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1198 struct lp_build_context
* base
= &bld_base
->base
;
1199 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1202 LLVMValueRef addr
, bufp
;
1203 LLVMValueRef result
;
1205 if (swizzle
== LP_CHAN_ALL
) {
1207 LLVMValueRef values
[4];
1208 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1209 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1211 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1214 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1215 idx
= reg
->Register
.Index
* 4 + swizzle
;
1217 if (!reg
->Register
.Indirect
&& !reg
->Dimension
.Indirect
) {
1218 if (type
!= TGSI_TYPE_DOUBLE
)
1219 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
1221 return radeon_llvm_emit_fetch_double(bld_base
,
1222 si_shader_ctx
->constants
[buf
][idx
],
1223 si_shader_ctx
->constants
[buf
][idx
+ 1]);
1227 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1228 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1230 index
= get_indirect_index(si_shader_ctx
, ®
->DimIndirect
,
1231 reg
->Dimension
.Index
);
1232 bufp
= build_indexed_load_const(si_shader_ctx
, ptr
, index
);
1234 bufp
= si_shader_ctx
->const_buffers
[buf
];
1236 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1237 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1238 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1239 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1240 lp_build_const_int32(base
->gallivm
, idx
* 4));
1242 result
= buffer_load_const(base
->gallivm
->builder
, bufp
,
1243 addr
, bld_base
->base
.elem_type
);
1245 if (type
!= TGSI_TYPE_DOUBLE
)
1246 result
= bitcast(bld_base
, type
, result
);
1248 LLVMValueRef addr2
, result2
;
1249 addr2
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
+ 1];
1250 addr2
= LLVMBuildLoad(base
->gallivm
->builder
, addr2
, "load addr reg2");
1251 addr2
= lp_build_mul_imm(&bld_base
->uint_bld
, addr2
, 16);
1252 addr2
= lp_build_add(&bld_base
->uint_bld
, addr2
,
1253 lp_build_const_int32(base
->gallivm
, idx
* 4));
1255 result2
= buffer_load_const(base
->gallivm
->builder
, si_shader_ctx
->const_buffers
[buf
],
1256 addr2
, bld_base
->base
.elem_type
);
1258 result
= radeon_llvm_emit_fetch_double(bld_base
,
1264 /* Upper 16 bits must be zero. */
1265 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1266 LLVMValueRef val
[2])
1268 return LLVMBuildOr(gallivm
->builder
, val
[0],
1269 LLVMBuildShl(gallivm
->builder
, val
[1],
1270 lp_build_const_int32(gallivm
, 16),
1274 /* Upper 16 bits are ignored and will be dropped. */
1275 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1276 LLVMValueRef val
[2])
1278 LLVMValueRef v
[2] = {
1279 LLVMBuildAnd(gallivm
->builder
, val
[0],
1280 lp_build_const_int32(gallivm
, 0xffff), ""),
1283 return si_llvm_pack_two_int16(gallivm
, v
);
1286 /* Initialize arguments for the shader export intrinsic */
1287 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1288 LLVMValueRef
*values
,
1292 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1293 struct lp_build_context
*uint
=
1294 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1295 struct lp_build_context
*base
= &bld_base
->base
;
1296 struct gallivm_state
*gallivm
= base
->gallivm
;
1297 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1298 LLVMValueRef val
[4];
1299 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1303 /* Default is 0xf. Adjusted below depending on the format. */
1304 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1306 /* Specify whether the EXEC mask represents the valid mask */
1307 args
[1] = uint
->zero
;
1309 /* Specify whether this is the last export */
1310 args
[2] = uint
->zero
;
1312 /* Specify the target we are exporting */
1313 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1315 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1316 const union si_shader_key
*key
= &si_shader_ctx
->shader
->key
;
1317 unsigned col_formats
= key
->ps
.spi_shader_col_format
;
1318 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1320 assert(cbuf
>= 0 && cbuf
< 8);
1321 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1322 is_int8
= (key
->ps
.color_is_int8
>> cbuf
) & 0x1;
1325 args
[4] = uint
->zero
; /* COMPR flag */
1326 args
[5] = base
->undef
;
1327 args
[6] = base
->undef
;
1328 args
[7] = base
->undef
;
1329 args
[8] = base
->undef
;
1331 switch (spi_shader_col_format
) {
1332 case V_028714_SPI_SHADER_ZERO
:
1333 args
[0] = uint
->zero
; /* writemask */
1334 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1337 case V_028714_SPI_SHADER_32_R
:
1338 args
[0] = uint
->one
; /* writemask */
1339 args
[5] = values
[0];
1342 case V_028714_SPI_SHADER_32_GR
:
1343 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1344 args
[5] = values
[0];
1345 args
[6] = values
[1];
1348 case V_028714_SPI_SHADER_32_AR
:
1349 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1350 args
[5] = values
[0];
1351 args
[8] = values
[3];
1354 case V_028714_SPI_SHADER_FP16_ABGR
:
1355 args
[4] = uint
->one
; /* COMPR flag */
1357 for (chan
= 0; chan
< 2; chan
++) {
1358 LLVMValueRef pack_args
[2] = {
1360 values
[2 * chan
+ 1]
1362 LLVMValueRef packed
;
1364 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1366 uint
->elem_type
, pack_args
, 2,
1367 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1369 LLVMBuildBitCast(base
->gallivm
->builder
,
1370 packed
, base
->elem_type
, "");
1374 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1375 for (chan
= 0; chan
< 4; chan
++) {
1376 val
[chan
] = radeon_llvm_saturate(bld_base
, values
[chan
]);
1377 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1378 lp_build_const_float(gallivm
, 65535), "");
1379 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1380 lp_build_const_float(gallivm
, 0.5), "");
1381 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
1382 uint
->elem_type
, "");
1385 args
[4] = uint
->one
; /* COMPR flag */
1386 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1387 si_llvm_pack_two_int16(gallivm
, val
));
1388 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1389 si_llvm_pack_two_int16(gallivm
, val
+2));
1392 case V_028714_SPI_SHADER_SNORM16_ABGR
:
1393 for (chan
= 0; chan
< 4; chan
++) {
1394 /* Clamp between [-1, 1]. */
1395 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
1397 lp_build_const_float(gallivm
, 1));
1398 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
1400 lp_build_const_float(gallivm
, -1));
1401 /* Convert to a signed integer in [-32767, 32767]. */
1402 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
1403 lp_build_const_float(gallivm
, 32767), "");
1404 /* If positive, add 0.5, else add -0.5. */
1405 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
1406 LLVMBuildSelect(builder
,
1407 LLVMBuildFCmp(builder
, LLVMRealOGE
,
1408 val
[chan
], base
->zero
, ""),
1409 lp_build_const_float(gallivm
, 0.5),
1410 lp_build_const_float(gallivm
, -0.5), ""), "");
1411 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], uint
->elem_type
, "");
1414 args
[4] = uint
->one
; /* COMPR flag */
1415 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1416 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1417 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1418 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1421 case V_028714_SPI_SHADER_UINT16_ABGR
: {
1422 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1425 for (chan
= 0; chan
< 4; chan
++) {
1426 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1427 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
1431 args
[4] = uint
->one
; /* COMPR flag */
1432 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1433 si_llvm_pack_two_int16(gallivm
, val
));
1434 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1435 si_llvm_pack_two_int16(gallivm
, val
+2));
1439 case V_028714_SPI_SHADER_SINT16_ABGR
: {
1440 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
1442 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
1445 for (chan
= 0; chan
< 4; chan
++) {
1446 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
1447 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1450 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1455 args
[4] = uint
->one
; /* COMPR flag */
1456 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1457 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
1458 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
1459 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
1463 case V_028714_SPI_SHADER_32_ABGR
:
1464 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
1469 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
1472 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1473 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1475 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
1476 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1477 SI_PARAM_ALPHA_REF
);
1479 LLVMValueRef alpha_pass
=
1480 lp_build_cmp(&bld_base
->base
,
1481 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
1484 lp_build_select(&bld_base
->base
,
1486 lp_build_const_float(gallivm
, 1.0f
),
1487 lp_build_const_float(gallivm
, -1.0f
));
1489 lp_build_intrinsic(gallivm
->builder
,
1491 LLVMVoidTypeInContext(gallivm
->context
),
1494 lp_build_intrinsic(gallivm
->builder
,
1496 LLVMVoidTypeInContext(gallivm
->context
),
1501 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
1504 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1505 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1506 LLVMValueRef coverage
;
1508 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
1509 coverage
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1510 SI_PARAM_SAMPLE_COVERAGE
);
1511 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
1513 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
1514 bld_base
->int_bld
.elem_type
,
1515 &coverage
, 1, LLVMReadNoneAttribute
);
1517 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
1518 bld_base
->base
.elem_type
, "");
1520 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
1521 lp_build_const_float(gallivm
,
1522 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
1524 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
1527 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
1528 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
1530 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1531 struct lp_build_context
*base
= &bld_base
->base
;
1532 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1535 unsigned const_chan
;
1536 LLVMValueRef base_elt
;
1537 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
1538 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
1539 LLVMValueRef const_resource
= build_indexed_load_const(si_shader_ctx
, ptr
, constbuf_index
);
1541 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
1542 LLVMValueRef
*args
= pos
[2 + reg_index
];
1547 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
1549 /* Compute dot products of position and user clip plane vectors */
1550 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1551 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
1552 args
[1] = lp_build_const_int32(base
->gallivm
,
1553 ((reg_index
* 4 + chan
) * 4 +
1555 base_elt
= buffer_load_const(base
->gallivm
->builder
, const_resource
,
1556 args
[1], base
->elem_type
);
1558 lp_build_add(base
, args
[5 + chan
],
1559 lp_build_mul(base
, base_elt
,
1560 out_elts
[const_chan
]));
1564 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
1565 args
[1] = uint
->zero
;
1566 args
[2] = uint
->zero
;
1567 args
[3] = lp_build_const_int32(base
->gallivm
,
1568 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
1569 args
[4] = uint
->zero
;
1573 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
1577 if (so
->num_outputs
)
1578 fprintf(stderr
, "STREAMOUT\n");
1580 for (i
= 0; i
< so
->num_outputs
; i
++) {
1581 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
1582 so
->output
[i
].start_component
;
1583 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
1584 i
, so
->output
[i
].output_buffer
,
1585 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
1586 so
->output
[i
].register_index
,
1587 mask
& 1 ? "x" : "",
1588 mask
& 2 ? "y" : "",
1589 mask
& 4 ? "z" : "",
1590 mask
& 8 ? "w" : "");
1594 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1595 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1596 * or v4i32 (num_channels=3,4). */
1597 static void build_tbuffer_store(struct si_shader_context
*shader
,
1600 unsigned num_channels
,
1602 LLVMValueRef soffset
,
1603 unsigned inst_offset
,
1612 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1613 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1614 LLVMValueRef args
[] = {
1617 LLVMConstInt(i32
, num_channels
, 0),
1620 LLVMConstInt(i32
, inst_offset
, 0),
1621 LLVMConstInt(i32
, dfmt
, 0),
1622 LLVMConstInt(i32
, nfmt
, 0),
1623 LLVMConstInt(i32
, offen
, 0),
1624 LLVMConstInt(i32
, idxen
, 0),
1625 LLVMConstInt(i32
, glc
, 0),
1626 LLVMConstInt(i32
, slc
, 0),
1627 LLVMConstInt(i32
, tfe
, 0)
1630 /* The instruction offset field has 12 bits */
1631 assert(offen
|| inst_offset
< (1 << 12));
1633 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
1634 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1635 const char *types
[] = {"i32", "v2i32", "v4i32"};
1637 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
1639 lp_build_intrinsic(gallivm
->builder
, name
,
1640 LLVMVoidTypeInContext(gallivm
->context
),
1641 args
, Elements(args
), 0);
1644 static void build_tbuffer_store_dwords(struct si_shader_context
*shader
,
1647 unsigned num_channels
,
1649 LLVMValueRef soffset
,
1650 unsigned inst_offset
)
1652 static unsigned dfmt
[] = {
1653 V_008F0C_BUF_DATA_FORMAT_32
,
1654 V_008F0C_BUF_DATA_FORMAT_32_32
,
1655 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1656 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1658 assert(num_channels
>= 1 && num_channels
<= 4);
1660 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
1661 inst_offset
, dfmt
[num_channels
-1],
1662 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
1665 /* On SI, the vertex shader is responsible for writing streamout data
1667 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
1668 struct si_shader_output_values
*outputs
,
1671 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
1672 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
1673 LLVMBuilderRef builder
= gallivm
->builder
;
1675 struct lp_build_if_state if_ctx
;
1677 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1679 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1680 LLVMValueRef so_vtx_count
=
1681 unpack_param(shader
, shader
->param_streamout_config
, 16, 7);
1683 LLVMValueRef tid
= lp_build_intrinsic(builder
, "llvm.SI.tid", i32
,
1684 NULL
, 0, LLVMReadNoneAttribute
);
1686 /* can_emit = tid < so_vtx_count; */
1687 LLVMValueRef can_emit
=
1688 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
1690 LLVMValueRef stream_id
=
1691 unpack_param(shader
, shader
->param_streamout_config
, 24, 2);
1693 /* Emit the streamout code conditionally. This actually avoids
1694 * out-of-bounds buffer access. The hw tells us via the SGPR
1695 * (so_vtx_count) which threads are allowed to emit streamout data. */
1696 lp_build_if(&if_ctx
, gallivm
, can_emit
);
1698 /* The buffer offset is computed as follows:
1699 * ByteOffset = streamout_offset[buffer_id]*4 +
1700 * (streamout_write_index + thread_id)*stride[buffer_id] +
1704 LLVMValueRef so_write_index
=
1705 LLVMGetParam(shader
->radeon_bld
.main_fn
,
1706 shader
->param_streamout_write_index
);
1708 /* Compute (streamout_write_index + thread_id). */
1709 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
1711 /* Compute the write offset for each enabled buffer. */
1712 LLVMValueRef so_write_offset
[4] = {};
1713 for (i
= 0; i
< 4; i
++) {
1717 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
1718 shader
->param_streamout_offset
[i
]);
1719 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
1721 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1722 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
1723 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1726 /* Write streamout data. */
1727 for (i
= 0; i
< so
->num_outputs
; i
++) {
1728 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1729 unsigned reg
= so
->output
[i
].register_index
;
1730 unsigned start
= so
->output
[i
].start_component
;
1731 unsigned num_comps
= so
->output
[i
].num_components
;
1732 unsigned stream
= so
->output
[i
].stream
;
1733 LLVMValueRef out
[4];
1734 struct lp_build_if_state if_ctx_stream
;
1736 assert(num_comps
&& num_comps
<= 4);
1737 if (!num_comps
|| num_comps
> 4)
1743 /* Load the output as int. */
1744 for (j
= 0; j
< num_comps
; j
++) {
1745 out
[j
] = LLVMBuildBitCast(builder
,
1746 outputs
[reg
].values
[start
+j
],
1750 /* Pack the output. */
1751 LLVMValueRef vdata
= NULL
;
1753 switch (num_comps
) {
1754 case 1: /* as i32 */
1757 case 2: /* as v2i32 */
1758 case 3: /* as v4i32 (aligned to 4) */
1759 case 4: /* as v4i32 */
1760 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
1761 for (j
= 0; j
< num_comps
; j
++) {
1762 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1763 LLVMConstInt(i32
, j
, 0), "");
1768 LLVMValueRef can_emit_stream
=
1769 LLVMBuildICmp(builder
, LLVMIntEQ
,
1771 lp_build_const_int32(gallivm
, stream
), "");
1773 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
1774 build_tbuffer_store_dwords(shader
, shader
->so_buffers
[buf_idx
],
1776 so_write_offset
[buf_idx
],
1777 LLVMConstInt(i32
, 0, 0),
1778 so
->output
[i
].dst_offset
*4);
1779 lp_build_endif(&if_ctx_stream
);
1782 lp_build_endif(&if_ctx
);
1786 /* Generate export instructions for hardware VS shader stage */
1787 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1788 struct si_shader_output_values
*outputs
,
1791 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1792 struct si_shader
* shader
= si_shader_ctx
->shader
;
1793 struct lp_build_context
* base
= &bld_base
->base
;
1794 struct lp_build_context
* uint
=
1795 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1796 LLVMValueRef args
[9];
1797 LLVMValueRef pos_args
[4][9] = { { 0 } };
1798 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
1799 unsigned semantic_name
, semantic_index
;
1801 unsigned param_count
= 0;
1805 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1806 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1809 for (i
= 0; i
< noutput
; i
++) {
1810 semantic_name
= outputs
[i
].name
;
1811 semantic_index
= outputs
[i
].sid
;
1814 /* Select the correct target */
1815 switch(semantic_name
) {
1816 case TGSI_SEMANTIC_PSIZE
:
1817 psize_value
= outputs
[i
].values
[0];
1819 case TGSI_SEMANTIC_EDGEFLAG
:
1820 edgeflag_value
= outputs
[i
].values
[0];
1822 case TGSI_SEMANTIC_LAYER
:
1823 layer_value
= outputs
[i
].values
[0];
1824 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1825 goto handle_semantic
;
1826 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
1827 viewport_index_value
= outputs
[i
].values
[0];
1828 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1829 goto handle_semantic
;
1830 case TGSI_SEMANTIC_POSITION
:
1831 target
= V_008DFC_SQ_EXP_POS
;
1833 case TGSI_SEMANTIC_COLOR
:
1834 case TGSI_SEMANTIC_BCOLOR
:
1835 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1836 shader
->vs_output_param_offset
[i
] = param_count
;
1839 case TGSI_SEMANTIC_CLIPDIST
:
1840 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1842 case TGSI_SEMANTIC_CLIPVERTEX
:
1843 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1845 case TGSI_SEMANTIC_PRIMID
:
1846 case TGSI_SEMANTIC_FOG
:
1847 case TGSI_SEMANTIC_TEXCOORD
:
1848 case TGSI_SEMANTIC_GENERIC
:
1849 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1850 shader
->vs_output_param_offset
[i
] = param_count
;
1856 "Warning: SI unhandled vs output type:%d\n",
1860 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1862 if (target
>= V_008DFC_SQ_EXP_POS
&&
1863 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1864 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1865 args
, sizeof(args
));
1867 lp_build_intrinsic(base
->gallivm
->builder
,
1869 LLVMVoidTypeInContext(base
->gallivm
->context
),
1873 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1874 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1875 goto handle_semantic
;
1879 shader
->nr_param_exports
= param_count
;
1881 /* We need to add the position output manually if it's missing. */
1882 if (!pos_args
[0][0]) {
1883 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1884 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1885 pos_args
[0][2] = uint
->zero
; /* last export? */
1886 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1887 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1888 pos_args
[0][5] = base
->zero
; /* X */
1889 pos_args
[0][6] = base
->zero
; /* Y */
1890 pos_args
[0][7] = base
->zero
; /* Z */
1891 pos_args
[0][8] = base
->one
; /* W */
1894 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1895 if (shader
->selector
->info
.writes_psize
||
1896 shader
->selector
->info
.writes_edgeflag
||
1897 shader
->selector
->info
.writes_viewport_index
||
1898 shader
->selector
->info
.writes_layer
) {
1899 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1900 shader
->selector
->info
.writes_psize
|
1901 (shader
->selector
->info
.writes_edgeflag
<< 1) |
1902 (shader
->selector
->info
.writes_layer
<< 2) |
1903 (shader
->selector
->info
.writes_viewport_index
<< 3));
1904 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1905 pos_args
[1][2] = uint
->zero
; /* last export? */
1906 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1907 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1908 pos_args
[1][5] = base
->zero
; /* X */
1909 pos_args
[1][6] = base
->zero
; /* Y */
1910 pos_args
[1][7] = base
->zero
; /* Z */
1911 pos_args
[1][8] = base
->zero
; /* W */
1913 if (shader
->selector
->info
.writes_psize
)
1914 pos_args
[1][5] = psize_value
;
1916 if (shader
->selector
->info
.writes_edgeflag
) {
1917 /* The output is a float, but the hw expects an integer
1918 * with the first bit containing the edge flag. */
1919 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1921 bld_base
->uint_bld
.elem_type
, "");
1922 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1924 bld_base
->int_bld
.one
);
1926 /* The LLVM intrinsic expects a float. */
1927 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1929 base
->elem_type
, "");
1932 if (shader
->selector
->info
.writes_layer
)
1933 pos_args
[1][7] = layer_value
;
1935 if (shader
->selector
->info
.writes_viewport_index
)
1936 pos_args
[1][8] = viewport_index_value
;
1939 for (i
= 0; i
< 4; i
++)
1941 shader
->nr_pos_exports
++;
1944 for (i
= 0; i
< 4; i
++) {
1945 if (!pos_args
[i
][0])
1948 /* Specify the target we are exporting */
1949 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1951 if (pos_idx
== shader
->nr_pos_exports
)
1952 /* Specify that this is the last export */
1953 pos_args
[i
][2] = uint
->one
;
1955 lp_build_intrinsic(base
->gallivm
->builder
,
1957 LLVMVoidTypeInContext(base
->gallivm
->context
),
1962 /* This only writes the tessellation factor levels. */
1963 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1965 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1966 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1967 struct si_shader
*shader
= si_shader_ctx
->shader
;
1968 unsigned tess_inner_index
, tess_outer_index
;
1969 LLVMValueRef lds_base
, lds_inner
, lds_outer
;
1970 LLVMValueRef tf_base
, rel_patch_id
, byteoffset
, buffer
, rw_buffers
;
1971 LLVMValueRef out
[6], vec0
, vec1
, invocation_id
;
1972 unsigned stride
, outer_comps
, inner_comps
, i
;
1973 struct lp_build_if_state if_ctx
;
1975 invocation_id
= unpack_param(si_shader_ctx
, SI_PARAM_REL_IDS
, 8, 5);
1977 /* Do this only for invocation 0, because the tess levels are per-patch,
1980 * This can't jump, because invocation 0 executes this. It should
1981 * at least mask out the loads and stores for other invocations.
1983 lp_build_if(&if_ctx
, gallivm
,
1984 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
1985 invocation_id
, bld_base
->uint_bld
.zero
, ""));
1987 /* Determine the layout of one tess factor element in the buffer. */
1988 switch (shader
->key
.tcs
.prim_mode
) {
1989 case PIPE_PRIM_LINES
:
1990 stride
= 2; /* 2 dwords, 1 vec2 store */
1994 case PIPE_PRIM_TRIANGLES
:
1995 stride
= 4; /* 4 dwords, 1 vec4 store */
1999 case PIPE_PRIM_QUADS
:
2000 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2009 /* Load tess_inner and tess_outer from LDS.
2010 * Any invocation can write them, so we can't get them from a temporary.
2012 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2013 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2015 lds_base
= get_tcs_out_current_patch_data_offset(si_shader_ctx
);
2016 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2017 lp_build_const_int32(gallivm
,
2018 tess_inner_index
* 4), "");
2019 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2020 lp_build_const_int32(gallivm
,
2021 tess_outer_index
* 4), "");
2023 for (i
= 0; i
< outer_comps
; i
++)
2024 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2025 for (i
= 0; i
< inner_comps
; i
++)
2026 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2028 /* Convert the outputs to vectors for stores. */
2029 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2033 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2035 /* Get the buffer. */
2036 rw_buffers
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2037 SI_PARAM_RW_BUFFERS
);
2038 buffer
= build_indexed_load_const(si_shader_ctx
, rw_buffers
,
2039 lp_build_const_int32(gallivm
, SI_RING_TESS_FACTOR
));
2041 /* Get the offset. */
2042 tf_base
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2043 SI_PARAM_TESS_FACTOR_OFFSET
);
2044 rel_patch_id
= get_rel_patch_id(si_shader_ctx
);
2045 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2046 lp_build_const_int32(gallivm
, 4 * stride
), "");
2048 /* Store the outputs. */
2049 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec0
,
2050 MIN2(stride
, 4), byteoffset
, tf_base
, 0);
2052 build_tbuffer_store_dwords(si_shader_ctx
, buffer
, vec1
,
2053 stride
- 4, byteoffset
, tf_base
, 16);
2054 lp_build_endif(&if_ctx
);
2057 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
* bld_base
)
2059 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2060 struct si_shader
*shader
= si_shader_ctx
->shader
;
2061 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2062 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2064 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2065 si_shader_ctx
->param_rel_auto_id
);
2066 LLVMValueRef vertex_dw_stride
=
2067 unpack_param(si_shader_ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2068 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2069 vertex_dw_stride
, "");
2071 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2072 * its inputs from it. */
2073 for (i
= 0; i
< info
->num_outputs
; i
++) {
2074 LLVMValueRef
*out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
2075 unsigned name
= info
->output_semantic_name
[i
];
2076 unsigned index
= info
->output_semantic_index
[i
];
2077 int param
= si_shader_io_get_unique_index(name
, index
);
2078 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2079 lp_build_const_int32(gallivm
, param
* 4), "");
2081 for (chan
= 0; chan
< 4; chan
++) {
2082 lds_store(bld_base
, chan
, dw_addr
,
2083 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2088 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
2090 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2091 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2092 struct si_shader
*es
= si_shader_ctx
->shader
;
2093 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2094 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2095 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2096 si_shader_ctx
->param_es2gs_offset
);
2100 for (i
= 0; i
< info
->num_outputs
; i
++) {
2101 LLVMValueRef
*out_ptr
=
2102 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
2105 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2106 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2109 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2110 info
->output_semantic_index
[i
]);
2112 for (chan
= 0; chan
< 4; chan
++) {
2113 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2114 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
2116 build_tbuffer_store(si_shader_ctx
,
2117 si_shader_ctx
->esgs_ring
,
2119 LLVMGetUndef(i32
), soffset
,
2120 (4 * param_index
+ chan
) * 4,
2121 V_008F0C_BUF_DATA_FORMAT_32
,
2122 V_008F0C_BUF_NUM_FORMAT_UINT
,
2128 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2130 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2131 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2132 LLVMValueRef args
[2];
2134 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2135 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2136 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2137 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2138 LLVMNoUnwindAttribute
);
2141 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2143 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2144 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2145 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
2146 struct si_shader_output_values
*outputs
= NULL
;
2149 assert(!si_shader_ctx
->is_gs_copy_shader
);
2151 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2153 /* Vertex color clamping.
2155 * This uses a state constant loaded in a user data SGPR and
2156 * an IF statement is added that clamps all colors if the constant
2159 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
2160 struct lp_build_if_state if_ctx
;
2161 LLVMValueRef cond
= NULL
;
2162 LLVMValueRef addr
, val
;
2164 for (i
= 0; i
< info
->num_outputs
; i
++) {
2165 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2166 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2169 /* We've found a color. */
2171 /* The state is in the first bit of the user SGPR. */
2172 cond
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2173 SI_PARAM_VS_STATE_BITS
);
2174 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2175 LLVMInt1TypeInContext(gallivm
->context
), "");
2176 lp_build_if(&if_ctx
, gallivm
, cond
);
2179 for (j
= 0; j
< 4; j
++) {
2180 addr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
];
2181 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2182 val
= radeon_llvm_saturate(bld_base
, val
);
2183 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2188 lp_build_endif(&if_ctx
);
2191 for (i
= 0; i
< info
->num_outputs
; i
++) {
2192 outputs
[i
].name
= info
->output_semantic_name
[i
];
2193 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2195 for (j
= 0; j
< 4; j
++)
2196 outputs
[i
].values
[j
] =
2197 LLVMBuildLoad(gallivm
->builder
,
2198 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
],
2202 /* Export PrimitiveID when PS needs it. */
2203 if (si_vs_exports_prim_id(si_shader_ctx
->shader
)) {
2204 outputs
[i
].name
= TGSI_SEMANTIC_PRIMID
;
2206 outputs
[i
].values
[0] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2207 get_primitive_id(bld_base
, 0));
2208 outputs
[i
].values
[1] = bld_base
->base
.undef
;
2209 outputs
[i
].values
[2] = bld_base
->base
.undef
;
2210 outputs
[i
].values
[3] = bld_base
->base
.undef
;
2214 si_llvm_export_vs(bld_base
, outputs
, i
);
2218 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2219 LLVMValueRef depth
, LLVMValueRef stencil
,
2220 LLVMValueRef samplemask
)
2222 struct si_screen
*sscreen
= si_shader_context(bld_base
)->screen
;
2223 struct lp_build_context
*base
= &bld_base
->base
;
2224 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2225 LLVMValueRef args
[9];
2228 assert(depth
|| stencil
|| samplemask
);
2230 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2231 args
[2] = uint
->one
; /* DONE bit */
2233 /* Specify the target we are exporting */
2234 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2236 args
[4] = uint
->zero
; /* COMP flag */
2237 args
[5] = base
->undef
; /* R, depth */
2238 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2239 args
[7] = base
->undef
; /* B, sample mask */
2240 args
[8] = base
->undef
; /* A, alpha to mask */
2253 args
[7] = samplemask
;
2257 /* SI (except OLAND) has a bug that it only looks
2258 * at the X writemask component. */
2259 if (sscreen
->b
.chip_class
== SI
&&
2260 sscreen
->b
.family
!= CHIP_OLAND
)
2263 /* Specify which components to enable */
2264 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
2266 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2267 LLVMVoidTypeInContext(base
->gallivm
->context
),
2271 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
2272 LLVMValueRef
*color
, unsigned index
,
2275 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2276 struct lp_build_context
*base
= &bld_base
->base
;
2277 LLVMValueRef args
[9];
2281 if (si_shader_ctx
->shader
->key
.ps
.clamp_color
)
2282 for (i
= 0; i
< 4; i
++)
2283 color
[i
] = radeon_llvm_saturate(bld_base
, color
[i
]);
2286 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
2287 color
[3] = base
->one
;
2291 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
2292 si_alpha_test(bld_base
, color
[3]);
2294 /* Line & polygon smoothing */
2295 if (si_shader_ctx
->shader
->key
.ps
.poly_line_smoothing
)
2296 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3]);
2298 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
2300 si_shader_ctx
->shader
->key
.ps
.last_cbuf
> 0) {
2301 for (int c
= 1; c
<= si_shader_ctx
->shader
->key
.ps
.last_cbuf
; c
++) {
2302 si_llvm_init_export_args(bld_base
, color
,
2303 V_008DFC_SQ_EXP_MRT
+ c
, args
);
2304 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2305 LLVMVoidTypeInContext(base
->gallivm
->context
),
2311 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
2314 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
2315 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
2317 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2318 LLVMVoidTypeInContext(base
->gallivm
->context
),
2322 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
2324 struct lp_build_context
*base
= &bld_base
->base
;
2325 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2326 LLVMValueRef args
[9];
2328 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
2329 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2330 args
[2] = uint
->one
; /* DONE bit */
2331 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
2332 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
2333 args
[5] = uint
->undef
; /* R */
2334 args
[6] = uint
->undef
; /* G */
2335 args
[7] = uint
->undef
; /* B */
2336 args
[8] = uint
->undef
; /* A */
2338 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2339 LLVMVoidTypeInContext(base
->gallivm
->context
),
2343 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
2345 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
2346 struct si_shader
* shader
= si_shader_ctx
->shader
;
2347 struct lp_build_context
* base
= &bld_base
->base
;
2348 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2349 LLVMBuilderRef builder
= base
->gallivm
->builder
;
2350 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
2351 int last_color_export
= -1;
2354 /* If there are no outputs, add a dummy export. */
2355 if (!info
->num_outputs
) {
2356 si_export_null(bld_base
);
2360 /* Determine the last export. If MRTZ is present, it's always last.
2361 * Otherwise, find the last color export.
2363 if (!info
->writes_z
&& !info
->writes_stencil
&& !info
->writes_samplemask
)
2364 for (i
= 0; i
< info
->num_outputs
; i
++)
2365 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
)
2366 last_color_export
= i
;
2368 for (i
= 0; i
< info
->num_outputs
; i
++) {
2369 unsigned semantic_name
= info
->output_semantic_name
[i
];
2370 unsigned semantic_index
= info
->output_semantic_index
[i
];
2372 LLVMValueRef color
[4] = {};
2374 /* Select the correct target */
2375 switch (semantic_name
) {
2376 case TGSI_SEMANTIC_POSITION
:
2377 depth
= LLVMBuildLoad(builder
,
2378 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][2], "");
2380 case TGSI_SEMANTIC_STENCIL
:
2381 stencil
= LLVMBuildLoad(builder
,
2382 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][1], "");
2384 case TGSI_SEMANTIC_SAMPLEMASK
:
2385 samplemask
= LLVMBuildLoad(builder
,
2386 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][0], "");
2388 case TGSI_SEMANTIC_COLOR
:
2389 for (j
= 0; j
< 4; j
++)
2390 color
[j
] = LLVMBuildLoad(builder
,
2391 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
], "");
2393 si_export_mrt_color(bld_base
, color
, semantic_index
,
2394 last_color_export
== i
);
2398 "Warning: SI unhandled fs output type:%d\n",
2403 if (depth
|| stencil
|| samplemask
)
2404 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
);
2407 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2408 struct lp_build_tgsi_context
* bld_base
,
2409 struct lp_build_emit_data
* emit_data
);
2411 static bool tgsi_is_array_sampler(unsigned target
)
2413 return target
== TGSI_TEXTURE_1D_ARRAY
||
2414 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
2415 target
== TGSI_TEXTURE_2D_ARRAY
||
2416 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
2417 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2418 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
2419 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
2422 static void set_tex_fetch_args(struct gallivm_state
*gallivm
,
2423 struct lp_build_emit_data
*emit_data
,
2424 unsigned opcode
, unsigned target
,
2425 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
2426 LLVMValueRef
*param
, unsigned count
,
2430 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
2431 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2433 /* Pad to power of two vector */
2434 while (count
< util_next_power_of_two(count
))
2435 param
[count
++] = LLVMGetUndef(i32
);
2437 /* Texture coordinates. */
2439 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
2441 emit_data
->args
[0] = param
[0];
2444 emit_data
->args
[1] = res_ptr
;
2447 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
2448 emit_data
->dst_type
= LLVMVectorType(i32
, 4);
2450 emit_data
->dst_type
= LLVMVectorType(
2451 LLVMFloatTypeInContext(gallivm
->context
), 4);
2453 emit_data
->args
[num_args
++] = samp_ptr
;
2456 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
2457 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
2458 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
2459 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
2460 tgsi_is_array_sampler(target
)); /* da */
2461 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
2462 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
2463 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
2464 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
2466 emit_data
->arg_count
= num_args
;
2469 static const struct lp_build_tgsi_action tex_action
;
2471 static void tex_fetch_ptrs(
2472 struct lp_build_tgsi_context
* bld_base
,
2473 struct lp_build_emit_data
* emit_data
,
2474 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
2476 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2477 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2478 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2479 unsigned target
= inst
->Texture
.Texture
;
2480 unsigned sampler_src
;
2481 unsigned sampler_index
;
2483 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
2484 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
2486 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
2487 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
2488 LLVMValueRef ind_index
;
2490 ind_index
= get_indirect_index(si_shader_ctx
, ®
->Indirect
, reg
->Register
.Index
);
2492 *res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER_VIEWS
);
2493 *res_ptr
= build_indexed_load_const(si_shader_ctx
, *res_ptr
, ind_index
);
2495 *samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER_STATES
);
2496 *samp_ptr
= build_indexed_load_const(si_shader_ctx
, *samp_ptr
, ind_index
);
2498 if (target
== TGSI_TEXTURE_2D_MSAA
||
2499 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2500 ind_index
= LLVMBuildAdd(gallivm
->builder
, ind_index
,
2501 lp_build_const_int32(gallivm
,
2502 SI_FMASK_TEX_OFFSET
), "");
2503 *fmask_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER_VIEWS
);
2504 *fmask_ptr
= build_indexed_load_const(si_shader_ctx
, *fmask_ptr
, ind_index
);
2507 *res_ptr
= si_shader_ctx
->sampler_views
[sampler_index
];
2508 *samp_ptr
= si_shader_ctx
->sampler_states
[sampler_index
];
2509 *fmask_ptr
= si_shader_ctx
->sampler_views
[SI_FMASK_TEX_OFFSET
+ sampler_index
];
2513 static void tex_fetch_args(
2514 struct lp_build_tgsi_context
* bld_base
,
2515 struct lp_build_emit_data
* emit_data
)
2517 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2518 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2519 LLVMBuilderRef builder
= gallivm
->builder
;
2520 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
2521 unsigned opcode
= inst
->Instruction
.Opcode
;
2522 unsigned target
= inst
->Texture
.Texture
;
2523 LLVMValueRef coords
[5], derivs
[6];
2524 LLVMValueRef address
[16];
2526 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
2529 unsigned num_deriv_channels
= 0;
2530 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
2531 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2532 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2533 unsigned dmask
= 0xf;
2535 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2537 if (opcode
== TGSI_OPCODE_TXQ
) {
2538 if (target
== TGSI_TEXTURE_BUFFER
) {
2539 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2541 /* Read the size from the buffer descriptor directly. */
2542 LLVMValueRef res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2543 LLVMValueRef size
= LLVMBuildExtractElement(builder
, res
,
2544 lp_build_const_int32(gallivm
, 6), "");
2546 if (si_shader_ctx
->screen
->b
.chip_class
>= VI
) {
2547 /* On VI, the descriptor contains the size in bytes,
2548 * but TXQ must return the size in elements.
2549 * The stride is always non-zero for resources using TXQ.
2551 LLVMValueRef stride
=
2552 LLVMBuildExtractElement(builder
, res
,
2553 lp_build_const_int32(gallivm
, 5), "");
2554 stride
= LLVMBuildLShr(builder
, stride
,
2555 lp_build_const_int32(gallivm
, 16), "");
2556 stride
= LLVMBuildAnd(builder
, stride
,
2557 lp_build_const_int32(gallivm
, 0x3FFF), "");
2559 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
2562 emit_data
->args
[0] = size
;
2566 /* Textures - set the mip level. */
2567 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
2569 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2570 NULL
, address
, count
, 0xf);
2574 if (target
== TGSI_TEXTURE_BUFFER
) {
2575 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
2576 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
2577 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
2578 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
2580 /* Bitcast and truncate v8i32 to v16i8. */
2581 LLVMValueRef res
= res_ptr
;
2582 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
2583 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.one
, "");
2584 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
2586 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
2587 emit_data
->args
[0] = res
;
2588 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
2589 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
2590 emit_data
->arg_count
= 3;
2594 /* Fetch and project texture coordinates */
2595 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
2596 for (chan
= 0; chan
< 3; chan
++ ) {
2597 coords
[chan
] = lp_build_emit_fetch(bld_base
,
2600 if (opcode
== TGSI_OPCODE_TXP
)
2601 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2607 if (opcode
== TGSI_OPCODE_TXP
)
2608 coords
[3] = bld_base
->base
.one
;
2611 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
2612 /* The offsets are six-bit signed integers packed like this:
2613 * X=[5:0], Y=[13:8], and Z=[21:16].
2615 LLVMValueRef offset
[3], pack
;
2617 assert(inst
->Texture
.NumOffsets
== 1);
2619 for (chan
= 0; chan
< 3; chan
++) {
2620 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
2621 emit_data
->inst
, 0, chan
);
2622 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
2623 lp_build_const_int32(gallivm
, 0x3f), "");
2625 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
2626 lp_build_const_int32(gallivm
, chan
*8), "");
2629 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
2630 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
2631 address
[count
++] = pack
;
2634 /* Pack LOD bias value */
2635 if (opcode
== TGSI_OPCODE_TXB
)
2636 address
[count
++] = coords
[3];
2637 if (opcode
== TGSI_OPCODE_TXB2
)
2638 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2640 /* Pack depth comparison value */
2641 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
2642 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
2643 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2645 assert(ref_pos
>= 0);
2646 address
[count
++] = coords
[ref_pos
];
2650 /* Pack user derivatives */
2651 if (opcode
== TGSI_OPCODE_TXD
) {
2652 int param
, num_src_deriv_channels
;
2655 case TGSI_TEXTURE_3D
:
2656 num_src_deriv_channels
= 3;
2657 num_deriv_channels
= 3;
2659 case TGSI_TEXTURE_2D
:
2660 case TGSI_TEXTURE_SHADOW2D
:
2661 case TGSI_TEXTURE_RECT
:
2662 case TGSI_TEXTURE_SHADOWRECT
:
2663 case TGSI_TEXTURE_2D_ARRAY
:
2664 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2665 num_src_deriv_channels
= 2;
2666 num_deriv_channels
= 2;
2668 case TGSI_TEXTURE_CUBE
:
2669 case TGSI_TEXTURE_SHADOWCUBE
:
2670 case TGSI_TEXTURE_CUBE_ARRAY
:
2671 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
2672 /* Cube derivatives will be converted to 2D. */
2673 num_src_deriv_channels
= 3;
2674 num_deriv_channels
= 2;
2676 case TGSI_TEXTURE_1D
:
2677 case TGSI_TEXTURE_SHADOW1D
:
2678 case TGSI_TEXTURE_1D_ARRAY
:
2679 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2680 num_src_deriv_channels
= 1;
2681 num_deriv_channels
= 1;
2684 unreachable("invalid target");
2687 for (param
= 0; param
< 2; param
++)
2688 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
2689 derivs
[param
* num_src_deriv_channels
+ chan
] =
2690 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
2693 if (target
== TGSI_TEXTURE_CUBE
||
2694 target
== TGSI_TEXTURE_CUBE_ARRAY
||
2695 target
== TGSI_TEXTURE_SHADOWCUBE
||
2696 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
2697 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
2699 if (opcode
== TGSI_OPCODE_TXD
)
2700 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
2701 address
[count
++] = derivs
[i
];
2703 /* Pack texture coordinates */
2704 address
[count
++] = coords
[0];
2706 address
[count
++] = coords
[1];
2708 address
[count
++] = coords
[2];
2710 /* Pack LOD or sample index */
2711 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
2712 address
[count
++] = coords
[3];
2713 else if (opcode
== TGSI_OPCODE_TXL2
)
2714 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
2717 assert(!"Cannot handle more than 16 texture address parameters");
2721 for (chan
= 0; chan
< count
; chan
++ ) {
2722 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
2723 address
[chan
], i32
, "");
2726 /* Adjust the sample index according to FMASK.
2728 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2729 * which is the identity mapping. Each nibble says which physical sample
2730 * should be fetched to get that sample.
2732 * For example, 0x11111100 means there are only 2 samples stored and
2733 * the second sample covers 3/4 of the pixel. When reading samples 0
2734 * and 1, return physical sample 0 (determined by the first two 0s
2735 * in FMASK), otherwise return physical sample 1.
2737 * The sample index should be adjusted as follows:
2738 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2740 if (target
== TGSI_TEXTURE_2D_MSAA
||
2741 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
2742 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2743 struct lp_build_emit_data txf_emit_data
= *emit_data
;
2744 LLVMValueRef txf_address
[4];
2745 unsigned txf_count
= count
;
2746 struct tgsi_full_instruction inst
= {};
2748 memcpy(txf_address
, address
, sizeof(txf_address
));
2750 if (target
== TGSI_TEXTURE_2D_MSAA
) {
2751 txf_address
[2] = bld_base
->uint_bld
.zero
;
2753 txf_address
[3] = bld_base
->uint_bld
.zero
;
2755 /* Read FMASK using TXF. */
2756 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
2757 inst
.Texture
.Texture
= target
;
2758 txf_emit_data
.inst
= &inst
;
2759 txf_emit_data
.chan
= 0;
2760 set_tex_fetch_args(gallivm
, &txf_emit_data
, TGSI_OPCODE_TXF
,
2761 target
, fmask_ptr
, NULL
,
2762 txf_address
, txf_count
, 0xf);
2763 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
2765 /* Initialize some constants. */
2766 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
2767 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
2769 /* Apply the formula. */
2770 LLVMValueRef fmask
=
2771 LLVMBuildExtractElement(gallivm
->builder
,
2772 txf_emit_data
.output
[0],
2773 uint_bld
->zero
, "");
2775 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
2777 LLVMValueRef sample_index4
=
2778 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
2780 LLVMValueRef shifted_fmask
=
2781 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
2783 LLVMValueRef final_sample
=
2784 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
2786 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2787 * resource descriptor is 0 (invalid),
2789 LLVMValueRef fmask_desc
=
2790 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
2791 LLVMVectorType(uint_bld
->elem_type
, 8), "");
2793 LLVMValueRef fmask_word1
=
2794 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
2797 LLVMValueRef word1_is_nonzero
=
2798 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
2799 fmask_word1
, uint_bld
->zero
, "");
2801 /* Replace the MSAA sample index. */
2802 address
[sample_chan
] =
2803 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
2804 final_sample
, address
[sample_chan
], "");
2807 if (opcode
== TGSI_OPCODE_TXF
) {
2808 /* add tex offsets */
2809 if (inst
->Texture
.NumOffsets
) {
2810 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
2811 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
2812 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
2814 assert(inst
->Texture
.NumOffsets
== 1);
2817 case TGSI_TEXTURE_3D
:
2818 address
[2] = lp_build_add(uint_bld
, address
[2],
2819 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
2821 case TGSI_TEXTURE_2D
:
2822 case TGSI_TEXTURE_SHADOW2D
:
2823 case TGSI_TEXTURE_RECT
:
2824 case TGSI_TEXTURE_SHADOWRECT
:
2825 case TGSI_TEXTURE_2D_ARRAY
:
2826 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
2828 lp_build_add(uint_bld
, address
[1],
2829 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
2831 case TGSI_TEXTURE_1D
:
2832 case TGSI_TEXTURE_SHADOW1D
:
2833 case TGSI_TEXTURE_1D_ARRAY
:
2834 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
2836 lp_build_add(uint_bld
, address
[0],
2837 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
2839 /* texture offsets do not apply to other texture targets */
2844 if (opcode
== TGSI_OPCODE_TG4
) {
2845 unsigned gather_comp
= 0;
2847 /* DMASK was repurposed for GATHER4. 4 components are always
2848 * returned and DMASK works like a swizzle - it selects
2849 * the component to fetch. The only valid DMASK values are
2850 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
2851 * (red,red,red,red) etc.) The ISA document doesn't mention
2855 /* Get the component index from src1.x for Gather4. */
2856 if (!tgsi_is_shadow_target(target
)) {
2857 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
2858 LLVMValueRef comp_imm
;
2859 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
2861 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
2863 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
2864 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
2865 gather_comp
= CLAMP(gather_comp
, 0, 3);
2868 dmask
= 1 << gather_comp
;
2871 set_tex_fetch_args(gallivm
, emit_data
, opcode
, target
, res_ptr
,
2872 samp_ptr
, address
, count
, dmask
);
2875 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
2876 struct lp_build_tgsi_context
* bld_base
,
2877 struct lp_build_emit_data
* emit_data
)
2879 struct lp_build_context
* base
= &bld_base
->base
;
2880 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
2881 unsigned target
= emit_data
->inst
->Texture
.Texture
;
2882 char intr_name
[127];
2883 bool has_offset
= emit_data
->inst
->Texture
.NumOffsets
> 0;
2884 bool is_shadow
= tgsi_is_shadow_target(target
);
2886 const char *name
= "llvm.SI.image.sample";
2887 const char *infix
= "";
2889 if (opcode
== TGSI_OPCODE_TXQ
&& target
== TGSI_TEXTURE_BUFFER
) {
2890 /* Just return the buffer size. */
2891 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
2895 if (target
== TGSI_TEXTURE_BUFFER
) {
2896 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2897 base
->gallivm
->builder
,
2898 "llvm.SI.vs.load.input", emit_data
->dst_type
,
2899 emit_data
->args
, emit_data
->arg_count
,
2900 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2905 case TGSI_OPCODE_TXF
:
2906 name
= target
== TGSI_TEXTURE_2D_MSAA
||
2907 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
2908 "llvm.SI.image.load" :
2909 "llvm.SI.image.load.mip";
2913 case TGSI_OPCODE_TXQ
:
2914 name
= "llvm.SI.getresinfo";
2918 case TGSI_OPCODE_LODQ
:
2919 name
= "llvm.SI.getlod";
2923 case TGSI_OPCODE_TEX
:
2924 case TGSI_OPCODE_TEX2
:
2925 case TGSI_OPCODE_TXP
:
2927 case TGSI_OPCODE_TXB
:
2928 case TGSI_OPCODE_TXB2
:
2931 case TGSI_OPCODE_TXL
:
2932 case TGSI_OPCODE_TXL2
:
2935 case TGSI_OPCODE_TXD
:
2938 case TGSI_OPCODE_TG4
:
2939 name
= "llvm.SI.gather4";
2946 if (LLVMGetTypeKind(LLVMTypeOf(emit_data
->args
[0])) == LLVMVectorTypeKind
)
2947 sprintf(type
, ".v%ui32",
2948 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
2950 strcpy(type
, ".i32");
2952 /* Add the type and suffixes .c, .o if needed. */
2953 sprintf(intr_name
, "%s%s%s%s%s",
2954 name
, is_shadow
? ".c" : "", infix
,
2955 has_offset
? ".o" : "", type
);
2957 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
2958 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
2959 emit_data
->args
, emit_data
->arg_count
,
2960 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2962 /* Divide the number of layers by 6 to get the number of cubes. */
2963 if (opcode
== TGSI_OPCODE_TXQ
&&
2964 (target
== TGSI_TEXTURE_CUBE_ARRAY
||
2965 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)) {
2966 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2967 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
2968 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
2970 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
2971 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
2972 z
= LLVMBuildSDiv(builder
, z
, six
, "");
2974 emit_data
->output
[emit_data
->chan
] =
2975 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
2979 static void si_llvm_emit_txqs(
2980 const struct lp_build_tgsi_action
* action
,
2981 struct lp_build_tgsi_context
* bld_base
,
2982 struct lp_build_emit_data
* emit_data
)
2984 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2985 LLVMBuilderRef builder
= gallivm
->builder
;
2986 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2987 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2988 LLVMValueRef res
, samples
;
2989 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
2991 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
2994 /* Read the samples from the descriptor directly. */
2995 res
= LLVMBuildBitCast(builder
, res_ptr
, v8i32
, "");
2996 samples
= LLVMBuildExtractElement(
2998 lp_build_const_int32(gallivm
, 3), "");
2999 samples
= LLVMBuildLShr(builder
, samples
,
3000 lp_build_const_int32(gallivm
, 16), "");
3001 samples
= LLVMBuildAnd(builder
, samples
,
3002 lp_build_const_int32(gallivm
, 0xf), "");
3003 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
3006 emit_data
->output
[emit_data
->chan
] = samples
;
3010 * SI implements derivatives using the local data store (LDS)
3011 * All writes to the LDS happen in all executing threads at
3012 * the same time. TID is the Thread ID for the current
3013 * thread and is a value between 0 and 63, representing
3014 * the thread's position in the wavefront.
3016 * For the pixel shader threads are grouped into quads of four pixels.
3017 * The TIDs of the pixels of a quad are:
3025 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
3026 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
3027 * the current pixel's column, and masking with 0xfffffffe yields the TID
3028 * of the left pixel of the current pixel's row.
3030 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
3031 * adding 2 yields the TID of the pixel below the top pixel.
3033 /* masks for thread ID. */
3034 #define TID_MASK_TOP_LEFT 0xfffffffc
3035 #define TID_MASK_TOP 0xfffffffd
3036 #define TID_MASK_LEFT 0xfffffffe
3038 static void si_llvm_emit_ddxy(
3039 const struct lp_build_tgsi_action
* action
,
3040 struct lp_build_tgsi_context
* bld_base
,
3041 struct lp_build_emit_data
* emit_data
)
3043 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3044 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3045 struct lp_build_context
* base
= &bld_base
->base
;
3046 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3047 unsigned opcode
= inst
->Instruction
.Opcode
;
3048 LLVMValueRef indices
[2];
3049 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
3050 LLVMValueRef tl
, trbl
, result
[4];
3052 unsigned swizzle
[4];
3057 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3059 indices
[0] = bld_base
->uint_bld
.zero
;
3060 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
3061 NULL
, 0, LLVMReadNoneAttribute
);
3062 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3065 if (opcode
== TGSI_OPCODE_DDX_FINE
)
3066 mask
= TID_MASK_LEFT
;
3067 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
3068 mask
= TID_MASK_TOP
;
3070 mask
= TID_MASK_TOP_LEFT
;
3072 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
3073 lp_build_const_int32(gallivm
, mask
), "");
3074 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3077 /* for DDX we want to next X pixel, DDY next Y pixel. */
3078 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
3079 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
3080 lp_build_const_int32(gallivm
, idx
), "");
3081 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3084 for (c
= 0; c
< 4; ++c
) {
3087 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
3088 for (i
= 0; i
< c
; ++i
) {
3089 if (swizzle
[i
] == swizzle
[c
]) {
3090 result
[c
] = result
[i
];
3097 LLVMBuildStore(gallivm
->builder
,
3098 LLVMBuildBitCast(gallivm
->builder
,
3099 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
3103 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
3104 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3106 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
3107 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
3109 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
3112 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
3116 * this takes an I,J coordinate pair,
3117 * and works out the X and Y derivatives.
3118 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
3120 static LLVMValueRef
si_llvm_emit_ddxy_interp(
3121 struct lp_build_tgsi_context
*bld_base
,
3122 LLVMValueRef interp_ij
)
3124 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3125 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3126 struct lp_build_context
*base
= &bld_base
->base
;
3127 LLVMValueRef indices
[2];
3128 LLVMValueRef store_ptr
, load_ptr_x
, load_ptr_y
, load_ptr_ddx
, load_ptr_ddy
, temp
, temp2
;
3129 LLVMValueRef tl
, tr
, bl
, result
[4];
3133 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3135 indices
[0] = bld_base
->uint_bld
.zero
;
3136 indices
[1] = lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
3137 NULL
, 0, LLVMReadNoneAttribute
);
3138 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3141 temp
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3142 lp_build_const_int32(gallivm
, TID_MASK_LEFT
), "");
3144 temp2
= LLVMBuildAnd(gallivm
->builder
, indices
[1],
3145 lp_build_const_int32(gallivm
, TID_MASK_TOP
), "");
3148 load_ptr_x
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3152 load_ptr_y
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3155 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp
,
3156 lp_build_const_int32(gallivm
, 1), "");
3157 load_ptr_ddx
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3160 indices
[1] = LLVMBuildAdd(gallivm
->builder
, temp2
,
3161 lp_build_const_int32(gallivm
, 2), "");
3162 load_ptr_ddy
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->lds
,
3165 for (c
= 0; c
< 2; ++c
) {
3166 LLVMValueRef store_val
;
3167 LLVMValueRef c_ll
= lp_build_const_int32(gallivm
, c
);
3169 store_val
= LLVMBuildExtractElement(gallivm
->builder
,
3170 interp_ij
, c_ll
, "");
3171 LLVMBuildStore(gallivm
->builder
,
3175 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_x
, "");
3176 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3178 tr
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddx
, "");
3179 tr
= LLVMBuildBitCast(gallivm
->builder
, tr
, base
->elem_type
, "");
3181 result
[c
] = LLVMBuildFSub(gallivm
->builder
, tr
, tl
, "");
3183 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_y
, "");
3184 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
3186 bl
= LLVMBuildLoad(gallivm
->builder
, load_ptr_ddy
, "");
3187 bl
= LLVMBuildBitCast(gallivm
->builder
, bl
, base
->elem_type
, "");
3189 result
[c
+ 2] = LLVMBuildFSub(gallivm
->builder
, bl
, tl
, "");
3192 return lp_build_gather_values(gallivm
, result
, 4);
3195 static void interp_fetch_args(
3196 struct lp_build_tgsi_context
*bld_base
,
3197 struct lp_build_emit_data
*emit_data
)
3199 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3200 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3201 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3203 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
3204 /* offset is in second src, first two channels */
3205 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
3208 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
3211 emit_data
->arg_count
= 2;
3212 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3213 LLVMValueRef sample_position
;
3214 LLVMValueRef sample_id
;
3215 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
3217 /* fetch sample ID, then fetch its sample position,
3218 * and place into first two channels.
3220 sample_id
= lp_build_emit_fetch(bld_base
,
3221 emit_data
->inst
, 1, TGSI_CHAN_X
);
3222 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
3223 LLVMInt32TypeInContext(gallivm
->context
),
3225 sample_position
= load_sample_position(&si_shader_ctx
->radeon_bld
, sample_id
);
3227 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
3229 lp_build_const_int32(gallivm
, 0), "");
3231 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
3232 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
3234 lp_build_const_int32(gallivm
, 1), "");
3235 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
3236 emit_data
->arg_count
= 2;
3240 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
3241 struct lp_build_tgsi_context
*bld_base
,
3242 struct lp_build_emit_data
*emit_data
)
3244 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3245 struct si_shader
*shader
= si_shader_ctx
->shader
;
3246 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3247 LLVMValueRef interp_param
;
3248 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3249 const char *intr_name
;
3250 int input_index
= inst
->Src
[0].Register
.Index
;
3253 LLVMValueRef attr_number
;
3254 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
3255 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
3256 int interp_param_idx
;
3257 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
3260 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
3262 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3263 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
3264 location
= TGSI_INTERPOLATE_LOC_CENTER
;
3266 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
3268 interp_param_idx
= lookup_interp_param_index(interp
, location
);
3269 if (interp_param_idx
== -1)
3271 else if (interp_param_idx
)
3272 interp_param
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, interp_param_idx
);
3274 interp_param
= NULL
;
3276 attr_number
= lp_build_const_int32(gallivm
, input_index
);
3278 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
3279 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
3280 LLVMValueRef ij_out
[2];
3281 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
3284 * take the I then J parameters, and the DDX/Y for it, and
3285 * calculate the IJ inputs for the interpolator.
3286 * temp1 = ddx * offset/sample.x + I;
3287 * interp_param.I = ddy * offset/sample.y + temp1;
3288 * temp1 = ddx * offset/sample.x + J;
3289 * interp_param.J = ddy * offset/sample.y + temp1;
3291 for (i
= 0; i
< 2; i
++) {
3292 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
3293 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
3294 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
3295 ddxy_out
, ix_ll
, "");
3296 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
3297 ddxy_out
, iy_ll
, "");
3298 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
3299 interp_param
, ix_ll
, "");
3300 LLVMValueRef temp1
, temp2
;
3302 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
3303 LLVMFloatTypeInContext(gallivm
->context
), "");
3305 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
3307 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
3309 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
3311 temp2
= LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
3313 ij_out
[i
] = LLVMBuildBitCast(gallivm
->builder
,
3315 LLVMIntTypeInContext(gallivm
->context
, 32), "");
3317 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
3320 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
3321 for (chan
= 0; chan
< 2; chan
++) {
3322 LLVMValueRef args
[4];
3323 LLVMValueRef llvm_chan
;
3326 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
3327 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
3329 args
[0] = llvm_chan
;
3330 args
[1] = attr_number
;
3332 args
[3] = interp_param
;
3334 emit_data
->output
[chan
] =
3335 lp_build_intrinsic(gallivm
->builder
, intr_name
,
3336 input_type
, args
, args
[3] ? 4 : 3,
3337 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
3341 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
3342 struct lp_build_emit_data
*emit_data
)
3344 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
3345 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
3348 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
3350 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
3354 /* Emit one vertex from the geometry shader */
3355 static void si_llvm_emit_vertex(
3356 const struct lp_build_tgsi_action
*action
,
3357 struct lp_build_tgsi_context
*bld_base
,
3358 struct lp_build_emit_data
*emit_data
)
3360 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3361 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3362 struct si_shader
*shader
= si_shader_ctx
->shader
;
3363 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3364 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3365 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
3366 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3367 SI_PARAM_GS2VS_OFFSET
);
3368 LLVMValueRef gs_next_vertex
;
3369 LLVMValueRef can_emit
, kill
;
3370 LLVMValueRef args
[2];
3375 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3377 /* Write vertex attribute values to GSVS ring */
3378 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
3379 si_shader_ctx
->gs_next_vertex
[stream
],
3382 /* If this thread has already emitted the declared maximum number of
3383 * vertices, kill it: excessive vertex emissions are not supposed to
3384 * have any effect, and GS threads have no externally observable
3385 * effects other than emitting vertices.
3387 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
3388 lp_build_const_int32(gallivm
,
3389 shader
->selector
->gs_max_out_vertices
), "");
3390 kill
= lp_build_select(&bld_base
->base
, can_emit
,
3391 lp_build_const_float(gallivm
, 1.0f
),
3392 lp_build_const_float(gallivm
, -1.0f
));
3394 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
3395 LLVMVoidTypeInContext(gallivm
->context
), &kill
, 1, 0);
3397 for (i
= 0; i
< info
->num_outputs
; i
++) {
3398 LLVMValueRef
*out_ptr
=
3399 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
3401 for (chan
= 0; chan
< 4; chan
++) {
3402 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
3403 LLVMValueRef voffset
=
3404 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
3405 shader
->selector
->gs_max_out_vertices
);
3407 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
3408 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
3410 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
3412 build_tbuffer_store(si_shader_ctx
,
3413 si_shader_ctx
->gsvs_ring
[stream
],
3415 voffset
, soffset
, 0,
3416 V_008F0C_BUF_DATA_FORMAT_32
,
3417 V_008F0C_BUF_NUM_FORMAT_UINT
,
3421 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
3422 lp_build_const_int32(gallivm
, 1));
3424 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
[stream
]);
3426 /* Signal vertex emission */
3427 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
3428 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3429 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3430 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3431 LLVMNoUnwindAttribute
);
3434 /* Cut one primitive from the geometry shader */
3435 static void si_llvm_emit_primitive(
3436 const struct lp_build_tgsi_action
*action
,
3437 struct lp_build_tgsi_context
*bld_base
,
3438 struct lp_build_emit_data
*emit_data
)
3440 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
3441 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3442 LLVMValueRef args
[2];
3445 /* Signal primitive cut */
3446 stream
= si_llvm_get_stream(bld_base
, emit_data
);
3447 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
3448 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
3449 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
3450 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
3451 LLVMNoUnwindAttribute
);
3454 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
3455 struct lp_build_tgsi_context
*bld_base
,
3456 struct lp_build_emit_data
*emit_data
)
3458 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3460 lp_build_intrinsic(gallivm
->builder
,
3461 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
3462 : "llvm.AMDGPU.barrier.local",
3463 LLVMVoidTypeInContext(gallivm
->context
), NULL
, 0,
3464 LLVMNoUnwindAttribute
);
3467 static const struct lp_build_tgsi_action tex_action
= {
3468 .fetch_args
= tex_fetch_args
,
3469 .emit
= build_tex_intrinsic
,
3472 static const struct lp_build_tgsi_action interp_action
= {
3473 .fetch_args
= interp_fetch_args
,
3474 .emit
= build_interp_intrinsic
,
3477 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
3479 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3480 LLVMValueRef args
[3];
3482 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
3484 args
[2] = lp_build_const_int32(gallivm
, 1);
3486 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
3489 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3491 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3495 static void declare_streamout_params(struct si_shader_context
*si_shader_ctx
,
3496 struct pipe_stream_output_info
*so
,
3497 LLVMTypeRef
*params
, LLVMTypeRef i32
,
3498 unsigned *num_params
)
3502 /* Streamout SGPRs. */
3503 if (so
->num_outputs
) {
3504 params
[si_shader_ctx
->param_streamout_config
= (*num_params
)++] = i32
;
3505 params
[si_shader_ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
3507 /* A streamout buffer offset is loaded if the stride is non-zero. */
3508 for (i
= 0; i
< 4; i
++) {
3512 params
[si_shader_ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
3516 static void create_function(struct si_shader_context
*si_shader_ctx
)
3518 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3519 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3520 struct si_shader
*shader
= si_shader_ctx
->shader
;
3521 LLVMTypeRef params
[SI_NUM_PARAMS
], f32
, i8
, i32
, v2i32
, v3i32
, v16i8
, v4i32
, v8i32
;
3522 unsigned i
, last_array_pointer
, last_sgpr
, num_params
;
3524 i8
= LLVMInt8TypeInContext(gallivm
->context
);
3525 i32
= LLVMInt32TypeInContext(gallivm
->context
);
3526 f32
= LLVMFloatTypeInContext(gallivm
->context
);
3527 v2i32
= LLVMVectorType(i32
, 2);
3528 v3i32
= LLVMVectorType(i32
, 3);
3529 v4i32
= LLVMVectorType(i32
, 4);
3530 v8i32
= LLVMVectorType(i32
, 8);
3531 v16i8
= LLVMVectorType(i8
, 16);
3533 params
[SI_PARAM_RW_BUFFERS
] = const_array(v16i8
, SI_NUM_RW_BUFFERS
);
3534 params
[SI_PARAM_CONST_BUFFERS
] = const_array(v16i8
, SI_NUM_CONST_BUFFERS
);
3535 params
[SI_PARAM_SAMPLER_STATES
] = const_array(v4i32
, SI_NUM_SAMPLER_STATES
);
3536 params
[SI_PARAM_SAMPLER_VIEWS
] = const_array(v8i32
, SI_NUM_SAMPLER_VIEWS
);
3537 last_array_pointer
= SI_PARAM_SAMPLER_VIEWS
;
3539 switch (si_shader_ctx
->type
) {
3540 case TGSI_PROCESSOR_VERTEX
:
3541 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(v16i8
, SI_NUM_VERTEX_BUFFERS
);
3542 last_array_pointer
= SI_PARAM_VERTEX_BUFFERS
;
3543 params
[SI_PARAM_BASE_VERTEX
] = i32
;
3544 params
[SI_PARAM_START_INSTANCE
] = i32
;
3545 num_params
= SI_PARAM_START_INSTANCE
+1;
3547 if (shader
->key
.vs
.as_es
) {
3548 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3549 } else if (shader
->key
.vs
.as_ls
) {
3550 params
[SI_PARAM_LS_OUT_LAYOUT
] = i32
;
3551 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
3553 if (si_shader_ctx
->is_gs_copy_shader
) {
3554 last_array_pointer
= SI_PARAM_CONST_BUFFERS
;
3555 num_params
= SI_PARAM_CONST_BUFFERS
+1;
3557 params
[SI_PARAM_VS_STATE_BITS
] = i32
;
3558 num_params
= SI_PARAM_VS_STATE_BITS
+1;
3561 /* The locations of the other parameters are assigned dynamically. */
3562 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3563 params
, i32
, &num_params
);
3566 last_sgpr
= num_params
-1;
3569 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
3570 params
[si_shader_ctx
->param_rel_auto_id
= num_params
++] = i32
;
3571 params
[si_shader_ctx
->param_vs_prim_id
= num_params
++] = i32
;
3572 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
3575 case TGSI_PROCESSOR_TESS_CTRL
:
3576 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3577 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3578 params
[SI_PARAM_TCS_IN_LAYOUT
] = i32
;
3579 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = i32
;
3580 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
3583 params
[SI_PARAM_PATCH_ID
] = i32
;
3584 params
[SI_PARAM_REL_IDS
] = i32
;
3585 num_params
= SI_PARAM_REL_IDS
+1;
3588 case TGSI_PROCESSOR_TESS_EVAL
:
3589 params
[SI_PARAM_TCS_OUT_OFFSETS
] = i32
;
3590 params
[SI_PARAM_TCS_OUT_LAYOUT
] = i32
;
3591 num_params
= SI_PARAM_TCS_OUT_LAYOUT
+1;
3593 if (shader
->key
.tes
.as_es
) {
3594 params
[si_shader_ctx
->param_es2gs_offset
= num_params
++] = i32
;
3596 declare_streamout_params(si_shader_ctx
, &shader
->selector
->so
,
3597 params
, i32
, &num_params
);
3599 last_sgpr
= num_params
- 1;
3602 params
[si_shader_ctx
->param_tes_u
= num_params
++] = f32
;
3603 params
[si_shader_ctx
->param_tes_v
= num_params
++] = f32
;
3604 params
[si_shader_ctx
->param_tes_rel_patch_id
= num_params
++] = i32
;
3605 params
[si_shader_ctx
->param_tes_patch_id
= num_params
++] = i32
;
3608 case TGSI_PROCESSOR_GEOMETRY
:
3609 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
3610 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
3611 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
3614 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
3615 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
3616 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
3617 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
3618 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
3619 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
3620 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
3621 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
3622 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
3625 case TGSI_PROCESSOR_FRAGMENT
:
3626 params
[SI_PARAM_ALPHA_REF
] = f32
;
3627 params
[SI_PARAM_PRIM_MASK
] = i32
;
3628 last_sgpr
= SI_PARAM_PRIM_MASK
;
3629 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
3630 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
3631 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
3632 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
3633 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
3634 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
3635 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
3636 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
3637 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
3638 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
3639 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
3640 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
3641 params
[SI_PARAM_FRONT_FACE
] = i32
;
3642 params
[SI_PARAM_ANCILLARY
] = i32
;
3643 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
3644 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
3645 num_params
= SI_PARAM_POS_FIXED_PT
+1;
3649 assert(0 && "unimplemented shader");
3653 assert(num_params
<= Elements(params
));
3654 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
3655 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
3657 for (i
= 0; i
<= last_sgpr
; ++i
) {
3658 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
3660 /* We tell llvm that array inputs are passed by value to allow Sinking pass
3661 * to move load. Inputs are constant so this is fine. */
3662 if (i
<= last_array_pointer
)
3663 LLVMAddAttribute(P
, LLVMByValAttribute
);
3665 LLVMAddAttribute(P
, LLVMInRegAttribute
);
3668 if (bld_base
->info
&&
3669 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
3670 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
3671 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
3672 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
3673 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
3674 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
3675 si_shader_ctx
->lds
=
3676 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3677 LLVMArrayType(i32
, 64),
3681 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&& shader
->key
.vs
.as_ls
) ||
3682 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_CTRL
||
3683 si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
) {
3684 /* This is the upper bound, maximum is 32 inputs times 32 vertices */
3685 unsigned vertex_data_dw_size
= 32*32*4;
3686 unsigned patch_data_dw_size
= 32*4;
3687 /* The formula is: TCS inputs + TCS outputs + TCS patch outputs. */
3688 unsigned patch_dw_size
= vertex_data_dw_size
*2 + patch_data_dw_size
;
3689 unsigned lds_dwords
= patch_dw_size
;
3691 /* The actual size is computed outside of the shader to reduce
3692 * the number of shader variants. */
3693 si_shader_ctx
->lds
=
3694 LLVMAddGlobalInAddressSpace(gallivm
->module
,
3695 LLVMArrayType(i32
, lds_dwords
),
3701 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
3703 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3704 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3705 const struct tgsi_shader_info
* info
= bld_base
->info
;
3707 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST_BUFFERS
);
3709 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
3710 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
3715 /* Allocate space for the constant values */
3716 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
3718 /* Load the resource descriptor */
3719 si_shader_ctx
->const_buffers
[buf
] =
3720 build_indexed_load_const(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
3722 /* Load the constants, we rely on the code sinking to do the rest */
3723 for (i
= 0; i
< num_const
* 4; ++i
) {
3724 si_shader_ctx
->constants
[buf
][i
] =
3725 buffer_load_const(gallivm
->builder
,
3726 si_shader_ctx
->const_buffers
[buf
],
3727 lp_build_const_int32(gallivm
, i
* 4),
3728 bld_base
->base
.elem_type
);
3733 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
3735 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3736 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3737 const struct tgsi_shader_info
* info
= bld_base
->info
;
3739 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
3741 LLVMValueRef res_ptr
, samp_ptr
;
3742 LLVMValueRef offset
;
3744 if (num_samplers
== 0)
3747 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER_VIEWS
);
3748 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER_STATES
);
3750 /* Load the resources and samplers, we rely on the code sinking to do the rest */
3751 for (i
= 0; i
< num_samplers
; ++i
) {
3753 offset
= lp_build_const_int32(gallivm
, i
);
3754 si_shader_ctx
->sampler_views
[i
] = build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3757 offset
= lp_build_const_int32(gallivm
, i
);
3758 si_shader_ctx
->sampler_states
[i
] = build_indexed_load_const(si_shader_ctx
, samp_ptr
, offset
);
3760 /* FMASK resource */
3761 if (info
->is_msaa_sampler
[i
]) {
3762 offset
= lp_build_const_int32(gallivm
, SI_FMASK_TEX_OFFSET
+ i
);
3763 si_shader_ctx
->sampler_views
[SI_FMASK_TEX_OFFSET
+ i
] =
3764 build_indexed_load_const(si_shader_ctx
, res_ptr
, offset
);
3769 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
3771 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
3772 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
3775 /* Streamout can only be used if the shader is compiled as VS. */
3776 if (!si_shader_ctx
->shader
->selector
->so
.num_outputs
||
3777 (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3778 (si_shader_ctx
->shader
->key
.vs
.as_es
||
3779 si_shader_ctx
->shader
->key
.vs
.as_ls
)) ||
3780 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3781 si_shader_ctx
->shader
->key
.tes
.as_es
))
3784 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3785 SI_PARAM_RW_BUFFERS
);
3787 /* Load the resources, we rely on the code sinking to do the rest */
3788 for (i
= 0; i
< 4; ++i
) {
3789 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
3790 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
3791 SI_SO_BUF_OFFSET
+ i
);
3793 si_shader_ctx
->so_buffers
[i
] = build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3799 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
3802 static void preload_ring_buffers(struct si_shader_context
*si_shader_ctx
)
3804 struct gallivm_state
*gallivm
=
3805 si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
3807 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
3808 SI_PARAM_RW_BUFFERS
);
3810 if ((si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
3811 si_shader_ctx
->shader
->key
.vs
.as_es
) ||
3812 (si_shader_ctx
->type
== TGSI_PROCESSOR_TESS_EVAL
&&
3813 si_shader_ctx
->shader
->key
.tes
.as_es
) ||
3814 si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3815 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_ESGS
);
3817 si_shader_ctx
->esgs_ring
=
3818 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3821 if (si_shader_ctx
->is_gs_copy_shader
) {
3822 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
);
3824 si_shader_ctx
->gsvs_ring
[0] =
3825 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3827 if (si_shader_ctx
->type
== TGSI_PROCESSOR_GEOMETRY
) {
3829 for (i
= 0; i
< 4; i
++) {
3830 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_RING_GSVS
+ i
);
3832 si_shader_ctx
->gsvs_ring
[i
] =
3833 build_indexed_load_const(si_shader_ctx
, buf_ptr
, offset
);
3838 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
3839 struct si_shader_config
*conf
,
3840 unsigned symbol_offset
)
3843 const unsigned char *config
=
3844 radeon_shader_binary_config_start(binary
, symbol_offset
);
3846 /* XXX: We may be able to emit some of these values directly rather than
3847 * extracting fields to be emitted later.
3850 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
3851 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
3852 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
3854 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
3855 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
3856 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
3857 case R_00B848_COMPUTE_PGM_RSRC1
:
3858 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
3859 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
3860 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
3861 conf
->rsrc1
= value
;
3863 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
3864 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
3866 case R_00B84C_COMPUTE_PGM_RSRC2
:
3867 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
3868 conf
->rsrc2
= value
;
3870 case R_0286CC_SPI_PS_INPUT_ENA
:
3871 conf
->spi_ps_input_ena
= value
;
3873 case R_0286D0_SPI_PS_INPUT_ADDR
:
3874 conf
->spi_ps_input_addr
= value
;
3876 case R_0286E8_SPI_TMPRING_SIZE
:
3877 case R_00B860_COMPUTE_TMPRING_SIZE
:
3878 /* WAVESIZE is in units of 256 dwords. */
3879 conf
->scratch_bytes_per_wave
=
3880 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
3884 static bool printed
;
3887 fprintf(stderr
, "Warning: LLVM emitted unknown "
3888 "config register: 0x%x\n", reg
);
3895 if (!conf
->spi_ps_input_addr
)
3896 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
3900 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
3901 struct si_shader
*shader
,
3902 uint64_t scratch_va
)
3905 uint32_t scratch_rsrc_dword0
= scratch_va
;
3906 uint32_t scratch_rsrc_dword1
=
3907 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32)
3908 | S_008F04_STRIDE(shader
->config
.scratch_bytes_per_wave
/ 64);
3910 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
3911 const struct radeon_shader_reloc
*reloc
=
3912 &shader
->binary
.relocs
[i
];
3913 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
3914 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3915 &scratch_rsrc_dword0
, 4);
3916 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
3917 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
3918 &scratch_rsrc_dword1
, 4);
3923 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
3925 const struct radeon_shader_binary
*binary
= &shader
->binary
;
3926 unsigned code_size
= binary
->code_size
+ binary
->rodata_size
;
3929 r600_resource_reference(&shader
->bo
, NULL
);
3930 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
,
3931 PIPE_USAGE_IMMUTABLE
,
3936 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
3937 PIPE_TRANSFER_READ_WRITE
);
3938 util_memcpy_cpu_to_le32(ptr
, binary
->code
, binary
->code_size
);
3939 if (binary
->rodata_size
> 0) {
3940 ptr
+= binary
->code_size
;
3941 util_memcpy_cpu_to_le32(ptr
, binary
->rodata
,
3942 binary
->rodata_size
);
3945 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
3949 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
3950 struct pipe_debug_callback
*debug
)
3955 if (binary
->disasm_string
) {
3956 fprintf(stderr
, "\nShader Disassembly:\n\n");
3957 fprintf(stderr
, "%s\n", binary
->disasm_string
);
3959 if (debug
&& debug
->debug_message
) {
3960 /* Very long debug messages are cut off, so send the
3961 * disassembly one line at a time. This causes more
3962 * overhead, but on the plus side it simplifies
3963 * parsing of resulting logs.
3965 pipe_debug_message(debug
, SHADER_INFO
,
3966 "Shader Disassembly Begin");
3968 line
= binary
->disasm_string
;
3970 p
= strchrnul(line
, '\n');
3974 pipe_debug_message(debug
, SHADER_INFO
,
3975 "%.*s", count
, line
);
3983 pipe_debug_message(debug
, SHADER_INFO
,
3984 "Shader Disassembly End");
3987 fprintf(stderr
, "SI CODE:\n");
3988 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
3989 fprintf(stderr
, "@0x%x: %02x%02x%02x%02x\n", i
,
3990 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
3991 binary
->code
[i
+ 1], binary
->code
[i
]);
3996 static void si_shader_dump_stats(struct si_screen
*sscreen
,
3997 struct si_shader_config
*conf
,
3998 unsigned num_inputs
,
4000 struct pipe_debug_callback
*debug
,
4003 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
4004 unsigned lds_per_wave
= 0;
4005 unsigned max_simd_waves
= 10;
4007 /* Compute LDS usage for PS. */
4008 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4009 /* The minimum usage per wave is (num_inputs * 36). The maximum
4010 * usage is (num_inputs * 36 * 16).
4011 * We can get anything in between and it varies between waves.
4013 * Other stages don't know the size at compile time or don't
4014 * allocate LDS per wave, but instead they do it per thread group.
4016 lds_per_wave
= conf
->lds_size
* lds_increment
+
4017 align(num_inputs
* 36, lds_increment
);
4020 /* Compute the per-SIMD wave counts. */
4021 if (conf
->num_sgprs
) {
4022 if (sscreen
->b
.chip_class
>= VI
)
4023 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
4025 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
4028 if (conf
->num_vgprs
)
4029 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
4031 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
4035 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
4037 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
4038 if (processor
== TGSI_PROCESSOR_FRAGMENT
) {
4039 fprintf(stderr
, "*** SHADER CONFIG ***\n"
4040 "SPI_PS_INPUT_ADDR = 0x%04x\n"
4041 "SPI_PS_INPUT_ENA = 0x%04x\n",
4042 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
4045 fprintf(stderr
, "*** SHADER STATS ***\n"
4048 "Code Size: %d bytes\n"
4050 "Scratch: %d bytes per wave\n"
4052 "********************\n",
4053 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4054 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4058 pipe_debug_message(debug
, SHADER_INFO
,
4059 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
4060 "LDS: %d Scratch: %d Max Waves: %d",
4061 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
4062 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
4066 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
4067 struct pipe_debug_callback
*debug
, unsigned processor
)
4069 if (r600_can_dump_shader(&sscreen
->b
, processor
))
4070 if (!(sscreen
->b
.debug_flags
& DBG_NO_ASM
))
4071 si_shader_dump_disassembly(&shader
->binary
, debug
);
4073 si_shader_dump_stats(sscreen
, &shader
->config
,
4074 shader
->selector
? shader
->selector
->info
.num_inputs
: 0,
4075 shader
->binary
.code_size
, debug
, processor
);
4078 int si_compile_llvm(struct si_screen
*sscreen
,
4079 struct radeon_shader_binary
*binary
,
4080 struct si_shader_config
*conf
,
4081 LLVMTargetMachineRef tm
,
4083 struct pipe_debug_callback
*debug
,
4087 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
4089 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
4090 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
4092 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
)))
4093 LLVMDumpModule(mod
);
4096 if (!si_replace_shader(count
, binary
)) {
4097 r
= radeon_llvm_compile(mod
, binary
,
4098 r600_get_llvm_processor_name(sscreen
->b
.family
), tm
,
4104 si_shader_binary_read_config(binary
, conf
, 0);
4106 FREE(binary
->config
);
4107 FREE(binary
->global_symbol_offsets
);
4108 binary
->config
= NULL
;
4109 binary
->global_symbol_offsets
= NULL
;
4113 /* Generate code for the hardware VS shader stage to go with a geometry shader */
4114 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
4115 struct si_shader_context
*si_shader_ctx
,
4116 struct si_shader
*gs
,
4117 struct pipe_debug_callback
*debug
)
4119 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
4120 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
4121 struct lp_build_context
*base
= &bld_base
->base
;
4122 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
4123 struct si_shader_output_values
*outputs
;
4124 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
4125 LLVMValueRef args
[9];
4128 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
4130 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
4131 si_shader_ctx
->is_gs_copy_shader
= true;
4133 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
4135 create_meta_data(si_shader_ctx
);
4136 create_function(si_shader_ctx
);
4137 preload_streamout_buffers(si_shader_ctx
);
4138 preload_ring_buffers(si_shader_ctx
);
4140 args
[0] = si_shader_ctx
->gsvs_ring
[0];
4141 args
[1] = lp_build_mul_imm(uint
,
4142 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
4143 si_shader_ctx
->param_vertex_id
),
4145 args
[3] = uint
->zero
;
4146 args
[4] = uint
->one
; /* OFFEN */
4147 args
[5] = uint
->zero
; /* IDXEN */
4148 args
[6] = uint
->one
; /* GLC */
4149 args
[7] = uint
->one
; /* SLC */
4150 args
[8] = uint
->zero
; /* TFE */
4152 /* Fetch vertex data from GSVS ring */
4153 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
4156 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
4157 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
4159 for (chan
= 0; chan
< 4; chan
++) {
4160 args
[2] = lp_build_const_int32(gallivm
,
4162 gs
->selector
->gs_max_out_vertices
* 16 * 4);
4164 outputs
[i
].values
[chan
] =
4165 LLVMBuildBitCast(gallivm
->builder
,
4166 lp_build_intrinsic(gallivm
->builder
,
4167 "llvm.SI.buffer.load.dword.i32.i32",
4168 LLVMInt32TypeInContext(gallivm
->context
),
4170 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
4171 base
->elem_type
, "");
4175 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
4177 LLVMBuildRetVoid(bld_base
->base
.gallivm
->builder
);
4179 /* Dump LLVM IR before any optimization passes */
4180 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
4181 r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
4182 LLVMDumpModule(bld_base
->base
.gallivm
->module
);
4184 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
4186 if (r600_can_dump_shader(&sscreen
->b
, TGSI_PROCESSOR_GEOMETRY
))
4187 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
4189 r
= si_compile_llvm(sscreen
, &si_shader_ctx
->shader
->binary
,
4190 &si_shader_ctx
->shader
->config
, si_shader_ctx
->tm
,
4191 bld_base
->base
.gallivm
->module
,
4192 debug
, TGSI_PROCESSOR_GEOMETRY
);
4194 si_shader_dump(sscreen
, si_shader_ctx
->shader
, debug
,
4195 TGSI_PROCESSOR_GEOMETRY
);
4196 r
= si_shader_binary_upload(sscreen
, si_shader_ctx
->shader
);
4199 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
4205 void si_dump_shader_key(unsigned shader
, union si_shader_key
*key
, FILE *f
)
4209 fprintf(f
, "SHADER KEY\n");
4212 case PIPE_SHADER_VERTEX
:
4213 fprintf(f
, " instance_divisors = {");
4214 for (i
= 0; i
< Elements(key
->vs
.instance_divisors
); i
++)
4215 fprintf(f
, !i
? "%u" : ", %u",
4216 key
->vs
.instance_divisors
[i
]);
4218 fprintf(f
, " as_es = %u\n", key
->vs
.as_es
);
4219 fprintf(f
, " as_ls = %u\n", key
->vs
.as_ls
);
4220 fprintf(f
, " export_prim_id = %u\n", key
->vs
.export_prim_id
);
4223 case PIPE_SHADER_TESS_CTRL
:
4224 fprintf(f
, " prim_mode = %u\n", key
->tcs
.prim_mode
);
4227 case PIPE_SHADER_TESS_EVAL
:
4228 fprintf(f
, " as_es = %u\n", key
->tes
.as_es
);
4229 fprintf(f
, " export_prim_id = %u\n", key
->tes
.export_prim_id
);
4232 case PIPE_SHADER_GEOMETRY
:
4235 case PIPE_SHADER_FRAGMENT
:
4236 fprintf(f
, " spi_shader_col_format = 0x%x\n", key
->ps
.spi_shader_col_format
);
4237 fprintf(f
, " last_cbuf = %u\n", key
->ps
.last_cbuf
);
4238 fprintf(f
, " color_two_side = %u\n", key
->ps
.color_two_side
);
4239 fprintf(f
, " alpha_func = %u\n", key
->ps
.alpha_func
);
4240 fprintf(f
, " alpha_to_one = %u\n", key
->ps
.alpha_to_one
);
4241 fprintf(f
, " poly_stipple = %u\n", key
->ps
.poly_stipple
);
4242 fprintf(f
, " clamp_color = %u\n", key
->ps
.clamp_color
);
4250 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
4251 struct si_screen
*sscreen
,
4252 struct si_shader
*shader
,
4253 LLVMTargetMachineRef tm
,
4254 struct tgsi_shader_info
*info
)
4256 struct lp_build_tgsi_context
*bld_base
;
4258 memset(ctx
, 0, sizeof(*ctx
));
4259 radeon_llvm_context_init(&ctx
->radeon_bld
);
4261 ctx
->screen
= sscreen
;
4262 if (shader
&& shader
->selector
)
4263 ctx
->type
= shader
->selector
->info
.processor
;
4266 ctx
->shader
= shader
;
4268 bld_base
= &ctx
->radeon_bld
.soa
.bld_base
;
4269 bld_base
->info
= info
;
4270 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
4272 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
4273 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
4274 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
4276 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
4277 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
4278 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
4279 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
4280 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
4281 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
4282 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
4283 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
4284 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
4285 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = tex_action
;
4286 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
4287 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
4288 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
4290 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
4291 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
4292 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
4293 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
4295 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
4296 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
4297 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
4299 if (HAVE_LLVM
>= 0x0306) {
4300 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
4301 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
4302 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
4303 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
4307 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
4308 struct si_shader
*shader
,
4309 struct pipe_debug_callback
*debug
)
4311 struct si_shader_selector
*sel
= shader
->selector
;
4312 struct tgsi_token
*tokens
= sel
->tokens
;
4313 struct si_shader_context si_shader_ctx
;
4314 struct lp_build_tgsi_context
* bld_base
;
4315 struct tgsi_shader_info stipple_shader_info
;
4318 bool poly_stipple
= sel
->type
== PIPE_SHADER_FRAGMENT
&&
4319 shader
->key
.ps
.poly_stipple
;
4322 tokens
= util_pstipple_create_fragment_shader(tokens
, NULL
,
4323 SI_POLY_STIPPLE_SAMPLER
,
4324 TGSI_FILE_SYSTEM_VALUE
);
4325 tgsi_scan_shader(tokens
, &stipple_shader_info
);
4328 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
4329 * conversion fails. */
4330 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
4331 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
4332 si_dump_shader_key(sel
->type
, &shader
->key
, stderr
);
4333 tgsi_dump(tokens
, 0);
4334 si_dump_streamout(&sel
->so
);
4337 si_init_shader_ctx(&si_shader_ctx
, sscreen
, shader
, tm
,
4338 poly_stipple
? &stipple_shader_info
: &sel
->info
);
4340 shader
->uses_instanceid
= sel
->info
.uses_instanceid
;
4342 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
4343 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
4345 switch (si_shader_ctx
.type
) {
4346 case TGSI_PROCESSOR_VERTEX
:
4347 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
4348 if (shader
->key
.vs
.as_ls
)
4349 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
4350 else if (shader
->key
.vs
.as_es
)
4351 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4353 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4355 case TGSI_PROCESSOR_TESS_CTRL
:
4356 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
4357 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
4358 bld_base
->emit_store
= store_output_tcs
;
4359 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
4361 case TGSI_PROCESSOR_TESS_EVAL
:
4362 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
4363 if (shader
->key
.tes
.as_es
)
4364 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
4366 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
4368 case TGSI_PROCESSOR_GEOMETRY
:
4369 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
4370 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
4372 case TGSI_PROCESSOR_FRAGMENT
:
4373 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
4374 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
4377 assert(!"Unsupported shader type");
4381 create_meta_data(&si_shader_ctx
);
4382 create_function(&si_shader_ctx
);
4383 preload_constants(&si_shader_ctx
);
4384 preload_samplers(&si_shader_ctx
);
4385 preload_streamout_buffers(&si_shader_ctx
);
4386 preload_ring_buffers(&si_shader_ctx
);
4388 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4390 for (i
= 0; i
< 4; i
++) {
4391 si_shader_ctx
.gs_next_vertex
[i
] =
4392 lp_build_alloca(bld_base
->base
.gallivm
,
4393 bld_base
->uint_bld
.elem_type
, "");
4397 if (!lp_build_tgsi_llvm(bld_base
, tokens
)) {
4398 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
4402 LLVMBuildRetVoid(bld_base
->base
.gallivm
->builder
);
4403 mod
= bld_base
->base
.gallivm
->module
;
4405 /* Dump LLVM IR before any optimization passes */
4406 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
4407 r600_can_dump_shader(&sscreen
->b
, si_shader_ctx
.type
))
4408 LLVMDumpModule(mod
);
4410 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
4412 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
4413 mod
, debug
, si_shader_ctx
.type
);
4415 fprintf(stderr
, "LLVM failed to compile shader\n");
4419 si_shader_dump(sscreen
, shader
, debug
, si_shader_ctx
.type
);
4421 r
= si_shader_binary_upload(sscreen
, shader
);
4423 fprintf(stderr
, "LLVM failed to upload shader\n");
4427 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
4429 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
4430 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
4431 shader
->gs_copy_shader
->selector
= shader
->selector
;
4432 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
4433 if ((r
= si_generate_gs_copy_shader(sscreen
, &si_shader_ctx
,
4435 free(shader
->gs_copy_shader
);
4436 shader
->gs_copy_shader
= NULL
;
4442 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
4443 FREE(si_shader_ctx
.constants
[i
]);
4445 tgsi_free_tokens(tokens
);
4449 void si_shader_destroy(struct si_shader
*shader
)
4451 if (shader
->gs_copy_shader
) {
4452 si_shader_destroy(shader
->gs_copy_shader
);
4453 FREE(shader
->gs_copy_shader
);
4456 if (shader
->scratch_bo
)
4457 r600_resource_reference(&shader
->scratch_bo
, NULL
);
4459 r600_resource_reference(&shader
->bo
, NULL
);
4461 radeon_shader_binary_clean(&shader
->binary
);