Skip to content

Commit 8407d20

Browse files
committed
Texture this
- Add blue noise texture
1 parent 91ffde5 commit 8407d20

File tree

8 files changed

+97
-76
lines changed

8 files changed

+97
-76
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dev.engine_room.flywheel.backend;
2+
3+
import java.io.IOException;
4+
5+
import org.jetbrains.annotations.UnknownNullability;
6+
7+
import com.mojang.blaze3d.platform.NativeImage;
8+
9+
import dev.engine_room.flywheel.api.Flywheel;
10+
import net.minecraft.client.renderer.texture.DynamicTexture;
11+
import net.minecraft.resources.ResourceLocation;
12+
import net.minecraft.server.packs.resources.ResourceManager;
13+
14+
public class NoiseTextures {
15+
16+
public static final ResourceLocation NOISE_TEXTURE = Flywheel.rl("textures/flywheel/noise/blue/0.png");
17+
public static final int NOISE_LAYERS = 16;
18+
19+
@UnknownNullability
20+
public static DynamicTexture BLUE_NOISE;
21+
22+
23+
public static void reload(ResourceManager manager) {
24+
if (BLUE_NOISE != null) {
25+
BLUE_NOISE.close();
26+
BLUE_NOISE = null;
27+
}
28+
var optional = manager.getResource(NOISE_TEXTURE);
29+
30+
if (optional.isEmpty()) {
31+
return;
32+
}
33+
34+
try (var is = optional.get()
35+
.open()) {
36+
var image = NativeImage.read(NativeImage.Format.LUMINANCE, is);
37+
38+
BLUE_NOISE = new DynamicTexture(image);
39+
} catch (IOException e) {
40+
41+
}
42+
}
43+
}

common/src/backend/java/dev/engine_room/flywheel/backend/Samplers.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ public class Samplers {
1313

1414
public static final GlTextureUnit DEPTH_RANGE = GlTextureUnit.T7;
1515
public static final GlTextureUnit COEFFICIENTS = GlTextureUnit.T8;
16+
public static final GlTextureUnit NOISE = GlTextureUnit.T9;
1617
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void submitTransparent(PipelineCompiler.OitMode oit) {
222222
// Don't need to do this unless the program changes.
223223
drawProgram.bind();
224224

225-
drawProgram.setFloat("_flw_blueNoiseFactor", 0.08f);
225+
drawProgram.setFloat("_flw_blueNoiseFactor", 0.07f);
226226
}
227227

228228
MaterialRenderState.setupOit(multiDraw.material);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mojang.blaze3d.platform.GlStateManager;
77
import com.mojang.blaze3d.systems.RenderSystem;
88

9+
import dev.engine_room.flywheel.backend.NoiseTextures;
910
import dev.engine_room.flywheel.backend.Samplers;
1011
import dev.engine_room.flywheel.backend.compile.IndirectPrograms;
1112
import dev.engine_room.flywheel.backend.gl.GlTextureUnit;
@@ -63,6 +64,14 @@ public void renderTransmittance() {
6364
Samplers.DEPTH_RANGE.makeActive();
6465
GlStateManager._bindTexture(depthBounds);
6566

67+
Samplers.NOISE.makeActive();
68+
NoiseTextures.BLUE_NOISE.bind();
69+
70+
NoiseTextures.BLUE_NOISE.setFilter(true, false);
71+
GL46.glTextureParameteri(NoiseTextures.BLUE_NOISE.getId(), GL32.GL_TEXTURE_WRAP_S, GL32.GL_REPEAT);
72+
GL46.glTextureParameteri(NoiseTextures.BLUE_NOISE.getId(), GL32.GL_TEXTURE_WRAP_T, GL32.GL_REPEAT);
73+
74+
6675
GL46.glNamedFramebufferDrawBuffers(fbo, new int[]{GL46.GL_COLOR_ATTACHMENT1, GL46.GL_COLOR_ATTACHMENT2, GL46.GL_COLOR_ATTACHMENT3, GL46.GL_COLOR_ATTACHMENT4});
6776

6877
GL46.glClearNamedFramebufferfv(fbo, GL46.GL_COLOR, 0, new float[]{0, 0, 0, 0});
@@ -85,9 +94,11 @@ public void shade() {
8594

8695
Samplers.COEFFICIENTS.makeActive();
8796
GlStateManager._bindTexture(0);
88-
8997
GL46.glBindTextureUnit(Samplers.COEFFICIENTS.number, coefficients);
9098

99+
Samplers.NOISE.makeActive();
100+
NoiseTextures.BLUE_NOISE.bind();
101+
91102
GL46.glNamedFramebufferDrawBuffers(fbo, new int[]{GL46.GL_COLOR_ATTACHMENT5});
92103

93104
GL46.glClearNamedFramebufferfv(fbo, GL46.GL_COLOR, 0, new float[]{0, 0, 0, 0});

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

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,42 @@ layout (binding = 7) uniform sampler2D _flw_depthRange;
2727

2828
layout (binding = 8) uniform sampler2DArray _flw_coefficients;
2929

30+
layout (binding = 9) uniform sampler2D _flw_blueNoise;
31+
32+
33+
uniform float _flw_blueNoiseFactor = 0.08;
34+
35+
float tented_blue_noise(float normalizedDepth) {
36+
37+
float tentIn = abs(normalizedDepth * 2. - 1);
38+
float tentIn2 = tentIn * tentIn;
39+
float tentIn4 = tentIn2 * tentIn2;
40+
float tent = 1 - (tentIn2 * tentIn4);
41+
42+
float b = texture(_flw_blueNoise, gl_FragCoord.xy / vec2(64)).r;
43+
44+
return b * tent;
45+
}
46+
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+
52+
float linear_depth() {
53+
return linearize_depth(gl_FragCoord.z, _flw_cullData.znear, _flw_cullData.zfar);
54+
}
55+
56+
float depth() {
57+
float linearDepth = linear_depth();
58+
59+
vec2 depthRange = texelFetch(_flw_depthRange, ivec2(gl_FragCoord.xy), 0).rg;
60+
float delta = depthRange.x + depthRange.y;
61+
float depth = (linearDepth + depthRange.x) / delta;
62+
63+
return depth - tented_blue_noise(depth) * _flw_blueNoiseFactor;
64+
}
65+
3066
#ifdef _FLW_DEPTH_RANGE
3167

3268
layout (location = 0) out vec2 _flw_depthRange_out;
@@ -155,80 +191,6 @@ float evaluate_transmittance_wavelets(in sampler2DArray coefficients, float dept
155191

156192
#endif
157193

158-
// TODO: blue noise texture
159-
uint HilbertIndex(uvec2 p) {
160-
uint i = 0u;
161-
for (uint l = 0x4000u; l > 0u; l >>= 1u) {
162-
uvec2 r = min(p & l, 1u);
163-
164-
i = (i << 2u) | ((r.x * 3u) ^ r.y);
165-
p = r.y == 0u ? (0x7FFFu * r.x) ^ p.yx : p;
166-
}
167-
return i;
168-
}
169-
170-
uint ReverseBits(uint x) {
171-
x = ((x & 0xaaaaaaaau) >> 1) | ((x & 0x55555555u) << 1);
172-
x = ((x & 0xccccccccu) >> 2) | ((x & 0x33333333u) << 2);
173-
x = ((x & 0xf0f0f0f0u) >> 4) | ((x & 0x0f0f0f0fu) << 4);
174-
x = ((x & 0xff00ff00u) >> 8) | ((x & 0x00ff00ffu) << 8);
175-
return (x >> 16) | (x << 16);
176-
}
177-
178-
// from: https://psychopath.io/post/2021_01_30_building_a_better_lk_hash
179-
uint OwenHash(uint x, uint seed) { // seed is any random number
180-
x ^= x * 0x3d20adeau;
181-
x += seed;
182-
x *= (seed >> 16) | 1u;
183-
x ^= x * 0x05526c56u;
184-
x ^= x * 0x53a22864u;
185-
return x;
186-
}
187-
188-
// https://www.shadertoy.com/view/ssBBW1
189-
float blue() {
190-
uint m = HilbertIndex(uvec2(gl_FragCoord.xy));// map pixel coords to hilbert curve index
191-
m = OwenHash(ReverseBits(m), 0xe7843fbfu);// owen-scramble hilbert index
192-
m = OwenHash(ReverseBits(m), 0x8d8fb1e0u);// map hilbert index to sobol sequence and owen-scramble
193-
float mask = float(ReverseBits(m)) / 4294967296.0;// convert to float
194-
195-
return mask;
196-
}
197-
198-
uniform float _flw_blueNoiseFactor = 0.08;
199-
200-
float tented_blue_noise(float normalizedDepth) {
201-
202-
float tentIn = abs(normalizedDepth * 2. - 1);
203-
float tentIn2 = tentIn * tentIn;
204-
float tentIn4 = tentIn2 * tentIn2;
205-
float tent = 1 - (tentIn2 * tentIn4);
206-
207-
float b = blue();
208-
209-
return b * tent;
210-
}
211-
212-
float linearize_depth(float d, float zNear, float zFar) {
213-
float z_n = 2.0 * d - 1.0;
214-
return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
215-
}
216-
217-
float linear_depth() {
218-
return linearize_depth(gl_FragCoord.z, _flw_cullData.znear, _flw_cullData.zfar);
219-
}
220-
221-
float depth() {
222-
float linearDepth = linear_depth();
223-
224-
vec2 depthRange = texelFetch(_flw_depthRange, ivec2(gl_FragCoord.xy), 0).rg;
225-
float delta = depthRange.x + depthRange.y;
226-
float depth = (linearDepth + depthRange.x) / delta;
227-
228-
return depth - tented_blue_noise(depth) * _flw_blueNoiseFactor;
229-
}
230-
231-
232194
#else
233195

234196
out vec4 _flw_outputColor;
6.95 KB
Loading

fabric/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwProgramsReloader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.engine_room.flywheel.backend.compile;
22

33
import dev.engine_room.flywheel.api.Flywheel;
4+
import dev.engine_room.flywheel.backend.NoiseTextures;
45
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
56
import net.minecraft.resources.ResourceLocation;
67
import net.minecraft.server.packs.resources.ResourceManager;
@@ -16,6 +17,7 @@ private FlwProgramsReloader() {
1617
@Override
1718
public void onResourceManagerReload(ResourceManager manager) {
1819
FlwPrograms.reload(manager);
20+
NoiseTextures.reload(manager);
1921
}
2022

2123
@Override

forge/src/backend/java/dev/engine_room/flywheel/backend/compile/FlwProgramsReloader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.engine_room.flywheel.backend.compile;
22

3+
import dev.engine_room.flywheel.backend.NoiseTextures;
34
import net.minecraft.server.packs.resources.ResourceManager;
45
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
56

@@ -12,5 +13,6 @@ private FlwProgramsReloader() {
1213
@Override
1314
public void onResourceManagerReload(ResourceManager manager) {
1415
FlwPrograms.reload(manager);
16+
NoiseTextures.reload(manager);
1517
}
1618
}

0 commit comments

Comments
 (0)