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