Skip to content

Commit cb7c982

Browse files
committed
Upscaler/Renderer: Fix RE4 and use heuristic scanning
1 parent 8ef3422 commit cb7c982

5 files changed

Lines changed: 131 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14563,8 +14563,10 @@ if(REF_BUILD_PRAGMATA_SDK OR REF_BUILD_FRAMEWORK) # build-pragmata-sdk
1456314563
"shared/sdk/regenny/re3/via/vec4.hpp"
1456414564
"shared/sdk/regenny/re4/BullShit.hpp"
1456514565
"shared/sdk/regenny/re4/DeserializeSequence.hpp"
14566+
"shared/sdk/regenny/re4/ID3D12Resource.hpp"
1456614567
"shared/sdk/regenny/re4/ManagedVtable.hpp"
1456714568
"shared/sdk/regenny/re4/RTInternal.hpp"
14569+
"shared/sdk/regenny/re4/RTL_CRITICAL_SECTION_DEBUG.hpp"
1456814570
"shared/sdk/regenny/re4/RayTrace.hpp"
1456914571
"shared/sdk/regenny/re4/RenderEntity.hpp"
1457014572
"shared/sdk/regenny/re4/System/String.hpp"
@@ -14584,6 +14586,7 @@ if(REF_BUILD_PRAGMATA_SDK OR REF_BUILD_FRAMEWORK) # build-pragmata-sdk
1458414586
"shared/sdk/regenny/re4/tdb71/TypeDefinition.hpp"
1458514587
"shared/sdk/regenny/re4/tdb71/TypeImpl.hpp"
1458614588
"shared/sdk/regenny/re4/via/BasisPlane.hpp"
14589+
"shared/sdk/regenny/re4/via/Camera.hpp"
1458714590
"shared/sdk/regenny/re4/via/CameraType.hpp"
1458814591
"shared/sdk/regenny/re4/via/Capsule.hpp"
1458914592
"shared/sdk/regenny/re4/via/Color.hpp"
@@ -14661,9 +14664,29 @@ if(REF_BUILD_PRAGMATA_SDK OR REF_BUILD_FRAMEWORK) # build-pragmata-sdk
1466114664
"shared/sdk/regenny/re4/via/motion/SecondaryAnimation.hpp"
1466214665
"shared/sdk/regenny/re4/via/motion/TransitionData.hpp"
1466314666
"shared/sdk/regenny/re4/via/motion/TransitionMap.hpp"
14667+
"shared/sdk/regenny/re4/via/render/DXResource.hpp"
14668+
"shared/sdk/regenny/re4/via/render/DepthStencilView.hpp"
14669+
"shared/sdk/regenny/re4/via/render/DepthStencilViewDX12.hpp"
14670+
"shared/sdk/regenny/re4/via/render/LayerArray.hpp"
14671+
"shared/sdk/regenny/re4/via/render/Mirror.hpp"
14672+
"shared/sdk/regenny/re4/via/render/Poop.hpp"
14673+
"shared/sdk/regenny/re4/via/render/RTL_CRITICAL_SECTION.hpp"
14674+
"shared/sdk/regenny/re4/via/render/RenderLayer.hpp"
1466414675
"shared/sdk/regenny/re4/via/render/RenderOutput.hpp"
14676+
"shared/sdk/regenny/re4/via/render/RenderResource.hpp"
14677+
"shared/sdk/regenny/re4/via/render/RenderTargetView.hpp"
14678+
"shared/sdk/regenny/re4/via/render/RenderTargetViewDX12.hpp"
1466514679
"shared/sdk/regenny/re4/via/render/SceneArray2.hpp"
14680+
"shared/sdk/regenny/re4/via/render/SceneInfo.hpp"
14681+
"shared/sdk/regenny/re4/via/render/TargetState.hpp"
14682+
"shared/sdk/regenny/re4/via/render/Texture.hpp"
14683+
"shared/sdk/regenny/re4/via/render/TextureDX12.hpp"
14684+
"shared/sdk/regenny/re4/via/render/TextureDesc.hpp"
14685+
"shared/sdk/regenny/re4/via/render/TextureFormat.hpp"
14686+
"shared/sdk/regenny/re4/via/render/TextureStreamingType.hpp"
14687+
"shared/sdk/regenny/re4/via/render/UsageType.hpp"
1466614688
"shared/sdk/regenny/re4/via/render/layer/PrepareOutput.hpp"
14689+
"shared/sdk/regenny/re4/via/render/layer/Scene.hpp"
1466714690
"shared/sdk/regenny/re4/via/typeinfo/TypeInfo.hpp"
1466814691
"shared/sdk/regenny/re4/via/vec3.hpp"
1466914692
"shared/sdk/regenny/re4/via/vec4.hpp"
@@ -15034,6 +15057,8 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
1503415057
"src/mods/Scene.hpp"
1503515058
"src/mods/ScriptRunner.cpp"
1503615059
"src/mods/ScriptRunner.hpp"
15060+
"src/mods/TemporalUpscaler.cpp"
15061+
"src/mods/TemporalUpscaler.hpp"
1503715062
"src/mods/VR.cpp"
1503815063
"src/mods/VR.hpp"
1503915064
"src/mods/bindings/FS.cpp"
@@ -15052,6 +15077,8 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
1505215077
"src/mods/tools/ObjectExplorer.cpp"
1505315078
"src/mods/tools/ObjectExplorer.hpp"
1505415079
"src/mods/vr/Bindings.cpp"
15080+
"src/mods/vr/CameraDuplicator.cpp"
15081+
"src/mods/vr/CameraDuplicator.hpp"
1505515082
"src/mods/vr/D3D11Component.cpp"
1505615083
"src/mods/vr/D3D11Component.hpp"
1505715084
"src/mods/vr/D3D12Component.cpp"
@@ -15061,6 +15088,8 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
1506115088
"src/mods/vr/d3d12/ComPtr.hpp"
1506215089
"src/mods/vr/d3d12/CommandContext.cpp"
1506315090
"src/mods/vr/d3d12/CommandContext.hpp"
15091+
"src/mods/vr/d3d12/DirectXTK.cpp"
15092+
"src/mods/vr/d3d12/DirectXTK.hpp"
1506415093
"src/mods/vr/d3d12/ResourceCopier.cpp"
1506515094
"src/mods/vr/d3d12/ResourceCopier.hpp"
1506615095
"src/mods/vr/d3d12/TextureContext.cpp"
@@ -15144,13 +15173,14 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
1514415173
delayimp
1514515174
DirectXTK
1514615175
DirectXTK12
15176+
pdperfmod
1514715177
)
1514815178

1514915179
set_target_properties(PRAGMATA PROPERTIES
1515015180
OUTPUT_NAME
1515115181
dinput8
1515215182
LINK_FLAGS
15153-
"/DELAYLOAD:openvr_api.dll /DELAYLOAD:openxr_loader.dll /DELAYLOAD:d3d11.dll /DELAYLOAD:d3d12.dll /DELAYLOAD:D3DCOMPILER_47.dll"
15183+
"/DELAYLOAD:openvr_api.dll /DELAYLOAD:openxr_loader.dll /DELAYLOAD:PDPerfPlugin.dll /DELAYLOAD:d3d11.dll /DELAYLOAD:d3d12.dll /DELAYLOAD:D3DCOMPILER_47.dll"
1515415184
RUNTIME_OUTPUT_DIRECTORY_RELEASE
1515515185
"${CMAKE_BINARY_DIR}/bin/${CMKR_TARGET}"
1515615186
RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO

reversing/re4.genny

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,8 @@ struct RenderResource {
12371237
Poop* unkpoop;
12381238
int32_t ref_count @ 8;
12391239
uint32_t render_frame
1240+
1241+
void* padding;
12401242
};
12411243

12421244
struct TextureDesc {

shared/sdk/Renderer.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,98 @@ ID3D12Resource* TargetState::get_native_resource_d3d12() const {
14971497
return internal_resource->get_native_resource();
14981498
}
14991499

1500+
DirectXResource<ID3D12Resource>* Texture::get_d3d12_resource_container() {
1501+
#if TDB_VER < 71
1502+
return *(DirectXResource<ID3D12Resource>**)((uintptr_t)this + s_d3d12_resource_offset);
1503+
#else
1504+
static std::optional<size_t> offset = std::nullopt;
1505+
1506+
if (offset) {
1507+
return *(DirectXResource<ID3D12Resource>**)((uintptr_t)this + *offset);
1508+
}
1509+
1510+
spdlog::info("Searching for Texture D3D12Resource offset");
1511+
1512+
// Scan through past offset 98 looking for pointers that contain
1513+
// vtable pointer to either D3D12Core.dll or dxgi/d3d12.dll
1514+
for (size_t i = 0x98; i < 0x200; i += sizeof(void*)) try {
1515+
if (offset) {
1516+
break;
1517+
}
1518+
1519+
const auto potential_ptr = *(uintptr_t*)((uintptr_t)this + i);
1520+
1521+
if (potential_ptr == 0) {
1522+
continue;
1523+
}
1524+
1525+
// Make sure this has a valid vtable pointer
1526+
const auto vtable_ptr = *(uintptr_t*)potential_ptr;
1527+
1528+
if (vtable_ptr == 0 || !utility::get_module_within(vtable_ptr)) {
1529+
continue;
1530+
}
1531+
1532+
// Scan memory of this object looking for another pointer that contains a vtable pointer to d3d12.dll or D3D12Core.dll
1533+
for (size_t j = 0; j < sizeof(RenderResource) + 0x18; j += sizeof(void*)) try {
1534+
const auto inner_potential_ptr = *(uintptr_t*)(potential_ptr + j);
1535+
1536+
if (inner_potential_ptr == 0) {
1537+
continue;
1538+
}
1539+
1540+
const auto inner_vtable_ptr = *(uintptr_t*)inner_potential_ptr;
1541+
1542+
if (inner_vtable_ptr == 0) {
1543+
continue;
1544+
}
1545+
1546+
const auto module = utility::get_module_within(inner_vtable_ptr);
1547+
1548+
if (module) {
1549+
const auto module_name = utility::get_module_pathw(*module);
1550+
1551+
if (!module_name.has_value()) {
1552+
continue;
1553+
}
1554+
1555+
std::wstring module_name_lower = *module_name;
1556+
std::transform(module_name_lower.begin(), module_name_lower.end(), module_name_lower.begin(), ::towlower);
1557+
1558+
auto is_vtable_d3d = [](std::wstring_view module_path_lower) {
1559+
return module_path_lower.ends_with(L"d3d11.dll") ||
1560+
module_path_lower.ends_with(L"d3d12.dll") ||
1561+
module_path_lower.ends_with(L"d3d12core.dll") ||
1562+
module_path_lower.ends_with(L"dxgi.dll") ||
1563+
module_path_lower.ends_with(L"d3d12sdklayers.dll") ||
1564+
module_path_lower.ends_with(L"d3d11_1sdklayers.dll") ||
1565+
module_path_lower.ends_with(L"d3d11_2sdklayers.dll") ||
1566+
module_path_lower.ends_with(L"d3d11_3sdklayers.dll") ||
1567+
module_path_lower.ends_with(L"d3d11on12.dll");
1568+
};
1569+
1570+
// Standard path for semi-newer UE versions
1571+
if (is_vtable_d3d(module_name_lower)) {
1572+
spdlog::info("Found Texture D3D12Resource at offset {:x}", i);
1573+
offset = i;
1574+
return *(DirectXResource<ID3D12Resource>**)((uintptr_t)this + *offset);
1575+
}
1576+
}
1577+
} catch(...) {
1578+
continue;
1579+
}
1580+
} catch(...) {
1581+
continue;
1582+
}
1583+
1584+
if (offset) {
1585+
return *(DirectXResource<ID3D12Resource>**)((uintptr_t)this + *offset);
1586+
}
1587+
1588+
return nullptr;
1589+
#endif
1590+
}
1591+
15001592
Texture* Texture::clone() {
15011593
return sdk::renderer::create_texture(get_desc());
15021594
}

shared/sdk/Renderer.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ class Texture : public RenderResource {
8585
return (Desc*)((uintptr_t)this + s_desc_offset);
8686
}
8787

88-
DirectXResource<ID3D12Resource>* get_d3d12_resource_container() {
89-
return *(DirectXResource<ID3D12Resource>**)((uintptr_t)this + s_d3d12_resource_offset);
90-
}
88+
DirectXResource<ID3D12Resource>* get_d3d12_resource_container();
9189

9290
private:
9391
#if TDB_VER >= 73 || defined(SF6)
@@ -199,8 +197,12 @@ static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rt
199197
#ifdef SF6
200198
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x28);
201199
#else
200+
#ifdef RE4
201+
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x28);
202+
#else
202203
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x20);
203204
#endif
205+
#endif
204206
#else
205207
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x28);
206208
#endif

shared/sdk/renderer/RenderResource.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RenderResource {
1616
int32_t m_ref_count;
1717
uint32_t m_render_frame;
1818

19-
#if defined(SF6) || TDB_VER >= 73 || TDB_VER <= 67
19+
#if defined(SF6) || TDB_VER >= 73 || TDB_VER <= 67 || defined (RE4) /* idk why new re4 has this. */
2020
void* _tdb73_padding; // something added here in Dec 1 2023 update
2121
#endif
2222
};

0 commit comments

Comments
 (0)