diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/glObject/buffer/GLBuffer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/glObject/buffer/GLBuffer.java index 908472614..ef6385e2c 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/glObject/buffer/GLBuffer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/glObject/buffer/GLBuffer.java @@ -140,6 +140,12 @@ public class GLBuffer implements AutoCloseable { GL32.glDeleteBuffers(id); bufferCount.decrementAndGet(); + + //LOGGER.info("destroyed buffer ["+id+"], remaining: ["+BUFFER_ID_TO_PHANTOM.size()+"]"); + } + else + { + LOGGER.warn("Attempted to destroy invalid buffer ["+id+"], remaining: ["+BUFFER_ID_TO_PHANTOM.size()+"]"); } } } @@ -167,13 +173,8 @@ public class GLBuffer implements AutoCloseable return; } - boolean useBuffStorage = uploadMethod.useBufferStorage; - if (useBuffStorage != this.bufferStorage) - { - this.destroy(false); - this.create(useBuffStorage); - this.bind(); - } + // make sure the buffer is ready for uploading + this.createOrChangeBufferTypeForUpload(uploadMethod); switch (uploadMethod) { @@ -240,13 +241,9 @@ public class GLBuffer implements AutoCloseable LodUtil.assertTrue(uploadMethod.useEarlyMapping, "Upload method must be one that use early mappings in order to call mapBuffer"); LodUtil.assertTrue(!this.isMapped, "Buffer is already mapped"); - boolean useBuffStorage = uploadMethod.useBufferStorage; - if (useBuffStorage != this.bufferStorage) - { - this.destroy(false); - this.create(useBuffStorage); - } - this.bind(); + // make sure the buffer is ready for uploading + this.createOrChangeBufferTypeForUpload(uploadMethod); + ByteBuffer vboBuffer; if (this.size < targetSize || this.size > targetSize * BUFFER_SHRINK_TRIGGER) @@ -300,6 +297,40 @@ public class GLBuffer implements AutoCloseable + //================// + // helper methods // + //================// + + /** + * Makes sure the buffer exists and is of the correct format + * before uploading. + */ + private void createOrChangeBufferTypeForUpload(EGpuUploadMethod uploadMethod) + { + // create/change the buffer type if necessary + if (uploadMethod.useBufferStorage != this.bufferStorage) + { + // recreate if the buffer storage type changed + this.bind(); + this.destroy(false); + this.create(uploadMethod.useBufferStorage); + this.bind(); + } + else + { + // Prevent uploading to the null buffer (ID 0). + // This can happen if the buffer was deleted previously. + if (this.id == 0) + { + this.create(this.bufferStorage); + } + + this.bind(); + } + } + + + //================// // static cleanup // //================// diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/DebugRenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/DebugRenderer.java index 4d205270a..acbb6a88f 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/DebugRenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/DebugRenderer.java @@ -183,7 +183,6 @@ public class DebugRenderer buffer.rewind(); this.boxOutlineBuffer = new GLElementBuffer(false); - this.boxOutlineBuffer.bind(); this.boxOutlineBuffer.uploadBuffer(buffer, EGpuUploadMethod.DATA, box_outline_indices.length * Integer.BYTES, GL32.GL_STATIC_DRAW); }