Skip to content

Commit f1852b1

Browse files
committed
Add a Shader class.
1 parent 9b120dc commit f1852b1

3 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import org.lwjgl.LWJGLException;
7+
import org.lwjgl.opengl.GL20;
8+
9+
/**
10+
* An OpenGL fragment shader.
11+
*/
12+
public class FragmentShader extends Shader {
13+
14+
/**
15+
* Create and compile a new shader from a string.
16+
* @param source the source string
17+
* @throws LWJGLException on compilation error
18+
*/
19+
public FragmentShader(final String source) throws LWJGLException {
20+
super(GL20.GL_FRAGMENT_SHADER, source);
21+
}
22+
23+
/**
24+
* Create and compile a new shader from a string.
25+
* @param file the source file
26+
* @throws LWJGLException on compilation error
27+
* @throws IOException if the source could not be read
28+
*/
29+
public FragmentShader(final File file) throws IOException, LWJGLException {
30+
super(GL20.GL_FRAGMENT_SHADER, file);
31+
}
32+
33+
/**
34+
* Create and compile a new shader from a string.
35+
* @param url the source resource
36+
* @throws LWJGLException on compilation error
37+
* @throws IOException if the source could not be read
38+
*/
39+
public FragmentShader(final URL url) throws IOException, LWJGLException {
40+
super(GL20.GL_FRAGMENT_SHADER, url);
41+
}
42+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import org.lwjgl.LWJGLException;
7+
import org.lwjgl.opengl.GL20;
8+
9+
/**
10+
* An OpenGL vertex shader.
11+
*/
12+
public class VertexShader extends Shader {
13+
14+
/**
15+
* Create and compile a new shader from a string.
16+
* @param source the source string
17+
* @throws LWJGLException on compilation error
18+
*/
19+
public VertexShader(final String source) throws LWJGLException {
20+
super(GL20.GL_VERTEX_SHADER, source);
21+
}
22+
23+
/**
24+
* Create and compile a new shader from a string.
25+
* @param file the source file
26+
* @throws LWJGLException on compilation error
27+
* @throws IOException if the source could not be read
28+
*/
29+
public VertexShader(final File file) throws IOException, LWJGLException {
30+
super(GL20.GL_VERTEX_SHADER, file);
31+
}
32+
33+
/**
34+
* Create and compile a new shader from a string.
35+
* @param url the source resource
36+
* @throws LWJGLException on compilation error
37+
* @throws IOException if the source could not be read
38+
*/
39+
public VertexShader(final URL url) throws IOException, LWJGLException {
40+
super(GL20.GL_VERTEX_SHADER, url);
41+
}
42+
}

0 commit comments

Comments
 (0)