Files
Triforce-Source/gpu/vk/fsr3_math.h
ruckus7382sfgsgdf 6c7117635c Triforce v0.6.0 "Sage Of Power"
Faster frames (the replay's memory work, held submits, helper threads, collection and capture cuts), the game letting old areas go (QueryMemoryInfo), the 0.5.2 stutter fixed, AMD RX 5000/6000 and integrated-GPU fixes, the GPU device chosen by name, the in-game menus and launcher reworked, controller, Linux and benchmark fixes.
2026-11-20 17:06:21 -06:00

100 lines
6.0 KiB
C++

// (hookshot) AMD FSR 3.1's camera, from the scene's own projection: plain functions, no Vulkan. tests/test_fsr3_math.cpp
// checks them.
//
// FSR 3.1's passes (the upscaler's and frame interpolation's) turn a depth into a view distance with four factors,
// device_to_view_depth (the SDK's setupDeviceDepthToViewSpaceDepthParams), and frame interpolation scales its disocclusion
// test by the tangent of half the field of view. The SDK builds them from a near plane, a far plane and a field of view; the
// projection the scene was drawn with has all three in it, so they are read from there instead (the camera's uniform buffer,
// gpu/vk/vk_fsr3_inputs.cpp: the same buffer NVIDIA DLSS reads its camera from), and no hook into the game is needed.
#pragma once
#include <cfloat>
#include <cmath>
namespace mkwvk::fsr3_math {
struct Camera {
/// view distance = [1] / (device depth - [0]); view x = [2] * ndc x * distance, y = [3] * ndc y * distance. For the
/// depth FSR is given, which is reversed (1 at the near plane: the passes are compiled with INVERTED_DEPTH)
float device_to_view_depth[4]{};
float tan_half_fov_x = 0.0f; ///< frame interpolation's fTanHalfFOV (the SDK's: horizontal, from the render aspect)
float fov_y = 0.0f; ///< radians
float near_plane = 0.0f, far_plane = 0.0f; ///< far: FLT_MAX for an infinite projection
/// The game's own depth runs the other way (0 at the near plane): FSR's copy is 1 - it (vk_fsr3_inputs.cpp)
bool flip_depth = false;
};
/// The camera of a perspective projection p (row major, clip = p * view position, its w row 0 0 -1 0, as the game fills its
/// views' uniform buffers), whose clip depth is NVN's -1 to 1, so the depth buffer holds (ndc z + 1) / 2 (shaders/
/// dlss_prep.comp reads it the same way). With a view distance v (> 0 in front of the camera), ndc z = -p22 + p23 / v, so the
/// stored depth is d = a + b / v with a = (1 - p22) / 2 and b = p23 / 2: reversed when b > 0. False for a projection that is
/// not one (no perspective, no depth range, a degenerate field of view)
inline bool camera_from_projection(const float p[16], Camera& c) {
c = Camera{};
if (p[12] != 0.0f || p[13] != 0.0f || p[14] != -1.0f || p[15] != 0.0f) return false;
if (!(p[0] > 0.0f) || !(p[5] > 0.0f) || !std::isfinite(p[0]) || !std::isfinite(p[5])) return false;
double a = (1.0 - (double)p[10]) * 0.5, b = (double)p[11] * 0.5;
if (!std::isfinite(a) || !std::isfinite(b) || std::fabs(b) < 1e-12) return false;
c.flip_depth = b < 0.0;
if (c.flip_depth) a = 1.0 - a, b = -b; // (FSR's copy: 1 - d = (1 - a) - b / v)
// (an infinite projection has a = 0, which puts the far plane at device depth 0 exactly: the SDK's own factor there is
// -FLT_EPSILON, so distances stay finite and positive)
if (a > -FLT_EPSILON) a = -FLT_EPSILON;
c.device_to_view_depth[0] = (float)a;
c.device_to_view_depth[1] = (float)b;
c.device_to_view_depth[2] = 1.0f / p[0]; // (the SDK's 1 / (cot(fov / 2) / aspect))
c.device_to_view_depth[3] = 1.0f / p[5]; // (1 / cot(fov / 2))
c.tan_half_fov_x = 1.0f / p[0];
c.fov_y = 2.0f * std::atan(1.0f / p[5]);
c.near_plane = (float)(b / (1.0 - a));
c.far_plane = a <= -1e-6 ? (float)(-b / a) : FLT_MAX;
return c.near_plane > 0.0f && std::isfinite(c.near_plane);
}
/// FSR's view distance for a device depth (as its shaders compute it: GetViewSpaceDepth), for the test
inline float view_distance(const Camera& c, float device_depth) {
return c.device_to_view_depth[1] / (device_depth - c.device_to_view_depth[0]);
}
/// The jitter sequence's length for FSR 3.1, AMD's own rule (ffxFsr3GetJitterPhaseCount, ffx_upscale.h): 8 times the
/// width's scale squared, truncated, with no upper bound: 18 at Quality (1.5x), 23 at Balanced, 32 at Performance, 71 or 72
/// at Ultra Performance (3x), 8 for the Native AA. AMD FSR 4 on the frame chain takes it too ((Triforce, FSR4-10)
/// vk_temporal.cpp chain); NVIDIA's rule (dlss_math.h dlss_jitter_phases, by area, rounded, 64 at most) stays DLSS's
inline unsigned jitter_phases(unsigned render_width, unsigned display_width) {
if (!render_width || !display_width) return 8;
const float ratio = (float)display_width / (float)render_width;
const int n = (int)(8.0f * ratio * ratio);
return n < 1 ? 1u : (unsigned)n;
}
/// The size FSR 3.1's render-size images are made at (the SDK's maxRenderSize), and whether they are made again, for a frame
/// of rw x rh into uw x uh with images of have_w x have_h for an output of have_uw x have_uh (0 x 0: none yet). They are kept
/// for any frame that fits them, so the game's dynamic resolution (the Render resolution row's Auto) changes only the
/// render size each dispatch is given and the history goes on, as the SDK's FFX_FSR3UPSCALER_ENABLE_DYNAMIC_RESOLUTION
/// does: its passes clamp what they read to the frame's render size (ClampUv, ClampLoad). Made again only for another
/// output, or for a frame larger than them: at the output's size then (a size that grew is the dynamic resolution's, and
/// the images are not made again for its next step), and at once with dynamic (the row's Auto). A fixed share is made at
/// its own size. Never smaller than the frame, nor larger than the output unless the frame is
struct ImagePlan {
unsigned width = 0, height = 0;
bool remake = false;
};
inline ImagePlan plan_images(unsigned have_w, unsigned have_h, unsigned have_uw, unsigned have_uh, unsigned rw, unsigned rh,
unsigned uw, unsigned uh, bool dynamic) {
ImagePlan p;
rw = rw ? rw : 1, rh = rh ? rh : 1, uw = uw ? uw : 1, uh = uh ? uh : 1;
const bool same_output = have_w && have_h && have_uw == uw && have_uh == uh;
if (same_output && rw <= have_w && rh <= have_h) {
p.width = have_w, p.height = have_h;
return p;
}
// (a larger frame for the same output: the dynamic resolution's step up)
const bool full = dynamic || same_output;
p.width = full && uw > rw ? uw : rw;
p.height = full && uh > rh ? uh : rh;
p.remake = true;
return p;
}
} // namespace mkwvk::fsr3_math