From 744199217c2257c9c3def8eef70d60528fc64c0a Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 20 Dec 2023 12:14:30 -0800 Subject: [PATCH] Drew buffer - Remove DrawBuffer and related mixins. --- .../java/com/jozufozu/flywheel/Flywheel.java | 2 - .../backend/engine/batching/DrawBuffer.java | 183 ------------------ .../engine/batching/DrawBufferSet.java | 32 --- .../extension/RenderTypeExtension.java | 26 --- .../flywheel/mixin/RenderTypeMixin.java | 31 --- src/main/resources/flywheel.mixins.json | 1 - 6 files changed, 275 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java delete mode 100644 src/main/java/com/jozufozu/flywheel/extension/RenderTypeExtension.java delete mode 100644 src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index e074cfbeb..811d3f168 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -11,7 +11,6 @@ import com.jozufozu.flywheel.backend.Backends; import com.jozufozu.flywheel.backend.ShaderIndices; import com.jozufozu.flywheel.backend.compile.FlwPrograms; import com.jozufozu.flywheel.backend.engine.UniformBuffer; -import com.jozufozu.flywheel.backend.engine.batching.DrawBuffer; import com.jozufozu.flywheel.config.BackendArgument; import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; @@ -96,7 +95,6 @@ public class Flywheel { forgeEventBus.addListener(FlwCommands::registerClientCommands); - forgeEventBus.addListener(DrawBuffer::onReloadRenderers); forgeEventBus.addListener(UniformBuffer::onReloadLevelRenderer); forgeEventBus.addListener(LightUpdater::onClientTick); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java deleted file mode 100644 index 9713df7ab..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.jozufozu.flywheel.backend.engine.batching; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -import com.jozufozu.flywheel.api.event.ReloadLevelRendererEvent; -import com.jozufozu.flywheel.api.event.RenderStage; -import com.jozufozu.flywheel.api.vertex.VertexView; -import com.jozufozu.flywheel.api.vertex.VertexViewProvider; -import com.jozufozu.flywheel.extension.BufferBuilderExtension; -import com.jozufozu.flywheel.extension.RenderTypeExtension; -import com.jozufozu.flywheel.lib.memory.MemoryBlock; -import com.mojang.blaze3d.vertex.VertexFormat; - -import net.minecraft.client.renderer.RenderType; -import net.minecraft.util.Mth; - -/** - * A byte buffer that can be used to draw vertices through multiple {@link VertexView}s. - *
- * Note: The number of vertices needs to be known ahead of time. - */ -public class DrawBuffer { - private static final List ALL = new ArrayList<>(); - - private final RenderType renderType; - private final VertexFormat format; - private final int stride; - private final boolean sortOnUpload; - private final VertexViewProvider provider; - - private MemoryBlock data; - private ByteBuffer buffer; - - private boolean prepared; - private int vertexCount; - private int verticesToDraw; - - public DrawBuffer(RenderType renderType, VertexFormat format, int stride, boolean sortOnUpload, VertexViewProvider provider) { - this.renderType = renderType; - this.format = format; - this.stride = stride; - this.sortOnUpload = sortOnUpload; - this.provider = provider; - - ALL.add(this); - } - - /** - * Prepares this buffer by initializing a block of memory. - * @param vertexCount The number of vertices to reserve memory for. - * @throws IllegalStateException If the buffer is already in use. - */ - public void prepare(int vertexCount) { - if (prepared) { - throw new IllegalStateException("Cannot prepare DrawBuffer twice!"); - } - - this.vertexCount = vertexCount; - verticesToDraw = this.vertexCount; - - // Add one extra vertex to uphold the vanilla assumption that BufferBuilders have at least - // enough buffer space for one more vertex. Rubidium checks for this extra space when popNextBuffer - // is called and reallocates the buffer if there is not space for one more vertex. - int byteSize = stride * (vertexCount + 1); - - // We'll need to allocate space for the index buffer if this render type needs sorting. - if (sortOnUpload) { - int i = renderType.mode() - .indexCount(vertexCount); - VertexFormat.IndexType indexType = VertexFormat.IndexType.least(i); - int extraBytes = Mth.roundToward(i * indexType.bytes, 4); - byteSize += extraBytes; - } - - if (data == null) { - data = MemoryBlock.malloc(byteSize); - buffer = data.asBuffer(); - } else if (byteSize > data.size()) { - data = data.realloc(byteSize); - buffer = data.asBuffer(); - } - - prepared = true; - } - - public VertexView slice(int startVertex, int vertexCount) { - if (!prepared) { - throw new IllegalStateException("Cannot slice DrawBuffer that is not prepared!"); - } - - if (startVertex + vertexCount > this.vertexCount) { - throw new IndexOutOfBoundsException("Vertex count greater than allocated: " + startVertex + " + " + vertexCount + " > " + this.vertexCount); - } - - VertexView vertexView = provider.createVertexView(); - vertexView.ptr(ptrForVertex(startVertex)); - vertexView.vertexCount(vertexCount); - return vertexView; - } - - public long ptrForVertex(long startVertex) { - return data.ptr() + startVertex * stride; - } - - public void verticesToDraw(int verticesToDraw) { - this.verticesToDraw = verticesToDraw; - } - - /** - * Injects the backing buffer into the given builder and prepares it for rendering. - * - * @param bufferBuilder The buffer builder to inject into. - */ - public void inject(BufferBuilderExtension bufferBuilder) { - if (!prepared) { - throw new IllegalStateException("Cannot inject DrawBuffer that is not prepared!"); - } - - buffer.clear(); - bufferBuilder.flywheel$injectForRender(buffer, format, verticesToDraw); - } - - public RenderType getRenderType() { - return renderType; - } - - public VertexFormat getVertexFormat() { - return format; - } - - public boolean isPrepared() { - return prepared; - } - - public int getVertexCount() { - return vertexCount; - } - - public int getVerticesToDraw() { - return verticesToDraw; - } - - /** - * @return {@code true} if the buffer has any vertices to draw. - */ - public boolean hasVertices() { - return verticesToDraw > 0; - } - - /** - * Reset the draw buffer to have no vertices.

- * - * Does not clear the backing buffer. - */ - public void reset() { - prepared = false; - vertexCount = 0; - verticesToDraw = 0; - } - - public void free() { - reset(); - - if (data == null) { - return; - } - - data.free(); - data = null; - buffer = null; - } - - public static void onReloadRenderers(ReloadLevelRendererEvent event) { - ALL.forEach(DrawBuffer::free); - } - - public static DrawBuffer get(RenderType renderType, RenderStage stage) { - return RenderTypeExtension.getDrawBufferSet(renderType) - .getBuffer(stage); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java deleted file mode 100644 index ce18ead93..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jozufozu.flywheel.backend.engine.batching; - -import java.util.EnumMap; -import java.util.Map; - -import com.jozufozu.flywheel.api.event.RenderStage; -import com.jozufozu.flywheel.api.vertex.VertexViewProvider; -import com.jozufozu.flywheel.api.vertex.VertexViewProviderRegistry; -import com.mojang.blaze3d.vertex.VertexFormat; - -import net.minecraft.client.renderer.RenderType; - -public class DrawBufferSet { - private final RenderType renderType; - private final VertexFormat format; - private final boolean sortOnUpload; - private final int stride; - private final VertexViewProvider provider; - private final Map buffers = new EnumMap<>(RenderStage.class); - - public DrawBufferSet(RenderType renderType, boolean sortOnUpload) { - this.renderType = renderType; - this.sortOnUpload = sortOnUpload; - format = renderType.format(); - stride = format.getVertexSize(); - provider = VertexViewProviderRegistry.getProvider(format); - } - - public DrawBuffer getBuffer(RenderStage stage) { - return buffers.computeIfAbsent(stage, renderStage -> new DrawBuffer(renderType, format, stride, sortOnUpload, provider)); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/extension/RenderTypeExtension.java b/src/main/java/com/jozufozu/flywheel/extension/RenderTypeExtension.java deleted file mode 100644 index d132ab426..000000000 --- a/src/main/java/com/jozufozu/flywheel/extension/RenderTypeExtension.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.jozufozu.flywheel.extension; - -import com.jozufozu.flywheel.backend.engine.batching.DrawBufferSet; - -import net.minecraft.client.renderer.RenderType; - -/** - * Duck interface to make RenderType store a DrawBufferSet. - * - * @see RenderType - */ -public interface RenderTypeExtension { - /** - * @return The DrawBufferSet associated with this RenderType. - */ - DrawBufferSet flywheel$getDrawBufferSet(); - - /** - * Helper function to cast a RenderType to a RenderTypeExtension and get its DrawBufferSet. - * @param type The RenderType to get the DrawBufferSet from. - * @return The DrawBufferSet associated with the given RenderType. - */ - static DrawBufferSet getDrawBufferSet(RenderType type) { - return ((RenderTypeExtension) type).flywheel$getDrawBufferSet(); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java deleted file mode 100644 index 1644c2c08..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; - -import com.jozufozu.flywheel.backend.engine.batching.DrawBufferSet; -import com.jozufozu.flywheel.extension.RenderTypeExtension; - -import net.minecraft.client.renderer.RenderType; - -@Mixin(RenderType.class) -abstract class RenderTypeMixin implements RenderTypeExtension { - @Shadow - @Final - private boolean sortOnUpload; - - @Unique - private DrawBufferSet flywheel$drawBufferSet; - - @Override - @NotNull - public DrawBufferSet flywheel$getDrawBufferSet() { - if (flywheel$drawBufferSet == null) { - flywheel$drawBufferSet = new DrawBufferSet((RenderType) (Object) this, sortOnUpload); - } - return flywheel$drawBufferSet; - } -} diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 5d0885cc2..396e1b26f 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -16,7 +16,6 @@ "LightUpdateMixin", "MinecraftMixin", "PoseStackMixin", - "RenderTypeMixin", "VertexFormatMixin", "fix.FixFabulousDepthMixin", "fix.FixNormalScalingMixin",