Untyped instance types

- Add SimpleInstanceType and builder.
- Remove OrientedType and TransformedType classes in favor of builder.
This commit is contained in:
Jozufozu 2024-01-04 13:41:47 -08:00
parent 7aa0e659d6
commit 0b59067701
5 changed files with 163 additions and 138 deletions

View file

@ -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<TransformedInstance> TRANSFORMED = InstanceType.REGISTRY.registerAndGet(new TransformedType());
public static final InstanceType<OrientedInstance> ORIENTED = InstanceType.REGISTRY.registerAndGet(new OrientedType());
public static final InstanceType<TransformedInstance> 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<OrientedInstance> 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() {
}

View file

@ -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<OrientedInstance> {
@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<OrientedInstance> writer() {
return OrientedWriter.INSTANCE;
}
@Override
public ResourceLocation vertexShader() {
return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
}
}

View file

@ -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<I extends Instance> implements InstanceType<I> {
private final Factory<I> factory;
private final BufferLayout bufferLayout;
private final Layout layout;
private final InstanceWriter<I> writer;
private final ResourceLocation vertexShader;
private final ResourceLocation cullShader;
public SimpleInstanceType(Factory<I> factory, BufferLayout bufferLayout, Layout layout, InstanceWriter<I> 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 <I extends Instance> Builder<I> builder(Factory<I> 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<I> writer() {
return writer;
}
@Override
public ResourceLocation vertexShader() {
return vertexShader;
}
@Override
public ResourceLocation cullShader() {
return cullShader;
}
@FunctionalInterface
public interface Factory<I extends Instance> {
I create(InstanceType<I> type, InstanceHandle handle);
}
public static class Builder<I extends Instance> {
private final Factory<I> factory;
private BufferLayout bufferLayout;
private Layout layout;
private InstanceWriter<I> writer;
private ResourceLocation vertexShader;
private ResourceLocation cullShader;
public Builder(Factory<I> factory) {
this.factory = factory;
}
public Builder<I> bufferLayout(BufferLayout bufferLayout) {
this.bufferLayout = bufferLayout;
return this;
}
public Builder<I> layout(Layout layout) {
this.layout = layout;
return this;
}
public Builder<I> writer(InstanceWriter<I> writer) {
this.writer = writer;
return this;
}
public Builder<I> vertexShader(ResourceLocation vertexShader) {
this.vertexShader = vertexShader;
return this;
}
public Builder<I> cullShader(ResourceLocation cullShader) {
this.cullShader = cullShader;
return this;
}
public SimpleInstanceType<I> 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);
}
}
}

View file

@ -12,8 +12,6 @@ import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.util.Mth;
public class TransformedInstance extends ColoredLitInstance implements Transform<TransformedInstance> {
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
* </p>
*/
public TransformedInstance setEmptyTransform() {
model.set(ZERO_MATRIX_4f);
normal.set(ZERO_MATRIX_3f);
model.zero();
normal.zero();
return this;
}

View file

@ -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<TransformedInstance> {
@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<TransformedInstance> writer() {
return TransformedWriter.INSTANCE;
}
@Override
public ResourceLocation vertexShader() {
return VERTEX_SHADER;
}
@Override
public ResourceLocation cullShader() {
return CULL_SHADER;
}
}