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]]
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]]

View file

@ -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"
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,
};
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<RendererBackendManager>,
frame_receiver: Arc<FrameReceiver>,
joypad_sender: Arc<JoypadSender>,
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) => {

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

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