| 1 |
/////////////////////////////////////////////////////////////////////////////////// |
| 2 |
// Copyright(c) 2016-2022 Panos Karabelas |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is furnished |
| 9 |
// to do so, subject to the following conditions : |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
| 17 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 |
/////////////////////////////////////////////////////////////////////////////////// |
| 21 |
// File changes (yyyy-mm-dd) |
| 22 |
// 2025-11-05: Jakub Brzyski: Added dynamic variance, base variance value adjusted to reduce ghosting |
| 23 |
// 2022-05-06: Panos Karabelas: first commit |
| 24 |
// 2020-12-05: Joan Fons: convert to Vulkan and Godot |
| 25 |
/////////////////////////////////////////////////////////////////////////////////// |
| 26 |
|
| 27 |
#[compute] |
| 28 |
|
| 29 |
#version 450 |
| 30 |
|
| 31 |
#VERSION_DEFINES |
| 32 |
|
| 33 |
// Based on Spartan Engine's TAA implementation (without TAA upscale). |
| 34 |
// <https://github.com/PanosK92/SpartanEngine/blob/a8338d0609b85dc32f3732a5c27fb4463816a3b9/Data/shaders/temporal_antialiasing.hlsl> |
| 35 |
|
| 36 |
#define GROUP_SIZE 8 |
| 37 |
#define FLT_MIN 0.00000001 |
| 38 |
#define FLT_MAX 32767.0 |
| 39 |
#define RPC_9 0.11111111111 |
| 40 |
#define RPC_16 0.0625 |
| 41 |
|
| 42 |
#define DISOCCLUSION_SCALE 0.01 // Scale the weight of this pixel calculated as (change in velocity - threshold) * scale. |
| 43 |
|
| 44 |
layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = 1) in; |
| 45 |
|
| 46 |
layout(rgba16f, set = 0, binding = 0) uniform restrict readonly image2D color_buffer; |
| 47 |
layout(set = 0, binding = 1) uniform sampler2D depth_buffer; |
| 48 |
layout(rg16f, set = 0, binding = 2) uniform restrict readonly image2D velocity_buffer; |
| 49 |
layout(rg16f, set = 0, binding = 3) uniform restrict readonly image2D last_velocity_buffer; |
| 50 |
layout(set = 0, binding = 4) uniform sampler2D history_buffer; |
| 51 |
layout(rgba16f, set = 0, binding = 5) uniform restrict writeonly image2D output_buffer; |
| 52 |
|
| 53 |
layout(push_constant, std430) uniform Params { |
| 54 |
vec2 resolution; |
| 55 |
float disocclusion_threshold; // 0.1 / max(params.resolution.x, params.resolution.y) |
| 56 |
float variance_dynamic; |
| 57 |
} |
| 58 |
params; |
| 59 |
|
| 60 |
const ivec2 kOffsets3x3[9] = { |
| 61 |
ivec2(-1, -1), |
| 62 |
ivec2(0, -1), |
| 63 |
ivec2(1, -1), |
| 64 |
ivec2(-1, 0), |
| 65 |
ivec2(0, 0), |
| 66 |
ivec2(1, 0), |
| 67 |
ivec2(-1, 1), |
| 68 |
ivec2(0, 1), |
| 69 |
ivec2(1, 1), |
| 70 |
}; |
| 71 |
|
| 72 |
/*------------------------------------------------------------------------------ |
| 73 |
THREAD GROUP SHARED MEMORY (LDS) |
| 74 |
------------------------------------------------------------------------------*/ |
| 75 |
|
| 76 |
const int kBorderSize = 1; |
| 77 |
const int kGroupSize = GROUP_SIZE; |
| 78 |
const int kTileDimension = kGroupSize + kBorderSize * 2; |
| 79 |
const int kTileDimension2 = kTileDimension * kTileDimension; |
| 80 |
|
| 81 |
vec3 reinhard(vec3 hdr) { |
| 82 |
return hdr / (hdr + 1.0); |
| 83 |
} |
| 84 |
vec3 reinhard_inverse(vec3 sdr) { |
| 85 |
return sdr / (1.0 - sdr); |
| 86 |
} |
| 87 |
|
| 88 |
float get_depth(ivec2 thread_id) { |
| 89 |
return texelFetch(depth_buffer, thread_id, 0).r; |
| 90 |
} |
| 91 |
|
| 92 |
shared vec3 tile_color[kTileDimension][kTileDimension]; |
| 93 |
shared float tile_depth[kTileDimension][kTileDimension]; |
| 94 |
|
| 95 |
vec3 load_color(uvec2 group_thread_id) { |
| 96 |
group_thread_id += kBorderSize; |
| 97 |
return tile_color[group_thread_id.x][group_thread_id.y]; |
| 98 |
} |
| 99 |
|
| 100 |
void store_color(uvec2 group_thread_id, vec3 color) { |
| 101 |
tile_color[group_thread_id.x][group_thread_id.y] = color; |
| 102 |
} |
| 103 |
|
| 104 |
float load_depth(uvec2 group_thread_id) { |
| 105 |
group_thread_id += kBorderSize; |
| 106 |
return tile_depth[group_thread_id.x][group_thread_id.y]; |
| 107 |
} |
| 108 |
|
| 109 |
void store_depth(uvec2 group_thread_id, float depth) { |
| 110 |
tile_depth[group_thread_id.x][group_thread_id.y] = depth; |
| 111 |
} |
| 112 |
|
| 113 |
void store_color_depth(uvec2 group_thread_id, ivec2 thread_id) { |
| 114 |
// out of bounds clamp |
| 115 |
thread_id = clamp(thread_id, ivec2(0, 0), ivec2(params.resolution) - ivec2(1, 1)); |
| 116 |
|
| 117 |
store_color(group_thread_id, imageLoad(color_buffer, thread_id).rgb); |
| 118 |
store_depth(group_thread_id, get_depth(thread_id)); |
| 119 |
} |
| 120 |
|
| 121 |
void populate_group_shared_memory(uvec2 group_id, uint group_index) { |
| 122 |
// Populate group shared memory |
| 123 |
ivec2 group_top_left = ivec2(group_id) * kGroupSize - kBorderSize; |
| 124 |
if (group_index < (kTileDimension2 >> 2)) { |
| 125 |
ivec2 group_thread_id_1 = ivec2(group_index % kTileDimension, group_index / kTileDimension); |
| 126 |
ivec2 group_thread_id_2 = ivec2((group_index + (kTileDimension2 >> 2)) % kTileDimension, (group_index + (kTileDimension2 >> 2)) / kTileDimension); |
| 127 |
ivec2 group_thread_id_3 = ivec2((group_index + (kTileDimension2 >> 1)) % kTileDimension, (group_index + (kTileDimension2 >> 1)) / kTileDimension); |
| 128 |
ivec2 group_thread_id_4 = ivec2((group_index + kTileDimension2 * 3 / 4) % kTileDimension, (group_index + kTileDimension2 * 3 / 4) / kTileDimension); |
| 129 |
|
| 130 |
store_color_depth(group_thread_id_1, group_top_left + group_thread_id_1); |
| 131 |
store_color_depth(group_thread_id_2, group_top_left + group_thread_id_2); |
| 132 |
store_color_depth(group_thread_id_3, group_top_left + group_thread_id_3); |
| 133 |
store_color_depth(group_thread_id_4, group_top_left + group_thread_id_4); |
| 134 |
} |
| 135 |
|
| 136 |
// Wait for group threads to load store data. |
| 137 |
groupMemoryBarrier(); |
| 138 |
barrier(); |
| 139 |
} |
| 140 |
|
| 141 |
/*------------------------------------------------------------------------------ |
| 142 |
VELOCITY |
| 143 |
------------------------------------------------------------------------------*/ |
| 144 |
|
| 145 |
void depth_test_min(uvec2 pos, inout float min_depth, inout uvec2 min_pos) { |
| 146 |
float depth = load_depth(pos); |
| 147 |
|
| 148 |
if (depth < min_depth) { |
| 149 |
min_depth = depth; |
| 150 |
min_pos = pos; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Returns velocity with closest depth (3x3 neighborhood) |
| 155 |
void get_closest_pixel_velocity_3x3(in uvec2 group_pos, uvec2 group_top_left, out vec2 velocity) { |
| 156 |
float min_depth = 1.0; |
| 157 |
uvec2 min_pos = group_pos; |
| 158 |
|
| 159 |
depth_test_min(group_pos + kOffsets3x3[0], min_depth, min_pos); |
| 160 |
depth_test_min(group_pos + kOffsets3x3[1], min_depth, min_pos); |
| 161 |
depth_test_min(group_pos + kOffsets3x3[2], min_depth, min_pos); |
| 162 |
depth_test_min(group_pos + kOffsets3x3[3], min_depth, min_pos); |
| 163 |
depth_test_min(group_pos + kOffsets3x3[4], min_depth, min_pos); |
| 164 |
depth_test_min(group_pos + kOffsets3x3[5], min_depth, min_pos); |
| 165 |
depth_test_min(group_pos + kOffsets3x3[6], min_depth, min_pos); |
| 166 |
depth_test_min(group_pos + kOffsets3x3[7], min_depth, min_pos); |
| 167 |
depth_test_min(group_pos + kOffsets3x3[8], min_depth, min_pos); |
| 168 |
|
| 169 |
// Velocity out |
| 170 |
velocity = imageLoad(velocity_buffer, ivec2(group_top_left + min_pos)).xy; |
| 171 |
} |
| 172 |
|
| 173 |
/*------------------------------------------------------------------------------ |
| 174 |
HISTORY SAMPLING |
| 175 |
------------------------------------------------------------------------------*/ |
| 176 |
|
| 177 |
vec3 sample_catmull_rom_9(sampler2D stex, vec2 uv, vec2 resolution) { |
| 178 |
// Source: https://gist.github.com/TheRealMJP/c83b8c0f46b63f3a88a5986f4fa982b1 |
| 179 |
// License: https://gist.github.com/TheRealMJP/bc503b0b87b643d3505d41eab8b332ae |
| 180 |
|
| 181 |
// We're going to sample a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding |
| 182 |
// down the sample location to get the exact center of our "starting" texel. The starting texel will be at |
| 183 |
// location [1, 1] in the grid, where [0, 0] is the top left corner. |
| 184 |
vec2 sample_pos = uv * resolution; |
| 185 |
vec2 texPos1 = floor(sample_pos - 0.5f) + 0.5f; |
| 186 |
|
| 187 |
// Compute the fractional offset from our starting texel to our original sample location, which we'll |
| 188 |
// feed into the Catmull-Rom spline function to get our filter weights. |
| 189 |
vec2 f = sample_pos - texPos1; |
| 190 |
|
| 191 |
// Compute the Catmull-Rom weights using the fractional offset that we calculated earlier. |
| 192 |
// These equations are pre-expanded based on our knowledge of where the texels will be located, |
| 193 |
// which lets us avoid having to evaluate a piece-wise function. |
| 194 |
vec2 w0 = f * (-0.5f + f * (1.0f - 0.5f * f)); |
| 195 |
vec2 w1 = 1.0f + f * f * (-2.5f + 1.5f * f); |
| 196 |
vec2 w2 = f * (0.5f + f * (2.0f - 1.5f * f)); |
| 197 |
vec2 w3 = f * f * (-0.5f + 0.5f * f); |
| 198 |
|
| 199 |
// Work out weighting factors and sampling offsets that will let us use bilinear filtering to |
| 200 |
// simultaneously evaluate the middle 2 samples from the 4x4 grid. |
| 201 |
vec2 w12 = w1 + w2; |
| 202 |
vec2 offset12 = w2 / (w1 + w2); |
| 203 |
|
| 204 |
// Compute the final UV coordinates we'll use for sampling the texture |
| 205 |
vec2 texPos0 = texPos1 - 1.0f; |
| 206 |
vec2 texPos3 = texPos1 + 2.0f; |
| 207 |
vec2 texPos12 = texPos1 + offset12; |
| 208 |
|
| 209 |
texPos0 /= resolution; |
| 210 |
texPos3 /= resolution; |
| 211 |
texPos12 /= resolution; |
| 212 |
|
| 213 |
vec3 result = vec3(0.0f, 0.0f, 0.0f); |
| 214 |
|
| 215 |
result += textureLod(stex, vec2(texPos0.x, texPos0.y), 0.0).xyz * w0.x * w0.y; |
| 216 |
result += textureLod(stex, vec2(texPos12.x, texPos0.y), 0.0).xyz * w12.x * w0.y; |
| 217 |
result += textureLod(stex, vec2(texPos3.x, texPos0.y), 0.0).xyz * w3.x * w0.y; |
| 218 |
|
| 219 |
result += textureLod(stex, vec2(texPos0.x, texPos12.y), 0.0).xyz * w0.x * w12.y; |
| 220 |
result += textureLod(stex, vec2(texPos12.x, texPos12.y), 0.0).xyz * w12.x * w12.y; |
| 221 |
result += textureLod(stex, vec2(texPos3.x, texPos12.y), 0.0).xyz * w3.x * w12.y; |
| 222 |
|
| 223 |
result += textureLod(stex, vec2(texPos0.x, texPos3.y), 0.0).xyz * w0.x * w3.y; |
| 224 |
result += textureLod(stex, vec2(texPos12.x, texPos3.y), 0.0).xyz * w12.x * w3.y; |
| 225 |
result += textureLod(stex, vec2(texPos3.x, texPos3.y), 0.0).xyz * w3.x * w3.y; |
| 226 |
|
| 227 |
return max(result, 0.0f); |
| 228 |
} |
| 229 |
|
| 230 |
/*------------------------------------------------------------------------------ |
| 231 |
HISTORY CLIPPING |
| 232 |
------------------------------------------------------------------------------*/ |
| 233 |
|
| 234 |
// Based on "Temporal Reprojection Anti-Aliasing" - https://github.com/playdeadgames/temporal |
| 235 |
vec3 clip_aabb(vec3 aabb_min, vec3 aabb_max, vec3 p, vec3 q) { |
| 236 |
vec3 r = q - p; |
| 237 |
vec3 rmax = (aabb_max - p.xyz); |
| 238 |
vec3 rmin = (aabb_min - p.xyz); |
| 239 |
|
| 240 |
if (r.x > rmax.x + FLT_MIN) { |
| 241 |
r *= (rmax.x / r.x); |
| 242 |
} |
| 243 |
if (r.y > rmax.y + FLT_MIN) { |
| 244 |
r *= (rmax.y / r.y); |
| 245 |
} |
| 246 |
if (r.z > rmax.z + FLT_MIN) { |
| 247 |
r *= (rmax.z / r.z); |
| 248 |
} |
| 249 |
|
| 250 |
if (r.x < rmin.x - FLT_MIN) { |
| 251 |
r *= (rmin.x / r.x); |
| 252 |
} |
| 253 |
if (r.y < rmin.y - FLT_MIN) { |
| 254 |
r *= (rmin.y / r.y); |
| 255 |
} |
| 256 |
if (r.z < rmin.z - FLT_MIN) { |
| 257 |
r *= (rmin.z / r.z); |
| 258 |
} |
| 259 |
|
| 260 |
return p + r; |
| 261 |
} |
| 262 |
|
| 263 |
// Clip history to the neighbourhood of the current sample |
| 264 |
vec3 clip_history_3x3(uvec2 group_pos, vec3 color_history, vec2 velocity_closest) { |
| 265 |
// Sample a 3x3 neighbourhood |
| 266 |
vec3 s1 = load_color(group_pos + kOffsets3x3[0]); |
| 267 |
vec3 s2 = load_color(group_pos + kOffsets3x3[1]); |
| 268 |
vec3 s3 = load_color(group_pos + kOffsets3x3[2]); |
| 269 |
vec3 s4 = load_color(group_pos + kOffsets3x3[3]); |
| 270 |
vec3 s5 = load_color(group_pos + kOffsets3x3[4]); |
| 271 |
vec3 s6 = load_color(group_pos + kOffsets3x3[5]); |
| 272 |
vec3 s7 = load_color(group_pos + kOffsets3x3[6]); |
| 273 |
vec3 s8 = load_color(group_pos + kOffsets3x3[7]); |
| 274 |
vec3 s9 = load_color(group_pos + kOffsets3x3[8]); |
| 275 |
|
| 276 |
// Compute min and max (with an adaptive box size, which greatly reduces ghosting) |
| 277 |
vec3 color_avg = (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9) * RPC_9; |
| 278 |
vec3 color_avg2 = ((s1 * s1) + (s2 * s2) + (s3 * s3) + (s4 * s4) + (s5 * s5) + (s6 * s6) + (s7 * s7) + (s8 * s8) + (s9 * s9)) * RPC_9; |
| 279 |
// Use variance clipping as described in https://developer.download.nvidia.com/gameworks/events/GDC2016/msalvi_temporal_supersampling.pdf |
| 280 |
float box_size = mix(0.0f, params.variance_dynamic, smoothstep(0.02f, 0.0f, length(velocity_closest))); |
| 281 |
vec3 dev = sqrt(abs(color_avg2 - (color_avg * color_avg))) * box_size; |
| 282 |
vec3 color_min = color_avg - dev; |
| 283 |
vec3 color_max = color_avg + dev; |
| 284 |
|
| 285 |
// Variance clipping |
| 286 |
vec3 color = clip_aabb(color_min, color_max, clamp(color_avg, color_min, color_max), color_history); |
| 287 |
|
| 288 |
// Clamp to prevent NaNs |
| 289 |
color = clamp(color, FLT_MIN, FLT_MAX); |
| 290 |
|
| 291 |
return color; |
| 292 |
} |
| 293 |
|
| 294 |
/*------------------------------------------------------------------------------ |
| 295 |
TAA |
| 296 |
------------------------------------------------------------------------------*/ |
| 297 |
|
| 298 |
const vec3 lumCoeff = vec3(0.299f, 0.587f, 0.114f); |
| 299 |
|
| 300 |
float luminance(vec3 color) { |
| 301 |
return max(dot(color, lumCoeff), 0.0001f); |
| 302 |
} |
| 303 |
|
| 304 |
// This is "velocity disocclusion" as described by https://www.elopezr.com/temporal-aa-and-the-quest-for-the-holy-trail/. |
| 305 |
// We use texel space, so our scale and threshold differ. |
| 306 |
float get_factor_disocclusion(vec2 uv_reprojected, vec2 velocity) { |
| 307 |
vec2 velocity_previous = imageLoad(last_velocity_buffer, ivec2(uv_reprojected * params.resolution)).xy; |
| 308 |
vec2 velocity_texels = velocity * params.resolution; |
| 309 |
vec2 prev_velocity_texels = velocity_previous * params.resolution; |
| 310 |
float disocclusion = length(prev_velocity_texels - velocity_texels) - params.disocclusion_threshold; |
| 311 |
return clamp(disocclusion * DISOCCLUSION_SCALE, 0.0, 1.0); |
| 312 |
} |
| 313 |
|
| 314 |
vec3 temporal_antialiasing(uvec2 pos_group_top_left, uvec2 pos_group, uvec2 pos_screen, vec2 uv, sampler2D tex_history) { |
| 315 |
// Get the velocity of the current pixel |
| 316 |
vec2 velocity = imageLoad(velocity_buffer, ivec2(pos_screen)).xy; |
| 317 |
|
| 318 |
// Get reprojected uv |
| 319 |
vec2 uv_reprojected = uv + velocity; |
| 320 |
|
| 321 |
// Get input color |
| 322 |
vec3 color_input = load_color(pos_group); |
| 323 |
|
| 324 |
// Get history color (catmull-rom reduces a lot of the blurring that you get under motion) |
| 325 |
vec3 color_history = sample_catmull_rom_9(tex_history, uv_reprojected, params.resolution).rgb; |
| 326 |
|
| 327 |
// Clip history to the neighbourhood of the current sample (fixes a lot of the ghosting). |
| 328 |
vec2 velocity_closest = vec2(0.0); // This is best done by using the velocity with the closest depth. |
| 329 |
get_closest_pixel_velocity_3x3(pos_group, pos_group_top_left, velocity_closest); |
| 330 |
color_history = clip_history_3x3(pos_group, color_history, velocity_closest); |
| 331 |
|
| 332 |
// Compute blend factor |
| 333 |
float blend_factor = RPC_16; // We want to be able to accumulate as many jitter samples as we generated, that is, 16. |
| 334 |
{ |
| 335 |
// If re-projected UV is out of screen, converge to current color immediately. |
| 336 |
float factor_screen = any(lessThan(uv_reprojected, vec2(0.0))) || any(greaterThan(uv_reprojected, vec2(1.0))) ? 1.0 : 0.0; |
| 337 |
|
| 338 |
// Increase blend factor when there is disocclusion (fixes a lot of the remaining ghosting). |
| 339 |
float factor_disocclusion = get_factor_disocclusion(uv_reprojected, velocity); |
| 340 |
|
| 341 |
// Add to the blend factor |
| 342 |
blend_factor = clamp(blend_factor + factor_screen + factor_disocclusion, 0.0, 1.0); |
| 343 |
} |
| 344 |
|
| 345 |
// Resolve |
| 346 |
vec3 color_resolved = vec3(0.0); |
| 347 |
{ |
| 348 |
// Tonemap |
| 349 |
color_history = reinhard(color_history); |
| 350 |
color_input = reinhard(color_input); |
| 351 |
|
| 352 |
// Reduce flickering |
| 353 |
float lum_color = luminance(color_input); |
| 354 |
float lum_history = luminance(color_history); |
| 355 |
float diff = abs(lum_color - lum_history) / max(lum_color, max(lum_history, 1.001)); |
| 356 |
diff = 1.0 - diff; |
| 357 |
diff = diff * diff; |
| 358 |
blend_factor = mix(0.0, blend_factor, diff); |
| 359 |
|
| 360 |
// Lerp/blend |
| 361 |
color_resolved = mix(color_history, color_input, blend_factor); |
| 362 |
|
| 363 |
// Inverse tonemap |
| 364 |
color_resolved = reinhard_inverse(color_resolved); |
| 365 |
} |
| 366 |
|
| 367 |
return color_resolved; |
| 368 |
} |
| 369 |
|
| 370 |
void main() { |
| 371 |
populate_group_shared_memory(gl_WorkGroupID.xy, gl_LocalInvocationIndex); |
| 372 |
|
| 373 |
// Out of bounds check |
| 374 |
if (any(greaterThanEqual(vec2(gl_GlobalInvocationID.xy), params.resolution))) { |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
const uvec2 pos_group = gl_LocalInvocationID.xy; |
| 379 |
const uvec2 pos_group_top_left = gl_WorkGroupID.xy * kGroupSize - kBorderSize; |
| 380 |
const uvec2 pos_screen = gl_GlobalInvocationID.xy; |
| 381 |
const vec2 uv = (gl_GlobalInvocationID.xy + 0.5f) / params.resolution; |
| 382 |
|
| 383 |
vec3 result = temporal_antialiasing(pos_group_top_left, pos_group, pos_screen, uv, history_buffer); |
| 384 |
imageStore(output_buffer, ivec2(gl_GlobalInvocationID.xy), vec4(result, 1.0)); |
| 385 |
} |