content: package com.hm.videoedit.egl;
  
  import android.content.Context;
  import android.opengl.GLES20;
  import android.util.Log;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  /**
   * Created by 海米 on 2017/7/7.
   */
  
  public class ShaderUtils {
      private static final String TAG = "ShaderUtils";
  
      public static void checkGlError(String label) {
          int error;
          while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
              Log.e(TAG, label + ": glError " + error);
              throw new RuntimeException(label + ": glError " + error);
          }
      }
  
      public static int createProgram(String vertexSource, String fragmentSource) {
          int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
          if (vertexShader == 0) {
              return 0;
          }
          int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
          if (pixelShader == 0) {
              return 0;
          }
          int program = GLES20.glCreateProgram();
          if (program != 0) {
              GLES20.glAttachShader(program, vertexShader);
              checkGlError("glAttachShader");
              GLES20.glAttachShader(program, pixelShader);
              checkGlError("glAttachShader");
              GLES20.glLinkProgram(program);
              int[] linkStatus = new int[1];
              GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
              if (linkStatus[0] != GLES20.GL_TRUE) {
                  Log.e(TAG, "Could not link program: ");
                  Log.e(TAG, GLES20.glGetProgramInfoLog(program));
                  GLES20.glDeleteProgram(program);
                  program = 0;
              }
          }
          return program;
      }
  
  
      public static int loadShader(int shaderType, String source) {
          int shader = GLES20.glCreateShader(shaderType);
          if (shader != 0) {
              GLES20.glShaderSource(shader, source);
              GLES20.glCompileShader(shader);
              int[] compiled = new int[1];
              GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
              if (compiled[0] == 0) {
                  Log.e(TAG, "Could not compile shader " + shaderType + ":");
                  Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
                  GLES20.glDeleteShader(shader);
                  shader = 0;
              }
          }
          return shader;
      }
  
      public static String readRawTextFile(Context context, int resId) {
          InputStream inputStream = context.getResources().openRawResource(resId);
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
              StringBuilder sb = new StringBuilder();
              String line;
              while ((line = reader.readLine()) != null) {
                  sb.append(line).append("\n");
              }
              reader.close();
              return sb.toString();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
  }
  