-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshader.cpp
126 lines (111 loc) · 3.67 KB
/
shader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#define LOG_TAG "Shader"
#include "shader.h"
#include <stdio.h>
#include <memory>
// Given shader source, load and compile it
static GLuint loadShader(GLenum type, const char *shaderSrc, const char *name)
{
// Create the shader object
GLuint shader = glCreateShader(type);
if (shader == 0)
{
return 0;
}
// Load and compile the shader
glShaderSource(shader, 1, &shaderSrc, nullptr);
glCompileShader(shader);
// Verify the compilation worked as expected
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
ALOGD("Error compiling %s shader for %s\n", (type == GL_VERTEX_SHADER) ? "vtx" : "pxl", name);
GLint size = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
if (size > 0)
{
// Get and report the error message
std::unique_ptr<char> infoLog(new char[size]);
glGetShaderInfoLog(shader, size, NULL, infoLog.get());
ALOGD(" msg:\n%s\n", infoLog.get());
}
glDeleteShader(shader);
return 0;
}
return shader;
}
// Create a program object given vertex and pixels shader source
GLuint buildShaderProgram(const char *vtxSrc, const char *pxlSrc, const char *name)
{
GLuint program = glCreateProgram();
if (program == 0)
{
ALOGD("Failed to allocate program object\n");
return 0;
}
// Compile the shaders and bind them to this program
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vtxSrc, name);
if (vertexShader == 0)
{
ALOGD("Failed to load vertex shader\n");
glDeleteProgram(program);
return 0;
}
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pxlSrc, name);
if (pixelShader == 0)
{
ALOGD("Failed to load pixel shader\n");
glDeleteProgram(program);
glDeleteShader(vertexShader);
return 0;
}
glAttachShader(program, vertexShader);
glAttachShader(program, pixelShader);
// Link the program
glLinkProgram(program);
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked)
{
ALOGD("Error linking program.\n");
GLint size = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &size);
if (size > 0)
{
// Get and report the error message
std::unique_ptr<char> infoLog(new char[size]);
glGetProgramInfoLog(program, size, NULL, infoLog.get());
ALOGD(" msg: %s\n", infoLog.get());
}
glDeleteProgram(program);
glDeleteShader(vertexShader);
glDeleteShader(pixelShader);
return 0;
}
#if 0 // Debug output to diagnose shader parameters
GLint numShaderParams;
GLchar paramName[128];
GLint paramSize;
GLenum paramType;
const char *typeName = "?";
ALOGD("Shader parameters for %s:\n", name);
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numShaderParams);
for (GLint i=0; i<numShaderParams; i++) {
glGetActiveUniform(program,
i,
sizeof(paramName),
nullptr,
¶mSize,
¶mType,
paramName);
switch (paramType) {
case GL_FLOAT: typeName = "GL_FLOAT"; break;
case GL_FLOAT_VEC4: typeName = "GL_FLOAT_VEC4"; break;
case GL_FLOAT_MAT4: typeName = "GL_FLOAT_MAT4"; break;
case GL_SAMPLER_2D: typeName = "GL_SAMPLER_2D"; break;
}
ALOGD(" %2d: %s\t (%d) of type %s(%d)\n", i, paramName, paramSize, typeName, paramType);
}
#endif
return program;
}