moved buffer stuff to a separate module
This commit is contained in:
+14
-131
@@ -1,19 +1,8 @@
|
||||
use winit::{window::Window, dpi::PhysicalSize};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
mod vertex; use vertex::Vertex;
|
||||
mod texture; use texture::Texture;
|
||||
mod buffers; use buffers::{NodeBuffer, UniformBuffer, RasterBuffer, Vertex};
|
||||
pub mod camera; use camera::Camera;
|
||||
mod uniforms; use uniforms::Uniforms;
|
||||
|
||||
|
||||
const VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [-1.0, 1.0, 0.0], uv: [-1.0, 1.0] }, // top left
|
||||
Vertex { position: [ 1.0, 1.0, 0.0], uv: [1.0, 1.0] }, // top right
|
||||
Vertex { position: [ 1.0,-1.0, 0.0], uv: [1.0, -1.0] }, // bottom right
|
||||
Vertex { position: [-1.0,-1.0, 0.0], uv: [-1.0, -1.0] }, // bottom left
|
||||
];
|
||||
const INDICES: &[u16] = &[ 0, 1, 3, 3, 1, 2 ];
|
||||
|
||||
|
||||
pub struct Renderer {
|
||||
@@ -24,16 +13,10 @@ pub struct Renderer {
|
||||
swapchain: wgpu::SwapChain,
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
// Buffers
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
index_buffer: wgpu::Buffer,
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
// Uniform bind group
|
||||
uniform_bind_group: wgpu::BindGroup,
|
||||
// Texture
|
||||
bind_group: wgpu::BindGroup,
|
||||
raster_buffer: RasterBuffer,
|
||||
uniform_buffer: UniformBuffer,
|
||||
// Camera
|
||||
pub camera: Camera,
|
||||
uniforms: Uniforms
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
@@ -96,101 +79,11 @@ impl Renderer {
|
||||
multisample: multisample_state,
|
||||
})
|
||||
}
|
||||
|
||||
fn create_bind_group(device: &wgpu::Device, tex: &Texture) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
|
||||
let bind_group_layout = device.create_bind_group_layout(
|
||||
&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
multisampled: false,
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: false },
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler {
|
||||
comparison: false,
|
||||
filtering: true,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
label: None,
|
||||
}
|
||||
);
|
||||
|
||||
let bind_group = device.create_bind_group(
|
||||
&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&tex.view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&tex.sampler),
|
||||
}
|
||||
],
|
||||
label: None,
|
||||
}
|
||||
);
|
||||
|
||||
(bind_group_layout, bind_group)
|
||||
}
|
||||
|
||||
fn create_uniform_bind_group(device: &wgpu::Device, buf: &wgpu::Buffer) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
|
||||
let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
}
|
||||
],
|
||||
label: Some("uniform_bind_group_layout"),
|
||||
});
|
||||
|
||||
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &uniform_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: buf.as_entire_binding(),
|
||||
}
|
||||
],
|
||||
label: Some("uniform_bind_group"),
|
||||
});
|
||||
|
||||
(uniform_bind_group_layout, uniform_bind_group)
|
||||
}
|
||||
|
||||
fn create_buffer(device: &wgpu::Device, usage: wgpu::BufferUsage, content: &[u8]) -> wgpu::Buffer {
|
||||
device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: content,
|
||||
usage: usage,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
|
||||
// Create surface and pick device
|
||||
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
||||
let instance = wgpu::Instance::new(wgpu::BackendBit::DX11);
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
|
||||
// Pick adapter (physical gpu)
|
||||
@@ -208,26 +101,17 @@ impl Renderer {
|
||||
// Cam
|
||||
let aspect = swapchain_desc.width as f32 / swapchain_desc.height as f32;
|
||||
let camera = Camera::new(aspect, 60.0);
|
||||
let mut uniforms = Uniforms::new();
|
||||
uniforms.update(&camera);
|
||||
|
||||
// Buffers
|
||||
let vertex_buffer = Self::create_buffer(&device, wgpu::BufferUsage::VERTEX, bytemuck::cast_slice(VERTICES));
|
||||
let index_buffer = Self::create_buffer(&device, wgpu::BufferUsage::INDEX, bytemuck::cast_slice(INDICES));
|
||||
let uniform_buffer = Self::create_buffer(&device, wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, bytemuck::cast_slice(&[uniforms]));
|
||||
|
||||
// Load image
|
||||
let tex = Texture::from_file(&device, &queue, "./assets/rick.png");
|
||||
let (bind_group_layout, bind_group) = Self::create_bind_group(&device, &tex);
|
||||
let (uniform_bind_group_layout, uniform_bind_group) = Self::create_uniform_bind_group(&device, &uniform_buffer);
|
||||
let uniform_buffer = UniformBuffer::new(&device);
|
||||
let raster_buffer = RasterBuffer::new(&device);
|
||||
|
||||
// Pipeline
|
||||
let bind_groups = [&bind_group_layout, &uniform_bind_group_layout];
|
||||
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &bind_groups);
|
||||
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout]);
|
||||
|
||||
// Save values in app
|
||||
println!("Initialized");
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer, uniform_buffer, bind_group, uniform_bind_group, camera, uniforms }
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, raster_buffer, uniform_buffer, camera }
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
|
||||
@@ -247,8 +131,8 @@ impl Renderer {
|
||||
pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> {
|
||||
|
||||
// Update uniform buffer
|
||||
self.uniforms.update(&self.camera);
|
||||
self.queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[self.uniforms]));
|
||||
self.uniform_buffer.values.update(&self.camera);
|
||||
self.queue.write_buffer(&self.uniform_buffer.buffer, 0, bytemuck::cast_slice(&[self.uniform_buffer.values]));
|
||||
|
||||
// Get next frame to render to
|
||||
let frame = self.swapchain.get_current_frame()?.output;
|
||||
@@ -273,12 +157,11 @@ impl Renderer {
|
||||
|
||||
// Data
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
|
||||
render_pass.set_bind_group(0, &self.uniform_buffer.bind_group, &[]);
|
||||
// Vertices and indices
|
||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||
render_pass.draw_indexed(0..(INDICES.len() as u32), 0, 0..1);
|
||||
render_pass.set_vertex_buffer(0, self.raster_buffer.vertex_buffer.slice(..));
|
||||
render_pass.set_index_buffer(self.raster_buffer.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||
render_pass.draw_indexed(0..self.raster_buffer.index_len, 0, 0..1);
|
||||
}
|
||||
|
||||
// Submit encoder (command buffer)
|
||||
|
||||
Reference in New Issue
Block a user