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