This commit is contained in:
Alex Janka 2023-09-28 17:28:25 +10:00
parent 14046462d8
commit a39b31dc33
6 changed files with 53 additions and 28 deletions

1
Cargo.lock generated
View file

@ -1103,6 +1103,7 @@ dependencies = [
name = "gb-emu" name = "gb-emu"
version = "0.3.3" version = "0.3.3"
dependencies = [ dependencies = [
"ash",
"bytemuck", "bytemuck",
"clap", "clap",
"cpal", "cpal",

View file

@ -4,7 +4,10 @@ version = "0.3.3"
edition = "2021" edition = "2021"
[features] [features]
default = [] # default = ["vulkan"]
default = ["pixels"]
pixels = ["dep:pixels"]
vulkan = ["dep:ash"]
camera = ["dep:nokhwa", "dep:send_wrapper"] camera = ["dep:nokhwa", "dep:send_wrapper"]
[dependencies] [dependencies]
@ -20,5 +23,6 @@ nokhwa = { version = "0.10.3", features = [
send_wrapper = { version = "0.6.0", optional = true } send_wrapper = { version = "0.6.0", optional = true }
winit = "0.28" winit = "0.28"
winit_input_helper = "0.14" winit_input_helper = "0.14"
pixels = "0.12"
bytemuck = "1.13" bytemuck = "1.13"
pixels = { version = "0.12", optional = true }
ash = { version = "0.37", optional = true }

View file

@ -24,6 +24,11 @@ mod camera;
mod debug; mod debug;
mod window; 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 /// Gameboy (DMG/CGB) emulator
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]

View file

@ -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()
}

View file

View file

@ -8,7 +8,6 @@ use gilrs::{
ff::{BaseEffect, BaseEffectType, EffectBuilder, Replay, Ticks}, ff::{BaseEffect, BaseEffectType, EffectBuilder, Replay, Ticks},
Button, Gilrs, Button, Gilrs,
}; };
use pixels::{Pixels, SurfaceTexture};
use winit::{ use winit::{
dpi::PhysicalSize, dpi::PhysicalSize,
event::{Event, VirtualKeyCode, WindowEvent}, event::{Event, VirtualKeyCode, WindowEvent},
@ -18,15 +17,17 @@ use winit::{
}; };
use winit_input_helper::WinitInputHelper; 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 { pub struct WindowInfo {
id: WindowId, id: WindowId,
data: Arc<Mutex<WindowData>>, data: Arc<Mutex<WindowData>>,
} }
pub struct WindowData {
pixels: Pixels,
}
pub struct WindowManager { pub struct WindowManager {
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
windows: HashMap<WindowId, Arc<Mutex<WindowData>>>, windows: HashMap<WindowId, Arc<Mutex<WindowData>>>,
@ -113,14 +114,7 @@ impl WindowRenderer {
let real_factor = (window.scale_factor() * factor as f64) as usize; let real_factor = (window.scale_factor() * factor as f64) as usize;
let pixels = { let data = Arc::new(Mutex::new(WindowData::new(real_factor as u32, &window)));
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 info = WindowInfo { let info = WindowInfo {
id: window.id(), id: window.id(),
data: data.clone(), data: data.clone(),
@ -156,7 +150,7 @@ impl Renderer<[u8; 4]> for WindowRenderer {
self.window.set_inner_size(PhysicalSize::new(w, h)); self.window.set_inner_size(PhysicalSize::new(w, h));
if let Ok(mut data) = self.data.lock() { if let Ok(mut data) = self.data.lock() {
data.pixels = new_pixels( data.resize(
width as u32, width as u32,
height as u32, height as u32,
self.real_factor 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()
}