mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 21:15:57 +00:00
Per-object motion vectors for DLSS, camera motion in double precision, Ultra Performance and DLSS presets.
Solid scene draws whose position reads an object's own uniform blocks (bones c5, place c7, c6) are drawn a second time with a vertex shader variant (translator patch 0012) that computes last frame's position from last frame's blocks; the game writes those into two copies that take turns, and the uniform snapshot keeps both (vk_mvprev.c). The draws are listed during the replay and recorded through the recorder thread with repeated state skipped. Blocks whose last-frame copies measured wrong (c9 and c11 of walls and cliffs) keep this frame's; positions without an object block (terrain, grass) keep the camera's motion, which measured as good. The camera motion is reprojected in double precision on the CPU: the float inverse made noise on far surfaces and a still camera that DLSS followed into a wobble. DLSS gets Ultra Performance (a third of the output, also in the F1 menu) and NVIDIA's recommended preset per mode (K, M for Performance, L for Ultra Performance; TOTK_DLSS_PRESET overrides). Test switches: TOTK_MV_CHECK measures the motion vectors and DLSS's output against the picture, TOTK_DLAA_TIMES times DLSS and the motion passes on the GPU. A reactive mask pass for transparents is included but off (TOTK_DLSS_REACTIVE): its mask covers sky and fog too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2950132ad8
commit
ad3ecd8f3c
28 files changed
+3177
-78
No files matched your search
@@ -99,8 +99,8 @@ def prepare_sources() -> Path:
|
||||
for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")):
|
||||
shutil.rmtree(junk)
|
||||
patches = sorted((XLATE / "patches").glob("*.patch"))
|
||||
if len(patches) != 11:
|
||||
sys.exit(f"expected 11 patches, found {len(patches)}")
|
||||
if len(patches) != 12:
|
||||
sys.exit(f"expected 12 patches, found {len(patches)}")
|
||||
for patch in patches:
|
||||
# git apply works outside a repository when given a directory; --check first so a failure names the patch.
|
||||
for extra in (["--check"], []):
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
"""Writes runtime/dlaa_mask_spv.h: the SPIR-V of DLSS's reactive mask pass (vk_dlaa.c, mask_pass), assembled here like
|
||||
tools/make_mvobj_fs.py (no GLSL compiler on the machine this was written on).
|
||||
|
||||
The shader, as GLSL:
|
||||
|
||||
#version 450
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D before; // the light target when the transparents start
|
||||
layout(binding = 1) uniform sampler2D after; // the light target when DLSS runs
|
||||
layout(binding = 2, r8) uniform writeonly image2D mask;
|
||||
layout(push_constant) uniform P { vec4 p; }; // render size xy, threshold z, gain w
|
||||
void main() {
|
||||
vec2 g = vec2(gl_GlobalInvocationID.xy);
|
||||
if (g.x < p.x && g.y < p.y) {
|
||||
ivec2 c = ivec2(gl_GlobalInvocationID.xy);
|
||||
vec3 b = texelFetch(before, c, 0).rgb, a = texelFetch(after, c, 0).rgb;
|
||||
vec3 d = abs(a - b);
|
||||
float dm = max(max(d.r, d.g), d.b);
|
||||
float m = max(max(max(a.r, a.g), a.b), max(max(b.r, b.g), b.b));
|
||||
float r = dm / max(m, 1e-4);
|
||||
imageStore(mask, c, vec4(clamp((r - p.z) * p.w, 0.0, 1.0)));
|
||||
}
|
||||
}
|
||||
|
||||
The mask is the share of a pixel's colour the transparents changed (particles, smoke, sparks: what has no motion
|
||||
vectors of its own), 0 where nothing was drawn over the solid scene.
|
||||
|
||||
python tools/make_dlaa_mask.py
|
||||
"""
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
OUT = ROOT / 'runtime' / 'dlaa_mask_spv.h'
|
||||
|
||||
SECTIONS = ('cap', 'ext', 'mem', 'entry', 'exec', 'dec', 'types', 'func')
|
||||
|
||||
|
||||
class Asm:
|
||||
def __init__(self):
|
||||
self.next = 1
|
||||
self.sections = {k: [] for k in SECTIONS}
|
||||
|
||||
def id(self):
|
||||
self.next += 1
|
||||
return self.next - 1
|
||||
|
||||
def op(self, sec, code, *words):
|
||||
self.sections[sec].append([(len(words) + 1) << 16 | code, *words])
|
||||
|
||||
def words(self):
|
||||
out = [0x07230203, 0x00010000, 0, self.next, 0]
|
||||
for sec in SECTIONS:
|
||||
for ins in self.sections[sec]:
|
||||
out.extend(ins)
|
||||
return out
|
||||
|
||||
|
||||
def string(s):
|
||||
b = s.encode() + b'\0'
|
||||
b += b'\0' * (-len(b) % 4)
|
||||
return list(struct.unpack('<%dI' % (len(b) // 4), b))
|
||||
|
||||
|
||||
def f32(x):
|
||||
return struct.unpack('<I', struct.pack('<f', x))[0]
|
||||
|
||||
|
||||
OpCapability, OpExtInstImport, OpExtInst, OpMemoryModel, OpEntryPoint, OpExecutionMode = 17, 11, 12, 14, 15, 16
|
||||
OpDecorate, OpMemberDecorate = 71, 72
|
||||
OpTypeVoid, OpTypeBool, OpTypeInt, OpTypeFloat, OpTypeVector, OpTypeImage, OpTypeSampledImage = 19, 20, 21, 22, 23, 25, 27
|
||||
OpTypeStruct, OpTypePointer, OpTypeFunction = 30, 32, 33
|
||||
OpConstant, OpConstantComposite, OpVariable, OpFunction, OpFunctionEnd, OpLabel, OpReturn = 43, 44, 59, 54, 56, 248, 253
|
||||
OpLoad, OpAccessChain, OpVectorShuffle, OpCompositeExtract, OpCompositeConstruct = 61, 65, 79, 81, 80
|
||||
OpImage, OpImageFetch, OpImageWrite, OpConvertUToF, OpBitcast = 100, 95, 99, 112, 124
|
||||
OpFSub, OpFMul, OpFDiv, OpFOrdLessThan, OpLogicalAnd = 131, 133, 136, 184, 167
|
||||
OpSelectionMerge, OpBranch, OpBranchConditional = 247, 249, 250
|
||||
FAbs, FMax, FClamp = 4, 40, 43 # GLSL.std.450
|
||||
UniformConstant, Input, PushConstant = 0, 1, 9
|
||||
Binding, DescriptorSet, Block, Offset, BuiltIn, NonReadable = 33, 34, 2, 35, 11, 25
|
||||
GlobalInvocationId = 28
|
||||
|
||||
a = Asm()
|
||||
a.op('cap', OpCapability, 1) # Shader
|
||||
a.op('cap', OpCapability, 49) # StorageImageExtendedFormats (r8)
|
||||
glsl = a.id()
|
||||
a.op('ext', OpExtInstImport, glsl, *string('GLSL.std.450'))
|
||||
a.op('mem', OpMemoryModel, 0, 1) # Logical GLSL450
|
||||
main = a.id()
|
||||
t_void, t_fn, t_bool, t_f, t_i, t_u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
t_v2, t_v3, t_v4, t_v2i, t_v2u, t_v3u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
t_img, t_simg, t_wimg = a.id(), a.id(), a.id()
|
||||
t_psimg, t_pwimg, t_pgid, t_pc, t_ppc, t_ppv4 = a.id(), a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
v_before, v_after, v_mask, v_gid, v_p = a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
c_i0, c_zero, c_one, c_eps = a.id(), a.id(), a.id(), a.id()
|
||||
|
||||
a.op('entry', OpEntryPoint, 5, main, *string('main'), v_gid) # GLCompute
|
||||
a.op('exec', OpExecutionMode, main, 17, 8, 8, 1) # LocalSize
|
||||
a.op('dec', OpDecorate, v_gid, BuiltIn, GlobalInvocationId)
|
||||
for var, binding in ((v_before, 0), (v_after, 1), (v_mask, 2)):
|
||||
a.op('dec', OpDecorate, var, DescriptorSet, 0)
|
||||
a.op('dec', OpDecorate, var, Binding, binding)
|
||||
a.op('dec', OpDecorate, v_mask, NonReadable)
|
||||
a.op('dec', OpDecorate, t_pc, Block)
|
||||
a.op('dec', OpMemberDecorate, t_pc, 0, Offset, 0)
|
||||
|
||||
T = 'types'
|
||||
a.op(T, OpTypeVoid, t_void)
|
||||
a.op(T, OpTypeFunction, t_fn, t_void)
|
||||
a.op(T, OpTypeBool, t_bool)
|
||||
a.op(T, OpTypeFloat, t_f, 32)
|
||||
a.op(T, OpTypeInt, t_i, 32, 1)
|
||||
a.op(T, OpTypeInt, t_u, 32, 0)
|
||||
a.op(T, OpTypeVector, t_v2, t_f, 2)
|
||||
a.op(T, OpTypeVector, t_v3, t_f, 3)
|
||||
a.op(T, OpTypeVector, t_v4, t_f, 4)
|
||||
a.op(T, OpTypeVector, t_v2i, t_i, 2)
|
||||
a.op(T, OpTypeVector, t_v2u, t_u, 2)
|
||||
a.op(T, OpTypeVector, t_v3u, t_u, 3)
|
||||
a.op(T, OpTypeImage, t_img, t_f, 1, 0, 0, 0, 1, 0) # 2D, sampled, format unknown
|
||||
a.op(T, OpTypeSampledImage, t_simg, t_img)
|
||||
a.op(T, OpTypeImage, t_wimg, t_f, 1, 0, 0, 0, 2, 15) # 2D, storage, R8
|
||||
a.op(T, OpTypePointer, t_psimg, UniformConstant, t_simg)
|
||||
a.op(T, OpTypePointer, t_pwimg, UniformConstant, t_wimg)
|
||||
a.op(T, OpTypePointer, t_pgid, Input, t_v3u)
|
||||
a.op(T, OpTypeStruct, t_pc, t_v4)
|
||||
a.op(T, OpTypePointer, t_ppc, PushConstant, t_pc)
|
||||
a.op(T, OpTypePointer, t_ppv4, PushConstant, t_v4)
|
||||
a.op(T, OpConstant, t_i, c_i0, 0)
|
||||
a.op(T, OpConstant, t_f, c_zero, f32(0.0))
|
||||
a.op(T, OpConstant, t_f, c_one, f32(1.0))
|
||||
a.op(T, OpConstant, t_f, c_eps, f32(1e-4))
|
||||
a.op(T, OpVariable, t_psimg, v_before, UniformConstant)
|
||||
a.op(T, OpVariable, t_psimg, v_after, UniformConstant)
|
||||
a.op(T, OpVariable, t_pwimg, v_mask, UniformConstant)
|
||||
a.op(T, OpVariable, t_pgid, v_gid, Input)
|
||||
a.op(T, OpVariable, t_ppc, v_p, PushConstant)
|
||||
|
||||
F = 'func'
|
||||
|
||||
|
||||
def ins(code, rtype, *args):
|
||||
r = a.id()
|
||||
a.op(F, code, rtype, r, *args)
|
||||
return r
|
||||
|
||||
|
||||
def ext(rtype, inst, *args):
|
||||
return ins(OpExtInst, rtype, glsl, inst, *args)
|
||||
|
||||
|
||||
l_entry, l_body, l_merge = a.id(), a.id(), a.id()
|
||||
a.op(F, OpFunction, t_void, main, 0, t_fn)
|
||||
a.op(F, OpLabel, l_entry)
|
||||
pp = ins(OpAccessChain, t_ppv4, v_p, c_i0)
|
||||
p = ins(OpLoad, t_v4, pp)
|
||||
gid = ins(OpLoad, t_v3u, v_gid)
|
||||
gu = ins(OpVectorShuffle, t_v2u, gid, gid, 0, 1)
|
||||
gx = ins(OpConvertUToF, t_f, ins(OpCompositeExtract, t_u, gid, 0))
|
||||
gy = ins(OpConvertUToF, t_f, ins(OpCompositeExtract, t_u, gid, 1))
|
||||
px = ins(OpCompositeExtract, t_f, p, 0)
|
||||
py = ins(OpCompositeExtract, t_f, p, 1)
|
||||
inside = ins(OpLogicalAnd, t_bool, ins(OpFOrdLessThan, t_bool, gx, px), ins(OpFOrdLessThan, t_bool, gy, py))
|
||||
a.op(F, OpSelectionMerge, l_merge, 0)
|
||||
a.op(F, OpBranchConditional, inside, l_body, l_merge)
|
||||
a.op(F, OpLabel, l_body)
|
||||
c = ins(OpBitcast, t_v2i, gu)
|
||||
|
||||
|
||||
def fetch(var):
|
||||
img = ins(OpImage, t_img, ins(OpLoad, t_simg, var))
|
||||
texel = ins(OpImageFetch, t_v4, img, c, 2, c_i0) # Lod 0
|
||||
return ins(OpVectorShuffle, t_v3, texel, texel, 0, 1, 2)
|
||||
|
||||
|
||||
def maxc(v):
|
||||
x, y, z = (ins(OpCompositeExtract, t_f, v, k) for k in range(3))
|
||||
return ext(t_f, FMax, ext(t_f, FMax, x, y), z)
|
||||
|
||||
|
||||
b = fetch(v_before)
|
||||
af = fetch(v_after)
|
||||
d = ext(t_v3, FAbs, ins(OpFSub, t_v3, af, b))
|
||||
dm = maxc(d)
|
||||
m = ext(t_f, FMax, ext(t_f, FMax, maxc(af), maxc(b)), c_eps)
|
||||
r = ins(OpFDiv, t_f, dm, m)
|
||||
thr = ins(OpCompositeExtract, t_f, p, 2)
|
||||
gain = ins(OpCompositeExtract, t_f, p, 3)
|
||||
k = ext(t_f, FClamp, ins(OpFMul, t_f, ins(OpFSub, t_f, r, thr), gain), c_zero, c_one)
|
||||
out = ins(OpCompositeConstruct, t_v4, k, k, k, k)
|
||||
a.op(F, OpImageWrite, ins(OpLoad, t_wimg, v_mask), c, out)
|
||||
a.op(F, OpBranch, l_merge)
|
||||
a.op(F, OpLabel, l_merge)
|
||||
a.op(F, OpReturn)
|
||||
a.op(F, OpFunctionEnd)
|
||||
|
||||
w = a.words()
|
||||
lines = [', '.join(f'0x{x:08x}' for x in w[i:i + 8]) for i in range(0, len(w), 8)]
|
||||
OUT.write_text(
|
||||
'// Generated by tools/make_dlaa_mask.py: DLSS\'s reactive mask pass (vk_dlaa.c mask_pass). Do not edit.\n'
|
||||
'#pragma once\n#include <stdint.h>\n'
|
||||
'static const uint32_t dlaa_mask_spv[] = {\n ' + ',\n '.join(lines) + '\n};\n', newline='\n')
|
||||
print(f'{OUT}: {len(w)} words')
|
||||
@@ -0,0 +1,163 @@
|
||||
"""Writes runtime/vk_mvobj_fs.h: the SPIR-V of the per-object motion fragment shader (vk_mvobj.c), assembled here
|
||||
because it must be patched at run time (the Location of its input follows the vertex shader variant) and is small.
|
||||
|
||||
The shader, as GLSL:
|
||||
|
||||
#version 450
|
||||
layout(location = 0) in vec4 prev; // last frame's clip position (patch 0012); Location patched
|
||||
layout(set = 0, binding = 1, std140) uniform P { vec4 p; }; // render size xy, this frame's jitter zw (pixels)
|
||||
layout(location = 0) out vec4 o;
|
||||
void main() {
|
||||
vec2 win = gl_FragCoord.xy - p.zw; // this frame's window position, without the jitter
|
||||
vec2 pn = prev.xy / prev.w;
|
||||
vec2 pwin = vec2((pn.x * 0.5 + 0.5) * p.x, (0.5 - pn.y * 0.5) * p.y);
|
||||
o = prev.w > 1e-6 ? vec4(pwin - win, 0.0, 0.0) : vec4(0.0);
|
||||
}
|
||||
|
||||
The vector is last frame's window position minus this frame's, in render pixels, as runtime/shaders/dlaa_mv.comp
|
||||
writes it. Its descriptor set and binding are renumbered by vk_pipeline.c like a translated shader's (the .res.json
|
||||
text in vk_mvobj.c names binding 1 as slot 3).
|
||||
|
||||
python tools/make_mvobj_fs.py
|
||||
"""
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
OUT = ROOT / 'runtime' / 'vk_mvobj_fs.h'
|
||||
|
||||
|
||||
class Asm:
|
||||
def __init__(self):
|
||||
self.next = 1
|
||||
self.sections = {k: [] for k in ('cap', 'mem', 'entry', 'exec', 'dec', 'types', 'func')}
|
||||
self.loc_word = None # index of the patched Location literal in the final words
|
||||
|
||||
def id(self):
|
||||
self.next += 1
|
||||
return self.next - 1
|
||||
|
||||
def op(self, sec, code, *words):
|
||||
self.sections[sec].append([(len(words) + 1) << 16 | code, *words])
|
||||
return self.sections[sec][-1]
|
||||
|
||||
def words(self):
|
||||
out = [0x07230203, 0x00010000, 0, self.next, 0]
|
||||
for sec in ('cap', 'mem', 'entry', 'exec', 'dec', 'types', 'func'):
|
||||
for ins in self.sections[sec]:
|
||||
if ins is self.loc_ins:
|
||||
self.loc_word = len(out) + 3
|
||||
out.extend(ins)
|
||||
return out
|
||||
|
||||
|
||||
def string(s):
|
||||
b = s.encode() + b'\0'
|
||||
b += b'\0' * (-len(b) % 4)
|
||||
return list(struct.unpack('<%dI' % (len(b) // 4), b))
|
||||
|
||||
|
||||
def f32(x):
|
||||
return struct.unpack('<I', struct.pack('<f', x))[0]
|
||||
|
||||
|
||||
a = Asm()
|
||||
OpCapability, OpMemoryModel, OpEntryPoint, OpExecutionMode, OpDecorate, OpMemberDecorate = 17, 14, 15, 16, 71, 72
|
||||
OpTypeVoid, OpTypeFunction, OpTypeFloat, OpTypeVector, OpTypePointer, OpTypeStruct, OpTypeInt, OpTypeBool = 19, 33, 22, 23, 32, 30, 21, 20
|
||||
OpConstant, OpConstantComposite, OpVariable, OpFunction, OpLabel, OpLoad, OpStore, OpReturn, OpFunctionEnd = 43, 44, 59, 54, 248, 61, 62, 253, 56
|
||||
OpAccessChain, OpVectorShuffle, OpCompositeExtract, OpCompositeConstruct, OpFSub, OpFMul, OpFAdd, OpFDiv = 65, 79, 81, 80, 131, 133, 129, 136
|
||||
OpFOrdGreaterThan, OpSelect = 186, 169
|
||||
Input, Output, Uniform = 1, 3, 2
|
||||
Location, Binding, DescriptorSet, Block, Offset, BuiltIn = 30, 33, 34, 2, 35, 11
|
||||
FragCoord = 15
|
||||
|
||||
a.op('cap', OpCapability, 1) # Shader
|
||||
a.op('mem', OpMemoryModel, 0, 1) # Logical GLSL450
|
||||
main = a.id()
|
||||
t_void, t_fn, t_f, t_v2, t_v4, t_u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
t_bool = a.id()
|
||||
t_pin, t_pout, t_struct, t_pu, t_pf_u = a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
v_prev, v_frag, v_out, v_p = a.id(), a.id(), a.id(), a.id()
|
||||
c_half, c_zero, c_eps, c_u0, c_vzero = a.id(), a.id(), a.id(), a.id(), a.id()
|
||||
t_pv4u = a.id()
|
||||
a.op('entry', OpEntryPoint, 4, main, *string('main'), v_prev, v_frag, v_out) # Fragment
|
||||
a.op('exec', OpExecutionMode, main, 7) # OriginUpperLeft
|
||||
a.loc_ins = a.op('dec', OpDecorate, v_prev, Location, 0)
|
||||
a.op('dec', OpDecorate, v_frag, BuiltIn, FragCoord)
|
||||
a.op('dec', OpDecorate, v_out, Location, 0)
|
||||
a.op('dec', OpDecorate, t_struct, Block)
|
||||
a.op('dec', OpMemberDecorate, t_struct, 0, Offset, 0)
|
||||
a.op('dec', OpDecorate, v_p, DescriptorSet, 0)
|
||||
a.op('dec', OpDecorate, v_p, Binding, 1)
|
||||
a.op('types', OpTypeVoid, t_void)
|
||||
a.op('types', OpTypeFunction, t_fn, t_void)
|
||||
a.op('types', OpTypeFloat, t_f, 32)
|
||||
a.op('types', OpTypeVector, t_v2, t_f, 2)
|
||||
a.op('types', OpTypeVector, t_v4, t_f, 4)
|
||||
a.op('types', OpTypeInt, t_u, 32, 0)
|
||||
a.op('types', OpTypeBool, t_bool)
|
||||
a.op('types', OpTypePointer, t_pin, Input, t_v4)
|
||||
a.op('types', OpTypePointer, t_pout, Output, t_v4)
|
||||
a.op('types', OpTypeStruct, t_struct, t_v4)
|
||||
a.op('types', OpTypePointer, t_pu, Uniform, t_struct)
|
||||
a.op('types', OpTypePointer, t_pv4u, Uniform, t_v4)
|
||||
a.op('types', OpConstant, t_f, c_half, f32(0.5))
|
||||
a.op('types', OpConstant, t_f, c_zero, f32(0.0))
|
||||
a.op('types', OpConstant, t_f, c_eps, f32(1e-6))
|
||||
a.op('types', OpConstant, t_u, c_u0, 0)
|
||||
a.op('types', OpConstantComposite, t_v4, c_vzero, c_zero, c_zero, c_zero, c_zero)
|
||||
a.op('types', OpVariable, t_pin, v_prev, Input)
|
||||
a.op('types', OpVariable, t_pin, v_frag, Input)
|
||||
a.op('types', OpVariable, t_pout, v_out, Output)
|
||||
a.op('types', OpVariable, t_pu, v_p, Uniform)
|
||||
|
||||
F = 'func'
|
||||
a.op(F, OpFunction, t_void, main, 0, t_fn)
|
||||
a.op(F, OpLabel, a.id())
|
||||
pp = a.id(); a.op(F, OpAccessChain, t_pv4u, pp, v_p, c_u0)
|
||||
p = a.id(); a.op(F, OpLoad, t_v4, p, pp)
|
||||
prev = a.id(); a.op(F, OpLoad, t_v4, prev, v_prev)
|
||||
frag = a.id(); a.op(F, OpLoad, t_v4, frag, v_frag)
|
||||
fxy = a.id(); a.op(F, OpVectorShuffle, t_v2, fxy, frag, frag, 0, 1)
|
||||
jit = a.id(); a.op(F, OpVectorShuffle, t_v2, jit, p, p, 2, 3)
|
||||
win = a.id(); a.op(F, OpFSub, t_v2, win, fxy, jit)
|
||||
px, py, pw = a.id(), a.id(), a.id()
|
||||
a.op(F, OpCompositeExtract, t_f, px, prev, 0)
|
||||
a.op(F, OpCompositeExtract, t_f, py, prev, 1)
|
||||
a.op(F, OpCompositeExtract, t_f, pw, prev, 3)
|
||||
nx, ny = a.id(), a.id()
|
||||
a.op(F, OpFDiv, t_f, nx, px, pw)
|
||||
a.op(F, OpFDiv, t_f, ny, py, pw)
|
||||
sx, sy = a.id(), a.id()
|
||||
a.op(F, OpCompositeExtract, t_f, sx, p, 0)
|
||||
a.op(F, OpCompositeExtract, t_f, sy, p, 1)
|
||||
# x: (nx * 0.5 + 0.5) * sx
|
||||
x1, x2, x3 = a.id(), a.id(), a.id()
|
||||
a.op(F, OpFMul, t_f, x1, nx, c_half)
|
||||
a.op(F, OpFAdd, t_f, x2, x1, c_half)
|
||||
a.op(F, OpFMul, t_f, x3, x2, sx)
|
||||
# y: (0.5 - ny * 0.5) * sy
|
||||
y1, y2, y3 = a.id(), a.id(), a.id()
|
||||
a.op(F, OpFMul, t_f, y1, ny, c_half)
|
||||
a.op(F, OpFSub, t_f, y2, c_half, y1)
|
||||
a.op(F, OpFMul, t_f, y3, y2, sy)
|
||||
wx, wy = a.id(), a.id()
|
||||
a.op(F, OpCompositeExtract, t_f, wx, win, 0)
|
||||
a.op(F, OpCompositeExtract, t_f, wy, win, 1)
|
||||
mx, my = a.id(), a.id()
|
||||
a.op(F, OpFSub, t_f, mx, x3, wx)
|
||||
a.op(F, OpFSub, t_f, my, y3, wy)
|
||||
mv = a.id(); a.op(F, OpCompositeConstruct, t_v4, mv, mx, my, c_zero, c_zero)
|
||||
ok = a.id(); a.op(F, OpFOrdGreaterThan, t_bool, ok, pw, c_eps)
|
||||
res = a.id(); a.op(F, OpSelect, t_v4, res, ok, mv, c_vzero)
|
||||
a.op(F, OpStore, v_out, res)
|
||||
a.op(F, OpReturn)
|
||||
a.op(F, OpFunctionEnd)
|
||||
|
||||
w = a.words()
|
||||
lines = [', '.join(f'0x{x:08x}' for x in w[i:i + 8]) for i in range(0, len(w), 8)]
|
||||
OUT.write_text(
|
||||
'// Generated by tools/make_mvobj_fs.py: the per-object motion fragment shader (vk_mvobj.c). Do not edit.\n'
|
||||
f'#define MVOBJ_FS_LOCATION_WORD {a.loc_word} // the input Location literal, patched to the variant\'s\n'
|
||||
'static const uint32_t mvobj_fs_spv[] = {\n ' + ',\n '.join(lines) + '\n};\n', newline='\n')
|
||||
print(f'{OUT}: {len(w)} words, Location at word {a.loc_word}')
|
||||
@@ -117,6 +117,12 @@ static class SwshXlate
|
||||
public int QueryHostStorageBufferOffsetAlignment() => _job["sbuf_align"] is JsonNode n ? (int)n : 16;
|
||||
// "subgroup": n: lanes per subgroup when the host cannot pin 32 (AMD GCN/Vega run 64): ballots and votes span n lanes.
|
||||
public int QueryHostSubgroupSize() => _job["subgroup"] is JsonNode n ? (int)n : 32;
|
||||
// "mvprev": true: the vertex shader also writes last frame's position, computed from slots bank + 15 in place of
|
||||
// the user banks c3-c16 (patch 0012, runtime/vk_mvprev.c); res.json's "mvprev" names the output location (-1:
|
||||
// none) and "mvprev_banks" the banks the position depends on (bit n: cn).
|
||||
public bool QueryMotionPrevious() => _job["mvprev"] is JsonValue v && v.TryGetValue<bool>(out bool b) && b;
|
||||
public int MotionPreviousLocation = -1, MotionPreviousBanks;
|
||||
public void ReportMotionPrevious(int location, int banks) => (MotionPreviousLocation, MotionPreviousBanks) = (location, banks);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,6 +146,7 @@ static class SwshXlate
|
||||
["uses_frag_coord"] = p.Info.UsesFragCoord, ["frag_out_map"] = p.Info.FragmentOutputMap,
|
||||
["tex_queried"] = new JsonArray(acc.TexQueried.Select(x => (JsonNode)x).ToArray()), ["img_queried"] = new JsonArray(acc.ImgQueried.Select(x => (JsonNode)x).ToArray()),
|
||||
};
|
||||
if (acc.QueryMotionPrevious()) (res["mvprev"], res["mvprev_banks"]) = (acc.MotionPreviousLocation, acc.MotionPreviousBanks);
|
||||
foreach (var b in p.Info.CBuffers) res["cbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot });
|
||||
foreach (var b in p.Info.SBuffers) res["sbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot, ["cb_slot"] = b.SbCbSlot, ["cb_offset"] = b.SbCbOffset, ["flags"] = (int)b.Flags });
|
||||
foreach (var (list, name) in new[] { (p.Info.Textures, "textures"), (p.Info.Images, "images") })
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
--- a/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs
|
||||
+++ b/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs
|
||||
@@ -184,6 +184,14 @@ namespace Ryujinx.Graphics.Shader
|
||||
return false;
|
||||
}
|
||||
|
||||
+ // TOTK: true when a vertex shader should also write last frame's position (Transforms/MotionPrevious.cs),
|
||||
+ // the user output location it went to (-1: none) and the banks the position depends on (bit n: cn).
|
||||
+ bool QueryMotionPrevious() => false;
|
||||
+
|
||||
+ void ReportMotionPrevious(int location, int banks)
|
||||
+ {
|
||||
+ }
|
||||
+
|
||||
/// <summary>
|
||||
/// Queries host about the presence of the FrontFacing built-in variable bug.
|
||||
/// </summary>
|
||||
--- a/src/Ryujinx.Graphics.Shader/Translation/ResourceManager.cs
|
||||
+++ b/src/Ryujinx.Graphics.Shader/Translation/ResourceManager.cs
|
||||
@@ -65,7 +65,7 @@ namespace Ryujinx.Graphics.Shader.Translation
|
||||
_stage = stage;
|
||||
_stagePrefix = GetShaderStagePrefix(stage);
|
||||
|
||||
- _cbSlotToBindingMap = new SetBindingPair[18];
|
||||
+ _cbSlotToBindingMap = new SetBindingPair[32]; // TOTK: slots 18-31 for Transforms/MotionPrevious.cs
|
||||
_sbSlotToBindingMap = new SetBindingPair[16];
|
||||
_cbSlotToBindingMap.AsSpan().Fill(new(-1, -1));
|
||||
_sbSlotToBindingMap.AsSpan().Fill(new(-1, -1));
|
||||
--- /dev/null
|
||||
+++ b/src/Ryujinx.Graphics.Shader/Translation/Transforms/MotionPrevious.cs
|
||||
@@ -0,0 +1,330 @@
|
||||
+using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
+using System.Collections.Generic;
|
||||
+using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
||||
+
|
||||
+namespace Ryujinx.Graphics.Shader.Translation.Transforms
|
||||
+{
|
||||
+ // TOTK: last frame's position for per-object motion vectors (runtime/vk_exec_draw.c, vk_mvprev.c). A vertex shader's
|
||||
+ // position depends on uniform banks of this frame: the camera (c4), the object's place (c7), its bones (c5), the
|
||||
+ // clock that moves wind and water (c14), and so on. Every operation the position depends on that reads one of the
|
||||
+ // user banks c3..c16, directly or through other values, is computed a second time beside the original, reading slot
|
||||
+ // bank + 15 (18..31) instead, where the runtime binds that bank's block of last frame; the position this gives is
|
||||
+ // written to a free user output. Control flow stays the original's. Operations that cannot be repeated (stores,
|
||||
+ // texture operations) keep their original result. The runtime gets the output location and the banks the position
|
||||
+ // depends on (ReportMotionPrevious): an object with none of them changed since last frame held still.
|
||||
+ static class MotionPrevious
|
||||
+ {
|
||||
+ public const int SlotOffset = 15;
|
||||
+
|
||||
+ private static int Remap(int slot) => slot >= 3 && slot <= 16 ? slot + SlotOffset : -1;
|
||||
+
|
||||
+ // The output location written (-1: none free, or no position store), and in banks the mask of the banks (bit
|
||||
+ // n: cn) the position depends on.
|
||||
+ public static int Run(BasicBlock[] blocks, ResourceManager resourceManager, AttributeUsage attributeUsage, out int banks)
|
||||
+ {
|
||||
+ banks = 0;
|
||||
+ int location = -1;
|
||||
+
|
||||
+ for (int l = Constants.MaxAttributes - 1; l >= 0; l--)
|
||||
+ {
|
||||
+ if ((attributeUsage.UsedOutputAttributes & (1 << l)) == 0)
|
||||
+ {
|
||||
+ location = l;
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ if (location < 0)
|
||||
+ {
|
||||
+ return -1;
|
||||
+ }
|
||||
+
|
||||
+ // 1. What the position depends on: back from the values the position stores write, through every
|
||||
+ // operation and phi that defines them.
|
||||
+ HashSet<INode> slice = new();
|
||||
+ Stack<Operand> work = new();
|
||||
+ bool stores = false;
|
||||
+
|
||||
+ foreach (BasicBlock block in blocks)
|
||||
+ {
|
||||
+ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
||||
+ {
|
||||
+ if (IsPositionStore(node.Value))
|
||||
+ {
|
||||
+ stores = true;
|
||||
+ work.Push(((Operation)node.Value).GetSource(2));
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ if (!stores)
|
||||
+ {
|
||||
+ return -1;
|
||||
+ }
|
||||
+
|
||||
+ while (work.Count != 0)
|
||||
+ {
|
||||
+ Operand v = work.Pop();
|
||||
+
|
||||
+ if (v == null || v.Type != OperandType.LocalVariable || v.AsgOp is not INode def || !slice.Add(def))
|
||||
+ {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ for (int s = 0; s < def.SourcesCount; s++)
|
||||
+ {
|
||||
+ work.Push(def.GetSource(s));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ // 2. The banks the slice reads, and the bindings of those it loads with a computed offset to their
|
||||
+ // last-frame slot's (only these get one: a binding is declared in the SPIR-V whether used or not, and the
|
||||
+ // host's layout comes from the used ones). Direct operands (c[slot][offset]) get theirs when the
|
||||
+ // structured program is made.
|
||||
+ Dictionary<int, int> bindings = new();
|
||||
+
|
||||
+ foreach (INode n in slice)
|
||||
+ {
|
||||
+ if (n is Operation op && IsBankLoad(op, resourceManager, out int slot))
|
||||
+ {
|
||||
+ banks |= 1 << slot;
|
||||
+
|
||||
+ if (!bindings.ContainsKey(op.GetSource(0).Value))
|
||||
+ {
|
||||
+ bindings[op.GetSource(0).Value] = resourceManager.GetConstantBufferBinding(Remap(slot));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ for (int s = 0; s < n.SourcesCount; s++)
|
||||
+ {
|
||||
+ Operand src = n.GetSource(s);
|
||||
+
|
||||
+ if (src != null && src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0)
|
||||
+ {
|
||||
+ banks |= 1 << src.GetCbufSlot();
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ if (banks == 0)
|
||||
+ {
|
||||
+ return -1;
|
||||
+ }
|
||||
+
|
||||
+ // 3. Which of the slice's values depend on those banks, to a fixed point (phis carry it around loops).
|
||||
+ HashSet<Operand> tainted = new();
|
||||
+ bool changed = true;
|
||||
+
|
||||
+ while (changed)
|
||||
+ {
|
||||
+ changed = false;
|
||||
+
|
||||
+ foreach (BasicBlock block in blocks)
|
||||
+ {
|
||||
+ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
||||
+ {
|
||||
+ INode n = node.Value;
|
||||
+
|
||||
+ if (!slice.Contains(n) || n.DestsCount == 0 || !Reads(n, tainted, bindings))
|
||||
+ {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ for (int d = 0; d < n.DestsCount; d++)
|
||||
+ {
|
||||
+ Operand dest = n.GetDest(d);
|
||||
+
|
||||
+ if (dest != null && dest.Type == OperandType.LocalVariable && tainted.Add(dest))
|
||||
+ {
|
||||
+ changed = true;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ // 4. A copy of each such operation beside it, with new destinations.
|
||||
+ Dictionary<Operand, Operand> prev = new();
|
||||
+ List<(INode Original, INode Copy)> copies = new();
|
||||
+
|
||||
+ foreach (BasicBlock block in blocks)
|
||||
+ {
|
||||
+ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
||||
+ {
|
||||
+ INode n = node.Value;
|
||||
+
|
||||
+ if (!slice.Contains(n) || n.DestsCount == 0 || !IsTainted(n, tainted))
|
||||
+ {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ INode copy = null;
|
||||
+
|
||||
+ if (n is PhiNode phi)
|
||||
+ {
|
||||
+ Operand dest = Local();
|
||||
+ prev[phi.Dest] = dest;
|
||||
+ copy = new PhiNode(dest);
|
||||
+ }
|
||||
+ else if (n is Operation op && op.GetType() == typeof(Operation) && op.Inst != Instruction.Store)
|
||||
+ {
|
||||
+ Operand[] dests = new Operand[op.DestsCount];
|
||||
+
|
||||
+ for (int d = 0; d < dests.Length; d++)
|
||||
+ {
|
||||
+ Operand dest = op.GetDest(d);
|
||||
+ dests[d] = dest != null && dest.Type == OperandType.LocalVariable ? Local() : dest;
|
||||
+
|
||||
+ if (dest != null && dest.Type == OperandType.LocalVariable)
|
||||
+ {
|
||||
+ prev[dest] = dests[d];
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ Operand[] sources = new Operand[op.SourcesCount];
|
||||
+
|
||||
+ for (int s = 0; s < sources.Length; s++)
|
||||
+ {
|
||||
+ sources[s] = op.GetSource(s);
|
||||
+ }
|
||||
+
|
||||
+ Operation c = op.StorageKind != StorageKind.None && dests.Length == 1
|
||||
+ ? new Operation(op.Inst, op.StorageKind, dests[0], sources)
|
||||
+ : new Operation(op.Inst, op.Index, dests, sources);
|
||||
+ c.ForcePrecise = op.ForcePrecise;
|
||||
+ copy = c;
|
||||
+ }
|
||||
+
|
||||
+ if (copy != null)
|
||||
+ {
|
||||
+ node = block.Operations.AddAfter(node, copy);
|
||||
+ copies.Add((n, copy));
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ // 5. The copies' sources: last frame's values, the remapped banks.
|
||||
+ foreach ((INode original, INode copy) in copies)
|
||||
+ {
|
||||
+ if (copy is PhiNode phiCopy)
|
||||
+ {
|
||||
+ PhiNode phi = (PhiNode)original;
|
||||
+
|
||||
+ for (int s = 0; s < phi.SourcesCount; s++)
|
||||
+ {
|
||||
+ phiCopy.AddSource(phi.GetBlock(s), Prev(phi.GetSource(s), prev));
|
||||
+ }
|
||||
+
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ Operation op = (Operation)original, c = (Operation)copy;
|
||||
+
|
||||
+ for (int s = 0; s < op.SourcesCount; s++)
|
||||
+ {
|
||||
+ Operand src = op.GetSource(s);
|
||||
+
|
||||
+ if (s == 0 && op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer &&
|
||||
+ src.Type == OperandType.Constant && bindings.TryGetValue(src.Value, out int binding))
|
||||
+ {
|
||||
+ c.SetSource(s, Const(binding));
|
||||
+ }
|
||||
+ else if (src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0)
|
||||
+ {
|
||||
+ c.SetSource(s, Cbuf(Remap(src.GetCbufSlot()), src.GetCbufOffset()));
|
||||
+ }
|
||||
+ else
|
||||
+ {
|
||||
+ c.SetSource(s, Prev(src, prev));
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ // 6. Each position store also writes last frame's position to the output.
|
||||
+ foreach (BasicBlock block in blocks)
|
||||
+ {
|
||||
+ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
||||
+ {
|
||||
+ if (!IsPositionStore(node.Value))
|
||||
+ {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ Operation op = (Operation)node.Value;
|
||||
+ Operation store = new(Instruction.Store, StorageKind.Output, null,
|
||||
+ Const((int)IoVariable.UserDefined), Const(location), op.GetSource(1), Prev(op.GetSource(2), prev));
|
||||
+ node = block.Operations.AddAfter(node, store);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ attributeUsage.SetOutputUserAttribute(location);
|
||||
+
|
||||
+ return location;
|
||||
+ }
|
||||
+
|
||||
+ private static bool IsPositionStore(INode n)
|
||||
+ {
|
||||
+ return n is Operation op && op.Inst == Instruction.Store && op.StorageKind == StorageKind.Output &&
|
||||
+ op.SourcesCount == 3 && op.GetSource(0).Type == OperandType.Constant &&
|
||||
+ (IoVariable)op.GetSource(0).Value == IoVariable.Position;
|
||||
+ }
|
||||
+
|
||||
+ private static bool IsBankLoad(Operation op, ResourceManager resourceManager, out int slot)
|
||||
+ {
|
||||
+ slot = -1;
|
||||
+
|
||||
+ return op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer &&
|
||||
+ op.GetSource(0).Type == OperandType.Constant &&
|
||||
+ resourceManager.TryGetConstantBufferSlot(op.GetSource(0).Value, out slot) && Remap(slot) >= 0;
|
||||
+ }
|
||||
+
|
||||
+ private static Operand Prev(Operand src, Dictionary<Operand, Operand> prev)
|
||||
+ {
|
||||
+ return src != null && prev.TryGetValue(src, out Operand p) ? p : src;
|
||||
+ }
|
||||
+
|
||||
+ private static bool IsTainted(INode n, HashSet<Operand> tainted)
|
||||
+ {
|
||||
+ for (int d = 0; d < n.DestsCount; d++)
|
||||
+ {
|
||||
+ if (n.GetDest(d) is Operand dest && tainted.Contains(dest))
|
||||
+ {
|
||||
+ return true;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ private static bool Reads(INode n, HashSet<Operand> tainted, Dictionary<int, int> bindings)
|
||||
+ {
|
||||
+ if (n is Operation op && op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer &&
|
||||
+ op.GetSource(0).Type == OperandType.Constant && bindings.ContainsKey(op.GetSource(0).Value))
|
||||
+ {
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ for (int s = 0; s < n.SourcesCount; s++)
|
||||
+ {
|
||||
+ Operand src = n.GetSource(s);
|
||||
+
|
||||
+ if (src == null)
|
||||
+ {
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ if (src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0)
|
||||
+ {
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ if (tainted.Contains(src))
|
||||
+ {
|
||||
+ return true;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ return false;
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
--- a/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs
|
||||
+++ b/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs
|
||||
@@ -301,6 +301,12 @@ namespace Ryujinx.Graphics.Shader.Translation
|
||||
|
||||
Optimizer.RunPass(context);
|
||||
TransformPasses.RunPass(context);
|
||||
+
|
||||
+ if (i == 0 && Definitions.Stage == ShaderStage.Vertex && GpuAccessor.QueryMotionPrevious())
|
||||
+ {
|
||||
+ int location = MotionPrevious.Run(cfg.Blocks, resourceManager, AttributeUsage, out int banks);
|
||||
+ GpuAccessor.ReportMotionPrevious(location, banks);
|
||||
+ }
|
||||
}
|
||||
|
||||
funcs[i] = new Function(cfg.Blocks, $"fun{i}", false, inArgumentsCount, outArgumentsCount);
|
||||
Reference in new issue
Block a user