Even more compatibility.

This commit is contained in:
JozsefA 2021-02-23 16:20:46 -08:00
parent f525b7e913
commit cad71e5843
7 changed files with 110 additions and 33 deletions

View File

@ -3,15 +3,12 @@ package com.simibubi.create.foundation.render.backend;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import com.simibubi.create.foundation.render.backend.gl.versioned.GlFunctions; import com.simibubi.create.foundation.render.backend.gl.versioned.GlFeatureCompat;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
@ -23,8 +20,6 @@ import com.simibubi.create.foundation.render.backend.gl.shader.GlProgram;
import com.simibubi.create.foundation.render.backend.gl.shader.GlShader; import com.simibubi.create.foundation.render.backend.gl.shader.GlShader;
import com.simibubi.create.foundation.render.backend.gl.shader.ProgramSpec; import com.simibubi.create.foundation.render.backend.gl.shader.ProgramSpec;
import com.simibubi.create.foundation.render.backend.gl.shader.ShaderType; import com.simibubi.create.foundation.render.backend.gl.shader.ShaderType;
import com.simibubi.create.foundation.render.backend.gl.versioned.GlVersioned;
import com.simibubi.create.foundation.render.backend.gl.versioned.MapBuffer;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureUtil; import net.minecraft.client.renderer.texture.TextureUtil;
@ -45,7 +40,7 @@ public class Backend {
private static boolean enabled; private static boolean enabled;
public static GLCapabilities capabilities; public static GLCapabilities capabilities;
public static GlFunctions functions; public static GlFeatureCompat compat;
public Backend() { public Backend() {
throw new IllegalStateException(); throw new IllegalStateException();
@ -74,9 +69,9 @@ public class Backend {
public static boolean canUseInstancing() { public static boolean canUseInstancing() {
return enabled && return enabled &&
functions.vertexArrayObjectsSupported() && compat.vertexArrayObjectsSupported() &&
functions.drawInstancedSupported() && compat.drawInstancedSupported() &&
functions.instancedArraysSupported(); compat.instancedArraysSupported();
} }
public static boolean canUseVBOs() { public static boolean canUseVBOs() {
@ -107,7 +102,7 @@ public class Backend {
private static void onResourceManagerReload(IResourceManager manager, Predicate<IResourceType> predicate) { private static void onResourceManagerReload(IResourceManager manager, Predicate<IResourceType> predicate) {
if (predicate.test(VanillaResourceType.SHADERS)) { if (predicate.test(VanillaResourceType.SHADERS)) {
capabilities = GL.createCapabilities(); capabilities = GL.createCapabilities();
functions = new GlFunctions(capabilities); compat = new GlFeatureCompat(capabilities);
OptifineHandler.refresh(); OptifineHandler.refresh();
refresh(); refresh();

View File

@ -4,7 +4,6 @@ import java.nio.ByteBuffer;
import java.util.function.Consumer; import java.util.function.Consumer;
import com.simibubi.create.foundation.render.backend.Backend; import com.simibubi.create.foundation.render.backend.Backend;
import com.simibubi.create.foundation.render.backend.gl.versioned.GlFunctions;
import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL20;
public class GlBuffer extends GlObject { public class GlBuffer extends GlObject {
@ -35,11 +34,11 @@ public class GlBuffer extends GlObject {
} }
public void map(int length, Consumer<ByteBuffer> upload) { public void map(int length, Consumer<ByteBuffer> upload) {
Backend.functions.mapBuffer(bufferType, 0, length, upload); Backend.compat.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.functions.mapBuffer(bufferType, offset, length, upload); Backend.compat.mapBuffer(bufferType, offset, length, upload);
} }
protected void deleteInternal(int handle) { protected void deleteInternal(int handle) {

View File

@ -3,19 +3,18 @@ package com.simibubi.create.foundation.render.backend.gl;
import java.util.function.Consumer; import java.util.function.Consumer;
import com.simibubi.create.foundation.render.backend.Backend; import com.simibubi.create.foundation.render.backend.Backend;
import org.lwjgl.opengl.GL30;
public class GlVertexArray extends GlObject { public class GlVertexArray extends GlObject {
public GlVertexArray() { public GlVertexArray() {
setHandle(Backend.functions.genVertexArrays()); setHandle(Backend.compat.genVertexArrays());
} }
public void bind() { public void bind() {
Backend.functions.bindVertexArray(handle()); Backend.compat.bindVertexArray(handle());
} }
public void unbind() { public void unbind() {
Backend.functions.bindVertexArray(0); Backend.compat.bindVertexArray(0);
} }
public void with(Consumer<GlVertexArray> action) { public void with(Consumer<GlVertexArray> action) {
@ -25,6 +24,6 @@ public class GlVertexArray extends GlObject {
} }
protected void deleteInternal(int handle) { protected void deleteInternal(int handle) {
Backend.functions.deleteVertexArrays(handle); Backend.compat.deleteVertexArrays(handle);
} }
} }

View File

@ -13,19 +13,23 @@ import java.util.function.Consumer;
* 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 GlFunctions { public class GlFeatureCompat {
public final MapBuffer mapBuffer; public final MapBuffer mapBuffer;
public final VertexArrayObject vertexArrayObject; public final VertexArrayObject vertexArrayObject;
public final InstancedArrays instancedArrays; public final InstancedArrays instancedArrays;
public final DrawInstanced drawInstanced; public final DrawInstanced drawInstanced;
public GlFunctions(GLCapabilities caps) { public final RGPixelFormat pixelFormat;
public GlFeatureCompat(GLCapabilities caps) {
mapBuffer = getLatest(MapBuffer.class, caps); mapBuffer = getLatest(MapBuffer.class, caps);
vertexArrayObject = getLatest(VertexArrayObject.class, caps); vertexArrayObject = getLatest(VertexArrayObject.class, caps);
instancedArrays = getLatest(InstancedArrays.class, caps); instancedArrays = getLatest(InstancedArrays.class, caps);
drawInstanced = getLatest(DrawInstanced.class, caps); drawInstanced = getLatest(DrawInstanced.class, caps);
pixelFormat = getLatest(RGPixelFormat.class, caps);
} }
public void mapBuffer(int target, int offset, int length, Consumer<ByteBuffer> upload) { public void mapBuffer(int target, int offset, int length, Consumer<ByteBuffer> upload) {

View File

@ -0,0 +1,75 @@
package com.simibubi.create.foundation.render.backend.gl.versioned;
import org.lwjgl.opengl.*;
public enum RGPixelFormat implements GlVersioned {
GL30_RG {
@Override
public boolean supported(GLCapabilities caps) {
return caps.OpenGL30;
}
@Override
public int internalFormat() {
return GL30.GL_RG8;
}
@Override
public int format() {
return GL30.GL_RG;
}
@Override
public int byteCount() {
return 2;
}
},
GL11_RGB {
@Override
public boolean supported(GLCapabilities caps) {
return caps.OpenGL11;
}
@Override
public int internalFormat() {
return GL11.GL_RGB8;
}
@Override
public int format() {
return GL11.GL_RGB;
}
@Override
public int byteCount() {
return 3;
}
},
UNSUPPORTED {
@Override
public boolean supported(GLCapabilities caps) {
return true;
}
@Override
public int internalFormat() {
throw new UnsupportedOperationException();
}
@Override
public int format() {
throw new UnsupportedOperationException();
}
@Override
public int byteCount() {
throw new UnsupportedOperationException();
}
}
;
public abstract int internalFormat();
public abstract int format();
public abstract int byteCount();
}

View File

@ -115,7 +115,7 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
protected void doRender() { protected void doRender() {
vao.with(vao -> { vao.with(vao -> {
renderSetup(); renderSetup();
Backend.functions.drawArraysInstanced(GL11.GL_QUADS, 0, vertexCount, glInstanceCount); Backend.compat.drawArraysInstanced(GL11.GL_QUADS, 0, vertexCount, glInstanceCount);
}); });
} }
@ -162,7 +162,7 @@ public abstract class InstancedModel<D extends InstanceData> extends BufferedMod
instanceFormat.vertexAttribPointers(staticAttributes); instanceFormat.vertexAttribPointers(staticAttributes);
for (int i = 0; i < instanceFormat.getShaderAttributeCount(); i++) { for (int i = 0; i < instanceFormat.getShaderAttributeCount(); i++) {
Backend.functions.vertexAttribDivisor(i + staticAttributes, 1); Backend.compat.vertexAttribDivisor(i + staticAttributes, 1);
} }
}); });

View File

@ -2,11 +2,12 @@ package com.simibubi.create.foundation.render.backend.light;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import com.simibubi.create.foundation.render.backend.Backend;
import com.simibubi.create.foundation.render.backend.gl.versioned.RGPixelFormat;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12; import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL40;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.MemoryUtil;
import com.simibubi.create.foundation.render.backend.RenderWork; import com.simibubi.create.foundation.render.backend.RenderWork;
@ -28,11 +29,15 @@ public class LightVolume {
private final GlTexture glTexture; private final GlTexture glTexture;
private final RGPixelFormat pixelFormat;
public LightVolume(GridAlignedBB sampleVolume) { public LightVolume(GridAlignedBB sampleVolume) {
setSampleVolume(sampleVolume); setSampleVolume(sampleVolume);
pixelFormat = Backend.compat.pixelFormat;
this.glTexture = new GlTexture(GL20.GL_TEXTURE_3D); this.glTexture = new GlTexture(GL20.GL_TEXTURE_3D);
this.lightData = MemoryUtil.memAlloc(this.textureVolume.volume() * 2); // TODO: reduce this to span only sampleVolume this.lightData = MemoryUtil.memAlloc(this.textureVolume.volume() * pixelFormat.byteCount());
// allocate space for the texture // allocate space for the texture
GL20.glActiveTexture(GL20.GL_TEXTURE4); GL20.glActiveTexture(GL20.GL_TEXTURE4);
@ -41,7 +46,7 @@ public class LightVolume {
int sizeX = textureVolume.sizeX(); int sizeX = textureVolume.sizeX();
int sizeY = textureVolume.sizeY(); int sizeY = textureVolume.sizeY();
int sizeZ = textureVolume.sizeZ(); int sizeZ = textureVolume.sizeZ();
GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0, GL40.GL_RG8, sizeX, sizeY, sizeZ, 0, GL40.GL_RG, GL40.GL_UNSIGNED_BYTE, 0); GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0, pixelFormat.internalFormat(), sizeX, sizeY, sizeZ, 0, pixelFormat.format(), GL20.GL_UNSIGNED_BYTE, 0);
glTexture.unbind(); glTexture.unbind();
GL20.glActiveTexture(GL20.GL_TEXTURE0); GL20.glActiveTexture(GL20.GL_TEXTURE0);
@ -222,7 +227,7 @@ public class LightVolume {
// just in case something goes wrong or we accidentally call this before this volume is properly disposed of. // just in case something goes wrong or we accidentally call this before this volume is properly disposed of.
if (lightData == null || removed) return; if (lightData == null || removed) return;
GL13.glActiveTexture(GL40.GL_TEXTURE4); GL13.glActiveTexture(GL20.GL_TEXTURE4);
glTexture.bind(); glTexture.bind();
GL11.glTexParameteri(GL13.GL_TEXTURE_3D, GL13.GL_TEXTURE_MIN_FILTER, GL13.GL_LINEAR); GL11.glTexParameteri(GL13.GL_TEXTURE_3D, GL13.GL_TEXTURE_MIN_FILTER, GL13.GL_LINEAR);
GL11.glTexParameteri(GL13.GL_TEXTURE_3D, GL13.GL_TEXTURE_MAG_FILTER, GL13.GL_LINEAR); GL11.glTexParameteri(GL13.GL_TEXTURE_3D, GL13.GL_TEXTURE_MAG_FILTER, GL13.GL_LINEAR);
@ -245,7 +250,7 @@ public class LightVolume {
int sizeY = textureVolume.sizeY(); int sizeY = textureVolume.sizeY();
int sizeZ = textureVolume.sizeZ(); int sizeZ = textureVolume.sizeZ();
GL12.glTexSubImage3D(GL12.GL_TEXTURE_3D, 0, 0, 0, 0, sizeX, sizeY, sizeZ, GL40.GL_RG, GL40.GL_UNSIGNED_BYTE, lightData); GL12.glTexSubImage3D(GL12.GL_TEXTURE_3D, 0, 0, 0, 0, sizeX, sizeY, sizeZ, pixelFormat.format(), GL20.GL_UNSIGNED_BYTE, lightData);
GL20.glPixelStorei(GL20.GL_UNPACK_ALIGNMENT, 4); // 4 is the default GL20.glPixelStorei(GL20.GL_UNPACK_ALIGNMENT, 4); // 4 is the default
bufferDirty = false; bufferDirty = false;
@ -269,7 +274,7 @@ public class LightVolume {
byte b = (byte) ((block & 0xF) << 4); byte b = (byte) ((block & 0xF) << 4);
byte s = (byte) ((sky & 0xF) << 4); byte s = (byte) ((sky & 0xF) << 4);
int i = index(x, y, z); int i = posToIndex(x, y, z);
lightData.put(i, b); lightData.put(i, b);
lightData.put(i + 1, s); lightData.put(i + 1, s);
} }
@ -277,16 +282,16 @@ public class LightVolume {
private void writeBlock(int x, int y, int z, int block) { private void writeBlock(int x, int y, int z, int block) {
byte b = (byte) ((block & 0xF) << 4); byte b = (byte) ((block & 0xF) << 4);
lightData.put(index(x, y, z), b); lightData.put(posToIndex(x, y, z), b);
} }
private void writeSky(int x, int y, int z, int sky) { private void writeSky(int x, int y, int z, int sky) {
byte b = (byte) ((sky & 0xF) << 4); byte b = (byte) ((sky & 0xF) << 4);
lightData.put(index(x, y, z) + 1, b); lightData.put(posToIndex(x, y, z) + 1, b);
} }
private int index(int x, int y, int z) { private int posToIndex(int x, int y, int z) {
return (x + textureVolume.sizeX() * (y + z * textureVolume.sizeY())) * 2; return (x + textureVolume.sizeX() * (y + z * textureVolume.sizeY())) * pixelFormat.byteCount();
} }
} }