|
| 1 | +package com.nullprogram.lwjgl; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.net.URL; |
| 9 | +import lombok.EqualsAndHashCode; |
| 10 | +import lombok.Getter; |
| 11 | +import org.lwjgl.LWJGLException; |
| 12 | +import org.lwjgl.opengl.GL20; |
| 13 | + |
| 14 | +/** |
| 15 | + * An OpenGL shader. Call dispose() on the shader to delete the shader |
| 16 | + * and free up GPU resources. |
| 17 | + */ |
| 18 | +@EqualsAndHashCode(of = "handle") |
| 19 | +public abstract class Shader { |
| 20 | + |
| 21 | + /** Byte array read buffer size. */ |
| 22 | + private static final int BUFFER_SIZE = 1024; |
| 23 | + |
| 24 | + /** The handle for this shader. */ |
| 25 | + @Getter |
| 26 | + private final int handle; |
| 27 | + |
| 28 | + /** True if this shader has been deleted already. */ |
| 29 | + private boolean disposed = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * Create and compile a new shader from a string. |
| 33 | + * @param type the type of this shader |
| 34 | + * @param source the source string |
| 35 | + * @throws LWJGLException on compilation error |
| 36 | + */ |
| 37 | + public Shader(final int type, final String source) throws LWJGLException { |
| 38 | + handle = compile(type, source); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create and compile a new shader from a string. |
| 43 | + * @param type the type of this shader |
| 44 | + * @param file the source file |
| 45 | + * @throws LWJGLException on compilation error |
| 46 | + * @throws IOException if the source could not be read |
| 47 | + */ |
| 48 | + public Shader(final int type, final File file) |
| 49 | + throws IOException, LWJGLException { |
| 50 | + |
| 51 | + this(type, fetch(new FileInputStream(file))); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create and compile a new shader from a string. |
| 56 | + * @param type the type of this shader |
| 57 | + * @param resource the source resource |
| 58 | + * @throws LWJGLException on compilation error |
| 59 | + * @throws IOException if the source could not be read |
| 60 | + */ |
| 61 | + public Shader(final int type, final URL resource) |
| 62 | + throws IOException, LWJGLException { |
| 63 | + |
| 64 | + this(type, fetch(resource.openStream())); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Disposes of this shader and the system resources it is using. |
| 69 | + */ |
| 70 | + public void dispose() { |
| 71 | + if (!disposed) { |
| 72 | + GL20.glDeleteShader(handle); |
| 73 | + disposed = true; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void finalize() { |
| 79 | + dispose(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Compile the shader code and return the handle. |
| 84 | + * @param type the type of this shader |
| 85 | + * @param source the shader source code |
| 86 | + * @return the shader's handle |
| 87 | + * @throws LWJGLException when compilation fails |
| 88 | + */ |
| 89 | + private static int compile(final int type, final String source) |
| 90 | + throws LWJGLException { |
| 91 | + |
| 92 | + int shader = GL20.glCreateShader(type); |
| 93 | + GL20.glShaderSource(shader, source); |
| 94 | + GL20.glCompileShader(shader); |
| 95 | + if (GL20.glGetShader(shader, GL20.GL_COMPILE_STATUS) == 0) { |
| 96 | + /* Compile failed. */ |
| 97 | + int len = GL20.glGetShader(shader, GL20.GL_INFO_LOG_LENGTH); |
| 98 | + String info = GL20.glGetShaderInfoLog(shader, len); |
| 99 | + GL20.glDeleteShader(shader); |
| 100 | + throw new LWJGLException(info); |
| 101 | + } |
| 102 | + return shader; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Read the given input into a byte array. |
| 107 | + * @param in the input stream to be read |
| 108 | + * @return the byte array containing the resource |
| 109 | + * @throws java.io.IOException on IO error |
| 110 | + */ |
| 111 | + private static String fetch(final InputStream in) |
| 112 | + throws IOException { |
| 113 | + |
| 114 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 115 | + byte[] buffer = new byte[BUFFER_SIZE]; |
| 116 | + int n; |
| 117 | + while ((n = in.read(buffer)) > 0) { |
| 118 | + out.write(buffer, 0, n); |
| 119 | + } |
| 120 | + out.close(); |
| 121 | + return new String(out.toByteArray()); |
| 122 | + } |
| 123 | +} |
0 commit comments