aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/javax/media/opengl/util/glsl/ShaderCode.java
blob: b512f59126937b5a92d4018d66d2ef5f408473d1 (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

package javax.media.opengl.util.glsl;

import javax.media.opengl.util.*;
import javax.media.opengl.*;

import java.nio.*;
import java.io.*;
import java.net.*;

public class ShaderCode {
    public ShaderCode(int type, int number, 
                      int binFormat, Buffer binary, String[][] source) {
        switch (type) {
            case GL2ES2.GL_VERTEX_SHADER:
            case GL2ES2.GL_FRAGMENT_SHADER:
                break;
            default:
                throw new GLException("Unknown shader type: "+type);
        }
        shaderSource = source;
        shaderBinaryFormat = binFormat;
        shaderBinary = binary;
        shaderType   = type;
        shader = BufferUtil.newIntBuffer(number);
        id = getNextID();
    }

    public static ShaderCode create(int type, int number, 
                                    Class context, int binFormat, String binaryFile, String[] sourceFiles) {
        String[][] shaderSources = null;
        ByteBuffer shaderBinary = null;
        if(null!=sourceFiles) {
            shaderSources = new String[sourceFiles.length][1];
            for(int i=0; i<sourceFiles.length; i++) {
                shaderSources[i][0] = readShaderSource(context, sourceFiles[i]);
                if(null == shaderSources[i][0]) {
                    throw new RuntimeException("Can't find shader source " + sourceFiles[i]);
                }
            }
        }
        if(null!=binaryFile && 0<=binFormat) {
            shaderBinary = readShaderBinary(context, binaryFile);
            if(null == shaderBinary) {
                System.err.println("Can't find shader binary " + binaryFile + " - ignored");
                binFormat = -1;
            }
        }
        return new ShaderCode(type, number, binFormat, shaderBinary, shaderSources);
    }

    /**
     * returns the uniq shader id as an integer
     * @see #key()
     */
    public int        id() { return id.intValue(); }

    /**
     * returns the uniq shader id as an Integer
     *
     * @see #id()
     */
    public Integer    key() { return id; }

    public int        shaderType() { return shaderType; }
    public String     shaderTypeStr() { return shaderTypeStr(shaderType); }

    public static String shaderTypeStr(int type) { 
        switch (type) {
            case GL2ES2.GL_VERTEX_SHADER:
                return "VERTEX_SHADER";
            case GL2ES2.GL_FRAGMENT_SHADER:
                return "FRAGMENT_SHADER";
        }
        return "UNKNOWN_SHADER";
    }

    public int        shaderBinaryFormat() { return shaderBinaryFormat; }
    public Buffer     shaderBinary() { return shaderBinary; }
    public String[][] shaderSource() { return shaderSource; }

    public boolean    isValid() { return valid; }

    public IntBuffer  shader() { return shader; }

    public boolean compile(GL2ES2 gl) {
        return compile(gl, null);
    }
    public boolean compile(GL2ES2 gl, PrintStream verboseOut) {
        if(isValid()) return true;

        // Create & Compile the vertex/fragment shader objects
        valid=gl.glCreateCompileShader(shader, shaderType,
                                       shaderBinaryFormat, shaderBinary,
                                       shaderSource, verboseOut);
        shader.clear();
        return valid;
    }

    public void release(GL2ES2 gl) {
        if(isValid()) {
            gl.glDeleteShader(shader());
            valid=false;
        }
    }

    public boolean equals(Object obj) {
        if(this==obj) return true;
        if(obj instanceof ShaderCode) {
            return id()==((ShaderCode)obj).id();
        }
        return false;
    }
    public int hashCode() {
        return id.intValue();
    }
    public String toString() {
        StringBuffer buf = new StringBuffer("ShaderCode [id="+id+", type="+shaderTypeStr()+", valid="+valid);
        /*
        if(shaderSource!=null) {
            for(int i=0; i<shaderSource.length; i++) {
                for(int j=0; j<shaderSource[i].length; j++) {
                    buf.append("\n\t, ShaderSource["+i+"]["+j+"]:\n");
                    buf.append(shaderSource[i][j]);
                }
            }
        } */
        buf.append("]");
        return buf.toString();
    }

    public static void readShaderSource(ClassLoader context, String path, URL url, StringBuffer result) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("#include ")) {
                    String includeFile = line.substring(9).trim();
                    // Try relative path first
                    String next = makeRelative(path, includeFile);
                    URL nextURL = getResource(context, next);
                    if (nextURL == null) {
                        // Try absolute path
                        next = includeFile;
                        nextURL = getResource(context, next);
                    }
                    if (nextURL == null) {
                        // Fail
                        throw new FileNotFoundException("Can't find include file " + includeFile);
                    }
                    readShaderSource(context, next, nextURL, result);
                } else {
                    result.append(line + "\n");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readShaderSource(Class context, String path) {
        URL url = getResource(context.getClassLoader(), path);
        if (url == null) {
            // Try again by scoping the path within the class's package
            String className = context.getName().replace('.', '/');
            int lastSlash = className.lastIndexOf('/');
            if (lastSlash >= 0) {
                String tmpPath = className.substring(0, lastSlash + 1) + path;
                url = getResource(context.getClassLoader(), tmpPath);
                if (url != null) {
                    path = tmpPath;
                }
            }
        }
        if (url == null) {
            return null;
        }
        StringBuffer result = new StringBuffer();
        readShaderSource(context.getClassLoader(), path, url, result);
        return result.toString();
    }

    public static ByteBuffer readShaderBinary(Class context, String path) {
        try {
            URL url = getResource(context.getClassLoader(), path);
            if (url == null) {
                // Try again by scoping the path within the class's package
                String className = context.getName().replace('.', '/');
                int lastSlash = className.lastIndexOf('/');
                if (lastSlash >= 0) {
                    String tmpPath = className.substring(0, lastSlash + 1) + path;
                    url = getResource(context.getClassLoader(), tmpPath);
                    if (url != null) {
                        path = tmpPath;
                    }
                }
            }
            if (url == null) {
                return null;
            }
            return readAll(new BufferedInputStream(url.openStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //----------------------------------------------------------------------
    // Internals only below this point
    //

    protected static URL getResource(ClassLoader context, String path) {
        if (context != null) {
            return context.getResource(path);
        } else {
            return ClassLoader.getSystemResource(path);
        }
    }

    protected static String makeRelative(String context, String includeFile) {
        File file = new File(context);
        file = file.getParentFile();
        while (file != null && includeFile.startsWith("../")) {
            file = file.getParentFile();
            includeFile = includeFile.substring(3);
        }
        if (file != null) {
            String res = new File(file, includeFile).getPath();
	    // Handle things on Windows
            return res.replace('\\', '/');
        } else {
            return includeFile;
        }
    }

    private static ByteBuffer readAll(InputStream stream) throws IOException {
        byte[] data = new byte[1024];
        int numRead = 0;
        int pos = 0;
        do {
            int avail = data.length - pos;
            if (avail == 0) {
                int newSize = 2 * data.length;
                byte[] newData = new byte[newSize];
                System.arraycopy(data, 0, newData, 0, data.length);
                data = newData;
                avail = data.length - pos;
            }
            numRead = stream.read(data, pos, avail);
            if (numRead > 0) {
                pos += numRead;
            }
        } while (numRead >= 0);
        ByteBuffer res = ByteBuffer.wrap(data);
        if (data.length != pos) {
            res.limit(pos);
        }
        return res;
    }
    protected String[][] shaderSource = null;
    protected Buffer     shaderBinary = null;
    protected int        shaderBinaryFormat = -1;
    protected IntBuffer  shader = null;
    protected int        shaderType = -1;
    protected Integer    id = null;

    protected boolean valid=false;

    private static synchronized Integer getNextID() {
        return new Integer(nextID++);
    }
    protected static int nextID = 1;
}