Batching Engine

- Implement alternate backend using SBBs
This commit is contained in:
Jozufozu 2021-12-15 00:35:49 -08:00
parent 34aa336683
commit 3e01d3f441
13 changed files with 207 additions and 61 deletions

View file

@ -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<BeltData>, Batched<BeltData> {
@ -34,7 +38,29 @@ public class BeltType implements Instanced<BeltData>, Batched<BeltData> {
}
@Override
public BatchingTransformer<BeltData> getTransformer(Model model) {
return null;
public BatchingTransformer<BeltData> 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);
}
};
}
}

View file

@ -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<RotatingData>, Batched<RotatingData> {
@ -34,7 +38,19 @@ public class RotatingType implements Instanced<RotatingData>, Batched<RotatingDa
}
@Override
public BatchingTransformer<RotatingData> getTransformer(Model model) {
return null;
public BatchingTransformer<RotatingData> 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);
}
};
}
}

View file

@ -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<BeltData> {
public class UnsafeBeltWriter extends UnsafeKineticWriter<BeltData> {
public UnsafeBeltWriter(VecBuffer backingBuffer, StructType<BeltData> 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<BeltData> {
MemoryUtil.memPutFloat(addr + 58, d.maxU);
MemoryUtil.memPutFloat(addr + 62, d.maxV);
MemoryUtil.memPutByte(addr + 66, d.scrollMult);
advance();
}
}

View file

@ -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<D extends KineticData> extends UnsafeBasicWriter<D> {
public UnsafeKineticWriter(VecBuffer backingBuffer, StructType<D> 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);
}
}

View file

@ -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<RotatingData> {
public class UnsafeRotatingWriter extends UnsafeKineticWriter<RotatingData> {
public UnsafeRotatingWriter(VecBuffer backingBuffer, StructType<RotatingData> 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();
}
}

View file

@ -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<ActorData>, Batched<ActorData> {
}
@Override
public BatchingTransformer<ActorData> getTransformer(Model model) {
return null;
public BatchingTransformer<ActorData> getTransformer() {
return (d, sbb) -> {
};
}
}

View file

@ -12,7 +12,7 @@ public class UnsafeActorWriter extends UnsafeBufferWriter<ActorData> {
}
@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<ActorData> {
MemoryUtil.memPutByte(addr + 38, d.rotationCenterY);
MemoryUtil.memPutByte(addr + 39, d.rotationCenterZ);
MemoryUtil.memPutFloat(addr + 40, d.speed);
advance();
}
}

View file

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

View file

@ -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<FlapData> {
@ -42,18 +43,23 @@ public class FlapData extends InstanceData implements FlatLit<FlapData> {
@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;

View file

@ -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<FlapData>, Batched<FlapData> {
@ -34,7 +35,29 @@ public class FlapType implements Instanced<FlapData>, Batched<FlapData> {
}
@Override
public BatchingTransformer<FlapData> getTransformer(Model model) {
return null;
public BatchingTransformer<FlapData> 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;
}
}
}

View file

@ -12,13 +12,13 @@ public class UnsafeFlapWriter extends UnsafeBufferWriter<FlapData> {
}
@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<FlapData> {
MemoryUtil.memPutFloat(addr + 42, d.intensity);
MemoryUtil.memPutFloat(addr + 46, d.flapScale);
MemoryUtil.memPutFloat(addr + 50, d.flapness);
advance();
}
}

View file

@ -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<SuperByteBuffer>, Translate<SuperByteBuffer>, Rotate<SuperByteBuffer>, TStack<SuperByteBuffer> {
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<SuperByteBuffer>, Translate<SuperB
byte g = template.getG(i);
byte b = template.getB(i);
byte a = template.getA(i);
float normalX = template.getNX(i) / 127f;
float normalY = template.getNY(i) / 127f;
float normalZ = template.getNZ(i) / 127f;
float normalX = template.getNX(i);
float normalY = template.getNY(i);
float normalZ = template.getNZ(i);
normal.set(normalX, normalY, normalZ);
normal.transform(normalMat);
@ -132,6 +134,7 @@ public class SuperByteBuffer implements Scale<SuperByteBuffer>, Translate<SuperB
pos.transform(modelMat);
builder.vertex(pos.x(), pos.y(), pos.z());
//builder.color(nx, ny, nz, 1f);
if (shouldColor) {
if (disableDiffuseMult) {
builder.color(this.r, this.g, this.b, this.a);