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> programs = new Object2ObjectOpenHashMap<>(); protected final RenderPassConfiguration renderPassConfiguration; @@ -39,7 +62,9 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer { if (program == null && !this.programs.containsKey(options)) { try { - program = this.createShader("blocks/block_layer_opaque", options); + String path = "blocks/block_layer_opaque"; + if (USE_LEGACY_SHADERS) path += "_legacy"; + program = this.createShader(path, options); } catch(Exception e) { LOGGER.error("There was an error creating a chunk program. Terrain will not render until this is fixed.", e); } @@ -67,7 +92,9 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer { for (var attr : options.pass().vertexType().getVertexFormat().getAttributes()) { builder.bindAttribute(attr.getName(), i++); } - builder.bindFragmentData("fragColor", ChunkShaderBindingPoints.FRAG_COLOR); + if (!USE_LEGACY_SHADERS) { + builder.bindFragmentData("fragColor", ChunkShaderBindingPoints.FRAG_COLOR); + } return builder.link((shader) -> new DefaultChunkShaderInterface(shader, options)); } finally { loadedShaders.forEach(GlShader::delete); diff --git a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.fsh b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.fsh new file mode 100644 index 000000000..f23bb2907 --- /dev/null +++ b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.fsh @@ -0,0 +1,84 @@ +#version 120 +#import + +#import + +varying vec4 v_Color; +varying vec2 v_TexCoord; + +varying float v_ChunkAgeMs; + +varying float v_MaterialMipBias; +#ifdef USE_FRAGMENT_DISCARD +varying float v_MaterialAlphaCutoff; +#endif + +#if defined(USE_FOG_POSTMODERN) +varying float v_SphericalFragDistance; +varying float v_CylindricalFragDistance; +#elif defined(USE_FOG) +varying float v_FragDistance; +#endif + +uniform sampler2D u_BlockTex; // The block texture + +uniform vec4 u_FogColor; // The color of the shader fog + +#ifdef USE_FOG_SMOOTH +uniform float u_FogStart; // The starting position of the shader fog +uniform float u_FogEnd; // The ending position of the shader fog +#endif + +#ifdef USE_FOG_POSTMODERN +uniform float u_RenderDistFogStart; +uniform float u_RenderDistFogEnd; +uniform float u_EnvFogStart; +uniform float u_EnvFogEnd; +#endif + +#ifdef USE_FOG_EXP2 +uniform float u_FogDensity; // The density of the shader fog +#endif + +void main() { + vec4 diffuseColor = texture2D(u_BlockTex, v_TexCoord, v_MaterialMipBias); + +#ifdef USE_FRAGMENT_DISCARD + if (diffuseColor.a < v_MaterialAlphaCutoff) { + discard; + } +#endif + + vec4 m_color = v_Color; + +#ifdef USE_VANILLA_COLOR_FORMAT + // Apply per-vertex color. AO shade is applied ahead of time on the CPU. + diffuseColor *= m_color; +#else + // Apply per-vertex color + diffuseColor.rgb *= m_color.rgb; + + // Apply ambient occlusion "shade" + diffuseColor.rgb *= m_color.a; +#endif + +#ifdef USE_FOG +#if defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0 + // Make chunk fade in over a short duration + diffuseColor = vec4(mix(u_FogColor.rgb, diffuseColor.rgb, (clamp(v_ChunkAgeMs, 0, CHUNK_FADE_IN_DURATION_MS) / CHUNK_FADE_IN_DURATION_MS)), diffuseColor.a); +#endif + +#ifdef USE_FOG_POSTMODERN + float fogValue = max(_linearFogValue(v_CylindricalFragDistance, u_RenderDistFogStart, u_RenderDistFogEnd), + _linearFogValue(v_SphericalFragDistance, u_EnvFogStart, u_EnvFogEnd)); + + gl_FragColor = vec4(mix(diffuseColor.rgb, u_FogColor.rgb, fogValue * u_FogColor.a), diffuseColor.a); +#elif defined(USE_FOG_EXP2) + gl_FragColor = _exp2Fog(diffuseColor, v_FragDistance, u_FogColor, u_FogDensity); +#elif defined(USE_FOG_SMOOTH) + gl_FragColor = _linearFog(diffuseColor, v_FragDistance, u_FogColor, u_FogStart, u_FogEnd); +#endif +#else + gl_FragColor = diffuseColor; +#endif +} diff --git a/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.vsh b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.vsh new file mode 100644 index 000000000..25468da5b --- /dev/null +++ b/common/src/main/resources/assets/sodium/shaders/blocks/block_layer_opaque_legacy.vsh @@ -0,0 +1,71 @@ +#version 120 +#import + +#define in attribute +#import +#import +#import +#import + +varying vec4 v_Color; +varying vec2 v_TexCoord; + +varying float v_ChunkAgeMs; + +varying float v_MaterialMipBias; +#ifdef USE_FRAGMENT_DISCARD +varying float v_MaterialAlphaCutoff; +#endif + +#if defined(USE_FOG_POSTMODERN) +varying float v_SphericalFragDistance; +varying float v_CylindricalFragDistance; +#elif defined(USE_FOG) +varying float v_FragDistance; +#endif + +uniform int u_FogShape; + +uniform vec3 u_RegionOffset; + +#ifndef CELERITAS_NO_LIGHTMAP +uniform sampler2D u_LightTex; // The light map texture sampler + +vec4 _sample_lightmap(sampler2D lightMap, ivec2 uv) { + return texture2D(lightMap, clamp(uv / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0))); +} +#endif + +uniform float celeritas_ChunkAges[REGION_SIZE]; + +void main() { + _vert_init(); + + // Transform the chunk-local vertex position into world model space + vec3 translation = u_RegionOffset + _get_draw_translation(_draw_id); + vec3 position = _vert_position + translation; + +#if defined(USE_FOG_POSTMODERN) + v_SphericalFragDistance = getFragDistance(FOG_SHAPE_SPHERICAL, position); + v_CylindricalFragDistance = getFragDistance(FOG_SHAPE_CYLINDRICAL, position); +#elif defined(USE_FOG) + v_FragDistance = getFragDistance(u_FogShape, position); +#endif + + // Transform the vertex position into model-view-projection space + gl_Position = u_ProjectionMatrix * u_ModelViewMatrix * vec4(position, 1.0); + + // Add the light color to the vertex color, and pass the texture coordinates to the fragment shader +#ifdef CELERITAS_NO_LIGHTMAP + v_Color = _vert_color; +#else + v_Color = _vert_color * _sample_lightmap(u_LightTex, _vert_tex_light_coord); +#endif + v_TexCoord = _vert_tex_diffuse_coord; + + v_MaterialMipBias = _material_mip_bias(_material_params); +#ifdef USE_FRAGMENT_DISCARD + v_MaterialAlphaCutoff = _material_alpha_cutoff(_material_params); +#endif + v_ChunkAgeMs = celeritas_ChunkAges[_draw_id]; +} diff --git a/common/src/main/resources/assets/sodium/shaders/include/fog.glsl b/common/src/main/resources/assets/sodium/shaders/include/fog.glsl index 550746b5c..9bcddeca2 100644 --- a/common/src/main/resources/assets/sodium/shaders/include/fog.glsl +++ b/common/src/main/resources/assets/sodium/shaders/include/fog.glsl @@ -43,9 +43,11 @@ vec4 _exp2Fog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogDensit float getFragDistance(int fogShape, vec3 position) { // Use the maximum of the horizontal and vertical distance to get cylindrical fog if fog shape is cylindrical - switch (fogShape) { - case FOG_SHAPE_SPHERICAL: return length(position); - case FOG_SHAPE_CYLINDRICAL: return max(length(position.xz), abs(position.y)); - default: return length(position); // This shouldn't be possible to get, but return a sane value just in case + if (fogShape == FOG_SHAPE_SPHERICAL) { + return length(position); + } else if (fogShape == FOG_SHAPE_CYLINDRICAL) { + return max(length(position.xz), abs(position.y)); + } else { + return length(position); // This shouldn't be possible to get, but return a sane value just in case } } \ No newline at end of file diff --git a/common/src/main/resources/assets/sodium/shaders/include/legacy.glsl b/common/src/main/resources/assets/sodium/shaders/include/legacy.glsl new file mode 100644 index 000000000..f8eb37c26 --- /dev/null +++ b/common/src/main/resources/assets/sodium/shaders/include/legacy.glsl @@ -0,0 +1,3 @@ +#extension GL_EXT_gpu_shader4 : require +#define LEGACY +#define uint unsigned int