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
117 changes: 117 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once

#include <d3d9.h>

// Private, optional, copied-data telemetry. This interface does not alter
// API1-7 or the diagnostics/lifecycle vtables. All timestamps use the same
// QPC-derived nanosecond clock as BridgeMeasurementRuntime::NowNs().
inline constexpr GUID IID_D3D9BridgeTelemetry =
{0x5722e0c7,0xa495,0x4e93,{0xb7,0xcd,0x13,0x87,0x8c,0xb2,0x94,0x31}};
inline constexpr UINT D3D9_BRIDGE_TELEMETRY_VERSION = 1;
inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_EVENTS = 65536;
inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_CHUNKS = 4096;
inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_MASKS = 65536;

enum D3D9BridgeTelemetryKind : UINT {
D3D9_BRIDGE_TELEMETRY_ALLOC = 1,
D3D9_BRIDGE_TELEMETRY_FREE,
D3D9_BRIDGE_TELEMETRY_MAP,
D3D9_BRIDGE_TELEMETRY_UNMAP,
D3D9_BRIDGE_TELEMETRY_CS_WAIT,
D3D9_BRIDGE_TELEMETRY_RESOURCE_WAIT,
D3D9_BRIDGE_TELEMETRY_GPU_WAIT,
// CPU waiting for submit/present status, not GPU completion.
D3D9_BRIDGE_TELEMETRY_SUBMISSION_WAIT,
// Signal was observed incomplete before the frame-latency wait call.
// It may become ready before the calling thread actually blocks.
D3D9_BRIDGE_TELEMETRY_FRAME_LATENCY_WAIT,
// Inclusive acquireNextImage call, including CPU, driver and any waits.
D3D9_BRIDGE_TELEMETRY_PRESENT_ACQUIRE,
// Sampler-pool pressure: waiting for a submission fence observed incomplete.
D3D9_BRIDGE_TELEMETRY_SAMPLER_POOL_WAIT,
// Enqueue-to-completion latency of unreachable file mappings, not CPU time.
D3D9_BRIDGE_TELEMETRY_MAPPING_RETIRE,
D3D9_BRIDGE_TELEMETRY_CS_CHUNK,
D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE,
D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN,
D3D9_BRIDGE_TELEMETRY_COMMAND_RESET,
};
enum D3D9BridgeTelemetryFlags : UINT {
D3D9_BRIDGE_TELEMETRY_FILE_BACKED = 1u << 0,
D3D9_BRIDGE_TELEMETRY_FAILED = 1u << 1,
D3D9_BRIDGE_TELEMETRY_RESOURCE = 1u << 2,
D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY = 1u << 3,
D3D9_BRIDGE_TELEMETRY_OS_MAPPING = 1u << 4,
// GPU_WAIT includes queue draining plus vkDeviceWaitIdle.
D3D9_BRIDGE_TELEMETRY_DEVICE_IDLE = 1u << 5,
D3D9_BRIDGE_TELEMETRY_CACHE_HIT = 1u << 6,
D3D9_BRIDGE_TELEMETRY_CACHE_RETURN = 1u << 7,
D3D9_BRIDGE_TELEMETRY_CACHE_FILL = 1u << 8,
D3D9_BRIDGE_TELEMETRY_CACHE_EVICT = 1u << 9,
D3D9_BRIDGE_TELEMETRY_DEFERRED = 1u << 10,
D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY = 1u << 11,
D3D9_BRIDGE_TELEMETRY_SECONDARY = 1u << 12,
};
enum D3D9BridgeMemoryFlags : UINT {
D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED = 1u << 0,
D3D9_BRIDGE_MEMORY_MASKS_TRUNCATED = 1u << 1,
};

struct D3D9BridgeTelemetryConfig {
UINT StructSize, Version;
UINT64 CaptureId;
UINT MaxEvents, Reserved;
// Successful spans are filtered only if BOTH duration and bytes are below
// these thresholds. Zero thresholds retain all completed spans. Failures
// remain eligible regardless of size and duration.
UINT64 MinDurationNs, MinBytes;
};
struct D3D9BridgeTelemetryEvent {
UINT64 CaptureId, BeginNs, EndNs, Identity, Bytes;
UINT Kind, ThreadId;
INT Result;
UINT Flags;
};
struct D3D9BridgeTelemetryRead {
UINT StructSize, Version;
UINT64 CaptureId, Dropped, Filtered;
// Events are completed spans only. InFlightAtStop records spans still open
// at the stop boundary; they cannot be interpreted as completed timings.
UINT64 Started, InFlight, InFlightAtStop, LateDropped;
UINT Written, Remaining, Capacity, Active;
};
struct D3D9BridgeMemoryChunk {
UINT64 Cookie, Capacity, Used;
UINT Heap, Type, Mapped, Active;
// Offset/count refer to the caller's mask buffer. If the entire mask for a
// chunk cannot fit, MaskCount is zero and MASKS_TRUNCATED is set.
UINT PageCount, MaskOffset, MaskCount, Reserved;
};
struct D3D9BridgeMemorySnapshot {
UINT StructSize, Version;
// Timestamp is taken under the allocator lock. ScanDurationNs includes
// time spent waiting for that lock. Totals describe HUD-visible pooled
// chunks, not dedicated allocations.
UINT64 CaptureId, TimestampNs, ScanDurationNs, TotalCapacity, TotalUsed;
UINT TotalChunks, CopiedChunks, TotalMasks, CopiedMasks, Flags, Reserved;
};

// Calls may run concurrently. Capture IDs must increase strictly over the
// device lifetime, so an old reader cannot consume a later generation.
// Start creates a new generation only when no
// capture is active. Stop is idempotent for that generation; completed data
// remains readable until the next Start. A stale/mismatched ID never consumes
// events of the current generation. Late operation completions from older
// generations are discarded. ReadEvents consumes copied entries in order;
// pass capacity zero and a null array to inspect counters without consuming.
// GetMemorySnapshot succeeds only for the active capture; its arrays and all
// returned data are caller owned. No recorder lock spans device operations,
// waits, or allocator lock acquisition.
struct ID3D9BridgeTelemetry : IUnknown {
virtual HRESULT STDMETHODCALLTYPE StartCapture(const D3D9BridgeTelemetryConfig*) = 0;
virtual HRESULT STDMETHODCALLTYPE StopCapture(UINT64 captureId) = 0;
virtual HRESULT STDMETHODCALLTYPE ReadEvents(UINT64 captureId, D3D9BridgeTelemetryRead*,
D3D9BridgeTelemetryEvent*, UINT capacity) = 0;
virtual HRESULT STDMETHODCALLTYPE GetMemorySnapshot(UINT64 captureId, D3D9BridgeMemorySnapshot*,
D3D9BridgeMemoryChunk*, UINT chunkCapacity, UINT* masks, UINT maskCapacity) = 0;
};
9 changes: 9 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_common_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ namespace dxvk {
*/
void* GetData(UINT Subresource);

// Used only by initial texture creation, before any client can write it.
bool HasZeroInitializedMemory() const {
return !m_buffer && m_data.wasInitiallyZero();
}

bool WasMappingUsed() const { return m_mappingWasUsed; }
void MarkMappingUsed() { m_mappingWasUsed = true; }

const Rc<DxvkBuffer>& GetBuffer();

DxvkBufferSlice GetBufferSlice(UINT Subresource);
Expand Down Expand Up @@ -505,6 +513,7 @@ namespace dxvk {
Rc<DxvkImage> m_resolveImage;
Rc<DxvkBuffer> m_buffer;
MemoryFileRegion m_data = { };
bool m_mappingWasUsed = false;

D3D9SubresourceArray<
uint64_t> m_seqs = { };
Expand Down
115 changes: 110 additions & 5 deletions backend/dxvk/src/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,38 @@ namespace dxvk {
HRESULT STDMETHODCALLTYPE D3D9BridgeThreadStats::GetThreadStats(D3D9BridgeThreadSnapshot* output) {
return m_device->GetBridgeThreadStats(output);
}
ULONG STDMETHODCALLTYPE D3D9BridgeTelemetry::AddRef() { return m_device->AddRef(); }
ULONG STDMETHODCALLTYPE D3D9BridgeTelemetry::Release() { return m_device->Release(); }
HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::QueryInterface(REFIID iid, void** output) {
return m_device->QueryInterface(iid, output);
}
HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::StartCapture(const D3D9BridgeTelemetryConfig* c) {
return m_device->GetDXVKDevice()->telemetry().start(c);
}
HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::StopCapture(UINT64 id) {
return m_device->GetDXVKDevice()->telemetry().stop(id);
}
HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::ReadEvents(UINT64 id, D3D9BridgeTelemetryRead* out,
D3D9BridgeTelemetryEvent* events, UINT capacity) {
return m_device->GetDXVKDevice()->telemetry().read(id,out,events,capacity);
}
HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::GetMemorySnapshot(UINT64 id, D3D9BridgeMemorySnapshot* out,
D3D9BridgeMemoryChunk* chunks, UINT chunkCapacity, UINT* masks, UINT maskCapacity) {
if (!out) return E_POINTER;
if (out->StructSize!=sizeof(*out) || out->Version!=D3D9_BRIDGE_TELEMETRY_VERSION ||
chunkCapacity>D3D9_BRIDGE_TELEMETRY_MAX_CHUNKS || maskCapacity>D3D9_BRIDGE_TELEMETRY_MAX_MASKS ||
(chunkCapacity && !chunks) || (maskCapacity && !masks)) return D3DERR_INVALIDCALL;
auto device=m_device->GetDXVKDevice(); auto& recorder=device->telemetry();
const auto generation=recorder.activeGeneration(id);
if (!generation) return D3DERR_INVALIDCALL;
D3D9BridgeMemorySnapshot snapshot{};
snapshot.StructSize=sizeof(snapshot); snapshot.Version=D3D9_BRIDGE_TELEMETRY_VERSION;
snapshot.CaptureId=id; const auto scanStart=DxvkTelemetry::nowNs();
device->getTelemetryMemorySnapshot(snapshot,chunks,chunkCapacity,masks,maskCapacity);
snapshot.ScanDurationNs=DxvkTelemetry::nowNs()-scanStart;
if (!recorder.isGenerationActive(id,generation)) return D3DERR_INVALIDCALL;
*out=snapshot; return S_OK;
}
HRESULT D3D9DeviceEx::GetBridgeThreadStats(D3D9BridgeThreadSnapshot* output) {
if (!output || output->StructSize != sizeof(*output) || output->Version != 1)
return D3DERR_INVALIDCALL;
Expand Down Expand Up @@ -533,6 +565,12 @@ namespace dxvk {
, m_d3d8Bridge ( this )
, m_bridgeDiagnostics ( this ) {

m_memoryAllocator.setTelemetry(&m_dxvkDevice->telemetry());
#ifdef DXVK_USE_UNMAPPABLE_MEMORY
m_memoryAllocator.enableRetirement();
#endif
m_shaderAllocator.setTelemetry(&m_dxvkDevice->telemetry());

InitShaderOptions();

// If we can SWVP, then we use an extended constant set
Expand Down Expand Up @@ -657,6 +695,11 @@ namespace dxvk {
return S_OK;
}

if (riid == IID_D3D9BridgeTelemetry) {
*ppvObject = ref(&m_bridgeTelemetry);
return S_OK;
}

if (riid == IID_D3D9BridgeDiagnostics) {
*ppvObject = ref(&m_bridgeDiagnostics);
return S_OK;
Expand Down Expand Up @@ -6267,6 +6310,8 @@ namespace dxvk {
ExecuteFlush(false);
SynchronizeCsThread(SequenceNumber);

DxvkTelemetryScope scope(&m_dxvkDevice->telemetry(),D3D9_BRIDGE_TELEMETRY_RESOURCE_WAIT,
reinterpret_cast<uintptr_t>(&Resource));
m_dxvkDevice->waitForResource(Resource, access);
}
}
Expand Down Expand Up @@ -6419,6 +6464,14 @@ namespace dxvk {
// Don't use MapTexture here to keep the mapped list small while the resource is still locked.
void* mapPtr = pResource->GetData(Subresource);

#ifdef DXVK_USE_UNMAPPABLE_MEMORY
if (pResource->GetMapMode() == D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE
&& !pResource->WasMappingUsed()) {
pResource->MarkMappingUsed();
m_mappedTextures.touch(pResource);
}
#endif

if (unlikely(needsReadback)) {
// The texture was written to on the GPU.
// This can be either the image (for D3DPOOL_DEFAULT)
Expand Down Expand Up @@ -9417,7 +9470,13 @@ namespace dxvk {
uint64_t submissionId = m_submissionFence->value();

while (++submissionId <= m_submissionId) {
m_submissionFence->wait(submissionId);
if (unlikely(m_dxvkDevice->telemetry().enabled() && m_submissionFence->value()<submissionId)) {
DxvkTelemetryScope scope(&m_dxvkDevice->telemetry(),D3D9_BRIDGE_TELEMETRY_SAMPLER_POOL_WAIT,
reinterpret_cast<uintptr_t>(m_submissionFence.ptr()));
m_submissionFence->wait(submissionId);
} else {
m_submissionFence->wait(submissionId);
}

// Need to manually update sampler stats here since we
// might otherwise hit this path again the next time
Expand Down Expand Up @@ -10867,6 +10926,7 @@ namespace dxvk {

#ifdef DXVK_USE_UNMAPPABLE_MEMORY
if (likely(pTexture->GetMapMode() == D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE)) {
pTexture->MarkMappingUsed();
m_mappedTextures.insert(pTexture);
}
#endif
Expand All @@ -10875,12 +10935,54 @@ namespace dxvk {
}


void D3D9DeviceEx::RetainInitializedTextureMapping(D3D9CommonTexture* pTexture) {
#ifdef DXVK_USE_UNMAPPABLE_MEMORY
if (pTexture->GetMapMode() != D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE)
return;

D3D9DeviceLock lock = LockDevice();
// Initial mappings form a cold prefix of the LRU until actually accessed.
// Replace only this prefix during admission, never the hot working set.
const uint64_t budget = uint64_t(m_d3d9Options.textureMemory);
if (m_memoryAllocator.getStats().memoryMapped > budget)
m_memoryAllocator.drainRetired();
auto iter = m_mappedTextures.leastRecentlyUsedIter();
while (m_memoryAllocator.getStats().memoryMapped > budget
&& iter != m_mappedTextures.leastRecentlyUsedEndIter()
&& !(*iter)->WasMappingUsed()) {
if ((*iter)->IsAnySubresourceLocked()
|| (m_textureUploadMappingScope && m_textureUploadMappingScope->Contains(*iter))) {
++iter;
continue;
}
(*iter)->UnmapData();
iter = m_mappedTextures.remove(iter);
}
if (m_memoryAllocator.getStats().memoryMapped > budget) {
pTexture->UnmapData();
UnmapTextures();
return;
}

try {
m_mappedTextures.insert(pTexture, false);
} catch (const std::bad_alloc&) {
// A cache admission failure must not turn successful initialization into
// resource creation failure, nor leave an untracked resident mapping.
pTexture->UnmapData();
}
UnmapTextures();
#endif
}


void D3D9DeviceEx::TouchMappedTexture(D3D9CommonTexture* pTexture) {
#ifdef DXVK_USE_UNMAPPABLE_MEMORY
if (pTexture->GetMapMode() != D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE)
return;

D3D9DeviceLock lock = LockDevice();
pTexture->MarkMappingUsed();
m_mappedTextures.touch(pTexture);
#endif
}
Expand All @@ -10901,15 +11003,18 @@ namespace dxvk {
// Will only be called inside the device lock

#ifdef DXVK_USE_UNMAPPABLE_MEMORY
uint32_t mappedMemory = m_memoryAllocator.getStats().memoryMapped;
const uint64_t budget = uint64_t(m_d3d9Options.textureMemory);
const uint64_t mappedMemory = m_memoryAllocator.getStats().memoryMapped;

if (likely(mappedMemory < uint32_t(m_d3d9Options.textureMemory)))
if (likely(!budget || mappedMemory <= budget))
return;

uint32_t threshold = (m_d3d9Options.textureMemory / 4) * 3;
// Pending final releases still count against this budget. Reclaim them
// before evicting a live, useful texture mapping.
m_memoryAllocator.drainRetired();

auto iter = m_mappedTextures.leastRecentlyUsedIter();
while (m_memoryAllocator.getStats().memoryMapped >= threshold && iter != m_mappedTextures.leastRecentlyUsedEndIter()) {
while (m_memoryAllocator.getStats().memoryMapped > budget && iter != m_mappedTextures.leastRecentlyUsedEndIter()) {
if (unlikely((*iter)->IsAnySubresourceLocked() != 0
|| (m_textureUploadMappingScope && m_textureUploadMappingScope->Contains(*iter)))) {
iter++;
Expand Down
19 changes: 19 additions & 0 deletions backend/dxvk/src/d3d9/d3d9_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ namespace dxvk {
D3D9DeviceEx* m_device;
};

class D3D9BridgeTelemetry final : public ID3D9BridgeTelemetry {
public:
explicit D3D9BridgeTelemetry(D3D9DeviceEx* device) : m_device(device) { }
ULONG STDMETHODCALLTYPE AddRef() override;
ULONG STDMETHODCALLTYPE Release() override;
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) override;
HRESULT STDMETHODCALLTYPE StartCapture(const D3D9BridgeTelemetryConfig*) override;
HRESULT STDMETHODCALLTYPE StopCapture(UINT64) override;
HRESULT STDMETHODCALLTYPE ReadEvents(UINT64, D3D9BridgeTelemetryRead*, D3D9BridgeTelemetryEvent*, UINT) override;
HRESULT STDMETHODCALLTYPE GetMemorySnapshot(UINT64, D3D9BridgeMemorySnapshot*,
D3D9BridgeMemoryChunk*, UINT, UINT*, UINT) 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 {
Expand Down Expand Up @@ -1278,6 +1293,9 @@ namespace dxvk {
*/
void* MapTexture(D3D9CommonTexture* pTexture, UINT Subresource);

/** Registers an already initialized mapping, then enforces its budget. */
void RetainInitializedTextureMapping(D3D9CommonTexture* pTexture);

/**
* \brief Moves the texture to the front of the LRU list of mapped textures
*/
Expand Down Expand Up @@ -1992,6 +2010,7 @@ namespace dxvk {

D3D9BridgeDiagnostics m_bridgeDiagnostics;
D3D9BridgeThreadStats m_threadStats { this };
D3D9BridgeTelemetry m_bridgeTelemetry { this };
};

}
8 changes: 6 additions & 2 deletions backend/dxvk/src/d3d9/d3d9_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ namespace dxvk {

if (mapPtr != nullptr) {
InitHostVisibleTexture(pTexture, pInitialData, mapPtr);
pTexture->UnmapData();
}

SyncSharedTexture(pTexture);

// Publish only once initialization and shared synchronization no longer
// access the mapped pointer. Creation does not hold the device lock.
if (mapPtr != nullptr)
m_parent->RetainInitializedTextureMapping(pTexture);
}


Expand Down Expand Up @@ -135,7 +139,7 @@ namespace dxvk {
pTexture->Desc()->ArraySize,
formatInfo,
VK_IMAGE_ASPECT_COLOR_BIT);
} else {
} else if (!pTexture->HasZeroInitializedMemory()) {
// All subresources are allocated in one chunk of memory.
// So we can just get the pointer for subresource 0 and memset all of them at once.
std::memset(
Expand Down
Loading
Loading