forked from Creators-of-Create/Create
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
289 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/DrawInstanced.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.simibubi.create.foundation.render.backend.gl.versioned; | ||
|
||
import org.lwjgl.opengl.*; | ||
|
||
public enum DrawInstanced implements GlVersioned { | ||
GL31_DRAW_INSTANCED { | ||
@Override | ||
public boolean supported(GLCapabilities caps) { | ||
return caps.OpenGL31; | ||
} | ||
|
||
@Override | ||
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 | ||
public void drawArraysInstanced(int mode, int first, int count, int primcount) { | ||
ARBDrawInstanced.glDrawArraysInstancedARB(mode, first, count, primcount); | ||
} | ||
}, | ||
EXT_DRAW_INSTANCED { | ||
@Override | ||
public boolean supported(GLCapabilities caps) { | ||
return caps.GL_EXT_draw_instanced; | ||
} | ||
|
||
@Override | ||
public void drawArraysInstanced(int mode, int first, int count, int primcount) { | ||
EXTDrawInstanced.glDrawArraysInstancedEXT(mode, first, count, primcount); | ||
} | ||
}, | ||
UNSUPPORTED { | ||
@Override | ||
public boolean supported(GLCapabilities caps) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void drawArraysInstanced(int mode, int first, int count, int primcount) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
; | ||
|
||
public abstract void drawArraysInstanced(int mode, int first, int count, int primcount); | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.simibubi.create.foundation.render.backend.gl.versioned; | ||
|
||
import org.lwjgl.opengl.GLCapabilities; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* An instance of this class stores information | ||
* about what OpenGL features are available. | ||
* | ||
* Each field stores an enum variant that provides access to the | ||
* most appropriate version of a feature for the current system. | ||
*/ | ||
public class GlFunctions { | ||
public final MapBuffer mapBuffer; | ||
|
||
public final VertexArrayObject vertexArrayObject; | ||
public final InstancedArrays instancedArrays; | ||
public final DrawInstanced drawInstanced; | ||
|
||
public GlFunctions(GLCapabilities caps) { | ||
mapBuffer = getLatest(MapBuffer.class, caps); | ||
|
||
vertexArrayObject = getLatest(VertexArrayObject.class, caps); | ||
instancedArrays = getLatest(InstancedArrays.class, caps); | ||
drawInstanced = getLatest(DrawInstanced.class, caps); | ||
} | ||
|
||
public void mapBuffer(int target, int offset, int length, Consumer<ByteBuffer> upload) { | ||
mapBuffer.mapBuffer(target, offset, length, upload); | ||
} | ||
|
||
public void vertexAttribDivisor(int index, int divisor) { | ||
instancedArrays.vertexAttribDivisor(index, divisor); | ||
} | ||
|
||
public void drawArraysInstanced(int mode, int first, int count, int primcount) { | ||
drawInstanced.drawArraysInstanced(mode, first, count, primcount); | ||
} | ||
|
||
public int genVertexArrays() { | ||
return vertexArrayObject.genVertexArrays(); | ||
} | ||
|
||
public void deleteVertexArrays(int array) { | ||
vertexArrayObject.deleteVertexArrays(array); | ||
} | ||
|
||
public void bindVertexArray(int array) { | ||
vertexArrayObject.bindVertexArray(array); | ||
} | ||
|
||
public boolean vertexArrayObjectsSupported() { | ||
return vertexArrayObject != VertexArrayObject.UNSUPPORTED; | ||
} | ||
|
||
public boolean instancedArraysSupported() { | ||
return instancedArrays != InstancedArrays.UNSUPPORTED; | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...main/java/com/simibubi/create/foundation/render/backend/gl/versioned/InstancedArrays.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.simibubi.create.foundation.render.backend.gl.versioned; | ||
|
||
import org.lwjgl.opengl.*; | ||
|
||
public enum InstancedArrays implements GlVersioned { | ||
GL33_INSTANCED_ARRAYS { | ||
@Override | ||
public boolean supported(GLCapabilities caps) { | ||
return caps.OpenGL33; | ||
} | ||
|
||
@Override | ||
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 | ||
public void vertexAttribDivisor(int index, int divisor) { | ||
ARBInstancedArrays.glVertexAttribDivisorARB(index, divisor); | ||
} | ||
}, | ||
UNSUPPORTED { | ||
@Override | ||
public boolean supported(GLCapabilities caps) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void vertexAttribDivisor(int index, int divisor) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
; | ||
|
||
public abstract void vertexAttribDivisor(int index, int divisor); | ||
} |
Oops, something went wrong.