mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2025-01-13 15:56:07 +01:00
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:
parent
2394b02e3d
commit
c0d5c32766
15 changed files with 42 additions and 150 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue