rasterized occlusion culling #38
No files matched your search
+4
-5
@@ -1,6 +1,5 @@
|
|||||||
package org.embeddedt.embeddium.impl.render.chunk.bench.voxel;
|
package org.embeddedt.embeddium.impl.render.chunk.bench.voxel;
|
||||||
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.OccluderBoxBuilder;
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.SectionVisibilityBuilder;
|
import org.embeddedt.embeddium.impl.render.chunk.occlusion.SectionVisibilityBuilder;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding;
|
import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding;
|
||||||
|
|
||||||
@@ -108,7 +107,7 @@ public final class VoxelSlice {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var builder = new OccluderBoxBuilder();
|
var occluder = new SectionVisibilityBuilder();
|
||||||
byte[] blocks = this.blocks;
|
byte[] blocks = this.blocks;
|
||||||
|
|
||||||
for (int y = 0; y < 16; y++) {
|
for (int y = 0; y < 16; y++) {
|
||||||
@@ -119,17 +118,17 @@ public final class VoxelSlice {
|
|||||||
int block = blocks[row + x];
|
int block = blocks[row + x];
|
||||||
|
|
||||||
if (VoxelWorld.isOpaque(block)) {
|
if (VoxelWorld.isOpaque(block)) {
|
||||||
builder.markOpaque(x, y, z);
|
occluder.markOpaque(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VoxelWorld.isRenderable(block)) {
|
if (VoxelWorld.isRenderable(block)) {
|
||||||
builder.markRenderable(x, y, z);
|
occluder.markRenderable(x, y, z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
return occluder.computeOccluderBoxes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long[] opacityBits() {
|
public long[] opacityBits() {
|
||||||
|
|||||||
-50
@@ -1,50 +0,0 @@
|
|||||||
package org.embeddedt.embeddium.impl.render.chunk.occlusion;
|
|
||||||
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.BoxFinder;
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.OccluderBoxes;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.BitSet;
|
|
||||||
|
|
||||||
// Separate from SectionVisibilityBuilder so a platform whose game already computes the visibility encoding
|
|
||||||
// can keep using that and supply opacity here instead of paying for a second flood fill it doesn't need
|
|
||||||
public final class OccluderBoxBuilder {
|
|
||||||
private final BitSet blocks = new BitSet(SectionVisibilityBuilder.TOTAL_BLOCKS);
|
|
||||||
private final RenderableBounds bounds = new RenderableBounds();
|
|
||||||
|
|
||||||
public void markOpaque(int x, int y, int z) {
|
|
||||||
this.blocks.set(SectionVisibilityBuilder.getIndex(x & 15, y & 15, z & 15));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void markRenderable(int x, int y, int z) {
|
|
||||||
this.bounds.mark(x & 15, y & 15, z & 15);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int @Nullable [] build() {
|
|
||||||
return dataFrom(this.blocks, this.bounds);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int bounds(int[] data) {
|
|
||||||
return data[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
static int @Nullable [] dataFrom(BitSet blocks, RenderableBounds bounds) {
|
|
||||||
if (blocks.isEmpty() && bounds.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
int[] boxes = blocks.isEmpty() ? null
|
|
||||||
: OccluderBoxes.from(Arrays.copyOf(blocks.toLongArray(), BoxFinder.INTERIOR_CACHE_WORDS));
|
|
||||||
|
|
||||||
int count = boxes == null ? 0 : boxes.length;
|
|
||||||
int[] data = new int[count + 1];
|
|
||||||
data[0] = bounds.packed();
|
|
||||||
|
|
||||||
if (boxes != null) {
|
|
||||||
System.arraycopy(boxes, 0, data, 1, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -833,7 +833,7 @@ public final class SectionLattice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int boundsOf(int @Nullable [] occluderData) {
|
private static int boundsOf(int @Nullable [] occluderData) {
|
||||||
return occluderData != null ? OccluderBoxBuilder.bounds(occluderData) : 0;
|
return occluderData != null ? SectionVisibilityBuilder.bounds(occluderData) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void install(RenderSection section) {
|
private void install(RenderSection section) {
|
||||||
|
|||||||
+35
-14
@@ -2,8 +2,11 @@ package org.embeddedt.embeddium.impl.render.chunk.occlusion;
|
|||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
|
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
|
||||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||||
|
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.BoxFinder;
|
||||||
|
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.OccluderBoxes;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,8 +31,6 @@ public class SectionVisibilityBuilder {
|
|||||||
private final BitSet blocks;
|
private final BitSet blocks;
|
||||||
private final RenderableBounds renderableBounds = new RenderableBounds();
|
private final RenderableBounds renderableBounds = new RenderableBounds();
|
||||||
|
|
||||||
private boolean floodFilled;
|
|
||||||
|
|
||||||
private static int[] buildFloodfillIndices() {
|
private static int[] buildFloodfillIndices() {
|
||||||
IntArrayList indicesList = new IntArrayList(TOTAL_BLOCKS - (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2));
|
IntArrayList indicesList = new IntArrayList(TOTAL_BLOCKS - (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2));
|
||||||
for (int x = 0; x < SECTION_AXIS_SIZE; x++) {
|
for (int x = 0; x < SECTION_AXIS_SIZE; x++) {
|
||||||
@@ -71,24 +72,45 @@ public class SectionVisibilityBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int @Nullable [] computeOccluderBoxes() {
|
public int @Nullable [] computeOccluderBoxes() {
|
||||||
if (this.floodFilled) {
|
return dataFrom(this.blocks, this.renderableBounds);
|
||||||
throw new IllegalStateException("opacity was consumed by computeVisibilityEncoding()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return OccluderBoxBuilder.dataFrom(this.blocks, this.renderableBounds);
|
static int bounds(int[] data) {
|
||||||
|
return data[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int @Nullable [] dataFrom(BitSet blocks, RenderableBounds bounds) {
|
||||||
|
if (blocks.isEmpty() && bounds.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] boxes = blocks.isEmpty() ? null
|
||||||
|
: OccluderBoxes.from(Arrays.copyOf(blocks.toLongArray(), BoxFinder.INTERIOR_CACHE_WORDS));
|
||||||
|
|
||||||
|
int count = boxes == null ? 0 : boxes.length;
|
||||||
|
int[] data = new int[count + 1];
|
||||||
|
data[0] = bounds.packed();
|
||||||
|
|
||||||
|
if (boxes != null) {
|
||||||
|
System.arraycopy(boxes, 0, data, 1, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long computeWithFloodFill() {
|
private long computeWithFloodFill() {
|
||||||
this.floodFilled = true;
|
|
||||||
long resultEncoding = 0;
|
long resultEncoding = 0;
|
||||||
var blocks = this.blocks;
|
// The fill marks cells as visited by setting their bit, which would corrupt the opacity data other
|
||||||
|
// callers (computeOccluderBoxes()) still need. Clone it into scratch space instead of mutating
|
||||||
|
// this.blocks in place; opaque cells start pre-marked since the fill must not pass through them.
|
||||||
|
BitSet visited = (BitSet) this.blocks.clone();
|
||||||
BitSet escapedFaces = new BitSet(GraphDirection.COUNT);
|
BitSet escapedFaces = new BitSet(GraphDirection.COUNT);
|
||||||
IntArrayFIFOQueue queue = new IntArrayFIFOQueue();
|
IntArrayFIFOQueue queue = new IntArrayFIFOQueue();
|
||||||
for (int i : INDICES_TO_INITIATE_FLOODFILL) {
|
for (int i : INDICES_TO_INITIATE_FLOODFILL) {
|
||||||
if (!blocks.get(i)) {
|
if (!visited.get(i)) {
|
||||||
escapedFaces.clear();
|
escapedFaces.clear();
|
||||||
queue.clear();
|
queue.clear();
|
||||||
this.exploreFrom(escapedFaces, queue, i);
|
exploreFrom(visited, escapedFaces, queue, i);
|
||||||
for (int dir = escapedFaces.nextSetBit(0); dir >= 0; dir = escapedFaces.nextSetBit(dir+1)) {
|
for (int dir = escapedFaces.nextSetBit(0); dir >= 0; dir = escapedFaces.nextSetBit(dir+1)) {
|
||||||
for (int dir2 = escapedFaces.nextSetBit(0); dir2 >= 0; dir2 = escapedFaces.nextSetBit(dir2+1)) {
|
for (int dir2 = escapedFaces.nextSetBit(0); dir2 >= 0; dir2 = escapedFaces.nextSetBit(dir2+1)) {
|
||||||
resultEncoding |= 1L << VisibilityEncoding.bit(dir, dir2);
|
resultEncoding |= 1L << VisibilityEncoding.bit(dir, dir2);
|
||||||
@@ -99,20 +121,19 @@ public class SectionVisibilityBuilder {
|
|||||||
return resultEncoding;
|
return resultEncoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exploreFrom(BitSet escapedFaces, IntArrayFIFOQueue queue, int startIndex) {
|
private static void exploreFrom(BitSet visited, BitSet escapedFaces, IntArrayFIFOQueue queue, int startIndex) {
|
||||||
queue.enqueue(startIndex);
|
queue.enqueue(startIndex);
|
||||||
var blocks = this.blocks;
|
|
||||||
// Mark the start location as handled
|
// Mark the start location as handled
|
||||||
blocks.set(startIndex, true);
|
visited.set(startIndex, true);
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
int idx = queue.dequeueInt();
|
int idx = queue.dequeueInt();
|
||||||
for (int dir = 0; dir < GraphDirection.COUNT; dir++) {
|
for (int dir = 0; dir < GraphDirection.COUNT; dir++) {
|
||||||
int neighborIdx = getNeighborIndex(idx, dir);
|
int neighborIdx = getNeighborIndex(idx, dir);
|
||||||
if (neighborIdx >= 0) {
|
if (neighborIdx >= 0) {
|
||||||
// We can move within the section in that direction
|
// We can move within the section in that direction
|
||||||
if (!blocks.get(neighborIdx)) {
|
if (!visited.get(neighborIdx)) {
|
||||||
// Mark this location as handled
|
// Mark this location as handled
|
||||||
blocks.set(neighborIdx, true);
|
visited.set(neighborIdx, true);
|
||||||
queue.enqueue(neighborIdx);
|
queue.enqueue(neighborIdx);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+9
-23
@@ -11,18 +11,15 @@ import net.minecraft.world.level.block.AirBlock;
|
|||||||
import org.embeddedt.embeddium.api.render.chunk.SectionInfoBuilder;
|
import org.embeddedt.embeddium.api.render.chunk.SectionInfoBuilder;
|
||||||
import org.embeddedt.embeddium.api.render.texture.SpriteUtil;
|
import org.embeddedt.embeddium.api.render.texture.SpriteUtil;
|
||||||
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.ModernChunkBuildContext;
|
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.ModernChunkBuildContext;
|
||||||
import org.embeddedt.embeddium.impl.modern.render.chunk.occlusion.ModernGraphDirection;
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.RenderSection;
|
import org.embeddedt.embeddium.impl.render.chunk.RenderSection;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.compile.*;
|
import org.embeddedt.embeddium.impl.render.chunk.compile.*;
|
||||||
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRenderCache;
|
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRenderCache;
|
||||||
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRenderContext;
|
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRenderContext;
|
||||||
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.GeometryCategory;
|
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.GeometryCategory;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.compile.tasks.ChunkBuilderTask;
|
import org.embeddedt.embeddium.impl.render.chunk.compile.tasks.ChunkBuilderTask;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.data.BuiltRenderSectionData;
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.data.BuiltSectionMeshParts;
|
import org.embeddedt.embeddium.impl.render.chunk.data.BuiltSectionMeshParts;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.data.MinecraftBuiltRenderSectionData;
|
import org.embeddedt.embeddium.impl.render.chunk.data.MinecraftBuiltRenderSectionData;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.OccluderBoxBuilder;
|
import org.embeddedt.embeddium.impl.render.chunk.occlusion.SectionVisibilityBuilder;
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding;
|
|
||||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
|
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
|
||||||
import org.embeddedt.embeddium.impl.util.WorldUtil;
|
import org.embeddedt.embeddium.impl.util.WorldUtil;
|
||||||
import org.embeddedt.embeddium.impl.util.task.CancellationToken;
|
import org.embeddedt.embeddium.impl.util.task.CancellationToken;
|
||||||
@@ -33,7 +30,6 @@ import net.minecraft.CrashReportCategory;
|
|||||||
import net.minecraft.ReportedException;
|
import net.minecraft.ReportedException;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
|
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
|
||||||
import net.minecraft.client.renderer.chunk.VisGraph;
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.world.level.block.RenderShape;
|
import net.minecraft.world.level.block.RenderShape;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
@@ -83,8 +79,7 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
|
|||||||
public ChunkBuildOutput execute(ChunkBuildContext jobContext, CancellationToken cancellationToken) {
|
public ChunkBuildOutput execute(ChunkBuildContext jobContext, CancellationToken cancellationToken) {
|
||||||
ModernChunkBuildContext buildContext = (ModernChunkBuildContext)jobContext;
|
ModernChunkBuildContext buildContext = (ModernChunkBuildContext)jobContext;
|
||||||
MinecraftBuiltRenderSectionData<TextureAtlasSprite, BlockEntity> renderData = new MinecraftBuiltRenderSectionData<>();
|
MinecraftBuiltRenderSectionData<TextureAtlasSprite, BlockEntity> renderData = new MinecraftBuiltRenderSectionData<>();
|
||||||
VisGraph occluder = new VisGraph();
|
SectionVisibilityBuilder occluder = new SectionVisibilityBuilder();
|
||||||
OccluderBoxBuilder occluderBoxes = this.rasterOcclusion ? new OccluderBoxBuilder() : null;
|
|
||||||
|
|
||||||
ChunkBuildBuffers buffers = buildContext.buffers;
|
ChunkBuildBuffers buffers = buildContext.buffers;
|
||||||
buffers.init(renderData, this.render.getSectionIndex());
|
buffers.init(renderData, this.render.getSectionIndex());
|
||||||
@@ -137,8 +132,8 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
|
|||||||
blockPos.set(x, y, z);
|
blockPos.set(x, y, z);
|
||||||
modelOffset.set(x & 15, y & 15, z & 15);
|
modelOffset.set(x & 15, y & 15, z & 15);
|
||||||
|
|
||||||
if (occluderBoxes != null) {
|
if (this.rasterOcclusion) {
|
||||||
occluderBoxes.markRenderable(x, y, z);
|
occluder.markRenderable(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
//? if shaders && >=1.17 {
|
//? if shaders && >=1.17 {
|
||||||
@@ -232,11 +227,7 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (blockState.isSolidRender(/*? if <1.21.11 {*/ slice, blockPos /*?}*/)) {
|
if (blockState.isSolidRender(/*? if <1.21.11 {*/ slice, blockPos /*?}*/)) {
|
||||||
occluder.setOpaque(blockPos);
|
occluder.markOpaque(x, y, z);
|
||||||
|
|
||||||
if (occluderBoxes != null) {
|
|
||||||
occluderBoxes.markOpaque(x, y, z);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,22 +248,17 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
|
|||||||
renderData.hasBlockGeometry = true;
|
renderData.hasBlockGeometry = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeVisibilityData(occluder, renderData);
|
if (this.rasterOcclusion) {
|
||||||
|
renderData.occluderBoxes = occluder.computeOccluderBoxes();
|
||||||
if (occluderBoxes != null) {
|
|
||||||
renderData.occluderBoxes = occluderBoxes.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderData.visibilityData = occluder.computeVisibilityEncoding();
|
||||||
|
|
||||||
postSectionDataBuiltEvent(renderData);
|
postSectionDataBuiltEvent(renderData);
|
||||||
|
|
||||||
return new ChunkBuildOutput(this.render, renderData, meshes, this.buildTime);
|
return new ChunkBuildOutput(this.render, renderData, meshes, this.buildTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void encodeVisibilityData(VisGraph occluder, BuiltRenderSectionData renderData) {
|
|
||||||
var data = occluder.resolve();
|
|
||||||
renderData.visibilityData = VisibilityEncoding.encode((from, to) -> data.visibilityBetween(ModernGraphDirection.toEnum(from), ModernGraphDirection.toEnum(to)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void postSectionDataBuiltEvent(MinecraftBuiltRenderSectionData<TextureAtlasSprite, BlockEntity> renderData) {
|
private static void postSectionDataBuiltEvent(MinecraftBuiltRenderSectionData<TextureAtlasSprite, BlockEntity> renderData) {
|
||||||
ChunkDataBuiltEvent.BUS.post(new ChunkDataBuiltEvent(new SectionInfoBuilder() {
|
ChunkDataBuiltEvent.BUS.post(new ChunkDataBuiltEvent(new SectionInfoBuilder() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
package org.embeddedt.embeddium.impl.modern.render.chunk.occlusion;
|
|
||||||
|
|
||||||
import net.minecraft.core.Direction;
|
|
||||||
|
|
||||||
import static org.embeddedt.embeddium.impl.render.chunk.occlusion.GraphDirection.*;
|
|
||||||
|
|
||||||
public class ModernGraphDirection {
|
|
||||||
private static final Direction[] ENUMS;
|
|
||||||
|
|
||||||
static {
|
|
||||||
ENUMS = new Direction[COUNT];
|
|
||||||
ENUMS[DOWN] = Direction.DOWN;
|
|
||||||
ENUMS[UP] = Direction.UP;
|
|
||||||
ENUMS[NORTH] = Direction.NORTH;
|
|
||||||
ENUMS[SOUTH] = Direction.SOUTH;
|
|
||||||
ENUMS[WEST] = Direction.WEST;
|
|
||||||
ENUMS[EAST] = Direction.EAST;
|
|
||||||
}
|
|
||||||
public static Direction toEnum(int direction) {
|
|
||||||
return ENUMS[direction];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user