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