OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chromoting.cardboard; | |
6 | |
7 import android.opengl.GLES20; | |
8 | |
9 import org.chromium.base.Log; | |
10 | |
11 /** | |
12 * Helper class for working with OpenGL shaders and programs. | |
13 */ | |
14 public class ShaderHelper { | |
15 private static final String TAG = "Cardboard"; | |
16 | |
17 /** | |
18 * Compile a shader. | |
19 * | |
20 * @param shaderType The shader type. | |
21 * @param shaderSource The shader source code. | |
22 * @return An OpenGL handle to the shader. | |
23 */ | |
24 public static int compileShader(int shaderType, String shaderSource) { | |
25 int shaderHandle = GLES20.glCreateShader(shaderType); | |
26 | |
27 if (shaderHandle != 0) { | |
28 // Pass in the shader source. | |
29 GLES20.glShaderSource(shaderHandle, shaderSource); | |
30 | |
31 // Compile the shader. | |
32 GLES20.glCompileShader(shaderHandle); | |
33 | |
34 // Get the compilation status. | |
35 int[] compileStatus = new int[1]; | |
36 GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compile
Status, 0); | |
37 | |
38 // If the compilation failed, delete the shader. | |
39 if (compileStatus[0] == 0) { | |
40 Log.e(TAG, "Error compiling shader: %s", GLES20.glGetShaderInfoL
og(shaderHandle)); | |
41 GLES20.glDeleteShader(shaderHandle); | |
42 shaderHandle = 0; | |
43 } | |
44 } | |
45 | |
46 if (shaderHandle == 0) { | |
47 // TODO(shichengfeng): Handle Exception gracefully. | |
48 throw new RuntimeException("Error creating shader."); | |
49 } | |
50 | |
51 return shaderHandle; | |
52 } | |
53 | |
54 /** | |
55 * Compile and link a program. | |
56 * | |
57 * @param vertexShaderHandle An OpenGL handle to an already-compiled vertex
shader. | |
58 * @param fragmentShaderHandle An OpenGL handle to an already-compiled fragm
ent shader. | |
59 * @param attributes Attributes that need to be bound to the program. | |
60 * @return An OpenGL handle to the program. | |
61 */ | |
62 public static int createAndLinkProgram(int vertexShaderHandle, | |
63 int fragmentShaderHandle, String[] attributes) { | |
64 int programHandle = GLES20.glCreateProgram(); | |
65 | |
66 if (programHandle != 0) { | |
67 // Bind the vertex shader to the program. | |
68 GLES20.glAttachShader(programHandle, vertexShaderHandle); | |
69 | |
70 // Bind the fragment shader to the program. | |
71 GLES20.glAttachShader(programHandle, fragmentShaderHandle); | |
72 | |
73 // Bind attributes | |
74 if (attributes != null) { | |
75 int size = attributes.length; | |
76 for (int i = 0; i < size; i++) { | |
77 GLES20.glBindAttribLocation(programHandle, i, attributes[i])
; | |
78 } | |
79 } | |
80 | |
81 // Link the two shaders together into a program. | |
82 GLES20.glLinkProgram(programHandle); | |
83 | |
84 // Get the link status. | |
85 int[] linkStatus = new int[1]; | |
86 GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStat
us, 0); | |
87 | |
88 // If the link failed, delete the program. | |
89 if (linkStatus[0] == 0) { | |
90 Log.e(TAG, "Error compiling program: %s", | |
91 GLES20.glGetProgramInfoLog(programHandle)); | |
92 GLES20.glDeleteProgram(programHandle); | |
93 programHandle = 0; | |
94 } | |
95 } | |
96 | |
97 if (programHandle == 0) { | |
98 // TODO(shichengfeng): Handle Exception gracefully. | |
99 throw new RuntimeException("Error creating program."); | |
100 } | |
101 | |
102 return programHandle; | |
103 } | |
104 } | |
OLD | NEW |