581f90a7b2f3b5fcfe322f2b42d365d970af451a
[mesa.git] / src / gallium / drivers / radeonsi / si_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "radeonsi/si_pipe.h"
27 #include "radeonsi/si_query.h"
28 #include "util/u_format.h"
29 #include "util/u_log.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include "util/u_resource.h"
33 #include "util/u_surface.h"
34 #include "util/u_transfer.h"
35 #include "util/os_time.h"
36 #include <errno.h>
37 #include <inttypes.h>
38 #include "state_tracker/drm_driver.h"
39 #include "amd/common/sid.h"
40
41 static enum radeon_surf_mode
42 si_choose_tiling(struct si_screen *sscreen,
43 const struct pipe_resource *templ, bool tc_compatible_htile);
44
45
46 bool si_prepare_for_dma_blit(struct si_context *sctx,
47 struct si_texture *dst,
48 unsigned dst_level, unsigned dstx,
49 unsigned dsty, unsigned dstz,
50 struct si_texture *src,
51 unsigned src_level,
52 const struct pipe_box *src_box)
53 {
54 if (!sctx->dma_cs)
55 return false;
56
57 if (dst->surface.bpe != src->surface.bpe)
58 return false;
59
60 /* MSAA: Blits don't exist in the real world. */
61 if (src->buffer.b.b.nr_samples > 1 ||
62 dst->buffer.b.b.nr_samples > 1)
63 return false;
64
65 /* Depth-stencil surfaces:
66 * When dst is linear, the DB->CB copy preserves HTILE.
67 * When dst is tiled, the 3D path must be used to update HTILE.
68 */
69 if (src->is_depth || dst->is_depth)
70 return false;
71
72 /* DCC as:
73 * src: Use the 3D path. DCC decompression is expensive.
74 * dst: Use the 3D path to compress the pixels with DCC.
75 */
76 if (vi_dcc_enabled(src, src_level) ||
77 vi_dcc_enabled(dst, dst_level))
78 return false;
79
80 /* CMASK as:
81 * src: Both texture and SDMA paths need decompression. Use SDMA.
82 * dst: If overwriting the whole texture, discard CMASK and use
83 * SDMA. Otherwise, use the 3D path.
84 */
85 if (dst->cmask_buffer && dst->dirty_level_mask & (1 << dst_level)) {
86 /* The CMASK clear is only enabled for the first level. */
87 assert(dst_level == 0);
88 if (!util_texrange_covers_whole_level(&dst->buffer.b.b, dst_level,
89 dstx, dsty, dstz, src_box->width,
90 src_box->height, src_box->depth))
91 return false;
92
93 si_texture_discard_cmask(sctx->screen, dst);
94 }
95
96 /* All requirements are met. Prepare textures for SDMA. */
97 if (src->cmask_buffer && src->dirty_level_mask & (1 << src_level))
98 sctx->b.flush_resource(&sctx->b, &src->buffer.b.b);
99
100 assert(!(src->dirty_level_mask & (1 << src_level)));
101 assert(!(dst->dirty_level_mask & (1 << dst_level)));
102
103 return true;
104 }
105
106 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
107 static void si_copy_region_with_blit(struct pipe_context *pipe,
108 struct pipe_resource *dst,
109 unsigned dst_level,
110 unsigned dstx, unsigned dsty, unsigned dstz,
111 struct pipe_resource *src,
112 unsigned src_level,
113 const struct pipe_box *src_box)
114 {
115 struct pipe_blit_info blit;
116
117 memset(&blit, 0, sizeof(blit));
118 blit.src.resource = src;
119 blit.src.format = src->format;
120 blit.src.level = src_level;
121 blit.src.box = *src_box;
122 blit.dst.resource = dst;
123 blit.dst.format = dst->format;
124 blit.dst.level = dst_level;
125 blit.dst.box.x = dstx;
126 blit.dst.box.y = dsty;
127 blit.dst.box.z = dstz;
128 blit.dst.box.width = src_box->width;
129 blit.dst.box.height = src_box->height;
130 blit.dst.box.depth = src_box->depth;
131 blit.mask = util_format_get_mask(src->format) &
132 util_format_get_mask(dst->format);
133 blit.filter = PIPE_TEX_FILTER_NEAREST;
134
135 if (blit.mask) {
136 pipe->blit(pipe, &blit);
137 }
138 }
139
140 /* Copy from a full GPU texture to a transfer's staging one. */
141 static void si_copy_to_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
142 {
143 struct si_context *sctx = (struct si_context*)ctx;
144 struct pipe_transfer *transfer = (struct pipe_transfer*)stransfer;
145 struct pipe_resource *dst = &stransfer->staging->b.b;
146 struct pipe_resource *src = transfer->resource;
147
148 if (src->nr_samples > 1) {
149 si_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
150 src, transfer->level, &transfer->box);
151 return;
152 }
153
154 sctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
155 &transfer->box);
156 }
157
158 /* Copy from a transfer's staging texture to a full GPU one. */
159 static void si_copy_from_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
160 {
161 struct si_context *sctx = (struct si_context*)ctx;
162 struct pipe_transfer *transfer = (struct pipe_transfer*)stransfer;
163 struct pipe_resource *dst = transfer->resource;
164 struct pipe_resource *src = &stransfer->staging->b.b;
165 struct pipe_box sbox;
166
167 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
168
169 if (dst->nr_samples > 1) {
170 si_copy_region_with_blit(ctx, dst, transfer->level,
171 transfer->box.x, transfer->box.y, transfer->box.z,
172 src, 0, &sbox);
173 return;
174 }
175
176 sctx->dma_copy(ctx, dst, transfer->level,
177 transfer->box.x, transfer->box.y, transfer->box.z,
178 src, 0, &sbox);
179 }
180
181 static unsigned si_texture_get_offset(struct si_screen *sscreen,
182 struct si_texture *tex, unsigned level,
183 const struct pipe_box *box,
184 unsigned *stride,
185 unsigned *layer_stride)
186 {
187 if (sscreen->info.chip_class >= GFX9) {
188 *stride = tex->surface.u.gfx9.surf_pitch * tex->surface.bpe;
189 *layer_stride = tex->surface.u.gfx9.surf_slice_size;
190
191 if (!box)
192 return 0;
193
194 /* Each texture is an array of slices. Each slice is an array
195 * of mipmap levels. */
196 return box->z * tex->surface.u.gfx9.surf_slice_size +
197 tex->surface.u.gfx9.offset[level] +
198 (box->y / tex->surface.blk_h *
199 tex->surface.u.gfx9.surf_pitch +
200 box->x / tex->surface.blk_w) * tex->surface.bpe;
201 } else {
202 *stride = tex->surface.u.legacy.level[level].nblk_x *
203 tex->surface.bpe;
204 assert((uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 <= UINT_MAX);
205 *layer_stride = (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4;
206
207 if (!box)
208 return tex->surface.u.legacy.level[level].offset;
209
210 /* Each texture is an array of mipmap levels. Each level is
211 * an array of slices. */
212 return tex->surface.u.legacy.level[level].offset +
213 box->z * (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 +
214 (box->y / tex->surface.blk_h *
215 tex->surface.u.legacy.level[level].nblk_x +
216 box->x / tex->surface.blk_w) * tex->surface.bpe;
217 }
218 }
219
220 static int si_init_surface(struct si_screen *sscreen,
221 struct radeon_surf *surface,
222 const struct pipe_resource *ptex,
223 enum radeon_surf_mode array_mode,
224 unsigned pitch_in_bytes_override,
225 unsigned offset,
226 bool is_imported,
227 bool is_scanout,
228 bool is_flushed_depth,
229 bool tc_compatible_htile)
230 {
231 const struct util_format_description *desc =
232 util_format_description(ptex->format);
233 bool is_depth, is_stencil;
234 int r;
235 unsigned i, bpe, flags = 0;
236
237 is_depth = util_format_has_depth(desc);
238 is_stencil = util_format_has_stencil(desc);
239
240 if (!is_flushed_depth &&
241 ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
242 bpe = 4; /* stencil is allocated separately */
243 } else {
244 bpe = util_format_get_blocksize(ptex->format);
245 assert(util_is_power_of_two_or_zero(bpe));
246 }
247
248 if (!is_flushed_depth && is_depth) {
249 flags |= RADEON_SURF_ZBUFFER;
250
251 if (tc_compatible_htile &&
252 (sscreen->info.chip_class >= GFX9 ||
253 array_mode == RADEON_SURF_MODE_2D)) {
254 /* TC-compatible HTILE only supports Z32_FLOAT.
255 * GFX9 also supports Z16_UNORM.
256 * On VI, promote Z16 to Z32. DB->CB copies will convert
257 * the format for transfers.
258 */
259 if (sscreen->info.chip_class == VI)
260 bpe = 4;
261
262 flags |= RADEON_SURF_TC_COMPATIBLE_HTILE;
263 }
264
265 if (is_stencil)
266 flags |= RADEON_SURF_SBUFFER;
267 }
268
269 if (sscreen->info.chip_class >= VI &&
270 (ptex->flags & SI_RESOURCE_FLAG_DISABLE_DCC ||
271 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT ||
272 (ptex->nr_samples >= 2 && !sscreen->dcc_msaa_allowed)))
273 flags |= RADEON_SURF_DISABLE_DCC;
274
275 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
276 if (sscreen->info.family == CHIP_STONEY &&
277 bpe == 16 && ptex->nr_samples >= 2)
278 flags |= RADEON_SURF_DISABLE_DCC;
279
280 /* VI: DCC clear for 4x and 8x MSAA array textures unimplemented. */
281 if (sscreen->info.chip_class == VI &&
282 ptex->nr_storage_samples >= 4 &&
283 ptex->array_size > 1)
284 flags |= RADEON_SURF_DISABLE_DCC;
285
286 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
287 if (sscreen->info.chip_class >= GFX9 &&
288 ptex->nr_storage_samples >= 4)
289 flags |= RADEON_SURF_DISABLE_DCC;
290
291 if (ptex->bind & PIPE_BIND_SCANOUT || is_scanout) {
292 /* This should catch bugs in gallium users setting incorrect flags. */
293 assert(ptex->nr_samples <= 1 &&
294 ptex->array_size == 1 &&
295 ptex->depth0 == 1 &&
296 ptex->last_level == 0 &&
297 !(flags & RADEON_SURF_Z_OR_SBUFFER));
298
299 flags |= RADEON_SURF_SCANOUT;
300 }
301
302 if (ptex->bind & PIPE_BIND_SHARED)
303 flags |= RADEON_SURF_SHAREABLE;
304 if (is_imported)
305 flags |= RADEON_SURF_IMPORTED | RADEON_SURF_SHAREABLE;
306 if (!(ptex->flags & SI_RESOURCE_FLAG_FORCE_MSAA_TILING))
307 flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
308
309 r = sscreen->ws->surface_init(sscreen->ws, ptex, flags, bpe,
310 array_mode, surface);
311 if (r) {
312 return r;
313 }
314
315 unsigned pitch = pitch_in_bytes_override / bpe;
316
317 if (sscreen->info.chip_class >= GFX9) {
318 if (pitch) {
319 surface->u.gfx9.surf_pitch = pitch;
320 surface->u.gfx9.surf_slice_size =
321 (uint64_t)pitch * surface->u.gfx9.surf_height * bpe;
322 }
323 surface->u.gfx9.surf_offset = offset;
324 } else {
325 if (pitch) {
326 surface->u.legacy.level[0].nblk_x = pitch;
327 surface->u.legacy.level[0].slice_size_dw =
328 ((uint64_t)pitch * surface->u.legacy.level[0].nblk_y * bpe) / 4;
329 }
330 if (offset) {
331 for (i = 0; i < ARRAY_SIZE(surface->u.legacy.level); ++i)
332 surface->u.legacy.level[i].offset += offset;
333 }
334 }
335 return 0;
336 }
337
338 static void si_get_display_metadata(struct si_screen *sscreen,
339 struct radeon_surf *surf,
340 struct radeon_bo_metadata *metadata,
341 enum radeon_surf_mode *array_mode,
342 bool *is_scanout)
343 {
344 if (sscreen->info.chip_class >= GFX9) {
345 if (metadata->u.gfx9.swizzle_mode > 0)
346 *array_mode = RADEON_SURF_MODE_2D;
347 else
348 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
349
350 *is_scanout = metadata->u.gfx9.swizzle_mode == 0 ||
351 metadata->u.gfx9.swizzle_mode % 4 == 2;
352
353 surf->u.gfx9.surf.swizzle_mode = metadata->u.gfx9.swizzle_mode;
354 } else {
355 surf->u.legacy.pipe_config = metadata->u.legacy.pipe_config;
356 surf->u.legacy.bankw = metadata->u.legacy.bankw;
357 surf->u.legacy.bankh = metadata->u.legacy.bankh;
358 surf->u.legacy.tile_split = metadata->u.legacy.tile_split;
359 surf->u.legacy.mtilea = metadata->u.legacy.mtilea;
360 surf->u.legacy.num_banks = metadata->u.legacy.num_banks;
361
362 if (metadata->u.legacy.macrotile == RADEON_LAYOUT_TILED)
363 *array_mode = RADEON_SURF_MODE_2D;
364 else if (metadata->u.legacy.microtile == RADEON_LAYOUT_TILED)
365 *array_mode = RADEON_SURF_MODE_1D;
366 else
367 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
368
369 *is_scanout = metadata->u.legacy.scanout;
370 }
371 }
372
373 void si_eliminate_fast_color_clear(struct si_context *sctx,
374 struct si_texture *tex)
375 {
376 struct si_screen *sscreen = sctx->screen;
377 struct pipe_context *ctx = &sctx->b;
378
379 if (ctx == sscreen->aux_context)
380 mtx_lock(&sscreen->aux_context_lock);
381
382 unsigned n = sctx->num_decompress_calls;
383 ctx->flush_resource(ctx, &tex->buffer.b.b);
384
385 /* Flush only if any fast clear elimination took place. */
386 if (n != sctx->num_decompress_calls)
387 ctx->flush(ctx, NULL, 0);
388
389 if (ctx == sscreen->aux_context)
390 mtx_unlock(&sscreen->aux_context_lock);
391 }
392
393 void si_texture_discard_cmask(struct si_screen *sscreen,
394 struct si_texture *tex)
395 {
396 if (!tex->cmask_buffer)
397 return;
398
399 assert(tex->buffer.b.b.nr_samples <= 1);
400
401 /* Disable CMASK. */
402 tex->cmask_base_address_reg = tex->buffer.gpu_address >> 8;
403 tex->dirty_level_mask = 0;
404
405 tex->cb_color_info &= ~S_028C70_FAST_CLEAR(1);
406
407 if (tex->cmask_buffer != &tex->buffer)
408 si_resource_reference(&tex->cmask_buffer, NULL);
409
410 tex->cmask_buffer = NULL;
411
412 /* Notify all contexts about the change. */
413 p_atomic_inc(&sscreen->dirty_tex_counter);
414 p_atomic_inc(&sscreen->compressed_colortex_counter);
415 }
416
417 static bool si_can_disable_dcc(struct si_texture *tex)
418 {
419 /* We can't disable DCC if it can be written by another process. */
420 return tex->dcc_offset &&
421 (!tex->buffer.b.is_shared ||
422 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
423 }
424
425 static bool si_texture_discard_dcc(struct si_screen *sscreen,
426 struct si_texture *tex)
427 {
428 if (!si_can_disable_dcc(tex))
429 return false;
430
431 assert(tex->dcc_separate_buffer == NULL);
432
433 /* Disable DCC. */
434 tex->dcc_offset = 0;
435
436 /* Notify all contexts about the change. */
437 p_atomic_inc(&sscreen->dirty_tex_counter);
438 return true;
439 }
440
441 /**
442 * Disable DCC for the texture. (first decompress, then discard metadata).
443 *
444 * There is unresolved multi-context synchronization issue between
445 * screen::aux_context and the current context. If applications do this with
446 * multiple contexts, it's already undefined behavior for them and we don't
447 * have to worry about that. The scenario is:
448 *
449 * If context 1 disables DCC and context 2 has queued commands that write
450 * to the texture via CB with DCC enabled, and the order of operations is
451 * as follows:
452 * context 2 queues draw calls rendering to the texture, but doesn't flush
453 * context 1 disables DCC and flushes
454 * context 1 & 2 reset descriptors and FB state
455 * context 2 flushes (new compressed tiles written by the draw calls)
456 * context 1 & 2 read garbage, because DCC is disabled, yet there are
457 * compressed tiled
458 *
459 * \param sctx the current context if you have one, or sscreen->aux_context
460 * if you don't.
461 */
462 bool si_texture_disable_dcc(struct si_context *sctx,
463 struct si_texture *tex)
464 {
465 struct si_screen *sscreen = sctx->screen;
466
467 if (!sctx->has_graphics)
468 return si_texture_discard_dcc(sscreen, tex);
469
470 if (!si_can_disable_dcc(tex))
471 return false;
472
473 if (&sctx->b == sscreen->aux_context)
474 mtx_lock(&sscreen->aux_context_lock);
475
476 /* Decompress DCC. */
477 si_decompress_dcc(sctx, tex);
478 sctx->b.flush(&sctx->b, NULL, 0);
479
480 if (&sctx->b == sscreen->aux_context)
481 mtx_unlock(&sscreen->aux_context_lock);
482
483 return si_texture_discard_dcc(sscreen, tex);
484 }
485
486 static void si_reallocate_texture_inplace(struct si_context *sctx,
487 struct si_texture *tex,
488 unsigned new_bind_flag,
489 bool invalidate_storage)
490 {
491 struct pipe_screen *screen = sctx->b.screen;
492 struct si_texture *new_tex;
493 struct pipe_resource templ = tex->buffer.b.b;
494 unsigned i;
495
496 templ.bind |= new_bind_flag;
497
498 if (tex->buffer.b.is_shared)
499 return;
500
501 if (new_bind_flag == PIPE_BIND_LINEAR) {
502 if (tex->surface.is_linear)
503 return;
504
505 /* This fails with MSAA, depth, and compressed textures. */
506 if (si_choose_tiling(sctx->screen, &templ, false) !=
507 RADEON_SURF_MODE_LINEAR_ALIGNED)
508 return;
509 }
510
511 new_tex = (struct si_texture*)screen->resource_create(screen, &templ);
512 if (!new_tex)
513 return;
514
515 /* Copy the pixels to the new texture. */
516 if (!invalidate_storage) {
517 for (i = 0; i <= templ.last_level; i++) {
518 struct pipe_box box;
519
520 u_box_3d(0, 0, 0,
521 u_minify(templ.width0, i), u_minify(templ.height0, i),
522 util_num_layers(&templ, i), &box);
523
524 sctx->dma_copy(&sctx->b, &new_tex->buffer.b.b, i, 0, 0, 0,
525 &tex->buffer.b.b, i, &box);
526 }
527 }
528
529 if (new_bind_flag == PIPE_BIND_LINEAR) {
530 si_texture_discard_cmask(sctx->screen, tex);
531 si_texture_discard_dcc(sctx->screen, tex);
532 }
533
534 /* Replace the structure fields of tex. */
535 tex->buffer.b.b.bind = templ.bind;
536 pb_reference(&tex->buffer.buf, new_tex->buffer.buf);
537 tex->buffer.gpu_address = new_tex->buffer.gpu_address;
538 tex->buffer.vram_usage = new_tex->buffer.vram_usage;
539 tex->buffer.gart_usage = new_tex->buffer.gart_usage;
540 tex->buffer.bo_size = new_tex->buffer.bo_size;
541 tex->buffer.bo_alignment = new_tex->buffer.bo_alignment;
542 tex->buffer.domains = new_tex->buffer.domains;
543 tex->buffer.flags = new_tex->buffer.flags;
544
545 tex->surface = new_tex->surface;
546 tex->size = new_tex->size;
547 si_texture_reference(&tex->flushed_depth_texture,
548 new_tex->flushed_depth_texture);
549
550 tex->fmask_offset = new_tex->fmask_offset;
551 tex->cmask_offset = new_tex->cmask_offset;
552 tex->cmask_base_address_reg = new_tex->cmask_base_address_reg;
553
554 if (tex->cmask_buffer == &tex->buffer)
555 tex->cmask_buffer = NULL;
556 else
557 si_resource_reference(&tex->cmask_buffer, NULL);
558
559 if (new_tex->cmask_buffer == &new_tex->buffer)
560 tex->cmask_buffer = &tex->buffer;
561 else
562 si_resource_reference(&tex->cmask_buffer, new_tex->cmask_buffer);
563
564 tex->dcc_offset = new_tex->dcc_offset;
565 tex->cb_color_info = new_tex->cb_color_info;
566 memcpy(tex->color_clear_value, new_tex->color_clear_value,
567 sizeof(tex->color_clear_value));
568 tex->last_msaa_resolve_target_micro_mode = new_tex->last_msaa_resolve_target_micro_mode;
569
570 tex->htile_offset = new_tex->htile_offset;
571 tex->depth_clear_value = new_tex->depth_clear_value;
572 tex->dirty_level_mask = new_tex->dirty_level_mask;
573 tex->stencil_dirty_level_mask = new_tex->stencil_dirty_level_mask;
574 tex->db_render_format = new_tex->db_render_format;
575 tex->stencil_clear_value = new_tex->stencil_clear_value;
576 tex->tc_compatible_htile = new_tex->tc_compatible_htile;
577 tex->depth_cleared = new_tex->depth_cleared;
578 tex->stencil_cleared = new_tex->stencil_cleared;
579 tex->upgraded_depth = new_tex->upgraded_depth;
580 tex->db_compatible = new_tex->db_compatible;
581 tex->can_sample_z = new_tex->can_sample_z;
582 tex->can_sample_s = new_tex->can_sample_s;
583
584 tex->separate_dcc_dirty = new_tex->separate_dcc_dirty;
585 tex->dcc_gather_statistics = new_tex->dcc_gather_statistics;
586 si_resource_reference(&tex->dcc_separate_buffer,
587 new_tex->dcc_separate_buffer);
588 si_resource_reference(&tex->last_dcc_separate_buffer,
589 new_tex->last_dcc_separate_buffer);
590
591 if (new_bind_flag == PIPE_BIND_LINEAR) {
592 assert(!tex->htile_offset);
593 assert(!tex->cmask_buffer);
594 assert(!tex->surface.fmask_size);
595 assert(!tex->dcc_offset);
596 assert(!tex->is_depth);
597 }
598
599 si_texture_reference(&new_tex, NULL);
600
601 p_atomic_inc(&sctx->screen->dirty_tex_counter);
602 }
603
604 static uint32_t si_get_bo_metadata_word1(struct si_screen *sscreen)
605 {
606 return (ATI_VENDOR_ID << 16) | sscreen->info.pci_id;
607 }
608
609 static void si_set_tex_bo_metadata(struct si_screen *sscreen,
610 struct si_texture *tex)
611 {
612 struct radeon_surf *surface = &tex->surface;
613 struct pipe_resource *res = &tex->buffer.b.b;
614 struct radeon_bo_metadata md;
615
616 memset(&md, 0, sizeof(md));
617
618 if (sscreen->info.chip_class >= GFX9) {
619 md.u.gfx9.swizzle_mode = surface->u.gfx9.surf.swizzle_mode;
620 } else {
621 md.u.legacy.microtile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D ?
622 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
623 md.u.legacy.macrotile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D ?
624 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
625 md.u.legacy.pipe_config = surface->u.legacy.pipe_config;
626 md.u.legacy.bankw = surface->u.legacy.bankw;
627 md.u.legacy.bankh = surface->u.legacy.bankh;
628 md.u.legacy.tile_split = surface->u.legacy.tile_split;
629 md.u.legacy.mtilea = surface->u.legacy.mtilea;
630 md.u.legacy.num_banks = surface->u.legacy.num_banks;
631 md.u.legacy.stride = surface->u.legacy.level[0].nblk_x * surface->bpe;
632 md.u.legacy.scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
633 }
634
635 assert(tex->dcc_separate_buffer == NULL);
636 assert(tex->surface.fmask_size == 0);
637
638 /* Metadata image format format version 1:
639 * [0] = 1 (metadata format identifier)
640 * [1] = (VENDOR_ID << 16) | PCI_ID
641 * [2:9] = image descriptor for the whole resource
642 * [2] is always 0, because the base address is cleared
643 * [9] is the DCC offset bits [39:8] from the beginning of
644 * the buffer
645 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
646 */
647
648 md.metadata[0] = 1; /* metadata image format version 1 */
649
650 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
651 md.metadata[1] = si_get_bo_metadata_word1(sscreen);
652
653 static const unsigned char swizzle[] = {
654 PIPE_SWIZZLE_X,
655 PIPE_SWIZZLE_Y,
656 PIPE_SWIZZLE_Z,
657 PIPE_SWIZZLE_W
658 };
659 bool is_array = util_texture_is_array(res->target);
660 uint32_t desc[8];
661
662 si_make_texture_descriptor(sscreen, tex, true,
663 res->target, res->format,
664 swizzle, 0, res->last_level, 0,
665 is_array ? res->array_size - 1 : 0,
666 res->width0, res->height0, res->depth0,
667 desc, NULL);
668
669 si_set_mutable_tex_desc_fields(sscreen, tex, &tex->surface.u.legacy.level[0],
670 0, 0, tex->surface.blk_w, false, desc);
671
672 /* Clear the base address and set the relative DCC offset. */
673 desc[0] = 0;
674 desc[1] &= C_008F14_BASE_ADDRESS_HI;
675 desc[7] = tex->dcc_offset >> 8;
676
677 /* Dwords [2:9] contain the image descriptor. */
678 memcpy(&md.metadata[2], desc, sizeof(desc));
679 md.size_metadata = 10 * 4;
680
681 /* Dwords [10:..] contain the mipmap level offsets. */
682 if (sscreen->info.chip_class <= VI) {
683 for (unsigned i = 0; i <= res->last_level; i++)
684 md.metadata[10+i] = tex->surface.u.legacy.level[i].offset >> 8;
685
686 md.size_metadata += (1 + res->last_level) * 4;
687 }
688
689 sscreen->ws->buffer_set_metadata(tex->buffer.buf, &md);
690 }
691
692 static void si_get_opaque_metadata(struct si_screen *sscreen,
693 struct si_texture *tex,
694 struct radeon_bo_metadata *md)
695 {
696 uint32_t *desc = &md->metadata[2];
697
698 if (sscreen->info.chip_class < VI)
699 return;
700
701 /* Return if DCC is enabled. The texture should be set up with it
702 * already.
703 */
704 if (md->size_metadata >= 10 * 4 && /* at least 2(header) + 8(desc) dwords */
705 md->metadata[0] != 0 &&
706 md->metadata[1] == si_get_bo_metadata_word1(sscreen) &&
707 G_008F28_COMPRESSION_EN(desc[6])) {
708 tex->dcc_offset = (uint64_t)desc[7] << 8;
709 return;
710 }
711
712 /* Disable DCC. These are always set by texture_from_handle and must
713 * be cleared here.
714 */
715 tex->dcc_offset = 0;
716 }
717
718 static boolean si_texture_get_handle(struct pipe_screen* screen,
719 struct pipe_context *ctx,
720 struct pipe_resource *resource,
721 struct winsys_handle *whandle,
722 unsigned usage)
723 {
724 struct si_screen *sscreen = (struct si_screen*)screen;
725 struct si_context *sctx;
726 struct si_resource *res = si_resource(resource);
727 struct si_texture *tex = (struct si_texture*)resource;
728 bool update_metadata = false;
729 unsigned stride, offset, slice_size;
730 bool flush = false;
731
732 ctx = threaded_context_unwrap_sync(ctx);
733 sctx = (struct si_context*)(ctx ? ctx : sscreen->aux_context);
734
735 if (resource->target != PIPE_BUFFER) {
736 /* This is not supported now, but it might be required for OpenCL
737 * interop in the future.
738 */
739 if (resource->nr_samples > 1 || tex->is_depth)
740 return false;
741
742 /* Move a suballocated texture into a non-suballocated allocation. */
743 if (sscreen->ws->buffer_is_suballocated(res->buf) ||
744 tex->surface.tile_swizzle ||
745 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
746 sscreen->info.has_local_buffers &&
747 whandle->type != WINSYS_HANDLE_TYPE_KMS)) {
748 assert(!res->b.is_shared);
749 si_reallocate_texture_inplace(sctx, tex,
750 PIPE_BIND_SHARED, false);
751 flush = true;
752 assert(res->b.b.bind & PIPE_BIND_SHARED);
753 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
754 assert(!(res->flags & RADEON_FLAG_NO_INTERPROCESS_SHARING));
755 assert(tex->surface.tile_swizzle == 0);
756 }
757
758 /* Since shader image stores don't support DCC on VI,
759 * disable it for external clients that want write
760 * access.
761 */
762 if (usage & PIPE_HANDLE_USAGE_SHADER_WRITE && tex->dcc_offset) {
763 if (si_texture_disable_dcc(sctx, tex)) {
764 update_metadata = true;
765 /* si_texture_disable_dcc flushes the context */
766 flush = false;
767 }
768 }
769
770 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
771 (tex->cmask_buffer || tex->dcc_offset)) {
772 /* Eliminate fast clear (both CMASK and DCC) */
773 si_eliminate_fast_color_clear(sctx, tex);
774 /* eliminate_fast_color_clear flushes the context */
775 flush = false;
776
777 /* Disable CMASK if flush_resource isn't going
778 * to be called.
779 */
780 if (tex->cmask_buffer)
781 si_texture_discard_cmask(sscreen, tex);
782 }
783
784 /* Set metadata. */
785 if (!res->b.is_shared || update_metadata)
786 si_set_tex_bo_metadata(sscreen, tex);
787
788 if (sscreen->info.chip_class >= GFX9) {
789 offset = tex->surface.u.gfx9.surf_offset;
790 stride = tex->surface.u.gfx9.surf_pitch *
791 tex->surface.bpe;
792 slice_size = tex->surface.u.gfx9.surf_slice_size;
793 } else {
794 offset = tex->surface.u.legacy.level[0].offset;
795 stride = tex->surface.u.legacy.level[0].nblk_x *
796 tex->surface.bpe;
797 slice_size = (uint64_t)tex->surface.u.legacy.level[0].slice_size_dw * 4;
798 }
799 } else {
800 /* Buffer exports are for the OpenCL interop. */
801 /* Move a suballocated buffer into a non-suballocated allocation. */
802 if (sscreen->ws->buffer_is_suballocated(res->buf) ||
803 /* A DMABUF export always fails if the BO is local. */
804 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
805 sscreen->info.has_local_buffers)) {
806 assert(!res->b.is_shared);
807
808 /* Allocate a new buffer with PIPE_BIND_SHARED. */
809 struct pipe_resource templ = res->b.b;
810 templ.bind |= PIPE_BIND_SHARED;
811
812 struct pipe_resource *newb =
813 screen->resource_create(screen, &templ);
814 if (!newb)
815 return false;
816
817 /* Copy the old buffer contents to the new one. */
818 struct pipe_box box;
819 u_box_1d(0, newb->width0, &box);
820 sctx->b.resource_copy_region(&sctx->b, newb, 0, 0, 0, 0,
821 &res->b.b, 0, &box);
822 flush = true;
823 /* Move the new buffer storage to the old pipe_resource. */
824 si_replace_buffer_storage(&sctx->b, &res->b.b, newb);
825 pipe_resource_reference(&newb, NULL);
826
827 assert(res->b.b.bind & PIPE_BIND_SHARED);
828 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
829 }
830
831 /* Buffers */
832 offset = 0;
833 stride = 0;
834 slice_size = 0;
835 }
836
837 if (flush)
838 sctx->b.flush(&sctx->b, NULL, 0);
839
840 if (res->b.is_shared) {
841 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
842 * doesn't set it.
843 */
844 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
845 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
846 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
847 } else {
848 res->b.is_shared = true;
849 res->external_usage = usage;
850 }
851
852 return sscreen->ws->buffer_get_handle(res->buf, stride, offset,
853 slice_size, whandle);
854 }
855
856 static void si_texture_destroy(struct pipe_screen *screen,
857 struct pipe_resource *ptex)
858 {
859 struct si_texture *tex = (struct si_texture*)ptex;
860 struct si_resource *resource = &tex->buffer;
861
862 si_texture_reference(&tex->flushed_depth_texture, NULL);
863
864 if (tex->cmask_buffer != &tex->buffer) {
865 si_resource_reference(&tex->cmask_buffer, NULL);
866 }
867 pb_reference(&resource->buf, NULL);
868 si_resource_reference(&tex->dcc_separate_buffer, NULL);
869 si_resource_reference(&tex->last_dcc_separate_buffer, NULL);
870 FREE(tex);
871 }
872
873 static const struct u_resource_vtbl si_texture_vtbl;
874
875 static void si_texture_get_htile_size(struct si_screen *sscreen,
876 struct si_texture *tex)
877 {
878 unsigned cl_width, cl_height, width, height;
879 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
880 unsigned num_pipes = sscreen->info.num_tile_pipes;
881
882 assert(sscreen->info.chip_class <= VI);
883
884 tex->surface.htile_size = 0;
885
886 if (tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
887 !sscreen->info.htile_cmask_support_1d_tiling)
888 return;
889
890 /* Overalign HTILE on P2 configs to work around GPU hangs in
891 * piglit/depthstencil-render-miplevels 585.
892 *
893 * This has been confirmed to help Kabini & Stoney, where the hangs
894 * are always reproducible. I think I have seen the test hang
895 * on Carrizo too, though it was very rare there.
896 */
897 if (sscreen->info.chip_class >= CIK && num_pipes < 4)
898 num_pipes = 4;
899
900 switch (num_pipes) {
901 case 1:
902 cl_width = 32;
903 cl_height = 16;
904 break;
905 case 2:
906 cl_width = 32;
907 cl_height = 32;
908 break;
909 case 4:
910 cl_width = 64;
911 cl_height = 32;
912 break;
913 case 8:
914 cl_width = 64;
915 cl_height = 64;
916 break;
917 case 16:
918 cl_width = 128;
919 cl_height = 64;
920 break;
921 default:
922 assert(0);
923 return;
924 }
925
926 width = align(tex->surface.u.legacy.level[0].nblk_x, cl_width * 8);
927 height = align(tex->surface.u.legacy.level[0].nblk_y, cl_height * 8);
928
929 slice_elements = (width * height) / (8 * 8);
930 slice_bytes = slice_elements * 4;
931
932 pipe_interleave_bytes = sscreen->info.pipe_interleave_bytes;
933 base_align = num_pipes * pipe_interleave_bytes;
934
935 tex->surface.htile_alignment = base_align;
936 tex->surface.htile_size =
937 util_num_layers(&tex->buffer.b.b, 0) *
938 align(slice_bytes, base_align);
939 }
940
941 static void si_texture_allocate_htile(struct si_screen *sscreen,
942 struct si_texture *tex)
943 {
944 if (sscreen->info.chip_class <= VI && !tex->tc_compatible_htile)
945 si_texture_get_htile_size(sscreen, tex);
946
947 if (!tex->surface.htile_size)
948 return;
949
950 tex->htile_offset = align(tex->size, tex->surface.htile_alignment);
951 tex->size = tex->htile_offset + tex->surface.htile_size;
952 }
953
954 void si_print_texture_info(struct si_screen *sscreen,
955 struct si_texture *tex, struct u_log_context *log)
956 {
957 int i;
958
959 /* Common parameters. */
960 u_log_printf(log, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
961 "blk_h=%u, array_size=%u, last_level=%u, "
962 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
963 tex->buffer.b.b.width0, tex->buffer.b.b.height0,
964 tex->buffer.b.b.depth0, tex->surface.blk_w,
965 tex->surface.blk_h,
966 tex->buffer.b.b.array_size, tex->buffer.b.b.last_level,
967 tex->surface.bpe, tex->buffer.b.b.nr_samples,
968 tex->surface.flags, util_format_short_name(tex->buffer.b.b.format));
969
970 if (sscreen->info.chip_class >= GFX9) {
971 u_log_printf(log, " Surf: size=%"PRIu64", slice_size=%"PRIu64", "
972 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
973 tex->surface.surf_size,
974 tex->surface.u.gfx9.surf_slice_size,
975 tex->surface.surf_alignment,
976 tex->surface.u.gfx9.surf.swizzle_mode,
977 tex->surface.u.gfx9.surf.epitch,
978 tex->surface.u.gfx9.surf_pitch);
979
980 if (tex->surface.fmask_size) {
981 u_log_printf(log, " FMASK: offset=%"PRIu64", size=%"PRIu64", "
982 "alignment=%u, swmode=%u, epitch=%u\n",
983 tex->fmask_offset,
984 tex->surface.fmask_size,
985 tex->surface.fmask_alignment,
986 tex->surface.u.gfx9.fmask.swizzle_mode,
987 tex->surface.u.gfx9.fmask.epitch);
988 }
989
990 if (tex->cmask_buffer) {
991 u_log_printf(log, " CMask: offset=%"PRIu64", size=%u, "
992 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
993 tex->cmask_offset,
994 tex->surface.cmask_size,
995 tex->surface.cmask_alignment,
996 tex->surface.u.gfx9.cmask.rb_aligned,
997 tex->surface.u.gfx9.cmask.pipe_aligned);
998 }
999
1000 if (tex->htile_offset) {
1001 u_log_printf(log, " HTile: offset=%"PRIu64", size=%u, alignment=%u, "
1002 "rb_aligned=%u, pipe_aligned=%u\n",
1003 tex->htile_offset,
1004 tex->surface.htile_size,
1005 tex->surface.htile_alignment,
1006 tex->surface.u.gfx9.htile.rb_aligned,
1007 tex->surface.u.gfx9.htile.pipe_aligned);
1008 }
1009
1010 if (tex->dcc_offset) {
1011 u_log_printf(log, " DCC: offset=%"PRIu64", size=%u, "
1012 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1013 tex->dcc_offset, tex->surface.dcc_size,
1014 tex->surface.dcc_alignment,
1015 tex->surface.u.gfx9.dcc_pitch_max,
1016 tex->surface.num_dcc_levels);
1017 }
1018
1019 if (tex->surface.u.gfx9.stencil_offset) {
1020 u_log_printf(log, " Stencil: offset=%"PRIu64", swmode=%u, epitch=%u\n",
1021 tex->surface.u.gfx9.stencil_offset,
1022 tex->surface.u.gfx9.stencil.swizzle_mode,
1023 tex->surface.u.gfx9.stencil.epitch);
1024 }
1025 return;
1026 }
1027
1028 u_log_printf(log, " Layout: size=%"PRIu64", alignment=%u, bankw=%u, "
1029 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1030 tex->surface.surf_size, tex->surface.surf_alignment, tex->surface.u.legacy.bankw,
1031 tex->surface.u.legacy.bankh, tex->surface.u.legacy.num_banks, tex->surface.u.legacy.mtilea,
1032 tex->surface.u.legacy.tile_split, tex->surface.u.legacy.pipe_config,
1033 (tex->surface.flags & RADEON_SURF_SCANOUT) != 0);
1034
1035 if (tex->surface.fmask_size)
1036 u_log_printf(log, " FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
1037 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1038 tex->fmask_offset, tex->surface.fmask_size, tex->surface.fmask_alignment,
1039 tex->surface.u.legacy.fmask.pitch_in_pixels,
1040 tex->surface.u.legacy.fmask.bankh,
1041 tex->surface.u.legacy.fmask.slice_tile_max,
1042 tex->surface.u.legacy.fmask.tiling_index);
1043
1044 if (tex->cmask_buffer)
1045 u_log_printf(log, " CMask: offset=%"PRIu64", size=%u, alignment=%u, "
1046 "slice_tile_max=%u\n",
1047 tex->cmask_offset, tex->surface.cmask_size, tex->surface.cmask_alignment,
1048 tex->surface.u.legacy.cmask_slice_tile_max);
1049
1050 if (tex->htile_offset)
1051 u_log_printf(log, " HTile: offset=%"PRIu64", size=%u, "
1052 "alignment=%u, TC_compatible = %u\n",
1053 tex->htile_offset, tex->surface.htile_size,
1054 tex->surface.htile_alignment,
1055 tex->tc_compatible_htile);
1056
1057 if (tex->dcc_offset) {
1058 u_log_printf(log, " DCC: offset=%"PRIu64", size=%u, alignment=%u\n",
1059 tex->dcc_offset, tex->surface.dcc_size,
1060 tex->surface.dcc_alignment);
1061 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
1062 u_log_printf(log, " DCCLevel[%i]: enabled=%u, offset=%u, "
1063 "fast_clear_size=%u\n",
1064 i, i < tex->surface.num_dcc_levels,
1065 tex->surface.u.legacy.level[i].dcc_offset,
1066 tex->surface.u.legacy.level[i].dcc_fast_clear_size);
1067 }
1068
1069 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
1070 u_log_printf(log, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
1071 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1072 "mode=%u, tiling_index = %u\n",
1073 i, tex->surface.u.legacy.level[i].offset,
1074 (uint64_t)tex->surface.u.legacy.level[i].slice_size_dw * 4,
1075 u_minify(tex->buffer.b.b.width0, i),
1076 u_minify(tex->buffer.b.b.height0, i),
1077 u_minify(tex->buffer.b.b.depth0, i),
1078 tex->surface.u.legacy.level[i].nblk_x,
1079 tex->surface.u.legacy.level[i].nblk_y,
1080 tex->surface.u.legacy.level[i].mode,
1081 tex->surface.u.legacy.tiling_index[i]);
1082
1083 if (tex->surface.has_stencil) {
1084 u_log_printf(log, " StencilLayout: tilesplit=%u\n",
1085 tex->surface.u.legacy.stencil_tile_split);
1086 for (i = 0; i <= tex->buffer.b.b.last_level; i++) {
1087 u_log_printf(log, " StencilLevel[%i]: offset=%"PRIu64", "
1088 "slice_size=%"PRIu64", npix_x=%u, "
1089 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1090 "mode=%u, tiling_index = %u\n",
1091 i, tex->surface.u.legacy.stencil_level[i].offset,
1092 (uint64_t)tex->surface.u.legacy.stencil_level[i].slice_size_dw * 4,
1093 u_minify(tex->buffer.b.b.width0, i),
1094 u_minify(tex->buffer.b.b.height0, i),
1095 u_minify(tex->buffer.b.b.depth0, i),
1096 tex->surface.u.legacy.stencil_level[i].nblk_x,
1097 tex->surface.u.legacy.stencil_level[i].nblk_y,
1098 tex->surface.u.legacy.stencil_level[i].mode,
1099 tex->surface.u.legacy.stencil_tiling_index[i]);
1100 }
1101 }
1102 }
1103
1104 /* Common processing for si_texture_create and si_texture_from_handle */
1105 static struct si_texture *
1106 si_texture_create_object(struct pipe_screen *screen,
1107 const struct pipe_resource *base,
1108 struct pb_buffer *buf,
1109 struct radeon_surf *surface)
1110 {
1111 struct si_texture *tex;
1112 struct si_resource *resource;
1113 struct si_screen *sscreen = (struct si_screen*)screen;
1114
1115 tex = CALLOC_STRUCT(si_texture);
1116 if (!tex)
1117 goto error;
1118
1119 resource = &tex->buffer;
1120 resource->b.b = *base;
1121 resource->b.b.next = NULL;
1122 resource->b.vtbl = &si_texture_vtbl;
1123 pipe_reference_init(&resource->b.b.reference, 1);
1124 resource->b.b.screen = screen;
1125
1126 /* don't include stencil-only formats which we don't support for rendering */
1127 tex->is_depth = util_format_has_depth(util_format_description(tex->buffer.b.b.format));
1128
1129 tex->surface = *surface;
1130 tex->size = tex->surface.surf_size;
1131
1132 tex->tc_compatible_htile = tex->surface.htile_size != 0 &&
1133 (tex->surface.flags &
1134 RADEON_SURF_TC_COMPATIBLE_HTILE);
1135
1136 /* TC-compatible HTILE:
1137 * - VI only supports Z32_FLOAT.
1138 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1139 if (tex->tc_compatible_htile) {
1140 if (sscreen->info.chip_class >= GFX9 &&
1141 base->format == PIPE_FORMAT_Z16_UNORM)
1142 tex->db_render_format = base->format;
1143 else {
1144 tex->db_render_format = PIPE_FORMAT_Z32_FLOAT;
1145 tex->upgraded_depth = base->format != PIPE_FORMAT_Z32_FLOAT &&
1146 base->format != PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
1147 }
1148 } else {
1149 tex->db_render_format = base->format;
1150 }
1151
1152 /* Applies to GCN. */
1153 tex->last_msaa_resolve_target_micro_mode = tex->surface.micro_tile_mode;
1154
1155 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1156 * between frames, so the only thing that can enable separate DCC
1157 * with DRI2 is multiple slow clears within a frame.
1158 */
1159 tex->ps_draw_ratio = 0;
1160
1161 if (tex->is_depth) {
1162 if (sscreen->info.chip_class >= GFX9) {
1163 tex->can_sample_z = true;
1164 tex->can_sample_s = true;
1165 } else {
1166 tex->can_sample_z = !tex->surface.u.legacy.depth_adjusted;
1167 tex->can_sample_s = !tex->surface.u.legacy.stencil_adjusted;
1168 }
1169
1170 if (!(base->flags & (SI_RESOURCE_FLAG_TRANSFER |
1171 SI_RESOURCE_FLAG_FLUSHED_DEPTH))) {
1172 tex->db_compatible = true;
1173
1174 if (!(sscreen->debug_flags & DBG(NO_HYPERZ)))
1175 si_texture_allocate_htile(sscreen, tex);
1176 }
1177 } else {
1178 if (base->nr_samples > 1 &&
1179 !buf &&
1180 !(sscreen->debug_flags & DBG(NO_FMASK))) {
1181 /* Allocate FMASK. */
1182 tex->fmask_offset = align64(tex->size,
1183 tex->surface.fmask_alignment);
1184 tex->size = tex->fmask_offset + tex->surface.fmask_size;
1185
1186 /* Allocate CMASK. */
1187 tex->cmask_offset = align64(tex->size, tex->surface.cmask_alignment);
1188 tex->size = tex->cmask_offset + tex->surface.cmask_size;
1189 tex->cb_color_info |= S_028C70_FAST_CLEAR(1);
1190 tex->cmask_buffer = &tex->buffer;
1191
1192 if (!tex->surface.fmask_size || !tex->surface.cmask_size)
1193 goto error;
1194 }
1195
1196 /* Shared textures must always set up DCC here.
1197 * If it's not present, it will be disabled by
1198 * apply_opaque_metadata later.
1199 */
1200 if (tex->surface.dcc_size &&
1201 (buf || !(sscreen->debug_flags & DBG(NO_DCC))) &&
1202 !(tex->surface.flags & RADEON_SURF_SCANOUT)) {
1203 /* Reserve space for the DCC buffer. */
1204 tex->dcc_offset = align64(tex->size, tex->surface.dcc_alignment);
1205 tex->size = tex->dcc_offset + tex->surface.dcc_size;
1206 }
1207 }
1208
1209 /* Now create the backing buffer. */
1210 if (!buf) {
1211 si_init_resource_fields(sscreen, resource, tex->size,
1212 tex->surface.surf_alignment);
1213
1214 if (!si_alloc_resource(sscreen, resource))
1215 goto error;
1216 } else {
1217 resource->buf = buf;
1218 resource->gpu_address = sscreen->ws->buffer_get_virtual_address(resource->buf);
1219 resource->bo_size = buf->size;
1220 resource->bo_alignment = buf->alignment;
1221 resource->domains = sscreen->ws->buffer_get_initial_domain(resource->buf);
1222 if (resource->domains & RADEON_DOMAIN_VRAM)
1223 resource->vram_usage = buf->size;
1224 else if (resource->domains & RADEON_DOMAIN_GTT)
1225 resource->gart_usage = buf->size;
1226 }
1227
1228 if (tex->cmask_buffer) {
1229 /* Initialize the cmask to 0xCC (= compressed state). */
1230 si_screen_clear_buffer(sscreen, &tex->cmask_buffer->b.b,
1231 tex->cmask_offset, tex->surface.cmask_size,
1232 0xCCCCCCCC);
1233 }
1234 if (tex->htile_offset) {
1235 uint32_t clear_value = 0;
1236
1237 if (sscreen->info.chip_class >= GFX9 || tex->tc_compatible_htile)
1238 clear_value = 0x0000030F;
1239
1240 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1241 tex->htile_offset,
1242 tex->surface.htile_size,
1243 clear_value);
1244 }
1245
1246 /* Initialize DCC only if the texture is not being imported. */
1247 if (!buf && tex->dcc_offset) {
1248 /* Clear DCC to black for all tiles with DCC enabled.
1249 *
1250 * This fixes corruption in 3DMark Slingshot Extreme, which
1251 * uses uninitialized textures, causing corruption.
1252 */
1253 if (tex->surface.num_dcc_levels == tex->buffer.b.b.last_level + 1 &&
1254 tex->buffer.b.b.nr_samples <= 2) {
1255 /* Simple case - all tiles have DCC enabled. */
1256 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1257 tex->dcc_offset,
1258 tex->surface.dcc_size,
1259 DCC_CLEAR_COLOR_0000);
1260 } else if (sscreen->info.chip_class >= GFX9) {
1261 /* Clear to uncompressed. Clearing this to black is complicated. */
1262 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1263 tex->dcc_offset,
1264 tex->surface.dcc_size,
1265 DCC_UNCOMPRESSED);
1266 } else {
1267 /* GFX8: Initialize mipmap levels and multisamples separately. */
1268 if (tex->buffer.b.b.nr_samples >= 2) {
1269 /* Clearing this to black is complicated. */
1270 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1271 tex->dcc_offset,
1272 tex->surface.dcc_size,
1273 DCC_UNCOMPRESSED);
1274 } else {
1275 /* Clear the enabled mipmap levels to black. */
1276 unsigned size = 0;
1277
1278 for (unsigned i = 0; i < tex->surface.num_dcc_levels; i++) {
1279 if (!tex->surface.u.legacy.level[i].dcc_fast_clear_size)
1280 break;
1281
1282 size = tex->surface.u.legacy.level[i].dcc_offset +
1283 tex->surface.u.legacy.level[i].dcc_fast_clear_size;
1284 }
1285
1286 /* Mipmap levels with DCC. */
1287 if (size) {
1288 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1289 tex->dcc_offset, size,
1290 DCC_CLEAR_COLOR_0000);
1291 }
1292 /* Mipmap levels without DCC. */
1293 if (size != tex->surface.dcc_size) {
1294 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1295 tex->dcc_offset + size,
1296 tex->surface.dcc_size - size,
1297 DCC_UNCOMPRESSED);
1298 }
1299 }
1300 }
1301 }
1302
1303 /* Initialize the CMASK base register value. */
1304 tex->cmask_base_address_reg =
1305 (tex->buffer.gpu_address + tex->cmask_offset) >> 8;
1306
1307 if (sscreen->debug_flags & DBG(VM)) {
1308 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1309 tex->buffer.gpu_address,
1310 tex->buffer.gpu_address + tex->buffer.buf->size,
1311 base->width0, base->height0, util_num_layers(base, 0), base->last_level+1,
1312 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1313 }
1314
1315 if (sscreen->debug_flags & DBG(TEX)) {
1316 puts("Texture:");
1317 struct u_log_context log;
1318 u_log_context_init(&log);
1319 si_print_texture_info(sscreen, tex, &log);
1320 u_log_new_page_print(&log, stdout);
1321 fflush(stdout);
1322 u_log_context_destroy(&log);
1323 }
1324
1325 return tex;
1326
1327 error:
1328 FREE(tex);
1329 return NULL;
1330 }
1331
1332 static enum radeon_surf_mode
1333 si_choose_tiling(struct si_screen *sscreen,
1334 const struct pipe_resource *templ, bool tc_compatible_htile)
1335 {
1336 const struct util_format_description *desc = util_format_description(templ->format);
1337 bool force_tiling = templ->flags & SI_RESOURCE_FLAG_FORCE_MSAA_TILING;
1338 bool is_depth_stencil = util_format_is_depth_or_stencil(templ->format) &&
1339 !(templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH);
1340
1341 /* MSAA resources must be 2D tiled. */
1342 if (templ->nr_samples > 1)
1343 return RADEON_SURF_MODE_2D;
1344
1345 /* Transfer resources should be linear. */
1346 if (templ->flags & SI_RESOURCE_FLAG_TRANSFER)
1347 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1348
1349 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1350 * which requires 2D tiling.
1351 */
1352 if (sscreen->info.chip_class == VI && tc_compatible_htile)
1353 return RADEON_SURF_MODE_2D;
1354
1355 /* Handle common candidates for the linear mode.
1356 * Compressed textures and DB surfaces must always be tiled.
1357 */
1358 if (!force_tiling &&
1359 !is_depth_stencil &&
1360 !util_format_is_compressed(templ->format)) {
1361 if (sscreen->debug_flags & DBG(NO_TILING))
1362 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1363
1364 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1365 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1366 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1367
1368 /* Cursors are linear on SI.
1369 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1370 if (templ->bind & PIPE_BIND_CURSOR)
1371 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1372
1373 if (templ->bind & PIPE_BIND_LINEAR)
1374 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1375
1376 /* Textures with a very small height are recommended to be linear. */
1377 if (templ->target == PIPE_TEXTURE_1D ||
1378 templ->target == PIPE_TEXTURE_1D_ARRAY ||
1379 /* Only very thin and long 2D textures should benefit from
1380 * linear_aligned. */
1381 (templ->width0 > 8 && templ->height0 <= 2))
1382 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1383
1384 /* Textures likely to be mapped often. */
1385 if (templ->usage == PIPE_USAGE_STAGING ||
1386 templ->usage == PIPE_USAGE_STREAM)
1387 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1388 }
1389
1390 /* Make small textures 1D tiled. */
1391 if (templ->width0 <= 16 || templ->height0 <= 16 ||
1392 (sscreen->debug_flags & DBG(NO_2D_TILING)))
1393 return RADEON_SURF_MODE_1D;
1394
1395 /* The allocator will switch to 1D if needed. */
1396 return RADEON_SURF_MODE_2D;
1397 }
1398
1399 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
1400 const struct pipe_resource *templ)
1401 {
1402 struct si_screen *sscreen = (struct si_screen*)screen;
1403 bool is_zs = util_format_is_depth_or_stencil(templ->format);
1404
1405 if (templ->nr_samples >= 2) {
1406 /* This is hackish (overwriting the const pipe_resource template),
1407 * but should be harmless and state trackers can also see
1408 * the overriden number of samples in the created pipe_resource.
1409 */
1410 if (is_zs && sscreen->eqaa_force_z_samples) {
1411 ((struct pipe_resource*)templ)->nr_samples =
1412 ((struct pipe_resource*)templ)->nr_storage_samples =
1413 sscreen->eqaa_force_z_samples;
1414 } else if (!is_zs && sscreen->eqaa_force_color_samples) {
1415 ((struct pipe_resource*)templ)->nr_samples =
1416 sscreen->eqaa_force_coverage_samples;
1417 ((struct pipe_resource*)templ)->nr_storage_samples =
1418 sscreen->eqaa_force_color_samples;
1419 }
1420 }
1421
1422 struct radeon_surf surface = {0};
1423 bool is_flushed_depth = templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH;
1424 bool tc_compatible_htile =
1425 sscreen->info.chip_class >= VI &&
1426 /* There are issues with TC-compatible HTILE on Tonga (and
1427 * Iceland is the same design), and documented bug workarounds
1428 * don't help. For example, this fails:
1429 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1430 */
1431 sscreen->info.family != CHIP_TONGA &&
1432 sscreen->info.family != CHIP_ICELAND &&
1433 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) &&
1434 !(sscreen->debug_flags & DBG(NO_HYPERZ)) &&
1435 !is_flushed_depth &&
1436 templ->nr_samples <= 1 && /* TC-compat HTILE is less efficient with MSAA */
1437 is_zs;
1438 int r;
1439
1440 r = si_init_surface(sscreen, &surface, templ,
1441 si_choose_tiling(sscreen, templ, tc_compatible_htile),
1442 0, 0, false, false, is_flushed_depth,
1443 tc_compatible_htile);
1444 if (r) {
1445 return NULL;
1446 }
1447
1448 return (struct pipe_resource *)
1449 si_texture_create_object(screen, templ, NULL, &surface);
1450 }
1451
1452 static struct pipe_resource *si_texture_from_winsys_buffer(struct si_screen *sscreen,
1453 const struct pipe_resource *templ,
1454 struct pb_buffer *buf,
1455 unsigned stride,
1456 unsigned offset,
1457 unsigned usage,
1458 bool dedicated)
1459 {
1460 enum radeon_surf_mode array_mode;
1461 struct radeon_surf surface = {};
1462 struct radeon_bo_metadata metadata = {};
1463 struct si_texture *tex;
1464 bool is_scanout;
1465 int r;
1466
1467 if (dedicated) {
1468 sscreen->ws->buffer_get_metadata(buf, &metadata);
1469 si_get_display_metadata(sscreen, &surface, &metadata,
1470 &array_mode, &is_scanout);
1471 } else {
1472 /**
1473 * The bo metadata is unset for un-dedicated images. So we fall
1474 * back to linear. See answer to question 5 of the
1475 * VK_KHX_external_memory spec for some details.
1476 *
1477 * It is possible that this case isn't going to work if the
1478 * surface pitch isn't correctly aligned by default.
1479 *
1480 * In order to support it correctly we require multi-image
1481 * metadata to be syncrhonized between radv and radeonsi. The
1482 * semantics of associating multiple image metadata to a memory
1483 * object on the vulkan export side are not concretely defined
1484 * either.
1485 *
1486 * All the use cases we are aware of at the moment for memory
1487 * objects use dedicated allocations. So lets keep the initial
1488 * implementation simple.
1489 *
1490 * A possible alternative is to attempt to reconstruct the
1491 * tiling information when the TexParameter TEXTURE_TILING_EXT
1492 * is set.
1493 */
1494 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1495 is_scanout = false;
1496 }
1497
1498 r = si_init_surface(sscreen, &surface, templ,
1499 array_mode, stride, offset, true, is_scanout,
1500 false, false);
1501 if (r)
1502 return NULL;
1503
1504 tex = si_texture_create_object(&sscreen->b, templ, buf, &surface);
1505 if (!tex)
1506 return NULL;
1507
1508 tex->buffer.b.is_shared = true;
1509 tex->buffer.external_usage = usage;
1510
1511 si_get_opaque_metadata(sscreen, tex, &metadata);
1512
1513 assert(tex->surface.tile_swizzle == 0);
1514 return &tex->buffer.b.b;
1515 }
1516
1517 static struct pipe_resource *si_texture_from_handle(struct pipe_screen *screen,
1518 const struct pipe_resource *templ,
1519 struct winsys_handle *whandle,
1520 unsigned usage)
1521 {
1522 struct si_screen *sscreen = (struct si_screen*)screen;
1523 struct pb_buffer *buf = NULL;
1524 unsigned stride = 0, offset = 0;
1525
1526 /* Support only 2D textures without mipmaps */
1527 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
1528 templ->depth0 != 1 || templ->last_level != 0)
1529 return NULL;
1530
1531 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle,
1532 sscreen->info.max_alignment,
1533 &stride, &offset);
1534 if (!buf)
1535 return NULL;
1536
1537 return si_texture_from_winsys_buffer(sscreen, templ, buf, stride,
1538 offset, usage, true);
1539 }
1540
1541 bool si_init_flushed_depth_texture(struct pipe_context *ctx,
1542 struct pipe_resource *texture,
1543 struct si_texture **staging)
1544 {
1545 struct si_texture *tex = (struct si_texture*)texture;
1546 struct pipe_resource resource;
1547 struct si_texture **flushed_depth_texture = staging ?
1548 staging : &tex->flushed_depth_texture;
1549 enum pipe_format pipe_format = texture->format;
1550
1551 if (!staging) {
1552 if (tex->flushed_depth_texture)
1553 return true; /* it's ready */
1554
1555 if (!tex->can_sample_z && tex->can_sample_s) {
1556 switch (pipe_format) {
1557 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1558 /* Save memory by not allocating the S plane. */
1559 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1560 break;
1561 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1562 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1563 /* Save memory bandwidth by not copying the
1564 * stencil part during flush.
1565 *
1566 * This potentially increases memory bandwidth
1567 * if an application uses both Z and S texturing
1568 * simultaneously (a flushed Z24S8 texture
1569 * would be stored compactly), but how often
1570 * does that really happen?
1571 */
1572 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1573 break;
1574 default:;
1575 }
1576 } else if (!tex->can_sample_s && tex->can_sample_z) {
1577 assert(util_format_has_stencil(util_format_description(pipe_format)));
1578
1579 /* DB->CB copies to an 8bpp surface don't work. */
1580 pipe_format = PIPE_FORMAT_X24S8_UINT;
1581 }
1582 }
1583
1584 memset(&resource, 0, sizeof(resource));
1585 resource.target = texture->target;
1586 resource.format = pipe_format;
1587 resource.width0 = texture->width0;
1588 resource.height0 = texture->height0;
1589 resource.depth0 = texture->depth0;
1590 resource.array_size = texture->array_size;
1591 resource.last_level = texture->last_level;
1592 resource.nr_samples = texture->nr_samples;
1593 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1594 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1595 resource.flags = texture->flags | SI_RESOURCE_FLAG_FLUSHED_DEPTH;
1596
1597 if (staging)
1598 resource.flags |= SI_RESOURCE_FLAG_TRANSFER;
1599
1600 *flushed_depth_texture = (struct si_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1601 if (*flushed_depth_texture == NULL) {
1602 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1603 return false;
1604 }
1605 return true;
1606 }
1607
1608 /**
1609 * Initialize the pipe_resource descriptor to be of the same size as the box,
1610 * which is supposed to hold a subregion of the texture "orig" at the given
1611 * mipmap level.
1612 */
1613 static void si_init_temp_resource_from_box(struct pipe_resource *res,
1614 struct pipe_resource *orig,
1615 const struct pipe_box *box,
1616 unsigned level, unsigned flags)
1617 {
1618 memset(res, 0, sizeof(*res));
1619 res->format = orig->format;
1620 res->width0 = box->width;
1621 res->height0 = box->height;
1622 res->depth0 = 1;
1623 res->array_size = 1;
1624 res->usage = flags & SI_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1625 res->flags = flags;
1626
1627 /* We must set the correct texture target and dimensions for a 3D box. */
1628 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1629 res->target = PIPE_TEXTURE_2D_ARRAY;
1630 res->array_size = box->depth;
1631 } else {
1632 res->target = PIPE_TEXTURE_2D;
1633 }
1634 }
1635
1636 static bool si_can_invalidate_texture(struct si_screen *sscreen,
1637 struct si_texture *tex,
1638 unsigned transfer_usage,
1639 const struct pipe_box *box)
1640 {
1641 return !tex->buffer.b.is_shared &&
1642 !(transfer_usage & PIPE_TRANSFER_READ) &&
1643 tex->buffer.b.b.last_level == 0 &&
1644 util_texrange_covers_whole_level(&tex->buffer.b.b, 0,
1645 box->x, box->y, box->z,
1646 box->width, box->height,
1647 box->depth);
1648 }
1649
1650 static void si_texture_invalidate_storage(struct si_context *sctx,
1651 struct si_texture *tex)
1652 {
1653 struct si_screen *sscreen = sctx->screen;
1654
1655 /* There is no point in discarding depth and tiled buffers. */
1656 assert(!tex->is_depth);
1657 assert(tex->surface.is_linear);
1658
1659 /* Reallocate the buffer in the same pipe_resource. */
1660 si_alloc_resource(sscreen, &tex->buffer);
1661
1662 /* Initialize the CMASK base address (needed even without CMASK). */
1663 tex->cmask_base_address_reg =
1664 (tex->buffer.gpu_address + tex->cmask_offset) >> 8;
1665
1666 p_atomic_inc(&sscreen->dirty_tex_counter);
1667
1668 sctx->num_alloc_tex_transfer_bytes += tex->size;
1669 }
1670
1671 static void *si_texture_transfer_map(struct pipe_context *ctx,
1672 struct pipe_resource *texture,
1673 unsigned level,
1674 unsigned usage,
1675 const struct pipe_box *box,
1676 struct pipe_transfer **ptransfer)
1677 {
1678 struct si_context *sctx = (struct si_context*)ctx;
1679 struct si_texture *tex = (struct si_texture*)texture;
1680 struct si_transfer *trans;
1681 struct si_resource *buf;
1682 unsigned offset = 0;
1683 char *map;
1684 bool use_staging_texture = false;
1685
1686 assert(!(texture->flags & SI_RESOURCE_FLAG_TRANSFER));
1687 assert(box->width && box->height && box->depth);
1688
1689 /* Depth textures use staging unconditionally. */
1690 if (!tex->is_depth) {
1691 /* Degrade the tile mode if we get too many transfers on APUs.
1692 * On dGPUs, the staging texture is always faster.
1693 * Only count uploads that are at least 4x4 pixels large.
1694 */
1695 if (!sctx->screen->info.has_dedicated_vram &&
1696 level == 0 &&
1697 box->width >= 4 && box->height >= 4 &&
1698 p_atomic_inc_return(&tex->num_level0_transfers) == 10) {
1699 bool can_invalidate =
1700 si_can_invalidate_texture(sctx->screen, tex,
1701 usage, box);
1702
1703 si_reallocate_texture_inplace(sctx, tex,
1704 PIPE_BIND_LINEAR,
1705 can_invalidate);
1706 }
1707
1708 /* Tiled textures need to be converted into a linear texture for CPU
1709 * access. The staging texture is always linear and is placed in GART.
1710 *
1711 * Reading from VRAM or GTT WC is slow, always use the staging
1712 * texture in this case.
1713 *
1714 * Use the staging texture for uploads if the underlying BO
1715 * is busy.
1716 */
1717 if (!tex->surface.is_linear)
1718 use_staging_texture = true;
1719 else if (usage & PIPE_TRANSFER_READ)
1720 use_staging_texture =
1721 tex->buffer.domains & RADEON_DOMAIN_VRAM ||
1722 tex->buffer.flags & RADEON_FLAG_GTT_WC;
1723 /* Write & linear only: */
1724 else if (si_rings_is_buffer_referenced(sctx, tex->buffer.buf,
1725 RADEON_USAGE_READWRITE) ||
1726 !sctx->ws->buffer_wait(tex->buffer.buf, 0,
1727 RADEON_USAGE_READWRITE)) {
1728 /* It's busy. */
1729 if (si_can_invalidate_texture(sctx->screen, tex,
1730 usage, box))
1731 si_texture_invalidate_storage(sctx, tex);
1732 else
1733 use_staging_texture = true;
1734 }
1735 }
1736
1737 trans = CALLOC_STRUCT(si_transfer);
1738 if (!trans)
1739 return NULL;
1740 pipe_resource_reference(&trans->b.b.resource, texture);
1741 trans->b.b.level = level;
1742 trans->b.b.usage = usage;
1743 trans->b.b.box = *box;
1744
1745 if (tex->is_depth) {
1746 struct si_texture *staging_depth;
1747
1748 if (tex->buffer.b.b.nr_samples > 1) {
1749 /* MSAA depth buffers need to be converted to single sample buffers.
1750 *
1751 * Mapping MSAA depth buffers can occur if ReadPixels is called
1752 * with a multisample GLX visual.
1753 *
1754 * First downsample the depth buffer to a temporary texture,
1755 * then decompress the temporary one to staging.
1756 *
1757 * Only the region being mapped is transfered.
1758 */
1759 struct pipe_resource resource;
1760
1761 si_init_temp_resource_from_box(&resource, texture, box, level, 0);
1762
1763 if (!si_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1764 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1765 goto fail_trans;
1766 }
1767
1768 if (usage & PIPE_TRANSFER_READ) {
1769 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1770 if (!temp) {
1771 PRINT_ERR("failed to create a temporary depth texture\n");
1772 goto fail_trans;
1773 }
1774
1775 si_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1776 si_blit_decompress_depth(ctx, (struct si_texture*)temp, staging_depth,
1777 0, 0, 0, box->depth, 0, 0);
1778 pipe_resource_reference(&temp, NULL);
1779 }
1780
1781 /* Just get the strides. */
1782 si_texture_get_offset(sctx->screen, staging_depth, level, NULL,
1783 &trans->b.b.stride,
1784 &trans->b.b.layer_stride);
1785 } else {
1786 /* XXX: only readback the rectangle which is being mapped? */
1787 /* XXX: when discard is true, no need to read back from depth texture */
1788 if (!si_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1789 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1790 goto fail_trans;
1791 }
1792
1793 si_blit_decompress_depth(ctx, tex, staging_depth,
1794 level, level,
1795 box->z, box->z + box->depth - 1,
1796 0, 0);
1797
1798 offset = si_texture_get_offset(sctx->screen, staging_depth,
1799 level, box,
1800 &trans->b.b.stride,
1801 &trans->b.b.layer_stride);
1802 }
1803
1804 trans->staging = &staging_depth->buffer;
1805 buf = trans->staging;
1806 } else if (use_staging_texture) {
1807 struct pipe_resource resource;
1808 struct si_texture *staging;
1809
1810 si_init_temp_resource_from_box(&resource, texture, box, level,
1811 SI_RESOURCE_FLAG_TRANSFER);
1812 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1813 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1814
1815 /* Create the temporary texture. */
1816 staging = (struct si_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1817 if (!staging) {
1818 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1819 goto fail_trans;
1820 }
1821 trans->staging = &staging->buffer;
1822
1823 /* Just get the strides. */
1824 si_texture_get_offset(sctx->screen, staging, 0, NULL,
1825 &trans->b.b.stride,
1826 &trans->b.b.layer_stride);
1827
1828 if (usage & PIPE_TRANSFER_READ)
1829 si_copy_to_staging_texture(ctx, trans);
1830 else
1831 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1832
1833 buf = trans->staging;
1834 } else {
1835 /* the resource is mapped directly */
1836 offset = si_texture_get_offset(sctx->screen, tex, level, box,
1837 &trans->b.b.stride,
1838 &trans->b.b.layer_stride);
1839 buf = &tex->buffer;
1840 }
1841
1842 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1843 * we don't run out of the CPU address space.
1844 */
1845 if (sizeof(void*) == 4)
1846 usage |= RADEON_TRANSFER_TEMPORARY;
1847
1848 if (!(map = si_buffer_map_sync_with_rings(sctx, buf, usage)))
1849 goto fail_trans;
1850
1851 *ptransfer = &trans->b.b;
1852 return map + offset;
1853
1854 fail_trans:
1855 si_resource_reference(&trans->staging, NULL);
1856 pipe_resource_reference(&trans->b.b.resource, NULL);
1857 FREE(trans);
1858 return NULL;
1859 }
1860
1861 static void si_texture_transfer_unmap(struct pipe_context *ctx,
1862 struct pipe_transfer* transfer)
1863 {
1864 struct si_context *sctx = (struct si_context*)ctx;
1865 struct si_transfer *stransfer = (struct si_transfer*)transfer;
1866 struct pipe_resource *texture = transfer->resource;
1867 struct si_texture *tex = (struct si_texture*)texture;
1868
1869 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1870 * we don't run out of the CPU address space.
1871 */
1872 if (sizeof(void*) == 4) {
1873 struct si_resource *buf =
1874 stransfer->staging ? stransfer->staging : &tex->buffer;
1875
1876 sctx->ws->buffer_unmap(buf->buf);
1877 }
1878
1879 if ((transfer->usage & PIPE_TRANSFER_WRITE) && stransfer->staging) {
1880 if (tex->is_depth && tex->buffer.b.b.nr_samples <= 1) {
1881 ctx->resource_copy_region(ctx, texture, transfer->level,
1882 transfer->box.x, transfer->box.y, transfer->box.z,
1883 &stransfer->staging->b.b, transfer->level,
1884 &transfer->box);
1885 } else {
1886 si_copy_from_staging_texture(ctx, stransfer);
1887 }
1888 }
1889
1890 if (stransfer->staging) {
1891 sctx->num_alloc_tex_transfer_bytes += stransfer->staging->buf->size;
1892 si_resource_reference(&stransfer->staging, NULL);
1893 }
1894
1895 /* Heuristic for {upload, draw, upload, draw, ..}:
1896 *
1897 * Flush the gfx IB if we've allocated too much texture storage.
1898 *
1899 * The idea is that we don't want to build IBs that use too much
1900 * memory and put pressure on the kernel memory manager and we also
1901 * want to make temporary and invalidated buffers go idle ASAP to
1902 * decrease the total memory usage or make them reusable. The memory
1903 * usage will be slightly higher than given here because of the buffer
1904 * cache in the winsys.
1905 *
1906 * The result is that the kernel memory manager is never a bottleneck.
1907 */
1908 if (sctx->num_alloc_tex_transfer_bytes > sctx->screen->info.gart_size / 4) {
1909 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1910 sctx->num_alloc_tex_transfer_bytes = 0;
1911 }
1912
1913 pipe_resource_reference(&transfer->resource, NULL);
1914 FREE(transfer);
1915 }
1916
1917 static const struct u_resource_vtbl si_texture_vtbl =
1918 {
1919 NULL, /* get_handle */
1920 si_texture_destroy, /* resource_destroy */
1921 si_texture_transfer_map, /* transfer_map */
1922 u_default_transfer_flush_region, /* transfer_flush_region */
1923 si_texture_transfer_unmap, /* transfer_unmap */
1924 };
1925
1926 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
1927 */
1928 bool vi_dcc_formats_compatible(enum pipe_format format1,
1929 enum pipe_format format2)
1930 {
1931 const struct util_format_description *desc1, *desc2;
1932
1933 /* No format change - exit early. */
1934 if (format1 == format2)
1935 return true;
1936
1937 format1 = si_simplify_cb_format(format1);
1938 format2 = si_simplify_cb_format(format2);
1939
1940 /* Check again after format adjustments. */
1941 if (format1 == format2)
1942 return true;
1943
1944 desc1 = util_format_description(format1);
1945 desc2 = util_format_description(format2);
1946
1947 if (desc1->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
1948 desc2->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1949 return false;
1950
1951 /* Float and non-float are totally incompatible. */
1952 if ((desc1->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) !=
1953 (desc2->channel[0].type == UTIL_FORMAT_TYPE_FLOAT))
1954 return false;
1955
1956 /* Channel sizes must match across DCC formats.
1957 * Comparing just the first 2 channels should be enough.
1958 */
1959 if (desc1->channel[0].size != desc2->channel[0].size ||
1960 (desc1->nr_channels >= 2 &&
1961 desc1->channel[1].size != desc2->channel[1].size))
1962 return false;
1963
1964 /* Everything below is not needed if the driver never uses the DCC
1965 * clear code with the value of 1.
1966 */
1967
1968 /* If the clear values are all 1 or all 0, this constraint can be
1969 * ignored. */
1970 if (vi_alpha_is_on_msb(format1) != vi_alpha_is_on_msb(format2))
1971 return false;
1972
1973 /* Channel types must match if the clear value of 1 is used.
1974 * The type categories are only float, signed, unsigned.
1975 * NORM and INT are always compatible.
1976 */
1977 if (desc1->channel[0].type != desc2->channel[0].type ||
1978 (desc1->nr_channels >= 2 &&
1979 desc1->channel[1].type != desc2->channel[1].type))
1980 return false;
1981
1982 return true;
1983 }
1984
1985 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex,
1986 unsigned level,
1987 enum pipe_format view_format)
1988 {
1989 struct si_texture *stex = (struct si_texture *)tex;
1990
1991 return vi_dcc_enabled(stex, level) &&
1992 !vi_dcc_formats_compatible(tex->format, view_format);
1993 }
1994
1995 /* This can't be merged with the above function, because
1996 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1997 void vi_disable_dcc_if_incompatible_format(struct si_context *sctx,
1998 struct pipe_resource *tex,
1999 unsigned level,
2000 enum pipe_format view_format)
2001 {
2002 struct si_texture *stex = (struct si_texture *)tex;
2003
2004 if (vi_dcc_formats_are_incompatible(tex, level, view_format))
2005 if (!si_texture_disable_dcc(sctx, stex))
2006 si_decompress_dcc(sctx, stex);
2007 }
2008
2009 struct pipe_surface *si_create_surface_custom(struct pipe_context *pipe,
2010 struct pipe_resource *texture,
2011 const struct pipe_surface *templ,
2012 unsigned width0, unsigned height0,
2013 unsigned width, unsigned height)
2014 {
2015 struct si_surface *surface = CALLOC_STRUCT(si_surface);
2016
2017 if (!surface)
2018 return NULL;
2019
2020 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
2021 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
2022
2023 pipe_reference_init(&surface->base.reference, 1);
2024 pipe_resource_reference(&surface->base.texture, texture);
2025 surface->base.context = pipe;
2026 surface->base.format = templ->format;
2027 surface->base.width = width;
2028 surface->base.height = height;
2029 surface->base.u = templ->u;
2030
2031 surface->width0 = width0;
2032 surface->height0 = height0;
2033
2034 surface->dcc_incompatible =
2035 texture->target != PIPE_BUFFER &&
2036 vi_dcc_formats_are_incompatible(texture, templ->u.tex.level,
2037 templ->format);
2038 return &surface->base;
2039 }
2040
2041 static struct pipe_surface *si_create_surface(struct pipe_context *pipe,
2042 struct pipe_resource *tex,
2043 const struct pipe_surface *templ)
2044 {
2045 unsigned level = templ->u.tex.level;
2046 unsigned width = u_minify(tex->width0, level);
2047 unsigned height = u_minify(tex->height0, level);
2048 unsigned width0 = tex->width0;
2049 unsigned height0 = tex->height0;
2050
2051 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
2052 const struct util_format_description *tex_desc
2053 = util_format_description(tex->format);
2054 const struct util_format_description *templ_desc
2055 = util_format_description(templ->format);
2056
2057 assert(tex_desc->block.bits == templ_desc->block.bits);
2058
2059 /* Adjust size of surface if and only if the block width or
2060 * height is changed. */
2061 if (tex_desc->block.width != templ_desc->block.width ||
2062 tex_desc->block.height != templ_desc->block.height) {
2063 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
2064 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
2065
2066 width = nblks_x * templ_desc->block.width;
2067 height = nblks_y * templ_desc->block.height;
2068
2069 width0 = util_format_get_nblocksx(tex->format, width0);
2070 height0 = util_format_get_nblocksy(tex->format, height0);
2071 }
2072 }
2073
2074 return si_create_surface_custom(pipe, tex, templ,
2075 width0, height0,
2076 width, height);
2077 }
2078
2079 static void si_surface_destroy(struct pipe_context *pipe,
2080 struct pipe_surface *surface)
2081 {
2082 pipe_resource_reference(&surface->texture, NULL);
2083 FREE(surface);
2084 }
2085
2086 unsigned si_translate_colorswap(enum pipe_format format, bool do_endian_swap)
2087 {
2088 const struct util_format_description *desc = util_format_description(format);
2089
2090 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2091
2092 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2093 return V_028C70_SWAP_STD;
2094
2095 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
2096 return ~0U;
2097
2098 switch (desc->nr_channels) {
2099 case 1:
2100 if (HAS_SWIZZLE(0,X))
2101 return V_028C70_SWAP_STD; /* X___ */
2102 else if (HAS_SWIZZLE(3,X))
2103 return V_028C70_SWAP_ALT_REV; /* ___X */
2104 break;
2105 case 2:
2106 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
2107 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
2108 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
2109 return V_028C70_SWAP_STD; /* XY__ */
2110 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
2111 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
2112 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
2113 /* YX__ */
2114 return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
2115 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
2116 return V_028C70_SWAP_ALT; /* X__Y */
2117 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
2118 return V_028C70_SWAP_ALT_REV; /* Y__X */
2119 break;
2120 case 3:
2121 if (HAS_SWIZZLE(0,X))
2122 return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
2123 else if (HAS_SWIZZLE(0,Z))
2124 return V_028C70_SWAP_STD_REV; /* ZYX */
2125 break;
2126 case 4:
2127 /* check the middle channels, the 1st and 4th channel can be NONE */
2128 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
2129 return V_028C70_SWAP_STD; /* XYZW */
2130 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
2131 return V_028C70_SWAP_STD_REV; /* WZYX */
2132 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
2133 return V_028C70_SWAP_ALT; /* ZYXW */
2134 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
2135 /* YZWX */
2136 if (desc->is_array)
2137 return V_028C70_SWAP_ALT_REV;
2138 else
2139 return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
2140 }
2141 break;
2142 }
2143 return ~0U;
2144 }
2145
2146 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2147
2148 static void vi_dcc_clean_up_context_slot(struct si_context *sctx,
2149 int slot)
2150 {
2151 int i;
2152
2153 if (sctx->dcc_stats[slot].query_active)
2154 vi_separate_dcc_stop_query(sctx,
2155 sctx->dcc_stats[slot].tex);
2156
2157 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats[slot].ps_stats); i++)
2158 if (sctx->dcc_stats[slot].ps_stats[i]) {
2159 sctx->b.destroy_query(&sctx->b,
2160 sctx->dcc_stats[slot].ps_stats[i]);
2161 sctx->dcc_stats[slot].ps_stats[i] = NULL;
2162 }
2163
2164 si_texture_reference(&sctx->dcc_stats[slot].tex, NULL);
2165 }
2166
2167 /**
2168 * Return the per-context slot where DCC statistics queries for the texture live.
2169 */
2170 static unsigned vi_get_context_dcc_stats_index(struct si_context *sctx,
2171 struct si_texture *tex)
2172 {
2173 int i, empty_slot = -1;
2174
2175 /* Remove zombie textures (textures kept alive by this array only). */
2176 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2177 if (sctx->dcc_stats[i].tex &&
2178 sctx->dcc_stats[i].tex->buffer.b.b.reference.count == 1)
2179 vi_dcc_clean_up_context_slot(sctx, i);
2180
2181 /* Find the texture. */
2182 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++) {
2183 /* Return if found. */
2184 if (sctx->dcc_stats[i].tex == tex) {
2185 sctx->dcc_stats[i].last_use_timestamp = os_time_get();
2186 return i;
2187 }
2188
2189 /* Record the first seen empty slot. */
2190 if (empty_slot == -1 && !sctx->dcc_stats[i].tex)
2191 empty_slot = i;
2192 }
2193
2194 /* Not found. Remove the oldest member to make space in the array. */
2195 if (empty_slot == -1) {
2196 int oldest_slot = 0;
2197
2198 /* Find the oldest slot. */
2199 for (i = 1; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2200 if (sctx->dcc_stats[oldest_slot].last_use_timestamp >
2201 sctx->dcc_stats[i].last_use_timestamp)
2202 oldest_slot = i;
2203
2204 /* Clean up the oldest slot. */
2205 vi_dcc_clean_up_context_slot(sctx, oldest_slot);
2206 empty_slot = oldest_slot;
2207 }
2208
2209 /* Add the texture to the new slot. */
2210 si_texture_reference(&sctx->dcc_stats[empty_slot].tex, tex);
2211 sctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2212 return empty_slot;
2213 }
2214
2215 static struct pipe_query *
2216 vi_create_resuming_pipestats_query(struct si_context *sctx)
2217 {
2218 struct si_query_hw *query = (struct si_query_hw*)
2219 sctx->b.create_query(&sctx->b, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2220
2221 query->flags |= SI_QUERY_HW_FLAG_BEGIN_RESUMES;
2222 return (struct pipe_query*)query;
2223 }
2224
2225 /**
2226 * Called when binding a color buffer.
2227 */
2228 void vi_separate_dcc_start_query(struct si_context *sctx,
2229 struct si_texture *tex)
2230 {
2231 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2232
2233 assert(!sctx->dcc_stats[i].query_active);
2234
2235 if (!sctx->dcc_stats[i].ps_stats[0])
2236 sctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(sctx);
2237
2238 /* begin or resume the query */
2239 sctx->b.begin_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2240 sctx->dcc_stats[i].query_active = true;
2241 }
2242
2243 /**
2244 * Called when unbinding a color buffer.
2245 */
2246 void vi_separate_dcc_stop_query(struct si_context *sctx,
2247 struct si_texture *tex)
2248 {
2249 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2250
2251 assert(sctx->dcc_stats[i].query_active);
2252 assert(sctx->dcc_stats[i].ps_stats[0]);
2253
2254 /* pause or end the query */
2255 sctx->b.end_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2256 sctx->dcc_stats[i].query_active = false;
2257 }
2258
2259 static bool vi_should_enable_separate_dcc(struct si_texture *tex)
2260 {
2261 /* The minimum number of fullscreen draws per frame that is required
2262 * to enable DCC. */
2263 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2264 }
2265
2266 /* Called by fast clear. */
2267 void vi_separate_dcc_try_enable(struct si_context *sctx,
2268 struct si_texture *tex)
2269 {
2270 /* The intent is to use this with shared displayable back buffers,
2271 * but it's not strictly limited only to them.
2272 */
2273 if (!tex->buffer.b.is_shared ||
2274 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2275 tex->buffer.b.b.target != PIPE_TEXTURE_2D ||
2276 tex->buffer.b.b.last_level > 0 ||
2277 !tex->surface.dcc_size ||
2278 sctx->screen->debug_flags & DBG(NO_DCC) ||
2279 sctx->screen->debug_flags & DBG(NO_DCC_FB))
2280 return;
2281
2282 assert(sctx->chip_class >= VI);
2283
2284 if (tex->dcc_offset)
2285 return; /* already enabled */
2286
2287 /* Enable the DCC stat gathering. */
2288 if (!tex->dcc_gather_statistics) {
2289 tex->dcc_gather_statistics = true;
2290 vi_separate_dcc_start_query(sctx, tex);
2291 }
2292
2293 if (!vi_should_enable_separate_dcc(tex))
2294 return; /* stats show that DCC decompression is too expensive */
2295
2296 assert(tex->surface.num_dcc_levels);
2297 assert(!tex->dcc_separate_buffer);
2298
2299 si_texture_discard_cmask(sctx->screen, tex);
2300
2301 /* Get a DCC buffer. */
2302 if (tex->last_dcc_separate_buffer) {
2303 assert(tex->dcc_gather_statistics);
2304 assert(!tex->dcc_separate_buffer);
2305 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2306 tex->last_dcc_separate_buffer = NULL;
2307 } else {
2308 tex->dcc_separate_buffer =
2309 si_aligned_buffer_create(sctx->b.screen,
2310 SI_RESOURCE_FLAG_UNMAPPABLE,
2311 PIPE_USAGE_DEFAULT,
2312 tex->surface.dcc_size,
2313 tex->surface.dcc_alignment);
2314 if (!tex->dcc_separate_buffer)
2315 return;
2316 }
2317
2318 /* dcc_offset is the absolute GPUVM address. */
2319 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
2320
2321 /* no need to flag anything since this is called by fast clear that
2322 * flags framebuffer state
2323 */
2324 }
2325
2326 /**
2327 * Called by pipe_context::flush_resource, the place where DCC decompression
2328 * takes place.
2329 */
2330 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
2331 struct si_texture *tex)
2332 {
2333 struct si_context *sctx = (struct si_context*)ctx;
2334 struct pipe_query *tmp;
2335 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2336 bool query_active = sctx->dcc_stats[i].query_active;
2337 bool disable = false;
2338
2339 if (sctx->dcc_stats[i].ps_stats[2]) {
2340 union pipe_query_result result;
2341
2342 /* Read the results. */
2343 struct pipe_query *query = sctx->dcc_stats[i].ps_stats[2];
2344 ctx->get_query_result(ctx, query,
2345 true, &result);
2346 si_query_buffer_reset(sctx, &((struct si_query_hw*)query)->buffer);
2347
2348 /* Compute the approximate number of fullscreen draws. */
2349 tex->ps_draw_ratio =
2350 result.pipeline_statistics.ps_invocations /
2351 (tex->buffer.b.b.width0 * tex->buffer.b.b.height0);
2352 sctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2353
2354 disable = tex->dcc_separate_buffer &&
2355 !vi_should_enable_separate_dcc(tex);
2356 }
2357
2358 tex->num_slow_clears = 0;
2359
2360 /* stop the statistics query for ps_stats[0] */
2361 if (query_active)
2362 vi_separate_dcc_stop_query(sctx, tex);
2363
2364 /* Move the queries in the queue by one. */
2365 tmp = sctx->dcc_stats[i].ps_stats[2];
2366 sctx->dcc_stats[i].ps_stats[2] = sctx->dcc_stats[i].ps_stats[1];
2367 sctx->dcc_stats[i].ps_stats[1] = sctx->dcc_stats[i].ps_stats[0];
2368 sctx->dcc_stats[i].ps_stats[0] = tmp;
2369
2370 /* create and start a new query as ps_stats[0] */
2371 if (query_active)
2372 vi_separate_dcc_start_query(sctx, tex);
2373
2374 if (disable) {
2375 assert(!tex->last_dcc_separate_buffer);
2376 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2377 tex->dcc_separate_buffer = NULL;
2378 tex->dcc_offset = 0;
2379 /* no need to flag anything since this is called after
2380 * decompression that re-sets framebuffer state
2381 */
2382 }
2383 }
2384
2385 static struct pipe_memory_object *
2386 si_memobj_from_handle(struct pipe_screen *screen,
2387 struct winsys_handle *whandle,
2388 bool dedicated)
2389 {
2390 struct si_screen *sscreen = (struct si_screen*)screen;
2391 struct si_memory_object *memobj = CALLOC_STRUCT(si_memory_object);
2392 struct pb_buffer *buf = NULL;
2393 uint32_t stride, offset;
2394
2395 if (!memobj)
2396 return NULL;
2397
2398 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle,
2399 sscreen->info.max_alignment,
2400 &stride, &offset);
2401 if (!buf) {
2402 free(memobj);
2403 return NULL;
2404 }
2405
2406 memobj->b.dedicated = dedicated;
2407 memobj->buf = buf;
2408 memobj->stride = stride;
2409
2410 return (struct pipe_memory_object *)memobj;
2411
2412 }
2413
2414 static void
2415 si_memobj_destroy(struct pipe_screen *screen,
2416 struct pipe_memory_object *_memobj)
2417 {
2418 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2419
2420 pb_reference(&memobj->buf, NULL);
2421 free(memobj);
2422 }
2423
2424 static struct pipe_resource *
2425 si_texture_from_memobj(struct pipe_screen *screen,
2426 const struct pipe_resource *templ,
2427 struct pipe_memory_object *_memobj,
2428 uint64_t offset)
2429 {
2430 struct si_screen *sscreen = (struct si_screen*)screen;
2431 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2432 struct pipe_resource *tex =
2433 si_texture_from_winsys_buffer(sscreen, templ, memobj->buf,
2434 memobj->stride, offset,
2435 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE |
2436 PIPE_HANDLE_USAGE_SHADER_WRITE,
2437 memobj->b.dedicated);
2438 if (!tex)
2439 return NULL;
2440
2441 /* si_texture_from_winsys_buffer doesn't increment refcount of
2442 * memobj->buf, so increment it here.
2443 */
2444 struct pb_buffer *buf = NULL;
2445 pb_reference(&buf, memobj->buf);
2446 return tex;
2447 }
2448
2449 static bool si_check_resource_capability(struct pipe_screen *screen,
2450 struct pipe_resource *resource,
2451 unsigned bind)
2452 {
2453 struct si_texture *tex = (struct si_texture*)resource;
2454
2455 /* Buffers only support the linear flag. */
2456 if (resource->target == PIPE_BUFFER)
2457 return (bind & ~PIPE_BIND_LINEAR) == 0;
2458
2459 if (bind & PIPE_BIND_LINEAR && !tex->surface.is_linear)
2460 return false;
2461
2462 if (bind & PIPE_BIND_SCANOUT && !tex->surface.is_displayable)
2463 return false;
2464
2465 /* TODO: PIPE_BIND_CURSOR - do we care? */
2466 return true;
2467 }
2468
2469 void si_init_screen_texture_functions(struct si_screen *sscreen)
2470 {
2471 sscreen->b.resource_from_handle = si_texture_from_handle;
2472 sscreen->b.resource_get_handle = si_texture_get_handle;
2473 sscreen->b.resource_from_memobj = si_texture_from_memobj;
2474 sscreen->b.memobj_create_from_handle = si_memobj_from_handle;
2475 sscreen->b.memobj_destroy = si_memobj_destroy;
2476 sscreen->b.check_resource_capability = si_check_resource_capability;
2477 }
2478
2479 void si_init_context_texture_functions(struct si_context *sctx)
2480 {
2481 sctx->b.create_surface = si_create_surface;
2482 sctx->b.surface_destroy = si_surface_destroy;
2483 }