From 307cb748d5361aa9cc906678bbf59c9eebc3afc8 Mon Sep 17 00:00:00 2001 From: Vijfhoek Date: Thu, 10 Jun 2021 18:29:15 +0200 Subject: [PATCH] Move GeometryBuffers to its own file --- src/geometry.rs | 68 +---------------------------------------- src/geometry_buffers.rs | 67 ++++++++++++++++++++++++++++++++++++++++ src/hud/debug_hud.rs | 2 +- src/hud/hotbar_hud.rs | 3 +- src/hud/widgets_hud.rs | 3 +- src/main.rs | 1 + src/text_renderer.rs | 6 ++-- src/world/chunk.rs | 3 +- src/world/npc.rs | 3 +- 9 files changed, 79 insertions(+), 77 deletions(-) create mode 100644 src/geometry_buffers.rs diff --git a/src/geometry.rs b/src/geometry.rs index a9c317e..55d2425 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -1,11 +1,4 @@ -use std::marker::PhantomData; - -use wgpu::{ - util::{BufferInitDescriptor, DeviceExt}, - RenderPass, -}; - -use crate::{render_context::RenderContext, vertex::Vertex}; +use crate::vertex::Vertex; /// Represents a set of triangles by its vertices and indices. #[derive(Default)] @@ -30,62 +23,3 @@ impl Geometry { self.indices.len() } } - -pub struct GeometryBuffers { - pub vertices: wgpu::Buffer, - pub indices: wgpu::Buffer, - pub index_count: usize, - - // Phantom data to store the index type - _phantom: PhantomData, -} - -impl GeometryBuffers { - pub fn from_geometry( - render_context: &RenderContext, - geometry: &Geometry, - usage: wgpu::BufferUsage, - ) -> Self { - let vertices = render_context - .device - .create_buffer_init(&BufferInitDescriptor { - label: None, - contents: bytemuck::cast_slice(&geometry.vertices), - usage: wgpu::BufferUsage::VERTEX | usage, - }); - - let indices = render_context - .device - .create_buffer_init(&BufferInitDescriptor { - label: None, - contents: bytemuck::cast_slice(&geometry.indices), - usage: wgpu::BufferUsage::INDEX | usage, - }); - - Self { - vertices, - indices, - index_count: geometry.index_count(), - _phantom: PhantomData, - } - } - - pub fn draw_indexed(&self, render_pass: &mut RenderPass) -> usize { - render_pass.draw_indexed(0..self.index_count as u32, 0, 0..1); - self.index_count / 3 - } -} - -impl GeometryBuffers { - pub fn apply_buffers<'a>(&'a self, render_pass: &mut RenderPass<'a>) { - render_pass.set_vertex_buffer(0, self.vertices.slice(..)); - render_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint16); - } -} - -impl GeometryBuffers { - pub fn apply_buffers<'a>(&'a self, render_pass: &mut RenderPass<'a>) { - render_pass.set_vertex_buffer(0, self.vertices.slice(..)); - render_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); - } -} diff --git a/src/geometry_buffers.rs b/src/geometry_buffers.rs new file mode 100644 index 0000000..77f1f95 --- /dev/null +++ b/src/geometry_buffers.rs @@ -0,0 +1,67 @@ +use std::marker::PhantomData; + +use wgpu::{ + util::{BufferInitDescriptor, DeviceExt}, + RenderPass, +}; + +use crate::{geometry::Geometry, render_context::RenderContext, vertex::Vertex}; + +pub struct GeometryBuffers { + pub vertices: wgpu::Buffer, + pub indices: wgpu::Buffer, + pub index_count: usize, + + // Phantom data to store the index type + _phantom: PhantomData, +} + +impl GeometryBuffers { + pub fn from_geometry( + render_context: &RenderContext, + geometry: &Geometry, + usage: wgpu::BufferUsage, + ) -> Self { + let vertices = render_context + .device + .create_buffer_init(&BufferInitDescriptor { + label: None, + contents: bytemuck::cast_slice(&geometry.vertices), + usage: wgpu::BufferUsage::VERTEX | usage, + }); + + let indices = render_context + .device + .create_buffer_init(&BufferInitDescriptor { + label: None, + contents: bytemuck::cast_slice(&geometry.indices), + usage: wgpu::BufferUsage::INDEX | usage, + }); + + Self { + vertices, + indices, + index_count: geometry.index_count(), + _phantom: PhantomData, + } + } + + pub fn draw_indexed(&self, render_pass: &mut RenderPass) -> usize { + render_pass.draw_indexed(0..self.index_count as u32, 0, 0..1); + self.index_count / 3 + } +} + +impl GeometryBuffers { + pub fn apply_buffers<'a>(&'a self, render_pass: &mut RenderPass<'a>) { + render_pass.set_vertex_buffer(0, self.vertices.slice(..)); + render_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint16); + } +} + +impl GeometryBuffers { + pub fn apply_buffers<'a>(&'a self, render_pass: &mut RenderPass<'a>) { + render_pass.set_vertex_buffer(0, self.vertices.slice(..)); + render_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + } +} diff --git a/src/hud/debug_hud.rs b/src/hud/debug_hud.rs index 1d5b247..149964f 100644 --- a/src/hud/debug_hud.rs +++ b/src/hud/debug_hud.rs @@ -4,7 +4,7 @@ use cgmath::Point3; use wgpu::RenderPass; use crate::{ - geometry::GeometryBuffers, + geometry_buffers::GeometryBuffers, render_context::RenderContext, text_renderer::{self, TextRenderer}, }; diff --git a/src/hud/hotbar_hud.rs b/src/hud/hotbar_hud.rs index 85028f4..6a1f337 100644 --- a/src/hud/hotbar_hud.rs +++ b/src/hud/hotbar_hud.rs @@ -3,7 +3,8 @@ use std::convert::TryInto; use wgpu::{BufferUsage, RenderPass}; use crate::{ - geometry::{Geometry, GeometryBuffers}, + geometry::Geometry, + geometry_buffers::GeometryBuffers, hud::{UI_SCALE_X, UI_SCALE_Y}, render_context::RenderContext, vertex::HudVertex, diff --git a/src/hud/widgets_hud.rs b/src/hud/widgets_hud.rs index 8c4e720..834b92c 100644 --- a/src/hud/widgets_hud.rs +++ b/src/hud/widgets_hud.rs @@ -2,7 +2,8 @@ use wgpu::{BindGroup, BufferUsage, RenderPass}; use crate::{ - geometry::{Geometry, GeometryBuffers}, + geometry::Geometry, + geometry_buffers::GeometryBuffers, hud::{UI_SCALE_X, UI_SCALE_Y}, render_context::RenderContext, texture::Texture, diff --git a/src/main.rs b/src/main.rs index 5103f55..4e0789d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod aabb; mod camera; mod geometry; +mod geometry_buffers; mod hud; mod player; mod render_context; diff --git a/src/text_renderer.rs b/src/text_renderer.rs index 4cc90c5..86893b0 100644 --- a/src/text_renderer.rs +++ b/src/text_renderer.rs @@ -1,10 +1,8 @@ use std::convert::TryInto; use crate::{ - geometry::{Geometry, GeometryBuffers}, - render_context::RenderContext, - texture::Texture, - vertex::HudVertex, + geometry::Geometry, geometry_buffers::GeometryBuffers, render_context::RenderContext, + texture::Texture, vertex::HudVertex, }; pub const DX: f32 = 20.0 / 640.0; diff --git a/src/world/chunk.rs b/src/world/chunk.rs index 2608bc4..094210c 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -2,7 +2,8 @@ use std::collections::VecDeque; use crate::{ aabb::Aabb, - geometry::{Geometry, GeometryBuffers}, + geometry::Geometry, + geometry_buffers::GeometryBuffers, render_context::RenderContext, vertex::BlockVertex, view::View, diff --git a/src/world/npc.rs b/src/world/npc.rs index 2c38543..18ad5ed 100644 --- a/src/world/npc.rs +++ b/src/world/npc.rs @@ -5,8 +5,7 @@ use cgmath::Vector3; use wgpu::{BufferUsage, RenderPass}; use crate::{ - geometry::{Geometry, GeometryBuffers}, - render_context::RenderContext, + geometry::Geometry, geometry_buffers::GeometryBuffers, render_context::RenderContext, vertex::BlockVertex, };