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