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