gpu: refactor shader compilation

This commit is contained in:
Max Weber
2020-02-10 18:33:27 -07:00
committed by Adam
parent 9cf95ef324
commit cf9f7fc9fb
6 changed files with 214 additions and 392 deletions

View File

@@ -25,8 +25,6 @@
package net.runelite.client.plugins.gpu;
import com.jogamp.opengl.GL4;
import java.io.InputStream;
import java.util.Scanner;
class GLUtil
{
@@ -58,14 +56,14 @@ class GLUtil
{
byte[] err = new byte[ERR_LEN];
gl.glGetShaderInfoLog(shader, ERR_LEN, buf, 0, err, 0);
return new String(err);
return new String(err, 0, buf[0]);
}
static String glGetProgramInfoLog(GL4 gl, int program)
{
byte[] err = new byte[ERR_LEN];
gl.glGetProgramInfoLog(program, ERR_LEN, buf, 0, err, 0);
return new String(err);
return new String(err, 0, buf[0]);
}
static int glGenVertexArrays(GL4 gl)
@@ -127,76 +125,4 @@ class GLUtil
buf[0] = renderBuffer;
gl.glDeleteRenderbuffers(1, buf, 0);
}
static void loadShaders(GL4 gl, int glProgram, int glVertexShader, int glGeometryShader, int glFragmentShader,
String vertexShaderStr, String geomShaderStr, String fragShaderStr) throws ShaderException
{
compileAndAttach(gl, glProgram, glVertexShader, vertexShaderStr);
if (glGeometryShader != -1)
{
compileAndAttach(gl, glProgram, glGeometryShader, geomShaderStr);
}
compileAndAttach(gl, glProgram, glFragmentShader, fragShaderStr);
gl.glLinkProgram(glProgram);
if (glGetProgram(gl, glProgram, gl.GL_LINK_STATUS) == gl.GL_FALSE)
{
String err = glGetProgramInfoLog(gl, glProgram);
throw new ShaderException(err);
}
gl.glValidateProgram(glProgram);
if (glGetProgram(gl, glProgram, gl.GL_VALIDATE_STATUS) == gl.GL_FALSE)
{
String err = glGetProgramInfoLog(gl, glProgram);
throw new ShaderException(err);
}
}
static void loadComputeShader(GL4 gl, int glProgram, int glComputeShader, String str) throws ShaderException
{
compileAndAttach(gl, glProgram, glComputeShader, str);
gl.glLinkProgram(glProgram);
if (glGetProgram(gl, glProgram, gl.GL_LINK_STATUS) == gl.GL_FALSE)
{
String err = glGetProgramInfoLog(gl, glProgram);
throw new ShaderException(err);
}
gl.glValidateProgram(glProgram);
if (glGetProgram(gl, glProgram, gl.GL_VALIDATE_STATUS) == gl.GL_FALSE)
{
String err = glGetProgramInfoLog(gl, glProgram);
throw new ShaderException(err);
}
}
private static void compileAndAttach(GL4 gl, int program, int shader, String source) throws ShaderException
{
gl.glShaderSource(shader, 1, new String[]{source}, null);
gl.glCompileShader(shader);
if (glGetShader(gl, shader, gl.GL_COMPILE_STATUS) == gl.GL_TRUE)
{
gl.glAttachShader(program, shader);
}
else
{
String err = glGetShaderInfoLog(gl, shader);
throw new ShaderException(err);
}
}
static String inputStreamToString(InputStream in)
{
Scanner scanner = new Scanner(in).useDelimiter("\\A");
return scanner.next();
}
}

View File

@@ -46,7 +46,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.function.Function;
import javax.inject.Inject;
import javax.swing.SwingUtilities;
import jogamp.nativewindow.SurfaceScaleUtils;
@@ -124,28 +123,41 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private GLContext glContext;
private GLDrawable glDrawable;
static final String LINUX_VERSION_HEADER =
"#version 420\n" +
"#extension GL_ARB_compute_shader : require\n" +
"#extension GL_ARB_shader_storage_buffer_object : require\n" +
"#extension GL_ARB_explicit_attrib_location : require\n";
static final String WINDOWS_VERSION_HEADER = "#version 430\n";
static final Shader PROGRAM = new Shader()
.add(GL4.GL_VERTEX_SHADER, "vert.glsl")
.add(GL4.GL_GEOMETRY_SHADER, "geom.glsl")
.add(GL4.GL_FRAGMENT_SHADER, "frag.glsl");
static final Shader COMPUTE_PROGRAM = new Shader()
.add(GL4.GL_COMPUTE_SHADER, "comp.glsl");
static final Shader SMALL_COMPUTE_PROGRAM = new Shader()
.add(GL4.GL_COMPUTE_SHADER, "comp_small.glsl");
static final Shader UNORDERED_COMPUTE_PROGRAM = new Shader()
.add(GL4.GL_COMPUTE_SHADER, "comp_unordered.glsl");
static final Shader UI_PROGRAM = new Shader()
.add(GL4.GL_VERTEX_SHADER, "vertui.glsl")
.add(GL4.GL_FRAGMENT_SHADER, "fragui.glsl");
private int glProgram;
private int glVertexShader;
private int glGeomShader;
private int glFragmentShader;
private int glComputeProgram;
private int glComputeShader;
private int glSmallComputeProgram;
private int glSmallComputeShader;
private int glUnorderedComputeProgram;
private int glUnorderedComputeShader;
private int glUiProgram;
private int vaoHandle;
private int interfaceTexture;
private int glUiProgram;
private int glUiVertexShader;
private int glUiFragmentShader;
private int vaoUiHandle;
private int vboUiHandle;
@@ -434,79 +446,23 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private void initProgram() throws ShaderException
{
glProgram = gl.glCreateProgram();
glVertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER);
glGeomShader = gl.glCreateShader(gl.GL_GEOMETRY_SHADER);
glFragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
final String glVersionHeader;
if (OSType.getOSType() == OSType.Linux)
String versionHeader = OSType.getOSType() == OSType.Linux ? LINUX_VERSION_HEADER : WINDOWS_VERSION_HEADER;
Template template = new Template();
template.add(key ->
{
glVersionHeader =
"#version 420\n" +
"#extension GL_ARB_compute_shader : require\n" +
"#extension GL_ARB_shader_storage_buffer_object : require\n";
}
else
{
glVersionHeader = "#version 430\n";
}
Function<String, String> resourceLoader = (s) ->
{
if (s.endsWith(".glsl"))
if ("version_header".equals(key))
{
return inputStreamToString(getClass().getResourceAsStream(s));
return versionHeader;
}
return null;
});
template.addInclude(GpuPlugin.class);
if (s.equals("version_header"))
{
return glVersionHeader;
}
return "";
};
Template template = new Template(resourceLoader);
String source = template.process(resourceLoader.apply("geom.glsl"));
template = new Template(resourceLoader);
String vertSource = template.process(resourceLoader.apply("vert.glsl"));
template = new Template(resourceLoader);
String fragSource = template.process(resourceLoader.apply("frag.glsl"));
GLUtil.loadShaders(gl, glProgram, glVertexShader, glGeomShader, glFragmentShader,
vertSource,
source,
fragSource);
glComputeProgram = gl.glCreateProgram();
glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
template = new Template(resourceLoader);
source = template.process(resourceLoader.apply("comp.glsl"));
GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
glSmallComputeProgram = gl.glCreateProgram();
glSmallComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
template = new Template(resourceLoader);
source = template.process(resourceLoader.apply("comp_small.glsl"));
GLUtil.loadComputeShader(gl, glSmallComputeProgram, glSmallComputeShader, source);
glUnorderedComputeProgram = gl.glCreateProgram();
glUnorderedComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
template = new Template(resourceLoader);
source = template.process(resourceLoader.apply("comp_unordered.glsl"));
GLUtil.loadComputeShader(gl, glUnorderedComputeProgram, glUnorderedComputeShader, source);
glUiProgram = gl.glCreateProgram();
glUiVertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER);
glUiFragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
GLUtil.loadShaders(gl, glUiProgram, glUiVertexShader, -1, glUiFragmentShader,
inputStreamToString(getClass().getResourceAsStream("vertui.glsl")),
null,
inputStreamToString(getClass().getResourceAsStream("fragui.glsl")));
glProgram = PROGRAM.compile(gl, template);
glComputeProgram = COMPUTE_PROGRAM.compile(gl, template);
glSmallComputeProgram = SMALL_COMPUTE_PROGRAM.compile(gl, template);
glUnorderedComputeProgram = UNORDERED_COMPUTE_PROGRAM.compile(gl, template);
glUiProgram = UI_PROGRAM.compile(gl, template);
initUniforms();
}
@@ -532,46 +488,18 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private void shutdownProgram()
{
gl.glDeleteShader(glVertexShader);
glVertexShader = -1;
gl.glDeleteShader(glGeomShader);
glGeomShader = -1;
gl.glDeleteShader(glFragmentShader);
glFragmentShader = -1;
gl.glDeleteProgram(glProgram);
glProgram = -1;
///
gl.glDeleteShader(glComputeShader);
glComputeShader = -1;
gl.glDeleteProgram(glComputeProgram);
glComputeProgram = -1;
gl.glDeleteShader(glSmallComputeShader);
glSmallComputeShader = -1;
gl.glDeleteProgram(glSmallComputeProgram);
glSmallComputeProgram = -1;
gl.glDeleteShader(glUnorderedComputeShader);
glUnorderedComputeShader = -1;
gl.glDeleteProgram(glUnorderedComputeProgram);
glUnorderedComputeProgram = -1;
///
gl.glDeleteShader(glUiVertexShader);
glUiVertexShader = -1;
gl.glDeleteShader(glUiFragmentShader);
glUiFragmentShader = -1;
gl.glDeleteProgram(glUiProgram);
glUiProgram = -1;
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* Copyright (c) 2020 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.gpu;
import com.google.common.annotations.VisibleForTesting;
import com.jogamp.opengl.GL4;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.runelite.client.plugins.gpu.template.Template;
public class Shader
{
@VisibleForTesting
final List<Unit> units = new ArrayList<>();
@RequiredArgsConstructor
@VisibleForTesting
static class Unit
{
@Getter
private final int type;
@Getter
private final String filename;
}
public Shader()
{
}
public Shader add(int type, String name)
{
units.add(new Unit(type, name));
return this;
}
public int compile(GL4 gl, Template template) throws ShaderException
{
int program = gl.glCreateProgram();
int[] shaders = new int[units.size()];
int i = 0;
boolean ok = false;
try
{
while (i < shaders.length)
{
Unit unit = units.get(i);
int shader = gl.glCreateShader(unit.type);
String source = template.load(unit.filename);
gl.glShaderSource(shader, 1, new String[]{source}, null);
gl.glCompileShader(shader);
if (GLUtil.glGetShader(gl, shader, gl.GL_COMPILE_STATUS) != gl.GL_TRUE)
{
String err = GLUtil.glGetShaderInfoLog(gl, shader);
gl.glDeleteShader(shader);
throw new ShaderException(err);
}
gl.glAttachShader(program, shader);
shaders[i++] = shader;
}
gl.glLinkProgram(program);
if (GLUtil.glGetProgram(gl, program, gl.GL_LINK_STATUS) == gl.GL_FALSE)
{
String err = GLUtil.glGetProgramInfoLog(gl, program);
throw new ShaderException(err);
}
gl.glValidateProgram(program);
if (GLUtil.glGetProgram(gl, program, gl.GL_VALIDATE_STATUS) == gl.GL_FALSE)
{
String err = GLUtil.glGetProgramInfoLog(gl, program);
throw new ShaderException(err);
}
ok = true;
}
finally
{
while (i > 0)
{
int shader = shaders[--i];
gl.glDetachShader(program, shader);
gl.glDeleteShader(shader);
}
if (!ok)
{
gl.glDeleteProgram(program);
}
}
return program;
}
}

View File

@@ -24,15 +24,18 @@
*/
package net.runelite.client.plugins.gpu.template;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
public class Template
{
private final Function<String, String> resourceLoader;
private final List<Function<String, String>> resourceLoaders = new ArrayList<>();
public Template(Function<String, String> resourceLoader)
public Template()
{
this.resourceLoader = resourceLoader;
}
public String process(String str)
@@ -43,8 +46,8 @@ public class Template
if (line.startsWith("#include "))
{
String resource = line.substring(9);
String resourceStr = resourceLoader.apply(resource);
sb.append(process(resourceStr));
String resourceStr = load(resource);
sb.append(resourceStr);
}
else
{
@@ -53,4 +56,43 @@ public class Template
}
return sb.toString();
}
public String load(String filename)
{
for (Function<String, String> loader : resourceLoaders)
{
String value = loader.apply(filename);
if (value != null)
{
return process(value);
}
}
return "";
}
public Template add(Function<String, String> fn)
{
resourceLoaders.add(fn);
return this;
}
public Template addInclude(Class<?> clazz)
{
return add(f ->
{
InputStream is = clazz.getResourceAsStream(f);
if (is != null)
{
return inputStreamToString(is);
}
return null;
});
}
private static String inputStreamToString(InputStream in)
{
Scanner scanner = new Scanner(in).useDelimiter("\\A");
return scanner.next();
}
}

View File

@@ -1,199 +0,0 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.gpu;
import com.jogamp.nativewindow.AbstractGraphicsConfiguration;
import com.jogamp.nativewindow.NativeWindowFactory;
import com.jogamp.nativewindow.awt.AWTGraphicsConfiguration;
import com.jogamp.nativewindow.awt.JAWTWindow;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLContext;
import com.jogamp.opengl.GLDrawable;
import com.jogamp.opengl.GLDrawableFactory;
import com.jogamp.opengl.GLProfile;
import java.awt.Canvas;
import java.util.function.Function;
import javax.swing.JFrame;
import static net.runelite.client.plugins.gpu.GLUtil.inputStreamToString;
import net.runelite.client.plugins.gpu.template.Template;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class ShaderTest
{
private static final String VERTEX_SHADER = "" +
"void main() {" +
" gl_Position = vec4(1.0, 1.0, 1.0, 1.0);" +
"}";
private GL4 gl;
@Before
public void before()
{
Canvas canvas = new Canvas();
JFrame frame = new JFrame();
frame.setSize(100, 100);
frame.add(canvas);
frame.setVisible(true);
GLProfile glProfile = GLProfile.getMaxFixedFunc(true);
GLCapabilities glCaps = new GLCapabilities(glProfile);
AbstractGraphicsConfiguration config = AWTGraphicsConfiguration.create(canvas.getGraphicsConfiguration(),
glCaps, glCaps);
JAWTWindow jawtWindow = (JAWTWindow) NativeWindowFactory.getNativeWindow(canvas, config);
GLDrawableFactory glDrawableFactory = GLDrawableFactory.getFactory(glProfile);
GLDrawable glDrawable = glDrawableFactory.createGLDrawable(jawtWindow);
glDrawable.setRealized(true);
GLContext glContext = glDrawable.createContext(null);
int res = glContext.makeCurrent();
if (res == GLContext.CONTEXT_NOT_CURRENT)
{
fail("error making context current");
}
gl = glContext.getGL().getGL4();
}
@Test
@Ignore
public void testUnordered() throws ShaderException
{
int glComputeProgram = gl.glCreateProgram();
int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
try
{
Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
Template template = new Template(func);
String source = template.process(func.apply("comp_unordered.glsl"));
int line = 0;
for (String str : source.split("\\n"))
{
System.out.println(++line + " " + str);
}
GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
}
finally
{
gl.glDeleteShader(glComputeShader);
gl.glDeleteProgram(glComputeProgram);
}
}
@Test
@Ignore
public void testSmall() throws ShaderException
{
int glComputeProgram = gl.glCreateProgram();
int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
try
{
Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
Template template = new Template(func);
String source = template.process(func.apply("comp_small.glsl"));
int line = 0;
for (String str : source.split("\\n"))
{
System.out.println(++line + " " + str);
}
GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
}
finally
{
gl.glDeleteShader(glComputeShader);
gl.glDeleteProgram(glComputeProgram);
}
}
@Test
@Ignore
public void testComp() throws ShaderException
{
int glComputeProgram = gl.glCreateProgram();
int glComputeShader = gl.glCreateShader(gl.GL_COMPUTE_SHADER);
try
{
Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
Template template = new Template(func);
String source = template.process(func.apply("comp.glsl"));
int line = 0;
for (String str : source.split("\\n"))
{
System.out.println(++line + " " + str);
}
GLUtil.loadComputeShader(gl, glComputeProgram, glComputeShader, source);
}
finally
{
gl.glDeleteShader(glComputeShader);
gl.glDeleteProgram(glComputeProgram);
}
}
@Test
@Ignore
public void testGeom() throws ShaderException
{
int glComputeProgram = gl.glCreateProgram();
int glVertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER);
int glGeometryShader = gl.glCreateShader(gl.GL_GEOMETRY_SHADER);
int glFragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
try
{
Function<String, String> func = (s) -> inputStreamToString(getClass().getResourceAsStream(s));
Template template = new Template(func);
String source = template.process(func.apply("geom.glsl"));
int line = 0;
for (String str : source.split("\\n"))
{
System.out.println(++line + " " + str);
}
GLUtil.loadShaders(gl, glComputeProgram, glVertexShader, glGeometryShader, glFragmentShader, VERTEX_SHADER, source, "");
}
finally
{
gl.glDeleteShader(glVertexShader);
gl.glDeleteShader(glGeometryShader);
gl.glDeleteShader(glFragmentShader);
gl.glDeleteProgram(glComputeProgram);
}
}
}

View File

@@ -58,7 +58,9 @@ public class TemplateTest
throw new RuntimeException("unknown resource");
}
};
String out = new Template(func).process(FILE1);
String out = new Template()
.add(func)
.process(FILE1);
assertEquals(RESULT, out);
}
}