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