Files
Triforce-Source/gpu/vk/shaders/dlss_actor.vert
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

78 lines
4.6 KiB
GLSL

#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
// (hookshot) NVIDIA DLSS's actor motion (vk_dlss.cpp): a skinned draw of the scene drawn again, its vertices skinned with this
// frame's bones and with the frame before's, so each pixel it covers gets its own motion and not that of what is behind it.
// The cameras are in a storage buffer (data): this frame's view-projection as the game drew with it, the jitter in it; the frame
// before's with its jitter taken off (both row major, clip = M * position); the jitter; the render size. The vertices and
// indices are read where the game keeps them, the poses (bone palettes of 3 x 4 row-major matrices, origins) where vk_dlss.cpp
// copied them, by their device addresses. The game skins in model space and adds the object's origin:
// world = sum(w * B[j] * p) + origin.
layout(std430, binding = 0) readonly buffer Data { uint w[]; } data;
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer Words { uint w[]; };
layout(push_constant) uniform P {
uint64_t vertices; // (the vertex stream's address)
uint64_t indices; // (the index buffer's address, 4-aligned down; first: the draw's first index in it)
uint64_t origins[2]; // (the draw's origin, xyz: this frame's and the frame before's)
uint64_t bones[2]; // (its palettes: this frame's, the frame before's)
uint nbones;
uint stride; // (in words)
uint count; // (vertices in the stream: an index past them draws nothing)
uint first;
uint itype; // (0: 8-bit, 1: 16-bit, 2: 32-bit indices)
int base_vertex;
uint pos, sets; // (byte offsets in a vertex, the joint and weight sets: 1 or 2)
uint joints[2], weights[2];
uint formats; // (1: the position RGBA16F, else RGB32F; 2 << set: that set's joints RGBA8UI, else RGBA16UI)
uint tolerance; // (the fragment stage's)
} pc;
layout(location = 0) out vec4 before;
float f(uint i) { return uintBitsToFloat(data.w[i]); }
vec4 f4(uint i) { return vec4(f(i), f(i + 1), f(i + 2), f(i + 3)); }
mat4 camera(uint at) { // (row major in the buffer)
return transpose(mat4(f4(at), f4(at + 4), f4(at + 8), f4(at + 12)));
}
vec4 row(Words m, uint i) { return uintBitsToFloat(uvec4(m.w[i], m.w[i + 1u], m.w[i + 2u], m.w[i + 3u])); }
void skin(Words palette, uvec4 j, vec4 wt, vec4 p, inout vec3 acc) {
for (int k = 0; k < 4; k++) {
if (wt[k] <= 0.0 || j[k] >= pc.nbones) continue;
const uint b = j[k] * 12u;
acc += wt[k] * vec3(dot(row(palette, b), p), dot(row(palette, b + 4u), p), dot(row(palette, b + 8u), p));
}
}
void main() {
const uint at = pc.first + uint(gl_VertexIndex);
Words ib = Words(pc.indices);
uint index;
if (pc.itype == 2u) index = ib.w[at];
else if (pc.itype == 1u) index = (ib.w[at >> 1] >> ((at & 1u) * 16u)) & 0xffffu;
else index = (ib.w[at >> 2] >> ((at & 3u) * 8u)) & 0xffu;
const int vi = pc.base_vertex + int(index);
if (vi < 0 || uint(vi) >= pc.count) { // (outside the stream: nothing, its triangle dropped)
gl_Position = vec4(0.0, 0.0, 0.0, -1.0);
before = vec4(0.0);
return;
}
Words vb = Words(pc.vertices + uint64_t(uint(vi)) * uint64_t(pc.stride) * 4ul);
const uint q = pc.pos / 4u;
const vec4 p = (pc.formats & 1u) != 0u ? vec4(unpackHalf2x16(vb.w[q]), unpackHalf2x16(vb.w[q + 1u]).x, 1.0)
: vec4(uintBitsToFloat(vb.w[q]), uintBitsToFloat(vb.w[q + 1u]),
uintBitsToFloat(vb.w[q + 2u]), 1.0);
vec3 now = vec3(0.0), then = vec3(0.0);
for (uint s = 0u; s < pc.sets; s++) {
const uint a = vb.w[pc.joints[s] / 4u];
const uvec4 j = (pc.formats & (2u << s)) != 0u ? (uvec4(a, a >> 8, a >> 16, a >> 24) & 0xffu)
: uvec4(a & 0xffffu, a >> 16, vb.w[pc.joints[s] / 4u + 1u] & 0xffffu,
vb.w[pc.joints[s] / 4u + 1u] >> 16);
const vec4 wt = unpackUnorm4x8(vb.w[pc.weights[s] / 4u]);
skin(Words(pc.bones[0]), j, wt, p, now);
skin(Words(pc.bones[1]), j, wt, p, then);
}
const vec3 o0 = row(Words(pc.origins[0]), 0u).xyz, o1 = row(Words(pc.origins[1]), 0u).xyz;
const vec4 clip = camera(0u) * vec4(now + o0, 1.0);
before = camera(16u) * vec4(then + o1, 1.0);
// (the game's clip space: y up, depth -1 to 1; Vulkan's: y down, depth 0 to 1)
gl_Position = vec4(clip.x, -clip.y, (clip.z + clip.w) * 0.5, clip.w);
}