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
++ ];
52 if( start
[i
] == '\0' )
58 strncpy( uf
->name
, start
, sizeof(uf
->name
) );
64 strncpy( uf
->name
, start
, sizeof(uf
->name
) );
71 strncpy( uf
->type
, start
, sizeof(uf
->type
) );
80 fputs( "\"", header
);
84 fputs( "\\n\"\n", header
);
90 fputs( "},", header
);
97 int main( int argc
, char *argv
[] )
99 if( argc
< 2 || (argc
-1)%3 != 0 )
101 fprintf( stderr
, "invalid\n" );
106 int shader_count
= (argc
-1)/3;
107 for( int i
=0; i
<shader_count
; i
++ )
109 char **args
= &argv
[1+i
*3];
110 strcpy( path
, args
[0] );
111 strcat( path
, ".h" );
113 printf( "Compiling shader called '%s'\n", args
[0] );
115 FILE *header
= fopen( path
, "w" );
118 fprintf(stderr
, "Could not open '%s'\n", path
);
122 fprintf( header
, "#ifndef SHADER_%s_H\n"
123 "#define SHADER_%s_H\n", args
[0], args
[0] );
124 fprintf( header
, "static void shader_%s_link(void);\n", args
[0] );
125 fprintf( header
, "static void shader_%s_register(void);\n", args
[0] );
126 fprintf( header
, "static struct vg_shader _shader_%s = {\n"
128 " .link = shader_%s_link,\n"
129 " .vs = \n", args
[0], args
[0], args
[0] );
132 if( !compile_subshader(header
,args
[1]) )
138 fprintf( header
, "\n .fs = \n" );
139 if( !compile_subshader(header
,args
[2]) )
145 fprintf( header
, "\n};\n\n" );
147 for( int i
=0; i
<uniform_count
; i
++ )
149 struct uniform
*uf
= &uniform_buffer
[i
];
150 fprintf( header
, "static GLuint _uniform_%s_%s;\n", args
[0], uf
->name
);
153 for( int i
=0; i
<uniform_count
; i
++ )
155 struct uniform
*uf
= &uniform_buffer
[i
];
156 if( uf
->array
) continue;
158 if( !strcmp(uf
->type
,"vec2") )
160 fprintf( header
, "static void shader_%s_%s(v2f v){\n"
161 " glUniform2fv( _uniform_%s_%s, 1, v );\n"
162 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
164 if( !strcmp(uf
->type
,"vec3") )
166 fprintf( header
, "static void shader_%s_%s(v3f v){\n"
167 " glUniform3fv( _uniform_%s_%s, 1, v );\n"
168 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
170 if( !strcmp(uf
->type
,"vec4") )
172 fprintf( header
, "static void shader_%s_%s(v4f v){\n"
173 " glUniform4fv( _uniform_%s_%s, 1, v );\n"
174 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
176 if( !strcmp(uf
->type
,"sampler2D") )
178 fprintf( header
, "static void shader_%s_%s(int i){\n"
179 " glUniform1i( _uniform_%s_%s, i );\n"
180 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
182 if( !strcmp(uf
->type
,"float") )
184 fprintf( header
, "static void shader_%s_%s(float f){\n"
185 " glUniform1f( _uniform_%s_%s, f );\n"
186 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
188 if( !strcmp(uf
->type
,"mat4x3") )
191 "static void shader_%s_%s(m4x3f m){\n"
192 " glUniformMatrix4x3fv"
193 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
194 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
196 if( !strcmp(uf
->type
,"mat3") )
199 "static void shader_%s_%s(m3x3f m){\n"
200 " glUniformMatrix3fv"
201 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
202 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
204 if( !strcmp(uf
->type
,"mat4") )
207 "static void shader_%s_%s(m4x4f m){\n"
208 " glUniformMatrix4fv"
209 "( _uniform_%s_%s, 1, GL_FALSE, (float *)m );\n"
210 "}\n", args
[0], uf
->name
, args
[0], uf
->name
);
215 "static void shader_%s_register(void){\n"
216 " vg_shader_register( &_shader_%s );\n"
221 "static void shader_%s_use(void){ glUseProgram(_shader_%s.id); }\n",
225 "static void shader_%s_link(void){\n",
228 for( int i
=0; i
<uniform_count
; i
++ )
230 struct uniform
*uf
= &uniform_buffer
[i
];
233 "glGetUniformLocation( _shader_%s.id, \"%s\" );\n",
238 fprintf( header
, "}\n" );
239 fprintf( header
, "#endif /* SHADER_%s_H */\n", args
[0] );