Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/asahi/lib/agx_g17p_compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
*
* Every format here has a tilebuffer footprint of at most 8 bytes per
* sample, and none has a different store class (R11G11B10 and RGB9E5 are
* stored as R32_UINT words). With at most seven other render targets of at
* most 16 bytes, a single-sample G17P tilebuffer (120 bytes per sample)
* therefore never spills one of these, so a compressed render target never
* needs the in-place decompression kernel, which has no G17P build.
* test-tilebuffer.cpp checks both properties for every format here.
* stored as R32_UINT words). At 1x, with at most seven other render targets
* of at most 16 bytes, a G17P tilebuffer (120 bytes per sample) never spills
* one of these; test-tilebuffer.cpp checks both properties at 1x. At 2x/4x
* MSAA, wide MRT framebuffers (such as 5x RGBA16F at 4x) can exceed the
* tilebuffer byte budget and spill, so Gallium pre-decompresses spilling
* private targets via agx_decompress() in agx_set_framebuffer_state() rather
* than relying on AGX2-only in-place decompression.
*/
static inline bool
agx_g17p_compression_color_format(enum pipe_format format)
Expand Down
12 changes: 7 additions & 5 deletions src/asahi/lib/tests/test-tilebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,11 +866,13 @@ g17p_single_sample_layout(const enum pipe_format *formats, unsigned count,
formats, count, 1, layered, AGX_G17P_MAX_BYTES_PER_SAMPLE, 32768, 2048);
}

/* A spilled compressed render target needs in-place decompression, which
* has no G17P build, so no compressed G17P colour format may ever spill.
* Every renderable format takes at most 16 bytes of a sample and every
* compressed one at most 8, so eight targets need at most 7 * 16 + 8 = 120
* bytes, the G17P per-sample limit. Check the bound and every placement.
/* At single-sample (1x), every renderable format takes at most 16 bytes of a
* sample and every compressed G17P colour format at most 8, so eight targets
* need at most 7 * 16 + 8 = 120 bytes, the G17P per-sample limit, and never
* spill. Check the 1x bound and every placement. (At 2x/4x MSAA, wide MRT
* framebuffers can exceed the tilebuffer byte budget and spill; Gallium
* pre-decompresses those targets via agx_decompress() in
* agx_set_framebuffer_state() instead of using in-place decompression.)
*/
TEST(Tilebuffer, G17PCompressedColourNeverSpills)
{
Expand Down
10 changes: 8 additions & 2 deletions src/gallium/drivers/asahi/agx_native_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ agx_gallium_pack_native_pbe_impl(
legacy->compressed && gen9 &&
legacy->dimension == AGX_TEXTURE_DIMENSION_2D &&
legacy->layout == AGX_LAYOUT_GPU && !multisample;
const bool compressed_2d_ms =
legacy->compressed && gen9 &&
legacy->dimension == AGX_TEXTURE_DIMENSION_2D_MULTISAMPLED &&
legacy->layout == AGX_LAYOUT_GPU;
if ((!layered && !multisample && !g18_layered &&
legacy->dimension != AGX_TEXTURE_DIMENSION_2D) ||
/* Gen10 linear 2D is agx_pbe_agx3_2d_pack()'s layout exactly. The
Expand All @@ -503,13 +507,15 @@ agx_gallium_pack_native_pbe_impl(
(multisample ? (legacy->samples != AGX_SAMPLE_COUNT_2 &&
legacy->samples != AGX_SAMPLE_COUNT_4)
: legacy->samples != AGX_SAMPLE_COUNT_2) ||
(legacy->compressed && !compressed_2d && !gen10_multisample) ||
(legacy->compressed && !compressed_2d && !compressed_2d_ms &&
!gen10_multisample) ||
legacy->mode != AGX_IMAGE_MODE_NORMAL ||
(!layered && !g18_layered && legacy->layers != 1) ||
(g18_layered && legacy->layout == AGX_LAYOUT_LINEAR) ||
(multisample && (legacy->layout == AGX_LAYOUT_LINEAR ||
legacy->level != 0 || legacy->levels != 1 ||
legacy->unk_mipmapped || (gen9 && legacy->compressed))) ||
legacy->unk_mipmapped ||
(gen9 && legacy->compressed && !compressed_2d_ms))) ||
(!gen9 && !g18_mips && (legacy->level != 0 || legacy->levels != 1 ||
legacy->unk_mipmapped)) ||
(!gen9 && legacy->srgb) ||
Expand Down
30 changes: 16 additions & 14 deletions src/gallium/drivers/asahi/agx_native_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ agx_gallium_native_texture_sample_count_representable(
*
* This is a generation predicate, not a per-image one. AGX3's recovered PBE
* geometry is a bounded subset -- non-sparse 2D (on Gen9 also arrays, 3D,
* mips and multisample, and the GPU-tiled compressed single-layer 2D packet)
* with no software addressing sideband -- and that subset is enforced per
* descriptor by agx_gallium_pack_native_pbe(), which fails closed for
* anything outside it. Gallium still feeds AGX2 sideband state (image
* atomics, EMRT, spilled render targets) through the same packet, so those
* images are refused rather than silently encoded with the bits dropped.
* mips and multisample, and the GPU-tiled compressed single-layer 2D and
* 2x/4x multisample 2D packets) with no software addressing sideband -- and
* that subset is enforced per descriptor by agx_gallium_pack_native_pbe(),
* which fails closed for anything outside it. Gallium still feeds AGX2
* sideband state (image atomics, EMRT, spilled render targets) through the
* same packet, so those images are refused rather than silently encoded with
* the bits dropped.
*/
static inline bool
agx_gallium_native_pbe_supported(
Expand All @@ -175,14 +176,15 @@ agx_gallium_native_pbe_supported(

/* Whether Gallium may write any compressed image, shared ones included.
*
* Gallium decompresses a compressed render target in place when its
* tilebuffer spills it, and a shared one when a feedback loop samples it
* (agx_decompress_inplace()). That kernel takes the AGX2 24+24-byte
* descriptor pair and has no AGX3 build, so only AGX2 qualifies. On AGX3
* the compressed PBE itself is encoded (Gen9 GPU-tiled 2D, see
* agx_gallium_pack_native_pbe()); G17P compresses its private images within
* agx_g17p_compression_color_format(), which never spill, and imported
* compressed images stay sample-only (external) there.
* Gallium decompresses a shared compressed image in place when a feedback
* loop samples it (agx_decompress_inplace()). That kernel takes the AGX2
* 24+24-byte descriptor pair and has no AGX3 build, so only AGX2 qualifies.
* On AGX3 the compressed PBE itself is encoded (Gen9 GPU-tiled 2D and 2x/4x
* multisample 2D, see agx_gallium_pack_native_pbe()); G17P compresses its
* private images within agx_g17p_compression_color_format() and reallocates
* them uncompressed via agx_decompress() if a framebuffer spills them or a
* feedback loop/shader image binds them, while imported compressed images
* stay sample-only (external) there.
*/
static inline bool
agx_gallium_shared_compression_supported(
Expand Down
46 changes: 30 additions & 16 deletions src/gallium/drivers/asahi/agx_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,39 +441,53 @@ agx_twiddled_allowed(const struct agx_resource *pres)
return true;
}

/* G17P depth: a private, single-level, single-layer, single-sample Z32F
* depth buffer, as Honeykrisp compresses (hk_g17p_compression_depth_format()).
* ZLS writes it compressed through the metadata agx_batch already passes, and
* the compressed Gen9 2D texture packet reads it. On the M4 Max this is
* byte-identical to plain depth through depth testing across passes,
* readback, sampling, shadow comparison, blits and scissored clears. */
/* G17P depth: a private, single-level, single-layer 1x/2x/4x Z32_FLOAT or
* Z16_UNORM depth buffer (including the separated Z32_FLOAT plane of
* Z32F_S8 and Z24S8). ZLS writes it compressed through the metadata
* agx_batch already passes, and the compressed Gen9 2D / 2D-multisample
* texture packet reads it.
*
* The width0/height0 >= 16 check is intentionally in pixels (stricter than
* ail_can_compress(), which measures in samples at 2x/4x) so sub-16px
* multisample targets stay uncompressed where metadata overhead outweighs
* bandwidth savings.
*/
static bool
agx_g17p_private_depth_compression_allowed(const struct agx_device *dev,
const struct pipe_resource *templ)
{
return dev->chip == AGX_CHIP_G17P &&
templ->target == PIPE_TEXTURE_2D && templ->depth0 == 1 &&
templ->array_size <= 1 && templ->last_level == 0 &&
templ->nr_samples <= 1 && templ->format == PIPE_FORMAT_Z32_FLOAT &&
(templ->nr_samples <= 1 || templ->nr_samples == 2 ||
templ->nr_samples == 4) &&
(templ->format == PIPE_FORMAT_Z32_FLOAT ||
templ->format == PIPE_FORMAT_Z16_UNORM) &&
templ->width0 >= 16 && templ->height0 >= 16 &&
!(templ->bind & ~(PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL));
}

/* G17P compresses a private, single-level, single-layer, single-sample 2D
* colour image of agx_g17p_compression_color_format(): written by the
* compressed Gen9 2D PBE (end-of-tile stores, blits, staging uploads) and
* read by the compressed texture descriptor, the packets Honeykrisp
* compresses its own G17P colour images with. Nothing decompresses such an
* image in place: its format never spills from the tilebuffer, and it is
* neither shared nor scanned out, so a feedback loop or a shader-image
* binding reallocates it uncompressed (agx_decompress()), as on AGX2.
/* G17P compresses a private, single-level, single-layer 1x/2x/4x 2D colour
* image of agx_g17p_compression_color_format(): written by the compressed
* Gen9 2D or 2x/4x multisample PBE (end-of-tile stores, blits, staging
* uploads) and read by the compressed texture descriptor. Nothing
* decompresses such an image in place: it is neither shared nor scanned out,
* so a spilling MRT framebuffer, feedback loop, or shader-image binding
* reallocates it uncompressed via agx_decompress().
*
* As with depth above, width0/height0 >= 16 is intentionally kept in pixels
* rather than samples to leave tiny sub-16px 2x/4x targets uncompressed.
*/
static bool
agx_g17p_private_compression_allowed(const struct agx_device *dev,
const struct pipe_resource *templ)
{
return dev->chip == AGX_CHIP_G17P && templ->target == PIPE_TEXTURE_2D &&
templ->depth0 == 1 && templ->array_size <= 1 &&
templ->last_level == 0 && templ->nr_samples <= 1 &&
templ->last_level == 0 &&
(templ->nr_samples <= 1 || templ->nr_samples == 2 ||
templ->nr_samples == 4) &&
templ->width0 >= 16 && templ->height0 >= 16 &&
!(templ->bind &
~(PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET)) &&
agx_g17p_compression_color_format(templ->format);
Expand Down
58 changes: 57 additions & 1 deletion src/gallium/drivers/asahi/agx_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,12 @@ agx_check_zs_merge(struct agx_context *ctx)
agx_dirty_all(ctx);
}

static struct agx_tilebuffer_layout
agx_gallium_tilebuffer_layout(const struct agx_device *dev,
const enum pipe_format *formats,
unsigned nr_cbufs, unsigned nr_samples,
bool layered);

static void
agx_set_framebuffer_state(struct pipe_context *pctx,
const struct pipe_framebuffer_state *state)
Expand All @@ -1595,6 +1601,49 @@ agx_set_framebuffer_state(struct pipe_context *pctx,
if (!state)
return;

/* On chips without in-place decompression (AGX3), reallocate any
* compressed render target that this framebuffer's tilebuffer layout will
* spill before binding `state` into `ctx->framebuffer`. Running
* agx_decompress() here avoids re-entering with the spilling framebuffer
* when the blitter restores `ctx->framebuffer`, and guarantees no batch
* reaches AGX2-only agx_decompress_inplace().
*/
const struct agx_device *dev = agx_device(pctx->screen);
if (!agx_gallium_shared_compression_supported(
agx_chip_descriptor_generation(dev->chip)) &&
state->nr_cbufs > 1 && !agx_chip_g17_m5(dev->chip)) {
bool any_compressed = false;
enum pipe_format formats[PIPE_MAX_COLOR_BUFS] = {0};

for (unsigned i = 0; i < state->nr_cbufs; ++i) {
formats[i] = state->cbufs[i].format;
if (state->cbufs[i].texture &&
ail_is_level_logically_compressed(
&agx_resource(state->cbufs[i].texture)->layout,
state->cbufs[i].level))
any_compressed = true;
}

if (any_compressed) {
struct agx_tilebuffer_layout tib = agx_gallium_tilebuffer_layout(
dev, formats, state->nr_cbufs,
util_framebuffer_get_num_samples(state),
util_framebuffer_get_num_layers(state) > 1);
if (agx_tilebuffer_spills(&tib)) {
for (unsigned i = 0; i < state->nr_cbufs; ++i) {
if (!tib.spilled[i] || !state->cbufs[i].texture)
continue;

struct agx_resource *rsrc = agx_resource(state->cbufs[i].texture);
if (!(rsrc->base.bind & PIPE_BIND_SHARED) &&
ail_is_level_logically_compressed(&rsrc->layout,
state->cbufs[i].level))
agx_decompress(ctx, rsrc, "Render target spilled");
}
}
}
}

util_copy_framebuffer_state(&ctx->framebuffer, state);
ctx->g17g_mrt_spills_known = false;
ctx->zs_merge = false;
Expand Down Expand Up @@ -11045,9 +11094,16 @@ agx_batch_init_state(struct agx_batch *batch)
if (!ail_is_level_logically_compressed(layout, level))
continue;

if (true || (rsrc->base.bind & PIPE_BIND_SHARED)) {
if (agx_gallium_shared_compression_supported(
agx_chip_descriptor_generation(dev->chip)) ||
(rsrc->base.bind & PIPE_BIND_SHARED)) {
agx_decompress_inplace(batch, surf, "Render target spilled");
} else {
/* agx_set_framebuffer_state() pre-decompresses spilling private
* compressed targets on AGX3 before copying ctx->framebuffer so
* blitter restore cannot re-enter batch setup. */
assert(!"Spilled compressed render target was not pre-decompressed "
"in agx_set_framebuffer_state()");
agx_decompress(batch->ctx, rsrc, "Render target spilled");
}
}
Expand Down
32 changes: 31 additions & 1 deletion src/gallium/drivers/asahi/tests/test-agx-native-descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,31 @@ test_g17p_compressed_render_target(void)
CHECK(memcmp(native.opaque, targets[i].words,
sizeof(targets[i].words)) == 0);

/* Outside the compressed single-layer 2D packet nothing is encoded. */
/* Gen9 compressed 2x/4x 2D multisample keeps the single-sample compressed
* payload and updates only the dimension nibble (4) and sample selector
* at word0[57:56]. */
const struct AGX_PBE original = rt;
static const enum agx_sample_count ms_counts[] = {
AGX_SAMPLE_COUNT_2,
AGX_SAMPLE_COUNT_4,
};
for (unsigned s = 0; s < ARRAY_SIZE(ms_counts); ++s) {
rt = original;
rt.dimension = AGX_TEXTURE_DIMENSION_2D_MULTISAMPLED;
rt.samples = ms_counts[s];
uint64_t expected_ms[4];
memcpy(expected_ms, targets[i].words, sizeof(expected_ms));
expected_ms[0] = (expected_ms[0] & ~UINT64_C(0xf)) | 4u |
((uint64_t)s << 56);
memset(&native, 0xa5, sizeof(native));
CHECK(agx_gallium_pack_native_pbe(AGX_DESCRIPTOR_GENERATION_9, &native,
&rt));
CHECK(memcmp(native.opaque, expected_ms, sizeof(expected_ms)) == 0);
}

/* Outside the compressed single-layer 2D and 2x/4x 2D multisample
* packets nothing is encoded. */
rt = original;
rt.acceleration_buffer = 0;
CHECK(!agx_gallium_pack_native_pbe(AGX_DESCRIPTOR_GENERATION_9, &native,
&rt));
Expand All @@ -666,8 +689,15 @@ test_g17p_compressed_render_target(void)
CHECK(!agx_gallium_pack_native_pbe(AGX_DESCRIPTOR_GENERATION_9, &native,
&rt));
rt = original;
rt.dimension = AGX_TEXTURE_DIMENSION_2D_ARRAY_MULTISAMPLED;
rt.layers = 2;
rt.samples = AGX_SAMPLE_COUNT_4;
CHECK(!agx_gallium_pack_native_pbe(AGX_DESCRIPTOR_GENERATION_9, &native,
&rt));
rt = original;
rt.dimension = AGX_TEXTURE_DIMENSION_2D_MULTISAMPLED;
rt.samples = AGX_SAMPLE_COUNT_4;
rt.layout = AGX_LAYOUT_TWIDDLED;
CHECK(!agx_gallium_pack_native_pbe(AGX_DESCRIPTOR_GENERATION_9, &native,
&rt));
rt = original;
Expand Down