50e8bd0e291589a17d32b17fe1c11f0038e9234e
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
8 #include "vg/vg_platform.h"
10 #define STB_INCLUDE_IMPLEMENTATION
11 #define STB_INCLUDE_LINE_GLSL
12 #define STB_MALLOC vg_alloc
13 #define STB_FREE vg_free
14 #define STB_REALLOC vg_realloc
15 #include "submodules/stb/stb_include.h"
17 const char *vg_shader_gl_ver
= "#version 330 core\n";
19 typedef struct vg_shader vg_shader
;
30 const char *orig_file
,
43 VG_STATIC GLuint
vg_shader_subshader( const char *src
, GLint gliShaderType
)
45 GLint shader
= glCreateShader( gliShaderType
);
47 if( shader
== GL_NONE
)
49 vg_error( "Could not 'glCreateShader()'\n" );
53 glShaderSource( shader
, 2, (const char*[2]){ vg_shader_gl_ver
, src
}, NULL
);
54 glCompileShader( shader
);
57 glGetShaderiv( shader
, GL_COMPILE_STATUS
, &status
);
59 if( status
!= GL_TRUE
)
64 glGetShaderInfoLog( shader
, sizeof(info
), &len
, info
);
65 vg_error( "Error info:\n%s\n", info
);
72 VG_STATIC
int vg_shader_compile( struct vg_shader
*shader
)
74 GLuint program
, vert
, frag
;
76 /* If we are compiling this again, we obviously need to try to take the src
77 * from the disk instead.
79 * Only do this if we have filenames set on the shader, so engine shaders
80 * dont have to do it (text.. etc).
83 int use_source_files
= 0;
84 if( shader
->compiled
)
86 if( shader
->vs
.orig_file
&& shader
->fs
.orig_file
)
92 vg_warn( "No source files for shader '%s'\n", shader
->name
);
97 vg_info( "Compile shader '%s'\n", shader
->name
);
99 if( use_source_files
)
104 strcpy( path
, "../../" );
105 strcat( path
, shader
->vs
.orig_file
);
106 char *vertex_src
= stb_include_file( path
, "", "../../shaders", error
);
108 strcpy( path
, "../../" );
109 strcat( path
, shader
->fs
.orig_file
);
110 char *fragment_src
= stb_include_file( path
, "", "../../shaders", error
);
112 if( !vertex_src
|| !fragment_src
)
114 vg_error( "Could not find shader source files (%s)\n",
115 shader
->vs
.orig_file
);
118 free( fragment_src
);
122 vert
= vg_shader_subshader( vertex_src
, GL_VERTEX_SHADER
);
123 frag
= vg_shader_subshader( fragment_src
, GL_FRAGMENT_SHADER
);
126 free( fragment_src
);
130 vert
= vg_shader_subshader( shader
->vs
.static_src
, GL_VERTEX_SHADER
);
131 frag
= vg_shader_subshader( shader
->fs
.static_src
, GL_FRAGMENT_SHADER
);
137 program
= glCreateProgram();
139 glAttachShader( program
, vert
);
140 glAttachShader( program
, frag
);
141 glLinkProgram( program
);
143 glDeleteShader( vert
);
144 glDeleteShader( frag
);
146 /* Check for link errors */
148 int success_link
= 1;
150 glGetProgramiv( program
, GL_LINK_STATUS
, &success_link
);
153 glGetProgramInfoLog( program
, 512, NULL
, infoLog
);
154 vg_error( "Link failed: %s\n", infoLog
);
155 glDeleteProgram( program
);
159 if( shader
->compiled
)
160 glDeleteProgram( shader
->id
);
162 shader
->id
= program
;
163 shader
->compiled
= 1;
169 VG_STATIC
void vg_free_shader( struct vg_shader
*shader
)
171 if( shader
->compiled
)
173 glDeleteProgram( shader
->id
);
174 shader
->compiled
= 0;
178 VG_STATIC
void vg_shaders_compile(void)
180 vg_info( "Compiling shaders\n" );
182 for( int i
=0; i
<vg_shaders
.count
; i
++ )
184 vg_shader
*shader
= vg_shaders
.shaders
[i
];
186 if( !vg_shader_compile( shader
) )
187 vg_fatal_exit_loop( "Failed to compile shader" );
191 VG_STATIC
int vg_shaders_live_recompile(int argc
, const char *argv
[])
193 vg_info( "Recompiling shaders\n" );
194 for( int i
=0; i
<vg_shaders
.count
; i
++ )
196 struct vg_shader
*shader
= vg_shaders
.shaders
[i
];
197 vg_shader_compile( shader
);
203 VG_STATIC
void vg_shader_register( struct vg_shader
*shader
)
205 if( vg_shaders
.count
== vg_list_size(vg_shaders
.shaders
) )
206 vg_fatal_exit_loop( "Too many shaders" );
208 shader
->compiled
= 0;
209 shader
->id
= 0; /* TODO: make this an error shader */
210 vg_shaders
.shaders
[ vg_shaders
.count
++ ] = shader
;
213 #endif /* VG_SHADER_H */