A Rust and egui based front-end for the SwanStation PS1 Libretro core
  • C++ 58%
  • C 36.4%
  • Rust 2.8%
  • HLSL 0.7%
  • GLSL 0.6%
  • Other 1.4%
Find a file
Lib f188bb0ee2 gpu_hw_d3d11: introduce three-mode shader precompile core option
Switching the texture filter from Nearest to JINC2 / xBR / Bilinear
in the D3D11 backend currently freezes RetroArch for multiple
seconds: GPU_HW_D3D11::UpdateSettings -> DestroyShaders ->
CompileShaders rebuilds the entire batch fragment shader matrix
(4 render modes x 9 texture modes x 2 dithering x 2 interlacing =
144 permutations, 112 of them unique) synchronously on the runloop
thread. With high-optimisation HLSL compiles at 50-200 ms per
shader the total can run 10-20 seconds; the previously-shipped
RETRO_MESSAGE_TYPE_PROGRESS notification is unreadable against
the RetroArch menu UI during the stall, so users perceive a hang.

This adds a 'swanstation_GPU_ShaderPrecompile' core option with
three values, defaulting to 'Lazy', and threads its behaviour
through the D3D11 backend (other backends follow in subsequent
commits):

  - Disabled: CompileShaders skips the batch fragment shader matrix
    entirely. Each combination compiles on the main thread the
    first time the game dispatches a draw using it, then is
    cached. Lowest startup latency; trades the single multi-second
    stall for a small number of first-use hitches scattered
    through early gameplay. Useful on low-end hardware and for
    users who don't intend to use the heavier filters.
  - Enabled: the historical behaviour. CompileShaders walks the
    full matrix synchronously at GPU init. RetroArch is blocked
    the entire time. After that, no per-draw hitches. Useful for
    benchmarking and for users who want a single up-front cost.
  - Lazy (default): CompileShaders spawns a background thread
    that walks the matrix and fills it slot-by-slot. The main
    thread starts gameplay immediately. If the game asks for a
    combo the worker hasn't reached yet, the main thread compiles
    it itself (same hitch as Disabled for that one combo);
    otherwise the slot is already populated and the draw proceeds
    at full speed. After the worker finishes, behaviour is
    identical to Enabled.

A new GetBatchPixelShader(render_mode, texture_mode, dithering,
interlacing) helper centralises the lazy-compile path. It:

  - Applies the Reserved_Direct16Bit (3) -> Direct16Bit (2) and
    Reserved_RawDirect16Bit (7) -> RawDirect16Bit (6) dedup
    discovered in commit 8e7f7a3, by routing the lookup through a
    canonical slot and ComPtr-copying the result into the
    reserved-variant slot the first time it's accessed.
  - Takes m_batch_shader_mutex around both the cache mutation
    (D3D11::ShaderCache::m_index is an unordered_map and is not
    thread-safe on insert) and the m_batch_pixel_shaders array
    write. The fast path is one uncontended mutex lock per
    DrawBatchVertices call; on a modern std::mutex impl that's
    roughly 20 ns of overhead.
  - Returns a non-owning ID3D11PixelShader* (consumer side is
    free-threaded, so PSSetShader from the runloop is safe even
    while the worker thread is still publishing future entries).

DrawBatchVertices routes its m_batch_pixel_shaders lookup through
GetBatchPixelShader so all three precompile modes use the same
fault-in path; the difference is purely whether anyone called
GetBatchPixelShader before the draw did.

D3D11::ShaderCache and GPU_HW_ShaderGen lifetimes change to match
the GPU backend's lifetime instead of being locals in
CompileShaders. The shader cache now lives in m_shader_cache; the
shader generator lives in m_shadergen (a unique_ptr because the
class is non-default-constructible). On UpdateSettings round-trips
through DestroyShaders/CompileShaders, the shader cache instance
persists (Open is guarded by a new D3D11::ShaderCache::IsOpen()
accessor), so the in-memory cache index isn't re-read from disk
each time. The shadergen is rebuilt because its baked-in settings
may have changed.

The background-thread worker walks the matrix in
(render, texture, dithering, interlacing) order, calling
GetBatchPixelShader on each slot. The double-checked-locking
pattern means the main thread can race ahead and pre-fill any
slot it needs at draw time; the worker just observes the filled
slot under the lock and skips its own compile.

DestroyShaders signals m_shader_compile_thread_quit before
joining the worker so teardown latency is bounded by one
in-flight shader compile (up to ~200 ms in pathological cases).
The worker checks the flag between every cell. Re-entry safety:
CompileShaders calls StopShaderCompileThread at the top so any
worker from a prior UpdateSettings round-trip is joined before a
new matrix fill begins.

A standalone TSAN test exercising the same concurrency model
(double-checked lock + dedup + race against random main-thread
fault-ins) confirmed: exactly 112 unique compiles, 32 dedup'd
slots, no data races, no double-init, clean early-quit termination,
clean 5-cycle teardown/recreate.

For 'Lazy' mode the ShaderCompileProgressTracker is given a total
of 0 for the batch matrix dimension (the worker doesn't update it),
so the progress widget shipped in commit 1089822 only reports the
~12 non-batch shaders during init - a sub-second blip rather than
the previous multi-second crawl. The total wall-clock cost is the
same; it's just moved off the runloop.

The other three backends (D3D12, OpenGL, Vulkan) still take the
historical Enabled-style path regardless of the user's choice,
because the core option doesn't reach their CompileShaders yet.
Follow-up commits will extend this. OpenGL is harder because the
GL context can't be shared with a worker thread under the libretro
hardware-renderer protocol - the Lazy option will degrade to
Disabled there (compile-on-first-use on the main thread, no
background work).

Build: zero warnings, zero errors on linux platform=unix (which
doesn't compile gpu_hw_d3d11.cpp - guarded ifeq IS_WINDOWS=1).
gpu_hw_d3d11.cpp and common/d3d11/shader_cache.cpp both
cross-compile cleanly with x86_64-w64-mingw32-g++. .so size
8030576 (+2336 vs master); the growth is GetBatchPixelShader,
the worker entry point, and the thread/mutex member machinery.
25 retro_* exports unchanged.

Files touched:
  - src/core/types.h: new GPUShaderPrecompileMode enum
  - src/core/settings.{h,cpp}: gpu_shader_precompile_mode field,
    Parse/Get helpers, Load wiring
  - src/libretro/libretro_core_options.h: swanstation_GPU_ShaderPrecompile
  - src/common/d3d11/shader_cache.h: IsOpen() accessor
  - src/core/gpu_hw_d3d11.{h,cpp}: persistent shader cache and
    shadergen members, GetBatchPixelShader, worker thread
    machinery, three-way CompileShaders branch, DrawBatchVertices
    routes through the helper, DestroyShaders joins the worker.
2026-05-11 17:25:43 +02:00
CMakeModules Remove unused files 2023-03-25 17:43:35 +01:00
dep Make OpenBIOS a built-in fallback. 2026-03-28 10:43:30 +00:00
src gpu_hw_d3d11: introduce three-mode shader precompile core option 2026-05-11 17:25:43 +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 audio: devirtualize AudioStream, fold NullAudioStream into LibretroAudioStream 2026-05-08 15:07:36 +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.