From a6b06580a64de383982dd74d8d7e27ac5019419c Mon Sep 17 00:00:00 2001 From: Vowxky Date: Sat, 23 May 2026 18:04:17 -0500 Subject: [PATCH] use PBO for VoxelShadowGrid texture upload --- .../client/render/light/VoxelShadowGrid.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/foundry/veil/impl/client/render/light/VoxelShadowGrid.java b/common/src/main/java/foundry/veil/impl/client/render/light/VoxelShadowGrid.java index 01d9de737..f538314dd 100644 --- a/common/src/main/java/foundry/veil/impl/client/render/light/VoxelShadowGrid.java +++ b/common/src/main/java/foundry/veil/impl/client/render/light/VoxelShadowGrid.java @@ -24,6 +24,8 @@ import static org.lwjgl.opengl.GL11C.*; import static org.lwjgl.opengl.GL12C.*; +import static org.lwjgl.opengl.GL15C.*; +import static org.lwjgl.opengl.GL21C.GL_PIXEL_UNPACK_BUFFER; import static org.lwjgl.opengl.GL30C.GL_R8; @ApiStatus.Internal @@ -41,6 +43,7 @@ public final class VoxelShadowGrid { private static final Vector3f uniformGridPos = new Vector3f(); private static int textureId; + private static int pboId; private static ResourceKey gridDimension; private static int originX, originY, originZ; @@ -151,6 +154,10 @@ public static void clearLevel() { public static void close() { RenderSystem.assertOnRenderThreadOrInit(); clearLevel(); + if (pboId != 0) { + glDeleteBuffers(pboId); + pboId = 0; + } if (textureId != 0) { glDeleteTextures(textureId); textureId = 0; @@ -421,19 +428,23 @@ private static byte voxelOccupancy(ClientLevel level, BlockPos pos, BlockState s } private static void uploadBuffer(ByteBuffer buffer) { - if (buffer == null) { + if (buffer == null || pboId == 0) { return; } - ByteBuffer upload = buffer.duplicate(); - upload.position(0); - upload.limit(GRID_VOLUME); + buffer.position(0); + buffer.limit(GRID_VOLUME); int unpackAlignment = glGetInteger(GL_UNPACK_ALIGNMENT); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboId); + glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer, GL_STREAM_DRAW); + glBindTexture(GL_TEXTURE_3D, textureId); - glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, GRID_SIZE, GRID_SIZE, GRID_SIZE, GL_RED, GL_UNSIGNED_BYTE, upload); + glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, GRID_SIZE, GRID_SIZE, GRID_SIZE, GL_RED, GL_UNSIGNED_BYTE, 0L); glBindTexture(GL_TEXTURE_3D, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlignment); } @@ -452,6 +463,11 @@ private static void ensureTexture() { glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, GRID_SIZE, GRID_SIZE, GRID_SIZE, 0, GL_RED, GL_UNSIGNED_BYTE, zeros); MemoryUtil.memFree(zeros); glBindTexture(GL_TEXTURE_3D, 0); + + pboId = glGenBuffers(); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboId); + glBufferData(GL_PIXEL_UNPACK_BUFFER, GRID_VOLUME, GL_STREAM_DRAW); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } private static boolean hasOccludedLights() {