2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "si_shader_internal.h"
27 #include "gallivm/lp_bld_arit.h"
28 #include "gallivm/lp_bld_gather.h"
29 #include "gallivm/lp_bld_intr.h"
30 #include "tgsi/tgsi_build.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "tgsi/tgsi_util.h"
34 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
35 struct lp_build_tgsi_context
*bld_base
,
36 struct lp_build_emit_data
*emit_data
);
38 static const struct lp_build_tgsi_action tex_action
;
41 * Given a v8i32 resource descriptor for a buffer, extract the size of the
42 * buffer in number of elements and return it as an i32.
44 static LLVMValueRef
get_buffer_size(
45 struct lp_build_tgsi_context
*bld_base
,
46 LLVMValueRef descriptor
)
48 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
49 LLVMBuilderRef builder
= ctx
->ac
.builder
;
51 LLVMBuildExtractElement(builder
, descriptor
,
52 LLVMConstInt(ctx
->i32
, 2, 0), "");
54 if (ctx
->screen
->info
.chip_class
== VI
) {
55 /* On VI, the descriptor contains the size in bytes,
56 * but TXQ must return the size in elements.
57 * The stride is always non-zero for resources using TXQ.
60 LLVMBuildExtractElement(builder
, descriptor
,
62 stride
= LLVMBuildLShr(builder
, stride
,
63 LLVMConstInt(ctx
->i32
, 16, 0), "");
64 stride
= LLVMBuildAnd(builder
, stride
,
65 LLVMConstInt(ctx
->i32
, 0x3FFF, 0), "");
67 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
74 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
75 const struct tgsi_full_src_register
*reg
,
80 if (!reg
->Register
.Indirect
) {
81 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, false);
83 index
= si_get_indirect_index(ctx
, ®
->Indirect
,
84 1, reg
->Register
.Index
);
88 return ctx
->abi
.load_ubo(&ctx
->abi
, index
);
90 return ctx
->abi
.load_ssbo(&ctx
->abi
, index
, false);
93 static bool tgsi_is_array_image(unsigned target
)
95 return target
== TGSI_TEXTURE_3D
||
96 target
== TGSI_TEXTURE_CUBE
||
97 target
== TGSI_TEXTURE_1D_ARRAY
||
98 target
== TGSI_TEXTURE_2D_ARRAY
||
99 target
== TGSI_TEXTURE_CUBE_ARRAY
||
100 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
;
104 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
106 * At least on Tonga, executing image stores on images with DCC enabled and
107 * non-trivial can eventually lead to lockups. This can occur when an
108 * application binds an image as read-only but then uses a shader that writes
109 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
110 * program termination) in this case, but it doesn't cost much to be a bit
111 * nicer: disabling DCC in the shader still leads to undefined results but
114 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
117 if (ctx
->screen
->info
.chip_class
<= CIK
) {
120 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
121 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
124 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, i32_6
, "");
125 tmp
= LLVMBuildAnd(ctx
->ac
.builder
, tmp
, i32_C
, "");
126 return LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, tmp
, i32_6
, "");
130 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
131 LLVMValueRef list
, LLVMValueRef index
,
132 enum ac_descriptor_type desc_type
, bool dcc_off
)
134 LLVMBuilderRef builder
= ctx
->ac
.builder
;
137 if (desc_type
== AC_DESC_BUFFER
) {
138 index
= LLVMBuildMul(builder
, index
,
139 LLVMConstInt(ctx
->i32
, 2, 0), "");
140 index
= LLVMBuildAdd(builder
, index
,
142 list
= LLVMBuildPointerCast(builder
, list
,
143 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
145 assert(desc_type
== AC_DESC_IMAGE
);
148 rsrc
= ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
149 if (desc_type
== AC_DESC_IMAGE
&& dcc_off
)
150 rsrc
= force_dcc_off(ctx
, rsrc
);
155 * Load the resource descriptor for \p image.
159 struct lp_build_tgsi_context
*bld_base
,
160 const struct tgsi_full_src_register
*image
,
161 bool is_store
, unsigned target
,
164 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
165 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
166 ctx
->param_samplers_and_images
);
168 bool dcc_off
= is_store
;
170 if (!image
->Register
.Indirect
) {
171 const struct tgsi_shader_info
*info
= bld_base
->info
;
172 unsigned images_writemask
= info
->images_store
|
175 index
= LLVMConstInt(ctx
->i32
,
176 si_get_image_slot(image
->Register
.Index
), 0);
178 if (images_writemask
& (1 << image
->Register
.Index
))
181 /* From the GL_ARB_shader_image_load_store extension spec:
183 * If a shader performs an image load, store, or atomic
184 * operation using an image variable declared as an array,
185 * and if the index used to select an individual element is
186 * negative or greater than or equal to the size of the
187 * array, the results of the operation are undefined but may
188 * not lead to termination.
190 index
= si_get_bounded_indirect_index(ctx
, &image
->Indirect
,
191 image
->Register
.Index
,
193 index
= LLVMBuildSub(ctx
->ac
.builder
,
194 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
- 1, 0),
198 if (image
->Register
.File
!= TGSI_FILE_IMAGE
) {
199 /* Bindless descriptors are accessible from a different pair of
202 rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
203 ctx
->param_bindless_samplers_and_images
);
204 index
= lp_build_emit_fetch_src(bld_base
, image
,
205 TGSI_TYPE_UNSIGNED
, 0);
207 /* For simplicity, bindless image descriptors use fixed
208 * 16-dword slots for now.
210 index
= LLVMBuildMul(ctx
->ac
.builder
, index
,
211 LLVMConstInt(ctx
->i32
, 2, 0), "");
214 *rsrc
= si_load_image_desc(ctx
, rsrc_ptr
, index
,
215 target
== TGSI_TEXTURE_BUFFER
? AC_DESC_BUFFER
: AC_DESC_IMAGE
,
219 static LLVMValueRef
image_fetch_coords(
220 struct lp_build_tgsi_context
*bld_base
,
221 const struct tgsi_full_instruction
*inst
,
222 unsigned src
, LLVMValueRef desc
)
224 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
225 LLVMBuilderRef builder
= ctx
->ac
.builder
;
226 unsigned target
= inst
->Memory
.Texture
;
227 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
228 LLVMValueRef coords
[4];
232 for (chan
= 0; chan
< num_coords
; ++chan
) {
233 tmp
= lp_build_emit_fetch(bld_base
, inst
, src
, chan
);
234 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
238 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
239 /* 1D textures are allocated and used as 2D on GFX9. */
240 if (target
== TGSI_TEXTURE_1D
) {
241 coords
[1] = ctx
->i32_0
;
243 } else if (target
== TGSI_TEXTURE_1D_ARRAY
) {
244 coords
[2] = coords
[1];
245 coords
[1] = ctx
->i32_0
;
247 } else if (target
== TGSI_TEXTURE_2D
) {
248 /* The hw can't bind a slice of a 3D image as a 2D
249 * image, because it ignores BASE_ARRAY if the target
250 * is 3D. The workaround is to read BASE_ARRAY and set
251 * it as the 3rd address operand for all 2D images.
253 LLVMValueRef first_layer
, const5
, mask
;
255 const5
= LLVMConstInt(ctx
->i32
, 5, 0);
256 mask
= LLVMConstInt(ctx
->i32
, S_008F24_BASE_ARRAY(~0), 0);
257 first_layer
= LLVMBuildExtractElement(builder
, desc
, const5
, "");
258 first_layer
= LLVMBuildAnd(builder
, first_layer
, mask
, "");
260 coords
[2] = first_layer
;
268 if (num_coords
== 3) {
269 /* LLVM has difficulties lowering 3-element vectors. */
270 coords
[3] = bld_base
->uint_bld
.undef
;
274 return lp_build_gather_values(&ctx
->gallivm
, coords
, num_coords
);
278 * Append the extra mode bits that are used by image load and store.
280 static void image_append_args(
281 struct si_shader_context
*ctx
,
282 struct lp_build_emit_data
* emit_data
,
287 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
288 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
289 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
290 LLVMValueRef r128
= i1false
;
291 LLVMValueRef da
= tgsi_is_array_image(target
) ? i1true
: i1false
;
294 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
296 LLVMValueRef slc
= i1false
;
297 LLVMValueRef lwe
= i1false
;
300 emit_data
->args
[emit_data
->arg_count
++] = r128
;
301 emit_data
->args
[emit_data
->arg_count
++] = da
;
303 emit_data
->args
[emit_data
->arg_count
++] = glc
;
305 emit_data
->args
[emit_data
->arg_count
++] = slc
;
309 emit_data
->args
[emit_data
->arg_count
++] = glc
;
310 emit_data
->args
[emit_data
->arg_count
++] = slc
;
311 emit_data
->args
[emit_data
->arg_count
++] = lwe
;
312 emit_data
->args
[emit_data
->arg_count
++] = da
;
316 * Append the resource and indexing arguments for buffer intrinsics.
318 * \param rsrc the v4i32 buffer resource
319 * \param index index into the buffer (stride-based)
320 * \param offset byte offset into the buffer
322 static void buffer_append_args(
323 struct si_shader_context
*ctx
,
324 struct lp_build_emit_data
*emit_data
,
331 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
332 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
333 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
335 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
336 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
337 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
339 emit_data
->args
[emit_data
->arg_count
++] =
341 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
342 i1true
: i1false
; /* glc */
344 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
347 static void load_fetch_args(
348 struct lp_build_tgsi_context
* bld_base
,
349 struct lp_build_emit_data
* emit_data
)
351 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
352 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
353 unsigned target
= inst
->Memory
.Texture
;
356 emit_data
->dst_type
= ctx
->v4f32
;
358 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
359 inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
363 bool ubo
= inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
;
364 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], ubo
);
366 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
367 offset
= ac_to_integer(&ctx
->ac
, tmp
);
369 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
370 offset
, false, false);
371 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
372 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
375 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
376 coords
= image_fetch_coords(bld_base
, inst
, 1, rsrc
);
378 if (target
== TGSI_TEXTURE_BUFFER
) {
379 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
380 ctx
->i32_0
, false, false);
382 emit_data
->args
[0] = coords
;
383 emit_data
->args
[1] = rsrc
;
384 emit_data
->args
[2] = LLVMConstInt(ctx
->i32
, 15, 0); /* dmask */
385 emit_data
->arg_count
= 3;
387 image_append_args(ctx
, emit_data
, target
, false, false);
392 static void load_emit_buffer(struct si_shader_context
*ctx
,
393 struct lp_build_emit_data
*emit_data
,
394 bool can_speculate
, bool allow_smem
)
396 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
397 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
398 uint count
= util_last_bit(writemask
);
399 LLVMValueRef
*args
= emit_data
->args
;
401 /* Don't use SMEM for shader buffer loads, because LLVM doesn't
402 * select SMEM for SI.load.const with a non-constant offset, and
403 * constant offsets practically don't exist with shader buffers.
405 * Also, SI.load.const doesn't use inst_offset when it's lowered
406 * to VMEM, so we just end up with more VALU instructions in the end
409 * TODO: Remove this line once LLVM can select SMEM with a non-constant
410 * offset, and can derive inst_offset when VMEM is selected.
411 * After that, si_memory_barrier should invalidate sL1 for shader
415 assert(LLVMConstIntGetZExtValue(args
[1]) == 0); /* vindex */
416 emit_data
->output
[emit_data
->chan
] =
417 ac_build_buffer_load(&ctx
->ac
, args
[0], count
, NULL
,
419 LLVMConstIntGetZExtValue(args
[3]),
420 LLVMConstIntGetZExtValue(args
[4]),
421 can_speculate
, allow_smem
);
424 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
425 const struct tgsi_full_instruction
*inst
,
426 LLVMTypeRef type
, int arg
)
428 LLVMBuilderRef builder
= ctx
->ac
.builder
;
429 LLVMValueRef offset
, ptr
;
432 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
433 offset
= ac_to_integer(&ctx
->ac
, offset
);
436 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
437 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
438 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
443 static void load_emit_memory(
444 struct si_shader_context
*ctx
,
445 struct lp_build_emit_data
*emit_data
)
447 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
448 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
449 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
452 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 1);
454 for (chan
= 0; chan
< 4; ++chan
) {
455 if (!(writemask
& (1 << chan
))) {
456 channels
[chan
] = LLVMGetUndef(ctx
->f32
);
460 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
461 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
462 channels
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
464 emit_data
->output
[emit_data
->chan
] = lp_build_gather_values(&ctx
->gallivm
, channels
, 4);
468 * Return true if the memory accessed by a LOAD or STORE instruction is
469 * read-only or write-only, respectively.
471 * \param shader_buffers_reverse_access_mask
472 * For LOAD, set this to (store | atomic) slot usage in the shader.
473 * For STORE, set this to (load | atomic) slot usage in the shader.
474 * \param images_reverse_access_mask Same as above, but for images.
476 static bool is_oneway_access_only(const struct tgsi_full_instruction
*inst
,
477 const struct tgsi_shader_info
*info
,
478 unsigned shader_buffers_reverse_access_mask
,
479 unsigned images_reverse_access_mask
)
481 /* RESTRICT means NOALIAS.
482 * If there are no writes, we can assume the accessed memory is read-only.
483 * If there are no reads, we can assume the accessed memory is write-only.
485 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_RESTRICT
) {
486 unsigned reverse_access_mask
;
488 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
489 reverse_access_mask
= shader_buffers_reverse_access_mask
;
490 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
491 reverse_access_mask
= info
->images_buffers
&
492 images_reverse_access_mask
;
494 reverse_access_mask
= ~info
->images_buffers
&
495 images_reverse_access_mask
;
498 if (inst
->Src
[0].Register
.Indirect
) {
499 if (!reverse_access_mask
)
502 if (!(reverse_access_mask
&
503 (1u << inst
->Src
[0].Register
.Index
)))
508 /* If there are no buffer writes (for both shader buffers & image
509 * buffers), it implies that buffer memory is read-only.
510 * If there are no buffer reads (for both shader buffers & image
511 * buffers), it implies that buffer memory is write-only.
513 * Same for the case when there are no writes/reads for non-buffer
516 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
517 (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
&&
518 (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
519 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)))) {
520 if (!shader_buffers_reverse_access_mask
&&
521 !(info
->images_buffers
& images_reverse_access_mask
))
524 if (!(~info
->images_buffers
& images_reverse_access_mask
))
530 static void load_emit(
531 const struct lp_build_tgsi_action
*action
,
532 struct lp_build_tgsi_context
*bld_base
,
533 struct lp_build_emit_data
*emit_data
)
535 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
536 LLVMBuilderRef builder
= ctx
->ac
.builder
;
537 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
538 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
539 char intrinsic_name
[64];
540 bool can_speculate
= false;
542 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
543 load_emit_memory(ctx
, emit_data
);
547 if (inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
548 load_emit_buffer(ctx
, emit_data
, true, true);
552 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
553 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
555 can_speculate
= !(inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
) &&
556 is_oneway_access_only(inst
, info
,
557 info
->shader_buffers_store
|
558 info
->shader_buffers_atomic
,
560 info
->images_atomic
);
562 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
563 load_emit_buffer(ctx
, emit_data
, can_speculate
, false);
567 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
568 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
569 LLVMValueRef result
=
570 ac_build_buffer_load_format(&ctx
->ac
,
575 LLVMConstIntGetZExtValue(emit_data
->args
[3]),
577 emit_data
->output
[emit_data
->chan
] =
578 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
580 ac_get_image_intr_name("llvm.amdgcn.image.load",
581 emit_data
->dst_type
, /* vdata */
582 LLVMTypeOf(emit_data
->args
[0]), /* coords */
583 LLVMTypeOf(emit_data
->args
[1]), /* rsrc */
584 intrinsic_name
, sizeof(intrinsic_name
));
586 emit_data
->output
[emit_data
->chan
] =
588 builder
, intrinsic_name
, emit_data
->dst_type
,
589 emit_data
->args
, emit_data
->arg_count
,
590 ac_get_load_intr_attribs(can_speculate
));
594 static void store_fetch_args(
595 struct lp_build_tgsi_context
* bld_base
,
596 struct lp_build_emit_data
* emit_data
)
598 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
599 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
600 struct tgsi_full_src_register memory
;
601 LLVMValueRef chans
[4];
606 emit_data
->dst_type
= ctx
->voidt
;
608 for (chan
= 0; chan
< 4; ++chan
) {
609 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
611 data
= lp_build_gather_values(&ctx
->gallivm
, chans
, 4);
613 emit_data
->args
[emit_data
->arg_count
++] = data
;
615 memory
= tgsi_full_src_register_from_dst(&inst
->Dst
[0]);
617 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
621 rsrc
= shader_buffer_fetch_rsrc(ctx
, &memory
, false);
623 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
624 offset
= ac_to_integer(&ctx
->ac
, tmp
);
626 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
627 offset
, false, false);
628 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
||
629 tgsi_is_bindless_image_file(inst
->Dst
[0].Register
.File
)) {
630 unsigned target
= inst
->Memory
.Texture
;
633 /* 8bit/16bit TC L1 write corruption bug on SI.
634 * All store opcodes not aligned to a dword are affected.
636 * The only way to get unaligned stores in radeonsi is through
639 bool force_glc
= ctx
->screen
->info
.chip_class
== SI
;
641 image_fetch_rsrc(bld_base
, &memory
, true, target
, &rsrc
);
642 coords
= image_fetch_coords(bld_base
, inst
, 0, rsrc
);
644 if (target
== TGSI_TEXTURE_BUFFER
) {
645 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
646 ctx
->i32_0
, false, force_glc
);
648 emit_data
->args
[1] = coords
;
649 emit_data
->args
[2] = rsrc
;
650 emit_data
->args
[3] = LLVMConstInt(ctx
->i32
, 15, 0); /* dmask */
651 emit_data
->arg_count
= 4;
653 image_append_args(ctx
, emit_data
, target
, false, force_glc
);
658 static void store_emit_buffer(
659 struct si_shader_context
*ctx
,
660 struct lp_build_emit_data
*emit_data
,
661 bool writeonly_memory
)
663 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
664 LLVMBuilderRef builder
= ctx
->ac
.builder
;
665 LLVMValueRef base_data
= emit_data
->args
[0];
666 LLVMValueRef base_offset
= emit_data
->args
[3];
667 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
671 const char *intrinsic_name
;
676 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
678 /* Due to an LLVM limitation, split 3-element writes
679 * into a 2-element and a 1-element write. */
681 writemask
|= 1 << (start
+ 2);
687 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
688 } else if (count
== 2) {
689 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
691 tmp
= LLVMBuildExtractElement(
693 LLVMConstInt(ctx
->i32
, start
, 0), "");
694 data
= LLVMBuildInsertElement(
695 builder
, LLVMGetUndef(v2f32
), tmp
,
698 tmp
= LLVMBuildExtractElement(
700 LLVMConstInt(ctx
->i32
, start
+ 1, 0), "");
701 data
= LLVMBuildInsertElement(
702 builder
, data
, tmp
, ctx
->i32_1
, "");
704 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
707 data
= LLVMBuildExtractElement(
709 LLVMConstInt(ctx
->i32
, start
, 0), "");
710 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
713 offset
= base_offset
;
715 offset
= LLVMBuildAdd(
717 LLVMConstInt(ctx
->i32
, start
* 4, 0), "");
720 emit_data
->args
[0] = data
;
721 emit_data
->args
[3] = offset
;
724 builder
, intrinsic_name
, emit_data
->dst_type
,
725 emit_data
->args
, emit_data
->arg_count
,
726 ac_get_store_intr_attribs(writeonly_memory
));
730 static void store_emit_memory(
731 struct si_shader_context
*ctx
,
732 struct lp_build_emit_data
*emit_data
)
734 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
735 LLVMBuilderRef builder
= ctx
->ac
.builder
;
736 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
737 LLVMValueRef ptr
, derived_ptr
, data
, index
;
740 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 0);
742 for (chan
= 0; chan
< 4; ++chan
) {
743 if (!(writemask
& (1 << chan
))) {
746 data
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 1, chan
);
747 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
748 derived_ptr
= LLVMBuildGEP(builder
, ptr
, &index
, 1, "");
749 LLVMBuildStore(builder
, data
, derived_ptr
);
753 static void store_emit(
754 const struct lp_build_tgsi_action
*action
,
755 struct lp_build_tgsi_context
*bld_base
,
756 struct lp_build_emit_data
*emit_data
)
758 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
759 LLVMBuilderRef builder
= ctx
->ac
.builder
;
760 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
761 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
762 unsigned target
= inst
->Memory
.Texture
;
763 char intrinsic_name
[64];
764 bool writeonly_memory
= false;
766 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
767 store_emit_memory(ctx
, emit_data
);
771 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
772 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
774 writeonly_memory
= is_oneway_access_only(inst
, info
,
775 info
->shader_buffers_load
|
776 info
->shader_buffers_atomic
,
778 info
->images_atomic
);
780 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
781 store_emit_buffer(ctx
, emit_data
, writeonly_memory
);
785 if (target
== TGSI_TEXTURE_BUFFER
) {
786 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
787 builder
, "llvm.amdgcn.buffer.store.format.v4f32",
788 emit_data
->dst_type
, emit_data
->args
,
789 emit_data
->arg_count
,
790 ac_get_store_intr_attribs(writeonly_memory
));
792 ac_get_image_intr_name("llvm.amdgcn.image.store",
793 LLVMTypeOf(emit_data
->args
[0]), /* vdata */
794 LLVMTypeOf(emit_data
->args
[1]), /* coords */
795 LLVMTypeOf(emit_data
->args
[2]), /* rsrc */
796 intrinsic_name
, sizeof(intrinsic_name
));
798 emit_data
->output
[emit_data
->chan
] =
800 builder
, intrinsic_name
, emit_data
->dst_type
,
801 emit_data
->args
, emit_data
->arg_count
,
802 ac_get_store_intr_attribs(writeonly_memory
));
806 static void atomic_fetch_args(
807 struct lp_build_tgsi_context
* bld_base
,
808 struct lp_build_emit_data
* emit_data
)
810 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
811 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
812 LLVMValueRef data1
, data2
;
816 emit_data
->dst_type
= ctx
->f32
;
818 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
819 data1
= ac_to_integer(&ctx
->ac
, tmp
);
821 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
822 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
823 data2
= ac_to_integer(&ctx
->ac
, tmp
);
826 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
827 * of arguments, which is reversed relative to TGSI (and GLSL)
829 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
830 emit_data
->args
[emit_data
->arg_count
++] = data2
;
831 emit_data
->args
[emit_data
->arg_count
++] = data1
;
833 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
836 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], false);
838 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
839 offset
= ac_to_integer(&ctx
->ac
, tmp
);
841 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
842 offset
, true, false);
843 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
844 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
845 unsigned target
= inst
->Memory
.Texture
;
848 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
849 coords
= image_fetch_coords(bld_base
, inst
, 1, rsrc
);
851 if (target
== TGSI_TEXTURE_BUFFER
) {
852 buffer_append_args(ctx
, emit_data
, rsrc
, coords
,
853 ctx
->i32_0
, true, false);
855 emit_data
->args
[emit_data
->arg_count
++] = coords
;
856 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
858 image_append_args(ctx
, emit_data
, target
, true, false);
863 static void atomic_emit_memory(struct si_shader_context
*ctx
,
864 struct lp_build_emit_data
*emit_data
) {
865 LLVMBuilderRef builder
= ctx
->ac
.builder
;
866 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
867 LLVMValueRef ptr
, result
, arg
;
869 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
871 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
872 arg
= ac_to_integer(&ctx
->ac
, arg
);
874 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
875 LLVMValueRef new_data
;
876 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
879 new_data
= ac_to_integer(&ctx
->ac
, new_data
);
881 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
882 LLVMAtomicOrderingSequentiallyConsistent
,
883 LLVMAtomicOrderingSequentiallyConsistent
,
886 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
888 LLVMAtomicRMWBinOp op
;
890 switch(inst
->Instruction
.Opcode
) {
891 case TGSI_OPCODE_ATOMUADD
:
892 op
= LLVMAtomicRMWBinOpAdd
;
894 case TGSI_OPCODE_ATOMXCHG
:
895 op
= LLVMAtomicRMWBinOpXchg
;
897 case TGSI_OPCODE_ATOMAND
:
898 op
= LLVMAtomicRMWBinOpAnd
;
900 case TGSI_OPCODE_ATOMOR
:
901 op
= LLVMAtomicRMWBinOpOr
;
903 case TGSI_OPCODE_ATOMXOR
:
904 op
= LLVMAtomicRMWBinOpXor
;
906 case TGSI_OPCODE_ATOMUMIN
:
907 op
= LLVMAtomicRMWBinOpUMin
;
909 case TGSI_OPCODE_ATOMUMAX
:
910 op
= LLVMAtomicRMWBinOpUMax
;
912 case TGSI_OPCODE_ATOMIMIN
:
913 op
= LLVMAtomicRMWBinOpMin
;
915 case TGSI_OPCODE_ATOMIMAX
:
916 op
= LLVMAtomicRMWBinOpMax
;
919 unreachable("unknown atomic opcode");
922 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
923 LLVMAtomicOrderingSequentiallyConsistent
,
926 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
929 static void atomic_emit(
930 const struct lp_build_tgsi_action
*action
,
931 struct lp_build_tgsi_context
*bld_base
,
932 struct lp_build_emit_data
*emit_data
)
934 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
935 LLVMBuilderRef builder
= ctx
->ac
.builder
;
936 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
937 char intrinsic_name
[40];
940 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
941 atomic_emit_memory(ctx
, emit_data
);
945 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
946 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
947 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
948 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
953 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
954 coords
= emit_data
->args
[2];
956 coords
= emit_data
->args
[1];
958 ac_build_type_name_for_intr(LLVMTypeOf(coords
), coords_type
, sizeof(coords_type
));
959 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
960 "llvm.amdgcn.image.atomic.%s.%s",
961 action
->intr_name
, coords_type
);
964 tmp
= lp_build_intrinsic(
965 builder
, intrinsic_name
, ctx
->i32
,
966 emit_data
->args
, emit_data
->arg_count
, 0);
967 emit_data
->output
[emit_data
->chan
] = ac_to_float(&ctx
->ac
, tmp
);
970 static void set_tex_fetch_args(struct si_shader_context
*ctx
,
971 struct lp_build_emit_data
*emit_data
,
973 LLVMValueRef res_ptr
, LLVMValueRef samp_ptr
,
974 LLVMValueRef
*param
, unsigned count
,
977 struct ac_image_args args
= {};
979 /* Pad to power of two vector */
980 while (count
< util_next_power_of_two(count
))
981 param
[count
++] = LLVMGetUndef(ctx
->i32
);
984 args
.addr
= lp_build_gather_values(&ctx
->gallivm
, param
, count
);
986 args
.addr
= param
[0];
988 args
.resource
= res_ptr
;
989 args
.sampler
= samp_ptr
;
991 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
992 target
== TGSI_TEXTURE_SHADOWRECT
;
993 args
.da
= tgsi_is_array_sampler(target
);
995 /* Ugly, but we seem to have no other choice right now. */
996 STATIC_ASSERT(sizeof(args
) <= sizeof(emit_data
->args
));
997 memcpy(emit_data
->args
, &args
, sizeof(args
));
1000 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
1001 unsigned target
, LLVMValueRef out
)
1003 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1005 /* 1D textures are allocated and used as 2D on GFX9. */
1006 if (ctx
->screen
->info
.chip_class
>= GFX9
&&
1007 (target
== TGSI_TEXTURE_1D_ARRAY
||
1008 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
1009 LLVMValueRef layers
=
1010 LLVMBuildExtractElement(builder
, out
,
1011 LLVMConstInt(ctx
->i32
, 2, 0), "");
1012 out
= LLVMBuildInsertElement(builder
, out
, layers
,
1016 /* Divide the number of layers by 6 to get the number of cubes. */
1017 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
1018 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1019 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
1021 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
1022 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
1024 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
1029 static void resq_fetch_args(
1030 struct lp_build_tgsi_context
* bld_base
,
1031 struct lp_build_emit_data
* emit_data
)
1033 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1034 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1035 const struct tgsi_full_src_register
*reg
= &inst
->Src
[0];
1037 emit_data
->dst_type
= ctx
->v4i32
;
1039 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
1040 emit_data
->args
[0] = shader_buffer_fetch_rsrc(ctx
, reg
, false);
1041 emit_data
->arg_count
= 1;
1042 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1043 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
1044 &emit_data
->args
[0]);
1045 emit_data
->arg_count
= 1;
1047 LLVMValueRef res_ptr
;
1048 unsigned image_target
;
1050 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
1051 image_target
= TGSI_TEXTURE_2D_ARRAY
;
1053 image_target
= inst
->Memory
.Texture
;
1055 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
,
1057 set_tex_fetch_args(ctx
, emit_data
, image_target
,
1058 res_ptr
, NULL
, &ctx
->i32_0
, 1,
1063 static void resq_emit(
1064 const struct lp_build_tgsi_action
*action
,
1065 struct lp_build_tgsi_context
*bld_base
,
1066 struct lp_build_emit_data
*emit_data
)
1068 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1069 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1070 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1073 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
1074 out
= LLVMBuildExtractElement(builder
, emit_data
->args
[0],
1075 LLVMConstInt(ctx
->i32
, 2, 0), "");
1076 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1077 out
= get_buffer_size(bld_base
, emit_data
->args
[0]);
1079 struct ac_image_args args
;
1081 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1082 args
.opcode
= ac_image_get_resinfo
;
1083 out
= ac_build_image_opcode(&ctx
->ac
, &args
);
1085 out
= fix_resinfo(ctx
, inst
->Memory
.Texture
, out
);
1088 emit_data
->output
[emit_data
->chan
] = out
;
1092 * Load an image view, fmask view. or sampler state descriptor.
1094 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
1095 LLVMValueRef list
, LLVMValueRef index
,
1096 enum ac_descriptor_type type
)
1098 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1102 /* The image is at [0:7]. */
1103 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1105 case AC_DESC_BUFFER
:
1106 /* The buffer is in [4:7]. */
1107 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1108 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1109 list
= LLVMBuildPointerCast(builder
, list
,
1110 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1113 /* The FMASK is at [8:15]. */
1114 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1115 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1117 case AC_DESC_SAMPLER
:
1118 /* The sampler state is at [12:15]. */
1119 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1120 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
1121 list
= LLVMBuildPointerCast(builder
, list
,
1122 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1126 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
1129 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1132 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1133 * filtering manually. The driver sets img7 to a mask clearing
1134 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1135 * s_and_b32 samp0, samp0, img7
1138 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1140 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
1141 LLVMValueRef res
, LLVMValueRef samp
)
1143 LLVMValueRef img7
, samp0
;
1145 if (ctx
->screen
->info
.chip_class
>= VI
)
1148 img7
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1149 LLVMConstInt(ctx
->i32
, 7, 0), "");
1150 samp0
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp
,
1152 samp0
= LLVMBuildAnd(ctx
->ac
.builder
, samp0
, img7
, "");
1153 return LLVMBuildInsertElement(ctx
->ac
.builder
, samp
, samp0
,
1157 static void tex_fetch_ptrs(
1158 struct lp_build_tgsi_context
*bld_base
,
1159 struct lp_build_emit_data
*emit_data
,
1160 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
, LLVMValueRef
*fmask_ptr
)
1162 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1163 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
1164 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1165 const struct tgsi_full_src_register
*reg
;
1166 unsigned target
= inst
->Texture
.Texture
;
1167 unsigned sampler_src
;
1170 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1171 reg
= &emit_data
->inst
->Src
[sampler_src
];
1173 if (reg
->Register
.Indirect
) {
1174 index
= si_get_bounded_indirect_index(ctx
,
1176 reg
->Register
.Index
,
1178 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1179 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
/ 2, 0), "");
1181 index
= LLVMConstInt(ctx
->i32
,
1182 si_get_sampler_slot(reg
->Register
.Index
), 0);
1185 if (reg
->Register
.File
!= TGSI_FILE_SAMPLER
) {
1186 /* Bindless descriptors are accessible from a different pair of
1187 * user SGPR indices.
1189 list
= LLVMGetParam(ctx
->main_fn
,
1190 ctx
->param_bindless_samplers_and_images
);
1191 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1192 TGSI_TYPE_UNSIGNED
, 0);
1195 if (target
== TGSI_TEXTURE_BUFFER
)
1196 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_BUFFER
);
1198 *res_ptr
= si_load_sampler_desc(ctx
, list
, index
, AC_DESC_IMAGE
);
1205 if (target
== TGSI_TEXTURE_2D_MSAA
||
1206 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1208 *fmask_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1210 } else if (target
!= TGSI_TEXTURE_BUFFER
) {
1212 *samp_ptr
= si_load_sampler_desc(ctx
, list
, index
,
1214 *samp_ptr
= sici_fix_sampler_aniso(ctx
, *res_ptr
, *samp_ptr
);
1219 static void txq_fetch_args(
1220 struct lp_build_tgsi_context
*bld_base
,
1221 struct lp_build_emit_data
*emit_data
)
1223 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1224 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1225 unsigned target
= inst
->Texture
.Texture
;
1226 LLVMValueRef res_ptr
;
1227 LLVMValueRef address
;
1229 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, NULL
, NULL
);
1231 if (target
== TGSI_TEXTURE_BUFFER
) {
1232 /* Read the size from the buffer descriptor directly. */
1233 emit_data
->args
[0] = get_buffer_size(bld_base
, res_ptr
);
1237 /* Textures - set the mip level. */
1238 address
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1240 set_tex_fetch_args(ctx
, emit_data
, target
, res_ptr
,
1241 NULL
, &address
, 1, 0xf);
1244 static void txq_emit(const struct lp_build_tgsi_action
*action
,
1245 struct lp_build_tgsi_context
*bld_base
,
1246 struct lp_build_emit_data
*emit_data
)
1248 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1249 struct ac_image_args args
;
1250 unsigned target
= emit_data
->inst
->Texture
.Texture
;
1252 if (target
== TGSI_TEXTURE_BUFFER
) {
1253 /* Just return the buffer size. */
1254 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
1258 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1260 args
.opcode
= ac_image_get_resinfo
;
1261 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1263 emit_data
->output
[emit_data
->chan
] = fix_resinfo(ctx
, target
, result
);
1266 static void tex_fetch_args(
1267 struct lp_build_tgsi_context
*bld_base
,
1268 struct lp_build_emit_data
*emit_data
)
1270 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1271 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1272 unsigned opcode
= inst
->Instruction
.Opcode
;
1273 unsigned target
= inst
->Texture
.Texture
;
1274 LLVMValueRef coords
[5], derivs
[6];
1275 LLVMValueRef address
[16];
1276 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
1277 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1280 unsigned num_deriv_channels
= 0;
1281 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1282 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
1283 unsigned dmask
= 0xf;
1285 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
1287 if (target
== TGSI_TEXTURE_BUFFER
) {
1288 emit_data
->dst_type
= ctx
->v4f32
;
1289 emit_data
->args
[0] = res_ptr
;
1290 emit_data
->args
[1] = ctx
->i32_0
;
1291 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
1292 emit_data
->arg_count
= 3;
1296 /* Fetch and project texture coordinates */
1297 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1298 for (chan
= 0; chan
< 3; chan
++) {
1299 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1302 if (opcode
== TGSI_OPCODE_TXP
)
1303 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1309 if (opcode
== TGSI_OPCODE_TXP
)
1310 coords
[3] = ctx
->ac
.f32_1
;
1314 opcode
!= TGSI_OPCODE_TXF
&&
1315 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1316 /* The offsets are six-bit signed integers packed like this:
1317 * X=[5:0], Y=[13:8], and Z=[21:16].
1319 LLVMValueRef offset
[3], pack
;
1321 assert(inst
->Texture
.NumOffsets
== 1);
1323 for (chan
= 0; chan
< 3; chan
++) {
1324 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
1325 emit_data
->inst
, 0, chan
);
1326 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1327 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1329 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1330 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1333 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1334 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1335 address
[count
++] = pack
;
1338 /* Pack LOD bias value */
1339 if (opcode
== TGSI_OPCODE_TXB
)
1340 address
[count
++] = coords
[3];
1341 if (opcode
== TGSI_OPCODE_TXB2
)
1342 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1344 /* Pack depth comparison value */
1345 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1348 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1349 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1351 assert(ref_pos
>= 0);
1352 z
= coords
[ref_pos
];
1355 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1356 * OpenGL 4.5 spec says:
1358 * "If the texture’s internal format indicates a fixed-point
1359 * depth texture, then D_t and D_ref are clamped to the
1360 * range [0, 1]; otherwise no clamping is performed."
1362 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1363 * so the depth comparison value isn't clamped for Z16 and
1364 * Z24 anymore. Do it manually here.
1366 if (ctx
->screen
->info
.chip_class
>= VI
) {
1367 LLVMValueRef upgraded
;
1368 LLVMValueRef clamped
;
1369 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp_ptr
,
1370 LLVMConstInt(ctx
->i32
, 3, false), "");
1371 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1372 LLVMConstInt(ctx
->i32
, 29, false), "");
1373 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1374 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1375 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1378 address
[count
++] = z
;
1381 /* Pack user derivatives */
1382 if (opcode
== TGSI_OPCODE_TXD
) {
1383 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1386 case TGSI_TEXTURE_3D
:
1387 num_src_deriv_channels
= 3;
1388 num_dst_deriv_channels
= 3;
1389 num_deriv_channels
= 3;
1391 case TGSI_TEXTURE_2D
:
1392 case TGSI_TEXTURE_SHADOW2D
:
1393 case TGSI_TEXTURE_RECT
:
1394 case TGSI_TEXTURE_SHADOWRECT
:
1395 case TGSI_TEXTURE_2D_ARRAY
:
1396 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1397 num_src_deriv_channels
= 2;
1398 num_dst_deriv_channels
= 2;
1399 num_deriv_channels
= 2;
1401 case TGSI_TEXTURE_CUBE
:
1402 case TGSI_TEXTURE_SHADOWCUBE
:
1403 case TGSI_TEXTURE_CUBE_ARRAY
:
1404 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1405 /* Cube derivatives will be converted to 2D. */
1406 num_src_deriv_channels
= 3;
1407 num_dst_deriv_channels
= 3;
1408 num_deriv_channels
= 2;
1410 case TGSI_TEXTURE_1D
:
1411 case TGSI_TEXTURE_SHADOW1D
:
1412 case TGSI_TEXTURE_1D_ARRAY
:
1413 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1414 num_src_deriv_channels
= 1;
1416 /* 1D textures are allocated and used as 2D on GFX9. */
1417 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1418 num_dst_deriv_channels
= 2;
1419 num_deriv_channels
= 2;
1421 num_dst_deriv_channels
= 1;
1422 num_deriv_channels
= 1;
1426 unreachable("invalid target");
1429 for (param
= 0; param
< 2; param
++) {
1430 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1431 derivs
[param
* num_dst_deriv_channels
+ chan
] =
1432 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1434 /* Fill in the rest with zeros. */
1435 for (chan
= num_src_deriv_channels
;
1436 chan
< num_dst_deriv_channels
; chan
++)
1437 derivs
[param
* num_dst_deriv_channels
+ chan
] =
1442 if (target
== TGSI_TEXTURE_CUBE
||
1443 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1444 target
== TGSI_TEXTURE_SHADOWCUBE
||
1445 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1446 ac_prepare_cube_coords(&ctx
->ac
,
1447 opcode
== TGSI_OPCODE_TXD
,
1448 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1449 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1450 opcode
== TGSI_OPCODE_LODQ
,
1452 } else if (tgsi_is_array_sampler(target
) &&
1453 opcode
!= TGSI_OPCODE_TXF
&&
1454 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1455 ctx
->screen
->info
.chip_class
<= VI
) {
1456 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1457 coords
[array_coord
] =
1458 ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
,
1459 &coords
[array_coord
], 1, 0);
1462 if (opcode
== TGSI_OPCODE_TXD
)
1463 for (int i
= 0; i
< num_deriv_channels
* 2; i
++)
1464 address
[count
++] = derivs
[i
];
1466 /* Pack texture coordinates */
1467 address
[count
++] = coords
[0];
1469 address
[count
++] = coords
[1];
1471 address
[count
++] = coords
[2];
1473 /* 1D textures are allocated and used as 2D on GFX9. */
1474 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1475 LLVMValueRef filler
;
1477 /* Use 0.5, so that we don't sample the border color. */
1478 if (opcode
== TGSI_OPCODE_TXF
||
1479 opcode
== TGSI_OPCODE_TXF_LZ
)
1480 filler
= ctx
->i32_0
;
1482 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1484 if (target
== TGSI_TEXTURE_1D
||
1485 target
== TGSI_TEXTURE_SHADOW1D
) {
1486 address
[count
++] = filler
;
1487 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1488 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1489 address
[count
] = address
[count
- 1];
1490 address
[count
- 1] = filler
;
1495 /* Pack LOD or sample index */
1496 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1497 address
[count
++] = coords
[3];
1498 else if (opcode
== TGSI_OPCODE_TXL2
)
1499 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1502 assert(!"Cannot handle more than 16 texture address parameters");
1506 for (chan
= 0; chan
< count
; chan
++)
1507 address
[chan
] = ac_to_integer(&ctx
->ac
, address
[chan
]);
1509 /* Adjust the sample index according to FMASK.
1511 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1512 * which is the identity mapping. Each nibble says which physical sample
1513 * should be fetched to get that sample.
1515 * For example, 0x11111100 means there are only 2 samples stored and
1516 * the second sample covers 3/4 of the pixel. When reading samples 0
1517 * and 1, return physical sample 0 (determined by the first two 0s
1518 * in FMASK), otherwise return physical sample 1.
1520 * The sample index should be adjusted as follows:
1521 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1523 if (target
== TGSI_TEXTURE_2D_MSAA
||
1524 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1525 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1526 LLVMValueRef txf_address
[4];
1527 /* We only need .xy for non-arrays, and .xyz for arrays. */
1528 unsigned txf_count
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1529 struct tgsi_full_instruction inst
= {};
1531 memcpy(txf_address
, address
, sizeof(txf_address
));
1533 /* Read FMASK using TXF_LZ. */
1534 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF_LZ
;
1535 inst
.Texture
.Texture
= target
;
1536 txf_emit_data
.inst
= &inst
;
1537 txf_emit_data
.chan
= 0;
1538 set_tex_fetch_args(ctx
, &txf_emit_data
,
1539 target
, fmask_ptr
, NULL
,
1540 txf_address
, txf_count
, 0xf);
1541 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
1543 /* Initialize some constants. */
1544 LLVMValueRef four
= LLVMConstInt(ctx
->i32
, 4, 0);
1545 LLVMValueRef F
= LLVMConstInt(ctx
->i32
, 0xF, 0);
1547 /* Apply the formula. */
1548 LLVMValueRef fmask
=
1549 LLVMBuildExtractElement(ctx
->ac
.builder
,
1550 txf_emit_data
.output
[0],
1553 unsigned sample_chan
= txf_count
; /* the sample index is last */
1555 LLVMValueRef sample_index4
=
1556 LLVMBuildMul(ctx
->ac
.builder
, address
[sample_chan
], four
, "");
1558 LLVMValueRef shifted_fmask
=
1559 LLVMBuildLShr(ctx
->ac
.builder
, fmask
, sample_index4
, "");
1561 LLVMValueRef final_sample
=
1562 LLVMBuildAnd(ctx
->ac
.builder
, shifted_fmask
, F
, "");
1564 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1565 * resource descriptor is 0 (invalid),
1567 LLVMValueRef fmask_desc
=
1568 LLVMBuildBitCast(ctx
->ac
.builder
, fmask_ptr
,
1571 LLVMValueRef fmask_word1
=
1572 LLVMBuildExtractElement(ctx
->ac
.builder
, fmask_desc
,
1575 LLVMValueRef word1_is_nonzero
=
1576 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
1577 fmask_word1
, ctx
->i32_0
, "");
1579 /* Replace the MSAA sample index. */
1580 address
[sample_chan
] =
1581 LLVMBuildSelect(ctx
->ac
.builder
, word1_is_nonzero
,
1582 final_sample
, address
[sample_chan
], "");
1585 if (opcode
== TGSI_OPCODE_TXF
||
1586 opcode
== TGSI_OPCODE_TXF_LZ
) {
1587 /* add tex offsets */
1588 if (inst
->Texture
.NumOffsets
) {
1589 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1590 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1592 assert(inst
->Texture
.NumOffsets
== 1);
1595 case TGSI_TEXTURE_3D
:
1596 address
[2] = lp_build_add(uint_bld
, address
[2],
1597 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
]);
1599 case TGSI_TEXTURE_2D
:
1600 case TGSI_TEXTURE_SHADOW2D
:
1601 case TGSI_TEXTURE_RECT
:
1602 case TGSI_TEXTURE_SHADOWRECT
:
1603 case TGSI_TEXTURE_2D_ARRAY
:
1604 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1606 lp_build_add(uint_bld
, address
[1],
1607 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
]);
1609 case TGSI_TEXTURE_1D
:
1610 case TGSI_TEXTURE_SHADOW1D
:
1611 case TGSI_TEXTURE_1D_ARRAY
:
1612 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1614 lp_build_add(uint_bld
, address
[0],
1615 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
]);
1617 /* texture offsets do not apply to other texture targets */
1622 if (opcode
== TGSI_OPCODE_TG4
) {
1623 unsigned gather_comp
= 0;
1625 /* DMASK was repurposed for GATHER4. 4 components are always
1626 * returned and DMASK works like a swizzle - it selects
1627 * the component to fetch. The only valid DMASK values are
1628 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1629 * (red,red,red,red) etc.) The ISA document doesn't mention
1633 /* Get the component index from src1.x for Gather4. */
1634 if (!tgsi_is_shadow_target(target
)) {
1635 LLVMValueRef comp_imm
;
1636 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1638 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1640 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1641 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1642 gather_comp
= CLAMP(gather_comp
, 0, 3);
1645 dmask
= 1 << gather_comp
;
1648 set_tex_fetch_args(ctx
, emit_data
, target
, res_ptr
,
1649 samp_ptr
, address
, count
, dmask
);
1652 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1653 * incorrectly forces nearest filtering if the texture format is integer.
1654 * The only effect it has on Gather4, which always returns 4 texels for
1655 * bilinear filtering, is that the final coordinates are off by 0.5 of
1658 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1659 * or (0.5 / size) from the normalized coordinates.
1661 * However, cube textures with 8_8_8_8 data formats require a different
1662 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1663 * precision in 32-bit data formats, so it needs to be applied dynamically at
1664 * runtime. In this case, return an i1 value that indicates whether the
1665 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1668 si_lower_gather4_integer(struct si_shader_context
*ctx
,
1669 struct ac_image_args
*args
,
1671 enum tgsi_return_type return_type
)
1673 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1674 LLVMValueRef wa_8888
= NULL
;
1675 LLVMValueRef coord
= args
->addr
;
1676 LLVMValueRef half_texel
[2];
1677 /* Texture coordinates start after:
1678 * {offset, bias, z-compare, derivatives}
1679 * Only the offset and z-compare can occur here.
1681 unsigned coord_vgpr_index
= (int)args
->offset
+ (int)args
->compare
;
1684 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1685 return_type
== TGSI_RETURN_TYPE_UINT
);
1687 if (target
== TGSI_TEXTURE_CUBE
||
1688 target
== TGSI_TEXTURE_CUBE_ARRAY
) {
1689 LLVMValueRef formats
;
1690 LLVMValueRef data_format
;
1691 LLVMValueRef wa_formats
;
1693 formats
= LLVMBuildExtractElement(builder
, args
->resource
, ctx
->i32_1
, "");
1695 data_format
= LLVMBuildLShr(builder
, formats
,
1696 LLVMConstInt(ctx
->i32
, 20, false), "");
1697 data_format
= LLVMBuildAnd(builder
, data_format
,
1698 LLVMConstInt(ctx
->i32
, (1u << 6) - 1, false), "");
1699 wa_8888
= LLVMBuildICmp(
1700 builder
, LLVMIntEQ
, data_format
,
1701 LLVMConstInt(ctx
->i32
, V_008F14_IMG_DATA_FORMAT_8_8_8_8
, false),
1704 uint32_t wa_num_format
=
1705 return_type
== TGSI_RETURN_TYPE_UINT
?
1706 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1707 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1708 wa_formats
= LLVMBuildAnd(builder
, formats
,
1709 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, false),
1711 wa_formats
= LLVMBuildOr(builder
, wa_formats
,
1712 LLVMConstInt(ctx
->i32
, wa_num_format
, false), "");
1714 formats
= LLVMBuildSelect(builder
, wa_8888
, wa_formats
, formats
, "");
1715 args
->resource
= LLVMBuildInsertElement(
1716 builder
, args
->resource
, formats
, ctx
->i32_1
, "");
1719 if (target
== TGSI_TEXTURE_RECT
||
1720 target
== TGSI_TEXTURE_SHADOWRECT
) {
1722 half_texel
[0] = half_texel
[1] = LLVMConstReal(ctx
->f32
, -0.5);
1724 struct tgsi_full_instruction txq_inst
= {};
1725 struct lp_build_emit_data txq_emit_data
= {};
1726 struct lp_build_if_state if_ctx
;
1729 /* Skip the texture size query entirely if we don't need it. */
1730 lp_build_if(&if_ctx
, &ctx
->gallivm
, LLVMBuildNot(builder
, wa_8888
, ""));
1733 /* Query the texture size. */
1734 txq_inst
.Texture
.Texture
= target
;
1735 txq_emit_data
.inst
= &txq_inst
;
1736 txq_emit_data
.dst_type
= ctx
->v4i32
;
1737 set_tex_fetch_args(ctx
, &txq_emit_data
, target
,
1738 args
->resource
, NULL
, &ctx
->i32_0
,
1740 txq_emit(NULL
, &ctx
->bld_base
, &txq_emit_data
);
1742 /* Compute -0.5 / size. */
1743 for (c
= 0; c
< 2; c
++) {
1745 LLVMBuildExtractElement(builder
, txq_emit_data
.output
[0],
1746 LLVMConstInt(ctx
->i32
, c
, 0), "");
1747 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1749 lp_build_emit_llvm_unary(&ctx
->bld_base
,
1750 TGSI_OPCODE_RCP
, half_texel
[c
]);
1751 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1752 LLVMConstReal(ctx
->f32
, -0.5), "");
1756 lp_build_endif(&if_ctx
);
1758 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1760 for (c
= 0; c
< 2; c
++) {
1761 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1762 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1768 for (c
= 0; c
< 2; c
++) {
1770 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, coord_vgpr_index
+ c
, 0);
1772 tmp
= LLVMBuildExtractElement(builder
, coord
, index
, "");
1773 tmp
= ac_to_float(&ctx
->ac
, tmp
);
1774 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1775 tmp
= ac_to_integer(&ctx
->ac
, tmp
);
1776 coord
= LLVMBuildInsertElement(builder
, coord
, tmp
, index
, "");
1784 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1785 * result after the gather operation.
1788 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1789 LLVMValueRef result
,
1790 enum tgsi_return_type return_type
,
1793 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1795 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1796 return_type
== TGSI_RETURN_TYPE_UINT
);
1798 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1799 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1801 LLVMValueRef wa_value
;
1803 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1805 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1806 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1808 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1809 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1810 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1812 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1818 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1819 struct lp_build_tgsi_context
*bld_base
,
1820 struct lp_build_emit_data
*emit_data
)
1822 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1823 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1824 struct ac_image_args args
;
1825 unsigned opcode
= inst
->Instruction
.Opcode
;
1826 unsigned target
= inst
->Texture
.Texture
;
1828 if (target
== TGSI_TEXTURE_BUFFER
) {
1829 unsigned num_channels
=
1830 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1831 LLVMValueRef result
=
1832 ac_build_buffer_load_format(&ctx
->ac
,
1836 num_channels
, false, true);
1837 emit_data
->output
[emit_data
->chan
] =
1838 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1842 memcpy(&args
, emit_data
->args
, sizeof(args
)); /* ugly */
1844 args
.opcode
= ac_image_sample
;
1845 args
.compare
= tgsi_is_shadow_target(target
);
1846 args
.offset
= inst
->Texture
.NumOffsets
> 0;
1849 case TGSI_OPCODE_TXF
:
1850 case TGSI_OPCODE_TXF_LZ
:
1851 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1852 target
== TGSI_TEXTURE_2D_MSAA
||
1853 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1854 ac_image_load
: ac_image_load_mip
;
1855 args
.compare
= false;
1856 args
.offset
= false;
1858 case TGSI_OPCODE_LODQ
:
1859 args
.opcode
= ac_image_get_lod
;
1860 args
.compare
= false;
1861 args
.offset
= false;
1863 case TGSI_OPCODE_TEX
:
1864 case TGSI_OPCODE_TEX2
:
1865 case TGSI_OPCODE_TXP
:
1866 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1867 args
.level_zero
= true;
1869 case TGSI_OPCODE_TEX_LZ
:
1870 args
.level_zero
= true;
1872 case TGSI_OPCODE_TXB
:
1873 case TGSI_OPCODE_TXB2
:
1874 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1877 case TGSI_OPCODE_TXL
:
1878 case TGSI_OPCODE_TXL2
:
1881 case TGSI_OPCODE_TXD
:
1884 case TGSI_OPCODE_TG4
:
1885 args
.opcode
= ac_image_gather4
;
1886 args
.level_zero
= true;
1893 /* The hardware needs special lowering for Gather4 with integer formats. */
1894 LLVMValueRef gather4_int_result_workaround
= NULL
;
1896 if (ctx
->screen
->info
.chip_class
<= VI
&&
1897 opcode
== TGSI_OPCODE_TG4
) {
1898 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1900 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1901 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1902 gather4_int_result_workaround
=
1903 si_lower_gather4_integer(ctx
, &args
, target
,
1904 inst
->Texture
.ReturnType
);
1908 LLVMValueRef result
=
1909 ac_build_image_opcode(&ctx
->ac
, &args
);
1911 if (gather4_int_result_workaround
) {
1912 result
= si_fix_gather4_integer_result(ctx
, result
,
1913 inst
->Texture
.ReturnType
,
1914 gather4_int_result_workaround
);
1917 emit_data
->output
[emit_data
->chan
] = result
;
1920 static void si_llvm_emit_txqs(
1921 const struct lp_build_tgsi_action
*action
,
1922 struct lp_build_tgsi_context
*bld_base
,
1923 struct lp_build_emit_data
*emit_data
)
1925 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1926 LLVMValueRef res
, samples
;
1927 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
1929 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
1932 /* Read the samples from the descriptor directly. */
1933 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->v8i32
, "");
1934 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1935 LLVMConstInt(ctx
->i32
, 3, 0), "");
1936 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
1937 LLVMConstInt(ctx
->i32
, 16, 0), "");
1938 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
1939 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
1940 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->i32_1
,
1943 emit_data
->output
[emit_data
->chan
] = samples
;
1946 static const struct lp_build_tgsi_action tex_action
= {
1947 .fetch_args
= tex_fetch_args
,
1948 .emit
= build_tex_intrinsic
,
1952 * Setup actions for TGSI memory opcode, including texture opcodes.
1954 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1956 struct lp_build_tgsi_context
*bld_base
;
1957 struct lp_build_tgsi_action tmpl
= {};
1959 bld_base
= &ctx
->bld_base
;
1961 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1962 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
] = tex_action
;
1963 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
1964 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
1965 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
1966 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
1967 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
1968 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
] = tex_action
;
1969 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
1970 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
1971 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1972 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].fetch_args
= txq_fetch_args
;
1973 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= txq_emit
;
1974 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
1975 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
1976 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1978 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].fetch_args
= load_fetch_args
;
1979 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1980 bld_base
->op_actions
[TGSI_OPCODE_STORE
].fetch_args
= store_fetch_args
;
1981 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1982 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].fetch_args
= resq_fetch_args
;
1983 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1985 tmpl
.fetch_args
= atomic_fetch_args
;
1986 tmpl
.emit
= atomic_emit
;
1987 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
1988 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1989 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
1990 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1991 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
1992 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1993 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
1994 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1995 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
1996 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1997 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
1998 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1999 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
2000 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
2001 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
2002 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
2003 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
2004 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
2005 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
2006 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";