From d22f715f793c82471557224e7afd482a509a4660 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 9 Jun 2022 13:51:50 -0700 Subject: [PATCH] Simply struct - Inline Instanced* and Batched* StructType - flw.dumpShaderSource need only be present - Make Backend's static fields screaming snake case --- .../api/struct/BatchedStructType.java | 7 ----- .../api/struct/InstancedStructType.java | 16 ------------ .../flywheel/api/struct/StructType.java | 15 +++++++++++ .../jozufozu/flywheel/backend/Backend.java | 26 +++++++++---------- .../flywheel/backend/gl/shader/GlShader.java | 2 +- .../batching/BatchedMaterialGroup.java | 9 ++----- .../instancing/batching/BatchingEngine.java | 9 ++----- .../instancing/batching/CPUInstancer.java | 4 +-- .../batching/CPUInstancerFactory.java | 6 ++--- .../instancing/instancing/GPUInstancer.java | 6 ++--- .../instancing/GPUInstancerFactory.java | 6 ++--- .../instancing/InstancingEngine.java | 17 +++++------- .../core/crumbling/CrumblingRenderer.java | 10 +++---- .../core/structs/model/ModelType.java | 5 ++-- .../core/structs/oriented/OrientedType.java | 5 ++-- 15 files changed, 56 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/api/struct/BatchedStructType.java delete mode 100644 src/main/java/com/jozufozu/flywheel/api/struct/InstancedStructType.java diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/BatchedStructType.java b/src/main/java/com/jozufozu/flywheel/api/struct/BatchedStructType.java deleted file mode 100644 index 52eeacf85..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/struct/BatchedStructType.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.jozufozu.flywheel.api.struct; - -import com.jozufozu.flywheel.core.model.ModelTransformer; - -public interface BatchedStructType extends StructType { - void transform(S d, ModelTransformer.Params b); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/InstancedStructType.java b/src/main/java/com/jozufozu/flywheel/api/struct/InstancedStructType.java deleted file mode 100644 index 1a809163a..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/struct/InstancedStructType.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jozufozu.flywheel.api.struct; - -import java.nio.ByteBuffer; - -import com.jozufozu.flywheel.core.source.FileResolution; - -public interface InstancedStructType extends StructType { - /** - * Create a {@link StructWriter} that will consume instances of S and write them to the given buffer. - * - * @param backing The buffer that the StructWriter will write to. - */ - StructWriter getWriter(ByteBuffer backing); - - FileResolution getInstanceShader(); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java index a405d518c..7c7b4bd43 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java @@ -1,6 +1,10 @@ package com.jozufozu.flywheel.api.struct; +import java.nio.ByteBuffer; + import com.jozufozu.flywheel.core.layout.BufferLayout; +import com.jozufozu.flywheel.core.model.ModelTransformer; +import com.jozufozu.flywheel.core.source.FileResolution; /** * A StructType contains metadata for a specific instance struct that Flywheel can interface with. @@ -18,4 +22,15 @@ public interface StructType { */ BufferLayout getLayout(); + /** + * Create a {@link StructWriter} that will consume instances of S and write them to the given buffer. + * + * @param backing The buffer that the StructWriter will write to. + */ + StructWriter getWriter(ByteBuffer backing); + + FileResolution getInstanceShader(); + + void transform(S d, ModelTransformer.Params b); + } diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index aaf2e4e51..5ef696741 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.backend; -import java.lang.ref.Cleaner; - import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -19,19 +17,19 @@ import net.minecraft.world.level.LevelAccessor; public class Backend { public static final Logger LOGGER = LogUtils.getLogger(); - public static boolean dumpShaderSource = Boolean.getBoolean("flw.dumpShaderSource"); + public static boolean DUMP_SHADER_SOURCE = System.getProperty("flw.dumpShaderSource") != null; - private static BackendType backendType; + private static BackendType TYPE; - private static ParallelTaskEngine taskEngine; + private static ParallelTaskEngine EXECUTOR; - private static final Loader loader = new Loader(); + private static final Loader LOADER = new Loader(); /** * Get the current Flywheel backend type. */ public static BackendType getBackendType() { - return backendType; + return TYPE; } /** @@ -39,12 +37,12 @@ public class Backend { * @return A global Flywheel thread pool. */ public static ParallelTaskEngine getTaskEngine() { - if (taskEngine == null) { - taskEngine = new ParallelTaskEngine("Flywheel"); - taskEngine.startWorkers(); + if (EXECUTOR == null) { + EXECUTOR = new ParallelTaskEngine("Flywheel"); + EXECUTOR.startWorkers(); } - return taskEngine; + return EXECUTOR; } /** @@ -52,15 +50,15 @@ public class Backend { * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use. */ public static String getBackendDescriptor() { - return backendType == null ? "Uninitialized" : backendType.getProperName(); + return TYPE == null ? "Uninitialized" : TYPE.getProperName(); } public static void refresh() { - backendType = chooseEngine(); + TYPE = chooseEngine(); } public static boolean isOn() { - return backendType != BackendType.OFF; + return TYPE != BackendType.OFF; } public static boolean canUseInstancing(@Nullable Level world) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java index c4195c68f..988cb4414 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java @@ -54,7 +54,7 @@ public class GlShader extends GlObject { } private void dumpSource(String source, ShaderType type) { - if (!Backend.dumpShaderSource) { + if (!Backend.DUMP_SHADER_SOURCE) { return; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java index d324b953f..618d53d45 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java @@ -5,7 +5,6 @@ import java.util.Map; import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.MaterialGroup; -import com.jozufozu.flywheel.api.struct.BatchedStructType; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.instancing.BatchDrawingTracker; import com.jozufozu.flywheel.backend.instancing.TaskEngine; @@ -17,7 +16,7 @@ public class BatchedMaterialGroup implements MaterialGroup { protected final RenderType state; - private final Map, CPUInstancerFactory> materials = new HashMap<>(); + private final Map, CPUInstancerFactory> materials = new HashMap<>(); private int vertexCount; private int instanceCount; @@ -28,11 +27,7 @@ public class BatchedMaterialGroup implements MaterialGroup { @SuppressWarnings("unchecked") @Override public CPUInstancerFactory material(StructType type) { - if (type instanceof BatchedStructType batched) { - return (CPUInstancerFactory) materials.computeIfAbsent(batched, CPUInstancerFactory::new); - } else { - throw new ClassCastException("Cannot use type '" + type + "' with CPU instancing."); - } + return (CPUInstancerFactory) materials.computeIfAbsent(type, CPUInstancerFactory::new); } public void render(PoseStack stack, BatchDrawingTracker source, TaskEngine pool) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java index 1049bb041..75a860a49 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Map; import com.jozufozu.flywheel.api.InstanceData; -import com.jozufozu.flywheel.api.struct.BatchedStructType; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.instancing.BatchDrawingTracker; import com.jozufozu.flywheel.backend.instancing.Engine; @@ -21,17 +20,13 @@ import net.minecraft.core.Vec3i; public class BatchingEngine implements Engine { - private final Map, CPUInstancerFactory> factories = new HashMap<>(); + private final Map, CPUInstancerFactory> factories = new HashMap<>(); private final BatchDrawingTracker batchTracker = new BatchDrawingTracker(); @SuppressWarnings("unchecked") @Override public CPUInstancerFactory factory(StructType type) { - if (type instanceof BatchedStructType batched) { - return (CPUInstancerFactory) factories.computeIfAbsent(batched, CPUInstancerFactory::new); - } else { - throw new ClassCastException("Cannot use type '" + type + "' with CPU instancing."); - } + return (CPUInstancerFactory) factories.computeIfAbsent(type, CPUInstancerFactory::new); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java index 2807e55f8..5e26617ca 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.backend.instancing.batching; import com.jozufozu.flywheel.api.InstanceData; -import com.jozufozu.flywheel.api.struct.BatchedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.instancing.AbstractInstancer; import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.backend.model.DirectVertexConsumer; @@ -14,7 +14,7 @@ public class CPUInstancer extends AbstractInstancer { // // final ModelTransformer sbb; - public CPUInstancer(BatchedStructType type) { + public CPUInstancer(StructType type) { super(type::create); // batchingType = type; // diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancerFactory.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancerFactory.java index 00a9ded88..c2dc7183b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancerFactory.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancerFactory.java @@ -6,7 +6,7 @@ import java.util.Map; import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.Instancer; import com.jozufozu.flywheel.api.InstancerFactory; -import com.jozufozu.flywheel.api.struct.BatchedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.core.model.ModelSupplier; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; @@ -14,9 +14,9 @@ import com.mojang.blaze3d.vertex.VertexConsumer; public class CPUInstancerFactory implements InstancerFactory { protected final Map> models; - private final BatchedStructType type; + private final StructType type; - public CPUInstancerFactory(BatchedStructType type) { + public CPUInstancerFactory(StructType type) { this.type = type; this.models = new HashMap<>(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java index 6eae582eb..368d1de36 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java @@ -5,7 +5,7 @@ import java.util.Set; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.InstanceData; -import com.jozufozu.flywheel.api.struct.InstancedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.backend.gl.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; @@ -17,7 +17,7 @@ import com.jozufozu.flywheel.core.layout.BufferLayout; public class GPUInstancer extends AbstractInstancer { final BufferLayout instanceFormat; - final InstancedStructType instancedType; + final StructType instancedType; GlBuffer vbo; int attributeBaseIndex; @@ -25,7 +25,7 @@ public class GPUInstancer extends AbstractInstancer { boolean anyToUpdate; - public GPUInstancer(InstancedStructType type) { + public GPUInstancer(StructType type) { super(type::create); this.instanceFormat = type.getLayout(); instancedType = type; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancerFactory.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancerFactory.java index 3783e815c..99b4b530e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancerFactory.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancerFactory.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.Instancer; import com.jozufozu.flywheel.api.InstancerFactory; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.api.struct.InstancedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.model.MeshPool; import com.jozufozu.flywheel.core.model.ModelSupplier; @@ -25,7 +25,7 @@ import net.minecraft.client.renderer.RenderType; public class GPUInstancerFactory implements InstancerFactory { protected final Map> models = new HashMap<>(); - protected final InstancedStructType type; + protected final StructType type; protected final List> uninitialized = new ArrayList<>(); @@ -33,7 +33,7 @@ public class GPUInstancerFactory implements InstancerFac public final Multimap materials = HashMultimap.create(); public final Multimap renderables = ArrayListMultimap.create(); - public GPUInstancerFactory(InstancedStructType type) { + public GPUInstancerFactory(StructType type) { this.type = type; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index 254f1330f..f87307d36 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull; import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.api.struct.InstancedStructType; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.backend.instancing.TaskEngine; @@ -44,7 +43,7 @@ public class InstancingEngine

implements Engine { protected final ProgramCompiler

context; private MeshPool allocator; - protected final Map, GPUInstancerFactory> factories = new HashMap<>(); + protected final Map, GPUInstancerFactory> factories = new HashMap<>(); protected final Set toRender = new HashSet<>(); @@ -62,11 +61,7 @@ public class InstancingEngine

implements Engine { @NotNull @Override public GPUInstancerFactory factory(StructType type) { - if (type instanceof InstancedStructType instanced) { - return (GPUInstancerFactory) factories.computeIfAbsent(instanced, GPUInstancerFactory::new); - } else { - throw new ClassCastException("Cannot use type '" + type + "' with GPU instancing."); - } + return (GPUInstancerFactory) factories.computeIfAbsent(type, GPUInstancerFactory::new); } @Override @@ -108,9 +103,9 @@ public class InstancingEngine

implements Engine { Textures.bindActiveTextures(); CoreShaderInfo coreShaderInfo = getCoreShaderInfo(); - for (Map.Entry, GPUInstancerFactory> entry : factories.entrySet()) { - InstancedStructType instanceType = entry.getKey(); - GPUInstancerFactory factory = entry.getValue(); + for (var entry : factories.entrySet()) { + var instanceType = entry.getKey(); + var factory = entry.getValue(); var materials = factory.materials.get(type); for (Material material : materials) { @@ -146,7 +141,7 @@ public class InstancingEngine

implements Engine { return coreShaderInfo; } - protected P setup(InstancedStructType instanceType, Material material, CoreShaderInfo coreShaderInfo, double camX, double camY, double camZ, Matrix4f viewProjection, ClientLevel level) { + protected P setup(StructType instanceType, Material material, CoreShaderInfo coreShaderInfo, double camX, double camY, double camZ, Matrix4f viewProjection, ClientLevel level) { float alphaDiscard = coreShaderInfo.alphaDiscard(); if (alphaDiscard == 0) { alphaDiscard = 0.0001f; diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java index 9dc640d71..d7be53423 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -2,17 +2,13 @@ package com.jozufozu.flywheel.core.crumbling; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.SortedSet; -import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.api.struct.InstancedStructType; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.SerialTaskEngine; -import com.jozufozu.flywheel.backend.instancing.instancing.GPUInstancerFactory; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; import com.jozufozu.flywheel.backend.instancing.instancing.Renderable; import com.jozufozu.flywheel.core.Contexts; @@ -182,9 +178,9 @@ public class CrumblingRenderer { Textures.bindActiveTextures(); CoreShaderInfo coreShaderInfo = getCoreShaderInfo(); - for (Map.Entry, GPUInstancerFactory> entry : factories.entrySet()) { - InstancedStructType instanceType = entry.getKey(); - GPUInstancerFactory factory = entry.getValue(); + for (var entry : factories.entrySet()) { + var instanceType = entry.getKey(); + var factory = entry.getValue(); var materials = factory.materials.get(type); for (Material material : materials) { diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/model/ModelType.java b/src/main/java/com/jozufozu/flywheel/core/structs/model/ModelType.java index 660ebd611..df0cf052f 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/model/ModelType.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/model/ModelType.java @@ -2,8 +2,7 @@ package com.jozufozu.flywheel.core.structs.model; import java.nio.ByteBuffer; -import com.jozufozu.flywheel.api.struct.BatchedStructType; -import com.jozufozu.flywheel.api.struct.InstancedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.core.layout.BufferLayout; import com.jozufozu.flywheel.core.layout.CommonItems; @@ -11,7 +10,7 @@ import com.jozufozu.flywheel.core.model.ModelTransformer; import com.jozufozu.flywheel.core.source.FileResolution; import com.jozufozu.flywheel.core.structs.InstanceShaders; -public class ModelType implements InstancedStructType, BatchedStructType { +public class ModelType implements StructType { public static final BufferLayout FORMAT = BufferLayout.builder() .addItems(CommonItems.LIGHT, CommonItems.RGBA) diff --git a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java index 0c00fe649..829ce0121 100644 --- a/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java +++ b/src/main/java/com/jozufozu/flywheel/core/structs/oriented/OrientedType.java @@ -2,8 +2,7 @@ package com.jozufozu.flywheel.core.structs.oriented; import java.nio.ByteBuffer; -import com.jozufozu.flywheel.api.struct.BatchedStructType; -import com.jozufozu.flywheel.api.struct.InstancedStructType; +import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.struct.StructWriter; import com.jozufozu.flywheel.core.layout.BufferLayout; import com.jozufozu.flywheel.core.layout.CommonItems; @@ -12,7 +11,7 @@ import com.jozufozu.flywheel.core.source.FileResolution; import com.jozufozu.flywheel.core.structs.InstanceShaders; import com.mojang.math.Quaternion; -public class OrientedType implements InstancedStructType, BatchedStructType { +public class OrientedType implements StructType { public static final BufferLayout FORMAT = BufferLayout.builder() .addItems(CommonItems.LIGHT, CommonItems.RGBA)