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 70e092e75..7cad92ecb 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 @@ -22,12 +22,6 @@ public class BatchedMaterialGroup implements MaterialGroup { this.state = state; } - /** - * Get the material as defined by the given {@link StructType type}. - * @param spec The material you want to create instances with. - * @param The type representing the per instance data. - * @return A - */ @SuppressWarnings("unchecked") @Override public BatchedMaterial material(StructType spec) { 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 ac7f9b36f..466c82e15 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 @@ -1,12 +1,13 @@ package com.jozufozu.flywheel.backend.instancing.instancing; -import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import com.jozufozu.flywheel.api.InstanceData; import com.jozufozu.flywheel.api.MaterialGroup; import com.jozufozu.flywheel.api.struct.StructType; +import com.jozufozu.flywheel.backend.model.ModelPool; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.util.TextureBinder; import com.mojang.math.Matrix4f; @@ -24,8 +25,6 @@ public class InstancedMaterialGroup

implements MaterialG protected final InstancingEngine

owner; protected final RenderType type; - protected final ArrayList> renderers = new ArrayList<>(); - private final Map, InstancedMaterial> materials = new HashMap<>(); public InstancedMaterialGroup(InstancingEngine

owner, RenderType type) { @@ -33,27 +32,48 @@ public class InstancedMaterialGroup

implements MaterialG this.type = type; } - /** - * Get the material as defined by the given {@link MaterialSpec spec}. - * @param spec The material you want to create instances with. - * @param The type representing the per instance data. - * @return A - */ @SuppressWarnings("unchecked") @Override public InstancedMaterial material(StructType spec) { - return (InstancedMaterial) materials.computeIfAbsent(spec, this::createInstanceMaterial); + return (InstancedMaterial) materials.computeIfAbsent(spec, InstancedMaterial::new); } public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { type.setupRenderState(); TextureBinder.bindActiveTextures(); - for (InstancedMaterialRenderer

renderer : renderers) { - renderer.render(viewProjection, camX, camY, camZ); - } + renderAll(viewProjection, camX, camY, camZ); type.clearRenderState(); } + protected void renderAll(Matrix4f viewProjection, double camX, double camY, double camZ) { + for (Map.Entry, InstancedMaterial> entry : materials.entrySet()) { + InstancedMaterial material = entry.getValue(); + if (material.nothingToRender()) continue; + + Collection> instancers = material.models.asMap() + .values(); + + // initialize all uninitialized instancers... + instancers.forEach(GPUInstancer::init); + if (material.allocator instanceof ModelPool pool) { + // ...and then flush the model arena in case anything was marked for upload + pool.flush(); + } + + P program = owner.getProgram(entry.getKey() + .asInstanced() + .getProgramSpec()).get(); + + program.bind(); + program.uploadViewProjection(viewProjection); + program.uploadCameraPos(camX, camY, camZ); + + setup(program); + + instancers.forEach(GPUInstancer::render); + } + } + public void setup(P program) { } @@ -67,15 +87,5 @@ public class InstancedMaterialGroup

implements MaterialG .forEach(InstancedMaterial::delete); materials.clear(); - renderers.clear(); - } - - private InstancedMaterial createInstanceMaterial(StructType type) { - InstancedMaterial material = new InstancedMaterial<>(type); - - this.renderers.add(new InstancedMaterialRenderer<>(owner.getProgram(type.asInstanced() - .getProgramSpec()), material, this::setup)); - - return material; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialRenderer.java deleted file mode 100644 index a6ab47680..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialRenderer.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.jozufozu.flywheel.backend.instancing.instancing; - -import java.util.Collection; -import java.util.function.Consumer; -import java.util.function.Supplier; - -import com.jozufozu.flywheel.backend.model.ModelPool; -import com.jozufozu.flywheel.core.shader.WorldProgram; -import com.mojang.math.Matrix4f; - -public class InstancedMaterialRenderer

{ - - protected final Supplier

program; - protected final InstancedMaterial material; - - protected final Consumer

setupFunc; - - public InstancedMaterialRenderer(Supplier

programSupplier, InstancedMaterial material, Consumer

setupFunc) { - this.program = programSupplier; - this.material = material; - this.setupFunc = setupFunc; - } - - public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { - if (material.nothingToRender()) return; - - Collection> instancers = material.models.asMap() - .values(); - - // initialize all uninitialized instancers... - instancers.forEach(GPUInstancer::init); - if (material.allocator instanceof ModelPool pool) { - // ...and then flush the model arena in case anything was marked for upload - pool.flush(); - } - - P program = this.program.get(); - - program.bind(); - program.uploadViewProjection(viewProjection); - program.uploadCameraPos(camX, camY, camZ); - - setupFunc.accept(program); - - instancers.forEach(GPUInstancer::render); - } - -} 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 a5093fbe5..c0eff27ae 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -2,7 +2,6 @@ package com.jozufozu.flywheel.core.crumbling; import com.jozufozu.flywheel.backend.instancing.instancing.InstancedMaterialGroup; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; -import com.jozufozu.flywheel.backend.instancing.instancing.InstancedMaterialRenderer; import com.jozufozu.flywheel.core.atlas.AtlasInfo; import com.jozufozu.flywheel.core.atlas.SheetData; import com.jozufozu.flywheel.util.RenderTextures; @@ -39,9 +38,7 @@ public class CrumblingGroup

extends InstancedMateria RenderSystem.setShaderTexture(4, breakingTex); TextureBinder.bindActiveTextures(); - for (InstancedMaterialRenderer

renderer : renderers) { - renderer.render(viewProjection, camX, camY, camZ); - } + renderAll(viewProjection, camX, camY, camZ); CrumblingRenderer._currentLayer.clearRenderState(); }