Browse Source

add index buffer handling

master
Alison Watson 1 year ago
parent
commit
c4ef21e933
3 changed files with 123 additions and 38 deletions
  1. +27
    -12
      source/main.rs
  2. +74
    -18
      source/render/buffer.rs
  3. +22
    -8
      source/render/cmd.rs

+ 27
- 12
source/main.rs View File

@@ -41,6 +41,7 @@ impl SwapchainData<'_> {
shader_frag: Rc<ShaderModule>,
layout: Rc<PipelineLayout>,
vertex_buffer: Rc<Buffer>,
index_buffer: Rc<Buffer>,
queue_graphics: &Queue,
queue_surface: &Queue,
conf: &render::Conf,
@@ -93,7 +94,9 @@ impl SwapchainData<'_> {
swapchain.extent,
|cmd| {
cmd.bind_pipeline(pipelines.main.clone());
cmd.bind_buffer(vertex_buffer.clone(), VERTICES.len());
cmd.bind_vertex_buffer(vertex_buffer.clone());
cmd.bind_index_buffer(index_buffer.clone());
cmd.draw(INDICES.len());
},
)?;

@@ -101,17 +104,6 @@ impl SwapchainData<'_> {
}
}

const VERTICES: &[Vertex] = &[
Vertex { pos: Vector2::new( 0.0, -0.5), color: Vector3::new(1.0, 0.0, 0.0) },
Vertex { pos: Vector2::new( 0.5, 0.5), color: Vector3::new(0.0, 1.0, 0.0) },
Vertex { pos: Vector2::new(-0.5, 0.5), color: Vector3::new(0.0, 0.0, 1.0) },
];

const MAIN_VERT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.vert.o"));
const MAIN_FRAG: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.frag.o"));

fn draw_frame<'a>(
device: &Device,
swapchain: &Swapchain,
@@ -253,6 +245,13 @@ fn fallback_main(
&queue_graphics,
)?;

let index_buffer = Buffer::create_index(
device.clone(),
&phys_device,
INDICES,
&queue_graphics,
)?;

let image_avail_s = Semaphore::create_all(device.clone(), concur_frames)?;
let render_fini_s = Semaphore::create_all(device.clone(), concur_frames)?;

@@ -274,6 +273,7 @@ fn fallback_main(
shader_frag.clone(),
layout.clone(),
vertex_buffer.clone(),
index_buffer.clone(),
&queue_graphics,
&queue_surface,
&conf.render,
@@ -312,6 +312,7 @@ fn fallback_main(
shader_frag.clone(),
layout.clone(),
vertex_buffer.clone(),
index_buffer.clone(),
&queue_graphics,
&queue_surface,
&conf.render,
@@ -351,4 +352,18 @@ fn main() {
}
}

const VERTICES: &[Vertex] = &[
Vertex { pos: Vector2::new(-0.5, -0.5), color: Vector3::new(1.0, 0.0, 0.0) },
Vertex { pos: Vector2::new( 0.5, -0.5), color: Vector3::new(0.0, 1.0, 0.0) },
Vertex { pos: Vector2::new( 0.5, 0.5), color: Vector3::new(0.0, 0.0, 1.0) },
Vertex { pos: Vector2::new(-0.5, 0.5), color: Vector3::new(1.0, 1.0, 1.0) },
];

const INDICES: &[u16] = &[0, 1, 2, 2, 3, 0];

const MAIN_VERT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.vert.o"));
const MAIN_FRAG: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.frag.o"));

// EOF

+ 74
- 18
source/render/buffer.rs View File

@@ -78,6 +78,38 @@ unsafe fn create_buffer(
Ok((handle, memory))
}

unsafe fn copy_buffer(
src_handle: vk::Buffer,
dst_handle: vk::Buffer,
size: vk::DeviceSize,
device: Rc<Device>,
xfer_queue: &Queue,
) -> Result<(), vk::Result> {
let pool = CommandPool::create_transfer(
device.clone(),
xfer_queue.index,
|cmd| cmd.transfer(src_handle, dst_handle, size),
)?;

let submit_buffers = pool.buf_handles();

let submit_info = [
vk::SubmitInfo {
command_buffer_count: submit_buffers.len() as u32,
p_command_buffers: submit_buffers.as_ptr(),
..Default::default()
}
];

device.queue_submit(
**xfer_queue,
&submit_info,
vk::Fence::null(),
)?;

device.queue_wait_idle(**xfer_queue)
}

impl Buffer {
pub fn create_vertex(
device: Rc<Device>,
@@ -112,30 +144,54 @@ impl Buffer {
vk::MemoryPropertyFlags::DEVICE_LOCAL,
)?;

let pool = CommandPool::create_transfer(
device.clone(),
xfer_queue.index,
|cmd| cmd.transfer(src_handle, dst_handle, size),
)?;
copy_buffer(src_handle, dst_handle, size, device.clone(), xfer_queue)?;
// TODO: express with Drop
device.free_memory(src_memory, None);
device.destroy_buffer(src_handle, None);

let submit_buffers = pool.buf_handles();
(dst_handle, dst_memory)
};

let submit_info = [
vk::SubmitInfo {
command_buffer_count: submit_buffers.len() as u32,
p_command_buffers: submit_buffers.as_ptr(),
..Default::default()
}
];
Ok(Rc::new(Self { handle, device, memory }))
}

device.queue_submit(
**xfer_queue,
&submit_info,
vk::Fence::null(),
pub fn create_index(
device: Rc<Device>,
phys_device: &PhysicalDevice,
indices: &[u16],
xfer_queue: &Queue,
) -> Result<Rc<Self>, ErrBufferCreate> {
let size = std::mem::size_of::<u16>() * indices.len();
let size = size as vk::DeviceSize;

let (handle, memory) = unsafe {
let (src_handle, src_memory) = create_buffer(
&device,
phys_device,
size,
vk::BufferUsageFlags::TRANSFER_SRC,
vk::MemoryPropertyFlags::HOST_VISIBLE |
vk::MemoryPropertyFlags::HOST_COHERENT,
)?;

let mem_flg = vk::MemoryMapFlags::empty();
let mem_map = device.map_memory(src_memory, 0, size, mem_flg)?;
(mem_map as *mut u16).copy_from(indices.as_ptr(), indices.len());
device.unmap_memory(src_memory);

let (dst_handle, dst_memory) = create_buffer(
&device,
phys_device,
size,
vk::BufferUsageFlags::TRANSFER_DST |
vk::BufferUsageFlags::INDEX_BUFFER,
vk::MemoryPropertyFlags::DEVICE_LOCAL,
)?;

device.queue_wait_idle(**xfer_queue)?;
copy_buffer(src_handle, dst_handle, size, device.clone(), xfer_queue)?;

// TODO: express with Drop
device.free_memory(src_memory, None);
device.destroy_buffer(src_handle, None);



+ 22
- 8
source/render/cmd.rs View File

@@ -21,6 +21,7 @@ pub struct CommandBuffer {
framebuffer: Option<Rc<Framebuffer>>,
pipeline: Option<Rc<Pipeline>>,
vertex_buffer: Option<Rc<Buffer>>,
index_buffer: Option<Rc<Buffer>>,
}

impl CommandPool {
@@ -142,21 +143,32 @@ impl CommandBuffer {
}
}

pub fn bind_buffer(&mut self, vertex_buffer: Rc<Buffer>, num_vert: usize) {
let vertex_buffers = [ **vertex_buffer ];
let vertex_offsets = [ 0 ];

pub fn bind_vertex_buffer(&mut self, buffer: Rc<Buffer>) {
unsafe {
self.device.cmd_bind_vertex_buffers(
self.handle,
0,
&vertex_buffers,
&vertex_offsets,
&[ **buffer ],
&[ 0 ],
);

self.device.cmd_draw(self.handle, num_vert as u32, 1, 0, 0);
self.vertex_buffer = Some(buffer);
}
}

pub fn bind_index_buffer(&mut self, buffer: Rc<Buffer>) {
let ty = vk::IndexType::UINT16;

unsafe {
self.device.cmd_bind_index_buffer(self.handle, **buffer, 0, ty);

self.vertex_buffer = Some(vertex_buffer);
self.index_buffer = Some(buffer);
}
}

pub fn draw(&self, num: usize) {
unsafe {
self.device.cmd_draw_indexed(self.handle, num as u32, 1, 0, 0, 0);
}
}

@@ -213,6 +225,7 @@ impl CommandBuffer {
framebuffer: Some(framebuffer),
pipeline: None,
vertex_buffer: None,
index_buffer: None,
};

unsafe {
@@ -251,6 +264,7 @@ impl CommandBuffer {
framebuffer: None,
pipeline: None,
vertex_buffer: None,
index_buffer: None,
};

unsafe {


Loading…
Cancel
Save