e21dc37af892884184f86a2bb4452c0be3081b8a
[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 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include "r600_pipe_common.h"
28 #include "r600_cs.h"
29 #include "r600_query.h"
30 #include "util/u_format.h"
31 #include "util/u_memory.h"
32 #include "util/u_pack_color.h"
33 #include "util/u_surface.h"
34 #include "os/os_time.h"
35 #include <errno.h>
36 #include <inttypes.h>
37
38 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
39 struct r600_texture *rtex);
40 static enum radeon_surf_mode
41 r600_choose_tiling(struct r600_common_screen *rscreen,
42 const struct pipe_resource *templ);
43
44
45 bool r600_prepare_for_dma_blit(struct r600_common_context *rctx,
46 struct r600_texture *rdst,
47 unsigned dst_level, unsigned dstx,
48 unsigned dsty, unsigned dstz,
49 struct r600_texture *rsrc,
50 unsigned src_level,
51 const struct pipe_box *src_box)
52 {
53 if (!rctx->dma.cs)
54 return false;
55
56 if (rdst->surface.bpe != rsrc->surface.bpe)
57 return false;
58
59 /* MSAA: Blits don't exist in the real world. */
60 if (rsrc->resource.b.b.nr_samples > 1 ||
61 rdst->resource.b.b.nr_samples > 1)
62 return false;
63
64 /* Depth-stencil surfaces:
65 * When dst is linear, the DB->CB copy preserves HTILE.
66 * When dst is tiled, the 3D path must be used to update HTILE.
67 */
68 if (rsrc->is_depth || rdst->is_depth)
69 return false;
70
71 /* DCC as:
72 * src: Use the 3D path. DCC decompression is expensive.
73 * dst: Use the 3D path to compress the pixels with DCC.
74 */
75 if (vi_dcc_enabled(rsrc, src_level) ||
76 vi_dcc_enabled(rdst, dst_level))
77 return false;
78
79 /* CMASK as:
80 * src: Both texture and SDMA paths need decompression. Use SDMA.
81 * dst: If overwriting the whole texture, discard CMASK and use
82 * SDMA. Otherwise, use the 3D path.
83 */
84 if (rdst->cmask.size && rdst->dirty_level_mask & (1 << dst_level)) {
85 /* The CMASK clear is only enabled for the first level. */
86 assert(dst_level == 0);
87 if (!util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
88 dstx, dsty, dstz, src_box->width,
89 src_box->height, src_box->depth))
90 return false;
91
92 r600_texture_discard_cmask(rctx->screen, rdst);
93 }
94
95 /* All requirements are met. Prepare textures for SDMA. */
96 if (rsrc->cmask.size && rsrc->dirty_level_mask & (1 << src_level))
97 rctx->b.flush_resource(&rctx->b, &rsrc->resource.b.b);
98
99 assert(!(rsrc->dirty_level_mask & (1 << src_level)));
100 assert(!(rdst->dirty_level_mask & (1 << dst_level)));
101
102 return true;
103 }
104
105 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
106 static void r600_copy_region_with_blit(struct pipe_context *pipe,
107 struct pipe_resource *dst,
108 unsigned dst_level,
109 unsigned dstx, unsigned dsty, unsigned dstz,
110 struct pipe_resource *src,
111 unsigned src_level,
112 const struct pipe_box *src_box)
113 {
114 struct pipe_blit_info blit;
115
116 memset(&blit, 0, sizeof(blit));
117 blit.src.resource = src;
118 blit.src.format = src->format;
119 blit.src.level = src_level;
120 blit.src.box = *src_box;
121 blit.dst.resource = dst;
122 blit.dst.format = dst->format;
123 blit.dst.level = dst_level;
124 blit.dst.box.x = dstx;
125 blit.dst.box.y = dsty;
126 blit.dst.box.z = dstz;
127 blit.dst.box.width = src_box->width;
128 blit.dst.box.height = src_box->height;
129 blit.dst.box.depth = src_box->depth;
130 blit.mask = util_format_get_mask(src->format) &
131 util_format_get_mask(dst->format);
132 blit.filter = PIPE_TEX_FILTER_NEAREST;
133
134 if (blit.mask) {
135 pipe->blit(pipe, &blit);
136 }
137 }
138
139 /* Copy from a full GPU texture to a transfer's staging one. */
140 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
141 {
142 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
143 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
144 struct pipe_resource *dst = &rtransfer->staging->b.b;
145 struct pipe_resource *src = transfer->resource;
146
147 if (src->nr_samples > 1) {
148 r600_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
149 src, transfer->level, &transfer->box);
150 return;
151 }
152
153 rctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
154 &transfer->box);
155 }
156
157 /* Copy from a transfer's staging texture to a full GPU one. */
158 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
159 {
160 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
161 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
162 struct pipe_resource *dst = transfer->resource;
163 struct pipe_resource *src = &rtransfer->staging->b.b;
164 struct pipe_box sbox;
165
166 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
167
168 if (dst->nr_samples > 1) {
169 r600_copy_region_with_blit(ctx, dst, transfer->level,
170 transfer->box.x, transfer->box.y, transfer->box.z,
171 src, 0, &sbox);
172 return;
173 }
174
175 rctx->dma_copy(ctx, dst, transfer->level,
176 transfer->box.x, transfer->box.y, transfer->box.z,
177 src, 0, &sbox);
178 }
179
180 static unsigned r600_texture_get_offset(struct r600_common_screen *rscreen,
181 struct r600_texture *rtex, unsigned level,
182 const struct pipe_box *box,
183 unsigned *stride,
184 unsigned *layer_stride)
185 {
186 if (rscreen->chip_class >= GFX9) {
187 *stride = rtex->surface.u.gfx9.surf_pitch * rtex->surface.bpe;
188 *layer_stride = rtex->surface.u.gfx9.surf_slice_size;
189
190 if (!box)
191 return 0;
192
193 /* Each texture is an array of slices. Each slice is an array
194 * of mipmap levels. */
195 return box->z * rtex->surface.u.gfx9.surf_slice_size +
196 rtex->surface.u.gfx9.offset[level] +
197 (box->y / rtex->surface.blk_h *
198 rtex->surface.u.gfx9.surf_pitch +
199 box->x / rtex->surface.blk_w) * rtex->surface.bpe;
200 } else {
201 *stride = rtex->surface.u.legacy.level[level].nblk_x *
202 rtex->surface.bpe;
203 *layer_stride = rtex->surface.u.legacy.level[level].slice_size;
204
205 if (!box)
206 return rtex->surface.u.legacy.level[level].offset;
207
208 /* Each texture is an array of mipmap levels. Each level is
209 * an array of slices. */
210 return rtex->surface.u.legacy.level[level].offset +
211 box->z * rtex->surface.u.legacy.level[level].slice_size +
212 (box->y / rtex->surface.blk_h *
213 rtex->surface.u.legacy.level[level].nblk_x +
214 box->x / rtex->surface.blk_w) * rtex->surface.bpe;
215 }
216 }
217
218 static int r600_init_surface(struct r600_common_screen *rscreen,
219 struct radeon_surf *surface,
220 const struct pipe_resource *ptex,
221 enum radeon_surf_mode array_mode,
222 unsigned pitch_in_bytes_override,
223 unsigned offset,
224 bool is_imported,
225 bool is_scanout,
226 bool is_flushed_depth,
227 bool tc_compatible_htile)
228 {
229 const struct util_format_description *desc =
230 util_format_description(ptex->format);
231 bool is_depth, is_stencil;
232 int r;
233 unsigned i, bpe, flags = 0;
234
235 is_depth = util_format_has_depth(desc);
236 is_stencil = util_format_has_stencil(desc);
237
238 if (rscreen->chip_class >= EVERGREEN && !is_flushed_depth &&
239 ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
240 bpe = 4; /* stencil is allocated separately on evergreen */
241 } else {
242 bpe = util_format_get_blocksize(ptex->format);
243 assert(util_is_power_of_two(bpe));
244 }
245
246 if (!is_flushed_depth && is_depth) {
247 flags |= RADEON_SURF_ZBUFFER;
248
249 if (tc_compatible_htile &&
250 (rscreen->chip_class >= GFX9 ||
251 array_mode == RADEON_SURF_MODE_2D)) {
252 /* TC-compatible HTILE only supports Z32_FLOAT.
253 * GFX9 also supports Z16_UNORM.
254 * On VI, promote Z16 to Z32. DB->CB copies will convert
255 * the format for transfers.
256 */
257 if (rscreen->chip_class == VI)
258 bpe = 4;
259
260 flags |= RADEON_SURF_TC_COMPATIBLE_HTILE;
261 }
262
263 if (is_stencil)
264 flags |= RADEON_SURF_SBUFFER;
265 }
266
267 if (rscreen->chip_class >= VI &&
268 (ptex->flags & R600_RESOURCE_FLAG_DISABLE_DCC ||
269 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT))
270 flags |= RADEON_SURF_DISABLE_DCC;
271
272 if (ptex->bind & PIPE_BIND_SCANOUT || is_scanout) {
273 /* This should catch bugs in gallium users setting incorrect flags. */
274 assert(ptex->nr_samples <= 1 &&
275 ptex->array_size == 1 &&
276 ptex->depth0 == 1 &&
277 ptex->last_level == 0 &&
278 !(flags & RADEON_SURF_Z_OR_SBUFFER));
279
280 flags |= RADEON_SURF_SCANOUT;
281 }
282
283 if (is_imported)
284 flags |= RADEON_SURF_IMPORTED;
285 if (!(ptex->flags & R600_RESOURCE_FLAG_FORCE_TILING))
286 flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
287
288 r = rscreen->ws->surface_init(rscreen->ws, ptex, flags, bpe,
289 array_mode, surface);
290 if (r) {
291 return r;
292 }
293
294 if (rscreen->chip_class >= GFX9) {
295 assert(!pitch_in_bytes_override ||
296 pitch_in_bytes_override == surface->u.gfx9.surf_pitch * bpe);
297 surface->u.gfx9.surf_offset = offset;
298 } else {
299 if (pitch_in_bytes_override &&
300 pitch_in_bytes_override != surface->u.legacy.level[0].nblk_x * bpe) {
301 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
302 * for those
303 */
304 surface->u.legacy.level[0].nblk_x = pitch_in_bytes_override / bpe;
305 surface->u.legacy.level[0].slice_size = pitch_in_bytes_override *
306 surface->u.legacy.level[0].nblk_y;
307 }
308
309 if (offset) {
310 for (i = 0; i < ARRAY_SIZE(surface->u.legacy.level); ++i)
311 surface->u.legacy.level[i].offset += offset;
312 }
313 }
314 return 0;
315 }
316
317 static void r600_texture_init_metadata(struct r600_common_screen *rscreen,
318 struct r600_texture *rtex,
319 struct radeon_bo_metadata *metadata)
320 {
321 struct radeon_surf *surface = &rtex->surface;
322
323 memset(metadata, 0, sizeof(*metadata));
324
325 if (rscreen->chip_class >= GFX9) {
326 metadata->u.gfx9.swizzle_mode = surface->u.gfx9.surf.swizzle_mode;
327 } else {
328 metadata->u.legacy.microtile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D ?
329 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
330 metadata->u.legacy.macrotile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D ?
331 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
332 metadata->u.legacy.pipe_config = surface->u.legacy.pipe_config;
333 metadata->u.legacy.bankw = surface->u.legacy.bankw;
334 metadata->u.legacy.bankh = surface->u.legacy.bankh;
335 metadata->u.legacy.tile_split = surface->u.legacy.tile_split;
336 metadata->u.legacy.mtilea = surface->u.legacy.mtilea;
337 metadata->u.legacy.num_banks = surface->u.legacy.num_banks;
338 metadata->u.legacy.stride = surface->u.legacy.level[0].nblk_x * surface->bpe;
339 metadata->u.legacy.scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
340 }
341 }
342
343 static void r600_eliminate_fast_color_clear(struct r600_common_context *rctx,
344 struct r600_texture *rtex)
345 {
346 struct r600_common_screen *rscreen = rctx->screen;
347 struct pipe_context *ctx = &rctx->b;
348
349 if (ctx == rscreen->aux_context)
350 mtx_lock(&rscreen->aux_context_lock);
351
352 ctx->flush_resource(ctx, &rtex->resource.b.b);
353 ctx->flush(ctx, NULL, 0);
354
355 if (ctx == rscreen->aux_context)
356 mtx_unlock(&rscreen->aux_context_lock);
357 }
358
359 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
360 struct r600_texture *rtex)
361 {
362 if (!rtex->cmask.size)
363 return;
364
365 assert(rtex->resource.b.b.nr_samples <= 1);
366
367 /* Disable CMASK. */
368 memset(&rtex->cmask, 0, sizeof(rtex->cmask));
369 rtex->cmask.base_address_reg = rtex->resource.gpu_address >> 8;
370 rtex->dirty_level_mask = 0;
371
372 if (rscreen->chip_class >= SI)
373 rtex->cb_color_info &= ~SI_S_028C70_FAST_CLEAR(1);
374 else
375 rtex->cb_color_info &= ~EG_S_028C70_FAST_CLEAR(1);
376
377 if (rtex->cmask_buffer != &rtex->resource)
378 r600_resource_reference(&rtex->cmask_buffer, NULL);
379
380 /* Notify all contexts about the change. */
381 p_atomic_inc(&rscreen->dirty_tex_counter);
382 p_atomic_inc(&rscreen->compressed_colortex_counter);
383 }
384
385 static bool r600_can_disable_dcc(struct r600_texture *rtex)
386 {
387 /* We can't disable DCC if it can be written by another process. */
388 return rtex->dcc_offset &&
389 (!rtex->resource.b.is_shared ||
390 !(rtex->resource.external_usage & PIPE_HANDLE_USAGE_WRITE));
391 }
392
393 static bool r600_texture_discard_dcc(struct r600_common_screen *rscreen,
394 struct r600_texture *rtex)
395 {
396 if (!r600_can_disable_dcc(rtex))
397 return false;
398
399 assert(rtex->dcc_separate_buffer == NULL);
400
401 /* Disable DCC. */
402 rtex->dcc_offset = 0;
403
404 /* Notify all contexts about the change. */
405 p_atomic_inc(&rscreen->dirty_tex_counter);
406 return true;
407 }
408
409 /**
410 * Disable DCC for the texture. (first decompress, then discard metadata).
411 *
412 * There is unresolved multi-context synchronization issue between
413 * screen::aux_context and the current context. If applications do this with
414 * multiple contexts, it's already undefined behavior for them and we don't
415 * have to worry about that. The scenario is:
416 *
417 * If context 1 disables DCC and context 2 has queued commands that write
418 * to the texture via CB with DCC enabled, and the order of operations is
419 * as follows:
420 * context 2 queues draw calls rendering to the texture, but doesn't flush
421 * context 1 disables DCC and flushes
422 * context 1 & 2 reset descriptors and FB state
423 * context 2 flushes (new compressed tiles written by the draw calls)
424 * context 1 & 2 read garbage, because DCC is disabled, yet there are
425 * compressed tiled
426 *
427 * \param rctx the current context if you have one, or rscreen->aux_context
428 * if you don't.
429 */
430 bool r600_texture_disable_dcc(struct r600_common_context *rctx,
431 struct r600_texture *rtex)
432 {
433 struct r600_common_screen *rscreen = rctx->screen;
434
435 if (!r600_can_disable_dcc(rtex))
436 return false;
437
438 if (&rctx->b == rscreen->aux_context)
439 mtx_lock(&rscreen->aux_context_lock);
440
441 /* Decompress DCC. */
442 rctx->decompress_dcc(&rctx->b, rtex);
443 rctx->b.flush(&rctx->b, NULL, 0);
444
445 if (&rctx->b == rscreen->aux_context)
446 mtx_unlock(&rscreen->aux_context_lock);
447
448 return r600_texture_discard_dcc(rscreen, rtex);
449 }
450
451 static void r600_degrade_tile_mode_to_linear(struct r600_common_context *rctx,
452 struct r600_texture *rtex,
453 bool invalidate_storage)
454 {
455 struct pipe_screen *screen = rctx->b.screen;
456 struct r600_texture *new_tex;
457 struct pipe_resource templ = rtex->resource.b.b;
458 unsigned i;
459
460 templ.bind |= PIPE_BIND_LINEAR;
461
462 /* r600g doesn't react to dirty_tex_descriptor_counter */
463 if (rctx->chip_class < SI)
464 return;
465
466 if (rtex->resource.b.is_shared ||
467 rtex->surface.is_linear)
468 return;
469
470 /* This fails with MSAA, depth, and compressed textures. */
471 if (r600_choose_tiling(rctx->screen, &templ) !=
472 RADEON_SURF_MODE_LINEAR_ALIGNED)
473 return;
474
475 new_tex = (struct r600_texture*)screen->resource_create(screen, &templ);
476 if (!new_tex)
477 return;
478
479 /* Copy the pixels to the new texture. */
480 if (!invalidate_storage) {
481 for (i = 0; i <= templ.last_level; i++) {
482 struct pipe_box box;
483
484 u_box_3d(0, 0, 0,
485 u_minify(templ.width0, i), u_minify(templ.height0, i),
486 util_max_layer(&templ, i) + 1, &box);
487
488 rctx->dma_copy(&rctx->b, &new_tex->resource.b.b, i, 0, 0, 0,
489 &rtex->resource.b.b, i, &box);
490 }
491 }
492
493 r600_texture_discard_cmask(rctx->screen, rtex);
494 r600_texture_discard_dcc(rctx->screen, rtex);
495
496 /* Replace the structure fields of rtex. */
497 rtex->resource.b.b.bind = templ.bind;
498 pb_reference(&rtex->resource.buf, new_tex->resource.buf);
499 rtex->resource.gpu_address = new_tex->resource.gpu_address;
500 rtex->resource.vram_usage = new_tex->resource.vram_usage;
501 rtex->resource.gart_usage = new_tex->resource.gart_usage;
502 rtex->resource.bo_size = new_tex->resource.bo_size;
503 rtex->resource.bo_alignment = new_tex->resource.bo_alignment;
504 rtex->resource.domains = new_tex->resource.domains;
505 rtex->resource.flags = new_tex->resource.flags;
506 rtex->size = new_tex->size;
507 rtex->surface = new_tex->surface;
508 rtex->non_disp_tiling = new_tex->non_disp_tiling;
509 rtex->cb_color_info = new_tex->cb_color_info;
510 rtex->cmask = new_tex->cmask; /* needed even without CMASK */
511
512 assert(!rtex->htile_offset);
513 assert(!rtex->cmask.size);
514 assert(!rtex->fmask.size);
515 assert(!rtex->dcc_offset);
516 assert(!rtex->is_depth);
517
518 r600_texture_reference(&new_tex, NULL);
519
520 p_atomic_inc(&rctx->screen->dirty_tex_counter);
521 }
522
523 static boolean r600_texture_get_handle(struct pipe_screen* screen,
524 struct pipe_context *ctx,
525 struct pipe_resource *resource,
526 struct winsys_handle *whandle,
527 unsigned usage)
528 {
529 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
530 struct r600_common_context *rctx;
531 struct r600_resource *res = (struct r600_resource*)resource;
532 struct r600_texture *rtex = (struct r600_texture*)resource;
533 struct radeon_bo_metadata metadata;
534 bool update_metadata = false;
535 unsigned stride, offset, slice_size;
536
537 ctx = threaded_context_unwrap_sync(ctx);
538 rctx = (struct r600_common_context*)(ctx ? ctx : rscreen->aux_context);
539
540 if (resource->target != PIPE_BUFFER) {
541 /* This is not supported now, but it might be required for OpenCL
542 * interop in the future.
543 */
544 if (resource->nr_samples > 1 || rtex->is_depth)
545 return false;
546
547 /* Since shader image stores don't support DCC on VI,
548 * disable it for external clients that want write
549 * access.
550 */
551 if (usage & PIPE_HANDLE_USAGE_WRITE && rtex->dcc_offset) {
552 if (r600_texture_disable_dcc(rctx, rtex))
553 update_metadata = true;
554 }
555
556 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
557 (rtex->cmask.size || rtex->dcc_offset)) {
558 /* Eliminate fast clear (both CMASK and DCC) */
559 r600_eliminate_fast_color_clear(rctx, rtex);
560
561 /* Disable CMASK if flush_resource isn't going
562 * to be called.
563 */
564 if (rtex->cmask.size)
565 r600_texture_discard_cmask(rscreen, rtex);
566 }
567
568 /* Set metadata. */
569 if (!res->b.is_shared || update_metadata) {
570 r600_texture_init_metadata(rscreen, rtex, &metadata);
571 if (rscreen->query_opaque_metadata)
572 rscreen->query_opaque_metadata(rscreen, rtex,
573 &metadata);
574
575 rscreen->ws->buffer_set_metadata(res->buf, &metadata);
576 }
577
578 if (rscreen->chip_class >= GFX9) {
579 offset = rtex->surface.u.gfx9.surf_offset;
580 stride = rtex->surface.u.gfx9.surf_pitch *
581 rtex->surface.bpe;
582 slice_size = rtex->surface.u.gfx9.surf_slice_size;
583 } else {
584 offset = rtex->surface.u.legacy.level[0].offset;
585 stride = rtex->surface.u.legacy.level[0].nblk_x *
586 rtex->surface.bpe;
587 slice_size = rtex->surface.u.legacy.level[0].slice_size;
588 }
589 } else {
590 /* Buffers */
591 offset = 0;
592 stride = 0;
593 slice_size = 0;
594 }
595
596 if (res->b.is_shared) {
597 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
598 * doesn't set it.
599 */
600 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
601 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
602 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
603 } else {
604 res->b.is_shared = true;
605 res->external_usage = usage;
606 }
607
608 return rscreen->ws->buffer_get_handle(res->buf, stride, offset,
609 slice_size, whandle);
610 }
611
612 static void r600_texture_destroy(struct pipe_screen *screen,
613 struct pipe_resource *ptex)
614 {
615 struct r600_texture *rtex = (struct r600_texture*)ptex;
616 struct r600_resource *resource = &rtex->resource;
617
618 r600_texture_reference(&rtex->flushed_depth_texture, NULL);
619
620 if (rtex->cmask_buffer != &rtex->resource) {
621 r600_resource_reference(&rtex->cmask_buffer, NULL);
622 }
623 pb_reference(&resource->buf, NULL);
624 r600_resource_reference(&rtex->dcc_separate_buffer, NULL);
625 r600_resource_reference(&rtex->last_dcc_separate_buffer, NULL);
626 FREE(rtex);
627 }
628
629 static const struct u_resource_vtbl r600_texture_vtbl;
630
631 /* The number of samples can be specified independently of the texture. */
632 void r600_texture_get_fmask_info(struct r600_common_screen *rscreen,
633 struct r600_texture *rtex,
634 unsigned nr_samples,
635 struct r600_fmask_info *out)
636 {
637 /* FMASK is allocated like an ordinary texture. */
638 struct pipe_resource templ = rtex->resource.b.b;
639 struct radeon_surf fmask = {};
640 unsigned flags, bpe;
641
642 memset(out, 0, sizeof(*out));
643
644 if (rscreen->chip_class >= GFX9) {
645 out->alignment = rtex->surface.u.gfx9.fmask_alignment;
646 out->size = rtex->surface.u.gfx9.fmask_size;
647 return;
648 }
649
650 templ.nr_samples = 1;
651 flags = rtex->surface.flags | RADEON_SURF_FMASK;
652
653 if (rscreen->chip_class <= CAYMAN) {
654 /* Use the same parameters and tile mode. */
655 fmask.u.legacy.bankw = rtex->surface.u.legacy.bankw;
656 fmask.u.legacy.bankh = rtex->surface.u.legacy.bankh;
657 fmask.u.legacy.mtilea = rtex->surface.u.legacy.mtilea;
658 fmask.u.legacy.tile_split = rtex->surface.u.legacy.tile_split;
659
660 if (nr_samples <= 4)
661 fmask.u.legacy.bankh = 4;
662 }
663
664 switch (nr_samples) {
665 case 2:
666 case 4:
667 bpe = 1;
668 break;
669 case 8:
670 bpe = 4;
671 break;
672 default:
673 R600_ERR("Invalid sample count for FMASK allocation.\n");
674 return;
675 }
676
677 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
678 * This can be fixed by writing a separate FMASK allocator specifically
679 * for R600-R700 asics. */
680 if (rscreen->chip_class <= R700) {
681 bpe *= 2;
682 }
683
684 if (rscreen->ws->surface_init(rscreen->ws, &templ, flags, bpe,
685 RADEON_SURF_MODE_2D, &fmask)) {
686 R600_ERR("Got error in surface_init while allocating FMASK.\n");
687 return;
688 }
689
690 assert(fmask.u.legacy.level[0].mode == RADEON_SURF_MODE_2D);
691
692 out->slice_tile_max = (fmask.u.legacy.level[0].nblk_x * fmask.u.legacy.level[0].nblk_y) / 64;
693 if (out->slice_tile_max)
694 out->slice_tile_max -= 1;
695
696 out->tile_mode_index = fmask.u.legacy.tiling_index[0];
697 out->pitch_in_pixels = fmask.u.legacy.level[0].nblk_x;
698 out->bank_height = fmask.u.legacy.bankh;
699 out->alignment = MAX2(256, fmask.surf_alignment);
700 out->size = fmask.surf_size;
701 }
702
703 static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
704 struct r600_texture *rtex)
705 {
706 r600_texture_get_fmask_info(rscreen, rtex,
707 rtex->resource.b.b.nr_samples, &rtex->fmask);
708
709 rtex->fmask.offset = align64(rtex->size, rtex->fmask.alignment);
710 rtex->size = rtex->fmask.offset + rtex->fmask.size;
711 }
712
713 void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
714 struct r600_texture *rtex,
715 struct r600_cmask_info *out)
716 {
717 unsigned cmask_tile_width = 8;
718 unsigned cmask_tile_height = 8;
719 unsigned cmask_tile_elements = cmask_tile_width * cmask_tile_height;
720 unsigned element_bits = 4;
721 unsigned cmask_cache_bits = 1024;
722 unsigned num_pipes = rscreen->info.num_tile_pipes;
723 unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
724
725 unsigned elements_per_macro_tile = (cmask_cache_bits / element_bits) * num_pipes;
726 unsigned pixels_per_macro_tile = elements_per_macro_tile * cmask_tile_elements;
727 unsigned sqrt_pixels_per_macro_tile = sqrt(pixels_per_macro_tile);
728 unsigned macro_tile_width = util_next_power_of_two(sqrt_pixels_per_macro_tile);
729 unsigned macro_tile_height = pixels_per_macro_tile / macro_tile_width;
730
731 unsigned pitch_elements = align(rtex->resource.b.b.width0, macro_tile_width);
732 unsigned height = align(rtex->resource.b.b.height0, macro_tile_height);
733
734 unsigned base_align = num_pipes * pipe_interleave_bytes;
735 unsigned slice_bytes =
736 ((pitch_elements * height * element_bits + 7) / 8) / cmask_tile_elements;
737
738 assert(macro_tile_width % 128 == 0);
739 assert(macro_tile_height % 128 == 0);
740
741 out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
742 out->alignment = MAX2(256, base_align);
743 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
744 align(slice_bytes, base_align);
745 }
746
747 static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
748 struct r600_texture *rtex,
749 struct r600_cmask_info *out)
750 {
751 unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
752 unsigned num_pipes = rscreen->info.num_tile_pipes;
753 unsigned cl_width, cl_height;
754
755 if (rscreen->chip_class >= GFX9) {
756 out->alignment = rtex->surface.u.gfx9.cmask_alignment;
757 out->size = rtex->surface.u.gfx9.cmask_size;
758 return;
759 }
760
761 switch (num_pipes) {
762 case 2:
763 cl_width = 32;
764 cl_height = 16;
765 break;
766 case 4:
767 cl_width = 32;
768 cl_height = 32;
769 break;
770 case 8:
771 cl_width = 64;
772 cl_height = 32;
773 break;
774 case 16: /* Hawaii */
775 cl_width = 64;
776 cl_height = 64;
777 break;
778 default:
779 assert(0);
780 return;
781 }
782
783 unsigned base_align = num_pipes * pipe_interleave_bytes;
784
785 unsigned width = align(rtex->resource.b.b.width0, cl_width*8);
786 unsigned height = align(rtex->resource.b.b.height0, cl_height*8);
787 unsigned slice_elements = (width * height) / (8*8);
788
789 /* Each element of CMASK is a nibble. */
790 unsigned slice_bytes = slice_elements / 2;
791
792 out->slice_tile_max = (width * height) / (128*128);
793 if (out->slice_tile_max)
794 out->slice_tile_max -= 1;
795
796 out->alignment = MAX2(256, base_align);
797 out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
798 align(slice_bytes, base_align);
799 }
800
801 static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
802 struct r600_texture *rtex)
803 {
804 if (rscreen->chip_class >= SI) {
805 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
806 } else {
807 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
808 }
809
810 rtex->cmask.offset = align64(rtex->size, rtex->cmask.alignment);
811 rtex->size = rtex->cmask.offset + rtex->cmask.size;
812
813 if (rscreen->chip_class >= SI)
814 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
815 else
816 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
817 }
818
819 static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen,
820 struct r600_texture *rtex)
821 {
822 if (rtex->cmask_buffer)
823 return;
824
825 assert(rtex->cmask.size == 0);
826
827 if (rscreen->chip_class >= SI) {
828 si_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
829 } else {
830 r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
831 }
832
833 rtex->cmask_buffer = (struct r600_resource *)
834 r600_aligned_buffer_create(&rscreen->b,
835 R600_RESOURCE_FLAG_UNMAPPABLE,
836 PIPE_USAGE_DEFAULT,
837 rtex->cmask.size,
838 rtex->cmask.alignment);
839 if (rtex->cmask_buffer == NULL) {
840 rtex->cmask.size = 0;
841 return;
842 }
843
844 /* update colorbuffer state bits */
845 rtex->cmask.base_address_reg = rtex->cmask_buffer->gpu_address >> 8;
846
847 if (rscreen->chip_class >= SI)
848 rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
849 else
850 rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
851
852 p_atomic_inc(&rscreen->compressed_colortex_counter);
853 }
854
855 static void r600_texture_get_htile_size(struct r600_common_screen *rscreen,
856 struct r600_texture *rtex)
857 {
858 unsigned cl_width, cl_height, width, height;
859 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
860 unsigned num_pipes = rscreen->info.num_tile_pipes;
861
862 assert(rscreen->chip_class <= VI);
863
864 rtex->surface.htile_size = 0;
865
866 if (rscreen->chip_class <= EVERGREEN &&
867 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 26)
868 return;
869
870 /* HW bug on R6xx. */
871 if (rscreen->chip_class == R600 &&
872 (rtex->resource.b.b.width0 > 7680 ||
873 rtex->resource.b.b.height0 > 7680))
874 return;
875
876 /* HTILE is broken with 1D tiling on old kernels and CIK. */
877 if (rscreen->chip_class >= CIK &&
878 rtex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
879 rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
880 return;
881
882 /* Overalign HTILE on P2 configs to work around GPU hangs in
883 * piglit/depthstencil-render-miplevels 585.
884 *
885 * This has been confirmed to help Kabini & Stoney, where the hangs
886 * are always reproducible. I think I have seen the test hang
887 * on Carrizo too, though it was very rare there.
888 */
889 if (rscreen->chip_class >= CIK && num_pipes < 4)
890 num_pipes = 4;
891
892 switch (num_pipes) {
893 case 1:
894 cl_width = 32;
895 cl_height = 16;
896 break;
897 case 2:
898 cl_width = 32;
899 cl_height = 32;
900 break;
901 case 4:
902 cl_width = 64;
903 cl_height = 32;
904 break;
905 case 8:
906 cl_width = 64;
907 cl_height = 64;
908 break;
909 case 16:
910 cl_width = 128;
911 cl_height = 64;
912 break;
913 default:
914 assert(0);
915 return;
916 }
917
918 width = align(rtex->resource.b.b.width0, cl_width * 8);
919 height = align(rtex->resource.b.b.height0, cl_height * 8);
920
921 slice_elements = (width * height) / (8 * 8);
922 slice_bytes = slice_elements * 4;
923
924 pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
925 base_align = num_pipes * pipe_interleave_bytes;
926
927 rtex->surface.htile_alignment = base_align;
928 rtex->surface.htile_size =
929 (util_max_layer(&rtex->resource.b.b, 0) + 1) *
930 align(slice_bytes, base_align);
931 }
932
933 static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
934 struct r600_texture *rtex)
935 {
936 if (rscreen->chip_class <= VI && !rtex->tc_compatible_htile)
937 r600_texture_get_htile_size(rscreen, rtex);
938
939 if (!rtex->surface.htile_size)
940 return;
941
942 rtex->htile_offset = align(rtex->size, rtex->surface.htile_alignment);
943 rtex->size = rtex->htile_offset + rtex->surface.htile_size;
944 }
945
946 void r600_print_texture_info(struct r600_common_screen *rscreen,
947 struct r600_texture *rtex, FILE *f)
948 {
949 int i;
950
951 /* Common parameters. */
952 fprintf(f, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
953 "blk_h=%u, array_size=%u, last_level=%u, "
954 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
955 rtex->resource.b.b.width0, rtex->resource.b.b.height0,
956 rtex->resource.b.b.depth0, rtex->surface.blk_w,
957 rtex->surface.blk_h,
958 rtex->resource.b.b.array_size, rtex->resource.b.b.last_level,
959 rtex->surface.bpe, rtex->resource.b.b.nr_samples,
960 rtex->surface.flags, util_format_short_name(rtex->resource.b.b.format));
961
962 if (rscreen->chip_class >= GFX9) {
963 fprintf(f, " Surf: size=%"PRIu64", slice_size=%"PRIu64", "
964 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
965 rtex->surface.surf_size,
966 rtex->surface.u.gfx9.surf_slice_size,
967 rtex->surface.surf_alignment,
968 rtex->surface.u.gfx9.surf.swizzle_mode,
969 rtex->surface.u.gfx9.surf.epitch,
970 rtex->surface.u.gfx9.surf_pitch);
971
972 if (rtex->fmask.size) {
973 fprintf(f, " FMASK: offset=%"PRIu64", size=%"PRIu64", "
974 "alignment=%u, swmode=%u, epitch=%u\n",
975 rtex->fmask.offset,
976 rtex->surface.u.gfx9.fmask_size,
977 rtex->surface.u.gfx9.fmask_alignment,
978 rtex->surface.u.gfx9.fmask.swizzle_mode,
979 rtex->surface.u.gfx9.fmask.epitch);
980 }
981
982 if (rtex->cmask.size) {
983 fprintf(f, " CMask: offset=%"PRIu64", size=%"PRIu64", "
984 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
985 rtex->cmask.offset,
986 rtex->surface.u.gfx9.cmask_size,
987 rtex->surface.u.gfx9.cmask_alignment,
988 rtex->surface.u.gfx9.cmask.rb_aligned,
989 rtex->surface.u.gfx9.cmask.pipe_aligned);
990 }
991
992 if (rtex->htile_offset) {
993 fprintf(f, " HTile: offset=%"PRIu64", size=%"PRIu64", alignment=%u, "
994 "rb_aligned=%u, pipe_aligned=%u\n",
995 rtex->htile_offset,
996 rtex->surface.htile_size,
997 rtex->surface.htile_alignment,
998 rtex->surface.u.gfx9.htile.rb_aligned,
999 rtex->surface.u.gfx9.htile.pipe_aligned);
1000 }
1001
1002 if (rtex->dcc_offset) {
1003 fprintf(f, " DCC: offset=%"PRIu64", size=%"PRIu64", "
1004 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1005 rtex->dcc_offset, rtex->surface.dcc_size,
1006 rtex->surface.dcc_alignment,
1007 rtex->surface.u.gfx9.dcc_pitch_max,
1008 rtex->surface.num_dcc_levels);
1009 }
1010
1011 if (rtex->surface.u.gfx9.stencil_offset) {
1012 fprintf(f, " Stencil: offset=%"PRIu64", swmode=%u, epitch=%u\n",
1013 rtex->surface.u.gfx9.stencil_offset,
1014 rtex->surface.u.gfx9.stencil.swizzle_mode,
1015 rtex->surface.u.gfx9.stencil.epitch);
1016 }
1017 return;
1018 }
1019
1020 fprintf(f, " Layout: size=%"PRIu64", alignment=%u, bankw=%u, "
1021 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1022 rtex->surface.surf_size, rtex->surface.surf_alignment, rtex->surface.u.legacy.bankw,
1023 rtex->surface.u.legacy.bankh, rtex->surface.u.legacy.num_banks, rtex->surface.u.legacy.mtilea,
1024 rtex->surface.u.legacy.tile_split, rtex->surface.u.legacy.pipe_config,
1025 (rtex->surface.flags & RADEON_SURF_SCANOUT) != 0);
1026
1027 if (rtex->fmask.size)
1028 fprintf(f, " FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
1029 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1030 rtex->fmask.offset, rtex->fmask.size, rtex->fmask.alignment,
1031 rtex->fmask.pitch_in_pixels, rtex->fmask.bank_height,
1032 rtex->fmask.slice_tile_max, rtex->fmask.tile_mode_index);
1033
1034 if (rtex->cmask.size)
1035 fprintf(f, " CMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, "
1036 "slice_tile_max=%u\n",
1037 rtex->cmask.offset, rtex->cmask.size, rtex->cmask.alignment,
1038 rtex->cmask.slice_tile_max);
1039
1040 if (rtex->htile_offset)
1041 fprintf(f, " HTile: offset=%"PRIu64", size=%"PRIu64", "
1042 "alignment=%u, TC_compatible = %u\n",
1043 rtex->htile_offset, rtex->surface.htile_size,
1044 rtex->surface.htile_alignment,
1045 rtex->tc_compatible_htile);
1046
1047 if (rtex->dcc_offset) {
1048 fprintf(f, " DCC: offset=%"PRIu64", size=%"PRIu64", alignment=%u\n",
1049 rtex->dcc_offset, rtex->surface.dcc_size,
1050 rtex->surface.dcc_alignment);
1051 for (i = 0; i <= rtex->resource.b.b.last_level; i++)
1052 fprintf(f, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64", "
1053 "fast_clear_size=%"PRIu64"\n",
1054 i, i < rtex->surface.num_dcc_levels,
1055 rtex->surface.u.legacy.level[i].dcc_offset,
1056 rtex->surface.u.legacy.level[i].dcc_fast_clear_size);
1057 }
1058
1059 for (i = 0; i <= rtex->resource.b.b.last_level; i++)
1060 fprintf(f, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
1061 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1062 "mode=%u, tiling_index = %u\n",
1063 i, rtex->surface.u.legacy.level[i].offset,
1064 rtex->surface.u.legacy.level[i].slice_size,
1065 u_minify(rtex->resource.b.b.width0, i),
1066 u_minify(rtex->resource.b.b.height0, i),
1067 u_minify(rtex->resource.b.b.depth0, i),
1068 rtex->surface.u.legacy.level[i].nblk_x,
1069 rtex->surface.u.legacy.level[i].nblk_y,
1070 rtex->surface.u.legacy.level[i].mode,
1071 rtex->surface.u.legacy.tiling_index[i]);
1072
1073 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
1074 fprintf(f, " StencilLayout: tilesplit=%u\n",
1075 rtex->surface.u.legacy.stencil_tile_split);
1076 for (i = 0; i <= rtex->resource.b.b.last_level; i++) {
1077 fprintf(f, " StencilLevel[%i]: offset=%"PRIu64", "
1078 "slice_size=%"PRIu64", npix_x=%u, "
1079 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1080 "mode=%u, tiling_index = %u\n",
1081 i, rtex->surface.u.legacy.stencil_level[i].offset,
1082 rtex->surface.u.legacy.stencil_level[i].slice_size,
1083 u_minify(rtex->resource.b.b.width0, i),
1084 u_minify(rtex->resource.b.b.height0, i),
1085 u_minify(rtex->resource.b.b.depth0, i),
1086 rtex->surface.u.legacy.stencil_level[i].nblk_x,
1087 rtex->surface.u.legacy.stencil_level[i].nblk_y,
1088 rtex->surface.u.legacy.stencil_level[i].mode,
1089 rtex->surface.u.legacy.stencil_tiling_index[i]);
1090 }
1091 }
1092 }
1093
1094 /* Common processing for r600_texture_create and r600_texture_from_handle */
1095 static struct r600_texture *
1096 r600_texture_create_object(struct pipe_screen *screen,
1097 const struct pipe_resource *base,
1098 struct pb_buffer *buf,
1099 struct radeon_surf *surface)
1100 {
1101 struct r600_texture *rtex;
1102 struct r600_resource *resource;
1103 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1104
1105 rtex = CALLOC_STRUCT(r600_texture);
1106 if (!rtex)
1107 return NULL;
1108
1109 resource = &rtex->resource;
1110 resource->b.b = *base;
1111 resource->b.b.next = NULL;
1112 resource->b.vtbl = &r600_texture_vtbl;
1113 pipe_reference_init(&resource->b.b.reference, 1);
1114 resource->b.b.screen = screen;
1115
1116 /* don't include stencil-only formats which we don't support for rendering */
1117 rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
1118
1119 rtex->surface = *surface;
1120 rtex->size = rtex->surface.surf_size;
1121
1122 rtex->tc_compatible_htile = rtex->surface.htile_size != 0 &&
1123 (rtex->surface.flags &
1124 RADEON_SURF_TC_COMPATIBLE_HTILE);
1125
1126 /* TC-compatible HTILE:
1127 * - VI only supports Z32_FLOAT.
1128 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1129 if (rtex->tc_compatible_htile) {
1130 if (rscreen->chip_class >= GFX9 &&
1131 base->format == PIPE_FORMAT_Z16_UNORM)
1132 rtex->db_render_format = base->format;
1133 else
1134 rtex->db_render_format = PIPE_FORMAT_Z32_FLOAT;
1135 } else {
1136 rtex->db_render_format = base->format;
1137 }
1138
1139 /* Tiled depth textures utilize the non-displayable tile order.
1140 * This must be done after r600_setup_surface.
1141 * Applies to R600-Cayman. */
1142 rtex->non_disp_tiling = rtex->is_depth && rtex->surface.u.legacy.level[0].mode >= RADEON_SURF_MODE_1D;
1143 /* Applies to GCN. */
1144 rtex->last_msaa_resolve_target_micro_mode = rtex->surface.micro_tile_mode;
1145
1146 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1147 * between frames, so the only thing that can enable separate DCC
1148 * with DRI2 is multiple slow clears within a frame.
1149 */
1150 rtex->ps_draw_ratio = 0;
1151
1152 if (rtex->is_depth) {
1153 if (base->flags & (R600_RESOURCE_FLAG_TRANSFER |
1154 R600_RESOURCE_FLAG_FLUSHED_DEPTH) ||
1155 rscreen->chip_class >= EVERGREEN) {
1156 if (rscreen->chip_class >= GFX9) {
1157 rtex->can_sample_z = true;
1158 rtex->can_sample_s = true;
1159 } else {
1160 rtex->can_sample_z = !rtex->surface.u.legacy.depth_adjusted;
1161 rtex->can_sample_s = !rtex->surface.u.legacy.stencil_adjusted;
1162 }
1163 } else {
1164 if (rtex->resource.b.b.nr_samples <= 1 &&
1165 (rtex->resource.b.b.format == PIPE_FORMAT_Z16_UNORM ||
1166 rtex->resource.b.b.format == PIPE_FORMAT_Z32_FLOAT))
1167 rtex->can_sample_z = true;
1168 }
1169
1170 if (!(base->flags & (R600_RESOURCE_FLAG_TRANSFER |
1171 R600_RESOURCE_FLAG_FLUSHED_DEPTH))) {
1172 rtex->db_compatible = true;
1173
1174 if (!(rscreen->debug_flags & DBG_NO_HYPERZ))
1175 r600_texture_allocate_htile(rscreen, rtex);
1176 }
1177 } else {
1178 if (base->nr_samples > 1) {
1179 if (!buf) {
1180 r600_texture_allocate_fmask(rscreen, rtex);
1181 r600_texture_allocate_cmask(rscreen, rtex);
1182 rtex->cmask_buffer = &rtex->resource;
1183 }
1184 if (!rtex->fmask.size || !rtex->cmask.size) {
1185 FREE(rtex);
1186 return NULL;
1187 }
1188 }
1189
1190 /* Shared textures must always set up DCC here.
1191 * If it's not present, it will be disabled by
1192 * apply_opaque_metadata later.
1193 */
1194 if (rtex->surface.dcc_size &&
1195 (buf || !(rscreen->debug_flags & DBG_NO_DCC)) &&
1196 !(rtex->surface.flags & RADEON_SURF_SCANOUT)) {
1197 /* Reserve space for the DCC buffer. */
1198 rtex->dcc_offset = align64(rtex->size, rtex->surface.dcc_alignment);
1199 rtex->size = rtex->dcc_offset + rtex->surface.dcc_size;
1200 }
1201 }
1202
1203 /* Now create the backing buffer. */
1204 if (!buf) {
1205 r600_init_resource_fields(rscreen, resource, rtex->size,
1206 rtex->surface.surf_alignment);
1207
1208 resource->flags |= RADEON_FLAG_NO_SUBALLOC;
1209
1210 if (!r600_alloc_resource(rscreen, resource)) {
1211 FREE(rtex);
1212 return NULL;
1213 }
1214 } else {
1215 resource->buf = buf;
1216 resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->buf);
1217 resource->bo_size = buf->size;
1218 resource->bo_alignment = buf->alignment;
1219 resource->domains = rscreen->ws->buffer_get_initial_domain(resource->buf);
1220 if (resource->domains & RADEON_DOMAIN_VRAM)
1221 resource->vram_usage = buf->size;
1222 else if (resource->domains & RADEON_DOMAIN_GTT)
1223 resource->gart_usage = buf->size;
1224 }
1225
1226 if (rtex->cmask.size) {
1227 /* Initialize the cmask to 0xCC (= compressed state). */
1228 r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
1229 rtex->cmask.offset, rtex->cmask.size,
1230 0xCCCCCCCC);
1231 }
1232 if (rtex->htile_offset) {
1233 uint32_t clear_value = 0;
1234
1235 if (rscreen->chip_class >= GFX9 || rtex->tc_compatible_htile)
1236 clear_value = 0x0000030F;
1237
1238 r600_screen_clear_buffer(rscreen, &rtex->resource.b.b,
1239 rtex->htile_offset,
1240 rtex->surface.htile_size,
1241 clear_value);
1242 }
1243
1244 /* Initialize DCC only if the texture is not being imported. */
1245 if (!buf && rtex->dcc_offset) {
1246 r600_screen_clear_buffer(rscreen, &rtex->resource.b.b,
1247 rtex->dcc_offset,
1248 rtex->surface.dcc_size,
1249 0xFFFFFFFF);
1250 }
1251
1252 /* Initialize the CMASK base register value. */
1253 rtex->cmask.base_address_reg =
1254 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1255
1256 if (rscreen->debug_flags & DBG_VM) {
1257 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1258 rtex->resource.gpu_address,
1259 rtex->resource.gpu_address + rtex->resource.buf->size,
1260 base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
1261 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1262 }
1263
1264 if (rscreen->debug_flags & DBG_TEX) {
1265 puts("Texture:");
1266 r600_print_texture_info(rscreen, rtex, stdout);
1267 fflush(stdout);
1268 }
1269
1270 return rtex;
1271 }
1272
1273 static enum radeon_surf_mode
1274 r600_choose_tiling(struct r600_common_screen *rscreen,
1275 const struct pipe_resource *templ)
1276 {
1277 const struct util_format_description *desc = util_format_description(templ->format);
1278 bool force_tiling = templ->flags & R600_RESOURCE_FLAG_FORCE_TILING;
1279 bool is_depth_stencil = util_format_is_depth_or_stencil(templ->format) &&
1280 !(templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH);
1281
1282 /* MSAA resources must be 2D tiled. */
1283 if (templ->nr_samples > 1)
1284 return RADEON_SURF_MODE_2D;
1285
1286 /* Transfer resources should be linear. */
1287 if (templ->flags & R600_RESOURCE_FLAG_TRANSFER)
1288 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1289
1290 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1291 * which requires 2D tiling.
1292 */
1293 if (rscreen->chip_class == VI &&
1294 is_depth_stencil &&
1295 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY))
1296 return RADEON_SURF_MODE_2D;
1297
1298 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
1299 if (rscreen->chip_class >= R600 && rscreen->chip_class <= CAYMAN &&
1300 (templ->bind & PIPE_BIND_COMPUTE_RESOURCE) &&
1301 (templ->target == PIPE_TEXTURE_2D ||
1302 templ->target == PIPE_TEXTURE_3D))
1303 force_tiling = true;
1304
1305 /* Handle common candidates for the linear mode.
1306 * Compressed textures and DB surfaces must always be tiled.
1307 */
1308 if (!force_tiling &&
1309 !is_depth_stencil &&
1310 !util_format_is_compressed(templ->format)) {
1311 if (rscreen->debug_flags & DBG_NO_TILING)
1312 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1313
1314 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1315 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1316 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1317
1318 /* Cursors are linear on SI.
1319 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1320 if (rscreen->chip_class >= SI &&
1321 (templ->bind & PIPE_BIND_CURSOR))
1322 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1323
1324 if (templ->bind & PIPE_BIND_LINEAR)
1325 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1326
1327 /* Textures with a very small height are recommended to be linear. */
1328 if (templ->target == PIPE_TEXTURE_1D ||
1329 templ->target == PIPE_TEXTURE_1D_ARRAY ||
1330 /* Only very thin and long 2D textures should benefit from
1331 * linear_aligned. */
1332 (templ->width0 > 8 && templ->height0 <= 2))
1333 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1334
1335 /* Textures likely to be mapped often. */
1336 if (templ->usage == PIPE_USAGE_STAGING ||
1337 templ->usage == PIPE_USAGE_STREAM)
1338 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1339 }
1340
1341 /* Make small textures 1D tiled. */
1342 if (templ->width0 <= 16 || templ->height0 <= 16 ||
1343 (rscreen->debug_flags & DBG_NO_2D_TILING))
1344 return RADEON_SURF_MODE_1D;
1345
1346 /* The allocator will switch to 1D if needed. */
1347 return RADEON_SURF_MODE_2D;
1348 }
1349
1350 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
1351 const struct pipe_resource *templ)
1352 {
1353 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1354 struct radeon_surf surface = {0};
1355 bool is_flushed_depth = templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH;
1356 bool tc_compatible_htile =
1357 rscreen->chip_class >= VI &&
1358 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) &&
1359 !(rscreen->debug_flags & DBG_NO_HYPERZ) &&
1360 !is_flushed_depth &&
1361 templ->nr_samples <= 1 && /* TC-compat HTILE is less efficient with MSAA */
1362 util_format_is_depth_or_stencil(templ->format);
1363
1364 int r;
1365
1366 r = r600_init_surface(rscreen, &surface, templ,
1367 r600_choose_tiling(rscreen, templ), 0, 0,
1368 false, false, is_flushed_depth,
1369 tc_compatible_htile);
1370 if (r) {
1371 return NULL;
1372 }
1373
1374 return (struct pipe_resource *)
1375 r600_texture_create_object(screen, templ, NULL, &surface);
1376 }
1377
1378 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
1379 const struct pipe_resource *templ,
1380 struct winsys_handle *whandle,
1381 unsigned usage)
1382 {
1383 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1384 struct pb_buffer *buf = NULL;
1385 unsigned stride = 0, offset = 0;
1386 unsigned array_mode;
1387 struct radeon_surf surface;
1388 int r;
1389 struct radeon_bo_metadata metadata = {};
1390 struct r600_texture *rtex;
1391 bool is_scanout;
1392
1393 /* Support only 2D textures without mipmaps */
1394 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
1395 templ->depth0 != 1 || templ->last_level != 0)
1396 return NULL;
1397
1398 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride, &offset);
1399 if (!buf)
1400 return NULL;
1401
1402 rscreen->ws->buffer_get_metadata(buf, &metadata);
1403
1404 if (rscreen->chip_class >= GFX9) {
1405 if (metadata.u.gfx9.swizzle_mode > 0)
1406 array_mode = RADEON_SURF_MODE_2D;
1407 else
1408 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1409
1410 is_scanout = metadata.u.gfx9.swizzle_mode == 0 ||
1411 metadata.u.gfx9.swizzle_mode % 4 == 2;
1412 } else {
1413 surface.u.legacy.pipe_config = metadata.u.legacy.pipe_config;
1414 surface.u.legacy.bankw = metadata.u.legacy.bankw;
1415 surface.u.legacy.bankh = metadata.u.legacy.bankh;
1416 surface.u.legacy.tile_split = metadata.u.legacy.tile_split;
1417 surface.u.legacy.mtilea = metadata.u.legacy.mtilea;
1418 surface.u.legacy.num_banks = metadata.u.legacy.num_banks;
1419
1420 if (metadata.u.legacy.macrotile == RADEON_LAYOUT_TILED)
1421 array_mode = RADEON_SURF_MODE_2D;
1422 else if (metadata.u.legacy.microtile == RADEON_LAYOUT_TILED)
1423 array_mode = RADEON_SURF_MODE_1D;
1424 else
1425 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1426
1427 is_scanout = metadata.u.legacy.scanout;
1428 }
1429
1430 r = r600_init_surface(rscreen, &surface, templ, array_mode, stride,
1431 offset, true, is_scanout, false, false);
1432 if (r) {
1433 return NULL;
1434 }
1435
1436 rtex = r600_texture_create_object(screen, templ, buf, &surface);
1437 if (!rtex)
1438 return NULL;
1439
1440 rtex->resource.b.is_shared = true;
1441 rtex->resource.external_usage = usage;
1442
1443 if (rscreen->apply_opaque_metadata)
1444 rscreen->apply_opaque_metadata(rscreen, rtex, &metadata);
1445
1446 /* Validate that addrlib arrived at the same surface parameters. */
1447 if (rscreen->chip_class >= GFX9) {
1448 assert(metadata.u.gfx9.swizzle_mode == surface.u.gfx9.surf.swizzle_mode);
1449 }
1450
1451 return &rtex->resource.b.b;
1452 }
1453
1454 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
1455 struct pipe_resource *texture,
1456 struct r600_texture **staging)
1457 {
1458 struct r600_texture *rtex = (struct r600_texture*)texture;
1459 struct pipe_resource resource;
1460 struct r600_texture **flushed_depth_texture = staging ?
1461 staging : &rtex->flushed_depth_texture;
1462 enum pipe_format pipe_format = texture->format;
1463
1464 if (!staging) {
1465 if (rtex->flushed_depth_texture)
1466 return true; /* it's ready */
1467
1468 if (!rtex->can_sample_z && rtex->can_sample_s) {
1469 switch (pipe_format) {
1470 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1471 /* Save memory by not allocating the S plane. */
1472 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1473 break;
1474 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1475 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1476 /* Save memory bandwidth by not copying the
1477 * stencil part during flush.
1478 *
1479 * This potentially increases memory bandwidth
1480 * if an application uses both Z and S texturing
1481 * simultaneously (a flushed Z24S8 texture
1482 * would be stored compactly), but how often
1483 * does that really happen?
1484 */
1485 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1486 break;
1487 default:;
1488 }
1489 } else if (!rtex->can_sample_s && rtex->can_sample_z) {
1490 assert(util_format_has_stencil(util_format_description(pipe_format)));
1491
1492 /* DB->CB copies to an 8bpp surface don't work. */
1493 pipe_format = PIPE_FORMAT_X24S8_UINT;
1494 }
1495 }
1496
1497 memset(&resource, 0, sizeof(resource));
1498 resource.target = texture->target;
1499 resource.format = pipe_format;
1500 resource.width0 = texture->width0;
1501 resource.height0 = texture->height0;
1502 resource.depth0 = texture->depth0;
1503 resource.array_size = texture->array_size;
1504 resource.last_level = texture->last_level;
1505 resource.nr_samples = texture->nr_samples;
1506 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1507 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1508 resource.flags = texture->flags | R600_RESOURCE_FLAG_FLUSHED_DEPTH;
1509
1510 if (staging)
1511 resource.flags |= R600_RESOURCE_FLAG_TRANSFER;
1512
1513 *flushed_depth_texture = (struct r600_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1514 if (*flushed_depth_texture == NULL) {
1515 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1516 return false;
1517 }
1518
1519 (*flushed_depth_texture)->non_disp_tiling = false;
1520 return true;
1521 }
1522
1523 /**
1524 * Initialize the pipe_resource descriptor to be of the same size as the box,
1525 * which is supposed to hold a subregion of the texture "orig" at the given
1526 * mipmap level.
1527 */
1528 static void r600_init_temp_resource_from_box(struct pipe_resource *res,
1529 struct pipe_resource *orig,
1530 const struct pipe_box *box,
1531 unsigned level, unsigned flags)
1532 {
1533 memset(res, 0, sizeof(*res));
1534 res->format = orig->format;
1535 res->width0 = box->width;
1536 res->height0 = box->height;
1537 res->depth0 = 1;
1538 res->array_size = 1;
1539 res->usage = flags & R600_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1540 res->flags = flags;
1541
1542 /* We must set the correct texture target and dimensions for a 3D box. */
1543 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1544 res->target = PIPE_TEXTURE_2D_ARRAY;
1545 res->array_size = box->depth;
1546 } else {
1547 res->target = PIPE_TEXTURE_2D;
1548 }
1549 }
1550
1551 static bool r600_can_invalidate_texture(struct r600_common_screen *rscreen,
1552 struct r600_texture *rtex,
1553 unsigned transfer_usage,
1554 const struct pipe_box *box)
1555 {
1556 /* r600g doesn't react to dirty_tex_descriptor_counter */
1557 return rscreen->chip_class >= SI &&
1558 !rtex->resource.b.is_shared &&
1559 !(transfer_usage & PIPE_TRANSFER_READ) &&
1560 rtex->resource.b.b.last_level == 0 &&
1561 util_texrange_covers_whole_level(&rtex->resource.b.b, 0,
1562 box->x, box->y, box->z,
1563 box->width, box->height,
1564 box->depth);
1565 }
1566
1567 static void r600_texture_invalidate_storage(struct r600_common_context *rctx,
1568 struct r600_texture *rtex)
1569 {
1570 struct r600_common_screen *rscreen = rctx->screen;
1571
1572 /* There is no point in discarding depth and tiled buffers. */
1573 assert(!rtex->is_depth);
1574 assert(rtex->surface.is_linear);
1575
1576 /* Reallocate the buffer in the same pipe_resource. */
1577 r600_alloc_resource(rscreen, &rtex->resource);
1578
1579 /* Initialize the CMASK base address (needed even without CMASK). */
1580 rtex->cmask.base_address_reg =
1581 (rtex->resource.gpu_address + rtex->cmask.offset) >> 8;
1582
1583 p_atomic_inc(&rscreen->dirty_tex_counter);
1584
1585 rctx->num_alloc_tex_transfer_bytes += rtex->size;
1586 }
1587
1588 static void *r600_texture_transfer_map(struct pipe_context *ctx,
1589 struct pipe_resource *texture,
1590 unsigned level,
1591 unsigned usage,
1592 const struct pipe_box *box,
1593 struct pipe_transfer **ptransfer)
1594 {
1595 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1596 struct r600_texture *rtex = (struct r600_texture*)texture;
1597 struct r600_transfer *trans;
1598 struct r600_resource *buf;
1599 unsigned offset = 0;
1600 char *map;
1601 bool use_staging_texture = false;
1602
1603 assert(!(texture->flags & R600_RESOURCE_FLAG_TRANSFER));
1604 assert(box->width && box->height && box->depth);
1605
1606 /* Depth textures use staging unconditionally. */
1607 if (!rtex->is_depth) {
1608 /* Degrade the tile mode if we get too many transfers on APUs.
1609 * On dGPUs, the staging texture is always faster.
1610 * Only count uploads that are at least 4x4 pixels large.
1611 */
1612 if (!rctx->screen->info.has_dedicated_vram &&
1613 level == 0 &&
1614 box->width >= 4 && box->height >= 4 &&
1615 p_atomic_inc_return(&rtex->num_level0_transfers) == 10) {
1616 bool can_invalidate =
1617 r600_can_invalidate_texture(rctx->screen, rtex,
1618 usage, box);
1619
1620 r600_degrade_tile_mode_to_linear(rctx, rtex,
1621 can_invalidate);
1622 }
1623
1624 /* Tiled textures need to be converted into a linear texture for CPU
1625 * access. The staging texture is always linear and is placed in GART.
1626 *
1627 * Reading from VRAM or GTT WC is slow, always use the staging
1628 * texture in this case.
1629 *
1630 * Use the staging texture for uploads if the underlying BO
1631 * is busy.
1632 */
1633 if (!rtex->surface.is_linear)
1634 use_staging_texture = true;
1635 else if (usage & PIPE_TRANSFER_READ)
1636 use_staging_texture =
1637 rtex->resource.domains & RADEON_DOMAIN_VRAM ||
1638 rtex->resource.flags & RADEON_FLAG_GTT_WC;
1639 /* Write & linear only: */
1640 else if (r600_rings_is_buffer_referenced(rctx, rtex->resource.buf,
1641 RADEON_USAGE_READWRITE) ||
1642 !rctx->ws->buffer_wait(rtex->resource.buf, 0,
1643 RADEON_USAGE_READWRITE)) {
1644 /* It's busy. */
1645 if (r600_can_invalidate_texture(rctx->screen, rtex,
1646 usage, box))
1647 r600_texture_invalidate_storage(rctx, rtex);
1648 else
1649 use_staging_texture = true;
1650 }
1651 }
1652
1653 trans = CALLOC_STRUCT(r600_transfer);
1654 if (!trans)
1655 return NULL;
1656 pipe_resource_reference(&trans->b.b.resource, texture);
1657 trans->b.b.level = level;
1658 trans->b.b.usage = usage;
1659 trans->b.b.box = *box;
1660
1661 if (rtex->is_depth) {
1662 struct r600_texture *staging_depth;
1663
1664 if (rtex->resource.b.b.nr_samples > 1) {
1665 /* MSAA depth buffers need to be converted to single sample buffers.
1666 *
1667 * Mapping MSAA depth buffers can occur if ReadPixels is called
1668 * with a multisample GLX visual.
1669 *
1670 * First downsample the depth buffer to a temporary texture,
1671 * then decompress the temporary one to staging.
1672 *
1673 * Only the region being mapped is transfered.
1674 */
1675 struct pipe_resource resource;
1676
1677 r600_init_temp_resource_from_box(&resource, texture, box, level, 0);
1678
1679 if (!r600_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1680 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1681 FREE(trans);
1682 return NULL;
1683 }
1684
1685 if (usage & PIPE_TRANSFER_READ) {
1686 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1687 if (!temp) {
1688 R600_ERR("failed to create a temporary depth texture\n");
1689 FREE(trans);
1690 return NULL;
1691 }
1692
1693 r600_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1694 rctx->blit_decompress_depth(ctx, (struct r600_texture*)temp, staging_depth,
1695 0, 0, 0, box->depth, 0, 0);
1696 pipe_resource_reference(&temp, NULL);
1697 }
1698
1699 /* Just get the strides. */
1700 r600_texture_get_offset(rctx->screen, staging_depth, level, NULL,
1701 &trans->b.b.stride,
1702 &trans->b.b.layer_stride);
1703 } else {
1704 /* XXX: only readback the rectangle which is being mapped? */
1705 /* XXX: when discard is true, no need to read back from depth texture */
1706 if (!r600_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1707 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1708 FREE(trans);
1709 return NULL;
1710 }
1711
1712 rctx->blit_decompress_depth(ctx, rtex, staging_depth,
1713 level, level,
1714 box->z, box->z + box->depth - 1,
1715 0, 0);
1716
1717 offset = r600_texture_get_offset(rctx->screen, staging_depth,
1718 level, box,
1719 &trans->b.b.stride,
1720 &trans->b.b.layer_stride);
1721 }
1722
1723 trans->staging = (struct r600_resource*)staging_depth;
1724 buf = trans->staging;
1725 } else if (use_staging_texture) {
1726 struct pipe_resource resource;
1727 struct r600_texture *staging;
1728
1729 r600_init_temp_resource_from_box(&resource, texture, box, level,
1730 R600_RESOURCE_FLAG_TRANSFER);
1731 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1732 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1733
1734 /* Create the temporary texture. */
1735 staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1736 if (!staging) {
1737 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1738 FREE(trans);
1739 return NULL;
1740 }
1741 trans->staging = &staging->resource;
1742
1743 /* Just get the strides. */
1744 r600_texture_get_offset(rctx->screen, staging, 0, NULL,
1745 &trans->b.b.stride,
1746 &trans->b.b.layer_stride);
1747
1748 if (usage & PIPE_TRANSFER_READ)
1749 r600_copy_to_staging_texture(ctx, trans);
1750 else
1751 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1752
1753 buf = trans->staging;
1754 } else {
1755 /* the resource is mapped directly */
1756 offset = r600_texture_get_offset(rctx->screen, rtex, level, box,
1757 &trans->b.b.stride,
1758 &trans->b.b.layer_stride);
1759 buf = &rtex->resource;
1760 }
1761
1762 if (!(map = r600_buffer_map_sync_with_rings(rctx, buf, usage))) {
1763 r600_resource_reference(&trans->staging, NULL);
1764 FREE(trans);
1765 return NULL;
1766 }
1767
1768 *ptransfer = &trans->b.b;
1769 return map + offset;
1770 }
1771
1772 static void r600_texture_transfer_unmap(struct pipe_context *ctx,
1773 struct pipe_transfer* transfer)
1774 {
1775 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
1776 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
1777 struct pipe_resource *texture = transfer->resource;
1778 struct r600_texture *rtex = (struct r600_texture*)texture;
1779
1780 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtransfer->staging) {
1781 if (rtex->is_depth && rtex->resource.b.b.nr_samples <= 1) {
1782 ctx->resource_copy_region(ctx, texture, transfer->level,
1783 transfer->box.x, transfer->box.y, transfer->box.z,
1784 &rtransfer->staging->b.b, transfer->level,
1785 &transfer->box);
1786 } else {
1787 r600_copy_from_staging_texture(ctx, rtransfer);
1788 }
1789 }
1790
1791 if (rtransfer->staging) {
1792 rctx->num_alloc_tex_transfer_bytes += rtransfer->staging->buf->size;
1793 r600_resource_reference(&rtransfer->staging, NULL);
1794 }
1795
1796 /* Heuristic for {upload, draw, upload, draw, ..}:
1797 *
1798 * Flush the gfx IB if we've allocated too much texture storage.
1799 *
1800 * The idea is that we don't want to build IBs that use too much
1801 * memory and put pressure on the kernel memory manager and we also
1802 * want to make temporary and invalidated buffers go idle ASAP to
1803 * decrease the total memory usage or make them reusable. The memory
1804 * usage will be slightly higher than given here because of the buffer
1805 * cache in the winsys.
1806 *
1807 * The result is that the kernel memory manager is never a bottleneck.
1808 */
1809 if (rctx->num_alloc_tex_transfer_bytes > rctx->screen->info.gart_size / 4) {
1810 rctx->gfx.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
1811 rctx->num_alloc_tex_transfer_bytes = 0;
1812 }
1813
1814 pipe_resource_reference(&transfer->resource, NULL);
1815 FREE(transfer);
1816 }
1817
1818 static const struct u_resource_vtbl r600_texture_vtbl =
1819 {
1820 NULL, /* get_handle */
1821 r600_texture_destroy, /* resource_destroy */
1822 r600_texture_transfer_map, /* transfer_map */
1823 u_default_transfer_flush_region, /* transfer_flush_region */
1824 r600_texture_transfer_unmap, /* transfer_unmap */
1825 };
1826
1827 /* DCC channel type categories within which formats can be reinterpreted
1828 * while keeping the same DCC encoding. The swizzle must also match. */
1829 enum dcc_channel_type {
1830 dcc_channel_float32,
1831 dcc_channel_uint32,
1832 dcc_channel_sint32,
1833 dcc_channel_float16,
1834 dcc_channel_uint16,
1835 dcc_channel_sint16,
1836 dcc_channel_uint_10_10_10_2,
1837 dcc_channel_uint8,
1838 dcc_channel_sint8,
1839 dcc_channel_incompatible,
1840 };
1841
1842 /* Return the type of DCC encoding. */
1843 static enum dcc_channel_type
1844 vi_get_dcc_channel_type(const struct util_format_description *desc)
1845 {
1846 int i;
1847
1848 /* Find the first non-void channel. */
1849 for (i = 0; i < desc->nr_channels; i++)
1850 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1851 break;
1852 if (i == desc->nr_channels)
1853 return dcc_channel_incompatible;
1854
1855 switch (desc->channel[i].size) {
1856 case 32:
1857 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1858 return dcc_channel_float32;
1859 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1860 return dcc_channel_uint32;
1861 return dcc_channel_sint32;
1862 case 16:
1863 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1864 return dcc_channel_float16;
1865 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1866 return dcc_channel_uint16;
1867 return dcc_channel_sint16;
1868 case 10:
1869 return dcc_channel_uint_10_10_10_2;
1870 case 8:
1871 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1872 return dcc_channel_uint8;
1873 return dcc_channel_sint8;
1874 default:
1875 return dcc_channel_incompatible;
1876 }
1877 }
1878
1879 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1880 bool vi_dcc_formats_compatible(enum pipe_format format1,
1881 enum pipe_format format2)
1882 {
1883 const struct util_format_description *desc1, *desc2;
1884 enum dcc_channel_type type1, type2;
1885 int i;
1886
1887 if (format1 == format2)
1888 return true;
1889
1890 desc1 = util_format_description(format1);
1891 desc2 = util_format_description(format2);
1892
1893 if (desc1->nr_channels != desc2->nr_channels)
1894 return false;
1895
1896 /* Swizzles must be the same. */
1897 for (i = 0; i < desc1->nr_channels; i++)
1898 if (desc1->swizzle[i] <= PIPE_SWIZZLE_W &&
1899 desc2->swizzle[i] <= PIPE_SWIZZLE_W &&
1900 desc1->swizzle[i] != desc2->swizzle[i])
1901 return false;
1902
1903 type1 = vi_get_dcc_channel_type(desc1);
1904 type2 = vi_get_dcc_channel_type(desc2);
1905
1906 return type1 != dcc_channel_incompatible &&
1907 type2 != dcc_channel_incompatible &&
1908 type1 == type2;
1909 }
1910
1911 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex,
1912 unsigned level,
1913 enum pipe_format view_format)
1914 {
1915 struct r600_texture *rtex = (struct r600_texture *)tex;
1916
1917 return vi_dcc_enabled(rtex, level) &&
1918 !vi_dcc_formats_compatible(tex->format, view_format);
1919 }
1920
1921 /* This can't be merged with the above function, because
1922 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1923 void vi_disable_dcc_if_incompatible_format(struct r600_common_context *rctx,
1924 struct pipe_resource *tex,
1925 unsigned level,
1926 enum pipe_format view_format)
1927 {
1928 struct r600_texture *rtex = (struct r600_texture *)tex;
1929
1930 if (vi_dcc_enabled(rtex, level) &&
1931 !vi_dcc_formats_compatible(tex->format, view_format))
1932 if (!r600_texture_disable_dcc(rctx, (struct r600_texture*)tex))
1933 rctx->decompress_dcc(&rctx->b, rtex);
1934 }
1935
1936 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1937 struct pipe_resource *texture,
1938 const struct pipe_surface *templ,
1939 unsigned width0, unsigned height0,
1940 unsigned width, unsigned height)
1941 {
1942 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1943
1944 if (!surface)
1945 return NULL;
1946
1947 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1948 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1949
1950 pipe_reference_init(&surface->base.reference, 1);
1951 pipe_resource_reference(&surface->base.texture, texture);
1952 surface->base.context = pipe;
1953 surface->base.format = templ->format;
1954 surface->base.width = width;
1955 surface->base.height = height;
1956 surface->base.u = templ->u;
1957
1958 surface->width0 = width0;
1959 surface->height0 = height0;
1960
1961 surface->dcc_incompatible =
1962 texture->target != PIPE_BUFFER &&
1963 vi_dcc_formats_are_incompatible(texture, templ->u.tex.level,
1964 templ->format);
1965 return &surface->base;
1966 }
1967
1968 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1969 struct pipe_resource *tex,
1970 const struct pipe_surface *templ)
1971 {
1972 unsigned level = templ->u.tex.level;
1973 unsigned width = u_minify(tex->width0, level);
1974 unsigned height = u_minify(tex->height0, level);
1975 unsigned width0 = tex->width0;
1976 unsigned height0 = tex->height0;
1977
1978 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1979 const struct util_format_description *tex_desc
1980 = util_format_description(tex->format);
1981 const struct util_format_description *templ_desc
1982 = util_format_description(templ->format);
1983
1984 assert(tex_desc->block.bits == templ_desc->block.bits);
1985
1986 /* Adjust size of surface if and only if the block width or
1987 * height is changed. */
1988 if (tex_desc->block.width != templ_desc->block.width ||
1989 tex_desc->block.height != templ_desc->block.height) {
1990 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
1991 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
1992
1993 width = nblks_x * templ_desc->block.width;
1994 height = nblks_y * templ_desc->block.height;
1995
1996 width0 = util_format_get_nblocksx(tex->format, width0);
1997 height0 = util_format_get_nblocksy(tex->format, height0);
1998 }
1999 }
2000
2001 return r600_create_surface_custom(pipe, tex, templ,
2002 width0, height0,
2003 width, height);
2004 }
2005
2006 static void r600_surface_destroy(struct pipe_context *pipe,
2007 struct pipe_surface *surface)
2008 {
2009 struct r600_surface *surf = (struct r600_surface*)surface;
2010 r600_resource_reference(&surf->cb_buffer_fmask, NULL);
2011 r600_resource_reference(&surf->cb_buffer_cmask, NULL);
2012 pipe_resource_reference(&surface->texture, NULL);
2013 FREE(surface);
2014 }
2015
2016 static void r600_clear_texture(struct pipe_context *pipe,
2017 struct pipe_resource *tex,
2018 unsigned level,
2019 const struct pipe_box *box,
2020 const void *data)
2021 {
2022 struct pipe_screen *screen = pipe->screen;
2023 struct r600_texture *rtex = (struct r600_texture*)tex;
2024 struct pipe_surface tmpl = {{0}};
2025 struct pipe_surface *sf;
2026 const struct util_format_description *desc =
2027 util_format_description(tex->format);
2028
2029 tmpl.format = tex->format;
2030 tmpl.u.tex.first_layer = box->z;
2031 tmpl.u.tex.last_layer = box->z + box->depth - 1;
2032 tmpl.u.tex.level = level;
2033 sf = pipe->create_surface(pipe, tex, &tmpl);
2034 if (!sf)
2035 return;
2036
2037 if (rtex->is_depth) {
2038 unsigned clear;
2039 float depth;
2040 uint8_t stencil = 0;
2041
2042 /* Depth is always present. */
2043 clear = PIPE_CLEAR_DEPTH;
2044 desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
2045
2046 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
2047 clear |= PIPE_CLEAR_STENCIL;
2048 desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
2049 }
2050
2051 pipe->clear_depth_stencil(pipe, sf, clear, depth, stencil,
2052 box->x, box->y,
2053 box->width, box->height, false);
2054 } else {
2055 union pipe_color_union color;
2056
2057 /* pipe_color_union requires the full vec4 representation. */
2058 if (util_format_is_pure_uint(tex->format))
2059 desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
2060 else if (util_format_is_pure_sint(tex->format))
2061 desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
2062 else
2063 desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
2064
2065 if (screen->is_format_supported(screen, tex->format,
2066 tex->target, 0,
2067 PIPE_BIND_RENDER_TARGET)) {
2068 pipe->clear_render_target(pipe, sf, &color,
2069 box->x, box->y,
2070 box->width, box->height, false);
2071 } else {
2072 /* Software fallback - just for R9G9B9E5_FLOAT */
2073 util_clear_render_target(pipe, sf, &color,
2074 box->x, box->y,
2075 box->width, box->height);
2076 }
2077 }
2078 pipe_surface_reference(&sf, NULL);
2079 }
2080
2081 unsigned r600_translate_colorswap(enum pipe_format format, bool do_endian_swap)
2082 {
2083 const struct util_format_description *desc = util_format_description(format);
2084
2085 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2086
2087 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2088 return V_0280A0_SWAP_STD;
2089
2090 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
2091 return ~0U;
2092
2093 switch (desc->nr_channels) {
2094 case 1:
2095 if (HAS_SWIZZLE(0,X))
2096 return V_0280A0_SWAP_STD; /* X___ */
2097 else if (HAS_SWIZZLE(3,X))
2098 return V_0280A0_SWAP_ALT_REV; /* ___X */
2099 break;
2100 case 2:
2101 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
2102 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
2103 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
2104 return V_0280A0_SWAP_STD; /* XY__ */
2105 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
2106 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
2107 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
2108 /* YX__ */
2109 return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
2110 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
2111 return V_0280A0_SWAP_ALT; /* X__Y */
2112 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
2113 return V_0280A0_SWAP_ALT_REV; /* Y__X */
2114 break;
2115 case 3:
2116 if (HAS_SWIZZLE(0,X))
2117 return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
2118 else if (HAS_SWIZZLE(0,Z))
2119 return V_0280A0_SWAP_STD_REV; /* ZYX */
2120 break;
2121 case 4:
2122 /* check the middle channels, the 1st and 4th channel can be NONE */
2123 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
2124 return V_0280A0_SWAP_STD; /* XYZW */
2125 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
2126 return V_0280A0_SWAP_STD_REV; /* WZYX */
2127 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
2128 return V_0280A0_SWAP_ALT; /* ZYXW */
2129 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
2130 /* YZWX */
2131 if (desc->is_array)
2132 return V_0280A0_SWAP_ALT_REV;
2133 else
2134 return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
2135 }
2136 break;
2137 }
2138 return ~0U;
2139 }
2140
2141 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2142
2143 static void vi_dcc_clean_up_context_slot(struct r600_common_context *rctx,
2144 int slot)
2145 {
2146 int i;
2147
2148 if (rctx->dcc_stats[slot].query_active)
2149 vi_separate_dcc_stop_query(&rctx->b,
2150 rctx->dcc_stats[slot].tex);
2151
2152 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats[slot].ps_stats); i++)
2153 if (rctx->dcc_stats[slot].ps_stats[i]) {
2154 rctx->b.destroy_query(&rctx->b,
2155 rctx->dcc_stats[slot].ps_stats[i]);
2156 rctx->dcc_stats[slot].ps_stats[i] = NULL;
2157 }
2158
2159 r600_texture_reference(&rctx->dcc_stats[slot].tex, NULL);
2160 }
2161
2162 /**
2163 * Return the per-context slot where DCC statistics queries for the texture live.
2164 */
2165 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context *rctx,
2166 struct r600_texture *tex)
2167 {
2168 int i, empty_slot = -1;
2169
2170 /* Remove zombie textures (textures kept alive by this array only). */
2171 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2172 if (rctx->dcc_stats[i].tex &&
2173 rctx->dcc_stats[i].tex->resource.b.b.reference.count == 1)
2174 vi_dcc_clean_up_context_slot(rctx, i);
2175
2176 /* Find the texture. */
2177 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++) {
2178 /* Return if found. */
2179 if (rctx->dcc_stats[i].tex == tex) {
2180 rctx->dcc_stats[i].last_use_timestamp = os_time_get();
2181 return i;
2182 }
2183
2184 /* Record the first seen empty slot. */
2185 if (empty_slot == -1 && !rctx->dcc_stats[i].tex)
2186 empty_slot = i;
2187 }
2188
2189 /* Not found. Remove the oldest member to make space in the array. */
2190 if (empty_slot == -1) {
2191 int oldest_slot = 0;
2192
2193 /* Find the oldest slot. */
2194 for (i = 1; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2195 if (rctx->dcc_stats[oldest_slot].last_use_timestamp >
2196 rctx->dcc_stats[i].last_use_timestamp)
2197 oldest_slot = i;
2198
2199 /* Clean up the oldest slot. */
2200 vi_dcc_clean_up_context_slot(rctx, oldest_slot);
2201 empty_slot = oldest_slot;
2202 }
2203
2204 /* Add the texture to the new slot. */
2205 r600_texture_reference(&rctx->dcc_stats[empty_slot].tex, tex);
2206 rctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2207 return empty_slot;
2208 }
2209
2210 static struct pipe_query *
2211 vi_create_resuming_pipestats_query(struct pipe_context *ctx)
2212 {
2213 struct r600_query_hw *query = (struct r600_query_hw*)
2214 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2215
2216 query->flags |= R600_QUERY_HW_FLAG_BEGIN_RESUMES;
2217 return (struct pipe_query*)query;
2218 }
2219
2220 /**
2221 * Called when binding a color buffer.
2222 */
2223 void vi_separate_dcc_start_query(struct pipe_context *ctx,
2224 struct r600_texture *tex)
2225 {
2226 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2227 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2228
2229 assert(!rctx->dcc_stats[i].query_active);
2230
2231 if (!rctx->dcc_stats[i].ps_stats[0])
2232 rctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(ctx);
2233
2234 /* begin or resume the query */
2235 ctx->begin_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2236 rctx->dcc_stats[i].query_active = true;
2237 }
2238
2239 /**
2240 * Called when unbinding a color buffer.
2241 */
2242 void vi_separate_dcc_stop_query(struct pipe_context *ctx,
2243 struct r600_texture *tex)
2244 {
2245 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2246 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2247
2248 assert(rctx->dcc_stats[i].query_active);
2249 assert(rctx->dcc_stats[i].ps_stats[0]);
2250
2251 /* pause or end the query */
2252 ctx->end_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2253 rctx->dcc_stats[i].query_active = false;
2254 }
2255
2256 static bool vi_should_enable_separate_dcc(struct r600_texture *tex)
2257 {
2258 /* The minimum number of fullscreen draws per frame that is required
2259 * to enable DCC. */
2260 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2261 }
2262
2263 /* Called by fast clear. */
2264 static void vi_separate_dcc_try_enable(struct r600_common_context *rctx,
2265 struct r600_texture *tex)
2266 {
2267 /* The intent is to use this with shared displayable back buffers,
2268 * but it's not strictly limited only to them.
2269 */
2270 if (!tex->resource.b.is_shared ||
2271 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2272 tex->resource.b.b.target != PIPE_TEXTURE_2D ||
2273 tex->resource.b.b.last_level > 0 ||
2274 !tex->surface.dcc_size)
2275 return;
2276
2277 if (tex->dcc_offset)
2278 return; /* already enabled */
2279
2280 /* Enable the DCC stat gathering. */
2281 if (!tex->dcc_gather_statistics) {
2282 tex->dcc_gather_statistics = true;
2283 vi_separate_dcc_start_query(&rctx->b, tex);
2284 }
2285
2286 if (!vi_should_enable_separate_dcc(tex))
2287 return; /* stats show that DCC decompression is too expensive */
2288
2289 assert(tex->surface.num_dcc_levels);
2290 assert(!tex->dcc_separate_buffer);
2291
2292 r600_texture_discard_cmask(rctx->screen, tex);
2293
2294 /* Get a DCC buffer. */
2295 if (tex->last_dcc_separate_buffer) {
2296 assert(tex->dcc_gather_statistics);
2297 assert(!tex->dcc_separate_buffer);
2298 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2299 tex->last_dcc_separate_buffer = NULL;
2300 } else {
2301 tex->dcc_separate_buffer = (struct r600_resource*)
2302 r600_aligned_buffer_create(rctx->b.screen,
2303 R600_RESOURCE_FLAG_UNMAPPABLE,
2304 PIPE_USAGE_DEFAULT,
2305 tex->surface.dcc_size,
2306 tex->surface.dcc_alignment);
2307 if (!tex->dcc_separate_buffer)
2308 return;
2309 }
2310
2311 /* dcc_offset is the absolute GPUVM address. */
2312 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
2313
2314 /* no need to flag anything since this is called by fast clear that
2315 * flags framebuffer state
2316 */
2317 }
2318
2319 /**
2320 * Called by pipe_context::flush_resource, the place where DCC decompression
2321 * takes place.
2322 */
2323 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
2324 struct r600_texture *tex)
2325 {
2326 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2327 struct pipe_query *tmp;
2328 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2329 bool query_active = rctx->dcc_stats[i].query_active;
2330 bool disable = false;
2331
2332 if (rctx->dcc_stats[i].ps_stats[2]) {
2333 union pipe_query_result result;
2334
2335 /* Read the results. */
2336 ctx->get_query_result(ctx, rctx->dcc_stats[i].ps_stats[2],
2337 true, &result);
2338 r600_query_hw_reset_buffers(rctx,
2339 (struct r600_query_hw*)
2340 rctx->dcc_stats[i].ps_stats[2]);
2341
2342 /* Compute the approximate number of fullscreen draws. */
2343 tex->ps_draw_ratio =
2344 result.pipeline_statistics.ps_invocations /
2345 (tex->resource.b.b.width0 * tex->resource.b.b.height0);
2346 rctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2347
2348 disable = tex->dcc_separate_buffer &&
2349 !vi_should_enable_separate_dcc(tex);
2350 }
2351
2352 tex->num_slow_clears = 0;
2353
2354 /* stop the statistics query for ps_stats[0] */
2355 if (query_active)
2356 vi_separate_dcc_stop_query(ctx, tex);
2357
2358 /* Move the queries in the queue by one. */
2359 tmp = rctx->dcc_stats[i].ps_stats[2];
2360 rctx->dcc_stats[i].ps_stats[2] = rctx->dcc_stats[i].ps_stats[1];
2361 rctx->dcc_stats[i].ps_stats[1] = rctx->dcc_stats[i].ps_stats[0];
2362 rctx->dcc_stats[i].ps_stats[0] = tmp;
2363
2364 /* create and start a new query as ps_stats[0] */
2365 if (query_active)
2366 vi_separate_dcc_start_query(ctx, tex);
2367
2368 if (disable) {
2369 assert(!tex->last_dcc_separate_buffer);
2370 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2371 tex->dcc_separate_buffer = NULL;
2372 tex->dcc_offset = 0;
2373 /* no need to flag anything since this is called after
2374 * decompression that re-sets framebuffer state
2375 */
2376 }
2377 }
2378
2379 /* FAST COLOR CLEAR */
2380
2381 static void evergreen_set_clear_color(struct r600_texture *rtex,
2382 enum pipe_format surface_format,
2383 const union pipe_color_union *color)
2384 {
2385 union util_color uc;
2386
2387 memset(&uc, 0, sizeof(uc));
2388
2389 if (rtex->surface.bpe == 16) {
2390 /* DCC fast clear only:
2391 * CLEAR_WORD0 = R = G = B
2392 * CLEAR_WORD1 = A
2393 */
2394 assert(color->ui[0] == color->ui[1] &&
2395 color->ui[0] == color->ui[2]);
2396 uc.ui[0] = color->ui[0];
2397 uc.ui[1] = color->ui[3];
2398 } else if (util_format_is_pure_uint(surface_format)) {
2399 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
2400 } else if (util_format_is_pure_sint(surface_format)) {
2401 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
2402 } else {
2403 util_pack_color(color->f, surface_format, &uc);
2404 }
2405
2406 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
2407 }
2408
2409 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format,
2410 const union pipe_color_union *color,
2411 uint32_t* reset_value,
2412 bool* clear_words_needed)
2413 {
2414 bool values[4] = {};
2415 int i;
2416 bool main_value = false;
2417 bool extra_value = false;
2418 int extra_channel;
2419
2420 /* This is needed to get the correct DCC clear value for luminance formats.
2421 * 1) Get the linear format (because the next step can't handle L8_SRGB).
2422 * 2) Convert luminance to red. (the real hw format for luminance)
2423 */
2424 surface_format = util_format_linear(surface_format);
2425 surface_format = util_format_luminance_to_red(surface_format);
2426
2427 const struct util_format_description *desc = util_format_description(surface_format);
2428
2429 if (desc->block.bits == 128 &&
2430 (color->ui[0] != color->ui[1] ||
2431 color->ui[0] != color->ui[2]))
2432 return false;
2433
2434 *clear_words_needed = true;
2435 *reset_value = 0x20202020U;
2436
2437 /* If we want to clear without needing a fast clear eliminate step, we
2438 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2439 * have two sets of flags, one for the last or first channel(extra) and
2440 * one for the other channels(main).
2441 */
2442
2443 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
2444 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
2445 surface_format == PIPE_FORMAT_B5G6R5_SRGB ||
2446 util_format_is_alpha(surface_format)) {
2447 extra_channel = -1;
2448 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
2449 if(r600_translate_colorswap(surface_format, false) <= 1)
2450 extra_channel = desc->nr_channels - 1;
2451 else
2452 extra_channel = 0;
2453 } else
2454 return true;
2455
2456 for (i = 0; i < 4; ++i) {
2457 int index = desc->swizzle[i] - PIPE_SWIZZLE_X;
2458
2459 if (desc->swizzle[i] < PIPE_SWIZZLE_X ||
2460 desc->swizzle[i] > PIPE_SWIZZLE_W)
2461 continue;
2462
2463 if (desc->channel[i].pure_integer &&
2464 desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2465 /* Use the maximum value for clamping the clear color. */
2466 int max = u_bit_consecutive(0, desc->channel[i].size - 1);
2467
2468 values[i] = color->i[i] != 0;
2469 if (color->i[i] != 0 && MIN2(color->i[i], max) != max)
2470 return true;
2471 } else if (desc->channel[i].pure_integer &&
2472 desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2473 /* Use the maximum value for clamping the clear color. */
2474 unsigned max = u_bit_consecutive(0, desc->channel[i].size);
2475
2476 values[i] = color->ui[i] != 0U;
2477 if (color->ui[i] != 0U && MIN2(color->ui[i], max) != max)
2478 return true;
2479 } else {
2480 values[i] = color->f[i] != 0.0F;
2481 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
2482 return true;
2483 }
2484
2485 if (index == extra_channel)
2486 extra_value = values[i];
2487 else
2488 main_value = values[i];
2489 }
2490
2491 for (int i = 0; i < 4; ++i)
2492 if (values[i] != main_value &&
2493 desc->swizzle[i] - PIPE_SWIZZLE_X != extra_channel &&
2494 desc->swizzle[i] >= PIPE_SWIZZLE_X &&
2495 desc->swizzle[i] <= PIPE_SWIZZLE_W)
2496 return true;
2497
2498 *clear_words_needed = false;
2499 if (main_value)
2500 *reset_value |= 0x80808080U;
2501
2502 if (extra_value)
2503 *reset_value |= 0x40404040U;
2504 return true;
2505 }
2506
2507 void vi_dcc_clear_level(struct r600_common_context *rctx,
2508 struct r600_texture *rtex,
2509 unsigned level, unsigned clear_value)
2510 {
2511 struct pipe_resource *dcc_buffer;
2512 uint64_t dcc_offset, clear_size;
2513
2514 assert(vi_dcc_enabled(rtex, level));
2515
2516 if (rtex->dcc_separate_buffer) {
2517 dcc_buffer = &rtex->dcc_separate_buffer->b.b;
2518 dcc_offset = 0;
2519 } else {
2520 dcc_buffer = &rtex->resource.b.b;
2521 dcc_offset = rtex->dcc_offset;
2522 }
2523
2524 if (rctx->chip_class >= GFX9) {
2525 /* Mipmap level clears aren't implemented. */
2526 assert(rtex->resource.b.b.last_level == 0);
2527 /* MSAA needs a different clear size. */
2528 assert(rtex->resource.b.b.nr_samples <= 1);
2529 clear_size = rtex->surface.dcc_size;
2530 } else {
2531 dcc_offset += rtex->surface.u.legacy.level[level].dcc_offset;
2532 clear_size = rtex->surface.u.legacy.level[level].dcc_fast_clear_size;
2533 }
2534
2535 rctx->clear_buffer(&rctx->b, dcc_buffer, dcc_offset, clear_size,
2536 clear_value, R600_COHERENCY_CB_META);
2537 }
2538
2539 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2540 * This allows hitting the MSAA resolve fast path, which requires that both
2541 * src and dst micro tile modes match.
2542 */
2543 static void si_set_optimal_micro_tile_mode(struct r600_common_screen *rscreen,
2544 struct r600_texture *rtex)
2545 {
2546 if (rtex->resource.b.is_shared ||
2547 rtex->resource.b.b.nr_samples <= 1 ||
2548 rtex->surface.micro_tile_mode == rtex->last_msaa_resolve_target_micro_mode)
2549 return;
2550
2551 assert(rscreen->chip_class >= GFX9 ||
2552 rtex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_2D);
2553 assert(rtex->resource.b.b.last_level == 0);
2554
2555 if (rscreen->chip_class >= GFX9) {
2556 /* 4K or larger tiles only. 0 is linear. 1-3 are 256B tiles. */
2557 assert(rtex->surface.u.gfx9.surf.swizzle_mode >= 4);
2558
2559 /* If you do swizzle_mode % 4, you'll get:
2560 * 0 = Depth
2561 * 1 = Standard,
2562 * 2 = Displayable
2563 * 3 = Rotated
2564 *
2565 * Depth-sample order isn't allowed:
2566 */
2567 assert(rtex->surface.u.gfx9.surf.swizzle_mode % 4 != 0);
2568
2569 switch (rtex->last_msaa_resolve_target_micro_mode) {
2570 case RADEON_MICRO_MODE_DISPLAY:
2571 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2572 rtex->surface.u.gfx9.surf.swizzle_mode += 2; /* D */
2573 break;
2574 case RADEON_MICRO_MODE_THIN:
2575 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2576 rtex->surface.u.gfx9.surf.swizzle_mode += 1; /* S */
2577 break;
2578 case RADEON_MICRO_MODE_ROTATED:
2579 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2580 rtex->surface.u.gfx9.surf.swizzle_mode += 3; /* R */
2581 break;
2582 default: /* depth */
2583 assert(!"unexpected micro mode");
2584 return;
2585 }
2586 } else if (rscreen->chip_class >= CIK) {
2587 /* These magic numbers were copied from addrlib. It doesn't use
2588 * any definitions for them either. They are all 2D_TILED_THIN1
2589 * modes with different bpp and micro tile mode.
2590 */
2591 switch (rtex->last_msaa_resolve_target_micro_mode) {
2592 case RADEON_MICRO_MODE_DISPLAY:
2593 rtex->surface.u.legacy.tiling_index[0] = 10;
2594 break;
2595 case RADEON_MICRO_MODE_THIN:
2596 rtex->surface.u.legacy.tiling_index[0] = 14;
2597 break;
2598 case RADEON_MICRO_MODE_ROTATED:
2599 rtex->surface.u.legacy.tiling_index[0] = 28;
2600 break;
2601 default: /* depth, thick */
2602 assert(!"unexpected micro mode");
2603 return;
2604 }
2605 } else { /* SI */
2606 switch (rtex->last_msaa_resolve_target_micro_mode) {
2607 case RADEON_MICRO_MODE_DISPLAY:
2608 switch (rtex->surface.bpe) {
2609 case 1:
2610 rtex->surface.u.legacy.tiling_index[0] = 10;
2611 break;
2612 case 2:
2613 rtex->surface.u.legacy.tiling_index[0] = 11;
2614 break;
2615 default: /* 4, 8 */
2616 rtex->surface.u.legacy.tiling_index[0] = 12;
2617 break;
2618 }
2619 break;
2620 case RADEON_MICRO_MODE_THIN:
2621 switch (rtex->surface.bpe) {
2622 case 1:
2623 rtex->surface.u.legacy.tiling_index[0] = 14;
2624 break;
2625 case 2:
2626 rtex->surface.u.legacy.tiling_index[0] = 15;
2627 break;
2628 case 4:
2629 rtex->surface.u.legacy.tiling_index[0] = 16;
2630 break;
2631 default: /* 8, 16 */
2632 rtex->surface.u.legacy.tiling_index[0] = 17;
2633 break;
2634 }
2635 break;
2636 default: /* depth, thick */
2637 assert(!"unexpected micro mode");
2638 return;
2639 }
2640 }
2641
2642 rtex->surface.micro_tile_mode = rtex->last_msaa_resolve_target_micro_mode;
2643
2644 p_atomic_inc(&rscreen->dirty_tex_counter);
2645 }
2646
2647 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
2648 struct pipe_framebuffer_state *fb,
2649 struct r600_atom *fb_state,
2650 unsigned *buffers, ubyte *dirty_cbufs,
2651 const union pipe_color_union *color)
2652 {
2653 int i;
2654
2655 /* This function is broken in BE, so just disable this path for now */
2656 #ifdef PIPE_ARCH_BIG_ENDIAN
2657 return;
2658 #endif
2659
2660 if (rctx->render_cond)
2661 return;
2662
2663 for (i = 0; i < fb->nr_cbufs; i++) {
2664 struct r600_texture *tex;
2665 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
2666
2667 if (!fb->cbufs[i])
2668 continue;
2669
2670 /* if this colorbuffer is not being cleared */
2671 if (!(*buffers & clear_bit))
2672 continue;
2673
2674 tex = (struct r600_texture *)fb->cbufs[i]->texture;
2675
2676 /* the clear is allowed if all layers are bound */
2677 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
2678 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
2679 continue;
2680 }
2681
2682 /* cannot clear mipmapped textures */
2683 if (fb->cbufs[i]->texture->last_level != 0) {
2684 continue;
2685 }
2686
2687 /* only supported on tiled surfaces */
2688 if (tex->surface.is_linear) {
2689 continue;
2690 }
2691
2692 /* shared textures can't use fast clear without an explicit flush,
2693 * because there is no way to communicate the clear color among
2694 * all clients
2695 */
2696 if (tex->resource.b.is_shared &&
2697 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
2698 continue;
2699
2700 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2701 if (rctx->chip_class == CIK &&
2702 tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
2703 rctx->screen->info.drm_major == 2 &&
2704 rctx->screen->info.drm_minor < 38) {
2705 continue;
2706 }
2707
2708 /* Fast clear is the most appropriate place to enable DCC for
2709 * displayable surfaces.
2710 */
2711 if (rctx->chip_class >= VI &&
2712 !(rctx->screen->debug_flags & DBG_NO_DCC_FB)) {
2713 vi_separate_dcc_try_enable(rctx, tex);
2714
2715 /* RB+ isn't supported with a CMASK clear only on Stoney,
2716 * so all clears are considered to be hypothetically slow
2717 * clears, which is weighed when determining whether to
2718 * enable separate DCC.
2719 */
2720 if (tex->dcc_gather_statistics &&
2721 rctx->family == CHIP_STONEY)
2722 tex->num_slow_clears++;
2723 }
2724
2725 /* Try to clear DCC first, otherwise try CMASK. */
2726 if (vi_dcc_enabled(tex, 0)) {
2727 uint32_t reset_value;
2728 bool clear_words_needed;
2729
2730 if (rctx->screen->debug_flags & DBG_NO_DCC_CLEAR)
2731 continue;
2732
2733 if (!vi_get_fast_clear_parameters(fb->cbufs[i]->format,
2734 color, &reset_value,
2735 &clear_words_needed))
2736 continue;
2737
2738 vi_dcc_clear_level(rctx, tex, 0, reset_value);
2739
2740 unsigned level_bit = 1 << fb->cbufs[i]->u.tex.level;
2741 if (clear_words_needed) {
2742 bool need_compressed_update = !tex->dirty_level_mask;
2743
2744 tex->dirty_level_mask |= level_bit;
2745
2746 if (need_compressed_update)
2747 p_atomic_inc(&rctx->screen->compressed_colortex_counter);
2748 }
2749 tex->separate_dcc_dirty = true;
2750 } else {
2751 /* 128-bit formats are unusupported */
2752 if (tex->surface.bpe > 8) {
2753 continue;
2754 }
2755
2756 /* RB+ doesn't work with CMASK fast clear on Stoney. */
2757 if (rctx->family == CHIP_STONEY)
2758 continue;
2759
2760 /* ensure CMASK is enabled */
2761 r600_texture_alloc_cmask_separate(rctx->screen, tex);
2762 if (tex->cmask.size == 0) {
2763 continue;
2764 }
2765
2766 /* Do the fast clear. */
2767 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
2768 tex->cmask.offset, tex->cmask.size, 0,
2769 R600_COHERENCY_CB_META);
2770
2771 bool need_compressed_update = !tex->dirty_level_mask;
2772
2773 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2774
2775 if (need_compressed_update)
2776 p_atomic_inc(&rctx->screen->compressed_colortex_counter);
2777 }
2778
2779 /* We can change the micro tile mode before a full clear. */
2780 if (rctx->screen->chip_class >= SI)
2781 si_set_optimal_micro_tile_mode(rctx->screen, tex);
2782
2783 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
2784
2785 if (dirty_cbufs)
2786 *dirty_cbufs |= 1 << i;
2787 rctx->set_atom_dirty(rctx, fb_state, true);
2788 *buffers &= ~clear_bit;
2789 }
2790 }
2791
2792 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
2793 {
2794 rscreen->b.resource_from_handle = r600_texture_from_handle;
2795 rscreen->b.resource_get_handle = r600_texture_get_handle;
2796 }
2797
2798 void r600_init_context_texture_functions(struct r600_common_context *rctx)
2799 {
2800 rctx->b.create_surface = r600_create_surface;
2801 rctx->b.surface_destroy = r600_surface_destroy;
2802 rctx->b.clear_texture = r600_clear_texture;
2803 }