mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:58:37 +00:00
A buffer too small for an entry now gets the size to retry with, for byte and interface entries alike, as vkd3d-proton's private store does. The test retries with the returned size for both kinds. Review on #419. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
7.1 KiB
C++
187 lines
7.1 KiB
C++
// Shared IUnknown/ID3D12Object/ID3D12DeviceChild implementation for the Vulkan-backed D3D12 COM objects
|
|
// (d3d12_com.cpp, d3d12_vulkan_device.cpp). Private to the runtime; include after the generated d3d12_stubs.h.
|
|
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <d3d12.h>
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <cwchar>
|
|
#include <mutex>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace rt::d3d12 {
|
|
|
|
void stub(const char* method);
|
|
|
|
// IUnknown and ID3D12Object for one implementation class. QueryInterface answers the listed public interfaces
|
|
// and the class's private Derived::kTag (which returns the implementation itself); anything else is logged with
|
|
// its IID and refused, as docs/d3d12_vulkan.md section 3.1 asks.
|
|
template<class Derived, class Stubs, class... Interfaces>
|
|
class Object : public Stubs {
|
|
public:
|
|
ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; }
|
|
ULONG STDMETHODCALLTYPE Release() override {
|
|
const ULONG left = --refs_;
|
|
if (!left) delete static_cast<Derived*>(this);
|
|
return left;
|
|
}
|
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override {
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
if (riid == Derived::kTag) {
|
|
*out = static_cast<Derived*>(this);
|
|
} else if (riid == __uuidof(IUnknown) || ((riid == __uuidof(Interfaces)) || ...)) {
|
|
*out = static_cast<Stubs*>(this); // one inheritance chain: every interface shares this pointer
|
|
} else {
|
|
std::fprintf(stderr,
|
|
"[rt] d3d12: QueryInterface: unknown IID {%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
|
|
static_cast<unsigned long>(riid.Data1), riid.Data2, riid.Data3, riid.Data4[0], riid.Data4[1],
|
|
riid.Data4[2], riid.Data4[3], riid.Data4[4], riid.Data4[5], riid.Data4[6], riid.Data4[7]);
|
|
return E_NOINTERFACE;
|
|
}
|
|
AddRef();
|
|
return S_OK;
|
|
}
|
|
|
|
// Private data (ID3D12Object): bytes, or with SetPrivateDataInterface an interface the object holds a reference
|
|
// to until the entry is replaced, removed or the object dies. GetPrivateData hands such an interface back with a
|
|
// reference added, as D3D12 does. A buffer too small for the entry gets DXGI_ERROR_MORE_DATA and the size it
|
|
// needs, so the caller can retry. SetName stores the name as WKPDID_D3DDebugObjectNameW, which is where D3D12
|
|
// keeps it, so GetPrivateData reads it back.
|
|
HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID guid, UINT* size, void* data) override {
|
|
if (!size) return E_INVALIDARG;
|
|
std::lock_guard lock(data_mutex_);
|
|
for (const auto& entry : data_) {
|
|
if (entry.key != guid) continue;
|
|
const UINT bytes = entry.object ? UINT(sizeof(IUnknown*)) : UINT(entry.bytes.size());
|
|
if (data && *size < bytes) {
|
|
*size = bytes; // the size to retry with, as vkd3d-proton does
|
|
return DXGI_ERROR_MORE_DATA;
|
|
}
|
|
if (data) {
|
|
if (entry.object) {
|
|
entry.object->AddRef();
|
|
std::memcpy(data, &entry.object, sizeof(IUnknown*));
|
|
} else {
|
|
std::memcpy(data, entry.bytes.data(), bytes);
|
|
}
|
|
}
|
|
*size = bytes;
|
|
return S_OK;
|
|
}
|
|
*size = 0;
|
|
return DXGI_ERROR_NOT_FOUND;
|
|
}
|
|
|
|
HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID guid, UINT size, const void* data) override {
|
|
if (size && !data) return E_INVALIDARG;
|
|
PrivateData entry{guid, {}, nullptr};
|
|
if (size) {
|
|
const auto* bytes = static_cast<const std::uint8_t*>(data);
|
|
entry.bytes.assign(bytes, bytes + size);
|
|
}
|
|
store(std::move(entry), size != 0);
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID guid, const IUnknown* object) override {
|
|
IUnknown* held = const_cast<IUnknown*>(object);
|
|
if (held) held->AddRef();
|
|
store(PrivateData{guid, {}, held}, held != nullptr);
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT STDMETHODCALLTYPE SetName(LPCWSTR name) override {
|
|
if (!name) return SetPrivateData(kDebugObjectNameW, 0, nullptr);
|
|
return SetPrivateData(kDebugObjectNameW, UINT((std::wcslen(name) + 1) * sizeof(wchar_t)), name);
|
|
}
|
|
|
|
// WKPDID_D3DDebugObjectNameW (d3dcommon.h), spelled out so nothing needs dxguid.lib.
|
|
static constexpr GUID kDebugObjectNameW{0x4cca5fd8, 0x921f, 0x42c8, {0x85, 0x99, 0xd2, 0x86, 0x6a, 0x3e, 0x5f, 0x7e}};
|
|
|
|
// The implementation behind a public interface pointer, or null when it is not a T. No reference is added.
|
|
template<class T>
|
|
static T* from(IUnknown* object) {
|
|
if (!object) return nullptr;
|
|
void* implementation = nullptr;
|
|
if (FAILED(object->QueryInterface(T::kTag, &implementation))) return nullptr;
|
|
static_cast<T*>(implementation)->Release(); // the caller holds its own reference
|
|
return static_cast<T*>(implementation);
|
|
}
|
|
|
|
protected:
|
|
virtual ~Object() {
|
|
for (auto& entry : data_)
|
|
if (entry.object) entry.object->Release();
|
|
}
|
|
|
|
private:
|
|
struct PrivateData {
|
|
GUID key;
|
|
std::vector<std::uint8_t> bytes;
|
|
IUnknown* object; // SetPrivateDataInterface: one reference held
|
|
};
|
|
|
|
// Replaces the entry for `entry.key` (or only removes it when `keep` is false). The replaced interface is
|
|
// released after the lock is dropped, since its Release may run arbitrary code.
|
|
void store(PrivateData entry, bool keep) {
|
|
IUnknown* released = nullptr;
|
|
{
|
|
std::lock_guard lock(data_mutex_);
|
|
for (auto it = data_.begin(); it != data_.end(); ++it) {
|
|
if (it->key != entry.key) continue;
|
|
released = it->object;
|
|
data_.erase(it);
|
|
break;
|
|
}
|
|
if (keep) data_.push_back(std::move(entry));
|
|
}
|
|
if (released) released->Release();
|
|
}
|
|
|
|
std::atomic<ULONG> refs_{1};
|
|
std::mutex data_mutex_;
|
|
std::vector<PrivateData> data_;
|
|
};
|
|
|
|
// An ID3D12DeviceChild: holds a reference to its device, as D3D12 children do, and GetDevice returns it. When the
|
|
// device sits behind the Xbox-ABI proxy, the reference is to the proxy, so the game never sees the inner device.
|
|
template<class Derived, class Stubs, class... Interfaces>
|
|
class Child : public Object<Derived, Stubs, Interfaces...> {
|
|
public:
|
|
void attach_device(IUnknown* device) {
|
|
if (device) device->AddRef();
|
|
if (device_) device_->Release();
|
|
device_ = device;
|
|
}
|
|
|
|
HRESULT STDMETHODCALLTYPE GetDevice(REFIID riid, void** out) override {
|
|
if (!out) return E_POINTER;
|
|
*out = nullptr;
|
|
return device_ ? device_->QueryInterface(riid, out) : E_NOINTERFACE;
|
|
}
|
|
|
|
protected:
|
|
~Child() override {
|
|
if (device_) device_->Release();
|
|
}
|
|
|
|
private:
|
|
IUnknown* device_ = nullptr;
|
|
};
|
|
|
|
template<class T>
|
|
HRESULT hand_out(T* object, REFIID riid, void** out) {
|
|
const HRESULT hr = object->QueryInterface(riid, out);
|
|
object->Release();
|
|
return hr;
|
|
}
|
|
|
|
} // namespace rt::d3d12
|