Skip to content

Commit

Permalink
Don't return null results in ChunkRenderTranslucencySortTask
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
embeddedt committed Feb 23, 2024
1 parent cbd90a0 commit 3d52d1a
Showing 1 changed file with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -28,6 +29,8 @@
public class ChunkRenderTranslucencySortTask<T extends ChunkGraphicsState> extends ChunkRenderBuildTask<T> {
private static final BlockRenderPass[] TRANSLUCENT_PASSES = Arrays.stream(BlockRenderPass.VALUES).filter(BlockRenderPass::isTranslucent).toArray(BlockRenderPass[]::new);

private static final BlockRenderPass[] NO_PASSES = new BlockRenderPass[0];

private final ChunkRenderContainer<T> render;
private final BlockPos offset;
private final Vec3d camera;
Expand All @@ -43,45 +46,47 @@ public ChunkRenderTranslucencySortTask(ChunkRenderContainer<T> render, BlockPos
@Override
public ChunkBuildResult<T> performBuild(ChunkRenderCacheLocal cache, ChunkBuildBuffers buffers, CancellationSource cancellationSource) {
ChunkRenderData data = this.render.getData();
if(data.isEmpty())
return null;

Map<BlockRenderPass, ChunkMeshData> replacementMeshes = new HashMap<>();
for(BlockRenderPass pass : TRANSLUCENT_PASSES) {
ChunkGraphicsState state = this.render.getGraphicsState(pass);
if(state == null)
continue;
ByteBuffer translucencyData = state.getTranslucencyData();
if(translucencyData == null)
continue;
ChunkMeshData translucentMesh = data.getMesh(pass);
if(translucentMesh == null)
continue;

// Make a snapshot of the translucency data to sort
ByteBuffer sortedData = GLAllocation.createDirectByteBuffer(translucencyData.capacity());
synchronized (translucencyData) {
sortedData.put(translucencyData);
translucencyData.position(0);
translucencyData.limit(translucencyData.capacity());
}

sortedData.flip();
// Sort it and create the new mesh
ChunkBufferSorter.sortStandardFormat(buffers.getVertexType(), sortedData, sortedData.capacity(), (float) camera.x - offset.getX(), (float)camera.y - offset.getY(), (float)camera.z - offset.getZ());
ChunkMeshData newMesh = new ChunkMeshData();
newMesh.setVertexData(new VertexData(sortedData, buffers.getVertexType().getCustomVertexFormat()));
for(Map.Entry<ModelQuadFacing, BufferSlice> entry : translucentMesh.getSlices()) {
newMesh.setModelSlice(entry.getKey(), entry.getValue());

Map<BlockRenderPass, ChunkMeshData> replacementMeshes;

if(!data.isEmpty()) {
replacementMeshes = new HashMap<>();
for(BlockRenderPass pass : TRANSLUCENT_PASSES) {
ChunkGraphicsState state = this.render.getGraphicsState(pass);
if(state == null)
continue;
ByteBuffer translucencyData = state.getTranslucencyData();
if(translucencyData == null)
continue;
ChunkMeshData translucentMesh = data.getMesh(pass);
if(translucentMesh == null)
continue;

// Make a snapshot of the translucency data to sort
ByteBuffer sortedData = GLAllocation.createDirectByteBuffer(translucencyData.capacity());
synchronized (translucencyData) {
sortedData.put(translucencyData);
translucencyData.position(0);
translucencyData.limit(translucencyData.capacity());
}

sortedData.flip();
// Sort it and create the new mesh
ChunkBufferSorter.sortStandardFormat(buffers.getVertexType(), sortedData, sortedData.capacity(), (float) camera.x - offset.getX(), (float)camera.y - offset.getY(), (float)camera.z - offset.getZ());
ChunkMeshData newMesh = new ChunkMeshData();
newMesh.setVertexData(new VertexData(sortedData, buffers.getVertexType().getCustomVertexFormat()));
for(Map.Entry<ModelQuadFacing, BufferSlice> entry : translucentMesh.getSlices()) {
newMesh.setModelSlice(entry.getKey(), entry.getValue());
}
replacementMeshes.put(pass, newMesh);
}
replacementMeshes.put(pass, newMesh);
} else {
replacementMeshes = Collections.emptyMap();
}

if(replacementMeshes.isEmpty())
return null;

ChunkBuildResult<T> result = new ChunkBuildResult<>(this.render, data.copyAndReplaceMesh(replacementMeshes));
result.passesToUpload = replacementMeshes.keySet().toArray(new BlockRenderPass[0]);
result.passesToUpload = replacementMeshes.keySet().toArray(NO_PASSES);
return result;
}

Expand Down

0 comments on commit 3d52d1a

Please sign in to comment.