Switch util::* to DeviceSize
This commit is contained in:
parent
0f8ac7ecff
commit
90d67c29c9
3 changed files with 44 additions and 31 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::iter::Iterator;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
use vk;
|
||||
|
||||
/// AlignByteSlice is the dynamic alternative to `Align`. Sometimes the user wants to
|
||||
/// align slices at runtime. One example would be to align different images in one buffer.
|
||||
|
@ -9,8 +10,8 @@ use std::mem::size_of;
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct AlignByteSlice {
|
||||
ptr: *mut (),
|
||||
alignment: usize,
|
||||
size: usize,
|
||||
alignment: vk::DeviceSize,
|
||||
size: vk::DeviceSize,
|
||||
}
|
||||
|
||||
impl AlignByteSlice {
|
||||
|
@ -23,7 +24,7 @@ impl AlignByteSlice {
|
|||
let ptr = (self.ptr as *mut u8).offset(current as isize);
|
||||
let raw_slice = ::std::slice::from_raw_parts_mut(ptr, slice.len());
|
||||
raw_slice.copy_from_slice(slice);
|
||||
current += slice.len();
|
||||
current += slice.len() as vk::DeviceSize;
|
||||
let padding = current % self.alignment;
|
||||
current += padding;
|
||||
}
|
||||
|
@ -32,7 +33,7 @@ impl AlignByteSlice {
|
|||
}
|
||||
|
||||
impl AlignByteSlice {
|
||||
pub unsafe fn new(ptr: *mut (), alignment: usize, size: usize) -> Self {
|
||||
pub unsafe fn new(ptr: *mut (), alignment: vk::DeviceSize, size: vk::DeviceSize) -> Self {
|
||||
AlignByteSlice {
|
||||
ptr,
|
||||
size,
|
||||
|
@ -52,15 +53,15 @@ impl AlignByteSlice {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Align<T> {
|
||||
ptr: *mut (),
|
||||
offset: usize,
|
||||
size: usize,
|
||||
offset: vk::DeviceSize,
|
||||
size: vk::DeviceSize,
|
||||
_m: PhantomData<T>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AlignIter<'a, T: 'a> {
|
||||
align: &'a mut Align<T>,
|
||||
current: usize,
|
||||
current: vk::DeviceSize,
|
||||
}
|
||||
|
||||
impl<T: Copy> Align<T> {
|
||||
|
@ -71,13 +72,14 @@ impl<T: Copy> Align<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn calc_padding(adr: usize, align: usize) -> usize {
|
||||
fn calc_padding(adr: vk::DeviceSize, align: vk::DeviceSize) -> vk::DeviceSize {
|
||||
(align - adr % align) % align
|
||||
}
|
||||
|
||||
impl<T> Align<T> {
|
||||
pub unsafe fn new(ptr: *mut (), alignment: usize, size: usize) -> Self {
|
||||
let offset = size_of::<T>() + calc_padding(size_of::<T>(), alignment);
|
||||
pub unsafe fn new(ptr: *mut (), alignment: vk::DeviceSize, size: vk::DeviceSize) -> Self {
|
||||
let offset = size_of::<T>() as vk::DeviceSize +
|
||||
calc_padding(size_of::<T>() as vk::DeviceSize, alignment);
|
||||
Align {
|
||||
ptr,
|
||||
offset,
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#[macro_use]
|
||||
extern crate ash;
|
||||
#[macro_use]
|
||||
extern crate examples;
|
||||
|
@ -21,6 +20,13 @@ struct Vertex {
|
|||
uv: [f32; 2],
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy)]
|
||||
pub struct Vector3 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
pub z: f32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let base = ExampleBase::new(1920, 1080);
|
||||
|
@ -145,8 +151,8 @@ fn main() {
|
|||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut index_slice = Align::new(index_ptr,
|
||||
index_buffer_memory_req.alignment as usize,
|
||||
index_buffer_memory_req.size as usize);
|
||||
index_buffer_memory_req.alignment,
|
||||
index_buffer_memory_req.size);
|
||||
index_slice.copy_from_slice(&index_buffer_data);
|
||||
base.device.unmap_memory(index_buffer_memory);
|
||||
base.device
|
||||
|
@ -207,15 +213,19 @@ fn main() {
|
|||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut slice = Align::new(vert_ptr,
|
||||
vertex_input_buffer_memory_req.alignment as usize,
|
||||
vertex_input_buffer_memory_req.size as usize);
|
||||
vertex_input_buffer_memory_req.alignment,
|
||||
vertex_input_buffer_memory_req.size);
|
||||
slice.copy_from_slice(&vertices);
|
||||
base.device.unmap_memory(vertex_input_buffer_memory);
|
||||
base.device
|
||||
.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0)
|
||||
.unwrap();
|
||||
|
||||
let uniform_color_buffer_data = [0.0f32, 0.0, 1.0];
|
||||
let uniform_color_buffer_data = Vector3 {
|
||||
x: 1.0,
|
||||
y: 1.0,
|
||||
z: 1.0,
|
||||
};
|
||||
let uniform_color_buffer_info = vk::BufferCreateInfo {
|
||||
s_type: vk::StructureType::BufferCreateInfo,
|
||||
p_next: ptr::null(),
|
||||
|
@ -247,15 +257,16 @@ fn main() {
|
|||
let uniform_color_buffer_memory = base.device
|
||||
.allocate_memory(&uniform_color_buffer_allocate_info, None)
|
||||
.unwrap();
|
||||
//let mut uniform_slice = base.device
|
||||
// .map_memory::<f32>(uniform_color_buffer_memory,
|
||||
// 0,
|
||||
// 12,
|
||||
// vk::MemoryMapFlags::empty(),
|
||||
// 4)
|
||||
// .unwrap();
|
||||
// println!("{:?}", uniform_slice);
|
||||
// uniform_slice.copy_from_slice(&uniform_color_buffer_data[..]);
|
||||
let uniform_ptr = base.device
|
||||
.map_memory(uniform_color_buffer_memory,
|
||||
0,
|
||||
uniform_color_buffer_memory_req.size,
|
||||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut uniform_aligned_slice = Align::new(uniform_ptr,
|
||||
uniform_color_buffer_memory_req.alignment,
|
||||
uniform_color_buffer_memory_req.size);
|
||||
uniform_aligned_slice.copy_from_slice(&[uniform_color_buffer_data]);
|
||||
base.device.unmap_memory(uniform_color_buffer_memory);
|
||||
base.device
|
||||
.bind_buffer_memory(uniform_color_buffer, uniform_color_buffer_memory, 0)
|
||||
|
@ -298,8 +309,8 @@ fn main() {
|
|||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut image_slice = AlignByteSlice::new(image_ptr,
|
||||
image_buffer_memory_req.alignment as usize,
|
||||
image_buffer_memory_req.size as usize);
|
||||
image_buffer_memory_req.alignment,
|
||||
image_buffer_memory_req.size);
|
||||
image_slice.copy_from_slices(&[&image_data]);
|
||||
base.device.unmap_memory(image_buffer_memory);
|
||||
base.device
|
||||
|
|
|
@ -143,8 +143,8 @@ fn main() {
|
|||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut index_slice = Align::new(index_ptr,
|
||||
index_buffer_memory_req.alignment as usize,
|
||||
index_buffer_memory_req.size as usize);
|
||||
index_buffer_memory_req.alignment,
|
||||
index_buffer_memory_req.size);
|
||||
index_slice.copy_from_slice(&index_buffer_data);
|
||||
base.device.unmap_memory(index_buffer_memory);
|
||||
base.device
|
||||
|
@ -201,8 +201,8 @@ fn main() {
|
|||
vk::MemoryMapFlags::empty())
|
||||
.unwrap();
|
||||
let mut vert_align = Align::new(vert_ptr,
|
||||
vertex_input_buffer_memory_req.alignment as usize,
|
||||
vertex_input_buffer_memory_req.size as usize);
|
||||
vertex_input_buffer_memory_req.alignment,
|
||||
vertex_input_buffer_memory_req.size);
|
||||
vert_align.copy_from_slice(&vertices);
|
||||
base.device.unmap_memory(vertex_input_buffer_memory);
|
||||
base.device
|
||||
|
|
Loading…
Add table
Reference in a new issue