Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.mojang.blaze3d.systems.RenderSystem;
import meteordevelopment.meteorclient.renderer.MeshUniforms;
import meteordevelopment.meteorclient.systems.modules.render.Blur;
import meteordevelopment.meteorclient.utils.render.postprocess.ChamsShader;
import meteordevelopment.meteorclient.utils.render.postprocess.OutlineUniforms;
import meteordevelopment.meteorclient.utils.render.postprocess.PostProcessShader;
Expand All @@ -21,7 +20,6 @@ public abstract class RenderSystemMixin {
@Inject(method = "flipFrame", at = @At("TAIL"))
private static void meteor$flipFrame(CallbackInfo info) {
MeshUniforms.flipFrame();
Blur.flipFrame();
PostProcessShader.flipFrame();
ChamsShader.flipFrame();
OutlineUniforms.flipFrame();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.renderer;

import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.buffers.GpuBufferSlice;
import com.mojang.blaze3d.systems.GpuDevice;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gl.DynamicUniformStorage;
import net.minecraft.client.gl.MappableRingBuffer;
import net.minecraft.util.math.MathHelper;

import java.nio.ByteBuffer;

/**
* UBO storage with a constant size. Exceeding this size causes an {@link IndexOutOfBoundsException} to be thrown.
*
* @see DynamicUniformStorage
* @author Crosby
*/
public class FixedUniformStorage<T extends DynamicUniformStorage.Uploadable> {
private final MappableRingBuffer buffer;
private final int blockSize;
private final int capacity;
private int size;

public FixedUniformStorage(String name, int blockSize, int capacity) {
GpuDevice gpuDevice = RenderSystem.getDevice();
this.blockSize = MathHelper.roundUpToMultiple(blockSize, gpuDevice.getUniformOffsetAlignment());
this.capacity = capacity;
int alignedCapacity = MathHelper.smallestEncompassingPowerOfTwo(capacity);
this.size = 0;
this.buffer = new MappableRingBuffer(() -> name + " x" + this.blockSize, 130, this.blockSize * alignedCapacity);
}

public GpuBufferSlice write(T value) {
if (this.size >= this.capacity) {
throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", this.size, this.capacity));
} else {
int i = this.size * this.blockSize;
GpuBufferSlice slice = this.buffer.getBlocking().slice(i, this.blockSize);

try (GpuBuffer.MappedView mappedView = RenderSystem.getDevice()
.createCommandEncoder()
.mapBuffer(slice, false, true)) {
value.write(mappedView.data());
}

this.size++;
return slice;
}
}

public GpuBufferSlice[] writeAll(T[] values) {
if (values.length == 0) {
return new GpuBufferSlice[0];
} else if (this.size + values.length > this.capacity) {
throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", this.size + values.length - 1, this.capacity));
} else {
int i = this.size * this.blockSize;
GpuBufferSlice[] gpuBufferSlices = new GpuBufferSlice[values.length];
GpuBuffer ubo = this.buffer.getBlocking();

try (GpuBuffer.MappedView mappedView = RenderSystem.getDevice()
.createCommandEncoder()
.mapBuffer(ubo.slice(i, values.length * this.blockSize), false, true)) {
ByteBuffer byteBuffer = mappedView.data();

for (int j = 0; j < values.length; j++) {
T uploadable = values[j];
gpuBufferSlices[j] = ubo.slice(i + j * this.blockSize, this.blockSize);
byteBuffer.position(j * this.blockSize);
uploadable.write(byteBuffer);
}
}

this.size += values.length;
return gpuBufferSlices;
}
}

public void clear() {
this.size = 0;
this.buffer.rotate();
}

public void close() {
this.buffer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@

package meteordevelopment.meteorclient.renderer;

import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.vertex.VertexFormat;
import meteordevelopment.meteorclient.utils.PreInit;

public class FullScreenRenderer {
public static GpuBuffer vbo;
public static GpuBuffer ibo;

/**
* Deprecated for performance reasons, use {@link MeshRenderer#fullscreen()} or the {@link FullScreenRenderer#vbo}
* and {@link FullScreenRenderer#ibo} buffer objects instead.
*/
@Deprecated(forRemoval = true)
public static MeshBuilder mesh;

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

mesh.begin();
mesh.ensureQuadCapacity();

mesh.quad(
mesh.vec2(-1, -1).next(),
Expand All @@ -28,5 +36,8 @@ public static void init() {
);

mesh.end();

vbo = mesh.getVertexBuffer();
ibo = mesh.getIndexBuffer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;

import java.util.HashMap;
Expand All @@ -36,7 +37,9 @@ public class MeshRenderer {
private GpuTextureView depthAttachment;
private Color clearColor;
private RenderPipeline pipeline;
private MeshBuilder mesh;
private @Nullable MeshBuilder mesh;
private @Nullable GpuBuffer vertexBuffer;
private @Nullable GpuBuffer indexBuffer;
private Matrix4f matrix;
private final HashMap<String, GpuBufferSlice> uniforms = new HashMap<>();
private final HashMap<String, GpuTextureView> samplers = new HashMap<>();
Expand Down Expand Up @@ -73,23 +76,41 @@ public MeshRenderer pipeline(RenderPipeline pipeline) {
return this;
}

public MeshRenderer mesh(GpuBuffer vertices, GpuBuffer indices) {
this.vertexBuffer = vertices;
this.indexBuffer = indices;
return this;
}

public MeshRenderer mesh(MeshBuilder mesh) {
this.mesh = mesh;
return this;
}

public MeshRenderer mesh(MeshBuilder mesh, Matrix4f matrix) {
this.mesh = mesh;
this.matrix = matrix;
return this;
return this.transform(matrix);
}

public MeshRenderer mesh(MeshBuilder mesh, MatrixStack matrices) {
this.mesh = mesh;
return this.transform(matrices);
}

public MeshRenderer transform(Matrix4f matrix) {
this.matrix = matrix;
return this;
}

public MeshRenderer transform(MatrixStack matrices) {
this.matrix = matrices.peek().getPositionMatrix();
return this;
}

public MeshRenderer fullscreen() {
return this.mesh(FullScreenRenderer.vbo, FullScreenRenderer.ibo);
}

public MeshRenderer uniform(String name, GpuBufferSlice slice) {
uniforms.put(name, slice);
return this;
Expand All @@ -104,11 +125,15 @@ public MeshRenderer sampler(String name, GpuTextureView view) {
}

public void end() {
if (mesh.isBuilding()) {
if (mesh != null && mesh.isBuilding()) {
mesh.end();
}

if (mesh.getIndicesCount() > 0) {
int indexCount = mesh != null ? mesh.getIndicesCount()
: (indexBuffer != null ? indexBuffer.size() / Integer.BYTES : -1);

if (indexCount > 0) {

if (Utils.rendering3D || matrix != null) {
RenderSystem.getModelViewStack().pushMatrix();
}
Expand All @@ -121,8 +146,8 @@ public void end() {
applyCameraPos();
}

GpuBuffer vertexBuffer = mesh.getVertexBuffer();
GpuBuffer indexBuffer = mesh.getIndexBuffer();
GpuBuffer vertexBuffer = mesh != null ? mesh.getVertexBuffer() : this.vertexBuffer;
GpuBuffer indexBuffer = mesh != null ? mesh.getIndexBuffer() : this.indexBuffer;

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

pass.setVertexBuffer(0, vertexBuffer);
pass.setIndexBuffer(indexBuffer, VertexFormat.IndexType.INT);
pass.drawIndexed(0, 0, mesh.getIndicesCount(), 1);
pass.drawIndexed(0, 0, indexCount, 1);

pass.close();
}
Expand All @@ -163,6 +188,8 @@ public void end() {
clearColor = null;
pipeline = null;
mesh = null;
vertexBuffer = null;
indexBuffer = null;
matrix = null;
uniforms.clear();
samplers.clear();
Expand Down
Loading