2023-12-16 22:28:41 +11:00
|
|
|
use array_concat::concat_arrays;
|
2024-02-12 08:46:43 +11:00
|
|
|
use bytemuck::{Pod, Zeroable};
|
2023-12-16 22:28:41 +11:00
|
|
|
use librashader_runtime::quad::QuadType;
|
|
|
|
use wgpu::util::{BufferInitDescriptor, DeviceExt};
|
2024-02-06 17:45:31 +11:00
|
|
|
use wgpu::{Buffer, Device, RenderPass};
|
2023-12-16 22:28:41 +11:00
|
|
|
|
2024-02-12 08:46:43 +11:00
|
|
|
// As per https://www.w3.org/TR/webgpu/#vertex-processing,
|
|
|
|
// WGPU does vertex expansion
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug, Copy, Clone, Default, Zeroable, Pod)]
|
|
|
|
struct WgpuVertex {
|
|
|
|
position: [f32; 2],
|
|
|
|
texcoord: [f32; 2],
|
|
|
|
}
|
|
|
|
|
|
|
|
const OFFSCREEN_VBO_DATA: [WgpuVertex; 4] = [
|
|
|
|
WgpuVertex {
|
|
|
|
position: [-1.0, -1.0],
|
|
|
|
texcoord: [0.0, 0.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [-1.0, 1.0],
|
|
|
|
texcoord: [0.0, 1.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [1.0, -1.0],
|
|
|
|
texcoord: [1.0, 0.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [1.0, 1.0],
|
|
|
|
texcoord: [1.0, 1.0],
|
|
|
|
},
|
2023-12-16 22:28:41 +11:00
|
|
|
];
|
|
|
|
|
2024-02-12 08:46:43 +11:00
|
|
|
const FINAL_VBO_DATA: [WgpuVertex; 4] = [
|
|
|
|
WgpuVertex {
|
|
|
|
position: [0.0, 0.0],
|
|
|
|
texcoord: [0.0, 0.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [0.0, 1.0],
|
|
|
|
texcoord: [0.0, 1.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [1.0, 0.0],
|
|
|
|
texcoord: [1.0, 0.0],
|
|
|
|
},
|
|
|
|
WgpuVertex {
|
|
|
|
position: [1.0, 1.0],
|
|
|
|
texcoord: [1.0, 1.0],
|
|
|
|
},
|
2023-12-16 22:28:41 +11:00
|
|
|
];
|
|
|
|
|
2024-02-12 08:46:43 +11:00
|
|
|
static VBO_DATA: &[WgpuVertex; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
|
2024-01-18 14:35:52 +11:00
|
|
|
|
2023-12-16 22:28:41 +11:00
|
|
|
pub struct DrawQuad {
|
|
|
|
buffer: Buffer,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawQuad {
|
2024-02-06 16:29:45 +11:00
|
|
|
pub fn new(device: &Device) -> DrawQuad {
|
2023-12-16 22:28:41 +11:00
|
|
|
let buffer = device.create_buffer_init(&BufferInitDescriptor {
|
|
|
|
label: Some("librashader vbo"),
|
2024-02-12 08:46:43 +11:00
|
|
|
contents: bytemuck::cast_slice(VBO_DATA),
|
2023-12-16 22:28:41 +11:00
|
|
|
usage: wgpu::BufferUsages::VERTEX,
|
|
|
|
});
|
|
|
|
|
|
|
|
DrawQuad { buffer }
|
|
|
|
}
|
|
|
|
|
2024-02-06 16:29:45 +11:00
|
|
|
pub fn draw_quad<'a, 'b: 'a>(&'b self, cmd: &mut RenderPass<'a>, vbo: QuadType) {
|
2023-12-16 22:28:41 +11:00
|
|
|
cmd.set_vertex_buffer(0, self.buffer.slice(0..));
|
|
|
|
|
|
|
|
let offset = match vbo {
|
2024-02-06 16:29:45 +11:00
|
|
|
QuadType::Offscreen => 0..4,
|
|
|
|
QuadType::Final => 4..8,
|
2023-12-16 22:28:41 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
cmd.draw(offset, 0..1)
|
|
|
|
}
|
|
|
|
}
|