Skip to content

Commit 57a3d88

Browse files
PetarBadzhakovclaude
authored andcommitted
Need for Speed 2016: Add the Luma mod from upstream
Needs Anti-Aliasing set to TAA in the game's video settings. - The mod exactly as in Filoppi#277 (its head, 487ce78), so the next commit shows only the native port. Built on DristoforColumb's Mass Effect Andromeda mod; FSR 4 on DX11 uses izueh's interop. - It still uses ReShade's API, so it only builds with the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent efd1ebb commit 57a3d88

15 files changed

Lines changed: 2167 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+
"\nReShade"
38+
"\nImGui"
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: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
// The large dynamic constant buffers the game uploads its per frame constants to
14+
static bool MayHoldCamera(const D3D11_BUFFER_DESC& desc);
15+
// The camera's constant buffer, if this binding update has it (null otherwise)
16+
static const reshade::api::buffer_range* FindBinding(reshade::api::shader_stage stages, const reshade::api::descriptor_table_update& update);
17+
18+
// From any thread
19+
void OnBufferMapped(uint64_t buffer, const void* data, uint64_t size);
20+
void OnBufferDestroyed(uint64_t buffer);
21+
// From the immediate context, so the game's render thread, where the camera is also used (the resolve and present)
22+
void OnCameraBound(const reshade::api::buffer_range& binding, RenderSize render_size);
23+
24+
// The main camera, as last bound this frame (null if none was)
25+
const Camera* GetCamera() const
26+
{
27+
if (!has_camera)
28+
return nullptr;
29+
return &camera;
30+
}
31+
void EndFrame();
32+
33+
private:
34+
struct MappedBuffer
35+
{
36+
uint64_t handle;
37+
const uint8_t* data;
38+
uint64_t size;
39+
};
40+
static constexpr uint32_t kCameraSlot = 2; // In the scene's vertex shaders
41+
static constexpr size_t kMaxMappedBuffers = 64;
42+
43+
// The caller holds the mutex
44+
MappedBuffer* FindMappedBuffer(uint64_t handle);
45+
46+
// The mutex guards the mapped buffers. The camera is only used on the render thread.
47+
std::mutex mutex;
48+
MappedBuffer mapped_buffers[kMaxMappedBuffers] = {};
49+
size_t mapped_count = 0;
50+
bool logged_overflow = false;
51+
Camera camera = {};
52+
bool has_camera = false;
53+
};
54+
55+
// 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
56+
inline bool CameraCapture::MayHoldCamera(const D3D11_BUFFER_DESC& desc)
57+
{
58+
return (desc.BindFlags & D3D11_BIND_CONSTANT_BUFFER) && desc.Usage == D3D11_USAGE_DYNAMIC && desc.ByteWidth >= 65536;
59+
}
60+
61+
// Runs for every binding the game makes, so integer checks only. An update can cover a range of slots.
62+
inline const reshade::api::buffer_range* CameraCapture::FindBinding(reshade::api::shader_stage stages, const reshade::api::descriptor_table_update& update)
63+
{
64+
if (update.type != reshade::api::descriptor_type::constant_buffer || !(uint32_t(stages) & uint32_t(reshade::api::shader_stage::vertex)))
65+
return nullptr;
66+
if (kCameraSlot < update.binding || kCameraSlot >= update.binding + update.count)
67+
return nullptr;
68+
return &static_cast<const reshade::api::buffer_range*>(update.descriptors)[kCameraSlot - update.binding];
69+
}
70+
71+
// The latest map wins: a discard returns the buffer's new memory, which the later uploads go to
72+
inline void CameraCapture::OnBufferMapped(uint64_t buffer, const void* data, uint64_t size)
73+
{
74+
const MappedBuffer map = {buffer, static_cast<const uint8_t*>(data), size};
75+
const std::lock_guard lock(mutex);
76+
if (MappedBuffer* known = FindMappedBuffer(buffer))
77+
{
78+
*known = map;
79+
}
80+
else if (mapped_count < kMaxMappedBuffers)
81+
{
82+
mapped_buffers[mapped_count++] = map;
83+
}
84+
else if (!logged_overflow)
85+
{
86+
logged_overflow = true;
87+
LogDiagnostic(reshade::log::level::warning, "over {} large constant buffers were mapped in a frame, so the camera can be missed", kMaxMappedBuffers);
88+
}
89+
}
90+
91+
// A destroyed buffer's handle can be reused
92+
inline void CameraCapture::OnBufferDestroyed(uint64_t buffer)
93+
{
94+
const std::lock_guard lock(mutex);
95+
if (MappedBuffer* known = FindMappedBuffer(buffer))
96+
*known = mapped_buffers[--mapped_count];
97+
}
98+
99+
// 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)
100+
inline void CameraCapture::OnCameraBound(const reshade::api::buffer_range& binding, RenderSize render_size)
101+
{
102+
const std::lock_guard lock(mutex);
103+
const MappedBuffer* map = FindMappedBuffer(binding.buffer.handle);
104+
// The binding's offset is into the buffer, so the block has to be within what was mapped
105+
if (!map || binding.offset + sizeof(Camera) > map->size)
106+
return;
107+
const auto& bound = *reinterpret_cast<const Camera*>(map->data + binding.offset);
108+
if (!bound.IsMain(render_size))
109+
return;
110+
camera = bound;
111+
has_camera = true;
112+
}
113+
114+
// At present: the mapped memory is only valid until the buffers are mapped again, and each frame binds its own camera
115+
inline void CameraCapture::EndFrame()
116+
{
117+
const std::lock_guard lock(mutex);
118+
mapped_count = 0;
119+
has_camera = false;
120+
}
121+
122+
// A linear search: only a handful of large buffers are mapped in a frame
123+
inline CameraCapture::MappedBuffer* CameraCapture::FindMappedBuffer(uint64_t handle)
124+
{
125+
for (size_t i = 0; i < mapped_count; i++)
126+
if (mapped_buffers[i].handle == handle)
127+
return &mapped_buffers[i];
128+
return nullptr;
129+
}

0 commit comments

Comments
 (0)