radeonsi/gfx9: disable features that don't work
[mesa.git] / src / gallium / drivers / radeon / r600_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include "r600_pipe_common.h"
28 #include "r600_cs.h"
29 #include "r600_query.h"
30 #include "util/u_format.h"
31 #include "util/u_memory.h"
32 #include "util/u_pack_color.h"
33 #include "util/u_surface.h"
34 #include "os/os_time.h"
35 #include <errno.h>
36 #include <inttypes.h>
37
38 static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
39 struct r600_texture *rtex);
40 static enum radeon_surf_mode
41 r600_choose_tiling(struct r600_common_screen *rscreen,
42 const struct pipe_resource *templ);
43
44
45 bool r600_prepare_for_dma_blit(struct r600_common_context *rctx,
46 struct r600_texture *rdst,
47 unsigned dst_level, unsigned dstx,
48 unsigned dsty, unsigned dstz,
49 struct r600_texture *rsrc,
50 unsigned src_level,
51 const struct pipe_box *src_box)
52 {
53 if (!rctx->dma.cs)
54 return false;
55
56 if (rdst->surface.bpe != rsrc->surface.bpe)
57 return false;
58
59 /* MSAA: Blits don't exist in the real world. */
60 if (rsrc->resource.b.b.nr_samples > 1 ||
61 rdst->resource.b.b.nr_samples > 1)
62 return false;
63
64 /* Depth-stencil surfaces:
65 * When dst is linear, the DB->CB copy preserves HTILE.
66 * When dst is tiled, the 3D path must be used to update HTILE.
67 */
68 if (rsrc->is_depth || rdst->is_depth)
69 return false;
70
71 /* DCC as:
72 * src: Use the 3D path. DCC decompression is expensive.
73 * dst: Use the 3D path to compress the pixels with DCC.
74 */
75 if ((rsrc->dcc_offset && src_level < rsrc->surface.num_dcc_levels) ||
76 (rdst->dcc_offset && dst_level < rdst->surface.num_dcc_levels))
77 return false;
78
79 /* CMASK as:
80 * src: Both texture and SDMA paths need decompression. Use SDMA.
81 * dst: If overwriting the whole texture, discard CMASK and use
82 * SDMA. Otherwise, use the 3D path.
83 */
84 if (rdst->cmask.size && rdst->dirty_level_mask & (1 << dst_level)) {
85 /* The CMASK clear is only enabled for the first level. */
86 assert(dst_level == 0);
87 if (!util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
88 dstx, dsty, dstz, src_box->width,
89 src_box->height, src_box->depth))
90 return false;
91
92 r600_texture_discard_cmask(rctx->screen, rdst);
93 }
94
95 /* All requirements are met. Prepare textures for SDMA. */
96 if (rsrc->cmask.size && rsrc->dirty_level_mask & (1 << src_level))
97 rctx->b.flush_resource(&rctx->b, &rsrc->resource.b.b);
98
99 assert(!(rsrc->dirty_level_mask & (1 << src_level)));
100 assert(!(rdst->dirty_level_mask & (1 << dst_level)));
101
102 return true;
103 }
104
105 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
106 static void r600_copy_region_with_blit(struct pipe_context *pipe,
107 struct pipe_resource *dst,
108 unsigned dst_level,
109 unsigned dstx, unsigned dsty, unsigned dstz,
110 struct pipe_resource *src,
111 unsigned src_level,
112 const struct pipe_box *src_box)
113 {
114 struct pipe_blit_info blit;
115
116 memset(&blit, 0, sizeof(blit));
117 blit.src.resource = src;
118 blit.src.format = src->format;
119 blit.src.level = src_level;
120 blit.src.box = *src_box;
121 blit.dst.resource = dst;
122 blit.dst.format = dst->format;
123 blit.dst.level = dst_level;
124 blit.dst.box.x = dstx;
125 blit.dst.box.y = dsty;
126 blit.dst.box.z = dstz;
127 blit.dst.box.width = src_box->width;
128 blit.dst.box.height = src_box->height;
129 blit.dst.box.depth = src_box->depth;
130 blit.mask = util_format_get_mask(src->format) &
131 util_format_get_mask(dst->format);
132 blit.filter = PIPE_TEX_FILTER_NEAREST;
133
134 if (blit.mask) {
135 pipe->blit(pipe, &blit);
136 }
137 }
138
139 /* Copy from a full GPU texture to a transfer's staging one. */
140 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
141 {
142 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
143 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
144 struct pipe_resource *dst = &rtransfer->staging->b.b;
145 struct pipe_resource *src = transfer->resource;
146
147 if (src->nr_samples > 1) {
148 r600_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
149 src, transfer->level, &transfer->box);
150 return;
151 }
152
153 rctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
154 &transfer->box);
155 }
156
157 /* Copy from a transfer's staging texture to a full GPU one. */
158 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
159 {
160 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
161 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
162 struct pipe_resource *dst = transfer->resource;
163 struct pipe_resource *src = &rtransfer->staging->b.b;
164 struct pipe_box sbox;
165
166 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
167
168 if (dst->nr_samples > 1) {
169 r600_copy_region_with_blit(ctx, dst, transfer->level,
170 transfer->box.x, transfer->box.y, transfer->box.z,
171 src, 0, &sbox);
172 return;
173 }
174
175 rctx->dma_copy(ctx, dst, transfer->level,
176 transfer->box.x, transfer->box.y, transfer->box.z,
177 src, 0, &sbox);
178 }
179
180 static unsigned r600_texture_get_offset(struct r600_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 (rtex->dcc_offset &&
1916 level < rtex->surface.num_dcc_levels &&
1917 !vi_dcc_formats_compatible(tex->format, view_format))
1918 if (!r600_texture_disable_dcc(rctx, (struct r600_texture*)tex))
1919 rctx->decompress_dcc(&rctx->b, rtex);
1920 }
1921
1922 struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
1923 struct pipe_resource *texture,
1924 const struct pipe_surface *templ,
1925 unsigned width0, unsigned height0,
1926 unsigned width, unsigned height)
1927 {
1928 struct r600_common_context *rctx = (struct r600_common_context*)pipe;
1929 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
1930
1931 if (!surface)
1932 return NULL;
1933
1934 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1935 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1936
1937 pipe_reference_init(&surface->base.reference, 1);
1938 pipe_resource_reference(&surface->base.texture, texture);
1939 surface->base.context = pipe;
1940 surface->base.format = templ->format;
1941 surface->base.width = width;
1942 surface->base.height = height;
1943 surface->base.u = templ->u;
1944
1945 surface->width0 = width0;
1946 surface->height0 = height0;
1947
1948 if (texture->target != PIPE_BUFFER)
1949 vi_dcc_disable_if_incompatible_format(rctx, texture,
1950 templ->u.tex.level,
1951 templ->format);
1952
1953 return &surface->base;
1954 }
1955
1956 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
1957 struct pipe_resource *tex,
1958 const struct pipe_surface *templ)
1959 {
1960 unsigned level = templ->u.tex.level;
1961 unsigned width = u_minify(tex->width0, level);
1962 unsigned height = u_minify(tex->height0, level);
1963
1964 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1965 const struct util_format_description *tex_desc
1966 = util_format_description(tex->format);
1967 const struct util_format_description *templ_desc
1968 = util_format_description(templ->format);
1969
1970 assert(tex_desc->block.bits == templ_desc->block.bits);
1971
1972 /* Adjust size of surface if and only if the block width or
1973 * height is changed. */
1974 if (tex_desc->block.width != templ_desc->block.width ||
1975 tex_desc->block.height != templ_desc->block.height) {
1976 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
1977 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
1978
1979 width = nblks_x * templ_desc->block.width;
1980 height = nblks_y * templ_desc->block.height;
1981 }
1982 }
1983
1984 return r600_create_surface_custom(pipe, tex, templ,
1985 tex->width0, tex->height0,
1986 width, height);
1987 }
1988
1989 static void r600_surface_destroy(struct pipe_context *pipe,
1990 struct pipe_surface *surface)
1991 {
1992 struct r600_surface *surf = (struct r600_surface*)surface;
1993 r600_resource_reference(&surf->cb_buffer_fmask, NULL);
1994 r600_resource_reference(&surf->cb_buffer_cmask, NULL);
1995 pipe_resource_reference(&surface->texture, NULL);
1996 FREE(surface);
1997 }
1998
1999 static void r600_clear_texture(struct pipe_context *pipe,
2000 struct pipe_resource *tex,
2001 unsigned level,
2002 const struct pipe_box *box,
2003 const void *data)
2004 {
2005 struct pipe_screen *screen = pipe->screen;
2006 struct r600_texture *rtex = (struct r600_texture*)tex;
2007 struct pipe_surface tmpl = {{0}};
2008 struct pipe_surface *sf;
2009 const struct util_format_description *desc =
2010 util_format_description(tex->format);
2011
2012 tmpl.format = tex->format;
2013 tmpl.u.tex.first_layer = box->z;
2014 tmpl.u.tex.last_layer = box->z + box->depth - 1;
2015 tmpl.u.tex.level = level;
2016 sf = pipe->create_surface(pipe, tex, &tmpl);
2017 if (!sf)
2018 return;
2019
2020 if (rtex->is_depth) {
2021 unsigned clear;
2022 float depth;
2023 uint8_t stencil = 0;
2024
2025 /* Depth is always present. */
2026 clear = PIPE_CLEAR_DEPTH;
2027 desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
2028
2029 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
2030 clear |= PIPE_CLEAR_STENCIL;
2031 desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
2032 }
2033
2034 pipe->clear_depth_stencil(pipe, sf, clear, depth, stencil,
2035 box->x, box->y,
2036 box->width, box->height, false);
2037 } else {
2038 union pipe_color_union color;
2039
2040 /* pipe_color_union requires the full vec4 representation. */
2041 if (util_format_is_pure_uint(tex->format))
2042 desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
2043 else if (util_format_is_pure_sint(tex->format))
2044 desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
2045 else
2046 desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
2047
2048 if (screen->is_format_supported(screen, tex->format,
2049 tex->target, 0,
2050 PIPE_BIND_RENDER_TARGET)) {
2051 pipe->clear_render_target(pipe, sf, &color,
2052 box->x, box->y,
2053 box->width, box->height, false);
2054 } else {
2055 /* Software fallback - just for R9G9B9E5_FLOAT */
2056 util_clear_render_target(pipe, sf, &color,
2057 box->x, box->y,
2058 box->width, box->height);
2059 }
2060 }
2061 pipe_surface_reference(&sf, NULL);
2062 }
2063
2064 unsigned r600_translate_colorswap(enum pipe_format format, bool do_endian_swap)
2065 {
2066 const struct util_format_description *desc = util_format_description(format);
2067
2068 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2069
2070 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2071 return V_0280A0_SWAP_STD;
2072
2073 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
2074 return ~0U;
2075
2076 switch (desc->nr_channels) {
2077 case 1:
2078 if (HAS_SWIZZLE(0,X))
2079 return V_0280A0_SWAP_STD; /* X___ */
2080 else if (HAS_SWIZZLE(3,X))
2081 return V_0280A0_SWAP_ALT_REV; /* ___X */
2082 break;
2083 case 2:
2084 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
2085 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
2086 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
2087 return V_0280A0_SWAP_STD; /* XY__ */
2088 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
2089 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
2090 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
2091 /* YX__ */
2092 return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
2093 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
2094 return V_0280A0_SWAP_ALT; /* X__Y */
2095 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
2096 return V_0280A0_SWAP_ALT_REV; /* Y__X */
2097 break;
2098 case 3:
2099 if (HAS_SWIZZLE(0,X))
2100 return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
2101 else if (HAS_SWIZZLE(0,Z))
2102 return V_0280A0_SWAP_STD_REV; /* ZYX */
2103 break;
2104 case 4:
2105 /* check the middle channels, the 1st and 4th channel can be NONE */
2106 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
2107 return V_0280A0_SWAP_STD; /* XYZW */
2108 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
2109 return V_0280A0_SWAP_STD_REV; /* WZYX */
2110 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
2111 return V_0280A0_SWAP_ALT; /* ZYXW */
2112 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
2113 /* YZWX */
2114 if (desc->is_array)
2115 return V_0280A0_SWAP_ALT_REV;
2116 else
2117 return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
2118 }
2119 break;
2120 }
2121 return ~0U;
2122 }
2123
2124 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2125
2126 static void vi_dcc_clean_up_context_slot(struct r600_common_context *rctx,
2127 int slot)
2128 {
2129 int i;
2130
2131 if (rctx->dcc_stats[slot].query_active)
2132 vi_separate_dcc_stop_query(&rctx->b,
2133 rctx->dcc_stats[slot].tex);
2134
2135 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats[slot].ps_stats); i++)
2136 if (rctx->dcc_stats[slot].ps_stats[i]) {
2137 rctx->b.destroy_query(&rctx->b,
2138 rctx->dcc_stats[slot].ps_stats[i]);
2139 rctx->dcc_stats[slot].ps_stats[i] = NULL;
2140 }
2141
2142 r600_texture_reference(&rctx->dcc_stats[slot].tex, NULL);
2143 }
2144
2145 /**
2146 * Return the per-context slot where DCC statistics queries for the texture live.
2147 */
2148 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context *rctx,
2149 struct r600_texture *tex)
2150 {
2151 int i, empty_slot = -1;
2152
2153 /* Remove zombie textures (textures kept alive by this array only). */
2154 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2155 if (rctx->dcc_stats[i].tex &&
2156 rctx->dcc_stats[i].tex->resource.b.b.reference.count == 1)
2157 vi_dcc_clean_up_context_slot(rctx, i);
2158
2159 /* Find the texture. */
2160 for (i = 0; i < ARRAY_SIZE(rctx->dcc_stats); i++) {
2161 /* Return if found. */
2162 if (rctx->dcc_stats[i].tex == tex) {
2163 rctx->dcc_stats[i].last_use_timestamp = os_time_get();
2164 return i;
2165 }
2166
2167 /* Record the first seen empty slot. */
2168 if (empty_slot == -1 && !rctx->dcc_stats[i].tex)
2169 empty_slot = i;
2170 }
2171
2172 /* Not found. Remove the oldest member to make space in the array. */
2173 if (empty_slot == -1) {
2174 int oldest_slot = 0;
2175
2176 /* Find the oldest slot. */
2177 for (i = 1; i < ARRAY_SIZE(rctx->dcc_stats); i++)
2178 if (rctx->dcc_stats[oldest_slot].last_use_timestamp >
2179 rctx->dcc_stats[i].last_use_timestamp)
2180 oldest_slot = i;
2181
2182 /* Clean up the oldest slot. */
2183 vi_dcc_clean_up_context_slot(rctx, oldest_slot);
2184 empty_slot = oldest_slot;
2185 }
2186
2187 /* Add the texture to the new slot. */
2188 r600_texture_reference(&rctx->dcc_stats[empty_slot].tex, tex);
2189 rctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2190 return empty_slot;
2191 }
2192
2193 static struct pipe_query *
2194 vi_create_resuming_pipestats_query(struct pipe_context *ctx)
2195 {
2196 struct r600_query_hw *query = (struct r600_query_hw*)
2197 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2198
2199 query->flags |= R600_QUERY_HW_FLAG_BEGIN_RESUMES;
2200 return (struct pipe_query*)query;
2201 }
2202
2203 /**
2204 * Called when binding a color buffer.
2205 */
2206 void vi_separate_dcc_start_query(struct pipe_context *ctx,
2207 struct r600_texture *tex)
2208 {
2209 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2210 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2211
2212 assert(!rctx->dcc_stats[i].query_active);
2213
2214 if (!rctx->dcc_stats[i].ps_stats[0])
2215 rctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(ctx);
2216
2217 /* begin or resume the query */
2218 ctx->begin_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2219 rctx->dcc_stats[i].query_active = true;
2220 }
2221
2222 /**
2223 * Called when unbinding a color buffer.
2224 */
2225 void vi_separate_dcc_stop_query(struct pipe_context *ctx,
2226 struct r600_texture *tex)
2227 {
2228 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2229 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2230
2231 assert(rctx->dcc_stats[i].query_active);
2232 assert(rctx->dcc_stats[i].ps_stats[0]);
2233
2234 /* pause or end the query */
2235 ctx->end_query(ctx, rctx->dcc_stats[i].ps_stats[0]);
2236 rctx->dcc_stats[i].query_active = false;
2237 }
2238
2239 static bool vi_should_enable_separate_dcc(struct r600_texture *tex)
2240 {
2241 /* The minimum number of fullscreen draws per frame that is required
2242 * to enable DCC. */
2243 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2244 }
2245
2246 /* Called by fast clear. */
2247 static void vi_separate_dcc_try_enable(struct r600_common_context *rctx,
2248 struct r600_texture *tex)
2249 {
2250 /* The intent is to use this with shared displayable back buffers,
2251 * but it's not strictly limited only to them.
2252 */
2253 if (!tex->resource.is_shared ||
2254 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2255 tex->resource.b.b.target != PIPE_TEXTURE_2D ||
2256 tex->resource.b.b.last_level > 0 ||
2257 !tex->surface.dcc_size)
2258 return;
2259
2260 if (tex->dcc_offset)
2261 return; /* already enabled */
2262
2263 /* Enable the DCC stat gathering. */
2264 if (!tex->dcc_gather_statistics) {
2265 tex->dcc_gather_statistics = true;
2266 vi_separate_dcc_start_query(&rctx->b, tex);
2267 }
2268
2269 if (!vi_should_enable_separate_dcc(tex))
2270 return; /* stats show that DCC decompression is too expensive */
2271
2272 assert(tex->surface.num_dcc_levels);
2273 assert(!tex->dcc_separate_buffer);
2274
2275 r600_texture_discard_cmask(rctx->screen, tex);
2276
2277 /* Get a DCC buffer. */
2278 if (tex->last_dcc_separate_buffer) {
2279 assert(tex->dcc_gather_statistics);
2280 assert(!tex->dcc_separate_buffer);
2281 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2282 tex->last_dcc_separate_buffer = NULL;
2283 } else {
2284 tex->dcc_separate_buffer = (struct r600_resource*)
2285 r600_aligned_buffer_create(rctx->b.screen,
2286 R600_RESOURCE_FLAG_UNMAPPABLE,
2287 PIPE_USAGE_DEFAULT,
2288 tex->surface.dcc_size,
2289 tex->surface.dcc_alignment);
2290 if (!tex->dcc_separate_buffer)
2291 return;
2292 }
2293
2294 /* dcc_offset is the absolute GPUVM address. */
2295 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
2296
2297 /* no need to flag anything since this is called by fast clear that
2298 * flags framebuffer state
2299 */
2300 }
2301
2302 /**
2303 * Called by pipe_context::flush_resource, the place where DCC decompression
2304 * takes place.
2305 */
2306 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
2307 struct r600_texture *tex)
2308 {
2309 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
2310 struct pipe_query *tmp;
2311 unsigned i = vi_get_context_dcc_stats_index(rctx, tex);
2312 bool query_active = rctx->dcc_stats[i].query_active;
2313 bool disable = false;
2314
2315 if (rctx->dcc_stats[i].ps_stats[2]) {
2316 union pipe_query_result result;
2317
2318 /* Read the results. */
2319 ctx->get_query_result(ctx, rctx->dcc_stats[i].ps_stats[2],
2320 true, &result);
2321 r600_query_hw_reset_buffers(rctx,
2322 (struct r600_query_hw*)
2323 rctx->dcc_stats[i].ps_stats[2]);
2324
2325 /* Compute the approximate number of fullscreen draws. */
2326 tex->ps_draw_ratio =
2327 result.pipeline_statistics.ps_invocations /
2328 (tex->resource.b.b.width0 * tex->resource.b.b.height0);
2329 rctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2330
2331 disable = tex->dcc_separate_buffer &&
2332 !vi_should_enable_separate_dcc(tex);
2333 }
2334
2335 tex->num_slow_clears = 0;
2336
2337 /* stop the statistics query for ps_stats[0] */
2338 if (query_active)
2339 vi_separate_dcc_stop_query(ctx, tex);
2340
2341 /* Move the queries in the queue by one. */
2342 tmp = rctx->dcc_stats[i].ps_stats[2];
2343 rctx->dcc_stats[i].ps_stats[2] = rctx->dcc_stats[i].ps_stats[1];
2344 rctx->dcc_stats[i].ps_stats[1] = rctx->dcc_stats[i].ps_stats[0];
2345 rctx->dcc_stats[i].ps_stats[0] = tmp;
2346
2347 /* create and start a new query as ps_stats[0] */
2348 if (query_active)
2349 vi_separate_dcc_start_query(ctx, tex);
2350
2351 if (disable) {
2352 assert(!tex->last_dcc_separate_buffer);
2353 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2354 tex->dcc_separate_buffer = NULL;
2355 tex->dcc_offset = 0;
2356 /* no need to flag anything since this is called after
2357 * decompression that re-sets framebuffer state
2358 */
2359 }
2360 }
2361
2362 /* FAST COLOR CLEAR */
2363
2364 static void evergreen_set_clear_color(struct r600_texture *rtex,
2365 enum pipe_format surface_format,
2366 const union pipe_color_union *color)
2367 {
2368 union util_color uc;
2369
2370 memset(&uc, 0, sizeof(uc));
2371
2372 if (rtex->surface.bpe == 16) {
2373 /* DCC fast clear only:
2374 * CLEAR_WORD0 = R = G = B
2375 * CLEAR_WORD1 = A
2376 */
2377 assert(color->ui[0] == color->ui[1] &&
2378 color->ui[0] == color->ui[2]);
2379 uc.ui[0] = color->ui[0];
2380 uc.ui[1] = color->ui[3];
2381 } else if (util_format_is_pure_uint(surface_format)) {
2382 util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
2383 } else if (util_format_is_pure_sint(surface_format)) {
2384 util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
2385 } else {
2386 util_pack_color(color->f, surface_format, &uc);
2387 }
2388
2389 memcpy(rtex->color_clear_value, &uc, 2 * sizeof(uint32_t));
2390 }
2391
2392 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format,
2393 const union pipe_color_union *color,
2394 uint32_t* reset_value,
2395 bool* clear_words_needed)
2396 {
2397 bool values[4] = {};
2398 int i;
2399 bool main_value = false;
2400 bool extra_value = false;
2401 int extra_channel;
2402 const struct util_format_description *desc = util_format_description(surface_format);
2403
2404 if (desc->block.bits == 128 &&
2405 (color->ui[0] != color->ui[1] ||
2406 color->ui[0] != color->ui[2]))
2407 return false;
2408
2409 *clear_words_needed = true;
2410 *reset_value = 0x20202020U;
2411
2412 /* If we want to clear without needing a fast clear eliminate step, we
2413 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2414 * have two sets of flags, one for the last or first channel(extra) and
2415 * one for the other channels(main).
2416 */
2417
2418 if (surface_format == PIPE_FORMAT_R11G11B10_FLOAT ||
2419 surface_format == PIPE_FORMAT_B5G6R5_UNORM ||
2420 surface_format == PIPE_FORMAT_B5G6R5_SRGB) {
2421 extra_channel = -1;
2422 } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
2423 if(r600_translate_colorswap(surface_format, false) <= 1)
2424 extra_channel = desc->nr_channels - 1;
2425 else
2426 extra_channel = 0;
2427 } else
2428 return true;
2429
2430 for (i = 0; i < 4; ++i) {
2431 int index = desc->swizzle[i] - PIPE_SWIZZLE_X;
2432
2433 if (desc->swizzle[i] < PIPE_SWIZZLE_X ||
2434 desc->swizzle[i] > PIPE_SWIZZLE_W)
2435 continue;
2436
2437 if (desc->channel[i].pure_integer &&
2438 desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2439 /* Use the maximum value for clamping the clear color. */
2440 int max = u_bit_consecutive(0, desc->channel[i].size - 1);
2441
2442 values[i] = color->i[i] != 0;
2443 if (color->i[i] != 0 && MIN2(color->i[i], max) != max)
2444 return true;
2445 } else if (desc->channel[i].pure_integer &&
2446 desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2447 /* Use the maximum value for clamping the clear color. */
2448 unsigned max = u_bit_consecutive(0, desc->channel[i].size);
2449
2450 values[i] = color->ui[i] != 0U;
2451 if (color->ui[i] != 0U && MIN2(color->ui[i], max) != max)
2452 return true;
2453 } else {
2454 values[i] = color->f[i] != 0.0F;
2455 if (color->f[i] != 0.0F && color->f[i] != 1.0F)
2456 return true;
2457 }
2458
2459 if (index == extra_channel)
2460 extra_value = values[i];
2461 else
2462 main_value = values[i];
2463 }
2464
2465 for (int i = 0; i < 4; ++i)
2466 if (values[i] != main_value &&
2467 desc->swizzle[i] - PIPE_SWIZZLE_X != extra_channel &&
2468 desc->swizzle[i] >= PIPE_SWIZZLE_X &&
2469 desc->swizzle[i] <= PIPE_SWIZZLE_W)
2470 return true;
2471
2472 *clear_words_needed = false;
2473 if (main_value)
2474 *reset_value |= 0x80808080U;
2475
2476 if (extra_value)
2477 *reset_value |= 0x40404040U;
2478 return true;
2479 }
2480
2481 void vi_dcc_clear_level(struct r600_common_context *rctx,
2482 struct r600_texture *rtex,
2483 unsigned level, unsigned clear_value)
2484 {
2485 struct pipe_resource *dcc_buffer;
2486 uint64_t dcc_offset, clear_size;
2487
2488 assert(rtex->dcc_offset && level < rtex->surface.num_dcc_levels);
2489
2490 if (rtex->dcc_separate_buffer) {
2491 dcc_buffer = &rtex->dcc_separate_buffer->b.b;
2492 dcc_offset = 0;
2493 } else {
2494 dcc_buffer = &rtex->resource.b.b;
2495 dcc_offset = rtex->dcc_offset;
2496 }
2497
2498 if (rctx->chip_class >= GFX9) {
2499 /* Mipmap level clears aren't implemented. */
2500 assert(rtex->resource.b.b.last_level == 0);
2501 /* MSAA needs a different clear size. */
2502 assert(rtex->resource.b.b.nr_samples <= 1);
2503 clear_size = rtex->surface.dcc_size;
2504 } else {
2505 dcc_offset += rtex->surface.u.legacy.level[level].dcc_offset;
2506 clear_size = rtex->surface.u.legacy.level[level].dcc_fast_clear_size;
2507 }
2508
2509 rctx->clear_buffer(&rctx->b, dcc_buffer, dcc_offset, clear_size,
2510 clear_value, R600_COHERENCY_CB_META);
2511 }
2512
2513 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2514 * This allows hitting the MSAA resolve fast path, which requires that both
2515 * src and dst micro tile modes match.
2516 */
2517 static void si_set_optimal_micro_tile_mode(struct r600_common_screen *rscreen,
2518 struct r600_texture *rtex)
2519 {
2520 if (rtex->resource.is_shared ||
2521 rtex->resource.b.b.nr_samples <= 1 ||
2522 rtex->surface.micro_tile_mode == rtex->last_msaa_resolve_target_micro_mode)
2523 return;
2524
2525 assert(rscreen->chip_class >= GFX9 ||
2526 rtex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_2D);
2527 assert(rtex->resource.b.b.last_level == 0);
2528
2529 if (rscreen->chip_class >= GFX9) {
2530 /* 4K or larger tiles only. 0 is linear. 1-3 are 256B tiles. */
2531 assert(rtex->surface.u.gfx9.surf.swizzle_mode >= 4);
2532
2533 /* If you do swizzle_mode % 4, you'll get:
2534 * 0 = Depth
2535 * 1 = Standard,
2536 * 2 = Displayable
2537 * 3 = Rotated
2538 *
2539 * Depth-sample order isn't allowed:
2540 */
2541 assert(rtex->surface.u.gfx9.surf.swizzle_mode % 4 != 0);
2542
2543 switch (rtex->last_msaa_resolve_target_micro_mode) {
2544 case RADEON_MICRO_MODE_DISPLAY:
2545 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2546 rtex->surface.u.gfx9.surf.swizzle_mode += 2; /* D */
2547 break;
2548 case RADEON_MICRO_MODE_THIN:
2549 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2550 rtex->surface.u.gfx9.surf.swizzle_mode += 1; /* S */
2551 break;
2552 case RADEON_MICRO_MODE_ROTATED:
2553 rtex->surface.u.gfx9.surf.swizzle_mode &= ~0x3;
2554 rtex->surface.u.gfx9.surf.swizzle_mode += 3; /* R */
2555 break;
2556 default: /* depth */
2557 assert(!"unexpected micro mode");
2558 return;
2559 }
2560 } else if (rscreen->chip_class >= CIK) {
2561 /* These magic numbers were copied from addrlib. It doesn't use
2562 * any definitions for them either. They are all 2D_TILED_THIN1
2563 * modes with different bpp and micro tile mode.
2564 */
2565 switch (rtex->last_msaa_resolve_target_micro_mode) {
2566 case RADEON_MICRO_MODE_DISPLAY:
2567 rtex->surface.u.legacy.tiling_index[0] = 10;
2568 break;
2569 case RADEON_MICRO_MODE_THIN:
2570 rtex->surface.u.legacy.tiling_index[0] = 14;
2571 break;
2572 case RADEON_MICRO_MODE_ROTATED:
2573 rtex->surface.u.legacy.tiling_index[0] = 28;
2574 break;
2575 default: /* depth, thick */
2576 assert(!"unexpected micro mode");
2577 return;
2578 }
2579 } else { /* SI */
2580 switch (rtex->last_msaa_resolve_target_micro_mode) {
2581 case RADEON_MICRO_MODE_DISPLAY:
2582 switch (rtex->surface.bpe) {
2583 case 1:
2584 rtex->surface.u.legacy.tiling_index[0] = 10;
2585 break;
2586 case 2:
2587 rtex->surface.u.legacy.tiling_index[0] = 11;
2588 break;
2589 default: /* 4, 8 */
2590 rtex->surface.u.legacy.tiling_index[0] = 12;
2591 break;
2592 }
2593 break;
2594 case RADEON_MICRO_MODE_THIN:
2595 switch (rtex->surface.bpe) {
2596 case 1:
2597 rtex->surface.u.legacy.tiling_index[0] = 14;
2598 break;
2599 case 2:
2600 rtex->surface.u.legacy.tiling_index[0] = 15;
2601 break;
2602 case 4:
2603 rtex->surface.u.legacy.tiling_index[0] = 16;
2604 break;
2605 default: /* 8, 16 */
2606 rtex->surface.u.legacy.tiling_index[0] = 17;
2607 break;
2608 }
2609 break;
2610 default: /* depth, thick */
2611 assert(!"unexpected micro mode");
2612 return;
2613 }
2614 }
2615
2616 rtex->surface.micro_tile_mode = rtex->last_msaa_resolve_target_micro_mode;
2617
2618 p_atomic_inc(&rscreen->dirty_tex_counter);
2619 }
2620
2621 void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
2622 struct pipe_framebuffer_state *fb,
2623 struct r600_atom *fb_state,
2624 unsigned *buffers, unsigned *dirty_cbufs,
2625 const union pipe_color_union *color)
2626 {
2627 int i;
2628
2629 /* This function is broken in BE, so just disable this path for now */
2630 #ifdef PIPE_ARCH_BIG_ENDIAN
2631 return;
2632 #endif
2633
2634 if (rctx->render_cond)
2635 return;
2636
2637 /* TODO: fix CMASK and DCC fast clear */
2638 if (rctx->chip_class >= GFX9)
2639 return;
2640
2641 for (i = 0; i < fb->nr_cbufs; i++) {
2642 struct r600_texture *tex;
2643 unsigned clear_bit = PIPE_CLEAR_COLOR0 << i;
2644
2645 if (!fb->cbufs[i])
2646 continue;
2647
2648 /* if this colorbuffer is not being cleared */
2649 if (!(*buffers & clear_bit))
2650 continue;
2651
2652 tex = (struct r600_texture *)fb->cbufs[i]->texture;
2653
2654 /* the clear is allowed if all layers are bound */
2655 if (fb->cbufs[i]->u.tex.first_layer != 0 ||
2656 fb->cbufs[i]->u.tex.last_layer != util_max_layer(&tex->resource.b.b, 0)) {
2657 continue;
2658 }
2659
2660 /* cannot clear mipmapped textures */
2661 if (fb->cbufs[i]->texture->last_level != 0) {
2662 continue;
2663 }
2664
2665 /* only supported on tiled surfaces */
2666 if (tex->surface.is_linear) {
2667 continue;
2668 }
2669
2670 /* shared textures can't use fast clear without an explicit flush,
2671 * because there is no way to communicate the clear color among
2672 * all clients
2673 */
2674 if (tex->resource.is_shared &&
2675 !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
2676 continue;
2677
2678 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2679 if (rctx->chip_class == CIK &&
2680 tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
2681 rctx->screen->info.drm_major == 2 &&
2682 rctx->screen->info.drm_minor < 38) {
2683 continue;
2684 }
2685
2686 /* Fast clear is the most appropriate place to enable DCC for
2687 * displayable surfaces.
2688 */
2689 if (rctx->chip_class >= VI &&
2690 !(rctx->screen->debug_flags & DBG_NO_DCC_FB)) {
2691 vi_separate_dcc_try_enable(rctx, tex);
2692
2693 /* RB+ isn't supported with a CMASK-based clear, so all
2694 * clears are considered to be hypothetically slow
2695 * clears, which is weighed when determining whether to
2696 * enable separate DCC.
2697 */
2698 if (tex->dcc_gather_statistics &&
2699 rctx->screen->rbplus_allowed)
2700 tex->num_slow_clears++;
2701 }
2702
2703 /* Try to clear DCC first, otherwise try CMASK. */
2704 if (tex->dcc_offset && tex->surface.num_dcc_levels) {
2705 uint32_t reset_value;
2706 bool clear_words_needed;
2707
2708 if (rctx->screen->debug_flags & DBG_NO_DCC_CLEAR)
2709 continue;
2710
2711 if (!vi_get_fast_clear_parameters(fb->cbufs[i]->format,
2712 color, &reset_value,
2713 &clear_words_needed))
2714 continue;
2715
2716 vi_dcc_clear_level(rctx, tex, 0, reset_value);
2717
2718 if (clear_words_needed)
2719 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2720 tex->separate_dcc_dirty = true;
2721 } else {
2722 /* 128-bit formats are unusupported */
2723 if (tex->surface.bpe > 8) {
2724 continue;
2725 }
2726
2727 /* RB+ doesn't work with CMASK fast clear. */
2728 if (rctx->screen->rbplus_allowed)
2729 continue;
2730
2731 /* ensure CMASK is enabled */
2732 r600_texture_alloc_cmask_separate(rctx->screen, tex);
2733 if (tex->cmask.size == 0) {
2734 continue;
2735 }
2736
2737 /* Do the fast clear. */
2738 rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
2739 tex->cmask.offset, tex->cmask.size, 0,
2740 R600_COHERENCY_CB_META);
2741
2742 tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
2743 }
2744
2745 /* We can change the micro tile mode before a full clear. */
2746 if (rctx->screen->chip_class >= SI)
2747 si_set_optimal_micro_tile_mode(rctx->screen, tex);
2748
2749 evergreen_set_clear_color(tex, fb->cbufs[i]->format, color);
2750
2751 if (dirty_cbufs)
2752 *dirty_cbufs |= 1 << i;
2753 rctx->set_atom_dirty(rctx, fb_state, true);
2754 *buffers &= ~clear_bit;
2755 }
2756 }
2757
2758 void r600_init_screen_texture_functions(struct r600_common_screen *rscreen)
2759 {
2760 rscreen->b.resource_from_handle = r600_texture_from_handle;
2761 rscreen->b.resource_get_handle = r600_texture_get_handle;
2762 }
2763
2764 void r600_init_context_texture_functions(struct r600_common_context *rctx)
2765 {
2766 rctx->b.create_surface = r600_create_surface;
2767 rctx->b.surface_destroy = r600_surface_destroy;
2768 rctx->b.clear_texture = r600_clear_texture;
2769 }