allow option for backfaces in ray_triangle
[vg.git] / vg_loader.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
4 *
5 * Splash / load screen
6 *
7 * -----------------------------------------------------------------------------
8 */
9
10 #ifndef VG_LOADER_H
11 #define VG_LOADER_H
12
13 #define VG_GAME
14 #include "vg/vg.h"
15 #include "vg/vg_shader.h"
16
17 static void vg_loader_start( void(*pfn)(void *data), void *data );
18 static void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) );
19
20 static struct vg_shader _shader_loader =
21 {
22 .name = "[vg] loader",
23 .link = NULL,
24
25 /* This is the new foreground shader */
26 .vs =
27 {
28 .orig_file = NULL,
29 .static_src = ""
30 "layout (location=0) in vec2 a_co;"
31 "out vec2 aUv;"
32 "void main()"
33 "{"
34 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
35 "aUv = a_co;"
36 "}"
37 },
38 .fs =
39 {
40 .orig_file = NULL,
41 .static_src =
42
43 "out vec4 FragColor;"
44 "uniform float uTime;"
45 "uniform float uRatio;"
46 "uniform float uOpacity;"
47 "in vec2 aUv;"
48
49 "float eval_zero( vec2 uv )"
50 "{"
51 "vec4 vsines = sin( (uTime+uv.y*80.0) * vec4(1.1,2.0234,3.73,2.444) );"
52 "float gradient = min( uv.y, 0.0 );"
53 "float offset = vsines.x*vsines.y*vsines.z*vsines.w*gradient;"
54
55 "vec2 vpos = uv + vec2( offset, 0.0 );"
56 "float dist = dot( vpos, vpos );"
57
58 "float fring = step(0.1*0.1,dist) * step(dist,0.15*0.15);"
59 "return max( 0.0, fring * 1.0+gradient*6.0 );"
60 "}"
61
62 "void main()"
63 "{"
64 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
65
66 "vec2 uvx = aUv - vec2( 0.5 );"
67 "uvx.x *= uRatio;"
68 "uvx.y *= 0.75;"
69
70 "float zero = eval_zero( uvx );"
71
72 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
73 "float fmt1 = step( 0.5, zero*zero + dither )*0.8+0.2;"
74
75 "FragColor = vec4(vec3(fmt1),uOpacity);"
76 "}"
77 }
78 };
79
80 static struct vg_loader
81 {
82 /* Shutdown steps */
83 struct loader_free_step{
84 void (*fn_free)(void);
85 }
86 step_buffer[16];
87 u32 step_count, step_action;
88
89 GLuint vao, vbo;
90 }
91 vg_loader;
92
93 static void _vg_loader_init(void)
94 {
95 float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
96 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
97
98 glGenVertexArrays( 1, &vg_loader.vao );
99 glGenBuffers( 1, &vg_loader.vbo );
100 glBindVertexArray( vg_loader.vao );
101 glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
102 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
103 glBindVertexArray( vg_loader.vao );
104 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
105 glEnableVertexAttribArray( 0 );
106
107 VG_CHECK_GL_ERR();
108
109 if( !vg_shader_compile( &_shader_loader ) )
110 vg_fatal_error( "failed to compile shader" );
111 }
112
113 static void _vg_loader_free(void)
114 {
115 vg_info( "vg_loader_free\n" );
116 glDeleteVertexArrays( 1, &vg_loader.vao );
117 glDeleteBuffers( 1, &vg_loader.vbo );
118
119 for( int i=0; i<vg_loader.step_count; i++ )
120 {
121 struct loader_free_step *step =
122 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
123
124 vg_info( " -> %p\n", step->fn_free );
125 step->fn_free();
126 }
127 }
128
129 static void _vg_render_log(void)
130 {
131 #if 0
132 ui_begin( vg.window_x, vg.window_y );
133 SDL_AtomicLock( &log_print_sl );
134
135 int const fh = 14;
136 int lines_screen_max = ((vg.window_y/fh)-2),
137 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
138 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
139
140 int ptr = vg_log.buffer_line_current;
141
142 vg_uictx.cursor[0] = 0;
143 vg_uictx.cursor[1] = lines_to_draw*fh;
144 vg_uictx.cursor[3] = fh;
145 ui_fill_x();
146
147 for( int i=0; i<lines_to_draw; i ++ ){
148 ptr --;
149
150 if( ptr < 0 )
151 ptr = vg_list_size( vg_log.buffer )-1;
152
153 ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
154 vg_uictx.cursor[1] -= fh;
155 }
156
157 SDL_AtomicUnlock( &log_print_sl );
158
159 ui_resolve();
160 ui_draw( NULL );
161 #endif
162 }
163
164 static void _vg_loader_render_ring( f32 opacity ){
165 glEnable(GL_BLEND);
166 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167 glBlendEquation(GL_FUNC_ADD);
168
169 opacity *= opacity;
170
171 glUseProgram( _shader_loader.id );
172 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
173 f32 ratio = (f32)vg.window_x / (f32)vg.window_y;
174 glUniform1f( glGetUniformLocation( _shader_loader.id, "uRatio"), ratio );
175 glUniform1f( glGetUniformLocation( _shader_loader.id, "uOpacity"), opacity );
176 glBindVertexArray( vg_loader.vao );
177 glDrawArrays( GL_TRIANGLES, 0, 6 );
178 }
179
180 static void _vg_loader_render(void)
181 {
182 glViewport( 0,0, vg.window_x, vg.window_y );
183 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
184 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
185 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
186
187 _vg_render_log();
188 vg.loader_ring = 1.0f;
189 }
190
191
192 static void vg_load_full(void);
193
194 static int _vg_loader_thread( void *pfn ){
195 if( setjmp( vg.env_loader_exit ) )
196 return 0;
197
198 /* Run client loader */
199 //vg_info( "Starting client loader thread @%p\n", pfn );
200 void (*call_func)(void *data) = pfn;
201 call_func( vg.thread_data );
202
203 SDL_SemPost( vg.sem_loader );
204 vg.thread_id_loader = 0;
205
206 return 0;
207 }
208
209 static int vg_loader_availible(void){
210 if( SDL_SemValue( vg.sem_loader ) ){
211 if( !(vg_async.start) )
212 return 1;
213 }
214
215 return 0;
216 }
217
218 static void vg_loader_start( void(*pfn)(void *data), void *data )
219 {
220 SDL_SemWait( vg.sem_loader );
221
222 vg.thread_data = data;
223 SDL_CreateThread( _vg_loader_thread, "vg: loader", pfn );
224 }
225
226 /*
227 * Schedule something to be ran now, freed later. Checks in with engine status
228 */
229 static void _vg_loader_step( void( *fn_load )(void), void( *fn_free )(void),
230 const char *alias ){
231
232 u64 t0 = SDL_GetPerformanceCounter();
233 vg.time_hp_last = vg.time_hp;
234
235 if( fn_load )
236 fn_load();
237
238 u64 udt = SDL_GetPerformanceCounter() - t0;
239 double dt = (double)udt / (double)SDL_GetPerformanceFrequency();
240 vg_info( "ltime [%p] %s: %fs\n", fn_load, alias, dt );
241
242 if( fn_free ){
243 struct loader_free_step step;
244 step.fn_free = fn_free;
245
246 if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
247 vg_fatal_error( "Too many free steps" );
248
249 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
250 }
251
252 /* TODO: There was a quit checker here, re-add this? */
253 }
254
255 #define vg_loader_step( FN, FN_FREE )\
256 _vg_loader_step( FN, FN_FREE, #FN )
257
258 #endif /* VG_LOADER_H */