-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgramFactory.java
140 lines (123 loc) · 3.89 KB
/
ShaderProgramFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package de.damios.guacamole.gdx.graphics;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.GdxRuntimeException;
/**
* A factory for {@link ShaderProgram}s, which supports some convenience
* options.
*
* @author damios
*
*/
public final class ShaderProgramFactory {
private ShaderProgramFactory() {
throw new UnsupportedOperationException();
}
/**
* Creates a {@link ShaderProgram}.
*
* @param vertexShader
* the vertex shader code
* @param fragmentShader
* the fragment shader code
* @param throwException
* whether to throw an exception when the shader couldn't be
* compiled
* ({@link ShaderPreconditions#checkCompilation(ShaderProgram)})
* @param ignorePrepend
* whether to ignore the code in
* {@link ShaderProgram#prependFragmentCode} and
* {@link ShaderProgram#prependVertexCode}; is useful to prevent
* the version being set twice
* @return the shader program
*/
public static ShaderProgram fromString(String vertexShader,
String fragmentShader, boolean throwException,
boolean ignorePrepend) {
String prependVertexCode = null, prependFragmentCode = null;
if (ignorePrepend) {
prependVertexCode = ShaderProgram.prependVertexCode;
ShaderProgram.prependVertexCode = null;
prependFragmentCode = ShaderProgram.prependFragmentCode;
ShaderProgram.prependFragmentCode = null;
}
ShaderProgram program = new ShaderProgram(vertexShader, fragmentShader);
if (ignorePrepend) {
ShaderProgram.prependVertexCode = prependVertexCode;
ShaderProgram.prependFragmentCode = prependFragmentCode;
}
if (throwException)
ShaderPreconditions.checkCompilation(program);
return program;
}
/**
* @param vertexShader
* @param fragmentShader
* @param throwException
* whether to throw an exception when the shader couldn't be
* compiled
* @return the shader program
*/
public static ShaderProgram fromString(String vertexShader,
String fragmentShader, boolean throwException) {
return fromString(vertexShader, fragmentShader, throwException, false);
}
/**
* Creates a ShaderProgram and automatically throws an
* {@link GdxRuntimeException} when it couldn't be compiled.
*
* @param vertexShader
* @param fragmentShader
* @return the shader program
*/
public static ShaderProgram fromString(String vertexShader,
String fragmentShader) {
return fromString(vertexShader, fragmentShader, true);
}
/**
* Creates a ShaderProgram and automatically throws an
* {@link GdxRuntimeException} when it couldn't be compiled.
*
* @param vertexShader
* @param fragmentShader
* @return the shader program
*/
public static ShaderProgram fromFile(FileHandle vertexShader,
FileHandle fragmentShader) {
return fromString(vertexShader.readString(),
fragmentShader.readString());
}
/**
* A simple preconditions class used to check whether a
* {@link ShaderProgram} was properly compiled.
*
* @author damios
*/
public static final class ShaderPreconditions {
private ShaderPreconditions() {
throw new UnsupportedOperationException();
}
/**
* Throws a {@link GdxRuntimeException} when the program was not
* compiled. The compilation log is appended to {@code msg}.
*
* @param program
* @param msg
* the exception's message
*/
public static void checkCompilation(ShaderProgram program, String msg) {
if (!program.isCompiled())
throw new GdxRuntimeException(msg + program.getLog());
}
/**
* Throws a {@link GdxRuntimeException} when the program was not
* compiled. The compilation log is printed as part of the exception's
* message.
*
* @param program
*/
public static void checkCompilation(ShaderProgram program) {
checkCompilation(program, "");
}
}
}