Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Present with sharp bilinear scaling

Samir Nuridd2b01a024c4
diff --git a/host/web.cpp b/host/web.cpp
index 50d46b6..f8291bc 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -68,10 +68,20 @@ EM_JS(void, ps2web_blit, (const std::uint32_t *rgba, int width, int height), {
const ctx = Module.blitContext;
const scale = Math.min(canvas.width / 4, canvas.height / 3);
const w = scale * 4, h = scale * 3;
+ const sx = Math.max(1, Math.floor(w / width)), sy = Math.max(1, Math.floor(h / height));
+ let source = Module.blitSource;
+ if (sx > 1 || sy > 1) {
+ if (!Module.blitSharp || Module.blitSharp.width !== width * sx || Module.blitSharp.height !== height * sy)
+ Module.blitSharp = new OffscreenCanvas(width * sx, height * sy);
+ const sharp = Module.blitSharp.getContext('2d');
+ sharp.imageSmoothingEnabled = false;
+ sharp.drawImage(Module.blitSource, 0, 0, width * sx, height * sy);
+ source = Module.blitSharp;
+ }
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.imageSmoothingEnabled = true;
- ctx.drawImage(Module.blitSource, (canvas.width - w) / 2, (canvas.height - h) / 2, w, h);
+ ctx.drawImage(source, (canvas.width - w) / 2, (canvas.height - h) / 2, w, h);
});
namespace {
diff --git a/runtime/src/gs/gpu/blit.wgsl b/runtime/src/gs/gpu/blit.wgsl
index 4ecbba5..2bc9939 100644
--- a/runtime/src/gs/gpu/blit.wgsl
+++ b/runtime/src/gs/gpu/blit.wgsl
@@ -4,9 +4,9 @@ struct Blit {
current: u32,
previous: u32,
blend: u32,
- pad0: u32,
- pad1: u32,
- pad2: u32,
+ out_width: u32,
+ out_height: u32,
+ pad: u32,
}
@group(0) @binding(0) var<storage, read> pixels: array<u32>;
@@ -33,7 +33,12 @@ fn texel(base: u32, x: i32, y: i32) -> vec4<f32> {
}
fn sample(base: u32, uv: vec2<f32>) -> vec4<f32> {
- let p = uv * vec2<f32>(f32(blit.width), f32(blit.height)) - 0.5;
+ let size = vec2<f32>(f32(blit.width), f32(blit.height));
+ let scale = max(floor(vec2<f32>(f32(blit.out_width), f32(blit.out_height)) / size), vec2<f32>(1.0));
+ let t = uv * size;
+ let d = fract(t) - 0.5;
+ let region = 0.5 - 0.5 / scale;
+ let p = floor(t) + (d - clamp(d, -region, region)) * scale;
let origin = floor(p);
let f = p - origin;
let x = i32(origin.x);
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cpp
index ea19707..457c1fb 100644
--- a/runtime/src/gs/gpu/gpu.cpp
+++ b/runtime/src/gs/gpu/gpu.cpp
@@ -1105,7 +1105,9 @@ bool present(const DisplaySource &src) {
const bool blend = src.halved && g_have_previous && g_last_width == src.width && g_last_height == src.height && !weave_off;
const std::uint32_t param = push_params({src.fbp, src.fbw, src.psm, src.dbx, src.dby, src.width, src.height, src.halved ? 1u : 0u, current});
g_commands.push_back({kDisplay, (src.width + 7) / 8, (src.height + 7) / 8, param});
- const std::uint32_t blit[8] = {src.width, src.height, current, previous, blend ? 1u : 0u, 0, 0, 0};
+ const float sw = static_cast<float>(g_surface_width), sh = static_cast<float>(g_surface_height);
+ const float vw = std::min(sw, sh * 4.0f / 3.0f), vh = vw * 3.0f / 4.0f;
+ const std::uint32_t blit[8] = {src.width, src.height, current, previous, blend ? 1u : 0u, static_cast<std::uint32_t>(vw), static_cast<std::uint32_t>(vh), 0};
wgpuQueueWriteBuffer(g_queue, g_blit_params, 0, blit, sizeof blit);
++g_field;
g_have_previous = true;