Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/asahi/lib/tests/test-i386-layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int
main(void)
{
typedef struct libagx_imm_write imm;
CHECK(imm, M(imm, address), M(imm, value), M(imm, pad));
CHECK(imm, M(imm, address), M(imm, value), M(imm, kind));

typedef struct agx_rodata rodata;
CHECK(rodata, M(rodata, offset), M(rodata, base_uniform),
Expand Down
5 changes: 2 additions & 3 deletions src/asahi/libagx/draws.cl
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ libagx_task_setup(global struct libagx_task_record *rec,
if (enabled && heap) {
uint64_t size = (uint64_t)n * rec->stride_B;
uint offs = 0;
enabled = size <= heap->size &&
poly_heap_try_alloc_offs(heap, (uint)size, &offs);
enabled = poly_heap_try_alloc_offs(heap, size, &offs);
if (enabled)
rec->entries = (uint64_t)heap->base + offs;
}
Expand Down Expand Up @@ -338,7 +337,7 @@ libagx_write_adj_indices_indirect(global struct poly_heap *heap,

if (tid == 0) {
uint offs = 0;
uint ok = count > 0 && poly_heap_try_alloc_offs(heap, count * 4, &offs);
uint ok = count > 0 && poly_heap_try_alloc_offs(heap, (uint64_t)count * 4, &offs);

offs_sh = offs;
ok_sh = ok;
Expand Down
12 changes: 10 additions & 2 deletions src/asahi/libagx/geometry.cl
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ libagx_prefix_sum_tess(global struct poly_tess_params *p, global uint *c_prims,

/* Allocate 4-byte indices */
uint32_t elsize_B = sizeof(uint32_t);
uint32_t size_B = total * elsize_B;
uint alloc_B = poly_heap_alloc_offs(p->heap, size_B);
uint64_t size_B = (uint64_t)total * elsize_B;
uint alloc_B = 0;
bool allocated = poly_heap_try_alloc_offs(p->heap, size_B, &alloc_B);
if (!allocated) {
total = 0;
p->nr_patches = 0;
}
p->index_buffer = (global uint32_t *)(((uintptr_t)p->heap->base) + alloc_B);

/* ...and now we can generate the API indexed draw */
Expand All @@ -208,6 +213,9 @@ libagx_prefix_sum_tess(global struct poly_tess_params *p, global uint *c_prims,
desc[3] = 0; /* index_bias */
desc[4] = 0; /* start_instance */

if (!allocated)
abort();

/* If necessary, increment clipper statistics too. This is only used when
* there's no geometry shader following us. See poly_nir_lower_gs.c for more
* info on the emulation. We just need to calculate the # of primitives
Expand Down
22 changes: 19 additions & 3 deletions src/asahi/libagx/query.cl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include "compiler/libcl/libcl.h"
#include "compiler/libcl/libcl_vk.h"
#include "poly/poly_heap_math.h"
#include "query.h"
#include "query_copy.h"

Expand Down Expand Up @@ -176,18 +177,33 @@ libagx_setup_indirect_dispatch(global uint *api_grid, global uint *grid,
grid[5] = local_z;
}

static inline void
libagx_apply_imm_write(constant struct libagx_imm_write *entry)
{
global uint32_t *address = entry->address;
if (entry->kind == LIBAGX_IMM_WRITE_HEAP_RESET) {
if (*address > address[1] || *address == POLY_HEAP_EXHAUSTED) {
volatile global atomic_uint *status =
(volatile global atomic_uint *)((global uchar *)address + entry->value);
atomic_store(status, 1u);
}
*address = 0;
} else {
*address = entry->value;
}
}

KERNEL(32)
libagx_write_u32s(constant struct libagx_imm_write *p)
{
uint id = cl_global_id.x;
*(p[id].address) = p[id].value;
libagx_apply_imm_write(&p[cl_global_id.x]);
}

KERNEL(1)
libagx_write_u32s_ordered(constant struct libagx_imm_write *p, uint32_t count)
{
for (uint32_t i = 0; i < count; ++i)
*(p[i].address) = p[i].value;
libagx_apply_imm_write(&p[i]);
}

/*
Expand Down
16 changes: 13 additions & 3 deletions src/asahi/libagx/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ struct libagx_xfb_counter_copy {
DEVICE(uint32_t) src[4];
};

/* What libagx_write_u32s does with an entry. */
enum libagx_imm_write_kind {
/* *address = value. */
LIBAGX_IMM_WRITE_STORE = 0,

/* Reset the heap cursor. On exhaustion, first set the device status word
* at the byte offset in value relative to the cursor. */
LIBAGX_IMM_WRITE_HEAP_RESET = 1,
};

struct libagx_imm_write {
DEVICE(uint32_t) address;
uint32_t value;
/* i386 aligns uint64_t to 4 bytes in structs: without this word the host
* packs 12-byte elements while libagx_write_u32s reads 16-byte ones. */
uint32_t pad;
/* enum libagx_imm_write_kind. It also keeps the element 16 bytes on i386,
* which aligns uint64_t to 4 bytes in structs. */
uint32_t kind;
};
static_assert(sizeof(struct libagx_imm_write) == 16,
"struct libagx_imm_write must match the device layout");
Expand Down
46 changes: 28 additions & 18 deletions src/asahi/libagx/tessellation.cl
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,28 @@ libagx_tess_setup_indirect(
*tcs_statistic += in_patches;
}

size_t draw_stride = 5 * sizeof(uint32_t);
unsigned unrolled_patches = in_patches * instance_count;

uint32_t alloc = 0;
uint32_t tcs_out_offs = alloc;
alloc += unrolled_patches * p->tcs_stride_el * 4;

uint32_t patch_coord_offs = alloc;
alloc += unrolled_patches * 4;

uint32_t count_offs = alloc;
alloc += unrolled_patches * sizeof(uint32_t);

uint vb_offs = alloc;
uint vb_size = poly_tcs_in_size(count * instance_count, vertex_outputs);
alloc += vb_size;
uint64_t patches = (uint64_t)in_patches * instance_count;
uint64_t vertices = (uint64_t)count * instance_count;
uint64_t tcs_words = min(patches, (uint64_t)UINT32_MAX + 1) * p->tcs_stride_el;
uint vertex_stride = poly_tcs_in_size(1, vertex_outputs);
/* Each count must fit the downstream 32-bit dispatch and buffer fields. */
uint64_t alloc = UINT64_MAX;
if (patches <= UINT32_MAX && vertices <= UINT32_MAX && tcs_words <= UINT32_MAX)
alloc = tcs_words * 4 + patches * 8 + vertices * vertex_stride;

uint offset = 0;
bool allocated = poly_heap_try_alloc_offs(p->heap, alloc, &offset);
if (!allocated) {
count = instance_count = in_patches = 0;
patches = 0;
}

/* Allocate all patch calculations in one go */
global uchar *blob = poly_heap_alloc(p->heap, alloc);
unsigned unrolled_patches = patches;
uint tcs_out_offs = 0;
uint patch_coord_offs = patches * p->tcs_stride_el * 4;
uint count_offs = patch_coord_offs + patches * 4;
uint vb_offs = count_offs + patches * 4;
global uchar *blob = p->heap->base + offset;

p->tcs_buffer = (global float *)(blob + tcs_out_offs);
p->patches_per_instance = in_patches;
Expand Down Expand Up @@ -104,4 +107,11 @@ libagx_tess_setup_indirect(
grids[15] = 64;
grids[16] = 1;
grids[17] = 1;

/* Gallium also uses this kernel. Preserve its abort notification after
* initializing the indirect records that follow this dispatch. */
if (!allocated) {
p->out_draws[0] = 0;
abort();
}
}
1 change: 1 addition & 0 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4524,6 +4524,7 @@ uint64_t hk_apply_conditional_rendering(struct hk_cmd_buffer *cmd,

void hk_queue_write(struct hk_cmd_buffer *cmd, uint64_t address, uint32_t value,
bool after_gfx);
void hk_queue_reset_heap(struct hk_cmd_buffer *cmd, uint64_t heap_addr);
/* Launch libagx_copy_timestamps for count copies in cs, the open
* timestamp-only compute stream, which is ordered after the stamps they
* read. */
Expand Down
8 changes: 3 additions & 5 deletions src/asahi/vulkan/hk_cmd_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7693,7 +7693,7 @@ hk_device_ensure_geometry_heap_slot(struct hk_device *dev, uint8_t slot)

perf_debug_dev(&dev->dev, "Allocating geometry heap slot %u", slot);

const size_t size = 128 * 1024 * 1024;
const size_t size = HK_GEOMETRY_HEAP_SIZE_B;
struct agx_bo *heap =
agx_bo_create(&dev->dev, size, 0, 0, "Geometry heap");
if (!heap) {
Expand Down Expand Up @@ -7778,16 +7778,14 @@ hk_heap(struct hk_cmd_buffer *cmd)
dev->rodata.heap + (uint64_t)slot * sizeof(struct poly_heap);
if (hk_heap_ring_enabled(dev)) {
hk_sync_heap_slot_used(cmd, slot);
hk_queue_write(cmd, addr + offsetof(struct poly_heap, bottom), 0,
true /* after gfx */);
hk_queue_reset_heap(cmd, addr);
} else {
/* One device-wide bump allocator: every pass that uses it runs after
* every earlier stream and its free runs after the pass. */
hk_sync_heap_used(cmd);

/* Zeroing the allocated index frees everything */
hk_queue_write(cmd, addr + offsetof(struct poly_heap, bottom), 0,
true /* after gfx */);
hk_queue_reset_heap(cmd, addr);
}

cmd->uses_heap = true;
Expand Down
23 changes: 21 additions & 2 deletions src/asahi/vulkan/hk_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,21 @@ hk_upload_rodata(struct hk_device *dev)
* own buffer object so the sampler table next to the texel-fetch sampler
* never changes after device creation.
*/
const size_t heap_descs_B = HK_HEAP_SLOTS * sizeof(struct poly_heap);
dev->rodata.heap_bo = agx_bo_create(&dev->dev,
HK_HEAP_SLOTS * sizeof(struct poly_heap),
heap_descs_B + sizeof(uint32_t),
0, 0, "Geometry heap descriptor");
if (!dev->rodata.heap_bo)
goto fail;

void *heap_map = agx_bo_map(dev->rodata.heap_bo);
if (!heap_map)
goto fail;
memset(heap_map, 0, HK_HEAP_SLOTS * sizeof(struct poly_heap));
memset(heap_map, 0, heap_descs_B + sizeof(uint32_t));
dev->rodata.heap = dev->rodata.heap_bo->va->addr;
dev->rodata.heap_overflow_addr = dev->rodata.heap + heap_descs_B;
dev->rodata.heap_overflow =
(const uint32_t *)((const uint8_t *)heap_map + heap_descs_B);

return VK_SUCCESS;

Expand Down Expand Up @@ -587,6 +591,21 @@ static VkResult
hk_check_status(struct vk_device *device)
{
struct hk_device *dev = container_of(device, struct hk_device, vk);
/* A geometry, tessellation or index pass needed more of the geometry heap
* than its slot holds: the pass dropped draws or ran on unwritten
* parameters (LIBAGX_IMM_WRITE_HEAP_RESET). The allocator's own message,
* if it printed one, comes first. Checked first: running on unwritten
* parameters may also have failed the VM, but this is the cause. */
if (dev->rodata.heap_overflow &&
p_atomic_read((uint32_t *)dev->rodata.heap_overflow)) {
u_printf_with_ctx(dev->vk.debug_output, &dev->dev.printf);
/* vk_device_set_lost() logs only to debug messengers in release
* builds; this line always reaches stderr, once. */
mesa_loge("geometry heap overflow: a geometry, tessellation or index "
"pass needed more than its %u MiB heap slot; device lost",
(unsigned)(HK_GEOMETRY_HEAP_SIZE_B >> 20));
return vk_device_set_lost(&dev->vk, "geometry heap overflow");
}
if (dev->vm_status_mirror.word) {
/* Acquire: the kernel stored the error before signalling the fence
* this thread has already observed through its syncobj wait. */
Expand Down
7 changes: 7 additions & 0 deletions src/asahi/vulkan/hk_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ struct hk_gen1_as_window {
struct hk_frame_anatomy;
struct hk_compute_kick;

/* Size of one geometry heap slot (hk_cmd_draw.c). */
#define HK_GEOMETRY_HEAP_SIZE_B (128ull * 1024 * 1024)

struct hk_device {
struct vk_device vk;
struct agx_device dev;
Expand Down Expand Up @@ -346,6 +349,10 @@ struct hk_device {
* mutation cannot reach the sampler table above. */
struct agx_bo *heap_bo;
uint64_t heap;
/* After the heap descriptors: set by the GPU when a pass ran past its
* heap (hk_queue_reset_heap()), read by hk_check_status(). */
uint64_t heap_overflow_addr;
const uint32_t *heap_overflow;
} rodata;

uint32_t heap_next_slot;
Expand Down
26 changes: 26 additions & 0 deletions src/asahi/vulkan/hk_query_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,32 @@ hk_record_stamp_copies(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
}
}

/* Free the geometry heap whose descriptor is at heap_addr after this pass,
* recording in dev->rodata.heap_overflow whether an allocation of the pass ran
* past it (LIBAGX_IMM_WRITE_HEAP_RESET; hk_check_status() reports it). */
void
hk_queue_reset_heap(struct hk_cmd_buffer *cmd, uint64_t heap_addr)
{
static_assert(offsetof(struct poly_heap, size) ==
offsetof(struct poly_heap, bottom) + sizeof(uint32_t),
"libagx_write_u32s reads size after bottom");
struct hk_device *dev = hk_cmd_buffer_device(cmd);
struct hk_cs *cs =
hk_cmd_buffer_get_cs_general(cmd, &cmd->current_cs.post_gfx, true);
if (!cs)
return;

const uint64_t bottom = heap_addr + offsetof(struct poly_heap, bottom);
const struct libagx_imm_write imm = {
.address = bottom,
.value = (uint32_t)(dev->rodata.heap_overflow_addr - bottom),
.kind = LIBAGX_IMM_WRITE_HEAP_RESET,
};
if (!cs->imm_writes.data)
cs->imm_writes = UTIL_DYNARRAY_INIT;
util_dynarray_append(&cs->imm_writes, imm);
}

void
hk_queue_write(struct hk_cmd_buffer *cmd, uint64_t address, uint32_t value,
bool after_gfx)
Expand Down
9 changes: 9 additions & 0 deletions src/asahi/vulkan/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,15 @@ if with_tests
gnu_symbol_visibility : 'hidden',
)

hk_vulkan_heap_overflow = executable(
'hk-vulkan-heap-overflow',
files('tests/hk_vulkan_heap_overflow.c'),
include_directories : [inc_include],
dependencies : [cc.find_library('vulkan')],
c_args : [c_msvc_compat_args],
gnu_symbol_visibility : 'hidden',
)

g17p_vulkan_cross_queue = executable(
'g17p-vulkan-cross-queue',
files('tests/g17p_vulkan_cross_queue.c'),
Expand Down
Loading