diff --git a/buildSrc/src/main/java/org/embeddedt/embeddium/gradle/build/conventions/LWJGLHelper.java b/buildSrc/src/main/java/org/embeddedt/embeddium/gradle/build/conventions/LWJGLHelper.java index b079e2cd9..d21b79348 100644 --- a/buildSrc/src/main/java/org/embeddedt/embeddium/gradle/build/conventions/LWJGLHelper.java +++ b/buildSrc/src/main/java/org/embeddedt/embeddium/gradle/build/conventions/LWJGLHelper.java @@ -31,6 +31,7 @@ public class LWJGLHelper { deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION); deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "linux"); deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "windows"); + deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "macos-arm64"); } } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/array/GlVertexArray.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/array/GlVertexArray.java index 0cc98fcbd..4dd5128b6 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/array/GlVertexArray.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/array/GlVertexArray.java @@ -1,7 +1,7 @@ package org.embeddedt.embeddium.impl.gl.array; import org.embeddedt.embeddium.impl.gl.GlObject; -import org.lwjgl.opengl.GL30C; +import org.embeddedt.embeddium.impl.gl.util.VAOUtil; /** * Provides Vertex Array functionality on supported platforms. @@ -10,11 +10,11 @@ public class GlVertexArray extends GlObject { public static final int NULL_ARRAY_ID = 0; public GlVertexArray() { - this.setHandle(GL30C.glGenVertexArrays()); + this.setHandle(VAOUtil.glGenVertexArrays()); } @Override protected void destroyInternal() { - GL30C.glDeleteVertexArrays(this.handle()); + VAOUtil.glDeleteVertexArrays(this.handle()); } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/buffer/GlBufferTarget.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/buffer/GlBufferTarget.java index 62735ca93..771487c2a 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/buffer/GlBufferTarget.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/buffer/GlBufferTarget.java @@ -1,6 +1,7 @@ package org.embeddedt.embeddium.impl.gl.buffer; import org.lwjgl.opengl.GL20C; +import org.lwjgl.opengl.GL21C; import org.lwjgl.opengl.GL31C; import org.lwjgl.opengl.GL43C; @@ -9,7 +10,9 @@ public enum GlBufferTarget { ELEMENT_BUFFER(GL20C.GL_ELEMENT_ARRAY_BUFFER, GL20C.GL_ELEMENT_ARRAY_BUFFER_BINDING), COPY_READ_BUFFER(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_READ_BUFFER), COPY_WRITE_BUFFER(GL31C.GL_COPY_WRITE_BUFFER, GL31C.GL_COPY_WRITE_BUFFER), - DRAW_INDIRECT_BUFFER(GL43C.GL_DRAW_INDIRECT_BUFFER, GL43C.GL_DRAW_INDIRECT_BUFFER_BINDING); + DRAW_INDIRECT_BUFFER(GL43C.GL_DRAW_INDIRECT_BUFFER, GL43C.GL_DRAW_INDIRECT_BUFFER_BINDING), + PIXEL_PACK_BUFFER(GL21C.GL_PIXEL_PACK_BUFFER, GL21C.GL_PIXEL_PACK_BUFFER_BINDING), + PIXEL_UNPACK_BUFFER(GL21C.GL_PIXEL_UNPACK_BUFFER, GL21C.GL_PIXEL_UNPACK_BUFFER_BINDING); public static final GlBufferTarget[] VALUES = GlBufferTarget.values(); public static final int COUNT = VALUES.length; diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/device/GLRenderDevice.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/device/GLRenderDevice.java index f08e5d5f7..f0970a029 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/device/GLRenderDevice.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/device/GLRenderDevice.java @@ -7,8 +7,11 @@ import org.embeddedt.embeddium.impl.gl.state.GlStateTracker; import org.embeddedt.embeddium.impl.gl.sync.GlFence; import org.embeddedt.embeddium.impl.gl.tessellation.*; import org.embeddedt.embeddium.impl.gl.util.EnumBitField; +import org.embeddedt.embeddium.impl.gl.util.VAOUtil; import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.*; +import org.lwjgl.system.libc.LibCString; + import java.nio.ByteBuffer; public class GLRenderDevice implements RenderDevice { @@ -86,7 +89,7 @@ public class GLRenderDevice implements RenderDevice { @Override public void bindVertexArray(GlVertexArray array) { if (this.stateTracker.makeVertexArrayActive(array)) { - GL30C.glBindVertexArray(array.handle()); + VAOUtil.glBindVertexArray(array.handle()); } } @@ -108,10 +111,29 @@ public class GLRenderDevice implements RenderDevice { @Override public void copyBufferSubData(GlBuffer src, GlBuffer dst, long readOffset, long writeOffset, long bytes) { - this.bindBuffer(GlBufferTarget.COPY_READ_BUFFER, src); - this.bindBuffer(GlBufferTarget.COPY_WRITE_BUFFER, dst); + if(GL.getCapabilities().OpenGL31 || GL.getCapabilities().GL_ARB_copy_buffer) { + this.bindBuffer(GlBufferTarget.COPY_READ_BUFFER, src); + this.bindBuffer(GlBufferTarget.COPY_WRITE_BUFFER, dst); + GL31C.glCopyBufferSubData(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_WRITE_BUFFER, readOffset, writeOffset, bytes); + return; + } - GL31C.glCopyBufferSubData(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_WRITE_BUFFER, readOffset, writeOffset, bytes); + this.bindBuffer(GlBufferTarget.PIXEL_PACK_BUFFER, src); + this.bindBuffer(GlBufferTarget.PIXEL_UNPACK_BUFFER, dst); + + ByteBuffer a = GL15.glMapBuffer(GlBufferTarget.PIXEL_PACK_BUFFER.getTargetParameter(), GL15.GL_READ_ONLY); + ByteBuffer b = GL15.glMapBuffer(GlBufferTarget.PIXEL_UNPACK_BUFFER.getTargetParameter(), GL15.GL_WRITE_ONLY); + + LibCString.memcpy( + b.position(Math.toIntExact(writeOffset)).limit(Math.toIntExact(writeOffset + bytes)), + a.position(Math.toIntExact(readOffset)).limit(Math.toIntExact(readOffset + bytes)) + ); + + GL15.glUnmapBuffer(GlBufferTarget.PIXEL_PACK_BUFFER.getTargetParameter()); + GL15.glUnmapBuffer(GlBufferTarget.PIXEL_UNPACK_BUFFER.getTargetParameter()); + + this.bindBuffer(GlBufferTarget.PIXEL_PACK_BUFFER, null); + this.bindBuffer(GlBufferTarget.PIXEL_UNPACK_BUFFER, null); } @Override @@ -124,7 +146,7 @@ public class GLRenderDevice implements RenderDevice { @Override public void unbindVertexArray() { if (this.stateTracker.makeVertexArrayActive(null)) { - GL30C.glBindVertexArray(GlVertexArray.NULL_ARRAY_ID); + VAOUtil.glBindVertexArray(GlVertexArray.NULL_ARRAY_ID); } } @@ -201,7 +223,28 @@ public class GLRenderDevice implements RenderDevice { this.bindBuffer(GlBufferTarget.ARRAY_BUFFER, buffer); - ByteBuffer buf = GL32C.glMapBufferRange(GlBufferTarget.ARRAY_BUFFER.getTargetParameter(), offset, length, flags.getBitField()); + ByteBuffer buf; + if (GL.getCapabilities().OpenGL30 || GL.getCapabilities().GL_ARB_map_buffer_range) { + buf = GL32C.glMapBufferRange( + GlBufferTarget.ARRAY_BUFFER.getTargetParameter(), + offset, length, flags.getBitField() + ); + } else { + int access; + if (flags.contains(GlBufferMapFlags.WRITE) && flags.contains(GlBufferMapFlags.READ)) { + access = GL15C.GL_READ_WRITE; + } else if (flags.contains(GlBufferMapFlags.WRITE)) { + access = GL15C.GL_WRITE_ONLY; + } else { + access = GL15C.GL_READ_ONLY; + } + + buf = GL15C.glMapBuffer(GlBufferTarget.ARRAY_BUFFER.getTargetParameter(), access, null); + + buf = buf.position(Math.toIntExact(offset)) + .limit(Math.toIntExact(offset) + Math.toIntExact(length)) + .slice(); + } if (buf == null) { throw new RuntimeException("Failed to map buffer"); diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java index 7d467f0f8..242681bca 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/tessellation/GlAbstractTessellation.java @@ -2,8 +2,11 @@ package org.embeddedt.embeddium.impl.gl.tessellation; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeBinding; import org.embeddedt.embeddium.impl.gl.device.CommandList; +import org.lwjgl.opengl.EXTGPUShader4; +import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL20C; import org.lwjgl.opengl.GL30C; +import org.lwjgl.opengl.GLCapabilities; public abstract class GlAbstractTessellation implements GlTessellation { protected final TessellationBinding[] bindings; @@ -17,14 +20,26 @@ public abstract class GlAbstractTessellation implements GlTessellation { commandList.bindBuffer(binding.target(), binding.buffer()); for (GlVertexAttributeBinding attrib : binding.attributeBindings()) { + int index = attrib.getIndex(); + int size = attrib.getCount(); + int type = attrib.getFormat().typeId(); + int stride = attrib.getStride(); + long pointer = attrib.getPointer(); + if (attrib.isIntType()) { - GL30C.glVertexAttribIPointer(attrib.getIndex(), attrib.getCount(), attrib.getFormat().typeId(), - attrib.getStride(), attrib.getPointer()); + GLCapabilities caps = GL.getCapabilities(); + + if (caps.OpenGL30) { + GL30C.glVertexAttribIPointer(index, size, type, stride, pointer); + } else if (caps.GL_EXT_gpu_shader4) { + EXTGPUShader4.glVertexAttribIPointerEXT(index, size, type, stride, pointer); + } else { + GL20C.glVertexAttribPointer(index, size, type, false, stride, pointer); + } } else { - GL20C.glVertexAttribPointer(attrib.getIndex(), attrib.getCount(), attrib.getFormat().typeId(), attrib.isNormalized(), - attrib.getStride(), attrib.getPointer()); + GL20C.glVertexAttribPointer(index, size, type, attrib.isNormalized(), stride, pointer); } - GL20C.glEnableVertexAttribArray(attrib.getIndex()); + GL20C.glEnableVertexAttribArray(index); } } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/APPLEVertexArrayObject.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/APPLEVertexArrayObject.java new file mode 100644 index 000000000..d3412c431 --- /dev/null +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/APPLEVertexArrayObject.java @@ -0,0 +1,82 @@ +package org.embeddedt.embeddium.impl.gl.util; + +import org.lwjgl.opengl.GL; +import org.lwjgl.system.JNI; +import org.lwjgl.system.MemoryStack; +import org.lwjgl.system.NativeType; + +import java.nio.IntBuffer; + +import static org.lwjgl.system.MemoryStack.stackGet; +import static org.lwjgl.system.MemoryUtil.memAddress; + +/** + * Native bindings to the APPLE_vertex_array_object extension. + *
This extension introduces named vertex array objects which encapsulate vertex array state on the client side. + * The main purpose of these objects is to keep pointers to static vertex data and provide a name for different sets of static vertex data.
+ * + *By extending vertex array range functionality this extension allows multiple vertex array ranges to exist at one time, + * including their complete sets of state, in manner analogous to texture objects.
+ * + *GenVertexArraysAPPLE creates a list of n number of vertex array object names. After creating a name, + * BindVertexArrayAPPLE associates the name with a vertex array object and selects this vertex array and its associated state as current. + * To get back to the default vertex array and its associated state the client should bind to vertex array named 0.
+ * + *Once a client is done using a vertex array object it can be deleted with DeleteVertexArraysAPPLE. + * The client is responsible for allocating and deallocating the memory used by the vertex array data, + * while the DeleteVertexArraysAPPLE command deletes vertex array object names and associated state only.
+ */ +public class APPLEVertexArrayObject { + private static final long bindVertexArray = GL.getFunctionProvider().getFunctionAddress("glBindVertexArrayAPPLE"); + private static final long deleteVertexArrays = GL.getFunctionProvider().getFunctionAddress("glDeleteVertexArraysAPPLE"); + private static final long genVertexArrays = GL.getFunctionProvider().getFunctionAddress("glGenVertexArraysAPPLE"); + private static final long isVertexArray = GL.getFunctionProvider().getFunctionAddress("glIsVertexArrayAPPLE"); + + public static final boolean supported = bindVertexArray != 0; + + public static void glBindVertexArrayAPPLE(@NativeType("GLuint") int array) { + JNI.callV(array, bindVertexArray); + } + + public static void nglDeleteVertexArraysAPPLE(int n, long arrays) { + JNI.callPV(n, arrays, deleteVertexArrays); + } + + public static void glDeleteVertexArraysAPPLE(@NativeType("GLuint const *") IntBuffer arrays) { + nglDeleteVertexArraysAPPLE(arrays.remaining(), memAddress(arrays)); + } + + public static void glDeleteVertexArraysAPPLE(@NativeType("GLuint const *") int array) { + MemoryStack stack = stackGet(); int stackPointer = stack.getPointer(); + try { + IntBuffer arrays = stack.callocInt(1); + arrays.put(0, array); + nglDeleteVertexArraysAPPLE(1, memAddress(arrays)); + } finally { + stack.setPointer(stackPointer); + } + } + + public static void nglGenVertexArraysAPPLE(int n, long arrays) { + JNI.callPV(n, arrays, genVertexArrays); + } + + public static void glGenVertexArraysAPPLE(@NativeType("GLuint *") IntBuffer arrays) { + nglGenVertexArraysAPPLE(arrays.remaining(), memAddress(arrays)); + } + + public static int glGenVertexArraysAPPLE() { + MemoryStack stack = stackGet(); int stackPointer = stack.getPointer(); + try { + IntBuffer arrays = stack.callocInt(1); + nglGenVertexArraysAPPLE(1, memAddress(arrays)); + return arrays.get(0); + } finally { + stack.setPointer(stackPointer); + } + } + + public static boolean glIsVertexArrayAPPLE(@NativeType("GLuint") int array) { + return JNI.callZ(array, isVertexArray); + } +} diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/VAOUtil.java b/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/VAOUtil.java new file mode 100644 index 000000000..5cc8ef509 --- /dev/null +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gl/util/VAOUtil.java @@ -0,0 +1,115 @@ +package org.embeddedt.embeddium.impl.gl.util; + +import org.lwjgl.opengl.ARBVertexArrayObject; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL30C; +import org.lwjgl.system.NativeType; + +import java.nio.IntBuffer; + +/** + * Provides an abstraction over various VAO implementations. + */ +public class VAOUtil { + public static final boolean coreSupported = GL.getCapabilities().OpenGL32; + public static final boolean arbSupported = GL.getCapabilities().GL_ARB_vertex_array_object; + public static final boolean appleSupported = APPLEVertexArrayObject.supported; + + public static final boolean supported = coreSupported || arbSupported || appleSupported; + + public static void glBindVertexArray(@NativeType("GLuint") int array) { + if (coreSupported) { + GL30C.glBindVertexArray(array); + } else if (arbSupported) { + ARBVertexArrayObject.glBindVertexArray(array); + } else if (appleSupported) { + APPLEVertexArrayObject.glBindVertexArrayAPPLE(array); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static void nglDeleteVertexArrays(int n, long arrays) { + if (coreSupported) { + GL30C.nglDeleteVertexArrays(n, arrays); + } else if (arbSupported) { + ARBVertexArrayObject.nglDeleteVertexArrays(n, arrays); + } else if (appleSupported) { + APPLEVertexArrayObject.nglDeleteVertexArraysAPPLE(n, arrays); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static void glDeleteVertexArrays(@NativeType("GLuint const *") IntBuffer arrays) { + if (coreSupported) { + GL30C.glDeleteVertexArrays(arrays); + } else if (arbSupported) { + ARBVertexArrayObject.glDeleteVertexArrays(arrays); + } else if (appleSupported) { + APPLEVertexArrayObject.glDeleteVertexArraysAPPLE(arrays); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static void glDeleteVertexArrays(@NativeType("GLuint const *") int array) { + if (coreSupported) { + GL30C.glDeleteVertexArrays(array); + } else if (arbSupported) { + ARBVertexArrayObject.glDeleteVertexArrays(array); + } else if (appleSupported) { + APPLEVertexArrayObject.glDeleteVertexArraysAPPLE(array); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static void nglGenVertexArrays(int n, long arrays) { + if (coreSupported) { + GL30C.nglGenVertexArrays(n, arrays); + } else if (arbSupported) { + ARBVertexArrayObject.nglGenVertexArrays(n, arrays); + } else if (appleSupported) { + APPLEVertexArrayObject.nglGenVertexArraysAPPLE(n, arrays); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static void glGenVertexArrays(@NativeType("GLuint *") IntBuffer arrays) { + if (coreSupported) { + GL30C.glGenVertexArrays(arrays); + } else if (arbSupported) { + ARBVertexArrayObject.glGenVertexArrays(arrays); + } else if (appleSupported) { + APPLEVertexArrayObject.glGenVertexArraysAPPLE(arrays); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static int glGenVertexArrays() { + if (coreSupported) { + return GL30C.glGenVertexArrays(); + } else if (arbSupported) { + return ARBVertexArrayObject.glGenVertexArrays(); + } else if (appleSupported) { + return APPLEVertexArrayObject.glGenVertexArraysAPPLE(); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } + + public static boolean glIsVertexArray(@NativeType("GLuint") int array) { + if (coreSupported) { + return GL30C.glIsVertexArray(array); + } else if (arbSupported) { + return ARBVertexArrayObject.glIsVertexArray(array); + } else if (appleSupported) { + return APPLEVertexArrayObject.glIsVertexArrayAPPLE(array); + } else { + throw new UnsupportedOperationException("VAO not supported"); + } + } +} diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java index c45836036..727734b3c 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java @@ -12,6 +12,9 @@ import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType; import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; import org.jetbrains.annotations.Nullable; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL20; import java.util.ArrayList; import java.util.List; @@ -21,6 +24,26 @@ import java.util.Objects; public abstract class ShaderChunkRenderer implements ChunkRenderer { private static final Logger LOGGER = LogManager.getLogger(ShaderChunkRenderer.class); + private static final boolean USE_LEGACY_SHADERS; + + static { + boolean useLegacyShaders; + String glslVersion = GL11.glGetString(GL20.GL_SHADING_LANGUAGE_VERSION); + try { + glslVersion = glslVersion.replace(".", ""); + int versionNumber = Integer.parseInt(glslVersion); + useLegacyShaders = versionNumber < 130; + if (useLegacyShaders && !GL.getCapabilities().GL_EXT_gpu_shader4) { + throw new IllegalStateException("GLSL version " + glslVersion + " is too old and EXT_gpu_shader4 is not supported"); + } + } catch (NumberFormatException e) { + LOGGER.error("Failed to parse GLSL version {}", glslVersion, e); + useLegacyShaders = false; + } + + USE_LEGACY_SHADERS = useLegacyShaders; + } + private final Map