xash3d-fwgs/ref_vk/shaders/denoiser.comp
2021-11-09 10:59:36 -08:00

82 lines
2.3 KiB
Plaintext

#version 460
#include "noise.glsl"
layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
layout(set = 0, binding = 0, rgba16f) uniform image2D dest;
layout(set = 0, binding = 1, rgba8) uniform image2D src_base_color;
layout(set = 0, binding = 2, rgba16f) uniform image2D src_diffuse_gi;
layout(set = 0, binding = 3, rgba16f) uniform image2D src_specular;
layout(set = 0, binding = 4, rgba16f) uniform image2D src_additive;
// Blatantly copypasted from https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color){
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
void main() {
ivec2 res = ivec2(imageSize(src_base_color));
ivec2 pix = ivec2(gl_GlobalInvocationID);
if (any(greaterThanEqual(pix, res))) {
return;
}
vec3 colour = vec3(0.);
const vec4 base_color = imageLoad(src_base_color, pix);
const float material_index = imageLoad(src_diffuse_gi, pix).a;
//imageStore(dest, pix, vec4(aces_tonemap(base_color.rgb), 0.)); return;
//imageStore(dest, pix, vec4(aces_tonemap(imageLoad(src_diffuse_gi, pix).rgb), 0.)); return;
//imageStore(dest, pix, vec4(aces_tonemap(imageLoad(src_specular, pix).rgb), 0.)); return;
// const uint mi = uint(material_index);
// imageStore(dest, pix, vec4(rand3_f01(uvec3(mi,mi+1,mi+2)), 0.));
// return;
const int KERNEL_SIZE = 8;
float scale = 0.;
for (int x = -KERNEL_SIZE; x <= KERNEL_SIZE; ++x)
for (int y = -KERNEL_SIZE; y <= KERNEL_SIZE; ++y) {
const ivec2 p = pix + ivec2(x, y);
if (any(greaterThanEqual(p, res)) || any(lessThan(p, ivec2(0)))) {
continue;
}
const vec4 c = imageLoad(src_diffuse_gi, p);
if (c.a == material_index) {
colour += imageLoad(src_diffuse_gi, p).rgb;
scale += 1.;
}
}
if (scale > 0.) {
colour /= scale;
colour *= base_color.rgb;
}
colour += imageLoad(src_specular, pix).rgb;
colour += imageLoad(src_additive, pix).rgb;
colour = aces_tonemap(colour);
imageStore(dest, pix, vec4(colour, 0.));
//imageStore(dest, pix, imageLoad(src_diffuse_gi, pix));
}