@@ -21,19 +21,12 @@ flat in uvec2 _flw_ids;
2121
2222#define TRANSPARENCY_WAVELET_RANK 3
2323#define TRANSPARENCY_WAVELET_COEFFICIENT_COUNT 16
24- #define floatN float
25- #define all (e) (e)
26- #define mad fma
27- #define lerp mix
28- #define Coefficients_Out vec4 [4 ]
29- #define Coefficients_In sampler2DArray
24+ #define REMOVE_SIGNAL true
3025
3126layout (binding = 7 ) uniform sampler2D _flw_depthRange;
3227
3328layout (binding = 8 ) uniform sampler2DArray _flw_coefficients;
3429
35- #define REMOVE_SIGNAL true
36-
3730#ifdef _FLW_DEPTH_RANGE
3831
3932layout (location = 0 ) out vec2 _flw_depthRange_out;
@@ -49,11 +42,11 @@ layout (location = 1) out vec4 _flw_coeffs1;
4942layout (location = 2 ) out vec4 _flw_coeffs2;
5043layout (location = 3 ) out vec4 _flw_coeffs3;
5144
52- void add_to_index(inout Coefficients_Out coefficients, uint index, floatN addend) {
45+ void add_to_index(inout vec4 [ 4 ] coefficients, uint index, float addend) {
5346 coefficients[index >> 2 ][index & 3u] = addend;
5447}
5548
56- void add_event_to_wavelets(inout Coefficients_Out coefficients, floatN signal, float depth)
49+ void add_event_to_wavelets(inout vec4 [ 4 ] coefficients, float signal, float depth)
5750{
5851 depth *= float (TRANSPARENCY_WAVELET_COEFFICIENT_COUNT- 1 ) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
5952
@@ -68,17 +61,17 @@ void add_event_to_wavelets(inout Coefficients_Out coefficients, floatN signal, f
6861
6962 int wavelet_sign = ((index & 1 ) << 1 ) - 1 ;
7063 float wavelet_phase = ((index + 1 ) & 1 ) * exp2 (- power);
71- floatN addend = mad(mad (- exp2 (- power), k, depth), wavelet_sign, wavelet_phase) * exp2 (power * 0.5 ) * signal;
64+ float addend = fma(fma (- exp2 (- power), k, depth), wavelet_sign, wavelet_phase) * exp2 (power * 0.5 ) * signal;
7265 add_to_index(coefficients, new_index, addend);
7366
7467 index = new_index;
7568 }
7669
77- floatN addend = mad (signal, - depth, signal);
70+ float addend = fma (signal, - depth, signal);
7871 add_to_index(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 , addend);
7972}
8073
81- void add_transmittance_event_to_wavelets(inout Coefficients_Out coefficients, floatN transmittance, float depth)
74+ void add_transmittance_event_to_wavelets(inout vec4 [ 4 ] coefficients, float transmittance, float depth)
8275{
8376 float absorbance = - log (max (transmittance, 0.00001 ));// transforming the signal from multiplicative transmittance to additive absorbance
8477 add_event_to_wavelets(coefficients, absorbance, depth);
@@ -91,25 +84,26 @@ void add_transmittance_event_to_wavelets(inout Coefficients_Out coefficients, fl
9184layout (location = 0 ) out vec4 _flw_accumulate;
9285
9386
94- floatN get_coefficients(in Coefficients_In coefficients, uint index) {
87+ float get_coefficients(in sampler2DArray coefficients, uint index) {
9588 return texelFetch(coefficients, ivec3 (gl_FragCoord .xy, index >> 2 ), 0 )[index & 3u];
9689}
9790
98- floatN evaluate_wavelets(in Coefficients_In coefficients, float depth, floatN signal)
91+ float evaluate_wavelets(in sampler2DArray coefficients, float depth, float signal)
9992{
100- floatN scale_coefficient = get_coefficients(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
101- if (all ( scale_coefficient == 0 ) )
93+ float scale_coefficient = get_coefficients(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
94+ if (scale_coefficient == 0 )
10295 {
10396 return 0 ;
10497 }
98+
99+ depth *= float (TRANSPARENCY_WAVELET_COEFFICIENT_COUNT- 1 ) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
100+
105101 if (REMOVE_SIGNAL)
106102 {
107- floatN scale_coefficient_addend = mad (signal, - depth, signal);
103+ float scale_coefficient_addend = fma (signal, - depth, signal);
108104 scale_coefficient -= scale_coefficient_addend;
109105 }
110106
111- depth *= float (TRANSPARENCY_WAVELET_COEFFICIENT_COUNT- 1 ) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
112-
113107 float coefficient_depth = depth * TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
114108 int index_b = clamp (int (floor (coefficient_depth)), 0 , TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
115109 bool sample_a = index_b >= 1 ;
@@ -118,21 +112,21 @@ floatN get_coefficients(in Coefficients_In coefficients, uint index) {
118112 index_b += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 ;
119113 index_a += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 ;
120114
121- floatN b = scale_coefficient;
122- floatN a = sample_a ? scale_coefficient : 0 ;
115+ float b = scale_coefficient;
116+ float a = sample_a ? scale_coefficient : 0 ;
123117
124118 for (int i = 0 ; i < (TRANSPARENCY_WAVELET_RANK+ 1 ); ++ i)
125119 {
126120 int power = TRANSPARENCY_WAVELET_RANK - i;
127121
128122 int new_index_b = (index_b - 1 ) >> 1 ;
129123 int wavelet_sign_b = ((index_b & 1 ) << 1 ) - 1 ;
130- floatN coeff_b = get_coefficients(coefficients, new_index_b);
124+ float coeff_b = get_coefficients(coefficients, new_index_b);
131125 if (REMOVE_SIGNAL)
132126 {
133127 float wavelet_phase_b = ((index_b + 1 ) & 1 ) * exp2 (- power);
134128 float k = float ((new_index_b + 1 ) & ((1 << power) - 1 ));
135- floatN addend = mad(mad (- exp2 (- power), k, depth), wavelet_sign_b, wavelet_phase_b) * exp2 (power * 0.5 ) * signal;
129+ float addend = fma(fma (- exp2 (- power), k, depth), wavelet_sign_b, wavelet_phase_b) * exp2 (power * 0.5 ) * signal;
136130 coeff_b -= addend;
137131 }
138132 b -= exp2 (float (power) * 0.5 ) * coeff_b * wavelet_sign_b;
@@ -142,20 +136,20 @@ floatN a = sample_a ? scale_coefficient : 0;
142136 {
143137 int new_index_a = (index_a - 1 ) >> 1 ;
144138 int wavelet_sign_a = ((index_a & 1 ) << 1 ) - 1 ;
145- floatN coeff_a = (new_index_a == new_index_b) ? coeff_b : get_coefficients(coefficients, new_index_a);// No addend here on purpose, the original signal didn't contribute to this coefficient
139+ float coeff_a = (new_index_a == new_index_b) ? coeff_b : get_coefficients(coefficients, new_index_a);// No addend here on purpose, the original signal didn't contribute to this coefficient
146140 a -= exp2 (float (power) * 0.5 ) * coeff_a * wavelet_sign_a;
147141 index_a = new_index_a;
148142 }
149143 }
150144
151145 float t = coefficient_depth >= TRANSPARENCY_WAVELET_COEFFICIENT_COUNT ? 1.0 : fract (coefficient_depth);
152146
153- return lerp (a, b, t);
147+ return mix (a, b, t);
154148}
155149
156- floatN evaluate_transmittance_wavelets(in Coefficients_In coefficients, float depth, floatN signal)
150+ float evaluate_transmittance_wavelets(in sampler2DArray coefficients, float depth, float signal)
157151{
158- floatN absorbance = evaluate_wavelets(coefficients, depth, signal);
152+ float absorbance = evaluate_wavelets(coefficients, depth, signal);
159153 return clamp (exp (- absorbance), 0 ., 1 .);// undoing the transformation from absorbance back to transmittance
160154}
161155
@@ -201,9 +195,9 @@ float blue() {
201195 return mask;
202196}
203197
204- uniform vec3 _flw_depthAdjust ;
198+ uniform float _flw_blueNoiseFactor = 0.08 ;
205199
206- float adjust_depth (float normalizedDepth) {
200+ float tented_blue_noise (float normalizedDepth) {
207201
208202 float tentIn = abs (normalizedDepth * 2 . - 1 );
209203 float tentIn2 = tentIn * tentIn;
@@ -212,7 +206,7 @@ float adjust_depth(float normalizedDepth) {
212206
213207 float b = blue();
214208
215- return normalizedDepth - b * tent * 0.08 ;
209+ return b * tent;
216210}
217211
218212float linearize_depth(float d, float zNear, float zFar) {
@@ -228,9 +222,10 @@ float depth() {
228222 float linearDepth = linear_depth();
229223
230224 vec2 depthRange = texelFetch(_flw_depthRange, ivec2 (gl_FragCoord .xy), 0 ).rg;
231- float depth = (linearDepth + depthRange.x) / (depthRange.x + depthRange.y);
225+ float delta = depthRange.x + depthRange.y;
226+ float depth = (linearDepth + depthRange.x) / delta;
232227
233- return adjust_depth (depth);
228+ return depth - tented_blue_noise (depth) * _flw_blueNoiseFactor ;
234229}
235230
236231
@@ -335,7 +330,7 @@ void _flw_main() {
335330
336331 #ifdef _FLW_COLLECT_COEFFS
337332
338- Coefficients_Out result;
333+ vec4 [ 4 ] result;
339334 result[0 ] = vec4 (0 .);
340335 result[1 ] = vec4 (0 .);
341336 result[2 ] = vec4 (0 .);
@@ -352,7 +347,7 @@ void _flw_main() {
352347
353348 #ifdef _FLW_EVALUATE
354349
355- floatN transmittance = evaluate_transmittance_wavelets(_flw_coefficients, depth(), 1 . - color.a);
350+ float transmittance = evaluate_transmittance_wavelets(_flw_coefficients, depth(), 1 . - color.a);
356351
357352 _flw_accumulate = vec4 (color.rgb * color.a, color.a) * transmittance;
358353
0 commit comments