Skip to content

Commit 91ffde5

Browse files
committed
Inline tuning
- Inline most defines, I don't plan on changing them - Fix depth passed to REMOVE_SIGNAL block - Use (blindly copied) optimized function in compositing - Make the noise factor a uniform
1 parent 3f2086e commit 91ffde5

File tree

3 files changed

+69
-74
lines changed

3 files changed

+69
-74
lines changed

common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectCullingGroup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ public void submitTransparent(PipelineCompiler.OitMode oit) {
221221

222222
// Don't need to do this unless the program changes.
223223
drawProgram.bind();
224+
225+
drawProgram.setFloat("_flw_blueNoiseFactor", 0.08f);
224226
}
225227

226228
MaterialRenderState.setupOit(multiDraw.material);

common/src/backend/resources/assets/flywheel/flywheel/internal/common.frag

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3126
layout (binding = 7) uniform sampler2D _flw_depthRange;
3227

3328
layout (binding = 8) uniform sampler2DArray _flw_coefficients;
3429

35-
#define REMOVE_SIGNAL true
36-
3730
#ifdef _FLW_DEPTH_RANGE
3831

3932
layout (location = 0) out vec2 _flw_depthRange_out;
@@ -49,11 +42,11 @@ layout (location = 1) out vec4 _flw_coeffs1;
4942
layout (location = 2) out vec4 _flw_coeffs2;
5043
layout (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
9184
layout (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

218212
float 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

common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/oit_composite.frag

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,60 @@ layout (binding = 1) uniform sampler2D _flw_accumulate;
55

66
#define TRANSPARENCY_WAVELET_RANK 3
77
#define TRANSPARENCY_WAVELET_COEFFICIENT_COUNT 16
8-
#define floatN float
9-
#define all(e) (e)
10-
#define mad fma
11-
#define lerp mix
12-
#define Coefficients_Out vec4[4]
13-
#define Coefficients_In sampler2DArray
148

15-
16-
floatN get_coefficients(in Coefficients_In coefficients, uint index) {
9+
float get_coefficients(in sampler2DArray coefficients, uint index) {
1710
return texelFetch(coefficients, ivec3(gl_FragCoord.xy, index >> 2), 0)[index & 3u];
1811
}
1912

20-
floatN evaluate_wavelet_index(in Coefficients_In coefficients, int index)
13+
float evaluate_wavelets(in sampler2DArray coefficients, float depth)
2114
{
22-
floatN result = 0;
23-
24-
index += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1;
25-
26-
for (int i = 0; i < (TRANSPARENCY_WAVELET_RANK+1); ++i)
27-
{
28-
int power = TRANSPARENCY_WAVELET_RANK - i;
29-
int new_index = (index - 1) >> 1;
30-
floatN coeff = get_coefficients(coefficients, new_index);
31-
int wavelet_sign = ((index & 1) << 1) - 1;
32-
result -= exp2(float(power) * 0.5) * coeff * wavelet_sign;
33-
index = new_index;
34-
}
35-
return result;
36-
}
37-
38-
39-
floatN evaluate_wavelets(in Coefficients_In coefficients, float depth)
40-
{
41-
floatN scale_coefficient = get_coefficients(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1);
42-
if (all(scale_coefficient == 0))
15+
float scale_coefficient = get_coefficients(coefficients, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1);
16+
if (scale_coefficient == 0)
4317
{
4418
return 0;
4519
}
4620

4721
depth *= float(TRANSPARENCY_WAVELET_COEFFICIENT_COUNT-1) / TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
4822

4923
float coefficient_depth = depth * TRANSPARENCY_WAVELET_COEFFICIENT_COUNT;
50-
int index = clamp(int(floor(coefficient_depth)), 0, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1);
24+
int index_b = clamp(int(floor(coefficient_depth)), 0, TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1);
25+
bool sample_a = index_b >= 1;
26+
int index_a = sample_a ? (index_b - 1) : index_b;
27+
28+
index_b += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1;
29+
index_a += TRANSPARENCY_WAVELET_COEFFICIENT_COUNT - 1;
5130

52-
floatN a = 0;
53-
floatN b = scale_coefficient + evaluate_wavelet_index(coefficients, index);
54-
if (index > 0) { a = scale_coefficient + evaluate_wavelet_index(coefficients, index - 1); }
31+
float b = scale_coefficient;
32+
float a = sample_a ? scale_coefficient : 0;
33+
34+
for (int i = 0; i < (TRANSPARENCY_WAVELET_RANK+1); ++i)
35+
{
36+
int power = TRANSPARENCY_WAVELET_RANK - i;
37+
38+
int new_index_b = (index_b - 1) >> 1;
39+
int wavelet_sign_b = ((index_b & 1) << 1) - 1;
40+
float coeff_b = get_coefficients(coefficients, new_index_b);
41+
b -= exp2(float(power) * 0.5) * coeff_b * wavelet_sign_b;
42+
index_b = new_index_b;
43+
44+
if (sample_a)
45+
{
46+
int new_index_a = (index_a - 1) >> 1;
47+
int wavelet_sign_a = ((index_a & 1) << 1) - 1;
48+
float coeff_a = (new_index_a == new_index_b) ? coeff_b : get_coefficients(coefficients, new_index_a);
49+
a -= exp2(float(power) * 0.5) * coeff_a * wavelet_sign_a;
50+
index_a = new_index_a;
51+
}
52+
}
5553

5654
float t = coefficient_depth >= TRANSPARENCY_WAVELET_COEFFICIENT_COUNT ? 1.0 : fract(coefficient_depth);
57-
floatN signal = lerp(a, b, t);// You can experiment here with different types of interpolation as well
58-
return signal;
55+
56+
return mix(a, b, t);
5957
}
6058

61-
floatN evaluate_transmittance_wavelets(in Coefficients_In coefficients, float depth)
59+
float evaluate_transmittance_wavelets(in sampler2DArray coefficients, float depth)
6260
{
63-
floatN absorbance = evaluate_wavelets(coefficients, depth);
61+
float absorbance = evaluate_wavelets(coefficients, depth);
6462
return clamp(exp(-absorbance), 0., 1.);// undoing the transformation from absorbance back to transmittance
6563
}
6664

@@ -73,7 +71,7 @@ void main() {
7371
discard;
7472
}
7573

76-
floatN total_transmittance = evaluate_transmittance_wavelets(_flw_coefficients, infinity);
74+
float total_transmittance = evaluate_transmittance_wavelets(_flw_coefficients, infinity);
7775

7876
frag = vec4(texel.rgb / texel.a, total_transmittance);
7977
}

0 commit comments

Comments
 (0)