Implement vertex buffers
This commit is contained in:
parent
9b570a68e3
commit
b7200a4cc6
|
@ -1,17 +1,22 @@
|
|||
// Vertex shader
|
||||
|
||||
struct VertexInput {
|
||||
@location(0) position: vec3<f32>,
|
||||
@location(1) color: vec3<f32>,
|
||||
};
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) color: vec3<f32>,
|
||||
};
|
||||
|
||||
@vertex
|
||||
fn vs_main(
|
||||
@builtin(vertex_index) in_vertex_index: u32,
|
||||
model: VertexInput,
|
||||
) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
||||
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
||||
out.color = model.color;
|
||||
out.clip_position = vec4<f32>(model.position, 1.0);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -19,5 +24,5 @@ fn vs_main(
|
|||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
|
||||
return vec4<f32>(in.color, 1.0);
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod model_2d;
|
|
@ -0,0 +1,4 @@
|
|||
#[derive(Component, Debug, Default)]
|
||||
pub struct Model2d {
|
||||
pub vertices: Vec<Vertex>,
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod components;
|
|
@ -13,7 +13,7 @@ use tracing_subscriber::{
|
|||
|
||||
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode}, event_loop::{EventLoop, ControlFlow}};
|
||||
|
||||
use crate::{renderer::{Renderer, BasicRenderer}, input_event::InputEvent};
|
||||
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent};
|
||||
|
||||
struct GameLoop<'a, 'b> {
|
||||
window: Arc<Window>,
|
||||
|
@ -125,7 +125,7 @@ impl<'a, 'b> GameLoop<'a, 'b> {
|
|||
match self.renderer.as_mut().render().await {
|
||||
Ok(_) => {}
|
||||
// Reconfigure the surface if lost
|
||||
Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().size()).await,
|
||||
Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().surface_size()).await,
|
||||
// The system is out of memory, we should probably quit
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
mod system;
|
||||
mod game;
|
||||
mod renderer;
|
||||
mod render;
|
||||
mod input_event;
|
||||
mod resources;
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
pub trait DescVertexBufferLayout {
|
||||
fn desc<'a>() -> wgpu::VertexBufferLayout<'a>;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
pub mod renderer;
|
||||
pub mod render_pipeline;
|
||||
pub mod vertex;
|
||||
pub mod desc_buf_lay;
|
||||
pub mod render_buffer;
|
|
@ -0,0 +1,42 @@
|
|||
use super::{desc_buf_lay::DescVertexBufferLayout, vertex::Vertex};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RenderBuffer {
|
||||
VertexBuffer(BufferStorage),
|
||||
}
|
||||
|
||||
impl RenderBuffer {
|
||||
pub fn desc<'a>(&self) -> wgpu::VertexBufferLayout<'a> {
|
||||
match self {
|
||||
RenderBuffer::VertexBuffer(b) => Vertex::desc(),
|
||||
_ => panic!("Cannot create a VertexBufferLayout for {:?}!", *self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BufferStorage {
|
||||
buffer: wgpu::Buffer,
|
||||
slot: u32,
|
||||
}
|
||||
|
||||
impl BufferStorage {
|
||||
pub fn new(buffer: wgpu::Buffer, slot: u32) -> Self {
|
||||
Self {
|
||||
buffer,
|
||||
slot,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buffer(&self) -> &wgpu::Buffer {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
pub fn buffer_mut(&mut self) -> &mut wgpu::Buffer {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
pub fn slot(&self) -> u32 {
|
||||
self.slot
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
use std::ops::Range;
|
||||
|
||||
use wgpu::{TextureFormat, PipelineLayout, RenderPipeline, VertexBufferLayout, RenderPass};
|
||||
|
||||
use super::render_buffer::RenderBuffer;
|
||||
|
||||
pub struct FullRenderPipeline {
|
||||
layout: PipelineLayout,
|
||||
wgpu_pipeline: RenderPipeline,
|
||||
buffers: Vec<RenderBuffer>,
|
||||
}
|
||||
|
||||
impl FullRenderPipeline {
|
||||
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, shader: &wgpu::ShaderModule, buffers: Vec<RenderBuffer>) -> Self {
|
||||
let buffer_layouts: Vec<VertexBufferLayout> = buffers.iter().map(|b| match b {
|
||||
RenderBuffer::VertexBuffer(_) => b.desc()
|
||||
}).collect();
|
||||
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &buffer_layouts,
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
layout: render_pipeline_layout,
|
||||
wgpu_pipeline: render_pipeline,
|
||||
buffers,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_layout(&self) -> &PipelineLayout {
|
||||
&self.layout
|
||||
}
|
||||
|
||||
pub fn get_wgpu_pipeline(&self) -> &RenderPipeline {
|
||||
&self.wgpu_pipeline
|
||||
}
|
||||
|
||||
pub fn render<'a>(&'a self, pass: &mut RenderPass<'a>, vertices: Range<u32>, instances: Range<u32>) {
|
||||
pass.set_pipeline(&self.wgpu_pipeline);
|
||||
|
||||
for buffer in self.buffers.iter() {
|
||||
match buffer {
|
||||
RenderBuffer::VertexBuffer(b) => {
|
||||
pass.set_vertex_buffer(b.slot(), b.buffer().slice(..));
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pass.draw(vertices, instances);
|
||||
}
|
||||
}
|
|
@ -3,20 +3,22 @@ use std::borrow::Cow;
|
|||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use wgpu::util::DeviceExt;
|
||||
//use winit::{window::Window, event::WindowEvent};
|
||||
//use winit::window::Window;
|
||||
use winit::{window::Window, event::WindowEvent};
|
||||
|
||||
use crate::resources;
|
||||
|
||||
use super::{render_pipeline::FullRenderPipeline, vertex::{Vertex, VERTICES}, desc_buf_lay::DescVertexBufferLayout, render_buffer::{BufferStorage, RenderBuffer}};
|
||||
|
||||
#[async_trait]
|
||||
pub trait Renderer {
|
||||
//fn new(surface: wgpu::Surface, config: wgpu::SurfaceConfiguration, device: wgpu::Device)
|
||||
//async fn create_with_window(window: Window) -> Self;
|
||||
async fn render(&mut self) -> Result<(), wgpu::SurfaceError>;
|
||||
async fn on_resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>);
|
||||
|
||||
fn size(&self) -> winit::dpi::PhysicalSize<u32>;
|
||||
fn surface_size(&self) -> winit::dpi::PhysicalSize<u32>;
|
||||
fn add_render_pipeline(&mut self, pipeline: FullRenderPipeline);
|
||||
}
|
||||
|
||||
pub struct BasicRenderer {
|
||||
|
@ -29,7 +31,7 @@ pub struct BasicRenderer {
|
|||
|
||||
pub clear_color: wgpu::Color,
|
||||
|
||||
pub render_pipeline: wgpu::RenderPipeline,
|
||||
pub render_pipelines: Vec<FullRenderPipeline>,
|
||||
}
|
||||
|
||||
impl BasicRenderer {
|
||||
|
@ -87,58 +89,26 @@ impl BasicRenderer {
|
|||
};
|
||||
surface.configure(&device, &config);
|
||||
|
||||
|
||||
|
||||
let shader_src = resources::load_string("shader.wgsl").await.expect("Failed to load shader!");
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(&shader_src)),
|
||||
});
|
||||
|
||||
// Create a render pipeline. At some point this will be created by something else and given to the renderer
|
||||
let vertex_buffer = device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(super::vertex::VERTICES),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
}
|
||||
);
|
||||
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
let pipelines = vec![
|
||||
FullRenderPipeline::new(&device, &config, &shader,
|
||||
vec![RenderBuffer::VertexBuffer(BufferStorage::new(vertex_buffer, 0))])
|
||||
];
|
||||
|
||||
Self {
|
||||
window,
|
||||
|
@ -153,7 +123,7 @@ impl BasicRenderer {
|
|||
b: 0.3,
|
||||
a: 1.0,
|
||||
},
|
||||
render_pipeline
|
||||
render_pipelines: pipelines,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -169,7 +139,8 @@ impl Renderer for BasicRenderer {
|
|||
label: Some("Basic Renderer's Encoder")
|
||||
});
|
||||
|
||||
{
|
||||
// Loop through all the render pipelines and draw the buffers.
|
||||
for pipeline in self.render_pipelines.iter() {
|
||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Basic Renderer's Render Pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
|
@ -183,8 +154,7 @@ impl Renderer for BasicRenderer {
|
|||
depth_stencil_attachment: None,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
render_pass.draw(0..3, 0..1);
|
||||
pipeline.render(&mut render_pass, 0..VERTICES.len() as u32, 0..1);
|
||||
}
|
||||
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
|
@ -202,7 +172,11 @@ impl Renderer for BasicRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
fn size(&self) -> winit::dpi::PhysicalSize<u32> {
|
||||
fn surface_size(&self) -> winit::dpi::PhysicalSize<u32> {
|
||||
self.size
|
||||
}
|
||||
|
||||
fn add_render_pipeline(&mut self, pipeline: FullRenderPipeline) {
|
||||
self.render_pipelines.push(pipeline);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
use super::desc_buf_lay::DescVertexBufferLayout;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
pub position: [f32; 3],
|
||||
pub color: [f32; 3],
|
||||
}
|
||||
|
||||
pub const VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [-0.5, -0.5, 0.0], color: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] },
|
||||
];
|
||||
|
||||
impl DescVertexBufferLayout for Vertex {
|
||||
fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &[
|
||||
wgpu::VertexAttribute {
|
||||
offset: 0,
|
||||
shader_location: 0,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue