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 <llvm/Config/llvm-config.h>
27 #include "si_shader_internal.h"
30 #include "tgsi/tgsi_build.h"
31 #include "tgsi/tgsi_util.h"
32 #include "ac_llvm_util.h"
34 static void tex_fetch_ptrs(struct lp_build_tgsi_context
*bld_base
,
35 struct lp_build_emit_data
*emit_data
,
36 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
37 LLVMValueRef
*fmask_ptr
);
40 * Given a v8i32 resource descriptor for a buffer, extract the size of the
41 * buffer in number of elements and return it as an i32.
43 static LLVMValueRef
get_buffer_size(
44 struct lp_build_tgsi_context
*bld_base
,
45 LLVMValueRef descriptor
)
47 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
48 LLVMBuilderRef builder
= ctx
->ac
.builder
;
50 LLVMBuildExtractElement(builder
, descriptor
,
51 LLVMConstInt(ctx
->i32
, 2, 0), "");
53 if (ctx
->screen
->info
.chip_class
== GFX8
) {
54 /* On GFX8, the descriptor contains the size in bytes,
55 * but TXQ must return the size in elements.
56 * The stride is always non-zero for resources using TXQ.
59 LLVMBuildExtractElement(builder
, descriptor
,
61 stride
= LLVMBuildLShr(builder
, stride
,
62 LLVMConstInt(ctx
->i32
, 16, 0), "");
63 stride
= LLVMBuildAnd(builder
, stride
,
64 LLVMConstInt(ctx
->i32
, 0x3FFF, 0), "");
66 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
73 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
74 const struct tgsi_full_src_register
*reg
,
79 if (!reg
->Register
.Indirect
) {
80 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, false);
82 index
= si_get_indirect_index(ctx
, ®
->Indirect
,
83 1, reg
->Register
.Index
);
87 return ctx
->abi
.load_ubo(&ctx
->abi
, index
);
89 return ctx
->abi
.load_ssbo(&ctx
->abi
, index
, false);
92 static enum ac_image_dim
93 ac_texture_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
97 case TGSI_TEXTURE_SHADOW1D
:
98 if (screen
->info
.chip_class
== GFX9
)
101 case TGSI_TEXTURE_2D
:
102 case TGSI_TEXTURE_SHADOW2D
:
103 case TGSI_TEXTURE_RECT
:
104 case TGSI_TEXTURE_SHADOWRECT
:
106 case TGSI_TEXTURE_3D
:
108 case TGSI_TEXTURE_CUBE
:
109 case TGSI_TEXTURE_SHADOWCUBE
:
110 case TGSI_TEXTURE_CUBE_ARRAY
:
111 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
112 return ac_image_cube
;
113 case TGSI_TEXTURE_1D_ARRAY
:
114 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
115 if (screen
->info
.chip_class
== GFX9
)
116 return ac_image_2darray
;
117 return ac_image_1darray
;
118 case TGSI_TEXTURE_2D_ARRAY
:
119 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
120 return ac_image_2darray
;
121 case TGSI_TEXTURE_2D_MSAA
:
122 return ac_image_2dmsaa
;
123 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
124 return ac_image_2darraymsaa
;
126 unreachable("unhandled texture type");
130 static enum ac_image_dim
131 ac_image_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
133 enum ac_image_dim dim
= ac_texture_dim_from_tgsi_target(screen
, target
);
135 /* Match the resource type set in the descriptor. */
136 if (dim
== ac_image_cube
||
137 (screen
->info
.chip_class
<= GFX8
&& dim
== ac_image_3d
))
138 dim
= ac_image_2darray
;
139 else if (target
== TGSI_TEXTURE_2D
&& screen
->info
.chip_class
== GFX9
) {
140 /* When a single layer of a 3D texture is bound, the shader
141 * will refer to a 2D target, but the descriptor has a 3D type.
142 * Since the HW ignores BASE_ARRAY in this case, we need to
143 * send 3 coordinates. This doesn't hurt when the underlying
153 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
155 * At least on Tonga, executing image stores on images with DCC enabled and
156 * non-trivial can eventually lead to lockups. This can occur when an
157 * application binds an image as read-only but then uses a shader that writes
158 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
159 * program termination) in this case, but it doesn't cost much to be a bit
160 * nicer: disabling DCC in the shader still leads to undefined results but
163 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
166 if (ctx
->screen
->info
.chip_class
<= GFX7
) {
169 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
170 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
173 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, i32_6
, "");
174 tmp
= LLVMBuildAnd(ctx
->ac
.builder
, tmp
, i32_C
, "");
175 return LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, tmp
, i32_6
, "");
179 /* AC_DESC_FMASK is handled exactly like AC_DESC_IMAGE. The caller should
180 * adjust "index" to point to FMASK. */
181 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
182 LLVMValueRef list
, LLVMValueRef index
,
183 enum ac_descriptor_type desc_type
,
184 bool uses_store
, bool bindless
)
186 LLVMBuilderRef builder
= ctx
->ac
.builder
;
189 if (desc_type
== AC_DESC_BUFFER
) {
190 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 2, 0),
192 list
= LLVMBuildPointerCast(builder
, list
,
193 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
195 assert(desc_type
== AC_DESC_IMAGE
||
196 desc_type
== AC_DESC_FMASK
);
200 rsrc
= ac_build_load_to_sgpr_uint_wraparound(&ctx
->ac
, list
, index
);
202 rsrc
= ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
204 if (desc_type
== AC_DESC_IMAGE
&& uses_store
)
205 rsrc
= force_dcc_off(ctx
, rsrc
);
210 * Load the resource descriptor for \p image.
214 struct lp_build_tgsi_context
*bld_base
,
215 const struct tgsi_full_src_register
*image
,
216 bool fmask
, bool is_store
, unsigned target
,
219 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
220 bool bindless
= image
->Register
.File
!= TGSI_FILE_IMAGE
;
221 LLVMValueRef rsrc_ptr
, index
;
224 /* Bindless descriptors are accessible from a different pair of
227 rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
228 ctx
->param_bindless_samplers_and_images
);
229 index
= lp_build_emit_fetch_src(bld_base
, image
, TGSI_TYPE_UNSIGNED
, 0);
231 /* Bindless image descriptors use 16-dword slots. */
232 index
= LLVMBuildMul(ctx
->ac
.builder
, index
,
233 LLVMConstInt(ctx
->i32
, 2, 0), "");
234 /* FMASK is right after the image. */
236 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
, ctx
->i32_1
, "");
238 rsrc_ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
240 if (!image
->Register
.Indirect
) {
241 index
= LLVMConstInt(ctx
->i32
, image
->Register
.Index
, 0);
243 /* From the GL_ARB_shader_image_load_store extension spec:
245 * If a shader performs an image load, store, or atomic
246 * operation using an image variable declared as an array,
247 * and if the index used to select an individual element is
248 * negative or greater than or equal to the size of the
249 * array, the results of the operation are undefined but may
250 * not lead to termination.
252 index
= si_get_bounded_indirect_index(ctx
, &image
->Indirect
,
253 image
->Register
.Index
,
256 /* FMASKs are separate from images. */
258 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
259 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
, 0), "");
261 index
= LLVMBuildSub(ctx
->ac
.builder
,
262 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGE_SLOTS
- 1, 0),
266 *rsrc
= si_load_image_desc(ctx
, rsrc_ptr
, index
,
267 fmask
? AC_DESC_FMASK
:
268 target
== TGSI_TEXTURE_BUFFER
? AC_DESC_BUFFER
: AC_DESC_IMAGE
,
272 static void image_fetch_coords(
273 struct lp_build_tgsi_context
*bld_base
,
274 const struct tgsi_full_instruction
*inst
,
275 unsigned src
, LLVMValueRef desc
,
276 LLVMValueRef
*coords
)
278 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
279 LLVMBuilderRef builder
= ctx
->ac
.builder
;
280 unsigned target
= inst
->Memory
.Texture
;
281 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
285 for (chan
= 0; chan
< num_coords
; ++chan
) {
286 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
287 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
291 if (target
== TGSI_TEXTURE_2D_MSAA
||
292 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
293 /* Need the sample index as well. */
294 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, TGSI_SWIZZLE_W
);
295 coords
[chan
] = ac_to_integer(&ctx
->ac
, tmp
);
298 if (ctx
->screen
->info
.chip_class
== GFX9
) {
299 /* 1D textures are allocated and used as 2D on GFX9. */
300 if (target
== TGSI_TEXTURE_1D
) {
301 coords
[1] = ctx
->i32_0
;
302 } else if (target
== TGSI_TEXTURE_1D_ARRAY
) {
303 coords
[2] = coords
[1];
304 coords
[1] = ctx
->i32_0
;
305 } else if (target
== TGSI_TEXTURE_2D
) {
306 /* The hw can't bind a slice of a 3D image as a 2D
307 * image, because it ignores BASE_ARRAY if the target
308 * is 3D. The workaround is to read BASE_ARRAY and set
309 * it as the 3rd address operand for all 2D images.
311 LLVMValueRef first_layer
, const5
, mask
;
313 const5
= LLVMConstInt(ctx
->i32
, 5, 0);
314 mask
= LLVMConstInt(ctx
->i32
, S_008F24_BASE_ARRAY(~0), 0);
315 first_layer
= LLVMBuildExtractElement(builder
, desc
, const5
, "");
316 first_layer
= LLVMBuildAnd(builder
, first_layer
, mask
, "");
318 coords
[2] = first_layer
;
323 static unsigned get_cache_policy(struct si_shader_context
*ctx
,
324 const struct tgsi_full_instruction
*inst
,
325 bool atomic
, bool may_store_unaligned
,
326 bool writeonly_memory
)
328 unsigned cache_policy
= 0;
331 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores.
332 * All store opcodes not aligned to a dword are affected.
333 * The only way to get unaligned stores in radeonsi is through
335 ((may_store_unaligned
&& ctx
->screen
->info
.chip_class
== GFX6
) ||
336 /* If this is write-only, don't keep data in L1 to prevent
337 * evicting L1 cache lines that may be needed by other
340 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
))) {
341 cache_policy
|= ac_glc
;
344 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_STREAM_CACHE_POLICY
)
345 cache_policy
|= ac_slc
;
350 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
351 const struct tgsi_full_instruction
*inst
,
352 LLVMTypeRef type
, int arg
)
354 LLVMBuilderRef builder
= ctx
->ac
.builder
;
355 LLVMValueRef offset
, ptr
;
358 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
359 offset
= ac_to_integer(&ctx
->ac
, offset
);
362 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
363 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
364 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
369 static void load_emit_memory(
370 struct si_shader_context
*ctx
,
371 struct lp_build_emit_data
*emit_data
)
373 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
374 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
375 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
378 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 1);
380 for (chan
= 0; chan
< 4; ++chan
) {
381 if (!(writemask
& (1 << chan
))) {
382 channels
[chan
] = LLVMGetUndef(ctx
->f32
);
386 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
387 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
388 channels
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
390 emit_data
->output
[emit_data
->chan
] = ac_build_gather_values(&ctx
->ac
, channels
, 4);
394 * Return true if the memory accessed by a LOAD or STORE instruction is
395 * read-only or write-only, respectively.
397 * \param shader_buffers_reverse_access_mask
398 * For LOAD, set this to (store | atomic) slot usage in the shader.
399 * For STORE, set this to (load | atomic) slot usage in the shader.
400 * \param images_reverse_access_mask Same as above, but for images.
401 * \param bindless_buffer_reverse_access_mask Same as above, but for bindless image buffers.
402 * \param bindless_image_reverse_access_mask Same as above, but for bindless images.
404 static bool is_oneway_access_only(const struct tgsi_full_instruction
*inst
,
405 const struct tgsi_shader_info
*info
,
406 unsigned shader_buffers_reverse_access_mask
,
407 unsigned images_reverse_access_mask
,
408 bool bindless_buffer_reverse_access_mask
,
409 bool bindless_image_reverse_access_mask
)
411 enum tgsi_file_type resource_file
;
412 unsigned resource_index
;
413 bool resource_indirect
;
415 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
) {
416 resource_file
= inst
->Dst
[0].Register
.File
;
417 resource_index
= inst
->Dst
[0].Register
.Index
;
418 resource_indirect
= inst
->Dst
[0].Register
.Indirect
;
420 resource_file
= inst
->Src
[0].Register
.File
;
421 resource_index
= inst
->Src
[0].Register
.Index
;
422 resource_indirect
= inst
->Src
[0].Register
.Indirect
;
425 assert(resource_file
== TGSI_FILE_BUFFER
||
426 resource_file
== TGSI_FILE_IMAGE
||
428 resource_file
== TGSI_FILE_INPUT
||
429 resource_file
== TGSI_FILE_OUTPUT
||
430 resource_file
== TGSI_FILE_CONSTANT
||
431 resource_file
== TGSI_FILE_TEMPORARY
||
432 resource_file
== TGSI_FILE_IMMEDIATE
);
434 assert(resource_file
!= TGSI_FILE_BUFFER
||
435 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
);
437 bool bindless
= resource_file
!= TGSI_FILE_BUFFER
&&
438 resource_file
!= TGSI_FILE_IMAGE
;
440 /* RESTRICT means NOALIAS.
441 * If there are no writes, we can assume the accessed memory is read-only.
442 * If there are no reads, we can assume the accessed memory is write-only.
444 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_RESTRICT
&& !bindless
) {
445 unsigned reverse_access_mask
;
447 if (resource_file
== TGSI_FILE_BUFFER
) {
448 reverse_access_mask
= shader_buffers_reverse_access_mask
;
449 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
450 reverse_access_mask
= info
->images_buffers
&
451 images_reverse_access_mask
;
453 reverse_access_mask
= ~info
->images_buffers
&
454 images_reverse_access_mask
;
457 if (resource_indirect
) {
458 if (!reverse_access_mask
)
461 if (!(reverse_access_mask
&
462 (1u << resource_index
)))
467 /* If there are no buffer writes (for both shader buffers & image
468 * buffers), it implies that buffer memory is read-only.
469 * If there are no buffer reads (for both shader buffers & image
470 * buffers), it implies that buffer memory is write-only.
472 * Same for the case when there are no writes/reads for non-buffer
475 if (resource_file
== TGSI_FILE_BUFFER
||
476 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
477 if (!shader_buffers_reverse_access_mask
&&
478 !(info
->images_buffers
& images_reverse_access_mask
) &&
479 !bindless_buffer_reverse_access_mask
)
482 if (!(~info
->images_buffers
& images_reverse_access_mask
) &&
483 !bindless_image_reverse_access_mask
)
489 static void load_emit(
490 const struct lp_build_tgsi_action
*action
,
491 struct lp_build_tgsi_context
*bld_base
,
492 struct lp_build_emit_data
*emit_data
)
494 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
495 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
496 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
497 bool can_speculate
= false;
498 LLVMValueRef vindex
= ctx
->i32_0
;
499 LLVMValueRef voffset
= ctx
->i32_0
;
500 struct ac_image_args args
= {};
502 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
503 load_emit_memory(ctx
, emit_data
);
507 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
508 inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
509 bool ubo
= inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
;
510 args
.resource
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], ubo
);
511 voffset
= ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 1, 0));
513 unsigned target
= inst
->Memory
.Texture
;
515 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, false, target
, &args
.resource
);
516 image_fetch_coords(bld_base
, inst
, 1, args
.resource
, args
.coords
);
518 if ((inst
->Memory
.Texture
== TGSI_TEXTURE_2D_MSAA
||
519 inst
->Memory
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
) &&
520 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
523 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, false, target
, &fmask
);
524 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
525 inst
->Memory
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
527 vindex
= args
.coords
[0]; /* for buffers only */
530 if (inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
531 emit_data
->output
[emit_data
->chan
] =
532 ac_build_buffer_load(&ctx
->ac
, args
.resource
,
533 util_last_bit(inst
->Dst
[0].Register
.WriteMask
),
534 NULL
, voffset
, NULL
, 0, 0, true, true);
538 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
539 ac_build_waitcnt(&ctx
->ac
, AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
541 can_speculate
= !(inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
) &&
542 is_oneway_access_only(inst
, info
,
543 info
->shader_buffers_store
|
544 info
->shader_buffers_atomic
,
547 info
->uses_bindless_buffer_store
|
548 info
->uses_bindless_buffer_atomic
,
549 info
->uses_bindless_image_store
|
550 info
->uses_bindless_image_atomic
);
551 args
.cache_policy
= get_cache_policy(ctx
, inst
, false, false, false);
553 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
554 /* Don't use SMEM for shader buffer loads, because LLVM doesn't
555 * select SMEM for SI.load.const with a non-constant offset, and
556 * constant offsets practically don't exist with shader buffers.
558 * Also, SI.load.const doesn't use inst_offset when it's lowered
559 * to VMEM, so we just end up with more VALU instructions in the end
562 * TODO: Remove this line once LLVM can select SMEM with a non-constant
563 * offset, and can derive inst_offset when VMEM is selected.
564 * After that, si_memory_barrier should invalidate sL1 for shader
567 emit_data
->output
[emit_data
->chan
] =
568 ac_build_buffer_load(&ctx
->ac
, args
.resource
,
569 util_last_bit(inst
->Dst
[0].Register
.WriteMask
),
570 NULL
, voffset
, NULL
, 0,
571 args
.cache_policy
, can_speculate
, false);
575 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
576 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
577 LLVMValueRef result
=
578 ac_build_buffer_load_format(&ctx
->ac
,
585 emit_data
->output
[emit_data
->chan
] =
586 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
588 args
.opcode
= ac_image_load
;
589 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
590 args
.attributes
= ac_get_load_intr_attribs(can_speculate
);
593 emit_data
->output
[emit_data
->chan
] =
594 ac_build_image_opcode(&ctx
->ac
, &args
);
598 static void store_emit_buffer(struct si_shader_context
*ctx
,
599 LLVMValueRef resource
,
602 LLVMValueRef voffset
,
603 unsigned cache_policy
,
604 bool writeonly_memory
)
606 LLVMBuilderRef builder
= ctx
->ac
.builder
;
607 LLVMValueRef base_data
= value
;
608 LLVMValueRef base_offset
= voffset
;
612 LLVMValueRef data
, voff
;
614 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
616 if (count
== 3 && ac_has_vec3_support(ctx
->ac
.chip_class
, false)) {
617 LLVMValueRef values
[3] = {
618 LLVMBuildExtractElement(builder
, base_data
,
619 LLVMConstInt(ctx
->i32
, start
, 0), ""),
620 LLVMBuildExtractElement(builder
, base_data
,
621 LLVMConstInt(ctx
->i32
, start
+ 1, 0), ""),
622 LLVMBuildExtractElement(builder
, base_data
,
623 LLVMConstInt(ctx
->i32
, start
+ 2, 0), ""),
625 data
= ac_build_gather_values(&ctx
->ac
, values
, 3);
626 } else if (count
>= 3) {
628 } else if (count
== 2) {
629 LLVMValueRef values
[2] = {
630 LLVMBuildExtractElement(builder
, base_data
,
631 LLVMConstInt(ctx
->i32
, start
, 0), ""),
632 LLVMBuildExtractElement(builder
, base_data
,
633 LLVMConstInt(ctx
->i32
, start
+ 1, 0), ""),
636 data
= ac_build_gather_values(&ctx
->ac
, values
, 2);
639 data
= LLVMBuildExtractElement(
641 LLVMConstInt(ctx
->i32
, start
, 0), "");
648 LLVMConstInt(ctx
->i32
, start
* 4, 0), "");
651 ac_build_buffer_store_dword(&ctx
->ac
, resource
, data
, count
,
652 voff
, ctx
->i32_0
, 0, cache_policy
,
657 static void store_emit_memory(
658 struct si_shader_context
*ctx
,
659 struct lp_build_emit_data
*emit_data
)
661 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
662 LLVMBuilderRef builder
= ctx
->ac
.builder
;
663 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
664 LLVMValueRef ptr
, derived_ptr
, data
, index
;
667 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 0);
669 for (chan
= 0; chan
< 4; ++chan
) {
670 if (!(writemask
& (1 << chan
))) {
673 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
674 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
675 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
676 LLVMBuildStore(builder
, data
, derived_ptr
);
680 static void store_emit(
681 const struct lp_build_tgsi_action
*action
,
682 struct lp_build_tgsi_context
*bld_base
,
683 struct lp_build_emit_data
*emit_data
)
685 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
686 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
687 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
688 struct tgsi_full_src_register resource_reg
=
689 tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
690 unsigned target
= inst
->Memory
.Texture
;
692 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
693 store_emit_memory(ctx
, emit_data
);
697 bool writeonly_memory
= is_oneway_access_only(inst
, info
,
698 info
->shader_buffers_load
|
699 info
->shader_buffers_atomic
,
702 info
->uses_bindless_buffer_load
|
703 info
->uses_bindless_buffer_atomic
,
704 info
->uses_bindless_image_load
|
705 info
->uses_bindless_image_atomic
);
706 LLVMValueRef chans
[4];
707 LLVMValueRef vindex
= ctx
->i32_0
;
708 LLVMValueRef voffset
= ctx
->i32_0
;
709 struct ac_image_args args
= {};
711 for (unsigned chan
= 0; chan
< 4; ++chan
)
712 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
714 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
715 args
.resource
= shader_buffer_fetch_rsrc(ctx
, &resource_reg
, false);
716 voffset
= ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 0, 0));
718 image_fetch_rsrc(bld_base
, &resource_reg
, false, true, target
, &args
.resource
);
719 image_fetch_coords(bld_base
, inst
, 0, args
.resource
, args
.coords
);
720 vindex
= args
.coords
[0]; /* for buffers only */
723 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
724 ac_build_waitcnt(&ctx
->ac
, AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
726 bool is_image
= inst
->Dst
[0].Register
.File
!= TGSI_FILE_BUFFER
;
727 args
.cache_policy
= get_cache_policy(ctx
, inst
,
729 is_image
, /* may_store_unaligned */
732 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
733 store_emit_buffer(ctx
, args
.resource
, inst
->Dst
[0].Register
.WriteMask
,
734 ac_build_gather_values(&ctx
->ac
, chans
, 4),
735 voffset
, args
.cache_policy
, writeonly_memory
);
739 if (target
== TGSI_TEXTURE_BUFFER
) {
740 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
742 ac_build_buffer_store_format(&ctx
->ac
, args
.resource
,
743 ac_build_gather_values(&ctx
->ac
, chans
, num_channels
),
744 vindex
, ctx
->i32_0
/* voffset */,
748 args
.opcode
= ac_image_store
;
749 args
.data
[0] = ac_build_gather_values(&ctx
->ac
, chans
, 4);
750 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
751 args
.attributes
= AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
;
754 emit_data
->output
[emit_data
->chan
] =
755 ac_build_image_opcode(&ctx
->ac
, &args
);
759 static void atomic_emit_memory(struct si_shader_context
*ctx
,
760 struct lp_build_emit_data
*emit_data
) {
761 LLVMBuilderRef builder
= ctx
->ac
.builder
;
762 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
763 LLVMValueRef ptr
, result
, arg
;
764 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
766 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
768 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
769 arg
= ac_to_integer(&ctx
->ac
, arg
);
771 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
772 LLVMValueRef new_data
;
773 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
776 new_data
= ac_to_integer(&ctx
->ac
, new_data
);
778 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, arg
, new_data
,
780 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
782 LLVMAtomicRMWBinOp op
;
784 switch(inst
->Instruction
.Opcode
) {
785 case TGSI_OPCODE_ATOMUADD
:
786 op
= LLVMAtomicRMWBinOpAdd
;
788 case TGSI_OPCODE_ATOMXCHG
:
789 op
= LLVMAtomicRMWBinOpXchg
;
791 case TGSI_OPCODE_ATOMAND
:
792 op
= LLVMAtomicRMWBinOpAnd
;
794 case TGSI_OPCODE_ATOMOR
:
795 op
= LLVMAtomicRMWBinOpOr
;
797 case TGSI_OPCODE_ATOMXOR
:
798 op
= LLVMAtomicRMWBinOpXor
;
800 case TGSI_OPCODE_ATOMUMIN
:
801 op
= LLVMAtomicRMWBinOpUMin
;
803 case TGSI_OPCODE_ATOMUMAX
:
804 op
= LLVMAtomicRMWBinOpUMax
;
806 case TGSI_OPCODE_ATOMIMIN
:
807 op
= LLVMAtomicRMWBinOpMin
;
809 case TGSI_OPCODE_ATOMIMAX
:
810 op
= LLVMAtomicRMWBinOpMax
;
813 unreachable("unknown atomic opcode");
816 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, arg
, sync_scope
);
818 emit_data
->output
[emit_data
->chan
] =
819 LLVMBuildBitCast(builder
, result
, ctx
->f32
, "");
822 static void atomic_emit(
823 const struct lp_build_tgsi_action
*action
,
824 struct lp_build_tgsi_context
*bld_base
,
825 struct lp_build_emit_data
*emit_data
)
827 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
828 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
829 struct ac_image_args args
= {};
830 unsigned num_data
= 0;
831 LLVMValueRef vindex
= ctx
->i32_0
;
832 LLVMValueRef voffset
= ctx
->i32_0
;
834 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
835 atomic_emit_memory(ctx
, emit_data
);
839 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
840 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
841 * of arguments, which is reversed relative to TGSI (and GLSL)
843 args
.data
[num_data
++] =
844 ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 3, 0));
847 args
.data
[num_data
++] =
848 ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 2, 0));
850 args
.cache_policy
= get_cache_policy(ctx
, inst
, true, false, false);
852 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
853 args
.resource
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], false);
854 voffset
= ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 1, 0));
856 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, true,
857 inst
->Memory
.Texture
, &args
.resource
);
858 image_fetch_coords(bld_base
, inst
, 1, args
.resource
, args
.coords
);
859 vindex
= args
.coords
[0]; /* for buffers only */
862 if (inst
->Src
[0].Register
.File
!= TGSI_FILE_BUFFER
&&
863 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
864 LLVMValueRef buf_args
[7];
865 unsigned num_args
= 0;
867 buf_args
[num_args
++] = args
.data
[0];
868 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
869 buf_args
[num_args
++] = args
.data
[1];
871 buf_args
[num_args
++] = args
.resource
;
872 buf_args
[num_args
++] = vindex
;
873 buf_args
[num_args
++] = voffset
;
874 buf_args
[num_args
++] = ctx
->i32_0
; /* soffset */
875 buf_args
[num_args
++] = LLVMConstInt(ctx
->i32
, args
.cache_policy
& ac_slc
, 0);
877 char intrinsic_name
[64];
878 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
879 "llvm.amdgcn.struct.buffer.atomic.%s", action
->intr_name
);
880 emit_data
->output
[emit_data
->chan
] =
881 ac_to_float(&ctx
->ac
,
882 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
,
883 ctx
->i32
, buf_args
, num_args
, 0));
887 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
888 LLVMValueRef buf_args
[7];
889 unsigned num_args
= 0;
891 buf_args
[num_args
++] = args
.data
[0];
892 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
893 buf_args
[num_args
++] = args
.data
[1];
895 buf_args
[num_args
++] = args
.resource
;
896 buf_args
[num_args
++] = vindex
;
897 buf_args
[num_args
++] = voffset
;
898 buf_args
[num_args
++] = args
.cache_policy
& ac_slc
? ctx
->i1true
: ctx
->i1false
;
900 char intrinsic_name
[40];
901 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
902 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
903 emit_data
->output
[emit_data
->chan
] =
904 ac_to_float(&ctx
->ac
,
905 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
,
906 ctx
->i32
, buf_args
, num_args
, 0));
908 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
909 args
.opcode
= ac_image_atomic_cmpswap
;
911 args
.opcode
= ac_image_atomic
;
912 switch (inst
->Instruction
.Opcode
) {
913 case TGSI_OPCODE_ATOMXCHG
: args
.atomic
= ac_atomic_swap
; break;
914 case TGSI_OPCODE_ATOMUADD
: args
.atomic
= ac_atomic_add
; break;
915 case TGSI_OPCODE_ATOMAND
: args
.atomic
= ac_atomic_and
; break;
916 case TGSI_OPCODE_ATOMOR
: args
.atomic
= ac_atomic_or
; break;
917 case TGSI_OPCODE_ATOMXOR
: args
.atomic
= ac_atomic_xor
; break;
918 case TGSI_OPCODE_ATOMUMIN
: args
.atomic
= ac_atomic_umin
; break;
919 case TGSI_OPCODE_ATOMUMAX
: args
.atomic
= ac_atomic_umax
; break;
920 case TGSI_OPCODE_ATOMIMIN
: args
.atomic
= ac_atomic_smin
; break;
921 case TGSI_OPCODE_ATOMIMAX
: args
.atomic
= ac_atomic_smax
; break;
922 case TGSI_OPCODE_ATOMINC_WRAP
:
923 args
.atomic
= ac_atomic_inc_wrap
;
925 case TGSI_OPCODE_ATOMDEC_WRAP
:
926 args
.atomic
= ac_atomic_dec_wrap
;
928 default: unreachable("unhandled image atomic");
932 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
933 emit_data
->output
[emit_data
->chan
] =
934 ac_to_float(&ctx
->ac
, ac_build_image_opcode(&ctx
->ac
, &args
));
938 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
939 unsigned target
, LLVMValueRef out
)
941 LLVMBuilderRef builder
= ctx
->ac
.builder
;
943 /* 1D textures are allocated and used as 2D on GFX9. */
944 if (ctx
->screen
->info
.chip_class
== GFX9
&&
945 (target
== TGSI_TEXTURE_1D_ARRAY
||
946 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
947 LLVMValueRef layers
=
948 LLVMBuildExtractElement(builder
, out
,
949 LLVMConstInt(ctx
->i32
, 2, 0), "");
950 out
= LLVMBuildInsertElement(builder
, out
, layers
,
954 /* Divide the number of layers by 6 to get the number of cubes. */
955 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
956 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
957 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
959 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
960 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
962 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
967 static void resq_emit(
968 const struct lp_build_tgsi_action
*action
,
969 struct lp_build_tgsi_context
*bld_base
,
970 struct lp_build_emit_data
*emit_data
)
972 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
973 LLVMBuilderRef builder
= ctx
->ac
.builder
;
974 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
975 const struct tgsi_full_src_register
*reg
=
976 &inst
->Src
[inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
? 1 : 0];
978 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
979 LLVMValueRef rsrc
= shader_buffer_fetch_rsrc(ctx
, reg
, false);
981 emit_data
->output
[emit_data
->chan
] =
982 LLVMBuildExtractElement(builder
, rsrc
,
983 LLVMConstInt(ctx
->i32
, 2, 0), "");
987 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
&&
988 inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
991 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
992 /* Read the size from the buffer descriptor directly. */
993 emit_data
->output
[emit_data
->chan
] =
994 get_buffer_size(bld_base
, rsrc
);
998 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_RESQ
&&
999 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1002 image_fetch_rsrc(bld_base
, reg
, false, false, inst
->Memory
.Texture
, &rsrc
);
1003 emit_data
->output
[emit_data
->chan
] =
1004 get_buffer_size(bld_base
, rsrc
);
1010 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1011 target
= inst
->Texture
.Texture
;
1013 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
1014 target
= TGSI_TEXTURE_2D_ARRAY
;
1016 target
= inst
->Memory
.Texture
;
1019 struct ac_image_args args
= {};
1020 args
.opcode
= ac_image_get_resinfo
;
1021 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1023 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1025 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1026 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, NULL
, NULL
);
1027 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1029 image_fetch_rsrc(bld_base
, reg
, false, false, target
, &args
.resource
);
1030 args
.lod
= ctx
->i32_0
;
1033 emit_data
->output
[emit_data
->chan
] =
1034 fix_resinfo(ctx
, target
, ac_build_image_opcode(&ctx
->ac
, &args
));
1036 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_RESQ
&&
1037 (target
== TGSI_TEXTURE_2D_MSAA
||
1038 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
1039 LLVMValueRef samples
=
1040 ac_build_image_get_sample_count(&ctx
->ac
, args
.resource
);
1042 emit_data
->output
[emit_data
->chan
] =
1043 LLVMBuildInsertElement(ctx
->ac
.builder
,
1044 emit_data
->output
[emit_data
->chan
],
1046 LLVMConstInt(ctx
->i32
, 3, 0), "");
1051 * Load an image view, fmask view. or sampler state descriptor.
1053 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
1054 LLVMValueRef list
, LLVMValueRef index
,
1055 enum ac_descriptor_type type
)
1057 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1061 /* The image is at [0:7]. */
1062 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1064 case AC_DESC_BUFFER
:
1065 /* The buffer is in [4:7]. */
1066 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 4, 0),
1068 list
= LLVMBuildPointerCast(builder
, list
,
1069 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1072 /* The FMASK is at [8:15]. */
1073 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 2, 0),
1076 case AC_DESC_SAMPLER
:
1077 /* The sampler state is at [12:15]. */
1078 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 4, 0),
1079 LLVMConstInt(ctx
->i32
, 3, 0));
1080 list
= LLVMBuildPointerCast(builder
, list
,
1081 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1083 case AC_DESC_PLANE_0
:
1084 case AC_DESC_PLANE_1
:
1085 case AC_DESC_PLANE_2
:
1086 /* Only used for the multiplane image support for Vulkan. Should
1087 * never be reached in radeonsi.
1089 unreachable("Plane descriptor requested in radeonsi.");
1092 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
1095 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1098 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1099 * filtering manually. The driver sets img7 to a mask clearing
1100 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1101 * s_and_b32 samp0, samp0, img7
1104 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1106 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
1107 LLVMValueRef res
, LLVMValueRef samp
)
1109 LLVMValueRef img7
, samp0
;
1111 if (ctx
->screen
->info
.chip_class
>= GFX8
)
1114 img7
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1115 LLVMConstInt(ctx
->i32
, 7, 0), "");
1116 samp0
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp
,
1118 samp0
= LLVMBuildAnd(ctx
->ac
.builder
, samp0
, img7
, "");
1119 return LLVMBuildInsertElement(ctx
->ac
.builder
, samp
, samp0
,
1123 static void tex_fetch_ptrs(struct lp_build_tgsi_context
*bld_base
,
1124 struct lp_build_emit_data
*emit_data
,
1125 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
1126 LLVMValueRef
*fmask_ptr
)
1128 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1129 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
1130 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1131 const struct tgsi_full_src_register
*reg
;
1132 unsigned target
= inst
->Texture
.Texture
;
1133 unsigned sampler_src
;
1136 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1137 reg
= &emit_data
->inst
->Src
[sampler_src
];
1139 if (reg
->Register
.Indirect
) {
1140 index
= si_get_bounded_indirect_index(ctx
,
1142 reg
->Register
.Index
,
1144 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1145 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGE_SLOTS
/ 2, 0), "");
1147 index
= LLVMConstInt(ctx
->i32
,
1148 si_get_sampler_slot(reg
->Register
.Index
), 0);
1151 if (reg
->Register
.File
!= TGSI_FILE_SAMPLER
) {
1152 /* Bindless descriptors are accessible from a different pair of
1153 * user SGPR indices.
1155 list
= LLVMGetParam(ctx
->main_fn
,
1156 ctx
->param_bindless_samplers_and_images
);
1157 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1158 TGSI_TYPE_UNSIGNED
, 0);
1160 /* Since bindless handle arithmetic can contain an unsigned integer
1161 * wraparound and si_load_sampler_desc assumes there isn't any,
1162 * use GEP without "inbounds" (inside ac_build_pointer_add)
1163 * to prevent incorrect code generation and hangs.
1165 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1166 list
= ac_build_pointer_add(&ctx
->ac
, list
, index
);
1170 if (target
== TGSI_TEXTURE_BUFFER
)
1171 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_BUFFER
);
1173 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_IMAGE
);
1180 if (target
== TGSI_TEXTURE_2D_MSAA
||
1181 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1183 *fmask_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1185 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
1187 *samp_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1189 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
1194 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1195 * incorrectly forces nearest filtering if the texture format is integer.
1196 * The only effect it has on Gather4, which always returns 4 texels for
1197 * bilinear filtering, is that the final coordinates are off by 0.5 of
1200 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1201 * or (0.5 / size) from the normalized coordinates.
1203 * However, cube textures with 8_8_8_8 data formats require a different
1204 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1205 * precision in 32-bit data formats, so it needs to be applied dynamically at
1206 * runtime. In this case, return an i1 value that indicates whether the
1207 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1210 si_lower_gather4_integer(struct si_shader_context
*ctx
,
1211 struct ac_image_args
*args
,
1213 enum tgsi_return_type return_type
)
1215 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1216 LLVMValueRef wa_8888
= NULL
;
1217 LLVMValueRef half_texel
[2];
1219 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1220 return_type
== TGSI_RETURN_TYPE_UINT
);
1222 if (target
== TGSI_TEXTURE_CUBE
||
1223 target
== TGSI_TEXTURE_CUBE_ARRAY
) {
1224 LLVMValueRef formats
;
1225 LLVMValueRef data_format
;
1226 LLVMValueRef wa_formats
;
1228 formats
= LLVMBuildExtractElement(builder
, args
->resource
, ctx
->i32_1
, "");
1230 data_format
= LLVMBuildLShr(builder
, formats
,
1231 LLVMConstInt(ctx
->i32
, 20, false), "");
1232 data_format
= LLVMBuildAnd(builder
, data_format
,
1233 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1234 wa_8888
= LLVMBuildICmp(
1235 builder
, LLVMIntEQ
, data_format
,
1236 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1239 uint32_t wa_num_format
=
1240 return_type
== TGSI_RETURN_TYPE_UINT
?
1241 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1242 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1243 wa_formats
= LLVMBuildAnd(builder
, formats
,
1244 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1246 wa_formats
= LLVMBuildOr(builder
, wa_formats
,
1247 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1249 formats
= LLVMBuildSelect(builder
, wa_8888
, wa_formats
, formats
, "");
1250 args
->resource
= LLVMBuildInsertElement(
1251 builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1254 if (target
== TGSI_TEXTURE_RECT
||
1255 target
== TGSI_TEXTURE_SHADOWRECT
) {
1257 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1259 struct ac_image_args resinfo
= {};
1260 struct lp_build_if_state if_ctx
;
1263 /* Skip the texture size query entirely if we don't need it. */
1264 lp_build_if(&if_ctx
, &ctx
->gallivm
, LLVMBuildNot(builder
, wa_8888
, ""));
1267 /* Query the texture size. */
1268 resinfo
.opcode
= ac_image_get_resinfo
;
1269 resinfo
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1270 resinfo
.resource
= args
->resource
;
1271 resinfo
.sampler
= args
->sampler
;
1272 resinfo
.lod
= ctx
->ac
.i32_0
;
1273 resinfo
.dmask
= 0xf;
1274 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1276 LLVMValueRef texsize
=
1277 fix_resinfo(ctx
, target
,
1278 ac_build_image_opcode(&ctx
->ac
, &resinfo
));
1280 /* Compute -0.5 / size. */
1281 for (unsigned c
= 0; c
< 2; c
++) {
1283 LLVMBuildExtractElement(builder
, texsize
,
1284 LLVMConstInt(ctx
->i32
, c
, 0), "");
1285 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1286 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, half_texel
[c
]);
1287 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1288 LLVMConstReal(ctx
->f32
, -0.5), "");
1292 lp_build_endif(&if_ctx
);
1294 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1296 for (unsigned c
= 0; c
< 2; c
++) {
1297 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1298 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1304 for (unsigned c
= 0; c
< 2; c
++) {
1306 tmp
= ac_to_float(&ctx
->ac
, args
->coords
[c
]);
1307 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1308 args
->coords
[c
] = ac_to_integer(&ctx
->ac
, tmp
);
1314 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1315 * result after the gather operation.
1318 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1319 LLVMValueRef result
,
1320 enum tgsi_return_type return_type
,
1323 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1325 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1326 return_type
== TGSI_RETURN_TYPE_UINT
);
1328 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1329 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1331 LLVMValueRef wa_value
;
1333 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1335 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1336 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1338 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1339 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1340 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1342 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1348 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1349 struct lp_build_tgsi_context
*bld_base
,
1350 struct lp_build_emit_data
*emit_data
)
1352 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1353 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1354 unsigned opcode
= inst
->Instruction
.Opcode
;
1355 unsigned target
= inst
->Texture
.Texture
;
1356 struct ac_image_args args
= {};
1357 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1359 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1360 LLVMValueRef fmask_ptr
= NULL
;
1362 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
1364 if (target
== TGSI_TEXTURE_BUFFER
) {
1365 LLVMValueRef vindex
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1366 unsigned num_channels
=
1367 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1368 LLVMValueRef result
=
1369 ac_build_buffer_load_format(&ctx
->ac
,
1373 num_channels
, 0, true);
1374 emit_data
->output
[emit_data
->chan
] =
1375 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1379 /* Fetch and project texture coordinates */
1380 args
.coords
[3] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_W
);
1381 for (chan
= 0; chan
< 3; chan
++) {
1382 args
.coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
1383 if (opcode
== TGSI_OPCODE_TXP
)
1384 args
.coords
[chan
] = ac_build_fdiv(&ctx
->ac
,
1385 args
.coords
[chan
], args
.coords
[3]);
1388 if (opcode
== TGSI_OPCODE_TXP
)
1389 args
.coords
[3] = ctx
->ac
.f32_1
;
1393 opcode
!= TGSI_OPCODE_TXF
&&
1394 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1395 /* The offsets are six-bit signed integers packed like this:
1396 * X=[5:0], Y=[13:8], and Z=[21:16].
1398 LLVMValueRef offset
[3], pack
;
1400 assert(inst
->Texture
.NumOffsets
== 1);
1402 for (chan
= 0; chan
< 3; chan
++) {
1403 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
, inst
, 0, chan
);
1404 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1405 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1407 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1408 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1411 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1412 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1416 /* Pack LOD bias value */
1417 if (opcode
== TGSI_OPCODE_TXB
)
1418 args
.bias
= args
.coords
[3];
1419 if (opcode
== TGSI_OPCODE_TXB2
)
1420 args
.bias
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1422 /* Pack depth comparison value */
1423 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1426 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1427 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1429 assert(ref_pos
>= 0);
1430 z
= args
.coords
[ref_pos
];
1433 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1434 * OpenGL 4.5 spec says:
1436 * "If the texture’s internal format indicates a fixed-point
1437 * depth texture, then D_t and D_ref are clamped to the
1438 * range [0, 1]; otherwise no clamping is performed."
1440 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1441 * so the depth comparison value isn't clamped for Z16 and
1442 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
1443 * an explicitly clamped 32-bit float format.
1445 if (ctx
->screen
->info
.chip_class
>= GFX8
&&
1446 ctx
->screen
->info
.chip_class
<= GFX9
) {
1447 LLVMValueRef upgraded
;
1448 LLVMValueRef clamped
;
1449 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
1450 LLVMConstInt(ctx
->i32
, 3, false), "");
1451 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1452 LLVMConstInt(ctx
->i32
, 29, false), "");
1453 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1454 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1455 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1461 /* Pack user derivatives */
1462 if (opcode
== TGSI_OPCODE_TXD
) {
1463 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1466 case TGSI_TEXTURE_3D
:
1467 num_src_deriv_channels
= 3;
1468 num_dst_deriv_channels
= 3;
1470 case TGSI_TEXTURE_2D
:
1471 case TGSI_TEXTURE_SHADOW2D
:
1472 case TGSI_TEXTURE_RECT
:
1473 case TGSI_TEXTURE_SHADOWRECT
:
1474 case TGSI_TEXTURE_2D_ARRAY
:
1475 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1476 num_src_deriv_channels
= 2;
1477 num_dst_deriv_channels
= 2;
1479 case TGSI_TEXTURE_CUBE
:
1480 case TGSI_TEXTURE_SHADOWCUBE
:
1481 case TGSI_TEXTURE_CUBE_ARRAY
:
1482 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1483 /* Cube derivatives will be converted to 2D. */
1484 num_src_deriv_channels
= 3;
1485 num_dst_deriv_channels
= 3;
1487 case TGSI_TEXTURE_1D
:
1488 case TGSI_TEXTURE_SHADOW1D
:
1489 case TGSI_TEXTURE_1D_ARRAY
:
1490 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1491 num_src_deriv_channels
= 1;
1493 /* 1D textures are allocated and used as 2D on GFX9. */
1494 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1495 num_dst_deriv_channels
= 2;
1497 num_dst_deriv_channels
= 1;
1501 unreachable("invalid target");
1504 for (param
= 0; param
< 2; param
++) {
1505 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1506 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1507 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1509 /* Fill in the rest with zeros. */
1510 for (chan
= num_src_deriv_channels
;
1511 chan
< num_dst_deriv_channels
; chan
++)
1512 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1517 if (target
== TGSI_TEXTURE_CUBE
||
1518 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1519 target
== TGSI_TEXTURE_SHADOWCUBE
||
1520 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1521 ac_prepare_cube_coords(&ctx
->ac
,
1522 opcode
== TGSI_OPCODE_TXD
,
1523 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1524 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1525 opcode
== TGSI_OPCODE_LODQ
,
1526 args
.coords
, args
.derivs
);
1527 } else if (tgsi_is_array_sampler(target
) &&
1528 opcode
!= TGSI_OPCODE_TXF
&&
1529 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1530 ctx
->screen
->info
.chip_class
<= GFX8
) {
1531 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1532 args
.coords
[array_coord
] = ac_build_round(&ctx
->ac
, args
.coords
[array_coord
]);
1535 /* 1D textures are allocated and used as 2D on GFX9. */
1536 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1537 LLVMValueRef filler
;
1539 /* Use 0.5, so that we don't sample the border color. */
1540 if (opcode
== TGSI_OPCODE_TXF
||
1541 opcode
== TGSI_OPCODE_TXF_LZ
)
1542 filler
= ctx
->i32_0
;
1544 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1546 if (target
== TGSI_TEXTURE_1D
||
1547 target
== TGSI_TEXTURE_SHADOW1D
) {
1548 args
.coords
[1] = filler
;
1549 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1550 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1551 args
.coords
[2] = args
.coords
[1];
1552 args
.coords
[1] = filler
;
1556 /* Pack LOD or sample index */
1557 if (opcode
== TGSI_OPCODE_TXL
)
1558 args
.lod
= args
.coords
[3];
1559 else if (opcode
== TGSI_OPCODE_TXL2
)
1560 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1561 else if (opcode
== TGSI_OPCODE_TXF
) {
1562 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1563 /* No LOD, but move sample index into the right place. */
1564 args
.coords
[2] = args
.coords
[3];
1565 } else if (target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1566 args
.lod
= args
.coords
[3];
1570 if ((target
== TGSI_TEXTURE_2D_MSAA
||
1571 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) &&
1572 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1573 ac_apply_fmask_to_sample(&ctx
->ac
, fmask_ptr
, args
.coords
,
1574 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
1577 if (opcode
== TGSI_OPCODE_TXF
||
1578 opcode
== TGSI_OPCODE_TXF_LZ
) {
1579 /* add tex offsets */
1580 if (inst
->Texture
.NumOffsets
) {
1581 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1583 assert(inst
->Texture
.NumOffsets
== 1);
1586 case TGSI_TEXTURE_3D
:
1588 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[2],
1589 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
], "");
1591 case TGSI_TEXTURE_2D
:
1592 case TGSI_TEXTURE_SHADOW2D
:
1593 case TGSI_TEXTURE_RECT
:
1594 case TGSI_TEXTURE_SHADOWRECT
:
1595 case TGSI_TEXTURE_2D_ARRAY
:
1596 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1598 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[1],
1599 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
], "");
1601 case TGSI_TEXTURE_1D
:
1602 case TGSI_TEXTURE_SHADOW1D
:
1603 case TGSI_TEXTURE_1D_ARRAY
:
1604 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1606 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[0],
1607 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
], "");
1609 /* texture offsets do not apply to other texture targets */
1614 if (opcode
== TGSI_OPCODE_TG4
) {
1615 unsigned gather_comp
= 0;
1617 /* DMASK was repurposed for GATHER4. 4 components are always
1618 * returned and DMASK works like a swizzle - it selects
1619 * the component to fetch. The only valid DMASK values are
1620 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1621 * (red,red,red,red) etc.) The ISA document doesn't mention
1625 /* Get the component index from src1.x for Gather4. */
1626 if (!tgsi_is_shadow_target(target
)) {
1627 LLVMValueRef comp_imm
;
1628 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1630 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1632 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1633 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1634 gather_comp
= CLAMP(gather_comp
, 0, 3);
1637 args
.dmask
= 1 << gather_comp
;
1642 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1643 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
1644 target
== TGSI_TEXTURE_SHADOWRECT
;
1645 args
.opcode
= ac_image_sample
;
1648 case TGSI_OPCODE_TXF
:
1649 case TGSI_OPCODE_TXF_LZ
:
1650 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1651 target
== TGSI_TEXTURE_2D_MSAA
||
1652 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1653 ac_image_load
: ac_image_load_mip
;
1655 case TGSI_OPCODE_LODQ
:
1656 args
.opcode
= ac_image_get_lod
;
1658 case TGSI_OPCODE_TEX
:
1659 case TGSI_OPCODE_TEX2
:
1660 case TGSI_OPCODE_TXP
:
1661 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1662 args
.level_zero
= true;
1664 case TGSI_OPCODE_TEX_LZ
:
1665 args
.level_zero
= true;
1667 case TGSI_OPCODE_TXB
:
1668 case TGSI_OPCODE_TXB2
:
1669 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1671 case TGSI_OPCODE_TXL
:
1672 case TGSI_OPCODE_TXL2
:
1674 case TGSI_OPCODE_TXD
:
1676 case TGSI_OPCODE_TG4
:
1677 args
.opcode
= ac_image_gather4
;
1678 args
.level_zero
= true;
1685 /* The hardware needs special lowering for Gather4 with integer formats. */
1686 LLVMValueRef gather4_int_result_workaround
= NULL
;
1688 if (ctx
->screen
->info
.chip_class
<= GFX8
&&
1689 opcode
== TGSI_OPCODE_TG4
) {
1690 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1692 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1693 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1694 gather4_int_result_workaround
=
1695 si_lower_gather4_integer(ctx
, &args
, target
,
1696 inst
->Texture
.ReturnType
);
1700 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1701 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1703 if (gather4_int_result_workaround
) {
1704 result
= si_fix_gather4_integer_result(ctx
, result
,
1705 inst
->Texture
.ReturnType
,
1706 gather4_int_result_workaround
);
1709 emit_data
->output
[emit_data
->chan
] = result
;
1712 static void si_llvm_emit_txqs(
1713 const struct lp_build_tgsi_action
*action
,
1714 struct lp_build_tgsi_context
*bld_base
,
1715 struct lp_build_emit_data
*emit_data
)
1717 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1720 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
1722 rsrc
= LLVMBuildBitCast(ctx
->ac
.builder
, rsrc
, ctx
->v8i32
, "");
1723 emit_data
->output
[emit_data
->chan
] =
1724 ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
1727 static LLVMValueRef
si_llvm_emit_fbfetch(struct si_shader_context
*ctx
)
1729 struct ac_image_args args
= {};
1730 LLVMValueRef ptr
, image
, fmask
;
1732 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1736 /* Load the image descriptor. */
1737 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
1738 ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1739 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
1740 ac_array_in_const32_addr_space(ctx
->v8i32
), "");
1741 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1742 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
1746 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 0, 16);
1748 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1749 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 16, 16);
1751 /* Get the current render target layer index. */
1752 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
1753 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 16, 11);
1755 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1756 args
.coords
[chan
++] = si_get_sample_id(ctx
);
1758 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
&&
1759 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1760 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1761 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
1763 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
1764 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
1767 args
.opcode
= ac_image_load
;
1768 args
.resource
= image
;
1770 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1772 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1773 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1774 ac_image_2darraymsaa
: ac_image_2dmsaa
;
1775 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1776 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1777 ac_image_1darray
: ac_image_1d
;
1779 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1780 ac_image_2darray
: ac_image_2d
;
1782 return ac_build_image_opcode(&ctx
->ac
, &args
);
1785 static void si_tgsi_emit_fbfetch(const struct lp_build_tgsi_action
*action
,
1786 struct lp_build_tgsi_context
*bld_base
,
1787 struct lp_build_emit_data
*emit_data
)
1789 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1791 emit_data
->output
[emit_data
->chan
] = si_llvm_emit_fbfetch(ctx
);
1794 LLVMValueRef
si_nir_emit_fbfetch(struct ac_shader_abi
*abi
)
1796 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1798 return si_llvm_emit_fbfetch(ctx
);
1802 * Setup actions for TGSI memory opcode, including texture opcodes.
1804 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1806 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1808 bld_base
->op_actions
[TGSI_OPCODE_TEX
].emit
= build_tex_intrinsic
;
1809 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
].emit
= build_tex_intrinsic
;
1810 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].emit
= build_tex_intrinsic
;
1811 bld_base
->op_actions
[TGSI_OPCODE_TXB
].emit
= build_tex_intrinsic
;
1812 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].emit
= build_tex_intrinsic
;
1813 bld_base
->op_actions
[TGSI_OPCODE_TXD
].emit
= build_tex_intrinsic
;
1814 bld_base
->op_actions
[TGSI_OPCODE_TXF
].emit
= build_tex_intrinsic
;
1815 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
].emit
= build_tex_intrinsic
;
1816 bld_base
->op_actions
[TGSI_OPCODE_TXL
].emit
= build_tex_intrinsic
;
1817 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].emit
= build_tex_intrinsic
;
1818 bld_base
->op_actions
[TGSI_OPCODE_TXP
].emit
= build_tex_intrinsic
;
1819 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= resq_emit
;
1820 bld_base
->op_actions
[TGSI_OPCODE_TG4
].emit
= build_tex_intrinsic
;
1821 bld_base
->op_actions
[TGSI_OPCODE_LODQ
].emit
= build_tex_intrinsic
;
1822 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1824 bld_base
->op_actions
[TGSI_OPCODE_FBFETCH
].emit
= si_tgsi_emit_fbfetch
;
1826 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1827 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1828 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1830 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].emit
= atomic_emit
;
1831 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1832 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].emit
= atomic_emit
;
1833 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1834 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].emit
= atomic_emit
;
1835 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1836 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].emit
= atomic_emit
;
1837 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1838 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].emit
= atomic_emit
;
1839 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1840 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].emit
= atomic_emit
;
1841 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1842 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].emit
= atomic_emit
;
1843 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
1844 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].emit
= atomic_emit
;
1845 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
1846 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].emit
= atomic_emit
;
1847 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
1848 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].emit
= atomic_emit
;
1849 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
1850 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].emit
= atomic_emit
;
1851 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].intr_name
= "inc";
1852 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].emit
= atomic_emit
;
1853 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].intr_name
= "dec";