OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "common.hpp"
22 #include <stdlib.h>
23 #include <string>
24 #include <vector>
25 
26 using namespace std;
27 
28 static GLuint common_compile(GLenum type, const char *source)
29 {
30  GLuint shader = glCreateShader(type);
31  glShaderSource(shader, 1, &source, NULL);
32  glCompileShader(shader);
33 
34  GLint status;
35  glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
36  if (status == GL_FALSE)
37  {
38  GLint len;
39  GLsizei out_len;
40 
41  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
42  vector<char> buf(len);
43  glGetShaderInfoLog(shader, len, &out_len, &buf[0]);
44  LOGI("Shader log:\n%s", &buf[0]);
45 
46  glDeleteShader(shader);
47  return 0;
48  }
49 
50  return shader;
51 }
52 
53 static bool check_program(GLuint prog)
54 {
55  GLint status;
56  glGetProgramiv(prog, GL_LINK_STATUS, &status);
57  if (status == GL_FALSE)
58  {
59  GLint len;
60  GLsizei out_len;
61 
62  glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
63  vector<char> buf(len);
64  glGetProgramInfoLog(prog, len, &out_len, &buf[0]);
65  LOGI("Program log:\n%s", &buf[0]);
66 
67  glDeleteProgram(prog);
68  return false;
69  }
70 
71  return true;
72 }
73 
74 GLuint common_compile_shader(const char *vs_source, const char *fs_source)
75 {
76  GLuint prog = glCreateProgram();
77  GLuint vs = common_compile(GL_VERTEX_SHADER, vs_source);
78  if (!vs)
79  {
80  glDeleteProgram(prog);
81  return 0;
82  }
83 
84  GLuint fs = common_compile(GL_FRAGMENT_SHADER, fs_source);
85  if (!fs)
86  {
87  glDeleteShader(vs);
88  glDeleteProgram(prog);
89  return 0;
90  }
91 
92  glAttachShader(prog, vs);
93  glAttachShader(prog, fs);
94  glLinkProgram(prog);
95 
96  glDeleteShader(vs);
97  glDeleteShader(fs);
98 
99  if (!check_program(prog))
100  {
101  LOGE("Failed to link program.");
102  return 0;
103  }
104 
105  return prog;
106 }
107 
108 GLuint common_compile_compute_shader(const char *cs_source)
109 {
110  GLuint prog = glCreateProgram();
111  GLuint cs = common_compile(GL_COMPUTE_SHADER, cs_source);
112  if (!cs)
113  {
114  glDeleteProgram(prog);
115  return 0;
116  }
117 
118  glAttachShader(prog, cs);
119  glLinkProgram(prog);
120 
121  glDeleteShader(cs);
122 
123  if (!check_program(prog))
124  {
125  LOGE("Failed to link program.");
126  return 0;
127  }
128 
129  return prog;
130 }
131 
132 static bool read_file_string(const char *path, char **out_buf)
133 {
134  FILE *file = common_fopen(path, "rb");
135  if (!file)
136  {
137  LOGE("Failed to open file: %s.", path);
138  return false;
139  }
140 
141  fseek(file, 0, SEEK_END);
142  long len = ftell(file);
143  rewind(file);
144 
145  char *buf = (char*)malloc(len + 1);
146  if (!buf)
147  {
148  fclose(file);
149  return false;
150  }
151 
152  long ret = fread(buf, 1, len, file);
153  fclose(file);
154 
155  if (ret == len)
156  {
157  buf[len] = '\0';
158  *out_buf = buf;
159  return true;
160  }
161  else
162  {
163  free(buf);
164  *out_buf = NULL;
165  return false;
166  }
167 }
168 
169 GLuint common_compile_shader_from_file(const char *vs_source, const char *fs_source)
170 {
171  LOGI("Compiling vertex/fragment shader: %s, %s.", vs_source, fs_source);
172  char *vs_buf = NULL;
173  char *fs_buf = NULL;
174  if (!read_file_string(vs_source, &vs_buf))
175  {
176  return 0;
177  }
178 
179  if (!read_file_string(fs_source, &fs_buf))
180  {
181  free(vs_buf);
182  return 0;
183  }
184 
185  GLuint prog = common_compile_shader(vs_buf, fs_buf);
186  free(vs_buf);
187  free(fs_buf);
188  return prog;
189 }
190 
192 {
193  LOGI("Compiling compute shader from %s.", cs_source);
194  char *cs_buf = NULL;
195  if (!read_file_string(cs_source, &cs_buf))
196  {
197  return 0;
198  }
199 
200  GLuint prog = common_compile_compute_shader(cs_buf);
201  free(cs_buf);
202  return prog;
203 }
204 
205 static string common_basedir;
206 void common_set_basedir(const char *basedir)
207 {
208  common_basedir = basedir;
209 }
210 
211 string common_get_path(const char *basepath)
212 {
213  if (!common_basedir.empty())
214  {
215  return common_basedir + "/" + basepath;
216  }
217  else
218  {
219  return basepath;
220  }
221 }
222 
223 FILE *common_fopen(const char *path, const char *mode)
224 {
225  string join_path = common_get_path(path);
226  FILE *ret = fopen(join_path.c_str(), mode);
227  LOGI("Opening: %s (%s).", join_path.c_str(), ret ? "success" : "failure");
228  return ret;
229 }
230 
#define LOGI(...)
Definition: AstcTextures.h:29
GLuint common_compile_compute_shader_from_file(const char *cs_source)
Definition: common.cpp:317
GLuint common_compile_shader_from_file(const char *vs_source, const char *fs_source)
Definition: common.cpp:241
static bool check_program(GLuint prog)
Definition: common.cpp:53
GLuint common_compile_shader(const char *vs_source, const char *fs_source)
Definition: common.cpp:74
GLenum mode
Definition: gl2ext.h:302
void common_set_basedir(const char *basedir)
Definition: common.cpp:332
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:877
static bool read_file_string(const char *path, char **out_buf)
Definition: common.cpp:132
static GLuint common_compile(GLenum type, const char *source)
Definition: common.cpp:28
GLenum type
Definition: gl2ext.h:133
string common_get_path(const char *basepath)
Definition: common.cpp:337
FILE * common_fopen(const char *path, const char *mode)
Definition: common.cpp:349
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: gl2ext.h:134
#define LOGE(...)
Definition: AstcTextures.h:30
GLuint common_compile_compute_shader(const char *cs_source)
Definition: common.cpp:180
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
static string common_basedir
Definition: common.cpp:205
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count