Skip to content

Commit

Permalink
Switch from opaque to traversable blocks with native culling, add oth…
Browse files Browse the repository at this point in the history
…er debug stuff
  • Loading branch information
burgerindividual committed Dec 26, 2024
1 parent 85fbf4b commit 0e60be5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunkSection;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -161,15 +162,18 @@ private void createTerrainRenderList(Camera camera, Viewport viewport, int frame
var visitor = new VisibleChunkCollector(frame);

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

// TODO: actually read results
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
}
}
} else {
// this.occlusionCuller.findVisible(visitor, viewport, searchDistance, useOcclusionCulling, frame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ 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 opaqueBlocksBuffer = stack.ncalloc(64, 512, 1);
var traversableBlocksBuffer = stack.ncalloc(64, 512, 1);

for (int y = minY; y < maxY; y++) {
if (cancellationToken.isCancelled()) {
Expand Down Expand Up @@ -142,15 +142,15 @@ 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;
byteIdx |= (z & 0b1111) << 3;
byteIdx |= (y & 0b1000) << 4;
byteIdx |= (x & 0b1000) << 5;

var blockPointer = opaqueBlocksBuffer + byteIdx;
var blockPointer = traversableBlocksBuffer + byteIdx;
MemoryUtil.memPutByte(
blockPointer,
(byte) (MemoryUtil.memGetByte(blockPointer) | (1 << bitIdx))
Expand All @@ -166,7 +166,7 @@ public ChunkBuildOutput execute(ChunkBuildContext buildContext, CancellationToke
this.render.getChunkX(),
this.render.getChunkY(),
this.render.getChunkZ(),
opaqueBlocksBuffer
traversableBlocksBuffer
);
}
} catch (ReportedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public long search(MemoryStack stack, Viewport viewport, float searchDistance, b
return returnValuePtr;
}

public void setSection(int x, int y, int z, long opaqueBlocksBuffer) {
public void setSection(int x, int y, int z, long traversableBlocksBuffer) {
lock.lock();
try {
NativeCull.graphSetSection(
this.nativePtr,
x,
y,
z,
opaqueBlocksBuffer
traversableBlocksBuffer
);
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ public static long frustumCreate(MemoryStack stack, FrustumIntersection frustum,
public static native long graphCreate(byte render_distance, byte world_bottom_section_y, byte world_top_section_y);

/**
* @param graph_ptr Rust Type: {@code *mut Graph}
* @param x Rust Type: {@code i32}
* @param y Rust Type: {@code i32}
* @param z Rust Type: {@code i32}
* @param opaque_blocks_ptr Rust Type: {@code *const FFISectionOpaqueBlocks}
* @param graph_ptr Rust Type: {@code *mut Graph}
* @param x Rust Type: {@code i32}
* @param y Rust Type: {@code i32}
* @param z Rust Type: {@code i32}
* @param traversable_blocks_ptr Rust Type: {@code *const FFISectionOpaqueBlocks}
*/
public static native void graphSetSection(long graph_ptr, int x, int y, int z, long opaque_blocks_ptr);
public static native void graphSetSection(long graph_ptr, int x, int y, int z, long traversable_blocks_ptr);

/**
* @param graph_ptr Rust Type: {@code *mut Graph}
Expand Down
Binary file not shown.

0 comments on commit 0e60be5

Please sign in to comment.