Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// 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.
// On multithreaded devices notification and registration take the recursive
// device lock before the observer lock, allowing same-device API reentry.
inline constexpr GUID IID_D3D9BridgeLifecycle =
{0x36f6d3a1,0xc28c,0x4dd0,{0x91,0xaf,0x23,0x1e,0x8b,0xa0,0xb2,0x01}};
inline constexpr GUID IID_D3D9BridgeRuntime =
Expand Down
46 changes: 39 additions & 7 deletions backend/dxvk/src/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ namespace dxvk {
}

HRESULT STDMETHODCALLTYPE D3D9DeviceEx::SetObserver(const D3D9BridgeObserver* observer) {
D3D9DeviceLock deviceLock = LockDevice();
std::lock_guard<std::recursive_mutex> lock(m_bridgeObserverMutex);
if (observer && (observer->StructSize < sizeof(*observer)
|| !observer->Notify || m_bridgeObserver.Notify))
Expand All @@ -746,6 +747,9 @@ namespace dxvk {

void D3D9DeviceEx::NotifyBridge(D3D9BridgeEvent event, HRESULT result,
D3DPRESENT_PARAMETERS* params, const D3D9BridgePresentArgs* present) {
// Reset already owns the device lock. Present and final release must use
// the same device -> observer order: callbacks may reenter device APIs.
D3D9DeviceLock deviceLock = LockDevice();
std::lock_guard<std::recursive_mutex> lock(m_bridgeObserverMutex);
const auto observer = m_bridgeObserver;
if (event == D3D9BridgeEvent::FinalRelease)
Expand Down Expand Up @@ -3728,7 +3732,9 @@ namespace dxvk {

for (UINT i = 0u; i < pBatch->SamplerStateCount; i++) {
const auto& binding = pBatch->SamplerStates[i];
result = SetSamplerState(binding.Sampler, binding.Type, binding.Value);
// This batch was validated before mutation and owns the outer lock.
result = SetStateSamplerStateLocked(
RemapSamplerState(binding.Sampler), binding.Type, binding.Value);
if (FAILED(result)) return result;
}

Expand Down Expand Up @@ -5828,29 +5834,39 @@ namespace dxvk {
}


HRESULT D3D9DeviceEx::SetStateSamplerState(
template<bool AlreadyLocked>
force_inline HRESULT D3D9DeviceEx::SetStateSamplerStateImpl(
DWORD StateSampler,
D3DSAMPLERSTATETYPE Type,
DWORD Value) {
D3D9DeviceLock lock = LockDevice();
D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::SamplerState);
D3D9DeviceLock lock = AlreadyLocked ? D3D9DeviceLock() : LockDevice();

// Recording must observe same-value writes without mutating live state.
if (unlikely(ShouldRecord()))
return m_recorder->SetStateSamplerState(StateSampler, Type, Value);

auto& state = m_state.samplerStates;
const DWORD oldValue = state[StateSampler][Type];
const bool unchanged = oldValue == Value;

// Ordinary setters are mostly redundant. Locked batch/StateBlock callers
// also handle changed sequences, so avoid an extra branch on that route.
if constexpr (!AlreadyLocked) {
if (unchanged && likely(!m_gtaSaCompat.IsStateAuditActive()))
return D3D_OK;
}

D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::SamplerState);
m_gtaSaCompat.RecordStateAudit(
D3D9GtaSaStateAuditKind::SamplerState,
state[StateSampler][Type] == Value);
unchanged);

if (state[StateSampler][Type] == Value)
if (unchanged)
return D3D_OK;

if (unlikely(ShouldCaptureGtaSaStateJournal()))
m_gtaSaStateJournal->CaptureCurrentStateSamplerState(StateSampler, Type);

const DWORD oldValue = state[StateSampler][Type];
state[StateSampler][Type] = Value;
NotifyGtaSaStateChanged();

Expand Down Expand Up @@ -5888,6 +5904,22 @@ namespace dxvk {
}


HRESULT D3D9DeviceEx::SetStateSamplerState(
DWORD StateSampler,
D3DSAMPLERSTATETYPE Type,
DWORD Value) {
return SetStateSamplerStateImpl<false>(StateSampler, Type, Value);
}


HRESULT D3D9DeviceEx::SetStateSamplerStateLocked(
DWORD StateSampler,
D3DSAMPLERSTATETYPE Type,
DWORD Value) {
return SetStateSamplerStateImpl<true>(StateSampler, Type, Value);
}


HRESULT D3D9DeviceEx::SetStateTexture(DWORD StateSampler, IDirect3DBaseTexture9* pTexture) {
D3D9DeviceLock lock = LockDevice();
D3D9GtaSaStateAuditScope audit(m_gtaSaCompat, D3D9GtaSaStateAuditKind::Texture);
Expand Down
13 changes: 13 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,12 @@ namespace dxvk {
D3DSAMPLERSTATETYPE Type,
DWORD Value);

// Internal entry for validated callers that already own the device lock.
HRESULT SetStateSamplerStateLocked(
DWORD StateSampler,
D3DSAMPLERSTATETYPE Type,
DWORD Value);

/**
* @brief Sets the given sampler texture
*
Expand Down Expand Up @@ -1381,6 +1387,13 @@ namespace dxvk {

private:

// Inline both lock policies so ordinary changed setters keep one call frame.
template<bool AlreadyLocked>
HRESULT SetStateSamplerStateImpl(
DWORD StateSampler,
D3DSAMPLERSTATETYPE Type,
DWORD Value);

void CloseD3D9DrawBatch(
D3D9GtaSaBatchBreakReason reason) {
if (m_d3d9BatchSize == 0u)
Expand Down
33 changes: 31 additions & 2 deletions backend/dxvk/src/d3d9/d3d9_hud.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "d3d9_hud.h"
#include "../dxvk/hud/dxvk_hud_font.h"

namespace dxvk::hud {

Expand All @@ -8,8 +9,13 @@ namespace dxvk::hud {
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)
size_t maxCharacters = 0;
for (UINT i = 0; i < m_snapshot.LineCount; ++i) {
m_lines[i] = m_snapshot.Lines[i];
maxCharacters = std::max(maxCharacters, m_lines[i].size());
}
m_textWidth = (maxCharacters * 16 * g_hudFont.advance + g_hudFont.size - 1) / g_hudFont.size;
m_renderWidth = -1;
m_lastUpdate = time;
}

Expand All @@ -18,9 +24,32 @@ namespace dxvk::hud {
HudPos position) {
if (!m_snapshot.Visible)
return position;
const auto surface = renderer.getPushConstants().surfaceSize;
const auto width = int32_t(surface.width / options.scale);
const auto height = int32_t(surface.height / options.scale);
// The frame-time graph occupies the bottom 144 logical pixels. When the
// full statistics column reaches it, continue in a separate right column;
// following D3D9 memory items inherit that position as well.
if (position.y + int32_t(m_snapshot.LineCount * 16 + 8) > height - 144
&& width >= 560 + 256 + 16)
position = { std::max(568, width - m_textWidth - 8), 8 };
const int32_t available = std::max(0, width - position.x - 8);
if (m_renderWidth != available) {
m_renderWidth = available;
const size_t characters = available * g_hudFont.size / (16 * g_hudFont.advance);
for (UINT i = 0; i < m_snapshot.LineCount; ++i) {
m_renderLines[i] = m_lines[i];
if (m_renderLines[i].size() > characters) {
// Long symbols remain complete in the report. Cache visible text
// until the next snapshot/resize, keeping per-frame rendering cheap.
m_renderLines[i].resize(characters > 3 ? characters - 3 : 0);
m_renderLines[i].append(std::min(characters, size_t(3)), '.');
}
}
}
for (UINT i = 0; i < m_snapshot.LineCount; ++i) {
position.y += 16;
renderer.drawText(16, position, i ? 0xffffffffu : 0xffc0ff00u, m_lines[i]);
renderer.drawText(16, position, i ? 0xffffffffu : 0xffc0ff00u, m_renderLines[i]);
}
position.y += 8;
return position;
Expand Down
3 changes: 3 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace dxvk::hud {
D3D9DeviceEx* m_device;
D3D9BridgeHudSnapshot m_snapshot = { };
std::array<std::string, D3D9_BRIDGE_HUD_LINE_COUNT> m_lines;
std::array<std::string, D3D9_BRIDGE_HUD_LINE_COUNT> m_renderLines;
int32_t m_textWidth = 0;
int32_t m_renderWidth = -1;
dxvk::high_resolution_clock::time_point m_lastUpdate = { };
};

Expand Down
8 changes: 6 additions & 2 deletions backend/dxvk/src/d3d9/d3d9_stateblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,12 @@ namespace dxvk {
unchanged = m_state.samplerStates[samplerIdx][stateIdx] == value;
}

if (!unchanged)
dst->SetStateSamplerState(samplerIdx, D3DSAMPLERSTATETYPE(stateIdx), value);
if (!unchanged) {
if constexpr (std::is_same_v<Dst, D3D9DeviceEx>)
dst->SetStateSamplerStateLocked(samplerIdx, D3DSAMPLERSTATETYPE(stateIdx), value);
else
dst->SetStateSamplerState(samplerIdx, D3DSAMPLERSTATETYPE(stateIdx), value);
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions docs/development/evidence/2026-11-10-review-fixes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Review fixes: native validation and paired measurements

See [the validation report](../../review-fixes-20261110.md) for the three fixes,
real RED/GREEN regressions, HUD captures, method and interpretation.

- `validation.json`: full local run of `f6876b2f` — 85 passed, zero failed,
two missing-external-input skips; all 87 per-gate logs have verified hashes.
- `sampler-ab.json`: final frozen candidate vs the previously installed sampler
candidate, 12 independent processes × four alternating rounds, 1,536 samples.
- `sampler-aa.json`: six processes × four rounds against the same baseline.
- `present-first.json` and `present-final.json`: preserve both passes, including
the noisy controls. Each mode has six A/B pairs and three A/A pairs; each
process executes 128 warmup and 2,000 timed Presents on a real multithreaded
device. Observer mode also verifies every native callback reenters GetAudit.

Binary/probe identities and individual process/round measurements are retained.
Negative percentage changes mean reduced time/cycles. Thread cycles are not
game FPS. Benchmark/runtime scheduling is unchanged; only compiler processes
used the local Clang affinity workaround. Hosted CI remains a separate build
with its existing GPU/environment-sensitive skip policy.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"warmup":128,"iterations":2000,"affinity":"normal","rows":[{"observer":0,"iterations":2000,"ns":429684.0,"cycles":37719.721,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":0,"variant":"baseline","control":true,"slot":0},{"observer":0,"iterations":2000,"ns":404601.1,"cycles":38505.3895,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":0,"variant":"baseline","control":true,"slot":1},{"observer":1,"iterations":2000,"ns":577353.35,"cycles":50678.183,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":0,"variant":"baseline","control":true,"slot":0},{"observer":1,"iterations":2000,"ns":548048.0,"cycles":48918.33,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":0,"variant":"baseline","control":true,"slot":1},{"observer":0,"iterations":2000,"ns":450224.65,"cycles":43302.29,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":1,"variant":"baseline","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":557446.15,"cycles":51918.997,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":1,"variant":"candidate","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":372507.0,"cycles":35930.0095,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":1,"variant":"baseline","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":352709.9,"cycles":32093.9715,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":1,"variant":"candidate","control":false,"slot":1},{"observer":0,"iterations":2000,"ns":339391.65,"cycles":29262.0445,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":2,"variant":"candidate","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":378973.0,"cycles":34042.349,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":2,"variant":"baseline","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":340053.15,"cycles":31663.75,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":2,"variant":"candidate","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":329793.0,"cycles":30015.6815,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":2,"variant":"baseline","control":false,"slot":1},{"observer":0,"iterations":2000,"ns":344903.1,"cycles":30777.444,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":3,"variant":"baseline","control":true,"slot":0},{"observer":0,"iterations":2000,"ns":331663.3,"cycles":28876.6825,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":3,"variant":"baseline","control":true,"slot":1},{"observer":1,"iterations":2000,"ns":343646.75,"cycles":31196.9885,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":3,"variant":"baseline","control":true,"slot":0},{"observer":1,"iterations":2000,"ns":400752.25,"cycles":34844.4925,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":3,"variant":"baseline","control":true,"slot":1},{"observer":0,"iterations":2000,"ns":390684.45,"cycles":34233.216,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":4,"variant":"baseline","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":392994.8,"cycles":38474.669,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":4,"variant":"candidate","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":413886.5,"cycles":39434.282,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":4,"variant":"baseline","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":394443.8,"cycles":39218.53,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":4,"variant":"candidate","control":false,"slot":1},{"observer":0,"iterations":2000,"ns":413129.35,"cycles":36337.362,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":5,"variant":"candidate","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":412511.75,"cycles":34663.783,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":5,"variant":"baseline","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":389502.35,"cycles":32519.707,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":5,"variant":"candidate","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":356395.85,"cycles":33866.7225,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":5,"variant":"baseline","control":false,"slot":1},{"observer":0,"iterations":2000,"ns":359521.75,"cycles":32407.952,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":6,"variant":"baseline","control":true,"slot":0},{"observer":0,"iterations":2000,"ns":347951.3,"cycles":30569.2595,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":6,"variant":"baseline","control":true,"slot":1},{"observer":1,"iterations":2000,"ns":335122.25,"cycles":30363.7595,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":6,"variant":"baseline","control":true,"slot":0},{"observer":1,"iterations":2000,"ns":324707.5,"cycles":29262.052,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":6,"variant":"baseline","control":true,"slot":1},{"observer":0,"iterations":2000,"ns":338889.55,"cycles":28902.091,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":7,"variant":"baseline","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":376834.35,"cycles":33409.158,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":7,"variant":"candidate","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":344254.8,"cycles":30838.7435,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":7,"variant":"baseline","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":360863.6,"cycles":33865.2645,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":7,"variant":"candidate","control":false,"slot":1},{"observer":0,"iterations":2000,"ns":333196.2,"cycles":28815.1295,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":8,"variant":"candidate","control":false,"slot":0},{"observer":0,"iterations":2000,"ns":335933.65,"cycles":28399.684,"callbacks":0,"failedPresents":0,"failures":0,"mode":"present","pair":8,"variant":"baseline","control":false,"slot":1},{"observer":1,"iterations":2000,"ns":330114.15,"cycles":29628.9755,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":8,"variant":"candidate","control":false,"slot":0},{"observer":1,"iterations":2000,"ns":337315.45,"cycles":30053.669,"callbacks":2128,"failedPresents":0,"failures":0,"mode":"observer","pair":8,"variant":"baseline","control":false,"slot":1}],"binaries":[{"Path":"out\\perf\\20261109-sampler-batch\\candidate-final\\d3d9.dll","Hash":"1CB76F17306FDF25C40FCC6F822B8B948CE95FE45DD24EF7037D8E1F7EC6C806"},{"Path":"out\\review-fixes-20261110\\candidate-final\\d3d9.dll","Hash":"714BF82766BCABF4D8C42262BDF40B8FD4A13850900FEB4A3570FD3B209E4B22"},{"Path":"out\\review-fixes-20261110\\present-probe\\measurement-backend-probe.exe","Hash":"194F8136EB20DDC2B8D7972259A9F2963D9DA2F98CE48916527746C6BC945044"}],"summary":[{"mode":"present","control":false,"metric":"ns","changePercent":0.3705382736282181,"pairs":6,"changes":[23.815110967380406,-10.444372026503201,0.59135959979979,0.1497169474566462,11.19680438656192,-0.8148781760922152]},{"mode":"present","control":false,"metric":"cycles","changePercent":8.608954650493207,"pairs":6,"changes":[19.898963773047562,-14.042228695793002,12.389875961405439,4.828033339580973,15.594259252730192,1.462852544415627]},{"mode":"present","control":true,"metric":"ns","changePercent":-3.8387013627885547,"pairs":3,"changes":[-5.837522458364752,-3.8387013627885547,-3.218289296822796]},{"mode":"present","control":true,"metric":"cycles","changePercent":-5.673584372131879,"pairs":3,"changes":[2.082911747942151,-6.175826361669278,-5.673584372131879]},{"mode":"observer","control":false,"metric":"ns","changePercent":0.4881009792230051,"pairs":6,"changes":[-5.314557847235079,3.1110878642057305,-4.697592214290635,9.28924958020696,4.824565990074792,-2.1348859057597203]},{"mode":"observer","control":false,"metric":"cycles","changePercent":-0.9801174206855057,"pairs":6,"changes":[-10.676417995380715,5.490691590660712,-0.5471178605458094,-3.977401415209292,9.8140217677805,-1.413116980825202]},{"mode":"observer","control":true,"metric":"ns","changePercent":-3.107746501463271,"pairs":3,"changes":[-5.075808428235495,16.617500383751626,-3.107746501463271]},{"mode":"observer","control":true,"metric":"cycles","changePercent":-3.4726047695908813,"pairs":3,"changes":[-3.4726047695908813,11.69184647421979,-3.628363279586644]}]}
Loading
Loading