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_flow.h"
35 #include "gallivm/lp_bld_misc.h"
36 #include "radeon/radeon_elf_util.h"
37 #include "util/u_memory.h"
38 #include "util/u_string.h"
39 #include "tgsi/tgsi_build.h"
40 #include "tgsi/tgsi_util.h"
41 #include "tgsi/tgsi_dump.h"
43 #include "ac_llvm_util.h"
44 #include "si_shader_internal.h"
49 static const char *scratch_rsrc_dword0_symbol
=
50 "SCRATCH_RSRC_DWORD0";
52 static const char *scratch_rsrc_dword1_symbol
=
53 "SCRATCH_RSRC_DWORD1";
55 struct si_shader_output_values
57 LLVMValueRef values
[4];
62 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
63 struct si_screen
*sscreen
,
64 struct si_shader
*shader
,
65 LLVMTargetMachineRef tm
);
67 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
68 struct lp_build_tgsi_context
*bld_base
,
69 struct lp_build_emit_data
*emit_data
);
71 static void si_dump_shader_key(unsigned shader
, struct si_shader_key
*key
,
74 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
75 union si_shader_part_key
*key
);
76 static void si_build_vs_epilog_function(struct si_shader_context
*ctx
,
77 union si_shader_part_key
*key
);
78 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
79 union si_shader_part_key
*key
);
80 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
81 union si_shader_part_key
*key
);
82 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
83 union si_shader_part_key
*key
);
85 /* Ideally pass the sample mask input to the PS epilog as v13, which
86 * is its usual location, so that the shader doesn't have to add v_mov.
88 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
90 /* The VS location of the PrimitiveID input is the same in the epilog,
91 * so that the main shader part doesn't have to move it.
93 #define VS_EPILOG_PRIMID_LOC 2
101 #define SENDMSG_GS_DONE 3
103 #define SENDMSG_GS_OP_NOP (0 << 4)
104 #define SENDMSG_GS_OP_CUT (1 << 4)
105 #define SENDMSG_GS_OP_EMIT (2 << 4)
106 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
109 * Returns a unique index for a semantic name and index. The index must be
110 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
113 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
115 switch (semantic_name
) {
116 case TGSI_SEMANTIC_POSITION
:
118 case TGSI_SEMANTIC_PSIZE
:
120 case TGSI_SEMANTIC_CLIPDIST
:
123 case TGSI_SEMANTIC_GENERIC
:
127 assert(!"invalid generic index");
130 /* patch indices are completely separate and thus start from 0 */
131 case TGSI_SEMANTIC_TESSOUTER
:
133 case TGSI_SEMANTIC_TESSINNER
:
135 case TGSI_SEMANTIC_PATCH
:
139 assert(!"invalid semantic name");
144 unsigned si_shader_io_get_unique_index2(unsigned name
, unsigned index
)
147 case TGSI_SEMANTIC_FOG
:
149 case TGSI_SEMANTIC_LAYER
:
151 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
153 case TGSI_SEMANTIC_PRIMID
:
155 case TGSI_SEMANTIC_COLOR
: /* these alias */
156 case TGSI_SEMANTIC_BCOLOR
:
158 case TGSI_SEMANTIC_TEXCOORD
:
161 assert(!"invalid semantic name");
167 * Get the value of a shader input parameter and extract a bitfield.
169 static LLVMValueRef
unpack_param(struct si_shader_context
*ctx
,
170 unsigned param
, unsigned rshift
,
173 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
174 LLVMValueRef value
= LLVMGetParam(ctx
->main_fn
,
177 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
178 value
= bitcast(&ctx
->soa
.bld_base
,
179 TGSI_TYPE_UNSIGNED
, value
);
182 value
= LLVMBuildLShr(gallivm
->builder
, value
,
183 lp_build_const_int32(gallivm
, rshift
), "");
185 if (rshift
+ bitwidth
< 32) {
186 unsigned mask
= (1 << bitwidth
) - 1;
187 value
= LLVMBuildAnd(gallivm
->builder
, value
,
188 lp_build_const_int32(gallivm
, mask
), "");
194 static LLVMValueRef
get_rel_patch_id(struct si_shader_context
*ctx
)
197 case PIPE_SHADER_TESS_CTRL
:
198 return unpack_param(ctx
, SI_PARAM_REL_IDS
, 0, 8);
200 case PIPE_SHADER_TESS_EVAL
:
201 return LLVMGetParam(ctx
->main_fn
,
202 ctx
->param_tes_rel_patch_id
);
210 /* Tessellation shaders pass outputs to the next shader using LDS.
212 * LS outputs = TCS inputs
213 * TCS outputs = TES inputs
216 * - TCS inputs for patch 0
217 * - TCS inputs for patch 1
218 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
220 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
221 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
222 * - TCS outputs for patch 1
223 * - Per-patch TCS outputs for patch 1
224 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
225 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
228 * All three shaders VS(LS), TCS, TES share the same LDS space.
232 get_tcs_in_patch_stride(struct si_shader_context
*ctx
)
234 if (ctx
->type
== PIPE_SHADER_VERTEX
)
235 return unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 0, 13);
236 else if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
237 return unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 0, 13);
245 get_tcs_out_patch_stride(struct si_shader_context
*ctx
)
247 return unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 0, 13);
251 get_tcs_out_patch0_offset(struct si_shader_context
*ctx
)
253 return lp_build_mul_imm(&ctx
->soa
.bld_base
.uint_bld
,
255 SI_PARAM_TCS_OUT_OFFSETS
,
261 get_tcs_out_patch0_patch_data_offset(struct si_shader_context
*ctx
)
263 return lp_build_mul_imm(&ctx
->soa
.bld_base
.uint_bld
,
265 SI_PARAM_TCS_OUT_OFFSETS
,
271 get_tcs_in_current_patch_offset(struct si_shader_context
*ctx
)
273 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
274 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
275 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
277 return LLVMBuildMul(gallivm
->builder
, patch_stride
, rel_patch_id
, "");
281 get_tcs_out_current_patch_offset(struct si_shader_context
*ctx
)
283 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
284 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
285 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
286 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
288 return LLVMBuildAdd(gallivm
->builder
, patch0_offset
,
289 LLVMBuildMul(gallivm
->builder
, patch_stride
,
295 get_tcs_out_current_patch_data_offset(struct si_shader_context
*ctx
)
297 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
298 LLVMValueRef patch0_patch_data_offset
=
299 get_tcs_out_patch0_patch_data_offset(ctx
);
300 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
301 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
303 return LLVMBuildAdd(gallivm
->builder
, patch0_patch_data_offset
,
304 LLVMBuildMul(gallivm
->builder
, patch_stride
,
309 static LLVMValueRef
build_gep0(struct si_shader_context
*ctx
,
310 LLVMValueRef base_ptr
, LLVMValueRef index
)
312 LLVMValueRef indices
[2] = {
313 LLVMConstInt(ctx
->i32
, 0, 0),
316 return LLVMBuildGEP(ctx
->gallivm
.builder
, base_ptr
,
320 static void build_indexed_store(struct si_shader_context
*ctx
,
321 LLVMValueRef base_ptr
, LLVMValueRef index
,
324 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
325 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
327 LLVMBuildStore(gallivm
->builder
, value
,
328 build_gep0(ctx
, base_ptr
, index
));
332 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
333 * It's equivalent to doing a load from &base_ptr[index].
335 * \param base_ptr Where the array starts.
336 * \param index The element index into the array.
337 * \param uniform Whether the base_ptr and index can be assumed to be
338 * dynamically uniform
340 static LLVMValueRef
build_indexed_load(struct si_shader_context
*ctx
,
341 LLVMValueRef base_ptr
, LLVMValueRef index
,
344 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
345 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
346 LLVMValueRef pointer
;
348 pointer
= build_gep0(ctx
, base_ptr
, index
);
350 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
351 return LLVMBuildLoad(gallivm
->builder
, pointer
, "");
355 * Do a load from &base_ptr[index], but also add a flag that it's loading
356 * a constant from a dynamically uniform index.
358 static LLVMValueRef
build_indexed_load_const(
359 struct si_shader_context
*ctx
,
360 LLVMValueRef base_ptr
, LLVMValueRef index
)
362 LLVMValueRef result
= build_indexed_load(ctx
, base_ptr
, index
, true);
363 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
367 static LLVMValueRef
get_instance_index_for_fetch(
368 struct si_shader_context
*radeon_bld
,
369 unsigned param_start_instance
, unsigned divisor
)
371 struct si_shader_context
*ctx
=
372 si_shader_context(&radeon_bld
->soa
.bld_base
);
373 struct gallivm_state
*gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
375 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
376 ctx
->param_instance_id
);
378 /* The division must be done before START_INSTANCE is added. */
380 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
381 lp_build_const_int32(gallivm
, divisor
), "");
383 return LLVMBuildAdd(gallivm
->builder
, result
,
384 LLVMGetParam(radeon_bld
->main_fn
, param_start_instance
), "");
387 static void declare_input_vs(
388 struct si_shader_context
*ctx
,
389 unsigned input_index
,
390 const struct tgsi_full_declaration
*decl
,
393 struct lp_build_context
*base
= &ctx
->soa
.bld_base
.base
;
394 struct gallivm_state
*gallivm
= base
->gallivm
;
399 LLVMValueRef t_list_ptr
;
400 LLVMValueRef t_offset
;
402 LLVMValueRef attribute_offset
;
403 LLVMValueRef buffer_index
;
404 LLVMValueRef args
[3];
407 /* Load the T list */
408 t_list_ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_VERTEX_BUFFERS
);
410 t_offset
= lp_build_const_int32(gallivm
, input_index
);
412 t_list
= build_indexed_load_const(ctx
, t_list_ptr
, t_offset
);
414 /* Build the attribute offset */
415 attribute_offset
= lp_build_const_int32(gallivm
, 0);
417 buffer_index
= LLVMGetParam(ctx
->main_fn
,
418 ctx
->param_vertex_index0
+
422 args
[1] = attribute_offset
;
423 args
[2] = buffer_index
;
424 input
= lp_build_intrinsic(gallivm
->builder
,
425 "llvm.SI.vs.load.input", ctx
->v4f32
, args
, 3,
426 LP_FUNC_ATTR_READNONE
);
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 out
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
432 input
, llvm_chan
, "");
435 fix_fetch
= (ctx
->shader
->key
.mono
.vs
.fix_fetch
>> (2 * input_index
)) & 3;
437 /* The hardware returns an unsigned value; convert it to a
440 LLVMValueRef tmp
= out
[3];
441 LLVMValueRef c30
= LLVMConstInt(ctx
->i32
, 30, 0);
443 /* First, recover the sign-extended signed integer value. */
444 if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
)
445 tmp
= LLVMBuildFPToUI(gallivm
->builder
, tmp
, ctx
->i32
, "");
447 tmp
= LLVMBuildBitCast(gallivm
->builder
, tmp
, ctx
->i32
, "");
449 /* For the integer-like cases, do a natural sign extension.
451 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
452 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
455 tmp
= LLVMBuildShl(gallivm
->builder
, tmp
,
456 fix_fetch
== SI_FIX_FETCH_A2_SNORM
?
457 LLVMConstInt(ctx
->i32
, 7, 0) : c30
, "");
458 tmp
= LLVMBuildAShr(gallivm
->builder
, tmp
, c30
, "");
460 /* Convert back to the right type. */
461 if (fix_fetch
== SI_FIX_FETCH_A2_SNORM
) {
463 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
464 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
465 clamp
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
466 tmp
= LLVMBuildSelect(gallivm
->builder
, clamp
, neg_one
, tmp
, "");
467 } else if (fix_fetch
== SI_FIX_FETCH_A2_SSCALED
) {
468 tmp
= LLVMBuildSIToFP(gallivm
->builder
, tmp
, ctx
->f32
, "");
475 static LLVMValueRef
get_primitive_id(struct lp_build_tgsi_context
*bld_base
,
478 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
481 return bld_base
->uint_bld
.zero
;
484 case PIPE_SHADER_VERTEX
:
485 return LLVMGetParam(ctx
->main_fn
,
486 ctx
->param_vs_prim_id
);
487 case PIPE_SHADER_TESS_CTRL
:
488 return LLVMGetParam(ctx
->main_fn
,
490 case PIPE_SHADER_TESS_EVAL
:
491 return LLVMGetParam(ctx
->main_fn
,
492 ctx
->param_tes_patch_id
);
493 case PIPE_SHADER_GEOMETRY
:
494 return LLVMGetParam(ctx
->main_fn
,
495 SI_PARAM_PRIMITIVE_ID
);
498 return bld_base
->uint_bld
.zero
;
503 * Return the value of tgsi_ind_register for indexing.
504 * This is the indirect index with the constant offset added to it.
506 static LLVMValueRef
get_indirect_index(struct si_shader_context
*ctx
,
507 const struct tgsi_ind_register
*ind
,
510 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
513 result
= ctx
->soa
.addr
[ind
->Index
][ind
->Swizzle
];
514 result
= LLVMBuildLoad(gallivm
->builder
, result
, "");
515 result
= LLVMBuildAdd(gallivm
->builder
, result
,
516 lp_build_const_int32(gallivm
, rel_index
), "");
521 * Like get_indirect_index, but restricts the return value to a (possibly
522 * undefined) value inside [0..num).
524 static LLVMValueRef
get_bounded_indirect_index(struct si_shader_context
*ctx
,
525 const struct tgsi_ind_register
*ind
,
526 int rel_index
, unsigned num
)
528 LLVMValueRef result
= get_indirect_index(ctx
, ind
, rel_index
);
530 /* LLVM 3.8: If indirect resource indexing is used:
534 if (HAVE_LLVM
<= 0x0308)
535 return LLVMGetUndef(ctx
->i32
);
537 return si_llvm_bound_index(ctx
, result
, num
);
542 * Calculate a dword address given an input or output register and a stride.
544 static LLVMValueRef
get_dw_address(struct si_shader_context
*ctx
,
545 const struct tgsi_full_dst_register
*dst
,
546 const struct tgsi_full_src_register
*src
,
547 LLVMValueRef vertex_dw_stride
,
548 LLVMValueRef base_addr
)
550 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
551 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
552 ubyte
*name
, *index
, *array_first
;
554 struct tgsi_full_dst_register reg
;
556 /* Set the register description. The address computation is the same
557 * for sources and destinations. */
559 reg
.Register
.File
= src
->Register
.File
;
560 reg
.Register
.Index
= src
->Register
.Index
;
561 reg
.Register
.Indirect
= src
->Register
.Indirect
;
562 reg
.Register
.Dimension
= src
->Register
.Dimension
;
563 reg
.Indirect
= src
->Indirect
;
564 reg
.Dimension
= src
->Dimension
;
565 reg
.DimIndirect
= src
->DimIndirect
;
569 /* If the register is 2-dimensional (e.g. an array of vertices
570 * in a primitive), calculate the base address of the vertex. */
571 if (reg
.Register
.Dimension
) {
574 if (reg
.Dimension
.Indirect
)
575 index
= get_indirect_index(ctx
, ®
.DimIndirect
,
576 reg
.Dimension
.Index
);
578 index
= lp_build_const_int32(gallivm
, reg
.Dimension
.Index
);
580 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
581 LLVMBuildMul(gallivm
->builder
, index
,
582 vertex_dw_stride
, ""), "");
585 /* Get information about the register. */
586 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
587 name
= info
->input_semantic_name
;
588 index
= info
->input_semantic_index
;
589 array_first
= info
->input_array_first
;
590 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
591 name
= info
->output_semantic_name
;
592 index
= info
->output_semantic_index
;
593 array_first
= info
->output_array_first
;
599 if (reg
.Register
.Indirect
) {
600 /* Add the relative address of the element. */
601 LLVMValueRef ind_index
;
603 if (reg
.Indirect
.ArrayID
)
604 first
= array_first
[reg
.Indirect
.ArrayID
];
606 first
= reg
.Register
.Index
;
608 ind_index
= get_indirect_index(ctx
, ®
.Indirect
,
609 reg
.Register
.Index
- first
);
611 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
612 LLVMBuildMul(gallivm
->builder
, ind_index
,
613 lp_build_const_int32(gallivm
, 4), ""), "");
615 param
= si_shader_io_get_unique_index(name
[first
], index
[first
]);
617 param
= si_shader_io_get_unique_index(name
[reg
.Register
.Index
],
618 index
[reg
.Register
.Index
]);
621 /* Add the base address of the element. */
622 return LLVMBuildAdd(gallivm
->builder
, base_addr
,
623 lp_build_const_int32(gallivm
, param
* 4), "");
626 /* The offchip buffer layout for TCS->TES is
628 * - attribute 0 of patch 0 vertex 0
629 * - attribute 0 of patch 0 vertex 1
630 * - attribute 0 of patch 0 vertex 2
632 * - attribute 0 of patch 1 vertex 0
633 * - attribute 0 of patch 1 vertex 1
635 * - attribute 1 of patch 0 vertex 0
636 * - attribute 1 of patch 0 vertex 1
638 * - per patch attribute 0 of patch 0
639 * - per patch attribute 0 of patch 1
642 * Note that every attribute has 4 components.
644 static LLVMValueRef
get_tcs_tes_buffer_address(struct si_shader_context
*ctx
,
645 LLVMValueRef vertex_index
,
646 LLVMValueRef param_index
)
648 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
649 LLVMValueRef base_addr
, vertices_per_patch
, num_patches
, total_vertices
;
650 LLVMValueRef param_stride
, constant16
;
652 vertices_per_patch
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 6);
653 num_patches
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 0, 9);
654 total_vertices
= LLVMBuildMul(gallivm
->builder
, vertices_per_patch
,
657 constant16
= lp_build_const_int32(gallivm
, 16);
659 base_addr
= LLVMBuildMul(gallivm
->builder
, get_rel_patch_id(ctx
),
660 vertices_per_patch
, "");
662 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
665 param_stride
= total_vertices
;
667 base_addr
= get_rel_patch_id(ctx
);
668 param_stride
= num_patches
;
671 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
672 LLVMBuildMul(gallivm
->builder
, param_index
,
673 param_stride
, ""), "");
675 base_addr
= LLVMBuildMul(gallivm
->builder
, base_addr
, constant16
, "");
678 LLVMValueRef patch_data_offset
=
679 unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 16, 16);
681 base_addr
= LLVMBuildAdd(gallivm
->builder
, base_addr
,
682 patch_data_offset
, "");
687 static LLVMValueRef
get_tcs_tes_buffer_address_from_reg(
688 struct si_shader_context
*ctx
,
689 const struct tgsi_full_dst_register
*dst
,
690 const struct tgsi_full_src_register
*src
)
692 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
693 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
694 ubyte
*name
, *index
, *array_first
;
695 struct tgsi_full_src_register reg
;
696 LLVMValueRef vertex_index
= NULL
;
697 LLVMValueRef param_index
= NULL
;
698 unsigned param_index_base
, param_base
;
700 reg
= src
? *src
: tgsi_full_src_register_from_dst(dst
);
702 if (reg
.Register
.Dimension
) {
704 if (reg
.Dimension
.Indirect
)
705 vertex_index
= get_indirect_index(ctx
, ®
.DimIndirect
,
706 reg
.Dimension
.Index
);
708 vertex_index
= lp_build_const_int32(gallivm
,
709 reg
.Dimension
.Index
);
712 /* Get information about the register. */
713 if (reg
.Register
.File
== TGSI_FILE_INPUT
) {
714 name
= info
->input_semantic_name
;
715 index
= info
->input_semantic_index
;
716 array_first
= info
->input_array_first
;
717 } else if (reg
.Register
.File
== TGSI_FILE_OUTPUT
) {
718 name
= info
->output_semantic_name
;
719 index
= info
->output_semantic_index
;
720 array_first
= info
->output_array_first
;
726 if (reg
.Register
.Indirect
) {
727 if (reg
.Indirect
.ArrayID
)
728 param_base
= array_first
[reg
.Indirect
.ArrayID
];
730 param_base
= reg
.Register
.Index
;
732 param_index
= get_indirect_index(ctx
, ®
.Indirect
,
733 reg
.Register
.Index
- param_base
);
736 param_base
= reg
.Register
.Index
;
737 param_index
= lp_build_const_int32(gallivm
, 0);
740 param_index_base
= si_shader_io_get_unique_index(name
[param_base
],
743 param_index
= LLVMBuildAdd(gallivm
->builder
, param_index
,
744 lp_build_const_int32(gallivm
, param_index_base
),
747 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
750 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
751 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
752 * or v4i32 (num_channels=3,4). */
753 static void build_tbuffer_store(struct si_shader_context
*ctx
,
756 unsigned num_channels
,
758 LLVMValueRef soffset
,
759 unsigned inst_offset
,
768 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
769 LLVMValueRef args
[] = {
772 LLVMConstInt(ctx
->i32
, num_channels
, 0),
775 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
776 LLVMConstInt(ctx
->i32
, dfmt
, 0),
777 LLVMConstInt(ctx
->i32
, nfmt
, 0),
778 LLVMConstInt(ctx
->i32
, offen
, 0),
779 LLVMConstInt(ctx
->i32
, idxen
, 0),
780 LLVMConstInt(ctx
->i32
, glc
, 0),
781 LLVMConstInt(ctx
->i32
, slc
, 0),
782 LLVMConstInt(ctx
->i32
, tfe
, 0)
785 /* The instruction offset field has 12 bits */
786 assert(offen
|| inst_offset
< (1 << 12));
788 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
789 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
790 const char *types
[] = {"i32", "v2i32", "v4i32"};
792 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
794 lp_build_intrinsic(gallivm
->builder
, name
, ctx
->voidt
,
795 args
, ARRAY_SIZE(args
), 0);
798 static void build_tbuffer_store_dwords(struct si_shader_context
*ctx
,
801 unsigned num_channels
,
803 LLVMValueRef soffset
,
804 unsigned inst_offset
)
806 static unsigned dfmt
[] = {
807 V_008F0C_BUF_DATA_FORMAT_32
,
808 V_008F0C_BUF_DATA_FORMAT_32_32
,
809 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
810 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
812 assert(num_channels
>= 1 && num_channels
<= 4);
814 build_tbuffer_store(ctx
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
815 inst_offset
, dfmt
[num_channels
-1],
816 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
819 static LLVMValueRef
build_buffer_load(struct si_shader_context
*ctx
,
823 LLVMValueRef voffset
,
824 LLVMValueRef soffset
,
825 unsigned inst_offset
,
829 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
830 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
832 if (HAVE_LLVM
>= 0x309) {
833 LLVMValueRef args
[] = {
834 LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v4i32
, ""),
835 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
836 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
837 LLVMConstInt(ctx
->i1
, glc
, 0),
838 LLVMConstInt(ctx
->i1
, slc
, 0)
841 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
843 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
847 args
[2] = LLVMBuildAdd(gallivm
->builder
, args
[2], voffset
,
852 args
[2] = LLVMBuildAdd(gallivm
->builder
, args
[2], soffset
,
856 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
859 return lp_build_intrinsic(gallivm
->builder
, name
, types
[func
], args
,
860 ARRAY_SIZE(args
), LP_FUNC_ATTR_READONLY
);
862 LLVMValueRef args
[] = {
863 LLVMBuildBitCast(gallivm
->builder
, rsrc
, ctx
->v16i8
, ""),
864 voffset
? voffset
: vindex
,
866 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
867 LLVMConstInt(ctx
->i32
, voffset
? 1 : 0, 0), // offen
868 LLVMConstInt(ctx
->i32
, vindex
? 1 : 0, 0), //idxen
869 LLVMConstInt(ctx
->i32
, glc
, 0),
870 LLVMConstInt(ctx
->i32
, slc
, 0),
871 LLVMConstInt(ctx
->i32
, 0, 0), // TFE
874 LLVMTypeRef types
[] = {ctx
->i32
, LLVMVectorType(ctx
->i32
, 2),
876 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
877 const char *arg_type
= "i32";
880 if (voffset
&& vindex
) {
881 LLVMValueRef vaddr
[] = {vindex
, voffset
};
884 args
[1] = lp_build_gather_values(gallivm
, vaddr
, 2);
887 snprintf(name
, sizeof(name
), "llvm.SI.buffer.load.dword.%s.%s",
888 type_names
[func
], arg_type
);
890 return lp_build_intrinsic(gallivm
->builder
, name
, types
[func
], args
,
891 ARRAY_SIZE(args
), LP_FUNC_ATTR_READONLY
);
895 static LLVMValueRef
buffer_load(struct lp_build_tgsi_context
*bld_base
,
896 enum tgsi_opcode_type type
, unsigned swizzle
,
897 LLVMValueRef buffer
, LLVMValueRef offset
,
900 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
901 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
902 LLVMValueRef value
, value2
;
903 LLVMTypeRef llvm_type
= tgsi2llvmtype(bld_base
, type
);
904 LLVMTypeRef vec_type
= LLVMVectorType(llvm_type
, 4);
907 value
= build_buffer_load(ctx
, buffer
, 4, NULL
, base
, offset
,
910 return LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
913 if (!tgsi_type_is_64bit(type
)) {
914 value
= build_buffer_load(ctx
, buffer
, 4, NULL
, base
, offset
,
917 value
= LLVMBuildBitCast(gallivm
->builder
, value
, vec_type
, "");
918 return LLVMBuildExtractElement(gallivm
->builder
, value
,
919 lp_build_const_int32(gallivm
, swizzle
), "");
922 value
= build_buffer_load(ctx
, buffer
, 1, NULL
, base
, offset
,
925 value2
= build_buffer_load(ctx
, buffer
, 1, NULL
, base
, offset
,
926 swizzle
* 4 + 4, 1, 0);
928 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
934 * \param type output value type
935 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
936 * \param dw_addr address in dwords
938 static LLVMValueRef
lds_load(struct lp_build_tgsi_context
*bld_base
,
939 enum tgsi_opcode_type type
, unsigned swizzle
,
940 LLVMValueRef dw_addr
)
942 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
943 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
947 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
949 for (unsigned chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++)
950 values
[chan
] = lds_load(bld_base
, type
, chan
, dw_addr
);
952 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
956 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
957 lp_build_const_int32(gallivm
, swizzle
));
959 value
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
, false);
960 if (tgsi_type_is_64bit(type
)) {
962 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
963 lp_build_const_int32(gallivm
, 1));
964 value2
= build_indexed_load(ctx
, ctx
->lds
, dw_addr
, false);
965 return si_llvm_emit_fetch_64bit(bld_base
, type
, value
, value2
);
968 return LLVMBuildBitCast(gallivm
->builder
, value
,
969 tgsi2llvmtype(bld_base
, type
), "");
975 * \param swizzle offset (typically 0..3)
976 * \param dw_addr address in dwords
977 * \param value value to store
979 static void lds_store(struct lp_build_tgsi_context
*bld_base
,
980 unsigned swizzle
, LLVMValueRef dw_addr
,
983 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
984 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
986 dw_addr
= lp_build_add(&bld_base
->uint_bld
, dw_addr
,
987 lp_build_const_int32(gallivm
, swizzle
));
989 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
990 build_indexed_store(ctx
, ctx
->lds
,
994 static LLVMValueRef
fetch_input_tcs(
995 struct lp_build_tgsi_context
*bld_base
,
996 const struct tgsi_full_src_register
*reg
,
997 enum tgsi_opcode_type type
, unsigned swizzle
)
999 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1000 LLVMValueRef dw_addr
, stride
;
1002 stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
1003 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1004 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1006 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
1009 static LLVMValueRef
fetch_output_tcs(
1010 struct lp_build_tgsi_context
*bld_base
,
1011 const struct tgsi_full_src_register
*reg
,
1012 enum tgsi_opcode_type type
, unsigned swizzle
)
1014 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1015 LLVMValueRef dw_addr
, stride
;
1017 if (reg
->Register
.Dimension
) {
1018 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
1019 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1020 dw_addr
= get_dw_address(ctx
, NULL
, reg
, stride
, dw_addr
);
1022 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1023 dw_addr
= get_dw_address(ctx
, NULL
, reg
, NULL
, dw_addr
);
1026 return lds_load(bld_base
, type
, swizzle
, dw_addr
);
1029 static LLVMValueRef
fetch_input_tes(
1030 struct lp_build_tgsi_context
*bld_base
,
1031 const struct tgsi_full_src_register
*reg
,
1032 enum tgsi_opcode_type type
, unsigned swizzle
)
1034 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1035 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1036 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
1038 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1039 SI_PARAM_RW_BUFFERS
);
1040 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
1041 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1043 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1044 addr
= get_tcs_tes_buffer_address_from_reg(ctx
, NULL
, reg
);
1046 return buffer_load(bld_base
, type
, swizzle
, buffer
, base
, addr
);
1049 static void store_output_tcs(struct lp_build_tgsi_context
*bld_base
,
1050 const struct tgsi_full_instruction
*inst
,
1051 const struct tgsi_opcode_info
*info
,
1052 LLVMValueRef dst
[4])
1054 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1055 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1056 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
1057 unsigned chan_index
;
1058 LLVMValueRef dw_addr
, stride
;
1059 LLVMValueRef rw_buffers
, buffer
, base
, buf_addr
;
1060 LLVMValueRef values
[4];
1062 /* Only handle per-patch and per-vertex outputs here.
1063 * Vectors will be lowered to scalars and this function will be called again.
1065 if (reg
->Register
.File
!= TGSI_FILE_OUTPUT
||
1066 (dst
[0] && LLVMGetTypeKind(LLVMTypeOf(dst
[0])) == LLVMVectorTypeKind
)) {
1067 si_llvm_emit_store(bld_base
, inst
, info
, dst
);
1071 if (reg
->Register
.Dimension
) {
1072 stride
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 13, 8);
1073 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1074 dw_addr
= get_dw_address(ctx
, reg
, NULL
, stride
, dw_addr
);
1076 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1077 dw_addr
= get_dw_address(ctx
, reg
, NULL
, NULL
, dw_addr
);
1080 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1081 SI_PARAM_RW_BUFFERS
);
1082 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
1083 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1085 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1086 buf_addr
= get_tcs_tes_buffer_address_from_reg(ctx
, reg
, NULL
);
1089 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan_index
) {
1090 LLVMValueRef value
= dst
[chan_index
];
1092 if (inst
->Instruction
.Saturate
)
1093 value
= si_llvm_saturate(bld_base
, value
);
1095 lds_store(bld_base
, chan_index
, dw_addr
, value
);
1097 value
= LLVMBuildBitCast(gallivm
->builder
, value
, ctx
->i32
, "");
1098 values
[chan_index
] = value
;
1100 if (inst
->Dst
[0].Register
.WriteMask
!= 0xF) {
1101 build_tbuffer_store_dwords(ctx
, buffer
, value
, 1,
1107 if (inst
->Dst
[0].Register
.WriteMask
== 0xF) {
1108 LLVMValueRef value
= lp_build_gather_values(bld_base
->base
.gallivm
,
1110 build_tbuffer_store_dwords(ctx
, buffer
, value
, 4, buf_addr
,
1115 static LLVMValueRef
fetch_input_gs(
1116 struct lp_build_tgsi_context
*bld_base
,
1117 const struct tgsi_full_src_register
*reg
,
1118 enum tgsi_opcode_type type
,
1121 struct lp_build_context
*base
= &bld_base
->base
;
1122 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1123 struct si_shader
*shader
= ctx
->shader
;
1124 struct lp_build_context
*uint
= &ctx
->soa
.bld_base
.uint_bld
;
1125 struct gallivm_state
*gallivm
= base
->gallivm
;
1126 LLVMValueRef vtx_offset
;
1127 LLVMValueRef args
[9];
1128 unsigned vtx_offset_param
;
1129 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1130 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
1131 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
1135 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
)
1136 return get_primitive_id(bld_base
, swizzle
);
1138 if (!reg
->Register
.Dimension
)
1141 if (swizzle
== ~0) {
1142 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
1144 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1145 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
1147 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
1151 /* Get the vertex offset parameter */
1152 vtx_offset_param
= reg
->Dimension
.Index
;
1153 if (vtx_offset_param
< 2) {
1154 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
1156 assert(vtx_offset_param
< 6);
1157 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
1159 vtx_offset
= lp_build_mul_imm(uint
,
1160 LLVMGetParam(ctx
->main_fn
,
1164 param
= si_shader_io_get_unique_index(semantic_name
, semantic_index
);
1165 args
[0] = ctx
->esgs_ring
;
1166 args
[1] = vtx_offset
;
1167 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
) * 256);
1168 args
[3] = uint
->zero
;
1169 args
[4] = uint
->one
; /* OFFEN */
1170 args
[5] = uint
->zero
; /* IDXEN */
1171 args
[6] = uint
->one
; /* GLC */
1172 args
[7] = uint
->zero
; /* SLC */
1173 args
[8] = uint
->zero
; /* TFE */
1175 value
= lp_build_intrinsic(gallivm
->builder
,
1176 "llvm.SI.buffer.load.dword.i32.i32",
1178 LP_FUNC_ATTR_READONLY
);
1179 if (tgsi_type_is_64bit(type
)) {
1180 LLVMValueRef value2
;
1181 args
[2] = lp_build_const_int32(gallivm
, (param
* 4 + swizzle
+ 1) * 256);
1182 value2
= lp_build_intrinsic(gallivm
->builder
,
1183 "llvm.SI.buffer.load.dword.i32.i32",
1185 LP_FUNC_ATTR_READONLY
);
1186 return si_llvm_emit_fetch_64bit(bld_base
, type
,
1189 return LLVMBuildBitCast(gallivm
->builder
,
1191 tgsi2llvmtype(bld_base
, type
), "");
1194 static int lookup_interp_param_index(unsigned interpolate
, unsigned location
)
1196 switch (interpolate
) {
1197 case TGSI_INTERPOLATE_CONSTANT
:
1200 case TGSI_INTERPOLATE_LINEAR
:
1201 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1202 return SI_PARAM_LINEAR_SAMPLE
;
1203 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1204 return SI_PARAM_LINEAR_CENTROID
;
1206 return SI_PARAM_LINEAR_CENTER
;
1208 case TGSI_INTERPOLATE_COLOR
:
1209 case TGSI_INTERPOLATE_PERSPECTIVE
:
1210 if (location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
1211 return SI_PARAM_PERSP_SAMPLE
;
1212 else if (location
== TGSI_INTERPOLATE_LOC_CENTROID
)
1213 return SI_PARAM_PERSP_CENTROID
;
1215 return SI_PARAM_PERSP_CENTER
;
1218 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
1223 static LLVMValueRef
build_fs_interp(
1224 struct lp_build_tgsi_context
*bld_base
,
1225 LLVMValueRef llvm_chan
,
1226 LLVMValueRef attr_number
,
1227 LLVMValueRef params
,
1231 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1232 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1233 LLVMValueRef args
[5];
1235 if (HAVE_LLVM
< 0x0400) {
1237 ij
[0] = LLVMBuildBitCast(gallivm
->builder
, i
, ctx
->i32
, "");
1238 ij
[1] = LLVMBuildBitCast(gallivm
->builder
, j
, ctx
->i32
, "");
1240 args
[0] = llvm_chan
;
1241 args
[1] = attr_number
;
1243 args
[3] = lp_build_gather_values(gallivm
, ij
, 2);
1244 return lp_build_intrinsic(gallivm
->builder
, "llvm.fs.interp",
1246 LP_FUNC_ATTR_READNONE
);
1250 args
[1] = llvm_chan
;
1251 args
[2] = attr_number
;
1254 p1
= lp_build_intrinsic(gallivm
->builder
, "llvm.amdgcn.interp.p1",
1255 ctx
->f32
, args
, 4, LP_FUNC_ATTR_READNONE
);
1259 args
[2] = llvm_chan
;
1260 args
[3] = attr_number
;
1263 return lp_build_intrinsic(gallivm
->builder
, "llvm.amdgcn.interp.p2",
1264 ctx
->f32
, args
, 5, LP_FUNC_ATTR_READNONE
);
1267 static LLVMValueRef
build_fs_interp_mov(
1268 struct lp_build_tgsi_context
*bld_base
,
1269 LLVMValueRef parameter
,
1270 LLVMValueRef llvm_chan
,
1271 LLVMValueRef attr_number
,
1272 LLVMValueRef params
) {
1274 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1275 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1276 LLVMValueRef args
[4];
1277 if (HAVE_LLVM
< 0x0400) {
1278 args
[0] = llvm_chan
;
1279 args
[1] = attr_number
;
1282 return lp_build_intrinsic(gallivm
->builder
,
1283 "llvm.SI.fs.constant",
1285 LP_FUNC_ATTR_READNONE
);
1288 args
[0] = parameter
;
1289 args
[1] = llvm_chan
;
1290 args
[2] = attr_number
;
1293 return lp_build_intrinsic(gallivm
->builder
, "llvm.amdgcn.interp.mov",
1294 ctx
->f32
, args
, 4, LP_FUNC_ATTR_READNONE
);
1298 * Interpolate a fragment shader input.
1300 * @param ctx context
1301 * @param input_index index of the input in hardware
1302 * @param semantic_name TGSI_SEMANTIC_*
1303 * @param semantic_index semantic index
1304 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1305 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1306 * @param interp_param interpolation weights (i,j)
1307 * @param prim_mask SI_PARAM_PRIM_MASK
1308 * @param face SI_PARAM_FRONT_FACE
1309 * @param result the return value (4 components)
1311 static void interp_fs_input(struct si_shader_context
*ctx
,
1312 unsigned input_index
,
1313 unsigned semantic_name
,
1314 unsigned semantic_index
,
1315 unsigned num_interp_inputs
,
1316 unsigned colors_read_mask
,
1317 LLVMValueRef interp_param
,
1318 LLVMValueRef prim_mask
,
1320 LLVMValueRef result
[4])
1322 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
1323 struct lp_build_context
*base
= &bld_base
->base
;
1324 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1325 struct gallivm_state
*gallivm
= base
->gallivm
;
1326 LLVMValueRef attr_number
;
1331 /* fs.constant returns the param from the middle vertex, so it's not
1332 * really useful for flat shading. It's meant to be used for custom
1333 * interpolation (but the intrinsic can't fetch from the other two
1336 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1337 * to do the right thing. The only reason we use fs.constant is that
1338 * fs.interp cannot be used on integers, because they can be equal
1341 * When interp is false we will use fs.constant or for newer llvm,
1342 * amdgcn.interp.mov.
1344 bool interp
= interp_param
!= NULL
;
1346 attr_number
= lp_build_const_int32(gallivm
, input_index
);
1349 interp_param
= LLVMBuildBitCast(gallivm
->builder
, interp_param
,
1350 LLVMVectorType(ctx
->f32
, 2), "");
1352 i
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1354 j
= LLVMBuildExtractElement(gallivm
->builder
, interp_param
,
1358 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1359 ctx
->shader
->key
.part
.ps
.prolog
.color_two_side
) {
1360 LLVMValueRef is_face_positive
;
1361 LLVMValueRef back_attr_number
;
1363 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1364 * otherwise it's at offset "num_inputs".
1366 unsigned back_attr_offset
= num_interp_inputs
;
1367 if (semantic_index
== 1 && colors_read_mask
& 0xf)
1368 back_attr_offset
+= 1;
1370 back_attr_number
= lp_build_const_int32(gallivm
, back_attr_offset
);
1372 is_face_positive
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1373 face
, uint
->zero
, "");
1375 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1376 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1377 LLVMValueRef front
, back
;
1380 front
= build_fs_interp(bld_base
, llvm_chan
,
1381 attr_number
, prim_mask
,
1383 back
= build_fs_interp(bld_base
, llvm_chan
,
1384 back_attr_number
, prim_mask
,
1387 front
= build_fs_interp_mov(bld_base
,
1388 lp_build_const_int32(gallivm
, 2), /* P0 */
1389 llvm_chan
, attr_number
, prim_mask
);
1390 back
= build_fs_interp_mov(bld_base
,
1391 lp_build_const_int32(gallivm
, 2), /* P0 */
1392 llvm_chan
, back_attr_number
, prim_mask
);
1395 result
[chan
] = LLVMBuildSelect(gallivm
->builder
,
1401 } else if (semantic_name
== TGSI_SEMANTIC_FOG
) {
1403 result
[0] = build_fs_interp(bld_base
, uint
->zero
,
1404 attr_number
, prim_mask
, i
, j
);
1406 result
[0] = build_fs_interp_mov(bld_base
, uint
->zero
,
1407 lp_build_const_int32(gallivm
, 2), /* P0 */
1408 attr_number
, prim_mask
);
1411 result
[2] = lp_build_const_float(gallivm
, 0.0f
);
1412 result
[3] = lp_build_const_float(gallivm
, 1.0f
);
1414 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
1415 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
1418 result
[chan
] = build_fs_interp(bld_base
,
1419 llvm_chan
, attr_number
, prim_mask
, i
, j
);
1421 result
[chan
] = build_fs_interp_mov(bld_base
,
1422 lp_build_const_int32(gallivm
, 2), /* P0 */
1423 llvm_chan
, attr_number
, prim_mask
);
1429 static void declare_input_fs(
1430 struct si_shader_context
*radeon_bld
,
1431 unsigned input_index
,
1432 const struct tgsi_full_declaration
*decl
,
1433 LLVMValueRef out
[4])
1435 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
1436 struct si_shader_context
*ctx
=
1437 si_shader_context(&radeon_bld
->soa
.bld_base
);
1438 struct si_shader
*shader
= ctx
->shader
;
1439 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
1440 LLVMValueRef interp_param
= NULL
;
1441 int interp_param_idx
;
1443 /* Get colors from input VGPRs (set by the prolog). */
1444 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
) {
1445 unsigned i
= decl
->Semantic
.Index
;
1446 unsigned colors_read
= shader
->selector
->info
.colors_read
;
1447 unsigned mask
= colors_read
>> (i
* 4);
1448 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1 +
1449 (i
? util_bitcount(colors_read
& 0xf) : 0);
1451 out
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1452 out
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1453 out
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1454 out
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : base
->undef
;
1458 interp_param_idx
= lookup_interp_param_index(decl
->Interp
.Interpolate
,
1459 decl
->Interp
.Location
);
1460 if (interp_param_idx
== -1)
1462 else if (interp_param_idx
) {
1463 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
1466 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
1467 decl
->Interp
.Interpolate
== TGSI_INTERPOLATE_COLOR
&&
1468 ctx
->shader
->key
.part
.ps
.prolog
.flatshade_colors
)
1469 interp_param
= NULL
; /* load the constant color */
1471 interp_fs_input(ctx
, input_index
, decl
->Semantic
.Name
,
1472 decl
->Semantic
.Index
, shader
->selector
->info
.num_inputs
,
1473 shader
->selector
->info
.colors_read
, interp_param
,
1474 LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
),
1475 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
),
1479 static LLVMValueRef
get_sample_id(struct si_shader_context
*radeon_bld
)
1481 return unpack_param(si_shader_context(&radeon_bld
->soa
.bld_base
),
1482 SI_PARAM_ANCILLARY
, 8, 4);
1486 * Set range metadata on an instruction. This can only be used on load and
1487 * call instructions. If you know an instruction can only produce the values
1488 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1489 * \p lo is the minimum value inclusive.
1490 * \p hi is the maximum value exclusive.
1492 static void set_range_metadata(struct si_shader_context
*ctx
,
1493 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1495 LLVMValueRef range_md
, md_args
[2];
1496 LLVMTypeRef type
= LLVMTypeOf(value
);
1497 LLVMContextRef context
= LLVMGetTypeContext(type
);
1499 md_args
[0] = LLVMConstInt(type
, lo
, false);
1500 md_args
[1] = LLVMConstInt(type
, hi
, false);
1501 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1502 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1505 static LLVMValueRef
get_thread_id(struct si_shader_context
*ctx
)
1507 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1510 if (HAVE_LLVM
< 0x0308) {
1511 tid
= lp_build_intrinsic(gallivm
->builder
, "llvm.SI.tid",
1512 ctx
->i32
, NULL
, 0, LP_FUNC_ATTR_READNONE
);
1514 LLVMValueRef tid_args
[2];
1515 tid_args
[0] = lp_build_const_int32(gallivm
, 0xffffffff);
1516 tid_args
[1] = lp_build_const_int32(gallivm
, 0);
1517 tid_args
[1] = lp_build_intrinsic(gallivm
->builder
,
1518 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1519 tid_args
, 2, LP_FUNC_ATTR_READNONE
);
1521 tid
= lp_build_intrinsic(gallivm
->builder
,
1522 "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
1523 tid_args
, 2, LP_FUNC_ATTR_READNONE
);
1525 set_range_metadata(ctx
, tid
, 0, 64);
1530 * Load a dword from a constant buffer.
1532 static LLVMValueRef
buffer_load_const(struct si_shader_context
*ctx
,
1533 LLVMValueRef resource
,
1534 LLVMValueRef offset
)
1536 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
1537 LLVMValueRef args
[2] = {resource
, offset
};
1539 return lp_build_intrinsic(builder
, "llvm.SI.load.const", ctx
->f32
, args
, 2,
1540 LP_FUNC_ATTR_READNONE
);
1543 static LLVMValueRef
load_sample_position(struct si_shader_context
*radeon_bld
, LLVMValueRef sample_id
)
1545 struct si_shader_context
*ctx
=
1546 si_shader_context(&radeon_bld
->soa
.bld_base
);
1547 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
1548 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1549 LLVMBuilderRef builder
= gallivm
->builder
;
1550 LLVMValueRef desc
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1551 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_PS_CONST_SAMPLE_POSITIONS
);
1552 LLVMValueRef resource
= build_indexed_load_const(ctx
, desc
, buf_index
);
1554 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1555 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, sample_id
, 8);
1556 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
1558 LLVMValueRef pos
[4] = {
1559 buffer_load_const(ctx
, resource
, offset0
),
1560 buffer_load_const(ctx
, resource
, offset1
),
1561 lp_build_const_float(gallivm
, 0),
1562 lp_build_const_float(gallivm
, 0)
1565 return lp_build_gather_values(gallivm
, pos
, 4);
1568 static void declare_system_value(
1569 struct si_shader_context
*radeon_bld
,
1571 const struct tgsi_full_declaration
*decl
)
1573 struct si_shader_context
*ctx
=
1574 si_shader_context(&radeon_bld
->soa
.bld_base
);
1575 struct lp_build_context
*bld
= &radeon_bld
->soa
.bld_base
.base
;
1576 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1577 LLVMValueRef value
= 0;
1579 switch (decl
->Semantic
.Name
) {
1580 case TGSI_SEMANTIC_INSTANCEID
:
1581 value
= LLVMGetParam(radeon_bld
->main_fn
,
1582 ctx
->param_instance_id
);
1585 case TGSI_SEMANTIC_VERTEXID
:
1586 value
= LLVMBuildAdd(gallivm
->builder
,
1587 LLVMGetParam(radeon_bld
->main_fn
,
1588 ctx
->param_vertex_id
),
1589 LLVMGetParam(radeon_bld
->main_fn
,
1590 SI_PARAM_BASE_VERTEX
), "");
1593 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
1594 value
= LLVMGetParam(radeon_bld
->main_fn
,
1595 ctx
->param_vertex_id
);
1598 case TGSI_SEMANTIC_BASEVERTEX
:
1599 value
= LLVMGetParam(radeon_bld
->main_fn
,
1600 SI_PARAM_BASE_VERTEX
);
1603 case TGSI_SEMANTIC_BASEINSTANCE
:
1604 value
= LLVMGetParam(radeon_bld
->main_fn
,
1605 SI_PARAM_START_INSTANCE
);
1608 case TGSI_SEMANTIC_DRAWID
:
1609 value
= LLVMGetParam(radeon_bld
->main_fn
,
1613 case TGSI_SEMANTIC_INVOCATIONID
:
1614 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1615 value
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
1616 else if (ctx
->type
== PIPE_SHADER_GEOMETRY
)
1617 value
= LLVMGetParam(radeon_bld
->main_fn
,
1618 SI_PARAM_GS_INSTANCE_ID
);
1620 assert(!"INVOCATIONID not implemented");
1623 case TGSI_SEMANTIC_POSITION
:
1625 LLVMValueRef pos
[4] = {
1626 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1627 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1628 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Z_FLOAT
),
1629 lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
, TGSI_OPCODE_RCP
,
1630 LLVMGetParam(radeon_bld
->main_fn
,
1631 SI_PARAM_POS_W_FLOAT
)),
1633 value
= lp_build_gather_values(gallivm
, pos
, 4);
1637 case TGSI_SEMANTIC_FACE
:
1638 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_FRONT_FACE
);
1641 case TGSI_SEMANTIC_SAMPLEID
:
1642 value
= get_sample_id(radeon_bld
);
1645 case TGSI_SEMANTIC_SAMPLEPOS
: {
1646 LLVMValueRef pos
[4] = {
1647 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_X_FLOAT
),
1648 LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_POS_Y_FLOAT
),
1649 lp_build_const_float(gallivm
, 0),
1650 lp_build_const_float(gallivm
, 0)
1652 pos
[0] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1653 TGSI_OPCODE_FRC
, pos
[0]);
1654 pos
[1] = lp_build_emit_llvm_unary(&radeon_bld
->soa
.bld_base
,
1655 TGSI_OPCODE_FRC
, pos
[1]);
1656 value
= lp_build_gather_values(gallivm
, pos
, 4);
1660 case TGSI_SEMANTIC_SAMPLEMASK
:
1661 /* This can only occur with the OpenGL Core profile, which
1662 * doesn't support smoothing.
1664 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_SAMPLE_COVERAGE
);
1667 case TGSI_SEMANTIC_TESSCOORD
:
1669 LLVMValueRef coord
[4] = {
1670 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_u
),
1671 LLVMGetParam(radeon_bld
->main_fn
, ctx
->param_tes_v
),
1676 /* For triangles, the vector should be (u, v, 1-u-v). */
1677 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
] ==
1678 PIPE_PRIM_TRIANGLES
)
1679 coord
[2] = lp_build_sub(bld
, bld
->one
,
1680 lp_build_add(bld
, coord
[0], coord
[1]));
1682 value
= lp_build_gather_values(gallivm
, coord
, 4);
1686 case TGSI_SEMANTIC_VERTICESIN
:
1687 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
)
1688 value
= unpack_param(ctx
, SI_PARAM_TCS_OUT_LAYOUT
, 26, 6);
1689 else if (ctx
->type
== PIPE_SHADER_TESS_EVAL
)
1690 value
= unpack_param(ctx
, SI_PARAM_TCS_OFFCHIP_LAYOUT
, 9, 7);
1692 assert(!"invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
1695 case TGSI_SEMANTIC_TESSINNER
:
1696 case TGSI_SEMANTIC_TESSOUTER
:
1698 LLVMValueRef rw_buffers
, buffer
, base
, addr
;
1699 int param
= si_shader_io_get_unique_index(decl
->Semantic
.Name
, 0);
1701 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
1702 SI_PARAM_RW_BUFFERS
);
1703 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
1704 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
1706 base
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
1707 addr
= get_tcs_tes_buffer_address(ctx
, NULL
,
1708 lp_build_const_int32(gallivm
, param
));
1710 value
= buffer_load(&radeon_bld
->soa
.bld_base
, TGSI_TYPE_FLOAT
,
1711 ~0, buffer
, base
, addr
);
1716 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
:
1717 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
:
1719 LLVMValueRef buf
, slot
, val
[4];
1722 slot
= lp_build_const_int32(gallivm
, SI_HS_CONST_DEFAULT_TESS_LEVELS
);
1723 buf
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
1724 buf
= build_indexed_load_const(ctx
, buf
, slot
);
1725 offset
= decl
->Semantic
.Name
== TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
? 4 : 0;
1727 for (i
= 0; i
< 4; i
++)
1728 val
[i
] = buffer_load_const(ctx
, buf
,
1729 lp_build_const_int32(gallivm
, (offset
+ i
) * 4));
1730 value
= lp_build_gather_values(gallivm
, val
, 4);
1734 case TGSI_SEMANTIC_PRIMID
:
1735 value
= get_primitive_id(&radeon_bld
->soa
.bld_base
, 0);
1738 case TGSI_SEMANTIC_GRID_SIZE
:
1739 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_GRID_SIZE
);
1742 case TGSI_SEMANTIC_BLOCK_SIZE
:
1744 LLVMValueRef values
[3];
1746 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
1748 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
1749 unsigned sizes
[3] = {
1750 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
1751 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
1752 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
1755 for (i
= 0; i
< 3; ++i
)
1756 values
[i
] = lp_build_const_int32(gallivm
, sizes
[i
]);
1758 value
= lp_build_gather_values(gallivm
, values
, 3);
1760 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_BLOCK_SIZE
);
1765 case TGSI_SEMANTIC_BLOCK_ID
:
1766 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_BLOCK_ID
);
1769 case TGSI_SEMANTIC_THREAD_ID
:
1770 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_THREAD_ID
);
1773 #if HAVE_LLVM >= 0x0309
1774 case TGSI_SEMANTIC_HELPER_INVOCATION
:
1775 value
= lp_build_intrinsic(gallivm
->builder
,
1776 "llvm.amdgcn.ps.live",
1778 LP_FUNC_ATTR_READNONE
);
1779 value
= LLVMBuildNot(gallivm
->builder
, value
, "");
1780 value
= LLVMBuildSExt(gallivm
->builder
, value
, ctx
->i32
, "");
1785 assert(!"unknown system value");
1789 radeon_bld
->system_values
[index
] = value
;
1792 static void declare_compute_memory(struct si_shader_context
*radeon_bld
,
1793 const struct tgsi_full_declaration
*decl
)
1795 struct si_shader_context
*ctx
=
1796 si_shader_context(&radeon_bld
->soa
.bld_base
);
1797 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
1798 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
1800 LLVMTypeRef i8p
= LLVMPointerType(ctx
->i8
, LOCAL_ADDR_SPACE
);
1803 assert(decl
->Declaration
.MemType
== TGSI_MEMORY_TYPE_SHARED
);
1804 assert(decl
->Range
.First
== decl
->Range
.Last
);
1805 assert(!ctx
->shared_memory
);
1807 var
= LLVMAddGlobalInAddressSpace(gallivm
->module
,
1808 LLVMArrayType(ctx
->i8
, sel
->local_size
),
1811 LLVMSetAlignment(var
, 4);
1813 ctx
->shared_memory
= LLVMBuildBitCast(gallivm
->builder
, var
, i8p
, "");
1816 static LLVMValueRef
load_const_buffer_desc(struct si_shader_context
*ctx
, int i
)
1818 LLVMValueRef list_ptr
= LLVMGetParam(ctx
->main_fn
,
1819 SI_PARAM_CONST_BUFFERS
);
1821 return build_indexed_load_const(ctx
, list_ptr
,
1822 LLVMConstInt(ctx
->i32
, i
, 0));
1825 static LLVMValueRef
fetch_constant(
1826 struct lp_build_tgsi_context
*bld_base
,
1827 const struct tgsi_full_src_register
*reg
,
1828 enum tgsi_opcode_type type
,
1831 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1832 struct lp_build_context
*base
= &bld_base
->base
;
1833 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
1836 LLVMValueRef addr
, bufp
;
1837 LLVMValueRef result
;
1839 if (swizzle
== LP_CHAN_ALL
) {
1841 LLVMValueRef values
[4];
1842 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
1843 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
1845 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
1848 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
1849 idx
= reg
->Register
.Index
* 4 + swizzle
;
1851 if (reg
->Register
.Dimension
&& reg
->Dimension
.Indirect
) {
1852 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_CONST_BUFFERS
);
1854 index
= get_bounded_indirect_index(ctx
, ®
->DimIndirect
,
1855 reg
->Dimension
.Index
,
1856 SI_NUM_CONST_BUFFERS
);
1857 bufp
= build_indexed_load_const(ctx
, ptr
, index
);
1859 bufp
= load_const_buffer_desc(ctx
, buf
);
1861 if (reg
->Register
.Indirect
) {
1862 addr
= ctx
->soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
1863 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
1864 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
1865 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
1866 lp_build_const_int32(base
->gallivm
, idx
* 4));
1868 addr
= LLVMConstInt(ctx
->i32
, idx
* 4, 0);
1871 result
= buffer_load_const(ctx
, bufp
, addr
);
1873 if (!tgsi_type_is_64bit(type
))
1874 result
= bitcast(bld_base
, type
, result
);
1876 LLVMValueRef addr2
, result2
;
1878 addr2
= lp_build_add(&bld_base
->uint_bld
, addr
,
1879 LLVMConstInt(ctx
->i32
, 4, 0));
1880 result2
= buffer_load_const(ctx
, bufp
, addr2
);
1882 result
= si_llvm_emit_fetch_64bit(bld_base
, type
,
1888 /* Upper 16 bits must be zero. */
1889 static LLVMValueRef
si_llvm_pack_two_int16(struct gallivm_state
*gallivm
,
1890 LLVMValueRef val
[2])
1892 return LLVMBuildOr(gallivm
->builder
, val
[0],
1893 LLVMBuildShl(gallivm
->builder
, val
[1],
1894 lp_build_const_int32(gallivm
, 16),
1898 /* Upper 16 bits are ignored and will be dropped. */
1899 static LLVMValueRef
si_llvm_pack_two_int32_as_int16(struct gallivm_state
*gallivm
,
1900 LLVMValueRef val
[2])
1902 LLVMValueRef v
[2] = {
1903 LLVMBuildAnd(gallivm
->builder
, val
[0],
1904 lp_build_const_int32(gallivm
, 0xffff), ""),
1907 return si_llvm_pack_two_int16(gallivm
, v
);
1910 /* Initialize arguments for the shader export intrinsic */
1911 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
1912 LLVMValueRef
*values
,
1916 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1917 struct lp_build_context
*uint
=
1918 &ctx
->soa
.bld_base
.uint_bld
;
1919 struct lp_build_context
*base
= &bld_base
->base
;
1920 struct gallivm_state
*gallivm
= base
->gallivm
;
1921 LLVMBuilderRef builder
= base
->gallivm
->builder
;
1922 LLVMValueRef val
[4];
1923 unsigned spi_shader_col_format
= V_028714_SPI_SHADER_32_ABGR
;
1927 /* Default is 0xf. Adjusted below depending on the format. */
1928 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1930 /* Specify whether the EXEC mask represents the valid mask */
1931 args
[1] = uint
->zero
;
1933 /* Specify whether this is the last export */
1934 args
[2] = uint
->zero
;
1936 /* Specify the target we are exporting */
1937 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
1939 if (ctx
->type
== PIPE_SHADER_FRAGMENT
) {
1940 const struct si_shader_key
*key
= &ctx
->shader
->key
;
1941 unsigned col_formats
= key
->part
.ps
.epilog
.spi_shader_col_format
;
1942 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
1944 assert(cbuf
>= 0 && cbuf
< 8);
1945 spi_shader_col_format
= (col_formats
>> (cbuf
* 4)) & 0xf;
1946 is_int8
= (key
->part
.ps
.epilog
.color_is_int8
>> cbuf
) & 0x1;
1949 args
[4] = uint
->zero
; /* COMPR flag */
1950 args
[5] = base
->undef
;
1951 args
[6] = base
->undef
;
1952 args
[7] = base
->undef
;
1953 args
[8] = base
->undef
;
1955 switch (spi_shader_col_format
) {
1956 case V_028714_SPI_SHADER_ZERO
:
1957 args
[0] = uint
->zero
; /* writemask */
1958 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
1961 case V_028714_SPI_SHADER_32_R
:
1962 args
[0] = uint
->one
; /* writemask */
1963 args
[5] = values
[0];
1966 case V_028714_SPI_SHADER_32_GR
:
1967 args
[0] = lp_build_const_int32(base
->gallivm
, 0x3); /* writemask */
1968 args
[5] = values
[0];
1969 args
[6] = values
[1];
1972 case V_028714_SPI_SHADER_32_AR
:
1973 args
[0] = lp_build_const_int32(base
->gallivm
, 0x9); /* writemask */
1974 args
[5] = values
[0];
1975 args
[8] = values
[3];
1978 case V_028714_SPI_SHADER_FP16_ABGR
:
1979 args
[4] = uint
->one
; /* COMPR flag */
1981 for (chan
= 0; chan
< 2; chan
++) {
1982 LLVMValueRef pack_args
[2] = {
1984 values
[2 * chan
+ 1]
1986 LLVMValueRef packed
;
1988 packed
= lp_build_intrinsic(base
->gallivm
->builder
,
1990 ctx
->i32
, pack_args
, 2,
1991 LP_FUNC_ATTR_READNONE
);
1993 LLVMBuildBitCast(base
->gallivm
->builder
,
1994 packed
, ctx
->f32
, "");
1998 case V_028714_SPI_SHADER_UNORM16_ABGR
:
1999 for (chan
= 0; chan
< 4; chan
++) {
2000 val
[chan
] = si_llvm_saturate(bld_base
, values
[chan
]);
2001 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
2002 lp_build_const_float(gallivm
, 65535), "");
2003 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
2004 lp_build_const_float(gallivm
, 0.5), "");
2005 val
[chan
] = LLVMBuildFPToUI(builder
, val
[chan
],
2009 args
[4] = uint
->one
; /* COMPR flag */
2010 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2011 si_llvm_pack_two_int16(gallivm
, val
));
2012 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2013 si_llvm_pack_two_int16(gallivm
, val
+2));
2016 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2017 for (chan
= 0; chan
< 4; chan
++) {
2018 /* Clamp between [-1, 1]. */
2019 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MIN
,
2021 lp_build_const_float(gallivm
, 1));
2022 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_MAX
,
2024 lp_build_const_float(gallivm
, -1));
2025 /* Convert to a signed integer in [-32767, 32767]. */
2026 val
[chan
] = LLVMBuildFMul(builder
, val
[chan
],
2027 lp_build_const_float(gallivm
, 32767), "");
2028 /* If positive, add 0.5, else add -0.5. */
2029 val
[chan
] = LLVMBuildFAdd(builder
, val
[chan
],
2030 LLVMBuildSelect(builder
,
2031 LLVMBuildFCmp(builder
, LLVMRealOGE
,
2032 val
[chan
], base
->zero
, ""),
2033 lp_build_const_float(gallivm
, 0.5),
2034 lp_build_const_float(gallivm
, -0.5), ""), "");
2035 val
[chan
] = LLVMBuildFPToSI(builder
, val
[chan
], ctx
->i32
, "");
2038 args
[4] = uint
->one
; /* COMPR flag */
2039 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2040 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
2041 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2042 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
2045 case V_028714_SPI_SHADER_UINT16_ABGR
: {
2046 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
2049 for (chan
= 0; chan
< 4; chan
++) {
2050 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
2051 val
[chan
] = lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_UMIN
,
2055 args
[4] = uint
->one
; /* COMPR flag */
2056 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2057 si_llvm_pack_two_int16(gallivm
, val
));
2058 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2059 si_llvm_pack_two_int16(gallivm
, val
+2));
2063 case V_028714_SPI_SHADER_SINT16_ABGR
: {
2064 LLVMValueRef max
= lp_build_const_int32(gallivm
, is_int8
?
2066 LLVMValueRef min
= lp_build_const_int32(gallivm
, is_int8
?
2069 for (chan
= 0; chan
< 4; chan
++) {
2070 val
[chan
] = bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, values
[chan
]);
2071 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2074 val
[chan
] = lp_build_emit_llvm_binary(bld_base
,
2079 args
[4] = uint
->one
; /* COMPR flag */
2080 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2081 si_llvm_pack_two_int32_as_int16(gallivm
, val
));
2082 args
[6] = bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2083 si_llvm_pack_two_int32_as_int16(gallivm
, val
+2));
2087 case V_028714_SPI_SHADER_32_ABGR
:
2088 memcpy(&args
[5], values
, sizeof(values
[0]) * 4);
2093 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
2096 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2097 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2099 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_NEVER
) {
2100 LLVMValueRef alpha_ref
= LLVMGetParam(ctx
->main_fn
,
2101 SI_PARAM_ALPHA_REF
);
2103 LLVMValueRef alpha_pass
=
2104 lp_build_cmp(&bld_base
->base
,
2105 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
,
2108 lp_build_select(&bld_base
->base
,
2110 lp_build_const_float(gallivm
, 1.0f
),
2111 lp_build_const_float(gallivm
, -1.0f
));
2113 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
2114 ctx
->voidt
, &arg
, 1, 0);
2116 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kilp",
2117 ctx
->voidt
, NULL
, 0, 0);
2121 static LLVMValueRef
si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context
*bld_base
,
2123 unsigned samplemask_param
)
2125 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2126 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2127 LLVMValueRef coverage
;
2129 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2130 coverage
= LLVMGetParam(ctx
->main_fn
,
2132 coverage
= bitcast(bld_base
, TGSI_TYPE_SIGNED
, coverage
);
2134 coverage
= lp_build_intrinsic(gallivm
->builder
, "llvm.ctpop.i32",
2136 &coverage
, 1, LP_FUNC_ATTR_READNONE
);
2138 coverage
= LLVMBuildUIToFP(gallivm
->builder
, coverage
,
2141 coverage
= LLVMBuildFMul(gallivm
->builder
, coverage
,
2142 lp_build_const_float(gallivm
,
2143 1.0 / SI_NUM_SMOOTH_AA_SAMPLES
), "");
2145 return LLVMBuildFMul(gallivm
->builder
, alpha
, coverage
, "");
2148 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
*bld_base
,
2149 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
2151 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2152 struct lp_build_context
*base
= &bld_base
->base
;
2153 struct lp_build_context
*uint
= &ctx
->soa
.bld_base
.uint_bld
;
2156 unsigned const_chan
;
2157 LLVMValueRef base_elt
;
2158 LLVMValueRef ptr
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
2159 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
,
2160 SI_VS_CONST_CLIP_PLANES
);
2161 LLVMValueRef const_resource
= build_indexed_load_const(ctx
, ptr
, constbuf_index
);
2163 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
2164 LLVMValueRef
*args
= pos
[2 + reg_index
];
2169 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
2171 /* Compute dot products of position and user clip plane vectors */
2172 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
2173 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
2174 args
[1] = lp_build_const_int32(base
->gallivm
,
2175 ((reg_index
* 4 + chan
) * 4 +
2177 base_elt
= buffer_load_const(ctx
, const_resource
,
2180 lp_build_add(base
, args
[5 + chan
],
2181 lp_build_mul(base
, base_elt
,
2182 out_elts
[const_chan
]));
2186 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
2187 args
[1] = uint
->zero
;
2188 args
[2] = uint
->zero
;
2189 args
[3] = lp_build_const_int32(base
->gallivm
,
2190 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
2191 args
[4] = uint
->zero
;
2195 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
2199 if (so
->num_outputs
)
2200 fprintf(stderr
, "STREAMOUT\n");
2202 for (i
= 0; i
< so
->num_outputs
; i
++) {
2203 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
2204 so
->output
[i
].start_component
;
2205 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2206 i
, so
->output
[i
].output_buffer
,
2207 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
2208 so
->output
[i
].register_index
,
2209 mask
& 1 ? "x" : "",
2210 mask
& 2 ? "y" : "",
2211 mask
& 4 ? "z" : "",
2212 mask
& 8 ? "w" : "");
2216 /* On SI, the vertex shader is responsible for writing streamout data
2218 static void si_llvm_emit_streamout(struct si_shader_context
*ctx
,
2219 struct si_shader_output_values
*outputs
,
2222 struct pipe_stream_output_info
*so
= &ctx
->shader
->selector
->so
;
2223 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
2224 LLVMBuilderRef builder
= gallivm
->builder
;
2226 struct lp_build_if_state if_ctx
;
2227 LLVMValueRef so_buffers
[4];
2228 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
2229 SI_PARAM_RW_BUFFERS
);
2231 /* Load the descriptors. */
2232 for (i
= 0; i
< 4; ++i
) {
2233 if (ctx
->shader
->selector
->so
.stride
[i
]) {
2234 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
2235 SI_VS_STREAMOUT_BUF0
+ i
);
2237 so_buffers
[i
] = build_indexed_load_const(ctx
, buf_ptr
, offset
);
2241 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2242 LLVMValueRef so_vtx_count
=
2243 unpack_param(ctx
, ctx
->param_streamout_config
, 16, 7);
2245 LLVMValueRef tid
= get_thread_id(ctx
);
2247 /* can_emit = tid < so_vtx_count; */
2248 LLVMValueRef can_emit
=
2249 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
2251 LLVMValueRef stream_id
=
2252 unpack_param(ctx
, ctx
->param_streamout_config
, 24, 2);
2254 /* Emit the streamout code conditionally. This actually avoids
2255 * out-of-bounds buffer access. The hw tells us via the SGPR
2256 * (so_vtx_count) which threads are allowed to emit streamout data. */
2257 lp_build_if(&if_ctx
, gallivm
, can_emit
);
2259 /* The buffer offset is computed as follows:
2260 * ByteOffset = streamout_offset[buffer_id]*4 +
2261 * (streamout_write_index + thread_id)*stride[buffer_id] +
2265 LLVMValueRef so_write_index
=
2266 LLVMGetParam(ctx
->main_fn
,
2267 ctx
->param_streamout_write_index
);
2269 /* Compute (streamout_write_index + thread_id). */
2270 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
2272 /* Compute the write offset for each enabled buffer. */
2273 LLVMValueRef so_write_offset
[4] = {};
2274 for (i
= 0; i
< 4; i
++) {
2278 LLVMValueRef so_offset
= LLVMGetParam(ctx
->main_fn
,
2279 ctx
->param_streamout_offset
[i
]);
2280 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(ctx
->i32
, 4, 0), "");
2282 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
2283 LLVMConstInt(ctx
->i32
, so
->stride
[i
]*4, 0), "");
2284 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
2287 /* Write streamout data. */
2288 for (i
= 0; i
< so
->num_outputs
; i
++) {
2289 unsigned buf_idx
= so
->output
[i
].output_buffer
;
2290 unsigned reg
= so
->output
[i
].register_index
;
2291 unsigned start
= so
->output
[i
].start_component
;
2292 unsigned num_comps
= so
->output
[i
].num_components
;
2293 unsigned stream
= so
->output
[i
].stream
;
2294 LLVMValueRef out
[4];
2295 struct lp_build_if_state if_ctx_stream
;
2297 assert(num_comps
&& num_comps
<= 4);
2298 if (!num_comps
|| num_comps
> 4)
2304 /* Load the output as int. */
2305 for (j
= 0; j
< num_comps
; j
++) {
2306 out
[j
] = LLVMBuildBitCast(builder
,
2307 outputs
[reg
].values
[start
+j
],
2311 /* Pack the output. */
2312 LLVMValueRef vdata
= NULL
;
2314 switch (num_comps
) {
2315 case 1: /* as i32 */
2318 case 2: /* as v2i32 */
2319 case 3: /* as v4i32 (aligned to 4) */
2320 case 4: /* as v4i32 */
2321 vdata
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, util_next_power_of_two(num_comps
)));
2322 for (j
= 0; j
< num_comps
; j
++) {
2323 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
2324 LLVMConstInt(ctx
->i32
, j
, 0), "");
2329 LLVMValueRef can_emit_stream
=
2330 LLVMBuildICmp(builder
, LLVMIntEQ
,
2332 lp_build_const_int32(gallivm
, stream
), "");
2334 lp_build_if(&if_ctx_stream
, gallivm
, can_emit_stream
);
2335 build_tbuffer_store_dwords(ctx
, so_buffers
[buf_idx
],
2337 so_write_offset
[buf_idx
],
2338 LLVMConstInt(ctx
->i32
, 0, 0),
2339 so
->output
[i
].dst_offset
*4);
2340 lp_build_endif(&if_ctx_stream
);
2343 lp_build_endif(&if_ctx
);
2347 /* Generate export instructions for hardware VS shader stage */
2348 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
2349 struct si_shader_output_values
*outputs
,
2352 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2353 struct si_shader
*shader
= ctx
->shader
;
2354 struct lp_build_context
*base
= &bld_base
->base
;
2355 struct lp_build_context
*uint
=
2356 &ctx
->soa
.bld_base
.uint_bld
;
2357 LLVMValueRef args
[9];
2358 LLVMValueRef pos_args
[4][9] = { { 0 } };
2359 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2360 unsigned semantic_name
, semantic_index
;
2362 unsigned param_count
= 0;
2366 if (outputs
&& ctx
->shader
->selector
->so
.num_outputs
) {
2367 si_llvm_emit_streamout(ctx
, outputs
, noutput
);
2370 for (i
= 0; i
< noutput
; i
++) {
2371 semantic_name
= outputs
[i
].name
;
2372 semantic_index
= outputs
[i
].sid
;
2373 bool export_param
= true;
2375 switch (semantic_name
) {
2376 case TGSI_SEMANTIC_POSITION
: /* ignore these */
2377 case TGSI_SEMANTIC_PSIZE
:
2378 case TGSI_SEMANTIC_CLIPVERTEX
:
2379 case TGSI_SEMANTIC_EDGEFLAG
:
2381 case TGSI_SEMANTIC_GENERIC
:
2382 case TGSI_SEMANTIC_CLIPDIST
:
2383 if (shader
->key
.opt
.hw_vs
.kill_outputs
&
2384 (1ull << si_shader_io_get_unique_index(semantic_name
, semantic_index
)))
2385 export_param
= false;
2388 if (shader
->key
.opt
.hw_vs
.kill_outputs2
&
2389 (1u << si_shader_io_get_unique_index2(semantic_name
, semantic_index
)))
2390 export_param
= false;
2395 /* Select the correct target */
2396 switch(semantic_name
) {
2397 case TGSI_SEMANTIC_PSIZE
:
2398 psize_value
= outputs
[i
].values
[0];
2400 case TGSI_SEMANTIC_EDGEFLAG
:
2401 edgeflag_value
= outputs
[i
].values
[0];
2403 case TGSI_SEMANTIC_LAYER
:
2404 layer_value
= outputs
[i
].values
[0];
2405 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2406 goto handle_semantic
;
2407 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
2408 viewport_index_value
= outputs
[i
].values
[0];
2409 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2410 goto handle_semantic
;
2411 case TGSI_SEMANTIC_POSITION
:
2412 target
= V_008DFC_SQ_EXP_POS
;
2414 case TGSI_SEMANTIC_COLOR
:
2415 case TGSI_SEMANTIC_BCOLOR
:
2418 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
2419 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2420 shader
->info
.vs_output_param_offset
[i
] = param_count
;
2423 case TGSI_SEMANTIC_CLIPDIST
:
2424 if (shader
->key
.opt
.hw_vs
.clip_disable
) {
2425 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2426 goto handle_semantic
;
2428 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
2430 case TGSI_SEMANTIC_CLIPVERTEX
:
2431 if (shader
->key
.opt
.hw_vs
.clip_disable
)
2433 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
2435 case TGSI_SEMANTIC_PRIMID
:
2436 case TGSI_SEMANTIC_FOG
:
2437 case TGSI_SEMANTIC_TEXCOORD
:
2438 case TGSI_SEMANTIC_GENERIC
:
2441 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
2442 assert(i
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
2443 shader
->info
.vs_output_param_offset
[i
] = param_count
;
2449 "Warning: SI unhandled vs output type:%d\n",
2453 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
2455 if (target
>= V_008DFC_SQ_EXP_POS
&&
2456 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
2457 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
2458 args
, sizeof(args
));
2460 lp_build_intrinsic(base
->gallivm
->builder
,
2461 "llvm.SI.export", ctx
->voidt
,
2465 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
2466 semantic_name
= TGSI_SEMANTIC_GENERIC
;
2467 goto handle_semantic
;
2471 shader
->info
.nr_param_exports
= param_count
;
2473 /* We need to add the position output manually if it's missing. */
2474 if (!pos_args
[0][0]) {
2475 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
2476 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
2477 pos_args
[0][2] = uint
->zero
; /* last export? */
2478 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
2479 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
2480 pos_args
[0][5] = base
->zero
; /* X */
2481 pos_args
[0][6] = base
->zero
; /* Y */
2482 pos_args
[0][7] = base
->zero
; /* Z */
2483 pos_args
[0][8] = base
->one
; /* W */
2486 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2487 if (shader
->selector
->info
.writes_psize
||
2488 shader
->selector
->info
.writes_edgeflag
||
2489 shader
->selector
->info
.writes_viewport_index
||
2490 shader
->selector
->info
.writes_layer
) {
2491 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
2492 shader
->selector
->info
.writes_psize
|
2493 (shader
->selector
->info
.writes_edgeflag
<< 1) |
2494 (shader
->selector
->info
.writes_layer
<< 2) |
2495 (shader
->selector
->info
.writes_viewport_index
<< 3));
2496 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
2497 pos_args
[1][2] = uint
->zero
; /* last export? */
2498 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
2499 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
2500 pos_args
[1][5] = base
->zero
; /* X */
2501 pos_args
[1][6] = base
->zero
; /* Y */
2502 pos_args
[1][7] = base
->zero
; /* Z */
2503 pos_args
[1][8] = base
->zero
; /* W */
2505 if (shader
->selector
->info
.writes_psize
)
2506 pos_args
[1][5] = psize_value
;
2508 if (shader
->selector
->info
.writes_edgeflag
) {
2509 /* The output is a float, but the hw expects an integer
2510 * with the first bit containing the edge flag. */
2511 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
2514 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
2516 bld_base
->int_bld
.one
);
2518 /* The LLVM intrinsic expects a float. */
2519 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
2524 if (shader
->selector
->info
.writes_layer
)
2525 pos_args
[1][7] = layer_value
;
2527 if (shader
->selector
->info
.writes_viewport_index
)
2528 pos_args
[1][8] = viewport_index_value
;
2531 for (i
= 0; i
< 4; i
++)
2533 shader
->info
.nr_pos_exports
++;
2536 for (i
= 0; i
< 4; i
++) {
2537 if (!pos_args
[i
][0])
2540 /* Specify the target we are exporting */
2541 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
2543 if (pos_idx
== shader
->info
.nr_pos_exports
)
2544 /* Specify that this is the last export */
2545 pos_args
[i
][2] = uint
->one
;
2547 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
2548 ctx
->voidt
, pos_args
[i
], 9, 0);
2553 * Forward all outputs from the vertex shader to the TES. This is only used
2554 * for the fixed function TCS.
2556 static void si_copy_tcs_inputs(struct lp_build_tgsi_context
*bld_base
)
2558 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2559 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2560 LLVMValueRef invocation_id
, rw_buffers
, buffer
, buffer_offset
;
2561 LLVMValueRef lds_vertex_stride
, lds_vertex_offset
, lds_base
;
2564 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2566 rw_buffers
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_RW_BUFFERS
);
2567 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
2568 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_OFFCHIP
));
2570 buffer_offset
= LLVMGetParam(ctx
->main_fn
, ctx
->param_oc_lds
);
2572 lds_vertex_stride
= unpack_param(ctx
, SI_PARAM_TCS_IN_LAYOUT
, 13, 8);
2573 lds_vertex_offset
= LLVMBuildMul(gallivm
->builder
, invocation_id
,
2574 lds_vertex_stride
, "");
2575 lds_base
= get_tcs_in_current_patch_offset(ctx
);
2576 lds_base
= LLVMBuildAdd(gallivm
->builder
, lds_base
, lds_vertex_offset
, "");
2578 inputs
= ctx
->shader
->key
.mono
.tcs
.inputs_to_copy
;
2580 unsigned i
= u_bit_scan64(&inputs
);
2582 LLVMValueRef lds_ptr
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2583 lp_build_const_int32(gallivm
, 4 * i
),
2586 LLVMValueRef buffer_addr
= get_tcs_tes_buffer_address(ctx
,
2588 lp_build_const_int32(gallivm
, i
));
2590 LLVMValueRef value
= lds_load(bld_base
, TGSI_TYPE_SIGNED
, ~0,
2593 build_tbuffer_store_dwords(ctx
, buffer
, value
, 4, buffer_addr
,
2598 static void si_write_tess_factors(struct lp_build_tgsi_context
*bld_base
,
2599 LLVMValueRef rel_patch_id
,
2600 LLVMValueRef invocation_id
,
2601 LLVMValueRef tcs_out_current_patch_data_offset
)
2603 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2604 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2605 struct si_shader
*shader
= ctx
->shader
;
2606 unsigned tess_inner_index
, tess_outer_index
;
2607 LLVMValueRef lds_base
, lds_inner
, lds_outer
, byteoffset
, buffer
;
2608 LLVMValueRef out
[6], vec0
, vec1
, rw_buffers
, tf_base
;
2609 unsigned stride
, outer_comps
, inner_comps
, i
;
2610 struct lp_build_if_state if_ctx
, inner_if_ctx
;
2612 si_llvm_emit_barrier(NULL
, bld_base
, NULL
);
2614 /* Do this only for invocation 0, because the tess levels are per-patch,
2617 * This can't jump, because invocation 0 executes this. It should
2618 * at least mask out the loads and stores for other invocations.
2620 lp_build_if(&if_ctx
, gallivm
,
2621 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2622 invocation_id
, bld_base
->uint_bld
.zero
, ""));
2624 /* Determine the layout of one tess factor element in the buffer. */
2625 switch (shader
->key
.part
.tcs
.epilog
.prim_mode
) {
2626 case PIPE_PRIM_LINES
:
2627 stride
= 2; /* 2 dwords, 1 vec2 store */
2631 case PIPE_PRIM_TRIANGLES
:
2632 stride
= 4; /* 4 dwords, 1 vec4 store */
2636 case PIPE_PRIM_QUADS
:
2637 stride
= 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2646 /* Load tess_inner and tess_outer from LDS.
2647 * Any invocation can write them, so we can't get them from a temporary.
2649 tess_inner_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0);
2650 tess_outer_index
= si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0);
2652 lds_base
= tcs_out_current_patch_data_offset
;
2653 lds_inner
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2654 lp_build_const_int32(gallivm
,
2655 tess_inner_index
* 4), "");
2656 lds_outer
= LLVMBuildAdd(gallivm
->builder
, lds_base
,
2657 lp_build_const_int32(gallivm
,
2658 tess_outer_index
* 4), "");
2660 if (shader
->key
.part
.tcs
.epilog
.prim_mode
== PIPE_PRIM_LINES
) {
2661 /* For isolines, the hardware expects tess factors in the
2662 * reverse order from what GLSL / TGSI specify.
2664 out
[0] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 1, lds_outer
);
2665 out
[1] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, 0, lds_outer
);
2667 for (i
= 0; i
< outer_comps
; i
++)
2668 out
[i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_outer
);
2669 for (i
= 0; i
< inner_comps
; i
++)
2670 out
[outer_comps
+i
] = lds_load(bld_base
, TGSI_TYPE_SIGNED
, i
, lds_inner
);
2673 /* Convert the outputs to vectors for stores. */
2674 vec0
= lp_build_gather_values(gallivm
, out
, MIN2(stride
, 4));
2678 vec1
= lp_build_gather_values(gallivm
, out
+4, stride
- 4);
2680 /* Get the buffer. */
2681 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2682 SI_PARAM_RW_BUFFERS
);
2683 buffer
= build_indexed_load_const(ctx
, rw_buffers
,
2684 lp_build_const_int32(gallivm
, SI_HS_RING_TESS_FACTOR
));
2686 /* Get the offset. */
2687 tf_base
= LLVMGetParam(ctx
->main_fn
,
2688 SI_PARAM_TESS_FACTOR_OFFSET
);
2689 byteoffset
= LLVMBuildMul(gallivm
->builder
, rel_patch_id
,
2690 lp_build_const_int32(gallivm
, 4 * stride
), "");
2692 lp_build_if(&inner_if_ctx
, gallivm
,
2693 LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
,
2694 rel_patch_id
, bld_base
->uint_bld
.zero
, ""));
2696 /* Store the dynamic HS control word. */
2697 build_tbuffer_store_dwords(ctx
, buffer
,
2698 lp_build_const_int32(gallivm
, 0x80000000),
2699 1, lp_build_const_int32(gallivm
, 0), tf_base
, 0);
2701 lp_build_endif(&inner_if_ctx
);
2703 /* Store the tessellation factors. */
2704 build_tbuffer_store_dwords(ctx
, buffer
, vec0
,
2705 MIN2(stride
, 4), byteoffset
, tf_base
, 4);
2707 build_tbuffer_store_dwords(ctx
, buffer
, vec1
,
2708 stride
- 4, byteoffset
, tf_base
, 20);
2709 lp_build_endif(&if_ctx
);
2712 /* This only writes the tessellation factor levels. */
2713 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2715 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2716 LLVMValueRef rel_patch_id
, invocation_id
, tf_lds_offset
;
2718 si_copy_tcs_inputs(bld_base
);
2720 rel_patch_id
= get_rel_patch_id(ctx
);
2721 invocation_id
= unpack_param(ctx
, SI_PARAM_REL_IDS
, 8, 5);
2722 tf_lds_offset
= get_tcs_out_current_patch_data_offset(ctx
);
2724 /* Return epilog parameters from this function. */
2725 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2726 LLVMValueRef ret
= ctx
->return_value
;
2727 LLVMValueRef rw_buffers
, rw0
, rw1
, tf_soffset
;
2730 /* RW_BUFFERS pointer */
2731 rw_buffers
= LLVMGetParam(ctx
->main_fn
,
2732 SI_PARAM_RW_BUFFERS
);
2733 rw_buffers
= LLVMBuildPtrToInt(builder
, rw_buffers
, ctx
->i64
, "");
2734 rw_buffers
= LLVMBuildBitCast(builder
, rw_buffers
, ctx
->v2i32
, "");
2735 rw0
= LLVMBuildExtractElement(builder
, rw_buffers
,
2736 bld_base
->uint_bld
.zero
, "");
2737 rw1
= LLVMBuildExtractElement(builder
, rw_buffers
,
2738 bld_base
->uint_bld
.one
, "");
2739 ret
= LLVMBuildInsertValue(builder
, ret
, rw0
, 0, "");
2740 ret
= LLVMBuildInsertValue(builder
, ret
, rw1
, 1, "");
2742 /* Tess factor buffer soffset is after user SGPRs. */
2743 tf_soffset
= LLVMGetParam(ctx
->main_fn
,
2744 SI_PARAM_TESS_FACTOR_OFFSET
);
2745 ret
= LLVMBuildInsertValue(builder
, ret
, tf_soffset
,
2746 SI_TCS_NUM_USER_SGPR
+ 1, "");
2749 rel_patch_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, rel_patch_id
);
2750 invocation_id
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, invocation_id
);
2751 tf_lds_offset
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, tf_lds_offset
);
2753 vgpr
= SI_TCS_NUM_USER_SGPR
+ 2;
2754 ret
= LLVMBuildInsertValue(builder
, ret
, rel_patch_id
, vgpr
++, "");
2755 ret
= LLVMBuildInsertValue(builder
, ret
, invocation_id
, vgpr
++, "");
2756 ret
= LLVMBuildInsertValue(builder
, ret
, tf_lds_offset
, vgpr
++, "");
2757 ctx
->return_value
= ret
;
2760 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context
*bld_base
)
2762 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2763 struct si_shader
*shader
= ctx
->shader
;
2764 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2765 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2767 LLVMValueRef vertex_id
= LLVMGetParam(ctx
->main_fn
,
2768 ctx
->param_rel_auto_id
);
2769 LLVMValueRef vertex_dw_stride
=
2770 unpack_param(ctx
, SI_PARAM_LS_OUT_LAYOUT
, 13, 8);
2771 LLVMValueRef base_dw_addr
= LLVMBuildMul(gallivm
->builder
, vertex_id
,
2772 vertex_dw_stride
, "");
2774 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2775 * its inputs from it. */
2776 for (i
= 0; i
< info
->num_outputs
; i
++) {
2777 LLVMValueRef
*out_ptr
= ctx
->soa
.outputs
[i
];
2778 unsigned name
= info
->output_semantic_name
[i
];
2779 unsigned index
= info
->output_semantic_index
[i
];
2780 int param
= si_shader_io_get_unique_index(name
, index
);
2781 LLVMValueRef dw_addr
= LLVMBuildAdd(gallivm
->builder
, base_dw_addr
,
2782 lp_build_const_int32(gallivm
, param
* 4), "");
2784 for (chan
= 0; chan
< 4; chan
++) {
2785 lds_store(bld_base
, chan
, dw_addr
,
2786 LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], ""));
2791 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
*bld_base
)
2793 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2794 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2795 struct si_shader
*es
= ctx
->shader
;
2796 struct tgsi_shader_info
*info
= &es
->selector
->info
;
2797 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
2798 ctx
->param_es2gs_offset
);
2802 for (i
= 0; i
< info
->num_outputs
; i
++) {
2803 LLVMValueRef
*out_ptr
=
2804 ctx
->soa
.outputs
[i
];
2807 if (info
->output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
||
2808 info
->output_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
)
2811 param_index
= si_shader_io_get_unique_index(info
->output_semantic_name
[i
],
2812 info
->output_semantic_index
[i
]);
2814 for (chan
= 0; chan
< 4; chan
++) {
2815 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2816 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
2818 build_tbuffer_store(ctx
,
2821 LLVMGetUndef(ctx
->i32
), soffset
,
2822 (4 * param_index
+ chan
) * 4,
2823 V_008F0C_BUF_DATA_FORMAT_32
,
2824 V_008F0C_BUF_NUM_FORMAT_UINT
,
2830 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2832 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2833 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2834 LLVMValueRef args
[2];
2836 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
2837 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
2838 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2839 ctx
->voidt
, args
, 2, 0);
2842 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
*bld_base
)
2844 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2845 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2846 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
2847 struct si_shader_output_values
*outputs
= NULL
;
2850 assert(!ctx
->shader
->is_gs_copy_shader
);
2852 outputs
= MALLOC((info
->num_outputs
+ 1) * sizeof(outputs
[0]));
2854 /* Vertex color clamping.
2856 * This uses a state constant loaded in a user data SGPR and
2857 * an IF statement is added that clamps all colors if the constant
2860 if (ctx
->type
== PIPE_SHADER_VERTEX
) {
2861 struct lp_build_if_state if_ctx
;
2862 LLVMValueRef cond
= NULL
;
2863 LLVMValueRef addr
, val
;
2865 for (i
= 0; i
< info
->num_outputs
; i
++) {
2866 if (info
->output_semantic_name
[i
] != TGSI_SEMANTIC_COLOR
&&
2867 info
->output_semantic_name
[i
] != TGSI_SEMANTIC_BCOLOR
)
2870 /* We've found a color. */
2872 /* The state is in the first bit of the user SGPR. */
2873 cond
= LLVMGetParam(ctx
->main_fn
,
2874 SI_PARAM_VS_STATE_BITS
);
2875 cond
= LLVMBuildTrunc(gallivm
->builder
, cond
,
2877 lp_build_if(&if_ctx
, gallivm
, cond
);
2880 for (j
= 0; j
< 4; j
++) {
2881 addr
= ctx
->soa
.outputs
[i
][j
];
2882 val
= LLVMBuildLoad(gallivm
->builder
, addr
, "");
2883 val
= si_llvm_saturate(bld_base
, val
);
2884 LLVMBuildStore(gallivm
->builder
, val
, addr
);
2889 lp_build_endif(&if_ctx
);
2892 for (i
= 0; i
< info
->num_outputs
; i
++) {
2893 outputs
[i
].name
= info
->output_semantic_name
[i
];
2894 outputs
[i
].sid
= info
->output_semantic_index
[i
];
2896 for (j
= 0; j
< 4; j
++)
2897 outputs
[i
].values
[j
] =
2898 LLVMBuildLoad(gallivm
->builder
,
2899 ctx
->soa
.outputs
[i
][j
],
2903 /* Return the primitive ID from the LLVM function. */
2905 LLVMBuildInsertValue(gallivm
->builder
,
2907 bitcast(bld_base
, TGSI_TYPE_FLOAT
,
2908 get_primitive_id(bld_base
, 0)),
2909 VS_EPILOG_PRIMID_LOC
, "");
2911 si_llvm_export_vs(bld_base
, outputs
, i
);
2915 struct si_ps_exports
{
2917 LLVMValueRef args
[10][9];
2920 unsigned si_get_spi_shader_z_format(bool writes_z
, bool writes_stencil
,
2921 bool writes_samplemask
)
2924 /* Z needs 32 bits. */
2925 if (writes_samplemask
)
2926 return V_028710_SPI_SHADER_32_ABGR
;
2927 else if (writes_stencil
)
2928 return V_028710_SPI_SHADER_32_GR
;
2930 return V_028710_SPI_SHADER_32_R
;
2931 } else if (writes_stencil
|| writes_samplemask
) {
2932 /* Both stencil and sample mask need only 16 bits. */
2933 return V_028710_SPI_SHADER_UINT16_ABGR
;
2935 return V_028710_SPI_SHADER_ZERO
;
2939 static void si_export_mrt_z(struct lp_build_tgsi_context
*bld_base
,
2940 LLVMValueRef depth
, LLVMValueRef stencil
,
2941 LLVMValueRef samplemask
, struct si_ps_exports
*exp
)
2943 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
2944 struct lp_build_context
*base
= &bld_base
->base
;
2945 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2946 LLVMValueRef args
[9];
2948 unsigned format
= si_get_spi_shader_z_format(depth
!= NULL
,
2950 samplemask
!= NULL
);
2952 assert(depth
|| stencil
|| samplemask
);
2954 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
2955 args
[2] = uint
->one
; /* DONE bit */
2957 /* Specify the target we are exporting */
2958 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
2960 args
[4] = uint
->zero
; /* COMP flag */
2961 args
[5] = base
->undef
; /* R, depth */
2962 args
[6] = base
->undef
; /* G, stencil test value[0:7], stencil op value[8:15] */
2963 args
[7] = base
->undef
; /* B, sample mask */
2964 args
[8] = base
->undef
; /* A, alpha to mask */
2966 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
2968 args
[4] = uint
->one
; /* COMPR flag */
2971 /* Stencil should be in X[23:16]. */
2972 stencil
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, stencil
);
2973 stencil
= LLVMBuildShl(base
->gallivm
->builder
, stencil
,
2974 LLVMConstInt(ctx
->i32
, 16, 0), "");
2975 args
[5] = bitcast(bld_base
, TGSI_TYPE_FLOAT
, stencil
);
2979 /* SampleMask should be in Y[15:0]. */
2980 args
[6] = samplemask
;
2993 args
[7] = samplemask
;
2998 /* SI (except OLAND and HAINAN) has a bug that it only looks
2999 * at the X writemask component. */
3000 if (ctx
->screen
->b
.chip_class
== SI
&&
3001 ctx
->screen
->b
.family
!= CHIP_OLAND
&&
3002 ctx
->screen
->b
.family
!= CHIP_HAINAN
)
3005 /* Specify which components to enable */
3006 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
3008 memcpy(exp
->args
[exp
->num
++], args
, sizeof(args
));
3011 static void si_export_mrt_color(struct lp_build_tgsi_context
*bld_base
,
3012 LLVMValueRef
*color
, unsigned index
,
3013 unsigned samplemask_param
,
3014 bool is_last
, struct si_ps_exports
*exp
)
3016 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3017 struct lp_build_context
*base
= &bld_base
->base
;
3021 if (ctx
->shader
->key
.part
.ps
.epilog
.clamp_color
)
3022 for (i
= 0; i
< 4; i
++)
3023 color
[i
] = si_llvm_saturate(bld_base
, color
[i
]);
3026 if (ctx
->shader
->key
.part
.ps
.epilog
.alpha_to_one
)
3027 color
[3] = base
->one
;
3031 ctx
->shader
->key
.part
.ps
.epilog
.alpha_func
!= PIPE_FUNC_ALWAYS
)
3032 si_alpha_test(bld_base
, color
[3]);
3034 /* Line & polygon smoothing */
3035 if (ctx
->shader
->key
.part
.ps
.epilog
.poly_line_smoothing
)
3036 color
[3] = si_scale_alpha_by_sample_mask(bld_base
, color
[3],
3039 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3040 if (ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
> 0) {
3041 LLVMValueRef args
[8][9];
3044 /* Get the export arguments, also find out what the last one is. */
3045 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3046 si_llvm_init_export_args(bld_base
, color
,
3047 V_008DFC_SQ_EXP_MRT
+ c
, args
[c
]);
3048 if (args
[c
][0] != bld_base
->uint_bld
.zero
)
3052 /* Emit all exports. */
3053 for (c
= 0; c
<= ctx
->shader
->key
.part
.ps
.epilog
.last_cbuf
; c
++) {
3054 if (is_last
&& last
== c
) {
3055 args
[c
][1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
3056 args
[c
][2] = bld_base
->uint_bld
.one
; /* DONE bit */
3057 } else if (args
[c
][0] == bld_base
->uint_bld
.zero
)
3058 continue; /* unnecessary NULL export */
3060 memcpy(exp
->args
[exp
->num
++], args
[c
], sizeof(args
[c
]));
3063 LLVMValueRef args
[9];
3066 si_llvm_init_export_args(bld_base
, color
, V_008DFC_SQ_EXP_MRT
+ index
,
3069 args
[1] = bld_base
->uint_bld
.one
; /* whether the EXEC mask is valid */
3070 args
[2] = bld_base
->uint_bld
.one
; /* DONE bit */
3071 } else if (args
[0] == bld_base
->uint_bld
.zero
)
3072 return; /* unnecessary NULL export */
3074 memcpy(exp
->args
[exp
->num
++], args
, sizeof(args
));
3078 static void si_emit_ps_exports(struct si_shader_context
*ctx
,
3079 struct si_ps_exports
*exp
)
3081 for (unsigned i
= 0; i
< exp
->num
; i
++)
3082 lp_build_intrinsic(ctx
->gallivm
.builder
,
3083 "llvm.SI.export", ctx
->voidt
,
3084 exp
->args
[i
], 9, 0);
3087 static void si_export_null(struct lp_build_tgsi_context
*bld_base
)
3089 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3090 struct lp_build_context
*base
= &bld_base
->base
;
3091 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
3092 LLVMValueRef args
[9];
3094 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
3095 args
[1] = uint
->one
; /* whether the EXEC mask is valid */
3096 args
[2] = uint
->one
; /* DONE bit */
3097 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_NULL
);
3098 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
3099 args
[5] = base
->undef
; /* R */
3100 args
[6] = base
->undef
; /* G */
3101 args
[7] = base
->undef
; /* B */
3102 args
[8] = base
->undef
; /* A */
3104 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
3105 ctx
->voidt
, args
, 9, 0);
3109 * Return PS outputs in this order:
3111 * v[0:3] = color0.xyzw
3112 * v[4:7] = color1.xyzw
3117 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3119 * The alpha-ref SGPR is returned via its original location.
3121 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context
*bld_base
)
3123 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3124 struct si_shader
*shader
= ctx
->shader
;
3125 struct lp_build_context
*base
= &bld_base
->base
;
3126 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
3127 LLVMBuilderRef builder
= base
->gallivm
->builder
;
3128 unsigned i
, j
, first_vgpr
, vgpr
;
3130 LLVMValueRef color
[8][4] = {};
3131 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3134 /* Read the output values. */
3135 for (i
= 0; i
< info
->num_outputs
; i
++) {
3136 unsigned semantic_name
= info
->output_semantic_name
[i
];
3137 unsigned semantic_index
= info
->output_semantic_index
[i
];
3139 switch (semantic_name
) {
3140 case TGSI_SEMANTIC_COLOR
:
3141 assert(semantic_index
< 8);
3142 for (j
= 0; j
< 4; j
++) {
3143 LLVMValueRef ptr
= ctx
->soa
.outputs
[i
][j
];
3144 LLVMValueRef result
= LLVMBuildLoad(builder
, ptr
, "");
3145 color
[semantic_index
][j
] = result
;
3148 case TGSI_SEMANTIC_POSITION
:
3149 depth
= LLVMBuildLoad(builder
,
3150 ctx
->soa
.outputs
[i
][2], "");
3152 case TGSI_SEMANTIC_STENCIL
:
3153 stencil
= LLVMBuildLoad(builder
,
3154 ctx
->soa
.outputs
[i
][1], "");
3156 case TGSI_SEMANTIC_SAMPLEMASK
:
3157 samplemask
= LLVMBuildLoad(builder
,
3158 ctx
->soa
.outputs
[i
][0], "");
3161 fprintf(stderr
, "Warning: SI unhandled fs output type:%d\n",
3166 /* Fill the return structure. */
3167 ret
= ctx
->return_value
;
3170 ret
= LLVMBuildInsertValue(builder
, ret
,
3171 bitcast(bld_base
, TGSI_TYPE_SIGNED
,
3172 LLVMGetParam(ctx
->main_fn
,
3173 SI_PARAM_ALPHA_REF
)),
3174 SI_SGPR_ALPHA_REF
, "");
3177 first_vgpr
= vgpr
= SI_SGPR_ALPHA_REF
+ 1;
3178 for (i
= 0; i
< ARRAY_SIZE(color
); i
++) {
3182 for (j
= 0; j
< 4; j
++)
3183 ret
= LLVMBuildInsertValue(builder
, ret
, color
[i
][j
], vgpr
++, "");
3186 ret
= LLVMBuildInsertValue(builder
, ret
, depth
, vgpr
++, "");
3188 ret
= LLVMBuildInsertValue(builder
, ret
, stencil
, vgpr
++, "");
3190 ret
= LLVMBuildInsertValue(builder
, ret
, samplemask
, vgpr
++, "");
3192 /* Add the input sample mask for smoothing at the end. */
3193 if (vgpr
< first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
)
3194 vgpr
= first_vgpr
+ PS_EPILOG_SAMPLEMASK_MIN_LOC
;
3195 ret
= LLVMBuildInsertValue(builder
, ret
,
3196 LLVMGetParam(ctx
->main_fn
,
3197 SI_PARAM_SAMPLE_COVERAGE
), vgpr
++, "");
3199 ctx
->return_value
= ret
;
3203 * Given a v8i32 resource descriptor for a buffer, extract the size of the
3204 * buffer in number of elements and return it as an i32.
3206 static LLVMValueRef
get_buffer_size(
3207 struct lp_build_tgsi_context
*bld_base
,
3208 LLVMValueRef descriptor
)
3210 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3211 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3212 LLVMBuilderRef builder
= gallivm
->builder
;
3214 LLVMBuildExtractElement(builder
, descriptor
,
3215 lp_build_const_int32(gallivm
, 2), "");
3217 if (ctx
->screen
->b
.chip_class
>= VI
) {
3218 /* On VI, the descriptor contains the size in bytes,
3219 * but TXQ must return the size in elements.
3220 * The stride is always non-zero for resources using TXQ.
3222 LLVMValueRef stride
=
3223 LLVMBuildExtractElement(builder
, descriptor
,
3224 lp_build_const_int32(gallivm
, 1), "");
3225 stride
= LLVMBuildLShr(builder
, stride
,
3226 lp_build_const_int32(gallivm
, 16), "");
3227 stride
= LLVMBuildAnd(builder
, stride
,
3228 lp_build_const_int32(gallivm
, 0x3FFF), "");
3230 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
3237 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
3240 static void build_type_name_for_intr(
3242 char *buf
, unsigned bufsize
)
3244 LLVMTypeRef elem_type
= type
;
3246 assert(bufsize
>= 8);
3248 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
3249 int ret
= snprintf(buf
, bufsize
, "v%u",
3250 LLVMGetVectorSize(type
));
3252 char *type_name
= LLVMPrintTypeToString(type
);
3253 fprintf(stderr
, "Error building type name for: %s\n",
3257 elem_type
= LLVMGetElementType(type
);
3261 switch (LLVMGetTypeKind(elem_type
)) {
3263 case LLVMIntegerTypeKind
:
3264 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
3266 case LLVMFloatTypeKind
:
3267 snprintf(buf
, bufsize
, "f32");
3269 case LLVMDoubleTypeKind
:
3270 snprintf(buf
, bufsize
, "f64");
3275 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
3276 struct lp_build_tgsi_context
*bld_base
,
3277 struct lp_build_emit_data
*emit_data
);
3279 /* Prevent optimizations (at least of memory accesses) across the current
3280 * point in the program by emitting empty inline assembly that is marked as
3281 * having side effects.
3283 static void emit_optimization_barrier(struct si_shader_context
*ctx
)
3285 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3286 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
3287 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, "", "", true, false);
3288 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
3291 static void emit_waitcnt(struct si_shader_context
*ctx
)
3293 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3294 LLVMBuilderRef builder
= gallivm
->builder
;
3295 LLVMValueRef args
[1] = {
3296 lp_build_const_int32(gallivm
, 0xf70)
3298 lp_build_intrinsic(builder
, "llvm.amdgcn.s.waitcnt",
3299 ctx
->voidt
, args
, 1, 0);
3302 static void membar_emit(
3303 const struct lp_build_tgsi_action
*action
,
3304 struct lp_build_tgsi_context
*bld_base
,
3305 struct lp_build_emit_data
*emit_data
)
3307 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3313 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
3314 const struct tgsi_full_src_register
*reg
)
3317 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3318 SI_PARAM_SHADER_BUFFERS
);
3320 if (!reg
->Register
.Indirect
)
3321 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, 0);
3323 index
= get_bounded_indirect_index(ctx
, ®
->Indirect
,
3324 reg
->Register
.Index
,
3325 SI_NUM_SHADER_BUFFERS
);
3327 return build_indexed_load_const(ctx
, rsrc_ptr
, index
);
3330 static bool tgsi_is_array_sampler(unsigned target
)
3332 return target
== TGSI_TEXTURE_1D_ARRAY
||
3333 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
3334 target
== TGSI_TEXTURE_2D_ARRAY
||
3335 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
3336 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3337 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
3338 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3341 static bool tgsi_is_array_image(unsigned target
)
3343 return target
== TGSI_TEXTURE_3D
||
3344 target
== TGSI_TEXTURE_CUBE
||
3345 target
== TGSI_TEXTURE_1D_ARRAY
||
3346 target
== TGSI_TEXTURE_2D_ARRAY
||
3347 target
== TGSI_TEXTURE_CUBE_ARRAY
||
3348 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
3352 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
3354 * At least on Tonga, executing image stores on images with DCC enabled and
3355 * non-trivial can eventually lead to lockups. This can occur when an
3356 * application binds an image as read-only but then uses a shader that writes
3357 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
3358 * program termination) in this case, but it doesn't cost much to be a bit
3359 * nicer: disabling DCC in the shader still leads to undefined results but
3360 * avoids the lockup.
3362 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
3365 if (ctx
->screen
->b
.chip_class
<= CIK
) {
3368 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3369 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
3370 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
3373 tmp
= LLVMBuildExtractElement(builder
, rsrc
, i32_6
, "");
3374 tmp
= LLVMBuildAnd(builder
, tmp
, i32_C
, "");
3375 return LLVMBuildInsertElement(builder
, rsrc
, tmp
, i32_6
, "");
3379 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
3381 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
3386 * Load the resource descriptor for \p image.
3390 struct lp_build_tgsi_context
*bld_base
,
3391 const struct tgsi_full_src_register
*image
,
3392 bool is_store
, unsigned target
,
3395 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3396 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
3398 LLVMValueRef index
, tmp
;
3399 bool dcc_off
= target
!= TGSI_TEXTURE_BUFFER
&& is_store
;
3401 assert(image
->Register
.File
== TGSI_FILE_IMAGE
);
3403 if (!image
->Register
.Indirect
) {
3404 const struct tgsi_shader_info
*info
= bld_base
->info
;
3406 index
= LLVMConstInt(ctx
->i32
, image
->Register
.Index
, 0);
3408 if (info
->images_writemask
& (1 << image
->Register
.Index
) &&
3409 target
!= TGSI_TEXTURE_BUFFER
)
3412 /* From the GL_ARB_shader_image_load_store extension spec:
3414 * If a shader performs an image load, store, or atomic
3415 * operation using an image variable declared as an array,
3416 * and if the index used to select an individual element is
3417 * negative or greater than or equal to the size of the
3418 * array, the results of the operation are undefined but may
3419 * not lead to termination.
3421 index
= get_bounded_indirect_index(ctx
, &image
->Indirect
,
3422 image
->Register
.Index
,
3426 if (target
== TGSI_TEXTURE_BUFFER
) {
3427 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
3429 rsrc_ptr
= LLVMBuildPointerCast(builder
, rsrc_ptr
,
3430 const_array(ctx
->v4i32
, 0), "");
3431 index
= LLVMBuildMul(builder
, index
,
3432 LLVMConstInt(ctx
->i32
, 2, 0), "");
3433 index
= LLVMBuildAdd(builder
, index
,
3434 LLVMConstInt(ctx
->i32
, 1, 0), "");
3435 *rsrc
= build_indexed_load_const(ctx
, rsrc_ptr
, index
);
3439 tmp
= build_indexed_load_const(ctx
, rsrc_ptr
, index
);
3441 tmp
= force_dcc_off(ctx
, tmp
);
3445 static LLVMValueRef
image_fetch_coords(
3446 struct lp_build_tgsi_context
*bld_base
,
3447 const struct tgsi_full_instruction
*inst
,
3450 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3451 LLVMBuilderRef builder
= gallivm
->builder
;
3452 unsigned target
= inst
->Memory
.Texture
;
3453 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
3454 LLVMValueRef coords
[4];
3458 for (chan
= 0; chan
< num_coords
; ++chan
) {
3459 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
3460 tmp
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3464 if (num_coords
== 1)
3467 if (num_coords
== 3) {
3468 /* LLVM has difficulties lowering 3-element vectors. */
3469 coords
[3] = bld_base
->uint_bld
.undef
;
3473 return lp_build_gather_values(gallivm
, coords
, num_coords
);
3477 * Append the extra mode bits that are used by image load and store.
3479 static void image_append_args(
3480 struct si_shader_context
*ctx
,
3481 struct lp_build_emit_data
* emit_data
,
3486 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3487 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3488 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3489 LLVMValueRef r128
= i1false
;
3490 LLVMValueRef da
= tgsi_is_array_image(target
) ? i1true
: i1false
;
3493 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3495 LLVMValueRef slc
= i1false
;
3496 LLVMValueRef lwe
= i1false
;
3498 if (atomic
|| (HAVE_LLVM
<= 0x0309)) {
3499 emit_data
->args
[emit_data
->arg_count
++] = r128
;
3500 emit_data
->args
[emit_data
->arg_count
++] = da
;
3502 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3504 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3508 /* HAVE_LLVM >= 0x0400 */
3509 emit_data
->args
[emit_data
->arg_count
++] = glc
;
3510 emit_data
->args
[emit_data
->arg_count
++] = slc
;
3511 emit_data
->args
[emit_data
->arg_count
++] = lwe
;
3512 emit_data
->args
[emit_data
->arg_count
++] = da
;
3516 * Append the resource and indexing arguments for buffer intrinsics.
3518 * \param rsrc the v4i32 buffer resource
3519 * \param index index into the buffer (stride-based)
3520 * \param offset byte offset into the buffer
3522 static void buffer_append_args(
3523 struct si_shader_context
*ctx
,
3524 struct lp_build_emit_data
*emit_data
,
3527 LLVMValueRef offset
,
3531 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3532 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
3533 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
3535 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
3536 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
3537 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
3539 emit_data
->args
[emit_data
->arg_count
++] =
3541 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
3542 i1true
: i1false
; /* glc */
3544 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
3547 static void load_fetch_args(
3548 struct lp_build_tgsi_context
* bld_base
,
3549 struct lp_build_emit_data
* emit_data
)
3551 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3552 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3553 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3554 unsigned target
= inst
->Memory
.Texture
;
3557 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
3559 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3560 LLVMBuilderRef builder
= gallivm
->builder
;
3561 LLVMValueRef offset
;
3564 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3566 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3567 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3569 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3570 offset
, false, false);
3571 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3572 LLVMValueRef coords
;
3574 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
3575 coords
= image_fetch_coords(bld_base
, inst
, 1);
3577 if (target
== TGSI_TEXTURE_BUFFER
) {
3578 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3579 bld_base
->uint_bld
.zero
, false, false);
3581 emit_data
->args
[0] = coords
;
3582 emit_data
->args
[1] = rsrc
;
3583 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
3584 emit_data
->arg_count
= 3;
3586 image_append_args(ctx
, emit_data
, target
, false, false);
3591 static void load_emit_buffer(struct si_shader_context
*ctx
,
3592 struct lp_build_emit_data
*emit_data
)
3594 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3595 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3596 LLVMBuilderRef builder
= gallivm
->builder
;
3597 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
3598 uint count
= util_last_bit(writemask
);
3599 const char *intrinsic_name
;
3600 LLVMTypeRef dst_type
;
3604 intrinsic_name
= "llvm.amdgcn.buffer.load.f32";
3605 dst_type
= ctx
->f32
;
3608 intrinsic_name
= "llvm.amdgcn.buffer.load.v2f32";
3609 dst_type
= LLVMVectorType(ctx
->f32
, 2);
3612 intrinsic_name
= "llvm.amdgcn.buffer.load.v4f32";
3613 dst_type
= ctx
->v4f32
;
3617 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3618 builder
, intrinsic_name
, dst_type
,
3619 emit_data
->args
, emit_data
->arg_count
,
3620 LP_FUNC_ATTR_READONLY
);
3623 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
3624 const struct tgsi_full_instruction
*inst
,
3625 LLVMTypeRef type
, int arg
)
3627 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3628 LLVMBuilderRef builder
= gallivm
->builder
;
3629 LLVMValueRef offset
, ptr
;
3632 offset
= lp_build_emit_fetch(&ctx
->soa
.bld_base
, inst
, arg
, 0);
3633 offset
= LLVMBuildBitCast(builder
, offset
, ctx
->i32
, "");
3635 ptr
= ctx
->shared_memory
;
3636 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
3637 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3638 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
3643 static void load_emit_memory(
3644 struct si_shader_context
*ctx
,
3645 struct lp_build_emit_data
*emit_data
)
3647 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3648 struct lp_build_context
*base
= &ctx
->soa
.bld_base
.base
;
3649 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3650 LLVMBuilderRef builder
= gallivm
->builder
;
3651 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3652 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
3655 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 1);
3657 for (chan
= 0; chan
< 4; ++chan
) {
3658 if (!(writemask
& (1 << chan
))) {
3659 channels
[chan
] = LLVMGetUndef(base
->elem_type
);
3663 index
= lp_build_const_int32(gallivm
, chan
);
3664 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3665 channels
[chan
] = LLVMBuildLoad(builder
, derived_ptr
, "");
3667 emit_data
->output
[emit_data
->chan
] = lp_build_gather_values(gallivm
, channels
, 4);
3670 static void get_image_intr_name(const char *base_name
,
3671 LLVMTypeRef data_type
,
3672 LLVMTypeRef coords_type
,
3673 LLVMTypeRef rsrc_type
,
3674 char *out_name
, unsigned out_len
)
3676 char coords_type_name
[8];
3678 build_type_name_for_intr(coords_type
, coords_type_name
,
3679 sizeof(coords_type_name
));
3681 if (HAVE_LLVM
<= 0x0309) {
3682 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
3684 char data_type_name
[8];
3685 char rsrc_type_name
[8];
3687 build_type_name_for_intr(data_type
, data_type_name
,
3688 sizeof(data_type_name
));
3689 build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
3690 sizeof(rsrc_type_name
));
3691 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
3692 data_type_name
, coords_type_name
, rsrc_type_name
);
3696 static void load_emit(
3697 const struct lp_build_tgsi_action
*action
,
3698 struct lp_build_tgsi_context
*bld_base
,
3699 struct lp_build_emit_data
*emit_data
)
3701 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3702 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3703 LLVMBuilderRef builder
= gallivm
->builder
;
3704 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3705 char intrinsic_name
[64];
3707 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3708 load_emit_memory(ctx
, emit_data
);
3712 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3715 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3716 load_emit_buffer(ctx
, emit_data
);
3720 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
3721 emit_data
->output
[emit_data
->chan
] =
3723 builder
, "llvm.amdgcn.buffer.load.format.v4f32", emit_data
->dst_type
,
3724 emit_data
->args
, emit_data
->arg_count
,
3725 LP_FUNC_ATTR_READONLY
);
3727 get_image_intr_name("llvm.amdgcn.image.load",
3728 emit_data
->dst_type
, /* vdata */
3729 LLVMTypeOf(emit_data
->args
[0]), /* coords */
3730 LLVMTypeOf(emit_data
->args
[1]), /* rsrc */
3731 intrinsic_name
, sizeof(intrinsic_name
));
3733 emit_data
->output
[emit_data
->chan
] =
3735 builder
, intrinsic_name
, emit_data
->dst_type
,
3736 emit_data
->args
, emit_data
->arg_count
,
3737 LP_FUNC_ATTR_READONLY
);
3741 static void store_fetch_args(
3742 struct lp_build_tgsi_context
* bld_base
,
3743 struct lp_build_emit_data
* emit_data
)
3745 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3746 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3747 LLVMBuilderRef builder
= gallivm
->builder
;
3748 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3749 struct tgsi_full_src_register memory
;
3750 LLVMValueRef chans
[4];
3755 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
3757 for (chan
= 0; chan
< 4; ++chan
) {
3758 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
3760 data
= lp_build_gather_values(gallivm
, chans
, 4);
3762 emit_data
->args
[emit_data
->arg_count
++] = data
;
3764 memory
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
3766 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3767 LLVMValueRef offset
;
3770 rsrc
= shader_buffer_fetch_rsrc(ctx
, &memory
);
3772 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
3773 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3775 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3776 offset
, false, false);
3777 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3778 unsigned target
= inst
->Memory
.Texture
;
3779 LLVMValueRef coords
;
3781 /* 8bit/16bit TC L1 write corruption bug on SI.
3782 * All store opcodes not aligned to a dword are affected.
3784 * The only way to get unaligned stores in radeonsi is through
3787 bool force_glc
= ctx
->screen
->b
.chip_class
== SI
;
3789 coords
= image_fetch_coords(bld_base
, inst
, 0);
3791 if (target
== TGSI_TEXTURE_BUFFER
) {
3792 image_fetch_rsrc(bld_base
, &memory
, true, target
, &rsrc
);
3793 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3794 bld_base
->uint_bld
.zero
, false, force_glc
);
3796 emit_data
->args
[1] = coords
;
3797 image_fetch_rsrc(bld_base
, &memory
, true, target
,
3798 &emit_data
->args
[2]);
3799 emit_data
->args
[3] = lp_build_const_int32(gallivm
, 15); /* dmask */
3800 emit_data
->arg_count
= 4;
3802 image_append_args(ctx
, emit_data
, target
, false, force_glc
);
3807 static void store_emit_buffer(
3808 struct si_shader_context
*ctx
,
3809 struct lp_build_emit_data
*emit_data
)
3811 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3812 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3813 LLVMBuilderRef builder
= gallivm
->builder
;
3814 struct lp_build_context
*uint_bld
= &ctx
->soa
.bld_base
.uint_bld
;
3815 LLVMValueRef base_data
= emit_data
->args
[0];
3816 LLVMValueRef base_offset
= emit_data
->args
[3];
3817 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3821 const char *intrinsic_name
;
3823 LLVMValueRef offset
;
3826 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
3828 /* Due to an LLVM limitation, split 3-element writes
3829 * into a 2-element and a 1-element write. */
3831 writemask
|= 1 << (start
+ 2);
3837 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
3838 } else if (count
== 2) {
3839 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
3841 tmp
= LLVMBuildExtractElement(
3843 lp_build_const_int32(gallivm
, start
), "");
3844 data
= LLVMBuildInsertElement(
3845 builder
, LLVMGetUndef(v2f32
), tmp
,
3846 uint_bld
->zero
, "");
3848 tmp
= LLVMBuildExtractElement(
3850 lp_build_const_int32(gallivm
, start
+ 1), "");
3851 data
= LLVMBuildInsertElement(
3852 builder
, data
, tmp
, uint_bld
->one
, "");
3854 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
3857 data
= LLVMBuildExtractElement(
3859 lp_build_const_int32(gallivm
, start
), "");
3860 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
3863 offset
= base_offset
;
3865 offset
= LLVMBuildAdd(
3867 lp_build_const_int32(gallivm
, start
* 4), "");
3870 emit_data
->args
[0] = data
;
3871 emit_data
->args
[3] = offset
;
3874 builder
, intrinsic_name
, emit_data
->dst_type
,
3875 emit_data
->args
, emit_data
->arg_count
, 0);
3879 static void store_emit_memory(
3880 struct si_shader_context
*ctx
,
3881 struct lp_build_emit_data
*emit_data
)
3883 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
3884 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
3885 struct lp_build_context
*base
= &ctx
->soa
.bld_base
.base
;
3886 LLVMBuilderRef builder
= gallivm
->builder
;
3887 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
3888 LLVMValueRef ptr
, derived_ptr
, data
, index
;
3891 ptr
= get_memory_ptr(ctx
, inst
, base
->elem_type
, 0);
3893 for (chan
= 0; chan
< 4; ++chan
) {
3894 if (!(writemask
& (1 << chan
))) {
3897 data
= lp_build_emit_fetch(&ctx
->soa
.bld_base
, inst
, 1, chan
);
3898 index
= lp_build_const_int32(gallivm
, chan
);
3899 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
3900 LLVMBuildStore(builder
, data
, derived_ptr
);
3904 static void store_emit(
3905 const struct lp_build_tgsi_action
*action
,
3906 struct lp_build_tgsi_context
*bld_base
,
3907 struct lp_build_emit_data
*emit_data
)
3909 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3910 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3911 LLVMBuilderRef builder
= gallivm
->builder
;
3912 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3913 unsigned target
= inst
->Memory
.Texture
;
3914 char intrinsic_name
[64];
3916 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
3917 store_emit_memory(ctx
, emit_data
);
3921 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
3924 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3925 store_emit_buffer(ctx
, emit_data
);
3929 if (target
== TGSI_TEXTURE_BUFFER
) {
3930 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
3931 builder
, "llvm.amdgcn.buffer.store.format.v4f32",
3932 emit_data
->dst_type
, emit_data
->args
,
3933 emit_data
->arg_count
, 0);
3935 get_image_intr_name("llvm.amdgcn.image.store",
3936 LLVMTypeOf(emit_data
->args
[0]), /* vdata */
3937 LLVMTypeOf(emit_data
->args
[1]), /* coords */
3938 LLVMTypeOf(emit_data
->args
[2]), /* rsrc */
3939 intrinsic_name
, sizeof(intrinsic_name
));
3941 emit_data
->output
[emit_data
->chan
] =
3943 builder
, intrinsic_name
, emit_data
->dst_type
,
3944 emit_data
->args
, emit_data
->arg_count
, 0);
3948 static void atomic_fetch_args(
3949 struct lp_build_tgsi_context
* bld_base
,
3950 struct lp_build_emit_data
* emit_data
)
3952 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
3953 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
3954 LLVMBuilderRef builder
= gallivm
->builder
;
3955 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
3956 LLVMValueRef data1
, data2
;
3960 emit_data
->dst_type
= bld_base
->base
.elem_type
;
3962 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
3963 data1
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3965 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
3966 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
3967 data2
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3970 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
3971 * of arguments, which is reversed relative to TGSI (and GLSL)
3973 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
3974 emit_data
->args
[emit_data
->arg_count
++] = data2
;
3975 emit_data
->args
[emit_data
->arg_count
++] = data1
;
3977 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
3978 LLVMValueRef offset
;
3980 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0]);
3982 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
3983 offset
= LLVMBuildBitCast(builder
, tmp
, bld_base
->uint_bld
.elem_type
, "");
3985 buffer_append_args(ctx
, emit_data
, rsrc
, bld_base
->uint_bld
.zero
,
3986 offset
, true, false);
3987 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
) {
3988 unsigned target
= inst
->Memory
.Texture
;
3989 LLVMValueRef coords
;
3991 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
3992 coords
= image_fetch_coords(bld_base
, inst
, 1);
3994 if (target
== TGSI_TEXTURE_BUFFER
) {
3995 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
3996 bld_base
->uint_bld
.zero
, true, false);
3998 emit_data
->args
[emit_data
->arg_count
++] = coords
;
3999 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
4001 image_append_args(ctx
, emit_data
, target
, true, false);
4006 static void atomic_emit_memory(struct si_shader_context
*ctx
,
4007 struct lp_build_emit_data
*emit_data
) {
4008 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4009 LLVMBuilderRef builder
= gallivm
->builder
;
4010 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
4011 LLVMValueRef ptr
, result
, arg
;
4013 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
4015 arg
= lp_build_emit_fetch(&ctx
->soa
.bld_base
, inst
, 2, 0);
4016 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i32
, "");
4018 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
4019 LLVMValueRef new_data
;
4020 new_data
= lp_build_emit_fetch(&ctx
->soa
.bld_base
,
4023 new_data
= LLVMBuildBitCast(builder
, new_data
, ctx
->i32
, "");
4025 #if HAVE_LLVM >= 0x309
4026 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
4027 LLVMAtomicOrderingSequentiallyConsistent
,
4028 LLVMAtomicOrderingSequentiallyConsistent
,
4032 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
4034 LLVMAtomicRMWBinOp op
;
4036 switch(inst
->Instruction
.Opcode
) {
4037 case TGSI_OPCODE_ATOMUADD
:
4038 op
= LLVMAtomicRMWBinOpAdd
;
4040 case TGSI_OPCODE_ATOMXCHG
:
4041 op
= LLVMAtomicRMWBinOpXchg
;
4043 case TGSI_OPCODE_ATOMAND
:
4044 op
= LLVMAtomicRMWBinOpAnd
;
4046 case TGSI_OPCODE_ATOMOR
:
4047 op
= LLVMAtomicRMWBinOpOr
;
4049 case TGSI_OPCODE_ATOMXOR
:
4050 op
= LLVMAtomicRMWBinOpXor
;
4052 case TGSI_OPCODE_ATOMUMIN
:
4053 op
= LLVMAtomicRMWBinOpUMin
;
4055 case TGSI_OPCODE_ATOMUMAX
:
4056 op
= LLVMAtomicRMWBinOpUMax
;
4058 case TGSI_OPCODE_ATOMIMIN
:
4059 op
= LLVMAtomicRMWBinOpMin
;
4061 case TGSI_OPCODE_ATOMIMAX
:
4062 op
= LLVMAtomicRMWBinOpMax
;
4065 unreachable("unknown atomic opcode");
4068 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
4069 LLVMAtomicOrderingSequentiallyConsistent
,
4072 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
4075 static void atomic_emit(
4076 const struct lp_build_tgsi_action
*action
,
4077 struct lp_build_tgsi_context
*bld_base
,
4078 struct lp_build_emit_data
*emit_data
)
4080 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4081 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4082 LLVMBuilderRef builder
= gallivm
->builder
;
4083 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
4084 char intrinsic_name
[40];
4087 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
4088 atomic_emit_memory(ctx
, emit_data
);
4092 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
4093 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4094 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4095 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
4097 LLVMValueRef coords
;
4098 char coords_type
[8];
4100 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
4101 coords
= emit_data
->args
[2];
4103 coords
= emit_data
->args
[1];
4105 build_type_name_for_intr(LLVMTypeOf(coords
), coords_type
, sizeof(coords_type
));
4106 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
4107 "llvm.amdgcn.image.atomic.%s.%s",
4108 action
->intr_name
, coords_type
);
4111 tmp
= lp_build_intrinsic(
4112 builder
, intrinsic_name
, bld_base
->uint_bld
.elem_type
,
4113 emit_data
->args
, emit_data
->arg_count
, 0);
4114 emit_data
->output
[emit_data
->chan
] =
4115 LLVMBuildBitCast(builder
, tmp
, bld_base
->base
.elem_type
, "");
4118 static void resq_fetch_args(
4119 struct lp_build_tgsi_context
* bld_base
,
4120 struct lp_build_emit_data
* emit_data
)
4122 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4123 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4124 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4125 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
4127 emit_data
->dst_type
= ctx
->v4i32
;
4129 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
4130 emit_data
->args
[0] = shader_buffer_fetch_rsrc(ctx
, reg
);
4131 emit_data
->arg_count
= 1;
4132 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4133 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4134 &emit_data
->args
[0]);
4135 emit_data
->arg_count
= 1;
4137 emit_data
->args
[0] = bld_base
->uint_bld
.zero
; /* mip level */
4138 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
4139 &emit_data
->args
[1]);
4140 emit_data
->args
[2] = lp_build_const_int32(gallivm
, 15); /* dmask */
4141 emit_data
->args
[3] = bld_base
->uint_bld
.zero
; /* unorm */
4142 emit_data
->args
[4] = bld_base
->uint_bld
.zero
; /* r128 */
4143 emit_data
->args
[5] = tgsi_is_array_image(inst
->Memory
.Texture
) ?
4144 bld_base
->uint_bld
.one
: bld_base
->uint_bld
.zero
; /* da */
4145 emit_data
->args
[6] = bld_base
->uint_bld
.zero
; /* glc */
4146 emit_data
->args
[7] = bld_base
->uint_bld
.zero
; /* slc */
4147 emit_data
->args
[8] = bld_base
->uint_bld
.zero
; /* tfe */
4148 emit_data
->args
[9] = bld_base
->uint_bld
.zero
; /* lwe */
4149 emit_data
->arg_count
= 10;
4153 static void resq_emit(
4154 const struct lp_build_tgsi_action
*action
,
4155 struct lp_build_tgsi_context
*bld_base
,
4156 struct lp_build_emit_data
*emit_data
)
4158 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4159 LLVMBuilderRef builder
= gallivm
->builder
;
4160 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4163 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
4164 out
= LLVMBuildExtractElement(builder
, emit_data
->args
[0],
4165 lp_build_const_int32(gallivm
, 2), "");
4166 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
4167 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
4169 out
= lp_build_intrinsic(
4170 builder
, "llvm.SI.getresinfo.i32", emit_data
->dst_type
,
4171 emit_data
->args
, emit_data
->arg_count
,
4172 LP_FUNC_ATTR_READNONE
);
4174 /* Divide the number of layers by 6 to get the number of cubes. */
4175 if (inst
->Memory
.Texture
== TGSI_TEXTURE_CUBE_ARRAY
) {
4176 LLVMValueRef imm2
= lp_build_const_int32(gallivm
, 2);
4177 LLVMValueRef imm6
= lp_build_const_int32(gallivm
, 6);
4179 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
4180 z
= LLVMBuildSDiv(builder
, z
, imm6
, "");
4181 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
4185 emit_data
->output
[emit_data
->chan
] = out
;
4188 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
4189 struct lp_build_emit_data
*emit_data
,
4190 unsigned opcode
, unsigned target
,
4191 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
4192 LLVMValueRef
*param
, unsigned count
,
4195 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4197 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
4199 /* Pad to power of two vector */
4200 while (count
< util_next_power_of_two(count
))
4201 param
[count
++] = LLVMGetUndef(ctx
->i32
);
4203 /* Texture coordinates. */
4205 emit_data
->args
[0] = lp_build_gather_values(gallivm
, param
, count
);
4207 emit_data
->args
[0] = param
[0];
4210 emit_data
->args
[1] = res_ptr
;
4213 if (opcode
== TGSI_OPCODE_TXF
|| opcode
== TGSI_OPCODE_TXQ
)
4214 emit_data
->dst_type
= ctx
->v4i32
;
4216 emit_data
->dst_type
= ctx
->v4f32
;
4218 emit_data
->args
[num_args
++] = samp_ptr
;
4221 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, dmask
);
4222 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
4223 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* r128 */
4224 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
,
4225 tgsi_is_array_sampler(target
)); /* da */
4226 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* glc */
4227 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* slc */
4228 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* tfe */
4229 emit_data
->args
[num_args
++] = lp_build_const_int32(gallivm
, 0); /* lwe */
4231 emit_data
->arg_count
= num_args
;
4234 static const struct lp_build_tgsi_action tex_action
;
4244 * Load an image view, fmask view. or sampler state descriptor.
4246 static LLVMValueRef
load_sampler_desc_custom(struct si_shader_context
*ctx
,
4247 LLVMValueRef list
, LLVMValueRef index
,
4248 enum desc_type type
)
4250 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
4251 LLVMBuilderRef builder
= gallivm
->builder
;
4255 /* The image is at [0:7]. */
4256 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4259 /* The buffer is in [4:7]. */
4260 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4261 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4262 list
= LLVMBuildPointerCast(builder
, list
,
4263 const_array(ctx
->v4i32
, 0), "");
4266 /* The FMASK is at [8:15]. */
4267 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
4268 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 1, 0), "");
4271 /* The sampler state is at [12:15]. */
4272 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4273 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
4274 list
= LLVMBuildPointerCast(builder
, list
,
4275 const_array(ctx
->v4i32
, 0), "");
4279 return build_indexed_load_const(ctx
, list
, index
);
4282 static LLVMValueRef
load_sampler_desc(struct si_shader_context
*ctx
,
4283 LLVMValueRef index
, enum desc_type type
)
4285 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
,
4288 return load_sampler_desc_custom(ctx
, list
, index
, type
);
4291 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4294 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4295 * filtering manually. The driver sets img7 to a mask clearing
4296 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4297 * s_and_b32 samp0, samp0, img7
4300 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4302 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
4303 LLVMValueRef res
, LLVMValueRef samp
)
4305 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4306 LLVMValueRef img7
, samp0
;
4308 if (ctx
->screen
->b
.chip_class
>= VI
)
4311 img7
= LLVMBuildExtractElement(builder
, res
,
4312 LLVMConstInt(ctx
->i32
, 7, 0), "");
4313 samp0
= LLVMBuildExtractElement(builder
, samp
,
4314 LLVMConstInt(ctx
->i32
, 0, 0), "");
4315 samp0
= LLVMBuildAnd(builder
, samp0
, img7
, "");
4316 return LLVMBuildInsertElement(builder
, samp
, samp0
,
4317 LLVMConstInt(ctx
->i32
, 0, 0), "");
4320 static void tex_fetch_ptrs(
4321 struct lp_build_tgsi_context
*bld_base
,
4322 struct lp_build_emit_data
*emit_data
,
4323 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
4325 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4326 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4327 unsigned target
= inst
->Texture
.Texture
;
4328 unsigned sampler_src
;
4329 unsigned sampler_index
;
4332 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
4333 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
4335 if (emit_data
->inst
->Src
[sampler_src
].Register
.Indirect
) {
4336 const struct tgsi_full_src_register
*reg
= &emit_data
->inst
->Src
[sampler_src
];
4338 index
= get_bounded_indirect_index(ctx
,
4340 reg
->Register
.Index
,
4343 index
= LLVMConstInt(ctx
->i32
, sampler_index
, 0);
4346 if (target
== TGSI_TEXTURE_BUFFER
)
4347 *res_ptr
= load_sampler_desc(ctx
, index
, DESC_BUFFER
);
4349 *res_ptr
= load_sampler_desc(ctx
, index
, DESC_IMAGE
);
4356 if (target
== TGSI_TEXTURE_2D_MSAA
||
4357 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4359 *fmask_ptr
= load_sampler_desc(ctx
, index
, DESC_FMASK
);
4360 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
4362 *samp_ptr
= load_sampler_desc(ctx
, index
, DESC_SAMPLER
);
4363 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
4368 static void txq_fetch_args(
4369 struct lp_build_tgsi_context
*bld_base
,
4370 struct lp_build_emit_data
*emit_data
)
4372 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4373 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4374 unsigned target
= inst
->Texture
.Texture
;
4375 LLVMValueRef res_ptr
;
4376 LLVMValueRef address
;
4378 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, NULL
, NULL
);
4380 if (target
== TGSI_TEXTURE_BUFFER
) {
4381 /* Read the size from the buffer descriptor directly. */
4382 emit_data
->args
[0] = get_buffer_size(bld_base
, res_ptr
);
4386 /* Textures - set the mip level. */
4387 address
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
4389 set_tex_fetch_args(ctx
, emit_data
, TGSI_OPCODE_TXQ
, target
, res_ptr
,
4390 NULL
, &address
, 1, 0xf);
4393 static void txq_emit(const struct lp_build_tgsi_action
*action
,
4394 struct lp_build_tgsi_context
*bld_base
,
4395 struct lp_build_emit_data
*emit_data
)
4397 struct lp_build_context
*base
= &bld_base
->base
;
4398 unsigned target
= emit_data
->inst
->Texture
.Texture
;
4400 if (target
== TGSI_TEXTURE_BUFFER
) {
4401 /* Just return the buffer size. */
4402 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
4406 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4407 base
->gallivm
->builder
, "llvm.SI.getresinfo.i32",
4408 emit_data
->dst_type
, emit_data
->args
, emit_data
->arg_count
,
4409 LP_FUNC_ATTR_READNONE
);
4411 /* Divide the number of layers by 6 to get the number of cubes. */
4412 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
4413 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4414 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
4415 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
4416 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
4418 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
4419 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
4420 z
= LLVMBuildSDiv(builder
, z
, six
, "");
4422 emit_data
->output
[emit_data
->chan
] =
4423 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
4427 static void tex_fetch_args(
4428 struct lp_build_tgsi_context
*bld_base
,
4429 struct lp_build_emit_data
*emit_data
)
4431 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4432 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4433 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4434 unsigned opcode
= inst
->Instruction
.Opcode
;
4435 unsigned target
= inst
->Texture
.Texture
;
4436 LLVMValueRef coords
[5], derivs
[6];
4437 LLVMValueRef address
[16];
4438 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
4439 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
4442 unsigned num_deriv_channels
= 0;
4443 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
4444 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4445 unsigned dmask
= 0xf;
4447 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4449 if (target
== TGSI_TEXTURE_BUFFER
) {
4450 emit_data
->dst_type
= ctx
->v4f32
;
4451 emit_data
->args
[0] = LLVMBuildBitCast(gallivm
->builder
, res_ptr
,
4453 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
4454 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
4455 emit_data
->arg_count
= 3;
4459 /* Fetch and project texture coordinates */
4460 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
4461 for (chan
= 0; chan
< 3; chan
++ ) {
4462 coords
[chan
] = lp_build_emit_fetch(bld_base
,
4465 if (opcode
== TGSI_OPCODE_TXP
)
4466 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
4472 if (opcode
== TGSI_OPCODE_TXP
)
4473 coords
[3] = bld_base
->base
.one
;
4476 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
4477 /* The offsets are six-bit signed integers packed like this:
4478 * X=[5:0], Y=[13:8], and Z=[21:16].
4480 LLVMValueRef offset
[3], pack
;
4482 assert(inst
->Texture
.NumOffsets
== 1);
4484 for (chan
= 0; chan
< 3; chan
++) {
4485 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
4486 emit_data
->inst
, 0, chan
);
4487 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
4488 lp_build_const_int32(gallivm
, 0x3f), "");
4490 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
4491 lp_build_const_int32(gallivm
, chan
*8), "");
4494 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
4495 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
4496 address
[count
++] = pack
;
4499 /* Pack LOD bias value */
4500 if (opcode
== TGSI_OPCODE_TXB
)
4501 address
[count
++] = coords
[3];
4502 if (opcode
== TGSI_OPCODE_TXB2
)
4503 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4505 /* Pack depth comparison value */
4506 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
4509 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
4510 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4512 assert(ref_pos
>= 0);
4513 z
= coords
[ref_pos
];
4516 /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
4517 * so the depth comparison value isn't clamped for Z16 and
4518 * Z24 anymore. Do it manually here.
4520 * It's unnecessary if the original texture format was
4521 * Z32_FLOAT, but we don't know that here.
4523 if (ctx
->screen
->b
.chip_class
== VI
)
4524 z
= si_llvm_saturate(bld_base
, z
);
4526 address
[count
++] = z
;
4529 /* Pack user derivatives */
4530 if (opcode
== TGSI_OPCODE_TXD
) {
4531 int param
, num_src_deriv_channels
;
4534 case TGSI_TEXTURE_3D
:
4535 num_src_deriv_channels
= 3;
4536 num_deriv_channels
= 3;
4538 case TGSI_TEXTURE_2D
:
4539 case TGSI_TEXTURE_SHADOW2D
:
4540 case TGSI_TEXTURE_RECT
:
4541 case TGSI_TEXTURE_SHADOWRECT
:
4542 case TGSI_TEXTURE_2D_ARRAY
:
4543 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4544 num_src_deriv_channels
= 2;
4545 num_deriv_channels
= 2;
4547 case TGSI_TEXTURE_CUBE
:
4548 case TGSI_TEXTURE_SHADOWCUBE
:
4549 case TGSI_TEXTURE_CUBE_ARRAY
:
4550 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
4551 /* Cube derivatives will be converted to 2D. */
4552 num_src_deriv_channels
= 3;
4553 num_deriv_channels
= 2;
4555 case TGSI_TEXTURE_1D
:
4556 case TGSI_TEXTURE_SHADOW1D
:
4557 case TGSI_TEXTURE_1D_ARRAY
:
4558 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4559 num_src_deriv_channels
= 1;
4560 num_deriv_channels
= 1;
4563 unreachable("invalid target");
4566 for (param
= 0; param
< 2; param
++)
4567 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
4568 derivs
[param
* num_src_deriv_channels
+ chan
] =
4569 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
4572 if (target
== TGSI_TEXTURE_CUBE
||
4573 target
== TGSI_TEXTURE_CUBE_ARRAY
||
4574 target
== TGSI_TEXTURE_SHADOWCUBE
||
4575 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
4576 si_prepare_cube_coords(bld_base
, emit_data
, coords
, derivs
);
4578 if (opcode
== TGSI_OPCODE_TXD
)
4579 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
4580 address
[count
++] = derivs
[i
];
4582 /* Pack texture coordinates */
4583 address
[count
++] = coords
[0];
4585 address
[count
++] = coords
[1];
4587 address
[count
++] = coords
[2];
4589 /* Pack LOD or sample index */
4590 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
4591 address
[count
++] = coords
[3];
4592 else if (opcode
== TGSI_OPCODE_TXL2
)
4593 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
4596 assert(!"Cannot handle more than 16 texture address parameters");
4600 for (chan
= 0; chan
< count
; chan
++ ) {
4601 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
4602 address
[chan
], ctx
->i32
, "");
4605 /* Adjust the sample index according to FMASK.
4607 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4608 * which is the identity mapping. Each nibble says which physical sample
4609 * should be fetched to get that sample.
4611 * For example, 0x11111100 means there are only 2 samples stored and
4612 * the second sample covers 3/4 of the pixel. When reading samples 0
4613 * and 1, return physical sample 0 (determined by the first two 0s
4614 * in FMASK), otherwise return physical sample 1.
4616 * The sample index should be adjusted as follows:
4617 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4619 if (target
== TGSI_TEXTURE_2D_MSAA
||
4620 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
4621 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4622 struct lp_build_emit_data txf_emit_data
= *emit_data
;
4623 LLVMValueRef txf_address
[4];
4624 unsigned txf_count
= count
;
4625 struct tgsi_full_instruction inst
= {};
4627 memcpy(txf_address
, address
, sizeof(txf_address
));
4629 if (target
== TGSI_TEXTURE_2D_MSAA
) {
4630 txf_address
[2] = bld_base
->uint_bld
.zero
;
4632 txf_address
[3] = bld_base
->uint_bld
.zero
;
4634 /* Read FMASK using TXF. */
4635 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
4636 inst
.Texture
.Texture
= target
;
4637 txf_emit_data
.inst
= &inst
;
4638 txf_emit_data
.chan
= 0;
4639 set_tex_fetch_args(ctx
, &txf_emit_data
, TGSI_OPCODE_TXF
,
4640 target
, fmask_ptr
, NULL
,
4641 txf_address
, txf_count
, 0xf);
4642 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
4644 /* Initialize some constants. */
4645 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
4646 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
4648 /* Apply the formula. */
4649 LLVMValueRef fmask
=
4650 LLVMBuildExtractElement(gallivm
->builder
,
4651 txf_emit_data
.output
[0],
4652 uint_bld
->zero
, "");
4654 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
4656 LLVMValueRef sample_index4
=
4657 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
4659 LLVMValueRef shifted_fmask
=
4660 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
4662 LLVMValueRef final_sample
=
4663 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
4665 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4666 * resource descriptor is 0 (invalid),
4668 LLVMValueRef fmask_desc
=
4669 LLVMBuildBitCast(gallivm
->builder
, fmask_ptr
,
4672 LLVMValueRef fmask_word1
=
4673 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
4676 LLVMValueRef word1_is_nonzero
=
4677 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
4678 fmask_word1
, uint_bld
->zero
, "");
4680 /* Replace the MSAA sample index. */
4681 address
[sample_chan
] =
4682 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
4683 final_sample
, address
[sample_chan
], "");
4686 if (opcode
== TGSI_OPCODE_TXF
) {
4687 /* add tex offsets */
4688 if (inst
->Texture
.NumOffsets
) {
4689 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
4690 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
4691 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
4693 assert(inst
->Texture
.NumOffsets
== 1);
4696 case TGSI_TEXTURE_3D
:
4697 address
[2] = lp_build_add(uint_bld
, address
[2],
4698 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
4700 case TGSI_TEXTURE_2D
:
4701 case TGSI_TEXTURE_SHADOW2D
:
4702 case TGSI_TEXTURE_RECT
:
4703 case TGSI_TEXTURE_SHADOWRECT
:
4704 case TGSI_TEXTURE_2D_ARRAY
:
4705 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
4707 lp_build_add(uint_bld
, address
[1],
4708 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
4710 case TGSI_TEXTURE_1D
:
4711 case TGSI_TEXTURE_SHADOW1D
:
4712 case TGSI_TEXTURE_1D_ARRAY
:
4713 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
4715 lp_build_add(uint_bld
, address
[0],
4716 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
4718 /* texture offsets do not apply to other texture targets */
4723 if (opcode
== TGSI_OPCODE_TG4
) {
4724 unsigned gather_comp
= 0;
4726 /* DMASK was repurposed for GATHER4. 4 components are always
4727 * returned and DMASK works like a swizzle - it selects
4728 * the component to fetch. The only valid DMASK values are
4729 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4730 * (red,red,red,red) etc.) The ISA document doesn't mention
4734 /* Get the component index from src1.x for Gather4. */
4735 if (!tgsi_is_shadow_target(target
)) {
4736 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
4737 LLVMValueRef comp_imm
;
4738 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
4740 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
4742 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
4743 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
4744 gather_comp
= CLAMP(gather_comp
, 0, 3);
4747 dmask
= 1 << gather_comp
;
4750 set_tex_fetch_args(ctx
, emit_data
, opcode
, target
, res_ptr
,
4751 samp_ptr
, address
, count
, dmask
);
4754 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
4755 * incorrectly forces nearest filtering if the texture format is integer.
4756 * The only effect it has on Gather4, which always returns 4 texels for
4757 * bilinear filtering, is that the final coordinates are off by 0.5 of
4760 * The workaround is to subtract 0.5 from the unnormalized coordinates,
4761 * or (0.5 / size) from the normalized coordinates.
4763 static void si_lower_gather4_integer(struct si_shader_context
*ctx
,
4764 struct lp_build_emit_data
*emit_data
,
4765 const char *intr_name
,
4766 unsigned coord_vgpr_index
)
4768 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
4769 LLVMValueRef coord
= emit_data
->args
[0];
4770 LLVMValueRef half_texel
[2];
4773 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
||
4774 emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWRECT
) {
4775 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
4777 struct tgsi_full_instruction txq_inst
= {};
4778 struct lp_build_emit_data txq_emit_data
= {};
4780 /* Query the texture size. */
4781 txq_inst
.Texture
.Texture
= emit_data
->inst
->Texture
.Texture
;
4782 txq_emit_data
.inst
= &txq_inst
;
4783 txq_emit_data
.dst_type
= ctx
->v4i32
;
4784 set_tex_fetch_args(ctx
, &txq_emit_data
, TGSI_OPCODE_TXQ
,
4785 txq_inst
.Texture
.Texture
,
4786 emit_data
->args
[1], NULL
,
4787 &ctx
->soa
.bld_base
.uint_bld
.zero
,
4789 txq_emit(NULL
, &ctx
->soa
.bld_base
, &txq_emit_data
);
4791 /* Compute -0.5 / size. */
4792 for (c
= 0; c
< 2; c
++) {
4794 LLVMBuildExtractElement(builder
, txq_emit_data
.output
[0],
4795 LLVMConstInt(ctx
->i32
, c
, 0), "");
4796 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
4798 lp_build_emit_llvm_unary(&ctx
->soa
.bld_base
,
4799 TGSI_OPCODE_RCP
, half_texel
[c
]);
4800 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
4801 LLVMConstReal(ctx
->f32
, -0.5), "");
4805 for (c
= 0; c
< 2; c
++) {
4807 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
4809 tmp
= LLVMBuildExtractElement(builder
, coord
, index
, "");
4810 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->f32
, "");
4811 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
4812 tmp
= LLVMBuildBitCast(builder
, tmp
, ctx
->i32
, "");
4813 coord
= LLVMBuildInsertElement(builder
, coord
, tmp
, index
, "");
4816 emit_data
->args
[0] = coord
;
4817 emit_data
->output
[emit_data
->chan
] =
4818 lp_build_intrinsic(builder
, intr_name
, emit_data
->dst_type
,
4819 emit_data
->args
, emit_data
->arg_count
,
4820 LP_FUNC_ATTR_READNONE
);
4823 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
4824 struct lp_build_tgsi_context
*bld_base
,
4825 struct lp_build_emit_data
*emit_data
)
4827 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4828 struct lp_build_context
*base
= &bld_base
->base
;
4829 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
4830 unsigned opcode
= inst
->Instruction
.Opcode
;
4831 unsigned target
= inst
->Texture
.Texture
;
4832 char intr_name
[127];
4833 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
4834 bool is_shadow
= tgsi_is_shadow_target(target
);
4836 const char *name
= "llvm.SI.image.sample";
4837 const char *infix
= "";
4839 if (target
== TGSI_TEXTURE_BUFFER
) {
4840 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4841 base
->gallivm
->builder
,
4842 "llvm.SI.vs.load.input", emit_data
->dst_type
,
4843 emit_data
->args
, emit_data
->arg_count
,
4844 LP_FUNC_ATTR_READNONE
);
4849 case TGSI_OPCODE_TXF
:
4850 name
= target
== TGSI_TEXTURE_2D_MSAA
||
4851 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
4852 "llvm.SI.image.load" :
4853 "llvm.SI.image.load.mip";
4857 case TGSI_OPCODE_LODQ
:
4858 name
= "llvm.SI.getlod";
4862 case TGSI_OPCODE_TEX
:
4863 case TGSI_OPCODE_TEX2
:
4864 case TGSI_OPCODE_TXP
:
4865 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
4868 case TGSI_OPCODE_TXB
:
4869 case TGSI_OPCODE_TXB2
:
4870 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
4873 case TGSI_OPCODE_TXL
:
4874 case TGSI_OPCODE_TXL2
:
4877 case TGSI_OPCODE_TXD
:
4880 case TGSI_OPCODE_TG4
:
4881 name
= "llvm.SI.gather4";
4889 /* Add the type and suffixes .c, .o if needed. */
4890 build_type_name_for_intr(LLVMTypeOf(emit_data
->args
[0]), type
, sizeof(type
));
4891 sprintf(intr_name
, "%s%s%s%s.%s",
4892 name
, is_shadow
? ".c" : "", infix
,
4893 has_offset
? ".o" : "", type
);
4895 /* The hardware needs special lowering for Gather4 with integer formats. */
4896 if (opcode
== TGSI_OPCODE_TG4
) {
4897 struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
4898 /* This will also work with non-constant indexing because of how
4899 * glsl_to_tgsi works and we intent to preserve that behavior.
4901 const unsigned src_idx
= 2;
4902 unsigned sampler
= inst
->Src
[src_idx
].Register
.Index
;
4904 assert(inst
->Src
[src_idx
].Register
.File
== TGSI_FILE_SAMPLER
);
4906 if (info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_SINT
||
4907 info
->sampler_type
[sampler
] == TGSI_RETURN_TYPE_UINT
) {
4908 /* Texture coordinates start after:
4909 * {offset, bias, z-compare, derivatives}
4910 * Only the offset and z-compare can occur here.
4912 si_lower_gather4_integer(ctx
, emit_data
, intr_name
,
4913 (int)has_offset
+ (int)is_shadow
);
4918 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
4919 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
4920 emit_data
->args
, emit_data
->arg_count
,
4921 LP_FUNC_ATTR_READNONE
);
4924 static void si_llvm_emit_txqs(
4925 const struct lp_build_tgsi_action
*action
,
4926 struct lp_build_tgsi_context
*bld_base
,
4927 struct lp_build_emit_data
*emit_data
)
4929 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4930 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4931 LLVMBuilderRef builder
= gallivm
->builder
;
4932 LLVMValueRef res
, samples
;
4933 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
4935 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
4938 /* Read the samples from the descriptor directly. */
4939 res
= LLVMBuildBitCast(builder
, res_ptr
, ctx
->v8i32
, "");
4940 samples
= LLVMBuildExtractElement(
4942 lp_build_const_int32(gallivm
, 3), "");
4943 samples
= LLVMBuildLShr(builder
, samples
,
4944 lp_build_const_int32(gallivm
, 16), "");
4945 samples
= LLVMBuildAnd(builder
, samples
,
4946 lp_build_const_int32(gallivm
, 0xf), "");
4947 samples
= LLVMBuildShl(builder
, lp_build_const_int32(gallivm
, 1),
4950 emit_data
->output
[emit_data
->chan
] = samples
;
4954 * SI implements derivatives using the local data store (LDS)
4955 * All writes to the LDS happen in all executing threads at
4956 * the same time. TID is the Thread ID for the current
4957 * thread and is a value between 0 and 63, representing
4958 * the thread's position in the wavefront.
4960 * For the pixel shader threads are grouped into quads of four pixels.
4961 * The TIDs of the pixels of a quad are:
4969 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
4970 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
4971 * the current pixel's column, and masking with 0xfffffffe yields the TID
4972 * of the left pixel of the current pixel's row.
4974 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
4975 * adding 2 yields the TID of the pixel below the top pixel.
4977 /* masks for thread ID. */
4978 #define TID_MASK_TOP_LEFT 0xfffffffc
4979 #define TID_MASK_TOP 0xfffffffd
4980 #define TID_MASK_LEFT 0xfffffffe
4982 static void si_llvm_emit_ddxy(
4983 const struct lp_build_tgsi_action
*action
,
4984 struct lp_build_tgsi_context
*bld_base
,
4985 struct lp_build_emit_data
*emit_data
)
4987 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
4988 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
4989 unsigned opcode
= emit_data
->info
->opcode
;
4990 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, val
, args
[2];
4994 thread_id
= get_thread_id(ctx
);
4996 if (opcode
== TGSI_OPCODE_DDX_FINE
)
4997 mask
= TID_MASK_LEFT
;
4998 else if (opcode
== TGSI_OPCODE_DDY_FINE
)
4999 mask
= TID_MASK_TOP
;
5001 mask
= TID_MASK_TOP_LEFT
;
5003 tl_tid
= LLVMBuildAnd(gallivm
->builder
, thread_id
,
5004 lp_build_const_int32(gallivm
, mask
), "");
5006 /* for DDX we want to next X pixel, DDY next Y pixel. */
5007 idx
= (opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
) ? 1 : 2;
5008 trbl_tid
= LLVMBuildAdd(gallivm
->builder
, tl_tid
,
5009 lp_build_const_int32(gallivm
, idx
), "");
5011 val
= LLVMBuildBitCast(gallivm
->builder
, emit_data
->args
[0], ctx
->i32
, "");
5013 if (ctx
->screen
->has_ds_bpermute
) {
5014 args
[0] = LLVMBuildMul(gallivm
->builder
, tl_tid
,
5015 lp_build_const_int32(gallivm
, 4), "");
5017 tl
= lp_build_intrinsic(gallivm
->builder
,
5018 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
5019 args
, 2, LP_FUNC_ATTR_READNONE
);
5021 args
[0] = LLVMBuildMul(gallivm
->builder
, trbl_tid
,
5022 lp_build_const_int32(gallivm
, 4), "");
5023 trbl
= lp_build_intrinsic(gallivm
->builder
,
5024 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
5025 args
, 2, LP_FUNC_ATTR_READNONE
);
5027 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
5029 store_ptr
= build_gep0(ctx
, ctx
->lds
, thread_id
);
5030 load_ptr0
= build_gep0(ctx
, ctx
->lds
, tl_tid
);
5031 load_ptr1
= build_gep0(ctx
, ctx
->lds
, trbl_tid
);
5033 LLVMBuildStore(gallivm
->builder
, val
, store_ptr
);
5034 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
5035 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
5038 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, ctx
->f32
, "");
5039 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, ctx
->f32
, "");
5041 emit_data
->output
[emit_data
->chan
] =
5042 LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
5046 * this takes an I,J coordinate pair,
5047 * and works out the X and Y derivatives.
5048 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
5050 static LLVMValueRef
si_llvm_emit_ddxy_interp(
5051 struct lp_build_tgsi_context
*bld_base
,
5052 LLVMValueRef interp_ij
)
5054 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5055 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5056 LLVMValueRef result
[4], a
;
5059 for (i
= 0; i
< 2; i
++) {
5060 a
= LLVMBuildExtractElement(gallivm
->builder
, interp_ij
,
5061 LLVMConstInt(ctx
->i32
, i
, 0), "");
5062 result
[i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDX
, a
);
5063 result
[2+i
] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_DDY
, a
);
5066 return lp_build_gather_values(gallivm
, result
, 4);
5069 static void interp_fetch_args(
5070 struct lp_build_tgsi_context
*bld_base
,
5071 struct lp_build_emit_data
*emit_data
)
5073 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5074 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5075 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
5077 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
) {
5078 /* offset is in second src, first two channels */
5079 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
,
5082 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
,
5085 emit_data
->arg_count
= 2;
5086 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
5087 LLVMValueRef sample_position
;
5088 LLVMValueRef sample_id
;
5089 LLVMValueRef halfval
= lp_build_const_float(gallivm
, 0.5f
);
5091 /* fetch sample ID, then fetch its sample position,
5092 * and place into first two channels.
5094 sample_id
= lp_build_emit_fetch(bld_base
,
5095 emit_data
->inst
, 1, TGSI_CHAN_X
);
5096 sample_id
= LLVMBuildBitCast(gallivm
->builder
, sample_id
,
5098 sample_position
= load_sample_position(ctx
, sample_id
);
5100 emit_data
->args
[0] = LLVMBuildExtractElement(gallivm
->builder
,
5102 lp_build_const_int32(gallivm
, 0), "");
5104 emit_data
->args
[0] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[0], halfval
, "");
5105 emit_data
->args
[1] = LLVMBuildExtractElement(gallivm
->builder
,
5107 lp_build_const_int32(gallivm
, 1), "");
5108 emit_data
->args
[1] = LLVMBuildFSub(gallivm
->builder
, emit_data
->args
[1], halfval
, "");
5109 emit_data
->arg_count
= 2;
5113 static void build_interp_intrinsic(const struct lp_build_tgsi_action
*action
,
5114 struct lp_build_tgsi_context
*bld_base
,
5115 struct lp_build_emit_data
*emit_data
)
5117 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5118 struct si_shader
*shader
= ctx
->shader
;
5119 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5120 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5121 LLVMValueRef interp_param
;
5122 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
5123 int input_index
= inst
->Src
[0].Register
.Index
;
5126 LLVMValueRef attr_number
;
5127 LLVMValueRef params
= LLVMGetParam(ctx
->main_fn
, SI_PARAM_PRIM_MASK
);
5128 int interp_param_idx
;
5129 unsigned interp
= shader
->selector
->info
.input_interpolate
[input_index
];
5132 assert(inst
->Src
[0].Register
.File
== TGSI_FILE_INPUT
);
5134 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5135 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
)
5136 location
= TGSI_INTERPOLATE_LOC_CENTER
;
5138 location
= TGSI_INTERPOLATE_LOC_CENTROID
;
5140 interp_param_idx
= lookup_interp_param_index(interp
, location
);
5141 if (interp_param_idx
== -1)
5143 else if (interp_param_idx
)
5144 interp_param
= LLVMGetParam(ctx
->main_fn
, interp_param_idx
);
5146 interp_param
= NULL
;
5148 attr_number
= lp_build_const_int32(gallivm
, input_index
);
5150 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
5151 inst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
5152 LLVMValueRef ij_out
[2];
5153 LLVMValueRef ddxy_out
= si_llvm_emit_ddxy_interp(bld_base
, interp_param
);
5156 * take the I then J parameters, and the DDX/Y for it, and
5157 * calculate the IJ inputs for the interpolator.
5158 * temp1 = ddx * offset/sample.x + I;
5159 * interp_param.I = ddy * offset/sample.y + temp1;
5160 * temp1 = ddx * offset/sample.x + J;
5161 * interp_param.J = ddy * offset/sample.y + temp1;
5163 for (i
= 0; i
< 2; i
++) {
5164 LLVMValueRef ix_ll
= lp_build_const_int32(gallivm
, i
);
5165 LLVMValueRef iy_ll
= lp_build_const_int32(gallivm
, i
+ 2);
5166 LLVMValueRef ddx_el
= LLVMBuildExtractElement(gallivm
->builder
,
5167 ddxy_out
, ix_ll
, "");
5168 LLVMValueRef ddy_el
= LLVMBuildExtractElement(gallivm
->builder
,
5169 ddxy_out
, iy_ll
, "");
5170 LLVMValueRef interp_el
= LLVMBuildExtractElement(gallivm
->builder
,
5171 interp_param
, ix_ll
, "");
5172 LLVMValueRef temp1
, temp2
;
5174 interp_el
= LLVMBuildBitCast(gallivm
->builder
, interp_el
,
5177 temp1
= LLVMBuildFMul(gallivm
->builder
, ddx_el
, emit_data
->args
[0], "");
5179 temp1
= LLVMBuildFAdd(gallivm
->builder
, temp1
, interp_el
, "");
5181 temp2
= LLVMBuildFMul(gallivm
->builder
, ddy_el
, emit_data
->args
[1], "");
5183 ij_out
[i
] = LLVMBuildFAdd(gallivm
->builder
, temp2
, temp1
, "");
5185 interp_param
= lp_build_gather_values(bld_base
->base
.gallivm
, ij_out
, 2);
5188 for (chan
= 0; chan
< 4; chan
++) {
5189 LLVMValueRef llvm_chan
;
5192 schan
= tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], chan
);
5193 llvm_chan
= lp_build_const_int32(gallivm
, schan
);
5196 interp_param
= LLVMBuildBitCast(gallivm
->builder
,
5197 interp_param
, LLVMVectorType(ctx
->f32
, 2), "");
5198 LLVMValueRef i
= LLVMBuildExtractElement(
5199 gallivm
->builder
, interp_param
, uint
->zero
, "");
5200 LLVMValueRef j
= LLVMBuildExtractElement(
5201 gallivm
->builder
, interp_param
, uint
->one
, "");
5202 emit_data
->output
[chan
] = build_fs_interp(bld_base
,
5203 llvm_chan
, attr_number
, params
,
5206 emit_data
->output
[chan
] = build_fs_interp_mov(bld_base
,
5207 lp_build_const_int32(gallivm
, 2), /* P0 */
5208 llvm_chan
, attr_number
, params
);
5213 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context
*bld_base
,
5214 struct lp_build_emit_data
*emit_data
)
5216 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
5217 struct tgsi_src_register src0
= emit_data
->inst
->Src
[0].Register
;
5220 assert(src0
.File
== TGSI_FILE_IMMEDIATE
);
5222 stream
= LLVMConstIntGetZExtValue(imms
[src0
.Index
][src0
.SwizzleX
]) & 0x3;
5226 /* Emit one vertex from the geometry shader */
5227 static void si_llvm_emit_vertex(
5228 const struct lp_build_tgsi_action
*action
,
5229 struct lp_build_tgsi_context
*bld_base
,
5230 struct lp_build_emit_data
*emit_data
)
5232 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5233 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5234 struct si_shader
*shader
= ctx
->shader
;
5235 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
5236 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5237 LLVMValueRef soffset
= LLVMGetParam(ctx
->main_fn
,
5238 SI_PARAM_GS2VS_OFFSET
);
5239 LLVMValueRef gs_next_vertex
;
5240 LLVMValueRef can_emit
, kill
;
5241 LLVMValueRef args
[2];
5246 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5248 /* Write vertex attribute values to GSVS ring */
5249 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
,
5250 ctx
->gs_next_vertex
[stream
],
5253 /* If this thread has already emitted the declared maximum number of
5254 * vertices, kill it: excessive vertex emissions are not supposed to
5255 * have any effect, and GS threads have no externally observable
5256 * effects other than emitting vertices.
5258 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
5259 lp_build_const_int32(gallivm
,
5260 shader
->selector
->gs_max_out_vertices
), "");
5261 kill
= lp_build_select(&bld_base
->base
, can_emit
,
5262 lp_build_const_float(gallivm
, 1.0f
),
5263 lp_build_const_float(gallivm
, -1.0f
));
5265 lp_build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
5266 ctx
->voidt
, &kill
, 1, 0);
5268 for (i
= 0; i
< info
->num_outputs
; i
++) {
5269 LLVMValueRef
*out_ptr
=
5270 ctx
->soa
.outputs
[i
];
5272 for (chan
= 0; chan
< 4; chan
++) {
5273 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
5274 LLVMValueRef voffset
=
5275 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
5276 shader
->selector
->gs_max_out_vertices
);
5278 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
5279 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
5281 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, ctx
->i32
, "");
5283 build_tbuffer_store(ctx
,
5284 ctx
->gsvs_ring
[stream
],
5286 voffset
, soffset
, 0,
5287 V_008F0C_BUF_DATA_FORMAT_32
,
5288 V_008F0C_BUF_NUM_FORMAT_UINT
,
5292 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
5293 lp_build_const_int32(gallivm
, 1));
5295 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
5297 /* Signal vertex emission */
5298 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
| (stream
<< 8));
5299 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
5300 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
5301 ctx
->voidt
, args
, 2, 0);
5304 /* Cut one primitive from the geometry shader */
5305 static void si_llvm_emit_primitive(
5306 const struct lp_build_tgsi_action
*action
,
5307 struct lp_build_tgsi_context
*bld_base
,
5308 struct lp_build_emit_data
*emit_data
)
5310 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5311 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5312 LLVMValueRef args
[2];
5315 /* Signal primitive cut */
5316 stream
= si_llvm_get_stream(bld_base
, emit_data
);
5317 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
| (stream
<< 8));
5318 args
[1] = LLVMGetParam(ctx
->main_fn
, SI_PARAM_GS_WAVE_ID
);
5319 lp_build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
5320 ctx
->voidt
, args
, 2, 0);
5323 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action
*action
,
5324 struct lp_build_tgsi_context
*bld_base
,
5325 struct lp_build_emit_data
*emit_data
)
5327 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
5328 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5330 /* The real barrier instruction isn’t needed, because an entire patch
5331 * always fits into a single wave.
5333 if (ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
5334 emit_optimization_barrier(ctx
);
5338 lp_build_intrinsic(gallivm
->builder
,
5339 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.s.barrier"
5340 : "llvm.AMDGPU.barrier.local",
5341 ctx
->voidt
, NULL
, 0, 0);
5344 static const struct lp_build_tgsi_action tex_action
= {
5345 .fetch_args
= tex_fetch_args
,
5346 .emit
= build_tex_intrinsic
,
5349 static const struct lp_build_tgsi_action interp_action
= {
5350 .fetch_args
= interp_fetch_args
,
5351 .emit
= build_interp_intrinsic
,
5354 static void si_create_function(struct si_shader_context
*ctx
,
5356 LLVMTypeRef
*returns
, unsigned num_returns
,
5357 LLVMTypeRef
*params
, unsigned num_params
,
5362 si_llvm_create_func(ctx
, name
, returns
, num_returns
,
5363 params
, num_params
);
5364 si_llvm_shader_type(ctx
->main_fn
, ctx
->type
);
5365 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
5367 for (i
= 0; i
<= last_sgpr
; ++i
) {
5368 LLVMValueRef P
= LLVMGetParam(ctx
->main_fn
, i
);
5370 /* The combination of:
5374 * allows the optimization passes to move loads and reduces
5375 * SGPR spilling significantly.
5377 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
5378 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_BYVAL
);
5379 lp_add_attr_dereferenceable(P
, UINT64_MAX
);
5381 lp_add_function_attr(ctx
->main_fn
, i
+ 1, LP_FUNC_ATTR_INREG
);
5384 if (ctx
->screen
->b
.debug_flags
& DBG_UNSAFE_MATH
) {
5385 /* These were copied from some LLVM test. */
5386 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5387 "less-precise-fpmad",
5389 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5392 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5395 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
5401 static void create_meta_data(struct si_shader_context
*ctx
)
5403 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
5405 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(gallivm
->context
,
5406 "invariant.load", 14);
5407 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(gallivm
->context
,
5409 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(gallivm
->context
,
5410 "amdgpu.uniform", 14);
5412 ctx
->empty_md
= LLVMMDNodeInContext(gallivm
->context
, NULL
, 0);
5415 static void declare_streamout_params(struct si_shader_context
*ctx
,
5416 struct pipe_stream_output_info
*so
,
5417 LLVMTypeRef
*params
, LLVMTypeRef i32
,
5418 unsigned *num_params
)
5422 /* Streamout SGPRs. */
5423 if (so
->num_outputs
) {
5424 if (ctx
->type
!= PIPE_SHADER_TESS_EVAL
)
5425 params
[ctx
->param_streamout_config
= (*num_params
)++] = i32
;
5427 ctx
->param_streamout_config
= ctx
->param_tess_offchip
;
5429 params
[ctx
->param_streamout_write_index
= (*num_params
)++] = i32
;
5431 /* A streamout buffer offset is loaded if the stride is non-zero. */
5432 for (i
= 0; i
< 4; i
++) {
5436 params
[ctx
->param_streamout_offset
[i
] = (*num_params
)++] = i32
;
5440 static unsigned llvm_get_type_size(LLVMTypeRef type
)
5442 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
5445 case LLVMIntegerTypeKind
:
5446 return LLVMGetIntTypeWidth(type
) / 8;
5447 case LLVMFloatTypeKind
:
5449 case LLVMPointerTypeKind
:
5451 case LLVMVectorTypeKind
:
5452 return LLVMGetVectorSize(type
) *
5453 llvm_get_type_size(LLVMGetElementType(type
));
5454 case LLVMArrayTypeKind
:
5455 return LLVMGetArrayLength(type
) *
5456 llvm_get_type_size(LLVMGetElementType(type
));
5463 static void declare_tess_lds(struct si_shader_context
*ctx
)
5465 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
5466 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
5467 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
5469 unsigned lds_size
= ctx
->screen
->b
.chip_class
>= CIK
? 65536 : 32768;
5470 ctx
->lds
= LLVMBuildIntToPtr(gallivm
->builder
, uint
->zero
,
5471 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), LOCAL_ADDR_SPACE
),
5475 static unsigned si_get_max_workgroup_size(struct si_shader
*shader
)
5477 const unsigned *properties
= shader
->selector
->info
.properties
;
5478 unsigned max_work_group_size
=
5479 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] *
5480 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
] *
5481 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
];
5483 if (!max_work_group_size
) {
5484 /* This is a variable group size compute shader,
5485 * compile it for the maximum possible group size.
5487 max_work_group_size
= SI_MAX_VARIABLE_THREADS_PER_BLOCK
;
5489 return max_work_group_size
;
5492 static void create_function(struct si_shader_context
*ctx
)
5494 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
5495 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5496 struct si_shader
*shader
= ctx
->shader
;
5497 LLVMTypeRef params
[SI_NUM_PARAMS
+ SI_NUM_VERTEX_BUFFERS
], v3i32
;
5498 LLVMTypeRef returns
[16+32*4];
5499 unsigned i
, last_sgpr
, num_params
, num_return_sgprs
;
5500 unsigned num_returns
= 0;
5501 unsigned num_prolog_vgprs
= 0;
5503 v3i32
= LLVMVectorType(ctx
->i32
, 3);
5505 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
5506 params
[SI_PARAM_CONST_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_CONST_BUFFERS
);
5507 params
[SI_PARAM_SAMPLERS
] = const_array(ctx
->v8i32
, SI_NUM_SAMPLERS
);
5508 params
[SI_PARAM_IMAGES
] = const_array(ctx
->v8i32
, SI_NUM_IMAGES
);
5509 params
[SI_PARAM_SHADER_BUFFERS
] = const_array(ctx
->v4i32
, SI_NUM_SHADER_BUFFERS
);
5511 switch (ctx
->type
) {
5512 case PIPE_SHADER_VERTEX
:
5513 params
[SI_PARAM_VERTEX_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_VERTEX_BUFFERS
);
5514 params
[SI_PARAM_BASE_VERTEX
] = ctx
->i32
;
5515 params
[SI_PARAM_START_INSTANCE
] = ctx
->i32
;
5516 params
[SI_PARAM_DRAWID
] = ctx
->i32
;
5517 num_params
= SI_PARAM_DRAWID
+1;
5519 if (shader
->key
.as_es
) {
5520 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5521 } else if (shader
->key
.as_ls
) {
5522 params
[SI_PARAM_LS_OUT_LAYOUT
] = ctx
->i32
;
5523 num_params
= SI_PARAM_LS_OUT_LAYOUT
+1;
5525 if (shader
->is_gs_copy_shader
) {
5526 num_params
= SI_PARAM_RW_BUFFERS
+1;
5528 params
[SI_PARAM_VS_STATE_BITS
] = ctx
->i32
;
5529 num_params
= SI_PARAM_VS_STATE_BITS
+1;
5532 /* The locations of the other parameters are assigned dynamically. */
5533 declare_streamout_params(ctx
, &shader
->selector
->so
,
5534 params
, ctx
->i32
, &num_params
);
5537 last_sgpr
= num_params
-1;
5540 params
[ctx
->param_vertex_id
= num_params
++] = ctx
->i32
;
5541 params
[ctx
->param_rel_auto_id
= num_params
++] = ctx
->i32
;
5542 params
[ctx
->param_vs_prim_id
= num_params
++] = ctx
->i32
;
5543 params
[ctx
->param_instance_id
= num_params
++] = ctx
->i32
;
5545 if (!shader
->is_gs_copy_shader
) {
5546 /* Vertex load indices. */
5547 ctx
->param_vertex_index0
= num_params
;
5549 for (i
= 0; i
< shader
->selector
->info
.num_inputs
; i
++)
5550 params
[num_params
++] = ctx
->i32
;
5552 num_prolog_vgprs
+= shader
->selector
->info
.num_inputs
;
5554 /* PrimitiveID output. */
5555 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
)
5556 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5557 returns
[num_returns
++] = ctx
->f32
;
5561 case PIPE_SHADER_TESS_CTRL
:
5562 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5563 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
5564 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
5565 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
5566 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
5567 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
5568 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
5571 params
[SI_PARAM_PATCH_ID
] = ctx
->i32
;
5572 params
[SI_PARAM_REL_IDS
] = ctx
->i32
;
5573 num_params
= SI_PARAM_REL_IDS
+1;
5575 /* SI_PARAM_TCS_OC_LDS and PARAM_TESS_FACTOR_OFFSET are
5576 * placed after the user SGPRs.
5578 for (i
= 0; i
< SI_TCS_NUM_USER_SGPR
+ 2; i
++)
5579 returns
[num_returns
++] = ctx
->i32
; /* SGPRs */
5581 for (i
= 0; i
< 3; i
++)
5582 returns
[num_returns
++] = ctx
->f32
; /* VGPRs */
5585 case PIPE_SHADER_TESS_EVAL
:
5586 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
5587 num_params
= SI_PARAM_TCS_OFFCHIP_LAYOUT
+1;
5589 if (shader
->key
.as_es
) {
5590 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5591 params
[ctx
->param_tess_offchip
= num_params
++] = ctx
->i32
;
5592 params
[ctx
->param_es2gs_offset
= num_params
++] = ctx
->i32
;
5594 params
[ctx
->param_tess_offchip
= num_params
++] = ctx
->i32
;
5595 declare_streamout_params(ctx
, &shader
->selector
->so
,
5596 params
, ctx
->i32
, &num_params
);
5597 params
[ctx
->param_oc_lds
= num_params
++] = ctx
->i32
;
5599 last_sgpr
= num_params
- 1;
5602 params
[ctx
->param_tes_u
= num_params
++] = ctx
->f32
;
5603 params
[ctx
->param_tes_v
= num_params
++] = ctx
->f32
;
5604 params
[ctx
->param_tes_rel_patch_id
= num_params
++] = ctx
->i32
;
5605 params
[ctx
->param_tes_patch_id
= num_params
++] = ctx
->i32
;
5607 /* PrimitiveID output. */
5608 if (!shader
->key
.as_es
)
5609 for (i
= 0; i
<= VS_EPILOG_PRIMID_LOC
; i
++)
5610 returns
[num_returns
++] = ctx
->f32
;
5613 case PIPE_SHADER_GEOMETRY
:
5614 params
[SI_PARAM_GS2VS_OFFSET
] = ctx
->i32
;
5615 params
[SI_PARAM_GS_WAVE_ID
] = ctx
->i32
;
5616 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
5619 params
[SI_PARAM_VTX0_OFFSET
] = ctx
->i32
;
5620 params
[SI_PARAM_VTX1_OFFSET
] = ctx
->i32
;
5621 params
[SI_PARAM_PRIMITIVE_ID
] = ctx
->i32
;
5622 params
[SI_PARAM_VTX2_OFFSET
] = ctx
->i32
;
5623 params
[SI_PARAM_VTX3_OFFSET
] = ctx
->i32
;
5624 params
[SI_PARAM_VTX4_OFFSET
] = ctx
->i32
;
5625 params
[SI_PARAM_VTX5_OFFSET
] = ctx
->i32
;
5626 params
[SI_PARAM_GS_INSTANCE_ID
] = ctx
->i32
;
5627 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
5630 case PIPE_SHADER_FRAGMENT
:
5631 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
5632 params
[SI_PARAM_PRIM_MASK
] = ctx
->i32
;
5633 last_sgpr
= SI_PARAM_PRIM_MASK
;
5634 params
[SI_PARAM_PERSP_SAMPLE
] = ctx
->v2i32
;
5635 params
[SI_PARAM_PERSP_CENTER
] = ctx
->v2i32
;
5636 params
[SI_PARAM_PERSP_CENTROID
] = ctx
->v2i32
;
5637 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
5638 params
[SI_PARAM_LINEAR_SAMPLE
] = ctx
->v2i32
;
5639 params
[SI_PARAM_LINEAR_CENTER
] = ctx
->v2i32
;
5640 params
[SI_PARAM_LINEAR_CENTROID
] = ctx
->v2i32
;
5641 params
[SI_PARAM_LINE_STIPPLE_TEX
] = ctx
->f32
;
5642 params
[SI_PARAM_POS_X_FLOAT
] = ctx
->f32
;
5643 params
[SI_PARAM_POS_Y_FLOAT
] = ctx
->f32
;
5644 params
[SI_PARAM_POS_Z_FLOAT
] = ctx
->f32
;
5645 params
[SI_PARAM_POS_W_FLOAT
] = ctx
->f32
;
5646 params
[SI_PARAM_FRONT_FACE
] = ctx
->i32
;
5647 shader
->info
.face_vgpr_index
= 20;
5648 params
[SI_PARAM_ANCILLARY
] = ctx
->i32
;
5649 params
[SI_PARAM_SAMPLE_COVERAGE
] = ctx
->f32
;
5650 params
[SI_PARAM_POS_FIXED_PT
] = ctx
->i32
;
5651 num_params
= SI_PARAM_POS_FIXED_PT
+1;
5653 /* Color inputs from the prolog. */
5654 if (shader
->selector
->info
.colors_read
) {
5655 unsigned num_color_elements
=
5656 util_bitcount(shader
->selector
->info
.colors_read
);
5658 assert(num_params
+ num_color_elements
<= ARRAY_SIZE(params
));
5659 for (i
= 0; i
< num_color_elements
; i
++)
5660 params
[num_params
++] = ctx
->f32
;
5662 num_prolog_vgprs
+= num_color_elements
;
5665 /* Outputs for the epilog. */
5666 num_return_sgprs
= SI_SGPR_ALPHA_REF
+ 1;
5669 util_bitcount(shader
->selector
->info
.colors_written
) * 4 +
5670 shader
->selector
->info
.writes_z
+
5671 shader
->selector
->info
.writes_stencil
+
5672 shader
->selector
->info
.writes_samplemask
+
5673 1 /* SampleMaskIn */;
5675 num_returns
= MAX2(num_returns
,
5677 PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
5679 for (i
= 0; i
< num_return_sgprs
; i
++)
5680 returns
[i
] = ctx
->i32
;
5681 for (; i
< num_returns
; i
++)
5682 returns
[i
] = ctx
->f32
;
5685 case PIPE_SHADER_COMPUTE
:
5686 params
[SI_PARAM_GRID_SIZE
] = v3i32
;
5687 params
[SI_PARAM_BLOCK_SIZE
] = v3i32
;
5688 params
[SI_PARAM_BLOCK_ID
] = v3i32
;
5689 last_sgpr
= SI_PARAM_BLOCK_ID
;
5691 params
[SI_PARAM_THREAD_ID
] = v3i32
;
5692 num_params
= SI_PARAM_THREAD_ID
+ 1;
5695 assert(0 && "unimplemented shader");
5699 assert(num_params
<= ARRAY_SIZE(params
));
5701 si_create_function(ctx
, "main", returns
, num_returns
, params
,
5702 num_params
, last_sgpr
);
5704 /* Reserve register locations for VGPR inputs the PS prolog may need. */
5705 if (ctx
->type
== PIPE_SHADER_FRAGMENT
&&
5706 ctx
->separate_prolog
) {
5707 si_llvm_add_attribute(ctx
->main_fn
,
5708 "InitialPSInputAddr",
5709 S_0286D0_PERSP_SAMPLE_ENA(1) |
5710 S_0286D0_PERSP_CENTER_ENA(1) |
5711 S_0286D0_PERSP_CENTROID_ENA(1) |
5712 S_0286D0_LINEAR_SAMPLE_ENA(1) |
5713 S_0286D0_LINEAR_CENTER_ENA(1) |
5714 S_0286D0_LINEAR_CENTROID_ENA(1) |
5715 S_0286D0_FRONT_FACE_ENA(1) |
5716 S_0286D0_POS_FIXED_PT_ENA(1));
5717 } else if (ctx
->type
== PIPE_SHADER_COMPUTE
) {
5718 si_llvm_add_attribute(ctx
->main_fn
,
5719 "amdgpu-max-work-group-size",
5720 si_get_max_workgroup_size(shader
));
5723 shader
->info
.num_input_sgprs
= 0;
5724 shader
->info
.num_input_vgprs
= 0;
5726 for (i
= 0; i
<= last_sgpr
; ++i
)
5727 shader
->info
.num_input_sgprs
+= llvm_get_type_size(params
[i
]) / 4;
5729 for (; i
< num_params
; ++i
)
5730 shader
->info
.num_input_vgprs
+= llvm_get_type_size(params
[i
]) / 4;
5732 assert(shader
->info
.num_input_vgprs
>= num_prolog_vgprs
);
5733 shader
->info
.num_input_vgprs
-= num_prolog_vgprs
;
5735 if (!ctx
->screen
->has_ds_bpermute
&&
5737 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
5738 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0 ||
5739 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX_FINE
] > 0 ||
5740 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY_FINE
] > 0 ||
5741 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_OFFSET
] > 0 ||
5742 bld_base
->info
->opcode_count
[TGSI_OPCODE_INTERP_SAMPLE
] > 0))
5744 LLVMAddGlobalInAddressSpace(gallivm
->module
,
5745 LLVMArrayType(ctx
->i32
, 64),
5749 if ((ctx
->type
== PIPE_SHADER_VERTEX
&& shader
->key
.as_ls
) ||
5750 ctx
->type
== PIPE_SHADER_TESS_CTRL
||
5751 ctx
->type
== PIPE_SHADER_TESS_EVAL
)
5752 declare_tess_lds(ctx
);
5756 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5759 static void preload_ring_buffers(struct si_shader_context
*ctx
)
5761 struct gallivm_state
*gallivm
=
5762 ctx
->soa
.bld_base
.base
.gallivm
;
5764 LLVMValueRef buf_ptr
= LLVMGetParam(ctx
->main_fn
,
5765 SI_PARAM_RW_BUFFERS
);
5767 if ((ctx
->type
== PIPE_SHADER_VERTEX
&&
5768 ctx
->shader
->key
.as_es
) ||
5769 (ctx
->type
== PIPE_SHADER_TESS_EVAL
&&
5770 ctx
->shader
->key
.as_es
) ||
5771 ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5773 ctx
->type
== PIPE_SHADER_GEOMETRY
? SI_GS_RING_ESGS
5775 LLVMValueRef offset
= lp_build_const_int32(gallivm
, ring
);
5778 build_indexed_load_const(ctx
, buf_ptr
, offset
);
5781 if (ctx
->shader
->is_gs_copy_shader
) {
5782 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_VS_RING_GSVS
);
5785 build_indexed_load_const(ctx
, buf_ptr
, offset
);
5787 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
5789 for (i
= 0; i
< 4; i
++) {
5790 LLVMValueRef offset
= lp_build_const_int32(gallivm
, SI_GS_RING_GSVS0
+ i
);
5793 build_indexed_load_const(ctx
, buf_ptr
, offset
);
5798 static void si_llvm_emit_polygon_stipple(struct si_shader_context
*ctx
,
5799 LLVMValueRef param_rw_buffers
,
5800 unsigned param_pos_fixed_pt
)
5802 struct lp_build_tgsi_context
*bld_base
=
5804 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
5805 LLVMBuilderRef builder
= gallivm
->builder
;
5806 LLVMValueRef slot
, desc
, offset
, row
, bit
, address
[2];
5808 /* Use the fixed-point gl_FragCoord input.
5809 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5810 * per coordinate to get the repeating effect.
5812 address
[0] = unpack_param(ctx
, param_pos_fixed_pt
, 0, 5);
5813 address
[1] = unpack_param(ctx
, param_pos_fixed_pt
, 16, 5);
5815 /* Load the buffer descriptor. */
5816 slot
= lp_build_const_int32(gallivm
, SI_PS_CONST_POLY_STIPPLE
);
5817 desc
= build_indexed_load_const(ctx
, param_rw_buffers
, slot
);
5819 /* The stipple pattern is 32x32, each row has 32 bits. */
5820 offset
= LLVMBuildMul(builder
, address
[1],
5821 LLVMConstInt(ctx
->i32
, 4, 0), "");
5822 row
= buffer_load_const(ctx
, desc
, offset
);
5823 row
= LLVMBuildBitCast(builder
, row
, ctx
->i32
, "");
5824 bit
= LLVMBuildLShr(builder
, row
, address
[0], "");
5825 bit
= LLVMBuildTrunc(builder
, bit
, ctx
->i1
, "");
5827 /* The intrinsic kills the thread if arg < 0. */
5828 bit
= LLVMBuildSelect(builder
, bit
, LLVMConstReal(ctx
->f32
, 0),
5829 LLVMConstReal(ctx
->f32
, -1), "");
5830 lp_build_intrinsic(builder
, "llvm.AMDGPU.kill", ctx
->voidt
, &bit
, 1, 0);
5833 void si_shader_binary_read_config(struct radeon_shader_binary
*binary
,
5834 struct si_shader_config
*conf
,
5835 unsigned symbol_offset
)
5838 const unsigned char *config
=
5839 radeon_shader_binary_config_start(binary
, symbol_offset
);
5840 bool really_needs_scratch
= false;
5842 /* LLVM adds SGPR spills to the scratch size.
5843 * Find out if we really need the scratch buffer.
5845 for (i
= 0; i
< binary
->reloc_count
; i
++) {
5846 const struct radeon_shader_reloc
*reloc
= &binary
->relocs
[i
];
5848 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
) ||
5849 !strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5850 really_needs_scratch
= true;
5855 /* XXX: We may be able to emit some of these values directly rather than
5856 * extracting fields to be emitted later.
5859 for (i
= 0; i
< binary
->config_size_per_symbol
; i
+= 8) {
5860 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(config
+ i
));
5861 unsigned value
= util_le32_to_cpu(*(uint32_t*)(config
+ i
+ 4));
5863 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
5864 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
5865 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
5866 case R_00B848_COMPUTE_PGM_RSRC1
:
5867 conf
->num_sgprs
= MAX2(conf
->num_sgprs
, (G_00B028_SGPRS(value
) + 1) * 8);
5868 conf
->num_vgprs
= MAX2(conf
->num_vgprs
, (G_00B028_VGPRS(value
) + 1) * 4);
5869 conf
->float_mode
= G_00B028_FLOAT_MODE(value
);
5870 conf
->rsrc1
= value
;
5872 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
5873 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B02C_EXTRA_LDS_SIZE(value
));
5875 case R_00B84C_COMPUTE_PGM_RSRC2
:
5876 conf
->lds_size
= MAX2(conf
->lds_size
, G_00B84C_LDS_SIZE(value
));
5877 conf
->rsrc2
= value
;
5879 case R_0286CC_SPI_PS_INPUT_ENA
:
5880 conf
->spi_ps_input_ena
= value
;
5882 case R_0286D0_SPI_PS_INPUT_ADDR
:
5883 conf
->spi_ps_input_addr
= value
;
5885 case R_0286E8_SPI_TMPRING_SIZE
:
5886 case R_00B860_COMPUTE_TMPRING_SIZE
:
5887 /* WAVESIZE is in units of 256 dwords. */
5888 if (really_needs_scratch
)
5889 conf
->scratch_bytes_per_wave
=
5890 G_00B860_WAVESIZE(value
) * 256 * 4;
5892 case 0x4: /* SPILLED_SGPRS */
5893 conf
->spilled_sgprs
= value
;
5895 case 0x8: /* SPILLED_VGPRS */
5896 conf
->spilled_vgprs
= value
;
5900 static bool printed
;
5903 fprintf(stderr
, "Warning: LLVM emitted unknown "
5904 "config register: 0x%x\n", reg
);
5912 if (!conf
->spi_ps_input_addr
)
5913 conf
->spi_ps_input_addr
= conf
->spi_ps_input_ena
;
5916 void si_shader_apply_scratch_relocs(struct si_context
*sctx
,
5917 struct si_shader
*shader
,
5918 struct si_shader_config
*config
,
5919 uint64_t scratch_va
)
5922 uint32_t scratch_rsrc_dword0
= scratch_va
;
5923 uint32_t scratch_rsrc_dword1
=
5924 S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32);
5926 /* Enable scratch coalescing if LLVM sets ELEMENT_SIZE & INDEX_STRIDE
5929 if (HAVE_LLVM
>= 0x0309)
5930 scratch_rsrc_dword1
|= S_008F04_SWIZZLE_ENABLE(1);
5932 scratch_rsrc_dword1
|=
5933 S_008F04_STRIDE(config
->scratch_bytes_per_wave
/ 64);
5935 for (i
= 0 ; i
< shader
->binary
.reloc_count
; i
++) {
5936 const struct radeon_shader_reloc
*reloc
=
5937 &shader
->binary
.relocs
[i
];
5938 if (!strcmp(scratch_rsrc_dword0_symbol
, reloc
->name
)) {
5939 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5940 &scratch_rsrc_dword0
, 4);
5941 } else if (!strcmp(scratch_rsrc_dword1_symbol
, reloc
->name
)) {
5942 util_memcpy_cpu_to_le32(shader
->binary
.code
+ reloc
->offset
,
5943 &scratch_rsrc_dword1
, 4);
5948 static unsigned si_get_shader_binary_size(struct si_shader
*shader
)
5950 unsigned size
= shader
->binary
.code_size
;
5953 size
+= shader
->prolog
->binary
.code_size
;
5955 size
+= shader
->epilog
->binary
.code_size
;
5959 int si_shader_binary_upload(struct si_screen
*sscreen
, struct si_shader
*shader
)
5961 const struct radeon_shader_binary
*prolog
=
5962 shader
->prolog
? &shader
->prolog
->binary
: NULL
;
5963 const struct radeon_shader_binary
*epilog
=
5964 shader
->epilog
? &shader
->epilog
->binary
: NULL
;
5965 const struct radeon_shader_binary
*mainb
= &shader
->binary
;
5966 unsigned bo_size
= si_get_shader_binary_size(shader
) +
5967 (!epilog
? mainb
->rodata_size
: 0);
5970 assert(!prolog
|| !prolog
->rodata_size
);
5971 assert((!prolog
&& !epilog
) || !mainb
->rodata_size
);
5972 assert(!epilog
|| !epilog
->rodata_size
);
5974 r600_resource_reference(&shader
->bo
, NULL
);
5975 shader
->bo
= (struct r600_resource
*)
5976 pipe_buffer_create(&sscreen
->b
.b
, 0,
5977 PIPE_USAGE_IMMUTABLE
, bo_size
);
5982 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->buf
, NULL
,
5983 PIPE_TRANSFER_READ_WRITE
);
5986 util_memcpy_cpu_to_le32(ptr
, prolog
->code
, prolog
->code_size
);
5987 ptr
+= prolog
->code_size
;
5990 util_memcpy_cpu_to_le32(ptr
, mainb
->code
, mainb
->code_size
);
5991 ptr
+= mainb
->code_size
;
5994 util_memcpy_cpu_to_le32(ptr
, epilog
->code
, epilog
->code_size
);
5995 else if (mainb
->rodata_size
> 0)
5996 util_memcpy_cpu_to_le32(ptr
, mainb
->rodata
, mainb
->rodata_size
);
5998 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->buf
);
6002 static void si_shader_dump_disassembly(const struct radeon_shader_binary
*binary
,
6003 struct pipe_debug_callback
*debug
,
6004 const char *name
, FILE *file
)
6009 if (binary
->disasm_string
) {
6010 fprintf(file
, "Shader %s disassembly:\n", name
);
6011 fprintf(file
, "%s", binary
->disasm_string
);
6013 if (debug
&& debug
->debug_message
) {
6014 /* Very long debug messages are cut off, so send the
6015 * disassembly one line at a time. This causes more
6016 * overhead, but on the plus side it simplifies
6017 * parsing of resulting logs.
6019 pipe_debug_message(debug
, SHADER_INFO
,
6020 "Shader Disassembly Begin");
6022 line
= binary
->disasm_string
;
6024 p
= util_strchrnul(line
, '\n');
6028 pipe_debug_message(debug
, SHADER_INFO
,
6029 "%.*s", count
, line
);
6037 pipe_debug_message(debug
, SHADER_INFO
,
6038 "Shader Disassembly End");
6041 fprintf(file
, "Shader %s binary:\n", name
);
6042 for (i
= 0; i
< binary
->code_size
; i
+= 4) {
6043 fprintf(file
, "@0x%x: %02x%02x%02x%02x\n", i
,
6044 binary
->code
[i
+ 3], binary
->code
[i
+ 2],
6045 binary
->code
[i
+ 1], binary
->code
[i
]);
6050 static void si_shader_dump_stats(struct si_screen
*sscreen
,
6051 struct si_shader
*shader
,
6052 struct pipe_debug_callback
*debug
,
6056 struct si_shader_config
*conf
= &shader
->config
;
6057 unsigned num_inputs
= shader
->selector
? shader
->selector
->info
.num_inputs
: 0;
6058 unsigned code_size
= si_get_shader_binary_size(shader
);
6059 unsigned lds_increment
= sscreen
->b
.chip_class
>= CIK
? 512 : 256;
6060 unsigned lds_per_wave
= 0;
6061 unsigned max_simd_waves
= 10;
6063 /* Compute LDS usage for PS. */
6064 switch (processor
) {
6065 case PIPE_SHADER_FRAGMENT
:
6066 /* The minimum usage per wave is (num_inputs * 48). The maximum
6067 * usage is (num_inputs * 48 * 16).
6068 * We can get anything in between and it varies between waves.
6070 * The 48 bytes per input for a single primitive is equal to
6071 * 4 bytes/component * 4 components/input * 3 points.
6073 * Other stages don't know the size at compile time or don't
6074 * allocate LDS per wave, but instead they do it per thread group.
6076 lds_per_wave
= conf
->lds_size
* lds_increment
+
6077 align(num_inputs
* 48, lds_increment
);
6079 case PIPE_SHADER_COMPUTE
:
6080 if (shader
->selector
) {
6081 unsigned max_workgroup_size
=
6082 si_get_max_workgroup_size(shader
);
6083 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
6084 DIV_ROUND_UP(max_workgroup_size
, 64);
6089 /* Compute the per-SIMD wave counts. */
6090 if (conf
->num_sgprs
) {
6091 if (sscreen
->b
.chip_class
>= VI
)
6092 max_simd_waves
= MIN2(max_simd_waves
, 800 / conf
->num_sgprs
);
6094 max_simd_waves
= MIN2(max_simd_waves
, 512 / conf
->num_sgprs
);
6097 if (conf
->num_vgprs
)
6098 max_simd_waves
= MIN2(max_simd_waves
, 256 / conf
->num_vgprs
);
6100 /* LDS is 64KB per CU (4 SIMDs), which is 16KB per SIMD (usage above
6101 * 16KB makes some SIMDs unoccupied). */
6103 max_simd_waves
= MIN2(max_simd_waves
, 16384 / lds_per_wave
);
6105 if (file
!= stderr
||
6106 r600_can_dump_shader(&sscreen
->b
, processor
)) {
6107 if (processor
== PIPE_SHADER_FRAGMENT
) {
6108 fprintf(file
, "*** SHADER CONFIG ***\n"
6109 "SPI_PS_INPUT_ADDR = 0x%04x\n"
6110 "SPI_PS_INPUT_ENA = 0x%04x\n",
6111 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
6114 fprintf(file
, "*** SHADER STATS ***\n"
6117 "Spilled SGPRs: %d\n"
6118 "Spilled VGPRs: %d\n"
6119 "Private memory VGPRs: %d\n"
6120 "Code Size: %d bytes\n"
6122 "Scratch: %d bytes per wave\n"
6124 "********************\n\n\n",
6125 conf
->num_sgprs
, conf
->num_vgprs
,
6126 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
6127 conf
->private_mem_vgprs
, code_size
,
6128 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6132 pipe_debug_message(debug
, SHADER_INFO
,
6133 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
6134 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
6135 "Spilled VGPRs: %d PrivMem VGPRs: %d",
6136 conf
->num_sgprs
, conf
->num_vgprs
, code_size
,
6137 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
6138 max_simd_waves
, conf
->spilled_sgprs
,
6139 conf
->spilled_vgprs
, conf
->private_mem_vgprs
);
6142 static const char *si_get_shader_name(struct si_shader
*shader
,
6145 switch (processor
) {
6146 case PIPE_SHADER_VERTEX
:
6147 if (shader
->key
.as_es
)
6148 return "Vertex Shader as ES";
6149 else if (shader
->key
.as_ls
)
6150 return "Vertex Shader as LS";
6152 return "Vertex Shader as VS";
6153 case PIPE_SHADER_TESS_CTRL
:
6154 return "Tessellation Control Shader";
6155 case PIPE_SHADER_TESS_EVAL
:
6156 if (shader
->key
.as_es
)
6157 return "Tessellation Evaluation Shader as ES";
6159 return "Tessellation Evaluation Shader as VS";
6160 case PIPE_SHADER_GEOMETRY
:
6161 if (shader
->is_gs_copy_shader
)
6162 return "GS Copy Shader as VS";
6164 return "Geometry Shader";
6165 case PIPE_SHADER_FRAGMENT
:
6166 return "Pixel Shader";
6167 case PIPE_SHADER_COMPUTE
:
6168 return "Compute Shader";
6170 return "Unknown Shader";
6174 void si_shader_dump(struct si_screen
*sscreen
, struct si_shader
*shader
,
6175 struct pipe_debug_callback
*debug
, unsigned processor
,
6178 if (file
!= stderr
||
6179 r600_can_dump_shader(&sscreen
->b
, processor
))
6180 si_dump_shader_key(processor
, &shader
->key
, file
);
6182 if (file
!= stderr
&& shader
->binary
.llvm_ir_string
) {
6183 fprintf(file
, "\n%s - main shader part - LLVM IR:\n\n",
6184 si_get_shader_name(shader
, processor
));
6185 fprintf(file
, "%s\n", shader
->binary
.llvm_ir_string
);
6188 if (file
!= stderr
||
6189 (r600_can_dump_shader(&sscreen
->b
, processor
) &&
6190 !(sscreen
->b
.debug_flags
& DBG_NO_ASM
))) {
6191 fprintf(file
, "\n%s:\n", si_get_shader_name(shader
, processor
));
6194 si_shader_dump_disassembly(&shader
->prolog
->binary
,
6195 debug
, "prolog", file
);
6197 si_shader_dump_disassembly(&shader
->binary
, debug
, "main", file
);
6200 si_shader_dump_disassembly(&shader
->epilog
->binary
,
6201 debug
, "epilog", file
);
6202 fprintf(file
, "\n");
6205 si_shader_dump_stats(sscreen
, shader
, debug
, processor
, file
);
6208 int si_compile_llvm(struct si_screen
*sscreen
,
6209 struct radeon_shader_binary
*binary
,
6210 struct si_shader_config
*conf
,
6211 LLVMTargetMachineRef tm
,
6213 struct pipe_debug_callback
*debug
,
6218 unsigned count
= p_atomic_inc_return(&sscreen
->b
.num_compilations
);
6220 if (r600_can_dump_shader(&sscreen
->b
, processor
)) {
6221 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
6223 if (!(sscreen
->b
.debug_flags
& (DBG_NO_IR
| DBG_PREOPT_IR
))) {
6224 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
6225 LLVMDumpModule(mod
);
6226 fprintf(stderr
, "\n");
6230 if (sscreen
->record_llvm_ir
) {
6231 char *ir
= LLVMPrintModuleToString(mod
);
6232 binary
->llvm_ir_string
= strdup(ir
);
6233 LLVMDisposeMessage(ir
);
6236 if (!si_replace_shader(count
, binary
)) {
6237 r
= si_llvm_compile(mod
, binary
, tm
, debug
);
6242 si_shader_binary_read_config(binary
, conf
, 0);
6244 /* Enable 64-bit and 16-bit denormals, because there is no performance
6247 * If denormals are enabled, all floating-point output modifiers are
6250 * Don't enable denormals for 32-bit floats, because:
6251 * - Floating-point output modifiers would be ignored by the hw.
6252 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6253 * have to stop using those.
6254 * - SI & CI would be very slow.
6256 conf
->float_mode
|= V_00B028_FP_64_DENORMS
;
6258 FREE(binary
->config
);
6259 FREE(binary
->global_symbol_offsets
);
6260 binary
->config
= NULL
;
6261 binary
->global_symbol_offsets
= NULL
;
6263 /* Some shaders can't have rodata because their binaries can be
6266 if (binary
->rodata_size
&&
6267 (processor
== PIPE_SHADER_VERTEX
||
6268 processor
== PIPE_SHADER_TESS_CTRL
||
6269 processor
== PIPE_SHADER_TESS_EVAL
||
6270 processor
== PIPE_SHADER_FRAGMENT
)) {
6271 fprintf(stderr
, "radeonsi: The shader can't have rodata.");
6278 static void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
6280 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
6281 LLVMBuildRetVoid(ctx
->gallivm
.builder
);
6283 LLVMBuildRet(ctx
->gallivm
.builder
, ret
);
6286 /* Generate code for the hardware VS shader stage to go with a geometry shader */
6288 si_generate_gs_copy_shader(struct si_screen
*sscreen
,
6289 LLVMTargetMachineRef tm
,
6290 struct si_shader_selector
*gs_selector
,
6291 struct pipe_debug_callback
*debug
)
6293 struct si_shader_context ctx
;
6294 struct si_shader
*shader
;
6295 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
6296 struct lp_build_tgsi_context
*bld_base
= &ctx
.soa
.bld_base
;
6297 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
6298 struct si_shader_output_values
*outputs
;
6299 struct tgsi_shader_info
*gsinfo
= &gs_selector
->info
;
6300 LLVMValueRef args
[9];
6303 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
6308 shader
= CALLOC_STRUCT(si_shader
);
6315 shader
->selector
= gs_selector
;
6316 shader
->is_gs_copy_shader
= true;
6318 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
6319 ctx
.type
= PIPE_SHADER_VERTEX
;
6321 create_meta_data(&ctx
);
6322 create_function(&ctx
);
6323 preload_ring_buffers(&ctx
);
6325 args
[0] = ctx
.gsvs_ring
[0];
6326 args
[1] = lp_build_mul_imm(uint
,
6327 LLVMGetParam(ctx
.main_fn
,
6328 ctx
.param_vertex_id
),
6330 args
[3] = uint
->zero
;
6331 args
[4] = uint
->one
; /* OFFEN */
6332 args
[5] = uint
->zero
; /* IDXEN */
6333 args
[6] = uint
->one
; /* GLC */
6334 args
[7] = uint
->one
; /* SLC */
6335 args
[8] = uint
->zero
; /* TFE */
6337 /* Fetch vertex data from GSVS ring */
6338 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
6341 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
6342 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
6344 for (chan
= 0; chan
< 4; chan
++) {
6345 args
[2] = lp_build_const_int32(gallivm
,
6347 gs_selector
->gs_max_out_vertices
* 16 * 4);
6349 outputs
[i
].values
[chan
] =
6350 LLVMBuildBitCast(gallivm
->builder
,
6351 lp_build_intrinsic(gallivm
->builder
,
6352 "llvm.SI.buffer.load.dword.i32.i32",
6354 LP_FUNC_ATTR_READONLY
),
6359 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
6361 LLVMBuildRetVoid(gallivm
->builder
);
6363 /* Dump LLVM IR before any optimization passes */
6364 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
6365 r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6366 LLVMDumpModule(bld_base
->base
.gallivm
->module
);
6368 si_llvm_finalize_module(&ctx
,
6369 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_GEOMETRY
));
6371 r
= si_compile_llvm(sscreen
, &ctx
.shader
->binary
,
6372 &ctx
.shader
->config
, ctx
.tm
,
6373 bld_base
->base
.gallivm
->module
,
6374 debug
, PIPE_SHADER_GEOMETRY
,
6377 if (r600_can_dump_shader(&sscreen
->b
, PIPE_SHADER_GEOMETRY
))
6378 fprintf(stderr
, "GS Copy Shader:\n");
6379 si_shader_dump(sscreen
, ctx
.shader
, debug
,
6380 PIPE_SHADER_GEOMETRY
, stderr
);
6381 r
= si_shader_binary_upload(sscreen
, ctx
.shader
);
6384 si_llvm_dispose(&ctx
);
6395 static void si_dump_shader_key(unsigned shader
, struct si_shader_key
*key
,
6400 fprintf(f
, "SHADER KEY\n");
6403 case PIPE_SHADER_VERTEX
:
6404 fprintf(f
, " part.vs.prolog.instance_divisors = {");
6405 for (i
= 0; i
< ARRAY_SIZE(key
->part
.vs
.prolog
.instance_divisors
); i
++)
6406 fprintf(f
, !i
? "%u" : ", %u",
6407 key
->part
.vs
.prolog
.instance_divisors
[i
]);
6409 fprintf(f
, " part.vs.epilog.export_prim_id = %u\n", key
->part
.vs
.epilog
.export_prim_id
);
6410 fprintf(f
, " as_es = %u\n", key
->as_es
);
6411 fprintf(f
, " as_ls = %u\n", key
->as_ls
);
6412 fprintf(f
, " mono.vs.fix_fetch = 0x%x\n", key
->mono
.vs
.fix_fetch
);
6415 case PIPE_SHADER_TESS_CTRL
:
6416 fprintf(f
, " part.tcs.epilog.prim_mode = %u\n", key
->part
.tcs
.epilog
.prim_mode
);
6417 fprintf(f
, " mono.tcs.inputs_to_copy = 0x%"PRIx64
"\n", key
->mono
.tcs
.inputs_to_copy
);
6420 case PIPE_SHADER_TESS_EVAL
:
6421 fprintf(f
, " part.tes.epilog.export_prim_id = %u\n", key
->part
.tes
.epilog
.export_prim_id
);
6422 fprintf(f
, " as_es = %u\n", key
->as_es
);
6425 case PIPE_SHADER_GEOMETRY
:
6426 fprintf(f
, " part.gs.prolog.tri_strip_adj_fix = %u\n", key
->part
.gs
.prolog
.tri_strip_adj_fix
);
6429 case PIPE_SHADER_COMPUTE
:
6432 case PIPE_SHADER_FRAGMENT
:
6433 fprintf(f
, " part.ps.prolog.color_two_side = %u\n", key
->part
.ps
.prolog
.color_two_side
);
6434 fprintf(f
, " part.ps.prolog.flatshade_colors = %u\n", key
->part
.ps
.prolog
.flatshade_colors
);
6435 fprintf(f
, " part.ps.prolog.poly_stipple = %u\n", key
->part
.ps
.prolog
.poly_stipple
);
6436 fprintf(f
, " part.ps.prolog.force_persp_sample_interp = %u\n", key
->part
.ps
.prolog
.force_persp_sample_interp
);
6437 fprintf(f
, " part.ps.prolog.force_linear_sample_interp = %u\n", key
->part
.ps
.prolog
.force_linear_sample_interp
);
6438 fprintf(f
, " part.ps.prolog.force_persp_center_interp = %u\n", key
->part
.ps
.prolog
.force_persp_center_interp
);
6439 fprintf(f
, " part.ps.prolog.force_linear_center_interp = %u\n", key
->part
.ps
.prolog
.force_linear_center_interp
);
6440 fprintf(f
, " part.ps.prolog.bc_optimize_for_persp = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_persp
);
6441 fprintf(f
, " part.ps.prolog.bc_optimize_for_linear = %u\n", key
->part
.ps
.prolog
.bc_optimize_for_linear
);
6442 fprintf(f
, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key
->part
.ps
.epilog
.spi_shader_col_format
);
6443 fprintf(f
, " part.ps.epilog.color_is_int8 = 0x%X\n", key
->part
.ps
.epilog
.color_is_int8
);
6444 fprintf(f
, " part.ps.epilog.last_cbuf = %u\n", key
->part
.ps
.epilog
.last_cbuf
);
6445 fprintf(f
, " part.ps.epilog.alpha_func = %u\n", key
->part
.ps
.epilog
.alpha_func
);
6446 fprintf(f
, " part.ps.epilog.alpha_to_one = %u\n", key
->part
.ps
.epilog
.alpha_to_one
);
6447 fprintf(f
, " part.ps.epilog.poly_line_smoothing = %u\n", key
->part
.ps
.epilog
.poly_line_smoothing
);
6448 fprintf(f
, " part.ps.epilog.clamp_color = %u\n", key
->part
.ps
.epilog
.clamp_color
);
6455 if ((shader
== PIPE_SHADER_GEOMETRY
||
6456 shader
== PIPE_SHADER_TESS_EVAL
||
6457 shader
== PIPE_SHADER_VERTEX
) &&
6458 !key
->as_es
&& !key
->as_ls
) {
6459 fprintf(f
, " opt.hw_vs.kill_outputs = 0x%"PRIx64
"\n", key
->opt
.hw_vs
.kill_outputs
);
6460 fprintf(f
, " opt.hw_vs.kill_outputs2 = 0x%x\n", key
->opt
.hw_vs
.kill_outputs2
);
6461 fprintf(f
, " opt.hw_vs.clip_disable = %u\n", key
->opt
.hw_vs
.clip_disable
);
6465 static void si_init_shader_ctx(struct si_shader_context
*ctx
,
6466 struct si_screen
*sscreen
,
6467 struct si_shader
*shader
,
6468 LLVMTargetMachineRef tm
)
6470 struct lp_build_tgsi_context
*bld_base
;
6471 struct lp_build_tgsi_action tmpl
= {};
6473 si_llvm_context_init(ctx
, sscreen
, shader
, tm
,
6474 (shader
&& shader
->selector
) ? &shader
->selector
->info
: NULL
,
6475 (shader
&& shader
->selector
) ? shader
->selector
->tokens
: NULL
);
6477 bld_base
= &ctx
->soa
.bld_base
;
6478 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
6480 bld_base
->op_actions
[TGSI_OPCODE_INTERP_CENTROID
] = interp_action
;
6481 bld_base
->op_actions
[TGSI_OPCODE_INTERP_SAMPLE
] = interp_action
;
6482 bld_base
->op_actions
[TGSI_OPCODE_INTERP_OFFSET
] = interp_action
;
6484 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
6485 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
6486 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
6487 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
6488 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
6489 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
6490 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
6491 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
6492 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
6493 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= txq_fetch_args
;
6494 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
6495 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
6496 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
6497 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
6499 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
6500 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
6501 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
6502 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
6503 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
6504 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
6506 tmpl
.fetch_args
= atomic_fetch_args
;
6507 tmpl
.emit
= atomic_emit
;
6508 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
6509 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
6510 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
6511 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
6512 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
6513 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
6514 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
6515 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
6516 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
6517 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
6518 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
6519 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
6520 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
6521 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
6522 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
6523 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
6524 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
6525 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
6526 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
6527 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
6529 bld_base
->op_actions
[TGSI_OPCODE_MEMBAR
].emit
= membar_emit
;
6531 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
6532 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
6533 bld_base
->op_actions
[TGSI_OPCODE_DDX_FINE
].emit
= si_llvm_emit_ddxy
;
6534 bld_base
->op_actions
[TGSI_OPCODE_DDY_FINE
].emit
= si_llvm_emit_ddxy
;
6536 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
6537 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
6538 bld_base
->op_actions
[TGSI_OPCODE_BARRIER
].emit
= si_llvm_emit_barrier
;
6541 /* Return true if the PARAM export has been eliminated. */
6542 static bool si_eliminate_const_output(struct si_shader_context
*ctx
,
6543 LLVMValueRef inst
, unsigned offset
)
6545 struct si_shader
*shader
= ctx
->shader
;
6546 unsigned num_outputs
= shader
->selector
->info
.num_outputs
;
6547 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
6548 bool is_zero
[4] = {}, is_one
[4] = {};
6550 for (i
= 0; i
< 4; i
++) {
6551 LLVMBool loses_info
;
6552 LLVMValueRef p
= LLVMGetOperand(inst
, 5 + i
);
6554 /* It's a constant expression. Undef outputs are eliminated too. */
6555 if (LLVMIsUndef(p
)) {
6558 } else if (LLVMIsAConstantFP(p
)) {
6559 double a
= LLVMConstRealGetDouble(p
, &loses_info
);
6566 return false; /* other constant */
6571 /* Only certain combinations of 0 and 1 can be eliminated. */
6572 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
6573 default_val
= is_zero
[3] ? 0 : 1;
6574 else if (is_one
[0] && is_one
[1] && is_one
[2])
6575 default_val
= is_zero
[3] ? 2 : 3;
6579 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
6580 LLVMInstructionEraseFromParent(inst
);
6582 /* Change OFFSET to DEFAULT_VAL. */
6583 for (i
= 0; i
< num_outputs
; i
++) {
6584 if (shader
->info
.vs_output_param_offset
[i
] == offset
) {
6585 shader
->info
.vs_output_param_offset
[i
] =
6586 EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
6593 struct si_vs_exports
{
6595 unsigned offset
[SI_MAX_VS_OUTPUTS
];
6596 LLVMValueRef inst
[SI_MAX_VS_OUTPUTS
];
6599 static void si_eliminate_const_vs_outputs(struct si_shader_context
*ctx
)
6601 struct si_shader
*shader
= ctx
->shader
;
6602 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6603 LLVMBasicBlockRef bb
;
6604 struct si_vs_exports exports
;
6605 bool removed_any
= false;
6609 if (ctx
->type
== PIPE_SHADER_FRAGMENT
||
6610 ctx
->type
== PIPE_SHADER_COMPUTE
||
6611 shader
->key
.as_es
||
6615 /* Process all LLVM instructions. */
6616 bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6618 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
6621 LLVMValueRef cur
= inst
;
6622 inst
= LLVMGetNextInstruction(inst
);
6624 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
6627 LLVMValueRef callee
= lp_get_called_value(cur
);
6629 if (!lp_is_function(callee
))
6632 const char *name
= LLVMGetValueName(callee
);
6633 unsigned num_args
= LLVMCountParams(callee
);
6635 /* Check if this is an export instruction. */
6636 if (num_args
!= 9 || strcmp(name
, "llvm.SI.export"))
6639 LLVMValueRef arg
= LLVMGetOperand(cur
, 3);
6640 unsigned target
= LLVMConstIntGetZExtValue(arg
);
6642 if (target
< V_008DFC_SQ_EXP_PARAM
)
6645 target
-= V_008DFC_SQ_EXP_PARAM
;
6647 /* Eliminate constant value PARAM exports. */
6648 if (si_eliminate_const_output(ctx
, cur
, target
)) {
6651 exports
.offset
[exports
.num
] = target
;
6652 exports
.inst
[exports
.num
] = cur
;
6656 bb
= LLVMGetNextBasicBlock(bb
);
6659 /* Remove holes in export memory due to removed PARAM exports.
6660 * This is done by renumbering all PARAM exports.
6663 ubyte current_offset
[SI_MAX_VS_OUTPUTS
];
6664 unsigned new_count
= 0;
6667 /* Make a copy of the offsets. We need the old version while
6668 * we are modifying some of them. */
6669 assert(sizeof(current_offset
) ==
6670 sizeof(shader
->info
.vs_output_param_offset
));
6671 memcpy(current_offset
, shader
->info
.vs_output_param_offset
,
6672 sizeof(current_offset
));
6674 for (i
= 0; i
< exports
.num
; i
++) {
6675 unsigned offset
= exports
.offset
[i
];
6677 for (out
= 0; out
< info
->num_outputs
; out
++) {
6678 if (current_offset
[out
] != offset
)
6681 LLVMSetOperand(exports
.inst
[i
], 3,
6682 LLVMConstInt(ctx
->i32
,
6683 V_008DFC_SQ_EXP_PARAM
+ new_count
, 0));
6684 shader
->info
.vs_output_param_offset
[out
] = new_count
;
6689 shader
->info
.nr_param_exports
= new_count
;
6693 static void si_count_scratch_private_memory(struct si_shader_context
*ctx
)
6695 ctx
->shader
->config
.private_mem_vgprs
= 0;
6697 /* Process all LLVM instructions. */
6698 LLVMBasicBlockRef bb
= LLVMGetFirstBasicBlock(ctx
->main_fn
);
6700 LLVMValueRef next
= LLVMGetFirstInstruction(bb
);
6703 LLVMValueRef inst
= next
;
6704 next
= LLVMGetNextInstruction(next
);
6706 if (LLVMGetInstructionOpcode(inst
) != LLVMAlloca
)
6709 LLVMTypeRef type
= LLVMGetElementType(LLVMTypeOf(inst
));
6710 /* No idea why LLVM aligns allocas to 4 elements. */
6711 unsigned alignment
= LLVMGetAlignment(inst
);
6712 unsigned dw_size
= align(llvm_get_type_size(type
) / 4, alignment
);
6713 ctx
->shader
->config
.private_mem_vgprs
+= dw_size
;
6715 bb
= LLVMGetNextBasicBlock(bb
);
6719 static bool si_compile_tgsi_main(struct si_shader_context
*ctx
,
6720 struct si_shader
*shader
)
6722 struct si_shader_selector
*sel
= shader
->selector
;
6723 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
6725 switch (ctx
->type
) {
6726 case PIPE_SHADER_VERTEX
:
6727 ctx
->load_input
= declare_input_vs
;
6728 if (shader
->key
.as_ls
)
6729 bld_base
->emit_epilogue
= si_llvm_emit_ls_epilogue
;
6730 else if (shader
->key
.as_es
)
6731 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6733 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6735 case PIPE_SHADER_TESS_CTRL
:
6736 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tcs
;
6737 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = fetch_output_tcs
;
6738 bld_base
->emit_store
= store_output_tcs
;
6739 bld_base
->emit_epilogue
= si_llvm_emit_tcs_epilogue
;
6741 case PIPE_SHADER_TESS_EVAL
:
6742 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_tes
;
6743 if (shader
->key
.as_es
)
6744 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
6746 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
6748 case PIPE_SHADER_GEOMETRY
:
6749 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
6750 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
6752 case PIPE_SHADER_FRAGMENT
:
6753 ctx
->load_input
= declare_input_fs
;
6754 bld_base
->emit_epilogue
= si_llvm_return_fs_outputs
;
6756 case PIPE_SHADER_COMPUTE
:
6757 ctx
->declare_memory_region
= declare_compute_memory
;
6760 assert(!"Unsupported shader type");
6764 create_meta_data(ctx
);
6765 create_function(ctx
);
6766 preload_ring_buffers(ctx
);
6768 if (ctx
->type
== PIPE_SHADER_GEOMETRY
) {
6770 for (i
= 0; i
< 4; i
++) {
6771 ctx
->gs_next_vertex
[i
] =
6772 lp_build_alloca(bld_base
->base
.gallivm
,
6777 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
6778 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
6782 si_llvm_build_ret(ctx
, ctx
->return_value
);
6787 * Compute the VS prolog key, which contains all the information needed to
6788 * build the VS prolog function, and set shader->info bits where needed.
6790 static void si_get_vs_prolog_key(struct si_shader
*shader
,
6791 union si_shader_part_key
*key
)
6793 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6795 memset(key
, 0, sizeof(*key
));
6796 key
->vs_prolog
.states
= shader
->key
.part
.vs
.prolog
;
6797 key
->vs_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6798 key
->vs_prolog
.last_input
= MAX2(1, info
->num_inputs
) - 1;
6800 /* Set the instanceID flag. */
6801 for (unsigned i
= 0; i
< info
->num_inputs
; i
++)
6802 if (key
->vs_prolog
.states
.instance_divisors
[i
])
6803 shader
->info
.uses_instanceid
= true;
6807 * Compute the VS epilog key, which contains all the information needed to
6808 * build the VS epilog function, and set the PrimitiveID output offset.
6810 static void si_get_vs_epilog_key(struct si_shader
*shader
,
6811 struct si_vs_epilog_bits
*states
,
6812 union si_shader_part_key
*key
)
6814 memset(key
, 0, sizeof(*key
));
6815 key
->vs_epilog
.states
= *states
;
6817 /* Set up the PrimitiveID output. */
6818 if (shader
->key
.part
.vs
.epilog
.export_prim_id
) {
6819 unsigned index
= shader
->selector
->info
.num_outputs
;
6820 unsigned offset
= shader
->info
.nr_param_exports
++;
6822 key
->vs_epilog
.prim_id_param_offset
= offset
;
6823 assert(index
< ARRAY_SIZE(shader
->info
.vs_output_param_offset
));
6824 shader
->info
.vs_output_param_offset
[index
] = offset
;
6829 * Compute the PS prolog key, which contains all the information needed to
6830 * build the PS prolog function, and set related bits in shader->config.
6832 static void si_get_ps_prolog_key(struct si_shader
*shader
,
6833 union si_shader_part_key
*key
,
6834 bool separate_prolog
)
6836 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6838 memset(key
, 0, sizeof(*key
));
6839 key
->ps_prolog
.states
= shader
->key
.part
.ps
.prolog
;
6840 key
->ps_prolog
.colors_read
= info
->colors_read
;
6841 key
->ps_prolog
.num_input_sgprs
= shader
->info
.num_input_sgprs
;
6842 key
->ps_prolog
.num_input_vgprs
= shader
->info
.num_input_vgprs
;
6843 key
->ps_prolog
.wqm
= info
->uses_derivatives
&&
6844 (key
->ps_prolog
.colors_read
||
6845 key
->ps_prolog
.states
.force_persp_sample_interp
||
6846 key
->ps_prolog
.states
.force_linear_sample_interp
||
6847 key
->ps_prolog
.states
.force_persp_center_interp
||
6848 key
->ps_prolog
.states
.force_linear_center_interp
||
6849 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6850 key
->ps_prolog
.states
.bc_optimize_for_linear
);
6852 if (info
->colors_read
) {
6853 unsigned *color
= shader
->selector
->color_attr_index
;
6855 if (shader
->key
.part
.ps
.prolog
.color_two_side
) {
6856 /* BCOLORs are stored after the last input. */
6857 key
->ps_prolog
.num_interp_inputs
= info
->num_inputs
;
6858 key
->ps_prolog
.face_vgpr_index
= shader
->info
.face_vgpr_index
;
6859 shader
->config
.spi_ps_input_ena
|= S_0286CC_FRONT_FACE_ENA(1);
6862 for (unsigned i
= 0; i
< 2; i
++) {
6863 unsigned interp
= info
->input_interpolate
[color
[i
]];
6864 unsigned location
= info
->input_interpolate_loc
[color
[i
]];
6866 if (!(info
->colors_read
& (0xf << i
*4)))
6869 key
->ps_prolog
.color_attr_index
[i
] = color
[i
];
6871 if (shader
->key
.part
.ps
.prolog
.flatshade_colors
&&
6872 interp
== TGSI_INTERPOLATE_COLOR
)
6873 interp
= TGSI_INTERPOLATE_CONSTANT
;
6876 case TGSI_INTERPOLATE_CONSTANT
:
6877 key
->ps_prolog
.color_interp_vgpr_index
[i
] = -1;
6879 case TGSI_INTERPOLATE_PERSPECTIVE
:
6880 case TGSI_INTERPOLATE_COLOR
:
6881 /* Force the interpolation location for colors here. */
6882 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
)
6883 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6884 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
)
6885 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6888 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6889 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 0;
6890 shader
->config
.spi_ps_input_ena
|=
6891 S_0286CC_PERSP_SAMPLE_ENA(1);
6893 case TGSI_INTERPOLATE_LOC_CENTER
:
6894 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 2;
6895 shader
->config
.spi_ps_input_ena
|=
6896 S_0286CC_PERSP_CENTER_ENA(1);
6898 case TGSI_INTERPOLATE_LOC_CENTROID
:
6899 key
->ps_prolog
.color_interp_vgpr_index
[i
] = 4;
6900 shader
->config
.spi_ps_input_ena
|=
6901 S_0286CC_PERSP_CENTROID_ENA(1);
6907 case TGSI_INTERPOLATE_LINEAR
:
6908 /* Force the interpolation location for colors here. */
6909 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
)
6910 location
= TGSI_INTERPOLATE_LOC_SAMPLE
;
6911 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
)
6912 location
= TGSI_INTERPOLATE_LOC_CENTER
;
6914 /* The VGPR assignment for non-monolithic shaders
6915 * works because InitialPSInputAddr is set on the
6916 * main shader and PERSP_PULL_MODEL is never used.
6919 case TGSI_INTERPOLATE_LOC_SAMPLE
:
6920 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6921 separate_prolog
? 6 : 9;
6922 shader
->config
.spi_ps_input_ena
|=
6923 S_0286CC_LINEAR_SAMPLE_ENA(1);
6925 case TGSI_INTERPOLATE_LOC_CENTER
:
6926 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6927 separate_prolog
? 8 : 11;
6928 shader
->config
.spi_ps_input_ena
|=
6929 S_0286CC_LINEAR_CENTER_ENA(1);
6931 case TGSI_INTERPOLATE_LOC_CENTROID
:
6932 key
->ps_prolog
.color_interp_vgpr_index
[i
] =
6933 separate_prolog
? 10 : 13;
6934 shader
->config
.spi_ps_input_ena
|=
6935 S_0286CC_LINEAR_CENTROID_ENA(1);
6949 * Check whether a PS prolog is required based on the key.
6951 static bool si_need_ps_prolog(const union si_shader_part_key
*key
)
6953 return key
->ps_prolog
.colors_read
||
6954 key
->ps_prolog
.states
.force_persp_sample_interp
||
6955 key
->ps_prolog
.states
.force_linear_sample_interp
||
6956 key
->ps_prolog
.states
.force_persp_center_interp
||
6957 key
->ps_prolog
.states
.force_linear_center_interp
||
6958 key
->ps_prolog
.states
.bc_optimize_for_persp
||
6959 key
->ps_prolog
.states
.bc_optimize_for_linear
||
6960 key
->ps_prolog
.states
.poly_stipple
;
6964 * Compute the PS epilog key, which contains all the information needed to
6965 * build the PS epilog function.
6967 static void si_get_ps_epilog_key(struct si_shader
*shader
,
6968 union si_shader_part_key
*key
)
6970 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
6971 memset(key
, 0, sizeof(*key
));
6972 key
->ps_epilog
.colors_written
= info
->colors_written
;
6973 key
->ps_epilog
.writes_z
= info
->writes_z
;
6974 key
->ps_epilog
.writes_stencil
= info
->writes_stencil
;
6975 key
->ps_epilog
.writes_samplemask
= info
->writes_samplemask
;
6976 key
->ps_epilog
.states
= shader
->key
.part
.ps
.epilog
;
6980 * Build the GS prolog function. Rotate the input vertices for triangle strips
6983 static void si_build_gs_prolog_function(struct si_shader_context
*ctx
,
6984 union si_shader_part_key
*key
)
6986 const unsigned num_sgprs
= SI_GS_NUM_USER_SGPR
+ 2;
6987 const unsigned num_vgprs
= 8;
6988 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
6989 LLVMBuilderRef builder
= gallivm
->builder
;
6990 LLVMTypeRef params
[32];
6991 LLVMTypeRef returns
[32];
6992 LLVMValueRef func
, ret
;
6994 for (unsigned i
= 0; i
< num_sgprs
; ++i
) {
6995 params
[i
] = ctx
->i32
;
6996 returns
[i
] = ctx
->i32
;
6999 for (unsigned i
= 0; i
< num_vgprs
; ++i
) {
7000 params
[num_sgprs
+ i
] = ctx
->i32
;
7001 returns
[num_sgprs
+ i
] = ctx
->f32
;
7004 /* Create the function. */
7005 si_create_function(ctx
, "gs_prolog", returns
, num_sgprs
+ num_vgprs
,
7006 params
, num_sgprs
+ num_vgprs
, num_sgprs
- 1);
7007 func
= ctx
->main_fn
;
7009 /* Copy inputs to outputs. This should be no-op, as the registers match,
7010 * but it will prevent the compiler from overwriting them unintentionally.
7012 ret
= ctx
->return_value
;
7013 for (unsigned i
= 0; i
< num_sgprs
; i
++) {
7014 LLVMValueRef p
= LLVMGetParam(func
, i
);
7015 ret
= LLVMBuildInsertValue(builder
, ret
, p
, i
, "");
7017 for (unsigned i
= 0; i
< num_vgprs
; i
++) {
7018 LLVMValueRef p
= LLVMGetParam(func
, num_sgprs
+ i
);
7019 p
= LLVMBuildBitCast(builder
, p
, ctx
->f32
, "");
7020 ret
= LLVMBuildInsertValue(builder
, ret
, p
, num_sgprs
+ i
, "");
7023 if (key
->gs_prolog
.states
.tri_strip_adj_fix
) {
7024 /* Remap the input vertices for every other primitive. */
7025 const unsigned vtx_params
[6] = {
7033 LLVMValueRef prim_id
, rotate
;
7035 prim_id
= LLVMGetParam(func
, num_sgprs
+ 2);
7036 rotate
= LLVMBuildTrunc(builder
, prim_id
, ctx
->i1
, "");
7038 for (unsigned i
= 0; i
< 6; ++i
) {
7039 LLVMValueRef base
, rotated
, actual
;
7040 base
= LLVMGetParam(func
, vtx_params
[i
]);
7041 rotated
= LLVMGetParam(func
, vtx_params
[(i
+ 4) % 6]);
7042 actual
= LLVMBuildSelect(builder
, rotate
, rotated
, base
, "");
7043 actual
= LLVMBuildBitCast(builder
, actual
, ctx
->f32
, "");
7044 ret
= LLVMBuildInsertValue(builder
, ret
, actual
, vtx_params
[i
], "");
7048 LLVMBuildRet(builder
, ret
);
7052 * Given a list of shader part functions, build a wrapper function that
7053 * runs them in sequence to form a monolithic shader.
7055 static void si_build_wrapper_function(struct si_shader_context
*ctx
,
7056 LLVMValueRef
*parts
,
7060 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7061 LLVMBuilderRef builder
= ctx
->gallivm
.builder
;
7062 /* PS epilog has one arg per color component */
7063 LLVMTypeRef param_types
[48];
7064 LLVMValueRef out
[48];
7065 LLVMTypeRef function_type
;
7066 unsigned num_params
;
7068 MAYBE_UNUSED
unsigned num_out_sgpr
; /* used in debug checks */
7069 unsigned num_sgprs
, num_vgprs
;
7070 unsigned last_sgpr_param
;
7073 for (unsigned i
= 0; i
< num_parts
; ++i
) {
7074 lp_add_function_attr(parts
[i
], -1, LP_FUNC_ATTR_ALWAYSINLINE
);
7075 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
7078 /* The parameters of the wrapper function correspond to those of the
7079 * first part in terms of SGPRs and VGPRs, but we use the types of the
7080 * main part to get the right types. This is relevant for the
7081 * dereferenceable attribute on descriptor table pointers.
7086 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
7087 num_params
= LLVMCountParamTypes(function_type
);
7089 for (unsigned i
= 0; i
< num_params
; ++i
) {
7090 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
7092 if (ac_is_sgpr_param(param
)) {
7093 assert(num_vgprs
== 0);
7094 num_sgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7096 num_vgprs
+= llvm_get_type_size(LLVMTypeOf(param
)) / 4;
7099 assert(num_vgprs
+ num_sgprs
<= ARRAY_SIZE(param_types
));
7102 last_sgpr_param
= 0;
7104 while (gprs
< num_sgprs
+ num_vgprs
) {
7105 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], num_params
);
7108 param_types
[num_params
] = LLVMTypeOf(param
);
7109 if (gprs
< num_sgprs
)
7110 last_sgpr_param
= num_params
;
7111 size
= llvm_get_type_size(param_types
[num_params
]) / 4;
7114 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
7115 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
7116 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
7121 si_create_function(ctx
, "wrapper", NULL
, 0, param_types
, num_params
, last_sgpr_param
);
7123 /* Record the arguments of the function as if they were an output of
7129 for (unsigned i
= 0; i
< num_params
; ++i
) {
7130 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
7131 LLVMTypeRef param_type
= LLVMTypeOf(param
);
7132 LLVMTypeRef out_type
= i
<= last_sgpr_param
? ctx
->i32
: ctx
->f32
;
7133 unsigned size
= llvm_get_type_size(param_type
) / 4;
7136 if (param_type
!= out_type
)
7137 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
7138 out
[num_out
++] = param
;
7140 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
7142 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7143 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->i64
, "");
7144 param_type
= ctx
->i64
;
7147 if (param_type
!= vector_type
)
7148 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
7150 for (unsigned j
= 0; j
< size
; ++j
)
7151 out
[num_out
++] = LLVMBuildExtractElement(
7152 builder
, param
, LLVMConstInt(ctx
->i32
, j
, 0), "");
7155 if (i
<= last_sgpr_param
)
7156 num_out_sgpr
= num_out
;
7159 /* Now chain the parts. */
7160 for (unsigned part
= 0; part
< num_parts
; ++part
) {
7161 LLVMValueRef in
[48];
7163 LLVMTypeRef ret_type
;
7164 unsigned out_idx
= 0;
7166 num_params
= LLVMCountParams(parts
[part
]);
7167 assert(num_params
<= ARRAY_SIZE(param_types
));
7169 /* Derive arguments for the next part from outputs of the
7172 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
7174 LLVMTypeRef param_type
;
7176 unsigned param_size
;
7177 LLVMValueRef arg
= NULL
;
7179 param
= LLVMGetParam(parts
[part
], param_idx
);
7180 param_type
= LLVMTypeOf(param
);
7181 param_size
= llvm_get_type_size(param_type
) / 4;
7182 is_sgpr
= ac_is_sgpr_param(param
);
7185 #if HAVE_LLVM < 0x0400
7186 LLVMRemoveAttribute(param
, LLVMByValAttribute
);
7188 unsigned kind_id
= LLVMGetEnumAttributeKindForName("byval", 5);
7189 LLVMRemoveEnumAttributeAtIndex(parts
[part
], param_idx
+ 1, kind_id
);
7191 lp_add_function_attr(parts
[part
], param_idx
+ 1, LP_FUNC_ATTR_INREG
);
7194 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
7195 assert(is_sgpr
|| out_idx
>= num_out_sgpr
);
7197 if (param_size
== 1)
7200 arg
= lp_build_gather_values(gallivm
, &out
[out_idx
], param_size
);
7202 if (LLVMTypeOf(arg
) != param_type
) {
7203 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
7204 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->i64
, "");
7205 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
7207 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
7211 in
[param_idx
] = arg
;
7212 out_idx
+= param_size
;
7215 ret
= LLVMBuildCall(builder
, parts
[part
], in
, num_params
, "");
7216 ret_type
= LLVMTypeOf(ret
);
7218 /* Extract the returned GPRs. */
7222 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
7223 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
7225 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
7227 for (unsigned i
= 0; i
< ret_size
; ++i
) {
7229 LLVMBuildExtractValue(builder
, ret
, i
, "");
7231 out
[num_out
++] = val
;
7233 if (LLVMTypeOf(val
) == ctx
->i32
) {
7234 assert(num_out_sgpr
+ 1 == num_out
);
7235 num_out_sgpr
= num_out
;
7241 LLVMBuildRetVoid(builder
);
7244 int si_compile_tgsi_shader(struct si_screen
*sscreen
,
7245 LLVMTargetMachineRef tm
,
7246 struct si_shader
*shader
,
7248 struct pipe_debug_callback
*debug
)
7250 struct si_shader_selector
*sel
= shader
->selector
;
7251 struct si_shader_context ctx
;
7252 struct lp_build_tgsi_context
*bld_base
;
7256 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
7257 * conversion fails. */
7258 if (r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
) &&
7259 !(sscreen
->b
.debug_flags
& DBG_NO_TGSI
)) {
7260 tgsi_dump(sel
->tokens
, 0);
7261 si_dump_streamout(&sel
->so
);
7264 si_init_shader_ctx(&ctx
, sscreen
, shader
, tm
);
7265 ctx
.separate_prolog
= !is_monolithic
;
7267 memset(shader
->info
.vs_output_param_offset
, EXP_PARAM_UNDEFINED
,
7268 sizeof(shader
->info
.vs_output_param_offset
));
7270 shader
->info
.uses_instanceid
= sel
->info
.uses_instanceid
;
7272 bld_base
= &ctx
.soa
.bld_base
;
7273 ctx
.load_system_value
= declare_system_value
;
7275 if (!si_compile_tgsi_main(&ctx
, shader
)) {
7276 si_llvm_dispose(&ctx
);
7280 if (is_monolithic
&& ctx
.type
== PIPE_SHADER_VERTEX
) {
7281 LLVMValueRef parts
[3];
7285 need_prolog
= sel
->info
.num_inputs
;
7286 need_epilog
= !shader
->key
.as_es
&& !shader
->key
.as_ls
;
7288 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7291 union si_shader_part_key prolog_key
;
7292 si_get_vs_prolog_key(shader
, &prolog_key
);
7293 si_build_vs_prolog_function(&ctx
, &prolog_key
);
7294 parts
[0] = ctx
.main_fn
;
7298 union si_shader_part_key epilog_key
;
7299 si_get_vs_epilog_key(shader
, &shader
->key
.part
.vs
.epilog
, &epilog_key
);
7300 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7301 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7304 si_build_wrapper_function(&ctx
, parts
, 1 + need_prolog
+ need_epilog
,
7305 need_prolog
? 1 : 0);
7306 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_CTRL
) {
7307 LLVMValueRef parts
[2];
7308 union si_shader_part_key epilog_key
;
7310 parts
[0] = ctx
.main_fn
;
7312 memset(&epilog_key
, 0, sizeof(epilog_key
));
7313 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7314 si_build_tcs_epilog_function(&ctx
, &epilog_key
);
7315 parts
[1] = ctx
.main_fn
;
7317 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7318 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_TESS_EVAL
&&
7319 !shader
->key
.as_es
) {
7320 LLVMValueRef parts
[2];
7321 union si_shader_part_key epilog_key
;
7323 parts
[0] = ctx
.main_fn
;
7325 si_get_vs_epilog_key(shader
, &shader
->key
.part
.tes
.epilog
, &epilog_key
);
7326 si_build_vs_epilog_function(&ctx
, &epilog_key
);
7327 parts
[1] = ctx
.main_fn
;
7329 si_build_wrapper_function(&ctx
, parts
, 2, 0);
7330 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_GEOMETRY
) {
7331 LLVMValueRef parts
[2];
7332 union si_shader_part_key prolog_key
;
7334 parts
[1] = ctx
.main_fn
;
7336 memset(&prolog_key
, 0, sizeof(prolog_key
));
7337 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7338 si_build_gs_prolog_function(&ctx
, &prolog_key
);
7339 parts
[0] = ctx
.main_fn
;
7341 si_build_wrapper_function(&ctx
, parts
, 2, 1);
7342 } else if (is_monolithic
&& ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7343 LLVMValueRef parts
[3];
7344 union si_shader_part_key prolog_key
;
7345 union si_shader_part_key epilog_key
;
7348 si_get_ps_prolog_key(shader
, &prolog_key
, false);
7349 need_prolog
= si_need_ps_prolog(&prolog_key
);
7351 parts
[need_prolog
? 1 : 0] = ctx
.main_fn
;
7354 si_build_ps_prolog_function(&ctx
, &prolog_key
);
7355 parts
[0] = ctx
.main_fn
;
7358 si_get_ps_epilog_key(shader
, &epilog_key
);
7359 si_build_ps_epilog_function(&ctx
, &epilog_key
);
7360 parts
[need_prolog
? 2 : 1] = ctx
.main_fn
;
7362 si_build_wrapper_function(&ctx
, parts
, need_prolog
? 3 : 2, need_prolog
? 1 : 0);
7365 mod
= bld_base
->base
.gallivm
->module
;
7367 /* Dump LLVM IR before any optimization passes */
7368 if (sscreen
->b
.debug_flags
& DBG_PREOPT_IR
&&
7369 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7370 LLVMDumpModule(mod
);
7372 si_llvm_finalize_module(&ctx
,
7373 r600_extra_shader_checks(&sscreen
->b
, ctx
.type
));
7375 /* Post-optimization transformations and analysis. */
7376 si_eliminate_const_vs_outputs(&ctx
);
7378 if ((debug
&& debug
->debug_message
) ||
7379 r600_can_dump_shader(&sscreen
->b
, ctx
.type
))
7380 si_count_scratch_private_memory(&ctx
);
7382 /* Compile to bytecode. */
7383 r
= si_compile_llvm(sscreen
, &shader
->binary
, &shader
->config
, tm
,
7384 mod
, debug
, ctx
.type
, "TGSI shader");
7385 si_llvm_dispose(&ctx
);
7387 fprintf(stderr
, "LLVM failed to compile shader\n");
7391 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
7392 * LLVM 3.9svn has this bug.
7394 if (sel
->type
== PIPE_SHADER_COMPUTE
) {
7395 unsigned wave_size
= 64;
7396 unsigned max_vgprs
= 256;
7397 unsigned max_sgprs
= sscreen
->b
.chip_class
>= VI
? 800 : 512;
7398 unsigned max_sgprs_per_wave
= 128;
7399 unsigned max_block_threads
= si_get_max_workgroup_size(shader
);
7400 unsigned min_waves_per_cu
= DIV_ROUND_UP(max_block_threads
, wave_size
);
7401 unsigned min_waves_per_simd
= DIV_ROUND_UP(min_waves_per_cu
, 4);
7403 max_vgprs
= max_vgprs
/ min_waves_per_simd
;
7404 max_sgprs
= MIN2(max_sgprs
/ min_waves_per_simd
, max_sgprs_per_wave
);
7406 if (shader
->config
.num_sgprs
> max_sgprs
||
7407 shader
->config
.num_vgprs
> max_vgprs
) {
7408 fprintf(stderr
, "LLVM failed to compile a shader correctly: "
7409 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
7410 shader
->config
.num_sgprs
, shader
->config
.num_vgprs
,
7411 max_sgprs
, max_vgprs
);
7413 /* Just terminate the process, because dependent
7414 * shaders can hang due to bad input data, but use
7415 * the env var to allow shader-db to work.
7417 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
7422 /* Add the scratch offset to input SGPRs. */
7423 if (shader
->config
.scratch_bytes_per_wave
)
7424 shader
->info
.num_input_sgprs
+= 1; /* scratch byte offset */
7426 /* Calculate the number of fragment input VGPRs. */
7427 if (ctx
.type
== PIPE_SHADER_FRAGMENT
) {
7428 shader
->info
.num_input_vgprs
= 0;
7429 shader
->info
.face_vgpr_index
= -1;
7431 if (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7432 shader
->info
.num_input_vgprs
+= 2;
7433 if (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7434 shader
->info
.num_input_vgprs
+= 2;
7435 if (G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7436 shader
->info
.num_input_vgprs
+= 2;
7437 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader
->config
.spi_ps_input_addr
))
7438 shader
->info
.num_input_vgprs
+= 3;
7439 if (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_addr
))
7440 shader
->info
.num_input_vgprs
+= 2;
7441 if (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
))
7442 shader
->info
.num_input_vgprs
+= 2;
7443 if (G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_addr
))
7444 shader
->info
.num_input_vgprs
+= 2;
7445 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader
->config
.spi_ps_input_addr
))
7446 shader
->info
.num_input_vgprs
+= 1;
7447 if (G_0286CC_POS_X_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7448 shader
->info
.num_input_vgprs
+= 1;
7449 if (G_0286CC_POS_Y_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7450 shader
->info
.num_input_vgprs
+= 1;
7451 if (G_0286CC_POS_Z_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7452 shader
->info
.num_input_vgprs
+= 1;
7453 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_addr
))
7454 shader
->info
.num_input_vgprs
+= 1;
7455 if (G_0286CC_FRONT_FACE_ENA(shader
->config
.spi_ps_input_addr
)) {
7456 shader
->info
.face_vgpr_index
= shader
->info
.num_input_vgprs
;
7457 shader
->info
.num_input_vgprs
+= 1;
7459 if (G_0286CC_ANCILLARY_ENA(shader
->config
.spi_ps_input_addr
))
7460 shader
->info
.num_input_vgprs
+= 1;
7461 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader
->config
.spi_ps_input_addr
))
7462 shader
->info
.num_input_vgprs
+= 1;
7463 if (G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
))
7464 shader
->info
.num_input_vgprs
+= 1;
7471 * Create, compile and return a shader part (prolog or epilog).
7473 * \param sscreen screen
7474 * \param list list of shader parts of the same category
7475 * \param type shader type
7476 * \param key shader part key
7477 * \param prolog whether the part being requested is a prolog
7478 * \param tm LLVM target machine
7479 * \param debug debug callback
7480 * \param build the callback responsible for building the main function
7481 * \return non-NULL on success
7483 static struct si_shader_part
*
7484 si_get_shader_part(struct si_screen
*sscreen
,
7485 struct si_shader_part
**list
,
7486 enum pipe_shader_type type
,
7488 union si_shader_part_key
*key
,
7489 LLVMTargetMachineRef tm
,
7490 struct pipe_debug_callback
*debug
,
7491 void (*build
)(struct si_shader_context
*,
7492 union si_shader_part_key
*),
7495 struct si_shader_part
*result
;
7497 pipe_mutex_lock(sscreen
->shader_parts_mutex
);
7499 /* Find existing. */
7500 for (result
= *list
; result
; result
= result
->next
) {
7501 if (memcmp(&result
->key
, key
, sizeof(*key
)) == 0) {
7502 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
7507 /* Compile a new one. */
7508 result
= CALLOC_STRUCT(si_shader_part
);
7511 struct si_shader shader
= {};
7512 struct si_shader_context ctx
;
7513 struct gallivm_state
*gallivm
= &ctx
.gallivm
;
7515 si_init_shader_ctx(&ctx
, sscreen
, &shader
, tm
);
7519 case PIPE_SHADER_VERTEX
:
7521 case PIPE_SHADER_TESS_CTRL
:
7523 shader
.key
.part
.tcs
.epilog
= key
->tcs_epilog
.states
;
7525 case PIPE_SHADER_GEOMETRY
:
7528 case PIPE_SHADER_FRAGMENT
:
7530 shader
.key
.part
.ps
.prolog
= key
->ps_prolog
.states
;
7532 shader
.key
.part
.ps
.epilog
= key
->ps_epilog
.states
;
7535 unreachable("bad shader part");
7541 si_llvm_finalize_module(&ctx
,
7542 r600_extra_shader_checks(&sscreen
->b
, PIPE_SHADER_FRAGMENT
));
7544 if (si_compile_llvm(sscreen
, &result
->binary
, &result
->config
, tm
,
7545 gallivm
->module
, debug
, ctx
.type
, name
)) {
7551 result
->next
= *list
;
7555 si_llvm_dispose(&ctx
);
7556 pipe_mutex_unlock(sscreen
->shader_parts_mutex
);
7561 * Build the vertex shader prolog function.
7563 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
7564 * All inputs are returned unmodified. The vertex load indices are
7565 * stored after them, which will be used by the API VS for fetching inputs.
7567 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
7572 * (VertexID + BaseVertex),
7573 * (InstanceID + StartInstance),
7574 * (InstanceID / 2 + StartInstance)
7576 static void si_build_vs_prolog_function(struct si_shader_context
*ctx
,
7577 union si_shader_part_key
*key
)
7579 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7580 LLVMTypeRef
*params
, *returns
;
7581 LLVMValueRef ret
, func
;
7582 int last_sgpr
, num_params
, num_returns
, i
;
7584 ctx
->param_vertex_id
= key
->vs_prolog
.num_input_sgprs
;
7585 ctx
->param_instance_id
= key
->vs_prolog
.num_input_sgprs
+ 3;
7587 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
7588 params
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4) *
7589 sizeof(LLVMTypeRef
));
7590 returns
= alloca((key
->vs_prolog
.num_input_sgprs
+ 4 +
7591 key
->vs_prolog
.last_input
+ 1) *
7592 sizeof(LLVMTypeRef
));
7596 /* Declare input and output SGPRs. */
7598 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7599 params
[num_params
++] = ctx
->i32
;
7600 returns
[num_returns
++] = ctx
->i32
;
7602 last_sgpr
= num_params
- 1;
7604 /* 4 preloaded VGPRs (outputs must be floats) */
7605 for (i
= 0; i
< 4; i
++) {
7606 params
[num_params
++] = ctx
->i32
;
7607 returns
[num_returns
++] = ctx
->f32
;
7610 /* Vertex load indices. */
7611 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++)
7612 returns
[num_returns
++] = ctx
->f32
;
7614 /* Create the function. */
7615 si_create_function(ctx
, "vs_prolog", returns
, num_returns
, params
,
7616 num_params
, last_sgpr
);
7617 func
= ctx
->main_fn
;
7619 /* Copy inputs to outputs. This should be no-op, as the registers match,
7620 * but it will prevent the compiler from overwriting them unintentionally.
7622 ret
= ctx
->return_value
;
7623 for (i
= 0; i
< key
->vs_prolog
.num_input_sgprs
; i
++) {
7624 LLVMValueRef p
= LLVMGetParam(func
, i
);
7625 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7627 for (i
= num_params
- 4; i
< num_params
; i
++) {
7628 LLVMValueRef p
= LLVMGetParam(func
, i
);
7629 p
= LLVMBuildBitCast(gallivm
->builder
, p
, ctx
->f32
, "");
7630 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7633 /* Compute vertex load indices from instance divisors. */
7634 for (i
= 0; i
<= key
->vs_prolog
.last_input
; i
++) {
7635 unsigned divisor
= key
->vs_prolog
.states
.instance_divisors
[i
];
7639 /* InstanceID / Divisor + StartInstance */
7640 index
= get_instance_index_for_fetch(ctx
,
7641 SI_SGPR_START_INSTANCE
,
7644 /* VertexID + BaseVertex */
7645 index
= LLVMBuildAdd(gallivm
->builder
,
7646 LLVMGetParam(func
, ctx
->param_vertex_id
),
7647 LLVMGetParam(func
, SI_SGPR_BASE_VERTEX
), "");
7650 index
= LLVMBuildBitCast(gallivm
->builder
, index
, ctx
->f32
, "");
7651 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, index
,
7655 si_llvm_build_ret(ctx
, ret
);
7659 * Build the vertex shader epilog function. This is also used by the tessellation
7660 * evaluation shader compiled as VS.
7662 * The input is PrimitiveID.
7664 * If PrimitiveID is required by the pixel shader, export it.
7665 * Otherwise, do nothing.
7667 static void si_build_vs_epilog_function(struct si_shader_context
*ctx
,
7668 union si_shader_part_key
*key
)
7670 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7671 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
7672 LLVMTypeRef params
[5];
7675 /* Declare input VGPRs. */
7676 num_params
= key
->vs_epilog
.states
.export_prim_id
?
7677 (VS_EPILOG_PRIMID_LOC
+ 1) : 0;
7678 assert(num_params
<= ARRAY_SIZE(params
));
7680 for (i
= 0; i
< num_params
; i
++)
7681 params
[i
] = ctx
->f32
;
7683 /* Create the function. */
7684 si_create_function(ctx
, "vs_epilog", NULL
, 0, params
, num_params
, -1);
7687 if (key
->vs_epilog
.states
.export_prim_id
) {
7688 struct lp_build_context
*base
= &bld_base
->base
;
7689 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
7690 LLVMValueRef args
[9];
7692 args
[0] = lp_build_const_int32(base
->gallivm
, 0x0); /* enabled channels */
7693 args
[1] = uint
->zero
; /* whether the EXEC mask is valid */
7694 args
[2] = uint
->zero
; /* DONE bit */
7695 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_PARAM
+
7696 key
->vs_epilog
.prim_id_param_offset
);
7697 args
[4] = uint
->zero
; /* COMPR flag (0 = 32-bit export) */
7698 args
[5] = LLVMGetParam(ctx
->main_fn
,
7699 VS_EPILOG_PRIMID_LOC
); /* X */
7700 args
[6] = base
->undef
; /* Y */
7701 args
[7] = base
->undef
; /* Z */
7702 args
[8] = base
->undef
; /* W */
7704 lp_build_intrinsic(base
->gallivm
->builder
, "llvm.SI.export",
7705 LLVMVoidTypeInContext(base
->gallivm
->context
),
7709 LLVMBuildRetVoid(gallivm
->builder
);
7713 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
7715 static bool si_get_vs_epilog(struct si_screen
*sscreen
,
7716 LLVMTargetMachineRef tm
,
7717 struct si_shader
*shader
,
7718 struct pipe_debug_callback
*debug
,
7719 struct si_vs_epilog_bits
*states
)
7721 union si_shader_part_key epilog_key
;
7723 si_get_vs_epilog_key(shader
, states
, &epilog_key
);
7725 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->vs_epilogs
,
7726 PIPE_SHADER_VERTEX
, true,
7727 &epilog_key
, tm
, debug
,
7728 si_build_vs_epilog_function
,
7729 "Vertex Shader Epilog");
7730 return shader
->epilog
!= NULL
;
7734 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7736 static bool si_shader_select_vs_parts(struct si_screen
*sscreen
,
7737 LLVMTargetMachineRef tm
,
7738 struct si_shader
*shader
,
7739 struct pipe_debug_callback
*debug
)
7741 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
7742 union si_shader_part_key prolog_key
;
7744 /* Get the prolog. */
7745 si_get_vs_prolog_key(shader
, &prolog_key
);
7747 /* The prolog is a no-op if there are no inputs. */
7748 if (info
->num_inputs
) {
7750 si_get_shader_part(sscreen
, &sscreen
->vs_prologs
,
7751 PIPE_SHADER_VERTEX
, true,
7752 &prolog_key
, tm
, debug
,
7753 si_build_vs_prolog_function
,
7754 "Vertex Shader Prolog");
7755 if (!shader
->prolog
)
7759 /* Get the epilog. */
7760 if (!shader
->key
.as_es
&& !shader
->key
.as_ls
&&
7761 !si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7762 &shader
->key
.part
.vs
.epilog
))
7769 * Select and compile (or reuse) TES parts (epilog).
7771 static bool si_shader_select_tes_parts(struct si_screen
*sscreen
,
7772 LLVMTargetMachineRef tm
,
7773 struct si_shader
*shader
,
7774 struct pipe_debug_callback
*debug
)
7776 if (shader
->key
.as_es
)
7779 /* TES compiled as VS. */
7780 return si_get_vs_epilog(sscreen
, tm
, shader
, debug
,
7781 &shader
->key
.part
.tes
.epilog
);
7785 * Compile the TCS epilog function. This writes tesselation factors to memory
7786 * based on the output primitive type of the tesselator (determined by TES).
7788 static void si_build_tcs_epilog_function(struct si_shader_context
*ctx
,
7789 union si_shader_part_key
*key
)
7791 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7792 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
7793 LLVMTypeRef params
[16];
7795 int last_sgpr
, num_params
;
7797 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
7798 params
[SI_PARAM_RW_BUFFERS
] = const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
);
7799 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
7800 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
7801 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
7802 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
7803 params
[SI_PARAM_TCS_OFFCHIP_LAYOUT
] = ctx
->i32
;
7804 params
[SI_PARAM_TCS_OUT_OFFSETS
] = ctx
->i32
;
7805 params
[SI_PARAM_TCS_OUT_LAYOUT
] = ctx
->i32
;
7806 params
[SI_PARAM_TCS_IN_LAYOUT
] = ctx
->i32
;
7807 params
[ctx
->param_oc_lds
= SI_PARAM_TCS_OC_LDS
] = ctx
->i32
;
7808 params
[SI_PARAM_TESS_FACTOR_OFFSET
] = ctx
->i32
;
7809 last_sgpr
= SI_PARAM_TESS_FACTOR_OFFSET
;
7810 num_params
= last_sgpr
+ 1;
7812 params
[num_params
++] = ctx
->i32
; /* patch index within the wave (REL_PATCH_ID) */
7813 params
[num_params
++] = ctx
->i32
; /* invocation ID within the patch */
7814 params
[num_params
++] = ctx
->i32
; /* LDS offset where tess factors should be loaded from */
7816 /* Create the function. */
7817 si_create_function(ctx
, "tcs_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
7818 declare_tess_lds(ctx
);
7819 func
= ctx
->main_fn
;
7821 si_write_tess_factors(bld_base
,
7822 LLVMGetParam(func
, last_sgpr
+ 1),
7823 LLVMGetParam(func
, last_sgpr
+ 2),
7824 LLVMGetParam(func
, last_sgpr
+ 3));
7826 LLVMBuildRetVoid(gallivm
->builder
);
7830 * Select and compile (or reuse) TCS parts (epilog).
7832 static bool si_shader_select_tcs_parts(struct si_screen
*sscreen
,
7833 LLVMTargetMachineRef tm
,
7834 struct si_shader
*shader
,
7835 struct pipe_debug_callback
*debug
)
7837 union si_shader_part_key epilog_key
;
7839 /* Get the epilog. */
7840 memset(&epilog_key
, 0, sizeof(epilog_key
));
7841 epilog_key
.tcs_epilog
.states
= shader
->key
.part
.tcs
.epilog
;
7843 shader
->epilog
= si_get_shader_part(sscreen
, &sscreen
->tcs_epilogs
,
7844 PIPE_SHADER_TESS_CTRL
, false,
7845 &epilog_key
, tm
, debug
,
7846 si_build_tcs_epilog_function
,
7847 "Tessellation Control Shader Epilog");
7848 return shader
->epilog
!= NULL
;
7852 * Select and compile (or reuse) GS parts (prolog).
7854 static bool si_shader_select_gs_parts(struct si_screen
*sscreen
,
7855 LLVMTargetMachineRef tm
,
7856 struct si_shader
*shader
,
7857 struct pipe_debug_callback
*debug
)
7859 union si_shader_part_key prolog_key
;
7861 if (!shader
->key
.part
.gs
.prolog
.tri_strip_adj_fix
)
7864 memset(&prolog_key
, 0, sizeof(prolog_key
));
7865 prolog_key
.gs_prolog
.states
= shader
->key
.part
.gs
.prolog
;
7867 shader
->prolog
= si_get_shader_part(sscreen
, &sscreen
->gs_prologs
,
7868 PIPE_SHADER_GEOMETRY
, true,
7869 &prolog_key
, tm
, debug
,
7870 si_build_gs_prolog_function
,
7871 "Geometry Shader Prolog");
7872 return shader
->prolog
!= NULL
;
7876 * Build the pixel shader prolog function. This handles:
7877 * - two-side color selection and interpolation
7878 * - overriding interpolation parameters for the API PS
7879 * - polygon stippling
7881 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7882 * overriden by other states. (e.g. per-sample interpolation)
7883 * Interpolated colors are stored after the preloaded VGPRs.
7885 static void si_build_ps_prolog_function(struct si_shader_context
*ctx
,
7886 union si_shader_part_key
*key
)
7888 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
7889 LLVMTypeRef
*params
;
7890 LLVMValueRef ret
, func
;
7891 int last_sgpr
, num_params
, num_returns
, i
, num_color_channels
;
7893 assert(si_need_ps_prolog(key
));
7895 /* Number of inputs + 8 color elements. */
7896 params
= alloca((key
->ps_prolog
.num_input_sgprs
+
7897 key
->ps_prolog
.num_input_vgprs
+ 8) *
7898 sizeof(LLVMTypeRef
));
7900 /* Declare inputs. */
7902 for (i
= 0; i
< key
->ps_prolog
.num_input_sgprs
; i
++)
7903 params
[num_params
++] = ctx
->i32
;
7904 last_sgpr
= num_params
- 1;
7906 for (i
= 0; i
< key
->ps_prolog
.num_input_vgprs
; i
++)
7907 params
[num_params
++] = ctx
->f32
;
7909 /* Declare outputs (same as inputs + add colors if needed) */
7910 num_returns
= num_params
;
7911 num_color_channels
= util_bitcount(key
->ps_prolog
.colors_read
);
7912 for (i
= 0; i
< num_color_channels
; i
++)
7913 params
[num_returns
++] = ctx
->f32
;
7915 /* Create the function. */
7916 si_create_function(ctx
, "ps_prolog", params
, num_returns
, params
,
7917 num_params
, last_sgpr
);
7918 func
= ctx
->main_fn
;
7920 /* Copy inputs to outputs. This should be no-op, as the registers match,
7921 * but it will prevent the compiler from overwriting them unintentionally.
7923 ret
= ctx
->return_value
;
7924 for (i
= 0; i
< num_params
; i
++) {
7925 LLVMValueRef p
= LLVMGetParam(func
, i
);
7926 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, p
, i
, "");
7929 /* Polygon stippling. */
7930 if (key
->ps_prolog
.states
.poly_stipple
) {
7931 /* POS_FIXED_PT is always last. */
7932 unsigned pos
= key
->ps_prolog
.num_input_sgprs
+
7933 key
->ps_prolog
.num_input_vgprs
- 1;
7934 LLVMValueRef ptr
[2], list
;
7936 /* Get the pointer to rw buffers. */
7937 ptr
[0] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS
);
7938 ptr
[1] = LLVMGetParam(func
, SI_SGPR_RW_BUFFERS_HI
);
7939 list
= lp_build_gather_values(gallivm
, ptr
, 2);
7940 list
= LLVMBuildBitCast(gallivm
->builder
, list
, ctx
->i64
, "");
7941 list
= LLVMBuildIntToPtr(gallivm
->builder
, list
,
7942 const_array(ctx
->v16i8
, SI_NUM_RW_BUFFERS
), "");
7944 si_llvm_emit_polygon_stipple(ctx
, list
, pos
);
7947 if (key
->ps_prolog
.states
.bc_optimize_for_persp
||
7948 key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7949 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7950 LLVMValueRef center
[2], centroid
[2], tmp
, bc_optimize
;
7952 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7953 * The hw doesn't compute CENTROID if the whole wave only
7954 * contains fully-covered quads.
7956 * PRIM_MASK is after user SGPRs.
7958 bc_optimize
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
7959 bc_optimize
= LLVMBuildLShr(gallivm
->builder
, bc_optimize
,
7960 LLVMConstInt(ctx
->i32
, 31, 0), "");
7961 bc_optimize
= LLVMBuildTrunc(gallivm
->builder
, bc_optimize
,
7964 if (key
->ps_prolog
.states
.bc_optimize_for_persp
) {
7965 /* Read PERSP_CENTER. */
7966 for (i
= 0; i
< 2; i
++)
7967 center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
7968 /* Read PERSP_CENTROID. */
7969 for (i
= 0; i
< 2; i
++)
7970 centroid
[i
] = LLVMGetParam(func
, base
+ 4 + i
);
7971 /* Select PERSP_CENTROID. */
7972 for (i
= 0; i
< 2; i
++) {
7973 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
7974 center
[i
], centroid
[i
], "");
7975 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7976 tmp
, base
+ 4 + i
, "");
7979 if (key
->ps_prolog
.states
.bc_optimize_for_linear
) {
7980 /* Read LINEAR_CENTER. */
7981 for (i
= 0; i
< 2; i
++)
7982 center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
7983 /* Read LINEAR_CENTROID. */
7984 for (i
= 0; i
< 2; i
++)
7985 centroid
[i
] = LLVMGetParam(func
, base
+ 10 + i
);
7986 /* Select LINEAR_CENTROID. */
7987 for (i
= 0; i
< 2; i
++) {
7988 tmp
= LLVMBuildSelect(gallivm
->builder
, bc_optimize
,
7989 center
[i
], centroid
[i
], "");
7990 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
7991 tmp
, base
+ 10 + i
, "");
7996 /* Force per-sample interpolation. */
7997 if (key
->ps_prolog
.states
.force_persp_sample_interp
) {
7998 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
7999 LLVMValueRef persp_sample
[2];
8001 /* Read PERSP_SAMPLE. */
8002 for (i
= 0; i
< 2; i
++)
8003 persp_sample
[i
] = LLVMGetParam(func
, base
+ i
);
8004 /* Overwrite PERSP_CENTER. */
8005 for (i
= 0; i
< 2; i
++)
8006 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8007 persp_sample
[i
], base
+ 2 + i
, "");
8008 /* Overwrite PERSP_CENTROID. */
8009 for (i
= 0; i
< 2; i
++)
8010 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8011 persp_sample
[i
], base
+ 4 + i
, "");
8013 if (key
->ps_prolog
.states
.force_linear_sample_interp
) {
8014 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8015 LLVMValueRef linear_sample
[2];
8017 /* Read LINEAR_SAMPLE. */
8018 for (i
= 0; i
< 2; i
++)
8019 linear_sample
[i
] = LLVMGetParam(func
, base
+ 6 + i
);
8020 /* Overwrite LINEAR_CENTER. */
8021 for (i
= 0; i
< 2; i
++)
8022 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8023 linear_sample
[i
], base
+ 8 + i
, "");
8024 /* Overwrite LINEAR_CENTROID. */
8025 for (i
= 0; i
< 2; i
++)
8026 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8027 linear_sample
[i
], base
+ 10 + i
, "");
8030 /* Force center interpolation. */
8031 if (key
->ps_prolog
.states
.force_persp_center_interp
) {
8032 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8033 LLVMValueRef persp_center
[2];
8035 /* Read PERSP_CENTER. */
8036 for (i
= 0; i
< 2; i
++)
8037 persp_center
[i
] = LLVMGetParam(func
, base
+ 2 + i
);
8038 /* Overwrite PERSP_SAMPLE. */
8039 for (i
= 0; i
< 2; i
++)
8040 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8041 persp_center
[i
], base
+ i
, "");
8042 /* Overwrite PERSP_CENTROID. */
8043 for (i
= 0; i
< 2; i
++)
8044 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8045 persp_center
[i
], base
+ 4 + i
, "");
8047 if (key
->ps_prolog
.states
.force_linear_center_interp
) {
8048 unsigned i
, base
= key
->ps_prolog
.num_input_sgprs
;
8049 LLVMValueRef linear_center
[2];
8051 /* Read LINEAR_CENTER. */
8052 for (i
= 0; i
< 2; i
++)
8053 linear_center
[i
] = LLVMGetParam(func
, base
+ 8 + i
);
8054 /* Overwrite LINEAR_SAMPLE. */
8055 for (i
= 0; i
< 2; i
++)
8056 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8057 linear_center
[i
], base
+ 6 + i
, "");
8058 /* Overwrite LINEAR_CENTROID. */
8059 for (i
= 0; i
< 2; i
++)
8060 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
,
8061 linear_center
[i
], base
+ 10 + i
, "");
8064 /* Interpolate colors. */
8065 for (i
= 0; i
< 2; i
++) {
8066 unsigned writemask
= (key
->ps_prolog
.colors_read
>> (i
* 4)) & 0xf;
8067 unsigned face_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8068 key
->ps_prolog
.face_vgpr_index
;
8069 LLVMValueRef interp
[2], color
[4];
8070 LLVMValueRef interp_ij
= NULL
, prim_mask
= NULL
, face
= NULL
;
8075 /* If the interpolation qualifier is not CONSTANT (-1). */
8076 if (key
->ps_prolog
.color_interp_vgpr_index
[i
] != -1) {
8077 unsigned interp_vgpr
= key
->ps_prolog
.num_input_sgprs
+
8078 key
->ps_prolog
.color_interp_vgpr_index
[i
];
8080 /* Get the (i,j) updated by bc_optimize handling. */
8081 interp
[0] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8083 interp
[1] = LLVMBuildExtractValue(gallivm
->builder
, ret
,
8084 interp_vgpr
+ 1, "");
8085 interp_ij
= lp_build_gather_values(gallivm
, interp
, 2);
8088 /* Use the absolute location of the input. */
8089 prim_mask
= LLVMGetParam(func
, SI_PS_NUM_USER_SGPR
);
8091 if (key
->ps_prolog
.states
.color_two_side
) {
8092 face
= LLVMGetParam(func
, face_vgpr
);
8093 face
= LLVMBuildBitCast(gallivm
->builder
, face
, ctx
->i32
, "");
8096 interp_fs_input(ctx
,
8097 key
->ps_prolog
.color_attr_index
[i
],
8098 TGSI_SEMANTIC_COLOR
, i
,
8099 key
->ps_prolog
.num_interp_inputs
,
8100 key
->ps_prolog
.colors_read
, interp_ij
,
8101 prim_mask
, face
, color
);
8104 unsigned chan
= u_bit_scan(&writemask
);
8105 ret
= LLVMBuildInsertValue(gallivm
->builder
, ret
, color
[chan
],
8110 /* Tell LLVM to insert WQM instruction sequence when needed. */
8111 if (key
->ps_prolog
.wqm
) {
8112 LLVMAddTargetDependentFunctionAttr(func
,
8113 "amdgpu-ps-wqm-outputs", "");
8116 si_llvm_build_ret(ctx
, ret
);
8120 * Build the pixel shader epilog function. This handles everything that must be
8121 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
8123 static void si_build_ps_epilog_function(struct si_shader_context
*ctx
,
8124 union si_shader_part_key
*key
)
8126 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
8127 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
8128 LLVMTypeRef params
[16+8*4+3];
8129 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
8130 int last_sgpr
, num_params
, i
;
8131 struct si_ps_exports exp
= {};
8133 /* Declare input SGPRs. */
8134 params
[SI_PARAM_RW_BUFFERS
] = ctx
->i64
;
8135 params
[SI_PARAM_CONST_BUFFERS
] = ctx
->i64
;
8136 params
[SI_PARAM_SAMPLERS
] = ctx
->i64
;
8137 params
[SI_PARAM_IMAGES
] = ctx
->i64
;
8138 params
[SI_PARAM_SHADER_BUFFERS
] = ctx
->i64
;
8139 params
[SI_PARAM_ALPHA_REF
] = ctx
->f32
;
8140 last_sgpr
= SI_PARAM_ALPHA_REF
;
8142 /* Declare input VGPRs. */
8143 num_params
= (last_sgpr
+ 1) +
8144 util_bitcount(key
->ps_epilog
.colors_written
) * 4 +
8145 key
->ps_epilog
.writes_z
+
8146 key
->ps_epilog
.writes_stencil
+
8147 key
->ps_epilog
.writes_samplemask
;
8149 num_params
= MAX2(num_params
,
8150 last_sgpr
+ 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC
+ 1);
8152 assert(num_params
<= ARRAY_SIZE(params
));
8154 for (i
= last_sgpr
+ 1; i
< num_params
; i
++)
8155 params
[i
] = ctx
->f32
;
8157 /* Create the function. */
8158 si_create_function(ctx
, "ps_epilog", NULL
, 0, params
, num_params
, last_sgpr
);
8159 /* Disable elimination of unused inputs. */
8160 si_llvm_add_attribute(ctx
->main_fn
,
8161 "InitialPSInputAddr", 0xffffff);
8163 /* Process colors. */
8164 unsigned vgpr
= last_sgpr
+ 1;
8165 unsigned colors_written
= key
->ps_epilog
.colors_written
;
8166 int last_color_export
= -1;
8168 /* Find the last color export. */
8169 if (!key
->ps_epilog
.writes_z
&&
8170 !key
->ps_epilog
.writes_stencil
&&
8171 !key
->ps_epilog
.writes_samplemask
) {
8172 unsigned spi_format
= key
->ps_epilog
.states
.spi_shader_col_format
;
8174 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
8175 if (colors_written
== 0x1 && key
->ps_epilog
.states
.last_cbuf
> 0) {
8176 /* Just set this if any of the colorbuffers are enabled. */
8178 ((1llu << (4 * (key
->ps_epilog
.states
.last_cbuf
+ 1))) - 1))
8179 last_color_export
= 0;
8181 for (i
= 0; i
< 8; i
++)
8182 if (colors_written
& (1 << i
) &&
8183 (spi_format
>> (i
* 4)) & 0xf)
8184 last_color_export
= i
;
8188 while (colors_written
) {
8189 LLVMValueRef color
[4];
8190 int mrt
= u_bit_scan(&colors_written
);
8192 for (i
= 0; i
< 4; i
++)
8193 color
[i
] = LLVMGetParam(ctx
->main_fn
, vgpr
++);
8195 si_export_mrt_color(bld_base
, color
, mrt
,
8197 mrt
== last_color_export
, &exp
);
8200 /* Process depth, stencil, samplemask. */
8201 if (key
->ps_epilog
.writes_z
)
8202 depth
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8203 if (key
->ps_epilog
.writes_stencil
)
8204 stencil
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8205 if (key
->ps_epilog
.writes_samplemask
)
8206 samplemask
= LLVMGetParam(ctx
->main_fn
, vgpr
++);
8208 if (depth
|| stencil
|| samplemask
)
8209 si_export_mrt_z(bld_base
, depth
, stencil
, samplemask
, &exp
);
8210 else if (last_color_export
== -1)
8211 si_export_null(bld_base
);
8214 si_emit_ps_exports(ctx
, &exp
);
8217 LLVMBuildRetVoid(gallivm
->builder
);
8221 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
8223 static bool si_shader_select_ps_parts(struct si_screen
*sscreen
,
8224 LLVMTargetMachineRef tm
,
8225 struct si_shader
*shader
,
8226 struct pipe_debug_callback
*debug
)
8228 union si_shader_part_key prolog_key
;
8229 union si_shader_part_key epilog_key
;
8231 /* Get the prolog. */
8232 si_get_ps_prolog_key(shader
, &prolog_key
, true);
8234 /* The prolog is a no-op if these aren't set. */
8235 if (si_need_ps_prolog(&prolog_key
)) {
8237 si_get_shader_part(sscreen
, &sscreen
->ps_prologs
,
8238 PIPE_SHADER_FRAGMENT
, true,
8239 &prolog_key
, tm
, debug
,
8240 si_build_ps_prolog_function
,
8241 "Fragment Shader Prolog");
8242 if (!shader
->prolog
)
8246 /* Get the epilog. */
8247 si_get_ps_epilog_key(shader
, &epilog_key
);
8250 si_get_shader_part(sscreen
, &sscreen
->ps_epilogs
,
8251 PIPE_SHADER_FRAGMENT
, false,
8252 &epilog_key
, tm
, debug
,
8253 si_build_ps_epilog_function
,
8254 "Fragment Shader Epilog");
8255 if (!shader
->epilog
)
8258 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
8259 if (shader
->key
.part
.ps
.prolog
.poly_stipple
) {
8260 shader
->config
.spi_ps_input_ena
|= S_0286CC_POS_FIXED_PT_ENA(1);
8261 assert(G_0286CC_POS_FIXED_PT_ENA(shader
->config
.spi_ps_input_addr
));
8264 /* Set up the enable bits for per-sample shading if needed. */
8265 if (shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
&&
8266 (G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8267 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8268 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTER_ENA
;
8269 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8270 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_SAMPLE_ENA(1);
8272 if (shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
&&
8273 (G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_ena
) ||
8274 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8275 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTER_ENA
;
8276 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8277 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_SAMPLE_ENA(1);
8279 if (shader
->key
.part
.ps
.prolog
.force_persp_center_interp
&&
8280 (G_0286CC_PERSP_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8281 G_0286CC_PERSP_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8282 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_SAMPLE_ENA
;
8283 shader
->config
.spi_ps_input_ena
&= C_0286CC_PERSP_CENTROID_ENA
;
8284 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8286 if (shader
->key
.part
.ps
.prolog
.force_linear_center_interp
&&
8287 (G_0286CC_LINEAR_SAMPLE_ENA(shader
->config
.spi_ps_input_ena
) ||
8288 G_0286CC_LINEAR_CENTROID_ENA(shader
->config
.spi_ps_input_ena
))) {
8289 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_SAMPLE_ENA
;
8290 shader
->config
.spi_ps_input_ena
&= C_0286CC_LINEAR_CENTROID_ENA
;
8291 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8294 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
8295 if (G_0286CC_POS_W_FLOAT_ENA(shader
->config
.spi_ps_input_ena
) &&
8296 !(shader
->config
.spi_ps_input_ena
& 0xf)) {
8297 shader
->config
.spi_ps_input_ena
|= S_0286CC_PERSP_CENTER_ENA(1);
8298 assert(G_0286CC_PERSP_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8301 /* At least one pair of interpolation weights must be enabled. */
8302 if (!(shader
->config
.spi_ps_input_ena
& 0x7f)) {
8303 shader
->config
.spi_ps_input_ena
|= S_0286CC_LINEAR_CENTER_ENA(1);
8304 assert(G_0286CC_LINEAR_CENTER_ENA(shader
->config
.spi_ps_input_addr
));
8307 /* The sample mask input is always enabled, because the API shader always
8308 * passes it through to the epilog. Disable it here if it's unused.
8310 if (!shader
->key
.part
.ps
.epilog
.poly_line_smoothing
&&
8311 !shader
->selector
->info
.reads_samplemask
)
8312 shader
->config
.spi_ps_input_ena
&= C_0286CC_SAMPLE_COVERAGE_ENA
;
8317 void si_multiwave_lds_size_workaround(struct si_screen
*sscreen
,
8320 /* SPI barrier management bug:
8321 * Make sure we have at least 4k of LDS in use to avoid the bug.
8322 * It applies to workgroup sizes of more than one wavefront.
8324 if (sscreen
->b
.family
== CHIP_BONAIRE
||
8325 sscreen
->b
.family
== CHIP_KABINI
||
8326 sscreen
->b
.family
== CHIP_MULLINS
)
8327 *lds_size
= MAX2(*lds_size
, 8);
8330 static void si_fix_resource_usage(struct si_screen
*sscreen
,
8331 struct si_shader
*shader
)
8333 unsigned min_sgprs
= shader
->info
.num_input_sgprs
+ 2; /* VCC */
8335 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
, min_sgprs
);
8337 if (shader
->selector
->type
== PIPE_SHADER_COMPUTE
&&
8338 si_get_max_workgroup_size(shader
) > 64) {
8339 si_multiwave_lds_size_workaround(sscreen
,
8340 &shader
->config
.lds_size
);
8344 int si_shader_create(struct si_screen
*sscreen
, LLVMTargetMachineRef tm
,
8345 struct si_shader
*shader
,
8346 struct pipe_debug_callback
*debug
)
8348 struct si_shader_selector
*sel
= shader
->selector
;
8349 struct si_shader
*mainp
= sel
->main_shader_part
;
8352 /* LS, ES, VS are compiled on demand if the main part hasn't been
8353 * compiled for that stage.
8355 * Vertex shaders are compiled on demand when a vertex fetch
8356 * workaround must be applied.
8358 if (shader
->is_monolithic
) {
8359 /* Monolithic shader (compiled as a whole, has many variants,
8360 * may take a long time to compile).
8362 r
= si_compile_tgsi_shader(sscreen
, tm
, shader
, true, debug
);
8366 /* The shader consists of 2-3 parts:
8368 * - the middle part is the user shader, it has 1 variant only
8369 * and it was compiled during the creation of the shader
8371 * - the prolog part is inserted at the beginning
8372 * - the epilog part is inserted at the end
8374 * The prolog and epilog have many (but simple) variants.
8377 /* Copy the compiled TGSI shader data over. */
8378 shader
->is_binary_shared
= true;
8379 shader
->binary
= mainp
->binary
;
8380 shader
->config
= mainp
->config
;
8381 shader
->info
.num_input_sgprs
= mainp
->info
.num_input_sgprs
;
8382 shader
->info
.num_input_vgprs
= mainp
->info
.num_input_vgprs
;
8383 shader
->info
.face_vgpr_index
= mainp
->info
.face_vgpr_index
;
8384 memcpy(shader
->info
.vs_output_param_offset
,
8385 mainp
->info
.vs_output_param_offset
,
8386 sizeof(mainp
->info
.vs_output_param_offset
));
8387 shader
->info
.uses_instanceid
= mainp
->info
.uses_instanceid
;
8388 shader
->info
.nr_pos_exports
= mainp
->info
.nr_pos_exports
;
8389 shader
->info
.nr_param_exports
= mainp
->info
.nr_param_exports
;
8391 /* Select prologs and/or epilogs. */
8392 switch (sel
->type
) {
8393 case PIPE_SHADER_VERTEX
:
8394 if (!si_shader_select_vs_parts(sscreen
, tm
, shader
, debug
))
8397 case PIPE_SHADER_TESS_CTRL
:
8398 if (!si_shader_select_tcs_parts(sscreen
, tm
, shader
, debug
))
8401 case PIPE_SHADER_TESS_EVAL
:
8402 if (!si_shader_select_tes_parts(sscreen
, tm
, shader
, debug
))
8405 case PIPE_SHADER_GEOMETRY
:
8406 if (!si_shader_select_gs_parts(sscreen
, tm
, shader
, debug
))
8409 case PIPE_SHADER_FRAGMENT
:
8410 if (!si_shader_select_ps_parts(sscreen
, tm
, shader
, debug
))
8413 /* Make sure we have at least as many VGPRs as there
8414 * are allocated inputs.
8416 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8417 shader
->info
.num_input_vgprs
);
8421 /* Update SGPR and VGPR counts. */
8422 if (shader
->prolog
) {
8423 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8424 shader
->prolog
->config
.num_sgprs
);
8425 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8426 shader
->prolog
->config
.num_vgprs
);
8428 if (shader
->epilog
) {
8429 shader
->config
.num_sgprs
= MAX2(shader
->config
.num_sgprs
,
8430 shader
->epilog
->config
.num_sgprs
);
8431 shader
->config
.num_vgprs
= MAX2(shader
->config
.num_vgprs
,
8432 shader
->epilog
->config
.num_vgprs
);
8436 si_fix_resource_usage(sscreen
, shader
);
8437 si_shader_dump(sscreen
, shader
, debug
, sel
->info
.processor
,
8441 r
= si_shader_binary_upload(sscreen
, shader
);
8443 fprintf(stderr
, "LLVM failed to upload shader\n");
8450 void si_shader_destroy(struct si_shader
*shader
)
8452 if (shader
->scratch_bo
)
8453 r600_resource_reference(&shader
->scratch_bo
, NULL
);
8455 r600_resource_reference(&shader
->bo
, NULL
);
8457 if (!shader
->is_binary_shared
)
8458 radeon_shader_binary_clean(&shader
->binary
);
8460 free(shader
->shader_log
);