librashader/librashader-runtime-vk/src/draw_quad.rs

80 lines
1.9 KiB
Rust
Raw Normal View History

2022-12-25 17:18:11 +11:00
use crate::error;
use crate::memory::VulkanBuffer;
2023-02-06 08:17:23 +11:00
use ash::vk;
use gpu_allocator::vulkan::Allocator;
2023-02-06 08:17:23 +11:00
use librashader_runtime::quad::QuadType;
use parking_lot::RwLock;
2023-02-06 08:17:23 +11:00
use std::sync::Arc;
2022-12-25 17:18:11 +11:00
2022-12-22 14:03:38 +11:00
#[rustfmt::skip]
static VBO_OFFSCREEN: &[f32; 16] = &[
// Offscreen
-1.0, -1.0, 0.0, 0.0,
-1.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
];
#[rustfmt::skip]
static VBO_DEFAULT_FINAL: &[f32; 16] = &[
2022-12-11 17:06:28 +11:00
// Final
2022-12-22 14:03:38 +11:00
0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
2022-12-11 17:06:28 +11:00
];
2022-12-25 17:18:11 +11:00
pub struct DrawQuad {
buffer: VulkanBuffer,
2023-02-06 08:17:23 +11:00
device: Arc<ash::Device>,
2022-12-25 17:18:11 +11:00
}
impl DrawQuad {
2023-02-06 08:17:23 +11:00
pub fn new(
device: &Arc<ash::Device>,
allocator: &Arc<RwLock<Allocator>>,
2023-02-06 08:17:23 +11:00
) -> error::Result<DrawQuad> {
let mut buffer = VulkanBuffer::new(
device,
allocator,
2023-02-06 08:17:23 +11:00
vk::BufferUsageFlags::VERTEX_BUFFER,
2 * std::mem::size_of::<[f32; 16]>(),
)?;
2022-12-25 17:18:11 +11:00
{
let slice = buffer.as_mut_slice()?;
slice[0..std::mem::size_of::<[f32; 16]>()]
.copy_from_slice(bytemuck::cast_slice(VBO_OFFSCREEN));
slice[std::mem::size_of::<[f32; 16]>()..]
.copy_from_slice(bytemuck::cast_slice(VBO_DEFAULT_FINAL));
2022-12-25 17:18:11 +11:00
}
2022-12-25 17:18:11 +11:00
Ok(DrawQuad {
buffer,
2023-02-06 08:17:23 +11:00
device: device.clone(),
2022-12-25 17:18:11 +11:00
})
}
pub fn bind_vbo_for_frame(&self, cmd: vk::CommandBuffer) {
2022-12-25 17:18:11 +11:00
unsafe {
2023-02-06 08:17:23 +11:00
self.device.cmd_bind_vertex_buffers(
cmd,
0,
&[self.buffer.handle],
&[0 as vk::DeviceSize],
2023-02-06 08:17:23 +11:00
)
2022-12-25 17:18:11 +11:00
}
}
pub fn draw_quad(&self, cmd: vk::CommandBuffer, vbo: QuadType) {
let offset = match vbo {
QuadType::Offscreen => 0,
QuadType::Final => 4,
};
unsafe {
self.device.cmd_draw(cmd, 4, 1, offset, 0);
}
}
2023-02-06 08:17:23 +11:00
}