From 82992436d814412b28c5d5e0e1e2d9f8c7901751 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Wed, 4 Oct 2023 11:39:27 +1100 Subject: [PATCH] vst uses shaders - resolution is wrong though? --- Cargo.lock | 10 ++--- gb-vst/Cargo.toml | 6 +-- gb-vst/src/ui.rs | 112 +++++++++++++++++++++------------------------- vendored/baseview | 2 +- vendored/nih-plug | 2 +- 5 files changed, 60 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5604648..8268fc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,9 +176,9 @@ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "anymap" -version = "0.12.1" +version = "1.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344" +checksum = "8f1f8f5a6f3d50d89e3797d7593a50f96bb2aaa20ca0cc7be1fb673232c91d72" [[package]] name = "arrayref" @@ -230,9 +230,9 @@ checksum = "62af46d040ba9df09edc6528dae9d8e49f5f3e82f55b7d2ec31a733c38dbc49d" [[package]] name = "atomic_refcell" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "112ef6b3f6cb3cb6fc5b6b494ef7a848492cff1ab0ef4de10b0f7d572861c905" +checksum = "76f2bfe491d41d45507b8431da8274f7feeca64a49e86d980eed2937ec2ff020" [[package]] name = "atty" @@ -3412,7 +3412,7 @@ dependencies = [ "gb-emu-lib", "keyboard-types", "nih_plug", - "pixels", + "raw-window-handle", ] [[package]] diff --git a/gb-vst/Cargo.toml b/gb-vst/Cargo.toml index b25ddd3..7311e7d 100644 --- a/gb-vst/Cargo.toml +++ b/gb-vst/Cargo.toml @@ -11,10 +11,10 @@ crate-type = ["cdylib", "rlib"] default = [] [dependencies] -gb-emu-lib = { path = "../lib" } +gb-emu-lib = { path = "../lib", features = ["vulkan-renderer"] } nih_plug = { path = "../vendored/nih-plug", features = ["standalone"] } baseview = { path = "../vendored/baseview" } -pixels = "0.12" async-ringbuf = "0.1.2" futures = "0.3" -keyboard-types = "0.6.2" \ No newline at end of file +keyboard-types = "0.6.2" +raw-window-handle = "0.5" diff --git a/gb-vst/src/ui.rs b/gb-vst/src/ui.rs index f0503c0..1cce493 100644 --- a/gb-vst/src/ui.rs +++ b/gb-vst/src/ui.rs @@ -7,12 +7,12 @@ use baseview::{ Event, EventStatus, Size, Window, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, }; use gb_emu_lib::{ - connect::{JoypadButtons, JoypadState, Renderer, HEIGHT, WIDTH}, - util::scale_buffer_in_place, + connect::{JoypadButtons, JoypadState, Renderer, ResolutionData, HEIGHT, WIDTH}, + renderer::{RendererBackend, RendererBackendManager, WindowOptions}, }; use keyboard_types::{Code, KeyState}; use nih_plug::prelude::*; -use pixels::{wgpu::PowerPreference, Pixels, SurfaceTexture}; +use raw_window_handle::HasRawDisplayHandle; use crate::{Frame, FrameReceiver, JoypadInfo, JoypadSender}; @@ -30,9 +30,7 @@ impl Emulator { } } -const EXTRA_SCALE: usize = 3; -const S_WIDTH: usize = WIDTH * EXTRA_SCALE; -const S_HEIGHT: usize = HEIGHT * EXTRA_SCALE; +const EXTRA_SCALE: usize = 4; impl Editor for Emulator { fn spawn( @@ -43,24 +41,24 @@ impl Editor for Emulator { let fr_cloned = self.frame_receiver.clone(); let js_cloned = self.joypad_sender.clone(); - let (size, scale) = if cfg!(target_os = "macos") { - ( - Size::new(S_WIDTH as f64, S_HEIGHT as f64), - baseview::WindowScalePolicy::SystemScaleFactor, - ) - } else { - ( - Size::new(WIDTH as f64, HEIGHT as f64), - baseview::WindowScalePolicy::ScaleFactor(EXTRA_SCALE as f64), - ) - }; + // let (size, scale) = if cfg!(target_os = "macos") { + // ( + // Size::new((WIDTH * EXTRA_SCALE) as f64, (HEIGHT * EXTRA_SCALE) as f64), + // baseview::WindowScalePolicy::SystemScaleFactor, + // ) + // } else { + // ( + // Size::new(WIDTH as f64, HEIGHT as f64), + // baseview::WindowScalePolicy::ScaleFactor(EXTRA_SCALE as f64), + // ) + // }; Window::open_parented( &parent, WindowOpenOptions { title: String::from("gb-emu"), - size, - scale, + size: Size::new((WIDTH * EXTRA_SCALE) as f64, (HEIGHT * EXTRA_SCALE) as f64), + scale: baseview::WindowScalePolicy::SystemScaleFactor, gl_config: None, }, |w| EmulatorWindow::new(w, fr_cloned, js_cloned), @@ -76,6 +74,7 @@ impl Editor for Emulator { } fn set_scale_factor(&self, _factor: f32) -> bool { + println!("factor: {_factor}"); true } @@ -87,11 +86,11 @@ impl Editor for Emulator { } pub struct EmulatorWindow { - pix: Pixels, - scale: usize, - scaled_buf: Frame, + renderer: RendererBackend, + manager: Arc, frame_receiver: Arc, joypad_sender: Arc, + current_resolution: ResolutionData, } impl EmulatorWindow { @@ -104,65 +103,54 @@ impl EmulatorWindow { Size::new(WIDTH as f64, HEIGHT as f64), EXTRA_SCALE as f64, ); + let physical_size = info.physical_size(); - let (pix, scale, scaled_buf) = init_pixbuf(info, window); + let current_resolution = ResolutionData { + real_width: physical_size.width, + real_height: physical_size.height, + scaled_width: WIDTH as u32, + scaled_height: HEIGHT as u32, + }; + + let manager = Arc::new(RendererBackendManager::new(window.raw_display_handle())); + + let renderer = RendererBackend::new(current_resolution, window, WindowOptions { + shader_path: Some(std::path::PathBuf::from( + "./test-roms/shaders/slang-shaders/handheld/console-border/gbc-lcd-grid-v2.slangp", + )), + }, manager.clone()); Self { - pix, - scale, - scaled_buf, + renderer, + manager, frame_receiver, joypad_sender, + current_resolution, } } } - -fn init_pixbuf(info: WindowInfo, window: &mut Window) -> (Pixels, usize, Frame) { - let physical_size = info.physical_size(); - let scale = (physical_size.width as usize / WIDTH).min(physical_size.height as usize / HEIGHT); - let scaled_buf = vec![[0, 0, 0, 0xFF]; WIDTH * scale * HEIGHT * scale]; - - ( - pixels::PixelsBuilder::new( - physical_size.width, - physical_size.height, - SurfaceTexture::new(physical_size.width, physical_size.height, window), - ) - .request_adapter_options(pixels::wgpu::RequestAdapterOptionsBase { - power_preference: PowerPreference::HighPerformance, - ..pixels::wgpu::RequestAdapterOptionsBase::default() - }) - .build() - .unwrap(), - scale, - scaled_buf, - ) -} - impl WindowHandler for EmulatorWindow { fn on_frame(&mut self, _window: &mut Window) { if let Some(ref mut receiver) = *self.frame_receiver.lock().expect("failed to lock mutex") { if let Some(ref buf) = receiver.try_iter().last() { - if self.scale != 1 { - scale_buffer_in_place(buf, &mut self.scaled_buf, WIDTH, HEIGHT, self.scale); - } - for (pixel, source) in self - .pix - .frame_mut() - .chunks_exact_mut(4) - .zip(&self.scaled_buf) - { - pixel.copy_from_slice(source); - } - self.pix.render().unwrap(); + self.renderer.new_frame(buf); } } + + self.renderer.render(self.current_resolution, &self.manager); } fn on_event(&mut self, window: &mut Window, event: baseview::Event) -> EventStatus { match event { Event::Window(WindowEvent::Resized(info)) => { - (self.pix, self.scale, self.scaled_buf) = init_pixbuf(info, window); + let physical_size = info.physical_size(); + self.current_resolution = ResolutionData { + real_width: physical_size.width, + real_height: physical_size.height, + scaled_width: WIDTH as u32, + scaled_height: HEIGHT as u32, + }; + self.renderer.resize(self.current_resolution, window); EventStatus::Captured } Event::Keyboard(event) => { diff --git a/vendored/baseview b/vendored/baseview index 974ed55..99bded6 160000 --- a/vendored/baseview +++ b/vendored/baseview @@ -1 +1 @@ -Subproject commit 974ed55ea29fd5fe2f0de85f53aaa552f3b38d32 +Subproject commit 99bded69793397f32521a894bd83b760dc3b863f diff --git a/vendored/nih-plug b/vendored/nih-plug index 33c266b..00ff795 160000 --- a/vendored/nih-plug +++ b/vendored/nih-plug @@ -1 +1 @@ -Subproject commit 33c266b496ec6a1cd87ef9fcc1ca541b5d34eb6e +Subproject commit 00ff79515000b9ef6477aad9a4dafa78b3dc6150