Skip to content

Commit

Permalink
add: wip buffer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAPenguin0 committed Aug 29, 2023
1 parent a775036 commit c4ec598
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/allocator/scratch_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<A: Allocator> ScratchAllocator<A> {
/// # use phobos::*;
/// # use anyhow::Result;
/// fn make_scratch_allocator<A: Allocator>(device: Device, alloc: &mut A) -> Result<ScratchAllocator<A>> {
/// ScratchAllocator::new(device, alloc, 1024 as usize)
/// ScratchAllocator::new(device, alloc, 1024 as u64)
/// }
/// ```
pub fn new(
Expand Down
16 changes: 8 additions & 8 deletions tests/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod framework;

#[test]
pub fn basic_allocator_usage() -> Result<()> {
let context = framework::make_context()?;
let context = framework::make_context().expect("Can initialize context.");
let mut allocator = context.allocator.clone();
let allocation = allocator.allocate(
"allocation",
Expand All @@ -32,7 +32,7 @@ pub fn basic_allocator_usage() -> Result<()> {

#[test]
pub fn cpu_to_gpu_is_mappable() -> Result<()> {
let context = framework::make_context()?;
let context = framework::make_context().expect("Can initialize context.");
let mut allocator = context.allocator.clone();
let allocation = allocator.allocate(
"allocation",
Expand All @@ -55,7 +55,7 @@ const CHUNK_SIZE: u64 = 32768;

#[test]
pub fn make_scratch_allocator() -> Result<()> {
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");

let _scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, CHUNK_SIZE)?;
Expand All @@ -64,7 +64,7 @@ pub fn make_scratch_allocator() -> Result<()> {

#[test]
pub fn use_scratch_allocator() -> Result<()> {
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");
let mut scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, CHUNK_SIZE)?;
// Try allocating a buffer that should fit in the scratch allocator's memory.
Expand All @@ -75,7 +75,7 @@ pub fn use_scratch_allocator() -> Result<()> {
#[test]
pub fn use_entire_scratch_allocator() -> Result<()> {
// Try allocating the entire scratch allocator's memory for a single buffer
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");
let mut scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, CHUNK_SIZE)?;
let _buffer = scratch_allocator.allocate(1024 as u64)?;
Expand All @@ -84,7 +84,7 @@ pub fn use_entire_scratch_allocator() -> Result<()> {

#[test]
pub fn scratch_allocator_allocate_new_chunks() -> Result<()> {
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");
let mut scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, 1024)?;
// First allocate a smaller buffer
Expand All @@ -96,7 +96,7 @@ pub fn scratch_allocator_allocate_new_chunks() -> Result<()> {

#[test]
pub fn reset_scratch_allocator() -> Result<()> {
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");
let mut scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, CHUNK_SIZE)?;
// Allocate a first buffer.
Expand All @@ -109,7 +109,7 @@ pub fn reset_scratch_allocator() -> Result<()> {

#[test]
pub fn scratch_allocator_mass_allocate() -> Result<()> {
let mut context = framework::make_context()?;
let mut context = framework::make_context().expect("Can initialize context.");
let mut scratch_allocator =
ScratchAllocator::new(context.device.clone(), &mut context.allocator, CHUNK_SIZE)?;

Expand Down
134 changes: 134 additions & 0 deletions tests/buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use anyhow::Result;
use ash::vk;
use ash::vk::Handle;

use phobos::{Buffer, MemoryType};

mod framework;

#[test]
pub fn alloc_buffer() -> Result<()> {
let mut context = framework::make_context().expect("Can initialize context.");

const ALLOC_SIZE: u64 = 1024u64;

let buffer = Buffer::new(
context.device.clone(),
&mut context.allocator,
ALLOC_SIZE,
MemoryType::GpuOnly,
)?;

assert_ne!(unsafe { buffer.handle().as_raw() }, 0, "Buffer handle should not be null.");
assert!(
buffer.size() >= ALLOC_SIZE,
"Allocated buffer should at least fit requested size."
);
assert_ne!(buffer.address(), 0, "Buffer device address should not be null.");

Ok(())
}

#[test]
pub fn alloc_aligned_buffer() -> Result<()> {
let mut context = framework::make_context().expect("Can initialize context.");

const ALLOC_SIZE: u64 = 1000u64;
const ALIGN: u64 = 128;

let buffer = Buffer::new_aligned(
context.device.clone(),
&mut context.allocator,
ALLOC_SIZE,
ALIGN,
MemoryType::GpuOnly,
)?;

assert_ne!(unsafe { buffer.handle().as_raw() }, 0, "Buffer handle should not be null.");
assert!(
buffer.size() >= ALLOC_SIZE,
"Allocated buffer should at least fit requested size."
);
assert_eq!(buffer.size() % ALIGN, 0, "Buffer size should be aligned correctly.");
assert_eq!(buffer.address() % ALIGN, 0, "Buffer address should be aligned correctly.");

Ok(())
}

#[test]
pub fn buffer_view_full() -> Result<()> {
let mut context = framework::make_context().expect("Can initialize context.");

const ALLOC_SIZE: u64 = 1024u64;

let buffer = Buffer::new(
context.device.clone(),
&mut context.allocator,
ALLOC_SIZE,
MemoryType::GpuOnly,
)?;

let view = buffer.view_full();
assert_eq!(view.address(), buffer.address(), "Full view should have same address as buffer");
assert_eq!(view.offset(), 0, "Full view should have zero offset");
assert_eq!(view.size(), buffer.size(), "Full view should have same size as buffer");
assert_eq!(
unsafe { view.handle() },
unsafe { buffer.handle() },
"View and buffer should refer to the same buffer"
);

Ok(())
}

#[test]
pub fn buffer_view() -> Result<()> {
let mut context = framework::make_context().expect("Can initialize context.");

const ALLOC_SIZE: u64 = 1024u64;

let buffer = Buffer::new(
context.device.clone(),
&mut context.allocator,
ALLOC_SIZE,
MemoryType::GpuOnly,
)?;

let view = buffer
.view(ALLOC_SIZE / 2, ALLOC_SIZE / 2)
.expect("Can view second half of buffer");
assert_eq!(
view.address(),
buffer.address() + ALLOC_SIZE / 2,
"Buffer address respects pointer arithmetic"
);
assert_eq!(view.offset(), ALLOC_SIZE / 2, "View should have correct offset");
assert_eq!(view.size(), ALLOC_SIZE / 2, "View should have correct size");
assert_eq!(
unsafe { view.handle() },
unsafe { buffer.handle() },
"View and buffer should refer to the same buffer"
);

Ok(())
}

#[test]
pub fn invalid_buffer_view() -> Result<()> {
let mut context = framework::make_context().expect("Can initialize context.");

const ALLOC_SIZE: u64 = 1024u64;

let buffer = Buffer::new(
context.device.clone(),
&mut context.allocator,
ALLOC_SIZE,
MemoryType::GpuOnly,
)?;

buffer
.view(0u64, ALLOC_SIZE * 2)
.expect_err("Cannot create view outside of buffer range");

Ok(())
}
4 changes: 2 additions & 2 deletions tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn requesting_raytracing_does_not_fail() -> Result<()> {

#[test]
pub fn vulkan_loaded() -> Result<()> {
let context = framework::make_context()?;
let context = framework::make_context().expect("Can initialize context.");
let handle = context.instance.handle();
assert_ne!(handle.as_raw(), 0, "VkInstance handle should not be zero");
let loader = unsafe { context.instance.loader() };
Expand All @@ -65,7 +65,7 @@ pub fn vulkan_loaded() -> Result<()> {

#[test]
pub fn valid_device() -> Result<()> {
let context = framework::make_context()?;
let context = framework::make_context().expect("Can initialize context.");
let handle = unsafe { context.device.handle() };
assert_ne!(handle.handle().as_raw(), 0, "VkDevice handle should not be zero");
// Also try a vulkan function call on it to make sure it is loaded properly
Expand Down

0 comments on commit c4ec598

Please sign in to comment.