Support choosing display mode

This commit is contained in:
Simon Gellis 2024-11-30 23:14:01 -05:00
parent 2cef67b129
commit 4e42179ef3
4 changed files with 131 additions and 55 deletions

View File

@ -49,7 +49,19 @@ struct Colors {
var<uniform> colors: Colors; var<uniform> colors: Colors;
@fragment @fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { fn fs_lefteye(in: VertexOutput) -> @location(0) vec4<f32> {
let brt = textureSample(u_texture, u_sampler, in.tex_coords);
return colors.left * brt[0];
}
@fragment
fn fs_righteye(in: VertexOutput) -> @location(0) vec4<f32> {
let brt = textureSample(u_texture, u_sampler, in.tex_coords);
return colors.right * brt[1];
}
@fragment
fn fs_anaglyph(in: VertexOutput) -> @location(0) vec4<f32> {
let brt = textureSample(u_texture, u_sampler, in.tex_coords); let brt = textureSample(u_texture, u_sampler, in.tex_coords);
return colors.left * brt[0] + colors.right * brt[1]; return colors.left * brt[0] + colors.right * brt[1];
} }

View File

@ -196,7 +196,7 @@ impl Viewport {
); );
let ctx = Context::default(); let ctx = Context::default();
let mut fonts = FontDefinitions::empty(); let mut fonts = FontDefinitions::default();
fonts.font_data.insert( fonts.font_data.insert(
"Selawik".into(), "Selawik".into(),
FontData::from_static(include_bytes!("../assets/selawik.ttf")), FontData::from_static(include_bytes!("../assets/selawik.ttf")),

View File

@ -8,12 +8,16 @@ use egui::{
}; };
use winit::event_loop::EventLoopProxy; use winit::event_loop::EventLoopProxy;
use super::{game_screen::GameScreen, AppWindow}; use super::{
game_screen::{DisplayMode, GameScreen},
AppWindow,
};
pub struct GameWindow { pub struct GameWindow {
client: EmulatorClient, client: EmulatorClient,
proxy: EventLoopProxy<UserEvent>, proxy: EventLoopProxy<UserEvent>,
sim_id: SimId, sim_id: SimId,
display_mode: DisplayMode,
screen: Option<GameScreen>, screen: Option<GameScreen>,
} }
@ -23,6 +27,7 @@ impl GameWindow {
client, client,
proxy, proxy,
sim_id, sim_id,
display_mode: DisplayMode::Anaglyph,
screen: None, screen: None,
} }
} }
@ -61,21 +66,44 @@ impl GameWindow {
} }
}); });
ui.menu_button("Video", |ui| { ui.menu_button("Video", |ui| {
let current_dims = ctx.input(|i| i.viewport().inner_rect.unwrap()); ui.menu_button("Screen Size", |ui| {
let current_dims = current_dims.max - current_dims.min; let current_dims = ctx.input(|i| i.viewport().inner_rect.unwrap());
let current_dims = current_dims.max - current_dims.min;
for scale in 1..=4 { for scale in 1..=4 {
let label = format!("x{scale}"); let label = format!("x{scale}");
let scale = scale as f32; let scale = scale as f32;
let dims = (384.0 * scale, 224.0 * scale + 22.0).into(); let dims = (384.0 * scale, 224.0 * scale + 22.0).into();
if ui
.selectable_button((current_dims - dims).length() < 1.0, label)
.clicked()
{
ctx.send_viewport_cmd(ViewportCommand::InnerSize(dims));
ui.close_menu();
}
}
});
ui.menu_button("Display Mode", |ui| {
if ui if ui
.selectable_button((current_dims - dims).length() < 1.0, label) .selectable_option(&mut self.display_mode, DisplayMode::Anaglyph, "Anaglyph")
.clicked() .clicked()
{ {
ctx.send_viewport_cmd(ViewportCommand::InnerSize(dims));
ui.close_menu(); ui.close_menu();
} }
} if ui
.selectable_option(&mut self.display_mode, DisplayMode::LeftEye, "Left Eye")
.clicked()
{
ui.close_menu();
}
if ui
.selectable_option(&mut self.display_mode, DisplayMode::RightEye, "Right Eye")
.clicked()
{
ui.close_menu();
}
});
}); });
ui.menu_button("Audio", |ui| { ui.menu_button("Audio", |ui| {
let p1_enabled = self.client.is_audio_enabled(SimId::Player1); let p1_enabled = self.client.is_audio_enabled(SimId::Player1);
@ -146,7 +174,8 @@ impl AppWindow for GameWindow {
}); });
let frame = Frame::central_panel(&ctx.style()).fill(Color32::BLACK); let frame = Frame::central_panel(&ctx.style()).fill(Color32::BLACK);
CentralPanel::default().frame(frame).show(ctx, |ui| { CentralPanel::default().frame(frame).show(ctx, |ui| {
if let Some(screen) = self.screen.as_ref() { if let Some(screen) = self.screen.as_mut() {
screen.display_mode = self.display_mode;
ui.add(screen); ui.add(screen);
} }
}); });
@ -168,6 +197,18 @@ impl AppWindow for GameWindow {
trait UiExt { trait UiExt {
fn selectable_button(&mut self, selected: bool, text: impl Into<WidgetText>) -> Response; fn selectable_button(&mut self, selected: bool, text: impl Into<WidgetText>) -> Response;
fn selectable_option<T: Eq>(
&mut self,
current_value: &mut T,
selected_value: T,
text: impl Into<WidgetText>,
) -> Response {
let response = self.selectable_button(*current_value == selected_value, text);
if response.clicked() {
*current_value = selected_value;
}
response
}
} }
impl UiExt for Ui { impl UiExt for Ui {

View File

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