From a39b31dc333af0fc905fc889559a0746e553b07a Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Thu, 28 Sep 2023 17:28:25 +1000 Subject: [PATCH] refactor --- Cargo.lock | 1 + gb-emu/Cargo.toml | 8 ++++++-- gb-emu/src/main.rs | 5 +++++ gb-emu/src/renderer/pixels.rs | 33 +++++++++++++++++++++++++++++++++ gb-emu/src/renderer/vulkan.rs | 0 gb-emu/src/window.rs | 34 ++++++++-------------------------- 6 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 gb-emu/src/renderer/pixels.rs create mode 100644 gb-emu/src/renderer/vulkan.rs diff --git a/Cargo.lock b/Cargo.lock index 48a1aa4..be066b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,6 +1103,7 @@ dependencies = [ name = "gb-emu" version = "0.3.3" dependencies = [ + "ash", "bytemuck", "clap", "cpal", diff --git a/gb-emu/Cargo.toml b/gb-emu/Cargo.toml index 29ceb9b..cb6d4e1 100644 --- a/gb-emu/Cargo.toml +++ b/gb-emu/Cargo.toml @@ -4,7 +4,10 @@ version = "0.3.3" edition = "2021" [features] -default = [] +# default = ["vulkan"] +default = ["pixels"] +pixels = ["dep:pixels"] +vulkan = ["dep:ash"] camera = ["dep:nokhwa", "dep:send_wrapper"] [dependencies] @@ -20,5 +23,6 @@ nokhwa = { version = "0.10.3", features = [ send_wrapper = { version = "0.6.0", optional = true } winit = "0.28" winit_input_helper = "0.14" -pixels = "0.12" bytemuck = "1.13" +pixels = { version = "0.12", optional = true } +ash = { version = "0.37", optional = true } diff --git a/gb-emu/src/main.rs b/gb-emu/src/main.rs index 84fa2f5..a81a826 100644 --- a/gb-emu/src/main.rs +++ b/gb-emu/src/main.rs @@ -24,6 +24,11 @@ mod camera; mod debug; mod window; +#[cfg(all(feature = "vulkan", feature = "pixels"))] +compile_error!("select only one rendering backend!"); +#[cfg(all(not(feature = "vulkan"), not(feature = "pixels")))] +compile_error!("select one rendering backend!"); + /// Gameboy (DMG/CGB) emulator #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] diff --git a/gb-emu/src/renderer/pixels.rs b/gb-emu/src/renderer/pixels.rs new file mode 100644 index 0000000..1eda6c9 --- /dev/null +++ b/gb-emu/src/renderer/pixels.rs @@ -0,0 +1,33 @@ +use pixels::{Pixels, SurfaceTexture}; +use winit::window::Window; +pub struct WindowData { + pub pixels: Pixels, +} + +impl WindowData { + pub fn new(factor: u32, window: &Window) -> Self { + let pixels = { + let window_size = window.inner_size(); + let width = window_size.width / factor; + let height = window_size.height / factor; + new_pixels(width, height, factor, window) + }; + Self { pixels } + } + + pub fn resize(&mut self, width: u32, height: u32, factor: u32, window: &Window) { + self.pixels = new_pixels(width, height, factor, window); + } +} + +fn new_pixels(width: u32, height: u32, scaling: u32, window: &Window) -> Pixels { + let surface_texture: SurfaceTexture<'_, Window> = + SurfaceTexture::new(width * scaling, height * scaling, window); + pixels::PixelsBuilder::new(width, height, surface_texture) + .request_adapter_options(pixels::wgpu::RequestAdapterOptionsBase { + power_preference: pixels::wgpu::PowerPreference::HighPerformance, + ..pixels::wgpu::RequestAdapterOptionsBase::default() + }) + .build() + .unwrap() +} diff --git a/gb-emu/src/renderer/vulkan.rs b/gb-emu/src/renderer/vulkan.rs new file mode 100644 index 0000000..e69de29 diff --git a/gb-emu/src/window.rs b/gb-emu/src/window.rs index 142ee91..13a778d 100644 --- a/gb-emu/src/window.rs +++ b/gb-emu/src/window.rs @@ -8,7 +8,6 @@ use gilrs::{ ff::{BaseEffect, BaseEffectType, EffectBuilder, Replay, Ticks}, Button, Gilrs, }; -use pixels::{Pixels, SurfaceTexture}; use winit::{ dpi::PhysicalSize, event::{Event, VirtualKeyCode, WindowEvent}, @@ -18,15 +17,17 @@ use winit::{ }; use winit_input_helper::WinitInputHelper; +#[cfg_attr(feature = "pixels", path = "renderer/pixels.rs")] +#[cfg_attr(feature = "vulkan", path = "renderer/vulkan.rs")] +mod renderer; + +use renderer::WindowData; + pub struct WindowInfo { id: WindowId, data: Arc>, } -pub struct WindowData { - pixels: Pixels, -} - pub struct WindowManager { event_loop: EventLoop<()>, windows: HashMap>>, @@ -113,14 +114,7 @@ impl WindowRenderer { let real_factor = (window.scale_factor() * factor as f64) as usize; - let pixels = { - let window_size = window.inner_size(); - let width = window_size.width / (real_factor as u32); - let height = window_size.height / (real_factor as u32); - new_pixels(width, height, real_factor as u32, &window) - }; - - let data = Arc::new(Mutex::new(WindowData { pixels })); + let data = Arc::new(Mutex::new(WindowData::new(real_factor as u32, &window))); let info = WindowInfo { id: window.id(), data: data.clone(), @@ -156,7 +150,7 @@ impl Renderer<[u8; 4]> for WindowRenderer { self.window.set_inner_size(PhysicalSize::new(w, h)); if let Ok(mut data) = self.data.lock() { - data.pixels = new_pixels( + data.resize( width as u32, height as u32, self.real_factor as u32, @@ -258,15 +252,3 @@ impl Renderer<[u8; 4]> for WindowRenderer { } } } - -fn new_pixels(width: u32, height: u32, scaling: u32, window: &Window) -> Pixels { - let surface_texture: SurfaceTexture<'_, Window> = - SurfaceTexture::new(width * scaling, height * scaling, window); - pixels::PixelsBuilder::new(width, height, surface_texture) - .request_adapter_options(pixels::wgpu::RequestAdapterOptionsBase { - power_preference: pixels::wgpu::PowerPreference::HighPerformance, - ..pixels::wgpu::RequestAdapterOptionsBase::default() - }) - .build() - .unwrap() -}