001: /*
002: * $RCSfile: ShaderRetained.java,v $
003: *
004: * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.7 $
028: * $Date: 2008/02/28 20:17:30 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.*;
035:
036: /**
037: * The ShaderRetained object is the abstract base class for programmable
038: * shader code. Currently, only text-based source code shaders are
039: * supported, so the only subclass of Shader is SourceCodeShader. We
040: * leave open the possibility for binary (object code) shaders in the
041: * future.
042: */
043: abstract class ShaderRetained extends NodeComponentRetained {
044: int shadingLanguage;
045: int shaderType;
046:
047: // Each element in the array corresponds to a unique renderer if shared
048: // context or a unique canvas otherwise.
049: // shaderId use by native code. One per Canvas.
050: ShaderData[] shaderData;
051:
052: // Flag indicating whether a COMPILE_ERROR has occurred for this shader
053: // object. It is set in updateNative to indicate that the compileShader
054: // operation failed. It is cleared in setLive or clearLive.
055: // TODO KCR: Add code to clear this in setLive or clearLive
056: boolean compileErrorOccurred = false;
057:
058: // need to synchronize access from multiple rendering threads
059: Object resourceLock = new Object();
060:
061: void initializeShader(int shadingLanguage, int shaderType) {
062: this .shadingLanguage = shadingLanguage;
063: this .shaderType = shaderType;
064: }
065:
066: int getShadingLanguage() {
067: return shadingLanguage;
068: }
069:
070: int getShaderType() {
071: return shaderType;
072: }
073:
074: void setLive(boolean inBackgroundGroup, int refCount) {
075: // System.err.println("SourceCodeShaderRetained.setLive()");
076: super .setLive(inBackgroundGroup, refCount);
077: }
078:
079: void clearLive(int refCount) {
080: // System.err.println("SourceCodeShaderRetained.clearLive()");
081: super .clearLive(refCount);
082: }
083:
084: /**
085: * Shader object doesn't really have mirror object.
086: * But it's using the updateMirrorObject interface to propagate
087: * the changes to the users
088: */
089: synchronized void updateMirrorObject(int component, Object value) {
090: System.err
091: .println("Shader.updateMirrorObject not implemented yet!");
092: }
093:
094: void handleFrequencyChange(int bit) {
095: System.err
096: .println("Shader.handleFrequencyChange not implemented yet!");
097: }
098:
099: void createShaderData(int cvRdrIndex, long ctxTimeStamp) {
100: // Create shaderProgram resources if it has not been done.
101: synchronized (resourceLock) {
102: if (shaderData == null) {
103: shaderData = new ShaderData[cvRdrIndex + 1];
104: } else if (shaderData.length <= cvRdrIndex) {
105: ShaderData[] tempSData = new ShaderData[cvRdrIndex + 1];
106:
107: System.arraycopy(shaderData, 0, tempSData, 0,
108: shaderData.length);
109: shaderData = tempSData;
110: }
111:
112: if (shaderData[cvRdrIndex] == null) {
113: shaderData[cvRdrIndex] = new ShaderData();
114: } else if (shaderData[cvRdrIndex].getCtxTimeStamp() != ctxTimeStamp) {
115: // Issue 378 - reset the shader data for this canvas / renderer
116: // if the context has been recreated
117: shaderData[cvRdrIndex].reset();
118: }
119: shaderData[cvRdrIndex].setCtxTimeStamp(ctxTimeStamp);
120: }
121: }
122:
123: // Per-context (canvas) data for this shader
124: class ShaderData extends Object {
125:
126: // Issue 378 - time stamp of context creation for this canvas
127: private long ctxTimeStamp;
128:
129: // shaderId use by native code
130: private ShaderId shaderId = null;
131:
132: // indicated that the shader has been compiled for this canvas
133: private boolean compiled = false;
134:
135: /** ShaderProgramData Constructor */
136: ShaderData() {
137: }
138:
139: void reset() {
140: ctxTimeStamp = 0L;
141: shaderId = null;
142: compiled = false;
143: }
144:
145: long getCtxTimeStamp() {
146: return ctxTimeStamp;
147: }
148:
149: void setCtxTimeStamp(long ctxTimeStamp) {
150: this .ctxTimeStamp = ctxTimeStamp;
151: }
152:
153: ShaderId getShaderId() {
154: return shaderId;
155: }
156:
157: void setShaderId(ShaderId shaderId) {
158: this .shaderId = shaderId;
159: }
160:
161: boolean isCompiled() {
162: return compiled;
163: }
164:
165: void setCompiled(boolean compiled) {
166: this.compiled = compiled;
167: }
168:
169: }
170:
171: }
|