• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google LLC All Rights Reserved.
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.google.skar.examples.helloskar.rendering;
18 
19 import android.content.Context;
20 import android.opengl.GLES20;
21 import android.util.Log;
22 
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 
28 /**
29  * Shader helper functions.
30  */
31 public class ShaderUtil {
32     /**
33      * Converts a raw text file, saved as a resource, into an OpenGL ES shader.
34      *
35      * @param type     The type of shader we will be creating.
36      * @param filename The filename of the asset file about to be turned into a shader.
37      * @return The shader object handler.
38      */
loadGLShader(String tag, Context context, int type, String filename)39     public static int loadGLShader(String tag, Context context, int type, String filename)
40             throws IOException {
41         String code = readRawTextFileFromAssets(context, filename);
42         int shader = GLES20.glCreateShader(type);
43         GLES20.glShaderSource(shader, code);
44         GLES20.glCompileShader(shader);
45 
46         // Get the compilation status.
47         final int[] compileStatus = new int[1];
48         GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
49 
50         // If the compilation failed, delete the shader.
51         if (compileStatus[0] == 0) {
52             Log.e(tag, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
53             GLES20.glDeleteShader(shader);
54             shader = 0;
55         }
56 
57         if (shader == 0) {
58             throw new RuntimeException("Error creating shader.");
59         }
60 
61         return shader;
62     }
63 
64     /**
65      * Checks if we've had an error inside of OpenGL ES, and if so what that error is.
66      *
67      * @param label Label to report in case of error.
68      * @throws RuntimeException If an OpenGL error is detected.
69      */
checkGLError(String tag, String label)70     public static void checkGLError(String tag, String label) {
71         int lastError = GLES20.GL_NO_ERROR;
72         // Drain the queue of all errors.
73         int error;
74         while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
75             Log.e(tag, label + ": glError " + error);
76             lastError = error;
77         }
78         if (lastError != GLES20.GL_NO_ERROR) {
79             throw new RuntimeException(label + ": glError " + lastError);
80         }
81     }
82 
83     /**
84      * Converts a raw text file into a string.
85      *
86      * @param filename The filename of the asset file about to be turned into a shader.
87      * @return The context of the text file, or null in case of error.
88      */
readRawTextFileFromAssets(Context context, String filename)89     private static String readRawTextFileFromAssets(Context context, String filename)
90             throws IOException {
91         try (InputStream inputStream = context.getAssets().open(filename);
92              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
93             StringBuilder sb = new StringBuilder();
94             String line;
95             while ((line = reader.readLine()) != null) {
96                 sb.append(line).append("\n");
97             }
98             return sb.toString();
99         }
100     }
101 }
102