- 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.
This commit is contained in:
Jozufozu 2024-01-08 16:50:22 -08:00
parent 381288da52
commit 89f0e54717
5 changed files with 38 additions and 58 deletions

View file

@ -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<I extends ColoredLitInstance> implements InstanceWriter<I> {
@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);
}
}

View file

@ -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<TransformedInstance> 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();

View file

@ -1,24 +0,0 @@
package com.jozufozu.flywheel.lib.instance;
import org.lwjgl.system.MemoryUtil;
public class OrientedWriter extends ColoredLitWriter<OrientedInstance> {
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);
}
}

View file

@ -1,14 +0,0 @@
package com.jozufozu.flywheel.lib.instance;
import com.jozufozu.flywheel.lib.math.MatrixMath;
public class TransformedWriter extends ColoredLitWriter<TransformedInstance> {
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);
}
}

View file

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