Simply struct

- Inline Instanced* and Batched* StructType
 - flw.dumpShaderSource need only be present
 - Make Backend's static fields screaming snake case
This commit is contained in:
Jozufozu 2022-06-09 13:51:50 -07:00
parent 1765aa74f8
commit d22f715f79
15 changed files with 56 additions and 87 deletions

View file

@ -1,7 +0,0 @@
package com.jozufozu.flywheel.api.struct;
import com.jozufozu.flywheel.core.model.ModelTransformer;
public interface BatchedStructType<S> extends StructType<S> {
void transform(S d, ModelTransformer.Params b);
}

View file

@ -1,16 +0,0 @@
package com.jozufozu.flywheel.api.struct;
import java.nio.ByteBuffer;
import com.jozufozu.flywheel.core.source.FileResolution;
public interface InstancedStructType<S> extends StructType<S> {
/**
* 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<S> getWriter(ByteBuffer backing);
FileResolution getInstanceShader();
}

View file

@ -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<S> {
*/
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<S> getWriter(ByteBuffer backing);
FileResolution getInstanceShader();
void transform(S d, ModelTransformer.Params b);
}

View file

@ -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) {

View file

@ -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;
}

View file

@ -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<BatchedStructType<? extends InstanceData>, CPUInstancerFactory<?>> materials = new HashMap<>();
private final Map<StructType<? extends InstanceData>, CPUInstancerFactory<?>> materials = new HashMap<>();
private int vertexCount;
private int instanceCount;
@ -28,11 +27,7 @@ public class BatchedMaterialGroup implements MaterialGroup {
@SuppressWarnings("unchecked")
@Override
public <D extends InstanceData> CPUInstancerFactory<D> material(StructType<D> type) {
if (type instanceof BatchedStructType<D> batched) {
return (CPUInstancerFactory<D>) materials.computeIfAbsent(batched, CPUInstancerFactory::new);
} else {
throw new ClassCastException("Cannot use type '" + type + "' with CPU instancing.");
}
return (CPUInstancerFactory<D>) materials.computeIfAbsent(type, CPUInstancerFactory::new);
}
public void render(PoseStack stack, BatchDrawingTracker source, TaskEngine pool) {

View file

@ -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<BatchedStructType<? extends InstanceData>, CPUInstancerFactory<?>> factories = new HashMap<>();
private final Map<StructType<? extends InstanceData>, CPUInstancerFactory<?>> factories = new HashMap<>();
private final BatchDrawingTracker batchTracker = new BatchDrawingTracker();
@SuppressWarnings("unchecked")
@Override
public <D extends InstanceData> CPUInstancerFactory<D> factory(StructType<D> type) {
if (type instanceof BatchedStructType<D> batched) {
return (CPUInstancerFactory<D>) factories.computeIfAbsent(batched, CPUInstancerFactory::new);
} else {
throw new ClassCastException("Cannot use type '" + type + "' with CPU instancing.");
}
return (CPUInstancerFactory<D>) factories.computeIfAbsent(type, CPUInstancerFactory::new);
}
@Override

View file

@ -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<D extends InstanceData> extends AbstractInstancer<D> {
//
// final ModelTransformer sbb;
public CPUInstancer(BatchedStructType<D> type) {
public CPUInstancer(StructType<D> type) {
super(type::create);
// batchingType = type;
//

View file

@ -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<D extends InstanceData> implements InstancerFactory<D> {
protected final Map<ModelSupplier, CPUInstancer<D>> models;
private final BatchedStructType<D> type;
private final StructType<D> type;
public CPUInstancerFactory(BatchedStructType<D> type) {
public CPUInstancerFactory(StructType<D> type) {
this.type = type;
this.models = new HashMap<>();

View file

@ -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<D extends InstanceData> extends AbstractInstancer<D> {
final BufferLayout instanceFormat;
final InstancedStructType<D> instancedType;
final StructType<D> instancedType;
GlBuffer vbo;
int attributeBaseIndex;
@ -25,7 +25,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
boolean anyToUpdate;
public GPUInstancer(InstancedStructType<D> type) {
public GPUInstancer(StructType<D> type) {
super(type::create);
this.instanceFormat = type.getLayout();
instancedType = type;

View file

@ -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<D extends InstanceData> implements InstancerFactory<D> {
protected final Map<ModelSupplier, InstancedModel<D>> models = new HashMap<>();
protected final InstancedStructType<D> type;
protected final StructType<D> type;
protected final List<InstancedModel<D>> uninitialized = new ArrayList<>();
@ -33,7 +33,7 @@ public class GPUInstancerFactory<D extends InstanceData> implements InstancerFac
public final Multimap<RenderType, Material> materials = HashMultimap.create();
public final Multimap<Material, Renderable> renderables = ArrayListMultimap.create();
public GPUInstancerFactory(InstancedStructType<D> type) {
public GPUInstancerFactory(StructType<D> type) {
this.type = type;
}

View file

@ -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<P extends WorldProgram> implements Engine {
protected final ProgramCompiler<P> context;
private MeshPool allocator;
protected final Map<InstancedStructType<? extends InstanceData>, GPUInstancerFactory<?>> factories = new HashMap<>();
protected final Map<StructType<? extends InstanceData>, GPUInstancerFactory<?>> factories = new HashMap<>();
protected final Set<RenderType> toRender = new HashSet<>();
@ -62,11 +61,7 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
@NotNull
@Override
public <D extends InstanceData> GPUInstancerFactory<D> factory(StructType<D> type) {
if (type instanceof InstancedStructType<D> instanced) {
return (GPUInstancerFactory<D>) factories.computeIfAbsent(instanced, GPUInstancerFactory::new);
} else {
throw new ClassCastException("Cannot use type '" + type + "' with GPU instancing.");
}
return (GPUInstancerFactory<D>) factories.computeIfAbsent(type, GPUInstancerFactory::new);
}
@Override
@ -108,9 +103,9 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
Textures.bindActiveTextures();
CoreShaderInfo coreShaderInfo = getCoreShaderInfo();
for (Map.Entry<InstancedStructType<? extends InstanceData>, GPUInstancerFactory<?>> entry : factories.entrySet()) {
InstancedStructType<? extends InstanceData> 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<P extends WorldProgram> 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;

View file

@ -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<InstancedStructType<? extends InstanceData>, GPUInstancerFactory<?>> entry : factories.entrySet()) {
InstancedStructType<? extends InstanceData> 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) {

View file

@ -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<ModelData>, BatchedStructType<ModelData> {
public class ModelType implements StructType<ModelData> {
public static final BufferLayout FORMAT = BufferLayout.builder()
.addItems(CommonItems.LIGHT, CommonItems.RGBA)

View file

@ -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<OrientedData>, BatchedStructType<OrientedData> {
public class OrientedType implements StructType<OrientedData> {
public static final BufferLayout FORMAT = BufferLayout.builder()
.addItems(CommonItems.LIGHT, CommonItems.RGBA)