winsys/amdgpu,radeon: pass vm_alignment to buffer_from_handle
[mesa.git] / src / gallium / drivers / radeonsi / si_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "radeonsi/si_pipe.h"
27 #include "radeonsi/si_query.h"
28 #include "util/u_format.h"
29 #include "util/u_log.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include "util/u_resource.h"
33 #include "util/u_surface.h"
34 #include "util/u_transfer.h"
35 #include "util/os_time.h"
36 #include <errno.h>
37 #include <inttypes.h>
38 #include "state_tracker/drm_driver.h"
39 #include "amd/common/sid.h"
40
41 static enum radeon_surf_mode
42 si_choose_tiling(struct si_screen *sscreen,
43 const struct pipe_resource *templ, bool tc_compatible_htile);
44
45
46 bool si_prepare_for_dma_blit(struct si_context *sctx,
47 struct si_texture *dst,
48 unsigned dst_level, unsigned dstx,
49 unsigned dsty, unsigned dstz,
50 struct si_texture *src,
51 unsigned src_level,
52 const struct pipe_box *src_box)
53 {
54 if (!sctx->dma_cs)
55 return false;
56
57 if (dst->surface.bpe != src->surface.bpe)
58 return false;
59
60 /* MSAA: Blits don't exist in the real world. */
61 if (src->buffer.b.b.nr_samples > 1 ||
62 dst->buffer.b.b.nr_samples > 1)
63 return false;
64
65 /* Depth-stencil surfaces:
66 * When dst is linear, the DB->CB copy preserves HTILE.
67 * When dst is tiled, the 3D path must be used to update HTILE.
68 */
69 if (src->is_depth || dst->is_depth)
70 return false;
71
72 /* DCC as:
73 * src: Use the 3D path. DCC decompression is expensive.
74 * dst: Use the 3D path to compress the pixels with DCC.
75 */
76 if (vi_dcc_enabled(src, src_level) ||
77 vi_dcc_enabled(dst, dst_level))
78 return false;
79
80 /* CMASK as:
81 * src: Both texture and SDMA paths need decompression. Use SDMA.
82 * dst: If overwriting the whole texture, discard CMASK and use
83 * SDMA. Otherwise, use the 3D path.
84 */
85 if (dst->cmask_buffer && dst->dirty_level_mask & (1 << dst_level)) {
86 /* The CMASK clear is only enabled for the first level. */
87 assert(dst_level == 0);
88 if (!util_texrange_covers_whole_level(&dst->buffer.b.b, dst_level,
89 dstx, dsty, dstz, src_box->width,
90 src_box->height, src_box->depth))
91 return false;
92
93 si_texture_discard_cmask(sctx->screen, dst);
94 }
95
96 /* All requirements are met. Prepare textures for SDMA. */
97 if (src->cmask_buffer && src->dirty_level_mask & (1 << src_level))
98 sctx->b.flush_resource(&sctx->b, &src->buffer.b.b);
99
100 assert(!(src->dirty_level_mask & (1 << src_level)));
101 assert(!(dst->dirty_level_mask & (1 << dst_level)));
102
103 return true;
104 }
105
106 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
107 static void si_copy_region_with_blit(struct pipe_context *pipe,
108 struct pipe_resource *dst,
109 unsigned dst_level,
110 unsigned dstx, unsigned dsty, unsigned dstz,
111 struct pipe_resource *src,
112 unsigned src_level,
113 const struct pipe_box *src_box)
114 {
115 struct pipe_blit_info blit;
116
117 memset(&blit, 0, sizeof(blit));
118 blit.src.resource = src;
119 blit.src.format = src->format;
120 blit.src.level = src_level;
121 blit.src.box = *src_box;
122 blit.dst.resource = dst;
123 blit.dst.format = dst->format;
124 blit.dst.level = dst_level;
125 blit.dst.box.x = dstx;
126 blit.dst.box.y = dsty;
127 blit.dst.box.z = dstz;
128 blit.dst.box.width = src_box->width;
129 blit.dst.box.height = src_box->height;
130 blit.dst.box.depth = src_box->depth;
131 blit.mask = util_format_get_mask(src->format) &
132 util_format_get_mask(dst->format);
133 blit.filter = PIPE_TEX_FILTER_NEAREST;
134
135 if (blit.mask) {
136 pipe->blit(pipe, &blit);
137 }
138 }
139
140 /* Copy from a full GPU texture to a transfer's staging one. */
141 static void si_copy_to_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
142 {
143 struct si_context *sctx = (struct si_context*)ctx;
144 struct pipe_transfer *transfer = (struct pipe_transfer*)stransfer;
145 struct pipe_resource *dst = &stransfer->staging->b.b;
146 struct pipe_resource *src = transfer->resource;
147
148 if (src->nr_samples > 1) {
149 si_copy_region_with_blit(ctx, dst, 0, 0, 0, 0,
150 src, transfer->level, &transfer->box);
151 return;
152 }
153
154 sctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level,
155 &transfer->box);
156 }
157
158 /* Copy from a transfer's staging texture to a full GPU one. */
159 static void si_copy_from_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
160 {
161 struct si_context *sctx = (struct si_context*)ctx;
162 struct pipe_transfer *transfer = (struct pipe_transfer*)stransfer;
163 struct pipe_resource *dst = transfer->resource;
164 struct pipe_resource *src = &stransfer->staging->b.b;
165 struct pipe_box sbox;
166
167 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
168
169 if (dst->nr_samples > 1) {
170 si_copy_region_with_blit(ctx, dst, transfer->level,
171 transfer->box.x, transfer->box.y, transfer->box.z,
172 src, 0, &sbox);
173 return;
174 }
175
176 sctx->dma_copy(ctx, dst, transfer->level,
177 transfer->box.x, transfer->box.y, transfer->box.z,
178 src, 0, &sbox);
179 }
180
181 static unsigned si_texture_get_offset(struct si_screen *sscreen,
182 struct si_texture *tex, unsigned level,
183 const struct pipe_box *box,
184 unsigned *stride,
185 unsigned *layer_stride)
186 {
187 if (sscreen->info.chip_class >= GFX9) {
188 *stride = tex->surface.u.gfx9.surf_pitch * tex->surface.bpe;
189 *layer_stride = tex->surface.u.gfx9.surf_slice_size;
190
191 if (!box)
192 return 0;
193
194 /* Each texture is an array of slices. Each slice is an array
195 * of mipmap levels. */
196 return box->z * tex->surface.u.gfx9.surf_slice_size +
197 tex->surface.u.gfx9.offset[level] +
198 (box->y / tex->surface.blk_h *
199 tex->surface.u.gfx9.surf_pitch +
200 box->x / tex->surface.blk_w) * tex->surface.bpe;
201 } else {
202 *stride = tex->surface.u.legacy.level[level].nblk_x *
203 tex->surface.bpe;
204 assert((uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 <= UINT_MAX);
205 *layer_stride = (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4;
206
207 if (!box)
208 return tex->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 tex->surface.u.legacy.level[level].offset +
213 box->z * (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 +
214 (box->y / tex->surface.blk_h *
215 tex->surface.u.legacy.level[level].nblk_x +
216 box->x / tex->surface.blk_w) * tex->surface.bpe;
217 }
218 }
219
220 static int si_init_surface(struct si_screen *sscreen,
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 */
243 } else {
244 bpe = util_format_get_blocksize(ptex->format);
245 assert(util_is_power_of_two_or_zero(bpe));
246 }
247
248 if (!is_flushed_depth && is_depth) {
249 flags |= RADEON_SURF_ZBUFFER;
250
251 if (tc_compatible_htile &&
252 (sscreen->info.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 (sscreen->info.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 (sscreen->info.chip_class >= VI &&
270 (ptex->flags & SI_RESOURCE_FLAG_DISABLE_DCC ||
271 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT ||
272 (ptex->nr_samples >= 2 && !sscreen->dcc_msaa_allowed)))
273 flags |= RADEON_SURF_DISABLE_DCC;
274
275 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
276 if (sscreen->info.family == CHIP_STONEY &&
277 bpe == 16 && ptex->nr_samples >= 2)
278 flags |= RADEON_SURF_DISABLE_DCC;
279
280 /* VI: DCC clear for 4x and 8x MSAA array textures unimplemented. */
281 if (sscreen->info.chip_class == VI &&
282 ptex->nr_storage_samples >= 4 &&
283 ptex->array_size > 1)
284 flags |= RADEON_SURF_DISABLE_DCC;
285
286 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
287 if (sscreen->info.chip_class >= GFX9 &&
288 ptex->nr_storage_samples >= 4)
289 flags |= RADEON_SURF_DISABLE_DCC;
290
291 if (ptex->bind & PIPE_BIND_SCANOUT || is_scanout) {
292 /* This should catch bugs in gallium users setting incorrect flags. */
293 assert(ptex->nr_samples <= 1 &&
294 ptex->array_size == 1 &&
295 ptex->depth0 == 1 &&
296 ptex->last_level == 0 &&
297 !(flags & RADEON_SURF_Z_OR_SBUFFER));
298
299 flags |= RADEON_SURF_SCANOUT;
300 }
301
302 if (ptex->bind & PIPE_BIND_SHARED)
303 flags |= RADEON_SURF_SHAREABLE;
304 if (is_imported)
305 flags |= RADEON_SURF_IMPORTED | RADEON_SURF_SHAREABLE;
306 if (!(ptex->flags & SI_RESOURCE_FLAG_FORCE_TILING))
307 flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
308
309 r = sscreen->ws->surface_init(sscreen->ws, ptex, flags, bpe,
310 array_mode, surface);
311 if (r) {
312 return r;
313 }
314
315 unsigned pitch = pitch_in_bytes_override / bpe;
316
317 if (sscreen->info.chip_class >= GFX9) {
318 if (pitch) {
319 surface->u.gfx9.surf_pitch = pitch;
320 surface->u.gfx9.surf_slice_size =
321 (uint64_t)pitch * surface->u.gfx9.surf_height * bpe;
322 }
323 surface->u.gfx9.surf_offset = offset;
324 } else {
325 if (pitch) {
326 surface->u.legacy.level[0].nblk_x = pitch;
327 surface->u.legacy.level[0].slice_size_dw =
328 ((uint64_t)pitch * surface->u.legacy.level[0].nblk_y * bpe) / 4;
329 }
330 if (offset) {
331 for (i = 0; i < ARRAY_SIZE(surface->u.legacy.level); ++i)
332 surface->u.legacy.level[i].offset += offset;
333 }
334 }
335 return 0;
336 }
337
338 static void si_texture_init_metadata(struct si_screen *sscreen,
339 struct si_texture *tex,
340 struct radeon_bo_metadata *metadata)
341 {
342 struct radeon_surf *surface = &tex->surface;
343
344 memset(metadata, 0, sizeof(*metadata));
345
346 if (sscreen->info.chip_class >= GFX9) {
347 metadata->u.gfx9.swizzle_mode = surface->u.gfx9.surf.swizzle_mode;
348 } else {
349 metadata->u.legacy.microtile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D ?
350 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
351 metadata->u.legacy.macrotile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D ?
352 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
353 metadata->u.legacy.pipe_config = surface->u.legacy.pipe_config;
354 metadata->u.legacy.bankw = surface->u.legacy.bankw;
355 metadata->u.legacy.bankh = surface->u.legacy.bankh;
356 metadata->u.legacy.tile_split = surface->u.legacy.tile_split;
357 metadata->u.legacy.mtilea = surface->u.legacy.mtilea;
358 metadata->u.legacy.num_banks = surface->u.legacy.num_banks;
359 metadata->u.legacy.stride = surface->u.legacy.level[0].nblk_x * surface->bpe;
360 metadata->u.legacy.scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
361 }
362 }
363
364 static void si_surface_import_metadata(struct si_screen *sscreen,
365 struct radeon_surf *surf,
366 struct radeon_bo_metadata *metadata,
367 enum radeon_surf_mode *array_mode,
368 bool *is_scanout)
369 {
370 if (sscreen->info.chip_class >= GFX9) {
371 if (metadata->u.gfx9.swizzle_mode > 0)
372 *array_mode = RADEON_SURF_MODE_2D;
373 else
374 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
375
376 *is_scanout = metadata->u.gfx9.swizzle_mode == 0 ||
377 metadata->u.gfx9.swizzle_mode % 4 == 2;
378
379 surf->u.gfx9.surf.swizzle_mode = metadata->u.gfx9.swizzle_mode;
380 } else {
381 surf->u.legacy.pipe_config = metadata->u.legacy.pipe_config;
382 surf->u.legacy.bankw = metadata->u.legacy.bankw;
383 surf->u.legacy.bankh = metadata->u.legacy.bankh;
384 surf->u.legacy.tile_split = metadata->u.legacy.tile_split;
385 surf->u.legacy.mtilea = metadata->u.legacy.mtilea;
386 surf->u.legacy.num_banks = metadata->u.legacy.num_banks;
387
388 if (metadata->u.legacy.macrotile == RADEON_LAYOUT_TILED)
389 *array_mode = RADEON_SURF_MODE_2D;
390 else if (metadata->u.legacy.microtile == RADEON_LAYOUT_TILED)
391 *array_mode = RADEON_SURF_MODE_1D;
392 else
393 *array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
394
395 *is_scanout = metadata->u.legacy.scanout;
396 }
397 }
398
399 void si_eliminate_fast_color_clear(struct si_context *sctx,
400 struct si_texture *tex)
401 {
402 struct si_screen *sscreen = sctx->screen;
403 struct pipe_context *ctx = &sctx->b;
404
405 if (ctx == sscreen->aux_context)
406 mtx_lock(&sscreen->aux_context_lock);
407
408 unsigned n = sctx->num_decompress_calls;
409 ctx->flush_resource(ctx, &tex->buffer.b.b);
410
411 /* Flush only if any fast clear elimination took place. */
412 if (n != sctx->num_decompress_calls)
413 ctx->flush(ctx, NULL, 0);
414
415 if (ctx == sscreen->aux_context)
416 mtx_unlock(&sscreen->aux_context_lock);
417 }
418
419 void si_texture_discard_cmask(struct si_screen *sscreen,
420 struct si_texture *tex)
421 {
422 if (!tex->cmask_buffer)
423 return;
424
425 assert(tex->buffer.b.b.nr_samples <= 1);
426
427 /* Disable CMASK. */
428 tex->cmask_base_address_reg = tex->buffer.gpu_address >> 8;
429 tex->dirty_level_mask = 0;
430
431 tex->cb_color_info &= ~S_028C70_FAST_CLEAR(1);
432
433 if (tex->cmask_buffer != &tex->buffer)
434 r600_resource_reference(&tex->cmask_buffer, NULL);
435
436 tex->cmask_buffer = NULL;
437
438 /* Notify all contexts about the change. */
439 p_atomic_inc(&sscreen->dirty_tex_counter);
440 p_atomic_inc(&sscreen->compressed_colortex_counter);
441 }
442
443 static bool si_can_disable_dcc(struct si_texture *tex)
444 {
445 /* We can't disable DCC if it can be written by another process. */
446 return tex->dcc_offset &&
447 (!tex->buffer.b.is_shared ||
448 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
449 }
450
451 static bool si_texture_discard_dcc(struct si_screen *sscreen,
452 struct si_texture *tex)
453 {
454 if (!si_can_disable_dcc(tex))
455 return false;
456
457 assert(tex->dcc_separate_buffer == NULL);
458
459 /* Disable DCC. */
460 tex->dcc_offset = 0;
461
462 /* Notify all contexts about the change. */
463 p_atomic_inc(&sscreen->dirty_tex_counter);
464 return true;
465 }
466
467 /**
468 * Disable DCC for the texture. (first decompress, then discard metadata).
469 *
470 * There is unresolved multi-context synchronization issue between
471 * screen::aux_context and the current context. If applications do this with
472 * multiple contexts, it's already undefined behavior for them and we don't
473 * have to worry about that. The scenario is:
474 *
475 * If context 1 disables DCC and context 2 has queued commands that write
476 * to the texture via CB with DCC enabled, and the order of operations is
477 * as follows:
478 * context 2 queues draw calls rendering to the texture, but doesn't flush
479 * context 1 disables DCC and flushes
480 * context 1 & 2 reset descriptors and FB state
481 * context 2 flushes (new compressed tiles written by the draw calls)
482 * context 1 & 2 read garbage, because DCC is disabled, yet there are
483 * compressed tiled
484 *
485 * \param sctx the current context if you have one, or rscreen->aux_context
486 * if you don't.
487 */
488 bool si_texture_disable_dcc(struct si_context *sctx,
489 struct si_texture *tex)
490 {
491 struct si_screen *sscreen = sctx->screen;
492
493 if (!si_can_disable_dcc(tex))
494 return false;
495
496 if (&sctx->b == sscreen->aux_context)
497 mtx_lock(&sscreen->aux_context_lock);
498
499 /* Decompress DCC. */
500 si_decompress_dcc(sctx, tex);
501 sctx->b.flush(&sctx->b, NULL, 0);
502
503 if (&sctx->b == sscreen->aux_context)
504 mtx_unlock(&sscreen->aux_context_lock);
505
506 return si_texture_discard_dcc(sscreen, tex);
507 }
508
509 static void si_reallocate_texture_inplace(struct si_context *sctx,
510 struct si_texture *tex,
511 unsigned new_bind_flag,
512 bool invalidate_storage)
513 {
514 struct pipe_screen *screen = sctx->b.screen;
515 struct si_texture *new_tex;
516 struct pipe_resource templ = tex->buffer.b.b;
517 unsigned i;
518
519 templ.bind |= new_bind_flag;
520
521 if (tex->buffer.b.is_shared)
522 return;
523
524 if (new_bind_flag == PIPE_BIND_LINEAR) {
525 if (tex->surface.is_linear)
526 return;
527
528 /* This fails with MSAA, depth, and compressed textures. */
529 if (si_choose_tiling(sctx->screen, &templ, false) !=
530 RADEON_SURF_MODE_LINEAR_ALIGNED)
531 return;
532 }
533
534 new_tex = (struct si_texture*)screen->resource_create(screen, &templ);
535 if (!new_tex)
536 return;
537
538 /* Copy the pixels to the new texture. */
539 if (!invalidate_storage) {
540 for (i = 0; i <= templ.last_level; i++) {
541 struct pipe_box box;
542
543 u_box_3d(0, 0, 0,
544 u_minify(templ.width0, i), u_minify(templ.height0, i),
545 util_num_layers(&templ, i), &box);
546
547 sctx->dma_copy(&sctx->b, &new_tex->buffer.b.b, i, 0, 0, 0,
548 &tex->buffer.b.b, i, &box);
549 }
550 }
551
552 if (new_bind_flag == PIPE_BIND_LINEAR) {
553 si_texture_discard_cmask(sctx->screen, tex);
554 si_texture_discard_dcc(sctx->screen, tex);
555 }
556
557 /* Replace the structure fields of tex. */
558 tex->buffer.b.b.bind = templ.bind;
559 pb_reference(&tex->buffer.buf, new_tex->buffer.buf);
560 tex->buffer.gpu_address = new_tex->buffer.gpu_address;
561 tex->buffer.vram_usage = new_tex->buffer.vram_usage;
562 tex->buffer.gart_usage = new_tex->buffer.gart_usage;
563 tex->buffer.bo_size = new_tex->buffer.bo_size;
564 tex->buffer.bo_alignment = new_tex->buffer.bo_alignment;
565 tex->buffer.domains = new_tex->buffer.domains;
566 tex->buffer.flags = new_tex->buffer.flags;
567
568 tex->surface = new_tex->surface;
569 tex->size = new_tex->size;
570 si_texture_reference(&tex->flushed_depth_texture,
571 new_tex->flushed_depth_texture);
572
573 tex->fmask_offset = new_tex->fmask_offset;
574 tex->cmask_offset = new_tex->cmask_offset;
575 tex->cmask_base_address_reg = new_tex->cmask_base_address_reg;
576
577 if (tex->cmask_buffer == &tex->buffer)
578 tex->cmask_buffer = NULL;
579 else
580 r600_resource_reference(&tex->cmask_buffer, NULL);
581
582 if (new_tex->cmask_buffer == &new_tex->buffer)
583 tex->cmask_buffer = &tex->buffer;
584 else
585 r600_resource_reference(&tex->cmask_buffer, new_tex->cmask_buffer);
586
587 tex->dcc_offset = new_tex->dcc_offset;
588 tex->cb_color_info = new_tex->cb_color_info;
589 memcpy(tex->color_clear_value, new_tex->color_clear_value,
590 sizeof(tex->color_clear_value));
591 tex->last_msaa_resolve_target_micro_mode = new_tex->last_msaa_resolve_target_micro_mode;
592
593 tex->htile_offset = new_tex->htile_offset;
594 tex->depth_clear_value = new_tex->depth_clear_value;
595 tex->dirty_level_mask = new_tex->dirty_level_mask;
596 tex->stencil_dirty_level_mask = new_tex->stencil_dirty_level_mask;
597 tex->db_render_format = new_tex->db_render_format;
598 tex->stencil_clear_value = new_tex->stencil_clear_value;
599 tex->tc_compatible_htile = new_tex->tc_compatible_htile;
600 tex->depth_cleared = new_tex->depth_cleared;
601 tex->stencil_cleared = new_tex->stencil_cleared;
602 tex->upgraded_depth = new_tex->upgraded_depth;
603 tex->db_compatible = new_tex->db_compatible;
604 tex->can_sample_z = new_tex->can_sample_z;
605 tex->can_sample_s = new_tex->can_sample_s;
606
607 tex->separate_dcc_dirty = new_tex->separate_dcc_dirty;
608 tex->dcc_gather_statistics = new_tex->dcc_gather_statistics;
609 r600_resource_reference(&tex->dcc_separate_buffer,
610 new_tex->dcc_separate_buffer);
611 r600_resource_reference(&tex->last_dcc_separate_buffer,
612 new_tex->last_dcc_separate_buffer);
613
614 if (new_bind_flag == PIPE_BIND_LINEAR) {
615 assert(!tex->htile_offset);
616 assert(!tex->cmask_buffer);
617 assert(!tex->surface.fmask_size);
618 assert(!tex->dcc_offset);
619 assert(!tex->is_depth);
620 }
621
622 si_texture_reference(&new_tex, NULL);
623
624 p_atomic_inc(&sctx->screen->dirty_tex_counter);
625 }
626
627 static uint32_t si_get_bo_metadata_word1(struct si_screen *sscreen)
628 {
629 return (ATI_VENDOR_ID << 16) | sscreen->info.pci_id;
630 }
631
632 static void si_query_opaque_metadata(struct si_screen *sscreen,
633 struct si_texture *tex,
634 struct radeon_bo_metadata *md)
635 {
636 struct pipe_resource *res = &tex->buffer.b.b;
637 static const unsigned char swizzle[] = {
638 PIPE_SWIZZLE_X,
639 PIPE_SWIZZLE_Y,
640 PIPE_SWIZZLE_Z,
641 PIPE_SWIZZLE_W
642 };
643 uint32_t desc[8], i;
644 bool is_array = util_texture_is_array(res->target);
645
646 if (!sscreen->info.has_bo_metadata)
647 return;
648
649 assert(tex->dcc_separate_buffer == NULL);
650 assert(tex->surface.fmask_size == 0);
651
652 /* Metadata image format format version 1:
653 * [0] = 1 (metadata format identifier)
654 * [1] = (VENDOR_ID << 16) | PCI_ID
655 * [2:9] = image descriptor for the whole resource
656 * [2] is always 0, because the base address is cleared
657 * [9] is the DCC offset bits [39:8] from the beginning of
658 * the buffer
659 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
660 */
661
662 md->metadata[0] = 1; /* metadata image format version 1 */
663
664 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
665 md->metadata[1] = si_get_bo_metadata_word1(sscreen);
666
667 si_make_texture_descriptor(sscreen, tex, true,
668 res->target, res->format,
669 swizzle, 0, res->last_level, 0,
670 is_array ? res->array_size - 1 : 0,
671 res->width0, res->height0, res->depth0,
672 desc, NULL);
673
674 si_set_mutable_tex_desc_fields(sscreen, tex, &tex->surface.u.legacy.level[0],
675 0, 0, tex->surface.blk_w, false, desc);
676
677 /* Clear the base address and set the relative DCC offset. */
678 desc[0] = 0;
679 desc[1] &= C_008F14_BASE_ADDRESS_HI;
680 desc[7] = tex->dcc_offset >> 8;
681
682 /* Dwords [2:9] contain the image descriptor. */
683 memcpy(&md->metadata[2], desc, sizeof(desc));
684 md->size_metadata = 10 * 4;
685
686 /* Dwords [10:..] contain the mipmap level offsets. */
687 if (sscreen->info.chip_class <= VI) {
688 for (i = 0; i <= res->last_level; i++)
689 md->metadata[10+i] = tex->surface.u.legacy.level[i].offset >> 8;
690
691 md->size_metadata += (1 + res->last_level) * 4;
692 }
693 }
694
695 static void si_apply_opaque_metadata(struct si_screen *sscreen,
696 struct si_texture *tex,
697 struct radeon_bo_metadata *md)
698 {
699 uint32_t *desc = &md->metadata[2];
700
701 if (sscreen->info.chip_class < VI)
702 return;
703
704 /* Return if DCC is enabled. The texture should be set up with it
705 * already.
706 */
707 if (md->size_metadata >= 10 * 4 && /* at least 2(header) + 8(desc) dwords */
708 md->metadata[0] != 0 &&
709 md->metadata[1] == si_get_bo_metadata_word1(sscreen) &&
710 G_008F28_COMPRESSION_EN(desc[6])) {
711 tex->dcc_offset = (uint64_t)desc[7] << 8;
712 return;
713 }
714
715 /* Disable DCC. These are always set by texture_from_handle and must
716 * be cleared here.
717 */
718 tex->dcc_offset = 0;
719 }
720
721 static boolean si_texture_get_handle(struct pipe_screen* screen,
722 struct pipe_context *ctx,
723 struct pipe_resource *resource,
724 struct winsys_handle *whandle,
725 unsigned usage)
726 {
727 struct si_screen *sscreen = (struct si_screen*)screen;
728 struct si_context *sctx;
729 struct r600_resource *res = r600_resource(resource);
730 struct si_texture *tex = (struct si_texture*)resource;
731 struct radeon_bo_metadata metadata;
732 bool update_metadata = false;
733 unsigned stride, offset, slice_size;
734 bool flush = false;
735
736 ctx = threaded_context_unwrap_sync(ctx);
737 sctx = (struct si_context*)(ctx ? ctx : sscreen->aux_context);
738
739 if (resource->target != PIPE_BUFFER) {
740 /* This is not supported now, but it might be required for OpenCL
741 * interop in the future.
742 */
743 if (resource->nr_samples > 1 || tex->is_depth)
744 return false;
745
746 /* Move a suballocated texture into a non-suballocated allocation. */
747 if (sscreen->ws->buffer_is_suballocated(res->buf) ||
748 tex->surface.tile_swizzle ||
749 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
750 sscreen->info.has_local_buffers &&
751 whandle->type != WINSYS_HANDLE_TYPE_KMS)) {
752 assert(!res->b.is_shared);
753 si_reallocate_texture_inplace(sctx, tex,
754 PIPE_BIND_SHARED, false);
755 flush = true;
756 assert(res->b.b.bind & PIPE_BIND_SHARED);
757 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
758 assert(!(res->flags & RADEON_FLAG_NO_INTERPROCESS_SHARING));
759 assert(tex->surface.tile_swizzle == 0);
760 }
761
762 /* Since shader image stores don't support DCC on VI,
763 * disable it for external clients that want write
764 * access.
765 */
766 if (usage & PIPE_HANDLE_USAGE_SHADER_WRITE && tex->dcc_offset) {
767 if (si_texture_disable_dcc(sctx, tex)) {
768 update_metadata = true;
769 /* si_texture_disable_dcc flushes the context */
770 flush = false;
771 }
772 }
773
774 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
775 (tex->cmask_buffer || tex->dcc_offset)) {
776 /* Eliminate fast clear (both CMASK and DCC) */
777 si_eliminate_fast_color_clear(sctx, tex);
778 /* eliminate_fast_color_clear flushes the context */
779 flush = false;
780
781 /* Disable CMASK if flush_resource isn't going
782 * to be called.
783 */
784 if (tex->cmask_buffer)
785 si_texture_discard_cmask(sscreen, tex);
786 }
787
788 /* Set metadata. */
789 if (!res->b.is_shared || update_metadata) {
790 si_texture_init_metadata(sscreen, tex, &metadata);
791 si_query_opaque_metadata(sscreen, tex, &metadata);
792
793 sscreen->ws->buffer_set_metadata(res->buf, &metadata);
794 }
795
796 if (sscreen->info.chip_class >= GFX9) {
797 offset = tex->surface.u.gfx9.surf_offset;
798 stride = tex->surface.u.gfx9.surf_pitch *
799 tex->surface.bpe;
800 slice_size = tex->surface.u.gfx9.surf_slice_size;
801 } else {
802 offset = tex->surface.u.legacy.level[0].offset;
803 stride = tex->surface.u.legacy.level[0].nblk_x *
804 tex->surface.bpe;
805 slice_size = (uint64_t)tex->surface.u.legacy.level[0].slice_size_dw * 4;
806 }
807 } else {
808 /* Buffer exports are for the OpenCL interop. */
809 /* Move a suballocated buffer into a non-suballocated allocation. */
810 if (sscreen->ws->buffer_is_suballocated(res->buf) ||
811 /* A DMABUF export always fails if the BO is local. */
812 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
813 sscreen->info.has_local_buffers)) {
814 assert(!res->b.is_shared);
815
816 /* Allocate a new buffer with PIPE_BIND_SHARED. */
817 struct pipe_resource templ = res->b.b;
818 templ.bind |= PIPE_BIND_SHARED;
819
820 struct pipe_resource *newb =
821 screen->resource_create(screen, &templ);
822 if (!newb)
823 return false;
824
825 /* Copy the old buffer contents to the new one. */
826 struct pipe_box box;
827 u_box_1d(0, newb->width0, &box);
828 sctx->b.resource_copy_region(&sctx->b, newb, 0, 0, 0, 0,
829 &res->b.b, 0, &box);
830 flush = true;
831 /* Move the new buffer storage to the old pipe_resource. */
832 si_replace_buffer_storage(&sctx->b, &res->b.b, newb);
833 pipe_resource_reference(&newb, NULL);
834
835 assert(res->b.b.bind & PIPE_BIND_SHARED);
836 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
837 }
838
839 /* Buffers */
840 offset = 0;
841 stride = 0;
842 slice_size = 0;
843 }
844
845 if (flush)
846 sctx->b.flush(&sctx->b, NULL, 0);
847
848 if (res->b.is_shared) {
849 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
850 * doesn't set it.
851 */
852 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
853 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
854 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
855 } else {
856 res->b.is_shared = true;
857 res->external_usage = usage;
858 }
859
860 return sscreen->ws->buffer_get_handle(res->buf, stride, offset,
861 slice_size, whandle);
862 }
863
864 static void si_texture_destroy(struct pipe_screen *screen,
865 struct pipe_resource *ptex)
866 {
867 struct si_texture *tex = (struct si_texture*)ptex;
868 struct r600_resource *resource = &tex->buffer;
869
870 si_texture_reference(&tex->flushed_depth_texture, NULL);
871
872 if (tex->cmask_buffer != &tex->buffer) {
873 r600_resource_reference(&tex->cmask_buffer, NULL);
874 }
875 pb_reference(&resource->buf, NULL);
876 r600_resource_reference(&tex->dcc_separate_buffer, NULL);
877 r600_resource_reference(&tex->last_dcc_separate_buffer, NULL);
878 FREE(tex);
879 }
880
881 static const struct u_resource_vtbl si_texture_vtbl;
882
883 static void si_texture_get_htile_size(struct si_screen *sscreen,
884 struct si_texture *tex)
885 {
886 unsigned cl_width, cl_height, width, height;
887 unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
888 unsigned num_pipes = sscreen->info.num_tile_pipes;
889
890 assert(sscreen->info.chip_class <= VI);
891
892 tex->surface.htile_size = 0;
893
894 if (tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
895 !sscreen->info.htile_cmask_support_1d_tiling)
896 return;
897
898 /* Overalign HTILE on P2 configs to work around GPU hangs in
899 * piglit/depthstencil-render-miplevels 585.
900 *
901 * This has been confirmed to help Kabini & Stoney, where the hangs
902 * are always reproducible. I think I have seen the test hang
903 * on Carrizo too, though it was very rare there.
904 */
905 if (sscreen->info.chip_class >= CIK && num_pipes < 4)
906 num_pipes = 4;
907
908 switch (num_pipes) {
909 case 1:
910 cl_width = 32;
911 cl_height = 16;
912 break;
913 case 2:
914 cl_width = 32;
915 cl_height = 32;
916 break;
917 case 4:
918 cl_width = 64;
919 cl_height = 32;
920 break;
921 case 8:
922 cl_width = 64;
923 cl_height = 64;
924 break;
925 case 16:
926 cl_width = 128;
927 cl_height = 64;
928 break;
929 default:
930 assert(0);
931 return;
932 }
933
934 width = align(tex->surface.u.legacy.level[0].nblk_x, cl_width * 8);
935 height = align(tex->surface.u.legacy.level[0].nblk_y, cl_height * 8);
936
937 slice_elements = (width * height) / (8 * 8);
938 slice_bytes = slice_elements * 4;
939
940 pipe_interleave_bytes = sscreen->info.pipe_interleave_bytes;
941 base_align = num_pipes * pipe_interleave_bytes;
942
943 tex->surface.htile_alignment = base_align;
944 tex->surface.htile_size =
945 util_num_layers(&tex->buffer.b.b, 0) *
946 align(slice_bytes, base_align);
947 }
948
949 static void si_texture_allocate_htile(struct si_screen *sscreen,
950 struct si_texture *tex)
951 {
952 if (sscreen->info.chip_class <= VI && !tex->tc_compatible_htile)
953 si_texture_get_htile_size(sscreen, tex);
954
955 if (!tex->surface.htile_size)
956 return;
957
958 tex->htile_offset = align(tex->size, tex->surface.htile_alignment);
959 tex->size = tex->htile_offset + tex->surface.htile_size;
960 }
961
962 void si_print_texture_info(struct si_screen *sscreen,
963 struct si_texture *tex, struct u_log_context *log)
964 {
965 int i;
966
967 /* Common parameters. */
968 u_log_printf(log, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
969 "blk_h=%u, array_size=%u, last_level=%u, "
970 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
971 tex->buffer.b.b.width0, tex->buffer.b.b.height0,
972 tex->buffer.b.b.depth0, tex->surface.blk_w,
973 tex->surface.blk_h,
974 tex->buffer.b.b.array_size, tex->buffer.b.b.last_level,
975 tex->surface.bpe, tex->buffer.b.b.nr_samples,
976 tex->surface.flags, util_format_short_name(tex->buffer.b.b.format));
977
978 if (sscreen->info.chip_class >= GFX9) {
979 u_log_printf(log, " Surf: size=%"PRIu64", slice_size=%"PRIu64", "
980 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
981 tex->surface.surf_size,
982 tex->surface.u.gfx9.surf_slice_size,
983 tex->surface.surf_alignment,
984 tex->surface.u.gfx9.surf.swizzle_mode,
985 tex->surface.u.gfx9.surf.epitch,
986 tex->surface.u.gfx9.surf_pitch);
987
988 if (tex->surface.fmask_size) {
989 u_log_printf(log, " FMASK: offset=%"PRIu64", size=%"PRIu64", "
990 "alignment=%u, swmode=%u, epitch=%u\n",
991 tex->fmask_offset,
992 tex->surface.fmask_size,
993 tex->surface.fmask_alignment,
994 tex->surface.u.gfx9.fmask.swizzle_mode,
995 tex->surface.u.gfx9.fmask.epitch);
996 }
997
998 if (tex->cmask_buffer) {
999 u_log_printf(log, " CMask: offset=%"PRIu64", size=%u, "
1000 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1001 tex->cmask_offset,
1002 tex->surface.cmask_size,
1003 tex->surface.cmask_alignment,
1004 tex->surface.u.gfx9.cmask.rb_aligned,
1005 tex->surface.u.gfx9.cmask.pipe_aligned);
1006 }
1007
1008 if (tex->htile_offset) {
1009 u_log_printf(log, " HTile: offset=%"PRIu64", size=%u, alignment=%u, "
1010 "rb_aligned=%u, pipe_aligned=%u\n",
1011 tex->htile_offset,
1012 tex->surface.htile_size,
1013 tex->surface.htile_alignment,
1014 tex->surface.u.gfx9.htile.rb_aligned,
1015 tex->surface.u.gfx9.htile.pipe_aligned);
1016 }
1017
1018 if (tex->dcc_offset) {
1019 u_log_printf(log, " DCC: offset=%"PRIu64", size=%u, "
1020 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1021 tex->dcc_offset, tex->surface.dcc_size,
1022 tex->surface.dcc_alignment,
1023 tex->surface.u.gfx9.dcc_pitch_max,
1024 tex->surface.num_dcc_levels);
1025 }
1026
1027 if (tex->surface.u.gfx9.stencil_offset) {
1028 u_log_printf(log, " Stencil: offset=%"PRIu64", swmode=%u, epitch=%u\n",
1029 tex->surface.u.gfx9.stencil_offset,
1030 tex->surface.u.gfx9.stencil.swizzle_mode,
1031 tex->surface.u.gfx9.stencil.epitch);
1032 }
1033 return;
1034 }
1035
1036 u_log_printf(log, " Layout: size=%"PRIu64", alignment=%u, bankw=%u, "
1037 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1038 tex->surface.surf_size, tex->surface.surf_alignment, tex->surface.u.legacy.bankw,
1039 tex->surface.u.legacy.bankh, tex->surface.u.legacy.num_banks, tex->surface.u.legacy.mtilea,
1040 tex->surface.u.legacy.tile_split, tex->surface.u.legacy.pipe_config,
1041 (tex->surface.flags & RADEON_SURF_SCANOUT) != 0);
1042
1043 if (tex->surface.fmask_size)
1044 u_log_printf(log, " FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
1045 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1046 tex->fmask_offset, tex->surface.fmask_size, tex->surface.fmask_alignment,
1047 tex->surface.u.legacy.fmask.pitch_in_pixels,
1048 tex->surface.u.legacy.fmask.bankh,
1049 tex->surface.u.legacy.fmask.slice_tile_max,
1050 tex->surface.u.legacy.fmask.tiling_index);
1051
1052 if (tex->cmask_buffer)
1053 u_log_printf(log, " CMask: offset=%"PRIu64", size=%u, alignment=%u, "
1054 "slice_tile_max=%u\n",
1055 tex->cmask_offset, tex->surface.cmask_size, tex->surface.cmask_alignment,
1056 tex->surface.u.legacy.cmask_slice_tile_max);
1057
1058 if (tex->htile_offset)
1059 u_log_printf(log, " HTile: offset=%"PRIu64", size=%u, "
1060 "alignment=%u, TC_compatible = %u\n",
1061 tex->htile_offset, tex->surface.htile_size,
1062 tex->surface.htile_alignment,
1063 tex->tc_compatible_htile);
1064
1065 if (tex->dcc_offset) {
1066 u_log_printf(log, " DCC: offset=%"PRIu64", size=%u, alignment=%u\n",
1067 tex->dcc_offset, tex->surface.dcc_size,
1068 tex->surface.dcc_alignment);
1069 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
1070 u_log_printf(log, " DCCLevel[%i]: enabled=%u, offset=%u, "
1071 "fast_clear_size=%u\n",
1072 i, i < tex->surface.num_dcc_levels,
1073 tex->surface.u.legacy.level[i].dcc_offset,
1074 tex->surface.u.legacy.level[i].dcc_fast_clear_size);
1075 }
1076
1077 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
1078 u_log_printf(log, " Level[%i]: offset=%"PRIu64", slice_size=%"PRIu64", "
1079 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1080 "mode=%u, tiling_index = %u\n",
1081 i, tex->surface.u.legacy.level[i].offset,
1082 (uint64_t)tex->surface.u.legacy.level[i].slice_size_dw * 4,
1083 u_minify(tex->buffer.b.b.width0, i),
1084 u_minify(tex->buffer.b.b.height0, i),
1085 u_minify(tex->buffer.b.b.depth0, i),
1086 tex->surface.u.legacy.level[i].nblk_x,
1087 tex->surface.u.legacy.level[i].nblk_y,
1088 tex->surface.u.legacy.level[i].mode,
1089 tex->surface.u.legacy.tiling_index[i]);
1090
1091 if (tex->surface.has_stencil) {
1092 u_log_printf(log, " StencilLayout: tilesplit=%u\n",
1093 tex->surface.u.legacy.stencil_tile_split);
1094 for (i = 0; i <= tex->buffer.b.b.last_level; i++) {
1095 u_log_printf(log, " StencilLevel[%i]: offset=%"PRIu64", "
1096 "slice_size=%"PRIu64", npix_x=%u, "
1097 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1098 "mode=%u, tiling_index = %u\n",
1099 i, tex->surface.u.legacy.stencil_level[i].offset,
1100 (uint64_t)tex->surface.u.legacy.stencil_level[i].slice_size_dw * 4,
1101 u_minify(tex->buffer.b.b.width0, i),
1102 u_minify(tex->buffer.b.b.height0, i),
1103 u_minify(tex->buffer.b.b.depth0, i),
1104 tex->surface.u.legacy.stencil_level[i].nblk_x,
1105 tex->surface.u.legacy.stencil_level[i].nblk_y,
1106 tex->surface.u.legacy.stencil_level[i].mode,
1107 tex->surface.u.legacy.stencil_tiling_index[i]);
1108 }
1109 }
1110 }
1111
1112 /* Common processing for si_texture_create and si_texture_from_handle */
1113 static struct si_texture *
1114 si_texture_create_object(struct pipe_screen *screen,
1115 const struct pipe_resource *base,
1116 struct pb_buffer *buf,
1117 struct radeon_surf *surface)
1118 {
1119 struct si_texture *tex;
1120 struct r600_resource *resource;
1121 struct si_screen *sscreen = (struct si_screen*)screen;
1122
1123 tex = CALLOC_STRUCT(si_texture);
1124 if (!tex)
1125 return NULL;
1126
1127 resource = &tex->buffer;
1128 resource->b.b = *base;
1129 resource->b.b.next = NULL;
1130 resource->b.vtbl = &si_texture_vtbl;
1131 pipe_reference_init(&resource->b.b.reference, 1);
1132 resource->b.b.screen = screen;
1133
1134 /* don't include stencil-only formats which we don't support for rendering */
1135 tex->is_depth = util_format_has_depth(util_format_description(tex->buffer.b.b.format));
1136
1137 tex->surface = *surface;
1138 tex->size = tex->surface.surf_size;
1139
1140 tex->tc_compatible_htile = tex->surface.htile_size != 0 &&
1141 (tex->surface.flags &
1142 RADEON_SURF_TC_COMPATIBLE_HTILE);
1143
1144 /* TC-compatible HTILE:
1145 * - VI only supports Z32_FLOAT.
1146 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1147 if (tex->tc_compatible_htile) {
1148 if (sscreen->info.chip_class >= GFX9 &&
1149 base->format == PIPE_FORMAT_Z16_UNORM)
1150 tex->db_render_format = base->format;
1151 else {
1152 tex->db_render_format = PIPE_FORMAT_Z32_FLOAT;
1153 tex->upgraded_depth = base->format != PIPE_FORMAT_Z32_FLOAT &&
1154 base->format != PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
1155 }
1156 } else {
1157 tex->db_render_format = base->format;
1158 }
1159
1160 /* Applies to GCN. */
1161 tex->last_msaa_resolve_target_micro_mode = tex->surface.micro_tile_mode;
1162
1163 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1164 * between frames, so the only thing that can enable separate DCC
1165 * with DRI2 is multiple slow clears within a frame.
1166 */
1167 tex->ps_draw_ratio = 0;
1168
1169 if (tex->is_depth) {
1170 if (sscreen->info.chip_class >= GFX9) {
1171 tex->can_sample_z = true;
1172 tex->can_sample_s = true;
1173 } else {
1174 tex->can_sample_z = !tex->surface.u.legacy.depth_adjusted;
1175 tex->can_sample_s = !tex->surface.u.legacy.stencil_adjusted;
1176 }
1177
1178 if (!(base->flags & (SI_RESOURCE_FLAG_TRANSFER |
1179 SI_RESOURCE_FLAG_FLUSHED_DEPTH))) {
1180 tex->db_compatible = true;
1181
1182 if (!(sscreen->debug_flags & DBG(NO_HYPERZ)))
1183 si_texture_allocate_htile(sscreen, tex);
1184 }
1185 } else {
1186 if (base->nr_samples > 1 &&
1187 !buf &&
1188 !(sscreen->debug_flags & DBG(NO_FMASK))) {
1189 /* Allocate FMASK. */
1190 tex->fmask_offset = align64(tex->size,
1191 tex->surface.fmask_alignment);
1192 tex->size = tex->fmask_offset + tex->surface.fmask_size;
1193
1194 /* Allocate CMASK. */
1195 tex->cmask_offset = align64(tex->size, tex->surface.cmask_alignment);
1196 tex->size = tex->cmask_offset + tex->surface.cmask_size;
1197 tex->cb_color_info |= S_028C70_FAST_CLEAR(1);
1198 tex->cmask_buffer = &tex->buffer;
1199
1200 if (!tex->surface.fmask_size || !tex->surface.cmask_size) {
1201 FREE(tex);
1202 return NULL;
1203 }
1204 }
1205
1206 /* Shared textures must always set up DCC here.
1207 * If it's not present, it will be disabled by
1208 * apply_opaque_metadata later.
1209 */
1210 if (tex->surface.dcc_size &&
1211 (buf || !(sscreen->debug_flags & DBG(NO_DCC))) &&
1212 !(tex->surface.flags & RADEON_SURF_SCANOUT)) {
1213 /* Reserve space for the DCC buffer. */
1214 tex->dcc_offset = align64(tex->size, tex->surface.dcc_alignment);
1215 tex->size = tex->dcc_offset + tex->surface.dcc_size;
1216 }
1217 }
1218
1219 /* Now create the backing buffer. */
1220 if (!buf) {
1221 si_init_resource_fields(sscreen, resource, tex->size,
1222 tex->surface.surf_alignment);
1223
1224 if (!si_alloc_resource(sscreen, resource)) {
1225 FREE(tex);
1226 return NULL;
1227 }
1228 } else {
1229 resource->buf = buf;
1230 resource->gpu_address = sscreen->ws->buffer_get_virtual_address(resource->buf);
1231 resource->bo_size = buf->size;
1232 resource->bo_alignment = buf->alignment;
1233 resource->domains = sscreen->ws->buffer_get_initial_domain(resource->buf);
1234 if (resource->domains & RADEON_DOMAIN_VRAM)
1235 resource->vram_usage = buf->size;
1236 else if (resource->domains & RADEON_DOMAIN_GTT)
1237 resource->gart_usage = buf->size;
1238 }
1239
1240 if (tex->cmask_buffer) {
1241 /* Initialize the cmask to 0xCC (= compressed state). */
1242 si_screen_clear_buffer(sscreen, &tex->cmask_buffer->b.b,
1243 tex->cmask_offset, tex->surface.cmask_size,
1244 0xCCCCCCCC);
1245 }
1246 if (tex->htile_offset) {
1247 uint32_t clear_value = 0;
1248
1249 if (sscreen->info.chip_class >= GFX9 || tex->tc_compatible_htile)
1250 clear_value = 0x0000030F;
1251
1252 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1253 tex->htile_offset,
1254 tex->surface.htile_size,
1255 clear_value);
1256 }
1257
1258 /* Initialize DCC only if the texture is not being imported. */
1259 if (!buf && tex->dcc_offset) {
1260 si_screen_clear_buffer(sscreen, &tex->buffer.b.b,
1261 tex->dcc_offset,
1262 tex->surface.dcc_size,
1263 0xFFFFFFFF);
1264 }
1265
1266 /* Initialize the CMASK base register value. */
1267 tex->cmask_base_address_reg =
1268 (tex->buffer.gpu_address + tex->cmask_offset) >> 8;
1269
1270 if (sscreen->debug_flags & DBG(VM)) {
1271 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1272 tex->buffer.gpu_address,
1273 tex->buffer.gpu_address + tex->buffer.buf->size,
1274 base->width0, base->height0, util_num_layers(base, 0), base->last_level+1,
1275 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1276 }
1277
1278 if (sscreen->debug_flags & DBG(TEX)) {
1279 puts("Texture:");
1280 struct u_log_context log;
1281 u_log_context_init(&log);
1282 si_print_texture_info(sscreen, tex, &log);
1283 u_log_new_page_print(&log, stdout);
1284 fflush(stdout);
1285 u_log_context_destroy(&log);
1286 }
1287
1288 return tex;
1289 }
1290
1291 static enum radeon_surf_mode
1292 si_choose_tiling(struct si_screen *sscreen,
1293 const struct pipe_resource *templ, bool tc_compatible_htile)
1294 {
1295 const struct util_format_description *desc = util_format_description(templ->format);
1296 bool force_tiling = templ->flags & SI_RESOURCE_FLAG_FORCE_TILING;
1297 bool is_depth_stencil = util_format_is_depth_or_stencil(templ->format) &&
1298 !(templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH);
1299
1300 /* MSAA resources must be 2D tiled. */
1301 if (templ->nr_samples > 1)
1302 return RADEON_SURF_MODE_2D;
1303
1304 /* Transfer resources should be linear. */
1305 if (templ->flags & SI_RESOURCE_FLAG_TRANSFER)
1306 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1307
1308 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1309 * which requires 2D tiling.
1310 */
1311 if (sscreen->info.chip_class == VI && tc_compatible_htile)
1312 return RADEON_SURF_MODE_2D;
1313
1314 /* Handle common candidates for the linear mode.
1315 * Compressed textures and DB surfaces must always be tiled.
1316 */
1317 if (!force_tiling &&
1318 !is_depth_stencil &&
1319 !util_format_is_compressed(templ->format)) {
1320 if (sscreen->debug_flags & DBG(NO_TILING))
1321 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1322
1323 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1324 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1325 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1326
1327 /* Cursors are linear on SI.
1328 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1329 if (templ->bind & PIPE_BIND_CURSOR)
1330 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1331
1332 if (templ->bind & PIPE_BIND_LINEAR)
1333 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1334
1335 /* Textures with a very small height are recommended to be linear. */
1336 if (templ->target == PIPE_TEXTURE_1D ||
1337 templ->target == PIPE_TEXTURE_1D_ARRAY ||
1338 /* Only very thin and long 2D textures should benefit from
1339 * linear_aligned. */
1340 (templ->width0 > 8 && templ->height0 <= 2))
1341 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1342
1343 /* Textures likely to be mapped often. */
1344 if (templ->usage == PIPE_USAGE_STAGING ||
1345 templ->usage == PIPE_USAGE_STREAM)
1346 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1347 }
1348
1349 /* Make small textures 1D tiled. */
1350 if (templ->width0 <= 16 || templ->height0 <= 16 ||
1351 (sscreen->debug_flags & DBG(NO_2D_TILING)))
1352 return RADEON_SURF_MODE_1D;
1353
1354 /* The allocator will switch to 1D if needed. */
1355 return RADEON_SURF_MODE_2D;
1356 }
1357
1358 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
1359 const struct pipe_resource *templ)
1360 {
1361 struct si_screen *sscreen = (struct si_screen*)screen;
1362 bool is_zs = util_format_is_depth_or_stencil(templ->format);
1363
1364 if (templ->nr_samples >= 2) {
1365 /* This is hackish (overwriting the const pipe_resource template),
1366 * but should be harmless and state trackers can also see
1367 * the overriden number of samples in the created pipe_resource.
1368 */
1369 if (is_zs && sscreen->eqaa_force_z_samples) {
1370 ((struct pipe_resource*)templ)->nr_samples =
1371 ((struct pipe_resource*)templ)->nr_storage_samples =
1372 sscreen->eqaa_force_z_samples;
1373 } else if (!is_zs && sscreen->eqaa_force_color_samples) {
1374 ((struct pipe_resource*)templ)->nr_samples =
1375 sscreen->eqaa_force_coverage_samples;
1376 ((struct pipe_resource*)templ)->nr_storage_samples =
1377 sscreen->eqaa_force_color_samples;
1378 }
1379 }
1380
1381 struct radeon_surf surface = {0};
1382 bool is_flushed_depth = templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH;
1383 bool tc_compatible_htile =
1384 sscreen->info.chip_class >= VI &&
1385 /* There are issues with TC-compatible HTILE on Tonga (and
1386 * Iceland is the same design), and documented bug workarounds
1387 * don't help. For example, this fails:
1388 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1389 */
1390 sscreen->info.family != CHIP_TONGA &&
1391 sscreen->info.family != CHIP_ICELAND &&
1392 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) &&
1393 !(sscreen->debug_flags & DBG(NO_HYPERZ)) &&
1394 !is_flushed_depth &&
1395 templ->nr_samples <= 1 && /* TC-compat HTILE is less efficient with MSAA */
1396 is_zs;
1397 int r;
1398
1399 r = si_init_surface(sscreen, &surface, templ,
1400 si_choose_tiling(sscreen, templ, tc_compatible_htile),
1401 0, 0, false, false, is_flushed_depth,
1402 tc_compatible_htile);
1403 if (r) {
1404 return NULL;
1405 }
1406
1407 return (struct pipe_resource *)
1408 si_texture_create_object(screen, templ, NULL, &surface);
1409 }
1410
1411 static struct pipe_resource *si_texture_from_winsys_buffer(struct si_screen *sscreen,
1412 const struct pipe_resource *templ,
1413 struct pb_buffer *buf,
1414 unsigned stride,
1415 unsigned offset,
1416 unsigned usage,
1417 bool dedicated)
1418 {
1419 enum radeon_surf_mode array_mode;
1420 struct radeon_surf surface = {};
1421 struct radeon_bo_metadata metadata = {};
1422 struct si_texture *tex;
1423 bool is_scanout;
1424 int r;
1425
1426 if (dedicated) {
1427 sscreen->ws->buffer_get_metadata(buf, &metadata);
1428 si_surface_import_metadata(sscreen, &surface, &metadata,
1429 &array_mode, &is_scanout);
1430 } else {
1431 /**
1432 * The bo metadata is unset for un-dedicated images. So we fall
1433 * back to linear. See answer to question 5 of the
1434 * VK_KHX_external_memory spec for some details.
1435 *
1436 * It is possible that this case isn't going to work if the
1437 * surface pitch isn't correctly aligned by default.
1438 *
1439 * In order to support it correctly we require multi-image
1440 * metadata to be syncrhonized between radv and radeonsi. The
1441 * semantics of associating multiple image metadata to a memory
1442 * object on the vulkan export side are not concretely defined
1443 * either.
1444 *
1445 * All the use cases we are aware of at the moment for memory
1446 * objects use dedicated allocations. So lets keep the initial
1447 * implementation simple.
1448 *
1449 * A possible alternative is to attempt to reconstruct the
1450 * tiling information when the TexParameter TEXTURE_TILING_EXT
1451 * is set.
1452 */
1453 array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1454 is_scanout = false;
1455 }
1456
1457 r = si_init_surface(sscreen, &surface, templ,
1458 array_mode, stride, offset, true, is_scanout,
1459 false, false);
1460 if (r)
1461 return NULL;
1462
1463 tex = si_texture_create_object(&sscreen->b, templ, buf, &surface);
1464 if (!tex)
1465 return NULL;
1466
1467 tex->buffer.b.is_shared = true;
1468 tex->buffer.external_usage = usage;
1469
1470 si_apply_opaque_metadata(sscreen, tex, &metadata);
1471
1472 assert(tex->surface.tile_swizzle == 0);
1473 return &tex->buffer.b.b;
1474 }
1475
1476 static struct pipe_resource *si_texture_from_handle(struct pipe_screen *screen,
1477 const struct pipe_resource *templ,
1478 struct winsys_handle *whandle,
1479 unsigned usage)
1480 {
1481 struct si_screen *sscreen = (struct si_screen*)screen;
1482 struct pb_buffer *buf = NULL;
1483 unsigned stride = 0, offset = 0;
1484
1485 /* Support only 2D textures without mipmaps */
1486 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
1487 templ->depth0 != 1 || templ->last_level != 0)
1488 return NULL;
1489
1490 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle,
1491 sscreen->info.max_alignment,
1492 &stride, &offset);
1493 if (!buf)
1494 return NULL;
1495
1496 return si_texture_from_winsys_buffer(sscreen, templ, buf, stride,
1497 offset, usage, true);
1498 }
1499
1500 bool si_init_flushed_depth_texture(struct pipe_context *ctx,
1501 struct pipe_resource *texture,
1502 struct si_texture **staging)
1503 {
1504 struct si_texture *tex = (struct si_texture*)texture;
1505 struct pipe_resource resource;
1506 struct si_texture **flushed_depth_texture = staging ?
1507 staging : &tex->flushed_depth_texture;
1508 enum pipe_format pipe_format = texture->format;
1509
1510 if (!staging) {
1511 if (tex->flushed_depth_texture)
1512 return true; /* it's ready */
1513
1514 if (!tex->can_sample_z && tex->can_sample_s) {
1515 switch (pipe_format) {
1516 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1517 /* Save memory by not allocating the S plane. */
1518 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1519 break;
1520 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1521 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1522 /* Save memory bandwidth by not copying the
1523 * stencil part during flush.
1524 *
1525 * This potentially increases memory bandwidth
1526 * if an application uses both Z and S texturing
1527 * simultaneously (a flushed Z24S8 texture
1528 * would be stored compactly), but how often
1529 * does that really happen?
1530 */
1531 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1532 break;
1533 default:;
1534 }
1535 } else if (!tex->can_sample_s && tex->can_sample_z) {
1536 assert(util_format_has_stencil(util_format_description(pipe_format)));
1537
1538 /* DB->CB copies to an 8bpp surface don't work. */
1539 pipe_format = PIPE_FORMAT_X24S8_UINT;
1540 }
1541 }
1542
1543 memset(&resource, 0, sizeof(resource));
1544 resource.target = texture->target;
1545 resource.format = pipe_format;
1546 resource.width0 = texture->width0;
1547 resource.height0 = texture->height0;
1548 resource.depth0 = texture->depth0;
1549 resource.array_size = texture->array_size;
1550 resource.last_level = texture->last_level;
1551 resource.nr_samples = texture->nr_samples;
1552 resource.usage = staging ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1553 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1554 resource.flags = texture->flags | SI_RESOURCE_FLAG_FLUSHED_DEPTH;
1555
1556 if (staging)
1557 resource.flags |= SI_RESOURCE_FLAG_TRANSFER;
1558
1559 *flushed_depth_texture = (struct si_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1560 if (*flushed_depth_texture == NULL) {
1561 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1562 return false;
1563 }
1564 return true;
1565 }
1566
1567 /**
1568 * Initialize the pipe_resource descriptor to be of the same size as the box,
1569 * which is supposed to hold a subregion of the texture "orig" at the given
1570 * mipmap level.
1571 */
1572 static void si_init_temp_resource_from_box(struct pipe_resource *res,
1573 struct pipe_resource *orig,
1574 const struct pipe_box *box,
1575 unsigned level, unsigned flags)
1576 {
1577 memset(res, 0, sizeof(*res));
1578 res->format = orig->format;
1579 res->width0 = box->width;
1580 res->height0 = box->height;
1581 res->depth0 = 1;
1582 res->array_size = 1;
1583 res->usage = flags & SI_RESOURCE_FLAG_TRANSFER ? PIPE_USAGE_STAGING : PIPE_USAGE_DEFAULT;
1584 res->flags = flags;
1585
1586 /* We must set the correct texture target and dimensions for a 3D box. */
1587 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1588 res->target = PIPE_TEXTURE_2D_ARRAY;
1589 res->array_size = box->depth;
1590 } else {
1591 res->target = PIPE_TEXTURE_2D;
1592 }
1593 }
1594
1595 static bool si_can_invalidate_texture(struct si_screen *sscreen,
1596 struct si_texture *tex,
1597 unsigned transfer_usage,
1598 const struct pipe_box *box)
1599 {
1600 return !tex->buffer.b.is_shared &&
1601 !(transfer_usage & PIPE_TRANSFER_READ) &&
1602 tex->buffer.b.b.last_level == 0 &&
1603 util_texrange_covers_whole_level(&tex->buffer.b.b, 0,
1604 box->x, box->y, box->z,
1605 box->width, box->height,
1606 box->depth);
1607 }
1608
1609 static void si_texture_invalidate_storage(struct si_context *sctx,
1610 struct si_texture *tex)
1611 {
1612 struct si_screen *sscreen = sctx->screen;
1613
1614 /* There is no point in discarding depth and tiled buffers. */
1615 assert(!tex->is_depth);
1616 assert(tex->surface.is_linear);
1617
1618 /* Reallocate the buffer in the same pipe_resource. */
1619 si_alloc_resource(sscreen, &tex->buffer);
1620
1621 /* Initialize the CMASK base address (needed even without CMASK). */
1622 tex->cmask_base_address_reg =
1623 (tex->buffer.gpu_address + tex->cmask_offset) >> 8;
1624
1625 p_atomic_inc(&sscreen->dirty_tex_counter);
1626
1627 sctx->num_alloc_tex_transfer_bytes += tex->size;
1628 }
1629
1630 static void *si_texture_transfer_map(struct pipe_context *ctx,
1631 struct pipe_resource *texture,
1632 unsigned level,
1633 unsigned usage,
1634 const struct pipe_box *box,
1635 struct pipe_transfer **ptransfer)
1636 {
1637 struct si_context *sctx = (struct si_context*)ctx;
1638 struct si_texture *tex = (struct si_texture*)texture;
1639 struct si_transfer *trans;
1640 struct r600_resource *buf;
1641 unsigned offset = 0;
1642 char *map;
1643 bool use_staging_texture = false;
1644
1645 assert(!(texture->flags & SI_RESOURCE_FLAG_TRANSFER));
1646 assert(box->width && box->height && box->depth);
1647
1648 /* Depth textures use staging unconditionally. */
1649 if (!tex->is_depth) {
1650 /* Degrade the tile mode if we get too many transfers on APUs.
1651 * On dGPUs, the staging texture is always faster.
1652 * Only count uploads that are at least 4x4 pixels large.
1653 */
1654 if (!sctx->screen->info.has_dedicated_vram &&
1655 level == 0 &&
1656 box->width >= 4 && box->height >= 4 &&
1657 p_atomic_inc_return(&tex->num_level0_transfers) == 10) {
1658 bool can_invalidate =
1659 si_can_invalidate_texture(sctx->screen, tex,
1660 usage, box);
1661
1662 si_reallocate_texture_inplace(sctx, tex,
1663 PIPE_BIND_LINEAR,
1664 can_invalidate);
1665 }
1666
1667 /* Tiled textures need to be converted into a linear texture for CPU
1668 * access. The staging texture is always linear and is placed in GART.
1669 *
1670 * Reading from VRAM or GTT WC is slow, always use the staging
1671 * texture in this case.
1672 *
1673 * Use the staging texture for uploads if the underlying BO
1674 * is busy.
1675 */
1676 if (!tex->surface.is_linear)
1677 use_staging_texture = true;
1678 else if (usage & PIPE_TRANSFER_READ)
1679 use_staging_texture =
1680 tex->buffer.domains & RADEON_DOMAIN_VRAM ||
1681 tex->buffer.flags & RADEON_FLAG_GTT_WC;
1682 /* Write & linear only: */
1683 else if (si_rings_is_buffer_referenced(sctx, tex->buffer.buf,
1684 RADEON_USAGE_READWRITE) ||
1685 !sctx->ws->buffer_wait(tex->buffer.buf, 0,
1686 RADEON_USAGE_READWRITE)) {
1687 /* It's busy. */
1688 if (si_can_invalidate_texture(sctx->screen, tex,
1689 usage, box))
1690 si_texture_invalidate_storage(sctx, tex);
1691 else
1692 use_staging_texture = true;
1693 }
1694 }
1695
1696 trans = CALLOC_STRUCT(si_transfer);
1697 if (!trans)
1698 return NULL;
1699 pipe_resource_reference(&trans->b.b.resource, texture);
1700 trans->b.b.level = level;
1701 trans->b.b.usage = usage;
1702 trans->b.b.box = *box;
1703
1704 if (tex->is_depth) {
1705 struct si_texture *staging_depth;
1706
1707 if (tex->buffer.b.b.nr_samples > 1) {
1708 /* MSAA depth buffers need to be converted to single sample buffers.
1709 *
1710 * Mapping MSAA depth buffers can occur if ReadPixels is called
1711 * with a multisample GLX visual.
1712 *
1713 * First downsample the depth buffer to a temporary texture,
1714 * then decompress the temporary one to staging.
1715 *
1716 * Only the region being mapped is transfered.
1717 */
1718 struct pipe_resource resource;
1719
1720 si_init_temp_resource_from_box(&resource, texture, box, level, 0);
1721
1722 if (!si_init_flushed_depth_texture(ctx, &resource, &staging_depth)) {
1723 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1724 goto fail_trans;
1725 }
1726
1727 if (usage & PIPE_TRANSFER_READ) {
1728 struct pipe_resource *temp = ctx->screen->resource_create(ctx->screen, &resource);
1729 if (!temp) {
1730 PRINT_ERR("failed to create a temporary depth texture\n");
1731 goto fail_trans;
1732 }
1733
1734 si_copy_region_with_blit(ctx, temp, 0, 0, 0, 0, texture, level, box);
1735 si_blit_decompress_depth(ctx, (struct si_texture*)temp, staging_depth,
1736 0, 0, 0, box->depth, 0, 0);
1737 pipe_resource_reference(&temp, NULL);
1738 }
1739
1740 /* Just get the strides. */
1741 si_texture_get_offset(sctx->screen, staging_depth, level, NULL,
1742 &trans->b.b.stride,
1743 &trans->b.b.layer_stride);
1744 } else {
1745 /* XXX: only readback the rectangle which is being mapped? */
1746 /* XXX: when discard is true, no need to read back from depth texture */
1747 if (!si_init_flushed_depth_texture(ctx, texture, &staging_depth)) {
1748 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1749 goto fail_trans;
1750 }
1751
1752 si_blit_decompress_depth(ctx, tex, staging_depth,
1753 level, level,
1754 box->z, box->z + box->depth - 1,
1755 0, 0);
1756
1757 offset = si_texture_get_offset(sctx->screen, staging_depth,
1758 level, box,
1759 &trans->b.b.stride,
1760 &trans->b.b.layer_stride);
1761 }
1762
1763 trans->staging = &staging_depth->buffer;
1764 buf = trans->staging;
1765 } else if (use_staging_texture) {
1766 struct pipe_resource resource;
1767 struct si_texture *staging;
1768
1769 si_init_temp_resource_from_box(&resource, texture, box, level,
1770 SI_RESOURCE_FLAG_TRANSFER);
1771 resource.usage = (usage & PIPE_TRANSFER_READ) ?
1772 PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1773
1774 /* Create the temporary texture. */
1775 staging = (struct si_texture*)ctx->screen->resource_create(ctx->screen, &resource);
1776 if (!staging) {
1777 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1778 goto fail_trans;
1779 }
1780 trans->staging = &staging->buffer;
1781
1782 /* Just get the strides. */
1783 si_texture_get_offset(sctx->screen, staging, 0, NULL,
1784 &trans->b.b.stride,
1785 &trans->b.b.layer_stride);
1786
1787 if (usage & PIPE_TRANSFER_READ)
1788 si_copy_to_staging_texture(ctx, trans);
1789 else
1790 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1791
1792 buf = trans->staging;
1793 } else {
1794 /* the resource is mapped directly */
1795 offset = si_texture_get_offset(sctx->screen, tex, level, box,
1796 &trans->b.b.stride,
1797 &trans->b.b.layer_stride);
1798 buf = &tex->buffer;
1799 }
1800
1801 if (!(map = si_buffer_map_sync_with_rings(sctx, buf, usage)))
1802 goto fail_trans;
1803
1804 *ptransfer = &trans->b.b;
1805 return map + offset;
1806
1807 fail_trans:
1808 r600_resource_reference(&trans->staging, NULL);
1809 pipe_resource_reference(&trans->b.b.resource, NULL);
1810 FREE(trans);
1811 return NULL;
1812 }
1813
1814 static void si_texture_transfer_unmap(struct pipe_context *ctx,
1815 struct pipe_transfer* transfer)
1816 {
1817 struct si_context *sctx = (struct si_context*)ctx;
1818 struct si_transfer *stransfer = (struct si_transfer*)transfer;
1819 struct pipe_resource *texture = transfer->resource;
1820 struct si_texture *tex = (struct si_texture*)texture;
1821
1822 if ((transfer->usage & PIPE_TRANSFER_WRITE) && stransfer->staging) {
1823 if (tex->is_depth && tex->buffer.b.b.nr_samples <= 1) {
1824 ctx->resource_copy_region(ctx, texture, transfer->level,
1825 transfer->box.x, transfer->box.y, transfer->box.z,
1826 &stransfer->staging->b.b, transfer->level,
1827 &transfer->box);
1828 } else {
1829 si_copy_from_staging_texture(ctx, stransfer);
1830 }
1831 }
1832
1833 if (stransfer->staging) {
1834 sctx->num_alloc_tex_transfer_bytes += stransfer->staging->buf->size;
1835 r600_resource_reference(&stransfer->staging, NULL);
1836 }
1837
1838 /* Heuristic for {upload, draw, upload, draw, ..}:
1839 *
1840 * Flush the gfx IB if we've allocated too much texture storage.
1841 *
1842 * The idea is that we don't want to build IBs that use too much
1843 * memory and put pressure on the kernel memory manager and we also
1844 * want to make temporary and invalidated buffers go idle ASAP to
1845 * decrease the total memory usage or make them reusable. The memory
1846 * usage will be slightly higher than given here because of the buffer
1847 * cache in the winsys.
1848 *
1849 * The result is that the kernel memory manager is never a bottleneck.
1850 */
1851 if (sctx->num_alloc_tex_transfer_bytes > sctx->screen->info.gart_size / 4) {
1852 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1853 sctx->num_alloc_tex_transfer_bytes = 0;
1854 }
1855
1856 pipe_resource_reference(&transfer->resource, NULL);
1857 FREE(transfer);
1858 }
1859
1860 static const struct u_resource_vtbl si_texture_vtbl =
1861 {
1862 NULL, /* get_handle */
1863 si_texture_destroy, /* resource_destroy */
1864 si_texture_transfer_map, /* transfer_map */
1865 u_default_transfer_flush_region, /* transfer_flush_region */
1866 si_texture_transfer_unmap, /* transfer_unmap */
1867 };
1868
1869 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
1870 */
1871 bool vi_dcc_formats_compatible(enum pipe_format format1,
1872 enum pipe_format format2)
1873 {
1874 const struct util_format_description *desc1, *desc2;
1875
1876 /* No format change - exit early. */
1877 if (format1 == format2)
1878 return true;
1879
1880 format1 = si_simplify_cb_format(format1);
1881 format2 = si_simplify_cb_format(format2);
1882
1883 /* Check again after format adjustments. */
1884 if (format1 == format2)
1885 return true;
1886
1887 desc1 = util_format_description(format1);
1888 desc2 = util_format_description(format2);
1889
1890 if (desc1->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
1891 desc2->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1892 return false;
1893
1894 /* Float and non-float are totally incompatible. */
1895 if ((desc1->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) !=
1896 (desc2->channel[0].type == UTIL_FORMAT_TYPE_FLOAT))
1897 return false;
1898
1899 /* Channel sizes must match across DCC formats.
1900 * Comparing just the first 2 channels should be enough.
1901 */
1902 if (desc1->channel[0].size != desc2->channel[0].size ||
1903 (desc1->nr_channels >= 2 &&
1904 desc1->channel[1].size != desc2->channel[1].size))
1905 return false;
1906
1907 /* Everything below is not needed if the driver never uses the DCC
1908 * clear code with the value of 1.
1909 */
1910
1911 /* If the clear values are all 1 or all 0, this constraint can be
1912 * ignored. */
1913 if (vi_alpha_is_on_msb(format1) != vi_alpha_is_on_msb(format2))
1914 return false;
1915
1916 /* Channel types must match if the clear value of 1 is used.
1917 * The type categories are only float, signed, unsigned.
1918 * NORM and INT are always compatible.
1919 */
1920 if (desc1->channel[0].type != desc2->channel[0].type ||
1921 (desc1->nr_channels >= 2 &&
1922 desc1->channel[1].type != desc2->channel[1].type))
1923 return false;
1924
1925 return true;
1926 }
1927
1928 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex,
1929 unsigned level,
1930 enum pipe_format view_format)
1931 {
1932 struct si_texture *stex = (struct si_texture *)tex;
1933
1934 return vi_dcc_enabled(stex, level) &&
1935 !vi_dcc_formats_compatible(tex->format, view_format);
1936 }
1937
1938 /* This can't be merged with the above function, because
1939 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1940 void vi_disable_dcc_if_incompatible_format(struct si_context *sctx,
1941 struct pipe_resource *tex,
1942 unsigned level,
1943 enum pipe_format view_format)
1944 {
1945 struct si_texture *stex = (struct si_texture *)tex;
1946
1947 if (vi_dcc_formats_are_incompatible(tex, level, view_format))
1948 if (!si_texture_disable_dcc(sctx, stex))
1949 si_decompress_dcc(sctx, stex);
1950 }
1951
1952 struct pipe_surface *si_create_surface_custom(struct pipe_context *pipe,
1953 struct pipe_resource *texture,
1954 const struct pipe_surface *templ,
1955 unsigned width0, unsigned height0,
1956 unsigned width, unsigned height)
1957 {
1958 struct si_surface *surface = CALLOC_STRUCT(si_surface);
1959
1960 if (!surface)
1961 return NULL;
1962
1963 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1964 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1965
1966 pipe_reference_init(&surface->base.reference, 1);
1967 pipe_resource_reference(&surface->base.texture, texture);
1968 surface->base.context = pipe;
1969 surface->base.format = templ->format;
1970 surface->base.width = width;
1971 surface->base.height = height;
1972 surface->base.u = templ->u;
1973
1974 surface->width0 = width0;
1975 surface->height0 = height0;
1976
1977 surface->dcc_incompatible =
1978 texture->target != PIPE_BUFFER &&
1979 vi_dcc_formats_are_incompatible(texture, templ->u.tex.level,
1980 templ->format);
1981 return &surface->base;
1982 }
1983
1984 static struct pipe_surface *si_create_surface(struct pipe_context *pipe,
1985 struct pipe_resource *tex,
1986 const struct pipe_surface *templ)
1987 {
1988 unsigned level = templ->u.tex.level;
1989 unsigned width = u_minify(tex->width0, level);
1990 unsigned height = u_minify(tex->height0, level);
1991 unsigned width0 = tex->width0;
1992 unsigned height0 = tex->height0;
1993
1994 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1995 const struct util_format_description *tex_desc
1996 = util_format_description(tex->format);
1997 const struct util_format_description *templ_desc
1998 = util_format_description(templ->format);
1999
2000 assert(tex_desc->block.bits == templ_desc->block.bits);
2001
2002 /* Adjust size of surface if and only if the block width or
2003 * height is changed. */
2004 if (tex_desc->block.width != templ_desc->block.width ||
2005 tex_desc->block.height != templ_desc->block.height) {
2006 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
2007 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
2008
2009 width = nblks_x * templ_desc->block.width;
2010 height = nblks_y * templ_desc->block.height;
2011
2012 width0 = util_format_get_nblocksx(tex->format, width0);
2013 height0 = util_format_get_nblocksy(tex->format, height0);
2014 }
2015 }
2016
2017 return si_create_surface_custom(pipe, tex, templ,
2018 width0, height0,
2019 width, height);
2020 }
2021
2022 static void si_surface_destroy(struct pipe_context *pipe,
2023 struct pipe_surface *surface)
2024 {
2025 pipe_resource_reference(&surface->texture, NULL);
2026 FREE(surface);
2027 }
2028
2029 unsigned si_translate_colorswap(enum pipe_format format, bool do_endian_swap)
2030 {
2031 const struct util_format_description *desc = util_format_description(format);
2032
2033 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2034
2035 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2036 return V_028C70_SWAP_STD;
2037
2038 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
2039 return ~0U;
2040
2041 switch (desc->nr_channels) {
2042 case 1:
2043 if (HAS_SWIZZLE(0,X))
2044 return V_028C70_SWAP_STD; /* X___ */
2045 else if (HAS_SWIZZLE(3,X))
2046 return V_028C70_SWAP_ALT_REV; /* ___X */
2047 break;
2048 case 2:
2049 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
2050 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
2051 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
2052 return V_028C70_SWAP_STD; /* XY__ */
2053 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
2054 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
2055 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
2056 /* YX__ */
2057 return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
2058 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
2059 return V_028C70_SWAP_ALT; /* X__Y */
2060 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
2061 return V_028C70_SWAP_ALT_REV; /* Y__X */
2062 break;
2063 case 3:
2064 if (HAS_SWIZZLE(0,X))
2065 return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
2066 else if (HAS_SWIZZLE(0,Z))
2067 return V_028C70_SWAP_STD_REV; /* ZYX */
2068 break;
2069 case 4:
2070 /* check the middle channels, the 1st and 4th channel can be NONE */
2071 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
2072 return V_028C70_SWAP_STD; /* XYZW */
2073 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
2074 return V_028C70_SWAP_STD_REV; /* WZYX */
2075 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
2076 return V_028C70_SWAP_ALT; /* ZYXW */
2077 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
2078 /* YZWX */
2079 if (desc->is_array)
2080 return V_028C70_SWAP_ALT_REV;
2081 else
2082 return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
2083 }
2084 break;
2085 }
2086 return ~0U;
2087 }
2088
2089 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2090
2091 static void vi_dcc_clean_up_context_slot(struct si_context *sctx,
2092 int slot)
2093 {
2094 int i;
2095
2096 if (sctx->dcc_stats[slot].query_active)
2097 vi_separate_dcc_stop_query(sctx,
2098 sctx->dcc_stats[slot].tex);
2099
2100 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats[slot].ps_stats); i++)
2101 if (sctx->dcc_stats[slot].ps_stats[i]) {
2102 sctx->b.destroy_query(&sctx->b,
2103 sctx->dcc_stats[slot].ps_stats[i]);
2104 sctx->dcc_stats[slot].ps_stats[i] = NULL;
2105 }
2106
2107 si_texture_reference(&sctx->dcc_stats[slot].tex, NULL);
2108 }
2109
2110 /**
2111 * Return the per-context slot where DCC statistics queries for the texture live.
2112 */
2113 static unsigned vi_get_context_dcc_stats_index(struct si_context *sctx,
2114 struct si_texture *tex)
2115 {
2116 int i, empty_slot = -1;
2117
2118 /* Remove zombie textures (textures kept alive by this array only). */
2119 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2120 if (sctx->dcc_stats[i].tex &&
2121 sctx->dcc_stats[i].tex->buffer.b.b.reference.count == 1)
2122 vi_dcc_clean_up_context_slot(sctx, i);
2123
2124 /* Find the texture. */
2125 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++) {
2126 /* Return if found. */
2127 if (sctx->dcc_stats[i].tex == tex) {
2128 sctx->dcc_stats[i].last_use_timestamp = os_time_get();
2129 return i;
2130 }
2131
2132 /* Record the first seen empty slot. */
2133 if (empty_slot == -1 && !sctx->dcc_stats[i].tex)
2134 empty_slot = i;
2135 }
2136
2137 /* Not found. Remove the oldest member to make space in the array. */
2138 if (empty_slot == -1) {
2139 int oldest_slot = 0;
2140
2141 /* Find the oldest slot. */
2142 for (i = 1; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2143 if (sctx->dcc_stats[oldest_slot].last_use_timestamp >
2144 sctx->dcc_stats[i].last_use_timestamp)
2145 oldest_slot = i;
2146
2147 /* Clean up the oldest slot. */
2148 vi_dcc_clean_up_context_slot(sctx, oldest_slot);
2149 empty_slot = oldest_slot;
2150 }
2151
2152 /* Add the texture to the new slot. */
2153 si_texture_reference(&sctx->dcc_stats[empty_slot].tex, tex);
2154 sctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2155 return empty_slot;
2156 }
2157
2158 static struct pipe_query *
2159 vi_create_resuming_pipestats_query(struct si_context *sctx)
2160 {
2161 struct si_query_hw *query = (struct si_query_hw*)
2162 sctx->b.create_query(&sctx->b, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2163
2164 query->flags |= SI_QUERY_HW_FLAG_BEGIN_RESUMES;
2165 return (struct pipe_query*)query;
2166 }
2167
2168 /**
2169 * Called when binding a color buffer.
2170 */
2171 void vi_separate_dcc_start_query(struct si_context *sctx,
2172 struct si_texture *tex)
2173 {
2174 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2175
2176 assert(!sctx->dcc_stats[i].query_active);
2177
2178 if (!sctx->dcc_stats[i].ps_stats[0])
2179 sctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(sctx);
2180
2181 /* begin or resume the query */
2182 sctx->b.begin_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2183 sctx->dcc_stats[i].query_active = true;
2184 }
2185
2186 /**
2187 * Called when unbinding a color buffer.
2188 */
2189 void vi_separate_dcc_stop_query(struct si_context *sctx,
2190 struct si_texture *tex)
2191 {
2192 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2193
2194 assert(sctx->dcc_stats[i].query_active);
2195 assert(sctx->dcc_stats[i].ps_stats[0]);
2196
2197 /* pause or end the query */
2198 sctx->b.end_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2199 sctx->dcc_stats[i].query_active = false;
2200 }
2201
2202 static bool vi_should_enable_separate_dcc(struct si_texture *tex)
2203 {
2204 /* The minimum number of fullscreen draws per frame that is required
2205 * to enable DCC. */
2206 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2207 }
2208
2209 /* Called by fast clear. */
2210 void vi_separate_dcc_try_enable(struct si_context *sctx,
2211 struct si_texture *tex)
2212 {
2213 /* The intent is to use this with shared displayable back buffers,
2214 * but it's not strictly limited only to them.
2215 */
2216 if (!tex->buffer.b.is_shared ||
2217 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2218 tex->buffer.b.b.target != PIPE_TEXTURE_2D ||
2219 tex->buffer.b.b.last_level > 0 ||
2220 !tex->surface.dcc_size ||
2221 sctx->screen->debug_flags & DBG(NO_DCC) ||
2222 sctx->screen->debug_flags & DBG(NO_DCC_FB))
2223 return;
2224
2225 assert(sctx->chip_class >= VI);
2226
2227 if (tex->dcc_offset)
2228 return; /* already enabled */
2229
2230 /* Enable the DCC stat gathering. */
2231 if (!tex->dcc_gather_statistics) {
2232 tex->dcc_gather_statistics = true;
2233 vi_separate_dcc_start_query(sctx, tex);
2234 }
2235
2236 if (!vi_should_enable_separate_dcc(tex))
2237 return; /* stats show that DCC decompression is too expensive */
2238
2239 assert(tex->surface.num_dcc_levels);
2240 assert(!tex->dcc_separate_buffer);
2241
2242 si_texture_discard_cmask(sctx->screen, tex);
2243
2244 /* Get a DCC buffer. */
2245 if (tex->last_dcc_separate_buffer) {
2246 assert(tex->dcc_gather_statistics);
2247 assert(!tex->dcc_separate_buffer);
2248 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2249 tex->last_dcc_separate_buffer = NULL;
2250 } else {
2251 tex->dcc_separate_buffer =
2252 si_aligned_buffer_create(sctx->b.screen,
2253 SI_RESOURCE_FLAG_UNMAPPABLE,
2254 PIPE_USAGE_DEFAULT,
2255 tex->surface.dcc_size,
2256 tex->surface.dcc_alignment);
2257 if (!tex->dcc_separate_buffer)
2258 return;
2259 }
2260
2261 /* dcc_offset is the absolute GPUVM address. */
2262 tex->dcc_offset = tex->dcc_separate_buffer->gpu_address;
2263
2264 /* no need to flag anything since this is called by fast clear that
2265 * flags framebuffer state
2266 */
2267 }
2268
2269 /**
2270 * Called by pipe_context::flush_resource, the place where DCC decompression
2271 * takes place.
2272 */
2273 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
2274 struct si_texture *tex)
2275 {
2276 struct si_context *sctx = (struct si_context*)ctx;
2277 struct pipe_query *tmp;
2278 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2279 bool query_active = sctx->dcc_stats[i].query_active;
2280 bool disable = false;
2281
2282 if (sctx->dcc_stats[i].ps_stats[2]) {
2283 union pipe_query_result result;
2284
2285 /* Read the results. */
2286 ctx->get_query_result(ctx, sctx->dcc_stats[i].ps_stats[2],
2287 true, &result);
2288 si_query_hw_reset_buffers(sctx,
2289 (struct si_query_hw*)
2290 sctx->dcc_stats[i].ps_stats[2]);
2291
2292 /* Compute the approximate number of fullscreen draws. */
2293 tex->ps_draw_ratio =
2294 result.pipeline_statistics.ps_invocations /
2295 (tex->buffer.b.b.width0 * tex->buffer.b.b.height0);
2296 sctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2297
2298 disable = tex->dcc_separate_buffer &&
2299 !vi_should_enable_separate_dcc(tex);
2300 }
2301
2302 tex->num_slow_clears = 0;
2303
2304 /* stop the statistics query for ps_stats[0] */
2305 if (query_active)
2306 vi_separate_dcc_stop_query(sctx, tex);
2307
2308 /* Move the queries in the queue by one. */
2309 tmp = sctx->dcc_stats[i].ps_stats[2];
2310 sctx->dcc_stats[i].ps_stats[2] = sctx->dcc_stats[i].ps_stats[1];
2311 sctx->dcc_stats[i].ps_stats[1] = sctx->dcc_stats[i].ps_stats[0];
2312 sctx->dcc_stats[i].ps_stats[0] = tmp;
2313
2314 /* create and start a new query as ps_stats[0] */
2315 if (query_active)
2316 vi_separate_dcc_start_query(sctx, tex);
2317
2318 if (disable) {
2319 assert(!tex->last_dcc_separate_buffer);
2320 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2321 tex->dcc_separate_buffer = NULL;
2322 tex->dcc_offset = 0;
2323 /* no need to flag anything since this is called after
2324 * decompression that re-sets framebuffer state
2325 */
2326 }
2327 }
2328
2329 static struct pipe_memory_object *
2330 si_memobj_from_handle(struct pipe_screen *screen,
2331 struct winsys_handle *whandle,
2332 bool dedicated)
2333 {
2334 struct si_screen *sscreen = (struct si_screen*)screen;
2335 struct si_memory_object *memobj = CALLOC_STRUCT(si_memory_object);
2336 struct pb_buffer *buf = NULL;
2337 uint32_t stride, offset;
2338
2339 if (!memobj)
2340 return NULL;
2341
2342 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle,
2343 sscreen->info.max_alignment,
2344 &stride, &offset);
2345 if (!buf) {
2346 free(memobj);
2347 return NULL;
2348 }
2349
2350 memobj->b.dedicated = dedicated;
2351 memobj->buf = buf;
2352 memobj->stride = stride;
2353
2354 return (struct pipe_memory_object *)memobj;
2355
2356 }
2357
2358 static void
2359 si_memobj_destroy(struct pipe_screen *screen,
2360 struct pipe_memory_object *_memobj)
2361 {
2362 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2363
2364 pb_reference(&memobj->buf, NULL);
2365 free(memobj);
2366 }
2367
2368 static struct pipe_resource *
2369 si_texture_from_memobj(struct pipe_screen *screen,
2370 const struct pipe_resource *templ,
2371 struct pipe_memory_object *_memobj,
2372 uint64_t offset)
2373 {
2374 struct si_screen *sscreen = (struct si_screen*)screen;
2375 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2376 struct pipe_resource *tex =
2377 si_texture_from_winsys_buffer(sscreen, templ, memobj->buf,
2378 memobj->stride, offset,
2379 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE |
2380 PIPE_HANDLE_USAGE_SHADER_WRITE,
2381 memobj->b.dedicated);
2382 if (!tex)
2383 return NULL;
2384
2385 /* si_texture_from_winsys_buffer doesn't increment refcount of
2386 * memobj->buf, so increment it here.
2387 */
2388 struct pb_buffer *buf = NULL;
2389 pb_reference(&buf, memobj->buf);
2390 return tex;
2391 }
2392
2393 static bool si_check_resource_capability(struct pipe_screen *screen,
2394 struct pipe_resource *resource,
2395 unsigned bind)
2396 {
2397 struct si_texture *tex = (struct si_texture*)resource;
2398
2399 /* Buffers only support the linear flag. */
2400 if (resource->target == PIPE_BUFFER)
2401 return (bind & ~PIPE_BIND_LINEAR) == 0;
2402
2403 if (bind & PIPE_BIND_LINEAR && !tex->surface.is_linear)
2404 return false;
2405
2406 if (bind & PIPE_BIND_SCANOUT && !tex->surface.is_displayable)
2407 return false;
2408
2409 /* TODO: PIPE_BIND_CURSOR - do we care? */
2410 return true;
2411 }
2412
2413 void si_init_screen_texture_functions(struct si_screen *sscreen)
2414 {
2415 sscreen->b.resource_from_handle = si_texture_from_handle;
2416 sscreen->b.resource_get_handle = si_texture_get_handle;
2417 sscreen->b.resource_from_memobj = si_texture_from_memobj;
2418 sscreen->b.memobj_create_from_handle = si_memobj_from_handle;
2419 sscreen->b.memobj_destroy = si_memobj_destroy;
2420 sscreen->b.check_resource_capability = si_check_resource_capability;
2421 }
2422
2423 void si_init_context_texture_functions(struct si_context *sctx)
2424 {
2425 sctx->b.create_surface = si_create_surface;
2426 sctx->b.surface_destroy = si_surface_destroy;
2427 }