#ifndef VG_SHADER_H
#define VG_SHADER_H
+#define VG_GAME
#include "vg/vg.h"
#include "vg/vg_platform.h"
-#if 0
#define STB_INCLUDE_IMPLEMENTATION
#define STB_INCLUDE_LINE_GLSL
#define STB_MALLOC vg_alloc
#define STB_FREE vg_free
#define STB_REALLOC vg_realloc
-#include "stb/stb_include.h"
-#endif
+#include "submodules/stb/stb_include.h"
const char *vg_shader_gl_ver = "#version 330 core\n";
}
static vg_shaders;
-VG_STATIC GLuint vg_shader_subshader( const char *src, GLint gliShaderType )
+static GLuint vg_shader_subshader( const char *src, GLint gliShaderType )
{
GLint shader = glCreateShader( gliShaderType );
return shader;
}
-VG_STATIC int vg_shader_compile( struct vg_shader *shader )
+static int vg_shader_compile( struct vg_shader *shader )
{
- vg_info( "Compile shader '%s'\n", shader->name );
-
GLuint program, vert, frag;
- const char *svs, *sfs;
- char *avs, *afs;
- int static_src = 1;
-
/* If we are compiling this again, we obviously need to try to take the src
* from the disk instead.
*
* Only do this if we have filenames set on the shader, so engine shaders
* dont have to do it (text.. etc).
*/
- if( shader->compiled )
- {
- if( shader->vs.orig_file && shader->fs.orig_file )
- static_src = 0;
- else return 1;
- }
- if( static_src )
- {
- svs = shader->vs.static_src;
- sfs = shader->fs.static_src;
+ int use_source_files = 0;
+ if( shader->compiled ){
+ if( shader->vs.orig_file && shader->fs.orig_file ){
+ use_source_files = 1;
+ }
+ else {
+ vg_warn( "No source files for shader '%s'\n", shader->name );
+ return 1;
+ }
}
- else
- {
- vg_fatal_exit_loop( "Unimplemented" );
-#if 0
+ vg_info( "Compile shader '%s'\n", shader->name );
+
+ if( use_source_files ){
char error[260];
char path[260];
- strcpy( path, shader->vs.orig_file );
- avs = stb_include_file( path, "", "../../shaders", error );
- strcpy( path, shader->fs.orig_file );
- afs = stb_include_file( path, "", "../../shaders", error );
-
- if( !avs || !afs )
- {
- vg_error( "Could not find shader source files (%s)\n",
- shader->vs.orig_file );
+ strcpy( path, "../../" );
+ strcat( path, shader->vs.orig_file );
+ char *vertex_src = stb_include_file( path, "", "../../shaders", error );
- vg_free( avs );
- vg_free( afs );
- return 0;
+ strcpy( path, "../../" );
+ strcat( path, shader->fs.orig_file );
+ char *fragment_src = stb_include_file( path, "", "../../shaders", error );
+
+ if( !vertex_src || !fragment_src ){
+ const char *errstr = "Could not find shader source files (%s)\n";
+ if( shader->compiled ){
+ vg_warn( errstr, shader->vs.orig_file );
+ free( vertex_src );
+ free( fragment_src );
+ return 1;
+ }
+ else{
+ vg_error( errstr, shader->vs.orig_file );
+ free( vertex_src );
+ free( fragment_src );
+ return 0;
+ }
}
- svs = avs;
- sfs = afs;
-#endif
+ vert = vg_shader_subshader( vertex_src, GL_VERTEX_SHADER );
+ frag = vg_shader_subshader( fragment_src, GL_FRAGMENT_SHADER );
+
+ free( vertex_src );
+ free( fragment_src );
}
-
- vert = vg_shader_subshader( svs, GL_VERTEX_SHADER );
- frag = vg_shader_subshader( sfs, GL_FRAGMENT_SHADER );
-
-#if 0
- if( !static_src )
- {
- free( avs );
- free( afs );
+ else{
+ vert = vg_shader_subshader( shader->vs.static_src, GL_VERTEX_SHADER );
+ frag = vg_shader_subshader( shader->fs.static_src, GL_FRAGMENT_SHADER );
}
-#endif
-
+
if( !vert || !frag )
return 0;
return 1;
}
-VG_STATIC void vg_free_shader( struct vg_shader *shader )
+static void vg_free_shader( struct vg_shader *shader )
{
if( shader->compiled )
{
}
}
-VG_STATIC void vg_shaders_compile(void)
+static void vg_shaders_compile(void)
{
vg_info( "Compiling shaders\n" );
- for( int i=0; i<vg_shaders.count; i ++ )
- {
+ for( int i=0; i<vg_shaders.count; i ++ ){
vg_shader *shader = vg_shaders.shaders[i];
if( !vg_shader_compile( shader ) )
- vg_fatal_exit_loop( "Failed to compile shader" );
+ vg_fatal_error( "Failed to compile shader" );
}
}
-VG_STATIC int vg_shaders_live_recompile(int argc, const char *argv[])
+static int vg_shaders_live_recompile(int argc, const char *argv[])
{
vg_info( "Recompiling shaders\n" );
for( int i=0; i<vg_shaders.count; i ++ )
return 0;
}
-VG_STATIC void vg_shader_register( struct vg_shader *shader )
+static void vg_shader_register( struct vg_shader *shader )
{
if( vg_shaders.count == vg_list_size(vg_shaders.shaders) )
- vg_fatal_exit_loop( "Too many shaders" );
+ vg_fatal_error( "Too many shaders" );
shader->compiled = 0;
shader->id = 0; /* TODO: make this an error shader */