-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e29bce5
commit 55fd120
Showing
12 changed files
with
1,071 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod model; | ||
mod texture; | ||
mod vertex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// code from: https://github.com/sotrh/learn-wgpu | ||
|
||
use std::ops::Range; | ||
|
||
use super::texture; | ||
|
||
pub struct Model { | ||
pub meshes: Vec<Mesh>, | ||
pub materials: Vec<Material>, | ||
} | ||
|
||
pub struct Material { | ||
pub name: String, | ||
pub diffuse_texture: texture::Texture, | ||
pub bind_group: wgpu::BindGroup, | ||
} | ||
|
||
pub struct Mesh { | ||
pub name: String, | ||
pub vertex_buffer: wgpu::Buffer, | ||
pub index_buffer: wgpu::Buffer, | ||
pub num_elements: u32, | ||
pub material: usize, | ||
} | ||
|
||
pub trait DrawModel<'a> { | ||
fn draw_mesh( | ||
&mut self, | ||
mesh: &'a Mesh, | ||
material: &'a Material, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
); | ||
|
||
fn draw_mesh_instanced( | ||
&mut self, | ||
mesh: &'a Mesh, | ||
material: &'a Material, | ||
instances: Range<u32>, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
); | ||
|
||
fn draw_model(&mut self, model: &'a Model, camera_bind_group: &'a wgpu::BindGroup); | ||
fn draw_model_instanced( | ||
&mut self, | ||
model: &'a Model, | ||
instances: Range<u32>, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
); | ||
} | ||
|
||
impl<'a> DrawModel<'a> for wgpu::RenderPass<'a> { | ||
fn draw_mesh( | ||
&mut self, | ||
mesh: &'a Mesh, | ||
material: &'a Material, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
) { | ||
self.draw_mesh_instanced(mesh, material, 0..1, camera_bind_group); | ||
} | ||
|
||
fn draw_mesh_instanced( | ||
&mut self, | ||
mesh: &'a Mesh, | ||
material: &'a Material, | ||
instances: Range<u32>, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
) { | ||
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..)); | ||
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32); | ||
self.set_bind_group(0, &material.bind_group, &[]); | ||
self.set_bind_group(1, camera_bind_group, &[]); | ||
self.draw_indexed(0..mesh.num_elements, 0, instances); | ||
} | ||
|
||
fn draw_model(&mut self, model: &'a Model, camera_bind_group: &'a wgpu::BindGroup) { | ||
self.draw_model_instanced(model, 0..1, camera_bind_group); | ||
} | ||
|
||
fn draw_model_instanced( | ||
&mut self, | ||
model: &'a Model, | ||
instances: Range<u32>, | ||
camera_bind_group: &'a wgpu::BindGroup, | ||
) { | ||
for mesh in &model.meshes { | ||
let material = &model.materials[mesh.material]; | ||
self.draw_mesh_instanced(mesh, material, instances.clone(), camera_bind_group); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// code from: https://github.com/sotrh/learn-wgpu | ||
|
||
use image::GenericImageView; | ||
|
||
pub struct Texture { | ||
texture: wgpu::Texture, | ||
view: wgpu::TextureView, | ||
sampler: wgpu::Sampler, | ||
} | ||
|
||
impl Texture { | ||
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; | ||
|
||
pub fn create_depth_texture( | ||
device: &wgpu::Device, | ||
config: &wgpu::SurfaceConfiguration, | ||
label: &str, | ||
) -> Self { | ||
let size = wgpu::Extent3d { | ||
width: config.width, | ||
height: config.height, | ||
depth_or_array_layers: 1, | ||
}; | ||
let desc = wgpu::TextureDescriptor { | ||
label: Some(label), | ||
size, | ||
mip_level_count: 1, | ||
sample_count: 1, | ||
dimension: wgpu::TextureDimension::D2, | ||
format: Self::DEPTH_FORMAT, | ||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, | ||
view_formats: &[], | ||
}; | ||
let texture = device.create_texture(&desc); | ||
|
||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); | ||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor { | ||
address_mode_u: wgpu::AddressMode::ClampToEdge, | ||
address_mode_v: wgpu::AddressMode::ClampToEdge, | ||
address_mode_w: wgpu::AddressMode::ClampToEdge, | ||
mag_filter: wgpu::FilterMode::Linear, | ||
min_filter: wgpu::FilterMode::Linear, | ||
mipmap_filter: wgpu::FilterMode::Nearest, | ||
compare: Some(wgpu::CompareFunction::LessEqual), | ||
lod_min_clamp: 0.0, | ||
lod_max_clamp: 100.0, | ||
..Default::default() | ||
}); | ||
|
||
Self { | ||
texture, | ||
view, | ||
sampler, | ||
} | ||
} | ||
|
||
pub fn from_bytes( | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
bytes: &[u8], | ||
label: &str, | ||
) -> anyhow::Result<Self> { | ||
let img = image::load_from_memory(bytes)?; | ||
Self::from_image(device, queue, &img, Some(label)) | ||
} | ||
|
||
pub fn from_image( | ||
device: &wgpu::Device, | ||
queue: &wgpu::Queue, | ||
img: &image::DynamicImage, | ||
label: Option<&str>, | ||
) -> anyhow::Result<Self> { | ||
let dimensions = img.dimensions(); | ||
let rgba = img.to_rgba8(); | ||
|
||
let size = wgpu::Extent3d { | ||
width: dimensions.0, | ||
height: dimensions.1, | ||
depth_or_array_layers: 1, | ||
}; | ||
let format = wgpu::TextureFormat::Rgba8UnormSrgb; | ||
let texture = device.create_texture(&wgpu::TextureDescriptor { | ||
label, | ||
size, | ||
mip_level_count: 1, | ||
sample_count: 1, | ||
dimension: wgpu::TextureDimension::D2, | ||
format, | ||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, | ||
view_formats: &[], | ||
}); | ||
|
||
queue.write_texture( | ||
wgpu::ImageCopyTexture { | ||
aspect: wgpu::TextureAspect::All, | ||
texture: &texture, | ||
mip_level: 0, | ||
origin: wgpu::Origin3d::ZERO, | ||
}, | ||
&rgba, | ||
wgpu::ImageDataLayout { | ||
offset: 0, | ||
bytes_per_row: Some(4 * dimensions.0), | ||
rows_per_image: Some(dimensions.1), | ||
}, | ||
size, | ||
); | ||
|
||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); | ||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor { | ||
address_mode_u: wgpu::AddressMode::ClampToEdge, | ||
address_mode_v: wgpu::AddressMode::ClampToEdge, | ||
address_mode_w: wgpu::AddressMode::ClampToEdge, | ||
mag_filter: wgpu::FilterMode::Linear, | ||
min_filter: wgpu::FilterMode::Nearest, | ||
mipmap_filter: wgpu::FilterMode::Nearest, | ||
..Default::default() | ||
}); | ||
|
||
Ok(Self { | ||
texture, | ||
view, | ||
sampler, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::mem; | ||
|
||
pub trait Vertex { | ||
fn desc() -> wgpu::VertexBufferLayout<'static>; | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] | ||
pub struct ModelVertex { | ||
pub position: [f32; 3], | ||
pub tex_coords: [f32; 2], | ||
pub normal: [f32; 3], | ||
} | ||
|
||
impl Vertex for ModelVertex { | ||
fn desc() -> wgpu::VertexBufferLayout<'static> { | ||
wgpu::VertexBufferLayout { | ||
array_stride: mem::size_of::<ModelVertex>() as wgpu::BufferAddress, | ||
step_mode: wgpu::VertexStepMode::Vertex, | ||
attributes: &[ | ||
wgpu::VertexAttribute { | ||
offset: 0, | ||
shader_location: 0, | ||
format: wgpu::VertexFormat::Float32x3, | ||
}, | ||
wgpu::VertexAttribute { | ||
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, | ||
shader_location: 1, | ||
format: wgpu::VertexFormat::Float32x2, | ||
}, | ||
wgpu::VertexAttribute { | ||
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress, | ||
shader_location: 2, | ||
format: wgpu::VertexFormat::Float32x3, | ||
}, | ||
], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::obj::model::Model; | ||
|
||
|
||
struct Scene { | ||
render_pipeline: wgpu::RenderPipeline, | ||
models: Vec<Model> | ||
} |
Oops, something went wrong.