11#include "flywheel: internal/ packed_material.glsl"
22#include "flywheel: internal/ diffuse.glsl"
33#include "flywheel: internal/ colorizer.glsl"
4+ #include "flywheel: internal/ wavelet.glsl"
5+ #include "flywheel: internal/ depth.glsl"
46
57// optimize discard usage
68#if defined(GL_ARB_conservative_depth) && defined(_FLW_USE_DISCARD)
@@ -19,10 +21,6 @@ flat in uvec2 _flw_ids;
1921
2022#ifdef _FLW_OIT
2123
22- #define TRANSPARENCY_WAVELET_RANK 3
23- #define TRANSPARENCY_WAVELET_COEFFICIENT_COUNT 16
24- #define REMOVE_SIGNAL true
25-
2624layout (binding = 7 ) uniform sampler2D _flw_depthRange;
2725
2826layout (binding = 8 ) uniform sampler2DArray _flw_coefficients;
@@ -44,11 +42,6 @@ float tented_blue_noise(float normalizedDepth) {
4442 return b * tent;
4543}
4644
47- float linearize_depth(float d, float zNear, float zFar) {
48- float z_n = 2.0 * d - 1.0 ;
49- return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
50- }
51-
5245float linear_depth() {
5346 return linearize_depth(gl_FragCoord .z, _flw_cullData.znear, _flw_cullData.zfar);
5447}
@@ -69,126 +62,19 @@ layout (location = 0) out vec2 _flw_depthRange_out;
6962
7063#endif
7164
72-
7365#ifdef _FLW_COLLECT_COEFFS
7466
75-
7667layout (location = 0 ) out vec4 _flw_coeffs0;
7768layout (location = 1 ) out vec4 _flw_coeffs1;
7869layout (location = 2 ) out vec4 _flw_coeffs2;
7970layout (location = 3 ) out vec4 _flw_coeffs3;
8071
81- void add_to_index(inout vec4 [4 ] coefficients, uint index, float addend) {
82- coefficients[index >> 2 ][index & 3u] = addend;
83- }
84-
85- void add_event_to_wavelets(inout vec4 [4 ] coefficients, float signal, float depth)
86- {
87- depth *= float (TRANSPARENCY_WAVELET_COEFFICIENT_COUNT- 1 ) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
88-
89- int index = clamp (int (floor (depth * TRANSPARENCY_WAVELET_COEFFICIENT_COUNT)), 0 , TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
90- index += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 ;
91-
92- for (int i = 0 ; i < (TRANSPARENCY_WAVELET_RANK+ 1 ); ++ i)
93- {
94- int power = TRANSPARENCY_WAVELET_RANK - i;
95- int new_index = (index - 1 ) >> 1 ;
96- float k = float ((new_index + 1 ) & ((1 << power) - 1 ));
97-
98- int wavelet_sign = ((index & 1 ) << 1 ) - 1 ;
99- float wavelet_phase = ((index + 1 ) & 1 ) * exp2 (- power);
100- float addend = fma(fma(- exp2 (- power), k, depth), wavelet_sign, wavelet_phase) * exp2 (power * 0.5 ) * signal;
101- add_to_index(coefficients, new_index, addend);
102-
103- index = new_index;
104- }
105-
106- float addend = fma(signal, - depth, signal);
107- add_to_index(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 , addend);
108- }
109-
110- void add_transmittance_event_to_wavelets(inout vec4 [4 ] coefficients, float transmittance, float depth)
111- {
112- float absorbance = - log (max (transmittance, 0.00001 ));// transforming the signal from multiplicative transmittance to additive absorbance
113- add_event_to_wavelets(coefficients, absorbance, depth);
114- }
115-
11672#endif
11773
11874#ifdef _FLW_EVALUATE
11975
12076layout (location = 0 ) out vec4 _flw_accumulate;
12177
122-
123- float get_coefficients(in sampler2DArray coefficients, uint index) {
124- return texelFetch(coefficients, ivec3 (gl_FragCoord .xy, index >> 2 ), 0 )[index & 3u];
125- }
126-
127- float evaluate_wavelets(in sampler2DArray coefficients, float depth, float signal)
128- {
129- float scale_coefficient = get_coefficients(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
130- if (scale_coefficient == 0 )
131- {
132- return 0 ;
133- }
134-
135- depth *= float (TRANSPARENCY_WAVELET_COEFFICIENT_COUNT- 1 ) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
136-
137- if (REMOVE_SIGNAL)
138- {
139- float scale_coefficient_addend = fma(signal, - depth, signal);
140- scale_coefficient -= scale_coefficient_addend;
141- }
142-
143- float coefficient_depth = depth * TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
144- int index_b = clamp (int (floor (coefficient_depth)), 0 , TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 );
145- bool sample_a = index_b >= 1 ;
146- int index_a = sample_a ? (index_b - 1 ) : index_b;
147-
148- index_b += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 ;
149- index_a += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1 ;
150-
151- float b = scale_coefficient;
152- float a = sample_a ? scale_coefficient : 0 ;
153-
154- for (int i = 0 ; i < (TRANSPARENCY_WAVELET_RANK+ 1 ); ++ i)
155- {
156- int power = TRANSPARENCY_WAVELET_RANK - i;
157-
158- int new_index_b = (index_b - 1 ) >> 1 ;
159- int wavelet_sign_b = ((index_b & 1 ) << 1 ) - 1 ;
160- float coeff_b = get_coefficients(coefficients, new_index_b);
161- if (REMOVE_SIGNAL)
162- {
163- float wavelet_phase_b = ((index_b + 1 ) & 1 ) * exp2 (- power);
164- float k = float ((new_index_b + 1 ) & ((1 << power) - 1 ));
165- float addend = fma(fma(- exp2 (- power), k, depth), wavelet_sign_b, wavelet_phase_b) * exp2 (power * 0.5 ) * signal;
166- coeff_b -= addend;
167- }
168- b -= exp2 (float (power) * 0.5 ) * coeff_b * wavelet_sign_b;
169- index_b = new_index_b;
170-
171- if (sample_a)
172- {
173- int new_index_a = (index_a - 1 ) >> 1 ;
174- int wavelet_sign_a = ((index_a & 1 ) << 1 ) - 1 ;
175- 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
176- a -= exp2 (float (power) * 0.5 ) * coeff_a * wavelet_sign_a;
177- index_a = new_index_a;
178- }
179- }
180-
181- float t = coefficient_depth >= TRANSPARENCY_WAVELET_COEFFICIENT_COUNT ? 1.0 : fract (coefficient_depth);
182-
183- return mix (a, b, t);
184- }
185-
186- float evaluate_transmittance_wavelets(in sampler2DArray coefficients, float depth, float signal)
187- {
188- float absorbance = evaluate_wavelets(coefficients, depth, signal);
189- return clamp (exp (- absorbance), 0 ., 1 .);// undoing the transformation from absorbance back to transmittance
190- }
191-
19278#endif
19379
19480#else
@@ -298,7 +184,7 @@ void _flw_main() {
298184 result[2 ] = vec4 (0 .);
299185 result[3 ] = vec4 (0 .);
300186
301- add_transmittance_event_to_wavelets (result, 1 . - color.a, depth());
187+ add_transmittance (result, 1 . - color.a, depth());
302188
303189 _flw_coeffs0 = result[0 ];
304190 _flw_coeffs1 = result[1 ];
@@ -309,7 +195,7 @@ void _flw_main() {
309195
310196 #ifdef _FLW_EVALUATE
311197
312- float transmittance = evaluate_transmittance_wavelets (_flw_coefficients, depth(), 1 . - color.a);
198+ float transmittance = signal_corrected_transmittance (_flw_coefficients, depth(), 1 . - color.a);
313199
314200 _flw_accumulate = vec4 (color.rgb * color.a, color.a) * transmittance;
315201
0 commit comments