From d06fe4e6ba614b426d7d548ad9582142bbe779e3 Mon Sep 17 00:00:00 2001 From: narzoul Date: Sun, 19 Apr 2020 01:31:15 +0200 Subject: [PATCH] Hook Direct3D execute buffer interface --- DDrawCompat/DDrawCompat.vcxproj | 3 ++ DDrawCompat/DDrawCompat.vcxproj.filters | 9 +++++ .../Direct3d/Direct3dExecuteBuffer.cpp | 8 ++++ DDrawCompat/Direct3d/Direct3dExecuteBuffer.h | 16 ++++++++ DDrawCompat/Direct3d/Hooks.cpp | 26 +++++++++++++ DDrawCompat/Direct3d/Log.cpp | 37 +++++++++++++++++++ DDrawCompat/Direct3d/Log.h | 4 ++ .../Direct3dExecuteBufferVtblVisitor.h | 23 ++++++++++++ 8 files changed, 126 insertions(+) create mode 100644 DDrawCompat/Direct3d/Direct3dExecuteBuffer.cpp create mode 100644 DDrawCompat/Direct3d/Direct3dExecuteBuffer.h create mode 100644 DDrawCompat/Direct3d/Visitors/Direct3dExecuteBufferVtblVisitor.h diff --git a/DDrawCompat/DDrawCompat.vcxproj b/DDrawCompat/DDrawCompat.vcxproj index 2560c9c..23a714b 100644 --- a/DDrawCompat/DDrawCompat.vcxproj +++ b/DDrawCompat/DDrawCompat.vcxproj @@ -195,6 +195,7 @@ + @@ -204,6 +205,7 @@ + @@ -271,6 +273,7 @@ + diff --git a/DDrawCompat/DDrawCompat.vcxproj.filters b/DDrawCompat/DDrawCompat.vcxproj.filters index bfa63e8..3470305 100644 --- a/DDrawCompat/DDrawCompat.vcxproj.filters +++ b/DDrawCompat/DDrawCompat.vcxproj.filters @@ -381,6 +381,12 @@ Header Files\Direct3d + + Header Files\Direct3d\Visitors + + + Header Files\Direct3d + @@ -587,6 +593,9 @@ Source Files\Direct3d + + Source Files\Direct3d + diff --git a/DDrawCompat/Direct3d/Direct3dExecuteBuffer.cpp b/DDrawCompat/Direct3d/Direct3dExecuteBuffer.cpp new file mode 100644 index 0000000..e226714 --- /dev/null +++ b/DDrawCompat/Direct3d/Direct3dExecuteBuffer.cpp @@ -0,0 +1,8 @@ +#include + +namespace Direct3d +{ + void Direct3dExecuteBuffer::setCompatVtable(IDirect3DExecuteBufferVtbl& /*vtable*/) + { + } +} diff --git a/DDrawCompat/Direct3d/Direct3dExecuteBuffer.h b/DDrawCompat/Direct3d/Direct3dExecuteBuffer.h new file mode 100644 index 0000000..a6dfdaa --- /dev/null +++ b/DDrawCompat/Direct3d/Direct3dExecuteBuffer.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +namespace Direct3d +{ + class Direct3dExecuteBuffer : public CompatVtable + { + public: + static void setCompatVtable(IDirect3DExecuteBufferVtbl& vtable); + }; +} + +SET_COMPAT_VTABLE(IDirect3DExecuteBufferVtbl, Direct3d::Direct3dExecuteBuffer); diff --git a/DDrawCompat/Direct3d/Hooks.cpp b/DDrawCompat/Direct3d/Hooks.cpp index af0167a..2806433 100644 --- a/DDrawCompat/Direct3d/Hooks.cpp +++ b/DDrawCompat/Direct3d/Hooks.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ namespace { void hookDirect3dDevice(CompatRef d3d, CompatRef renderTarget); + void hookDirect3dExecuteBuffer(CompatRef dev); void hookDirect3dLight(CompatRef d3d); void hookDirect3dMaterial(CompatRef d3d); void hookDirect3dTexture(CompatRef dd); @@ -106,6 +108,30 @@ namespace hookVtable(d3dDevice); hookVtable(d3dDevice); hookVtable(d3dDevice); + + CompatPtr dev(d3dDevice); + if (dev) + { + hookDirect3dExecuteBuffer(*dev); + } + } + + void hookDirect3dExecuteBuffer(CompatRef dev) + { + D3DEXECUTEBUFFERDESC desc = {}; + desc.dwSize = sizeof(desc); + desc.dwFlags = D3DDEB_BUFSIZE; + desc.dwBufferSize = 1; + + CompatPtr buffer; + HRESULT result = dev->CreateExecuteBuffer(&dev, &desc, &buffer.getRef(), nullptr); + if (FAILED(result)) + { + Compat::Log() << "ERROR: Failed to create an execute buffer for hooking: " << Compat::hex(result); + return; + } + + hookVtable(buffer); } void hookDirect3dLight(CompatRef d3d) diff --git a/DDrawCompat/Direct3d/Log.cpp b/DDrawCompat/Direct3d/Log.cpp index 4ab0e25..163eb71 100644 --- a/DDrawCompat/Direct3d/Log.cpp +++ b/DDrawCompat/Direct3d/Log.cpp @@ -26,6 +26,26 @@ std::ostream& operator<<(std::ostream& os, const D3DDRAWPRIMITIVESTRIDEDDATA& da << Compat::array(data.textureCoords, D3DDP_MAXTEXCOORD); } +std::ostream& operator<<(std::ostream& os, const D3DEXECUTEBUFFERDESC& data) +{ + return Compat::LogStruct(os) + << Compat::hex(data.dwFlags) + << Compat::hex(data.dwCaps) + << data.dwBufferSize + << data.lpData; +} + +std::ostream& operator<<(std::ostream& os, const D3DEXECUTEDATA& data) +{ + return Compat::LogStruct(os) + << data.dwVertexOffset + << data.dwVertexCount + << data.dwInstructionOffset + << data.dwInstructionLength + << data.dwHVertexOffset + << data.dsStatus; +} + std::ostream& operator<<(std::ostream& os, const D3DLIGHT& data) { D3DLIGHT2 light = {}; @@ -87,6 +107,23 @@ std::ostream& operator<<(std::ostream& os, const D3DMATERIAL7& data) return os << material; } +std::ostream& operator<<(std::ostream& os, const D3DRECT& data) +{ + return Compat::LogStruct(os) + << data.x1 + << data.y1 + << data.x2 + << data.y2; +} + +std::ostream& operator<<(std::ostream& os, const D3DSTATUS& data) +{ + return Compat::LogStruct(os) + << Compat::hex(data.dwFlags) + << Compat::hex(data.dwStatus) + << data.drExtent; +} + std::ostream& operator<<(std::ostream& os, const D3DVERTEXBUFFERDESC& data) { return Compat::LogStruct(os) diff --git a/DDrawCompat/Direct3d/Log.h b/DDrawCompat/Direct3d/Log.h index f0ed0a5..6607755 100644 --- a/DDrawCompat/Direct3d/Log.h +++ b/DDrawCompat/Direct3d/Log.h @@ -7,9 +7,13 @@ std::ostream& operator<<(std::ostream& os, const D3DCOLORVALUE& data); std::ostream& operator<<(std::ostream& os, const D3DDP_PTRSTRIDE& data); std::ostream& operator<<(std::ostream& os, const D3DDRAWPRIMITIVESTRIDEDDATA& data); +std::ostream& operator<<(std::ostream& os, const D3DEXECUTEBUFFERDESC& data); +std::ostream& operator<<(std::ostream& os, const D3DEXECUTEDATA& data); std::ostream& operator<<(std::ostream& os, const D3DLIGHT& data); std::ostream& operator<<(std::ostream& os, const D3DLIGHT2& data); std::ostream& operator<<(std::ostream& os, const D3DLIGHT7& data); std::ostream& operator<<(std::ostream& os, const D3DMATERIAL& data); std::ostream& operator<<(std::ostream& os, const D3DMATERIAL7& data); +std::ostream& operator<<(std::ostream& os, const D3DRECT& data); +std::ostream& operator<<(std::ostream& os, const D3DSTATUS& data); std::ostream& operator<<(std::ostream& os, const D3DVERTEXBUFFERDESC& data); diff --git a/DDrawCompat/Direct3d/Visitors/Direct3dExecuteBufferVtblVisitor.h b/DDrawCompat/Direct3d/Visitors/Direct3dExecuteBufferVtblVisitor.h new file mode 100644 index 0000000..f899d29 --- /dev/null +++ b/DDrawCompat/Direct3d/Visitors/Direct3dExecuteBufferVtblVisitor.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include + +template <> +struct VtableForEach +{ + template + static void forEach(Visitor& visitor) + { + VtableForEach::forEach(visitor); + + DD_VISIT(Initialize); + DD_VISIT(Lock); + DD_VISIT(Unlock); + DD_VISIT(SetExecuteData); + DD_VISIT(GetExecuteData); + DD_VISIT(Validate); + DD_VISIT(Optimize); + } +};