Skip to content

Commit

Permalink
Merge pull request IrisShaders#917 from IMS212/trunk-concern
Browse files Browse the repository at this point in the history
Use RenderSystem and GlStateManager instead of direct GL calls when possible
  • Loading branch information
coderbot16 authored Dec 13, 2021
2 parents 704a414 + 190e4bc commit d1b6892
Show file tree
Hide file tree
Showing 39 changed files with 369 additions and 182 deletions.
173 changes: 173 additions & 0 deletions src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package net.coderbot.iris.gl;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.EXTShaderImageLoadStore;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL21;
import org.lwjgl.opengl.GL30C;
import org.lwjgl.opengl.GL42C;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
* This class is responsible for abstracting calls to OpenGL and asserting that calls are run on the render thread.
*/
public class IrisRenderSystem {
public static void generateMipmaps(int mipmapTarget) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glGenerateMipmap(mipmapTarget);
}

public static void bindAttributeLocation(int program, int index, CharSequence name) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glBindAttribLocation(program, index, name);
}

public static void texImage2D(int i, int j, int k, int l, int m, int n, int o, int p, @Nullable ByteBuffer byteBuffer) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glTexImage2D(i, j, k, l, m, n, o, p, byteBuffer);
}

public static void uniformMatrix4fv(int location, boolean transpose, FloatBuffer matrix) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniformMatrix4fv(location, transpose, matrix);
}

public static void uniform1f(int location, float v0) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform1f(location, v0);
}

public static void uniform1i(int location, int v0) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform1i(location, v0);
}

public static void uniform2f(int location, float v0, float v1) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform2f(location, v0, v1);
}

public static void uniform2i(int location, int v0, int v1) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform2i(location, v0, v1);
}

public static void uniform3f(int location, float v0, float v1, float v2) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform3f(location, v0, v1, v2);
}

public static void uniform4f(int location, float v0, float v1, float v2, float v3) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glUniform4f(location, v0, v1, v2, v3);
}

public static int getAttribLocation(int programId, String name) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
return GL30C.glGetAttribLocation(programId, name);
}

public static int getUniformLocation(int programId, String name) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
return GL30C.glGetUniformLocation(programId, name);
}

public static void blitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
}

public static void copyTexImage2D(int target, int level, int internalFormat, int x, int y, int width, int height, int border) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glCopyTexImage2D(target, level, internalFormat, x, y, width, height, border);
}

public static String getProgramInfoLog(int program) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
return GL30C.glGetProgramInfoLog(program);
}

public static String getShaderInfoLog(int shader) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
return GL30C.glGetShaderInfoLog(shader);
}

public static void drawBuffers(int[] buffers) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glDrawBuffers(buffers);
}

public static void readBuffer(int buffer) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glReadBuffer(buffer);
}

public static String getActiveUniform(int program, int index, int size, IntBuffer type, IntBuffer name) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
return GL30C.glGetActiveUniform(program, index, size, type, name);
}

public static void readPixels(int x, int y, int width, int height, int format, int type, float[] pixels) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glReadPixels(x, y, width, height, format, type, pixels);
}

public static void bufferData(int target, float[] data, int usage) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glBufferData(target, data, usage);
}

public static void vertexAttrib4f(int index, float v0, float v1, float v2, float v3) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glVertexAttrib4f(index, v0, v1, v2, v3);
}

public static void detachShader(int program, int shader) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
GL30C.glDetachShader(program, shader);
}

public static void bindImageTexture(int unit, int texture, int level, boolean layered, int layer, int access, int format) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
if (GL.getCapabilities().OpenGL42) {
GL42C.glBindImageTexture(unit, texture, level, layered, layer, access, format);
} else {
EXTShaderImageLoadStore.glBindImageTextureEXT(unit, texture, level, layered, layer, access, format);
}
}

public static int getMaxImageUnits() {
if (GL.getCapabilities().OpenGL42) {
return GlStateManager._getInteger(GL42C.GL_MAX_IMAGE_UNITS);
} else if (GL.getCapabilities().GL_EXT_shader_image_load_store) {
return GlStateManager._getInteger(EXTShaderImageLoadStore.GL_MAX_IMAGE_UNITS_EXT);
} else {
return 0;
}
}

// These functions are deprecated and unavailable in the core profile.

@Deprecated
public static void setupProjectionMatrix(float[] matrix) {
RenderSystem.assertThread(RenderSystem::isOnRenderThreadOrInit);
RenderSystem.matrixMode(GL11.GL_PROJECTION);
RenderSystem.pushMatrix();
GL20.glLoadMatrixf(matrix);
RenderSystem.matrixMode(GL11.GL_MODELVIEW);
}

@Deprecated
public static void restoreProjectionMatrix() {
RenderSystem.matrixMode(GL11.GL_PROJECTION);
RenderSystem.popMatrix();
RenderSystem.matrixMode(GL11.GL_MODELVIEW);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.GlResource;
import net.coderbot.iris.gl.IrisRenderSystem;
import org.lwjgl.opengl.GL30C;

import java.util.Arrays;
Expand All @@ -22,12 +23,12 @@ public GlFramebuffer() {

public void addDepthAttachment(int texture) {
bind();
GL30C.glFramebufferTexture2D(GL30C.GL_FRAMEBUFFER, GL30C.GL_DEPTH_ATTACHMENT, GL30C.GL_TEXTURE_2D, texture, 0);
GlStateManager._glFramebufferTexture2D(GL30C.GL_FRAMEBUFFER, GL30C.GL_DEPTH_ATTACHMENT, GL30C.GL_TEXTURE_2D, texture, 0);
}

public void addColorAttachment(int index, int texture) {
bind();
GL30C.glFramebufferTexture2D(GL30C.GL_FRAMEBUFFER, GL30C.GL_COLOR_ATTACHMENT0 + index, GL30C.GL_TEXTURE_2D, texture, 0);
GlStateManager._glFramebufferTexture2D(GL30C.GL_FRAMEBUFFER, GL30C.GL_COLOR_ATTACHMENT0 + index, GL30C.GL_TEXTURE_2D, texture, 0);
attachments.put(index, texture);
}

Expand All @@ -50,13 +51,13 @@ public void drawBuffers(int[] buffers) {
glBuffers[index++] = GL30C.GL_COLOR_ATTACHMENT0 + buffer;
}

GL30C.glDrawBuffers(glBuffers);
IrisRenderSystem.drawBuffers(glBuffers);
}

public void readBuffer(int buffer) {
bind();

GL30C.glReadBuffer(GL30C.GL_COLOR_ATTACHMENT0 + buffer);
IrisRenderSystem.readBuffer(GL30C.GL_COLOR_ATTACHMENT0 + buffer);
}

public int getColorAttachment(int index) {
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/net/coderbot/iris/gl/image/ImageBinding.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.coderbot.iris.gl.image;

import org.lwjgl.opengl.EXTShaderImageLoadStore;
import net.coderbot.iris.gl.IrisRenderSystem;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL42C;

Expand All @@ -10,23 +10,16 @@ public class ImageBinding {
private final int imageUnit;
private final int internalFormat;
private final IntSupplier textureID;
private final boolean useExt;

public ImageBinding(int imageUnit, int internalFormat, IntSupplier textureID) {
this.textureID = textureID;
this.imageUnit = imageUnit;
this.internalFormat = internalFormat;

// We can assume that image bindings are supported here, as otherwise ImageLimits
// would report that zero image units are supported.
this.useExt = !GL.getCapabilities().OpenGL42;
}

public void update() {
if (useExt) {
EXTShaderImageLoadStore.glBindImageTextureEXT(imageUnit, textureID.getAsInt(), 0, false, 0, GL42C.GL_READ_WRITE, internalFormat);
} else {
GL42C.glBindImageTexture(imageUnit, textureID.getAsInt(), 0, false, 0, GL42C.GL_READ_WRITE, internalFormat);
}
// We can assume that image bindings are supported here as either the EXT extension or 4.2 core, as otherwise ImageLimits
// would report that zero image units are supported.
IrisRenderSystem.bindImageTexture(imageUnit, textureID.getAsInt(), 0, false, 0, GL42C.GL_READ_WRITE, internalFormat);
}
}
10 changes: 3 additions & 7 deletions src/main/java/net/coderbot/iris/gl/image/ImageLimits.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.coderbot.iris.gl.image;

import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.IrisRenderSystem;
import org.lwjgl.opengl.EXTShaderImageLoadStore;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL42C;
Expand All @@ -9,13 +11,7 @@ public class ImageLimits {
private static ImageLimits instance;

private ImageLimits() {
if (GL.getCapabilities().OpenGL42) {
this.maxImageUnits = GL42C.glGetInteger(GL42C.GL_MAX_IMAGE_UNITS);
} else if (GL.getCapabilities().GL_EXT_shader_image_load_store) {
this.maxImageUnits = EXTShaderImageLoadStore.GL_MAX_IMAGE_UNITS_EXT;
} else {
this.maxImageUnits = 0;
}
this.maxImageUnits = IrisRenderSystem.getMaxImageUnits();
}

public int getMaxImageUnits() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/coderbot/iris/gl/program/Program.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.coderbot.iris.gl.program;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.shaders.ProgramManager;
import net.coderbot.iris.gl.GlResource;
import org.lwjgl.opengl.GL20C;

public final class Program extends GlResource {
private final ProgramUniforms uniforms;
Expand Down Expand Up @@ -31,7 +31,7 @@ public static void unbind() {
}

public void destroyInternal() {
GL20C.glDeleteProgram(getGlId());
GlStateManager.glDeleteProgram(getGlId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableSet;
import com.mojang.blaze3d.systems.RenderSystem;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.image.ImageHolder;
import net.coderbot.iris.gl.sampler.SamplerHolder;
import net.coderbot.iris.gl.shader.GlShader;
Expand Down Expand Up @@ -45,7 +46,7 @@ private ProgramBuilder(String name, int program, ImmutableSet<Integer> reservedT
}

public void bindAttributeLocation(int index, String name) {
GL21C.glBindAttribLocation(program, index, name);
IrisRenderSystem.bindAttributeLocation(program, index, name);
}

public static ProgramBuilder begin(String name, @Nullable String vertexSource, @Nullable String geometrySource,
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/coderbot/iris/gl/program/ProgramImages.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.coderbot.iris.gl.program;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.image.ImageBinding;
import net.coderbot.iris.gl.image.ImageHolder;
import net.coderbot.iris.gl.image.ImageLimits;
Expand All @@ -23,7 +25,7 @@ private ProgramImages(ImmutableList<ImageBinding> imageBindings, List<GlUniform1
public void update() {
if (initializer != null) {
for (GlUniform1iCall call : initializer) {
GL20C.glUniform1i(call.getLocation(), call.getValue());
IrisRenderSystem.uniform1i(call.getLocation(), call.getValue());
}

initializer = null;
Expand Down Expand Up @@ -59,12 +61,12 @@ private Builder(int program) {

@Override
public boolean hasImage(String name) {
return GL20C.glGetUniformLocation(program, name) != -1;
return GlStateManager._glGetUniformLocation(program, name) != -1;
}

@Override
public void addTextureImage(IntSupplier textureID, InternalTextureFormat internalFormat, String name) {
int location = GL20C.glGetUniformLocation(program, name);
int location = GlStateManager._glGetUniformLocation(program, name);

if (location == -1) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ImmutableSet;
import com.mojang.blaze3d.systems.RenderSystem;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.sampler.SamplerBinding;
import net.coderbot.iris.gl.sampler.SamplerHolder;
import net.coderbot.iris.gl.sampler.SamplerLimits;
Expand All @@ -27,7 +28,7 @@ private ProgramSamplers(ImmutableList<SamplerBinding> samplerBindings, List<GlUn
public void update() {
if (initializer != null) {
for (GlUniform1iCall call : initializer) {
GL20C.glUniform1i(call.getLocation(), call.getValue());
IrisRenderSystem.uniform1i(call.getLocation(), call.getValue());
}

initializer = null;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void addExternalSampler(int textureUnit, String... names) {
}

for (String name : names) {
int location = GL20C.glGetUniformLocation(program, name);
int location = IrisRenderSystem.getUniformLocation(program, name);

if (location == -1) {
// There's no active sampler with this particular name in the program.
Expand All @@ -110,7 +111,7 @@ public void addExternalSampler(int textureUnit, String... names) {

@Override
public boolean hasSampler(String name) {
return GL20C.glGetUniformLocation(program, name) != -1;
return IrisRenderSystem.getUniformLocation(program, name) != -1;
}

@Override
Expand All @@ -134,7 +135,7 @@ public boolean addDynamicSampler(IntSupplier sampler, String... names) {

private boolean addDynamicSampler(IntSupplier sampler, boolean used, String... names) {
for (String name : names) {
int location = GL20C.glGetUniformLocation(program, name);
int location = IrisRenderSystem.getUniformLocation(program, name);

if (location == -1) {
// There's no active sampler with this particular name in the program.
Expand Down
Loading

0 comments on commit d1b6892

Please sign in to comment.