Buffer building changes

- Remove ShadeSeparatedBufferBuilder and use Pair<RenderedBuffer,
Integer> instead to properly support 1.19's buffer changes
- Add ModelUtil#isVirtual to check if a ModelData is virtual
This commit is contained in:
PepperCode1 2022-08-03 23:18:49 -07:00
parent be78f92df6
commit c4942ca29e
8 changed files with 44 additions and 66 deletions

View file

@ -1,6 +1,5 @@
package com.jozufozu.flywheel.core.model; package com.jozufozu.flywheel.core.model;
import java.lang.management.MemoryUsage;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.function.Supplier; 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.VertexList;
import com.jozufozu.flywheel.api.vertex.VertexType; 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.GlBufferType;
import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage;
import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer;
import com.jozufozu.flywheel.backend.model.ElementBuffer; import com.jozufozu.flywheel.backend.model.ElementBuffer;
import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.QuadConverter; import com.jozufozu.flywheel.core.QuadConverter;
import com.jozufozu.flywheel.util.Pair;
import com.mojang.blaze3d.platform.MemoryTracker; import com.mojang.blaze3d.platform.MemoryTracker;
import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexBuffer;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.BakedModel;
@ -54,11 +52,10 @@ public class BlockModel implements Model {
this(bufferable.build(), name); this(bufferable.build(), name);
} }
public BlockModel(ShadeSeparatedBufferBuilder buffer, String name) { public BlockModel(Pair<RenderedBuffer, Integer> pair, String name) {
this.name = name; this.name = name;
BufferBuilder.RenderedBuffer renderedBuffer = buffer.endOrDiscardIfEmpty(); RenderedBuffer renderedBuffer = pair.first();
if (renderedBuffer == null) { if (renderedBuffer == null) {
reader = null; reader = null;
eboSupplier = () -> null; eboSupplier = () -> null;
@ -67,7 +64,7 @@ public class BlockModel implements Model {
BufferBuilder.DrawState drawState = renderedBuffer.drawState(); BufferBuilder.DrawState drawState = renderedBuffer.drawState();
reader = Formats.BLOCK.createReader(renderedBuffer, buffer.getUnshadedStartVertex()); reader = Formats.BLOCK.createReader(renderedBuffer, pair.second());
if (drawState.sequentialIndex()) { if (drawState.sequentialIndex()) {
ByteBuffer src = renderedBuffer.indexBuffer(); ByteBuffer src = renderedBuffer.indexBuffer();

View file

@ -1,6 +1,8 @@
package com.jozufozu.flywheel.core.model; 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 com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.block.ModelBlockRenderer; import net.minecraft.client.renderer.block.ModelBlockRenderer;
@ -12,7 +14,7 @@ import net.minecraft.util.RandomSource;
public interface Bufferable { public interface Bufferable {
void bufferInto(ModelBlockRenderer renderer, VertexConsumer consumer, RandomSource random); void bufferInto(ModelBlockRenderer renderer, VertexConsumer consumer, RandomSource random);
default ShadeSeparatedBufferBuilder build() { default Pair<RenderedBuffer, Integer> build() {
return ModelUtil.getBufferBuilder(this); return ModelUtil.getRenderedBuffer(this);
} }
} }

View file

@ -5,8 +5,11 @@ import java.util.Collection;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.jozufozu.flywheel.Flywheel; 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.jozufozu.flywheel.util.transform.TransformStack;
import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferBuilder.RenderedBuffer;
import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat; import com.mojang.blaze3d.vertex.VertexFormat;
@ -53,7 +56,22 @@ public class ModelUtil {
return dispatcher; 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<RenderedBuffer, Integer> 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<RenderedBuffer, Integer> getRenderedBuffer(Bufferable bufferable) {
ModelBlockRenderer blockRenderer = VANILLA_RENDERER.getModelRenderer(); ModelBlockRenderer blockRenderer = VANILLA_RENDERER.getModelRenderer();
ThreadLocalObjects objects = THREAD_LOCAL_OBJECTS.get(); ThreadLocalObjects objects = THREAD_LOCAL_OBJECTS.get();
@ -61,31 +79,29 @@ public class ModelUtil {
bufferable.bufferInto(blockRenderer, objects.shadeSeparatingWrapper, objects.random); bufferable.bufferInto(blockRenderer, objects.shadeSeparatingWrapper, objects.random);
objects.end(); return objects.end();
return objects.separatedBufferBuilder;
} }
public static ShadeSeparatedBufferBuilder getBufferBuilder(BakedModel model, BlockState referenceState, PoseStack poseStack) { public static Pair<RenderedBuffer, Integer> getBufferBuilder(BakedModel model, BlockState referenceState, PoseStack poseStack) {
return new BakedModelBuilder(model).withReferenceState(referenceState) return new BakedModelBuilder(model).withReferenceState(referenceState)
.withPoseStack(poseStack) .withPoseStack(poseStack)
.build(); .build();
} }
public static ShadeSeparatedBufferBuilder getBufferBuilder(BlockAndTintGetter renderWorld, BakedModel model, BlockState referenceState, PoseStack poseStack) { public static Pair<RenderedBuffer, Integer> getBufferBuilder(BlockAndTintGetter renderWorld, BakedModel model, BlockState referenceState, PoseStack poseStack) {
return new BakedModelBuilder(model).withReferenceState(referenceState) return new BakedModelBuilder(model).withReferenceState(referenceState)
.withPoseStack(poseStack) .withPoseStack(poseStack)
.withRenderWorld(renderWorld) .withRenderWorld(renderWorld)
.build(); .build();
} }
public static ShadeSeparatedBufferBuilder getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection<StructureTemplate.StructureBlockInfo> blocks) { public static Pair<RenderedBuffer, Integer> getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection<StructureTemplate.StructureBlockInfo> blocks) {
return new WorldModelBuilder(layer).withRenderWorld(renderWorld) return new WorldModelBuilder(layer).withRenderWorld(renderWorld)
.withBlocks(blocks) .withBlocks(blocks)
.build(); .build();
} }
public static ShadeSeparatedBufferBuilder getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection<StructureTemplate.StructureBlockInfo> blocks, PoseStack poseStack) { public static Pair<RenderedBuffer, Integer> getBufferBuilderFromTemplate(BlockAndTintGetter renderWorld, RenderType layer, Collection<StructureTemplate.StructureBlockInfo> blocks, PoseStack poseStack) {
return new WorldModelBuilder(layer).withRenderWorld(renderWorld) return new WorldModelBuilder(layer).withRenderWorld(renderWorld)
.withBlocks(blocks) .withBlocks(blocks)
.withPoseStack(poseStack) .withPoseStack(poseStack)
@ -106,20 +122,18 @@ public class ModelUtil {
private static class ThreadLocalObjects { private static class ThreadLocalObjects {
public final RandomSource random = RandomSource.create(); public final RandomSource random = RandomSource.create();
public final ShadeSeparatingVertexConsumer shadeSeparatingWrapper = new ShadeSeparatingVertexConsumer(); 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); public final BufferBuilder unshadedBuilder = new BufferBuilder(512);
private void begin() { 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.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<RenderedBuffer, Integer> end() {
this.shadeSeparatingWrapper.clear(); this.shadeSeparatingWrapper.clear();
this.unshadedBuilder.end(); return ModelUtil.endShadeSeparated(shadedBuilder, unshadedBuilder);
this.separatedBufferBuilder.appendUnshadedVertices(this.unshadedBuilder);
this.separatedBufferBuilder.end();
} }
} }
} }

View file

@ -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;
}
}

View file

@ -6,10 +6,8 @@ import com.jozufozu.flywheel.api.vertex.VertexList;
import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.core.layout.BufferLayout; import com.jozufozu.flywheel.core.layout.BufferLayout;
import com.jozufozu.flywheel.core.layout.CommonItems; 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.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.datafixers.util.Pair;
public class BlockVertex implements VertexType { public class BlockVertex implements VertexType {
@ -72,10 +70,11 @@ Vertex FLWCreateVertex() {
} }
ByteBuffer vertexBuffer = renderedBuffer.vertexBuffer(); ByteBuffer vertexBuffer = renderedBuffer.vertexBuffer();
if (unshadedStartVertex > 0) { int vertexCount = drawState.vertexCount();
return createReader(vertexBuffer, drawState.vertexCount(), unshadedStartVertex); if (unshadedStartVertex > 0 && unshadedStartVertex < vertexCount) {
return createReader(vertexBuffer, vertexCount, unshadedStartVertex);
} else { } else {
return createReader(vertexBuffer, drawState.vertexCount()); return createReader(vertexBuffer, vertexCount);
} }
} }
} }

View file

@ -3,9 +3,7 @@ package com.jozufozu.flywheel.core.vertex;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import com.jozufozu.flywheel.api.vertex.ShadedVertexList; import com.jozufozu.flywheel.api.vertex.ShadedVertexList;
import com.jozufozu.flywheel.core.model.ShadeSeparatedBufferBuilder;
import com.jozufozu.flywheel.util.RenderMath; import com.jozufozu.flywheel.util.RenderMath;
import com.mojang.blaze3d.vertex.BufferBuilder;
public class BlockVertexList extends AbstractVertexList { public class BlockVertexList extends AbstractVertexList {

View file

@ -1,5 +1,5 @@
modLoader = "javafml" modLoader = "javafml"
loaderVersion = "[40,)" loaderVersion = "[41,)"
issueTrackerURL = "https://github.com/Jozufozu/Flywheel/issues" issueTrackerURL = "https://github.com/Jozufozu/Flywheel/issues"
license = "MIT" license = "MIT"

View file

@ -1,6 +1,6 @@
{ {
"pack": { "pack": {
"description": "Flywheel resources", "description": "Flywheel resources",
"pack_format": 8 "pack_format": 9
} }
} }