#version 460 core #define STAGE_COMPUTE 1 // the clouds of the main view smoothed over frames: the image of this frame (clouds_march.comp, ray started at another // distance every frame) blended into the one of the previous frames, found where the clouds were by the distance of the // clouds, the rotation and the move of the camera. the history is held to the range of the neighbourhood of this frame, // so moving clouds and a turning camera don't leave trails. the pixels the scenery hid (alpha -1 from clouds_march.comp, which // didn't march them) keep what the history holds where they were, as it is: when the scenery uncovers them there's something to // blend into. // with a part of the pixels marched a frame (stride over 1: the ones at phase in the blocks of stride x stride pixels, the others // hold what they were given in their turn, some frames ago and for the view of that frame) the pixels of this frame are blended // in as they are; the others take the pixels of this frame next to them, evened out, with a smaller share, so every pixel gets // something of every frame and the turns of the pixels don't show as a pattern. the history is held to the range of the pixels // of this frame around #define LAYOUT_COMPUTE 1 #include "common.glsl" layout(local_size_x = WEATHER_GROUP_SIZE, local_size_y = WEATHER_GROUP_SIZE, local_size_z = 1) in; layout(set = COMPUTE_SET, binding = 0) uniform sampler2D current_color; layout(set = COMPUTE_SET, binding = 1) uniform sampler2D current_depth; layout(set = COMPUTE_SET, binding = 2) uniform sampler2D history_color; layout(rgba16f, set = COMPUTE_SET, binding = 3) uniform writeonly image2D result; // the parameters of the kernel (push constants) layout(push_constant) uniform kernel_constants { ivec2 size; mat4 inverse_projection; mat4 view_to_world; // world axes to the clip space of the previous frame, the camera of the previous frame at the origin mat4 previous_projection_view; // where the camera was in the previous frame, relative to where it is (world, metres) vec3 previous_camera; // share of this frame in the result; 1 when there is no history float blend; // the pixels marched in this frame: the ones at phase in the blocks of stride x stride pixels (1: all of them) int stride; ivec2 phase; }; void main() { ivec2 texel = ivec2(gl_GlobalInvocationID.xy); if (any(greaterThanEqual(texel, size))) return; vec4 current = texelFetch(current_color, texel, 0); bool hidden = current.a < 0.0; // no clouds, where there's nothing to keep if (hidden) current = vec4(0.0, 0.0, 0.0, 1.0); if (blend >= 1.0) { imageStore(result, texel, current); return; } // the pixels of this frame around: the one the pixel is, or the one before it, and those a stride away on both sides (a // pixel between two of them reaches as far both ways). their range holds the history, and the nearest stands for a pixel of // another frame which has no history // the share of the pixels of this frame next to a pixel of another frame, of the share of a pixel of this frame const float BESIDE_SHARE = 0.35; int spacing = max(stride, 1); ivec2 past = (texel + spacing - phase) % spacing; bool fresh = all(equal(past, ivec2(0))); ivec2 base = texel - past; vec4 low = vec4(1e30); vec4 high = vec4(-1e30); vec4 nearest = vec4(0.0, 0.0, 0.0, 1.0); float nearestdistance = 1e30; bool ranged = false; // the pixels of this frame on both sides of a pixel of another frame, which is half way between them vec4 beside = vec4(0.0); float besidecount = 0.0; if (!hidden) { for (int y = -1; y <= (past.y == 0 ? 1 : 2); y++) for (int x = -1; x <= (past.x == 0 ? 1 : 2); x++) { ivec2 place = base + ivec2(x, y) * spacing; if (any(lessThan(place, ivec2(0))) || any(greaterThanEqual(place, size))) continue; vec4 neighbour = texelFetch(current_color, place, 0); if (neighbour.a < 0.0) continue; low = min(low, neighbour); high = max(high, neighbour); ranged = true; vec2 away = vec2(place - texel); float distance2 = dot(away, away); if (distance2 < nearestdistance) { nearestdistance = distance2; nearest = neighbour; } if (x >= 0 && y >= 0 && x <= min(past.x, 1) && y <= min(past.y, 1)) { beside += neighbour; besidecount += 1.0; } } // what a pixel of another frame holds is of the view of that frame if (!fresh) current = (besidecount > 0.0) ? beside / besidecount : nearest; } vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0; vec4 near = inverse_projection * vec4(ndc, 1.0, 1.0); vec3 direction = normalize(mat3(view_to_world) * normalize(near.xyz / near.w)); float span_distance = texelFetch(current_depth, texel, 0).r; vec3 position = direction * span_distance - previous_camera; vec4 clip = previous_projection_view * vec4(position, 1.0); float share = hidden ? 0.0 : blend * (fresh ? 1.0 : BESIDE_SHARE); vec4 history = current; if (clip.w > 0.0) { vec2 coords = clip.xy / clip.w * 0.5 + 0.5; if (all(greaterThanEqual(coords, vec2(0.0))) && all(lessThanEqual(coords, vec2(1.0)))) { history = textureLod(history_color, coords, 0.0); if (ranged) history = clamp(history, low, high); } else share = 1.0; } else share = 1.0; imageStore(result, texel, mix(history, current, share)); }