alex/beta-minecraft-rust
Minecraft Beta 1.7.3 Rewrite
BRANCH main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
root crates volcanic-mem src virtual_block.rs
use crate::error::{Error, Result};
use crate::tlsf::Tlsf;
/// TLSF allocator over an abstract range, e.g. descriptor slots inside a [`DescriptorHeap`](crate::DescriptorHeap)
/// or sub-ranges of a large buffer. No Vulkan memory is involved.
pub struct VirtualBlock(Tlsf);
/// A range handed out by a [`VirtualBlock`]. Must be returned to the block it came from.
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct VirtualAllocation {
node: u32,
offset: u64,
}
impl VirtualAllocation {
#[inline]
pub fn offset(&self) -> u64 {
self.offset
}
}
impl VirtualBlock {
pub fn new(size: u64) -> Result<Self> {
if size == 0 {
return Err(Error::ZeroSize);
}
Ok(Self(Tlsf::new(size, 1)))
}
#[inline]
pub fn allocate(&mut self, size: u64, alignment: u64) -> Result<VirtualAllocation> {
if size == 0 {
return Err(Error::ZeroSize);
}
if !alignment.is_power_of_two() {
return Err(Error::InvalidAlignment(alignment));
}
let (node, offset) = self
.0
.allocate(size, alignment, 0)
.ok_or(Error::OutOfMemory)?;
Ok(VirtualAllocation { node, offset })
}
#[inline]
pub fn free(&mut self, allocation: VirtualAllocation) {
self.0.free(allocation.node);
}
#[inline]
pub fn allocation_size(&self, allocation: &VirtualAllocation) -> u64 {
self.0.allocation_size(allocation.node)
}
#[inline]
pub fn size(&self) -> u64 {
self.0.size()
}
#[inline]
pub fn free_bytes(&self) -> u64 {
self.0.free_bytes()
}
#[inline]
pub fn allocation_count(&self) -> u32 {
self.0.allocation_count()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Checks all internal invariants, panicking on corruption. Intended for tests.
#[doc(hidden)]
pub fn validate(&self) {
self.0.validate();
}
}