Support choosing display mode
This commit is contained in:
+65
-42
@@ -1,4 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use egui::Widget;
|
||||
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, RenderPipeline};
|
||||
@@ -7,6 +7,7 @@ use crate::graphics::TextureSink;
|
||||
|
||||
pub struct GameScreen {
|
||||
bind_group: Arc<BindGroup>,
|
||||
pub display_mode: DisplayMode,
|
||||
}
|
||||
|
||||
impl GameScreen {
|
||||
@@ -52,50 +53,58 @@ impl GameScreen {
|
||||
bind_group_layouts: &[&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: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: wgpu::TextureFormat::Bgra8Unorm,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multiview: None,
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let create_render_pipeline = |entry_point: &str| {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("render pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point,
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: wgpu::TextureFormat::Bgra8Unorm,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multiview: None,
|
||||
cache: None,
|
||||
})
|
||||
};
|
||||
|
||||
let mut render_pipelines = HashMap::new();
|
||||
render_pipelines.insert(DisplayMode::Anaglyph, create_render_pipeline("fs_anaglyph"));
|
||||
render_pipelines.insert(DisplayMode::LeftEye, create_render_pipeline("fs_lefteye"));
|
||||
render_pipelines.insert(DisplayMode::RightEye, create_render_pipeline("fs_righteye"));
|
||||
|
||||
render_state
|
||||
.renderer
|
||||
.write()
|
||||
.callback_resources
|
||||
.insert(SharedGameScreenResources {
|
||||
pipeline: render_pipeline,
|
||||
render_pipelines,
|
||||
bind_group_layout,
|
||||
});
|
||||
}
|
||||
@@ -144,19 +153,21 @@ impl GameScreen {
|
||||
(
|
||||
Self {
|
||||
bind_group: Arc::new(bind_group),
|
||||
display_mode: DisplayMode::Anaglyph,
|
||||
},
|
||||
sink,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for &GameScreen {
|
||||
impl Widget for &mut GameScreen {
|
||||
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
|
||||
let response = ui.allocate_rect(ui.clip_rect(), egui::Sense::hover());
|
||||
let callback = egui_wgpu::Callback::new_paint_callback(
|
||||
response.rect,
|
||||
GameScreenCallback {
|
||||
bind_group: self.bind_group.clone(),
|
||||
display_mode: self.display_mode,
|
||||
},
|
||||
);
|
||||
ui.painter().add(callback);
|
||||
@@ -166,6 +177,7 @@ impl Widget for &GameScreen {
|
||||
|
||||
struct GameScreenCallback {
|
||||
bind_group: Arc<BindGroup>,
|
||||
display_mode: DisplayMode,
|
||||
}
|
||||
|
||||
impl egui_wgpu::CallbackTrait for GameScreenCallback {
|
||||
@@ -186,7 +198,11 @@ impl egui_wgpu::CallbackTrait for GameScreenCallback {
|
||||
let h = height.min(width / aspect_ratio);
|
||||
let x = left + (width - w) / 2.0;
|
||||
let y = top + (height - h) / 2.0;
|
||||
render_pass.set_pipeline(&resources.pipeline);
|
||||
let pipeline = resources
|
||||
.render_pipelines
|
||||
.get(&self.display_mode)
|
||||
.unwrap_or_else(|| panic!("Unrecognized display mode {:?}", self.display_mode));
|
||||
render_pass.set_pipeline(pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.set_viewport(x, y, w, h, 0.0, 1.0);
|
||||
render_pass.draw(0..6, 0..1);
|
||||
@@ -194,7 +210,7 @@ impl egui_wgpu::CallbackTrait for GameScreenCallback {
|
||||
}
|
||||
|
||||
struct SharedGameScreenResources {
|
||||
pipeline: RenderPipeline,
|
||||
render_pipelines: HashMap<DisplayMode, RenderPipeline>,
|
||||
bind_group_layout: BindGroupLayout,
|
||||
}
|
||||
|
||||
@@ -204,3 +220,10 @@ struct Colors {
|
||||
left: [f32; 4],
|
||||
right: [f32; 4],
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
pub enum DisplayMode {
|
||||
Anaglyph,
|
||||
LeftEye,
|
||||
RightEye,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user