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