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
);
656 static void store_emit_memory(
657 struct si_shader_context
*ctx
,
658 struct lp_build_emit_data
*emit_data
)
660 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
661 LLVMBuilderRef builder
= ctx
->ac
.builder
;
662 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
663 LLVMValueRef ptr
, derived_ptr
, data
, index
;
666 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 0);
668 for (chan
= 0; chan
< 4; ++chan
) {
669 if (!(writemask
& (1 << chan
))) {
672 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
673 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
674 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
675 LLVMBuildStore(builder
, data
, derived_ptr
);
679 static void store_emit(
680 const struct lp_build_tgsi_action
*action
,
681 struct lp_build_tgsi_context
*bld_base
,
682 struct lp_build_emit_data
*emit_data
)
684 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
685 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
686 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
687 struct tgsi_full_src_register resource_reg
=
688 tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
689 unsigned target
= inst
->Memory
.Texture
;
691 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
692 store_emit_memory(ctx
, emit_data
);
696 bool writeonly_memory
= is_oneway_access_only(inst
, info
,
697 info
->shader_buffers_load
|
698 info
->shader_buffers_atomic
,
701 info
->uses_bindless_buffer_load
|
702 info
->uses_bindless_buffer_atomic
,
703 info
->uses_bindless_image_load
|
704 info
->uses_bindless_image_atomic
);
705 LLVMValueRef chans
[4];
706 LLVMValueRef vindex
= ctx
->i32_0
;
707 LLVMValueRef voffset
= ctx
->i32_0
;
708 struct ac_image_args args
= {};
710 for (unsigned chan
= 0; chan
< 4; ++chan
)
711 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
713 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
714 args
.resource
= shader_buffer_fetch_rsrc(ctx
, &resource_reg
, false);
715 voffset
= ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 0, 0));
717 image_fetch_rsrc(bld_base
, &resource_reg
, false, true, target
, &args
.resource
);
718 image_fetch_coords(bld_base
, inst
, 0, args
.resource
, args
.coords
);
719 vindex
= args
.coords
[0]; /* for buffers only */
722 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
723 ac_build_waitcnt(&ctx
->ac
, AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
725 bool is_image
= inst
->Dst
[0].Register
.File
!= TGSI_FILE_BUFFER
;
726 args
.cache_policy
= get_cache_policy(ctx
, inst
,
728 is_image
, /* may_store_unaligned */
731 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
732 store_emit_buffer(ctx
, args
.resource
, inst
->Dst
[0].Register
.WriteMask
,
733 ac_build_gather_values(&ctx
->ac
, chans
, 4),
734 voffset
, args
.cache_policy
, writeonly_memory
);
738 if (target
== TGSI_TEXTURE_BUFFER
) {
739 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
741 ac_build_buffer_store_format(&ctx
->ac
, args
.resource
,
742 ac_build_gather_values(&ctx
->ac
, chans
, num_channels
),
743 vindex
, ctx
->i32_0
/* voffset */,
747 args
.opcode
= ac_image_store
;
748 args
.data
[0] = ac_build_gather_values(&ctx
->ac
, chans
, 4);
749 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
750 args
.attributes
= AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
;
753 emit_data
->output
[emit_data
->chan
] =
754 ac_build_image_opcode(&ctx
->ac
, &args
);
758 static void atomic_emit_memory(struct si_shader_context
*ctx
,
759 struct lp_build_emit_data
*emit_data
) {
760 LLVMBuilderRef builder
= ctx
->ac
.builder
;
761 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
762 LLVMValueRef ptr
, result
, arg
;
763 const char *sync_scope
= LLVM_VERSION_MAJOR
>= 9 ? "workgroup-one-as" : "workgroup";
765 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
767 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
768 arg
= ac_to_integer(&ctx
->ac
, arg
);
770 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
771 LLVMValueRef new_data
;
772 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
775 new_data
= ac_to_integer(&ctx
->ac
, new_data
);
777 result
= ac_build_atomic_cmp_xchg(&ctx
->ac
, ptr
, arg
, new_data
,
779 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
781 LLVMAtomicRMWBinOp op
;
783 switch(inst
->Instruction
.Opcode
) {
784 case TGSI_OPCODE_ATOMUADD
:
785 op
= LLVMAtomicRMWBinOpAdd
;
787 case TGSI_OPCODE_ATOMXCHG
:
788 op
= LLVMAtomicRMWBinOpXchg
;
790 case TGSI_OPCODE_ATOMAND
:
791 op
= LLVMAtomicRMWBinOpAnd
;
793 case TGSI_OPCODE_ATOMOR
:
794 op
= LLVMAtomicRMWBinOpOr
;
796 case TGSI_OPCODE_ATOMXOR
:
797 op
= LLVMAtomicRMWBinOpXor
;
799 case TGSI_OPCODE_ATOMUMIN
:
800 op
= LLVMAtomicRMWBinOpUMin
;
802 case TGSI_OPCODE_ATOMUMAX
:
803 op
= LLVMAtomicRMWBinOpUMax
;
805 case TGSI_OPCODE_ATOMIMIN
:
806 op
= LLVMAtomicRMWBinOpMin
;
808 case TGSI_OPCODE_ATOMIMAX
:
809 op
= LLVMAtomicRMWBinOpMax
;
812 unreachable("unknown atomic opcode");
815 result
= ac_build_atomic_rmw(&ctx
->ac
, op
, ptr
, arg
, sync_scope
);
817 emit_data
->output
[emit_data
->chan
] =
818 LLVMBuildBitCast(builder
, result
, ctx
->f32
, "");
821 static void atomic_emit(
822 const struct lp_build_tgsi_action
*action
,
823 struct lp_build_tgsi_context
*bld_base
,
824 struct lp_build_emit_data
*emit_data
)
826 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
827 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
828 struct ac_image_args args
= {};
829 unsigned num_data
= 0;
830 LLVMValueRef vindex
= ctx
->i32_0
;
831 LLVMValueRef voffset
= ctx
->i32_0
;
833 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
834 atomic_emit_memory(ctx
, emit_data
);
838 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
839 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
840 * of arguments, which is reversed relative to TGSI (and GLSL)
842 args
.data
[num_data
++] =
843 ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 3, 0));
846 args
.data
[num_data
++] =
847 ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 2, 0));
849 args
.cache_policy
= get_cache_policy(ctx
, inst
, true, false, false);
851 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
852 args
.resource
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], false);
853 voffset
= ac_to_integer(&ctx
->ac
, lp_build_emit_fetch(bld_base
, inst
, 1, 0));
855 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, true,
856 inst
->Memory
.Texture
, &args
.resource
);
857 image_fetch_coords(bld_base
, inst
, 1, args
.resource
, args
.coords
);
858 vindex
= args
.coords
[0]; /* for buffers only */
861 if (inst
->Src
[0].Register
.File
!= TGSI_FILE_BUFFER
&&
862 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
863 LLVMValueRef buf_args
[7];
864 unsigned num_args
= 0;
866 buf_args
[num_args
++] = args
.data
[0];
867 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
868 buf_args
[num_args
++] = args
.data
[1];
870 buf_args
[num_args
++] = args
.resource
;
871 buf_args
[num_args
++] = vindex
;
872 buf_args
[num_args
++] = voffset
;
873 buf_args
[num_args
++] = ctx
->i32_0
; /* soffset */
874 buf_args
[num_args
++] = LLVMConstInt(ctx
->i32
, args
.cache_policy
& ac_slc
, 0);
876 char intrinsic_name
[64];
877 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
878 "llvm.amdgcn.struct.buffer.atomic.%s", action
->intr_name
);
879 emit_data
->output
[emit_data
->chan
] =
880 ac_to_float(&ctx
->ac
,
881 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
,
882 ctx
->i32
, buf_args
, num_args
, 0));
886 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
887 LLVMValueRef buf_args
[7];
888 unsigned num_args
= 0;
890 buf_args
[num_args
++] = args
.data
[0];
891 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
892 buf_args
[num_args
++] = args
.data
[1];
894 buf_args
[num_args
++] = args
.resource
;
895 buf_args
[num_args
++] = vindex
;
896 buf_args
[num_args
++] = voffset
;
897 buf_args
[num_args
++] = args
.cache_policy
& ac_slc
? ctx
->i1true
: ctx
->i1false
;
899 char intrinsic_name
[40];
900 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
901 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
902 emit_data
->output
[emit_data
->chan
] =
903 ac_to_float(&ctx
->ac
,
904 ac_build_intrinsic(&ctx
->ac
, intrinsic_name
,
905 ctx
->i32
, buf_args
, num_args
, 0));
907 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
908 args
.opcode
= ac_image_atomic_cmpswap
;
910 args
.opcode
= ac_image_atomic
;
911 switch (inst
->Instruction
.Opcode
) {
912 case TGSI_OPCODE_ATOMXCHG
: args
.atomic
= ac_atomic_swap
; break;
913 case TGSI_OPCODE_ATOMUADD
: args
.atomic
= ac_atomic_add
; break;
914 case TGSI_OPCODE_ATOMAND
: args
.atomic
= ac_atomic_and
; break;
915 case TGSI_OPCODE_ATOMOR
: args
.atomic
= ac_atomic_or
; break;
916 case TGSI_OPCODE_ATOMXOR
: args
.atomic
= ac_atomic_xor
; break;
917 case TGSI_OPCODE_ATOMUMIN
: args
.atomic
= ac_atomic_umin
; break;
918 case TGSI_OPCODE_ATOMUMAX
: args
.atomic
= ac_atomic_umax
; break;
919 case TGSI_OPCODE_ATOMIMIN
: args
.atomic
= ac_atomic_smin
; break;
920 case TGSI_OPCODE_ATOMIMAX
: args
.atomic
= ac_atomic_smax
; break;
921 case TGSI_OPCODE_ATOMINC_WRAP
:
922 args
.atomic
= ac_atomic_inc_wrap
;
924 case TGSI_OPCODE_ATOMDEC_WRAP
:
925 args
.atomic
= ac_atomic_dec_wrap
;
927 default: unreachable("unhandled image atomic");
931 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
932 emit_data
->output
[emit_data
->chan
] =
933 ac_to_float(&ctx
->ac
, ac_build_image_opcode(&ctx
->ac
, &args
));
937 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
938 unsigned target
, LLVMValueRef out
)
940 LLVMBuilderRef builder
= ctx
->ac
.builder
;
942 /* 1D textures are allocated and used as 2D on GFX9. */
943 if (ctx
->screen
->info
.chip_class
== GFX9
&&
944 (target
== TGSI_TEXTURE_1D_ARRAY
||
945 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
946 LLVMValueRef layers
=
947 LLVMBuildExtractElement(builder
, out
,
948 LLVMConstInt(ctx
->i32
, 2, 0), "");
949 out
= LLVMBuildInsertElement(builder
, out
, layers
,
953 /* Divide the number of layers by 6 to get the number of cubes. */
954 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
955 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
956 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
958 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
959 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
961 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
966 static void resq_emit(
967 const struct lp_build_tgsi_action
*action
,
968 struct lp_build_tgsi_context
*bld_base
,
969 struct lp_build_emit_data
*emit_data
)
971 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
972 LLVMBuilderRef builder
= ctx
->ac
.builder
;
973 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
974 const struct tgsi_full_src_register
*reg
=
975 &inst
->Src
[inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
? 1 : 0];
977 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
978 LLVMValueRef rsrc
= shader_buffer_fetch_rsrc(ctx
, reg
, false);
980 emit_data
->output
[emit_data
->chan
] =
981 LLVMBuildExtractElement(builder
, rsrc
,
982 LLVMConstInt(ctx
->i32
, 2, 0), "");
986 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
&&
987 inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
990 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
991 /* Read the size from the buffer descriptor directly. */
992 emit_data
->output
[emit_data
->chan
] =
993 get_buffer_size(bld_base
, rsrc
);
997 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_RESQ
&&
998 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1001 image_fetch_rsrc(bld_base
, reg
, false, false, inst
->Memory
.Texture
, &rsrc
);
1002 emit_data
->output
[emit_data
->chan
] =
1003 get_buffer_size(bld_base
, rsrc
);
1009 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1010 target
= inst
->Texture
.Texture
;
1012 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
1013 target
= TGSI_TEXTURE_2D_ARRAY
;
1015 target
= inst
->Memory
.Texture
;
1018 struct ac_image_args args
= {};
1019 args
.opcode
= ac_image_get_resinfo
;
1020 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1022 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1024 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1025 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, NULL
, NULL
);
1026 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1028 image_fetch_rsrc(bld_base
, reg
, false, false, target
, &args
.resource
);
1029 args
.lod
= ctx
->i32_0
;
1032 emit_data
->output
[emit_data
->chan
] =
1033 fix_resinfo(ctx
, target
, ac_build_image_opcode(&ctx
->ac
, &args
));
1035 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_RESQ
&&
1036 (target
== TGSI_TEXTURE_2D_MSAA
||
1037 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
1038 LLVMValueRef samples
=
1039 ac_build_image_get_sample_count(&ctx
->ac
, args
.resource
);
1041 emit_data
->output
[emit_data
->chan
] =
1042 LLVMBuildInsertElement(ctx
->ac
.builder
,
1043 emit_data
->output
[emit_data
->chan
],
1045 LLVMConstInt(ctx
->i32
, 3, 0), "");
1050 * Load an image view, fmask view. or sampler state descriptor.
1052 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
1053 LLVMValueRef list
, LLVMValueRef index
,
1054 enum ac_descriptor_type type
)
1056 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1060 /* The image is at [0:7]. */
1061 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1063 case AC_DESC_BUFFER
:
1064 /* The buffer is in [4:7]. */
1065 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 4, 0),
1067 list
= LLVMBuildPointerCast(builder
, list
,
1068 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1071 /* The FMASK is at [8:15]. */
1072 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 2, 0),
1075 case AC_DESC_SAMPLER
:
1076 /* The sampler state is at [12:15]. */
1077 index
= ac_build_imad(&ctx
->ac
, index
, LLVMConstInt(ctx
->i32
, 4, 0),
1078 LLVMConstInt(ctx
->i32
, 3, 0));
1079 list
= LLVMBuildPointerCast(builder
, list
,
1080 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1082 case AC_DESC_PLANE_0
:
1083 case AC_DESC_PLANE_1
:
1084 case AC_DESC_PLANE_2
:
1085 /* Only used for the multiplane image support for Vulkan. Should
1086 * never be reached in radeonsi.
1088 unreachable("Plane descriptor requested in radeonsi.");
1091 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
1094 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1097 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1098 * filtering manually. The driver sets img7 to a mask clearing
1099 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1100 * s_and_b32 samp0, samp0, img7
1103 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1105 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
1106 LLVMValueRef res
, LLVMValueRef samp
)
1108 LLVMValueRef img7
, samp0
;
1110 if (ctx
->screen
->info
.chip_class
>= GFX8
)
1113 img7
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1114 LLVMConstInt(ctx
->i32
, 7, 0), "");
1115 samp0
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp
,
1117 samp0
= LLVMBuildAnd(ctx
->ac
.builder
, samp0
, img7
, "");
1118 return LLVMBuildInsertElement(ctx
->ac
.builder
, samp
, samp0
,
1122 static void tex_fetch_ptrs(struct lp_build_tgsi_context
*bld_base
,
1123 struct lp_build_emit_data
*emit_data
,
1124 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
1125 LLVMValueRef
*fmask_ptr
)
1127 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1128 LLVMValueRef list
= ac_get_arg(&ctx
->ac
, ctx
->samplers_and_images
);
1129 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1130 const struct tgsi_full_src_register
*reg
;
1131 unsigned target
= inst
->Texture
.Texture
;
1132 unsigned sampler_src
;
1135 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1136 reg
= &emit_data
->inst
->Src
[sampler_src
];
1138 if (reg
->Register
.Indirect
) {
1139 index
= si_get_bounded_indirect_index(ctx
,
1141 reg
->Register
.Index
,
1143 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1144 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGE_SLOTS
/ 2, 0), "");
1146 index
= LLVMConstInt(ctx
->i32
,
1147 si_get_sampler_slot(reg
->Register
.Index
), 0);
1150 if (reg
->Register
.File
!= TGSI_FILE_SAMPLER
) {
1151 /* Bindless descriptors are accessible from a different pair of
1152 * user SGPR indices.
1154 list
= ac_get_arg(&ctx
->ac
, ctx
->bindless_samplers_and_images
);
1155 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1156 TGSI_TYPE_UNSIGNED
, 0);
1158 /* Since bindless handle arithmetic can contain an unsigned integer
1159 * wraparound and si_load_sampler_desc assumes there isn't any,
1160 * use GEP without "inbounds" (inside ac_build_pointer_add)
1161 * to prevent incorrect code generation and hangs.
1163 index
= LLVMBuildMul(ctx
->ac
.builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1164 list
= ac_build_pointer_add(&ctx
->ac
, list
, index
);
1168 if (target
== TGSI_TEXTURE_BUFFER
)
1169 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_BUFFER
);
1171 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_IMAGE
);
1178 if (target
== TGSI_TEXTURE_2D_MSAA
||
1179 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1181 *fmask_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1183 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
1185 *samp_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1187 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
1192 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1193 * incorrectly forces nearest filtering if the texture format is integer.
1194 * The only effect it has on Gather4, which always returns 4 texels for
1195 * bilinear filtering, is that the final coordinates are off by 0.5 of
1198 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1199 * or (0.5 / size) from the normalized coordinates.
1201 * However, cube textures with 8_8_8_8 data formats require a different
1202 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1203 * precision in 32-bit data formats, so it needs to be applied dynamically at
1204 * runtime. In this case, return an i1 value that indicates whether the
1205 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1208 si_lower_gather4_integer(struct si_shader_context
*ctx
,
1209 struct ac_image_args
*args
,
1211 enum tgsi_return_type return_type
)
1213 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1214 LLVMValueRef wa_8888
= NULL
;
1215 LLVMValueRef half_texel
[2];
1217 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1218 return_type
== TGSI_RETURN_TYPE_UINT
);
1220 if (target
== TGSI_TEXTURE_CUBE
||
1221 target
== TGSI_TEXTURE_CUBE_ARRAY
) {
1222 LLVMValueRef formats
;
1223 LLVMValueRef data_format
;
1224 LLVMValueRef wa_formats
;
1226 formats
= LLVMBuildExtractElement(builder
, args
->resource
, ctx
->i32_1
, "");
1228 data_format
= LLVMBuildLShr(builder
, formats
,
1229 LLVMConstInt(ctx
->i32
, 20, false), "");
1230 data_format
= LLVMBuildAnd(builder
, data_format
,
1231 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1232 wa_8888
= LLVMBuildICmp(
1233 builder
, LLVMIntEQ
, data_format
,
1234 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1237 uint32_t wa_num_format
=
1238 return_type
== TGSI_RETURN_TYPE_UINT
?
1239 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1240 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1241 wa_formats
= LLVMBuildAnd(builder
, formats
,
1242 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT
, false),
1244 wa_formats
= LLVMBuildOr(builder
, wa_formats
,
1245 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1247 formats
= LLVMBuildSelect(builder
, wa_8888
, wa_formats
, formats
, "");
1248 args
->resource
= LLVMBuildInsertElement(
1249 builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1252 if (target
== TGSI_TEXTURE_RECT
||
1253 target
== TGSI_TEXTURE_SHADOWRECT
) {
1255 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1257 struct ac_image_args resinfo
= {};
1258 struct lp_build_if_state if_ctx
;
1261 /* Skip the texture size query entirely if we don't need it. */
1262 lp_build_if(&if_ctx
, &ctx
->gallivm
, LLVMBuildNot(builder
, wa_8888
, ""));
1265 /* Query the texture size. */
1266 resinfo
.opcode
= ac_image_get_resinfo
;
1267 resinfo
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1268 resinfo
.resource
= args
->resource
;
1269 resinfo
.sampler
= args
->sampler
;
1270 resinfo
.lod
= ctx
->ac
.i32_0
;
1271 resinfo
.dmask
= 0xf;
1272 resinfo
.attributes
= AC_FUNC_ATTR_READNONE
;
1274 LLVMValueRef texsize
=
1275 fix_resinfo(ctx
, target
,
1276 ac_build_image_opcode(&ctx
->ac
, &resinfo
));
1278 /* Compute -0.5 / size. */
1279 for (unsigned c
= 0; c
< 2; c
++) {
1281 LLVMBuildExtractElement(builder
, texsize
,
1282 LLVMConstInt(ctx
->i32
, c
, 0), "");
1283 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1284 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, half_texel
[c
]);
1285 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1286 LLVMConstReal(ctx
->f32
, -0.5), "");
1290 lp_build_endif(&if_ctx
);
1292 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1294 for (unsigned c
= 0; c
< 2; c
++) {
1295 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1296 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1302 for (unsigned c
= 0; c
< 2; c
++) {
1304 tmp
= ac_to_float(&ctx
->ac
, args
->coords
[c
]);
1305 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1306 args
->coords
[c
] = ac_to_integer(&ctx
->ac
, tmp
);
1312 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1313 * result after the gather operation.
1316 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1317 LLVMValueRef result
,
1318 enum tgsi_return_type return_type
,
1321 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1323 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1324 return_type
== TGSI_RETURN_TYPE_UINT
);
1326 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1327 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1329 LLVMValueRef wa_value
;
1331 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1333 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1334 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1336 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1337 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1338 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1340 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1346 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1347 struct lp_build_tgsi_context
*bld_base
,
1348 struct lp_build_emit_data
*emit_data
)
1350 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1351 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1352 unsigned opcode
= inst
->Instruction
.Opcode
;
1353 unsigned target
= inst
->Texture
.Texture
;
1354 struct ac_image_args args
= {};
1355 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1357 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1358 LLVMValueRef fmask_ptr
= NULL
;
1360 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
1362 if (target
== TGSI_TEXTURE_BUFFER
) {
1363 LLVMValueRef vindex
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1364 unsigned num_channels
=
1365 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1366 LLVMValueRef result
=
1367 ac_build_buffer_load_format(&ctx
->ac
,
1371 num_channels
, 0, true);
1372 emit_data
->output
[emit_data
->chan
] =
1373 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1377 /* Fetch and project texture coordinates */
1378 args
.coords
[3] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_W
);
1379 for (chan
= 0; chan
< 3; chan
++) {
1380 args
.coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
1381 if (opcode
== TGSI_OPCODE_TXP
)
1382 args
.coords
[chan
] = ac_build_fdiv(&ctx
->ac
,
1383 args
.coords
[chan
], args
.coords
[3]);
1386 if (opcode
== TGSI_OPCODE_TXP
)
1387 args
.coords
[3] = ctx
->ac
.f32_1
;
1391 opcode
!= TGSI_OPCODE_TXF
&&
1392 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1393 /* The offsets are six-bit signed integers packed like this:
1394 * X=[5:0], Y=[13:8], and Z=[21:16].
1396 LLVMValueRef offset
[3], pack
;
1398 assert(inst
->Texture
.NumOffsets
== 1);
1400 for (chan
= 0; chan
< 3; chan
++) {
1401 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
, inst
, 0, chan
);
1402 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1403 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1405 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1406 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1409 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1410 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1414 /* Pack LOD bias value */
1415 if (opcode
== TGSI_OPCODE_TXB
)
1416 args
.bias
= args
.coords
[3];
1417 if (opcode
== TGSI_OPCODE_TXB2
)
1418 args
.bias
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1420 /* Pack depth comparison value */
1421 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1424 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1425 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1427 assert(ref_pos
>= 0);
1428 z
= args
.coords
[ref_pos
];
1431 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1432 * OpenGL 4.5 spec says:
1434 * "If the texture’s internal format indicates a fixed-point
1435 * depth texture, then D_t and D_ref are clamped to the
1436 * range [0, 1]; otherwise no clamping is performed."
1438 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1439 * so the depth comparison value isn't clamped for Z16 and
1440 * Z24 anymore. Do it manually here for GFX8-9; GFX10 has
1441 * an explicitly clamped 32-bit float format.
1443 if (ctx
->screen
->info
.chip_class
>= GFX8
&&
1444 ctx
->screen
->info
.chip_class
<= GFX9
) {
1445 LLVMValueRef upgraded
;
1446 LLVMValueRef clamped
;
1447 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
1448 LLVMConstInt(ctx
->i32
, 3, false), "");
1449 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1450 LLVMConstInt(ctx
->i32
, 29, false), "");
1451 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1452 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1453 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1459 /* Pack user derivatives */
1460 if (opcode
== TGSI_OPCODE_TXD
) {
1461 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1464 case TGSI_TEXTURE_3D
:
1465 num_src_deriv_channels
= 3;
1466 num_dst_deriv_channels
= 3;
1468 case TGSI_TEXTURE_2D
:
1469 case TGSI_TEXTURE_SHADOW2D
:
1470 case TGSI_TEXTURE_RECT
:
1471 case TGSI_TEXTURE_SHADOWRECT
:
1472 case TGSI_TEXTURE_2D_ARRAY
:
1473 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1474 num_src_deriv_channels
= 2;
1475 num_dst_deriv_channels
= 2;
1477 case TGSI_TEXTURE_CUBE
:
1478 case TGSI_TEXTURE_SHADOWCUBE
:
1479 case TGSI_TEXTURE_CUBE_ARRAY
:
1480 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1481 /* Cube derivatives will be converted to 2D. */
1482 num_src_deriv_channels
= 3;
1483 num_dst_deriv_channels
= 3;
1485 case TGSI_TEXTURE_1D
:
1486 case TGSI_TEXTURE_SHADOW1D
:
1487 case TGSI_TEXTURE_1D_ARRAY
:
1488 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1489 num_src_deriv_channels
= 1;
1491 /* 1D textures are allocated and used as 2D on GFX9. */
1492 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1493 num_dst_deriv_channels
= 2;
1495 num_dst_deriv_channels
= 1;
1499 unreachable("invalid target");
1502 for (param
= 0; param
< 2; param
++) {
1503 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1504 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1505 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1507 /* Fill in the rest with zeros. */
1508 for (chan
= num_src_deriv_channels
;
1509 chan
< num_dst_deriv_channels
; chan
++)
1510 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1515 if (target
== TGSI_TEXTURE_CUBE
||
1516 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1517 target
== TGSI_TEXTURE_SHADOWCUBE
||
1518 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1519 ac_prepare_cube_coords(&ctx
->ac
,
1520 opcode
== TGSI_OPCODE_TXD
,
1521 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1522 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1523 opcode
== TGSI_OPCODE_LODQ
,
1524 args
.coords
, args
.derivs
);
1525 } else if (tgsi_is_array_sampler(target
) &&
1526 opcode
!= TGSI_OPCODE_TXF
&&
1527 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1528 ctx
->screen
->info
.chip_class
<= GFX8
) {
1529 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1530 args
.coords
[array_coord
] = ac_build_round(&ctx
->ac
, args
.coords
[array_coord
]);
1533 /* 1D textures are allocated and used as 2D on GFX9. */
1534 if (ctx
->screen
->info
.chip_class
== GFX9
) {
1535 LLVMValueRef filler
;
1537 /* Use 0.5, so that we don't sample the border color. */
1538 if (opcode
== TGSI_OPCODE_TXF
||
1539 opcode
== TGSI_OPCODE_TXF_LZ
)
1540 filler
= ctx
->i32_0
;
1542 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1544 if (target
== TGSI_TEXTURE_1D
||
1545 target
== TGSI_TEXTURE_SHADOW1D
) {
1546 args
.coords
[1] = filler
;
1547 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1548 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1549 args
.coords
[2] = args
.coords
[1];
1550 args
.coords
[1] = filler
;
1554 /* Pack LOD or sample index */
1555 if (opcode
== TGSI_OPCODE_TXL
)
1556 args
.lod
= args
.coords
[3];
1557 else if (opcode
== TGSI_OPCODE_TXL2
)
1558 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1559 else if (opcode
== TGSI_OPCODE_TXF
) {
1560 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1561 /* No LOD, but move sample index into the right place. */
1562 args
.coords
[2] = args
.coords
[3];
1563 } else if (target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1564 args
.lod
= args
.coords
[3];
1568 if ((target
== TGSI_TEXTURE_2D_MSAA
||
1569 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) &&
1570 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1571 ac_apply_fmask_to_sample(&ctx
->ac
, fmask_ptr
, args
.coords
,
1572 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
1575 if (opcode
== TGSI_OPCODE_TXF
||
1576 opcode
== TGSI_OPCODE_TXF_LZ
) {
1577 /* add tex offsets */
1578 if (inst
->Texture
.NumOffsets
) {
1579 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1581 assert(inst
->Texture
.NumOffsets
== 1);
1584 case TGSI_TEXTURE_3D
:
1586 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[2],
1587 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
], "");
1589 case TGSI_TEXTURE_2D
:
1590 case TGSI_TEXTURE_SHADOW2D
:
1591 case TGSI_TEXTURE_RECT
:
1592 case TGSI_TEXTURE_SHADOWRECT
:
1593 case TGSI_TEXTURE_2D_ARRAY
:
1594 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1596 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[1],
1597 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
], "");
1599 case TGSI_TEXTURE_1D
:
1600 case TGSI_TEXTURE_SHADOW1D
:
1601 case TGSI_TEXTURE_1D_ARRAY
:
1602 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1604 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[0],
1605 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
], "");
1607 /* texture offsets do not apply to other texture targets */
1612 if (opcode
== TGSI_OPCODE_TG4
) {
1613 unsigned gather_comp
= 0;
1615 /* DMASK was repurposed for GATHER4. 4 components are always
1616 * returned and DMASK works like a swizzle - it selects
1617 * the component to fetch. The only valid DMASK values are
1618 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1619 * (red,red,red,red) etc.) The ISA document doesn't mention
1623 /* Get the component index from src1.x for Gather4. */
1624 if (!tgsi_is_shadow_target(target
)) {
1625 LLVMValueRef comp_imm
;
1626 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1628 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1630 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1631 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1632 gather_comp
= CLAMP(gather_comp
, 0, 3);
1635 args
.dmask
= 1 << gather_comp
;
1640 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1641 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
1642 target
== TGSI_TEXTURE_SHADOWRECT
;
1643 args
.opcode
= ac_image_sample
;
1646 case TGSI_OPCODE_TXF
:
1647 case TGSI_OPCODE_TXF_LZ
:
1648 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1649 target
== TGSI_TEXTURE_2D_MSAA
||
1650 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1651 ac_image_load
: ac_image_load_mip
;
1653 case TGSI_OPCODE_LODQ
:
1654 args
.opcode
= ac_image_get_lod
;
1656 case TGSI_OPCODE_TEX
:
1657 case TGSI_OPCODE_TEX2
:
1658 case TGSI_OPCODE_TXP
:
1659 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1660 args
.level_zero
= true;
1662 case TGSI_OPCODE_TEX_LZ
:
1663 args
.level_zero
= true;
1665 case TGSI_OPCODE_TXB
:
1666 case TGSI_OPCODE_TXB2
:
1667 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1669 case TGSI_OPCODE_TXL
:
1670 case TGSI_OPCODE_TXL2
:
1672 case TGSI_OPCODE_TXD
:
1674 case TGSI_OPCODE_TG4
:
1675 args
.opcode
= ac_image_gather4
;
1676 args
.level_zero
= true;
1683 /* The hardware needs special lowering for Gather4 with integer formats. */
1684 LLVMValueRef gather4_int_result_workaround
= NULL
;
1686 if (ctx
->screen
->info
.chip_class
<= GFX8
&&
1687 opcode
== TGSI_OPCODE_TG4
) {
1688 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1690 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1691 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1692 gather4_int_result_workaround
=
1693 si_lower_gather4_integer(ctx
, &args
, target
,
1694 inst
->Texture
.ReturnType
);
1698 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1699 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1701 if (gather4_int_result_workaround
) {
1702 result
= si_fix_gather4_integer_result(ctx
, result
,
1703 inst
->Texture
.ReturnType
,
1704 gather4_int_result_workaround
);
1707 emit_data
->output
[emit_data
->chan
] = result
;
1710 static void si_llvm_emit_txqs(
1711 const struct lp_build_tgsi_action
*action
,
1712 struct lp_build_tgsi_context
*bld_base
,
1713 struct lp_build_emit_data
*emit_data
)
1715 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1718 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
1720 rsrc
= LLVMBuildBitCast(ctx
->ac
.builder
, rsrc
, ctx
->v8i32
, "");
1721 emit_data
->output
[emit_data
->chan
] =
1722 ac_build_image_get_sample_count(&ctx
->ac
, rsrc
);
1725 static LLVMValueRef
si_llvm_emit_fbfetch(struct si_shader_context
*ctx
)
1727 struct ac_image_args args
= {};
1728 LLVMValueRef ptr
, image
, fmask
;
1730 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1734 /* Load the image descriptor. */
1735 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
1736 ptr
= ac_get_arg(&ctx
->ac
, ctx
->rw_buffers
);
1737 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
1738 ac_array_in_const32_addr_space(ctx
->v8i32
), "");
1739 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1740 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
1744 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 0, 16);
1746 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1747 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->pos_fixed_pt
, 16, 16);
1749 /* Get the current render target layer index. */
1750 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
1751 args
.coords
[chan
++] = si_unpack_param(ctx
, ctx
->args
.ancillary
, 16, 11);
1753 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1754 args
.coords
[chan
++] = si_get_sample_id(ctx
);
1756 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
&&
1757 !(ctx
->screen
->debug_flags
& DBG(NO_FMASK
))) {
1758 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1759 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
1761 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
1762 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
1765 args
.opcode
= ac_image_load
;
1766 args
.resource
= image
;
1768 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1770 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1771 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1772 ac_image_2darraymsaa
: ac_image_2dmsaa
;
1773 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1774 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1775 ac_image_1darray
: ac_image_1d
;
1777 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1778 ac_image_2darray
: ac_image_2d
;
1780 return ac_build_image_opcode(&ctx
->ac
, &args
);
1783 static void si_tgsi_emit_fbfetch(const struct lp_build_tgsi_action
*action
,
1784 struct lp_build_tgsi_context
*bld_base
,
1785 struct lp_build_emit_data
*emit_data
)
1787 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1789 emit_data
->output
[emit_data
->chan
] = si_llvm_emit_fbfetch(ctx
);
1792 LLVMValueRef
si_nir_emit_fbfetch(struct ac_shader_abi
*abi
)
1794 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
1796 return si_llvm_emit_fbfetch(ctx
);
1800 * Setup actions for TGSI memory opcode, including texture opcodes.
1802 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1804 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1806 bld_base
->op_actions
[TGSI_OPCODE_TEX
].emit
= build_tex_intrinsic
;
1807 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
].emit
= build_tex_intrinsic
;
1808 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].emit
= build_tex_intrinsic
;
1809 bld_base
->op_actions
[TGSI_OPCODE_TXB
].emit
= build_tex_intrinsic
;
1810 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].emit
= build_tex_intrinsic
;
1811 bld_base
->op_actions
[TGSI_OPCODE_TXD
].emit
= build_tex_intrinsic
;
1812 bld_base
->op_actions
[TGSI_OPCODE_TXF
].emit
= build_tex_intrinsic
;
1813 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
].emit
= build_tex_intrinsic
;
1814 bld_base
->op_actions
[TGSI_OPCODE_TXL
].emit
= build_tex_intrinsic
;
1815 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].emit
= build_tex_intrinsic
;
1816 bld_base
->op_actions
[TGSI_OPCODE_TXP
].emit
= build_tex_intrinsic
;
1817 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= resq_emit
;
1818 bld_base
->op_actions
[TGSI_OPCODE_TG4
].emit
= build_tex_intrinsic
;
1819 bld_base
->op_actions
[TGSI_OPCODE_LODQ
].emit
= build_tex_intrinsic
;
1820 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1822 bld_base
->op_actions
[TGSI_OPCODE_FBFETCH
].emit
= si_tgsi_emit_fbfetch
;
1824 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1825 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1826 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1828 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].emit
= atomic_emit
;
1829 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1830 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].emit
= atomic_emit
;
1831 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1832 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].emit
= atomic_emit
;
1833 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1834 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].emit
= atomic_emit
;
1835 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1836 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].emit
= atomic_emit
;
1837 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1838 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].emit
= atomic_emit
;
1839 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1840 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].emit
= atomic_emit
;
1841 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
1842 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].emit
= atomic_emit
;
1843 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
1844 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].emit
= atomic_emit
;
1845 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
1846 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].emit
= atomic_emit
;
1847 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";
1848 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].emit
= atomic_emit
;
1849 bld_base
->op_actions
[TGSI_OPCODE_ATOMINC_WRAP
].intr_name
= "inc";
1850 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].emit
= atomic_emit
;
1851 bld_base
->op_actions
[TGSI_OPCODE_ATOMDEC_WRAP
].intr_name
= "dec";