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
This commit is contained in:
Jozufozu 2021-12-10 00:07:52 -08:00
parent 2394b02e3d
commit c0d5c32766
15 changed files with 42 additions and 150 deletions

View file

@ -22,4 +22,8 @@ public class GlTexture extends GlObject {
public void unbind() { public void unbind() {
GL20.glBindTexture(textureType, 0); GL20.glBindTexture(textureType, 0);
} }
public void setParameteri(int parameter, int value) {
GL20.glTexParameteri(textureType, parameter, value);
}
} }

View file

@ -1,9 +1,12 @@
package com.jozufozu.flywheel.backend.gl.buffer; package com.jozufozu.flywheel.backend.gl.buffer;
import org.lwjgl.opengl.GL30;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.gl.error.GlError;
import com.jozufozu.flywheel.backend.gl.error.GlException; import com.jozufozu.flywheel.backend.gl.error.GlException;
import com.jozufozu.flywheel.util.StringUtil; import com.jozufozu.flywheel.util.StringUtil;
import com.mojang.blaze3d.platform.GlStateManager;
public class MappedBufferRange extends MappedBuffer { public class MappedBufferRange extends MappedBuffer {
@ -28,13 +31,9 @@ public class MappedBufferRange extends MappedBuffer {
@Override @Override
protected void checkAndMap() { protected void checkAndMap() {
if (!mapped) { 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(); GlError.pollAndThrow(() -> StringUtil.args("mapBufferRange", owner.type, offset, length, access));
if (error != null) {
throw new GlException(error, StringUtil.args("mapBufferRange", owner.type, offset, length, access));
}
mapped = true; mapped = true;
} }
} }

View file

@ -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;
}
}
}

View file

@ -5,9 +5,6 @@ import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.versioned.MapBufferRange;
public class MappedGlBuffer extends GlBuffer { public class MappedGlBuffer extends GlBuffer {
protected final GlBufferUsage usage; protected final GlBufferUsage usage;
@ -30,12 +27,6 @@ public class MappedGlBuffer extends GlBuffer {
} }
public MappedBuffer getBuffer(int offset, int length) { 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);
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;
}
} }
} }

View file

@ -48,11 +48,7 @@ public class PersistentGlBuffer extends GlBuffer {
Backend.getInstance().compat.bufferStorage.bufferStorage(type, size, flags); Backend.getInstance().compat.bufferStorage.bufferStorage(type, size, flags);
GlError error = GlError.poll(); GlError.pollAndThrow(() -> StringUtil.args("bufferStorage", type, size, flags));
if (error != null) {
throw new GlException(error, StringUtil.args("bufferStorage", type, size, flags));
}
buffer = new PersistentMappedBuffer(this); buffer = new PersistentMappedBuffer(this);
} }

View file

@ -2,6 +2,8 @@ package com.jozufozu.flywheel.backend.gl.buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL30;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.gl.error.GlError;
import com.jozufozu.flywheel.backend.gl.error.GlException; import com.jozufozu.flywheel.backend.gl.error.GlException;
@ -19,13 +21,9 @@ public class PersistentMappedBuffer extends MappedBuffer {
offset = 0; offset = 0;
length = owner.size; 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(); GlError.pollAndThrow(() -> StringUtil.args("mapBuffer", owner.type, offset, length, owner.flags));
if (error != null) {
throw new GlException(error, StringUtil.args("mapBuffer", owner.type, offset, length, owner.flags));
}
setInternal(byteBuffer); setInternal(byteBuffer);
} }

View file

@ -5,6 +5,8 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import javax.annotation.Nullable;
public class VecBuffer { public class VecBuffer {
protected ByteBuffer internal; protected ByteBuffer internal;
@ -22,7 +24,7 @@ public class VecBuffer {
return new VecBuffer(buffer); return new VecBuffer(buffer);
} }
protected void setInternal(ByteBuffer internal) { protected void setInternal(@Nullable ByteBuffer internal) {
this.internal = internal; this.internal = internal;
} }

View file

@ -35,7 +35,7 @@ public enum GlError {
this.glEnum = glEnum; this.glEnum = glEnum;
} }
// Great for use in your debugger's expression evaluator
public static GlError poll() { public static GlError poll() {
return errorLookup.get(GL20.glGetError()); return errorLookup.get(GL20.glGetError());
} }

View file

@ -16,14 +16,11 @@ import org.lwjgl.system.MemoryUtil;
* system. * system.
*/ */
public class GlCompat { public class GlCompat {
public final MapBufferRange mapBufferRange;
public final InstancedArrays instancedArrays; public final InstancedArrays instancedArrays;
public final BufferStorage bufferStorage; public final BufferStorage bufferStorage;
public GlCompat(GLCapabilities caps) { public GlCompat(GLCapabilities caps) {
mapBufferRange = getLatest(MapBufferRange.class, caps);
instancedArrays = getLatest(InstancedArrays.class, caps); instancedArrays = getLatest(InstancedArrays.class, caps);
bufferStorage = getLatest(BufferStorage.class, caps); bufferStorage = getLatest(BufferStorage.class, caps);
} }

View file

@ -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);
}

View file

@ -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.MappedBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer;
import com.jozufozu.flywheel.core.model.Model; 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; import com.jozufozu.flywheel.util.AttribUtil;
public class BufferedModel implements IBufferedModel { 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. // mirror it in system memory so we can write to it, and upload our model.
MappedBuffer buffer = vbo.getBuffer(0, model.size()); MappedBuffer buffer = vbo.getBuffer(0, model.size());
model.buffer(new VecBufferConsumer(buffer, model.format())); model.buffer(new VecBufferWriter(buffer));
buffer.flush(); buffer.flush();
vbo.unbind(); vbo.unbind();

View file

@ -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.MappedBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer;
import com.jozufozu.flywheel.core.model.Model; 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; import com.jozufozu.flywheel.util.AttribUtil;
public class ModelPool implements ModelAllocator { public class ModelPool implements ModelAllocator {
@ -117,7 +117,7 @@ public class ModelPool implements ModelAllocator {
private void uploadAll() { private void uploadAll() {
MappedBuffer buffer = vbo.getBuffer(0, bufferSize); MappedBuffer buffer = vbo.getBuffer(0, bufferSize);
VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); VecBufferWriter consumer = new VecBufferWriter(buffer);
for (PooledModel model : models) { for (PooledModel model : models) {
model.model.buffer(consumer); model.model.buffer(consumer);
@ -130,7 +130,7 @@ public class ModelPool implements ModelAllocator {
private void uploadPending() { private void uploadPending() {
MappedBuffer buffer = vbo.getBuffer(0, bufferSize); MappedBuffer buffer = vbo.getBuffer(0, bufferSize);
VecBufferConsumer consumer = new VecBufferConsumer(buffer, format); VecBufferWriter consumer = new VecBufferWriter(buffer);
int stride = format.getStride(); int stride = format.getStride();
for (PooledModel model : pendingUpload) { for (PooledModel model : pendingUpload) {

View file

@ -2,19 +2,15 @@ package com.jozufozu.flywheel.core.model;
import static com.jozufozu.flywheel.util.RenderMath.nb; 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.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexConsumer;
public class VecBufferConsumer implements VertexConsumer { public class VecBufferWriter implements VertexConsumer {
private final VecBuffer buffer; private final VecBuffer buffer;
private final VertexFormat format;
public VecBufferConsumer(VecBuffer buffer, VertexFormat format) { public VecBufferWriter(VecBuffer buffer) {
this.buffer = buffer; this.buffer = buffer;
this.format = format;
} }
@Override @Override

View file

@ -44,8 +44,10 @@ public class GPULightVolume extends LightVolume {
glTexture = new GlTexture(GL_TEXTURE_3D); glTexture = new GlTexture(GL_TEXTURE_3D);
GlTextureUnit oldState = GlTextureUnit.getActive();
// allocate space for the texture // allocate space for the texture
glActiveTexture(GL_TEXTURE4); textureUnit.makeActive();
glTexture.bind(); glTexture.bind();
int sizeX = box.sizeX(); 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); glTexImage3D(GL_TEXTURE_3D, 0, GL30.GL_RG8, sizeX, sizeY, sizeZ, 0, GL30.GL_RG, GL_UNSIGNED_BYTE, 0);
glTexture.unbind(); glTexture.unbind();
glActiveTexture(GL_TEXTURE0); oldState.makeActive();
} }
@Override @Override
@ -67,15 +69,15 @@ public class GPULightVolume extends LightVolume {
public void bind() { public void bind() {
// just in case something goes wrong, or we accidentally call this before this volume is properly disposed of. // 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(); textureUnit.makeActive();
glTexture.bind(); glTexture.bind();
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexture.setParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexture.setParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexture.setParameteri(GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); glTexture.setParameteri(GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexture.setParameteri(GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
uploadTexture(); uploadTexture();
} }
@ -87,7 +89,7 @@ public class GPULightVolume extends LightVolume {
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0); glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 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 sizeX = box.sizeX();
int sizeY = box.sizeY(); int sizeY = box.sizeY();
int sizeZ = box.sizeZ(); int sizeZ = box.sizeZ();
@ -146,11 +148,6 @@ public class GPULightVolume extends LightVolume {
bufferDirty = true; bufferDirty = true;
} }
@Override
protected int getStride() {
return 2;
}
@Override @Override
public ImmutableBox getVolume() { public ImmutableBox getVolume() {
return sampleVolume; return sampleVolume;

View file

@ -15,7 +15,7 @@ public class LightVolume implements ImmutableBox, LightListener {
public LightVolume(ImmutableBox sampleVolume) { public LightVolume(ImmutableBox sampleVolume) {
this.setBox(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) { protected void setBox(ImmutableBox box) {
@ -58,9 +58,9 @@ public class LightVolume implements ImmutableBox, LightListener {
if (lightData == null) return; if (lightData == null) return;
setBox(newSampleVolume); setBox(newSampleVolume);
int volume = box.volume(); int neededCapacity = box.volume() * 2;
if (volume * 2 > lightData.capacity()) { if (neededCapacity > lightData.capacity()) {
lightData = MemoryUtil.memRealloc(lightData, volume * 2); lightData = MemoryUtil.memRealloc(lightData, neededCapacity);
} }
initialize(world); initialize(world);
} }
@ -198,14 +198,7 @@ public class LightVolume implements ImmutableBox, LightListener {
} }
protected int boxPosToBufferIndex(int x, int y, int z) { protected int boxPosToBufferIndex(int x, int y, int z) {
return (x + box.sizeX() * (y + z * box.sizeY())) * getStride(); return (x + box.sizeX() * (y + z * box.sizeY())) * 2;
}
/**
* @return The stride of the texels, in bytes.
*/
protected int getStride() {
return 2;
} }
@Override @Override