Remove compat layer wrapper functions

This commit is contained in:
JozsefA 2021-04-17 18:54:57 -07:00
parent 83dc241cd2
commit 128a2c2e06
8 changed files with 132 additions and 127 deletions

View file

@ -126,6 +126,7 @@ public class SphereFilterProgram extends GlProgram {
public Vector3d center; public Vector3d center;
public float radius; public float radius;
public float feather; public float feather;
public float fade;
public float strength = 1; public float strength = 1;
public boolean hsv; public boolean hsv;
@ -146,6 +147,11 @@ public class SphereFilterProgram extends GlProgram {
return this; return this;
} }
public FilterSphere setFade(float fade) {
this.fade = fade;
return this;
}
public FilterSphere setStrength(float strength) { public FilterSphere setStrength(float strength) {
this.strength = strength; this.strength = strength;
return this; return this;
@ -168,9 +174,9 @@ public class SphereFilterProgram extends GlProgram {
(float) center.z, (float) center.z,
radius, radius,
feather, feather,
fade,
strength, strength,
hsv ? 1f : 0f, hsv ? 1f : 0f,
0 // padding, we could add more parameters here
}); });
buf.put(RenderUtil.writeMatrix(filter)); buf.put(RenderUtil.writeMatrix(filter));

View file

@ -48,15 +48,15 @@ public class GlBuffer extends GlObject {
} }
public void map(int length, Consumer<ByteBuffer> upload) { public void map(int length, Consumer<ByteBuffer> upload) {
Backend.compat.mapBuffer(bufferType, 0, length, upload); Backend.compat.mapBuffer.mapBuffer(bufferType, 0, length, upload);
} }
public void map(int offset, int length, Consumer<ByteBuffer> upload) { public void map(int offset, int length, Consumer<ByteBuffer> upload) {
Backend.compat.mapBuffer(bufferType, offset, length, upload); Backend.compat.mapBuffer.mapBuffer(bufferType, offset, length, upload);
} }
public void map(int type, int offset, int length, Consumer<ByteBuffer> upload) { public void map(int type, int offset, int length, Consumer<ByteBuffer> upload) {
Backend.compat.mapBuffer(type, offset, length, upload); Backend.compat.mapBuffer.mapBuffer(type, offset, length, upload);
} }
protected void deleteInternal(int handle) { protected void deleteInternal(int handle) {

View file

@ -6,16 +6,16 @@ import com.simibubi.create.foundation.render.backend.Backend;
public class GlVertexArray extends GlObject { public class GlVertexArray extends GlObject {
public GlVertexArray() { public GlVertexArray() {
setHandle(Backend.compat.genVertexArrays()); setHandle(Backend.compat.vao.genVertexArrays());
} }
public void bind() { public void bind() {
Backend.compat.bindVertexArray(handle()); Backend.compat.vao.bindVertexArray(handle());
} }
public void unbind() { public void unbind() {
Backend.compat.bindVertexArray(0); Backend.compat.vao.bindVertexArray(0);
} }
public void with(Consumer<GlVertexArray> action) { public void with(Consumer<GlVertexArray> action) {
bind(); bind();
@ -24,6 +24,6 @@ public class GlVertexArray extends GlObject {
} }
protected void deleteInternal(int handle) { protected void deleteInternal(int handle) {
Backend.compat.deleteVertexArrays(handle); Backend.compat.vao.deleteVertexArrays(handle);
} }
} }

View file

@ -2,7 +2,6 @@ package com.simibubi.create.foundation.render.backend.gl.versioned;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer;
import org.lwjgl.PointerBuffer; import org.lwjgl.PointerBuffer;
import org.lwjgl.opengl.GL20C; import org.lwjgl.opengl.GL20C;
@ -10,89 +9,83 @@ import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.MemoryUtil;
import com.simibubi.create.foundation.render.backend.gl.versioned.framebuffer.Blit;
import com.simibubi.create.foundation.render.backend.gl.versioned.framebuffer.Framebuffer;
import com.simibubi.create.foundation.render.backend.gl.versioned.instancing.DrawInstanced;
import com.simibubi.create.foundation.render.backend.gl.versioned.instancing.InstancedArrays;
import com.simibubi.create.foundation.render.backend.gl.versioned.instancing.VertexArrayObject;
/** /**
* An instance of this class stores information * An instance of this class stores information
* about what OpenGL features are available. * about what OpenGL features are available.
* * <p>
* Each field stores an enum variant that provides access to the * Each field stores an enum variant that provides access to the
* most appropriate version of a feature for the current system. * most appropriate version of a feature for the current system.
*/ */
public class GlCompat { public class GlCompat {
public final MapBuffer mapBuffer; public final MapBuffer mapBuffer;
public final VertexArrayObject vertexArrayObject; public final VertexArrayObject vao;
public final InstancedArrays instancedArrays; public final InstancedArrays instancedArrays;
public final DrawInstanced drawInstanced; public final DrawInstanced drawInstanced;
public final Blit blit;
public final Framebuffer fbo;
public final RGPixelFormat pixelFormat; public final RGPixelFormat pixelFormat;
public GlCompat(GLCapabilities caps) { public GlCompat(GLCapabilities caps) {
mapBuffer = getLatest(MapBuffer.class, caps); mapBuffer = getLatest(MapBuffer.class, caps);
vertexArrayObject = getLatest(VertexArrayObject.class, caps); vao = getLatest(VertexArrayObject.class, caps);
instancedArrays = getLatest(InstancedArrays.class, caps); instancedArrays = getLatest(InstancedArrays.class, caps);
drawInstanced = getLatest(DrawInstanced.class, caps); drawInstanced = getLatest(DrawInstanced.class, caps);
blit = getLatest(Blit.class, caps);
fbo = getLatest(Framebuffer.class, caps);
pixelFormat = getLatest(RGPixelFormat.class, caps); pixelFormat = getLatest(RGPixelFormat.class, caps);
} }
public void mapBuffer(int target, int offset, int length, Consumer<ByteBuffer> upload) { public boolean vertexArrayObjectsSupported() {
mapBuffer.mapBuffer(target, offset, length, upload); return vao != VertexArrayObject.UNSUPPORTED;
} }
public void vertexAttribDivisor(int index, int divisor) { public boolean instancedArraysSupported() {
instancedArrays.vertexAttribDivisor(index, divisor); return instancedArrays != InstancedArrays.UNSUPPORTED;
} }
public void drawArraysInstanced(int mode, int first, int count, int primcount) { public boolean drawInstancedSupported() {
drawInstanced.drawArraysInstanced(mode, first, count, primcount); return drawInstanced != DrawInstanced.UNSUPPORTED;
} }
public int genVertexArrays() { public boolean fbosSupported() {
return vertexArrayObject.genVertexArrays(); return fbo != Framebuffer.UNSUPPORTED;
} }
public void deleteVertexArrays(int array) { public boolean blitSupported() {
vertexArrayObject.deleteVertexArrays(array); return blit != Blit.UNSUPPORTED;
} }
public void bindVertexArray(int array) { /**
vertexArrayObject.bindVertexArray(array); * Get the most compatible version of a specific OpenGL feature by iterating over enum constants in order.
} *
* @param clazz The class of the versioning enum.
* @param caps The current system's supported features.
* @param <V> The type of the versioning enum.
* @return The first defined enum variant to return true.
*/
public static <V extends Enum<V> & GlVersioned> V getLatest(Class<V> clazz, GLCapabilities caps) {
V[] constants = clazz.getEnumConstants();
V last = constants[constants.length - 1];
if (!last.supported(caps)) {
throw new IllegalStateException("");
}
public boolean vertexArrayObjectsSupported() { return Arrays.stream(constants).filter(it -> it.supported(caps)).findFirst().get();
return vertexArrayObject != VertexArrayObject.UNSUPPORTED; }
}
public boolean instancedArraysSupported() { /**
return instancedArrays != InstancedArrays.UNSUPPORTED; * Copied from:
} * <br> https://github.com/grondag/canvas/commit/820bf754092ccaf8d0c169620c2ff575722d7d96
public boolean drawInstancedSupported() {
return drawInstanced != DrawInstanced.UNSUPPORTED;
}
/**
* Get the most compatible version of a specific OpenGL feature by iterating over enum constants in order.
*
* @param clazz The class of the versioning enum.
* @param caps The current system's supported features.
* @param <V> The type of the versioning enum.
* @return The first defined enum variant to return true.
*/
public static <V extends Enum<V> & GlVersioned> V getLatest(Class<V> clazz, GLCapabilities caps) {
V[] constants = clazz.getEnumConstants();
V last = constants[constants.length - 1];
if (!last.supported(caps)) {
throw new IllegalStateException("");
}
return Arrays.stream(constants).filter(it -> it.supported(caps)).findFirst().get();
}
/**
* Copied from:
* <br> https://github.com/grondag/canvas/commit/820bf754092ccaf8d0c169620c2ff575722d7d96
* *
* <p>Identical in function to {@link GL20C#glShaderSource(int, CharSequence)} but * <p>Identical in function to {@link GL20C#glShaderSource(int, CharSequence)} but
* passes a null pointer for string length to force the driver to rely on the null * passes a null pointer for string length to force the driver to rely on the null

View file

@ -1,26 +1,28 @@
package com.simibubi.create.foundation.render.backend.gl.versioned; package com.simibubi.create.foundation.render.backend.gl.versioned.instancing;
import org.lwjgl.opengl.ARBDrawInstanced; import org.lwjgl.opengl.ARBDrawInstanced;
import org.lwjgl.opengl.EXTDrawInstanced; import org.lwjgl.opengl.EXTDrawInstanced;
import org.lwjgl.opengl.GL31; import org.lwjgl.opengl.GL31;
import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.GLCapabilities;
public enum DrawInstanced implements GlVersioned { import com.simibubi.create.foundation.render.backend.gl.versioned.GlVersioned;
GL31_DRAW_INSTANCED {
@Override
public boolean supported(GLCapabilities caps) {
return caps.OpenGL31;
}
@Override public enum DrawInstanced implements GlVersioned {
public void drawArraysInstanced(int mode, int first, int count, int primcount) { GL31_DRAW_INSTANCED {
GL31.glDrawArraysInstanced(mode, first, count, primcount); @Override
} public boolean supported(GLCapabilities caps) {
}, return caps.OpenGL31;
ARB_DRAW_INSTANCED { }
@Override
public boolean supported(GLCapabilities caps) { @Override
return caps.GL_ARB_draw_instanced; public void drawArraysInstanced(int mode, int first, int count, int primcount) {
GL31.glDrawArraysInstanced(mode, first, count, primcount);
}
},
ARB_DRAW_INSTANCED {
@Override
public boolean supported(GLCapabilities caps) {
return caps.GL_ARB_draw_instanced;
} }
@Override @Override

View file

@ -1,25 +1,27 @@
package com.simibubi.create.foundation.render.backend.gl.versioned; package com.simibubi.create.foundation.render.backend.gl.versioned.instancing;
import org.lwjgl.opengl.ARBInstancedArrays; import org.lwjgl.opengl.ARBInstancedArrays;
import org.lwjgl.opengl.GL33; import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.GLCapabilities;
public enum InstancedArrays implements GlVersioned { import com.simibubi.create.foundation.render.backend.gl.versioned.GlVersioned;
GL33_INSTANCED_ARRAYS {
@Override
public boolean supported(GLCapabilities caps) {
return caps.OpenGL33;
}
@Override public enum InstancedArrays implements GlVersioned {
public void vertexAttribDivisor(int index, int divisor) { GL33_INSTANCED_ARRAYS {
GL33.glVertexAttribDivisor(index, divisor); @Override
} public boolean supported(GLCapabilities caps) {
}, return caps.OpenGL33;
ARB_INSTANCED_ARRAYS { }
@Override
public boolean supported(GLCapabilities caps) { @Override
return caps.GL_ARB_instanced_arrays; public void vertexAttribDivisor(int index, int divisor) {
GL33.glVertexAttribDivisor(index, divisor);
}
},
ARB_INSTANCED_ARRAYS {
@Override
public boolean supported(GLCapabilities caps) {
return caps.GL_ARB_instanced_arrays;
} }
@Override @Override

View file

@ -1,28 +1,30 @@
package com.simibubi.create.foundation.render.backend.gl.versioned; package com.simibubi.create.foundation.render.backend.gl.versioned.instancing;
import org.lwjgl.opengl.ARBVertexArrayObject; import org.lwjgl.opengl.ARBVertexArrayObject;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.GLCapabilities;
import com.simibubi.create.foundation.render.backend.gl.versioned.GlVersioned;
public enum VertexArrayObject implements GlVersioned { public enum VertexArrayObject implements GlVersioned {
GL30_VAO { GL30_VAO {
@Override @Override
public boolean supported(GLCapabilities caps) { public boolean supported(GLCapabilities caps) {
return caps.OpenGL30; return caps.OpenGL30;
} }
@Override @Override
public int genVertexArrays() { public int genVertexArrays() {
return GL30.glGenVertexArrays(); return GL30.glGenVertexArrays();
} }
@Override @Override
public void bindVertexArray(int array) { public void bindVertexArray(int array) {
GL30.glBindVertexArray(array); GL30.glBindVertexArray(array);
} }
@Override @Override
public void deleteVertexArrays(int array) { public void deleteVertexArrays(int array) {
GL30.glDeleteVertexArrays(array); GL30.glDeleteVertexArrays(array);
} }
}, },

View file

@ -83,7 +83,7 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
renderSetup(); renderSetup();
if (glInstanceCount > 0) if (glInstanceCount > 0)
Backend.compat.drawArraysInstanced(GL11.GL_QUADS, 0, vertexCount, glInstanceCount); Backend.compat.drawInstanced.drawArraysInstanced(GL11.GL_QUADS, 0, vertexCount, glInstanceCount);
}); });
} }
@ -118,8 +118,8 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
getInstanceFormat().vertexAttribPointers(staticAttributes); getInstanceFormat().vertexAttribPointers(staticAttributes);
for (int i = 0; i < getInstanceFormat().getShaderAttributeCount(); i++) { for (int i = 0; i < getInstanceFormat().getShaderAttributeCount(); i++) {
Backend.compat.vertexAttribDivisor(i + staticAttributes, 1); Backend.compat.instancedArrays.vertexAttribDivisor(i + staticAttributes, 1);
} }
} }
private void clearBufferTail() { private void clearBufferTail() {