2022-11-18 22:31:58 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright 2022 Alyssa Rosenzweig
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-27 17:36:08 -05:00
|
|
|
#include "agx_tilebuffer.h"
|
2022-11-18 22:31:58 -05:00
|
|
|
#include <assert.h>
|
2023-06-14 17:37:04 -04:00
|
|
|
#include "util/bitscan.h"
|
2022-11-18 22:31:58 -05:00
|
|
|
#include "util/format/u_format.h"
|
|
|
|
|
#include "agx_usc.h"
|
2024-08-13 11:40:36 -04:00
|
|
|
#include "layout.h"
|
2022-11-18 22:31:58 -05:00
|
|
|
|
|
|
|
|
/* Maximum number of bytes per tile on G13G. This may change in future versions
|
|
|
|
|
* of the architecture.
|
|
|
|
|
*/
|
|
|
|
|
#define MAX_BYTES_PER_TILE (32768 - 1)
|
|
|
|
|
|
2023-06-14 17:37:04 -04:00
|
|
|
/* Maximum bytes per sample in the tilebuffer. Greater allocations require
|
|
|
|
|
* spilling render targets to memory.
|
|
|
|
|
*/
|
|
|
|
|
#define MAX_BYTES_PER_SAMPLE (64)
|
|
|
|
|
|
|
|
|
|
/* Minimum tile size in pixels, architectural. */
|
|
|
|
|
#define MIN_TILE_SIZE_PX (16 * 16)
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
/* Select the largest tile size that fits */
|
|
|
|
|
static struct agx_tile_size
|
|
|
|
|
agx_select_tile_size(unsigned bytes_per_pixel)
|
|
|
|
|
{
|
2022-12-26 09:45:48 -05:00
|
|
|
/* clang-format off */
|
2022-11-18 22:31:58 -05:00
|
|
|
struct agx_tile_size sizes[] = {
|
|
|
|
|
{ 32, 32 },
|
|
|
|
|
{ 32, 16 },
|
|
|
|
|
{ 16, 16 }
|
|
|
|
|
};
|
2022-12-26 09:45:48 -05:00
|
|
|
/* clang-format on */
|
2022-11-18 22:31:58 -05:00
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(sizes); ++i) {
|
|
|
|
|
struct agx_tile_size size = sizes[i];
|
|
|
|
|
|
|
|
|
|
if ((bytes_per_pixel * size.width * size.height) <= MAX_BYTES_PER_TILE)
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 09:17:35 +02:00
|
|
|
UNREACHABLE("No supported tile size meets the bytes per pixel requirement");
|
2022-11-18 22:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-20 21:31:27 -04:00
|
|
|
static unsigned
|
|
|
|
|
agx_shared_layout_from_tile_size(struct agx_tile_size t)
|
|
|
|
|
{
|
|
|
|
|
if (t.width == 32 && t.height == 32)
|
|
|
|
|
return AGX_SHARED_LAYOUT_32X32;
|
|
|
|
|
else if (t.width == 32 && t.height == 16)
|
|
|
|
|
return AGX_SHARED_LAYOUT_32X16;
|
|
|
|
|
else if (t.width == 16 && t.height == 16)
|
|
|
|
|
return AGX_SHARED_LAYOUT_16X16;
|
|
|
|
|
else
|
2025-07-23 09:17:35 +02:00
|
|
|
UNREACHABLE("Invalid tile size");
|
2024-04-20 21:31:27 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
struct agx_tilebuffer_layout
|
2024-03-18 14:57:48 -04:00
|
|
|
agx_build_tilebuffer_layout(const enum pipe_format *formats, uint8_t nr_cbufs,
|
2023-08-28 08:42:14 -04:00
|
|
|
uint8_t nr_samples, bool layered)
|
2022-11-18 22:31:58 -05:00
|
|
|
{
|
2023-08-28 08:42:14 -04:00
|
|
|
struct agx_tilebuffer_layout tib = {
|
|
|
|
|
.nr_samples = nr_samples,
|
|
|
|
|
.layered = layered,
|
|
|
|
|
};
|
2022-11-18 22:31:58 -05:00
|
|
|
|
|
|
|
|
uint32_t offset_B = 0;
|
|
|
|
|
|
|
|
|
|
for (unsigned rt = 0; rt < nr_cbufs; ++rt) {
|
|
|
|
|
tib.logical_format[rt] = formats[rt];
|
|
|
|
|
|
2024-03-18 14:58:03 -04:00
|
|
|
/* If there are gaps in the layout, don't allocate holes. Obscure,
|
|
|
|
|
* PIPE_FORMAT_NONE has a size of 1, not 0.
|
|
|
|
|
*/
|
|
|
|
|
if (formats[rt] == PIPE_FORMAT_NONE)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
/* Require natural alignment for tilebuffer allocations. This could be
|
|
|
|
|
* optimized, but this shouldn't be a problem in practice.
|
|
|
|
|
*/
|
|
|
|
|
enum pipe_format physical_fmt = agx_tilebuffer_physical_format(&tib, rt);
|
|
|
|
|
unsigned align_B = util_format_get_blocksize(physical_fmt);
|
2023-06-14 17:37:04 -04:00
|
|
|
assert(util_is_power_of_two_nonzero(align_B) &&
|
|
|
|
|
util_is_power_of_two_nonzero(MAX_BYTES_PER_SAMPLE) &&
|
|
|
|
|
align_B < MAX_BYTES_PER_SAMPLE &&
|
|
|
|
|
"max bytes per sample divisible by alignment");
|
2022-11-18 22:31:58 -05:00
|
|
|
|
2023-06-14 17:37:04 -04:00
|
|
|
offset_B = ALIGN_POT(offset_B, align_B);
|
|
|
|
|
assert(offset_B <= MAX_BYTES_PER_SAMPLE && "loop invariant + above");
|
2022-11-18 22:31:58 -05:00
|
|
|
|
2023-06-14 17:37:04 -04:00
|
|
|
/* Determine the size, if we were to allocate this render target to the
|
|
|
|
|
* tilebuffer as desired.
|
|
|
|
|
*/
|
2022-12-27 17:36:08 -05:00
|
|
|
unsigned nr = util_format_get_nr_components(physical_fmt) == 1
|
|
|
|
|
? util_format_get_nr_components(formats[rt])
|
|
|
|
|
: 1;
|
2022-11-18 22:31:58 -05:00
|
|
|
|
|
|
|
|
unsigned size_B = align_B * nr;
|
2023-06-14 17:37:04 -04:00
|
|
|
unsigned new_offset_B = offset_B + size_B;
|
|
|
|
|
|
|
|
|
|
/* If allocating this render target would exceed any tilebuffer limits, we
|
|
|
|
|
* need to spill it to memory. We continue processing in case there are
|
|
|
|
|
* smaller render targets after that would still fit. Otherwise, we
|
|
|
|
|
* allocate it to the tilebuffer.
|
|
|
|
|
*
|
|
|
|
|
* TODO: Suboptimal, we might be able to reorder render targets to
|
|
|
|
|
* avoid fragmentation causing spilling.
|
|
|
|
|
*/
|
2024-01-07 12:08:43 -04:00
|
|
|
bool fits = (new_offset_B <= MAX_BYTES_PER_SAMPLE) &&
|
|
|
|
|
(ALIGN_POT(new_offset_B, 8) * MIN_TILE_SIZE_PX *
|
|
|
|
|
nr_samples) <= MAX_BYTES_PER_TILE;
|
2023-06-14 17:37:04 -04:00
|
|
|
|
|
|
|
|
if (fits) {
|
|
|
|
|
tib._offset_B[rt] = offset_B;
|
|
|
|
|
offset_B = new_offset_B;
|
|
|
|
|
} else {
|
|
|
|
|
tib.spilled[rt] = true;
|
|
|
|
|
}
|
2022-11-18 22:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-14 17:37:04 -04:00
|
|
|
assert(offset_B <= MAX_BYTES_PER_SAMPLE && "loop invariant");
|
2023-05-25 13:22:52 -04:00
|
|
|
|
|
|
|
|
/* Multisampling needs a nonempty allocation.
|
|
|
|
|
* XXX: Check this against hw
|
|
|
|
|
*/
|
|
|
|
|
if (nr_samples > 1)
|
|
|
|
|
offset_B = MAX2(offset_B, 1);
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
tib.sample_size_B = ALIGN_POT(offset_B, 8);
|
|
|
|
|
|
|
|
|
|
tib.tile_size = agx_select_tile_size(tib.sample_size_B * nr_samples);
|
2024-04-20 21:31:27 -04:00
|
|
|
|
|
|
|
|
agx_tilebuffer_pack_usc(&tib);
|
2022-11-18 22:31:58 -05:00
|
|
|
return tib;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 19:19:22 -04:00
|
|
|
/*
|
|
|
|
|
* With attachmentless rendering in Vulkan, the sample count may not known until
|
|
|
|
|
* draw-time. It's convenient to construct an agx_tilebuffer_layout anyway when
|
|
|
|
|
* beginning rendering, updating the sample count later. This helper allows the
|
|
|
|
|
* driver to set the sample count in a partial agx_tilebuffer_layout.
|
|
|
|
|
*
|
|
|
|
|
* When doing so, we need to rebuild entirely since e.g. tile size might change.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
agx_tilebuffer_set_samples(struct agx_tilebuffer_layout *tib,
|
|
|
|
|
unsigned nr_samples)
|
|
|
|
|
{
|
|
|
|
|
assert(tib->nr_samples == 0 && "must not be initialized");
|
|
|
|
|
|
|
|
|
|
*tib = agx_build_tilebuffer_layout(tib->logical_format,
|
|
|
|
|
ARRAY_SIZE(tib->logical_format),
|
|
|
|
|
nr_samples, tib->layered);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
enum pipe_format
|
|
|
|
|
agx_tilebuffer_physical_format(struct agx_tilebuffer_layout *tib, unsigned rt)
|
|
|
|
|
{
|
2024-08-13 11:40:36 -04:00
|
|
|
return ail_pixel_format[tib->logical_format[rt]].renderable;
|
2022-11-18 22:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-17 18:24:38 -05:00
|
|
|
bool
|
|
|
|
|
agx_tilebuffer_supports_mask(struct agx_tilebuffer_layout *tib, unsigned rt)
|
|
|
|
|
{
|
2023-06-14 17:53:38 -04:00
|
|
|
/* We don't bother support masking with spilled render targets. This might be
|
|
|
|
|
* optimized in the future but spilling is so rare anyway it's not worth it.
|
|
|
|
|
*/
|
|
|
|
|
if (tib->spilled[rt])
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-02-17 18:24:38 -05:00
|
|
|
enum pipe_format fmt = agx_tilebuffer_physical_format(tib, rt);
|
2024-08-13 11:40:36 -04:00
|
|
|
return ail_isa_format_supports_mask((enum ail_isa_format)fmt);
|
2023-02-17 18:24:38 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-18 22:31:58 -05:00
|
|
|
uint32_t
|
|
|
|
|
agx_tilebuffer_total_size(struct agx_tilebuffer_layout *tib)
|
|
|
|
|
{
|
2022-12-27 17:36:08 -05:00
|
|
|
return tib->sample_size_B * tib->nr_samples * tib->tile_size.width *
|
|
|
|
|
tib->tile_size.height;
|
2022-11-18 22:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2024-04-20 21:31:27 -04:00
|
|
|
agx_tilebuffer_pack_usc(struct agx_tilebuffer_layout *tib)
|
2022-11-18 22:31:58 -05:00
|
|
|
{
|
2024-04-20 21:31:27 -04:00
|
|
|
agx_pack(&tib->usc, USC_SHARED, cfg) {
|
|
|
|
|
if (tib->nr_samples > 0) {
|
|
|
|
|
cfg.uses_shared_memory = true;
|
|
|
|
|
cfg.layout = agx_shared_layout_from_tile_size(tib->tile_size);
|
|
|
|
|
cfg.sample_stride_in_8_bytes = tib->sample_size_B / 8;
|
|
|
|
|
cfg.sample_count = tib->nr_samples;
|
|
|
|
|
cfg.bytes_per_threadgroup = agx_tilebuffer_total_size(tib);
|
|
|
|
|
} else {
|
|
|
|
|
cfg.layout = AGX_SHARED_LAYOUT_VERTEX_COMPUTE;
|
|
|
|
|
cfg.bytes_per_threadgroup = 65536;
|
|
|
|
|
}
|
2022-11-18 22:31:58 -05:00
|
|
|
}
|
|
|
|
|
}
|