From 89f0e547173f9c8d893c86bd0011eacf20b8aa13 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 8 Jan 2024 16:50:22 -0800 Subject: [PATCH] Write me - Convert InstanceWriters into lambdas passed directly to the builder. - It's very nice to have everything in one place like that, will make tweaking the layout much easier. - Also micro-optimize the cull shader for TransformedInstance. --- .../lib/instance/ColoredLitWriter.java | 17 ---------- .../flywheel/lib/instance/InstanceTypes.java | 32 +++++++++++++++++-- .../flywheel/lib/instance/OrientedWriter.java | 24 -------------- .../lib/instance/TransformedWriter.java | 14 -------- .../flywheel/instance/cull/transformed.glsl | 9 +++++- 5 files changed, 38 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/ColoredLitWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/OrientedWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/ColoredLitWriter.java b/src/main/java/com/jozufozu/flywheel/lib/instance/ColoredLitWriter.java deleted file mode 100644 index 1b9d612e7..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/ColoredLitWriter.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jozufozu.flywheel.lib.instance; - -import org.lwjgl.system.MemoryUtil; - -import com.jozufozu.flywheel.api.instance.InstanceWriter; - -public abstract class ColoredLitWriter implements InstanceWriter { - @Override - public void write(final long ptr, final I instance) { - MemoryUtil.memPutShort(ptr, instance.blockLight); - MemoryUtil.memPutShort(ptr + 2, instance.skyLight); - MemoryUtil.memPutByte(ptr + 4, instance.r); - MemoryUtil.memPutByte(ptr + 5, instance.g); - MemoryUtil.memPutByte(ptr + 6, instance.b); - MemoryUtil.memPutByte(ptr + 7, instance.a); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java index f0c3a6c39..c937de7fc 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java @@ -1,12 +1,14 @@ package com.jozufozu.flywheel.lib.instance; import org.jetbrains.annotations.ApiStatus; +import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.layout.FloatRepr; import com.jozufozu.flywheel.api.layout.IntegerRepr; import com.jozufozu.flywheel.api.layout.LayoutBuilder; +import com.jozufozu.flywheel.lib.math.MatrixMath; public final class InstanceTypes { public static final InstanceType TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new) @@ -16,7 +18,16 @@ public final class InstanceTypes { .matrix("pose", FloatRepr.FLOAT, 4) .matrix("normal", FloatRepr.FLOAT, 3) .build()) - .writer(TransformedWriter.INSTANCE) + .writer((ptr, instance) -> { + MemoryUtil.memPutShort(ptr, instance.blockLight); + MemoryUtil.memPutShort(ptr + 2, instance.skyLight); + MemoryUtil.memPutByte(ptr + 4, instance.r); + MemoryUtil.memPutByte(ptr + 5, instance.g); + MemoryUtil.memPutByte(ptr + 6, instance.b); + MemoryUtil.memPutByte(ptr + 7, instance.a); + MatrixMath.writeUnsafe(instance.model, ptr + 8); + MatrixMath.writeUnsafe(instance.normal, ptr + 72); + }) .vertexShader(Flywheel.rl("instance/transformed.vert")) .cullShader(Flywheel.rl("instance/cull/transformed.glsl")) .register(); @@ -29,7 +40,24 @@ public final class InstanceTypes { .vector("pivot", FloatRepr.FLOAT, 3) .vector("rotation", FloatRepr.FLOAT, 4) .build()) - .writer(OrientedWriter.INSTANCE) + .writer((ptr, instance) -> { + MemoryUtil.memPutShort(ptr, instance.blockLight); + MemoryUtil.memPutShort(ptr + 2, instance.skyLight); + MemoryUtil.memPutByte(ptr + 4, instance.r); + MemoryUtil.memPutByte(ptr + 5, instance.g); + MemoryUtil.memPutByte(ptr + 6, instance.b); + MemoryUtil.memPutByte(ptr + 7, instance.a); + MemoryUtil.memPutFloat(ptr + 8, instance.posX); + MemoryUtil.memPutFloat(ptr + 12, instance.posY); + MemoryUtil.memPutFloat(ptr + 16, instance.posZ); + MemoryUtil.memPutFloat(ptr + 20, instance.pivotX); + MemoryUtil.memPutFloat(ptr + 24, instance.pivotY); + MemoryUtil.memPutFloat(ptr + 28, instance.pivotZ); + MemoryUtil.memPutFloat(ptr + 32, instance.rotation.x); + MemoryUtil.memPutFloat(ptr + 36, instance.rotation.y); + MemoryUtil.memPutFloat(ptr + 40, instance.rotation.z); + MemoryUtil.memPutFloat(ptr + 44, instance.rotation.w); + }) .vertexShader(Flywheel.rl("instance/oriented.vert")) .cullShader(Flywheel.rl("instance/cull/oriented.glsl")) .register(); diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedWriter.java b/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedWriter.java deleted file mode 100644 index fe127e055..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedWriter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jozufozu.flywheel.lib.instance; - -import org.lwjgl.system.MemoryUtil; - -public class OrientedWriter extends ColoredLitWriter { - public static final OrientedWriter INSTANCE = new OrientedWriter(); - - @Override - public void write(long ptr, OrientedInstance instance) { - super.write(ptr, instance); - - MemoryUtil.memPutFloat(ptr + 8, instance.posX); - MemoryUtil.memPutFloat(ptr + 12, instance.posY); - MemoryUtil.memPutFloat(ptr + 16, instance.posZ); - MemoryUtil.memPutFloat(ptr + 20, instance.pivotX); - MemoryUtil.memPutFloat(ptr + 24, instance.pivotY); - MemoryUtil.memPutFloat(ptr + 28, instance.pivotZ); - MemoryUtil.memPutFloat(ptr + 32, instance.rotation.x); - MemoryUtil.memPutFloat(ptr + 36, instance.rotation.y); - MemoryUtil.memPutFloat(ptr + 40, instance.rotation.z); - MemoryUtil.memPutFloat(ptr + 44, instance.rotation.w); - } - -} diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java deleted file mode 100644 index a1406fcfe..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedWriter.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jozufozu.flywheel.lib.instance; - -import com.jozufozu.flywheel.lib.math.MatrixMath; - -public class TransformedWriter extends ColoredLitWriter { - public static final TransformedWriter INSTANCE = new TransformedWriter(); - - @Override - public void write(final long ptr, final TransformedInstance instance) { - super.write(ptr, instance); - MatrixMath.writeUnsafe(instance.model, ptr + 8); - MatrixMath.writeUnsafe(instance.normal, ptr + 72); - } -} diff --git a/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl index db7b66341..8b70b9b44 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl +++ b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl @@ -2,6 +2,13 @@ void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout floa mat4 pose = i.pose; center = (pose * vec4(center, 1.0)).xyz; - float scale = max(length(pose[0].xyz), max(length(pose[1].xyz), length(pose[2].xyz))); + vec3 c0 = pose[0].xyz; + vec3 c1 = pose[1].xyz; + vec3 c2 = pose[2].xyz; + + // Comute the squared maximum to avoid 2 unnecessary sqrts. + // I don't think this makes it any faster but why not /shrug + float scaleSqr = max(dot(c0, c0), max(dot(c1, c1), dot(c2, c2))); + float scale = sqrt(scaleSqr); radius *= scale; }