Skip to content

Commit

Permalink
Use sodium's shader code in Iris, and avoid an AMD driver bug in glSh…
Browse files Browse the repository at this point in the history
…aderSource

Fixes #77
  • Loading branch information
coderbot16 committed Feb 26, 2021
1 parent bb1909c commit 629fbed
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 83 deletions.
12 changes: 5 additions & 7 deletions src/main/java/net/coderbot/iris/gl/program/Program.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package net.coderbot.iris.gl.program;

import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.GlResource;

import net.minecraft.client.gl.GlProgram;
import net.minecraft.client.gl.GlProgramManager;
import org.lwjgl.opengl.GL20C;

public final class Program extends GlResource {
private final ProgramUniforms uniforms;

Program(GlProgram glProgram, ProgramUniforms uniforms) {
super(glProgram.getProgramRef());
Program(int program, ProgramUniforms uniforms) {
super(program);

this.uniforms = uniforms;
}

public void use() {
GlProgramManager.useProgram(getGlId());
GL20C.glUseProgram(getGlId());

uniforms.update();
}

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

/**
Expand Down
93 changes: 21 additions & 72 deletions src/main/java/net/coderbot/iris/gl/program/ProgramBuilder.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,52 @@
package net.coderbot.iris.gl.program;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.systems.RenderSystem;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.uniform.Uniform;
import net.coderbot.iris.gl.uniform.UniformHolder;
import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.gl.shader.GlShader;
import net.coderbot.iris.gl.shader.ProgramCreator;
import net.coderbot.iris.gl.shader.ShaderConstants;
import net.coderbot.iris.gl.shader.ShaderType;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL21;
import org.lwjgl.opengl.GL21C;

import net.minecraft.client.gl.GlProgram;
import net.minecraft.client.gl.GlProgramManager;
import net.minecraft.client.gl.GlShader;

public class ProgramBuilder extends ProgramUniforms.Builder {
private final GlProgram program;
private static final ShaderConstants EMPTY_CONSTANTS = ShaderConstants.builder().build();

private ProgramBuilder(String name, GlProgram program) {
super(name, program.getProgramRef());
private final int program;

private ProgramBuilder(String name, int program) {
super(name, program);

this.program = program;
}

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

public static ProgramBuilder begin(String name, @Nullable String vertexSource, @Nullable String fragmentSource) throws IOException {
public static ProgramBuilder begin(String name, @Nullable String vertexSource, @Nullable String fragmentSource) {
RenderSystem.assertThread(RenderSystem::isOnRenderThread);

GlShader vertex;
GlShader fragment;

try {
InputStream vertexSourceStream = new ByteArrayInputStream(vertexSource.getBytes(StandardCharsets.UTF_8));
vertex = GlShader.createFromResource(GlShader.Type.VERTEX, name + ".vsh", vertexSourceStream, "iris");
} catch (IOException e) {
throw new IOException("Failed to compile vertex shader for program " + name, e);
vertex = new GlShader(ShaderType.VERTEX, name + ".vsh", vertexSource, EMPTY_CONSTANTS);
} catch (RuntimeException e) {
throw new RuntimeException("Failed to compile vertex shader for program " + name, e);
}

try {
InputStream fragmentSourceStream = new ByteArrayInputStream(fragmentSource.getBytes(StandardCharsets.UTF_8));
fragment = GlShader.createFromResource(GlShader.Type.FRAGMENT, name + ".fsh", fragmentSourceStream, "iris");
} catch (IOException e) {
throw new IOException("Failed to compile fragment shader for program " + name, e);
}

int programId;

try {
programId = GlProgramManager.createProgram();
} catch (IOException e) {
e.printStackTrace();
programId = 0;
fragment = new GlShader(ShaderType.FRAGMENT, name + ".fsh", fragmentSource, EMPTY_CONSTANTS);
} catch (RuntimeException e) {
throw new RuntimeException("Failed to compile fragment shader for program " + name, e);
}

final int finalProgramId = programId;

GlProgram program = new GlProgram() {
@Override
public int getProgramRef() {
return finalProgramId;
}

@Override
public void markUniformsDirty() {
// nah
}

@Override
public GlShader getVertexShader() {
return vertex;
}

@Override
public GlShader getFragmentShader() {
return fragment;
}
};

try {
GlProgramManager.linkProgram(program);
} catch (IOException e) {
e.printStackTrace();
}
int programId = ProgramCreator.create(name, vertex, fragment);

vertex.release();
fragment.release();
vertex.destroy();
fragment.destroy();

return new ProgramBuilder(name, program);
return new ProgramBuilder(name, programId);
}

public Program build() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/coderbot/iris/gl/shader/GlShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GlShader extends GlResource {
private final String name;

public GlShader(ShaderType type, String name, String src, ShaderConstants constants) {
super(createShader(type, src, name, constants));
super(createShader(type, name, src, constants));

this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ProgramCreator {
private static final Logger LOGGER = LogManager.getLogger(ProgramCreator.class);

public int create(String name, GlShader... shaders) {
public static int create(String name, GlShader... shaders) {
int program = GL20C.glCreateProgram();

for (GlShader shader : shaders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private Pass createPass(ShaderPack.ProgramSource source) {
try {
builder = ProgramBuilder.begin(source.getName(), source.getVertexSource().orElse(null),
source.getFragmentSource().orElse(null));
} catch (IOException e) {
} catch (RuntimeException e) {
// TODO: Better error handling
throw new RuntimeException("Shader compilation failed!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private Pair<Program, ProgramDirectives> createProgram(ShaderPack.ProgramSource
try {
builder = ProgramBuilder.begin(source.getName(), source.getVertexSource().orElse(null),
source.getFragmentSource().orElse(null));
} catch (IOException e) {
} catch (RuntimeException e) {
// TODO: Better error handling
throw new RuntimeException("Shader compilation failed!", e);
}
Expand Down

0 comments on commit 629fbed

Please sign in to comment.