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 333a145
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 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
18 changes: 14 additions & 4 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.nio.ByteBuffer;
import java.util.EnumMap;
import java.util.Set;

import static net.vulkanmod.render.vertex.TerrainRenderType.*;
import static org.lwjgl.vulkan.VK10.*;
Expand Down Expand Up @@ -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,6 +85,14 @@ 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);
}
Expand Down Expand Up @@ -251,7 +260,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
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 333a145

Please sign in to comment.