Layout on me, dev

- Move gl-coupled BufferLayout and co. to lib.
- Add new Layout record to API.
  - List of Elements, a sealed interface of type safe records
    representing most possible vertex attributes.
- Deprecate InstanceType#getLayout.
- Add LayoutBuilder in lib for convenience.
- TODO: use new Layouts in backends.
This commit is contained in:
Jozufozu 2023-12-13 13:40:31 -08:00
parent 867d0fa304
commit c79e94cd18
19 changed files with 216 additions and 14 deletions

View file

@ -1,8 +1,9 @@
package com.jozufozu.flywheel.api.instance;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.layout.Layout;
import com.jozufozu.flywheel.api.registry.Registry;
import com.jozufozu.flywheel.impl.RegistryImpl;
import com.jozufozu.flywheel.lib.layout.BufferLayout;
import net.minecraft.resources.ResourceLocation;
@ -22,9 +23,13 @@ public interface InstanceType<I extends Instance> {
/**
* @return The layout of I when buffered.
* @deprecated Use {@link #layout()} instead.
*/
@Deprecated
BufferLayout getLayout();
Layout layout();
InstanceWriter<I> getWriter();
ResourceLocation vertexShader();

View file

@ -0,0 +1,54 @@
package com.jozufozu.flywheel.api.layout;
/**
* A single element in a {@link Layout}.
*/
public sealed interface Element {
String name();
/**
* A simple vector of floats, i.e. {@code vec3}, {@code dvec2}.
* <br>
* If {@link #type} is an integral type, the shader will implicitly convert it to a float without normalization.
* If you want normalization, use {@link NormalizedVector}.
*
* @param name The name of the element to be used in the shader.
* @param type The backing type of the element.
* @param size The number of components in the vector.
*/
record Vector(String name, FloatType type, VectorSize size) implements Element {
}
/**
* A vector of integers, i.e. {@code ivec3}, {@code uvec2}.
* <br>
* All backing types will be presented as either {@code int} or {@code uint} in the shader,
* depending on the signedness of the type.
*
* @param name The name of the element to be used in the shader.
* @param type The backing type of the element.
* @param size The number of components in the vector.
*/
record IntegerVector(String name, IntegerType type, VectorSize size) implements Element {
}
/**
* A vector of integers, normalized and presented as a float in the shader.
*
* @param name The name of the element to be used in the shader.
* @param type The backing type of the element.
* @param size The number of components in the vector.
*/
record NormalizedVector(String name, IntegerType type, VectorSize size) implements Element {
}
/**
* A matrix of 32-bit floating point numbers, i.e. {@code mat3}, {@code mat2x4}.
*
* @param name The name of the element to be used in the shader.
* @param rows The number of rows in the matrix.
* @param cols The number of columns in the matrix.
*/
record Matrix(String name, MatrixSize rows, MatrixSize cols) implements Element {
}
}

View file

@ -0,0 +1,19 @@
package com.jozufozu.flywheel.api.layout;
/**
* The backing type of traditional floating point vertex attributes.
* <br>
* Integral types are implicitly converted to floats in the shader.
*/
public enum FloatType {
BYTE,
UNSIGNED_BYTE,
SHORT,
UNSIGNED_SHORT,
INT,
UNSIGNED_INT,
HALF_FLOAT,
FLOAT,
DOUBLE,
FIXED,
}

View file

@ -0,0 +1,13 @@
package com.jozufozu.flywheel.api.layout;
/**
* Integral types backing layout elements.
*/
public enum IntegerType {
BYTE,
UNSIGNED_BYTE,
SHORT,
UNSIGNED_SHORT,
INT,
UNSIGNED_INT
}

View file

@ -0,0 +1,6 @@
package com.jozufozu.flywheel.api.layout;
import java.util.List;
public record Layout(List<Element> elements) {
}

View file

@ -0,0 +1,10 @@
package com.jozufozu.flywheel.api.layout;
/**
* The size of a single dimension in a matrix.
*/
public enum MatrixSize {
TWO,
THREE,
FOUR,
}

View file

@ -0,0 +1,13 @@
package com.jozufozu.flywheel.api.layout;
/**
* The number of components in a vector.
* <br>
* If the vector size is 1, the attribute is presented as a scalar.
*/
public enum VectorSize {
ONE,
TWO,
THREE,
FOUR,
}

View file

@ -1,8 +1,8 @@
package com.jozufozu.flywheel.backend;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.vertex.VertexView;
import com.jozufozu.flywheel.lib.layout.BufferLayout;
import com.jozufozu.flywheel.lib.layout.CommonItems;
import com.jozufozu.flywheel.lib.vertex.FullVertexView;

View file

@ -6,13 +6,13 @@ import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.layout.LayoutItem;
import com.jozufozu.flywheel.backend.compile.Pipeline;
import com.jozufozu.flywheel.glsl.SourceComponent;
import com.jozufozu.flywheel.glsl.generate.FnSignature;
import com.jozufozu.flywheel.glsl.generate.GlslBlock;
import com.jozufozu.flywheel.glsl.generate.GlslBuilder;
import com.jozufozu.flywheel.glsl.generate.GlslExpr;
import com.jozufozu.flywheel.lib.layout.LayoutItem;
import net.minecraft.resources.ResourceLocation;

View file

@ -5,13 +5,13 @@ import java.util.Collections;
import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.layout.LayoutItem;
import com.jozufozu.flywheel.backend.compile.Pipeline;
import com.jozufozu.flywheel.glsl.SourceComponent;
import com.jozufozu.flywheel.glsl.generate.FnSignature;
import com.jozufozu.flywheel.glsl.generate.GlslBlock;
import com.jozufozu.flywheel.glsl.generate.GlslBuilder;
import com.jozufozu.flywheel.glsl.generate.GlslExpr;
import com.jozufozu.flywheel.lib.layout.LayoutItem;
import net.minecraft.resources.ResourceLocation;

View file

@ -9,12 +9,12 @@ import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.Instance;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.InstanceWriter;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.backend.engine.AbstractInstancer;
import com.jozufozu.flywheel.gl.array.GlVertexArray;
import com.jozufozu.flywheel.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.gl.buffer.GlBufferUsage;
import com.jozufozu.flywheel.gl.buffer.MappedBuffer;
import com.jozufozu.flywheel.lib.layout.BufferLayout;
public class InstancedInstancer<I extends Instance> extends AbstractInstancer<I> {
private final BufferLayout instanceFormat;

View file

@ -9,15 +9,20 @@ import com.jozufozu.flywheel.api.instance.InstanceHandle;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.InstanceVertexTransformer;
import com.jozufozu.flywheel.api.instance.InstanceWriter;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.layout.FloatType;
import com.jozufozu.flywheel.api.layout.IntegerType;
import com.jozufozu.flywheel.api.layout.Layout;
import com.jozufozu.flywheel.api.layout.VectorSize;
import com.jozufozu.flywheel.lib.layout.BufferLayout;
import com.jozufozu.flywheel.lib.layout.CommonItems;
import com.jozufozu.flywheel.lib.layout.LayoutBuilder;
import com.jozufozu.flywheel.lib.math.RenderMath;
import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
import net.minecraft.resources.ResourceLocation;
public class OrientedType implements InstanceType<OrientedInstance> {
private static final BufferLayout LAYOUT = BufferLayout.builder()
private static final BufferLayout OLD_LAYOUT = BufferLayout.builder()
.addItem(CommonItems.LIGHT_COORD, "light")
.addItem(CommonItems.UNORM_4x8, "color")
.addItem(CommonItems.VEC3, "position")
@ -25,6 +30,14 @@ public class OrientedType implements InstanceType<OrientedInstance> {
.addItem(CommonItems.VEC4, "rotation")
.build();
private static final Layout LAYOUT = LayoutBuilder.of()
.integer("light", IntegerType.SHORT, VectorSize.TWO)
.normalized("color", IntegerType.BYTE, VectorSize.FOUR)
.vector("position", FloatType.FLOAT, VectorSize.THREE)
.vector("pivot", FloatType.FLOAT, VectorSize.THREE)
.vector("rotation", FloatType.FLOAT, VectorSize.FOUR)
.build();
private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/oriented.vert");
private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/oriented.glsl");
@ -35,6 +48,11 @@ public class OrientedType implements InstanceType<OrientedInstance> {
@Override
public BufferLayout getLayout() {
return OLD_LAYOUT;
}
@Override
public Layout layout() {
return LAYOUT;
}

View file

@ -6,8 +6,13 @@ import com.jozufozu.flywheel.api.instance.InstanceHandle;
import com.jozufozu.flywheel.api.instance.InstanceType;
import com.jozufozu.flywheel.api.instance.InstanceVertexTransformer;
import com.jozufozu.flywheel.api.instance.InstanceWriter;
import com.jozufozu.flywheel.api.layout.BufferLayout;
import com.jozufozu.flywheel.api.layout.IntegerType;
import com.jozufozu.flywheel.api.layout.Layout;
import com.jozufozu.flywheel.api.layout.MatrixSize;
import com.jozufozu.flywheel.api.layout.VectorSize;
import com.jozufozu.flywheel.lib.layout.BufferLayout;
import com.jozufozu.flywheel.lib.layout.CommonItems;
import com.jozufozu.flywheel.lib.layout.LayoutBuilder;
import com.jozufozu.flywheel.lib.math.MatrixMath;
import com.jozufozu.flywheel.lib.math.RenderMath;
import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
@ -15,13 +20,20 @@ import com.jozufozu.flywheel.lib.vertex.VertexTransformations;
import net.minecraft.resources.ResourceLocation;
public class TransformedType implements InstanceType<TransformedInstance> {
private static final BufferLayout LAYOUT = BufferLayout.builder()
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();
public static final Layout LAYOUT = LayoutBuilder.of()
.integer("light", IntegerType.SHORT, VectorSize.TWO)
.normalized("color", IntegerType.BYTE, VectorSize.FOUR)
.mat("pose", MatrixSize.FOUR)
.mat("normal", MatrixSize.THREE)
.build();
private static final ResourceLocation VERTEX_SHADER = Flywheel.rl("instance/transformed.vert");
private static final ResourceLocation CULL_SHADER = Flywheel.rl("instance/cull/transformed.glsl");
@ -32,6 +44,11 @@ public class TransformedType implements InstanceType<TransformedInstance> {
@Override
public BufferLayout getLayout() {
return OLD_LAYOUT;
}
@Override
public Layout layout() {
return LAYOUT;
}

View file

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.api.layout;
package com.jozufozu.flywheel.lib.layout;
import java.util.List;

View file

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.api.layout;
package com.jozufozu.flywheel.lib.layout;
import java.util.function.Consumer;

View file

@ -0,0 +1,49 @@
package com.jozufozu.flywheel.lib.layout;
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.ImmutableList;
import com.jozufozu.flywheel.api.layout.Element;
import com.jozufozu.flywheel.api.layout.FloatType;
import com.jozufozu.flywheel.api.layout.IntegerType;
import com.jozufozu.flywheel.api.layout.Layout;
import com.jozufozu.flywheel.api.layout.MatrixSize;
import com.jozufozu.flywheel.api.layout.VectorSize;
public class LayoutBuilder {
private final List<Element> elements = new ArrayList<>();
public static LayoutBuilder of() {
return new LayoutBuilder();
}
public Layout build() {
return new Layout(ImmutableList.copyOf(elements));
}
public LayoutBuilder element(Element element) {
elements.add(element);
return this;
}
public LayoutBuilder integer(String name, IntegerType type, VectorSize size) {
return element(new Element.IntegerVector(name, type, size));
}
public LayoutBuilder normalized(String name, IntegerType type, VectorSize size) {
return element(new Element.NormalizedVector(name, type, size));
}
public LayoutBuilder vector(String name, FloatType type, VectorSize size) {
return element(new Element.Vector(name, type, size));
}
public LayoutBuilder mat(String name, MatrixSize rows, MatrixSize cols) {
return element(new Element.Matrix(name, rows, cols));
}
public LayoutBuilder mat(String name, MatrixSize size) {
return mat(name, size, size);
}
}

View file

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.api.layout;
package com.jozufozu.flywheel.lib.layout;
public record LayoutItem(InputType type, String name) {

View file

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import com.jozufozu.flywheel.api.layout.InputType;
import com.jozufozu.flywheel.gl.GlNumericType;
import com.jozufozu.flywheel.gl.array.VertexAttribute;
import com.jozufozu.flywheel.glsl.generate.FnSignature;

View file

@ -3,7 +3,6 @@ package com.jozufozu.flywheel.lib.layout;
import java.util.function.Consumer;
import java.util.function.Function;
import com.jozufozu.flywheel.api.layout.InputType;
import com.jozufozu.flywheel.gl.array.VertexAttribute;
import com.jozufozu.flywheel.glsl.generate.GlslBuilder;
import com.jozufozu.flywheel.glsl.generate.GlslExpr;