-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc4g_glsl.cpp
226 lines (165 loc) · 4.26 KB
/
c4g_glsl.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
** C4GPU.
**
** For the latest info, see https://github.com/c4gpu/c4gpu_runtime/
**
** Copyright (C) 2017 Wang Renxin. All rights reserved.
*/
#include "c4g_glsl.hpp"
#include <string.h>
namespace c4g {
namespace gl {
Shader::Shader() {
}
Shader::Shader(ShaderTypes type) : _type(type) {
}
Shader::~Shader() {
if (_code)
free(_code);
}
bool Shader::readFile(const char* const file) {
// Gets file length.
FILE* fp = fopen(file, "rb");
int l = -1;
if (!fp)
return false;
fseek(fp, 0, SEEK_END);
l = (int)(ftell(fp) + 1);
// Reads code.
_code = (GLchar*)malloc(sizeof(GLchar) * l);
// Get the shader from a file.
fseek(fp, 0, SEEK_SET);
l = (int)fread(_code, sizeof(GLchar), l, fp);
_code[l] = '\0';
if (ferror(fp))
l = 0;
fclose(fp);
if (!l) {
printf("Cannot read the file %s.\n", file);
return false;
}
return true;
}
bool Shader::readString(const char* const str) {
if (!str)
return false;
#ifdef C4G_RUNTIME_OS_WIN
_code = _strdup(str);
#else /* C4G_RUNTIME_OS_WIN */
_code = strdup(str);
#endif /* C4G_RUNTIME_OS_WIN */
return true;
}
bool Shader::compile(const SimpleErrorHandler &&callback) {
if (!_code) return false;
GLint status = 0;
switch (_type) {
case ST_VERT:
_object = glCreateShader(GL_VERTEX_SHADER);
break;
case ST_FRAG:
_object = glCreateShader(GL_FRAGMENT_SHADER);
break;
default:
return false;
}
glShaderSource(_object, 1, &_code, nullptr);
glCompileShader(_object);
glGetShaderiv(_object, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char errorLog[1024];
GLsizei len = 0;
glGetShaderInfoLog(_object, sizeof(errorLog), &len, errorLog);
if (callback != nullptr)
callback(errorLog);
else
printf("%s\n", errorLog);
return false;
}
return true;
}
GLuint Shader::object(void) {
return _object;
}
Program::Program() {
}
Program::~Program() {
if (_prog) {
glDeleteProgram(_prog);
_prog = 0;
}
}
bool Program::link(Shader &&vert, Shader &&frag) {
GLint linked = 0;
std::swap(_vert, vert);
std::swap(_frag, frag);
_prog = glCreateProgram();
glAttachShader(_prog, _vert.object());
glAttachShader(_prog, _frag.object());
glLinkProgram(_prog);
glGetProgramiv(_prog, GL_LINK_STATUS, &linked);
glDetachShader(_prog, _vert.object());
glDetachShader(_prog, _frag.object());
glDeleteShader(_vert.object());
glDeleteShader(_frag.object());
if (!linked)
return false;
return true;
}
bool Program::link(Shader &&vert, Shader &&frag, const char* const varyings[], size_t vs) {
GLint linked = 0;
std::swap(_vert, vert);
std::swap(_frag, frag);
_prog = glCreateProgram();
glAttachShader(_prog, _vert.object());
glAttachShader(_prog, _frag.object());
glTransformFeedbackVaryings(_prog, (GLsizei)vs, varyings, GL_SEPARATE_ATTRIBS);
glLinkProgram(_prog);
glGetProgramiv(_prog, GL_LINK_STATUS, &linked);
glDetachShader(_prog, _vert.object());
glDetachShader(_prog, _frag.object());
glDeleteShader(_vert.object());
glDeleteShader(_frag.object());
if (!linked)
return false;
return true;
}
bool Program::use(void) {
glUseProgram(_prog);
return true;
}
GLint Program::attributeLocation(const char* const name) {
return glGetAttribLocation(_prog, name);
}
GLint Program::uniformLocation(const char* const name) {
return glGetUniformLocation(_prog, name);
}
void Program::uniform(int loc, float f0) {
glUniform1f((GLint)loc, f0);
}
void Program::uniform(int loc, float f0, float f1) {
glUniform2f((GLint)loc, f0, f1);
}
void Program::uniform(int loc, float f0, float f1, float f2) {
glUniform3f((GLint)loc, f0, f1, f2);
}
void Program::uniform(int loc, float f0, float f1, float f2, float f3) {
glUniform4f((GLint)loc, f0, f1, f2, f3);
}
void Program::uniform(int loc, int i0) {
glUniform1i((GLint)loc, i0);
}
void Program::uniform(int loc, int i0, int i1) {
glUniform2i((GLint)loc, i0, i1);
}
void Program::uniform(int loc, int i0, int i1, int i2) {
glUniform3i((GLint)loc, i0, i1, i2);
}
void Program::uniform(int loc, int i0, int i1, int i2, int i3) {
glUniform4i((GLint)loc, i0, i1, i2, i3);
}
GLuint Program::object(void) {
return _prog;
}
}
}