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