Skip to content

Commit

Permalink
time to implement
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Nov 12, 2023
1 parent f9ae3a3 commit 0608cc2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
28 changes: 23 additions & 5 deletions flare3d/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winit::{
window::Window,
};

use crate::texture;
use crate::{texture, Texture};

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
Expand Down Expand Up @@ -302,14 +302,15 @@ pub struct State {
vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer,
diffuse_bind_group: wgpu::BindGroup,
diffuse_texture: texture::Texture,
diffuse_texture: Texture,
camera: Camera,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: CameraController,
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
depth_texture: Texture,
}

impl State {
Expand Down Expand Up @@ -436,6 +437,8 @@ impl State {
label: Some("camera_bind_group"),
});

let depth_texture = texture::Texture::create_depth_texture(&device, &config, "depth_texture");

let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
Expand Down Expand Up @@ -469,7 +472,13 @@ impl State {
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
Expand Down Expand Up @@ -539,6 +548,7 @@ impl State {
camera_controller,
instances,
instance_buffer,
depth_texture,
}
}
}
Expand All @@ -551,6 +561,7 @@ impl State {
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
}
self.depth_texture = texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}

pub fn update(&mut self) {
Expand Down Expand Up @@ -590,8 +601,15 @@ impl State {
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
..Default::default()
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store
}),
stencil_ops: None,
}),
..Default::default()
});

render_pass.set_pipeline(&self.render_pipeline);
Expand Down
42 changes: 42 additions & 0 deletions flare3d/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,45 @@ impl Texture {
})
}
}

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: 200.0,
..Default::default()
}
);

Self { texture, view, sampler }
}
}

0 comments on commit 0608cc2

Please sign in to comment.