1 #define STB_INCLUDE_IMPLEMENTATION
2 #define STB_INCLUDE_LINE_GLSL
3 #include "../dep/stb/stb_include.h"
15 static int uniform_count
;
17 static int compile_subshader( FILE *header
, char *name
)
20 char *full
= stb_include_file( name
, "", ".", error
);
24 fprintf( stderr
, "stb_include_file error:\n%s\n", error
);
31 fprintf( header
, "{\n"
32 ".orig_file = \"../../shaders/%s\",\n"
33 ".static_src = \n", name
);
35 char *cur
= full
, *start
= full
;
39 if( c
== '\n' || c
== '\0' )
42 fputs( "\"", header
);
43 fputs( start
, header
);
45 if( !strncmp(start
,"uniform",7) )
48 struct uniform
*uf
= &uniform_buffer
[ uniform_count
++ ];
51 if( start
[i
] == '\0' )
57 strncpy( uf
->name
, start
, sizeof(uf
->name
) );
63 strncpy( uf
->name
, start
, sizeof(uf
->name
) );
70 strncpy( uf
->type
, start
, sizeof(uf
->type
) );
79 fputs( "\"", header
);
83 fputs( "\\n\"\n", header
);
89 fputs( "},", header
);
96 int main( int argc
, char *argv
[] )
98 if( argc
< 2 || (argc
-1)%3 != 0 )
100 fprintf( stderr
, "invalid\n" );
105 int shader_count
= (argc
-1)/3;
106 for( int i
=0; i
<shader_count
; i
++ )
108 char **args
= &argv
[1+i
*3];
109 strcpy( path
, args
[0] );
110 strcat( path
, ".h" );
112 printf( "Compiling shader called '%s'\n", args
[0] );
114 FILE *header
= fopen( path
, "w" );
117 fprintf(stderr
, "Could not open '%s'\n", path
);
121 fprintf( header
, "#ifndef SHADER_%s_H\n"
122 "#define SHADER_%s_H\n", args
[0], args
[0] );
123 fprintf( header
, "static void shader_%s_link(void);\n", args
[0] );
124 fprintf( header
, "static void shader_%s_register(void);\n", args
[0] );
125 fprintf( header
, "static struct vg_shader _shader_%s = {\n"
127 " .link = shader_%s_link,\n"
128 " .vs = \n", args
[0], args
[0], args
[0] );
131 if( !compile_subshader(header
,args
[1]) )
137 fprintf( header
, "\n .fs = \n" );
138 if( !compile_subshader(header
,args
[2]) )
144 fprintf( header
, "\n};\n\n" );
146 for( int i
=0; i
<uniform_count
; i
++ )
148 struct uniform
*uf
= &uniform_buffer
[i
];
149 fprintf( header
, "static GLuint _uniform_%s_%s;\n", args
[0], uf
->name
);
152 for( int i
=0; i
<uniform_count
; i
++ )
154 struct uniform
*uf
= &uniform_buffer
[i
];
155 if( uf
->array
) continue;
157 if( !strcmp(uf
->type
,"vec2") )
159 fprintf( header
, "static void shader_%s_%s(v2f v){\n"
160 " glUniform2fv( _uniform_%s_%s, 1, v );\n"
161 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
163 if( !strcmp(uf
->type
,"vec3") )
165 fprintf( header
, "static void shader_%s_%s(v3f v){\n"
166 " glUniform3fv( _uniform_%s_%s, 1, v );\n"
167 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
169 if( !strcmp(uf
->type
,"vec4") )
171 fprintf( header
, "static void shader_%s_%s(v4f v){\n"
172 " glUniform4fv( _uniform_%s_%s, 1, v );\n"
173 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
175 if( !strcmp(uf
->type
,"sampler2D") )
177 fprintf( header
, "static void shader_%s_%s(int i){\n"
178 " glUniform1i( _uniform_%s_%s, i );\n"
179 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
181 if( !strcmp(uf
->type
,"float") )
183 fprintf( header
, "static void shader_%s_%s(float f){\n"
184 " glUniform1f( _uniform_%s_%s, f );\n"
185 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
187 if( !strcmp(uf
->type
,"mat4x3") )
190 "static void shader_%s_%s(m4x3f m){\n"
191 " glUniformMatrix4x3fv"
192 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
193 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
195 if( !strcmp(uf
->type
,"mat3") )
198 "static void shader_%s_%s(m3x3f m){\n"
199 " glUniformMatrix3fv"
200 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
201 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
203 if( !strcmp(uf
->type
,"mat4") )
206 "static void shader_%s_%s(m4x4f m){\n"
207 " glUniformMatrix4fv"
208 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
209 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
214 "static void shader_%s_register(void){\n"
215 " vg_shader_register( &_shader_%s );\n"
220 "static void shader_%s_use(void){ glUseProgram(_shader_%s.id); }\n",
224 "static void shader_%s_link(void){\n",
227 for( int i
=0; i
<uniform_count
; i
++ )
229 struct uniform
*uf
= &uniform_buffer
[i
];
232 "glGetUniformLocation( _shader_%s.id, \"%s\" );\n",
237 fprintf( header
, "}\n" );
238 fprintf( header
, "#endif /* SHADER_%s_H */\n", args
[0] );