lower opengl requirement to 2.1 when higher versions are unsupported #21

Closed
rdh wants to merge 10 commits from rdh/celeritas:stonecutter into stonecutter
pull from: rdh/celeritas:stonecutter
10 changed files with 248 additions and 9 deletions
Showing only changes of commit 1ed044d6db - Show all commits

No files matched your search

@@ -3,6 +3,7 @@ package org.taumc.celeritas.impl.render.terrain.compile.task;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap; import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher; import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.world.WorldRegion; import net.minecraft.world.WorldRegion;
import net.minecraft.world.chunk.WorldChunk; import net.minecraft.world.chunk.WorldChunk;
@@ -31,6 +31,7 @@ public class LWJGLHelper {
deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION); 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-" + "linux");
deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "windows"); deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "windows");
deps.add(configurationName, "org.lwjgl:" + component + ":" + LWJGL3_VERSION + ":natives-" + "macos-arm64");
} }
} }
} }
@@ -1,6 +1,7 @@
package org.embeddedt.embeddium.impl.gl.buffer; package org.embeddedt.embeddium.impl.gl.buffer;
import org.lwjgl.opengl.GL20C; import org.lwjgl.opengl.GL20C;
import org.lwjgl.opengl.GL21C;
import org.lwjgl.opengl.GL31C; import org.lwjgl.opengl.GL31C;
import org.lwjgl.opengl.GL43C; 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), 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_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), 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 GlBufferTarget[] VALUES = GlBufferTarget.values();
public static final int COUNT = VALUES.length; public static final int COUNT = VALUES.length;
@@ -108,10 +108,27 @@ public class GLRenderDevice implements RenderDevice {
@Override @Override
public void copyBufferSubData(GlBuffer src, GlBuffer dst, long readOffset, long writeOffset, long bytes) { public void copyBufferSubData(GlBuffer src, GlBuffer dst, long readOffset, long writeOffset, long bytes) {
if (GL.getCapabilities().OpenGL31 || GL.getCapabilities().GL_ARB_copy_buffer) {
this.bindBuffer(GlBufferTarget.COPY_READ_BUFFER, src); this.bindBuffer(GlBufferTarget.COPY_READ_BUFFER, src);
this.bindBuffer(GlBufferTarget.COPY_WRITE_BUFFER, dst); this.bindBuffer(GlBufferTarget.COPY_WRITE_BUFFER, dst);
GL31C.glCopyBufferSubData(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_WRITE_BUFFER, readOffset, writeOffset, bytes); GL31C.glCopyBufferSubData(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_WRITE_BUFFER, readOffset, writeOffset, bytes);
return;
}
this.bindBuffer(GlBufferTarget.ARRAY_BUFFER, src);
ByteBuffer srcData = GL15C.glMapBuffer(GL15C.GL_ARRAY_BUFFER, GL15C.GL_READ_ONLY, bytes, null);
srcData.position((int) readOffset);
GL15C.glUnmapBuffer(GL15C.GL_ARRAY_BUFFER);
this.bindBuffer(GlBufferTarget.ARRAY_BUFFER, dst);
ByteBuffer dstData = GL15C.glMapBuffer(GL15C.GL_ARRAY_BUFFER, GL15C.GL_WRITE_ONLY, null);
dstData.position((int) writeOffset);
ByteBuffer srcSlice = srcData.slice(0, (int) bytes);
dstData.put(srcSlice);
GL15C.glUnmapBuffer(GL15C.GL_ARRAY_BUFFER);
} }
@Override @Override
@@ -201,7 +218,28 @@ public class GLRenderDevice implements RenderDevice {
this.bindBuffer(GlBufferTarget.ARRAY_BUFFER, buffer); 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) { if (buf == null) {
throw new RuntimeException("Failed to map buffer"); throw new RuntimeException("Failed to map buffer");
@@ -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.chunk.vertex.format.ChunkVertexType;
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
import org.jetbrains.annotations.Nullable; 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.ArrayList;
import java.util.List; import java.util.List;
@@ -21,6 +24,26 @@ import java.util.Objects;
public abstract class ShaderChunkRenderer implements ChunkRenderer { public abstract class ShaderChunkRenderer implements ChunkRenderer {
private static final Logger LOGGER = LogManager.getLogger(ShaderChunkRenderer.class); 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<ChunkShaderOptions, @Nullable GlProgram<ChunkShaderInterface>> programs = new Object2ObjectOpenHashMap<>(); private final Map<ChunkShaderOptions, @Nullable GlProgram<ChunkShaderInterface>> programs = new Object2ObjectOpenHashMap<>();
protected final RenderPassConfiguration<?> renderPassConfiguration; protected final RenderPassConfiguration<?> renderPassConfiguration;
@@ -39,7 +62,9 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer {
if (program == null && !this.programs.containsKey(options)) { if (program == null && !this.programs.containsKey(options)) {
try { 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) { } catch(Exception e) {
LOGGER.error("There was an error creating a chunk program. Terrain will not render until this is fixed.", 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()) { for (var attr : options.pass().vertexType().getVertexFormat().getAttributes()) {
builder.bindAttribute(attr.getName(), i++); builder.bindAttribute(attr.getName(), i++);
} }
if (!USE_LEGACY_SHADERS) {
builder.bindFragmentData("fragColor", ChunkShaderBindingPoints.FRAG_COLOR); builder.bindFragmentData("fragColor", ChunkShaderBindingPoints.FRAG_COLOR);
}
return builder.link((shader) -> new DefaultChunkShaderInterface(shader, options)); return builder.link((shader) -> new DefaultChunkShaderInterface(shader, options));
} finally { } finally {
loadedShaders.forEach(GlShader::delete); loadedShaders.forEach(GlShader::delete);
@@ -0,0 +1,84 @@
#version 120
#import <sodium:include/legacy.glsl>
#import <sodium:include/fog.glsl>
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
}
@@ -0,0 +1,71 @@
#version 120
#import <sodium:include/legacy.glsl>
#define in attribute
#import <sodium:include/fog.glsl>
#import <sodium:include/chunk_vertex.glsl>
#import <sodium:include/chunk_matrices.glsl>
#import <sodium:include/chunk_material.glsl>
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];
}
@@ -43,9 +43,11 @@ vec4 _exp2Fog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogDensit
float getFragDistance(int fogShape, vec3 position) { float getFragDistance(int fogShape, vec3 position) {
// Use the maximum of the horizontal and vertical distance to get cylindrical fog if fog shape is cylindrical // Use the maximum of the horizontal and vertical distance to get cylindrical fog if fog shape is cylindrical
switch (fogShape) { if (fogShape == FOG_SHAPE_SPHERICAL) {
case FOG_SHAPE_SPHERICAL: return length(position); return length(position);
case FOG_SHAPE_CYLINDRICAL: return max(length(position.xz), abs(position.y)); } else if (fogShape == FOG_SHAPE_CYLINDRICAL) {
default: return length(position); // This shouldn't be possible to get, but return a sane value just in case 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
} }
} }
@@ -0,0 +1,3 @@
#extension GL_EXT_gpu_shader4 : require
#define LEGACY
#define uint unsigned int
+10 -1
View File
@@ -79,10 +79,11 @@ repositories {
includeGroup("com.gtnewhorizons") includeGroup("com.gtnewhorizons")
} }
} }
maven("https://maven.gtceu.com")
mavenLocal() mavenLocal()
} }
val lwjgl3ifyVersion = "1.0.1-38-g6410323" val lwjgl3ifyVersion = "1.0.1"
val forgePatchDeps by configurations.creating { val forgePatchDeps by configurations.creating {
isCanBeResolved = true isCanBeResolved = true
@@ -130,6 +131,14 @@ tasks.named("reobfJar").configure {
enabled = false enabled = false
} }
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "net.java.dev.jna" && requested.name == "jna") {
useVersion("5.15.0")
}
}
}
tasks.register<ReobfuscateCodeAndMixinsTask>("celeritasRemapJar") { tasks.register<ReobfuscateCodeAndMixinsTask>("celeritasRemapJar") {
tsrgMappings = mcpTasks.srgFile("mcp-srg.srg") tsrgMappings = mcpTasks.srgFile("mcp-srg.srg")
deobfMinecraftJar = mcpTasks.taskPackagePatchedMc.flatMap { it.archiveFile } deobfMinecraftJar = mcpTasks.taskPackagePatchedMc.flatMap { it.archiveFile }