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.
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.
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
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.
* 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>