OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Shader.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-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 "Shader.h"
22 #include "Platform.h"
23 
24 #include <cstdio>
25 #include <cstdlib>
26 
27 namespace MaliSDK
28 {
29  void Shader::processShader(GLuint *shader, const char *filename, GLint shaderType)
30  {
31  const char *strings[1] = { NULL };
32 
33  /* Create shader and load into GL. */
34  *shader = GL_CHECK(glCreateShader(shaderType));
35  strings[0] = loadShader(filename);
36  GL_CHECK(glShaderSource(*shader, 1, strings, NULL));
37 
38  /* Clean up shader source. */
39  free((void *)(strings[0]));
40  strings[0] = NULL;
41 
42  /* Try compiling the shader. */
43  GL_CHECK(glCompileShader(*shader));
44  GLint status;
45  GL_CHECK(glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
46 
47  /* Dump debug info (source and log) if compilation failed. */
48  if(status != GL_TRUE)
49  {
50  GLint length;
51  char *debugSource = NULL;
52  char *errorLog = NULL;
53 
54  /* Get shader source. */
55  GL_CHECK(glGetShaderiv(*shader, GL_SHADER_SOURCE_LENGTH, &length));
56  debugSource = (char *)malloc(length);
57  GL_CHECK(glGetShaderSource(*shader, length, NULL, debugSource));
58  LOGE("Debug source START:\n%s\nDebug source END\n\n", debugSource);
59  free(debugSource);
60 
61  /* Now get the info log. */
62  GL_CHECK(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &length));
63  errorLog = (char *)malloc(length);
64  GL_CHECK(glGetShaderInfoLog(*shader, length, NULL, errorLog));
65  LOGE("Log START:\n%s\nLog END\n\n", errorLog);
66  free(errorLog);
67 
68  LOGE("Compilation FAILED!\n\n");
69  exit(1);
70  }
71  }
72 
73  char* Shader::loadShader(const char *filename)
74  {
75  FILE *file = fopen(filename, "rb");
76  if(file == NULL)
77  {
78  LOGE("Cannot read file '%s'\n", filename);
79  exit(1);
80  }
81  /* Seek end of file. */
82  fseek(file, 0, SEEK_END);
83  /* Record the size of the file for memory allocation. */
84  long length = ftell(file);
85  /* Seek start of file again. */
86  fseek(file, 0, SEEK_SET);
87  char *shader = (char *)calloc(length + 1, sizeof(char));
88  if(shader == NULL)
89  {
90  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
91  exit(1);
92  }
93  /* Read in the file */
94  size_t numberOfBytesRead = fread(shader, sizeof(char), length, file);
95  if (numberOfBytesRead != length)
96  {
97  LOGE("Error reading %s (read %d of %ld)", filename, numberOfBytesRead, length);
98  exit(1);
99  }
100  shader[length] = '\0';
101  fclose(file);
102 
103  return shader;
104  }
105 }
static char * loadShader(const char *filename)
Load shader source from a file into memory.
Definition: Shader.cpp:73
GLsizei const GLchar ** strings
Definition: gl2ext.h:1477
#define GL_CHECK(x)
Definition: AstcTextures.h:59
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
#define LOGE(...)
Definition: Shader.cpp:24
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count