Skip to content
Closed
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: 0 additions & 2 deletions backend/dxvk/src/d3d9/d3d9_bridge_lifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// 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: 7 additions & 39 deletions backend/dxvk/src/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ 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 @@ -747,9 +746,6 @@ 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);
Comment on lines 747 to 749

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore device-before-observer lock ordering

On a multithreaded device, concurrent Present and Reset can now deadlock: PresentEx enters this function without the device lock and holds m_bridgeObserverMutex while invoking the observer, whereas Reset holds the device lock before calling NotifyBridge. If the Present observer reenters a device API, each thread waits for the other's lock. This occurs in the production measurement observer, where OnUnwrappedDeviceEvent calls RuntimePresent, which can call diagnostics methods such as GetAudit that acquire the device lock. Restore the device-before-observer ordering before invoking callbacks.

Useful? React with 👍 / 👎.

const auto observer = m_bridgeObserver;
if (event == D3D9BridgeEvent::FinalRelease)
Expand Down Expand Up @@ -3732,9 +3728,7 @@ namespace dxvk {

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

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


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

// 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,
unchanged);
state[StateSampler][Type] == Value);

if (unchanged)
if (state[StateSampler][Type] == Value)
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 @@ -5904,22 +5888,6 @@ 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: 0 additions & 13 deletions backend/dxvk/src/d3d9/d3d9_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,6 @@ 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 @@ -1387,13 +1381,6 @@ 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: 2 additions & 31 deletions backend/dxvk/src/d3d9/d3d9_hud.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "d3d9_hud.h"
#include "../dxvk/hud/dxvk_hud_font.h"

namespace dxvk::hud {

Expand All @@ -9,13 +8,8 @@ namespace dxvk::hud {
m_snapshot.StructSize = sizeof(m_snapshot);
m_snapshot.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION;
m_device->GetBridgeHudSnapshot(&m_snapshot);
size_t maxCharacters = 0;
for (UINT i = 0; i < m_snapshot.LineCount; ++i) {
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 @@ -24,32 +18,9 @@ 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_renderLines[i]);
renderer.drawText(16, position, i ? 0xffffffffu : 0xffc0ff00u, m_lines[i]);
Comment on lines 21 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore overflow handling for the measurement HUD

With DXVK_HUD=full at 1280×720 and default scale, the eight-line measurement block now continues below the existing statistics column instead of moving to the right, causing it to overlap the bottom-left frame-time graph and potentially extend beyond the viewport. In the inspected HUD wiring, d3d9_swapchain.cpp appends HudRenderStack after the standard statistics items, while HudFrameTimeItem::render independently anchors its graph near the bottom of the screen; neither performs collision avoidance for this block. Restore the available-height check and alternate-column placement so capture statistics remain readable with the full HUD enabled.

Useful? React with 👍 / 👎.

}
position.y += 8;
return position;
Expand Down
3 changes: 0 additions & 3 deletions backend/dxvk/src/d3d9/d3d9_hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ 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: 2 additions & 6 deletions backend/dxvk/src/d3d9/d3d9_stateblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,8 @@ namespace dxvk {
unchanged = m_state.samplerStates[samplerIdx][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);
}
if (!unchanged)
dst->SetStateSamplerState(samplerIdx, D3DSAMPLERSTATETYPE(stateIdx), value);
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions docs/development/evidence/2026-11-10-review-fixes/README.md

This file was deleted.

This file was deleted.

Loading
Loading