radeonsi: remove unused variable addr_vec
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_mem.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include "si_shader_internal.h"
26 #include "si_pipe.h"
27 #include "sid.h"
28 #include "gallivm/lp_bld_arit.h"
29 #include "gallivm/lp_bld_gather.h"
30 #include "gallivm/lp_bld_intr.h"
31 #include "tgsi/tgsi_build.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "tgsi/tgsi_util.h"
34
35 static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
36 struct lp_build_tgsi_context *bld_base,
37 struct lp_build_emit_data *emit_data);
38
39 static const struct lp_build_tgsi_action tex_action;
40
41 /**
42 * Given a v8i32 resource descriptor for a buffer, extract the size of the
43 * buffer in number of elements and return it as an i32.
44 */
45 static LLVMValueRef get_buffer_size(
46 struct lp_build_tgsi_context *bld_base,
47 LLVMValueRef descriptor)
48 {
49 struct si_shader_context *ctx = si_shader_context(bld_base);
50 LLVMBuilderRef builder = ctx->ac.builder;
51 LLVMValueRef size =
52 LLVMBuildExtractElement(builder, descriptor,
53 LLVMConstInt(ctx->i32, 2, 0), "");
54
55 if (ctx->screen->info.chip_class == VI) {
56 /* On VI, the descriptor contains the size in bytes,
57 * but TXQ must return the size in elements.
58 * The stride is always non-zero for resources using TXQ.
59 */
60 LLVMValueRef stride =
61 LLVMBuildExtractElement(builder, descriptor,
62 ctx->i32_1, "");
63 stride = LLVMBuildLShr(builder, stride,
64 LLVMConstInt(ctx->i32, 16, 0), "");
65 stride = LLVMBuildAnd(builder, stride,
66 LLVMConstInt(ctx->i32, 0x3FFF, 0), "");
67
68 size = LLVMBuildUDiv(builder, size, stride, "");
69 }
70
71 return size;
72 }
73
74 static LLVMValueRef
75 shader_buffer_fetch_rsrc(struct si_shader_context *ctx,
76 const struct tgsi_full_src_register *reg,
77 bool ubo)
78 {
79 LLVMValueRef index;
80
81 if (!reg->Register.Indirect) {
82 index = LLVMConstInt(ctx->i32, reg->Register.Index, false);
83 } else {
84 index = si_get_indirect_index(ctx, &reg->Indirect,
85 1, reg->Register.Index);
86 }
87
88 if (ubo)
89 return ctx->abi.load_ubo(&ctx->abi, index);
90 else
91 return ctx->abi.load_ssbo(&ctx->abi, index, false);
92 }
93
94 static enum ac_image_dim
95 ac_texture_dim_from_tgsi_target(struct si_screen *screen, enum tgsi_texture_type target)
96 {
97 switch (target) {
98 case TGSI_TEXTURE_1D:
99 case TGSI_TEXTURE_SHADOW1D:
100 if (screen->info.chip_class >= GFX9)
101 return ac_image_2d;
102 return ac_image_1d;
103 case TGSI_TEXTURE_2D:
104 case TGSI_TEXTURE_SHADOW2D:
105 case TGSI_TEXTURE_RECT:
106 case TGSI_TEXTURE_SHADOWRECT:
107 return ac_image_2d;
108 case TGSI_TEXTURE_3D:
109 return ac_image_3d;
110 case TGSI_TEXTURE_CUBE:
111 case TGSI_TEXTURE_SHADOWCUBE:
112 case TGSI_TEXTURE_CUBE_ARRAY:
113 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
114 return ac_image_cube;
115 case TGSI_TEXTURE_1D_ARRAY:
116 case TGSI_TEXTURE_SHADOW1D_ARRAY:
117 if (screen->info.chip_class >= GFX9)
118 return ac_image_2darray;
119 return ac_image_1darray;
120 case TGSI_TEXTURE_2D_ARRAY:
121 case TGSI_TEXTURE_SHADOW2D_ARRAY:
122 return ac_image_2darray;
123 case TGSI_TEXTURE_2D_MSAA:
124 return ac_image_2dmsaa;
125 case TGSI_TEXTURE_2D_ARRAY_MSAA:
126 return ac_image_2darraymsaa;
127 default:
128 unreachable("unhandled texture type");
129 }
130 }
131
132 static enum ac_image_dim
133 ac_image_dim_from_tgsi_target(struct si_screen *screen, enum tgsi_texture_type target)
134 {
135 enum ac_image_dim dim = ac_texture_dim_from_tgsi_target(screen, target);
136
137 /* Match the resource type set in the descriptor. */
138 if (dim == ac_image_cube ||
139 (screen->info.chip_class <= VI && dim == ac_image_3d))
140 dim = ac_image_2darray;
141 else if (target == TGSI_TEXTURE_2D && screen->info.chip_class >= GFX9) {
142 /* When a single layer of a 3D texture is bound, the shader
143 * will refer to a 2D target, but the descriptor has a 3D type.
144 * Since the HW ignores BASE_ARRAY in this case, we need to
145 * send 3 coordinates. This doesn't hurt when the underlying
146 * texture is non-3D.
147 */
148 dim = ac_image_3d;
149 }
150
151 return dim;
152 }
153
154 /**
155 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
156 *
157 * At least on Tonga, executing image stores on images with DCC enabled and
158 * non-trivial can eventually lead to lockups. This can occur when an
159 * application binds an image as read-only but then uses a shader that writes
160 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
161 * program termination) in this case, but it doesn't cost much to be a bit
162 * nicer: disabling DCC in the shader still leads to undefined results but
163 * avoids the lockup.
164 */
165 static LLVMValueRef force_dcc_off(struct si_shader_context *ctx,
166 LLVMValueRef rsrc)
167 {
168 if (ctx->screen->info.chip_class <= CIK) {
169 return rsrc;
170 } else {
171 LLVMValueRef i32_6 = LLVMConstInt(ctx->i32, 6, 0);
172 LLVMValueRef i32_C = LLVMConstInt(ctx->i32, C_008F28_COMPRESSION_EN, 0);
173 LLVMValueRef tmp;
174
175 tmp = LLVMBuildExtractElement(ctx->ac.builder, rsrc, i32_6, "");
176 tmp = LLVMBuildAnd(ctx->ac.builder, tmp, i32_C, "");
177 return LLVMBuildInsertElement(ctx->ac.builder, rsrc, tmp, i32_6, "");
178 }
179 }
180
181 LLVMValueRef si_load_image_desc(struct si_shader_context *ctx,
182 LLVMValueRef list, LLVMValueRef index,
183 enum ac_descriptor_type desc_type, bool dcc_off)
184 {
185 LLVMBuilderRef builder = ctx->ac.builder;
186 LLVMValueRef rsrc;
187
188 if (desc_type == AC_DESC_BUFFER) {
189 index = LLVMBuildMul(builder, index,
190 LLVMConstInt(ctx->i32, 2, 0), "");
191 index = LLVMBuildAdd(builder, index,
192 ctx->i32_1, "");
193 list = LLVMBuildPointerCast(builder, list,
194 ac_array_in_const32_addr_space(ctx->v4i32), "");
195 } else {
196 assert(desc_type == AC_DESC_IMAGE);
197 }
198
199 rsrc = ac_build_load_to_sgpr(&ctx->ac, list, index);
200 if (desc_type == AC_DESC_IMAGE && dcc_off)
201 rsrc = force_dcc_off(ctx, rsrc);
202 return rsrc;
203 }
204
205 /**
206 * Load the resource descriptor for \p image.
207 */
208 static void
209 image_fetch_rsrc(
210 struct lp_build_tgsi_context *bld_base,
211 const struct tgsi_full_src_register *image,
212 bool is_store, unsigned target,
213 LLVMValueRef *rsrc)
214 {
215 struct si_shader_context *ctx = si_shader_context(bld_base);
216 LLVMValueRef rsrc_ptr = LLVMGetParam(ctx->main_fn,
217 ctx->param_samplers_and_images);
218 LLVMValueRef index;
219 bool dcc_off = is_store;
220
221 if (!image->Register.Indirect) {
222 const struct tgsi_shader_info *info = bld_base->info;
223 unsigned images_writemask = info->images_store |
224 info->images_atomic;
225
226 index = LLVMConstInt(ctx->i32,
227 si_get_image_slot(image->Register.Index), 0);
228
229 if (images_writemask & (1 << image->Register.Index))
230 dcc_off = true;
231 } else {
232 /* From the GL_ARB_shader_image_load_store extension spec:
233 *
234 * If a shader performs an image load, store, or atomic
235 * operation using an image variable declared as an array,
236 * and if the index used to select an individual element is
237 * negative or greater than or equal to the size of the
238 * array, the results of the operation are undefined but may
239 * not lead to termination.
240 */
241 index = si_get_bounded_indirect_index(ctx, &image->Indirect,
242 image->Register.Index,
243 ctx->num_images);
244 index = LLVMBuildSub(ctx->ac.builder,
245 LLVMConstInt(ctx->i32, SI_NUM_IMAGES - 1, 0),
246 index, "");
247 }
248
249 if (image->Register.File != TGSI_FILE_IMAGE) {
250 /* Bindless descriptors are accessible from a different pair of
251 * user SGPR indices.
252 */
253 rsrc_ptr = LLVMGetParam(ctx->main_fn,
254 ctx->param_bindless_samplers_and_images);
255 index = lp_build_emit_fetch_src(bld_base, image,
256 TGSI_TYPE_UNSIGNED, 0);
257
258 /* For simplicity, bindless image descriptors use fixed
259 * 16-dword slots for now.
260 */
261 index = LLVMBuildMul(ctx->ac.builder, index,
262 LLVMConstInt(ctx->i32, 2, 0), "");
263 }
264
265 *rsrc = si_load_image_desc(ctx, rsrc_ptr, index,
266 target == TGSI_TEXTURE_BUFFER ? AC_DESC_BUFFER : AC_DESC_IMAGE,
267 dcc_off);
268 }
269
270 static void image_fetch_coords(
271 struct lp_build_tgsi_context *bld_base,
272 const struct tgsi_full_instruction *inst,
273 unsigned src, LLVMValueRef desc,
274 LLVMValueRef *coords)
275 {
276 struct si_shader_context *ctx = si_shader_context(bld_base);
277 LLVMBuilderRef builder = ctx->ac.builder;
278 unsigned target = inst->Memory.Texture;
279 const unsigned num_coords = tgsi_util_get_texture_coord_dim(target);
280 LLVMValueRef tmp;
281 int chan;
282
283 for (chan = 0; chan < num_coords; ++chan) {
284 tmp = lp_build_emit_fetch(bld_base, inst, src, chan);
285 tmp = ac_to_integer(&ctx->ac, tmp);
286 coords[chan] = tmp;
287 }
288
289 if (ctx->screen->info.chip_class >= GFX9) {
290 /* 1D textures are allocated and used as 2D on GFX9. */
291 if (target == TGSI_TEXTURE_1D) {
292 coords[1] = ctx->i32_0;
293 } else if (target == TGSI_TEXTURE_1D_ARRAY) {
294 coords[2] = coords[1];
295 coords[1] = ctx->i32_0;
296 } else if (target == TGSI_TEXTURE_2D) {
297 /* The hw can't bind a slice of a 3D image as a 2D
298 * image, because it ignores BASE_ARRAY if the target
299 * is 3D. The workaround is to read BASE_ARRAY and set
300 * it as the 3rd address operand for all 2D images.
301 */
302 LLVMValueRef first_layer, const5, mask;
303
304 const5 = LLVMConstInt(ctx->i32, 5, 0);
305 mask = LLVMConstInt(ctx->i32, S_008F24_BASE_ARRAY(~0), 0);
306 first_layer = LLVMBuildExtractElement(builder, desc, const5, "");
307 first_layer = LLVMBuildAnd(builder, first_layer, mask, "");
308
309 coords[2] = first_layer;
310 }
311 }
312 }
313
314 /**
315 * Append the resource and indexing arguments for buffer intrinsics.
316 *
317 * \param rsrc the v4i32 buffer resource
318 * \param index index into the buffer (stride-based)
319 * \param offset byte offset into the buffer
320 */
321 static void buffer_append_args(
322 struct si_shader_context *ctx,
323 struct lp_build_emit_data *emit_data,
324 LLVMValueRef rsrc,
325 LLVMValueRef index,
326 LLVMValueRef offset,
327 bool atomic,
328 bool force_glc)
329 {
330 const struct tgsi_full_instruction *inst = emit_data->inst;
331 LLVMValueRef i1false = LLVMConstInt(ctx->i1, 0, 0);
332 LLVMValueRef i1true = LLVMConstInt(ctx->i1, 1, 0);
333
334 emit_data->args[emit_data->arg_count++] = rsrc;
335 emit_data->args[emit_data->arg_count++] = index; /* vindex */
336 emit_data->args[emit_data->arg_count++] = offset; /* voffset */
337 if (!atomic) {
338 emit_data->args[emit_data->arg_count++] =
339 force_glc ||
340 inst->Memory.Qualifier & (TGSI_MEMORY_COHERENT | TGSI_MEMORY_VOLATILE) ?
341 i1true : i1false; /* glc */
342 }
343 emit_data->args[emit_data->arg_count++] = i1false; /* slc */
344 }
345
346 static void load_fetch_args(
347 struct lp_build_tgsi_context * bld_base,
348 struct lp_build_emit_data * emit_data)
349 {
350 struct si_shader_context *ctx = si_shader_context(bld_base);
351 const struct tgsi_full_instruction * inst = emit_data->inst;
352 unsigned target = inst->Memory.Texture;
353 LLVMValueRef rsrc;
354
355 emit_data->dst_type = ctx->v4f32;
356
357 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER ||
358 inst->Src[0].Register.File == TGSI_FILE_CONSTBUF) {
359 LLVMValueRef offset;
360 LLVMValueRef tmp;
361
362 bool ubo = inst->Src[0].Register.File == TGSI_FILE_CONSTBUF;
363 rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0], ubo);
364
365 tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
366 offset = ac_to_integer(&ctx->ac, tmp);
367
368 buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
369 offset, false, false);
370 } else if (inst->Src[0].Register.File == TGSI_FILE_IMAGE ||
371 tgsi_is_bindless_image_file(inst->Src[0].Register.File)) {
372 image_fetch_rsrc(bld_base, &inst->Src[0], false, target, &rsrc);
373 image_fetch_coords(bld_base, inst, 1, rsrc, &emit_data->args[1]);
374
375 if (target == TGSI_TEXTURE_BUFFER) {
376 buffer_append_args(ctx, emit_data, rsrc, emit_data->args[1],
377 ctx->i32_0, false, false);
378 } else {
379 emit_data->args[0] = rsrc;
380 }
381 }
382 }
383
384 static void load_emit_buffer(struct si_shader_context *ctx,
385 struct lp_build_emit_data *emit_data,
386 bool can_speculate, bool allow_smem)
387 {
388 const struct tgsi_full_instruction *inst = emit_data->inst;
389 uint writemask = inst->Dst[0].Register.WriteMask;
390 uint count = util_last_bit(writemask);
391 LLVMValueRef *args = emit_data->args;
392
393 /* Don't use SMEM for shader buffer loads, because LLVM doesn't
394 * select SMEM for SI.load.const with a non-constant offset, and
395 * constant offsets practically don't exist with shader buffers.
396 *
397 * Also, SI.load.const doesn't use inst_offset when it's lowered
398 * to VMEM, so we just end up with more VALU instructions in the end
399 * and no benefit.
400 *
401 * TODO: Remove this line once LLVM can select SMEM with a non-constant
402 * offset, and can derive inst_offset when VMEM is selected.
403 * After that, si_memory_barrier should invalidate sL1 for shader
404 * buffers.
405 */
406
407 assert(LLVMConstIntGetZExtValue(args[1]) == 0); /* vindex */
408 emit_data->output[emit_data->chan] =
409 ac_build_buffer_load(&ctx->ac, args[0], count, NULL,
410 args[2], NULL, 0,
411 LLVMConstIntGetZExtValue(args[3]),
412 LLVMConstIntGetZExtValue(args[4]),
413 can_speculate, allow_smem);
414 }
415
416 static LLVMValueRef get_memory_ptr(struct si_shader_context *ctx,
417 const struct tgsi_full_instruction *inst,
418 LLVMTypeRef type, int arg)
419 {
420 LLVMBuilderRef builder = ctx->ac.builder;
421 LLVMValueRef offset, ptr;
422 int addr_space;
423
424 offset = lp_build_emit_fetch(&ctx->bld_base, inst, arg, 0);
425 offset = ac_to_integer(&ctx->ac, offset);
426
427 ptr = ctx->ac.lds;
428 ptr = LLVMBuildGEP(builder, ptr, &offset, 1, "");
429 addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
430 ptr = LLVMBuildBitCast(builder, ptr, LLVMPointerType(type, addr_space), "");
431
432 return ptr;
433 }
434
435 static void load_emit_memory(
436 struct si_shader_context *ctx,
437 struct lp_build_emit_data *emit_data)
438 {
439 const struct tgsi_full_instruction *inst = emit_data->inst;
440 unsigned writemask = inst->Dst[0].Register.WriteMask;
441 LLVMValueRef channels[4], ptr, derived_ptr, index;
442 int chan;
443
444 ptr = get_memory_ptr(ctx, inst, ctx->f32, 1);
445
446 for (chan = 0; chan < 4; ++chan) {
447 if (!(writemask & (1 << chan))) {
448 channels[chan] = LLVMGetUndef(ctx->f32);
449 continue;
450 }
451
452 index = LLVMConstInt(ctx->i32, chan, 0);
453 derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
454 channels[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
455 }
456 emit_data->output[emit_data->chan] = lp_build_gather_values(&ctx->gallivm, channels, 4);
457 }
458
459 /**
460 * Return true if the memory accessed by a LOAD or STORE instruction is
461 * read-only or write-only, respectively.
462 *
463 * \param shader_buffers_reverse_access_mask
464 * For LOAD, set this to (store | atomic) slot usage in the shader.
465 * For STORE, set this to (load | atomic) slot usage in the shader.
466 * \param images_reverse_access_mask Same as above, but for images.
467 */
468 static bool is_oneway_access_only(const struct tgsi_full_instruction *inst,
469 const struct tgsi_shader_info *info,
470 unsigned shader_buffers_reverse_access_mask,
471 unsigned images_reverse_access_mask)
472 {
473 /* RESTRICT means NOALIAS.
474 * If there are no writes, we can assume the accessed memory is read-only.
475 * If there are no reads, we can assume the accessed memory is write-only.
476 */
477 if (inst->Memory.Qualifier & TGSI_MEMORY_RESTRICT) {
478 unsigned reverse_access_mask;
479
480 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
481 reverse_access_mask = shader_buffers_reverse_access_mask;
482 } else if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
483 reverse_access_mask = info->images_buffers &
484 images_reverse_access_mask;
485 } else {
486 reverse_access_mask = ~info->images_buffers &
487 images_reverse_access_mask;
488 }
489
490 if (inst->Src[0].Register.Indirect) {
491 if (!reverse_access_mask)
492 return true;
493 } else {
494 if (!(reverse_access_mask &
495 (1u << inst->Src[0].Register.Index)))
496 return true;
497 }
498 }
499
500 /* If there are no buffer writes (for both shader buffers & image
501 * buffers), it implies that buffer memory is read-only.
502 * If there are no buffer reads (for both shader buffers & image
503 * buffers), it implies that buffer memory is write-only.
504 *
505 * Same for the case when there are no writes/reads for non-buffer
506 * images.
507 */
508 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER ||
509 (inst->Memory.Texture == TGSI_TEXTURE_BUFFER &&
510 (inst->Src[0].Register.File == TGSI_FILE_IMAGE ||
511 tgsi_is_bindless_image_file(inst->Src[0].Register.File)))) {
512 if (!shader_buffers_reverse_access_mask &&
513 !(info->images_buffers & images_reverse_access_mask))
514 return true;
515 } else {
516 if (!(~info->images_buffers & images_reverse_access_mask))
517 return true;
518 }
519 return false;
520 }
521
522 static void load_emit(
523 const struct lp_build_tgsi_action *action,
524 struct lp_build_tgsi_context *bld_base,
525 struct lp_build_emit_data *emit_data)
526 {
527 struct si_shader_context *ctx = si_shader_context(bld_base);
528 const struct tgsi_full_instruction * inst = emit_data->inst;
529 const struct tgsi_shader_info *info = &ctx->shader->selector->info;
530 bool can_speculate = false;
531
532 if (inst->Src[0].Register.File == TGSI_FILE_MEMORY) {
533 load_emit_memory(ctx, emit_data);
534 return;
535 }
536
537 if (inst->Src[0].Register.File == TGSI_FILE_CONSTBUF) {
538 load_emit_buffer(ctx, emit_data, true, true);
539 return;
540 }
541
542 if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
543 ac_build_waitcnt(&ctx->ac, VM_CNT);
544
545 can_speculate = !(inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE) &&
546 is_oneway_access_only(inst, info,
547 info->shader_buffers_store |
548 info->shader_buffers_atomic,
549 info->images_store |
550 info->images_atomic);
551
552 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
553 load_emit_buffer(ctx, emit_data, can_speculate, false);
554 return;
555 }
556
557 if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
558 unsigned num_channels = util_last_bit(inst->Dst[0].Register.WriteMask);
559 LLVMValueRef result =
560 ac_build_buffer_load_format(&ctx->ac,
561 emit_data->args[0],
562 emit_data->args[1],
563 emit_data->args[2],
564 num_channels,
565 LLVMConstIntGetZExtValue(emit_data->args[3]),
566 can_speculate);
567 emit_data->output[emit_data->chan] =
568 ac_build_expand_to_vec4(&ctx->ac, result, num_channels);
569 } else {
570 struct ac_image_args args = {};
571 args.opcode = ac_image_load;
572 args.resource = emit_data->args[0];
573 memcpy(args.coords, &emit_data->args[1], sizeof(args.coords));
574 args.dim = ac_image_dim_from_tgsi_target(ctx->screen, inst->Memory.Texture);
575 if (inst->Memory.Qualifier & (TGSI_MEMORY_COHERENT | TGSI_MEMORY_VOLATILE))
576 args.cache_policy = ac_glc;
577 args.attributes = ac_get_load_intr_attribs(can_speculate);
578 args.dmask = 0xf;
579
580 emit_data->output[emit_data->chan] =
581 ac_build_image_opcode(&ctx->ac, &args);
582 }
583 }
584
585 static void store_fetch_args(
586 struct lp_build_tgsi_context * bld_base,
587 struct lp_build_emit_data * emit_data)
588 {
589 struct si_shader_context *ctx = si_shader_context(bld_base);
590 const struct tgsi_full_instruction * inst = emit_data->inst;
591 struct tgsi_full_src_register memory;
592 LLVMValueRef chans[4];
593 LLVMValueRef data;
594 LLVMValueRef rsrc;
595 unsigned chan;
596
597 emit_data->dst_type = ctx->voidt;
598
599 for (chan = 0; chan < 4; ++chan) {
600 chans[chan] = lp_build_emit_fetch(bld_base, inst, 1, chan);
601 }
602 data = lp_build_gather_values(&ctx->gallivm, chans, 4);
603
604 emit_data->args[emit_data->arg_count++] = data;
605
606 memory = tgsi_full_src_register_from_dst(&inst->Dst[0]);
607
608 if (inst->Dst[0].Register.File == TGSI_FILE_BUFFER) {
609 LLVMValueRef offset;
610 LLVMValueRef tmp;
611
612 rsrc = shader_buffer_fetch_rsrc(ctx, &memory, false);
613
614 tmp = lp_build_emit_fetch(bld_base, inst, 0, 0);
615 offset = ac_to_integer(&ctx->ac, tmp);
616
617 buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
618 offset, false, false);
619 } else if (inst->Dst[0].Register.File == TGSI_FILE_IMAGE ||
620 tgsi_is_bindless_image_file(inst->Dst[0].Register.File)) {
621 unsigned target = inst->Memory.Texture;
622
623 /* 8bit/16bit TC L1 write corruption bug on SI.
624 * All store opcodes not aligned to a dword are affected.
625 *
626 * The only way to get unaligned stores in radeonsi is through
627 * shader images.
628 */
629 bool force_glc = ctx->screen->info.chip_class == SI;
630
631 image_fetch_rsrc(bld_base, &memory, true, target, &rsrc);
632 image_fetch_coords(bld_base, inst, 0, rsrc, &emit_data->args[2]);
633
634 if (target == TGSI_TEXTURE_BUFFER) {
635 buffer_append_args(ctx, emit_data, rsrc, emit_data->args[2],
636 ctx->i32_0, false, force_glc);
637 } else {
638 emit_data->args[1] = rsrc;
639 }
640 }
641 }
642
643 static void store_emit_buffer(
644 struct si_shader_context *ctx,
645 struct lp_build_emit_data *emit_data,
646 bool writeonly_memory)
647 {
648 const struct tgsi_full_instruction *inst = emit_data->inst;
649 LLVMBuilderRef builder = ctx->ac.builder;
650 LLVMValueRef base_data = emit_data->args[0];
651 LLVMValueRef base_offset = emit_data->args[3];
652 unsigned writemask = inst->Dst[0].Register.WriteMask;
653
654 while (writemask) {
655 int start, count;
656 const char *intrinsic_name;
657 LLVMValueRef data;
658 LLVMValueRef offset;
659 LLVMValueRef tmp;
660
661 u_bit_scan_consecutive_range(&writemask, &start, &count);
662
663 /* Due to an LLVM limitation, split 3-element writes
664 * into a 2-element and a 1-element write. */
665 if (count == 3) {
666 writemask |= 1 << (start + 2);
667 count = 2;
668 }
669
670 if (count == 4) {
671 data = base_data;
672 intrinsic_name = "llvm.amdgcn.buffer.store.v4f32";
673 } else if (count == 2) {
674 LLVMTypeRef v2f32 = LLVMVectorType(ctx->f32, 2);
675
676 tmp = LLVMBuildExtractElement(
677 builder, base_data,
678 LLVMConstInt(ctx->i32, start, 0), "");
679 data = LLVMBuildInsertElement(
680 builder, LLVMGetUndef(v2f32), tmp,
681 ctx->i32_0, "");
682
683 tmp = LLVMBuildExtractElement(
684 builder, base_data,
685 LLVMConstInt(ctx->i32, start + 1, 0), "");
686 data = LLVMBuildInsertElement(
687 builder, data, tmp, ctx->i32_1, "");
688
689 intrinsic_name = "llvm.amdgcn.buffer.store.v2f32";
690 } else {
691 assert(count == 1);
692 data = LLVMBuildExtractElement(
693 builder, base_data,
694 LLVMConstInt(ctx->i32, start, 0), "");
695 intrinsic_name = "llvm.amdgcn.buffer.store.f32";
696 }
697
698 offset = base_offset;
699 if (start != 0) {
700 offset = LLVMBuildAdd(
701 builder, offset,
702 LLVMConstInt(ctx->i32, start * 4, 0), "");
703 }
704
705 emit_data->args[0] = data;
706 emit_data->args[3] = offset;
707
708 lp_build_intrinsic(
709 builder, intrinsic_name, emit_data->dst_type,
710 emit_data->args, emit_data->arg_count,
711 ac_get_store_intr_attribs(writeonly_memory));
712 }
713 }
714
715 static void store_emit_memory(
716 struct si_shader_context *ctx,
717 struct lp_build_emit_data *emit_data)
718 {
719 const struct tgsi_full_instruction *inst = emit_data->inst;
720 LLVMBuilderRef builder = ctx->ac.builder;
721 unsigned writemask = inst->Dst[0].Register.WriteMask;
722 LLVMValueRef ptr, derived_ptr, data, index;
723 int chan;
724
725 ptr = get_memory_ptr(ctx, inst, ctx->f32, 0);
726
727 for (chan = 0; chan < 4; ++chan) {
728 if (!(writemask & (1 << chan))) {
729 continue;
730 }
731 data = lp_build_emit_fetch(&ctx->bld_base, inst, 1, chan);
732 index = LLVMConstInt(ctx->i32, chan, 0);
733 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
734 LLVMBuildStore(builder, data, derived_ptr);
735 }
736 }
737
738 static void store_emit(
739 const struct lp_build_tgsi_action *action,
740 struct lp_build_tgsi_context *bld_base,
741 struct lp_build_emit_data *emit_data)
742 {
743 struct si_shader_context *ctx = si_shader_context(bld_base);
744 LLVMBuilderRef builder = ctx->ac.builder;
745 const struct tgsi_full_instruction * inst = emit_data->inst;
746 const struct tgsi_shader_info *info = &ctx->shader->selector->info;
747 unsigned target = inst->Memory.Texture;
748 bool writeonly_memory = false;
749
750 if (inst->Dst[0].Register.File == TGSI_FILE_MEMORY) {
751 store_emit_memory(ctx, emit_data);
752 return;
753 }
754
755 if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
756 ac_build_waitcnt(&ctx->ac, VM_CNT);
757
758 writeonly_memory = is_oneway_access_only(inst, info,
759 info->shader_buffers_load |
760 info->shader_buffers_atomic,
761 info->images_load |
762 info->images_atomic);
763
764 if (inst->Dst[0].Register.File == TGSI_FILE_BUFFER) {
765 store_emit_buffer(ctx, emit_data, writeonly_memory);
766 return;
767 }
768
769 if (target == TGSI_TEXTURE_BUFFER) {
770 emit_data->output[emit_data->chan] = lp_build_intrinsic(
771 builder, "llvm.amdgcn.buffer.store.format.v4f32",
772 emit_data->dst_type, emit_data->args,
773 emit_data->arg_count,
774 ac_get_store_intr_attribs(writeonly_memory));
775 } else {
776 struct ac_image_args args = {};
777 args.opcode = ac_image_store;
778 args.data[0] = emit_data->args[0];
779 args.resource = emit_data->args[1];
780 memcpy(args.coords, &emit_data->args[2], sizeof(args.coords));
781 args.dim = ac_image_dim_from_tgsi_target(ctx->screen, inst->Memory.Texture);
782 args.attributes = ac_get_store_intr_attribs(writeonly_memory);
783 args.dmask = 0xf;
784
785 /* Workaround for 8bit/16bit TC L1 write corruption bug on SI.
786 * All store opcodes not aligned to a dword are affected.
787 */
788 bool force_glc = ctx->screen->info.chip_class == SI;
789 if (force_glc ||
790 inst->Memory.Qualifier & (TGSI_MEMORY_COHERENT | TGSI_MEMORY_VOLATILE))
791 args.cache_policy = ac_glc;
792
793 emit_data->output[emit_data->chan] =
794 ac_build_image_opcode(&ctx->ac, &args);
795 }
796 }
797
798 static void atomic_fetch_args(
799 struct lp_build_tgsi_context * bld_base,
800 struct lp_build_emit_data * emit_data)
801 {
802 struct si_shader_context *ctx = si_shader_context(bld_base);
803 const struct tgsi_full_instruction * inst = emit_data->inst;
804 LLVMValueRef data1, data2;
805 LLVMValueRef rsrc;
806 LLVMValueRef tmp;
807
808 emit_data->dst_type = ctx->f32;
809
810 tmp = lp_build_emit_fetch(bld_base, inst, 2, 0);
811 data1 = ac_to_integer(&ctx->ac, tmp);
812
813 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
814 tmp = lp_build_emit_fetch(bld_base, inst, 3, 0);
815 data2 = ac_to_integer(&ctx->ac, tmp);
816 }
817
818 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
819 * of arguments, which is reversed relative to TGSI (and GLSL)
820 */
821 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS)
822 emit_data->args[emit_data->arg_count++] = data2;
823 emit_data->args[emit_data->arg_count++] = data1;
824
825 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
826 LLVMValueRef offset;
827
828 rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0], false);
829
830 tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
831 offset = ac_to_integer(&ctx->ac, tmp);
832
833 buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
834 offset, true, false);
835 } else if (inst->Src[0].Register.File == TGSI_FILE_IMAGE ||
836 tgsi_is_bindless_image_file(inst->Src[0].Register.File)) {
837 unsigned target = inst->Memory.Texture;
838
839 image_fetch_rsrc(bld_base, &inst->Src[0], true, target, &rsrc);
840 image_fetch_coords(bld_base, inst, 1, rsrc,
841 &emit_data->args[emit_data->arg_count + 1]);
842
843 if (target == TGSI_TEXTURE_BUFFER) {
844 buffer_append_args(ctx, emit_data, rsrc,
845 emit_data->args[emit_data->arg_count + 1],
846 ctx->i32_0, true, false);
847 } else {
848 emit_data->args[emit_data->arg_count] = rsrc;
849 }
850 }
851 }
852
853 static void atomic_emit_memory(struct si_shader_context *ctx,
854 struct lp_build_emit_data *emit_data) {
855 LLVMBuilderRef builder = ctx->ac.builder;
856 const struct tgsi_full_instruction * inst = emit_data->inst;
857 LLVMValueRef ptr, result, arg;
858
859 ptr = get_memory_ptr(ctx, inst, ctx->i32, 1);
860
861 arg = lp_build_emit_fetch(&ctx->bld_base, inst, 2, 0);
862 arg = ac_to_integer(&ctx->ac, arg);
863
864 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
865 LLVMValueRef new_data;
866 new_data = lp_build_emit_fetch(&ctx->bld_base,
867 inst, 3, 0);
868
869 new_data = ac_to_integer(&ctx->ac, new_data);
870
871 result = LLVMBuildAtomicCmpXchg(builder, ptr, arg, new_data,
872 LLVMAtomicOrderingSequentiallyConsistent,
873 LLVMAtomicOrderingSequentiallyConsistent,
874 false);
875
876 result = LLVMBuildExtractValue(builder, result, 0, "");
877 } else {
878 LLVMAtomicRMWBinOp op;
879
880 switch(inst->Instruction.Opcode) {
881 case TGSI_OPCODE_ATOMUADD:
882 op = LLVMAtomicRMWBinOpAdd;
883 break;
884 case TGSI_OPCODE_ATOMXCHG:
885 op = LLVMAtomicRMWBinOpXchg;
886 break;
887 case TGSI_OPCODE_ATOMAND:
888 op = LLVMAtomicRMWBinOpAnd;
889 break;
890 case TGSI_OPCODE_ATOMOR:
891 op = LLVMAtomicRMWBinOpOr;
892 break;
893 case TGSI_OPCODE_ATOMXOR:
894 op = LLVMAtomicRMWBinOpXor;
895 break;
896 case TGSI_OPCODE_ATOMUMIN:
897 op = LLVMAtomicRMWBinOpUMin;
898 break;
899 case TGSI_OPCODE_ATOMUMAX:
900 op = LLVMAtomicRMWBinOpUMax;
901 break;
902 case TGSI_OPCODE_ATOMIMIN:
903 op = LLVMAtomicRMWBinOpMin;
904 break;
905 case TGSI_OPCODE_ATOMIMAX:
906 op = LLVMAtomicRMWBinOpMax;
907 break;
908 default:
909 unreachable("unknown atomic opcode");
910 }
911
912 result = LLVMBuildAtomicRMW(builder, op, ptr, arg,
913 LLVMAtomicOrderingSequentiallyConsistent,
914 false);
915 }
916 emit_data->output[emit_data->chan] = LLVMBuildBitCast(builder, result, emit_data->dst_type, "");
917 }
918
919 static void atomic_emit(
920 const struct lp_build_tgsi_action *action,
921 struct lp_build_tgsi_context *bld_base,
922 struct lp_build_emit_data *emit_data)
923 {
924 struct si_shader_context *ctx = si_shader_context(bld_base);
925 LLVMBuilderRef builder = ctx->ac.builder;
926 const struct tgsi_full_instruction * inst = emit_data->inst;
927 LLVMValueRef tmp;
928
929 if (inst->Src[0].Register.File == TGSI_FILE_MEMORY) {
930 atomic_emit_memory(ctx, emit_data);
931 return;
932 }
933
934 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER ||
935 inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
936 char intrinsic_name[40];
937 snprintf(intrinsic_name, sizeof(intrinsic_name),
938 "llvm.amdgcn.buffer.atomic.%s", action->intr_name);
939 tmp = lp_build_intrinsic(
940 builder, intrinsic_name, ctx->i32,
941 emit_data->args, emit_data->arg_count, 0);
942 emit_data->output[emit_data->chan] = ac_to_float(&ctx->ac, tmp);
943 } else {
944 unsigned num_data = inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS ? 2 : 1;
945 struct ac_image_args args = {};
946
947 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
948 args.opcode = ac_image_atomic_cmpswap;
949 } else {
950 args.opcode = ac_image_atomic;
951 switch (inst->Instruction.Opcode) {
952 case TGSI_OPCODE_ATOMXCHG: args.atomic = ac_atomic_swap; break;
953 case TGSI_OPCODE_ATOMUADD: args.atomic = ac_atomic_add; break;
954 case TGSI_OPCODE_ATOMAND: args.atomic = ac_atomic_and; break;
955 case TGSI_OPCODE_ATOMOR: args.atomic = ac_atomic_or; break;
956 case TGSI_OPCODE_ATOMXOR: args.atomic = ac_atomic_xor; break;
957 case TGSI_OPCODE_ATOMUMIN: args.atomic = ac_atomic_umin; break;
958 case TGSI_OPCODE_ATOMUMAX: args.atomic = ac_atomic_umax; break;
959 case TGSI_OPCODE_ATOMIMIN: args.atomic = ac_atomic_smin; break;
960 case TGSI_OPCODE_ATOMIMAX: args.atomic = ac_atomic_smax; break;
961 default: unreachable("unhandled image atomic");
962 }
963 }
964
965 for (unsigned i = 0; i < num_data; ++i)
966 args.data[i] = emit_data->args[i];
967
968 args.resource = emit_data->args[num_data];
969 memcpy(args.coords, &emit_data->args[num_data + 1], sizeof(args.coords));
970 args.dim = ac_image_dim_from_tgsi_target(ctx->screen, inst->Memory.Texture);
971
972 emit_data->output[emit_data->chan] =
973 ac_to_float(&ctx->ac, ac_build_image_opcode(&ctx->ac, &args));
974 }
975 }
976
977 static void set_tex_fetch_args(struct si_shader_context *ctx,
978 struct lp_build_emit_data *emit_data,
979 struct ac_image_args *args,
980 unsigned target)
981 {
982 args->dim = ac_texture_dim_from_tgsi_target(ctx->screen, target);
983 args->unorm = target == TGSI_TEXTURE_RECT ||
984 target == TGSI_TEXTURE_SHADOWRECT;
985
986 /* Ugly, but we seem to have no other choice right now. */
987 STATIC_ASSERT(sizeof(*args) <= sizeof(emit_data->args));
988 memcpy(emit_data->args, args, sizeof(*args));
989 }
990
991 static LLVMValueRef fix_resinfo(struct si_shader_context *ctx,
992 unsigned target, LLVMValueRef out)
993 {
994 LLVMBuilderRef builder = ctx->ac.builder;
995
996 /* 1D textures are allocated and used as 2D on GFX9. */
997 if (ctx->screen->info.chip_class >= GFX9 &&
998 (target == TGSI_TEXTURE_1D_ARRAY ||
999 target == TGSI_TEXTURE_SHADOW1D_ARRAY)) {
1000 LLVMValueRef layers =
1001 LLVMBuildExtractElement(builder, out,
1002 LLVMConstInt(ctx->i32, 2, 0), "");
1003 out = LLVMBuildInsertElement(builder, out, layers,
1004 ctx->i32_1, "");
1005 }
1006
1007 /* Divide the number of layers by 6 to get the number of cubes. */
1008 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1009 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1010 LLVMValueRef imm2 = LLVMConstInt(ctx->i32, 2, 0);
1011
1012 LLVMValueRef z = LLVMBuildExtractElement(builder, out, imm2, "");
1013 z = LLVMBuildSDiv(builder, z, LLVMConstInt(ctx->i32, 6, 0), "");
1014
1015 out = LLVMBuildInsertElement(builder, out, z, imm2, "");
1016 }
1017 return out;
1018 }
1019
1020 static void resq_fetch_args(
1021 struct lp_build_tgsi_context * bld_base,
1022 struct lp_build_emit_data * emit_data)
1023 {
1024 struct si_shader_context *ctx = si_shader_context(bld_base);
1025 const struct tgsi_full_instruction *inst = emit_data->inst;
1026 const struct tgsi_full_src_register *reg = &inst->Src[0];
1027
1028 emit_data->dst_type = ctx->v4i32;
1029
1030 if (reg->Register.File == TGSI_FILE_BUFFER) {
1031 emit_data->args[0] = shader_buffer_fetch_rsrc(ctx, reg, false);
1032 emit_data->arg_count = 1;
1033 } else if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
1034 image_fetch_rsrc(bld_base, reg, false, inst->Memory.Texture,
1035 &emit_data->args[0]);
1036 emit_data->arg_count = 1;
1037 } else {
1038 struct ac_image_args args = {};
1039 unsigned image_target;
1040
1041 if (inst->Memory.Texture == TGSI_TEXTURE_3D)
1042 image_target = TGSI_TEXTURE_2D_ARRAY;
1043 else
1044 image_target = inst->Memory.Texture;
1045
1046 image_fetch_rsrc(bld_base, reg, false, inst->Memory.Texture,
1047 &args.resource);
1048 args.lod = ctx->i32_0;
1049 args.dmask = 0xf;
1050 set_tex_fetch_args(ctx, emit_data, &args, image_target);
1051 }
1052 }
1053
1054 static void resq_emit(
1055 const struct lp_build_tgsi_action *action,
1056 struct lp_build_tgsi_context *bld_base,
1057 struct lp_build_emit_data *emit_data)
1058 {
1059 struct si_shader_context *ctx = si_shader_context(bld_base);
1060 LLVMBuilderRef builder = ctx->ac.builder;
1061 const struct tgsi_full_instruction *inst = emit_data->inst;
1062 LLVMValueRef out;
1063
1064 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
1065 out = LLVMBuildExtractElement(builder, emit_data->args[0],
1066 LLVMConstInt(ctx->i32, 2, 0), "");
1067 } else if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
1068 out = get_buffer_size(bld_base, emit_data->args[0]);
1069 } else {
1070 struct ac_image_args args;
1071
1072 memcpy(&args, emit_data->args, sizeof(args)); /* ugly */
1073 args.opcode = ac_image_get_resinfo;
1074 out = ac_build_image_opcode(&ctx->ac, &args);
1075
1076 out = fix_resinfo(ctx, inst->Memory.Texture, out);
1077 }
1078
1079 emit_data->output[emit_data->chan] = out;
1080 }
1081
1082 /**
1083 * Load an image view, fmask view. or sampler state descriptor.
1084 */
1085 LLVMValueRef si_load_sampler_desc(struct si_shader_context *ctx,
1086 LLVMValueRef list, LLVMValueRef index,
1087 enum ac_descriptor_type type)
1088 {
1089 LLVMBuilderRef builder = ctx->ac.builder;
1090
1091 switch (type) {
1092 case AC_DESC_IMAGE:
1093 /* The image is at [0:7]. */
1094 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 2, 0), "");
1095 break;
1096 case AC_DESC_BUFFER:
1097 /* The buffer is in [4:7]. */
1098 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
1099 index = LLVMBuildAdd(builder, index, ctx->i32_1, "");
1100 list = LLVMBuildPointerCast(builder, list,
1101 ac_array_in_const32_addr_space(ctx->v4i32), "");
1102 break;
1103 case AC_DESC_FMASK:
1104 /* The FMASK is at [8:15]. */
1105 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 2, 0), "");
1106 index = LLVMBuildAdd(builder, index, ctx->i32_1, "");
1107 break;
1108 case AC_DESC_SAMPLER:
1109 /* The sampler state is at [12:15]. */
1110 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
1111 index = LLVMBuildAdd(builder, index, LLVMConstInt(ctx->i32, 3, 0), "");
1112 list = LLVMBuildPointerCast(builder, list,
1113 ac_array_in_const32_addr_space(ctx->v4i32), "");
1114 break;
1115 }
1116
1117 return ac_build_load_to_sgpr(&ctx->ac, list, index);
1118 }
1119
1120 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
1121 *
1122 * SI-CI:
1123 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
1124 * filtering manually. The driver sets img7 to a mask clearing
1125 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
1126 * s_and_b32 samp0, samp0, img7
1127 *
1128 * VI:
1129 * The ANISO_OVERRIDE sampler field enables this fix in TA.
1130 */
1131 static LLVMValueRef sici_fix_sampler_aniso(struct si_shader_context *ctx,
1132 LLVMValueRef res, LLVMValueRef samp)
1133 {
1134 LLVMValueRef img7, samp0;
1135
1136 if (ctx->screen->info.chip_class >= VI)
1137 return samp;
1138
1139 img7 = LLVMBuildExtractElement(ctx->ac.builder, res,
1140 LLVMConstInt(ctx->i32, 7, 0), "");
1141 samp0 = LLVMBuildExtractElement(ctx->ac.builder, samp,
1142 ctx->i32_0, "");
1143 samp0 = LLVMBuildAnd(ctx->ac.builder, samp0, img7, "");
1144 return LLVMBuildInsertElement(ctx->ac.builder, samp, samp0,
1145 ctx->i32_0, "");
1146 }
1147
1148 static void tex_fetch_ptrs(
1149 struct lp_build_tgsi_context *bld_base,
1150 struct lp_build_emit_data *emit_data,
1151 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr, LLVMValueRef *fmask_ptr)
1152 {
1153 struct si_shader_context *ctx = si_shader_context(bld_base);
1154 LLVMValueRef list = LLVMGetParam(ctx->main_fn, ctx->param_samplers_and_images);
1155 const struct tgsi_full_instruction *inst = emit_data->inst;
1156 const struct tgsi_full_src_register *reg;
1157 unsigned target = inst->Texture.Texture;
1158 unsigned sampler_src;
1159 LLVMValueRef index;
1160
1161 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1162 reg = &emit_data->inst->Src[sampler_src];
1163
1164 if (reg->Register.Indirect) {
1165 index = si_get_bounded_indirect_index(ctx,
1166 &reg->Indirect,
1167 reg->Register.Index,
1168 ctx->num_samplers);
1169 index = LLVMBuildAdd(ctx->ac.builder, index,
1170 LLVMConstInt(ctx->i32, SI_NUM_IMAGES / 2, 0), "");
1171 } else {
1172 index = LLVMConstInt(ctx->i32,
1173 si_get_sampler_slot(reg->Register.Index), 0);
1174 }
1175
1176 if (reg->Register.File != TGSI_FILE_SAMPLER) {
1177 /* Bindless descriptors are accessible from a different pair of
1178 * user SGPR indices.
1179 */
1180 list = LLVMGetParam(ctx->main_fn,
1181 ctx->param_bindless_samplers_and_images);
1182 index = lp_build_emit_fetch_src(bld_base, reg,
1183 TGSI_TYPE_UNSIGNED, 0);
1184 }
1185
1186 if (target == TGSI_TEXTURE_BUFFER)
1187 *res_ptr = si_load_sampler_desc(ctx, list, index, AC_DESC_BUFFER);
1188 else
1189 *res_ptr = si_load_sampler_desc(ctx, list, index, AC_DESC_IMAGE);
1190
1191 if (samp_ptr)
1192 *samp_ptr = NULL;
1193 if (fmask_ptr)
1194 *fmask_ptr = NULL;
1195
1196 if (target == TGSI_TEXTURE_2D_MSAA ||
1197 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1198 if (fmask_ptr)
1199 *fmask_ptr = si_load_sampler_desc(ctx, list, index,
1200 AC_DESC_FMASK);
1201 } else if (target != TGSI_TEXTURE_BUFFER) {
1202 if (samp_ptr) {
1203 *samp_ptr = si_load_sampler_desc(ctx, list, index,
1204 AC_DESC_SAMPLER);
1205 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
1206 }
1207 }
1208 }
1209
1210 static void txq_fetch_args(
1211 struct lp_build_tgsi_context *bld_base,
1212 struct lp_build_emit_data *emit_data)
1213 {
1214 struct si_shader_context *ctx = si_shader_context(bld_base);
1215 const struct tgsi_full_instruction *inst = emit_data->inst;
1216 unsigned target = inst->Texture.Texture;
1217 struct ac_image_args args = {};
1218
1219 tex_fetch_ptrs(bld_base, emit_data, &args.resource, NULL, NULL);
1220
1221 if (target == TGSI_TEXTURE_BUFFER) {
1222 /* Read the size from the buffer descriptor directly. */
1223 emit_data->args[0] = get_buffer_size(bld_base, args.resource);
1224 return;
1225 }
1226
1227 /* Textures - set the mip level. */
1228 args.lod = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1229 args.dmask = 0xf;
1230
1231 set_tex_fetch_args(ctx, emit_data, &args, target);
1232 }
1233
1234 static void txq_emit(const struct lp_build_tgsi_action *action,
1235 struct lp_build_tgsi_context *bld_base,
1236 struct lp_build_emit_data *emit_data)
1237 {
1238 struct si_shader_context *ctx = si_shader_context(bld_base);
1239 struct ac_image_args args;
1240 unsigned target = emit_data->inst->Texture.Texture;
1241
1242 if (target == TGSI_TEXTURE_BUFFER) {
1243 /* Just return the buffer size. */
1244 emit_data->output[emit_data->chan] = emit_data->args[0];
1245 return;
1246 }
1247
1248 memcpy(&args, emit_data->args, sizeof(args)); /* ugly */
1249
1250 args.opcode = ac_image_get_resinfo;
1251 LLVMValueRef result = ac_build_image_opcode(&ctx->ac, &args);
1252
1253 emit_data->output[emit_data->chan] = fix_resinfo(ctx, target, result);
1254 }
1255
1256 static void tex_fetch_args(
1257 struct lp_build_tgsi_context *bld_base,
1258 struct lp_build_emit_data *emit_data)
1259 {
1260 struct si_shader_context *ctx = si_shader_context(bld_base);
1261 const struct tgsi_full_instruction *inst = emit_data->inst;
1262 unsigned opcode = inst->Instruction.Opcode;
1263 unsigned target = inst->Texture.Texture;
1264 struct ac_image_args args = {};
1265 int ref_pos = tgsi_util_get_shadow_ref_src_index(target);
1266 unsigned chan;
1267 bool has_offset = inst->Texture.NumOffsets > 0;
1268 LLVMValueRef fmask_ptr = NULL;
1269
1270 tex_fetch_ptrs(bld_base, emit_data, &args.resource, &args.sampler, &fmask_ptr);
1271
1272 if (target == TGSI_TEXTURE_BUFFER) {
1273 emit_data->dst_type = ctx->v4f32;
1274 emit_data->args[0] = args.resource;
1275 emit_data->args[1] = ctx->i32_0;
1276 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
1277 emit_data->arg_count = 3;
1278 return;
1279 }
1280
1281 /* Fetch and project texture coordinates */
1282 args.coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
1283 for (chan = 0; chan < 3; chan++) {
1284 args.coords[chan] = lp_build_emit_fetch(bld_base,
1285 emit_data->inst, 0,
1286 chan);
1287 if (opcode == TGSI_OPCODE_TXP)
1288 args.coords[chan] = lp_build_emit_llvm_binary(
1289 bld_base, TGSI_OPCODE_DIV,
1290 args.coords[chan], args.coords[3]);
1291 }
1292
1293 if (opcode == TGSI_OPCODE_TXP)
1294 args.coords[3] = ctx->ac.f32_1;
1295
1296 /* Pack offsets. */
1297 if (has_offset &&
1298 opcode != TGSI_OPCODE_TXF &&
1299 opcode != TGSI_OPCODE_TXF_LZ) {
1300 /* The offsets are six-bit signed integers packed like this:
1301 * X=[5:0], Y=[13:8], and Z=[21:16].
1302 */
1303 LLVMValueRef offset[3], pack;
1304
1305 assert(inst->Texture.NumOffsets == 1);
1306
1307 for (chan = 0; chan < 3; chan++) {
1308 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1309 emit_data->inst, 0, chan);
1310 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
1311 LLVMConstInt(ctx->i32, 0x3f, 0), "");
1312 if (chan)
1313 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
1314 LLVMConstInt(ctx->i32, chan*8, 0), "");
1315 }
1316
1317 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
1318 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
1319 args.offset = pack;
1320 }
1321
1322 /* Pack LOD bias value */
1323 if (opcode == TGSI_OPCODE_TXB)
1324 args.bias = args.coords[3];
1325 if (opcode == TGSI_OPCODE_TXB2)
1326 args.bias = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
1327
1328 /* Pack depth comparison value */
1329 if (tgsi_is_shadow_target(target) && opcode != TGSI_OPCODE_LODQ) {
1330 LLVMValueRef z;
1331
1332 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1333 z = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
1334 } else {
1335 assert(ref_pos >= 0);
1336 z = args.coords[ref_pos];
1337 }
1338
1339 /* Section 8.23.1 (Depth Texture Comparison Mode) of the
1340 * OpenGL 4.5 spec says:
1341 *
1342 * "If the texture’s internal format indicates a fixed-point
1343 * depth texture, then D_t and D_ref are clamped to the
1344 * range [0, 1]; otherwise no clamping is performed."
1345 *
1346 * TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
1347 * so the depth comparison value isn't clamped for Z16 and
1348 * Z24 anymore. Do it manually here.
1349 */
1350 if (ctx->screen->info.chip_class >= VI) {
1351 LLVMValueRef upgraded;
1352 LLVMValueRef clamped;
1353 upgraded = LLVMBuildExtractElement(ctx->ac.builder, args.sampler,
1354 LLVMConstInt(ctx->i32, 3, false), "");
1355 upgraded = LLVMBuildLShr(ctx->ac.builder, upgraded,
1356 LLVMConstInt(ctx->i32, 29, false), "");
1357 upgraded = LLVMBuildTrunc(ctx->ac.builder, upgraded, ctx->i1, "");
1358 clamped = ac_build_clamp(&ctx->ac, z);
1359 z = LLVMBuildSelect(ctx->ac.builder, upgraded, clamped, z, "");
1360 }
1361
1362 args.compare = z;
1363 }
1364
1365 /* Pack user derivatives */
1366 if (opcode == TGSI_OPCODE_TXD) {
1367 int param, num_src_deriv_channels, num_dst_deriv_channels;
1368
1369 switch (target) {
1370 case TGSI_TEXTURE_3D:
1371 num_src_deriv_channels = 3;
1372 num_dst_deriv_channels = 3;
1373 break;
1374 case TGSI_TEXTURE_2D:
1375 case TGSI_TEXTURE_SHADOW2D:
1376 case TGSI_TEXTURE_RECT:
1377 case TGSI_TEXTURE_SHADOWRECT:
1378 case TGSI_TEXTURE_2D_ARRAY:
1379 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1380 num_src_deriv_channels = 2;
1381 num_dst_deriv_channels = 2;
1382 break;
1383 case TGSI_TEXTURE_CUBE:
1384 case TGSI_TEXTURE_SHADOWCUBE:
1385 case TGSI_TEXTURE_CUBE_ARRAY:
1386 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1387 /* Cube derivatives will be converted to 2D. */
1388 num_src_deriv_channels = 3;
1389 num_dst_deriv_channels = 3;
1390 break;
1391 case TGSI_TEXTURE_1D:
1392 case TGSI_TEXTURE_SHADOW1D:
1393 case TGSI_TEXTURE_1D_ARRAY:
1394 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1395 num_src_deriv_channels = 1;
1396
1397 /* 1D textures are allocated and used as 2D on GFX9. */
1398 if (ctx->screen->info.chip_class >= GFX9) {
1399 num_dst_deriv_channels = 2;
1400 } else {
1401 num_dst_deriv_channels = 1;
1402 }
1403 break;
1404 default:
1405 unreachable("invalid target");
1406 }
1407
1408 for (param = 0; param < 2; param++) {
1409 for (chan = 0; chan < num_src_deriv_channels; chan++)
1410 args.derivs[param * num_dst_deriv_channels + chan] =
1411 lp_build_emit_fetch(bld_base, inst, param+1, chan);
1412
1413 /* Fill in the rest with zeros. */
1414 for (chan = num_src_deriv_channels;
1415 chan < num_dst_deriv_channels; chan++)
1416 args.derivs[param * num_dst_deriv_channels + chan] =
1417 ctx->ac.f32_0;
1418 }
1419 }
1420
1421 if (target == TGSI_TEXTURE_CUBE ||
1422 target == TGSI_TEXTURE_CUBE_ARRAY ||
1423 target == TGSI_TEXTURE_SHADOWCUBE ||
1424 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1425 ac_prepare_cube_coords(&ctx->ac,
1426 opcode == TGSI_OPCODE_TXD,
1427 target == TGSI_TEXTURE_CUBE_ARRAY ||
1428 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY,
1429 opcode == TGSI_OPCODE_LODQ,
1430 args.coords, args.derivs);
1431 } else if (tgsi_is_array_sampler(target) &&
1432 opcode != TGSI_OPCODE_TXF &&
1433 opcode != TGSI_OPCODE_TXF_LZ &&
1434 ctx->screen->info.chip_class <= VI) {
1435 unsigned array_coord = target == TGSI_TEXTURE_1D_ARRAY ? 1 : 2;
1436 args.coords[array_coord] =
1437 ac_build_intrinsic(&ctx->ac, "llvm.rint.f32", ctx->f32,
1438 &args.coords[array_coord], 1, 0);
1439 }
1440
1441 /* 1D textures are allocated and used as 2D on GFX9. */
1442 if (ctx->screen->info.chip_class >= GFX9) {
1443 LLVMValueRef filler;
1444
1445 /* Use 0.5, so that we don't sample the border color. */
1446 if (opcode == TGSI_OPCODE_TXF ||
1447 opcode == TGSI_OPCODE_TXF_LZ)
1448 filler = ctx->i32_0;
1449 else
1450 filler = LLVMConstReal(ctx->f32, 0.5);
1451
1452 if (target == TGSI_TEXTURE_1D ||
1453 target == TGSI_TEXTURE_SHADOW1D) {
1454 args.coords[1] = filler;
1455 } else if (target == TGSI_TEXTURE_1D_ARRAY ||
1456 target == TGSI_TEXTURE_SHADOW1D_ARRAY) {
1457 args.coords[2] = args.coords[1];
1458 args.coords[1] = filler;
1459 }
1460 }
1461
1462 /* Pack LOD or sample index */
1463 if (opcode == TGSI_OPCODE_TXL)
1464 args.lod = args.coords[3];
1465 else if (opcode == TGSI_OPCODE_TXL2)
1466 args.lod = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
1467 else if (opcode == TGSI_OPCODE_TXF) {
1468 if (target == TGSI_TEXTURE_2D_MSAA) {
1469 /* No LOD, but move sample index into the right place. */
1470 args.coords[2] = args.coords[3];
1471 } else if (target != TGSI_TEXTURE_2D_ARRAY_MSAA) {
1472 args.lod = args.coords[3];
1473 }
1474 }
1475
1476 if (target == TGSI_TEXTURE_2D_MSAA ||
1477 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1478 ac_apply_fmask_to_sample(&ctx->ac, fmask_ptr, args.coords,
1479 target == TGSI_TEXTURE_2D_ARRAY_MSAA);
1480 }
1481
1482 if (opcode == TGSI_OPCODE_TXF ||
1483 opcode == TGSI_OPCODE_TXF_LZ) {
1484 /* add tex offsets */
1485 if (inst->Texture.NumOffsets) {
1486 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1487 const struct tgsi_texture_offset *off = inst->TexOffsets;
1488
1489 assert(inst->Texture.NumOffsets == 1);
1490
1491 switch (target) {
1492 case TGSI_TEXTURE_3D:
1493 args.coords[2] = lp_build_add(uint_bld, args.coords[2],
1494 ctx->imms[off->Index * TGSI_NUM_CHANNELS + off->SwizzleZ]);
1495 /* fall through */
1496 case TGSI_TEXTURE_2D:
1497 case TGSI_TEXTURE_SHADOW2D:
1498 case TGSI_TEXTURE_RECT:
1499 case TGSI_TEXTURE_SHADOWRECT:
1500 case TGSI_TEXTURE_2D_ARRAY:
1501 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1502 args.coords[1] =
1503 lp_build_add(uint_bld, args.coords[1],
1504 ctx->imms[off->Index * TGSI_NUM_CHANNELS + off->SwizzleY]);
1505 /* fall through */
1506 case TGSI_TEXTURE_1D:
1507 case TGSI_TEXTURE_SHADOW1D:
1508 case TGSI_TEXTURE_1D_ARRAY:
1509 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1510 args.coords[0] =
1511 lp_build_add(uint_bld, args.coords[0],
1512 ctx->imms[off->Index * TGSI_NUM_CHANNELS + off->SwizzleX]);
1513 break;
1514 /* texture offsets do not apply to other texture targets */
1515 }
1516 }
1517 }
1518
1519 args.dmask = 0xf;
1520
1521 if (opcode == TGSI_OPCODE_TG4) {
1522 unsigned gather_comp = 0;
1523
1524 /* DMASK was repurposed for GATHER4. 4 components are always
1525 * returned and DMASK works like a swizzle - it selects
1526 * the component to fetch. The only valid DMASK values are
1527 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1528 * (red,red,red,red) etc.) The ISA document doesn't mention
1529 * this.
1530 */
1531
1532 /* Get the component index from src1.x for Gather4. */
1533 if (!tgsi_is_shadow_target(target)) {
1534 LLVMValueRef comp_imm;
1535 struct tgsi_src_register src1 = inst->Src[1].Register;
1536
1537 assert(src1.File == TGSI_FILE_IMMEDIATE);
1538
1539 comp_imm = ctx->imms[src1.Index * TGSI_NUM_CHANNELS + src1.SwizzleX];
1540 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1541 gather_comp = CLAMP(gather_comp, 0, 3);
1542 }
1543
1544 args.dmask = 1 << gather_comp;
1545 }
1546
1547 set_tex_fetch_args(ctx, emit_data, &args, target);
1548 }
1549
1550 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1551 * incorrectly forces nearest filtering if the texture format is integer.
1552 * The only effect it has on Gather4, which always returns 4 texels for
1553 * bilinear filtering, is that the final coordinates are off by 0.5 of
1554 * the texel size.
1555 *
1556 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1557 * or (0.5 / size) from the normalized coordinates.
1558 *
1559 * However, cube textures with 8_8_8_8 data formats require a different
1560 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1561 * precision in 32-bit data formats, so it needs to be applied dynamically at
1562 * runtime. In this case, return an i1 value that indicates whether the
1563 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1564 */
1565 static LLVMValueRef
1566 si_lower_gather4_integer(struct si_shader_context *ctx,
1567 struct ac_image_args *args,
1568 unsigned target,
1569 enum tgsi_return_type return_type)
1570 {
1571 LLVMBuilderRef builder = ctx->ac.builder;
1572 LLVMValueRef wa_8888 = NULL;
1573 LLVMValueRef half_texel[2];
1574
1575 assert(return_type == TGSI_RETURN_TYPE_SINT ||
1576 return_type == TGSI_RETURN_TYPE_UINT);
1577
1578 if (target == TGSI_TEXTURE_CUBE ||
1579 target == TGSI_TEXTURE_CUBE_ARRAY) {
1580 LLVMValueRef formats;
1581 LLVMValueRef data_format;
1582 LLVMValueRef wa_formats;
1583
1584 formats = LLVMBuildExtractElement(builder, args->resource, ctx->i32_1, "");
1585
1586 data_format = LLVMBuildLShr(builder, formats,
1587 LLVMConstInt(ctx->i32, 20, false), "");
1588 data_format = LLVMBuildAnd(builder, data_format,
1589 LLVMConstInt(ctx->i32, (1u << 6) - 1, false), "");
1590 wa_8888 = LLVMBuildICmp(
1591 builder, LLVMIntEQ, data_format,
1592 LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false),
1593 "");
1594
1595 uint32_t wa_num_format =
1596 return_type == TGSI_RETURN_TYPE_UINT ?
1597 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_USCALED) :
1598 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_SSCALED);
1599 wa_formats = LLVMBuildAnd(builder, formats,
1600 LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false),
1601 "");
1602 wa_formats = LLVMBuildOr(builder, wa_formats,
1603 LLVMConstInt(ctx->i32, wa_num_format, false), "");
1604
1605 formats = LLVMBuildSelect(builder, wa_8888, wa_formats, formats, "");
1606 args->resource = LLVMBuildInsertElement(
1607 builder, args->resource, formats, ctx->i32_1, "");
1608 }
1609
1610 if (target == TGSI_TEXTURE_RECT ||
1611 target == TGSI_TEXTURE_SHADOWRECT) {
1612 assert(!wa_8888);
1613 half_texel[0] = half_texel[1] = LLVMConstReal(ctx->f32, -0.5);
1614 } else {
1615 struct tgsi_full_instruction txq_inst = {};
1616 struct ac_image_args txq_args = {};
1617 struct lp_build_emit_data txq_emit_data = {};
1618 struct lp_build_if_state if_ctx;
1619
1620 if (wa_8888) {
1621 /* Skip the texture size query entirely if we don't need it. */
1622 lp_build_if(&if_ctx, &ctx->gallivm, LLVMBuildNot(builder, wa_8888, ""));
1623 }
1624
1625 /* Query the texture size. */
1626 txq_inst.Texture.Texture = target;
1627 txq_emit_data.inst = &txq_inst;
1628 txq_emit_data.dst_type = ctx->v4i32;
1629 txq_args.resource = args->resource;
1630 txq_args.sampler = args->sampler;
1631 txq_args.lod = ctx->ac.i32_0;
1632 txq_args.dmask = 0xf;
1633 set_tex_fetch_args(ctx, &txq_emit_data, &txq_args, target);
1634 txq_emit(NULL, &ctx->bld_base, &txq_emit_data);
1635
1636 /* Compute -0.5 / size. */
1637 for (unsigned c = 0; c < 2; c++) {
1638 half_texel[c] =
1639 LLVMBuildExtractElement(builder, txq_emit_data.output[0],
1640 LLVMConstInt(ctx->i32, c, 0), "");
1641 half_texel[c] = LLVMBuildUIToFP(builder, half_texel[c], ctx->f32, "");
1642 half_texel[c] =
1643 lp_build_emit_llvm_unary(&ctx->bld_base,
1644 TGSI_OPCODE_RCP, half_texel[c]);
1645 half_texel[c] = LLVMBuildFMul(builder, half_texel[c],
1646 LLVMConstReal(ctx->f32, -0.5), "");
1647 }
1648
1649 if (wa_8888) {
1650 lp_build_endif(&if_ctx);
1651
1652 LLVMBasicBlockRef bb[2] = { if_ctx.true_block, if_ctx.entry_block };
1653
1654 for (unsigned c = 0; c < 2; c++) {
1655 LLVMValueRef values[2] = { half_texel[c], ctx->ac.f32_0 };
1656 half_texel[c] = ac_build_phi(&ctx->ac, ctx->f32, 2,
1657 values, bb);
1658 }
1659 }
1660 }
1661
1662 for (unsigned c = 0; c < 2; c++) {
1663 LLVMValueRef tmp;
1664 tmp = ac_to_float(&ctx->ac, args->coords[c]);
1665 tmp = LLVMBuildFAdd(builder, tmp, half_texel[c], "");
1666 args->coords[c] = ac_to_integer(&ctx->ac, tmp);
1667 }
1668
1669 return wa_8888;
1670 }
1671
1672 /* The second half of the cube texture 8_8_8_8 integer workaround: adjust the
1673 * result after the gather operation.
1674 */
1675 static LLVMValueRef
1676 si_fix_gather4_integer_result(struct si_shader_context *ctx,
1677 LLVMValueRef result,
1678 enum tgsi_return_type return_type,
1679 LLVMValueRef wa)
1680 {
1681 LLVMBuilderRef builder = ctx->ac.builder;
1682
1683 assert(return_type == TGSI_RETURN_TYPE_SINT ||
1684 return_type == TGSI_RETURN_TYPE_UINT);
1685
1686 for (unsigned chan = 0; chan < 4; ++chan) {
1687 LLVMValueRef chanv = LLVMConstInt(ctx->i32, chan, false);
1688 LLVMValueRef value;
1689 LLVMValueRef wa_value;
1690
1691 value = LLVMBuildExtractElement(builder, result, chanv, "");
1692
1693 if (return_type == TGSI_RETURN_TYPE_UINT)
1694 wa_value = LLVMBuildFPToUI(builder, value, ctx->i32, "");
1695 else
1696 wa_value = LLVMBuildFPToSI(builder, value, ctx->i32, "");
1697 wa_value = ac_to_float(&ctx->ac, wa_value);
1698 value = LLVMBuildSelect(builder, wa, wa_value, value, "");
1699
1700 result = LLVMBuildInsertElement(builder, result, value, chanv, "");
1701 }
1702
1703 return result;
1704 }
1705
1706 static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
1707 struct lp_build_tgsi_context *bld_base,
1708 struct lp_build_emit_data *emit_data)
1709 {
1710 struct si_shader_context *ctx = si_shader_context(bld_base);
1711 const struct tgsi_full_instruction *inst = emit_data->inst;
1712 struct ac_image_args args;
1713 unsigned opcode = inst->Instruction.Opcode;
1714 unsigned target = inst->Texture.Texture;
1715
1716 if (target == TGSI_TEXTURE_BUFFER) {
1717 unsigned num_channels =
1718 util_last_bit(inst->Dst[0].Register.WriteMask);
1719 LLVMValueRef result =
1720 ac_build_buffer_load_format(&ctx->ac,
1721 emit_data->args[0],
1722 emit_data->args[2],
1723 emit_data->args[1],
1724 num_channels, false, true);
1725 emit_data->output[emit_data->chan] =
1726 ac_build_expand_to_vec4(&ctx->ac, result, num_channels);
1727 return;
1728 }
1729
1730 memcpy(&args, emit_data->args, sizeof(args)); /* ugly */
1731
1732 args.opcode = ac_image_sample;
1733
1734 switch (opcode) {
1735 case TGSI_OPCODE_TXF:
1736 case TGSI_OPCODE_TXF_LZ:
1737 args.opcode = opcode == TGSI_OPCODE_TXF_LZ ||
1738 target == TGSI_TEXTURE_2D_MSAA ||
1739 target == TGSI_TEXTURE_2D_ARRAY_MSAA ?
1740 ac_image_load : ac_image_load_mip;
1741 break;
1742 case TGSI_OPCODE_LODQ:
1743 args.opcode = ac_image_get_lod;
1744 break;
1745 case TGSI_OPCODE_TEX:
1746 case TGSI_OPCODE_TEX2:
1747 case TGSI_OPCODE_TXP:
1748 if (ctx->type != PIPE_SHADER_FRAGMENT)
1749 args.level_zero = true;
1750 break;
1751 case TGSI_OPCODE_TEX_LZ:
1752 args.level_zero = true;
1753 break;
1754 case TGSI_OPCODE_TXB:
1755 case TGSI_OPCODE_TXB2:
1756 assert(ctx->type == PIPE_SHADER_FRAGMENT);
1757 break;
1758 case TGSI_OPCODE_TXL:
1759 case TGSI_OPCODE_TXL2:
1760 break;
1761 case TGSI_OPCODE_TXD:
1762 break;
1763 case TGSI_OPCODE_TG4:
1764 args.opcode = ac_image_gather4;
1765 args.level_zero = true;
1766 break;
1767 default:
1768 assert(0);
1769 return;
1770 }
1771
1772 /* The hardware needs special lowering for Gather4 with integer formats. */
1773 LLVMValueRef gather4_int_result_workaround = NULL;
1774
1775 if (ctx->screen->info.chip_class <= VI &&
1776 opcode == TGSI_OPCODE_TG4) {
1777 assert(inst->Texture.ReturnType != TGSI_RETURN_TYPE_UNKNOWN);
1778
1779 if (inst->Texture.ReturnType == TGSI_RETURN_TYPE_SINT ||
1780 inst->Texture.ReturnType == TGSI_RETURN_TYPE_UINT) {
1781 gather4_int_result_workaround =
1782 si_lower_gather4_integer(ctx, &args, target,
1783 inst->Texture.ReturnType);
1784 }
1785 }
1786
1787 args.attributes = AC_FUNC_ATTR_READNONE;
1788 LLVMValueRef result = ac_build_image_opcode(&ctx->ac, &args);
1789
1790 if (gather4_int_result_workaround) {
1791 result = si_fix_gather4_integer_result(ctx, result,
1792 inst->Texture.ReturnType,
1793 gather4_int_result_workaround);
1794 }
1795
1796 emit_data->output[emit_data->chan] = result;
1797 }
1798
1799 static void si_llvm_emit_txqs(
1800 const struct lp_build_tgsi_action *action,
1801 struct lp_build_tgsi_context *bld_base,
1802 struct lp_build_emit_data *emit_data)
1803 {
1804 struct si_shader_context *ctx = si_shader_context(bld_base);
1805 LLVMValueRef res, samples;
1806 LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL;
1807
1808 tex_fetch_ptrs(bld_base, emit_data, &res_ptr, &samp_ptr, &fmask_ptr);
1809
1810 /* Read the samples from the descriptor directly. */
1811 res = LLVMBuildBitCast(ctx->ac.builder, res_ptr, ctx->v8i32, "");
1812 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
1813 LLVMConstInt(ctx->i32, 3, 0), "");
1814 samples = LLVMBuildLShr(ctx->ac.builder, samples,
1815 LLVMConstInt(ctx->i32, 16, 0), "");
1816 samples = LLVMBuildAnd(ctx->ac.builder, samples,
1817 LLVMConstInt(ctx->i32, 0xf, 0), "");
1818 samples = LLVMBuildShl(ctx->ac.builder, ctx->i32_1,
1819 samples, "");
1820
1821 emit_data->output[emit_data->chan] = samples;
1822 }
1823
1824 static void si_llvm_emit_fbfetch(const struct lp_build_tgsi_action *action,
1825 struct lp_build_tgsi_context *bld_base,
1826 struct lp_build_emit_data *emit_data)
1827 {
1828 struct si_shader_context *ctx = si_shader_context(bld_base);
1829 struct ac_image_args args = {};
1830 LLVMValueRef ptr, image, fmask;
1831
1832 /* Ignore src0, because KHR_blend_func_extended disallows multiple render
1833 * targets.
1834 */
1835
1836 /* Load the image descriptor. */
1837 STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0 % 2 == 0);
1838 ptr = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
1839 ptr = LLVMBuildPointerCast(ctx->ac.builder, ptr,
1840 ac_array_in_const32_addr_space(ctx->v8i32), "");
1841 image = ac_build_load_to_sgpr(&ctx->ac, ptr,
1842 LLVMConstInt(ctx->i32, SI_PS_IMAGE_COLORBUF0 / 2, 0));
1843
1844 unsigned chan = 0;
1845
1846 args.coords[chan++] = si_unpack_param(ctx, SI_PARAM_POS_FIXED_PT, 0, 16);
1847
1848 if (!ctx->shader->key.mono.u.ps.fbfetch_is_1D)
1849 args.coords[chan++] = si_unpack_param(ctx, SI_PARAM_POS_FIXED_PT, 16, 16);
1850
1851 /* Get the current render target layer index. */
1852 if (ctx->shader->key.mono.u.ps.fbfetch_layered)
1853 args.coords[chan++] = si_unpack_param(ctx, SI_PARAM_ANCILLARY, 16, 11);
1854
1855 if (ctx->shader->key.mono.u.ps.fbfetch_msaa)
1856 args.coords[chan++] = si_get_sample_id(ctx);
1857
1858 if (ctx->shader->key.mono.u.ps.fbfetch_msaa) {
1859 fmask = ac_build_load_to_sgpr(&ctx->ac, ptr,
1860 LLVMConstInt(ctx->i32, SI_PS_IMAGE_COLORBUF0_FMASK / 2, 0));
1861
1862 ac_apply_fmask_to_sample(&ctx->ac, fmask, args.coords, false);
1863 }
1864
1865 args.opcode = ac_image_load;
1866 args.resource = image;
1867 args.dmask = 0xf;
1868 if (ctx->shader->key.mono.u.ps.fbfetch_msaa)
1869 args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ?
1870 ac_image_2darraymsaa : ac_image_2dmsaa;
1871 else if (ctx->shader->key.mono.u.ps.fbfetch_is_1D)
1872 args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ?
1873 ac_image_1darray : ac_image_1d;
1874 else
1875 args.dim = ctx->shader->key.mono.u.ps.fbfetch_layered ?
1876 ac_image_2darray : ac_image_2d;
1877
1878 emit_data->output[emit_data->chan] =
1879 ac_build_image_opcode(&ctx->ac, &args);
1880 }
1881
1882 static const struct lp_build_tgsi_action tex_action = {
1883 .fetch_args = tex_fetch_args,
1884 .emit = build_tex_intrinsic,
1885 };
1886
1887 /**
1888 * Setup actions for TGSI memory opcode, including texture opcodes.
1889 */
1890 void si_shader_context_init_mem(struct si_shader_context *ctx)
1891 {
1892 struct lp_build_tgsi_context *bld_base;
1893 struct lp_build_tgsi_action tmpl = {};
1894
1895 bld_base = &ctx->bld_base;
1896
1897 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
1898 bld_base->op_actions[TGSI_OPCODE_TEX_LZ] = tex_action;
1899 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
1900 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
1901 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
1902 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
1903 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
1904 bld_base->op_actions[TGSI_OPCODE_TXF_LZ] = tex_action;
1905 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
1906 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
1907 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
1908 bld_base->op_actions[TGSI_OPCODE_TXQ].fetch_args = txq_fetch_args;
1909 bld_base->op_actions[TGSI_OPCODE_TXQ].emit = txq_emit;
1910 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
1911 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
1912 bld_base->op_actions[TGSI_OPCODE_TXQS].emit = si_llvm_emit_txqs;
1913
1914 bld_base->op_actions[TGSI_OPCODE_FBFETCH].emit = si_llvm_emit_fbfetch;
1915
1916 bld_base->op_actions[TGSI_OPCODE_LOAD].fetch_args = load_fetch_args;
1917 bld_base->op_actions[TGSI_OPCODE_LOAD].emit = load_emit;
1918 bld_base->op_actions[TGSI_OPCODE_STORE].fetch_args = store_fetch_args;
1919 bld_base->op_actions[TGSI_OPCODE_STORE].emit = store_emit;
1920 bld_base->op_actions[TGSI_OPCODE_RESQ].fetch_args = resq_fetch_args;
1921 bld_base->op_actions[TGSI_OPCODE_RESQ].emit = resq_emit;
1922
1923 tmpl.fetch_args = atomic_fetch_args;
1924 tmpl.emit = atomic_emit;
1925 bld_base->op_actions[TGSI_OPCODE_ATOMUADD] = tmpl;
1926 bld_base->op_actions[TGSI_OPCODE_ATOMUADD].intr_name = "add";
1927 bld_base->op_actions[TGSI_OPCODE_ATOMXCHG] = tmpl;
1928 bld_base->op_actions[TGSI_OPCODE_ATOMXCHG].intr_name = "swap";
1929 bld_base->op_actions[TGSI_OPCODE_ATOMCAS] = tmpl;
1930 bld_base->op_actions[TGSI_OPCODE_ATOMCAS].intr_name = "cmpswap";
1931 bld_base->op_actions[TGSI_OPCODE_ATOMAND] = tmpl;
1932 bld_base->op_actions[TGSI_OPCODE_ATOMAND].intr_name = "and";
1933 bld_base->op_actions[TGSI_OPCODE_ATOMOR] = tmpl;
1934 bld_base->op_actions[TGSI_OPCODE_ATOMOR].intr_name = "or";
1935 bld_base->op_actions[TGSI_OPCODE_ATOMXOR] = tmpl;
1936 bld_base->op_actions[TGSI_OPCODE_ATOMXOR].intr_name = "xor";
1937 bld_base->op_actions[TGSI_OPCODE_ATOMUMIN] = tmpl;
1938 bld_base->op_actions[TGSI_OPCODE_ATOMUMIN].intr_name = "umin";
1939 bld_base->op_actions[TGSI_OPCODE_ATOMUMAX] = tmpl;
1940 bld_base->op_actions[TGSI_OPCODE_ATOMUMAX].intr_name = "umax";
1941 bld_base->op_actions[TGSI_OPCODE_ATOMIMIN] = tmpl;
1942 bld_base->op_actions[TGSI_OPCODE_ATOMIMIN].intr_name = "smin";
1943 bld_base->op_actions[TGSI_OPCODE_ATOMIMAX] = tmpl;
1944 bld_base->op_actions[TGSI_OPCODE_ATOMIMAX].intr_name = "smax";
1945 }