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