From e58bfe0bcbbcbf7d34bd573e71e4ce0dc8e24ee8 Mon Sep 17 00:00:00 2001 From: Matti Ruohonen Date: Mon, 19 Dec 2022 20:09:13 +0200 Subject: [PATCH] Make the creation of the `VertexBuffer`s in `ChunkRendererSchematicVbo` lazy This helps avoid some of the recent AMD driver related crashes (see #576) --- .../schematic/ChunkRendererSchematicVbo.java | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/main/java/fi/dy/masa/litematica/render/schematic/ChunkRendererSchematicVbo.java b/src/main/java/fi/dy/masa/litematica/render/schematic/ChunkRendererSchematicVbo.java index 1174402a74..5ea6107c45 100644 --- a/src/main/java/fi/dy/masa/litematica/render/schematic/ChunkRendererSchematicVbo.java +++ b/src/main/java/fi/dy/masa/litematica/render/schematic/ChunkRendererSchematicVbo.java @@ -2,8 +2,8 @@ import java.util.ArrayList; import java.util.EnumSet; -import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -12,6 +12,7 @@ import javax.annotation.Nullable; import com.google.common.collect.Sets; import com.mojang.blaze3d.systems.RenderSystem; + import net.minecraft.block.Block; import net.minecraft.block.BlockRenderType; import net.minecraft.block.BlockState; @@ -35,17 +36,18 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3i; import net.minecraft.world.chunk.WorldChunk; -import fi.dy.masa.malilib.util.Color4f; -import fi.dy.masa.malilib.util.EntityUtils; -import fi.dy.masa.malilib.util.IntBoundingBox; -import fi.dy.masa.malilib.util.LayerRange; -import fi.dy.masa.malilib.util.SubChunkPos; + import fi.dy.masa.litematica.config.Configs; import fi.dy.masa.litematica.data.DataManager; import fi.dy.masa.litematica.render.RenderUtils; import fi.dy.masa.litematica.util.OverlayType; import fi.dy.masa.litematica.util.PositionUtils; import fi.dy.masa.litematica.world.WorldSchematic; +import fi.dy.masa.malilib.util.Color4f; +import fi.dy.masa.malilib.util.EntityUtils; +import fi.dy.masa.malilib.util.IntBoundingBox; +import fi.dy.masa.malilib.util.LayerRange; +import fi.dy.masa.malilib.util.SubChunkPos; public class ChunkRendererSchematicVbo { @@ -60,7 +62,7 @@ public class ChunkRendererSchematicVbo protected final BlockPos.Mutable chunkRelativePos; protected final Map vertexBufferBlocks; - protected final VertexBuffer[] vertexBufferOverlay; + protected final Map vertexBufferOverlay; protected final List boxes = new ArrayList<>(); protected final EnumSet existingOverlays = EnumSet.noneOf(OverlayRenderType.class); @@ -85,20 +87,10 @@ public ChunkRendererSchematicVbo(WorldSchematic world, WorldRendererSchematic wo this.chunkRenderData = ChunkRenderDataSchematic.EMPTY; this.chunkRenderLock = new ReentrantLock(); this.chunkRenderDataLock = new ReentrantLock(); - this.vertexBufferBlocks = new HashMap<>(); - this.vertexBufferOverlay = new VertexBuffer[OverlayRenderType.values().length]; + this.vertexBufferBlocks = new IdentityHashMap<>(); + this.vertexBufferOverlay = new IdentityHashMap<>(); this.position = new BlockPos.Mutable(); this.chunkRelativePos = new BlockPos.Mutable(); - - for (RenderLayer layer : RenderLayer.getBlockLayers()) - { - this.vertexBufferBlocks.put(layer, new VertexBuffer()); - } - - for (int i = 0; i < OverlayRenderType.values().length; ++i) - { - this.vertexBufferOverlay[i] = new VertexBuffer(); - } } public boolean hasOverlay() @@ -113,13 +105,13 @@ public EnumSet getOverlayTypes() public VertexBuffer getBlocksVertexBufferByLayer(RenderLayer layer) { - return this.vertexBufferBlocks.get(layer); + return this.vertexBufferBlocks.computeIfAbsent(layer, l -> new VertexBuffer()); } public VertexBuffer getOverlayVertexBuffer(OverlayRenderType type) { //if (GuiBase.isCtrlDown()) System.out.printf("getOverlayVertexBuffer: type: %s, buf: %s\n", type, this.vertexBufferOverlay[type.ordinal()]); - return this.vertexBufferOverlay[type.ordinal()]; + return this.vertexBufferOverlay.computeIfAbsent(type, l -> new VertexBuffer()); } public ChunkRenderDataSchematic getChunkRenderData() @@ -185,15 +177,8 @@ public void deleteGlResources() this.clear(); this.world = null; - this.vertexBufferBlocks.values().forEach((buf) -> buf.close()); - - for (int i = 0; i < this.vertexBufferOverlay.length; ++i) - { - if (this.vertexBufferOverlay[i] != null) - { - this.vertexBufferOverlay[i].close(); - } - } + this.vertexBufferBlocks.values().forEach(VertexBuffer::close); + this.vertexBufferOverlay.values().forEach(VertexBuffer::close); } public void resortTransparency(ChunkRenderTaskSchematic task)