Skip to content

Commit

Permalink
TemporalUpscaler: Fix in SF6 after Dec 1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Dec 2, 2023
1 parent 7801dbe commit d51f91c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
20 changes: 6 additions & 14 deletions reversing/sf6.genny
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ struct RenderOutput : via.Component 0x1000 {
SceneArray2 scenes @ 0x98
}

struct RenderResource {
struct RenderResource 0x18 {
//void* vtable;
Poop* unkpoop;
int32_t ref_count @ 8;
Expand Down Expand Up @@ -1263,23 +1263,15 @@ struct DXResource : via.render.RenderResource {
void* destructor;
};

struct Texture : RenderResource {
struct Texture : RenderResource 0x90 {
via.render.UsageType usage_type;
TextureDesc desc + 4;
uint32_t vram;
uint32_t pad_;
};

struct TextureDX12 : Texture 0x1000 {
uint16_t index + 2; // might not be right.
DXResource* unk_resource + 0xC;
uint32_t last_frame;
uint32_t locked_thing;
uint32_t scale;
bool b1;
bool b2;

DXResource* resource @ 0xA0
DXResource* resource + 0x28
// IDK
};

Expand Down Expand Up @@ -1311,12 +1303,12 @@ struct RenderLayer : via.clr.ManagedObject 0x88 {
uint32_t version
};

struct RenderTargetView : via.render.RenderResource 0x90 {
struct RenderTargetView : via.render.RenderResource 0x98 {
uint32_t format
uint32_t dimension
}

struct RenderTargetViewDX12 : RenderTargetView 0x100 {
struct RenderTargetViewDX12 : RenderTargetView 0x108 {
struct via.render.TargetState* output_target
TextureDX12* tex
}
Expand All @@ -1328,7 +1320,7 @@ struct DepthStencilView : via.render.RenderResource 0x18 {

struct DepthStencilViewDX12 : DepthStencilView 0x100 {
uint32_t format2
TextureDX12* tex @ 0x30
TextureDX12* tex + 0x20
}

struct TargetState : via.render.RenderResource 0x1000 {
Expand Down
46 changes: 46 additions & 0 deletions shared/sdk/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Application.hpp"
#include "RETypeDB.hpp"
#include "RETypes.hpp"
#include "SceneManager.hpp"

#include "Renderer.hpp"
Expand Down Expand Up @@ -1105,6 +1106,51 @@ sdk::intrusive_ptr<RenderTargetView> RenderTargetView::clone(uint32_t new_width,
return sdk::renderer::create_render_target_view(tex->clone(new_width, new_height), &get_desc());
}

namespace detail {
#if TDB_VER == 71
#ifdef SF6
constexpr auto rtv_size = 0x98;
#else
constexpr auto rtv_size = 0x98 - sizeof(void*);
#endif
#elif TDB_VER == 70
constexpr auto rtv_size = 0x90 - sizeof(void*);
#elif TDB_VER == 69
constexpr auto rtv_size = 0x88 - sizeof(void*);
#elif TDB_VER <= 67
// TODO: 66 and below
constexpr auto rtv_size = 0x88 - sizeof(void*);
#endif
}

sdk::intrusive_ptr<Texture>& RenderTargetView::get_texture_d3d12() const {
// The via.render.RenderTargetView is not part of the normal TDB... I think.
static const auto rtv_type = reframework::get_types()->get("via.render.RenderTargetView");

// The texture and target state members are always at the very start of the RenderTargetViewDX12 structure
// so we can very easily automate it like this, otherwise we fall back to the hardcoded offset
if (rtv_type != nullptr && rtv_type->size > 0 && rtv_type->size < 0x1000) {
const auto rtv_size = rtv_type->size;

return *(sdk::intrusive_ptr<Texture>*)((uintptr_t)this + rtv_size + sizeof(void*));
}

return *(sdk::intrusive_ptr<Texture>*)((uintptr_t)this + detail::rtv_size + sizeof(void*));
}

sdk::intrusive_ptr<TargetState>& RenderTargetView::get_target_state_d3d12() const {
// The via.render.RenderTargetView is not part of the normal TDB... I think.
static const auto rtv_type = reframework::get_types()->get("via.render.RenderTargetView");

if (rtv_type != nullptr && rtv_type->size > 0 && rtv_type->size < 0x1000) {
const auto rtv_size = rtv_type->size;

return *(sdk::intrusive_ptr<TargetState>*)((uintptr_t)this + rtv_size);
}

return *(sdk::intrusive_ptr<TargetState>*)((uintptr_t)this + detail::rtv_size);
}

sdk::intrusive_ptr<TargetState> TargetState::clone() const {
auto cloned_desc = get_desc();

Expand Down
38 changes: 13 additions & 25 deletions shared/sdk/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ template<typename T>
class DirectXResource : public RenderResource {
public:
T* get_native_resource() const {
#if TDB_VER > 67
return *(ID3D12Resource**)((uintptr_t)this + 0x10);
#else
return *(ID3D12Resource**)((uintptr_t)this + 0x18);
#endif
return *(ID3D12Resource**)((uintptr_t)this + sizeof(RenderResource));
}

private:
Expand Down Expand Up @@ -94,7 +90,13 @@ class Texture : public RenderResource {

private:
#if TDB_VER >= 71
#ifdef SF6
// So because this discrepancy in SF6 is > 8 bytes (which is how much was added to RenderResource), trying to automate this
// is a bit trickier so we can look into this later, and just hardcode it for now.
static constexpr inline auto s_d3d12_resource_offset = 0xB8;
#else
static constexpr inline auto s_d3d12_resource_offset = 0xA0;
#endif
#elif TDB_VER == 70
static constexpr inline auto s_d3d12_resource_offset = 0x98;
#elif TDB_VER == 69
Expand Down Expand Up @@ -124,29 +126,11 @@ class RenderTargetView : public RenderResource {
return m_desc;
}

sdk::intrusive_ptr<Texture>& get_texture_d3d12() const {
return *(sdk::intrusive_ptr<Texture>*)((uintptr_t)this + s_texture_d3d12_offset);
}

sdk::intrusive_ptr<TargetState>& get_target_state_d3d12() const {
return *(sdk::intrusive_ptr<TargetState>*)((uintptr_t)this + s_target_state_d3d12_offset);
}
sdk::intrusive_ptr<Texture>& get_texture_d3d12() const;
sdk::intrusive_ptr<TargetState>& get_target_state_d3d12() const;

private:
Desc m_desc;

#if TDB_VER == 71
static constexpr inline auto s_texture_d3d12_offset = 0x98;
#elif TDB_VER == 70
static constexpr inline auto s_texture_d3d12_offset = 0x90;
#elif TDB_VER == 69
static constexpr inline auto s_texture_d3d12_offset = 0x88;
#elif TDB_VER <= 67
// TODO: 66 and below
static constexpr inline auto s_texture_d3d12_offset = 0x88;
#endif

static constexpr inline auto s_target_state_d3d12_offset = s_texture_d3d12_offset - sizeof(void*);
};

static_assert(sizeof(RenderTargetView::Desc) == 0x14);
Expand Down Expand Up @@ -202,7 +186,11 @@ class TargetState : public RenderResource {
// more here but not needed... for now
};
#if TDB_VER > 67
#ifdef SF6
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x28);
#else
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x20);
#endif
#else
static_assert(offsetof(TargetState, m_desc) + offsetof(TargetState::Desc, num_rtv) == 0x28);
#endif
Expand Down
4 changes: 4 additions & 0 deletions shared/sdk/renderer/RenderResource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ class RenderResource {
void* m_vtable;
int32_t m_ref_count;
uint32_t m_render_frame;

#ifdef SF6
void* _sf6_padding; // something added here in Dec 1 2023 update
#endif
};
}

0 comments on commit d51f91c

Please sign in to comment.