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