diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/Backend.java b/src/main/java/com/simibubi/create/foundation/render/backend/Backend.java index 858d54572..5f53ee500 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/Backend.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/Backend.java @@ -3,15 +3,12 @@ package com.simibubi.create.foundation.render.backend; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.ByteBuffer; import java.nio.FloatBuffer; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.function.Consumer; 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.Logger; 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.ProgramSpec; 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.renderer.texture.TextureUtil; @@ -45,7 +40,7 @@ public class Backend { private static boolean enabled; public static GLCapabilities capabilities; - public static GlFunctions functions; + public static GlFeatureCompat compat; public Backend() { throw new IllegalStateException(); @@ -74,9 +69,9 @@ public class Backend { public static boolean canUseInstancing() { return enabled && - functions.vertexArrayObjectsSupported() && - functions.drawInstancedSupported() && - functions.instancedArraysSupported(); + compat.vertexArrayObjectsSupported() && + compat.drawInstancedSupported() && + compat.instancedArraysSupported(); } public static boolean canUseVBOs() { @@ -107,7 +102,7 @@ public class Backend { private static void onResourceManagerReload(IResourceManager manager, Predicate predicate) { if (predicate.test(VanillaResourceType.SHADERS)) { capabilities = GL.createCapabilities(); - functions = new GlFunctions(capabilities); + compat = new GlFeatureCompat(capabilities); OptifineHandler.refresh(); refresh(); diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlBuffer.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlBuffer.java index 8420dab87..1dbd256e5 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlBuffer.java @@ -4,7 +4,6 @@ import java.nio.ByteBuffer; import java.util.function.Consumer; import com.simibubi.create.foundation.render.backend.Backend; -import com.simibubi.create.foundation.render.backend.gl.versioned.GlFunctions; import org.lwjgl.opengl.GL20; public class GlBuffer extends GlObject { @@ -35,11 +34,11 @@ public class GlBuffer extends GlObject { } public void map(int length, Consumer upload) { - Backend.functions.mapBuffer(bufferType, 0, length, upload); + Backend.compat.mapBuffer(bufferType, 0, length, upload); } public void map(int offset, int length, Consumer upload) { - Backend.functions.mapBuffer(bufferType, offset, length, upload); + Backend.compat.mapBuffer(bufferType, offset, length, upload); } protected void deleteInternal(int handle) { diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlVertexArray.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlVertexArray.java index cf6229f6e..e4854990e 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlVertexArray.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/GlVertexArray.java @@ -3,19 +3,18 @@ package com.simibubi.create.foundation.render.backend.gl; import java.util.function.Consumer; import com.simibubi.create.foundation.render.backend.Backend; -import org.lwjgl.opengl.GL30; public class GlVertexArray extends GlObject { public GlVertexArray() { - setHandle(Backend.functions.genVertexArrays()); + setHandle(Backend.compat.genVertexArrays()); } public void bind() { - Backend.functions.bindVertexArray(handle()); + Backend.compat.bindVertexArray(handle()); } public void unbind() { - Backend.functions.bindVertexArray(0); + Backend.compat.bindVertexArray(0); } public void with(Consumer action) { @@ -25,6 +24,6 @@ public class GlVertexArray extends GlObject { } protected void deleteInternal(int handle) { - Backend.functions.deleteVertexArrays(handle); + Backend.compat.deleteVertexArrays(handle); } } diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFunctions.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFeatureCompat.java similarity index 93% rename from src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFunctions.java rename to src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFeatureCompat.java index 1345661fc..b2eba08e1 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFunctions.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/GlFeatureCompat.java @@ -13,19 +13,23 @@ import java.util.function.Consumer; * 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 class GlFeatureCompat { public final MapBuffer mapBuffer; public final VertexArrayObject vertexArrayObject; public final InstancedArrays instancedArrays; public final DrawInstanced drawInstanced; - public GlFunctions(GLCapabilities caps) { + public final RGPixelFormat pixelFormat; + + public GlFeatureCompat(GLCapabilities caps) { mapBuffer = getLatest(MapBuffer.class, caps); vertexArrayObject = getLatest(VertexArrayObject.class, caps); instancedArrays = getLatest(InstancedArrays.class, caps); drawInstanced = getLatest(DrawInstanced.class, caps); + + pixelFormat = getLatest(RGPixelFormat.class, caps); } public void mapBuffer(int target, int offset, int length, Consumer upload) { diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/RGPixelFormat.java b/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/RGPixelFormat.java new file mode 100644 index 000000000..9cf035535 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/render/backend/gl/versioned/RGPixelFormat.java @@ -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(); +} diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedModel.java b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedModel.java index 3b092c919..5903ae496 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedModel.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/instancing/InstancedModel.java @@ -115,7 +115,7 @@ public abstract class InstancedModel extends BufferedMod protected void doRender() { vao.with(vao -> { 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 extends BufferedMod instanceFormat.vertexAttribPointers(staticAttributes); for (int i = 0; i < instanceFormat.getShaderAttributeCount(); i++) { - Backend.functions.vertexAttribDivisor(i + staticAttributes, 1); + Backend.compat.vertexAttribDivisor(i + staticAttributes, 1); } }); diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/light/LightVolume.java b/src/main/java/com/simibubi/create/foundation/render/backend/light/LightVolume.java index 143964ba5..cada1ecad 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/light/LightVolume.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/light/LightVolume.java @@ -2,11 +2,12 @@ package com.simibubi.create.foundation.render.backend.light; 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.GL12; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL20; -import org.lwjgl.opengl.GL40; import org.lwjgl.system.MemoryUtil; import com.simibubi.create.foundation.render.backend.RenderWork; @@ -28,11 +29,15 @@ public class LightVolume { private final GlTexture glTexture; + private final RGPixelFormat pixelFormat; + public LightVolume(GridAlignedBB sampleVolume) { setSampleVolume(sampleVolume); + pixelFormat = Backend.compat.pixelFormat; + 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 GL20.glActiveTexture(GL20.GL_TEXTURE4); @@ -41,7 +46,7 @@ public class LightVolume { int sizeX = textureVolume.sizeX(); int sizeY = textureVolume.sizeY(); 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(); 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. if (lightData == null || removed) return; - GL13.glActiveTexture(GL40.GL_TEXTURE4); + GL13.glActiveTexture(GL20.GL_TEXTURE4); 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_MAG_FILTER, GL13.GL_LINEAR); @@ -245,7 +250,7 @@ public class LightVolume { int sizeY = textureVolume.sizeY(); 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 bufferDirty = false; @@ -269,7 +274,7 @@ public class LightVolume { byte b = (byte) ((block & 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 + 1, s); } @@ -277,16 +282,16 @@ public class LightVolume { private void writeBlock(int x, int y, int z, int block) { 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) { 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) { - return (x + textureVolume.sizeX() * (y + z * textureVolume.sizeY())) * 2; + private int posToIndex(int x, int y, int z) { + return (x + textureVolume.sizeX() * (y + z * textureVolume.sizeY())) * pixelFormat.byteCount(); } }