From 78dcc30258ff4cb5b3fdea14df6c9e0e2dc8a23e Mon Sep 17 00:00:00 2001 From: tolelom <98kimsungmin@naver.com> Date: Tue, 24 Mar 2026 19:47:59 +0900 Subject: [PATCH] feat(renderer): add MeshVertex, Mesh, and depth buffer support Co-Authored-By: Claude Sonnet 4.6 --- crates/voltex_renderer/src/gpu.rs | 21 ++++++++++++++++++ crates/voltex_renderer/src/lib.rs | 4 +++- crates/voltex_renderer/src/mesh.rs | 24 +++++++++++++++++++++ crates/voltex_renderer/src/vertex.rs | 32 ++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 crates/voltex_renderer/src/mesh.rs diff --git a/crates/voltex_renderer/src/gpu.rs b/crates/voltex_renderer/src/gpu.rs index b8c0a3c..85041de 100644 --- a/crates/voltex_renderer/src/gpu.rs +++ b/crates/voltex_renderer/src/gpu.rs @@ -1,12 +1,29 @@ use std::sync::Arc; use winit::window::Window; +pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; + +fn create_depth_texture(device: &wgpu::Device, width: u32, height: u32) -> wgpu::TextureView { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Depth Texture"), + size: wgpu::Extent3d { width, height, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: DEPTH_FORMAT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + texture.create_view(&wgpu::TextureViewDescriptor::default()) +} + pub struct GpuContext { pub surface: wgpu::Surface<'static>, pub device: wgpu::Device, pub queue: wgpu::Queue, pub config: wgpu::SurfaceConfiguration, pub surface_format: wgpu::TextureFormat, + pub depth_view: wgpu::TextureView, } impl GpuContext { @@ -64,12 +81,15 @@ impl GpuContext { }; surface.configure(&device, &config); + let depth_view = create_depth_texture(&device, config.width, config.height); + Self { surface, device, queue, config, surface_format, + depth_view, } } @@ -78,6 +98,7 @@ impl GpuContext { self.config.width = width; self.config.height = height; self.surface.configure(&self.device, &self.config); + self.depth_view = create_depth_texture(&self.device, width, height); } } } diff --git a/crates/voltex_renderer/src/lib.rs b/crates/voltex_renderer/src/lib.rs index b3d1710..f9021fe 100644 --- a/crates/voltex_renderer/src/lib.rs +++ b/crates/voltex_renderer/src/lib.rs @@ -1,5 +1,7 @@ pub mod gpu; pub mod pipeline; pub mod vertex; +pub mod mesh; -pub use gpu::GpuContext; +pub use gpu::{GpuContext, DEPTH_FORMAT}; +pub use mesh::Mesh; diff --git a/crates/voltex_renderer/src/mesh.rs b/crates/voltex_renderer/src/mesh.rs new file mode 100644 index 0000000..5d0ba2a --- /dev/null +++ b/crates/voltex_renderer/src/mesh.rs @@ -0,0 +1,24 @@ +use crate::vertex::MeshVertex; +use wgpu::util::DeviceExt; + +pub struct Mesh { + pub vertex_buffer: wgpu::Buffer, + pub index_buffer: wgpu::Buffer, + pub num_indices: u32, +} + +impl Mesh { + pub fn new(device: &wgpu::Device, vertices: &[MeshVertex], indices: &[u32]) -> Self { + let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Mesh Vertex Buffer"), + contents: bytemuck::cast_slice(vertices), + usage: wgpu::BufferUsages::VERTEX, + }); + let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Mesh Index Buffer"), + contents: bytemuck::cast_slice(indices), + usage: wgpu::BufferUsages::INDEX, + }); + Self { vertex_buffer, index_buffer, num_indices: indices.len() as u32 } + } +} diff --git a/crates/voltex_renderer/src/vertex.rs b/crates/voltex_renderer/src/vertex.rs index 1cfe87f..337de76 100644 --- a/crates/voltex_renderer/src/vertex.rs +++ b/crates/voltex_renderer/src/vertex.rs @@ -25,3 +25,35 @@ impl Vertex { ], }; } + +#[repr(C)] +#[derive(Copy, Clone, Debug, Pod, Zeroable)] +pub struct MeshVertex { + pub position: [f32; 3], + pub normal: [f32; 3], + pub uv: [f32; 2], +} + +impl MeshVertex { + pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &[ + wgpu::VertexAttribute { + offset: 0, + shader_location: 0, + format: wgpu::VertexFormat::Float32x3, + }, + wgpu::VertexAttribute { + offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, + shader_location: 1, + format: wgpu::VertexFormat::Float32x3, + }, + wgpu::VertexAttribute { + offset: (std::mem::size_of::<[f32; 3]>() * 2) as wgpu::BufferAddress, + shader_location: 2, + format: wgpu::VertexFormat::Float32x2, + }, + ], + }; +}