Files
Triforce-Source/gpu/vk/shaders/evil_ao.comp
ruckus7382sfgsgdf 2df08c751e Triforce v0.8.4
Smoother frames and less memory: a compact pipeline cache on NVIDIA, the renderer and game-code profiles, native float in the hot game code, late GPU work left as before. A PC whose NVIDIA shader cache drops the game's gets a driver cache folder of its own. Fixes from the 0.8.3 reports: RenoDX's encoder and tone mapping matched on NVIDIA again; the out-of-memory notice at the next start; no microphone open without a recording device; Evil HD undergrowth kept to 80/120 m; the FG HUD layer keeps what it learned; a stick past 92% is a full push; the DualSense jack read only when used; Linux Save Select, Quit and Restart, and the Linux launcher's sound; the update check uses the program's own version.
2026-11-26 20:04:11 -06:00

109 lines
6.7 KiB
Plaintext

#version 450
// (Triforce, 11-23) Evil HD's ambient occlusion (gpu/vk/vk_aa.cpp ao_after, evil_hd.h): how much of the sky each pixel of the
// 3D sees, from the scene's depth alone (the game keeps no normals we know of). A horizon-style estimate: the pixel's
// position and normal rebuilt from the depth, then twelve points on a spiral around it in the screen, a world radius wide;
// each one above the pixel's surface occludes it by its cosine, less with its distance. Stored as 1 (open) to 0 (closed),
// faded out with distance (the game's fog and far detail), 1 where there is no depth (the sky). evil_ao_apply.comp blurs it
// and darkens the picture by it.
// ((Triforce, 11-24) and the light bounced: the same points that cover the pixel are lit surfaces facing it, so their colour
// in the picture, by the same weights, is the light they throw on it (one bounce, screen-space): stored in rgb, the
// occlusion in alpha; evil_ao_apply.comp tints the pixel by it.)
//
// The depth is NVN's (0 at the near plane; flip: the other way): view distance = b / (d - a) (fsr3_math.h
// camera_from_projection). View x and y = ndc * distance / P[0][0] and / P[1][1]; their signs do not matter here: the normal
// is turned to face the camera.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform sampler2D depth_tex;
layout(binding = 1) uniform sampler2D src; // ((Triforce, 11-24) the picture the composition took, for the bounce)
layout(binding = 2, rgba16f) uniform writeonly image2D ao_out;
layout(push_constant) uniform Push {
vec2 inv; // (1 / size)
ivec2 size; // (the depth's and the picture's)
vec4 cam; // (a, b, 1 / P[0][0], 1 / P[1][1])
vec4 k; // (radius in world units, strength, the distance it is gone by, flags: 1 the depth runs the other way, 2 show)
} pc;
const int kSamples = 12;
const float kTurns = 7.0; // (the spiral's turns over the samples: no two on one line)
float distance_at(float d) {
if ((int(pc.k.w) & 1) != 0) d = 1.0 - d;
return pc.cam.y / max(d - pc.cam.x, 1e-7);
}
vec3 view_at(ivec2 p) {
p = clamp(p, ivec2(0), pc.size - 1);
const float z = distance_at(texelFetch(depth_tex, p, 0).r);
const vec2 ndc = (vec2(p) + 0.5) * pc.inv * 2.0 - 1.0;
return vec3(ndc.x * pc.cam.z * z, ndc.y * pc.cam.w * z, z);
}
void main() {
const ivec2 c = ivec2(gl_GlobalInvocationID.xy);
if (c.x >= pc.size.x || c.y >= pc.size.y) return;
const float radius = pc.k.x, far = pc.k.z;
const vec3 p = view_at(c);
if (!(p.z > 0.0) || p.z >= far || isinf(p.z) || isnan(p.z)) {
imageStore(ao_out, c, vec4(0.0, 0.0, 0.0, 1.0));
return;
}
// (the normal from the nearer neighbour on each axis: an edge's far side is not taken for the surface)
const vec3 l = view_at(c - ivec2(1, 0)), r = view_at(c + ivec2(1, 0)), d = view_at(c - ivec2(0, 1)), u = view_at(c + ivec2(0, 1));
const vec3 dx = abs(r.z - p.z) < abs(p.z - l.z) ? r - p : p - l;
const vec3 dy = abs(u.z - p.z) < abs(p.z - d.z) ? u - p : p - d;
vec3 n = normalize(cross(dx, dy));
if (dot(n, -p) < 0.0) n = -n;
// (how rough the depth is here: its second difference across and down, as a share of the distance. A wall, a floor or
// the inside of a corner is smooth; grass, leaves and hair are rough at every pixel, and their own blades would darken
// them in clumps: there the occlusion fades out, lab 11-23 at Hyrule Field)
const float rough = (abs(l.z + r.z - 2.0 * p.z) + abs(u.z + d.z - 2.0 * p.z)) / p.z;
const float smooth_here = 1.0 - smoothstep(0.004, 0.02, rough);
if (smooth_here <= 0.0) {
imageStore(ao_out, c, vec4(0.0, 0.0, 0.0, 1.0));
return;
}
// (the radius in pixels at this distance: P[1][1] maps view y / z to ndc, half the height an ndc unit; at least 2, at
// most a tenth of the picture, so the near ground costs no more than the far)
const float px = clamp(radius / (p.z * pc.cam.w) * 0.5 * float(pc.size.y), 2.0, 0.1 * float(pc.size.y));
const float noise = fract(52.9829189 * fract(0.06711056 * float(c.x) + 0.00583715 * float(c.y)));
const float r2 = radius * radius;
float occluded = 0.0;
vec3 bounce = vec3(0.0);
// ((Triforce, 11-25, a player's report "Ambient Occlusion in fog") how much the picture itself varies around the pixel: its
// brightness here and at the samples' points, lowest and highest. The game's fog is already in the picture this takes, and
// fog (or a room the game keeps dark) makes it nearly flat: the creases the occlusion finds there are ones the fog hides, and
// drawn over it they showed the rock through a foggy cave. Where the picture is that flat the occlusion fades out
const vec3 luma_w = vec3(0.2126, 0.7152, 0.0722);
float lo = dot(texelFetch(src, c, 0).rgb, luma_w), hi = lo;
for (int i = 0; i < kSamples; i++) {
const float t = (float(i) + 0.5) / float(kSamples);
const float angle = 6.2831853 * (t * kTurns + noise);
const vec2 offset = vec2(cos(angle), sin(angle)) * (px * t);
const ivec2 q = c + ivec2(round(offset));
const vec3 seen = texelFetch(src, clamp(q, ivec2(0), pc.size - 1), 0).rgb;
const float y = dot(seen, luma_w);
lo = min(lo, y), hi = max(hi, y);
const vec3 v = view_at(q) - p;
const float vv = dot(v, v);
if (vv < 1e-8) continue;
const float cosine = dot(v, n) * inversesqrt(vv);
// (a point only counts above a slope of about 15 degrees: grass blades a hand apart, nearly level with the
// ground around them, do not darken each other in clumps)
// (and not from what is within a hand of it: a grass blade's own ground, a few centimetres off, made every blade
// grey against the open ground between them (lab 11-23); a corner's walls are tens of centimetres away and count)
const float near_share = smoothstep(0.15 * radius, 0.4 * radius, sqrt(vv));
const float w = clamp((cosine - 0.25) / 0.75, 0.0, 1.0) * clamp(1.0 - vv / r2, 0.0, 1.0) * near_share;
occluded += w;
bounce += w * seen;
}
// (the spread as a share of the brightest, and 0.05 more: a dark place's small differences count as flat too)
const float contrast = (hi - lo) / (hi + 0.05);
const float not_flat = smoothstep(0.04, 0.16, contrast);
// (never darker than kFloor: a corner keeps some light, and what goes wrong (a fringe, a clump) cannot turn black)
const float kFloor = 0.5;
float ao = clamp(1.0 - pc.k.y * occluded / float(kSamples), kFloor, 1.0);
ao = mix(ao, 1.0, smoothstep(0.6 * far, far, p.z));
ao = mix(1.0, ao, smooth_here * not_flat);
const float keep = smooth_here * not_flat * (1.0 - smoothstep(0.6 * far, far, p.z));
imageStore(ao_out, c, vec4(bounce / float(kSamples) * keep, ao));
}