adjusted migration to tutorial9

pull/29/head
Ben Hansen 4 years ago
parent a5bdad14ab
commit 368021047a

2
Cargo.lock generated

@ -2045,7 +2045,7 @@ dependencies = [
"glsl-to-spirv 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.23.4 (registry+https://github.com/rust-lang/crates.io-index)",
"tobj 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wgpu 0.5.0 (git+https://github.com/gfx-rs/wgpu-rs.git)",
"wgpu 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.22.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

@ -12,12 +12,10 @@ winit = "0.22"
glsl-to-spirv = "0.1"
cgmath = "0.17"
failure = "0.1"
tobj = "1"
bytemuck = "1.2"
futures = "0.3"
# wgpu = "0.5"
wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git" }
# zerocopy = "0.2.8"
tobj = "1.0.0"
wgpu = "0.5.0"
futures = "0.3.4"
bytemuck = "1.2.0"
[[bin]]
name = "tutorial9-models"

@ -13,7 +13,7 @@ use model::{DrawModel, Vertex};
#[cfg_attr(rustfmt, rustfmt_skip)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);
@ -52,8 +52,7 @@ impl Uniforms {
}
fn update_view_proj(&mut self, camera: &Camera) {
self.view_proj = camera.build_view_projection_matrix();
// self.view_proj = OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix();
self.view_proj = OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix();
}
}
unsafe impl bytemuck::Zeroable for Uniforms {}
@ -152,11 +151,22 @@ struct Instance {
}
impl Instance {
fn to_matrix(&self) -> cgmath::Matrix4<f32> {
cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation)
fn to_raw(&self) -> InstanceRaw {
InstanceRaw {
model: cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation),
}
}
}
#[derive(Copy, Clone)]
struct InstanceRaw {
#[allow(dead_code)]
model: cgmath::Matrix4<f32>,
}
unsafe impl bytemuck::Pod for InstanceRaw {}
unsafe impl bytemuck::Zeroable for InstanceRaw {}
struct State {
surface: wgpu::Surface,
device: wgpu::Device,
@ -171,9 +181,9 @@ struct State {
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
depth_texture: wgpu::Texture,
depth_texture_view: wgpu::TextureView,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
}
@ -277,12 +287,12 @@ impl State {
let instance_data = instances
.iter()
.map(Instance::to_matrix)
.map(Instance::to_raw)
.collect::<Vec<_>>();
let instance_buffer_size =
instance_data.len() * std::mem::size_of::<cgmath::Matrix4<f32>>();
let instance_buffer = device.create_buffer_with_data(
matrix4f_cast_slice(&[instance_data]),
bytemuck::cast_slice(&instance_data),
wgpu::BufferUsage::STORAGE_READ,
);
@ -343,8 +353,7 @@ impl State {
let vs_module = device.create_shader_module(&vs_data);
let fs_module = device.create_shader_module(&fs_data);
let depth_texture = texture::Texture::create_depth_texture(&device, &sc_desc).texture;
let depth_texture_view = depth_texture.create_default_view();
let depth_texture = texture::Texture::create_depth_texture(&device, &sc_desc, "depth_texture");
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
@ -376,7 +385,7 @@ impl State {
write_mask: wgpu::ColorWrite::ALL,
}],
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
format: texture::DEPTH_FORMAT,
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE,
@ -409,14 +418,12 @@ impl State {
instances,
instance_buffer,
depth_texture,
depth_texture_view,
size,
}
}
async fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
self.depth_texture = texture::Texture::create_depth_texture(&self.device, &self.sc_desc).texture;
self.depth_texture_view = self.depth_texture.create_default_view();
self.depth_texture = texture::Texture::create_depth_texture(&self.device, &self.sc_desc, "depth_texture");
self.camera.aspect = self.sc_desc.width as f32 / self.sc_desc.height as f32;
self.size = new_size;
self.sc_desc.width = new_size.width;
@ -471,7 +478,7 @@ impl State {
},
}],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor {
attachment: &self.depth_texture_view,
attachment: &self.depth_texture.view,
depth_load_op: wgpu::LoadOp::Clear,
depth_store_op: wgpu::StoreOp::Store,
clear_depth: 1.0,
@ -495,54 +502,50 @@ impl State {
fn main() {
let event_loop = EventLoop::new();
let title = "tutorial9-models";
let title = env!("CARGO_PKG_NAME");
let window = winit::window::WindowBuilder::new()
.with_title(title)
.build(&event_loop)
.unwrap();
use futures::executor::block_on;
let mut state = block_on(State::new(&window));
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => window.request_redraw(),
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { input, .. } => match input {
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
} => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
WindowEvent::Resized(physical_size) => {
block_on(state.resize(*physical_size));
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
block_on(state.resize(**new_inner_size));
use futures::executor::block_on;
let mut state = block_on(State::new(&window));
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => window.request_redraw(),
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { input, .. } => match input {
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
} => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
WindowEvent::Resized(physical_size) => {
block_on(state.resize(*physical_size));
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
block_on(state.resize(**new_inner_size));
}
_ => {}
}
}
Event::RedrawRequested(_) => {
block_on(state.update());
block_on(state.render());
}
_ => {}
}
});
}
#[inline]
pub fn matrix4f_cast_slice(a: &[Vec<cgmath::Matrix4<f32>>]) -> &[u8] {
let new_len = a[0].len() * std::mem::size_of::<cgmath::Matrix4<f32>>();
unsafe { core::slice::from_raw_parts(a[0].as_ptr() as *const u8, new_len) }
Event::RedrawRequested(_) => {
block_on(state.update());
block_on(state.render());
}
_ => {}
}
});
}

@ -1,7 +1,6 @@
use image::GenericImageView;
use std::path::Path;
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub struct Texture {
pub texture: wgpu::Texture,
@ -10,30 +9,37 @@ pub struct Texture {
}
impl Texture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn load<P: AsRef<Path>>(
device: &wgpu::Device,
path: P,
) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
// Needed to appease the borrow checker
let path_copy = path.as_ref().to_path_buf();
let label = path_copy.to_str();
let img = image::open(path)?;
Self::from_image(device, &img)
Self::from_image(device, &img, label)
}
pub fn create_depth_texture(
device: &wgpu::Device,
sc_desc: &wgpu::SwapChainDescriptor,
) -> Self {
pub fn create_depth_texture(device: &wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor, label: &str) -> Self {
let size = wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth: 1,
};
let desc = wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth: 1,
},
label: Some(label),
size,
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: DEPTH_FORMAT,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT
| wgpu::TextureUsage::SAMPLED
| wgpu::TextureUsage::COPY_SRC,
};
let texture = device.create_texture(&desc);
@ -43,31 +49,26 @@ impl Texture {
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
compare: wgpu::CompareFunction::Always,
compare: wgpu::CompareFunction::LessEqual,
});
Self {
texture,
view,
sampler,
}
Self { texture, view, sampler }
}
pub fn from_bytes(
device: &wgpu::Device,
bytes: &[u8],
) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
#[allow(dead_code)]
pub fn from_bytes(device: &wgpu::Device, bytes: &[u8], label: &str) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
let img = image::load_from_memory(bytes)?;
Self::from_image(device, &img)
Self::from_image(device, &img, Some(label))
}
pub fn from_image(
device: &wgpu::Device,
img: &image::DynamicImage,
label: Option<&str>,
) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
let rgba = img.to_rgba();
let dimensions = img.dimensions();
@ -78,8 +79,9 @@ impl Texture {
depth: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: None,
label,
size,
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
@ -87,10 +89,14 @@ impl Texture {
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
});
let buffer = device
.create_buffer_with_data(bytemuck::cast_slice(&rgba), wgpu::BufferUsage::COPY_SRC);
let buffer = device.create_buffer_with_data(
&rgba,
wgpu::BufferUsage::COPY_SRC,
);
let mut encoder = device.create_command_encoder(&Default::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("texture_buffer_copy_encoder"),
});
encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
@ -98,13 +104,13 @@ impl Texture {
offset: 0,
bytes_per_row: 4 * dimensions.0,
rows_per_image: dimensions.1,
},
},
wgpu::TextureCopyView {
texture: &texture,
mip_level: 0,
array_layer: 0,
origin: wgpu::Origin3d::ZERO,
},
},
size,
);
@ -122,14 +128,7 @@ impl Texture {
lod_max_clamp: 100.0,
compare: wgpu::CompareFunction::Always,
});
Ok((
Self {
texture,
view,
sampler,
},
cmd_buffer,
))
Ok((Self { texture, view, sampler }, cmd_buffer))
}
}

@ -80,11 +80,16 @@ Speaking of textures, let's add a `load()` method to `Texture` in `texture.rs`.
```rust
use std::path::Path;
impl Texture {
pub fn load<P: AsRef<Path>>(device: &wgpu::Device, path: P) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
let img = image::open(path)?;
Self::from_image(device, &img)
}
pub fn load<P: AsRef<Path>>(
device: &wgpu::Device,
path: P,
) -> Result<(Self, wgpu::CommandBuffer), failure::Error> {
// Needed to appease the borrow checker
let path_copy = path.as_ref().to_path_buf();
let label = path_copy.to_str();
let img = image::open(path)?;
Self::from_image(device, &img, label)
}
```

Loading…
Cancel
Save