diff --git a/kubi/src/rendering.rs b/kubi/src/rendering.rs index 33a052a..843e93b 100644 --- a/kubi/src/rendering.rs +++ b/kubi/src/rendering.rs @@ -14,12 +14,20 @@ pub mod sumberge; pub struct BufferPair { pub index: wgpu::Buffer, + pub index_len: u32, pub vertex: wgpu::Buffer, + pub vertex_len: u32, } #[derive(Unique)] pub struct BackgroundColor(pub Vec3); +pub struct RenderCtx<'a> { + pub renderer: &'a Renderer, + pub encoder: &'a mut wgpu::CommandEncoder, + pub surface_view: &'a wgpu::TextureView, +} + pub fn render_master(storages: AllStoragesViewMut) { let renderer = storages.borrow::>().unwrap(); @@ -29,11 +37,10 @@ pub fn render_master(storages: AllStoragesViewMut) { let surface_texture = renderer.surface().get_current_texture().unwrap(); let surface_view = surface_texture.texture.create_view(&wgpu::TextureViewDescriptor::default()); - //Main in-game render pass - if storages.run(is_ingame) { + { let bg = storages.borrow::>().unwrap().0; - let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("main0_pass"), + let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("rpass_background"), color_attachments: &[Some(wgpu::RenderPassColorAttachment { view: &surface_view, resolve_target: None, @@ -50,10 +57,16 @@ pub fn render_master(storages: AllStoragesViewMut) { depth_stencil_attachment: None, ..Default::default() }); + } - let data = (&mut render_pass, &*renderer); + let mut data = RenderCtx { + renderer: &renderer, + encoder: &mut encoder, + surface_view: &surface_view, + }; - storages.run_with_data(world::draw_world, data); + if storages.run(is_ingame) { + storages.run_with_data(world::draw_world, &mut data); } renderer.queue().submit(std::iter::once(encoder.finish())); diff --git a/kubi/src/rendering/world.rs b/kubi/src/rendering/world.rs index 96fd83c..17d20ec 100644 --- a/kubi/src/rendering/world.rs +++ b/kubi/src/rendering/world.rs @@ -9,7 +9,7 @@ use crate::{ settings::GameSettings, world::{ChunkMeshStorage, ChunkStorage}, }; -use super::Renderer; +use super::{RenderCtx, Renderer}; #[derive(Clone, Copy, Pod, Zeroable)] #[repr(C, packed)] @@ -41,7 +41,7 @@ pub fn init_trans_chunk_queue(storages: AllStoragesView) { } pub fn draw_world( - (render_pass, renderer): (&mut wgpu::RenderPass, &Renderer), + ctx: &mut RenderCtx, textures: UniqueView, chunks: UniqueView, meshes: NonSendSync>, @@ -53,6 +53,19 @@ pub fn draw_world( let camera = camera.iter().next().expect("No cameras in the scene"); let camera_matrix = camera.view_matrix * camera.perspective_matrix; + let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("draw_world"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: ctx.surface_view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })], + ..Default::default() + }); + for (&position, chunk) in &chunks.chunks { if let Some(key) = chunk.mesh_index { let mesh = meshes.get(key).expect("Mesh index pointing to nothing"); @@ -73,9 +86,10 @@ pub fn draw_world( //Draw chunk mesh if mesh.main.index.size() > 0 { //TODO - // render_pass.set_index_buffer(mesh.main.index.slice(..), wgpu::IndexFormat::Uint32); - // render_pass.set_vertex_buffer(0, mesh.main.vertex.slice(..)); - // render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]); + render_pass.set_index_buffer(mesh.main.index.slice(..), wgpu::IndexFormat::Uint32); + render_pass.set_vertex_buffer(0, mesh.main.vertex.slice(..)); + render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]); + render_pass.draw_indexed(0..mesh.main.index_len, 0, 0..1); } //TODO trans chunks diff --git a/kubi/src/world/loading.rs b/kubi/src/world/loading.rs index 48be8d1..6037473 100644 --- a/kubi/src/world/loading.rs +++ b/kubi/src/world/loading.rs @@ -355,12 +355,16 @@ fn process_completed_tasks( let main_buffer_pair = BufferPair { vertex: vtx_buffer, + vertex_len: vertices.len() as u32, index: idx_buffer, + index_len: indices.len() as u32, }; let trans_buffer_pair = BufferPair { vertex: vtx_buffer_trans, + vertex_len: vertices.len() as u32, index: idx_buffer_trans, + index_len: indices.len() as u32, }; let mesh = ChunkMesh {