use std::{collections::HashMap, sync::Arc}; use egui::{Color32, Rgba, Vec2, Widget}; use serde::{Deserialize, Serialize}; use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, Buffer, RenderPipeline}; use crate::graphics::TextureSink; pub struct GameScreen { bind_group: Arc, color_buffer: Arc, display_mode: DisplayMode, colors: Colors, } impl GameScreen { fn init_pipeline(render_state: &egui_wgpu::RenderState) { let device = &render_state.device; let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("texture bind group layout"), entries: &[ wgpu::BindGroupLayoutEntry { binding: 0, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: true }, view_dimension: wgpu::TextureViewDimension::D2, multisampled: false, }, count: None, }, wgpu::BindGroupLayoutEntry { binding: 1, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), count: None, }, wgpu::BindGroupLayoutEntry { binding: 2, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: None, }, count: None, }, ], }); let shader = device.create_shader_module(wgpu::include_wgsl!("../game.wgsl")); let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("render pipeline layout"), bind_group_layouts: &[&bind_group_layout], push_constant_ranges: &[], }); 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: Some("vs_main"), buffers: &[], compilation_options: wgpu::PipelineCompilationOptions::default(), }, fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some(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_pipelines.insert( DisplayMode::SideBySide, create_render_pipeline("fs_sidebyside"), ); render_state .renderer .write() .callback_resources .insert(SharedGameScreenResources { render_pipelines, bind_group_layout, }); } pub fn init(render_state: &egui_wgpu::RenderState) -> (Self, TextureSink) { Self::init_pipeline(render_state); let device = &render_state.device; let queue = &render_state.queue; let (sink, texture_view) = TextureSink::new(device, queue.clone()); let sampler = device.create_sampler(&wgpu::SamplerDescriptor::default()); let colors = Colors::new( Color32::from_rgb(0xff, 0x00, 0x00), Color32::from_rgb(0x00, 0xc6, 0xf0), ); let color_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("colors"), contents: bytemuck::bytes_of(&colors), usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, }); let renderer = render_state.renderer.read(); let resources: &SharedGameScreenResources = renderer.callback_resources.get().unwrap(); let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("bind group"), layout: &resources.bind_group_layout, entries: &[ wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&texture_view), }, wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler), }, wgpu::BindGroupEntry { binding: 2, resource: color_buffer.as_entire_binding(), }, ], }); ( Self { bind_group: Arc::new(bind_group), color_buffer: Arc::new(color_buffer), display_mode: DisplayMode::Anaglyph, colors, }, sink, ) } pub fn update(&mut self, display_mode: DisplayMode, colors: [Color32; 2]) { self.display_mode = display_mode; self.colors = Colors::new(colors[0], colors[1]); } } 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(), color_buffer: self.color_buffer.clone(), display_mode: self.display_mode, colors: self.colors, }, ); ui.painter().add(callback); response } } struct GameScreenCallback { bind_group: Arc, color_buffer: Arc, display_mode: DisplayMode, colors: Colors, } impl egui_wgpu::CallbackTrait for GameScreenCallback { fn prepare( &self, _device: &wgpu::Device, queue: &wgpu::Queue, _screen_descriptor: &egui_wgpu::ScreenDescriptor, _egui_encoder: &mut wgpu::CommandEncoder, _callback_resources: &mut egui_wgpu::CallbackResources, ) -> Vec { queue.write_buffer(&self.color_buffer, 0, bytemuck::bytes_of(&self.colors)); vec![] } fn paint( &self, info: egui::PaintCallbackInfo, render_pass: &mut wgpu::RenderPass<'static>, callback_resources: &egui_wgpu::CallbackResources, ) { let resources: &SharedGameScreenResources = callback_resources.get().unwrap(); let viewport = info.viewport_in_pixels(); let left = viewport.left_px as f32; let top = viewport.top_px as f32; let width = viewport.width_px as f32; let height = viewport.height_px as f32; let aspect_ratio = { let proportions = self.display_mode.proportions(); proportions.x / proportions.y }; let w = width.min(height * aspect_ratio); let h = height.min(width / aspect_ratio); let x = left + (width - w) / 2.0; let y = top + (height - h) / 2.0; 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, Some(self.bind_group.as_ref()), &[]); render_pass.set_viewport(x, y, w, h, 0.0, 1.0); render_pass.draw(0..6, 0..1); } } struct SharedGameScreenResources { render_pipelines: HashMap, bind_group_layout: BindGroupLayout, } #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] #[repr(C)] struct Colors { left: Rgba, right: Rgba, } impl Colors { fn new(left: Color32, right: Color32) -> Self { Self { left: left.into(), right: right.into(), } } } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub enum DisplayMode { Anaglyph, LeftEye, RightEye, SideBySide, } impl DisplayMode { pub fn proportions(self) -> Vec2 { match self { Self::SideBySide => Vec2::new(768.0, 224.0), _ => Vec2::new(384.0, 224.0), } } }