mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add ScaleMode::Fill to fill screen without black borders
This commit is contained in:
@@ -468,6 +468,7 @@ when using the native runtime:
|
||||
* `--no-gpu`: Force old cpu-only window code
|
||||
* `--filter FILTER`: Select an upscale filter at startup
|
||||
* `--fullscreen`: Start in fullscreen mode
|
||||
* `--scale-fill`: Scale to fill whole screen, potentially cropping parts of the frame buffer.
|
||||
|
||||
Note that the cpu-only window does not support fullscreen nor upscale filters.
|
||||
|
||||
@@ -484,7 +485,7 @@ The upscale filter options are:
|
||||
5, auto_crt (default) : ss_crt below 960x720, chromatic_crt otherwise
|
||||
```
|
||||
|
||||
You can switch the upscale filter at any time using the keys 1-5. You can toggle fullscreen with F.
|
||||
You can switch the upscale filter at any time using the keys 1-5. You can toggle fullscreen with F. You can toggle between scale modes 'fit' and 'fill' with M.
|
||||
|
||||
## `uw8 pack`
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::dpi::PhysicalSize;
|
||||
|
||||
use super::Filter;
|
||||
use super::{scale_mode::ScaleMode, Filter};
|
||||
|
||||
pub struct CrtFilter {
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
@@ -15,9 +15,10 @@ impl CrtFilter {
|
||||
screen: &wgpu::TextureView,
|
||||
resolution: PhysicalSize<u32>,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
scale_mode: ScaleMode,
|
||||
) -> CrtFilter {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(resolution),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(resolution),
|
||||
};
|
||||
|
||||
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
@@ -112,9 +113,9 @@ impl CrtFilter {
|
||||
}
|
||||
|
||||
impl Filter for CrtFilter {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>) {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>, scale_mode: ScaleMode) {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(new_size),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(new_size),
|
||||
};
|
||||
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[uniforms]));
|
||||
}
|
||||
@@ -126,16 +127,6 @@ impl Filter for CrtFilter {
|
||||
}
|
||||
}
|
||||
|
||||
fn texture_scale_from_resolution(res: PhysicalSize<u32>) -> [f32; 4] {
|
||||
let scale = ((res.width as f32) / 160.0).min((res.height as f32) / 120.0);
|
||||
[
|
||||
res.width as f32 / scale,
|
||||
res.height as f32 / scale,
|
||||
2.0 / scale,
|
||||
0.0,
|
||||
]
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Uniforms {
|
||||
|
||||
@@ -17,7 +17,7 @@ fn vs_main(
|
||||
let i = in_vertex_index / 3u + in_vertex_index % 3u;
|
||||
let x = -1.0 + f32(i % 2u) * 322.0;
|
||||
let y = -1.0 + f32(i / 2u) * 242.0;
|
||||
out.clip_position = vec4<f32>((vec2<f32>(x, y) - vec2<f32>(160.0, 120.0)) / uniforms.texture_scale.xy, 0.0, 1.0);
|
||||
out.clip_position = vec4<f32>((vec2<f32>(x, y) - vec2<f32>(160.0, 120.0)) * uniforms.texture_scale.xy, 0.0, 1.0);
|
||||
out.tex_coords = vec2<f32>(x, y);
|
||||
return out;
|
||||
}
|
||||
@@ -72,4 +72,4 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
acc = acc + sample_pixel(pixel + vec2<i32>(1, 1), offset_x2 + offset_yr);
|
||||
|
||||
return vec4<f32>(acc, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::dpi::PhysicalSize;
|
||||
|
||||
use super::Filter;
|
||||
use super::{scale_mode::ScaleMode, Filter};
|
||||
|
||||
pub struct FastCrtFilter {
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
@@ -16,9 +16,10 @@ impl FastCrtFilter {
|
||||
resolution: PhysicalSize<u32>,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
chromatic: bool,
|
||||
scale_mode: ScaleMode,
|
||||
) -> FastCrtFilter {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(resolution),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(resolution),
|
||||
};
|
||||
|
||||
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
@@ -131,9 +132,9 @@ impl FastCrtFilter {
|
||||
}
|
||||
|
||||
impl Filter for FastCrtFilter {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>) {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>, scale_mode: ScaleMode) {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(new_size),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(new_size),
|
||||
};
|
||||
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[uniforms]));
|
||||
}
|
||||
@@ -145,16 +146,6 @@ impl Filter for FastCrtFilter {
|
||||
}
|
||||
}
|
||||
|
||||
fn texture_scale_from_resolution(res: PhysicalSize<u32>) -> [f32; 4] {
|
||||
let scale = ((res.width as f32) / 160.0).min((res.height as f32) / 120.0);
|
||||
[
|
||||
scale / res.width as f32,
|
||||
scale / res.height as f32,
|
||||
2.0 / scale,
|
||||
0.0,
|
||||
]
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Uniforms {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::{Input, WindowConfig, WindowImpl};
|
||||
use anyhow::{anyhow, Result};
|
||||
use scale_mode::ScaleMode;
|
||||
use std::time::Instant;
|
||||
|
||||
use winit::{
|
||||
@@ -13,6 +14,7 @@ use winit::platform::run_return::EventLoopExtRunReturn;
|
||||
|
||||
mod crt;
|
||||
mod fast_crt;
|
||||
pub mod scale_mode;
|
||||
mod square;
|
||||
|
||||
use crt::CrtFilter;
|
||||
@@ -34,6 +36,7 @@ pub struct Window {
|
||||
next_frame: Instant,
|
||||
is_fullscreen: bool,
|
||||
is_open: bool,
|
||||
scale_mode: ScaleMode,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
@@ -90,6 +93,7 @@ impl Window {
|
||||
window.inner_size(),
|
||||
surface_config.format,
|
||||
window_config.filter,
|
||||
window_config.scale_mode,
|
||||
);
|
||||
|
||||
surface.configure(&device, &surface_config);
|
||||
@@ -109,6 +113,7 @@ impl Window {
|
||||
next_frame: Instant::now(),
|
||||
is_fullscreen: window_config.fullscreen,
|
||||
is_open: true,
|
||||
scale_mode: window_config.scale_mode,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -128,7 +133,7 @@ impl WindowImpl for Window {
|
||||
self.surface_config.width = new_size.width;
|
||||
self.surface_config.height = new_size.height;
|
||||
self.surface.configure(&self.device, &self.surface_config);
|
||||
self.filter.resize(&self.queue, new_size);
|
||||
self.filter.resize(&self.queue, new_size, self.scale_mode);
|
||||
}
|
||||
WindowEvent::CloseRequested => {
|
||||
self.is_open = false;
|
||||
@@ -165,6 +170,20 @@ impl WindowImpl for Window {
|
||||
self.is_fullscreen = fullscreen.is_some();
|
||||
self.window.set_fullscreen(fullscreen);
|
||||
}
|
||||
Some(VirtualKeyCode::M) => {
|
||||
self.scale_mode = match self.scale_mode {
|
||||
ScaleMode::Fit => ScaleMode::Fill,
|
||||
ScaleMode::Fill => ScaleMode::Fit,
|
||||
};
|
||||
self.filter.resize(
|
||||
&self.queue,
|
||||
PhysicalSize {
|
||||
width: self.surface_config.width,
|
||||
height: self.surface_config.height,
|
||||
},
|
||||
self.scale_mode,
|
||||
);
|
||||
}
|
||||
Some(VirtualKeyCode::R) => reset = true,
|
||||
Some(VirtualKeyCode::Key1) => new_filter = Some(1),
|
||||
Some(VirtualKeyCode::Key2) => new_filter = Some(2),
|
||||
@@ -198,6 +217,7 @@ impl WindowImpl for Window {
|
||||
self.window.inner_size(),
|
||||
self.surface_config.format,
|
||||
new_filter,
|
||||
self.scale_mode,
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -260,6 +280,7 @@ fn create_filter(
|
||||
window_size: PhysicalSize<u32>,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
filter: u32,
|
||||
scale_mode: ScaleMode,
|
||||
) -> Box<dyn Filter> {
|
||||
match filter {
|
||||
1 => Box::new(SquareFilter::new(
|
||||
@@ -267,6 +288,7 @@ fn create_filter(
|
||||
screen_texture,
|
||||
window_size,
|
||||
surface_format,
|
||||
scale_mode,
|
||||
)),
|
||||
2 => Box::new(FastCrtFilter::new(
|
||||
device,
|
||||
@@ -274,12 +296,14 @@ fn create_filter(
|
||||
window_size,
|
||||
surface_format,
|
||||
false,
|
||||
scale_mode,
|
||||
)),
|
||||
3 => Box::new(CrtFilter::new(
|
||||
device,
|
||||
screen_texture,
|
||||
window_size,
|
||||
surface_format,
|
||||
scale_mode,
|
||||
)),
|
||||
4 => Box::new(FastCrtFilter::new(
|
||||
device,
|
||||
@@ -287,18 +311,20 @@ fn create_filter(
|
||||
window_size,
|
||||
surface_format,
|
||||
true,
|
||||
scale_mode,
|
||||
)),
|
||||
_ => Box::new(AutoCrtFilter::new(
|
||||
device,
|
||||
screen_texture,
|
||||
window_size,
|
||||
surface_format,
|
||||
scale_mode,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
trait Filter {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>);
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>, scale_mode: ScaleMode);
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
|
||||
}
|
||||
|
||||
@@ -314,9 +340,11 @@ impl AutoCrtFilter {
|
||||
screen: &wgpu::TextureView,
|
||||
resolution: PhysicalSize<u32>,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
scale_mode: ScaleMode,
|
||||
) -> AutoCrtFilter {
|
||||
let small = CrtFilter::new(device, screen, resolution, surface_format);
|
||||
let large = FastCrtFilter::new(device, screen, resolution, surface_format, true);
|
||||
let small = CrtFilter::new(device, screen, resolution, surface_format, scale_mode);
|
||||
let large =
|
||||
FastCrtFilter::new(device, screen, resolution, surface_format, true, scale_mode);
|
||||
AutoCrtFilter {
|
||||
small,
|
||||
large,
|
||||
@@ -326,9 +354,9 @@ impl AutoCrtFilter {
|
||||
}
|
||||
|
||||
impl Filter for AutoCrtFilter {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>) {
|
||||
self.small.resize(queue, new_size);
|
||||
self.large.resize(queue, new_size);
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>, scale_mode: ScaleMode) {
|
||||
self.small.resize(queue, new_size, scale_mode);
|
||||
self.large.resize(queue, new_size, scale_mode);
|
||||
self.resolution = new_size;
|
||||
}
|
||||
|
||||
|
||||
28
uw8-window/src/gpu/scale_mode.rs
Normal file
28
uw8-window/src/gpu/scale_mode.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use winit::dpi::PhysicalSize;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum ScaleMode {
|
||||
Fit,
|
||||
Fill,
|
||||
}
|
||||
|
||||
impl Default for ScaleMode {
|
||||
fn default() -> ScaleMode {
|
||||
ScaleMode::Fit
|
||||
}
|
||||
}
|
||||
|
||||
impl ScaleMode {
|
||||
pub fn texture_scale_from_resolution(&self, res: PhysicalSize<u32>) -> [f32; 4] {
|
||||
let scale = match self {
|
||||
ScaleMode::Fit => ((res.width as f32) / 160.0).min((res.height as f32) / 120.0),
|
||||
ScaleMode::Fill => ((res.width as f32) / 160.0).max((res.height as f32) / 120.0),
|
||||
};
|
||||
[
|
||||
scale / res.width as f32,
|
||||
scale / res.height as f32,
|
||||
2.0 / scale,
|
||||
0.0,
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::dpi::PhysicalSize;
|
||||
|
||||
use super::Filter;
|
||||
use super::{scale_mode::ScaleMode, Filter};
|
||||
|
||||
pub struct SquareFilter {
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
@@ -15,9 +15,10 @@ impl SquareFilter {
|
||||
screen: &wgpu::TextureView,
|
||||
resolution: PhysicalSize<u32>,
|
||||
surface_format: wgpu::TextureFormat,
|
||||
scale_mode: ScaleMode,
|
||||
) -> SquareFilter {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(resolution),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(resolution),
|
||||
};
|
||||
|
||||
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
@@ -126,9 +127,9 @@ impl SquareFilter {
|
||||
}
|
||||
|
||||
impl Filter for SquareFilter {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>) {
|
||||
fn resize(&mut self, queue: &wgpu::Queue, new_size: PhysicalSize<u32>, scale_mode: ScaleMode) {
|
||||
let uniforms = Uniforms {
|
||||
texture_scale: texture_scale_from_resolution(new_size),
|
||||
texture_scale: scale_mode.texture_scale_from_resolution(new_size),
|
||||
};
|
||||
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[uniforms]));
|
||||
}
|
||||
@@ -140,16 +141,6 @@ impl Filter for SquareFilter {
|
||||
}
|
||||
}
|
||||
|
||||
fn texture_scale_from_resolution(res: PhysicalSize<u32>) -> [f32; 4] {
|
||||
let scale = ((res.width as f32) / 160.0).min((res.height as f32) / 120.0);
|
||||
[
|
||||
scale / res.width as f32,
|
||||
scale / res.height as f32,
|
||||
2.0 / scale,
|
||||
0.0,
|
||||
]
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Uniforms {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use anyhow::Result;
|
||||
use gpu::scale_mode::ScaleMode;
|
||||
use std::time::Instant;
|
||||
|
||||
mod cpu;
|
||||
@@ -73,6 +74,7 @@ pub struct WindowConfig {
|
||||
fullscreen: bool,
|
||||
fps_counter: bool,
|
||||
scale: f32,
|
||||
scale_mode: ScaleMode,
|
||||
}
|
||||
|
||||
impl Default for WindowConfig {
|
||||
@@ -83,6 +85,7 @@ impl Default for WindowConfig {
|
||||
fullscreen: false,
|
||||
fps_counter: false,
|
||||
scale: 2.,
|
||||
scale_mode: ScaleMode::Fit,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +112,9 @@ impl WindowConfig {
|
||||
.opt_value_from_str("--scale")
|
||||
.unwrap()
|
||||
.unwrap_or(self.scale);
|
||||
if args.contains("--scale-fill") {
|
||||
self.scale_mode = ScaleMode::Fill;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user