Skip to content

Commit

Permalink
Final changes before refactoring to use visibility data
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerindividual committed Jan 18, 2025
1 parent 0e60be5 commit 2715111
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3dc;
import org.lwjgl.system.MemoryStack;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
Expand Down Expand Up @@ -159,33 +158,33 @@ private void createTerrainRenderList(Camera camera, Viewport viewport, int frame
final var searchDistance = this.getSearchDistance();
final var useOcclusionCulling = this.shouldUseOcclusionCulling(camera, spectator);

boolean culled = false;

var visitor = new VisibleChunkCollector(frame);

if (NativeCull.SUPPORTED && this.nativeGraph != null) {
var player = Minecraft.getInstance().player;
if (player != null && player.isHolding(Items.DEBUG_STICK)) {
try (var stack = MemoryStack.stackPush()) {
var resultsPtr = this.nativeGraph.search(
stack,
viewport,
searchDistance,
useOcclusionCulling
);

// TODO: actually read results
}
this.nativeGraph.findVisible(
visitor,
this.sectionByPosition,
viewport,
searchDistance,
useOcclusionCulling,
frame
);
culled = true;
}
} else {
// this.occlusionCuller.findVisible(visitor, viewport, searchDistance, useOcclusionCulling, frame);
}

this.occlusionCuller.findVisible(visitor, viewport, searchDistance, useOcclusionCulling, frame);
if (!culled) {
this.occlusionCuller.findVisible(visitor, viewport, searchDistance, useOcclusionCulling, frame);
}

this.renderLists = visitor.createRenderLists(viewport);
this.taskLists = visitor.getRebuildLists();
}


private float getSearchDistance() {
float distance;

Expand Down Expand Up @@ -572,7 +571,8 @@ public void markGraphDirty() {
}

public boolean needsUpdate() {
return this.needsGraphUpdate;
var player = Minecraft.getInstance().player;
return !(player != null && player.isHolding(Items.DIAMOND_HOE));
}

public ChunkBuilder getBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public ChunkBuildOutput execute(ChunkBuildContext buildContext, CancellationToke
profiler.push("render blocks");
try (var stack = MemoryStack.stackPush()) {
// align to 512 bits to allow for faster memory reads.
var traversableBlocksBuffer = stack.ncalloc(64, 512, 1);
var traversableBlocksBuffer = stack.nmalloc(64, 512);
MemoryUtil.memSet(traversableBlocksBuffer, 0xFF, 512);

for (int y = minY; y < maxY; y++) {
if (cancellationToken.isCancelled()) {
Expand Down Expand Up @@ -142,7 +143,7 @@ public ChunkBuildOutput execute(ChunkBuildContext buildContext, CancellationToke
if (blockState.isSolidRender()) {
// TODO: disable the visgraph stuff when using native culling
occluder.setOpaque(blockPos);
} else {

// bits are ordered with the bit pattern of "XYZZZZYYY_XXX".
int bitIdx = x & 0b111;
int byteIdx = y & 0b111;
Expand All @@ -153,7 +154,7 @@ public ChunkBuildOutput execute(ChunkBuildContext buildContext, CancellationToke
var blockPointer = traversableBlocksBuffer + byteIdx;
MemoryUtil.memPutByte(
blockPointer,
(byte) (MemoryUtil.memGetByte(blockPointer) | (1 << bitIdx))
(byte) (MemoryUtil.memGetByte(blockPointer) & ~(1 << bitIdx))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package net.caffeinemc.mods.sodium.client.render.chunk.occlusion;

import it.unimi.dsi.fastutil.longs.Long2ReferenceMap;
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection;
import net.caffeinemc.mods.sodium.client.render.viewport.Viewport;
import net.caffeinemc.mods.sodium.ffi.NativeCull;
import net.minecraft.core.SectionPos;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Pointer;

import java.io.Closeable;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -19,37 +24,76 @@ public NativeGraph(byte renderDistance, byte minSectionY, byte maxSectionY) {
);
}

public long search(MemoryStack stack, Viewport viewport, float searchDistance, boolean useOcclusionCulling) {
var returnValuePtr = stack.ncalloc(8, 16, 1);

// we push the stack again because we want to deallocate the camera info right after
// the function is run.
try (var argsStack = stack.push()) {
public void findVisible(
OcclusionCuller.Visitor visitor,
Long2ReferenceMap<RenderSection> sectionByPosition,
Viewport viewport,
float searchDistance,
boolean useOcclusionCulling,
int frame
) {
try (var stack = MemoryStack.stackPush()) {
var resultsPtr = stack.ncalloc(8, 16, 1);
var cameraPtr = NativeCull.frustumCreate(
argsStack,
stack,
viewport.getFrustumIntersection(),
viewport.getTransform()
);

lock.lock();
this.lock.lock();

try {
NativeCull.graphSearch(
returnValuePtr,
resultsPtr,
this.nativePtr,
cameraPtr,
searchDistance,
useOcclusionCulling
);

var tileCount = MemoryUtil.memGetAddress(resultsPtr);
var tileSlicePtr = MemoryUtil.memGetAddress(resultsPtr + Pointer.POINTER_SIZE);

for (int tileIdx = 0; tileIdx < tileCount; tileIdx++) {
var originSectionX = MemoryUtil.memGetInt(tileSlicePtr);
var originSectionY = MemoryUtil.memGetInt(tileSlicePtr + Integer.BYTES);
var originSectionZ = MemoryUtil.memGetInt(tileSlicePtr + (Integer.BYTES * 2));
var visibleSectionsPtr = MemoryUtil.memGetAddress(tileSlicePtr + 16);

for (int z = 0; z < 8; z++) {
var bits = MemoryUtil.memGetLong(visibleSectionsPtr + (z * Long.BYTES));
while (bits != 0) {
var bitIdx = Long.numberOfTrailingZeros(bits);
bits &= bits - 1;

// bits are ordered with the bit pattern of "ZZZYYYXXX".
// we have to disassemble bitIdx to retrieve our x and y coordinate
// of the set bit.
var x = bitIdx & 0b111;
var y = (bitIdx >> 3) & 0b111;
// if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
// y = 7 - y;
// }
long key = SectionPos.asLong(originSectionX + x, originSectionY + y, originSectionZ + z);
var section = sectionByPosition.get(key);

if (section != null) {
section.setLastVisibleFrame(frame);
visitor.visit(section);
}
}
}

tileSlicePtr += 24;
}
} finally {
lock.unlock();
this.lock.unlock();
}
}

return returnValuePtr;
}

public void setSection(int x, int y, int z, long traversableBlocksBuffer) {
lock.lock();
this.lock.lock();
try {
NativeCull.graphSetSection(
this.nativePtr,
Expand All @@ -59,12 +103,12 @@ public void setSection(int x, int y, int z, long traversableBlocksBuffer) {
traversableBlocksBuffer
);
} finally {
lock.unlock();
this.lock.unlock();
}
}

public void removeSection(int x, int y, int z) {
lock.lock();
this.lock.lock();
try {
NativeCull.graphRemoveSection(
this.nativePtr,
Expand All @@ -73,17 +117,17 @@ public void removeSection(int x, int y, int z) {
z
);
} finally {
lock.unlock();
this.lock.unlock();
}
}

@Override
public void close() {
lock.lock();
this.lock.lock();
try {
NativeCull.graphDelete(this.nativePtr);
} finally {
lock.unlock();
this.lock.unlock();
}
}
}
Binary file not shown.

0 comments on commit 2715111

Please sign in to comment.