Skip to content

Commit

Permalink
feat: choose view index with right arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
philpax committed Oct 2, 2022
1 parent 97a6c45 commit 4197245
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/blit.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var blit_texture: texture_2d_array<f32>;
@group(0)@binding(1)
var blit_sampler: sampler;

var<push_constant> view_index: u32;

@vertex
fn blit_vs_main(model: BlitVertexInput) -> BlitVertexOutput {
var out: BlitVertexOutput;
Expand All @@ -23,5 +25,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, select(0, 1, in.uv_coords.x < 0.5));
return textureSample(blit_texture, blit_sampler, in.uv_coords, i32(view_index));
}
11 changes: 10 additions & 1 deletion src/blit_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ impl BlitState {
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
push_constant_ranges: &[wgpu::PushConstantRange {
stages: wgpu::ShaderStages::FRAGMENT,
range: 0..4,
}],
});
let vertex_buffer_layout = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<BlitVertex>() as _,
Expand Down Expand Up @@ -165,6 +168,7 @@ impl BlitState {
&self,
encoder: &mut wgpu::CommandEncoder,
output_view: &wgpu::TextureView,
view_index: u32,
) {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
Expand All @@ -181,6 +185,11 @@ impl BlitState {
rpass.set_pipeline(&self.render_pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
rpass.set_push_constants(
wgpu::ShaderStages::FRAGMENT,
0,
bytemuck::cast_slice(&[view_index]),
);
rpass.draw(0..6, 0..1);
}
}
35 changes: 27 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Context;
use glam::{vec3, vec4, Quat};
use wgpu::util::DeviceExt;
use winit::{
event::{Event, WindowEvent},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};

Expand Down Expand Up @@ -30,8 +30,11 @@ pub struct WgpuState {
}

fn main() -> anyhow::Result<()> {
let wgpu_features = wgpu::Features::MULTIVIEW;
let wgpu_limits = wgpu::Limits::default();
let wgpu_features = wgpu::Features::MULTIVIEW | wgpu::Features::PUSH_CONSTANTS;
let wgpu_limits = wgpu::Limits {
max_push_constant_size: 4,
..Default::default()
};

let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop)?;
Expand Down Expand Up @@ -84,6 +87,7 @@ fn main() -> anyhow::Result<()> {

let start_time = std::time::Instant::now();
let (mut fps_timer, mut fps_count) = (std::time::Instant::now(), 0);
let mut view_index = 0;
event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
Expand Down Expand Up @@ -120,14 +124,29 @@ fn main() -> anyhow::Result<()> {
// On macos the window needs to be redrawn manually after resizing
window.request_redraw();
}
Event::MainEventsCleared => {
window.request_redraw();
cleared = true;
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Right),
state: ElementState::Released,
..
},
..
},
..
} => {
view_index = (view_index + 1) % 2;
}
Event::MainEventsCleared => {
window.request_redraw();
cleared = true;
}
_ => {}
}

Expand Down Expand Up @@ -155,7 +174,7 @@ fn main() -> anyhow::Result<()> {
let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
blit_state.encode_draw_pass(&mut encoder, &view);
blit_state.encode_draw_pass(&mut encoder, &view, view_index);

let time_since_start = start_time.elapsed().as_secs_f32();
camera_state.data.eye.z = time_since_start.cos() - 1.0;
Expand Down

0 comments on commit 4197245

Please sign in to comment.