bgfx/examples/43-denoise/fs_denoise_deferred_combine.sc

62 lines
1.5 KiB
Python
Raw Normal View History

add denoise example (#2344) /* * Implement SVGF style denoising as bgfx example. Goal is to explore various * options and parameters, not produce an optimized, efficient denoiser. * * Starts with deferred rendering scene with very basic lighting. Lighting is * masked out with a noise pattern to provide something to denoise. There are * two options for the noise pattern. One is a fixed 2x2 dither pattern to * stand-in for lighting at quarter resolution. The other is the common * shadertoy random pattern as a stand-in for some fancier lighting without * enough samples per pixel, like ray tracing. * * First a temporal denoising filter is applied. The temporal filter is only * using normals to reject previous samples. The SVGF paper also describes using * depth comparison to reject samples but that is not implemented here. * * Followed by some number of spatial filters. These are implemented like in the * SVGF paper. As an alternative to the 5x5 Edge-Avoiding A-Trous filter, can * select a 3x3 filter instead. The 3x3 filter takes fewer samples and covers a * smaller area, but takes less time to compute. From a loosely eyeballed * comparison, N 5x5 passes looks similar to N+1 3x3 passes. The wider spatial * filters take a fair chunk of time to compute. I wonder if it would be a good * idea to interleave the input texture before computing, after the first pass * which skips zero pixels. * * I have not implemetened the variance guided part. * * There's also an optional TXAA pass to be applied after. I am not happy with * its implementation yet, so it defaults to off here. */ /* * References: * Spatiotemporal Variance-Guided Filtering: Real-Time Reconstruction for * Path-Traced Global Illumination. by Christoph Schied and more. * - SVGF denoising algorithm * * Streaming G-Buffer Compression for Multi-Sample Anti-Aliasing. * by E. Kerzner and M. Salvi. * - details about history comparison for temporal denoising filter * * Edge-Avoiding À-Trous Wavelet Transform for Fast Global Illumination * Filtering. by Holger Dammertz and more. * - details about a-trous algorithm for spatial denoising filter */
2021-01-02 21:42:02 +03:00
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"
SAMPLER2D(s_color, 0);
SAMPLER2D(s_normal, 1);
float ShadertoyNoise (vec2 uv) {
return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
int ModHelper (float a, float b)
{
return int( a - (b*floor(a/b)));
}
void main()
{
vec2 texCoord = v_texcoord0;
// mess with result so there's something to denosie
float sn = 1.0;
if (1.5 < u_noiseType)
{
sn = ShadertoyNoise(gl_FragCoord.xy + vec2(314.0, 159.0)*u_frameIdx);
sn = (sn < 0.5) ? 0.0 : 1.0;
}
else if (0.5 < u_noiseType)
{
// having trouble compiling for gles when using % or mod :(
int modCoordX = ModHelper(gl_FragCoord.x, 2.0);
int modCoordY = ModHelper(gl_FragCoord.y, 2.0);
int frameSelect = modCoordY * 2 + modCoordX;
int frameMod4 = ModHelper(u_frameIdx, 4.0);
sn = (frameSelect == frameMod4) ? 1.0 : 0.0;
}
vec4 normalRoughness = texture2D(s_normal, texCoord).xyzw;
vec3 normal = NormalDecode(normalRoughness.xyz);
float roughness = 0.5;
// need to get a valid view vector for any microfacet stuff :(
float gloss = 1.0-roughness;
float specPower = 1022.0 * gloss + 2.0;
vec3 light = normalize(vec3(-0.2, 1.0, -0.2));
float NdotL = saturate(dot(normal, light));
float diff = NdotL*0.99 + 0.01;
float spec = 5.0 * pow(NdotL, specPower);
float lightAmt = (diff + spec) * sn;
gl_FragColor = vec4(vec3_splat(lightAmt), 1.0);
}