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
190
191
192
193
194
195
196
197
|
package com.sun.opengl.util.glsl;
import javax.media.opengl.*;
import java.util.HashMap;
import java.util.Iterator;
import java.nio.*;
import java.io.PrintStream;
public class ShaderProgram {
public ShaderProgram() {
id = getNextID();
}
public boolean linked() {
return programLinked;
}
public boolean inUse() {
return programInUse;
}
public int program() { return shaderProgram; }
/**
* returns the uniq shader id as an integer
* @see #key()
*/
public int id() { return id.intValue(); }
/**
* returns the uniq shader id as an Integer
*
* @see #id()
*/
public Integer key() { return id; }
/**
* @see #glReleaseAllVertexAttributes
*/
public synchronized void release(GL2ES2 gl) {
release(gl, false);
}
/**
* @see #glReleaseAllVertexAttributes
*/
public synchronized void release(GL2ES2 gl, boolean releaseShaderToo) {
glUseProgram(gl, false);
for(Iterator iter=shaderMap.values().iterator(); iter.hasNext(); ) {
ShaderCode shaderCode = (ShaderCode) iter.next();
gl.glDetachShader(shaderProgram, shaderCode.shader());
if(releaseShaderToo) {
shaderCode.release(gl);
}
}
shaderMap.clear();
gl.glDeleteProgram(shaderProgram);
shaderProgram=-1;
}
//
// ShaderCode handling
//
/**
* Adds a new shader to a this non running program.
*
* @return false if the program is in use, or the shader already exist,
* otherwise true.
*/
public synchronized boolean add(ShaderCode shaderCode) {
if(shaderMap.containsKey(shaderCode.key())) return false;
shaderMap.put(shaderCode.key(), shaderCode);
return true;
}
public synchronized ShaderCode getShader(int id) {
return (ShaderCode) shaderMap.get(new Integer(id));
}
//
// Program handling
//
/**
* Replace a shader in a 'running' program.
* Refetches all previously bin/get attribute names
* and resets all attribute data as well
*
* @see getAttribLocation
* @param gl
* @param oldShaderID the to be replace Shader
* @param newShader the new ShaderCode
* @param verboseOut the optional verbose outputstream
* @throws GLException is the program is not linked
*
* @see #glRefetchAttribLocations
* @see #glResetAllVertexAttributes
* @see #glReplaceShader
*/
public synchronized boolean glReplaceShader(GL2ES2 gl, int oldShaderID, ShaderCode newShader, PrintStream verboseOut) {
if(!programLinked) throw new GLException("Program is not linked");
boolean shaderWasInUse = programInUse;
glUseProgram(gl, false);
if(!newShader.compile(gl, verboseOut)) {
return false;
}
if(oldShaderID>=0) {
ShaderCode oldShader = (ShaderCode) shaderMap.remove(new Integer(oldShaderID));
if(null!=oldShader) {
gl.glDetachShader(shaderProgram, oldShader.shader());
}
}
add(newShader);
gl.glAttachShader(shaderProgram, newShader.shader());
gl.glLinkProgram(shaderProgram);
if ( ! gl.glIsProgramValid(shaderProgram, System.err) ) {
return false;
}
if(shaderWasInUse) {
glUseProgram(gl, true);
}
return true;
}
public synchronized boolean link(GL2ES2 gl, PrintStream verboseOut) {
if(programLinked) throw new GLException("Program is already linked");
if(0>shaderProgram) {
shaderProgram = gl.glCreateProgram();
}
for(Iterator iter=shaderMap.values().iterator(); iter.hasNext(); ) {
ShaderCode shaderCode = (ShaderCode) iter.next();
if(!shaderCode.compile(gl, verboseOut)) {
return false;
}
gl.glAttachShader(shaderProgram, shaderCode.shader());
}
// Link the program
gl.glLinkProgram(shaderProgram);
programLinked = gl.glIsProgramValid(shaderProgram, System.err);
return programLinked;
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(obj instanceof ShaderCode) {
return id()==((ShaderCode)obj).id();
}
return false;
}
public int hashCode() {
return id.intValue();
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("ShaderProgram[id="+id);
buf.append(", linked="+programLinked+", inUse="+programInUse+", program: "+shaderProgram+", [");
for(Iterator iter=shaderMap.values().iterator(); iter.hasNext(); ) {
buf.append((ShaderCode) iter.next());
buf.append(" ");
}
buf.append("]");
return buf.toString();
}
protected synchronized void glUseProgram(GL2ES2 gl, boolean on) {
if(!programLinked) throw new GLException("Program is not linked");
if(programInUse==on) return;
gl.glUseProgram(on?shaderProgram:0);
programInUse = on;
//Throwable tX = new Throwable("Info: ShaderProgram.glUseProgram: "+on);
//tX.printStackTrace();
}
protected boolean programLinked = false;
protected boolean programInUse = false;
protected int shaderProgram=-1;
protected HashMap shaderMap = new HashMap();
protected Integer id = null;
private static synchronized Integer getNextID() {
return new Integer(nextID++);
}
protected static int nextID = 1;
}
|