aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/TextureUnitStateRetained.java
blob: c3b40c604c305a4029d23efb5071522f0386dc10 (plain)
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 * Copyright 1999-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;

class TextureUnitStateRetained extends NodeComponentRetained {

    static final int TEXTURE_CHANGED		= 0x0001;
    static final int TEXTURE_ATTRS_CHANGED 	= 0x0002;
    static final int TEXCOORD_GEN_CHANGED	= 0x0004;
    static final int ALL_STATE_CHANGED		= 0x0008;

    TextureRetained texture = null;
    TextureAttributesRetained texAttrs = null;
    TexCoordGenerationRetained texGen = null;

    /**
     * An abstract method to validate the texture unit state component
     */
    final void setTextureUnitStateComponent(NodeComponent comp,
					    NodeComponentRetained thisComp,
					    int messageOp) {
	if (source.isLive()) {

	    if ((comp == null && thisComp == null) ||
		(comp != null && comp.retained == thisComp))
		return;

	    if (thisComp != null) {
		thisComp.clearLive(refCount);
		thisComp.removeMirrorUsers(this);
	    }

	    if (comp != null) {
		((NodeComponentRetained)comp.retained).setLive(inBackgroundGroup, refCount);
		// If texture unit is live, then copy all the users of this
		// texture unit state as users of this texture component
		((NodeComponentRetained)comp.retained).copyMirrorUsers(this);
	    }

	    if (messageOp != -1) {
	        sendMessage(messageOp,
		    (comp == null ? null :
		        ((NodeComponentRetained)comp.retained).mirror));
	    }

	}
    }

    final void initTextureUnitState(Texture texture,
              TextureAttributes texAttrs,
              TexCoordGeneration texGen) {

	initTexture(texture);
	initTextureAttributes(texAttrs);
	initTexCoordGeneration(texGen);
    }

    final void setTextureUnitState(Texture texture,
              TextureAttributes texAttrs,
              TexCoordGeneration texGen) {

	setTextureUnitStateComponent(texture, this.texture, -1);
	setTextureUnitStateComponent(texAttrs, this.texAttrs, -1);
	setTextureUnitStateComponent(texGen, this.texGen, -1);


	// send all changes to the target threads in one
	// message to avoid modifying the renderBin repeatedly

	Object args[] = new Object[3];
	args[0] = (texture == null ? null :
			((TextureRetained)texture.retained).mirror);
	args[1] = (texAttrs == null ? null :
			((TextureAttributesRetained)texAttrs.retained).mirror);
	args[2] = (texGen == null ? null :
			((TexCoordGenerationRetained)texGen.retained).mirror);

	sendMessage(ALL_STATE_CHANGED, args);

	initTextureUnitState(texture, texAttrs, texGen);
    }

    final void initTexture(Texture texture) {
	if (texture == null)
	    this.texture = null;
	else
	    this.texture = (TextureRetained)texture.retained;
    }

    final void setTexture(Texture texture) {
	setTextureUnitStateComponent(texture, this.texture, TEXTURE_CHANGED);
	initTexture(texture);
    }

    final void initTextureAttributes(TextureAttributes texAttrs) {
	if (texAttrs == null)
	    this.texAttrs = null;
	else
	    this.texAttrs = (TextureAttributesRetained)texAttrs.retained;
    }

    final void setTextureAttributes(TextureAttributes texAttrs) {
	setTextureUnitStateComponent(texAttrs, this.texAttrs,
					TEXTURE_ATTRS_CHANGED);
	initTextureAttributes(texAttrs);
    }

    final void initTexCoordGeneration(TexCoordGeneration texGen) {
	if (texGen == null)
	    this.texGen = null;
	else
	    this.texGen = (TexCoordGenerationRetained)texGen.retained;
    }

    final void setTexCoordGeneration(TexCoordGeneration texGen) {
	setTextureUnitStateComponent(texGen, this.texGen, TEXCOORD_GEN_CHANGED);
	initTexCoordGeneration(texGen);
    }

    Texture getTexture() {
	return (texture == null ? null : (Texture)texture.source);
    }

    TextureAttributes getTextureAttributes() {
	return (texAttrs == null ? null : (TextureAttributes)texAttrs.source);
    }

    TexCoordGeneration getTexCoordGeneration() {
	return (texGen == null ? null : (TexCoordGeneration)texGen.source);
    }

    void updateNative(int unitIndex, Canvas3D cv,
			boolean reload, boolean simulate) {

	//System.err.println("TextureUnitState/updateNative: unitIndex= " + unitIndex + " reload= " + reload + " simulate= " + simulate);

	// unitIndex can be -1 for the single texture case, so
	// can't use unitIndex to index into the cv.texUnitState;
	// in this case, use index 0

	int index = unitIndex;

	if (index < 0)
	    index = 0;


	boolean dirty = ((cv.canvasDirty & (Canvas3D.TEXTUREATTRIBUTES_DIRTY|Canvas3D.TEXTUREBIN_DIRTY)) != 0);

        if (this.texture == null) {
	    // if texture is null, then texture mapped is
	    // disabled for this texture unit; and no more
	    // state update is needed

	    //System.err.println("texture is null");

	    if (cv.texUnitState[index].texture != null) {
	        cv.resetTexture(cv.ctx, unitIndex);
		cv.texUnitState[index].texture = null;
	    }
	    cv.canvasDirty &= ~Canvas3D.TEXTUREATTRIBUTES_DIRTY;
	    return;
        } else {

	    Pipeline.getPipeline().updateTextureUnitState(cv.ctx, unitIndex, true);
        }

        // reload is needed in a multi-texture case to bind the
	// texture parameters to the texture unit state

	if (reload || dirty || cv.texUnitState[index].texture != this.texture) {

	    // texture cannot be null at this point because it is
	    // already checked above
            this.texture.updateNative(cv);

	    cv.texUnitState[index].texture = this.texture;

        }

        if (this.texAttrs == null) {
	    if (reload || dirty || cv.texUnitState[index].texAttrs != null) {
                cv.resetTextureAttributes(cv.ctx);
		cv.setBlendFunc(cv.ctx, TransparencyAttributes.BLEND_ONE,
				TransparencyAttributes.BLEND_ZERO);
	    	cv.texUnitState[index].texAttrs = null;
	    }
        } else {

	    TextureAttributesRetained mTexAttrs;
	    if (this.texAttrs.mirror == null) {
		mTexAttrs = this.texAttrs;
	    } else {
		mTexAttrs = (TextureAttributesRetained) this.texAttrs.mirror;
	    }


	    if (mTexAttrs.mirrorCompDirty) {
		// This happen when canvas reference is same as
		// texUnitState.texAttrs and we update the later without
		// notify cache.
		cv.texUnitState[index].texAttrs = null;
		mTexAttrs.mirrorCompDirty = false;
	    }

	    if (reload || dirty || cv.texUnitState[index].texAttrs != mTexAttrs) {
                this.texAttrs.updateNative(cv, simulate, texture.format);
	    	cv.texUnitState[index].texAttrs = mTexAttrs;
	    }
        }

	if (this.texGen == null) {
	    if (reload || dirty || cv.texUnitState[index].texGen != null) {
		cv.resetTexCoordGeneration(cv.ctx);
		cv.texUnitState[index].texGen = null;
	    }
        } else {
	    TexCoordGenerationRetained mTexGen;

	    if (this.texGen.mirror == null) {
		mTexGen = this.texGen;
	    } else {
		mTexGen = (TexCoordGenerationRetained)this.texGen.mirror;
	    }

	    if (mTexGen.mirrorCompDirty) {
		// This happen when canvas reference is same as
		// texUnitState.texGen and we update the later without
		// notify cache.
		cv.texUnitState[index].texGen = null;
		mTexGen.mirrorCompDirty = false;
	    }

	    if (reload || dirty || cv.texUnitState[index].texGen != mTexGen) {
		this.texGen.updateNative(cv);
		cv.texUnitState[index].texGen = mTexGen;
            }
        }
	cv.canvasDirty &= ~Canvas3D.TEXTUREATTRIBUTES_DIRTY;
    }


   /**
    * Creates and initializes a mirror object, point the mirror object
    * to the retained object if the object is not editable
    */
    @Override
    synchronized void createMirrorObject() {

	if (mirror == null) {
	    TextureUnitStateRetained mirrorTus  =
			new TextureUnitStateRetained();
	    mirror = mirrorTus;
        }
	mirror.source = source;
	initMirrorObject();

    }

    @Override
    synchronized void initMirrorObject() {

	TextureUnitStateRetained mirrorTus =
		(TextureUnitStateRetained)mirror;

	if (this.texture != null)
	    mirrorTus.texture = (TextureRetained)this.texture.mirror;
	else
	    mirrorTus.texture = null;

	if (this.texAttrs != null)
	    mirrorTus.texAttrs =
		(TextureAttributesRetained)this.texAttrs.mirror;
	else
	    mirrorTus.texAttrs = null;

	if (this.texGen != null)
	    mirrorTus.texGen = (TexCoordGenerationRetained)this.texGen.mirror;
	else
	    mirrorTus.texGen = null;
    }


    /** Update the "component" field of the mirror object with the
     *  given "value"
     */
    @Override
    synchronized void updateMirrorObject(int component, Object value) {

	TextureUnitStateRetained mirrorTus = (TextureUnitStateRetained)mirror;

	if ((component & TEXTURE_CHANGED) != 0) {
	    mirrorTus.texture = (TextureRetained)value;
	}
	else if ((component & TEXTURE_ATTRS_CHANGED) != 0) {
	    mirrorTus.texAttrs = (TextureAttributesRetained)value;
	}
	else if ((component & TEXCOORD_GEN_CHANGED) != 0) {
	    mirrorTus.texGen = (TexCoordGenerationRetained)value;
	}
	else if ((component & ALL_STATE_CHANGED) != 0) {
	    Object [] args = (Object []) value;
	    mirrorTus.texture = (TextureRetained)args[0];
	    mirrorTus.texAttrs = (TextureAttributesRetained)args[1];
	    mirrorTus.texGen = (TexCoordGenerationRetained)args[2];
	}
    }


    boolean equivalent(TextureUnitStateRetained tr) {

	if (tr == null) {
	    return (false);

	} else if ((this.changedFrequent != 0) || (tr.changedFrequent != 0)) {
	    return (this.mirror == tr);

	} else {

	    if (this.texture != tr.texture) {
	        return false;
	    }

	    if (this.texAttrs != null &&
		    !this.texAttrs.equivalent(tr.texAttrs)) {
		return false;
	    }

	    if (this.texGen != null &&
		    !this.texGen.equivalent(tr.texGen)) {
		return false;
	    }
	}

	return true;
    }

    @Override
    protected Object clone() {
	TextureUnitStateRetained tr = (TextureUnitStateRetained)super.clone();

	// the cloned object is used for RenderBin only.
	// In most cases, it will duplicate all attributes in the RenderBin
	// so that updating a mirror object in one level will not affect the
	// entire structure of the RenderBin, but will affect only those bins
	// that got affected by the modified mirror object
	if (this.texAttrs != null)
	    tr.texAttrs = (TextureAttributesRetained)this.texAttrs.clone();

	if (this.texGen != null)
	    tr.texGen = (TexCoordGenerationRetained)this.texGen.clone();

	return tr;
    }


    /**
     * set the texture unit state according to the specified texture
     * unit state
     */
    protected void set(TextureUnitStateRetained tr) {
	super.set(tr);
	this.texture = tr.texture;

	if (tr.texAttrs == null) {
	    this.texAttrs = null;
	} else {
	    if (this.texAttrs == null) {
		this.texAttrs = (TextureAttributesRetained)tr.texAttrs.clone();
	    } else {
		this.texAttrs.set(tr.texAttrs);
	    }
	}

	if (tr.texGen == null) {
	    this.texGen = null;
	} else {
	    if (this.texGen == null) {
		this.texGen = (TexCoordGenerationRetained)tr.texGen.clone();
	    } else {
		this.texGen.set(tr.texGen);
	    }
	}
    }

    protected void set(TextureRetained texture,
			TextureAttributesRetained texAttrs,
			TexCoordGenerationRetained texGen) {
	this.texture = texture;
	this.texAttrs = texAttrs;
	this.texGen = texGen;
    }

    @Override
    synchronized void addAMirrorUser(Shape3DRetained shape) {

	super.addAMirrorUser(shape);

        if (texture != null)
            texture.addAMirrorUser(shape);
        if (texAttrs != null)
            texAttrs.addAMirrorUser(shape);
        if (texGen != null)
            texGen.addAMirrorUser(shape);
    }

    @Override
    synchronized void removeAMirrorUser(Shape3DRetained shape) {
	super.removeAMirrorUser(shape);

	if (texture != null)
	    texture.removeAMirrorUser(shape);
	if (texAttrs != null)
	    texAttrs.removeAMirrorUser(shape);
	if (texGen != null)
	    texGen.removeAMirrorUser(shape);
    }

    @Override
    synchronized void removeMirrorUsers(NodeComponentRetained node) {
	super.removeMirrorUsers(node);

	if (texture != null)
	    texture.removeMirrorUsers(node);
	if (texAttrs != null)
	    texAttrs.removeMirrorUsers(node);
	if (texGen != null)
	    texGen.removeMirrorUsers(node);
    }

    @Override
    synchronized void copyMirrorUsers(NodeComponentRetained node) {
	super.copyMirrorUsers(node);

	if (texture != null)
	    texture.copyMirrorUsers(node);
	if (texAttrs != null)
	    texAttrs.copyMirrorUsers(node);
	if (texGen != null)
	    texGen.copyMirrorUsers(node);
    }


    @Override
    void setLive(boolean backgroundGroup, int refCount) {
	if (texture != null)
	    texture.setLive(backgroundGroup, refCount);

	if (texAttrs != null)
	    texAttrs.setLive(backgroundGroup, refCount);

	if (texGen != null)
	    texGen.setLive(backgroundGroup, refCount);

        // Increment the reference count and initialize the textureUnitState
        // mirror object
        super.doSetLive(backgroundGroup, refCount);
	super.markAsLive();

    }


    @Override
    void clearLive(int refCount) {
	super.clearLive(refCount);

	if (texture != null)
	    texture.clearLive(refCount);
	if (texAttrs != null)
	    texAttrs.clearLive(refCount);
	if (texGen != null)
	    texGen.clearLive(refCount);
    }

    @Override
    boolean isStatic() {

	return (source.capabilityBitsEmpty() &&
		((texture == null) || (texture.isStatic())) &&
		((texAttrs == null) || (texAttrs.isStatic())) &&
		((texGen == null) || (texGen.isStatic())));
    }

    // Issue 209 - enable this method (was previously commented out)
    // Simply pass along to the NodeComponent
    @Override
    void compile (CompileState compState) {
	setCompiled();

	if (texture != null)
	    texture.compile(compState);
	if (texAttrs != null)
	    texAttrs.compile(compState);
	if (texGen != null)
	    texGen.compile(compState);
    }

    boolean equals(TextureUnitStateRetained ts) {
        return ((ts == this) ||
		(ts != null) &&
		((texture == ts.texture) ||
		 ((texture != null) && (texture.equals(ts.texture)))) &&
		((texAttrs == ts.texAttrs) ||
		 ((texAttrs != null) && (texAttrs.equals(ts.texAttrs)))) &&
		((texGen == ts.texGen) ||
		 ((texGen != null) && (texGen.equals(ts.texGen)))));
    }


    @Override
    void setInImmCtx(boolean flag) {
        super.setInImmCtx(flag);
	if (texture != null)
	    texture.setInImmCtx(flag);
	if (texAttrs != null)
	    texAttrs.setInImmCtx(flag);
	if (texGen != null)
	    texGen.setInImmCtx(flag);
    }

    @Override
    boolean getInImmCtx() {
        return (super.getInImmCtx() ||
                ((texture != null) && (texture.getInImmCtx())) ||
                ((texAttrs != null) && (texAttrs.getInImmCtx())) ||
                ((texGen != null) && (texGen.getInImmCtx())));
    }


    boolean isLive() {
	return (source.isLive() ||
		((texture != null) && (texture.source.isLive())) ||
		((texAttrs != null) && (texAttrs.source.isLive())) ||
		((texGen != null) && (texGen.source.isLive())));
    }

    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.TEXTURE_UNIT_STATE_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.TEXTURE_UNIT_STATE_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);
	}

    }

    boolean isTextureEnabled() {
	// Check the internal enable , instead of userSpecifiedEnable
	return (texture != null && texture.enable);
    }

    @Override
    void handleFrequencyChange(int bit) {
        switch (bit) {
        case TextureUnitState.ALLOW_STATE_WRITE: {
            setFrequencyChangeMask(bit, bit);
        }
        default:
            break;
        }
    }
}