001: /*
002: * $RCSfile: CgShaderProgramRetained.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.9 $
028: * $Date: 2008/02/28 20:17:20 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: /**
035: * The CgShaderProgram object is a concrete implementation of a
036: * ShaderProgram node component for NVIDIA's Cg shader language.
037: */
038:
039: class CgShaderProgramRetained extends ShaderProgramRetained {
040:
041: /**
042: * Constructs a Cg shader program node component.
043: */
044: CgShaderProgramRetained() {
045: }
046:
047: synchronized void createMirrorObject() {
048: // System.err.println("CgShaderProgramRetained : createMirrorObject");
049: // This method should only call by setLive().
050: if (mirror == null) {
051: CgShaderProgramRetained mirrorCgSP = new CgShaderProgramRetained();
052: mirror = mirrorCgSP;
053: }
054: initMirrorObject();
055: }
056:
057: // ShaderAttributeValue methods
058:
059: ShaderError setUniform1i(Context ctx,
060: ShaderProgramId shaderProgramId,
061: ShaderAttrLoc uniformLocation, int value) {
062:
063: return Pipeline.getPipeline().setCgUniform1i(ctx,
064: shaderProgramId, uniformLocation, value);
065: }
066:
067: ShaderError setUniform1f(Context ctx,
068: ShaderProgramId shaderProgramId,
069: ShaderAttrLoc uniformLocation, float value) {
070:
071: return Pipeline.getPipeline().setCgUniform1f(ctx,
072: shaderProgramId, uniformLocation, value);
073: }
074:
075: ShaderError setUniform2i(Context ctx,
076: ShaderProgramId shaderProgramId,
077: ShaderAttrLoc uniformLocation, int[] value) {
078:
079: return Pipeline.getPipeline().setCgUniform2i(ctx,
080: shaderProgramId, uniformLocation, value);
081: }
082:
083: ShaderError setUniform2f(Context ctx,
084: ShaderProgramId shaderProgramId,
085: ShaderAttrLoc uniformLocation, float[] value) {
086:
087: return Pipeline.getPipeline().setCgUniform2f(ctx,
088: shaderProgramId, uniformLocation, value);
089: }
090:
091: ShaderError setUniform3i(Context ctx,
092: ShaderProgramId shaderProgramId,
093: ShaderAttrLoc uniformLocation, int[] value) {
094:
095: return Pipeline.getPipeline().setCgUniform3i(ctx,
096: shaderProgramId, uniformLocation, value);
097: }
098:
099: ShaderError setUniform3f(Context ctx,
100: ShaderProgramId shaderProgramId,
101: ShaderAttrLoc uniformLocation, float[] value) {
102:
103: return Pipeline.getPipeline().setCgUniform3f(ctx,
104: shaderProgramId, uniformLocation, value);
105: }
106:
107: ShaderError setUniform4i(Context ctx,
108: ShaderProgramId shaderProgramId,
109: ShaderAttrLoc uniformLocation, int[] value) {
110:
111: return Pipeline.getPipeline().setCgUniform4i(ctx,
112: shaderProgramId, uniformLocation, value);
113: }
114:
115: ShaderError setUniform4f(Context ctx,
116: ShaderProgramId shaderProgramId,
117: ShaderAttrLoc uniformLocation, float[] value) {
118:
119: return Pipeline.getPipeline().setCgUniform4f(ctx,
120: shaderProgramId, uniformLocation, value);
121: }
122:
123: ShaderError setUniformMatrix3f(Context ctx,
124: ShaderProgramId shaderProgramId,
125: ShaderAttrLoc uniformLocation, float[] value) {
126:
127: return Pipeline.getPipeline().setCgUniformMatrix3f(ctx,
128: shaderProgramId, uniformLocation, value);
129: }
130:
131: ShaderError setUniformMatrix4f(Context ctx,
132: ShaderProgramId shaderProgramId,
133: ShaderAttrLoc uniformLocation, float[] value) {
134:
135: return Pipeline.getPipeline().setCgUniformMatrix4f(ctx,
136: shaderProgramId, uniformLocation, value);
137: }
138:
139: // ShaderAttributeArray methods
140:
141: ShaderError setUniform1iArray(Context ctx,
142: ShaderProgramId shaderProgramId,
143: ShaderAttrLoc uniformLocation, int numElements, int[] value) {
144:
145: return Pipeline.getPipeline().setCgUniform1iArray(ctx,
146: shaderProgramId, uniformLocation, numElements, value);
147: }
148:
149: ShaderError setUniform1fArray(Context ctx,
150: ShaderProgramId shaderProgramId,
151: ShaderAttrLoc uniformLocation, int numElements,
152: float[] value) {
153:
154: return Pipeline.getPipeline().setCgUniform1fArray(ctx,
155: shaderProgramId, uniformLocation, numElements, value);
156: }
157:
158: ShaderError setUniform2iArray(Context ctx,
159: ShaderProgramId shaderProgramId,
160: ShaderAttrLoc uniformLocation, int numElements, int[] value) {
161:
162: return Pipeline.getPipeline().setCgUniform2iArray(ctx,
163: shaderProgramId, uniformLocation, numElements, value);
164: }
165:
166: ShaderError setUniform2fArray(Context ctx,
167: ShaderProgramId shaderProgramId,
168: ShaderAttrLoc uniformLocation, int numElements,
169: float[] value) {
170:
171: return Pipeline.getPipeline().setCgUniform2fArray(ctx,
172: shaderProgramId, uniformLocation, numElements, value);
173: }
174:
175: ShaderError setUniform3iArray(Context ctx,
176: ShaderProgramId shaderProgramId,
177: ShaderAttrLoc uniformLocation, int numElements, int[] value) {
178:
179: return Pipeline.getPipeline().setCgUniform3iArray(ctx,
180: shaderProgramId, uniformLocation, numElements, value);
181: }
182:
183: ShaderError setUniform3fArray(Context ctx,
184: ShaderProgramId shaderProgramId,
185: ShaderAttrLoc uniformLocation, int numElements,
186: float[] value) {
187:
188: return Pipeline.getPipeline().setCgUniform3fArray(ctx,
189: shaderProgramId, uniformLocation, numElements, value);
190: }
191:
192: ShaderError setUniform4iArray(Context ctx,
193: ShaderProgramId shaderProgramId,
194: ShaderAttrLoc uniformLocation, int numElements, int[] value) {
195:
196: return Pipeline.getPipeline().setCgUniform4iArray(ctx,
197: shaderProgramId, uniformLocation, numElements, value);
198: }
199:
200: ShaderError setUniform4fArray(Context ctx,
201: ShaderProgramId shaderProgramId,
202: ShaderAttrLoc uniformLocation, int numElements,
203: float[] value) {
204:
205: return Pipeline.getPipeline().setCgUniform4fArray(ctx,
206: shaderProgramId, uniformLocation, numElements, value);
207: }
208:
209: ShaderError setUniformMatrix3fArray(Context ctx,
210: ShaderProgramId shaderProgramId,
211: ShaderAttrLoc uniformLocation, int numElements,
212: float[] value) {
213:
214: return Pipeline.getPipeline().setCgUniformMatrix3fArray(ctx,
215: shaderProgramId, uniformLocation, numElements, value);
216: }
217:
218: ShaderError setUniformMatrix4fArray(Context ctx,
219: ShaderProgramId shaderProgramId,
220: ShaderAttrLoc uniformLocation, int numElements,
221: float[] value) {
222:
223: return Pipeline.getPipeline().setCgUniformMatrix4fArray(ctx,
224: shaderProgramId, uniformLocation, numElements, value);
225: }
226:
227: /**
228: * Method to return a flag indicating whether this
229: * ShaderProgram is supported on the specified Canvas.
230: */
231: boolean isSupported(Canvas3D cv) {
232: return cv.shadingLanguageCg;
233: }
234:
235: /**
236: * Method to create the native shader.
237: */
238: ShaderError createShader(Context ctx, ShaderRetained shader,
239: ShaderId[] shaderIdArr) {
240: return Pipeline.getPipeline().createCgShader(ctx,
241: shader.shaderType, shaderIdArr);
242: }
243:
244: /**
245: * Method to destroy the native shader.
246: */
247: ShaderError destroyShader(Context ctx, ShaderId shaderId) {
248: return Pipeline.getPipeline().destroyCgShader(ctx, shaderId);
249: }
250:
251: /**
252: * Method to compile the native shader.
253: */
254: ShaderError compileShader(Context ctx, ShaderId shaderId,
255: String source) {
256: return Pipeline.getPipeline().compileCgShader(ctx, shaderId,
257: source);
258: }
259:
260: /**
261: * Method to create the native shader program.
262: */
263: ShaderError createShaderProgram(Context ctx,
264: ShaderProgramId[] shaderProgramIdArr) {
265: return Pipeline.getPipeline().createCgShaderProgram(ctx,
266: shaderProgramIdArr);
267: }
268:
269: /**
270: * Method to destroy the native shader program.
271: */
272: ShaderError destroyShaderProgram(Context ctx,
273: ShaderProgramId shaderProgramId) {
274: return Pipeline.getPipeline().destroyCgShaderProgram(ctx,
275: shaderProgramId);
276: }
277:
278: /**
279: * Method to link the native shader program.
280: */
281: ShaderError linkShaderProgram(Context ctx,
282: ShaderProgramId shaderProgramId, ShaderId[] shaderIds) {
283: return Pipeline.getPipeline().linkCgShaderProgram(ctx,
284: shaderProgramId, shaderIds);
285: }
286:
287: ShaderError bindVertexAttrName(Context ctx,
288: ShaderProgramId shaderProgramId, String attrName,
289: int attrIndex) {
290: // This is a no-op for Cg
291: return null;
292: }
293:
294: void lookupVertexAttrNames(Context ctx,
295: ShaderProgramId shaderProgramId, String[] attrNames,
296: boolean[] errArr) {
297: Pipeline.getPipeline().lookupCgVertexAttrNames(ctx,
298: shaderProgramId, attrNames.length, attrNames, errArr);
299: }
300:
301: void lookupShaderAttrNames(Context ctx,
302: ShaderProgramId shaderProgramId, String[] attrNames,
303: AttrNameInfo[] attrNameInfoArr) {
304:
305: int numAttrNames = attrNames.length;
306:
307: ShaderAttrLoc[] locArr = new ShaderAttrLoc[numAttrNames];
308: int[] typeArr = new int[numAttrNames];
309: int[] sizeArr = new int[numAttrNames]; // currently unused
310: boolean[] isArrayArr = new boolean[numAttrNames];
311:
312: Pipeline.getPipeline().lookupCgShaderAttrNames(ctx,
313: shaderProgramId, numAttrNames, attrNames, locArr,
314: typeArr, sizeArr, isArrayArr);
315:
316: for (int i = 0; i < numAttrNames; i++) {
317: attrNameInfoArr[i] = new AttrNameInfo();
318: attrNameInfoArr[i].setLocation(locArr[i]);
319: attrNameInfoArr[i].setArray(isArrayArr[i]);
320: attrNameInfoArr[i].setType(typeArr[i]);
321: /*
322: System.err.println(attrNames[i] +
323: " : loc = " + locArr[i] +
324: ", type = " + typeArr[i] +
325: ", isArray = " + isArrayArr[i] +
326: ", size = " + sizeArr[i]);
327: */
328: }
329: }
330:
331: /**
332: * Method to enable the native shader program.
333: */
334: ShaderError enableShaderProgram(Context ctx,
335: ShaderProgramId shaderProgramId) {
336: return Pipeline.getPipeline().useCgShaderProgram(ctx,
337: shaderProgramId);
338: }
339:
340: /**
341: * Method to disable the native shader program.
342: */
343: ShaderError disableShaderProgram(Context ctx) {
344: return Pipeline.getPipeline().useCgShaderProgram(ctx, null);
345: }
346:
347: }
|