No more material spec, everything is StructType

This commit is contained in:
Jozufozu 2021-12-09 16:34:32 -08:00
parent eee9b3fb51
commit 5374baf241
28 changed files with 101 additions and 90 deletions

View File

@ -1,7 +1,5 @@
package com.jozufozu.flywheel.api;
import net.minecraft.world.level.Level;
/**
* Something (a BlockEntity or Entity) that can be rendered using the instancing API.
*/
@ -13,6 +11,4 @@ public interface FlywheelRendered {
default boolean shouldRenderNormally() {
return false;
}
Level getWorld();
}

View File

@ -1,5 +1,7 @@
package com.jozufozu.flywheel.api;
import com.jozufozu.flywheel.api.struct.StructType;
public interface MaterialGroup {
/**
* Get the material as defined by the given {@link MaterialSpec spec}.
@ -8,5 +10,5 @@ public interface MaterialGroup {
* @param <D> The type representing the per instance data.
* @return A material you can use to render models.
*/
<D extends InstanceData> Material<D> material(MaterialSpec<D> spec);
<D extends InstanceData> Material<D> material(StructType<D> spec);
}

View File

@ -1,28 +0,0 @@
package com.jozufozu.flywheel.api;
import com.jozufozu.flywheel.backend.struct.StructType;
import net.minecraft.resources.ResourceLocation;
public class MaterialSpec<D extends InstanceData> {
public final ResourceLocation name;
private final ResourceLocation programSpec;
private final StructType<D> instanceType;
public MaterialSpec(ResourceLocation name, ResourceLocation programSpec, StructType<D> type) {
this.name = name;
this.programSpec = programSpec;
this.instanceType = type;
}
public ResourceLocation getProgramName() {
return programSpec;
}
public StructType<D> getInstanceType() {
return instanceType;
}
}

View File

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.backend.struct;
package com.jozufozu.flywheel.api.struct;
import com.jozufozu.flywheel.core.model.Model;

View File

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.backend.struct;
package com.jozufozu.flywheel.api.struct;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;

View File

@ -1,8 +1,10 @@
package com.jozufozu.flywheel.backend.struct;
package com.jozufozu.flywheel.api.struct;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
public interface Writeable<S> extends StructType<S> {
import net.minecraft.resources.ResourceLocation;
public interface Instanced<S> extends StructType<S> {
/**
* Create a {@link StructWriter} that will consume instances of S and write them to the given buffer.
*
@ -10,8 +12,10 @@ public interface Writeable<S> extends StructType<S> {
*/
StructWriter<S> getWriter(VecBuffer backing);
ResourceLocation getProgramSpec();
@Override
default Writeable<S> asWriteable() {
default Instanced<S> asInstanced() {
return this;
}
}

View File

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.backend.struct;
package com.jozufozu.flywheel.api.struct;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
@ -18,7 +18,7 @@ public interface StructType<S> {
*/
VertexFormat format();
Writeable<S> asWriteable();
Instanced<S> asInstanced();
Batched<S> asBatched();
}

View File

@ -1,4 +1,4 @@
package com.jozufozu.flywheel.backend.struct;
package com.jozufozu.flywheel.api.struct;
/**
* StructWriters can quickly consume many instances of S and write them to some backing buffer.

View File

@ -0,0 +1,6 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.jozufozu.flywheel.api.struct;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;

View File

@ -17,6 +17,7 @@ import com.jozufozu.flywheel.api.FlywheelWorld;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
@ -43,7 +44,7 @@ public class Backend {
private boolean enabled;
private final List<ShaderContext<?>> contexts = new ArrayList<>();
private final Map<ResourceLocation, MaterialSpec<?>> materialRegistry = new HashMap<>();
private final Map<ResourceLocation, StructType<?>> materialRegistry = new HashMap<>();
private final Map<ResourceLocation, ProgramSpec> programSpecRegistry = new HashMap<>();
protected Backend() {
@ -91,14 +92,13 @@ public class Backend {
/**
* Register an instancing material.
*/
public <D extends InstanceData> MaterialSpec<D> register(MaterialSpec<D> spec) {
ResourceLocation name = spec.name;
public <D extends InstanceData> StructType<D> register(ResourceLocation name, StructType<D> spec) {
if (materialRegistry.containsKey(name)) {
throw new IllegalStateException("Material spec '" + name + "' already registered.");
}
materialRegistry.put(name, spec);
log.debug("registered material '" + name + "' with instance size " + spec.getInstanceType().format().getStride());
log.debug("registered material '" + name + "' with instance size " + spec.format().getStride());
return spec;
}
@ -143,7 +143,7 @@ public class Backend {
return canUseInstancing() && isFlywheelWorld(world);
}
public Collection<MaterialSpec<?>> allMaterials() {
public Collection<StructType<?>> allMaterials() {
return materialRegistry.values();
}

View File

@ -5,7 +5,7 @@ import java.util.BitSet;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.Instancer;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.model.Model;
public abstract class AbstractInstancer<D extends InstanceData> implements Instancer<D> {

View File

@ -8,7 +8,7 @@ import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.Instancer;
import com.jozufozu.flywheel.api.Material;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.model.Model;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
@ -18,8 +18,8 @@ public class BatchedMaterial<D extends InstanceData> implements Material<D> {
protected final Map<Object, CPUInstancer<D>> models;
private final StructType<D> type;
public BatchedMaterial(MaterialSpec<D> spec) {
type = spec.getInstanceType();
public BatchedMaterial(StructType<D> type) {
this.type = type;
this.models = new HashMap<>();
}

View File

@ -6,6 +6,7 @@ import java.util.Map;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.MaterialGroup;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.api.struct.StructType;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
@ -16,7 +17,7 @@ public class BatchedMaterialGroup implements MaterialGroup {
protected final RenderType state;
private final Map<MaterialSpec<?>, BatchedMaterial<?>> materials = new HashMap<>();
private final Map<StructType<? extends InstanceData>, BatchedMaterial<?>> materials = new HashMap<>();
public BatchedMaterialGroup(RenderType state) {
this.state = state;
@ -30,7 +31,7 @@ public class BatchedMaterialGroup implements MaterialGroup {
*/
@SuppressWarnings("unchecked")
@Override
public <D extends InstanceData> BatchedMaterial<D> material(MaterialSpec<D> spec) {
public <D extends InstanceData> BatchedMaterial<D> material(StructType<D> spec) {
return (BatchedMaterial<D>) materials.computeIfAbsent(spec, BatchedMaterial::new);
}

View File

@ -2,8 +2,8 @@ package com.jozufozu.flywheel.backend.instancing.batching;
import com.jozufozu.flywheel.backend.instancing.AbstractInstancer;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.backend.struct.BatchingTransformer;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.BatchingTransformer;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.model.Model;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;

View File

@ -13,8 +13,8 @@ import com.jozufozu.flywheel.backend.instancing.AbstractInstancer;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.backend.model.IBufferedModel;
import com.jozufozu.flywheel.backend.model.ModelAllocator;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.backend.struct.StructWriter;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.core.model.Model;
import com.jozufozu.flywheel.util.AttribUtil;
@ -161,7 +161,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
if (length > 0) {
MappedBuffer mapped = instanceVBO.getBuffer(offset, length);
StructWriter<D> writer = type.asWriteable().getWriter(mapped);
StructWriter<D> writer = type.asInstanced().getWriter(mapped);
dirtySet.stream()
.forEach(i -> {
@ -181,7 +181,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
instanceVBO.alloc(glBufferSize);
MappedBuffer buffer = instanceVBO.getBuffer(0, glBufferSize);
StructWriter<D> writer = type.asWriteable().getWriter(buffer);
StructWriter<D> writer = type.asInstanced().getWriter(buffer);
for (D datum : data) {
writer.write(datum);
}

View File

@ -11,7 +11,7 @@ import com.jozufozu.flywheel.api.Instancer;
import com.jozufozu.flywheel.api.Material;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.backend.model.ModelPool;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.model.Model;
@ -25,8 +25,8 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
protected final Cache<Object, GPUInstancer<D>> models;
protected final StructType<D> type;
public InstancedMaterial(MaterialSpec<D> spec) {
this.type = spec.getInstanceType();
public InstancedMaterial(StructType<D> spec) {
this.type = spec;
modelPool = new ModelPool(Formats.UNLIT_MODEL, 64);
this.models = CacheBuilder.newBuilder()

View File

@ -7,6 +7,7 @@ import java.util.Map;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.MaterialGroup;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.shader.WorldProgram;
import com.jozufozu.flywheel.util.TextureBinder;
import com.mojang.math.Matrix4f;
@ -26,7 +27,7 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
protected final ArrayList<InstancedMaterialRenderer<P>> renderers = new ArrayList<>();
private final Map<MaterialSpec<?>, InstancedMaterial<?>> materials = new HashMap<>();
private final Map<StructType<? extends InstanceData>, InstancedMaterial<?>> materials = new HashMap<>();
public InstancedMaterialGroup(InstancingEngine<P> owner, RenderType type) {
this.owner = owner;
@ -41,7 +42,7 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
*/
@SuppressWarnings("unchecked")
@Override
public <D extends InstanceData> InstancedMaterial<D> material(MaterialSpec<D> spec) {
public <D extends InstanceData> InstancedMaterial<D> material(StructType<D> spec) {
return (InstancedMaterial<D>) materials.computeIfAbsent(spec, this::createInstanceMaterial);
}
@ -70,10 +71,11 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
renderers.clear();
}
private InstancedMaterial<?> createInstanceMaterial(MaterialSpec<?> type) {
private InstancedMaterial<?> createInstanceMaterial(StructType<? extends InstanceData> type) {
InstancedMaterial<?> material = new InstancedMaterial<>(type);
this.renderers.add(new InstancedMaterialRenderer<>(owner.getProgram(type.getProgramName()), material, this::setup));
this.renderers.add(new InstancedMaterialRenderer<>(owner.getProgram(type.asInstanced()
.getProgramSpec()), material, this::setup));
return material;
}

View File

@ -1,5 +1,7 @@
package com.jozufozu.flywheel.backend.struct;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;

View File

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.struct;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
/**

View File

@ -1,7 +1,7 @@
package com.jozufozu.flywheel.core;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.materials.model.ModelData;
import com.jozufozu.flywheel.core.materials.model.ModelType;
import com.jozufozu.flywheel.core.materials.oriented.OrientedData;
@ -14,17 +14,14 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class Materials {
public static final StructType<OrientedData> ORIENTED_TYPE = new OrientedType();
public static final StructType<ModelData> TRANSFORMED_TYPE = new ModelType();
public static final MaterialSpec<OrientedData> ORIENTED = new MaterialSpec<>(Names.ORIENTED, Programs.ORIENTED, ORIENTED_TYPE);
public static final MaterialSpec<ModelData> TRANSFORMED = new MaterialSpec<>(Names.MODEL, Programs.TRANSFORMED, TRANSFORMED_TYPE);
public static final StructType<OrientedData> ORIENTED = new OrientedType();
public static final StructType<ModelData> TRANSFORMED = new ModelType();
public static void flwInit(GatherContextEvent event) {
event.getBackend()
.register(ORIENTED);
event.getBackend()
.register(TRANSFORMED);
Backend backend = event.getBackend();
backend.register(Names.ORIENTED, ORIENTED);
backend.register(Names.MODEL, TRANSFORMED);
}
public static class Names {

View File

@ -7,7 +7,6 @@ import java.util.stream.Stream;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.ShaderContext;
import com.jozufozu.flywheel.api.MaterialSpec;
import com.jozufozu.flywheel.backend.pipeline.ShaderPipeline;
import com.jozufozu.flywheel.core.shader.ContextAwareProgram;
import com.jozufozu.flywheel.core.shader.WorldProgram;
@ -88,7 +87,8 @@ public class WorldContext<P extends WorldProgram> implements ShaderContext<P> {
if (specStream == null) {
specStream = () -> backend.allMaterials()
.stream()
.map(MaterialSpec::getProgramName);
.map(type -> type.asInstanced()
.getProgramSpec());
}
return new WorldContext<>(backend, name, specStream, pipeline);
}

View File

@ -1,6 +1,6 @@
package com.jozufozu.flywheel.core.materials.model;
import com.jozufozu.flywheel.backend.struct.BatchingTransformer;
import com.jozufozu.flywheel.api.struct.BatchingTransformer;
import com.jozufozu.flywheel.core.model.Model;
public class ModelTransformer extends BatchingTransformer<ModelData> {

View File

@ -2,15 +2,18 @@ package com.jozufozu.flywheel.core.materials.model;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.Batched;
import com.jozufozu.flywheel.backend.struct.BatchingTransformer;
import com.jozufozu.flywheel.backend.struct.StructWriter;
import com.jozufozu.flywheel.backend.struct.Writeable;
import com.jozufozu.flywheel.api.struct.Batched;
import com.jozufozu.flywheel.api.struct.BatchingTransformer;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.Programs;
import com.jozufozu.flywheel.core.materials.model.writer.UnsafeModelWriter;
import com.jozufozu.flywheel.core.model.Model;
public class ModelType implements Writeable<ModelData>, Batched<ModelData> {
import net.minecraft.resources.ResourceLocation;
public class ModelType implements Instanced<ModelData>, Batched<ModelData> {
@Override
public ModelData create() {
@ -27,6 +30,11 @@ public class ModelType implements Writeable<ModelData>, Batched<ModelData> {
return new UnsafeModelWriter(backing, this);
}
@Override
public ResourceLocation getProgramSpec() {
return Programs.TRANSFORMED;
}
@Override
public BatchingTransformer<ModelData> getTransformer(Model model) {
return null;

View File

@ -0,0 +1,6 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.jozufozu.flywheel.core.materials.model;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;

View File

@ -3,7 +3,7 @@ package com.jozufozu.flywheel.core.materials.model.writer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter;
import com.jozufozu.flywheel.core.materials.model.ModelData;
import com.jozufozu.flywheel.util.WriteUnsafe;

View File

@ -2,15 +2,18 @@ package com.jozufozu.flywheel.core.materials.oriented;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.Batched;
import com.jozufozu.flywheel.backend.struct.BatchingTransformer;
import com.jozufozu.flywheel.backend.struct.StructWriter;
import com.jozufozu.flywheel.backend.struct.Writeable;
import com.jozufozu.flywheel.api.struct.Batched;
import com.jozufozu.flywheel.api.struct.BatchingTransformer;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.Programs;
import com.jozufozu.flywheel.core.materials.oriented.writer.UnsafeOrientedWriter;
import com.jozufozu.flywheel.core.model.Model;
public class OrientedType implements Writeable<OrientedData>, Batched<OrientedData> {
import net.minecraft.resources.ResourceLocation;
public class OrientedType implements Instanced<OrientedData>, Batched<OrientedData> {
@Override
public OrientedData create() {
@ -27,6 +30,11 @@ public class OrientedType implements Writeable<OrientedData>, Batched<OrientedDa
return new UnsafeOrientedWriter(backing, this);
}
@Override
public ResourceLocation getProgramSpec() {
return Programs.ORIENTED;
}
@Override
public BatchingTransformer<OrientedData> getTransformer(Model model) {
return null;

View File

@ -0,0 +1,6 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.jozufozu.flywheel.core.materials.oriented;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;

View File

@ -3,7 +3,7 @@ package com.jozufozu.flywheel.core.materials.oriented.writer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter;
import com.jozufozu.flywheel.core.materials.oriented.OrientedData;