|
| 1 | +package com.recursive_pineapple.matter_manipulator.common.items.manipulator; |
| 2 | + |
| 3 | +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; |
| 4 | + |
| 5 | +import org.intellij.lang.annotations.MagicConstant; |
| 6 | +import org.lwjgl.opengl.ARBBufferStorage; |
| 7 | +import org.lwjgl.opengl.GL15; |
| 8 | +import org.lwjgl.opengl.GL30; |
| 9 | + |
| 10 | +/// Note: this doesn't work properly for some reason, don't use it as-is |
| 11 | +public class FixedLengthVertexBuffer extends StreamingVertexBuffer { |
| 12 | + |
| 13 | + public FixedLengthVertexBuffer(VertexFormat format, int drawMode) { |
| 14 | + super(format, drawMode); |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public void reallocate() { |
| 19 | + generate(); |
| 20 | + |
| 21 | + // noinspection MagicConstant |
| 22 | + setSize(this.length, this.bufferFlags); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void allocate( |
| 27 | + int vertexCount, |
| 28 | + @MagicConstant(intValues = { |
| 29 | + ARBBufferStorage.GL_DYNAMIC_STORAGE_BIT, |
| 30 | + GL30.GL_MAP_READ_BIT, |
| 31 | + GL30.GL_MAP_WRITE_BIT, |
| 32 | + ARBBufferStorage.GL_MAP_PERSISTENT_BIT, |
| 33 | + ARBBufferStorage.GL_MAP_COHERENT_BIT, |
| 34 | + ARBBufferStorage.GL_CLIENT_STORAGE_BIT, |
| 35 | + }) int usage |
| 36 | + ) { |
| 37 | + // noinspection MagicConstant |
| 38 | + if (this.id == 0 || vertexCount < this.vertexCount / 4 || vertexCount > this.vertexCount || usage != this.bufferFlags) { |
| 39 | + generate(); |
| 40 | + |
| 41 | + setSize(vertexCount * (long) format.getVertexSize(), usage); |
| 42 | + } |
| 43 | + |
| 44 | + this.vertexCount = vertexCount; |
| 45 | + } |
| 46 | + |
| 47 | + public void setSize( |
| 48 | + long length, |
| 49 | + @MagicConstant(intValues = { |
| 50 | + ARBBufferStorage.GL_DYNAMIC_STORAGE_BIT, |
| 51 | + GL30.GL_MAP_READ_BIT, |
| 52 | + GL30.GL_MAP_WRITE_BIT, |
| 53 | + ARBBufferStorage.GL_MAP_PERSISTENT_BIT, |
| 54 | + ARBBufferStorage.GL_MAP_COHERENT_BIT, |
| 55 | + ARBBufferStorage.GL_CLIENT_STORAGE_BIT, |
| 56 | + }) int bufferFlags |
| 57 | + ) { |
| 58 | + if (this.length > 0) throw new IllegalStateException("Cannot resize an immutable (fixed length) vertex buffer"); |
| 59 | + |
| 60 | + bind(); |
| 61 | + |
| 62 | + this.length = length; |
| 63 | + this.bufferFlags = bufferFlags; |
| 64 | + ARBBufferStorage.glBufferStorage(GL15.GL_ARRAY_BUFFER, length, bufferFlags); |
| 65 | + |
| 66 | + unbind(); |
| 67 | + } |
| 68 | + |
| 69 | + public void flush() { |
| 70 | + bind(); |
| 71 | + GL30.glFlushMappedBufferRange(GL15.GL_ARRAY_BUFFER, 0, vertexCount * (long) format.getVertexSize()); |
| 72 | + unbind(); |
| 73 | + } |
| 74 | + |
| 75 | + public void flushAll() { |
| 76 | + bind(); |
| 77 | + GL30.glFlushMappedBufferRange(GL15.GL_ARRAY_BUFFER, 0, length); |
| 78 | + unbind(); |
| 79 | + } |
| 80 | +} |
0 commit comments