From 0b59067701d1d320f0a59254dbe21c0d6d9ce107 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 4 Jan 2024 13:41:47 -0800 Subject: [PATCH] Untyped instance types - Add SimpleInstanceType and builder. - Remove OrientedType and TransformedType classes in favor of builder. --- .../flywheel/lib/instance/InstanceTypes.java | 45 ++++++- .../flywheel/lib/instance/OrientedType.java | 67 ---------- .../lib/instance/SimpleInstanceType.java | 118 ++++++++++++++++++ .../lib/instance/TransformedInstance.java | 6 +- .../lib/instance/TransformedType.java | 65 ---------- 5 files changed, 163 insertions(+), 138 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java create mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/SimpleInstanceType.java delete mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java 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 e111b621c..4b66ca51e 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/InstanceTypes.java @@ -2,11 +2,52 @@ package com.jozufozu.flywheel.lib.instance; import org.jetbrains.annotations.ApiStatus; +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.layout.BufferLayout; +import com.jozufozu.flywheel.lib.layout.CommonItems; public final class InstanceTypes { - public static final InstanceType TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType()); - public static final InstanceType ORIENTED = InstanceType.REGISTRY.registerAndGet(new OrientedType()); + public static final InstanceType TRANSFORMED = SimpleInstanceType.builder(TransformedInstance::new) + .bufferLayout(BufferLayout.builder() + .addItem(CommonItems.LIGHT_COORD, "light") + .addItem(CommonItems.UNORM_4x8, "color") + .addItem(CommonItems.MAT4, "pose") + .addItem(CommonItems.MAT3, "normal") + .build()) + .layout(LayoutBuilder.create() + .vector("light", IntegerRepr.SHORT, 2) + .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) + .matrix("pose", FloatRepr.FLOAT, 4) + .matrix("normal", FloatRepr.FLOAT, 3) + .build()) + .writer(TransformedWriter.INSTANCE) + .vertexShader(Flywheel.rl("instance/transformed.vert")) + .cullShader(Flywheel.rl("instance/cull/transformed.glsl")) + .register(); + + public static final InstanceType ORIENTED = SimpleInstanceType.builder(OrientedInstance::new) + .bufferLayout(BufferLayout.builder() + .addItem(CommonItems.LIGHT_COORD, "light") + .addItem(CommonItems.UNORM_4x8, "color") + .addItem(CommonItems.VEC3, "position") + .addItem(CommonItems.VEC3, "pivot") + .addItem(CommonItems.VEC4, "rotation") + .build()) + .layout(LayoutBuilder.create() + .vector("light", IntegerRepr.SHORT, 2) + .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) + .vector("position", FloatRepr.FLOAT, 3) + .vector("pivot", FloatRepr.FLOAT, 3) + .vector("rotation", FloatRepr.FLOAT, 4) + .build()) + .writer(OrientedWriter.INSTANCE) + .vertexShader(Flywheel.rl("instance/oriented.vert")) + .cullShader(Flywheel.rl("instance/cull/oriented.glsl")) + .register(); private InstanceTypes() { } diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java b/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java deleted file mode 100644 index 86846dba5..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/OrientedType.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.jozufozu.flywheel.lib.instance; - -import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.instance.InstanceHandle; -import com.jozufozu.flywheel.api.instance.InstanceType; -import com.jozufozu.flywheel.api.instance.InstanceWriter; -import com.jozufozu.flywheel.api.layout.FloatRepr; -import com.jozufozu.flywheel.api.layout.IntegerRepr; -import com.jozufozu.flywheel.api.layout.Layout; -import com.jozufozu.flywheel.api.layout.LayoutBuilder; -import com.jozufozu.flywheel.lib.layout.BufferLayout; -import com.jozufozu.flywheel.lib.layout.CommonItems; - -import net.minecraft.resources.ResourceLocation; - -public class OrientedType implements InstanceType { - @Deprecated - private static final BufferLayout OLD_LAYOUT = BufferLayout.builder() - .addItem(CommonItems.LIGHT_COORD, "light") - .addItem(CommonItems.UNORM_4x8, "color") - .addItem(CommonItems.VEC3, "position") - .addItem(CommonItems.VEC3, "pivot") - .addItem(CommonItems.VEC4, "rotation") - .build(); - - private static final Layout LAYOUT = LayoutBuilder.create() - .vector("light", IntegerRepr.SHORT, 2) - .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) - .vector("position", FloatRepr.FLOAT, 3) - .vector("pivot", FloatRepr.FLOAT, 3) - .vector("rotation", FloatRepr.FLOAT, 4) - .build(); - - private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/oriented.vert"); - private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/oriented.glsl"); - - @Override - public OrientedInstance create(InstanceHandle handle) { - return new OrientedInstance(this, handle); - } - - @Override - @Deprecated - public BufferLayout oldLayout() { - return OLD_LAYOUT; - } - - @Override - public Layout layout() { - return LAYOUT; - } - - @Override - public InstanceWriter writer() { - return OrientedWriter.INSTANCE; - } - - @Override - public ResourceLocation vertexShader() { - return VERTEX_SHADER; - } - - @Override - public ResourceLocation cullShader() { - return CULL_SHADER; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleInstanceType.java b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleInstanceType.java new file mode 100644 index 000000000..dda1ec476 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleInstanceType.java @@ -0,0 +1,118 @@ +package com.jozufozu.flywheel.lib.instance; + +import java.util.Objects; + +import com.jozufozu.flywheel.api.instance.Instance; +import com.jozufozu.flywheel.api.instance.InstanceHandle; +import com.jozufozu.flywheel.api.instance.InstanceType; +import com.jozufozu.flywheel.api.instance.InstanceWriter; +import com.jozufozu.flywheel.api.layout.Layout; +import com.jozufozu.flywheel.lib.layout.BufferLayout; + +import net.minecraft.resources.ResourceLocation; + +public class SimpleInstanceType implements InstanceType { + private final Factory factory; + private final BufferLayout bufferLayout; + private final Layout layout; + private final InstanceWriter writer; + private final ResourceLocation vertexShader; + private final ResourceLocation cullShader; + + public SimpleInstanceType(Factory factory, BufferLayout bufferLayout, Layout layout, InstanceWriter writer, ResourceLocation vertexShader, ResourceLocation cullShader) { + this.factory = factory; + this.bufferLayout = bufferLayout; + this.layout = layout; + this.writer = writer; + this.vertexShader = vertexShader; + this.cullShader = cullShader; + } + + public static Builder builder(Factory factory) { + return new Builder<>(factory); + } + + @Override + public I create(InstanceHandle handle) { + return factory.create(this, handle); + } + + @Override + public BufferLayout oldLayout() { + return bufferLayout; + } + + @Override + public Layout layout() { + return layout; + } + + @Override + public InstanceWriter writer() { + return writer; + } + + @Override + public ResourceLocation vertexShader() { + return vertexShader; + } + + @Override + public ResourceLocation cullShader() { + return cullShader; + } + + @FunctionalInterface + public interface Factory { + I create(InstanceType type, InstanceHandle handle); + } + + public static class Builder { + private final Factory factory; + private BufferLayout bufferLayout; + private Layout layout; + private InstanceWriter writer; + private ResourceLocation vertexShader; + private ResourceLocation cullShader; + + public Builder(Factory factory) { + this.factory = factory; + } + + public Builder bufferLayout(BufferLayout bufferLayout) { + this.bufferLayout = bufferLayout; + return this; + } + + public Builder layout(Layout layout) { + this.layout = layout; + return this; + } + + public Builder writer(InstanceWriter writer) { + this.writer = writer; + return this; + } + + public Builder vertexShader(ResourceLocation vertexShader) { + this.vertexShader = vertexShader; + return this; + } + + public Builder cullShader(ResourceLocation cullShader) { + this.cullShader = cullShader; + return this; + } + + public SimpleInstanceType register() { + Objects.requireNonNull(bufferLayout); + Objects.requireNonNull(layout); + Objects.requireNonNull(writer); + Objects.requireNonNull(vertexShader); + Objects.requireNonNull(cullShader); + + var out = new SimpleInstanceType<>(factory, bufferLayout, layout, writer, vertexShader, cullShader); + return InstanceType.REGISTRY.registerAndGet(out); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java index 26b00258b..4443ca11c 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java @@ -12,8 +12,6 @@ import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.util.Mth; public class TransformedInstance extends ColoredLitInstance implements Transform { - private static final Matrix4f ZERO_MATRIX_4f = new Matrix4f(); - private static final Matrix3f ZERO_MATRIX_3f = new Matrix3f(); public final Matrix4f model = new Matrix4f(); public final Matrix3f normal = new Matrix3f(); @@ -83,8 +81,8 @@ public class TransformedInstance extends ColoredLitInstance implements Transform *

*/ public TransformedInstance setEmptyTransform() { - model.set(ZERO_MATRIX_4f); - normal.set(ZERO_MATRIX_3f); + model.zero(); + normal.zero(); return this; } diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java deleted file mode 100644 index 393ea11b0..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedType.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.jozufozu.flywheel.lib.instance; - -import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.instance.InstanceHandle; -import com.jozufozu.flywheel.api.instance.InstanceType; -import com.jozufozu.flywheel.api.instance.InstanceWriter; -import com.jozufozu.flywheel.api.layout.FloatRepr; -import com.jozufozu.flywheel.api.layout.IntegerRepr; -import com.jozufozu.flywheel.api.layout.Layout; -import com.jozufozu.flywheel.api.layout.LayoutBuilder; -import com.jozufozu.flywheel.lib.layout.BufferLayout; -import com.jozufozu.flywheel.lib.layout.CommonItems; - -import net.minecraft.resources.ResourceLocation; - -public class TransformedType implements InstanceType { - @Deprecated - private static final BufferLayout OLD_LAYOUT = BufferLayout.builder() - .addItem(CommonItems.LIGHT_COORD, "light") - .addItem(CommonItems.UNORM_4x8, "color") - .addItem(CommonItems.MAT4, "pose") - .addItem(CommonItems.MAT3, "normal") - .build(); - - private static final Layout LAYOUT = LayoutBuilder.create() - .vector("light", IntegerRepr.SHORT, 2) - .vector("color", FloatRepr.NORMALIZED_UNSIGNED_BYTE, 4) - .matrix("pose", FloatRepr.FLOAT, 4) - .matrix("normal", FloatRepr.FLOAT, 3) - .build(); - - private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/transformed.vert"); - private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/transformed.glsl"); - - @Override - public TransformedInstance create(InstanceHandle handle) { - return new TransformedInstance(this, handle); - } - - @Override - @Deprecated - public BufferLayout oldLayout() { - return OLD_LAYOUT; - } - - @Override - public Layout layout() { - return LAYOUT; - } - - @Override - public InstanceWriter writer() { - return TransformedWriter.INSTANCE; - } - - @Override - public ResourceLocation vertexShader() { - return VERTEX_SHADER; - } - - @Override - public ResourceLocation cullShader() { - return CULL_SHADER; - } -}