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;
@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);
return colors.left * brt[0] + colors.right * brt[1];
}

View File

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

View File

@ -8,12 +8,16 @@ use egui::{
};
use winit::event_loop::EventLoopProxy;
use super::{game_screen::GameScreen, AppWindow};
use super::{
game_screen::{DisplayMode, GameScreen},
AppWindow,
};
pub struct GameWindow {
client: EmulatorClient,
proxy: EventLoopProxy<UserEvent>,
sim_id: SimId,
display_mode: DisplayMode,
screen: Option<GameScreen>,
}
@ -23,6 +27,7 @@ impl GameWindow {
client,
proxy,
sim_id,
display_mode: DisplayMode::Anaglyph,
screen: None,
}
}
@ -61,6 +66,7 @@ impl GameWindow {
}
});
ui.menu_button("Video", |ui| {
ui.menu_button("Screen Size", |ui| {
let current_dims = ctx.input(|i| i.viewport().inner_rect.unwrap());
let current_dims = current_dims.max - current_dims.min;
@ -77,6 +83,28 @@ impl GameWindow {
}
}
});
ui.menu_button("Display Mode", |ui| {
if ui
.selectable_option(&mut self.display_mode, DisplayMode::Anaglyph, "Anaglyph")
.clicked()
{
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| {
let p1_enabled = self.client.is_audio_enabled(SimId::Player1);
let p2_enabled = self.client.is_audio_enabled(SimId::Player2);
@ -146,7 +174,8 @@ impl AppWindow for GameWindow {
});
let frame = Frame::central_panel(&ctx.style()).fill(Color32::BLACK);
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);
}
});
@ -168,6 +197,18 @@ impl AppWindow for GameWindow {
trait UiExt {
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 {

View File

@ -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,7 +53,9 @@ impl GameScreen {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
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 {
@ -63,7 +66,7 @@ impl GameScreen {
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point,
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Bgra8Unorm,
blend: Some(wgpu::BlendState::REPLACE),
@ -88,14 +91,20 @@ impl GameScreen {
},
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,
}