heebie
[vg.git] / vg_build_utils_shader.h
1 #include "vg.h"
2
3 #ifdef VG_GAME
4 #error !
5 #endif
6
7 #define STB_INCLUDE_IMPLEMENTATION
8 #define STB_INCLUDE_LINE_GLSL
9 #include "submodules/stb/stb_include.h"
10
11 struct
12 {
13 struct uniform
14 {
15 char name[32];
16 char type[20];
17 char uniform_code_id[128];
18
19 int array;
20 }
21 uniform_buffer[100];
22
23 int uniform_count,
24 uniform_uid;
25 char shader_dir[ 256 ];
26 char current_shader_name[ 128 ];
27 }
28 static vg_shaderbuild;
29
30 static void vg_shader_set_include_dir( char *dir )
31 {
32 strcpy( vg_shaderbuild.shader_dir, dir );
33 }
34
35 static void parse_uniform_name( char *start, struct uniform *uf )
36 {
37 uf->array = 0;
38 int type_set = 0;
39
40 for( int i=0;; i++ )
41 {
42 if( start[i] == '\0' )
43 break;
44
45 if( start[i] == ';' )
46 {
47 start[i] = '\0';
48 strncpy( uf->name, start, sizeof(uf->name) );
49 }
50
51 if( start[i] == '[' )
52 {
53 start[i] = '\0';
54 strncpy( uf->name, start, sizeof(uf->name) );
55 uf->array = 1;
56 }
57
58 if( start[i] == ' ' )
59 {
60 start[i] = '\0';
61
62 if( !type_set )
63 {
64 strncpy( uf->type, start, sizeof(uf->type) );
65 type_set = 1;
66 }
67 start = start+i+1;
68 i=0;
69 }
70 }
71
72 snprintf( uf->uniform_code_id, 64, "_uniform_%s_%s",
73 vg_shaderbuild.current_shader_name,
74 uf->name );
75 }
76
77 static int compile_subshader( FILE *header, char *name )
78 {
79 char error[256];
80 char *full = stb_include_file( name, "", vg_shaderbuild.shader_dir, error );
81
82 if( !full )
83 {
84 fprintf( stderr, "stb_include_file error:\n%s\n", error );
85 return 0;
86 }
87 else
88 {
89 fprintf( header, "{\n"
90 ".orig_file = \"%s\",\n"
91 ".static_src = \n",
92 name );
93
94 char *cur = full, *start = full;
95 while( 1 )
96 {
97 char c = *cur;
98 if( c == '\n' || c == '\0' )
99 {
100 *cur = '\0';
101 fputs( "\"", header );
102 fputs( start, header );
103
104 if( !strncmp(start,"uniform",7) )
105 {
106 start += 8;
107 struct uniform *uf =
108 &vg_shaderbuild.uniform_buffer[
109 vg_shaderbuild.uniform_count ++ ];
110
111 parse_uniform_name( start, uf );
112 }
113
114 if( c == '\0' )
115 {
116 fputs( "\"", header );
117 break;
118 }
119
120 fputs( "\\n\"\n", header );
121 start = cur+1;
122 }
123 cur ++;
124 }
125
126 fputs( "},", header );
127 }
128
129 free( full );
130 return 1;
131 }
132
133 int vg_build_shader( char *src_vert, /* path/to/vert.vs */
134 char *src_frag, /* path/to/frag.fs */
135 char *src_geo, /* unused currently */
136 char *dst_h, /* folder where .h go */
137 char *name /* shader name */ )
138 {
139 char path[260];
140
141 strcpy( vg_shaderbuild.current_shader_name, name );
142
143 strcpy( path, dst_h );
144 strcat( path, "/" );
145 strcat( path, name );
146 strcat( path, ".h" );
147
148 printf( "Compiling shader called '%s'\n", name );
149
150 FILE *header = fopen( path, "w" );
151 if( !header )
152 {
153 fprintf(stderr, "Could not open '%s'\n", path );
154 return 0;
155 }
156
157 fprintf( header, "#ifndef SHADER_%s_H\n"
158 "#define SHADER_%s_H\n", name, name );
159 fprintf( header, "static void shader_%s_link(void);\n", name );
160 fprintf( header, "static void shader_%s_register(void);\n", name );
161 fprintf( header, "static struct vg_shader _shader_%s = {\n"
162 " .name = \"%s\",\n"
163 " .link = shader_%s_link,\n"
164 " .vs = \n", name, name, name );
165
166 vg_shaderbuild.uniform_count = 0;
167 if( !compile_subshader(header,src_vert) )
168 {
169 fclose( header );
170 return 0;
171 }
172
173 fprintf( header, "\n .fs = \n" );
174 if( !compile_subshader(header,src_frag) )
175 {
176 fclose( header );
177 return 0;
178 }
179
180 fprintf( header, "\n};\n\n" );
181
182 for( int i=0; i<vg_shaderbuild.uniform_count; i++ )
183 {
184 struct uniform *uf = &vg_shaderbuild.uniform_buffer[i];
185 fprintf( header, "static GLuint %s;\n", uf->uniform_code_id );
186 }
187
188 struct type_info
189 {
190 const char *glsl_type,
191 *args,
192 *gl_call_pattern;
193 }
194 types[] =
195 {
196 { "float", "float f", "glUniform1f(%s,f);" },
197
198 { "vec2", "v2f v", "glUniform2fv(%s,1,v);" },
199 { "vec3", "v3f v", "glUniform3fv(%s,1,v);" },
200 { "vec4", "v4f v", "glUniform4fv(%s,1,v);" },
201
202 { "sampler2D", "int i", "glUniform1i(%s,i);" },
203 { "mat4x3", "m4x3f m", "glUniformMatrix4x3fv(%s,1,GL_FALSE,(float*)m);" },
204 { "mat3", "m3x3f m", "glUniformMatrix3fv(%s,1,GL_FALSE,(float*)m);" },
205 { "mat4", "m4x4f m", "glUniformMatrix4fv(%s,1,GL_FALSE,(float*)m);" },
206 };
207
208 for( int i=0; i<vg_shaderbuild.uniform_count; i++ )
209 {
210 struct uniform *uf = &vg_shaderbuild.uniform_buffer[i];
211 if( uf->array )
212 continue;
213
214 for( int j=0; j<vg_list_size(types); j ++ )
215 {
216 struct type_info *inf = &types[j];
217
218 if( !strcmp( inf->glsl_type, uf->type ) )
219 {
220 fprintf( header, "static void shader_%s_%s(%s){\n",
221 name, uf->name, inf->args );
222 fprintf( header, " " );
223 fprintf( header, inf->gl_call_pattern, uf->uniform_code_id );
224 fprintf( header, "\n}\n" );
225 }
226 }
227 }
228
229 fprintf( header,
230 "static void shader_%s_register(void){\n"
231 " vg_shader_register( &_shader_%s );\n"
232 "}\n",
233 name,name );
234
235 fprintf( header,
236 "static void shader_%s_use(void){ glUseProgram(_shader_%s.id); }\n",
237 name, name );
238
239 fprintf( header,
240 "static void shader_%s_link(void){\n",
241 name );
242
243 for( int i=0; i<vg_shaderbuild.uniform_count; i++ )
244 {
245 struct uniform *uf = &vg_shaderbuild.uniform_buffer[i];
246 fprintf( header,
247 " _uniform_%s_%s = "
248 "glGetUniformLocation( _shader_%s.id, \"%s\" );\n",
249 name, uf->name,
250 name, uf->name );
251 }
252
253 fprintf( header, "}\n" );
254 fprintf( header, "#endif /* SHADER_%s_H */\n", name );
255 fclose( header );
256
257 return 1;
258 }