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
= ac_get_arg(&ctx
->ac
,
228 ctx
->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
= ac_get_arg(&ctx
->ac
, ctx
->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
= ac_get_arg(&ctx
->ac
, ctx
->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
= ac_get_arg(&ctx
->ac
, ctx
->bindless_samplers_and_images
);
1156 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1157 TGSI_TYPE_UNSIGNED
, 0);
1159 /* Since bindless handle arithmetic can contain an unsigned integer
1160 * wraparound and si_load_sampler_desc assumes there isn't any,
1161 * use GEP without "inbounds" (inside ac_build_pointer_add)
1162 * to prevent incorrect code generation and hangs.
1164 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1165 list
= ac_build_pointer_add(&ctx
->ac
, list
, index
);
1169 if (target
== TGSI_TEXTURE_BUFFER
)
1170 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_BUFFER
);
1172 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_IMAGE
);
1179 if (target
== TGSI_TEXTURE_2D_MSAA
||
1180 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1182 *fmask_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1184 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
1186 *samp_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1188 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
1193 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1194 * incorrectly forces nearest filtering if the texture format is integer.
1195 * The only effect it has on Gather4, which always returns 4 texels for
1196 * bilinear filtering, is that the final coordinates are off by 0.5 of
1199 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1200 * or (0.5 / size) from the normalized coordinates.
1202 * However, cube textures with 8_8_8_8 data formats require a different
1203 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1204 * precision in 32-bit data formats, so it needs to be applied dynamically at
1205 * runtime. In this case, return an i1 value that indicates whether the
1206 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1209 si_lower_gather4_integer(struct si_shader_context
*ctx
,
1210 struct ac_image_args
*args
,
1212 enum tgsi_return_type return_type
)
1214 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1215 LLVMValueRef wa_8888
= NULL
;
1216 LLVMValueRef half_texel
[2];
1218 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1219 return_type
== TGSI_RETURN_TYPE_UINT
);
1221 if (target
== TGSI_TEXTURE_CUBE
||
1222 target
== TGSI_TEXTURE_CUBE_ARRAY
) {
1223 LLVMValueRef formats
;
1224 LLVMValueRef data_format
;
1225 LLVMValueRef wa_formats
;
1227 formats
= LLVMBuildExtractElement(builder
, args
->resource
, ctx
->i32_1
, "");
1229 data_format
= LLVMBuildLShr(builder
, formats
,
1230 LLVMConstInt(ctx
->i32
, 20, false), "");
1231 data_format
= LLVMBuildAnd(builder
, data_format
,
1232 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1233 wa_8888
= LLVMBuildICmp(
1234 builder
, LLVMIntEQ
, data_format
,
1235 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1238 uint32_t wa_num_format
=
1239 return_type
== TGSI_RETURN_TYPE_UINT
?
1240 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1241 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1242 wa_formats
= LLVMBuildAnd(builder
, formats
,
1243 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1245 wa_formats
= LLVMBuildOr(builder
, wa_formats
,
1246 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1248 formats
= LLVMBuildSelect(builder
, wa_8888
, wa_formats
, formats
, "");
1249 args
->resource
= LLVMBuildInsertElement(
1250 builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1253 if (target
== TGSI_TEXTURE_RECT
||
1254 target
== TGSI_TEXTURE_SHADOWRECT
) {
1256 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1258 struct ac_image_args resinfo
= {};
1259 struct lp_build_if_state if_ctx
;
1262 /* Skip the texture size query entirely if we don't need it. */
1263 lp_build_if(&if_ctx
, &ctx
->gallivm
, LLVMBuildNot(builder
, wa_8888
, ""));
1266 /* Query the texture size. */
1267 resinfo
.opcode
= ac_image_get_resinfo
;
1268 resinfo
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1269 resinfo
.resource
= args
->resource
;
1270 resinfo
.sampler
= args
->sampler
;
1271 resinfo
.lod
= ctx
->ac
.i32_0
;
1272 resinfo
.dmask
= 0xf;
1273 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1275 LLVMValueRef texsize
=
1276 fix_resinfo(ctx
, target
,
1277 ac_build_image_opcode(&ctx
->ac
, &resinfo
));
1279 /* Compute -0.5 / size. */
1280 for (unsigned c
= 0; c
< 2; c
++) {
1282 LLVMBuildExtractElement(builder
, texsize
,
1283 LLVMConstInt(ctx
->i32
, c
, 0), "");
1284 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1285 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, half_texel
[c
]);
1286 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1287 LLVMConstReal(ctx
->f32
, -0.5), "");
1291 lp_build_endif(&if_ctx
);
1293 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1295 for (unsigned c
= 0; c
< 2; c
++) {
1296 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1297 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1303 for (unsigned c
= 0; c
< 2; c
++) {
1305 tmp
= ac_to_float(&ctx
->ac
, args
->coords
[c
]);
1306 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1307 args
->coords
[c
] = ac_to_integer(&ctx
->ac
, tmp
);
1313 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1314 * result after the gather operation.
1317 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1318 LLVMValueRef result
,
1319 enum tgsi_return_type return_type
,
1322 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1324 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1325 return_type
== TGSI_RETURN_TYPE_UINT
);
1327 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1328 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1330 LLVMValueRef wa_value
;
1332 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1334 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1335 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1337 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1338 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1339 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1341 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1347 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1348 struct lp_build_tgsi_context
*bld_base
,
1349 struct lp_build_emit_data
*emit_data
)
1351 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1352 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1353 unsigned opcode
= inst
->Instruction
.Opcode
;
1354 unsigned target
= inst
->Texture
.Texture
;
1355 struct ac_image_args args
= {};
1356 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1358 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1359 LLVMValueRef fmask_ptr
= NULL
;
1361 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
1363 if (target
== TGSI_TEXTURE_BUFFER
) {
1364 LLVMValueRef vindex
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1365 unsigned num_channels
=
1366 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1367 LLVMValueRef result
=
1368 ac_build_buffer_load_format(&ctx
->ac
,
1372 num_channels
, 0, true);
1373 emit_data
->output
[emit_data
->chan
] =
1374 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1378 /* Fetch and project texture coordinates */
1379 args
.coords
[3] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_W
);
1380 for (chan
= 0; chan
< 3; chan
++) {
1381 args
.coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
1382 if (opcode
== TGSI_OPCODE_TXP
)
1383 args
.coords
[chan
] = ac_build_fdiv(&ctx
->ac
,
1384 args
.coords
[chan
], args
.coords
[3]);
1387 if (opcode
== TGSI_OPCODE_TXP
)
1388 args
.coords
[3] = ctx
->ac
.f32_1
;
1392 opcode
!= TGSI_OPCODE_TXF
&&
1393 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1394 /* The offsets are six-bit signed integers packed like this:
1395 * X=[5:0], Y=[13:8], and Z=[21:16].
1397 LLVMValueRef offset
[3], pack
;
1399 assert(inst
->Texture
.NumOffsets
== 1);
1401 for (chan
= 0; chan
< 3; chan
++) {
1402 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
, inst
, 0, chan
);
1403 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1404 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1406 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1407 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1410 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1411 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1415 /* Pack LOD bias value */
1416 if (opcode
== TGSI_OPCODE_TXB
)
1417 args
.bias
= args
.coords
[3];
1418 if (opcode
== TGSI_OPCODE_TXB2
)
1419 args
.bias
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1421 /* Pack depth comparison value */
1422 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1425 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1426 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1428 assert(ref_pos
>= 0);
1429 z
= args
.coords
[ref_pos
];
1432 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1433 * OpenGL 4.5 spec says:
1435 * "If the texture’s internal format indicates a fixed-point
1436 * depth texture, then D_t and D_ref are clamped to the
1437 * range [0, 1]; otherwise no clamping is performed."
1439 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1440 * so the depth comparison value isn't clamped for Z16 and
1441 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
1442 * an explicitly clamped 32-bit float format.
1444 if (ctx
->screen
->info
.chip_class
>= GFX8
&&
1445 ctx
->screen
->info
.chip_class
<= GFX9
) {
1446 LLVMValueRef upgraded
;
1447 LLVMValueRef clamped
;
1448 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
1449 LLVMConstInt(ctx
->i32
, 3, false), "");
1450 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1451 LLVMConstInt(ctx
->i32
, 29, false), "");
1452 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1453 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1454 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1460 /* Pack user derivatives */
1461 if (opcode
== TGSI_OPCODE_TXD
) {
1462 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1465 case TGSI_TEXTURE_3D
:
1466 num_src_deriv_channels
= 3;
1467 num_dst_deriv_channels
= 3;
1469 case TGSI_TEXTURE_2D
:
1470 case TGSI_TEXTURE_SHADOW2D
:
1471 case TGSI_TEXTURE_RECT
:
1472 case TGSI_TEXTURE_SHADOWRECT
:
1473 case TGSI_TEXTURE_2D_ARRAY
:
1474 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1475 num_src_deriv_channels
= 2;
1476 num_dst_deriv_channels
= 2;
1478 case TGSI_TEXTURE_CUBE
:
1479 case TGSI_TEXTURE_SHADOWCUBE
:
1480 case TGSI_TEXTURE_CUBE_ARRAY
:
1481 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1482 /* Cube derivatives will be converted to 2D. */
1483 num_src_deriv_channels
= 3;
1484 num_dst_deriv_channels
= 3;
1486 case TGSI_TEXTURE_1D
:
1487 case TGSI_TEXTURE_SHADOW1D
:
1488 case TGSI_TEXTURE_1D_ARRAY
:
1489 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1490 num_src_deriv_channels
= 1;
1492 /* 1D textures are allocated and used as 2D on GFX9. */
1493 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1494 num_dst_deriv_channels
= 2;
1496 num_dst_deriv_channels
= 1;
1500 unreachable("invalid target");
1503 for (param
= 0; param
< 2; param
++) {
1504 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1505 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1506 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1508 /* Fill in the rest with zeros. */
1509 for (chan
= num_src_deriv_channels
;
1510 chan
< num_dst_deriv_channels
; chan
++)
1511 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1516 if (target
== TGSI_TEXTURE_CUBE
||
1517 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1518 target
== TGSI_TEXTURE_SHADOWCUBE
||
1519 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1520 ac_prepare_cube_coords(&ctx
->ac
,
1521 opcode
== TGSI_OPCODE_TXD
,
1522 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1523 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1524 opcode
== TGSI_OPCODE_LODQ
,
1525 args
.coords
, args
.derivs
);
1526 } else if (tgsi_is_array_sampler(target
) &&
1527 opcode
!= TGSI_OPCODE_TXF
&&
1528 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1529 ctx
->screen
->info
.chip_class
<= GFX8
) {
1530 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1531 args
.coords
[array_coord
] = ac_build_round(&ctx
->ac
, args
.coords
[array_coord
]);
1534 /* 1D textures are allocated and used as 2D on GFX9. */
1535 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1536 LLVMValueRef filler
;
1538 /* Use 0.5, so that we don't sample the border color. */
1539 if (opcode
== TGSI_OPCODE_TXF
||
1540 opcode
== TGSI_OPCODE_TXF_LZ
)
1541 filler
= ctx
->i32_0
;
1543 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1545 if (target
== TGSI_TEXTURE_1D
||
1546 target
== TGSI_TEXTURE_SHADOW1D
) {
1547 args
.coords
[1] = filler
;
1548 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1549 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1550 args
.coords
[2] = args
.coords
[1];
1551 args
.coords
[1] = filler
;
1555 /* Pack LOD or sample index */
1556 if (opcode
== TGSI_OPCODE_TXL
)
1557 args
.lod
= args
.coords
[3];
1558 else if (opcode
== TGSI_OPCODE_TXL2
)
1559 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1560 else if (opcode
== TGSI_OPCODE_TXF
) {
1561 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1562 /* No LOD, but move sample index into the right place. */
1563 args
.coords
[2] = args
.coords
[3];
1564 } else if (target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1565 args
.lod
= args
.coords
[3];
1569 if ((target
== TGSI_TEXTURE_2D_MSAA
||
1570 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) &&
1571 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1572 ac_apply_fmask_to_sample(&ctx
->ac
, fmask_ptr
, args
.coords
,
1573 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
1576 if (opcode
== TGSI_OPCODE_TXF
||
1577 opcode
== TGSI_OPCODE_TXF_LZ
) {
1578 /* add tex offsets */
1579 if (inst
->Texture
.NumOffsets
) {
1580 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1582 assert(inst
->Texture
.NumOffsets
== 1);
1585 case TGSI_TEXTURE_3D
:
1587 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[2],
1588 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
], "");
1590 case TGSI_TEXTURE_2D
:
1591 case TGSI_TEXTURE_SHADOW2D
:
1592 case TGSI_TEXTURE_RECT
:
1593 case TGSI_TEXTURE_SHADOWRECT
:
1594 case TGSI_TEXTURE_2D_ARRAY
:
1595 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1597 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[1],
1598 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
], "");
1600 case TGSI_TEXTURE_1D
:
1601 case TGSI_TEXTURE_SHADOW1D
:
1602 case TGSI_TEXTURE_1D_ARRAY
:
1603 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1605 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[0],
1606 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
], "");
1608 /* texture offsets do not apply to other texture targets */
1613 if (opcode
== TGSI_OPCODE_TG4
) {
1614 unsigned gather_comp
= 0;
1616 /* DMASK was repurposed for GATHER4. 4 components are always
1617 * returned and DMASK works like a swizzle - it selects
1618 * the component to fetch. The only valid DMASK values are
1619 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1620 * (red,red,red,red) etc.) The ISA document doesn't mention
1624 /* Get the component index from src1.x for Gather4. */
1625 if (!tgsi_is_shadow_target(target
)) {
1626 LLVMValueRef comp_imm
;
1627 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1629 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1631 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1632 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1633 gather_comp
= CLAMP(gather_comp
, 0, 3);
1636 args
.dmask
= 1 << gather_comp
;
1641 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1642 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
1643 target
== TGSI_TEXTURE_SHADOWRECT
;
1644 args
.opcode
= ac_image_sample
;
1647 case TGSI_OPCODE_TXF
:
1648 case TGSI_OPCODE_TXF_LZ
:
1649 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1650 target
== TGSI_TEXTURE_2D_MSAA
||
1651 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1652 ac_image_load
: ac_image_load_mip
;
1654 case TGSI_OPCODE_LODQ
:
1655 args
.opcode
= ac_image_get_lod
;
1657 case TGSI_OPCODE_TEX
:
1658 case TGSI_OPCODE_TEX2
:
1659 case TGSI_OPCODE_TXP
:
1660 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1661 args
.level_zero
= true;
1663 case TGSI_OPCODE_TEX_LZ
:
1664 args
.level_zero
= true;
1666 case TGSI_OPCODE_TXB
:
1667 case TGSI_OPCODE_TXB2
:
1668 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1670 case TGSI_OPCODE_TXL
:
1671 case TGSI_OPCODE_TXL2
:
1673 case TGSI_OPCODE_TXD
:
1675 case TGSI_OPCODE_TG4
:
1676 args
.opcode
= ac_image_gather4
;
1677 args
.level_zero
= true;
1684 /* The hardware needs special lowering for Gather4 with integer formats. */
1685 LLVMValueRef gather4_int_result_workaround
= NULL
;
1687 if (ctx
->screen
->info
.chip_class
<= GFX8
&&
1688 opcode
== TGSI_OPCODE_TG4
) {
1689 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1691 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1692 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1693 gather4_int_result_workaround
=
1694 si_lower_gather4_integer(ctx
, &args
, target
,
1695 inst
->Texture
.ReturnType
);
1699 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1700 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1702 if (gather4_int_result_workaround
) {
1703 result
= si_fix_gather4_integer_result(ctx
, result
,
1704 inst
->Texture
.ReturnType
,
1705 gather4_int_result_workaround
);
1708 emit_data
->output
[emit_data
->chan
] = result
;
1711 static void si_llvm_emit_txqs(
1712 const struct lp_build_tgsi_action
*action
,
1713 struct lp_build_tgsi_context
*bld_base
,
1714 struct lp_build_emit_data
*emit_data
)
1716 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1719 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
1721 rsrc
= LLVMBuildBitCast(ctx
->ac
.builder
, rsrc
, ctx
->v8i32
, "");
1722 emit_data
->output
[emit_data
->chan
] =
1723 ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
1726 static LLVMValueRef
si_llvm_emit_fbfetch(struct si_shader_context
*ctx
)
1728 struct ac_image_args args
= {};
1729 LLVMValueRef ptr
, image
, fmask
;
1731 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1735 /* Load the image descriptor. */
1736 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
1737 ptr
= ac_get_arg(&ctx
->ac
, ctx
->rw_buffers
);
1738 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
1739 ac_array_in_const32_addr_space(ctx
->v8i32
), "");
1740 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1741 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
1745 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 0, 16);
1747 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1748 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 16, 16);
1750 /* Get the current render target layer index. */
1751 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
1752 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->args
.ancillary
, 16, 11);
1754 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1755 args
.coords
[chan
++] = si_get_sample_id(ctx
);
1757 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
&&
1758 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1759 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1760 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
1762 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
1763 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
1766 args
.opcode
= ac_image_load
;
1767 args
.resource
= image
;
1769 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1771 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1772 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1773 ac_image_2darraymsaa
: ac_image_2dmsaa
;
1774 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1775 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1776 ac_image_1darray
: ac_image_1d
;
1778 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1779 ac_image_2darray
: ac_image_2d
;
1781 return ac_build_image_opcode(&ctx
->ac
, &args
);
1784 static void si_tgsi_emit_fbfetch(const struct lp_build_tgsi_action
*action
,
1785 struct lp_build_tgsi_context
*bld_base
,
1786 struct lp_build_emit_data
*emit_data
)
1788 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1790 emit_data
->output
[emit_data
->chan
] = si_llvm_emit_fbfetch(ctx
);
1793 LLVMValueRef
si_nir_emit_fbfetch(struct ac_shader_abi
*abi
)
1795 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1797 return si_llvm_emit_fbfetch(ctx
);
1801 * Setup actions for TGSI memory opcode, including texture opcodes.
1803 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1805 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1807 bld_base
->op_actions
[TGSI_OPCODE_TEX
].emit
= build_tex_intrinsic
;
1808 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
].emit
= build_tex_intrinsic
;
1809 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].emit
= build_tex_intrinsic
;
1810 bld_base
->op_actions
[TGSI_OPCODE_TXB
].emit
= build_tex_intrinsic
;
1811 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].emit
= build_tex_intrinsic
;
1812 bld_base
->op_actions
[TGSI_OPCODE_TXD
].emit
= build_tex_intrinsic
;
1813 bld_base
->op_actions
[TGSI_OPCODE_TXF
].emit
= build_tex_intrinsic
;
1814 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
].emit
= build_tex_intrinsic
;
1815 bld_base
->op_actions
[TGSI_OPCODE_TXL
].emit
= build_tex_intrinsic
;
1816 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].emit
= build_tex_intrinsic
;
1817 bld_base
->op_actions
[TGSI_OPCODE_TXP
].emit
= build_tex_intrinsic
;
1818 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= resq_emit
;
1819 bld_base
->op_actions
[TGSI_OPCODE_TG4
].emit
= build_tex_intrinsic
;
1820 bld_base
->op_actions
[TGSI_OPCODE_LODQ
].emit
= build_tex_intrinsic
;
1821 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1823 bld_base
->op_actions
[TGSI_OPCODE_FBFETCH
].emit
= si_tgsi_emit_fbfetch
;
1825 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1826 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1827 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1829 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].emit
= atomic_emit
;
1830 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1831 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].emit
= atomic_emit
;
1832 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1833 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].emit
= atomic_emit
;
1834 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1835 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].emit
= atomic_emit
;
1836 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1837 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].emit
= atomic_emit
;
1838 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1839 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].emit
= atomic_emit
;
1840 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1841 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].emit
= atomic_emit
;
1842 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
1843 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].emit
= atomic_emit
;
1844 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
1845 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].emit
= atomic_emit
;
1846 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
1847 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].emit
= atomic_emit
;
1848 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
1849 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].emit
= atomic_emit
;
1850 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].intr_name
= "inc";
1851 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].emit
= atomic_emit
;
1852 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].intr_name
= "dec";