vello/piet-gpu/shader/mem.h
Elias Naur 4de67d9081 unify GPU memory management
Merge all static and dynamic buffers to just one, "memory". Add a malloc
function for dynamic allocations.

Unify static allocation offsets into a "config" buffer containing scene setup
(number of paths, number of path segments), as well as the memory offsets of
the static allocations.

Finally, set an overflow flag when an allocation fail, and make sure to exit
shader execution as soon as that triggers. Add checks before beginning
execution in case the client wants to run two or more shaders before checking
the flag.

The "state" buffer is left alone because it needs zero'ing and because it is
accessed with the "volatile" keyword.

Fixes #40

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-12-27 20:24:29 +01:00

30 lines
748 B
C

// SPDX-License-Identifier: Apache-2.0 OR MIT OR Unlicense
layout(set = 0, binding = 0) buffer Memory {
// offset into memory of the next allocation, initialized by the user.
uint mem_offset;
bool mem_overflow;
uint[] memory;
};
// Alloc represents a memory allocation.
struct Alloc {
// offset in bytes into memory.
uint offset;
// failed is true if the allocation overflowed memory.
bool failed;
};
// malloc allocates size bytes of memory.
Alloc malloc(uint size) {
Alloc a;
// Round up to nearest 32-bit word.
size = (size + 3) & ~3;
a.offset = atomicAdd(mem_offset, size);
a.failed = a.offset + size > memory.length() * 4;
if (a.failed) {
mem_overflow = true;
}
return a;
}