Houstin, we have rendering.
This commit is contained in:
120
src/render.rs
120
src/render.rs
@@ -3,19 +3,19 @@ use std::{iter, num::NonZeroU32, ops::Range, rc::Rc};
|
||||
|
||||
use bytemuck::__core::mem;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use cgmath::Point3;
|
||||
use futures::executor::LocalPool;
|
||||
use legion::world::SubWorld;
|
||||
use legion::*;
|
||||
use wgpu::util::DeviceExt;
|
||||
use wgpu::{Buffer, Device, SwapChain, Queue, SwapChainFrame, Surface, SwapChainDescriptor, Instance};
|
||||
use winit::dpi::{PhysicalSize};
|
||||
use wgpu::{Buffer, Device, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, BindGroup, BindGroupLayout};
|
||||
use winit::dpi::PhysicalSize;
|
||||
use winit::platform::unix::x11::ffi::Time;
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::geometry::{create_plane, import_mesh, Vertex};
|
||||
use crate::light::LightRaw;
|
||||
use crate::{Velocity, OPENGL_TO_WGPU_MATRIX, Color, Mesh, Position};
|
||||
use futures::executor::LocalPool;
|
||||
use legion::world::SubWorld;
|
||||
use cgmath::Point3;
|
||||
use crate::{Color, Mesh, Position, Velocity, OPENGL_TO_WGPU_MATRIX};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -65,11 +65,10 @@ pub struct Renderer {
|
||||
shadow_pass: Pass,
|
||||
forward_pass: Pass,
|
||||
forward_depth: wgpu::TextureView,
|
||||
entity_bind_group_layout: BindGroupLayout,
|
||||
|
||||
light_uniform_buf: wgpu::Buffer,
|
||||
// plane_uniform_buf: wgpu::Buffer,
|
||||
// plane_vertex_buf: wgpu::Buffer,
|
||||
// plane_index_buf: wgpu::Buffer,
|
||||
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
@@ -94,20 +93,33 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
//#[system(for_each)]
|
||||
/*
|
||||
SOOOOOOOOOOOOooo... Legion systems have to be standalone functions, which is fine
|
||||
we can do a special kind of song and dance
|
||||
|
||||
Main loop {
|
||||
|
||||
renderer
|
||||
runtime
|
||||
|
||||
render_system(param1,2,3, renderer);
|
||||
animation_system(param1,2,3, runtime);
|
||||
|
||||
renderer.finalize()
|
||||
|
||||
}
|
||||
*/
|
||||
#[system]
|
||||
#[write_component(Position)]
|
||||
#[write_component(Mesh)]
|
||||
#[write_component(Color)]
|
||||
pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
||||
|
||||
let frame = renderer.get_current_frame();
|
||||
|
||||
let mut query = <(&mut Position, &mut Mesh, &mut Color)>::query();
|
||||
|
||||
// update uniforms
|
||||
// Update the entity uniforms
|
||||
for (pos, mesh, color) in query.iter_mut(world) {
|
||||
|
||||
// Revolve the entity by the rotation speed, only if it is non-zero
|
||||
// if vel.rs != 0.0 {
|
||||
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(vel.rs));
|
||||
@@ -123,7 +135,9 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
||||
color.a as f32,
|
||||
],
|
||||
};
|
||||
renderer.queue.write_buffer(&mesh.uniform_buf, 0, bytemuck::bytes_of(&data));
|
||||
renderer
|
||||
.queue
|
||||
.write_buffer(&mesh.uniform_buffer, 0, bytemuck::bytes_of(&data));
|
||||
}
|
||||
|
||||
// if self.lights_are_dirty {
|
||||
@@ -196,7 +210,12 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
||||
attachment: &frame.output.view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, b: 0.3, a: 1.0 }),
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
},
|
||||
}],
|
||||
@@ -212,17 +231,30 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
||||
pass.set_pipeline(&renderer.forward_pass.pipeline);
|
||||
pass.set_bind_group(0, &renderer.forward_pass.bind_group, &[]);
|
||||
|
||||
// for entity in &self.entities {
|
||||
// pass.set_bind_group(1, &entity.bind_group, &[]);
|
||||
// pass.set_index_buffer(entity.index_buf.slice(..));
|
||||
// pass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
||||
// pass.draw_indexed(0..entity.index_count as u32, 0, 0..1);
|
||||
// }
|
||||
let mut query = <(&mut Position, &mut Mesh, &mut Color)>::query();
|
||||
|
||||
for (pos, mesh, color) in query.iter_mut(world) {
|
||||
pass.set_bind_group(1, &mesh.bind_group, &[]);
|
||||
pass.set_index_buffer(mesh.index_buffer.slice(..));
|
||||
pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
|
||||
pass.draw_indexed(0..mesh.index_count as u32, 0, 0..1);
|
||||
let data = EntityUniforms {
|
||||
model: pos.mx.into(),
|
||||
color: [
|
||||
color.r as f32,
|
||||
color.g as f32,
|
||||
color.b as f32,
|
||||
color.a as f32,
|
||||
],
|
||||
};
|
||||
renderer
|
||||
.queue
|
||||
.write_buffer(&mesh.uniform_buffer, 0, bytemuck::bytes_of(&data));
|
||||
}
|
||||
}
|
||||
encoder.pop_debug_group();
|
||||
|
||||
renderer.queue.submit(iter::once(encoder.finish()));
|
||||
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
@@ -232,7 +264,9 @@ impl Renderer {
|
||||
match self.swapchain.get_current_frame() {
|
||||
Ok(frame) => frame,
|
||||
Err(_) => {
|
||||
self.swapchain = self.device.create_swap_chain(&self.surface, &self.swapchain_description);
|
||||
self.swapchain = self
|
||||
.device
|
||||
.create_swap_chain(&self.surface, &self.swapchain_description);
|
||||
self.swapchain
|
||||
.get_current_frame()
|
||||
.expect("Failed to acquire next swap chain texture!")
|
||||
@@ -284,8 +318,9 @@ impl Renderer {
|
||||
(vertex_buf, index_buf)
|
||||
}
|
||||
|
||||
pub fn load_mesh_to_buffer(&self, filepath: &str) -> (Arc<Buffer>, Arc<Buffer>, Arc<Buffer>) {
|
||||
pub fn load_mesh_to_buffer(&self, filepath: &str) -> Mesh {
|
||||
let (vertices, indices) = import_mesh(filepath);
|
||||
let index_count = indices.len();
|
||||
let (vertex_buf, index_buf) = Renderer::create_buffer(&self.device, indices, vertices);
|
||||
|
||||
let uniform_buf = Arc::new(self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
@@ -294,7 +329,23 @@ impl Renderer {
|
||||
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
}));
|
||||
(vertex_buf, index_buf, uniform_buf)
|
||||
|
||||
let bind_group = Arc::new(self.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.entity_bind_group_layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)),
|
||||
}],
|
||||
label: None,
|
||||
}));
|
||||
|
||||
Mesh {
|
||||
index_buffer: index_buf,
|
||||
index_count: index_count,
|
||||
vertex_buffer: vertex_buf,
|
||||
uniform_buffer: uniform_buf,
|
||||
bind_group: bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(window: Window) -> Renderer {
|
||||
@@ -345,8 +396,6 @@ impl Renderer {
|
||||
let queue = Arc::new(queue);
|
||||
let device = Arc::new(device);
|
||||
|
||||
|
||||
|
||||
// This is some gross-ass web shit
|
||||
/*#[cfg(target_arch = "wasm32")]
|
||||
let spawner = {
|
||||
@@ -429,7 +478,7 @@ impl Renderer {
|
||||
};
|
||||
|
||||
// This is also in the runtime which really shouldn't have this
|
||||
let local_bind_group_layout =
|
||||
let entity_bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
@@ -445,6 +494,7 @@ impl Renderer {
|
||||
}],
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
There appear to be two passes required for shadows, the shadow pass, and the forward pass
|
||||
Need to open this up in renderdoc and see what it's actually doing
|
||||
@@ -471,7 +521,7 @@ impl Renderer {
|
||||
// Pipeline is similar between passes, but with a different label
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("shadow"),
|
||||
bind_group_layouts: &[&bind_group_layout, &local_bind_group_layout],
|
||||
bind_group_layouts: &[&bind_group_layout, &entity_bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
@@ -591,7 +641,7 @@ impl Renderer {
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("main"),
|
||||
bind_group_layouts: &[&bind_group_layout, &local_bind_group_layout],
|
||||
bind_group_layouts: &[&bind_group_layout, &entity_bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
@@ -745,18 +795,15 @@ impl Renderer {
|
||||
shadow_pass,
|
||||
forward_pass,
|
||||
forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()),
|
||||
entity_bind_group_layout: entity_bind_group_layout,
|
||||
light_uniform_buf,
|
||||
// plane_uniform_buf,
|
||||
// plane_vertex_buf: (),
|
||||
// plane_index_buf: ()
|
||||
|
||||
swapchain_description: sc_desc,
|
||||
surface,
|
||||
instance: Arc::new(instance)
|
||||
instance: Arc::new(instance),
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn render(
|
||||
&mut self,
|
||||
frame: &wgpu::SwapChainTexture,
|
||||
@@ -885,7 +932,6 @@ impl Renderer {
|
||||
encoder.pop_debug_group();
|
||||
|
||||
queue.submit(iter::once(encoder.finish()));
|
||||
|
||||
}
|
||||
|
||||
pub(crate) fn required_features() -> wgpu::Features {
|
||||
|
||||
Reference in New Issue
Block a user