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