diff --git a/kubi/src/rendering.rs b/kubi/src/rendering.rs index 5593aae..e194906 100644 --- a/kubi/src/rendering.rs +++ b/kubi/src/rendering.rs @@ -4,6 +4,7 @@ use glam::Vec3; use crate::{events::WindowResizedEvent, state::is_ingame}; mod renderer; +mod primitives; pub use renderer::Renderer; pub mod background; @@ -27,11 +28,14 @@ pub struct RenderCtx<'a> { pub surface_view: &'a wgpu::TextureView, } +//TODO run init_world_render_state only once ingame? + pub fn init_rendering() -> Workload { ( depth::init_depth_texture, camera::init_camera_uniform_buffer, - world::init_world_render_state, //TODO run only once ingame + world::init_world_render_state, //requires depth and camera buffers + primitives::init_primitives, ).into_sequential_workload() } @@ -82,30 +86,6 @@ pub fn resize_renderer( } } -//Deprecated WindowSize thingy - -// #[derive(Unique, Clone, Copy)] -// #[repr(transparent)] -// #[deprecated = "use Renderer.size instead"] -// #[allow(deprecated)] -// pub struct WindowSize(pub UVec2); - -// pub fn init_window_size(storages: AllStoragesView) { -// let size = storages.borrow::>().unwrap().iter().next().unwrap().0; -// storages.add_unique(WindowSize(size)) -// } - -// pub fn update_window_size( -// mut win_size: UniqueViewMut, -// resize: View, -// ) { -// if let Some(resize) = resize.iter().next() { -// win_size.0 = resize.0; -// } -// } - -// pub fn if_resized ( -// resize: View, -// ) -> bool { +// pub fn if_resized (resize: View,) -> bool { // resize.len() > 0 // } diff --git a/kubi/src/rendering/primitives.rs b/kubi/src/rendering/primitives.rs index 115ff49..8dfbfa4 100644 --- a/kubi/src/rendering/primitives.rs +++ b/kubi/src/rendering/primitives.rs @@ -1,30 +1,24 @@ -// use shipyard::{Workload, IntoWorkload}; -// use glium::implement_vertex; +use bytemuck::{Pod, Zeroable}; +use shipyard::{IntoWorkload, Workload}; -// pub mod cube; -// pub mod rect; -// pub mod stri; +mod cube; -// use cube::init_cube_primitive; -// use rect::init_rect_primitive; -// use stri::init_stri_primitive; +pub fn init_primitives() -> Workload { + ( + cube::init_cube_primitive, + ).into_workload() +} -// #[derive(Clone, Copy, Default)] -// pub struct PositionOnlyVertex { -// pub position: [f32; 3], -// } -// implement_vertex!(PositionOnlyVertex, position); +#[derive(Clone, Copy, Default, Pod, Zeroable)] +#[repr(C, packed)] +pub struct PrimitiveVertex { + pub position: [f32; 3], +} -// #[derive(Clone, Copy, Default)] -// pub struct PositionOnlyVertex2d { -// pub position: [f32; 2], -// } -// implement_vertex!(PositionOnlyVertex2d, position); - -// pub fn init_primitives() -> Workload { -// ( -// init_cube_primitive, -// init_rect_primitive, -// init_stri_primitive, -// ).into_workload() -// } +impl PrimitiveVertex { + pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &wgpu::vertex_attr_array![0 => Float32x3], + }; +} diff --git a/kubi/src/rendering/primitives/cube.rs b/kubi/src/rendering/primitives/cube.rs index 2e3b5c8..3abd3e2 100644 --- a/kubi/src/rendering/primitives/cube.rs +++ b/kubi/src/rendering/primitives/cube.rs @@ -1,85 +1,49 @@ -// use shipyard::{AllStoragesView, NonSendSync, UniqueView, Unique}; -// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; -// use crate::rendering::Renderer; -// use super::PositionOnlyVertex; +use shipyard::{AllStoragesView, Unique, UniqueView}; +use wgpu::util::DeviceExt; +use crate::rendering::{BufferPair, Renderer}; +use super::PrimitiveVertex; -// #[derive(Unique)] -// pub struct CubePrimitive(pub VertexBuffer, pub IndexBuffer); +#[derive(Unique)] +pub struct CubePrimitive(pub BufferPair); -// #[derive(Unique)] -// pub struct CenteredCubePrimitive(pub VertexBuffer, pub IndexBuffer); +/// Vertices for a centered cube with a side length of 1 +const CUBE_VERTICES: &[PrimitiveVertex] = &[ + // front + PrimitiveVertex { position: [-0.5, -0.5, 0.5] }, + PrimitiveVertex { position: [ 0.5, -0.5, 0.5] }, + PrimitiveVertex { position: [ 0.5, 0.5, 0.5] }, + PrimitiveVertex { position: [-0.5, 0.5, 0.5] }, + // back + PrimitiveVertex { position: [-0.5, -0.5, -0.5] }, + PrimitiveVertex { position: [ 0.5, -0.5, -0.5] }, + PrimitiveVertex { position: [ 0.5, 0.5, -0.5] }, + PrimitiveVertex { position: [-0.5, 0.5, -0.5] }, +]; -// const CENTERED_CUBE_VERTICES: &[PositionOnlyVertex] = &[ -// // front -// PositionOnlyVertex { position: [-0.5, -0.5, 0.5] }, -// PositionOnlyVertex { position: [ 0.5, -0.5, 0.5] }, -// PositionOnlyVertex { position: [ 0.5, 0.5, 0.5] }, -// PositionOnlyVertex { position: [-0.5, 0.5, 0.5] }, -// // back -// PositionOnlyVertex { position: [-0.5, -0.5, -0.5] }, -// PositionOnlyVertex { position: [ 0.5, -0.5, -0.5] }, -// PositionOnlyVertex { position: [ 0.5, 0.5, -0.5] }, -// PositionOnlyVertex { position: [-0.5, 0.5, -0.5] }, -// ]; -// const CUBE_VERTICES: &[PositionOnlyVertex] = &[ -// // front -// PositionOnlyVertex { position: [0.0, 0.0, 1.0] }, -// PositionOnlyVertex { position: [1.0, 0.0, 1.0] }, -// PositionOnlyVertex { position: [1.0, 1.0, 1.0] }, -// PositionOnlyVertex { position: [0.0, 1.0, 1.0] }, -// // back -// PositionOnlyVertex { position: [0.0, 0.0, 0.0] }, -// PositionOnlyVertex { position: [1.0, 0.0, 0.0] }, -// PositionOnlyVertex { position: [1.0, 1.0, 0.0] }, -// PositionOnlyVertex { position: [0.0, 1.0, 0.0] }, -// ]; -// const CUBE_INDICES: &[u16] = &[ -// // front -// 0, 1, 2, -// 2, 3, 0, -// // right -// 1, 5, 6, -// 6, 2, 1, -// // back -// 7, 6, 5, -// 5, 4, 7, -// // left -// 4, 0, 3, -// 3, 7, 4, -// // bottom -// 4, 5, 1, -// 1, 0, 4, -// // top -// 3, 2, 6, -// 6, 7, 3 -// ]; +/// Indices for a cube primitive +const CUBE_INDICES: &[u16] = &[ + 0, 1, 2, 2, 3, 0, // front + 1, 5, 6, 6, 2, 1, // right + 7, 6, 5, 5, 4, 7, // back + 4, 0, 3, 3, 7, 4, // left + 4, 5, 1, 1, 0, 4, // bottom + 3, 2, 6, 6, 7, 3, // top +]; -// pub(super) fn init_cube_primitive( -// storages: AllStoragesView, -// display: NonSendSync> -// ) { -// { -// let vert = VertexBuffer::immutable( -// &display.display, -// CUBE_VERTICES -// ).unwrap(); -// let index = IndexBuffer::immutable( -// &display.display, -// PrimitiveType::TrianglesList, -// CUBE_INDICES -// ).unwrap(); -// storages.add_unique_non_send_sync(CubePrimitive(vert, index)); -// } -// { -// let vert = VertexBuffer::immutable( -// &display.display, -// CENTERED_CUBE_VERTICES -// ).unwrap(); -// let index = IndexBuffer::immutable( -// &display.display, -// PrimitiveType::TrianglesList, -// CUBE_INDICES -// ).unwrap(); -// storages.add_unique_non_send_sync(CenteredCubePrimitive(vert, index)); -// } -// } +pub fn init_cube_primitive(storages: AllStoragesView) { + let renderer = storages.borrow::>().unwrap(); + storages.add_unique(CubePrimitive(BufferPair { + index: renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("cube_index_buffer"), + contents: bytemuck::cast_slice(CUBE_INDICES), + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::INDEX, + }), + index_len: CUBE_INDICES.len() as u32, + vertex: renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("cube_vertex_buffer"), + contents: bytemuck::cast_slice(CUBE_VERTICES), + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::VERTEX, + }), + vertex_len: CUBE_VERTICES.len() as u32, + })); +} diff --git a/kubi/src/rendering/primitives/rect.rs b/kubi/src/rendering/primitives/rect.rs deleted file mode 100644 index 4eca75c..0000000 --- a/kubi/src/rendering/primitives/rect.rs +++ /dev/null @@ -1,31 +0,0 @@ -// use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView}; -// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; -// use crate::rendering::Renderer; -// use super::PositionOnlyVertex2d; - -// #[derive(Unique)] -// pub struct RectPrimitive(pub VertexBuffer, pub IndexBuffer); - -// const RECT_VERTEX: &[PositionOnlyVertex2d] = &[ -// PositionOnlyVertex2d { position: [0., 0.] }, -// PositionOnlyVertex2d { position: [1., 0.] }, -// PositionOnlyVertex2d { position: [0., 1.] }, -// PositionOnlyVertex2d { position: [1., 1.] }, -// ]; -// const RECT_INDEX: &[u16] = &[0, 1, 2, 1, 3, 2]; - -// pub(super) fn init_rect_primitive( -// storages: AllStoragesView, -// display: NonSendSync> -// ) { -// let vert = VertexBuffer::immutable( -// &display.display, -// RECT_VERTEX -// ).unwrap(); -// let index = IndexBuffer::immutable( -// &display.display, -// PrimitiveType::TrianglesList, -// RECT_INDEX -// ).unwrap(); -// storages.add_unique_non_send_sync(RectPrimitive(vert, index)); -// } diff --git a/kubi/src/rendering/primitives/stri.rs b/kubi/src/rendering/primitives/stri.rs deleted file mode 100644 index 2cafaf9..0000000 --- a/kubi/src/rendering/primitives/stri.rs +++ /dev/null @@ -1,30 +0,0 @@ -// use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView}; -// use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; -// use crate::rendering::Renderer; -// use super::PositionOnlyVertex2d; - -// #[derive(Unique)] -// pub struct STriPrimitive(pub VertexBuffer, pub IndexBuffer); - -// const STRI_VERTEX: &[PositionOnlyVertex2d] = &[ -// PositionOnlyVertex2d { position: [-1., -1.] }, -// PositionOnlyVertex2d { position: [ 3., -1.] }, -// PositionOnlyVertex2d { position: [-1., 3.] }, -// ]; -// const STRI_INDEX: &[u16] = &[0, 1, 2]; - -// pub(super) fn init_stri_primitive( -// storages: AllStoragesView, -// display: NonSendSync> -// ) { -// let vert = VertexBuffer::immutable( -// &display.display, -// STRI_VERTEX -// ).unwrap(); -// let index = IndexBuffer::immutable( -// &display.display, -// PrimitiveType::TrianglesList, -// STRI_INDEX -// ).unwrap(); -// storages.add_unique_non_send_sync(STriPrimitive(vert, index)); -// }