GlShader.java revision 59a677ada27c660e9cd7486f0d702753dbeb6d39
1/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28package org.webrtc;
29
30import android.opengl.GLES20;
31import android.util.Log;
32
33import java.nio.FloatBuffer;
34
35// Helper class for handling OpenGL shaders and shader programs.
36public class GlShader {
37  private static final String TAG = "GlShader";
38
39  private static int compileShader(int shaderType, String source) {
40    int[] result = new int[] {
41        GLES20.GL_FALSE
42    };
43    int shader = GLES20.glCreateShader(shaderType);
44    GLES20.glShaderSource(shader, source);
45    GLES20.glCompileShader(shader);
46    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, 0);
47    if (result[0] != GLES20.GL_TRUE) {
48      Log.e(TAG, "Could not compile shader " + shaderType + ":" +
49          GLES20.glGetShaderInfoLog(shader));
50      throw new RuntimeException(GLES20.glGetShaderInfoLog(shader));
51    }
52    GlUtil.checkNoGLES2Error("compileShader");
53    return shader;
54  }
55
56  private int vertexShader;
57  private int fragmentShader;
58  private int program;
59
60  public GlShader(String vertexSource, String fragmentSource) {
61    vertexShader = compileShader(GLES20.GL_VERTEX_SHADER, vertexSource);
62    fragmentShader = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
63    program = GLES20.glCreateProgram();
64    if (program == 0) {
65      throw new RuntimeException("Could not create program");
66    }
67    GLES20.glAttachShader(program, vertexShader);
68    GLES20.glAttachShader(program, fragmentShader);
69    GLES20.glLinkProgram(program);
70    int[] linkStatus = new int[] {
71      GLES20.GL_FALSE
72    };
73    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
74    if (linkStatus[0] != GLES20.GL_TRUE) {
75      Log.e(TAG, "Could not link program: " +
76          GLES20.glGetProgramInfoLog(program));
77      throw new RuntimeException(GLES20.glGetProgramInfoLog(program));
78    }
79    GlUtil.checkNoGLES2Error("Creating GlShader");
80  }
81
82  public int getAttribLocation(String label) {
83    if (program == -1) {
84      throw new RuntimeException("The program has been released");
85    }
86    int location = GLES20.glGetAttribLocation(program, label);
87    if (location < 0) {
88      throw new RuntimeException("Could not locate '" + label + "' in program");
89    }
90    return location;
91  }
92
93  /**
94   * Enable and upload a vertex array for attribute |label|. The vertex data is specified in
95   * |buffer| with |dimension| number of components per vertex.
96   */
97  public void setVertexAttribArray(String label, int dimension, FloatBuffer buffer) {
98    if (program == -1) {
99      throw new RuntimeException("The program has been released");
100    }
101    int location = getAttribLocation(label);
102    GLES20.glEnableVertexAttribArray(location);
103    GLES20.glVertexAttribPointer(location, dimension, GLES20.GL_FLOAT, false, 0, buffer);
104    GlUtil.checkNoGLES2Error("setVertexAttribArray");
105  }
106
107  public int getUniformLocation(String label) {
108    if (program == -1) {
109      throw new RuntimeException("The program has been released");
110    }
111    int location = GLES20.glGetUniformLocation(program, label);
112    if (location < 0) {
113      throw new RuntimeException("Could not locate uniform '" + label + "' in program");
114    }
115    return location;
116  }
117
118  public void useProgram() {
119    if (program == -1) {
120      throw new RuntimeException("The program has been released");
121    }
122    GLES20.glUseProgram(program);
123    GlUtil.checkNoGLES2Error("glUseProgram");
124  }
125
126  public void release() {
127    Log.d(TAG, "Deleting shader.");
128    // Flag shaders for deletion (does not delete until no longer attached to a program).
129    if (vertexShader != -1) {
130      GLES20.glDeleteShader(vertexShader);
131      vertexShader = -1;
132    }
133    if (fragmentShader != -1) {
134      GLES20.glDeleteShader(fragmentShader);
135      fragmentShader = -1;
136    }
137    // Delete program, automatically detaching any shaders from it.
138    if (program != -1) {
139      GLES20.glDeleteProgram(program);
140      program = -1;
141    }
142  }
143}
144