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