vst uses shaders - resolution is wrong though?

This commit is contained in:
Alex Janka 2023-10-04 11:39:27 +11:00
parent b6a3e83c85
commit 82992436d8
5 changed files with 60 additions and 72 deletions

10
Cargo.lock generated
View file

@ -176,9 +176,9 @@ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]] [[package]]
name = "anymap" name = "anymap"
version = "0.12.1" version = "1.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344" checksum = "8f1f8f5a6f3d50d89e3797d7593a50f96bb2aaa20ca0cc7be1fb673232c91d72"
[[package]] [[package]]
name = "arrayref" name = "arrayref"
@ -230,9 +230,9 @@ checksum = "62af46d040ba9df09edc6528dae9d8e49f5f3e82f55b7d2ec31a733c38dbc49d"
[[package]] [[package]]
name = "atomic_refcell" name = "atomic_refcell"
version = "0.1.11" version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112ef6b3f6cb3cb6fc5b6b494ef7a848492cff1ab0ef4de10b0f7d572861c905" checksum = "76f2bfe491d41d45507b8431da8274f7feeca64a49e86d980eed2937ec2ff020"
[[package]] [[package]]
name = "atty" name = "atty"
@ -3412,7 +3412,7 @@ dependencies = [
"gb-emu-lib", "gb-emu-lib",
"keyboard-types", "keyboard-types",
"nih_plug", "nih_plug",
"pixels", "raw-window-handle",
] ]
[[package]] [[package]]

View file

@ -11,10 +11,10 @@ crate-type = ["cdylib", "rlib"]
default = [] default = []
[dependencies] [dependencies]
gb-emu-lib = { path = "../lib" } gb-emu-lib = { path = "../lib", features = ["vulkan-renderer"] }
nih_plug = { path = "../vendored/nih-plug", features = ["standalone"] } nih_plug = { path = "../vendored/nih-plug", features = ["standalone"] }
baseview = { path = "../vendored/baseview" } baseview = { path = "../vendored/baseview" }
pixels = "0.12"
async-ringbuf = "0.1.2" async-ringbuf = "0.1.2"
futures = "0.3" futures = "0.3"
keyboard-types = "0.6.2" keyboard-types = "0.6.2"
raw-window-handle = "0.5"

View file

@ -7,12 +7,12 @@ use baseview::{
Event, EventStatus, Size, Window, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, Event, EventStatus, Size, Window, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
}; };
use gb_emu_lib::{ use gb_emu_lib::{
connect::{JoypadButtons, JoypadState, Renderer, HEIGHT, WIDTH}, connect::{JoypadButtons, JoypadState, Renderer, ResolutionData, HEIGHT, WIDTH},
util::scale_buffer_in_place, renderer::{RendererBackend, RendererBackendManager, WindowOptions},
}; };
use keyboard_types::{Code, KeyState}; use keyboard_types::{Code, KeyState};
use nih_plug::prelude::*; use nih_plug::prelude::*;
use pixels::{wgpu::PowerPreference, Pixels, SurfaceTexture}; use raw_window_handle::HasRawDisplayHandle;
use crate::{Frame, FrameReceiver, JoypadInfo, JoypadSender}; use crate::{Frame, FrameReceiver, JoypadInfo, JoypadSender};
@ -30,9 +30,7 @@ impl Emulator {
} }
} }
const EXTRA_SCALE: usize = 3; const EXTRA_SCALE: usize = 4;
const S_WIDTH: usize = WIDTH * EXTRA_SCALE;
const S_HEIGHT: usize = HEIGHT * EXTRA_SCALE;
impl Editor for Emulator { impl Editor for Emulator {
fn spawn( fn spawn(
@ -43,24 +41,24 @@ impl Editor for Emulator {
let fr_cloned = self.frame_receiver.clone(); let fr_cloned = self.frame_receiver.clone();
let js_cloned = self.joypad_sender.clone(); let js_cloned = self.joypad_sender.clone();
let (size, scale) = if cfg!(target_os = "macos") { // let (size, scale) = if cfg!(target_os = "macos") {
( // (
Size::new(S_WIDTH as f64, S_HEIGHT as f64), // Size::new((WIDTH * EXTRA_SCALE) as f64, (HEIGHT * EXTRA_SCALE) as f64),
baseview::WindowScalePolicy::SystemScaleFactor, // baseview::WindowScalePolicy::SystemScaleFactor,
) // )
} else { // } else {
( // (
Size::new(WIDTH as f64, HEIGHT as f64), // Size::new(WIDTH as f64, HEIGHT as f64),
baseview::WindowScalePolicy::ScaleFactor(EXTRA_SCALE as f64), // baseview::WindowScalePolicy::ScaleFactor(EXTRA_SCALE as f64),
) // )
}; // };
Window::open_parented( Window::open_parented(
&parent, &parent,
WindowOpenOptions { WindowOpenOptions {
title: String::from("gb-emu"), title: String::from("gb-emu"),
size, size: Size::new((WIDTH * EXTRA_SCALE) as f64, (HEIGHT * EXTRA_SCALE) as f64),
scale, scale: baseview::WindowScalePolicy::SystemScaleFactor,
gl_config: None, gl_config: None,
}, },
|w| EmulatorWindow::new(w, fr_cloned, js_cloned), |w| EmulatorWindow::new(w, fr_cloned, js_cloned),
@ -76,6 +74,7 @@ impl Editor for Emulator {
} }
fn set_scale_factor(&self, _factor: f32) -> bool { fn set_scale_factor(&self, _factor: f32) -> bool {
println!("factor: {_factor}");
true true
} }
@ -87,11 +86,11 @@ impl Editor for Emulator {
} }
pub struct EmulatorWindow { pub struct EmulatorWindow {
pix: Pixels, renderer: RendererBackend,
scale: usize, manager: Arc<RendererBackendManager>,
scaled_buf: Frame,
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
joypad_sender: Arc<JoypadSender>, joypad_sender: Arc<JoypadSender>,
current_resolution: ResolutionData,
} }
impl EmulatorWindow { impl EmulatorWindow {
@ -104,65 +103,54 @@ impl EmulatorWindow {
Size::new(WIDTH as f64, HEIGHT as f64), Size::new(WIDTH as f64, HEIGHT as f64),
EXTRA_SCALE 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 { Self {
pix, renderer,
scale, manager,
scaled_buf,
frame_receiver, frame_receiver,
joypad_sender, 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 { impl WindowHandler for EmulatorWindow {
fn on_frame(&mut self, _window: &mut Window) { 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 mut receiver) = *self.frame_receiver.lock().expect("failed to lock mutex") {
if let Some(ref buf) = receiver.try_iter().last() { if let Some(ref buf) = receiver.try_iter().last() {
if self.scale != 1 { self.renderer.new_frame(buf);
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.render(self.current_resolution, &self.manager);
} }
fn on_event(&mut self, window: &mut Window, event: baseview::Event) -> EventStatus { fn on_event(&mut self, window: &mut Window, event: baseview::Event) -> EventStatus {
match event { match event {
Event::Window(WindowEvent::Resized(info)) => { 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 EventStatus::Captured
} }
Event::Keyboard(event) => { Event::Keyboard(event) => {

@ -1 +1 @@
Subproject commit 974ed55ea29fd5fe2f0de85f53aaa552f3b38d32 Subproject commit 99bded69793397f32521a894bd83b760dc3b863f

@ -1 +1 @@
Subproject commit 33c266b496ec6a1cd87ef9fcc1ca541b5d34eb6e Subproject commit 00ff79515000b9ef6477aad9a4dafa78b3dc6150