1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.mediaeffects; 18 19 import android.opengl.GLES20; 20 21 public class GLToolbox { 22 loadShader(int shaderType, String source)23 public static int loadShader(int shaderType, String source) { 24 int shader = GLES20.glCreateShader(shaderType); 25 if (shader != 0) { 26 GLES20.glShaderSource(shader, source); 27 GLES20.glCompileShader(shader); 28 int[] compiled = new int[1]; 29 GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); 30 if (compiled[0] == 0) { 31 String info = GLES20.glGetShaderInfoLog(shader); 32 GLES20.glDeleteShader(shader); 33 throw new RuntimeException("Could not compile shader " + shaderType + ":" + info); 34 } 35 } 36 return shader; 37 } 38 createProgram(String vertexSource, String fragmentSource)39 public static int createProgram(String vertexSource, String fragmentSource) { 40 int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); 41 if (vertexShader == 0) { 42 return 0; 43 } 44 int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); 45 if (pixelShader == 0) { 46 return 0; 47 } 48 49 int program = GLES20.glCreateProgram(); 50 if (program != 0) { 51 GLES20.glAttachShader(program, vertexShader); 52 checkGlError("glAttachShader"); 53 GLES20.glAttachShader(program, pixelShader); 54 checkGlError("glAttachShader"); 55 GLES20.glLinkProgram(program); 56 int[] linkStatus = new int[1]; 57 GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 58 0); 59 if (linkStatus[0] != GLES20.GL_TRUE) { 60 String info = GLES20.glGetProgramInfoLog(program); 61 GLES20.glDeleteProgram(program); 62 throw new RuntimeException("Could not link program: " + info); 63 } 64 } 65 return program; 66 } 67 checkGlError(String op)68 public static void checkGlError(String op) { 69 int error; 70 while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { 71 throw new RuntimeException(op + ": glError " + error); 72 } 73 } 74 initTexParams()75 public static void initTexParams() { 76 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 77 GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 78 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 79 GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); 80 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, 81 GLES20.GL_CLAMP_TO_EDGE); 82 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, 83 GLES20.GL_CLAMP_TO_EDGE); 84 } 85 86 } 87