From 3e01d3f4419feafe75b88ecb05a80daafafab8c0 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 15 Dec 2021 00:35:49 -0800 Subject: [PATCH] Batching Engine - Implement alternate backend using SBBs --- .../contraptions/base/flwdata/BeltType.java | 32 +++++++- .../base/flwdata/RotatingType.java | 22 +++++- .../base/flwdata/UnsafeBeltWriter.java | 19 +---- .../base/flwdata/UnsafeKineticWriter.java | 24 ++++++ .../base/flwdata/UnsafeRotatingWriter.java | 19 +---- .../components/actors/flwdata/ActorType.java | 7 +- .../actors/flwdata/UnsafeActorWriter.java | 3 +- .../structureMovement/glue/GlueInstance.java | 78 ++++++++++++++++++- .../logistics/block/flap/FlapData.java | 10 ++- .../logistics/block/flap/FlapType.java | 29 ++++++- .../block/flap/UnsafeFlapWriter.java | 8 +- .../foundation/render/SuperByteBuffer.java | 13 ++-- .../assets/create/flywheel/shaders/flap.vert | 4 +- 13 files changed, 207 insertions(+), 61 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeKineticWriter.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/BeltType.java b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/BeltType.java index 1c72568d5..8e3a62a6b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/BeltType.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/BeltType.java @@ -6,10 +6,14 @@ import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.core.model.Model; +import com.jozufozu.flywheel.util.RenderMath; +import com.mojang.math.Quaternion; +import com.simibubi.create.content.contraptions.KineticDebugger; import com.simibubi.create.foundation.render.AllInstanceFormats; import com.simibubi.create.foundation.render.AllProgramSpecs; +import com.simibubi.create.foundation.utility.AnimationTickHolder; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.resources.ResourceLocation; public class BeltType implements Instanced, Batched { @@ -34,7 +38,29 @@ public class BeltType implements Instanced, Batched { } @Override - public BatchingTransformer getTransformer(Model model) { - return null; + public BatchingTransformer getTransformer() { + return (d, sbb) -> { + + float spriteHeight = d.maxV - d.minV; + double scroll = d.rotationalSpeed * AnimationTickHolder.getRenderTime() / (31.5 * 16) + d.rotationOffset; + scroll = scroll - Math.floor(scroll); + scroll = scroll * spriteHeight * RenderMath.f(d.scrollMult); + + float finalScroll = (float) scroll; + sbb.shiftUV((builder, u, v) -> { + float targetU = u - d.sourceU + d.minU; + float targetV = v - d.sourceV + d.minV + + finalScroll; + builder.uv(targetU, targetV); + }); + + sbb.translate(d.x + 0.5, d.y + 0.5, d.z + 0.5) + .multiply(new Quaternion(d.qX, d.qY, d.qZ, d.qW)) + .unCentre() + .light(d.getPackedLight()); + if (KineticDebugger.isActive()) { + sbb.color(d.r, d.g, d.b, d.a); + } + }; } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/RotatingType.java b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/RotatingType.java index 94e05590f..400afd94f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/RotatingType.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/RotatingType.java @@ -6,10 +6,14 @@ import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.core.model.Model; +import com.jozufozu.flywheel.util.RenderMath; +import com.mojang.math.Vector3f; +import com.simibubi.create.content.contraptions.KineticDebugger; import com.simibubi.create.foundation.render.AllInstanceFormats; import com.simibubi.create.foundation.render.AllProgramSpecs; +import com.simibubi.create.foundation.utility.AnimationTickHolder; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.resources.ResourceLocation; public class RotatingType implements Instanced, Batched { @@ -34,7 +38,19 @@ public class RotatingType implements Instanced, Batched getTransformer(Model model) { - return null; + public BatchingTransformer getTransformer() { + return (d, b) -> { + float angle = ((AnimationTickHolder.getRenderTime() * d.rotationalSpeed * 3f / 10 + d.rotationOffset) % 360); + + Vector3f axis = new Vector3f(RenderMath.f(d.rotationAxisX), RenderMath.f(d.rotationAxisY), RenderMath.f(d.rotationAxisZ)); + b.light(d.getPackedLight()) + .translate(d.x + 0.5, d.y + 0.5, d.z + 0.5) + .multiply(axis.rotationDegrees(angle)) + .unCentre(); + + if (KineticDebugger.isActive()) { + b.color(d.r, d.g, d.b, d.a); + } + }; } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeBeltWriter.java b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeBeltWriter.java index 1bd4e3ac5..59ba5203f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeBeltWriter.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeBeltWriter.java @@ -4,27 +4,16 @@ import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter; -public class UnsafeBeltWriter extends UnsafeBufferWriter { +public class UnsafeBeltWriter extends UnsafeKineticWriter { public UnsafeBeltWriter(VecBuffer backingBuffer, StructType vertexType) { super(backingBuffer, vertexType); } @Override - public void write(BeltData d) { + protected void writeInternal(BeltData d) { + super.writeInternal(d); long addr = writePointer; - MemoryUtil.memPutByte(addr, d.blockLight); - MemoryUtil.memPutByte(addr + 1, d.skyLight); - MemoryUtil.memPutByte(addr + 2, d.r); - MemoryUtil.memPutByte(addr + 3, d.g); - MemoryUtil.memPutByte(addr + 4, d.b); - MemoryUtil.memPutByte(addr + 5, d.a); - MemoryUtil.memPutFloat(addr + 6, d.x); - MemoryUtil.memPutFloat(addr + 10, d.y); - MemoryUtil.memPutFloat(addr + 14, d.z); - MemoryUtil.memPutFloat(addr + 18, d.rotationalSpeed); - MemoryUtil.memPutFloat(addr + 22, d.rotationOffset); MemoryUtil.memPutFloat(addr + 26, d.qX); MemoryUtil.memPutFloat(addr + 30, d.qY); MemoryUtil.memPutFloat(addr + 34, d.qZ); @@ -36,7 +25,5 @@ public class UnsafeBeltWriter extends UnsafeBufferWriter { MemoryUtil.memPutFloat(addr + 58, d.maxU); MemoryUtil.memPutFloat(addr + 62, d.maxV); MemoryUtil.memPutByte(addr + 66, d.scrollMult); - - advance(); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeKineticWriter.java b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeKineticWriter.java new file mode 100644 index 000000000..f6fbeebfc --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeKineticWriter.java @@ -0,0 +1,24 @@ +package com.simibubi.create.content.contraptions.base.flwdata; + +import org.lwjgl.system.MemoryUtil; + +import com.jozufozu.flywheel.api.struct.StructType; +import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; +import com.jozufozu.flywheel.core.materials.UnsafeBasicWriter; + +public abstract class UnsafeKineticWriter extends UnsafeBasicWriter { + public UnsafeKineticWriter(VecBuffer backingBuffer, StructType vertexType) { + super(backingBuffer, vertexType); + } + + @Override + protected void writeInternal(D d) { + super.writeInternal(d); + long addr = writePointer; + MemoryUtil.memPutFloat(addr + 6, d.x); + MemoryUtil.memPutFloat(addr + 10, d.y); + MemoryUtil.memPutFloat(addr + 14, d.z); + MemoryUtil.memPutFloat(addr + 18, d.rotationalSpeed); + MemoryUtil.memPutFloat(addr + 22, d.rotationOffset); + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeRotatingWriter.java b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeRotatingWriter.java index 8b0f13835..b477c8e20 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeRotatingWriter.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/flwdata/UnsafeRotatingWriter.java @@ -4,31 +4,18 @@ import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter; -public class UnsafeRotatingWriter extends UnsafeBufferWriter { +public class UnsafeRotatingWriter extends UnsafeKineticWriter { public UnsafeRotatingWriter(VecBuffer backingBuffer, StructType vertexType) { super(backingBuffer, vertexType); } @Override - public void write(RotatingData d) { + protected void writeInternal(RotatingData d) { + super.writeInternal(d); long addr = writePointer; - MemoryUtil.memPutByte(addr, d.blockLight); - MemoryUtil.memPutByte(addr + 1, d.skyLight); - MemoryUtil.memPutByte(addr + 2, d.r); - MemoryUtil.memPutByte(addr + 3, d.g); - MemoryUtil.memPutByte(addr + 4, d.b); - MemoryUtil.memPutByte(addr + 5, d.a); - MemoryUtil.memPutFloat(addr + 6, d.x); - MemoryUtil.memPutFloat(addr + 10, d.y); - MemoryUtil.memPutFloat(addr + 14, d.z); - MemoryUtil.memPutFloat(addr + 18, d.rotationalSpeed); - MemoryUtil.memPutFloat(addr + 22, d.rotationOffset); MemoryUtil.memPutByte(addr + 26, d.rotationAxisX); MemoryUtil.memPutByte(addr + 27, d.rotationAxisY); MemoryUtil.memPutByte(addr + 28, d.rotationAxisZ); - - advance(); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/ActorType.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/ActorType.java index f664a6698..5ff165d7c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/ActorType.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/ActorType.java @@ -6,7 +6,6 @@ import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.core.model.Model; import com.simibubi.create.foundation.render.AllInstanceFormats; import com.simibubi.create.foundation.render.AllProgramSpecs; @@ -34,7 +33,9 @@ public class ActorType implements Instanced, Batched { } @Override - public BatchingTransformer getTransformer(Model model) { - return null; + public BatchingTransformer getTransformer() { + return (d, sbb) -> { + + }; } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/UnsafeActorWriter.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/UnsafeActorWriter.java index 0a7184910..4479e800d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/UnsafeActorWriter.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/flwdata/UnsafeActorWriter.java @@ -12,7 +12,7 @@ public class UnsafeActorWriter extends UnsafeBufferWriter { } @Override - public void write(ActorData d) { + protected void writeInternal(ActorData d) { long addr = writePointer; MemoryUtil.memPutFloat(addr, d.x); MemoryUtil.memPutFloat(addr + 4, d.y); @@ -31,6 +31,5 @@ public class UnsafeActorWriter extends UnsafeBufferWriter { MemoryUtil.memPutByte(addr + 38, d.rotationCenterY); MemoryUtil.memPutByte(addr + 39, d.rotationCenterZ); MemoryUtil.memPutFloat(addr + 40, d.speed); - advance(); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java index bd464c94c..46a053521 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java @@ -11,6 +11,7 @@ import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.instancing.ConditionalInstance; import com.jozufozu.flywheel.core.materials.oriented.OrientedData; import com.jozufozu.flywheel.core.model.Model; +import com.jozufozu.flywheel.util.ModelReader; import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.math.Quaternion; import com.simibubi.create.AllItems; @@ -95,7 +96,7 @@ public class GlueInstance extends EntityInstance implements ITi || AllItems.SUPER_GLUE.isIn(player.getOffhandItem()); } - public static class GlueModel implements Model { + public static class GlueModel implements Model, ModelReader { @Override public String name() { @@ -165,5 +166,80 @@ public class GlueInstance extends EntityInstance implements ITi public VertexFormat format() { return Formats.UNLIT_MODEL; } + + @Override + public ModelReader getReader() { + return this; + } + + @Override + public int getVertexCount() { + return 8; + } + + @Override + public float getX(int index) { + return 0; + } + + @Override + public float getY(int index) { + return 0; + } + + @Override + public float getZ(int index) { + return 0; + } + + @Override + public byte getR(int index) { + return (byte) 0xFF; + } + + @Override + public byte getG(int index) { + return (byte) 0xFF; + } + + @Override + public byte getB(int index) { + return (byte) 0xFF; + } + + @Override + public byte getA(int index) { + return (byte) 0xFF; + } + + @Override + public float getU(int index) { + return 0; + } + + @Override + public float getV(int index) { + return 0; + } + + @Override + public int getLight(int index) { + return 0; + } + + @Override + public float getNX(int index) { + return 0; + } + + @Override + public float getNY(int index) { + return 0; + } + + @Override + public float getNZ(int index) { + return index > 4 ? -1 : 1; + } } } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapData.java b/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapData.java index 2e2ebb44b..591f0160c 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapData.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapData.java @@ -4,6 +4,7 @@ import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.core.materials.FlatLit; import com.mojang.math.Vector3f; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.core.BlockPos; public class FlapData extends InstanceData implements FlatLit { @@ -42,18 +43,23 @@ public class FlapData extends InstanceData implements FlatLit { @Override public FlapData setBlockLight(int blockLight) { - this.blockLight = (byte) ((blockLight & 0xF) << 4); + this.blockLight = (byte) (blockLight & 0xF); markDirty(); return this; } @Override public FlapData setSkyLight(int skyLight) { - this.skyLight = (byte) ((skyLight & 0xF) << 4); + this.skyLight = (byte) (skyLight & 0xF); markDirty(); return this; } + @Override + public int getPackedLight() { + return LightTexture.pack(this.blockLight, this.skyLight); + } + public FlapData setSegmentOffset(float x, float y, float z) { this.segmentOffsetX = x; this.segmentOffsetY = y; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapType.java b/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapType.java index 9838d5708..4d3a67b04 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapType.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/flap/FlapType.java @@ -6,10 +6,11 @@ import com.jozufozu.flywheel.api.struct.Instanced; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.core.model.Model; +import com.mojang.math.Vector3f; import com.simibubi.create.foundation.render.AllInstanceFormats; import com.simibubi.create.foundation.render.AllProgramSpecs; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.resources.ResourceLocation; public class FlapType implements Instanced, Batched { @@ -34,7 +35,29 @@ public class FlapType implements Instanced, Batched { } @Override - public BatchingTransformer getTransformer(Model model) { - return null; + public BatchingTransformer getTransformer() { + return (d, sbb) -> { + sbb.translate(d.x, d.y, d.z) + .centre() + .rotateY(-d.horizontalAngle) + .unCentre() + .translate(d.pivotX, d.pivotY, d.pivotZ) + .rotateX(getFlapAngle(d.flapness, d.intensity, d.flapScale)) + .translateBack(d.pivotX, d.pivotY, d.pivotZ) + .translate(d.segmentOffsetX, d.segmentOffsetY, d.segmentOffsetZ) + .light(d.getPackedLight()); + }; + } + + private static float getFlapAngle(float flapness, float intensity, float scale) { + float absFlap = Math.abs(flapness); + + float angle = (float) (Math.sin((1. - absFlap) * Math.PI * intensity) * 30. * flapness * scale); + + if (flapness > 0) { + return angle * 0.5f; + } else { + return angle; + } } } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/flap/UnsafeFlapWriter.java b/src/main/java/com/simibubi/create/content/logistics/block/flap/UnsafeFlapWriter.java index f0ace42cc..345fde063 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/flap/UnsafeFlapWriter.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/flap/UnsafeFlapWriter.java @@ -12,13 +12,13 @@ public class UnsafeFlapWriter extends UnsafeBufferWriter { } @Override - public void write(FlapData d) { + protected void writeInternal(FlapData d) { long addr = writePointer; MemoryUtil.memPutFloat(addr, d.x); MemoryUtil.memPutFloat(addr + 4, d.y); MemoryUtil.memPutFloat(addr + 8, d.z); - MemoryUtil.memPutByte(addr + 12, d.blockLight); - MemoryUtil.memPutByte(addr + 13, d.skyLight); + MemoryUtil.memPutByte(addr + 12, (byte) (d.blockLight << 4)); + MemoryUtil.memPutByte(addr + 13, (byte) (d.skyLight << 4)); MemoryUtil.memPutFloat(addr + 14, d.segmentOffsetX); MemoryUtil.memPutFloat(addr + 18, d.segmentOffsetY); MemoryUtil.memPutFloat(addr + 22, d.segmentOffsetZ); @@ -29,7 +29,5 @@ public class UnsafeFlapWriter extends UnsafeBufferWriter { MemoryUtil.memPutFloat(addr + 42, d.intensity); MemoryUtil.memPutFloat(addr + 46, d.flapScale); MemoryUtil.memPutFloat(addr + 50, d.flapness); - - advance(); } } diff --git a/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java b/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java index dec20e4b0..7cb2197cd 100644 --- a/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java @@ -1,6 +1,8 @@ package com.simibubi.create.foundation.render; import com.jozufozu.flywheel.util.BufferBuilderReader; +import com.jozufozu.flywheel.util.ModelReader; +import com.jozufozu.flywheel.util.RenderMath; import com.jozufozu.flywheel.util.transform.Rotate; import com.jozufozu.flywheel.util.transform.Scale; import com.jozufozu.flywheel.util.transform.TStack; @@ -31,10 +33,10 @@ import net.minecraftforge.client.model.pipeline.LightUtil; public class SuperByteBuffer implements Scale, Translate, Rotate, TStack { - private final BufferBuilderReader template; + private final ModelReader template; // Vertex Position - private PoseStack transforms; + private final PoseStack transforms; // Vertex Coloring private boolean shouldColor; @@ -115,9 +117,9 @@ public class SuperByteBuffer implements Scale, Translate, Translate