Skip to content

Commit

Permalink
feat: multiview rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
philpax committed Oct 2, 2022
1 parent 1902edc commit 4efbf52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/blit.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct BlitVertexOutput {
}

@group(0) @binding(0)
var blit_texture: texture_2d<f32>;
var blit_texture: texture_2d_array<f32>;
@group(0)@binding(1)
var blit_sampler: sampler;

Expand All @@ -23,5 +23,5 @@ fn blit_vs_main(model: BlitVertexInput) -> BlitVertexOutput {

@fragment
fn blit_fs_main(in: BlitVertexOutput) -> @location(0) vec4<f32> {
return textureSample(blit_texture, blit_sampler, in.uv_coords);
return textureSample(blit_texture, blit_sampler, in.uv_coords, select(0, 1, in.uv_coords.x < 0.5));
}
54 changes: 37 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Context;
use glam::{vec3, vec4, Mat4, Quat, Vec3, Vec4};
use std::borrow::Cow;
use std::{borrow::Cow, num::NonZeroU32};
use wgpu::util::DeviceExt;
use winit::{
event::{Event, WindowEvent},
Expand Down Expand Up @@ -58,7 +58,7 @@ fn main() -> anyhow::Result<()> {
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera buffer"),
contents: bytemuck::cast_slice(&camera.to_view_proj_matrix().to_cols_array()),
contents: bytemuck::cast_slice(&camera.to_view_proj_matrices()),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let camera_bind_group_layout =
Expand Down Expand Up @@ -182,7 +182,7 @@ fn main() -> anyhow::Result<()> {
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState::default(),
multiview: None,
multiview: NonZeroU32::new(2),
});

let mut config = wgpu::SurfaceConfiguration {
Expand Down Expand Up @@ -216,7 +216,7 @@ fn main() -> anyhow::Result<()> {
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
view_dimension: wgpu::TextureViewDimension::D2Array,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
},
count: None,
Expand Down Expand Up @@ -303,13 +303,13 @@ fn main() -> anyhow::Result<()> {
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Blit Vertex Buffer"),
contents: bytemuck::cast_slice(&[
BlitVertex::new(vec3(1.0, 1.0, 0.0), [1.0, 1.0]),
BlitVertex::new(vec3(-1.0, 1.0, 0.0), [0.0, 1.0]),
BlitVertex::new(vec3(-1.0, -1.0, 0.0), [0.0, 0.0]),
BlitVertex::new(vec3(1.0, 1.0, 0.0), [1.0, 0.0]),
BlitVertex::new(vec3(-1.0, 1.0, 0.0), [0.0, 0.0]),
BlitVertex::new(vec3(-1.0, -1.0, 0.0), [0.0, 1.0]),
//
BlitVertex::new(vec3(1.0, -1.0, 0.0), [1.0, 0.0]),
BlitVertex::new(vec3(1.0, 1.0, 0.0), [1.0, 1.0]),
BlitVertex::new(vec3(-1.0, -1.0, 0.0), [0.0, 0.0]),
BlitVertex::new(vec3(1.0, -1.0, 0.0), [1.0, 1.0]),
BlitVertex::new(vec3(1.0, 1.0, 0.0), [1.0, 0.0]),
BlitVertex::new(vec3(-1.0, -1.0, 0.0), [0.0, 1.0]),
]),
usage: wgpu::BufferUsages::VERTEX,
});
Expand Down Expand Up @@ -439,7 +439,7 @@ fn main() -> anyhow::Result<()> {
wgpu_state.queue.write_buffer(
&camera_buffer,
0,
bytemuck::cast_slice(&camera.to_view_proj_matrix().to_cols_array()),
bytemuck::cast_slice(&camera.to_view_proj_matrices()),
);

instances[0].1 = Quat::from_rotation_y(time_since_start / std::f32::consts::PI);
Expand Down Expand Up @@ -480,7 +480,7 @@ fn create_render_target_texture(
size: wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
depth_or_array_layers: 2,
},
mip_level_count: 1,
sample_count: 1,
Expand All @@ -490,7 +490,11 @@ fn create_render_target_texture(
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::TEXTURE_BINDING,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
array_layer_count: NonZeroU32::new(2),
..Default::default()
});
(texture, view)
}

Expand All @@ -503,15 +507,19 @@ fn create_depth_texture(
size: wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
depth_or_array_layers: 2,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let view = texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
array_layer_count: NonZeroU32::new(2),
..Default::default()
});
(texture, view)
}

Expand Down Expand Up @@ -608,10 +616,22 @@ struct PerspectiveCamera {
z_far: f32,
}
impl PerspectiveCamera {
fn to_view_proj_matrix(&self) -> Mat4 {
fn to_view_proj_matrices(&self) -> Vec<f32> {
let ipd = 68.3 / 1_000.0;
let offset = vec4(ipd / 2.0, 0.0, 0.0, 0.0);

let view = Mat4::look_at_rh(self.eye, self.target, self.up);
let proj = Mat4::perspective_rh(self.fov_y_rad, self.aspect_ratio, self.z_near, self.z_far);

proj * view
let mut view_l = view;
view_l.w_axis += view * -offset;

let mut view_r = view;
view_r.w_axis += view * offset;
[
(proj * view_l).to_cols_array(),
(proj * view_r).to_cols_array(),
]
.concat()
}
}
6 changes: 4 additions & 2 deletions src/triangle.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@group(0) @binding(0)
var<uniform> view_projection_matrix: mat4x4<f32>;
var<uniform> view_projection_matrix: array<mat4x4<f32>, 2>;


struct VertexInput {
@location(0) position: vec3<f32>,
Expand All @@ -21,6 +22,7 @@ struct VertexOutput {
fn vs_main(
model: VertexInput,
instance: InstanceInput,
@builtin(view_index) view_index: i32,
) -> VertexOutput {
let model_matrix = mat4x4<f32>(
instance.model_matrix_0,
Expand All @@ -30,7 +32,7 @@ fn vs_main(
);

var out: VertexOutput;
out.position = view_projection_matrix * model_matrix * vec4<f32>(model.position, 1.0);
out.position = view_projection_matrix[view_index] * model_matrix * vec4<f32>(model.position, 1.0);
out.color = model.color;
return out;
}
Expand Down

0 comments on commit 4efbf52

Please sign in to comment.