From d0924175deffe8327e9fa36b38e4d90140a413d4 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 10 Jul 2024 11:44:52 -0400 Subject: [PATCH] 1.21 Port II --- .../impl/mixin/BufferBuilderAccessor.java | 12 ++++++++ .../main/resources/flywheel.impl.mixins.json | 1 + .../flywheel/lib/model/baked/MeshEmitter.java | 20 +++++++------ .../flywheel/lib/model/baked/MeshEmitter.java | 28 +++++++++++-------- 4 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 common/src/main/java/dev/engine_room/flywheel/impl/mixin/BufferBuilderAccessor.java diff --git a/common/src/main/java/dev/engine_room/flywheel/impl/mixin/BufferBuilderAccessor.java b/common/src/main/java/dev/engine_room/flywheel/impl/mixin/BufferBuilderAccessor.java new file mode 100644 index 000000000..1b9e4b0a3 --- /dev/null +++ b/common/src/main/java/dev/engine_room/flywheel/impl/mixin/BufferBuilderAccessor.java @@ -0,0 +1,12 @@ +package dev.engine_room.flywheel.impl.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import com.mojang.blaze3d.vertex.BufferBuilder; + +@Mixin(BufferBuilder.class) +public interface BufferBuilderAccessor { + @Accessor("building") + boolean flywheel$getBuilding(); +} diff --git a/common/src/main/resources/flywheel.impl.mixins.json b/common/src/main/resources/flywheel.impl.mixins.json index c8d878e85..927b23ce3 100644 --- a/common/src/main/resources/flywheel.impl.mixins.json +++ b/common/src/main/resources/flywheel.impl.mixins.json @@ -6,6 +6,7 @@ "refmap": "flywheel.refmap.json", "client": [ "BlockEntityTypeMixin", + "BufferBuilderAccessor", "ClientChunkCacheMixin", "ClientLevelMixin", "EntityTypeMixin", diff --git a/fabric/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java b/fabric/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java index 268a6e1da..db3eb85ba 100644 --- a/fabric/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java +++ b/fabric/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java @@ -3,21 +3,25 @@ package dev.engine_room.flywheel.lib.model.baked; import org.jetbrains.annotations.UnknownNullability; import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.ByteBufferBuilder; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.VertexFormat; +import dev.engine_room.flywheel.impl.mixin.BufferBuilderAccessor; import net.minecraft.client.renderer.RenderType; class MeshEmitter { private final RenderType renderType; - private final BufferBuilder bufferBuilder; + private final ByteBufferBuilder byteBufferBuilder; + private BufferBuilder bufferBuilder; private BakedModelBufferer.@UnknownNullability ResultConsumer resultConsumer; private boolean currentShade; MeshEmitter(RenderType renderType) { this.renderType = renderType; - this.bufferBuilder = new BufferBuilder(renderType.bufferSize()); + this.byteBufferBuilder = new ByteBufferBuilder(renderType.bufferSize()); + this.bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); } public void prepare(BakedModelBufferer.ResultConsumer resultConsumer) { @@ -25,7 +29,7 @@ class MeshEmitter { } public void end() { - if (bufferBuilder.building()) { + if (((BufferBuilderAccessor) bufferBuilder).flywheel$getBuilding()) { emit(); } resultConsumer = null; @@ -37,22 +41,22 @@ class MeshEmitter { } void prepareForGeometry(boolean shade) { - if (!bufferBuilder.building()) { - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + if (!((BufferBuilderAccessor) bufferBuilder).flywheel$getBuilding()) { + bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); } else if (shade != currentShade) { emit(); - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); } currentShade = shade; } void emit() { - var renderedBuffer = bufferBuilder.endOrDiscardIfEmpty(); + var renderedBuffer = bufferBuilder.build(); if (renderedBuffer != null) { resultConsumer.accept(renderType, currentShade, renderedBuffer); - renderedBuffer.release(); + renderedBuffer.close(); } } } diff --git a/forge/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java b/forge/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java index c6e181a5c..b45845d6f 100644 --- a/forge/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java +++ b/forge/src/lib/java/dev/engine_room/flywheel/lib/model/baked/MeshEmitter.java @@ -3,32 +3,36 @@ package dev.engine_room.flywheel.lib.model.baked; import org.jetbrains.annotations.UnknownNullability; import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.ByteBufferBuilder; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexFormat; +import dev.engine_room.flywheel.impl.mixin.BufferBuilderAccessor; import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.block.model.BakedQuad; class MeshEmitter implements VertexConsumer { private final RenderType renderType; - private final BufferBuilder bufferBuilder; + private final ByteBufferBuilder byteBufferBuilder; + private BufferBuilder bufferBuilder; private BakedModelBufferer.@UnknownNullability ResultConsumer resultConsumer; private boolean currentShade; - MeshEmitter(RenderType renderType) { - this.renderType = renderType; - this.bufferBuilder = new BufferBuilder(renderType.bufferSize()); - } + MeshEmitter(RenderType renderType) { + this.renderType = renderType; + this.byteBufferBuilder = new ByteBufferBuilder(renderType.bufferSize()); + this.bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + } public void prepare(BakedModelBufferer.ResultConsumer resultConsumer) { this.resultConsumer = resultConsumer; } public void end() { - if (bufferBuilder.building()) { + if (((BufferBuilderAccessor) bufferBuilder).flywheel$getBuilding()) { emit(); } resultConsumer = null; @@ -39,12 +43,12 @@ class MeshEmitter implements VertexConsumer { return bufferBuilder; } - private void prepareForGeometry(boolean shade) { - if (!bufferBuilder.building()) { - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + void prepareForGeometry(boolean shade) { + if (!((BufferBuilderAccessor) bufferBuilder).flywheel$getBuilding()) { + bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); } else if (shade != currentShade) { emit(); - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + bufferBuilder = new BufferBuilder(byteBufferBuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); } currentShade = shade; @@ -55,11 +59,11 @@ class MeshEmitter implements VertexConsumer { } private void emit() { - var renderedBuffer = bufferBuilder.endOrDiscardIfEmpty(); + var renderedBuffer = bufferBuilder.build(); if (renderedBuffer != null) { resultConsumer.accept(renderType, currentShade, renderedBuffer); - renderedBuffer.release(); + renderedBuffer.close(); } }