parent
48d9f1dd5a
commit
9e02b7f3b3
196 changed files with 116992 additions and 29969 deletions
238
samples/shaders/common/pbr.glsl
Normal file
238
samples/shaders/common/pbr.glsl
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Shared PBR helpers, included via `@include../common/pbr.glsl` by each
|
||||
// lit shape shader (cube, sphere, capsule, geom).
|
||||
//
|
||||
// brdf_evaluate: a subset of Disney/Principled. GGX (Trowbridge-Reitz)
|
||||
// NDF, Smith correlated geometry, Schlick Fresnel, Lambert diffuse. No
|
||||
// subsurface, transmission, clearcoat, sheen, anisotropy. References:
|
||||
// Walter et al. 2007, Karis 2013 (UE4), Heitz 2014.
|
||||
//
|
||||
// proceduralGrid: fwidth-antialiased two-tier grid pattern over world-space
|
||||
// XZ. Used by the geom pipeline when v_material.z selects the ground
|
||||
// material. Designed to feed straight into brdf_evaluate as baseColor.
|
||||
//
|
||||
// Callers pick the coordinate space. N, V, L must all be in the same
|
||||
// space. n.l, n.v, n.h are rotation-invariant. The cube shader uses view
|
||||
// space (its dFdx/dFdy normal can't safely cross to world on D3D11, see
|
||||
// cube.glsl), every other lit shader uses world space.
|
||||
|
||||
const float PBR_PI = 3.14159265358979323846;
|
||||
|
||||
float pbrDistributionGGX(float n_dot_h, float alpha)
|
||||
{
|
||||
float a2 = alpha * alpha;
|
||||
float d = (n_dot_h * n_dot_h) * (a2 - 1.0) + 1.0;
|
||||
return a2 / (PBR_PI * d * d);
|
||||
}
|
||||
|
||||
// Smith correlated G, Heitz 2014.
|
||||
float pbrVisibilitySmithCorrelated(float n_dot_v, float n_dot_l, float alpha)
|
||||
{
|
||||
float a2 = alpha * alpha;
|
||||
float vv = n_dot_l * sqrt(n_dot_v * n_dot_v * (1.0 - a2) + a2);
|
||||
float vl = n_dot_v * sqrt(n_dot_l * n_dot_l * (1.0 - a2) + a2);
|
||||
return 0.5 / (vv + vl + 1e-5);
|
||||
}
|
||||
|
||||
vec3 pbrFresnelSchlick(float v_dot_h, vec3 f0)
|
||||
{
|
||||
float t = 1.0 - v_dot_h;
|
||||
float t2 = t * t;
|
||||
return f0 + (vec3(1.0) - f0) * (t2 * t2 * t);
|
||||
}
|
||||
|
||||
// Direct sun lighting only. sun_radiance is sun_color.rgb premultiplied
|
||||
// by the shadow factor so the caller's existing shadow path is unchanged.
|
||||
// Ambient is split out into pbrEvaluateIbl below, callers compose
|
||||
// direct + ibl_ambient.
|
||||
vec3 brdf_evaluate(vec3 N, vec3 V, vec3 L, vec3 sun_radiance,
|
||||
vec3 base_color, float metallic, float roughness)
|
||||
{
|
||||
float n_dot_l = max(dot(N, L), 0.0);
|
||||
float n_dot_v = max(dot(N, V), 1e-4);
|
||||
|
||||
vec3 H = normalize(V + L);
|
||||
float n_dot_h = max(dot(N, H), 0.0);
|
||||
float v_dot_h = max(dot(V, H), 0.0);
|
||||
|
||||
// Perceptual roughness -> roughness alpha (Karis). The 0.045 floor
|
||||
// keeps the specular highlight from collapsing to a singular point
|
||||
// at roughness 0, which causes hot-pixel sparkle under MSAA.
|
||||
float perc = max(roughness, 0.045);
|
||||
float alpha = perc * perc;
|
||||
|
||||
vec3 f0 = mix(vec3(0.04), base_color, metallic);
|
||||
|
||||
float D = pbrDistributionGGX(n_dot_h, alpha);
|
||||
float Vis = pbrVisibilitySmithCorrelated(n_dot_v, n_dot_l, alpha);
|
||||
vec3 F = pbrFresnelSchlick(v_dot_h, f0);
|
||||
|
||||
vec3 specular = D * Vis * F;
|
||||
vec3 diffuse = (vec3(1.0) - F) * (1.0 - metallic) * base_color / PBR_PI;
|
||||
|
||||
return (diffuse + specular) * sun_radiance * n_dot_l;
|
||||
}
|
||||
|
||||
// Evaluate band-0..2 SH at world-space direction N. The stored
|
||||
// coefficients (sh[0..8].xyz) are already pre-multiplied with the
|
||||
// Funk-Hecke / Ramamoorthi diffuse convolution weights, so this returns
|
||||
// irradiance E(N) directly. Pre-multiply convention is fixed C-side in
|
||||
// projectSkyToSh, the shader just dots against the SH basis.
|
||||
//
|
||||
// Index order matches Sloan / Ramamoorthi:
|
||||
// 0 = (l=0,m= 0)
|
||||
// 1 = (l=1,m=-1) 2 = (l=1,m= 0) 3 = (l=1,m=+1)
|
||||
// 4 = (l=2,m=-2) 5 = (l=2,m=-1) 6 = (l=2,m= 0) 7 = (l=2,m=+1) 8 = (l=2,m=+2).
|
||||
vec3 pbrEvaluateShIrradiance(vec3 N, vec4 sh[9])
|
||||
{
|
||||
const float Y00 = 0.282094791773878;
|
||||
const float Y1n1 = 0.488602511902919;
|
||||
const float Y10 = 0.488602511902919;
|
||||
const float Y1p1 = 0.488602511902919;
|
||||
const float Y2n2 = 1.092548430592079;
|
||||
const float Y2n1 = 1.092548430592079;
|
||||
const float Y20 = 0.315391565252520;
|
||||
const float Y2p1 = 1.092548430592079;
|
||||
const float Y2p2 = 0.546274215296039;
|
||||
|
||||
float b0 = Y00;
|
||||
float b1 = -Y1n1 * N.y;
|
||||
float b2 = Y10 * N.z;
|
||||
float b3 = -Y1p1 * N.x;
|
||||
float b4 = Y2n2 * N.x * N.y;
|
||||
float b5 = -Y2n1 * N.y * N.z;
|
||||
float b6 = Y20 * (3.0 * N.z * N.z - 1.0);
|
||||
float b7 = -Y2p1 * N.x * N.z;
|
||||
float b8 = Y2p2 * (N.x * N.x - N.y * N.y);
|
||||
|
||||
return sh[0].xyz * b0
|
||||
+ sh[1].xyz * b1 + sh[2].xyz * b2 + sh[3].xyz * b3
|
||||
+ sh[4].xyz * b4 + sh[5].xyz * b5 + sh[6].xyz * b6
|
||||
+ sh[7].xyz * b7 + sh[8].xyz * b8;
|
||||
}
|
||||
|
||||
// IBL ambient: SH diffuse + GGX prefilter specular via Karis split-sum.
|
||||
// Returns the ambient RGB to add to the direct lighting term. N_world
|
||||
// and V_world must be in world space (the cube shader transforms its
|
||||
// view-space normal here before calling).
|
||||
//
|
||||
// Fresnel-roughness compensates the diffuse fraction at grazing angles
|
||||
// for rough surfaces, Lagarde 2014. F0_max = max(1-roughness, F0) lets
|
||||
// dielectrics stay bright even when roughness is high.
|
||||
vec3 pbrEvaluateIbl(vec3 N_world, vec3 V_world, vec3 base_color, float metallic,
|
||||
float roughness, vec4 sh[9],
|
||||
textureCube tex_spec_cube, sampler smp_spec_cube,
|
||||
texture2D tex_brdf_lut, sampler smp_brdf_lut,
|
||||
float max_lod)
|
||||
{
|
||||
// Clamp upper bound too: impostor n_world (sphere/capsule) comes from
|
||||
// (hit - center) / radius and isn't explicitly normalized, so float
|
||||
// drift can push |n_world| slightly above 1 and n_dot_v above 1. Then
|
||||
// 1.0 - n_dot_v < 0 and pow(negative, 5.0) is undefined per GLSL spec
|
||||
// (NVIDIA returns NaN), which propagates through F and renders as
|
||||
// isolated black pixels on the surface.
|
||||
float n_dot_v = clamp(dot(N_world, V_world), 1e-4, 1.0);
|
||||
vec3 R = reflect(-V_world, N_world);
|
||||
|
||||
vec3 F0 = mix(vec3(0.04), base_color, metallic);
|
||||
vec3 F0m = max(vec3(1.0 - roughness), F0);
|
||||
|
||||
// max(0, ...) is a no-op given the n_dot_v clamp above, but the HLSL
|
||||
// cross-compile loses the range info and warns about pow(negative, n)
|
||||
// (X3571) without it.
|
||||
vec3 F = F0 + (F0m - F0) * pow(max(0.0, 1.0 - n_dot_v), 5.0);
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = (vec3(1.0) - kS) * (1.0 - metallic);
|
||||
|
||||
vec3 irradiance = max(pbrEvaluateShIrradiance(N_world, sh), vec3(0.0));
|
||||
vec3 diffuse = kD * base_color * irradiance / PBR_PI;
|
||||
|
||||
float lod = roughness * max_lod;
|
||||
vec3 prefiltered = textureLod(samplerCube(tex_spec_cube, smp_spec_cube), R, lod).rgb;
|
||||
vec2 brdf = texture(sampler2D(tex_brdf_lut, smp_brdf_lut),
|
||||
vec2(n_dot_v, roughness)).rg;
|
||||
vec3 specular = prefiltered * (F0 * brdf.x + brdf.y);
|
||||
|
||||
return diffuse + specular;
|
||||
}
|
||||
|
||||
// Ambient selector. iblEnabled chooses between the image-based ambient
|
||||
// (pbrEvaluateIbl above) and the legacy flat ambient, base_color *
|
||||
// flat_strength, that the renderer used. The renderer drives
|
||||
// iblEnabled from a Render Settings panel toggle so the IBL contribution
|
||||
// can be A/B'd against the flat term.
|
||||
//
|
||||
// The branch is on a uniform: every fragment of every lit draw sees the
|
||||
// same iblEnabled, so this is uniform control flow and the implicit-LOD
|
||||
// BRDF-LUT fetch inside pbrEvaluateIbl stays well-defined. Callers pass the
|
||||
// same N_world/V_world/base_color they would have passed to pbrEvaluateIbl.
|
||||
vec3 pbrEvaluateAmbient(bool iblEnabled,
|
||||
vec3 N_world, vec3 V_world,
|
||||
vec3 base_color, float metallic, float roughness,
|
||||
float flat_strength,
|
||||
vec4 sh[9],
|
||||
textureCube tex_spec_cube, sampler smp_spec_cube,
|
||||
texture2D tex_brdf_lut, sampler smp_brdf_lut,
|
||||
float max_lod)
|
||||
{
|
||||
// Single result, pre-seeded with the flat-ambient fallback. The early-
|
||||
// return form `if (iblEnabled) return...; return flat;` cross-
|
||||
// compiles through SPIRV-Cross into a returned-flag pattern whose return
|
||||
// temp fxc can't prove definitely-assigned (warning X4000). Seeding the
|
||||
// result and overwriting it in the one branch sidesteps that. The IBL
|
||||
// path is still evaluated only when enabled, and the value is bit-
|
||||
// identical to a direct pbrEvaluateIbl call.
|
||||
vec3 result = base_color * flat_strength;
|
||||
if (iblEnabled) {
|
||||
result = pbrEvaluateIbl(N_world, V_world,
|
||||
base_color, metallic, roughness,
|
||||
sh,
|
||||
tex_spec_cube, smp_spec_cube,
|
||||
tex_brdf_lut, smp_brdf_lut,
|
||||
max_lod);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Two-tier antialiased grid:
|
||||
// * minor lines at `cell_size` cadence, slight darkening
|
||||
// * major lines every 10 cells, stronger darkening
|
||||
// `fwidth` of the cell coordinate keeps the line width near 1 screen pixel
|
||||
// at any distance / viewing angle. Caller passes a flat per-instance
|
||||
// `cell_size`, the result is suitable as `base_color` for brdf_evaluate.
|
||||
//
|
||||
// Derivatives are valid here only because the geom shader calls this
|
||||
// from a branch on a flat per-instance varying (every fragment in a
|
||||
// quad of a single draw shares the same materialMode).
|
||||
vec3 proceduralGrid(vec2 world_xz, vec3 base_color, float cell_size)
|
||||
{
|
||||
vec2 coord_minor = world_xz / cell_size;
|
||||
vec2 d_minor = max(fwidth(coord_minor), vec2(1.0e-6));
|
||||
vec2 g_minor = abs(fract(coord_minor - 0.5) - 0.5) / d_minor;
|
||||
float line_minor = 1.0 - clamp(min(g_minor.x, g_minor.y), 0.0, 1.0);
|
||||
|
||||
vec2 coord_major = world_xz / (cell_size * 10.0);
|
||||
vec2 d_major = max(fwidth(coord_major), vec2(1.0e-6));
|
||||
vec2 g_major = abs(fract(coord_major - 0.5) - 0.5) / d_major;
|
||||
float line_major = 1.0 - clamp(min(g_major.x, g_major.y), 0.0, 1.0);
|
||||
|
||||
vec3 minor_color = base_color * 0.70;
|
||||
vec3 major_color = base_color * 0.35;
|
||||
|
||||
vec3 result = mix(base_color, minor_color, line_minor);
|
||||
result = mix(result, major_color, line_major);
|
||||
|
||||
// Colored axes: red along +X, blue along +Z. Pixel-wide like the grid
|
||||
// lines, gated to the positive half so the origin reads as a corner.
|
||||
// Saturated primaries are identical in linear and sRGB, so they need no
|
||||
// encoding fixup before the BRDF.
|
||||
vec2 d_axis = max(fwidth(world_xz), vec2(1.0e-6));
|
||||
float axis_x = (1.0 - clamp(abs(world_xz.y) / d_axis.y, 0.0, 1.0)) * step(0.0, world_xz.x);
|
||||
float axis_z = (1.0 - clamp(abs(world_xz.x) / d_axis.x, 0.0, 1.0)) * step(0.0, world_xz.y);
|
||||
result = mix(result, vec3(1.0, 0.0, 0.0), axis_x);
|
||||
result = mix(result, vec3(0.0, 0.0, 1.0), axis_z);
|
||||
return result;
|
||||
}
|
||||
146
samples/shaders/common/preetham.glsl
Normal file
146
samples/shaders/common/preetham.glsl
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Preetham/Shirley/Smits 1999 analytic daylight sky.
|
||||
//
|
||||
// preethamSky(view_dir, sun_dir, turbidity) returns linear-sRGB radiance
|
||||
// for an unoccluded sky direction. Both directions must be unit-length,
|
||||
// Y is up. Output is unmultiplied, callers apply the luminance scale
|
||||
// and below-horizon fade themselves, or call preethamSkyScaled which
|
||||
// does both.
|
||||
//
|
||||
// Preetham diverges at and below the horizon. The helper clamps the
|
||||
// view direction to slightly above the horizon and clamps sun.y to the
|
||||
// upper hemisphere so the Perez/zenith math stays finite when the sun
|
||||
// is below the horizon, the caller is responsible for fading output to
|
||||
// zero in that case.
|
||||
|
||||
const float PREETHAM_PI = 3.14159265358979323846;
|
||||
|
||||
// Empirical luminance scale, chosen to land near the target ~10:1
|
||||
// sun:sky ratio under the renderer's sun_color premultiplied-by-pi
|
||||
// convention. Shared between the sky backdrop (sky.glsl) and the IBL
|
||||
// sky cubemap (sky_to_cube.glsl) so both consumers see the same sky.
|
||||
const float PREETHAM_LUMINANCE_SCALE = 0.06;
|
||||
|
||||
// Perez luminance distribution:
|
||||
// F(theta, gamma) = (1 + A * exp(B / cos(theta)))
|
||||
// * (1 + C * exp(D * gamma) + E * cos^2(gamma))
|
||||
// theta = view angle from zenith, gamma = view angle from sun direction.
|
||||
// cos_theta is clamped >= 0.01 to avoid the exp(B / 0) singularity at
|
||||
// the horizon (Preetham is invalid there anyway).
|
||||
float preethamPerez(float cos_theta, float cos_gamma, float gamma,
|
||||
float A, float B, float C, float D, float E)
|
||||
{
|
||||
float term1 = 1.0 + A * exp(B / max(cos_theta, 0.01));
|
||||
float term2 = 1.0 + C * exp(D * gamma) + E * cos_gamma * cos_gamma;
|
||||
return term1 * term2;
|
||||
}
|
||||
|
||||
// CIE xyY -> XYZ.
|
||||
vec3 preethamXyY_to_XYZ(float x, float y, float Y)
|
||||
{
|
||||
float yy = max(y, 1.0e-5);
|
||||
return vec3(Y * x / yy, Y, Y * (1.0 - x - y) / yy);
|
||||
}
|
||||
|
||||
// CIE XYZ (D65) -> linear sRGB.
|
||||
vec3 preethamXYZ_to_linear_srgb(vec3 c)
|
||||
{
|
||||
return vec3(
|
||||
dot(c, vec3(3.2404542, - 1.5371385, - 0.4985314)),
|
||||
dot(c, vec3(-0.9692660, 1.8760108, 0.0415560)),
|
||||
dot(c, vec3(0.0556434, - 0.2040259, 1.0572252))
|
||||
);
|
||||
}
|
||||
|
||||
vec3 preethamSky(vec3 view_dir, vec3 sun_dir, float turbidity)
|
||||
{
|
||||
// Clamp sun's elevation to the upper hemisphere so the Perez/zenith
|
||||
// math stays finite below the horizon. The horizontal components
|
||||
// sun_dir are intentionally kept unmodified, cos_gamma reads the
|
||||
// original direction so the sun's azimuth is preserved.
|
||||
float sun_y = clamp(sun_dir.y, 0.0, 1.0);
|
||||
|
||||
// Pin the look-up direction to slightly above the horizon when the
|
||||
// view points downward. Preetham diverges below the horizon, and
|
||||
// this gives the sky a sensible colour where the ground would
|
||||
// otherwise occlude it (e.g. scenes with no ground in the lower
|
||||
// portion of frame).
|
||||
vec3 view_clamped = normalize(vec3(view_dir.x, max(view_dir.y, 0.0) + 0.01, view_dir.z));
|
||||
|
||||
float cos_theta = max(view_clamped.y, 0.0); // zenith angle of view
|
||||
float cos_gamma = clamp(dot(sun_dir, view_clamped), - 1.0, 1.0); // sun-to-view angle
|
||||
float gamma = acos(cos_gamma);
|
||||
float sun_theta = acos(sun_y);
|
||||
|
||||
// Perez coefficients as functions of turbidity T (Preetham table 2)
|
||||
float T = max(turbidity, 1.0);
|
||||
float T2 = T * T;
|
||||
|
||||
// Y (luminance):
|
||||
float A_Y = 0.1787 * T - 1.4630;
|
||||
float B_Y = -0.3554 * T + 0.4275;
|
||||
float C_Y = -0.0227 * T + 5.3251;
|
||||
float D_Y = 0.1206 * T - 2.5771;
|
||||
float E_Y = -0.0670 * T + 0.3703;
|
||||
// x (chromaticity):
|
||||
float A_x = -0.0193 * T - 0.2592;
|
||||
float B_x = -0.0665 * T + 0.0008;
|
||||
float C_x = -0.0004 * T + 0.2125;
|
||||
float D_x = -0.0641 * T - 0.8989;
|
||||
float E_x = -0.0033 * T + 0.0452;
|
||||
// y (chromaticity):
|
||||
float A_y = -0.0167 * T - 0.2608;
|
||||
float B_y = -0.0950 * T + 0.0092;
|
||||
float C_y = -0.0079 * T + 0.2102;
|
||||
float D_y = -0.0441 * T - 1.6537;
|
||||
float E_y = -0.0109 * T + 0.0529;
|
||||
|
||||
// Zenith chromaticities (Preetham Eq. A.3, polynomial in T and sun_theta).
|
||||
float ts = sun_theta;
|
||||
float ts2 = ts * ts;
|
||||
float ts3 = ts2 * ts;
|
||||
|
||||
float x_z =
|
||||
(0.00166 * ts3 - 0.00375 * ts2 + 0.00209 * ts) * T2 +
|
||||
(-0.02903 * ts3 + 0.06377 * ts2 - 0.03202 * ts + 0.00394) * T +
|
||||
(0.11693 * ts3 - 0.21196 * ts2 + 0.06052 * ts + 0.25886);
|
||||
|
||||
float y_z =
|
||||
(0.00275 * ts3 - 0.00610 * ts2 + 0.00317 * ts) * T2 +
|
||||
(-0.04214 * ts3 + 0.08970 * ts2 - 0.04153 * ts + 0.00516) * T +
|
||||
(0.15346 * ts3 - 0.26756 * ts2 + 0.06670 * ts + 0.26688);
|
||||
|
||||
// Zenith luminance (Preetham Eq. A.2). Result in kcd/m^2.
|
||||
float chi = (4.0 / 9.0 - T / 120.0) * (PREETHAM_PI - 2.0 * sun_theta);
|
||||
float Y_z = (4.0453 * T - 4.9710) * tan(chi) - 0.2155 * T + 2.4192;
|
||||
|
||||
// Normalize: F(theta=0, gamma=sun_theta) at zenith equals zenith
|
||||
// values, so divide by perez(zenith) and multiply by perez(view).
|
||||
float cos_zen_gamma = sun_y; // dot(zenith, sun_dir) = sun_dir.y
|
||||
float pY_v = preethamPerez(cos_theta, cos_gamma, gamma, A_Y, B_Y, C_Y, D_Y, E_Y);
|
||||
float px_v = preethamPerez(cos_theta, cos_gamma, gamma, A_x, B_x, C_x, D_x, E_x);
|
||||
float py_v = preethamPerez(cos_theta, cos_gamma, gamma, A_y, B_y, C_y, D_y, E_y);
|
||||
float pY_z = preethamPerez(1.0, cos_zen_gamma, sun_theta, A_Y, B_Y, C_Y, D_Y, E_Y);
|
||||
float px_z = preethamPerez(1.0, cos_zen_gamma, sun_theta, A_x, B_x, C_x, D_x, E_x);
|
||||
float py_z = preethamPerez(1.0, cos_zen_gamma, sun_theta, A_y, B_y, C_y, D_y, E_y);
|
||||
|
||||
float Y = max(Y_z * pY_v / max(pY_z, 1.0e-5), 0.0);
|
||||
float x = x_z * px_v / max(px_z, 1.0e-5);
|
||||
float y = y_z * py_v / max(py_z, 1.0e-5);
|
||||
|
||||
vec3 XYZ = preethamXyY_to_XYZ(x, y, Y);
|
||||
vec3 rgb = preethamXYZ_to_linear_srgb(XYZ);
|
||||
return max(rgb, vec3(0.0));
|
||||
}
|
||||
|
||||
// Convenience wrapper: raw Preetham scaled by the empirical luminance
|
||||
// factor and the caller's below-horizon fade weight (1 above horizon,
|
||||
// 0 below). The sky backdrop shader and the IBL sky cubemap both call
|
||||
// this so they're guaranteed to agree pixel-for-pixel.
|
||||
vec3 preethamSkyScaled(vec3 view_dir, vec3 sun_dir, float turbidity, float fade)
|
||||
{
|
||||
return preethamSky(view_dir, sun_dir, turbidity) * PREETHAM_LUMINANCE_SCALE * fade;
|
||||
}
|
||||
|
||||
192
samples/shaders/common/xe_gtao_common.glsl
Normal file
192
samples/shaders/common/xe_gtao_common.glsl
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// XeGTAO helper functions shared between the GTAO compute shaders
|
||||
// (MainPass, Denoise). Ported from Esoterica HLSL with the following
|
||||
// GLSL adaptations:
|
||||
//
|
||||
// * lpfloat collapses to float (XE_GTAO_USE_HALF_FLOAT_PRECISION=0,
|
||||
// XE_GTAO_FP32_DEPTHS=1, matches Esoterica's config).
|
||||
// * HLSL `out` parameters become inout where needed.
|
||||
// * `mul(matrix, vec)` is rewritten as `matrix * vec` and column-major
|
||||
// conventions are preserved.
|
||||
// * Functions that took a `Texture2D<float>` / `SamplerState` /
|
||||
// `RWTexture2D<float>` parameter in HLSL cannot be cleanly expressed
|
||||
// in cross-compiled GLSL. Those functions (XeGTAO_MainPass,
|
||||
// XeGTAO_ComputeViewspaceNormal, XeGTAO_OutputWorkingTerm,
|
||||
// XeGTAO_Denoise) are inlined into the consuming shader and reference
|
||||
// its texture/image bindings directly. This file holds only the pure
|
||||
// helpers.
|
||||
// * GTAOConsts is a plain struct populated once per dispatch from the
|
||||
// ub_gtao UBO. Each consumer constructs a GTAOConsts in main and
|
||||
// passes it down to the helpers below. The struct is a strict subset
|
||||
// of Esoterica's `GTAOConstants`, only the fields the helpers
|
||||
// actually read are carried, so each consuming shader's ub_gtao only
|
||||
// needs to declare those (plus whatever else its own main reads
|
||||
// directly).
|
||||
//
|
||||
// Include via `@include xe_gtao_common.glsl` inside the consumer's @cs
|
||||
// block. The consumer is responsible for declaring the ub_gtao UBO with
|
||||
// the fields its main reads.
|
||||
|
||||
const float XE_GTAO_PI = 3.1415926535897932384626433832795;
|
||||
const float XE_GTAO_PI_HALF = 1.5707963267948966192313216916398;
|
||||
|
||||
// Esoterica's XE_GTAO_OCCLUSION_TERM_SCALE, applied by
|
||||
// XeGTAO_OutputWorkingTerm (divides) and the denoise finalApply
|
||||
// (multiplies). Keeps the noisy occlusion-term in [0,1] for storage,
|
||||
// rescaled to ~[0,1.5] for the final lit lookup.
|
||||
const float XE_GTAO_OCCLUSION_TERM_SCALE = 1.5;
|
||||
|
||||
// XE_GTAO_DEPTH_MIP_LEVELS = 5, the prefiltered depth chain has 5 mips
|
||||
// (mip 0..4). MainPass clamps the computed sample mip to [0, 5).
|
||||
const float XE_GTAO_DEPTH_MIP_LEVELS = 5.0;
|
||||
|
||||
// HilbertIndex parameters, XE_HILBERT_LEVEL = 6U, width 64.
|
||||
const uint XE_HILBERT_WIDTH = 64u;
|
||||
|
||||
struct GTAOConsts
|
||||
{
|
||||
vec2 ViewportPixelSize;
|
||||
vec2 NDCToViewMul;
|
||||
vec2 NDCToViewAdd;
|
||||
vec2 NDCToViewMul_x_PixelSize;
|
||||
float EffectRadius;
|
||||
float EffectFalloffRange;
|
||||
float RadiusMultiplier;
|
||||
float FinalValuePower;
|
||||
float SampleDistributionPower;
|
||||
float ThinOccluderCompensation;
|
||||
float DepthMIPSamplingOffset;
|
||||
float SliceCount;
|
||||
float StepsPerSlice;
|
||||
};
|
||||
|
||||
// http://h14s.p5r.org/2012/09/0x5f3759df.html via XeGTAO.esh:184-187.
|
||||
// Bit-cast through floatBitsToInt / intBitsToFloat for the GLSL idiom.
|
||||
float XeGTAO_FastSqrt(float x)
|
||||
{
|
||||
return intBitsToFloat(0x1fbd1df5 + (floatBitsToInt(x) >> 1));
|
||||
}
|
||||
|
||||
// Lagarde 2014 approximation, input [-1, 1], output [0, PI]. XeGTAO.esh:189.
|
||||
float XeGTAO_FastACos(float inX)
|
||||
{
|
||||
float x = abs(inX);
|
||||
float res = -0.156583 * x + XE_GTAO_PI_HALF;
|
||||
res *= XeGTAO_FastSqrt(1.0 - x);
|
||||
return (inX >= 0.0) ? res : (XE_GTAO_PI - res);
|
||||
}
|
||||
|
||||
// Clamp depth into fp32-finite range. XeGTAO.esh:648-655.
|
||||
float XeGTAO_ClampDepth(float depth)
|
||||
{
|
||||
return clamp(depth, 0.0, 3.402823466e+38);
|
||||
}
|
||||
|
||||
// Pass-through: our input depth is already positive linear view-Z, so the
|
||||
// "screen-space -> view-space" linearize step is the identity. Kept for
|
||||
// signature parity with XeGTAO.esh:117-123 so ports of MainPass and
|
||||
// ComputeViewspaceNormal stay verbatim.
|
||||
float XeGTAO_ScreenSpaceToViewSpaceDepth(float screenDepth, GTAOConsts consts)
|
||||
{
|
||||
return screenDepth;
|
||||
}
|
||||
|
||||
// XeGTAO.esh:104-115 with the Esoterica reverse-Z patch (negate Z so it
|
||||
// points OUT of the screen in view space). render3d's view space is
|
||||
// also right-handed Y-up with camera looking down -Z, matching Esoterica.
|
||||
vec3 XeGTAO_ComputeViewspacePosition(vec2 screenPos, float viewspaceDepth, GTAOConsts consts) {
|
||||
vec3 ret;
|
||||
ret.xy = (consts.NDCToViewMul * screenPos + consts.NDCToViewAdd) * viewspaceDepth;
|
||||
ret.z = -viewspaceDepth;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// XeGTAO.esh:125-134. Output is in [0,1] per channel, 0 = strong edge,
|
||||
// 1 = no edge. Used by both the edges-output (PackEdges) and the
|
||||
// normal-from-depth pass (CalculateNormal).
|
||||
vec4 XeGTAO_CalculateEdges(float centerZ, float leftZ, float rightZ, float topZ, float bottomZ)
|
||||
{
|
||||
vec4 edgesLRTB = vec4(leftZ, rightZ, topZ, bottomZ) - centerZ;
|
||||
|
||||
float slopeLR = (edgesLRTB.y - edgesLRTB.x) * 0.5;
|
||||
float slopeTB = (edgesLRTB.w - edgesLRTB.z) * 0.5;
|
||||
vec4 edgesLRTBSlopeAdjusted = edgesLRTB + vec4(slopeLR, - slopeLR, slopeTB, - slopeTB);
|
||||
edgesLRTB = min(abs(edgesLRTB), abs(edgesLRTBSlopeAdjusted));
|
||||
return vec4(clamp((1.25 - edgesLRTB / (centerZ * 0.011)), 0.0, 1.0));
|
||||
}
|
||||
|
||||
// XeGTAO.esh:137-146. Packs 4 edge gradients (2 bits each) into a single
|
||||
// UNORM8 channel. The dot-product form matches the reference, rounding
|
||||
// happens before the dot so the four bins are { 0, 64, 128, 192 } / 255.
|
||||
float XeGTAO_PackEdges(vec4 edgesLRTB)
|
||||
{
|
||||
edgesLRTB = round(clamp(edgesLRTB, 0.0, 1.0) * 2.9);
|
||||
return dot(edgesLRTB, vec4(64.0 / 255.0, 16.0 / 255.0, 4.0 / 255.0, 1.0 / 255.0));
|
||||
}
|
||||
|
||||
// XeGTAO.esh:727-737. Reverse of PackEdges. Used by Denoise.
|
||||
vec4 XeGTAO_UnpackEdges(float packed_val)
|
||||
{
|
||||
uint p = uint(packed_val * 255.5);
|
||||
vec4 edgesLRTB;
|
||||
edgesLRTB.x = float((p >> 6)& 0x03u) / 3.0;
|
||||
edgesLRTB.y = float((p >> 4)& 0x03u) / 3.0;
|
||||
edgesLRTB.z = float((p >> 2)& 0x03u) / 3.0;
|
||||
edgesLRTB.w = float((p >> 0)& 0x03u) / 3.0;
|
||||
return clamp(edgesLRTB, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// XeGTAO.esh:148-173 with the Esoterica reverse-Z cross-product reorder
|
||||
// (cross args swapped to keep the winding consistent with view space
|
||||
// pointing OUT of the screen).
|
||||
vec3 XeGTAO_CalculateNormal(vec4 edgesLRTB, vec3 pixCenterPos,
|
||||
vec3 pixLPos, vec3 pixRPos, vec3 pixTPos, vec3 pixBPos)
|
||||
{
|
||||
vec4 acceptedNormals = clamp(vec4(
|
||||
edgesLRTB.x * edgesLRTB.z,
|
||||
edgesLRTB.z * edgesLRTB.y,
|
||||
edgesLRTB.y * edgesLRTB.w,
|
||||
edgesLRTB.w * edgesLRTB.x) + 0.01, 0.0, 1.0);
|
||||
|
||||
pixLPos = normalize(pixLPos - pixCenterPos);
|
||||
pixRPos = normalize(pixRPos - pixCenterPos);
|
||||
pixTPos = normalize(pixTPos - pixCenterPos);
|
||||
pixBPos = normalize(pixBPos - pixCenterPos);
|
||||
|
||||
vec3 pixelNormal = acceptedNormals.x * cross(pixTPos, pixLPos)
|
||||
+ acceptedNormals.y * cross(pixRPos, pixTPos)
|
||||
+ acceptedNormals.z * cross(pixBPos, pixRPos)
|
||||
+ acceptedNormals.w * cross(pixLPos, pixBPos);
|
||||
|
||||
return normalize(pixelNormal);
|
||||
}
|
||||
|
||||
// XeGTAO_Types.esh:120-142. Hilbert curve index for a 64x64 tile
|
||||
// pixCoord (pixCoord & 63). Combined with the R2 sequence in the
|
||||
// consumer's SpatioTemporalNoise to produce blue-noise-like spatial
|
||||
// distribution. No LUT, generated in-shader, matches Esoterica's
|
||||
// `#ifndef XE_GTAO_HILBERT_LUT_AVAILABLE` branch.
|
||||
uint HilbertIndex(uint posX, uint posY)
|
||||
{
|
||||
uint index = 0u;
|
||||
for (uint curLevel = XE_HILBERT_WIDTH / 2u; curLevel > 0u; curLevel /= 2u)
|
||||
{
|
||||
uint regionX = (posX & curLevel) > 0u ? 1u : 0u;
|
||||
uint regionY = (posY & curLevel) > 0u ? 1u : 0u;
|
||||
index += curLevel * curLevel * ((3u * regionX)^ regionY);
|
||||
if (regionY == 0u)
|
||||
{
|
||||
if (regionX == 1u)
|
||||
{
|
||||
posX = (XE_HILBERT_WIDTH - 1u) - posX;
|
||||
posY = (XE_HILBERT_WIDTH - 1u) - posY;
|
||||
}
|
||||
uint temp = posX;
|
||||
posX = posY;
|
||||
posY = temp;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
Loading…
Reference in a new issue