Commit graph

166 commits

Author SHA1 Message Date
LibretroAdmin
778cb660d0 common/types: drop <cstring>, add explicit includes at the 41 callers
types.h carried <cstring> for transitive purposes only - the header
itself never references std::memcpy / std::memset / std::strlen
etc. 41 TUs across the tree used those C-string functions while
relying on types.h to supply the declaration.

Add an explicit '#include <cstring>' to each of those files and
drop it from types.h. The headers that also use cstring functions
(error.h, jit_code_buffer.h are not in the list because they
already include <cstring> directly, or use the C-namespace
<string.h> already in scope) are unaffected.

Files getting an explicit <cstring>:

  src/common:  cd_image.cpp, cd_image_ecm.cpp, cd_image_mds.cpp,
               cd_image_memory.cpp, cd_image_pbp.cpp,
               cd_image_ppf.cpp, cd_subchannel_replacement.cpp,
               cue_parser.cpp, d3d12/shader_cache.cpp,
               d3d12/staging_texture.cpp, d3d12/texture.cpp,
               d3d12/util.cpp, iso_reader.cpp, jit_code_buffer.cpp,
               md5_digest.cpp, vulkan/shader_cache.cpp,
               vulkan/staging_texture.cpp
  src/core:    bios.cpp, bios.h, bus.cpp, cdrom.cpp, cheats.cpp,
               cpu_code_cache.cpp, cpu_recompiler_code_generator.cpp,
               cpu_recompiler_register_cache.cpp, dma.cpp, gpu.cpp,
               gpu_hw.cpp, gpu_hw.h, gpu_hw_d3d11.cpp,
               gpu_hw_d3d12.cpp, gpu_hw_opengl.cpp,
               gpu_hw_vulkan.cpp, gpu_sw.cpp, gte.cpp, mdec.cpp,
               memory_card_image.cpp, pgxp.cpp, spu.cpp, system.cpp
  src/libretro: libretro_host_display.cpp

Most files get one '#include <cstring>' added into the existing
system-include block, sorted alphabetically. A handful of files
(system.cpp, memory_card_image.cpp, d3d12/shader_cache.cpp)
had pre-existing blank-line separations between the stdlib
include block and libretro third-party headers (<compat/strl.h>,
<file/file_path.h>); those separations are preserved.

Build: Makefile.libretro platform=unix - clean, no warnings.
swanstation_libretro.so size unchanged (8026128 bytes).
retro_* exports: 25 (unchanged).

types.h now only includes <cstddef>, <cstdint>, and <type_traits>
- the three headers it actually needs for its own contents.
2026-05-14 14:20:36 +02:00
LibretroAdmin
67051a0828 common: remove BoolToUInt8/16/32/64 helpers in favour of explicit casts
The BoolToUInt8/16/32/64 helpers in common/types.h are one-line
wrappers around `static_cast<uN>(bool)`. C++ already guarantees
that converting a bool to an unsigned integer yields exactly 0
(for false) or 1 (for true), so the helpers add nothing beyond a
shorter name - and a less obvious one, since 'BoolToUInt32(x)'
doesn't immediately read as 'cast to u32' the way the static_cast
spelling does.

Mechanical fold across 16 files:

  BoolToUInt8(x)   -> static_cast<u8>(x)
  BoolToUInt32(x)  -> static_cast<u32>(x)

(BoolToUInt16 and BoolToUInt64 had no callers in the tree.)

103 callsites rewritten: 82 BoolToUInt8 and 21 BoolToUInt32, spread
across gpu_commands.cpp, pgxp.cpp, gpu.cpp, gpu_hw_d3d11/d3d12/
vulkan/opengl.cpp, gpu_sw.cpp, gpu_hw.cpp, gpu.h, shadergen.cpp,
spu.cpp, analog_controller.cpp, negcon_rumble.cpp,
cpu_recompiler_code_generator.cpp, cpu_core.cpp. Most are array
indices (s_setup_time[BoolToUInt8(...)][...]), bit-packing in
shader-cache lookups, and arithmetic with vertex-count offsets
(words_per_vertex + BoolToUInt32(rc.texture_enable)).

After the rewrite, the four helper definitions are removed from
common/types.h.

Build verification:
  Makefile.libretro platform=unix - clean syntax-check on every
  touched .cpp that compiles on Linux. d3d11/d3d12 sites are all
  simple BoolToUInt8(v) -> static_cast<u8>(v) substitutions on
  bitfield values and verified by code review only.

No behaviour change. Codegen is identical at -O2 - both forms
compile to the same setcc/movzx sequence on x86-64.

Same shape of cleanup as the recently-landed SignExtend/Truncate
and ConvertToBoolUnchecked removals: kill a thin readability
wrapper, use the explicit static_cast spelling everywhere.
common/types.h shrinks by another 16 lines.
2026-05-14 06:08:13 +02:00
LibretroAdmin
e4b17615a9 common: remove SignExtend / ZeroExtend / Truncate helpers in favour of explicit casts
The src/common/types.h helpers SignExtend16/32/64, ZeroExtend16/32/64,
Truncate8/16/32, the generic SignExtend<TR,TV> and ZeroExtend<TR,TV>
templates, and SignExtendN<N,T> were thin wrappers around explicit
static_cast chains. They served a documentation role ("this cast is
intentional, not a typo") in places where the underlying intent was
"narrow this value" or "sign-extend this value", but in practice
they obscured rather than clarified - particularly when several
nested together (SignExtend32(Truncate8(x)) is just (u32)(s8)x).

Tree-wide fold:

  Truncate8(x)              -> static_cast<u8>(x)
  Truncate16(x)             -> static_cast<u16>(x)
  Truncate32(x)             -> static_cast<u32>(x)
  ZeroExtendN(u_x)          -> static_cast<uN>(u_x)
  SignExtendN(u_x)          -> static_cast<uN>(static_cast<sM>(u_x))   (M = source width)
  SignExtendN(s_x)          -> static_cast<uN>(s_x)                   (relies on int promotion)
  SignExtendN<BITS,T>(x)    -> static_cast<sN>(static_cast<uN>(x) << S) >> S   (S = N*8 - BITS)
  SignExtendK(TruncateM(x)) -> static_cast<uK>(static_cast<sM>(x))
  ZeroExtendK(TruncateM(x)) -> static_cast<uK>(static_cast<uM>(x))

Mask cleanup applied where the mask matched the truncation width:

  Truncate8(x & 0xFFu)            -> static_cast<u8>(x)
  Truncate8((x & 0xFF00u) >> 8)   -> static_cast<u8>(x >> 8)
  Truncate16(x & 0xFFFFu)         -> static_cast<u16>(x)
  Truncate16((x & 0xFFFF0000u) >> 16) -> static_cast<u16>(x >> 16)

The masks were dead-code: a static_cast to uN already keeps exactly
the low N bits.

UB fixes incidental to the fold (visible once the helpers were
unwrapped):

  - SignExtendN<NBITS,T> internally did (static_cast<signed_T>(v) << S) >> S.
    Signed left shift of a negative value is UB pre-C++20. After the
    fold the shift is done in the unsigned domain first:
      static_cast<sN>(static_cast<uN>(v) << S) >> S
    All five SignExtendN callsite expansions (gpu_commands.cpp x2,
    gpu_types.h, gte SignExtendMACResult, mdec.cpp x12) now do the
    left shift on uN. Defined behaviour on every C++ standard from
    C++98 onward. Right shift of a signed negative value remains
    implementation-defined pre-C++20; this is the same as the
    original helper and unchanged.

  - mdec.cpp:650's SignExtendN<9,s32> took an s64 expression and
    forced an implicit s64->int narrowing inside the template, which
    -Wconversion flagged. The explicit static_cast<u32>(...) now
    makes the narrow visible and the warning goes away. Bit-identical
    result.

Sites deliberately not collapsed further, with reasons:

  - spu.cpp:1347 (ADPCM pitch modulation): SignExtend32(step) where
    step is u16 deliberately produces a u32 with the s16 sign bit
    replicated, then multiplies by a u32 factor. Folding it as
    static_cast<u32>(static_cast<s16>(step)) preserves the semantics
    exactly. The naive 'static_cast<s32>(step) * factor' would not -
    it'd evaluate s32 * s32 which is signed and may overflow as UB.

  - gte.cpp:297-299 (IRGB conversion): Sign-extension here is observably
    equivalent to zero-extension because (value & 0x1F) * 0x80 is
    always <= 0xF80, bit 15 always clear. Folded as the literal
    static_cast<u32>(static_cast<s16>(static_cast<u16>(...))) rather
    than dropping the sign-extend step entirely - that would be a
    semantic claim about hardware behaviour beyond the scope of a
    helper-removal change.

  - GTE TruncateRGB, TruncateAndSetMAC, TruncateAndSetIR,
    TruncateAndSetMACAndIR (gte.cpp) and TruncateVertexPosition,
    TruncateGPUVertexPosition (pgxp.cpp, gpu_types.h): these are
    different functions, not the removed helpers. Left untouched.

bitutils.h's CountLeadingZeros / CountTrailingZeros are template
functions over T. The helper-removal leaves them inline-folding via
std::make_unsigned<T>::type, which is what ZeroExtend was doing
under the hood:

  ZeroExtend64(value) ->
  static_cast<u64>(static_cast<typename std::make_unsigned<T>::type>(value))

The remaining mechanical changes in cpu_recompiler_register_cache.h's
GetS32ConstantValue / GetS64ConstantValue and in pgxp.cpp's CPU_MULT
fold dropped redundant intermediate u-of-s-of-x casts that became
visible after the SignExtend64 expansion. Same codegen; cleaner.

Files touched: 43.
Helper definitions removed: 13 functions (~60 lines of types.h).

Build verification:
  - Makefile.libretro platform=unix: clean build, zero warnings,
    .so size 8031016 bytes (unchanged from HEAD).
  - 25 retro_* exports unchanged.
  - -Wall -Wextra on touched files: same warnings as HEAD (zero
    new warnings from the fold, all pre-existing -Wunused-parameter).
  - -Wconversion on touched files: one pre-existing warning
    eliminated (mdec.cpp:650 SignExtendN<9,s32>(s64_expr) narrowing
    is now an explicit static_cast<u32>); zero new warnings.

Codegen equivalence verified by inspection at every transformation
site:

  Truncate8(x)        = static_cast<u8>(x)   ; both emit a movzx/byte-load
  Truncate16(x)       = static_cast<u16>(x)  ; both emit a movzx/halfword-load
  ZeroExtend32(u8)    = static_cast<u32>(u8) ; both emit a movzx eax,al
  ZeroExtend64(u32)   = static_cast<u64>(u32); both emit a mov eax,reg (implicit zero-extend on x86-64)
  SignExtend32(u8)    = static_cast<u32>(static_cast<s8>(x))  ; both emit movsx eax,byte
  SignExtend32(u16)   = static_cast<u32>(static_cast<s16>(x)) ; both emit movsx eax,word
  SignExtend64(u32)   = static_cast<u64>(static_cast<s32>(x)) ; both emit movsxd rax,dword
  SignExtendN<11,s32> = static_cast<s32>(static_cast<u32>(x) << 21) >> 21
                        both emit shl ; sar, identical 2-instruction sequence
2026-05-14 05:41:46 +02:00
LibretroAdmin
5d73da3896 gpu_hw: trigger shader recompile on runtime swanstation_GPU_ShaderPrecompile change
Reported bug: flipping the swanstation_GPU_ShaderPrecompile core
option between Lazy / Enabled / Disabled at runtime had no
immediate effect. The new mode wouldn't take effect until the
user changed some OTHER shader-affecting setting (texture filter,
resolution scale, multisampling, etc.) that triggered the existing
destroy/recompile path.

Root cause: GPU_HW::UpdateHWSettings's shaders_changed predicate
checked 11 fields (resolution_scale, multisamples, true_color,
per_sample_shading, scaled_dithering, texture_filter, uv_limits,
chroma_smoothing, downsample_mode, pgxp_depth_buffer,
disable_color_perspective) but NOT g_settings.gpu_shader_precompile_mode.
So a pure precompile-mode flip computed shaders_changed = false,
the per-backend UpdateSettings skipped the DestroyPipelines /
CompilePipelines round-trip, and:

  - Lazy -> Disabled: the background worker that was already
    spawned just kept running until it naturally finished filling
    the matrix (~30 seconds on heavy filters). The user-visible
    'Disabled' semantics didn't apply until then.

  - Disabled -> Lazy: no worker spawn happened. Each draw kept
    paying first-use compile cost on the runloop. The user got
    Disabled behaviour while the setting said Lazy.

  - {Lazy, Disabled} -> Enabled: no synchronous full precompile
    happened. The user expected 'block until done' and got
    whatever state the matrix was in.

  - Enabled -> {Lazy, Disabled}: the matrix was already fully
    populated from the Enabled-mode sync pass, so this only had
    bearing on FUTURE recompile cycles. Still wrong but the
    least-visible case.

Fix: add m_shader_precompile_mode as the cached previous value
(seeded in GPU_HW::Initialize from g_settings.gpu_shader_precompile_mode,
re-stored every UpdateHWSettings call alongside the other field
copies). Include it in the shaders_changed predicate.

After this patch a runtime flip triggers shaders_changed = true,
which calls into the per-backend UpdateSettings's normal
destroy/recompile path:

  - Lazy -> Disabled: DestroyPipelines calls StopShaderCompileThread
    which joins the worker; CompilePipelines runs in Disabled
    mode (skips the precompile matrix, doesn't spawn a worker).
    The worker is gone within at most one in-flight cell's
    compile worth of latency.

  - Disabled -> Lazy: DestroyPipelines is a no-op for the worker
    (none running); CompilePipelines runs in Lazy mode (skips
    the precompile matrix, spawns a worker that fills the matrix
    asynchronously).

  - Disabled -> Enabled: full sync precompile on the calling
    thread - the documented Enabled-mode behaviour. The frontend
    blocks for tens of seconds on cold disk cache; cached, it's
    a few seconds.

  - Enabled -> Lazy / Disabled: matrix destroyed, recompiled
    according to the new mode. Symmetric with the other cases.

  - Lazy -> Enabled or vice versa with an in-flight worker:
    DestroyPipelines joins the in-flight worker first, then
    CompilePipelines starts fresh in the new mode.

Cost is one full destroy/recompile cycle on each precompile-mode
flip - same cost path the existing 11 settings already pay. For
Lazy and Disabled this is roughly 100-300 ms of teardown plus
non-batch shader rebuild. For Enabled it's that plus the actual
sync precompile of the batch matrix, dominated by driver compile
time (10-40s cold, sub-second from disk cache). The cell-fill
work hits the disk shader cache so it's only expensive on the
first time any given (filter, scale, ...) tuple is seen.

We could have written more precise handling - 'on Lazy ->
Disabled just stop the worker, keep the populated matrix' - but
that's a more complex patch and the simple destroy/recompile path
is well-trodden, well-tested, and the user already pays this cost
for any other shader-affecting setting change. Symmetry trumps
the small redundant work in the Lazy <-> Disabled case.

Files touched:
  - src/core/gpu_hw.h: m_shader_precompile_mode member added.
  - src/core/gpu_hw.cpp: Initialize seeds it from g_settings;
    UpdateHWSettings includes it in shaders_changed predicate
    and writes the latest value back.

Build verification: linux platform=unix clean, zero warnings,
zero errors. .so size 8040152 unchanged (one u8-sized field and
one extra comparison, well within an existing structure-padding
slot). 25 retro_* exports unchanged.
2026-05-11 20:37:35 +02:00
libretroadmin
521a3cb9b4 Get rid of more std::arrays 2024-04-17 12:39:47 +02:00
DarthMew
0175913d3e Reduce the amount of OSD messages 2023-03-21 17:56:39 +01:00
libretroadmin
c48ce29c50 Remove some unused functions 2023-03-09 18:32:46 +01:00
DarthMew
e6c53a15cd Revert 27c76b0a48 and fix underlying issue 2023-02-09 14:04:28 +01:00
DarthMew
27c76b0a48 Revert 28d4d33b2c 3bfe5959da 0d47de2dd7 & 826d58355c 2023-02-09 01:51:40 +01:00
DarthMew
3bfe5959da Don't allow disabling PGXP color perspective when using OpenGLES 2023-02-01 00:26:33 +01:00
DarthMew
826d58355c Backport PGXP perspective-correct color interpolation toggle & cleanup libretro_game_settings 2023-01-31 01:52:51 +01:00
LibretroAdmin
7ffe05afb4 Get rid of some align/log.h include 2022-09-12 01:13:23 +02:00
LibretroAdmin
d6c66e0d82 Remove common/assert.h 2022-09-11 10:12:54 +02:00
LibretroAdmin
048ac38a1b Remove DebugAssert 2022-09-08 07:58:20 +02:00
libretroadmin
3e5b4abaa1 Move some of the align pow2 functions that are only used for one file to
their respective proper locations
2022-07-22 21:20:00 +02:00
libretroadmin
a806334dba Remove UnreachableCode 2022-06-27 20:42:28 +02:00
libretroadmin
8af94fb6f7 - Simplify CalculateResolutionScale
- Turn IsPow2 into a macro
- JIT code buffer - Get rid of some of the logs
2022-06-26 03:03:37 +02:00
libretroadmin
3873ee74d7 - Get rid of unused GetEffectiveDisplayResolution/GetFullDisplayResolution
- Get rid of RendererStats
2022-06-24 20:06:14 +02:00
libretroadmin
f4b55de2a8 Reduce more reliance on file_system.h/FileSystem and remove
more logs
2022-06-24 08:23:21 +02:00
libretroadmin
3c0b044d91 Remove cpu_disasm.cpp/.h 2022-06-24 07:54:25 +02:00
libretroadmin
a644b95125 Remove WITH_IMGUI 2022-06-24 01:30:27 +02:00
DarthMew
b4c3d31f28 Revert https://github.com/libretro/swanstation/pull/43, fixes 'Use Software Renderer For Readbacks' 2022-01-21 21:14:41 +01:00
DarthMew
be4783514a reapply swanstation commits to latest upstream 2021-10-14 23:30:13 +02:00
Connor McLaughlin
628dc36e77 GPU: Add GPU::GetFullDisplayResolution() 2021-08-24 11:57:09 +10:00
Jake Stine
6566e665df 1X and 4X Software Up-Rendering Feature Added (#43)
* 2X and 4X Software Up-Rendering Feature Added

What this change includes:
 - 2X software uprender
 - 4X software uprender (mostly untested, but yolo)
 - significant perf improvements to all SW rendering, including native res
 - config setting '[GPU].SoftwareUprender'

What this change is missing:
 - UI integration. It can only be enabled from the settings.ini file at the moment
 - Testing. I only checked four games so far

* SW Uprender 2x Fixed.

Mainly two typos, one in SetPixel, and one in a template parameter, caused the problems.
I did a bunch of var renaming and cleanup along the way.

* Add preliminary support for on-the-fly resolution change, rename and refactor some vars

* SW uprender: simplified so HW/SW share same setting.

* Fixed crash caused by bad malloc from on-the-fly res change refactor

* Fixed another crash. Bad math typo.

* Fix mode bad math in ReadVRAM

* Fix 4x rendering (mostly) - gradients are off, but could be SW integer precision issue.

hooks up proper software display buffer texture allocation.

* Fix FMVs rendering on host 16bpp display buffers

This bug varies by user's PC hardware and OS. Hard to verify and reproduce. >_<

* Fix hardcoded upscale factor

* Reduce diff size a bit

Co-authored-by: Jean-André Santoni <jean.andre.santoni@gmail.com>
2021-07-29 17:22:51 +07:00
Connor McLaughlin
70209db402 GPU/HW: Implement oversized VRAM fills in hardware
Fixes downscaling in Bugs and Taz PAL.
2021-07-21 19:24:10 +10:00
Connor McLaughlin
096a92ba84 GPU/HW: Simplify shader compile progress updates 2021-07-11 13:21:41 +10:00
Connor McLaughlin
defce709da GPU/HW: Don't allow adaptive downsampling at non-pow2 scales 2021-06-25 00:19:14 +10:00
Connor McLaughlin
815146e7d8 GPU/HW: Fix incorrect blend factor
Regression from 75e6c175de
2021-06-17 20:04:05 +10:00
Connor McLaughlin
75e6c175de GPU/HW: Elide redundant UBO updates on draw mode change 2021-06-15 23:10:49 +10:00
Connor McLaughlin
179a7aea89 GPU/HW: Assume 240/288p for auto scale on start with all borders 2021-05-31 03:48:38 +10:00
Connor McLaughlin
245dd5b27a GPU/HW: Fix crash with oversized writes and sw readback 2021-05-20 14:14:26 +10:00
Connor McLaughlin
5d877250e9 GPU/HW: Add an option to use software renderer for readbacks 2021-05-20 13:28:36 +10:00
Connor McLaughlin
df5de7730c GPU/HW: Disable depth buffering on transparent polygons
These are the most likely to be coplanar and cause issues.
2021-03-30 01:40:05 +10:00
Connor McLaughlin
ddea2818d9 Qt: Add renderer/resolution to status bar 2021-03-28 13:47:53 +10:00
Connor McLaughlin
b13c51a2e2 GPU/SW: Use new 5-to-8 converters for RGBA8 display 2021-03-18 13:00:58 +10:00
Connor McLaughlin
c043643c4d GPU/HW: Fix typo in –IncludeVRAMDirtyRectangle() name 2021-02-07 20:00:16 +10:00
Connor McLaughlin
837fb6128b GPU/HW: Fix mask bit when rendering with transparency and no DSB
Fixes some sprites in Bloody Roar on Mali GPUs.
2021-02-06 20:14:28 +10:00
Connor McLaughlin
516d685dd0 System: Add memory-only save states and rewind 2021-01-25 01:15:45 +10:00
Connor McLaughlin
a927bfbe50 GPU/HW: Flush after each primitive for B-F transparency
Slow, but necessary for some games such as Suikoden II, where it draws
multiple polygons to the same area on screen with different transparency
modes.
2021-01-20 01:08:36 +10:00
Connor McLaughlin
40257e5b20 GPU/HW: Don't set m_pgxp_depth_buffer when PGXP is disabled
Fixes text corruption in Castlevania when PGXP is disabled and depth
buffer is enabled.
2021-01-01 17:49:20 +10:00
Connor McLaughlin
3cb2cd8235 GPU: Add adaptive and box downsampling modes 2020-12-30 17:41:39 +10:00
Connor McLaughlin
d187c8b08d GPU/HW: Fix a potential case where depth isn't cleared on viewport change 2020-12-26 00:47:03 +10:00
Connor McLaughlin
aa1543271e GPU: Support emulating a depth buffer from PGXP depth values 2020-12-23 01:39:49 +10:00
Connor McLaughlin
7b6d30a994 GPU/HW: Fix possible VRAM corruption when live switching resolutions
Mask bit was getting lost based on guest state.
2020-12-17 01:20:06 +10:00
Connor McLaughlin
5e05a14f97 GPU: Fix incorrect draw time approximation of lines
Fixes flickering pre-mission screen in Omega Boost.
2020-12-06 00:35:54 +10:00
Connor McLaughlin
b45bee5954 libretro: Work around crash in RetroArch Vulkan driver 2020-11-25 23:06:41 +10:00
Connor McLaughlin
07dbc5a80d GPU/HW: Draw opaque before semitransparent for reverse subtract
Fixes missing shadows in Suikoden II.
2020-11-22 13:46:20 +10:00
Connor McLaughlin
e06f85a328 GPU: Fix VRAM palette changes not being detected in hw renderer
Fixes wall texture animation in Mega Man Legends.
2020-11-22 13:22:29 +10:00
Connor McLaughlin
2595e31575 GPU: Split software to frontend/backend 2020-11-21 18:39:04 +10:00