Skip to content

Commit 82e19ec

Browse files
committed
Graphics: Add new path tracing option seen in Dead Rising
1 parent 870f190 commit 82e19ec

File tree

3 files changed

+144
-16
lines changed

3 files changed

+144
-16
lines changed

src/Mod.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ class ModCombo : public ModValue<int32_t> {
229229
ImGui::Text("%s: %s", name.data(), m_options[m_value]);
230230
}
231231

232+
void recreate_options(const std::vector<std::string>& options) {
233+
m_options_stdstr = options;
234+
m_options.clear();
235+
236+
for (auto& o : m_options_stdstr) {
237+
m_options.push_back(o.c_str());
238+
}
239+
}
240+
232241
const auto& options() const {
233242
return m_options;
234243
}

src/mods/Graphics.cpp

+92-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,76 @@ std::shared_ptr<Graphics>& Graphics::get() {
4242
return mod;
4343
}
4444

45+
std::optional<std::string> Graphics::on_initialize() {
46+
#if TDB_VER >= 69
47+
const auto raytracing_enum = sdk::find_type_definition("via.render.ExperimentalRayTrace.Raytracing");
48+
49+
if (raytracing_enum == nullptr) {
50+
return Mod::on_initialize(); // OK
51+
}
52+
53+
s_ray_trace_type.clear();
54+
s_ray_trace_type.push_back("Disabled");
55+
56+
s_ray_trace_type.resize(raytracing_enum->get_fields().size() + 1);
57+
58+
int32_t actual_size = 1;
59+
60+
for (auto f : raytracing_enum->get_fields()) {
61+
const auto field_flags = f->get_flags();
62+
63+
if ((field_flags & (uint16_t)via::clr::FieldFlag::Static) != 0 && (field_flags & (uint16_t)via::clr::FieldFlag::Literal) != 0) {
64+
auto raw_data = f->get_data_raw(nullptr, true);
65+
int64_t enum_data = 0;
66+
67+
switch(raytracing_enum->get_valuetype_size()) {
68+
case 1:
69+
enum_data = (int64_t)*(int8_t*)raw_data;
70+
break;
71+
case 2:
72+
enum_data = (int64_t)*(int16_t*)raw_data;
73+
break;
74+
case 4:
75+
enum_data = (int64_t)*(int32_t*)raw_data;
76+
break;
77+
case 8:
78+
enum_data = *(int64_t*)raw_data;
79+
break;
80+
default:
81+
spdlog::error("Unknown enum size: {}", raytracing_enum->get_valuetype_size());
82+
break;
83+
}
84+
85+
if (enum_data < 0 || enum_data + 1 >= s_ray_trace_type.size()) {
86+
spdlog::error("Invalid enum data: {} {}", f->get_name(), enum_data);
87+
continue;
88+
}
89+
90+
auto unfriendly_name = std::string{f->get_name()};
91+
92+
// Format into a friendly name (Spacing between words)
93+
for (size_t i = 1; i < unfriendly_name.size(); ++i) {
94+
if (unfriendly_name[i] >= 'A' && unfriendly_name[i] <= 'Z') {
95+
unfriendly_name.insert(i, " ");
96+
i++;
97+
}
98+
}
99+
100+
s_ray_trace_type[enum_data + 1] = unfriendly_name;
101+
++actual_size;
102+
}
103+
}
104+
105+
s_ray_trace_type.resize(actual_size);
106+
m_ray_trace_type->recreate_options(s_ray_trace_type);
107+
m_ray_trace_clone_type_pre->recreate_options(s_ray_trace_type);
108+
m_ray_trace_clone_type_post->recreate_options(s_ray_trace_type);
109+
m_ray_trace_clone_type_true->recreate_options(s_ray_trace_type);
110+
#endif
111+
112+
return Mod::on_initialize(); // OK
113+
}
114+
45115
void Graphics::on_config_load(const utility::Config& cfg) {
46116
for (IModValue& option : m_options) {
47117
option.config_load(cfg);
@@ -126,6 +196,11 @@ void Graphics::on_draw_ui() {
126196

127197
if (m_ray_tracing_tweaks->value()) {
128198
m_ray_trace_disable_raster_shadows->draw("Disable Raster Shadows (with PT)");
199+
m_ray_trace_always_recreate_rt_component->draw("Always Recreate RT Component");
200+
// Description of the above option
201+
if (ImGui::IsItemHovered()) {
202+
ImGui::SetTooltip("Recreates the RT component. Useful if Ray Tracing Tweaks is not working.");
203+
}
129204
m_ray_trace_type->draw("Ray Trace Type");
130205

131206
const auto clone_tooltip =
@@ -152,9 +227,7 @@ void Graphics::on_draw_ui() {
152227
}
153228

154229
// Hybrid/pure
155-
if (m_ray_trace_type->value() == (int32_t)RayTraceType::Hybrid || m_ray_trace_type->value() == (int32_t)RayTraceType::Pure
156-
|| m_ray_trace_clone_type_true->value() == (int32_t)RayTraceType::Hybrid || m_ray_trace_clone_type_true->value() == (int32_t)RayTraceType::Pure)
157-
{
230+
if (is_pt_type(m_ray_trace_type->value()) || is_pt_type(m_ray_trace_clone_type_true->value())) {
158231
m_bounce_count->draw("Bounce Count");
159232
m_samples_per_pixel->draw("Samples Per Pixel");
160233
}
@@ -929,11 +1002,16 @@ void Graphics::setup_rt_component() {
9291002
auto rt_component = utility::re_component::find<REComponent>(game_object->transform, rt_t->get_type());
9301003

9311004
// Attempt to create the component if it doesn't exist
932-
if (rt_component == nullptr) {
1005+
if (rt_component == nullptr || (m_ray_trace_always_recreate_rt_component->value() && m_rt_recreated_component.get() != (sdk::ManagedObject*)rt_component)) {
1006+
if (rt_component != nullptr) {
1007+
sdk::call_object_func_easy<void*>(rt_component, "destroy", rt_component);
1008+
}
1009+
9331010
rt_component = sdk::call_object_func_easy<REComponent*>(game_object, "createComponent(System.Type)", rt_t->get_runtime_type());
9341011

9351012
if (rt_component != nullptr) {
9361013
spdlog::info("[Graphics] Successfully created new RT component @ {:x}", (uintptr_t)rt_component);
1014+
m_rt_recreated_component = (sdk::ManagedObject*)rt_component;
9371015
}
9381016
}
9391017

@@ -946,7 +1024,7 @@ void Graphics::setup_rt_component() {
9461024
m_rt_component = (sdk::ManagedObject*)rt_component;
9471025
}
9481026

949-
if (m_rt_cloned_component.get() == nullptr && m_ray_trace_clone_type_true->value() > (int32_t)RayTraceType::Disabled) {
1027+
if (m_rt_cloned_component.get() == nullptr && m_ray_trace_clone_type_true->value() > 0) {
9501028
m_rt_cloned_component = (sdk::ManagedObject*)rt_t->create_instance_full(false);
9511029

9521030
if (m_rt_cloned_component.get() != nullptr) {
@@ -986,11 +1064,13 @@ void Graphics::apply_ray_tracing_tweaks() {
9861064
return;
9871065
}
9881066

989-
if (set_RaytracingMode != nullptr && rt_type > (int32_t)RayTraceType::Disabled) {
990-
any_pt = any_pt || rt_type == (int32_t)RayTraceType::Pure;
1067+
const auto is_pure_pt = is_pure_pt_type(rt_type);
1068+
1069+
if (set_RaytracingMode != nullptr && rt_type > 0) {
1070+
any_pt = any_pt || is_pure_pt;
9911071
set_RaytracingMode->call<void>(context, target, rt_type - 1);
9921072

993-
if (rt_type == (int32_t)RayTraceType::Pure && m_ray_trace_disable_raster_shadows->value()) {
1073+
if (is_pure_pt && m_ray_trace_disable_raster_shadows->value()) {
9941074
if (get_DynamicShadowEnable != nullptr && set_DynamicShadowEnable != nullptr) {
9951075
const bool is_shadow_enabled = get_DynamicShadowEnable->call<bool>(context);
9961076

@@ -1003,7 +1083,7 @@ void Graphics::apply_ray_tracing_tweaks() {
10031083
}
10041084
}
10051085

1006-
if (rt_type == (int32_t)RayTraceType::Hybrid || rt_type == (int32_t)RayTraceType::Pure) {
1086+
if (is_pt_type(rt_type)) { // hybrid or any pure
10071087
if (setBounce != nullptr) {
10081088
setBounce->call<void>(context, target, m_bounce_count->value());
10091089
}
@@ -1034,7 +1114,7 @@ void* Graphics::rt_draw_hook(REComponent* rt, void* draw_context, void* r8, void
10341114
return og(rt, draw_context, r8, r9);
10351115
}
10361116

1037-
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_true->value() > (int32_t)RayTraceType::Disabled) {
1117+
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_true->value() > 0) {
10381118
static std::recursive_mutex mtx{};
10391119
std::scoped_lock _{mtx};
10401120

@@ -1086,7 +1166,7 @@ void* Graphics::rt_draw_impl_hook(void* rt_impl, void* draw_context, void* r8, v
10861166
.unk = unk
10871167
};
10881168

1089-
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_pre->value() > (int32_t)RayTraceType::Disabled) {
1169+
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_pre->value() > 0) {
10901170
ray_tracing_mode = graphics->m_ray_trace_clone_type_pre->value() - 1;
10911171
og(rt_impl, draw_context, r8, r9, unk);
10921172
ray_tracing_mode = old_mode;
@@ -1097,7 +1177,7 @@ void* Graphics::rt_draw_impl_hook(void* rt_impl, void* draw_context, void* r8, v
10971177
result = og(rt_impl, draw_context, r8, r9, unk);
10981178
}*/
10991179

1100-
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_post->value() > (int32_t)RayTraceType::Disabled) {
1180+
if (graphics->m_ray_tracing_tweaks->value() && graphics->m_ray_trace_clone_type_post->value() > 0) {
11011181
ray_tracing_mode = graphics->m_ray_trace_clone_type_post->value() - 1;
11021182
og(rt_impl, draw_context, r8, r9, unk);
11031183
ray_tracing_mode = old_mode;

src/mods/Graphics.hpp

+43-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Graphics : public Mod {
1717
static std::shared_ptr<Graphics>& get();
1818

1919
public:
20+
std::optional<std::string> on_initialize() override;
21+
2022
std::string_view get_name() const override { return "Graphics"; };
2123

2224
void on_config_load(const utility::Config& cfg) override;
@@ -139,6 +141,7 @@ class Graphics : public Mod {
139141
std::optional<size_t> m_rt_type_offset{};
140142
sdk::intrusive_ptr<sdk::ManagedObject> m_rt_component{};
141143
sdk::intrusive_ptr<sdk::ManagedObject> m_rt_cloned_component{};
144+
sdk::intrusive_ptr<sdk::ManagedObject> m_rt_recreated_component{};
142145
void* m_pt_pipeline_resource{nullptr};
143146
void* m_dxr_shader_resource{nullptr};
144147

@@ -185,7 +188,7 @@ class Graphics : public Mod {
185188
const ModToggle::Ptr m_shader_playground { ModToggle::create(generate_name("ShaderPlayground"), false) };
186189
const ModToggle::Ptr m_ray_tracing_tweaks { ModToggle::create(generate_name("RayTracingTweaks"), false) };
187190

188-
enum class RayTraceType : uint8_t {
191+
/*enum class RayTraceType : uint8_t {
189192
Disabled = 0,
190193
AmbientOcclusion = 1,
191194
Hybrid = 2,
@@ -194,19 +197,53 @@ class Graphics : public Mod {
194197
ScreenSpacePhotonMapping = 5,
195198
Debug = 6,
196199
ASVGF = 7,
197-
};
200+
};*/
198201

199-
static const inline std::vector<std::string> s_ray_trace_type {
202+
// We will replace this dynamically with reflected data upon loading
203+
static inline std::vector<std::string> s_ray_trace_type {
200204
"Disabled",
201205
"Ambient Occlusion",
202206
"Hybrid Path Tracing",
203207
"Pure Path Tracing",
204208
"Path Space Filter",
205209
"Screen Space Photon Mapping",
206210
"Debug",
207-
"ASVGF",
211+
"A S V G F",
208212
};
209213

214+
static std::string_view get_ray_trace_type_name(uint8_t type) {
215+
if (type >= s_ray_trace_type.size()) {
216+
return "Unknown";
217+
}
218+
219+
return s_ray_trace_type[type];
220+
}
221+
222+
static bool is_pt_type(uint8_t type) {
223+
const auto name = get_ray_trace_type_name(type);
224+
225+
if (name == "Hybrid Path Tracing" || name == "Pure Path Tracing") {
226+
return true;
227+
}
228+
229+
// Added in TDB73, post dragon's dogma 2
230+
if (name == "Prototype Reference") {
231+
return true;
232+
}
233+
234+
return false;
235+
}
236+
237+
static bool is_pure_pt_type(uint8_t type) {
238+
const auto name = get_ray_trace_type_name(type);
239+
240+
if (name == "Pure Path Tracing" || name == "Prototype Reference") {
241+
return true;
242+
}
243+
244+
return false;
245+
}
246+
210247
static const inline std::vector<std::string> s_bounce_count {
211248
"0",
212249
"1",
@@ -223,6 +260,7 @@ class Graphics : public Mod {
223260

224261
const ModCombo::Ptr m_ray_trace_type{ ModCombo::create(generate_name("RayTraceType"), s_ray_trace_type) };
225262
const ModToggle::Ptr m_ray_trace_disable_raster_shadows{ ModToggle::create(generate_name("RayTraceDisableRasterShadowsWithPT"), true) };
263+
const ModToggle::Ptr m_ray_trace_always_recreate_rt_component{ ModToggle::create(generate_name("RayTraceAlwaysRecreateRTComponent"), false) };
226264
bool m_was_shadows_disabled{ false };
227265
const ModCombo::Ptr m_ray_trace_clone_type_true{ ModCombo::create(generate_name("RayTraceTrueCloneType"), s_ray_trace_type) };
228266
const ModCombo::Ptr m_ray_trace_clone_type_pre{ ModCombo::create(generate_name("RayTraceCloneTypePre"), s_ray_trace_type) };
@@ -256,6 +294,7 @@ class Graphics : public Mod {
256294
*m_ray_tracing_tweaks,
257295
*m_ray_trace_type,
258296
*m_ray_trace_disable_raster_shadows,
297+
*m_ray_trace_always_recreate_rt_component,
259298
*m_ray_trace_clone_type_true,
260299
*m_ray_trace_clone_type_pre,
261300
*m_ray_trace_clone_type_post,

0 commit comments

Comments
 (0)