A Rust and egui based front-end for the SwanStation PS1 Libretro core
  • C++ 58.4%
  • C 36.7%
  • Rust 2.2%
  • HLSL 0.7%
  • GLSL 0.6%
  • Other 1.2%
Find a file
LibretroAdmin e56d4d4f7b core: route RESOLUTION_SCALE through vram_copy_ps cbuffer
First step of the non-batch RESOLUTION_SCALE cbuffer refactor
that unblocks pre-bake migrations for the shaders that currently
reference RESOLUTION_SCALE / VRAM_SIZE in their bodies. Scope is
intentionally narrow: just vram_copy_ps in this commit, with the
shared helper that follow-up commits use to refactor vram_write_ps,
vram_read_ps, display_ps, etc.

Background. Pre-7b575a3, every batch shader had m_resolution_scale
baked compile-time via WriteCommonFunctions's 'CONSTANT uint
RESOLUTION_SCALE = X;' emission, which forced a fresh shader
compile and PSO rebuild for every resolution-scale change. 7b575a3
moved RESOLUTION_SCALE / true_color / scaled_dithering into the
batch UBO as cbuffer-routed fields (u_resolution_scale,
u_true_color, u_scaled_dithering) and aliased the legacy
CONSTANT names to the cbuffer fields via #define. Same shader
source serves every resolution scale; no recompile on toggle.

Non-batch shaders kept the legacy literal path because they don't
bind the batch UBO. Each one had its own per-shader cbuffer at
register(b0), and WriteCommonFunctions's batch_uniform_buffer=false
branch emitted the CONSTANT block right after WriteHeader. So
vram_copy_ps, vram_write_ps, vram_read_ps, display_ps and
adaptive_downsample_* all still embed m_resolution_scale into
their source string, and every scale change recompiles them.

That's a problem for pre-bake. The whole point of pre-bake is
one DXBC blob per (semantic) variant; if RESOLUTION_SCALE bakes
into the source, every scale value needs its own .inc file -
typically 5+ blobs per shader just for the scale dimension. The
disk-shader-cache layer hides this at runtime (the second toggle
hits the cache), but pre-bake bypasses the runtime cache and
needs the variant count to stay small.

Refactor pattern (this commit applies it to vram_copy_ps; follow-
ups apply the same shape to other affected shaders):

  1. Add `uint u_resolution_scale, uint u_pad0` to the shader's
     DeclareUniformBuffer member list. The u_pad0 keeps the
     cbuffer 16-byte-aligned (HLSL cbuffer rows are 16 bytes;
     std140 GLSL block sizes round up to vec4).
  2. Reorder shadergen calls so DeclareUniformBuffer runs before
     anything that wants RESOLUTION_SCALE in scope.
  3. After DeclareUniformBuffer, call the new helper
     WriteCBufferResolutionScaleAliases(ss). This emits the same
     three #define lines that WriteBatchUniformBuffer has been
     emitting at the end of its declaration since 7b575a3:
       #define RESOLUTION_SCALE u_resolution_scale
       #define VRAM_SIZE        (uint2(VRAM_W, VRAM_H) * u_resolution_scale)
       #define RCP_VRAM_SIZE    (float2(1,1) / float2(VRAM_SIZE))
  4. Call WriteCommonFunctions(ss, true) instead of (ss, false)
     so the legacy 'CONSTANT uint RESOLUTION_SCALE = X;' block
     is skipped. MULTISAMPLES, PER_SAMPLE_SHADING, fixYCoord and
     the RGBA conversion helpers still emit as before.
  5. Append u_resolution_scale (+ u_pad0) to the matching
     C++ UBO struct in gpu_hw.h, and populate it in the
     GetXxxUBOData getter in gpu_hw.cpp.

Helper plumbing:

* gpu_hw_shadergen.h: declare WriteCBufferResolutionScaleAliases.
* gpu_hw_shadergen.cpp: factor the three alias lines out of
  WriteBatchUniformBuffer into WriteCBufferResolutionScaleAliases.
  WriteBatchUniformBuffer calls the helper at the same spot it
  used to inline the lines, so batch shaders are unchanged.

Shader change in this commit:

* GenerateVRAMCopyFragmentShader (gpu_hw_shadergen.cpp): reordered
  so the cbuffer is declared before WriteCommonFunctions, two
  trailing members added to the cbuffer
  (uint u_resolution_scale, uint u_pad0), helper called between
  cbuffer and WriteCommonFunctions(ss, true). Body untouched -
  the existing VRAM_SIZE references inside the body keep
  resolving through the #define aliases just like in batch
  shaders.

Runtime change in this commit:

* VRAMCopyUBOData (gpu_hw.h): two new uint32_t fields appended
  at the end (u_resolution_scale, u_pad0). Struct grows 40 -> 48
  bytes. All four backends (D3D11 / D3D12 / OpenGL / Vulkan)
  upload via runtime sizeof(uniforms), so they pick up the new
  size automatically. The two push-constant backends have
  MAX_PUSH_CONSTANTS_SIZE = 64 bytes - 48 still fits.
* GetVRAMCopyUBOData (gpu_hw.cpp): populate u_resolution_scale
  with m_resolution_scale, u_pad0 with 0.

Compatibility notes:

* Shader cache invalidation: vram_copy_ps's source string
  changes (different cbuffer members + different macro
  expansion). First boot after this lands re-compiles vram_copy
  on every backend; subsequent boots hit the regenerated cache
  entry.
* Runtime correctness: the VRAM_SIZE / RCP_VRAM_SIZE references
  in the shader body resolve to the same arithmetic at runtime
  that the literal would have. Tested mentally against the
  body code paths - the source-target offset math at lines
  1336-1338 uses VRAM_SIZE.x and VRAM_SIZE.y, both of which
  expand to (1024 * u_resolution_scale) and (512 * u_resolution_scale)
  respectively. Identical to the previous CONSTANT-baked
  values for any given m_resolution_scale.
* Push-constant size: 48 bytes / 12 DWORDs on the D3D12 root
  signature side, well under the 64-byte / 16-DWORD limit at
  Add32BitConstants. Vulkan vkCmdPushConstants accepts up to
  the pipeline layout's reserved range (MAX_PUSH_CONSTANTS_SIZE
  = 64). D3D11 / OpenGL use regular UBO uploads with no fixed
  size limit.
* No behaviour change for batch shaders. WriteBatchUniformBuffer
  still emits the same final source - the three alias lines
  just come from a function call now instead of inline.

Cost: zero in steady state. The shader does one extra mul
(1024u * u_resolution_scale) per pixel where the literal was
constant-folded - but HLSL constant-folds u_resolution_scale's
known-at-PSO-bind-time value too, and even if it didn't, an
imul on a known small constant is sub-nanosecond. The push
constants are 8 extra bytes on every CopyVRAM call (negligible
compared to the rest of the per-call setup).

Test:
  - Build under each backend (D3D11 / D3D12 / OpenGL / Vulkan)
    succeeds. The new helper has no callers outside
    gpu_hw_shadergen.cpp so there's no link-side surface area.
  - CopyVRAM renders correctly at every resolution scale. The
    VRAM_SIZE references in the body now resolve through
    u_resolution_scale; if the cbuffer field weren't populated
    correctly, source/dest offsets in CopyVRAM would be wrong
    and game graphics with VRAM-to-VRAM transfers (most
    PSX games) would corrupt visibly within a few frames.
  - Toggle resolution scale at runtime (1x -> 2x -> 4x -> 1x).
    First toggle of each scale recompiles vram_copy_ps once;
    second toggle hits the cache. Behaviour matches what
    happened pre-refactor.
  - Pre-existing PGXP-depth dimension still works (two PSOs
    in the m_vram_copy_pipelines[depth_test] matrix), with
    the embedded blob path TBD - that's a follow-up patch.

Next in the arc:
  - Apply the same refactor to vram_write_ps (uses both
    RESOLUTION_SCALE and VRAM_SIZE), vram_read_ps (uses
    RESOLUTION_SCALE in the loop bounds), and display_ps
    (uses RESOLUTION_SCALE directly).
  - After all of those are refactored, the RESOLUTION_SCALE
    axis is gone from every non-batch shader's variant matrix.
    Then pre-bake migrations can proceed for each one.
2026-05-18 20:59:54 +02:00
CMakeModules Remove unused files 2023-03-25 17:43:35 +01:00
data/shaders core/d3d12: pre-bake the copy/blit pixel shader 2026-05-18 20:22:17 +02:00
dep dep, common/vulkan, core, build: remove dep/glslang and the Vulkan ShaderCompiler infrastructure 2026-05-18 02:21:53 +02:00
src core: route RESOLUTION_SCALE through vram_copy_ps cbuffer 2026-05-18 20:59:54 +02:00
tools core/d3d12: scaffolding for pre-baked DXBC blobs 2026-05-18 09:14:52 +02:00
.clang-format clang-format: Enable brace wrapping after case label 2020-01-24 14:49:43 +10:00
.gitattributes Initial commit 2019-09-11 14:00:42 +10:00
.gitignore Sync Android strings 2021-07-17 20:14:40 +10:00
.gitlab-ci.yml Merge pull request #167 from cscd98/webos-ci 2026-04-10 05:12:17 +02:00
CMakeLists.txt build: add MSB_FIRST opt-in flag for big-endian hosts 2026-05-08 00:32:03 +02:00
libretro.osx.def libretro: add static Makefile.libretro build 2026-05-07 21:32:02 +02:00
LICENSE Initial commit 2019-09-11 14:00:42 +10:00
link.T libretro: add static Makefile.libretro build 2026-05-07 21:32:02 +02:00
Makefile.libretro core/d3d12: scaffolding for pre-baked DXBC blobs 2026-05-18 09:14:52 +02:00
README.md Documentation OpenBIOS usage in the README. 2026-04-02 10:46:10 +01:00
TODO Update TODO (#132) 2025-01-17 16:28:45 +01:00

SwanStation - PlayStation 1, aka. PSX Emulator

Features | System Requirements | Disclaimers

SwanStation is a hard fork and open-source Libretro core implementation of DuckStation, which is an emulator of the Sony PlayStation(TM) console, focusing on playability, speed, and long-term maintainability. The goal is to be as accurate as possible while maintaining performance suitable for low-end devices. "Hack" options are discouraged, the default configuration should support all playable games with only some of the enhancements having compatibility issues.

A "BIOS" ROM image can be used, but is not required. You can use an image from any hardware version or region, although mismatching game regions and BIOS regions may have compatibility issues. A ROM image is not provided with the emulator for legal reasons, you should dump this from your own console using Caetla or other means. If no ROM image is provided, OpenBIOS will be used instead.

Features

SwanStation features include:

  • CPU Recompiler/JIT (x86-64, armv7/AArch32 and AArch64)
  • Hardware (D3D11, OpenGL, Vulkan) and software rendering
  • Upscaling, texture filtering, and true colour (24-bit) in hardware renderers
  • PGXP for geometry precision, texture correction, and depth buffer emulation
  • Adaptive downsampling filter
  • Post processing shader chains
  • "Fast boot" for skipping BIOS splash/intro
  • Save state support
  • Supports bin/cue images, raw bin/img files, MAME CHD, single-track ECM, MDS/MDF, and unencrypted PBP formats.
  • Direct booting of homebrew executables
  • Direct loading of Portable Sound Format (psf) files
  • Digital and analog controllers for input (rumble is forwarded to host)
  • Namco GunCon lightgun support (simulated with mouse)
  • NeGcon support
  • Emulated CPU overclocking
  • Multitap controllers (up to 8 devices)
  • RetroAchievements
  • Automatic loading/applying of PPF patches

System Requirements

  • A CPU faster than a potato. But it needs to be x86_64, AArch32/armv7, or AArch64/ARMv8, otherwise you won't get a recompiler and it'll be slow.
  • For the hardware renderers, a GPU capable of OpenGL 3.1/OpenGL ES 3.0/Direct3D 11 Feature Level 10.0 (or Vulkan 1.0) and above. So, basically anything made in 2013 or later.

Region detection and BIOS images

By default, SwanStation will emulate the region check present in the CD-ROM controller of the console. This means that when the region of the console does not match the disc, it will refuse to boot, giving a "Please insert PlayStation CD-ROM" message. SwanStation supports automatic detection disc regions, and if you set the console region to auto-detect as well, this should never be a problem.

The region checking can be disabled in the console options tab. This is the only way to play unlicensed games or homebrew which does not supply a correct region string on the disc, aside from using fastboot which skips the check entirely.

Mismatching the disc and console regions with the check disabled is supported, but may break games if they are patching the BIOS and expecting specific content.

LibCrypt protection and SBI files

A number of PAL region games use LibCrypt protection, requiring additional CD subchannel information to run properly. libcrypt not functioning usually manifests as hanging or crashing, but can sometimes affect gameplay too, depending on how the game implemented it.

For these games, make sure that the CD image and its corresponding SBI (.sbi) file have the same name and are placed in the same directory. SwanStation will automatically load the SBI file when it is found next to the CD image.

For example, if your disc image was named Spyro3.cue, you would place the SBI file in the same directory, and name it Spyro3.sbi.

Tests

  • Passes amidog's CPU and GTE tests in both interpreter and recompiler modes, partial passing of CPX tests

Disclaimers

"PlayStation" and "PSX" are registered trademarks of Sony Interactive Entertainment Europe Limited. This project is not affiliated in any way with Sony Interactive Entertainment.