Skip to content

Commit e930f60

Browse files
authored
Blur module optimizations (#5812)
1 parent b05fe6c commit e930f60

File tree

6 files changed

+207
-66
lines changed

6 files changed

+207
-66
lines changed

src/main/java/meteordevelopment/meteorclient/mixin/RenderSystemMixin.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import com.mojang.blaze3d.systems.RenderSystem;
99
import meteordevelopment.meteorclient.renderer.MeshUniforms;
10-
import meteordevelopment.meteorclient.systems.modules.render.Blur;
1110
import meteordevelopment.meteorclient.utils.render.postprocess.ChamsShader;
1211
import meteordevelopment.meteorclient.utils.render.postprocess.OutlineUniforms;
1312
import meteordevelopment.meteorclient.utils.render.postprocess.PostProcessShader;
@@ -21,7 +20,6 @@ public abstract class RenderSystemMixin {
2120
@Inject(method = "flipFrame", at = @At("TAIL"))
2221
private static void meteor$flipFrame(CallbackInfo info) {
2322
MeshUniforms.flipFrame();
24-
Blur.flipFrame();
2523
PostProcessShader.flipFrame();
2624
ChamsShader.flipFrame();
2725
OutlineUniforms.flipFrame();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.renderer;
7+
8+
import com.mojang.blaze3d.buffers.GpuBuffer;
9+
import com.mojang.blaze3d.buffers.GpuBufferSlice;
10+
import com.mojang.blaze3d.systems.GpuDevice;
11+
import com.mojang.blaze3d.systems.RenderSystem;
12+
import net.minecraft.client.gl.DynamicUniformStorage;
13+
import net.minecraft.client.gl.MappableRingBuffer;
14+
import net.minecraft.util.math.MathHelper;
15+
16+
import java.nio.ByteBuffer;
17+
18+
/**
19+
* UBO storage with a constant size. Exceeding this size causes an {@link IndexOutOfBoundsException} to be thrown.
20+
*
21+
* @see DynamicUniformStorage
22+
* @author Crosby
23+
*/
24+
public class FixedUniformStorage<T extends DynamicUniformStorage.Uploadable> {
25+
private final MappableRingBuffer buffer;
26+
private final int blockSize;
27+
private final int capacity;
28+
private int size;
29+
30+
public FixedUniformStorage(String name, int blockSize, int capacity) {
31+
GpuDevice gpuDevice = RenderSystem.getDevice();
32+
this.blockSize = MathHelper.roundUpToMultiple(blockSize, gpuDevice.getUniformOffsetAlignment());
33+
this.capacity = capacity;
34+
int alignedCapacity = MathHelper.smallestEncompassingPowerOfTwo(capacity);
35+
this.size = 0;
36+
this.buffer = new MappableRingBuffer(() -> name + " x" + this.blockSize, 130, this.blockSize * alignedCapacity);
37+
}
38+
39+
public GpuBufferSlice write(T value) {
40+
if (this.size >= this.capacity) {
41+
throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", this.size, this.capacity));
42+
} else {
43+
int i = this.size * this.blockSize;
44+
GpuBufferSlice slice = this.buffer.getBlocking().slice(i, this.blockSize);
45+
46+
try (GpuBuffer.MappedView mappedView = RenderSystem.getDevice()
47+
.createCommandEncoder()
48+
.mapBuffer(slice, false, true)) {
49+
value.write(mappedView.data());
50+
}
51+
52+
this.size++;
53+
return slice;
54+
}
55+
}
56+
57+
public GpuBufferSlice[] writeAll(T[] values) {
58+
if (values.length == 0) {
59+
return new GpuBufferSlice[0];
60+
} else if (this.size + values.length > this.capacity) {
61+
throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", this.size + values.length - 1, this.capacity));
62+
} else {
63+
int i = this.size * this.blockSize;
64+
GpuBufferSlice[] gpuBufferSlices = new GpuBufferSlice[values.length];
65+
GpuBuffer ubo = this.buffer.getBlocking();
66+
67+
try (GpuBuffer.MappedView mappedView = RenderSystem.getDevice()
68+
.createCommandEncoder()
69+
.mapBuffer(ubo.slice(i, values.length * this.blockSize), false, true)) {
70+
ByteBuffer byteBuffer = mappedView.data();
71+
72+
for (int j = 0; j < values.length; j++) {
73+
T uploadable = values[j];
74+
gpuBufferSlices[j] = ubo.slice(i + j * this.blockSize, this.blockSize);
75+
byteBuffer.position(j * this.blockSize);
76+
uploadable.write(byteBuffer);
77+
}
78+
}
79+
80+
this.size += values.length;
81+
return gpuBufferSlices;
82+
}
83+
}
84+
85+
public void clear() {
86+
this.size = 0;
87+
this.buffer.rotate();
88+
}
89+
90+
public void close() {
91+
this.buffer.close();
92+
}
93+
}

src/main/java/meteordevelopment/meteorclient/renderer/FullScreenRenderer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55

66
package meteordevelopment.meteorclient.renderer;
77

8+
import com.mojang.blaze3d.buffers.GpuBuffer;
89
import com.mojang.blaze3d.vertex.VertexFormat;
910
import meteordevelopment.meteorclient.utils.PreInit;
1011

1112
public class FullScreenRenderer {
13+
public static GpuBuffer vbo;
14+
public static GpuBuffer ibo;
15+
16+
/**
17+
* Deprecated for performance reasons, use {@link MeshRenderer#fullscreen()} or the {@link FullScreenRenderer#vbo}
18+
* and {@link FullScreenRenderer#ibo} buffer objects instead.
19+
*/
20+
@Deprecated(forRemoval = true)
1221
public static MeshBuilder mesh;
1322

1423
private FullScreenRenderer() {}
@@ -18,7 +27,6 @@ public static void init() {
1827
mesh = new MeshBuilder(MeteorVertexFormats.POS2, VertexFormat.DrawMode.TRIANGLES, 4, 6);
1928

2029
mesh.begin();
21-
mesh.ensureQuadCapacity();
2230

2331
mesh.quad(
2432
mesh.vec2(-1, -1).next(),
@@ -28,5 +36,8 @@ public static void init() {
2836
);
2937

3038
mesh.end();
39+
40+
vbo = mesh.getVertexBuffer();
41+
ibo = mesh.getIndexBuffer();
3142
}
3243
}

src/main/java/meteordevelopment/meteorclient/renderer/MeshRenderer.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.minecraft.client.util.math.MatrixStack;
2020
import net.minecraft.util.math.ColorHelper;
2121
import net.minecraft.util.math.Vec3d;
22+
import org.jetbrains.annotations.Nullable;
2223
import org.joml.Matrix4f;
2324

2425
import java.util.HashMap;
@@ -36,7 +37,9 @@ public class MeshRenderer {
3637
private GpuTextureView depthAttachment;
3738
private Color clearColor;
3839
private RenderPipeline pipeline;
39-
private MeshBuilder mesh;
40+
private @Nullable MeshBuilder mesh;
41+
private @Nullable GpuBuffer vertexBuffer;
42+
private @Nullable GpuBuffer indexBuffer;
4043
private Matrix4f matrix;
4144
private final HashMap<String, GpuBufferSlice> uniforms = new HashMap<>();
4245
private final HashMap<String, GpuTextureView> samplers = new HashMap<>();
@@ -73,23 +76,41 @@ public MeshRenderer pipeline(RenderPipeline pipeline) {
7376
return this;
7477
}
7578

79+
public MeshRenderer mesh(GpuBuffer vertices, GpuBuffer indices) {
80+
this.vertexBuffer = vertices;
81+
this.indexBuffer = indices;
82+
return this;
83+
}
84+
7685
public MeshRenderer mesh(MeshBuilder mesh) {
7786
this.mesh = mesh;
7887
return this;
7988
}
8089

8190
public MeshRenderer mesh(MeshBuilder mesh, Matrix4f matrix) {
8291
this.mesh = mesh;
83-
this.matrix = matrix;
84-
return this;
92+
return this.transform(matrix);
8593
}
8694

8795
public MeshRenderer mesh(MeshBuilder mesh, MatrixStack matrices) {
8896
this.mesh = mesh;
97+
return this.transform(matrices);
98+
}
99+
100+
public MeshRenderer transform(Matrix4f matrix) {
101+
this.matrix = matrix;
102+
return this;
103+
}
104+
105+
public MeshRenderer transform(MatrixStack matrices) {
89106
this.matrix = matrices.peek().getPositionMatrix();
90107
return this;
91108
}
92109

110+
public MeshRenderer fullscreen() {
111+
return this.mesh(FullScreenRenderer.vbo, FullScreenRenderer.ibo);
112+
}
113+
93114
public MeshRenderer uniform(String name, GpuBufferSlice slice) {
94115
uniforms.put(name, slice);
95116
return this;
@@ -104,11 +125,15 @@ public MeshRenderer sampler(String name, GpuTextureView view) {
104125
}
105126

106127
public void end() {
107-
if (mesh.isBuilding()) {
128+
if (mesh != null && mesh.isBuilding()) {
108129
mesh.end();
109130
}
110131

111-
if (mesh.getIndicesCount() > 0) {
132+
int indexCount = mesh != null ? mesh.getIndicesCount()
133+
: (indexBuffer != null ? indexBuffer.size() / Integer.BYTES : -1);
134+
135+
if (indexCount > 0) {
136+
112137
if (Utils.rendering3D || matrix != null) {
113138
RenderSystem.getModelViewStack().pushMatrix();
114139
}
@@ -121,8 +146,8 @@ public void end() {
121146
applyCameraPos();
122147
}
123148

124-
GpuBuffer vertexBuffer = mesh.getVertexBuffer();
125-
GpuBuffer indexBuffer = mesh.getIndexBuffer();
149+
GpuBuffer vertexBuffer = mesh != null ? mesh.getVertexBuffer() : this.vertexBuffer;
150+
GpuBuffer indexBuffer = mesh != null ? mesh.getIndexBuffer() : this.indexBuffer;
126151

127152
{
128153
OptionalInt clearColor = this.clearColor != null ?
@@ -148,7 +173,7 @@ public void end() {
148173

149174
pass.setVertexBuffer(0, vertexBuffer);
150175
pass.setIndexBuffer(indexBuffer, VertexFormat.IndexType.INT);
151-
pass.drawIndexed(0, 0, mesh.getIndicesCount(), 1);
176+
pass.drawIndexed(0, 0, indexCount, 1);
152177

153178
pass.close();
154179
}
@@ -163,6 +188,8 @@ public void end() {
163188
clearColor = null;
164189
pipeline = null;
165190
mesh = null;
191+
vertexBuffer = null;
192+
indexBuffer = null;
166193
matrix = null;
167194
uniforms.clear();
168195
samplers.clear();

0 commit comments

Comments
 (0)