Skip to content

Commit

Permalink
[VRAM optimisation] Selectively allocate AreaBuffers based on needed …
Browse files Browse the repository at this point in the history
…TerrainRenderType
  • Loading branch information
thr3343 committed Nov 21, 2023
1 parent e395109 commit d5349e2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/vulkanmod/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Config {
public int advCulling = 2;
public boolean indirectDraw = false;
public boolean uniqueOpaqueLayer = false;
public boolean enableCutouts = true;
public boolean entityCulling = true;

private static Path path;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/vulkanmod/config/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public static Option<?>[] getGraphicsOpts() {
.setTooltip(Component.nullToEmpty("""
Improves performance by using a unique render layer for opaque terrain rendering.
It changes distant grass aspect and may cause unexpected texture behaviour""")),
new SwitchOption("enableCutouts",
value -> config.enableCutouts = value,
() -> config.enableCutouts),
new CyclingOption<>("Mipmap Levels",
new Integer[]{0, 1, 2, 3, 4},
value -> Component.nullToEmpty(value.toString()),
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.nio.ByteBuffer;
import java.util.EnumMap;

import static net.vulkanmod.render.vertex.TerrainRenderType.*;
import static net.vulkanmod.render.vertex.TerrainRenderType.TRANSLUCENT;
import static net.vulkanmod.render.vertex.TerrainRenderType.getActiveLayers;
import static org.lwjgl.vulkan.VK10.*;

public class DrawBuffers {
Expand All @@ -42,8 +43,8 @@ public DrawBuffers(int index, Vector3i origin) {
}

public void allocateBuffers() {
getActiveLayers().forEach(t -> areaBufferTypes.put(t, new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, t.initialSize, VERTEX_SIZE)));
this.indexBuffer = new AreaBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 1000000, INDEX_SIZE);
// getActiveLayers().forEach(t -> areaBufferTypes.put(t, new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, t.initialSize, VERTEX_SIZE)));
// this.indexBuffer = new AreaBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 1000000, INDEX_SIZE);

this.allocated = true;
}
Expand All @@ -54,7 +55,7 @@ public DrawParameters upload(int xOffset, int yOffset, int zOffset, UploadBuffer
drawParameters.baseInstance = encodeSectionOffset(xOffset, yOffset, zOffset);

if(!buffer.indexOnly) {
getAreaBuffer(r).upload(buffer.getVertexBuffer(), drawParameters.vertexBufferSegment);
getAreaBufferCheckedAlloc(r).upload(buffer.getVertexBuffer(), drawParameters.vertexBufferSegment);
// drawParameters.vertexOffset = drawParameters.vertexBufferSegment.getOffset() / VERTEX_SIZE;
vertexOffset = drawParameters.vertexBufferSegment.getOffset() / VERTEX_SIZE;

Expand Down Expand Up @@ -84,10 +85,22 @@ public DrawParameters upload(int xOffset, int yOffset, int zOffset, UploadBuffer
return drawParameters;
}

private AreaBuffer getAreaBufferCheckedAlloc(TerrainRenderType r) {
if(!this.areaBufferTypes.containsKey(r))
{
if(r==TRANSLUCENT) this.indexBuffer=new AreaBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, r.initialSize, INDEX_SIZE);
this.areaBufferTypes.put(r, new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, r.initialSize, VERTEX_SIZE));
}
return this.areaBufferTypes.get(r);
}
private AreaBuffer getAreaBuffer(TerrainRenderType r) {
return this.areaBufferTypes.get(r);
}

private boolean hasRenderType(TerrainRenderType r) {
return this.areaBufferTypes.containsKey(r);
}

private static int encodeSectionOffset(int xOffset, int yOffset, int zOffset) {
final int xOffset1 = (xOffset & 127);
final int zOffset1 = (zOffset & 127);
Expand Down Expand Up @@ -224,7 +237,7 @@ private static void fakeIndirectCmd(VkCommandBuffer commandBuffer, IndirectBuffe
}

public void buildDrawBatchesDirect(TerrainRenderType terrainRenderType, double camX, double camY, double camZ) {

if(!this.hasRenderType(terrainRenderType)) return;
boolean isTranslucent = terrainRenderType == TRANSLUCENT;

VkCommandBuffer commandBuffer = Renderer.getCommandBuffer();
Expand All @@ -251,7 +264,8 @@ public void releaseBuffers() {
return;

this.areaBufferTypes.values().forEach(AreaBuffer::freeBuffer);
this.indexBuffer.freeBuffer();
if(this.areaBufferTypes.containsKey(TRANSLUCENT)) this.indexBuffer.freeBuffer();
this.areaBufferTypes.clear();


this.indexBuffer = null;
Expand Down Expand Up @@ -293,7 +307,7 @@ public void reset(ChunkArea chunkArea, TerrainRenderType r) {
this.vertexOffset = 0;

int segmentOffset = this.vertexBufferSegment.getOffset();
if(chunkArea != null && chunkArea.drawBuffers.isAllocated() && segmentOffset != -1) {
if(chunkArea != null && chunkArea.drawBuffers.hasRenderType(r) && segmentOffset != -1) {
// this.chunkArea.drawBuffers.vertexBuffer.setSegmentFree(segmentOffset);
chunkArea.drawBuffers.getAreaBuffer(r).setSegmentFree(this.vertexBufferSegment);
if(r==TRANSLUCENT||this.indexBufferSegment!=null) chunkArea.drawBuffers.indexBuffer.setSegmentFree(this.indexBufferSegment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ else if(terrainRenderType.equals(TRANSLUCENT))
p.push("draw batches");

final int currentFrame = Renderer.getCurrentFrame();
if((Initializer.CONFIG.uniqueOpaqueLayer ? COMPACT_RENDER_TYPES : SEMI_COMPACT_RENDER_TYPES).contains(terrainRenderType)) {
if ((!Initializer.CONFIG.enableCutouts | Initializer.CONFIG.uniqueOpaqueLayer ? COMPACT_RENDER_TYPES : SEMI_COMPACT_RENDER_TYPES).contains(terrainRenderType)) {


Renderer.getInstance().bindGraphicsPipeline(terrainShader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.annotation.Nullable;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Queue;

public class TaskDispatcher {
Expand Down

0 comments on commit d5349e2

Please sign in to comment.