|
|
|
@ -1,12 +1,12 @@
|
|
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
|
|
use cgmath::prelude::*;
|
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
|
use winit::{
|
|
|
|
|
event::*,
|
|
|
|
|
event_loop::{EventLoop, ControlFlow},
|
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
|
window::{Window, WindowBuilder},
|
|
|
|
|
};
|
|
|
|
|
use cgmath::prelude::*;
|
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
|
|
|
|
|
|
mod texture;
|
|
|
|
|
|
|
|
|
@ -37,36 +37,56 @@ impl Vertex {
|
|
|
|
|
shader_location: 1,
|
|
|
|
|
format: wgpu::VertexFormat::Float2,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const VERTICES: &[Vertex] = &[
|
|
|
|
|
Vertex { position: [-0.0868241, -0.49240386, 0.0], tex_coords: [1.0 - 0.4131759, 1.0 - 0.00759614], }, // A
|
|
|
|
|
Vertex { position: [-0.49513406, -0.06958647, 0.0], tex_coords: [1.0 - 0.0048659444, 1.0 - 0.43041354], }, // B
|
|
|
|
|
Vertex { position: [-0.21918549, 0.44939706, 0.0], tex_coords: [1.0 - 0.28081453, 1.0 - 0.949397057], }, // C
|
|
|
|
|
Vertex { position: [0.35966998, 0.3473291, 0.0], tex_coords: [1.0 - 0.85967, 1.0 - 0.84732911], }, // D
|
|
|
|
|
Vertex { position: [0.44147372, -0.2347359, 0.0], tex_coords: [1.0 - 0.9414737, 1.0 - 0.2652641], }, // E
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [-0.0868241, -0.49240386, 0.0],
|
|
|
|
|
tex_coords: [1.0 - 0.4131759, 1.0 - 0.00759614],
|
|
|
|
|
}, // A
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [-0.49513406, -0.06958647, 0.0],
|
|
|
|
|
tex_coords: [1.0 - 0.0048659444, 1.0 - 0.43041354],
|
|
|
|
|
}, // B
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [-0.21918549, 0.44939706, 0.0],
|
|
|
|
|
tex_coords: [1.0 - 0.28081453, 1.0 - 0.949397057],
|
|
|
|
|
}, // C
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [0.35966998, 0.3473291, 0.0],
|
|
|
|
|
tex_coords: [1.0 - 0.85967, 1.0 - 0.84732911],
|
|
|
|
|
}, // D
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [0.44147372, -0.2347359, 0.0],
|
|
|
|
|
tex_coords: [1.0 - 0.9414737, 1.0 - 0.2652641],
|
|
|
|
|
}, // E
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const INDICES: &[u16] = &[
|
|
|
|
|
0, 1, 4,
|
|
|
|
|
1, 2, 4,
|
|
|
|
|
2, 3, 4,
|
|
|
|
|
];
|
|
|
|
|
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
|
|
|
|
|
|
|
|
|
|
const DEPTH_VERTICES: &[Vertex] = &[
|
|
|
|
|
Vertex { position: [0.0, 0.0, 0.0], tex_coords: [0.0, 1.0]},
|
|
|
|
|
Vertex { position: [1.0, 0.0, 0.0], tex_coords: [1.0, 1.0]},
|
|
|
|
|
Vertex { position: [1.0, 1.0, 0.0], tex_coords: [1.0, 0.0]},
|
|
|
|
|
Vertex { position: [0.0, 1.0, 0.0], tex_coords: [0.0, 0.0]},
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [0.0, 0.0, 0.0],
|
|
|
|
|
tex_coords: [0.0, 1.0],
|
|
|
|
|
},
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [1.0, 0.0, 0.0],
|
|
|
|
|
tex_coords: [1.0, 1.0],
|
|
|
|
|
},
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [1.0, 1.0, 0.0],
|
|
|
|
|
tex_coords: [1.0, 0.0],
|
|
|
|
|
},
|
|
|
|
|
Vertex {
|
|
|
|
|
position: [0.0, 1.0, 0.0],
|
|
|
|
|
tex_coords: [0.0, 0.0],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const DEPTH_INDICES: &[u16] = &[
|
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
|
|
|
|
];
|
|
|
|
|
const DEPTH_INDICES: &[u16] = &[0, 1, 2, 0, 2, 3];
|
|
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
|
|
|
@ -79,8 +99,11 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
|
|
|
|
const NUM_INSTANCES_PER_ROW: u32 = 10;
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
const NUM_INSTANCES: u32 = NUM_INSTANCES_PER_ROW * NUM_INSTANCES_PER_ROW;
|
|
|
|
|
const INSTANCE_DISPLACEMENT: cgmath::Vector3<f32> = cgmath::Vector3::new(NUM_INSTANCES_PER_ROW as f32 * 0.5, 0.0, NUM_INSTANCES_PER_ROW as f32 * 0.5);
|
|
|
|
|
|
|
|
|
|
const INSTANCE_DISPLACEMENT: cgmath::Vector3<f32> = cgmath::Vector3::new(
|
|
|
|
|
NUM_INSTANCES_PER_ROW as f32 * 0.5,
|
|
|
|
|
0.0,
|
|
|
|
|
NUM_INSTANCES_PER_ROW as f32 * 0.5,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
struct Camera {
|
|
|
|
|
eye: cgmath::Point3<f32>,
|
|
|
|
@ -147,11 +170,12 @@ impl CameraController {
|
|
|
|
|
fn process_events(&mut self, event: &WindowEvent) -> bool {
|
|
|
|
|
match event {
|
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
|
input: KeyboardInput {
|
|
|
|
|
state,
|
|
|
|
|
virtual_keycode: Some(keycode),
|
|
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
input:
|
|
|
|
|
KeyboardInput {
|
|
|
|
|
state,
|
|
|
|
|
virtual_keycode: Some(keycode),
|
|
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
..
|
|
|
|
|
} => {
|
|
|
|
|
let is_pressed = *state == ElementState::Pressed;
|
|
|
|
@ -208,8 +232,8 @@ impl CameraController {
|
|
|
|
|
let forward_mag = forward.magnitude();
|
|
|
|
|
|
|
|
|
|
if self.is_right_pressed {
|
|
|
|
|
// Rescale the distance between the target and eye so
|
|
|
|
|
// that it doesn't change. The eye therefore still
|
|
|
|
|
// Rescale the distance between the target and eye so
|
|
|
|
|
// that it doesn't change. The eye therefore still
|
|
|
|
|
// lies on the circle made by the target and eye.
|
|
|
|
|
camera.eye = camera.target - (forward + right * self.speed).normalize() * forward_mag;
|
|
|
|
|
}
|
|
|
|
@ -227,7 +251,8 @@ struct Instance {
|
|
|
|
|
impl Instance {
|
|
|
|
|
fn to_raw(&self) -> InstanceRaw {
|
|
|
|
|
InstanceRaw {
|
|
|
|
|
model: cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation),
|
|
|
|
|
model: cgmath::Matrix4::from_translation(self.position)
|
|
|
|
|
* cgmath::Matrix4::from(self.rotation),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -255,31 +280,27 @@ impl DepthPass {
|
|
|
|
|
fn new(device: &wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Self {
|
|
|
|
|
let texture = texture::Texture::create_depth_texture(device, sc_desc, "depth_texture");
|
|
|
|
|
|
|
|
|
|
let layout = device.create_bind_group_layout(
|
|
|
|
|
&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
label: Some("Depth Pass Layout"),
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
count: None,
|
|
|
|
|
ty: wgpu::BindingType::SampledTexture {
|
|
|
|
|
component_type: wgpu::TextureComponentType::Float,
|
|
|
|
|
multisampled: false,
|
|
|
|
|
dimension: wgpu::TextureViewDimension::D2,
|
|
|
|
|
},
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
label: Some("Depth Pass Layout"),
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
count: None,
|
|
|
|
|
ty: wgpu::BindingType::SampledTexture {
|
|
|
|
|
component_type: wgpu::TextureComponentType::Float,
|
|
|
|
|
multisampled: false,
|
|
|
|
|
dimension: wgpu::TextureViewDimension::D2,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
count: None,
|
|
|
|
|
ty: wgpu::BindingType::Sampler {
|
|
|
|
|
comparison: true,
|
|
|
|
|
},
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
count: None,
|
|
|
|
|
ty: wgpu::BindingType::Sampler { comparison: true },
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout: &layout,
|
|
|
|
@ -291,33 +312,27 @@ impl DepthPass {
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
resource: wgpu::BindingResource::Sampler(&texture.sampler),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("depth_pass.bind_group"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Depth Pass VB"),
|
|
|
|
|
contents: bytemuck::cast_slice(DEPTH_VERTICES),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let index_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Depth Pass IB"),
|
|
|
|
|
contents: bytemuck::cast_slice(DEPTH_INDICES),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Depth Pass VB"),
|
|
|
|
|
contents: bytemuck::cast_slice(DEPTH_VERTICES),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
});
|
|
|
|
|
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Depth Pass IB"),
|
|
|
|
|
contents: bytemuck::cast_slice(DEPTH_INDICES),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let pipeline_layout = device.create_pipeline_layout(
|
|
|
|
|
&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: Some("Depth Pass Pipeline Layout"),
|
|
|
|
|
bind_group_layouts: &[&layout],
|
|
|
|
|
push_constant_ranges: &[],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: Some("Depth Pass Pipeline Layout"),
|
|
|
|
|
bind_group_layouts: &[&layout],
|
|
|
|
|
push_constant_ranges: &[],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let vs_module = device.create_shader_module(wgpu::include_spirv!("challenge.vert.spv"));
|
|
|
|
|
let fs_module = device.create_shader_module(wgpu::include_spirv!("challenge.frag.spv"));
|
|
|
|
@ -342,20 +357,16 @@ impl DepthPass {
|
|
|
|
|
clamp_depth: false,
|
|
|
|
|
}),
|
|
|
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
|
color_states: &[
|
|
|
|
|
wgpu::ColorStateDescriptor {
|
|
|
|
|
format: sc_desc.format,
|
|
|
|
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
color_states: &[wgpu::ColorStateDescriptor {
|
|
|
|
|
format: sc_desc.format,
|
|
|
|
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
|
|
|
}],
|
|
|
|
|
depth_stencil_state: None,
|
|
|
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
|
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
|
|
|
vertex_buffers: &[
|
|
|
|
|
Vertex::desc(),
|
|
|
|
|
],
|
|
|
|
|
vertex_buffers: &[Vertex::desc()],
|
|
|
|
|
},
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
sample_mask: !0,
|
|
|
|
@ -363,10 +374,10 @@ impl DepthPass {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
texture,
|
|
|
|
|
texture,
|
|
|
|
|
layout,
|
|
|
|
|
bind_group,
|
|
|
|
|
vertex_buffer,
|
|
|
|
|
vertex_buffer,
|
|
|
|
|
index_buffer,
|
|
|
|
|
num_depth_indices: DEPTH_INDICES.len() as u32,
|
|
|
|
|
render_pipeline,
|
|
|
|
@ -385,7 +396,7 @@ impl DepthPass {
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
resource: wgpu::BindingResource::Sampler(&self.texture.sampler),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("depth_pass.bind_group"),
|
|
|
|
|
});
|
|
|
|
@ -393,16 +404,14 @@ impl DepthPass {
|
|
|
|
|
|
|
|
|
|
fn render(&self, frame: &wgpu::SwapChainTexture, encoder: &mut wgpu::CommandEncoder) {
|
|
|
|
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
|
|
color_attachments: &[
|
|
|
|
|
wgpu::RenderPassColorAttachmentDescriptor {
|
|
|
|
|
attachment: &frame.view,
|
|
|
|
|
resolve_target: None,
|
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
|
load: wgpu::LoadOp::Load,
|
|
|
|
|
store: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
|
|
|
|
attachment: &frame.view,
|
|
|
|
|
resolve_target: None,
|
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
|
load: wgpu::LoadOp::Load,
|
|
|
|
|
store: true,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
depth_stencil_attachment: None,
|
|
|
|
|
});
|
|
|
|
|
render_pass.set_pipeline(&self.render_pipeline);
|
|
|
|
@ -446,20 +455,24 @@ impl State {
|
|
|
|
|
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
|
|
|
|
|
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
|
|
|
|
let surface = unsafe { instance.create_surface(window) };
|
|
|
|
|
let adapter = instance.request_adapter(
|
|
|
|
|
&wgpu::RequestAdapterOptions {
|
|
|
|
|
let adapter = instance
|
|
|
|
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
|
|
|
|
power_preference: wgpu::PowerPreference::Default,
|
|
|
|
|
compatible_surface: Some(&surface),
|
|
|
|
|
},
|
|
|
|
|
).await.unwrap();
|
|
|
|
|
let (device, queue) = adapter.request_device(
|
|
|
|
|
&wgpu::DeviceDescriptor {
|
|
|
|
|
features: wgpu::Features::empty(),
|
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
|
shader_validation: true,
|
|
|
|
|
},
|
|
|
|
|
None, // Trace path
|
|
|
|
|
).await.unwrap();
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
let (device, queue) = adapter
|
|
|
|
|
.request_device(
|
|
|
|
|
&wgpu::DeviceDescriptor {
|
|
|
|
|
features: wgpu::Features::empty(),
|
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
|
shader_validation: true,
|
|
|
|
|
},
|
|
|
|
|
None, // Trace path
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let sc_desc = wgpu::SwapChainDescriptor {
|
|
|
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
|
|
|
@ -471,15 +484,11 @@ impl State {
|
|
|
|
|
let swap_chain = device.create_swap_chain(&surface, &sc_desc);
|
|
|
|
|
|
|
|
|
|
let diffuse_bytes = include_bytes!("happy-tree.png");
|
|
|
|
|
let diffuse_texture = texture::Texture::from_bytes(
|
|
|
|
|
&device,
|
|
|
|
|
&queue,
|
|
|
|
|
diffuse_bytes,
|
|
|
|
|
"happy-tree.png"
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
let texture_bind_group_layout = device.create_bind_group_layout(
|
|
|
|
|
&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
let diffuse_texture =
|
|
|
|
|
texture::Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png").unwrap();
|
|
|
|
|
|
|
|
|
|
let texture_bind_group_layout =
|
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
@ -494,32 +503,27 @@ impl State {
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
|
ty: wgpu::BindingType::Sampler {
|
|
|
|
|
comparison: false,
|
|
|
|
|
},
|
|
|
|
|
ty: wgpu::BindingType::Sampler { comparison: false },
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("texture_bind_group_layout"),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let diffuse_bind_group = device.create_bind_group(
|
|
|
|
|
&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout: &texture_bind_group_layout,
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("diffuse_bind_group"),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout: &texture_bind_group_layout,
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
|
|
|
|
},
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("diffuse_bind_group"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let camera = Camera {
|
|
|
|
|
eye: (0.0, 5.0, -10.0).into(),
|
|
|
|
@ -535,80 +539,87 @@ impl State {
|
|
|
|
|
let mut uniforms = Uniforms::new();
|
|
|
|
|
uniforms.update_view_proj(&camera);
|
|
|
|
|
|
|
|
|
|
let uniform_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Uniform Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&[uniforms]),
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let instances = (0..NUM_INSTANCES_PER_ROW).flat_map(|z| {
|
|
|
|
|
(0..NUM_INSTANCES_PER_ROW).map(move |x| {
|
|
|
|
|
let position = cgmath::Vector3 { x: x as f32, y: 0.0, z: z as f32 } - INSTANCE_DISPLACEMENT;
|
|
|
|
|
|
|
|
|
|
let rotation = if position.is_zero() {
|
|
|
|
|
// this is needed so an object at (0, 0, 0) won't get scaled to zero
|
|
|
|
|
// as Quaternions can effect scale if they're not create correctly
|
|
|
|
|
cgmath::Quaternion::from_axis_angle(cgmath::Vector3::unit_z(), cgmath::Deg(0.0))
|
|
|
|
|
} else {
|
|
|
|
|
cgmath::Quaternion::from_axis_angle(position.clone().normalize(), cgmath::Deg(45.0))
|
|
|
|
|
};
|
|
|
|
|
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Uniform Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&[uniforms]),
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Instance {
|
|
|
|
|
position, rotation,
|
|
|
|
|
}
|
|
|
|
|
let instances = (0..NUM_INSTANCES_PER_ROW)
|
|
|
|
|
.flat_map(|z| {
|
|
|
|
|
(0..NUM_INSTANCES_PER_ROW).map(move |x| {
|
|
|
|
|
let position = cgmath::Vector3 {
|
|
|
|
|
x: x as f32,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
z: z as f32,
|
|
|
|
|
} - INSTANCE_DISPLACEMENT;
|
|
|
|
|
|
|
|
|
|
let rotation = if position.is_zero() {
|
|
|
|
|
// this is needed so an object at (0, 0, 0) won't get scaled to zero
|
|
|
|
|
// as Quaternions can effect scale if they're not create correctly
|
|
|
|
|
cgmath::Quaternion::from_axis_angle(
|
|
|
|
|
cgmath::Vector3::unit_z(),
|
|
|
|
|
cgmath::Deg(0.0),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
cgmath::Quaternion::from_axis_angle(
|
|
|
|
|
position.clone().normalize(),
|
|
|
|
|
cgmath::Deg(45.0),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Instance { position, rotation }
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
let instance_data = instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
|
|
|
|
|
let instance_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Instance Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&instance_data),
|
|
|
|
|
usage: wgpu::BufferUsage::STORAGE,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Instance Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(&instance_data),
|
|
|
|
|
usage: wgpu::BufferUsage::STORAGE,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
|
|
|
dynamic: false,
|
|
|
|
|
min_binding_size: None,
|
|
|
|
|
let uniform_bind_group_layout =
|
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
|
|
|
dynamic: false,
|
|
|
|
|
min_binding_size: None,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
// NEW!
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::StorageBuffer {
|
|
|
|
|
// We don't plan on changing the size of this buffer
|
|
|
|
|
dynamic: false,
|
|
|
|
|
// The shader is not allowed to modify it's contents
|
|
|
|
|
readonly: true,
|
|
|
|
|
min_binding_size: None,
|
|
|
|
|
// NEW!
|
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
|
ty: wgpu::BindingType::StorageBuffer {
|
|
|
|
|
// We don't plan on changing the size of this buffer
|
|
|
|
|
dynamic: false,
|
|
|
|
|
// The shader is not allowed to modify it's contents
|
|
|
|
|
readonly: true,
|
|
|
|
|
min_binding_size: None,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
count: None,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("uniform_bind_group_layout"),
|
|
|
|
|
});
|
|
|
|
|
],
|
|
|
|
|
label: Some("uniform_bind_group_layout"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
|
layout: &uniform_bind_group_layout,
|
|
|
|
|
entries: &[
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 0,
|
|
|
|
|
resource: wgpu::BindingResource::Buffer(uniform_buffer.slice(..))
|
|
|
|
|
resource: wgpu::BindingResource::Buffer(uniform_buffer.slice(..)),
|
|
|
|
|
},
|
|
|
|
|
// NEW!
|
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
|
binding: 1,
|
|
|
|
|
resource: wgpu::BindingResource::Buffer(instance_buffer.slice(..))
|
|
|
|
|
resource: wgpu::BindingResource::Buffer(instance_buffer.slice(..)),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
label: Some("uniform_bind_group"),
|
|
|
|
@ -617,78 +628,64 @@ impl State {
|
|
|
|
|
let vs_module = device.create_shader_module(wgpu::include_spirv!("shader.vert.spv"));
|
|
|
|
|
let fs_module = device.create_shader_module(wgpu::include_spirv!("shader.frag.spv"));
|
|
|
|
|
|
|
|
|
|
let render_pipeline_layout = device.create_pipeline_layout(
|
|
|
|
|
&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
let render_pipeline_layout =
|
|
|
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
|
label: Some("Render Pipeline Layout"),
|
|
|
|
|
bind_group_layouts: &[
|
|
|
|
|
&texture_bind_group_layout,
|
|
|
|
|
&uniform_bind_group_layout,
|
|
|
|
|
],
|
|
|
|
|
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
|
|
|
|
|
push_constant_ranges: &[],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let render_pipeline = device.create_render_pipeline(
|
|
|
|
|
&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some("Render Pipeline"),
|
|
|
|
|
layout: Some(&render_pipeline_layout),
|
|
|
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
|
|
|
module: &vs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
},
|
|
|
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
|
|
|
module: &fs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
}),
|
|
|
|
|
rasterization_state: Some(
|
|
|
|
|
wgpu::RasterizationStateDescriptor {
|
|
|
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
|
|
|
cull_mode: wgpu::CullMode::Back,
|
|
|
|
|
depth_bias: 2, // corresponds to bilinear filtering
|
|
|
|
|
depth_bias_slope_scale: 2.0,
|
|
|
|
|
depth_bias_clamp: 0.0,
|
|
|
|
|
clamp_depth: device.features().contains(wgpu::Features::DEPTH_CLAMPING),
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
|
color_states: &[
|
|
|
|
|
wgpu::ColorStateDescriptor {
|
|
|
|
|
format: sc_desc.format,
|
|
|
|
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
|
|
|
|
format: texture::Texture::DEPTH_FORMAT,
|
|
|
|
|
depth_write_enabled: true,
|
|
|
|
|
depth_compare: wgpu::CompareFunction::Less,
|
|
|
|
|
stencil: wgpu::StencilStateDescriptor::default(),
|
|
|
|
|
}),
|
|
|
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
|
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
|
|
|
vertex_buffers: &[Vertex::desc()],
|
|
|
|
|
},
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
sample_mask: !0,
|
|
|
|
|
alpha_to_coverage_enabled: false,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
|
label: Some("Render Pipeline"),
|
|
|
|
|
layout: Some(&render_pipeline_layout),
|
|
|
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
|
|
|
module: &vs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
},
|
|
|
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
|
|
|
module: &fs_module,
|
|
|
|
|
entry_point: "main",
|
|
|
|
|
}),
|
|
|
|
|
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
|
|
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
|
|
|
cull_mode: wgpu::CullMode::Back,
|
|
|
|
|
depth_bias: 2, // corresponds to bilinear filtering
|
|
|
|
|
depth_bias_slope_scale: 2.0,
|
|
|
|
|
depth_bias_clamp: 0.0,
|
|
|
|
|
clamp_depth: device.features().contains(wgpu::Features::DEPTH_CLAMPING),
|
|
|
|
|
}),
|
|
|
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
|
color_states: &[wgpu::ColorStateDescriptor {
|
|
|
|
|
format: sc_desc.format,
|
|
|
|
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
|
|
|
}],
|
|
|
|
|
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
|
|
|
|
format: texture::Texture::DEPTH_FORMAT,
|
|
|
|
|
depth_write_enabled: true,
|
|
|
|
|
depth_compare: wgpu::CompareFunction::Less,
|
|
|
|
|
stencil: wgpu::StencilStateDescriptor::default(),
|
|
|
|
|
}),
|
|
|
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
|
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
|
|
|
vertex_buffers: &[Vertex::desc()],
|
|
|
|
|
},
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
sample_mask: !0,
|
|
|
|
|
alpha_to_coverage_enabled: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Vertex Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(VERTICES),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let index_buffer = device.create_buffer_init(
|
|
|
|
|
&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Index Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(INDICES),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Vertex Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(VERTICES),
|
|
|
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
|
|
|
});
|
|
|
|
|
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
|
|
|
label: Some("Index Buffer"),
|
|
|
|
|
contents: bytemuck::cast_slice(INDICES),
|
|
|
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
|
|
|
});
|
|
|
|
|
let num_indices = INDICES.len() as u32;
|
|
|
|
|
|
|
|
|
|
let depth_pass = DepthPass::new(&device, &sc_desc);
|
|
|
|
@ -717,7 +714,6 @@ impl State {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
|
|
|
|
self.size = new_size;
|
|
|
|
|
self.sc_desc.width = new_size.width;
|
|
|
|
@ -736,37 +732,41 @@ impl State {
|
|
|
|
|
fn update(&mut self) {
|
|
|
|
|
self.camera_controller.update_camera(&mut self.camera);
|
|
|
|
|
self.uniforms.update_view_proj(&self.camera);
|
|
|
|
|
self.queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[self.uniforms]));
|
|
|
|
|
self.queue.write_buffer(
|
|
|
|
|
&self.uniform_buffer,
|
|
|
|
|
0,
|
|
|
|
|
bytemuck::cast_slice(&[self.uniforms]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self) {
|
|
|
|
|
let frame = self.swap_chain.get_current_frame()
|
|
|
|
|
let frame = self
|
|
|
|
|
.swap_chain
|
|
|
|
|
.get_current_frame()
|
|
|
|
|
.expect("Timeout getting texture")
|
|
|
|
|
.output;
|
|
|
|
|
|
|
|
|
|
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
|
|
|
label: Some("Render Encoder"),
|
|
|
|
|
});
|
|
|
|
|
let mut encoder = self
|
|
|
|
|
.device
|
|
|
|
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
|
|
|
label: Some("Render Encoder"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
|
|
color_attachments: &[
|
|
|
|
|
wgpu::RenderPassColorAttachmentDescriptor {
|
|
|
|
|
attachment: &frame.view,
|
|
|
|
|
resolve_target: None,
|
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
|
load: wgpu::LoadOp::Clear(
|
|
|
|
|
wgpu::Color {
|
|
|
|
|
r: 0.1,
|
|
|
|
|
g: 0.2,
|
|
|
|
|
b: 0.3,
|
|
|
|
|
a: 1.0,
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
store: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
|
|
|
|
attachment: &frame.view,
|
|
|
|
|
resolve_target: None,
|
|
|
|
|
ops: wgpu::Operations {
|
|
|
|
|
load: wgpu::LoadOp::Clear(wgpu::Color {
|
|
|
|
|
r: 0.1,
|
|
|
|
|
g: 0.2,
|
|
|
|
|
b: 0.3,
|
|
|
|
|
a: 1.0,
|
|
|
|
|
}),
|
|
|
|
|
store: true,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor {
|
|
|
|
|
attachment: &self.depth_pass.texture.view,
|
|
|
|
|
depth_ops: Some(wgpu::Operations {
|
|
|
|
@ -794,9 +794,7 @@ impl State {
|
|
|
|
|
fn main() {
|
|
|
|
|
env_logger::init();
|
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
|
.build(&event_loop)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
|
|
|
|
|
|
|
|
|
use futures::executor::block_on;
|
|
|
|
|
|
|
|
|
@ -808,30 +806,27 @@ fn main() {
|
|
|
|
|
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 {
|
|
|
|
|
} 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) => {
|
|
|
|
|
state.resize(*physical_size);
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
|
// new_inner_size is &mut so w have to dereference it twice
|
|
|
|
|
state.resize(**new_inner_size);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::Resized(physical_size) => {
|
|
|
|
|
state.resize(*physical_size);
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
|
// new_inner_size is &mut so w have to dereference it twice
|
|
|
|
|
state.resize(**new_inner_size);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::RedrawRequested(_) => {
|
|
|
|
|