Expand description
A pool that packs many small ranges into one GPU buffer.
Uploading a mesh per buffer is wasteful: every wgpu::Buffer carries
driver overhead, and a draw can only bind a handful of them. A
BufferPool instead owns one large buffer and hands out byte ranges
inside it with an Allocator, so
every mesh of a kind shares the same buffer.
A range is a BufferRange: a byte offset and a size, plus the
Allocation that keeps the range reserved until it is handed back with
BufferPool::release. A range names no buffer of its own, so it stays
cheap to store and copy; read the buffer from BufferPool::buffer
whenever you need to bind or write into it.
Ranges never move while they live — growing the pool copies the buffer and appends free space rather than re-packing — so an offset stays valid across a grow and only the buffer handle changes.
Every range starts at a multiple of COPY_BUFFER_ALIGNMENT, which is
what a GPU buffer sub-allocation needs. The pool grows by 1.5x when a
range does not fit, copying the old contents into the new buffer.
§Sizing
A range’s allocation covers the requested size rounded up to the
alignment, which BufferRange::size reports. Sizing the pool with
min_allocator_size is not
needed here: the pool grows on demand.
§Example
use unlit_wgpu::buffer_pool::BufferPool;
let (device, queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default());
let mut pool = BufferPool::new(
&device,
"example::pool",
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
1024,
);
let range = pool.allocate(&device, &queue, 64).expect("the pool has room");
queue.write_buffer(pool.buffer(), range.allocation.offset as u64, &[0u8; 64]);
pool.release(range.allocation);Re-exports§
use crate::offset_allocator::Allocation;🔒use crate::offset_allocator::Allocator;🔒use crate::offset_allocator::min_allocator_size;🔒use core::num::NonZeroU32;🔒
Structs§
- Buffer
Pool - One GPU buffer sub-allocated into many ranges.
- Buffer
Range - A byte range inside a
BufferPool’s buffer.
Constants§
- ALIGNMENT 🔒
- The alignment every range starts at, in bytes.
- DEFAULT_
MAX_ 🔒RANGES - How many ranges a pool tracks unless the caller says otherwise.
- MIN_
SIZE 🔒 - The smallest pool size, so that the first grow has something to grow from.
Functions§
- create_
buffer 🔒 - Creates the pool’s buffer with the usages it needs to grow.
- round_
up_ 🔒size - Rounds a byte size up to the pool’s alignment, with a floor of one unit.