Skip to content

Commit e4d7b15

Browse files
Need for Speed 2016: Add the Luma mod, under the native hooks
Needs Anti-Aliasing set to TAA in the game's video settings. - The mod from Filoppi#277: SR (DLSS, FSR 4, XeSS, FSR 3) in place of the TAA resolve, SR's Halton in the engine's jitter table, and the game's hidden resolution scale as a setting. Built on DristoforColumb's Mass Effect Andromeda mod; FSR 4 on DX11 uses izueh's interop. - The camera capture moves from ReShade's events to core's hooks: PostMap, OnResourceDestroyed and PreVSSetConstantBuffers, which also sees VSSetConstantBuffers1 (the camera's offset now comes from first_constant). SetGame<>() installs them. - CHECK_GRAPHICS_API_COMPATIBILITY is gone: only ReShade wrapped the SR interop's D3D12 device. Logs go to Luma.log, the Resolution Scale is saved in Luma.toml, and the credits list SafetyHook instead of ReShade. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent efd1ebb commit e4d7b15

15 files changed

Lines changed: 2164 additions & 0 deletions

‎Luma.sln‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Middle-earth Shadow of War"
126126
EndProject
127127
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Need For Speed Rivals", "Source\Games\Need For Speed Rivals\Need For Speed Rivals.vcxproj", "{935A2A28-2CE7-4F7A-96AE-964AEDEDC952}"
128128
EndProject
129+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Need for Speed 2016", "Source\Games\Need for Speed 2016\Need for Speed 2016.vcxproj", "{76FF7FB7-7635-4131-8DCB-48583740961D}"
130+
EndProject
129131
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Halo The Master Chief Collection", "Source\Games\Halo The Master Chief Collection\Halo The Master Chief Collection.vcxproj", "{6A95DCDD-FD5F-4064-B12C-F6602E31FEEE}"
130132
EndProject
131133
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "The Witcher 2", "Source\Games\The Witcher 2\The Witcher 2.vcxproj", "{8F4A1D26-93BC-4E07-A5D1-64C2E7B9F310}"
@@ -904,6 +906,18 @@ Global
904906
{935A2A28-2CE7-4F7A-96AE-964AEDEDC952}.Test-Release|Win32.ActiveCfg = Test-Release|x64
905907
{935A2A28-2CE7-4F7A-96AE-964AEDEDC952}.Test-Release|x64.ActiveCfg = Test-Release|x64
906908
{935A2A28-2CE7-4F7A-96AE-964AEDEDC952}.Test-Release|x64.Build.0 = Test-Release|x64
909+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Debug|Win32.ActiveCfg = Development-Debug|x64
910+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Debug|x64.ActiveCfg = Development-Debug|x64
911+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Debug|x64.Build.0 = Development-Debug|x64
912+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Release|Win32.ActiveCfg = Development-Release|x64
913+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Release|x64.ActiveCfg = Development-Release|x64
914+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Development-Release|x64.Build.0 = Development-Release|x64
915+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Publishing-Release|Win32.ActiveCfg = Publishing-Release|x64
916+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Publishing-Release|x64.ActiveCfg = Publishing-Release|x64
917+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Publishing-Release|x64.Build.0 = Publishing-Release|x64
918+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Test-Release|Win32.ActiveCfg = Test-Release|x64
919+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Test-Release|x64.ActiveCfg = Test-Release|x64
920+
{76FF7FB7-7635-4131-8DCB-48583740961D}.Test-Release|x64.Build.0 = Test-Release|x64
907921
{6A95DCDD-FD5F-4064-B12C-F6602E31FEEE}.Development-Debug|Win32.ActiveCfg = Development-Debug|x64
908922
{6A95DCDD-FD5F-4064-B12C-F6602E31FEEE}.Development-Debug|x64.ActiveCfg = Development-Debug|x64
909923
{6A95DCDD-FD5F-4064-B12C-F6602E31FEEE}.Development-Debug|x64.Build.0 = Development-Debug|x64
@@ -1111,6 +1125,7 @@ Global
11111125
{7D3E1C92-5A64-4B18-9F2A-1E8C4D6B0A37} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
11121126
{17FBB4FD-8E4E-4E61-AC32-BBEBA09B8D18} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
11131127
{935A2A28-2CE7-4F7A-96AE-964AEDEDC952} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
1128+
{76FF7FB7-7635-4131-8DCB-48583740961D} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
11141129
{6A95DCDD-FD5F-4064-B12C-F6602E31FEEE} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
11151130
{8F4A1D26-93BC-4E07-A5D1-64C2E7B9F310} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
11161131
{7C3E9A18-4D62-4B85-9E30-1A8F5D2C6B44} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}

‎Source/Games/Need for Speed 2016/Need for Speed 2016.vcxproj‎

Lines changed: 337 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="main.cpp" />
5+
<ClCompile Include="..\..\Core\unity_build.cpp" />
6+
</ItemGroup>
7+
<ItemGroup>
8+
<ClInclude Include="about.h" />
9+
<ClInclude Include="camera.h" />
10+
<ClInclude Include="camera_capture.h" />
11+
<ClInclude Include="diagnostics.h" />
12+
<ClInclude Include="engine_jitter.h" />
13+
<ClInclude Include="frostbite_settings.h" />
14+
<ClInclude Include="log.h" />
15+
<ClInclude Include="memory_scan.h" />
16+
<ClInclude Include="render_scale.h" />
17+
<ClInclude Include="super_resolution_pass.h" />
18+
<ClInclude Include="taa_resolve.h" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
// Luma's "About" tab
4+
inline void DrawAbout()
5+
{
6+
ImGui::PushTextWrapPos(0.f);
7+
ImGui::Text(
8+
"Luma for \"Need for Speed (2016)\" is developed by PetarBadzhakov and is open source and free.\n"
9+
"It replaces the game's TAA with super resolution anti-aliasing: DLSS, FSR 4, XeSS or FSR 3.\n"
10+
"Set Anti-Aliasing in the game's video settings to TAA. Luma's \"Auto\" Super Resolution picks the best one your GPU supports.\n"
11+
"The Resolution Scale setting renders the game above the screen's resolution (supersampling), for GPUs with performance to spare.\n"
12+
"Thanks to the Luma team and contributors, and to DristoforColumb's Mass Effect: Andromeda Luma mod, which this is based on.");
13+
ImGui::PopTextWrapPos();
14+
15+
ImGui::NewLine();
16+
static const std::string social_link = std::string("Join our \"HDR Den\" Discord ") + std::string(ICON_FK_SEARCH);
17+
if (ImGui::Button(social_link.c_str()))
18+
{
19+
// Unique link for Luma's HDR Den (tracks the origin of people joining); do not share for other purposes.
20+
static const std::string discord_link = std::string("https://discord.gg/J9fM") + std::string("3EVuEZ");
21+
ShellExecuteA(nullptr, "open", discord_link.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
22+
}
23+
static const std::string contributing_link = std::string("Contribute on Github ") + std::string(ICON_FK_FILE_CODE);
24+
if (ImGui::Button(contributing_link.c_str()))
25+
ShellExecuteA(nullptr, "open", "https://github.com/Filoppi/Luma-Framework", nullptr, nullptr, SW_SHOWNORMAL);
26+
27+
ImGui::NewLine();
28+
ImGui::Text("Build Date: %s %s", __DATE__, __TIME__);
29+
30+
ImGui::NewLine();
31+
ImGui::Text("Credits:"
32+
"\n\nMain:"
33+
"\nPetarBadzhakov"
34+
"\n\nBased on:"
35+
"\nMass Effect: Andromeda Luma mod (DristoforColumb)"
36+
"\n\nThird Party:"
37+
"\nImGui"
38+
"\nSafetyHook"
39+
"\nAMD FidelityFX (FSR 3, FSR 4)"
40+
"\nIntel XeSS"
41+
"\nNVIDIA NGX (DLSS)",
42+
"");
43+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#pragma once
2+
3+
#include <cmath>
4+
#include <cstdint>
5+
#include <type_traits>
6+
7+
struct RenderSize
8+
{
9+
uint32_t width = 0;
10+
uint32_t height = 0;
11+
12+
bool operator==(const RenderSize& other) const
13+
{
14+
return width == other.width && height == other.height;
15+
}
16+
};
17+
18+
// A jitter offset in pixels, y down (SR's convention)
19+
struct Jitter
20+
{
21+
float x;
22+
float y;
23+
};
24+
25+
// Frostbite's "viewConstants" constant buffer, up to the projection matrix (the same layout as in Mass Effect Andromeda). It's read
26+
// straight from the game's constant buffer memory, so it stays plain data.
27+
struct Camera
28+
{
29+
float time[4];
30+
float screen_size[4]; // Width, height, 1 / width, 1 / height
31+
float view[4][4];
32+
float projection[4][4]; // Column vectors (w = -z), reverse-Z with an infinite far plane, plus the jitter
33+
34+
// Whether it's the main camera's: other views share the layout (shadows, reflections, half resolution effects)
35+
bool IsMain(RenderSize render_size) const;
36+
37+
// The projection adds the jitter as projection[0][2] and [1][2], so the NDC offset is their negation (w = -z)
38+
Jitter GetJitter(RenderSize render_size) const
39+
{
40+
return {-projection[0][2] * 0.5f * float(render_size.width), projection[1][2] * 0.5f * float(render_size.height)};
41+
}
42+
43+
// With an infinite reverse-Z projection, the depth row's w term is the near plane
44+
float GetNearPlane() const
45+
{
46+
return projection[2][3];
47+
}
48+
49+
// In radians, from the projection's y scale (1 / tan(fov / 2))
50+
float GetVerticalFov() const
51+
{
52+
return 2.f * atanf(1.f / projection[1][1]);
53+
}
54+
};
55+
static_assert(std::is_trivially_copyable_v<Camera> && std::is_standard_layout_v<Camera> && sizeof(Camera) == 10 * 4 * sizeof(float));
56+
57+
// Told apart by its screen size and the shape of its matrices. The screen size is checked first: it rejects most other views.
58+
inline bool Camera::IsMain(RenderSize render_size) const
59+
{
60+
auto nearly_equal = [](float a, float b, float tolerance)
61+
{ return fabsf(a - b) <= tolerance; };
62+
63+
if (!nearly_equal(screen_size[0], float(render_size.width), 1.f) || !nearly_equal(screen_size[1], float(render_size.height), 1.f))
64+
return false;
65+
66+
// An affine view matrix
67+
const float* view_row = view[3];
68+
if (!nearly_equal(view_row[0], 0.f, 1e-3f) || !nearly_equal(view_row[1], 0.f, 1e-3f) || !nearly_equal(view_row[2], 0.f, 1e-3f) || !nearly_equal(view_row[3], 1.f, 1e-3f))
69+
return false;
70+
71+
// A reverse-Z perspective projection
72+
const auto& p = projection;
73+
return p[0][0] > 0.f && p[1][1] > 0.f && p[2][3] > 0.f && nearly_equal(p[3][2], -1.f, 1e-2f) && nearly_equal(p[3][3], 0.f, 1e-3f);
74+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
5+
#include "camera.h"
6+
#include "log.h"
7+
8+
// Captures the main camera when the game binds it for a draw, straight from the CPU memory the game uploaded it to. Its constant buffer
9+
// is renamed (discarded) mid frame, so by the resolve the camera can be gone, and some views draw nothing that binds it after the scene.
10+
class CameraCapture
11+
{
12+
public:
13+
// A constant buffer binding: the buffer, and where its constants start in it
14+
struct Binding
15+
{
16+
const ID3D11Resource* buffer = nullptr;
17+
uint64_t offset = 0; // In bytes
18+
};
19+
20+
// The large dynamic constant buffers the game uploads its per frame constants to
21+
static bool MayHoldCamera(const D3D11_BUFFER_DESC& desc);
22+
// The camera's constant buffer, if this vertex shader binding call sets it (no buffer otherwise)
23+
static Binding FindBinding(UINT start, UINT count, ID3D11Buffer* const* buffers, const UINT* first_constant);
24+
25+
// From any thread
26+
void OnBufferMapped(const ID3D11Resource* buffer, const void* data, uint64_t size);
27+
void OnBufferDestroyed(const ID3D11Resource* buffer);
28+
// From the immediate context, so the game's render thread, where the camera is also used (the resolve and present)
29+
void OnCameraBound(Binding binding, RenderSize render_size);
30+
31+
// The main camera, as last bound this frame (null if none was)
32+
const Camera* GetCamera() const
33+
{
34+
if (!has_camera)
35+
return nullptr;
36+
return &camera;
37+
}
38+
void EndFrame();
39+
40+
private:
41+
struct MappedBuffer
42+
{
43+
const ID3D11Resource* buffer;
44+
const uint8_t* data;
45+
uint64_t size;
46+
};
47+
static constexpr UINT kCameraSlot = 2; // In the scene's vertex shaders
48+
// "first_constant" counts float4 constants
49+
static constexpr UINT kConstantSize = 16;
50+
static constexpr size_t kMaxMappedBuffers = 64;
51+
52+
// The caller holds the mutex
53+
MappedBuffer* FindMappedBuffer(const ID3D11Resource* buffer);
54+
55+
// The mutex guards the mapped buffers. The camera is only used on the render thread.
56+
std::mutex mutex;
57+
MappedBuffer mapped_buffers[kMaxMappedBuffers] = {};
58+
size_t mapped_count = 0;
59+
bool logged_overflow = false;
60+
Camera camera = {};
61+
bool has_camera = false;
62+
};
63+
64+
// The game suballocates its per frame constants from a few big dynamic buffers (64 KB and up), so small ones are skipped before any lock
65+
inline bool CameraCapture::MayHoldCamera(const D3D11_BUFFER_DESC& desc)
66+
{
67+
return (desc.BindFlags & D3D11_BIND_CONSTANT_BUFFER) && desc.Usage == D3D11_USAGE_DYNAMIC && desc.ByteWidth >= 65536;
68+
}
69+
70+
// Runs for every vertex shader constant buffer binding the game makes, so integer checks only. A call can cover a range of slots, and
71+
// "VSSetConstantBuffers1" binds a range of a buffer.
72+
inline CameraCapture::Binding CameraCapture::FindBinding(UINT start, UINT count, ID3D11Buffer* const* buffers, const UINT* first_constant)
73+
{
74+
if (!buffers || kCameraSlot < start || kCameraSlot >= start + count)
75+
return {};
76+
const UINT index = kCameraSlot - start;
77+
Binding binding = {.buffer = buffers[index]};
78+
if (first_constant)
79+
binding.offset = uint64_t(first_constant[index]) * kConstantSize;
80+
return binding;
81+
}
82+
83+
// The latest map wins: a discard returns the buffer's new memory, which the later uploads go to
84+
inline void CameraCapture::OnBufferMapped(const ID3D11Resource* buffer, const void* data, uint64_t size)
85+
{
86+
const MappedBuffer map = {buffer, static_cast<const uint8_t*>(data), size};
87+
const std::lock_guard lock(mutex);
88+
if (MappedBuffer* known = FindMappedBuffer(buffer))
89+
{
90+
*known = map;
91+
}
92+
else if (mapped_count < kMaxMappedBuffers)
93+
{
94+
mapped_buffers[mapped_count++] = map;
95+
}
96+
else if (!logged_overflow)
97+
{
98+
logged_overflow = true;
99+
LogDiagnostic(LogLevel::Warning, "over {} large constant buffers were mapped in a frame, so the camera can be missed", kMaxMappedBuffers);
100+
}
101+
}
102+
103+
// A destroyed buffer's address can be reused by a new one
104+
inline void CameraCapture::OnBufferDestroyed(const ID3D11Resource* buffer)
105+
{
106+
const std::lock_guard lock(mutex);
107+
if (MappedBuffer* known = FindMappedBuffer(buffer))
108+
*known = mapped_buffers[--mapped_count];
109+
}
110+
111+
// Keeps the bound block if it's the main camera, read from where its buffer was last mapped (the game wrote it there before binding it)
112+
inline void CameraCapture::OnCameraBound(Binding binding, RenderSize render_size)
113+
{
114+
const std::lock_guard lock(mutex);
115+
const MappedBuffer* map = FindMappedBuffer(binding.buffer);
116+
// The binding's offset is into the buffer, so the block has to be within what was mapped
117+
if (!map || binding.offset + sizeof(Camera) > map->size)
118+
return;
119+
const auto& bound = *reinterpret_cast<const Camera*>(map->data + binding.offset);
120+
if (!bound.IsMain(render_size))
121+
return;
122+
camera = bound;
123+
has_camera = true;
124+
}
125+
126+
// At present: the mapped memory is only valid until the buffers are mapped again, and each frame binds its own camera
127+
inline void CameraCapture::EndFrame()
128+
{
129+
const std::lock_guard lock(mutex);
130+
mapped_count = 0;
131+
has_camera = false;
132+
}
133+
134+
// A linear search: only a handful of large buffers are mapped in a frame
135+
inline CameraCapture::MappedBuffer* CameraCapture::FindMappedBuffer(const ID3D11Resource* buffer)
136+
{
137+
for (size_t i = 0; i < mapped_count; i++)
138+
if (mapped_buffers[i].buffer == buffer)
139+
return &mapped_buffers[i];
140+
return nullptr;
141+
}

0 commit comments

Comments
 (0)