aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/javax/media/opengl/sdk/glsl/CompileShader.java
blob: 1e239df9cfbb8acff0259e0a81a0c7059f9dba59 (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
package javax.media.opengl.sdk.glsl;

import javax.media.opengl.glsl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.io.Locator;

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

/** Precompiles a shader into a vendor binary format. Input is the
    resource name of the shader, such as
    "com/sun/opengl/impl/glsl/fixed/shader/a.fp".
    Output is "com/sun/opengl/impl/glsl/fixed/shader/bin/nvidia/a.bfp". 

    All path and suffixes are determined by the ShaderCode class,
    which ensures runtime compatibility.

    @see javax.media.opengl.glsl.ShaderCode
  */

public abstract class CompileShader {

    public abstract int getBinaryFormat();

    public abstract File getSDKCompilerDir();

    public abstract String getVertexShaderCompiler();

    public abstract String getFragmentShaderCompiler();

    public void processOneShader(String resourceName)
        throws IOException, UnsupportedEncodingException, InterruptedException
    {
        int type = -1;
        String outName=null;
        int suffixLen = -1;
        if(resourceName.endsWith(ShaderCode.getFileSuffix(false, GL2ES2.GL_FRAGMENT_SHADER))) {
            suffixLen = 2;
            type = GL2ES2.GL_FRAGMENT_SHADER;
        } else if(resourceName.endsWith(".frag")) {
            suffixLen = 4;
            type = GL2ES2.GL_FRAGMENT_SHADER;
        } else if(resourceName.endsWith(ShaderCode.getFileSuffix(false, GL2ES2.GL_VERTEX_SHADER))) {
            suffixLen = 2;
            type = GL2ES2.GL_VERTEX_SHADER;
        } else if(resourceName.endsWith(".vert")) {
            suffixLen = 4;
            type = GL2ES2.GL_VERTEX_SHADER;
        }
        String justName = basename(resourceName);
        outName = justName.substring(0, justName.length() - suffixLen) +
                  ShaderCode.getFileSuffix(true, type);
        URL resourceURL = Locator.getResource(null, resourceName);
        String dirName = dirname(resourceURL.getPath());

        outName = dirName + File.separator + "bin" + File.separator + 
                  ShaderCode.getBinarySubPath(getBinaryFormat()) + File.separator + 
                  outName;
        processOneShader(resourceName, outName, type);
    }

    public void processOneShader(String resourceName, String outName, int type)
        throws IOException, UnsupportedEncodingException, InterruptedException
    {
        URL resourceURL = Locator.getResource(null, resourceName);
        String dirName = dirname(resourceURL.getPath());

        String shader = ShaderCode.readShaderSource(null, resourceName);
        if(null==shader) {
            System.err.println("Can't find shader source " + resourceName + " - ignored");
            return;
        }
        System.err.println("Preprocessing: "+ resourceName+", in dir: "+dirName);
        String justName = basename(resourceName);
        String processor;
        switch (type) {
            case GL2ES2.GL_VERTEX_SHADER:
                processor = getVertexShaderCompiler();
                break;
            case GL2ES2.GL_FRAGMENT_SHADER:
                processor = getFragmentShaderCompiler();
                break;
            default:
                throw new GLException("Unknown shader type: "+type);
        }
        File outputFile = new File(outName);

        // Write shader to a file in java.io.tmpdir
        File tmpDir = new File(dirName+File.separator+"tmp");
        tmpDir.mkdirs();
        File tmpFile = new File(tmpDir, justName);
        Writer writer = new BufferedWriter(new FileWriter(tmpFile));
        writer.write(shader, 0, shader.length());
        writer.flush();
        writer.close();
        System.err.println("Preprocessed: "+ tmpFile.getAbsolutePath());

        File processorDir = getSDKCompilerDir();

        System.err.println("SDK: "+ processorDir.getAbsolutePath() + ", compiler: "+processor);

        System.err.println("Output: "+ outputFile.getAbsolutePath());

        // Run the tool
        Process process = Runtime.getRuntime().exec(new String[] {
                processorDir.getAbsolutePath() + File.separator + processor,
                tmpFile.getAbsolutePath(),
                outputFile.getAbsolutePath()
            }); // , null, processorDir);
        new StreamMonitor(process.getInputStream());
        new StreamMonitor(process.getErrorStream());
        process.waitFor();
        // Delete the temporary file
        // tmpFile.delete();
    }

    protected static String basename(String path) {
        int lastSlash = path.lastIndexOf("/");
        if (lastSlash < 0) {
            lastSlash = path.lastIndexOf("\\");
        }
        String basename;
        if (lastSlash < 0) {
            basename = path;
        } else {
            basename = path.substring(lastSlash + 1);
        }
        return basename;
    }

    protected static String dirname(String path) {
        int lastSlash = path.lastIndexOf("/");
        if (lastSlash < 0) {
            lastSlash = path.lastIndexOf("\\");
        }
        String dirname;
        if (lastSlash < 0) {
            dirname = new String();
        } else {
            dirname = path.substring(0, lastSlash + 1);
        }
        return dirname;
    }

    public void run(String[] args) {
        try {
            for (int i = 0; i < args.length; i++) {
                processOneShader(args[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class StreamMonitor implements Runnable {
        private InputStream istream;
        public StreamMonitor(InputStream stream) {
            istream = stream;
            new Thread(this, "Output Reader Thread").start();
        }

        public void run()
        {
            byte[] buffer = new byte[4096];
            try {
                int numRead = 0;
                do {
                    numRead = istream.read(buffer);
                    if (numRead > 0) {
                        System.out.write(buffer, 0, numRead);
                        System.out.flush();
                    }
                } while (numRead >= 0);
            }
            catch (IOException e) {
                try {
                    istream.close();
                } catch (IOException e2) {
                }
                // Should allow clean exit when process shuts down
            }
        }
    }
}