| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.opengl.GLES20; |
| 14 |
| 15 | import org.telegram.messenger.FileLog; |
| 16 |
| 17 | import java.nio.FloatBuffer; |
| 18 |
| 19 | // Helper class for handling OpenGL shaders and shader programs. |
| 20 | public class GlShader { |
| 21 | private static final String TAG = "GlShader"; |
| 22 |
| 23 | private static int compileShader(int shaderType, String source) { |
| 24 | final int shader = GLES20.glCreateShader(shaderType); |
| 25 | if (shader == 0) { |
| 26 | throw new RuntimeException("glCreateShader() failed. GLES20 error: " + GLES20.glGetError()); |
| 27 | } |
| 28 | GLES20.glShaderSource(shader, source); |
| 29 | GLES20.glCompileShader(shader); |
| 30 | int[] compileStatus = new int[] {GLES20.GL_FALSE}; |
| 31 | GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0); |
| 32 | if (compileStatus[0] != GLES20.GL_TRUE) { |
| 33 | Logging.e( |
| 34 | TAG, "Compile error " + GLES20.glGetShaderInfoLog(shader) + " in shader:\n" + source); |
| 35 | throw new RuntimeException(GLES20.glGetShaderInfoLog(shader)); |
| 36 | } |
| 37 | GlUtil.checkNoGLES2Error("compileShader"); |
| 38 | return shader; |
| 39 | } |
| 40 |
| 41 | private int program; |
| 42 |
| 43 | public GlShader(String vertexSource, String fragmentSource) { |
| 44 | final int vertexShader = compileShader(GLES20.GL_VERTEX_SHADER, vertexSource); |
| 45 | final int fragmentShader = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); |
| 46 | program = GLES20.glCreateProgram(); |
| 47 | if (program == 0) { |
| 48 | throw new RuntimeException("glCreateProgram() failed. GLES20 error: " + GLES20.glGetError()); |
| 49 | } |
| 50 | GLES20.glAttachShader(program, vertexShader); |
| 51 | GLES20.glAttachShader(program, fragmentShader); |
| 52 | GLES20.glLinkProgram(program); |
| 53 | int[] linkStatus = new int[] {GLES20.GL_FALSE}; |
| 54 | GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); |
| 55 | if (linkStatus[0] != GLES20.GL_TRUE) { |
| 56 | Logging.e(TAG, "Could not link program: " + GLES20.glGetProgramInfoLog(program)); |
| 57 | throw new RuntimeException(GLES20.glGetProgramInfoLog(program)); |
| 58 | } |
| 59 | // According to the documentation of glLinkProgram(): |
| 60 | // "After the link operation, applications are free to modify attached shader objects, compile |
| 61 | // attached shader objects, detach shader objects, delete shader objects, and attach additional |
| 62 | // shader objects. None of these operations affects the information log or the program that is |
| 63 | // part of the program object." |
| 64 | // But in practice, detaching shaders from the program seems to break some devices. Deleting the |
| 65 | // shaders are fine however - it will delete them when they are no longer attached to a program. |
| 66 | //GLES20.glDeleteShader(vertexShader); delete crashes on xiaomi |
| 67 | //GLES20.glDeleteShader(fragmentShader); |
| 68 | //GlUtil.checkNoGLES2Error("Creating GlShader"); |
| 69 | } |
| 70 |
| 71 | public int getAttribLocation(String label) { |
| 72 | if (program == -1) { |
| 73 | throw new RuntimeException("The program has been released"); |
| 74 | } |
| 75 | int location = GLES20.glGetAttribLocation(program, label); |
| 76 | if (location < 0) { |
| 77 | throw new RuntimeException("Could not locate '" + label + "' in program"); |
| 78 | } |
| 79 | return location; |
| 80 | } |
| 81 |
| 82 | /** |
| 83 | * Enable and upload a vertex array for attribute |label|. The vertex data is specified in |
| 84 | * |buffer| with |dimension| number of components per vertex. |
| 85 | */ |
| 86 | public void setVertexAttribArray(String label, int dimension, FloatBuffer buffer) { |
| 87 | setVertexAttribArray(label, dimension, 0 /* stride */, buffer); |
| 88 | } |
| 89 |
| 90 | /** |
| 91 | * Enable and upload a vertex array for attribute |label|. The vertex data is specified in |
| 92 | * |buffer| with |dimension| number of components per vertex and specified |stride|. |
| 93 | */ |
| 94 | public void setVertexAttribArray(String label, int dimension, int stride, FloatBuffer buffer) { |
| 95 | if (program == -1) { |
| 96 | throw new RuntimeException("The program has been released"); |
| 97 | } |
| 98 | int location = getAttribLocation(label); |
| 99 | GLES20.glEnableVertexAttribArray(location); |
| 100 | GLES20.glVertexAttribPointer(location, dimension, GLES20.GL_FLOAT, false, stride, buffer); |
| 101 | GlUtil.checkNoGLES2Error("setVertexAttribArray"); |
| 102 | } |
| 103 |
| 104 | public int getUniformLocation(String label) { |
| 105 | if (program == -1) { |
| 106 | throw new RuntimeException("The program has been released"); |
| 107 | } |
| 108 | int location = GLES20.glGetUniformLocation(program, label); |
| 109 | if (location < 0) { |
| 110 | throw new RuntimeException("Could not locate uniform '" + label + "' in program"); |
| 111 | } |
| 112 | return location; |
| 113 | } |
| 114 |
| 115 | public void useProgram() { |
| 116 | if (program == -1) { |
| 117 | throw new RuntimeException("The program has been released"); |
| 118 | } |
| 119 | synchronized (EglBase.lock) { |
| 120 | GLES20.glUseProgram(program); |
| 121 | } |
| 122 | try { |
| 123 | GlUtil.checkNoGLES2Error("glUseProgram"); |
| 124 | } catch (Exception e) { |
| 125 | FileLog.e(e); |
| 126 | } |
| 127 | } |
| 128 |
| 129 | public void release() { |
| 130 | Logging.d(TAG, "Deleting shader."); |
| 131 | // Delete program, automatically detaching any shaders from it. |
| 132 | if (program != -1) { |
| 133 | GLES20.glDeleteProgram(program); |
| 134 | program = -1; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 |