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 3f0ff7c87..4b469c02f 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 @@ -19,7 +19,6 @@ import com.jozufozu.flywheel.core.model.Model; public class GPUInstancer extends AbstractInstancer { - private final ModelAllocator modelAllocator; private final BufferLayout instanceFormat; private final Instanced instancedType; @@ -32,9 +31,8 @@ public class GPUInstancer extends AbstractInstancer { protected boolean anyToUpdate; - public GPUInstancer(Instanced type, Model model, ModelAllocator modelAllocator) { + public GPUInstancer(Instanced type, Model model) { super(type::create, model); - this.modelAllocator = modelAllocator; this.instanceFormat = type.getLayout(); instancedType = type; } @@ -63,7 +61,7 @@ public class GPUInstancer extends AbstractInstancer { return deleted || model == null; } - public void init() { + public void init(ModelAllocator modelAllocator) { if (isInitialized()) return; initialized = true; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java index e22b57ce4..7a8373136 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java @@ -11,7 +11,6 @@ import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.Instancer; import com.jozufozu.flywheel.api.Material; import com.jozufozu.flywheel.api.struct.Instanced; -import com.jozufozu.flywheel.backend.model.ModelAllocator; import com.jozufozu.flywheel.core.model.Model; /** @@ -20,14 +19,12 @@ import com.jozufozu.flywheel.core.model.Model; */ public class InstancedMaterial implements Material { - protected final ModelAllocator allocator; protected final Map> models = new HashMap<>(); protected final Instanced type; protected final List> uninitialized = new ArrayList<>(); - public InstancedMaterial(Instanced type, ModelAllocator allocator) { + public InstancedMaterial(Instanced type) { this.type = type; - this.allocator = allocator; } /** @@ -40,7 +37,7 @@ public class InstancedMaterial implements Material { @Override public Instancer model(Object key, Supplier modelSupplier) { return models.computeIfAbsent(key, $ -> { - GPUInstancer instancer = new GPUInstancer<>(type, modelSupplier.get(), allocator); + GPUInstancer instancer = new GPUInstancer<>(type, modelSupplier.get()); uninitialized.add(instancer); return instancer; }); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java index bd85a26b8..2bc228ed9 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java @@ -32,27 +32,21 @@ public class InstancedMaterialGroup

implements MaterialG protected final RenderType type; private final Map, InstancedMaterial> materials = new HashMap<>(); - private final ModelAllocator allocator; + private ModelAllocator allocator; private int vertexCount; private int instanceCount; public InstancedMaterialGroup(InstancingEngine

owner, RenderType type) { this.owner = owner; this.type = type; - if (GlCompat.getInstance() - .onAMDWindows()) { - this.allocator = FallbackAllocator.INSTANCE; - } else { - this.allocator = new ModelPool(Formats.POS_TEX_NORMAL); - } } @SuppressWarnings("unchecked") @Override public InstancedMaterial material(StructType type) { if (type instanceof Instanced instanced) { - return (InstancedMaterial) materials.computeIfAbsent(instanced, t -> new InstancedMaterial<>(t, allocator)); + return (InstancedMaterial) materials.computeIfAbsent(instanced, InstancedMaterial::new); } else { throw new ClassCastException("Cannot use type '" + type + "' with GPU instancing."); } @@ -82,18 +76,7 @@ public class InstancedMaterialGroup

implements MaterialG } protected void renderAll(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { - // initialize all uninitialized instancers... - for (InstancedMaterial material : materials.values()) { - for (GPUInstancer instancer : material.uninitialized) { - instancer.init(); - } - material.uninitialized.clear(); - } - - if (allocator instanceof ModelPool pool) { - // ...and then flush the model arena in case anything was marked for upload - pool.flush(); - } + initializeInstancers(); vertexCount = 0; instanceCount = 0; @@ -119,7 +102,24 @@ public class InstancedMaterialGroup

implements MaterialG } } - public void setup(P program) { + private void initializeInstancers() { + ModelAllocator allocator = getModelAllocator(); + + // initialize all uninitialized instancers... + for (InstancedMaterial material : materials.values()) { + for (GPUInstancer instancer : material.uninitialized) { + instancer.init(allocator); + } + material.uninitialized.clear(); + } + + if (allocator instanceof ModelPool pool) { + // ...and then flush the model arena in case anything was marked for upload + pool.flush(); + } + } + + protected void setup(P program) { } @@ -133,4 +133,20 @@ public class InstancedMaterialGroup

implements MaterialG materials.clear(); } + + private ModelAllocator getModelAllocator() { + if (allocator == null) { + allocator = createAllocator(); + } + return this.allocator; + } + + private static ModelAllocator createAllocator() { + if (GlCompat.getInstance() + .onAMDWindows()) { + return FallbackAllocator.INSTANCE; + } else { + return new ModelPool(Formats.POS_TEX_NORMAL); + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index ac27997f3..ee113de17 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -54,7 +54,7 @@ public class CrumblingGroup

extends InstancedMateria } @Override - public void setup(P p) { + protected void setup(P p) { p.setAtlasSize(width, height); } }