2 * Copyright 2017 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "si_shader_internal.h"
28 #include "tgsi/tgsi_build.h"
29 #include "tgsi/tgsi_util.h"
30 #include "ac_llvm_util.h"
32 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
33 struct lp_build_tgsi_context
*bld_base
,
34 struct lp_build_emit_data
*emit_data
);
36 static const struct lp_build_tgsi_action tex_action
;
39 * Given a v8i32 resource descriptor for a buffer, extract the size of the
40 * buffer in number of elements and return it as an i32.
42 static LLVMValueRef
get_buffer_size(
43 struct lp_build_tgsi_context
*bld_base
,
44 LLVMValueRef descriptor
)
46 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
47 LLVMBuilderRef builder
= ctx
->ac
.builder
;
49 LLVMBuildExtractElement(builder
, descriptor
,
50 LLVMConstInt(ctx
->i32
, 2, 0), "");
52 if (ctx
->screen
->info
.chip_class
== VI
) {
53 /* On VI, the descriptor contains the size in bytes,
54 * but TXQ must return the size in elements.
55 * The stride is always non-zero for resources using TXQ.
58 LLVMBuildExtractElement(builder
, descriptor
,
60 stride
= LLVMBuildLShr(builder
, stride
,
61 LLVMConstInt(ctx
->i32
, 16, 0), "");
62 stride
= LLVMBuildAnd(builder
, stride
,
63 LLVMConstInt(ctx
->i32
, 0x3FFF, 0), "");
65 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
72 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
73 const struct tgsi_full_src_register
*reg
,
78 if (!reg
->Register
.Indirect
) {
79 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, false);
81 index
= si_get_indirect_index(ctx
, ®
->Indirect
,
82 1, reg
->Register
.Index
);
86 return ctx
->abi
.load_ubo(&ctx
->abi
, index
);
88 return ctx
->abi
.load_ssbo(&ctx
->abi
, index
, false);
91 static enum ac_image_dim
92 ac_texture_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
96 case TGSI_TEXTURE_SHADOW1D
:
97 if (screen
->info
.chip_class
>= GFX9
)
100 case TGSI_TEXTURE_2D
:
101 case TGSI_TEXTURE_SHADOW2D
:
102 case TGSI_TEXTURE_RECT
:
103 case TGSI_TEXTURE_SHADOWRECT
:
105 case TGSI_TEXTURE_3D
:
107 case TGSI_TEXTURE_CUBE
:
108 case TGSI_TEXTURE_SHADOWCUBE
:
109 case TGSI_TEXTURE_CUBE_ARRAY
:
110 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
111 return ac_image_cube
;
112 case TGSI_TEXTURE_1D_ARRAY
:
113 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
114 if (screen
->info
.chip_class
>= GFX9
)
115 return ac_image_2darray
;
116 return ac_image_1darray
;
117 case TGSI_TEXTURE_2D_ARRAY
:
118 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
119 return ac_image_2darray
;
120 case TGSI_TEXTURE_2D_MSAA
:
121 return ac_image_2dmsaa
;
122 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
123 return ac_image_2darraymsaa
;
125 unreachable("unhandled texture type");
129 static enum ac_image_dim
130 ac_image_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
132 enum ac_image_dim dim
= ac_texture_dim_from_tgsi_target(screen
, target
);
134 /* Match the resource type set in the descriptor. */
135 if (dim
== ac_image_cube
||
136 (screen
->info
.chip_class
<= VI
&& dim
== ac_image_3d
))
137 dim
= ac_image_2darray
;
138 else if (target
== TGSI_TEXTURE_2D
&& screen
->info
.chip_class
>= GFX9
) {
139 /* When a single layer of a 3D texture is bound, the shader
140 * will refer to a 2D target, but the descriptor has a 3D type.
141 * Since the HW ignores BASE_ARRAY in this case, we need to
142 * send 3 coordinates. This doesn't hurt when the underlying
152 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
154 * At least on Tonga, executing image stores on images with DCC enabled and
155 * non-trivial can eventually lead to lockups. This can occur when an
156 * application binds an image as read-only but then uses a shader that writes
157 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
158 * program termination) in this case, but it doesn't cost much to be a bit
159 * nicer: disabling DCC in the shader still leads to undefined results but
162 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
165 if (ctx
->screen
->info
.chip_class
<= CIK
) {
168 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
169 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
172 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, i32_6
, "");
173 tmp
= LLVMBuildAnd(ctx
->ac
.builder
, tmp
, i32_C
, "");
174 return LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, tmp
, i32_6
, "");
178 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
179 LLVMValueRef list
, LLVMValueRef index
,
180 enum ac_descriptor_type desc_type
, bool dcc_off
)
182 LLVMBuilderRef builder
= ctx
->ac
.builder
;
185 if (desc_type
== AC_DESC_BUFFER
) {
186 index
= LLVMBuildMul(builder
, index
,
187 LLVMConstInt(ctx
->i32
, 2, 0), "");
188 index
= LLVMBuildAdd(builder
, index
,
190 list
= LLVMBuildPointerCast(builder
, list
,
191 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
193 assert(desc_type
== AC_DESC_IMAGE
);
196 rsrc
= ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
197 if (desc_type
== AC_DESC_IMAGE
&& dcc_off
)
198 rsrc
= force_dcc_off(ctx
, rsrc
);
203 * Load the resource descriptor for \p image.
207 struct lp_build_tgsi_context
*bld_base
,
208 const struct tgsi_full_src_register
*image
,
209 bool is_store
, unsigned target
,
212 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
213 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
214 ctx
->param_samplers_and_images
);
216 bool dcc_off
= is_store
;
218 if (!image
->Register
.Indirect
) {
219 const struct tgsi_shader_info
*info
= bld_base
->info
;
220 unsigned images_writemask
= info
->images_store
|
223 index
= LLVMConstInt(ctx
->i32
,
224 si_get_image_slot(image
->Register
.Index
), 0);
226 if (images_writemask
& (1 << image
->Register
.Index
))
229 /* From the GL_ARB_shader_image_load_store extension spec:
231 * If a shader performs an image load, store, or atomic
232 * operation using an image variable declared as an array,
233 * and if the index used to select an individual element is
234 * negative or greater than or equal to the size of the
235 * array, the results of the operation are undefined but may
236 * not lead to termination.
238 index
= si_get_bounded_indirect_index(ctx
, &image
->Indirect
,
239 image
->Register
.Index
,
241 index
= LLVMBuildSub(ctx
->ac
.builder
,
242 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
- 1, 0),
246 if (image
->Register
.File
!= TGSI_FILE_IMAGE
) {
247 /* Bindless descriptors are accessible from a different pair of
250 rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
251 ctx
->param_bindless_samplers_and_images
);
252 index
= lp_build_emit_fetch_src(bld_base
, image
,
253 TGSI_TYPE_UNSIGNED
, 0);
255 /* For simplicity, bindless image descriptors use fixed
256 * 16-dword slots for now.
258 index
= LLVMBuildMul(ctx
->ac
.builder
, index
,
259 LLVMConstInt(ctx
->i32
, 2, 0), "");
262 *rsrc
= si_load_image_desc(ctx
, rsrc_ptr
, index
,
263 target
== TGSI_TEXTURE_BUFFER
? AC_DESC_BUFFER
: AC_DESC_IMAGE
,
267 static void image_fetch_coords(
268 struct lp_build_tgsi_context
*bld_base
,
269 const struct tgsi_full_instruction
*inst
,
270 unsigned src
, LLVMValueRef desc
,
271 LLVMValueRef
*coords
)
273 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
274 LLVMBuilderRef builder
= ctx
->ac
.builder
;
275 unsigned target
= inst
->Memory
.Texture
;
276 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
280 if (target
== TGSI_TEXTURE_2D_MSAA
||
281 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
282 /* Need the sample index as well. */
286 for (chan
= 0; chan
< num_coords
; ++chan
) {
287 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
288 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
292 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
293 /* 1D textures are allocated and used as 2D on GFX9. */
294 if (target
== TGSI_TEXTURE_1D
) {
295 coords
[1] = ctx
->i32_0
;
296 } else if (target
== TGSI_TEXTURE_1D_ARRAY
) {
297 coords
[2] = coords
[1];
298 coords
[1] = ctx
->i32_0
;
299 } else if (target
== TGSI_TEXTURE_2D
) {
300 /* The hw can't bind a slice of a 3D image as a 2D
301 * image, because it ignores BASE_ARRAY if the target
302 * is 3D. The workaround is to read BASE_ARRAY and set
303 * it as the 3rd address operand for all 2D images.
305 LLVMValueRef first_layer
, const5
, mask
;
307 const5
= LLVMConstInt(ctx
->i32
, 5, 0);
308 mask
= LLVMConstInt(ctx
->i32
, S_008F24_BASE_ARRAY(~0), 0);
309 first_layer
= LLVMBuildExtractElement(builder
, desc
, const5
, "");
310 first_layer
= LLVMBuildAnd(builder
, first_layer
, mask
, "");
312 coords
[2] = first_layer
;
318 * Append the resource and indexing arguments for buffer intrinsics.
320 * \param rsrc the v4i32 buffer resource
321 * \param index index into the buffer (stride-based)
322 * \param offset byte offset into the buffer
324 static void buffer_append_args(
325 struct si_shader_context
*ctx
,
326 struct lp_build_emit_data
*emit_data
,
333 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
334 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
335 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
337 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
338 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
339 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
341 emit_data
->args
[emit_data
->arg_count
++] =
343 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
344 i1true
: i1false
; /* glc */
346 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
349 static void load_fetch_args(
350 struct lp_build_tgsi_context
* bld_base
,
351 struct lp_build_emit_data
* emit_data
)
353 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
354 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
355 unsigned target
= inst
->Memory
.Texture
;
358 emit_data
->dst_type
= ctx
->v4f32
;
360 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
361 inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
365 bool ubo
= inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
;
366 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], ubo
);
368 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
369 offset
= ac_to_integer(&ctx
->ac
, tmp
);
371 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
372 offset
, false, false);
373 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
374 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
375 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
376 image_fetch_coords(bld_base
, inst
, 1, rsrc
, &emit_data
->args
[1]);
378 if (target
== TGSI_TEXTURE_BUFFER
) {
379 buffer_append_args(ctx
, emit_data
, rsrc
, emit_data
->args
[1],
380 ctx
->i32_0
, false, false);
382 emit_data
->args
[0] = rsrc
;
387 static void load_emit_buffer(struct si_shader_context
*ctx
,
388 struct lp_build_emit_data
*emit_data
,
389 bool can_speculate
, bool allow_smem
)
391 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
392 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
393 uint count
= util_last_bit(writemask
);
394 LLVMValueRef
*args
= emit_data
->args
;
396 /* Don't use SMEM for shader buffer loads, because LLVM doesn't
397 * select SMEM for SI.load.const with a non-constant offset, and
398 * constant offsets practically don't exist with shader buffers.
400 * Also, SI.load.const doesn't use inst_offset when it's lowered
401 * to VMEM, so we just end up with more VALU instructions in the end
404 * TODO: Remove this line once LLVM can select SMEM with a non-constant
405 * offset, and can derive inst_offset when VMEM is selected.
406 * After that, si_memory_barrier should invalidate sL1 for shader
410 assert(LLVMConstIntGetZExtValue(args
[1]) == 0); /* vindex */
411 emit_data
->output
[emit_data
->chan
] =
412 ac_build_buffer_load(&ctx
->ac
, args
[0], count
, NULL
,
414 LLVMConstIntGetZExtValue(args
[3]),
415 LLVMConstIntGetZExtValue(args
[4]),
416 can_speculate
, allow_smem
);
419 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
420 const struct tgsi_full_instruction
*inst
,
421 LLVMTypeRef type
, int arg
)
423 LLVMBuilderRef builder
= ctx
->ac
.builder
;
424 LLVMValueRef offset
, ptr
;
427 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
428 offset
= ac_to_integer(&ctx
->ac
, offset
);
431 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
432 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
433 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
438 static void load_emit_memory(
439 struct si_shader_context
*ctx
,
440 struct lp_build_emit_data
*emit_data
)
442 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
443 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
444 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
447 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 1);
449 for (chan
= 0; chan
< 4; ++chan
) {
450 if (!(writemask
& (1 << chan
))) {
451 channels
[chan
] = LLVMGetUndef(ctx
->f32
);
455 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
456 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
457 channels
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
459 emit_data
->output
[emit_data
->chan
] = ac_build_gather_values(&ctx
->ac
, channels
, 4);
463 * Return true if the memory accessed by a LOAD or STORE instruction is
464 * read-only or write-only, respectively.
466 * \param shader_buffers_reverse_access_mask
467 * For LOAD, set this to (store | atomic) slot usage in the shader.
468 * For STORE, set this to (load | atomic) slot usage in the shader.
469 * \param images_reverse_access_mask Same as above, but for images.
471 static bool is_oneway_access_only(const struct tgsi_full_instruction
*inst
,
472 const struct tgsi_shader_info
*info
,
473 unsigned shader_buffers_reverse_access_mask
,
474 unsigned images_reverse_access_mask
)
476 /* RESTRICT means NOALIAS.
477 * If there are no writes, we can assume the accessed memory is read-only.
478 * If there are no reads, we can assume the accessed memory is write-only.
480 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_RESTRICT
) {
481 unsigned reverse_access_mask
;
483 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
484 reverse_access_mask
= shader_buffers_reverse_access_mask
;
485 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
486 reverse_access_mask
= info
->images_buffers
&
487 images_reverse_access_mask
;
489 reverse_access_mask
= ~info
->images_buffers
&
490 images_reverse_access_mask
;
493 if (inst
->Src
[0].Register
.Indirect
) {
494 if (!reverse_access_mask
)
497 if (!(reverse_access_mask
&
498 (1u << inst
->Src
[0].Register
.Index
)))
503 /* If there are no buffer writes (for both shader buffers & image
504 * buffers), it implies that buffer memory is read-only.
505 * If there are no buffer reads (for both shader buffers & image
506 * buffers), it implies that buffer memory is write-only.
508 * Same for the case when there are no writes/reads for non-buffer
511 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
512 (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
&&
513 (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
514 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)))) {
515 if (!shader_buffers_reverse_access_mask
&&
516 !(info
->images_buffers
& images_reverse_access_mask
))
519 if (!(~info
->images_buffers
& images_reverse_access_mask
))
525 static void load_emit(
526 const struct lp_build_tgsi_action
*action
,
527 struct lp_build_tgsi_context
*bld_base
,
528 struct lp_build_emit_data
*emit_data
)
530 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
531 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
532 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
533 bool can_speculate
= false;
535 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
536 load_emit_memory(ctx
, emit_data
);
540 if (inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
541 load_emit_buffer(ctx
, emit_data
, true, true);
545 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
546 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
548 can_speculate
= !(inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
) &&
549 is_oneway_access_only(inst
, info
,
550 info
->shader_buffers_store
|
551 info
->shader_buffers_atomic
,
553 info
->images_atomic
);
555 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
556 load_emit_buffer(ctx
, emit_data
, can_speculate
, false);
560 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
561 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
562 LLVMValueRef result
=
563 ac_build_buffer_load_format(&ctx
->ac
,
568 LLVMConstIntGetZExtValue(emit_data
->args
[3]),
570 emit_data
->output
[emit_data
->chan
] =
571 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
573 struct ac_image_args args
= {};
574 args
.opcode
= ac_image_load
;
575 args
.resource
= emit_data
->args
[0];
576 memcpy(args
.coords
, &emit_data
->args
[1], sizeof(args
.coords
));
577 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
578 if (inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
))
579 args
.cache_policy
= ac_glc
;
580 args
.attributes
= ac_get_load_intr_attribs(can_speculate
);
583 emit_data
->output
[emit_data
->chan
] =
584 ac_build_image_opcode(&ctx
->ac
, &args
);
588 static void store_fetch_args(
589 struct lp_build_tgsi_context
* bld_base
,
590 struct lp_build_emit_data
* emit_data
)
592 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
593 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
594 struct tgsi_full_src_register memory
;
595 LLVMValueRef chans
[4];
600 emit_data
->dst_type
= ctx
->voidt
;
602 for (chan
= 0; chan
< 4; ++chan
) {
603 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
605 data
= ac_build_gather_values(&ctx
->ac
, chans
, 4);
607 emit_data
->args
[emit_data
->arg_count
++] = data
;
609 memory
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
611 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
615 rsrc
= shader_buffer_fetch_rsrc(ctx
, &memory
, false);
617 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
618 offset
= ac_to_integer(&ctx
->ac
, tmp
);
620 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
621 offset
, false, false);
622 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
||
623 tgsi_is_bindless_image_file(inst
->Dst
[0].Register
.File
)) {
624 unsigned target
= inst
->Memory
.Texture
;
626 /* 8bit/16bit TC L1 write corruption bug on SI.
627 * All store opcodes not aligned to a dword are affected.
629 * The only way to get unaligned stores in radeonsi is through
632 bool force_glc
= ctx
->screen
->info
.chip_class
== SI
;
634 image_fetch_rsrc(bld_base
, &memory
, true, target
, &rsrc
);
635 image_fetch_coords(bld_base
, inst
, 0, rsrc
, &emit_data
->args
[2]);
637 if (target
== TGSI_TEXTURE_BUFFER
) {
638 buffer_append_args(ctx
, emit_data
, rsrc
, emit_data
->args
[2],
639 ctx
->i32_0
, false, force_glc
);
641 emit_data
->args
[1] = rsrc
;
646 static void store_emit_buffer(
647 struct si_shader_context
*ctx
,
648 struct lp_build_emit_data
*emit_data
,
649 bool writeonly_memory
)
651 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
652 LLVMBuilderRef builder
= ctx
->ac
.builder
;
653 LLVMValueRef base_data
= emit_data
->args
[0];
654 LLVMValueRef base_offset
= emit_data
->args
[3];
655 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
657 /* If this is write-only, don't keep data in L1 to prevent
658 * evicting L1 cache lines that may be needed by other
661 if (writeonly_memory
)
662 emit_data
->args
[4] = LLVMConstInt(ctx
->i1
, 1, 0); /* GLC = 1 */
666 const char *intrinsic_name
;
671 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
673 /* Due to an LLVM limitation, split 3-element writes
674 * into a 2-element and a 1-element write. */
676 writemask
|= 1 << (start
+ 2);
682 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
683 } else if (count
== 2) {
684 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
686 tmp
= LLVMBuildExtractElement(
688 LLVMConstInt(ctx
->i32
, start
, 0), "");
689 data
= LLVMBuildInsertElement(
690 builder
, LLVMGetUndef(v2f32
), tmp
,
693 tmp
= LLVMBuildExtractElement(
695 LLVMConstInt(ctx
->i32
, start
+ 1, 0), "");
696 data
= LLVMBuildInsertElement(
697 builder
, data
, tmp
, ctx
->i32_1
, "");
699 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
702 data
= LLVMBuildExtractElement(
704 LLVMConstInt(ctx
->i32
, start
, 0), "");
705 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
708 offset
= base_offset
;
710 offset
= LLVMBuildAdd(
712 LLVMConstInt(ctx
->i32
, start
* 4, 0), "");
715 emit_data
->args
[0] = data
;
716 emit_data
->args
[3] = offset
;
719 &ctx
->ac
, intrinsic_name
, emit_data
->dst_type
,
720 emit_data
->args
, emit_data
->arg_count
,
721 ac_get_store_intr_attribs(writeonly_memory
));
725 static void store_emit_memory(
726 struct si_shader_context
*ctx
,
727 struct lp_build_emit_data
*emit_data
)
729 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
730 LLVMBuilderRef builder
= ctx
->ac
.builder
;
731 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
732 LLVMValueRef ptr
, derived_ptr
, data
, index
;
735 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 0);
737 for (chan
= 0; chan
< 4; ++chan
) {
738 if (!(writemask
& (1 << chan
))) {
741 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
742 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
743 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
744 LLVMBuildStore(builder
, data
, derived_ptr
);
748 static void store_emit(
749 const struct lp_build_tgsi_action
*action
,
750 struct lp_build_tgsi_context
*bld_base
,
751 struct lp_build_emit_data
*emit_data
)
753 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
754 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
755 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
756 unsigned target
= inst
->Memory
.Texture
;
757 bool writeonly_memory
= false;
759 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
760 store_emit_memory(ctx
, emit_data
);
764 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
765 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
767 writeonly_memory
= is_oneway_access_only(inst
, info
,
768 info
->shader_buffers_load
|
769 info
->shader_buffers_atomic
,
771 info
->images_atomic
);
773 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
774 store_emit_buffer(ctx
, emit_data
, writeonly_memory
);
778 if (target
== TGSI_TEXTURE_BUFFER
) {
779 /* If this is write-only, don't keep data in L1 to prevent
780 * evicting L1 cache lines that may be needed by other
783 if (writeonly_memory
)
784 emit_data
->args
[4] = LLVMConstInt(ctx
->i1
, 1, 0); /* GLC = 1 */
786 emit_data
->output
[emit_data
->chan
] = ac_build_intrinsic(
787 &ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32",
788 emit_data
->dst_type
, emit_data
->args
,
789 emit_data
->arg_count
,
790 ac_get_store_intr_attribs(writeonly_memory
));
792 struct ac_image_args args
= {};
793 args
.opcode
= ac_image_store
;
794 args
.data
[0] = emit_data
->args
[0];
795 args
.resource
= emit_data
->args
[1];
796 memcpy(args
.coords
, &emit_data
->args
[2], sizeof(args
.coords
));
797 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
798 args
.attributes
= ac_get_store_intr_attribs(writeonly_memory
);
801 /* Workaround for 8bit/16bit TC L1 write corruption bug on SI.
802 * All store opcodes not aligned to a dword are affected.
804 if (ctx
->screen
->info
.chip_class
== SI
||
805 /* If this is write-only, don't keep data in L1 to prevent
806 * evicting L1 cache lines that may be needed by other
809 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
))
810 args
.cache_policy
= ac_glc
;
812 emit_data
->output
[emit_data
->chan
] =
813 ac_build_image_opcode(&ctx
->ac
, &args
);
817 static void atomic_fetch_args(
818 struct lp_build_tgsi_context
* bld_base
,
819 struct lp_build_emit_data
* emit_data
)
821 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
822 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
823 LLVMValueRef data1
, data2
;
827 emit_data
->dst_type
= ctx
->f32
;
829 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
830 data1
= ac_to_integer(&ctx
->ac
, tmp
);
832 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
833 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
834 data2
= ac_to_integer(&ctx
->ac
, tmp
);
837 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
838 * of arguments, which is reversed relative to TGSI (and GLSL)
840 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
841 emit_data
->args
[emit_data
->arg_count
++] = data2
;
842 emit_data
->args
[emit_data
->arg_count
++] = data1
;
844 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
847 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], false);
849 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
850 offset
= ac_to_integer(&ctx
->ac
, tmp
);
852 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
853 offset
, true, false);
854 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
855 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
856 unsigned target
= inst
->Memory
.Texture
;
858 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
859 image_fetch_coords(bld_base
, inst
, 1, rsrc
,
860 &emit_data
->args
[emit_data
->arg_count
+ 1]);
862 if (target
== TGSI_TEXTURE_BUFFER
) {
863 buffer_append_args(ctx
, emit_data
, rsrc
,
864 emit_data
->args
[emit_data
->arg_count
+ 1],
865 ctx
->i32_0
, true, false);
867 emit_data
->args
[emit_data
->arg_count
] = rsrc
;
872 static void atomic_emit_memory(struct si_shader_context
*ctx
,
873 struct lp_build_emit_data
*emit_data
) {
874 LLVMBuilderRef builder
= ctx
->ac
.builder
;
875 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
876 LLVMValueRef ptr
, result
, arg
;
878 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
880 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
881 arg
= ac_to_integer(&ctx
->ac
, arg
);
883 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
884 LLVMValueRef new_data
;
885 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
888 new_data
= ac_to_integer(&ctx
->ac
, new_data
);
890 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
891 LLVMAtomicOrderingSequentiallyConsistent
,
892 LLVMAtomicOrderingSequentiallyConsistent
,
895 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
897 LLVMAtomicRMWBinOp op
;
899 switch(inst
->Instruction
.Opcode
) {
900 case TGSI_OPCODE_ATOMUADD
:
901 op
= LLVMAtomicRMWBinOpAdd
;
903 case TGSI_OPCODE_ATOMXCHG
:
904 op
= LLVMAtomicRMWBinOpXchg
;
906 case TGSI_OPCODE_ATOMAND
:
907 op
= LLVMAtomicRMWBinOpAnd
;
909 case TGSI_OPCODE_ATOMOR
:
910 op
= LLVMAtomicRMWBinOpOr
;
912 case TGSI_OPCODE_ATOMXOR
:
913 op
= LLVMAtomicRMWBinOpXor
;
915 case TGSI_OPCODE_ATOMUMIN
:
916 op
= LLVMAtomicRMWBinOpUMin
;
918 case TGSI_OPCODE_ATOMUMAX
:
919 op
= LLVMAtomicRMWBinOpUMax
;
921 case TGSI_OPCODE_ATOMIMIN
:
922 op
= LLVMAtomicRMWBinOpMin
;
924 case TGSI_OPCODE_ATOMIMAX
:
925 op
= LLVMAtomicRMWBinOpMax
;
928 unreachable("unknown atomic opcode");
931 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
932 LLVMAtomicOrderingSequentiallyConsistent
,
935 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
938 static void atomic_emit(
939 const struct lp_build_tgsi_action
*action
,
940 struct lp_build_tgsi_context
*bld_base
,
941 struct lp_build_emit_data
*emit_data
)
943 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
944 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
947 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
948 atomic_emit_memory(ctx
, emit_data
);
952 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
953 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
954 char intrinsic_name
[40];
955 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
956 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
957 tmp
= ac_build_intrinsic(
958 &ctx
->ac
, intrinsic_name
, ctx
->i32
,
959 emit_data
->args
, emit_data
->arg_count
, 0);
960 emit_data
->output
[emit_data
->chan
] = ac_to_float(&ctx
->ac
, tmp
);
962 unsigned num_data
= inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
? 2 : 1;
963 struct ac_image_args args
= {};
965 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
966 args
.opcode
= ac_image_atomic_cmpswap
;
968 args
.opcode
= ac_image_atomic
;
969 switch (inst
->Instruction
.Opcode
) {
970 case TGSI_OPCODE_ATOMXCHG
: args
.atomic
= ac_atomic_swap
; break;
971 case TGSI_OPCODE_ATOMUADD
: args
.atomic
= ac_atomic_add
; break;
972 case TGSI_OPCODE_ATOMAND
: args
.atomic
= ac_atomic_and
; break;
973 case TGSI_OPCODE_ATOMOR
: args
.atomic
= ac_atomic_or
; break;
974 case TGSI_OPCODE_ATOMXOR
: args
.atomic
= ac_atomic_xor
; break;
975 case TGSI_OPCODE_ATOMUMIN
: args
.atomic
= ac_atomic_umin
; break;
976 case TGSI_OPCODE_ATOMUMAX
: args
.atomic
= ac_atomic_umax
; break;
977 case TGSI_OPCODE_ATOMIMIN
: args
.atomic
= ac_atomic_smin
; break;
978 case TGSI_OPCODE_ATOMIMAX
: args
.atomic
= ac_atomic_smax
; break;
979 default: unreachable("unhandled image atomic");
983 for (unsigned i
= 0; i
< num_data
; ++i
)
984 args
.data
[i
] = emit_data
->args
[i
];
986 args
.resource
= emit_data
->args
[num_data
];
987 memcpy(args
.coords
, &emit_data
->args
[num_data
+ 1], sizeof(args
.coords
));
988 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
990 emit_data
->output
[emit_data
->chan
] =
991 ac_to_float(&ctx
->ac
, ac_build_image_opcode(&ctx
->ac
, &args
));
995 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
996 struct lp_build_emit_data
*emit_data
,
997 struct ac_image_args
*args
,
1000 args
->dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1001 args
->unorm
= target
== TGSI_TEXTURE_RECT
||
1002 target
== TGSI_TEXTURE_SHADOWRECT
;
1004 /* Ugly, but we seem to have no other choice right now. */
1005 STATIC_ASSERT(sizeof(*args
) <= sizeof(emit_data
->args
));
1006 memcpy(emit_data
->args
, args
, sizeof(*args
));
1009 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
1010 unsigned target
, LLVMValueRef out
)
1012 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1014 /* 1D textures are allocated and used as 2D on GFX9. */
1015 if (ctx
->screen
->info
.chip_class
>= GFX9
&&
1016 (target
== TGSI_TEXTURE_1D_ARRAY
||
1017 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
1018 LLVMValueRef layers
=
1019 LLVMBuildExtractElement(builder
, out
,
1020 LLVMConstInt(ctx
->i32
, 2, 0), "");
1021 out
= LLVMBuildInsertElement(builder
, out
, layers
,
1025 /* Divide the number of layers by 6 to get the number of cubes. */
1026 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
1027 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1028 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
1030 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
1031 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
1033 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
1038 static void resq_fetch_args(
1039 struct lp_build_tgsi_context
* bld_base
,
1040 struct lp_build_emit_data
* emit_data
)
1042 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1043 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1044 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1046 emit_data
->dst_type
= ctx
->v4i32
;
1048 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
1049 emit_data
->args
[0] = shader_buffer_fetch_rsrc(ctx
, reg
, false);
1050 emit_data
->arg_count
= 1;
1051 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1052 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
1053 &emit_data
->args
[0]);
1054 emit_data
->arg_count
= 1;
1056 struct ac_image_args args
= {};
1057 unsigned image_target
;
1059 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
1060 image_target
= TGSI_TEXTURE_2D_ARRAY
;
1062 image_target
= inst
->Memory
.Texture
;
1064 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
1066 args
.lod
= ctx
->i32_0
;
1068 set_tex_fetch_args(ctx
, emit_data
, &args
, image_target
);
1072 static void resq_emit(
1073 const struct lp_build_tgsi_action
*action
,
1074 struct lp_build_tgsi_context
*bld_base
,
1075 struct lp_build_emit_data
*emit_data
)
1077 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1078 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1079 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1082 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
1083 out
= LLVMBuildExtractElement(builder
, emit_data
->args
[0],
1084 LLVMConstInt(ctx
->i32
, 2, 0), "");
1085 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1086 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
1088 struct ac_image_args args
;
1090 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1091 args
.opcode
= ac_image_get_resinfo
;
1092 out
= ac_build_image_opcode(&ctx
->ac
, &args
);
1094 out
= fix_resinfo(ctx
, inst
->Memory
.Texture
, out
);
1097 emit_data
->output
[emit_data
->chan
] = out
;
1101 * Load an image view, fmask view. or sampler state descriptor.
1103 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
1104 LLVMValueRef list
, LLVMValueRef index
,
1105 enum ac_descriptor_type type
)
1107 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1111 /* The image is at [0:7]. */
1112 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1114 case AC_DESC_BUFFER
:
1115 /* The buffer is in [4:7]. */
1116 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1117 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1118 list
= LLVMBuildPointerCast(builder
, list
,
1119 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1122 /* The FMASK is at [8:15]. */
1123 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1124 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1126 case AC_DESC_SAMPLER
:
1127 /* The sampler state is at [12:15]. */
1128 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1129 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
1130 list
= LLVMBuildPointerCast(builder
, list
,
1131 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1135 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
1138 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1141 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1142 * filtering manually. The driver sets img7 to a mask clearing
1143 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1144 * s_and_b32 samp0, samp0, img7
1147 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1149 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
1150 LLVMValueRef res
, LLVMValueRef samp
)
1152 LLVMValueRef img7
, samp0
;
1154 if (ctx
->screen
->info
.chip_class
>= VI
)
1157 img7
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1158 LLVMConstInt(ctx
->i32
, 7, 0), "");
1159 samp0
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp
,
1161 samp0
= LLVMBuildAnd(ctx
->ac
.builder
, samp0
, img7
, "");
1162 return LLVMBuildInsertElement(ctx
->ac
.builder
, samp
, samp0
,
1166 static void tex_fetch_ptrs(
1167 struct lp_build_tgsi_context
*bld_base
,
1168 struct lp_build_emit_data
*emit_data
,
1169 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
1171 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1172 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
1173 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1174 const struct tgsi_full_src_register
*reg
;
1175 unsigned target
= inst
->Texture
.Texture
;
1176 unsigned sampler_src
;
1179 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1180 reg
= &emit_data
->inst
->Src
[sampler_src
];
1182 if (reg
->Register
.Indirect
) {
1183 index
= si_get_bounded_indirect_index(ctx
,
1185 reg
->Register
.Index
,
1187 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1188 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
/ 2, 0), "");
1190 index
= LLVMConstInt(ctx
->i32
,
1191 si_get_sampler_slot(reg
->Register
.Index
), 0);
1194 if (reg
->Register
.File
!= TGSI_FILE_SAMPLER
) {
1195 /* Bindless descriptors are accessible from a different pair of
1196 * user SGPR indices.
1198 list
= LLVMGetParam(ctx
->main_fn
,
1199 ctx
->param_bindless_samplers_and_images
);
1200 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1201 TGSI_TYPE_UNSIGNED
, 0);
1204 if (target
== TGSI_TEXTURE_BUFFER
)
1205 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_BUFFER
);
1207 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_IMAGE
);
1214 if (target
== TGSI_TEXTURE_2D_MSAA
||
1215 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1217 *fmask_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1219 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
1221 *samp_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1223 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
1228 static void txq_fetch_args(
1229 struct lp_build_tgsi_context
*bld_base
,
1230 struct lp_build_emit_data
*emit_data
)
1232 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1233 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1234 unsigned target
= inst
->Texture
.Texture
;
1235 struct ac_image_args args
= {};
1237 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, NULL
, NULL
);
1239 if (target
== TGSI_TEXTURE_BUFFER
) {
1240 /* Read the size from the buffer descriptor directly. */
1241 emit_data
->args
[0] = get_buffer_size(bld_base
, args
.resource
);
1245 /* Textures - set the mip level. */
1246 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1249 set_tex_fetch_args(ctx
, emit_data
, &args
, target
);
1252 static void txq_emit(const struct lp_build_tgsi_action
*action
,
1253 struct lp_build_tgsi_context
*bld_base
,
1254 struct lp_build_emit_data
*emit_data
)
1256 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1257 struct ac_image_args args
;
1258 unsigned target
= emit_data
->inst
->Texture
.Texture
;
1260 if (target
== TGSI_TEXTURE_BUFFER
) {
1261 /* Just return the buffer size. */
1262 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
1266 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1268 args
.opcode
= ac_image_get_resinfo
;
1269 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1271 emit_data
->output
[emit_data
->chan
] = fix_resinfo(ctx
, target
, result
);
1274 static void tex_fetch_args(
1275 struct lp_build_tgsi_context
*bld_base
,
1276 struct lp_build_emit_data
*emit_data
)
1278 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1279 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1280 unsigned opcode
= inst
->Instruction
.Opcode
;
1281 unsigned target
= inst
->Texture
.Texture
;
1282 struct ac_image_args args
= {};
1283 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1285 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1286 LLVMValueRef fmask_ptr
= NULL
;
1288 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
1290 if (target
== TGSI_TEXTURE_BUFFER
) {
1291 emit_data
->dst_type
= ctx
->v4f32
;
1292 emit_data
->args
[0] = args
.resource
;
1293 emit_data
->args
[1] = ctx
->i32_0
;
1294 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
1295 emit_data
->arg_count
= 3;
1299 /* Fetch and project texture coordinates */
1300 args
.coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1301 for (chan
= 0; chan
< 3; chan
++) {
1302 args
.coords
[chan
] = lp_build_emit_fetch(bld_base
,
1305 if (opcode
== TGSI_OPCODE_TXP
)
1306 args
.coords
[chan
] = ac_build_fdiv(&ctx
->ac
,
1307 args
.coords
[chan
], args
.coords
[3]);
1310 if (opcode
== TGSI_OPCODE_TXP
)
1311 args
.coords
[3] = ctx
->ac
.f32_1
;
1315 opcode
!= TGSI_OPCODE_TXF
&&
1316 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1317 /* The offsets are six-bit signed integers packed like this:
1318 * X=[5:0], Y=[13:8], and Z=[21:16].
1320 LLVMValueRef offset
[3], pack
;
1322 assert(inst
->Texture
.NumOffsets
== 1);
1324 for (chan
= 0; chan
< 3; chan
++) {
1325 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
1326 emit_data
->inst
, 0, chan
);
1327 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1328 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1330 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1331 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1334 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1335 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1339 /* Pack LOD bias value */
1340 if (opcode
== TGSI_OPCODE_TXB
)
1341 args
.bias
= args
.coords
[3];
1342 if (opcode
== TGSI_OPCODE_TXB2
)
1343 args
.bias
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1345 /* Pack depth comparison value */
1346 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1349 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1350 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1352 assert(ref_pos
>= 0);
1353 z
= args
.coords
[ref_pos
];
1356 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1357 * OpenGL 4.5 spec says:
1359 * "If the texture’s internal format indicates a fixed-point
1360 * depth texture, then D_t and D_ref are clamped to the
1361 * range [0, 1]; otherwise no clamping is performed."
1363 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1364 * so the depth comparison value isn't clamped for Z16 and
1365 * Z24 anymore. Do it manually here.
1367 if (ctx
->screen
->info
.chip_class
>= VI
) {
1368 LLVMValueRef upgraded
;
1369 LLVMValueRef clamped
;
1370 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
1371 LLVMConstInt(ctx
->i32
, 3, false), "");
1372 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1373 LLVMConstInt(ctx
->i32
, 29, false), "");
1374 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1375 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1376 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1382 /* Pack user derivatives */
1383 if (opcode
== TGSI_OPCODE_TXD
) {
1384 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1387 case TGSI_TEXTURE_3D
:
1388 num_src_deriv_channels
= 3;
1389 num_dst_deriv_channels
= 3;
1391 case TGSI_TEXTURE_2D
:
1392 case TGSI_TEXTURE_SHADOW2D
:
1393 case TGSI_TEXTURE_RECT
:
1394 case TGSI_TEXTURE_SHADOWRECT
:
1395 case TGSI_TEXTURE_2D_ARRAY
:
1396 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1397 num_src_deriv_channels
= 2;
1398 num_dst_deriv_channels
= 2;
1400 case TGSI_TEXTURE_CUBE
:
1401 case TGSI_TEXTURE_SHADOWCUBE
:
1402 case TGSI_TEXTURE_CUBE_ARRAY
:
1403 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1404 /* Cube derivatives will be converted to 2D. */
1405 num_src_deriv_channels
= 3;
1406 num_dst_deriv_channels
= 3;
1408 case TGSI_TEXTURE_1D
:
1409 case TGSI_TEXTURE_SHADOW1D
:
1410 case TGSI_TEXTURE_1D_ARRAY
:
1411 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1412 num_src_deriv_channels
= 1;
1414 /* 1D textures are allocated and used as 2D on GFX9. */
1415 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1416 num_dst_deriv_channels
= 2;
1418 num_dst_deriv_channels
= 1;
1422 unreachable("invalid target");
1425 for (param
= 0; param
< 2; param
++) {
1426 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1427 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1428 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1430 /* Fill in the rest with zeros. */
1431 for (chan
= num_src_deriv_channels
;
1432 chan
< num_dst_deriv_channels
; chan
++)
1433 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1438 if (target
== TGSI_TEXTURE_CUBE
||
1439 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1440 target
== TGSI_TEXTURE_SHADOWCUBE
||
1441 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1442 ac_prepare_cube_coords(&ctx
->ac
,
1443 opcode
== TGSI_OPCODE_TXD
,
1444 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1445 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1446 opcode
== TGSI_OPCODE_LODQ
,
1447 args
.coords
, args
.derivs
);
1448 } else if (tgsi_is_array_sampler(target
) &&
1449 opcode
!= TGSI_OPCODE_TXF
&&
1450 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1451 ctx
->screen
->info
.chip_class
<= VI
) {
1452 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1453 args
.coords
[array_coord
] =
1454 ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
,
1455 &args
.coords
[array_coord
], 1, 0);
1458 /* 1D textures are allocated and used as 2D on GFX9. */
1459 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1460 LLVMValueRef filler
;
1462 /* Use 0.5, so that we don't sample the border color. */
1463 if (opcode
== TGSI_OPCODE_TXF
||
1464 opcode
== TGSI_OPCODE_TXF_LZ
)
1465 filler
= ctx
->i32_0
;
1467 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1469 if (target
== TGSI_TEXTURE_1D
||
1470 target
== TGSI_TEXTURE_SHADOW1D
) {
1471 args
.coords
[1] = filler
;
1472 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1473 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1474 args
.coords
[2] = args
.coords
[1];
1475 args
.coords
[1] = filler
;
1479 /* Pack LOD or sample index */
1480 if (opcode
== TGSI_OPCODE_TXL
)
1481 args
.lod
= args
.coords
[3];
1482 else if (opcode
== TGSI_OPCODE_TXL2
)
1483 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1484 else if (opcode
== TGSI_OPCODE_TXF
) {
1485 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1486 /* No LOD, but move sample index into the right place. */
1487 args
.coords
[2] = args
.coords
[3];
1488 } else if (target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1489 args
.lod
= args
.coords
[3];
1493 if (target
== TGSI_TEXTURE_2D_MSAA
||
1494 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1495 ac_apply_fmask_to_sample(&ctx
->ac
, fmask_ptr
, args
.coords
,
1496 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
1499 if (opcode
== TGSI_OPCODE_TXF
||
1500 opcode
== TGSI_OPCODE_TXF_LZ
) {
1501 /* add tex offsets */
1502 if (inst
->Texture
.NumOffsets
) {
1503 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1505 assert(inst
->Texture
.NumOffsets
== 1);
1508 case TGSI_TEXTURE_3D
:
1510 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[2],
1511 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
], "");
1513 case TGSI_TEXTURE_2D
:
1514 case TGSI_TEXTURE_SHADOW2D
:
1515 case TGSI_TEXTURE_RECT
:
1516 case TGSI_TEXTURE_SHADOWRECT
:
1517 case TGSI_TEXTURE_2D_ARRAY
:
1518 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1520 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[1],
1521 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
], "");
1523 case TGSI_TEXTURE_1D
:
1524 case TGSI_TEXTURE_SHADOW1D
:
1525 case TGSI_TEXTURE_1D_ARRAY
:
1526 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1528 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[0],
1529 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
], "");
1531 /* texture offsets do not apply to other texture targets */
1538 if (opcode
== TGSI_OPCODE_TG4
) {
1539 unsigned gather_comp
= 0;
1541 /* DMASK was repurposed for GATHER4. 4 components are always
1542 * returned and DMASK works like a swizzle - it selects
1543 * the component to fetch. The only valid DMASK values are
1544 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1545 * (red,red,red,red) etc.) The ISA document doesn't mention
1549 /* Get the component index from src1.x for Gather4. */
1550 if (!tgsi_is_shadow_target(target
)) {
1551 LLVMValueRef comp_imm
;
1552 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1554 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1556 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1557 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1558 gather_comp
= CLAMP(gather_comp
, 0, 3);
1561 args
.dmask
= 1 << gather_comp
;
1564 set_tex_fetch_args(ctx
, emit_data
, &args
, target
);
1567 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1568 * incorrectly forces nearest filtering if the texture format is integer.
1569 * The only effect it has on Gather4, which always returns 4 texels for
1570 * bilinear filtering, is that the final coordinates are off by 0.5 of
1573 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1574 * or (0.5 / size) from the normalized coordinates.
1576 * However, cube textures with 8_8_8_8 data formats require a different
1577 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1578 * precision in 32-bit data formats, so it needs to be applied dynamically at
1579 * runtime. In this case, return an i1 value that indicates whether the
1580 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1583 si_lower_gather4_integer(struct si_shader_context
*ctx
,
1584 struct ac_image_args
*args
,
1586 enum tgsi_return_type return_type
)
1588 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1589 LLVMValueRef wa_8888
= NULL
;
1590 LLVMValueRef half_texel
[2];
1592 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1593 return_type
== TGSI_RETURN_TYPE_UINT
);
1595 if (target
== TGSI_TEXTURE_CUBE
||
1596 target
== TGSI_TEXTURE_CUBE_ARRAY
) {
1597 LLVMValueRef formats
;
1598 LLVMValueRef data_format
;
1599 LLVMValueRef wa_formats
;
1601 formats
= LLVMBuildExtractElement(builder
, args
->resource
, ctx
->i32_1
, "");
1603 data_format
= LLVMBuildLShr(builder
, formats
,
1604 LLVMConstInt(ctx
->i32
, 20, false), "");
1605 data_format
= LLVMBuildAnd(builder
, data_format
,
1606 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1607 wa_8888
= LLVMBuildICmp(
1608 builder
, LLVMIntEQ
, data_format
,
1609 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1612 uint32_t wa_num_format
=
1613 return_type
== TGSI_RETURN_TYPE_UINT
?
1614 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1615 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1616 wa_formats
= LLVMBuildAnd(builder
, formats
,
1617 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false),
1619 wa_formats
= LLVMBuildOr(builder
, wa_formats
,
1620 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1622 formats
= LLVMBuildSelect(builder
, wa_8888
, wa_formats
, formats
, "");
1623 args
->resource
= LLVMBuildInsertElement(
1624 builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1627 if (target
== TGSI_TEXTURE_RECT
||
1628 target
== TGSI_TEXTURE_SHADOWRECT
) {
1630 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1632 struct tgsi_full_instruction txq_inst
= {};
1633 struct ac_image_args txq_args
= {};
1634 struct lp_build_emit_data txq_emit_data
= {};
1635 struct lp_build_if_state if_ctx
;
1638 /* Skip the texture size query entirely if we don't need it. */
1639 lp_build_if(&if_ctx
, &ctx
->gallivm
, LLVMBuildNot(builder
, wa_8888
, ""));
1642 /* Query the texture size. */
1643 txq_inst
.Texture
.Texture
= target
;
1644 txq_emit_data
.inst
= &txq_inst
;
1645 txq_emit_data
.dst_type
= ctx
->v4i32
;
1646 txq_args
.resource
= args
->resource
;
1647 txq_args
.sampler
= args
->sampler
;
1648 txq_args
.lod
= ctx
->ac
.i32_0
;
1649 txq_args
.dmask
= 0xf;
1650 set_tex_fetch_args(ctx
, &txq_emit_data
, &txq_args
, target
);
1651 txq_emit(NULL
, &ctx
->bld_base
, &txq_emit_data
);
1653 /* Compute -0.5 / size. */
1654 for (unsigned c
= 0; c
< 2; c
++) {
1656 LLVMBuildExtractElement(builder
, txq_emit_data
.output
[0],
1657 LLVMConstInt(ctx
->i32
, c
, 0), "");
1658 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1659 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, half_texel
[c
]);
1660 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1661 LLVMConstReal(ctx
->f32
, -0.5), "");
1665 lp_build_endif(&if_ctx
);
1667 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1669 for (unsigned c
= 0; c
< 2; c
++) {
1670 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1671 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1677 for (unsigned c
= 0; c
< 2; c
++) {
1679 tmp
= ac_to_float(&ctx
->ac
, args
->coords
[c
]);
1680 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1681 args
->coords
[c
] = ac_to_integer(&ctx
->ac
, tmp
);
1687 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1688 * result after the gather operation.
1691 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1692 LLVMValueRef result
,
1693 enum tgsi_return_type return_type
,
1696 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1698 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1699 return_type
== TGSI_RETURN_TYPE_UINT
);
1701 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1702 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1704 LLVMValueRef wa_value
;
1706 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1708 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1709 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1711 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1712 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1713 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1715 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1721 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1722 struct lp_build_tgsi_context
*bld_base
,
1723 struct lp_build_emit_data
*emit_data
)
1725 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1726 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1727 struct ac_image_args args
;
1728 unsigned opcode
= inst
->Instruction
.Opcode
;
1729 unsigned target
= inst
->Texture
.Texture
;
1731 if (target
== TGSI_TEXTURE_BUFFER
) {
1732 unsigned num_channels
=
1733 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1734 LLVMValueRef result
=
1735 ac_build_buffer_load_format(&ctx
->ac
,
1739 num_channels
, false, true);
1740 emit_data
->output
[emit_data
->chan
] =
1741 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1745 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1747 args
.opcode
= ac_image_sample
;
1750 case TGSI_OPCODE_TXF
:
1751 case TGSI_OPCODE_TXF_LZ
:
1752 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1753 target
== TGSI_TEXTURE_2D_MSAA
||
1754 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1755 ac_image_load
: ac_image_load_mip
;
1757 case TGSI_OPCODE_LODQ
:
1758 args
.opcode
= ac_image_get_lod
;
1760 case TGSI_OPCODE_TEX
:
1761 case TGSI_OPCODE_TEX2
:
1762 case TGSI_OPCODE_TXP
:
1763 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1764 args
.level_zero
= true;
1766 case TGSI_OPCODE_TEX_LZ
:
1767 args
.level_zero
= true;
1769 case TGSI_OPCODE_TXB
:
1770 case TGSI_OPCODE_TXB2
:
1771 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1773 case TGSI_OPCODE_TXL
:
1774 case TGSI_OPCODE_TXL2
:
1776 case TGSI_OPCODE_TXD
:
1778 case TGSI_OPCODE_TG4
:
1779 args
.opcode
= ac_image_gather4
;
1780 args
.level_zero
= true;
1787 /* The hardware needs special lowering for Gather4 with integer formats. */
1788 LLVMValueRef gather4_int_result_workaround
= NULL
;
1790 if (ctx
->screen
->info
.chip_class
<= VI
&&
1791 opcode
== TGSI_OPCODE_TG4
) {
1792 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1794 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1795 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1796 gather4_int_result_workaround
=
1797 si_lower_gather4_integer(ctx
, &args
, target
,
1798 inst
->Texture
.ReturnType
);
1802 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1803 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1805 if (gather4_int_result_workaround
) {
1806 result
= si_fix_gather4_integer_result(ctx
, result
,
1807 inst
->Texture
.ReturnType
,
1808 gather4_int_result_workaround
);
1811 emit_data
->output
[emit_data
->chan
] = result
;
1814 static void si_llvm_emit_txqs(
1815 const struct lp_build_tgsi_action
*action
,
1816 struct lp_build_tgsi_context
*bld_base
,
1817 struct lp_build_emit_data
*emit_data
)
1819 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1820 LLVMValueRef res
, samples
;
1821 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
1823 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
1825 /* Read the samples from the descriptor directly. */
1826 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->v8i32
, "");
1827 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1828 LLVMConstInt(ctx
->i32
, 3, 0), "");
1829 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
1830 LLVMConstInt(ctx
->i32
, 16, 0), "");
1831 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
1832 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
1833 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->i32_1
,
1836 emit_data
->output
[emit_data
->chan
] = samples
;
1839 static void si_llvm_emit_fbfetch(const struct lp_build_tgsi_action
*action
,
1840 struct lp_build_tgsi_context
*bld_base
,
1841 struct lp_build_emit_data
*emit_data
)
1843 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1844 struct ac_image_args args
= {};
1845 LLVMValueRef ptr
, image
, fmask
;
1847 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1851 /* Load the image descriptor. */
1852 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
1853 ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1854 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
1855 ac_array_in_const32_addr_space(ctx
->v8i32
), "");
1856 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1857 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
1861 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 0, 16);
1863 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1864 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 16, 16);
1866 /* Get the current render target layer index. */
1867 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
1868 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 16, 11);
1870 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1871 args
.coords
[chan
++] = si_get_sample_id(ctx
);
1873 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
) {
1874 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1875 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
1877 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
1878 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
1881 args
.opcode
= ac_image_load
;
1882 args
.resource
= image
;
1884 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1885 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1886 ac_image_2darraymsaa
: ac_image_2dmsaa
;
1887 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1888 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1889 ac_image_1darray
: ac_image_1d
;
1891 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1892 ac_image_2darray
: ac_image_2d
;
1894 emit_data
->output
[emit_data
->chan
] =
1895 ac_build_image_opcode(&ctx
->ac
, &args
);
1898 static const struct lp_build_tgsi_action tex_action
= {
1899 .fetch_args
= tex_fetch_args
,
1900 .emit
= build_tex_intrinsic
,
1904 * Setup actions for TGSI memory opcode, including texture opcodes.
1906 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1908 struct lp_build_tgsi_context
*bld_base
;
1909 struct lp_build_tgsi_action tmpl
= {};
1911 bld_base
= &ctx
->bld_base
;
1913 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1914 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
] = tex_action
;
1915 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
1916 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
1917 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
1918 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
1919 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
1920 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
] = tex_action
;
1921 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
1922 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
1923 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1924 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= txq_fetch_args
;
1925 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
1926 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
1927 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
1928 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1930 bld_base
->op_actions
[TGSI_OPCODE_FBFETCH
].emit
= si_llvm_emit_fbfetch
;
1932 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
1933 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1934 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
1935 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1936 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
1937 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1939 tmpl
.fetch_args
= atomic_fetch_args
;
1940 tmpl
.emit
= atomic_emit
;
1941 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
1942 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1943 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
1944 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1945 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
1946 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1947 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
1948 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1949 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
1950 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1951 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
1952 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1953 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
1954 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
1955 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
1956 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
1957 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
1958 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
1959 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
1960 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";