-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.sj
261 lines (228 loc) · 7.04 KB
/
shader.sj
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
boxShader : shader("shaders/v3f-c4f.vert", "shaders/v3f-c4f.frag")
imageShader : shader("shaders/v3f-t2f.vert", "shaders/v3f-t2f.frag")
phongColorShader : shader("shaders/v3f-n3f-phong.vert", "shaders/v3f-n3f-phong.frag")
phongTextureShader : shader("shaders/v3f-t2f-n3f-phong.vert", "shaders/v3f-t2f-n3f-phong.frag")
textShader : shader("shaders/v3f-t2f-c4f.vert", "shaders/v3f-t2f-c4f.frag")
shader(
vertex : 'string
pixel : 'string
--cvar--
GLuint id;
--cvar--
) {
--c--
_this->id = shader_load((char*)_this->vertex.data.data, (char*)_this->pixel.data.data);
--c--
this
} copy {
--c--
_this->id = _from->id;
glid_retain(_this->id);
--c--
} destroy {
--c--
if (glid_release(_this->id)) {
glDeleteShader(_this->id);
}
--c--
}
--cdefine--
/* Freetype GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
/**
* Read a fragment or vertex shader from a file
*
* @param filename file to read shader from
* @return a newly-allocated text buffer containing code. This buffer
* must be freed after usage.
*
*/
char *
shader_read( const char *filename );
/**
* Compile a shader from a text buffer.
*
* @param source code of the shader
* @param type type of the shader
*
* @return a handle on the compiled program
*
*/
GLuint
shader_compile( const char* source,
const GLenum type );
/**
* Load a vertex and fragment shader sources and build program
*
* @param vert_filename vertex shader filename
* @param frag_filename fragment shader filename
*
* @return a handle on the built program
*
*/
GLuint
shader_load( const char * vert_filename,
const char * frag_filename );
--cdefine--
--cfunction--
#include(<stdio.h>)
#include(<stdlib.h>)
#include(<string.h>)
char *repl_str(const char *str, const char *from, const char *to) {
/* Adjust each of the below values to suit your needs. */
/* Increment positions cache size initially by this number. */
size_t cache_sz_inc = 16;
/* Thereafter, each time capacity needs to be increased,
* multiply the increment by this factor. */
const size_t cache_sz_inc_factor = 3;
/* But never increment capacity by more than this number. */
const size_t cache_sz_inc_max = 1048576;
char *pret, *ret = NULL;
const char *pstr2, *pstr = str;
size_t i, count = 0;
##if (__STDC_VERSION__ >= 199901L)
uintptr_t *pos_cache_tmp, *pos_cache = NULL;
##else
ptrdiff_t *pos_cache_tmp, *pos_cache = NULL;
##endif
size_t cache_sz = 0;
size_t cpylen, orglen, retlen, tolen = 0, fromlen = strlen(from);
/* Find all matches and cache their positions. */
while ((pstr2 = strstr(pstr, from)) != NULL) {
count++;
/* Increase the cache size when necessary. */
if (cache_sz < count) {
cache_sz += cache_sz_inc;
pos_cache_tmp = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
if (pos_cache_tmp == NULL) {
goto end_repl_str;
} else pos_cache = pos_cache_tmp;
cache_sz_inc *= cache_sz_inc_factor;
if (cache_sz_inc > cache_sz_inc_max) {
cache_sz_inc = cache_sz_inc_max;
}
}
pos_cache[count-1] = pstr2 - str;
pstr = pstr2 + fromlen;
}
orglen = pstr - str + strlen(pstr);
/* Allocate memory for the post-replacement string. */
if (count > 0) {
tolen = strlen(to);
retlen = orglen + (tolen - fromlen) * count;
} else retlen = orglen;
ret = malloc(retlen + 1);
if (ret == NULL) {
goto end_repl_str;
}
if (count == 0) {
/* If no matches, then just duplicate the string. */
strcpy(ret, str);
} else {
/* Otherwise, duplicate the string whilst performing
* the replacements using the position cache. */
pret = ret;
memcpy(pret, str, pos_cache[0]);
pret += pos_cache[0];
for (i = 0; i < count; i++) {
memcpy(pret, to, tolen);
pret += tolen;
pstr = str + pos_cache[i] + fromlen;
cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
memcpy(pret, pstr, cpylen);
pret += cpylen;
}
ret[retlen] = '\0';
}
end_repl_str:
/* Free the cache and return the post-replacement string,
* which will be NULL in the event of an error. */
free(pos_cache);
return ret;
}
// ------------------------------------------------------------ shader_read ---
char *
shader_read( const char *filename )
{
FILE * file;
char * buffer;
size_t size;
##ifdef WIN32
errno_t err;
if( (err = fopen_s( &file, filename, "rb" )) !=0 ) {
##else
file = fopen( filename, "rb" );
if( !file ) {
##endif
fprintf( stderr, "Unable to open file \"%s\".\n", filename );
return 0;
}
fseek( file, 0, SEEK_END );
size = ftell( file );
fseek(file, 0, SEEK_SET );
buffer = (char *) malloc( (size+1) * sizeof( char *) );
fread( buffer, sizeof(char), size, file );
buffer[size] = 0;
fclose( file );
##ifdef __APPLE__
char* prev = buffer;
buffer = repl_str(buffer, "mediump", "");
free(prev);
##endif
return buffer;
}
// --------------------------------------------------------- shader_compile ---
GLuint
shader_compile( const char* source,
const GLenum type )
{
GLint compile_status;
GLuint handle = glCreateShader( type );
glShaderSource( handle, 1, &source, 0 );
glCompileShader( handle );
glGetShaderiv( handle, GL_COMPILE_STATUS, &compile_status );
if( compile_status == GL_FALSE )
{
GLchar messages[256];
glGetShaderInfoLog( handle, sizeof(messages), 0, &messages[0] );
halt("%s: %s\n", source, messages);
}
return handle;
}
// ------------------------------------------------------------ shader_load ---
GLuint
shader_load( const char * vert_filename,
const char * frag_filename )
{
GLuint handle = glCreateProgram( );
GLint link_status;
if( vert_filename && strlen( vert_filename ) )
{
char *vert_source = shader_read( vert_filename );
GLuint vert_shader = shader_compile( vert_source, GL_VERTEX_SHADER);
glAttachShader( handle, vert_shader);
glDeleteShader( vert_shader );
free( vert_source );
}
if( frag_filename && strlen( frag_filename ) )
{
char *frag_source = shader_read( frag_filename );
GLuint frag_shader = shader_compile( frag_source, GL_FRAGMENT_SHADER);
glAttachShader( handle, frag_shader);
glDeleteShader( frag_shader );
free( frag_source );
}
glLinkProgram( handle );
glGetProgramiv( handle, GL_LINK_STATUS, &link_status );
if (link_status == GL_FALSE)
{
GLchar messages[256];
glGetProgramInfoLog( handle, sizeof(messages), 0, &messages[0] );
halt("%s\n", messages );
}
return handle;
}
--cfunction--