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

72 lines
1.8 KiB
Rust
Raw Normal View History

use array_concat::concat_arrays;
2024-02-22 16:41:29 +11:00
use librashader_runtime::quad::{QuadType, VertexInput};
use wgpu::util::{BufferInitDescriptor, DeviceExt};
2024-02-06 17:45:31 +11:00
use wgpu::{Buffer, Device, RenderPass};
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],
},
];
2024-02-22 16:41:29 +11:00
static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_VBO_DATA);
2024-01-18 14:35:52 +11:00
pub struct DrawQuad {
buffer: Buffer,
}
impl DrawQuad {
pub fn new(device: &Device) -> DrawQuad {
let buffer = device.create_buffer_init(&BufferInitDescriptor {
label: Some("librashader vbo"),
contents: bytemuck::cast_slice(VBO_DATA),
usage: wgpu::BufferUsages::VERTEX,
});
DrawQuad { buffer }
}
pub fn draw_quad<'a, 'b: 'a>(&'b self, cmd: &mut RenderPass<'a>, vbo: QuadType) {
cmd.set_vertex_buffer(0, self.buffer.slice(0..));
let offset = match vbo {
QuadType::Offscreen => 0..4,
QuadType::Final => 4..8,
};
cmd.draw(offset, 0..1)
}
}