Skip to content

Commit

Permalink
Make the creation of the VertexBuffers in `ChunkRendererSchematicVb…
Browse files Browse the repository at this point in the history
…o` lazy

This helps avoid some of the recent AMD driver related crashes (see #576)
  • Loading branch information
maruohon committed Dec 19, 2022
1 parent 42ac33f commit c1ae25a
Showing 1 changed file with 15 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -60,7 +62,7 @@ public class ChunkRendererSchematicVbo
protected final BlockPos.Mutable chunkRelativePos;

protected final Map<RenderLayer, VertexBuffer> vertexBufferBlocks;
protected final VertexBuffer[] vertexBufferOverlay;
protected final Map<OverlayRenderType, VertexBuffer> vertexBufferOverlay;
protected final List<IntBoundingBox> boxes = new ArrayList<>();
protected final EnumSet<OverlayRenderType> existingOverlays = EnumSet.noneOf(OverlayRenderType.class);

Expand All @@ -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()
Expand All @@ -113,13 +105,13 @@ public EnumSet<OverlayRenderType> 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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c1ae25a

Please sign in to comment.