Skip to content

Commit

Permalink
Allow Enabling/Disabling Per RenderType AreaBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Nov 25, 2023
1 parent f79be80 commit 5d544c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/vulkanmod/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.vulkanmod.render.vertex.TerrainRenderType;

import java.io.FileReader;
import java.io.IOException;
Expand All @@ -19,7 +20,7 @@ public class Config {
public int advCulling = 2;
public boolean indirectDraw = false;
public boolean uniqueOpaqueLayer = false;
public boolean enableCutouts = true;
public boolean perRenderTypeAreaBuffers = true;
public boolean entityCulling = true;
public boolean animations = true;

Expand Down
15 changes: 11 additions & 4 deletions src/main/java/net/vulkanmod/config/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ 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 SwitchOption("Animations",
value -> config.animations = value,
() -> config.animations),
Expand Down Expand Up @@ -218,7 +215,17 @@ Enable Gui optimizations (Stats bar, Chat, Debug Hud)
() -> config.indirectDraw)
.setTooltip(Component.nullToEmpty("""
Reduces CPU overhead but increases GPU overhead.
Enabling it might help in CPU limited systems."""))
Enabling it might help in CPU limited systems.""")),
new SwitchOption("Per RenderType AreaBuffers",
value -> {
//fre before updating the Config Value
Minecraft.getInstance().levelRenderer.allChanged();
config.perRenderTypeAreaBuffers = value;
},
() -> config.perRenderTypeAreaBuffers).setTooltip(Component.nullToEmpty("""
Improves GPU Performance
But increases VRAM Usage slightly
May vary on System and/or GPU configuration""")),
};

}
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.vulkanmod.render.chunk;

import net.vulkanmod.Initializer;
import net.vulkanmod.render.chunk.build.UploadBuffer;
import net.vulkanmod.render.chunk.util.StaticQueue;
import net.vulkanmod.render.vertex.TerrainRenderType;
Expand All @@ -14,6 +15,7 @@

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

import static net.vulkanmod.render.vertex.TerrainRenderType.TRANSLUCENT;
import static net.vulkanmod.render.vertex.TerrainRenderType.getActiveLayers;
Expand All @@ -30,7 +32,7 @@ public class DrawBuffers {

private boolean allocated = false;
AreaBuffer indexBuffer;

AreaBuffer vertexBuffer;
private final EnumMap<TerrainRenderType, AreaBuffer> areaBufferTypes = new EnumMap<>(TerrainRenderType.class);

//Help JIT optimisations by hardcoding the queue size to the max possible ChunkArea limit
Expand All @@ -48,7 +50,7 @@ public DrawBuffers(int index, Vector3i origin, int minHeight) {
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);

if(!Initializer.CONFIG.perRenderTypeAreaBuffers) vertexBuffer = new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 3500000, VERTEX_SIZE);
//Don't allocate sectionQueues in constructor to avoid Unnecessary Heap Allocations
getActiveLayers().forEach(t -> sectionQueues.put(t, new StaticQueue<>(512)));
this.allocated = true;
Expand All @@ -71,6 +73,8 @@ public DrawParameters upload(int xOffset, int yOffset, int zOffset, UploadBuffer
}

if(!buffer.autoIndices) {
if (this.indexBuffer==null)
this.indexBuffer = new AreaBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, TRANSLUCENT.initialSize, INDEX_SIZE);
this.indexBuffer.upload(buffer.getIndexBuffer(), drawParameters.indexBufferSegment);
// drawParameters.firstIndex = drawParameters.indexBufferSegment.getOffset() / INDEX_SIZE;
firstIndex = drawParameters.indexBufferSegment.getOffset() / INDEX_SIZE;
Expand All @@ -89,12 +93,9 @@ public DrawParameters upload(int xOffset, int yOffset, int zOffset, UploadBuffer

return drawParameters;
}

//Exploit Pass by Reference to allow all keys to be the same AreaBufferObject (if perRenderTypeAreaBuffers is disabled)
private AreaBuffer getAreaBufferCheckedAlloc(TerrainRenderType r) {
if (this.indexBuffer==null && r == TRANSLUCENT)
this.indexBuffer = new AreaBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, r.initialSize, INDEX_SIZE);

return this.areaBufferTypes.computeIfAbsent(r, t -> new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, r.initialSize, VERTEX_SIZE));
return this.areaBufferTypes.computeIfAbsent(r, t -> Initializer.CONFIG.perRenderTypeAreaBuffers ? new AreaBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, r.initialSize, VERTEX_SIZE) : this.vertexBuffer);
}
private AreaBuffer getAreaBuffer(TerrainRenderType r) {
return this.areaBufferTypes.get(r);
Expand Down Expand Up @@ -267,12 +268,17 @@ public void releaseBuffers() {
if(!this.allocated)
return;

this.areaBufferTypes.values().forEach(AreaBuffer::freeBuffer);
if(Initializer.CONFIG.perRenderTypeAreaBuffers) {
this.areaBufferTypes.values().forEach(AreaBuffer::freeBuffer);
}
else if(this.vertexBuffer!=null) this.vertexBuffer.freeBuffer();

if(this.areaBufferTypes.containsKey(TRANSLUCENT)) this.indexBuffer.freeBuffer();
this.areaBufferTypes.clear();


this.indexBuffer = null;
this.vertexBuffer = null;
this.allocated = false;
}

Expand All @@ -287,7 +293,11 @@ public void addMeshlet(TerrainRenderType r, DrawParameters drawParameters) {
public void clear() {
this.sectionQueues.values().forEach(StaticQueue::clear);
}


public void addRenderTypes(Set<TerrainRenderType> renderTypes) {
renderTypes.forEach(renderType -> this.sectionQueues.computeIfAbsent(renderType, r->new StaticQueue<>(512)));
}

// public void clear(TerrainRenderType r) {
// this.sectionQueues.get(r).clear();
// }
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/vulkanmod/render/chunk/WorldRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ private void updateRenderChunks() {

if(!renderSection.isCompletelyEmpty()) {
final DrawBuffers drawBuffers = renderSection.getChunkArea().getDrawBuffers();
// drawBuffers.addRenderTypes(renderTypes);
for(var t : renderSection.getCompiledSection().renderTypes)
{
DrawBuffers.DrawParameters drawParameters = renderSection.getDrawParameters(t);
Expand Down Expand Up @@ -594,7 +595,7 @@ else if(terrainRenderType.equals(TRANSLUCENT))
p.push("draw batches");

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


Renderer.getInstance().bindGraphicsPipeline(terrainShader);
Expand Down

0 comments on commit 5d544c9

Please sign in to comment.