Skip to content

Commit

Permalink
Use ByteBuffer overload when indirect buffer is in client memory
Browse files Browse the repository at this point in the history
Taken from GTNewHorizons/Angelica@22d28ab

Co-Authored-By: makamys <[email protected]>
  • Loading branch information
Asek3 and makamys committed Apr 5, 2024
1 parent c383b16 commit 29dc4a8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package me.jellysquid.mods.sodium.client.gl.device;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public interface DrawCommandList extends AutoCloseable {
void multiDrawArrays(IntBuffer first, IntBuffer count);

void multiDrawArraysIndirect(ByteBuffer pointer, int count, int stride);

void multiDrawArraysIndirect(long pointer, int count, int stride);

void endTessellating();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ public void multiDrawArrays(IntBuffer first, IntBuffer count) {
GL14.glMultiDrawArrays(primitiveType.getId(), first, count);
}

@Override
public void multiDrawArraysIndirect(ByteBuffer pointer, int count, int stride) {
final GlPrimitiveType primitiveType = GLRenderDevice.this.activeTessellation.getPrimitiveType();
GlFunctions.INDIRECT_DRAW.glMultiDrawArraysIndirect(primitiveType.getId(), pointer, count, stride);
}

@Override
public void multiDrawArraysIndirect(long pointer, int count, int stride) {
GlPrimitiveType primitiveType = GLRenderDevice.this.activeTessellation.getPrimitiveType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,37 @@
import org.lwjgl.opengl.ContextCapabilities;
import org.lwjgl.opengl.GL43;

import java.nio.ByteBuffer;

public enum GlIndirectMultiDrawFunctions {
CORE {
@Override
public void glMultiDrawArraysIndirect(int mode, ByteBuffer indirect, int primcount, int stride) {
GL43.glMultiDrawArraysIndirect(mode, indirect, primcount, stride);
}

@Override
public void glMultiDrawArraysIndirect(int mode, long indirect, int primcount, int stride) {
GL43.glMultiDrawArraysIndirect(mode, indirect, primcount, stride);
}
},
ARB {
@Override
public void glMultiDrawArraysIndirect(int mode, ByteBuffer indirect, int primcount, int stride) {
ARBMultiDrawIndirect.glMultiDrawArraysIndirect(mode, indirect, primcount, stride);
}

@Override
public void glMultiDrawArraysIndirect(int mode, long indirect, int primcount, int stride) {
ARBMultiDrawIndirect.glMultiDrawArraysIndirect(mode, indirect, primcount, stride);
}
},
UNSUPPORTED {
@Override
public void glMultiDrawArraysIndirect(int mode, ByteBuffer indirect, int primcount, int stride) {
throw new UnsupportedOperationException();
}

@Override
public void glMultiDrawArraysIndirect(int mode, long indirect, int primcount, int stride) {
throw new UnsupportedOperationException();
Expand All @@ -34,5 +51,6 @@ public static GlIndirectMultiDrawFunctions load(ContextCapabilities capabilities
}
}

public abstract void glMultiDrawArraysIndirect(int mode, ByteBuffer indirect, int primcount, int stride);
public abstract void glMultiDrawArraysIndirect(int mode, long indirect, int primcount, int stride);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.util.text.TextFormatting;
import org.lwjgl.opengl.GL11;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -216,19 +217,37 @@ public void render(CommandList commandList, ChunkRenderListIterator<MultidrawGra
commandList.uploadData(this.commandBuffer, this.commandClientBufferBuilder.getBuffer());
}

long pointer = this.commandBuffer == null ? this.commandClientBufferBuilder.getBufferAddress() : 0L;
long pointer = 0L;
ByteBuffer pointerBuffer;
int originalPointerBufferPos = 0;
if (this.commandBuffer != null) {
pointerBuffer = null;
} else {
pointerBuffer = this.commandClientBufferBuilder.getBuffer();
originalPointerBufferPos = pointerBuffer.position();
}

for (ChunkRegion<?> region : this.pendingBatches) {
ChunkDrawCallBatcher batch = region.getDrawBatcher();

if (!batch.isEmpty()) {
try (DrawCommandList drawCommandList = commandList.beginTessellating(region.getTessellation())) {
drawCommandList.multiDrawArraysIndirect(pointer, batch.getCount(), 0 /* tightly packed */);
if(pointerBuffer == null) {
drawCommandList.multiDrawArraysIndirect(pointer, batch.getCount(), 0 /* tightly packed */);
} else {
drawCommandList.multiDrawArraysIndirect(pointerBuffer, batch.getCount(), 0 /* tightly packed */);
}
}
}

pointer += batch.getArrayLength();
if(pointerBuffer == null) {
pointer += batch.getArrayLength();
} else {
pointerBuffer.position(pointerBuffer.position() + batch.getArrayLength());
}
}
if (pointerBuffer != null)
pointerBuffer.position(originalPointerBufferPos);

this.pendingBatches.clear();
}
Expand Down

0 comments on commit 29dc4a8

Please sign in to comment.