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