Skip to content

Commit

Permalink
Remove empty drawcall checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Oct 6, 2024
1 parent 4917c0d commit 3cd6d1e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
7 changes: 5 additions & 2 deletions src/main/java/net/vulkanmod/render/chunk/ChunkArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import net.vulkanmod.render.chunk.buffer.DrawBuffers;
import net.vulkanmod.render.chunk.frustum.VFrustum;
import net.vulkanmod.render.chunk.util.StaticQueue;
import net.vulkanmod.render.vertex.TerrainRenderType;
import org.joml.FrustumIntersection;
import org.joml.Vector3i;

import java.util.Arrays;
import java.util.EnumMap;

public class ChunkArea {
public final int index;
Expand All @@ -18,12 +20,13 @@ public class ChunkArea {
DrawBuffers drawBuffers;

//Help JIT optimisations by hardcoding the queue size to the max possible ChunkArea limit
public final StaticQueue<RenderSection> sectionQueue = new StaticQueue<>(512);
public final EnumMap<TerrainRenderType, StaticQueue<DrawBuffers.DrawParameters>> sectionQueue = new EnumMap<>(TerrainRenderType.class);

public ChunkArea(int i, Vector3i origin, int minHeight) {
this.index = i;
this.position = origin;
this.drawBuffers = new DrawBuffers(i, origin, minHeight);
TerrainRenderType.VALUES.forEach(terrainRenderType -> this.sectionQueue.put(terrainRenderType, new StaticQueue<>(512)));
}

public void updateFrustum(VFrustum frustum) {
Expand Down Expand Up @@ -123,7 +126,7 @@ public DrawBuffers getDrawBuffers() {
}

public void resetQueue() {
this.sectionQueue.clear();
this.sectionQueue.values().forEach(StaticQueue::clear);
}

public void setPosition(int x, int y, int z) {
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/net/vulkanmod/render/chunk/RenderSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.vulkanmod.render.vertex.TerrainRenderType;

import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -47,7 +48,7 @@ public class RenderSection {

public int xOffset, yOffset, zOffset;

private final DrawBuffers.DrawParameters[] drawParametersArray;
private final EnumMap<TerrainRenderType, DrawBuffers.DrawParameters> drawParametersArray = new EnumMap<>(TerrainRenderType.class);

// Graph-info
public byte mainDir;
Expand All @@ -60,11 +61,6 @@ public RenderSection(int index, int x, int y, int z) {
this.xOffset = x;
this.yOffset = y;
this.zOffset = z;

this.drawParametersArray = new DrawBuffers.DrawParameters[TerrainRenderType.VALUES.length];
for (int i = 0; i < this.drawParametersArray.length; ++i) {
this.drawParametersArray[i] = new DrawBuffers.DrawParameters();
}
}

public void setOrigin(int x, int y, int z) {
Expand Down Expand Up @@ -297,8 +293,8 @@ public int zOffset() {
return zOffset;
}

public DrawBuffers.DrawParameters getDrawParameters(TerrainRenderType renderType) {
return drawParametersArray[renderType.ordinal()];
public DrawBuffers.DrawParameters getDrawParametersChecked(TerrainRenderType renderType) {
return drawParametersArray.computeIfAbsent(renderType, terrainRenderType -> new DrawBuffers.DrawParameters());
}

public void setChunkArea(ChunkArea chunkArea) {
Expand Down Expand Up @@ -385,9 +381,9 @@ private void resetDrawParameters() {
if (this.chunkArea == null)
return;

for (TerrainRenderType r : TerrainRenderType.VALUES) {
this.getDrawParameters(r).reset(this.chunkArea, r);
}
this.drawParametersArray.forEach((key, value) -> value.reset(this.chunkArea, key));
this.drawParametersArray.clear();

}

public void setDirty(boolean playerChanged) {
Expand Down Expand Up @@ -418,6 +414,16 @@ public short getLastFrame() {
return this.lastFrame;
}

public void removeDrawParameter(TerrainRenderType renderType) {
if (this.drawParametersArray.containsKey(renderType)) {
this.drawParametersArray.remove(renderType).reset(this.chunkArea, renderType);
}
}

public void updateDrawParameters() {
this.drawParametersArray.forEach((key, value) -> this.chunkArea.sectionQueue.get(key).add(value));
}

static class CompileStatus {
CompiledSection compiledSection = CompiledSection.UNCOMPILED;
BuildTask buildTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public void renderSectionLayer(RenderType renderType, double camX, double camY,

for (Iterator<ChunkArea> iterator = this.sectionGraph.getChunkAreaQueue().iterator(isTranslucent); iterator.hasNext(); ) {
ChunkArea chunkArea = iterator.next();
var queue = chunkArea.sectionQueue;
var queue = chunkArea.sectionQueue.get(terrainRenderType);
DrawBuffers drawBuffers = chunkArea.drawBuffers;

if (drawBuffers.getAreaBuffer(terrainRenderType) != null && queue.size() > 0) {
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/net/vulkanmod/render/chunk/buffer/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public DrawBuffers(int index, Vector3i origin, int minHeight) {
}

public void upload(RenderSection section, UploadBuffer buffer, TerrainRenderType renderType) {
DrawParameters drawParameters = section.getDrawParameters(renderType);
DrawParameters drawParameters = section.getDrawParametersChecked(renderType);
int vertexOffset = drawParameters.vertexOffset;
int firstIndex = -1;

Expand Down Expand Up @@ -110,7 +110,7 @@ private void updateChunkAreaOrigin(VkCommandBuffer commandBuffer, Pipeline pipel
vkCmdPushConstants(commandBuffer, pipeline.getLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, byteBuffer);
}

public void buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, StaticQueue<RenderSection> queue, TerrainRenderType terrainRenderType) {
public void buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, StaticQueue<DrawParameters> queue, TerrainRenderType terrainRenderType) {

try (MemoryStack stack = MemoryStack.stackPush()) {

Expand All @@ -122,11 +122,7 @@ public void buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, StaticQueue<
int drawCount = 0;
for (var iterator = queue.iterator(isTranslucent); iterator.hasNext(); ) {

final RenderSection section = iterator.next();
final DrawParameters drawParameters = section.getDrawParameters(terrainRenderType);

if (drawParameters.indexCount <= 0)
continue;
final DrawParameters drawParameters = iterator.next();

long ptr = bufferPtr + (drawCount * 20L);
MemoryUtil.memPutInt(ptr, drawParameters.indexCount);
Expand All @@ -149,16 +145,12 @@ public void buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, StaticQueue<

}

public void buildDrawBatchesDirect(StaticQueue<RenderSection> queue, TerrainRenderType renderType) {
public void buildDrawBatchesDirect(StaticQueue<DrawParameters> queue, TerrainRenderType renderType) {
boolean isTranslucent = renderType == TerrainRenderType.TRANSLUCENT;
VkCommandBuffer commandBuffer = Renderer.getCommandBuffer();

for (var iterator = queue.iterator(isTranslucent); iterator.hasNext(); ) {
final RenderSection section = iterator.next();
final DrawParameters drawParameters = section.getDrawParameters(renderType);

if (drawParameters.indexCount <= 0)
continue;
final DrawParameters drawParameters = iterator.next();

final int firstIndex = drawParameters.firstIndex == -1 ? 0 : drawParameters.firstIndex;
vkCmdDrawIndexed(commandBuffer, drawParameters.indexCount, 1, firstIndex, drawParameters.vertexOffset, drawParameters.baseInstance);
Expand Down Expand Up @@ -211,8 +203,6 @@ public static class DrawParameters {
int vertexOffset = -1;
int baseInstance;

public DrawParameters() {}

public void reset(ChunkArea chunkArea, TerrainRenderType r) {
AreaBuffer areaBuffer = chunkArea.getDrawBuffers().getAreaBuffer(r);
if (areaBuffer != null && this.vertexOffset != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.jetbrains.annotations.Nullable;

import java.util.EnumMap;
import java.util.Queue;

public class TaskDispatcher {
Expand Down Expand Up @@ -166,7 +167,7 @@ private void doSectionUpdate(CompileResult compileResult) {
if(uploadBuffer != null) {
drawBuffers.upload(section, uploadBuffer, renderType);
} else {
section.getDrawParameters(renderType).reset(renderArea, renderType);
section.removeDrawParameter(renderType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void updateRenderChunks() {
continue;

if (!renderSection.isCompletelyEmpty()) {
renderSection.getChunkArea().sectionQueue.add(renderSection);
renderSection.updateDrawParameters();
this.chunkAreaQueue.add(renderSection.getChunkArea());
this.nonEmptyChunks++;
}
Expand Down Expand Up @@ -254,7 +254,7 @@ private void updateRenderChunksSpectator() {
continue;

if (!renderSection.isCompletelyEmpty()) {
renderSection.getChunkArea().sectionQueue.add(renderSection);
renderSection.updateDrawParameters();
this.chunkAreaQueue.add(renderSection.getChunkArea());
this.nonEmptyChunks++;
}
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/net/vulkanmod/render/vertex/TerrainRenderType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@ public enum TerrainRenderType {
CUTOUT(0.1f),
TRANSLUCENT(0.0f),
TRIPWIRE(0.1f);

public static final TerrainRenderType[] VALUES = TerrainRenderType.values();
//Changed to EnumSet, in case it generates better ASM than a standard array (e.g. Compile time length guarantees)
public static final EnumSet<TerrainRenderType> VALUES = EnumSet.allOf(TerrainRenderType.class);

public static final EnumSet<TerrainRenderType> COMPACT_RENDER_TYPES = EnumSet.of(CUTOUT_MIPPED, TRANSLUCENT);
public static final EnumSet<TerrainRenderType> SEMI_COMPACT_RENDER_TYPES = EnumSet.of(CUTOUT_MIPPED, CUTOUT, TRANSLUCENT);

static {
SEMI_COMPACT_RENDER_TYPES.add(CUTOUT);
SEMI_COMPACT_RENDER_TYPES.add(CUTOUT_MIPPED);
SEMI_COMPACT_RENDER_TYPES.add(TRANSLUCENT);

COMPACT_RENDER_TYPES.add(CUTOUT_MIPPED);
COMPACT_RENDER_TYPES.add(TRANSLUCENT);
}

public final float alphaCutout;

TerrainRenderType(float alphaCutout) {
Expand Down

0 comments on commit 3cd6d1e

Please sign in to comment.