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

91 lines
2.2 KiB
Rust
Raw Normal View History

2022-12-25 17:18:11 +11:00
use crate::error;
use crate::memory::VulkanBuffer;
2024-02-13 17:26:40 +11:00
use array_concat::concat_arrays;
2023-02-06 08:17:23 +11:00
use ash::vk;
use gpu_allocator::vulkan::Allocator;
2024-02-22 16:41:29 +11:00
use librashader_runtime::quad::{QuadType, VertexInput};
use parking_lot::Mutex;
2023-02-06 08:17:23 +11:00
use std::sync::Arc;
2022-12-25 17:18:11 +11:00
2024-02-22 16:41:29 +11:00
const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
VertexInput {
position: [-1.0, -1.0, 0.0, 1.0],
texcoord: [0.0, 0.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [-1.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [1.0, -1.0, 0.0, 1.0],
texcoord: [1.0, 0.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0],
},
];
2024-02-22 16:41:29 +11:00
const FINAL_VBO_DATA: [VertexInput; 4] = [
VertexInput {
position: [0.0, 0.0, 0.0, 1.0],
texcoord: [0.0, 0.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [0.0, 1.0, 0.0, 1.0],
texcoord: [0.0, 1.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [1.0, 0.0, 0.0, 1.0],
texcoord: [1.0, 0.0],
},
2024-02-22 16:41:29 +11:00
VertexInput {
position: [1.0, 1.0, 0.0, 1.0],
texcoord: [1.0, 1.0],
},
2022-12-11 17:06:28 +11:00
];
2022-12-25 17:18:11 +11:00
2024-02-22 16:41:29 +11:00
static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
2022-12-25 17:18:11 +11:00
pub struct DrawQuad {
buffer: VulkanBuffer,
}
impl DrawQuad {
2023-02-06 08:17:23 +11:00
pub fn new(
device: &Arc<ash::Device>,
allocator: &Arc<Mutex<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,
2024-02-22 16:41:29 +11:00
std::mem::size_of::<[VertexInput; 8]>(),
2023-02-06 08:17:23 +11:00
)?;
2022-12-25 17:18:11 +11:00
{
let slice = buffer.as_mut_slice()?;
2024-02-13 17:26:40 +11:00
slice.copy_from_slice(bytemuck::cast_slice(VBO_DATA));
2022-12-25 17:18:11 +11:00
}
2024-02-26 16:21:51 +11:00
Ok(DrawQuad { buffer })
2022-12-25 17:18:11 +11:00
}
2024-02-26 16:21:51 +11:00
pub fn bind_vbo_for_frame(&self, device: &ash::Device, cmd: vk::CommandBuffer) {
2022-12-25 17:18:11 +11:00
unsafe {
2024-02-26 16:21:51 +11:00
device.cmd_bind_vertex_buffers(cmd, 0, &[self.buffer.handle], &[0 as vk::DeviceSize])
2022-12-25 17:18:11 +11:00
}
}
2024-02-26 16:21:51 +11:00
pub fn draw_quad(&self, device: &ash::Device, cmd: vk::CommandBuffer, vbo: QuadType) {
let offset = match vbo {
QuadType::Offscreen => 0,
QuadType::Final => 4,
};
unsafe {
2024-02-26 16:21:51 +11:00
device.cmd_draw(cmd, 4, 1, offset, 0);
}
}
2023-02-06 08:17:23 +11:00
}