ttn: Add new allow_disk_cache parameter
[mesa.git] / src / gallium / drivers / radeonsi / si_compute_blit.c
1 /*
2 * Copyright 2018 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
26 #include "si_pipe.h"
27 #include "util/format/u_format.h"
28 #include "util/format_srgb.h"
29
30 /* Note: Compute shaders always use SI_COMPUTE_DST_CACHE_POLICY for dst
31 * and L2_STREAM for src.
32 */
33 static enum si_cache_policy get_cache_policy(struct si_context *sctx, enum si_coherency coher,
34 uint64_t size)
35 {
36 if ((sctx->chip_class >= GFX9 && (coher == SI_COHERENCY_CB_META ||
37 coher == SI_COHERENCY_DB_META ||
38 coher == SI_COHERENCY_CP)) ||
39 (sctx->chip_class >= GFX7 && coher == SI_COHERENCY_SHADER))
40 return size <= 256 * 1024 ? L2_LRU : L2_STREAM;
41
42 return L2_BYPASS;
43 }
44
45 unsigned si_get_flush_flags(struct si_context *sctx, enum si_coherency coher,
46 enum si_cache_policy cache_policy)
47 {
48 switch (coher) {
49 default:
50 case SI_COHERENCY_NONE:
51 case SI_COHERENCY_CP:
52 return 0;
53 case SI_COHERENCY_SHADER:
54 return SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE |
55 (cache_policy == L2_BYPASS ? SI_CONTEXT_INV_L2 : 0);
56 case SI_COHERENCY_CB_META:
57 return SI_CONTEXT_FLUSH_AND_INV_CB;
58 case SI_COHERENCY_DB_META:
59 return SI_CONTEXT_FLUSH_AND_INV_DB;
60 }
61 }
62
63 #define SI_CS_IMAGE_OP (1 << 0)
64 #define SI_CS_WAIT_FOR_IDLE (1 << 1)
65 #define SI_CS_RENDER_COND_ENABLE (1 << 2)
66
67 static void si_launch_grid_internal(struct si_context *sctx, struct pipe_grid_info *info,
68 void *restore_cs, unsigned flags)
69 {
70 /* Wait for previous shaders to finish. */
71 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH | SI_CONTEXT_PS_PARTIAL_FLUSH;
72 /* Invalidate L0-L1 caches. */
73 /* sL0 is never invalidated, because src resources don't use it. */
74 sctx->flags |= SI_CONTEXT_INV_VCACHE;
75
76 /* Set settings for driver-internal compute dispatches. */
77 sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
78 sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
79
80 if (!(flags & SI_CS_RENDER_COND_ENABLE))
81 sctx->render_cond_force_off = true;
82
83 /* Skip decompression to prevent infinite recursion. */
84 if (sctx->blitter)
85 sctx->blitter->running = true;
86
87 /* Dispatch compute. */
88 sctx->b.launch_grid(&sctx->b, info);
89
90 /* Restore default settings. */
91 sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
92 sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
93 sctx->render_cond_force_off = false;
94 if (sctx->blitter)
95 sctx->blitter->running = false;
96
97 /* Restore the original compute shader. */
98 sctx->b.bind_compute_state(&sctx->b, restore_cs);
99
100 if (flags & SI_CS_WAIT_FOR_IDLE) {
101 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
102
103 if (flags & SI_CS_IMAGE_OP) {
104 /* Make sure image stores are visible to CB, which doesn't use L2 on GFX6-8. */
105 sctx->flags |= sctx->chip_class <= GFX8 ? SI_CONTEXT_WB_L2 : 0;
106 /* Make sure image stores are visible to all CUs. */
107 sctx->flags |= SI_CONTEXT_INV_VCACHE;
108 } else {
109 /* Make sure buffer stores are visible to all CUs. */
110 sctx->flags |= SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE;
111 }
112 } else {
113 assert(!(flags & SI_CS_IMAGE_OP));
114 }
115 }
116
117 static void si_compute_clear_12bytes_buffer(struct si_context *sctx, struct pipe_resource *dst,
118 unsigned dst_offset, unsigned size,
119 const uint32_t *clear_value, enum si_coherency coher)
120 {
121 struct pipe_context *ctx = &sctx->b;
122
123 assert(dst_offset % 4 == 0);
124 assert(size % 4 == 0);
125 unsigned size_12 = DIV_ROUND_UP(size, 12);
126
127 unsigned data[4] = {0};
128 memcpy(data, clear_value, 12);
129
130 sctx->flags |= si_get_flush_flags(sctx, coher, SI_COMPUTE_DST_CACHE_POLICY);
131
132 struct pipe_shader_buffer saved_sb = {0};
133 si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, 1, &saved_sb);
134
135 unsigned saved_writable_mask = 0;
136 if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
137 (1u << si_get_shaderbuf_slot(0)))
138 saved_writable_mask = 1;
139
140 struct pipe_constant_buffer saved_cb = {};
141 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
142
143 void *saved_cs = sctx->cs_shader_state.program;
144
145 struct pipe_constant_buffer cb = {};
146 cb.buffer_size = sizeof(data);
147 cb.user_buffer = data;
148 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &cb);
149
150 struct pipe_shader_buffer sb = {0};
151 sb.buffer = dst;
152 sb.buffer_offset = dst_offset;
153 sb.buffer_size = size;
154
155 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, 1, &sb, 0x1);
156
157 struct pipe_grid_info info = {0};
158
159 if (!sctx->cs_clear_12bytes_buffer)
160 sctx->cs_clear_12bytes_buffer = si_clear_12bytes_buffer_shader(ctx);
161 ctx->bind_compute_state(ctx, sctx->cs_clear_12bytes_buffer);
162 info.block[0] = 64;
163 info.last_block[0] = size_12 % 64;
164 info.block[1] = 1;
165 info.block[2] = 1;
166 info.grid[0] = DIV_ROUND_UP(size_12, 64);
167 info.grid[1] = 1;
168 info.grid[2] = 1;
169
170 si_launch_grid_internal(sctx, &info, saved_cs, SI_CS_WAIT_FOR_IDLE);
171
172 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, 1, &saved_sb, saved_writable_mask);
173 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
174
175 pipe_resource_reference(&saved_sb.buffer, NULL);
176 pipe_resource_reference(&saved_cb.buffer, NULL);
177 }
178
179 static void si_compute_do_clear_or_copy(struct si_context *sctx, struct pipe_resource *dst,
180 unsigned dst_offset, struct pipe_resource *src,
181 unsigned src_offset, unsigned size,
182 const uint32_t *clear_value, unsigned clear_value_size,
183 enum si_coherency coher)
184 {
185 struct pipe_context *ctx = &sctx->b;
186
187 assert(src_offset % 4 == 0);
188 assert(dst_offset % 4 == 0);
189 assert(size % 4 == 0);
190
191 assert(dst->target != PIPE_BUFFER || dst_offset + size <= dst->width0);
192 assert(!src || src_offset + size <= src->width0);
193
194 sctx->flags |= si_get_flush_flags(sctx, coher, SI_COMPUTE_DST_CACHE_POLICY);
195
196 /* Save states. */
197 void *saved_cs = sctx->cs_shader_state.program;
198 struct pipe_shader_buffer saved_sb[2] = {};
199 si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, src ? 2 : 1, saved_sb);
200
201 unsigned saved_writable_mask = 0;
202 for (unsigned i = 0; i < (src ? 2 : 1); i++) {
203 if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
204 (1u << si_get_shaderbuf_slot(i)))
205 saved_writable_mask |= 1 << i;
206 }
207
208 /* The memory accesses are coalesced, meaning that the 1st instruction writes
209 * the 1st contiguous block of data for the whole wave, the 2nd instruction
210 * writes the 2nd contiguous block of data, etc.
211 */
212 unsigned dwords_per_thread =
213 src ? SI_COMPUTE_COPY_DW_PER_THREAD : SI_COMPUTE_CLEAR_DW_PER_THREAD;
214 unsigned instructions_per_thread = MAX2(1, dwords_per_thread / 4);
215 unsigned dwords_per_instruction = dwords_per_thread / instructions_per_thread;
216 unsigned wave_size = sctx->screen->compute_wave_size;
217 unsigned dwords_per_wave = dwords_per_thread * wave_size;
218
219 unsigned num_dwords = size / 4;
220 unsigned num_instructions = DIV_ROUND_UP(num_dwords, dwords_per_instruction);
221
222 struct pipe_grid_info info = {};
223 info.block[0] = MIN2(wave_size, num_instructions);
224 info.block[1] = 1;
225 info.block[2] = 1;
226 info.grid[0] = DIV_ROUND_UP(num_dwords, dwords_per_wave);
227 info.grid[1] = 1;
228 info.grid[2] = 1;
229
230 struct pipe_shader_buffer sb[2] = {};
231 sb[0].buffer = dst;
232 sb[0].buffer_offset = dst_offset;
233 sb[0].buffer_size = size;
234
235 bool shader_dst_stream_policy = SI_COMPUTE_DST_CACHE_POLICY != L2_LRU;
236
237 if (src) {
238 sb[1].buffer = src;
239 sb[1].buffer_offset = src_offset;
240 sb[1].buffer_size = size;
241
242 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, 2, sb, 0x1);
243
244 if (!sctx->cs_copy_buffer) {
245 sctx->cs_copy_buffer = si_create_dma_compute_shader(
246 &sctx->b, SI_COMPUTE_COPY_DW_PER_THREAD, shader_dst_stream_policy, true);
247 }
248 ctx->bind_compute_state(ctx, sctx->cs_copy_buffer);
249 } else {
250 assert(clear_value_size >= 4 && clear_value_size <= 16 &&
251 util_is_power_of_two_or_zero(clear_value_size));
252
253 for (unsigned i = 0; i < 4; i++)
254 sctx->cs_user_data[i] = clear_value[i % (clear_value_size / 4)];
255
256 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, 1, sb, 0x1);
257
258 if (!sctx->cs_clear_buffer) {
259 sctx->cs_clear_buffer = si_create_dma_compute_shader(
260 &sctx->b, SI_COMPUTE_CLEAR_DW_PER_THREAD, shader_dst_stream_policy, false);
261 }
262 ctx->bind_compute_state(ctx, sctx->cs_clear_buffer);
263 }
264
265 si_launch_grid_internal(sctx, &info, saved_cs, SI_CS_WAIT_FOR_IDLE);
266
267 enum si_cache_policy cache_policy = get_cache_policy(sctx, coher, size);
268 sctx->flags |= cache_policy == L2_BYPASS ? SI_CONTEXT_WB_L2 : 0;
269
270 if (cache_policy != L2_BYPASS)
271 si_resource(dst)->TC_L2_dirty = true;
272
273 /* Restore states. */
274 ctx->set_shader_buffers(ctx, PIPE_SHADER_COMPUTE, 0, src ? 2 : 1, saved_sb, saved_writable_mask);
275 for (int i = 0; i < 2; i++)
276 pipe_resource_reference(&saved_sb[i].buffer, NULL);
277 }
278
279 void si_clear_buffer(struct si_context *sctx, struct pipe_resource *dst, uint64_t offset,
280 uint64_t size, uint32_t *clear_value, uint32_t clear_value_size,
281 enum si_coherency coher, bool force_cpdma)
282 {
283 if (!size)
284 return;
285
286 ASSERTED unsigned clear_alignment = MIN2(clear_value_size, 4);
287
288 assert(clear_value_size != 3 && clear_value_size != 6); /* 12 is allowed. */
289 assert(offset % clear_alignment == 0);
290 assert(size % clear_alignment == 0);
291 assert(size < (UINT_MAX & ~0xf)); /* TODO: test 64-bit sizes in all codepaths */
292
293 /* Reduce a large clear value size if possible. */
294 if (clear_value_size > 4) {
295 bool clear_dword_duplicated = true;
296
297 /* See if we can lower large fills to dword fills. */
298 for (unsigned i = 1; i < clear_value_size / 4; i++) {
299 if (clear_value[0] != clear_value[i]) {
300 clear_dword_duplicated = false;
301 break;
302 }
303 }
304 if (clear_dword_duplicated)
305 clear_value_size = 4;
306 }
307
308 /* Expand a small clear value size. */
309 uint32_t tmp_clear_value;
310 if (clear_value_size <= 2) {
311 if (clear_value_size == 1) {
312 tmp_clear_value = *(uint8_t *)clear_value;
313 tmp_clear_value |=
314 (tmp_clear_value << 8) | (tmp_clear_value << 16) | (tmp_clear_value << 24);
315 } else {
316 tmp_clear_value = *(uint16_t *)clear_value;
317 tmp_clear_value |= tmp_clear_value << 16;
318 }
319 clear_value = &tmp_clear_value;
320 clear_value_size = 4;
321 }
322
323 if (clear_value_size == 12) {
324 si_compute_clear_12bytes_buffer(sctx, dst, offset, size, clear_value, coher);
325 return;
326 }
327
328 uint64_t aligned_size = size & ~3ull;
329 if (aligned_size >= 4) {
330 /* Before GFX9, CP DMA was very slow when clearing GTT, so never
331 * use CP DMA clears on those chips, because we can't be certain
332 * about buffer placements.
333 */
334 if (clear_value_size > 4 || (!force_cpdma && clear_value_size == 4 && offset % 4 == 0 &&
335 (size > 32 * 1024 || sctx->chip_class <= GFX8))) {
336 si_compute_do_clear_or_copy(sctx, dst, offset, NULL, 0, aligned_size, clear_value,
337 clear_value_size, coher);
338 } else {
339 assert(clear_value_size == 4);
340 si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, dst, offset, aligned_size, *clear_value, 0,
341 coher, get_cache_policy(sctx, coher, size));
342 }
343
344 offset += aligned_size;
345 size -= aligned_size;
346 }
347
348 /* Handle non-dword alignment. */
349 if (size) {
350 assert(dst);
351 assert(dst->target == PIPE_BUFFER);
352 assert(size < 4);
353
354 pipe_buffer_write(&sctx->b, dst, offset, size, clear_value);
355 }
356 }
357
358 static void si_pipe_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
359 unsigned offset, unsigned size, const void *clear_value,
360 int clear_value_size)
361 {
362 si_clear_buffer((struct si_context *)ctx, dst, offset, size, (uint32_t *)clear_value,
363 clear_value_size, SI_COHERENCY_SHADER, false);
364 }
365
366 void si_copy_buffer(struct si_context *sctx, struct pipe_resource *dst, struct pipe_resource *src,
367 uint64_t dst_offset, uint64_t src_offset, unsigned size)
368 {
369 if (!size)
370 return;
371
372 enum si_coherency coher = SI_COHERENCY_SHADER;
373 enum si_cache_policy cache_policy = get_cache_policy(sctx, coher, size);
374
375 /* Only use compute for VRAM copies on dGPUs. */
376 if (sctx->screen->info.has_dedicated_vram && si_resource(dst)->domains & RADEON_DOMAIN_VRAM &&
377 si_resource(src)->domains & RADEON_DOMAIN_VRAM && size > 32 * 1024 && dst_offset % 4 == 0 &&
378 src_offset % 4 == 0 && size % 4 == 0) {
379 si_compute_do_clear_or_copy(sctx, dst, dst_offset, src, src_offset, size, NULL, 0, coher);
380 } else {
381 si_cp_dma_copy_buffer(sctx, dst, src, dst_offset, src_offset, size, 0, coher, cache_policy);
382 }
383 }
384
385 void si_compute_copy_image(struct si_context *sctx, struct pipe_resource *dst, unsigned dst_level,
386 struct pipe_resource *src, unsigned src_level, unsigned dstx,
387 unsigned dsty, unsigned dstz, const struct pipe_box *src_box,
388 bool is_dcc_decompress)
389 {
390 struct pipe_context *ctx = &sctx->b;
391 unsigned width = src_box->width;
392 unsigned height = src_box->height;
393 unsigned depth = src_box->depth;
394 enum pipe_format src_format = util_format_linear(src->format);
395 enum pipe_format dst_format = util_format_linear(dst->format);
396
397 assert(util_format_is_subsampled_422(src_format) == util_format_is_subsampled_422(dst_format));
398
399 if (util_format_is_subsampled_422(src_format)) {
400 src_format = dst_format = PIPE_FORMAT_R32_UINT;
401 /* Interpreting 422 subsampled format (16 bpp) as 32 bpp
402 * should force us to divide src_box->x, dstx and width by 2.
403 * But given that ac_surface allocates this format as 32 bpp
404 * and that surf_size is then modified to pack the values
405 * we must keep the original values to get the correct results.
406 */
407 }
408
409 if (width == 0 || height == 0)
410 return;
411
412 /* The driver doesn't decompress resources automatically here. */
413 si_decompress_subresource(ctx, dst, PIPE_MASK_RGBAZS, dst_level, dstz,
414 dstz + src_box->depth - 1);
415 si_decompress_subresource(ctx, src, PIPE_MASK_RGBAZS, src_level, src_box->z,
416 src_box->z + src_box->depth - 1);
417
418 /* src and dst have the same number of samples. */
419 si_make_CB_shader_coherent(sctx, src->nr_samples, true,
420 /* Only src can have DCC.*/
421 ((struct si_texture *)src)->surface.u.gfx9.dcc.pipe_aligned);
422
423 struct pipe_constant_buffer saved_cb = {};
424
425 struct si_images *images = &sctx->images[PIPE_SHADER_COMPUTE];
426 struct pipe_image_view saved_image[2] = {0};
427 util_copy_image_view(&saved_image[0], &images->views[0]);
428 util_copy_image_view(&saved_image[1], &images->views[1]);
429
430 void *saved_cs = sctx->cs_shader_state.program;
431
432 if (!is_dcc_decompress) {
433 unsigned data[] = {src_box->x, src_box->y, src_box->z, 0, dstx, dsty, dstz, 0};
434
435 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
436
437 struct pipe_constant_buffer cb = {};
438 cb.buffer_size = sizeof(data);
439 cb.user_buffer = data;
440 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &cb);
441 }
442
443 struct pipe_image_view image[2] = {0};
444 image[0].resource = src;
445 image[0].shader_access = image[0].access = PIPE_IMAGE_ACCESS_READ;
446 image[0].format = src_format;
447 image[0].u.tex.level = src_level;
448 image[0].u.tex.first_layer = 0;
449 image[0].u.tex.last_layer = src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, src_level) - 1
450 : (unsigned)(src->array_size - 1);
451 image[1].resource = dst;
452 image[1].shader_access = image[1].access = PIPE_IMAGE_ACCESS_WRITE;
453 image[1].format = dst_format;
454 image[1].u.tex.level = dst_level;
455 image[1].u.tex.first_layer = 0;
456 image[1].u.tex.last_layer = dst->target == PIPE_TEXTURE_3D ? u_minify(dst->depth0, dst_level) - 1
457 : (unsigned)(dst->array_size - 1);
458
459 if (src->format == PIPE_FORMAT_R9G9B9E5_FLOAT)
460 image[0].format = image[1].format = PIPE_FORMAT_R32_UINT;
461
462 /* SNORM8 blitting has precision issues on some chips. Use the SINT
463 * equivalent instead, which doesn't force DCC decompression.
464 * Note that some chips avoid this issue by using SDMA.
465 */
466 if (util_format_is_snorm8(dst->format)) {
467 image[0].format = image[1].format = util_format_snorm8_to_sint8(dst->format);
468 }
469
470 if (is_dcc_decompress)
471 image[1].access |= SI_IMAGE_ACCESS_DCC_OFF;
472
473 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 2, image);
474
475 struct pipe_grid_info info = {0};
476
477 if (is_dcc_decompress) {
478 /* The DCC decompression is a normal blit where the load is compressed
479 * and the store is uncompressed. The workgroup size is either equal to
480 * the DCC block size or a multiple thereof. The shader uses a barrier
481 * between loads and stores to safely overwrite each DCC block of pixels.
482 */
483 struct si_texture *tex = (struct si_texture*)src;
484 unsigned dim[3] = {src_box->width, src_box->height, src_box->depth};
485
486 assert(src == dst);
487 assert(dst->target != PIPE_TEXTURE_1D && dst->target != PIPE_TEXTURE_1D_ARRAY);
488
489 if (!sctx->cs_dcc_decompress)
490 sctx->cs_dcc_decompress = si_create_dcc_decompress_cs(ctx);
491 ctx->bind_compute_state(ctx, sctx->cs_dcc_decompress);
492
493 info.block[0] = tex->surface.u.gfx9.dcc_block_width;
494 info.block[1] = tex->surface.u.gfx9.dcc_block_height;
495 info.block[2] = tex->surface.u.gfx9.dcc_block_depth;
496
497 /* Make sure the block size is at least the same as wave size. */
498 while (info.block[0] * info.block[1] * info.block[2] <
499 sctx->screen->compute_wave_size) {
500 info.block[0] *= 2;
501 }
502
503 for (unsigned i = 0; i < 3; i++) {
504 info.last_block[i] = dim[i] % info.block[i];
505 info.grid[i] = DIV_ROUND_UP(dim[i], info.block[i]);
506 }
507 } else if (dst->target == PIPE_TEXTURE_1D_ARRAY && src->target == PIPE_TEXTURE_1D_ARRAY) {
508 if (!sctx->cs_copy_image_1d_array)
509 sctx->cs_copy_image_1d_array = si_create_copy_image_compute_shader_1d_array(ctx);
510 ctx->bind_compute_state(ctx, sctx->cs_copy_image_1d_array);
511 info.block[0] = 64;
512 info.last_block[0] = width % 64;
513 info.block[1] = 1;
514 info.block[2] = 1;
515 info.grid[0] = DIV_ROUND_UP(width, 64);
516 info.grid[1] = depth;
517 info.grid[2] = 1;
518 } else {
519 if (!sctx->cs_copy_image)
520 sctx->cs_copy_image = si_create_copy_image_compute_shader(ctx);
521 ctx->bind_compute_state(ctx, sctx->cs_copy_image);
522 info.block[0] = 8;
523 info.last_block[0] = width % 8;
524 info.block[1] = 8;
525 info.last_block[1] = height % 8;
526 info.block[2] = 1;
527 info.grid[0] = DIV_ROUND_UP(width, 8);
528 info.grid[1] = DIV_ROUND_UP(height, 8);
529 info.grid[2] = depth;
530 }
531
532 si_launch_grid_internal(sctx, &info, saved_cs,
533 SI_CS_WAIT_FOR_IDLE | SI_CS_IMAGE_OP);
534
535 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 2, saved_image);
536 for (int i = 0; i < 2; i++)
537 pipe_resource_reference(&saved_image[i].resource, NULL);
538 if (!is_dcc_decompress) {
539 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
540 pipe_resource_reference(&saved_cb.buffer, NULL);
541 }
542 }
543
544 void si_retile_dcc(struct si_context *sctx, struct si_texture *tex)
545 {
546 struct pipe_context *ctx = &sctx->b;
547
548 sctx->flags |= si_get_flush_flags(sctx, SI_COHERENCY_CB_META, L2_LRU);
549
550 /* Save states. */
551 void *saved_cs = sctx->cs_shader_state.program;
552 struct pipe_image_view saved_img[3] = {};
553
554 for (unsigned i = 0; i < 3; i++) {
555 util_copy_image_view(&saved_img[i], &sctx->images[PIPE_SHADER_COMPUTE].views[i]);
556 }
557
558 /* Set images. */
559 bool use_uint16 = tex->surface.u.gfx9.dcc_retile_use_uint16;
560 unsigned num_elements = tex->surface.u.gfx9.dcc_retile_num_elements;
561 struct pipe_image_view img[3];
562
563 assert(tex->surface.dcc_retile_map_offset && tex->surface.dcc_retile_map_offset <= UINT_MAX);
564 assert(tex->surface.dcc_offset && tex->surface.dcc_offset <= UINT_MAX);
565 assert(tex->surface.display_dcc_offset && tex->surface.display_dcc_offset <= UINT_MAX);
566
567 for (unsigned i = 0; i < 3; i++) {
568 img[i].resource = &tex->buffer.b.b;
569 img[i].access = i == 2 ? PIPE_IMAGE_ACCESS_WRITE : PIPE_IMAGE_ACCESS_READ;
570 img[i].shader_access = SI_IMAGE_ACCESS_AS_BUFFER;
571 }
572
573 img[0].format = use_uint16 ? PIPE_FORMAT_R16G16B16A16_UINT : PIPE_FORMAT_R32G32B32A32_UINT;
574 img[0].u.buf.offset = tex->surface.dcc_retile_map_offset;
575 img[0].u.buf.size = num_elements * (use_uint16 ? 2 : 4);
576
577 img[1].format = PIPE_FORMAT_R8_UINT;
578 img[1].u.buf.offset = tex->surface.dcc_offset;
579 img[1].u.buf.size = tex->surface.dcc_size;
580
581 img[2].format = PIPE_FORMAT_R8_UINT;
582 img[2].u.buf.offset = tex->surface.display_dcc_offset;
583 img[2].u.buf.size = tex->surface.u.gfx9.display_dcc_size;
584
585 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 3, img);
586
587 /* Bind the compute shader. */
588 if (!sctx->cs_dcc_retile)
589 sctx->cs_dcc_retile = si_create_dcc_retile_cs(ctx);
590 ctx->bind_compute_state(ctx, sctx->cs_dcc_retile);
591
592 /* Dispatch compute. */
593 /* img[0] has 4 channels per element containing 2 pairs of DCC offsets. */
594 unsigned num_threads = num_elements / 4;
595
596 struct pipe_grid_info info = {};
597 info.block[0] = 64;
598 info.block[1] = 1;
599 info.block[2] = 1;
600 info.grid[0] = DIV_ROUND_UP(num_threads, 64); /* includes the partial block */
601 info.grid[1] = 1;
602 info.grid[2] = 1;
603 info.last_block[0] = num_threads % 64;
604
605 si_launch_grid_internal(sctx, &info, saved_cs, 0);
606
607 /* Don't flush caches or wait. The driver will wait at the end of this IB,
608 * and L2 will be flushed by the kernel fence.
609 */
610
611 /* Restore states. */
612 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 3, saved_img);
613
614 for (unsigned i = 0; i < 3; i++) {
615 pipe_resource_reference(&saved_img[i].resource, NULL);
616 }
617 }
618
619 /* Expand FMASK to make it identity, so that image stores can ignore it. */
620 void si_compute_expand_fmask(struct pipe_context *ctx, struct pipe_resource *tex)
621 {
622 struct si_context *sctx = (struct si_context *)ctx;
623 bool is_array = tex->target == PIPE_TEXTURE_2D_ARRAY;
624 unsigned log_fragments = util_logbase2(tex->nr_storage_samples);
625 unsigned log_samples = util_logbase2(tex->nr_samples);
626 assert(tex->nr_samples >= 2);
627
628 /* EQAA FMASK expansion is unimplemented. */
629 if (tex->nr_samples != tex->nr_storage_samples)
630 return;
631
632 si_make_CB_shader_coherent(sctx, tex->nr_samples, true,
633 true /* DCC is not possible with image stores */);
634
635 /* Save states. */
636 void *saved_cs = sctx->cs_shader_state.program;
637 struct pipe_image_view saved_image = {0};
638 util_copy_image_view(&saved_image, &sctx->images[PIPE_SHADER_COMPUTE].views[0]);
639
640 /* Bind the image. */
641 struct pipe_image_view image = {0};
642 image.resource = tex;
643 /* Don't set WRITE so as not to trigger FMASK expansion, causing
644 * an infinite loop. */
645 image.shader_access = image.access = PIPE_IMAGE_ACCESS_READ;
646 image.format = util_format_linear(tex->format);
647 if (is_array)
648 image.u.tex.last_layer = tex->array_size - 1;
649
650 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, &image);
651
652 /* Bind the shader. */
653 void **shader = &sctx->cs_fmask_expand[log_samples - 1][is_array];
654 if (!*shader)
655 *shader = si_create_fmask_expand_cs(ctx, tex->nr_samples, is_array);
656 ctx->bind_compute_state(ctx, *shader);
657
658 /* Dispatch compute. */
659 struct pipe_grid_info info = {0};
660 info.block[0] = 8;
661 info.last_block[0] = tex->width0 % 8;
662 info.block[1] = 8;
663 info.last_block[1] = tex->height0 % 8;
664 info.block[2] = 1;
665 info.grid[0] = DIV_ROUND_UP(tex->width0, 8);
666 info.grid[1] = DIV_ROUND_UP(tex->height0, 8);
667 info.grid[2] = is_array ? tex->array_size : 1;
668
669 si_launch_grid_internal(sctx, &info, saved_cs,
670 SI_CS_WAIT_FOR_IDLE | SI_CS_IMAGE_OP);
671
672 /* Restore previous states. */
673 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, &saved_image);
674 pipe_resource_reference(&saved_image.resource, NULL);
675
676 /* Array of fully expanded FMASK values, arranged by [log2(fragments)][log2(samples)-1]. */
677 #define INVALID 0 /* never used */
678 static const uint64_t fmask_expand_values[][4] = {
679 /* samples */
680 /* 2 (8 bpp) 4 (8 bpp) 8 (8-32bpp) 16 (16-64bpp) fragments */
681 {0x02020202, 0x0E0E0E0E, 0xFEFEFEFE, 0xFFFEFFFE}, /* 1 */
682 {0x02020202, 0xA4A4A4A4, 0xAAA4AAA4, 0xAAAAAAA4}, /* 2 */
683 {INVALID, 0xE4E4E4E4, 0x44443210, 0x4444444444443210}, /* 4 */
684 {INVALID, INVALID, 0x76543210, 0x8888888876543210}, /* 8 */
685 };
686
687 /* Clear FMASK to identity. */
688 struct si_texture *stex = (struct si_texture *)tex;
689 si_clear_buffer(sctx, tex, stex->surface.fmask_offset, stex->surface.fmask_size,
690 (uint32_t *)&fmask_expand_values[log_fragments][log_samples - 1], 4,
691 SI_COHERENCY_SHADER, false);
692 }
693
694 void si_init_compute_blit_functions(struct si_context *sctx)
695 {
696 sctx->b.clear_buffer = si_pipe_clear_buffer;
697 }
698
699 /* Clear a region of a color surface to a constant value. */
700 void si_compute_clear_render_target(struct pipe_context *ctx, struct pipe_surface *dstsurf,
701 const union pipe_color_union *color, unsigned dstx,
702 unsigned dsty, unsigned width, unsigned height,
703 bool render_condition_enabled)
704 {
705 struct si_context *sctx = (struct si_context *)ctx;
706 unsigned num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
707 unsigned data[4 + sizeof(color->ui)] = {dstx, dsty, dstsurf->u.tex.first_layer, 0};
708
709 if (width == 0 || height == 0)
710 return;
711
712 /* The driver doesn't decompress resources automatically here. */
713 si_decompress_subresource(ctx, dstsurf->texture, PIPE_MASK_RGBA, dstsurf->u.tex.level,
714 dstsurf->u.tex.first_layer, dstsurf->u.tex.last_layer);
715
716 if (util_format_is_srgb(dstsurf->format)) {
717 union pipe_color_union color_srgb;
718 for (int i = 0; i < 3; i++)
719 color_srgb.f[i] = util_format_linear_to_srgb_float(color->f[i]);
720 color_srgb.f[3] = color->f[3];
721 memcpy(data + 4, color_srgb.ui, sizeof(color->ui));
722 } else {
723 memcpy(data + 4, color->ui, sizeof(color->ui));
724 }
725
726 si_make_CB_shader_coherent(sctx, dstsurf->texture->nr_samples, true,
727 true /* DCC is not possible with image stores */);
728
729 struct pipe_constant_buffer saved_cb = {};
730 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
731
732 struct si_images *images = &sctx->images[PIPE_SHADER_COMPUTE];
733 struct pipe_image_view saved_image = {0};
734 util_copy_image_view(&saved_image, &images->views[0]);
735
736 void *saved_cs = sctx->cs_shader_state.program;
737
738 struct pipe_constant_buffer cb = {};
739 cb.buffer_size = sizeof(data);
740 cb.user_buffer = data;
741 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &cb);
742
743 struct pipe_image_view image = {0};
744 image.resource = dstsurf->texture;
745 image.shader_access = image.access = PIPE_IMAGE_ACCESS_WRITE;
746 image.format = util_format_linear(dstsurf->format);
747 image.u.tex.level = dstsurf->u.tex.level;
748 image.u.tex.first_layer = 0; /* 3D images ignore first_layer (BASE_ARRAY) */
749 image.u.tex.last_layer = dstsurf->u.tex.last_layer;
750
751 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, &image);
752
753 struct pipe_grid_info info = {0};
754
755 if (dstsurf->texture->target != PIPE_TEXTURE_1D_ARRAY) {
756 if (!sctx->cs_clear_render_target)
757 sctx->cs_clear_render_target = si_clear_render_target_shader(ctx);
758 ctx->bind_compute_state(ctx, sctx->cs_clear_render_target);
759 info.block[0] = 8;
760 info.last_block[0] = width % 8;
761 info.block[1] = 8;
762 info.last_block[1] = height % 8;
763 info.block[2] = 1;
764 info.grid[0] = DIV_ROUND_UP(width, 8);
765 info.grid[1] = DIV_ROUND_UP(height, 8);
766 info.grid[2] = num_layers;
767 } else {
768 if (!sctx->cs_clear_render_target_1d_array)
769 sctx->cs_clear_render_target_1d_array = si_clear_render_target_shader_1d_array(ctx);
770 ctx->bind_compute_state(ctx, sctx->cs_clear_render_target_1d_array);
771 info.block[0] = 64;
772 info.last_block[0] = width % 64;
773 info.block[1] = 1;
774 info.block[2] = 1;
775 info.grid[0] = DIV_ROUND_UP(width, 64);
776 info.grid[1] = num_layers;
777 info.grid[2] = 1;
778 }
779
780 si_launch_grid_internal(sctx, &info, saved_cs,
781 SI_CS_WAIT_FOR_IDLE | SI_CS_IMAGE_OP |
782 (render_condition_enabled ? SI_CS_RENDER_COND_ENABLE : 0));
783
784 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, &saved_image);
785 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, &saved_cb);
786 pipe_resource_reference(&saved_image.resource, NULL);
787 pipe_resource_reference(&saved_cb.buffer, NULL);
788 }