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