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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package demos.apx;
import java.io.UnsupportedEncodingException;
import java.nio.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
public class Shader {
private int program;
private int vertexShader;
private int fragmentShader;
private Shader() {
}
public static Shader createBinaryProgram(byte[] vertexShaderCode, int vertexShaderFormat,
byte[] fragmentShaderCode, int fragmentShaderFormat) throws GLException {
Shader shader = new Shader();
shader.createBinaryProgramImpl(vertexShaderCode, vertexShaderFormat,
fragmentShaderCode, fragmentShaderFormat);
shader.useProgram();
return shader;
}
public void setAttribByName(String name, int size, int type, boolean normalized, int stride, Buffer pointer) {
GL gl = GLU.getCurrentGL();
int index = gl.glGetAttribLocation(program, name);
gl.glVertexAttribPointer(index, size, type, normalized, stride, pointer);
gl.glEnableVertexAttribArray(index);
// FIXME
// trackAttribLocation(index);
}
public int getUniformLocation(String name) {
GL gl = GLU.getCurrentGL();
return gl.glGetUniformLocation(program, name);
}
public void useProgram() {
GL gl = GLU.getCurrentGL();
gl.glUseProgram(program);
}
//----------------------------------------------------------------------
// Internals only below this point
//
private void createBinaryProgramImpl(byte[] vertexShaderCode, int vertexShaderFormat,
byte[] fragmentShaderCode, int fragmentShaderFormat) throws GLException {
allocProgram();
int[] numBinaryFormats = new int[1];
GL gl = GLU.getCurrentGL();
gl.glGetIntegerv(GL.GL_NUM_SHADER_BINARY_FORMATS, numBinaryFormats, 0);
if (numBinaryFormats[0] > 0) {
int[] binaryFormats = new int[numBinaryFormats[0]];
gl.glGetIntegerv(GL.GL_SHADER_BINARY_FORMATS, binaryFormats, 0);
boolean gotVertexFormat = false;
boolean gotFragmentFormat = false;
for (int i = 0; i < binaryFormats.length && (!gotVertexFormat || !gotFragmentFormat); i++) {
if (!gotVertexFormat) {
gotVertexFormat = (binaryFormats[i] == vertexShaderFormat);
}
if (!gotFragmentFormat) {
gotFragmentFormat = (binaryFormats[i] == fragmentShaderFormat);
}
}
if (!gotVertexFormat) {
throw new RuntimeException("Binary vertex program format 0x" + Integer.toHexString(vertexShaderFormat) +
" not available");
}
if (!gotFragmentFormat) {
throw new RuntimeException("Binary fragment program format 0x" + Integer.toHexString(fragmentShaderFormat) +
" not available");
}
}
// Set up the shaders
setupBinaryShader(vertexShader, vertexShaderCode, vertexShaderFormat);
setupBinaryShader(fragmentShader, fragmentShaderCode, fragmentShaderFormat);
// Set up the shader program
gl.glLinkProgram(program);
if (!glslLog(program, GL.GL_LINK_STATUS, "link")) {
throw new GLException("Error linking program");
}
}
private void allocProgram() {
GL gl = GLU.getCurrentGL();
vertexShader = gl.glCreateShader(GL.GL_VERTEX_SHADER);
fragmentShader = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
program = gl.glCreateProgram();
gl.glAttachShader(program, vertexShader);
gl.glAttachShader(program, fragmentShader);
}
private void setupBinaryShader(int shader,
byte[] shaderData,
int binaryFormat) {
ByteBuffer buf = ByteBuffer.wrap(shaderData);
int[] tmp = new int[1];
tmp[0] = shader;
GL gl = GLU.getCurrentGL();
gl.glShaderBinary(1, tmp, 0, binaryFormat, buf, shaderData.length);
}
private boolean glslLog(int obj, int checkCompile, String op) {
boolean success = false;
GL gl = GLU.getCurrentGL();
// log output.
String str = null;
if (checkCompile == GL.GL_COMPILE_STATUS) {
int[] len = new int[1];
gl.glGetShaderiv(obj, GL.GL_INFO_LOG_LENGTH, len, 0);
if (len[0] > 0) {
byte[] buf = new byte[len[0]];
gl.glGetShaderInfoLog(obj, len[0], null, 0, buf, 0);
try {
str = new String(buf, 0, buf.length, "US-ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} else {
// LINK or VALIDATE
int[] len = new int[1];
gl.glGetProgramiv(obj, GL.GL_INFO_LOG_LENGTH, len, 0);
if (len[0] > 0) {
byte[] buf = new byte[len[0]];
gl.glGetProgramInfoLog(obj, len[0], null, 0, buf, 0);
try {
str = new String(buf, 0, buf.length, "US-ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
if (str != null) {
System.out.println("--- ");
System.out.println(op);
System.out.println(" log ---");
System.out.println(str);
}
// check the compile / link status.
if (checkCompile == GL.GL_COMPILE_STATUS) {
int[] status = new int[1];
gl.glGetShaderiv(obj, checkCompile, status, 0);
success = (status[0] != 0);
if (!success) {
int[] len = new int[1];
gl.glGetShaderiv(obj, GL.GL_SHADER_SOURCE_LENGTH, len, 0);
if (len[0] > 0) {
byte[] buf = new byte[len[0]];
gl.glGetShaderSource(obj, len[0], null, 0, buf, 0);
try {
str = new String(buf, 0, buf.length, "US-ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (str != null) {
System.out.println("--- ");
System.out.println(op);
System.out.println(" code ---");
System.out.println(str);
}
}
}
} else { // LINK or VALIDATE
int[] status = new int[1];
gl.glGetProgramiv(obj, checkCompile, status, 0);
success = (status[0] != 0);
}
if (!success) {
System.out.println("--- ");
System.out.println(op);
System.out.println(" failed");
}
return success;
}
}
|