mirror of
https://github.com/vulkano-rs/vulkano.git
synced 2024-11-22 14:56:42 +00:00
Add some validity tests and fix update buffer type safety
This commit is contained in:
parent
a1d8c93ded
commit
07288af2ef
@ -443,9 +443,10 @@ impl<P> AutoCommandBufferBuilder<P> {
|
||||
///
|
||||
/// If `data` is larger than the buffer, only the part of `data` that fits is written. If the
|
||||
/// buffer is larger than `data`, only the start of the buffer is written.
|
||||
// TODO: allow unsized values
|
||||
#[inline]
|
||||
pub fn update_buffer<B, D>(mut self, buffer: B, data: D) -> Result<Self, UpdateBufferError>
|
||||
where B: BufferAccess + Send + Sync + 'static,
|
||||
where B: TypedBufferAccess<Content = D> + Send + Sync + 'static,
|
||||
D: Send + Sync + 'static
|
||||
{
|
||||
unsafe {
|
||||
|
@ -67,3 +67,34 @@ impl fmt::Display for CheckFillBufferError {
|
||||
write!(fmt, "{}", error::Error::description(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter;
|
||||
use buffer::BufferUsage;
|
||||
use buffer::CpuAccessibleBuffer;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn missing_usage() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::vertex_buffer(),
|
||||
iter::once(queue.family()), 0u32).unwrap();
|
||||
|
||||
match check_fill_buffer(&device, &buffer) {
|
||||
Err(CheckFillBufferError::BufferMissingUsage) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn wrong_device() {
|
||||
let (dev1, queue) = gfx_dev_and_queue!();
|
||||
let (dev2, _) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_data(dev1, BufferUsage::all(),
|
||||
iter::once(queue.family()), 0u32).unwrap();
|
||||
|
||||
let _ = check_fill_buffer(&dev2, &buffer);
|
||||
}
|
||||
}
|
||||
|
@ -86,3 +86,51 @@ impl fmt::Display for CheckIndexBufferError {
|
||||
write!(fmt, "{}", error::Error::description(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter;
|
||||
use buffer::BufferUsage;
|
||||
use buffer::CpuAccessibleBuffer;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn num_indices() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::index_buffer(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 500u32).unwrap();
|
||||
|
||||
match check_index_buffer(&device, &buffer) {
|
||||
Ok(CheckIndexBuffer { num_indices }) => {
|
||||
assert_eq!(num_indices, 500);
|
||||
},
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_usage() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 500u32).unwrap();
|
||||
|
||||
match check_index_buffer(&device, &buffer) {
|
||||
Err(CheckIndexBufferError::BufferMissingUsage) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn wrong_device() {
|
||||
let (dev1, queue) = gfx_dev_and_queue!();
|
||||
let (dev2, _) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(dev1, BufferUsage::all(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 500u32).unwrap();
|
||||
|
||||
let _ = check_index_buffer(&dev2, &buffer);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ use std::fmt;
|
||||
use std::mem;
|
||||
|
||||
use VulkanObject;
|
||||
use buffer::BufferAccess;
|
||||
use buffer::TypedBufferAccess;
|
||||
use device::Device;
|
||||
use device::DeviceOwned;
|
||||
|
||||
@ -25,7 +25,7 @@ use device::DeviceOwned;
|
||||
///
|
||||
pub fn check_update_buffer<B, D>(device: &Device, buffer: &B, data: &D)
|
||||
-> Result<(), CheckUpdateBufferError>
|
||||
where B: ?Sized + BufferAccess,
|
||||
where B: ?Sized + TypedBufferAccess<Content = D>,
|
||||
D: ?Sized
|
||||
{
|
||||
assert_eq!(buffer.inner().buffer.device().internal_object(),
|
||||
@ -84,3 +84,77 @@ impl fmt::Display for CheckUpdateBufferError {
|
||||
write!(fmt, "{}", error::Error::description(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter;
|
||||
use buffer::BufferAccess;
|
||||
use buffer::BufferUsage;
|
||||
use buffer::CpuAccessibleBuffer;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn missing_usage() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::vertex_buffer(),
|
||||
iter::once(queue.family()), 0u32).unwrap();
|
||||
|
||||
match check_update_buffer(&device, &buffer, &0) {
|
||||
Err(CheckUpdateBufferError::BufferMissingUsage) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_too_large() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::transfer_dest(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 65536).unwrap();
|
||||
let data = (0 .. 65536).collect::<Vec<u32>>();
|
||||
|
||||
match check_update_buffer(&device, &buffer, &data[..]) {
|
||||
Err(CheckUpdateBufferError::DataTooLarge) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_just_large_enough() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::transfer_dest(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 100000).unwrap();
|
||||
let data = (0 .. 65536).collect::<Vec<u8>>();
|
||||
|
||||
match check_update_buffer(&device, &buffer, &data[..]) {
|
||||
Ok(_) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_alignment() {
|
||||
let (device, queue) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::transfer_dest(),
|
||||
iter::once(queue.family()),
|
||||
0 .. 100).unwrap();
|
||||
let data = (0 .. 30).collect::<Vec<u8>>();
|
||||
|
||||
match check_update_buffer(&device, &buffer.slice(1 .. 50).unwrap(), &data[..]) {
|
||||
Err(CheckUpdateBufferError::WrongAlignment) => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn wrong_device() {
|
||||
let (dev1, queue) = gfx_dev_and_queue!();
|
||||
let (dev2, _) = gfx_dev_and_queue!();
|
||||
let buffer = CpuAccessibleBuffer::from_data(dev1, BufferUsage::all(),
|
||||
iter::once(queue.family()), 0u32).unwrap();
|
||||
|
||||
let _ = check_update_buffer(&dev2, &buffer, &0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user