From c4942ca29e80e72907397795c7b440e273cebf2a Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 3 Aug 2022 23:18:49 -0700 Subject: [PATCH] Buffer building changes - Remove ShadeSeparatedBufferBuilder and use Pair instead to properly support 1.19's buffer changes - Add ModelUtil#isVirtual to check if a ModelData is virtual --- .../flywheel/core/model/BlockModel.java | 13 +++--- .../flywheel/core/model/Bufferable.java | 6 ++- .../flywheel/core/model/ModelUtil.java | 44 ++++++++++++------- .../model/ShadeSeparatedBufferBuilder.java | 32 -------------- .../flywheel/core/vertex/BlockVertex.java | 9 ++-- .../flywheel/core/vertex/BlockVertexList.java | 2 - src/main/resources/META-INF/mods.toml | 2 +- src/main/resources/pack.mcmeta | 2 +- 8 files changed, 44 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/core/model/ShadeSeparatedBufferBuilder.java diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java index 45b2e6f1a..158806e7a 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java @@ -1,6 +1,5 @@ package com.jozufozu.flywheel.core.model; -import java.lang.management.MemoryUsage; import java.nio.ByteBuffer; import java.util.function.Supplier; @@ -8,18 +7,17 @@ import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.backend.gl.GlNumericType; -import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.backend.model.ElementBuffer; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.QuadConverter; +import com.jozufozu.flywheel.util.Pair; import com.mojang.blaze3d.platform.MemoryTracker; import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer; import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexBuffer; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.model.BakedModel; @@ -54,11 +52,10 @@ public class BlockModel implements Model { this(bufferable.build(), name); } - public BlockModel(ShadeSeparatedBufferBuilder buffer, String name) { + public BlockModel(Pair pair, String name) { this.name = name; - BufferBuilder.RenderedBuffer renderedBuffer = buffer.endOrDiscardIfEmpty(); - + RenderedBuffer renderedBuffer = pair.first(); if (renderedBuffer == null) { reader = null; eboSupplier = () -> null; @@ -67,7 +64,7 @@ public class BlockModel implements Model { BufferBuilder.DrawState drawState = renderedBuffer.drawState(); - reader = Formats.BLOCK.createReader(renderedBuffer, buffer.getUnshadedStartVertex()); + reader = Formats.BLOCK.createReader(renderedBuffer, pair.second()); if (drawState.sequentialIndex()) { ByteBuffer src = renderedBuffer.indexBuffer(); diff --git a/src/main/java/com/jozufozu/flywheel/core/model/Bufferable.java b/src/main/java/com/jozufozu/flywheel/core/model/Bufferable.java index 2c7c74a4a..e01fecd82 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/Bufferable.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/Bufferable.java @@ -1,6 +1,8 @@ package com.jozufozu.flywheel.core.model; +import com.jozufozu.flywheel.util.Pair; +import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer; import com.mojang.blaze3d.vertex.VertexConsumer; import net.minecraft.client.renderer.block.ModelBlockRenderer; @@ -12,7 +14,7 @@ import net.minecraft.util.RandomSource; public interface Bufferable { void bufferInto(ModelBlockRenderer renderer, VertexConsumer consumer, RandomSource random); - default ShadeSeparatedBufferBuilder build() { - return ModelUtil.getBufferBuilder(this); + default Pair build() { + return ModelUtil.getRenderedBuffer(this); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java index 26fc5859a..57d8aaeea 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/ModelUtil.java @@ -5,8 +5,11 @@ import java.util.Collection; import java.util.function.Supplier; import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.backend.model.BufferBuilderExtension; +import com.jozufozu.flywheel.util.Pair; import com.jozufozu.flywheel.util.transform.TransformStack; import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexFormat; @@ -53,7 +56,22 @@ public class ModelUtil { return dispatcher; } - public static ShadeSeparatedBufferBuilder getBufferBuilder(Bufferable bufferable) { + public static boolean isVirtual(ModelData data) { + return data.has(ModelUtil.VIRTUAL_PROPERTY) && data.get(ModelUtil.VIRTUAL_PROPERTY); + } + + public static Pair endShadeSeparated(BufferBuilder shadedBuilder, BufferBuilder unshadedBuilder) { + int unshadedStartVertex = ((BufferBuilderExtension) shadedBuilder).flywheel$getVertices(); + RenderedBuffer unshadedBuffer = unshadedBuilder.endOrDiscardIfEmpty(); + if (unshadedBuffer != null) { + // FIXME: Unshaded indices + ((BufferBuilderExtension) shadedBuilder).flywheel$appendBufferUnsafe(unshadedBuffer.vertexBuffer()); + } + RenderedBuffer buffer = shadedBuilder.endOrDiscardIfEmpty(); + return Pair.of(buffer, unshadedStartVertex); + } + + public static Pair getRenderedBuffer(Bufferable bufferable) { ModelBlockRenderer blockRenderer = VANILLA_RENDERER.getModelRenderer(); ThreadLocalObjects objects = THREAD_LOCAL_OBJECTS.get(); @@ -61,31 +79,29 @@ public class ModelUtil { bufferable.bufferInto(blockRenderer, objects.shadeSeparatingWrapper, objects.random); - objects.end(); - - return objects.separatedBufferBuilder; + return objects.end(); } - public static ShadeSeparatedBufferBuilder getBufferBuilder(BakedModel model, BlockState referenceState, PoseStack poseStack) { + public static Pair getBufferBuilder(BakedModel model, BlockState referenceState, PoseStack poseStack) { return new BakedModelBuilder(model).withReferenceState(referenceState) .withPoseStack(poseStack) .build(); } - public static ShadeSeparatedBufferBuilder getBufferBuilder(BlockAndTintGetter renderWorld, BakedModel model, BlockState referenceState, PoseStack poseStack) { + public static Pair getBufferBuilder(BlockAndTintGetter renderWorld, BakedModel model, BlockState referenceState, PoseStack poseStack) { return new BakedModelBuilder(model).withReferenceState(referenceState) .withPoseStack(poseStack) .withRenderWorld(renderWorld) .build(); } - public static ShadeSeparatedBufferBuilder getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks) { + public static Pair getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks) { return new WorldModelBuilder(layer).withRenderWorld(renderWorld) .withBlocks(blocks) .build(); } - public static ShadeSeparatedBufferBuilder getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks, PoseStack poseStack) { + public static Pair getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks, PoseStack poseStack) { return new WorldModelBuilder(layer).withRenderWorld(renderWorld) .withBlocks(blocks) .withPoseStack(poseStack) @@ -106,20 +122,18 @@ public class ModelUtil { private static class ThreadLocalObjects { public final RandomSource random = RandomSource.create(); public final ShadeSeparatingVertexConsumer shadeSeparatingWrapper = new ShadeSeparatingVertexConsumer(); - public final ShadeSeparatedBufferBuilder separatedBufferBuilder = new ShadeSeparatedBufferBuilder(512); + public final BufferBuilder shadedBuilder = new BufferBuilder(512); public final BufferBuilder unshadedBuilder = new BufferBuilder(512); private void begin() { - this.separatedBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); + this.shadedBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); this.unshadedBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK); - this.shadeSeparatingWrapper.prepare(this.separatedBufferBuilder, this.unshadedBuilder); + this.shadeSeparatingWrapper.prepare(this.shadedBuilder, this.unshadedBuilder); } - private void end() { + private Pair end() { this.shadeSeparatingWrapper.clear(); - this.unshadedBuilder.end(); - this.separatedBufferBuilder.appendUnshadedVertices(this.unshadedBuilder); - this.separatedBufferBuilder.end(); + return ModelUtil.endShadeSeparated(shadedBuilder, unshadedBuilder); } } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ShadeSeparatedBufferBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/ShadeSeparatedBufferBuilder.java deleted file mode 100644 index 71a05d70f..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/model/ShadeSeparatedBufferBuilder.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jozufozu.flywheel.core.model; - -import java.nio.ByteBuffer; - -import com.jozufozu.flywheel.backend.model.BufferBuilderExtension; -import com.mojang.blaze3d.vertex.BufferBuilder; -import com.mojang.datafixers.util.Pair; - -public class ShadeSeparatedBufferBuilder extends BufferBuilder { - protected int unshadedStartVertex; - - public ShadeSeparatedBufferBuilder(int capacity) { - super(capacity); - } - - public void appendUnshadedVertices(BufferBuilder unshadedBuilder) { - RenderedBuffer renderedBuffer = unshadedBuilder.endOrDiscardIfEmpty(); - - if (renderedBuffer == null) { - return; - } - - // FIXME: Unshaded indices - ByteBuffer buffer = renderedBuffer.vertexBuffer(); - unshadedStartVertex = ((BufferBuilderExtension) this).flywheel$getVertices(); - ((BufferBuilderExtension) this).flywheel$appendBufferUnsafe(buffer); - } - - public int getUnshadedStartVertex() { - return unshadedStartVertex; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertex.java b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertex.java index 1ac22ae94..c9c465ca7 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertex.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertex.java @@ -6,10 +6,8 @@ import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.core.layout.BufferLayout; import com.jozufozu.flywheel.core.layout.CommonItems; -import com.jozufozu.flywheel.core.model.ShadeSeparatedBufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.DefaultVertexFormat; -import com.mojang.datafixers.util.Pair; public class BlockVertex implements VertexType { @@ -72,10 +70,11 @@ Vertex FLWCreateVertex() { } ByteBuffer vertexBuffer = renderedBuffer.vertexBuffer(); - if (unshadedStartVertex > 0) { - return createReader(vertexBuffer, drawState.vertexCount(), unshadedStartVertex); + int vertexCount = drawState.vertexCount(); + if (unshadedStartVertex > 0 && unshadedStartVertex < vertexCount) { + return createReader(vertexBuffer, vertexCount, unshadedStartVertex); } else { - return createReader(vertexBuffer, drawState.vertexCount()); + return createReader(vertexBuffer, vertexCount); } } } diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java index 654f94865..ef46bdf36 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java @@ -3,9 +3,7 @@ package com.jozufozu.flywheel.core.vertex; import java.nio.ByteBuffer; import com.jozufozu.flywheel.api.vertex.ShadedVertexList; -import com.jozufozu.flywheel.core.model.ShadeSeparatedBufferBuilder; import com.jozufozu.flywheel.util.RenderMath; -import com.mojang.blaze3d.vertex.BufferBuilder; public class BlockVertexList extends AbstractVertexList { diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index b06375677..9eedd54e7 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -1,5 +1,5 @@ modLoader = "javafml" -loaderVersion = "[40,)" +loaderVersion = "[41,)" issueTrackerURL = "https://github.com/Jozufozu/Flywheel/issues" license = "MIT" diff --git a/src/main/resources/pack.mcmeta b/src/main/resources/pack.mcmeta index 2d8d4da03..79cff50a3 100644 --- a/src/main/resources/pack.mcmeta +++ b/src/main/resources/pack.mcmeta @@ -1,6 +1,6 @@ { "pack": { "description": "Flywheel resources", - "pack_format": 8 + "pack_format": 9 } }