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