2 * Copyright 2017 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "si_shader_internal.h"
28 #include "tgsi/tgsi_build.h"
29 #include "tgsi/tgsi_util.h"
30 #include "ac_llvm_util.h"
32 static void tex_fetch_ptrs(struct lp_build_tgsi_context
*bld_base
,
33 struct lp_build_emit_data
*emit_data
,
34 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
35 LLVMValueRef
*fmask_ptr
);
38 * Given a v8i32 resource descriptor for a buffer, extract the size of the
39 * buffer in number of elements and return it as an i32.
41 static LLVMValueRef
get_buffer_size(
42 struct lp_build_tgsi_context
*bld_base
,
43 LLVMValueRef descriptor
)
45 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
46 LLVMBuilderRef builder
= ctx
->ac
.builder
;
48 LLVMBuildExtractElement(builder
, descriptor
,
49 LLVMConstInt(ctx
->i32
, 2, 0), "");
51 if (ctx
->screen
->info
.chip_class
== VI
) {
52 /* On VI, the descriptor contains the size in bytes,
53 * but TXQ must return the size in elements.
54 * The stride is always non-zero for resources using TXQ.
57 LLVMBuildExtractElement(builder
, descriptor
,
59 stride
= LLVMBuildLShr(builder
, stride
,
60 LLVMConstInt(ctx
->i32
, 16, 0), "");
61 stride
= LLVMBuildAnd(builder
, stride
,
62 LLVMConstInt(ctx
->i32
, 0x3FFF, 0), "");
64 size
= LLVMBuildUDiv(builder
, size
, stride
, "");
71 shader_buffer_fetch_rsrc(struct si_shader_context
*ctx
,
72 const struct tgsi_full_src_register
*reg
,
77 if (!reg
->Register
.Indirect
) {
78 index
= LLVMConstInt(ctx
->i32
, reg
->Register
.Index
, false);
80 index
= si_get_indirect_index(ctx
, ®
->Indirect
,
81 1, reg
->Register
.Index
);
85 return ctx
->abi
.load_ubo(&ctx
->abi
, index
);
87 return ctx
->abi
.load_ssbo(&ctx
->abi
, index
, false);
90 static enum ac_image_dim
91 ac_texture_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
95 case TGSI_TEXTURE_SHADOW1D
:
96 if (screen
->info
.chip_class
>= GFX9
)
100 case TGSI_TEXTURE_SHADOW2D
:
101 case TGSI_TEXTURE_RECT
:
102 case TGSI_TEXTURE_SHADOWRECT
:
104 case TGSI_TEXTURE_3D
:
106 case TGSI_TEXTURE_CUBE
:
107 case TGSI_TEXTURE_SHADOWCUBE
:
108 case TGSI_TEXTURE_CUBE_ARRAY
:
109 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
110 return ac_image_cube
;
111 case TGSI_TEXTURE_1D_ARRAY
:
112 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
113 if (screen
->info
.chip_class
>= GFX9
)
114 return ac_image_2darray
;
115 return ac_image_1darray
;
116 case TGSI_TEXTURE_2D_ARRAY
:
117 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
118 return ac_image_2darray
;
119 case TGSI_TEXTURE_2D_MSAA
:
120 return ac_image_2dmsaa
;
121 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
122 return ac_image_2darraymsaa
;
124 unreachable("unhandled texture type");
128 static enum ac_image_dim
129 ac_image_dim_from_tgsi_target(struct si_screen
*screen
, enum tgsi_texture_type target
)
131 enum ac_image_dim dim
= ac_texture_dim_from_tgsi_target(screen
, target
);
133 /* Match the resource type set in the descriptor. */
134 if (dim
== ac_image_cube
||
135 (screen
->info
.chip_class
<= VI
&& dim
== ac_image_3d
))
136 dim
= ac_image_2darray
;
137 else if (target
== TGSI_TEXTURE_2D
&& screen
->info
.chip_class
>= GFX9
) {
138 /* When a single layer of a 3D texture is bound, the shader
139 * will refer to a 2D target, but the descriptor has a 3D type.
140 * Since the HW ignores BASE_ARRAY in this case, we need to
141 * send 3 coordinates. This doesn't hurt when the underlying
151 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
153 * At least on Tonga, executing image stores on images with DCC enabled and
154 * non-trivial can eventually lead to lockups. This can occur when an
155 * application binds an image as read-only but then uses a shader that writes
156 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
157 * program termination) in this case, but it doesn't cost much to be a bit
158 * nicer: disabling DCC in the shader still leads to undefined results but
161 static LLVMValueRef
force_dcc_off(struct si_shader_context
*ctx
,
164 if (ctx
->screen
->info
.chip_class
<= CIK
) {
167 LLVMValueRef i32_6
= LLVMConstInt(ctx
->i32
, 6, 0);
168 LLVMValueRef i32_C
= LLVMConstInt(ctx
->i32
, C_008F28_COMPRESSION_EN
, 0);
171 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, rsrc
, i32_6
, "");
172 tmp
= LLVMBuildAnd(ctx
->ac
.builder
, tmp
, i32_C
, "");
173 return LLVMBuildInsertElement(ctx
->ac
.builder
, rsrc
, tmp
, i32_6
, "");
177 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
178 LLVMValueRef list
, LLVMValueRef index
,
179 enum ac_descriptor_type desc_type
, bool dcc_off
)
181 LLVMBuilderRef builder
= ctx
->ac
.builder
;
184 if (desc_type
== AC_DESC_BUFFER
) {
185 index
= LLVMBuildMul(builder
, index
,
186 LLVMConstInt(ctx
->i32
, 2, 0), "");
187 index
= LLVMBuildAdd(builder
, index
,
189 list
= LLVMBuildPointerCast(builder
, list
,
190 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
192 assert(desc_type
== AC_DESC_IMAGE
);
195 rsrc
= ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
196 if (desc_type
== AC_DESC_IMAGE
&& dcc_off
)
197 rsrc
= force_dcc_off(ctx
, rsrc
);
202 * Load the resource descriptor for \p image.
206 struct lp_build_tgsi_context
*bld_base
,
207 const struct tgsi_full_src_register
*image
,
208 bool is_store
, unsigned target
,
211 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
212 LLVMValueRef rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
213 ctx
->param_samplers_and_images
);
215 bool dcc_off
= is_store
;
217 if (!image
->Register
.Indirect
) {
218 const struct tgsi_shader_info
*info
= bld_base
->info
;
219 unsigned images_writemask
= info
->images_store
|
222 index
= LLVMConstInt(ctx
->i32
,
223 si_get_image_slot(image
->Register
.Index
), 0);
225 if (images_writemask
& (1 << image
->Register
.Index
))
228 /* From the GL_ARB_shader_image_load_store extension spec:
230 * If a shader performs an image load, store, or atomic
231 * operation using an image variable declared as an array,
232 * and if the index used to select an individual element is
233 * negative or greater than or equal to the size of the
234 * array, the results of the operation are undefined but may
235 * not lead to termination.
237 index
= si_get_bounded_indirect_index(ctx
, &image
->Indirect
,
238 image
->Register
.Index
,
240 index
= LLVMBuildSub(ctx
->ac
.builder
,
241 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
- 1, 0),
245 if (image
->Register
.File
!= TGSI_FILE_IMAGE
) {
246 /* Bindless descriptors are accessible from a different pair of
249 rsrc_ptr
= LLVMGetParam(ctx
->main_fn
,
250 ctx
->param_bindless_samplers_and_images
);
251 index
= lp_build_emit_fetch_src(bld_base
, image
,
252 TGSI_TYPE_UNSIGNED
, 0);
254 /* For simplicity, bindless image descriptors use fixed
255 * 16-dword slots for now.
257 index
= LLVMBuildMul(ctx
->ac
.builder
, index
,
258 LLVMConstInt(ctx
->i32
, 2, 0), "");
261 *rsrc
= si_load_image_desc(ctx
, rsrc_ptr
, index
,
262 target
== TGSI_TEXTURE_BUFFER
? AC_DESC_BUFFER
: AC_DESC_IMAGE
,
266 static void image_fetch_coords(
267 struct lp_build_tgsi_context
*bld_base
,
268 const struct tgsi_full_instruction
*inst
,
269 unsigned src
, LLVMValueRef desc
,
270 LLVMValueRef
*coords
)
272 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
273 LLVMBuilderRef builder
= ctx
->ac
.builder
;
274 unsigned target
= inst
->Memory
.Texture
;
275 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
);
279 if (target
== TGSI_TEXTURE_2D_MSAA
||
280 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
281 /* Need the sample index as well. */
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 (ctx
->screen
->info
.chip_class
>= GFX9
) {
292 /* 1D textures are allocated and used as 2D on GFX9. */
293 if (target
== TGSI_TEXTURE_1D
) {
294 coords
[1] = ctx
->i32_0
;
295 } else if (target
== TGSI_TEXTURE_1D_ARRAY
) {
296 coords
[2] = coords
[1];
297 coords
[1] = ctx
->i32_0
;
298 } else if (target
== TGSI_TEXTURE_2D
) {
299 /* The hw can't bind a slice of a 3D image as a 2D
300 * image, because it ignores BASE_ARRAY if the target
301 * is 3D. The workaround is to read BASE_ARRAY and set
302 * it as the 3rd address operand for all 2D images.
304 LLVMValueRef first_layer
, const5
, mask
;
306 const5
= LLVMConstInt(ctx
->i32
, 5, 0);
307 mask
= LLVMConstInt(ctx
->i32
, S_008F24_BASE_ARRAY(~0), 0);
308 first_layer
= LLVMBuildExtractElement(builder
, desc
, const5
, "");
309 first_layer
= LLVMBuildAnd(builder
, first_layer
, mask
, "");
311 coords
[2] = first_layer
;
317 * Append the resource and indexing arguments for buffer intrinsics.
319 * \param rsrc the v4i32 buffer resource
320 * \param index index into the buffer (stride-based)
321 * \param offset byte offset into the buffer
323 static void buffer_append_args(
324 struct si_shader_context
*ctx
,
325 struct lp_build_emit_data
*emit_data
,
332 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
333 LLVMValueRef i1false
= LLVMConstInt(ctx
->i1
, 0, 0);
334 LLVMValueRef i1true
= LLVMConstInt(ctx
->i1
, 1, 0);
336 emit_data
->args
[emit_data
->arg_count
++] = rsrc
;
337 emit_data
->args
[emit_data
->arg_count
++] = index
; /* vindex */
338 emit_data
->args
[emit_data
->arg_count
++] = offset
; /* voffset */
340 emit_data
->args
[emit_data
->arg_count
++] =
342 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
) ?
343 i1true
: i1false
; /* glc */
345 emit_data
->args
[emit_data
->arg_count
++] = i1false
; /* slc */
348 static void load_emit_buffer(struct si_shader_context
*ctx
,
349 struct lp_build_emit_data
*emit_data
,
350 bool can_speculate
, bool allow_smem
)
352 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
353 uint writemask
= inst
->Dst
[0].Register
.WriteMask
;
354 uint count
= util_last_bit(writemask
);
355 LLVMValueRef
*args
= emit_data
->args
;
357 /* Don't use SMEM for shader buffer loads, because LLVM doesn't
358 * select SMEM for SI.load.const with a non-constant offset, and
359 * constant offsets practically don't exist with shader buffers.
361 * Also, SI.load.const doesn't use inst_offset when it's lowered
362 * to VMEM, so we just end up with more VALU instructions in the end
365 * TODO: Remove this line once LLVM can select SMEM with a non-constant
366 * offset, and can derive inst_offset when VMEM is selected.
367 * After that, si_memory_barrier should invalidate sL1 for shader
371 assert(LLVMConstIntGetZExtValue(args
[1]) == 0); /* vindex */
372 emit_data
->output
[emit_data
->chan
] =
373 ac_build_buffer_load(&ctx
->ac
, args
[0], count
, NULL
,
375 LLVMConstIntGetZExtValue(args
[3]),
376 LLVMConstIntGetZExtValue(args
[4]),
377 can_speculate
, allow_smem
);
380 static LLVMValueRef
get_memory_ptr(struct si_shader_context
*ctx
,
381 const struct tgsi_full_instruction
*inst
,
382 LLVMTypeRef type
, int arg
)
384 LLVMBuilderRef builder
= ctx
->ac
.builder
;
385 LLVMValueRef offset
, ptr
;
388 offset
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, arg
, 0);
389 offset
= ac_to_integer(&ctx
->ac
, offset
);
392 ptr
= LLVMBuildGEP(builder
, ptr
, &offset
, 1, "");
393 addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
394 ptr
= LLVMBuildBitCast(builder
, ptr
, LLVMPointerType(type
, addr_space
), "");
399 static void load_emit_memory(
400 struct si_shader_context
*ctx
,
401 struct lp_build_emit_data
*emit_data
)
403 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
404 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
405 LLVMValueRef channels
[4], ptr
, derived_ptr
, index
;
408 ptr
= get_memory_ptr(ctx
, inst
, ctx
->f32
, 1);
410 for (chan
= 0; chan
< 4; ++chan
) {
411 if (!(writemask
& (1 << chan
))) {
412 channels
[chan
] = LLVMGetUndef(ctx
->f32
);
416 index
= LLVMConstInt(ctx
->i32
, chan
, 0);
417 derived_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ptr
, &index
, 1, "");
418 channels
[chan
] = LLVMBuildLoad(ctx
->ac
.builder
, derived_ptr
, "");
420 emit_data
->output
[emit_data
->chan
] = ac_build_gather_values(&ctx
->ac
, channels
, 4);
424 * Return true if the memory accessed by a LOAD or STORE instruction is
425 * read-only or write-only, respectively.
427 * \param shader_buffers_reverse_access_mask
428 * For LOAD, set this to (store | atomic) slot usage in the shader.
429 * For STORE, set this to (load | atomic) slot usage in the shader.
430 * \param images_reverse_access_mask Same as above, but for images.
432 static bool is_oneway_access_only(const struct tgsi_full_instruction
*inst
,
433 const struct tgsi_shader_info
*info
,
434 unsigned shader_buffers_reverse_access_mask
,
435 unsigned images_reverse_access_mask
)
437 /* RESTRICT means NOALIAS.
438 * If there are no writes, we can assume the accessed memory is read-only.
439 * If there are no reads, we can assume the accessed memory is write-only.
441 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_RESTRICT
) {
442 unsigned reverse_access_mask
;
444 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
445 reverse_access_mask
= shader_buffers_reverse_access_mask
;
446 } else if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
447 reverse_access_mask
= info
->images_buffers
&
448 images_reverse_access_mask
;
450 reverse_access_mask
= ~info
->images_buffers
&
451 images_reverse_access_mask
;
454 if (inst
->Src
[0].Register
.Indirect
) {
455 if (!reverse_access_mask
)
458 if (!(reverse_access_mask
&
459 (1u << inst
->Src
[0].Register
.Index
)))
464 /* If there are no buffer writes (for both shader buffers & image
465 * buffers), it implies that buffer memory is read-only.
466 * If there are no buffer reads (for both shader buffers & image
467 * buffers), it implies that buffer memory is write-only.
469 * Same for the case when there are no writes/reads for non-buffer
472 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
473 (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
&&
474 (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
475 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)))) {
476 if (!shader_buffers_reverse_access_mask
&&
477 !(info
->images_buffers
& images_reverse_access_mask
))
480 if (!(~info
->images_buffers
& images_reverse_access_mask
))
486 static void load_emit(
487 const struct lp_build_tgsi_action
*action
,
488 struct lp_build_tgsi_context
*bld_base
,
489 struct lp_build_emit_data
*emit_data
)
491 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
492 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
493 const struct tgsi_shader_info
*info
= &ctx
->shader
->selector
->info
;
494 bool can_speculate
= false;
496 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
497 load_emit_memory(ctx
, emit_data
);
501 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
502 inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
503 LLVMValueRef offset
, tmp
, rsrc
;
505 bool ubo
= inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
;
506 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], ubo
);
508 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
509 offset
= ac_to_integer(&ctx
->ac
, tmp
);
511 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
512 offset
, false, false);
513 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
514 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
516 unsigned target
= inst
->Memory
.Texture
;
518 image_fetch_rsrc(bld_base
, &inst
->Src
[0], false, target
, &rsrc
);
519 image_fetch_coords(bld_base
, inst
, 1, rsrc
, &emit_data
->args
[1]);
521 if (target
== TGSI_TEXTURE_BUFFER
) {
522 buffer_append_args(ctx
, emit_data
, rsrc
, emit_data
->args
[1],
523 ctx
->i32_0
, false, false);
525 emit_data
->args
[0] = rsrc
;
529 if (inst
->Src
[0].Register
.File
== TGSI_FILE_CONSTBUF
) {
530 load_emit_buffer(ctx
, emit_data
, true, true);
534 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
535 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
537 can_speculate
= !(inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
) &&
538 is_oneway_access_only(inst
, info
,
539 info
->shader_buffers_store
|
540 info
->shader_buffers_atomic
,
542 info
->images_atomic
);
544 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
545 load_emit_buffer(ctx
, emit_data
, can_speculate
, false);
549 if (inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
550 unsigned num_channels
= util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
551 LLVMValueRef result
=
552 ac_build_buffer_load_format(&ctx
->ac
,
557 LLVMConstIntGetZExtValue(emit_data
->args
[3]),
559 emit_data
->output
[emit_data
->chan
] =
560 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
562 struct ac_image_args args
= {};
563 args
.opcode
= ac_image_load
;
564 args
.resource
= emit_data
->args
[0];
565 memcpy(args
.coords
, &emit_data
->args
[1], sizeof(args
.coords
));
566 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
567 if (inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
))
568 args
.cache_policy
= ac_glc
;
569 args
.attributes
= ac_get_load_intr_attribs(can_speculate
);
572 emit_data
->output
[emit_data
->chan
] =
573 ac_build_image_opcode(&ctx
->ac
, &args
);
577 static void store_emit_buffer(
578 struct si_shader_context
*ctx
,
579 struct lp_build_emit_data
*emit_data
,
580 bool writeonly_memory
)
582 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
583 LLVMBuilderRef builder
= ctx
->ac
.builder
;
584 LLVMValueRef base_data
= emit_data
->args
[0];
585 LLVMValueRef base_offset
= emit_data
->args
[3];
586 unsigned writemask
= inst
->Dst
[0].Register
.WriteMask
;
588 /* If this is write-only, don't keep data in L1 to prevent
589 * evicting L1 cache lines that may be needed by other
592 if (writeonly_memory
)
593 emit_data
->args
[4] = LLVMConstInt(ctx
->i1
, 1, 0); /* GLC = 1 */
597 const char *intrinsic_name
;
602 u_bit_scan_consecutive_range(&writemask
, &start
, &count
);
604 /* Due to an LLVM limitation, split 3-element writes
605 * into a 2-element and a 1-element write. */
607 writemask
|= 1 << (start
+ 2);
613 intrinsic_name
= "llvm.amdgcn.buffer.store.v4f32";
614 } else if (count
== 2) {
615 LLVMTypeRef v2f32
= LLVMVectorType(ctx
->f32
, 2);
617 tmp
= LLVMBuildExtractElement(
619 LLVMConstInt(ctx
->i32
, start
, 0), "");
620 data
= LLVMBuildInsertElement(
621 builder
, LLVMGetUndef(v2f32
), tmp
,
624 tmp
= LLVMBuildExtractElement(
626 LLVMConstInt(ctx
->i32
, start
+ 1, 0), "");
627 data
= LLVMBuildInsertElement(
628 builder
, data
, tmp
, ctx
->i32_1
, "");
630 intrinsic_name
= "llvm.amdgcn.buffer.store.v2f32";
633 data
= LLVMBuildExtractElement(
635 LLVMConstInt(ctx
->i32
, start
, 0), "");
636 intrinsic_name
= "llvm.amdgcn.buffer.store.f32";
639 offset
= base_offset
;
641 offset
= LLVMBuildAdd(
643 LLVMConstInt(ctx
->i32
, start
* 4, 0), "");
646 emit_data
->args
[0] = data
;
647 emit_data
->args
[3] = offset
;
650 &ctx
->ac
, intrinsic_name
, ctx
->voidt
,
651 emit_data
->args
, emit_data
->arg_count
,
652 ac_get_store_intr_attribs(writeonly_memory
));
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
;
690 bool writeonly_memory
= false;
691 LLVMValueRef chans
[4], rsrc
;
693 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_MEMORY
) {
694 store_emit_memory(ctx
, emit_data
);
698 for (unsigned chan
= 0; chan
< 4; ++chan
)
699 chans
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
701 emit_data
->args
[emit_data
->arg_count
++] =
702 ac_build_gather_values(&ctx
->ac
, chans
, 4);
704 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
705 LLVMValueRef offset
, tmp
;
707 rsrc
= shader_buffer_fetch_rsrc(ctx
, &resource_reg
, false);
709 tmp
= lp_build_emit_fetch(bld_base
, inst
, 0, 0);
710 offset
= ac_to_integer(&ctx
->ac
, tmp
);
712 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
713 offset
, false, false);
714 } else if (inst
->Dst
[0].Register
.File
== TGSI_FILE_IMAGE
||
715 tgsi_is_bindless_image_file(inst
->Dst
[0].Register
.File
)) {
716 /* 8bit/16bit TC L1 write corruption bug on SI.
717 * All store opcodes not aligned to a dword are affected.
719 * The only way to get unaligned stores in radeonsi is through
722 bool force_glc
= ctx
->screen
->info
.chip_class
== SI
;
724 image_fetch_rsrc(bld_base
, &resource_reg
, true, target
, &rsrc
);
725 image_fetch_coords(bld_base
, inst
, 0, rsrc
, &emit_data
->args
[2]);
727 if (target
== TGSI_TEXTURE_BUFFER
) {
728 buffer_append_args(ctx
, emit_data
, rsrc
, emit_data
->args
[2],
729 ctx
->i32_0
, false, force_glc
);
731 emit_data
->args
[1] = rsrc
;
735 if (inst
->Memory
.Qualifier
& TGSI_MEMORY_VOLATILE
)
736 ac_build_waitcnt(&ctx
->ac
, VM_CNT
);
738 writeonly_memory
= is_oneway_access_only(inst
, info
,
739 info
->shader_buffers_load
|
740 info
->shader_buffers_atomic
,
742 info
->images_atomic
);
744 if (inst
->Dst
[0].Register
.File
== TGSI_FILE_BUFFER
) {
745 store_emit_buffer(ctx
, emit_data
, writeonly_memory
);
749 if (target
== TGSI_TEXTURE_BUFFER
) {
750 /* If this is write-only, don't keep data in L1 to prevent
751 * evicting L1 cache lines that may be needed by other
754 if (writeonly_memory
)
755 emit_data
->args
[4] = LLVMConstInt(ctx
->i1
, 1, 0); /* GLC = 1 */
757 emit_data
->output
[emit_data
->chan
] = ac_build_intrinsic(
758 &ctx
->ac
, "llvm.amdgcn.buffer.store.format.v4f32",
759 ctx
->voidt
, emit_data
->args
,
760 emit_data
->arg_count
,
761 ac_get_store_intr_attribs(writeonly_memory
));
763 struct ac_image_args args
= {};
764 args
.opcode
= ac_image_store
;
765 args
.data
[0] = emit_data
->args
[0];
766 args
.resource
= emit_data
->args
[1];
767 memcpy(args
.coords
, &emit_data
->args
[2], sizeof(args
.coords
));
768 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
769 args
.attributes
= ac_get_store_intr_attribs(writeonly_memory
);
772 /* Workaround for 8bit/16bit TC L1 write corruption bug on SI.
773 * All store opcodes not aligned to a dword are affected.
775 if (ctx
->screen
->info
.chip_class
== SI
||
776 /* If this is write-only, don't keep data in L1 to prevent
777 * evicting L1 cache lines that may be needed by other
780 inst
->Memory
.Qualifier
& (TGSI_MEMORY_COHERENT
| TGSI_MEMORY_VOLATILE
))
781 args
.cache_policy
= ac_glc
;
783 emit_data
->output
[emit_data
->chan
] =
784 ac_build_image_opcode(&ctx
->ac
, &args
);
788 static void atomic_fetch_args(
789 struct lp_build_tgsi_context
* bld_base
,
790 struct lp_build_emit_data
* emit_data
)
792 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
793 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
794 LLVMValueRef data1
, data2
;
798 emit_data
->dst_type
= ctx
->f32
;
800 tmp
= lp_build_emit_fetch(bld_base
, inst
, 2, 0);
801 data1
= ac_to_integer(&ctx
->ac
, tmp
);
803 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
804 tmp
= lp_build_emit_fetch(bld_base
, inst
, 3, 0);
805 data2
= ac_to_integer(&ctx
->ac
, tmp
);
808 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
809 * of arguments, which is reversed relative to TGSI (and GLSL)
811 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
)
812 emit_data
->args
[emit_data
->arg_count
++] = data2
;
813 emit_data
->args
[emit_data
->arg_count
++] = data1
;
815 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
) {
818 rsrc
= shader_buffer_fetch_rsrc(ctx
, &inst
->Src
[0], false);
820 tmp
= lp_build_emit_fetch(bld_base
, inst
, 1, 0);
821 offset
= ac_to_integer(&ctx
->ac
, tmp
);
823 buffer_append_args(ctx
, emit_data
, rsrc
, ctx
->i32_0
,
824 offset
, true, false);
825 } else if (inst
->Src
[0].Register
.File
== TGSI_FILE_IMAGE
||
826 tgsi_is_bindless_image_file(inst
->Src
[0].Register
.File
)) {
827 unsigned target
= inst
->Memory
.Texture
;
829 image_fetch_rsrc(bld_base
, &inst
->Src
[0], true, target
, &rsrc
);
830 image_fetch_coords(bld_base
, inst
, 1, rsrc
,
831 &emit_data
->args
[emit_data
->arg_count
+ 1]);
833 if (target
== TGSI_TEXTURE_BUFFER
) {
834 buffer_append_args(ctx
, emit_data
, rsrc
,
835 emit_data
->args
[emit_data
->arg_count
+ 1],
836 ctx
->i32_0
, true, false);
838 emit_data
->args
[emit_data
->arg_count
] = rsrc
;
843 static void atomic_emit_memory(struct si_shader_context
*ctx
,
844 struct lp_build_emit_data
*emit_data
) {
845 LLVMBuilderRef builder
= ctx
->ac
.builder
;
846 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
847 LLVMValueRef ptr
, result
, arg
;
849 ptr
= get_memory_ptr(ctx
, inst
, ctx
->i32
, 1);
851 arg
= lp_build_emit_fetch(&ctx
->bld_base
, inst
, 2, 0);
852 arg
= ac_to_integer(&ctx
->ac
, arg
);
854 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
855 LLVMValueRef new_data
;
856 new_data
= lp_build_emit_fetch(&ctx
->bld_base
,
859 new_data
= ac_to_integer(&ctx
->ac
, new_data
);
861 result
= LLVMBuildAtomicCmpXchg(builder
, ptr
, arg
, new_data
,
862 LLVMAtomicOrderingSequentiallyConsistent
,
863 LLVMAtomicOrderingSequentiallyConsistent
,
866 result
= LLVMBuildExtractValue(builder
, result
, 0, "");
868 LLVMAtomicRMWBinOp op
;
870 switch(inst
->Instruction
.Opcode
) {
871 case TGSI_OPCODE_ATOMUADD
:
872 op
= LLVMAtomicRMWBinOpAdd
;
874 case TGSI_OPCODE_ATOMXCHG
:
875 op
= LLVMAtomicRMWBinOpXchg
;
877 case TGSI_OPCODE_ATOMAND
:
878 op
= LLVMAtomicRMWBinOpAnd
;
880 case TGSI_OPCODE_ATOMOR
:
881 op
= LLVMAtomicRMWBinOpOr
;
883 case TGSI_OPCODE_ATOMXOR
:
884 op
= LLVMAtomicRMWBinOpXor
;
886 case TGSI_OPCODE_ATOMUMIN
:
887 op
= LLVMAtomicRMWBinOpUMin
;
889 case TGSI_OPCODE_ATOMUMAX
:
890 op
= LLVMAtomicRMWBinOpUMax
;
892 case TGSI_OPCODE_ATOMIMIN
:
893 op
= LLVMAtomicRMWBinOpMin
;
895 case TGSI_OPCODE_ATOMIMAX
:
896 op
= LLVMAtomicRMWBinOpMax
;
899 unreachable("unknown atomic opcode");
902 result
= LLVMBuildAtomicRMW(builder
, op
, ptr
, arg
,
903 LLVMAtomicOrderingSequentiallyConsistent
,
906 emit_data
->output
[emit_data
->chan
] = LLVMBuildBitCast(builder
, result
, emit_data
->dst_type
, "");
909 static void atomic_emit(
910 const struct lp_build_tgsi_action
*action
,
911 struct lp_build_tgsi_context
*bld_base
,
912 struct lp_build_emit_data
*emit_data
)
914 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
915 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
918 if (inst
->Src
[0].Register
.File
== TGSI_FILE_MEMORY
) {
919 atomic_emit_memory(ctx
, emit_data
);
923 if (inst
->Src
[0].Register
.File
== TGSI_FILE_BUFFER
||
924 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
925 char intrinsic_name
[40];
926 snprintf(intrinsic_name
, sizeof(intrinsic_name
),
927 "llvm.amdgcn.buffer.atomic.%s", action
->intr_name
);
928 tmp
= ac_build_intrinsic(
929 &ctx
->ac
, intrinsic_name
, ctx
->i32
,
930 emit_data
->args
, emit_data
->arg_count
, 0);
931 emit_data
->output
[emit_data
->chan
] = ac_to_float(&ctx
->ac
, tmp
);
933 unsigned num_data
= inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
? 2 : 1;
934 struct ac_image_args args
= {};
936 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ATOMCAS
) {
937 args
.opcode
= ac_image_atomic_cmpswap
;
939 args
.opcode
= ac_image_atomic
;
940 switch (inst
->Instruction
.Opcode
) {
941 case TGSI_OPCODE_ATOMXCHG
: args
.atomic
= ac_atomic_swap
; break;
942 case TGSI_OPCODE_ATOMUADD
: args
.atomic
= ac_atomic_add
; break;
943 case TGSI_OPCODE_ATOMAND
: args
.atomic
= ac_atomic_and
; break;
944 case TGSI_OPCODE_ATOMOR
: args
.atomic
= ac_atomic_or
; break;
945 case TGSI_OPCODE_ATOMXOR
: args
.atomic
= ac_atomic_xor
; break;
946 case TGSI_OPCODE_ATOMUMIN
: args
.atomic
= ac_atomic_umin
; break;
947 case TGSI_OPCODE_ATOMUMAX
: args
.atomic
= ac_atomic_umax
; break;
948 case TGSI_OPCODE_ATOMIMIN
: args
.atomic
= ac_atomic_smin
; break;
949 case TGSI_OPCODE_ATOMIMAX
: args
.atomic
= ac_atomic_smax
; break;
950 default: unreachable("unhandled image atomic");
954 for (unsigned i
= 0; i
< num_data
; ++i
)
955 args
.data
[i
] = emit_data
->args
[i
];
957 args
.resource
= emit_data
->args
[num_data
];
958 memcpy(args
.coords
, &emit_data
->args
[num_data
+ 1], sizeof(args
.coords
));
959 args
.dim
= ac_image_dim_from_tgsi_target(ctx
->screen
, inst
->Memory
.Texture
);
961 emit_data
->output
[emit_data
->chan
] =
962 ac_to_float(&ctx
->ac
, ac_build_image_opcode(&ctx
->ac
, &args
));
966 static LLVMValueRef
fix_resinfo(struct si_shader_context
*ctx
,
967 unsigned target
, LLVMValueRef out
)
969 LLVMBuilderRef builder
= ctx
->ac
.builder
;
971 /* 1D textures are allocated and used as 2D on GFX9. */
972 if (ctx
->screen
->info
.chip_class
>= GFX9
&&
973 (target
== TGSI_TEXTURE_1D_ARRAY
||
974 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
)) {
975 LLVMValueRef layers
=
976 LLVMBuildExtractElement(builder
, out
,
977 LLVMConstInt(ctx
->i32
, 2, 0), "");
978 out
= LLVMBuildInsertElement(builder
, out
, layers
,
982 /* Divide the number of layers by 6 to get the number of cubes. */
983 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
984 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
985 LLVMValueRef imm2
= LLVMConstInt(ctx
->i32
, 2, 0);
987 LLVMValueRef z
= LLVMBuildExtractElement(builder
, out
, imm2
, "");
988 z
= LLVMBuildSDiv(builder
, z
, LLVMConstInt(ctx
->i32
, 6, 0), "");
990 out
= LLVMBuildInsertElement(builder
, out
, z
, imm2
, "");
995 static void resq_emit(
996 const struct lp_build_tgsi_action
*action
,
997 struct lp_build_tgsi_context
*bld_base
,
998 struct lp_build_emit_data
*emit_data
)
1000 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1001 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1002 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1003 const struct tgsi_full_src_register
*reg
=
1004 &inst
->Src
[inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
? 1 : 0];
1006 if (reg
->Register
.File
== TGSI_FILE_BUFFER
) {
1007 LLVMValueRef rsrc
= shader_buffer_fetch_rsrc(ctx
, reg
, false);
1009 emit_data
->output
[emit_data
->chan
] =
1010 LLVMBuildExtractElement(builder
, rsrc
,
1011 LLVMConstInt(ctx
->i32
, 2, 0), "");
1015 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
&&
1016 inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1019 tex_fetch_ptrs(bld_base
, emit_data
, &rsrc
, NULL
, NULL
);
1020 /* Read the size from the buffer descriptor directly. */
1021 emit_data
->output
[emit_data
->chan
] =
1022 get_buffer_size(bld_base
, rsrc
);
1026 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_RESQ
&&
1027 inst
->Memory
.Texture
== TGSI_TEXTURE_BUFFER
) {
1030 image_fetch_rsrc(bld_base
, reg
, false, inst
->Memory
.Texture
, &rsrc
);
1031 emit_data
->output
[emit_data
->chan
] =
1032 get_buffer_size(bld_base
, rsrc
);
1038 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1039 target
= inst
->Texture
.Texture
;
1041 if (inst
->Memory
.Texture
== TGSI_TEXTURE_3D
)
1042 target
= TGSI_TEXTURE_2D_ARRAY
;
1044 target
= inst
->Memory
.Texture
;
1047 struct ac_image_args args
= {};
1048 args
.opcode
= ac_image_get_resinfo
;
1049 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1052 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_TXQ
) {
1053 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, NULL
, NULL
);
1054 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1056 image_fetch_rsrc(bld_base
, reg
, false, target
, &args
.resource
);
1057 args
.lod
= ctx
->i32_0
;
1060 emit_data
->output
[emit_data
->chan
] =
1061 fix_resinfo(ctx
, target
, ac_build_image_opcode(&ctx
->ac
, &args
));
1065 * Load an image view, fmask view. or sampler state descriptor.
1067 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
1068 LLVMValueRef list
, LLVMValueRef index
,
1069 enum ac_descriptor_type type
)
1071 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1075 /* The image is at [0:7]. */
1076 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1078 case AC_DESC_BUFFER
:
1079 /* The buffer is in [4:7]. */
1080 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1081 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1082 list
= LLVMBuildPointerCast(builder
, list
,
1083 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1086 /* The FMASK is at [8:15]. */
1087 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1088 index
= LLVMBuildAdd(builder
, index
, ctx
->i32_1
, "");
1090 case AC_DESC_SAMPLER
:
1091 /* The sampler state is at [12:15]. */
1092 index
= LLVMBuildMul(builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
1093 index
= LLVMBuildAdd(builder
, index
, LLVMConstInt(ctx
->i32
, 3, 0), "");
1094 list
= LLVMBuildPointerCast(builder
, list
,
1095 ac_array_in_const32_addr_space(ctx
->v4i32
), "");
1099 return ac_build_load_to_sgpr(&ctx
->ac
, list
, index
);
1102 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1105 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1106 * filtering manually. The driver sets img7 to a mask clearing
1107 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1108 * s_and_b32 samp0, samp0, img7
1111 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1113 static LLVMValueRef
sici_fix_sampler_aniso(struct si_shader_context
*ctx
,
1114 LLVMValueRef res
, LLVMValueRef samp
)
1116 LLVMValueRef img7
, samp0
;
1118 if (ctx
->screen
->info
.chip_class
>= VI
)
1121 img7
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1122 LLVMConstInt(ctx
->i32
, 7, 0), "");
1123 samp0
= LLVMBuildExtractElement(ctx
->ac
.builder
, samp
,
1125 samp0
= LLVMBuildAnd(ctx
->ac
.builder
, samp0
, img7
, "");
1126 return LLVMBuildInsertElement(ctx
->ac
.builder
, samp
, samp0
,
1130 static void tex_fetch_ptrs(struct lp_build_tgsi_context
*bld_base
,
1131 struct lp_build_emit_data
*emit_data
,
1132 LLVMValueRef
*res_ptr
, LLVMValueRef
*samp_ptr
,
1133 LLVMValueRef
*fmask_ptr
)
1135 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1136 LLVMValueRef list
= LLVMGetParam(ctx
->main_fn
, ctx
->param_samplers_and_images
);
1137 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1138 const struct tgsi_full_src_register
*reg
;
1139 unsigned target
= inst
->Texture
.Texture
;
1140 unsigned sampler_src
;
1143 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1144 reg
= &emit_data
->inst
->Src
[sampler_src
];
1146 if (reg
->Register
.Indirect
) {
1147 index
= si_get_bounded_indirect_index(ctx
,
1149 reg
->Register
.Index
,
1151 index
= LLVMBuildAdd(ctx
->ac
.builder
, index
,
1152 LLVMConstInt(ctx
->i32
, SI_NUM_IMAGES
/ 2, 0), "");
1154 index
= LLVMConstInt(ctx
->i32
,
1155 si_get_sampler_slot(reg
->Register
.Index
), 0);
1158 if (reg
->Register
.File
!= TGSI_FILE_SAMPLER
) {
1159 /* Bindless descriptors are accessible from a different pair of
1160 * user SGPR indices.
1162 list
= LLVMGetParam(ctx
->main_fn
,
1163 ctx
->param_bindless_samplers_and_images
);
1164 index
= lp_build_emit_fetch_src(bld_base
, reg
,
1165 TGSI_TYPE_UNSIGNED
, 0);
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_GFX6(V_008F14_IMG_NUM_FORMAT_USCALED
) :
1240 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_SSCALED
);
1241 wa_formats
= LLVMBuildAnd(builder
, formats
,
1242 LLVMConstInt(ctx
->i32
, C_008F14_NUM_FORMAT_GFX6
, 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;
1273 LLVMValueRef texsize
=
1274 fix_resinfo(ctx
, target
,
1275 ac_build_image_opcode(&ctx
->ac
, &resinfo
));
1277 /* Compute -0.5 / size. */
1278 for (unsigned c
= 0; c
< 2; c
++) {
1280 LLVMBuildExtractElement(builder
, texsize
,
1281 LLVMConstInt(ctx
->i32
, c
, 0), "");
1282 half_texel
[c
] = LLVMBuildUIToFP(builder
, half_texel
[c
], ctx
->f32
, "");
1283 half_texel
[c
] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
, half_texel
[c
]);
1284 half_texel
[c
] = LLVMBuildFMul(builder
, half_texel
[c
],
1285 LLVMConstReal(ctx
->f32
, -0.5), "");
1289 lp_build_endif(&if_ctx
);
1291 LLVMBasicBlockRef bb
[2] = { if_ctx
.true_block
, if_ctx
.entry_block
};
1293 for (unsigned c
= 0; c
< 2; c
++) {
1294 LLVMValueRef values
[2] = { half_texel
[c
], ctx
->ac
.f32_0
};
1295 half_texel
[c
] = ac_build_phi(&ctx
->ac
, ctx
->f32
, 2,
1301 for (unsigned c
= 0; c
< 2; c
++) {
1303 tmp
= ac_to_float(&ctx
->ac
, args
->coords
[c
]);
1304 tmp
= LLVMBuildFAdd(builder
, tmp
, half_texel
[c
], "");
1305 args
->coords
[c
] = ac_to_integer(&ctx
->ac
, tmp
);
1311 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1312 * result after the gather operation.
1315 si_fix_gather4_integer_result(struct si_shader_context
*ctx
,
1316 LLVMValueRef result
,
1317 enum tgsi_return_type return_type
,
1320 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1322 assert(return_type
== TGSI_RETURN_TYPE_SINT
||
1323 return_type
== TGSI_RETURN_TYPE_UINT
);
1325 for (unsigned chan
= 0; chan
< 4; ++chan
) {
1326 LLVMValueRef chanv
= LLVMConstInt(ctx
->i32
, chan
, false);
1328 LLVMValueRef wa_value
;
1330 value
= LLVMBuildExtractElement(builder
, result
, chanv
, "");
1332 if (return_type
== TGSI_RETURN_TYPE_UINT
)
1333 wa_value
= LLVMBuildFPToUI(builder
, value
, ctx
->i32
, "");
1335 wa_value
= LLVMBuildFPToSI(builder
, value
, ctx
->i32
, "");
1336 wa_value
= ac_to_float(&ctx
->ac
, wa_value
);
1337 value
= LLVMBuildSelect(builder
, wa
, wa_value
, value
, "");
1339 result
= LLVMBuildInsertElement(builder
, result
, value
, chanv
, "");
1345 static void build_tex_intrinsic(const struct lp_build_tgsi_action
*action
,
1346 struct lp_build_tgsi_context
*bld_base
,
1347 struct lp_build_emit_data
*emit_data
)
1349 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1350 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1351 unsigned opcode
= inst
->Instruction
.Opcode
;
1352 unsigned target
= inst
->Texture
.Texture
;
1353 struct ac_image_args args
= {};
1354 int ref_pos
= tgsi_util_get_shadow_ref_src_index(target
);
1356 bool has_offset
= inst
->Texture
.NumOffsets
> 0;
1357 LLVMValueRef fmask_ptr
= NULL
;
1359 tex_fetch_ptrs(bld_base
, emit_data
, &args
.resource
, &args
.sampler
, &fmask_ptr
);
1361 if (target
== TGSI_TEXTURE_BUFFER
) {
1362 LLVMValueRef vindex
= lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1363 unsigned num_channels
=
1364 util_last_bit(inst
->Dst
[0].Register
.WriteMask
);
1365 LLVMValueRef result
=
1366 ac_build_buffer_load_format(&ctx
->ac
,
1370 num_channels
, false, true);
1371 emit_data
->output
[emit_data
->chan
] =
1372 ac_build_expand_to_vec4(&ctx
->ac
, result
, num_channels
);
1376 /* Fetch and project texture coordinates */
1377 args
.coords
[3] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_W
);
1378 for (chan
= 0; chan
< 3; chan
++) {
1379 args
.coords
[chan
] = lp_build_emit_fetch(bld_base
, inst
, 0, chan
);
1380 if (opcode
== TGSI_OPCODE_TXP
)
1381 args
.coords
[chan
] = ac_build_fdiv(&ctx
->ac
,
1382 args
.coords
[chan
], args
.coords
[3]);
1385 if (opcode
== TGSI_OPCODE_TXP
)
1386 args
.coords
[3] = ctx
->ac
.f32_1
;
1390 opcode
!= TGSI_OPCODE_TXF
&&
1391 opcode
!= TGSI_OPCODE_TXF_LZ
) {
1392 /* The offsets are six-bit signed integers packed like this:
1393 * X=[5:0], Y=[13:8], and Z=[21:16].
1395 LLVMValueRef offset
[3], pack
;
1397 assert(inst
->Texture
.NumOffsets
== 1);
1399 for (chan
= 0; chan
< 3; chan
++) {
1400 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
, inst
, 0, chan
);
1401 offset
[chan
] = LLVMBuildAnd(ctx
->ac
.builder
, offset
[chan
],
1402 LLVMConstInt(ctx
->i32
, 0x3f, 0), "");
1404 offset
[chan
] = LLVMBuildShl(ctx
->ac
.builder
, offset
[chan
],
1405 LLVMConstInt(ctx
->i32
, chan
*8, 0), "");
1408 pack
= LLVMBuildOr(ctx
->ac
.builder
, offset
[0], offset
[1], "");
1409 pack
= LLVMBuildOr(ctx
->ac
.builder
, pack
, offset
[2], "");
1413 /* Pack LOD bias value */
1414 if (opcode
== TGSI_OPCODE_TXB
)
1415 args
.bias
= args
.coords
[3];
1416 if (opcode
== TGSI_OPCODE_TXB2
)
1417 args
.bias
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1419 /* Pack depth comparison value */
1420 if (tgsi_is_shadow_target(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1423 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1424 z
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1426 assert(ref_pos
>= 0);
1427 z
= args
.coords
[ref_pos
];
1430 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1431 * OpenGL 4.5 spec says:
1433 * "If the texture’s internal format indicates a fixed-point
1434 * depth texture, then D_t and D_ref are clamped to the
1435 * range [0, 1]; otherwise no clamping is performed."
1437 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1438 * so the depth comparison value isn't clamped for Z16 and
1439 * Z24 anymore. Do it manually here.
1441 if (ctx
->screen
->info
.chip_class
>= VI
) {
1442 LLVMValueRef upgraded
;
1443 LLVMValueRef clamped
;
1444 upgraded
= LLVMBuildExtractElement(ctx
->ac
.builder
, args
.sampler
,
1445 LLVMConstInt(ctx
->i32
, 3, false), "");
1446 upgraded
= LLVMBuildLShr(ctx
->ac
.builder
, upgraded
,
1447 LLVMConstInt(ctx
->i32
, 29, false), "");
1448 upgraded
= LLVMBuildTrunc(ctx
->ac
.builder
, upgraded
, ctx
->i1
, "");
1449 clamped
= ac_build_clamp(&ctx
->ac
, z
);
1450 z
= LLVMBuildSelect(ctx
->ac
.builder
, upgraded
, clamped
, z
, "");
1456 /* Pack user derivatives */
1457 if (opcode
== TGSI_OPCODE_TXD
) {
1458 int param
, num_src_deriv_channels
, num_dst_deriv_channels
;
1461 case TGSI_TEXTURE_3D
:
1462 num_src_deriv_channels
= 3;
1463 num_dst_deriv_channels
= 3;
1465 case TGSI_TEXTURE_2D
:
1466 case TGSI_TEXTURE_SHADOW2D
:
1467 case TGSI_TEXTURE_RECT
:
1468 case TGSI_TEXTURE_SHADOWRECT
:
1469 case TGSI_TEXTURE_2D_ARRAY
:
1470 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1471 num_src_deriv_channels
= 2;
1472 num_dst_deriv_channels
= 2;
1474 case TGSI_TEXTURE_CUBE
:
1475 case TGSI_TEXTURE_SHADOWCUBE
:
1476 case TGSI_TEXTURE_CUBE_ARRAY
:
1477 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1478 /* Cube derivatives will be converted to 2D. */
1479 num_src_deriv_channels
= 3;
1480 num_dst_deriv_channels
= 3;
1482 case TGSI_TEXTURE_1D
:
1483 case TGSI_TEXTURE_SHADOW1D
:
1484 case TGSI_TEXTURE_1D_ARRAY
:
1485 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1486 num_src_deriv_channels
= 1;
1488 /* 1D textures are allocated and used as 2D on GFX9. */
1489 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1490 num_dst_deriv_channels
= 2;
1492 num_dst_deriv_channels
= 1;
1496 unreachable("invalid target");
1499 for (param
= 0; param
< 2; param
++) {
1500 for (chan
= 0; chan
< num_src_deriv_channels
; chan
++)
1501 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1502 lp_build_emit_fetch(bld_base
, inst
, param
+1, chan
);
1504 /* Fill in the rest with zeros. */
1505 for (chan
= num_src_deriv_channels
;
1506 chan
< num_dst_deriv_channels
; chan
++)
1507 args
.derivs
[param
* num_dst_deriv_channels
+ chan
] =
1512 if (target
== TGSI_TEXTURE_CUBE
||
1513 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1514 target
== TGSI_TEXTURE_SHADOWCUBE
||
1515 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1516 ac_prepare_cube_coords(&ctx
->ac
,
1517 opcode
== TGSI_OPCODE_TXD
,
1518 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1519 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
,
1520 opcode
== TGSI_OPCODE_LODQ
,
1521 args
.coords
, args
.derivs
);
1522 } else if (tgsi_is_array_sampler(target
) &&
1523 opcode
!= TGSI_OPCODE_TXF
&&
1524 opcode
!= TGSI_OPCODE_TXF_LZ
&&
1525 ctx
->screen
->info
.chip_class
<= VI
) {
1526 unsigned array_coord
= target
== TGSI_TEXTURE_1D_ARRAY
? 1 : 2;
1527 args
.coords
[array_coord
] =
1528 ac_build_intrinsic(&ctx
->ac
, "llvm.rint.f32", ctx
->f32
,
1529 &args
.coords
[array_coord
], 1, 0);
1532 /* 1D textures are allocated and used as 2D on GFX9. */
1533 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1534 LLVMValueRef filler
;
1536 /* Use 0.5, so that we don't sample the border color. */
1537 if (opcode
== TGSI_OPCODE_TXF
||
1538 opcode
== TGSI_OPCODE_TXF_LZ
)
1539 filler
= ctx
->i32_0
;
1541 filler
= LLVMConstReal(ctx
->f32
, 0.5);
1543 if (target
== TGSI_TEXTURE_1D
||
1544 target
== TGSI_TEXTURE_SHADOW1D
) {
1545 args
.coords
[1] = filler
;
1546 } else if (target
== TGSI_TEXTURE_1D_ARRAY
||
1547 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
) {
1548 args
.coords
[2] = args
.coords
[1];
1549 args
.coords
[1] = filler
;
1553 /* Pack LOD or sample index */
1554 if (opcode
== TGSI_OPCODE_TXL
)
1555 args
.lod
= args
.coords
[3];
1556 else if (opcode
== TGSI_OPCODE_TXL2
)
1557 args
.lod
= lp_build_emit_fetch(bld_base
, inst
, 1, TGSI_CHAN_X
);
1558 else if (opcode
== TGSI_OPCODE_TXF
) {
1559 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1560 /* No LOD, but move sample index into the right place. */
1561 args
.coords
[2] = args
.coords
[3];
1562 } else if (target
!= TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1563 args
.lod
= args
.coords
[3];
1567 if (target
== TGSI_TEXTURE_2D_MSAA
||
1568 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1569 ac_apply_fmask_to_sample(&ctx
->ac
, fmask_ptr
, args
.coords
,
1570 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
1573 if (opcode
== TGSI_OPCODE_TXF
||
1574 opcode
== TGSI_OPCODE_TXF_LZ
) {
1575 /* add tex offsets */
1576 if (inst
->Texture
.NumOffsets
) {
1577 const struct tgsi_texture_offset
*off
= inst
->TexOffsets
;
1579 assert(inst
->Texture
.NumOffsets
== 1);
1582 case TGSI_TEXTURE_3D
:
1584 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[2],
1585 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleZ
], "");
1587 case TGSI_TEXTURE_2D
:
1588 case TGSI_TEXTURE_SHADOW2D
:
1589 case TGSI_TEXTURE_RECT
:
1590 case TGSI_TEXTURE_SHADOWRECT
:
1591 case TGSI_TEXTURE_2D_ARRAY
:
1592 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1594 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[1],
1595 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleY
], "");
1597 case TGSI_TEXTURE_1D
:
1598 case TGSI_TEXTURE_SHADOW1D
:
1599 case TGSI_TEXTURE_1D_ARRAY
:
1600 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1602 LLVMBuildAdd(ctx
->ac
.builder
, args
.coords
[0],
1603 ctx
->imms
[off
->Index
* TGSI_NUM_CHANNELS
+ off
->SwizzleX
], "");
1605 /* texture offsets do not apply to other texture targets */
1610 if (opcode
== TGSI_OPCODE_TG4
) {
1611 unsigned gather_comp
= 0;
1613 /* DMASK was repurposed for GATHER4. 4 components are always
1614 * returned and DMASK works like a swizzle - it selects
1615 * the component to fetch. The only valid DMASK values are
1616 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1617 * (red,red,red,red) etc.) The ISA document doesn't mention
1621 /* Get the component index from src1.x for Gather4. */
1622 if (!tgsi_is_shadow_target(target
)) {
1623 LLVMValueRef comp_imm
;
1624 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1626 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1628 comp_imm
= ctx
->imms
[src1
.Index
* TGSI_NUM_CHANNELS
+ src1
.SwizzleX
];
1629 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1630 gather_comp
= CLAMP(gather_comp
, 0, 3);
1633 args
.dmask
= 1 << gather_comp
;
1638 args
.dim
= ac_texture_dim_from_tgsi_target(ctx
->screen
, target
);
1639 args
.unorm
= target
== TGSI_TEXTURE_RECT
||
1640 target
== TGSI_TEXTURE_SHADOWRECT
;
1641 args
.opcode
= ac_image_sample
;
1644 case TGSI_OPCODE_TXF
:
1645 case TGSI_OPCODE_TXF_LZ
:
1646 args
.opcode
= opcode
== TGSI_OPCODE_TXF_LZ
||
1647 target
== TGSI_TEXTURE_2D_MSAA
||
1648 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
?
1649 ac_image_load
: ac_image_load_mip
;
1651 case TGSI_OPCODE_LODQ
:
1652 args
.opcode
= ac_image_get_lod
;
1654 case TGSI_OPCODE_TEX
:
1655 case TGSI_OPCODE_TEX2
:
1656 case TGSI_OPCODE_TXP
:
1657 if (ctx
->type
!= PIPE_SHADER_FRAGMENT
)
1658 args
.level_zero
= true;
1660 case TGSI_OPCODE_TEX_LZ
:
1661 args
.level_zero
= true;
1663 case TGSI_OPCODE_TXB
:
1664 case TGSI_OPCODE_TXB2
:
1665 assert(ctx
->type
== PIPE_SHADER_FRAGMENT
);
1667 case TGSI_OPCODE_TXL
:
1668 case TGSI_OPCODE_TXL2
:
1670 case TGSI_OPCODE_TXD
:
1672 case TGSI_OPCODE_TG4
:
1673 args
.opcode
= ac_image_gather4
;
1674 args
.level_zero
= true;
1681 /* The hardware needs special lowering for Gather4 with integer formats. */
1682 LLVMValueRef gather4_int_result_workaround
= NULL
;
1684 if (ctx
->screen
->info
.chip_class
<= VI
&&
1685 opcode
== TGSI_OPCODE_TG4
) {
1686 assert(inst
->Texture
.ReturnType
!= TGSI_RETURN_TYPE_UNKNOWN
);
1688 if (inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_SINT
||
1689 inst
->Texture
.ReturnType
== TGSI_RETURN_TYPE_UINT
) {
1690 gather4_int_result_workaround
=
1691 si_lower_gather4_integer(ctx
, &args
, target
,
1692 inst
->Texture
.ReturnType
);
1696 args
.attributes
= AC_FUNC_ATTR_READNONE
;
1697 LLVMValueRef result
= ac_build_image_opcode(&ctx
->ac
, &args
);
1699 if (gather4_int_result_workaround
) {
1700 result
= si_fix_gather4_integer_result(ctx
, result
,
1701 inst
->Texture
.ReturnType
,
1702 gather4_int_result_workaround
);
1705 emit_data
->output
[emit_data
->chan
] = result
;
1708 static void si_llvm_emit_txqs(
1709 const struct lp_build_tgsi_action
*action
,
1710 struct lp_build_tgsi_context
*bld_base
,
1711 struct lp_build_emit_data
*emit_data
)
1713 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1714 LLVMValueRef res
, samples
;
1715 LLVMValueRef res_ptr
, samp_ptr
, fmask_ptr
= NULL
;
1717 tex_fetch_ptrs(bld_base
, emit_data
, &res_ptr
, &samp_ptr
, &fmask_ptr
);
1719 /* Read the samples from the descriptor directly. */
1720 res
= LLVMBuildBitCast(ctx
->ac
.builder
, res_ptr
, ctx
->v8i32
, "");
1721 samples
= LLVMBuildExtractElement(ctx
->ac
.builder
, res
,
1722 LLVMConstInt(ctx
->i32
, 3, 0), "");
1723 samples
= LLVMBuildLShr(ctx
->ac
.builder
, samples
,
1724 LLVMConstInt(ctx
->i32
, 16, 0), "");
1725 samples
= LLVMBuildAnd(ctx
->ac
.builder
, samples
,
1726 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
1727 samples
= LLVMBuildShl(ctx
->ac
.builder
, ctx
->i32_1
,
1730 emit_data
->output
[emit_data
->chan
] = samples
;
1733 static void si_llvm_emit_fbfetch(const struct lp_build_tgsi_action
*action
,
1734 struct lp_build_tgsi_context
*bld_base
,
1735 struct lp_build_emit_data
*emit_data
)
1737 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1738 struct ac_image_args args
= {};
1739 LLVMValueRef ptr
, image
, fmask
;
1741 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1745 /* Load the image descriptor. */
1746 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0
% 2 == 0);
1747 ptr
= LLVMGetParam(ctx
->main_fn
, ctx
->param_rw_buffers
);
1748 ptr
= LLVMBuildPointerCast(ctx
->ac
.builder
, ptr
,
1749 ac_array_in_const32_addr_space(ctx
->v8i32
), "");
1750 image
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1751 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0
/ 2, 0));
1755 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 0, 16);
1757 if (!ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1758 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_POS_FIXED_PT
, 16, 16);
1760 /* Get the current render target layer index. */
1761 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
)
1762 args
.coords
[chan
++] = si_unpack_param(ctx
, SI_PARAM_ANCILLARY
, 16, 11);
1764 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1765 args
.coords
[chan
++] = si_get_sample_id(ctx
);
1767 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
) {
1768 fmask
= ac_build_load_to_sgpr(&ctx
->ac
, ptr
,
1769 LLVMConstInt(ctx
->i32
, SI_PS_IMAGE_COLORBUF0_FMASK
/ 2, 0));
1771 ac_apply_fmask_to_sample(&ctx
->ac
, fmask
, args
.coords
,
1772 ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
);
1775 args
.opcode
= ac_image_load
;
1776 args
.resource
= image
;
1778 if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_msaa
)
1779 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1780 ac_image_2darraymsaa
: ac_image_2dmsaa
;
1781 else if (ctx
->shader
->key
.mono
.u
.ps
.fbfetch_is_1D
)
1782 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1783 ac_image_1darray
: ac_image_1d
;
1785 args
.dim
= ctx
->shader
->key
.mono
.u
.ps
.fbfetch_layered
?
1786 ac_image_2darray
: ac_image_2d
;
1788 emit_data
->output
[emit_data
->chan
] =
1789 ac_build_image_opcode(&ctx
->ac
, &args
);
1793 * Setup actions for TGSI memory opcode, including texture opcodes.
1795 void si_shader_context_init_mem(struct si_shader_context
*ctx
)
1797 struct lp_build_tgsi_context
*bld_base
;
1798 struct lp_build_tgsi_action tmpl
= {};
1800 bld_base
= &ctx
->bld_base
;
1802 bld_base
->op_actions
[TGSI_OPCODE_TEX
].emit
= build_tex_intrinsic
;
1803 bld_base
->op_actions
[TGSI_OPCODE_TEX_LZ
].emit
= build_tex_intrinsic
;
1804 bld_base
->op_actions
[TGSI_OPCODE_TEX2
].emit
= build_tex_intrinsic
;
1805 bld_base
->op_actions
[TGSI_OPCODE_TXB
].emit
= build_tex_intrinsic
;
1806 bld_base
->op_actions
[TGSI_OPCODE_TXB2
].emit
= build_tex_intrinsic
;
1807 bld_base
->op_actions
[TGSI_OPCODE_TXD
].emit
= build_tex_intrinsic
;
1808 bld_base
->op_actions
[TGSI_OPCODE_TXF
].emit
= build_tex_intrinsic
;
1809 bld_base
->op_actions
[TGSI_OPCODE_TXF_LZ
].emit
= build_tex_intrinsic
;
1810 bld_base
->op_actions
[TGSI_OPCODE_TXL
].emit
= build_tex_intrinsic
;
1811 bld_base
->op_actions
[TGSI_OPCODE_TXL2
].emit
= build_tex_intrinsic
;
1812 bld_base
->op_actions
[TGSI_OPCODE_TXP
].emit
= build_tex_intrinsic
;
1813 bld_base
->op_actions
[TGSI_OPCODE_TXQ
].emit
= resq_emit
;
1814 bld_base
->op_actions
[TGSI_OPCODE_TG4
].emit
= build_tex_intrinsic
;
1815 bld_base
->op_actions
[TGSI_OPCODE_LODQ
].emit
= build_tex_intrinsic
;
1816 bld_base
->op_actions
[TGSI_OPCODE_TXQS
].emit
= si_llvm_emit_txqs
;
1818 bld_base
->op_actions
[TGSI_OPCODE_FBFETCH
].emit
= si_llvm_emit_fbfetch
;
1820 bld_base
->op_actions
[TGSI_OPCODE_LOAD
].emit
= load_emit
;
1821 bld_base
->op_actions
[TGSI_OPCODE_STORE
].emit
= store_emit
;
1822 bld_base
->op_actions
[TGSI_OPCODE_RESQ
].emit
= resq_emit
;
1824 tmpl
.fetch_args
= atomic_fetch_args
;
1825 tmpl
.emit
= atomic_emit
;
1826 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
] = tmpl
;
1827 bld_base
->op_actions
[TGSI_OPCODE_ATOMUADD
].intr_name
= "add";
1828 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
] = tmpl
;
1829 bld_base
->op_actions
[TGSI_OPCODE_ATOMXCHG
].intr_name
= "swap";
1830 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
] = tmpl
;
1831 bld_base
->op_actions
[TGSI_OPCODE_ATOMCAS
].intr_name
= "cmpswap";
1832 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
] = tmpl
;
1833 bld_base
->op_actions
[TGSI_OPCODE_ATOMAND
].intr_name
= "and";
1834 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
] = tmpl
;
1835 bld_base
->op_actions
[TGSI_OPCODE_ATOMOR
].intr_name
= "or";
1836 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
] = tmpl
;
1837 bld_base
->op_actions
[TGSI_OPCODE_ATOMXOR
].intr_name
= "xor";
1838 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
] = tmpl
;
1839 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMIN
].intr_name
= "umin";
1840 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
] = tmpl
;
1841 bld_base
->op_actions
[TGSI_OPCODE_ATOMUMAX
].intr_name
= "umax";
1842 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
] = tmpl
;
1843 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMIN
].intr_name
= "smin";
1844 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
] = tmpl
;
1845 bld_base
->op_actions
[TGSI_OPCODE_ATOMIMAX
].intr_name
= "smax";