summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/43-denoise/fs_denoise_spatial_implementation.sh
blob: e04a91bb9a523612add62553445abfc62dbc1d8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/

#ifndef FS_DENOISE_SPATIAL_IMPLEMENTATION_SH
#define FS_DENOISE_SPATIAL_IMPLEMENTATION_SH

#include "../common/common.sh"
#include "parameters.sh"
#include "normal_encoding.sh"

SAMPLER2D(s_color,  0); // input color, signal to be denoised
SAMPLER2D(s_normal, 1); // scene's gbuffer normal, used for edge stopping function
SAMPLER2D(s_depth,  2); // scene's depth, used for edge stopping function

void main()
{
	vec2 texCoord = v_texcoord0;

	// read center pixel
	vec4 color = texture2D(s_color, texCoord);
	vec3 normal = NormalDecode(texture2D(s_normal, texCoord).xyz); // * 2.0 - 1.0;
	float depth = texture2D(s_depth, texCoord).x;
	// want depth gradient for edge stopping function
	float depthGradient = abs(dFdx(depth)) + abs(dFdy(depth));

	float du = u_texCoordStep * u_viewTexel.x;
	float dv = u_texCoordStep * u_viewTexel.y;

#if USE_SPATIAL_5X5
	float gaussianWeights[5];
	gaussianWeights[0] = 1.0/16.0;
	gaussianWeights[1] = 4.0/16.0;
	gaussianWeights[2] = 6.0/16.0;
	gaussianWeights[3] = 4.0/16.0;
	gaussianWeights[4] = 1.0/16.0;
	float initialWeight = (gaussianWeights[2]*gaussianWeights[2]);
	int centerIdx = 2;

	vec4 accumulateColor = color * initialWeight;
	float accumulateWeight = initialWeight;

	for (int yy = 0; yy < 5; ++yy)
	{
		for (int xx = 0; xx < 5; ++xx)
		{
#else
	float gaussianWeights[3];
	gaussianWeights[0] = 1.0/4.0;
	gaussianWeights[1] = 2.0/4.0;
	gaussianWeights[2] = 1.0/4.0;
	float initialWeight = (gaussianWeights[1]*gaussianWeights[1]);
	int centerIdx = 1;

	vec4 accumulateColor = color * initialWeight;
	float accumulateWeight = initialWeight;

	for (int yy = 0; yy < 3; ++yy)
	{
		for (int xx = 0; xx < 3; ++xx)
		{
#endif // USE_SPATIAL_5X5
			if ((centerIdx == xx) && (centerIdx == yy)) {
				continue;
			}

			float xOffset = float(xx) - float(centerIdx);
			float yOffset = float(yy) - float(centerIdx);
			vec2 sampleTexCoord = texCoord;
			sampleTexCoord.x += xOffset * du;
			sampleTexCoord.y += yOffset * dv;

			vec4 sampleColor = texture2D(s_color, sampleTexCoord);
			vec3 sampleNormal = NormalDecode(texture2D(s_normal, sampleTexCoord).xyz);
			float normalWeight = pow(saturate(dot(normal, sampleNormal)), u_sigmaNormal);
			
			float sampleDepth = texture2D(s_depth, sampleTexCoord).x;
			float depthDelta = depth - sampleDepth;
			float depthWeight = exp(-abs(depthDelta) / max(1e-5, u_sigmaDepth*u_sigmaDepth));

			float weight = depthWeight * normalWeight;

			// apply gaussian
			weight *= (gaussianWeights[xx]*gaussianWeights[yy]);

			accumulateColor += sampleColor * weight;
			accumulateWeight += weight;
		}
	}

	accumulateColor /= max(accumulateWeight, 1e-5);

	gl_FragColor = accumulateColor;
}

#endif // FS_DENOISE_SPATIAL_IMPLEMENTATION_SH