Move GeometryBuffers to its own file
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sijmen 2021-06-10 18:29:15 +02:00
parent 1960da95df
commit 307cb748d5
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
9 changed files with 79 additions and 77 deletions

View file

@ -1,11 +1,4 @@
use std::marker::PhantomData; use crate::vertex::Vertex;
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
RenderPass,
};
use crate::{render_context::RenderContext, vertex::Vertex};
/// Represents a set of triangles by its vertices and indices. /// Represents a set of triangles by its vertices and indices.
#[derive(Default)] #[derive(Default)]
@ -30,62 +23,3 @@ impl<T: Vertex, I> Geometry<T, I> {
self.indices.len() self.indices.len()
} }
} }
pub struct GeometryBuffers<I> {
pub vertices: wgpu::Buffer,
pub indices: wgpu::Buffer,
pub index_count: usize,
// Phantom data to store the index type
_phantom: PhantomData<I>,
}
impl<I: bytemuck::Pod> GeometryBuffers<I> {
pub fn from_geometry<V: Vertex + bytemuck::Pod>(
render_context: &RenderContext,
geometry: &Geometry<V, I>,
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<u16> {
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<u32> {
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);
}
}

67
src/geometry_buffers.rs Normal file
View file

@ -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<I> {
pub vertices: wgpu::Buffer,
pub indices: wgpu::Buffer,
pub index_count: usize,
// Phantom data to store the index type
_phantom: PhantomData<I>,
}
impl<I: bytemuck::Pod> GeometryBuffers<I> {
pub fn from_geometry<V: Vertex + bytemuck::Pod>(
render_context: &RenderContext,
geometry: &Geometry<V, I>,
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<u16> {
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<u32> {
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);
}
}

View file

@ -4,7 +4,7 @@ use cgmath::Point3;
use wgpu::RenderPass; use wgpu::RenderPass;
use crate::{ use crate::{
geometry::GeometryBuffers, geometry_buffers::GeometryBuffers,
render_context::RenderContext, render_context::RenderContext,
text_renderer::{self, TextRenderer}, text_renderer::{self, TextRenderer},
}; };

View file

@ -3,7 +3,8 @@ use std::convert::TryInto;
use wgpu::{BufferUsage, RenderPass}; use wgpu::{BufferUsage, RenderPass};
use crate::{ use crate::{
geometry::{Geometry, GeometryBuffers}, geometry::Geometry,
geometry_buffers::GeometryBuffers,
hud::{UI_SCALE_X, UI_SCALE_Y}, hud::{UI_SCALE_X, UI_SCALE_Y},
render_context::RenderContext, render_context::RenderContext,
vertex::HudVertex, vertex::HudVertex,

View file

@ -2,7 +2,8 @@
use wgpu::{BindGroup, BufferUsage, RenderPass}; use wgpu::{BindGroup, BufferUsage, RenderPass};
use crate::{ use crate::{
geometry::{Geometry, GeometryBuffers}, geometry::Geometry,
geometry_buffers::GeometryBuffers,
hud::{UI_SCALE_X, UI_SCALE_Y}, hud::{UI_SCALE_X, UI_SCALE_Y},
render_context::RenderContext, render_context::RenderContext,
texture::Texture, texture::Texture,

View file

@ -1,6 +1,7 @@
mod aabb; mod aabb;
mod camera; mod camera;
mod geometry; mod geometry;
mod geometry_buffers;
mod hud; mod hud;
mod player; mod player;
mod render_context; mod render_context;

View file

@ -1,10 +1,8 @@
use std::convert::TryInto; use std::convert::TryInto;
use crate::{ use crate::{
geometry::{Geometry, GeometryBuffers}, geometry::Geometry, geometry_buffers::GeometryBuffers, render_context::RenderContext,
render_context::RenderContext, texture::Texture, vertex::HudVertex,
texture::Texture,
vertex::HudVertex,
}; };
pub const DX: f32 = 20.0 / 640.0; pub const DX: f32 = 20.0 / 640.0;

View file

@ -2,7 +2,8 @@ use std::collections::VecDeque;
use crate::{ use crate::{
aabb::Aabb, aabb::Aabb,
geometry::{Geometry, GeometryBuffers}, geometry::Geometry,
geometry_buffers::GeometryBuffers,
render_context::RenderContext, render_context::RenderContext,
vertex::BlockVertex, vertex::BlockVertex,
view::View, view::View,

View file

@ -5,8 +5,7 @@ use cgmath::Vector3;
use wgpu::{BufferUsage, RenderPass}; use wgpu::{BufferUsage, RenderPass};
use crate::{ use crate::{
geometry::{Geometry, GeometryBuffers}, geometry::Geometry, geometry_buffers::GeometryBuffers, render_context::RenderContext,
render_context::RenderContext,
vertex::BlockVertex, vertex::BlockVertex,
}; };