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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Copyright 2004-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
import java.util.ArrayList;
/**
* The Appearance object defines all rendering state that can be set
* as a component object of a Shape3D node.
*/
class ShaderAppearanceRetained extends AppearanceRetained {
// Issue 485 - these values must start after the last value in Appearance
static final int SHADER_PROGRAM = 0x0800;
static final int SHADER_ATTRIBUTE_SET = 0x1000;
//
// State variables: these should all be initialized to approproate
// Java 3D defaults.
//
protected ShaderProgramRetained shaderProgram = null;
protected ShaderAttributeSetRetained shaderAttributeSet = null;
protected boolean isMirror = false; // For Debugging.
/**
* Set the shader program object to the specified object.
* @param shaderProgram object that specifies the desired shader program
* and shader program attributes.
*/
void setShaderProgram(ShaderProgram sp) {
synchronized(liveStateLock) {
if (source.isLive()) {
// System.err.println("**** ShaderAppearceRetained.setShaderProgram()");
if (this.shaderProgram != null) {
this.shaderProgram.clearLive(refCount);
this.shaderProgram.removeMirrorUsers(this);
}
if (sp != null) {
((ShaderProgramRetained)sp.retained).setLive(inBackgroundGroup,
refCount);
((ShaderProgramRetained)sp.retained).copyMirrorUsers(this);
}
sendMessage(SHADER_PROGRAM,
(sp != null ? ((ShaderProgramRetained)sp.retained).mirror : null));
}
if (sp == null) {
this.shaderProgram = null;
} else {
this.shaderProgram = (ShaderProgramRetained)sp.retained;
}
}
}
/**
* Retrieves the current shader program object.
* @return current shader program object
*/
ShaderProgram getShaderProgram() {
return (shaderProgram == null ? null : (ShaderProgram)shaderProgram.source);
}
/**
* Sets the ShaderAttributeSet object to the specified object. Setting it to
* null is equivalent to specifying an empty set of attributes.
*
* @param shaderAttributeSet object that specifies the desired shader attributes
*/
void setShaderAttributeSet(ShaderAttributeSet sas) {
synchronized(liveStateLock) {
if (source.isLive()) {
// System.err.println("**** ShaderAppearceRetained.setShaderAttributeSet()");
if (this.shaderAttributeSet != null) {
this.shaderAttributeSet.clearLive(refCount);
this.shaderAttributeSet.removeMirrorUsers(this);
}
if (sas != null) {
((ShaderAttributeSetRetained)sas.retained).setLive(inBackgroundGroup,
refCount);
((ShaderAttributeSetRetained)sas.retained).copyMirrorUsers(this);
}
// System.err.println(" -- testing needed!");
sendMessage(SHADER_ATTRIBUTE_SET,
(sas != null ?
((ShaderAttributeSetRetained)sas.retained).mirror : null));
}
if (sas == null) {
this.shaderAttributeSet = null;
} else {
this.shaderAttributeSet = (ShaderAttributeSetRetained)sas.retained;
}
}
}
/**
* Retrieves the current ShaderAttributeSet object.
* @return current ShaderAttributeSet object
*/
ShaderAttributeSet getShaderAttributeSet() {
return (shaderAttributeSet == null ? null : (ShaderAttributeSet)shaderAttributeSet.source);
}
@Override
public boolean equals(Object obj) {
return ((obj instanceof ShaderAppearanceRetained) &&
equals((ShaderAppearanceRetained) obj));
}
boolean equals(ShaderAppearanceRetained sApp) {
boolean flag;
flag = (sApp == this);
// If the reference is the same, we can stop check.
if(flag)
return flag;
// Check each member's reference for equal.
flag = ((sApp != null) &&
(shaderProgram == sApp.shaderProgram) &&
(shaderAttributeSet == sApp.shaderAttributeSet));
if (!flag)
return flag;
return super.equals(sApp);
}
@Override
synchronized void createMirrorObject() {
// System.err.println("ShaderAppearanceRetained : createMirrorObject()");
if (mirror == null) {
// we can't check isStatic() since it sub-NodeComponent
// create a new one, we should create a
// new AppearanceRetained() even though isStatic() = true.
// For simplicity, always create a retained side.
mirror = new ShaderAppearanceRetained();
((ShaderAppearanceRetained)mirror).isMirror = true; // For Debugging.
}
initMirrorObject();
}
/**
* This routine updates the mirror appearance for this appearance.
* It also calls the update method for each node component if it
* is not null.
*/
@Override
synchronized void initMirrorObject() {
// System.err.println("ShaderAppearanceRetained : initMirrorObject()");
super.initMirrorObject();
ShaderAppearanceRetained mirrorApp = (ShaderAppearanceRetained)mirror;
if(shaderProgram != null) {
mirrorApp.shaderProgram = (ShaderProgramRetained)shaderProgram.mirror;
}
else {
mirrorApp.shaderProgram = null;
}
if(shaderAttributeSet != null) {
mirrorApp.shaderAttributeSet =
(ShaderAttributeSetRetained)shaderAttributeSet.mirror;
}
else {
// System.err.println("shaderAttributeSet is null");
mirrorApp.shaderAttributeSet = null;
}
}
/**
* Update the "component" field of the mirror object with the
* given "value"
*/
@Override
synchronized void updateMirrorObject(int component, Object value) {
// System.err.println("ShaderAppearanceRetained : updateMirrorObject(): " +
// "this = " + this + " component = " + component + " value = " + value);
super.updateMirrorObject(component, value);
ShaderAppearanceRetained mirrorApp = (ShaderAppearanceRetained)mirror;
if ((component & SHADER_PROGRAM) != 0) {
mirrorApp.shaderProgram = (ShaderProgramRetained)value;
}
else if ((component & SHADER_ATTRIBUTE_SET) != 0) {
mirrorApp.shaderAttributeSet = (ShaderAttributeSetRetained)value;
}
}
/**
* This method calls the setLive method of all appearance bundle
* objects.
*/
@Override
void doSetLive(boolean backgroundGroup, int refCount) {
// System.err.println("ShaderAppearceRetained.doSetLive()");
if (shaderProgram != null) {
shaderProgram.setLive(backgroundGroup, refCount);
}
if (shaderAttributeSet != null) {
shaderAttributeSet.setLive(backgroundGroup, refCount);
}
// Increment the reference count and initialize the appearance
// mirror object
super.doSetLive(backgroundGroup, refCount);
}
/**
* This clearLive routine first calls the superclass's method, then
* it removes itself to the list of lights
*/
@Override
void clearLive(int refCount) {
super.clearLive(refCount);
if (shaderProgram != null) {
shaderProgram.clearLive(refCount);
}
if (shaderAttributeSet != null) {
shaderAttributeSet.clearLive(refCount);
}
}
@Override
synchronized void addAMirrorUser(Shape3DRetained shape) {
super.addAMirrorUser(shape);
if (shaderProgram != null)
shaderProgram.addAMirrorUser(shape);
if (shaderAttributeSet != null)
shaderAttributeSet.addAMirrorUser(shape);
}
@Override
synchronized void removeAMirrorUser(Shape3DRetained shape) {
super.removeAMirrorUser(shape);
if (shaderProgram != null)
shaderProgram.removeAMirrorUser(shape);
if (shaderAttributeSet != null)
shaderAttributeSet.removeAMirrorUser(shape);
}
final void sendMessage(int attrMask, Object attr) {
ArrayList<VirtualUniverse> univList = new ArrayList<VirtualUniverse>();
ArrayList<ArrayList<GeometryAtom>> gaList = Shape3DRetained.getGeomAtomsList(mirror.users, univList);
// Send to rendering attribute structure, regardless of
// whether there are users or not (alternate appearance case ..)
J3dMessage createMessage = new J3dMessage();
createMessage.threads = J3dThread.UPDATE_RENDERING_ATTRIBUTES;
createMessage.type = J3dMessage.SHADER_APPEARANCE_CHANGED;
createMessage.universe = null;
createMessage.args[0] = this;
createMessage.args[1]= new Integer(attrMask);
createMessage.args[2] = attr;
createMessage.args[3] = new Integer(changedFrequent);
VirtualUniverse.mc.processMessage(createMessage);
//System.err.println("univList.size is " + univList.size());
for(int i=0; i<univList.size(); i++) {
createMessage = new J3dMessage();
createMessage.threads = J3dThread.UPDATE_RENDER;
createMessage.type = J3dMessage.SHADER_APPEARANCE_CHANGED;
createMessage.universe = univList.get(i);
createMessage.args[0] = this;
createMessage.args[1]= new Integer(attrMask);
createMessage.args[2] = attr;
ArrayList<GeometryAtom> gL = gaList.get(i);
GeometryAtom[] gaArr = new GeometryAtom[gL.size()];
gL.toArray(gaArr);
createMessage.args[3] = gaArr;
VirtualUniverse.mc.processMessage(createMessage);
}
}
@Override
boolean isStatic() {
if (!super.isStatic()) {
return false;
}
boolean flag =
source.capabilityBitsEmpty() &&
((shaderProgram == null) ||
shaderProgram.source.capabilityBitsEmpty()) &&
((shaderAttributeSet == null) ||
shaderAttributeSet.source.capabilityBitsEmpty());
return flag;
}
// Issue 209 - implement the compile method
// Simply pass along to the NodeComponents
@Override
void compile(CompileState compState) {
super.compile(compState);
if (shaderProgram != null) {
shaderProgram.compile(compState);
}
if (shaderAttributeSet != null) {
shaderAttributeSet.compile(compState);
}
}
@Override
boolean isOpaque(int geoType) {
if (!super.isOpaque(geoType)) {
return false;
}
// TODO: IMPLEMENT THIS
// TODO: How do we determine whether a ShaderAppearance is opaque???
return true;
}
@Override
void handleFrequencyChange(int bit) {
// System.err.println("ShaderAppearanceRetained : handleFrequencyChange()");
super.handleFrequencyChange(bit);
int mask = 0;
if (bit == ShaderAppearance.ALLOW_SHADER_PROGRAM_WRITE)
mask = SHADER_PROGRAM;
else if (bit == ShaderAppearance.ALLOW_SHADER_ATTRIBUTE_SET_WRITE)
mask = SHADER_ATTRIBUTE_SET;
if (mask != 0)
setFrequencyChangeMask(bit, mask);
}
}
|