From c0d5c3276633100c2f0841834ebbc4a7f022d379 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 10 Dec 2021 00:07:52 -0800 Subject: [PATCH] Debugging changes - MapBufferRange compat layer not needed, missed that - Don't need MappedFullBuffer anymore - Add guard for size in GPULightVolume#bind - LightVolume#getStride not needed - Replace usaged of GlError#poll with #pollAndThrow - VecBufferConsumer doesn't need a format --- .../flywheel/backend/gl/GlTexture.java | 4 ++ .../backend/gl/buffer/MappedBufferRange.java | 11 ++--- .../backend/gl/buffer/MappedFullBuffer.java | 32 ------------ .../backend/gl/buffer/MappedGlBuffer.java | 11 +---- .../backend/gl/buffer/PersistentGlBuffer.java | 6 +-- .../gl/buffer/PersistentMappedBuffer.java | 10 ++-- .../flywheel/backend/gl/buffer/VecBuffer.java | 4 +- .../flywheel/backend/gl/error/GlError.java | 2 +- .../backend/gl/versioned/GlCompat.java | 3 -- .../backend/gl/versioned/MapBufferRange.java | 49 ------------------- .../flywheel/backend/model/BufferedModel.java | 4 +- .../flywheel/backend/model/ModelPool.java | 6 +-- ...fferConsumer.java => VecBufferWriter.java} | 8 +-- .../flywheel/light/GPULightVolume.java | 25 +++++----- .../jozufozu/flywheel/light/LightVolume.java | 17 ++----- 15 files changed, 42 insertions(+), 150 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedFullBuffer.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/versioned/MapBufferRange.java rename src/main/java/com/jozufozu/flywheel/core/model/{VecBufferConsumer.java => VecBufferWriter.java} (82%) diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlTexture.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlTexture.java index 76e637a6e..bc42e26bd 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlTexture.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlTexture.java @@ -22,4 +22,8 @@ public class GlTexture extends GlObject { public void unbind() { GL20.glBindTexture(textureType, 0); } + + public void setParameteri(int parameter, int value) { + GL20.glTexParameteri(textureType, parameter, value); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java index 47c6d764a..9385e3efd 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java @@ -1,9 +1,12 @@ package com.jozufozu.flywheel.backend.gl.buffer; +import org.lwjgl.opengl.GL30; + import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.gl.error.GlException; import com.jozufozu.flywheel.util.StringUtil; +import com.mojang.blaze3d.platform.GlStateManager; public class MappedBufferRange extends MappedBuffer { @@ -28,13 +31,9 @@ public class MappedBufferRange extends MappedBuffer { @Override protected void checkAndMap() { if (!mapped) { - setInternal(Backend.getInstance().compat.mapBufferRange.mapBuffer(owner.type, offset, length, access)); + setInternal(GL30.glMapBufferRange(owner.type.glEnum, offset, length, access)); - GlError error = GlError.poll(); - - if (error != null) { - throw new GlException(error, StringUtil.args("mapBufferRange", owner.type, offset, length, access)); - } + GlError.pollAndThrow(() -> StringUtil.args("mapBufferRange", owner.type, offset, length, access)); mapped = true; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedFullBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedFullBuffer.java deleted file mode 100644 index 4ed9f1fde..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedFullBuffer.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jozufozu.flywheel.backend.gl.buffer; - -import org.lwjgl.opengl.GL15; - -import com.jozufozu.flywheel.backend.gl.error.GlError; -import com.jozufozu.flywheel.backend.gl.error.GlException; -import com.jozufozu.flywheel.util.StringUtil; - -public class MappedFullBuffer extends MappedBuffer { - - MappedBufferUsage usage; - - public MappedFullBuffer(GlBuffer buffer, MappedBufferUsage usage) { - super(buffer); - this.usage = usage; - } - - @Override - protected void checkAndMap() { - if (!mapped) { - setInternal(GL15.glMapBuffer(owner.type.glEnum, usage.glEnum)); - - GlError error = GlError.poll(); - - if (error != null) { - throw new GlException(error, StringUtil.args("mapBuffer", owner.type, usage)); - } - - mapped = true; - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java index e05362a46..06ee17cfa 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedGlBuffer.java @@ -5,9 +5,6 @@ import java.nio.ByteBuffer; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL30; -import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.gl.versioned.MapBufferRange; - public class MappedGlBuffer extends GlBuffer { protected final GlBufferUsage usage; @@ -30,12 +27,6 @@ public class MappedGlBuffer extends GlBuffer { } public MappedBuffer getBuffer(int offset, int length) { - if (Backend.getInstance().compat.mapBufferRange != MapBufferRange.UNSUPPORTED) { - return new MappedBufferRange(this, offset, length, GL30.GL_MAP_WRITE_BIT); - } else { - MappedFullBuffer fullBuffer = new MappedFullBuffer(this, MappedBufferUsage.WRITE_ONLY); - fullBuffer.position(offset); - return fullBuffer; - } + return new MappedBufferRange(this, offset, length, GL30.GL_MAP_WRITE_BIT); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java index 6c5188dab..e5dd4348d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentGlBuffer.java @@ -48,11 +48,7 @@ public class PersistentGlBuffer extends GlBuffer { Backend.getInstance().compat.bufferStorage.bufferStorage(type, size, flags); - GlError error = GlError.poll(); - - if (error != null) { - throw new GlException(error, StringUtil.args("bufferStorage", type, size, flags)); - } + GlError.pollAndThrow(() -> StringUtil.args("bufferStorage", type, size, flags)); buffer = new PersistentMappedBuffer(this); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentMappedBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentMappedBuffer.java index e2f46728f..124c3bf58 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentMappedBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/PersistentMappedBuffer.java @@ -2,6 +2,8 @@ package com.jozufozu.flywheel.backend.gl.buffer; import java.nio.ByteBuffer; +import org.lwjgl.opengl.GL30; + import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.gl.error.GlException; @@ -19,13 +21,9 @@ public class PersistentMappedBuffer extends MappedBuffer { offset = 0; length = owner.size; - ByteBuffer byteBuffer = Backend.getInstance().compat.mapBufferRange.mapBuffer(owner.type, offset, length, owner.flags); + ByteBuffer byteBuffer = GL30.glMapBufferRange(owner.type.glEnum, offset, length, owner.flags); - GlError error = GlError.poll(); - - if (error != null) { - throw new GlException(error, StringUtil.args("mapBuffer", owner.type, offset, length, owner.flags)); - } + GlError.pollAndThrow(() -> StringUtil.args("mapBuffer", owner.type, offset, length, owner.flags)); setInternal(byteBuffer); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java index 96171c80b..9d36890f1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java @@ -5,6 +5,8 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; +import javax.annotation.Nullable; + public class VecBuffer { protected ByteBuffer internal; @@ -22,7 +24,7 @@ public class VecBuffer { return new VecBuffer(buffer); } - protected void setInternal(ByteBuffer internal) { + protected void setInternal(@Nullable ByteBuffer internal) { this.internal = internal; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java index 0e133d1c8..115ca765d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java @@ -35,7 +35,7 @@ public enum GlError { this.glEnum = glEnum; } - + // Great for use in your debugger's expression evaluator public static GlError poll() { return errorLookup.get(GL20.glGetError()); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java index 704d85bd7..9abdebfed 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java @@ -16,14 +16,11 @@ import org.lwjgl.system.MemoryUtil; * system. */ public class GlCompat { - public final MapBufferRange mapBufferRange; public final InstancedArrays instancedArrays; public final BufferStorage bufferStorage; public GlCompat(GLCapabilities caps) { - mapBufferRange = getLatest(MapBufferRange.class, caps); - instancedArrays = getLatest(InstancedArrays.class, caps); bufferStorage = getLatest(BufferStorage.class, caps); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/MapBufferRange.java b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/MapBufferRange.java deleted file mode 100644 index 85f75bc2c..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/MapBufferRange.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.jozufozu.flywheel.backend.gl.versioned; - -import java.nio.ByteBuffer; - -import org.lwjgl.opengl.ARBMapBufferRange; -import org.lwjgl.opengl.GL30; -import org.lwjgl.opengl.GLCapabilities; - -import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; - -public enum MapBufferRange implements GlVersioned { - - GL30_RANGE { - @Override - public boolean supported(GLCapabilities caps) { - return caps.OpenGL30; - } - - @Override - public ByteBuffer mapBuffer(GlBufferType target, long offset, long length, int access) { - return GL30.glMapBufferRange(target.glEnum, offset, length, access); - } - }, - ARB_RANGE { - @Override - public boolean supported(GLCapabilities caps) { - return caps.GL_ARB_map_buffer_range; - } - - @Override - public ByteBuffer mapBuffer(GlBufferType target, long offset, long length, int access) { - return ARBMapBufferRange.glMapBufferRange(target.glEnum, offset, length, access); - } - }, - UNSUPPORTED { - @Override - public boolean supported(GLCapabilities caps) { - return true; - } - - @Override - public ByteBuffer mapBuffer(GlBufferType target, long offset, long length, int access) { - throw new UnsupportedOperationException("glMapBuffer not supported"); - } - }; - - - public abstract ByteBuffer mapBuffer(GlBufferType target, long offset, long length, int access); -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java index da8b26bc5..14fe216ac 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java @@ -11,7 +11,7 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.core.model.Model; -import com.jozufozu.flywheel.core.model.VecBufferConsumer; +import com.jozufozu.flywheel.core.model.VecBufferWriter; import com.jozufozu.flywheel.util.AttribUtil; public class BufferedModel implements IBufferedModel { @@ -33,7 +33,7 @@ public class BufferedModel implements IBufferedModel { // mirror it in system memory so we can write to it, and upload our model. MappedBuffer buffer = vbo.getBuffer(0, model.size()); - model.buffer(new VecBufferConsumer(buffer, model.format())); + model.buffer(new VecBufferWriter(buffer)); buffer.flush(); vbo.unbind(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java index 9c64c6469..645858ede 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.core.model.Model; -import com.jozufozu.flywheel.core.model.VecBufferConsumer; +import com.jozufozu.flywheel.core.model.VecBufferWriter; import com.jozufozu.flywheel.util.AttribUtil; public class ModelPool implements ModelAllocator { @@ -117,7 +117,7 @@ public class ModelPool implements ModelAllocator { private void uploadAll() { MappedBuffer buffer = vbo.getBuffer(0, bufferSize); - VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); + VecBufferWriter consumer = new VecBufferWriter(buffer); for (PooledModel model : models) { model.model.buffer(consumer); @@ -130,7 +130,7 @@ public class ModelPool implements ModelAllocator { private void uploadPending() { MappedBuffer buffer = vbo.getBuffer(0, bufferSize); - VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); + VecBufferWriter consumer = new VecBufferWriter(buffer); int stride = format.getStride(); for (PooledModel model : pendingUpload) { diff --git a/src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java b/src/main/java/com/jozufozu/flywheel/core/model/VecBufferWriter.java similarity index 82% rename from src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java rename to src/main/java/com/jozufozu/flywheel/core/model/VecBufferWriter.java index 221193f06..fefc5cf80 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/VecBufferConsumer.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/VecBufferWriter.java @@ -2,19 +2,15 @@ package com.jozufozu.flywheel.core.model; import static com.jozufozu.flywheel.util.RenderMath.nb; -import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.mojang.blaze3d.vertex.VertexConsumer; -public class VecBufferConsumer implements VertexConsumer { - +public class VecBufferWriter implements VertexConsumer { private final VecBuffer buffer; - private final VertexFormat format; - public VecBufferConsumer(VecBuffer buffer, VertexFormat format) { + public VecBufferWriter(VecBuffer buffer) { this.buffer = buffer; - this.format = format; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java b/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java index 80ca9292d..d463a54d7 100644 --- a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java @@ -44,8 +44,10 @@ public class GPULightVolume extends LightVolume { glTexture = new GlTexture(GL_TEXTURE_3D); + GlTextureUnit oldState = GlTextureUnit.getActive(); + // allocate space for the texture - glActiveTexture(GL_TEXTURE4); + textureUnit.makeActive(); glTexture.bind(); int sizeX = box.sizeX(); @@ -54,7 +56,7 @@ public class GPULightVolume extends LightVolume { glTexImage3D(GL_TEXTURE_3D, 0, GL30.GL_RG8, sizeX, sizeY, sizeZ, 0, GL30.GL_RG, GL_UNSIGNED_BYTE, 0); glTexture.unbind(); - glActiveTexture(GL_TEXTURE0); + oldState.makeActive(); } @Override @@ -67,15 +69,15 @@ public class GPULightVolume extends LightVolume { public void bind() { // just in case something goes wrong, or we accidentally call this before this volume is properly disposed of. - if (lightData == null) return; + if (lightData == null || lightData.capacity() == 0) return; textureUnit.makeActive(); glTexture.bind(); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); + glTexture.setParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexture.setParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexture.setParameteri(GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + glTexture.setParameteri(GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); + glTexture.setParameteri(GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); uploadTexture(); } @@ -87,7 +89,7 @@ public class GPULightVolume extends LightVolume { glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0); glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0); - glPixelStorei(GL_UNPACK_ALIGNMENT, 2); + glPixelStorei(GL_UNPACK_ALIGNMENT, 2); // we use 2 bytes per texel int sizeX = box.sizeX(); int sizeY = box.sizeY(); int sizeZ = box.sizeZ(); @@ -146,11 +148,6 @@ public class GPULightVolume extends LightVolume { bufferDirty = true; } - @Override - protected int getStride() { - return 2; - } - @Override public ImmutableBox getVolume() { return sampleVolume; diff --git a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java index 27f40f070..16a86a345 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java @@ -15,7 +15,7 @@ public class LightVolume implements ImmutableBox, LightListener { public LightVolume(ImmutableBox sampleVolume) { this.setBox(sampleVolume); - this.lightData = MemoryUtil.memAlloc(this.box.volume() * getStride()); + this.lightData = MemoryUtil.memAlloc(this.box.volume() * 2); } protected void setBox(ImmutableBox box) { @@ -58,9 +58,9 @@ public class LightVolume implements ImmutableBox, LightListener { if (lightData == null) return; setBox(newSampleVolume); - int volume = box.volume(); - if (volume * 2 > lightData.capacity()) { - lightData = MemoryUtil.memRealloc(lightData, volume * 2); + int neededCapacity = box.volume() * 2; + if (neededCapacity > lightData.capacity()) { + lightData = MemoryUtil.memRealloc(lightData, neededCapacity); } initialize(world); } @@ -198,14 +198,7 @@ public class LightVolume implements ImmutableBox, LightListener { } protected int boxPosToBufferIndex(int x, int y, int z) { - return (x + box.sizeX() * (y + z * box.sizeY())) * getStride(); - } - - /** - * @return The stride of the texels, in bytes. - */ - protected int getStride() { - return 2; + return (x + box.sizeX() * (y + z * box.sizeY())) * 2; } @Override