diff --git a/.gitignore b/.gitignore index 4b7710f..6cf9652 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ api-projects/**/build*/ # Local state .wraplock .cache/ +__pycache__/ +*.pyc .superpowers/ Diagnostics/ CrashDumps/ diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index c0957f0..e3e1bb2 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -57,6 +57,15 @@ The exact notices from the build toolchain are preserved under The MinGW-w64 DirectX header notice is preserved at `backend/dxvk/include/native/directx/COPYING.MinGW-w64.txt`. +## Test-only Improved Streaming INI reader + +The offline streaming regression vendors the unmodified INI reader/parser from +JuniorDjjr/LoadWholeMap under `tests/vendor/loadwholemap-ini/`. Its pinned commit, +file hashes and coverage limits are recorded in that directory's `README.md`; +the original license is retained in `LICENSE.txt` and in the parser header. +These files are used by test executables only and are not linked into the +RenderStack runtime DLLs. + ## Distribution Rules - Keep this file with source and binary distributions. diff --git a/backend/dxvk/src/d3d9/d3d9_binding_fast_path.h b/backend/dxvk/src/d3d9/d3d9_binding_fast_path.h new file mode 100644 index 0000000..3ceda0e --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_binding_fast_path.h @@ -0,0 +1,17 @@ +#pragma once + +#include "../util/util_bit.h" + +namespace dxvk { + + // Return true only when a non-empty batch has been consumed by bindOne. + template + bool TryD3D9SingleBinding(uint32_t mask, const BindOne& bindOne) { + if (!mask || (mask & (mask - 1u))) + return false; + + bindOne(bit::tzcnt(mask)); + return true; + } + +} diff --git a/backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h b/backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h new file mode 100644 index 0000000..d9e6499 --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h @@ -0,0 +1,66 @@ +#pragma once + +#include + +// Private, copied-data diagnostics. Public API1-7 and lifecycle vtables do not +// change. Start/Stop/GetAudit run on the device's calling thread; Publish/GetHud +// additionally support concurrent copying from the HUD thread. No pointer in a +// snapshot refers to caller memory. Initialize StructSize and Version on reads. +inline constexpr GUID IID_D3D9BridgeDiagnostics = + {0x36f6d3a1,0xc28c,0x4dd0,{0x91,0xaf,0x23,0x1e,0x8b,0xa0,0xb2,0x03}}; +inline constexpr UINT D3D9_BRIDGE_DIAGNOSTICS_VERSION = 1; +inline constexpr UINT D3D9_BRIDGE_AUDIT_COUNTER_COUNT = 17; +inline constexpr UINT D3D9_BRIDGE_PREPARE_COUNTER_COUNT = 28; +inline constexpr UINT D3D9_BRIDGE_HUD_LINE_COUNT = 8; +inline constexpr UINT D3D9_BRIDGE_HUD_LINE_CAPACITY = 128; + +enum D3D9BridgeAuditStatus : UINT { + D3D9_BRIDGE_AUDIT_IDLE, D3D9_BRIDGE_AUDIT_ACTIVE, + D3D9_BRIDGE_AUDIT_COMPLETE, D3D9_BRIDGE_AUDIT_INTERRUPTED, + D3D9_BRIDGE_AUDIT_UNAVAILABLE, +}; + +struct D3D9BridgeAuditCounter { + UINT64 Calls, RedundantCalls, Units, RedundantUnits, QpcTicks, MaxQpcTicks; + // TimedCalls == 0 means no elapsed measurement was taken. A timed zero + // remains distinguishable from an unsupported/untimed category. Spans are + // device-locked API-body elapsed: lock acquisition wait is excluded. They + // include waits inside that body and may nest (e.g. state-block restore). + UINT64 TimedCalls; +}; +struct D3D9BridgePrepareCounter { UINT64 Draws, Units; }; + +struct D3D9BridgeAuditSnapshot { + UINT StructSize, Version; + UINT64 CaptureId; + UINT Status, Available, Active, DurationMs; + UINT64 ElapsedMs, ObservedPresentBoundaries, QpcFrequency; + D3D9BridgeAuditCounter Total; + // Order matches D3D9GtaSaStateAuditKind; timings are inclusive measured + // API spans, not additive frame costs. Present boundaries are attempts, + // never a successful-Present or FPS numerator. + D3D9BridgeAuditCounter Counters[D3D9_BRIDGE_AUDIT_COUNTER_COUNT]; + UINT64 SamplerBindingCacheHits, TextureBindingCacheHits; + UINT64 ResourceBindingCacheInvalidations, StateBlockFastSkips; + UINT64 PrepareDraws, PrepareCleanDraws, PrepareCategoryEvents; + UINT64 PrepareMaxCategoriesPerDraw; + D3D9BridgePrepareCounter Prepare[D3D9_BRIDGE_PREPARE_COUNTER_COUNT]; +}; + +struct D3D9BridgeHudSnapshot { + UINT StructSize, Version; + UINT64 CaptureId; + UINT Phase, Status, Visible, ProgressPermille; + UINT64 ElapsedMs, DurationMs; + UINT LineCount; + char Lines[D3D9_BRIDGE_HUD_LINE_COUNT][D3D9_BRIDGE_HUD_LINE_CAPACITY]; +}; + +struct ID3D9BridgeDiagnostics : IUnknown { + virtual HRESULT STDMETHODCALLTYPE SetCaptureOwner(BOOL) = 0; + virtual HRESULT STDMETHODCALLTYPE StartAudit(UINT64 captureId, UINT durationMs) = 0; + virtual HRESULT STDMETHODCALLTYPE StopAudit(UINT64 captureId) = 0; + virtual HRESULT STDMETHODCALLTYPE GetAudit(D3D9BridgeAuditSnapshot*) = 0; + virtual HRESULT STDMETHODCALLTYPE PublishHud(const D3D9BridgeHudSnapshot*) = 0; + virtual HRESULT STDMETHODCALLTYPE GetHud(D3D9BridgeHudSnapshot*) = 0; +}; diff --git a/backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h b/backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h new file mode 100644 index 0000000..a403e8c --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h @@ -0,0 +1,44 @@ +#pragma once + +#include + +// Private Bridge/backend integration. This is independent of public API1-7. +// Callbacks run synchronously on the calling device thread. The observer must +// outlive registration, must not retain the device, and may not throw. Clear +// registration serializes with callbacks. FinalRelease runs while the device +// is still usable, before its final public reference is retired. +inline constexpr GUID IID_D3D9BridgeLifecycle = + {0x36f6d3a1,0xc28c,0x4dd0,{0x91,0xaf,0x23,0x1e,0x8b,0xa0,0xb2,0x01}}; +inline constexpr GUID IID_D3D9BridgeRuntime = + {0x36f6d3a1,0xc28c,0x4dd0,{0x91,0xaf,0x23,0x1e,0x8b,0xa0,0xb2,0x02}}; + +enum class D3D9BridgeEvent : UINT { + PresentBefore, PresentAfter, ResetBefore, ResetAfter, FinalRelease +}; + +struct D3D9BridgePresentArgs { + const RECT* Source; + const RECT* Destination; + HWND Window; + const RGNDATA* DirtyRegion; +}; + +struct D3D9BridgeObserver { + UINT StructSize; + void* Context; + void (STDMETHODCALLTYPE* Notify)(void*, IDirect3DDevice9*, D3D9BridgeEvent, + HRESULT, D3DPRESENT_PARAMETERS*, const D3D9BridgePresentArgs*); +}; + +struct ID3D9BridgeLifecycle : IUnknown { + virtual HRESULT STDMETHODCALLTYPE SetObserver(const D3D9BridgeObserver*) = 0; +}; + +// Query on the Bridge IDirect3D9 factory. Shutdown is explicit and must be +// called outside callbacks/loader lock, after releasing all created devices. +// It returns D3DERR_INVALIDCALL while a device is live and E_FAIL when rollback +// needs a retry, E_PENDING for a concurrent shutdown, and S_FALSE after completed +// shutdown. An initialized Bridge stays module-pinned for stale hooks. +struct ID3D9BridgeRuntime : IUnknown { + virtual HRESULT STDMETHODCALLTYPE Shutdown() = 0; +}; diff --git a/backend/dxvk/src/d3d9/d3d9_bridge_thread_stats.h b/backend/dxvk/src/d3d9/d3d9_bridge_thread_stats.h new file mode 100644 index 0000000..4932d64 --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_bridge_thread_stats.h @@ -0,0 +1,22 @@ +#pragma once +#include + +// Private optional boundary query. Does not flush, synchronize, enable an +// audit, require GTA compatibility, or alter any public D3D9/API1-7 vtable. +inline constexpr GUID IID_D3D9BridgeThreadStats = + {0xd0967703,0x3642,0x49d2,{0x93,0xa8,0x91,0xa2,0xd1,0xe0,0x31,0x50}}; +struct D3D9BridgeThreadSnapshot { + UINT StructSize,Version,MainThreadId,CsThreadId,AvailableMask,Reserved; + // CPU is scheduled kernel+user execution, in 100ns units. Queue idle is + // wall time spent inside the CS condition wait, including an open wait. + // OrderedDispatched-OrderedCompleted includes in-flight work and excludes + // the high-priority/injected stream. Counters are lifetime totals. + UINT64 WallUs,MainCpu100ns,CsCpu100ns,CsQueueIdleUs,OrderedDispatched,OrderedCompleted; + // Existing CS/GPU wait counters count COMPLETED spans (possibly crossing + // a sampling boundary). Device-wide GPU waits are not necessarily main- + // thread waits. GPU idle is an estimate, not a timestamp-query GPU time. + UINT64 CsSyncCount,CsSyncUs,GpuSyncCount,GpuSyncUs,GpuIdleUs,Chunks,Submits; +}; +struct ID3D9BridgeThreadStats : IUnknown { + virtual HRESULT STDMETHODCALLTYPE GetThreadStats(D3D9BridgeThreadSnapshot*) = 0; +}; diff --git a/backend/dxvk/src/d3d9/d3d9_device.cpp b/backend/dxvk/src/d3d9/d3d9_device.cpp index 463df98..4e9698a 100644 --- a/backend/dxvk/src/d3d9/d3d9_device.cpp +++ b/backend/dxvk/src/d3d9/d3d9_device.cpp @@ -15,6 +15,7 @@ #include "d3d9_monitor.h" #include "d3d9_names.h" #include "d3d9_format_helpers.h" +#include "d3d9_binding_fast_path.h" #include "../dxvk/dxvk_adapter.h" #include "../dxvk/dxvk_instance.h" @@ -183,6 +184,321 @@ namespace dxvk { } + template + #if defined(__clang__) && defined(__i386__) + // Preserve the measured linked constant-control positions as diagnostics + // code grows. This prefix precedes the callable entry, so callers execute + // none of its NOPs. It fixes the VS-float loop's +16/mod64 placement while + // the PS/int loop alignment remains supplied by their existing code_align. + // tests/constant-layout-test.py checks the emitted loops, not this attribute. + __attribute__((aligned(64), patchable_function_entry(32, 32))) + #endif + HRESULT D3D9DeviceEx::SetShaderConstants( + UINT StartRegister, + const T* pConstantData, + UINT Count) { + uint32_t maxRegCount = DetermineSoftwareRegCount(); + + // Error out in case of StartRegister + Count overflow + if (unlikely(StartRegister > maxRegCount) || unlikely(Count > maxRegCount - StartRegister)) + return D3DERR_INVALIDCALL; + + if (unlikely(!Count)) + return D3D_OK; + + // Clamp count to the maximum that we are going to access in shaders. + // For PS or SWVP VS, constant counts won't change, so skip. + if (ShaderType == D3D9ShaderType::VertexShader && likely(!CanSWVP())) { + maxRegCount = DetermineHardwareRegCount(); + + if (unlikely(StartRegister >= maxRegCount)) + return D3D_OK; + + Count = std::min(maxRegCount - StartRegister, Count); + } + + if (unlikely(!pConstantData)) + return D3DERR_INVALIDCALL; + + if (unlikely(ShouldRecord())) { + return m_recorder->SetShaderConstants( + StartRegister, pConstantData, Count); + } + + if (GetOptions()->gtaSaConstantDedup && AreConstantsEqual( + &m_state, StartRegister, pConstantData, Count)) { + if constexpr (ShaderType == D3D9ShaderType::VertexShader) { + if constexpr (ConstantType == D3D9ConstantType::Float) + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexFloatConstants, true, Count); + else if constexpr (ConstantType == D3D9ConstantType::Int) + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexIntConstants, true, Count); + else + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexBoolConstants, true, Count); + } else { + if constexpr (ConstantType == D3D9ConstantType::Float) + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelFloatConstants, true, Count); + else if constexpr (ConstantType == D3D9ConstantType::Int) + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelIntConstants, true, Count); + else + m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelBoolConstants, true, Count); + } + return D3D_OK; + } + + if (unlikely(ShouldCaptureGtaSaStateJournal())) { + if constexpr (ShaderType == D3D9ShaderType::VertexShader) { + if constexpr (ConstantType == D3D9ConstantType::Float) + m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantF(StartRegister, Count); + else if constexpr (ConstantType == D3D9ConstantType::Int) + m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantI(StartRegister, Count); + else + m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantB(StartRegister, Count); + } else { + if constexpr (ConstantType == D3D9ConstantType::Float) + m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantF(StartRegister, Count); + else if constexpr (ConstantType == D3D9ConstantType::Int) + m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantI(StartRegister, Count); + else + m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantB(StartRegister, Count); + } + } + + // Clang 22 can drop the update scan's loop alignment metadata. Keep its + // measured device-only VS-int layout without changing mismatch weights. + // The linked-code gate checks the actual loop, not this preheader. + #if defined(__clang__) && defined(__i386__) + if constexpr (ShaderType == D3D9ShaderType::VertexShader && ConstantType == D3D9ConstantType::Int) + asm volatile (".p2align 4" : : : ); + #endif + + // Only mark constants as dirty if any of the actual data has changed + bool anyConstantDirty = UpdateStateConstants( + &m_state, StartRegister, pConstantData, Count); + + if constexpr (ShaderType == D3D9ShaderType::VertexShader) { + if constexpr (ConstantType == D3D9ConstantType::Float) { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::VertexFloatConstants, !anyConstantDirty, Count); + } else if constexpr (ConstantType == D3D9ConstantType::Int) { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::VertexIntConstants, !anyConstantDirty, Count); + } else { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::VertexBoolConstants, !anyConstantDirty, Count); + } + } else { + if constexpr (ConstantType == D3D9ConstantType::Float) { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::PixelFloatConstants, !anyConstantDirty, Count); + } else if constexpr (ConstantType == D3D9ConstantType::Int) { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::PixelIntConstants, !anyConstantDirty, Count); + } else { + m_gtaSaCompat.RecordStateAudit( + D3D9GtaSaStateAuditKind::PixelBoolConstants, !anyConstantDirty, Count); + } + } + + if (unlikely(!anyConstantDirty)) + return D3D_OK; + + NotifyGtaSaStateChanged(); + + D3D9ConstantSets& constSet = m_consts[uint32_t(ShaderType)]; + + if (ConstantType == D3D9ConstantType::Float) { + // Check whether the constant range has any effect on the bound shader. + // Also update the dynamic float count for vertex shaders, which is used + // to reduce the number of constants copied with dynamic indexing.q + constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.floatCount; + + if (ShaderType == D3D9ShaderType::VertexShader) + constSet.changedFloatCount = std::max(constSet.changedFloatCount, StartRegister + Count); + } else if (ConstantType == D3D9ConstantType::Int) { + // Same logic as above except we need to check against the used integer count. + constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.intCount; + } else if (ConstantType == D3D9ConstantType::Bool && ShaderType == D3D9ShaderType::VertexShader) { + // Bool constants are only backed by memory for SWVP vertex shaders + constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.boolCount && CanSWVP(); + } + + return D3D_OK; + } + + + template + HRESULT D3D9DeviceEx::SetShaderConstantsTimed( + UINT StartRegister, const T* pConstantData, UINT Count) { + // Callers already hold LockDevice. Keep QPC/counter guards and their + // live state out of the ordinary native setter's inactive stack frame. + constexpr D3D9GtaSaStateAuditKind kind = ShaderType == D3D9ShaderType::VertexShader + ? ConstantType == D3D9ConstantType::Float ? D3D9GtaSaStateAuditKind::VertexFloatConstants + : ConstantType == D3D9ConstantType::Int ? D3D9GtaSaStateAuditKind::VertexIntConstants + : D3D9GtaSaStateAuditKind::VertexBoolConstants + : ConstantType == D3D9ConstantType::Float ? D3D9GtaSaStateAuditKind::PixelFloatConstants + : ConstantType == D3D9ConstantType::Int ? D3D9GtaSaStateAuditKind::PixelIntConstants + : D3D9GtaSaStateAuditKind::PixelBoolConstants; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, kind); + return SetShaderConstants(StartRegister, pConstantData, Count); + } + + + ULONG STDMETHODCALLTYPE D3D9BridgeThreadStats::AddRef() { return m_device->AddRef(); } + ULONG STDMETHODCALLTYPE D3D9BridgeThreadStats::Release() { return m_device->Release(); } + HRESULT STDMETHODCALLTYPE D3D9BridgeThreadStats::QueryInterface(REFIID iid, void** output) { + return m_device->QueryInterface(iid, output); + } + HRESULT STDMETHODCALLTYPE D3D9BridgeThreadStats::GetThreadStats(D3D9BridgeThreadSnapshot* output) { + return m_device->GetBridgeThreadStats(output); + } + HRESULT D3D9DeviceEx::GetBridgeThreadStats(D3D9BridgeThreadSnapshot* output) { + if (!output || output->StructSize != sizeof(*output) || output->Version != 1) + return D3DERR_INVALIDCALL; + D3D9BridgeThreadSnapshot s = { }; + s.StructSize = sizeof(s); s.Version = 1; + const auto cs = m_csThread.getActivity(); + s.WallUs = cs.wallUs; s.CsCpu100ns = cs.cpu100ns; + s.CsQueueIdleUs = cs.queueIdleUs; s.CsThreadId = cs.threadId; + s.OrderedDispatched = cs.dispatched; s.OrderedCompleted = cs.completed; + s.AvailableMask = 4u | (cs.cpuAvailable ? 2u : 0u); +#ifdef _WIN32 + s.MainThreadId = GetCurrentThreadId(); + FILETIME created, exited, kernel, user; + if (GetThreadTimes(GetCurrentThread(), &created, &exited, &kernel, &user)) { + s.MainCpu100ns = (uint64_t(kernel.dwHighDateTime) << 32 | kernel.dwLowDateTime) + + (uint64_t(user.dwHighDateTime) << 32 | user.dwLowDateTime); + s.AvailableMask |= 1u; + } +#endif + const auto stats = m_dxvkDevice->getStatCounters(); + s.CsSyncCount = stats.getCtr(DxvkStatCounter::CsSyncCount); + s.CsSyncUs = stats.getCtr(DxvkStatCounter::CsSyncTicks); + s.GpuSyncCount = stats.getCtr(DxvkStatCounter::GpuSyncCount); + s.GpuSyncUs = stats.getCtr(DxvkStatCounter::GpuSyncTicks); + s.GpuIdleUs = stats.getCtr(DxvkStatCounter::GpuIdleTicks); + s.Chunks = stats.getCtr(DxvkStatCounter::CsChunkCount); + s.Submits = stats.getCtr(DxvkStatCounter::QueueSubmitCount); + *output = s; + return S_OK; + } + + ULONG STDMETHODCALLTYPE D3D9BridgeDiagnostics::AddRef() { return m_device->AddRef(); } + ULONG STDMETHODCALLTYPE D3D9BridgeDiagnostics::Release() { return m_device->Release(); } + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::QueryInterface(REFIID iid, void** output) { + return m_device->QueryInterface(iid, output); + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::SetCaptureOwner(BOOL owner) { + auto lock = m_device->LockDevice(); + if (!owner) + InterruptAudit("capture-owner-released"); + else if (!IsCaptureOwner() && m_device->GetGtaSaCompat()->IsStateAuditActive()) + m_device->GetGtaSaCompat()->FinishStateAudit("capture-owner-acquired"); + m_owner.store(owner != FALSE, std::memory_order_relaxed); + return S_OK; + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::StartAudit(UINT64 captureId, UINT durationMs) { + auto lock = m_device->LockDevice(); + if (!captureId || !durationMs || !IsCaptureOwner() || m_audit.Active) + return D3DERR_INVALIDCALL; + m_audit = { }; + m_audit.StructSize = sizeof(m_audit); + m_audit.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + m_audit.CaptureId = captureId; + m_audit.DurationMs = durationMs; + m_audit.Available = m_device->GetGtaSaCompat()->IsEnabled(); + if (!m_audit.Available) { + m_audit.Status = D3D9_BRIDGE_AUDIT_UNAVAILABLE; + return D3DERR_NOTAVAILABLE; + } + m_audit.Status = D3D9_BRIDGE_AUDIT_ACTIVE; + m_audit.Active = TRUE; + m_device->GetGtaSaCompat()->StartStateAudit("Bridge", durationMs); + return S_OK; + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::StopAudit(UINT64 captureId) { + auto lock = m_device->LockDevice(); + if (!captureId || captureId != m_audit.CaptureId) + return D3DERR_INVALIDCALL; + if (!m_audit.Active) + return S_FALSE; + m_device->GetGtaSaCompat()->FinishStateAudit("bridge-complete"); + return S_OK; + } + + void D3D9BridgeDiagnostics::InterruptAudit(const char* reason) { + if (m_audit.Active) + m_device->GetGtaSaCompat()->FinishStateAudit(reason); + } + + void D3D9BridgeDiagnostics::AuditFinished(const char* reason) { + if (!m_audit.Active) + return; + m_device->GetGtaSaCompat()->CopyStateAudit(m_audit); + m_audit.Active = FALSE; + m_audit.Status = reason && std::strcmp(reason, "bridge-complete") == 0 + ? D3D9_BRIDGE_AUDIT_COMPLETE : D3D9_BRIDGE_AUDIT_INTERRUPTED; + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::GetAudit(D3D9BridgeAuditSnapshot* output) { + if (!output) + return E_POINTER; + if (output->StructSize != sizeof(*output) || output->Version != D3D9_BRIDGE_DIAGNOSTICS_VERSION) + return D3DERR_INVALIDCALL; + auto lock = m_device->LockDevice(); + auto snapshot = m_audit; + snapshot.StructSize = sizeof(snapshot); + snapshot.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + if (!snapshot.CaptureId) { + snapshot.Available = m_device->GetGtaSaCompat()->IsEnabled(); + snapshot.Status = snapshot.Available ? D3D9_BRIDGE_AUDIT_IDLE : D3D9_BRIDGE_AUDIT_UNAVAILABLE; + } + if (snapshot.Active) + m_device->GetGtaSaCompat()->CopyStateAudit(snapshot); + *output = snapshot; + return S_OK; + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::PublishHud(const D3D9BridgeHudSnapshot* input) { + if (!input) + return E_POINTER; + if (input->StructSize != sizeof(*input) || input->Version != D3D9_BRIDGE_DIAGNOSTICS_VERSION + || input->LineCount > D3D9_BRIDGE_HUD_LINE_COUNT || input->ProgressPermille > 1000) + return D3DERR_INVALIDCALL; + for (UINT i = 0; i < input->LineCount; i++) { + const auto* end = static_cast(std::memchr(input->Lines[i], 0, D3D9_BRIDGE_HUD_LINE_CAPACITY)); + if (!end) + return D3DERR_INVALIDCALL; + for (const char* c = input->Lines[i]; c != end; ++c) { + if (static_cast(*c) < 32 || static_cast(*c) > 126) + return D3DERR_INVALIDCALL; + } + } + std::lock_guard lock(m_hudMutex); + m_hud = *input; + return S_OK; + } + + HRESULT STDMETHODCALLTYPE D3D9BridgeDiagnostics::GetHud(D3D9BridgeHudSnapshot* output) { + if (!output) + return E_POINTER; + if (output->StructSize != sizeof(*output) || output->Version != D3D9_BRIDGE_DIAGNOSTICS_VERSION) + return D3DERR_INVALIDCALL; + std::lock_guard lock(m_hudMutex); + *output = m_hud; + output->StructSize = sizeof(*output); + output->Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + if (!m_hud.StructSize && IsCaptureOwner()) { + output->Visible = TRUE; + output->LineCount = 1; + std::strcpy(output->Lines[0], "RenderStack ready: F8 / F7 capture"); + } + return S_OK; + } + + D3D9DeviceEx::D3D9DeviceEx( D3D9InterfaceEx* pParent, D3D9Adapter* pAdapter, @@ -214,7 +530,8 @@ namespace dxvk { , m_gtaSaCompat ( this, m_d3d9Options.gtaSaCompat, m_d3d9Options.gtaSaCompatDiagnostics ) , m_d3d9On12Args ( pAdapter->Get9On12Args() ) , m_d3d9On12 ( this ) - , m_d3d8Bridge ( this ) { + , m_d3d8Bridge ( this ) + , m_bridgeDiagnostics ( this ) { InitShaderOptions(); @@ -335,6 +652,21 @@ namespace dxvk { *ppvObject = nullptr; + if (riid == IID_D3D9BridgeThreadStats) { + *ppvObject = ref(&m_threadStats); + return S_OK; + } + + if (riid == IID_D3D9BridgeDiagnostics) { + *ppvObject = ref(&m_bridgeDiagnostics); + return S_OK; + } + + if (riid == IID_D3D9BridgeLifecycle) { + *ppvObject = ref(static_cast(this)); + return S_OK; + } + bool extended = m_parent->IsExtended() && riid == __uuidof(IDirect3DDevice9Ex); @@ -390,6 +722,38 @@ namespace dxvk { } + ULONG STDMETHODCALLTYPE D3D9DeviceEx::Release() { + uint32_t refs = m_refCount.load(); + do { + if (!refs) return 0; + } while (!m_refCount.compare_exchange_weak(refs, refs - 1)); + if (refs == 1) { + m_bridgeDiagnostics.InterruptAudit("device-final-release"); + NotifyBridge(D3D9BridgeEvent::FinalRelease); + ReleasePrivate(); + } + return refs - 1; + } + + HRESULT STDMETHODCALLTYPE D3D9DeviceEx::SetObserver(const D3D9BridgeObserver* observer) { + std::lock_guard lock(m_bridgeObserverMutex); + if (observer && (observer->StructSize < sizeof(*observer) + || !observer->Notify || m_bridgeObserver.Notify)) + return D3DERR_INVALIDCALL; + m_bridgeObserver = observer ? *observer : D3D9BridgeObserver{}; + return S_OK; + } + + void D3D9DeviceEx::NotifyBridge(D3D9BridgeEvent event, HRESULT result, + D3DPRESENT_PARAMETERS* params, const D3D9BridgePresentArgs* present) { + std::lock_guard lock(m_bridgeObserverMutex); + const auto observer = m_bridgeObserver; + if (event == D3D9BridgeEvent::FinalRelease) + m_bridgeObserver = {}; + if (observer.Notify) + observer.Notify(observer.Context, this, event, result, params, present); + } + HRESULT STDMETHODCALLTYPE D3D9DeviceEx::TestCooperativeLevel() { D3D9DeviceLock lock = LockDevice(); @@ -617,6 +981,7 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D9DeviceEx::Reset(D3DPRESENT_PARAMETERS* pPresentationParameters) { D3D9DeviceLock lock = LockDevice(); + NotifyBridge(D3D9BridgeEvent::ResetBefore, S_OK, pPresentationParameters); if (m_d3d9Options.gtaSaDeferShaderBinding) { InvalidateGtaSaDeferredShaderBinding(); @@ -625,6 +990,7 @@ namespace dxvk { m_gtaSaCompat.OnResetBegin(); auto finishReset = [this, pPresentationParameters] (HRESULT result) { m_gtaSaCompat.OnResetEnd(result, pPresentationParameters); + NotifyBridge(D3D9BridgeEvent::ResetAfter, result, pPresentationParameters); return result; }; @@ -1262,6 +1628,9 @@ namespace dxvk { Logger::warn("D3D9DeviceEx::UpdateTexture: Source dimensions exceed the destination"); } +#ifdef DXVK_USE_UNMAPPABLE_MEMORY + TextureUploadMappingScope mappingScope(*this, srcTexInfo); +#endif for (uint32_t a = 0; a < arraySlices; a++) { // The docs claim that the dirty box is just a performance optimization, however in practice games rely on it. const D3DBOX& box = srcTexInfo->GetDirtyBox(a); @@ -1858,6 +2227,8 @@ namespace dxvk { } if (m_state.viewport != viewport) { + if (unlikely(ShouldCaptureGtaSaStateJournal())) + m_gtaSaStateJournal->CaptureCurrentViewport(); m_dirty.set(D3D9DeviceDirtyFlag::FFViewport); m_dirty.set(D3D9DeviceDirtyFlag::PointScale); m_dirty.set(D3D9DeviceDirtyFlag::ViewportScissor); @@ -1866,6 +2237,8 @@ namespace dxvk { } if (m_state.scissorRect != scissorRect) { + if (unlikely(ShouldCaptureGtaSaStateJournal())) + m_gtaSaStateJournal->CaptureCurrentScissorRect(); m_dirty.set(D3D9DeviceDirtyFlag::ViewportScissor); m_state.scissorRect = scissorRect; gtaSaStateChanged = true; @@ -2543,6 +2916,7 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D9DeviceEx::SetRenderState(D3DRENDERSTATETYPE State, DWORD Value) { D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::RenderState); // D3D9 only allows reading for values 0 and 7-255 so we don't need to do anything but return OK if (unlikely(State > 255 || (State < D3DRS_ZENABLE && State != 0))) { @@ -3076,6 +3450,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->VertexFloatRangeCount; i++) { const auto& range = pBatch->VertexFloatRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexFloatConstants); result = SetShaderConstants< D3D9ShaderType::VertexShader, D3D9ConstantType::Float>( @@ -3085,6 +3460,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->VertexBoolRangeCount; i++) { const auto& range = pBatch->VertexBoolRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexBoolConstants); result = SetShaderConstants< D3D9ShaderType::VertexShader, D3D9ConstantType::Bool>( @@ -3094,6 +3470,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->PixelFloatRangeCount; i++) { const auto& range = pBatch->PixelFloatRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelFloatConstants); result = SetShaderConstants< D3D9ShaderType::PixelShader, D3D9ConstantType::Float>( @@ -3103,6 +3480,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->PixelBoolRangeCount; i++) { const auto& range = pBatch->PixelBoolRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelBoolConstants); result = SetShaderConstants< D3D9ShaderType::PixelShader, D3D9ConstantType::Bool>( @@ -3376,6 +3754,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->VertexFloatRangeCount; i++) { const auto& range = pBatch->VertexFloatRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexFloatConstants); result = SetShaderConstants< D3D9ShaderType::VertexShader, D3D9ConstantType::Float>( @@ -3385,6 +3764,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->VertexIntRangeCount; i++) { const auto& range = pBatch->VertexIntRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexIntConstants); result = SetShaderConstants< D3D9ShaderType::VertexShader, D3D9ConstantType::Int>( @@ -3394,6 +3774,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->VertexBoolRangeCount; i++) { const auto& range = pBatch->VertexBoolRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexBoolConstants); result = SetShaderConstants< D3D9ShaderType::VertexShader, D3D9ConstantType::Bool>( @@ -3403,6 +3784,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->PixelFloatRangeCount; i++) { const auto& range = pBatch->PixelFloatRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelFloatConstants); result = SetShaderConstants< D3D9ShaderType::PixelShader, D3D9ConstantType::Float>( @@ -3412,6 +3794,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->PixelIntRangeCount; i++) { const auto& range = pBatch->PixelIntRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelIntConstants); result = SetShaderConstants< D3D9ShaderType::PixelShader, D3D9ConstantType::Int>( @@ -3421,6 +3804,7 @@ namespace dxvk { for (UINT i = 0u; i < pBatch->PixelBoolRangeCount; i++) { const auto& range = pBatch->PixelBoolRanges[i]; + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelBoolConstants); result = SetShaderConstants< D3D9ShaderType::PixelShader, D3D9ConstantType::Bool>( @@ -3503,6 +3887,7 @@ namespace dxvk { m_dirty.set(D3D9DeviceDirtyFlag::FFVertexData); light = D3D9LightState(); + NotifyGtaSaStateChanged(); } @@ -3910,6 +4295,8 @@ namespace dxvk { || upStream.offset != 0u || upStream.length != 0u || upStream.stride != 0u) { + if (unlikely(ShouldCaptureGtaSaStateJournal())) + m_gtaSaStateJournal->CaptureCurrentStreamSource(0u); upStream.vertexBuffer = nullptr; upStream.offset = 0u; upStream.length = 0u; @@ -3990,6 +4377,13 @@ namespace dxvk { || upStream.stride != 0u; const bool indicesChanged = m_state.indices != nullptr; + if (unlikely(ShouldCaptureGtaSaStateJournal())) { + if (streamChanged) + m_gtaSaStateJournal->CaptureCurrentStreamSource(0u); + if (indicesChanged) + m_gtaSaStateJournal->CaptureCurrentIndices(); + } + upStream.vertexBuffer = nullptr; upStream.offset = 0u; upStream.length = 0u; @@ -4324,6 +4718,7 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D9DeviceEx::SetVertexShader(IDirect3DVertexShader9* pShader) { D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::VertexShader); D3D9VertexShader* shader = static_cast(pShader); @@ -4400,6 +4795,13 @@ namespace dxvk { const float* pConstantData, UINT Vector4fCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed< + D3D9ShaderType::VertexShader, + D3D9ConstantType::Float>( + StartRegister, + pConstantData, + Vector4fCount); return SetShaderConstants< D3D9ShaderType::VertexShader, @@ -4430,6 +4832,13 @@ namespace dxvk { const int* pConstantData, UINT Vector4iCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed< + D3D9ShaderType::VertexShader, + D3D9ConstantType::Int>( + StartRegister, + pConstantData, + Vector4iCount); return SetShaderConstants< D3D9ShaderType::VertexShader, @@ -4460,6 +4869,13 @@ namespace dxvk { const BOOL* pConstantData, UINT BoolCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed< + D3D9ShaderType::VertexShader, + D3D9ConstantType::Bool>( + StartRegister, + pConstantData, + BoolCount); return SetShaderConstants< D3D9ShaderType::VertexShader, @@ -4717,6 +5133,7 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D9DeviceEx::SetPixelShader(IDirect3DPixelShader9* pShader) { D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::PixelShader); D3D9PixelShader* shader = static_cast(pShader); @@ -4803,6 +5220,13 @@ namespace dxvk { const float* pConstantData, UINT Vector4fCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed < + D3D9ShaderType::PixelShader, + D3D9ConstantType::Float>( + StartRegister, + pConstantData, + Vector4fCount); return SetShaderConstants < D3D9ShaderType::PixelShader, @@ -4833,6 +5257,13 @@ namespace dxvk { const int* pConstantData, UINT Vector4iCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed< + D3D9ShaderType::PixelShader, + D3D9ConstantType::Int>( + StartRegister, + pConstantData, + Vector4iCount); return SetShaderConstants< D3D9ShaderType::PixelShader, @@ -4863,6 +5294,13 @@ namespace dxvk { const BOOL* pConstantData, UINT BoolCount) { D3D9DeviceLock lock = LockDevice(); + if (unlikely(m_gtaSaCompat.IsStateAuditActive())) + return SetShaderConstantsTimed< + D3D9ShaderType::PixelShader, + D3D9ConstantType::Bool>( + StartRegister, + pConstantData, + BoolCount); return SetShaderConstants< D3D9ShaderType::PixelShader, @@ -5064,6 +5502,9 @@ namespace dxvk { const RGNDATA* pDirtyRegion, DWORD dwFlags) { + const D3D9BridgePresentArgs present{pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion}; + NotifyBridge(D3D9BridgeEvent::PresentBefore, S_OK, nullptr, &present); + if (m_cursor.IsSoftwareCursor()) { D3D9_SOFTWARE_CURSOR* pSoftwareCursor = m_cursor.GetSoftwareCursor(); @@ -5086,12 +5527,14 @@ namespace dxvk { } } - return m_implicitSwapchain->Present( + const HRESULT result = m_implicitSwapchain->Present( pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags); + NotifyBridge(D3D9BridgeEvent::PresentAfter, result, nullptr, &present); + return result; } @@ -5306,6 +5749,7 @@ namespace dxvk { D3DPRESENT_PARAMETERS* pPresentationParameters, D3DDISPLAYMODEEX* pFullscreenDisplayMode) { D3D9DeviceLock lock = LockDevice(); + NotifyBridge(D3D9BridgeEvent::ResetBefore, S_OK, pPresentationParameters); if (m_d3d9Options.gtaSaDeferShaderBinding) { InvalidateGtaSaDeferredShaderBinding(); @@ -5314,6 +5758,7 @@ namespace dxvk { m_gtaSaCompat.OnResetBegin(); auto finishReset = [this, pPresentationParameters] (HRESULT result) { m_gtaSaCompat.OnResetEnd(result, pPresentationParameters); + NotifyBridge(D3D9BridgeEvent::ResetAfter, result, pPresentationParameters); return result; }; @@ -5388,6 +5833,7 @@ namespace dxvk { D3DSAMPLERSTATETYPE Type, DWORD Value) { D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::SamplerState); if (unlikely(ShouldRecord())) return m_recorder->SetStateSamplerState(StateSampler, Type, Value); @@ -5404,15 +5850,24 @@ namespace dxvk { if (unlikely(ShouldCaptureGtaSaStateJournal())) m_gtaSaStateJournal->CaptureCurrentStateSamplerState(StateSampler, Type); + const DWORD oldValue = state[StateSampler][Type]; state[StateSampler][Type] = Value; NotifyGtaSaStateChanged(); const uint32_t samplerBit = 1u << StateSampler; - m_textureSlotTracking.samplerStateDirty |= samplerBit; + // Only D3D9SamplerInfo fields affect sampler creation. Keep ignored state + // stored/captured, and defer an unused border color until border is enabled. + if (Type <= D3DSAMP_MAXANISOTROPY + && (Type != D3DSAMP_BORDERCOLOR || SamplerUsesBorderColor(StateSampler))) + m_textureSlotTracking.samplerStateDirty |= samplerBit; - if (Type == D3DSAMP_SRGBTEXTURE && (m_textureSlotTracking.bound & samplerBit)) + if (Type == D3DSAMP_SRGBTEXTURE && ((oldValue ^ Value) & 1u) + && (m_textureSlotTracking.bound & samplerBit)) { m_textureSlotTracking.textureDirty |= samplerBit; + if (SamplerUsesBorderColor(StateSampler)) + m_textureSlotTracking.samplerStateDirty |= samplerBit; + } constexpr DWORD Fetch4Enabled = MAKEFOURCC('G', 'E', 'T', '4'); constexpr DWORD Fetch4Disabled = MAKEFOURCC('G', 'E', 'T', '1'); @@ -5435,6 +5890,7 @@ namespace dxvk { HRESULT D3D9DeviceEx::SetStateTexture(DWORD StateSampler, IDirect3DBaseTexture9* pTexture) { D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::Texture); if (unlikely(ShouldRecord())) return m_recorder->SetStateTexture(StateSampler, pTexture); @@ -5480,15 +5936,17 @@ namespace dxvk { DWORD oldUsage = oldTexture != nullptr ? oldTexture->Desc()->Usage : 0; DWORD newUsage = newTexture != nullptr ? newTexture->Desc()->Usage : 0; DWORD combinedUsage = oldUsage | newUsage; + // Replacing the private binding may destroy the old texture. + const bool formatChanged = !oldTexture || !newTexture + || oldTexture->Desc()->Format != newTexture->Desc()->Format; TextureChangePrivate(m_state.textures[StateSampler], pTexture); NotifyGtaSaStateChanged(); m_textureSlotTracking.textureDirty |= 1u << StateSampler; - UpdateTextureBitmasks(StateSampler, combinedUsage); + UpdateTextureBitmasks(StateSampler, newTexture, combinedUsage); // If the texture format changes and the corresponding sampler uses // border colors, we may need to update the border color swizzle - if ((!oldTexture || !newTexture || oldTexture->Desc()->Format != newTexture->Desc()->Format) - && SamplerUsesBorderColor(StateSampler)) + if (formatChanged && SamplerUsesBorderColor(StateSampler)) m_textureSlotTracking.samplerStateDirty |= 1u << StateSampler; return D3D_OK; @@ -5535,6 +5993,7 @@ namespace dxvk { Type = std::min(Type, D3D9TextureStageStateTypes(DXVK_TSS_COUNT - 1)); D3D9DeviceLock lock = LockDevice(); + D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::TextureStageState); if (unlikely(ShouldRecord())) return m_recorder->SetStateTextureStageState(Stage, Type, Value); @@ -7093,7 +7552,7 @@ namespace dxvk { } - inline void D3D9DeviceEx::UpdateTextureBitmasks(uint32_t index, DWORD combinedUsage) { + inline void D3D9DeviceEx::UpdateTextureBitmasks(uint32_t index, D3D9CommonTexture* tex, DWORD combinedUsage) { const uint32_t bit = 1 << index; m_textureSlotTracking.rtUsage &= ~bit; @@ -7102,8 +7561,6 @@ namespace dxvk { m_textureSlotTracking.needsUpload &= ~bit; m_textureSlotTracking.needsMipGen &= ~bit; - auto tex = GetCommonTexture(m_state.textures[index]); - if (likely(IsPSSampler(index))) { const uint32_t textureType = tex != nullptr ? uint32_t(tex->GetType() - D3DRTYPE_TEXTURE) @@ -7360,6 +7817,9 @@ namespace dxvk { void D3D9DeviceEx::UploadManagedTexture(D3D9CommonTexture* pResource) { +#ifdef DXVK_USE_UNMAPPABLE_MEMORY + TextureUploadMappingScope mappingScope(*this, pResource); +#endif for (uint32_t subresource = 0; subresource < pResource->CountSubresources(); subresource++) { if (!pResource->NeedsUpload(subresource)) continue; @@ -8204,10 +8664,9 @@ namespace dxvk { const std::array samplerState = m_state.samplerStates[Sampler]; - Rc imageView; - - if (tex && SamplerUsesBorderColor(Sampler)) - imageView = tex->GetSampleView(srgb); + const Rc noView; + const auto& imageView = tex && SamplerUsesBorderColor(Sampler) + ? tex->GetSampleView(srgb) : noView; if (!PrepareGtaSaSamplerBinding( Sampler, @@ -8226,7 +8685,7 @@ namespace dxvk { cIsCube = tex && tex->IsCube(), cIsMultiMip = tex && (tex->Desc()->MipLevels > 1u), cIsDepth = bool(m_textureSlotTracking.depth & (1u << Sampler)), - cView = std::move(imageView), + cView = imageView, cBindId = m_samplerBindCount ] (DxvkContext* ctx) { DxvkSamplerKey key = { }; @@ -8294,14 +8753,14 @@ namespace dxvk { void D3D9DeviceEx::BindTexture(DWORD StateSampler) { bool srgb = m_state.samplerStates[StateSampler][D3DSAMP_SRGBTEXTURE] & 0x1; D3D9CommonTexture* commonTex = GetCommonTexture(m_state.textures[StateSampler]); - Rc imageView = commonTex->GetSampleView(srgb); + const auto& imageView = commonTex->GetSampleView(srgb); if (!PrepareGtaSaTextureBinding(StateSampler, imageView)) return; EmitCs([ cSlot = StateSampler, - cImageView = std::move(imageView) + cImageView = imageView ](DxvkContext* ctx) mutable { auto [stage, slot] = D3D9ShaderResourceMapping::getTextureSlotInfo(cSlot); ctx->bindResourceImageView(stage, slot, std::move(cImageView)); @@ -8310,6 +8769,11 @@ namespace dxvk { void D3D9DeviceEx::BindSamplerBatch(uint32_t mask) { + // A single dirty slot needs only the existing compact command. Avoid + // constructing and capturing the full SamplerCount array for that case. + if (TryD3D9SingleBinding(mask, [this](uint32_t slot) { BindSampler(slot); })) + return; + struct PendingSampler { DWORD slot = 0u; D3D9SamplerInfo state = D3D9SamplerInfo(std::array { }); @@ -8329,9 +8793,9 @@ namespace dxvk { const std::array samplerState = m_state.samplerStates[i]; - Rc imageView; - if (tex && SamplerUsesBorderColor(i)) - imageView = tex->GetSampleView(srgb); + const Rc noView; + const auto& imageView = tex && SamplerUsesBorderColor(i) + ? tex->GetSampleView(srgb) : noView; if (!PrepareGtaSaSamplerBinding( i, @@ -8350,7 +8814,7 @@ namespace dxvk { binding.isCube = tex && tex->IsCube(); binding.isMultiMip = tex && (tex->Desc()->MipLevels > 1u); binding.isDepth = bool(m_textureSlotTracking.depth & (1u << i)); - binding.view = std::move(imageView); + binding.view = imageView; binding.bindId = m_samplerBindCount; } @@ -8429,6 +8893,9 @@ namespace dxvk { void D3D9DeviceEx::BindTextureBatch(uint32_t mask) { + if (TryD3D9SingleBinding(mask, [this](uint32_t slot) { BindTexture(slot); })) + return; + struct PendingTexture { DWORD slot = 0u; Rc view; @@ -8440,14 +8907,14 @@ namespace dxvk { for (uint32_t i : bit::BitMask(mask)) { bool srgb = m_state.samplerStates[i][D3DSAMP_SRGBTEXTURE] & 0x1; D3D9CommonTexture* commonTex = GetCommonTexture(m_state.textures[i]); - Rc imageView = commonTex->GetSampleView(srgb); + const auto& imageView = commonTex->GetSampleView(srgb); if (!PrepareGtaSaTextureBinding(i, imageView)) continue; auto& binding = bindings[bindingCount++]; binding.slot = i; - binding.view = std::move(imageView); + binding.view = imageView; } if (bindingCount == 0u) @@ -9245,6 +9712,10 @@ namespace dxvk { void D3D9DeviceEx::SetVertexBoolBitfield(uint32_t idx, uint32_t mask, uint32_t bits) { + if (unlikely(ShouldCaptureGtaSaStateJournal())) { + for (uint32_t bit : bit::BitMask(mask)) + m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantB(idx * 32u + bit, 1u); + } m_state.vsConsts->bConsts[idx] &= ~mask; m_state.vsConsts->bConsts[idx] |= bits & mask; NotifyGtaSaStateChanged(); @@ -9254,6 +9725,10 @@ namespace dxvk { void D3D9DeviceEx::SetPixelBoolBitfield(uint32_t idx, uint32_t mask, uint32_t bits) { + if (unlikely(ShouldCaptureGtaSaStateJournal())) { + for (uint32_t bit : bit::BitMask(mask)) + m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantB(idx * 32u + bit, 1u); + } m_state.psConsts->bConsts[idx] &= ~mask; m_state.psConsts->bConsts[idx] |= bits & mask; NotifyGtaSaStateChanged(); @@ -9434,132 +9909,6 @@ namespace dxvk { } - template - HRESULT D3D9DeviceEx::SetShaderConstants( - UINT StartRegister, - const T* pConstantData, - UINT Count) { - uint32_t maxRegCount = DetermineSoftwareRegCount(); - - // Error out in case of StartRegister + Count overflow - if (unlikely(StartRegister > maxRegCount) || unlikely(Count > maxRegCount - StartRegister)) - return D3DERR_INVALIDCALL; - - if (unlikely(!Count)) - return D3D_OK; - - // Clamp count to the maximum that we are going to access in shaders. - // For PS or SWVP VS, constant counts won't change, so skip. - if (ShaderType == D3D9ShaderType::VertexShader && likely(!CanSWVP())) { - maxRegCount = DetermineHardwareRegCount(); - - if (unlikely(StartRegister >= maxRegCount)) - return D3D_OK; - - Count = std::min(maxRegCount - StartRegister, Count); - } - - if (unlikely(!pConstantData)) - return D3DERR_INVALIDCALL; - - if (unlikely(ShouldRecord())) { - return m_recorder->SetShaderConstants( - StartRegister, pConstantData, Count); - } - - if (GetOptions()->gtaSaConstantDedup && AreConstantsEqual( - &m_state, StartRegister, pConstantData, Count)) { - if constexpr (ShaderType == D3D9ShaderType::VertexShader) { - if constexpr (ConstantType == D3D9ConstantType::Float) - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexFloatConstants, true, Count); - else if constexpr (ConstantType == D3D9ConstantType::Int) - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexIntConstants, true, Count); - else - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::VertexBoolConstants, true, Count); - } else { - if constexpr (ConstantType == D3D9ConstantType::Float) - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelFloatConstants, true, Count); - else if constexpr (ConstantType == D3D9ConstantType::Int) - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelIntConstants, true, Count); - else - m_gtaSaCompat.RecordStateAudit(D3D9GtaSaStateAuditKind::PixelBoolConstants, true, Count); - } - return D3D_OK; - } - - if (unlikely(ShouldCaptureGtaSaStateJournal())) { - if constexpr (ShaderType == D3D9ShaderType::VertexShader) { - if constexpr (ConstantType == D3D9ConstantType::Float) - m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantF(StartRegister, Count); - else if constexpr (ConstantType == D3D9ConstantType::Int) - m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantI(StartRegister, Count); - else - m_gtaSaStateJournal->CaptureCurrentVertexShaderConstantB(StartRegister, Count); - } else { - if constexpr (ConstantType == D3D9ConstantType::Float) - m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantF(StartRegister, Count); - else if constexpr (ConstantType == D3D9ConstantType::Int) - m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantI(StartRegister, Count); - else - m_gtaSaStateJournal->CaptureCurrentPixelShaderConstantB(StartRegister, Count); - } - } - - // Only mark constants as dirty if any of the actual data has changed - bool anyConstantDirty = UpdateStateConstants( - &m_state, StartRegister, pConstantData, Count); - - if constexpr (ShaderType == D3D9ShaderType::VertexShader) { - if constexpr (ConstantType == D3D9ConstantType::Float) { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::VertexFloatConstants, !anyConstantDirty, Count); - } else if constexpr (ConstantType == D3D9ConstantType::Int) { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::VertexIntConstants, !anyConstantDirty, Count); - } else { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::VertexBoolConstants, !anyConstantDirty, Count); - } - } else { - if constexpr (ConstantType == D3D9ConstantType::Float) { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::PixelFloatConstants, !anyConstantDirty, Count); - } else if constexpr (ConstantType == D3D9ConstantType::Int) { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::PixelIntConstants, !anyConstantDirty, Count); - } else { - m_gtaSaCompat.RecordStateAudit( - D3D9GtaSaStateAuditKind::PixelBoolConstants, !anyConstantDirty, Count); - } - } - - if (unlikely(!anyConstantDirty)) - return D3D_OK; - - NotifyGtaSaStateChanged(); - - D3D9ConstantSets& constSet = m_consts[uint32_t(ShaderType)]; - - if (ConstantType == D3D9ConstantType::Float) { - // Check whether the constant range has any effect on the bound shader. - // Also update the dynamic float count for vertex shaders, which is used - // to reduce the number of constants copied with dynamic indexing.q - constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.floatCount; - - if (ShaderType == D3D9ShaderType::VertexShader) - constSet.changedFloatCount = std::max(constSet.changedFloatCount, StartRegister + Count); - } else if (ConstantType == D3D9ConstantType::Int) { - // Same logic as above except we need to check against the used integer count. - constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.intCount; - } else if (ConstantType == D3D9ConstantType::Bool && ShaderType == D3D9ShaderType::VertexShader) { - // Bool constants are only backed by memory for SWVP vertex shaders - constSet.dirty |= StartRegister < constSet.shaderConstantsInfo.boolCount && CanSWVP(); - } - - return D3D_OK; - } - - void D3D9DeviceEx::UpdateFixedFunctionVS() { bool hasPositionT = m_state.vertexDecl != nullptr && m_state.vertexDecl->TestFlag(D3D9VertexDeclFlag::HasPositionT); bool hasBlendWeight = m_state.vertexDecl != nullptr && m_state.vertexDecl->TestFlag(D3D9VertexDeclFlag::HasBlendWeight); @@ -10529,7 +10878,8 @@ namespace dxvk { auto iter = m_mappedTextures.leastRecentlyUsedIter(); while (m_memoryAllocator.getStats().memoryMapped >= threshold && iter != m_mappedTextures.leastRecentlyUsedEndIter()) { - if (unlikely((*iter)->IsAnySubresourceLocked() != 0)) { + if (unlikely((*iter)->IsAnySubresourceLocked() != 0 + || (m_textureUploadMappingScope && m_textureUploadMappingScope->Contains(*iter)))) { iter++; continue; } diff --git a/backend/dxvk/src/d3d9/d3d9_device.h b/backend/dxvk/src/d3d9/d3d9_device.h index fb76c48..0538d39 100644 --- a/backend/dxvk/src/d3d9/d3d9_device.h +++ b/backend/dxvk/src/d3d9/d3d9_device.h @@ -5,6 +5,7 @@ #include "../dxvk/dxvk_staging.h" #include "d3d9_include.h" +#include "d3d9_bridge_thread_stats.h" #include "d3d9_cursor.h" #include "d3d9_format.h" #include "d3d9_multithread.h" @@ -28,6 +29,9 @@ #include #include #include "d3d9_bridge.h" +#include "d3d9_bridge_lifecycle.h" +#include "d3d9_bridge_diagnostics.h" +#include #include #include @@ -40,6 +44,44 @@ namespace dxvk { + class D3D9DeviceEx; + + class D3D9BridgeThreadStats final : public ID3D9BridgeThreadStats { + public: + explicit D3D9BridgeThreadStats(D3D9DeviceEx* device) : m_device(device) { } + ULONG STDMETHODCALLTYPE AddRef() override; + ULONG STDMETHODCALLTYPE Release() override; + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) override; + HRESULT STDMETHODCALLTYPE GetThreadStats(D3D9BridgeThreadSnapshot*) override; + private: + D3D9DeviceEx* m_device; + }; + + // Delegating private interface: appending this helper preserves all existing + // D3D9DeviceEx member offsets and never takes a self-reference. + class D3D9BridgeDiagnostics final : public ID3D9BridgeDiagnostics { + public: + explicit D3D9BridgeDiagnostics(D3D9DeviceEx* device) : m_device(device) { } + ULONG STDMETHODCALLTYPE AddRef() override; + ULONG STDMETHODCALLTYPE Release() override; + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) override; + HRESULT STDMETHODCALLTYPE SetCaptureOwner(BOOL) override; + HRESULT STDMETHODCALLTYPE StartAudit(UINT64, UINT) override; + HRESULT STDMETHODCALLTYPE StopAudit(UINT64) override; + HRESULT STDMETHODCALLTYPE GetAudit(D3D9BridgeAuditSnapshot*) override; + HRESULT STDMETHODCALLTYPE PublishHud(const D3D9BridgeHudSnapshot*) override; + HRESULT STDMETHODCALLTYPE GetHud(D3D9BridgeHudSnapshot*) override; + bool IsCaptureOwner() const { return m_owner.load(std::memory_order_relaxed); } + void AuditFinished(const char* reason); + void InterruptAudit(const char* reason); + private: + D3D9DeviceEx* m_device; + std::atomic m_owner = { false }; + D3D9BridgeAuditSnapshot m_audit = { }; + std::mutex m_hudMutex; + D3D9BridgeHudSnapshot m_hud = { }; + }; + class D3D9InterfaceEx; class D3D9SwapChainEx; class D3D9CommonTexture; @@ -214,7 +256,7 @@ namespace dxvk { uint16_t instanced = 0; }; - class D3D9DeviceEx final : public ComObjectClamp { + class D3D9DeviceEx final : public ComObjectClamp, public ID3D9BridgeLifecycle { constexpr static uint32_t DefaultFrameLatency = 3; constexpr static uint32_t MaxFrameLatency = 20; @@ -248,6 +290,15 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject); + ULONG STDMETHODCALLTYPE AddRef() override { + return ComObjectClamp::AddRef(); + } + + ULONG STDMETHODCALLTYPE Release() override; + HRESULT STDMETHODCALLTYPE SetObserver(const D3D9BridgeObserver* observer) override; + void NotifyBridge(D3D9BridgeEvent event, HRESULT result = S_OK, + D3DPRESENT_PARAMETERS* params = nullptr, const D3D9BridgePresentArgs* present = nullptr); + HRESULT STDMETHODCALLTYPE TestCooperativeLevel(); UINT STDMETHODCALLTYPE GetAvailableTextureMem(); @@ -939,7 +990,7 @@ namespace dxvk { template void UpdateAnyColorWrites(); - void UpdateTextureBitmasks(uint32_t index, DWORD combinedUsage); + void UpdateTextureBitmasks(uint32_t index, D3D9CommonTexture* tex, DWORD combinedUsage); void UpdateActiveHazardsRT(uint32_t texMask); @@ -1127,6 +1178,13 @@ namespace dxvk { return &m_gtaSaCompat; } + D3D9BridgeDiagnostics* GetBridgeDiagnostics() { return &m_bridgeDiagnostics; } + HRESULT GetBridgeThreadStats(D3D9BridgeThreadSnapshot* output); + + bool GetBridgeHudSnapshot(D3D9BridgeHudSnapshot* snapshot) { + return SUCCEEDED(m_bridgeDiagnostics.GetHud(snapshot)) && snapshot->Visible; + } + HRESULT SubmitGtaSaStateBatch( const D3D9GtaSaStateBatch* pBatch); @@ -1491,6 +1549,20 @@ namespace dxvk { const T* pConstantData, UINT Count); + template < + D3D9ShaderType ShaderType, + D3D9ConstantType ConstantType, + typename T> + #if defined(__GNUC__) || defined(__clang__) + __attribute__((noinline, cold)) + #elif defined(_MSC_VER) + __declspec(noinline) + #endif + HRESULT SetShaderConstantsTimed( + UINT StartRegister, + const T* pConstantData, + UINT Count); + template < D3D9ShaderType ShaderType, D3D9ConstantType ConstantType, @@ -1708,6 +1780,8 @@ namespace dxvk { Com m_recorder; Com m_gtaSaStateJournal; + std::recursive_mutex m_bridgeObserverMutex; + D3D9BridgeObserver m_bridgeObserver = {}; Com m_gtaSaStateJournalCache; bool m_gtaSaStateJournalSelective = false; bool m_gtaSaStateJournalCaptureEnabled = false; @@ -1836,6 +1910,38 @@ namespace dxvk { D3D9SwapChainEx* m_mostRecentlyUsedSwapchain = nullptr; #ifdef DXVK_USE_UNMAPPABLE_MEMORY + // Keep active sources mapped across their mip/face uploads. Other LRU + // mappings remain reclaimable after every subresource. + class TextureUploadMappingScope { + public: + TextureUploadMappingScope(D3D9DeviceEx& device, D3D9CommonTexture* source) + : m_device(device), m_source(source), m_previous(device.m_textureUploadMappingScope) { + m_device.m_textureUploadMappingScope = this; + } + + ~TextureUploadMappingScope() { + m_device.m_textureUploadMappingScope = m_previous; + m_device.UnmapTextures(); + } + + bool Contains(D3D9CommonTexture* texture) const { + for (auto scope = this; scope; scope = scope->m_previous) { + if (scope->m_source == texture) + return true; + } + return false; + } + + TextureUploadMappingScope(const TextureUploadMappingScope&) = delete; + TextureUploadMappingScope& operator=(const TextureUploadMappingScope&) = delete; + + private: + D3D9DeviceEx& m_device; + D3D9CommonTexture* m_source; + const TextureUploadMappingScope* m_previous; + }; + + const TextureUploadMappingScope* m_textureUploadMappingScope = nullptr; lru_list m_mappedTextures; #endif @@ -1870,6 +1976,9 @@ namespace dxvk { std::atomic m_lastSamplerStats = { 0u }; bool m_unlockAdditionalFormats = false; + + D3D9BridgeDiagnostics m_bridgeDiagnostics; + D3D9BridgeThreadStats m_threadStats { this }; }; } diff --git a/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.cpp b/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.cpp index 2b70a3b..a48819b 100644 --- a/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.cpp +++ b/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.cpp @@ -316,15 +316,22 @@ namespace dxvk { m_presentCount.fetch_add(1u, std::memory_order_relaxed); - if (!m_diagnostics) - return; - if (m_stateAuditActive) { m_stateAuditFrames += 1u; - if (GetTickCount64() - m_stateAuditStartMs >= 3000u) + if (!m_device->GetBridgeDiagnostics()->IsCaptureOwner() + && GetTickCount64() - m_stateAuditStartMs >= 3000u) FinishStateAudit("duration-complete"); } + if (m_device->GetBridgeDiagnostics()->IsCaptureOwner()) { + // Require a release edge before standalone F7 may start another audit. + m_stateAuditTriggerWasDown = true; + return; + } + + if (!m_diagnostics) + return; + const bool triggerDown = (GetAsyncKeyState(VK_F7) & 0x8000) != 0; if (!triggerDown) { m_stateAuditTriggerWasDown = false; @@ -379,15 +386,26 @@ namespace dxvk { if (!m_stateAuditActive || beginQpc == 0u) return; + RecordStateAudit(kind); + RecordStateAuditElapsed(kind, beginQpc); + } + + + void D3D9GtaSaCompatDevice::RecordStateAuditElapsed( + D3D9GtaSaStateAuditKind kind, + uint64_t beginQpc) { + if (!m_stateAuditActive || beginQpc == 0u) + return; + LARGE_INTEGER value = { }; QueryPerformanceCounter(&value); const uint64_t endQpc = uint64_t(value.QuadPart); const uint64_t elapsed = endQpc >= beginQpc ? endQpc - beginQpc : 0u; - RecordStateAudit(kind); auto& counter = m_stateAuditCounters[uint32_t(kind)]; counter.QpcTicks += elapsed; counter.MaxQpcTicks = std::max(counter.MaxQpcTicks, elapsed); + counter.TimedCalls += 1u; } @@ -482,7 +500,12 @@ namespace dxvk { } - void D3D9GtaSaCompatDevice::StartStateAudit() { + void D3D9GtaSaCompatDevice::StartStateAudit(const char* source, UINT durationMs) { + if (m_stateAuditQpcFrequency <= 0) { + LARGE_INTEGER frequency = { }; + if (QueryPerformanceFrequency(&frequency)) + m_stateAuditQpcFrequency = frequency.QuadPart; + } for (auto& counter : m_stateAuditCounters) counter = { }; @@ -500,7 +523,11 @@ namespace dxvk { m_stateAuditFrames = 0u; m_stateAuditStartMs = GetTickCount64(); m_stateAuditActive = true; - Logger::info("GTA SA D3D9 state audit started: key=F7 durationMs=3000 mode=read-only"); + if (std::strcmp(source, "F7") == 0) + Logger::info("GTA SA D3D9 state audit started: key=F7 durationMs=3000 mode=read-only"); + else + Logger::info(str::format("GTA SA D3D9 state audit started: source=", source, + " durationMs=", durationMs, " mode=read-only")); } @@ -510,6 +537,7 @@ namespace dxvk { const UINT64 elapsedMs = GetTickCount64() - m_stateAuditStartMs; m_stateAuditActive = false; + m_device->GetBridgeDiagnostics()->AuditFinished(reason); static const char* names[] = { "renderState", @@ -726,12 +754,51 @@ namespace dxvk { : 0.0, " units=", counter.Units, " redundantUnits=", counter.RedundantUnits, + " timedCalls=", counter.TimedCalls, + " timing=device-locked-api-body-inclusive", " totalMs=", totalMs, " maxUs=", maxUs)); } } + void D3D9GtaSaCompatDevice::CopyStateAudit(D3D9BridgeAuditSnapshot& snapshot) const { + static_assert(D3D9_BRIDGE_AUDIT_COUNTER_COUNT == uint32_t(D3D9GtaSaStateAuditKind::Count)); + static_assert(D3D9_BRIDGE_PREPARE_COUNTER_COUNT == D3D9GtaSaPrepareDrawAudit::CategoryCount); + snapshot.ElapsedMs = GetTickCount64() - m_stateAuditStartMs; + snapshot.ObservedPresentBoundaries = m_stateAuditFrames; + snapshot.QpcFrequency = uint64_t(std::max(m_stateAuditQpcFrequency, 0)); + snapshot.Total = { }; + for (uint32_t i = 0; i < D3D9_BRIDGE_AUDIT_COUNTER_COUNT; i++) { + const auto& source = m_stateAuditCounters[i]; + auto& target = snapshot.Counters[i]; + target = { source.Calls, source.RedundantCalls, source.Units, + source.RedundantUnits, source.QpcTicks, source.MaxQpcTicks, source.TimedCalls }; + snapshot.Total.Calls += source.Calls; + snapshot.Total.RedundantCalls += source.RedundantCalls; + snapshot.Total.Units += source.Units; + snapshot.Total.RedundantUnits += source.RedundantUnits; + snapshot.Total.QpcTicks += source.QpcTicks; + snapshot.Total.TimedCalls += source.TimedCalls; + snapshot.Total.MaxQpcTicks = std::max(snapshot.Total.MaxQpcTicks, source.MaxQpcTicks); + } + snapshot.SamplerBindingCacheHits = m_device->GetGtaSaSamplerBindingCacheHits() + - m_stateAuditSamplerBindingCacheHits; + snapshot.TextureBindingCacheHits = m_device->GetGtaSaTextureBindingCacheHits() + - m_stateAuditTextureBindingCacheHits; + snapshot.ResourceBindingCacheInvalidations = m_device->GetGtaSaResourceBindingCacheInvalidations() + - m_stateAuditResourceBindingCacheInvalidations; + snapshot.StateBlockFastSkips = m_stateBlockFastSkips; + snapshot.PrepareDraws = m_prepareDrawAudit.Draws; + snapshot.PrepareCleanDraws = m_prepareDrawAudit.CleanDraws; + snapshot.PrepareCategoryEvents = m_prepareDrawAudit.CategoryEvents; + snapshot.PrepareMaxCategoriesPerDraw = m_prepareDrawAudit.MaxCategoriesPerDraw; + for (uint32_t i = 0; i < D3D9_BRIDGE_PREPARE_COUNTER_COUNT; i++) + snapshot.Prepare[i] = { m_prepareDrawAudit.Counters[i].Draws, + m_prepareDrawAudit.Counters[i].Units }; + } + + void D3D9GtaSaCompatDevice::RunVulkanPasses( UINT64 frameId, VkCommandBuffer commandBuffer, diff --git a/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.h b/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.h index aab6a63..0662aef 100644 --- a/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.h +++ b/backend/dxvk/src/d3d9/d3d9_gta_sa_compat.h @@ -1,6 +1,7 @@ #pragma once #include "d3d9_interfaces.h" +#include "d3d9_bridge_diagnostics.h" #include "../dxvk/dxvk_stats.h" @@ -41,6 +42,7 @@ namespace dxvk { uint64_t RedundantUnits = 0u; uint64_t QpcTicks = 0u; uint64_t MaxQpcTicks = 0u; + uint64_t TimedCalls = 0u; }; enum class D3D9GtaSaDrawKind : uint32_t { @@ -161,6 +163,7 @@ namespace dxvk { }; class D3D9GtaSaCompatDevice final : public ID3D9GtaSaCompatDevice6 { + friend class D3D9GtaSaStateAuditScope; public: @@ -225,6 +228,10 @@ namespace dxvk { void OnDeviceDestroy(); + void StartStateAudit(const char* source = "F7", UINT durationMs = 3000u); + void FinishStateAudit(const char* reason); + void CopyStateAudit(D3D9BridgeAuditSnapshot& snapshot) const; + bool IsStateAuditActive() const { return m_stateAuditActive; } @@ -252,6 +259,10 @@ namespace dxvk { D3D9GtaSaStateAuditKind kind, uint64_t beginQpc); + void RecordStateAuditElapsed( + D3D9GtaSaStateAuditKind kind, + uint64_t beginQpc); + void RecordStateBlockFastSkip() { if (m_stateAuditActive) m_stateBlockFastSkips += 1u; @@ -321,10 +332,6 @@ namespace dxvk { void UpdatePresentationState( const D3DPRESENT_PARAMETERS* params); - void StartStateAudit(); - - void FinishStateAudit(const char* reason); - struct VulkanPassEntry { UINT64 Token = 0u; UINT64 Sequence = 0u; @@ -375,4 +382,26 @@ namespace dxvk { std::vector m_passes; }; + // Construct only after the existing device lock is acquired. The scope + // records elapsed time only when the existing path actually increments its + // API counter, so rejected/record-only calls cannot invent timed coverage. + class D3D9GtaSaStateAuditScope { + public: + D3D9GtaSaStateAuditScope(D3D9GtaSaCompatDevice& compat, + D3D9GtaSaStateAuditKind kind) + : m_compat(compat), m_kind(kind), + m_beginQpc(compat.IsStateAuditActive() ? compat.BeginStateAuditTiming() : 0u), + m_calls(m_beginQpc ? compat.m_stateAuditCounters[uint32_t(kind)].Calls : 0u) { } + ~D3D9GtaSaStateAuditScope() { + if (m_beginQpc && m_compat.m_stateAuditCounters[uint32_t(m_kind)].Calls != m_calls) + m_compat.RecordStateAuditElapsed(m_kind, m_beginQpc); + } + D3D9GtaSaStateAuditScope(const D3D9GtaSaStateAuditScope&) = delete; + D3D9GtaSaStateAuditScope& operator=(const D3D9GtaSaStateAuditScope&) = delete; + private: + D3D9GtaSaCompatDevice& m_compat; + D3D9GtaSaStateAuditKind m_kind; + uint64_t m_beginQpc, m_calls; + }; + } diff --git a/backend/dxvk/src/d3d9/d3d9_hud.cpp b/backend/dxvk/src/d3d9/d3d9_hud.cpp index ef4e8e9..dd6c799 100644 --- a/backend/dxvk/src/d3d9/d3d9_hud.cpp +++ b/backend/dxvk/src/d3d9/d3d9_hud.cpp @@ -2,6 +2,30 @@ namespace dxvk::hud { + void HudRenderStack::update(dxvk::high_resolution_clock::time_point time) { + if (std::chrono::duration_cast(time - m_lastUpdate).count() < 200'000) + return; + m_snapshot.StructSize = sizeof(m_snapshot); + m_snapshot.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + m_device->GetBridgeHudSnapshot(&m_snapshot); + for (UINT i = 0; i < m_snapshot.LineCount; ++i) + m_lines[i] = m_snapshot.Lines[i]; + m_lastUpdate = time; + } + + HudPos HudRenderStack::render(const Rc& ctx, + const HudPipelineKey& key, const HudOptions& options, HudRenderer& renderer, + HudPos position) { + if (!m_snapshot.Visible) + return position; + for (UINT i = 0; i < m_snapshot.LineCount; ++i) { + position.y += 16; + renderer.drawText(16, position, i ? 0xffffffffu : 0xffc0ff00u, m_lines[i]); + } + position.y += 8; + return position; + } + HudTextureMemory::HudTextureMemory(D3D9DeviceEx* device) : m_device (device) , m_allocatedString ("") diff --git a/backend/dxvk/src/d3d9/d3d9_hud.h b/backend/dxvk/src/d3d9/d3d9_hud.h index 5bacd51..12fdd86 100644 --- a/backend/dxvk/src/d3d9/d3d9_hud.h +++ b/backend/dxvk/src/d3d9/d3d9_hud.h @@ -5,6 +5,19 @@ namespace dxvk::hud { + class HudRenderStack : public HudItem { + public: + explicit HudRenderStack(D3D9DeviceEx* device) : m_device(device) { } + void update(dxvk::high_resolution_clock::time_point time); + HudPos render(const Rc& ctx, const HudPipelineKey& key, + const HudOptions& options, HudRenderer& renderer, HudPos position); + private: + D3D9DeviceEx* m_device; + D3D9BridgeHudSnapshot m_snapshot = { }; + std::array m_lines; + dxvk::high_resolution_clock::time_point m_lastUpdate = { }; + }; + /** * \brief HUD item to display unmappable memory */ diff --git a/backend/dxvk/src/d3d9/d3d9_main.cpp b/backend/dxvk/src/d3d9/d3d9_main.cpp index 78c13a8..744440a 100644 --- a/backend/dxvk/src/d3d9/d3d9_main.cpp +++ b/backend/dxvk/src/d3d9/d3d9_main.cpp @@ -4,8 +4,6 @@ #include "d3d9_shader_validator.h" #include "d3d9_annotation.h" -#include "../util/util_thread_scheduling.h" -#include class D3DFE_PROCESSVERTICES; using PSGPERRORID = UINT; @@ -30,38 +28,9 @@ namespace dxvk { extern "C" { -#if defined(_WIN32) - static void ApplyGtaSaPCoreAffinity() { - SYSTEM_INFO sysInfo{}; - GetSystemInfo(&sysInfo); - DWORD numProcs = sysInfo.dwNumberOfProcessors; - - // Allow process to utilize all physical P-Cores freely without hard-pinning a single core - // In opt-in per-thread mode the Bridge's explicit process mask remains - // authoritative. Only read configuration here; CPU Sets/MMCSS setup runs - // later on the owning threads, outside DllMain. - if (numProcs >= 16 && !renderstack::scheduling::ReadOptions().enabled) { - DWORD_PTR pCoreMask = 0x0000FFFF; // P-Cores 0~7 with SMT - SetProcessAffinityMask(GetCurrentProcess(), pCoreMask); - } - - SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); - - // Enable Low Fragmentation Heap (LFH) on default process heap - ULONG lfh = 2; - HeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &lfh, sizeof(lfh)); - } - - static void ApplyGtaSaPCoreAffinityOnce() { - static std::once_flag once; - std::call_once(once, [] { ApplyGtaSaPCoreAffinity(); }); - } -#endif - + // Process affinity and priority are owned by the host/Bridge configuration. + // Opt-in per-thread scheduling remains local to the threads that own it. DLLEXPORT IDirect3D9* __stdcall Direct3DCreate9(UINT nSDKVersion) { -#if defined(_WIN32) - ApplyGtaSaPCoreAffinityOnce(); -#endif IDirect3D9Ex* pDirect3D = nullptr; dxvk::CreateD3D9(false, &pDirect3D, nullptr, 0); @@ -69,9 +38,6 @@ extern "C" { } DLLEXPORT HRESULT __stdcall Direct3DCreate9Ex(UINT nSDKVersion, IDirect3D9Ex** ppDirect3D9Ex) { -#if defined(_WIN32) - ApplyGtaSaPCoreAffinityOnce(); -#endif return dxvk::CreateD3D9(true, ppDirect3D9Ex, nullptr, 0); } diff --git a/backend/dxvk/src/d3d9/d3d9_state.h b/backend/dxvk/src/d3d9/d3d9_state.h index f8e4cec..de880a0 100644 --- a/backend/dxvk/src/d3d9/d3d9_state.h +++ b/backend/dxvk/src/d3d9/d3d9_state.h @@ -655,6 +655,15 @@ namespace dxvk { const auto* dstPtr = reinterpret_cast(dstData); const auto* srcPtr = reinterpret_cast(pConstantData); + // Code placement changed the measured x86 PS/int controls even when + // their instructions were identical. Check the final linked layout in + // tests/constant-layout-test.py whenever these paths or the compiler change. + #if defined(__clang__) && defined(__i386__) + #if __has_attribute(code_align) + [[clang::code_align(ShaderType == D3D9ShaderType::VertexShader + && ConstantType == D3D9ConstantType::Float ? 16 : 64)]] + #endif + #endif for (uint32_t i = 0u; i < Count; ++i) { const __m128i srcData = _mm_loadu_si128(srcPtr + i); const __m128i curData = _mm_loadu_si128(dstPtr + i); diff --git a/backend/dxvk/src/d3d9/d3d9_state_capture.h b/backend/dxvk/src/d3d9/d3d9_state_capture.h new file mode 100644 index 0000000..6677881 --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_state_capture.h @@ -0,0 +1,43 @@ +#pragma once + +#include "../util/util_bit.h" +#include + +namespace dxvk { + + // Enumerate only first writes in an already validated register interval. + // The callback may mark its range captured. Whole captured words are skipped. + template + void ForEachD3D9UncapturedRange(Mask& captured, uint32_t start, uint32_t count, + const Capture& capture) { + if (count == 1) { + if (!captured.get(start)) + capture(start, 1u); + return; + } + const uint32_t end = start + count; + while (start < end) { + const uint32_t word = start / 32; + const uint32_t pending = ~captured.dword(word) & (~0u << (start % 32)); + if (!pending) { + start = (word + 1) * 32; + continue; + } + start = word * 32 + bit::tzcnt(pending); + if (start >= end) + break; + uint32_t rangeEnd = start; + while (rangeEnd < end) { + const uint32_t offset = rangeEnd % 32; + const uint32_t limit = std::min(32u - offset, end - rangeEnd); + const uint32_t length = std::min(bit::tzcnt(captured.dword(rangeEnd / 32) >> offset), limit); + rangeEnd += length; + if (length < limit) + break; + } + capture(start, rangeEnd - start); + start = rangeEnd; + } + } + +} diff --git a/backend/dxvk/src/d3d9/d3d9_stateblock.cpp b/backend/dxvk/src/d3d9/d3d9_stateblock.cpp index 3f22cd4..f99bbd3 100644 --- a/backend/dxvk/src/d3d9/d3d9_stateblock.cpp +++ b/backend/dxvk/src/d3d9/d3d9_stateblock.cpp @@ -1,4 +1,5 @@ #include "d3d9_stateblock.h" +#include "d3d9_state_capture.h" #include "d3d9_caps.h" #include "d3d9_device.h" @@ -34,6 +35,8 @@ namespace dxvk { void D3D9StateBlock::ResetJournal() { m_hasApplied = false; + if (m_captures.flags.isClear()) + return; m_state.vertexDecl = nullptr; m_state.indices = nullptr; @@ -41,48 +44,45 @@ namespace dxvk { m_state.pixelShader = nullptr; if (m_state.vertexBuffers) { - for (auto& stream : m_state.vertexBuffers.get()) { + for (uint32_t idx : bit::BitMask(m_captures.vertexBuffers.dword(0))) { + auto& stream = m_state.vertexBuffers[idx]; stream.vertexBuffer = nullptr; - stream.offset = 0u; - stream.length = 0u; - stream.stride = 0u; + stream.offset = stream.length = stream.stride = 0u; } } - if (m_state.textures) { - for (auto& texture : m_state.textures.get()) - TextureChangePrivate(texture, nullptr); + for (uint32_t idx : bit::BitMask(m_captures.textures.dword(0))) + TextureChangePrivate(m_state.textures[idx], nullptr); } - m_state.lights.clear(); - - m_captures.flags = D3D9CapturedStateFlags(); + // Capture masks are the ownership/dirty sets. Uncaptured entries are + // already empty after the previous reset; do not revisit every slot. + if (m_captures.flags.test(D3D9CapturedStateFlag::Lights)) { + m_state.lights.clear(); + m_captures.lightChanges.clearAll(); + m_captures.lightEnabledChanges.clearAll(); + } m_captures.renderStates.clearAll(); + for (uint32_t idx : bit::BitMask(m_captures.samplers.dword(0))) + m_captures.samplerStates[idx].clearAll(); m_captures.samplers.clearAll(); - for (auto& states : m_captures.samplerStates) - states.clearAll(); m_captures.vertexBuffers.clearAll(); m_captures.textures.clearAll(); m_captures.clipPlanes.clearAll(); m_captures.streamFreq.clearAll(); m_captures.transforms.clearAll(); + for (uint32_t idx : bit::BitMask(m_captures.textureStages.dword(0))) + m_captures.textureStageStates[idx].clearAll(); m_captures.textureStages.clearAll(); - for (auto& states : m_captures.textureStageStates) - states.clearAll(); - m_captures.vsConsts.fConsts.clearAll(); - m_captures.vsConsts.iConsts.clearAll(); - m_captures.vsConsts.bConsts.clearAll(); - m_captures.vsConsts.fDwordCount = 0u; - m_captures.vsConsts.iDwordCount = 0u; - m_captures.vsConsts.bDwordCount = 0u; - m_captures.psConsts.fConsts.clearAll(); - m_captures.psConsts.iConsts.clearAll(); - m_captures.psConsts.bConsts.clearAll(); - m_captures.psConsts.fDwordCount = 0u; - m_captures.psConsts.iDwordCount = 0u; - m_captures.psConsts.bDwordCount = 0u; - m_captures.lightChanges.clearAll(); - m_captures.lightEnabledChanges.clearAll(); + const auto clearConstants = [] (auto& captures) { + for (uint32_t i = 0; i < captures.fDwordCount; i++) captures.fConsts.dword(i) = 0u; + for (uint32_t i = 0; i < captures.iDwordCount; i++) captures.iConsts.dword(i) = 0u; + for (uint32_t i = 0; i < captures.bDwordCount; i++) captures.bConsts.dword(i) = 0u; + captures.fDwordCount = captures.iDwordCount = captures.bDwordCount = 0u; + }; + clearConstants(m_captures.vsConsts); + clearConstants(m_captures.psConsts); + m_captures.flags.clrAll(); } HRESULT STDMETHODCALLTYPE D3D9StateBlock::QueryInterface( @@ -593,30 +593,23 @@ namespace dxvk { ? D3D9CapturedStateFlag::VsConstants : D3D9CapturedStateFlag::PsConstants); - for (UINT reg = StartRegister; reg < endRegister; reg++) { - if (captures.bConsts.get(reg)) - continue; - + for (UINT reg = StartRegister; reg < endRegister;) { const uint32_t dword = reg / 32u; - const uint32_t mask = 1u << (reg % 32u); - captures.bConsts.set(reg, true); - captures.bDwordCount = std::max(captures.bDwordCount, dword + 1u); - saved.bConsts[dword] &= ~mask; - saved.bConsts[dword] |= current.bConsts[dword] & mask; + const uint32_t next = std::min(endRegister, (dword + 1u) * 32u); + const uint32_t mask = (~0u << (reg % 32u)) + & (~0u >> (32u - (next - dword * 32u))) + & ~captures.bConsts.dword(dword); + if (mask) { + captures.bConsts.dword(dword) |= mask; + captures.bDwordCount = std::max(captures.bDwordCount, dword + 1u); + saved.bConsts[dword] = (saved.bConsts[dword] & ~mask) + | (current.bConsts[dword] & mask); + } + reg = next; } } else { auto captureRanges = [&] (auto& captured, auto&& captureRange) { - UINT reg = StartRegister; - while (reg < endRegister) { - while (reg < endRegister && captured.get(reg)) - reg++; - const UINT rangeStart = reg; - while (reg < endRegister && !captured.get(reg)) - reg++; - const UINT rangeCount = reg - rangeStart; - if (rangeCount) - captureRange(rangeStart, rangeCount); - } + ForEachD3D9UncapturedRange(captured, StartRegister, Count, captureRange); }; if constexpr (ConstantType == D3D9ConstantType::Float) { diff --git a/backend/dxvk/src/d3d9/d3d9_stateblock.h b/backend/dxvk/src/d3d9/d3d9_stateblock.h index ee39228..c86b474 100644 --- a/backend/dxvk/src/d3d9/d3d9_stateblock.h +++ b/backend/dxvk/src/d3d9/d3d9_stateblock.h @@ -509,15 +509,21 @@ namespace dxvk { uint32_t rangeStart = 0; while (rangeStart < registerCount) { - while (rangeStart < registerCount && !captures.get(rangeStart)) - rangeStart++; - - if (rangeStart == registerCount) - break; - - uint32_t rangeEnd = rangeStart + 1; - while (rangeEnd < registerCount && captures.get(rangeEnd)) - rangeEnd++; + const uint32_t wordIndex = rangeStart / 32; + const uint32_t pending = captures.dword(wordIndex) & (~0u << (rangeStart % 32)); + if (!pending) { + rangeStart = (wordIndex + 1) * 32; + continue; + } + rangeStart = wordIndex * 32 + bit::tzcnt(pending); + uint32_t rangeEnd = rangeStart; + while (rangeEnd < registerCount) { + const uint32_t offset = rangeEnd % 32; + const uint32_t count = std::min(bit::tzcnt(~captures.dword(rangeEnd / 32) >> offset), 32u - offset); + rangeEnd += count; + if (count < 32u - offset) + break; + } applyRange(rangeStart, rangeEnd - rangeStart); rangeStart = rangeEnd; @@ -676,19 +682,28 @@ namespace dxvk { else m_captures.flags.set(D3D9CapturedStateFlag::PsConstants); - for (uint32_t i = 0; i < Count; i++) { - uint32_t reg = StartRegister + i; - if constexpr (ConstantType == D3D9ConstantType::Float) { - setCaptures.fConsts.set(reg, true); - setCaptures.fDwordCount = std::max(setCaptures.fDwordCount, reg / 32 + 1); - } else if constexpr (ConstantType == D3D9ConstantType::Int) { - setCaptures.iConsts.set(reg, true); - setCaptures.iDwordCount = std::max(setCaptures.iDwordCount, reg / 32 + 1); - } else if constexpr (ConstantType == D3D9ConstantType::Bool) { - setCaptures.bConsts.set(reg, true); - setCaptures.bDwordCount = std::max(setCaptures.bDwordCount, reg / 32 + 1); + auto markRange = [&] (auto& mask, uint32_t& dwordCount) { + if (Count == 1) { + mask.set(StartRegister, true); + dwordCount = std::max(dwordCount, StartRegister / 32u + 1u); + } else if (Count) { + const uint32_t end = StartRegister + Count; + for (uint32_t reg = StartRegister; reg < end;) { + const uint32_t word = reg / 32u; + const uint32_t next = std::min(end, (word + 1u) * 32u); + mask.dword(word) |= (~0u << (reg % 32u)) + & (~0u >> (32u - (next - word * 32u))); + reg = next; + } + dwordCount = std::max(dwordCount, (end + 31u) / 32u); } - } + }; + if constexpr (ConstantType == D3D9ConstantType::Float) + markRange(setCaptures.fConsts, setCaptures.fDwordCount); + else if constexpr (ConstantType == D3D9ConstantType::Int) + markRange(setCaptures.iConsts, setCaptures.iDwordCount); + else + markRange(setCaptures.bConsts, setCaptures.bDwordCount); UpdateStateConstants( &m_state, StartRegister, pConstantData, Count); diff --git a/backend/dxvk/src/d3d9/d3d9_swapchain.cpp b/backend/dxvk/src/d3d9/d3d9_swapchain.cpp index 9e19298..496f08b 100644 --- a/backend/dxvk/src/d3d9/d3d9_swapchain.cpp +++ b/backend/dxvk/src/d3d9/d3d9_swapchain.cpp @@ -1111,6 +1111,7 @@ namespace dxvk { m_latencyHud = hud->addItem("latency", 4); hud->addItem("swvp", -1, m_parent); + hud->addItem("renderstack", -1, m_parent); #ifdef DXVK_USE_UNMAPPABLE_MEMORY hud->addItem("memory", -1, m_parent); diff --git a/backend/dxvk/src/dxvk/dxvk_cs.cpp b/backend/dxvk/src/dxvk/dxvk_cs.cpp index 6dc5842..6ae4673 100644 --- a/backend/dxvk/src/dxvk/dxvk_cs.cpp +++ b/backend/dxvk/src/dxvk/dxvk_cs.cpp @@ -2,6 +2,30 @@ #include "../util/util_thread_scheduling.h" namespace dxvk { + + static uint64_t activityTimeUs() { + return std::chrono::duration_cast( + dxvk::high_resolution_clock::now().time_since_epoch()).count(); + } + + DxvkCsThread::Activity DxvkCsThread::getActivity() { + std::unique_lock lock(m_mutex); + Activity result = { }; + result.wallUs = activityTimeUs(); + result.queueIdleUs = m_queueIdleUs + (m_queueIdleStartUs + ? result.wallUs - m_queueIdleStartUs : 0u); + result.dispatched = m_queueOrdered.seqDispatch; + result.completed = m_seqOrdered.load(); +#ifdef _WIN32 + result.threadId = m_thread.get_id(); + FILETIME created, exited, kernel, user; + result.cpuAvailable = GetThreadTimes(m_thread.native_handle(), &created, &exited, &kernel, &user); + if (result.cpuAvailable) + result.cpu100ns = (uint64_t(kernel.dwHighDateTime) << 32 | kernel.dwLowDateTime) + + (uint64_t(user.dwHighDateTime) << 32 | user.dwLowDateTime); +#endif + return result; + } DxvkCsChunk::DxvkCsChunk() { @@ -217,12 +241,15 @@ namespace dxvk { if (unlikely(!pred())) { auto t0 = dxvk::high_resolution_clock::now(); + m_queueIdleStartUs = std::chrono::duration_cast(t0.time_since_epoch()).count(); m_condOnAdd.wait(lock, [&] { return pred(); }); auto t1 = dxvk::high_resolution_clock::now(); + m_queueIdleUs += std::chrono::duration_cast(t1.time_since_epoch()).count() - m_queueIdleStartUs; + m_queueIdleStartUs = 0u; m_device->addStatCtr(DxvkStatCounter::CsIdleTicks, std::chrono::duration_cast(t1 - t0).count()); } diff --git a/backend/dxvk/src/dxvk/dxvk_cs.h b/backend/dxvk/src/dxvk/dxvk_cs.h index b4d98c3..2445adf 100644 --- a/backend/dxvk/src/dxvk/dxvk_cs.h +++ b/backend/dxvk/src/dxvk/dxvk_cs.h @@ -558,6 +558,13 @@ namespace dxvk { * \param [in] seq Sequence number to wait for. */ void synchronize(uint64_t seq); + + struct Activity { + uint64_t wallUs, cpu100ns, queueIdleUs, dispatched, completed; + uint32_t threadId; + bool cpuAvailable; + }; + Activity getActivity(); /** * \brief Retrieves last executed sequence number @@ -591,6 +598,10 @@ namespace dxvk { DxvkCsChunkQueue m_queueOrdered; DxvkCsChunkQueue m_queueHighPrio; + // Protected by m_mutex, which the condition wait releases while idle. + uint64_t m_queueIdleUs = 0u; + uint64_t m_queueIdleStartUs = 0u; + dxvk::thread m_thread; auto& getQueue(DxvkCsQueue which) { diff --git a/backend/dxvk/tools/dxvk_state_dedup_test.cpp b/backend/dxvk/tools/dxvk_state_dedup_test.cpp index 2d17650..72357e9 100644 --- a/backend/dxvk/tools/dxvk_state_dedup_test.cpp +++ b/backend/dxvk/tools/dxvk_state_dedup_test.cpp @@ -3,7 +3,15 @@ #include "../src/dxvk/dxvk_graphics_state.h" #include "../src/d3d9/d3d9_state.h" -#include +#include "../src/d3d9/d3d9_binding_fast_path.h" +#include "../src/d3d9/d3d9_state_capture.h" +#include +#include + +// Keep calls and checks active in Release builds. +#define CHECK(...) do { if (!(__VA_ARGS__)) { \ + std::fprintf(stderr, "FAIL %s:%u: %s\n", __FILE__, __LINE__, #__VA_ARGS__); \ + std::exit(1); } } while (false) namespace { @@ -15,8 +23,8 @@ void testInputAssemblyEquality() { const dxvk::DxvkIaInfo different( VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, VK_FALSE, 0u); - assert(first.eq(same)); - assert(!first.eq(different)); + CHECK(first.eq(same)); + CHECK(!first.eq(different)); } void testMultisampleEquality() { @@ -27,8 +35,8 @@ void testMultisampleEquality() { const dxvk::DxvkMsInfo different( VK_SAMPLE_COUNT_1_BIT, 0xff0fu, VK_FALSE); - assert(first.eq(same)); - assert(!first.eq(different)); + CHECK(first.eq(same)); + CHECK(!first.eq(different)); } void testBlendEquality() { @@ -63,8 +71,8 @@ void testBlendEquality() { VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT); - assert(first.eq(same)); - assert(!first.eq(different)); + CHECK(first.eq(same)); + CHECK(!first.eq(different)); } void testSamplerEquality() { @@ -86,13 +94,67 @@ void testSamplerEquality() { differentState[D3DSAMP_MINFILTER] = D3DTEXF_POINT; const dxvk::D3D9SamplerInfo first(firstState); - assert(first.eq(dxvk::D3D9SamplerInfo(sameState))); - assert(!first.eq(dxvk::D3D9SamplerInfo(differentState))); + CHECK(first.eq(dxvk::D3D9SamplerInfo(sameState))); + CHECK(!first.eq(dxvk::D3D9SamplerInfo(differentState))); auto borderState = firstState; borderState[D3DSAMP_ADDRESSU] = D3DTADDRESS_BORDER; borderState[D3DSAMP_BORDERCOLOR] = 0x12345678u; - assert(!first.eq(dxvk::D3D9SamplerInfo(borderState))); + CHECK(!first.eq(dxvk::D3D9SamplerInfo(borderState))); +} + +void testSingleBindingDispatch() { + UINT calls = 0, slot = 99; + const auto bind = [&](uint32_t i) { ++calls; slot = i; }; + CHECK(!dxvk::TryD3D9SingleBinding(0, bind)); + CHECK(calls == 0); + for (UINT i = 0; i < dxvk::SamplerCount; ++i) { + CHECK(dxvk::TryD3D9SingleBinding(1u << i, bind)); + CHECK(calls == i + 1); + CHECK(slot == i); + } + calls = 0; + for (uint32_t mask : {3u, 0x10001u, (1u << dxvk::SamplerCount) - 1}) { + CHECK(!dxvk::TryD3D9SingleBinding(mask, bind)); + CHECK(calls == 0); + } +} + +void testFirstWriteRanges() { + // Independent per-register oracle, including mutations between captures. + dxvk::bit::bitset<8192> captured; + dxvk::ForEachD3D9UncapturedRange(captured, 8192u, 0u, + [](uint32_t, uint32_t) { CHECK(false); }); + dxvk::ForEachD3D9UncapturedRange(captured, 8191u, 1u, + [&](uint32_t start, uint32_t count) { + CHECK(start == 8191 && count == 1); captured.set(start, true); + }); + dxvk::ForEachD3D9UncapturedRange(captured, 8191u, 1u, + [](uint32_t, uint32_t) { CHECK(false); }); + captured.clearAll(); + std::array current = {}, saved = {}, expected = {}; + std::array seen = {}; + uint32_t random = 0x71e45u; + for (uint32_t pass = 0; pass < 2000; pass++) { + random = random * 1664525u + 1013904223u; + const uint32_t start = pass < 5 ? 31u * pass : random % 8192; + const uint32_t count = std::min(8192u - start, pass < 5 ? 8192u - start : (random >> 16) % 513u); + for (uint32_t i = 0; i < 8192; i++) current[i] = pass * 8192 + i; + for (uint32_t i = start; i < start + count; i++) { + if (!seen[i]) { expected[i] = current[i]; seen[i] = true; } + } + dxvk::ForEachD3D9UncapturedRange(captured, start, count, + [&](uint32_t first, uint32_t length) { + CHECK(length && first >= start && first + length <= start + count); + for (uint32_t i = first; i < first + length; i++) { + CHECK(!captured.get(i)); + saved[i] = current[i]; captured.set(i, true); + } + }); + CHECK(saved == expected); + for (uint32_t i = 0; i < 8192; i++) CHECK(captured.get(i) == seen[i]); + if (pass % 11 == 0) { captured.clearAll(); seen.fill(false); saved.fill(0); expected.fill(0); } + } } } @@ -102,5 +164,8 @@ int main() { testMultisampleEquality(); testBlendEquality(); testSamplerEquality(); + testSingleBindingDispatch(); + testFirstWriteRanges(); + std::puts("single binding dispatch and first-write ranges: passed (Release checks enabled)"); return 0; } diff --git a/config/SA.RenderStack.ini b/config/SA.RenderStack.ini index 75b9a1b..c271951 100644 --- a/config/SA.RenderStack.ini +++ b/config/SA.RenderStack.ini @@ -22,8 +22,9 @@ DumpShaders=0 ; Opt-in M1 scheduling. Restart the process after changing these switches. ; CPU Sets are soft affinity, not exclusive core reservation. Physical cores ; are selected from OS topology within the existing process/thread masks. -; PerThread=1 also suppresses the backend's legacy 0xFFFF process pin and the -; Bridge's periodic process-mask reapply (the startup Mask below still applies). +; Bridge owns process Mask/Priority when Enable=1; the backend preserves them. +; PerThread=1 suppresses Bridge's periodic process-mask reapply (the startup +; Mask below still applies when Enable=1). ; Device-owner -> first performance core, DXVK CS -> second performance core, ; background/compiler workers -> lower-efficiency cores if available. ; Unknown/unsupported topology or a failed API leaves normal scheduling intact. @@ -102,6 +103,17 @@ TriggerVirtualKey=120 CaptureFrames=120 SampleEveryDraws=512 +[Measurement] +; F8 (F7 alias): baseline -> CPU sampling -> state/journal attribution. +; Stage FPS stays separate; sampled and inclusive times are never added into +; a fictional frame cost. Detailed reports go to scripts/BridgeD3D9.measurement-*.log. +Enable=1 +ShowHud=1 +CpuHotspots=1 +JournalAttribution=1 +BaselineMs=5000 +SlowFrameMs=50 + [CPUHotspotProfile] ; Press F8 in the slow scene to save a screenshot and sample the GTA render/main ; thread for DurationMs. This remains a clean CPU-only capture by default. @@ -145,9 +157,10 @@ BatchConsecutiveEffects=0 SkipDuplicateMatrices=1 ; Skip byte-identical vectors and float arrays inside the active Effect transaction. SkipDuplicateParameters=1 -; Submit verified LitPrelight constants and textures directly to D3D9, bypassing -; per-object D3DX CommitChanges. Shader hashes and effect signatures must match. -DirectConstants=1 +; Quarantined after live captures showed geometry and sprite state leaking into +; later draws. Production always uses effective=0, including when this key is +; missing or an older profile requests 1. Runtime logs requested/effective values. +DirectConstants=0 ; 2026-07-26: A/B lever. 1 = journal capture/restore runs inside DXVK via the ; api7 native interface (two cross-DLL toggles per effect transaction). ; 0 = journal stays local to the bridge (the proven 7/16 +11.6% path). @@ -251,10 +264,10 @@ Enable=1 [ProperShadersStateRing] ; Migrated from FLACompatBridge.ini EnableProperShadersStateRingTrace. Records -; the alpha render-state transitions observed by the guard into a 2048-entry -; ring and appends it to scripts\BridgeD3D9.propershaders-state-ring.log when -; the trigger key is pressed. Enabling it here mirrors the FLA++ side, which had -; EnableProperShadersStateRingTrace=1 for the same F10 diagnostic. +; detailed alpha/device transitions on demand into a 2048-entry +; ring. F10 collects about one second, then appends the results to +; scripts\BridgeD3D9.propershaders-state-ring.log. Idle play does not capture. +; The required Fading state correction remains active independently. Enable=1 TriggerVirtualKey=121 diff --git a/docs/architecture/module-map.md b/docs/architecture/module-map.md index 3b06bff..698f728 100644 --- a/docs/architecture/module-map.md +++ b/docs/architecture/module-map.md @@ -124,6 +124,40 @@ external integration. It must not maintain a stale cache that bypasses state cha DXVK-native restores or unwrapped state blocks. Experimental direct-effect and batch modes remain disabled unless a separate test proves them safe. +## Private Lifecycle Integration + +`backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h` defines a private COM observer +between the Bridge and backend, independent of the public API1-7 interfaces and +the DLL export set. Raw devices notify Present, Reset/ResetEx and final public +Release boundaries. A borrowed host interface does not keep the device alive; +the Bridge detaches its pass bindings before final release and admits later +devices. Backends lacking the private interface use the existing device wrapper. +Plugins/PostFX and per-call diagnostics continue to select the wrapped path. + +Instruction patches are published at the sole active device's owner-thread +Present boundary. The guard no longer has a patch-writing watcher, and the +support worker no longer suspends the render thread to install patches. Patch +transactions preserve original bytes/protection and retain failed rollback +state for retry; teardown refuses to overwrite another owner's bytes. + +Bridge initialization runs on the first exported call. DllMain only records its +module and disables thread notifications. Query `IID_D3D9BridgeRuntime` on the +Bridge factory for explicit `Shutdown()`, outside callbacks and loader lock, +after releasing all devices admitted through that factory. Shutdown refuses +live devices, stops/joins workers, restores tracked patches and shuts down +plugins. A restoration failure returns E_FAIL and retains state for retry; +completed shutdown rejects new device creation. + +Once initialized, the Bridge remains module-pinned because a caller may have +cached an old hook entry before restoration. Explicit shutdown releases runtime +activity; it does not promise physical hot unloading through FreeLibrary. +Third-party rendering on arbitrary threads and live module replacement require +a separate compatibility contract and are not established by these regressions. + +Lifecycle/patch/config behavior tests run through `tools/test.ps1`. They use +allocated executable code, real IAT calls, actual D3D9/Vulkan devices and isolated +child processes. They do not replace the fixed game workload or visual testing. + ## Build and Package Ownership - `tools/build.ps1` builds Bridge, DXVK, probes, tests, and build metadata. diff --git a/docs/development/candidate-20261109.md b/docs/development/candidate-20261109.md new file mode 100644 index 0000000..64dfa9b --- /dev/null +++ b/docs/development/candidate-20261109.md @@ -0,0 +1,61 @@ +# 2026-11-09 source candidate + +This snapshot collects the D3D9 correctness, resource lifetime, measurement and +CPU submission work developed since the previous published main branch. It is +a source/build candidate, not a claim of improved GTA frame rate. + +The changes cover state-block and journal restoration, shader-constant capture +and submission, texture/sampler binding and ownership, the x86 Fading entry +repair, Bridge lifecycle and patch transactions, staged F7/F8 diagnostics with +HUD/thread attribution and symbol sidecars, and streaming configuration +preparation. The latest batch protects active texture upload source mappings +and avoids repeating full preflight/logging for an unchanged rejected patch. + +The public D3D9/API interfaces and Vulkan staging/copy path are retained. Test +fixtures and benchmark tools are source-only; no game installation, dump, +private shader package, local binary or Python cache is included. + +## Validation status + +- The 2026-11-08 full local pipeline finished with 82 passed, 0 failed and two + optional external-audit skips. The latest native upload/retry fixtures ran + 4,127 checks with zero failures, including pixel sampling and lifecycle checks. +- The pre-publication 2026-11-09 local run finished with 77 passed, 5 failed and + two optional skips. All five failures occurred while LLVM-MinGW compiled + fixtures: thread-journal-native, upload-mapping-pressure, patch-retry-native, + measurement-backend-unavailable and measurement-hud-render. Standard Windows + and libc++ headers reported inconsistent preprocessing errors; two invocations + also reported Clang frontend access violations. These failures are retained + and are not counted as successful runtime tests. The local cause is under + investigation; no test retry or skip was added to hide it. +- GitHub Windows CI builds from a clean checkout, runs its existing hosted-runner + checks, verifies exports and package layout, and publishes split/SDK/symbol + artifacts. Its existing GPU/environment-sensitive skips do not replace local + GPU behavior tests. The PR check status identifies the actual cloud result. + +The first cloud run built Release x86 successfully, then reported 37 passed, +two failed and 45 existing hosted-runner skips. Both failures were fixture +setup: streaming-config-offline-regression and measurement-symbols requested +the entire DXVK toolchain even though they compile directly with LLVM. Ninja +was installed at the CI-specific path, which these child scripts did not use. +The follow-up removes their unnecessary Ninja/glslang discovery and resolves +only LLVM (plus Python for symbol tests), retaining all behavior assertions. +Both tests were reproduced failing with unavailable Ninja/glslang overrides, +then passed with the same overrides after the fix: 14 native decision checks, +120 consumer/preparation/replay assertions and 27 symbol tests. The local +diagnostic child process used logical CPU 0 because of the separate compiler +instability above; this is not a product or CI affinity setting. +The CI attempts and current check status remain visible in +[PR #1](https://github.com/prematurely/sa-renderstack/pull/1). + +See [the upload/retry report](upload-state-20261108.md) and +[raw paired measurements](evidence/2026-11-08-upload-state/) for the latest +performance evidence. Pressured mip-chain uploads reduced main-thread cycles +about 7%; rejected-patch polling also reduced CPU cycles. Single-mip controls +and empty-Present wall time include regressions. Whole-Present throughput and +same-route game frame-time acceptance remain open. + +No game-directory deployment or new batching/multicore implementation is part +of this publication. Future batching work must first account for the existing +DXVK command-stream worker and preserve state observation, lifetime, ordering +and memory-budget behavior. diff --git a/docs/development/evidence/2026-11-08-upload-state/README.md b/docs/development/evidence/2026-11-08-upload-state/README.md new file mode 100644 index 0000000..9fb5358 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/README.md @@ -0,0 +1,21 @@ +# Upload/retry candidate measurements + +These CSV/JSON files preserve the paired measurements described in +[the report](../../upload-state-20261108.md). Negative percentage changes mean +less work/time. CPU cycles are not milliseconds or game FPS. + +- `cycles-*`: first cycle-instrumented pass of the final implementation. +- `validated-*`: binaries rebuilt by the complete local validation pipeline. +- `waits-pinned-*` / `waits-normal-*`: follow-up empty-Present diagnosis with + fixed or ordinary calling-thread affinity; no game affinity was changed. +- Each profile has six A/B and three A/A pairs; AB and BA order alternate. +- Upload timing uses 512 iterations after 64 warmups. Retry timing uses 2,000 + Present calls after 16 warmups. Raw stdout/configuration copies remain in the + local `out/perf/20261108-upload-state` evidence directory. +- `package-manifest.json` records the measured local candidate binaries and + matching symbol files. CI rebuild hashes may differ. +- `import-dependencies.json` compares static runtime DLL imports only. + +The earlier superseded implementation and its timings are excluded here. +The final single-mip and empty-Present regressions are included. These records +do not constitute game-route or whole-Present performance acceptance. diff --git a/docs/development/evidence/2026-11-08-upload-state/cycles-benchmark-summary.json b/docs/development/evidence/2026-11-08-upload-state/cycles-benchmark-summary.json new file mode 100644 index 0000000..b019efc --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/cycles-benchmark-summary.json @@ -0,0 +1,246 @@ +{ + "uploads": [ + { + "profile": "pressure-mips", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 812.197, + "candidateMedianMs": 755.149, + "pairedMedianPct": -8.153, + "abRangePct": [ + -9.768, + -4.486 + ], + "aaMedianAbsolutePct": 1.633, + "aaMaximumAbsolutePct": 5.737, + "wins": 6, + "baselineMaps": [ + 5632 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1800605484.5, + "candidateMedian": 1674466884.0, + "pairedMedianPct": -7.273, + "abRangePct": [ + -9.142, + -4.618 + ], + "aaMedianAbsolutePct": 1.668, + "aaMaximumAbsolutePct": 4.273, + "wins": 6 + } + }, + { + "profile": "pressure-mips", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 805.848, + "candidateMedianMs": 752.639, + "pairedMedianPct": -8.038, + "abRangePct": [ + -10.17, + 0.409 + ], + "aaMedianAbsolutePct": 2.513, + "aaMaximumAbsolutePct": 9.427, + "wins": 5, + "baselineMaps": [ + 5632 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1786176405.5, + "candidateMedian": 1656963256.5, + "pairedMedianPct": -7.583, + "abRangePct": [ + -9.532, + -0.1 + ], + "aaMedianAbsolutePct": 3.528, + "aaMaximumAbsolutePct": 6.895, + "wins": 6 + } + }, + { + "profile": "ordinary-mips", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 235.244, + "candidateMedianMs": 237.065, + "pairedMedianPct": 0.58, + "abRangePct": [ + -1.271, + 2.308 + ], + "aaMedianAbsolutePct": 0.771, + "aaMaximumAbsolutePct": 1.784, + "wins": 1, + "baselineMaps": [ + 0 + ], + "candidateMaps": [ + 0 + ], + "mainCycles": { + "baselineMedian": 233711207.5, + "candidateMedian": 223988042.0, + "pairedMedianPct": -4.135, + "abRangePct": [ + -9.375, + 11.551 + ], + "aaMedianAbsolutePct": 3.295, + "aaMaximumAbsolutePct": 5.136, + "wins": 4 + } + }, + { + "profile": "ordinary-mips", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 236.323, + "candidateMedianMs": 239.771, + "pairedMedianPct": 1.459, + "abRangePct": [ + 0.114, + 4.207 + ], + "aaMedianAbsolutePct": 0.422, + "aaMaximumAbsolutePct": 2.189, + "wins": 0, + "baselineMaps": [ + 0 + ], + "candidateMaps": [ + 0 + ], + "mainCycles": { + "baselineMedian": 230169179.0, + "candidateMedian": 225405203.0, + "pairedMedianPct": -0.561, + "abRangePct": [ + -9.113, + 9.004 + ], + "aaMedianAbsolutePct": 2.047, + "aaMaximumAbsolutePct": 3.628, + "wins": 3 + } + }, + { + "profile": "pressure-single", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 562.943, + "candidateMedianMs": 563.416, + "pairedMedianPct": 0.473, + "abRangePct": [ + -4.773, + 13.084 + ], + "aaMedianAbsolutePct": 5.394, + "aaMaximumAbsolutePct": 10.722, + "wins": 3, + "baselineMaps": [ + 512 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1266003614.5, + "candidateMedian": 1263350937.0, + "pairedMedianPct": 0.428, + "abRangePct": [ + -3.052, + 5.584 + ], + "aaMedianAbsolutePct": 2.405, + "aaMaximumAbsolutePct": 8.751, + "wins": 3 + } + }, + { + "profile": "pressure-single", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 562.675, + "candidateMedianMs": 587.82, + "pairedMedianPct": 0.562, + "abRangePct": [ + -8.33, + 12.489 + ], + "aaMedianAbsolutePct": 2.091, + "aaMaximumAbsolutePct": 3.964, + "wins": 3, + "baselineMaps": [ + 512 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1264533454.5, + "candidateMedian": 1294456256.5, + "pairedMedianPct": 0.493, + "abRangePct": [ + -5.238, + 8.413 + ], + "aaMedianAbsolutePct": 2.073, + "aaMaximumAbsolutePct": 2.721, + "wins": 2 + } + } + ], + "retry": { + "elapsedMs": { + "baselineMedian": 465.922, + "candidateMedian": 437.279, + "pairedMedianPct": -7.728, + "abRangePct": [ + -18.197, + 13.195 + ], + "aaMedianAbsolutePct": 14.223, + "aaMaximumAbsolutePct": 43.511, + "wins": 4 + }, + "mainCpuMs": { + "baselineMedian": 62.5, + "candidateMedian": 39.062, + "pairedMedianPct": -42.5, + "abRangePct": [ + -66.667, + 100.0 + ], + "aaMedianAbsolutePct": 100.0, + "aaMaximumAbsolutePct": 600.0, + "wins": 5 + }, + "mainCycles": { + "baselineMedian": 157396468.0, + "candidateMedian": 98340691.5, + "pairedMedianPct": -37.774, + "abRangePct": [ + -39.78, + -32.329 + ], + "aaMedianAbsolutePct": 12.677, + "aaMaximumAbsolutePct": 14.62, + "wins": 6 + } + } +} diff --git a/docs/development/evidence/2026-11-08-upload-state/cycles-retry-ab.csv b/docs/development/evidence/2026-11-08-upload-state/cycles-retry-ab.csv new file mode 100644 index 0000000..352e030 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/cycles-retry-ab.csv @@ -0,0 +1,19 @@ +"pair","slot","version","elapsedMs","mainCpuMs","mainCycles" +"0","0","baseline","576.5764","109.375","167991314" +"0","1","baseline","658.5845","62.5","189287231" +"1","0","baseline","559.7821","46.875","168353458" +"1","1","candidate","457.9201","15.625","101383188" +"2","0","candidate","400.8076","46.875","94685472" +"2","1","baseline","433.2718","62.5","151973919" +"3","0","baseline","455.3707","31.25","153922192" +"3","1","baseline","425.1804","62.5","151370128" +"4","0","baseline","460.6483","62.5","157461429" +"4","1","candidate","416.6374","46.875","95298195" +"5","0","candidate","405.0106","31.25","94511787" +"5","1","baseline","440.0541","78.125","152073108" +"6","0","baseline","429.483","15.625","152948055" +"6","1","baseline","616.3567","109.375","175308793" +"7","0","baseline","627.6624","46.875","172902142" +"7","1","candidate","654.3767","93.75","111780795" +"8","0","candidate","533.3674","31.25","106467706" +"8","1","baseline","471.1955","93.75","157331507" diff --git a/docs/development/evidence/2026-11-08-upload-state/cycles-upload-ab.csv b/docs/development/evidence/2026-11-08-upload-state/cycles-upload-ab.csv new file mode 100644 index 0000000..448d4a9 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/cycles-upload-ab.csv @@ -0,0 +1,109 @@ +"profile","pair","slot","version","kind","iterations","ms","mainCycles","maps","unmaps" +"pressure-mips","0","0","baseline","managed","512","856.9716","1879232090","5632","5632" +"pressure-mips","0","0","baseline","systemmem","512","924.8055","1975649559","5632","5632" +"pressure-mips","0","1","baseline","managed","512","842.9759","1847888846","5632","5632" +"pressure-mips","0","1","baseline","systemmem","512","837.6286","1839432251","5632","5632" +"pressure-mips","1","0","baseline","managed","512","847.5148","1857430546","5632","5632" +"pressure-mips","1","0","baseline","systemmem","512","840.9276","1843573153","5632","5632" +"pressure-mips","1","1","candidate","managed","512","774.7612","1718517573","512","512" +"pressure-mips","1","1","candidate","systemmem","512","755.4045","1667849692","512","512" +"pressure-mips","2","0","candidate","managed","512","761.709","1696317692","512","512" +"pressure-mips","2","0","candidate","systemmem","512","792.0953","1748926614","512","512" +"pressure-mips","2","1","baseline","managed","512","806.6714","1778437665","5632","5632" +"pressure-mips","2","1","baseline","systemmem","512","788.8679","1750669935","5632","5632" +"pressure-mips","3","0","baseline","managed","512","803.6407","1762319419","5632","5632" +"pressure-mips","3","0","baseline","systemmem","512","803.8162","1765204535","5632","5632" +"pressure-mips","3","1","baseline","managed","512","796.362","1759344006","5632","5632" +"pressure-mips","3","1","baseline","systemmem","512","789.2249","1741722337","5632","5632" +"pressure-mips","4","0","baseline","managed","512","805.2385","1776669859","5632","5632" +"pressure-mips","4","0","baseline","systemmem","512","792.0207","1751303737","5632","5632" +"pressure-mips","4","1","candidate","managed","512","742.9987","1651116547","512","512" +"pressure-mips","4","1","candidate","systemmem","512","749.8736","1641900368","512","512" +"pressure-mips","5","0","candidate","managed","512","762.3732","1683021441","512","512" +"pressure-mips","5","0","candidate","systemmem","512","755.7313","1663387147","512","512" +"pressure-mips","5","1","baseline","managed","512","844.9045","1852355788","5632","5632" +"pressure-mips","5","1","baseline","systemmem","512","822.8158","1825612531","5632","5632" +"pressure-mips","6","0","baseline","managed","512","829.8413","1835638556","5632","5632" +"pressure-mips","6","0","baseline","systemmem","512","818.9042","1832985404","5632","5632" +"pressure-mips","6","1","baseline","managed","512","782.2311","1757207844","5632","5632" +"pressure-mips","6","1","baseline","systemmem","512","798.328","1768309558","5632","5632" +"pressure-mips","7","0","baseline","managed","512","817.7216","1822773304","5632","5632" +"pressure-mips","7","0","baseline","systemmem","512","815.5244","1811208081","5632","5632" +"pressure-mips","7","1","candidate","managed","512","747.5887","1660330709","512","512" +"pressure-mips","7","1","candidate","systemmem","512","741.9874","1641299699","512","512" +"pressure-mips","8","0","candidate","managed","512","748.588","1665912327","512","512" +"pressure-mips","8","0","candidate","systemmem","512","733.0962","1650539366","512","512" +"pressure-mips","8","1","baseline","managed","512","783.7495","1773678802","5632","5632" +"pressure-mips","8","1","baseline","systemmem","512","796.1715","1761144730","5632","5632" +"ordinary-mips","0","0","baseline","managed","512","238.4885","222766678","0","0" +"ordinary-mips","0","0","baseline","systemmem","512","235.8798","228461745","0","0" +"ordinary-mips","0","1","baseline","managed","512","236.6488","234209035","0","0" +"ordinary-mips","0","1","baseline","systemmem","512","236.6873","233111029","0","0" +"ordinary-mips","1","0","baseline","managed","512","235.058","236774386","0","0" +"ordinary-mips","1","0","baseline","systemmem","512","235.1534","235253557","0","0" +"ordinary-mips","1","1","candidate","managed","512","237.2699","221374058","0","0" +"ordinary-mips","1","1","candidate","systemmem","512","235.4208","218021563","0","0" +"ordinary-mips","2","0","candidate","managed","512","236.3074","224657852","0","0" +"ordinary-mips","2","0","candidate","systemmem","512","248.6691","240634143","0","0" +"ordinary-mips","2","1","baseline","managed","512","239.3503","233174668","0","0" +"ordinary-mips","2","1","baseline","systemmem","512","238.6288","220756727","0","0" +"ordinary-mips","3","0","baseline","managed","512","235.127","233058800","0","0" +"ordinary-mips","3","0","baseline","systemmem","512","235.2505","227918681","0","0" +"ordinary-mips","3","1","baseline","managed","512","235.4073","225379195","0","0" +"ordinary-mips","3","1","baseline","systemmem","512","240.4005","219649303","0","0" +"ordinary-mips","4","0","baseline","managed","512","234.9305","225976438","0","0" +"ordinary-mips","4","0","baseline","systemmem","512","236.3499","218876697","0","0" +"ordinary-mips","4","1","candidate","managed","512","240.0361","230979911","0","0" +"ordinary-mips","4","1","candidate","systemmem","512","239.3356","223486089","0","0" +"ordinary-mips","5","0","candidate","managed","512","235.6136","218428685","0","0" +"ordinary-mips","5","0","candidate","systemmem","512","240.4077","230446993","0","0" +"ordinary-mips","5","1","baseline","managed","512","235.2981","229004553","0","0" +"ordinary-mips","5","1","baseline","systemmem","512","235.3494","225430242","0","0" +"ordinary-mips","6","0","baseline","managed","512","235.2881","225935811","0","0" +"ordinary-mips","6","0","baseline","systemmem","512","236.5534","218725012","0","0" +"ordinary-mips","6","1","baseline","managed","512","239.4866","220867756","0","0" +"ordinary-mips","6","1","baseline","systemmem","512","237.5522","223203318","0","0" +"ordinary-mips","7","0","baseline","managed","512","236.342","234247747","0","0" +"ordinary-mips","7","0","baseline","systemmem","512","236.4228","234908116","0","0" +"ordinary-mips","7","1","candidate","managed","512","236.8596","261305461","0","0" +"ordinary-mips","7","1","candidate","systemmem","512","238.4752","227324317","0","0" +"ordinary-mips","8","0","candidate","managed","512","240.6174","223318232","0","0" +"ordinary-mips","8","0","candidate","systemmem","512","240.2057","222322307","0","0" +"ordinary-mips","8","1","baseline","managed","512","235.1895","246418923","0","0" +"ordinary-mips","8","1","baseline","systemmem","512","236.297","244614140","0","0" +"pressure-single","0","0","baseline","managed","512","574.1601","1277785705","512","512" +"pressure-single","0","0","baseline","systemmem","512","595.1682","1301534239","512","512" +"pressure-single","0","1","baseline","managed","512","600.1854","1308514784","512","512" +"pressure-single","0","1","baseline","systemmem","512","593.7878","1312043605","512","512" +"pressure-single","1","0","baseline","managed","512","577.87","1291730736","512","512" +"pressure-single","1","0","baseline","systemmem","512","597.2713","1323951073","512","512" +"pressure-single","1","1","candidate","managed","512","560.9983","1252308322","512","512" +"pressure-single","1","1","candidate","systemmem","512","561.7047","1254603451","512","512" +"pressure-single","2","0","candidate","managed","512","558.5715","1253420099","512","512" +"pressure-single","2","0","candidate","systemmem","512","551.516","1246112984","512","512" +"pressure-single","2","1","baseline","managed","512","550.8281","1237555093","512","512" +"pressure-single","2","1","baseline","systemmem","512","556.5844","1238542517","512","512" +"pressure-single","3","0","baseline","managed","512","544.287","1241860891","512","512" +"pressure-single","3","0","baseline","systemmem","512","556.102","1249165559","512","512" +"pressure-single","3","1","baseline","managed","512","602.6458","1350537160","512","512" +"pressure-single","3","1","baseline","systemmem","512","578.1438","1283155181","512","512" +"pressure-single","4","0","baseline","managed","512","560.0198","1258396720","512","512" +"pressure-single","4","0","baseline","systemmem","512","563.7448","1270288752","512","512" +"pressure-single","4","1","candidate","managed","512","557.442","1253044648","512","512" +"pressure-single","4","1","candidate","systemmem","512","575.2143","1275036783","512","512" +"pressure-single","5","0","candidate","managed","512","576.2108","1297271745","512","512" +"pressure-single","5","0","candidate","systemmem","512","623.0726","1364681018","512","512" +"pressure-single","5","1","baseline","managed","512","565.8655","1273610509","512","512" +"pressure-single","5","1","baseline","systemmem","512","553.8966","1258778157","512","512" +"pressure-single","6","0","baseline","managed","512","594.3246","1284112368","512","512" +"pressure-single","6","0","baseline","systemmem","512","566.3633","1253216665","512","512" +"pressure-single","6","1","baseline","managed","512","562.2677","1259583681","512","512" +"pressure-single","6","1","baseline","systemmem","512","578.2048","1279195527","512","512" +"pressure-single","7","0","baseline","managed","512","546.9006","1241092365","512","512" +"pressure-single","7","0","baseline","systemmem","512","561.6052","1256395707","512","512" +"pressure-single","7","1","candidate","managed","512","618.4544","1310398719","512","512" +"pressure-single","7","1","candidate","systemmem","512","603.1965","1322323848","512","512" +"pressure-single","8","0","candidate","managed","512","565.8334","1273281775","512","512" +"pressure-single","8","0","candidate","systemmem","512","600.4259","1313875730","512","512" +"pressure-single","8","1","baseline","managed","512","594.1966","1311432521","512","512" +"pressure-single","8","1","baseline","systemmem","512","654.9841","1362390691","512","512" diff --git a/docs/development/evidence/2026-11-08-upload-state/import-dependencies.json b/docs/development/evidence/2026-11-08-upload-state/import-dependencies.json new file mode 100644 index 0000000..5842a09 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/import-dependencies.json @@ -0,0 +1,38 @@ +[ + { + "module": "backend", + "baseline": [ + "ADVAPI32.dll", + "GDI32.dll", + "KERNEL32.dll", + "msvcrt.dll", + "SETUPAPI.dll", + "USER32.dll", + "VERSION.dll" + ], + "candidate": [ + "ADVAPI32.dll", + "GDI32.dll", + "KERNEL32.dll", + "msvcrt.dll", + "SETUPAPI.dll", + "USER32.dll", + "VERSION.dll" + ], + "added": [] + }, + { + "module": "bridge", + "baseline": [ + "bcrypt.dll", + "KERNEL32.dll", + "USER32.dll" + ], + "candidate": [ + "bcrypt.dll", + "KERNEL32.dll", + "USER32.dll" + ], + "added": [] + } +] diff --git a/docs/development/evidence/2026-11-08-upload-state/package-manifest.json b/docs/development/evidence/2026-11-08-upload-state/package-manifest.json new file mode 100644 index 0000000..fe0eeec --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/package-manifest.json @@ -0,0 +1,22 @@ +[ + { + "path": "d3d9.dll", + "bytes": 1692672, + "sha256": "A2407964045254C9D010E496D5A7BAB8D3AA2925960A48CB99F01BA603853391" + }, + { + "path": "d3d9.dll.rsym", + "bytes": 2194869, + "sha256": "F06843D4C61EE82E6CE0AFAE2B735EDD79D8629145DDE9490287FD0883B14FA3" + }, + { + "path": "backend\\dxvk-gta\\d3d9.dll", + "bytes": 8142848, + "sha256": "ECB34529DA46E6671D628D8BC56366017D17A625EDDB8B44F8226085252DA574" + }, + { + "path": "backend\\dxvk-gta\\d3d9.dll.rsym", + "bytes": 3500510, + "sha256": "A0BE3823C413791B000F46D943BEFF6AB50F4B1999829CCE85920331949C05B2" + } +] diff --git a/docs/development/evidence/2026-11-08-upload-state/retry-waits-summary.json b/docs/development/evidence/2026-11-08-upload-state/retry-waits-summary.json new file mode 100644 index 0000000..c880f99 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/retry-waits-summary.json @@ -0,0 +1,138 @@ +{ + "waits-pinned": { + "elapsedMs": { + "baselineMedian": 473.98235, + "candidateMedian": 497.65160000000003, + "pairedMedianPct": 4.522197273722961, + "aaMedianAbsPct": 9.565002562632808, + "aaMaxAbsPct": 10.843317663779061, + "wins": 2, + "rangePct": [ + -12.85321078731193, + 15.875126562200204 + ] + }, + "mainCycles": { + "baselineMedian": 159452123.5, + "candidateMedian": 102582777.0, + "pairedMedianPct": -34.96349497216307, + "aaMedianAbsPct": 2.4673109022314788, + "aaMaxAbsPct": 5.173596464009234, + "wins": 6, + "rangePct": [ + -40.14396035914176, + -28.081575970028606 + ] + }, + "csSyncUs": { + "baselineMedian": 0.0, + "candidateMedian": 0.0, + "pairedMedianPct": null, + "aaMedianAbsPct": null, + "aaMaxAbsPct": null, + "wins": 0, + "rangePct": null + }, + "gpuSyncUs": { + "baselineMedian": 0.0, + "candidateMedian": 0.0, + "pairedMedianPct": null, + "aaMedianAbsPct": null, + "aaMaxAbsPct": null, + "wins": 0, + "rangePct": null + }, + "csIdleUs": { + "baselineMedian": 414992.0, + "candidateMedian": 442581.5, + "pairedMedianPct": 5.715433521834312, + "aaMedianAbsPct": 9.108243284964491, + "aaMaxAbsPct": 10.14522483286784, + "wins": 2, + "rangePct": [ + -12.139436175967633, + 18.065239404021405 + ] + }, + "gpuIdleUs": { + "baselineMedian": 351228.0, + "candidateMedian": 370037.0, + "pairedMedianPct": 5.436402810600871, + "aaMedianAbsPct": 11.971489597992758, + "aaMaxAbsPct": 14.038037061752483, + "wins": 2, + "rangePct": [ + -13.952308592016049, + 19.075446220880753 + ] + } + }, + "waits-normal": { + "elapsedMs": { + "baselineMedian": 468.62555, + "candidateMedian": 474.25555, + "pairedMedianPct": -1.437942183650437, + "aaMedianAbsPct": 6.5887633232413645, + "aaMaxAbsPct": 15.745492872370924, + "wins": 4, + "rangePct": [ + -13.069587277726225, + 5.699459114484995 + ] + }, + "mainCycles": { + "baselineMedian": 210038382.0, + "candidateMedian": 125518523.5, + "pairedMedianPct": -40.50790999469035, + "aaMedianAbsPct": 4.178546786942672, + "aaMaxAbsPct": 16.104590618711214, + "wins": 6, + "rangePct": [ + -48.04933602378758, + -37.012738864037296 + ] + }, + "csSyncUs": { + "baselineMedian": 0.0, + "candidateMedian": 0.0, + "pairedMedianPct": null, + "aaMedianAbsPct": null, + "aaMaxAbsPct": null, + "wins": 0, + "rangePct": null + }, + "gpuSyncUs": { + "baselineMedian": 0.0, + "candidateMedian": 0.0, + "pairedMedianPct": null, + "aaMedianAbsPct": null, + "aaMaxAbsPct": null, + "wins": 0, + "rangePct": null + }, + "csIdleUs": { + "baselineMedian": 383421.5, + "candidateMedian": 409844.5, + "pairedMedianPct": 4.8772804439127455, + "aaMedianAbsPct": 7.055329887623962, + "aaMaxAbsPct": 11.43261695476021, + "wins": 2, + "rangePct": [ + -6.714759694776761, + 11.251670354666366 + ] + }, + "gpuIdleUs": { + "baselineMedian": 334383.5, + "candidateMedian": 337609.0, + "pairedMedianPct": 1.3019343586036958, + "aaMedianAbsPct": 3.63394909225786, + "aaMaxAbsPct": 18.133698549007992, + "wins": 3, + "rangePct": [ + -16.20441712871672, + 14.85095728814474 + ] + } + } +} diff --git a/docs/development/evidence/2026-11-08-upload-state/validated-benchmark-summary.json b/docs/development/evidence/2026-11-08-upload-state/validated-benchmark-summary.json new file mode 100644 index 0000000..6813c50 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/validated-benchmark-summary.json @@ -0,0 +1,246 @@ +{ + "uploads": [ + { + "profile": "pressure-mips", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 955.206, + "candidateMedianMs": 891.275, + "pairedMedianPct": -6.822, + "abRangePct": [ + -9.597, + 1.359 + ], + "aaMedianAbsolutePct": 3.992, + "aaMaximumAbsolutePct": 22.613, + "wins": 5, + "baselineMaps": [ + 5632 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1998121237.0, + "candidateMedian": 1884046026.5, + "pairedMedianPct": -6.987, + "abRangePct": [ + -10.541, + -3.912 + ], + "aaMedianAbsolutePct": 4.4, + "aaMaximumAbsolutePct": 7.141, + "wins": 6 + } + }, + { + "profile": "pressure-mips", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 941.887, + "candidateMedianMs": 866.871, + "pairedMedianPct": -8.705, + "abRangePct": [ + -13.871, + 0.054 + ], + "aaMedianAbsolutePct": 1.174, + "aaMaximumAbsolutePct": 5.043, + "wins": 5, + "baselineMaps": [ + 5632 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 2013993478.0, + "candidateMedian": 1851274252.5, + "pairedMedianPct": -7.375, + "abRangePct": [ + -11.194, + -0.149 + ], + "aaMedianAbsolutePct": 4.337, + "aaMaximumAbsolutePct": 5.748, + "wins": 6 + } + }, + { + "profile": "ordinary-mips", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 237.86, + "candidateMedianMs": 238.341, + "pairedMedianPct": -0.532, + "abRangePct": [ + -1.968, + 3.261 + ], + "aaMedianAbsolutePct": 1.555, + "aaMaximumAbsolutePct": 2.033, + "wins": 4, + "baselineMaps": [ + 0 + ], + "candidateMaps": [ + 0 + ], + "mainCycles": { + "baselineMedian": 231031670.0, + "candidateMedian": 238621156.5, + "pairedMedianPct": 2.242, + "abRangePct": [ + -12.877, + 21.523 + ], + "aaMedianAbsolutePct": 3.933, + "aaMaximumAbsolutePct": 5.531, + "wins": 2 + } + }, + { + "profile": "ordinary-mips", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 238.199, + "candidateMedianMs": 236.041, + "pairedMedianPct": -0.583, + "abRangePct": [ + -1.14, + 0.973 + ], + "aaMedianAbsolutePct": 1.584, + "aaMaximumAbsolutePct": 1.962, + "wins": 4, + "baselineMaps": [ + 0 + ], + "candidateMaps": [ + 0 + ], + "mainCycles": { + "baselineMedian": 234962428.0, + "candidateMedian": 235094278.5, + "pairedMedianPct": -0.533, + "abRangePct": [ + -7.436, + 16.321 + ], + "aaMedianAbsolutePct": 5.506, + "aaMaximumAbsolutePct": 9.514, + "wins": 3 + } + }, + { + "profile": "pressure-single", + "kind": "managed", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 692.309, + "candidateMedianMs": 736.975, + "pairedMedianPct": 5.633, + "abRangePct": [ + -3.982, + 17.049 + ], + "aaMedianAbsolutePct": 2.205, + "aaMaximumAbsolutePct": 5.515, + "wins": 2, + "baselineMaps": [ + 512 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1502586622.5, + "candidateMedian": 1555556582.5, + "pairedMedianPct": 3.032, + "abRangePct": [ + -1.361, + 7.434 + ], + "aaMedianAbsolutePct": 3.623, + "aaMaximumAbsolutePct": 5.503, + "wins": 2 + } + }, + { + "profile": "pressure-single", + "kind": "systemmem", + "abPairs": 6, + "aaPairs": 3, + "baselineMedianMs": 679.114, + "candidateMedianMs": 700.328, + "pairedMedianPct": 1.78, + "abRangePct": [ + -5.414, + 10.316 + ], + "aaMedianAbsolutePct": 1.117, + "aaMaximumAbsolutePct": 7.874, + "wins": 1, + "baselineMaps": [ + 512 + ], + "candidateMaps": [ + 512 + ], + "mainCycles": { + "baselineMedian": 1485477302.5, + "candidateMedian": 1503590871.5, + "pairedMedianPct": 1.59, + "abRangePct": [ + -1.561, + 5.609 + ], + "aaMedianAbsolutePct": 2.275, + "aaMaximumAbsolutePct": 5.425, + "wins": 1 + } + } + ], + "retry": { + "elapsedMs": { + "baselineMedian": 617.692, + "candidateMedian": 854.651, + "pairedMedianPct": 42.529, + "abRangePct": [ + 8.525, + 61.39 + ], + "aaMedianAbsolutePct": 9.991, + "aaMaximumAbsolutePct": 14.065, + "wins": 0 + }, + "mainCpuMs": { + "baselineMedian": 46.875, + "candidateMedian": 15.625, + "pairedMedianPct": -66.667, + "abRangePct": [ + -100.0, + 300.0 + ], + "aaMedianAbsolutePct": 60.0, + "aaMaximumAbsolutePct": 75.0, + "wins": 4 + }, + "mainCycles": { + "baselineMedian": 187860732.5, + "candidateMedian": 132997240.5, + "pairedMedianPct": -27.564, + "abRangePct": [ + -33.113, + -20.383 + ], + "aaMedianAbsolutePct": 4.263, + "aaMaximumAbsolutePct": 7.048, + "wins": 6 + } + } +} diff --git a/docs/development/evidence/2026-11-08-upload-state/validated-retry-ab.csv b/docs/development/evidence/2026-11-08-upload-state/validated-retry-ab.csv new file mode 100644 index 0000000..10489ee --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/validated-retry-ab.csv @@ -0,0 +1,19 @@ +"pair","slot","version","elapsedMs","mainCpuMs","mainCycles" +"0","0","baseline","745.0914","62.5","203822086" +"0","1","baseline","849.8888","15.625","201480914" +"1","0","baseline","888.4145","46.875","202112011" +"1","1","candidate","964.1491","15.625","135187476" +"2","0","candidate","835.3928","0","127421095" +"2","1","baseline","619.5688","46.875","189493317" +"3","0","baseline","631.1895","78.125","191158818" +"3","1","baseline","691.4184","31.25","199308667" +"4","0","baseline","615.8156","15.625","186228148" +"4","1","candidate","873.9099","62.5","134430860" +"5","0","candidate","755.5854","15.625","125110466" +"5","1","baseline","524.1026","78.125","172125891" +"6","0","baseline","979.9796","125","228024613" +"6","1","baseline","882.0663","125","211953504" +"7","0","baseline","861.8497","46.875","217955485" +"7","1","candidate","1390.9354","15.625","173528895" +"8","0","candidate","791.7915","62.5","131563621" +"8","1","baseline","553.1314","15.625","174686650" diff --git a/docs/development/evidence/2026-11-08-upload-state/validated-upload-ab.csv b/docs/development/evidence/2026-11-08-upload-state/validated-upload-ab.csv new file mode 100644 index 0000000..4248bc7 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/validated-upload-ab.csv @@ -0,0 +1,109 @@ +"profile","pair","slot","version","kind","iterations","ms","mainCycles","maps","unmaps" +"pressure-mips","0","0","baseline","managed","512","1090.9504","2278968005","5632","5632" +"pressure-mips","0","0","baseline","systemmem","512","1084.5222","2265965835","5632","5632" +"pressure-mips","0","1","baseline","managed","512","1337.6491","2441717341","5632","5632" +"pressure-mips","0","1","baseline","systemmem","512","1071.7922","2167699954","5632","5632" +"pressure-mips","1","0","baseline","managed","512","950.2956","2000880244","5632","5632" +"pressure-mips","1","0","baseline","systemmem","512","945.4286","1995924700","5632","5632" +"pressure-mips","1","1","candidate","managed","512","928.2722","1922610333","512","512" +"pressure-mips","1","1","candidate","systemmem","512","874.939","1846794258","512","512" +"pressure-mips","2","0","candidate","managed","512","868.673","1870387993","512","512" +"pressure-mips","2","0","candidate","systemmem","512","858.8039","1851278594","512","512" +"pressure-mips","2","1","baseline","managed","512","960.1172","1995362230","5632","5632" +"pressure-mips","2","1","baseline","systemmem","512","953.7487","1996594950","5632","5632" +"pressure-mips","3","0","baseline","managed","512","1002.2513","2069277425","5632","5632" +"pressure-mips","3","0","baseline","systemmem","512","1019.5478","2069848784","5632","5632" +"pressure-mips","3","1","baseline","managed","512","962.24","2009814061","5632","5632" +"pressure-mips","3","1","baseline","systemmem","512","1014.8729","2064427586","5632","5632" +"pressure-mips","4","0","baseline","managed","512","1000.2334","2085289059","5632","5632" +"pressure-mips","4","0","baseline","systemmem","512","978.7224","2032314118","5632","5632" +"pressure-mips","4","1","candidate","managed","512","936.3529","1924497867","512","512" +"pressure-mips","4","1","candidate","systemmem","512","979.2515","2029286532","512","512" +"pressure-mips","5","0","candidate","managed","512","890.5578","1868029204","512","512" +"pressure-mips","5","0","candidate","systemmem","512","834.677","1851269911","512","512" +"pressure-mips","5","1","baseline","managed","512","878.6183","1967775444","5632","5632" +"pressure-mips","5","1","baseline","systemmem","512","931.3455","2066145540","5632","5632" +"pressure-mips","6","0","baseline","managed","512","885.8486","1980900454","5632","5632" +"pressure-mips","6","0","baseline","systemmem","512","873.3887","1938248184","5632","5632" +"pressure-mips","6","1","baseline","managed","512","867.5612","1893738072","5632","5632" +"pressure-mips","6","1","baseline","systemmem","512","829.3423","1826840893","5632","5632" +"pressure-mips","7","0","baseline","managed","512","885.6136","1943956770","5632","5632" +"pressure-mips","7","0","baseline","systemmem","512","898.2594","1942991862","5632","5632" +"pressure-mips","7","1","candidate","managed","512","800.6176","1739043257","512","512" +"pressure-mips","7","1","candidate","systemmem","512","773.6583","1725488682","512","512" +"pressure-mips","8","0","candidate","managed","512","891.993","1897704060","512","512" +"pressure-mips","8","0","candidate","systemmem","512","886.7127","1921360422","512","512" +"pressure-mips","8","1","baseline","managed","512","961.7933","2076464738","5632","5632" +"pressure-mips","8","1","baseline","systemmem","512","938.3463","2031392006","5632","5632" +"ordinary-mips","0","0","baseline","managed","512","242.577","273166534","0","0" +"ordinary-mips","0","0","baseline","systemmem","512","243.2594","274719310","0","0" +"ordinary-mips","0","1","baseline","managed","512","237.6447","258056757","0","0" +"ordinary-mips","0","1","baseline","systemmem","512","238.4869","248582489","0","0" +"ordinary-mips","1","0","baseline","managed","512","241.5158","246241138","0","0" +"ordinary-mips","1","0","baseline","systemmem","512","238.2005","236404992","0","0" +"ordinary-mips","1","1","candidate","managed","512","240.9034","242447409","0","0" +"ordinary-mips","1","1","candidate","systemmem","512","235.7883","233734677","0","0" +"ordinary-mips","2","0","candidate","managed","512","238.6408","244317291","0","0" +"ordinary-mips","2","0","candidate","systemmem","512","236.2928","261744989","0","0" +"ordinary-mips","2","1","baseline","managed","512","243.2122","234758150","0","0" +"ordinary-mips","2","1","baseline","systemmem","512","238.3721","238775614","0","0" +"ordinary-mips","3","0","baseline","managed","512","235.092","230014013","0","0" +"ordinary-mips","3","0","baseline","systemmem","512","235.6148","233022762","0","0" +"ordinary-mips","3","1","baseline","managed","512","235.4688","239060338","0","0" +"ordinary-mips","3","1","baseline","systemmem","512","239.346","230184278","0","0" +"ordinary-mips","4","0","baseline","managed","512","242.818","227305190","0","0" +"ordinary-mips","4","0","baseline","systemmem","512","240.8203","226490965","0","0" +"ordinary-mips","4","1","candidate","managed","512","238.0402","234794904","0","0" +"ordinary-mips","4","1","candidate","systemmem","512","240.1144","226635371","0","0" +"ordinary-mips","5","0","candidate","managed","512","234.0958","223901670","0","0" +"ordinary-mips","5","0","candidate","systemmem","512","235.483","216156030","0","0" +"ordinary-mips","5","1","baseline","managed","512","233.9386","221272451","0","0" +"ordinary-mips","5","1","baseline","systemmem","512","238.1975","233519864","0","0" +"ordinary-mips","6","0","baseline","managed","512","239.0508","238918587","0","0" +"ordinary-mips","6","0","baseline","systemmem","512","236.8986","240979570","0","0" +"ordinary-mips","6","1","baseline","managed","512","235.3334","247293646","0","0" +"ordinary-mips","6","1","baseline","systemmem","512","235.0537","254248023","0","0" +"ordinary-mips","7","0","baseline","managed","512","232.0249","216332630","0","0" +"ordinary-mips","7","0","baseline","systemmem","512","236.6895","218752830","0","0" +"ordinary-mips","7","1","candidate","managed","512","239.5901","262894267","0","0" +"ordinary-mips","7","1","candidate","systemmem","512","238.9926","254455324","0","0" +"ordinary-mips","8","0","candidate","managed","512","232.3087","223120567","0","0" +"ordinary-mips","8","0","candidate","systemmem","512","234.0098","236453880","0","0" +"ordinary-mips","8","1","baseline","managed","512","234.205","256099276","0","0" +"ordinary-mips","8","1","baseline","systemmem","512","232.6029","240940367","0","0" +"pressure-single","0","0","baseline","managed","512","687.8004","1492442933","512","512" +"pressure-single","0","0","baseline","systemmem","512","692.8335","1503337372","512","512" +"pressure-single","0","1","baseline","managed","512","701.4079","1471344630","512","512" +"pressure-single","0","1","baseline","systemmem","512","700.5713","1517919841","512","512" +"pressure-single","1","0","baseline","managed","512","666.669","1475310812","512","512" +"pressure-single","1","0","baseline","systemmem","512","652.1018","1453766731","512","512" +"pressure-single","1","1","candidate","managed","512","753.5993","1561134092","512","512" +"pressure-single","1","1","candidate","systemmem","512","719.3749","1511940047","512","512" +"pressure-single","2","0","candidate","managed","512","780.706","1564239321","512","512" +"pressure-single","2","0","candidate","systemmem","512","737.622","1563387557","512","512" +"pressure-single","2","1","baseline","managed","512","666.9916","1456004073","512","512" +"pressure-single","2","1","baseline","systemmem","512","681.9622","1480355929","512","512" +"pressure-single","3","0","baseline","managed","512","703.6441","1554371982","512","512" +"pressure-single","3","0","baseline","systemmem","512","661.7832","1473902515","512","512" +"pressure-single","3","1","baseline","managed","512","688.1279","1498061309","512","512" +"pressure-single","3","1","baseline","systemmem","512","659.1407","1440366706","512","512" +"pressure-single","4","0","baseline","managed","512","679.1135","1483265927","512","512" +"pressure-single","4","0","baseline","systemmem","512","675.4269","1459675874","512","512" +"pressure-single","4","1","candidate","managed","512","720.3514","1539277673","512","512" +"pressure-single","4","1","candidate","systemmem","512","681.2802","1486512575","512","512" +"pressure-single","5","0","candidate","managed","512","870.8793","1685770420","512","512" +"pressure-single","5","0","candidate","systemmem","512","764.0483","1582958118","512","512" +"pressure-single","5","1","baseline","managed","512","827.8849","1648069296","512","512" +"pressure-single","5","1","baseline","systemmem","512","744.0056","1561997623","512","512" +"pressure-single","6","0","baseline","managed","512","698.2989","1549371206","512","512" +"pressure-single","6","0","baseline","systemmem","512","709.3698","1531187070","512","512" +"pressure-single","6","1","baseline","managed","512","659.7849","1464105801","512","512" +"pressure-single","6","1","baseline","systemmem","512","653.5113","1448117162","512","512" +"pressure-single","7","0","baseline","managed","512","738.6164","1557034807","512","512" +"pressure-single","7","0","baseline","systemmem","512","676.2657","1490598676","512","512" +"pressure-single","7","1","candidate","managed","512","709.2069","1549979073","512","512" +"pressure-single","7","1","candidate","systemmem","512","678.8145","1495241696","512","512" +"pressure-single","8","0","candidate","managed","512","690.2515","1501191230","512","512" +"pressure-single","8","0","candidate","systemmem","512","667.9917","1474222810","512","512" +"pressure-single","8","1","baseline","managed","512","705.5043","1521907318","512","512" +"pressure-single","8","1","baseline","systemmem","512","706.2246","1497599988","512","512" diff --git a/docs/development/evidence/2026-11-08-upload-state/waits-normal-retry-ab.csv b/docs/development/evidence/2026-11-08-upload-state/waits-normal-retry-ab.csv new file mode 100644 index 0000000..5d41fdc --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/waits-normal-retry-ab.csv @@ -0,0 +1,19 @@ +"pair","slot","version","elapsedMs","mainCpuMs","mainCycles" +"0","0","baseline","430.0799","93.75","197633251" +"0","1","baseline","497.7981","140.625","229461277" +"1","0","baseline","473.7927","109.375","211992431" +"1","1","candidate","471.0445","46.875","124762115" +"2","0","candidate","417.2898","78.125","113234871" +"2","1","baseline","480.0274","93.75","217966167" +"3","0","baseline","463.8139","46.875","206625168" +"3","1","baseline","472.3075","62.5","208925406" +"4","0","baseline","463.4584","78.125","204910844" +"4","1","candidate","409.798","78.125","111725859" +"5","0","candidate","486.2618","62.5","126274932" +"5","1","baseline","460.0419","62.5","208084333" +"6","0","baseline","427.2046","93.75","192696562" +"6","1","baseline","455.3521","125","200748478" +"7","0","baseline","500.849","46.875","214704873" +"7","1","candidate","489.3503","62.5","129106387" +"8","0","candidate","477.4666","62.5","127585053" +"8","1","baseline","455.2755","109.375","202556915" diff --git a/docs/development/evidence/2026-11-08-upload-state/waits-pinned-retry-ab.csv b/docs/development/evidence/2026-11-08-upload-state/waits-pinned-retry-ab.csv new file mode 100644 index 0000000..d41b2c9 --- /dev/null +++ b/docs/development/evidence/2026-11-08-upload-state/waits-pinned-retry-ab.csv @@ -0,0 +1,19 @@ +"pair","slot","version","elapsedMs","mainCpuMs","mainCycles" +"0","0","baseline","431.6013","15.625","153305830" +"0","1","baseline","478.4012","46.875","161237255" +"1","0","baseline","448.2551","62.5","153555698" +"1","1","candidate","461.9087","46.875","99410509" +"2","0","candidate","502.6296","15.625","103775846" +"2","1","baseline","474.1858","31.25","158839071" +"3","0","baseline","461.4395","140.625","152551549" +"3","1","baseline","417.3028","78.125","148787628" +"4","0","baseline","500.5372","15.625","161165677" +"4","1","candidate","466.2749","31.25","99603045" +"5","0","candidate","501.6047","15.625","105630431" +"5","1","baseline","461.4646","109.375","156911603" +"6","0","baseline","534.2546","62.5","170648837" +"6","1","baseline","567.8743","0","169174698" +"7","0","baseline","566.5137","31.25","169389269" +"7","1","candidate","493.6985","15.625","101389708" +"8","0","candidate","548.9919","46.875","115116352" +"8","1","baseline","473.7789","78.125","160065176" diff --git a/docs/development/known-audit-findings.md b/docs/development/known-audit-findings.md index d92676e..f66f56b 100644 --- a/docs/development/known-audit-findings.md +++ b/docs/development/known-audit-findings.md @@ -4,6 +4,26 @@ This file separates evidence from assumptions for the `v0.1.0-alpha.1` release. It is intentionally conservative: a passing source or probe test is not evidence that every GTA mod combination is stable. +## Current Source Configuration Policy + +The Bridge owns explicit process affinity and priority through `[Affinity]` +`Enable=1`, `Mask`, and `Priority`. The DXVK factories preserve the host's +process policy. Optional `PerThread` and `Mmcss` scheduling still follows its +own configuration on the threads that own those scopes. + +`[ProperShadersEffectOptimization] DirectConstants` is quarantined after live +captures showed geometry and sprite state leaking into later draws. Production +always uses an effective value of `0`, including when an older profile requests +`1` or omits the key. The Bridge logs both the requested value and the effective +value with `reason=production-quarantine`. Both repository INI profiles declare +`DirectConstants=0`; changing the key cannot reopen the production gate. + +The configuration regression loads the actual Bridge and backend DLLs in +isolated child processes. It checks process policy around the real factories, +Bridge-to-DXVK policy preservation, root/legacy configuration selection, and +the Bridge's effective configuration diagnostic. This gate does not establish +visual correctness for the quarantined path or amend a prior release's results. + ## Verified by the Local Release Gate - Clean x86 Release builds complete for the Bridge and DXVK backend. diff --git a/docs/development/performance-validation.md b/docs/development/performance-validation.md new file mode 100644 index 0000000..6b6439e --- /dev/null +++ b/docs/development/performance-validation.md @@ -0,0 +1,188 @@ +# DLL 性能改动的验证方法 + +优先降低 CPU 端重复工作,并保留画质、公开 COM/SDK 接口与状态恢复语义。 +构建产物保存在 `out/`;性能验证不会安装 DLL,也不会修改游戏配置。 + +## 当前单项绑定优化 + +启用 `d3d9.gtaSaCoalesceSamplerBindings` / `d3d9.gtaSaCoalesceTextureBindings` +时,原实现即使只有一个 dirty slot,也构造并捕获完整的 21 项数组。 +`TryD3D9SingleBinding` 对非零单 bit mask 复用现有 `BindSampler` / `BindTexture`, +避免该数组的初始化、移动和命令载荷。零项、多项继续使用原批处理。 +单项函数与批处理继续共享现有 binding cache、sampler key 和资源引用语义。 +此改动没有新增运行时配置开关。 + +第一批还测量了常量比较与更新共用扫描的方案。其尾部变化微基准有收益,但部分 +不变/整段变化和组合 draw 场景出现回退,因此已撤回全部常量生产代码改动。 +新增的常量行为回归保留,避免后续优化破坏原行为。 + +截至第二批,控制场景复验确认三项 16-register 不变调用存在稳定回退。曾测试比较循环对齐、 +局部更新扫描和双寄存器比较,部分控制场景有收益,但均在完整 on/off 矩阵引入其他 +回退,未达到保留标准。全部第二批常量生产代码实验已撤回,继续保留第一批已验证的 +单项绑定优化。三项已确认的控制回退仍未修复,不能将本批工具和回归补强视作 FPS 收益。 +实验 DLL、二进制布局诊断和原始测量记录保存在本地 `out/perf/20261022-controls/`。 + +## 第三批:常量控制布局修复(2026-10-22) + +三项已确认的 16-register 不变控制回退已恢复到第一批之前基线附近。本轮保持比较、复制、 +状态记录、journal 和 audit 算法及公开接口;修复 x86 Clang 构建中受相邻函数大小影响的热点布局。 +PS/int 早期比较使用 `code_align(64)`,VS float 保持已测的 16 字节位置。VS int 在设备 setter 的 +journal 捕获之后、更新调用之前加入预头对齐 directive,绕过当前 while 对齐 metadata 的丢失。 +指令布局由正常编译、汇编和链接生成;生产 DLL 不进行运行时代码改写,也不修改游戏调度设置。 + +最终六个私有 setter 的有效指令和控制流经忽略 NOP/重定位后均与基线一致。 +共享 helper 内的早期 directive 候选引入过额外指针 spill,虽然行为测试通过,仍造成约 4.68% +短写入回退,因此拒绝。将 directive 移至设备调用点后,栈帧恢复为 8 字节,spill 消失, +该 case 正式普通调度复验约 +0.66%,固定调用线程诊断约 -0.62%。 + +固定公开 COM 入口、设备、数据和 clone allocation 的受控实验支持整段代码布局具有因果影响。 +只移动孤立 27 字节循环没有复现 12–15% 回退,故不把问题简单归因为循环头的单一对齐值, +也不声称已确定具体 CPU 取指/解码/分支预测机制。 + +`tests/constant-layout-test.py` 读取最终 PE/COFF 并反汇编,检查四个比较循环和 VS int 更新循环的 +已测位置,作为 required gate。它不是整函数指纹检查;即使该 gate 通过,编译器或代码结构改变 +仍需重复 A/B。新增逐 lane 真 COM 测试验证每个首次变化位置、单 bit 特殊值、4/8/12 字节输入偏移、 +完整 bank 哨兵和相同重写;每种配置共 7567 项检查。 + +最终候选完整流水线 54 passed / 0 failed / 2 外部审计 skipped。普通调度 12 进程×4轮、on/off 各 +78 case 的正式 A/B 已记录;三项目标 on 调用耗时约改善 10–13%,历史基线对照约在 -1.70%~+0.36%。 +某个尾部变化 case 在普通调度点估计约 +5.18%,固定调用线程复验约 +0.16%,对应 A/A 约 +1.41%; +不将这些点估计写成零,也不据此宣称所有 workload 或 GTA FPS 都有提升。 + +本机完整原始证据和候选边界见 `out/reports/performance-controls-rootcause-20261022.md` / `.json`。 +游戏测试固定为葛洛夫街,重点测街区切换与室内到室外;微基准不证明该瓶颈已经解决。 + +## 真实行为回归 + +- `renderstack-dxvk-state-dedup-test` 在 Release 中使用始终有效的检查,验证空 mask、 + 所有 21 个单 bit mask 的槽位与调用次数、多 bit mask 的回退。 +- `api-state-runtime-tests` 通过真实公开 COM 调用验证 float/int 的 bitwise 往返、 + NaN、正负零、未按 16 字节对齐的数据、最大范围、状态块和 journal。包含选择性捕获 + 关闭前后的重叠写入,明确锁定完整原范围快照。 +- 逐 lane 回归逐一改变 16 个寄存器的每个分量,使用 4/8/12 字节错位输入;首次写入后立即全 bank 读回,再单独验证相等重写。 +- 常量扫描边界回归遍历每种 stage/type、多个奇偶长度及所有首次变化位置;首次变化 + 后立即读取整个常量 bank,再单独验证相等重写,包含提交范围前后的 canary。 +- `constant-submit-benchmark ... check` 在真实 D3D9/Vulkan 设备上运行 shader, + 使用 event query 等待并读回像素。测试采样槽 0/5/15,红/蓝纹理交替、棋盘纹理的 + 点/线性过滤、边框色、解绑、多槽加权采样,以及 stateblock/journal 恢复。 +- `tools/test.ps1` 在 reference / optimized 两种配置运行上述 GPU 回归。 + `-SkipGpuRuntimeProbes` 会明确记录对应的 skip,不能作为 GPU 验证通过。 + +## 可重复的 A/B + +先保存使用相同工具链构建的基线与候选 x86 DLL,再使用同一个 Release probe: + +```powershell +pwsh -NoProfile -File tests/constant-submit-benchmark.ps1 ` + -Baseline -Candidate ` + -Probe out/build/api-projects-x86/Release/constant-submit-benchmark.exe ` + -OutputDirectory out/perf/binding-ab -Pairs 12 -Dedup on +``` + +`-Dedup on` 对应 probe 的 `on` 配置,同时开启常量去重、两项 binding coalesce 和 +resource binding cache;`off` 同时关闭这四项。每个配置中基线与候选使用相同设置。 +脚本不修改优先级、亲和性、系统电源计划或驱动设置。 + +每组交替 AB/BA 进程顺序,预热后记录每次提交的 QPC 时间。每个进程应输出 78 个 +固定 case:68 个常量 setter case、5 种 draw 序列及各自包含 GPU 完成的时间。 +draw 序列包含仅常量、单纹理、单采样器、单槽纹理和采样器、两个活动槽同时切换。 +shader 实际使用纹理;计时纹理为相同白色,颜色差异在独立像素回归验证。 + +结果 JSON 保存 DLL/probe 哈希、原始样本、中位数、成对变化中位数和更快的组数。 +脚本检查每个进程的 case 集合、重复、迭代次数及时间有效性。比较各序列,避免仅选取 +最快的微基准。不要并行运行编译、其他 GPU 测试或另一份基准来污染采样。 + +小型合成场景不能证明 GTA 的 FPS 提升。只有用候选在相同游戏场景测量帧时间,才能 +判断最终 FPS 收益;不安装候选时应明确保留这项边界。 + +## 控制场景的配对复验 + +跨进程 AB/BA 仍可能受调度和频率变化影响。第二批用同一 DLL 做旧流程 A/A 时, +曾出现约 31% 的组合 draw 假差异。因此新增同进程配对模式,先校准再解释 A/B: + +```powershell +pwsh -NoProfile -File tests/control-path-benchmark.ps1 ` + -Baseline -Candidate ` + -Probe out/build/api-projects-x86/Release/constant-submit-benchmark.exe ` + -OutputDirectory out/perf/control-ab -Processes 12 -Rounds 4 ` + -Profile on -Cpu any -Cases all +``` + +- `pair` 在同一调用点测量两个真实 D3D9/Vulkan fixture,按 seed 打乱 workload + 顺序、轮换 DLL/device 创建顺序、每轮交替 AB/BA。每个 fixture 先完成 127 项真实 + 行为检查,包括 shader 像素读回。相同路径共享同一设备;不同路径要求模块基址不同, + 并完成 254 项检查。需要同时测量共享设备 A/A 和相同内容的双模块 A/A。 +- `all` 包含原有全部 78 个 case;`controls` 包含 9 个重点控制 case。每个进程先对 + 各轮成对百分比取中位数,再跨独立进程取中位数;报告保留各进程变化和全部样本。 +- `Cpu any` 为正常调度;可用 `Cpu 0` 做定位诊断,仅临时固定 probe 调用线程,退出 + 时恢复原 affinity。它不改变 DLL 调度策略,也不修改游戏、进程优先级、电源或驱动。 + QPC 给出 ns/call;QueryThreadCycleTime 单独记录 cycles/call,不将 cycles 换算成时间。 +- runner 严格校验检查数量、元数据与模块拓扑、固定迭代数、case/round/variant 完整性、 + 有限正计时及固定 CPU 一致性。拒绝重用已有输出命名空间,运行前后按路径检查 + DLL/probe 的 SHA256,避免把旧成功报告与新原始数据混合。 +- `control-path-protocol-test.ps1` 用真实设备输出的变异副本验证缺少检查、错误迭代、 + 重复/缺失样本、错误模块/affinity 元数据等会失败。布局诊断脚本仅保存在实验记录中, + 对齐方案已拒绝,因此不把它作为最终版本的 required gate。 +- 完整流水线在 reference/optimized 配置各运行共享和双模块 pair、协议回归与原有 + 状态和像素测试。CI 只验证行为和输出完整性,不使用机器相关的计时阈值。 + +## 工程参考 + +- [NVIDIA Nsight 工作流](https://docs.nvidia.com/nsight-graphics/UserGuide/expected-workflow.html): + 先确定 CPU/GPU 瓶颈,再定位对应开销。 +- [Microsoft D3D9 API profiling](https://learn.microsoft.com/en-us/windows/win32/direct3d9/accurately-profiling-direct3d-api-calls): + 状态调用可能延迟到 draw,需测量包含 draw 的序列和完成边界。 +- [DXVK PR 5686](https://github.com/doitsujin/dxvk/pull/5686): + 常量管理优化的微基准收益不能直接代替游戏 FPS 验证。 +- [Google Benchmark 降低方差](https://github.com/google/benchmark/blob/main/docs/reducing_variance.md) + 和[随机交错](https://github.com/google/benchmark/blob/main/docs/random_interleaving.md): + 控制系统噪声,并用交错重复测量评估优化。 +- [Clang code_align](https://clang.llvm.org/docs/AttributeReference.html#code-align): + 对循环指定代码对齐;实际收益仍由最终二进制的本地 A/B 决定。 +- [Microsoft QueryThreadCycleTime](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-querythreadcycletime): + 原始线程周期计数不应直接换算为耗时。 + +布局 directive 参考:[GNU assembler p2align](https://sourceware.org/binutils/docs/as/P2align.html)。 +它请求汇编位置的边界,并不保证某个 C++ 语法节点在最终代码中的位置;因此以链接后检查和 A/B 为准。 + +## 当前候选的验证记录 + +2026-11-09 发布快照包含下述历史工作及后续诊断、状态生命周期和上传映射改动。 +最新上传/重试候选的实际测量、回退数据和验收限制见 +[2026-11-08 上传与重试报告](upload-state-20261108.md)。其中的 CPU 周期收益 +不代表游戏 FPS 或整个 Present 流程的吞吐收益;该快照通过源码/行为验证后发布为候选。 + +## 纹理绑定与资源生命周期验证(2026-10-27) + +NVIDIA Skills 当前目录没有直接面向本项目 D3D9/DXVK DLL 的源代码优化技能。本轮使用其 +技能发现流程,并参考上面的 NVIDIA Vulkan / Nsight 方法,先验证已有 CPU 成本,不引入 +新的运行时 SDK,也不修改驱动设置或游戏安装。 + +`constant-submit-benchmark on|off lifetime` 在真实 D3D9/Vulkan 设备上执行 423 项检查。 +它覆盖单槽与双槽的真实换图、无 draw 的换出/恢复、stateblock/journal 恢复、线性/sRGB +视图、释放应用持有的纹理后再显式等待 GPU,以及 Reset 后重复验证。队列生命周期用多个 +不同 RT 保存各次 draw,后续 draw 会替换旧缓存 view,避免最后一个缓存引用意外掩盖问题。 +这不是对所有可能线程调度交错的穷举。新模式独立于原有 check / bench / pair 的检查数量。 +混合命中像素检查分别验证“第一槽命中、第二槽换图”和相反次序,避免只用白色计时纹理 +掩盖 pending 数组压缩后的错误槽位。重复计时使用归档、固定哈希的 benchmark 可执行文件; +随后追加生命周期检查时不改写该归档计时文件。 + +配对基准新增 `-Cases textures`:单槽缓存命中、双槽命中、混合命中、sRGB 切换,以及原有 +单槽/双槽换绑控制,CPU 提交与 GPU 完成合计 12 个 case。旧 `controls` 的 9 个和 `all` +的 78 个 case 保持不变;输出仍经过完整性、迭代数、哈希和模块拓扑检查。 + +```powershell +pwsh -NoProfile -File tests/control-path-benchmark.ps1 ` + -Baseline -Candidate ` + -Probe out/build/api-projects-x86/Release/constant-submit-benchmark.exe ` + -OutputDirectory out/perf/texture-ab -Processes 12 -Rounds 4 ` + -Profile on -Cpu any -Cases textures +``` + +命中路径改善必须同时检查换绑控制和原有常量矩阵;不能把局部减少引用计数直接写成 +GTA FPS 收益。链接后机器码检查只补充成本证据,不能替代像素、生命周期和重复计时。 + +本轮标量、指针快照和仅批量三种候选均未达到控制场景的保留标准,生产源码已逐字节恢复 +到本轮输入。保留的是上述真实行为回归、纹理 workload 与协议验证,不是新的 DLL 性能收益。 +尝试用过的原子操作机器码诊断脚本及拒绝候选保存在 +`out/perf/20261027-nvidia-binding/`,不将未保留优化的成本约束注册成稳定版本的必需 gate。 diff --git a/docs/development/streaming-config-preparation.md b/docs/development/streaming-config-preparation.md new file mode 100644 index 0000000..ebd765b --- /dev/null +++ b/docs/development/streaming-config-preparation.md @@ -0,0 +1,59 @@ +# Offline Improved Streaming interval preparation + +The first streaming batch corrects a configuration consumer mismatch. It does +not change either RenderStack DLL, create streaming threads/hooks, alter memory +budgets, or apply files to the game installation. + +The audited `ImprovedStreaming.SA.asi` v1.1 (SHA256 +`D7BD005EDE20F00B358B75C4D4844618024F2A74AAF60957553EBD964BEE527C`) +reads `Settings.RemoveUnusedInterval`, default `60`, at RVA `0x73E9`. +`RemoveUnusedIntervalMs=750` therefore leaves the default in effect. This is +evidence for that binary, not a compatibility promise for every plugin release. + +Run from the checkout using PowerShell 7: + +```powershell +pwsh -NoProfile -File tools/prepare-streaming-config.ps1 ` + -Source '' ` + -OutputDirectory '/out/candidates/streaming-interval' +``` + +The destination must be a new directory under this checkout's `out/`. The tool +emits a full `ImprovedStreaming.ini` and `preparation.json` with input/output +hashes, the preserved interval, and `applied=false`. It changes only the exact +key in `[Settings]`; comments, other sections/keys, the value, UTF-8 BOM, and LF +or CRLF bytes are preserved. A canonical key is copied unchanged. Missing, +duplicate, conflicting, malformed, or unsupported-encoding inputs are rejected. +No apply/deployment option exists. Keep both the source and preparation record +for review; this command does not authorize game installation. + +The value is preserved because this batch repairs the requested policy rather +than selecting a new policy. A longer reclaim interval can retain resources +longer during streaming pressure. Actual churn, loading waits and resource +residency still require controlled game measurements before rollout; offline +acceptance alone does not establish a net FPS gain. + +## Regression and evidence limits + +```powershell +pwsh -NoProfile -File tests/streaming-config-regression-test.ps1 +pwsh -NoProfile -File tools/test.ps1 -BridgeReference '' +``` + +The first command compiles a native x86 test executable. It reads real fixture +files with the unmodified upstream INI reader, and executes the installed v1.1 +interval-decision instruction fragment relocated into test-owned memory. +The ASI is never loaded, its DllMain never runs, and GTA is never started. +Input preservation, failure behavior, interval boundaries, timer wrap and RAM +bypass are checked. The full test orchestrator runs this gate as required. +Upstream hashes, license and the exact fragment/relocations are documented in +`tests/vendor/loadwholemap-ini/README.md`. + +The repeated A/B uses 12 alternating AB/BA rounds, a simulated 10-second clock, +16-ms gate visits and sustained outer pressure. The native decision returns +156 permits for 60 ms and 13 for 750 ms. With RAM above the supplied cap, both +return 625 permits. These counts are conditional decision-fragment results; +they do not model the outer callback cadence, eviction success, I/O, +decompression, RenderWare conversion, GPU upload or game frame time. The public +PC plugin source identifies itself as v1.0, so it is not used as a full v1.1 +policy replacement. diff --git a/docs/development/upload-state-20261108.md b/docs/development/upload-state-20261108.md new file mode 100644 index 0000000..21ccec2 --- /dev/null +++ b/docs/development/upload-state-20261108.md @@ -0,0 +1,107 @@ +# 2026-11-08 纹理映射与失败重试优化 + +两条路径已实现候选并通过自动行为回归。受压 mip-chain 上传和失败重试的主线程工作量减少;整个 Present 流程的墙钟收益尚未确认,本轮不视为游戏性能验收通过。没有部署、启动游戏、提交或推送。 + +## 触发证据与本轮范围 + +最新现场来自 PID 35304 / captureId 2,见 `out/perf/20261108-f8-35304-2/findings.md`。107.56 ms 长帧中有纹理数据准备栈;同一采集中出现约 2,500 条 DayVector 签名拒绝日志。现场采样不能单独证明源映射驱逐是这次长帧的原因,本轮先修复独立夹具能够重现的机制。 + +1. **通用上传源映射生命周期**:UpdateTexture 和 UploadManagedTexture 的整个 mip/face 上传事务中保护正在使用的源映射。其他 LRU 冷映射仍在原回收点回收,事务结束后源映射重新参与回收。没有改变暂存缓冲、CPU 数据复制、上传顺序、dirty rect 或公开接口,也没有按 MOD 名称分支。 +2. **既有兼容补丁的失败重试**:已 PIN 的模块记录第一个不匹配的调用点。后续安全点只复查该六字节签名,稳定拒绝时跳过完整预检和日志;签名恢复则重新预检和安装。临时 PIN 错误继续重试。这是既有兼容补丁机制的开销修复,不是通用 sampler 状态调用的去重。 + +没有新增加载线程或并行源页预热,也没有改周期性 CRC 重试路径。大量重复 sampler / texture / constant 调用仍是后续独立工作。 + +## 真实入口回归 + +新增测试通过真实 x86 D3D9 / Vulkan 设备以及 Bridge 工厂、CreateDevice、Present、Reset、Shutdown 入口运行。观察实际 MapViewOfFile / UnmapViewOfFile 和模块 PIN 调用;测试专用导入表观察器不进入产品 DLL。 + +| 自动场景 | 检查数 | 失败 | +| --- | ---: | ---: | +| 1 MiB 受压上传 | 1,569 | 0 | +| 100 MiB 普通预算 | 1,520 | 0 | +| 单 mip 对照 | 819 | 0 | +| 不匹配签名 | 38 | 0 | +| 临时 PIN 失败后恢复 | 38 | 0 | +| 签名恢复、成功安装与退出还原 | 143 | 0 | + +覆盖 managed、SYSTEMMEM→DEFAULT、完整 mip-chain、重复写入、partial rect、cube 全 face/mip、managed 资源存活时 Reset、释放和退出。每个二维 mip 检查两个像素采样点,每个 cube face/mip 检查一个采样点;不等于全图逐字节比较。 + +同一最终受压夹具运行原 backend:1,569 检查中仅六项映射次数断言失败;二维源每次上传 map/unmap 各 11 次,cube 各 60 次。候选均为各 1 次,像素采样通过。 + +还保留了两个预期失败的变异验证: + +- 全局延迟所有映射回收的早期候选违反冷映射回收顺序,最终夹具能够检出。 +- 永久记住失败而不再检查签名的早期候选无法从签名恢复,最终夹具能够检出。 + +回收顺序测试证明无关视图在后续 mip 复制前已回收,未测量整个 32 位进程的虚拟地址峰值。补丁夹具使用惰性假 ASI 提供输入字节,不执行假的替换调用,不加载游戏 MOD。 + +## 重复 A/B 与 A/A + +基线固定为开工前安装版本:backend `943827FE52992B02973BDECFCF51E2AB168D06176D4D0F73217F5305D143A50D`,Bridge `1484A9869FC85347950E7974EDF8B806D74A7886470A89E95156BCEF4C5EAEE8`。 + +每种上传场景分别计量 managed / SYSTEMMEM,64 次预热后计时 512 次上传。每个场景 6 组 A/B、3 组 A/A,AB 和 BA 交错。最终实现有两轮周期计量;第二轮使用完整流水线重新构建出的同一份交付二进制。计时主线程在设备/工作线程预热后固定到同一个允许的逻辑 CPU,退出计时后恢复;真实行为回归和游戏没有设置该限制。 + +以下是配对变化的中位数,负数代表减少。CPU 周期不换算成毫秒或游戏 FPS。 + +| 场景 | 首轮 CPU 周期 | 最终产物 CPU 周期 | 首轮墙钟 | 最终产物墙钟 | +| --- | ---: | ---: | ---: | ---: | +| 受压 managed mip-chain | -7.27% | -6.99% | -8.15% | -6.82% | +| 受压 SYSTEMMEM mip-chain | -7.58% | -7.38% | -8.04% | -8.70% | +| 普通 managed mip-chain | -4.14% | +2.24% | +0.58% | -0.53% | +| 普通 SYSTEMMEM mip-chain | -0.56% | -0.53% | +1.46% | -0.58% | +| 受压 managed 单 mip | +0.43% | +3.03% | +0.47% | +5.63% | +| 受压 SYSTEMMEM 单 mip | +0.49% | +1.59% | +0.56% | +1.78% | + +两轮受压 mip-chain 的 CPU 周期均为 6/6 配对减少;512 次上传映射数稳定从 5,632 降到 512。普通预算均为零额外映射;单 mip 均为 512 次。普通和单 mip 不计作收益;最终单 mip 墙钟有回退,应保留为验收限制。最终单 mip CPU 周期 A/A 绝对变化中位数为 managed 3.62%、SYSTEMMEM 2.28%,没有足够证据把其墙钟变化归因到新增 CPU 工作。 + +失败重试使用实际 Bridge 加固定 backend、同一个假模块输入,预热 16 次后计量 2,000 次 Present: + +| 计量轮次 | CPU 周期配对中位数 | CPU 减少组数 | CPU A/A 绝对变化中位数 | 墙钟配对中位数 | 墙钟 A/A 绝对变化中位数 | +| --- | ---: | ---: | ---: | ---: | ---: | +| 首轮固定逻辑 CPU | -37.77% | 6/6 | 12.68% | -7.73% | 14.22% | +| 最终产物固定逻辑 CPU | -27.56% | 6/6 | 4.26% | +42.53% | 9.99% | +| 补充等待统计、固定逻辑 CPU | -34.96% | 6/6 | 2.47% | +4.52% | 9.57% | +| 补充等待统计、正常线程调度 | -40.51% | 6/6 | 4.18% | -1.44% | 6.59% | + +稳定签名拒绝从夹具整个生命周期的 2,024 次 PIN / 拒绝日志降为 1 次。行为收益和 CPU 周期收益可重复;墙钟结果方向不稳定,尤其最终产物一轮的 +42.53% 不能丢弃,因此不确认整体 Present 吞吐收益。 + +补充读取现有线程统计接口,正常调度下基线 / 候选的 CS 空等中位数约 383 / 410 ms,墙钟中位数约 469 / 474 ms;已完成 CS 同步等待和资源 GPU 同步等待均为零。这说明该 32×32 空呈现夹具并非纯 CPU 工作基准,CS 多数时间空等。没有进一步确定异常的具体呈现等待点,也不能以这些计数断言游戏 GPU 不忙。GPU idle 是现有估计值,不是 timestamp-query GPU 耗时。 + +GetThreadTimes 的短窗口值受 15.625 ms 等量化影响,保留原始数据但不用其百分比接受收益。未引入产品线程亲和性、睡眠或人为节流来改善基准数字。 + +## 完整验证与限制 + +最终命令: + +```powershell +pwsh -NoProfile -File tools/test.ps1 -BridgeReference 'D:/GTA San Andreas/Temp/SA-RenderStack-backups/pre-alpha.1-20260829-230407-3d998b6e/d3d9.dll' +``` + +最终结果 **82 passed / 0 failed / 2 skipped**。两个跳过均为可选外部审计:未提供 external DXVK source,以及 external Bridge/config。新增四项 native gate、导出验证、依赖来源验证均通过。 + +首次完整运行是 81 passed / 1 failed / 2 skipped:thread-journal-no-compat 在未改动的 Vulkan 头文件 vkCmdEndQuery 声明处出现编译错误。未修改头文件、编译参数或弱化检查;同命令单独重跑 445 checks / 0 failures,完整重跑也通过。首次故障未能复现,根因未确定,原始日志和首次失败 JSON 均保留。 + +最终只读审查提出的跨平台条件编译、无关 LRU 及时回收、活跃签名恢复,以及基线重映射地址观察问题均已处理。实际构建和运行验证限 Windows x86;不声称已做其他平台构建。 + +两个产品 DLL 的静态导入 DLL 集合相较基线一致,没有新增运行时 DLL 依赖。此项不表示无需现有 Vulkan 驱动,也不是对全部既有功能依赖的重新认证。 + +没有进行同存档首次出门或室外稳态游戏采集,不能据此称 107 ms 尖峰已修复、提高了多少 GTA FPS,或允许忽略上述控制场景与呈现吞吐限制。 + +## 产物与追溯 + +所有证据位于 `out/perf/20261108-upload-state`: + +- `source-before/`、`worktree-before.patch`:保留编辑前已有的脏工作区。 +- `red-final-baseline/`、`green-final-mapping/`、`green-final-retry/`、`native*`、`retry/`:原始行为输出。 +- `review-mutation-*`:早期错误候选的预期失败。 +- `cycles-*-ab.csv`、`validated-*-ab.csv` 及同名前缀目录:两轮最终实现 A/B 原始输入输出。 +- `cycles-benchmark-summary.json`、`validated-benchmark-summary.json`:配对与 A/A 统计。 +- `waits-pinned-*`、`waits-normal-*`、`retry-waits-summary.json`、`retry-wait-profile.cpp`:补充等待统计,只用于诊断。 +- `superseded-v1/`、最早 `upload-ab*` / `repeat-upload-ab*`:已替代实现的数据,不作为最终候选收益。 +- `pipeline-results.json`:首次失败;`pipeline-results-repeat.json`:最终通过;具体完整日志路径包含于 JSON。 +- `package-manifest.json`、`import-dependencies.json`、`install-before.json`、`install-after.json`:指纹、静态 DLL 依赖与安装目录保护校验。 +- `candidate-package/`:两个最终 DLL 和匹配的 RSYM;仅供候选审查,未部署,整体性能验收未闭合。 + +最终候选 Bridge SHA-256 为 `A2407964045254C9D010E496D5A7BAB8D3AA2925960A48CB99F01BA603853391`,backend 为 `ECB34529DA46E6671D628D8BC56366017D17A625EDDB8B44F8226085252DA574`。RSYM 内的 binary SHA-256 已逐一匹配。 + +游戏目录两份 DLL、SA.RenderStack.ini 与 dxvk.conf 的 SHA-256 均与开工前一致。 diff --git a/docs/superpowers/plans/2026-10-21-state-correctness.md b/docs/superpowers/plans/2026-10-21-state-correctness.md new file mode 100644 index 0000000..ee58c64 --- /dev/null +++ b/docs/superpowers/plans/2026-10-21-state-correctness.md @@ -0,0 +1,64 @@ +# State Correctness Implementation Plan + +> **For agentic workers:** Use superpowers:executing-plans with test-driven-development. Steps use checkbox tracking; preserve all existing user changes. + +**Goal:** Repair the first state-correctness batch and prove it with real behavior before admitting the next batch. + +**Architecture:** Keep state authority in DXVK. Capture supported implicit state mutations before changing them, maintain state serials, and repair the ownership/write-order contracts of the SDK wrappers. Tests load an explicitly named test backend and use always-on checks in Release. + +**Tech Stack:** Windows x86, C++23, MSVC, LLVM-MinGW, Meson, CMake/CTest, PowerShell. + +**Spec:** User instruction of 2026-10-21: fix the first state-correctness batch, add real behavioral regressions, verify before continuing to the next batch, and do not replace game-directory DLLs. The preceding audit defines this batch as journal bool/UP/light-serial correctness, API3 write order, API7 ownership/cleanup, and explicit batch failure semantics. + +## Global Constraints + +- Never replace, copy over, or deploy any game-directory DLL or active configuration. +- Preserve the existing ProperShadersRenderStateGuard.cpp/.h changes byte for byte during this batch. +- No commits, pushes, merges, release packages, or runtime profile tuning. +- Keep the public backend API7 ABI unchanged. +- Use real D3D9/Vulkan devices for journal, resource binding, Reset, and batch behavior; mocks are only supplementary ownership/failure controls. +- Every behavior fix needs an observed failing regression before its implementation. +- A batch passes only after focused behavior tests, applicable full project tests, independent review, and protected-file hash checks. + +## Review Focus + +- Bool bitfield updates must capture masked old values without affecting uncaptured registers. +- UP draws must retain their ordinary D3D9 null-binding side effects when capture is disabled and restore prior resources when capture is enabled. +- Journal restore must invalidate fast-skip even when an undefined light disappears. +- SDK wrappers must not duplicate COM ownership or leave an active journal after same-thread destruction/move assignment. +- Overlapping constant updates must preserve the last caller write, including identical starts and reverse-address order. + +## Task 1: SDK state builders and selective journal + +**Files:** api-projects/api3-state-batch and api7-selective-journal headers/sources/tests; API aggregate test support. + +- [x] Replace disabled assert-only checks with always-on behavioral assertions and correct the r255 boundary. +- [x] Write API3 reversed/identical-start overlap, adjacency, invalid-input-preserves-state and reuse tests; observe RED. +- [x] Implement last-write-wins range construction with strong failure safety; observe GREEN. +- [x] Write API7 unique ownership, move/destruction, same-thread scope cleanup and real-device restore/Reset tests; observe RED. +- [x] Implement move-only ownership and deterministic same-thread cleanup without changing the backend ABI; observe GREEN. + +## Task 2: DXVK supported implicit mutations + +**Files:** backend/dxvk/src/d3d9/d3d9_device.cpp and associated journal/stateblock internals only as needed; a focused runtime regression executable in the public SDK CMake aggregate. + +- [x] Add a real-device regression executable covering ordinary stateblock bool apply within full/selective journals, UP stream/index restoration, capture-disabled behavior, undefined-light fast-skip, and RT viewport/scissor effects. +- [x] Run against the unchanged backend and record expected failures. +- [x] Add capture hooks and serial notification before the actual mutations; rebuild the test backend and run both reference and optimized profiles. +- [x] Pin Reset discard, repeated/nested capture, and valid-then-late-failing batch recovery semantics with real device assertions. + +## Task 3: Verification wiring and batch gate + +**Files:** tools/build.ps1, tools/test.ps1 and focused orchestration tests as needed; API aggregate CMake and runtime tests. + +- [x] Register new runtime checks and the API Release behavior suite in normal build/test orchestration, preserving explicit GPU skip policy. +- [x] Run new test executable, seven API unit tests and real API7 integration cases on the newly built backend. +- [x] Run the project's complete tools/test.ps1 command and report every failure/skip. +- [x] Request a fresh-context review of the first-batch patch and resolve important findings with RED/GREEN tests. +- [x] Verify protected-file hashes and diff hygiene; only then admit the second batch. + +## Execution Rulings + +- Use a dedicated codex branch in the existing source checkout and separate build/test outputs. This preserves the pre-existing dirty guard changes and avoids an unnecessary second source import; all runtime installation files are protected by recorded SHA-256 hashes. +- User has already authorized implementation and sequential verification, so proceed without another plan-approval pause. +- The first-batch gate is mandatory; no second-batch production changes before it passes. diff --git a/docs/superpowers/plans/2026-10-22-bridge-lifecycle.md b/docs/superpowers/plans/2026-10-22-bridge-lifecycle.md new file mode 100644 index 0000000..a96449b --- /dev/null +++ b/docs/superpowers/plans/2026-10-22-bridge-lifecycle.md @@ -0,0 +1,55 @@ +# Bridge Lifecycle Implementation Plan + +> **For agentic workers:** Use systematic-debugging and test-driven-development. Parent integrates lifecycle; independent IAT/config tasks use dispatching-parallel-agents, then one fresh whole-patch review. + +**Goal:** Complete the audited second batch with behavior regressions before changing installed runtime files. + +**Architecture:** Keep raw DXVK devices unwrapped through a private COM lifecycle observer. Bridge receives safe owner-thread boundaries, releases borrowed host bindings before final device release, and exposes explicit shutdown on its factory through private COM. Prepare and publish patches at these boundaries instead of suspending threads. + +**Tech Stack:** Windows x86, MSVC/LLVM-MinGW, CMake/CTest, D3D9/Vulkan, PowerShell. + +**Spec:** User request 2026-10-22 and original audit second batch: IAT publication, patch safety/rollback, unwrapped lifetime/reclaim, stoppable workers/shutdown, configuration ownership. First-batch evidence remains a prerequisite and regression gate. + +## Global Constraints + +- Do not replace installed game DLLs or current configs; record hashes before/after. +- Preserve first-batch and original guard changes. No commits, pushes or deployment. +- Keep existing API1-7 ABI and exact DLL export set unchanged; new lifecycle COM interfaces are private integration contracts. +- Each fix needs expected RED behavior before GREEN. Real devices/process calls are required; fault injection may supplement otherwise inaccessible OS failure paths. +- No next-batch production work before focused tests, full tools/test.ps1, independent review and hash checks pass. + +## Review Focus + +- A caller entering immediately after IAT CAS always has a valid original; failed restoration preserves retryable ownership. +- Device/compat references do not keep an unwrapped device alive; a second device can take over after final release. +- Failed Reset leaves passes suspended; successful Reset resumes them, with paired plugin events. +- Shutdown rejects live devices, joins workers outside loader lock, disables callbacks and reports failed patch restoration. +- Disabled process policy preserves actual affinity/priority; DirectConstants production quarantine cannot be bypassed by INI precedence. + +## Task 1: IAT publication and rollback + +- [x] Add real allocated-IAT call/concurrency and protection-failure regressions; observe RED. +- [x] Publish callable original before slot CAS, serialize ownership, retain original and retryable failed restoration state; observe GREEN. + +## Task 2: Device ownership, safe points and shutdown + +**Files:** backend d3d9_device.cpp/.h and new private lifecycle header; BridgeD3D9.cpp; guard .cpp/.h; new runtime regression; CMake/test orchestration. + +- [x] Add private COM contract and real-device observer/Bridge lifecycle tests; run against first-batch DLLs and record RED. +- [x] Implement observer notifications for Present, Reset/ResetEx and final public Release, with serialized unregister. +- [x] Attach borrowed unwrapped hosts, pair plugin callbacks, release claims and allow subsequent devices; fallback to existing wrapper for backends lacking the observer. +- [x] Move runtime initialization out of DllMain; pin initialized Bridge and add explicit shutdown through private factory COM, refusing live devices. +- [x] Replace guard watcher and suspended-thread patch installation with owner-thread create/Present safe points. Record original/installed bytes and restore conditionally; keep failure state. +- [x] Make support/affinity/diagnostic workers stoppable and join before plugin shutdown; run real repeated create/release/reset/shutdown and executable-patch tests. + +## Task 3: Configuration ownership + +- [x] Add actual factory-loading child-process affinity/priority regressions and effective config tests; observe RED. +- [x] Remove backend-wide process policy overrides, enforce production DirectConstants gate and update repository config documentation; observe GREEN. + +## Task 4: Batch gate + +- [x] Integrate focused tests into normal orchestration, keeping explicit hosted GPU skips. +- [x] Rebuild All and run full tools/test.ps1 with the verified archived BridgeReference; report all failures/skips. +- [x] Fresh-context review; address blocking findings and rerun relevant checks. +- [x] Verify installed file hashes and diff hygiene; save evidence/report before ending this batch. diff --git a/docs/superpowers/plans/2026-10-22-constant-control-layout.md b/docs/superpowers/plans/2026-10-22-constant-control-layout.md new file mode 100644 index 0000000..0158585 --- /dev/null +++ b/docs/superpowers/plans/2026-10-22-constant-control-layout.md @@ -0,0 +1,53 @@ +# Constant control layout implementation plan + +> **For agentic workers:** Use superpowers:executing-plans inline. No commit or push is authorized. + +**Goal:** Repair the three confirmed constant control regressions without changing rendering, public interfaces, or state semantics. + +**Architecture:** Controlled identical-machine-code relocation on one real device establishes instruction placement as a cause. Isolated loop results do not establish a single microarchitectural mechanism. Keep constant algorithms intact; constrain the measured early comparison and VS-int update path and reject any candidate that moves regressions elsewhere. + +**Tech Stack:** x86 LLVM-MinGW 22.1.7, MSVC real-device probes, D3D9/Vulkan, PowerShell paired measurements. + +**Spec:** User request in this chat, plus docs/development/performance-validation.md. + +## Global constraints + +- Preserve prior dirty changes, image quality, COM/SDK interfaces and runtime configuration. +- Root cause before tuning. No raw assembly patches or machine-wide scheduling changes. +- Real public COM behavior, repeated paired A/B and full validation must pass before installation. +- User authorized later backup, replacement and testing; announce that step and record backup hashes. + +## Review focus + +- Dedup off must retain short and long unchanged performance as well as changed writes. +- Every 32-bit lane, special bit pattern, unaligned input and bank boundary must remain bitwise correct. +- Journal and state-block recording must preserve their existing dedup-dependent behavior. +- Validate actual linked code; a source attribute alone is not performance evidence. +- Separate synthetic setter improvement from an unmeasured game FPS claim. + +## Task 1: Diagnose and constrain layout + +- [x] Preserve source and installed file snapshots in out/perf/20261022-controls-rootcause. +- [x] Reproduce the layout sensitivity with identical full functions on the same real device and distinguish isolated-loop evidence. +- [x] Add linked-code guard and observe baseline red; add real COM all-lane/boundary regression and run on baseline. +- [x] Apply the smallest layout-only candidate in d3d9_state.h. If the VS-int while alignment hint is lost, inspect generated IR before choosing another formulation. +- [x] Check linked layout, real COM and pixel behavior, then screen all 78 cases on/off. +- [x] Reject every candidate with repeatable material control regression; retain full rejected evidence. + +## Task 2: Verify and deliver + +- [x] Run repeated 12-process x 4-round paired A/B, on/off all cases, plus CPU0 control diagnostics and A/A calibration. +- [x] Run tools/test.ps1 with the archived Bridge reference and inspect every gate. +- [x] Obtain an independent read-only review; address actionable issues and reverify impacted checks. +- [ ] Publish report, exact DLL/probe hashes and performance limits. Only then back up and replace the active validated DLL and run feasible game/runtime checks under user authorization. + +## Investigation rulings + +- Branch-weight hints were rejected: the first improved the targets but slowed changed VS-int writes; two others did not retain the needed update-loop alignment. +- Moving the first store outside the scan did not preserve the metadata and is reverted. +- Ruling: allow an assembler alignment directive in the source, guarded to Clang i386 device-state VS int only. This is a toolchain layout workaround, not a binary/runtime patch; generated NOPs and implicit compiler asm constraints must be reviewed and measured. It replaces reliance on discarded while metadata, preserves scan/copy code, and excludes stateblock recording state. +- The preheader 64-byte candidate passed real behavior and improved the three controls, but its 4-register all-change screen warrants reducing preheader padding before formal repeats. Retain all diagnostic and rejected artifacts. + +- Ruling: reject the helper-preheader candidate after formal +4.681% VS-int 4/all regression; do not hide it behind unrelated AA noise. Move the directive to the device caller before pointer setup. This removes the added EDX spill and makes all six normalized setter instruction streams/CFG equal to baseline. Remove the helper directive and type-trait dependency. +- Final caller pipeline: 54 passed, 0 failed, 2 external audit skips. Normal 12x4 all-case A/B, historical CPU0 controls, full CPU0 A/B and AA are recorded. The previous short-write residual no longer repeats; an unstable tail point estimate is explicitly reported, not rounded to zero. +- Game backup: 10 verified files. Installed DLLs remain unchanged while the elevated Fastman startup dialog requires manual user handling. No claim of game FPS or transition-bottleneck remediation. diff --git a/docs/superpowers/plans/2026-10-22-fading-entry.md b/docs/superpowers/plans/2026-10-22-fading-entry.md new file mode 100644 index 0000000..f681e6c --- /dev/null +++ b/docs/superpowers/plans/2026-10-22-fading-entry.md @@ -0,0 +1,28 @@ +# Repair the ProperShaders Fading entry stack + +Scope: restore successful save loading without changing image quality, configuration, rendering algorithms or public interfaces. Existing unrelated working-tree changes must be preserved. No commit or push. + +## Evidence and design + +The supported local ProperShaders function begins at RVA 0x25590 with push ecx, then push imm32. Its epilogue is pop ecx; ret. The old hook installed at 0x25591 and copied only the latter instruction, so a normal C++ guard calling its gateway consumed the wrong return stack. Both original and candidate Bridge logs show ECX equal to the guard's gateway return address. The user-supplied PID 58628 dump confirms the live bytes 51 E9 and the five-byte gateway. + +Move only this hook to the complete entry. Validate 51 68 and the rebased string address, steal six bytes, and resume at 0x25596. Leave state synchronization intact. ECX is volatile at the external void cdecl guard boundary; the original/gateway pair independently preserves its own saved ECX stack slot. + +## Tasks + +- [x] Preserve original/candidate logs, DLL fingerprints, rollback record and user dump provenance. +- [x] Exercise the production installer, guard, gateway and shutdown in an isolated x86 executable with the real 41-byte function shape. +- [x] Observe executable failure against the old production code before implementing the fix. +- [x] Apply the full-entry/six-byte fix and verify normal returns, stack/nonvolatile registers, call/phase behavior, signatures and restoration. +- [x] Re-run the final fixture against saved old production code, with only include paths rewritten; keep its failing result distinct from fixture setup errors. +- [x] Obtain independent read-only review; add an exact hook target assertion and document test coverage limits. +- [x] Run the full pipeline: 54 passed, 0 failed, two external audit skips. After the test-only assertion, rerun the focused test and all 12 CTest tests. +- [x] Preserve the validated DLLs, symbols and hashes. Back up and replace only the active Bridge/backend after the game exits. +- [x] Verify the same save finishes loading and the player can move; keep startup acceptance separate from FPS improvement. +- [ ] Resume Grove Street transition profiling after successful load verification. + +## Verification boundaries + +The fixture substitutes only the two external RenderWare API addresses. It verifies patch/control-flow and stack safety, not actual RW-to-D3D alpha values, module discovery or pinning. Existing lifecycle tests disabled this guard and did not cover the failed entry stack. + +The rebuilt backend has a different file build identifier, but its .text SHA256 is unchanged from the repeatedly benchmarked constant-control candidate. The Fading fix is a crash correction, and a crashing old path is not a valid FPS A/B baseline. diff --git a/docs/superpowers/plans/2026-10-27-statering-cache.md b/docs/superpowers/plans/2026-10-27-statering-cache.md new file mode 100644 index 0000000..d1c879c --- /dev/null +++ b/docs/superpowers/plans/2026-10-27-statering-cache.md @@ -0,0 +1,62 @@ +# StateRing and device API cache Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox syntax for tracking. + +**Goal:** Remove idle full snapshots and redundant interface validation while preserving rendering and diagnostic availability. + +**Architecture:** Existing StateRing owns bounded manual capture admission; guard owns per-thread method caches with explicit lifecycle invalidation. Bridge forwards device boundaries. No backend/API layout changes. + +**Tech Stack:** x86 C++/Win32, existing MSVC/CMake, real D3D9/Vulkan fixtures and PowerShell tooling. + +**Spec:** docs/superpowers/specs/2026-10-27-statering-cache-design.md + +## Global Constraints + +- Keep the existing dirty feature checkout and all installed files unchanged. +- No deployment, game launch, commit/push, streaming or picture-quality edits. +- Preserve Fading correction and public API1-7/exports; F7/F8 unchanged. +- Retain cold validation, SEH and COM lifetime handling; cache pointers only. +- Timed A/B runs serially after builds/GPU tests stop. + +## Review Focus + +- Idle snapshots must stop without suppressing alpha/stencil correction. +- Vtable replacement/in-place changes and invalid getters must not use stale pointers. +- Reset/final release must invalidate raw and wrapped paths, without holding refs. +- Late records, shutdown and held keys must not reopen an expired manual capture. +- Benchmark reports must distinguish native fixture timings from game FPS. + +### Task 1: Preserve baseline and add real behavior RED + +**Files:** out/perf/20261027-statering-cache/source-before; tests/statering-runtime-tests.cpp; tests/statering-runtime-test.ps1; native test project. + +- [x] Preserve status/diff, old sources/DLL, installed identities. +- [x] Add actual native device forwarding-counter tests for idle snapshots, F10 window, warm queries, state/ref correctness and mutation/reset; observe RED. + +### Task 2: Implement bounded capture and method caches + +**Files:** ProperShadersStateRing.h/.cpp, ProperShadersRenderStateGuard.h/.cpp, BridgeD3D9.cpp. + +**Interfaces:** IsProperShadersStateTraceCaptureActive controls guard admission; InvalidateProperShadersDeviceApiCache advances pointer-cache generation. Existing key polling/dump and state snapshot APIs remain. + +- [x] Implement capture admission/expiry, late-record exclusion and lifecycle cleanup. +- [x] Implement pointer/vtable caches and wire raw/wrapped creation/reset/release. +- [x] Pass native/system RED cases and existing Fading/state/pixel regressions. + +### Task 3: Repeated A/B and required wiring + +**Files:** tests/statering-runtime-benchmark.ps1; tools/test.ps1; configuration comments and docs/unified-measurement.md. + +- [x] Compare saved/current source with identical actual device fixture, alternating A/B, 12 processes and 4 rounds; include idle and detailed capture. +- [x] Wire required native/system regression gates and document F10 behavior. +- [x] Keep candidates separate from the previous deployment candidate. + +### Task 4: Final verification and review + +- [x] Build All, run tools/test.ps1 with archived Bridge reference; inspect counts/logs. +- [x] One fresh scoped whole-change review; fix actual findings and verify affected work. +- [x] Check installed identities, retain artifacts, report measurements/limits and paths. + +## Execution rulings + +Existing feature checkout is retained because it contains the user's validated uncommitted changes. User's direct request and prior delegation resolve method choices; no additional approval or optional integration/cleanup step is needed. Ledger: out/perf/20261027-statering-cache/progress.md. diff --git a/docs/superpowers/plans/2026-10-27-texture-binding-ownership.md b/docs/superpowers/plans/2026-10-27-texture-binding-ownership.md new file mode 100644 index 0000000..f588afb --- /dev/null +++ b/docs/superpowers/plans/2026-10-27-texture-binding-ownership.md @@ -0,0 +1,58 @@ +# Texture Binding Ownership Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Remove unnecessary ownership changes before texture-cache decisions, retaining a candidate only after behavioral and repeated performance validation. + +**Architecture:** Borrow the existing sample-view Rc during the cache decision in BindTexture and BindTextureBatch; take ownership only for a real CS command. Preserve cache ownership and all invalidation logic. Keep changes in the existing working tree, which contains the earlier required state-correctness fixes; do not reset or commit those changes. + +**Tech Stack:** C++ / D3D9 / Vulkan, x86 LLVM-MinGW Release, MSVC native runtime probes, Python linked-code inspection, PowerShell orchestration. + +**Spec:** `out/reports/nvidia-dll-assessment-20261027.md`, approved by the user on 2026-10-27: "按短设计,先优化纹理绑定(推荐)". + +## Global Constraints + +- Preserve image quality, public ABI and queued-resource ownership. +- Do not modify the game installation, launch GTA, tune driver settings, change streaming budgets, commit or push. +- Limit production changes to BindTexture and BindTextureBatch in d3d9_device.cpp. +- Keep baseline/candidate DLLs and benchmark evidence under out; serialize timed GPU benchmarks and compilation. +- Use the existing inline execution workflow; no further approval is needed for the already approved implementation and validation steps. + +## Review Focus + +- Cache-hit and cache-miss paths must preserve actual sampled pixels. +- Single-slot and two-slot deferred commands must own views after application textures are released. +- sRGB view changes must not reuse stale linear views. +- Stateblock/journal restoration and Reset must preserve required invalidation. +- Constant controls and miss-heavy draw workloads must not gain a reproducible regression through changed x86 code layout. + +## Task 1: Texture ownership timing and acceptance + +**Files:** +- Modify: `backend/dxvk/src/d3d9/d3d9_device.cpp`, BindTexture and BindTextureBatch only. +- Modify: `src/bridge/legacy/api-projects/tests/constant_benchmark.cpp`, add isolated `lifetime`/binding measurement modes without changing the existing pair protocol. +- Create: `tests/texture-binding-ownership-test.py`, inspect actual linked x86 cache-hit control flow, not source text. +- Modify: `tools/test.ps1`, required linked-code and real-device lifecycle gates. +- Update: `docs/development/performance-validation.md`, final result and limits. + +**Interfaces:** +- Consume existing `const Rc& D3D9CommonTexture::GetSampleView(bool)`. +- Preserve `PrepareGtaSaTextureBinding(DWORD, const Rc&)` and all public interfaces. +- Add a probe command ` on|off lifetime`, separate from existing `check`/`bench` and `pair` modes. + +**Steps:** + +- [x] Archive same-toolchain baseline and calibrate existing paired A/A. +- [x] Add linked-code regression that fails on the existing cache-hit retain/release sequence; save RED evidence. Its counter anchor is derived from PrepareGtaSaTextureBinding, not a fixed source offset. +- [x] Add real-device regressions for restore-to-same-view, single/multi-slot view switches, linear/sRGB, queued resource release and Reset; run on baseline for characterization. Behavioral tests preserve correctness; the linked-code RED pins the performance defect. +- [x] Borrow sample-view Rc before cache lookup; copy it only into a real command/pending batch. Build candidate and rerun focused checks. +- [x] Run true cache-hit/miss draw measurements and the existing 78-case control matrix in alternating A/B, 12 processes x 4 rounds, on/off. Compare to A/A; do not time during compilation. +- [x] Reject a candidate with no supported benefit or a reproducible control regression, restoring only this batch's production delta. +- [x] Run complete `tools/test.ps1 -BridgeReference ` and inspect every result, including skips. +- [x] Obtain scoped independent read-only code review, address actionable findings and report retained/rejected candidate honestly. + +## Completion evidence + +Use fresh command exit codes, DLL/probe hashes, raw A/B samples, behavior check counts and the complete test report. Verify protected installed-file hashes against the batch input record. Distinguish native microbenchmark benefit from unmeasured GTA FPS. + +**Final outcome:** all runtime optimization variants rejected. Production source restored to the exact input SHA256; retained native423-check lifetime/mixed-hit regressions, texture workload/protocol and six required GPU gates. The unretained optimization's linked-code cost guard is archived with the experimental evidence, not required of stable source. Final restored state:61passed/0failed/2optional external skips. See `out/reports/nvidia-texture-validation-20261027.md`. diff --git a/docs/superpowers/plans/2026-10-27-unified-measurement-hud.md b/docs/superpowers/plans/2026-10-27-unified-measurement-hud.md new file mode 100644 index 0000000..981f913 --- /dev/null +++ b/docs/superpowers/plans/2026-10-27-unified-measurement-hud.md @@ -0,0 +1,90 @@ +# Unified Measurement HUD Implementation Plan + +> **For agentic workers:** Use superpowers:executing-plans for integrated changes; independently scoped symbol and journal work may use superpowers:dispatching-parallel-agents. Steps use checkbox syntax for tracking. + +**Goal:** One F8 press collects baseline, CPU hotspots, and former F7 journal/backend audit information with a visible HUD and precise, qualified attribution. + +**Architecture:** Bridge owns the sequential controller and capture identity. A new private, versioned COM interface transfers bounded HUD snapshots and controls the existing backend state audit without altering public API1-7 or existing private vtables. Symbol lookup and journal aggregation are separate components; expensive work stays outside suspended-thread and per-frame rendering paths. + +**Tech Stack:** C++23/MSVC x86 Bridge, C++17/LLVM-MinGW DXVK, existing Vulkan HUD renderer, Python/PowerShell build and validation tools. + +**Spec:** docs/superpowers/specs/2026-10-27-unified-measurement-hud-design.md (user approved revised design and delegated technical details). + +## Global Constraints + +- Preserve quality, public API1-7 and DLL exports, existing Fading/state fixes, and all pre-existing dirty work. +- No game launch, game-directory deployment, commit, push, driver change, new streaming hook, or budget adjustment. +- F7/F8 are aliases in combined mode; held/repeated keys never restart an active capture. +- Defaults: 5000 ms baseline, configured CPU duration/interval (currently 10000/5 ms), configured attribution duration (currently 3000 ms); text refresh at most 5 Hz. +- Preserve standalone behavior with combined mode disabled. Missing optional backend support produces an explicit unavailable status. +- CPU samples, heuristic callers, inferred symbol boundaries, and inclusive QPC timings must retain their evidence labels. +- All timed A/B processes run serially after builds and other GPU tests stop. + +## Review Focus + +- Two same-basename d3d9 modules or stale symbols must never be merged or silently named from the wrong build. +- Reset/release/shutdown during CPU sampling must terminate the capture without accessing retired device data or reporting success. +- Backend F7 polling must not independently start an audit during baseline/CPU phases in combined mode. +- An attribution transaction crossing the end boundary must finish recording before summary publication, without deadlocking the owner thread. +- HUD disabled/unsupported and missing journal records must remain distinguishable from measured zero cost. + +## Execution rulings + +Work in the existing feature checkout because the approved state includes extensive uncommitted fixes; a fresh checkout at HEAD would omit them. Capture a baseline patch and file identities first. The user's explicit 'implement directly, details are yours' replaces further plan/method confirmation. No optional skill commit or cleanup step overrides the no-commit requirement or prior cleanup-policy rejection. + +### Task 1: Pure controller and measurement algorithms + +**Files:** create `src/bridge/legacy/BridgeMeasurement.h`, `src/bridge/legacy/api-projects/tests/measurement_tests.cpp`; modify API-project CMake configuration. + +**Interfaces:** `BridgeMeasurement::Controller` consumes key edges, monotonic Present observations and sampler completion; produces phase/action snapshots. `FrameStats`, bounded frame/sample records, jitter and captured-frame-chain helpers operate on supplied data without Win32/global game state. + +- [x] Write behavioral tests first: complete ordering, failures/interruption, held/both keys, clock/arithmetic boundaries, failed Presents, explicit missing stages, slow/ordinary intervals, sample capacity/jitter, validated-versus-heuristic frames. +- [x] Run and save RED output; implement the minimal production helpers and run GREEN. +- [x] Confirm bounded storage, no allocating operations in the captured-stack helper, and stable Present-based units. + +### Task 2: Exact-build symbol preparation and resolution + +**Files:** create `src/bridge/legacy/MeasurementSymbols.h/.cpp`, `tools/prepare-measurement-symbols.py`, relevant native/Python tests; build/package wiring owned by the parent. + +**Interfaces:** immutable per-module lookup accepts a real binary path/base and a prepared identity-bound sidecar; result includes module path/base/RVA, symbol/offset, and resolution quality. Runtime interface details documented by implementer before parent integration. + +- [x] RED tests for wrong identities, same basenames/different bases, alias/ambiguous ranges, section boundaries, no symbols, and malformed/truncated input. +- [x] Generate sidecars from actual linker/COFF metadata; preserve explicit size versus inferred-next-symbol distinctions. +- [x] GREEN tests on fixtures and the actual baseline DLLs; no symbol download or invented third-party function names. + +### Task 3: Journal precision and inactive admission cost + +**Files:** modify `EffectInspector.cpp`, `ProperShadersStateJournal.h`; add scoped tests/runner without editing shared build orchestration concurrently. + +**Interfaces:** bounded `JournalAttributionSummary` exposes capture identity, frame/Present counts, technique/pass counts, inclusive transaction and restore timing, and dropped/unavailable indicators. Existing output remains parseable. + +- [x] RED for absent summary/per-frame attribution and inactive lock-free admission; include StopCapture admission ordering. +- [x] Add inactive fast check retaining the locked active recheck, stable identity, bounded frame aggregation and named summary. +- [x] GREEN on real journal transactions and duplicate/changed state; keep transaction span and restore cost separate. + +### Task 4: Private backend audit/HUD and unified Bridge integration + +**Files:** create `backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h`; modify D3D9 device, compat audit, HUD, swapchain; modify BridgeD3D9.cpp and project files; add focused integration source if needed. + +**Interfaces:** new IID `ID3D9BridgeDiagnostics` accepts combined-mode ownership, bounded HUD snapshots, and start/stop/read audit commands. Backend snapshots use copied data and independent synchronization; existing lifecycle IID/vtable is unchanged. + +- [x] Add real-device RED tests for the missing QI interface, audit identity/counters, HUD snapshot validation, raw/wrapped lifecycle and reset/release. +- [x] Implement QI integration and reused backend state audit; suppress standalone F7 polling only while combined ownership is active. +- [x] Add throttled HUD item and test its actual renderer path, preserving normal draw pixels and D3D9 state. +- [x] Integrate the sequential controller at owner-thread Present boundaries, bounded CPU sample collection, jitter, phase/frame correlation, symbol lookup after resume, journal/backend control, and one combined summary identity. +- [x] Preserve old standalone logs and wrapped/raw fallback behavior; add explicit failure/interrupted outcomes. +- [x] Run focused GREEN tests and inspect generated reports/HUD artifacts. + +### Task 5: Full verification and candidate delivery + +**Files:** modify tools/build.ps1, tools/package.ps1, tools/test.ps1, candidate configuration/docs and new measurement regression/benchmark runners. + +- [x] Wire all new required gates and sidecar production without changing installed files. +- [x] Repeated serial A/B: combined disabled versus ready/idle HUD; distinguish active CPU/journal measurement from baseline and report units/observer overhead honestly. +- [x] Run `pwsh -NoProfile -File tools/test.ps1 -BridgeReference 'D:/GTA San Andreas/Temp/SA-RenderStack-backups/pre-alpha.1-20260829-230407-3d998b6e/d3d9.dll'` and required native/Python tests; read every result. +- [x] Obtain a fresh scoped code review, resolve actionable issues, and rerun affected verification plus full pipeline after changes. +- [x] Verify installed file hashes/size/timestamps still match the input record; report actual test counts, benchmark limits, candidate paths, and remaining game visual acceptance. + +## Progress + +Execution ledger: `out/perf/20261027-unified-measurement/progress.md`. diff --git a/docs/superpowers/plans/2026-10-31-binding-dirty-capture.md b/docs/superpowers/plans/2026-10-31-binding-dirty-capture.md new file mode 100644 index 0000000..01accf5 --- /dev/null +++ b/docs/superpowers/plans/2026-10-31-binding-dirty-capture.md @@ -0,0 +1,42 @@ +# Generic binding, dirty propagation and first-write capture + +Execute inline with one fresh final review. User selected these three measured hotspots. No commit, deployment, game launch, quality change or public interface change. + +Baseline: backend D57B5D8BE5144B17CE71493EA8D924C46109827303CD1A79396EBFCF1475F710. Preserve existing dirty checkout; status/diff/source/installed files saved under out/perf/20261031-binding-dirty-capture. + +## Design and acceptance + +- Borrow image-view references until an actual binding command/cache entry needs ownership. Keep command and cache ownership, Reset and resource replacement intact. +- Propagate sampler dirtiness from the state actually consumed by sampler creation. sRGB selects a texture view; only its low bit matters and it affects the sampler only when a border view is consumed. Ignored sampler fields and unused border colors remain stored and captured. Preserve Fetch4 and upload/mipmap/hazard paths. +- Capture only previously untouched constant registers using bounded word scans; preserve first-write values across overlapping writes, holes, stages, types and word boundaries. +- Also read old texture format before releasing the old private reference; replacement can destroy an externally released bound texture. +- Do not add a common-texture shadow cache: current GetCommonTexture is already an inline offset access, not a virtual dispatch. +- Native tests must exercise actual DLL behavior, pixels, overlapping constant writes, resource destruction and Reset, with compatibility enabled and disabled. Repeated matched A/B includes controls and completed-work timings. No game FPS claim from fixtures. +- Check exports/imports, no-MOD isolated execution and installed-file hashes. Run the complete pinned-reference pipeline. + +## Tasks + +- [x] Add characterization and new helper tests; retain baseline results and observed RED. +- [x] Implement bounded first-write scanning and precise binding/dirty changes; run focused native tests. +- [x] Repeat A/B on sealed bytes, investigate any control regression, retain supported changes only. +- [x] Full validation, fresh final review, independent execution, candidate package and report. + +## Ledger + +2026-10-31: User explicitly requested all three selected targets. Routine implementation decisions are authorized. No repeated design approval needed. Candidate only. A/B baseline is the currently deployed validated backend, not the previous batch's invalidated mutant. Existing working tree includes required prior changes and is preserved without a commit. + +Characterization: baseline 1640/0 before pixel extensions; new first-write helper test observed missing-header RED, then GREEN with independent randomized 8192-register oracle. Mixed-device test initially compared unread outputs beyond the existing public getters' hardware clamp: both baseline and candidate failed identically. Ruling: retain getter behavior and compare the readable window; extended writes still exercise capture/restore, while the full-range helper oracle covers exact high-register masks. Do not claim native high-register readback coverage. + +Real-device dirty propagation RED: saved baseline fails exactly the sampler-unit and texture-unit expectations (1848 checks / 2 failures); refined candidate passes 1848/0. Linked scalar and batch cache-hit guards confirm no atomic ownership work; the constant-loop layout guard also passes. + +Pilot rejected as-is: per-register-sized capture paths and dense controls regressed. Added Count==1 shortcut and word-wise captured-mask marking. Refined 12-pair x 5-round native A/B: capture overlap -53.29%, capture repeat -56.19%, dense -4.17%, tail -4.12%, sparse -4.27%; control -0.86% (7/12 faster), empty -6.08% (9/12). Completed timing agrees; no native game FPS conclusion. + +Previous October 27 borrowing experiment was rejected for control regressions. This batch must re-pass the full 78-case on/off control matrix and texture subset, not infer acceptance from fewer atomics. Fresh reviewer found no production defect; identified sRGB-only border-sampler branch coverage gap. Added actual draw + sampler/texture unit assertions without a concurrent border-color change; pending final execution. + +Matched timing complete: 9 draw/setter groups, 22176 timing records including A/A; separate journal group 840 records. Two on-profile texture windows show scalar hits -7.27/-7.89%, double hits -13.13/-12.78%, mixed hits -7.51/-9.39%; corresponding A/A +0.36/-0.01/+0.99%. On/all miss-heavy texture-draw initially +4.32% (1/12 faster), texture subset +0.41%, repeated controls +2.41%, repeated texture subset +1.52%; A/A controls +2.59%. Completed timings also retained. Ruling: the miss/control results do not establish a stable improvement or a repeatable regression beyond fixture variability; retain this combined candidate on the consistent hit/constant improvements, report all positive estimates and do not claim zero regression or game FPS. Full on/off 78-case matrices and texture controls preserved. + +Fresh reviewer confirmed added sRGB-only border draw/counter assertion resolves the Minor gap at source level; no remaining review findings. Await final execution. No code or timed binary changes after sealing A5015E14E46CCCC60D0C31170BF327D0F5813B888FAC7EE5EDFD38E98AE8C275; only regression coverage and gate wiring changed. + +First complete run: 75 passed, 3 failed, 2 optional skips. Two hygiene gates found a manual probe log in the repository root; preserve/move it under this batch's out directory. measurement-backend's old cache-hit fixture deliberately changed only ignored sRGB high bits; the new optimization correctly prevents that dirty path. Preserve failed report/logs. Update the fixture to change/restore consumed ADDRESSU and texture identity before draw, then verify the final high-bit-only change adds no prepare event. New counters 7 sampler calls / 5 texture calls / 2 prepare draws, focused 3430/0. Reviewer confirmed the test-only correction, no open findings. Re-run full pipeline; production code unchanged. + +Final acceptance: complete pinned-reference pipeline exit 0, 78 passed / 0 failed / 2 optional external-source skips. Native on 1867/0, mixed 1883/0, off 446/0; measurement-backend 3430/0. Independent directory with only backend + probe, own performance flags enabled, repeats on/mixed/off with the same counts and no game-directory modules. Exports/import contracts unchanged; final backend 943827FE52992B02973BDECFCF51E2AB168D06176D4D0F73217F5305D143A50D has identical sections to timed A501 except build identity. Candidate packaged with matching symbols and unchanged config. All protected installed hashes/sizes/mtime unchanged. No deployment, game test, commit or push. Report out/reports/20261031-binding-dirty-capture-validation.md. diff --git a/docs/superpowers/plans/2026-10-31-thread-journal.md b/docs/superpowers/plans/2026-10-31-thread-journal.md new file mode 100644 index 0000000..a407b0a --- /dev/null +++ b/docs/superpowers/plans/2026-10-31-thread-journal.md @@ -0,0 +1,41 @@ +# Thread attribution and generic journal implementation plan + +> Execute inline with superpowers:executing-plans; use a fresh final reviewer. The user authorized the instrumentation-first sequence and complete automatic validation. Do not commit or deploy. + +**Goal:** distinguish API-thread CPU work, CS starvation/backlog and synchronization, then reduce measured generic journal overhead. + +**Architecture:** private read-only boundary snapshots, F8 report integration, independent native D3D9 fixtures. Keep public SDK/vtables/exports and quality settings unchanged. + +**Tech Stack:** C++17, DXVK Vulkan/D3D9, Win32 thread CPU queries, PowerShell test orchestration. + +**Spec:** ../specs/2026-10-31-thread-journal-design.md + +## Global constraints +- Preserve all pre-existing dirty work and installed game files; source-before snapshots in out/perf/20261031-thread-journal. +- No additional libraries, services, MOD-name recognition or game-specific addresses in new functionality. +- All performance conclusions identify fixture scope; game FPS remains unmeasured. + +## Review focus +- Ongoing CS idle intervals must be accounted for without waiting for a wake-up. +- Caller thread identity and counter availability must prevent misleading CPU percentages. +- Reset/release/aborted capture must not retain an interface/device self-reference. +- Sparse journal cleanup must preserve COM ownership and normal StateBlock capture semantics. +- Diagnostic support must work with gtaSaCompat disabled and no game/MOD modules loaded. + +## Tasks +- [ ] 1. Save status, source and installed hashes; add actual-device tests that fail for missing private thread query. Build/run RED against baseline and preserve baseline DLL. +- [ ] 2. Implement CS queue-idle snapshots/private query; integrate per-phase snapshots in F8 reports; verify CPU busy/idle, nonblocking reads, sequence progress, waits and unavailable behavior. +- [ ] 3. Run independent sparse/dense state/journal/control workloads and identify cost; add regression for the chosen optimization and retain existing pixel/lifecycle tests. +- [ ] 4. Implement targeted generic improvement, verify behavior then repeat matched A/B including instrumentation overhead and controls. +- [ ] 5. Wire required gates, build all, run full tools/test.ps1, fresh review and resolve material findings; inspect imports/exports/modules, package candidate and verify protected hashes. + +## Ledger +2026-10-31: User-approved research sequence is the selected approach; routine instrumentation mechanics are implementation decisions. Continue in the established dirty checkout because earlier uncommitted validated changes are required; no worktree or commit requested. + +Instrumentation RED: private QI absent on saved backend, real device probe 810 checks/1 expected failure. F8 report RED using saved production runtime and new backend: 67 checks/1 expected missing-thread-row failure. GREEN: current runtime 67/0. Extended native probe 1264/0; gtaSaCompat disabled 235/0, same private activity query and actual pixel readback work without any MOD module. + +Diagnosis: pure sparse/tail/dense journal workloads have zero CS dispatch, zero CS CPU and queue-idle approximately wall time. Actual draw+readback creates CS work and nonzero completed CS/GPU waits. Targeted source changes: captured-set journal cleanup and word-wise constant-run iteration; no MOD-name lookup or game addresses added. + +Fresh reviewer found two evidence issues, no core-code correctness issue: completed timing included printing; external AddRef counts did not test private retained resources. Both addressed: timestamps taken before printing, completion wait HRESULTs/deadline checked; non-null texture/VB/IB destruction sentinels cover restore and Reset/discard. Deliberate texture-release omission mutant fails sentinel test (1264 checks/1 failure), then correct source restored and full build succeeds. CPU phase and wait-counter assertions added. + +First A/B is a pilot only: tiny control windows showed excessive scheduling variance; reviewer also identified completed-time logging contamination. Final protocol uses longer loops, pins only the fixture main thread to an allowed CPU, leaves worker affinity unchanged, and separates logging from all timing windows. Preserve pilot logs; use paired-final for acceptance. diff --git a/docs/superpowers/plans/2026-11-08-upload-state.md b/docs/superpowers/plans/2026-11-08-upload-state.md new file mode 100644 index 0000000..f265d0c --- /dev/null +++ b/docs/superpowers/plans/2026-11-08-upload-state.md @@ -0,0 +1,47 @@ +# Upload source mapping and repeated state work implementation plan + +> Execute this plan inline with superpowers:executing-plans. Use the final fresh review required by that skill. + +**Goal:** Reduce source mapping churn during a texture upload and stop repeated deterministic compatibility failures at render safe points. + +**Architecture:** Keep active source mappings live throughout each texture upload transaction while allowing other LRU views to be reclaimed after each subresource. A rejected pinned module remembers its first mismatching call site. Stable bytes skip full preflight and logging, and restored bytes permit another full installation attempt. Transient installation errors remain retryable. + +**Spec:** The two paths authorized in chat on 2026-11-08 and the local F8 findings in out/perf/20261108-f8-35304-2/findings.md. + +**Constraints:** Preserve texture bytes, upload ordering, dirty rectangles, public interfaces, drawing quality, existing staging copies and mapping-budget enforcement. Keep candidates in out/perf/20261108-upload-state. Preserve pre-existing checkout changes. No commit or deployment is part of this batch. + +## Source mapping task + +- [x] Add a real D3D9/Vulkan fixture that counts the backend's actual Windows map/unmap calls around managed and SYSTEMMEM mip-chain uploads. Verify sampled mip content and repeated write/upload behavior. +- [x] Reproduce multiple mappings of the same source during a pressured mip-chain upload on the baseline. +- [x] Protect source mappings across UpdateTexture and UploadManagedTexture, retain immediate reclamation of unrelated views, and trim the source after the scope ends. +- [x] Verify mapping reuse, final source reclamation, partial updates, sampled pixels for every mip and cube face, release and managed device reset. +- [x] Repeat interleaved baseline/candidate A/B plus baseline A/A for pressured and ordinary controls. Record CPU work reductions and wall-time limitations separately. + +## Repeated state work task + +- [x] Run a fixture through the actual render-safe-point retry policy. An unchanged rejected signature performs one full preflight, transient PIN failures retry, and restored signatures allow a successful installation. +- [x] Reproduce the unbounded rejection attempts on the baseline. +- [x] Apply a module-instance rejected-site state to the failing optimization-patch path. +- [x] Regress shutdown, successful patch installation after signature restoration, signature refusal, device reset and transient PIN failure. +- [x] Measure repeated rejected-module polling with the actual production entry path. Keep this independent from generic state binding experiments. + +## Validation and handoff + +- [x] Run focused native tests and regression mutation checks. +- [x] Run tools/test.ps1 with the audited archived Bridge reference. Inspect every failure and optional skip. +- [x] Obtain final read-only review, address substantive findings, verify installed DLL/config hashes and package candidate evidence. + +## Progress + +- Baseline snapshots and protected-install hashes saved before product edits. +- Implementation choice: first fix directly reproducible source self-eviction across mip levels. Parallel page preparation remains a separate candidate until its integrated lifetime and net benefit are proved. +- Review ruling: protect only active upload sources, including nested sources, while reclaiming unrelated LRU mappings at existing points. The earlier global-deferral candidate failed the new ordering regression and is retained as superseded evidence. +- Review ruling: PIN does not make live bytes immutable. Recheck the rejected six-byte site cheaply and retry when it matches. The earlier permanent-rejection candidate failed the signature-restoration regression. +- The periodic unsupported-CRC path is outside this first retry change because its module lifetime differs. Generic repeated sampler calls remain measured separately from the Present retry fix. +- Pixel checks sample two positions per 2D mip and one position per cube face/mip. The new cold-view test verifies reclamation order and does not establish process-wide VA high water. +- Final native gates: upload pressure 1,569 checks; ordinary 1,520; single mip 819; retry reject/transient/restore 38/38/143; all zero failures. The same final pressure fixture fails the original backend on six map/unmap assertions, with pixel checks passing. +- Full validation: first run 81 passed, 1 compile failure, 2 optional skips. The unchanged thread-journal-no-compat compile could not be reproduced in isolation. A complete rerun passed 82 gates with zero failures and the same two missing external-audit inputs skipped. The first failure remains archived. +- Two final production A/B passes each use six A/B and three A/A pairs per case, with AB and BA order. Pressured upload main-thread cycles fall about 7%; mapping calls fall 5,632 to 512 over 512 uploads. Ordinary and single-mip controls show timing noise, including one single-mip wall-time regression that is not treated as an accepted performance result. +- Retry CPU cycles decrease in all six comparisons in both final passes, but 2,000 empty Present calls do not show stable end-to-end wall-time benefit. One final pass regressed 42.53%; follow-up instrumentation with normal affinity measured -1.44% against 6.59% A/A median variation, while CPU cycles fell 40.51%. CS queue idle dominates this fixture, and existing completed CS/GPU synchronization counters are zero. The specific presentation wait is not established. Do not claim throughput/FPS acceptance. +- Candidate DLLs and matching RSYM fingerprints are under out/perf/20261108-upload-state/candidate-package. No runtime DLL dependency was added. Installed DLL/config hashes are unchanged. Game-route validation remains unperformed and deployment is not part of this batch. diff --git a/docs/superpowers/specs/2026-10-27-statering-cache-design.md b/docs/superpowers/specs/2026-10-27-statering-cache-design.md new file mode 100644 index 0000000..0d850da --- /dev/null +++ b/docs/superpowers/specs/2026-10-27-statering-cache-design.md @@ -0,0 +1,41 @@ +# StateRing idle capture and interface validation optimization + +The user requested optimizing the two measured paths after Grove Street capture +59128/1: always-on full StateRing snapshots and repeated device API validation. +Preserve Fading/RW/D3D9/stencil restoration, picture quality, public API1-7, +F7/F8 collection, and all existing dirty work. Build candidates only; no game +launch, deployment, commits or changes to streaming/ProperShaders quality. + +Use the existing guard/ring files. StateRing Enable arms the existing F10 key; +idle guards perform no full diagnostic snapshots. A key edge opens a bounded +one-second detailed window and exports it at the next owner polling point after +expiry. Holding/repeating the key cannot extend/restart that window. Snapshot +admission is independent of the mandatory Fading state correction. Stop on +shutdown/reconfiguration, and reject records started in a previous window. +This deliberately changes F10 from an immediate historical dump to an on-demand +capture; document that change. F7/F8 timing and output stay unchanged. + +Cache resolved D3D9 method pointers per calling thread, keyed by actual device, +vtable, relevant method pointers and a lifecycle generation. Never cache render +state values. A warm matching resolution avoids repeated VirtualQuery. New +devices/vtables, in-place method changes, reset, final release, configuration, +shutdown and failed guarded accesses require fresh validation. Retain cold-path +readability/executability checks, getter-slot validation and SEH failure paths. +Release-method lookup may cache validated vtable/function identity; each object +still receives its matching Release, with no retained COM references. + +Alternatives: turning off the entire guard risks the known loading/Fading bug; +leaving detailed capture continuous retains measured cost. An on-demand ring and +lifetime-bound pointer cache address the observed paths without changing draws. +The user has delegated implementation details and explicitly selected this work; +continue without another design/permission round. + +Tests must first fail on production behavior: idle full-snapshot calls, unbounded +manual window and repeated warm validation. Exercise actual system D3D9 and +DXVK devices, state restoration/pixels, COM reference balance, vtable mutation, +device replacement/reset and invalid pointers. Forwarding counters may observe +real APIs; do not replace their behavior. Preserve the existing actual x86 +Fading gateway regression. Repeated A/B compares compiled old/current production +guard/ring sources with identical native fixtures; report units and limits. +After focused checks, build All, run the full pipeline, obtain one final scoped +review and verify installed hashes remain unchanged. diff --git a/docs/superpowers/specs/2026-10-27-unified-measurement-hud-design.md b/docs/superpowers/specs/2026-10-27-unified-measurement-hud-design.md new file mode 100644 index 0000000..b706b8a --- /dev/null +++ b/docs/superpowers/specs/2026-10-27-unified-measurement-hud-design.md @@ -0,0 +1,152 @@ +# Unified measurement capture and HUD + +Date: 2026-10-27 (Asia/Shanghai) + +## Intent and agreed approach + +The user requested a visible measurement HUD and a merged F7/F8 workflow, then +selected sequential collection to distinguish diagnostic overhead. Their latest +clarification makes diagnostic precision the priority: one F8 press should +collect the useful information from F7 and F8, and reports must explain concrete +functions and measured work rather than repeatedly stopping at module totals. +Keep picture quality, rendering correctness, and public API1-7/exports unchanged. + +One press of F8 starts a capture; F7 is an alias for the same workflow. Both keys +use edge detection. Holding either key or pressing the other during a capture +must not start a second capture or restart the timer. + +Alternatives considered: simultaneous CPU/state attribution would observe the +same interval but mix their measurement costs. Separate keys would retain the +current fragmented workflow. The selected approach runs the phases in order. + +## Capture behavior + +The proposed defaults are 5 seconds of baseline, the existing CPU-hotspot +duration (currently 10 seconds), then the existing state-attribution duration +(currently 3 seconds). Preserve the configured CPU sampling interval. Start +and transition phases at the owning device's Present boundaries. + +The baseline enables neither CPU suspension sampling nor journal attribution. +It measures the normal configuration with the same HUD enabled. All reported +frame rates use clearly identified successful Present counts and elapsed time; +they are not CPU/GPU execution times. Keep separate summaries for each phase. + +Use both parts of the current F7 behavior: Bridge state/journal attribution and +DXVK's existing D3D9 state audit. Today the backend separately polls F7 and dumps +its own three-second counters; the combined controller must own their timing +and associate both results with the same capture. Do not revive the legacy +effect-patching profiler or enable experimental state-save bypasses. +Retain standalone legacy behavior when the new combined mode is disabled. + +On Reset, final device release, shutdown, or sampler failure, end the current +capture with an explicit interrupted/error result. Preserve completed phases; +never display a failed or interrupted phase as a successful measurement. + +## HUD and data flow + +Reuse the DXVK HUD renderer and current FPS/frame-time display. Add a compact +RenderStack item with ASCII labels supported by the existing font: + +- Capture ID, current phase, elapsed/remaining time and progress. +- Live FPS and average frame interval, plus the maximum observed interval. +- Separate baseline, CPU-sampled and state-attribution summaries. +- CPU sample/missed counts, named function hotspots where verified symbols + exist, and completed journal/API attribution counts. +- Ready, collecting, complete, interrupted, or error status; log location. + +Refresh displayed text at a bounded rate, approximately 5 Hz. Reuse fixed-size +capture statistics; do not read log files or scan virtual address space in the +HUD's per-frame path. Do not add GPU queries or a per-call D3D9 wrapper merely +to display diagnostics. F8 CPU results remain statistical samples, not a +computed percentage of recoverable frame time. + +Bridge publishes a bounded snapshot through an additional private COM interface +with a distinct IID, independent of existing lifecycle and public SDK vtables. +The backend HUD renders a copied snapshot. No new DLL exports or public API1-7 +changes are required. A backend without this optional interface keeps logging +and explicitly reports unavailable HUD integration. + +## Diagnostic precision and measurement algorithm + +Current limitations were checked in source: CPU stacks are formed by scanning +64 stack words for executable-looking values, brief caller strings omit the +module base, only the top 256 IP rows are serialized, and the separate F7 +reports have no common capture identity. These directly limit attribution. + +The proposed revision addresses these gaps without promising unsupported full +unwinding of arbitrary optimized x86 third-party code: + +- Preserve full module identity (path, loaded base, image identity and RVA) for + sampled IPs and candidate callers, so the two d3d9.dll modules cannot be mixed. +- Generate function ranges for the actual Bridge/DXVK builds from existing + linker-map/COFF symbols, with strict binary identity matching. Resolve known + system exports where possible. Label missing, ambiguous, or mismatched + symbols explicitly; do not guess names from another build. +- Keep executable-address stack scanning explicitly marked heuristic. Where a + bounded captured x86 frame chain can be validated, report that separately; + count unresolved/truncated chains rather than manufacturing callers. +- Correlate samples with monotonic timestamps and observed Present intervals. + Summarize slow intervals separately from ordinary ones, with sample coverage + and truncation counters. A sampled function percentage is never rendered as + exact milliseconds saved or an exact duration. +- Use bounded sampling jitter around the configured average interval to reduce + fixed-interval phase bias. Copy only bounded raw state while suspended, then + resume before allocation, symbol lookup, aggregation, or file output. Record + sampling failures and directly measured suspension time. Suspension time is + one component of observer overhead, not a complete estimate of its FPS cost. +- Collect the existing backend state-audit counters during the attribution + phase: calls/frame, redundant calls/units, measured total/maximum elapsed + time, binding-cache hits/invalidations, and prepare-draw categories. Expose + summaries through the private integration rather than scraping logs. +- Associate Bridge journal transactions with technique/pass and present frame; + display transaction-span and restore timings with explicit inclusive labels. + Do not sum nested timings into a fictitious total frame cost. Show missing or + unavailable attribution instead of interpreting no records as zero cost. +- Reduce demonstrated measurement-path overhead: the current journal admission + function takes an exclusive lock even when attribution is disabled. Add a + cheap inactive check while retaining the locked admission check that pairs + transactions with StopCapture. Avoid repeated hot-path symbol resolution or + string formatting; verify active counts and shutdown behavior are preserved. + +The HUD presents the top verified results and measurement quality. Detailed +per-function/per-frame and technique/API tables remain in the combined report. +No new game streaming hooks are introduced; a resource-loading bottleneck is +not considered proved merely because one CPU sample lies in a loading module. + +## Reporting and verification + +Link the phase records with one session/capture identity, preserving the raw +CPU and state-attribution outputs and recording phase durations and failure +states. Produce a combined summary with observer-stage labels. Existing tools +must retain valid standalone log behavior. + +Before implementation, add behavior tests that fail for the missing workflow: +phase ordering, key edges/repeated keys, FPS/time arithmetic, disabled/missing +attribution, sampler failure, Reset, release, and shutdown. Add fixtures with two +same-basename modules, wrong symbol identities, ambiguous function ranges, +heuristic-versus-validated callers, bounded buffers, sampling jitter, and known +slow/ordinary Present intervals. Exercise actual journal/state-audit counters +with real API calls and true duplicate/changed state, and verify inclusive +timings are not double-counted. Exercise the private +snapshot interface and both wrapped/raw device lifecycle paths on real D3D9/ +Vulkan devices. Verify HUD rendering through the actual render path where the +test harness permits, and report any remaining visual acceptance separately. + +Run repeated paired measurement-overhead benchmarks with HUD/capture idle and +active states kept distinct. Run the full repository pipeline and existing +state, pixel, lifetime, layout, and protocol checks. Preserve all pre-existing +dirty work and the installed Fading fix. No commit, game launch, or deployment +is part of this implementation batch; validated candidates remain in out/. + +## Scope + +This batch improves measurement and its feedback. It does not change streaming +budgets, StateRing policy, game threading, texture-binding optimization, or +rendering quality. The original streaming configuration restored for comparison +remains installed until separately changed. + +## Design review + +A fresh read-only builder check of this revised design reported "nothing +important left". This checks that the design can be planned; it is not runtime +verification or approval of an implementation. diff --git a/docs/superpowers/specs/2026-10-31-thread-journal-design.md b/docs/superpowers/specs/2026-10-31-thread-journal-design.md new file mode 100644 index 0000000..325e058 --- /dev/null +++ b/docs/superpowers/specs/2026-10-31-thread-journal-design.md @@ -0,0 +1,11 @@ +# Thread attribution and generic journal reduction + +User-selected sequence: instrument main/API thread and CS activity, diagnose with independent real D3D9 workloads, then optimize common StateBlock/Journal paths. Preserve image quality and public API1-7/export contracts. Candidate only; no game deployment or game automation. + +Use a new private copied-data query (separate IID, no change to existing diagnostics ABI). Sample at F8 phase boundaries, not per API call. Read Windows thread CPU time, CS queue-idle wall time including an unfinished idle interval, ordered dispatch/completion sequence numbers, and existing device CS/GPU wait/chunk/submission counters. Report CPU execution separately from elapsed/non-CPU time. CS non-idle wall time includes scheduling and internal waits; GPU idle is an estimate. Counter deltas may include completed waits that cross a boundary; do not claim exhaustive mutually exclusive attribution. Sampling must not flush or wait for CS/GPU. + +Chosen approach: reuse the command stream and existing counters, adding only boundary snapshots and exact queue-idle accounting. Alternatives: continuous per-call tracing adds observer cost; a new parallel recorder changes scheduling before evidence establishes a need. Neither is required this batch. + +After measurement, optimize only demonstrated generic overhead. Existing equality checks/prefilters must remain; preserve capture masks, untouched state, resource ownership, getters, invalid calls and Reset. Independent HAL fixtures must exercise normal StateBlocks without GTA compatibility, journals with the existing API enabled but no MOD, actual pixel readback and lifetime behavior. Measure sparse/dense/no-op/control workloads and observer off/on using repeated alternating process pairs. Stop retaining a proposed optimization if controls show repeatable harm. + +Dependency acceptance: stand-alone fixture executable outside the game directory; no game/mod DLLs loaded; test compat on/off where supported; inspect PE imports and exports for both candidate DLLs. Windows and a Vulkan-capable driver are inherent dependencies, while journal entry uses the existing backend API, not a third-party MOD. Symbols improve diagnosis but must not be required to query thread counters or restore state. Keep installed files hash/size/mtime unchanged. diff --git a/docs/unified-measurement.md b/docs/unified-measurement.md new file mode 100644 index 0000000..a7292b1 --- /dev/null +++ b/docs/unified-measurement.md @@ -0,0 +1,102 @@ +# F8 unified measurement + +The candidate combines the previous CPU-hotspot and state-attribution captures. +Press F8 once, then keep the scene and camera fixed until the HUD reports a +terminal state. F7 starts the same sequence. Holding a key or pressing either +key during a capture does not restart it. + +The default sequence is 5 seconds of baseline, 10 seconds of CPU sampling, then +3 seconds of journal/backend attribution. The CPU duration and interval still +come from `[CPUHotspotProfile]`; the attribution duration comes from +`[PerformanceDiagnostics] StateAttributionDurationMs`. + +```ini +[Measurement] +Enable=1 +ShowHud=1 +CpuHotspots=1 +JournalAttribution=1 +BaselineMs=5000 +SlowFrameMs=50 +``` + +`Enable=0` retains the standalone F7/F8 behavior. `ShowHud=0` keeps collection +enabled and hides the display. The Bridge adds the `renderstack` DXVK HUD item +when the combined display is enabled. A backend without the private integration +can still provide CPU/journal reports; the missing backend/HUD is recorded as +unavailable. The public API1-7 interfaces and exports are unchanged. + +The HUD updates at most five times per second. It shows stage, progress, live +successful-Present FPS and intervals, each completed stage's FPS, sampling +quality, and the leading function/API/journal result. Files are written after +sampling; the game thread does not parse report files for the display. + +The combined report is +`scripts/BridgeD3D9.measurement--.log`. Raw CPU output +continues in `scripts/BridgeD3D9.cpuhotspots.log`, and the original journal +technique/pass/operation report is retained. Preserve the corresponding DLLs +and adjacent `d3d9.dll.rsym` files when sharing a capture. + +Interpret the results using their recorded units: + +- Baseline, CPU and attribution FPS are separate successful-Present counts over + elapsed time. The latter two include their diagnostic overhead. These are + observed presentation intervals, not CPU or GPU execution times. +- CPU rows contain sampled IPs, module path/base/RVA, function, and symbol + quality. Sidecars must match the exact DLL SHA-256. Missing, inferred and + ambiguous symbols remain labelled. A sample percentage is not an exact + function duration or a promise of FPS improvement. +- Captured x86 frame chains receive structural validation; arbitrary executable + stack addresses remain explicitly heuristic. This is not a full unwinder. + Suspension time, failed samples, unmatched intervals and dropped bounded + records are reported separately. +- Slow/ordinary classifications use the observed Present interval containing + each sample and the configured threshold. They associate evidence with a + frame interval; they do not prove which sampled function caused the delay. +- Backend timings cover only calls counted as timed. The measured scope starts + after the device lock is acquired, so lock-acquisition wait is excluded. + Nested calls and journal transaction/restore spans are inclusive; do not sum + them as disjoint frame costs. Untimed or unavailable work is not zero cost. + +Reset, release, or shutdown interrupts an active capture. Attribution closes +admission immediately and drains previously admitted transactions on a worker. +If draining times out, the report stays interrupted/partial even if a late +transaction eventually finishes. A new capture waits for that transaction to +finish so it cannot overwrite the previous capture's tables. + +F8 reports also contain `threadActivity` rows for baseline, CPU and attribution. +These use an optional private backend query independent of GTA compatibility +and third-party MODs. It samples only at phase boundaries, never flushes CS/GPU, +and reports scheduled main/API-thread and CS CPU time, CS condition-wait idle +time (including a wait that has not finished), ordered sequence progress and +pending work including in-flight work, plus existing CS/GPU waits and submission +counters. There is no per-setter clock or additional worker for this feature. + +CPU time is Windows kernel+user thread time with finite accounting precision; +very short windows can report zero CPU time or a percentage above 100. Use the +normal multi-second phases. Non-CPU wall time is not proof of idle or a particular +wait. Existing CS/GPU wait totals are device-wide completed spans that can cross +boundaries; GPU idle is estimated and chunk totals are merged on submission. +These counters are not additive frame costs, and pending excludes injected +high-priority commands. Missing support, incomplete windows or changed caller +thread identity produce `available=0`, without interrupting other diagnostics. + +The candidate is validated with native D3D9/Vulkan fixtures, symbol fixtures, +repeated serial measurement-overhead benchmarks, and the repository pipeline. +Native HUD captures verify the renderer with fixture text. Game appearance, +actual Grove Street frame times and indoor/outdoor behavior require a separate +manual game run after deployment is authorized. + +StateRing diagnostics use a separate on-demand F10 capture. With +`[ProperShadersStateRing] Enable=1`, normal play no longer takes full StateRing +device snapshots. Press its configured key once to collect about one second of +detailed transitions; the next owner polling point after expiry writes +`scripts/BridgeD3D9.propershaders-state-ring.log`. Holding or pressing again +during collection does not extend it. This replaces the former immediate dump +of continuously collected history. Device reset/release or reconfiguration +cancels collection. The Fading guard still runs its required state correction +while diagnostics are idle, and F7/F8 collection is unchanged. +Admission stops at the deadline even if another Present is delayed. A guard +record completed after that deadline is discarded; an empty dump can therefore +mean that no complete guard record fit inside the window, rather than no state +changes occurring. diff --git a/packaging/split/package.toml b/packaging/split/package.toml index 8b583ed..ee4efe9 100644 --- a/packaging/split/package.toml +++ b/packaging/split/package.toml @@ -2,6 +2,10 @@ source = "out/build/bridge/d3d9.dll" destination = "d3d9.dll" +[[file]] +source = "out/build/bridge/d3d9.dll.rsym" +destination = "d3d9.dll.rsym" + [[file]] source = "config/SA.RenderStack.ini" destination = "SA.RenderStack.ini" @@ -18,6 +22,10 @@ destination = "dxvk.conf" source = "out/build/dxvk-x86/src/d3d9/d3d9.dll" destination = "backend/dxvk-gta/d3d9.dll" +[[file]] +source = "out/build/dxvk-x86/src/d3d9/d3d9.dll.rsym" +destination = "backend/dxvk-gta/d3d9.dll.rsym" + [[file]] source = "README.md" destination = "docs/README.md" diff --git a/src/bridge/legacy/BridgeCodePatch.h b/src/bridge/legacy/BridgeCodePatch.h new file mode 100644 index 0000000..efdc1ae --- /dev/null +++ b/src/bridge/legacy/BridgeCodePatch.h @@ -0,0 +1,176 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace BridgeCodePatch { + +struct NativeOps { + decltype(&::VirtualProtect) protect = &::VirtualProtect; + decltype(&::FlushInstructionCache) flush = &::FlushInstructionCache; +}; + +// The caller serializes this object and code execution at its owner safe +// points, and keeps the target allocation/module alive until Restore succeeds. +// No destructor writes code: shutdown must explicitly retry failed rollback. +class CodePatch { +public: + explicit CodePatch(NativeOps ops = {}) noexcept : ops_(ops) {} + CodePatch(const CodePatch&) = delete; + CodePatch& operator=(const CodePatch&) = delete; + CodePatch(CodePatch&&) = delete; + CodePatch& operator=(CodePatch&&) = delete; + + bool Install(void* site, const void* expected, const void* replacement, + std::size_t size) noexcept + { + error_ = ERROR_SUCCESS; + if (!site || !expected || !replacement || size == 0 || + size > original_.size() || !ops_.protect || !ops_.flush) { + return Fail(ERROR_INVALID_PARAMETER); + } + if (owned_) return Fail(ERROR_BUSY); + + // One saved protection must describe the entire byte range. Reject a + // boundary between differently protected regions rather than applying + // the first page's protection to another owner's adjacent page. + MEMORY_BASIC_INFORMATION memory{}; + if (VirtualQuery(site, &memory, sizeof(memory)) != sizeof(memory)) { + return Fail(NativeError()); + } + const auto address = reinterpret_cast(site); + const auto base = reinterpret_cast(memory.BaseAddress); + if (memory.State != MEM_COMMIT || address < base || + address - base > memory.RegionSize || + size > memory.RegionSize - (address - base)) { + return Fail(ERROR_INVALID_ADDRESS); + } + std::array current{}; + if (!Read(site, current.data(), size)) return false; + if (std::memcmp(current.data(), expected, size) != 0) { + return Fail(ERROR_REVISION_MISMATCH); + } + + // Capture both byte arrays before making the site writable, including + // when the caller's replacement buffer aliases the target allocation. + original_ = current; + std::memcpy(replacement_.data(), replacement, size); + DWORD oldProtection = 0; + if (!ops_.protect(site, size, PAGE_EXECUTE_READWRITE, &oldProtection)) { + return Fail(NativeError()); + } + site_ = site; + size_ = size; + originalProtection_ = oldProtection; + protectionPending_ = true; + owned_ = true; + originalWritten_ = false; + std::memcpy(site_, replacement_.data(), size_); + cachePending_ = true; + return CompleteMemoryOperations(); + } + + bool Restore() noexcept + { + error_ = ERROR_SUCCESS; + if (!owned_) return true; + + std::array current{}; + if (!Read(site_, current.data(), size_)) return false; + const auto& expected = originalWritten_ ? original_ : replacement_; + if (std::memcmp(current.data(), expected.data(), size_) != 0) { + return Fail(ERROR_REVISION_MISMATCH); + } + + if (!originalWritten_) { + DWORD ignored = 0; + if (!ops_.protect(site_, size_, PAGE_EXECUTE_READWRITE, &ignored)) { + return Fail(NativeError()); + } + // Keep the protection from Install, even if a failed cleanup left + // this page writable before a later Restore retry. + protectionPending_ = true; + std::memcpy(site_, original_.data(), size_); + originalWritten_ = true; + cachePending_ = true; + } + if (!CompleteMemoryOperations()) return false; + + // Rollback state is released only after bytes, cache and protection + // have all reached their original state. + site_ = nullptr; + size_ = 0; + owned_ = false; + originalWritten_ = false; + return true; + } + + // True means rollback is still this transaction's responsibility. It also + // covers restored original bytes with failed cache/protection cleanup. + bool OwnsBytes() const noexcept { return owned_; } + bool HasPendingProtection() const noexcept { return protectionPending_; } + DWORD Error() const noexcept { return error_; } + +private: + static DWORD NativeError() noexcept + { + const DWORD error = GetLastError(); + return error == ERROR_SUCCESS ? ERROR_GEN_FAILURE : error; + } + + bool Fail(DWORD error) noexcept + { + error_ = error; + SetLastError(error); + return false; + } + + bool Read(const void* site, void* bytes, std::size_t size) noexcept + { + SIZE_T read = 0; + if (!ReadProcessMemory(GetCurrentProcess(), site, bytes, size, &read)) { + return Fail(NativeError()); + } + if (read != size) return Fail(ERROR_PARTIAL_COPY); + return true; + } + + bool CompleteMemoryOperations() noexcept + { + DWORD failure = ERROR_SUCCESS; + if (cachePending_) { + if (ops_.flush(GetCurrentProcess(), site_, size_)) { + cachePending_ = false; + } else { + failure = NativeError(); + } + } + // Always attempt to restore page protection, even after a cache error. + // Each failed operation stays pending until a later Restore retry. + if (protectionPending_) { + DWORD ignored = 0; + if (ops_.protect(site_, size_, originalProtection_, &ignored)) { + protectionPending_ = false; + } else { + failure = NativeError(); + } + } + return failure == ERROR_SUCCESS ? true : Fail(failure); + } + + NativeOps ops_; + std::array original_{}; + std::array replacement_{}; + void* site_ = nullptr; + std::size_t size_ = 0; + DWORD originalProtection_ = 0; + DWORD error_ = ERROR_SUCCESS; + bool owned_ = false; + bool originalWritten_ = false; + bool protectionPending_ = false; + bool cachePending_ = false; +}; +} // namespace BridgeCodePatch diff --git a/src/bridge/legacy/BridgeD3D9.cpp b/src/bridge/legacy/BridgeD3D9.cpp index 325392e..5c148e5 100644 --- a/src/bridge/legacy/BridgeD3D9.cpp +++ b/src/bridge/legacy/BridgeD3D9.cpp @@ -9,6 +9,9 @@ #include "ProperShadersBatchPolicy.h" #include "ProperShadersEffectBindingCache.h" #include "ProperShadersPatchValidation.h" +#include "BridgeCodePatch.h" +#include "BridgeLog.h" +#include "ProperShadersIatHook.h" #include "EffectInspector.h" #include "ProperShadersStateJournal.h" #include "ProperShadersRenderStateGuard.h" @@ -16,7 +19,9 @@ #include "BridgePerformanceProviderV1.h" #include "PerformanceAdapterConfig.h" #include "PerformanceAdapters.h" +#include "BridgeMeasurementRuntime.h" #include "../../../backend/dxvk/src/util/util_thread_scheduling.h" +#include "../../../backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h" #include #include #include @@ -52,6 +57,36 @@ static constexpr bool kBackendTraceBuild = false; static HMODULE g_selfModule = nullptr; static HMODULE g_realD3D9 = nullptr; static HMODULE g_psProxy = nullptr; +static INIT_ONCE g_bridgeInitOnce = INIT_ONCE_STATIC_INIT; +static std::recursive_mutex g_bridgeLifecycleMutex; +static std::atomic g_bridgeStopping{ false }; +static std::atomic g_bridgeShutdownRunning{ false }; +static bool g_bridgeShutdownComplete = false; +static UINT g_liveBridgeDevices = 0; +static DWORD g_patchOwnerThread = 0; +static HANDLE g_bridgeStopEvent = nullptr; +static std::mutex g_bridgeWorkerMutex; +static std::array g_bridgeWorkers{}; +static bool EnsureBridgeInitialized(); +static HRESULT ShutdownBridgeRuntime(); + +static HANDLE StartBridgeWorker(LPTHREAD_START_ROUTINE entry, void* context) +{ + std::lock_guard lock(g_bridgeWorkerMutex); + if (g_bridgeStopping.load()) return nullptr; + for (auto& worker : g_bridgeWorkers) { + if (worker && WaitForSingleObject(worker, 0) == WAIT_OBJECT_0) { + CloseHandle(worker); + worker = nullptr; + } + if (!worker) { + worker = CreateThread(nullptr, 0, entry, context, 0, nullptr); + return worker; + } + } + SetLastError(ERROR_TOO_MANY_TCBS); + return nullptr; +} static bool g_useDxvkBackend = false; static bool g_enablePostFxHost = false; static char g_postFxIniPath[MAX_PATH]{}; @@ -94,7 +129,7 @@ static bool g_properShadersGeneralStateJournal = true; static bool g_properShadersEffectBatching = false; static bool g_properShadersSkipDuplicateMatrices = true; static bool g_properShadersSkipDuplicateParameters = true; -static bool g_properShadersDirectConstants = true; +static bool g_properShadersDirectConstants = false; static bool g_properShadersEffectOptimizationNoSaveState = false; static bool g_properShadersEffectOptimizationAutoBenchmark = false; static DWORD g_properShadersEffectOptimizationWarmupMs = 10000; @@ -190,6 +225,7 @@ struct VulkanHostDevice ID3D9GtaSaCompatDevice1* compat = nullptr; D3D9GtaSaCompatStatus status{}; bool suspended = false; + bool ownsDevice = true; }; struct LoadedPlugin @@ -247,7 +283,7 @@ static void FormatTo(char* output, size_t outputSize, } template -static void Log(std::format_string fmt, Args&&... args) +static void Log(std::format_string fmt, Args&&... args) noexcept { static FILE* f = nullptr; if (!f) { @@ -256,13 +292,7 @@ static void Log(std::format_string fmt, Args&&... args) : "scripts\\BridgeD3D9.log", "a"); if (!f) return; } - SYSTEMTIME st; - GetLocalTime(&st); - std::print(f, "[{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] ", - st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); - std::vprint_nonunicode(f, fmt.get(), std::make_format_args(args...)); - fputc('\n', f); - fflush(f); + BridgeDiagnostics::Write(f, fmt, std::forward(args)...); } static void ThreadSchedulingLog(const char* message) noexcept @@ -330,6 +360,7 @@ struct ProperShadersCallPatch void* replacement = nullptr; uint8_t original[6]{}; bool installed = false; + BridgeCodePatch::CodePatch code{}; }; enum class ProperShadersAutoBenchmarkStage : uint32_t @@ -358,8 +389,11 @@ struct ProperShadersAutoBenchmarkState static ProperShadersEffectProfileState g_properShadersEffectProfile; static ProperShadersAutoBenchmarkState g_properShadersAutoBenchmark; static HMODULE g_properShadersOptimizationModule = nullptr; +// Rejected modules have already been pinned by the preflight path, so this +// identity cannot be unloaded and reused for another image. +static HMODULE g_properShadersOptimizationRejectedModule = nullptr; +static size_t g_properShadersOptimizationRejectedPatch = SIZE_MAX; static bool g_properShadersOptimizationHooksInstalled = false; -static PVOID volatile* g_properShadersCreateEffectIatSlot = nullptr; static std::atomic g_properShadersNoSaveStateActive{ false }; static thread_local uint32_t g_properShadersOptimizationBeginCalls = 0; static thread_local uint32_t g_properShadersOptimizationModifiedCalls = 0; @@ -422,7 +456,7 @@ using ProperShadersD3DXCreateEffectFn = HRESULT (WINAPI*)( IDirect3DDevice9*, const void*, UINT, const void*, void*, DWORD, void*, void**, void**); -static ProperShadersD3DXCreateEffectFn g_originalProperShadersD3DXCreateEffect = nullptr; +static ProperShadersPatching::IatHook g_properShadersCreateEffectHook; static uint64_t ReadPerformanceCounter() { @@ -1568,7 +1602,8 @@ static HRESULT WINAPI HookedProperShadersD3DXCreateEffect( void** effect, void** compilationErrors) { - const auto original = g_originalProperShadersD3DXCreateEffect; + const auto original = reinterpret_cast( + g_properShadersCreateEffectHook.Original()); if (!original) return E_FAIL; const HRESULT hr = original( @@ -1581,6 +1616,7 @@ static HRESULT WINAPI HookedProperShadersD3DXCreateEffect( pool, effect, compilationErrors); + if (g_bridgeStopping.load()) return hr; if (SUCCEEDED(hr) && effect && *effect && g_properShadersGeneralStateJournal) { ProperShadersStateJournal* journal = AcquireProperShadersStateJournal( *effect, true, false); @@ -3410,10 +3446,6 @@ static PVOID volatile* FindModuleImportSlot( static bool InstallProperShadersCreateEffectHook(HMODULE module) { if (!g_properShadersGeneralStateJournal) return true; - if (g_properShadersCreateEffectIatSlot && - g_originalProperShadersD3DXCreateEffect) { - return true; - } PVOID volatile* slot = FindModuleImportSlot( module, "d3dx9_43.dll", "D3DXCreateEffect"); @@ -3422,56 +3454,42 @@ static bool InstallProperShadersCreateEffectHook(HMODULE module) return false; } - DWORD oldProtect = 0; - if (!VirtualProtect( - const_cast(slot), sizeof(void*), PAGE_READWRITE, &oldProtect)) { - Log("effectgeneral: IAT VirtualProtect failed err={}", GetLastError()); + const HMODULE provider = GetModuleHandleA("d3dx9_43.dll"); + const PVOID expectedOriginal = provider + ? reinterpret_cast(GetProcAddress(provider, "D3DXCreateEffect")) + : nullptr; + if (!expectedOriginal) { + Log("effectgeneral: D3DXCreateEffect export not found err={}", GetLastError()); return false; } - PVOID current = *slot; const PVOID hook = reinterpret_cast(&HookedProperShadersD3DXCreateEffect); - bool installed = false; - if (current == hook && g_originalProperShadersD3DXCreateEffect) { - installed = true; - } else if (current && current != hook) { - PVOID previous = InterlockedCompareExchangePointer(slot, hook, current); - if (previous == current) { - g_originalProperShadersD3DXCreateEffect = - reinterpret_cast(current); - g_properShadersCreateEffectIatSlot = slot; - installed = true; - } - } - - DWORD ignored = 0; - VirtualProtect(const_cast(slot), sizeof(void*), oldProtect, &ignored); + HMODULE pinnedProvider = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(expectedOriginal), &pinnedProvider)) return false; + const bool installed = g_properShadersCreateEffectHook.Install( + slot, expectedOriginal, hook); if (installed) { - Log("effectgeneral: D3DXCreateEffect IAT hook installed slot={:08X} original={:08X}", reinterpret_cast(slot), reinterpret_cast(g_originalProperShadersD3DXCreateEffect)); + Log("effectgeneral: D3DXCreateEffect IAT hook installed slot={:08X} original={:08X}", reinterpret_cast(slot), reinterpret_cast(g_properShadersCreateEffectHook.Original())); } else { - Log("effectgeneral: D3DXCreateEffect IAT hook install race current={:08X}", reinterpret_cast(current)); + Log("effectgeneral: D3DXCreateEffect IAT hook install failed err={} owned={} protectionPending={}", + g_properShadersCreateEffectHook.Error(), + g_properShadersCreateEffectHook.OwnsSlot(), + g_properShadersCreateEffectHook.HasPendingProtection()); } return installed; } -static void RestoreProperShadersCreateEffectHook() +static bool RestoreProperShadersCreateEffectHook() { - PVOID volatile* slot = g_properShadersCreateEffectIatSlot; - const auto original = g_originalProperShadersD3DXCreateEffect; - if (!slot || !original) return; - - DWORD oldProtect = 0; - if (VirtualProtect( - const_cast(slot), sizeof(void*), PAGE_READWRITE, &oldProtect)) { - InterlockedCompareExchangePointer( - slot, - reinterpret_cast(original), - reinterpret_cast(&HookedProperShadersD3DXCreateEffect)); - DWORD ignored = 0; - VirtualProtect(const_cast(slot), sizeof(void*), oldProtect, &ignored); + const bool restored = g_properShadersCreateEffectHook.Restore(); + if (!restored) { + Log("effectgeneral: D3DXCreateEffect IAT hook restore failed err={} owned={} protectionPending={}", + g_properShadersCreateEffectHook.Error(), + g_properShadersCreateEffectHook.OwnsSlot(), + g_properShadersCreateEffectHook.HasPendingProtection()); } - g_properShadersCreateEffectIatSlot = nullptr; - g_originalProperShadersD3DXCreateEffect = nullptr; + return restored; } static bool WriteProperShadersCallPatch(ProperShadersCallPatch& patch, HMODULE module) @@ -3492,43 +3510,46 @@ static bool WriteProperShadersCallPatch(ProperShadersCallPatch& patch, HMODULE m const int32_t relative = static_cast(displacement); std::memcpy(bytes + 1, &relative, sizeof(relative)); - DWORD oldProtect = 0; - if (!VirtualProtect(site, sizeof(bytes), PAGE_EXECUTE_READWRITE, &oldProtect)) return false; - std::memcpy(patch.original, site, sizeof(patch.original)); - std::memcpy(site, bytes, sizeof(bytes)); - FlushInstructionCache(GetCurrentProcess(), site, sizeof(bytes)); - DWORD ignored = 0; - VirtualProtect(site, sizeof(bytes), oldProtect, &ignored); - patch.installed = true; - return true; + const bool installed = patch.code.Install(site, patch.expected, bytes, sizeof(bytes)); + patch.installed = patch.code.OwnsBytes(); + return installed; } -static void RestoreProperShadersOptimizationPatches() +static bool RestoreProperShadersOptimizationPatches() { - HMODULE module = g_properShadersOptimizationModule; - if (!module) return; - for (size_t i = sizeof(g_properShadersOptimizationPatches) / - sizeof(g_properShadersOptimizationPatches[0]); i > 0; --i) { - ProperShadersCallPatch& patch = g_properShadersOptimizationPatches[i - 1]; - if (!patch.installed) continue; - uint8_t* site = reinterpret_cast(module) + patch.rva; - DWORD oldProtect = 0; - if (VirtualProtect(site, sizeof(patch.original), PAGE_EXECUTE_READWRITE, &oldProtect)) { - std::memcpy(site, patch.original, sizeof(patch.original)); - FlushInstructionCache(GetCurrentProcess(), site, sizeof(patch.original)); - DWORD ignored = 0; - VirtualProtect(site, sizeof(patch.original), oldProtect, &ignored); + bool restored = true; + for (auto& patch : g_properShadersOptimizationPatches) { + if (!patch.code.Restore()) { + restored = false; + Log("effectopt: restore pending name={} err={} owned={} protectionPending={}", + patch.name, patch.code.Error(), patch.code.OwnsBytes(), patch.code.HasPendingProtection()); } - patch.installed = false; + patch.installed = patch.code.OwnsBytes(); + } + if (restored) { + g_properShadersOptimizationHooksInstalled = false; + g_properShadersOptimizationModule = nullptr; } - g_properShadersOptimizationHooksInstalled = false; - g_properShadersOptimizationModule = nullptr; g_properShadersNoSaveStateActive.store(false, std::memory_order_relaxed); + return restored; } static bool InstallProperShadersOptimizationPatches(HMODULE module) { if (g_properShadersOptimizationHooksInstalled) return true; + if (g_properShadersOptimizationRejectedModule == module) { + if (g_properShadersOptimizationRejectedPatch == SIZE_MAX) + return false; + const auto& rejected = g_properShadersOptimizationPatches[g_properShadersOptimizationRejectedPatch]; + const auto site = reinterpret_cast(module) + rejected.rva; + // A live call site can be restored by another hook owner. Rechecking + // just this signature avoids repeated full preflights and log writes. + if (!ProperShadersPatching::BytesMatch(site, rejected.expected, sizeof(rejected.expected))) + return false; + } + HMODULE pinned = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(module), &pinned)) return false; size_t imageSize = 0; if (!GetModuleImageSize(module, imageSize)) { @@ -3542,6 +3563,8 @@ static bool InstallProperShadersOptimizationPatches(HMODULE module) const ProperShadersCallPatch& patch = g_properShadersOptimizationPatches[i]; if (patch.rva > imageSize || sizeof(patch.expected) > imageSize - patch.rva) { + g_properShadersOptimizationRejectedModule = module; + g_properShadersOptimizationRejectedPatch = SIZE_MAX; Log("effectopt: signature out of image name={} rva=0x{:08X} imageSize=0x{:08X}", patch.name, static_cast(patch.rva), static_cast(imageSize)); return false; @@ -3553,6 +3576,8 @@ static bool InstallProperShadersOptimizationPatches(HMODULE module) const size_t mismatch = ProperShadersPatching::FindFirstMismatch( patchBytes); if (mismatch != patchCount) { + g_properShadersOptimizationRejectedModule = module; + g_properShadersOptimizationRejectedPatch = mismatch; const ProperShadersCallPatch& patch = g_properShadersOptimizationPatches[mismatch]; const uint8_t* actual = patchBytes[mismatch].actual; @@ -3596,28 +3621,25 @@ static bool InstallProperShadersOptimizationPatches(HMODULE module) return true; } -static void RestoreProperShadersCallPatches() +static bool RestoreProperShadersCallPatches() { - HMODULE module = g_properShadersEffectProfile.module; - if (!module) return; - for (size_t i = sizeof(g_properShadersCallPatches) / sizeof(g_properShadersCallPatches[0]); - i > 0; --i) { - ProperShadersCallPatch& patch = g_properShadersCallPatches[i - 1]; - if (!patch.installed) continue; - uint8_t* site = reinterpret_cast(module) + patch.rva; - DWORD oldProtect = 0; - if (VirtualProtect(site, sizeof(patch.original), PAGE_EXECUTE_READWRITE, &oldProtect)) { - std::memcpy(site, patch.original, sizeof(patch.original)); - FlushInstructionCache(GetCurrentProcess(), site, sizeof(patch.original)); - DWORD ignored = 0; - VirtualProtect(site, sizeof(patch.original), oldProtect, &ignored); + bool restored = true; + for (auto& patch : g_properShadersCallPatches) { + if (!patch.code.Restore()) { + restored = false; + Log("effectprofile: restore pending name={} err={} owned={} protectionPending={}", + patch.name, patch.code.Error(), patch.code.OwnsBytes(), patch.code.HasPendingProtection()); } - patch.installed = false; + patch.installed = patch.code.OwnsBytes(); } + return restored; } static bool InstallProperShadersCallPatches(HMODULE module) { + HMODULE pinned = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(module), &pinned)) return false; size_t imageSize = 0; if (!GetModuleImageSize(module, imageSize)) { Log("effectprofile: failed to read ProperShaders PE image"); @@ -3854,7 +3876,7 @@ static void __stdcall PluginLog(const char* message) } template -static void SafePluginCall(const char* name, Fn fn, Args... args) +static void SafePluginCallSeh(const char* name, Fn fn, Args... args) { if (!fn) return; __try { @@ -3864,6 +3886,12 @@ static void SafePluginCall(const char* name, Fn fn, Args... args) } } +template +static void SafePluginCall(const char* name, Fn fn, Args... args) noexcept +{ + BridgeDiagnostics::BestEffort([&] { SafePluginCallSeh(name, fn, args...); }); +} + static BOOL SafePluginInit1( BridgeD3D9_PluginInit init, const BridgeD3D9PluginApi* api, @@ -3998,7 +4026,7 @@ static void UnregisterPassBindingsLocked( } } -static VulkanHostDevice* AttachVulkanHost(IDirect3DDevice9* device) +static VulkanHostDevice* AttachVulkanHost(IDirect3DDevice9* device, bool borrowed = false) { if (!device) return nullptr; @@ -4034,28 +4062,58 @@ static VulkanHostDevice* AttachVulkanHost(IDirect3DDevice9* device) return nullptr; } host->compat = compat; + host->ownsDevice = !borrowed; host->status = status; size_t passCount = 0; size_t boundCount = 0; { std::lock_guard lock(g_vulkanHostMutex); - g_vulkanHostDevices.push_back(host); - for (auto& plugin : g_plugins) { - for (auto& pass : plugin.vulkanPasses) { - ++passCount; - if (SUCCEEDED(RegisterPassOnHostLocked(*pass, *host))) ++boundCount; + try { + // Allocate every Bridge-owned vector before binding any pass. A + // failed allocation leaves no callback pointing at a partial host. + g_vulkanHostDevices.reserve(g_vulkanHostDevices.size() + 1); + for (auto& plugin : g_plugins) + for (auto& pass : plugin.vulkanPasses) + pass->bindings.reserve(pass->bindings.size() + 1); + g_vulkanHostDevices.push_back(host); + for (auto& plugin : g_plugins) { + for (auto& pass : plugin.vulkanPasses) { + ++passCount; + if (SUCCEEDED(RegisterPassOnHostLocked(*pass, *host))) ++boundCount; + } + } + } catch (...) { + // Cleanup must not allocate/log. Backend tokens and metadata are + // retired before releasing the interface or destroying the host. + for (auto& plugin : g_plugins) { + for (auto& pass : plugin.vulkanPasses) { + for (size_t i = pass->bindings.size(); i > 0; --i) { + if (pass->bindings[i-1].host != host) continue; + (void)compat->UnregisterVulkanPass(pass->bindings[i-1].backendToken); + pass->bindings.erase(pass->bindings.begin() + (i-1)); + } + } } + std::erase(g_vulkanHostDevices, host); + compat->Release(); + delete host; + return nullptr; } } - Log("vulkanhost: attached api={} flags=0x{:08X} backbuffer={}x{} queued={} bound={}", - status.ApiVersion, status.Flags, status.BackBufferWidth, status.BackBufferHeight, - static_cast(passCount), - static_cast(boundCount)); + // Diagnostics must not unwind past a successfully registered host. + try { + Log("vulkanhost: attached api={} flags=0x{:08X} backbuffer={}x{} queued={} bound={}", + status.ApiVersion, status.Flags, status.BackBufferWidth, status.BackBufferHeight, + static_cast(passCount), + static_cast(boundCount)); + } catch (...) { } + if (borrowed) compat->Release(); return host; } + static void SuspendVulkanHost(VulkanHostDevice* host) { if (!host) return; @@ -4101,7 +4159,7 @@ static void DetachVulkanHost(VulkanHostDevice*& host) if (entry != g_vulkanHostDevices.end()) g_vulkanHostDevices.erase(entry); } - host->compat->Release(); + if (host->ownsDevice) host->compat->Release(); delete host; host = nullptr; Log("vulkanhost: detached"); @@ -4433,7 +4491,7 @@ static void OnProperShadersEffectOptimizationPresent() if (!module) module = GetModuleHandleA("propershaders.asi"); if (!module) return; - if (g_properShadersGeneralStateJournal && !g_properShadersCreateEffectIatSlot) { + if (g_properShadersGeneralStateJournal && !g_properShadersCreateEffectHook.OwnsSlot()) { InstallProperShadersCreateEffectHook(module); } if (!g_properShadersOptimizationHooksInstalled) { @@ -4590,6 +4648,7 @@ static void OnProperShadersEffectOptimizationPresent() static bool StartProperShadersStateAttribution() { + if (BridgeMeasurement::RuntimeEnabled()) return false; if (!g_enableProperShadersStateAttribution || g_properShadersStateAttributionActive.load(std::memory_order_acquire)) { return false; @@ -4613,6 +4672,7 @@ static bool StartProperShadersStateAttribution() static void FinishProperShadersStateAttribution(const char* reason) { + if (BridgeMeasurement::RuntimeEnabled()) return; if (!g_properShadersStateAttributionActive.load(std::memory_order_acquire)) return; const ULONGLONG now = GetTickCount64(); const ULONGLONG elapsed = g_properShadersStateAttributionStartTick @@ -5284,6 +5344,47 @@ static void CollectPerformanceProviderRows( } } +static void AppendUnifiedCpuAdapterReport(const char* path, + const BridgeMeasurement::RuntimeCpuReport& report) +{ + EnsurePerformanceConfigLoaded(); + if (!g_performanceRuntimeConfig.enabled) return; + std::vector> addresses; + std::vector> stacks; + std::vector modules; + for (const auto& row : report.addresses) addresses.emplace_back(row.address, row.samples); + // Heuristic stack words remain in the detailed report, but may not turn + // into confident shared-module ownership in the adapter attribution. + for (const auto& row : report.stacks) { + if (row.quality != BridgeMeasurement::ChainQuality::FrameChain) continue; + CpuHotspotStackKey key{}; + key.instructionPointer = row.ip; + key.caller1 = row.callers[0]; key.caller2 = row.callers[1]; key.caller3 = row.callers[2]; + stacks.emplace_back(key, row.samples); + } + for (const auto& module : report.modules) modules.push_back(MakePerformanceModuleIdentity(module.base)); + auto capture = BuildPerformanceCaptureSamples(addresses, stacks, modules, report.samples); + const auto attribution = BridgePerformance::AttributeCapture(g_performanceRuntimeConfig.registry, capture); + BridgePerformance::CaptureMetadata metadata{}; + metadata.captureId = report.captureId; metadata.elapsedMs = report.elapsedMs; + metadata.frameCount = report.presents; metadata.fps = report.fps; + BridgePerformance::ProcessMemoryEvidence memory; + if (g_performanceRuntimeConfig.includeProcessMemory) memory = BridgePerformance::CollectProcessMemory(); + std::vector metrics; + std::vector configs; + auto warnings = g_performanceRuntimeConfig.warnings; + for (const auto& warning : attribution.warnings) + AppendPerformanceWarning(warnings, warning.adapter, warning.code.c_str(), warning.detail); + CollectPerformanceConfigRows(g_performanceRuntimeConfig, configs, warnings); + CollectPerformanceProviderRows(g_performanceRuntimeConfig, metadata, modules, metrics, warnings); + FILE* file = nullptr; + if (fopen_s(&file, path, "a") == 0 && file) { + std::print(file, "adapterEvidence callerPolicy=structural-frame-chain-only frameSource=successful-present-counter\n"); + BridgePerformance::AppendAdapterReport(file, metadata, attribution, memory, metrics, configs, warnings); + fclose(file); + } +} + static std::expected SaveD3D9BackBufferBmp( IDirect3DDevice9* device, const char* outputPath) @@ -5475,7 +5576,7 @@ static DWORD WINAPI CpuHotspotWorker(void* parameter) GetLocalTime(&beginTime); const DWORD startTick = GetTickCount(); while (GetTickCount() - startTick < context->durationMs) { - Sleep(context->intervalMs); + if (WaitForSingleObject(g_bridgeStopEvent, context->intervalMs) != WAIT_TIMEOUT) break; const DWORD suspendResult = SuspendThread(targetThread); if (suspendResult == static_cast(-1)) { @@ -5771,47 +5872,30 @@ static bool g_forceDeviceWrap = false; static bool ShouldWrapD3D9Device() { // Only diagnostics that intercept individual D3D9 calls require the - // per-call device wrapper. Effect optimization, the F7/F8 profilers and - // the CPU hotspot sampler run without it: hook installation moved to the - // unwrapped support worker below, and F8 arming polls from that worker. + // per-call device wrapper. Hook installation runs at the owner-thread + // Present boundary; the support worker only polls the F8 diagnostic key. return kBackendTraceBuild || g_forceDeviceWrap || g_enablePostFxHost || !g_plugins.empty() || g_enableD3D9Stats || g_enableD3D9Trace || g_enableD3D9CallsiteProfile || g_enableD3D9Optimizer; } // ---- Unwrapped-device support ---------------------------------------------- -// With the per-call device wrapper removed, three responsibilities move here: -// 1. ProperShaders hook installation (previously retried from the wrapped -// Present every frame) now runs on a worker thread. Code patches use a -// suspend + EIP-outside-module check so the render thread is never paused -// inside the bytes being rewritten. -// 2. F8 CPU hotspot arming (previously polled in the wrapped Present). -// The backbuffer screenshot is skipped in this mode: this thread must not -// call into a device the render thread is using concurrently. -// 3. Vulkan host attach and render-thread id capture happen at CreateDevice. -static IDirect3DDevice9* g_unwrappedDevice = nullptr; -static VulkanHostDevice* g_unwrappedVulkanHost = nullptr; -static DWORD g_unwrappedRenderThreadId = 0; -static std::atomic g_unwrappedDeviceClaimed{ 0 }; -static std::atomic g_unwrappedWorkerStarted{ 0 }; +// Only lifecycle boundaries are observed; Draw/Set calls remain raw DXVK calls. +struct UnwrappedDeviceContext { + IDirect3DDevice9* device = nullptr; + ID3D9BridgeLifecycle* lifecycle = nullptr; // borrowed until FinalRelease + VulkanHostDevice* host = nullptr; + DWORD ownerThread = 0; +}; +static std::vector> g_unwrappedDevices; +static std::atomic g_unwrappedRenderThreadId{0}; +static std::atomic g_unwrappedWorkerStarted{false}; static bool g_unwrappedHotspotTriggerWasDown = false; static bool g_unwrappedStateAttributionTriggerWasDown = false; -static bool ThreadOutsideModule(HANDLE thread, HMODULE module) -{ - uintptr_t base = 0; - uintptr_t size = 0; - uintptr_t preferred = 0; - if (!GetPeImageInfo(module, base, size, preferred) || !size) return false; - CONTEXT context{}; - context.ContextFlags = CONTEXT_CONTROL; - if (!GetThreadContext(thread, &context)) return false; - const uintptr_t ip = context.Eip; - return ip < base || ip >= base + size; -} - static void MaybeArmCpuHotspotProfileUnwrapped() { + if (BridgeMeasurement::RuntimeEnabled()) return; if (!g_enableCpuHotspotProfile) return; const bool triggerDown = (GetAsyncKeyState(g_cpuHotspotTriggerKey) & 0x8000) != 0; @@ -5841,8 +5925,8 @@ static void MaybeArmCpuHotspotProfileUnwrapped() return; } - context->targetThreadId = g_unwrappedRenderThreadId - ? g_unwrappedRenderThreadId + context->targetThreadId = g_unwrappedRenderThreadId.load() + ? g_unwrappedRenderThreadId.load() : GetCurrentThreadId(); context->captureId = static_cast( g_cpuHotspotCaptureId.fetch_add(1) + 1); @@ -5857,7 +5941,7 @@ static void MaybeArmCpuHotspotProfileUnwrapped() const DWORD durationMs = context->durationMs; const DWORD intervalMs = context->intervalMs; - HANDLE worker = CreateThread(nullptr, 0, CpuHotspotWorker, context, 0, nullptr); + HANDLE worker = StartBridgeWorker(CpuHotspotWorker, context); if (!worker) { Log("cpuhotspots: capture={} CreateThread failed err={}", captureId, GetLastError()); @@ -5865,7 +5949,6 @@ static void MaybeArmCpuHotspotProfileUnwrapped() g_cpuHotspotActive.store(0); return; } - CloseHandle(worker); Log("cpuhotspots: capture={} armed targetThread={} durationMs={} intervalMs={} screenshotSaved=0 screenshot=(skipped-unwrapped)", captureId, targetThreadId, durationMs, intervalMs); @@ -5876,86 +5959,131 @@ static DWORD WINAPI UnwrappedSupportThread(LPVOID) renderstack::scheduling::ThreadSchedulingScope scheduling( g_threadSchedulingOptions, renderstack::scheduling::Role::Background, &ThreadSchedulingLog); - if (!g_threadSchedulingOptions.enabled) { - // Preserve the existing worker affinity when the M1 policy is disabled. - SetThreadAffinityMask(GetCurrentThread(), kEfficiencyCoreMask); - } - Log("postfx: unwrapped support worker started renderThread={}", - g_unwrappedRenderThreadId); - const ULONGLONG installDeadline = GetTickCount64() + 180000ull; - bool installDone = !g_enableProperShadersEffectOptimization; - for (;;) { - if (!installDone) { - HMODULE module = GetModuleHandleA("ProperShaders.asi"); - if (!module) module = GetModuleHandleA("propershaders.asi"); - if (module) { - if (g_properShadersGeneralStateJournal && !g_properShadersCreateEffectIatSlot) { - // Single aligned pointer store; safe without suspending. - InstallProperShadersCreateEffectHook(module); - } - if (!g_properShadersOptimizationHooksInstalled) { - HANDLE renderThread = OpenThread( - THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, - FALSE, g_unwrappedRenderThreadId); - if (renderThread) { - if (SuspendThread(renderThread) != static_cast(-1)) { - if (ThreadOutsideModule(renderThread, module)) { - InstallProperShadersOptimizationPatches(module); - } - ResumeThread(renderThread); - } - CloseHandle(renderThread); - } - } - const bool iatReady = !g_properShadersGeneralStateJournal || - g_properShadersCreateEffectIatSlot; - if (iatReady && g_properShadersOptimizationHooksInstalled) { - installDone = true; - g_properShadersNoSaveStateActive.store(true, std::memory_order_release); - Log("effectopt: incremental state journal active; installed from unwrapped worker"); - } - } - if (!installDone && GetTickCount64() >= installDeadline) { - installDone = true; - Log("effectopt: unwrapped install timed out after 180s; optimization inactive this session"); - } - } + Log("postfx: unwrapped support worker started"); + while (WaitForSingleObject(g_bridgeStopEvent, 30) == WAIT_TIMEOUT) { MaybeArmCpuHotspotProfileUnwrapped(); - PollProperShadersStateAttribution( - g_unwrappedStateAttributionTriggerWasDown, false); - // The worker owns the diagnostic hotkeys in unwrapped mode. Keep its - // polling interval short only while a diagnostic is enabled so normal - // gameplay does not acquire a tighter wake-up loop. - const DWORD pollIntervalMs = - (g_enableCpuHotspotProfile || g_enableProperShadersStateAttribution) - ? 8u - : (installDone ? 30u : 100u); - Sleep(pollIntervalMs); } + Log("postfx: unwrapped support worker stopped"); return 0; } -static void OnUnwrappedDeviceCreated(IDirect3DDevice9* device) +static void RunBridgeRenderSafePoint() +{ + std::lock_guard lock(g_bridgeLifecycleMutex); + if (g_bridgeStopping.load()) return; + if (g_liveBridgeDevices != 1) return; + if (!g_patchOwnerThread) g_patchOwnerThread = GetCurrentThreadId(); + if (g_patchOwnerThread != GetCurrentThreadId()) return; + PollProperShadersRenderStateGuards(); + OnProperShadersEffectOptimizationPresent(); +} + +static void STDMETHODCALLTYPE OnUnwrappedDeviceEvent(void* data, + IDirect3DDevice9* device, D3D9BridgeEvent event, HRESULT result, + D3DPRESENT_PARAMETERS* params, const D3D9BridgePresentArgs* present) noexcept +{ + BridgeDiagnostics::BestEffort([&] { + std::lock_guard lock(g_bridgeLifecycleMutex); + auto* context = static_cast(data); + switch (event) { + case D3D9BridgeEvent::PresentBefore: + // Hooks are installed only at the owning render thread's boundary. + // No other thread is suspended, so CRT/heap/log locks stay available. + if (GetCurrentThreadId() == context->ownerThread) RunBridgeRenderSafePoint(); + for (auto& plugin : g_plugins) + SafePluginCall("OnPresentBefore", plugin.onPresentBefore, device, + present->Source, present->Destination, present->Window, present->DirtyRegion); + break; + case D3D9BridgeEvent::PresentAfter: + ++g_cpuHotspotPresents; + if (BridgeMeasurement::RuntimeEnabled()) BridgeMeasurement::RuntimePresent(device, result); + else PollProperShadersStateAttribution(g_unwrappedStateAttributionTriggerWasDown, true); + for (auto& plugin : g_plugins) + SafePluginCall("OnPresentAfter", plugin.onPresentAfter, device, result); + break; + case D3D9BridgeEvent::ResetBefore: + BridgeDiagnostics::BestEffort([&] { + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimeInterrupt(device, "device-reset"); + FinishProperShadersEffectProfile("device-reset"); + FinishProperShadersStateAttribution("device-reset"); + }); + SuspendVulkanHost(context->host); + for (auto& plugin : g_plugins) + SafePluginCall("OnResetBefore", plugin.onResetBefore, device, params); + break; + case D3D9BridgeEvent::ResetAfter: + for (auto& plugin : g_plugins) + SafePluginCall("OnResetAfter", plugin.onResetAfter, device, result, params); + if (SUCCEEDED(result)) ResumeVulkanHost(context->host); + break; + case D3D9BridgeEvent::FinalRelease: + BridgeDiagnostics::BestEffort([&] { + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimeInterrupt(device, "device-destroyed", true); + FinishProperShadersEffectProfile("device-destroyed"); + FinishProperShadersStateAttribution("device-destroyed"); + }); + DetachVulkanHost(context->host); + for (auto& plugin : g_plugins) + SafePluginCall("OnReleaseDevice", plugin.onReleaseDevice, device); + std::erase_if(g_unwrappedDevices, + [context](const auto& entry) { return entry.get() == context; }); + --g_liveBridgeDevices; + if (!g_liveBridgeDevices) g_patchOwnerThread = 0; + else if (!g_unwrappedDevices.empty()) g_patchOwnerThread = g_unwrappedDevices.front()->ownerThread; + g_unwrappedRenderThreadId.store(g_unwrappedDevices.empty() + ? 0 : g_unwrappedDevices.front()->ownerThread); + Log("postfx: unwrapped device released; remaining={}", g_liveBridgeDevices); + break; + } + }); +} + +static bool OnUnwrappedDeviceCreated(IDirect3DDevice9* device) { - // Only the first device is the game's render device. D3DX9 helper devices - // created later must not steal the render-thread id or the Vulkan host. - if (g_unwrappedDeviceClaimed.exchange(1) != 0) { - Log("postfx: additional device left unwrapped inner=0x{:08X} (not claimed)", reinterpret_cast(device)); - return; + ID3D9BridgeLifecycle* lifecycle = nullptr; + if (FAILED(device->QueryInterface(IID_D3D9BridgeLifecycle, + reinterpret_cast(&lifecycle)))) return false; + const auto releaseLifecycle = [](ID3D9BridgeLifecycle* value) { value->Release(); }; + std::unique_ptr lifecycleRef(lifecycle, releaseLifecycle); + std::unique_ptr context; + try { + context = std::make_unique(); + g_unwrappedDevices.reserve(g_unwrappedDevices.size() + 1); + } catch (...) { return false; } + context->device = device; + context->lifecycle = lifecycle; + context->ownerThread = GetCurrentThreadId(); + // Host allocation/registration is transactional and precedes publication. + context->host = AttachVulkanHost(device, true); + const D3D9BridgeObserver observer{sizeof(observer), context.get(), &OnUnwrappedDeviceEvent}; + if (FAILED(lifecycle->SetObserver(&observer))) { + DetachVulkanHost(context->host); + return false; } - g_unwrappedDevice = device; - g_unwrappedRenderThreadId = GetCurrentThreadId(); - g_unwrappedVulkanHost = AttachVulkanHost(device); - if (g_unwrappedWorkerStarted.exchange(1) == 0) { - HANDLE thread = CreateThread(nullptr, 0, UnwrappedSupportThread, nullptr, 0, nullptr); - if (thread) { - CloseHandle(thread); - } else { - g_unwrappedWorkerStarted.store(0); - Log("postfx: unwrapped support worker CreateThread failed err={}", GetLastError()); + // The preallocated unique_ptr move cannot throw after SetObserver. The QI + // scope releases its temporary reference after context ownership commits. + g_unwrappedDevices.push_back(std::move(context)); + ++g_liveBridgeDevices; + if (g_unwrappedDevices.size() == 1) + g_unwrappedRenderThreadId.store(GetCurrentThreadId()); + if (!g_unwrappedWorkerStarted.exchange(true)) { + if (!StartBridgeWorker(UnwrappedSupportThread, nullptr)) { + g_unwrappedWorkerStarted.store(false); + Log("postfx: unwrapped worker start failed err={}", GetLastError()); } } - Log("postfx: device left unwrapped inner=0x{:08X} renderThread={} (per-call wrapper disabled)", reinterpret_cast(device), g_unwrappedRenderThreadId); + BridgeDiagnostics::BestEffort([&] { + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimePrepareDevice(device); + }); + Log("postfx: lifecycle attached to raw device=0x{:08X} live={}", + reinterpret_cast(device), g_liveBridgeDevices); + return true; } @@ -6325,7 +6453,7 @@ static DWORD WINAPI AffinityReapplyThread(void*) Log("affinity: reapply worker started count={} intervalMs={}", g_affinityReapplyCount, g_affinityReapplyIntervalMs); for (DWORD i = 0; i < g_affinityReapplyCount; ++i) { - Sleep(g_affinityReapplyIntervalMs); + if (WaitForSingleObject(g_bridgeStopEvent, g_affinityReapplyIntervalMs) != WAIT_TIMEOUT) break; ApplyAffinityAndPriority("reapply"); } Log("affinity: reapply worker finished"); @@ -6411,7 +6539,10 @@ static bool SetDxvkEnvironment(const char* name, const char* value) static void ConfigureDxvkDiagnostics(const char* iniPath) { const bool enabled = GetPrivateProfileIntA("DXVKDiagnostics", "Enable", 0, iniPath) != 0; + const bool measurementHud = GetPrivateProfileIntA("Measurement", "Enable", 0, iniPath) != 0 && + GetPrivateProfileIntA("Measurement", "ShowHud", 1, iniPath) != 0; if (!enabled) { + if (measurementHud) SetDxvkEnvironment("DXVK_HUD", "renderstack"); Log("dxvkdiag: disabled"); return; } @@ -6445,6 +6576,10 @@ static void ConfigureDxvkDiagnostics(const char* iniPath) char debugMode[32]{}; GetPrivateProfileStringA("DXVKDiagnostics", "LogLevel", "trace", logLevel, sizeof(logLevel), iniPath); GetPrivateProfileStringA("DXVKDiagnostics", "Hud", "full", hud, sizeof(hud), iniPath); + if (measurementHud && !std::strstr(hud, "renderstack")) { + if (hud[0]) strncat_s(hud, ",", _TRUNCATE); + strncat_s(hud, "renderstack", _TRUNCATE); + } GetPrivateProfileStringA("DXVKDiagnostics", "Debug", "markers", debugMode, sizeof(debugMode), iniPath); SetDxvkEnvironment("DXVK_LOG_LEVEL", logLevel); @@ -7118,6 +7253,28 @@ static void RestoreSystemDirectoryHook() Log("backend: temporary GetSystemDirectoryA hook restored"); } +struct MeasurementWorkerStart { + LPTHREAD_START_ROUTINE entry; + void* context; +}; + +static DWORD WINAPI MeasurementBackgroundWorker(void* data) +{ + std::unique_ptr start(static_cast(data)); + renderstack::scheduling::ThreadSchedulingScope scheduling( + g_threadSchedulingOptions, renderstack::scheduling::Role::Background, &ThreadSchedulingLog); + return start->entry(start->context); +} + +static HANDLE StartMeasurementWorker(LPTHREAD_START_ROUTINE entry, void* context) +{ + auto start = std::unique_ptr(new (std::nothrow) MeasurementWorkerStart{entry, context}); + if (!start) return nullptr; + HANDLE thread = StartBridgeWorker(&MeasurementBackgroundWorker, start.get()); + if (thread) start.release(); + return thread; +} + static void LoadBridgeConfig() { char gameDir[MAX_PATH]{}; @@ -7321,8 +7478,13 @@ static void LoadBridgeConfig() "ProperShadersEffectOptimization", "SkipDuplicateMatrices", 1, iniPath) != 0; g_properShadersSkipDuplicateParameters = GetPrivateProfileIntA( "ProperShadersEffectOptimization", "SkipDuplicateParameters", 1, iniPath) != 0; - g_properShadersDirectConstants = GetPrivateProfileIntA( - "ProperShadersEffectOptimization", "DirectConstants", 1, iniPath) != 0; + const bool requestedDirectConstants = GetPrivateProfileIntA( + "ProperShadersEffectOptimization", "DirectConstants", 0, iniPath) != 0; + // Quarantined after geometry/sprite state leaked into later draws. Keep + // this gate closed even for an older profile that explicitly requests it. + g_properShadersDirectConstants = false; + Log("directConstants: requested={} effective=0 reason=production-quarantine", + requestedDirectConstants ? 1 : 0); g_properShadersNativeStateJournalPolicy = GetPrivateProfileIntA( "ProperShadersEffectOptimization", "NativeStateJournal", 1, iniPath) != 0; g_properShadersInspectEffects = GetPrivateProfileIntA( @@ -7385,6 +7547,26 @@ static void LoadBridgeConfig() g_enablePostFxHost ? "deferred-to-Direct3DCreate9" : "none"); LoadProxyChainConfig(iniPath); + BridgeMeasurement::RuntimeConfig measurement{}; + measurement.enabled = GetPrivateProfileIntA("Measurement", "Enable", 0, iniPath) != 0; + measurement.hud = GetPrivateProfileIntA("Measurement", "ShowHud", 1, iniPath) != 0; + measurement.cpu = GetPrivateProfileIntA("Measurement", "CpuHotspots", 1, iniPath) != 0; + measurement.journal = GetPrivateProfileIntA("Measurement", "JournalAttribution", 1, iniPath) != 0; + measurement.timing.baselineMs = std::clamp(GetPrivateProfileIntA("Measurement", "BaselineMs", 5000, iniPath), 1000, 30000); + measurement.timing.cpuMs = g_cpuHotspotDurationMs; + measurement.timing.attributionMs = g_properShadersStateAttributionDurationMs; + measurement.timing.slowFrameMs = std::clamp(GetPrivateProfileIntA("Measurement", "SlowFrameMs", 50, iniPath), 1, 1000); + measurement.intervalMs = g_cpuHotspotIntervalMs; + measurement.gameDirectory = g_gameDir; measurement.stopEvent = g_bridgeStopEvent; + measurement.startWorker = &StartMeasurementWorker; + measurement.log = +[](const char* message) { Log("measurement: {}", message); }; + measurement.readFrame = &ReadGtasaFrameCounter; + measurement.appendCpuReport = &AppendUnifiedCpuAdapterReport; + BridgeMeasurement::ConfigureRuntime(measurement); + Log("measurement: enabled={} HUD={} F8/F7=combined baselineMs={} cpuMs={} stateMs={} samplingIntervalMs={}", + measurement.enabled, measurement.hud, measurement.timing.baselineMs, measurement.timing.cpuMs, + measurement.timing.attributionMs, measurement.intervalMs); + int useDxvk = GetPrivateProfileIntA("Backend", "UseDxvkBackend", 0, iniPath); char backendRel[MAX_PATH]{}; GetPrivateProfileStringA("Backend", "DxvkBackendDir", "dxvk", backendRel, sizeof(backendRel), iniPath); @@ -7808,36 +7990,54 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 explicit BridgeDirect3DDevice9(IDirect3DDevice9* inner) : m_inner(inner), m_refs(1), m_renderThreadId(GetCurrentThreadId()) { - Log("postfx: wrapped IDirect3DDevice9 0x{:08X} -> 0x{:08X}", reinterpret_cast(inner), reinterpret_cast(this)); - m_vulkanHost = AttachVulkanHost(inner); - m_lastStatsTick = GetTickCount(); - m_lastStats = m_stats; - GetPeImageInfo(GetModuleHandleA(nullptr), m_gameImageBase, m_gameImageSize, - m_gamePreferredBase); - if (g_enableD3D9CallsiteProfile) { - Log("d3d9callsites: gameImage base={:08X} size=0x{:08X} preferred={:08X}", reinterpret_cast(reinterpret_cast(m_gameImageBase)), - static_cast(m_gameImageSize), reinterpret_cast(reinterpret_cast(m_gamePreferredBase))); - RefreshCallsiteRenderTarget(); - } -#ifdef BRIDGE_D3D9_BACKEND_TRACE - m_backendAlphaRing = new (std::nothrow) BackendAlphaDrawRecord[kBackendAlphaRingCapacity]; - Log("backendtrace: alpha ring allocated records={} bytes={} ptr={:08X}", - static_cast(kBackendAlphaRingCapacity), - static_cast(sizeof(BackendAlphaDrawRecord) * kBackendAlphaRingCapacity), reinterpret_cast(m_backendAlphaRing)); -#endif + try { + Log("postfx: wrapped IDirect3DDevice9 0x{:08X} -> 0x{:08X}", reinterpret_cast(inner), reinterpret_cast(this)); + m_vulkanHost = AttachVulkanHost(inner); + m_lastStatsTick = GetTickCount(); + m_lastStats = m_stats; + GetPeImageInfo(GetModuleHandleA(nullptr), m_gameImageBase, m_gameImageSize, + m_gamePreferredBase); + if (g_enableD3D9CallsiteProfile) { + Log("d3d9callsites: gameImage base={:08X} size=0x{:08X} preferred={:08X}", reinterpret_cast(reinterpret_cast(m_gameImageBase)), + static_cast(m_gameImageSize), reinterpret_cast(reinterpret_cast(m_gamePreferredBase))); + RefreshCallsiteRenderTarget(); + } + #ifdef BRIDGE_D3D9_BACKEND_TRACE + m_backendAlphaRing = new (std::nothrow) BackendAlphaDrawRecord[kBackendAlphaRingCapacity]; + Log("backendtrace: alpha ring allocated records={} bytes={} ptr={:08X}", + static_cast(kBackendAlphaRingCapacity), + static_cast(sizeof(BackendAlphaDrawRecord) * kBackendAlphaRingCapacity), reinterpret_cast(m_backendAlphaRing)); + #endif + ++g_liveBridgeDevices; + } catch (...) { + DetachVulkanHost(m_vulkanHost); + throw; + } + // No fallible construction remains after publishing borrowed state. + BridgeDiagnostics::BestEffort([&] { + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimePrepareDevice(inner); + }); } ~BridgeDirect3DDevice9() { + std::lock_guard lifecycleLock(g_bridgeLifecycleMutex); #ifdef BRIDGE_D3D9_BACKEND_TRACE DumpBackendTrace("device-destroyed"); delete[] m_backendAlphaRing; m_backendAlphaRing = nullptr; #endif + BridgeDiagnostics::BestEffort([&] { FinishCallsiteProfile("device-destroyed"); FinishDrawTrace("device-destroyed"); FinishProperShadersEffectProfile("device-destroyed"); FinishProperShadersStateAttribution("device-destroyed"); + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimeInterrupt(m_inner, "device-destroyed", true); + }); DetachVulkanHost(m_vulkanHost); for (auto& plugin : g_plugins) { SafePluginCall("OnReleaseDevice", plugin.onReleaseDevice, m_inner); @@ -7847,6 +8047,8 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 m_inner = nullptr; } Log("postfx: device wrapper destroyed"); + --g_liveBridgeDevices; + if (!g_liveBridgeDevices) g_patchOwnerThread = 0; } HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObj) override @@ -7893,6 +8095,9 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 FinishDrawTrace("device-reset"); FinishProperShadersEffectProfile("device-reset"); FinishProperShadersStateAttribution("device-reset"); + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + BridgeMeasurement::RuntimeInterrupt(m_inner, "device-reset"); ResetTrackedState(); SuspendVulkanHost(m_vulkanHost); for (auto& plugin : g_plugins) { @@ -7908,6 +8113,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 HRESULT STDMETHODCALLTYPE Present(const RECT* pSourceRect, const RECT* pDestRect, HWND hDestWindowOverride, const RGNDATA* pDirtyRegion) override { + RunBridgeRenderSafePoint(); m_renderThreadId = GetCurrentThreadId(); MaybeArmCpuHotspotProfile(); for (auto& plugin : g_plugins) { @@ -7931,8 +8137,9 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 #endif OnCallsitePresent(); OnProperShadersEffectProfilePresent(); - PollProperShadersStateAttribution(m_stateAttributionTriggerWasDown, true); - OnProperShadersEffectOptimizationPresent(); + if (BridgeMeasurement::RuntimeEnabled()) { + BridgeDiagnostics::BestEffort([&] { BridgeMeasurement::RuntimePresent(m_inner, hr); }); + } else PollProperShadersStateAttribution(m_stateAttributionTriggerWasDown, true); ResetTrackedState(); for (auto& plugin : g_plugins) { SafePluginCall("OnPresentAfter", plugin.onPresentAfter, m_inner, hr); @@ -8786,6 +8993,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 void OnProperShadersEffectProfilePresent() { + if (BridgeMeasurement::RuntimeEnabled()) return; if (!g_enableProperShadersEffectProfile) return; const DWORD currentThreadId = GetCurrentThreadId(); @@ -8816,6 +9024,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 void MaybeArmCpuHotspotProfile() { + if (BridgeMeasurement::RuntimeEnabled()) return; if (!g_enableCpuHotspotProfile) return; const bool triggerDown = (GetAsyncKeyState(g_cpuHotspotTriggerKey) & 0x8000) != 0; @@ -8886,7 +9095,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 char screenshotPath[MAX_PATH]{}; FormatTo(screenshotPath, sizeof(screenshotPath), "{}", context->screenshotPath); - HANDLE worker = CreateThread(nullptr, 0, CpuHotspotWorker, context, 0, nullptr); + HANDLE worker = StartBridgeWorker(CpuHotspotWorker, context); if (!worker) { Log("cpuhotspots: capture={} CreateThread failed err={}", captureId, GetLastError()); @@ -8894,8 +9103,6 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 g_cpuHotspotActive.store(0); return; } - CloseHandle(worker); - Log("cpuhotspots: capture={} armed targetThread={} durationMs={} intervalMs={} screenshotSaved={} screenshot={}", captureId, targetThreadId, @@ -9715,7 +9922,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 #endif }; -class BridgeDirect3D9 final : public IDirect3D9 +class BridgeDirect3D9 final : public IDirect3D9, public ID3D9BridgeRuntime { public: explicit BridgeDirect3D9(IDirect3D9* inner) @@ -9736,6 +9943,11 @@ class BridgeDirect3D9 final : public IDirect3D9 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObj) override { if (!ppvObj) return E_POINTER; + if (riid == IID_D3D9BridgeRuntime) { + *ppvObj = static_cast(this); + AddRef(); + return S_OK; + } if (riid == IID_IUnknown || riid == IID_IDirect3D9) { *ppvObj = static_cast(this); AddRef(); @@ -9745,6 +9957,7 @@ class BridgeDirect3D9 final : public IDirect3D9 } ULONG STDMETHODCALLTYPE AddRef() override { return m_refs.fetch_add(1) + 1; } + HRESULT STDMETHODCALLTYPE Shutdown() override { return ShutdownBridgeRuntime(); } ULONG STDMETHODCALLTYPE Release() override { ULONG refs = m_refs.fetch_sub(1) - 1; @@ -9770,6 +9983,10 @@ class BridgeDirect3D9 final : public IDirect3D9 DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface) override { + std::lock_guard lifecycleLock(g_bridgeLifecycleMutex); + if (!ppReturnedDeviceInterface) return D3DERR_INVALIDCALL; + *ppReturnedDeviceInterface = nullptr; + if (g_bridgeStopping.load()) return D3DERR_INVALIDCALL; IDirect3DDevice9* device = nullptr; HRESULT hr = m_inner->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, &device); @@ -9777,6 +9994,7 @@ class BridgeDirect3D9 final : public IDirect3D9 if (ppReturnedDeviceInterface) *ppReturnedDeviceInterface = device; return hr; } + if (!g_liveBridgeDevices) g_patchOwnerThread = GetCurrentThreadId(); // The device creation thread is also used in unwrapped mode. Keep M1 // out of Present/Draw and apply it only once per creating thread. @@ -9793,13 +10011,17 @@ class BridgeDirect3D9 final : public IDirect3D9 // ProperShaders patches the same device the game renders with; the // render-state guard can only be installed once the device exists and // the ProperShaders module has been loaded. - StartProperShadersRenderStateGuardWatch(); - - if (ShouldWrapD3D9Device()) { - *ppReturnedDeviceInterface = new BridgeDirect3DDevice9(device); - } else { - OnUnwrappedDeviceCreated(device); - *ppReturnedDeviceInterface = device; + try { + if (ShouldWrapD3D9Device() || !OnUnwrappedDeviceCreated(device)) { + *ppReturnedDeviceInterface = new BridgeDirect3DDevice9(device); + } else { + *ppReturnedDeviceInterface = device; + } + } catch (...) { + for (auto& plugin : g_plugins) + SafePluginCall("OnReleaseDevice", plugin.onReleaseDevice, device); + device->Release(); + return E_OUTOFMEMORY; } return hr; } @@ -9811,6 +10033,9 @@ class BridgeDirect3D9 final : public IDirect3D9 extern "C" __declspec(dllexport) IDirect3D9* __stdcall Direct3DCreate9(UINT SDKVersion) { + if (!EnsureBridgeInitialized()) return nullptr; + std::lock_guard lifecycleLock(g_bridgeLifecycleMutex); + if (g_bridgeStopping.load()) return nullptr; Log("Direct3DCreate9 called SDKVersion={}", SDKVersion); EnsurePostFxPluginsLoaded(); @@ -9822,7 +10047,9 @@ extern "C" __declspec(dllexport) IDirect3D9* __stdcall Direct3DCreate9(UINT SDKV // The IDirect3D9 wrapper only intercepts creation-time calls and // costs nothing per frame; it must stay so CreateDevice can attach // the Vulkan host and the unwrapped support worker. - return new BridgeDirect3D9(result); + auto* wrapper = new (std::nothrow) BridgeDirect3D9(result); + if (!wrapper) result->Release(); + return wrapper; } Log("Direct3DCreate9: ProperShaders proxy returned null, falling back to system d3d9"); } @@ -9835,7 +10062,9 @@ extern "C" __declspec(dllexport) IDirect3D9* __stdcall Direct3DCreate9(UINT SDKV if (result) { // Always wrap the interface (creation-time only, no per-frame cost) // so CreateDevice interception keeps working in unwrapped mode. - return new BridgeDirect3D9(result); + auto* wrapper = new (std::nothrow) BridgeDirect3D9(result); + if (!wrapper) result->Release(); + return wrapper; } return result; } @@ -9845,6 +10074,7 @@ extern "C" __declspec(dllexport) IDirect3D9* __stdcall Direct3DCreate9(UINT SDKV extern "C" __declspec(dllexport) int __stdcall DebugSetLevel() { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_psProxy) { auto fn = (PFN_DebugSetLevel)GetProcAddress(g_psProxy, "DebugSetLevel"); if (fn) return fn(); @@ -9858,6 +10088,7 @@ extern "C" __declspec(dllexport) int __stdcall DebugSetLevel() extern "C" __declspec(dllexport) int __stdcall DebugSetMute() { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_psProxy) { auto fn = (PFN_DebugSetMute)GetProcAddress(g_psProxy, "DebugSetMute"); if (fn) return fn(); @@ -9871,6 +10102,7 @@ extern "C" __declspec(dllexport) int __stdcall DebugSetMute() extern "C" __declspec(dllexport) void* __stdcall Direct3DShaderValidatorCreate9() { + if (!EnsureBridgeInitialized()) return nullptr; if (g_psProxy) { auto fn = (PFN_Direct3DShaderValidatorCreate9)GetProcAddress(g_psProxy, "Direct3DShaderValidatorCreate9"); if (fn) return fn(); @@ -9884,6 +10116,7 @@ extern "C" __declspec(dllexport) void* __stdcall Direct3DShaderValidatorCreate9( extern "C" __declspec(dllexport) void* __stdcall PSGPError() { + if (!EnsureBridgeInitialized()) return nullptr; if (g_psProxy) { auto fn = (PFN_PSGPError)GetProcAddress(g_psProxy, "PSGPError"); if (fn) return fn(); @@ -9897,6 +10130,7 @@ extern "C" __declspec(dllexport) void* __stdcall PSGPError() extern "C" __declspec(dllexport) void* __stdcall PSGPSampleTexture() { + if (!EnsureBridgeInitialized()) return nullptr; if (g_psProxy) { auto fn = (PFN_PSGPSampleTexture)GetProcAddress(g_psProxy, "PSGPSampleTexture"); if (fn) return fn(); @@ -9910,6 +10144,7 @@ extern "C" __declspec(dllexport) void* __stdcall PSGPSampleTexture() extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory(REFIID riid, void** ppFactory) { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_realD3D9) { auto fn = reinterpret_cast(GetProcAddress(g_realD3D9, "CreateDXGIFactory")); if (fn) return fn(riid, ppFactory); @@ -9919,6 +10154,7 @@ extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory(REFIID riid extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory1(REFIID riid, void** ppFactory) { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_realD3D9) { auto fn = reinterpret_cast(GetProcAddress(g_realD3D9, "CreateDXGIFactory1")); if (fn) return fn(riid, ppFactory); @@ -9928,6 +10164,7 @@ extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory1(REFIID rii extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory2(UINT flags, REFIID riid, void** ppFactory) { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_realD3D9) { auto fn = reinterpret_cast(GetProcAddress(g_realD3D9, "CreateDXGIFactory2")); if (fn) return fn(flags, riid, ppFactory); @@ -9937,6 +10174,7 @@ extern "C" __declspec(dllexport) HRESULT __stdcall CreateDXGIFactory2(UINT flags extern "C" __declspec(dllexport) HRESULT __stdcall DXGIDeclareAdapterRemovalSupport() { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_realD3D9) { auto fn = reinterpret_cast( GetProcAddress(g_realD3D9, "DXGIDeclareAdapterRemovalSupport")); @@ -9947,6 +10185,7 @@ extern "C" __declspec(dllexport) HRESULT __stdcall DXGIDeclareAdapterRemovalSupp extern "C" __declspec(dllexport) HRESULT __stdcall DXGIGetDebugInterface1(UINT flags, REFIID riid, void** ppDebug) { + if (!EnsureBridgeInitialized()) return E_FAIL; if (g_realD3D9) { auto fn = reinterpret_cast( GetProcAddress(g_realD3D9, "DXGIGetDebugInterface1")); @@ -9982,48 +10221,102 @@ static void InstallSafeStub() } } -BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID reserved) +static BOOL CALLBACK InitializeBridgeOnce(PINIT_ONCE, PVOID, PVOID*) { - if (reason == DLL_PROCESS_ATTACH) { - g_selfModule = hModule; - DisableThreadLibraryCalls(hModule); - Log("BridgeD3D9: loaded mode={}", kBackendTraceBuild ? "backend-trace" : "root-proxy"); + HMODULE pinned = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(&EnsureBridgeInitialized), &pinned)) return FALSE; + g_bridgeStopEvent = CreateEventA(nullptr, TRUE, FALSE, nullptr); + if (!g_bridgeStopEvent) return FALSE; + Log("BridgeD3D9: loaded mode={}", kBackendTraceBuild ? "backend-trace" : "root-proxy"); - if (!kBackendTraceBuild) { - InstallSafeStub(); - } - LoadBridgeConfig(); - if (!kBackendTraceBuild) { - ApplyAffinityAndPriority("startup"); - } - if (!kBackendTraceBuild && g_affinityEnable && g_affinityReapply && g_affinityReapplyCount > 0) { - HANDLE thread = CreateThread(nullptr, 0, AffinityReapplyThread, nullptr, 0, nullptr); - if (thread) { - CloseHandle(thread); - } else { - Log("affinity: CreateThread failed err={}", GetLastError()); - } + if (!kBackendTraceBuild) { + InstallSafeStub(); + } + LoadBridgeConfig(); + if (!kBackendTraceBuild) { + ApplyAffinityAndPriority("startup"); + } + if (!kBackendTraceBuild && g_affinityEnable && g_affinityReapply && g_affinityReapplyCount > 0) { + HANDLE thread = StartBridgeWorker(AffinityReapplyThread, nullptr); + if (thread) { + // Handle retained for explicit shutdown. + } else { + Log("affinity: CreateThread failed err={}", GetLastError()); } + } + + g_realD3D9 = LoadBackendD3D9(); + if (g_realD3D9) { + g_real_Direct3DCreate9 = (PFN_Direct3DCreate9)GetProcAddress(g_realD3D9, "Direct3DCreate9"); + } - g_realD3D9 = LoadBackendD3D9(); - if (g_realD3D9) { - g_real_Direct3DCreate9 = (PFN_Direct3DCreate9)GetProcAddress(g_realD3D9, "Direct3DCreate9"); + if (!kBackendTraceBuild) { + g_psProxy = LoadPSProxy(); + if (g_psProxy) { + PatchPSProxyBackend(); + g_ps_Direct3DCreate9 = (PFN_Direct3DCreate9)GetProcAddress(g_psProxy, "Direct3DCreate9"); + Log("BridgeD3D9: primary proxy={} Direct3DCreate9=0x{:08X}", + g_primaryProxyName[0] ? g_primaryProxyName : "(unnamed)", reinterpret_cast(g_ps_Direct3DCreate9)); } + } + return TRUE; +} - if (!kBackendTraceBuild) { - g_psProxy = LoadPSProxy(); - if (g_psProxy) { - PatchPSProxyBackend(); - g_ps_Direct3DCreate9 = (PFN_Direct3DCreate9)GetProcAddress(g_psProxy, "Direct3DCreate9"); - Log("BridgeD3D9: primary proxy={} Direct3DCreate9=0x{:08X}", - g_primaryProxyName[0] ? g_primaryProxyName : "(unnamed)", reinterpret_cast(g_ps_Direct3DCreate9)); - } +static bool EnsureBridgeInitialized() +{ + return !g_bridgeStopping.load() && + InitOnceExecuteOnce(&g_bridgeInitOnce, InitializeBridgeOnce, nullptr, nullptr); +} + +static HRESULT ShutdownBridgeRuntime() +{ + std::unique_lock lock(g_bridgeLifecycleMutex); + if (g_bridgeShutdownComplete) return S_FALSE; + if (g_liveBridgeDevices) return D3DERR_INVALIDCALL; + if (g_bridgeShutdownRunning.exchange(true)) return E_PENDING; + struct ShutdownAttempt { + ~ShutdownAttempt() { g_bridgeShutdownRunning.store(false); } + } attempt; + g_bridgeStopping.store(true); + BridgeMeasurement::RuntimeShutdown(); + SetEvent(g_bridgeStopEvent); + lock.unlock(); + // No loader-lock waits. Module pinning keeps any cached hook entry mapped. + std::array workers; + { + std::lock_guard workersLock(g_bridgeWorkerMutex); + workers = g_bridgeWorkers; + } + // A support worker can be finishing a failed child-worker admission. Do + // not hold the admission mutex while waiting for that worker to exit. + for (auto worker : workers) + if (worker && WaitForSingleObject(worker, 30000) != WAIT_OBJECT_0) return E_FAIL; + { + std::lock_guard workersLock(g_bridgeWorkerMutex); + for (auto& worker : g_bridgeWorkers) { + if (worker) CloseHandle(worker); + worker = nullptr; } - } else if (reason == DLL_PROCESS_DETACH && reserved == nullptr) { - RestoreProperShadersCreateEffectHook(); - RestoreProperShadersOptimizationPatches(); - ShutdownProperShadersStateRing(); - ShutdownPostFxPlugins(); + } + lock.lock(); + bool restored = RestoreProperShadersCreateEffectHook(); + restored = ShutdownProperShadersRenderStateGuards() && restored; + restored = RestoreProperShadersOptimizationPatches() && restored; + restored = RestoreProperShadersCallPatches() && restored; + if (!restored) return E_FAIL; + ShutdownProperShadersStateRing(); + ShutdownPostFxPlugins(); + g_bridgeShutdownComplete = true; + Log("postfx: explicit shutdown complete; workers stopped"); + return S_OK; +} + +BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) +{ + if (reason == DLL_PROCESS_ATTACH) { + g_selfModule = hModule; + DisableThreadLibraryCalls(hModule); } return TRUE; } diff --git a/src/bridge/legacy/BridgeD3D9.ini b/src/bridge/legacy/BridgeD3D9.ini index 0b7e939..1b00a1d 100644 --- a/src/bridge/legacy/BridgeD3D9.ini +++ b/src/bridge/legacy/BridgeD3D9.ini @@ -68,6 +68,15 @@ TriggerVirtualKey=120 CaptureFrames=120 SampleEveryDraws=64 +[Measurement] +; One F8/F7 capture: baseline -> CPU sampling -> state/journal attribution. +Enable=1 +ShowHud=1 +CpuHotspots=1 +JournalAttribution=1 +BaselineMs=5000 +SlowFrameMs=50 + [CPUHotspotProfile] ; Press F8 in the slow scene to save a screenshot and sample the GTA render/main ; thread. The standalone F9 callsite profiler remains independent by default. @@ -95,7 +104,8 @@ SkipDuplicateMatrices=1 ; Skip byte-identical vectors and float arrays inside the active Effect transaction. SkipDuplicateParameters=1 ; Quarantined after live captures showed geometry and sprite state leaking into -; later draws. Production builds ignore this key even if it is set to 1. +; later draws. Production always uses effective=0, including when this key is +; missing or an older profile requests 1. Runtime logs requested/effective values. DirectConstants=0 ; Quarantined after repeated live captures showed vertex/material state leaking ; between RenderWare atomics. Production builds ignore these three keys even if @@ -131,11 +141,9 @@ TestNoSaveState=0 Enable=1 [ProperShadersStateRing] -; Migrated from FLACompatBridge.ini EnableProperShadersStateRingTrace. Records -; the alpha render-state transitions observed by the guard into a 2048-entry -; ring and appends it to scripts\BridgeD3D9.propershaders-state-ring.log when -; the trigger key is pressed. Off by default: the ring records nothing while -; disabled. +; F10 starts about one second of detailed alpha/device state capture, then +; exports scripts\BridgeD3D9.propershaders-state-ring.log. Idle play makes no +; full StateRing snapshots. Required Fading state correction is independent. Enable=0 TriggerVirtualKey=121 diff --git a/src/bridge/legacy/BridgeD3D9.vcxproj b/src/bridge/legacy/BridgeD3D9.vcxproj index 40c4a18..5d112a7 100644 --- a/src/bridge/legacy/BridgeD3D9.vcxproj +++ b/src/bridge/legacy/BridgeD3D9.vcxproj @@ -44,6 +44,8 @@ + + diff --git a/src/bridge/legacy/BridgeD3D9BackendTrace.vcxproj b/src/bridge/legacy/BridgeD3D9BackendTrace.vcxproj index 932092f..d863377 100644 --- a/src/bridge/legacy/BridgeD3D9BackendTrace.vcxproj +++ b/src/bridge/legacy/BridgeD3D9BackendTrace.vcxproj @@ -41,6 +41,8 @@ + + diff --git a/src/bridge/legacy/BridgeLog.h b/src/bridge/legacy/BridgeLog.h new file mode 100644 index 0000000..f34c90b --- /dev/null +++ b/src/bridge/legacy/BridgeLog.h @@ -0,0 +1,28 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace BridgeDiagnostics { +template +void BestEffort(Operation&& operation) noexcept +{ + try { operation(); } catch (...) { } +} +template +void Write(FILE* output, std::format_string fmt, Args&&... args) noexcept +{ + if (!output) return; + BestEffort([&] { + SYSTEMTIME time; + GetLocalTime(&time); + std::print(output, "[{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] ", + time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds); + std::vprint_nonunicode(output, fmt.get(), std::make_format_args(args...)); + fputc('\n', output); + fflush(output); + }); +} +} diff --git a/src/bridge/legacy/BridgeMeasurement.h b/src/bridge/legacy/BridgeMeasurement.h new file mode 100644 index 0000000..1b07a21 --- /dev/null +++ b/src/bridge/legacy/BridgeMeasurement.h @@ -0,0 +1,234 @@ +#pragma once + +#include +#include +#include +#include + +// Pure measurement policy. Timestamps are monotonic nanoseconds; recorded +// intervals describe successful Presents, never CPU/GPU execution duration. +namespace BridgeMeasurement { + +enum class Phase : std::uint32_t { + Ready, Baseline, Cpu, CpuFinishing, Attribution, Complete, Interrupted, Error, + AttributionFinishing +}; +enum class Action { None, StartCpu, StartAttribution, FinishAttribution, ReportError }; +inline const char* PhaseName(Phase phase) { + switch (phase) { + case Phase::Ready: return "Ready"; + case Phase::Baseline: return "Baseline"; + case Phase::Cpu: return "CPU sampling (overhead)"; + case Phase::CpuFinishing: return "Processing CPU samples"; + case Phase::Attribution: return "State attribution (overhead)"; + case Phase::Complete: return "Complete"; + case Phase::Interrupted: return "Interrupted"; + case Phase::Error: return "Error"; + case Phase::AttributionFinishing: return "Waiting for final transactions"; + } + return "Unknown"; +} + +struct FrameStats { + std::uint64_t presents = 0, failedPresents = 0, invalidTimes = 0; + std::uint64_t startNs = 0, endNs = 0, lastPresentNs = 0, lastObservedNs = 0; + std::uint64_t totalIntervalNs = 0, maxIntervalNs = 0; + void Begin(std::uint64_t now) { + *this = {}; + startNs = endNs = lastPresentNs = lastObservedNs = now; + } + bool Observe(std::uint64_t now, bool success) { + if (now < lastObservedNs) { ++invalidTimes; return false; } + lastObservedNs = endNs = now; + if (!success) { ++failedPresents; return false; } + const auto interval = now - lastPresentNs; + totalIntervalNs += interval; + maxIntervalNs = std::max(maxIntervalNs, interval); + lastPresentNs = now; + ++presents; + return true; + } + void Finish(std::uint64_t now) { endNs = std::max(endNs, now); } + std::uint64_t elapsedNs() const { return endNs >= startNs ? endNs - startNs : 0; } + double fps() const { return elapsedNs() ? 1e9 * double(presents) / double(elapsedNs()) : 0.0; } + double averageIntervalNs() const { return presents ? double(totalIntervalNs) / double(presents) : 0.0; } +}; + +struct FrameRecord { + std::uint64_t beginNs = 0, endNs = 0, frameId = 0; + Phase phase = Phase::Ready; +}; +class FrameTrace { +public: + static constexpr std::size_t Capacity = 4096; + void Clear() { m_count = m_dropped = 0; } + void Add(std::uint64_t begin, std::uint64_t end, std::uint64_t id, Phase phase) { + if (end <= begin) return; + if (m_count == Capacity) { ++m_dropped; return; } + m_rows[m_count++] = {begin, end, id, phase}; + } + const FrameRecord* Find(std::uint64_t time) const { + const auto first = m_rows.begin(), last = first + m_count; + const auto it = std::upper_bound(first, last, time, + [](std::uint64_t value, const FrameRecord& row) { return value < row.beginNs; }); + if (it == first) return nullptr; + const auto& row = *(it - 1); + return time >= row.beginNs && time < row.endNs ? &row : nullptr; + } + bool IsSlow(std::uint64_t time, std::uint64_t thresholdNs) const { + const auto* row = Find(time); + return row && row->endNs - row->beginNs > thresholdNs; + } + const FrameRecord* data() const { return m_rows.data(); } + std::size_t size() const { return m_count; } + std::uint64_t dropped() const { return m_dropped; } +private: + std::array m_rows{}; + std::size_t m_count = 0; + std::uint64_t m_dropped = 0; +}; + +struct Options { + std::uint32_t baselineMs = 5000, cpuMs = 10000, attributionMs = 3000, slowFrameMs = 50; +}; +class Controller { +public: + explicit Controller(Options options = {}) : m_options(options) { } + void Configure(Options options) { if (!active()) m_options = options; } + void SetAvailable(bool cpu, bool attribution) { m_cpuAvailable = cpu; m_attributionAvailable = attribution; } + bool Key(bool f7, bool f8, std::uint64_t now) { + const bool down = f7 || f8; + const bool edge = down && !m_keyDown; + m_keyDown = down; + if (!edge || active()) return false; + ++m_capture; + if (!m_capture) ++m_capture; + m_stats = {}; + m_trace.Clear(); + m_frame = 0; + m_cpuDone = m_cpuFailed = false; + m_unavailable = (!m_cpuAvailable ? 2u : 0u) | (!m_attributionAvailable ? 4u : 0u); + Enter(Phase::Baseline, now); + return true; + } + void SuppressKeys(bool f7, bool f8) { m_keyDown = f7 || f8; } + Action Present(std::uint64_t now, bool success) { + if (!active()) return Action::None; + if ((m_phase == Phase::Cpu || m_phase == Phase::CpuFinishing) && m_cpuFailed) { + m_phase = Phase::Error; + return Action::ReportError; + } + if (m_phase == Phase::CpuFinishing) { + if (!m_cpuDone) return Action::None; + return BeginAttribution(now); + } + const unsigned index = phaseIndex(); + const auto before = m_stats[index].lastPresentNs; + if (m_stats[index].Observe(now, success)) + m_trace.Add(before, now, ++m_frame, m_phase); + if (m_stats[index].elapsedNs() < std::uint64_t(durationMs()) * 1'000'000ull) + return Action::None; + m_stats[index].Finish(now); + if (m_phase == Phase::Baseline) { + if (!m_cpuAvailable) return BeginAttribution(now); + Enter(Phase::Cpu, now); + return Action::StartCpu; + } + if (m_phase == Phase::Cpu) { + m_phase = Phase::CpuFinishing; + if (m_cpuDone) return BeginAttribution(now); + return Action::None; + } + m_phase = Phase::Complete; + return Action::FinishAttribution; + } + void CpuFinished(bool success) { m_cpuDone = success; m_cpuFailed = !success; } + void Interrupt() { if (active()) m_phase = Phase::Interrupted; } + Phase phase() const { return m_phase; } + bool active() const { + return m_phase == Phase::Baseline || m_phase == Phase::Cpu || + m_phase == Phase::CpuFinishing || m_phase == Phase::Attribution; + } + std::uint64_t captureId() const { return m_capture; } + std::uint32_t unavailableMask() const { return m_unavailable; } + const FrameStats& stats(unsigned index) const { return m_stats[std::min(index, 2u)]; } + const FrameTrace& trace() const { return m_trace; } + std::uint32_t durationMs() const { + if (m_phase == Phase::Baseline) return m_options.baselineMs; + if (m_phase == Phase::Cpu) return m_options.cpuMs; + if (m_phase == Phase::Attribution) return m_options.attributionMs; + return 0; + } + unsigned phaseIndex() const { + return m_phase == Phase::Baseline ? 0u : (m_phase == Phase::Attribution ? 2u : 1u); + } +private: + void Enter(Phase phase, std::uint64_t now) { + m_phase = phase; + m_stats[phaseIndex()].Begin(now); + } + Action BeginAttribution(std::uint64_t now) { + if (!m_attributionAvailable) { m_phase = Phase::Complete; return Action::FinishAttribution; } + Enter(Phase::Attribution, now); + return Action::StartAttribution; + } + Options m_options; + std::array m_stats{}; + FrameTrace m_trace; + Phase m_phase = Phase::Ready; + std::uint64_t m_capture = 0, m_frame = 0; + std::uint32_t m_unavailable = 0; + bool m_keyDown = false, m_cpuAvailable = true, m_attributionAvailable = true; + bool m_cpuDone = false, m_cpuFailed = false; +}; + +class Jitter { +public: + explicit Jitter(std::uint32_t seed) : m_state(seed ? seed : 0x9e3779b9u) { } + std::uint32_t Next(std::uint32_t intervalMs) { + if (intervalMs <= 1) return 1; + m_state ^= m_state << 13; + m_state ^= m_state >> 17; + m_state ^= m_state << 5; + const auto spread = std::max(1u, intervalMs / 4u); + return std::uint32_t(std::max(1, + std::int64_t(intervalMs) + std::int64_t(m_state % (2u * spread + 1u)) - spread)); + } +private: + std::uint32_t m_state; +}; + +enum class ChainQuality { Unavailable, FrameChain, Partial, Invalid, Truncated }; +struct CapturedChain { + std::array addresses{}; + std::uint32_t count = 0; + ChainQuality quality = ChainQuality::Unavailable; +}; +// Structural frame-chain validation on a bounded snapshot, not a full x86 +// unwinder. Optimized/FPO frames may be missing and must remain unresolved. +template +CapturedChain WalkCapturedFrames(std::uintptr_t stackBase, const void* bytes, + std::size_t byteCount, std::uintptr_t frame, Executable executable) { + CapturedChain result; + if (!bytes || byteCount < 8 || !frame) return result; + for (std::size_t i = 0; i < result.addresses.size(); ++i) { + if (frame < stackBase || frame - stackBase > byteCount - 8 || (frame & 3u)) { + result.quality = result.count ? ChainQuality::Partial : ChainQuality::Unavailable; + return result; + } + std::uint32_t pair[2]{}; + std::memcpy(pair, static_cast(bytes) + (frame - stackBase), sizeof(pair)); + if (!pair[1] || !executable(std::uintptr_t(pair[1]) - 1u)) { + result.quality = ChainQuality::Invalid; + return result; + } + result.addresses[result.count++] = pair[1]; + if (!pair[0]) { result.quality = ChainQuality::FrameChain; return result; } + if (pair[0] <= frame) { result.quality = ChainQuality::Invalid; return result; } + frame = pair[0]; + } + result.quality = ChainQuality::Truncated; + return result; +} + +} // namespace BridgeMeasurement diff --git a/src/bridge/legacy/BridgeMeasurementRuntime.cpp b/src/bridge/legacy/BridgeMeasurementRuntime.cpp new file mode 100644 index 0000000..72f8126 --- /dev/null +++ b/src/bridge/legacy/BridgeMeasurementRuntime.cpp @@ -0,0 +1,833 @@ +#include "BridgeMeasurementRuntime.h" +#include "MeasurementSymbols.h" +#include "ProperShadersStateJournal.h" +#include "../../../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" +#include "../../../backend/dxvk/src/d3d9/d3d9_bridge_thread_stats.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace BridgeMeasurement { +namespace { +constexpr std::size_t MaxSamples = 16384; +constexpr std::size_t StackBytes = 4096; +struct Range { std::uintptr_t begin, end; }; +struct RawSample { + std::uint64_t timeNs = 0, suspendedNs = 0; + std::uintptr_t ip = 0; + CapturedChain chain; + std::array candidates{}; +}; +struct CpuFunction { + SymbolResult symbol; + std::string name; + std::uint64_t samples = 0; +}; +struct CpuResult { + std::uint64_t id = 0, elapsedNs = 0, missed = 0, dropped = 0, suspendedNs = 0; + std::uint64_t maxSuspendedNs = 0, cycles = 0, presents = 0, startedNs = 0; + std::uint64_t validChains = 0, partialChains = 0, invalidChains = 0; + std::vector samples; + std::vector modules; + std::vector functions; + std::unordered_map symbols; + std::string error; +}; +struct CpuJob { + RuntimeConfig config; + std::string directory; + DWORD thread = 0; + std::uint64_t id = 0; + std::atomic cancel{false}; +}; +struct ReportJob { + RuntimeConfig config; + std::string path; + RuntimeSnapshot snapshot; + FrameTrace trace; + JournalAttributionSummary journal; + D3D9BridgeAuditSnapshot audit{}; + std::array threadBegin{}, threadEnd{}; + std::shared_ptr cpu; + Phase finalPhase = Phase::Complete; +}; + +std::mutex g_mutex; +std::atomic g_enabled{false}; +std::atomic g_workers{0}; +struct WorkerExit { ~WorkerExit() { g_workers.fetch_sub(1, std::memory_order_release); } }; +std::atomic g_successfulPresents{0}; +RuntimeConfig g_config; +std::string g_directory; +Controller g_controller; +IDirect3DDevice9* g_device = nullptr; // borrowed; cleared before final release +ID3D9BridgeDiagnostics* g_diagnostics = nullptr; // likewise never retains device +ID3D9BridgeThreadStats* g_threadStats = nullptr; // borrowed, same device lifetime +std::array g_threadBegin{}, g_threadEnd{}; +int g_threadPhase = -1; +DWORD g_owner = 0; +std::shared_ptr g_cpuJob; +std::shared_ptr g_cpuResult; +bool g_cpuReady = false, g_journalStarted = false, g_auditStarted = false; +bool g_reportFailed = false, g_reportStarted = false, g_reportInterrupted = false; +bool g_journalDrainPending = false; +std::uint64_t g_nextCapture = 0, g_capture = 0, g_lastHudNs = 0; +std::uint32_t g_unavailable = 0; +FrameStats g_live; +bool g_liveStarted = false; +double g_liveFps = 0, g_liveMeanMs = 0, g_liveMaxMs = 0; +JournalAttributionSummary g_journal; +D3D9BridgeAuditSnapshot g_audit{}; +char g_reportPath[MAX_PATH]{}, g_error[128]{}; + +std::uint64_t NowNs() { + static const auto frequency = [] { LARGE_INTEGER value{}; QueryPerformanceFrequency(&value); return value.QuadPart; }(); + LARGE_INTEGER ticks{}; + QueryPerformanceCounter(&ticks); + if (frequency <= 0 || ticks.QuadPart < 0) return 0; + const auto t = std::uint64_t(ticks.QuadPart), f = std::uint64_t(frequency); + return (t / f) * 1'000'000'000ull + ((t % f) * 1'000'000'000ull) / f; +} +void CopyText(char* out, std::size_t size, const std::string& text) { + if (!size) return; + const auto count = (std::min)(size - 1, text.size()); + std::memcpy(out, text.data(), count); out[count] = 0; +} +void Log(const RuntimeConfig& config, const std::string& text) { + if (config.log) config.log(text.c_str()); +} +std::string Quote(const std::string& value) { + std::string result = "\""; + for (char c : value) { + if (c == '\\' || c == '"') result += '\\'; + if (c == '\r') result += "\\r"; + else if (c == '\n') result += "\\n"; + else result += c; + } + return result + '"'; +} +std::string Basename(const std::string& path) { + const auto slash = path.find_last_of("/\\"); + return slash == std::string::npos ? path : path.substr(slash + 1); +} +std::string Lower(std::string text) { + for (auto& c : text) if (c >= 'A' && c <= 'Z') c = char(c - 'A' + 'a'); + return text; +} +const RuntimeCpuModule* FindModule(const std::vector& modules, std::uintptr_t ip) { + for (const auto& module : modules) + if (ip >= module.base && ip - module.base < module.size) return &module; + return nullptr; +} +struct PeCopy { + DWORD timestamp = 0; + unsigned count = 0; + IMAGE_SECTION_HEADER sections[96]{}; +}; +bool CopyPe(std::uintptr_t base, PeCopy* copy) { + __try { + const auto* dos = reinterpret_cast(base); + if (dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew < 0 || dos->e_lfanew > 0x100000) return false; + const auto* nt = reinterpret_cast(base + dos->e_lfanew); + if (nt->Signature != IMAGE_NT_SIGNATURE || nt->FileHeader.NumberOfSections > 96) return false; + copy->timestamp = nt->FileHeader.TimeDateStamp; + copy->count = nt->FileHeader.NumberOfSections; + std::memcpy(copy->sections, IMAGE_FIRST_SECTION(nt), copy->count * sizeof(IMAGE_SECTION_HEADER)); + return true; + } __except(EXCEPTION_EXECUTE_HANDLER) { return false; } +} +void CollectModules(std::vector& modules, std::vector& ranges) { + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); + if (snapshot == INVALID_HANDLE_VALUE) return; + MODULEENTRY32 entry{}; entry.dwSize = sizeof(entry); + if (Module32First(snapshot, &entry)) do { + RuntimeCpuModule module; + module.base = reinterpret_cast(entry.modBaseAddr); + module.size = entry.modBaseSize; + module.path = entry.szExePath; + PeCopy pe{}; + if (CopyPe(module.base, &pe)) { + module.timestamp = pe.timestamp; + for (unsigned i = 0; i < pe.count; ++i) { + const auto& section = pe.sections[i]; + const auto size = (std::max)(section.Misc.VirtualSize, section.SizeOfRawData); + if ((section.Characteristics & IMAGE_SCN_MEM_EXECUTE) && section.VirtualAddress < module.size) + ranges.push_back({module.base + section.VirtualAddress, + module.base + section.VirtualAddress + (std::min)(size, module.size - section.VirtualAddress)}); + } + } + modules.push_back(std::move(module)); + } while (Module32Next(snapshot, &entry)); + CloseHandle(snapshot); + std::sort(ranges.begin(), ranges.end(), [](const Range& a, const Range& b) { return a.begin < b.begin; }); +} +bool Executable(const std::vector& ranges, std::uintptr_t address) { + auto it = std::upper_bound(ranges.begin(), ranges.end(), address, + [](std::uintptr_t value, const Range& range) { return value < range.begin; }); + return it != ranges.begin() && address < (--it)->end; +} +std::size_t CopyStack(std::uintptr_t pointer, void* bytes) { + MEMORY_BASIC_INFORMATION info{}; + if (!pointer || !VirtualQuery(reinterpret_cast(pointer), &info, sizeof(info)) || + info.State != MEM_COMMIT || (info.Protect & (PAGE_NOACCESS | PAGE_GUARD))) return 0; + const auto base = reinterpret_cast(info.BaseAddress); + if (pointer < base || pointer - base >= info.RegionSize) return 0; + const auto size = (std::min)(StackBytes, info.RegionSize - (pointer - base)); + __try { std::memcpy(bytes, reinterpret_cast(pointer), size); return size; } + __except(EXCEPTION_EXECUTE_HANDLER) { return 0; } +} +bool Stopping(const CpuJob& job) { + return job.cancel.load(std::memory_order_relaxed) || + (job.config.stopEvent && WaitForSingleObject(job.config.stopEvent, 0) == WAIT_OBJECT_0); +} +void ResolveCpu(CpuResult& result) { + MeasurementSymbolResolver resolver; + for (const auto& module : result.modules) { + const auto name = Lower(Basename(module.path)); + if (name == "d3d9.dll") resolver.LoadModule(module.base, module.path, module.path + ".rsym"); + else if (name == "ntdll.dll" || name == "kernelbase.dll" || name == "kernel32.dll" || name == "win32u.dll") + resolver.LoadExports(module.base, module.path); + } + std::unordered_map ips; + for (const auto& sample : result.samples) { + ++ips[sample.ip]; + for (auto caller : sample.chain.addresses) + if (caller && !result.symbols.count(caller)) result.symbols.emplace(caller, resolver.Resolve(caller)); + for (auto caller : sample.candidates) + if (caller && !result.symbols.count(caller)) result.symbols.emplace(caller, resolver.Resolve(caller)); + } + std::map, std::size_t> groups; + for (const auto& [ip, count] : ips) { + auto symbol = resolver.Resolve(ip); + if (symbol.quality == SymbolQuality::ModuleNotFound) if (const auto* module = FindModule(result.modules, ip)) { + symbol.modulePath = module->path; symbol.moduleBase = module->base; + symbol.rva = std::uint32_t(ip - module->base); symbol.quality = SymbolQuality::Missing; + } + if (symbol.quality == SymbolQuality::ModuleNotFound) { symbol.modulePath = "private"; symbol.rva = std::uint32_t(ip); } + result.symbols[ip] = symbol; + const bool named = !symbol.name.empty() || !symbol.aliases.empty(); + const auto key = std::make_pair(symbol.moduleBase, + named ? ip - symbol.offset : ip); + auto [it, inserted] = groups.emplace(key, result.functions.size()); + if (inserted) { + std::string name = symbol.name; + for (const auto& alias : symbol.aliases) { if (!name.empty()) name += '|'; name += alias; } + if (name.empty()) { + char rva[32]{}; std::snprintf(rva, sizeof(rva), "+0x%08X", symbol.rva); + name = Basename(symbol.modulePath) + rva; + } + result.functions.push_back({symbol, std::move(name), count}); + } else result.functions[it->second].samples += count; + } + std::sort(result.functions.begin(), result.functions.end(), + [](const CpuFunction& a, const CpuFunction& b) { return a.samples > b.samples; }); +} +void WriteCpuLog(const CpuJob& job, const CpuResult& result) { + const auto path = (std::filesystem::path(job.directory) / "scripts/BridgeD3D9.cpuhotspots.log").string(); + std::filesystem::create_directories(std::filesystem::path(path).parent_path()); + FILE* file = nullptr; + if (fopen_s(&file, path.c_str(), "a") || !file) throw std::runtime_error("cannot write CPU hotspot report"); + SYSTEMTIME utc{}; GetSystemTime(&utc); + const double fps = result.elapsedNs ? double(result.presents) * 1e9 / double(result.elapsedNs) : 0; + std::fprintf(file, "# capture=%llu begin=%04u-%02u-%02uT%02u:%02u:%02u.%03uZ targetThread=%lu durationMs=%u intervalMs=%u elapsedMs=%llu frames=%llu fps=%.2f presents=%llu presentRate=%.2f frameCounterValid=0 presentCounterValid=1 samples=%zu missed=%llu threadCycles=%llu source=unified suspendedNs=%llu dropped=%llu\n", + result.id, utc.wYear, utc.wMonth, utc.wDay, utc.wHour, utc.wMinute, utc.wSecond, utc.wMilliseconds, + job.thread, job.config.timing.cpuMs, job.config.intervalMs, result.elapsedNs / 1'000'000, + result.presents, fps, result.presents, fps, result.samples.size(), result.missed, + result.cycles, result.suspendedNs, result.dropped); + RuntimeCpuReport view; + view.captureId = result.id; view.elapsedMs = result.elapsedNs / 1'000'000; + view.presents = result.presents; view.samples = result.samples.size(); view.missed = result.missed; + view.fps = fps; view.modules = result.modules; + std::map moduleCounts, ipCounts; + for (const auto& sample : result.samples) { + ++ipCounts[sample.ip]; + const auto* module = FindModule(result.modules, sample.ip); + ++moduleCounts[module ? module->base : 0]; + if (sample.chain.quality == ChainQuality::FrameChain) { + RuntimeCpuStack stack; stack.ip = sample.ip; stack.quality = sample.chain.quality; stack.samples = 1; + for (unsigned i = 0; i < 3 && i < sample.chain.count; ++i) stack.callers[i] = sample.chain.addresses[i]; + view.stacks.push_back(stack); + } + } + std::vector> ordered(moduleCounts.begin(), moduleCounts.end()); + std::sort(ordered.begin(), ordered.end(), [](const auto& a, const auto& b) {return a.second > b.second;}); + unsigned rank = 0; + for (const auto& [base, count] : ordered) { + const auto* module = FindModule(result.modules, base); + std::fprintf(file, "moduleRank=%u samples=%llu percent=%.2f module=%s base=%08llX path=%s\n", + ++rank, count, result.samples.empty() ? 0.0 : 100.0 * double(count) / double(result.samples.size()), + module ? Basename(module->path).c_str() : "private", std::uint64_t(base), Quote(module ? module->path : "private").c_str()); + } + ordered.assign(ipCounts.begin(), ipCounts.end()); + std::sort(ordered.begin(), ordered.end(), [](const auto& a, const auto& b) {return a.second > b.second;}); + rank = 0; + for (const auto& [ip, count] : ordered) { + const auto* module = FindModule(result.modules, ip); + const auto it = result.symbols.find(ip); + std::fprintf(file, "rank=%u samples=%llu percent=%.2f address=%08llX module=%s base=%08llX rva=0x%08llX quality=%s\n", + ++rank, count, result.samples.empty() ? 0.0 : 100.0 * double(count) / double(result.samples.size()), std::uint64_t(ip), + module ? Basename(module->path).c_str() : "private", module ? std::uint64_t(module->base) : 0, + module ? std::uint64_t(ip - module->base) : 0, it != result.symbols.end() ? SymbolQualityName(it->second.quality) : "missing"); + view.addresses.push_back({ip, count}); + } + std::fclose(file); + if (job.config.appendCpuReport) job.config.appendCpuReport(path.c_str(), view); + file = nullptr; + if (!fopen_s(&file, path.c_str(), "a") && file) { + std::fprintf(file, "# capture=%llu end\n", result.id); std::fclose(file); + } +} + +DWORD WINAPI CpuWorker(void* opaque) { + WorkerExit activity; + std::unique_ptr> holder(static_cast*>(opaque)); + const auto job = *holder; + std::shared_ptr result; + HANDLE thread = nullptr; + try { + result = std::make_shared(); result->id = job->id; + result->samples.reserve(MaxSamples); + std::vector ranges; + CollectModules(result->modules, ranges); + thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, job->thread); + if (!thread || GetProcessIdOfThread(thread) != GetCurrentProcessId()) throw std::runtime_error("cannot open capture owner thread"); + ULONG64 startCycles = 0, endCycles = 0; + QueryThreadCycleTime(thread, &startCycles); + const auto startPresents = g_successfulPresents.load(std::memory_order_relaxed); + result->startedNs = NowNs(); + Jitter jitter(DWORD(job->id) ^ job->thread); + std::array stack{}; + while (!Stopping(*job) && NowNs() - result->startedNs < std::uint64_t(job->config.timing.cpuMs) * 1'000'000ull) { + const DWORD delay = jitter.Next(job->config.intervalMs); + if (job->config.stopEvent) { + if (WaitForSingleObject(job->config.stopEvent, delay) != WAIT_TIMEOUT) break; + } else Sleep(delay); + if (Stopping(*job)) break; + const auto pauseStart = NowNs(); + if (SuspendThread(thread) == DWORD(-1)) { ++result->missed; continue; } + CONTEXT context{}; context.ContextFlags = CONTEXT_CONTROL; + const bool got = GetThreadContext(thread, &context) != FALSE; + RawSample sample; sample.timeNs = NowNs(); + std::uintptr_t sp = 0, bp = 0; +#if defined(_M_IX86) + sample.ip = context.Eip; sp = context.Esp; bp = context.Ebp; +#elif defined(_M_X64) + sample.ip = context.Rip; sp = context.Rsp; bp = context.Rbp; +#endif + const auto copied = got ? CopyStack(sp, stack.data()) : 0; + const auto resumed = ResumeThread(thread); + const auto pauseEnd = NowNs(); + sample.suspendedNs = pauseEnd >= pauseStart ? pauseEnd - pauseStart : 0; + result->suspendedNs += sample.suspendedNs; + result->maxSuspendedNs = (std::max)(result->maxSuspendedNs, sample.suspendedNs); + if (!got || !sample.ip || resumed == DWORD(-1)) { ++result->missed; continue; } + // All parsing and aggregation happens after ResumeThread. The structural + // chain has its own quality; candidate executable words stay heuristic. + sample.chain = WalkCapturedFrames(sp, stack.data(), copied, bp, + [&](std::uintptr_t address) { return Executable(ranges, address); }); + unsigned candidates = 0; + for (unsigned i = 0; i < copied / 4 && i < 64 && candidates < 3; ++i) { + const auto address = std::uintptr_t(stack[i]); + if (address != sample.ip && Executable(ranges, address)) sample.candidates[candidates++] = address; + } + if (sample.chain.quality == ChainQuality::FrameChain) ++result->validChains; + else if (sample.chain.quality == ChainQuality::Partial || sample.chain.quality == ChainQuality::Truncated) ++result->partialChains; + else ++result->invalidChains; + if (result->samples.size() < MaxSamples) result->samples.push_back(sample); + else ++result->dropped; + } + result->elapsedNs = NowNs() - result->startedNs; + result->presents = g_successfulPresents.load(std::memory_order_relaxed) - startPresents; + QueryThreadCycleTime(thread, &endCycles); + result->cycles = endCycles >= startCycles ? endCycles - startCycles : 0; + CloseHandle(thread); thread = nullptr; + if (Stopping(*job)) throw std::runtime_error("CPU capture interrupted"); + if (result->samples.empty()) throw std::runtime_error("no valid CPU samples"); + ResolveCpu(*result); + if (Stopping(*job)) throw std::runtime_error("CPU capture interrupted"); + WriteCpuLog(*job, *result); + } catch (const std::exception& e) { if (result) result->error = e.what(); } + catch (...) { if (result) result->error = "CPU capture failed"; } + if (thread) CloseHandle(thread); + { + std::lock_guard lock(g_mutex); + if (g_cpuJob == job && g_capture == job->id) { + g_cpuResult = result; + g_cpuReady = true; + if (!result) CopyText(g_error, sizeof(g_error), "CPU result allocation failed"); + else if (!result->error.empty() && g_controller.active()) CopyText(g_error, sizeof(g_error), result->error); + } + } + return 0; +} + +const char* AuditName(unsigned i) { + static constexpr const char* names[] = {"renderState", "samplerState", "textureStageState", "texture", + "vertexShader", "pixelShader", "vertexFloatConstants", "vertexIntConstants", "vertexBoolConstants", + "pixelFloatConstants", "pixelIntConstants", "pixelBoolConstants", "stateBlockCreate", + "stateBlockBeginRecord", "stateBlockEndRecord", "stateBlockCapture", "stateBlockApply"}; + return i < std::size(names) ? names[i] : "unknown"; +} +void FillSnapshotLocked(RuntimeSnapshot& snapshot) { + snapshot = {}; + snapshot.phase = g_reportFailed ? Phase::Error : g_reportInterrupted ? Phase::Interrupted : g_controller.phase(); + if (g_journalDrainPending && snapshot.phase == Phase::Complete) snapshot.phase = Phase::AttributionFinishing; + snapshot.captureId = g_capture; + snapshot.unavailableMask = g_unavailable | g_controller.unavailableMask(); + for (unsigned i = 0; i < 3; ++i) snapshot.phaseStats[i] = g_controller.stats(i); + snapshot.frameTraceDropped = g_controller.trace().dropped(); + snapshot.workerBusy = g_workers.load(std::memory_order_acquire) != 0 || g_journalDrainPending; + snapshot.hudRequested = g_config.hud; + snapshot.hudIntegrationAvailable = g_diagnostics != nullptr; + if (g_cpuResult) { + snapshot.cpuSamples = g_cpuResult->samples.size(); snapshot.cpuMissed = g_cpuResult->missed; + snapshot.cpuDropped = g_cpuResult->dropped; snapshot.suspendedNs = g_cpuResult->suspendedNs; + } + CopyText(snapshot.reportPath, sizeof(snapshot.reportPath), g_reportPath); + CopyText(snapshot.lastError, sizeof(snapshot.lastError), g_error); +} +void ReadAuditLocked() { + if (!g_diagnostics) return; + D3D9BridgeAuditSnapshot result{}; + result.StructSize = sizeof(result); result.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + if (SUCCEEDED(g_diagnostics->GetAudit(&result))) g_audit = result; +} +void SyncThreadPhaseLocked() { + const auto phase = g_controller.phase(); + const int next = phase == Phase::Baseline ? 0 : phase == Phase::Cpu ? 1 : phase == Phase::Attribution ? 2 : -1; + if (next == g_threadPhase) return; + D3D9BridgeThreadSnapshot sample{}; + sample.StructSize = sizeof(sample); sample.Version = 1; + if (g_threadStats && FAILED(g_threadStats->GetThreadStats(&sample))) sample = {}; + if (g_threadPhase >= 0) g_threadEnd[unsigned(g_threadPhase)] = sample; + if (next >= 0) g_threadBegin[unsigned(next)] = sample; + g_threadPhase = next; +} +void StopAttributionLocked() { + if (g_journalStarted) { + // Present/Reset can already hold the device lock or be inside an admitted + // transaction. Never drain it synchronously from that calling thread. + JournalAttributionCloseAdmission(); + JournalAttributionGetSummary(&g_journal); + g_journalDrainPending = g_journal.inFlight != 0; + g_journalStarted = false; + if (!g_journalDrainPending && !g_journal.available) g_unavailable |= RuntimeJournalUnavailable; + } + if (g_diagnostics && g_auditStarted) { + g_diagnostics->StopAudit(g_capture); + ReadAuditLocked(); + g_auditStarted = false; + } +} +void WriteCombinedReport(const ReportJob& job) { + std::filesystem::create_directories(std::filesystem::path(job.path).parent_path()); + FILE* file = nullptr; + if (fopen_s(&file, job.path.c_str(), "w") || !file) throw std::runtime_error("cannot write combined report"); + struct FileClose { FILE* file; ~FileClose() { std::fclose(file); } } close{file}; + const auto& s = job.snapshot; + const char* status = s.phase == Phase::Complete ? (s.unavailableMask ? "complete-partial" : "complete") : + s.phase == Phase::Interrupted ? "interrupted" : "error"; + std::fprintf(file, "# measurement captureId=%llu status=%s pid=%lu clock=qpc-nanoseconds unavailableMask=%u\n", + s.captureId, status, GetCurrentProcessId(), s.unavailableMask); + std::fprintf(file, "hud requested=%u integrationAvailable=%u baselineKeepsSameHudConfiguration=1\n", unsigned(s.hudRequested), unsigned(s.hudIntegrationAvailable)); + std::fprintf(file, "limitations=sampling-is-statistical;frame-chain-is-structural;candidate-callers-are-heuristic;inclusive-timings-are-not-additive\n"); + const char* phases[] = {"baseline", "cpu", "attribution"}; + for (unsigned i = 0; i < 3; ++i) { + const auto& stats = s.phaseStats[i]; + std::fprintf(file, "phase=%s observer=%s elapsedMs=%.6f successfulPresents=%llu failedPresents=%llu fps=%.6f averageIntervalMs=%.6f maxIntervalMs=%.6f invalidTimes=%llu\n", + phases[i], i == 0 ? "baseline" : "instrumented", double(stats.elapsedNs()) / 1e6, + stats.presents, stats.failedPresents, stats.fps(), stats.averageIntervalNs() / 1e6, + double(stats.maxIntervalNs) / 1e6, stats.invalidTimes); + const auto& a = job.threadBegin[i]; const auto& b = job.threadEnd[i]; + const bool available = a.AvailableMask == 7 && b.AvailableMask == 7 + && a.MainThreadId == b.MainThreadId && a.CsThreadId == b.CsThreadId + && b.WallUs > a.WallUs && b.MainCpu100ns >= a.MainCpu100ns + && b.CsCpu100ns >= a.CsCpu100ns && b.CsQueueIdleUs >= a.CsQueueIdleUs; + if (!available) { + std::fprintf(file, "threadActivity phase=%s available=0 reason=missing-or-incomplete-window\n", phases[i]); + continue; + } + const auto wall = b.WallUs-a.WallUs; + std::fprintf(file, "threadActivity phase=%s available=1 mainTid=%u csTid=%u wallUs=%llu mainCpuUs=%llu csCpuUs=%llu csQueueIdleUs=%llu mainCpuPercent=%.6f csCpuPercent=%.6f orderedDispatched=%llu orderedCompleted=%llu pendingStart=%llu pendingEnd=%llu completedCsWaits=%llu completedCsWaitUs=%llu completedDeviceGpuWaits=%llu completedDeviceGpuWaitUs=%llu estimatedGpuIdleUs=%llu submittedChunks=%llu queueSubmissions=%llu\n", + phases[i], b.MainThreadId, b.CsThreadId, wall, (b.MainCpu100ns-a.MainCpu100ns)/10, + (b.CsCpu100ns-a.CsCpu100ns)/10, b.CsQueueIdleUs-a.CsQueueIdleUs, + 10.0*double(b.MainCpu100ns-a.MainCpu100ns)/double(wall), + 10.0*double(b.CsCpu100ns-a.CsCpu100ns)/double(wall), + b.OrderedDispatched-a.OrderedDispatched,b.OrderedCompleted-a.OrderedCompleted, + a.OrderedDispatched-a.OrderedCompleted,b.OrderedDispatched-b.OrderedCompleted, + b.CsSyncCount-a.CsSyncCount,b.CsSyncUs-a.CsSyncUs,b.GpuSyncCount-a.GpuSyncCount, + b.GpuSyncUs-a.GpuSyncUs,b.GpuIdleUs-a.GpuIdleUs,b.Chunks-a.Chunks,b.Submits-a.Submits); + } + std::fprintf(file, "threadActivitySemantics=CPU-is-scheduled-execution;non-CPU-wall-is-not-proven-idle;CS-queue-idle-includes-open-wait;ordered-pending-includes-inflight-excludes-injected;waits-count-completed-cross-boundary-spans;GPU-waits-device-wide;GPU-idle-estimated;chunks-merged-on-submission;no-forced-flush-or-sync\n"); + const auto& audit = job.audit; + std::fprintf(file, "backend captureId=%llu available=%u status=%u active=%u observedPresentBoundaries=%llu elapsedMs=%llu qpcFrequency=%llu timing=inclusive-api-elapsed\n", + audit.CaptureId, audit.Available, audit.Status, audit.Active, + audit.ObservedPresentBoundaries, audit.ElapsedMs, audit.QpcFrequency); + for (unsigned i = 0; i < D3D9_BRIDGE_AUDIT_COUNTER_COUNT; ++i) { + const auto& c = audit.Counters[i]; + const double ms = audit.QpcFrequency ? 1000.0 * double(c.QpcTicks) / double(audit.QpcFrequency) : 0; + std::fprintf(file, "backendCounter kind=%s calls=%llu callsPerBoundary=%.6f redundantCalls=%llu units=%llu redundantUnits=%llu timingAvailable=%u timedCalls=%llu timingScope=device-locked-api-body-inclusive ", + AuditName(i), c.Calls, audit.ObservedPresentBoundaries ? double(c.Calls) / double(audit.ObservedPresentBoundaries) : 0, + c.RedundantCalls, c.Units, c.RedundantUnits, unsigned(c.TimedCalls != 0), c.TimedCalls); + if (c.TimedCalls) std::fprintf(file, "inclusiveMs=%.9f inclusiveMsPerBoundary=%.9f maxUs=%.9f\n", ms, + audit.ObservedPresentBoundaries ? ms / double(audit.ObservedPresentBoundaries) : 0, + audit.QpcFrequency ? 1e6 * double(c.MaxQpcTicks) / double(audit.QpcFrequency) : 0); + else std::fprintf(file, "inclusiveMs=unavailable inclusiveMsPerBoundary=unavailable maxUs=unavailable\n"); + } + std::fprintf(file, "backendBindings samplerCacheHits=%llu textureCacheHits=%llu invalidations=%llu stateBlockFastSkips=%llu prepareDraws=%llu prepareCleanDraws=%llu categoryEvents=%llu\n", + audit.SamplerBindingCacheHits, audit.TextureBindingCacheHits, audit.ResourceBindingCacheInvalidations, + audit.StateBlockFastSkips, audit.PrepareDraws, audit.PrepareCleanDraws, audit.PrepareCategoryEvents); + for (unsigned i = 0; i < D3D9_BRIDGE_PREPARE_COUNTER_COUNT; ++i) + if (audit.Prepare[i].Draws || audit.Prepare[i].Units) + std::fprintf(file, "prepareCategory index=%u draws=%llu units=%llu\n", i, audit.Prepare[i].Draws, audit.Prepare[i].Units); + const auto& j = job.journal; + std::fprintf(file, "journal captureId=%llu available=%u active=%u inFlight=%u frames=%llu presents=%llu transactions=%llu records=%llu droppedFrames=%llu droppedTechniques=%llu droppedPasses=%llu orphanRestores=%llu timing=inclusive-transaction-span;restore-separate\n", + j.captureId, unsigned(j.available), unsigned(j.active), j.inFlight, j.frames, j.presents, j.transactions, + j.records, j.droppedFrames, j.droppedTechniques, j.droppedPasses, j.orphanRestores); + const double tickToMs = j.qpcFrequency ? 1000.0 / double(j.qpcFrequency) : 0; + for (unsigned i = 0; i < j.topTechniqueCount; ++i) { + const auto& t = j.topTechniques[i]; + std::fprintf(file, "journalTechnique name=%s transactions=%llu records=%llu passes=%u inclusiveSpanMs=%.9f inclusiveSpanMsPerPresent=%.9f restoreMs=%.9f restoreMsPerPresent=%.9f empty=%llu truncated=%llu\n", + t.name, t.transactions, t.records, t.passCount, double(t.transactionSpanQpc) * tickToMs, + j.presents ? double(t.transactionSpanQpc) * tickToMs / double(j.presents) : 0, + double(t.restoreQpc) * tickToMs, j.presents ? double(t.restoreQpc) * tickToMs / double(j.presents) : 0, + t.emptyTransactions, t.truncatedTransactions); + } + for (unsigned i = 0; i < j.frameStatCount; ++i) { + const auto& row = j.frameStats[i]; + std::fprintf(file, "journalFrame admissionFrame=%llu transactions=%llu records=%llu inclusiveSpanMs=%.9f restoreMs=%.9f restoreCount=%llu truncated=%llu\n", + row.frame, row.transactions, row.records, double(row.transactionSpanQpc) * tickToMs, + double(row.restoreQpc) * tickToMs, row.restoreCount, row.truncatedTransactions); + } + if (j.captureStarted && !j.inFlight && !JournalAttributionWriteReport(file)) + throw std::runtime_error("cannot write full technique/pass attribution"); + std::fprintf(file, "frameTrace count=%zu dropped=%llu slowThresholdMs=%u\n", job.trace.size(), job.trace.dropped(), job.config.timing.slowFrameMs); + for (std::size_t i = 0; i < job.trace.size(); ++i) { + const auto& row = job.trace.data()[i]; + std::fprintf(file, "frame id=%llu phase=%u beginNs=%llu endNs=%llu intervalMs=%.6f slow=%u\n", + row.frameId, unsigned(row.phase), row.beginNs, row.endNs, double(row.endNs - row.beginNs) / 1e6, + unsigned(row.endNs - row.beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull)); + } + if (job.cpu) { + const auto& cpu = *job.cpu; + std::fprintf(file, "cpu captureId=%llu samples=%zu missed=%llu dropped=%llu suspendedNs=%llu maxSuspendedNs=%llu samplingElapsedNs=%llu sampledPresents=%llu validFrameChains=%llu partialFrameChains=%llu invalidFrameChains=%llu\n", + cpu.id, cpu.samples.size(), cpu.missed, cpu.dropped, cpu.suspendedNs, cpu.maxSuspendedNs, + cpu.elapsedNs, cpu.presents, cpu.validChains, cpu.partialChains, cpu.invalidChains); + for (const auto& module : cpu.modules) + std::fprintf(file, "module base=0x%llX imageSize=%u timestamp=%u path=%s\n", std::uint64_t(module.base), module.size, module.timestamp, Quote(module.path).c_str()); + for (const auto& function : cpu.functions) + std::fprintf(file, "function base=0x%llX rva=0x%X samples=%llu percent=%.6f quality=%s provenance=%s name=%s path=%s binarySha256=%s\n", + std::uint64_t(function.symbol.moduleBase), function.symbol.rva - function.symbol.offset, function.samples, + cpu.samples.empty() ? 0 : 100.0 * double(function.samples) / double(cpu.samples.size()), + SymbolQualityName(function.symbol.quality), SymbolProvenanceName(function.symbol.provenance), + Quote(function.name).c_str(), Quote(function.symbol.modulePath).c_str(), function.symbol.binarySha256.c_str()); + std::map, std::pair> byClass; + std::uint64_t slow = 0, ordinary = 0, unmatched = 0; + for (const auto& sample : cpu.samples) { + const auto* frame = job.trace.Find(sample.timeNs); + const bool isSlow = frame && frame->endNs - frame->beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull; + if (!frame) ++unmatched; else if (isSlow) ++slow; else ++ordinary; + auto found = cpu.symbols.find(sample.ip); + if (frame && found != cpu.symbols.end()) { + const auto& symbol = found->second; + auto& counts = byClass[{symbol.moduleBase, sample.ip - symbol.offset}]; + if (isSlow) ++counts.first; else ++counts.second; + } + std::fprintf(file, "sample timeNs=%llu frame=%llu phase=%u class=%s ip=0x%llX suspendedNs=%llu chainQuality=%u chainCount=%u\n", + sample.timeNs, frame ? frame->frameId : 0, frame ? unsigned(frame->phase) : 0, + !frame ? "unmatched" : isSlow ? "slow" : "ordinary", std::uint64_t(sample.ip), sample.suspendedNs, + unsigned(sample.chain.quality), sample.chain.count); + for (unsigned i = 0; i < sample.chain.count; ++i) { + const auto address = sample.chain.addresses[i]; const auto* module = FindModule(cpu.modules, address); + std::fprintf(file, " caller kind=structural-frame-chain quality=%u index=%u address=0x%llX base=0x%llX rva=0x%llX path=%s\n", + unsigned(sample.chain.quality), i, std::uint64_t(address), module ? std::uint64_t(module->base) : 0, + module ? std::uint64_t(address - module->base) : 0, Quote(module ? module->path : "private").c_str()); + } + for (unsigned i = 0; i < sample.candidates.size(); ++i) if (sample.candidates[i]) { + const auto address = sample.candidates[i]; const auto* module = FindModule(cpu.modules, address); + std::fprintf(file, " caller kind=heuristic-candidate index=%u address=0x%llX base=0x%llX rva=0x%llX path=%s\n", + i, std::uint64_t(address), module ? std::uint64_t(module->base) : 0, + module ? std::uint64_t(address - module->base) : 0, Quote(module ? module->path : "private").c_str()); + } + } + std::fprintf(file, "sampleCoverage slow=%llu ordinary=%llu unmatched=%llu recorded=%zu dropped=%llu\n", slow, ordinary, unmatched, cpu.samples.size(), cpu.dropped); + for (const auto& [identity, counts] : byClass) + std::fprintf(file, "functionByFrameClass base=0x%llX functionStart=0x%llX slowSamples=%llu ordinarySamples=%llu\n", + std::uint64_t(identity.first), std::uint64_t(identity.second), counts.first, counts.second); + } + if (s.lastError[0]) std::fprintf(file, "error=%s\n", Quote(s.lastError).c_str()); + if (std::fflush(file) || std::ferror(file)) throw std::runtime_error("combined report write failed"); +} +DWORD WINAPI ReportWorker(void* opaque) { + WorkerExit activity; + std::unique_ptr> holder(static_cast*>(opaque)); + const auto job = *holder; + std::string error; + try { + // This wait is outside Runtime/device/owner locks. A missing transaction + // end yields an explicitly incomplete report after one second or shutdown, + // rather than a stuck game or falsely complete attribution. + if (job->journal.captureStarted) { + const auto deadline = NowNs() + 1'000'000'000ull; + do { + JournalAttributionGetSummary(&job->journal); + if (!job->journal.inFlight) break; + if (job->config.stopEvent) { + if (WaitForSingleObject(job->config.stopEvent, 2) != WAIT_TIMEOUT) break; + } else Sleep(2); + } while (NowNs() < deadline); + job->snapshot.phase = job->finalPhase; + if (job->journal.inFlight) { + job->snapshot.phase = Phase::Interrupted; + job->snapshot.unavailableMask |= RuntimeJournalUnavailable; + CopyText(job->snapshot.lastError, sizeof(job->snapshot.lastError), "journal transactions still pending; partial report"); + } else if (!job->journal.available) job->snapshot.unavailableMask |= RuntimeJournalUnavailable; + { + std::lock_guard lock(g_mutex); + if (g_capture == job->snapshot.captureId) { + g_journal = job->journal; g_journalDrainPending = job->journal.inFlight != 0; + g_unavailable = job->snapshot.unavailableMask; + if (job->journal.inFlight) { + g_reportInterrupted = true; + g_controller.Interrupt(); + CopyText(g_error, sizeof(g_error), job->snapshot.lastError); + } + } + } + } + WriteCombinedReport(*job); + if (job->journal.captureStarted && !job->journal.inFlight) JournalAttributionDump(); + Log(job->config, "capture=" + std::to_string(job->snapshot.captureId) + " report=" + job->path); + } catch (const std::exception& e) { error = e.what(); } + catch (...) { error = "combined report failed"; } + if (!error.empty()) { + std::lock_guard lock(g_mutex); + if (g_capture == job->snapshot.captureId) { g_reportFailed = true; CopyText(g_error, sizeof(g_error), error); } + } + return 0; +} +template +bool Launch(const RuntimeConfig& config, LPTHREAD_START_ROUTINE worker, const std::shared_ptr& job) { + auto* data = new (std::nothrow) std::shared_ptr(job); + if (!data) return false; + g_workers.fetch_add(1, std::memory_order_acq_rel); + const HANDLE handle = config.startWorker ? config.startWorker(worker, data) : nullptr; + if (handle) return true; + g_workers.fetch_sub(1, std::memory_order_release); delete data; return false; +} +void StartReportLocked() { + if (g_reportStarted || !g_capture) return; + g_reportStarted = true; + SyncThreadPhaseLocked(); + auto job = std::make_shared(); + job->config = g_config; job->path = g_reportPath; + FillSnapshotLocked(job->snapshot); + job->finalPhase = g_controller.phase(); + job->trace = g_controller.trace(); job->journal = g_journal; job->audit = g_audit; job->cpu = g_cpuResult; + job->threadBegin = g_threadBegin; job->threadEnd = g_threadEnd; + if (!Launch(g_config, ReportWorker, job)) { + g_reportFailed = true; CopyText(g_error, sizeof(g_error), "cannot start combined-report worker"); + } +} +void PublishHudLocked(std::uint64_t now, bool force = false) { + if (!g_diagnostics || (!force && g_lastHudNs && now - g_lastHudNs < 200'000'000ull)) return; + g_lastHudNs = now; + if (g_journalStarted) JournalAttributionGetSummary(&g_journal); + if (g_auditStarted) ReadAuditLocked(); + D3D9BridgeHudSnapshot hud{}; + hud.StructSize = sizeof(hud); hud.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + hud.CaptureId = g_capture; + hud.Phase = UINT(g_reportFailed ? Phase::Error : g_reportInterrupted ? Phase::Interrupted : g_controller.phase()); + if (g_journalDrainPending && Phase(hud.Phase) == Phase::Complete) hud.Phase = UINT(Phase::AttributionFinishing); + hud.Visible = g_config.hud; hud.LineCount = 8; + const auto phase = Phase(hud.Phase); + hud.Status = phase == Phase::Interrupted ? D3D9_BRIDGE_AUDIT_INTERRUPTED : + phase == Phase::Complete ? D3D9_BRIDGE_AUDIT_COMPLETE : phase == Phase::Ready ? D3D9_BRIDGE_AUDIT_IDLE : D3D9_BRIDGE_AUDIT_ACTIVE; + const auto& stats = g_controller.stats(g_controller.phaseIndex()); + hud.ElapsedMs = stats.elapsedNs() / 1'000'000ull; hud.DurationMs = g_controller.durationMs(); + hud.ProgressPermille = hud.DurationMs ? UINT((std::min)(1000ull, hud.ElapsedMs * 1000ull / hud.DurationMs)) : 0; + std::snprintf(hud.Lines[0], sizeof(hud.Lines[0]), "RenderStack #%llu: %s", g_capture, PhaseName(phase)); + std::snprintf(hud.Lines[1], sizeof(hud.Lines[1]), "F8/F7: baseline -> CPU -> state | %llu / %llu ms", hud.ElapsedMs, hud.DurationMs); + std::snprintf(hud.Lines[2], sizeof(hud.Lines[2]), "Live: %.1f FPS | mean %.2f ms | max %.2f ms", g_liveFps, g_liveMeanMs, g_liveMaxMs); + std::snprintf(hud.Lines[3], sizeof(hud.Lines[3]), "Phase FPS: base %.1f | CPU %.1f* | state %.1f* (*instrumented)", + g_controller.stats(0).fps(), g_controller.stats(1).fps(), g_controller.stats(2).fps()); + if (g_cpuResult) { + std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: %zu samples, %llu missed, %llu dropped | suspended %.2f ms", + g_cpuResult->samples.size(), g_cpuResult->missed, g_cpuResult->dropped, double(g_cpuResult->suspendedNs) / 1e6); + if (!g_cpuResult->functions.empty()) { + const auto& top = g_cpuResult->functions[0]; + std::snprintf(hud.Lines[5], sizeof(hud.Lines[5]), "Top: %.1f%% %s [%s]", + g_cpuResult->samples.empty() ? 0 : 100.0 * double(top.samples) / double(g_cpuResult->samples.size()), + top.name.c_str(), SymbolQualityName(top.symbol.quality)); + } + } else std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: waiting | no sampling during baseline"); + std::snprintf(hud.Lines[6], sizeof(hud.Lines[6]), "State: %llu transactions | API %llu calls, %llu redundant | missing=0x%X", + g_journal.transactions, g_audit.Total.Calls, g_audit.Total.RedundantCalls, g_unavailable | g_controller.unavailableMask()); + if (g_error[0]) std::snprintf(hud.Lines[7], sizeof(hud.Lines[7]), "Status: %s", g_error); + else if (g_reportStarted) std::snprintf(hud.Lines[7], sizeof(hud.Lines[7]), "Report: %s%s", Basename(g_reportPath).c_str(), g_workers.load() ? " (writing)" : ""); + else std::snprintf(hud.Lines[7], sizeof(hud.Lines[7]), "Detailed functions, slow frames, technique/pass and API timing in report"); + g_diagnostics->PublishHud(&hud); +} +void InterruptLocked(const char* reason, bool releaseDevice) { + if (g_cpuJob) g_cpuJob->cancel.store(true, std::memory_order_relaxed); + const bool wasActive = g_controller.active(); + if (wasActive) { + g_controller.Interrupt(); CopyText(g_error, sizeof(g_error), reason ? reason : "interrupted"); + StopAttributionLocked(); StartReportLocked(); + } + PublishHudLocked(NowNs(), true); + if (releaseDevice) { + if (g_diagnostics) { + D3D9BridgeHudSnapshot hidden{}; + hidden.StructSize = sizeof(hidden); hidden.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + g_diagnostics->PublishHud(&hidden); + g_diagnostics->SetCaptureOwner(FALSE); + } + g_diagnostics = nullptr; g_threadStats = nullptr; g_device = nullptr; g_owner = 0; + } +} +} // namespace + +void ConfigureRuntime(const RuntimeConfig& config) { + std::lock_guard lock(g_mutex); + if (g_workers.load(std::memory_order_acquire)) return; // host joins before reconfigure + if (g_journalDrainPending) { + JournalAttributionGetSummary(&g_journal); + if (g_journal.inFlight) return; // never reset a late transaction's tables + g_journalDrainPending = false; + } + if (g_diagnostics) { + D3D9BridgeHudSnapshot hidden{}; + hidden.StructSize = sizeof(hidden); hidden.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + g_diagnostics->PublishHud(&hidden); + g_diagnostics->SetCaptureOwner(FALSE); + } + g_config = config; g_directory = config.gameDirectory ? config.gameDirectory : "."; + g_controller = Controller(config.timing); + g_device = nullptr; g_diagnostics = nullptr; g_threadStats = nullptr; g_owner = 0; g_capture = 0; + g_threadBegin = {}; g_threadEnd = {}; g_threadPhase = -1; + g_cpuJob.reset(); g_cpuResult.reset(); g_cpuReady = false; + g_journalStarted = g_auditStarted = g_reportFailed = g_reportStarted = g_reportInterrupted = false; + g_journalDrainPending = false; + g_journal = {}; g_audit = {}; g_unavailable = 0; g_lastHudNs = 0; + g_reportPath[0] = g_error[0] = 0; g_liveStarted = false; + g_liveFps = g_liveMeanMs = g_liveMaxMs = 0; + g_enabled.store(config.enabled, std::memory_order_release); +} +bool RuntimeEnabled() noexcept { return g_enabled.load(std::memory_order_acquire); } + +void RuntimePrepareDevice(IDirect3DDevice9* device) { + if (!RuntimeEnabled() || !device) return; + std::lock_guard lock(g_mutex); + if (g_device) return; + ID3D9BridgeDiagnostics* diagnostics = nullptr; + if (SUCCEEDED(device->QueryInterface(IID_D3D9BridgeDiagnostics, reinterpret_cast(&diagnostics)))) { + const auto release = [](ID3D9BridgeDiagnostics* p) { p->Release(); }; + std::unique_ptr ref(diagnostics, release); + if (SUCCEEDED(diagnostics->SetCaptureOwner(TRUE))) g_diagnostics = diagnostics; + else g_unavailable |= RuntimeBackendUnavailable; + } else g_unavailable |= RuntimeBackendUnavailable; + ID3D9BridgeThreadStats* threadStats = nullptr; + if (SUCCEEDED(device->QueryInterface(IID_D3D9BridgeThreadStats, reinterpret_cast(&threadStats)))) { + g_threadStats = threadStats; + threadStats->Release(); + } + g_device = device; // publish borrowed state only after successful setup +} + +void RuntimePresent(IDirect3DDevice9* device, HRESULT result) { + if (!RuntimeEnabled() || !device) return; + RuntimePrepareDevice(device); + std::lock_guard lock(g_mutex); + const auto now = NowNs(); + if (g_journalDrainPending && !g_workers.load(std::memory_order_acquire)) { + JournalAttributionGetSummary(&g_journal); + if (!g_journal.inFlight) g_journalDrainPending = false; + } + if (!g_owner) g_owner = GetCurrentThreadId(); + g_controller.SetAvailable(g_config.cpu, g_config.journal || g_diagnostics != nullptr); + if (g_device != device || g_owner != GetCurrentThreadId()) return; + const bool success = result == S_OK; + if (success) g_successfulPresents.fetch_add(1, std::memory_order_relaxed); + if (!g_liveStarted) { g_live.Begin(now); g_liveStarted = true; } + else { + g_live.Observe(now, success); + if (g_live.elapsedNs() >= 500'000'000ull) { + g_liveFps = g_live.fps(); g_liveMeanMs = g_live.averageIntervalNs() / 1e6; + g_liveMaxMs = double(g_live.maxIntervalNs) / 1e6; g_live.Begin(now); + } + } + const auto key = [&](int vk) { return g_config.keyDown ? g_config.keyDown(vk) : (GetAsyncKeyState(vk) & 0x8000) != 0; }; + const bool f7 = key(VK_F7), f8 = key(VK_F8); + if ((!g_workers.load(std::memory_order_acquire) && !g_journalDrainPending) || g_controller.active()) { + const bool newCapture = g_controller.Key(f7, f8, now); + if (newCapture) { + g_capture = ++g_nextCapture; + g_cpuJob.reset(); g_cpuResult.reset(); g_cpuReady = false; + g_reportStarted = g_reportFailed = g_reportInterrupted = false; g_error[0] = 0; + g_journal = {}; g_audit = {}; + g_threadBegin = {}; g_threadEnd = {}; g_threadPhase = -1; + SyncThreadPhaseLocked(); + g_unavailable = g_diagnostics ? 0 : RuntimeBackendUnavailable; + if (!g_config.journal) g_unavailable |= RuntimeJournalUnavailable; + const auto path = (std::filesystem::path(g_directory) / "scripts" / + ("BridgeD3D9.measurement-" + std::to_string(GetCurrentProcessId()) + "-" + std::to_string(g_capture) + ".log")).string(); + CopyText(g_reportPath, sizeof(g_reportPath), path); + Log(g_config, "capture=" + std::to_string(g_capture) + " started baseline -> CPU -> attribution"); + PublishHudLocked(now, true); + return; + } + } else g_controller.SuppressKeys(f7, f8); + if (g_cpuReady && (g_controller.phase() == Phase::Cpu || g_controller.phase() == Phase::CpuFinishing)) { + g_controller.CpuFinished(g_cpuResult && g_cpuResult->error.empty()); + g_cpuReady = false; + } + if (g_journalStarted && success) JournalAttributionOnPresent(); + const auto action = g_controller.Present(now, success); + SyncThreadPhaseLocked(); + if (action == Action::StartCpu) { + auto job = std::make_shared(); job->config = g_config; job->directory = g_directory; + job->thread = g_owner; job->id = g_capture; g_cpuJob = job; + if (!g_config.cpu) { + g_unavailable |= RuntimeCpuUnavailable; g_controller.CpuFinished(true); + } else if (!Launch(g_config, CpuWorker, job)) { + CopyText(g_error, sizeof(g_error), "cannot start CPU sampler worker"); g_controller.CpuFinished(false); + } + } else if (action == Action::StartAttribution) { + if (g_config.journal) { + JournalAttributionSetCaptureIdentity(g_capture); JournalAttributionStartCapture(); g_journalStarted = true; + } + if (g_diagnostics) { + const auto started = g_diagnostics->StartAudit(g_capture, g_config.timing.attributionMs); + if (SUCCEEDED(started)) { g_auditStarted = true; ReadAuditLocked(); } + else g_unavailable |= RuntimeBackendUnavailable; + } + } else if (action == Action::FinishAttribution || action == Action::ReportError) { + StopAttributionLocked(); StartReportLocked(); + } + PublishHudLocked(now, action != Action::None); +} +void RuntimeInterrupt(IDirect3DDevice9* device, const char* reason, bool releaseDevice) { + std::lock_guard lock(g_mutex); + if (device && device != g_device) return; + InterruptLocked(reason, releaseDevice); +} +void RuntimeShutdown() { + std::lock_guard lock(g_mutex); + InterruptLocked("shutdown", true); + g_enabled.store(false, std::memory_order_release); +} +bool RuntimeGetSnapshot(RuntimeSnapshot* snapshot) { + if (!snapshot) return false; + std::lock_guard lock(g_mutex); + FillSnapshotLocked(*snapshot); + return true; +} +} // namespace BridgeMeasurement diff --git a/src/bridge/legacy/BridgeMeasurementRuntime.h b/src/bridge/legacy/BridgeMeasurementRuntime.h new file mode 100644 index 0000000..60cb261 --- /dev/null +++ b/src/bridge/legacy/BridgeMeasurementRuntime.h @@ -0,0 +1,74 @@ +#pragma once + +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#include +#include "BridgeMeasurement.h" + +namespace BridgeMeasurement { + +struct RuntimeCpuAddress { std::uintptr_t address = 0; std::uint64_t samples = 0; }; +struct RuntimeCpuStack { + std::uintptr_t ip = 0; + std::array callers{}; + std::uint64_t samples = 0; + ChainQuality quality = ChainQuality::Unavailable; +}; +struct RuntimeCpuModule { + std::uintptr_t base = 0; + std::uint32_t size = 0, timestamp = 0; + std::string path; +}; +struct RuntimeCpuReport { + std::uint64_t captureId = 0, elapsedMs = 0, presents = 0, samples = 0, missed = 0; + double fps = 0; + std::vector addresses; + std::vector stacks; + std::vector modules; +}; + +struct RuntimeConfig { + bool enabled = false, hud = true, journal = true, cpu = true; + Options timing{}; + std::uint32_t intervalMs = 5; + const char* gameDirectory = nullptr; + HANDLE stopEvent = nullptr; + // The host owns returned thread handles and must join them before unload or + // reconfiguration. Runtime never closes them or waits under its state lock. + HANDLE (*startWorker)(LPTHREAD_START_ROUTINE, void*) = nullptr; + bool (*keyDown)(int) = nullptr; + void (*log)(const char*) = nullptr; + bool (*readFrame)(std::uint32_t&) = nullptr; + void (*appendCpuReport)(const char*, const RuntimeCpuReport&) = nullptr; +}; + +inline constexpr std::uint32_t RuntimeCpuUnavailable = 2; +inline constexpr std::uint32_t RuntimeAttributionUnavailable = 4; +inline constexpr std::uint32_t RuntimeBackendUnavailable = 8; +inline constexpr std::uint32_t RuntimeJournalUnavailable = 16; +struct RuntimeSnapshot { + Phase phase = Phase::Ready; + std::uint64_t captureId = 0; + std::uint32_t unavailableMask = 0; + FrameStats phaseStats[3]{}; + std::uint64_t cpuSamples = 0, cpuMissed = 0, cpuDropped = 0; + std::uint64_t suspendedNs = 0, frameTraceDropped = 0; + bool workerBusy = false; + bool hudRequested = false, hudIntegrationAvailable = false; + char reportPath[MAX_PATH]{}, lastError[128]{}; +}; + +void ConfigureRuntime(const RuntimeConfig&); +bool RuntimeEnabled() noexcept; +// Acquire combined audit ownership before the backend can poll a first F7. +// The first real Present selects the capture thread, not device creation. +void RuntimePrepareDevice(IDirect3DDevice9* rawDevice); +void RuntimePresent(IDirect3DDevice9* rawDevice, HRESULT); +void RuntimeInterrupt(IDirect3DDevice9* rawDevice, const char* reason, bool releaseDevice = false); +void RuntimeShutdown(); +bool RuntimeGetSnapshot(RuntimeSnapshot*); + +} // namespace BridgeMeasurement diff --git a/src/bridge/legacy/EffectInspector.cpp b/src/bridge/legacy/EffectInspector.cpp index 7ea0d66..aec6978 100644 --- a/src/bridge/legacy/EffectInspector.cpp +++ b/src/bridge/legacy/EffectInspector.cpp @@ -1641,9 +1641,29 @@ std::uint64_t g_journalAttributionDroppedTechniques = 0; std::uint64_t g_journalAttributionDroppedPasses = 0; std::uint64_t g_journalAttributionOrphanRestores = 0; std::atomic g_journalAttributionInFlight{ 0 }; +JournalAttributionSummary g_journalAttributionSummary{}; +std::uint64_t g_journalAttributionCaptureId = 0; thread_local JournalAttributionTechniqueStat* g_pendingAttributionTechnique = nullptr; thread_local std::uint64_t g_pendingAttributionSequence = 0; +thread_local JournalAttributionFrameSummary* g_pendingAttributionFrame = nullptr; +thread_local bool g_pendingAttribution = false; + +JournalAttributionFrameSummary* FindAttributionFrame(std::uint64_t frame) +{ + if (frame >= kJournalAttributionSummaryFrames) { + g_journalAttributionSummary.droppedFrames = (std::max)( + g_journalAttributionSummary.droppedFrames, + frame - kJournalAttributionSummaryFrames + 1); + return nullptr; + } + auto& summary = g_journalAttributionSummary; + summary.frameStatCount = (std::max)(summary.frameStatCount, + static_cast(frame + 1)); + auto& result = summary.frameStats[frame]; + result.frame = frame; + return &result; +} JournalAttributionTechniqueStat* FindAttributionTechnique(const char* name) { @@ -1659,7 +1679,11 @@ JournalAttributionTechniqueStat* FindAttributionTechnique(const char* name) } JournalAttributionTechniqueStat& result = g_journalAttribution[g_journalAttributionTechniqueCount++]; - FormatTo(result.name, sizeof(result.name), "{}", normalized); + // Names are copied once on insertion; no formatting/allocation is needed + // in transaction admission or aggregation. + const std::size_t length = (std::min)(std::strlen(normalized), sizeof(result.name) - 1); + std::memcpy(result.name, normalized, length); + result.name[length] = '\0'; return &result; } @@ -1712,7 +1736,27 @@ void JournalAttributionObserveTransaction(const char* techniqueName, // not re-check the global flag here: a transaction already in flight must // still publish its final record set and restore timing. AcquireSRWLockExclusive(&g_journalAttributionLock); + auto& summary = g_journalAttributionSummary; + ++summary.transactions; + summary.records += records.size(); + summary.transactionSpanQpc += info.qpcTicks; + summary.maxTransactionSpanQpc = (std::max)(summary.maxTransactionSpanQpc, info.qpcTicks); + if (info.native) ++summary.nativeTransactions; + else ++summary.localTransactions; + if (records.empty()) ++summary.emptyTransactions; + if (truncated) ++summary.truncatedTransactions; + auto* frame = FindAttributionFrame(info.frame); + if (frame) { + ++frame->transactions; + frame->records += records.size(); + frame->transactionSpanQpc += info.qpcTicks; + if (truncated) ++frame->truncatedTransactions; + } JournalAttributionTechniqueStat* technique = FindAttributionTechnique(techniqueName); + g_pendingAttributionTechnique = technique; + g_pendingAttributionSequence = info.sequence; + g_pendingAttributionFrame = frame; + g_pendingAttribution = true; if (!technique) { ReleaseSRWLockExclusive(&g_journalAttributionLock); return; @@ -1764,18 +1808,30 @@ void JournalAttributionObserveRestore(std::uint64_t sequence, std::uint64_t qpcTicks) { AcquireSRWLockExclusive(&g_journalAttributionLock); - if (!g_pendingAttributionTechnique || + if (!g_pendingAttribution || g_pendingAttributionSequence != sequence) { ++g_journalAttributionOrphanRestores; ReleaseSRWLockExclusive(&g_journalAttributionLock); return; } - JournalAttributionTechniqueStat& technique = *g_pendingAttributionTechnique; - ++technique.restoreCount; - technique.restoreQpc += qpcTicks; - if (qpcTicks > technique.maxRestoreQpc) technique.maxRestoreQpc = qpcTicks; + auto& summary = g_journalAttributionSummary; + ++summary.restoreCount; + summary.restoreQpc += qpcTicks; + summary.maxRestoreQpc = (std::max)(summary.maxRestoreQpc, qpcTicks); + if (g_pendingAttributionTechnique) { + auto& technique = *g_pendingAttributionTechnique; + ++technique.restoreCount; + technique.restoreQpc += qpcTicks; + if (qpcTicks > technique.maxRestoreQpc) technique.maxRestoreQpc = qpcTicks; + } + if (g_pendingAttributionFrame) { + ++g_pendingAttributionFrame->restoreCount; + g_pendingAttributionFrame->restoreQpc += qpcTicks; + } g_pendingAttributionTechnique = nullptr; g_pendingAttributionSequence = 0; + g_pendingAttributionFrame = nullptr; + g_pendingAttribution = false; ReleaseSRWLockExclusive(&g_journalAttributionLock); } @@ -1789,24 +1845,93 @@ void JournalAttributionReset() g_journalAttributionDroppedTechniques = 0; g_journalAttributionDroppedPasses = 0; g_journalAttributionOrphanRestores = 0; + g_journalAttributionSummary = {}; + g_journalAttributionSummary.captureId = g_journalAttributionCaptureId; + LARGE_INTEGER frequency{}; + g_journalAttributionSummary.timingAvailable = + QueryPerformanceFrequency(&frequency) && frequency.QuadPart > 0; + g_journalAttributionSummary.qpcFrequency = static_cast(frequency.QuadPart); g_pendingAttributionTechnique = nullptr; g_pendingAttributionSequence = 0; + g_pendingAttributionFrame = nullptr; + g_pendingAttribution = false; ReleaseSRWLockExclusive(&g_journalAttributionLock); } +void JournalAttributionSetCaptureIdentity(std::uint64_t captureId) +{ + AcquireSRWLockExclusive(&g_journalAttributionLock); + if (!g_properShadersStateAttributionActive.load(std::memory_order_acquire) && + g_journalAttributionInFlight.load(std::memory_order_acquire) == 0) { + g_journalAttributionCaptureId = captureId; + } + ReleaseSRWLockExclusive(&g_journalAttributionLock); +} + +bool JournalAttributionGetSummary(JournalAttributionSummary* summary) +{ + if (!summary) return false; + AcquireSRWLockShared(&g_journalAttributionLock); + *summary = g_journalAttributionSummary; + summary->frames = g_journalAttributionFrame; + summary->presents = g_journalAttributionPresentCount; + summary->techniqueCount = g_journalAttributionTechniqueCount; + summary->droppedTechniques = g_journalAttributionDroppedTechniques; + summary->droppedPasses = g_journalAttributionDroppedPasses; + summary->orphanRestores = g_journalAttributionOrphanRestores; + summary->active = g_properShadersStateAttributionActive.load(std::memory_order_acquire); + summary->inFlight = static_cast(g_journalAttributionInFlight.load(std::memory_order_acquire)); + summary->available = summary->transactions != 0; + for (unsigned i = 0; i < g_journalAttributionTechniqueCount; ++i) { + const auto& technique = g_journalAttribution[i]; + summary->passCount += technique.passCount; + unsigned rank = 0; + while (rank < summary->topTechniqueCount && + summary->topTechniques[rank].transactionSpanQpc >= technique.totalQpc) ++rank; + if (rank == kJournalAttributionSummaryTechniques) continue; + const unsigned count = (std::min)(summary->topTechniqueCount + 1, + kJournalAttributionSummaryTechniques); + for (unsigned slot = count - 1; slot > rank; --slot) + summary->topTechniques[slot] = summary->topTechniques[slot - 1]; + auto& entry = summary->topTechniques[rank]; + std::memcpy(entry.name, technique.name, sizeof(entry.name)); + entry.transactions = technique.transactions; + entry.records = technique.records; + entry.nativeTransactions = technique.nativeTransactions; + entry.localTransactions = technique.localTransactions; + entry.emptyTransactions = technique.emptyTransactions; + entry.truncatedTransactions = technique.truncated; + entry.transactionSpanQpc = technique.totalQpc; + entry.maxTransactionSpanQpc = technique.maxQpc; + entry.restoreQpc = technique.restoreQpc; + entry.maxRestoreQpc = technique.maxRestoreQpc; + entry.restoreCount = technique.restoreCount; + entry.passCount = technique.passCount; + summary->topTechniqueCount = count; + } + ReleaseSRWLockShared(&g_journalAttributionLock); + return true; +} + void JournalAttributionStartCapture() { JournalAttributionReset(); AcquireSRWLockExclusive(&g_journalAttributionLock); + g_journalAttributionSummary.captureStarted = true; g_properShadersStateAttributionActive.store(true, std::memory_order_release); ReleaseSRWLockExclusive(&g_journalAttributionLock); } -void JournalAttributionStopCapture() +void JournalAttributionCloseAdmission() { AcquireSRWLockExclusive(&g_journalAttributionLock); g_properShadersStateAttributionActive.store(false, std::memory_order_release); ReleaseSRWLockExclusive(&g_journalAttributionLock); +} + +void JournalAttributionStopCapture() +{ + JournalAttributionCloseAdmission(); while (g_journalAttributionInFlight.load(std::memory_order_acquire) != 0) { Sleep(0); } @@ -1814,6 +1939,10 @@ void JournalAttributionStopCapture() bool JournalAttributionTryBeginTransaction() { + // The common inactive path never contends with snapshots or another + // thread's aggregation. Admission still rechecks under the StopCapture + // lock, so a stale true read cannot admit work after the stop boundary. + if (!g_properShadersStateAttributionActive.load(std::memory_order_acquire)) return false; AcquireSRWLockExclusive(&g_journalAttributionLock); const bool active = g_properShadersStateAttributionActive.load( std::memory_order_acquire); @@ -1839,6 +1968,7 @@ void JournalAttributionOnPresent() // this lock acquisition. Do not count a Present that belongs to the next // capture window. if (g_properShadersStateAttributionActive.load(std::memory_order_acquire)) { + FindAttributionFrame(g_journalAttributionFrame); ++g_journalAttributionFrame; ++g_journalAttributionPresentCount; } @@ -1853,18 +1983,12 @@ std::uint64_t JournalAttributionCurrentFrame() return frame; } -void JournalAttributionDump() +bool JournalAttributionWriteReport(std::FILE* report) { + if (!report) return false; AcquireSRWLockShared(&g_journalAttributionLock); - if (!g_journalAttributionTechniqueCount) { - ReleaseSRWLockShared(&g_journalAttributionLock); - return; - } - FILE* report = std::fopen("scripts\\BridgeD3D9.state-attribution.log", "a"); - if (!report) { - ReleaseSRWLockShared(&g_journalAttributionLock); - return; - } + bool succeeded = false; + try { LARGE_INTEGER frequency{}; QueryPerformanceFrequency(&frequency); @@ -1878,6 +2002,9 @@ void JournalAttributionDump() std::fputc('\n', report); }; line("---- ProperShaders state attribution (observation only) ----"); + line("captureId={} available={} transactionTiming=transaction-span-inclusive restoreTiming=restore-inclusive frameTotal=not-computed", + g_journalAttributionSummary.captureId, + g_journalAttributionSummary.transactions != 0); line("frames={} presents={} techniques={} droppedTechniques={} droppedPasses={} orphanRestores={} qpcToMs={:.9f}", g_journalAttributionFrame, g_journalAttributionPresentCount, @@ -1918,7 +2045,30 @@ void JournalAttributionDump() DumpAttributionOps(report, pass); } } + for (unsigned i = 0; i < g_journalAttributionSummary.frameStatCount; ++i) { + const auto& frame = g_journalAttributionSummary.frameStats[i]; + line("frame={} transactions={} records={} truncated={} transactionSpanInclusiveQpc={} restoreInclusiveQpc={} restoreCount={}", + frame.frame, frame.transactions, frame.records, frame.truncatedTransactions, + frame.transactionSpanQpc, frame.restoreQpc, frame.restoreCount); + } + line("droppedFrames={} transactionSpanInclusiveQpc={} restoreInclusiveQpc={}", + g_journalAttributionSummary.droppedFrames, + g_journalAttributionSummary.transactionSpanQpc, + g_journalAttributionSummary.restoreQpc); line("---- end ProperShaders state attribution ----"); - std::fclose(report); + succeeded = std::fflush(report) == 0 && std::ferror(report) == 0; + } catch (...) { + // std::print can throw on actual FILE output failure. The caller gets + // an explicit failure and subsequent captures can still acquire the lock. + } ReleaseSRWLockShared(&g_journalAttributionLock); + return succeeded; +} + +void JournalAttributionDump() +{ + FILE* report = std::fopen("scripts\\BridgeD3D9.state-attribution.log", "a"); + if (!report) return; + JournalAttributionWriteReport(report); + std::fclose(report); } diff --git a/src/bridge/legacy/MeasurementSymbols.cpp b/src/bridge/legacy/MeasurementSymbols.cpp new file mode 100644 index 0000000..e6deb1f --- /dev/null +++ b/src/bridge/legacy/MeasurementSymbols.cpp @@ -0,0 +1,462 @@ +#include "MeasurementSymbols.h" + +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#ifdef _MSC_VER +#pragma comment(lib, "bcrypt.lib") +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace BridgeMeasurement { +namespace { +constexpr size_t MaxBinaryBytes = 512u * 1024u * 1024u; +constexpr size_t MaxSidecarBytes = 64u * 1024u * 1024u; +constexpr size_t MaxLineBytes = 16384; +constexpr size_t MaxSymbols = 250000; +constexpr size_t MaxNameBytes = 4096; + +struct Section { uint32_t begin = 0, end = 0; }; +struct FileSection { Section range; uint32_t rawOffset = 0, rawSize = 0; }; +struct ExportLayout { + uint32_t directoryRva = 0, directorySize = 0, headersSize = 0; + std::vector sections; +}; +struct Symbol { + uint32_t begin = 0, end = 0, prefixEnd = 0; + SymbolQuality quality = SymbolQuality::Missing; + std::string name; + std::string rawName; + std::vector aliases; +}; + +std::string FullPathUtf8(std::string_view path) { + const auto encoded = std::filesystem::absolute(std::filesystem::u8path(path)).lexically_normal().u8string(); + // u8string stores UTF-8 bytes as char in C++17 and char8_t in C++20+. + return {reinterpret_cast(encoded.data()), encoded.size()}; +} + +bool ReadFile(const std::filesystem::path& path, size_t limit, std::vector& data) { + std::ifstream input(path, std::ios::binary | std::ios::ate); + if (!input) return false; + const auto length = input.tellg(); + if (length < 0 || static_cast(length) > limit) return false; + data.resize(static_cast(length)); + input.seekg(0); + return data.empty() || static_cast(input.read(reinterpret_cast(data.data()), data.size())); +} + +bool U16(const std::vector& data, size_t at, uint16_t& value) { + if (at > data.size() || data.size() - at < 2) return false; + value = static_cast(data[at] | data[at + 1] << 8); + return true; +} + +bool U32(const std::vector& data, size_t at, uint32_t& value) { + if (at > data.size() || data.size() - at < 4) return false; + value = uint32_t(data[at]) | uint32_t(data[at + 1]) << 8 | uint32_t(data[at + 2]) << 16 | uint32_t(data[at + 3]) << 24; + return true; +} + +bool ReadPe(const std::vector& data, uint32_t& imageSize, std::vector
& sections, + ExportLayout* exportLayout = nullptr) { + uint16_t dos = 0, count = 0, optionalSize = 0, magic = 0; + uint32_t pe = 0, signature = 0; + if (!U16(data, 0, dos) || dos != 0x5a4d || !U32(data, 0x3c, pe) || + pe > data.size() || data.size() - pe < 24 || !U32(data, pe, signature) || signature != 0x4550 || + !U16(data, pe + 6, count) || !count || count > 96 || !U16(data, pe + 20, optionalSize) || + !U16(data, pe + 24, magic) || (magic != 0x10b && magic != 0x20b) || + optionalSize < (magic == 0x10b ? 96 : 112) || !U32(data, pe + 80, imageSize) || !imageSize) return false; + const size_t table = static_cast(pe) + 24 + optionalSize; + if (table > data.size() || data.size() - table < size_t(count) * 40) return false; + if (exportLayout) { + uint32_t directoryCount = 0; + const size_t directoryOffset = magic == 0x10b ? 96 : 112; + if (!U32(data, static_cast(pe) + 24 + directoryOffset - 4, directoryCount) || + !U32(data, static_cast(pe) + 24 + 60, exportLayout->headersSize)) return false; + if (exportLayout->headersSize > imageSize) return false; + if (directoryCount) { + if (optionalSize < directoryOffset + 8 || + !U32(data, static_cast(pe) + 24 + directoryOffset, exportLayout->directoryRva) || + !U32(data, static_cast(pe) + 28 + directoryOffset, exportLayout->directorySize)) return false; + if (exportLayout->directoryRva || exportLayout->directorySize) { + if (!exportLayout->directoryRva || exportLayout->directorySize < 40 || + exportLayout->directoryRva >= imageSize || exportLayout->directorySize > imageSize - exportLayout->directoryRva) return false; + } + } + } + for (size_t i = 0; i < count; ++i) { + uint32_t size = 0, start = 0, raw = 0, rawOffset = 0, flags = 0; + const size_t at = table + i * 40; + if (!U32(data, at + 8, size) || !U32(data, at + 12, start) || + !U32(data, at + 16, raw) || !U32(data, at + 20, rawOffset) || !U32(data, at + 36, flags)) return false; + if (!size) size = raw; + if (start >= imageSize || size > imageSize - start) return false; + if ((flags & 0x20000000) && size) sections.push_back({start, start + size}); + if (exportLayout && size) exportLayout->sections.push_back({{start, start + size}, rawOffset, raw}); + } + std::sort(sections.begin(), sections.end(), [](const Section& a, const Section& b) { return a.begin < b.begin; }); + for (size_t i = 1; i < sections.size(); ++i) if (sections[i].begin < sections[i - 1].end) return false; + return true; +} + +// A metadata span must be backed by one unambiguous section (or PE headers). +bool MapRva(const std::vector& data, const ExportLayout& layout, + uint32_t rva, size_t count, size_t& offset, size_t* available = nullptr) { + size_t remaining = 0; + bool mapped = false; + if (rva < layout.headersSize && rva < data.size()) { + offset = rva; + remaining = (std::min)(size_t(layout.headersSize - rva), data.size() - rva); + mapped = true; + } + for (const auto& section : layout.sections) { + if (rva < section.range.begin || rva >= section.range.end) continue; + if (mapped) return false; + const uint32_t delta = rva - section.range.begin; + if (delta >= section.rawSize || section.rawOffset > data.size() || delta > data.size() - section.rawOffset) return false; + offset = size_t(section.rawOffset) + delta; + remaining = (std::min)(size_t(section.range.end - rva), size_t(section.rawSize - delta)); + remaining = (std::min)(remaining, data.size() - offset); + mapped = true; + } + if (!mapped || count > remaining) return false; + if (available) *available = remaining; + return true; +} + +bool ReadExportString(const std::vector& data, const ExportLayout& layout, + uint32_t rva, size_t limit, std::string& text) { + size_t offset = 0, available = 0; + if (!MapRva(data, layout, rva, 1, offset, &available)) return false; + const size_t length = (std::min)(available, limit + 1); + const auto begin = data.begin() + offset; + const auto end = std::find(begin, begin + length, 0); + if (end == begin || end == begin + length) return false; + text.assign(begin, end); + if (text.find_first_of("\t\r\n") != std::string::npos) return false; + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, text.data(), static_cast(text.size()), nullptr, 0) > 0; +} + +bool ParseExports(const std::vector& data, const ExportLayout& layout, + const std::vector
& executableSections, uint32_t imageSize, + std::vector& symbols) { + if (!layout.directoryRva) return true; + size_t directory = 0; + if (!MapRva(data, layout, layout.directoryRva, 40, directory)) return false; + uint32_t functionCount = 0, nameCount = 0, functionsRva = 0, namesRva = 0, ordinalsRva = 0; + if (!U32(data, directory + 20, functionCount) || !U32(data, directory + 24, nameCount) || + !U32(data, directory + 28, functionsRva) || !U32(data, directory + 32, namesRva) || + !U32(data, directory + 36, ordinalsRva) || functionCount > MaxSymbols || nameCount > MaxSymbols || + (!functionCount && nameCount)) return false; + size_t functions = 0, names = 0, ordinals = 0; + if ((functionCount && !MapRva(data, layout, functionsRva, size_t(functionCount) * 4, functions)) || + (nameCount && (!MapRva(data, layout, namesRva, size_t(nameCount) * 4, names) || + !MapRva(data, layout, ordinalsRva, size_t(nameCount) * 2, ordinals)))) return false; + struct Entry { uint32_t rva = 0; bool forwarded = false; }; + std::vector entries(functionCount); + std::vector starts; + const uint32_t directoryEnd = layout.directoryRva + layout.directorySize; + auto executable = [&](uint32_t rva) { + return std::find_if(executableSections.begin(), executableSections.end(), [&](const Section& section) { + return rva >= section.begin && rva < section.end; + }); + }; + for (size_t i = 0; i < functionCount; ++i) { + if (!U32(data, functions + i * 4, entries[i].rva) || entries[i].rva >= imageSize) return false; + const uint32_t rva = entries[i].rva; + if (!rva) continue; // Empty ordinal slots are not executable entries. + entries[i].forwarded = rva >= layout.directoryRva && rva < directoryEnd; + if (entries[i].forwarded) { + std::string target; + if (!ReadExportString(data, layout, rva, (std::min)(MaxNameBytes, size_t(directoryEnd - rva - 1)), target)) return false; + } + if (executable(rva) != executableSections.end()) starts.push_back(rva); + } + std::sort(starts.begin(), starts.end()); + starts.erase(std::unique(starts.begin(), starts.end()), starts.end()); + std::map> groups; + for (size_t i = 0; i < nameCount; ++i) { + uint32_t nameRva = 0; + uint16_t ordinal = 0; + std::string name; + if (!U32(data, names + i * 4, nameRva) || !U16(data, ordinals + i * 2, ordinal) || ordinal >= functionCount || + !entries[ordinal].rva || !ReadExportString(data, layout, nameRva, MaxNameBytes, name)) return false; + const auto& entry = entries[ordinal]; + if (!entry.forwarded && executable(entry.rva) != executableSections.end()) groups[entry.rva].push_back(std::move(name)); + } + uint32_t prefixEnd = 0; + for (auto& group : groups) { + Symbol symbol; + symbol.begin = group.first; + const auto section = executable(symbol.begin); + const auto next = std::upper_bound(starts.begin(), starts.end(), symbol.begin); + symbol.end = next == starts.end() ? section->end : (std::min)(section->end, *next); + // An executable-marked export directory still contains metadata/forwarders. + if (symbol.begin < layout.directoryRva && layout.directoryRva < symbol.end) symbol.end = layout.directoryRva; + auto& aliases = group.second; + std::sort(aliases.begin(), aliases.end()); + aliases.erase(std::unique(aliases.begin(), aliases.end()), aliases.end()); + if (aliases.size() == 1) { symbol.name = std::move(aliases.front()); symbol.rawName = symbol.name; } + else symbol.aliases = std::move(aliases); + symbol.quality = SymbolQuality::Inferred; + prefixEnd = (std::max)(prefixEnd, symbol.end); + symbol.prefixEnd = prefixEnd; + symbols.push_back(std::move(symbol)); + } + return true; +} + +std::string Hex(const unsigned char* data, size_t count) { + static const char digits[] = "0123456789abcdef"; + std::string text; + text.reserve(count * 2); + for (size_t i = 0; i < count; ++i) { text += digits[data[i] >> 4]; text += digits[data[i] & 15]; } + return text; +} + +bool Sha256(const std::vector& data, std::string& digest) { + BCRYPT_ALG_HANDLE algorithm = nullptr; + BCRYPT_HASH_HANDLE hash = nullptr; + DWORD objectSize = 0, written = 0; + bool okay = BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0) >= 0; + if (okay) okay = BCryptGetProperty(algorithm, BCRYPT_OBJECT_LENGTH, reinterpret_cast(&objectSize), sizeof(objectSize), &written, 0) >= 0; + std::vector object(okay ? objectSize : 0); + std::array result{}; + if (okay) okay = BCryptCreateHash(algorithm, &hash, object.data(), objectSize, nullptr, 0, 0) >= 0; + if (okay) okay = BCryptHashData(hash, const_cast(data.data()), static_cast(data.size()), 0) >= 0; + if (okay) okay = BCryptFinishHash(hash, result.data(), static_cast(result.size()), 0) >= 0; + if (hash) BCryptDestroyHash(hash); + if (algorithm) BCryptCloseAlgorithmProvider(algorithm, 0); + if (okay) digest = Hex(result.data(), result.size()); + return okay; +} + +int Digit(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + return -1; +} + +bool ParseHex(std::string_view text, uint32_t& value) { + if (text.size() != 8) return false; + value = 0; + for (char c : text) { const int digit = Digit(c); if (digit < 0) return false; value = (value << 4) | uint32_t(digit); } + return true; +} + +bool DigestValid(std::string_view digest) { + return digest.size() == 64 && std::all_of(digest.begin(), digest.end(), [](char c) { return Digit(c) >= 0; }); +} + +bool NameFromHex(std::string_view text, std::string& name) { + if (text.empty() || text.size() % 2 || text.size() > MaxNameBytes * 2) return false; + name.reserve(text.size() / 2); + for (size_t i = 0; i < text.size(); i += 2) { + const int high = Digit(text[i]), low = Digit(text[i + 1]); + if (high < 0 || low < 0) return false; + const char c = static_cast((high << 4) | low); + if (c == '\0' || c == '\t' || c == '\r' || c == '\n') return false; + name += c; + } + // Reject malformed UTF-8 before names can reach reports. + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, name.data(), static_cast(name.size()), nullptr, 0) > 0; +} + +SymbolLoadStatus ParseSidecar(const std::vector& data, uint32_t imageSize, + const std::vector
& peSections, const std::string& digest, + std::vector& symbols, SymbolProvenance& provenance) { + const std::string text(data.begin(), data.end()); + std::istringstream input(text); + std::string line; + auto next = [&]() { + if (!std::getline(input, line) || line.size() > MaxLineBytes || line.find('\0') != std::string::npos) return false; + if (!line.empty() && line.back() == '\r') line.pop_back(); + return true; + }; + if (!next() || line != "RSYM1" || !next() || line.rfind("sha256 ", 0) != 0 || !DigestValid(std::string_view(line).substr(7))) return SymbolLoadStatus::InvalidSidecar; + const bool identityMatches = line.substr(7) == digest; + uint32_t declaredSize = 0; + if (!next() || line.rfind("image_size ", 0) != 0 || !ParseHex(std::string_view(line).substr(11), declaredSize)) return SymbolLoadStatus::InvalidSidecar; + if (declaredSize != imageSize) return SymbolLoadStatus::IdentityMismatch; + if (!next() || (line != "source_kind coff" && line != "source_kind map")) return SymbolLoadStatus::InvalidSidecar; + const bool mapSource = line == "source_kind map"; + if (!next() || line.rfind("source_sha256 ", 0) != 0 || !DigestValid(std::string_view(line).substr(14))) return SymbolLoadStatus::InvalidSidecar; + const bool sourceMatches = mapSource || line.substr(14) == digest; + std::vector
sections; + bool ended = false, sawSymbols = false; + while (next()) { + if (line == "end") { ended = true; break; } + std::istringstream row(line); + std::string kind, beginText, endText, quality, name, rawName, extra; + uint32_t begin = 0, end = 0; + if (!(row >> kind >> beginText >> endText) || !ParseHex(beginText, begin) || !ParseHex(endText, end) || begin >= end) return SymbolLoadStatus::InvalidSidecar; + if (kind == "section" && !sawSymbols) { + if (row >> extra || sections.size() >= 96) return SymbolLoadStatus::InvalidSidecar; + sections.push_back({begin, end}); + } else if (kind == "symbol") { + sawSymbols = true; + Symbol symbol; + if (!(row >> quality >> name) || (quality != "E" && quality != "I") || + (mapSource && quality == "E") || symbols.size() >= MaxSymbols || !NameFromHex(name, symbol.name)) return SymbolLoadStatus::InvalidSidecar; + if (row >> rawName) { + if ((row >> extra) || !NameFromHex(rawName, symbol.rawName)) return SymbolLoadStatus::InvalidSidecar; + } else symbol.rawName = symbol.name; // Original RSYM1 records had one name. + if (!std::any_of(peSections.begin(), peSections.end(), [&](const Section& section) { return begin >= section.begin && end <= section.end; })) return SymbolLoadStatus::InvalidSidecar; + symbol.begin = begin; symbol.end = end; + symbol.quality = quality == "E" ? SymbolQuality::Exact : SymbolQuality::Inferred; + symbols.push_back(std::move(symbol)); + } else return SymbolLoadStatus::InvalidSidecar; + } + if (!ended || input.peek() != std::char_traits::eof() || sections.size() != peSections.size()) return SymbolLoadStatus::InvalidSidecar; + for (size_t i = 0; i < sections.size(); ++i) if (sections[i].begin != peSections[i].begin || sections[i].end != peSections[i].end) return SymbolLoadStatus::InvalidSidecar; + if (!identityMatches || !sourceMatches) return SymbolLoadStatus::IdentityMismatch; + provenance = mapSource ? SymbolProvenance::LinkerMap : SymbolProvenance::Coff; + std::sort(symbols.begin(), symbols.end(), [](const Symbol& a, const Symbol& b) { return a.begin < b.begin; }); + uint32_t prefixEnd = 0; + for (auto& symbol : symbols) { prefixEnd = (std::max)(prefixEnd, symbol.end); symbol.prefixEnd = prefixEnd; } + return symbols.empty() ? SymbolLoadStatus::Missing : SymbolLoadStatus::Ready; +} +} // namespace + +struct MeasurementSymbolResolver::Impl { + struct Module { + uintptr_t base = 0; + uint32_t imageSize = 0; + std::string path, digest; + SymbolQuality unavailable = SymbolQuality::Missing; + SymbolProvenance provenance = SymbolProvenance::None; + std::vector symbols; + }; + std::vector modules; +}; + +MeasurementSymbolResolver::MeasurementSymbolResolver() : m_impl(std::make_unique()) {} +MeasurementSymbolResolver::~MeasurementSymbolResolver() = default; + +SymbolLoadStatus MeasurementSymbolResolver::LoadModule(uintptr_t base, std::string_view binaryPath, std::string_view sidecarPath) { + try { + Impl::Module module; + module.base = base; + module.path = FullPathUtf8(binaryPath); + std::vector binary; + std::vector
sections; + if (!ReadFile(std::filesystem::u8path(module.path), MaxBinaryBytes, binary) || !ReadPe(binary, module.imageSize, sections) || + module.imageSize > (std::numeric_limits::max)() - base || !Sha256(binary, module.digest)) return SymbolLoadStatus::BinaryUnavailable; + for (const auto& prior : m_impl->modules) { + if (base < prior.base + prior.imageSize && prior.base < base + module.imageSize) return SymbolLoadStatus::ModuleConflict; + } + std::vector sidecar; + SymbolLoadStatus status = SymbolLoadStatus::Missing; + if (ReadFile(std::filesystem::u8path(sidecarPath), MaxSidecarBytes, sidecar)) { + status = ParseSidecar(sidecar, module.imageSize, sections, module.digest, module.symbols, module.provenance); + } else if (std::filesystem::exists(std::filesystem::u8path(sidecarPath))) status = SymbolLoadStatus::InvalidSidecar; + if (status == SymbolLoadStatus::IdentityMismatch) module.unavailable = SymbolQuality::IdentityMismatch; + else if (status == SymbolLoadStatus::InvalidSidecar) module.unavailable = SymbolQuality::InvalidSidecar; + if (status != SymbolLoadStatus::Ready) module.symbols.clear(); + m_impl->modules.push_back(std::move(module)); + return status; + } catch (const std::exception&) { + return SymbolLoadStatus::BinaryUnavailable; + } +} + +SymbolLoadStatus MeasurementSymbolResolver::LoadExports(uintptr_t base, std::string_view binaryPath) { + try { + Impl::Module module; + module.base = base; + module.path = FullPathUtf8(binaryPath); + std::vector binary; + std::vector
sections; + ExportLayout layout; + if (!ReadFile(std::filesystem::u8path(module.path), MaxBinaryBytes, binary) || !ReadPe(binary, module.imageSize, sections, &layout) || + module.imageSize > (std::numeric_limits::max)() - base || !Sha256(binary, module.digest)) return SymbolLoadStatus::BinaryUnavailable; + for (const auto& prior : m_impl->modules) { + if (base < prior.base + prior.imageSize && prior.base < base + module.imageSize) return SymbolLoadStatus::ModuleConflict; + } + if (!ParseExports(binary, layout, sections, module.imageSize, module.symbols)) return SymbolLoadStatus::BinaryUnavailable; + module.provenance = SymbolProvenance::Exports; + const auto status = module.symbols.empty() ? SymbolLoadStatus::Missing : SymbolLoadStatus::Ready; + m_impl->modules.push_back(std::move(module)); + return status; + } catch (const std::exception&) { + return SymbolLoadStatus::BinaryUnavailable; + } +} + +SymbolResult MeasurementSymbolResolver::Resolve(uintptr_t address) const { + SymbolResult result; + for (const auto& module : m_impl->modules) { + if (address < module.base || address - module.base >= module.imageSize) continue; + result.modulePath = module.path; + result.binarySha256 = module.digest; + result.moduleBase = module.base; + result.rva = static_cast(address - module.base); + result.quality = module.unavailable; + result.provenance = module.provenance; + const auto& symbols = module.symbols; + auto after = std::upper_bound(symbols.begin(), symbols.end(), result.rva, [](uint32_t rva, const Symbol& symbol) { return rva < symbol.begin; }); + const Symbol* found = nullptr; + while (after != symbols.begin()) { + --after; + if (after->prefixEnd <= result.rva) break; + if (after->begin <= result.rva && result.rva < after->end) { + if (found) { result.quality = SymbolQuality::Ambiguous; return result; } + found = &*after; + } + } + if (found) { + result.name = found->name; + result.rawName = found->rawName; + result.offset = result.rva - found->begin; + result.aliases = found->aliases; + result.quality = result.aliases.size() > 1 ? SymbolQuality::Ambiguous : found->quality; + } + return result; + } + return result; +} + +const char* SymbolQualityName(SymbolQuality quality) noexcept { + switch (quality) { + case SymbolQuality::Exact: return "exact"; + case SymbolQuality::Inferred: return "inferred"; + case SymbolQuality::Ambiguous: return "ambiguous"; + case SymbolQuality::Missing: return "missing"; + case SymbolQuality::IdentityMismatch: return "identity-mismatch"; + case SymbolQuality::InvalidSidecar: return "invalid-sidecar"; + default: return "module-not-found"; + } +} + +const char* SymbolLoadStatusName(SymbolLoadStatus status) noexcept { + switch (status) { + case SymbolLoadStatus::Ready: return "ready"; + case SymbolLoadStatus::Missing: return "missing"; + case SymbolLoadStatus::IdentityMismatch: return "identity-mismatch"; + case SymbolLoadStatus::InvalidSidecar: return "invalid-sidecar"; + case SymbolLoadStatus::ModuleConflict: return "module-conflict"; + default: return "binary-unavailable"; + } +} + +const char* SymbolProvenanceName(SymbolProvenance provenance) noexcept { + switch (provenance) { + case SymbolProvenance::Coff: return "coff"; + case SymbolProvenance::LinkerMap: return "linker-map"; + case SymbolProvenance::Exports: return "exports"; + default: return "none"; + } +} +} // namespace BridgeMeasurement diff --git a/src/bridge/legacy/MeasurementSymbols.h b/src/bridge/legacy/MeasurementSymbols.h new file mode 100644 index 0000000..c5560ef --- /dev/null +++ b/src/bridge/legacy/MeasurementSymbols.h @@ -0,0 +1,69 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace BridgeMeasurement { + +enum class SymbolQuality { + Exact, Inferred, Ambiguous, Missing, IdentityMismatch, InvalidSidecar, ModuleNotFound +}; + +enum class SymbolLoadStatus { + Ready, Missing, IdentityMismatch, InvalidSidecar, BinaryUnavailable, ModuleConflict +}; + +// Exports always have inferred range boundaries, including ambiguous aliases. +enum class SymbolProvenance { None, Coff, LinkerMap, Exports }; + +const char* SymbolQualityName(SymbolQuality quality) noexcept; +const char* SymbolLoadStatusName(SymbolLoadStatus status) noexcept; +const char* SymbolProvenanceName(SymbolProvenance provenance) noexcept; + +struct SymbolResult { + std::string modulePath; + std::string binarySha256; + uintptr_t moduleBase = 0; + uint32_t rva = 0; + std::string name; + uint32_t offset = 0; + SymbolQuality quality = SymbolQuality::ModuleNotFound; + SymbolProvenance provenance = SymbolProvenance::None; + // Export names sharing this RVA; populated instead of choosing a primary name. + std::vector aliases; + // Original metadata spelling; name may be a build-time demangled display name. + std::string rawName; +}; + +// Initialize on a background worker before publishing to sampling/report code. +// LoadModule reads/hashes disk files and allocates. A registered module is never +// replaced; missing/invalid sidecars retain its path/base/hash and explicit state. +// Once initialization ends, const Resolve calls are thread safe. Do not call +// LoadModule concurrently with Resolve. Resolve uses only prepared memory, but +// returns owning strings: call after resuming the sampled thread, outside the HUD +// per-frame path. Exact means a COFF explicit size, not an inferred next boundary. +class MeasurementSymbolResolver { +public: + MeasurementSymbolResolver(); + ~MeasurementSymbolResolver(); + MeasurementSymbolResolver(const MeasurementSymbolResolver&) = delete; + MeasurementSymbolResolver& operator=(const MeasurementSymbolResolver&) = delete; + + SymbolLoadStatus LoadModule(uintptr_t base, std::string_view binaryPath, + std::string_view sidecarPath); + // Explicit opt-in for caller-selected system DLLs. Reads the actual PE export + // table on the worker; skips forwarders/non-executable entries, and bounds each + // named range by the next distinct export RVA or executable-section end. + // Never replaces a registered module, even when its sidecar mismatched. + SymbolLoadStatus LoadExports(uintptr_t base, std::string_view binaryPath); + SymbolResult Resolve(uintptr_t address) const; + +private: + struct Impl; + std::unique_ptr m_impl; +}; + +} // namespace BridgeMeasurement diff --git a/src/bridge/legacy/ProperShadersIatHook.h b/src/bridge/legacy/ProperShadersIatHook.h new file mode 100644 index 0000000..8f47ab5 --- /dev/null +++ b/src/bridge/legacy/ProperShadersIatHook.h @@ -0,0 +1,167 @@ +#pragma once + +#include +#include +#include +#include + +namespace ProperShadersPatching +{ +// One process-lifetime hook entry and one immutable original target. Lifecycle +// code must keep both modules mapped and serialize hook teardown with its own +// users. A caller can cache the hook before Restore and enter after it returns, +// so a successful restore never clears or retargets Original(). +class IatHook +{ +public: + using ProtectMemory = decltype(&::VirtualProtect); + + explicit IatHook(ProtectMemory protect = &::VirtualProtect) noexcept + : protect_(protect) + { + } + + bool Install(PVOID volatile* slot, PVOID expectedOriginal, PVOID hook) + { + std::lock_guard lock(mutex_); + error_ = ERROR_SUCCESS; + if (!slot || !expectedOriginal || !hook || expectedOriginal == hook || + reinterpret_cast(slot) % alignof(PVOID) != 0) { + return Fail(ERROR_INVALID_PARAMETER); + } + + const PVOID retainedOriginal = original_.load(std::memory_order_acquire); + if (retainedOriginal && + (retainedOriginal != expectedOriginal || hook_ != hook)) { + return Fail(ERROR_REVISION_MISMATCH); + } + if (slot_ && slot_ != slot) return Fail(ERROR_BUSY); + + PVOID current = nullptr; + if (!ReadSlot(slot, current)) return Fail(GetLastError()); + if (slot_ && current == hook && published_) { + // A previous call may have published the slot but failed to put its + // original protection back. Report success only after that repair. + return RestoreProtection(); + } + if (current != expectedOriginal) return Fail(ERROR_REVISION_MISMATCH); + if (protectionPending_ && !RestoreProtection()) return false; + if (!MakeWritable(slot)) return false; + + // This release store precedes the interlocked publication of the hook. + // The callback takes an acquire snapshot and can immediately call it. + original_.store(expectedOriginal, std::memory_order_release); + hook_ = hook; + const PVOID previous = InterlockedCompareExchangePointer( + slot, hook, expectedOriginal); + const bool installed = previous == expectedOriginal; + if (installed) published_ = true; + + const bool protectedAgain = RestoreProtection(); + if (!installed) { + // The competing owner wins. Keep a failed protection rollback + // tracked so Restore can repair it without overwriting that owner. + if (protectedAgain) slot_ = nullptr; + return Fail(protectedAgain ? ERROR_REVISION_MISMATCH : error_.load()); + } + return protectedAgain; + } + + bool Restore() + { + std::lock_guard lock(mutex_); + error_ = ERROR_SUCCESS; + if (!slot_) return true; + if (!MakeWritable(slot_)) return false; + + const PVOID original = original_.load(std::memory_order_acquire); + const PVOID previous = InterlockedCompareExchangePointer( + slot_, original, hook_); + // If a different owner has already restored the same original, there is + // no pointer work left. An unrelated hook must never be overwritten. + const bool restored = previous == hook_ || previous == original; + const bool protectedAgain = RestoreProtection(); + if (restored && protectedAgain) slot_ = nullptr; + if (!restored) { + return Fail(protectedAgain ? ERROR_REVISION_MISMATCH : error_.load()); + } + return protectedAgain; + } + + PVOID Original() const noexcept + { + return original_.load(std::memory_order_acquire); + } + + bool OwnsSlot() const + { + std::lock_guard lock(mutex_); + PVOID current = nullptr; + return slot_ && published_ && ReadSlot(slot_, current) && current == hook_; + } + + bool HasPendingProtection() const + { + std::lock_guard lock(mutex_); + return protectionPending_; + } + + DWORD Error() const noexcept + { + return error_.load(std::memory_order_relaxed); + } + +private: + static bool ReadSlot(PVOID volatile* slot, PVOID& current) noexcept + { + SIZE_T bytesRead = 0; + return ReadProcessMemory(GetCurrentProcess(), + const_cast(slot), ¤t, sizeof(current), &bytesRead) && + bytesRead == sizeof(current); + } + + bool MakeWritable(PVOID volatile* slot) + { + DWORD previousProtection = 0; + if (!protect_(const_cast(slot), sizeof(PVOID), + PAGE_READWRITE, &previousProtection)) { + return Fail(GetLastError()); + } + // Preserve the first protection until it is actually restored. A retry + // otherwise mistakes PAGE_READWRITE for the original protection. + if (!protectionPending_) originalProtection_ = previousProtection; + slot_ = slot; + protectionPending_ = true; + return true; + } + + bool RestoreProtection() + { + if (!protectionPending_) return true; + DWORD ignored = 0; + if (!protect_(const_cast(slot_), sizeof(PVOID), + originalProtection_, &ignored)) { + return Fail(GetLastError()); + } + protectionPending_ = false; + return true; + } + + bool Fail(DWORD error) noexcept + { + error_.store(error, std::memory_order_relaxed); + SetLastError(error); + return false; + } + + ProtectMemory protect_; + mutable std::mutex mutex_; + std::atomic original_{ nullptr }; + std::atomic error_{ ERROR_SUCCESS }; + PVOID volatile* slot_ = nullptr; + PVOID hook_ = nullptr; + DWORD originalProtection_ = 0; + bool protectionPending_ = false; + bool published_ = false; +}; +} diff --git a/src/bridge/legacy/ProperShadersRenderStateGuard.cpp b/src/bridge/legacy/ProperShadersRenderStateGuard.cpp index 2e1a2a2..efa8010 100644 --- a/src/bridge/legacy/ProperShadersRenderStateGuard.cpp +++ b/src/bridge/legacy/ProperShadersRenderStateGuard.cpp @@ -1,7 +1,7 @@ // ProperShaders render-state guard implementation. // // Ported from FLA++ (FLACompatBridge/BridgeCompatProperShaders.cpp): the three -// ProperShaders patch sites (PS+0x25880 RenderScene, PS+0x25591 +// ProperShaders patch sites (PS+0x25880 RenderScene, PS+0x25590 // RenderFadingEntities, PS+0x25BEB main-scene state restore) are wrapped with // hand-written rel32 hooks, RenderWare's alpha-test states are synchronised // onto the D3D9 device, and the main-scene baseline stack is restored after the @@ -13,6 +13,10 @@ #include "ProperShadersRenderStateGuard.h" #include "ProperShadersStateRing.h" +#include "BridgeCodePatch.h" +#include "BridgeLog.h" +#include +#include #include #include @@ -22,20 +26,14 @@ namespace { template -void Log(std::format_string fmt, Args&&... args) +void Log(std::format_string fmt, Args&&... args) noexcept { static FILE* f = nullptr; if (!f) { f = fopen("scripts\\BridgeD3D9.log", "a"); if (!f) return; } - SYSTEMTIME st; - GetLocalTime(&st); - std::print(f, "[{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] ", - st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); - std::vprint_nonunicode(f, fmt.get(), std::make_format_args(args...)); - fputc('\n', f); - fflush(f); + BridgeDiagnostics::Write(f, fmt, std::forward(args)...); } } // namespace @@ -49,6 +47,10 @@ LONG g_properShadersRenderStateGuardInstallState = 0; LONG g_properShadersFadingStateGuardInstallState = 0; LONG g_properShadersMainSceneStateGuardInstallState = 0; LONG g_properShadersRenderStateGuardApiReady = 0; +// Shader-slot self-check state: validated once per process, and latched off if +// the probe ever says the slots are not the object getters we expect. +LONG g_properShadersShaderSlotsValidated = 0; +LONG g_properShadersShaderSlotsRejected = 0; LONG g_properShadersRenderStateSampleAttempts = 0; LONG g_properShadersRenderStateChangeLogs = 0; LONG g_properShadersFadingStateChangeLogs = 0; @@ -59,6 +61,35 @@ ProperShadersMainSceneBaseline LONG g_properShadersMainSceneBaselineDepth = 0; static bool SynchronizeProperShadersFadingAlphaState(); +static std::array g_guardCode; +static uintptr_t g_guardSites[3]{}; + +struct ProperShadersDeviceMethodCache { + uint32_t generation = 0; + void** renderTable = nullptr; + D3D9RenderStateApi render{}; + void** traceTable = nullptr; + D3D9StateTraceApi trace{}; + bool traceKnown = false; + bool traceValid = false; +}; +static std::atomic g_deviceMethodGeneration{1}; +static thread_local ProperShadersDeviceMethodCache g_deviceMethods; + +void InvalidateProperShadersDeviceApiCache() noexcept +{ + g_deviceMethodGeneration.fetch_add(1, std::memory_order_release); +} + +static ProperShadersDeviceMethodCache& CurrentDeviceMethods() noexcept +{ + const auto generation = g_deviceMethodGeneration.load(std::memory_order_acquire); + if (g_deviceMethods.generation != generation) { + g_deviceMethods = {}; + g_deviceMethods.generation = generation; + } + return g_deviceMethods; +} // ---- configuration ---- @@ -67,6 +98,8 @@ void LoadProperShadersRenderStateGuardConfig(const char* iniPath) if (!iniPath || !iniPath[0]) { return; } + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); g_properShadersRenderStateGuardConfig.enable = GetPrivateProfileIntA("ProperShadersRenderStateGuard", "Enable", 1, iniPath) != 0; @@ -114,21 +147,23 @@ static bool IsExecutableCommitted(uintptr_t address) protect == PAGE_EXECUTE_READWRITE || protect == PAGE_EXECUTE_WRITECOPY; } +// Prepared at owner-thread safe points; never writes from a watcher thread. static bool WriteBytesWithProtect(uintptr_t destination, const uint8_t* bytes, size_t size) { - DWORD oldProtect = 0; - if (!VirtualProtect(reinterpret_cast(destination), size, PAGE_EXECUTE_READWRITE, &oldProtect)) { - Log("patch: protect failed address=0x{:08X} size=0x{:X} gle={}", - destination, static_cast(size), GetLastError()); - return false; - } - - std::memcpy(reinterpret_cast(destination), bytes, size); - - DWORD ignored = 0; - VirtualProtect(reinterpret_cast(destination), size, oldProtect, &ignored); - FlushInstructionCache(GetCurrentProcess(), reinterpret_cast(destination), size); - return true; + for (size_t i = 0; i < g_guardCode.size(); ++i) { + if (g_guardSites[i] && g_guardSites[i] != destination) continue; + g_guardSites[i] = destination; + uint8_t expected[16]{}; + std::memcpy(expected, reinterpret_cast(destination), size); + const bool installed = g_guardCode[i].Install( + reinterpret_cast(destination), expected, bytes, size); + if (!installed) Log("guard patch: pending address=0x{:08X} err={} owned={} protectionPending={}", + destination, g_guardCode[i].Error(), g_guardCode[i].OwnsBytes(), g_guardCode[i].HasPendingProtection()); + // A failed cleanup can leave the jump live. Retain the trampoline + // until rollback succeeds rather than freeing a reachable gateway. + return installed || g_guardCode[i].OwnsBytes(); + } + return false; } static uintptr_t DecodeRel32JumpTarget(uintptr_t address) @@ -154,8 +189,24 @@ static uintptr_t DecodeRel32JumpTarget(uintptr_t address) return address + 5 + rel; } -static bool WriteRel32Jump(uintptr_t source, uintptr_t target) +// Writes E9 at `source` and pads the rest of `patchSize` with NOPs. +// +// patchSize MUST equal the number of bytes the matching trampoline stole. A +// rel32 jump is only 5 bytes, so stealing 6 or 7 bytes and writing 5 leaves the +// tail of the original instruction in place as an orphan: execution that +// reaches source+5 runs a fragment of a dead instruction. That is what broke +// the RenderScene site (prologue `55 8B EC 83 E4 F8`, where the stranded `F8` +// followed a stolen `and esp,-8`) and the main-scene site (7-byte `cmp`, two +// stranded zero bytes decoding as `add [eax],al`). +static bool WriteRel32Jump(uintptr_t source, uintptr_t target, size_t patchSize = 5) { + constexpr size_t kMaxPatchSize = 16; + if (patchSize < 5 || patchSize > kMaxPatchSize) { + Log("bridge stub: invalid rel32 patch size source=0x{:08X} size={}", + source, static_cast(patchSize)); + return false; + } + const int64_t diff = static_cast(target) - static_cast(source + 5); if (diff < INT32_MIN || diff > INT32_MAX) { Log("bridge stub: rel32 jump out of range source=0x{:08X} target=0x{:08X} diff={}", @@ -163,10 +214,12 @@ static bool WriteRel32Jump(uintptr_t source, uintptr_t target) return false; } - uint8_t bytes[5]{ 0xE9, 0, 0, 0, 0 }; + uint8_t bytes[kMaxPatchSize]; + std::memset(bytes, 0x90, sizeof(bytes)); + bytes[0] = 0xE9; const int32_t rel = static_cast(diff); std::memcpy(bytes + 1, &rel, sizeof(rel)); - return WriteBytesWithProtect(source, bytes, sizeof(bytes)); + return WriteBytesWithProtect(source, bytes, patchSize); } static uintptr_t CreateRel32Trampoline(uintptr_t source, size_t stolenBytes) @@ -307,6 +360,22 @@ static bool IsSupportedProperShadersTextHash(uint32_t hash) static bool ResolveD3D9RenderStateApi(D3D9RenderStateApi* api) { + if (!api) return false; + auto& cache = CurrentDeviceMethods(); + __try { + void* device = *reinterpret_cast(kRwD3D9DevicePointer); + void** table = device ? *reinterpret_cast(device) : nullptr; + if (device && cache.render.device == device && cache.renderTable == table && + table[kD3D9SetRenderStateVtableIndex] == reinterpret_cast(cache.render.setRenderState) && + table[kD3D9GetRenderStateVtableIndex] == reinterpret_cast(cache.render.getRenderState)) { + *api = cache.render; + return true; + } + } __except (EXCEPTION_EXECUTE_HANDLER) { + cache.render = {}; cache.traceKnown = false; + return false; + } + cache.render = {}; cache.traceKnown = false; if (!api || !IsReadableCommitted(kRwD3D9DevicePointer, sizeof(uintptr_t))) { return false; } @@ -336,6 +405,8 @@ static bool ResolveD3D9RenderStateApi(D3D9RenderStateApi* api) api->device = device; api->setRenderState = setRenderState; api->getRenderState = getRenderState; + cache.render = *api; + cache.renderTable = vtable; } __except (EXCEPTION_EXECUTE_HANDLER) { return false; @@ -358,6 +429,7 @@ static bool ReadD3D9DeviceRenderState(uint32_t state, uint32_t* value) return api.getRenderState(api.device, state, value) >= 0; } __except (EXCEPTION_EXECUTE_HANDLER) { + InvalidateProperShadersDeviceApiCache(); return false; } } @@ -391,6 +463,7 @@ static bool SynchronizeD3D9DeviceRenderState( return true; } __except (EXCEPTION_EXECUTE_HANDLER) { + InvalidateProperShadersDeviceApiCache(); return false; } } @@ -406,27 +479,55 @@ static bool ResolveD3D9StateTraceApi(D3D9StateTraceApi* api) return false; } + auto& cache = CurrentDeviceMethods(); + __try { void** vtable = *reinterpret_cast(renderStateApi.device); - constexpr size_t kRequiredVtableEntries = 112; + // Read the live table each time: replacing a method in-place must not + // keep a previously validated pointer alive in this cache. + D3D9StateTraceApi live{}; + live.device = renderStateApi.device; + live.getRenderState = renderStateApi.getRenderState; + live.getRenderTarget = reinterpret_cast(vtable[kD3D9GetRenderTargetVtableIndex]); + live.getDepthStencilSurface = reinterpret_cast(vtable[kD3D9GetDepthStencilSurfaceVtableIndex]); + live.getViewport = reinterpret_cast(vtable[kD3D9GetViewportVtableIndex]); + live.getTexture = reinterpret_cast(vtable[kD3D9GetTextureVtableIndex]); + live.getTextureStageState = reinterpret_cast(vtable[kD3D9GetTextureStageStateVtableIndex]); + live.getSamplerState = reinterpret_cast(vtable[kD3D9GetSamplerStateVtableIndex]); + live.getVertexShader = reinterpret_cast(vtable[kD3D9GetVertexShaderVtableIndex]); + live.getPixelShader = reinterpret_cast(vtable[kD3D9GetPixelShaderVtableIndex]); + if (cache.traceKnown && cache.traceTable == vtable && + std::memcmp(&cache.trace, &live, sizeof(live)) == 0) { + if (cache.traceValid) *api = cache.trace; + return cache.traceValid; + } + cache.traceKnown = false; if (!vtable || !IsReadableCommitted( reinterpret_cast(vtable), - kRequiredVtableEntries * sizeof(void*))) { + kD3D9RequiredVtableEntries * sizeof(void*))) { return false; } api->device = renderStateApi.device; api->getRenderState = renderStateApi.getRenderState; - api->getRenderTarget = reinterpret_cast(vtable[38]); + api->getRenderTarget = reinterpret_cast( + vtable[kD3D9GetRenderTargetVtableIndex]); api->getDepthStencilSurface = - reinterpret_cast(vtable[40]); - api->getViewport = reinterpret_cast(vtable[48]); - api->getTexture = reinterpret_cast(vtable[64]); + reinterpret_cast( + vtable[kD3D9GetDepthStencilSurfaceVtableIndex]); + api->getViewport = reinterpret_cast( + vtable[kD3D9GetViewportVtableIndex]); + api->getTexture = reinterpret_cast( + vtable[kD3D9GetTextureVtableIndex]); api->getTextureStageState = - reinterpret_cast(vtable[66]); - api->getSamplerState = reinterpret_cast(vtable[68]); - api->getVertexShader = reinterpret_cast(vtable[94]); - api->getPixelShader = reinterpret_cast(vtable[111]); + reinterpret_cast( + vtable[kD3D9GetTextureStageStateVtableIndex]); + api->getSamplerState = reinterpret_cast( + vtable[kD3D9GetSamplerStateVtableIndex]); + api->getVertexShader = reinterpret_cast( + vtable[kD3D9GetVertexShaderVtableIndex]); + api->getPixelShader = reinterpret_cast( + vtable[kD3D9GetPixelShaderVtableIndex]); const uintptr_t functions[] = { reinterpret_cast(api->getRenderTarget), @@ -443,8 +544,42 @@ static bool ResolveD3D9StateTraceApi(D3D9StateTraceApi* api) return false; } } + + // Runtime proof that the two shader slots really are the object getters + // and not the neighbouring constant getters. Both signatures start with + // an out-pointer, so a wrong slot would take our pointer as a register + // index and write through uninitialised stack. Probing with a null + // out-pointer is the cheap discriminator: the object getters reject it + // with D3DERR_INVALIDCALL and touch nothing, while the constant getters + // would fail on the count argument instead. Either way nothing is + // written, and a slot that returns success here is not what we want. + { + void* probe = nullptr; + const LONG vsProbe = api->getVertexShader(api->device, nullptr); + const LONG psProbe = api->getPixelShader(api->device, nullptr); + if (vsProbe >= 0 || psProbe >= 0) { + Log("proper shaders state trace: shader slot self-check failed vs={:#010X} ps={:#010X} (vsSlot={} psSlot={}); snapshots disabled", + static_cast(vsProbe), + static_cast(psProbe), + static_cast(kD3D9GetVertexShaderVtableIndex), + static_cast(kD3D9GetPixelShaderVtableIndex)); + InterlockedExchange(&g_properShadersShaderSlotsRejected, 1); + cache.trace = *api; cache.traceTable = vtable; + cache.traceKnown = true; cache.traceValid = false; + return false; + } + (void)probe; + InterlockedExchange(&g_properShadersShaderSlotsValidated, 1); + InterlockedExchange(&g_properShadersShaderSlotsRejected, 0); + } + if (InterlockedCompareExchange(&g_properShadersShaderSlotsRejected, 0, 0) != 0) { + return false; + } + cache.trace = *api; cache.traceTable = vtable; + cache.traceKnown = true; cache.traceValid = true; } __except (EXCEPTION_EXECUTE_HANDLER) { + cache.traceKnown = false; return false; } return true; @@ -461,7 +596,7 @@ static void ReleaseD3D9StateTraceObject(void* object) if (!vtable || !IsReadableCommitted(reinterpret_cast(vtable), 3 * sizeof(void*))) { return; } - auto release = reinterpret_cast(vtable[2]); + auto release = reinterpret_cast(vtable[kD3D9ReleaseVtableIndex]); if (release && IsExecutableCommitted(reinterpret_cast(release))) { release(object); } @@ -593,6 +728,7 @@ bool CaptureProperShadersD3D9StateSnapshot(ProperShadersD3D9StateSnapshot* snaps } } __except (EXCEPTION_EXECUTE_HANDLER) { + InvalidateProperShadersDeviceApiCache(); return false; } return true; @@ -643,6 +779,7 @@ static bool SynchronizeD3D9DeviceAlphaRenderStates( } } __except (EXCEPTION_EXECUTE_HANDLER) { + InvalidateProperShadersDeviceApiCache(); return false; } @@ -822,7 +959,7 @@ extern "C" void __cdecl Bridge_ProperShadersPatchedRenderSceneGuard() return; } - const bool traceEnabled = g_properShadersRenderStateGuardConfig.enableStateRing; + const bool traceEnabled = IsProperShadersStateTraceCaptureActive(); ProperShadersStateTraceRecord traceRecord{}; if (traceEnabled) { InitializeProperShadersStateTraceRecord( @@ -1018,7 +1155,7 @@ static bool SynchronizeProperShadersFadingAlphaState() extern "C" void __cdecl Bridge_ProperShadersRenderFadingEntitiesGuard() { - const bool traceEnabled = g_properShadersRenderStateGuardConfig.enableStateRing; + const bool traceEnabled = IsProperShadersStateTraceCaptureActive(); ProperShadersStateTraceRecord traceRecord{}; if (traceEnabled) { InitializeProperShadersStateTraceRecord( @@ -1105,7 +1242,7 @@ static void InstallProperShadersRenderStateGuard(HMODULE psAsi, uint32_t psTextH g_properShadersPatchedRenderSceneTrampoline = trampoline; InterlockedExchange(&g_properShadersRenderStateGuardApiReady, 1); - if (!WriteRel32Jump(patchAddress, guardTarget)) { + if (!WriteRel32Jump(patchAddress, guardTarget, sizeof(expected))) { InterlockedExchange(&g_properShadersRenderStateGuardApiReady, 0); g_properShadersPatchedRenderSceneTrampoline = 0; VirtualFree(reinterpret_cast(trampoline), 0, MEM_RELEASE); @@ -1134,7 +1271,10 @@ static void InstallProperShadersFadingStateGuard(HMODULE psAsi, uint32_t psTextH } const uintptr_t psBase = reinterpret_cast(psAsi); - const uintptr_t patchAddress = psBase + 0x25591; + // Hook the complete function entry. Its initial push ecx is paired with + // pop ecx before ret; entering after that push makes the gateway consume + // the guard's return address and return into its saved registers/locals. + const uintptr_t patchAddress = psBase + 0x25590; const uintptr_t guardTarget = reinterpret_cast(Bridge_ProperShadersRenderFadingEntitiesGuard); if (DecodeRel32JumpTarget(patchAddress) == guardTarget) { InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 2); @@ -1145,7 +1285,7 @@ static void InstallProperShadersFadingStateGuard(HMODULE psAsi, uint32_t psTextH return; } - uint8_t current[5]{}; + uint8_t current[6]{}; bool readable = IsReadableCommitted(patchAddress, sizeof(current)); if (readable) { __try { @@ -1158,12 +1298,12 @@ static void InstallProperShadersFadingStateGuard(HMODULE psAsi, uint32_t psTextH uint32_t pushedString = 0; if (readable) { - std::memcpy(&pushedString, current + 1, sizeof(pushedString)); + std::memcpy(&pushedString, current + 2, sizeof(pushedString)); } const uintptr_t expectedString = psBase + 0x14156C; - if (!readable || current[0] != 0x68 || pushedString != expectedString) { - Log("proper shaders fading-state guard: signature mismatch PS+0x25591 got={:02X} {:02X} {:02X} {:02X} {:02X} pushed=0x{:08X} expected=0x{:08X} textHash=0x{:08X}", - current[0], current[1], current[2], current[3], current[4], + if (!readable || current[0] != 0x51 || current[1] != 0x68 || pushedString != expectedString) { + Log("proper shaders fading-state guard: signature mismatch PS+0x25590 got={:02X} {:02X} {:02X} {:02X} {:02X} {:02X} pushed=0x{:08X} expected=0x{:08X} textHash=0x{:08X}", + current[0], current[1], current[2], current[3], current[4], current[5], pushedString, expectedString, psTextHash); InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 0); return; @@ -1177,14 +1317,14 @@ static void InstallProperShadersFadingStateGuard(HMODULE psAsi, uint32_t psTextH const uintptr_t trampoline = CreateRel32Trampoline(patchAddress, sizeof(current)); if (!trampoline) { - Log("proper shaders fading-state guard: trampoline creation failed PS+0x25591"); + Log("proper shaders fading-state guard: trampoline creation failed PS+0x25590"); InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 0); return; } g_properShadersRenderFadingEntitiesTrampoline = trampoline; InterlockedExchange(&g_properShadersRenderStateGuardApiReady, 1); - if (!WriteRel32Jump(patchAddress, guardTarget)) { + if (!WriteRel32Jump(patchAddress, guardTarget, sizeof(current))) { g_properShadersRenderFadingEntitiesTrampoline = 0; VirtualFree(reinterpret_cast(trampoline), 0, MEM_RELEASE); InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 0); @@ -1192,7 +1332,7 @@ static void InstallProperShadersFadingStateGuard(HMODULE psAsi, uint32_t psTextH } InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 2); - Log("proper shaders fading-state guard: installed PS+0x25591 trampoline=0x{:08X} guard=0x{:08X} textHash=0x{:08X}", + Log("proper shaders fading-state guard: installed PS+0x25590 trampoline=0x{:08X} guard=0x{:08X} textHash=0x{:08X}", trampoline, guardTarget, psTextHash); #else (void)psAsi; @@ -1254,7 +1394,7 @@ static void InstallProperShadersMainSceneStateGuard(HMODULE psAsi, uint32_t psTe } g_properShadersMainSceneStateTrampoline = trampoline; - if (!WriteRel32Jump(patchAddress, guardTarget)) { + if (!WriteRel32Jump(patchAddress, guardTarget, sizeof(expected))) { g_properShadersMainSceneStateTrampoline = 0; VirtualFree(reinterpret_cast(trampoline), 0, MEM_RELEASE); InterlockedExchange(&g_properShadersMainSceneStateGuardInstallState, 0); @@ -1290,6 +1430,9 @@ static void InstallProperShadersRenderStateGuards(HMODULE psAsi) return; } + HMODULE pinned = nullptr; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(psAsi), &pinned)) return; InstallProperShadersRenderStateGuard(psAsi, psTextHash); InstallProperShadersFadingStateGuard(psAsi, psTextHash); InstallProperShadersMainSceneStateGuard(psAsi, psTextHash); @@ -1300,55 +1443,44 @@ static void InstallProperShadersRenderStateGuards(HMODULE psAsi) // ---- activation ---- -static DWORD WINAPI ProperShadersRenderStateGuardWatchThread(void*) +void PollProperShadersRenderStateGuards() { - bool touchedOnce = false; - bool delayedAttemptDone = false; - const ULONGLONG startTick = GetTickCount64(); - - for (int attempt = 0; attempt < 400; ++attempt) { - HMODULE psAsi = GetModuleHandleA("ProperShaders.asi"); - if (!psAsi) { - psAsi = GetModuleHandleA("propershaders.asi"); - } - if (psAsi) { - // FLA++ installed once from its polling thread and once more from a - // delayed direct call; the installers are idempotent, so the second - // attempt is only a safety net for a partially failed first pass. - const bool delayedAttempt = - !delayedAttemptDone && (GetTickCount64() - startTick) >= 1500; - if (!touchedOnce || delayedAttempt) { - InstallProperShadersRenderStateGuards(psAsi); - touchedOnce = true; - if (delayedAttempt) { - delayedAttemptDone = true; - } - } - } - - Sleep(25); - } - - return 0; + MaybeDumpProperShadersStateTraceRing(); + if (!g_properShadersRenderStateGuardConfig.enable) return; + if (g_properShadersRenderStateGuardInstallState == 2 && + g_properShadersFadingStateGuardInstallState == 2 && + g_properShadersMainSceneStateGuardInstallState == 2) return; + static ULONGLONG nextAttempt = 0; + const ULONGLONG now = GetTickCount64(); + if (now < nextAttempt) return; + nextAttempt = now + 1000; + HMODULE module = GetModuleHandleA("ProperShaders.asi"); + if (!module) module = GetModuleHandleA("propershaders.asi"); + if (module) InstallProperShadersRenderStateGuards(module); } -void StartProperShadersRenderStateGuardWatch() +bool ShutdownProperShadersRenderStateGuards() { - if (!g_properShadersRenderStateGuardConfig.enable) { - return; - } - - static LONG started = 0; - if (InterlockedCompareExchange(&started, 1, 0) != 0) { - return; - } - - HANDLE thread = CreateThread( - nullptr, 0, ProperShadersRenderStateGuardWatchThread, nullptr, 0, nullptr); - if (thread) { - CloseHandle(thread); - Log("propershaders guard: watch thread started"); - } else { - Log("propershaders guard: watch thread CreateThread failed err={}", GetLastError()); + InvalidateProperShadersDeviceApiCache(); + ShutdownProperShadersStateRing(); + bool restored = true; + for (auto& patch : g_guardCode) { + if (!patch.Restore()) { + restored = false; + Log("guard patch: restore pending err={} owned={} protectionPending={}", + patch.Error(), patch.OwnsBytes(), patch.HasPendingProtection()); + } } + if (!restored) return false; + uintptr_t* trampolines[] = { &g_properShadersPatchedRenderSceneTrampoline, + &g_properShadersRenderFadingEntitiesTrampoline, &g_properShadersMainSceneStateTrampoline }; + for (auto* trampoline : trampolines) { + if (*trampoline) VirtualFree(reinterpret_cast(*trampoline), 0, MEM_RELEASE); + *trampoline = 0; + } + InterlockedExchange(&g_properShadersRenderStateGuardApiReady, 0); + InterlockedExchange(&g_properShadersRenderStateGuardInstallState, 0); + InterlockedExchange(&g_properShadersFadingStateGuardInstallState, 0); + InterlockedExchange(&g_properShadersMainSceneStateGuardInstallState, 0); + return true; } diff --git a/src/bridge/legacy/ProperShadersRenderStateGuard.h b/src/bridge/legacy/ProperShadersRenderStateGuard.h index 0dede7d..423a4e2 100644 --- a/src/bridge/legacy/ProperShadersRenderStateGuard.h +++ b/src/bridge/legacy/ProperShadersRenderStateGuard.h @@ -32,8 +32,13 @@ extern ProperShadersRenderStateGuardConfig g_properShadersRenderStateGuardConfig void LoadProperShadersRenderStateGuardConfig(const char* iniPath); -// ---- activation (called once the D3D9 device exists) ---- -void StartProperShadersRenderStateGuardWatch(); +// Owner render-thread safe points only. No polling thread writes instructions. +void PollProperShadersRenderStateGuards(); +// Call only after all device/render callbacks are quiescent. Failed restores +// retain patch/trampoline ownership so the caller may retry. +bool ShutdownProperShadersRenderStateGuards(); +// Borrowed method pointers only; no device/resource references or state values. +void InvalidateProperShadersDeviceApiCache() noexcept; // ---- state-ring lifecycle (process detach) ---- void ShutdownProperShadersStateRing(); @@ -46,13 +51,56 @@ constexpr uint32_t kSupportedProperShadersTextHash = 0x526DBD01; constexpr uintptr_t kRwD3D9SetRenderState = 0x007FC2D0; constexpr uintptr_t kRwD3D9GetRenderState = 0x007FC320; constexpr uintptr_t kRwD3D9DevicePointer = 0x00C97C28; -// IDirect3DDevice9 vtable slots (0-based): SetRenderState=57, GetRenderState=58. -// The FLA++ original resolved BOTH entries from the get slot, which made every -// device-side "set" call a no-op GetRenderState. Batch 4 fixes the set entry to -// resolve from its own slot (behaviour change vs the original, dormant while -// the text-hash gate rejects the current ProperShaders build). +// ---- IDirect3DDevice9 vtable slots (0-based, IUnknown occupies 0..2) ---- +// +// Every index below was cross-checked against five independent declarations of +// IDirect3DDevice9 (Windows SDK 10.0.26100 and 10.0.28000, plugin-sdk DXSDK, +// and the DXVK backend/2.7.1 native headers), which agree on all of them. +// +// Do NOT hand-count these. Two of them were previously off (GetVertexShader +// pointed at GetVertexShaderConstantF, GetPixelShader at GetPixelShaderConstantI), +// so a snapshot read called a three-argument constant getter through a +// two-argument signature and wrote float/int data through a stack pointer +// reinterpreted as a register index. Derive any new slot from a header, then +// add it to the static_assert block that follows. +// +// Batch 4 note: the FLA++ original resolved both the set and the get entry from +// slot 58, making every device-side "set" a no-op; the set entry now resolves +// from slot 57 as the layout requires. constexpr size_t kD3D9SetRenderStateVtableIndex = 57; constexpr size_t kD3D9GetRenderStateVtableIndex = 58; +constexpr size_t kD3D9GetRenderTargetVtableIndex = 38; +constexpr size_t kD3D9GetDepthStencilSurfaceVtableIndex = 40; +constexpr size_t kD3D9GetViewportVtableIndex = 48; +constexpr size_t kD3D9GetTextureVtableIndex = 64; +constexpr size_t kD3D9GetTextureStageStateVtableIndex = 66; +constexpr size_t kD3D9GetSamplerStateVtableIndex = 68; +constexpr size_t kD3D9GetVertexShaderVtableIndex = 93; +constexpr size_t kD3D9GetPixelShaderVtableIndex = 108; +constexpr size_t kD3D9ReleaseVtableIndex = 2; + +// Highest slot the guard dereferences, plus one: the readability probe must +// cover every index above, so keep this derived rather than hand-written. +constexpr size_t kD3D9RequiredVtableEntries = + (kD3D9GetPixelShaderVtableIndex > kD3D9GetVertexShaderVtableIndex + ? kD3D9GetPixelShaderVtableIndex + : kD3D9GetVertexShaderVtableIndex) + 1; + +// Slot ordering invariants taken straight from the interface declaration. If a +// future edit renumbers one of these, the build fails here instead of writing +// through a mismatched function signature at runtime. +static_assert(kD3D9GetRenderStateVtableIndex == kD3D9SetRenderStateVtableIndex + 1, + "GetRenderState directly follows SetRenderState in IDirect3DDevice9"); +static_assert(kD3D9GetDepthStencilSurfaceVtableIndex == kD3D9GetRenderTargetVtableIndex + 2, + "GetRenderTarget and GetDepthStencilSurface are two slots apart"); +static_assert(kD3D9GetTextureStageStateVtableIndex == kD3D9GetTextureVtableIndex + 2, + "GetTexture and GetTextureStageState are two slots apart"); +static_assert(kD3D9GetSamplerStateVtableIndex == kD3D9GetTextureStageStateVtableIndex + 2, + "GetTextureStageState and GetSamplerState are two slots apart"); +static_assert(kD3D9GetVertexShaderVtableIndex == 93 && kD3D9GetPixelShaderVtableIndex == 108, + "shader getters verified against five IDirect3DDevice9 declarations"); +static_assert(kD3D9RequiredVtableEntries == 109, + "readability probe must span every slot the guard dereferences"); constexpr uint32_t kProperShadersAlphaRenderStates[] = { 15, // D3DRS_ALPHATESTENABLE 19, // D3DRS_SRCBLEND @@ -201,6 +249,7 @@ enum ProperShadersStateTraceKind : uint32_t }; struct ProperShadersStateTraceRecord { + uint32_t captureGeneration = 0; uint32_t sequence = 0; uint32_t kind = 0; uint32_t threadId = 0; diff --git a/src/bridge/legacy/ProperShadersStateJournal.h b/src/bridge/legacy/ProperShadersStateJournal.h index 879cf55..e6d3fdb 100644 --- a/src/bridge/legacy/ProperShadersStateJournal.h +++ b/src/bridge/legacy/ProperShadersStateJournal.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -95,6 +96,78 @@ struct JournalProbeTransactionInfo bool native = false; }; +#define JOURNAL_ATTRIBUTION_SUMMARY_VERSION 1 +#define JOURNAL_ATTRIBUTION_REPORT_WRITER_VERSION 1 +#define JOURNAL_ATTRIBUTION_CLOSE_ADMISSION_VERSION 1 + +inline constexpr unsigned kJournalAttributionSummaryTechniques = 8; +inline constexpr unsigned kJournalAttributionSummaryFrames = 1024; + +struct JournalAttributionTechniqueSummary +{ + char name[64]{}; + std::uint64_t transactions = 0; + std::uint64_t records = 0; + std::uint64_t nativeTransactions = 0; + std::uint64_t localTransactions = 0; + std::uint64_t emptyTransactions = 0; + std::uint64_t truncatedTransactions = 0; + std::uint64_t transactionSpanQpc = 0; + std::uint64_t maxTransactionSpanQpc = 0; + std::uint64_t restoreQpc = 0; + std::uint64_t maxRestoreQpc = 0; + std::uint64_t restoreCount = 0; + unsigned passCount = 0; +}; + +struct JournalAttributionFrameSummary +{ + std::uint64_t frame = 0; // Zero-based transaction admission frame. + std::uint64_t transactions = 0; + std::uint64_t records = 0; + std::uint64_t truncatedTransactions = 0; + std::uint64_t transactionSpanQpc = 0; + std::uint64_t restoreQpc = 0; + std::uint64_t restoreCount = 0; +}; + +// CPU elapsed observations, not exclusive function costs or GPU/frame totals. +// transactionSpanQpc includes work between BeginTransaction and probe flush; +// restoreQpc includes nested work in Restore and remains a separate measurement. +struct JournalAttributionSummary +{ + std::uint64_t captureId = 0; + std::uint64_t qpcFrequency = 0; + std::uint64_t frames = 0; // Present boundaries observed during this window. + std::uint64_t presents = 0; + std::uint64_t transactions = 0; + std::uint64_t records = 0; + std::uint64_t nativeTransactions = 0; + std::uint64_t localTransactions = 0; + std::uint64_t emptyTransactions = 0; + std::uint64_t truncatedTransactions = 0; + std::uint64_t transactionSpanQpc = 0; + std::uint64_t maxTransactionSpanQpc = 0; + std::uint64_t restoreQpc = 0; + std::uint64_t maxRestoreQpc = 0; + std::uint64_t restoreCount = 0; + std::uint64_t droppedTechniques = 0; // Transactions lacking a technique slot. + std::uint64_t droppedPasses = 0; // Records lacking a technique/pass slot. + std::uint64_t droppedFrames = 0; // Frame slots beyond the bounded prefix. + std::uint64_t orphanRestores = 0; + unsigned techniqueCount = 0; + unsigned passCount = 0; // Distinct technique/pass pairs, includes Begin state. + unsigned inFlight = 0; + unsigned topTechniqueCount = 0; + unsigned frameStatCount = 0; + bool captureStarted = false; + bool active = false; + bool available = false; // At least one transaction observed, including empty. + bool timingAvailable = false; + JournalAttributionTechniqueSummary topTechniques[kJournalAttributionSummaryTechniques]{}; + JournalAttributionFrameSummary frameStats[kJournalAttributionSummaryFrames]{}; +}; + // A getter the journal can call to read one constant range back from the // device: invocable as getter(startRegister, values, count) and returning // something convertible to HRESULT. @@ -110,7 +183,21 @@ void JournalAttributionObserveTransaction(const char* techniqueName, void JournalAttributionObserveRestore(std::uint64_t sequence, std::uint64_t qpcTicks); void JournalAttributionReset(); +// Set while inactive before StartCapture; ignored during an active capture. +// StartCapture resets observations but preserves the assigned identity. +void JournalAttributionSetCaptureIdentity(std::uint64_t captureId); +// Bounded copied snapshot. Call after StopCapture for a complete result; +// active snapshots explicitly expose inFlight and may still be incomplete. +bool JournalAttributionGetSummary(JournalAttributionSummary* summary); +// Writes the complete existing technique/pass/operation report into the caller's +// file under the attribution shared lock. Returns false on null/output failure. +bool JournalAttributionWriteReport(std::FILE* report); void JournalAttributionStartCapture(); +// Closes future admission without waiting for admitted transactions. Device +// lifecycle/Present callers must use this before deferring their drain/report. +void JournalAttributionCloseAdmission(); +// Legacy synchronous close and drain. Do not call while holding a device lock +// needed by Restore, or on an owner thread with an admitted transaction. void JournalAttributionStopCapture(); bool JournalAttributionTryBeginTransaction(); void JournalAttributionEndTransaction(); diff --git a/src/bridge/legacy/ProperShadersStateRing.cpp b/src/bridge/legacy/ProperShadersStateRing.cpp index 3b9fad1..7ec485c 100644 --- a/src/bridge/legacy/ProperShadersStateRing.cpp +++ b/src/bridge/legacy/ProperShadersStateRing.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace { @@ -39,8 +40,36 @@ ProperShadersStateTraceRecord* g_properShadersStateTraceRing = nullptr; uint32_t g_properShadersStateTraceSequence = 0; LONG g_properShadersStateTraceCaptureId = 0; LONG g_properShadersStateTraceTriggerDown = 0; +std::atomic g_properShadersStateTraceActive{false}; +std::atomic g_properShadersStateTraceGeneration{0}; +std::atomic g_properShadersStateTraceDeadline{0}; +constexpr ULONGLONG kManualCaptureMs = 1000; } // namespace +bool IsProperShadersStateTraceCaptureActive() noexcept +{ + return g_properShadersRenderStateGuardConfig.enableStateRing && + g_properShadersStateTraceActive.load(std::memory_order_acquire) && + GetTickCount64() < g_properShadersStateTraceDeadline.load(std::memory_order_relaxed); +} + +bool BeginProperShadersStateTraceCapture() +{ + if (!g_properShadersRenderStateGuardConfig.enableStateRing) return false; + AcquireSRWLockExclusive(&g_properShadersStateTraceLock); + if (g_properShadersStateTraceActive.load(std::memory_order_acquire)) { + ReleaseSRWLockExclusive(&g_properShadersStateTraceLock); + return false; + } + g_properShadersStateTraceGeneration.fetch_add(1, std::memory_order_relaxed); + g_properShadersStateTraceSequence = 0; + g_properShadersStateTraceDeadline.store(GetTickCount64() + kManualCaptureMs, std::memory_order_relaxed); + g_properShadersStateTraceActive.store(true, std::memory_order_release); + ReleaseSRWLockExclusive(&g_properShadersStateTraceLock); + Log("proper shaders state ring: on-demand capture started durationMs={}", kManualCaptureMs); + return true; +} + static const char* ProperShadersStateTraceKindName(uint32_t kind) { switch (kind) { @@ -212,31 +241,42 @@ void DumpProperShadersStateTraceRing() void MaybeDumpProperShadersStateTraceRing() { - if (!g_properShadersRenderStateGuardConfig.enableStateRing) { - return; - } - const bool triggerDown = (GetAsyncKeyState(g_properShadersRenderStateGuardConfig.stateRingTriggerVirtualKey) & 0x8000) != 0; - if (!triggerDown) { - InterlockedExchange(&g_properShadersStateTraceTriggerDown, 0); + const bool pressed = InterlockedExchange(&g_properShadersStateTraceTriggerDown, triggerDown ? 1 : 0) == 0 && triggerDown; + if (!g_properShadersRenderStateGuardConfig.enableStateRing) { + g_properShadersStateTraceActive.store(false, std::memory_order_release); return; } - if (InterlockedCompareExchange(&g_properShadersStateTraceTriggerDown, 1, 0) != 0) { + const auto now = GetTickCount64(); + const bool wasActive = g_properShadersStateTraceActive.load(std::memory_order_acquire); + if (wasActive) { + if (now >= g_properShadersStateTraceDeadline.load(std::memory_order_relaxed)) { + g_properShadersStateTraceActive.store(false, std::memory_order_release); + DumpProperShadersStateTraceRing(); + } + // A key edge during the active window is consumed, never deferred. return; } - DumpProperShadersStateTraceRing(); + if (!pressed) return; + BeginProperShadersStateTraceCapture(); } void StoreProperShadersStateTraceRecord(ProperShadersStateTraceRecord* record) { - if (!record || !g_properShadersRenderStateGuardConfig.enableStateRing) { + if (!record || !IsProperShadersStateTraceCaptureActive() || + record->captureGeneration != g_properShadersStateTraceGeneration.load(std::memory_order_relaxed)) { return; } record->endTick = GetTickCount64(); DWORD allocationError = ERROR_SUCCESS; AcquireSRWLockExclusive(&g_properShadersStateTraceLock); + if (!IsProperShadersStateTraceCaptureActive() || + record->captureGeneration != g_properShadersStateTraceGeneration.load(std::memory_order_relaxed)) { + ReleaseSRWLockExclusive(&g_properShadersStateTraceLock); + return; + } if (!g_properShadersStateTraceRing) { g_properShadersStateTraceRing = static_cast(VirtualAlloc( @@ -263,7 +303,6 @@ void StoreProperShadersStateTraceRecord(ProperShadersStateTraceRecord* record) } return; } - MaybeDumpProperShadersStateTraceRing(); } void InitializeProperShadersStateTraceRecord( @@ -274,6 +313,7 @@ void InitializeProperShadersStateTraceRecord( return; } *record = ProperShadersStateTraceRecord{}; + record->captureGeneration = g_properShadersStateTraceGeneration.load(std::memory_order_relaxed); record->kind = static_cast(kind); record->threadId = GetCurrentThreadId(); record->beginTick = GetTickCount64(); @@ -283,7 +323,8 @@ void CaptureProperShadersStateTracePoint( ProperShadersStateTraceRecord* record, uint32_t snapshotIndex) { - if (!record || snapshotIndex >= 3 || !g_properShadersRenderStateGuardConfig.enableStateRing) { + if (!record || snapshotIndex >= 3 || !IsProperShadersStateTraceCaptureActive() || + record->captureGeneration != g_properShadersStateTraceGeneration.load(std::memory_order_relaxed)) { return; } @@ -298,7 +339,11 @@ void CaptureProperShadersStateTracePoint( void ShutdownProperShadersStateRing() { + g_properShadersStateTraceActive.store(false, std::memory_order_release); AcquireSRWLockExclusive(&g_properShadersStateTraceLock); + g_properShadersStateTraceGeneration.fetch_add(1, std::memory_order_relaxed); + // Require release before a held key can arm a new window after shutdown. + InterlockedExchange(&g_properShadersStateTraceTriggerDown, 1); if (g_properShadersStateTraceRing) { VirtualFree(g_properShadersStateTraceRing, 0, MEM_RELEASE); g_properShadersStateTraceRing = nullptr; diff --git a/src/bridge/legacy/ProperShadersStateRing.h b/src/bridge/legacy/ProperShadersStateRing.h index a13c833..8b9dce6 100644 --- a/src/bridge/legacy/ProperShadersStateRing.h +++ b/src/bridge/legacy/ProperShadersStateRing.h @@ -8,6 +8,8 @@ #include "ProperShadersRenderStateGuard.h" // ---- record lifecycle (called from the guards) ---- +bool IsProperShadersStateTraceCaptureActive() noexcept; +bool BeginProperShadersStateTraceCapture(); void InitializeProperShadersStateTraceRecord( ProperShadersStateTraceRecord* record, ProperShadersStateTraceKind kind); @@ -16,6 +18,6 @@ void CaptureProperShadersStateTracePoint( uint32_t snapshotIndex); void StoreProperShadersStateTraceRecord(ProperShadersStateTraceRecord* record); -// ---- manual dump (hotkey edge) ---- +// F10 opens a one-second capture. Expiry/dump is polled at owner safe points. void MaybeDumpProperShadersStateTraceRing(); void DumpProperShadersStateTraceRing(); diff --git a/src/bridge/legacy/api-projects/CMakeLists.txt b/src/bridge/legacy/api-projects/CMakeLists.txt index 06152f5..bdb2f99 100644 --- a/src/bridge/legacy/api-projects/CMakeLists.txt +++ b/src/bridge/legacy/api-projects/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.25) project(sa_renderstack_api_projects LANGUAGES CXX) enable_testing() +add_executable(api-test-check-failure tests/check_failure.cpp) +target_compile_features(api-test-check-failure PRIVATE cxx_std_23) +add_test(NAME api-test-check-failure COMMAND api-test-check-failure) +set_tests_properties(api-test-check-failure PROPERTIES WILL_FAIL TRUE) + # This aggregate is owned by the Bridge main program. Child projects consume # the parent-owned SDK/backend paths and are not standalone products. set(SA_RENDERSTACK_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../../.." CACHE INTERNAL "SA RenderStack root") @@ -21,3 +26,45 @@ foreach(api IN ITEMS api7-selective-journal) add_subdirectory(${api}) endforeach() + +# GPU tests are invoked by tools/test.ps1 with an explicit newly built backend. +# Keeping them outside unqualified CTest preserves hosted runners' GPU skip. +add_executable(api-state-runtime-tests tests/state_runtime.cpp) +target_compile_features(api-state-runtime-tests PRIVATE cxx_std_23) +target_link_libraries(api-state-runtime-tests PRIVATE api3-state-batch-lib api7_selective_journal_lib user32) + +add_executable(constant-submit-benchmark tests/constant_benchmark.cpp) +target_compile_features(constant-submit-benchmark PRIVATE cxx_std_23) +target_link_libraries(constant-submit-benchmark PRIVATE api3-state-batch-lib user32 d3dcompiler) + +add_executable(bridge-lifecycle-runtime-tests tests/lifecycle_runtime.cpp) +target_compile_features(bridge-lifecycle-runtime-tests PRIVATE cxx_std_23) +target_link_libraries(bridge-lifecycle-runtime-tests PRIVATE user32) + +add_executable(bridge-iat-hook-tests ../tests/ProperShadersIatHookTests.cpp) +target_compile_features(bridge-iat-hook-tests PRIVATE cxx_std_23) +target_include_directories(bridge-iat-hook-tests PRIVATE ..) +add_test(NAME bridge-iat-hook-tests COMMAND bridge-iat-hook-tests) +set_tests_properties(bridge-iat-hook-tests PROPERTIES TIMEOUT 30) + +add_executable(bridge-code-transaction-tests tests/code_patch_tests.cpp) +target_compile_features(bridge-code-transaction-tests PRIVATE cxx_std_23) +target_include_directories(bridge-code-transaction-tests PRIVATE ..) +add_test(NAME bridge-code-transaction-tests COMMAND bridge-code-transaction-tests) +set_tests_properties(bridge-code-transaction-tests PROPERTIES TIMEOUT 30) + +add_executable(bridge-log-failure-tests tests/log_failure_tests.cpp) +target_compile_features(bridge-log-failure-tests PRIVATE cxx_std_23) +add_test(NAME bridge-log-failure-tests COMMAND bridge-log-failure-tests) + +add_executable(proper-shaders-fading-runtime-tests + tests/fading_runtime.cpp ../ProperShadersStateRing.cpp) +target_compile_features(proper-shaders-fading-runtime-tests PRIVATE cxx_std_23) +target_compile_definitions(proper-shaders-fading-runtime-tests PRIVATE NOMINMAX) +target_link_libraries(proper-shaders-fading-runtime-tests PRIVATE user32) +add_test(NAME proper-shaders-fading-runtime-tests COMMAND proper-shaders-fading-runtime-tests) +set_tests_properties(proper-shaders-fading-runtime-tests PROPERTIES TIMEOUT 30) + +add_executable(measurement-core-tests tests/measurement_tests.cpp) +target_compile_features(measurement-core-tests PRIVATE cxx_std_17) +add_test(NAME measurement-core-tests COMMAND measurement-core-tests) diff --git a/src/bridge/legacy/api-projects/README.md b/src/bridge/legacy/api-projects/README.md index 660aab6..fe1983a 100644 --- a/src/bridge/legacy/api-projects/README.md +++ b/src/bridge/legacy/api-projects/README.md @@ -31,6 +31,22 @@ cmake --build out/api-project-build/all --config Release ProperShaders 或 GTA 主渲染路径采用。是否由 Bridge 在生产路径调用,仍以主程序的 consumer 代码、运行计数器和游戏回归证据为准。 +`tools/test.ps1` 现在会构建并执行附属 API 的 Release CTest;检查使用始终有效的 +`CHECK`,并包含一个预期失败项以验证 `NDEBUG` 不会隐藏断言。另一个 +`api-state-runtime-tests reference|optimized` 目标使用真实设备验证 +journal/stateblock/UP/viewport/scissor/light、API3 提交、API7 RAII、Reset 和批次 +失败恢复。主测试入口分别记录普通/优化配置结果;显式跳过 GPU 时记录 skip 原因。 + +`constant-submit-benchmark on|off check|bench` 使用真实设备,检查 +常量渲染结果,以及单槽、多槽纹理/采样器切换、点/线性过滤、边框色、状态块和 +journal 恢复后的像素。`on|off` 同时显式控制常量去重、采样器合并、纹理合并和 +绑定缓存。`check` 是主流水线的必需 GPU 回归,`bench` 另输出公开 COM setter、 +真实 draw 与 event-query 完成时间;计时不作为 CTest 阈值。 + +独立 A/B 入口为 `tests/constant-submit-benchmark.ps1`,使用同一 probe 交替启动 +基线和候选进程并保存哈希、全部原始样本和成对比较。方法与边界见 +[性能验证说明](../../../../docs/development/performance-validation.md)。 + 设计参考: - [Microsoft D3D9 CreateDevice](https://learn.microsoft.com/en-us/windows/win32/api/d3d9/nf-d3d9-idirect3d9-createdevice) diff --git a/src/bridge/legacy/api-projects/api1-status/tests/status_client_tests.cpp b/src/bridge/legacy/api-projects/api1-status/tests/status_client_tests.cpp index e59cd48..bdb6e0b 100644 --- a/src/bridge/legacy/api-projects/api1-status/tests/status_client_tests.cpp +++ b/src/bridge/legacy/api-projects/api1-status/tests/status_client_tests.cpp @@ -1,5 +1,5 @@ #include -#include +#include "../../tests/test_check.hpp" int main() { D3D9GtaSaCompatStatus good{}; @@ -7,8 +7,8 @@ int main() { good.ApiVersion = D3D9_GTA_SA_COMPAT_API_VERSION; good.Flags = D3D9_GTA_SA_COMPAT_ACTIVE | D3D9_GTA_SA_COMPAT_VULKAN_BACKEND | D3D9_GTA_SA_COMPAT_VULKAN_INTEROP; - assert(sa::api1::StatusClient::validate(good).has_value()); + CHECK(sa::api1::StatusClient::validate(good).has_value()); good.ApiVersion = 1; - assert(!sa::api1::StatusClient::validate(good).has_value()); - return 0; + CHECK(!sa::api1::StatusClient::validate(good).has_value()); + return api_test::finish(); } diff --git a/src/bridge/legacy/api-projects/api2-vulkan-pass/tests/pass_registration_tests.cpp b/src/bridge/legacy/api-projects/api2-vulkan-pass/tests/pass_registration_tests.cpp index 4dfc206..1fa6e19 100644 --- a/src/bridge/legacy/api-projects/api2-vulkan-pass/tests/pass_registration_tests.cpp +++ b/src/bridge/legacy/api-projects/api2-vulkan-pass/tests/pass_registration_tests.cpp @@ -1,3 +1,6 @@ #include -#include -int main(){ assert(!sa::api2::PassRegistration::create(nullptr,{},0,nullptr,nullptr).has_value()); return 0; } +#include "../../tests/test_check.hpp" +int main() { + CHECK(!sa::api2::PassRegistration::create(nullptr, {}, 0, nullptr, nullptr).has_value()); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/api3-state-batch/README.md b/src/bridge/legacy/api-projects/api3-state-batch/README.md index a790f8f..b3b7f74 100644 --- a/src/bridge/legacy/api-projects/api3-state-batch/README.md +++ b/src/bridge/legacy/api-projects/api3-state-batch/README.md @@ -2,6 +2,12 @@ This standalone C++23 project builds `api3-state-batch-lib` (a reusable `StateBatchBuilder`) plus a demo and unit test. The builder accepts `std::span` data, merges adjacent register ranges, validates capacity before constructing the ABI view, and submits through one COM transaction. The demo dynamically loads a supplied D3D9 backend, creates a hidden D3D9 device, and exercises `ID3D9GtaSaCompatDevice2::SubmitStateBatch`. +Overlapping additions follow caller order: the last write wins even when its +start register is lower than an earlier range. Rejected input or allocation +failure preserves the existing builder. A returned view borrows builder storage +until the next mutation or `view()` call. VS register 255 and PS register 223 +each accept one float4; a nonempty range starting at 256/224 is out of bounds. + ```text api3-state-batch.exe path\to\d3d9.dll ``` diff --git a/src/bridge/legacy/api-projects/api3-state-batch/include/api3_state_batch.hpp b/src/bridge/legacy/api-projects/api3-state-batch/include/api3_state_batch.hpp index 44da1ff..4d8fa5a 100644 --- a/src/bridge/legacy/api-projects/api3-state-batch/include/api3_state_batch.hpp +++ b/src/bridge/legacy/api-projects/api3-state-batch/include/api3_state_batch.hpp @@ -9,6 +9,7 @@ class StateBatchBuilder { public: std::expected add_vertex_float(UINT start, std::span values) noexcept; std::expected add_pixel_float(UINT start, std::span values) noexcept; + // The view borrows builder storage until the next view() or mutation. std::expected view() const noexcept; HRESULT submit(ID3D9GtaSaCompatDevice2* device) const noexcept; private: diff --git a/src/bridge/legacy/api-projects/api3-state-batch/src/api3_state_batch.cpp b/src/bridge/legacy/api-projects/api3-state-batch/src/api3_state_batch.cpp index 5b8cc75..7a804d3 100644 --- a/src/bridge/legacy/api-projects/api3-state-batch/src/api3_state_batch.cpp +++ b/src/bridge/legacy/api-projects/api3-state-batch/src/api3_state_batch.cpp @@ -7,19 +7,39 @@ std::expected StateBatchBuilder::add(std::vector& out, UIN if (start > capacity || values.size() % 4 != 0 || values.size()/4 > capacity - start || values.size() > std::numeric_limits::max()) return std::unexpected(D3DERR_INVALIDCALL); if (values.empty()) return {}; try { - out.push_back(Range{start, std::vector(values.begin(), values.end())}); - std::sort(out.begin(), out.end(), [](const Range& a, const Range& b){ return a.start < b.start; }); - std::vector merged; - for (auto& r : out) { - if (merged.empty() || merged.back().start + merged.back().values.size()/4 < r.start) { merged.push_back(std::move(r)); continue; } - auto& dst = merged.back(); - UINT end = std::max(dst.start + static_cast(dst.values.size()/4), r.start + static_cast(r.values.size()/4)); - std::vector values2(static_cast(end - dst.start)*4); - std::copy(dst.values.begin(), dst.values.end(), values2.begin()); - std::copy(r.values.begin(), r.values.end(), values2.begin() + static_cast(r.start - dst.start) * 4); - dst.values = std::move(values2); + Range pending{start, std::vector(values.begin(), values.end())}; + std::vector next; + next.reserve(out.size() + 1); + bool inserted = false; + for (const auto& range : out) { + if (inserted) { + next.push_back(range); + continue; + } + const UINT rangeEnd = range.start + static_cast(range.values.size() / 4); + const UINT pendingEnd = pending.start + static_cast(pending.values.size() / 4); + if (rangeEnd < pending.start) { + next.push_back(range); + } else if (pendingEnd < range.start) { + next.push_back(std::move(pending)); + next.push_back(range); + inserted = true; + } else { + const UINT first = std::min(range.start, pending.start); + const UINT end = std::max(rangeEnd, pendingEnd); + std::vector combined(static_cast(end - first) * 4); + std::copy(range.values.begin(), range.values.end(), + combined.begin() + static_cast(range.start - first) * 4); + // Existing ranges are disjoint. The pending range contains the newest + // write, so it wins every overlap regardless of register address order. + std::copy(pending.values.begin(), pending.values.end(), + combined.begin() + static_cast(pending.start - first) * 4); + pending = Range{first, std::move(combined)}; + } } - out = std::move(merged); + if (!inserted) next.push_back(std::move(pending)); + // Preserve the previous builder if allocation/copying fails. + out.swap(next); return {}; } catch (...) { return std::unexpected(E_OUTOFMEMORY); } } diff --git a/src/bridge/legacy/api-projects/api3-state-batch/tests/test_builder.cpp b/src/bridge/legacy/api-projects/api3-state-batch/tests/test_builder.cpp index 38c7854..33a46be 100644 --- a/src/bridge/legacy/api-projects/api3-state-batch/tests/test_builder.cpp +++ b/src/bridge/legacy/api-projects/api3-state-batch/tests/test_builder.cpp @@ -1,3 +1,77 @@ #include "api3_state_batch.hpp" -#include -int main(){ api3::StateBatchBuilder b; float a[4]={1,2,3,4}, c[4]={5,6,7,8}; assert(b.add_vertex_float(0,a).has_value()); assert(b.add_vertex_float(1,c).has_value()); auto v=b.view(); assert(v && v->VertexFloatRangeCount==1 && v->VertexFloatRanges[0].RegisterCount==2); assert(!b.add_vertex_float(255,std::span(a,4)).has_value()); return 0; } +#include "../../tests/test_check.hpp" + +#include + +namespace { +void check_values(const D3D9GtaSaFloatConstantRange& range, + UINT start, std::span expected) { + CHECK(range.StartRegister == start); + CHECK(range.RegisterCount * 4 == expected.size()); + if (range.RegisterCount * 4 == expected.size()) { + for (std::size_t i = 0; i < expected.size(); ++i) + CHECK(range.Data[i] == expected[i]); + } +} + +void last_write_wins(bool pixel) { + api3::StateBatchBuilder builder; + auto add = [&](UINT start, std::span value) { + return pixel ? builder.add_pixel_float(start, value) + : builder.add_vertex_float(start, value); + }; + const std::array old{1, 2, 3, 4}; + const std::array replacement{5, 6, 7, 8, 9, 10, 11, 12}; + CHECK(add(1, old).has_value()); + CHECK(add(0, replacement).has_value()); + auto view = builder.view(); + CHECK(view.has_value()); + if (!view) return; + const auto count = pixel ? view->PixelFloatRangeCount : view->VertexFloatRangeCount; + CHECK(count == 1); + if (count != 1) return; + check_values(pixel ? view->PixelFloatRanges[0] : view->VertexFloatRanges[0], 0, replacement); + + CHECK(add(0, old).has_value()); + const std::array partial{1, 2, 3, 4, 9, 10, 11, 12}; + view = builder.view(); + CHECK(view.has_value()); + if (view) check_values(pixel ? view->PixelFloatRanges[0] : view->VertexFloatRanges[0], 0, partial); + + CHECK(add(0, replacement).has_value()); + view = builder.view(); + CHECK(view.has_value()); + if (view) check_values(pixel ? view->PixelFloatRanges[0] : view->VertexFloatRanges[0], 0, replacement); +} + +void adjacency_and_boundaries() { + api3::StateBatchBuilder builder; + const std::array a{1, 2, 3, 4}, b{5, 6, 7, 8}; + CHECK(builder.add_vertex_float(0, a).has_value()); + CHECK(builder.add_vertex_float(1, b).has_value()); + auto view = builder.view(); + CHECK(view && view->VertexFloatRangeCount == 1); + const std::array expected{1, 2, 3, 4, 5, 6, 7, 8}; + if (view && view->VertexFloatRangeCount == 1) check_values(view->VertexFloatRanges[0], 0, expected); + CHECK(builder.add_vertex_float(255, a).has_value()); + CHECK(builder.add_pixel_float(223, b).has_value()); + CHECK(!builder.add_vertex_float(256, a).has_value()); + CHECK(!builder.add_pixel_float(224, a).has_value()); + CHECK(!builder.add_vertex_float(0, std::span(a).first(3)).has_value()); + CHECK(builder.add_vertex_float(256, {}).has_value()); + view = builder.view(); + CHECK(view && view->VertexFloatRangeCount == 2 && view->PixelFloatRangeCount == 1); + if (view && view->VertexFloatRangeCount == 2) { + check_values(view->VertexFloatRanges[0], 0, expected); + check_values(view->VertexFloatRanges[1], 255, a); + check_values(view->PixelFloatRanges[0], 223, b); + } +} +} + +int main() { + last_write_wins(false); + last_write_wins(true); + adjacency_and_boundaries(); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/api4-state-journal/tests/test_session.cpp b/src/bridge/legacy/api-projects/api4-state-journal/tests/test_session.cpp index 1f34074..4ee61b7 100644 --- a/src/bridge/legacy/api-projects/api4-state-journal/tests/test_session.cpp +++ b/src/bridge/legacy/api-projects/api4-state-journal/tests/test_session.cpp @@ -1,3 +1,6 @@ #include "api4_journal_session.hpp" -#include -int main(){ assert(!api4::JournalSession::begin(nullptr).has_value()); return 0; } +#include "../../tests/test_check.hpp" +int main() { + CHECK(!api4::JournalSession::begin(nullptr).has_value()); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/api5-effect-batch/tests/test_builder.cpp b/src/bridge/legacy/api-projects/api5-effect-batch/tests/test_builder.cpp index d8d635c..626c013 100644 --- a/src/bridge/legacy/api-projects/api5-effect-batch/tests/test_builder.cpp +++ b/src/bridge/legacy/api-projects/api5-effect-batch/tests/test_builder.cpp @@ -1,6 +1,6 @@ #include "api5_effect_batch.hpp" #include -#include +#include "../../tests/test_check.hpp" int main() { api5::EffectStateBatchBuilder builder; @@ -8,11 +8,12 @@ int main() { D3D9GtaSaFloatConstantRange range{0u, 1u, values.data()}; builder.vertexFloat(std::span{&range, 1}); auto valid = builder.validate(); - assert(valid.has_value()); + CHECK(valid.has_value()); api5::EffectStateBatchBuilder invalid; D3D9GtaSaRenderStateBinding bad{ static_cast(0xFFFFFFFFu), 0u}; invalid.renderStates(std::span{&bad, 1}); - assert(!invalid.validate().has_value()); + CHECK(!invalid.validate().has_value()); + return api_test::finish(); } diff --git a/src/bridge/legacy/api-projects/api6-state-draw/tests/test_transaction.cpp b/src/bridge/legacy/api-projects/api6-state-draw/tests/test_transaction.cpp index 67f5b98..f3e3be4 100644 --- a/src/bridge/legacy/api-projects/api6-state-draw/tests/test_transaction.cpp +++ b/src/bridge/legacy/api-projects/api6-state-draw/tests/test_transaction.cpp @@ -1,10 +1,11 @@ #include "api6_state_draw.hpp" -#include +#include "../../tests/test_check.hpp" int main() { api6::StateDrawTransaction transaction(nullptr); D3D9GtaSaStateBatch state{}; auto result = transaction.submit( state, api6::DrawKind::Primitive, D3DPT_TRIANGLELIST, 0u, 1u); - assert(!result.has_value()); + CHECK(!result.has_value()); + return api_test::finish(); } diff --git a/src/bridge/legacy/api-projects/api7-selective-journal/README.md b/src/bridge/legacy/api-projects/api7-selective-journal/README.md index c7d1db0..9cb05ce 100644 --- a/src/bridge/legacy/api-projects/api7-selective-journal/README.md +++ b/src/bridge/legacy/api-projects/api7-selective-journal/README.md @@ -2,6 +2,11 @@ 独立 C++23 SDK consumer:`include/`+`src/` 提供 `SelectiveJournal` RAII(same-thread、begin/capture/restore、Reset 失效和 fail-closed),`main.cpp` 是动态加载 backend 的 contract probe。 +`SelectiveJournal` 不可复制,可以移动;移动会转移 COM 引用和事务,但不会改变 +所属线程。同线程析构或移动赋值会先恢复仍在进行的事务,再释放接口。活动对象必须 +在所属线程恢复、移动和销毁;跨线程的 begin/capture/restore 会拒绝调用后端。 +Reset 后的 restore 失败会关闭本地事务标记,之后可以重新 begin。 + ```powershell cmake -S . -B build; cmake --build build --config Release build\\Release\\api7_selective_journal.exe path\\to\\d3d9.dll diff --git a/src/bridge/legacy/api-projects/api7-selective-journal/include/api7_selective_journal.hpp b/src/bridge/legacy/api-projects/api7-selective-journal/include/api7_selective_journal.hpp index 79d94cf..4677b25 100644 --- a/src/bridge/legacy/api-projects/api7-selective-journal/include/api7_selective_journal.hpp +++ b/src/bridge/legacy/api-projects/api7-selective-journal/include/api7_selective_journal.hpp @@ -3,4 +3,26 @@ #include #include #include -namespace api7 { class SelectiveJournal { ID3D9GtaSaCompatDevice6* api_{}; std::thread::id owner_; bool active_{}; public: explicit SelectiveJournal(ID3D9GtaSaCompatDevice6*a):api_(a),owner_(std::this_thread::get_id()){if(api_)api_->AddRef();} ~SelectiveJournal(){if(api_)api_->Release();} std::expected begin() noexcept; std::expected capture(bool on) noexcept; std::expected restore() noexcept; }; } +namespace api7 { +// An active journal must be restored, moved and destroyed on its owner thread. +// Moving transfers ownership; it does not change the backend transaction thread. +class SelectiveJournal { +public: + explicit SelectiveJournal(ID3D9GtaSaCompatDevice6* api); + SelectiveJournal(const SelectiveJournal&) = delete; + SelectiveJournal& operator=(const SelectiveJournal&) = delete; + SelectiveJournal(SelectiveJournal&& other) noexcept; + SelectiveJournal& operator=(SelectiveJournal&& other) noexcept; + ~SelectiveJournal(); + + std::expected begin() noexcept; + std::expected capture(bool on) noexcept; + std::expected restore() noexcept; + +private: + void close() noexcept; + ID3D9GtaSaCompatDevice6* api_{}; + std::thread::id owner_; + bool active_{}; +}; +} diff --git a/src/bridge/legacy/api-projects/api7-selective-journal/src/api7_selective_journal.cpp b/src/bridge/legacy/api-projects/api7-selective-journal/src/api7_selective_journal.cpp index 930f7ee..4a702e4 100644 --- a/src/bridge/legacy/api-projects/api7-selective-journal/src/api7_selective_journal.cpp +++ b/src/bridge/legacy/api-projects/api7-selective-journal/src/api7_selective_journal.cpp @@ -1,2 +1,61 @@ #include "api7_selective_journal.hpp" -namespace api7 { std::expected SelectiveJournal::begin()noexcept{if(!api_||std::this_thread::get_id()!=owner_)return std::unexpected("same-thread contract"); auto h=api_->BeginSelectiveStateJournal();if(FAILED(h))return std::unexpected("begin failed");active_=true;return{};} std::expected SelectiveJournal::capture(bool on)noexcept{if(!active_||std::this_thread::get_id()!=owner_)return std::unexpected("inactive or wrong thread");if(FAILED(api_->SetStateJournalCaptureEnabled(on)))return std::unexpected("capture failed");return{};} std::expected SelectiveJournal::restore()noexcept{if(!active_||std::this_thread::get_id()!=owner_)return std::unexpected("inactive or wrong thread");auto h=api_->RestoreStateJournal();active_=false;if(FAILED(h))return std::unexpected("restore failed (reset invalidates journal)");return{};} } + +#include + +namespace api7 { +SelectiveJournal::SelectiveJournal(ID3D9GtaSaCompatDevice6* api) + : api_(api), owner_(std::this_thread::get_id()) { + if (api_) api_->AddRef(); +} + +SelectiveJournal::SelectiveJournal(SelectiveJournal&& other) noexcept + : api_(std::exchange(other.api_, nullptr)), owner_(other.owner_), + active_(std::exchange(other.active_, false)) { } + +SelectiveJournal& SelectiveJournal::operator=(SelectiveJournal&& other) noexcept { + if (this != &other) { + close(); + api_ = std::exchange(other.api_, nullptr); + owner_ = other.owner_; + active_ = std::exchange(other.active_, false); + } + return *this; +} + +void SelectiveJournal::close() noexcept { + if (api_) { + if (active_ && std::this_thread::get_id() == owner_) + (void)api_->RestoreStateJournal(); + active_ = false; + api_->Release(); + api_ = nullptr; + } +} + +SelectiveJournal::~SelectiveJournal() { close(); } + +std::expected SelectiveJournal::begin() noexcept { + if (!api_ || std::this_thread::get_id() != owner_) + return std::unexpected("same-thread contract"); + if (active_) return std::unexpected("already active"); + if (FAILED(api_->BeginSelectiveStateJournal())) return std::unexpected("begin failed"); + active_ = true; + return {}; +} + +std::expected SelectiveJournal::capture(bool on) noexcept { + if (!active_ || std::this_thread::get_id() != owner_) + return std::unexpected("inactive or wrong thread"); + if (FAILED(api_->SetStateJournalCaptureEnabled(on))) return std::unexpected("capture failed"); + return {}; +} + +std::expected SelectiveJournal::restore() noexcept { + if (!active_ || std::this_thread::get_id() != owner_) + return std::unexpected("inactive or wrong thread"); + const HRESULT hr = api_->RestoreStateJournal(); + active_ = false; + if (FAILED(hr)) return std::unexpected("restore failed (reset invalidates journal)"); + return {}; +} +} diff --git a/src/bridge/legacy/api-projects/api7-selective-journal/tests/test_journal.cpp b/src/bridge/legacy/api-projects/api7-selective-journal/tests/test_journal.cpp index 5d96e48..38f6692 100644 --- a/src/bridge/legacy/api-projects/api7-selective-journal/tests/test_journal.cpp +++ b/src/bridge/legacy/api-projects/api7-selective-journal/tests/test_journal.cpp @@ -1,9 +1,104 @@ #include "api7_selective_journal.hpp" -#include +#include "../../tests/test_check.hpp" + +#include +#include +#include + +// Supplementary ownership/failure controls; runtime tests use a real device. +struct JournalControl final : ID3D9GtaSaCompatDevice6 { + ULONG refs = 1; + unsigned begins = 0, restores = 0, captures = 0; + bool active = false; + HRESULT restoreResult = D3D_OK; + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void** out) override { + if (out) *out = nullptr; + return E_NOINTERFACE; + } + ULONG STDMETHODCALLTYPE AddRef() override { return ++refs; } + ULONG STDMETHODCALLTYPE Release() override { return --refs; } + HRESULT STDMETHODCALLTYPE GetStatus(D3D9GtaSaCompatStatus*) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE GetVulkanInterop(ID3D9VkInteropDevice**) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE RegisterVulkanPass(const D3D9GtaSaVulkanPassDesc*, UINT64*) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE UnregisterVulkanPass(UINT64) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE SubmitStateBatch(const D3D9GtaSaStateBatch*) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE BeginStateJournal() override { return BeginSelectiveStateJournal(); } + HRESULT STDMETHODCALLTYPE RestoreStateJournal() override { + ++restores; + active = false; + return restoreResult; + } + HRESULT STDMETHODCALLTYPE SubmitEffectStateBatch(const D3D9GtaSaEffectStateBatch*) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE SubmitStateDrawBatch(const D3D9GtaSaStateDrawBatch*) override { return E_NOTIMPL; } + HRESULT STDMETHODCALLTYPE BeginSelectiveStateJournal() override { + ++begins; + if (active) return D3DERR_INVALIDCALL; + active = true; + return D3D_OK; + } + HRESULT STDMETHODCALLTYPE SetStateJournalCaptureEnabled(BOOL) override { + ++captures; + return active ? D3D_OK : D3DERR_INVALIDCALL; + } +}; int main() { - api7::SelectiveJournal journal(nullptr); - assert(!journal.begin().has_value()); - assert(!journal.capture(true).has_value()); - assert(!journal.restore().has_value()); + CHECK(!std::is_copy_constructible_v); + CHECK(!std::is_copy_assignable_v); + { + api7::SelectiveJournal empty(nullptr); + CHECK(!empty.begin()); + CHECK(!empty.capture(true)); + CHECK(!empty.restore()); + } + JournalControl control; + { + api7::SelectiveJournal journal(&control); + CHECK(control.refs == 2); + CHECK(journal.begin().has_value()); + CHECK(journal.capture(true).has_value()); + bool wrongThreadRejected = false; + std::thread foreign([&] { wrongThreadRejected = !journal.capture(false); }); + foreign.join(); + CHECK(wrongThreadRejected); + CHECK(control.captures == 1); + } + CHECK(control.refs == 1); + CHECK(control.restores == 1); + CHECK(!control.active); + + JournalControl source, destination; + { + api7::SelectiveJournal original(&source); + CHECK(original.begin().has_value()); + api7::SelectiveJournal moved(std::move(original)); + CHECK(!original.capture(true)); + CHECK(moved.capture(true).has_value()); + api7::SelectiveJournal target(&destination); + CHECK(target.begin().has_value()); + target = std::move(moved); + CHECK(!moved.restore()); + CHECK(destination.restores == 1); + CHECK(destination.refs == 1); + CHECK(target.restore().has_value()); + } + CHECK(source.refs == 1); + CHECK(source.restores == 1); + CHECK(destination.refs == 1); + + JournalControl reset; + { + api7::SelectiveJournal journal(&reset); + CHECK(journal.begin().has_value()); + reset.active = false; + reset.restoreResult = D3DERR_INVALIDCALL; + CHECK(!journal.restore()); + CHECK(!journal.capture(true)); + reset.restoreResult = D3D_OK; + CHECK(journal.begin().has_value()); + CHECK(journal.restore().has_value()); + } + CHECK(reset.refs == 1); + CHECK(reset.restores == 2); + return api_test::finish(); } diff --git a/src/bridge/legacy/api-projects/tests/check_failure.cpp b/src/bridge/legacy/api-projects/tests/check_failure.cpp new file mode 100644 index 0000000..00912b0 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/check_failure.cpp @@ -0,0 +1,6 @@ +#include "test_check.hpp" + +int main() { + CHECK(false); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/code_patch_tests.cpp b/src/bridge/legacy/api-projects/tests/code_patch_tests.cpp new file mode 100644 index 0000000..b641896 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/code_patch_tests.cpp @@ -0,0 +1,297 @@ +#include "test_check.hpp" +#include "BridgeCodePatch.h" + +#include +#include +#include + +namespace { +using BridgeCodePatch::CodePatch; +using BridgeCodePatch::NativeOps; + +// Every successful operation below still calls the real Win32 API. Only the +// otherwise inaccessible failure is injected; code execution and page state +// are always observed from real VirtualAlloc memory. +struct Faults { + inline static unsigned protectCalls = 0; + inline static unsigned flushCalls = 0; + inline static unsigned failProtect = 0; + inline static unsigned failFlush = 0; + static void Reset(unsigned protect = 0, unsigned flush = 0) { + protectCalls = flushCalls = 0; + failProtect = protect; + failFlush = flush; + } + static BOOL WINAPI Protect(LPVOID site, SIZE_T length, DWORD protection, PDWORD old) { + if (++protectCalls == failProtect) { + SetLastError(ERROR_ACCESS_DENIED); + return FALSE; + } + return VirtualProtect(site, length, protection, old); + } + static BOOL WINAPI Flush(HANDLE process, LPCVOID site, SIZE_T length) { + if (++flushCalls == failFlush) { + SetLastError(ERROR_WRITE_FAULT); + return FALSE; + } + return FlushInstructionCache(process, site, length); + } + static NativeOps Ops() { return {&Protect, &Flush}; } +}; + +struct Executable { + uint8_t* site = nullptr; + std::array original{}; + std::array replacement{}; + Executable() { + site = static_cast(VirtualAlloc(nullptr, 4096, + MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); + CHECK(site != nullptr); + if (!site) return; + std::memset(site, 0x90, 4096); + const uint8_t before[] = {0xB8, 7, 0, 0, 0, 0xC3}; + const uint8_t target[] = {0xB8, 42, 0, 0, 0, 0xC3}; + std::memcpy(site, before, sizeof(before)); + std::memcpy(site + 64, target, sizeof(target)); + std::memcpy(original.data(), site, original.size()); + replacement.fill(0x90); // Caller owns NOP padding and instruction shape. + replacement[0] = 0xE9; + const int32_t relative = 64 - 5; + std::memcpy(replacement.data() + 1, &relative, sizeof(relative)); + DWORD old = 0; + CHECK(VirtualProtect(site, 4096, PAGE_EXECUTE_READ, &old) != FALSE); + CHECK(FlushInstructionCache(GetCurrentProcess(), site, 4096) != FALSE); + } + ~Executable() { if (site) VirtualFree(site, 0, MEM_RELEASE); } + int Invoke() const { return reinterpret_cast(site)(); } + DWORD Protection() const { + MEMORY_BASIC_INFORMATION memory{}; + CHECK(VirtualQuery(site, &memory, sizeof(memory)) == sizeof(memory)); + return memory.Protect; + } + void Write(const void* bytes, size_t length) { + DWORD old = 0; + CHECK(VirtualProtect(site, length, PAGE_EXECUTE_READWRITE, &old) != FALSE); + std::memcpy(site, bytes, length); + CHECK(FlushInstructionCache(GetCurrentProcess(), site, length) != FALSE); + DWORD ignored = 0; + CHECK(VirtualProtect(site, length, old, &ignored) != FALSE); + } +}; + +void NormalInstructionLengths() { + for (size_t length : {size_t(5), size_t(6), size_t(7), size_t(16)}) { + Executable memory; + if (!memory.site) continue; + CodePatch patch; + CHECK(memory.Invoke() == 7); + CHECK(patch.Install(memory.site, memory.original.data(), memory.replacement.data(), length)); + CHECK(patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(patch.Error() == ERROR_SUCCESS); + CHECK(memory.Invoke() == 42); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + CHECK(std::memcmp(memory.site, memory.replacement.data(), length) == 0); + CHECK(patch.Restore()); + CHECK(!patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + CHECK(std::memcmp(memory.site, memory.original.data(), memory.original.size()) == 0); + CHECK(patch.Restore()); + } +} + +void InvalidInputAndSignature() { + Executable memory; + if (!memory.site) return; + CodePatch patch; + for (size_t length : {size_t(0), size_t(17)}) { + CHECK(!patch.Install(memory.site, memory.original.data(), memory.replacement.data(), length)); + CHECK(patch.Error() == ERROR_INVALID_PARAMETER); + CHECK(!patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Invoke() == 7); + } + CHECK(!patch.Install(nullptr, memory.original.data(), memory.replacement.data(), 6)); + CHECK(!patch.Install(memory.site, nullptr, memory.replacement.data(), 6)); + CHECK(!patch.Install(memory.site, memory.original.data(), nullptr, 6)); + auto unexpected = memory.original; + unexpected[0] = 0xCC; + CHECK(!patch.Install(memory.site, unexpected.data(), memory.replacement.data(), 6)); + CHECK(patch.Error() == ERROR_REVISION_MISMATCH); + CHECK(!patch.OwnsBytes()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); +} + +void RejectsMixedProtectionRange() { + auto* pages = static_cast(VirtualAlloc(nullptr, 8192, + MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); + CHECK(pages != nullptr); + if (!pages) return; + std::memset(pages, 0x90, 8192); + DWORD old = 0; + CHECK(VirtualProtect(pages, 4096, PAGE_EXECUTE_READ, &old) != FALSE); + CHECK(VirtualProtect(pages + 4096, 4096, PAGE_READONLY, &old) != FALSE); + const uint8_t expected[6] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90}; + const uint8_t replacement[6] = {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC}; + CodePatch patch; + CHECK(!patch.Install(pages + 4093, expected, replacement, sizeof(expected))); + CHECK(patch.Error() == ERROR_INVALID_ADDRESS); + CHECK(!patch.OwnsBytes()); + CHECK(std::memcmp(pages + 4093, expected, sizeof(expected)) == 0); + MEMORY_BASIC_INFORMATION memory{}; + CHECK(VirtualQuery(pages, &memory, sizeof(memory)) == sizeof(memory)); + CHECK(memory.Protect == PAGE_EXECUTE_READ); + CHECK(VirtualQuery(pages + 4096, &memory, sizeof(memory)) == sizeof(memory)); + CHECK(memory.Protect == PAGE_READONLY); + CHECK(VirtualFree(pages, 0, MEM_RELEASE) != FALSE); +} + +void RefusesOtherOwners() { + Executable memory; + if (!memory.site) return; + CodePatch patch; + CHECK(patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 7)); + CHECK(!patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 7)); + CHECK(patch.Error() == ERROR_BUSY); + CodePatch other; + CHECK(!other.Install(memory.site, memory.original.data(), memory.replacement.data(), 7)); + CHECK(!other.OwnsBytes()); + CHECK(other.Restore()); + CHECK(memory.Invoke() == 42); + const uint8_t foreign[] = {0xB8, 99, 0, 0, 0, 0xC3, 0x90}; + memory.Write(foreign, sizeof(foreign)); + CHECK(!patch.Restore()); + CHECK(patch.Error() == ERROR_REVISION_MISMATCH); + CHECK(patch.OwnsBytes()); + CHECK(memory.Invoke() == 99); + CHECK(std::memcmp(memory.site, foreign, sizeof(foreign)) == 0); + memory.Write(memory.replacement.data(), 7); + CHECK(patch.Restore()); + CHECK(memory.Invoke() == 7); +} + +void InitialProtectFailureDoesNotClaimBytes() { + Executable memory; + if (!memory.site) return; + Faults::Reset(1); + CodePatch patch(Faults::Ops()); + CHECK(!patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 6)); + CHECK(patch.Error() == ERROR_ACCESS_DENIED); + CHECK(!patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + Faults::Reset(); + CHECK(patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 6)); + CHECK(memory.Invoke() == 42); + CHECK(patch.Restore()); +} + +void InstallProtectionFailureRetainsRollback() { + Executable memory; + if (!memory.site) return; + Faults::Reset(2); + CodePatch patch(Faults::Ops()); + CHECK(!patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 7)); + CHECK(patch.Error() == ERROR_ACCESS_DENIED); + CHECK(patch.OwnsBytes()); + CHECK(patch.HasPendingProtection()); + CHECK(memory.Invoke() == 42); + CHECK(memory.Protection() == PAGE_EXECUTE_READWRITE); + Faults::Reset(); + CHECK(patch.Restore()); + CHECK(!patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); +} + +void RestoreFailuresAreRetryable() { + for (unsigned failedProtect : {1u, 2u}) { + Executable memory; + if (!memory.site) continue; + Faults::Reset(); + CodePatch patch(Faults::Ops()); + CHECK(patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 6)); + Faults::Reset(failedProtect); + CHECK(!patch.Restore()); + CHECK(patch.Error() == ERROR_ACCESS_DENIED); + CHECK(patch.OwnsBytes()); + CHECK(patch.HasPendingProtection() == (failedProtect == 2)); + CHECK(memory.Invoke() == (failedProtect == 1 ? 42 : 7)); + Faults::Reset(); + CHECK(patch.Restore()); + CHECK(!patch.OwnsBytes()); + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + } +} + +void CacheFailuresAreRetryable() { + for (bool failInstall : {true, false}) { + Executable memory; + if (!memory.site) continue; + Faults::Reset(0, failInstall ? 1u : 0u); + CodePatch patch(Faults::Ops()); + const bool installed = patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 5); + CHECK(installed == !failInstall); + CHECK(patch.OwnsBytes()); + if (failInstall) { + CHECK(patch.Error() == ERROR_WRITE_FAULT); + } else { + Faults::Reset(0, 1); + CHECK(!patch.Restore()); + CHECK(patch.Error() == ERROR_WRITE_FAULT); + CHECK(patch.OwnsBytes()); + CHECK(memory.Invoke() == 7); + } + CHECK(!patch.HasPendingProtection()); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + Faults::Reset(); + CHECK(patch.Restore()); + CHECK(!patch.OwnsBytes()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); + } +} + +void PendingCleanupRefusesForeignBytes() { + Executable memory; + if (!memory.site) return; + Faults::Reset(); + CodePatch patch(Faults::Ops()); + CHECK(patch.Install(memory.site, memory.original.data(), memory.replacement.data(), 6)); + Faults::Reset(2); + CHECK(!patch.Restore()); + const uint8_t foreign[] = {0xB8, 99, 0, 0, 0, 0xC3}; + memory.Write(foreign, sizeof(foreign)); + Faults::Reset(); + CHECK(!patch.Restore()); + CHECK(patch.Error() == ERROR_REVISION_MISMATCH); + CHECK(patch.OwnsBytes()); + CHECK(patch.HasPendingProtection()); + CHECK(memory.Invoke() == 99); + memory.Write(memory.original.data(), 6); + CHECK(patch.Restore()); + CHECK(!patch.OwnsBytes()); + CHECK(memory.Invoke() == 7); + CHECK(memory.Protection() == PAGE_EXECUTE_READ); +} +} // namespace + +int main() { + NormalInstructionLengths(); + InvalidInputAndSignature(); + RejectsMixedProtectionRange(); + RefusesOtherOwners(); + InitialProtectFailureDoesNotClaimBytes(); + InstallProtectionFailureRetainsRollback(); + RestoreFailuresAreRetryable(); + CacheFailuresAreRetryable(); + PendingCleanupRefusesForeignBytes(); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/constant_benchmark.cpp b/src/bridge/legacy/api-projects/tests/constant_benchmark.cpp new file mode 100644 index 0000000..5b30b3e --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/constant_benchmark.cpp @@ -0,0 +1,623 @@ +// Public D3D9 calls on an actual DXVK/Vulkan device. Timing is reported, never +// used as a pass/fail threshold. Run baseline/candidate in alternating processes. +#include "test_check.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template struct Ptr { + T* p{}; + Ptr() = default; + Ptr(const Ptr&) = delete; + Ptr& operator=(const Ptr&) = delete; + void reset() { if (p) p->Release(); p = nullptr; } + ~Ptr() { reset(); } + T* operator->() const { return p; } +}; + +#define HR(call) do { HRESULT hrResult = (call); if (FAILED(hrResult)) { \ + std::fprintf(stderr, "FAIL %s: %08lx\n", #call, static_cast(hrResult)); return false; } } while (false) + +struct Fixture { + HMODULE module{}; + HWND window{}; + Ptr d3d; + Ptr dev; + Ptr vs; + Ptr ps; + Ptr vb; + Ptr fence; + std::array, 2> textures; + ~Fixture() { + for (auto& texture : textures) texture.reset(); + fence.reset(); vb.reset(); ps.reset(); vs.reset(); dev.reset(); d3d.reset(); + if (window) DestroyWindow(window); + if (module) FreeLibrary(module); + } + bool open(const char* path, bool dedup) { + const std::string config = std::string("d3d9.gtaSaCompat=True;d3d9.gtaSaCompatDiagnostics=False;") + + "dxvk.hud=;d3d9.presentInterval=0;d3d9.gtaSaConstantDedup=" + (dedup ? "True" : "False") + + ";d3d9.gtaSaCoalesceSamplerBindings=" + (dedup ? "True" : "False") + + ";d3d9.gtaSaCoalesceTextureBindings=" + (dedup ? "True" : "False") + + ";d3d9.gtaSaResourceBindingCache=" + (dedup ? "True" : "False"); + SetEnvironmentVariableA("DXVK_CONFIG", config.c_str()); + SetEnvironmentVariableA("DXVK_CONFIG_FILE", "constant-benchmark-no-external.conf"); + SetEnvironmentVariableA("DXVK_LOG_LEVEL", "error"); + module = LoadLibraryA(path); + if (!module) return false; + auto create = reinterpret_cast(GetProcAddress(module, "Direct3DCreate9")); + if (!create || !(d3d.p = create(D3D_SDK_VERSION))) return false; + WNDCLASSA wc{}; wc.lpfnWndProc = DefWindowProcA; + wc.hInstance = GetModuleHandleA(nullptr); wc.lpszClassName = "RenderStackConstantBenchmark"; + RegisterClassA(&wc); + window = CreateWindowA(wc.lpszClassName, wc.lpszClassName, 0, 0, 0, 64, 64, + nullptr, nullptr, wc.hInstance, nullptr); + if (!window) return false; + D3DPRESENT_PARAMETERS pp{}; + pp.BackBufferWidth = pp.BackBufferHeight = 64; + pp.BackBufferFormat = D3DFMT_A8R8G8B8; pp.BackBufferCount = 1; + pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = window; + pp.Windowed = TRUE; pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + HR(d3d->CreateDevice(0, D3DDEVTYPE_HAL, window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &dev.p)); + HR(dev->CreateQuery(D3DQUERYTYPE_EVENT, &fence.p)); + return true; + } + bool wait() { + HR(fence->Issue(D3DISSUE_END)); + const ULONGLONG end = GetTickCount64() + 30000; + HRESULT hr; + while ((hr = fence->GetData(nullptr, 0, D3DGETDATA_FLUSH)) == S_FALSE) { + if (GetTickCount64() > end) return false; + SwitchToThread(); + } + HR(hr); + return true; + } +}; + +long long ticks() { LARGE_INTEGER t; QueryPerformanceCounter(&t); return t.QuadPart; } +double ns(long long t, UINT n) { + LARGE_INTEGER f; QueryPerformanceFrequency(&f); return double(t) * 1e9 / double(f.QuadPart) / n; +} + +bool setters(Fixture& f) { + constexpr UINT iterations = 300000; + for (bool pixel : {false, true}) for (bool ints : {false, true}) { + const UINT limit = ints ? 16 : (pixel ? 224 : 256); + for (UINT count : {1u, 4u, 16u, 64u, 224u, 256u}) { + if (count > limit) continue; + for (const char* pattern : {"same", "first", "tail", "all"}) { + std::vector a(count * 4, 0.25f), b = a; + if (!std::strcmp(pattern, "first")) b[0] = 0.75f; + if (!std::strcmp(pattern, "tail")) b[(count - 1) * 4] = 0.75f; + if (!std::strcmp(pattern, "all")) for (auto& x : b) x = 0.75f; + // int inputs use their own typed storage (no float/int aliasing). + std::vector ia(count * 4, 1), ib = ia; + for (UINT i = 0; i < count * 4; ++i) if (a[i] != b[i]) ib[i] = 2; + auto submit = [&](UINT i) { + if (ints) return pixel ? f.dev->SetPixelShaderConstantI(0, (i & 1) ? ia.data() : ib.data(), count) + : f.dev->SetVertexShaderConstantI(0, (i & 1) ? ia.data() : ib.data(), count); + return pixel ? f.dev->SetPixelShaderConstantF(0, (i & 1) ? a.data() : b.data(), count) + : f.dev->SetVertexShaderConstantF(0, (i & 1) ? a.data() : b.data(), count); + }; + for (UINT i = 0; i < 3000; ++i) HR(submit(i)); + HRESULT status = D3D_OK; + const auto begin = ticks(); + for (UINT i = 0; i < iterations; ++i) status |= submit(i); + const auto end = ticks(); + HR(status); + std::printf("setter,%s,%s,%u,%s,%u,%.3f\n", pixel ? "ps" : "vs", ints ? "int" : "float", + count, pattern, iterations, ns(end - begin, iterations)); + } + } + } + return true; +} + +bool draw_setup(Fixture& f) { + f.vs.reset(); f.ps.reset(); f.vb.reset(); + for (auto& texture : f.textures) texture.reset(); + const char* vs = "float4 tint:register(c3);struct O{float4 p:POSITION;float4 c:COLOR0;};" + "O main(float4 p:POSITION){O o;o.p=p;o.c=tint;return o;}"; + const char* ps = "float4 tint:register(c3);sampler2D tex:register(s0);sampler2D second:register(s5);" + "float4 main(float4 c:COLOR0):COLOR{return c*tint*(0.5*tex2D(tex,float2(0.5,0.5))" + "+0.5*tex2D(second,float2(0.5,0.5)));}"; + Ptr vcode, pcode, errors; + HR(D3DCompile(vs, std::strlen(vs), nullptr, nullptr, nullptr, "main", "vs_3_0", 0, 0, &vcode.p, &errors.p)); + HR(D3DCompile(ps, std::strlen(ps), nullptr, nullptr, nullptr, "main", "ps_3_0", 0, 0, &pcode.p, nullptr)); + HR(f.dev->CreateVertexShader(static_cast(vcode->GetBufferPointer()), &f.vs.p)); + HR(f.dev->CreatePixelShader(static_cast(pcode->GetBufferPointer()), &f.ps.p)); + HR(f.dev->SetVertexShader(f.vs.p)); HR(f.dev->SetPixelShader(f.ps.p)); + HR(f.dev->SetFVF(D3DFVF_XYZ)); + const float vertices[] = {-1,-1,0, -1,3,0, 3,-1,0}; + HR(f.dev->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_XYZ, D3DPOOL_MANAGED, &f.vb.p, nullptr)); + void* data = nullptr; HR(f.vb->Lock(0, sizeof(vertices), &data, 0)); + std::memcpy(data, vertices, sizeof(vertices)); HR(f.vb->Unlock()); + HR(f.dev->SetStreamSource(0, f.vb.p, 0, 12)); + HR(f.dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)); + HR(f.dev->SetRenderState(D3DRS_ZENABLE, FALSE)); + HR(f.dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)); + for (auto& texture : f.textures) { + HR(f.dev->CreateTexture(2, 2, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture.p, nullptr)); + D3DLOCKED_RECT lock{}; HR(texture->LockRect(0, &lock, nullptr, 0)); + for (UINT y = 0; y < 2; ++y) for (UINT x = 0; x < 2; ++x) { + const DWORD white = 0xffffffff; + std::memcpy(static_cast(lock.pBits) + y * lock.Pitch + x * 4, &white, 4); + } + HR(texture->UnlockRect(0)); + } + HR(f.dev->SetTexture(0, f.textures[0].p)); + HR(f.dev->SetTexture(5, f.textures[1].p)); + HR(f.dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT)); + HR(f.dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT)); + return true; +} + +bool pixel_check(Fixture& f) { + // Setters are followed by actual draws, synchronization and pixel readback. + // Exercise a changed tail after an equal prefix, then alternate both stages. + Ptr target, readback; + HR(f.dev->GetRenderTarget(0, &target.p)); + HR(f.dev->CreateOffscreenPlainSurface(64, 64, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &readback.p, nullptr)); + for (bool vertex : {false, true}) for (UINT frame = 0; frame < 4; ++frame) { + std::array white{}, color{}; + for (UINT k = 12; k < 16; ++k) white[k] = 1.0f; + color[12 + (frame & 1 ? 2 : 0)] = color[15] = 1.0f; + HR(f.dev->SetVertexShaderConstantF(0, vertex ? color.data() : white.data(), 4)); + HR(f.dev->SetPixelShaderConstantF(0, vertex ? white.data() : color.data(), 4)); + HR(f.dev->BeginScene()); HR(f.dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)); HR(f.dev->EndScene()); + if (!f.wait()) return false; + HR(f.dev->GetRenderTargetData(target.p, readback.p)); + D3DLOCKED_RECT lock{}; HR(readback->LockRect(&lock, nullptr, D3DLOCK_READONLY)); + DWORD actual; std::memcpy(&actual, static_cast(lock.pBits) + 32 * lock.Pitch + 32 * 4, 4); + HR(readback->UnlockRect()); + const DWORD expected = frame & 1 ? 0xff0000ff : 0xffff0000; + CHECK(actual == expected); + } + return true; +} + +bool binding_pixel_check(Fixture& f) { + Ptr target, readback; + HR(f.dev->GetRenderTarget(0, &target.p)); + HR(f.dev->CreateOffscreenPlainSurface(64, 64, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &readback.p, nullptr)); + std::array,3> images; + for (UINT i = 0; i < 3; ++i) { + HR(f.dev->CreateTexture(2, 2, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &images[i].p, nullptr)); + D3DLOCKED_RECT lock{}; HR(images[i]->LockRect(0, &lock, nullptr, 0)); + for (UINT y = 0; y < 2; ++y) for (UINT x = 0; x < 2; ++x) { + const DWORD color = i == 0 || (i == 2 && x == 0) ? 0xffff0000 : 0xff0000ff; + std::memcpy(static_cast(lock.pBits) + y * lock.Pitch + x * 4, &color, 4); + } + HR(images[i]->UnlockRect(0)); + } + auto shader = [&](const std::string& source) -> bool { + Ptr code; + Ptr compiled; + HR(D3DCompile(source.data(), source.size(), nullptr, nullptr, nullptr, "main", "ps_3_0", 0, 0, &code.p, nullptr)); + HR(f.dev->CreatePixelShader(static_cast(code->GetBufferPointer()), &compiled.p)); + HR(f.dev->SetPixelShader(compiled.p)); + return true; + }; + auto drawRead = [&](DWORD expected) -> bool { + HR(f.dev->BeginScene()); HR(f.dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)); HR(f.dev->EndScene()); + if (!f.wait()) return false; + HR(f.dev->GetRenderTargetData(target.p, readback.p)); + D3DLOCKED_RECT lock{}; HR(readback->LockRect(&lock, nullptr, D3DLOCK_READONLY)); + DWORD actual; std::memcpy(&actual, static_cast(lock.pBits) + 32 * lock.Pitch + 32 * 4, 4); + HR(readback->UnlockRect()); + for (UINT shift : {0u, 8u, 16u, 24u}) + CHECK(std::abs(int((actual >> shift) & 255) - int((expected >> shift) & 255)) <= 1); + return true; + }; + for (UINT slot : {0u, 5u, 15u}) { + const std::string prefix = "sampler2D tex:register(s" + std::to_string(slot) + ");"; + if (!shader(prefix + "float4 main():COLOR{return tex2D(tex,float2(0.5,0.5));}")) return false; + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_SRGBTEXTURE, FALSE)); + for (UINT frame = 0; frame < 4; ++frame) { + HR(f.dev->SetTexture(slot, images[frame & 1].p)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MINFILTER, (frame & 1) ? D3DTEXF_POINT : D3DTEXF_LINEAR)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MAGFILTER, (frame & 1) ? D3DTEXF_POINT : D3DTEXF_LINEAR)); + if (!drawRead(frame & 1 ? 0xff0000ff : 0xffff0000)) return false; + } + HR(f.dev->SetTexture(slot, images[2].p)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MINFILTER, D3DTEXF_POINT)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MAGFILTER, D3DTEXF_POINT)); + if (!drawRead(0xff0000ff)) return false; + HR(f.dev->SetSamplerState(slot, D3DSAMP_MINFILTER, D3DTEXF_LINEAR)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR)); + if (!drawRead(0xff800080)) return false; + if (!shader(prefix + "float4 main():COLOR{return tex2D(tex,float2(2,2));}")) return false; + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_BORDERCOLOR, 0xff00ff00)); + if (!drawRead(0xff00ff00)) return false; + HR(f.dev->SetSamplerState(slot, D3DSAMP_BORDERCOLOR, 0xffffff00)); + if (!drawRead(0xffffff00)) return false; + HR(f.dev->SetTexture(slot, nullptr)); + Ptr unbound; + HR(f.dev->GetTexture(slot, &unbound.p)); CHECK(unbound.p == nullptr); + } + // Two dirty slots retain the general batch path; restoring a state block or + // journal must also invalidate binding caches before the next draw. + if (!shader("sampler2D a:register(s0);sampler2D b:register(s5);" + "float4 main():COLOR{return 0.25*tex2D(a,float2(0.5,0.5))+0.75*tex2D(b,float2(0.5,0.5));}")) return false; + for (UINT slot : {0u, 5u}) { + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP)); + } + auto pair = [&](bool flip) -> bool { + HR(f.dev->SetTexture(0, images[flip ? 1 : 0].p)); + HR(f.dev->SetTexture(5, images[flip ? 0 : 1].p)); + for (UINT slot : {0u, 5u}) + HR(f.dev->SetSamplerState(slot, D3DSAMP_MAGFILTER, flip ? D3DTEXF_POINT : D3DTEXF_LINEAR)); + return true; + }; + if (!pair(false) || !drawRead(0xff4000bf)) return false; + Ptr block; + HR(f.dev->CreateStateBlock(D3DSBT_ALL, &block.p)); + if (!pair(true) || !drawRead(0xffbf0040)) return false; + HR(block->Apply()); if (!drawRead(0xff4000bf)) return false; + Ptr api; + HR(f.dev->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice6), reinterpret_cast(&api.p))); + HR(api->BeginStateJournal()); + if (!pair(true) || !drawRead(0xffbf0040)) return false; + HR(api->RestoreStateJournal()); if (!drawRead(0xff4000bf)) return false; + HR(f.dev->SetTexture(5, nullptr)); HR(f.dev->SetTexture(15, nullptr)); + // Restore the benchmark's shaders, streams and uniform white textures. + return draw_setup(f); +} + +bool texture_lifetime_pass(Fixture& f) { + Ptr original, readback; + HR(f.dev->GetRenderTarget(0, &original.p)); + HR(f.dev->CreateOffscreenPlainSurface(64, 64, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &readback.p, nullptr)); + auto image = [&](Ptr& texture, DWORD color) -> bool { + HR(f.dev->CreateTexture(2, 2, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture.p, nullptr)); + D3DLOCKED_RECT lock{}; HR(texture->LockRect(0, &lock, nullptr, 0)); + for (UINT y = 0; y < 2; ++y) for (UINT x = 0; x < 2; ++x) + std::memcpy(static_cast(lock.pBits) + y * lock.Pitch + x * 4, &color, 4); + HR(texture->UnlockRect(0)); + return true; + }; + auto pixel = [&](IDirect3DSurface9* target, DWORD expected) -> bool { + HR(f.dev->GetRenderTargetData(target, readback.p)); + D3DLOCKED_RECT lock{}; HR(readback->LockRect(&lock, nullptr, D3DLOCK_READONLY)); + DWORD actual; std::memcpy(&actual, static_cast(lock.pBits) + 32 * lock.Pitch + 32 * 4, 4); + HR(readback->UnlockRect()); + for (UINT shift : {0u, 8u, 16u, 24u}) + CHECK(std::abs(int((actual >> shift) & 255) - int((expected >> shift) & 255)) <= 1); + return true; + }; + auto draw = [&]() -> bool { + HR(f.dev->BeginScene()); HR(f.dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)); HR(f.dev->EndScene()); + return true; + }; + auto drawRead = [&](DWORD expected) -> bool { + return draw() && f.wait() && pixel(original.p, expected); + }; + for (bool multi : {false, true}) { + std::printf("case=texture-lifetime-%s\n", multi ? "multi" : "single"); + const char* source = multi + ? "sampler2D a:register(s0);sampler2D b:register(s5);float4 main():COLOR{return .25*tex2D(a,float2(.5,.5))+.75*tex2D(b,float2(.5,.5));}" + : "sampler2D a:register(s0);float4 main():COLOR{return tex2D(a,float2(.5,.5));}"; + Ptr code; Ptr shader; + HR(D3DCompile(source, std::strlen(source), nullptr, nullptr, nullptr, "main", "ps_3_0", 0, 0, &code.p, nullptr)); + HR(f.dev->CreatePixelShader(static_cast(code->GetBufferPointer()), &shader.p)); + HR(f.dev->SetPixelShader(shader.p)); + HR(f.dev->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE)); + for (UINT slot : {0u, 5u}) { + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MINFILTER, D3DTEXF_POINT)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_MAGFILTER, D3DTEXF_POINT)); + HR(f.dev->SetSamplerState(slot, D3DSAMP_SRGBTEXTURE, FALSE)); + } + Ptr red, blue, gray; + if (!image(red, 0xffff0000) || !image(blue, 0xff0000ff) || !image(gray, 0xff808080)) return false; + auto pair = [&](bool flip) -> bool { + HR(f.dev->SetTexture(0, flip ? blue.p : red.p)); + if (multi) HR(f.dev->SetTexture(5, flip ? red.p : blue.p)); + return true; + }; + const DWORD first = multi ? 0xff4000bf : 0xffff0000; + const DWORD second = multi ? 0xffbf0040 : 0xff0000ff; + if (!pair(false) || !drawRead(first)) return false; + // Dirty the slots then restore the original views without an intervening + // draw: the resource cache can elide the rebind, while pixels must persist. + if (!pair(true) || !pair(false) || !drawRead(first)) return false; + if (multi) { + // Exercise compacted pending entries with a hit before a miss, then a + // miss before a hit. Distinct colors make an incorrect slot visible. + HR(f.dev->SetTexture(0, blue.p)); HR(f.dev->SetTexture(0, red.p)); + HR(f.dev->SetTexture(5, gray.p)); + if (!drawRead(0xffa06060)) return false; + HR(f.dev->SetTexture(0, blue.p)); + HR(f.dev->SetTexture(5, blue.p)); HR(f.dev->SetTexture(5, gray.p)); + if (!drawRead(0xff6060a0)) return false; + if (!pair(false) || !drawRead(first)) return false; + } + Ptr block; + HR(f.dev->CreateStateBlock(D3DSBT_ALL, &block.p)); + if (!pair(true) || !drawRead(second)) return false; + HR(block->Apply()); if (!drawRead(first)) return false; + Ptr api; + HR(f.dev->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice6), reinterpret_cast(&api.p))); + HR(api->BeginStateJournal()); + if (!pair(true) || !drawRead(second)) return false; + HR(api->RestoreStateJournal()); if (!drawRead(first)) return false; + HR(f.dev->SetTexture(0, gray.p)); + if (multi) HR(f.dev->SetTexture(5, gray.p)); + if (!drawRead(0xff808080)) return false; + HR(f.dev->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, TRUE)); + if (multi) HR(f.dev->SetSamplerState(5, D3DSAMP_SRGBTEXTURE, TRUE)); + if (!drawRead(0xff373737)) return false; + HR(f.dev->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE)); + if (multi) HR(f.dev->SetSamplerState(5, D3DSAMP_SRGBTEXTURE, FALSE)); + if (!drawRead(0xff808080)) return false; + + // Queue different draws to different targets, dropping each application's + // texture references before the explicit completion fence. Later draws + // replace earlier cached views, so the cache cannot keep all of them alive. + std::array,8> targets; + for (UINT i = 0; i < targets.size(); ++i) { + Ptr a, b; + if (!image(a, i & 1 ? 0xff0000ff : 0xffff0000) || + !image(b, i & 1 ? 0xffff0000 : 0xff0000ff)) return false; + HR(f.dev->CreateRenderTarget(64, 64, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &targets[i].p, nullptr)); + HR(f.dev->SetRenderTarget(0, targets[i].p)); + HR(f.dev->SetTexture(0, a.p)); + if (multi) HR(f.dev->SetTexture(5, b.p)); + if (!draw()) return false; + HR(f.dev->SetTexture(0, nullptr)); + if (multi) HR(f.dev->SetTexture(5, nullptr)); + // a and b release at the end of this iteration, before the fence. + } + if (!f.wait()) return false; + for (UINT i = 0; i < targets.size(); ++i) + if (!pixel(targets[i].p, i & 1 ? second : first)) return false; + HR(f.dev->SetRenderTarget(0, original.p)); + HR(f.dev->SetTexture(0, nullptr)); HR(f.dev->SetTexture(5, nullptr)); + } + return true; +} + +bool texture_lifetime_check(Fixture& f) { + if (!texture_lifetime_pass(f) || !f.wait()) return false; + f.fence.reset(); + D3DPRESENT_PARAMETERS pp{}; + pp.BackBufferWidth = pp.BackBufferHeight = 64; + pp.BackBufferFormat = D3DFMT_A8R8G8B8; pp.BackBufferCount = 1; + pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = f.window; + pp.Windowed = TRUE; pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + HR(f.dev->Reset(&pp)); + HR(f.dev->CreateQuery(D3DQUERYTYPE_EVENT, &f.fence.p)); + if (!draw_setup(f)) return false; + std::printf("case=texture-lifetime-after-reset\n"); + return texture_lifetime_pass(f) && f.wait(); +} + +struct BenchTiming { + double cpuNs{}, completeNs{}, cycles{}; + DWORD cpuStart{}, cpuEnd{}; +}; + +bool draw_benchmark(Fixture& f, UINT mode, BenchTiming* timing = nullptr) { + constexpr UINT iterations = 16000; + std::array a{}, b{}; + for (UINT k = 12; k < 16; ++k) { a[k] = 0.25f; b[k] = 0.75f; } + HR(f.dev->BeginScene()); + auto bind = [&](UINT i) -> bool { + if (mode >= 5) { + HR(f.dev->SetTexture(0, f.textures[1].p)); + HR(f.dev->SetTexture(0, f.textures[0].p)); + if (mode == 6) { + HR(f.dev->SetTexture(5, f.textures[0].p)); + HR(f.dev->SetTexture(5, f.textures[1].p)); + } + if (mode == 7) HR(f.dev->SetTexture(5, f.textures[i & 1].p)); + if (mode == 8) HR(f.dev->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, i & 1)); + return true; + } + if (mode == 1 || mode >= 3) + HR(f.dev->SetTexture(0, f.textures[i & 1].p)); + if (mode >= 2) + HR(f.dev->SetSamplerState(0, D3DSAMP_MAGFILTER, (i & 1) ? D3DTEXF_POINT : D3DTEXF_LINEAR)); + if (mode == 4) { + HR(f.dev->SetTexture(5, f.textures[(i ^ 1) & 1].p)); + HR(f.dev->SetSamplerState(5, D3DSAMP_MAGFILTER, (i & 1) ? D3DTEXF_POINT : D3DTEXF_LINEAR)); + } + return true; + }; + for (UINT i = 0; i < 2000; ++i) { + if (!bind(i)) return false; + HR(f.dev->SetVertexShaderConstantF(0, (i & 1) ? a.data() : b.data(), 4)); + HR(f.dev->SetPixelShaderConstantF(0, (i & 1) ? a.data() : b.data(), 4)); + HR(f.dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)); + } + if (!f.wait()) return false; + const DWORD cpuStart = GetCurrentProcessorNumber(); + ULONG64 cyclesStart, cyclesEnd; + if (!QueryThreadCycleTime(GetCurrentThread(), &cyclesStart)) return false; + const auto begin = ticks(); + for (UINT i = 0; i < iterations; ++i) { + if (!bind(i)) return false; + HR(f.dev->SetVertexShaderConstantF(0, (i & 1) ? a.data() : b.data(), 4)); + HR(f.dev->SetPixelShaderConstantF(0, (i & 1) ? a.data() : b.data(), 4)); + HR(f.dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1)); + } + const auto submitted = ticks(); + if (!QueryThreadCycleTime(GetCurrentThread(), &cyclesEnd)) return false; + const DWORD cpuEnd = GetCurrentProcessorNumber(); + if (!f.wait()) return false; + const auto complete = ticks(); + HR(f.dev->EndScene()); + if (timing) { + *timing = {ns(submitted - begin, iterations), ns(complete - begin, iterations), + double(cyclesEnd - cyclesStart) / iterations, cpuStart, cpuEnd}; + return true; + } + const char* names[] = {"draw", "texture-draw", "sampler-draw", "bind-draw", "multi-bind-draw", + "texture-hit-draw", "texture-multi-hit-draw", "texture-mixed-draw", "texture-srgb-draw"}; + std::printf("%s,vs+ps,float,4,tail,%u,%.3f\n", names[mode], + iterations, ns(submitted - begin, iterations)); + std::printf("%s-complete,vs+ps,float,4,tail,%u,%.3f\n", names[mode], + iterations, ns(complete - begin, iterations)); + return true; +} + +int paired_benchmark(int argc, char** argv) { + auto number = [](const char* text, UINT& value) { + const auto end = text + std::strlen(text); + const auto result = std::from_chars(text, end, value); + return result.ec == std::errc{} && result.ptr == end; + }; + UINT rounds = 0, seed = 0, cpu = 0; + if (argc != 9 || (std::strcmp(argv[4], "on") && std::strcmp(argv[4], "off")) + || !number(argv[5], rounds) || !rounds || rounds > 16 || !number(argv[6], seed) + || (std::strcmp(argv[7], "any") && !number(argv[7], cpu)) + || (std::strcmp(argv[8], "controls") && std::strcmp(argv[8], "all") && std::strcmp(argv[8], "textures"))) { + std::fprintf(stderr, "usage: ... pair on|off any|cpu controls|all|textures\n"); + return 2; + } + struct CallerAffinity { + DWORD_PTR previous{}; + ~CallerAffinity() { if (previous) SetThreadAffinityMask(GetCurrentThread(), previous); } + } affinity; + const bool pinned = std::strcmp(argv[7], "any") != 0; + if (pinned) { + if (cpu >= sizeof(DWORD_PTR) * 8 || cpu >= GetActiveProcessorCount(0) + || !(affinity.previous = SetThreadAffinityMask(GetCurrentThread(), DWORD_PTR(1) << cpu))) { + std::fprintf(stderr, "FAIL caller-only diagnostic affinity, cpu=%u error=%lu\n", cpu, GetLastError()); + return 2; + } + } + // A/A uses the exact same device. For distinct DLLs rotate factory creation + // order across processes as well as the timed order within each process. + Fixture a, b; + Fixture* fixtures[] = {&a, &b}; + const bool shared = !_stricmp(argv[2], argv[3]); + if (shared) fixtures[1] = &a; + const bool enabled = !std::strcmp(argv[4], "on"); + if (shared) { + if (!a.open(argv[2], enabled)) return 2; + } else { + const UINT first = seed & 1; + if (!fixtures[first]->open(argv[2 + first], enabled) + || !fixtures[first ^ 1]->open(argv[2 + (first ^ 1)], enabled)) return 2; + } + if (!draw_setup(a) || !pixel_check(a) || !binding_pixel_check(a) || !a.wait()) return 1; + if (!shared && (!draw_setup(b) || !pixel_check(b) || !binding_pixel_check(b) || !b.wait())) return 1; + std::printf("{\"type\":\"metadata\",\"rounds\":%u,\"seed\":%u,\"profile\":\"%s\"," + "\"sharedDevice\":%s,\"baselineBase\":\"%p\",\"candidateBase\":\"%p\",\"pinned\":%s}\n", + rounds, seed, argv[4], shared ? "true" : "false", fixtures[0]->module, fixtures[1]->module, + pinned ? "true" : "false"); + struct Workload { + std::string name; + bool pixel{}, ints{}; + UINT count{}, pattern{}, mode{}; + }; + std::vector workloads; + const bool all = !std::strcmp(argv[8], "all"); + const bool textures = !std::strcmp(argv[8], "textures"); + const char* patterns[] = {"same", "first", "tail", "all"}; + for (bool pixel : {false, true}) for (bool ints : {false, true}) { + for (UINT count : {1u, 4u, 16u, 64u, 224u, 256u}) { + if (count > (ints ? 16u : pixel ? 224u : 256u)) continue; + for (UINT pattern = 0; pattern < 4; ++pattern) { + const bool control = (!pattern && ((!pixel && !ints && count == 4) + || (count == 16 && (pixel || ints)))) || (pixel && !ints && count == 64 && pattern == 3); + if (textures || (!all && !control)) continue; + const std::string name = std::string("setter,") + (pixel ? "ps," : "vs,") + + (ints ? "int," : "float,") + std::to_string(count) + "," + patterns[pattern]; + workloads.push_back({name, pixel, ints, count, pattern, 0}); + } + } + } + const char* draws[] = {"draw", "texture-draw", "sampler-draw", "bind-draw", "multi-bind-draw", + "texture-hit-draw", "texture-multi-hit-draw", "texture-mixed-draw", "texture-srgb-draw"}; + for (UINT mode = 0; mode < 9u; ++mode) { + if (textures ? (mode != 1 && mode != 4 && mode < 5) : mode >= (all ? 5u : 2u)) continue; + workloads.push_back({std::string(draws[mode]) + ",vs+ps,float,4,tail", false, false, 0, 0, mode}); + } + std::mt19937 random(seed); + std::shuffle(workloads.begin(), workloads.end(), random); + auto emit = [](const std::string& name, UINT variant, UINT round, UINT iterations, + const BenchTiming& t, bool complete) { + std::printf("{\"type\":\"sample\",\"case\":\"%s\",\"variant\":\"%s\",\"round\":%u," + "\"iterations\":%u,\"ns\":%.4f,\"cycles\":%.4f,\"cpuStart\":%lu,\"cpuEnd\":%lu}\n", + name.c_str(), variant ? "candidate" : "baseline", round, iterations, + complete ? t.completeNs : t.cpuNs, t.cycles, t.cpuStart, t.cpuEnd); + }; + for (const auto& w : workloads) { + std::vector fa(w.count * 4, 0.25f), fb = fa; + std::vector ia(w.count * 4, 1), ib = ia; + for (UINT i = 0; i < w.count * 4; ++i) { + const bool change = w.pattern == 3 || (w.pattern == 1 && i == 0) + || (w.pattern == 2 && i == (w.count - 1) * 4); + if (change) {fb[i] = 0.75f; ib[i] = 2;} + } + auto submit = [&](IDirect3DDevice9* dev, UINT i) { + if (w.ints) return w.pixel ? dev->SetPixelShaderConstantI(0, (i & 1) ? ia.data() : ib.data(), w.count) + : dev->SetVertexShaderConstantI(0, (i & 1) ? ia.data() : ib.data(), w.count); + return w.pixel ? dev->SetPixelShaderConstantF(0, (i & 1) ? fa.data() : fb.data(), w.count) + : dev->SetVertexShaderConstantF(0, (i & 1) ? fa.data() : fb.data(), w.count); + }; + for (UINT round = 0; round < rounds; ++round) for (UINT order = 0; order < 2; ++order) { + const UINT variant = order ^ ((round + seed) & 1); + Fixture& f = *fixtures[variant]; + BenchTiming t; + if (!w.count) { + if (!draw_benchmark(f, w.mode, &t)) return 1; + emit(w.name, variant, round, 16000, t, false); + const std::string completed = std::string(draws[w.mode]) + "-complete,vs+ps,float,4,tail"; + emit(completed, variant, round, 16000, t, true); + continue; + } + auto measure = [&]() -> bool { + for (UINT i = 0; i < 10000; ++i) HR(submit(f.dev.p, i)); + ULONG64 beforeCycles, afterCycles; + t.cpuStart = GetCurrentProcessorNumber(); + if (!QueryThreadCycleTime(GetCurrentThread(), &beforeCycles)) return false; + const auto begin = ticks(); + HRESULT status = D3D_OK; + for (UINT i = 0; i < 300000; ++i) status |= submit(f.dev.p, i); + const auto end = ticks(); + if (!QueryThreadCycleTime(GetCurrentThread(), &afterCycles)) return false; + t.cpuEnd = GetCurrentProcessorNumber(); + HR(status); + t.cpuNs = ns(end - begin, 300000); + t.cycles = double(afterCycles - beforeCycles) / 300000; + return true; + }; + if (!measure()) return 1; + emit(w.name, variant, round, 300000, t, false); + } + } + return api_test::finish(); +} + +int main(int argc, char** argv) { + if (argc > 1 && !std::strcmp(argv[1], "pair")) return paired_benchmark(argc, argv); + if (argc != 4 || (std::strcmp(argv[2], "on") && std::strcmp(argv[2], "off")) + || (std::strcmp(argv[3], "check") && std::strcmp(argv[3], "bench") && std::strcmp(argv[3], "lifetime"))) { + std::fprintf(stderr, "usage: constant-submit-benchmark on|off check|bench|lifetime\n"); return 2; + } + Fixture f; + if (!f.open(argv[1], !std::strcmp(argv[2], "on"))) return 2; + const bool bench = !std::strcmp(argv[3], "bench"); + if (bench && !setters(f)) return 1; + if (!draw_setup(f) || !pixel_check(f) || !binding_pixel_check(f)) return 1; + if (!std::strcmp(argv[3], "lifetime") && !texture_lifetime_check(f)) return 1; + if (bench) for (UINT mode = 0; mode < 5; ++mode) if (!draw_benchmark(f, mode)) return 1; + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/fading_runtime.cpp b/src/bridge/legacy/api-projects/tests/fading_runtime.cpp new file mode 100644 index 0000000..279cf6d --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/fading_runtime.cpp @@ -0,0 +1,190 @@ +#include "test_check.hpp" +#include "../../ProperShadersRenderStateGuard.h" +// The test process heap can reserve GTA's absolute API pages. Supply only +// those external API addresses from disposable executable memory; keep the +// production installer, hook target, gateway and shutdown code unchanged. +static uintptr_t fixtureSetState, fixtureGetState; +#define kRwD3D9SetRenderState fixtureSetState +#define kRwD3D9GetRenderState fixtureGetState +// Compile the production installer/guard/gateway in this executable so the +// regression exercises their actual patch and call paths, including shutdown. +#include "../../ProperShadersRenderStateGuard.cpp" +#undef kRwD3D9SetRenderState +#undef kRwD3D9GetRenderState + +static_assert(sizeof(void*) == 4, "The ProperShaders hook contract is x86."); + +namespace { +uint8_t* image; +unsigned loggerCalls, bodyCalls; +DWORD beforeEsp, afterEsp, beforeEbp, afterEbp, afterEbx, afterEsi, afterEdi, afterEcx; +constexpr DWORD sentinel = 0x13572468; + +void __cdecl LoggerProbe(void* context, const char* name) { + ++loggerCalls; + CHECK(name == reinterpret_cast(image + 0x14156C)); + CHECK(context == image + 0x16C108); +} +void __cdecl BodyProbe() { + ++bodyCalls; + CHECK(image[0x16BF96] == 1); + __asm { xor ecx, ecx } + __asm { mov edx, 55667788h } +} + +__declspec(naked) void Invoke(void* target) { + __asm { + push ebp + mov ebp, esp + push ebx + push esi + push edi + mov ebx, 11223344h + mov esi, 22334455h + mov edi, 33445566h + mov ecx, sentinel + mov eax, [ebp + 8] + mov beforeEsp, esp + mov beforeEbp, ebp + call eax + mov afterEsp, esp + mov afterEbp, ebp + mov afterEcx, ecx + mov afterEbx, ebx + mov afterEsi, esi + mov afterEdi, edi + lea esp, [ebp - 12] + pop edi + pop esi + pop ebx + pop ebp + ret + } +} +DWORD InvokeCaught(void* target) { + __try { Invoke(target); return 0; } + __except (EXCEPTION_EXECUTE_HANDLER) { return GetExceptionCode(); } +} +void Word(uint8_t* destination, uintptr_t value) { + const auto word = static_cast(value); + std::memcpy(destination, &word, sizeof(word)); +} +void MakeFunction() { + // ProperShaders 526DBD01 RenderFadingEntities, with only its absolute + // operands and logger rel32 relocated into this disposable executable image. + const uint8_t bytes[] = { + 0x51, 0x68,0,0,0,0, 0x68,0,0,0,0, 0xE8,0,0,0,0, 0x83,0xC4,0x08, + 0xC6,0x05,0,0,0,0,0x01, 0xFF,0x15,0,0,0,0, + 0xC6,0x05,0,0,0,0,0x00, 0x59,0xC3 + }; + auto* entry = image + 0x25590; + DWORD old; + CHECK(VirtualProtect(entry, sizeof(bytes), PAGE_EXECUTE_READWRITE, &old)); + std::memcpy(entry, bytes, sizeof(bytes)); + Word(entry + 2, uintptr_t(image + 0x14156C)); + Word(entry + 7, uintptr_t(image + 0x16C108)); + Word(entry + 12, uintptr_t(&LoggerProbe) - uintptr_t(entry + 16)); + Word(entry + 21, uintptr_t(image + 0x16BF96)); + Word(entry + 28, uintptr_t(image + 0x16C104)); + Word(entry + 34, uintptr_t(image + 0x16BF96)); + Word(image + 0x16C104, uintptr_t(&BodyProbe)); + CHECK(FlushInstructionCache(GetCurrentProcess(), entry, sizeof(bytes))); + CHECK(VirtualProtect(entry, sizeof(bytes), PAGE_EXECUTE_READ, &old)); +} +void VerifyCall(void* target, bool gateway) { + const auto oldLogger = loggerCalls, oldBody = bodyCalls; + const DWORD exception = InvokeCaught(target); + std::printf("call gateway=%d exception=0x%08lX\n", gateway, exception); + CHECK(exception == 0); + if (exception) return; + CHECK(beforeEsp == afterEsp); + CHECK(beforeEbp == afterEbp); + CHECK(afterEbx == 0x11223344); + CHECK(afterEsi == 0x22334455); + CHECK(afterEdi == 0x33445566); + // ECX is volatile at the C++ guard boundary. The original/gateway pair + // independently verifies the saved stack slot is consumed by pop ecx. + if (gateway) CHECK(afterEcx == sentinel); + CHECK(loggerCalls == oldLogger + 1); + CHECK(bodyCalls == oldBody + 1); + CHECK(image[0x16BF96] == 0); +} + +void RoundTrip(bool trace) { + MakeFunction(); + auto* entry = image + 0x25590; + std::array original; + std::memcpy(original.data(), entry, original.size()); + VerifyCall(entry, true); + g_properShadersRenderStateGuardConfig.enable = true; + g_properShadersRenderStateGuardConfig.enableStateRing = trace; + if (trace) CHECK(BeginProperShadersStateTraceCapture()); + InstallProperShadersFadingStateGuard(reinterpret_cast(image), kSupportedProperShadersTextHash); + CHECK(g_properShadersFadingStateGuardInstallState == 2); + CHECK(g_properShadersRenderFadingEntitiesTrampoline != 0); + const auto gateway = g_properShadersRenderFadingEntitiesTrampoline; + CHECK(entry[0] == 0xE9 && entry[5] == 0x90); + CHECK(DecodeRel32JumpTarget(uintptr_t(entry)) == + uintptr_t(&Bridge_ProperShadersRenderFadingEntitiesGuard)); + MEMORY_BASIC_INFORMATION memory{}; + CHECK(VirtualQuery(entry, &memory, sizeof(memory)) == sizeof(memory)); + CHECK(memory.Protect == PAGE_EXECUTE_READ); + InstallProperShadersFadingStateGuard(reinterpret_cast(image), kSupportedProperShadersTextHash); + CHECK(g_properShadersRenderFadingEntitiesTrampoline == gateway); + if (gateway) { + CHECK(std::memcmp(reinterpret_cast(gateway), original.data(), 6) == 0); + CHECK(DecodeRel32JumpTarget(gateway + 6) == uintptr_t(entry + 6)); + VerifyCall(reinterpret_cast(gateway), true); + VerifyCall(entry, false); + VerifyCall(entry, false); + } + CHECK(ShutdownProperShadersRenderStateGuards()); + CHECK(g_properShadersRenderFadingEntitiesTrampoline == 0); + CHECK(g_properShadersFadingStateGuardInstallState == 0); + CHECK(std::memcmp(original.data(), entry, original.size()) == 0); + CHECK(VirtualQuery(entry, &memory, sizeof(memory)) == sizeof(memory)); + CHECK(memory.Protect == PAGE_EXECUTE_READ); + VerifyCall(entry, true); +} +void RejectWrongEntry() { + for (size_t offset : {size_t(0), size_t(1), size_t(2)}) { + MakeFunction(); + auto* entry = image + 0x25590; + DWORD old, ignored; + CHECK(VirtualProtect(entry, 6, PAGE_EXECUTE_READWRITE, &old)); + entry[offset] ^= 1; // Independently reject push ecx, push imm32 and its address. + CHECK(FlushInstructionCache(GetCurrentProcess(), entry, 6)); + CHECK(VirtualProtect(entry, 6, old, &ignored)); + std::array before; + std::memcpy(before.data(), entry, before.size()); + InstallProperShadersFadingStateGuard(reinterpret_cast(image), kSupportedProperShadersTextHash); + CHECK(g_properShadersFadingStateGuardInstallState == 0); + CHECK(g_properShadersRenderFadingEntitiesTrampoline == 0); + CHECK(std::memcmp(before.data(), entry, before.size()) == 0); + CHECK(ShutdownProperShadersRenderStateGuards()); + } + MakeFunction(); + InstallProperShadersFadingStateGuard(reinterpret_cast(image), 0); + CHECK(g_properShadersFadingStateGuardInstallState == 0); +} +} + +int main() { + auto* apiPage = VirtualAlloc(nullptr, 4096, + MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); + image = static_cast(VirtualAlloc(nullptr, 0x180000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); + CHECK(image != nullptr); + CHECK(apiPage != nullptr); + if (!image || !apiPage) return api_test::finish(); + fixtureSetState = uintptr_t(apiPage); + fixtureGetState = uintptr_t(apiPage) + 16; + *reinterpret_cast(fixtureSetState) = 0xC3; + *reinterpret_cast(fixtureGetState) = 0xC3; + RoundTrip(false); + RoundTrip(true); + RejectWrongEntry(); + CHECK(ShutdownProperShadersRenderStateGuards()); + CHECK(VirtualFree(apiPage, 0, MEM_RELEASE)); + CHECK(VirtualFree(image, 0, MEM_RELEASE)); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/lifecycle_runtime.cpp b/src/bridge/legacy/api-projects/tests/lifecycle_runtime.cpp new file mode 100644 index 0000000..97e08b5 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/lifecycle_runtime.cpp @@ -0,0 +1,222 @@ +#include "test_check.hpp" +#include "../../../../../backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h" +#include +#include +#include +#include +#include + +namespace { +struct Events { + std::vector events; + std::vector resets; + DWORD owner = GetCurrentThreadId(); + HANDLE entered{}, resume{}; + static void STDMETHODCALLTYPE notify(void* context, IDirect3DDevice9* device, + D3D9BridgeEvent event, HRESULT result, D3DPRESENT_PARAMETERS*, const D3D9BridgePresentArgs* present) { + auto& e = *static_cast(context); + CHECK(device != nullptr); + e.events.push_back(event); + if (event == D3D9BridgeEvent::PresentBefore || event == D3D9BridgeEvent::PresentAfter) + CHECK(present != nullptr); + if (event == D3D9BridgeEvent::ResetAfter) e.resets.push_back(result); + if (event == D3D9BridgeEvent::PresentBefore && e.entered) { + SetEvent(e.entered); + CHECK(WaitForSingleObject(e.resume, 5000) == WAIT_OBJECT_0); + } + } +}; +struct Fixture { + HMODULE library{}; + HWND window{}; + IDirect3D9* factory{}; + D3DPRESENT_PARAMETERS params{}; + ~Fixture() { + if (factory) factory->Release(); + if (window) DestroyWindow(window); + if (library) FreeLibrary(library); + } + bool open(const char* dll, bool extended = false) { + SetEnvironmentVariableA("DXVK_CONFIG", "d3d9.gtaSaCompat=True;d3d9.presentInterval=0;dxvk.hud="); + SetEnvironmentVariableA("DXVK_CONFIG_FILE", "lifecycle-no-external.conf"); + SetEnvironmentVariableA("DXVK_LOG_LEVEL", "error"); + library = LoadLibraryA(dll); + if (!library) return false; + using Create = IDirect3D9* (WINAPI*)(UINT); + auto create = reinterpret_cast(GetProcAddress(library, "Direct3DCreate9")); + if (!create) return false; + if (extended) { + using CreateEx = HRESULT (WINAPI*)(UINT, IDirect3D9Ex**); + auto ex = reinterpret_cast(GetProcAddress(library, "Direct3DCreate9Ex")); + IDirect3D9Ex* result{}; + if (!ex || FAILED(ex(D3D_SDK_VERSION, &result))) return false; + factory = result; + } else factory = create(D3D_SDK_VERSION); + WNDCLASSA wc{}; wc.lpfnWndProc = DefWindowProcA; + wc.hInstance = GetModuleHandleA(nullptr); wc.lpszClassName = "RenderStackLifecycle"; + RegisterClassA(&wc); + window = CreateWindowA(wc.lpszClassName, "lifecycle", WS_OVERLAPPEDWINDOW, 0,0,128,128,nullptr,nullptr,wc.hInstance,nullptr); + ShowWindow(window, SW_SHOWNOACTIVATE); + UpdateWindow(window); + params.BackBufferWidth = params.BackBufferHeight = 64; + params.BackBufferFormat = D3DFMT_A8R8G8B8; params.BackBufferCount = 1; + params.SwapEffect = D3DSWAPEFFECT_DISCARD; params.hDeviceWindow = window; + params.Windowed = TRUE; params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + return factory && window; + } + IDirect3DDevice9* create() { + IDirect3DDevice9* d{}; + CHECK(SUCCEEDED(factory->CreateDevice(0, D3DDEVTYPE_HAL, window, + D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, ¶ms, &d))); + return d; + } + void finish_present(IDirect3DDevice9* device) { + // Present queues CS work. Wait for a real GPU event before Reset/Release + // may unregister queued pass callbacks; no timing-based observation. + IDirect3DQuery9* fence{}; + CHECK(SUCCEEDED(device->CreateQuery(D3DQUERYTYPE_EVENT, &fence))); + if (!fence) return; + CHECK(SUCCEEDED(fence->Issue(D3DISSUE_END))); + HRESULT ready = S_FALSE; + const auto deadline = GetTickCount64() + 5000; + while (ready == S_FALSE && GetTickCount64() < deadline) + ready = fence->GetData(nullptr,0,D3DGETDATA_FLUSH); + CHECK(ready == S_OK); + fence->Release(); + } +}; + +void backend(Fixture& f) { + auto* device = f.create(); if (!device) return; + ID3D9BridgeLifecycle* lifecycle{}; + const HRESULT qi = device->QueryInterface(IID_D3D9BridgeLifecycle, reinterpret_cast(&lifecycle)); + CHECK(qi == S_OK); + if (FAILED(qi)) { device->Release(); return; } + Events e; + D3D9BridgeObserver desc{sizeof(desc), &e, &Events::notify}; + CHECK(lifecycle->SetObserver(&desc) == S_OK); + CHECK(lifecycle->SetObserver(&desc) == D3DERR_INVALIDCALL); + auto invalid = desc; --invalid.StructSize; + CHECK(lifecycle->SetObserver(&invalid) == D3DERR_INVALIDCALL); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + auto bad = f.params; bad.SwapEffect = static_cast(0); + CHECK(FAILED(device->Reset(&bad))); + CHECK(SUCCEEDED(device->Reset(&f.params))); + CHECK(e.resets.size() == 2); + if (e.resets.size() == 2) { CHECK(FAILED(e.resets[0])); CHECK(e.resets[1] == S_OK); } + CHECK(e.events.size() == 6); + if (e.events.size() == 6) { + CHECK(e.events[0] == D3D9BridgeEvent::PresentBefore); + CHECK(e.events[1] == D3D9BridgeEvent::PresentAfter); + CHECK(e.events[2] == D3D9BridgeEvent::ResetBefore); + CHECK(e.events[3] == D3D9BridgeEvent::ResetAfter); + CHECK(e.events[4] == D3D9BridgeEvent::ResetBefore); + CHECK(e.events[5] == D3D9BridgeEvent::ResetAfter); + } + IDirect3DDevice9Ex* extended{}; + if (SUCCEEDED(device->QueryInterface(__uuidof(IDirect3DDevice9Ex), reinterpret_cast(&extended)))) { + auto resetCount = e.resets.size(); + CHECK(FAILED(extended->ResetEx(&bad, nullptr))); + CHECK(SUCCEEDED(extended->ResetEx(&f.params, nullptr))); + CHECK(e.resets.size() == resetCount + 2); + CHECK(SUCCEEDED(extended->PresentEx(nullptr,nullptr,nullptr,nullptr,0))); + extended->Release(); + } + CHECK(lifecycle->SetObserver(nullptr) == S_OK); + auto count = e.events.size(); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + CHECK(e.events.size() == count); + CHECK(lifecycle->SetObserver(&desc) == S_OK); + lifecycle->Release(); // registration is borrowed; no device retention + CHECK(device->Release() == 0); + CHECK(e.events.back() == D3D9BridgeEvent::FinalRelease); + + // Real concurrent unregister cannot return while callback data is in use. + device = f.create(); if (!device) return; + CHECK(SUCCEEDED(device->QueryInterface(IID_D3D9BridgeLifecycle, reinterpret_cast(&lifecycle)))); + if (!lifecycle) { device->Release(); return; } + Events blocked; + blocked.entered = CreateEventA(nullptr,TRUE,FALSE,nullptr); + blocked.resume = CreateEventA(nullptr,TRUE,FALSE,nullptr); + desc.Context = &blocked; + CHECK(lifecycle->SetObserver(&desc) == S_OK); + std::thread present([&] { CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); }); + CHECK(WaitForSingleObject(blocked.entered, 5000) == WAIT_OBJECT_0); + HANDLE cleared = CreateEventA(nullptr,TRUE,FALSE,nullptr); + std::thread clear([&] { CHECK(lifecycle->SetObserver(nullptr) == S_OK); SetEvent(cleared); }); + CHECK(WaitForSingleObject(cleared, 50) == WAIT_TIMEOUT); + SetEvent(blocked.resume); present.join(); clear.join(); + count = blocked.events.size(); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + CHECK(blocked.events.size() == count); + CloseHandle(cleared); CloseHandle(blocked.entered); CloseHandle(blocked.resume); + lifecycle->Release(); CHECK(device->Release() == 0); +} + +void bridge(Fixture& f, bool raw) { + auto* first = f.create(); + if (first) CHECK(first->Release() == 0); + ID3D9BridgeRuntime* runtime{}; + auto qi = f.factory->QueryInterface(IID_D3D9BridgeRuntime, reinterpret_cast(&runtime)); + CHECK(qi == S_OK); if (FAILED(qi)) return; + for (int i=0; i<3; ++i) { + auto* device = f.create(); if (!device) break; + if (raw) { + ID3D9BridgeLifecycle* observer{}; + CHECK(SUCCEEDED(device->QueryInterface(IID_D3D9BridgeLifecycle, reinterpret_cast(&observer)))); + if (observer) { + Events events; + D3D9BridgeObserver desc{sizeof(desc), &events, &Events::notify}; + CHECK(observer->SetObserver(&desc) == D3DERR_INVALIDCALL); + observer->Release(); + } + } + CHECK(runtime->Shutdown() == D3DERR_INVALIDCALL); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + f.finish_present(device); + auto bad=f.params; bad.SwapEffect=static_cast(0); + CHECK(FAILED(device->Reset(&bad))); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + f.finish_present(device); + CHECK(SUCCEEDED(device->Reset(&f.params))); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + f.finish_present(device); + CHECK(device->Release() == 0); + } + auto* primary = f.create(); + auto* secondary = f.create(); + if (primary) CHECK(primary->Release() == 0); + if (secondary) { + CHECK(runtime->Shutdown() == D3DERR_INVALIDCALL); + CHECK(SUCCEEDED(secondary->Present(nullptr,nullptr,nullptr,nullptr))); + f.finish_present(secondary); + CHECK(secondary->Release() == 0); + } + const auto shutdownStart = GetTickCount64(); + CHECK(runtime->Shutdown() == S_OK); + CHECK(GetTickCount64() - shutdownStart < 5000); + CHECK(runtime->Shutdown() == S_FALSE); + IDirect3DDevice9* rejected{}; + CHECK(f.factory->CreateDevice(0,D3DDEVTYPE_HAL,f.window,D3DCREATE_HARDWARE_VERTEXPROCESSING,&f.params,&rejected) == D3DERR_INVALIDCALL); + CHECK(rejected == nullptr); + runtime->Release(); +} +} +int main(int argc, char** argv) { + if (argc!=3) return 2; + if (std::strcmp(argv[2], "load-only") == 0) { + CHECK(SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS)); + const DWORD before = GetPriorityClass(GetCurrentProcess()); + auto library = LoadLibraryA(argv[1]); + CHECK(library != nullptr); + CHECK(GetPriorityClass(GetCurrentProcess()) == before); + CHECK(GetFileAttributesA("scripts/BridgeD3D9.log") == INVALID_FILE_ATTRIBUTES); + if (library) CHECK(FreeLibrary(library)); + CHECK(GetModuleHandleA(argv[1]) == nullptr); + return api_test::finish(); + } + Fixture f; + if (!f.open(argv[1], std::strcmp(argv[2], "backend-ex") == 0)) { std::fprintf(stderr,"device fixture failed: %lu\n",GetLastError()); return 2; } + if (std::strncmp(argv[2],"backend",7)==0) backend(f); else bridge(f, std::strcmp(argv[2], "bridge-raw") == 0); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/log_failure_tests.cpp b/src/bridge/legacy/api-projects/tests/log_failure_tests.cpp new file mode 100644 index 0000000..744b437 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/log_failure_tests.cpp @@ -0,0 +1,35 @@ +#include "test_check.hpp" +#include "../../BridgeLog.h" +#include +#include +#include + +void invalid_parameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, uintptr_t) { } + +int main() { + _set_invalid_parameter_handler(invalid_parameter); + FILE* output = tmpfile(); + CHECK(output != nullptr); + if (!output) return api_test::finish(); + BridgeDiagnostics::Write(output, "device {}", 42); + rewind(output); + char line[160]{}; + CHECK(fgets(line,sizeof(line),output) != nullptr); + CHECK(std::strstr(line, "device 42\n") != nullptr); + fclose(output); + + output = tmpfile(); + CHECK(output != nullptr); + if (!output) return api_test::finish(); + CHECK(setvbuf(output,nullptr,_IONBF,0) == 0); + // Actual CRT I/O failure, not a throwing mock: close the underlying file + // descriptor while retaining the FILE object that the logger writes through. + CHECK(_close(_fileno(output)) == 0); + bool threw = false; + try { BridgeDiagnostics::Write(output, "release {}", 1); } + catch (...) { threw = true; } + CHECK(!threw); + CHECK(ferror(output) != 0); + fclose(output); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/measurement_tests.cpp b/src/bridge/legacy/api-projects/tests/measurement_tests.cpp new file mode 100644 index 0000000..ca4c79e --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/measurement_tests.cpp @@ -0,0 +1,134 @@ +#include "test_check.hpp" +#include +#include +#include + +#if __has_include("../../BridgeMeasurement.h") +#include "../../BridgeMeasurement.h" +using namespace BridgeMeasurement; + +void phases() { + Controller c({5000, 10000, 3000, 50}); + CHECK(!c.Key(false, false, 0)); + CHECK(c.Key(false, true, 0)); + CHECK(c.phase() == Phase::Baseline); + CHECK(!c.Key(false, true, 1)); + CHECK(!c.Key(true, true, 2)); + CHECK(c.Present(1'000'000'000ull, true) == Action::None); + CHECK(c.Present(5'000'000'000ull, true) == Action::StartCpu); + CHECK(c.phase() == Phase::Cpu); + CHECK(c.stats(0).presents == 2); + CHECK(c.stats(0).fps() == 0.4); + CHECK(c.Present(15'000'000'000ull, true) == Action::None); + CHECK(c.phase() == Phase::CpuFinishing); + CHECK(c.stats(1).presents == 1); + CHECK(c.stats(1).elapsedNs() == 10'000'000'000ull); + c.CpuFinished(true); + CHECK(c.Present(16'000'000'000ull, true) == Action::StartAttribution); + CHECK(c.phase() == Phase::Attribution); + CHECK(c.Present(19'000'000'000ull, true) == Action::FinishAttribution); + CHECK(c.phase() == Phase::Complete); + CHECK(c.stats(1).elapsedNs() == 10'000'000'000ull); + CHECK(!c.Key(true, true, 20'000'000'000ull)); + CHECK(!c.Key(false, false, 20'000'000'001ull)); + CHECK(c.Key(true, false, 20'000'000'002ull)); + CHECK(c.captureId() == 2); + CHECK(c.stats(0).presents == 0); +} + +void failures() { + Controller c({1, 1, 1, 50}); + CHECK(c.Key(true, false, 1)); + CHECK(c.Present(1'000'001, false) == Action::StartCpu); + CHECK(c.stats(0).presents == 0); + CHECK(c.stats(0).failedPresents == 1); + c.CpuFinished(false); + CHECK(c.Present(2'000'001, true) == Action::ReportError); + CHECK(c.phase() == Phase::Error); + CHECK(!c.active()); + c.Key(false, false, 3'000'001); + CHECK(c.Key(false, true, 3'000'002)); + c.Interrupt(); + CHECK(c.phase() == Phase::Interrupted); + CHECK(!c.active()); + Controller missing({1, 1, 1, 50}); + missing.SetAvailable(false, false); + CHECK(missing.Key(false, true, 0)); + CHECK(missing.Present(1'000'000, true) == Action::FinishAttribution); + CHECK(missing.phase() == Phase::Complete); + CHECK(missing.unavailableMask() == 6); + CHECK(missing.stats(1).presents == 0); + CHECK(missing.stats(2).presents == 0); +} + +void frame_math() { + FrameStats stats; + stats.Begin(1'000'000'000ull); + CHECK(stats.Observe(1'020'000'000ull, true)); + CHECK(!stats.Observe(1'030'000'000ull, false)); + CHECK(stats.Observe(1'060'000'000ull, true)); + CHECK(!stats.Observe(1'050'000'000ull, true)); + stats.Finish(1'100'000'000ull); + CHECK(stats.presents == 2); + CHECK(stats.failedPresents == 1); + CHECK(stats.invalidTimes == 1); + CHECK(stats.fps() == 20.0); + CHECK(stats.maxIntervalNs == 40'000'000ull); + CHECK(stats.averageIntervalNs() == 30'000'000.0); + CHECK(stats.elapsedNs() == 100'000'000ull); + FrameTrace trace; + trace.Add(10, 20, 1, Phase::Cpu); + trace.Add(20, 90, 2, Phase::Cpu); + CHECK(trace.Find(10)->frameId == 1); + CHECK(trace.Find(19)->frameId == 1); + CHECK(trace.Find(20)->frameId == 2); + CHECK(trace.Find(90) == nullptr); + CHECK(trace.Find(9) == nullptr); + CHECK(!trace.IsSlow(15, 50)); + CHECK(trace.IsSlow(30, 50)); + for (std::uint64_t i = 0; i < FrameTrace::Capacity + 2; ++i) + trace.Add(100 + i * 10, 110 + i * 10, i + 3, Phase::Cpu); + CHECK(trace.size() == FrameTrace::Capacity); + CHECK(trace.dropped() == 4); +} + +void jitter_and_frames() { + Jitter jitter(7); + std::uint64_t total = 0; + bool sawLow = false, sawHigh = false; + for (int i = 0; i < 10000; ++i) { + const auto interval = jitter.Next(5); + CHECK(interval >= 4 && interval <= 6); + total += interval; + sawLow |= interval == 4; + sawHigh |= interval == 6; + } + CHECK(sawLow && sawHigh); + CHECK(total >= 49'000 && total <= 51'000); + CHECK(jitter.Next(0) == 1); + CHECK(jitter.Next(1) == 1); + std::array words{}; + words[2] = 0x1018; words[3] = 0x2030; + words[6] = 0; words[7] = 0x2050; + const auto executable = [](std::uintptr_t a) { return a >= 0x2000 && a < 0x3000; }; + const auto chain = WalkCapturedFrames(0x1000, words.data(), sizeof(words), 0x1008, executable); + CHECK(chain.count == 2); + CHECK(chain.addresses[0] == 0x2030); + CHECK(chain.addresses[1] == 0x2050); + CHECK(chain.quality == ChainQuality::FrameChain); + words[6] = 0x1008; + CHECK(WalkCapturedFrames(0x1000, words.data(), sizeof(words), 0x1008, executable).quality == ChainQuality::Invalid); + CHECK(WalkCapturedFrames(0x1000, words.data(), sizeof(words), 0x9000, executable).count == 0); + words[3] = 0x100000; + CHECK(WalkCapturedFrames(0x1000, words.data(), sizeof(words), 0x1008, executable).count == 0); +} +int main() { + phases(); failures(); frame_math(); jitter_and_frames(); + return api_test::finish(); +} +#else +int main() { + CHECK(false && "production measurement controller is missing"); + return api_test::finish(); +} +#endif diff --git a/src/bridge/legacy/api-projects/tests/state_runtime.cpp b/src/bridge/legacy/api-projects/tests/state_runtime.cpp new file mode 100644 index 0000000..b09e521 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/state_runtime.cpp @@ -0,0 +1,545 @@ +#include "test_check.hpp" +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace { +template class ComPtr { +public: + ComPtr() = default; + ComPtr(const ComPtr&) = delete; + ~ComPtr() { reset(); } + void reset() { if (ptr_) ptr_->Release(); ptr_ = nullptr; } + T** out() { reset(); return &ptr_; } + T* get() const { return ptr_; } + T* operator->() const { return ptr_; } +private: + T* ptr_{}; +}; + +struct Device { + HMODULE module{}; + HWND window{}; + ComPtr d3d; + ComPtr device; + ComPtr api; + D3DPRESENT_PARAMETERS params{}; + wchar_t configFile[MAX_PATH]{}; + + ~Device() { + api.reset(); device.reset(); d3d.reset(); + if (window) DestroyWindow(window); + if (module) FreeLibrary(module); + if (configFile[0]) DeleteFileW(configFile); + } + + bool open(const char* backend, bool optimized) { + const char* enabled = optimized ? "True" : "False"; + std::string config = std::string("d3d9.gtaSaCompat=True;d3d9.gtaSaCompatDiagnostics=False;") + + "d3d9.presentInterval=0;dxvk.hud=;d3d9.gtaSaStateBlockPrefilter=" + enabled + + ";d3d9.gtaSaStateBlockFastSkip=" + enabled + + ";d3d9.gtaSaCoalesceSamplerBindings=" + enabled + + ";d3d9.gtaSaCoalesceTextureBindings=" + enabled + + ";d3d9.gtaSaResourceBindingCache=" + enabled + + ";d3d9.gtaSaConstantDedup=" + enabled; + // DXVK's Windows environment reader rejects values >= MAX_PATH. The full + // profile exceeds that length, so use a unique, disposable config file. + for (auto& c : config) if (c == ';') c = '\n'; + wchar_t tempDirectory[MAX_PATH]; + const DWORD tempLength = GetTempPathW(MAX_PATH, tempDirectory); + if (!tempLength || tempLength >= MAX_PATH + || !GetTempFileNameW(tempDirectory, L"rss", 0, configFile)) return false; + HANDLE file = CreateFileW(configFile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (file == INVALID_HANDLE_VALUE) return false; + DWORD written = 0; + const BOOL saved = WriteFile(file, config.data(), DWORD(config.size()), &written, nullptr); + CloseHandle(file); + if (!saved || written != config.size()) return false; + SetEnvironmentVariableA("DXVK_CONFIG", nullptr); + SetEnvironmentVariableW(L"DXVK_CONFIG_FILE", configFile); + SetEnvironmentVariableA("DXVK_LOG_LEVEL", "error"); + module = LoadLibraryA(backend); + if (!module) return false; + using Create9 = IDirect3D9* (WINAPI*)(UINT); + const auto create = reinterpret_cast(GetProcAddress(module, "Direct3DCreate9")); + if (!create) return false; + constexpr char name[] = "RenderStackStateCorrectness"; + WNDCLASSA wc{}; + wc.lpfnWndProc = DefWindowProcA; + wc.hInstance = GetModuleHandleA(nullptr); + wc.lpszClassName = name; + if (!RegisterClassA(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) return false; + window = CreateWindowA(name, name, 0, 0, 0, 64, 64, nullptr, nullptr, wc.hInstance, nullptr); + if (!window) return false; + *d3d.out() = create(D3D_SDK_VERSION); + if (!d3d.get()) return false; + params.BackBufferWidth = params.BackBufferHeight = 64; + params.BackBufferFormat = D3DFMT_A8R8G8B8; + params.BackBufferCount = 1; + params.SwapEffect = D3DSWAPEFFECT_DISCARD; + params.hDeviceWindow = window; + params.Windowed = TRUE; + params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + const HRESULT created = d3d->CreateDevice(0, D3DDEVTYPE_HAL, window, + D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, device.out()); + if (FAILED(created)) { + std::fprintf(stderr, "FAIL CreateDevice hr=%08lx\n", static_cast(created)); + return false; + } + const HRESULT queried = device->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice6), + reinterpret_cast(api.out())); + if (FAILED(queried)) + std::fprintf(stderr, "FAIL compat QueryInterface hr=%08lx\n", static_cast(queried)); + return SUCCEEDED(queried); + } +}; + +// REQUIRE evaluates HRESULT-bearing expressions exactly once and still cleans +// scoped COM references/journals when a prerequisite unexpectedly fails. +#define REQUIRE(...) do { const bool passed = bool(__VA_ARGS__); \ + ::api_test::check(passed, #__VA_ARGS__, __FILE__, __LINE__); if (!passed) return; } while (false) + +enum class Capture { Full, SelectiveOn, SelectiveOff }; + +class Journal { +public: + Journal(ID3D9GtaSaCompatDevice6* api, Capture mode) : api_(api) { + begin = mode == Capture::Full ? api_->BeginStateJournal() : api_->BeginSelectiveStateJournal(); + active_ = SUCCEEDED(begin); + if (active_ && mode == Capture::SelectiveOn) + begin = api_->SetStateJournalCaptureEnabled(TRUE); + } + ~Journal() { if (active_) (void)api_->RestoreStateJournal(); } + HRESULT restore() { active_ = false; return api_->RestoreStateJournal(); } + HRESULT begin{}; +private: + ID3D9GtaSaCompatDevice6* api_; + bool active_{}; +}; + +void stateblock_bools(Device& f, Capture mode, bool allStates) { + std::printf("case=stateblock-bools capture=%u all=%u\n", unsigned(mode), unsigned(allStates)); + const std::array saved{}; + std::array baseline; + baseline.fill(TRUE); + ComPtr block; + if (allStates) { + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantB(0, saved.data(), 16))); + REQUIRE(SUCCEEDED(f.device->SetPixelShaderConstantB(0, saved.data(), 16))); + REQUIRE(SUCCEEDED(f.device->CreateStateBlock(D3DSBT_ALL, block.out()))); + } else { + REQUIRE(SUCCEEDED(f.device->BeginStateBlock())); + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantB(4, saved.data(), 3))); + REQUIRE(SUCCEEDED(f.device->SetPixelShaderConstantB(4, saved.data(), 3))); + REQUIRE(SUCCEEDED(f.device->EndStateBlock(block.out()))); + } + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantB(0, baseline.data(), 16))); + REQUIRE(SUCCEEDED(f.device->SetPixelShaderConstantB(0, baseline.data(), 16))); + Journal journal(f.api.get(), mode); + REQUIRE(SUCCEEDED(journal.begin)); + REQUIRE(SUCCEEDED(block->Apply())); + std::array during{}; + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantB(0, during.data(), 16))); + CHECK(during[4] == FALSE); + + auto expected = baseline; + if (mode == Capture::SelectiveOff) { + if (allStates) expected = saved; + else for (unsigned i = 4; i < 7; ++i) expected[i] = FALSE; + } + if (mode == Capture::SelectiveOn && !allStates) { + REQUIRE(SUCCEEDED(f.api->SetStateJournalCaptureEnabled(FALSE))); + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantB(0, saved.data(), 1))); + REQUIRE(SUCCEEDED(f.device->SetPixelShaderConstantB(0, saved.data(), 1))); + expected[0] = FALSE; // masked capture must not restore unrelated bits + } + REQUIRE(SUCCEEDED(journal.restore())); + for (bool pixel : {false, true}) { + std::array actual{}; + REQUIRE(SUCCEEDED(pixel ? f.device->GetPixelShaderConstantB(0, actual.data(), 16) + : f.device->GetVertexShaderConstantB(0, actual.data(), 16))); + CHECK(actual == expected); + } +} + +void up_bindings(Device& f, Capture mode, bool indexed) { + std::printf("case=up-bindings capture=%u indexed=%u\n", unsigned(mode), unsigned(indexed)); + ComPtr vb; + ComPtr ib; + REQUIRE(SUCCEEDED(f.device->CreateVertexBuffer(64, 0, D3DFVF_XYZ, D3DPOOL_MANAGED, vb.out(), nullptr))); + REQUIRE(SUCCEEDED(f.device->CreateIndexBuffer(6, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, ib.out(), nullptr))); + REQUIRE(SUCCEEDED(f.device->SetVertexDeclaration(nullptr))); + REQUIRE(SUCCEEDED(f.device->SetFVF(D3DFVF_XYZ))); + REQUIRE(SUCCEEDED(f.device->SetStreamSource(0, vb.get(), 12, 12))); + REQUIRE(SUCCEEDED(f.device->SetIndices(ib.get()))); + const float vertices[9] = {0, 0, 0, 1, 0, 0, 0, 1, 0}; + const WORD indices[3] = {0, 1, 2}; + Journal journal(f.api.get(), mode); + REQUIRE(SUCCEEDED(journal.begin)); + REQUIRE(SUCCEEDED(f.device->BeginScene())); + const HRESULT draw = indexed + ? f.device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 3, 1, indices, D3DFMT_INDEX16, vertices, 12) + : f.device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, 12); + CHECK(SUCCEEDED(draw)); + CHECK(SUCCEEDED(f.device->EndScene())); + ComPtr stream; + ComPtr index; + UINT offset = 0, stride = 0; + REQUIRE(SUCCEEDED(f.device->GetStreamSource(0, stream.out(), &offset, &stride))); + CHECK(stream.get() == nullptr); + REQUIRE(SUCCEEDED(journal.restore())); + REQUIRE(SUCCEEDED(f.device->GetStreamSource(0, stream.out(), &offset, &stride))); + REQUIRE(SUCCEEDED(f.device->GetIndices(index.out()))); + const bool capture = mode != Capture::SelectiveOff; + CHECK(stream.get() == (capture ? vb.get() : nullptr)); + CHECK(offset == (capture ? 12u : 0u)); + CHECK(stride == (capture ? 12u : 0u)); + CHECK(index.get() == (capture || !indexed ? ib.get() : nullptr)); + CHECK(SUCCEEDED(f.device->SetStreamSource(0, nullptr, 0, 0))); + CHECK(SUCCEEDED(f.device->SetIndices(nullptr))); +} + +void render_target_side_effects(Device& f, Capture mode, bool sameTarget) { + std::printf("case=render-target capture=%u same=%u\n", unsigned(mode), unsigned(sameTarget)); + ComPtr original, target, actualTarget; + REQUIRE(SUCCEEDED(f.device->GetRenderTarget(0, original.out()))); + REQUIRE(SUCCEEDED(f.device->CreateRenderTarget(16, 16, D3DFMT_A8R8G8B8, + D3DMULTISAMPLE_NONE, 0, FALSE, target.out(), nullptr))); + const D3DVIEWPORT9 viewport{1, 2, 10, 11, 0.2f, 0.8f}; + const RECT scissor{2, 3, 8, 9}; + REQUIRE(SUCCEEDED(f.device->SetViewport(&viewport))); + REQUIRE(SUCCEEDED(f.device->SetScissorRect(&scissor))); + Journal journal(f.api.get(), mode); + REQUIRE(SUCCEEDED(journal.begin)); + IDirect3DSurface9* selected = sameTarget ? original.get() : target.get(); + REQUIRE(SUCCEEDED(f.device->SetRenderTarget(0, selected))); + REQUIRE(SUCCEEDED(journal.restore())); + D3DVIEWPORT9 actualViewport{}; + RECT actualScissor{}; + REQUIRE(SUCCEEDED(f.device->GetViewport(&actualViewport))); + REQUIRE(SUCCEEDED(f.device->GetScissorRect(&actualScissor))); + REQUIRE(SUCCEEDED(f.device->GetRenderTarget(0, actualTarget.out()))); + CHECK(actualTarget.get() == selected); // RT objects remain outside the journal + if (mode != Capture::SelectiveOff) { + CHECK(std::memcmp(&actualViewport, &viewport, sizeof(viewport)) == 0); + CHECK(EqualRect(&actualScissor, &scissor)); + } else { + CHECK(actualViewport.X == 0 && actualViewport.Y == 0); + CHECK(actualViewport.Width == (sameTarget ? 64u : 16u)); + CHECK(actualViewport.Height == (sameTarget ? 64u : 16u)); + CHECK(actualScissor.left == 0 && actualScissor.top == 0); + } + CHECK(SUCCEEDED(f.device->SetRenderTarget(0, original.get()))); +} + +void undefined_light_reapply(Device& f, Capture mode, DWORD lightIndex) { + std::printf("case=undefined-light capture=%u\n", unsigned(mode)); + D3DLIGHT9 light{}; + light.Type = D3DLIGHT_POINT; + light.Diffuse.r = light.Diffuse.g = light.Diffuse.b = light.Diffuse.a = 1; + light.Range = 10; + light.Attenuation0 = 1; + D3DLIGHT9 actual{}; + REQUIRE(FAILED(f.device->GetLight(lightIndex, &actual))); + Journal journal(f.api.get(), mode); + REQUIRE(SUCCEEDED(journal.begin)); + REQUIRE(SUCCEEDED(f.device->SetLight(lightIndex, &light))); + ComPtr block; + REQUIRE(SUCCEEDED(f.device->CreateStateBlock(D3DSBT_ALL, block.out()))); + REQUIRE(SUCCEEDED(block->Apply())); + REQUIRE(SUCCEEDED(journal.restore())); + CHECK(FAILED(f.device->GetLight(lightIndex, &actual))); + REQUIRE(SUCCEEDED(block->Apply())); + CHECK(SUCCEEDED(f.device->GetLight(lightIndex, &actual))); + CHECK(std::memcmp(&actual, &light, sizeof(light)) == 0); +} + +void sdk_build_and_restore(Device& f) { + std::puts("case=sdk-api3-submit-and-api7-raii"); + const std::array old{1, 2, 3, 4}; + const std::array newest{5, 6, 7, 8, 9, 10, 11, 12}; + api3::StateBatchBuilder builder; + REQUIRE(builder.add_vertex_float(1, old).has_value()); + REQUIRE(builder.add_vertex_float(0, newest).has_value()); + REQUIRE(builder.add_pixel_float(1, old).has_value()); + REQUIRE(builder.add_pixel_float(0, newest).has_value()); + const std::array baseline{}; + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantF(0, baseline.data(), 2))); + REQUIRE(SUCCEEDED(f.device->SetPixelShaderConstantF(0, baseline.data(), 2))); + { + api7::SelectiveJournal original(f.api.get()); + REQUIRE(original.begin().has_value()); + REQUIRE(original.capture(true).has_value()); + api7::SelectiveJournal moved(std::move(original)); + CHECK(!original.capture(true)); + REQUIRE(SUCCEEDED(builder.submit(f.api.get()))); + std::array actual{}; + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantF(0, actual.data(), 2))); + CHECK(actual == newest); + REQUIRE(SUCCEEDED(f.device->GetPixelShaderConstantF(0, actual.data(), 2))); + CHECK(actual == newest); + // No explicit restore: scope destruction must restore the real device. + } + std::array actual{}; + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantF(0, actual.data(), 2))); + CHECK(actual == baseline); + REQUIRE(SUCCEEDED(f.device->GetPixelShaderConstantF(0, actual.data(), 2))); + CHECK(actual == baseline); + REQUIRE(SUCCEEDED(f.api->BeginStateJournal())); + CHECK(SUCCEEDED(f.api->RestoreStateJournal())); +} + +void batch_failure_semantics(Device& f) { + std::puts("case=batch-validation-and-late-draw-failure"); + const float old[4] = {1, 2, 3, 4}, changed[4] = {5, 6, 7, 8}; + REQUIRE(SUCCEEDED(f.device->SetVertexShaderConstantF(0, old, 1))); + D3D9GtaSaFloatConstantRange ranges[] = {{0, 1, changed}, {256, 1, changed}}; + D3D9GtaSaStateBatch batch{}; + batch.StructSize = sizeof(batch); batch.ApiVersion = 3; + batch.VertexFloatRangeCount = 2; batch.VertexFloatRanges = ranges; + CHECK(f.api->SubmitStateBatch(&batch) == D3DERR_INVALIDCALL); + float actual[4]{}; + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantF(0, actual, 1))); + CHECK(std::memcmp(actual, old, sizeof(old)) == 0); + + batch.VertexFloatRangeCount = 1; + REQUIRE(SUCCEEDED(f.device->SetVertexDeclaration(nullptr))); + Journal journal(f.api.get(), Capture::Full); + REQUIRE(SUCCEEDED(journal.begin)); + D3D9GtaSaStateDrawBatch draw{}; + draw.StructSize = sizeof(draw); draw.ApiVersion = 6; draw.StateBatch = &batch; + draw.Draw = {sizeof(D3D9GtaSaDrawDesc), D3D9_GTA_SA_DRAW_PRIMITIVE, + D3DPT_TRIANGLELIST, 0, 0, 0, 0, 0, 1}; + // Descriptor validation succeeds, then a missing declaration fails the draw. + CHECK(f.api->SubmitStateDrawBatch(&draw) == D3DERR_INVALIDCALL); + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantF(0, actual, 1))); + CHECK(std::memcmp(actual, changed, sizeof(changed)) == 0); + REQUIRE(SUCCEEDED(journal.restore())); + REQUIRE(SUCCEEDED(f.device->GetVertexShaderConstantF(0, actual, 1))); + CHECK(std::memcmp(actual, old, sizeof(old)) == 0); +} + +template HRESULT constants(Device& f, bool pixel, bool get, UINT start, T* data, UINT count) { + if constexpr (std::is_same_v) { + if (get) return pixel ? f.device->GetPixelShaderConstantF(start, data, count) + : f.device->GetVertexShaderConstantF(start, data, count); + return pixel ? f.device->SetPixelShaderConstantF(start, data, count) + : f.device->SetVertexShaderConstantF(start, data, count); + } else { + if (get) return pixel ? f.device->GetPixelShaderConstantI(start, data, count) + : f.device->GetVertexShaderConstantI(start, data, count); + return pixel ? f.device->SetPixelShaderConstantI(start, data, count) + : f.device->SetVertexShaderConstantI(start, data, count); + } +} + +template void constant_roundtrip(Device& f, bool pixel) { + const UINT limit = std::is_same_v ? (pixel ? 224u : 256u) : 16u; + // Include signed zero, distinct NaN payloads, infinities and subnormal bits. + const uint32_t bits[] = {0, 0x80000000, 0x7fc00001, 0x7fc00002, 0x7f800000, 0xff800000, 1, 0xffffffff}; + for (UINT count : {1u, 2u, 3u, 4u, 5u, 16u, 64u, 224u, 256u}) { + if (count > limit) continue; + const UINT start = limit - count; + std::vector storage(count * 4 + 1), read(count * 4); + T* data = storage.data() + 1; // deliberately not 16-byte aligned + for (UINT k = 0; k < count * 4; ++k) std::memcpy(data + k, &bits[k % 8], sizeof(T)); + for (UINT changed : {0u, count / 2, count - 1}) { + REQUIRE(SUCCEEDED(constants(f, pixel, false, start, data, count))); + REQUIRE(SUCCEEDED(constants(f, pixel, false, start, data, count))); // unchanged + uint32_t value; std::memcpy(&value, data + changed * 4, 4); value ^= 0x80000000; + std::memcpy(data + changed * 4, &value, 4); + REQUIRE(SUCCEEDED(constants(f, pixel, false, start, data, count))); + REQUIRE(SUCCEEDED(constants(f, pixel, true, start, read.data(), count))); + CHECK(std::memcmp(read.data(), data, count * sizeof(T) * 4) == 0); + } + } + // Recording an equal range must still record it; applying it later restores + // bytes through the state-block path, independent of the device fast path. + std::array old{}, changed{}; changed[15] = T(3); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + ComPtr block; + REQUIRE(SUCCEEDED(f.device->BeginStateBlock())); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + REQUIRE(SUCCEEDED(f.device->EndStateBlock(block.out()))); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + REQUIRE(SUCCEEDED(block->Apply())); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, changed.data(), 4))); + CHECK(std::memcmp(old.data(), changed.data(), sizeof(old)) == 0); + + for (Capture mode : {Capture::Full, Capture::SelectiveOn, Capture::SelectiveOff}) { + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + Journal journal(f.api.get(), mode); REQUIRE(SUCCEEDED(journal.begin)); + changed = old; changed[15] = T(9); // long equal prefix before first write + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + changed[3] = T(7); // overlapping write must preserve the first old value + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + REQUIRE(SUCCEEDED(journal.restore())); + std::array actual{}; + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), 4))); + const auto& expected = mode == Capture::SelectiveOff ? changed : old; + CHECK(std::memcmp(actual.data(), expected.data(), sizeof(actual)) == 0); + } +} + +void unchanged_selective_capture(Device& f, bool dedup) { + // Dedup-on ignores an equal write before capture is disabled. Dedup-off + // preserves its eager snapshot. Neither mode may change across this refactor. + for (bool pixel : {false, true}) { + std::array old{}, changed{}; changed[15] = 3; + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + Journal journal(f.api.get(), Capture::SelectiveOn); + REQUIRE(SUCCEEDED(journal.begin)); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + REQUIRE(SUCCEEDED(f.api->SetStateJournalCaptureEnabled(FALSE))); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + REQUIRE(SUCCEEDED(journal.restore())); + std::array actual{}; + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), 4))); + const auto& expected = dedup ? changed : old; + CHECK(std::memcmp(actual.data(), expected.data(), sizeof(actual)) == 0); + + // A real tail change must snapshot the original full submitted range. + // A later capture-off prefix write must also roll back from that snapshot. + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, old.data(), 4))); + Journal fullRange(f.api.get(), Capture::SelectiveOn); + REQUIRE(SUCCEEDED(fullRange.begin)); + changed = old; changed[15] = 5; + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + REQUIRE(SUCCEEDED(f.api->SetStateJournalCaptureEnabled(FALSE))); + changed[0] = 7; + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, changed.data(), 4))); + REQUIRE(SUCCEEDED(fullRange.restore())); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), 4))); + CHECK(std::memcmp(actual.data(), old.data(), sizeof(actual)) == 0); + } +} + +template void constant_scan_boundaries(Device& f, bool pixel) { + const UINT limit = std::is_same_v ? (pixel ? 224u : 256u) : 16u; + std::printf("case=constant-scan-boundaries-%s-%s\n", pixel ? "ps" : "vs", + std::is_same_v ? "float" : "int"); + // Exercise every possible first mismatch, both copy-loop parities, and + // unchanged writes. Read the entire bank to detect writes past either end. + for (UINT count : {1u, 2u, 3u, 4u, 5u, 7u, 14u, 16u}) { + if (count + 2 > limit) continue; + for (UINT first = 0; first < count; ++first) { + std::vector original(limit * 4, T(7)), expected = original, actual(limit * 4); + std::vector source(count * 4, T(7)); + for (UINT reg = first; reg < count; ++reg) for (UINT lane = 0; lane < 4; ++lane) + source[reg * 4 + lane] = T(20 + reg * 4 + lane); + std::memcpy(expected.data() + 4, source.data(), count * 4 * sizeof(T)); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, original.data(), limit))); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 1, source.data(), count))); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), limit))); + CHECK(std::memcmp(actual.data(), expected.data(), limit * 4 * sizeof(T)) == 0); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 1, source.data(), count))); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), limit))); + CHECK(std::memcmp(actual.data(), expected.data(), limit * 4 * sizeof(T)) == 0); + } + } +} + +// Compare every 32-bit component independently. In particular, signed zero +// and NaN payload changes must not be lost to a floating-point comparison. +template void constant_scan_lanes(Device& f, bool pixel) { + const UINT limit = std::is_same_v ? (pixel ? 224u : 256u) : 16u; + constexpr UINT count = 16; + std::printf("case=constant-scan-lanes-%s-%s\n", pixel ? "ps" : "vs", + std::is_same_v ? "float" : "int"); + const uint32_t bits[] = {0, 0x80000000, 0x7fc00001, 0x7f800000}; + std::vector original(limit * 4), actual(limit * 4); + for (UINT i = 0; i < limit * 4; ++i) + std::memcpy(original.data() + i, bits + i % 4, sizeof(T)); + const UINT start = limit - count; + for (UINT misalignment : {1u, 2u, 3u}) for (UINT first = 0; first < count; ++first) + for (UINT lane = 0; lane < 4; ++lane) { + alignas(16) std::array storage{}; + T* source = storage.data() + misalignment; + std::memcpy(source, original.data() + start * 4, count * 4 * sizeof(T)); + uint32_t changed; + std::memcpy(&changed, source + first * 4 + lane, sizeof(changed)); + changed ^= 1u; + std::memcpy(source + first * 4 + lane, &changed, sizeof(changed)); + auto expected = original; + std::memcpy(expected.data() + start * 4, source, count * 4 * sizeof(T)); + REQUIRE(SUCCEEDED(constants(f, pixel, false, 0, original.data(), limit))); + REQUIRE(SUCCEEDED(constants(f, pixel, false, start, source, count))); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), limit))); + CHECK(std::memcmp(actual.data(), expected.data(), limit * 4 * sizeof(T)) == 0); + REQUIRE(SUCCEEDED(constants(f, pixel, false, start, source, count))); + REQUIRE(SUCCEEDED(constants(f, pixel, true, 0, actual.data(), limit))); + CHECK(std::memcmp(actual.data(), expected.data(), limit * 4 * sizeof(T)) == 0); + } +} + +void reset_and_reuse(Device& f) { + std::puts("case=reset-discard-and-wrapper-reuse"); + REQUIRE(SUCCEEDED(f.api->BeginStateJournal())); + REQUIRE(SUCCEEDED(f.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))); + D3DPRESENT_PARAMETERS invalid = f.params; + invalid.SwapEffect = static_cast(0); + CHECK(f.device->Reset(&invalid) == D3DERR_INVALIDCALL); + CHECK(f.api->RestoreStateJournal() == D3DERR_INVALIDCALL); + REQUIRE(SUCCEEDED(f.device->Reset(&f.params))); + { + api7::SelectiveJournal journal(f.api.get()); + REQUIRE(journal.begin().has_value()); + REQUIRE(journal.capture(true).has_value()); + REQUIRE(SUCCEEDED(f.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))); + REQUIRE(SUCCEEDED(f.device->Reset(&f.params))); + CHECK(!journal.restore()); + CHECK(!journal.capture(true)); + REQUIRE(journal.begin().has_value()); + REQUIRE(journal.capture(true).has_value()); + REQUIRE(SUCCEEDED(f.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))); + } + DWORD cull = 0; + REQUIRE(SUCCEEDED(f.device->GetRenderState(D3DRS_CULLMODE, &cull))); + CHECK(cull == D3DCULL_CCW); + REQUIRE(SUCCEEDED(f.api->BeginSelectiveStateJournal())); + CHECK(f.api->BeginSelectiveStateJournal() == D3DERR_INVALIDCALL); + CHECK(f.device->BeginStateBlock() == D3DERR_INVALIDCALL); + CHECK(SUCCEEDED(f.api->RestoreStateJournal())); +} +} + +int main(int argc, char** argv) { + if (argc != 3 || (std::strcmp(argv[2], "reference") && std::strcmp(argv[2], "optimized"))) { + std::fprintf(stderr, "usage: api-state-runtime-tests reference|optimized\n"); + return 2; + } + std::printf("runtime-profile=%s backend=%s\n", argv[2], argv[1]); + Device fixture; + if (!fixture.open(argv[1], !std::strcmp(argv[2], "optimized"))) { + std::fprintf(stderr, "FAIL real D3D9/Vulkan device initialization, error=%lu\n", GetLastError()); + return 2; + } + DWORD lightIndex = 1200; + for (Capture mode : {Capture::Full, Capture::SelectiveOn, Capture::SelectiveOff}) { + for (bool variant : {false, true}) { + stateblock_bools(fixture, mode, variant); + up_bindings(fixture, mode, variant); + render_target_side_effects(fixture, mode, variant); + } + if (mode != Capture::SelectiveOff) undefined_light_reapply(fixture, mode, lightIndex++); + } + sdk_build_and_restore(fixture); + batch_failure_semantics(fixture); + for (bool pixel : {false, true}) { + constant_roundtrip(fixture, pixel); + constant_roundtrip(fixture, pixel); + constant_scan_boundaries(fixture, pixel); + constant_scan_boundaries(fixture, pixel); + constant_scan_lanes(fixture, pixel); + constant_scan_lanes(fixture, pixel); + } + unchanged_selective_capture(fixture, !std::strcmp(argv[2], "optimized")); + reset_and_reuse(fixture); + return api_test::finish(); +} diff --git a/src/bridge/legacy/api-projects/tests/test_check.hpp b/src/bridge/legacy/api-projects/tests/test_check.hpp new file mode 100644 index 0000000..65854c8 --- /dev/null +++ b/src/bridge/legacy/api-projects/tests/test_check.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +namespace api_test { +inline std::atomic checks = 0; +inline std::atomic failures = 0; + +inline void check(bool value, const char* expression, const char* file, int line) { + ++checks; + if (!value) { + ++failures; + std::fprintf(stderr, "FAIL %s:%d: %s\n", file, line, expression); + } +} + +inline int finish() { + std::printf("checks=%u failures=%u\n", checks.load(), failures.load()); + return failures.load() ? 1 : 0; +} +} + +// Unlike assert(), these checks and their side effects execute in Release. +#define CHECK(...) ::api_test::check(bool(__VA_ARGS__), #__VA_ARGS__, __FILE__, __LINE__) diff --git a/src/bridge/legacy/tests/ProperShadersIatHookTests.cpp b/src/bridge/legacy/tests/ProperShadersIatHookTests.cpp new file mode 100644 index 0000000..af85f28 --- /dev/null +++ b/src/bridge/legacy/tests/ProperShadersIatHookTests.cpp @@ -0,0 +1,312 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include + +#include +#include +#include +#include +#include + +namespace { +using ProperShadersPatching::IatHook; +using Target = int (WINAPI*)(int); +std::atomic activeHook{nullptr}; +int WINAPI Original(int value) { return value + 17; } +int WINAPI OtherOriginal(int value) { return value + 29; } +int WINAPI ForeignHook(int value) { return value + 41; } +HANDLE originalEntered = nullptr; +HANDLE originalResume = nullptr; +int WINAPI BlockingOriginal(int value) { + SetEvent(originalEntered); + if (WaitForSingleObject(originalResume, 10000) != WAIT_OBJECT_0) std::abort(); + return value + 17; +} +int WINAPI Hook(int value) { + const auto original = reinterpret_cast(activeHook.load()->Original()); + return original ? original(value) : -1000; +} +PVOID Address(Target value) { return reinterpret_cast(value); } + +bool Check(bool condition, const char* message) { + if (!condition) std::fprintf(stderr, "FAIL: %s\n", message); + return condition; +} + +struct Slot { + PVOID volatile* value = static_cast(VirtualAlloc( + nullptr, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + explicit Slot(Target target = &Original) { + if (!value) std::abort(); + *value = Address(target); + DWORD previous = 0; + if (!VirtualProtect(const_cast(value), 4096, PAGE_READONLY, &previous)) std::abort(); + } + ~Slot() { VirtualFree(const_cast(value), 0, MEM_RELEASE); } + int Call(int valueToPass) const { return reinterpret_cast(*value)(valueToPass); } + DWORD Protection() const { + MEMORY_BASIC_INFORMATION memory{}; + VirtualQuery(const_cast(value), &memory, sizeof(memory)); + return memory.Protect; + } + void Replace(Target target) { + DWORD previous = 0; + VirtualProtect(const_cast(value), 4096, PAGE_READWRITE, &previous); + InterlockedExchangePointer(value, Address(target)); + DWORD ignored = 0; + VirtualProtect(const_cast(value), 4096, previous, &ignored); + } +}; + +struct ReadOnlyMappedSlot { + HANDLE file = INVALID_HANDLE_VALUE; + HANDLE mapping = nullptr; + PVOID volatile* value = nullptr; + ReadOnlyMappedSlot() { + wchar_t directory[MAX_PATH]{}; + wchar_t path[MAX_PATH]{}; + if (!GetTempPathW(MAX_PATH, directory) || !GetTempFileNameW(directory, L"iat", 0, path)) std::abort(); + file = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, nullptr, + OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr); + if (file == INVALID_HANDLE_VALUE) std::abort(); + PVOID target = Address(&Original); + DWORD written = 0; + LARGE_INTEGER fileSize{}; + fileSize.QuadPart = 4096; + if (!WriteFile(file, &target, sizeof(target), &written, nullptr) || written != sizeof(target) || + !SetFilePointerEx(file, fileSize, nullptr, FILE_BEGIN) || !SetEndOfFile(file)) std::abort(); + mapping = CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr); + if (!mapping) std::abort(); + value = static_cast(MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0)); + if (!value) std::abort(); + } + ~ReadOnlyMappedSlot() { + UnmapViewOfFile(const_cast(value)); + CloseHandle(mapping); + CloseHandle(file); + } +}; + +// Protection rollback failure cannot be induced deterministically on a valid, +// committed private page without racing its lifetime. Only that Win32 operation +// is substituted; the slot, slot CAS, protection changes and target calls are real. +struct ProtectControl { + static inline ProtectControl* current = nullptr; + std::atomic calls{0}; + unsigned failAt = 0; + unsigned pauseAt = 0; + Target changeAtFirstProtect = nullptr; + HANDLE paused = CreateEventW(nullptr, TRUE, FALSE, nullptr); + HANDLE resume = CreateEventW(nullptr, TRUE, FALSE, nullptr); + ProtectControl() { current = this; } + ~ProtectControl() { CloseHandle(paused); CloseHandle(resume); current = nullptr; } + static BOOL WINAPI Protect(LPVOID address, SIZE_T size, DWORD protection, PDWORD oldProtection) { + auto& self = *current; + const unsigned call = ++self.calls; + if (call == self.pauseAt) { + SetEvent(self.paused); + if (WaitForSingleObject(self.resume, 10000) != WAIT_OBJECT_0) std::abort(); + } + if (call == self.failAt) { SetLastError(ERROR_ACCESS_DENIED); return FALSE; } + const BOOL result = VirtualProtect(address, size, protection, oldProtection); + if (result && call == 1 && self.changeAtFirstProtect) { + InterlockedExchangePointer(static_cast(address), Address(self.changeAtFirstProtect)); + } + return result; + } +}; + +bool PublicationWindow() { + Slot slot; + ProtectControl control; + control.pauseAt = 2; + IatHook hook(&ProtectControl::Protect); + activeHook.store(&hook); + bool installed = false; + std::thread installer([&] { installed = hook.Install(slot.value, Address(&Original), Address(&Hook)); }); + const bool paused = WaitForSingleObject(control.paused, 10000) == WAIT_OBJECT_0; + bool ok = Check(paused, "installer reaches deterministic post-CAS barrier"); + if (paused) { + ok &= Check(*slot.value == Address(&Hook), "hook is visible at publication barrier"); + // This fails if original publication moves behind the IAT CAS. + ok &= Check(slot.Call(5) == 22, "concurrent IAT call immediately after CAS reaches original"); + } + SetEvent(control.resume); + installer.join(); + ok &= Check(installed, "installation completes"); + ok &= Check(slot.Protection() == PAGE_READONLY, "successful install preserves IAT page protection"); + ok &= Check(hook.Restore(), "restore succeeds"); + ok &= Check(slot.Call(7) == 24, "restored slot invokes original"); + // A caller may cache the entry before restore and enter after it completes. + ok &= Check(Hook(11) == 28, "cached hook entry remains callable after restore"); + return ok; +} + +bool SignatureConflict() { + Slot slot(&ForeignHook); + IatHook hook; + activeHook.store(&hook); + bool ok = Check(!hook.Install(slot.value, Address(&Original), Address(&Hook)), "unexpected import signature is rejected"); + ok &= Check(slot.Call(1) == 42, "signature rejection preserves foreign owner"); + ok &= Check(!hook.OwnsSlot(), "signature rejection takes no ownership"); + hook.Restore(); + return ok; +} + +bool InstallOwnershipRace() { + Slot slot; + ProtectControl control; + control.changeAtFirstProtect = &ForeignHook; + IatHook hook(&ProtectControl::Protect); + activeHook.store(&hook); + const bool installed = hook.Install(slot.value, Address(&Original), Address(&Hook)); + bool ok = Check(!installed, "CAS rejects owner change after signature validation"); + ok &= Check(slot.Call(2) == 43, "CAS conflict preserves intervening owner"); + ok &= Check(slot.Protection() == PAGE_READONLY, "CAS conflict restores page protection"); + ok &= Check(!hook.OwnsSlot(), "CAS conflict does not claim publication"); + hook.Restore(); + return ok; +} + +bool RealProtectionFailure() { + ReadOnlyMappedSlot slot; + IatHook hook; + activeHook.store(&hook); + bool ok = Check(!hook.Install(slot.value, Address(&Original), Address(&Hook)), "real read-only section refuses writable IAT installation"); + ok &= Check(reinterpret_cast(*slot.value)(4) == 21, "real protection failure preserves callable import"); + ok &= Check(!hook.OwnsSlot() && !hook.HasPendingProtection() && !hook.Original(), "real protection failure retains no publication or protection debt"); + ok &= Check(hook.Restore(), "restore after real pre-publication failure is harmless"); + return ok; +} + +bool RestoreDuringOriginalCall() { + Slot slot(&BlockingOriginal); + IatHook hook; + activeHook.store(&hook); + originalEntered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + originalResume = CreateEventW(nullptr, TRUE, FALSE, nullptr); + bool ok = Check(hook.Install(slot.value, Address(&BlockingOriginal), Address(&Hook)), "active-call setup installs hook"); + int result = 0; + std::thread caller([&] { result = slot.Call(10); }); + ok &= Check(WaitForSingleObject(originalEntered, 10000) == WAIT_OBJECT_0, "real IAT callback is inside original target"); + ok &= Check(hook.Restore() && *slot.value == Address(&BlockingOriginal), "restore detaches while an original call is active"); + SetEvent(originalResume); + caller.join(); + ok &= Check(result == 27, "in-flight original call completes after slot restoration"); + ok &= Check(Hook(12) == 29, "stale callback remains callable after active call drains"); + CloseHandle(originalEntered); + CloseHandle(originalResume); + originalEntered = originalResume = nullptr; + return ok; +} + +bool InstallProtectionRollbackFailure() { + Slot slot; + ProtectControl control; + control.failAt = 2; + IatHook hook(&ProtectControl::Protect); + activeHook.store(&hook); + bool ok = Check(!hook.Install(slot.value, Address(&Original), Address(&Hook)), "install reports failed protection rollback"); + ok &= Check(hook.OwnsSlot() && hook.HasPendingProtection(), "failed install accurately retains published hook and protection debt"); + ok &= Check(slot.Call(4) == 21, "published hook calls original while protection rollback is pending"); + control.failAt = 0; + ok &= Check(hook.Restore(), "failed-install restoration can retry"); + ok &= Check(slot.Call(4) == 21 && slot.Protection() == PAGE_READONLY, "failed-install retry restores target and original page protection"); + ok &= Check(!hook.OwnsSlot() && !hook.HasPendingProtection(), "failed-install retry clears only repaired tracking"); + ok &= Check(Hook(6) == 23, "failed-install retry retains original for stale hook calls"); + return ok; +} + +bool RestoreProtectionFailure() { + Slot slot; + ProtectControl control; + IatHook hook(&ProtectControl::Protect); + activeHook.store(&hook); + bool ok = Check(hook.Install(slot.value, Address(&Original), Address(&Hook)), "restore-failure setup installs hook"); + control.failAt = control.calls + 1; + ok &= Check(!hook.Restore(), "restore reports write-protection failure"); + ok &= Check(hook.OwnsSlot() && slot.Call(5) == 22, "failed restore retains callable original and published-slot tracking"); + control.failAt = 0; + ok &= Check(hook.Restore(), "restore retries after write-protection failure"); + ok &= Check(slot.Call(5) == 22 && *slot.value == Address(&Original), "restore retry repairs original IAT target"); + return ok; +} + +bool RestoreProtectionRollbackFailure() { + Slot slot; + ProtectControl control; + IatHook hook(&ProtectControl::Protect); + activeHook.store(&hook); + bool ok = Check(hook.Install(slot.value, Address(&Original), Address(&Hook)), "restore rollback-failure setup installs hook"); + control.failAt = control.calls + 2; + ok &= Check(!hook.Restore(), "restore reports protection rollback failure after target CAS"); + ok &= Check(!hook.OwnsSlot() && hook.HasPendingProtection(), "restore rollback failure tracks restored target and remaining protection debt"); + ok &= Check(*slot.value == Address(&Original) && Hook(8) == 25, "restore rollback failure retains stale-entry target"); + control.failAt = 0; + ok &= Check(hook.Restore(), "restore protection debt retries"); + ok &= Check(slot.Protection() == PAGE_READONLY && !hook.HasPendingProtection(), "restore retry repairs original page protection"); + return ok; +} + +bool RestoreOwnershipConflict() { + Slot slot; + IatHook hook; + activeHook.store(&hook); + bool ok = Check(hook.Install(slot.value, Address(&Original), Address(&Hook)), "ownership-conflict setup installs hook"); + slot.Replace(&ForeignHook); + ok &= Check(!hook.Restore(), "restore reports foreign slot ownership"); + ok &= Check(slot.Call(9) == 50 && slot.Protection() == PAGE_READONLY, "restore conflict preserves foreign owner and page protection"); + ok &= Check(Hook(9) == 26, "restore ownership conflict retains stale-entry original"); + slot.Replace(&Hook); + ok &= Check(hook.Restore() && *slot.value == Address(&Original), "restoration can retry when original ownership returns"); + return ok; +} + +bool TargetLifetime() { + Slot slot; + Slot otherSlot(&OtherOriginal); + IatHook hook; + activeHook.store(&hook); + bool ok = Check(hook.Install(slot.value, Address(&Original), Address(&Hook)) && hook.Restore(), "target-lifetime setup installs and restores"); + ok &= Check(!hook.Install(otherSlot.value, Address(&OtherOriginal), Address(&Hook)), "published hook refuses to overwrite original target for a later module"); + ok &= Check(otherSlot.Call(1) == 30 && Hook(1) == 18, "rejected retarget preserves both new import and stale hook behavior"); + ok &= Check(hook.Install(slot.value, Address(&Original), Address(&Hook)), "same original target can reinstall"); + ok &= Check(slot.Call(1) == 18 && hook.Restore(), "reinstallation remains callable and restorable"); + return ok; +} +} + +#ifdef IAT_OLD_ALGORITHM +void IatBaselineAfterPublish() { + if (!ProtectControl::current) return; + auto& control = *ProtectControl::current; + if (control.pauseAt == 2) { + control.pauseAt = 0; + SetEvent(control.paused); + if (WaitForSingleObject(control.resume, 10000) != WAIT_OBJECT_0) std::abort(); + } +} +#endif + +int main() { + const struct { const char* name; bool (*run)(); } tests[] = { + {"publication_window_and_stale_entry", PublicationWindow}, + {"signature_conflict", SignatureConflict}, + {"install_ownership_race", InstallOwnershipRace}, + {"real_section_protection_failure", RealProtectionFailure}, + {"restore_during_original_call", RestoreDuringOriginalCall}, + {"install_protection_rollback_failure", InstallProtectionRollbackFailure}, + {"restore_protection_failure", RestoreProtectionFailure}, + {"restore_protection_rollback_failure", RestoreProtectionRollbackFailure}, + {"restore_ownership_conflict", RestoreOwnershipConflict}, + {"target_lifetime", TargetLifetime}, + }; + unsigned failed = 0; + for (const auto& test : tests) { + const bool passed = test.run(); + std::printf("%s: %s\n", passed ? "PASS" : "FAIL", test.name); + failed += !passed; + } + std::printf("IAT tests: %zu total, %u failed\n", std::size(tests), failed); + return failed ? 1 : 0; +} diff --git a/tests/bridge-lifecycle-regression-test.ps1 b/tests/bridge-lifecycle-regression-test.ps1 new file mode 100644 index 0000000..a6f83f7 --- /dev/null +++ b/tests/bridge-lifecycle-regression-test.ps1 @@ -0,0 +1,85 @@ +param([string]$Backend, [string]$Bridge, [string]$Probe, [switch]$Measurement) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +if (-not $Backend) { $Backend = Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll' } +if (-not $Bridge) { $Bridge = Join-Path $root 'out/build/bridge/d3d9.dll' } +if (-not $Probe) { $Probe = Join-Path $root 'out/build/api-projects-x86/Release/bridge-lifecycle-runtime-tests.exe' } +. (Join-Path $root 'tools/lib/process-runner.ps1') +$fixture = Join-Path $root ('out/test-fixtures/bridge-lifecycle-' + [guid]::NewGuid().ToString('N')) +New-Item -ItemType Directory -Path $fixture -Force | Out-Null +$inputs = @($Backend,$Bridge,$Probe) | ForEach-Object { Get-FileHash -LiteralPath $_ -Algorithm SHA256 } | Select-Object Path,Hash +$inputs | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $fixture 'inputs.json') +$failures = [Collections.Generic.List[string]]::new() +foreach ($mode in @('backend','backend-ex','load-only','bridge-raw','bridge-wrapped')) { + $directory = Join-Path $fixture $mode + New-Item -ItemType Directory -Path (Join-Path $directory 'scripts') -Force | Out-Null + $executable = Join-Path $directory 'lifecycle.exe' + Copy-Item -LiteralPath $Probe -Destination $executable + $wrapped = [int]($mode -eq 'bridge-wrapped') + $pluginDir = Join-Path $root 'out/build/bridge-tests' + $config = @" +[Backend] +UseDxvkBackend=1 +DxvkBackendDir=$(Split-Path -Parent $Backend) +[ProxyChain] +Enable=0 +ForceDeviceWrap=$wrapped +[PostFX] +EnableHost=$wrapped +PluginDir=$pluginDir +Plugins=BridgeLegacyPluginProbe.dll;BridgeVulkanPassProbe.dll +[Affinity] +Enable=1 +Mask=0 +Priority=Normal +PerThread=0 +Reapply=1 +ReapplyCount=60 +ReapplyIntervalMs=60000 +[ProperShadersRenderStateGuard] +Enable=0 +[ProperShadersEffectOptimization] +Enable=0 +[DXVKDiagnostics] +Enable=0 +"@ + if ($Measurement) { + $config += "`r`n[Measurement]`r`nEnable=1`r`nShowHud=0`r`nCpuHotspots=1`r`nJournalAttribution=1`r`n" + } + [IO.File]::WriteAllText((Join-Path $directory 'SA.RenderStack.ini'), $config) + $library = if ($mode -like 'backend*') { $Backend } else { $Bridge } + $start = [Diagnostics.ProcessStartInfo]::new() + $start.FileName=$executable; $start.WorkingDirectory=$directory + $start.UseShellExecute=$false; $start.CreateNoWindow=$true + $start.RedirectStandardOutput=$true; $start.RedirectStandardError=$true + [void]$start.ArgumentList.Add($library); [void]$start.ArgumentList.Add($mode) + $start.Environment['DXVK_LOG_PATH']=$directory + $child=[Diagnostics.Process]::Start($start) + $stdoutTask=$child.StandardOutput.ReadToEndAsync(); $stderrTask=$child.StandardError.ReadToEndAsync() + if (-not $child.WaitForExit(60000)) { $child.Kill($true); $child.WaitForExit(); $failures.Add("$mode timed out") } + $stdout=$stdoutTask.GetAwaiter().GetResult(); $stderr=$stderrTask.GetAwaiter().GetResult() + [IO.File]::WriteAllText((Join-Path $directory 'stdout.log'),$stdout) + [IO.File]::WriteAllText((Join-Path $directory 'stderr.log'),$stderr) + Write-Output "$mode exit=$($child.ExitCode) $($stdout.Trim()) $($stderr.Trim())" + if ($child.ExitCode -ne 0) { $failures.Add("$mode behavior failed") } + $child.Dispose() + if ($mode -eq 'bridge-raw') { + $log=[IO.File]::ReadAllText((Join-Path $directory 'scripts/BridgeD3D9.log')) + if ($log -notmatch 'unwrapped support worker stopped' -or $log -notmatch 'reapply worker finished' -or $log -notmatch 'explicit shutdown complete') { + $failures.Add('raw workers were not stopped before shutdown') + } + } + if ($mode -eq 'bridge-wrapped') { + $events=@(Get-Content -LiteralPath (Join-Path $directory 'bridge_legacy_plugin_probe.log')) + $expected=@('init-v1','create-device','release-device') + foreach ($i in 1..3) { $expected+=@('create-device','reset-before','reset-after-failed','reset-before','reset-after-ok','release-device') } + $expected+=@('create-device','create-device','release-device','release-device','shutdown') + if (($events -join '|') -cne ($expected -join '|')) { $failures.Add('plugin create/reset/release/shutdown event order differs') } + $passes=@(Get-Content -LiteralPath (Join-Path $directory 'bridge_vulkan_pass_probe.log')) + if (@($passes | Where-Object { $_ -match '^A frame=' }).Count -ne 7) { $failures.Add('native Vulkan pass was not suspended on failure/rebound across device/Reset lifetimes') } + } +} +Write-Output "Fixture retained: $fixture" +if ($failures.Count) { $failures | ForEach-Object { Write-Output "FAIL $_" }; exit 1 } +Write-Output 'PASS 5 lifecycle cases' diff --git a/tests/config-ownership-probe.cpp b/tests/config-ownership-probe.cpp new file mode 100644 index 0000000..8f54e65 --- /dev/null +++ b/tests/config-ownership-probe.cpp @@ -0,0 +1,100 @@ +#include +#include + +#include +#include +#include + +struct ProcessPolicy { + DWORD priority = 0; + DWORD_PTR processMask = 0; + DWORD_PTR systemMask = 0; +}; + +static bool ReadPolicy(ProcessPolicy& policy) { + policy.priority = GetPriorityClass(GetCurrentProcess()); + return policy.priority != 0 && GetProcessAffinityMask( + GetCurrentProcess(), &policy.processMask, &policy.systemMask); +} + +int main(int argc, char** argv) { + ProcessPolicy initial; + if (!ReadPolicy(initial)) { + std::fprintf(stderr, "FAIL read initial policy error=%lu\n", GetLastError()); + return 2; + } + if (argc == 2 && std::strcmp(argv[1], "--describe") == 0) { + const DWORD_PTR firstCpu = initial.systemMask & (~initial.systemMask + 1); + std::printf("first_cpu=0x%llX system_mask=0x%llX\n", + static_cast(firstCpu), + static_cast(initial.systemMask)); + return 0; + } + if (argc != 4 && argc != 5) { + std::fprintf(stderr, "usage: probe DLL Direct3DCreate9|Direct3DCreate9Ex preserve|bridge-policy [mask]\n"); + return 2; + } + const bool extended = std::strcmp(argv[2], "Direct3DCreate9Ex") == 0; + const bool bridgePolicy = std::strcmp(argv[3], "bridge-policy") == 0; + if ((!extended && std::strcmp(argv[2], "Direct3DCreate9") != 0) || + (!bridgePolicy && std::strcmp(argv[3], "preserve") != 0) || + (bridgePolicy && argc != 5)) return 2; + + // A distinct initial priority and the full available mask expose an implicit + // backend override, including the old 0xFFFF pin on machines with >16 CPUs. + if (!SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS) || + !SetProcessAffinityMask(GetCurrentProcess(), initial.systemMask)) { + std::fprintf(stderr, "FAIL seed child policy error=%lu\n", GetLastError()); + return 2; + } + ProcessPolicy before; + if (!ReadPolicy(before)) return 2; + const DWORD expectedPriority = bridgePolicy ? NORMAL_PRIORITY_CLASS : before.priority; + const DWORD_PTR expectedMask = bridgePolicy + ? static_cast(std::strtoull(argv[4], nullptr, 0)) + : before.processMask; + + HMODULE module = LoadLibraryA(argv[1]); + if (!module) { + std::fprintf(stderr, "FAIL LoadLibrary error=%lu path=%s\n", GetLastError(), argv[1]); + return 2; + } + FARPROC factory = GetProcAddress(module, argv[2]); + if (!factory) { + std::fprintf(stderr, "FAIL factory missing: %s\n", argv[2]); + return 2; + } + int failures = 0; + for (unsigned call = 1; call <= 2; call++) { + IDirect3D9* d3d9 = nullptr; + if (extended) { + IDirect3D9Ex* d3d9ex = nullptr; + const HRESULT hr = reinterpret_cast(factory)( + D3D_SDK_VERSION, &d3d9ex); + if (FAILED(hr)) { + std::fprintf(stderr, "FAIL factory HRESULT=0x%08lX\n", static_cast(hr)); + return 2; + } + d3d9 = d3d9ex; + } else { + d3d9 = reinterpret_cast(factory)(D3D_SDK_VERSION); + } + if (!d3d9) { + std::fprintf(stderr, "FAIL factory returned null\n"); + return 2; + } + ProcessPolicy after; + if (!ReadPolicy(after)) return 2; + const bool passed = after.priority == expectedPriority && after.processMask == expectedMask; + std::printf("%s %s call=%u before_priority=0x%lX after_priority=0x%lX expected_priority=0x%lX before_mask=0x%llX after_mask=0x%llX expected_mask=0x%llX\n", + passed ? "PASS" : "FAIL", argv[2], call, before.priority, after.priority, expectedPriority, + static_cast(before.processMask), + static_cast(after.processMask), + static_cast(expectedMask)); + failures += passed ? 0 : 1; + d3d9->Release(); + } + // The entire process is the isolation boundary; leaving the loaded module in + // place avoids making DLL unload semantics part of this configuration test. + return failures ? 1 : 0; +} diff --git a/tests/config-ownership-regression-test.ps1 b/tests/config-ownership-regression-test.ps1 new file mode 100644 index 0000000..3bacd62 --- /dev/null +++ b/tests/config-ownership-regression-test.ps1 @@ -0,0 +1,137 @@ +param( + [string]$Backend, + [string]$Bridge, + [string]$LlvmMingwBin, + [ValidateSet('All', 'Backend', 'Bridge')][string]$Component = 'All' +) + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +if (-not $Backend) { $Backend = Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll' } +if (-not $Bridge) { $Bridge = Join-Path $root 'out/build/bridge/d3d9.dll' } +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain = Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$fixtureParent = Join-Path $root 'out/test-fixtures' +$fixtureRoot = Assert-RenderStackPathUnder -Base $fixtureParent ` + -Path (Join-Path $fixtureParent ('config-ownership-' + [guid]::NewGuid().ToString('N'))) ` + -Description 'Configuration ownership fixture' +Assert-RenderStackNoReparsePath -Path $fixtureRoot -Anchor $root ` + -Description 'Configuration ownership fixture' | Out-Null +New-Item -ItemType Directory -Path $fixtureRoot -Force | Out-Null +Assert-RenderStackNoReparsePath -Path $fixtureRoot -Anchor $root ` + -Description 'Configuration ownership fixture' | Out-Null +$executable = Join-Path $fixtureRoot 'config-ownership-probe.exe' +& $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' ` + (Join-Path $PSScriptRoot 'config-ownership-probe.cpp') '-o' $executable +if ($LASTEXITCODE -ne 0) { throw "Probe compile failed: $LASTEXITCODE" } +$description = & $executable --describe | Out-String +if ($LASTEXITCODE -ne 0 -or $description -notmatch 'first_cpu=(0x[0-9A-F]+)') { + throw "Cannot read host affinity: $description" +} +$firstCpu = $Matches[1] +$utf8 = [Text.UTF8Encoding]::new($false) +$libraries = @() +if ($Component -in @('All', 'Backend')) { $libraries += $Backend } +if ($Component -in @('All', 'Bridge')) { $libraries += @($Bridge, $Backend) } +$inputs = @($libraries | Select-Object -Unique | ForEach-Object { + $item = Get-Item -LiteralPath $_ + [ordered]@{ path = $item.FullName; size = $item.Length; + sha256 = (Get-FileHash -LiteralPath $item.FullName -Algorithm SHA256).Hash; + lastWriteTimeUtc = $item.LastWriteTimeUtc.ToString('o') } +}) +[IO.File]::WriteAllText((Join-Path $fixtureRoot 'inputs.json'), ($inputs | ConvertTo-Json -Depth 3), $utf8) +$failures = [Collections.Generic.List[string]]::new() +$caseCount = 0 + +function Invoke-ConfigCase { + param([string]$Name, [string]$Library, [string]$Factory = 'Direct3DCreate9', + [AllowNull()][string]$RootConfig, [AllowNull()][string]$LegacyConfig, + [switch]$ExplicitBridgePolicy, [switch]$CheckDirectConstants, + [int]$RequestedDirectConstants = 0, [string]$ExpectedConfigSource) + $directory = Join-Path $fixtureRoot $Name + New-Item -ItemType Directory -Path (Join-Path $directory 'scripts') -Force | Out-Null + $probe = Join-Path $directory 'config-ownership-probe.exe' + Copy-Item -LiteralPath $executable -Destination $probe + if ($RootConfig) { [IO.File]::WriteAllText((Join-Path $directory 'SA.RenderStack.ini'), $RootConfig, $utf8) } + if ($LegacyConfig) { [IO.File]::WriteAllText((Join-Path $directory 'scripts/BridgeD3D9.ini'), $LegacyConfig, $utf8) } + $arguments = @([IO.Path]::GetFullPath($Library), $Factory, 'preserve') + if ($ExplicitBridgePolicy) { $arguments = @([IO.Path]::GetFullPath($Library), $Factory, 'bridge-policy', $firstCpu) } + $start = [Diagnostics.ProcessStartInfo]::new() + $start.FileName = $probe + foreach ($argument in $arguments) { [void]$start.ArgumentList.Add($argument) } + $start.WorkingDirectory = $directory + $start.UseShellExecute = $false + $start.CreateNoWindow = $true + $start.RedirectStandardOutput = $true + $start.RedirectStandardError = $true + $start.Environment['DXVK_LOG_PATH'] = $directory + [void]$start.Environment.Remove('DXVK_CONFIG') + [void]$start.Environment.Remove('DXVK_CONFIG_FILE') + $child = [Diagnostics.Process]::Start($start) + $stdoutTask = $child.StandardOutput.ReadToEndAsync() + $stderrTask = $child.StandardError.ReadToEndAsync() + if (-not $child.WaitForExit(30000)) { + $child.Kill($true) + $child.WaitForExit() + $script:failures.Add("$Name timed out") + } + $stdout = $stdoutTask.GetAwaiter().GetResult() + $stderr = $stderrTask.GetAwaiter().GetResult() + [IO.File]::WriteAllText((Join-Path $directory 'stdout.log'), $stdout, $utf8) + [IO.File]::WriteAllText((Join-Path $directory 'stderr.log'), $stderr, $utf8) + $script:caseCount++ + Write-Output "CASE $Name exit=$($child.ExitCode)" + Write-Output $stdout.TrimEnd() + if ($child.ExitCode -ne 0) { $script:failures.Add("$Name factory behavior failed (exit $($child.ExitCode))") } + if ($CheckDirectConstants) { + $log = Join-Path $directory 'scripts/BridgeD3D9.log' + $text = if (Test-Path -LiteralPath $log) { [IO.File]::ReadAllText($log) } else { '' } + # This is the effective runtime configuration emitted by the loaded + # Bridge, consumed by its effect hooks; never inspect source text. + if ($text -notmatch 'effectopt: enable=1 .*directConstants=0\b') { + $script:failures.Add("$Name DirectConstants was not disabled in the loaded Bridge") + } + if ($text -notmatch "directConstants: requested=$RequestedDirectConstants effective=0 reason=production-quarantine") { + $script:failures.Add("$Name missing requested/effective quarantine diagnostic") + } + if ($ExpectedConfigSource -and $text.Replace('\', '/') -notmatch [regex]::Escape($ExpectedConfigSource)) { + $script:failures.Add("$Name used the wrong configuration source") + } + } + $child.Dispose() +} + +if ($Component -in @('All', 'Backend')) { + foreach ($factory in @('Direct3DCreate9', 'Direct3DCreate9Ex')) { + Invoke-ConfigCase -Name "backend-missing-$factory" -Library $Backend -Factory $factory + foreach ($perThread in @(0, 1)) { + Invoke-ConfigCase -Name "backend-disabled-perthread$perThread-$factory" -Library $Backend -Factory $factory ` + -RootConfig "[Affinity]`nEnable=0`nPerThread=$perThread`nMmcss=0`n" + } + } +} +if ($Component -in @('All', 'Bridge')) { + $safeConfig = "[Backend]`nUseDxvkBackend=0`n[ProxyChain]`nEnable=0`n[PostFX]`nEnableHost=0`n[DXVKDiagnostics]`nEnable=0`n[Affinity]`nEnable=0`nPerThread=0`n[ProperShadersEffectOptimization]`nEnable=1`nStateJournal=1`n" + Invoke-ConfigCase -Name 'bridge-direct-missing' -Library $Bridge -RootConfig $safeConfig ` + -CheckDirectConstants -ExpectedConfigSource 'SA.RenderStack.ini' + Invoke-ConfigCase -Name 'bridge-direct-root-one' -Library $Bridge -RootConfig ($safeConfig + "DirectConstants=1`n") ` + -CheckDirectConstants -RequestedDirectConstants 1 -ExpectedConfigSource 'SA.RenderStack.ini' + Invoke-ConfigCase -Name 'bridge-direct-legacy-one' -Library $Bridge -LegacyConfig ($safeConfig + "DirectConstants=1`n") ` + -CheckDirectConstants -RequestedDirectConstants 1 -ExpectedConfigSource 'scripts/BridgeD3D9.ini' + Invoke-ConfigCase -Name 'bridge-root-overrides-legacy' -Library $Bridge -RootConfig ($safeConfig + "DirectConstants=0`n") ` + -LegacyConfig ($safeConfig + "DirectConstants=1`n") -CheckDirectConstants -ExpectedConfigSource 'SA.RenderStack.ini' + $explicit = $safeConfig.Replace("Enable=0`nPerThread=0", "Enable=1`nMask=$firstCpu`nPriority=Normal`nReapply=0`nPerThread=0") + Invoke-ConfigCase -Name 'bridge-explicit-policy' -Library $Bridge -RootConfig $explicit -ExplicitBridgePolicy ` + -CheckDirectConstants -ExpectedConfigSource 'SA.RenderStack.ini' + $dxvkPolicy = $explicit.Replace("UseDxvkBackend=0", "UseDxvkBackend=1`nDxvkBackendDir=$([IO.Path]::GetDirectoryName([IO.Path]::GetFullPath($Backend)))") + Invoke-ConfigCase -Name 'bridge-explicit-dxvk-policy' -Library $Bridge -RootConfig $dxvkPolicy -ExplicitBridgePolicy ` + -CheckDirectConstants -ExpectedConfigSource 'SA.RenderStack.ini' +} +Write-Output "Fixture retained: $fixtureRoot" +if ($failures.Count) { + $failures | ForEach-Object { Write-Output "FAIL $_" } + throw "$($failures.Count) failures across $caseCount configuration cases" +} +Write-Output "PASS $caseCount configuration cases" diff --git a/tests/constant-layout-test.py b/tests/constant-layout-test.py new file mode 100644 index 0000000..4989d95 --- /dev/null +++ b/tests/constant-layout-test.py @@ -0,0 +1,136 @@ +"""Check the measured linked x86 constant-control layout. + +PS/int comparison loops use a measured 64-byte header alignment; the VS-int +update loop also keeps the validated mod64=0 position. VS float keeps +the measured +16 offset within a 64-byte boundary, avoiding a known regression +in small non-deduplicated writes. A changed fingerprint needs new A/B evidence. +Changing that layout requires remeasuring the constant/draw control workloads. +This is not a timing threshold and does not make a game FPS claim. +""" +import argparse +import hashlib +import json +from pathlib import Path +import struct +import re +import subprocess +import sys + + +def constant_functions(data): + if data[:2] != b"MZ": + raise ValueError("not a PE image") + pe = struct.unpack_from(" 0: + rows.append({"name": name.decode("ascii"), "rva": value + addresses[section - 1]}) + index += 1 + data[offset + 17] + return rows + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("backend", type=Path) + parser.add_argument("--objdump", required=True, type=Path) + parser.add_argument("--report", type=Path) + args = parser.parse_args() + data = args.backend.read_bytes() + rows = [row for row in constant_functions(data) if re.search(r"D3D9ConstantTypeE[01]", row["name"])] + pe = struct.unpack_from(":", line) + if header: + current = by_name.get(header[1]) + instruction = re.match(r"\s*([0-9a-f]+):\s+(\w+)\s*(.*)", line) + if current is not None and instruction: + instructions[current['name']].append((int(instruction[1], 16), instruction[2], instruction[3])) + for row in rows: + code = instructions[row['name']] + loops = set() + update_loops = set() + for source, mnemonic, operands in code: + jump = re.match(r"0x([0-9a-f]+)", operands) + if not mnemonic.startswith('j') or mnemonic == 'jmp' or not jump: + continue + target = int(jump[1], 16) + body = [(op, arg) for addr, op, arg in code if target <= addr <= source] + # Identify the actual 16-byte equality loop rather than an arbitrary + # earlier back edge in SetShaderConstants. clang 22 uses pxor/ptest; + # older compilers may use pcmpeqd/pmovmskb for the same comparison. + ops = [op for op, _ in body] + loads = sum(op in ('movdqu','movups','movdqa','movaps') and 'xmmword ptr [' in arg + for op, arg in body) + compare = ('pxor' in ops and 'ptest' in ops) or ('pcmpeqd' in ops and 'pmovmskb' in ops) + exits = any(op.startswith('j') and re.match(r'0x([0-9a-f]+)', arg) + and int(re.match(r'0x([0-9a-f]+)', arg)[1], 16) > source for op, arg in body) + # A scalar pointer reload may precede the two vector loads. The + # update loop instead checks the count and branches before loading. + head = body[1:] if body and body[0][0] == 'mov' else body + header_loads = len(head) >= 2 and all(op in ('movdqu','movups','movdqa','movaps') + and 'xmmword ptr [' in arg for op, arg in head[:2]) + if target < source and len(body) <= 20 and loads == 2 and compare and exits: + stores = any(arg.startswith('xmmword ptr [') for op, arg in body) + # An update scan reaches its first vector store either by + # falling through the back edge (old while form) or through + # the mismatch exit (rotated for form). The early dedup scan + # goes to journal capture instead and never stores constants. + exits_to = [int(match[1], 16) for op, arg in body + if op.startswith('j') and (match := re.match(r'0x([0-9a-f]+)', arg)) + and int(match[1], 16) > source] + destinations = exits_to + [next((addr for addr, _, _ in code if addr > source), 0)] + writes_on_exit = any(any(arg.startswith('xmmword ptr [') + for _, op, arg in [(a,o,v) for a,o,v in code if a >= dest][:4]) for dest in destinations) + if not stores and writes_on_exit: + update_loops.add(target - image_base) + elif header_loads: + loops.add(target - image_base) + row['comparisonLoops'] = sorted(loops) + if len(loops) == 1: + row['compareLoopRva'] = next(iter(loops)) + vertex_float = 'ShaderType0E' in row['name'] and 'D3D9ConstantTypeE0E' in row['name'] + row['requiredOffsetMod64'] = 16 if vertex_float else 0 + row["layoutMatches"] = len(loops) == 1 and row["compareLoopRva"] % 64 == row['requiredOffsetMod64'] + row['updateLoops'] = sorted(update_loops) + if 'ShaderType0E' in row['name'] and 'D3D9ConstantTypeE1E' in row['name']: + row['requiredUpdateOffsetMod64'] = 0 + row['layoutMatches'] = row['layoutMatches'] and len(update_loops) == 1 and next(iter(update_loops)) % 64 == 0 + passed = len(rows) == 4 and all(row["layoutMatches"] for row in rows) + report = {"backend": str(args.backend.resolve()), "sha256": hashlib.sha256(data).hexdigest(), + "objdump": str(args.objdump.resolve()), "passed": passed, "functions": rows} + if args.report: + args.report.parent.mkdir(parents=True, exist_ok=True) + args.report.write_text(json.dumps(report, indent=2), encoding="utf-8", newline="\n") + print(json.dumps(report, indent=2)) + if not passed: + print("FAIL PS/int comparisons require mod64=0 and VS-int update requires mod64=0; " + "VS-float comparison requires offset mod64=16", file=sys.stderr) + return 0 if passed else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/constant-submit-benchmark.ps1 b/tests/constant-submit-benchmark.ps1 new file mode 100644 index 0000000..30d3d30 --- /dev/null +++ b/tests/constant-submit-benchmark.ps1 @@ -0,0 +1,98 @@ +param( + [Parameter(Mandatory)] [string]$Baseline, + [Parameter(Mandatory)] [string]$Candidate, + [Parameter(Mandatory)] [string]$Probe, + [Parameter(Mandatory)] [string]$OutputDirectory, + [ValidateRange(4, 50)] [int]$Pairs = 10, + [ValidateSet('on', 'off')] [string]$Dedup = 'on' +) +$ErrorActionPreference = 'Stop' +$Baseline = (Resolve-Path -LiteralPath $Baseline).Path +$Candidate = (Resolve-Path -LiteralPath $Candidate).Path +$Probe = (Resolve-Path -LiteralPath $Probe).Path +New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null +$OutputDirectory = (Resolve-Path -LiteralPath $OutputDirectory).Path +$samples = [Collections.Generic.List[object]]::new() +$expected = @{} +foreach ($stage in @('vs','ps')) { + foreach ($kind in @('float','int')) { + $limit = if ($kind -eq 'int') { 16 } elseif ($stage -eq 'ps') { 224 } else { 256 } + foreach ($count in @(1,4,16,64,224,256)) { + if ($count -gt $limit) { continue } + foreach ($pattern in @('same','first','tail','all')) { + $expected["setter,$stage,$kind,$count,$pattern"] = 300000 + } + } + } +} +foreach ($kind in @('draw','texture-draw','sampler-draw','bind-draw','multi-bind-draw')) { + $expected["$kind,vs+ps,float,4,tail"] = 16000 + $expected["$kind-complete,vs+ps,float,4,tail"] = 16000 +} +$started = [DateTime]::UtcNow.ToString('o') +Push-Location $OutputDirectory +try { + for ($pair = 0; $pair -lt $Pairs; $pair++) { + # Balanced AB/BA order limits a fixed ordering bias. No affinity, + # priority, power-plan or driver configuration is changed. + $order = if ($pair % 2) { @('candidate','baseline') } else { @('baseline','candidate') } + foreach ($variant in $order) { + $dll = if ($variant -eq 'baseline') { $Baseline } else { $Candidate } + $stem = '{0}-{1:D2}-{2}' -f $Dedup, $pair, $variant + & $Probe $dll $Dedup bench > "$stem.csv" 2> "$stem.err" + if ($LASTEXITCODE -ne 0) { throw "$stem failed with exit $LASTEXITCODE" } + $seen = [Collections.Generic.HashSet[string]]::new() + foreach ($line in Get-Content "$stem.csv") { + if ($line -notmatch '^(setter|[a-z-]*draw(?:-complete)?),') { continue } + $fields = $line.Split(',') + if ($fields.Count -ne 7) { throw "$stem invalid row: $line" } + $case = $fields[0..4] -join ',' + if (!$expected.ContainsKey($case) -or !$seen.Add($case)) { throw "$stem unexpected/duplicate case: $case" } + if ([int]$fields[5] -ne $expected[$case]) { throw "$stem wrong iteration count: $case" } + $elapsed = [double]::Parse($fields[6], [Globalization.CultureInfo]::InvariantCulture) + if ([double]::IsNaN($elapsed) -or [double]::IsInfinity($elapsed) -or $elapsed -le 0) { + throw "$stem invalid time: $case" + } + $samples.Add([pscustomobject]@{ + pair = $pair; variant = $variant + case = $case; iterations = [int]$fields[5]; ns = $elapsed + }) + } + if ($seen.Count -ne $expected.Count) { throw "$stem missing benchmark cases ($($seen.Count)/$($expected.Count))" } + } + Write-Output "completed pair $($pair + 1)/$Pairs dedup=$Dedup" + } +} finally { Pop-Location } + +function Median([double[]]$values) { + $v = @($values | Sort-Object) + if ($v.Count % 2) { return $v[[int][Math]::Floor($v.Count / 2)] } + return ($v[$v.Count / 2 - 1] + $v[$v.Count / 2]) / 2 +} +$summary = foreach ($group in $samples | Group-Object case) { + $a = @($group.Group | Where-Object variant -eq baseline | ForEach-Object ns) + $b = @($group.Group | Where-Object variant -eq candidate | ForEach-Object ns) + $ma = Median $a; $mb = Median $b + $pairChanges = foreach ($n in 0..($Pairs - 1)) { + $pa = $group.Group | Where-Object { $_.pair -eq $n -and $_.variant -eq 'baseline' } + $pb = $group.Group | Where-Object { $_.pair -eq $n -and $_.variant -eq 'candidate' } + if (!$pa -or !$pb) { throw "missing sample for $($group.Name) pair=$n" } + 100 * ($pb.ns / $pa.ns - 1) + } + [pscustomobject]@{ + case = $group.Name; baselineMedianNs = $ma; candidateMedianNs = $mb + changePercent = 100 * ($mb / $ma - 1) + pairedMedianChangePercent = Median $pairChanges + candidateFasterPairs = @($pairChanges | Where-Object { $_ -lt 0 }).Count + pairs = $Pairs + } +} +$report = [ordered]@{ + startedUtc = $started; endedUtc = [DateTime]::UtcNow.ToString('o') + dedup = $Dedup; pairs = $Pairs + binaries = @($Baseline, $Candidate, $Probe | Get-FileHash -Algorithm SHA256 | Select-Object Path,Hash) + samples = @($samples); summary = @($summary) + limits = 'Synthetic public API and small real draws; does not establish GTA scene FPS.' +} +$report | ConvertTo-Json -Depth 8 | Set-Content (Join-Path $OutputDirectory "results-$Dedup.json") +$summary | Export-Csv (Join-Path $OutputDirectory "summary-$Dedup.csv") -NoTypeInformation diff --git a/tests/control-path-benchmark.ps1 b/tests/control-path-benchmark.ps1 new file mode 100644 index 0000000..5646cd2 --- /dev/null +++ b/tests/control-path-benchmark.ps1 @@ -0,0 +1,105 @@ +param( + [Parameter(Mandatory)] [string]$Baseline, + [Parameter(Mandatory)] [string]$Candidate, + [Parameter(Mandatory)] [string]$Probe, + [Parameter(Mandatory)] [string]$OutputDirectory, + [ValidateRange(1,50)] [int]$Processes = 12, + [ValidateRange(1,16)] [int]$Rounds = 4, + [ValidateSet('on','off')] [string]$Profile = 'on', + [ValidateSet('controls','all','textures')] [string]$Cases = 'controls', + [string]$Cpu = 'any' +) +$ErrorActionPreference = 'Stop' +. (Join-Path $PSScriptRoot 'lib/control-path-results.ps1') +$Baseline = (Resolve-Path -LiteralPath $Baseline).Path +$Candidate = (Resolve-Path -LiteralPath $Candidate).Path +$Probe = (Resolve-Path -LiteralPath $Probe).Path +New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null +$OutputDirectory = (Resolve-Path -LiteralPath $OutputDirectory).Path +$expected = [Collections.Generic.HashSet[string]]::new() +foreach ($stage in @('vs','ps')) { + foreach ($kind in @('float','int')) { + $limit = if ($kind -eq 'int') {16} elseif ($stage -eq 'ps') {224} else {256} + foreach ($count in @(1,4,16,64,224,256)) { + if ($count -gt $limit) { continue } + foreach ($pattern in @('same','first','tail','all')) { + $key = "setter,$stage,$kind,$count,$pattern" + if ($Cases -eq 'all' -or ($Cases -ne 'textures' -and $key -in @('setter,vs,float,4,same','setter,ps,float,16,same', + 'setter,ps,int,16,same','setter,vs,int,16,same','setter,ps,float,64,all'))) { + [void]$expected.Add($key) + } + } + } + } +} +foreach ($kind in @('draw','texture-draw','sampler-draw','bind-draw','multi-bind-draw', + 'texture-hit-draw','texture-multi-hit-draw','texture-mixed-draw','texture-srgb-draw')) { + if ($Cases -eq 'controls' -and $kind -notin @('draw','texture-draw')) {continue} + if ($Cases -eq 'all' -and $kind -in @('texture-hit-draw','texture-multi-hit-draw','texture-mixed-draw','texture-srgb-draw')) {continue} + if ($Cases -eq 'textures' -and $kind -in @('draw','sampler-draw','bind-draw')) {continue} + [void]$expected.Add("$kind,vs+ps,float,4,tail") + [void]$expected.Add("$kind-complete,vs+ps,float,4,tail") +} +# Refuse a reused profile/CPU namespace before any file can be overwritten. +if (@(Get-ChildItem -LiteralPath $OutputDirectory -Filter "$Profile-$Cpu-*").Count -or + (Test-Path -LiteralPath (Join-Path $OutputDirectory "results-$Profile-$Cpu.json")) -or + (Test-Path -LiteralPath (Join-Path $OutputDirectory "summary-$Profile-$Cpu.csv"))) { + throw 'Output namespace is occupied; use a fresh output directory' +} +$binaryHashes = @($Baseline,$Candidate,$Probe | Get-FileHash -Algorithm SHA256 | Select-Object Path,Hash) +$started = [DateTime]::UtcNow.ToString('o') +$samples = [Collections.Generic.List[object]]::new() +$metadata = [Collections.Generic.List[object]]::new() +Push-Location $OutputDirectory +try { + for ($process = 0; $process -lt $Processes; ++$process) { + $stem = '{0}-{1}-{2:D2}' -f $Profile,$Cpu,$process + & $Probe pair $Baseline $Candidate $Profile $Rounds $process $Cpu $Cases > "$stem.jsonl" 2> "$stem.err" + if ($LASTEXITCODE -ne 0) {throw "$stem failed: $LASTEXITCODE"} + $parsed = Read-ControlPathSamples -Path "$stem.jsonl" -Expected $expected -Rounds $Rounds ` + -Seed $process -Profile $Profile -Cpu $Cpu -SharedDevice ($Baseline -ieq $Candidate) + $metadata.Add($parsed.metadata) + foreach ($row in $parsed.samples) { + $row | Add-Member -NotePropertyName process -NotePropertyValue $process + $samples.Add($row) + } + Write-Output "completed process $($process+1)/$Processes profile=$Profile cpu=$Cpu checks=$($parsed.checks) samples=$($parsed.samples.Count)" + } +} finally {Pop-Location} +function Median([double[]]$values) { + $v = @($values | Sort-Object) + if ($v.Count % 2) {return $v[[int][Math]::Floor($v.Count/2)]} + return ($v[$v.Count/2-1]+$v[$v.Count/2])/2 +} +$summary = foreach ($case in $expected | Sort-Object) { + $rows = @($samples | Where-Object case -eq $case) + $changes = foreach ($process in 0..($Processes-1)) { + $paired = foreach ($round in 0..($Rounds-1)) { + $a = $rows | Where-Object {$_.process -eq $process -and $_.round -eq $round -and $_.variant -eq 'baseline'} + $b = $rows | Where-Object {$_.process -eq $process -and $_.round -eq $round -and $_.variant -eq 'candidate'} + 100 * ($b.ns/$a.ns-1) + } + Median $paired + } + [pscustomobject]@{ + case=$case + baselineMedianNs=Median @($rows | Where-Object variant -eq baseline | ForEach-Object ns) + candidateMedianNs=Median @($rows | Where-Object variant -eq candidate | ForEach-Object ns) + processMedianChangePercent=Median $changes + fasterProcesses=@($changes | Where-Object {$_ -lt 0}).Count + processes=$Processes + processChanges=@($changes) + } +} +$finalHashes = @($Baseline,$Candidate,$Probe | Get-FileHash -Algorithm SHA256 | Select-Object Path,Hash) +if (@(Compare-Object $binaryHashes $finalHashes -Property Path,Hash).Count) { + throw 'Benchmark binaries changed during the run' +} +$report = [ordered]@{ + startedUtc=$started;endedUtc=[DateTime]::UtcNow.ToString('o');profile=$Profile;cpu=$Cpu;cases=$Cases + processes=$Processes;rounds=$Rounds + binaries=$binaryHashes + metadata=@($metadata);samples=@($samples);summary=@($summary) +} +$report | ConvertTo-Json -Depth 10 | Set-Content (Join-Path $OutputDirectory "results-$Profile-$Cpu.json") +$summary | Select-Object -ExcludeProperty processChanges | Export-Csv (Join-Path $OutputDirectory "summary-$Profile-$Cpu.csv") -NoTypeInformation diff --git a/tests/control-path-protocol-test.ps1 b/tests/control-path-protocol-test.ps1 new file mode 100644 index 0000000..f407226 --- /dev/null +++ b/tests/control-path-protocol-test.ps1 @@ -0,0 +1,36 @@ +param([Parameter(Mandatory)] [string]$InputPath) +$ErrorActionPreference = 'Stop' +. (Join-Path $PSScriptRoot 'lib/control-path-results.ps1') +# Mutate a successful real-device capture, so protocol tests stay tied to the +# producer's actual records and terminal behavioral-check count. +$lines = @(Get-Content -LiteralPath $InputPath) +$meta = $lines[0] | ConvertFrom-Json +$expected = [Collections.Generic.HashSet[string]]::new() +foreach ($line in $lines[1..($lines.Count-2)]) {[void]$expected.Add(($line | ConvertFrom-Json).case)} +if ($expected.Count -notin @(9,12,78)) {throw 'Not a complete benchmark capture'} +$cpu = if ($meta.pinned) { [string](($lines[1] | ConvertFrom-Json).cpuStart) } else {'any'} +$argsForParser = @{Expected=$expected;Rounds=$meta.rounds;Seed=$meta.seed;Profile=$meta.profile;Cpu=$cpu;SharedDevice=$meta.sharedDevice} +[void](Read-ControlPathSamples -Path $InputPath @argsForParser) +$temp = Join-Path ([IO.Path]::GetDirectoryName((Resolve-Path -LiteralPath $InputPath).Path)) ('protocol-' + [guid]::NewGuid().ToString('N') + '.jsonl') +$cases = [ordered]@{ + 'zero checks' = {param($a) $a[-1]='checks=0 failures=0'; $a} + 'missing completion' = {param($a) $a[0..($a.Count-2)]} + 'duplicate completion' = {param($a) $a + $a[-1]} + 'missing sample' = {param($a) $a[0] ; $a[2..($a.Count-1)]} + 'duplicate sample' = {param($a) $a[0];$a[1];$a[1..($a.Count-1)]} + 'wrong workload' = {param($a) $r=$a[1]|ConvertFrom-Json;$r.iterations=1;$a[1]=$r|ConvertTo-Json -Compress;$a} + 'nonfinite cycles' = {param($a) $r=$a[1]|ConvertFrom-Json;$r.cycles='Infinity';$a[1]=$r|ConvertTo-Json -Compress;$a} + 'wrong shared device' = {param($a) $r=$a[0]|ConvertFrom-Json;$r.sharedDevice=!$r.sharedDevice;$a[0]=$r|ConvertTo-Json -Compress;$a} + 'wrong affinity metadata' = {param($a) $r=$a[0]|ConvertFrom-Json;$r.pinned=!$r.pinned;$a[0]=$r|ConvertTo-Json -Compress;$a} + 'zero module' = {param($a) $r=$a[0]|ConvertFrom-Json;$r.baselineBase='0';$a[0]=$r|ConvertTo-Json -Compress;$a} + 'wrong module topology' = {param($a) $r=$a[0]|ConvertFrom-Json;$r.candidateBase=if($r.sharedDevice){'1'}else{$r.baselineBase};$a[0]=$r|ConvertTo-Json -Compress;$a} +} +try { + foreach ($entry in $cases.GetEnumerator()) { + & $entry.Value $lines.Clone() | Set-Content -LiteralPath $temp + $rejected=$false + try {[void](Read-ControlPathSamples -Path $temp @argsForParser)} catch {$rejected=$true} + if (!$rejected) {throw "Accepted invalid capture: $($entry.Key)"} + } +} finally {if (Test-Path -LiteralPath $temp) {Remove-Item -LiteralPath $temp}} +Write-Output "PASS real capture accepted; $($cases.Count) invalid capture mutations rejected" diff --git a/tests/journal-attribution-test.ps1 b/tests/journal-attribution-test.ps1 new file mode 100644 index 0000000..549527b --- /dev/null +++ b/tests/journal-attribution-test.ps1 @@ -0,0 +1,27 @@ +param([string]$EvidenceName = 'green', [switch]$Device, [string]$Backend, + [string]$MSBuild, [switch]$AllowNonV18MsBuild) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +$out = Join-Path $root 'out/perf/20261027-unified-measurement/journal' +New-Item -ItemType Directory -Path (Join-Path $out 'scripts') -Force | Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$msBuildInfo = Find-RenderStackMSBuild -MsBuildPath $MSBuild -RepoRoot $root -AllowNonV18MsBuild:$AllowNonV18MsBuild +$include = Join-Path $root 'out/deps/Microsoft.DXSDK.D3DX/9.29.952.8/package/build/native/include' +$buildArguments = @((Join-Path $PSScriptRoot 'journal-attribution-tests.vcxproj'), '/nologo', '/m:1', '/nr:false', '/t:Build', + '/p:Configuration=Release', '/p:Platform=Win32', '/p:PreferredToolArchitecture=x64', "/p:D3DX9IncludeDir=$include") +if ($msBuildInfo.ProductMajor -lt 18) { $buildArguments += '/p:PlatformToolset=v143' } +& $msBuildInfo.Path @buildArguments 2>&1 | + Tee-Object -FilePath (Join-Path $out "$EvidenceName-build.log") +if ($LASTEXITCODE -ne 0) { throw "journal attribution build failed: $LASTEXITCODE" } +Push-Location $out +try { + $testArguments = @() + if ($Device) { $testArguments += '--device' } + if ($Backend) { $testArguments += $Backend } + & (Join-Path $out 'bin/JournalAttributionTests.exe') @testArguments 2>&1 | + Tee-Object -FilePath (Join-Path $out "$EvidenceName-test.log") + $result = $LASTEXITCODE +} finally { Pop-Location } +if ($result -ne 0) { throw "journal attribution tests failed: $result" } diff --git a/tests/journal-attribution-tests.cpp b/tests/journal-attribution-tests.cpp new file mode 100644 index 0000000..a87b862 --- /dev/null +++ b/tests/journal-attribution-tests.cpp @@ -0,0 +1,430 @@ +#include +#include + +namespace { +thread_local bool g_pauseNextExclusive = false; +HANDLE g_admissionAtLock = nullptr; +HANDLE g_continueAdmission = nullptr; + +// Keep the actual SRW lock, and provide one deterministic scheduling point +// between the optimistic active read and the locked admission recheck. This +// interposition exists only in the test translation unit. +void AcquireJournalTestLock(PSRWLOCK lock) +{ + if (g_pauseNextExclusive) { + g_pauseNextExclusive = false; + SetEvent(g_admissionAtLock); + WaitForSingleObject(g_continueAdmission, INFINITE); + } + AcquireSRWLockExclusive(lock); +} +} + +#define AcquireSRWLockExclusive AcquireJournalTestLock +#include "../src/bridge/legacy/EffectInspector.cpp" +#undef AcquireSRWLockExclusive + +#include +#include +#include + +namespace { +unsigned g_checks = 0; +unsigned g_failures = 0; + +void Check(bool condition, const char* message) +{ + ++g_checks; + std::printf("%s: %s\n", condition ? "PASS" : "FAIL", message); + if (!condition) ++g_failures; +} + +void TestInactiveAdmission() +{ + // A disabled transaction must return while another thread owns the stats + // lock. Removing the inactive fast check makes this observable test fail. + JournalAttributionStopCapture(); + AcquireSRWLockExclusive(&g_journalAttributionLock); + std::promise result; + auto ready = result.get_future(); + std::thread worker([&] { result.set_value(JournalAttributionTryBeginTransaction()); }); + const bool returnedWithoutLock = ready.wait_for(std::chrono::milliseconds(150)) == + std::future_status::ready; + ReleaseSRWLockExclusive(&g_journalAttributionLock); + worker.join(); + Check(returnedWithoutLock && !ready.get(), + "inactive admission returns false without acquiring the stats lock"); +} + +#if defined(JOURNAL_ATTRIBUTION_SUMMARY_VERSION) +void Observe(const char* name, std::uint64_t sequence, std::uint64_t span, + std::uint64_t restore, std::span records = {}, + bool native = false, bool truncated = false) +{ + if (!JournalAttributionTryBeginTransaction()) { + Check(false, "active transaction admitted"); + return; + } + JournalProbeTransactionInfo info{sequence, JournalAttributionCurrentFrame(), span, native}; + JournalAttributionObserveTransaction(name, records, truncated, info); + JournalAttributionObserveRestore(sequence, restore); + JournalAttributionEndTransaction(); +} + +void TestSummary() +{ + JournalAttributionSummary summary{}; + JournalAttributionReset(); + Check(JournalAttributionGetSummary(&summary) && !summary.captureStarted && + !summary.available && !summary.active, + "no capture is explicitly unavailable"); + Check(!JournalAttributionGetSummary(nullptr), "null summary output is rejected"); + JournalAttributionSetCaptureIdentity(7654321); + JournalAttributionStartCapture(); + JournalAttributionSetCaptureIdentity(99); + JournalAttributionGetSummary(&summary); + Check(summary.captureId == 7654321 && summary.captureStarted && summary.active && + !summary.available, "capture identity stays stable and empty capture stays unavailable"); + const JournalProbeRecord records[] = { + {5, 22, 0, 0xFFFFFFFFu}, {5, 22, 0, 3}, {5, 22, 0, 3}, {6, 0, 0, 4} + }; + Observe("Alpha", 1, 100, 10, records, true); + Observe("Beta", 2, 40, 5); + JournalAttributionOnPresent(); + Observe("Alpha", 3, 20, 4, std::span(records, 2), true, true); + JournalAttributionOnPresent(); + JournalAttributionStopCapture(); + JournalAttributionOnPresent(); + JournalAttributionGetSummary(&summary); + Check(summary.available && !summary.active && summary.inFlight == 0 && + summary.captureId == 7654321 && summary.frames == 2 && summary.presents == 2, + "completed summary preserves capture and successful Present counts"); + Check(summary.transactions == 3 && summary.records == 6 && + summary.nativeTransactions == 2 && summary.localTransactions == 1 && + summary.emptyTransactions == 1 && summary.truncatedTransactions == 1 && + summary.techniqueCount == 2 && summary.passCount == 4, + "summary counts technique passes and admitted transaction records"); + Check(summary.transactionSpanQpc == 160 && summary.maxTransactionSpanQpc == 100 && + summary.restoreQpc == 19 && summary.maxRestoreQpc == 10 && summary.restoreCount == 3, + "inclusive transaction spans and restore timing remain separate"); + Check(summary.frameStatCount == 2 && summary.frameStats[0].frame == 0 && + summary.frameStats[0].transactions == 2 && summary.frameStats[0].records == 4 && + summary.frameStats[0].transactionSpanQpc == 140 && summary.frameStats[0].restoreQpc == 15 && + summary.frameStats[1].frame == 1 && summary.frameStats[1].transactions == 1 && + summary.frameStats[1].records == 2 && summary.frameStats[1].transactionSpanQpc == 20 && + summary.frameStats[1].restoreQpc == 4, + "transaction start frame receives records and distinct restore timings"); + Check(summary.topTechniqueCount == 2 && + std::strcmp(summary.topTechniques[0].name, "Alpha") == 0 && + summary.topTechniques[0].transactionSpanQpc == 120 && + summary.topTechniques[0].restoreQpc == 14 && summary.topTechniques[0].passCount == 3, + "top named techniques are ranked by measured inclusive transaction span"); + JournalAttributionDump(); +} + +void TestStopBoundary() +{ + JournalAttributionStartCapture(); + Check(JournalAttributionTryBeginTransaction(), "boundary transaction admitted"); + std::promise stopped; + auto ready = stopped.get_future(); + std::thread stopper([&] { JournalAttributionStopCapture(); stopped.set_value(); }); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (g_properShadersStateAttributionActive.load(std::memory_order_acquire) && + std::chrono::steady_clock::now() < deadline) std::this_thread::yield(); + Check(!g_properShadersStateAttributionActive.load(std::memory_order_acquire) && + !JournalAttributionTryBeginTransaction() && + ready.wait_for(std::chrono::milliseconds(10)) == std::future_status::timeout, + "StopCapture closes admission and waits for an admitted transaction"); + JournalAttributionObserveTransaction("CrossBoundary", {}, false, {77, 0, 50, false}); + JournalAttributionObserveRestore(77, 7); + JournalAttributionEndTransaction(); + stopper.join(); + JournalAttributionSummary summary{}; + JournalAttributionGetSummary(&summary); + Check(summary.transactions == 1 && summary.restoreCount == 1 && + summary.transactionSpanQpc == 50 && summary.restoreQpc == 7 && summary.inFlight == 0, + "in-flight transaction and restore publish before StopCapture returns"); +} + +void TestReportWriter() +{ +#if defined(JOURNAL_ATTRIBUTION_REPORT_WRITER_VERSION) + Check(!JournalAttributionWriteReport(nullptr), "report writer rejects null destination"); + FILE* output = nullptr; + fopen_s(&output, "journal-writer-test.log", "w+b"); + Check(output != nullptr, "report writer fixture opens a real output file"); + if (!output) return; + const bool written = JournalAttributionWriteReport(output); + std::rewind(output); + char text[16384]{}; + std::fread(text, 1, sizeof(text) - 1, output); + Check(written && std::strstr(text, "captureId=7654321") && + std::strstr(text, "technique=Alpha") && std::strstr(text, "technique=Beta") && + std::strstr(text, "pass=pass index=4 transactions=1 records=1") && + std::strstr(text, "op=TEX count=1"), + "direct writer retains full technique, pass and operation attribution in one destination"); + std::fclose(output); + FILE* readOnly = nullptr; + fopen_s(&readOnly, "journal-writer-test.log", "rb"); + Check(readOnly && !JournalAttributionWriteReport(readOnly), + "actual FILE output failure is reported without leaking the stats lock"); + if (readOnly) std::fclose(readOnly); + JournalAttributionStartCapture(); + JournalAttributionStopCapture(); + fopen_s(&output, "journal-writer-empty.log", "w+b"); + if (!output) { Check(false, "empty report output opens"); return; } + const bool emptyWritten = JournalAttributionWriteReport(output); + std::rewind(output); + std::memset(text, 0, sizeof(text)); + std::fread(text, 1, sizeof(text) - 1, output); + Check(emptyWritten && std::strstr(text, "available=false") && + std::strstr(text, "frames=0 presents=0 techniques=0"), + "direct writer preserves an empty window as explicitly unavailable"); + std::fclose(output); +#else + Check(false, "full journal report writer is available to the combined producer"); +#endif +} + +void TestLockedAdmissionRecheck() +{ + JournalAttributionStartCapture(); + g_admissionAtLock = CreateEventA(nullptr, TRUE, FALSE, nullptr); + g_continueAdmission = CreateEventA(nullptr, TRUE, FALSE, nullptr); + bool admitted = false; + std::thread contender([&] { + g_pauseNextExclusive = true; + admitted = JournalAttributionTryBeginTransaction(); + if (admitted) JournalAttributionEndTransaction(); + }); + const bool reachedLock = WaitForSingleObject(g_admissionAtLock, 2000) == WAIT_OBJECT_0; + JournalAttributionStopCapture(); + SetEvent(g_continueAdmission); + contender.join(); + Check(reachedLock && !admitted, + "locked admission recheck rejects a stale active read after StopCapture"); + CloseHandle(g_admissionAtLock); + CloseHandle(g_continueAdmission); + g_admissionAtLock = nullptr; + g_continueAdmission = nullptr; +} + +void TestCloseAdmission() +{ +#if defined(JOURNAL_ATTRIBUTION_CLOSE_ADMISSION_VERSION) + JournalAttributionStartCapture(); + Check(JournalAttributionTryBeginTransaction(), "nonblocking-close transaction admitted"); + std::promise closed; + auto ready = closed.get_future(); + std::thread closer([&] { JournalAttributionCloseAdmission(); closed.set_value(); }); + const bool returned = ready.wait_for(std::chrono::milliseconds(150)) == std::future_status::ready; + Check(returned && !g_properShadersStateAttributionActive.load(std::memory_order_acquire), + "CloseAdmission returns while a transaction remains admitted"); + if (returned) { + JournalAttributionSummary pending{}; + JournalAttributionGetSummary(&pending); + Check(pending.inFlight == 1 && !pending.active && !JournalAttributionTryBeginTransaction(), + "closed admission rejects new work and preserves the admitted transaction"); + } + JournalAttributionObserveTransaction("AfterClose", {}, false, {501, 0, 50, false}); + JournalAttributionObserveRestore(501, 5); + JournalAttributionEndTransaction(); + closer.join(); + JournalAttributionStopCapture(); + JournalAttributionSummary completed{}; + JournalAttributionGetSummary(&completed); + Check(completed.transactions == 1 && completed.restoreCount == 1 && completed.inFlight == 0, + "transaction completion after CloseAdmission remains visible to synchronous drain"); +#else + Check(false, "nonblocking journal admission close is available"); +#endif +} + +void TestBounds() +{ + JournalAttributionStartCapture(); + for (unsigned i = 0; i < 1100; ++i) { + Observe("Bounded", i + 1, 2, 1); + JournalAttributionOnPresent(); + } + JournalAttributionStopCapture(); + JournalAttributionSummary summary{}; + JournalAttributionGetSummary(&summary); + Check(summary.frames == 1100 && summary.transactions == 1100 && + summary.frameStatCount == 1024 && summary.droppedFrames == 76 && + summary.transactionSpanQpc == 2200 && summary.restoreQpc == 1100, + "bounded frame table reports unavailable frames without dropping capture totals"); +} + +void TestDroppedDetails() +{ + JournalAttributionStartCapture(); + char name[32]{}; + for (unsigned i = 0; i < 100; ++i) { + std::snprintf(name, sizeof(name), "Technique%u", i); + Observe(name, i + 1, i + 1, 1); + } + JournalAttributionStopCapture(); + JournalAttributionSummary summary{}; + JournalAttributionGetSummary(&summary); + Check(summary.techniqueCount == 96 && summary.droppedTechniques == 4 && + summary.transactions == 100 && summary.records == 0 && + summary.transactionSpanQpc == 5050 && summary.restoreCount == 100 && + summary.restoreQpc == 100 && summary.orphanRestores == 0 && + summary.topTechniqueCount == 8 && + std::strcmp(summary.topTechniques[0].name, "Technique95") == 0, + "technique overflow preserves aggregate and restore totals and bounds the top list"); + JournalAttributionStartCapture(); + JournalProbeRecord passes[40]{}; + for (unsigned i = 0; i < 40; ++i) passes[i] = {5, 22, 0, i}; + Observe("PassOverflow", 200, 10, 2, passes); + JournalAttributionStopCapture(); + JournalAttributionGetSummary(&summary); + Check(summary.passCount == 32 && summary.droppedPasses == 8 && + summary.records == 40 && summary.frameStats[0].records == 40, + "pass overflow is explicit without erasing observed records"); +} + +LRESULT CALLBACK JournalTestWindowProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) +{ + return DefWindowProcA(window, message, wparam, lparam); +} + +void TestRealDevice(const char* backendPath) +{ + const bool native = backendPath != nullptr; + std::string path; + if (backendPath) { + path = backendPath; + SetEnvironmentVariableA("DXVK_CONFIG", "d3d9.gtaSaCompat = True; d3d9.gtaSaCompatDiagnostics = True"); + } else { + char systemDirectory[MAX_PATH]{}; + GetSystemDirectoryA(systemDirectory, MAX_PATH); + path = std::string(systemDirectory) + "\\d3d9.dll"; + } + const HMODULE module = LoadLibraryExA(path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); + Check(module != nullptr, "real D3D9 library loads"); + if (!module) return; + using Create = IDirect3D9* (WINAPI*)(UINT); + const auto create = reinterpret_cast(GetProcAddress(module, "Direct3DCreate9")); + IDirect3D9* d3d = create ? create(D3D_SDK_VERSION) : nullptr; + Check(d3d != nullptr, "real D3D9 factory creates"); + if (!d3d) { FreeLibrary(module); return; } + WNDCLASSA wc{}; + wc.lpfnWndProc = JournalTestWindowProc; + wc.hInstance = GetModuleHandleA(nullptr); + wc.lpszClassName = "JournalAttributionRealDeviceTest"; + RegisterClassA(&wc); + HWND window = CreateWindowA(wc.lpszClassName, "journal test", WS_OVERLAPPEDWINDOW, + 0, 0, 64, 64, nullptr, nullptr, wc.hInstance, nullptr); + D3DPRESENT_PARAMETERS parameters{}; + parameters.Windowed = TRUE; + parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; + parameters.hDeviceWindow = window; + parameters.BackBufferWidth = 64; + parameters.BackBufferHeight = 64; + IDirect3DDevice9* device = nullptr; + const HRESULT createResult = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, + D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, ¶meters, &device); + Check(SUCCEEDED(createResult) && device, "real D3D9 HAL device creates"); + if (device) { + device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); + auto* journal = new ProperShadersStateJournal(device); + Check(journal->SupportsNativeStateJournal() == native, + "device journal uses the requested native or local restore path"); + JournalAttributionSetCaptureIdentity(9876); + JournalAttributionStartCapture(); + HRESULT result = journal->BeginTransaction(GetCurrentThreadId(), 0); + journal->SetProbeTechnique("RealDuplicateChanged"); + journal->SetCurrentPass(2); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + const HRESULT restored = journal->Restore(); + DWORD cull = 0; + device->GetRenderState(D3DRS_CULLMODE, &cull); + Check(SUCCEEDED(result) && SUCCEEDED(restored) && cull == D3DCULL_CCW, + "real duplicate and changed journal writes restore original state"); + if (SUCCEEDED(device->Present(nullptr, nullptr, nullptr, nullptr))) JournalAttributionOnPresent(); + result = journal->BeginTransaction(GetCurrentThreadId(), 0); + journal->SetProbeTechnique("RealEmpty"); + Check(SUCCEEDED(result) && SUCCEEDED(journal->Restore()), + "real empty transaction restores and publishes"); + if (SUCCEEDED(device->Present(nullptr, nullptr, nullptr, nullptr))) JournalAttributionOnPresent(); + result = journal->BeginTransaction(GetCurrentThreadId(), 0); + journal->SetProbeTechnique("RealStopBoundary"); + journal->SetCurrentPass(3); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + std::promise stopped; + auto ready = stopped.get_future(); + std::thread stopper([&] { JournalAttributionStopCapture(); stopped.set_value(); }); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (g_properShadersStateAttributionActive.load(std::memory_order_acquire) && + std::chrono::steady_clock::now() < deadline) std::this_thread::yield(); + Check(ready.wait_for(std::chrono::milliseconds(10)) == std::future_status::timeout, + "real journal remains admitted across StopCapture boundary"); + const HRESULT boundaryRestore = journal->Restore(); + stopper.join(); + device->GetRenderState(D3DRS_CULLMODE, &cull); + JournalAttributionSummary summary{}; + JournalAttributionGetSummary(&summary); + Check(SUCCEEDED(result) && SUCCEEDED(boundaryRestore) && cull == D3DCULL_CCW && + summary.captureId == 9876 && summary.transactions == 3 && summary.records == 4 && + summary.restoreCount == 3 && summary.emptyTransactions == 1 && summary.inFlight == 0 && + summary.frames == 2 && summary.frameStatCount == 3 && + summary.frameStats[0].transactions == 1 && summary.frameStats[0].records == 3 && + summary.frameStats[1].transactions == 1 && summary.frameStats[1].records == 0 && + summary.frameStats[2].transactions == 1 && summary.frameStats[2].records == 1 && + summary.transactionSpanQpc > 0 && summary.restoreQpc > 0 && + summary.nativeTransactions == (native ? 3u : 0u), + "real journal publishes precise counts, partial admission frame and separate QPC totals"); + JournalAttributionDump(); + FILE* report = nullptr; + fopen_s(&report, "journal-writer-device.log", "w+b"); + bool reportWritten = report && JournalAttributionWriteReport(report); + char reportText[16384]{}; + if (report) { + std::rewind(report); + std::fread(reportText, 1, sizeof(reportText) - 1, report); + std::fclose(report); + } + Check(reportWritten && std::strstr(reportText, "captureId=9876") && + std::strstr(reportText, "technique=RealDuplicateChanged transactions=1 records=3") && + std::strstr(reportText, "technique=RealStopBoundary"), + "direct writer emits complete actual native/local journal technique rows"); + journal->Release(); + device->Release(); + } + d3d->Release(); + FreeLibrary(module); + if (window) DestroyWindow(window); +} +#endif +} + +int main(int argc, char** argv) +{ +#if defined(JOURNAL_ATTRIBUTION_SUMMARY_VERSION) + if (argc > 1 && std::strcmp(argv[1], "--device") == 0) { + TestRealDevice(argc > 2 ? argv[2] : nullptr); + } else { +#endif + TestInactiveAdmission(); +#if defined(JOURNAL_ATTRIBUTION_SUMMARY_VERSION) + TestSummary(); + TestReportWriter(); + TestStopBoundary(); + TestLockedAdmissionRecheck(); + TestCloseAdmission(); + TestBounds(); + TestDroppedDetails(); +#else + Check(false, "bounded capture summary and per-frame attribution are available"); +#endif +#if defined(JOURNAL_ATTRIBUTION_SUMMARY_VERSION) + } +#endif + std::printf("checks=%u failures=%u\n", g_checks, g_failures); + return g_failures ? 1 : 0; +} diff --git a/tests/journal-attribution-tests.vcxproj b/tests/journal-attribution-tests.vcxproj new file mode 100644 index 0000000..769e00a --- /dev/null +++ b/tests/journal-attribution-tests.vcxproj @@ -0,0 +1,15 @@ + + + ReleaseWin32 + {11026B75-4035-44E7-A484-C6D9912681E8}JournalAttributionTests + + Applicationv145MultiByte + + $(ProjectDir)..\out\perf\20261027-unified-measurement\journal\bin\$(ProjectDir)..\out\perf\20261027-unified-measurement\journal\obj\JournalAttributionTests + + MaxSpeedWIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)MultiThreadedstdcpplatestfalse/utf-8 %(AdditionalOptions)$(ProjectDir)..\sdk\include;$(ProjectDir)..\backend\dxvk\include\vulkan\include;$(D3DX9IncludeDir);%(AdditionalIncludeDirectories) + Consoled3d9.lib;uuid.lib;user32.lib;%(AdditionalDependencies) + + + + diff --git a/tests/lib/benchmark-thread-affinity.h b/tests/lib/benchmark-thread-affinity.h new file mode 100644 index 0000000..99cd868 --- /dev/null +++ b/tests/lib/benchmark-thread-affinity.h @@ -0,0 +1,22 @@ +#pragma once +#include + +// Apply only after device/worker warmup and restore before fixture teardown. +// Timing controls share one allowed logical CPU; runtime checks use normal affinity. +class BenchmarkThreadAffinity { +public: + BenchmarkThreadAffinity() { + DWORD_PTR process = 0, system = 0; + if (GetProcessAffinityMask(GetCurrentProcess(), &process, &system) && process) { + const DWORD_PTR selected = process & (~process + 1); + previous_ = SetThreadAffinityMask(GetCurrentThread(), selected); + } + } + ~BenchmarkThreadAffinity() { + if (previous_) + SetThreadAffinityMask(GetCurrentThread(), previous_); + } + bool valid() const { return previous_ != 0; } +private: + DWORD_PTR previous_ = 0; +}; diff --git a/tests/lib/control-path-results.ps1 b/tests/lib/control-path-results.ps1 new file mode 100644 index 0000000..64a7b06 --- /dev/null +++ b/tests/lib/control-path-results.ps1 @@ -0,0 +1,55 @@ +function Read-ControlPathSamples { + param( + [Parameter(Mandatory)] [string]$Path, + [Parameter(Mandatory)] [Collections.Generic.HashSet[string]]$Expected, + [Parameter(Mandatory)] [int]$Rounds, + [Parameter(Mandatory)] [int]$Seed, + [Parameter(Mandatory)] [string]$Profile, + [Parameter(Mandatory)] [string]$Cpu, + [Parameter(Mandatory)] [bool]$SharedDevice + ) + $seen = [Collections.Generic.HashSet[string]]::new() + $samples = [Collections.Generic.List[object]]::new() + $metadata = $null + $completed = $false + foreach ($line in Get-Content -LiteralPath $Path) { + if ($completed) {throw 'Output after completion'} + if ($line -match '^checks=(\d+) failures=0$') { + $checks = if ($SharedDevice) {127} else {254} + if ([int]$Matches[1] -ne $checks) {throw 'Missing behavioral checks'} + $completed = $true + continue + } + $row = $line | ConvertFrom-Json -ErrorAction Stop + if ($row.type -eq 'metadata') { + if ($null -ne $metadata -or $samples.Count -ne 0 -or + $row.rounds -ne $Rounds -or $row.seed -ne $Seed -or $row.profile -cne $Profile -or + $row.sharedDevice -isnot [bool] -or $row.sharedDevice -ne $SharedDevice -or + $row.pinned -isnot [bool] -or $row.pinned -ne ($Cpu -ne 'any')) {throw 'Wrong metadata'} + foreach ($base in @($row.baselineBase,$row.candidateBase)) { + if ($base -isnot [string] -or $base -notmatch '^(?:0x)?[0-9a-fA-F]{1,16}$' -or + [Convert]::ToUInt64(($base -replace '^0x',''),16) -eq 0) {throw 'Invalid module base'} + } + $sameBase = [Convert]::ToUInt64(($row.baselineBase -replace '^0x',''),16) -eq + [Convert]::ToUInt64(($row.candidateBase -replace '^0x',''),16) + if ($sameBase -ne $SharedDevice) {throw 'Wrong module topology'} + $metadata = $row + continue + } + if ($null -eq $metadata -or $row.type -cne 'sample' -or !$Expected.Contains($row.case) -or + $row.variant -cnotin @('baseline','candidate') -or $row.round -isnot [long] -or + $row.round -lt 0 -or $row.round -ge $Rounds) {throw 'Unexpected sample'} + if (!$seen.Add("$($row.case)|$($row.variant)|$($row.round)")) {throw 'Duplicate sample'} + $iterations = if ($row.case.StartsWith('setter,')) {300000} else {16000} + foreach ($value in @($row.ns,$row.cycles)) { + if (($value -isnot [double] -and $value -isnot [long]) -or + [double]::IsNaN($value) -or [double]::IsInfinity($value) -or $value -le 0) {throw 'Invalid timing'} + } + if ($row.iterations -ne $iterations -or $row.cpuStart -isnot [long] -or $row.cpuStart -lt 0 -or + $row.cpuEnd -isnot [long] -or $row.cpuEnd -lt 0) {throw 'Invalid iterations/CPU'} + if ($Cpu -ne 'any' -and ($row.cpuStart -ne [int]$Cpu -or $row.cpuEnd -ne [int]$Cpu)) {throw 'Pinned CPU mismatch'} + $samples.Add($row) + } + if ($null -eq $metadata -or !$completed -or $seen.Count -ne 2 * $Rounds * $Expected.Count) {throw 'Incomplete output'} + return [pscustomobject]@{metadata=$metadata;samples=@($samples);checks=$checks} +} diff --git a/tests/lib/win32-import-hook.h b/tests/lib/win32-import-hook.h new file mode 100644 index 0000000..d577dce --- /dev/null +++ b/tests/lib/win32-import-hook.h @@ -0,0 +1,50 @@ +#pragma once +#include +#include + +// Single-threaded test harness utility. The loaded module outlives this object. +struct ImportHook { + ULONG_PTR* slot = nullptr; + ULONG_PTR original = 0; + + bool install(HMODULE module, const char* name, void* replacement) { + auto base = reinterpret_cast(module); + auto dos = reinterpret_cast(base); + auto nt = reinterpret_cast(base + dos->e_lfanew); + auto descriptor = reinterpret_cast( + base + nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress); + for (; descriptor->Name; ++descriptor) { + if (!descriptor->OriginalFirstThunk) + continue; + auto names = reinterpret_cast(base + descriptor->OriginalFirstThunk); + auto addresses = reinterpret_cast(base + descriptor->FirstThunk); + for (; names->u1.AddressOfData; ++names, ++addresses) { + if (IMAGE_SNAP_BY_ORDINAL(names->u1.Ordinal)) + continue; + auto import = reinterpret_cast(base + names->u1.AddressOfData); + if (std::strcmp(reinterpret_cast(import->Name), name)) + continue; + slot = reinterpret_cast(&addresses->u1.Function); + original = *slot; + DWORD oldProtection = 0; + if (!VirtualProtect(slot, sizeof(*slot), PAGE_READWRITE, &oldProtection)) + return false; + *slot = reinterpret_cast(replacement); + DWORD ignored; + return VirtualProtect(slot, sizeof(*slot), oldProtection, &ignored) != FALSE; + } + } + return false; + } + + ~ImportHook() { + if (!slot) + return; + DWORD oldProtection = 0; + if (VirtualProtect(slot, sizeof(*slot), PAGE_READWRITE, &oldProtection)) { + *slot = original; + DWORD ignored; + VirtualProtect(slot, sizeof(*slot), oldProtection, &ignored); + } + } +}; diff --git a/tests/measurement-backend-probe.cpp b/tests/measurement-backend-probe.cpp new file mode 100644 index 0000000..59172cf --- /dev/null +++ b/tests/measurement-backend-probe.cpp @@ -0,0 +1,434 @@ +#include +#include +#include "../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h" +#include "../sdk/include/sa_renderstack/backend_api.h" +#include +#include +#include +#include +#include + +static unsigned checks, failures; +#define CHECK(c) do { ++checks; if (!(c)) { ++failures; std::printf("FAIL line=%u %s\n", __LINE__, #c); } } while (0) +// The fallback lets the pre-fix header/DLL fail behaviorally for absent timing +// coverage, rather than making the RED a compile failure. +template static auto Timed(const Counter& c, int) -> decltype(c.TimedCalls) { return c.TimedCalls; } +template static UINT64 Timed(const Counter&, ...) { return 0; } +static D3D9BridgeAuditSnapshot Audit(ID3D9BridgeDiagnostics* d) { + D3D9BridgeAuditSnapshot s{}; s.StructSize=sizeof(s); s.Version=1; + CHECK(d->GetAudit(&s)==S_OK); return s; +} +static void TestOrdinaryTiming(IDirect3DDevice9* device, ID3D9BridgeDiagnostics* diagnostics) { + const DWORD vertexCode[]={0xfffe0101,0x00000001,0xc00f0000,0xa0e40000,0x0000ffff}; + const DWORD pixelCode[]={0xffff0101,0x00000001,0x800f0000,0xa0e40000,0x0000ffff}; + IDirect3DVertexShader9* vertex=nullptr;IDirect3DPixelShader9* pixel=nullptr; + CHECK(SUCCEEDED(device->CreateVertexShader(vertexCode,&vertex))); + CHECK(SUCCEEDED(device->CreatePixelShader(pixelCode,&pixel))); + CHECK(diagnostics->StartAudit(76,3000)==S_OK); + for(unsigned i=0;i<32;i++) { + const float f[4]={float(i+10),float(i+20),float(i+30),float(i+40)}; + const int integers[4]={int(i+10),int(i+20),int(i+30),int(i+40)}; + const BOOL boolean=(i&1)?FALSE:TRUE; + for(unsigned duplicate=0;duplicate<2;duplicate++) { + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ALPHABLENDENABLE,boolean))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSV,(i&1)?D3DTADDRESS_CLAMP:D3DTADDRESS_WRAP))); + CHECK(SUCCEEDED(device->SetTextureStageState(0,D3DTSS_COLOROP,(i&1)?D3DTOP_ADD:D3DTOP_SELECTARG1))); + CHECK(SUCCEEDED(device->SetTexture(0,nullptr))); + CHECK(SUCCEEDED(device->SetVertexShader((i&1)?nullptr:vertex))); + CHECK(SUCCEEDED(device->SetPixelShader((i&1)?nullptr:pixel))); + CHECK(SUCCEEDED(device->SetVertexShaderConstantF(0,f,1))); + CHECK(SUCCEEDED(device->SetVertexShaderConstantI(0,integers,1))); + CHECK(SUCCEEDED(device->SetVertexShaderConstantB(0,&boolean,1))); + CHECK(SUCCEEDED(device->SetPixelShaderConstantF(0,f,1))); + CHECK(SUCCEEDED(device->SetPixelShaderConstantI(0,integers,1))); + CHECK(SUCCEEDED(device->SetPixelShaderConstantB(0,&boolean,1))); + } + } + CHECK(device->SetVertexShaderConstantF(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetVertexShaderConstantF(0,nullptr,0)==S_OK); + CHECK(device->SetPixelShaderConstantI(100000,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(diagnostics->StopAudit(76)==S_OK); + const auto snapshot=Audit(diagnostics); + UINT64 sumCalls=0,sumTicks=0; + for(unsigned i=0;i<12;i++) { + const auto& counter=snapshot.Counters[i]; + CHECK(counter.Calls==64 && Timed(counter,0)==64); + CHECK(counter.QpcTicks>0 && counter.MaxQpcTicks>0 && counter.MaxQpcTicks<=counter.QpcTicks); + // BOOL uploads deliberately preserve upstream unconditional upload + // semantics; timing must not change that existing admission policy. + CHECK((i==8 || i==11) ? counter.RedundantCalls==0 : counter.RedundantCalls>=32); + sumCalls+=counter.Calls;sumTicks+=counter.QpcTicks; + std::printf("ordinary category=%u calls=%llu redundant=%llu timed=%llu qpc=%llu max=%llu\n", + i,counter.Calls,counter.RedundantCalls,Timed(counter,0),counter.QpcTicks,counter.MaxQpcTicks); + } + CHECK(snapshot.Total.Calls==768 && Timed(snapshot.Total,0)==768 && sumCalls==768); + CHECK(snapshot.Total.QpcTicks==sumTicks); + // Completed snapshots remain fixed when further ordinary setters run idle. + CHECK(SUCCEEDED(device->SetVertexShader(nullptr)));CHECK(SUCCEEDED(device->SetPixelShader(nullptr))); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE))); + const auto stopped=Audit(diagnostics); + CHECK(stopped.Total.Calls==768 && Timed(stopped.Total,0)==768 && stopped.Total.QpcTicks==sumTicks); + if(vertex)vertex->Release();if(pixel)pixel->Release(); +} +static void TestConstantBatchTiming(IDirect3DDevice9* device, ID3D9BridgeDiagnostics* diagnostics) { + ID3D9GtaSaCompatDevice2* compat=nullptr; + CHECK(SUCCEEDED(device->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice2),reinterpret_cast(&compat)))); + if(!compat)return; + CHECK(diagnostics->StartAudit(77,3000)==S_OK); + for(unsigned i=0;i<32;i++) { + const float values[4]={float(i+100),2,3,4}; + D3D9GtaSaFloatConstantRange range{0,1,values}; + D3D9GtaSaStateBatch batch{};batch.StructSize=sizeof(batch);batch.ApiVersion=7; + batch.VertexFloatRangeCount=1;batch.VertexFloatRanges=⦥ + CHECK(compat->SubmitStateBatch(&batch)==S_OK); + CHECK(compat->SubmitStateBatch(&batch)==S_OK); + } + CHECK(diagnostics->StopAudit(77)==S_OK); + const auto snapshot=Audit(diagnostics); + CHECK(snapshot.Counters[6].Calls==64 && snapshot.Counters[6].RedundantCalls==32); + CHECK(Timed(snapshot.Counters[6],0)==64 && snapshot.Counters[6].QpcTicks>0); + std::printf("batch constant timing calls=%llu redundant=%llu timed=%llu qpc=%llu\n", + snapshot.Counters[6].Calls,snapshot.Counters[6].RedundantCalls, + Timed(snapshot.Counters[6],0),snapshot.Counters[6].QpcTicks); + compat->Release(); + ID3D9GtaSaCompatDevice4* effect=nullptr; + CHECK(SUCCEEDED(device->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice4),reinterpret_cast(&effect)))); + if(!effect)return; + CHECK(diagnostics->StartAudit(78,3000)==S_OK); + for(unsigned i=0;i<32;i++) { + const float floats[4]={float(i+200),3,4,5};const int integers[4]={int(i+200),3,4,5}; + const BOOL boolean=(i&1)?FALSE:TRUE; + D3D9GtaSaFloatConstantRange f{0,1,floats};D3D9GtaSaIntConstantRange integersRange{0,1,integers}; + D3D9GtaSaBoolConstantRange b{0,1,&boolean}; + D3D9GtaSaEffectStateBatch batch{};batch.StructSize=sizeof(batch);batch.ApiVersion=7; + batch.VertexFloatRangeCount=batch.PixelFloatRangeCount=1; + batch.VertexFloatRanges=batch.PixelFloatRanges=&f; + batch.VertexIntRangeCount=batch.PixelIntRangeCount=1; + batch.VertexIntRanges=batch.PixelIntRanges=&integersRange; + batch.VertexBoolRangeCount=batch.PixelBoolRangeCount=1; + batch.VertexBoolRanges=batch.PixelBoolRanges=&b; + CHECK(effect->SubmitEffectStateBatch(&batch)==S_OK); + CHECK(effect->SubmitEffectStateBatch(&batch)==S_OK); + } + CHECK(diagnostics->StopAudit(78)==S_OK); + const auto effectSnapshot=Audit(diagnostics); + for(unsigned i=6;i<12;i++) { + const auto& counter=effectSnapshot.Counters[i]; + CHECK(counter.Calls==64 && Timed(counter,0)==64 && counter.QpcTicks>0); + CHECK((i==8 || i==11) ? counter.RedundantCalls==0 : counter.RedundantCalls==32); + } + CHECK(effectSnapshot.Total.Calls==384 && Timed(effectSnapshot.Total,0)==384); + std::printf("effect batch timing calls=%llu timed=%llu qpc=%llu\n",effectSnapshot.Total.Calls, + Timed(effectSnapshot.Total,0),effectSnapshot.Total.QpcTicks); + effect->Release(); +} +static void TestAuditAdmission(IDirect3DDevice9* device, ID3D9BridgeDiagnostics* diagnostics) { + DWORD oldRender=0,oldSampler=0; + CHECK(device->GetRenderState(D3DRS_ALPHABLENDENABLE,&oldRender)==S_OK); + CHECK(device->GetSamplerState(0,D3DSAMP_MAGFILTER,&oldSampler)==S_OK); + CHECK(diagnostics->StartAudit(79,3000)==S_OK); + CHECK(device->BeginStateBlock()==S_OK); + for(unsigned i=0;i<64;i++) { + const float floats[4]={float(i+500),2,3,4};const int integers[4]={int(i+500),2,3,4}; + const BOOL boolean=(i&1)?FALSE:TRUE; + CHECK(device->SetRenderState(D3DRS_ALPHABLENDENABLE,boolean)==S_OK); + CHECK(device->SetSamplerState(0,D3DSAMP_MAGFILTER,(i&1)?D3DTEXF_POINT:D3DTEXF_LINEAR)==S_OK); + CHECK(device->SetTextureStageState(0,D3DTSS_COLOROP,(i&1)?D3DTOP_ADD:D3DTOP_SELECTARG1)==S_OK); + CHECK(device->SetTexture(0,nullptr)==S_OK); + CHECK(device->SetVertexShader(nullptr)==S_OK); + CHECK(device->SetPixelShader(nullptr)==S_OK); + CHECK(device->SetVertexShaderConstantF(0,floats,1)==S_OK); + CHECK(device->SetVertexShaderConstantI(0,integers,1)==S_OK); + CHECK(device->SetVertexShaderConstantB(0,&boolean,1)==S_OK); + CHECK(device->SetPixelShaderConstantF(0,floats,1)==S_OK); + CHECK(device->SetPixelShaderConstantI(0,integers,1)==S_OK); + CHECK(device->SetPixelShaderConstantB(0,&boolean,1)==S_OK); + } + IDirect3DStateBlock9* recorded=nullptr;CHECK(device->EndStateBlock(&recorded)==S_OK && recorded); + auto recording=Audit(diagnostics); + for(unsigned i=0;i<12;i++) { + const auto& counter=recording.Counters[i]; + CHECK(counter.Calls==0 && Timed(counter,0)==0 && counter.QpcTicks==0 && counter.MaxQpcTicks==0); + std::printf("record-only category=%u attempts=64 calls=%llu timed=%llu qpc=%llu\n", + i,counter.Calls,Timed(counter,0),counter.QpcTicks); + } + CHECK(recording.Total.Calls==2 && Timed(recording.Total,0)==0 && recording.Total.QpcTicks==0); + CHECK(recording.Counters[13].Calls==1 && recording.Counters[14].Calls==1); + DWORD currentRender=0,currentSampler=0; + CHECK(device->GetRenderState(D3DRS_ALPHABLENDENABLE,¤tRender)==S_OK && currentRender==oldRender); + CHECK(device->GetSamplerState(0,D3DSAMP_MAGFILTER,¤tSampler)==S_OK && currentSampler==oldSampler); + for(unsigned i=0;i<32;i++) { + CHECK(device->SetVertexShaderConstantF(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetVertexShaderConstantI(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetVertexShaderConstantB(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetPixelShaderConstantF(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetPixelShaderConstantI(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetPixelShaderConstantB(0,nullptr,1)==D3DERR_INVALIDCALL); + CHECK(device->SetVertexShaderConstantF(0,nullptr,0)==S_OK); + CHECK(device->SetVertexShaderConstantI(0,nullptr,0)==S_OK); + CHECK(device->SetVertexShaderConstantB(0,nullptr,0)==S_OK); + CHECK(device->SetPixelShaderConstantF(0,nullptr,0)==S_OK); + CHECK(device->SetPixelShaderConstantI(0,nullptr,0)==S_OK); + CHECK(device->SetPixelShaderConstantB(0,nullptr,0)==S_OK); + CHECK(device->SetRenderState(static_cast(256),0)==S_OK); + CHECK(device->SetSamplerState(999,D3DSAMP_MAGFILTER,D3DTEXF_POINT)==S_OK); + CHECK(device->SetTexture(999,nullptr)==S_OK); + } + CHECK(diagnostics->StopAudit(79)==S_OK); + auto rejected=Audit(diagnostics); + for(unsigned i=0;i<12;i++) { + const auto& counter=rejected.Counters[i]; + CHECK(counter.Calls==0 && Timed(counter,0)==0 && counter.QpcTicks==0 && counter.MaxQpcTicks==0); + } + CHECK(rejected.Total.Calls==2 && Timed(rejected.Total,0)==0 && rejected.Total.QpcTicks==0); + std::printf("recording and rejected/zero-count total calls=%llu timed=%llu qpc=%llu\n", + rejected.Total.Calls,Timed(rejected.Total,0),rejected.Total.QpcTicks); + if(recorded)recorded->Release(); + // Positive control: after recording ends, the same admitted native path + // still records one elapsed span per counted setter. + CHECK(diagnostics->StartAudit(80,3000)==S_OK); + for(unsigned i=0;i<64;i++) + CHECK(device->SetSamplerState(0,D3DSAMP_MAGFILTER,(i&1)?D3DTEXF_POINT:D3DTEXF_LINEAR)==S_OK); + CHECK(diagnostics->StopAudit(80)==S_OK); + const auto positive=Audit(diagnostics); + CHECK(positive.Counters[1].Calls==64 && Timed(positive.Counters[1],0)==64 && positive.Counters[1].QpcTicks>0); + std::printf("post-record native sampler calls=%llu timed=%llu qpc=%llu\n", + positive.Counters[1].Calls,Timed(positive.Counters[1],0),positive.Counters[1].QpcTicks); +} +static unsigned CaptureHud(HWND window, const char* filename) { + RECT rect{};GetClientRect(window,&rect); + const int width=rect.right,height=rect.bottom; + BITMAPINFO info{};info.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); + info.bmiHeader.biWidth=width;info.bmiHeader.biHeight=-height; + info.bmiHeader.biPlanes=1;info.bmiHeader.biBitCount=32;info.bmiHeader.biCompression=BI_RGB; + HDC dc=GetDC(window),copy=CreateCompatibleDC(dc);void* pixels=nullptr; + HBITMAP bitmap=CreateDIBSection(dc,&info,DIB_RGB_COLORS,&pixels,nullptr,0); + auto old=SelectObject(copy,bitmap); + CHECK(BitBlt(copy,0,0,width,height,dc,0,0,SRCCOPY)); + unsigned textPixels=0; + auto data=static_cast(pixels); + for(int y=40;y<180 && y100 && p[1]>150 && p[2]<230) ++textPixels; + } + BITMAPFILEHEADER file{};file.bfType=0x4d42;file.bfOffBits=sizeof(file)+sizeof(info.bmiHeader); + file.bfSize=file.bfOffBits+width*height*4; + if(auto output=std::fopen(filename,"wb")) { + std::fwrite(&file,sizeof(file),1,output);std::fwrite(&info.bmiHeader,sizeof(info.bmiHeader),1,output); + std::fwrite(pixels,width*height*4,1,output);std::fclose(output); + } else CHECK(false); + SelectObject(copy,old);DeleteObject(bitmap);DeleteDC(copy);ReleaseDC(window,dc); + return textPixels; +} +static void PresentHud(IDirect3DDevice9* device, HWND window) { + for(unsigned i=0;i<8;i++) { + MSG message{};while(PeekMessage(&message,nullptr,0,0,PM_REMOVE)) {TranslateMessage(&message);DispatchMessage(&message);} + CHECK(SUCCEEDED(device->Clear(0,nullptr,D3DCLEAR_TARGET,0xff123456,1,0))); + CHECK(SUCCEEDED(device->Present(nullptr,nullptr,nullptr,nullptr))); + Sleep(60); + } + Sleep(100); +} +struct ReleaseResult { ID3D9BridgeDiagnostics* Diagnostics; D3D9BridgeAuditSnapshot Snapshot; bool Called; }; +static void STDMETHODCALLTYPE Observe(void* context, IDirect3DDevice9*, + D3D9BridgeEvent event, HRESULT, D3DPRESENT_PARAMETERS*, const D3D9BridgePresentArgs*) { + if (event != D3D9BridgeEvent::FinalRelease) return; + auto& result=*static_cast(context); + result.Snapshot=Audit(result.Diagnostics);result.Called=true; +} +int main(int argc,char**argv) { + if (argc!=2 && argc!=3) return 2; + const bool disabled=argc==3 && std::strcmp(argv[2],"--disabled")==0; + const bool render=argc==3 && std::strcmp(argv[2],"--render")==0; + auto module=LoadLibraryA(argv[1]); + if (!module) {std::printf("FAIL LoadLibrary %lu\n",GetLastError());return 2;} + auto create=reinterpret_cast(GetProcAddress(module,"Direct3DCreate9")); + auto factory=create?create(D3D_SDK_VERSION):nullptr; + if (!factory) return 2; + auto window=CreateWindowA("STATIC","Measurement backend fixture",WS_OVERLAPPEDWINDOW,0,0,640,480,nullptr,nullptr,GetModuleHandle(nullptr),nullptr); + D3DPRESENT_PARAMETERS pp{}; + pp.Windowed=TRUE;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.hDeviceWindow=window; + pp.BackBufferWidth=640;pp.BackBufferHeight=480;pp.BackBufferFormat=D3DFMT_X8R8G8B8; + pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* device=nullptr; + auto hr=factory->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&pp,&device); + if (FAILED(hr)) { std::printf("FAIL CreateDevice 0x%08lx\n",(unsigned long)hr);return 2; } + ID3D9BridgeDiagnostics* d=nullptr; + hr=device->QueryInterface(IID_D3D9BridgeDiagnostics,reinterpret_cast(&d)); + std::printf("diagnostics QI hr=0x%08lx\n",(unsigned long)hr); + CHECK(hr==S_OK && d); + if (!d) return 1; + if (render) { + ShowWindow(window,SW_SHOWNORMAL);UpdateWindow(window); + PresentHud(device,window); + const auto idlePixels=CaptureHud(window,"hud-unowned.bmp"); + CHECK(idlePixels==0); + CHECK(d->SetCaptureOwner(TRUE)==S_OK); + D3D9BridgeHudSnapshot visible{};visible.StructSize=sizeof(visible);visible.Version=1; + visible.Visible=1;visible.LineCount=8;visible.CaptureId=5001; + const char* lines[]={"RenderStack capture 5001: state attribution","Elapsed 1.0 / 3.0 sec; progress 33%", + "Baseline: 100 FPS, avg 10 ms, max 12 ms","CPU: 85 FPS; samples 2100, missed 2", + "Hotspot: verified function @ module+RVA","State API: 300 calls, 200 redundant", + "Journal: inclusive spans; backend available","Complete report: out/perf/measurement"}; + for(unsigned i=0;i<8;i++)std::strcpy(visible.Lines[i],lines[i]); + CHECK(d->PublishHud(&visible)==S_OK);PresentHud(device,window); + const auto visiblePixels=CaptureHud(window,"hud-published.bmp"); + CHECK(visiblePixels>100); + visible.Visible=0;CHECK(d->PublishHud(&visible)==S_OK); + CHECK(d->SetCaptureOwner(FALSE)==S_OK);PresentHud(device,window); + const auto hiddenPixels=CaptureHud(window,"hud-hidden.bmp");CHECK(hiddenPixels==0); + std::printf("post-HUD client pixels unowned=%u visible=%u hidden=%u\n",idlePixels,visiblePixels,hiddenPixels); + d->Release();device->Release();factory->Release();DestroyWindow(window); + std::printf("measurement backend HUD checks=%u failures=%u\n",checks,failures); + return failures?1:0; + } + IUnknown* identity=nullptr;CHECK(d->QueryInterface(IID_IUnknown,reinterpret_cast(&identity))==S_OK); + CHECK(identity==static_cast(device));if(identity)identity->Release(); + const auto initial=Audit(d); + if (disabled) { + CHECK(initial.Status==D3D9_BRIDGE_AUDIT_UNAVAILABLE && !initial.Available); + CHECK(d->SetCaptureOwner(TRUE)==S_OK); + CHECK(d->StartAudit(77,3000)==D3DERR_NOTAVAILABLE); + const auto unavailable=Audit(d); + CHECK(unavailable.CaptureId==77 && unavailable.Status==D3D9_BRIDGE_AUDIT_UNAVAILABLE && !unavailable.Active); + d->Release();device->Release();factory->Release();DestroyWindow(window); + std::printf("measurement backend unavailable checks=%u failures=%u\n",checks,failures); + return failures?1:0; + } + CHECK(initial.Status==D3D9_BRIDGE_AUDIT_IDLE && initial.CaptureId==0 && !initial.Active); + D3D9BridgeHudSnapshot idle{};idle.StructSize=sizeof(idle);idle.Version=1; + CHECK(d->GetHud(&idle)==S_OK && !idle.Visible); + CHECK(d->SetCaptureOwner(TRUE)==S_OK); + CHECK(d->GetHud(&idle)==S_OK && idle.Visible && idle.LineCount==1); + CHECK(d->StartAudit(0,3000)==D3DERR_INVALIDCALL); + CHECK(d->StartAudit(71,0)==D3DERR_INVALIDCALL); + CHECK(d->StartAudit(71,3000)==S_OK); + CHECK(d->StartAudit(72,3000)==D3DERR_INVALIDCALL); + CHECK(d->StopAudit(72)==D3DERR_INVALIDCALL); + auto s=Audit(d);CHECK(s.Active && s.CaptureId==71); + DWORD old=0;CHECK(SUCCEEDED(device->GetRenderState(D3DRS_ZENABLE,&old))); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ZENABLE,old))); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ZENABLE,old==D3DZB_FALSE?D3DZB_TRUE:D3DZB_FALSE))); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ZENABLE,old==D3DZB_FALSE?D3DZB_TRUE:D3DZB_FALSE))); + const float constants[4]={1,2,3,4}; + CHECK(SUCCEEDED(device->SetVertexShaderConstantF(0,constants,1))); + CHECK(SUCCEEDED(device->SetVertexShaderConstantF(0,constants,1))); + IDirect3DStateBlock9* block=nullptr; + CHECK(SUCCEEDED(device->CreateStateBlock(D3DSBT_ALL,&block))); + CHECK(block && SUCCEEDED(block->Capture()));if(block)block->Release(); + device->Clear(0,nullptr,D3DCLEAR_TARGET,0xff123456,1,0); + auto present=device->Present(nullptr,nullptr,nullptr,nullptr); + CHECK(SUCCEEDED(present)); + CHECK(d->StopAudit(71)==S_OK); + s=Audit(d); + CHECK(s.CaptureId==71 && s.Available && !s.Active && s.Status==D3D9_BRIDGE_AUDIT_COMPLETE); + CHECK(s.Counters[0].Calls==3 && s.Counters[0].RedundantCalls==2); + CHECK(Timed(s.Counters[0],0)==3 && s.Counters[0].MaxQpcTicks<=s.Counters[0].QpcTicks); + CHECK(s.Counters[6].Calls==2 && s.Counters[6].RedundantCalls==1 && s.Counters[6].Units==2); + CHECK(Timed(s.Counters[6],0)==2); + CHECK(s.Counters[15].Calls>=1 && s.Counters[15].QpcTicks>0 && s.QpcFrequency>0); + CHECK(Timed(s.Counters[15],0)==s.Counters[15].Calls); + CHECK(s.Counters[12].Calls==1 && Timed(s.Counters[12],0)==0 && s.Counters[12].QpcTicks==0); + CHECK(s.Total.Calls==7 && Timed(s.Total,0)==6); + std::printf("ordinary timing renderCalls=%llu renderTimed=%llu renderQpc=%llu constantCalls=%llu constantTimed=%llu constantQpc=%llu stateblockTimed=%llu\n", + s.Counters[0].Calls,Timed(s.Counters[0],0),s.Counters[0].QpcTicks, + s.Counters[6].Calls,Timed(s.Counters[6],0),s.Counters[6].QpcTicks,Timed(s.Counters[15],0)); + CHECK(s.ObservedPresentBoundaries==1); + CHECK(d->StopAudit(71)==S_FALSE); + CHECK(Audit(d).Total.Calls==s.Total.Calls); + // Existing sampler/texture/prepare-draw paths, with true duplicates and + // changed bindings; removing any CopyStateAudit category must fail here. + DWORD address=0;CHECK(SUCCEEDED(device->GetSamplerState(0,D3DSAMP_ADDRESSU,&address))); + IDirect3DTexture9* texture=nullptr; + CHECK(SUCCEEDED(device->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&texture,nullptr))); + CHECK(d->StartAudit(75,3000)==S_OK); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSU,address))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSU,address==D3DTADDRESS_WRAP?D3DTADDRESS_CLAMP:D3DTADDRESS_WRAP))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSU,address==D3DTADDRESS_WRAP?D3DTADDRESS_CLAMP:D3DTADDRESS_WRAP))); + CHECK(SUCCEEDED(device->SetTexture(0,nullptr))); + CHECK(SUCCEEDED(device->SetTexture(0,texture))); + CHECK(SUCCEEDED(device->SetTexture(0,texture))); + struct Vertex {float x,y,z,w;DWORD color;} vertices[]={{30,30,0,1,0xffffffff},{40,30,0,1,0xffffffff},{30,40,0,1,0xffffffff}}; + CHECK(SUCCEEDED(device->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE))); + CHECK(SUCCEEDED(device->BeginScene())); + CHECK(SUCCEEDED(device->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,vertices,sizeof(Vertex)))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,2))); + // Change consumed inputs, then restore them before the next draw. This + // genuinely dirties bindings while retaining the submitted sampler/view. + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSU,address))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_ADDRESSU,address==D3DTADDRESS_WRAP?D3DTADDRESS_CLAMP:D3DTADDRESS_WRAP))); + CHECK(SUCCEEDED(device->SetTexture(0,nullptr))); + CHECK(SUCCEEDED(device->SetTexture(0,texture))); + CHECK(SUCCEEDED(device->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,vertices,sizeof(Vertex)))); + CHECK(SUCCEEDED(device->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,4))); + CHECK(SUCCEEDED(device->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,vertices,sizeof(Vertex)))); + CHECK(SUCCEEDED(device->EndScene())); + CHECK(d->StopAudit(75)==S_OK); + const auto bindings=Audit(d); + CHECK(bindings.Counters[1].Calls==7 && bindings.Counters[1].RedundantCalls==2); + CHECK(Timed(bindings.Counters[1],0)==7); + CHECK(bindings.Counters[3].Calls==5 && bindings.Counters[3].RedundantCalls==2); + CHECK(Timed(bindings.Counters[3],0)==5); + CHECK(bindings.PrepareDraws==3 && bindings.PrepareCategoryEvents>0 && bindings.PrepareMaxCategoriesPerDraw>0); + CHECK(bindings.Prepare[7].Draws>=1 && bindings.Prepare[8].Draws>=1); + // The final high-bit-only sRGB change must not dirty either binding. + CHECK(bindings.Prepare[7].Draws==2 && bindings.Prepare[8].Draws==2); + CHECK(bindings.SamplerBindingCacheHits>0 && bindings.TextureBindingCacheHits>0); + CHECK(bindings.ResourceBindingCacheInvalidations==0); + std::printf("native counters samplerCalls=%llu textureCalls=%llu prepareDraws=%llu samplerHits=%llu textureHits=%llu invalidations=%llu\n", + bindings.Counters[1].Calls,bindings.Counters[3].Calls,bindings.PrepareDraws, + bindings.SamplerBindingCacheHits,bindings.TextureBindingCacheHits,bindings.ResourceBindingCacheInvalidations); + CHECK(SUCCEEDED(device->SetTexture(0,nullptr)));if(texture)texture->Release(); + TestOrdinaryTiming(device,d); + TestConstantBatchTiming(device,d); + TestAuditAdmission(device,d); + CHECK(d->GetAudit(nullptr)==E_POINTER); + D3D9BridgeAuditSnapshot invalid{};CHECK(d->GetAudit(&invalid)==D3DERR_INVALIDCALL); + D3D9BridgeHudSnapshot hud{};hud.StructSize=sizeof(hud);hud.Version=1;hud.CaptureId=71;hud.Visible=1;hud.LineCount=2; + std::strcpy(hud.Lines[0],"RenderStack capture 71");std::strcpy(hud.Lines[1],"Backend native API counters verified"); + CHECK(d->PublishHud(nullptr)==E_POINTER); + CHECK(d->PublishHud(&hud)==S_OK); + D3D9BridgeHudSnapshot copy{};copy.StructSize=sizeof(copy);copy.Version=1;CHECK(d->GetHud(©)==S_OK); + CHECK(std::strcmp(copy.Lines[0],hud.Lines[0])==0 && copy.CaptureId==71); + hud.Version=2;CHECK(d->PublishHud(&hud)==D3DERR_INVALIDCALL);hud.Version=1; + hud.StructSize=sizeof(hud)-1;CHECK(d->PublishHud(&hud)==D3DERR_INVALIDCALL);hud.StructSize=sizeof(hud); + hud.LineCount=9;CHECK(d->PublishHud(&hud)==D3DERR_INVALIDCALL);hud.LineCount=2; + hud.ProgressPermille=1001;CHECK(d->PublishHud(&hud)==D3DERR_INVALIDCALL);hud.ProgressPermille=0; + std::memset(hud.Lines[0],'x',sizeof(hud.Lines[0]));CHECK(d->PublishHud(&hud)==D3DERR_INVALIDCALL); + CHECK(d->GetHud(nullptr)==E_POINTER); + // Fixed snapshots are copied coherently while a publisher updates them. + std::atomic copying{true}, coherent{true}; + std::thread reader([&] {while(copying) {D3D9BridgeHudSnapshot c{};c.StructSize=sizeof(c);c.Version=1; + if(d->GetHud(&c)!=S_OK || (c.CaptureId!=71 && c.CaptureId!=91))coherent=false;}}); + std::memset(hud.Lines,0,sizeof(hud.Lines));hud.CaptureId=91; + for(unsigned i=0;i<1000;++i) CHECK(d->PublishHud(&hud)==S_OK); + copying=false;reader.join();CHECK(coherent); + CHECK(d->StartAudit(81,3000)==S_OK); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE))); + CHECK(SUCCEEDED(device->Reset(&pp))); + auto reset=Audit(d);CHECK(reset.CaptureId==81 && !reset.Active && reset.Status==D3D9_BRIDGE_AUDIT_INTERRUPTED); + CHECK(reset.Total.Calls==1); + CHECK(d->SetCaptureOwner(FALSE)==S_OK); + CHECK(Audit(d).CaptureId==81); + CHECK(d->StartAudit(85,3000)==D3DERR_INVALIDCALL); + CHECK(d->SetCaptureOwner(TRUE)==S_OK); + CHECK(d->StartAudit(85,3000)==S_OK); + CHECK(d->SetCaptureOwner(FALSE)==S_OK); + const auto abandoned=Audit(d);CHECK(abandoned.CaptureId==85 && !abandoned.Active && abandoned.Status==D3D9_BRIDGE_AUDIT_INTERRUPTED); + CHECK(d->SetCaptureOwner(TRUE)==S_OK); + CHECK(d->StartAudit(91,3000)==S_OK); + CHECK(SUCCEEDED(device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE))); + ID3D9BridgeLifecycle* lifecycle=nullptr; + CHECK(device->QueryInterface(IID_D3D9BridgeLifecycle,reinterpret_cast(&lifecycle))==S_OK); + ReleaseResult release{d,{ },false}; + D3D9BridgeObserver observer{sizeof(observer),&release,&Observe}; + CHECK(lifecycle && lifecycle->SetObserver(&observer)==S_OK); + if(lifecycle)lifecycle->Release(); + d->Release();device->Release(); + CHECK(release.Called && release.Snapshot.CaptureId==91 && !release.Snapshot.Active && + release.Snapshot.Status==D3D9_BRIDGE_AUDIT_INTERRUPTED && release.Snapshot.Total.Calls==1); + factory->Release();DestroyWindow(window); + std::printf("measurement backend checks=%u failures=%u\n",checks,failures); + return failures?1:0; +} diff --git a/tests/measurement-backend-test.ps1 b/tests/measurement-backend-test.ps1 new file mode 100644 index 0000000..4e741a1 --- /dev/null +++ b/tests/measurement-backend-test.ps1 @@ -0,0 +1,27 @@ +param([string]$Backend, [string]$OutputDirectory, [switch]$CompileOnly, [switch]$DisableCompat, [switch]$RenderHud, + [string]$LlvmMingwBin) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if (-not $Backend) { $Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll' } +if (-not $OutputDirectory) { $OutputDirectory=Join-Path $root 'out/perf/20261027-unified-measurement/backend' } +New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$exe=Join-Path $OutputDirectory 'measurement-backend-probe.exe' +& $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'measurement-backend-probe.cpp') '-I' (Join-Path $root 'backend/dxvk/include/vulkan/include') '-o' $exe '-luuid' '-luser32' '-lgdi32' +if ($LASTEXITCODE -ne 0) { throw "Probe compile failed: $LASTEXITCODE" } +if ($CompileOnly) { exit 0 } +$backendPath=(Resolve-Path -LiteralPath $Backend).Path +$enabled=if ($DisableCompat) { 'False' } else { 'True' } +$env:DXVK_CONFIG="d3d9.gtaSaCompat = $enabled; d3d9.gtaSaCompatDiagnostics = False; d3d9.gtaSaResourceBindingCache = True; dxvk.hud = renderstack,fps,frametimes" +$env:DXVK_HUD='renderstack,fps,frametimes' +$env:DXVK_LOG_PATH=$OutputDirectory +Push-Location $OutputDirectory +try { + if ($DisableCompat) { & $exe $backendPath --disabled } + elseif ($RenderHud) { & $exe $backendPath --render } + else { & $exe $backendPath } + $result=$LASTEXITCODE +} finally { Pop-Location } +exit $result diff --git a/tests/measurement-runtime-benchmark.ps1 b/tests/measurement-runtime-benchmark.ps1 new file mode 100644 index 0000000..d81a46c --- /dev/null +++ b/tests/measurement-runtime-benchmark.ps1 @@ -0,0 +1,125 @@ +param( + [Parameter(Mandatory)] [string]$Backend, + [ValidateRange(1, 64)] [int]$Processes = 6, + [ValidateRange(4, 64)] [int]$Rounds = 4, + [ValidateRange(32, 4096)] [int]$Iterations = 256, + [string]$EvidenceName = 'runtime-overhead', + [switch]$SkipActiveCapture, + [switch]$NoBuild, + [string]$MSBuild, + [switch]$AllowNonV18MsBuild +) + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +if (-not $NoBuild) { + $buildParameters = @{ BuildOnly = $true; Backend = $Backend; EvidenceName = 'benchmark-prepare' } + if ($MSBuild) { $buildParameters.MSBuild = $MSBuild } + if ($AllowNonV18MsBuild) { $buildParameters.AllowNonV18MsBuild = $true } + & (Join-Path $PSScriptRoot 'measurement-runtime-test.ps1') @buildParameters +} +$backendItem = Get-Item -LiteralPath $Backend +$probeItem = Get-Item -LiteralPath (Join-Path $root 'out/perf/20261027-unified-measurement/runtime/bin/MeasurementRuntimeTests.exe') +$backendHash = (Get-FileHash -LiteralPath $backendItem.FullName -Algorithm SHA256).Hash +$probeHash = (Get-FileHash -LiteralPath $probeItem.FullName -Algorithm SHA256).Hash +$stamp = [DateTime]::UtcNow.ToString('yyyyMMddTHHmmssfff') +$out = Join-Path $root "out/perf/20261027-unified-measurement/runtime/benchmarks/$EvidenceName-$stamp-$PID" +New-Item -ItemType Directory -Path $out -Force | Out-Null +$utf8 = [Text.UTF8Encoding]::new($false) +$metadata = [ordered]@{ + backend = $backendItem.FullName; backendSha256 = $backendHash; backendSize = $backendItem.Length + probe = $probeItem.FullName; probeSha256 = $probeHash; probeSize = $probeItem.Length + processes = $Processes; rounds = $Rounds; iterations = $Iterations; warmupPerVariant = 64 + hud = 'renderstack,fps,frametimes'; present = 'immediate'; gpuFence = 'outside-timed-region' + backBufferWidth = 640; backBufferHeight = 480; viewportCheck = 'native GetBackBuffer/GetDesc must match before timing' + window = 'visible, shown without activation; messages pumped outside timed variants'; visibleHudLines = 8 + measurement = 'wall-clear-draw-present-runtime'; units = 'nanoseconds-per-successful-Present' + variants = @{ A = 'runtime disabled'; B = 'runtime enabled, idle, RenderStack HUD hidden'; C = 'runtime enabled, idle, RenderStack HUD visible' } + ordering = 'AB/BA and AC/CA alternate by round within each independent process' + activeCapture = 'separate observer-stage illustration in the first process; not a matched A/B' +} +[IO.File]::WriteAllText((Join-Path $out 'metadata.json'), ($metadata | ConvertTo-Json -Depth 5), $utf8) +$rows = [Collections.Generic.List[object]]::new() +for ($index = 0; $index -lt $Processes; ++$index) { + if ((Get-FileHash -LiteralPath $backendItem.FullName -Algorithm SHA256).Hash -cne $backendHash -or + (Get-FileHash -LiteralPath $probeItem.FullName -Algorithm SHA256).Hash -cne $probeHash) { + throw 'Backend or probe identity changed between independent benchmark processes' + } + $directory = Join-Path $out "process-$index" + New-Item -ItemType Directory -Path $directory -Force | Out-Null + $start = [Diagnostics.ProcessStartInfo]::new() + $start.FileName = $probeItem.FullName + foreach ($argument in @('--benchmark', $backendItem.FullName, [string]$Rounds, [string]$Iterations)) { + [void]$start.ArgumentList.Add($argument) + } + $includeActive = $index -eq 0 -and -not $SkipActiveCapture + if ($includeActive) { [void]$start.ArgumentList.Add('--active') } + $start.WorkingDirectory = $directory + $start.UseShellExecute = $false + $start.CreateNoWindow = $true + $start.RedirectStandardOutput = $true + $start.RedirectStandardError = $true + $start.Environment['DXVK_HUD'] = 'renderstack,fps,frametimes' + $start.Environment['DXVK_LOG_PATH'] = $directory + $start.Environment['MEASUREMENT_BENCH_BACKEND_SHA256'] = $backendHash + $start.Environment['MEASUREMENT_BENCH_PROBE_SHA256'] = $probeHash + $process = [Diagnostics.Process]::new() + $process.StartInfo = $start + Write-Output "Benchmark process $($index + 1)/${Processes}: $Rounds alternating rounds, $Iterations Presents per variant" + [void]$process.Start() + $stdout = $process.StandardOutput.ReadToEndAsync() + $stderr = $process.StandardError.ReadToEndAsync() + if (-not $process.WaitForExit(120000)) { + $process.Kill($true) + $process.WaitForExit() + throw "Benchmark process $index exceeded 120 seconds" + } + $outputText = $stdout.GetAwaiter().GetResult() + $errorText = $stderr.GetAwaiter().GetResult() + [IO.File]::WriteAllText((Join-Path $directory 'stdout.log'), $outputText, $utf8) + [IO.File]::WriteAllText((Join-Path $directory 'stderr.log'), $errorText, $utf8) + if ($process.ExitCode -ne 0) { throw "Benchmark process $index failed: $($process.ExitCode); see $directory" } + $processRows = @($outputText -split '\r?\n' | Where-Object { $_.StartsWith('{') } | ForEach-Object { $_ | ConvertFrom-Json }) + $idleRows = @($processRows | Where-Object type -eq 'idle-round') + if ($idleRows.Count -ne $Rounds * 4) { throw "Expected $($Rounds * 4) idle rows, found $($idleRows.Count) in $directory" } + foreach ($row in $processRows) { + $row | Add-Member -NotePropertyName processIndex -NotePropertyValue $index + if ($row.type -eq 'idle-round' -and ($row.failedPresents -ne 0 -or $row.workloadFailures -ne 0 -or + $row.successfulPresents -ne $Iterations -or -not $row.idle -or -not $row.gpuCompleted -or + $row.backendSha256 -cne $backendHash -or $row.probeSha256 -cne $probeHash)) { + throw "Invalid timed row in $directory" + } + $rows.Add($row) + [IO.File]::AppendAllText((Join-Path $out 'per-round.jsonl'), (($row | ConvertTo-Json -Compress -Depth 5) + "`n"), $utf8) + } + $activeRows = @($processRows | Where-Object type -eq 'active-observer-stage') + if ($activeRows.Count -ne $(if ($includeActive) { 3 } else { 0 })) { + throw "Unexpected active observer-stage row count in $directory" + } + $process.Dispose() +} +$paired = @($rows | Where-Object type -eq 'idle-round' | Group-Object processIndex, round, pair | ForEach-Object { + $group = $_.Group + $baseline = @($group | Where-Object variant -eq 'A') + $observed = @($group | Where-Object variant -ne 'A') + if ($baseline.Count -ne 1 -or $observed.Count -ne 1) { throw "Unpaired benchmark rows: $($_.Name)" } + [ordered]@{ processIndex = $baseline[0].processIndex; pid = $baseline[0].pid; round = $baseline[0].round + pair = $baseline[0].pair; order = $baseline[0].order; variant = $observed[0].variant + disabledNsPerPresent = $baseline[0].wallNsPerSuccessfulPresent + observedNsPerPresent = $observed[0].wallNsPerSuccessfulPresent + deltaNsPerPresent = $observed[0].wallNsPerSuccessfulPresent - $baseline[0].wallNsPerSuccessfulPresent + ratio = $observed[0].wallNsPerSuccessfulPresent / $baseline[0].wallNsPerSuccessfulPresent } +}) +[IO.File]::WriteAllText((Join-Path $out 'paired.json'), (ConvertTo-Json -InputObject @($paired) -Depth 4), $utf8) +$overlay = @($paired | Group-Object processIndex, round | ForEach-Object { + $hidden = @($_.Group | Where-Object pair -eq 'AB')[0] + $visible = @($_.Group | Where-Object pair -eq 'AC')[0] + [ordered]@{ processIndex = $hidden.processIndex; pid = $hidden.pid; round = $hidden.round + hiddenNsPerPresent = $hidden.observedNsPerPresent; visibleNsPerPresent = $visible.observedNsPerPresent + visibleMinusHiddenNsPerPresent = $visible.observedNsPerPresent - $hidden.observedNsPerPresent + differenceOfPairedDeltasNsPerPresent = $visible.deltaNsPerPresent - $hidden.deltaNsPerPresent + visibleToHiddenRatio = $visible.observedNsPerPresent / $hidden.observedNsPerPresent } +}) +[IO.File]::WriteAllText((Join-Path $out 'overlay-paired.json'), (ConvertTo-Json -InputObject @($overlay) -Depth 4), $utf8) +Write-Output "Benchmark artifacts: $out" diff --git a/tests/measurement-runtime-test.ps1 b/tests/measurement-runtime-test.ps1 new file mode 100644 index 0000000..6f11042 --- /dev/null +++ b/tests/measurement-runtime-test.ps1 @@ -0,0 +1,50 @@ +param([string]$EvidenceName = 'green', [string]$Backend, [switch]$BuildOnly, + [string]$MSBuild, [switch]$AllowNonV18MsBuild, [string]$RuntimeSource, + [switch]$PendingStopOnly) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +$out = Join-Path $root 'out/perf/20261027-unified-measurement/runtime' +New-Item -ItemType Directory -Path (Join-Path $out 'scripts') -Force | Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$msBuildInfo = Find-RenderStackMSBuild -MsBuildPath $MSBuild -RepoRoot $root -AllowNonV18MsBuild:$AllowNonV18MsBuild +$include = Join-Path $root 'out/deps/Microsoft.DXSDK.D3DX/9.29.952.8/package/build/native/include' +$buildArguments = @((Join-Path $PSScriptRoot 'measurement-runtime-tests.vcxproj'), '/nologo', '/m:1', '/nr:false', '/t:Build', + '/p:Configuration=Release', '/p:Platform=Win32', '/p:PreferredToolArchitecture=x64', "/p:D3DX9IncludeDir=$include") +if ($msBuildInfo.ProductMajor -lt 18) { $buildArguments += '/p:PlatformToolset=v143' } +$testExe = Join-Path $out 'bin/MeasurementRuntimeTests.exe' +if ($RuntimeSource) { + $sourceItem = Get-Item -LiteralPath $RuntimeSource + $mutationRoot = Join-Path $out "mutations/$EvidenceName" + $buildArguments += @("/p:RuntimeSource=$($sourceItem.FullName)", + "/p:OutDir=$mutationRoot/bin/", "/p:IntDir=$mutationRoot/obj/") + $testExe = Join-Path $mutationRoot 'bin/MeasurementRuntimeTests.exe' +} +& $msBuildInfo.Path @buildArguments 2>&1 | + Tee-Object -FilePath (Join-Path $out "$EvidenceName-build.log") +if ($LASTEXITCODE -ne 0) { throw "measurement runtime build failed: $LASTEXITCODE" } +$inputs = @() +if ($RuntimeSource) { + $inputs += [ordered]@{ path = $sourceItem.FullName; size = $sourceItem.Length; + sha256 = (Get-FileHash -LiteralPath $sourceItem.FullName -Algorithm SHA256).Hash } +} +if ($Backend) { + $item = Get-Item -LiteralPath $Backend + $inputs += [ordered]@{ path = $item.FullName; size = $item.Length; + sha256 = (Get-FileHash -LiteralPath $item.FullName -Algorithm SHA256).Hash; + lastWriteTimeUtc = $item.LastWriteTimeUtc.ToString('o') } +} +[IO.File]::WriteAllText((Join-Path $out "$EvidenceName-inputs.json"), + (ConvertTo-Json -InputObject @($inputs) -Depth 3), [Text.UTF8Encoding]::new($false)) +if ($BuildOnly) { return } +Push-Location $out +try { + $testArguments = @() + if ($PendingStopOnly) { $testArguments += '--pending-stop-only' } + if ($Backend) { $testArguments += $Backend } + & $testExe @testArguments 2>&1 | + Tee-Object -FilePath (Join-Path $out "$EvidenceName-test.log") + $result = $LASTEXITCODE +} finally { Pop-Location } +if ($result -ne 0) { throw "measurement runtime tests failed: $result" } diff --git a/tests/measurement-runtime-tests.cpp b/tests/measurement-runtime-tests.cpp new file mode 100644 index 0000000..6ef2b2d --- /dev/null +++ b/tests/measurement-runtime-tests.cpp @@ -0,0 +1,815 @@ +#include "../src/bridge/legacy/EffectInspector.cpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __has_include("../src/bridge/legacy/BridgeMeasurementRuntime.h") +#include "../src/bridge/legacy/BridgeMeasurementRuntime.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" +#define HAS_MEASUREMENT_RUNTIME 1 +#endif + +namespace { +unsigned g_checks = 0, g_failures = 0; +void Check(bool value, const char* message) +{ + ++g_checks; + std::printf("%s: %s\n", value ? "PASS" : "FAIL", message); + if (!value) ++g_failures; + std::fflush(stdout); +} + +#if defined(HAS_MEASUREMENT_RUNTIME) +using namespace BridgeMeasurement; +std::atomic g_f7{false}, g_f8{false}; +bool g_failWorker = false; +bool g_delayReportWorker = false; +HANDLE g_reportWorkerStarted = nullptr, g_releaseReportWorker = nullptr; +std::atomic g_presentFrame{0}; +std::vector g_workers; + +bool TestKeyDown(int key) { return key == VK_F7 ? g_f7.load() : key == VK_F8 && g_f8.load(); } +bool TestFrame(std::uint32_t& frame) { frame = g_presentFrame.load(std::memory_order_relaxed); return true; } +void TestLog(const char* text) { std::printf("runtime: %s\n", text ? text : ""); } +struct DelayedWorker { LPTHREAD_START_ROUTINE start; void* context; }; +DWORD WINAPI StartDelayedWorker(void* context) +{ + std::unique_ptr worker(static_cast(context)); + SetEvent(g_reportWorkerStarted); + WaitForSingleObject(g_releaseReportWorker, INFINITE); + return worker->start(worker->context); +} +HANDLE StartTestWorker(LPTHREAD_START_ROUTINE start, void* context) +{ + if (g_failWorker) return nullptr; + const bool delay = g_delayReportWorker && g_workers.size() == 1; + auto* delayed = delay ? new DelayedWorker{start, context} : nullptr; + HANDLE handle = CreateThread(nullptr, 0, delay ? StartDelayedWorker : start, + delay ? delayed : context, 0, nullptr); + if (!handle) delete delayed; + if (handle) g_workers.push_back(handle); + return handle; +} + +LRESULT CALLBACK FixtureWindowProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) +{ + return DefWindowProcA(window, message, wparam, lparam); +} + +struct DeviceFixture { + HMODULE module = nullptr; + IDirect3D9* d3d = nullptr; + IDirect3DDevice9* device = nullptr; + HWND window = nullptr; + D3DPRESENT_PARAMETERS parameters{}; + bool backend = false; + std::string libraryPath; + + explicit DeviceFixture(const char* backendPath, UINT width = 64, UINT height = 64, bool visible = false) + { + backend = backendPath != nullptr; + std::string path; + if (backendPath) { + path = backendPath; + SetEnvironmentVariableA("DXVK_CONFIG", "d3d9.gtaSaCompat = True; d3d9.gtaSaCompatDiagnostics = True"); + } else { + char systemDirectory[MAX_PATH]{}; + GetSystemDirectoryA(systemDirectory, MAX_PATH); + path = std::string(systemDirectory) + "\\d3d9.dll"; + } + libraryPath = path; + module = LoadLibraryExA(path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); + using Create = IDirect3D9* (WINAPI*)(UINT); + const auto create = module ? reinterpret_cast(GetProcAddress(module, "Direct3DCreate9")) : nullptr; + d3d = create ? create(D3D_SDK_VERSION) : nullptr; + if (!d3d) return; + WNDCLASSA wc{}; + wc.lpfnWndProc = FixtureWindowProc; + wc.hInstance = GetModuleHandleA(nullptr); + wc.lpszClassName = "MeasurementRuntimeProductionTest"; + RegisterClassA(&wc); + RECT windowRect{0, 0, static_cast(width), static_cast(height)}; + AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE); + window = CreateWindowA(wc.lpszClassName, "runtime fixture", WS_OVERLAPPEDWINDOW, + 0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, + nullptr, nullptr, wc.hInstance, nullptr); + if (visible) { + ShowWindow(window, SW_SHOWNOACTIVATE); + UpdateWindow(window); + } + parameters.Windowed = TRUE; + parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; + parameters.hDeviceWindow = window; + parameters.BackBufferWidth = width; + parameters.BackBufferHeight = height; + parameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, + D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, ¶meters, &device); + } + ~DeviceFixture() + { + if (device) device->Release(); + if (d3d) d3d->Release(); + if (module) FreeLibrary(module); + if (window) DestroyWindow(window); + } + HRESULT Present() + { + const HRESULT result = device->Present(nullptr, nullptr, nullptr, nullptr); + ++g_presentFrame; + RuntimePresent(device, result); + return result; + } +}; + +struct RuntimeFixture { + HANDLE stopEvent = nullptr; + std::string directory; + RuntimeConfig config{}; + bool stopped = false; + + explicit RuntimeFixture(const char* caseName) + { + RuntimeShutdown(); + g_f7 = g_f8 = g_failWorker = false; + g_delayReportWorker = false; + g_workers.clear(); + g_presentFrame = 0; + directory = (std::filesystem::current_path() / "cases" / + (std::string(caseName) + "-" + std::to_string(GetCurrentProcessId()))).string(); + std::filesystem::create_directories(std::filesystem::path(directory) / "scripts"); + stopEvent = CreateEventA(nullptr, TRUE, FALSE, nullptr); + config.enabled = config.hud = config.journal = config.cpu = true; + config.timing = {10, 60, 20, 50}; + config.intervalMs = 1; + config.gameDirectory = directory.c_str(); + config.stopEvent = stopEvent; + config.startWorker = StartTestWorker; + config.keyDown = TestKeyDown; + config.log = TestLog; + config.readFrame = TestFrame; + } + void Configure() { ConfigureRuntime(config); } + void Stop() + { + if (stopped) return; + if (g_releaseReportWorker) SetEvent(g_releaseReportWorker); + RuntimeShutdown(); + SetEvent(stopEvent); + for (HANDLE worker : g_workers) { + Check(WaitForSingleObject(worker, 10000) == WAIT_OBJECT_0, + "provided worker joins before reconfiguration or event destruction"); + CloseHandle(worker); + } + g_workers.clear(); + if (g_reportWorkerStarted) CloseHandle(g_reportWorkerStarted); + if (g_releaseReportWorker) CloseHandle(g_releaseReportWorker); + g_reportWorkerStarted = g_releaseReportWorker = nullptr; + g_delayReportWorker = false; + stopped = true; + } + ~RuntimeFixture() { Stop(); CloseHandle(stopEvent); } +}; + +RuntimeSnapshot Snapshot() +{ + RuntimeSnapshot snapshot{}; + RuntimeGetSnapshot(&snapshot); + return snapshot; +} + +bool WaitPhase(DeviceFixture& device, Phase phase, DWORD maximumMs = 8000) +{ + const ULONGLONG deadline = GetTickCount64() + maximumMs; + do { + device.Present(); + if (Snapshot().phase == phase) return true; + Sleep(1); + } while (GetTickCount64() < deadline); + return false; +} + +void TestDisabled(DeviceFixture& device) +{ + RuntimeFixture runtime(device.backend ? "dxvk-disabled" : "system-disabled"); + runtime.config.enabled = false; + runtime.Configure(); + g_f7 = g_f8 = true; + for (unsigned i = 0; i < 4; ++i) device.Present(); + const auto snapshot = Snapshot(); + Check(!RuntimeEnabled() && snapshot.phase == Phase::Ready && + snapshot.captureId == 0 && !snapshot.workerBusy && g_workers.empty(), + "disabled runtime admits no capture and starts no sampling worker"); +} + +bool RecordJournal(ProperShadersStateJournal* journal) +{ + HRESULT result = journal->BeginTransaction(GetCurrentThreadId(), 0); + journal->SetProbeTechnique("RuntimeDuplicateChanged"); + journal->SetCurrentPass(3); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + return SUCCEEDED(journal->Restore()) && SUCCEEDED(result); +} + +template +bool HasMeasuredCoverage(const Counter& counter) +{ + if constexpr (requires { counter.TimedCalls; }) return counter.TimedCalls > 0; + else return false; +} + +void TestComplete(DeviceFixture& device) +{ + RuntimeFixture runtime(device.backend ? "dxvk-complete" : "system-complete"); + g_reportWorkerStarted = CreateEventA(nullptr, TRUE, FALSE, nullptr); + g_releaseReportWorker = CreateEventA(nullptr, TRUE, FALSE, nullptr); + g_delayReportWorker = true; + runtime.Configure(); + Check(RuntimeEnabled() && !RuntimeGetSnapshot(nullptr), + "enabled runtime exposes snapshots and rejects null output"); + device.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); + auto* journal = new ProperShadersStateJournal(device.device); + Check(journal->SupportsNativeStateJournal() == device.backend, + "runtime fixture uses the expected real native or local journal path"); + IDirect3DStateBlock9* stateBlock = nullptr; + Check(SUCCEEDED(device.device->CreateStateBlock(D3DSBT_ALL, &stateBlock)) && stateBlock, + "real state block creates for inclusive capture and apply timing"); + ID3D9BridgeDiagnostics* diagnostics = nullptr; + const HRESULT query = device.device->QueryInterface(IID_D3D9BridgeDiagnostics, + reinterpret_cast(&diagnostics)); + g_f8 = true; + device.Present(); + auto snapshot = Snapshot(); + const std::uint64_t capture = snapshot.captureId; + Check(capture != 0 && snapshot.phase == Phase::Baseline, + "injected F8 edge starts a baseline capture at a real Present boundary"); + g_f7 = true; + std::vector phases{snapshot.phase}; + unsigned journalTransactions = 0; + bool journalOk = true, stableIdentity = true; + bool observerStagesSeparated = g_workers.empty(); + const ULONGLONG deadline = GetTickCount64() + 10000; + while (snapshot.phase != Phase::Complete && snapshot.phase != Phase::Error && + GetTickCount64() < deadline) { + if (snapshot.phase == Phase::Baseline || snapshot.phase == Phase::Cpu || + snapshot.phase == Phase::CpuFinishing) { + observerStagesSeparated &= !g_properShadersStateAttributionActive.load(std::memory_order_acquire); + if (snapshot.phase == Phase::Baseline) observerStagesSeparated &= g_workers.empty(); + if (diagnostics) { + D3D9BridgeAuditSnapshot audit{}; + audit.StructSize = sizeof(audit); + audit.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + observerStagesSeparated &= SUCCEEDED(diagnostics->GetAudit(&audit)) && !audit.Active; + } + } + if (snapshot.phase == Phase::Attribution) { + journalOk &= RecordJournal(journal); + if (stateBlock) journalOk &= SUCCEEDED(stateBlock->Capture()) && SUCCEEDED(stateBlock->Apply()); + ++journalTransactions; + } + device.Present(); + snapshot = Snapshot(); + stableIdentity &= snapshot.captureId == capture; + if (phases.back() != snapshot.phase) phases.push_back(snapshot.phase); + if (snapshot.phase == Phase::AttributionFinishing && + WaitForSingleObject(g_reportWorkerStarted, 0) == WAIT_OBJECT_0) break; + Sleep(1); + } + const bool reportPaused = WaitForSingleObject(g_reportWorkerStarted, 2000) == WAIT_OBJECT_0; + g_f7 = g_f8 = false; + device.Present(); + g_f7 = true; + device.Present(); + Check(reportPaused && Snapshot().workerBusy && Snapshot().captureId == capture && + (Snapshot().phase == Phase::Complete || Snapshot().phase == Phase::AttributionFinishing), + "fresh key edges while the actual report worker is busy stay suppressed"); + SetEvent(g_releaseReportWorker); + g_delayReportWorker = false; + const ULONGLONG reportDeadline = GetTickCount64() + 10000; + while ((snapshot.workerBusy || snapshot.phase == Phase::AttributionFinishing) && + GetTickCount64() < reportDeadline) { + device.Present(); + Sleep(1); + snapshot = Snapshot(); + if (phases.back() != snapshot.phase) phases.push_back(snapshot.phase); + } + std::vector withoutFinishing; + for (const auto phase : phases) + if (phase != Phase::CpuFinishing && phase != Phase::AttributionFinishing) withoutFinishing.push_back(phase); + const std::vector expected{Phase::Baseline, Phase::Cpu, Phase::Attribution, Phase::Complete}; + Check(snapshot.phase == Phase::Complete && withoutFinishing == expected, + "production runtime orders baseline, CPU sampling and state attribution through completion"); + Check(!snapshot.workerBusy, "completed report worker finishes before report inspection"); + device.Present(); + Check(Snapshot().captureId == capture && Snapshot().phase == Phase::Complete, + "a key held across report finalization does not start a delayed capture"); + Check(observerStagesSeparated, + "baseline runs without sampling and baseline/CPU phases leave journal and backend attribution inactive"); + Check(stableIdentity && snapshot.cpuSamples > 0 && snapshot.suspendedNs > 0 && + snapshot.phaseStats[0].presents > 0 && snapshot.phaseStats[1].presents > 0 && + snapshot.phaseStats[2].presents > 0, + "one capture retains identity and records actual CPU samples and phase Present counts"); + JournalAttributionSummary summary{}; + JournalAttributionGetSummary(&summary); + DWORD cull = 0; + device.device->GetRenderState(D3DRS_CULLMODE, &cull); + Check(journalOk && journalTransactions > 0 && summary.available && + summary.captureId == capture && summary.transactions == journalTransactions && + summary.records == std::uint64_t(journalTransactions) * 3 && + summary.restoreCount == journalTransactions && summary.restoreQpc > 0 && + summary.topTechniqueCount > 0 && + std::strcmp(summary.topTechniques[0].name, "RuntimeDuplicateChanged") == 0 && + cull == D3DCULL_CCW, + "runtime attributes real named duplicate and changed journal writes without changing restore state"); + Check(snapshot.reportPath[0] && std::filesystem::is_regular_file(snapshot.reportPath), + "completed production runtime writes an inspectable combined report"); + if (snapshot.reportPath[0] && std::filesystem::is_regular_file(snapshot.reportPath)) { + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(report.find("threadActivity phase=baseline available=" + std::string(device.backend ? "1" : "0")) != std::string::npos && + report.find("threadActivity phase=cpu available=" + std::string(device.backend ? "1" : "0")) != std::string::npos && + report.find("threadActivity phase=attribution available=" + std::string(device.backend ? "1" : "0")) != std::string::npos && + report.find("non-CPU-wall-is-not-proven-idle") != std::string::npos, + "combined report includes real per-phase main/CS activity or explicit unavailable status"); + Check(!report.empty() && + report.find("captureId=" + std::to_string(capture)) != std::string::npos && + report.find("phase=baseline") != std::string::npos && + report.find("phase=cpu") != std::string::npos && + report.find("phase=attribution") != std::string::npos && + report.find("sample ") != std::string::npos && + report.find("frame ") != std::string::npos && + (!device.backend || report.find("backendCounter kind=") != std::string::npos) && + report.find("journalTechnique name=RuntimeDuplicateChanged") != std::string::npos && + report.find("technique=RuntimeDuplicateChanged") != std::string::npos && + report.find("pass=pass index=3") != std::string::npos && + report.find("op=RS count=") != std::string::npos, + "combined report links capture identity, phases, raw samples/frames and named attribution"); + } + if (device.backend) { + Check(SUCCEEDED(query) && diagnostics, "backend provides the actual private diagnostics interface"); + if (diagnostics) { + D3D9BridgeAuditSnapshot audit{}; + audit.StructSize = sizeof(audit); + audit.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + Check(SUCCEEDED(diagnostics->GetAudit(&audit)) && audit.CaptureId == capture && + audit.Status == D3D9_BRIDGE_AUDIT_COMPLETE && audit.Available && !audit.Active && + audit.Total.Calls > 0 && audit.Total.RedundantCalls > 0 && + audit.ObservedPresentBoundaries > 0, + "completed runtime links measured backend calls and redundant writes to capture identity"); + Check(HasMeasuredCoverage(audit.Total) && HasMeasuredCoverage(audit.Counters[0]) && + audit.Total.QpcTicks > 0 && audit.Total.MaxQpcTicks > 0 && + audit.Counters[15].Calls >= journalTransactions && audit.Counters[15].QpcTicks > 0 && + audit.Counters[16].Calls >= journalTransactions && audit.Counters[16].QpcTicks > 0, + "backend reports timing coverage and positive setter/state-block inclusive QPC observations"); + D3D9BridgeHudSnapshot hud{}; + hud.StructSize = sizeof(hud); + hud.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + Check(SUCCEEDED(diagnostics->GetHud(&hud)) && hud.CaptureId == capture && + hud.Phase == static_cast(Phase::Complete) && hud.Visible && hud.LineCount > 0, + "completed runtime publishes the copied HUD capture and phase"); + diagnostics->Release(); + } + } else { + Check(FAILED(query) && !diagnostics && + (snapshot.unavailableMask & RuntimeBackendUnavailable) != 0 && + (snapshot.unavailableMask & RuntimeJournalUnavailable) == 0, + "system D3D9 completes with explicit unavailable backend integration"); + } + if (stateBlock) stateBlock->Release(); + journal->Release(); + for (unsigned i = 0; i < 4; ++i) device.Present(); + Check(Snapshot().captureId == capture && Snapshot().phase == Phase::Complete, + "held F7 and F8 never restart a completed capture"); + g_f7 = g_f8 = false; + device.Present(); + g_f7 = true; + device.Present(); + Check(Snapshot().captureId != capture && Snapshot().phase == Phase::Baseline, + "released keys followed by an F7 edge start the same workflow"); + RuntimeInterrupt(device.device, "alias fixture cleanup"); + runtime.Stop(); +} + +void TestWorkerFailure(DeviceFixture& device) +{ + RuntimeFixture runtime(device.backend ? "dxvk-worker-failure" : "system-worker-failure"); + runtime.Configure(); + g_failWorker = true; + g_f8 = true; + Check(WaitPhase(device, Phase::Error), "worker creation failure reaches Error"); + const auto snapshot = Snapshot(); + Check(snapshot.lastError[0] && snapshot.cpuSamples == 0 && !snapshot.workerBusy && + snapshot.phaseStats[2].presents == 0, + "failed worker is explicit and never reports successful sampling or attribution"); +} + +void TestInterruption(DeviceFixture& device, bool release) +{ + RuntimeFixture runtime(release ? (device.backend ? "dxvk-release" : "system-release") : + (device.backend ? "dxvk-reset" : "system-reset")); + runtime.config.timing.cpuMs = 5000; + runtime.Configure(); + g_f8 = true; + const bool sampling = WaitPhase(device, Phase::Cpu); + const auto before = Snapshot(); + Check(sampling && before.workerBusy, "lifecycle fixture reaches an in-flight sampling worker"); + RuntimeInterrupt(device.device, release ? "Release" : "Reset", release); + const auto after = Snapshot(); + Check(after.phase == Phase::Interrupted && after.captureId == before.captureId && + after.phaseStats[0].presents > 0 && after.phaseStats[2].presents == 0, + release ? "Release interruption preserves baseline and does not invent completed attribution" : + "Reset interruption preserves baseline and does not invent completed attribution"); + if (release) { + Check(device.device->Release() == 0, "interrupted device reaches final release"); + device.device = nullptr; + } else { + Check(SUCCEEDED(device.device->Reset(&device.parameters)), + "real D3D9 Reset succeeds after runtime interruption"); + } + runtime.Stop(); +} + +void TestShutdown(DeviceFixture& device) +{ + RuntimeFixture runtime(device.backend ? "dxvk-shutdown" : "system-shutdown"); + runtime.config.timing.cpuMs = 5000; + runtime.Configure(); + g_f8 = true; + Check(WaitPhase(device, Phase::Cpu) && Snapshot().workerBusy, + "shutdown fixture reaches in-flight sampling"); + RuntimeShutdown(); + Check(Snapshot().phase == Phase::Interrupted, + "shutdown terminates an active capture as interrupted"); + runtime.Stop(); +} + +void TestAdmittedJournalInterruption(DeviceFixture& device, bool lateRestore, bool naturalStop = false) +{ + const std::string name = std::string(device.backend ? "dxvk-admitted-reset" : "system-admitted-reset") + + (naturalStop ? "-natural-late" : (lateRestore ? "-late" : "-prompt")); + RuntimeFixture runtime(name.c_str()); + runtime.Configure(); + HANDLE returned = CreateEventA(nullptr, TRUE, FALSE, nullptr); + HANDLE allowRestore = CreateEventA(nullptr, TRUE, FALSE, nullptr); + HANDLE restored = CreateEventA(nullptr, TRUE, FALSE, nullptr); + HANDLE requestPresent = CreateEventA(nullptr, FALSE, FALSE, nullptr); + HANDLE presented = CreateEventA(nullptr, FALSE, FALSE, nullptr); + HANDLE destroyDevice = CreateEventA(nullptr, TRUE, FALSE, nullptr); + HANDLE done = CreateEventA(nullptr, TRUE, FALSE, nullptr); + bool reachedAttribution = false, journalOk = false, resetOk = false, restoredOriginal = false; + std::uint64_t capture = 0; + g_f8 = true; + std::thread watchdog([&] { + if (WaitForSingleObject(done, 20000) != WAIT_OBJECT_0) { + std::fputs("FAIL: admitted Runtime interruption watchdog expired (owner/drain deadlock)\n", stderr); + std::fflush(stderr); + TerminateProcess(GetCurrentProcess(), 86); + } + }); + std::thread owner([&] { + // D3D9 Reset must run on the device's creating thread. This actor owns + // both creation and Reset, and retains the device through host shutdown. + { + DeviceFixture ownedDevice(device.backend ? device.libraryPath.c_str() : nullptr); + if (ownedDevice.device) { + ownedDevice.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); + reachedAttribution = WaitPhase(ownedDevice, Phase::Attribution); + capture = Snapshot().captureId; + auto* journal = new ProperShadersStateJournal(ownedDevice.device); + HRESULT result = reachedAttribution ? journal->BeginTransaction(GetCurrentThreadId(), 0) : E_FAIL; + journal->SetProbeTechnique("AdmittedAcrossReset"); + journal->SetCurrentPass(5); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (SUCCEEDED(result)) result = journal->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + if (naturalStop) { + const auto boundaryDeadline = GetTickCount64() + 2000; + while (Snapshot().phase == Phase::Attribution && GetTickCount64() < boundaryDeadline) { + ownedDevice.Present(); Sleep(1); + } + } else RuntimeInterrupt(ownedDevice.device, "Reset with an admitted owner transaction"); + SetEvent(returned); + while (WaitForSingleObject(allowRestore, 0) != WAIT_OBJECT_0) { + if (WaitForSingleObject(requestPresent, 1) == WAIT_OBJECT_0) { + ownedDevice.Present(); SetEvent(presented); + } + } + journalOk = SUCCEEDED(result) && SUCCEEDED(journal->Restore()); + DWORD cull = 0; + restoredOriginal = SUCCEEDED(ownedDevice.device->GetRenderState(D3DRS_CULLMODE, &cull)) && cull == D3DCULL_CCW; + journal->Release(); + resetOk = SUCCEEDED(ownedDevice.device->Reset(&ownedDevice.parameters)); + SetEvent(restored); + while (WaitForSingleObject(destroyDevice, 0) != WAIT_OBJECT_0) { + if (WaitForSingleObject(requestPresent, 1) == WAIT_OBJECT_0) { + ownedDevice.Present(); SetEvent(presented); + } + } + } else { + SetEvent(returned); SetEvent(restored); + WaitForSingleObject(destroyDevice, INFINITE); + } + } + SetEvent(done); + }); + const auto requireEvent = [](HANDLE event, DWORD timeout, const char* failure) { + if (WaitForSingleObject(event, timeout) != WAIT_OBJECT_0) { + std::fprintf(stderr, "FAIL: %s\n", failure); + std::fflush(stderr); + TerminateProcess(GetCurrentProcess(), 86); + } + }; + const auto presentOnOwner = [&] { + SetEvent(requestPresent); + requireEvent(presented, 3000, "owner Present stalled while a journal transaction was pending"); + }; + requireEvent(returned, 10000, "RuntimeInterrupt did not return before the admitted owner Restore"); + const auto pending = Snapshot(); + const HANDLE reportWorker = g_workers.empty() ? nullptr : g_workers.back(); + JournalAttributionSummary pendingJournal{}; + JournalAttributionGetSummary(&pendingJournal); + Check(pending.phase == (naturalStop ? Phase::AttributionFinishing : Phase::Interrupted) && pending.workerBusy && + pendingJournal.inFlight == 1 && !pendingJournal.active, + "Runtime Reset interruption returns with closed admission and an admitted transaction still pending"); + if (lateRestore) { + const bool reportFinished = reportWorker && WaitForSingleObject(reportWorker, 5000) == WAIT_OBJECT_0; + const auto timedOut = Snapshot(); + JournalAttributionGetSummary(&pendingJournal); + Check(reportFinished && timedOut.phase == Phase::Interrupted && + timedOut.captureId == pending.captureId && pendingJournal.inFlight == 1, + "bounded report worker finishes while late journal remains pending and capture stays Interrupted"); + g_f7 = g_f8 = false; presentOnOwner(); + g_f7 = true; presentOnOwner(); + Check(Snapshot().captureId == pending.captureId && Snapshot().phase == Phase::Interrupted, + "a fresh capture edge is rejected after report timeout while old journal is still admitted"); + std::ifstream timeoutFile(timedOut.reportPath); + const std::string timeoutReport((std::istreambuf_iterator(timeoutFile)), {}); + Check(timeoutReport.find("status=interrupted") != std::string::npos && + timeoutReport.find("status=complete") == std::string::npos, + "bounded pending report explicitly retains interrupted status"); + } + SetEvent(allowRestore); + requireEvent(restored, 5000, "actual journal Restore/Reset did not finish after owner interruption returned"); + Check(reportWorker && WaitForSingleObject(reportWorker, 5000) == WAIT_OBJECT_0, + "actual report worker reaches completion after its bounded journal drain"); + presentOnOwner(); + const auto completed = Snapshot(); + JournalAttributionSummary finalJournal{}; + JournalAttributionGetSummary(&finalJournal); + Check(reachedAttribution && journalOk && restoredOriginal && resetOk && + !completed.workerBusy && completed.phase == Phase::Interrupted && + completed.captureId == capture && finalJournal.inFlight == 0 && finalJournal.captureId == capture && + finalJournal.transactions == 1 && finalJournal.records == 3 && finalJournal.restoreCount == 1, + "late real Restore and actual Reset leave the original capture Interrupted with its original identity"); + if (!lateRestore) { + std::ifstream file(completed.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(report.find("status=interrupted") != std::string::npos && + report.find("technique=AdmittedAcrossReset transactions=1 records=3") != std::string::npos && + report.find("pass=pass index=5 transactions=1 records=3") != std::string::npos, + "deferred interrupted report contains the transaction completed after admission closed"); + } else { + g_f7 = g_f8 = false; presentOnOwner(); + g_f8 = true; presentOnOwner(); + Check(Snapshot().captureId != capture && Snapshot().phase == Phase::Baseline, + "a new edge starts a fresh capture only after late Restore has drained"); + RuntimeInterrupt(nullptr, "late-restore fixture cleanup"); + } + runtime.Stop(); + SetEvent(destroyDevice); + owner.join(); watchdog.join(); + for (HANDLE event : {returned, allowRestore, restored, requestPresent, presented, destroyDevice, done}) CloseHandle(event); +} +struct BenchmarkVertex { float x, y, z, rhw; DWORD colour; }; +constexpr BenchmarkVertex BenchmarkTriangle[] = { + {3, 3, 0, 1, 0xff80c0ff}, {61, 3, 0, 1, 0xff80c0ff}, {3, 61, 0, 1, 0xff80c0ff} +}; + +bool BenchmarkWorkload(DeviceFixture& device, HRESULT& presented) +{ + HRESULT work = device.device->Clear(0, nullptr, D3DCLEAR_TARGET, 0xff102030, 1, 0); + const HRESULT begun = device.device->BeginScene(); + if (SUCCEEDED(begun)) { + const HRESULT drawn = device.device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, + BenchmarkTriangle, sizeof(BenchmarkVertex)); + if (FAILED(drawn)) work = drawn; + const HRESULT ended = device.device->EndScene(); + if (FAILED(ended)) work = ended; + } else work = begun; + presented = device.Present(); + return SUCCEEDED(work); +} + +bool BenchmarkFence(IDirect3DQuery9* fence) +{ + if (!fence || FAILED(fence->Issue(D3DISSUE_END))) return false; + const auto deadline = GetTickCount64() + 5000; + BOOL completed = FALSE; + HRESULT result = S_FALSE; + do { + result = fence->GetData(&completed, sizeof(completed), D3DGETDATA_FLUSH); + if (result == S_OK) return completed != FALSE; + if (FAILED(result)) return false; + Sleep(0); + } while (GetTickCount64() < deadline); + return false; +} + +std::string JsonString(std::string_view text) +{ + std::string result = "\""; + for (char value : text) { + if (value == '\\' || value == '"') result += '\\'; + if (value == '\n') result += "\\n"; + else if (value == '\r') result += "\\r"; + else result += value; + } + result += '"'; + return result; +} + +std::string BenchmarkHash(const char* variable) +{ + char hash[65]{}; + return GetEnvironmentVariableA(variable, hash, sizeof(hash)) == 64 ? hash : "unknown"; +} + +bool BenchmarkVariant(DeviceFixture& device, IDirect3DQuery9* fence, unsigned round, + const char* pair, const char* order, unsigned position, char variant, unsigned iterations) +{ + MSG message{}; + while (PeekMessageA(&message, device.window, 0, 0, PM_REMOVE)) { + TranslateMessage(&message); + DispatchMessageA(&message); + } + const std::string caseName = std::string("benchmark-idle-") + variant; + RuntimeFixture runtime(caseName.c_str()); + runtime.config.enabled = variant != 'A'; + runtime.config.hud = variant == 'C'; + runtime.Configure(); + RuntimePrepareDevice(device.device); + device.device->SetRenderState(D3DRS_LIGHTING, FALSE); + device.device->SetRenderState(D3DRS_ZENABLE, FALSE); + device.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + device.device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); + device.device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); + bool warmOk = true; + for (unsigned i = 0; i < 64; ++i) { + HRESULT result = E_FAIL; + warmOk &= BenchmarkWorkload(device, result) && result == S_OK; + } + if (!warmOk || !BenchmarkFence(fence)) { + Check(false, "benchmark workload warms and GPU completes outside timing"); + return false; + } + ID3D9BridgeDiagnostics* diagnostics = nullptr; + device.device->QueryInterface(IID_D3D9BridgeDiagnostics, reinterpret_cast(&diagnostics)); + D3D9BridgeHudSnapshot hud{}; + hud.StructSize = sizeof(hud); hud.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + const bool visibilityMatches = diagnostics && SUCCEEDED(diagnostics->GetHud(&hud)) && + (hud.Visible != 0) == (variant == 'C') && (variant != 'C' || hud.LineCount == 8); + if (diagnostics) diagnostics->Release(); + if (!visibilityMatches) { + Check(false, "benchmark variant has the expected actual copied HUD visibility"); + return false; + } + LARGE_INTEGER frequency{}, begin{}, end{}; + QueryPerformanceFrequency(&frequency); + std::uint64_t successful = 0, failed = 0, workloadFailed = 0; + QueryPerformanceCounter(&begin); + for (unsigned i = 0; i < iterations; ++i) { + HRESULT result = E_FAIL; + if (!BenchmarkWorkload(device, result)) ++workloadFailed; + if (result == S_OK) ++successful; + else ++failed; + } + QueryPerformanceCounter(&end); + const bool gpuCompleted = BenchmarkFence(fence); + const auto snapshot = Snapshot(); + const bool idle = snapshot.captureId == 0 && snapshot.phase == Phase::Ready && !snapshot.workerBusy; + const auto ticks = static_cast(end.QuadPart - begin.QuadPart); + const auto wallNs = static_cast( + static_cast(ticks) * 1'000'000'000.0L / frequency.QuadPart); + const auto backendHash = BenchmarkHash("MEASUREMENT_BENCH_BACKEND_SHA256"); + const auto probeHash = BenchmarkHash("MEASUREMENT_BENCH_PROBE_SHA256"); + std::printf("{\"type\":\"idle-round\",\"pid\":%lu,\"round\":%u,\"pair\":\"%s\",\"order\":\"%s\",\"position\":%u,\"variant\":\"%c\",\"warmup\":64,\"iterations\":%u,\"successfulPresents\":%llu,\"failedPresents\":%llu,\"workloadFailures\":%llu,\"qpcBegin\":%llu,\"qpcEnd\":%llu,\"qpcFrequency\":%llu,\"wallNs\":%llu,\"wallNsPerSuccessfulPresent\":%.9f,\"gpuFenceOutsideTiming\":true,\"gpuCompleted\":%s,\"idle\":%s,\"measurement\":\"wall-clear-draw-present-runtime\",\"backendSha256\":\"%s\",\"probeSha256\":\"%s\"}\n", + GetCurrentProcessId(), round, pair, order, position, variant, iterations, successful, failed, + workloadFailed, static_cast(begin.QuadPart), + static_cast(end.QuadPart), static_cast(frequency.QuadPart), wallNs, + successful ? static_cast(wallNs) / static_cast(successful) : 0.0, + gpuCompleted ? "true" : "false", idle ? "true" : "false", backendHash.c_str(), probeHash.c_str()); + std::fflush(stdout); + const bool valid = gpuCompleted && idle && failed == 0 && workloadFailed == 0; + if (!valid) Check(false, "benchmark timed workload completed successfully and stayed idle"); + return valid; +} + +void BenchmarkActiveCapture(DeviceFixture& device) +{ + RuntimeFixture runtime("benchmark-active-observer-stages"); + runtime.config.timing = {100, 300, 100, 50}; + runtime.config.intervalMs = 5; + runtime.Configure(); + RuntimePrepareDevice(device.device); + auto* journal = new ProperShadersStateJournal(device.device); + g_f8 = true; + RuntimeSnapshot snapshot{}; + const auto deadline = GetTickCount64() + 15000; + do { + if (snapshot.phase == Phase::Attribution && !RecordJournal(journal)) Check(false, + "active observer illustration records actual journal transaction"); + HRESULT presented = E_FAIL; + BenchmarkWorkload(device, presented); + snapshot = Snapshot(); + if (snapshot.phase == Phase::Complete && !snapshot.workerBusy) break; + if (snapshot.phase == Phase::Error) break; + Sleep(1); + } while (GetTickCount64() < deadline); + Check(snapshot.phase == Phase::Complete && !snapshot.workerBusy && snapshot.cpuSamples > 0, + "untimed observer-stage illustration completes actual sequential capture"); + const char* names[] = {"baseline", "cpu", "attribution"}; + for (unsigned phase = 0; phase < 3; ++phase) { + const auto& stats = snapshot.phaseStats[phase]; + std::printf("{\"type\":\"active-observer-stage\",\"pid\":%lu,\"captureId\":%llu,\"stage\":\"%s\",\"elapsedNs\":%llu,\"successfulPresents\":%llu,\"failedPresents\":%llu,\"cpuSamples\":%llu,\"suspendedNs\":%llu,\"matchedAB\":false,\"reportPath\":%s}\n", + GetCurrentProcessId(), snapshot.captureId, names[phase], stats.elapsedNs(), stats.presents, + stats.failedPresents, snapshot.cpuSamples, snapshot.suspendedNs, + JsonString(snapshot.reportPath).c_str()); + } + journal->Release(); + runtime.Stop(); +} + +void TestBenchmark(DeviceFixture& device, unsigned rounds, unsigned iterations, bool activeCapture) +{ + if (!device.backend || rounds < 4 || rounds > 64 || iterations < 32 || iterations > 4096) { + Check(false, "benchmark requires backend, 4-64 rounds and 32-4096 iterations"); + return; + } + IDirect3DQuery9* fence = nullptr; + Check(SUCCEEDED(device.device->CreateQuery(D3DQUERYTYPE_EVENT, &fence)) && fence, + "benchmark creates a real GPU completion fence"); + if (!fence) return; + bool valid = true; + for (unsigned round = 0; round < rounds && valid; ++round) { + const char* orders[] = {round % 2 ? "BA" : "AB", round % 2 ? "CA" : "AC"}; + const char* pairs[] = {"AB", "AC"}; + for (unsigned pair = 0; pair < 2 && valid; ++pair) + for (unsigned position = 0; position < 2 && valid; ++position) + valid = BenchmarkVariant(device, fence, round, pairs[pair], orders[pair], position, + orders[pair][position], iterations); + } + fence->Release(); + if (valid && activeCapture) BenchmarkActiveCapture(device); +} +#endif +} + +int main(int argc, char** argv) +{ +#if defined(HAS_MEASUREMENT_RUNTIME) + const bool benchmark = argc > 1 && std::strcmp(argv[1], "--benchmark") == 0; + const bool pendingOnly = argc > 1 && std::strcmp(argv[1], "--pending-stop-only") == 0; + const char* backend = (benchmark || pendingOnly) ? (argc > 2 ? argv[2] : nullptr) : (argc > 1 ? argv[1] : nullptr); + DeviceFixture device(backend, benchmark ? 640u : 64u, benchmark ? 480u : 64u, benchmark); + Check(device.device != nullptr, "real D3D9 HAL device creates for production runtime fixture"); + if (device.device && benchmark) { + IDirect3DSurface9* backBuffer = nullptr; + D3DSURFACE_DESC description{}; + const bool fullHudViewport = IsWindowVisible(device.window) && + SUCCEEDED(device.device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer)) && + backBuffer && SUCCEEDED(backBuffer->GetDesc(&description)) && + description.Width == 640 && description.Height == 480; + if (backBuffer) backBuffer->Release(); + Check(fullHudViewport, "benchmark actual backbuffer is 640x480 and fits the full eight-line HUD"); + if (!fullHudViewport) return 1; + TestBenchmark(device, argc > 3 ? std::strtoul(argv[3], nullptr, 10) : 4, + argc > 4 ? std::strtoul(argv[4], nullptr, 10) : 256, + argc > 5 && std::strcmp(argv[5], "--active") == 0); + } else if (device.device && pendingOnly) { + TestAdmittedJournalInterruption(device, false); + } else if (device.device) { + TestDisabled(device); + TestComplete(device); + TestWorkerFailure(device); + TestInterruption(device, false); + TestShutdown(device); + TestAdmittedJournalInterruption(device, false); + TestAdmittedJournalInterruption(device, true); + TestAdmittedJournalInterruption(device, true, true); + TestInterruption(device, true); + } +#else + Check(false, "production measurement runtime is unavailable"); +#endif + std::printf("checks=%u failures=%u\n", g_checks, g_failures); + return g_failures ? 1 : 0; +} diff --git a/tests/measurement-runtime-tests.vcxproj b/tests/measurement-runtime-tests.vcxproj new file mode 100644 index 0000000..46b4ab2 --- /dev/null +++ b/tests/measurement-runtime-tests.vcxproj @@ -0,0 +1,21 @@ + + + ReleaseWin32 + {8DB26B75-4035-44E7-A484-C6D9912681E8}MeasurementRuntimeTests + + Applicationv145MultiByte + + $(ProjectDir)..\out\perf\20261027-unified-measurement\runtime\bin\$(ProjectDir)..\out\perf\20261027-unified-measurement\runtime\obj\MeasurementRuntimeTests + $(ProjectDir)..\src\bridge\legacy\BridgeMeasurementRuntime.cpp + + MaxSpeedWIN32;NDEBUG;_WINDOWS;NOMINMAX;%(PreprocessorDefinitions)MultiThreadedstdcpplatestfalse/utf-8 %(AdditionalOptions)$(ProjectDir)..\src\bridge\legacy;$(ProjectDir)..\sdk\include;$(ProjectDir)..\backend\dxvk\include\vulkan\include;$(D3DX9IncludeDir);%(AdditionalIncludeDirectories) + Consoled3d9.lib;uuid.lib;user32.lib;bcrypt.lib;%(AdditionalDependencies) + + + + + + + + + diff --git a/tests/measurement-symbols-msvc-test.ps1 b/tests/measurement-symbols-msvc-test.ps1 new file mode 100644 index 0000000..8b0519f --- /dev/null +++ b/tests/measurement-symbols-msvc-test.ps1 @@ -0,0 +1,53 @@ +param( + [string]$EvidenceName = 'msvc-green', + [string]$MSBuild, + [string]$Python, + [string]$LlvmMingwBin, + [switch]$AllowNonV18MsBuild +) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain = Get-RenderStackToolchain -RepoRoot $root -Component Bridge -MsBuildPath $MSBuild -AllowNonV18MsBuild:$AllowNonV18MsBuild +$pythonInfo = Find-RenderStackPython -PythonPath $Python -RepoRoot $root +$platformToolset = if ($toolchain.MSBuild.ProductMajor -eq 17) { 'v143' } else { 'v145' } +$demangler = $null +try { + $llvmInfo = Find-RenderStackLlvmMingw -LlvmMingwBin $LlvmMingwBin -RepoRoot $root + $candidateDemangler = Join-Path $llvmInfo.BinPath 'llvm-cxxfilt.exe' + if (Test-Path -LiteralPath $candidateDemangler -PathType Leaf) { $demangler = $candidateDemangler } +} catch { + if ($LlvmMingwBin) { throw } + Write-Warning "Optional LLVM demangler is unavailable; its test will be skipped: $_" +} +$output = Join-Path $root 'out/perf/20261027-unified-measurement/symbols' +New-Item -ItemType Directory -Force -Path $output | Out-Null +$project = Join-Path $output 'MeasurementSymbolsMsvc.vcxproj' +$xmlRoot = [Security.SecurityElement]::Escape($root) +@" + + + ReleaseWin32 + MeasurementSymbolsMsvc + + Application$platformToolsetMultiByte + + `$(ProjectDir)msvc-bin\`$(ProjectDir)msvc-obj\measurement-symbols-probe + + MaxSpeedMultiThreadedstdcpplatest/utf-8 %(AdditionalOptions)$xmlRoot\src\bridge\legacy;%(AdditionalIncludeDirectories) + Consolebcrypt.lib;%(AdditionalDependencies) + + + + +"@ | Set-Content -LiteralPath $project -Encoding utf8 +& $toolchain.MSBuild.Path $project /nologo /m:1 /nr:false /t:Build /p:Configuration=Release /p:Platform=Win32 /p:PreferredToolArchitecture=x64 2>&1 | + Tee-Object -FilePath (Join-Path $output "$EvidenceName-build.log") +if ($LASTEXITCODE -ne 0) { throw 'MSVC latest measurement symbol probe build failed' } +$testArguments = @((Join-Path $PSScriptRoot 'measurement-symbols-test.py'), '--probe', (Join-Path $output 'msvc-bin/measurement-symbols-probe.exe')) +if ($demangler) { $testArguments += @('--demangle-tool', $demangler) } +& $pythonInfo.Path @testArguments 2>&1 | + Tee-Object -FilePath (Join-Path $output "$EvidenceName-test.log") +if ($LASTEXITCODE -ne 0) { throw 'MSVC latest measurement symbol behavior tests failed' } diff --git a/tests/measurement-symbols-probe.cpp b/tests/measurement-symbols-probe.cpp new file mode 100644 index 0000000..ad92b3b --- /dev/null +++ b/tests/measurement-symbols-probe.cpp @@ -0,0 +1,52 @@ +#if __has_include("MeasurementSymbols.h") +#include "MeasurementSymbols.h" +#include +#include + +template +auto LoadExportsOrAbsent(Resolver& resolver, uintptr_t base, const char* path, int) + -> decltype(resolver.LoadExports(base, path), static_cast(nullptr)) { + return BridgeMeasurement::SymbolLoadStatusName(resolver.LoadExports(base, path)); +} +template +const char* LoadExportsOrAbsent(Resolver&, uintptr_t, const char*, long) { return "exports-unavailable"; } + +template +auto PrintEvidence(const Result& result, int) -> decltype((void)result.provenance, (void)result.aliases, void()) { + std::cout << '\t' << SymbolProvenanceName(result.provenance) << '\t'; + for (size_t i = 0; i < result.aliases.size(); ++i) { + if (i) std::cout << '|'; + std::cout << result.aliases[i]; + } +} +template +void PrintEvidence(const Result&, long) { std::cout << "\tnone\t"; } +template +auto PrintRawName(const Result& result, int) -> decltype((void)result.rawName, void()) { std::cout << '\t' << result.rawName; } +template +void PrintRawName(const Result&, long) { std::cout << '\t'; } + +int main(int argc, char** argv) { + BridgeMeasurement::MeasurementSymbolResolver resolver; + int i = 1; + for (; i + 2 < argc && std::string(argv[i]) != "--"; i += 3) { + const auto base = std::strtoull(argv[i + 2], nullptr, 0); + const char* status = std::string(argv[i + 1]) == "@exports" + ? LoadExportsOrAbsent(resolver, base, argv[i], 0) + : BridgeMeasurement::SymbolLoadStatusName(resolver.LoadModule(base, argv[i], argv[i + 1])); + std::cout << "load\t" << status << '\n'; + } + for (++i; i < argc; ++i) { + const auto result = resolver.Resolve(std::strtoull(argv[i], nullptr, 0)); + std::cout << "resolve\t" << BridgeMeasurement::SymbolQualityName(result.quality) + << '\t' << result.moduleBase << '\t' << result.rva << '\t' << result.offset + << '\t' << result.modulePath << '\t' << result.name; + PrintEvidence(result, 0); + PrintRawName(result, 0); + std::cout << '\n'; + } +} +#else +#include +int main() { std::cerr << "FAIL: exact-build measurement symbol resolver is absent\n"; return 1; } +#endif diff --git a/tests/measurement-symbols-test.ps1 b/tests/measurement-symbols-test.ps1 new file mode 100644 index 0000000..b9efc47 --- /dev/null +++ b/tests/measurement-symbols-test.ps1 @@ -0,0 +1,30 @@ +param( + [string]$OutputDirectory = 'out/perf/20261027-unified-measurement/symbols', + [string]$Compiler, + [string]$Python, + [string]$LlvmMingwBin +) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$compilerPath = if ($Compiler) { Resolve-RenderStackExecutable -Path $Compiler -Description 'symbol-test compiler' } else { $null } +if (-not $LlvmMingwBin -and $compilerPath) { $LlvmMingwBin = Split-Path -Parent $compilerPath } +# Compile and run this fixture directly; no DXVK build tools are required. +$llvm = Find-RenderStackLlvmMingw -RepoRoot $root -LlvmMingwBin $LlvmMingwBin +if (-not $compilerPath) { $compilerPath = $llvm.CompilerPath } +$pythonPath = (Find-RenderStackPython -RepoRoot $root -PythonPath $Python).Path +$output = [IO.Path]::GetFullPath((Join-Path $root $OutputDirectory)) +New-Item -ItemType Directory -Force -Path $output | Out-Null +$probe = Join-Path $output 'measurement-symbols-probe.exe' +$sources = @((Join-Path $PSScriptRoot 'measurement-symbols-probe.cpp')) +$implementation = Join-Path $root 'src/bridge/legacy/MeasurementSymbols.cpp' +if (Test-Path -LiteralPath $implementation) { $sources += $implementation } +& $compilerPath '-std=c++17' '-O2' '-Wall' '-Wextra' '-static' @sources '-I' (Join-Path $root 'src/bridge/legacy') '-lbcrypt' '-o' $probe +if ($LASTEXITCODE -ne 0) { throw 'measurement symbol probe build failed' } +$testArguments = @((Join-Path $PSScriptRoot 'measurement-symbols-test.py'), '--probe', $probe) +$demangler = Join-Path $llvm.BinPath 'llvm-cxxfilt.exe' +if (Test-Path -LiteralPath $demangler -PathType Leaf) { $testArguments += @('--demangle-tool', $demangler) } +& $pythonPath @testArguments +if ($LASTEXITCODE -ne 0) { throw 'measurement symbol behavior tests failed' } diff --git a/tests/measurement-symbols-test.py b/tests/measurement-symbols-test.py new file mode 100644 index 0000000..64b6c35 --- /dev/null +++ b/tests/measurement-symbols-test.py @@ -0,0 +1,373 @@ +"""Behavior tests: real PE/COFF bytes, real preparation CLI, real native resolver. + +Each case catches incorrect identity/range acceptance or invented symbol precision. +Run with --probe ; no game process is used. +""" +import hashlib +import json +import pathlib +import shutil +import struct +import subprocess +import sys +import tempfile +import unittest + +ROOT = pathlib.Path(__file__).resolve().parents[1] +TOOL = ROOT / "tools/prepare-measurement-symbols.py" +LLVM_CXXFILT = pathlib.Path(shutil.which("llvm-cxxfilt") or "") +if "--demangle-tool" in sys.argv: + at = sys.argv.index("--demangle-tool") + LLVM_CXXFILT = pathlib.Path(sys.argv[at + 1]).resolve() + del sys.argv[at:at + 2] +PROBE = None +if "--probe" in sys.argv: + at = sys.argv.index("--probe") + PROBE = pathlib.Path(sys.argv[at + 1]).resolve() + del sys.argv[at:at + 2] + + +def pe_bytes(symbols=(), timestamp=0x12345678): + """Tiny PE32 with a real COFF table; literal .text RVA [0x1000,0x1100).""" + data = bytearray(0x400) + data[:2] = b"MZ" + struct.pack_into(" 8: + n = struct.pack(" + +// Deliberately incompatible PE image, large enough for every preflight RVA. +// The fixture never invokes a game hook or loads a real MOD. +static volatile unsigned char padding[2 * 1024 * 1024] = {0xcc}; +extern "C" __declspec(dllexport) const volatile void* FixtureImage() { + return padding; +} diff --git a/tests/patch-retry-probe.cpp b/tests/patch-retry-probe.cpp new file mode 100644 index 0000000..28d5fad --- /dev/null +++ b/tests/patch-retry-probe.cpp @@ -0,0 +1,161 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include "lib/win32-import-hook.h" +#include "lib/benchmark-thread-affinity.h" +#include "patch-retry-sites.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h" + +static unsigned checks; +static unsigned failures; +#define CHECK(condition) do { ++checks; if (!(condition)) { ++failures; std::printf("FAIL line=%u %s\n", __LINE__, #condition); } } while (0) +static decltype(&GetModuleHandleExA) originalPin; +static HMODULE fixtureModule; +static unsigned pinAttempts; +static bool failFirstPin; +static DWORD ownerThread; +static void RestoreFixtureSignatures() { + for (const auto& patch : fixtureCallSites) { + auto site = reinterpret_cast(fixtureModule) + patch.rva; + DWORD old = 0, ignored = 0; + CHECK(VirtualProtect(site, sizeof(patch.bytes), PAGE_EXECUTE_READWRITE, &old)); + std::memcpy(site, patch.bytes, sizeof(patch.bytes)); + CHECK(VirtualProtect(site, sizeof(patch.bytes), old, &ignored)); + } +} + +static BOOL WINAPI ObservePin(DWORD flags, LPCSTR address, HMODULE* result) { + if ((flags & GET_MODULE_HANDLE_EX_FLAG_PIN) && + address == reinterpret_cast(fixtureModule) && GetCurrentThreadId() == ownerThread) { + ++pinAttempts; + if (failFirstPin && pinAttempts == 1) { + SetLastError(ERROR_NOT_READY); + return FALSE; + } + } + return originalPin(flags, address, result); +} + +static void Present(IDirect3DDevice9* device, unsigned frames) { + for (unsigned i = 0; i < frames; ++i) + CHECK(device->Present(nullptr, nullptr, nullptr, nullptr) == S_OK); +} + +static double ClockMilliseconds() { + LARGE_INTEGER ticks, frequency; + QueryPerformanceCounter(&ticks); + QueryPerformanceFrequency(&frequency); + return double(ticks.QuadPart) * 1000 / frequency.QuadPart; +} + +static double ThreadCpuMilliseconds() { + FILETIME created, exited, kernel, user; + CHECK(GetThreadTimes(GetCurrentThread(), &created, &exited, &kernel, &user)); + ULARGE_INTEGER k{}, u{}; + k.LowPart = kernel.dwLowDateTime; + k.HighPart = kernel.dwHighDateTime; + u.LowPart = user.dwLowDateTime; + u.HighPart = user.dwHighDateTime; + return double(k.QuadPart + u.QuadPart) / 10000; +} + +int main(int argc, char** argv) { + if (argc != 4) + return 2; + ownerThread = GetCurrentThreadId(); + failFirstPin = !std::strcmp(argv[3], "transient"); + const bool recover = !std::strcmp(argv[3], "restore"); + const bool benchmark = !std::strcmp(argv[3], "bench"); + fixtureModule = LoadLibraryA(argv[2]); + CHECK(fixtureModule != nullptr); + HMODULE bridge = LoadLibraryA(argv[1]); + CHECK(bridge != nullptr); + if (!bridge || !fixtureModule) + return 1; + { + ImportHook pin; + CHECK(pin.install(bridge, "GetModuleHandleExA", reinterpret_cast(&ObservePin))); + originalPin = reinterpret_cast(pin.original); + if (!originalPin) + return 1; + using Create = IDirect3D9* (WINAPI*)(UINT); + const auto create = reinterpret_cast(GetProcAddress(bridge, "Direct3DCreate9")); + CHECK(create != nullptr); + IDirect3D9* factory = create(D3D_SDK_VERSION); + CHECK(factory != nullptr); + WNDCLASSA wc{}; + wc.lpfnWndProc = DefWindowProcA; + wc.hInstance = GetModuleHandleA(nullptr); + wc.lpszClassName = "RenderStackPatchRetry"; + RegisterClassA(&wc); + HWND window = CreateWindowA(wc.lpszClassName, "retry", WS_OVERLAPPEDWINDOW, + 0, 0, 64, 64, nullptr, nullptr, wc.hInstance, nullptr); + D3DPRESENT_PARAMETERS params{}; + params.BackBufferWidth = params.BackBufferHeight = 32; + params.BackBufferFormat = D3DFMT_A8R8G8B8; + params.BackBufferCount = 1; + params.SwapEffect = D3DSWAPEFFECT_DISCARD; + params.Windowed = TRUE; + params.hDeviceWindow = window; + params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* device = nullptr; + CHECK(factory->CreateDevice(0, D3DDEVTYPE_HAL, window, + D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, &device) == S_OK); + if (device) { + unsigned char before[6]; + auto site = reinterpret_cast(fixtureModule) + 0x12dc9; + std::memcpy(before, site, sizeof(before)); + Present(device, 16); + if (!benchmark) + CHECK(pinAttempts == (failFirstPin ? 2u : 1u)); + CHECK(std::memcmp(before, site, sizeof(before)) == 0); + if (benchmark) { + BenchmarkThreadAffinity affinity; + CHECK(affinity.valid()); + const auto cpuStart = ThreadCpuMilliseconds(); + ULONG64 cyclesBefore = 0, cyclesAfter = 0; + CHECK(QueryThreadCycleTime(GetCurrentThread(), &cyclesBefore)); + const auto start = ClockMilliseconds(); + Present(device, 2000); + const auto elapsed = ClockMilliseconds() - start; + const auto cpu = ThreadCpuMilliseconds() - cpuStart; + CHECK(QueryThreadCycleTime(GetCurrentThread(), &cyclesAfter)); + std::printf("benchmark presents=2000 elapsedMs=%.6f mainCpuMs=%.6f mainCycles=%llu\n", + elapsed, cpu, static_cast(cyclesAfter - cyclesBefore)); + } + if (recover) { + RestoreFixtureSignatures(); + Present(device, 1); + CHECK(pinAttempts == 2u); + CHECK(site[0] == 0xe8 && site[5] == 0x90); + } + CHECK(device->Reset(¶ms) == S_OK); + Present(device, 8); + if (!benchmark) + CHECK(pinAttempts == (recover || failFirstPin ? 2u : 1u)); + CHECK(device->Release() == 0); + } + ID3D9BridgeRuntime* runtime = nullptr; + CHECK(factory->QueryInterface(IID_D3D9BridgeRuntime, reinterpret_cast(&runtime)) == S_OK); + if (runtime) { + CHECK(runtime->Shutdown() == S_OK); + CHECK(runtime->Shutdown() == S_FALSE); + runtime->Release(); + } + if (recover) { + for (const auto& patch : fixtureCallSites) + CHECK(std::memcmp(reinterpret_cast(fixtureModule) + patch.rva, + patch.bytes, sizeof(patch.bytes)) == 0); + } + factory->Release(); + DestroyWindow(window); + } + std::printf("pinAttempts=%u transient=%u checks=%u failures=%u\n", + pinAttempts, unsigned(failFirstPin), checks, failures); + FreeLibrary(bridge); + FreeLibrary(fixtureModule); + return failures ? 1 : 0; +} diff --git a/tests/patch-retry-sites.h b/tests/patch-retry-sites.h new file mode 100644 index 0000000..cd5ff8a --- /dev/null +++ b/tests/patch-retry-sites.h @@ -0,0 +1,40 @@ +#pragma once +#include +// Independent captured call-site fixture. The installed calls are never executed. +struct FixtureCallSite { std::uint32_t rva; unsigned char bytes[6]; }; +static constexpr FixtureCallSite fixtureCallSites[] = { + { 0x00012DC9, { 0xFF, 0x90, 0x88, 0x00, 0x00, 0x00 } }, + { 0x00012DD9, { 0xFF, 0x90, 0x88, 0x00, 0x00, 0x00 } }, + { 0x00012E2F, { 0xFF, 0x92, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00012E44, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00012F0F, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00012FC4, { 0xFF, 0x92, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00012FD5, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00013014, { 0xFF, 0x90, 0x88, 0x00, 0x00, 0x00 } }, + { 0x00013029, { 0xFF, 0x90, 0x80, 0x00, 0x00, 0x00 } }, + { 0x0001303D, { 0xFF, 0x91, 0x80, 0x00, 0x00, 0x00 } }, + { 0x00013078, { 0xFF, 0x90, 0xE8, 0x00, 0x00, 0x00 } }, + { 0x00013087, { 0xFF, 0x90, 0xFC, 0x00, 0x00, 0x00 } }, + { 0x00013092, { 0xFF, 0x90, 0x00, 0x01, 0x00, 0x00 } }, + { 0x000130AF, { 0xFF, 0x90, 0x08, 0x01, 0x00, 0x00 } }, + { 0x000130B8, { 0xFF, 0x90, 0x0C, 0x01, 0x00, 0x00 } }, + { 0x000132EE, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00013300, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00013312, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x000133F1, { 0xFF, 0x90, 0x88, 0x00, 0x00, 0x00 } }, + { 0x00013409, { 0xFF, 0x91, 0x80, 0x00, 0x00, 0x00 } }, + { 0x000134F1, { 0xFF, 0x90, 0x04, 0x01, 0x00, 0x00 } }, + { 0x00013784, { 0xFF, 0x91, 0xFC, 0x00, 0x00, 0x00 } }, + { 0x00013794, { 0xFF, 0x91, 0x00, 0x01, 0x00, 0x00 } }, + { 0x0001386F, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00013884, { 0xFF, 0x90, 0x98, 0x00, 0x00, 0x00 } }, + { 0x00013892, { 0xFF, 0x90, 0xE8, 0x00, 0x00, 0x00 } }, + { 0x000138A4, { 0xFF, 0x90, 0xFC, 0x00, 0x00, 0x00 } }, + { 0x000138AF, { 0xFF, 0x90, 0x00, 0x01, 0x00, 0x00 } }, + { 0x000138C9, { 0xFF, 0x90, 0x08, 0x01, 0x00, 0x00 } }, + { 0x000138D2, { 0xFF, 0x90, 0x0C, 0x01, 0x00, 0x00 } }, + { 0x000138EE, { 0xFF, 0x91, 0x08, 0x01, 0x00, 0x00 } }, + { 0x000138FC, { 0xFF, 0x91, 0x0C, 0x01, 0x00, 0x00 } }, + { 0x00013DA9, { 0xFF, 0x91, 0x08, 0x01, 0x00, 0x00 } }, + { 0x00013DB7, { 0xFF, 0x91, 0x0C, 0x01, 0x00, 0x00 } }, +}; diff --git a/tests/patch-retry-test.ps1 b/tests/patch-retry-test.ps1 new file mode 100644 index 0000000..cb0bd7c --- /dev/null +++ b/tests/patch-retry-test.ps1 @@ -0,0 +1,59 @@ +param([string]$Bridge,[string]$Backend,[string]$OutputDirectory,[string]$LlvmMingwBin,[switch]$Observe) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Bridge){$Bridge=Join-Path $root 'out/build/bridge/d3d9.dll'} +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/perf/20261108-upload-state/retry'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$compiler=$toolchain.LlvmMingw.CompilerPath +$fixture=Join-Path $OutputDirectory 'ProperShaders.asi' +$probe=Join-Path $OutputDirectory 'patch-retry-probe.exe' +& $compiler '-O2' '-static' '-shared' (Join-Path $PSScriptRoot 'patch-retry-module.cpp') '-o' $fixture +if($LASTEXITCODE){throw 'Patch fixture compilation failed'} +& $compiler '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'patch-retry-probe.cpp') '-o' $probe '-luser32' '-luuid' +if($LASTEXITCODE){throw 'Patch retry probe compilation failed'} +$library=(Resolve-Path -LiteralPath $Bridge).Path +$backendDirectory=Split-Path -Parent (Resolve-Path -LiteralPath $Backend).Path +$runDirectory=Join-Path $OutputDirectory ('run-'+[guid]::NewGuid().ToString('N')) +foreach($mode in @('reject','transient','restore')){ + $directory=Join-Path $runDirectory $mode + New-Item -ItemType Directory -Force -Path "$directory/scripts"|Out-Null + Copy-Item -LiteralPath $probe -Destination "$directory/probe.exe" + Copy-Item -LiteralPath $fixture -Destination "$directory/ProperShaders.asi" + $config=@" +[Backend] +UseDxvkBackend=1 +DxvkBackendDir=$backendDirectory +[ProxyChain] +Enable=0 +ForceDeviceWrap=0 +[PostFX] +EnableHost=0 +[Measurement] +Enable=0 +[ProperShadersRenderStateGuard] +Enable=0 +[ProperShadersEffectOptimization] +Enable=1 +GeneralStateJournal=0 +StateJournal=1 +[Affinity] +Enable=0 +"@ + [IO.File]::WriteAllText("$directory/SA.RenderStack.ini",$config) + $env:DXVK_LOG_PATH=$directory + $env:DXVK_CONFIG='d3d9.presentInterval=0;dxvk.hud=' + Push-Location $directory + try{ + & "$directory/probe.exe" $library "$directory/ProperShaders.asi" $mode 2>&1|Tee-Object "$directory/test.log" + $code=$LASTEXITCODE + }finally{Pop-Location} + $log=Get-Content -LiteralPath "$directory/scripts/BridgeD3D9.log" + $mismatches=@($log|Where-Object {$_ -match 'effectopt: patch preflight mismatch'}).Count + "mode=$mode mismatchLines=$mismatches nativeExit=$code" + if(!$Observe -and ($code -ne 0 -or $mismatches -ne 1)){throw "Patch retry regression failed: $mode"} +} +"Fixture retained: $runDirectory" diff --git a/tests/statering-runtime-benchmark.ps1 b/tests/statering-runtime-benchmark.ps1 new file mode 100644 index 0000000..66972bb --- /dev/null +++ b/tests/statering-runtime-benchmark.ps1 @@ -0,0 +1,36 @@ +param([Parameter(Mandatory)][string]$Backend,[int]$Processes=12,[int]$Rounds=4,[int]$Iterations=512,[string]$EvidenceName='paired-final') +$ErrorActionPreference='Stop' +Set-StrictMode -Version Latest +if($Processes -lt 2 -or $Rounds -lt 2 -or $Iterations -lt 64){throw 'Repeated benchmark requires processes>=2, rounds>=2 and iterations>=64'} +$root=Split-Path -Parent $PSScriptRoot +$base=Join-Path $root 'out/perf/20261027-statering-cache' +$exe=@{A=Join-Path $base 'bench-baseline/bin/StateRingRuntimeTests.exe';B=Join-Path $base 'bench-candidate/bin/StateRingRuntimeTests.exe'} +$out=Join-Path $base ($EvidenceName+'-'+[DateTime]::UtcNow.ToString('yyyyMMddTHHmmssfff')) +New-Item -ItemType Directory -Path $out|Out-Null +$Backend=(Resolve-Path -LiteralPath $Backend).Path +$identities=@($Backend,$exe.A,$exe.B)|Get-FileHash -Algorithm SHA256|Select-Object Path,Hash +[ordered]@{identities=$identities;processPairs=$Processes;rounds=$Rounds;iterations=$Iterations;order='AB/BA alternating process pairs; native modes reverse by round';measurement='wall-ns-per-guard-plus-owner-ring-poll';inputs='real D3D9/Vulkan device; controlled GTA API addresses and key/clock; no physical input';limits='source fixture, not game FPS; forwarding VirtualQuery observer shared by both versions'}|ConvertTo-Json -Depth 4|Set-Content (Join-Path $out 'metadata.json') +$rows=[Collections.Generic.List[object]]::new() +for($p=0;$p -lt $Processes;++$p){ + $order=if($p%2){'B','A'}else{'A','B'} + foreach($variant in $order){ + foreach($id in $identities){if((Get-FileHash -LiteralPath $id.Path -Algorithm SHA256).Hash -cne $id.Hash){throw 'Benchmark input changed'}} + $wd=Join-Path $out "$p-$variant";New-Item -ItemType Directory -Path $wd|Out-Null + Push-Location $wd + try {& $exe[$variant] --benchmark $Backend $Rounds $Iterations *> 'raw.log';$code=$LASTEXITCODE} + finally {Pop-Location} + if($code){throw "Native benchmark failed: $p-$variant"} + $data=@(Get-Content (Join-Path $wd 'raw.log')|Where-Object {$_.StartsWith('{')}|ForEach-Object {$_|ConvertFrom-Json}) + if($data.Count -ne $Rounds*3){throw 'Incomplete benchmark rows'} + foreach($row in $data){ + $row|Add-Member -NotePropertyName processPair -NotePropertyValue $p + $row|Add-Member -NotePropertyName variant -NotePropertyValue $variant + $rows.Add($row) + } + } + Write-Output "completed pair $($p+1)/$Processes" +} +$rows|ConvertTo-Json -Depth 4|Set-Content (Join-Path $out 'rows.json') +$paired=@($rows|Group-Object processPair,round,mode|ForEach-Object {$a=@($_.Group|Where-Object variant -eq A)[0];$b=@($_.Group|Where-Object variant -eq B)[0];[pscustomobject]@{processPair=$a.processPair;round=$a.round;mode=$a.mode;baselineNs=$a.wallNsPerCall;candidateNs=$b.wallNsPerCall;ratio=$b.wallNsPerCall/$a.wallNsPerCall;baselineQueries=$a.virtualQueries;candidateQueries=$b.virtualQueries;baselineSnapshots=$a.snapshotAttempts;candidateSnapshots=$b.snapshotAttempts}}) +$paired|ConvertTo-Json -Depth 4|Set-Content (Join-Path $out 'paired.json') +Write-Output "Artifacts: $out" diff --git a/tests/statering-runtime-test.ps1 b/tests/statering-runtime-test.ps1 new file mode 100644 index 0000000..0266122 --- /dev/null +++ b/tests/statering-runtime-test.ps1 @@ -0,0 +1,21 @@ +param([string]$Backend, [string]$EvidenceName='candidate', [switch]$BaselineSource, + [switch]$BuildOnly, [string]$MSBuild, [switch]$AllowNonV18MsBuild) +$ErrorActionPreference='Stop' +Set-StrictMode -Version Latest +$root=Split-Path -Parent $PSScriptRoot +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$compiler=Find-RenderStackMSBuild -MsBuildPath $MSBuild -RepoRoot $root -AllowNonV18MsBuild:$AllowNonV18MsBuild +$evidence=Join-Path $root "out/perf/20261027-statering-cache/$EvidenceName" +New-Item -ItemType Directory -Path $evidence -Force|Out-Null +$buildArgs=@((Join-Path $PSScriptRoot 'statering-runtime-tests.vcxproj'),'/nologo','/m:1','/nr:false','/p:Configuration=Release','/p:Platform=Win32',"/p:EvidenceRoot=$evidence") +if($compiler.ProductMajor -lt 18){$buildArgs+='/p:PlatformToolset=v143'} +if($BaselineSource){$buildArgs+=@("/p:GuardSourceRoot=$(Join-Path $root 'out/perf/20261027-statering-cache/source-before/src/bridge/legacy')",'/p:FixtureDefinitions=BASELINE_SOURCE')} +& $compiler.Path @buildArgs *> (Join-Path $evidence 'build.log') +if($LASTEXITCODE){Get-Content (Join-Path $evidence 'build.log') -Tail 15;throw 'StateRing native fixture build failed'} +if($BuildOnly){return} +$backendPath=if($Backend){(Resolve-Path -LiteralPath $Backend).Path}else{$null} +Push-Location $evidence +try { $run=@();if($backendPath){$run+=$backendPath}; & (Join-Path $evidence 'bin/StateRingRuntimeTests.exe') @run 2>&1|Tee-Object (Join-Path $evidence 'test.log');$code=$LASTEXITCODE } +finally {Pop-Location} +if($code){throw "StateRing native fixture failed: $code"} diff --git a/tests/statering-runtime-tests.cpp b/tests/statering-runtime-tests.cpp new file mode 100644 index 0000000..964533e --- /dev/null +++ b/tests/statering-runtime-tests.cpp @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Only the external GTA addresses and observation APIs are supplied by the +// fixture. All device methods below forward to an actual HAL device. +static uintptr_t fixtureSetState, fixtureGetState, fixtureDeviceCell; +static thread_local unsigned long long queryCalls; +static unsigned snapshotAttempts; +static SIZE_T WINAPI ObservedVirtualQuery(LPCVOID p, PMEMORY_BASIC_INFORMATION m, SIZE_T n) { + ++queryCalls; return ::VirtualQuery(p, m, n); +} +#define kRwD3D9SetRenderState fixtureSetState +#define kRwD3D9GetRenderState fixtureGetState +#define kRwD3D9DevicePointer fixtureDeviceCell +#define VirtualQuery ObservedVirtualQuery +#define CaptureProperShadersD3D9StateSnapshot ObservedSnapshotImplementation +#include +#undef CaptureProperShadersD3D9StateSnapshot +#undef VirtualQuery +#undef kRwD3D9SetRenderState +#undef kRwD3D9GetRenderState +#undef kRwD3D9DevicePointer +bool CaptureProperShadersD3D9StateSnapshot(ProperShadersD3D9StateSnapshot* s) { + ++snapshotAttempts; return ObservedSnapshotImplementation(s); +} + +static ULONGLONG fixtureTick = 100; +static bool fixtureKey; +static ULONGLONG WINAPI FixtureTick() { return fixtureTick; } +static SHORT WINAPI FixtureKey(int) { return fixtureKey ? SHORT(0x8000) : 0; } +#define GetTickCount64 FixtureTick +#define GetAsyncKeyState FixtureKey +#define Log RingFixtureLog +#include +#undef Log +#undef GetAsyncKeyState +#undef GetTickCount64 + +namespace { +unsigned checks, failures; +void Check(bool ok, const char* text) { ++checks; if (!ok) { ++failures; std::printf("FAIL %s\n", text); } } +struct Proxy { void** vtable; IDirect3DDevice9* real; }; +static std::array rwStates; +static unsigned textureGets, setVersion1, setVersion2; +void __cdecl RwGet(DWORD state, void* out) { *static_cast(out) = rwStates.at(state); } +void __cdecl RwSet(DWORD state, DWORD value) { rwStates.at(state) = value; } +HRESULT WINAPI SetState(Proxy* p, DWORD s, DWORD v) { ++setVersion1; return p->real->SetRenderState(D3DRENDERSTATETYPE(s), v); } +HRESULT WINAPI SetState2(Proxy* p, DWORD s, DWORD v) { ++setVersion2; return p->real->SetRenderState(D3DRENDERSTATETYPE(s), v); } +HRESULT WINAPI GetState(Proxy* p, DWORD s, DWORD* v) { return p->real->GetRenderState(D3DRENDERSTATETYPE(s), v); } +HRESULT WINAPI GetTarget(Proxy* p, DWORD i, void** v) { return p->real->GetRenderTarget(i, reinterpret_cast(v)); } +HRESULT WINAPI GetDepth(Proxy* p, void** v) { return p->real->GetDepthStencilSurface(reinterpret_cast(v)); } +HRESULT WINAPI GetView(Proxy* p, ProperShadersD3D9Viewport* v) { return p->real->GetViewport(reinterpret_cast(v)); } +HRESULT WINAPI GetTexture(Proxy* p, DWORD i, void** v) { ++textureGets; return p->real->GetTexture(i, reinterpret_cast(v)); } +HRESULT WINAPI GetStage(Proxy* p, DWORD i, DWORD s, DWORD* v) { return p->real->GetTextureStageState(i, D3DTEXTURESTAGESTATETYPE(s), v); } +HRESULT WINAPI GetSampler(Proxy* p, DWORD i, DWORD s, DWORD* v) { return p->real->GetSamplerState(i, D3DSAMPLERSTATETYPE(s), v); } +HRESULT WINAPI GetVS(Proxy* p, void** v) { return p->real->GetVertexShader(reinterpret_cast(v)); } +HRESULT WINAPI GetPS(Proxy* p, void** v) { return p->real->GetPixelShader(reinterpret_cast(v)); } +HRESULT WINAPI WrongShaderGetter(Proxy*, void**) { return D3D_OK; } +void* deviceCell; +Proxy* current; +void __cdecl SceneBody() { + current->real->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); + current->real->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_LESS); + current->real->SetRenderState(D3DRS_ALPHAREF, 0); + current->real->SetRenderState(D3DRS_STENCILENABLE, TRUE); +} +void Boundary() { +#if !defined(BASELINE_SOURCE) + InvalidateProperShadersDeviceApiCache(); +#endif +} +struct Device { + HMODULE library = nullptr; + IDirect3D9* d3d = nullptr; + IDirect3DDevice9* real = nullptr; + IDirect3DTexture9* texture = nullptr; + HWND window = nullptr; + D3DPRESENT_PARAMETERS pp{}; + std::array table{}, secondTable{}; + Proxy proxy{}, secondProxy{}; + explicit Device(const char* path) { + if (!path) { static char system[MAX_PATH]; GetSystemDirectoryA(system, MAX_PATH); std::strcat(system, "\\d3d9.dll"); path = system; } + library = LoadLibraryExA(path, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); + auto create = library ? reinterpret_cast(GetProcAddress(library, "Direct3DCreate9")) : nullptr; + d3d = create ? create(D3D_SDK_VERSION) : nullptr; + window = CreateWindowA("STATIC", "StateRing fixture", WS_OVERLAPPEDWINDOW, 0, 0, 96, 96, nullptr, nullptr, GetModuleHandleA(nullptr), nullptr); + pp.Windowed = TRUE; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = window; + pp.BackBufferWidth = pp.BackBufferHeight = 64; pp.BackBufferFormat = D3DFMT_A8R8G8B8; + pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + if (d3d) d3d->CreateDevice(0, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &pp, &real); + if (!real) return; + table[57] = reinterpret_cast(&SetState); table[58] = reinterpret_cast(&GetState); + table[38] = reinterpret_cast(&GetTarget); table[40] = reinterpret_cast(&GetDepth); + table[48] = reinterpret_cast(&GetView); table[64] = reinterpret_cast(&GetTexture); + table[66] = reinterpret_cast(&GetStage); table[68] = reinterpret_cast(&GetSampler); + table[93] = reinterpret_cast(&GetVS); table[108] = reinterpret_cast(&GetPS); + secondTable = table; proxy = {table.data(), real}; secondProxy = {secondTable.data(), real}; + current = &proxy; deviceCell = current; + real->CreateTexture(2, 2, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, nullptr); + real->SetTexture(0, texture); + real->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); + real->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER); + real->SetRenderState(D3DRS_ALPHAREF, 128); + real->SetRenderState(D3DRS_STENCILENABLE, FALSE); + for (auto s : kProperShadersAlphaRenderStates) real->GetRenderState(D3DRENDERSTATETYPE(s), &rwStates[s]); + fixtureSetState = uintptr_t(&RwSet); fixtureGetState = uintptr_t(&RwGet); + fixtureDeviceCell = uintptr_t(&deviceCell); + g_properShadersRenderStateGuardConfig.enable = true; + g_properShadersRenderStateGuardConfig.enableStateRing = true; + g_properShadersRenderStateGuardApiReady = 1; + g_properShadersPatchedRenderSceneTrampoline = uintptr_t(&SceneBody); + Boundary(); + } + ~Device() { + g_properShadersPatchedRenderSceneTrampoline = 0; + ShutdownProperShadersStateRing(); Boundary(); deviceCell = nullptr; + if (real) real->SetTexture(0, nullptr); + if (texture) texture->Release(); + if (real) real->Release(); + if (d3d) d3d->Release(); + if (library) FreeLibrary(library); + if (window) DestroyWindow(window); + } +}; +void VerifyCorrected(Device& d) { + DWORD value = 0; + for (auto [state, expected] : {std::pair{D3DRS_ALPHATESTENABLE, 1u}, {D3DRS_ALPHAFUNC, DWORD(D3DCMP_GREATER)}, {D3DRS_ALPHAREF, 128u}, {D3DRS_STENCILENABLE, 0u}}) { + Check(SUCCEEDED(d.real->GetRenderState(state, &value)) && value == expected, "guard preserves actual alpha/stencil restoration"); + } +} +void NativeBehavior(Device& d) { + D3D9StateTraceApi traceApi{}; + const bool traceAvailable = ResolveD3D9StateTraceApi(&traceApi); + std::printf("actual trace API available=%u shaderValidated=%ld shaderRejected=%ld textureGetter=%p\n", + unsigned(traceAvailable), g_properShadersShaderSlotsValidated, g_properShadersShaderSlotsRejected, + reinterpret_cast(traceApi.getTexture)); + for (auto slot : {38u, 40u, 48u, 57u, 58u, 64u, 66u, 68u, 93u, 108u}) + Check(IsExecutableCommitted(uintptr_t(d.table[slot])), "fixture forwarding method is executable"); + textureGets = snapshotAttempts = 0; fixtureKey = false; MaybeDumpProperShadersStateTraceRing(); + Bridge_ProperShadersPatchedRenderSceneGuard(); VerifyCorrected(d); + Check(snapshotAttempts == 0, "enabled idle StateRing makes no full device snapshot attempts"); + D3D9RenderStateApi api{}; Boundary(); queryCalls = 0; + Check(ResolveD3D9RenderStateApi(&api), "cold actual device API resolution succeeds"); + const auto cold = queryCalls; + for (unsigned i = 0; i < 64; ++i) Check(ResolveD3D9RenderStateApi(&api), "warm actual API resolves"); + Check(cold > 0 && queryCalls == cold, "warm matching interface avoids repeated VirtualQuery"); + std::thread other([&] { + queryCalls = 0; D3D9RenderStateApi local{}; + Check(ResolveD3D9RenderStateApi(&local) && queryCalls > 0, "second calling thread validates its own borrowed API cache"); + }); other.join(); + d.table[57] = reinterpret_cast(&SetState2); + bool changed = false; + Check(SynchronizeD3D9DeviceRenderState(15, 0, nullptr, &changed) && setVersion2 > 0, "in-place method change invokes current native forwarding method"); + d.table[57] = reinterpret_cast(&SetState); + deviceCell = &d.secondProxy; + Check(ResolveD3D9RenderStateApi(&api) && api.device == &d.secondProxy, "device/vtable replacement resolves current identity"); + deviceCell = &d.proxy; + d.table[58] = nullptr; Check(!ResolveD3D9RenderStateApi(&api), "null method after warm resolution is rejected"); d.table[58] = reinterpret_cast(&GetState); + auto* bad = VirtualAlloc(nullptr, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS); + d.proxy.vtable = static_cast(bad); Check(!ResolveD3D9RenderStateApi(&api), "inaccessible vtable fails safely after warm cache"); + d.proxy.vtable = d.table.data(); VirtualFree(bad, 0, MEM_RELEASE); + Check(ResolveD3D9RenderStateApi(&api), "valid interface recovers after invalid access"); + auto* thunk = static_cast(VirtualAlloc(nullptr, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); + Check(thunk != nullptr, "disposable executable method thunk allocates"); + if (thunk) { + thunk[0] = 0xE9; const uint32_t jump = uint32_t(uintptr_t(&GetState) - (uintptr_t(thunk) + 5)); + std::memcpy(thunk + 1, &jump, sizeof(jump)); FlushInstructionCache(GetCurrentProcess(), thunk, 5); + d.table[58] = thunk; Check(ResolveD3D9RenderStateApi(&api), "temporary real forwarding method validates"); + DWORD old; Check(VirtualProtect(thunk, 4096, PAGE_NOACCESS, &old) != FALSE, "method page becomes inaccessible without changing pointer"); + uint32_t value = 0; Check(!ReadD3D9DeviceRenderState(15, &value), "guarded cached method fault returns unavailable"); + queryCalls = 0; + Check(!ReadD3D9DeviceRenderState(15, &value) && queryCalls > 0, "access fault invalidates cache before the next resolution"); + d.table[58] = reinterpret_cast(&GetState); VirtualFree(thunk, 0, MEM_RELEASE); + } + fixtureKey = true; MaybeDumpProperShadersStateTraceRing(); + const auto before = snapshotAttempts; + const ULONG refsBefore = d.texture ? d.texture->AddRef() : 0; if (d.texture) d.texture->Release(); + Bridge_ProperShadersPatchedRenderSceneGuard(); VerifyCorrected(d); + Check(snapshotAttempts > before, "manual key edge attempts full actual device snapshots"); + Check(!traceAvailable || textureGets > 0, "supported native snapshot path reaches actual texture getter"); + if (traceAvailable) { + d.table[93] = reinterpret_cast(&WrongShaderGetter); + ProperShadersD3D9StateSnapshot badSnapshot{}; + Check(!CaptureProperShadersD3D9StateSnapshot(&badSnapshot), "changed shader getter is re-probed instead of trusting old slot validation"); + d.table[93] = reinterpret_cast(&GetVS); + Check(CaptureProperShadersD3D9StateSnapshot(&badSnapshot), "valid shader getter recovers on the next method identity"); + } + fixtureTick += 200; MaybeDumpProperShadersStateTraceRing(); + ProperShadersStateTraceRecord deadlineRecord{}; + InitializeProperShadersStateTraceRecord(&deadlineRecord, kProperShadersStateTraceRenderScene); + CaptureProperShadersStateTracePoint(&deadlineRecord, 0); + fixtureTick += 1000; // Deliberately no owner Present/key poll after deadline. + const auto expiredAttempts = snapshotAttempts; + const auto expiredSequence = g_properShadersStateTraceSequence; + Bridge_ProperShadersPatchedRenderSceneGuard(); + StoreProperShadersStateTraceRecord(&deadlineRecord); + Check(snapshotAttempts == expiredAttempts, "deadline rejects new snapshots without another owner poll"); + Check(g_properShadersStateTraceSequence == expiredSequence, "deadline rejects late stores before deferred export"); + MaybeDumpProperShadersStateTraceRing(); + const auto stopped = snapshotAttempts; + Bridge_ProperShadersPatchedRenderSceneGuard(); + Check(snapshotAttempts == stopped, "bounded capture expires even while key remains held"); + fixtureKey = false; MaybeDumpProperShadersStateTraceRing(); fixtureKey = true; MaybeDumpProperShadersStateTraceRing(); + Bridge_ProperShadersPatchedRenderSceneGuard(); Check(snapshotAttempts > stopped, "released then pressed key starts a fresh capture"); + ProperShadersStateTraceRecord late{}; + InitializeProperShadersStateTraceRecord(&late, kProperShadersStateTraceRenderScene); + CaptureProperShadersStateTracePoint(&late, 0); + const ULONG refsAfter = d.texture ? d.texture->AddRef() : 0; if (d.texture) d.texture->Release(); + Check(refsBefore == refsAfter, "actual texture getter references are balanced across captures"); + ShutdownProperShadersStateRing(); const auto shutdownGets = snapshotAttempts; + Bridge_ProperShadersPatchedRenderSceneGuard(); Check(snapshotAttempts == shutdownGets, "shutdown cancels detailed snapshot admission"); + fixtureKey = false; MaybeDumpProperShadersStateTraceRing(); fixtureKey = true; MaybeDumpProperShadersStateTraceRing(); + const auto sequenceBeforeLate = g_properShadersStateTraceSequence; + StoreProperShadersStateTraceRecord(&late); + Check(g_properShadersStateTraceSequence == sequenceBeforeLate, "late record from an earlier window cannot enter a new capture"); + ShutdownProperShadersStateRing(); + d.real->SetTexture(0, nullptr); + Check(SUCCEEDED(d.real->Reset(&d.pp)), "actual native device Reset succeeds after snapshots"); Boundary(); + queryCalls = 0; Check(ResolveD3D9RenderStateApi(&api) && queryCalls > 0, "reset lifecycle boundary revalidates methods"); + d.real->SetTexture(0, d.texture); +} +void NativePixel(Device& d) { + d.real->SetRenderState(D3DRS_LIGHTING, FALSE); d.real->SetRenderState(D3DRS_ZENABLE, FALSE); + d.real->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); d.real->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); + d.real->SetVertexShader(nullptr); d.real->SetPixelShader(nullptr); + d.real->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); + d.real->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE); + d.real->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); + d.real->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE); + d.real->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); + Bridge_ProperShadersPatchedRenderSceneGuard(); VerifyCorrected(d); + struct Vertex { float x,y,z,rhw; DWORD colour; }; + const Vertex triangle[]={{0,0,0,1,0xff00ff00},{63,0,0,1,0xff00ff00},{0,63,0,1,0xff00ff00}}; + d.real->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE); + Check(SUCCEEDED(d.real->Clear(0,nullptr,D3DCLEAR_TARGET,0xff000000,1,0)),"native pixel target clears"); + Check(SUCCEEDED(d.real->BeginScene()),"native pixel scene begins"); + Check(SUCCEEDED(d.real->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,triangle,sizeof(Vertex))),"native alpha-tested triangle draws"); + Check(SUCCEEDED(d.real->EndScene()),"native pixel scene ends"); + IDirect3DSurface9* target=nullptr; IDirect3DSurface9* copy=nullptr; D3DSURFACE_DESC desc{}; + HRESULT hr=d.real->GetRenderTarget(0,&target); + if(SUCCEEDED(hr))hr=target->GetDesc(&desc); + if(SUCCEEDED(hr))hr=d.real->CreateOffscreenPlainSurface(desc.Width,desc.Height,desc.Format,D3DPOOL_SYSTEMMEM,©,nullptr); + if(SUCCEEDED(hr))hr=d.real->GetRenderTargetData(target,copy); + D3DLOCKED_RECT lock{}; if(SUCCEEDED(hr))hr=copy->LockRect(&lock,nullptr,D3DLOCK_READONLY); + Check(SUCCEEDED(hr),"actual render pixels read back"); + if(SUCCEEDED(hr)){const DWORD pixel=*reinterpret_cast(static_cast(lock.pBits)+8*lock.Pitch+8*4);Check((pixel&0xffffff)==0x00ff00,"guard restoration preserves visible alpha-tested pixel");copy->UnlockRect();} + if(copy)copy->Release();if(target)target->Release(); +} +bool benchmarkMode; +void Benchmark(Device& d, unsigned rounds, unsigned iterations) { + benchmarkMode = true; + LARGE_INTEGER frequency{}; QueryPerformanceFrequency(&frequency); + const auto ref = d.texture ? d.texture->AddRef() : 0; if(d.texture)d.texture->Release(); + for (unsigned round=0;round0,"active benchmark completes full native snapshots"); +#if !defined(BASELINE_SOURCE) + if(mode!=2)Check(attempts==0,"idle benchmark admits no detailed snapshots"); +#endif + const double ns=double(end.QuadPart-begin.QuadPart)*1e9/frequency.QuadPart; + std::printf("{\"type\":\"guard-round\",\"pid\":%lu,\"round\":%u,\"mode\":%u,\"iterations\":%u,\"qpcBegin\":%llu,\"qpcEnd\":%llu,\"qpcFrequency\":%llu,\"wallNsPerCall\":%.9f,\"virtualQueries\":%llu,\"snapshotAttempts\":%u,\"textureGets\":%u}\n",GetCurrentProcessId(),round,mode,iterations,static_cast(begin.QuadPart),static_cast(end.QuadPart),static_cast(frequency.QuadPart),ns/iterations,queries,attempts,textureGets); + } + const auto after=d.texture?d.texture->AddRef():0;if(d.texture)d.texture->Release(); + Check(ref==after,"benchmark leaves actual texture reference count unchanged"); +} +} +int main(int argc, char** argv) { + std::filesystem::create_directories("scripts"); + const bool bench=argc>1 && std::strcmp(argv[1],"--benchmark")==0; + Device d(bench?(argc>2?argv[2]:nullptr):(argc>1?argv[1]:nullptr)); + Check(d.real != nullptr, "actual D3D9 HAL device created"); + if (d.real && bench) Benchmark(d,argc>3?std::strtoul(argv[3],nullptr,10):4,argc>4?std::strtoul(argv[4],nullptr,10):512); + else if (d.real) { NativeBehavior(d); NativePixel(d); } + std::printf("checks=%u failures=%u queryCalls=%llu\n", checks, failures, queryCalls); + return failures ? 1 : 0; +} diff --git a/tests/statering-runtime-tests.vcxproj b/tests/statering-runtime-tests.vcxproj new file mode 100644 index 0000000..41b0d59 --- /dev/null +++ b/tests/statering-runtime-tests.vcxproj @@ -0,0 +1,15 @@ + + + ReleaseWin32 + {655C26B6-81CE-4FDB-B0B1-684A55A7D14B} + + Applicationv145MultiByte + + $(ProjectDir)..\out\perf\20261027-statering-cache\candidate$(ProjectDir)..\src\bridge\legacy$(EvidenceRoot)\bin\$(EvidenceRoot)\obj\StateRingRuntimeTests + + MaxSpeedWIN32;NDEBUG;NOMINMAX;$(FixtureDefinitions);%(PreprocessorDefinitions)MultiThreadedstdcpplatest/utf-8 %(AdditionalOptions)$(GuardSourceRoot);$(ProjectDir)..\src\bridge\legacy;%(AdditionalIncludeDirectories) + Consoleuser32.lib;%(AdditionalDependencies) + + + + diff --git a/tests/streaming-config-regression-test.ps1 b/tests/streaming-config-regression-test.ps1 new file mode 100644 index 0000000..b043d3e --- /dev/null +++ b/tests/streaming-config-regression-test.ps1 @@ -0,0 +1,141 @@ +param([string]$LlvmMingwBin) + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +# This standalone consumer probe does not invoke Ninja, glslang, or Python. +$llvm = Find-RenderStackLlvmMingw -RepoRoot $root -LlvmMingwBin $LlvmMingwBin +$fixture = Join-Path $root ('out/test-fixtures/streaming-' + [guid]::NewGuid().ToString('N')) +[IO.Directory]::CreateDirectory($fixture) | Out-Null +$probe = Join-Path $fixture 'streaming-interval-probe.exe' +& $llvm.CompilerPath '-std=c++17' '-O2' '-msse2' '-static' ` + (Join-Path $PSScriptRoot 'streaming-interval-probe.cpp') '-o' $probe +if ($LASTEXITCODE -ne 0) { throw "Native consumer probe compile failed: $LASTEXITCODE" } +& $probe --self-test +if ($LASTEXITCODE -ne 0) { throw 'Native decision fragment checks failed' } +$tool = Join-Path $root 'tools/prepare-streaming-config.ps1' +$utf8 = [Text.UTF8Encoding]::new($false) +$checks = 0 +function Assert-Equal($Actual, $Expected, [string]$Name) { + if ($Actual -cne $Expected) { throw "FAIL $Name expected=[$Expected] actual=[$Actual]" } + $script:checks++ +} +function Read-Consumer([string]$Path) { + $result = & $probe $Path | Out-String + if ($LASTEXITCODE -ne 0) { throw "Consumer probe failed: $Path" } + return $result | ConvertFrom-Json +} +function Prepare([string]$Source, [string]$Output) { + $messages = & (Get-Command pwsh).Source -NoProfile -File $tool -Source $Source -OutputDirectory $Output 2>&1 | Out-String + return [pscustomobject]@{ ExitCode = $LASTEXITCODE; Messages = $messages } +} +$baseline = "[Settings]`r`nStreamMemoryForced = 2000`r`nDoubleStreamingMemoryLimit = 1`r`nMaxRAM = 3500`r`nPreLoadLODs = 1`r`n; RemoveUnusedIntervalMs = 99`r`nRemoveUnusedIntervalMs = 750 ; retain this comment`r`n[Range7]`r`nRemoveUnusedIntervalMs = 25`r`nKeepLoaded = 1`r`n" +$source = Join-Path $fixture 'baseline.ini' +[IO.File]::WriteAllText($source, $baseline, $utf8) +$sourceHash = (Get-FileHash -LiteralPath $source).Hash +$before = Read-Consumer $source +Assert-Equal $before.intervalMs 60 'original consumer falls back to default' +Assert-Equal $before.streamMemory 2000 'native stream budget reading' +Assert-Equal $before.doubleLimit 1 'native doubled accounting reading' +Assert-Equal $before.maxRam 3500 'native RAM cap reading' +Assert-Equal $before.preloadLods 1 'native LOD preload reading' +$output = Join-Path $fixture 'prepared' +$prepared = Prepare $source $output +Assert-Equal $prepared.ExitCode 0 'preparation succeeds' +$candidate = Join-Path $output 'ImprovedStreaming.ini' +$after = Read-Consumer $candidate +Assert-Equal $after.intervalMs 750 'candidate consumer reads intended interval' +Assert-Equal (Get-FileHash -LiteralPath $source).Hash $sourceHash 'source unchanged' +Assert-Equal ([IO.File]::ReadAllText($candidate)) ($baseline.Replace('RemoveUnusedIntervalMs = 750', 'RemoveUnusedInterval = 750')) 'only target key bytes change' +foreach ($key in @('streamMemory','doubleLimit','maxRam','preloadLods')) { + Assert-Equal $after.$key $before.$key "consumer preserves $key" +} +$manifest = Get-Content -LiteralPath (Join-Path $output 'preparation.json') -Raw | ConvertFrom-Json +Assert-Equal $manifest.applied $false 'manifest says not applied' +Assert-Equal $manifest.sourceSha256 $sourceHash 'manifest source identity' +Assert-Equal $manifest.candidateSha256 (Get-FileHash -LiteralPath $candidate).Hash 'manifest candidate identity' +Assert-Equal $manifest.intervalMs 750 'manifest effective interval' +Assert-Equal $manifest.changed $true 'manifest reports rename' + +# Idempotence and output-collision checks exercise the public preparation command. +$again = Join-Path $fixture 'idempotent' +Assert-Equal (Prepare $candidate $again).ExitCode 0 'canonical input accepted' +Assert-Equal (Get-FileHash -LiteralPath (Join-Path $again 'ImprovedStreaming.ini')).Hash ` + (Get-FileHash -LiteralPath $candidate).Hash 'idempotent bytes' +Assert-Equal ((Get-Content (Join-Path $again 'preparation.json') -Raw | ConvertFrom-Json).changed) $false 'idempotent manifest' +Assert-Equal ((Prepare $source $output).ExitCode -ne 0) $true 'existing output refused' +Assert-Equal (Get-FileHash -LiteralPath $candidate).Hash $manifest.candidateSha256 'collision leaves candidate unchanged' + +$invalid = [ordered]@{ + missing = "[Settings]`nMaxRAM=3500`n" + duplicate = "[Settings]`nRemoveUnusedIntervalMs=750`nRemoveUnusedIntervalMs=500`n" + conflict = "[Settings]`nRemoveUnusedIntervalMs=750`nRemoveUnusedInterval=60`n" + malformed = "[Settings]`nRemoveUnusedIntervalMs=750junk`n" + negative = "[Settings]`nRemoveUnusedIntervalMs=-1`n" + overflow = "[Settings]`nRemoveUnusedIntervalMs=2147483648`n" + wrongSection = "[Other]`nRemoveUnusedIntervalMs=750`n" + wrongCase = "[Settings]`nremoveunusedintervalms=750`n" + duplicateSection = "[Settings]`nRemoveUnusedIntervalMs=750`n[Settings]`nOther=1`n" + unicodeKeySpace = "[Settings]`n$([char]0x00A0)RemoveUnusedIntervalMs=750`n" + unicodeSectionSpace = "[$([char]0x2003)Settings]`nRemoveUnusedIntervalMs=750`n" + unicodeValueSpace = "[Settings]`nRemoveUnusedIntervalMs=$([char]0x00A0)750`n" +} +foreach ($entry in $invalid.GetEnumerator()) { + $path = Join-Path $fixture ($entry.Key + '.ini') + $destination = Join-Path $fixture ($entry.Key + '-output') + [IO.File]::WriteAllText($path, $entry.Value, $utf8) + $hash = (Get-FileHash -LiteralPath $path).Hash + Assert-Equal ((Prepare $path $destination).ExitCode -ne 0) $true "reject $($entry.Key)" + Assert-Equal (Test-Path -LiteralPath $destination) $false "no output on $($entry.Key)" + Assert-Equal (Get-FileHash -LiteralPath $path).Hash $hash "input preserved on $($entry.Key)" +} +foreach ($value in @('0','0x2EE')) { + $path = Join-Path $fixture ('value-' + $value + '.ini') + [IO.File]::WriteAllText($path, "[Settings]`nRemoveUnusedIntervalMs=$value`n", $utf8) + $destination = Join-Path $fixture ('value-' + $value) + Assert-Equal (Prepare $path $destination).ExitCode 0 "accept $value" + $expected = if ($value -ceq '0') {0} else {750} + Assert-Equal (Read-Consumer (Join-Path $destination 'ImprovedStreaming.ini')).intervalMs $expected "effective $value" +} +$bomSource = Join-Path $fixture 'bom.ini' +[IO.File]::WriteAllText($bomSource, $baseline.Replace("`r`n", "`n"), [Text.UTF8Encoding]::new($true)) +$bomOutput = Join-Path $fixture 'bom-output' +Assert-Equal (Prepare $bomSource $bomOutput).ExitCode 0 'UTF8 BOM accepted' +Assert-Equal ([Convert]::ToHexString([IO.File]::ReadAllBytes((Join-Path $bomOutput 'ImprovedStreaming.ini')))) ` + ([Convert]::ToHexString([Text.UTF8Encoding]::new($true).GetPreamble()) + [Convert]::ToHexString($utf8.GetBytes($baseline.Replace("`r`n", "`n").Replace('RemoveUnusedIntervalMs = 750','RemoveUnusedInterval = 750')))) 'BOM and LF preserved' +Assert-Equal (Read-Consumer (Join-Path $bomOutput 'ImprovedStreaming.ini')).intervalMs 750 'BOM native consumer' +$utf16Source = Join-Path $fixture 'utf16.ini' +[IO.File]::WriteAllText($utf16Source, $baseline, [Text.Encoding]::Unicode) +Assert-Equal ((Prepare $utf16Source (Join-Path $fixture 'utf16-output')).ExitCode -ne 0) $true 'UTF16 refused' +$badUtf8 = Join-Path $fixture 'invalid-utf8.ini' +[IO.File]::WriteAllBytes($badUtf8, [byte[]]@(0xC3,0x28)) +Assert-Equal ((Prepare $badUtf8 (Join-Path $fixture 'invalid-utf8-output')).ExitCode -ne 0) $true 'invalid UTF8 refused' +Assert-Equal (Test-Path -LiteralPath (Join-Path $fixture 'invalid-utf8-output')) $false 'no output for invalid UTF8' +Assert-Equal ((Prepare $source (Join-Path $root ('streaming-forbidden-' + [guid]::NewGuid().ToString('N')))).ExitCode -ne 0) $true 'output outside out refused' +Assert-Equal ((Prepare $source $fixture).ExitCode -ne 0) $true 'source directory refused' +Assert-Equal (Get-FileHash -LiteralPath $source).Hash $sourceHash 'source unchanged after all cases' + +# Repeated AB/BA replay through the real INI reader and relocated native gate. +# These are deterministic policy counts, deliberately not a CPU/FPS benchmark. +$samples = [Collections.Generic.List[object]]::new() +for ($round = 0; $round -lt 12; $round++) { + $order = if ($round % 2 -eq 0) {@('A','B')} else {@('B','A')} + foreach ($variant in $order) { + $path = if ($variant -ceq 'A') {$source} else {$candidate} + $sample = Read-Consumer $path + $expected = if ($variant -ceq 'A') {156} else {13} + Assert-Equal $sample.pressurePermits $expected "round $round $variant pressure permits" + Assert-Equal $sample.ramPermits 625 "round $round $variant emergency permits" + $samples.Add([ordered]@{round=$round; variant=$variant; intervalMs=$sample.intervalMs; + pressurePermits=$sample.pressurePermits; ramPermits=$sample.ramPermits}) + } +} +$report = [ordered]@{schemaVersion=1; kind='offline-policy-replay'; gameTest=$false; + fixture=$fixture; checks=$checks; rounds=12; simulatedDurationMs=10000; callbackStepMs=16; + sourceSha256=$sourceHash; candidateSha256=$manifest.candidateSha256; + nativeProbeSha256=(Get-FileHash -LiteralPath $probe).Hash; samples=@($samples.ToArray())} +[IO.File]::WriteAllText((Join-Path $fixture 'results.json'), ($report | ConvertTo-Json -Depth 6), $utf8) +Write-Output "PASS $checks preparation/consumer/replay assertions; 12 alternating A/B rounds" +Write-Output "Retained fixture and report: $fixture" diff --git a/tests/streaming-interval-probe.cpp b/tests/streaming-interval-probe.cpp new file mode 100644 index 0000000..03057c7 --- /dev/null +++ b/tests/streaming-interval-probe.cpp @@ -0,0 +1,114 @@ +// Offline consumer / decision-fragment probe. Never loads an ASI or the game. +#include +#include +#include +#include +#include +#include "vendor/loadwholemap-ini/IniReader.h" + +static_assert(sizeof(void*) == 4, "The captured decision fragment is x86"); + +class IntervalGate { + uint32_t now_ = 0, last_ = 0, interval_ = 0; + void* code_ = nullptr; +public: + IntervalGate() { + // ImprovedStreaming.SA.asi SHA256 D7BD005EDE20F00B358B75C4D4844618024F2A74AAF60957553EBD964BEE527C, + // RVA 0x7F4B..0x7F63. Only relocate the three data operands and the final + // jbe destination (to our false return); replace callees with bool returns. + // The original fragment and relocation offsets are recorded in README.md. + unsigned char bytes[] = { + 0xA1,0x84,0xCB,0xB7,0x00, + 0x2B,0x05,0xD0,0x08,0x05,0x10, + 0x3B,0x05,0xE4,0x08,0x05,0x10, + 0x77,0x06,0x66,0x0F,0x2F,0xC8,0x76,0x32, + 0xB8,0x01,0x00,0x00,0x00,0xC3,0x31,0xC0,0xC3 + }; + const uint32_t addresses[] = {uint32_t(uintptr_t(&now_)), + uint32_t(uintptr_t(&last_)), uint32_t(uintptr_t(&interval_))}; + const size_t offsets[] = {1, 7, 13}; + for (size_t i = 0; i < 3; ++i) std::memcpy(bytes + offsets[i], &addresses[i], 4); + bytes[24] = 6; + code_ = VirtualAlloc(nullptr, sizeof(bytes), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!code_) throw std::runtime_error("VirtualAlloc failed"); + std::memcpy(code_, bytes, sizeof(bytes)); + DWORD previous = 0; + if (!VirtualProtect(code_, sizeof(bytes), PAGE_EXECUTE_READ, &previous) || + !FlushInstructionCache(GetCurrentProcess(), code_, sizeof(bytes))) { + VirtualFree(code_, 0, MEM_RELEASE); + throw std::runtime_error("Cannot prepare decision fragment"); + } + } + IntervalGate(const IntervalGate&) = delete; + IntervalGate& operator=(const IntervalGate&) = delete; + ~IntervalGate() { VirtualFree(code_, 0, MEM_RELEASE); } + bool permits(uint32_t now, uint32_t last, uint32_t interval, double ram, double cap) { + now_ = now; last_ = last; interval_ = interval; + int result; + // Inputs match the installed callback at RVA 0x7F4B: xmm1=RAM, xmm0=cap. + asm volatile("movsd %[cap], %%xmm0\n\t" + "movsd %[ram], %%xmm1\n\t" + "call *%[entry]" + : "=a"(result) + : [entry]"r"(code_), [cap]"m"(cap), [ram]"m"(ram) + : "ecx", "edx", "xmm0", "xmm1", "cc", "memory"); + return result != 0; + } +}; + +static int replay(IntervalGate& gate, int interval, double ram) { + uint32_t last = 0; + int attempts = 0; + // A controlled 10-second, 16-ms callback trace, with pressure already true. + // Counts gate permits, not model evictions, I/O, conversion work, or FPS. + for (uint32_t now = 16; now <= 10000; now += 16) { + if (gate.permits(now, last, uint32_t(interval), ram, 3500.0)) { + ++attempts; + last = now; + } + } + return attempts; +} + +static void check(bool passed, const char* message) { + if (!passed) throw std::runtime_error(message); +} + +int main(int argc, char** argv) { + try { + IntervalGate gate; + if (argc == 2 && std::string(argv[1]) == "--self-test") { + check(!gate.permits(60, 0, 60, 1000, 3500), "60 equality"); + check(gate.permits(61, 0, 60, 1000, 3500), "60 plus one"); + check(!gate.permits(750, 0, 750, 1000, 3500), "750 equality"); + check(gate.permits(751, 0, 750, 1000, 3500), "750 plus one"); + check(!gate.permits(0, 0, 0, 1000, 3500), "zero same tick"); + check(gate.permits(1, 0, 0, 1000, 3500), "zero next tick"); + check(!gate.permits(1, 0, 750, 3500, 3500), "RAM equality"); + check(gate.permits(1, 0, 750, 3501, 3500), "RAM bypass"); + check(!gate.permits(0x10, 0xfffffff0u, 60, 1000, 3500), "wrap before interval"); + check(gate.permits(0x50, 0xfffffff0u, 60, 1000, 3500), "wrap after interval"); + check(replay(gate, 60, 1000) == 156, "baseline replay"); + check(replay(gate, 750, 1000) == 13, "candidate replay"); + check(replay(gate, 60, 3501) == 625, "baseline RAM replay"); + check(replay(gate, 750, 3501) == 625, "candidate RAM replay"); + std::cout << "PASS 14 native decision-fragment checks\n"; + return 0; + } + if (argc != 2) throw std::runtime_error("Usage: probe |--self-test"); + CIniReader reader(argv[1]); + const int interval = reader.ReadInteger("Settings", "RemoveUnusedInterval", 60); + // Also read unrelated budgets and flags through the real consumer. + std::cout << "{\"intervalMs\":" << interval + << ",\"streamMemory\":" << reader.ReadInteger("Settings", "StreamMemoryForced", -1) + << ",\"doubleLimit\":" << reader.ReadInteger("Settings", "DoubleStreamingMemoryLimit", -1) + << ",\"maxRam\":" << reader.ReadInteger("Settings", "MaxRAM", -1) + << ",\"preloadLods\":" << reader.ReadInteger("Settings", "PreLoadLODs", -1) + << ",\"pressurePermits\":" << replay(gate, interval, 1000) + << ",\"ramPermits\":" << replay(gate, interval, 3501) << "}\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/tests/texture-binding-ownership-test.py b/tests/texture-binding-ownership-test.py new file mode 100644 index 0000000..db6b964 --- /dev/null +++ b/tests/texture-binding-ownership-test.py @@ -0,0 +1,116 @@ +"""Check a linked x86 cache-hit path for redundant ownership work. + +This toolchain-specific cost guard supplements real D3D9/Vulkan lifecycle and +pixel tests. It does not establish resource correctness or a timing speedup. +It checks acyclic cache-hit paths; actual performance is measured separately. +The batch default covers the first dirty-slot iteration, not later loop visits. +""" +import argparse +import hashlib +import json +import re +import struct +import subprocess +from pathlib import Path + +PREPARE = "__ZN4dxvk12D3D9DeviceEx26PrepareGtaSaTextureBindingEmRKNS_2RcINS_13DxvkImageViewEEE" +BIND = "__ZN4dxvk12D3D9DeviceEx11BindTextureEm" +BATCH = "__ZN4dxvk12D3D9DeviceEx16BindTextureBatchEj" + + +def disassemble(tool, binary, symbol): + dump = subprocess.check_output([ + str(tool), "-d", "--no-show-raw-insn", "--x86-asm-syntax=intel", + "--disassemble-symbols=" + symbol, str(binary)], text=True) + code = [] + for line in dump.splitlines(): + match = re.match(r"\s*([0-9a-f]+):\s+(\w+)\s*(.*)", line) + if match: + code.append((int(match[1], 16), match[2], match[3])) + if not code: + raise ValueError("Missing linked symbol: " + symbol) + return code + + +def counter_offset(code): + matches = [] + for i, (_, op, args) in enumerate(code[:-1]): + match = re.fullmatch(r"dword ptr \[(e\w+) \+ (0x[0-9a-f]+)\], 0x1", args) + if op != "add" or not match: + continue + base, offset = match[1], int(match[2], 16) + if code[i + 1][1:] == ("adc", f"dword ptr [{base} + {hex(offset + 4)}], 0x0"): + matches.append(offset) + if len(matches) != 1: + raise ValueError("Cannot uniquely identify the real texture-cache hit counter") + return matches[0] + + +def reachable_costs(code, start, stop): + by_address = {row[0]: i for i, row in enumerate(code)} + pending = [(start, False, False)] + visited = set() + outcomes = set() + while pending: + address, atomic, call = pending.pop() + state = address, atomic, call + if state in visited or address not in by_address: + continue + visited.add(state) + if address == stop: + outcomes.add((atomic, call)) + continue + i = by_address[address] + _, op, args = code[i] + atomic |= op == "lock" or op.startswith("cmpxchg") or op == "xchg" + call |= op == "call" + if op in ("ret", "ud2", "int3"): + continue + successors = [] + if op.startswith("j"): + target = re.match(r"0x([0-9a-f]+)", args) + if target: + successors.append(int(target[1], 16)) + elif op == "jmp": + raise ValueError("Unreviewed indirect jump before cache-hit anchor") + if op != "jmp" and i + 1 < len(code): + successors.append(code[i + 1][0]) + # One straight-line iteration is sufficient to expose an unconditional + # retain before the decision. Do not model retry/outer-loop iterations. + pending.extend((target, atomic, call) for target in successors if target > address) + return outcomes + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("backend", type=Path) + parser.add_argument("--objdump", required=True, type=Path) + parser.add_argument("--report", type=Path) + parser.add_argument("--method", choices=("single", "batch"), default="batch") + args = parser.parse_args() + binary = args.backend.read_bytes() + pe = struct.unpack_from(" "preflight-$variant.log" 2> "preflight-$variant.err" + if($LASTEXITCODE){throw "Functional preflight failed: $variant; no performance result is accepted"} + } + $env:DXVK_CONFIG=$performanceConfig + for($pair=0;$pair -lt $Pairs;$pair++){ + $order=if($pair%2){@('candidate','baseline')}else{@('baseline','candidate')} + foreach($variant in $order){ + $path=if($variant -eq 'baseline'){$Baseline}else{$Candidate} + if($ObserverPair){$mode=if($variant -eq 'baseline'){'bench-off'}else{'bench'}} + $log=Join-Path $OutputDirectory "$pair-$variant.log";$env:DXVK_LOG_PATH=$OutputDirectory + & $Probe $path $mode $pair $Cases > $log 2> "$log.err" + if($LASTEXITCODE){throw "Benchmark process failed: $pair/$variant"} + $records=@(Get-Content -LiteralPath $log) + if(!($records -match '^checks=\d+ failures=0$')){throw 'Missing successful process footer'} + $expected=if($Cases -ne 'all'){@($Cases.Split(',')).Count*5}elseif($DisableCompat){15}else{45} + $bench=@($records|Where-Object {$_ -match '^bench '});$complete=@($records|Where-Object {$_ -match '^complete '}) + if($bench.Count -ne $expected -or $complete.Count -ne $expected){throw 'Missing/duplicate benchmark rows'} + foreach($line in $bench){ + if($line -notmatch '^bench case=(\S+) round=(\d+) count=(\d+) us=(\d+) observer=(\d+)$'){throw 'Invalid benchmark record'} + $case=$Matches[1];$round=[int]$Matches[2];$count=[int]$Matches[3];$us=[double]$Matches[4] + $completed=@($complete|Where-Object {$_ -match "^complete case=$case round=$round us=(\d+)$"}) + if($completed.Count -ne 1){throw 'Ambiguous completion record'} + $completed[0] -match 'us=(\d+)$'|Out-Null + $rows.Add([pscustomobject]@{pair=$pair;variant=$variant;case=$case;round=$round;count=$count;submitNs=$us*1000/$count;completeNs=[double]$Matches[1]*1000/$count}) + } + } + Write-Output "Completed pair $($pair+1)/$Pairs" + } +}finally{Pop-Location} +function Median($v){$s=@($v|Sort-Object);if($s.Count%2){$s[[int][Math]::Floor($s.Count/2)]}else{($s[$s.Count/2-1]+$s[$s.Count/2])/2}} +$summary=foreach($case in @($rows.case|Sort-Object -Unique)){ + foreach($metric in @('submitNs','completeNs')){ + $changes=for($pair=0;$pair -lt $Pairs;$pair++){ + $ratios=for($round=0;$round -lt 5;$round++){ + $a=@($rows|Where-Object {$_.pair -eq $pair -and $_.round -eq $round -and $_.case -eq $case -and $_.variant -eq 'baseline'}) + $b=@($rows|Where-Object {$_.pair -eq $pair -and $_.round -eq $round -and $_.case -eq $case -and $_.variant -eq 'candidate'}) + if($a.Count -ne 1 -or $b.Count -ne 1 -or $a[0].$metric -le 0){throw 'Invalid matched pair'} + 100*($b[0].$metric/$a[0].$metric-1) + };Median $ratios + } + [pscustomobject]@{case=$case;metric=$metric;baselineMedianNs=Median @($rows|Where-Object {$_.case -eq $case -and $_.variant -eq 'baseline'}|ForEach-Object {$_.$metric});candidateMedianNs=Median @($rows|Where-Object {$_.case -eq $case -and $_.variant -eq 'candidate'}|ForEach-Object {$_.$metric});pairedMedianChangePercent=Median $changes;fasterPairs=@($changes|Where-Object {$_ -lt 0}).Count;pairs=$Pairs;pairChanges=@($changes)} + } +} +$rows|ConvertTo-Json -Depth 4|Set-Content (Join-Path $OutputDirectory 'rows.json') +$summary|ConvertTo-Json -Depth 5|Set-Content (Join-Path $OutputDirectory 'summary.json') +if(@(Compare-Object $hashes @($Baseline,$Candidate,$Probe|Get-FileHash|Select-Object Path,Hash) -Property Path,Hash).Count){throw 'Binary hashes changed during benchmark'} +$summary|Format-Table case,metric,pairedMedianChangePercent,fasterPairs,pairs diff --git a/tests/thread-journal-probe.cpp b/tests/thread-journal-probe.cpp new file mode 100644 index 0000000..df7515a --- /dev/null +++ b/tests/thread-journal-probe.cpp @@ -0,0 +1,344 @@ +#include +#include +#include +#include "../sdk/include/sa_renderstack/backend_api.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" +#include +#include +#include +#include +#include + +// Independent ABI consumer: do not include the implementation's private header. +static const GUID ThreadStatsIID={0xd0967703,0x3642,0x49d2,{0x93,0xa8,0x91,0xa2,0xd1,0xe0,0x31,0x50}}; +struct ThreadSnapshot { + UINT StructSize,Version,MainThreadId,CsThreadId,AvailableMask,Reserved; + UINT64 WallUs,MainCpu100ns,CsCpu100ns,CsQueueIdleUs,OrderedDispatched,OrderedCompleted; + UINT64 CsSyncCount,CsSyncUs,GpuSyncCount,GpuSyncUs,GpuIdleUs,Chunks,Submits; +}; +struct ThreadStats : IUnknown { virtual HRESULT STDMETHODCALLTYPE GetThreadStats(ThreadSnapshot*)=0; }; +static unsigned checks,failures; +static unsigned privateDestructions; +static const GUID LifetimeIID={0x64443822,0xc10e,0x4c92,{0x92,0x30,0x73,0x42,0x10,0xaa,0x48,0x50}}; +struct Lifetime final : IUnknown { + ULONG refs=1; + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID id,void** out) override {if(!out)return E_POINTER;*out=nullptr;if(id!=__uuidof(IUnknown))return E_NOINTERFACE;*out=this;AddRef();return S_OK;} + ULONG STDMETHODCALLTYPE AddRef() override{return ++refs;} + ULONG STDMETHODCALLTYPE Release() override{const auto left=--refs;if(!left){++privateDestructions;delete this;}return left;} +}; +#define CHECK(c) do {++checks;if(!(c)){++failures;std::printf("FAIL line=%u %s\n",__LINE__,#c);}}while(0) +static UINT64 Now(){LARGE_INTEGER q,f;QueryPerformanceCounter(&q);QueryPerformanceFrequency(&f);return UINT64(q.QuadPart)*1000000/UINT64(f.QuadPart);} +static ThreadSnapshot Snap(ThreadStats* stats){ThreadSnapshot s{};s.StructSize=sizeof(s);s.Version=1;CHECK(stats->GetThreadStats(&s)==S_OK);return s;} +static void Print(const char* name,const ThreadSnapshot& a,const ThreadSnapshot& b){ + std::printf("activity case=%s wallUs=%llu mainCpuUs=%llu csCpuUs=%llu csQueueIdleUs=%llu dispatched=%llu completed=%llu pendingStart=%llu pendingEnd=%llu csWaitUs=%llu resourceWaitUs=%llu chunks=%llu submits=%llu\n",name, + b.WallUs-a.WallUs,(b.MainCpu100ns-a.MainCpu100ns)/10,(b.CsCpu100ns-a.CsCpu100ns)/10,b.CsQueueIdleUs-a.CsQueueIdleUs, + b.OrderedDispatched-a.OrderedDispatched,b.OrderedCompleted-a.OrderedCompleted,a.OrderedDispatched-a.OrderedCompleted,b.OrderedDispatched-b.OrderedCompleted, + b.CsSyncUs-a.CsSyncUs,b.GpuSyncUs-a.GpuSyncUs,b.Chunks-a.Chunks,b.Submits-a.Submits); +} +static void Modules(){ + auto h=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,GetCurrentProcessId());CHECK(h!=INVALID_HANDLE_VALUE); + MODULEENTRY32 e{};e.dwSize=sizeof(e);if(Module32First(h,&e))do{ + std::string name=e.szModule;for(auto& c:name)c=char(tolower(c)); + CHECK(name.find("propershaders")==std::string::npos&&name.find("cleo")==std::string::npos&&name.find("flacompat")==std::string::npos); + std::printf("module=%s\n",e.szExePath); + }while(Module32Next(h,&e));CloseHandle(h); +} +struct Vertex{float x,y,z,rhw;DWORD color;}; +static const Vertex Triangle[]={{0,0,.5f,1,0xff00ff00},{64,0,.5f,1,0xff00ff00},{0,64,.5f,1,0xff00ff00}}; +static void Draw(IDirect3DDevice9* d){d->BeginScene();d->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,Triangle,sizeof(Vertex));d->EndScene();} +static void Flush(IDirect3DDevice9* d){ + IDirect3DQuery9* q=nullptr;const auto created=d->CreateQuery(D3DQUERYTYPE_EVENT,&q);CHECK(created==S_OK&&q);if(!q)return; + const auto issued=q->Issue(D3DISSUE_END);CHECK(issued==S_OK);HRESULT result=issued; + const auto deadline=GetTickCount64()+20000; + if(SUCCEEDED(issued)){do{result=q->GetData(nullptr,0,D3DGETDATA_FLUSH);if(result==S_FALSE)Sleep(0);}while(result==S_FALSE&&GetTickCount64()Release(); +} +static void Work(IDirect3DDevice9* d,ID3D9GtaSaCompatDevice3* j,const std::string& mode,unsigned n){ + IDirect3DStateBlock9* block=nullptr; + const bool stateBlock=mode=="stateblock"||mode=="stateblock-tail"; + if(stateBlock) {d->BeginStateBlock();d->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);d->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP);if(mode=="stateblock-tail"){const float saved[4]={1,2,3,4};d->SetVertexShaderConstantF(255,saved,1);}d->EndStateBlock(&block);} + float first[256*4],second[256*4];for(unsigned k=0;k<1024;k++){first[k]=float(10000+k);second[k]=float(20000+k);} + for(unsigned i=0;iSetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);continue;} + if(mode=="draw"){Draw(d);continue;} + if(stateBlock){d->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);if(mode=="stateblock-tail"){const float changed[4]={float(i+10),7,8,9};d->SetVertexShaderConstantF(255,changed,1);}block->Apply();continue;} + if(mode=="capture-repeat"||mode=="capture-overlap"){ + if(j)j->BeginStateJournal(); + if(mode=="capture-repeat") {d->SetVertexShaderConstantF(0,first,256);d->SetVertexShaderConstantF(0,second,256);d->SetVertexShaderConstantF(0,first,256);} + else {d->SetVertexShaderConstantF(31,first,128);d->SetVertexShaderConstantF(0,second,64);d->SetVertexShaderConstantF(127,first,129);} + if(j)j->RestoreStateJournal();continue; + } + if(j)j->BeginStateJournal(); + if(mode!="empty"){ + d->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); + d->SetSamplerState(15,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP); + if(mode=="tail"||mode=="dense"||mode=="tail-draw"){ + float values[256*4];for(unsigned k=0;k<1024;k++)values[k]=float(i+k+100); + d->SetVertexShaderConstantF(mode!="dense"?255:0,values,mode!="dense"?1:256); + } + if(mode=="dense")for(unsigned s=0;s<16;s++)for(unsigned type=1;type<=13;type++)d->SetSamplerState(s,D3DSAMPLERSTATETYPE(type),1+(i&1)); + } + if(mode=="sparse-draw"||mode=="tail-draw")Draw(d); + if(j)j->RestoreStateJournal(); + }if(block)block->Release(); +} +static void OverlappingConstants(IDirect3DDevice9* d,ID3D9GtaSaCompatDevice3* journal,bool software){ + if(!journal)return; + const unsigned floats=software?8192:256, bools=software?2048:16; + std::vector initial(floats*4),changed(floats*4);for(unsigned i=0;iSetVertexShaderConstantF(s,p,n):d->SetPixelShaderConstantF(s,p,n);}; + auto si=[&](UINT s,const int* p,UINT n){return vertex?d->SetVertexShaderConstantI(s,p,n):d->SetPixelShaderConstantI(s,p,n);}; + auto sb=[&](UINT s,const BOOL* p,UINT n){return vertex?d->SetVertexShaderConstantB(s,p,n):d->SetPixelShaderConstantB(s,p,n);}; + CHECK(sf(0,initial.data(),fcount)==S_OK);CHECK(si(0,integers,16)==S_OK);CHECK(sb(0,boolean,bcount)==S_OK); + for(unsigned cycle=0;cycle<8;cycle++){ + CHECK(journal->BeginStateJournal()==S_OK); + // First write can be identical; later writes must still capture the original. + CHECK(sf(0,initial.data(),fcount)==S_OK); + for(auto range:{std::pair{31,65},{0,33},{63,65},{fcount-2,2},{32,32},{0,fcount},{1,fcount-2},{0,fcount}}) + CHECK(sf(range.first,changed.data(),range.second)==S_OK); + for(auto range:{std::pair{7,9},{0,8},{1,14},{0,16}})CHECK(si(range.first,otherInts,range.second)==S_OK); + if(bcount>32){CHECK(sb(31,otherBools,34)==S_OK);CHECK(sb(63,otherBools,65)==S_OK);} + CHECK(sb(bcount-1,otherBools,1)==S_OK);CHECK(sb(0,otherBools,bcount)==S_OK);CHECK(sb(1,boolean,bcount-2)==S_OK); + CHECK(journal->RestoreStateJournal()==S_OK); + std::vector result(fcount*4);int ri[64];std::vector rb(bcount); + // This backend's public getters preserve their hardware-window clamp, + // even on mixed devices. Exercise extended writes without changing it. + const unsigned readableFloats=std::min(fcount,vertex?256u:224u),readableBools=16; + CHECK((vertex?d->GetVertexShaderConstantF(0,result.data(),fcount):d->GetPixelShaderConstantF(0,result.data(),fcount))==S_OK&&!memcmp(result.data(),initial.data(),readableFloats*4*sizeof(float))); + CHECK((vertex?d->GetVertexShaderConstantI(0,ri,16):d->GetPixelShaderConstantI(0,ri,16))==S_OK&&!memcmp(ri,integers,sizeof(ri))); + CHECK((vertex?d->GetVertexShaderConstantB(0,rb.data(),bcount):d->GetPixelShaderConstantB(0,rb.data(),bcount))==S_OK&&!memcmp(rb.data(),boolean,readableBools*sizeof(BOOL))); + } + } +} +static void BindingPixels(IDirect3DDevice9* d,ID3D9GtaSaCompatDevice3* journal,bool verifyPropagation){ + IDirect3DTexture9* textures[2]={}; + for(unsigned i=0;i<2;i++){ + CHECK(d->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&textures[i],nullptr)==S_OK); + D3DLOCKED_RECT lock{};CHECK(textures[i]->LockRect(0,&lock,nullptr,0)==S_OK); + *static_cast(lock.pBits)=i?0xff202020:0xff808080;CHECK(textures[i]->UnlockRect(0)==S_OK); + } + struct TV{float x,y,z,rhw,u,v;}; + TV vertices[]={{0,0,.5f,1,2,2},{64,0,.5f,1,2,2},{0,64,.5f,1,2,2}}; + d->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1);d->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);d->SetRenderState(D3DRS_ZENABLE,FALSE); + d->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);d->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); + d->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);d->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); + d->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_POINT);d->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_POINT); + d->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP);d->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_WRAP); + IDirect3DSurface9* target=nullptr,*read=nullptr;CHECK(d->GetRenderTarget(0,&target)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(64,64,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&read,nullptr)==S_OK); + auto pixel=[&](DWORD expected){ + CHECK(d->BeginScene()==S_OK);CHECK(d->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,vertices,sizeof(TV))==S_OK);CHECK(d->EndScene()==S_OK); + CHECK(d->GetRenderTargetData(target,read)==S_OK);D3DLOCKED_RECT lock{};CHECK(read->LockRect(&lock,nullptr,D3DLOCK_READONLY)==S_OK); + const DWORD actual=*reinterpret_cast(static_cast(lock.pBits)+8*lock.Pitch+8*4); + for(unsigned shift:{0u,8u,16u,24u})CHECK(abs(int((actual>>shift)&255)-int((expected>>shift)&255))<=1); + CHECK(read->UnlockRect()==S_OK); + }; + d->SetTexture(0,textures[0]); + d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,0);pixel(0xff808080); + ID3D9BridgeDiagnostics* diagnostics=nullptr; + if(journal&&verifyPropagation){CHECK(d->QueryInterface(IID_D3D9BridgeDiagnostics,reinterpret_cast(&diagnostics))==S_OK);if(diagnostics){CHECK(diagnostics->SetCaptureOwner(TRUE)==S_OK);CHECK(diagnostics->StartAudit(0xdecaf,60000)==S_OK);}} + for(auto srgb:{0u,1u,3u,2u}){CHECK(d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,srgb)==S_OK);pixel(srgb&1?0xff373737:0xff808080);} + for(auto state:{D3DSAMP_BORDERCOLOR,D3DSAMP_ELEMENTINDEX,D3DSAMP_DMAPOFFSET}){ + const DWORD value=state==D3DSAMP_BORDERCOLOR?0xff00ff00:7; + CHECK(d->SetSamplerState(0,state,value)==S_OK);DWORD result=0;CHECK(d->GetSamplerState(0,state,&result)==S_OK&&result==value);pixel(0xff808080); + } + if(diagnostics){ + D3D9BridgeAuditSnapshot snapshot{};snapshot.StructSize=sizeof(snapshot);snapshot.Version=1; + CHECK(diagnostics->GetAudit(&snapshot)==S_OK);CHECK(snapshot.PrepareDraws==7); + CHECK(snapshot.Prepare[7].Units==0); // No actual sampler input changed. + CHECK(snapshot.Prepare[8].Units==2); // Only bit zero selects an sRGB view. + CHECK(diagnostics->StopAudit(0xdecaf)==S_OK);CHECK(diagnostics->SetCaptureOwner(FALSE)==S_OK);diagnostics->Release(); + } + d->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_BORDER);d->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_BORDER);pixel(0xff00ff00); + // With border already active, an sRGB-only view change must also dirty the + // sampler. Saturated pixels alone would not distinguish its view dependency. + if(journal&&verifyPropagation){ + CHECK(d->QueryInterface(IID_D3D9BridgeDiagnostics,reinterpret_cast(&diagnostics))==S_OK); + if(diagnostics){ + CHECK(diagnostics->SetCaptureOwner(TRUE)==S_OK);CHECK(diagnostics->StartAudit(0xdecaf,60000)==S_OK); + CHECK(d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,1)==S_OK);pixel(0xff00ff00); + D3D9BridgeAuditSnapshot snapshot{};snapshot.StructSize=sizeof(snapshot);snapshot.Version=1; + CHECK(diagnostics->GetAudit(&snapshot)==S_OK);CHECK(snapshot.Prepare[7].Units==1);CHECK(snapshot.Prepare[8].Units==1); + CHECK(diagnostics->StopAudit(0xdecaf)==S_OK);CHECK(diagnostics->SetCaptureOwner(FALSE)==S_OK);diagnostics->Release(); + } + } + CHECK(d->BeginStateBlock()==S_OK);d->SetSamplerState(0,D3DSAMP_BORDERCOLOR,0xff0000ff);d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,1); + IDirect3DStateBlock9* block=nullptr;CHECK(d->EndStateBlock(&block)==S_OK);CHECK(block->Apply()==S_OK);pixel(0xff0000ff); + if(journal){CHECK(journal->BeginStateJournal()==S_OK);d->SetTexture(0,textures[1]);d->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP);d->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_WRAP);d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,0);pixel(0xff202020);CHECK(journal->RestoreStateJournal()==S_OK);pixel(0xff0000ff);} + block->Release();d->SetTexture(0,nullptr);textures[0]->Release();textures[1]->Release();read->Release();target->Release(); + // A device binding can be the last private reference to the old resource. + for(unsigned cycle=0;cycle<8;cycle++){ + IDirect3DTexture9* old=nullptr,*next=nullptr; + CHECK(d->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&old,nullptr)==S_OK); + CHECK(d->CreateTexture(1,1,1,0,D3DFMT_X8R8G8B8,D3DPOOL_MANAGED,&next,nullptr)==S_OK); + const auto before=privateDestructions;auto* marker=new Lifetime;CHECK(old->SetPrivateData(LifetimeIID,marker,sizeof(IUnknown*),D3DSPD_IUNKNOWN)==S_OK);marker->Release(); + CHECK(d->SetTexture(0,old)==S_OK);old->Release();CHECK(privateDestructions==before); + CHECK(d->SetTexture(0,next)==S_OK);CHECK(privateDestructions==before+1);d->SetTexture(0,nullptr);next->Release(); + } + d->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP);d->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_WRAP);d->SetSamplerState(0,D3DSAMP_SRGBTEXTURE,0); +} +static void PrivateLifetime(IDirect3DDevice9* d,ID3D9GtaSaCompatDevice3* journal,D3DPRESENT_PARAMETERS pp){ + if(!journal)return; + for(unsigned cycle=0;cycle<3;cycle++){ + IDirect3DTexture9* first=nullptr,*last=nullptr;IDirect3DVertexBuffer9* vb=nullptr;IDirect3DIndexBuffer9* ib=nullptr; + CHECK(d->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&first,nullptr)==S_OK); + CHECK(d->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&last,nullptr)==S_OK); + CHECK(d->CreateVertexBuffer(64,0,0,D3DPOOL_MANAGED,&vb,nullptr)==S_OK); + CHECK(d->CreateIndexBuffer(16,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,nullptr)==S_OK); + const auto destroyedBefore=privateDestructions; + for(IDirect3DResource9* r:{static_cast(first),static_cast(last),static_cast(vb),static_cast(ib)}){ + auto* marker=new Lifetime;CHECK(r->SetPrivateData(LifetimeIID,marker,sizeof(IUnknown*),D3DSPD_IUNKNOWN)==S_OK);marker->Release(); + } + d->SetTexture(0,first);d->SetTexture(15,last);d->SetStreamSource(15,vb,8,16);d->SetIndices(ib); + CHECK(journal->BeginStateJournal()==S_OK); + d->SetTexture(15,nullptr); + if(cycle!=1){d->SetTexture(0,nullptr);d->SetStreamSource(15,nullptr,0,0);d->SetIndices(nullptr);} + if(cycle==2){CHECK(d->Reset(&pp)==S_OK);CHECK(journal->RestoreStateJournal()==D3DERR_INVALIDCALL);} + else{ + CHECK(journal->RestoreStateJournal()==S_OK); + IDirect3DBaseTexture9* restored=nullptr;CHECK(d->GetTexture(15,&restored)==S_OK&&restored==last);if(restored)restored->Release(); + } + d->SetTexture(0,nullptr);d->SetTexture(15,nullptr);d->SetStreamSource(15,nullptr,0,0);d->SetIndices(nullptr); + first->Release();last->Release();vb->Release();ib->Release();Flush(d); + CHECK(privateDestructions==destroyedBefore+4); + Work(d,journal,"empty",2); + } +} +static void Behavior(IDirect3DDevice9* d,ID3D9GtaSaCompatDevice3* journal){ + d->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);d->SetSamplerState(15,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP); + // Recording identical values must still populate a normal custom state block. + CHECK(d->BeginStateBlock()==S_OK);CHECK(d->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE)==S_OK); + IDirect3DStateBlock9* b=nullptr;CHECK(d->EndStateBlock(&b)==S_OK); + d->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);CHECK(b&&b->Apply()==S_OK);DWORD value=99; + CHECK(d->GetRenderState(D3DRS_ALPHABLENDENABLE,&value)==S_OK&&value==FALSE);if(b)b->Release(); + // The same word/range restoration runs for ordinary D3D9 StateBlocks, + // including when the GTA compatibility interface is disabled. + CHECK(d->BeginStateBlock()==S_OK); + const unsigned blockSlots[]={0,1,31,32,63,64,127,128,254,255}; + for(auto slot:blockSlots){const float saved[4]={float(slot+100),2,3,4};CHECK(d->SetVertexShaderConstantF(slot,saved,1)==S_OK);} + CHECK(d->EndStateBlock(&b)==S_OK); + for(auto slot:blockSlots){const float changed[4]={9,8,7,6};CHECK(d->SetVertexShaderConstantF(slot,changed,1)==S_OK);} + const float untouched[4]={700,701,702,703};d->SetVertexShaderConstantF(200,untouched,1); + CHECK(b->Apply()==S_OK); + for(auto slot:blockSlots){float restored[4];const float saved[4]={float(slot+100),2,3,4};CHECK(d->GetVertexShaderConstantF(slot,restored,1)==S_OK&&!memcmp(restored,saved,sizeof(saved)));} + float unaffected[4];CHECK(d->GetVertexShaderConstantF(200,unaffected,1)==S_OK&&!memcmp(unaffected,untouched,sizeof(untouched)));b->Release(); + IDirect3DTexture9* a=nullptr;CHECK(d->CreateTexture(1,1,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&a,nullptr)==S_OK); + auto refs=a->AddRef();a->Release(); + if(journal){ + CHECK(journal->RestoreStateJournal()==D3DERR_INVALIDCALL); + const float initial[4]={1,2,3,4};d->SetVertexShaderConstantF(255,initial,1); + for(unsigned i=0;i<100;i++){ + CHECK(journal->BeginStateJournal()==S_OK);CHECK(journal->BeginStateJournal()==D3DERR_INVALIDCALL); + CHECK(d->SetTexture(i&15,a)==S_OK);d->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); + float other[4]={9,8,7,6};d->SetVertexShaderConstantF(255,other,1); + CHECK(journal->RestoreStateJournal()==S_OK); + CHECK(d->GetRenderState(D3DRS_ALPHABLENDENABLE,&value)==S_OK&&value==FALSE); + float restored[4];CHECK(d->GetVertexShaderConstantF(255,restored,1)==S_OK&&!memcmp(restored,initial,sizeof(initial))); + IDirect3DBaseTexture9* texture=nullptr;CHECK(d->GetTexture(i&15,&texture)==S_OK&&texture==nullptr);if(texture)texture->Release(); + } + CHECK(a->AddRef()==refs);a->Release(); + IDirect3DVertexBuffer9* vertexBuffer=nullptr;IDirect3DIndexBuffer9* indexBuffer=nullptr; + CHECK(d->CreateVertexBuffer(64,0,0,D3DPOOL_MANAGED,&vertexBuffer,nullptr)==S_OK); + CHECK(d->CreateIndexBuffer(16,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,nullptr)==S_OK); + const auto vbRefs=vertexBuffer->AddRef();vertexBuffer->Release(); + const auto ibRefs=indexBuffer->AddRef();indexBuffer->Release(); + for(unsigned slot=0;slot<16;slot++){ + CHECK(journal->BeginStateJournal()==S_OK); + CHECK(d->SetStreamSource(slot,vertexBuffer,8,16)==S_OK);CHECK(d->SetIndices(indexBuffer)==S_OK); + CHECK(journal->RestoreStateJournal()==S_OK); + IDirect3DVertexBuffer9* restored=nullptr;UINT offset=99,stride=99; + CHECK(d->GetStreamSource(slot,&restored,&offset,&stride)==S_OK&&restored==nullptr); + if(restored)restored->Release();IDirect3DIndexBuffer9* restoredIndex=nullptr; + CHECK(d->GetIndices(&restoredIndex)==S_OK&&restoredIndex==nullptr);if(restoredIndex)restoredIndex->Release(); + } + CHECK(vertexBuffer->AddRef()==vbRefs);vertexBuffer->Release();vertexBuffer->Release(); + CHECK(indexBuffer->AddRef()==ibRefs);indexBuffer->Release();indexBuffer->Release(); + // Cross-word, holes, tail and consecutive ranges exercise restoration of + // exactly the captured registers rather than the enclosing interval. + const unsigned slots[]={0,1,30,31,32,33,63,64,127,128,254,255}; + float initialRanges[256*4];for(unsigned k=0;k<1024;k++)initialRanges[k]=float(k+2000); + CHECK(d->SetVertexShaderConstantF(0,initialRanges,256)==S_OK); + for(unsigned cycle=0;cycle<12;cycle++){ + CHECK(journal->BeginStateJournal()==S_OK); + for(auto slot:slots){float changed[4]={float(slot+cycle),0,0,0};CHECK(d->SetVertexShaderConstantF(slot,changed,1)==S_OK);} + CHECK(journal->RestoreStateJournal()==S_OK);float restored[256*4]; + CHECK(d->GetVertexShaderConstantF(0,restored,256)==S_OK&&!memcmp(restored,initialRanges,sizeof(restored))); + } + Work(d,journal,"dense",2);Work(d,journal,"sparse",2);Work(d,journal,"empty",2); + CHECK(d->GetSamplerState(15,D3DSAMP_ADDRESSU,&value)==S_OK&&value==D3DTADDRESS_WRAP); + } + a->Release();d->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);d->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_DIFFUSE); + d->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);d->SetRenderState(D3DRS_ZENABLE,FALSE);d->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE); + d->Clear(0,nullptr,D3DCLEAR_TARGET,0xff000000,1,0);Draw(d); + IDirect3DSurface9* rt=nullptr,*read=nullptr;CHECK(d->GetRenderTarget(0,&rt)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(64,64,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&read,nullptr)==S_OK); + CHECK(d->GetRenderTargetData(rt,read)==S_OK);D3DLOCKED_RECT lock{};CHECK(read->LockRect(&lock,nullptr,D3DLOCK_READONLY)==S_OK); + CHECK((*reinterpret_cast(static_cast(lock.pBits)+8*lock.Pitch+8*4)&0xffffff)==0x00ff00); + read->UnlockRect();read->Release();rt->Release(); +} +int main(int argc,char** argv){ + if(argc<2)return 2;const std::string mode=argc>2?argv[2]:"check";const bool software=mode=="check-software";const bool check=mode=="check"||mode=="check-optional-stats"||software;auto module=LoadLibraryA(argv[1]);CHECK(module!=nullptr);if(!module)return 2; + auto create=reinterpret_cast(GetProcAddress(module,"Direct3DCreate9"));CHECK(create);if(!create)return 2; + WNDCLASSA wc{};wc.lpfnWndProc=DefWindowProcA;wc.hInstance=GetModuleHandleA(nullptr);wc.lpszClassName="ThreadJournalProbe";RegisterClassA(&wc); + auto window=CreateWindowA(wc.lpszClassName,"Independent D3D9 fixture",WS_OVERLAPPEDWINDOW,0,0,64,64,nullptr,nullptr,wc.hInstance,nullptr); + auto api=create(D3D_SDK_VERSION);CHECK(api);if(!api)return 2;D3DPRESENT_PARAMETERS pp{}; + pp.Windowed=TRUE;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.hDeviceWindow=window;pp.BackBufferWidth=pp.BackBufferHeight=64;pp.BackBufferFormat=D3DFMT_A8R8G8B8;pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* d=nullptr;CHECK(api->CreateDevice(0,D3DDEVTYPE_HAL,window,software?D3DCREATE_MIXED_VERTEXPROCESSING:D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&d)==S_OK);if(!d)return 2; + if(software)CHECK(d->SetSoftwareVertexProcessing(TRUE)==S_OK); + ThreadStats* stats=nullptr;auto statResult=d->QueryInterface(ThreadStatsIID,reinterpret_cast(&stats)); + if(mode=="check"){CHECK(statResult==S_OK&&stats);} + ID3D9GtaSaCompatDevice3* journal=nullptr;d->QueryInterface(__uuidof(ID3D9GtaSaCompatDevice3),reinterpret_cast(&journal)); + std::printf("fixture executable=thread-journal-probe compat=%u privateStats=%u\n",unsigned(journal!=nullptr),unsigned(stats!=nullptr)); + if(check){ + Modules();OverlappingConstants(d,journal,software);BindingPixels(d,journal,mode!="check-optional-stats");Behavior(d,journal);PrivateLifetime(d,journal,pp);Flush(d);Sleep(20); + if(stats){ + CHECK(stats->GetThreadStats(nullptr)==D3DERR_INVALIDCALL);ThreadSnapshot invalid{};invalid.StructSize=sizeof(invalid)-1;invalid.Version=1; + CHECK(stats->GetThreadStats(&invalid)==D3DERR_INVALIDCALL);invalid.StructSize=sizeof(invalid);invalid.Version=999;CHECK(stats->GetThreadStats(&invalid)==D3DERR_INVALIDCALL); + auto a=Snap(stats);Sleep(100);auto b=Snap(stats);Print("idle",a,b); + CHECK(b.MainThreadId==GetCurrentThreadId()&&b.CsThreadId&&b.CsThreadId!=b.MainThreadId&&b.AvailableMask==7); + CHECK(b.WallUs-a.WallUs>=80000&&b.CsQueueIdleUs-a.CsQueueIdleUs>=70000); + auto end=Now()+100000;while(Now()SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);}auto c=Snap(stats);Print("main-busy",b,c); + CHECK(c.MainCpu100ns>b.MainCpu100ns+100000); // >=10ms scheduled CPU + d->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);Work(d,journal,"draw",150000); IDirect3DSurface9* rt=nullptr,*read=nullptr; d->GetRenderTarget(0,&rt); d->CreateOffscreenPlainSurface(64,64,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&read,nullptr); CHECK(d->GetRenderTargetData(rt,read)==S_OK); D3DLOCKED_RECT waited{}; CHECK(read->LockRect(&waited,nullptr,D3DLOCK_READONLY)==S_OK); read->UnlockRect(); read->Release(); rt->Release(); Flush(d);auto e=Snap(stats);Print("draw",c,e); + CHECK(e.OrderedDispatched>c.OrderedDispatched&&e.OrderedCompleted>c.OrderedCompleted&&e.CsCpu100ns>c.CsCpu100ns); + CHECK(e.OrderedCompleted<=e.OrderedDispatched); + CHECK(e.CsSyncCount>c.CsSyncCount&&e.CsSyncUs>c.CsSyncUs); + CHECK(e.GpuSyncCount>c.GpuSyncCount&&e.GpuSyncUs>c.GpuSyncUs); + const auto before=Snap(stats);for(unsigned i=0;i<100;i++)Snap(stats);const auto after=Snap(stats); + CHECK(after.OrderedDispatched==before.OrderedDispatched&&after.Submits==before.Submits); + CHECK(d->Reset(&pp)==S_OK);auto reset=Snap(stats);CHECK(reset.MainThreadId==e.MainThreadId&&reset.WallUs>=e.WallUs); + } + }else{ + d->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);d->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);Flush(d); + std::vector cases={"control","stateblock","draw"};if(journal)cases.insert(cases.end(),{"empty","sparse","tail","dense","sparse-draw","tail-draw"}); + if(argc>3)std::rotate(cases.begin(),cases.begin()+(std::strtoul(argv[3],nullptr,10)%cases.size()),cases.end()); + const bool observe=stats&&mode!="bench-off"; + if(argc>4&&std::string(argv[4])!="all"){ + const std::string selected=","+std::string(argv[4])+","; + if(selected.find(",stateblock-tail,")!=std::string::npos)cases.push_back("stateblock-tail"); + for(auto extra:{"capture-repeat","capture-overlap"})if(selected.find(","+std::string(extra)+",")!=std::string::npos)cases.push_back(extra); + cases.erase(std::remove_if(cases.begin(),cases.end(),[&](const auto& c){return selected.find(","+c+",")==std::string::npos;}),cases.end()); + } + DWORD_PTR processMask=0,systemMask=0;GetProcessAffinityMask(GetCurrentProcess(),&processMask,&systemMask); + const DWORD_PTR affinity=(processMask&4)?4:(processMask&(~processMask+1)); + CHECK(affinity&&SetThreadAffinityMask(GetCurrentThread(),affinity));std::printf("fixtureMainAffinityMask=%llu\n",UINT64(affinity)); + for(const auto& test:cases){Work(d,journal,test,200);Flush(d); + for(unsigned round=0;round<5;round++){ + const auto start=Now();const auto a=observe?Snap(stats):ThreadSnapshot{}; + const unsigned n=test=="control"?4000000:test.find("draw")!=std::string::npos?50000:200000; + Work(d,journal,test,n);const auto b=observe?Snap(stats):ThreadSnapshot{};const auto stop=Now(); + Flush(d);const auto completed=Now(); + std::printf("bench case=%s round=%u count=%u us=%llu observer=%u\n",test.c_str(),round,n,stop-start,unsigned(observe)); + std::printf("complete case=%s round=%u us=%llu\n",test.c_str(),round,completed-start); + if(observe)Print(test.c_str(),a,b); + } + } + } + if(stats)stats->Release();if(journal)journal->Release();CHECK(d->Release()==0);api->Release();DestroyWindow(window);FreeLibrary(module); + std::printf("checks=%u failures=%u\n",checks,failures);return failures?1:0; +} diff --git a/tests/thread-journal-test.ps1 b/tests/thread-journal-test.ps1 new file mode 100644 index 0000000..9bedc7c --- /dev/null +++ b/tests/thread-journal-test.ps1 @@ -0,0 +1,19 @@ +param([string]$Backend,[string]$OutputDirectory,[string]$LlvmMingwBin,[switch]$DisableCompat,[switch]$CompileOnly,[string]$Mode='check') +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/perf/20261031-thread-journal/native'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$tc=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$exe=Join-Path $OutputDirectory 'thread-journal-probe.exe' +& $tc.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'thread-journal-probe.cpp') '-I' (Join-Path $root 'backend/dxvk/include/vulkan/include') '-o' $exe '-luuid' '-luser32' +if($LASTEXITCODE){throw 'thread/journal probe compile failed'} +if($CompileOnly){return} +$env:DXVK_CONFIG="d3d9.gtaSaCompat = $(!$DisableCompat); d3d9.gtaSaStateBlockPrefilter = True; d3d9.gtaSaCompatDiagnostics = False; dxvk.hud = 0" +$env:DXVK_HUD='';$env:DXVK_LOG_PATH=$OutputDirectory +$backendPath=(Resolve-Path -LiteralPath $Backend).Path +Push-Location $OutputDirectory +try{& $exe $backendPath $Mode 2>&1|Tee-Object (Join-Path $OutputDirectory 'test.log');$code=$LASTEXITCODE}finally{Pop-Location} +if($code){throw "thread/journal fixture failed: $code"} diff --git a/tests/upload-mapping-probe.cpp b/tests/upload-mapping-probe.cpp new file mode 100644 index 0000000..7f31c07 --- /dev/null +++ b/tests/upload-mapping-probe.cpp @@ -0,0 +1,407 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include +#include +#include +#include "lib/win32-import-hook.h" +#include "lib/benchmark-thread-affinity.h" + +static unsigned checks; +static unsigned failures; +#define CHECK(condition) do { ++checks; if (!(condition)) { ++failures; std::printf("FAIL line=%u %s\n", __LINE__, #condition); } } while (0) + +using MapView = decltype(&MapViewOfFile); +using UnmapView = decltype(&UnmapViewOfFile); +static MapView originalMap; +static UnmapView originalUnmap; +static DWORD ownerThread; +static bool countCalls; +static unsigned maps; +static unsigned unmaps; +static void* coldView; +static bool coldViewReclaimed; +static const unsigned char* uploadView; +static SIZE_T uploadViewBytes; +static unsigned copiesAfterColdReclaim; +using CopyMemory = void* (__cdecl*)(void*, const void*, size_t); +static CopyMemory originalCopy; + +static LPVOID WINAPI CountMap(HANDLE file, DWORD access, DWORD high, DWORD low, SIZE_T size) { + auto result = originalMap(file, access, high, low, size); + if (GetCurrentThreadId() == ownerThread && countCalls && result) + ++maps; + if (GetCurrentThreadId() == ownerThread && countCalls && coldView && result) { + uploadView = static_cast(result); + uploadViewBytes = size; + } + return result; +} + +static BOOL WINAPI CountUnmap(LPCVOID address) { + const auto result = originalUnmap(address); + if (GetCurrentThreadId() == ownerThread && countCalls && result) + ++unmaps; + if (GetCurrentThreadId() == ownerThread && countCalls && address == coldView && result) + coldViewReclaimed = true; + return result; +} + +static void* __cdecl CountCopy(void* destination, const void* source, size_t bytes) { + if (GetCurrentThreadId() == ownerThread && countCalls && coldViewReclaimed && bytes >= 4096) { + const auto address = reinterpret_cast(source); + const auto first = reinterpret_cast(uploadView); + if (address >= first && address - first < uploadViewBytes) + ++copiesAfterColdReclaim; + } + return originalCopy(destination, source, bytes); +} + +static double Milliseconds() { + LARGE_INTEGER tick, frequency; + QueryPerformanceCounter(&tick); + QueryPerformanceFrequency(&frequency); + return double(tick.QuadPart) * 1000 / double(frequency.QuadPart); +} + +static void Flush(IDirect3DDevice9* device) { + IDirect3DQuery9* query = nullptr; + CHECK(device->CreateQuery(D3DQUERYTYPE_EVENT, &query) == S_OK); + if (!query) + return; + CHECK(query->Issue(D3DISSUE_END) == S_OK); + HRESULT result = S_FALSE; + const auto deadline = GetTickCount64() + 15000; + while (result == S_FALSE && GetTickCount64() < deadline) + result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH); + CHECK(result == S_OK); + query->Release(); +} + +static DWORD Color(unsigned level, unsigned version) { + return 0xff000000u | ((17 + level * 11 + version * 23) << 16) + | ((31 + level * 7 + version * 19) << 8) | (47 + level * 3 + version * 13); +} + +static void Fill(IDirect3DTexture9* texture, unsigned version) { + for (unsigned level = 0; level < texture->GetLevelCount(); ++level) { + D3DSURFACE_DESC desc{}; + CHECK(texture->GetLevelDesc(level, &desc) == S_OK); + D3DLOCKED_RECT lock{}; + CHECK(texture->LockRect(level, &lock, nullptr, 0) == S_OK); + if (!lock.pBits) + return; + for (unsigned row = 0; row < desc.Height; ++row) + std::fill_n(reinterpret_cast(static_cast(lock.pBits) + row * lock.Pitch), + desc.Width, Color(level, version)); + CHECK(texture->UnlockRect(level) == S_OK); + } +} + +struct Vertex { + float x, y, z, rhw, u, v; +}; + +static void Pixels(IDirect3DDevice9* device, IDirect3DTexture9* texture, unsigned version, bool partial = false) { + IDirect3DSurface9* target = nullptr; + IDirect3DSurface9* readback = nullptr; + CHECK(device->GetRenderTarget(0, &target) == S_OK); + CHECK(device->CreateOffscreenPlainSurface(32, 32, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &readback, nullptr) == S_OK); + if (!target || !readback) + return; + CHECK(device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1) == S_OK); + CHECK(device->SetRenderState(D3DRS_ZENABLE, FALSE) == S_OK); + CHECK(device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) == S_OK); + CHECK(device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1) == S_OK); + CHECK(device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE) == S_OK); + CHECK(device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1) == S_OK); + CHECK(device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE) == S_OK); + CHECK(device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT) == S_OK); + CHECK(device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT) == S_OK); + CHECK(device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT) == S_OK); + CHECK(device->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 0xc1800000u) == S_OK); // -16.0f + CHECK(device->SetTexture(0, texture) == S_OK); + const Vertex vertices[] = {{0,0,.5f,1,0,0},{64,0,.5f,1,2,0},{0,64,.5f,1,0,2}}; + for (unsigned level = 0; level < texture->GetLevelCount(); ++level) { + CHECK(device->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, level) == S_OK); + CHECK(device->BeginScene() == S_OK); + CHECK(device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex)) == S_OK); + CHECK(device->EndScene() == S_OK); + CHECK(device->GetRenderTargetData(target, readback) == S_OK); + D3DLOCKED_RECT lock{}; + CHECK(readback->LockRect(&lock, nullptr, D3DLOCK_READONLY) == S_OK); + if (lock.pBits) { + const DWORD expected = partial && level == 0 ? 0xff112233 : Color(level, version); + CHECK(*reinterpret_cast(static_cast(lock.pBits) + 8 * lock.Pitch + 8 * 4) + == expected); + // A sample outside the modified rectangle must retain its original content. + CHECK(*reinterpret_cast(static_cast(lock.pBits) + 8 * lock.Pitch + 24 * 4) + == Color(level, version)); + CHECK(readback->UnlockRect() == S_OK); + } + } + CHECK(device->SetTexture(0, nullptr) == S_OK); + target->Release(); + readback->Release(); +} + +static void Upload(IDirect3DTexture9* source, IDirect3DTexture9* destination, IDirect3DDevice9* device) { + if (destination) + CHECK(device->UpdateTexture(source, destination) == S_OK); + else + source->PreLoad(); +} + +static void Exercise(IDirect3DDevice9* device, bool managed, bool benchmark, bool observe, + unsigned budget, unsigned levels, unsigned iterations) { + IDirect3DTexture9* source = nullptr; + IDirect3DTexture9* destination = nullptr; + CHECK(device->CreateTexture(1024, 1024, levels, 0, D3DFMT_A8R8G8B8, + managed ? D3DPOOL_MANAGED : D3DPOOL_SYSTEMMEM, &source, nullptr) == S_OK); + if (!managed) + CHECK(device->CreateTexture(1024, 1024, levels, 0, D3DFMT_A8R8G8B8, + D3DPOOL_DEFAULT, &destination, nullptr) == S_OK); + if (!source || (!managed && !destination)) + return; + Fill(source, 0); + Flush(device); + maps = unmaps = 0; + countCalls = true; + Upload(source, destination, device); + countCalls = false; + std::printf("mapping case=%s levels=%u maps=%u unmaps=%u budgetMiB=%u\n", + managed ? "managed" : "systemmem", static_cast(source->GetLevelCount()), maps, unmaps, budget); + if (!benchmark && !observe && budget == 1) { + // One source mapping must survive the complete mip-chain transaction. + CHECK(maps == 1); + // The oversized source must still be reclaimed at the transaction end. + CHECK(unmaps == 1); + } + if (!benchmark) { + Pixels(device, destination ? destination : source, 0); + Fill(source, 1); + CHECK(source->AddDirtyRect(nullptr) == S_OK); + Upload(source, destination, device); + Pixels(device, destination ? destination : source, 1); + const RECT rectangle{0, 0, 512, 512}; + D3DLOCKED_RECT lock{}; + CHECK(source->LockRect(0, &lock, &rectangle, 0) == S_OK); + if (lock.pBits) { + for (unsigned y = 0; y < 512; ++y) + std::fill_n(reinterpret_cast(static_cast(lock.pBits) + y * lock.Pitch), + 512, 0xff112233); + CHECK(source->UnlockRect(0) == S_OK); + } + Upload(source, destination, device); + Pixels(device, destination ? destination : source, 1, true); + if (managed) { + D3DPRESENT_PARAMETERS params{}; + IDirect3DSwapChain9* swap = nullptr; + CHECK(device->GetSwapChain(0, &swap) == S_OK); + if (swap) { + CHECK(swap->GetPresentParameters(¶ms) == S_OK); + swap->Release(); + Flush(device); + CHECK(device->Reset(¶ms) == S_OK); + source->PreLoad(); + Pixels(device, source, 1, true); + } + } + } else { + // Warm driver clocks and upload throttling before the measured batch. + for (unsigned i = 0; i < 64; ++i) { + CHECK(source->AddDirtyRect(nullptr) == S_OK); + Upload(source, destination, device); + } + Flush(device); + BenchmarkThreadAffinity affinity; + CHECK(affinity.valid()); + maps = unmaps = 0; + countCalls = true; + ULONG64 cyclesBefore = 0, cyclesAfter = 0; + CHECK(QueryThreadCycleTime(GetCurrentThread(), &cyclesBefore)); + const auto start = Milliseconds(); + for (unsigned i = 0; i < iterations; ++i) { + CHECK(source->AddDirtyRect(nullptr) == S_OK); + Upload(source, destination, device); + } + const double elapsed = Milliseconds() - start; + CHECK(QueryThreadCycleTime(GetCurrentThread(), &cyclesAfter)); + countCalls = false; + std::printf("benchmark case=%s levels=%u iterations=%u elapsedMs=%.6f mainCycles=%llu maps=%u unmaps=%u budgetMiB=%u\n", + managed ? "managed" : "systemmem", static_cast(source->GetLevelCount()), + iterations, elapsed, static_cast(cyclesAfter - cyclesBefore), maps, unmaps, budget); + Flush(device); + } + if (destination) + destination->Release(); + source->Release(); + Flush(device); +} + +static void CubeFaces(IDirect3DDevice9* device, bool observe, unsigned budget) { + IDirect3DCubeTexture9* source = nullptr; + IDirect3DCubeTexture9* destination = nullptr; + CHECK(device->CreateCubeTexture(512, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &source, nullptr) == S_OK); + CHECK(device->CreateCubeTexture(512, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &destination, nullptr) == S_OK); + if (!source || !destination) + return; + for (unsigned face = 0; face < 6; ++face) { + for (unsigned level = 0; level < source->GetLevelCount(); ++level) { + D3DSURFACE_DESC desc{}; + CHECK(source->GetLevelDesc(level, &desc) == S_OK); + D3DLOCKED_RECT lock{}; + CHECK(source->LockRect(static_cast(face), level, &lock, nullptr, 0) == S_OK); + if (lock.pBits) { + for (unsigned y = 0; y < desc.Height; ++y) + std::fill_n(reinterpret_cast(static_cast(lock.pBits) + y * lock.Pitch), + desc.Width, Color(level, face)); + CHECK(source->UnlockRect(static_cast(face), level) == S_OK); + } + } + } + Flush(device); + maps = unmaps = 0; + countCalls = true; + CHECK(device->UpdateTexture(source, destination) == S_OK); + countCalls = false; + std::printf("mapping case=cube maps=%u unmaps=%u budgetMiB=%u\n", maps, unmaps, budget); + if (!observe && budget == 1) { + CHECK(maps == 1); + CHECK(unmaps == 1); + } + IDirect3DSurface9* target = nullptr; + IDirect3DSurface9* readback = nullptr; + CHECK(device->GetRenderTarget(0, &target) == S_OK); + CHECK(device->CreateOffscreenPlainSurface(32, 32, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &readback, nullptr) == S_OK); + for (unsigned face = 0; face < 6; ++face) { + for (unsigned level = 0; level < destination->GetLevelCount(); ++level) { + IDirect3DSurface9* surface = nullptr; + CHECK(destination->GetCubeMapSurface(static_cast(face), level, &surface) == S_OK); + CHECK(device->StretchRect(surface, nullptr, target, nullptr, D3DTEXF_POINT) == S_OK); + surface->Release(); + CHECK(device->GetRenderTargetData(target, readback) == S_OK); + D3DLOCKED_RECT lock{}; + CHECK(readback->LockRect(&lock, nullptr, D3DLOCK_READONLY) == S_OK); + if (lock.pBits) { + CHECK(*reinterpret_cast(lock.pBits) == Color(level, face)); + CHECK(readback->UnlockRect() == S_OK); + } + } + } + readback->Release(); + target->Release(); + destination->Release(); + source->Release(); + Flush(device); +} + +static void ReclaimOtherMappings(IDirect3DDevice9* device) { + IDirect3DTexture9* source = nullptr; + IDirect3DTexture9* cold = nullptr; + CHECK(device->CreateTexture(1024, 1024, 0, 0, D3DFMT_A8R8G8B8, + D3DPOOL_MANAGED, &source, nullptr) == S_OK); + CHECK(device->CreateTexture(64, 64, 1, 0, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &cold, nullptr) == S_OK); + Fill(source, 0); + D3DLOCKED_RECT lock{}; + CHECK(cold->LockRect(0, &lock, nullptr, 0) == S_OK); + MEMORY_BASIC_INFORMATION memory{}; + CHECK(VirtualQuery(lock.pBits, &memory, sizeof(memory)) == sizeof(memory)); + coldView = memory.AllocationBase; + CHECK(cold->UnlockRect(0) == S_OK); + coldViewReclaimed = false; + uploadView = nullptr; + uploadViewBytes = 0; + copiesAfterColdReclaim = 0; + countCalls = true; + source->PreLoad(); + countCalls = false; + CHECK(coldViewReclaimed); + // Other cold mappings must be reclaimed before later mips are copied. + CHECK(copiesAfterColdReclaim > 0); + coldView = nullptr; + cold->Release(); + source->Release(); + Flush(device); +} + +int main(int argc, char** argv) { + if (argc != 5 && argc != 6) + return 2; + ownerThread = GetCurrentThreadId(); + const bool benchmark = !std::strcmp(argv[2], "bench"); + const bool observe = !std::strcmp(argv[2], "observe"); + const unsigned budget = std::strtoul(argv[3], nullptr, 10); + const unsigned levels = std::strtoul(argv[4], nullptr, 10); + const unsigned iterations = argc == 6 ? std::strtoul(argv[5], nullptr, 10) : 64; + const std::string config = "d3d9.textureMemory=" + std::to_string(budget) + + ";d3d9.presentInterval=0;d3d9.gtaSaCompat=False;dxvk.hud="; + SetEnvironmentVariableA("DXVK_CONFIG", config.c_str()); + SetEnvironmentVariableA("DXVK_CONFIG_FILE", "upload-mapping-no-external.conf"); + SetEnvironmentVariableA("DXVK_LOG_LEVEL", "error"); + HMODULE backend = LoadLibraryA(argv[1]); + CHECK(backend != nullptr); + if (!backend) + return 1; + using Create = IDirect3D9* (WINAPI*)(UINT); + const auto create = reinterpret_cast(GetProcAddress(backend, "Direct3DCreate9")); + CHECK(create != nullptr); + if (!create) + return 1; + { + ImportHook mapHook, unmapHook, copyHook; + CHECK(mapHook.install(backend, "MapViewOfFile", reinterpret_cast(&CountMap))); + CHECK(unmapHook.install(backend, "UnmapViewOfFile", reinterpret_cast(&CountUnmap))); + originalMap = reinterpret_cast(mapHook.original); + originalUnmap = reinterpret_cast(unmapHook.original); + if (!originalMap || !originalUnmap) + return 1; + if (!benchmark) { + CHECK(copyHook.install(backend, "memcpy", reinterpret_cast(&CountCopy))); + originalCopy = reinterpret_cast(copyHook.original); + if (!originalCopy) + return 1; + } + IDirect3D9* factory = create(D3D_SDK_VERSION); + CHECK(factory != nullptr); + WNDCLASSA wc{}; + wc.lpfnWndProc = DefWindowProcA; + wc.hInstance = GetModuleHandleA(nullptr); + wc.lpszClassName = "RenderStackUploadMapping"; + RegisterClassA(&wc); + HWND window = CreateWindowA(wc.lpszClassName, "mapping", WS_OVERLAPPEDWINDOW, + 0, 0, 64, 64, nullptr, nullptr, wc.hInstance, nullptr); + D3DPRESENT_PARAMETERS params{}; + params.BackBufferWidth = params.BackBufferHeight = 32; + params.BackBufferFormat = D3DFMT_A8R8G8B8; + params.BackBufferCount = 1; + params.SwapEffect = D3DSWAPEFFECT_DISCARD; + params.Windowed = TRUE; + params.hDeviceWindow = window; + params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* device = nullptr; + CHECK(factory->CreateDevice(0, D3DDEVTYPE_HAL, window, + D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, &device) == S_OK); + if (device) { + Exercise(device, true, benchmark, observe, budget, levels, iterations); + Exercise(device, false, benchmark, observe, budget, levels, iterations); + if (!benchmark) + CubeFaces(device, observe, budget); + if (!benchmark && budget == 1) + ReclaimOtherMappings(device); + CHECK(device->Reset(¶ms) == S_OK); + CHECK(device->Release() == 0); + } + factory->Release(); + DestroyWindow(window); + } + FreeLibrary(backend); + std::printf("checks=%u failures=%u\n", checks, failures); + return failures ? 1 : 0; +} diff --git a/tests/upload-mapping-test.ps1 b/tests/upload-mapping-test.ps1 new file mode 100644 index 0000000..2b96ff3 --- /dev/null +++ b/tests/upload-mapping-test.ps1 @@ -0,0 +1,29 @@ +param( + [string]$Backend, + [string]$OutputDirectory, + [ValidateSet('check','observe','bench')][string]$Mode='check', + [int]$BudgetMiB=1, + [int]$Levels=0, + [string]$LlvmMingwBin, + [switch]$CompileOnly +) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/perf/20261108-upload-state/native'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$executable=Join-Path $OutputDirectory 'upload-mapping-probe.exe' +& $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'upload-mapping-probe.cpp') '-o' $executable '-luuid' '-luser32' +if($LASTEXITCODE){throw 'Upload mapping probe compilation failed'} +if($CompileOnly){return} +$library=(Resolve-Path -LiteralPath $Backend).Path +$env:DXVK_LOG_PATH=$OutputDirectory +Push-Location $OutputDirectory +try { + & $executable $library $Mode $BudgetMiB $Levels 2>&1|Tee-Object (Join-Path $OutputDirectory 'test.log') + $result=$LASTEXITCODE +} finally { Pop-Location } +if($result){throw "Upload mapping fixture failed ($result)"} diff --git a/tests/vendor/loadwholemap-ini/IniReader.h b/tests/vendor/loadwholemap-ini/IniReader.h new file mode 100644 index 0000000..ceffa9c --- /dev/null +++ b/tests/vendor/loadwholemap-ini/IniReader.h @@ -0,0 +1,282 @@ +#ifndef INIREADER_H +#define INIREADER_H +#include "ini_parser.hpp" +#include +#include +#include + +/* +* String comparision functions, with case sensitive option +*/ + +using std::strcmp; + +inline int strcmp(const char* str1, const char* str2, bool csensitive) +{ + return (csensitive ? ::strcmp(str1, str2) : ::_stricmp(str1, str2)); +} + +inline int strcmp(const char* str1, const char* str2, size_t num, bool csensitive) +{ + return (csensitive ? ::strncmp(str1, str2, num) : ::_strnicmp(str1, str2, num)); +} + +inline int compare(const std::string& str1, const std::string& str2, bool case_sensitive) +{ + if (str1.length() == str2.length()) + return strcmp(str1.c_str(), str2.c_str(), case_sensitive); + return (str1.length() < str2.length() ? -1 : 1); +} + +inline int compare(const std::string& str1, const std::string& str2, size_t num, bool case_sensitive) +{ + if (str1.length() == str2.length()) + return strcmp(str1.c_str(), str2.c_str(), num, case_sensitive); + return (str1.length() < str2.length() ? -1 : 1); +} + +inline int compare(const char* str1, const char* str2, bool case_sensitive) +{ + return strcmp(str1, str2, case_sensitive); +} + +inline int compare(const char* str1, const char* str2, size_t num, bool case_sensitive) +{ + return strcmp(str1, str2, num, case_sensitive); +} + +inline bool starts_with(const char* str, const char* prefix, bool case_sensitive) +{ + while (*prefix) + { + bool equal; + if (case_sensitive) + equal = (*str++ == *prefix++); + else + equal = (::tolower(*str++) == ::tolower(*prefix++)); + + if (!equal) return false; + } + return true; +} + +inline bool ends_with(const char* str, const char* prefix, bool case_sensitive) +{ + auto str2 = &str[strlen(str) - 1]; + auto prefix2 = &prefix[strlen(prefix) - 1]; + + while (prefix2 >= prefix) + { + bool equal; + if (case_sensitive) + equal = (*str2-- == *prefix2--); + else + equal = (::tolower(*str2--) == ::tolower(*prefix2--)); + + if (!equal) return false; + } + return true; +} + +class CIniReader +{ +private: + std::string m_szFileName; + +public: + linb::ini data; + + CIniReader() + { + SetIniPath(""); + } + + CIniReader(std::string_view szFileName) + { + SetIniPath(szFileName); + } + + CIniReader(std::stringstream& ini_mem) + { + data.load_file(ini_mem); + } + + bool operator==(CIniReader& ir) + { + if (data.size() != ir.data.size()) + return false; + + for (auto& section : data) + { + for (auto& key : data.at(section.first)) + { + if (key.second != ir.data.at(section.first)[key.first]) + return false; + } + } + return true; + } + + bool operator!=(CIniReader& ir) + { + return !(*this == ir); + } + + bool CompareBySections(CIniReader& ir) + { + if (data.size() != ir.data.size()) + return false; + + for (auto& section : data) + { + if (ir.data.find(section.first) == ir.data.end()) + return false; + + if (section.second.size() != ir.data.find(section.first)->second.size()) + return false; + + if (section.first != ir.data.find(section.first)->first) + return false; + } + return true; + } + + bool CompareByValues(CIniReader& ir) + { + return *this == ir; + } + + const std::string& GetIniPath() + { + return m_szFileName; + } + + void SetIniPath() + { + SetIniPath(""); + } + + void SetIniPath(std::string_view szFileName) + { + char buffer[MAX_PATH]; + HMODULE hm = NULL; + GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)&ends_with, &hm); + GetModuleFileNameA(hm, buffer, sizeof(buffer)); + std::string modulePath = buffer; + + if (szFileName.find(':') != std::string_view::npos) + { + m_szFileName = szFileName; + } + else if (szFileName.length() == 0) + { + m_szFileName = modulePath.substr(0, modulePath.find_last_of('.')) + ".ini"; + } + else + { + m_szFileName = modulePath.substr(0, modulePath.rfind('\\') + 1) + szFileName.data(); + } + + data.load_file(m_szFileName); + } + + int ReadInteger(std::string_view szSection, std::string_view szKey, int iDefaultValue) + { + auto str = data.get(szSection.data(), szKey.data(), std::to_string(iDefaultValue)); + return std::stoi(str, nullptr, starts_with(str.c_str(), "0x", false) ? 16 : 10); + } + + float ReadFloat(std::string_view szSection, std::string_view szKey, float fltDefaultValue) + { + return (float)atof(data.get(szSection.data(), szKey.data(), std::to_string(fltDefaultValue)).c_str()); + } + + bool ReadBoolean(std::string_view szSection, std::string_view szKey, bool bolDefaultValue) + { + auto val = data.get(szSection.data(), szKey.data(), ""); + if (!val.empty()) + { + if (val.size() == 1) + return val.front() != '0'; + else + return compare(val, "false", false); + } + return bolDefaultValue; + } + + std::string ReadString(std::string_view szSection, std::string_view szKey, std::string_view szDefaultValue) + { + auto s = data.get(szSection.data(), szKey.data(), szDefaultValue.data()); + + if (!s.empty()) + { + if (s.at(0) == '\"' || s.at(0) == '\'') + s.erase(0, 1); + + if (s.at(s.size() - 1) == '\"' || s.at(s.size() - 1) == '\'') + s.erase(s.size() - 1); + } + + return s; + } + + void WriteInteger(std::string_view szSection, std::string_view szKey, int iValue, bool useparser = false) + { + if (useparser) + { + data.set(szSection.data(), szKey.data(), std::to_string(iValue)); + data.write_file(m_szFileName); + } + else + { + char szValue[255]; + _snprintf_s(szValue, 255, "%s%d", " ", iValue); + WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str()); + } + } + + void WriteFloat(std::string_view szSection, std::string_view szKey, float fltValue, bool useparser = false) + { + if (useparser) + { + data.set(szSection.data(), szKey.data(), std::to_string(fltValue)); + data.write_file(m_szFileName); + } + else + { + char szValue[255]; + _snprintf_s(szValue, 255, "%s%f", " ", fltValue); + WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str()); + } + } + + void WriteBoolean(std::string_view szSection, std::string_view szKey, bool bolValue, bool useparser = false) + { + if (useparser) + { + data.set(szSection.data(), szKey.data(), std::to_string(bolValue)); + data.write_file(m_szFileName); + } + else + { + char szValue[255]; + _snprintf_s(szValue, 255, "%s%s", " ", bolValue ? "True" : "False"); + WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue, m_szFileName.c_str()); + } + } + + void WriteString(std::string_view szSection, std::string_view szKey, std::string_view szValue, bool useparser = false) + { + if (useparser) + { + data.set(szSection.data(), szKey.data(), szValue.data()); + data.write_file(m_szFileName); + } + else + { + WritePrivateProfileStringA(szSection.data(), szKey.data(), szValue.data(), m_szFileName.c_str()); + } + } +}; + +#endif //INIREADER_H diff --git a/tests/vendor/loadwholemap-ini/LICENSE.txt b/tests/vendor/loadwholemap-ini/LICENSE.txt new file mode 100644 index 0000000..71bf92b --- /dev/null +++ b/tests/vendor/loadwholemap-ini/LICENSE.txt @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2013-2015 Denilson das Mercês Amorim + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + * + */ \ No newline at end of file diff --git a/tests/vendor/loadwholemap-ini/README.md b/tests/vendor/loadwholemap-ini/README.md new file mode 100644 index 0000000..e3c134d --- /dev/null +++ b/tests/vendor/loadwholemap-ini/README.md @@ -0,0 +1,43 @@ +# Test-only INI consumer provenance + +`IniReader.h`, `ini_parser.hpp` and `LICENSE.txt` are unmodified copies from +[JuniorDjjr/LoadWholeMap](https://github.com/JuniorDjjr/LoadWholeMap), commit +`acf56ce9968aadfe169d01ab67ff3fb7a23f0af7`, directory +`PC/LoadWholeMap/IniReader/`. They are used only by the offline native test +probe, never linked into either RenderStack DLL. Keep the original notices. + +| File | SHA256 of imported bytes | +| --- | --- | +| IniReader.h | 09AD30491544CBF2643DB6FEE8149E0796F2471C073C8739BA383AB88EBC31D1 | +| ini_parser.hpp | AD2A875E6D05606A5B7ACE45E027CA472C19B8FEF42AFE203F07504F259CD7AD | +| LICENSE.txt | 72496FED468FA5CBE1FDC71BFD925CA03858A8066934B59B68014B7DB3FF83B8 | + +The public PC plugin source at this commit identifies itself as v1.0. It is +**not** a full source match for the installed v1.1 ASI. The regression combines +this real reader with separately audited v1.1 call-site evidence: the installed +binary passes `Settings`, `RemoveUnusedInterval` and default `60`. It does not +assert equivalence of every parser detail or all streaming behavior between +those versions. In particular, do not copy the v1.0 reaper policy into v1.1. + +The tested installed ASI SHA256 is +`D7BD005EDE20F00B358B75C4D4844618024F2A74AAF60957553EBD964BEE527C`. +Its key is at RVA `0x47D20`, default push at `0x73BB`, reader call at `0x73E9`. +The native probe executes the following captured 25-byte decision fragment +from RVA `0x7F4B` (no ASI/DllMain is loaded): + +```text +A1 84 CB B7 00 2B 05 D0 08 05 10 3B 05 E4 08 05 10 +77 06 66 0F 2F C8 76 32 +``` + +Three absolute address operands at offsets `1`, `7`, `13` are relocated to +probe-owned current-time, last-reclaim-time and interval DWORDs. The final +`jbe` displacement at offset `24` changes from `0x32` to `0x06`, so the two +outcomes return `true`/`false` instead of entering the original callees or +epilogue. The comparisons, unsigned timer subtraction, strict `>` branch and +RAM bypass are unchanged. RAM and cap enter as `xmm1` and `xmm0` respectively. + +This covers the actual interval decision fragment, not the outer pressure +test, model removal, conversion, I/O, the main-thread game call chain, or +resource residency. The deterministic A/B counts assume the pressure gate is +already entered at each simulated callback. They are not game FPS results. diff --git a/tests/vendor/loadwholemap-ini/ini_parser.hpp b/tests/vendor/loadwholemap-ini/ini_parser.hpp new file mode 100644 index 0000000..b45619f --- /dev/null +++ b/tests/vendor/loadwholemap-ini/ini_parser.hpp @@ -0,0 +1,375 @@ +/* + * Copyright (c) 2013-2015 Denilson das Mercês Amorim + * Copyright (c) 2017 ThirteenAG + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + * + */ +#ifndef LINB_INI_PARSER_HPP +#define LINB_INI_PARSER_HPP + + /* + * STL-like INI Container + */ + +#include // for std::string +#include // for std::map +#include // for std::FILE +#include // for std::find_if +#include // for std::function +#include // for std::vector +#include +#include + +namespace linb +{ + template< + class CharT = char, /* Not compatible with other type here, since we're using C streams */ + class StringType = std::basic_string, + class KeyContainer = std::map, + class SectionContainer = std::map + > class basic_ini + { + public: + typedef CharT char_type; + typedef StringType string_type; + typedef KeyContainer key_container; + typedef SectionContainer section_container; + + // Typedef container values types + typedef typename section_container::value_type value_type; + typedef typename section_container::key_type key_type; + typedef typename section_container::mapped_type mapped_type; + + // Typedef common types + typedef typename section_container::size_type size_type; + typedef typename section_container::difference_type difference_type; + + // Typedef iterators + typedef typename section_container::iterator iterator; + typedef typename section_container::const_iterator const_iterator; + typedef typename section_container::reverse_iterator reverse_iterator; + typedef typename section_container::const_reverse_iterator const_reverse_iterator; + + // typedef References and pointers + typedef typename section_container::reference reference; + typedef typename section_container::const_reference const_reference; + typedef typename section_container::pointer pointer; + typedef typename section_container::const_pointer const_pointer; + + private: + section_container data; + + public: + + basic_ini() + { } + + basic_ini(const char_type* filename) + { + this->read_file(filename); + } + + /* Iterator methods */ + iterator begin() + { + return data.begin(); + } + const_iterator begin() const + { + return data.begin(); + } + iterator end() + { + return data.end(); + } + const_iterator end() const + { + return data.end(); + } + const_iterator cbegin() const + { + return data.cbegin(); + } + const_iterator cend() const + { + return data.cend(); + } + + /* Reverse iterator methods */ + reverse_iterator rbegin() + { + return data.rbegin(); + } + const_reverse_iterator rbegin() const + { + return data.rbegin(); + } + reverse_iterator rend() + { + return data.rend(); + } + const_reverse_iterator rend() const + { + return data.rend(); + } + const_reverse_iterator crbegin() const + { + return data.crbegin(); + } + const_reverse_iterator crend() const + { + return data.crend(); + } + + /* Acessing index methods */ + mapped_type& operator[](const string_type& sect) + { + return data[sect]; + } + mapped_type& operator[](string_type&& sect) + { + return data[std::forward(sect)]; + } + mapped_type& at(const string_type& sect) + { + return data.at(sect); + } + const mapped_type& at(const string_type& sect) const + { + return data.at(sect); + } + + /* Capacity information */ + bool empty() const + { + return data.empty(); + } + size_type size() const + { + return data.size(); + } + size_type max_size() const + { + return data.max_size(); + } + + /* Modifiers */ + void clear() + { + return data.clear(); + } + + /* Lookup */ + size_type count(const string_type& sect) + { + return data.count(sect); + } + iterator find(const string_type& sect) + { + return data.find(sect); + } + + /* Gets a value from the specified section & key, default_value is returned if the sect & key doesn't exist */ + string_type get(const string_type& sect, const key_type& key, const string_type& default_value) + { + auto it = this->find(sect); + if (it != this->end()) + { + auto itv = it->second.find(key); + if (itv != it->second.end()) + return itv->second; + } + return default_value; + } + + /* Sets the value of a value in the ini */ + void set(const string_type& sect, const key_type& key, const string_type& value) + { + (*this)[sect][key] = value; // no emplace since overwrite! + } + + /* Too lazy to continue this container... If you need more methods, just add it */ + + + bool read_file(std::stringstream& ini_mem) + { + if (ini_mem.rdbuf()->in_avail()) + { + key_container* keys = nullptr; + string_type line; + string_type key; + string_type value; + string_type null_string; + size_type pos; + + // Trims an string + auto trim = [](string_type& s, bool trimLeft, bool trimRight) -> string_type& + { + if (s.size()) + { + // Ignore UTF-8 BOM + while (s.size() >= 3 && s[0] == (char)(0xEF) && s[1] == (char)(0xBB) && s[2] == (char)(0xBF)) + s.erase(s.begin(), s.begin() + 3); + + if (trimLeft) + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not_fn(std::function(::isspace)))); + if (trimRight) + s.erase(std::find_if(s.rbegin(), s.rend(), std::not_fn(std::function(::isspace))).base(), s.end()); + } + return s; + }; + + // Start parsing + while (std::getline(ini_mem, line)) + { + // Find comment and remove anything after it from the line + if ((pos = line.find_first_of(';')) != line.npos) + line.erase(pos); + + if ((pos = line.rfind(" //")) != line.npos) + line.erase(pos); + + // Trim the string, and if it gets empty, skip this line + if (trim(line, true, true).empty()) + continue; + + // Find section name + if (line.front() == '[' && line.back() == ']') + { + pos = line.length() - 1; //line.find_first_of(']'); + if (pos != line.npos) + { + trim(key.assign(line, 1, pos - 1), true, true); + keys = &data[std::move(key)]; // Create section + } + else + keys = nullptr; + } + else + { + // Find key and value positions + pos = line.find_first_of('='); + if (pos == line.npos) + { + // There's only the key + key = line; // No need for trim, line is already trimmed + value.clear(); + } + else + { + // There's the key and the value + trim(key.assign(line, 0, pos), false, true); // trim the right + trim(value.assign(line, pos + 1, line.npos), true, false); // trim the left + } + + // Put the key/value into the current keys object, or into the section "" if no section has been found +#if __cplusplus >= 201103L || _MSC_VER >= 1800 + (keys ? *keys : data[null_string]).emplace(std::move(key), std::move(value)); +#else + (keys ? *keys : data[null_string])[key] = value; + key.clear(); value.clear(); +#endif + } + } + + return true; + } + return false; + } + + bool read_file(const char_type* filename) + { + std::ifstream file(filename, std::ios::in); + if (file.is_open()) + { + std::stringstream ss; + ss << file.rdbuf(); + file.close(); + return read_file(ss); + } + return false; + } + + /* + * Dumps the content of this container into an ini file + */ + bool write_file(const char_type* filename) + { + FILE* f; + errno_t err; + if ((err = fopen_s(&f, filename, "w")) == 0) + { + bool first = true; + for (auto& sec : this->data) + { + fprintf(f, first ? "[%s]\n" : "\n[%s]\n", sec.first.c_str()); + first = false; + for (auto& kv : sec.second) + { + if (kv.second.empty()) + fprintf(f, "%s\n", kv.first.c_str()); + else + fprintf(f, "%s = %s\n", kv.first.c_str(), kv.second.c_str()); + } + } + fclose(f); + return true; + } + return false; + } + + + /* + */ + bool load_file(const char_type* filename) + { + return read_file(filename); + } + + bool load_file(const StringType& filename) + { + return load_file(filename.c_str()); + } + + bool load_file(std::stringstream& filename) + { + return read_file(filename); + } + + bool write_file(const StringType& filename) + { + return write_file(filename.c_str()); + } + }; + + + /* Use default basic_ini + * + * Limitations: + * * Not unicode aware + * * Case sensitive + * * Sections must have unique keys + */ + typedef basic_ini<> ini; +} + +#endif + diff --git a/tools/build.ps1 b/tools/build.ps1 index 157aa5c..3875faa 100644 --- a/tools/build.ps1 +++ b/tools/build.ps1 @@ -370,6 +370,27 @@ try { throw "Expected PE32 x86 output, found $($pe.Format)/$($pe.Machine): $output" } } + # Generate identity-bound function metadata after PE validation. Sidecars + # are ordinary artifacts, not additional PE images or public SDK inputs. + $symbolPython = Find-RenderStackPython -PythonPath $Python -RepoRoot $root + $symbolSpecs = @() + if ($Component -in @('All', 'Dxvk')) { + $symbolSpecs += [pscustomobject]@{ Binary = Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'; Map = $null } + } + if ($Component -in @('All', 'Bridge')) { + $symbolSpecs += [pscustomobject]@{ Binary = Join-Path $root 'out/build/bridge/d3d9.dll'; Map = Join-Path $root 'out/build/bridge/d3d9.map' } + } + $demangler = if ($null -ne $toolchain.LlvmMingw) { + Join-Path $toolchain.LlvmMingw.BinPath 'llvm-cxxfilt.exe' + } else { $null } + foreach ($spec in $symbolSpecs) { + $arguments = @((Join-Path $root 'tools/prepare-measurement-symbols.py'), '--binary', $spec.Binary, '--output', ($spec.Binary + '.rsym')) + if ($spec.Map) { $arguments += @('--map', $spec.Map) } + if ($demangler -and (Test-Path -LiteralPath $demangler)) { $arguments += @('--demangle-tool', $demangler) } + Invoke-BuildProcess -Label ('measurement-symbols-' + (Split-Path -Leaf (Split-Path -Parent $spec.Binary))) ` + -FilePath $symbolPython.Path -Arguments $arguments -WorkingDirectory $root | Out-Null + Add-ExpectedOutput -Path ($spec.Binary + '.rsym') + } } catch { if ($firstExitCode -eq 0) { $firstExitCode = 1 diff --git a/tools/package.ps1 b/tools/package.ps1 index 64b6959..a5ca80d 100644 --- a/tools/package.ps1 +++ b/tools/package.ps1 @@ -363,10 +363,12 @@ try { $mappings = Read-StrictPackageToml -Path $mappingPath $expectedMappings = @( 'out/build/bridge/d3d9.dll|d3d9.dll', + 'out/build/bridge/d3d9.dll.rsym|d3d9.dll.rsym', 'config/SA.RenderStack.ini|SA.RenderStack.ini', 'config/SA.RenderStack.ini|scripts/BridgeD3D9.ini', 'config/dxvk.conf|dxvk.conf', 'out/build/dxvk-x86/src/d3d9/d3d9.dll|backend/dxvk-gta/d3d9.dll', + 'out/build/dxvk-x86/src/d3d9/d3d9.dll.rsym|backend/dxvk-gta/d3d9.dll.rsym', 'README.md|docs/README.md', 'README.zh-CN.md|docs/README.zh-CN.md', 'README.pt-BR.md|docs/README.pt-BR.md', diff --git a/tools/prepare-measurement-symbols.py b/tools/prepare-measurement-symbols.py new file mode 100644 index 0000000..d3727b1 --- /dev/null +++ b/tools/prepare-measurement-symbols.py @@ -0,0 +1,245 @@ +#!/usr/bin/env python3 +"""Prepare bounded, exact-binary RSYM1 sidecars from PE COFF or an MSVC map. + +COFF function auxiliary TotalSize is explicit (E). A next-symbol/section-end +boundary is inferred (I), including linker-map ranges. Aliases are retained. +The source digest records provenance; sidecars must travel with that exact DLL. +No downloads, external tools, or system-export guesses are used. +""" +import argparse +import ctypes +import hashlib +import pathlib +import re +import shutil +import struct +import subprocess +import sys + +MAX_BINARY = 512 * 1024 * 1024 +MAX_METADATA = 64 * 1024 * 1024 +MAX_SYMBOLS = 250000 +MAX_NAME = 4096 + + +def bounded_read(path, limit): + with pathlib.Path(path).open("rb") as source: + data = source.read(limit + 1) + if len(data) > limit: + raise ValueError(f"input exceeds {limit} byte limit") + return data + + +def unpack(data, fmt, offset): + if offset < 0 or offset + struct.calcsize(fmt) > len(data): + raise ValueError("truncated PE/COFF metadata") + return struct.unpack_from(fmt, data, offset) + + +def parse_pe(data): + if data[:2] != b"MZ": + raise ValueError("missing DOS header") + pe, = unpack(data, " MAX_SYMBOLS * 4: + raise ValueError("PE metadata count exceeds limit") + magic, = unpack(data, "= image_size or end > image_size: + raise ValueError("section exceeds image") + sections.append((rva, end, bool(flags & 0x20000000))) + return dict(timestamp=timestamp, base=image_base, size=image_size, sections=sections, + symbol_pointer=pointer, symbol_count=symbols) + + +def check_name(name): + encoded = name.encode("utf-8") + if not encoded or len(encoded) > MAX_NAME or any(c in name for c in "\0\t\r\n"): + raise ValueError("invalid or oversized symbol name") + return name + + +def coff_symbols(data, pe): + pointer, count = pe["symbol_pointer"], pe["symbol_count"] + if not count: + return [] + if not pointer: + raise ValueError("missing COFF symbol table") + string_at = pointer + count * 18 + string_size, = unpack(data, " len(data): + raise ValueError("truncated COFF string table") + rows = [] + index = 0 + while index < count: + raw_name, value, section, kind, storage, aux = unpack(data, "<8sIhHBB", pointer + index * 18) + if index + aux >= count: + raise ValueError("truncated COFF auxiliary symbol") + if 0 < section <= len(pe["sections"]) and (kind & 0x30) == 0x20 and storage in (2, 3): + start, end, executable = pe["sections"][section - 1] + if executable: + if raw_name[:4] == b"\0" * 4: + offset, = struct.unpack_from("= string_size: + raise ValueError("invalid COFF string offset") + terminator = data.find(b"\0", string_at + offset, string_at + string_size) + if terminator < 0 or terminator - string_at - offset > MAX_NAME: + raise ValueError("unterminated COFF symbol name") + name = data[string_at + offset:terminator].decode("utf-8", errors="strict") + else: + name = raw_name.rstrip(b"\0").decode("utf-8", errors="strict") + size = unpack(data, "= end or (size and rva + size > end): + raise ValueError("COFF function exceeds executable section") + rows.append((section, rva, size, check_name(name))) + index += 1 + aux + return rows + + +def map_symbols(metadata, pe): + text = metadata.decode("utf-8-sig", errors="strict") + timestamp = re.search(r"Timestamp is\s+([0-9a-fA-F]+)", text) + base = re.search(r"Preferred load address is\s+([0-9a-fA-F]+)", text) + if not timestamp or int(timestamp[1], 16) != pe["timestamp"]: + raise ValueError("linker map timestamp does not match binary") + if not base or int(base[1], 16) != pe["base"]: + raise ValueError("linker map image base does not match binary") + rows = [] + pattern = re.compile(r"^\s+([0-9a-fA-F]{4}):([0-9a-fA-F]{8,16})\s+(\S+)\s+([0-9a-fA-F]{8,16})\s+(.*)$") + for line in text.splitlines(): + if len(line) > 16384: + raise ValueError("linker map line exceeds limit") + match = pattern.match(line) + if not match or not match[5].split() or match[5].split()[0] != "f": + continue + section, offset, address = int(match[1], 16), int(match[2], 16), int(match[4], 16) + if not 0 < section <= len(pe["sections"]): + raise ValueError("linker map function has invalid section") + start, end, executable = pe["sections"][section - 1] + if not executable or address != pe["base"] + start + offset or not start <= address - pe["base"] < end: + raise ValueError("linker map function address does not match PE section") + rows.append((section, address - pe["base"], 0, check_name(match[3]))) + if len(rows) > MAX_SYMBOLS: + raise ValueError("too many linker map symbols") + return rows + + +def demangle_names(names, tool=None): + """Optional, isolated build-time presentation. Never discard the raw name.""" + display = {name: name for name in names} + msvc_names = [name for name in names if name.startswith("?")] + if msvc_names and sys.platform == "win32": + try: + # DbgHelp is used serially in this short-lived Python process only. + library = ctypes.WinDLL("dbghelp.dll", winmode=0x800) # System32 only. + undecorate = library.UnDecorateSymbolName + undecorate.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_uint32, ctypes.c_uint32] + undecorate.restype = ctypes.c_uint32 + for name in msvc_names: + buffer = ctypes.create_string_buffer(MAX_NAME + 1) + length = undecorate(name.encode("utf-8"), buffer, len(buffer), 0x1000) # UNDNAME_NAME_ONLY. + if 0 < length <= MAX_NAME: + try: + readable = check_name(buffer.value.decode("utf-8", errors="strict")) + if readable != name: + display[name] = readable + except (ValueError, UnicodeError): + pass + except (AttributeError, OSError): + print("MSVC demangler unavailable; preserving original names", file=sys.stderr) + itanium = [] + for name in names: + canonical = name[1:] if name.startswith("__Z") else name + if canonical.startswith("_Z"): + # i686 COFF adds a leading underscore and may append @stackbytes. + canonical = re.sub(r"@\d+$", "", canonical) + itanium.append((name, canonical)) + executable = tool or shutil.which("llvm-cxxfilt") + if itanium and not executable: + print("Itanium demangler unavailable; preserving original names", file=sys.stderr) + if itanium and executable: + try: + for at in range(0, len(itanium), 512): + batch = itanium[at:at + 512] + result = subprocess.run([str(executable), "--no-strip-underscore", "--no-params"], + input="\n".join(canonical for _, canonical in batch) + "\n", + capture_output=True, encoding="utf-8", errors="strict", timeout=15) + lines = result.stdout.splitlines() + if result.returncode or len(lines) != len(batch): + print("Itanium demangler failed; preserving original names", file=sys.stderr) + continue + for (raw, canonical), readable in zip(batch, lines): + try: + if readable != canonical: + display[raw] = check_name(readable) + except ValueError: + pass + except (OSError, UnicodeError, subprocess.TimeoutExpired): + print("Itanium demangler unavailable or failed; preserving original names", file=sys.stderr) + return display + + +def prepare(binary, map_path=None, demangle_tool=None): + data = bounded_read(binary, MAX_BINARY) + pe = parse_pe(data) + metadata = bounded_read(map_path, MAX_METADATA) if map_path else data + rows = map_symbols(metadata, pe) if map_path else coff_symbols(data, pe) + if len(rows) > MAX_SYMBOLS: + raise ValueError("too many function symbols") + rows = sorted(set(rows)) + display_names = demangle_names(sorted({name for _, _, _, name in rows}), demangle_tool) + boundaries = {} + for section, rva, _, _ in rows: + boundaries.setdefault(section, set()).add(rva) + next_start = {} + for section, starts in boundaries.items(): + ordered = sorted(starts) + for index, start in enumerate(ordered): + next_start[(section, start)] = ordered[index + 1] if index + 1 < len(ordered) else pe["sections"][section - 1][1] + lines = ["RSYM1", "sha256 " + hashlib.sha256(data).hexdigest(), f"image_size {pe['size']:08x}", + "source_kind " + ("map" if map_path else "coff"), "source_sha256 " + hashlib.sha256(metadata).hexdigest()] + for start, end, executable in pe["sections"]: + if executable: + lines.append(f"section {start:08x} {end:08x}") + for section, start, size, name in rows: + end = start + size if size else next_start[(section, start)] + display_name = display_names[name] + lines.append(f"symbol {start:08x} {end:08x} {'E' if size else 'I'} {display_name.encode('utf-8').hex()} {name.encode('utf-8').hex()}") + result = "\n".join(lines + ["end", ""]) + if len(result.encode("ascii")) > MAX_METADATA: + raise ValueError("prepared sidecar exceeds limit") + return result, len(rows) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--binary", required=True) + parser.add_argument("--map", help="MSVC map from the same link; timestamp/base/section addresses are checked") + parser.add_argument("--output", required=True) + parser.add_argument("--demangle-tool", help="optional llvm-cxxfilt executable for build-time Itanium names; raw names survive failure") + args = parser.parse_args() + try: + content, count = prepare(args.binary, args.map, args.demangle_tool) + output = pathlib.Path(args.output) + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text(content, encoding="ascii", newline="\n") + print(f"Prepared {count} function records for {pathlib.Path(args.binary).resolve()} -> {output.resolve()}") + return 0 + except (ValueError, OSError, UnicodeError, struct.error) as error: + print(f"symbol preparation failed: {error}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/prepare-streaming-config.ps1 b/tools/prepare-streaming-config.ps1 new file mode 100644 index 0000000..44c9a00 --- /dev/null +++ b/tools/prepare-streaming-config.ps1 @@ -0,0 +1,110 @@ +param([switch]$Help, [string]$Source, [string]$OutputDirectory) + +if ($Help) { + @' +Usage: prepare-streaming-config.ps1 -Source -OutputDirectory + +Prepares a full UTF-8 candidate INI and preparation.json under this checkout's +out directory. Renames only Settings.RemoveUnusedIntervalMs to the audited +Improved Streaming v1.1 key RemoveUnusedInterval, preserving its integer value. +Never applies the candidate, overwrites an output, or writes to the source. +'@ | Write-Output + exit 0 +} +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +if (-not $Source -or -not $OutputDirectory) { throw 'Source and OutputDirectory are required; see -Help' } +. (Join-Path $PSScriptRoot 'lib/process-runner.ps1') +$root = Split-Path -Parent $PSScriptRoot +$sourcePath = [IO.Path]::GetFullPath($Source) +$destination = Assert-RenderStackPathUnder -Base (Join-Path $root 'out') -Path $OutputDirectory ` + -Description 'Streaming candidate output' +Assert-RenderStackNoReparsePath -Path $destination -Anchor $root -Description 'Streaming candidate output' | Out-Null +if ([IO.Directory]::Exists($destination) -or [IO.File]::Exists($destination)) { + throw 'OutputDirectory must be new; existing output is never overwritten' +} +# Hold a read-only handle which denies concurrent writers/deletion while preparing. +$inputFile = [IO.File]::Open($sourcePath, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read) +try { + $buffer = [IO.MemoryStream]::new() + try { $inputFile.CopyTo($buffer); $original = $buffer.ToArray() } finally { $buffer.Dispose() } + $utf8 = [Text.UTF8Encoding]::new($false, $true) + $text = $utf8.GetString($original) + if ($text.Contains([char]0) -or $text -match '\r(?!\n)') { + throw 'Only UTF-8 INI files with LF or CRLF line endings are supported' + } + $section = '' + $settingsCount = 0 + # The consumer trims C-locale byte whitespace, not Unicode whitespace. + $iniWhitespace = [char[]]@(' ', "`t", "`r", "`n", "`v", "`f") + $entries = [Collections.Generic.List[object]]::new() + foreach ($lineMatch in [regex]::Matches($text, '[^\n]+|\n')) { + $line = $lineMatch.Value + $semantic = $line.TrimStart([char]0xFEFF) + # Match the pinned consumer's comment syntax, section/key case and trim. + $comment = $semantic.IndexOf(';', [StringComparison]::Ordinal) + if ($comment -ge 0) { $semantic = $semantic.Substring(0, $comment) } + $comment = $semantic.LastIndexOf(' //', [StringComparison]::Ordinal) + if ($comment -ge 0) { $semantic = $semantic.Substring(0, $comment) } + $semantic = $semantic.Trim($iniWhitespace) + if ($semantic -match '^\[(.*)\]$') { + $section = $Matches[1].Trim($iniWhitespace) + if ($section -ceq 'Settings') { $settingsCount++ } + continue + } + if ($section -cne 'Settings' -or -not $semantic) { continue } + $parts = $semantic.Split('=', 2) + $key = $parts[0].Trim($iniWhitespace) + if ($key -ieq 'RemoveUnusedIntervalMs' -or $key -ieq 'RemoveUnusedInterval') { + if ($key -cne 'RemoveUnusedIntervalMs' -and $key -cne 'RemoveUnusedInterval') { + throw 'Interval key case differs from the audited consumer' + } + if ($parts.Count -ne 2) { throw 'Interval must have an integer value' } + $value = $parts[1].Trim($iniWhitespace) + if ($value -cmatch '^0[xX][0-9a-fA-F]+$') { + $number = [Convert]::ToInt64($value.Substring(2), 16) + } elseif ($value -cmatch '^[0-9]+$') { + $number = [long]::Parse($value, [Globalization.CultureInfo]::InvariantCulture) + } else { throw 'Interval must be an unambiguous non-negative integer' } + if ($number -lt 0 -or $number -gt [int]::MaxValue) { throw 'Interval is outside the consumer int32 range' } + $entries.Add([pscustomobject]@{key=$key; value=[int]$number; + offset=$lineMatch.Index + $line.IndexOf($key, [StringComparison]::Ordinal)}) + } + } + if ($settingsCount -ne 1 -or $entries.Count -ne 1) { + throw 'Require one Settings section and exactly one interval key; missing, duplicate and conflicting keys are refused' + } + $entry = $entries[0] + $changed = $entry.key -ceq 'RemoveUnusedIntervalMs' + $candidateText = $text + if ($changed) { + $candidateText = $text.Remove($entry.offset, $entry.key.Length).Insert($entry.offset, 'RemoveUnusedInterval') + } + $candidateBytes = $utf8.GetBytes($candidateText) + $manifest = [ordered]@{ + schemaVersion = 1 + applied = $false + changed = $changed + sourcePath = $sourcePath + sourceSha256 = [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData($original)) + candidateSha256 = [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData($candidateBytes)) + intervalMs = $entry.value + auditedConsumer = [ordered]@{ + name = 'ImprovedStreaming.SA.asi v1.1' + sha256 = 'D7BD005EDE20F00B358B75C4D4844618024F2A74AAF60957553EBD964BEE527C' + readerCallRva = '0x73E9' + keyRva = '0x47D20' + key = 'RemoveUnusedInterval' + defaultMs = 60 + } + note = 'Prepared only. Consumer identity is audit provenance, not automatic version detection or an FPS claim.' + } + [IO.Directory]::CreateDirectory($destination) | Out-Null + Assert-RenderStackNoReparsePath -Path $destination -Anchor $root -Description 'Streaming candidate output' | Out-Null + $candidateFile = [IO.File]::Open((Join-Path $destination 'ImprovedStreaming.ini'), [IO.FileMode]::CreateNew) + try { $candidateFile.Write($candidateBytes, 0, $candidateBytes.Length) } finally { $candidateFile.Dispose() } + $manifestBytes = $utf8.GetBytes(($manifest | ConvertTo-Json -Depth 5) + "`n") + $manifestFile = [IO.File]::Open((Join-Path $destination 'preparation.json'), [IO.FileMode]::CreateNew) + try { $manifestFile.Write($manifestBytes, 0, $manifestBytes.Length) } finally { $manifestFile.Dispose() } + Write-Output "Prepared candidate: $destination (interval=$($entry.value) ms, changed=$changed, applied=False)" +} finally { $inputFile.Dispose() } diff --git a/tools/test.ps1 b/tools/test.ps1 index 6a605a5..f905e75 100644 --- a/tools/test.ps1 +++ b/tools/test.ps1 @@ -6,6 +6,7 @@ param( [string]$BridgeSource, [string]$ActiveBridgeConfig, [string]$ActiveDxvkConfig, + [string]$BridgeReference, [string]$MSBuild, [string]$Python, [string]$LlvmMingwBin, @@ -22,6 +23,7 @@ if ($Help) { Usage: test.ps1 [-Help] [-Configuration Release] [-Architecture x86] [-DxvkSource ] [-BridgeSource ] [-ActiveBridgeConfig ] [-ActiveDxvkConfig ] + [-BridgeReference ] [-MSBuild ] [-Python ] [-LlvmMingwBin ] [-Ninja ] [-Glslang ] [-AllowNonV18MsBuild] [-SkipLocalBridgeEvidence] [-SkipGpuRuntimeProbes] @@ -447,6 +449,153 @@ function Add-BridgeRuntimeGates { } } +function Add-ApiProjectGates { + param([Parameter(Mandatory)] [string]$Backend, [switch]$SkipGpuRuntimeProbes) + + $bridgeToolchain = Get-RenderStackToolchain -RepoRoot $root -Component Bridge ` + -MSBuildPath $MSBuild -AllowNonV18MsBuild:$AllowNonV18MsBuild + $cmake = Join-Path $bridgeToolchain.MSBuild.InstallationPath ` + 'Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe' + if (-not (Test-Path -LiteralPath $cmake -PathType Leaf)) { + $command = Get-Command cmake.exe -ErrorAction Stop + $cmake = $command.Source + } + $ctest = Join-Path (Split-Path -Parent $cmake) 'ctest.exe' + if (-not (Test-Path -LiteralPath $ctest -PathType Leaf)) { throw "CTest is missing: $ctest" } + $generator = if ($bridgeToolchain.MSBuild.ProductMajor -ge 18) { + 'Visual Studio 18 2026' + } else { 'Visual Studio 17 2022' } + $apiBuild = Join-Path $root 'out/build/api-projects-x86' + $configure = Invoke-Gate -Name 'api-projects-configure' -Category 'api' -FilePath $cmake ` + -Arguments @('-S', (Join-Path $root 'src/bridge/legacy/api-projects'), '-B', $apiBuild, + '-G', $generator, '-A', 'Win32') -Required $true + if ($configure.status -ne 'passed') { return } + $build = Invoke-Gate -Name 'api-projects-build' -Category 'api' -FilePath $cmake ` + -Arguments @('--build', $apiBuild, '--config', $Configuration, '--parallel', '4') -Required $true ` + -EnvironmentRemovals @('CL', 'LINK', 'ExternalIncludePath') + if ($build.status -ne 'passed') { return } + [void](Invoke-Gate -Name 'api-projects-ctest' -Category 'api' -FilePath $ctest ` + -Arguments @('--test-dir', $apiBuild, '-C', $Configuration, '--output-on-failure', '--timeout', '30') ` + -Required $true) + foreach ($profile in @('reference', 'optimized')) { + $name = "api-state-runtime-$profile" + if ($SkipGpuRuntimeProbes) { + [void](Add-SkippedGate -Name $name -Category 'runtime' -Required $false ` + -Reason 'Vulkan runtime probing is unavailable on the hosted Windows runner') + [void](Add-SkippedGate -Name "constant-binding-pixel-runtime-$profile" -Category 'runtime' -Required $false ` + -Reason 'Vulkan runtime probing is unavailable on the hosted Windows runner') + [void](Add-SkippedGate -Name "texture-binding-lifetime-runtime-$profile" -Category 'runtime' -Required $false ` + -Reason 'Vulkan runtime probing is unavailable on the hosted Windows runner') + foreach ($pairGate in @('control-pair-shared','control-pair-dual','control-pair-protocol', + 'texture-pair','texture-pair-protocol')) { + [void](Add-SkippedGate -Name "$pairGate-$profile" -Category 'runtime' -Required $false ` + -Reason 'Vulkan runtime probing is unavailable on the hosted Windows runner') + } + continue + } + $runDirectory = New-TestRunDirectory -Name $name + [void](Invoke-Gate -Name $name -Category 'runtime' ` + -FilePath (Join-Path $apiBuild "$Configuration/api-state-runtime-tests.exe") ` + -Arguments @($Backend, $profile) -Required $true -WorkingDirectory $runDirectory ` + -EnvironmentRemovals @('DXVK_CONFIG', 'DXVK_CONFIG_FILE')) + $dedup = if ($profile -eq 'optimized') { 'on' } else { 'off' } + [void](Invoke-Gate -Name "constant-binding-pixel-runtime-$profile" -Category 'runtime' ` + -FilePath (Join-Path $apiBuild "$Configuration/constant-submit-benchmark.exe") ` + -Arguments @($Backend, $dedup, 'check') -Required $true -WorkingDirectory $runDirectory ` + -EnvironmentRemovals @('DXVK_CONFIG', 'DXVK_CONFIG_FILE')) + [void](Invoke-Gate -Name "texture-binding-lifetime-runtime-$profile" -Category 'runtime' ` + -FilePath (Join-Path $apiBuild "$Configuration/constant-submit-benchmark.exe") ` + -Arguments @($Backend, $dedup, 'lifetime') -Required $true -WorkingDirectory $runDirectory ` + -EnvironmentRemovals @('DXVK_CONFIG', 'DXVK_CONFIG_FILE')) + foreach ($topology in @('shared','dual')) { + $pairName = "control-pair-$topology-$profile" + $pairDirectory = New-TestRunDirectory -Name $pairName + $candidate = $Backend + if ($topology -eq 'dual') { + $candidate = Join-Path $pairDirectory 'paired-backend-copy.dll' + Copy-Item -LiteralPath $Backend -Destination $candidate + } + [void](Invoke-ScriptGate -Name $pairName -Category 'runtime' ` + -ScriptPath (Join-Path $root 'tests/control-path-benchmark.ps1') ` + -Arguments @('-Baseline', $Backend, '-Candidate', $candidate, '-Probe', + (Join-Path $apiBuild "$Configuration/constant-submit-benchmark.exe"), + '-OutputDirectory', $pairDirectory, '-Processes', '1', '-Rounds', '1', + '-Profile', $dedup, '-Cpu', 'any', '-Cases', 'controls') ` + -EnvironmentRemovals @('DXVK_CONFIG', 'DXVK_CONFIG_FILE')) + } + [void](Invoke-ScriptGate -Name "control-pair-protocol-$profile" -Category 'runtime' ` + -ScriptPath (Join-Path $root 'tests/control-path-protocol-test.ps1') ` + -Arguments @('-InputPath', (Join-Path $runRoot "control-pair-dual-$profile/$dedup-any-00.jsonl"))) + $texturePairDirectory = New-TestRunDirectory -Name "texture-pair-$profile" + [void](Invoke-ScriptGate -Name "texture-pair-$profile" -Category 'runtime' ` + -ScriptPath (Join-Path $root 'tests/control-path-benchmark.ps1') ` + -Arguments @('-Baseline', $Backend, '-Candidate', $Backend, '-Probe', + (Join-Path $apiBuild "$Configuration/constant-submit-benchmark.exe"), + '-OutputDirectory', $texturePairDirectory, '-Processes', '1', '-Rounds', '1', + '-Profile', $dedup, '-Cpu', 'any', '-Cases', 'textures') ` + -EnvironmentRemovals @('DXVK_CONFIG', 'DXVK_CONFIG_FILE')) + [void](Invoke-ScriptGate -Name "texture-pair-protocol-$profile" -Category 'runtime' ` + -ScriptPath (Join-Path $root 'tests/control-path-protocol-test.ps1') ` + -Arguments @('-InputPath', (Join-Path $texturePairDirectory "$dedup-any-00.jsonl"))) + } + foreach ($name in @('bridge-lifecycle-regression','config-ownership-regression')) { + if ($SkipGpuRuntimeProbes) { + [void](Add-SkippedGate -Name $name -Category 'runtime' -Required $false ` + -Reason 'Vulkan runtime probing is unavailable on the hosted Windows runner') + continue + } + $arguments = @('-Backend', $Backend, '-Bridge', (Join-Path $root 'out/build/bridge/d3d9.dll')) + if ($name -eq 'bridge-lifecycle-regression') { + $arguments += @('-Probe', (Join-Path $apiBuild "$Configuration/bridge-lifecycle-runtime-tests.exe")) + } + [void](Invoke-ScriptGate -Name $name -Category 'runtime' ` + -ScriptPath (Join-Path $root "tests/$name-test.ps1") -Arguments $arguments) + } +} + +function Add-MeasurementGates { + param([Parameter(Mandatory)] [string]$Backend, [switch]$SkipGpuRuntimeProbes) + $nativeTools = @('-MSBuild', (Find-RenderStackMSBuild -MsBuildPath $MSBuild -RepoRoot $root ` + -AllowNonV18MsBuild:$AllowNonV18MsBuild).Path) + if ($AllowNonV18MsBuild) { $nativeTools += '-AllowNonV18MsBuild' } + $symbolTools = @('-Python', $pythonPath, '-LlvmMingwBin', $toolchain.LlvmMingw.BinPath) + $backendTools = @('-LlvmMingwBin', $toolchain.LlvmMingw.BinPath) + foreach ($entry in @( + @{ Name='measurement-symbols'; Script='measurement-symbols-test.ps1'; Args=$symbolTools }, + @{ Name='measurement-symbols-msvc'; Script='measurement-symbols-msvc-test.ps1'; Args=($nativeTools + $symbolTools) }, + @{ Name='journal-attribution-offline'; Script='journal-attribution-test.ps1'; Args=(@('-EvidenceName','pipeline-offline') + $nativeTools) } + )) { + [void](Invoke-ScriptGate -Name $entry.Name -Category 'measurement-offline' ` + -ScriptPath (Join-Path $root ('tests/' + $entry.Script)) -Arguments $entry.Args) + } + foreach ($entry in @( + @{ Name='measurement-backend'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, + @{ Name='thread-journal-native'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-OutputDirectory',(Join-Path $root 'out/perf/20261031-thread-journal/pipeline-native')) + $backendTools) }, + @{ Name='thread-journal-mixed'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','check-software','-OutputDirectory',(Join-Path $root 'out/perf/20261031-binding-dirty-capture/pipeline-mixed')) + $backendTools) }, + @{ Name='thread-journal-no-compat'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-DisableCompat','-OutputDirectory',(Join-Path $root 'out/perf/20261031-thread-journal/pipeline-no-compat')) + $backendTools) }, + @{ Name='upload-mapping-pressure'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, + @{ Name='upload-mapping-ordinary'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend,'-BudgetMiB','100','-OutputDirectory',(Join-Path $root 'out/perf/20261108-upload-state/native-ordinary')) + $backendTools) }, + @{ Name='upload-mapping-single'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend,'-Levels','1','-OutputDirectory',(Join-Path $root 'out/perf/20261108-upload-state/native-single')) + $backendTools) }, + @{ Name='patch-retry-native'; Script='patch-retry-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, + @{ Name='measurement-backend-unavailable'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend,'-DisableCompat') + $backendTools) }, + @{ Name='measurement-hud-render'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend,'-RenderHud') + $backendTools) }, + @{ Name='measurement-runtime-local'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-local') + $nativeTools) }, + @{ Name='measurement-runtime-native'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-native','-Backend',$Backend) + $nativeTools) }, + @{ Name='journal-attribution-native'; Script='journal-attribution-test.ps1'; Args=(@('-EvidenceName','pipeline-native','-Device','-Backend',$Backend) + $nativeTools) } + @{ Name='bridge-lifecycle-measurement'; Script='bridge-lifecycle-regression-test.ps1'; Args=@('-Backend',$Backend,'-Measurement') } + @{ Name='statering-system'; Script='statering-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-system') + $nativeTools) } + @{ Name='statering-native'; Script='statering-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-native','-Backend',$Backend) + $nativeTools) } + )) { + if ($SkipGpuRuntimeProbes) { + [void](Add-SkippedGate -Name $entry.Name -Category 'measurement-runtime' -Required $false ` + -Reason 'GPU runtime checks were explicitly skipped') + } else { + [void](Invoke-ScriptGate -Name $entry.Name -Category 'measurement-runtime' ` + -ScriptPath (Join-Path $root ('tests/' + $entry.Script)) -Arguments $entry.Args) + } + } +} + function Remove-TemporaryRunDirectories { foreach ($directory in @($temporaryDirectories | Sort-Object Length -Descending)) { try { @@ -523,8 +672,11 @@ try { [void](Add-SkippedGate -Name 'bridge-evidence-types-regression' -Category 'bridge-regression' -Required $false ` -Reason 'Current Bridge evidence is unavailable without the local GTA game-root reference') } else { + $reference = if ([string]::IsNullOrWhiteSpace($BridgeReference)) { + Join-Path (Split-Path -Parent (Split-Path -Parent $root)) 'd3d9.dll' + } else { Get-RenderStackFullPath -Path $BridgeReference } [void](Invoke-ScriptGate -Name 'current-bridge-evidence' -ScriptPath (Join-Path $root 'tools/migration/verify-bridge-baseline.ps1') ` - -Arguments @('-Reference', (Join-Path (Split-Path -Parent (Split-Path -Parent $root)) 'd3d9.dll'), '-Candidate', (Get-OutputPath -RelativePath 'out/build/bridge/d3d9.dll'), + -Arguments @('-Reference', $reference, '-Candidate', (Get-OutputPath -RelativePath 'out/build/bridge/d3d9.dll'), '-ExportsOut', (Join-Path $currentEvidence 'bridge-exports.txt'), '-EvidenceOut', (Join-Path $currentEvidence 'bridge-build-evidence.json')) -Category 'evidence') [void](Invoke-ScriptGate -Name 'bridge-evidence-types-regression' -ScriptPath (Join-Path $scriptsRoot 'bridge-build-evidence-types-regression-test.ps1') ` -Arguments @('-ExportsPath', (Join-Path $currentEvidence 'bridge-exports.txt'), @@ -538,6 +690,9 @@ try { -LlvmMingwBin $LlvmMingwBin -NinjaPath $Ninja -GlslangPath $Glslang ` -AllowNonV18MsBuild:$AllowNonV18MsBuild $pythonPath = $toolchain.Python.Path + [void](Invoke-ScriptGate -Name 'streaming-config-offline-regression' -Category 'streaming-offline' ` + -ScriptPath (Join-Path $scriptsRoot 'streaming-config-regression-test.ps1') ` + -Arguments @('-LlvmMingwBin', $toolchain.LlvmMingw.BinPath)) $buildCurrent = Test-BuildIsCurrent -Toolchain $toolchain if (-not $buildCurrent) { $buildArguments = [Collections.Generic.List[string]]::new() @@ -568,8 +723,22 @@ try { $moduleDirectory = [string]$metadata.tools.meson.moduleDirectory $mesonEnvironment = Get-MesonEnvironment -Toolchain $toolchain -PythonPath $pythonPath -ModuleDirectory $moduleDirectory Add-TargetAndRegistrationGates -Toolchain $toolchain -PythonPath $pythonPath -MesonEnvironment $mesonEnvironment + $constantLayoutRun = New-TestRunDirectory -Name 'constant-control-layout' + [void](Invoke-Gate -Name 'constant-control-linked-layout' -Category 'performance-structure' -FilePath $pythonPath ` + -Arguments @((Join-Path $root 'tests/constant-layout-test.py'), + (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll'), + '--objdump', (Join-Path $toolchain.LlvmMingw.BinPath 'llvm-objdump.exe'), + '--report', (Join-Path $constantLayoutRun 'layout.json')) -Required $true) [void](Invoke-ScriptGate -Name 'thread-scheduling-config' -ScriptPath (Join-Path $scriptsRoot 'thread-scheduling-config-test.ps1') -Category 'runtime-config') [void](Invoke-Gate -Name 'backend-api-msvc-executable' -Category 'api' -FilePath (Get-OutputPath -RelativePath 'out/build/sdk-tests/backend-api-layout-test.exe') -Required $true) + foreach ($method in @('single','batch')) { + $bindingCostRun = New-TestRunDirectory -Name "texture-binding-cost-$method" + [void](Invoke-Gate -Name "texture-binding-linked-cost-$method" -Category 'performance-structure' -FilePath $pythonPath ` + -Arguments @((Join-Path $root 'tests/texture-binding-ownership-test.py'), + (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll'), + '--objdump', (Join-Path $toolchain.LlvmMingw.BinPath 'llvm-objdump.exe'), + '--method', $method, '--report', (Join-Path $bindingCostRun 'cost.json')) -Required $true) + } $llvmSyntaxDirectory = New-TestRunDirectory -Name 'backend-api-llvm-syntax' [void](Invoke-Gate -Name 'backend-api-llvm-syntax' -Category 'api' -FilePath $toolchain.LlvmMingw.CompilerPath ` -Arguments @('-std=c++23', '-fsyntax-only', (Join-Path $root 'tests/backend-api-layout-test.cpp'), '-I', (Join-Path $root 'sdk/include'), '-I', (Join-Path $root 'backend/dxvk/include/vulkan/include')) ` @@ -579,9 +748,13 @@ try { $stateProbe = (Get-Content -LiteralPath (Join-Path $root 'out/build/dxvk-x86/meson-info/intro-targets.json') -Raw | ConvertFrom-Json | Where-Object name -eq 'renderstack-stateblock-prefilter-probe').filename[0] Add-RuntimeProbeGates -Backend (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll') -CompatProbe $compatProbe -StateProbe $stateProbe ` -SkipGpuRuntimeProbes:$SkipGpuRuntimeProbes + Add-ApiProjectGates -Backend (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll') ` + -SkipGpuRuntimeProbes:$SkipGpuRuntimeProbes Add-BridgeRuntimeGates -Backend (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll') ` -SkipGpuRuntimeProbes:$SkipGpuRuntimeProbes ` -SkipEnvironmentSensitiveBridgeTests:$SkipEnvironmentSensitiveBridgeTests + Add-MeasurementGates -Backend (Get-OutputPath -RelativePath 'out/build/dxvk-x86/src/d3d9/d3d9.dll') ` + -SkipGpuRuntimeProbes:$SkipGpuRuntimeProbes $exportArguments = [Collections.Generic.List[string]]::new() foreach ($argument in @(