Files
Triforce-Source/gpu/vk/dlss_actor.h
ruckus7382sfgsgdf 822ee913bc Triforce v0.9.0
Character tab, faster renderer defaults, HD rumble shipped, and the 11-30 bug pass: ending softlock below 60 fps, adult first-night lock, Intel Arc robust buffers, adult map, randomizer load order.
2026-11-30 20:55:14 -06:00

412 lines
23 KiB
C++

// (hookshot) The temporal upscaler's own motion (vk_temporal.cpp): what is worked out on the CPU for a draw drawn again, from
// its bones (actor_motion) or from the positions its vertex stage stored (capture_motion), kept apart so
// tests/test_dlss_actor.cpp checks it without a GPU. Header only, no Vulkan.
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <vector>
namespace dlss_actor {
/// A draw's name across frames: its model (vertex and index buffers, count, base vertex, program) and which of the frame's
/// draws of that model it is (the same model drawn twice: the n-th draw of it is matched with the n-th the frame before)
inline uint64_t key(uint64_t vbuf, uint64_t ibuf, int count, int base_vertex, uint64_t program, int nth) {
uint64_t h = 0x5bd1e995;
for (uint64_t v : {vbuf, ibuf, (uint64_t)(uint32_t)count, (uint64_t)(uint32_t)base_vertex, program}) {
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
h *= 0xff51afd7ed558ccdull;
}
return (h & ~0xffull) | (uint64_t)std::clamp(nth, 0, 255);
}
inline bool same_model(uint64_t a, uint64_t b) { return a >> 8 == b >> 8; }
/// (Triforce) A copy of a model, for pair_by_place: where it is (place_of) and which draw of the model it is (its n-th)
struct Placed {
float place[3];
int nth;
};
/// (Triforce) Which of the frame before's copies of a model each of this frame's copies is, by place: for each of `now`, an
/// index into `then`, or -1 (none: the camera's motion then, never another copy's). All copies are paired at once, the
/// nearest pairs first, and never further apart than `reach` (in world units, moved between the two frames). On a tie (the
/// same geometry drawn twice for one object, both at one place) the same n-th draw pairs first. The pairing by draw order
/// (each copy with the frame before's same n-th) is kept when it is as good: every pair of it within the reach, and either
/// more copies paired than nearest first, or as many with the distance moved over all of them no longer (two copies 0.9
/// apart both moving 0.5 pair one by nearest first, at 0.4, and two by draw order, at 0.5 each, which is right). So the
/// reach is all that keeps draw order from pairing a row of copies each with its neighbour: it must stay under the copies'
/// spacing (2 at the most: the Gossip Stones stand 2.3 apart).
/// By draw order alone, the four Gossip Stones (one skinned model drawn four times, in another order from frame to frame,
/// and by two models) took each other's places as their own frame before, and frame generation drew a stone part way to the
/// next one. Nearest first alone fails the other way: two copies 0.6 apart that both move 0.35 have the first one's new
/// place nearest the second's old one
inline std::vector<int> pair_by_place(const std::vector<Placed>& now, const std::vector<Placed>& then, float reach) {
struct Candidate {
double d; // (squared)
bool other; // (not the same n-th: on a tie at one place, the same n-th pairs first)
int i, j;
};
const auto distance2 = [&](int i, int j) {
double d = 0.0;
for (int c = 0; c < 3; c++) {
const double e = (double)now[i].place[c] - (double)then[j].place[c];
d += e * e;
}
return d;
};
std::vector<Candidate> candidates;
const double r2 = (double)reach * (double)reach;
for (int i = 0; i < (int)now.size(); i++)
for (int j = 0; j < (int)then.size(); j++) {
const double d = distance2(i, j);
if (std::isfinite(d) && d <= r2) candidates.push_back({d, now[i].nth != then[j].nth, i, j});
}
std::sort(candidates.begin(), candidates.end(), [](const Candidate& a, const Candidate& b) {
return a.d != b.d ? a.d < b.d : a.other != b.other ? !a.other : a.i != b.i ? a.i < b.i : a.j < b.j;
});
std::vector<int> pairs(now.size(), -1);
std::vector<char> taken(then.size(), 0);
int nearest_n = 0;
double nearest_total = 0.0;
for (const Candidate& c : candidates)
if (pairs[c.i] < 0 && !taken[c.j])
pairs[c.i] = c.j, taken[c.j] = 1, nearest_n++, nearest_total += std::sqrt(c.d);
// (by draw order: kept when it is no worse, as above. A copy without a same n-th then is left unpaired by it)
std::vector<int> by_order(now.size(), -1);
std::vector<char> used(then.size(), 0);
int order_n = 0;
double order_total = 0.0;
for (int i = 0; i < (int)now.size(); i++) {
int j = 0;
while (j < (int)then.size() && then[j].nth != now[i].nth) j++;
if (j == (int)then.size()) continue;
const double d = std::sqrt(distance2(i, j));
if (used[j] || !(d <= (double)reach)) return pairs; // (not within the reach: nearest first; NaN fails here too)
by_order[i] = j, used[j] = 1, order_n++, order_total += d;
}
if (order_n > nearest_n || (order_n == nearest_n && order_total <= nearest_total + 1e-6)) return by_order;
return pairs;
}
/// (Triforce) The most copies of one model paired by place: pair_by_place and ambiguous_by_place cost copies x copies a model
/// (1000 copies took 11 ms a frame). A model with more has all its copies get the camera's motion (unsure_copies), as before
/// copies were paired by place. 256: the key's n-th stops at 255 (key())
constexpr size_t kCopyCap = 256;
/// (Triforce) How near another copy has to be to leave a pair in doubt, in ambiguous_by_place: within kTieFactor times the
/// distance the copy itself moved since the frame before, plus kTieSlack. Both in world units, as a copy's own move since the
/// frame before is, so it is not tuned to a screen (a copy that moved far among static ones is sure; one that moved little
/// beside another about as near is not)
constexpr double kTieFactor = 2.0, kTieSlack = 0.05;
/// (Triforce) Which of this frame's copies, paired by place (pairs: pair_by_place's), have no sure pair: another copy of the
/// frame before is about as near (within kTieFactor times the distance moved, plus kTieSlack), or another copy of this frame is about as near
/// that same copy of the frame before. A copy that moved on its own among static ones is sure (its own old place is nearest by
/// far); copies at one place (the origin of every copy of a piece of ground, or of a model whose place is not what was read)
/// are never sure. Those take the camera's motion. Copies without a pair are not flagged (they have none: the camera's)
inline std::vector<char> ambiguous_by_place(const std::vector<Placed>& now, const std::vector<Placed>& then,
const std::vector<int>& pairs) {
std::vector<char> out(now.size(), 0);
const auto distance = [&](const Placed& a, const Placed& b) {
double d = 0.0;
for (int c = 0; c < 3; c++) {
const double e = (double)a.place[c] - (double)b.place[c];
d += e * e;
}
return std::sqrt(d);
};
for (size_t i = 0; i < now.size() && i < pairs.size(); i++) {
const int j = pairs[i];
if (j < 0 || j >= (int)then.size()) continue;
const double limit = kTieFactor * distance(now[i], then[j]) + kTieSlack;
for (size_t k = 0; k < then.size() && !out[i]; k++)
if ((int)k != j && distance(now[i], then[k]) <= limit) out[i] = 1;
for (size_t m = 0; m < now.size() && !out[i]; m++)
if (m != i && distance(now[m], then[j]) <= limit) out[i] = 1;
}
return out;
}
/// (Triforce) For RX_VK_DLSS_TRACE: how a model's copies are spread: how many differ from all before them (by over 0.001), and the
/// least (over 0.001) and greatest distance between two of them. Tells whether what was read as a place is one
struct Spread {
int distinct = 0;
float min_gap = 0.0f, max_gap = 0.0f;
};
inline Spread origin_spread(const std::vector<Placed>& copies) {
Spread s;
double lo = 0.0, hi = 0.0;
for (size_t i = 0; i < copies.size(); i++) {
bool fresh = true;
for (size_t j = 0; j < i; j++) {
double d = 0.0;
for (int c = 0; c < 3; c++) {
const double e = (double)copies[i].place[c] - (double)copies[j].place[c];
d += e * e;
}
d = std::sqrt(d);
if (!std::isfinite(d)) continue;
if (d > 0.001) {
lo = lo > 0.0 ? std::min(lo, d) : d;
hi = std::max(hi, d);
} else fresh = false;
}
s.distinct += fresh;
}
s.min_gap = (float)lo, s.max_gap = (float)hi;
return s;
}
/// (Triforce) Which of this frame's scene draws have no sure draw of their own the frame before: a copy without a place (not
/// skinned, so pair_by_place can't tell it apart) of a model drawn more than once this frame, or the frame before (had: the
/// frame before drew that key; asked for the model's second draw, n-th 1). Its n-th draw the frame before is then only a
/// guess: the game draws a model's copies (trees, rocks, pieces of ground) in another order as the camera turns, and a copy
/// paired by n-th takes its neighbour's positions as its own frame before. Frame generation then draws it part way to the
/// neighbour (a tree's afterimage beside another tree, ground smeared along outlines). Those get the camera's motion
/// instead, as the draws with no frame before do. keys: this frame's scene draws (key()); placed[i]: it has a place
template <class Had>
std::vector<char> unsure_copies(const std::vector<uint64_t>& keys, const std::vector<char>& placed, Had&& had) {
std::vector<char> out(keys.size(), 0);
std::vector<uint64_t> models(keys.size());
for (size_t i = 0; i < keys.size(); i++) models[i] = keys[i] >> 8;
std::vector<uint64_t> sorted = models;
std::sort(sorted.begin(), sorted.end());
for (size_t i = 0; i < keys.size(); i++) {
if (placed[i]) continue;
const auto r = std::equal_range(sorted.begin(), sorted.end(), models[i]);
out[i] = r.second - r.first > 1 || had((keys[i] & ~0xffull) | 1);
}
return out;
}
/// (Triforce) Limits for object_place, from the 2026-11-28 in-game diagnostic (62 models: every object buffer's 3x3 was
/// rotation-scale, its axes well over kAxisMin long, pairwise |cos| near 0): a translation beyond kPlaceMax, or a 3x3 whose
/// rows and whose columns are both short or far from orthogonal, is not a world matrix
constexpr float kPlaceMax = 1e5f, kAxisMin = 1e-4f, kAxisCosMax = 0.05f;
/// (Triforce) True if the three vectors of the 3x3 of m (a 3x4 row-major matrix) taken along rows (step 4, next 1) or columns
/// (step 1, next 4) are all over kAxisMin long and pairwise |cos| under kAxisCosMax
inline bool orthogonal_axes(const float m[12], int step, int next) {
double len[3];
for (int r = 0; r < 3; r++) {
double d = 0.0;
for (int c = 0; c < 3; c++) d += (double)m[r * step + c * next] * m[r * step + c * next];
len[r] = std::sqrt(d);
if (!(len[r] > kAxisMin)) return false;
}
for (int r = 0; r < 3; r++) {
const int q = (r + 1) % 3;
double dot = 0.0;
for (int c = 0; c < 3; c++) dot += (double)m[r * step + c * next] * m[q * step + c * next];
if (!(std::fabs(dot) / (len[r] * len[q]) < kAxisCosMax)) return false;
}
return true;
}
/// (Triforce) The place a non-skinned draw's object buffer holds: m, its first 12 floats, a 3x4 row-major world matrix (rows at
/// +0x00, +0x10, +0x20 of 4 floats, the translation last in each: m[3], m[7], m[11]; the floats at +0x40 are not a place).
/// Taken only if all 12 are finite (infinities and NaN would otherwise pass the comparisons' edge cases), the translation is
/// under kPlaceMax, and the 3x3 is rotation-scale: its rows or its columns are orthogonal (R*diag(s) has orthogonal columns,
/// diag(s)*R orthogonal rows). Else false and out is untouched
inline bool object_place(const float m[12], float out[3]) {
for (int i = 0; i < 12; i++)
if (!std::isfinite(m[i])) return false;
const int t[3] = {3, 7, 11};
for (int a = 0; a < 3; a++)
if (!(std::fabs(m[t[a]]) < kPlaceMax)) return false;
if (!orthogonal_axes(m, 4, 1) && !orthogonal_axes(m, 1, 4)) return false;
for (int a = 0; a < 3; a++) out[a] = m[t[a]];
return true;
}
/// (Triforce) Which of this frame's scene draws without a skin are placed by their object's origin: those whose origin was read
/// (readable[i]) of a model with copies (drawn 2+ times this frame, or its 2nd the frame before: had(key of its 2nd)), and not
/// over kCopyCap of them, whose copies keep the camera's motion by unsure_copies instead
template <class Had>
std::vector<char> place_by_origin(const std::vector<uint64_t>& keys, const std::vector<char>& readable, Had&& had) {
std::vector<char> out(keys.size(), 0);
std::vector<uint64_t> sorted;
for (size_t i = 0; i < keys.size(); i++)
if (readable[i]) sorted.push_back(keys[i] >> 8);
std::sort(sorted.begin(), sorted.end());
for (size_t i = 0; i < keys.size(); i++) {
if (!readable[i]) continue;
const auto r = std::equal_range(sorted.begin(), sorted.end(), keys[i] >> 8);
const size_t n = (size_t)(r.second - r.first);
out[i] = n <= kCopyCap && (n > 1 || had((keys[i] & ~0xffull) | 1));
}
return out;
}
/// (Triforce) Which draws placed by origin (by_origin) had a second copy the frame before (had) that was not placed (placed:
/// its origin was not readable, so nothing to pair it by): their pair is a guess, flagged as unsure_copies flagged it
template <class Had, class Placed>
std::vector<char> lost_second_copy(const std::vector<uint64_t>& keys, const std::vector<char>& by_origin, Had&& had, Placed&& placed) {
std::vector<char> out(keys.size(), 0);
for (size_t i = 0; i < keys.size(); i++) {
const uint64_t second = (keys[i] & ~0xffull) | 1;
out[i] = by_origin[i] && had(second) && !placed(second);
}
return out;
}
/// (Triforce) Where a skinned draw is, to tell copies of one model apart: its origin (3 floats) plus the mean of its bones'
/// translations (nbones 3 x 4 row-major matrices: world = sum(w B p) + origin). A model placed by its origin differs there;
/// copies placed by their bones at one origin (the game's copies of one model share an origin, the Gossip Stones among them)
/// differ in the bones; the sum is the place either way. Bones with a translation that is not finite are left out
inline void place_of(const uint8_t* origin, const uint8_t* bones, uint32_t nbones, float place[3]) {
float o[3];
std::memcpy(o, origin, sizeof o);
double t[3] = {0.0, 0.0, 0.0};
uint32_t used = 0;
for (uint32_t b = 0; b < nbones; b++) {
float m[12];
std::memcpy(m, bones + (size_t)b * 48, sizeof m);
if (!std::isfinite(m[3]) || !std::isfinite(m[7]) || !std::isfinite(m[11])) continue;
t[0] += m[3], t[1] += m[7], t[2] += m[11];
used++;
}
for (int c = 0; c < 3; c++) place[c] = o[c] + (used ? (float)(t[c] / used) : 0.0f);
}
/// (Triforce) The reach of pair_by_place: speed (world units a second) times the time between the two frames, that time
/// taken as 1/60 s at the least and 1/30 s at the most (at the default 60 a second, 1 to 2 units: under the Gossip Stones'
/// 2.3 after any gap, and some ten times a running Link a frame)
inline float place_reach(float speed, double seconds) {
return speed * (float)std::clamp(std::isfinite(seconds) ? seconds : 1.0 / 60.0, 1.0 / 60.0, 1.0 / 30.0);
}
/// shaders/dlss_actor.vert's push constants (the fragment stage's the same)
struct Push {
uint64_t vertices; // (the vertex stream's device address)
uint64_t indices; // (the index buffer's, 4-aligned down: first says where the draw's indices start in it)
uint64_t origins[2]; // (the draw's origin, xyz: this frame's and the frame before's, copied into the pose buffers)
uint64_t bones[2]; // (its palettes: 3 x 4 row-major matrices, 12 floats a bone)
uint32_t nbones;
uint32_t stride; // (in words)
uint32_t count; // (the vertices in the stream: an index past them draws nothing)
uint32_t first; // (in indices, from the address)
uint32_t itype; // (0: 8-bit, 1: 16-bit, 2: 32-bit indices)
int32_t base_vertex;
uint32_t pos, sets; // (bytes in a vertex: the position; the joint and weight sets, 1 or 2)
uint32_t joints[2], weights[2];
uint32_t formats; // (kHalfPosition, kByteJoints << set)
uint32_t tolerance; // (unused: dlss_actor.frag reads the cameras' buffer's word 66)
};
static_assert(sizeof(Push) == 6 * 8 + 14 * 4 && sizeof(Push) <= 128, "dlss_actor.vert's push constants");
/// A draw's pose in a pose buffer: its origin (12 bytes, at 0), the view it was drawn with (3 x 4 row major, at 16), its
/// projection (4 x 4, at 64), then its bones (at kPoseHead); Push::origins and bones point there
constexpr uint32_t kPoseHead = 128;
/// Push::itype of a draw without indices (a plain vertex array): its vertex i is base_vertex + i
constexpr int kNoIndices = 3;
/// Where a draw's indices are for the shader: the address 4-aligned down, and its first index from there
inline void index_start(uint64_t address, int itype, uint64_t* aligned, uint32_t* first) {
*aligned = address & ~3ull;
*first = (uint32_t)((address & 3) >> (itype < 0 ? 0 : itype > 2 ? 2 : itype));
}
/// Push::formats: the position is RGBA16F (else RGB32F); a set's joints are 8-bit (else 16-bit); how many joints and weights
/// a set has (kCountShift + 2 * set: 0 four, 1 one, 2 two: RGBA, R or RG formats); kRigid: no weights, set 0's one joint has
/// all of the vertex (a part that moves with one bone). Joints and weights are read byte by byte, at any offset
constexpr uint32_t kHalfPosition = 1, kByteJoints = 2, kCountShift = 4, kRigid = 1u << 8;
/// The count code of a set of n joints and weights (1, 2 or 4), for Push::formats; 3 (none) for any other n
inline uint32_t count_code(int n) { return n == 4 ? 0u : n == 1 ? 1u : n == 2 ? 2u : 3u; }
/// How many joints and weights a set has, from Push::formats
inline int set_count(uint32_t formats, int set) {
const uint32_t c = (formats >> (kCountShift + 2 * set)) & 3u;
return c == 0 ? 4 : c == 1 ? 1 : c == 2 ? 2 : 0;
}
/// The cameras' buffer: this frame's camera (as drawn, jittered), the frame before's (its jitter taken off), this frame's
/// jitter in clip space, the render size (words 0 to 35); then this frame's view (3 x 4, row major) and projection (4 x 4),
/// apart, as the game's vertex shaders apply them (36 to 63); then its near and far planes (0: not a plain perspective
/// projection), for the fragment stage's test in distance (64, 65); the tolerance of that test in thousandths of the
/// distance, kMarkBit for RX_VK_DLSS_DEBUG=actors and kOwnBit with it for =characters (66); the frame before's jitter in clip space (68, 69), which
/// dlss_capture.vert takes off the positions stored then; this frame's clip space to the frame before's (72 to 87, as
/// shaders/dlss_prep.comp's reproject: the camera's motion), which the fragment stage's mark compares the draw's own with
constexpr uint32_t kHeadWords = 88;
constexpr uint32_t kToleranceWord = 66, kJitterBeforeWord = 68, kReprojectWord = 72, kMarkBit = 0x80000000u;
/// (with kMarkBit: the mark is not a motion but kOwnMark in x, which no motion is, and in y how far the draw's own motion
/// is from the camera's, in render pixels: shaders/dlss_prep.comp's flag 8 shows those pixels blue on grey)
constexpr uint32_t kOwnBit = 0x40000000u;
constexpr float kOwnMark = -30000.0f;
/// shaders/dlss_capture.vert's push constants: a draw's slots this frame and the frame before (the address of the first, the
/// vertex index it is, how many)
struct CapturePush {
uint64_t now;
uint64_t before;
uint32_t first_now;
uint32_t first_before;
uint32_t count_now;
uint32_t count_before;
float jitter_now[2]; // (the jitter of the camera the draw was drawn with, in clip space: this frame's, the frame before's)
float jitter_before[2];
};
static_assert(sizeof(CapturePush) == 48, "dlss_capture.vert's push constants");
/// The jitter of the camera a draw was drawn with, from its projection (4 x 4 row major, the vertex stage's uniform buffer 3
/// at 0x70): the offset in its third column, as the scene's (vk_temporal.cpp temporal_note_scene_draw). False when it is not
/// a perspective projection (its w row not 0, 0, -1, 0) or the offset is not a sub-pixel one: the scene's is used then
inline bool camera_jitter(const float proj[16], float out[2]) {
if (proj[12] != 0.0f || proj[13] != 0.0f || proj[14] != -1.0f || proj[15] != 0.0f) return false;
const float x = -proj[2], y = -proj[6];
if (!(x > -0.05f && x < 0.05f && y > -0.05f && y < 0.05f)) return false;
out[0] = x, out[1] = y;
return true;
}
/// The vertices an indexed draw's count indices name (itype 0, 1, 2: 8, 16, 32 bits): the least and the greatest. With
/// restart, the restart index (all ones) is none. False when there is none
inline bool index_range(const void* indices, int count, int itype, bool restart, uint32_t* lo, uint32_t* hi) {
if (count <= 0 || itype < 0 || itype > 2) return false;
const uint32_t size = 1u << itype;
const uint32_t cut = itype == 0 ? 0xffu : itype == 1 ? 0xffffu : 0xffffffffu;
uint32_t a = UINT32_MAX, b = 0;
bool any = false;
for (int i = 0; i < count; i++) {
uint32_t v = 0;
std::memcpy(&v, (const uint8_t*)indices + (size_t)i * size, size);
if (restart && v == cut) continue;
a = std::min(a, v), b = std::max(b, v), any = true;
}
if (any) *lo = a, *hi = b;
return any;
}
/// A draw's slots for its stored positions: the vertex index of the first (as the vertex stage sees it, the base vertex in it)
/// and how many, at most max. Indexed (itype 0 to 2): base_vertex + lo to base_vertex + hi; without indices (kNoIndices):
/// base_vertex (the draw's first) and count on. False when there are none, or more than max
inline bool capture_span(int itype, int count, int base_vertex, uint32_t lo, uint32_t hi, uint32_t max, uint32_t* first,
uint32_t* n) {
if (count <= 0) return false;
uint64_t many;
if (itype == kNoIndices) *first = (uint32_t)base_vertex, many = (uint64_t)count;
else if (itype >= 0 && itype <= 2 && lo <= hi) *first = (uint32_t)base_vertex + lo, many = (uint64_t)hi - lo + 1;
else return false;
if (many > max) return false;
*n = (uint32_t)many;
return true;
}
/// The slot of a vertex (its VertexIndex) in a span, as the vertex stages take it (unsigned, wrapping): false when outside
inline bool capture_slot(int32_t vertex_index, uint32_t first, uint32_t n, uint32_t* slot) {
const uint32_t s = (uint32_t)vertex_index - first;
if (s >= n) return false;
*slot = s;
return true;
}
/// A clip position stored the frame before, its jitter taken off (dlss_capture.vert; dlss_math::unjitter on a camera)
inline void unjitter_position(float p[4], const float jitter[2]) {
p[0] -= jitter[0] * p[3];
p[1] -= jitter[1] * p[3];
}
} // namespace dlss_actor