ffdd87ad3c9d9803ee9a131f9b84513c25b033f7
[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 #include "common.h"
14
15 VG_STATIC void vg_loader_start( void(*pfn)(void) );
16 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) );
17
18 static struct vg_shader _shader_loader =
19 {
20 .name = "[vg] loader",
21 .link = NULL,
22
23 /* This is the old background shader */
24 #if 0
25 .vs =
26 {
27 .orig_file = NULL,
28 .static_src = ""
29 "layout (location=0) in vec2 a_co;"
30 "out vec2 aUv;"
31 "void main()"
32 "{"
33 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
34 "aUv = a_co;"
35 "}"
36 },
37 .fs =
38 {
39 .orig_file = NULL,
40 .static_src =
41
42 "out vec4 FragColor;"
43 "uniform float uTime;"
44 "in vec2 aUv;"
45
46 "void main()"
47 "{"
48 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
49 "float grad = 1.0-(aUv.y*0.5+0.5)*0.5;"
50 "float fmt1 = step( 0.5, grad+dither );"
51
52 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
53
54 "FragColor = vec4(vec3(0.5,0.5,0.5)*grad*fmt1,1.0);"
55 "}"
56 }
57 #else
58
59 /* This is the new foreground shader */
60
61 .vs =
62 {
63 .orig_file = NULL,
64 .static_src = ""
65 "layout (location=0) in vec2 a_co;"
66 "out vec2 aUv;"
67 "void main()"
68 "{"
69 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
70 "aUv = a_co;"
71 "}"
72 },
73 .fs =
74 {
75 .orig_file = NULL,
76 .static_src =
77
78 "out vec4 FragColor;"
79 "uniform float uTime;"
80 "uniform float uRatio;"
81 "in vec2 aUv;"
82
83 "float eval_zero( vec2 uv )"
84 "{"
85 "vec4 vsines = sin( (uTime+uv.y*80.0) * vec4(1.1,2.0234,3.73,2.444) );"
86 "float gradient = min( uv.y, 0.0 );"
87 "float offset = vsines.x*vsines.y*vsines.z*vsines.w*gradient;"
88
89 "vec2 vpos = uv + vec2( offset, 0.0 );"
90 "float dist = dot( vpos, vpos );"
91
92 "float fring = step(0.1*0.1,dist) * step(dist,0.15*0.15);"
93 "return max( 0.0, fring * 1.0+gradient*6.0 );"
94 "}"
95
96 "void main()"
97 "{"
98
99 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
100
101 "vec2 uvx = aUv - vec2( 0.5 );"
102 "uvx.x *= uRatio;"
103 "uvx.y *= 0.75;"
104
105 "float zero = eval_zero( uvx );"
106
107 "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
108 "float fmt1 = step( 0.5, zero*zero + dither )*0.8+0.2;"
109
110 "FragColor = vec4(vec3(fmt1),0.8);"
111 "}"
112 }
113
114 #endif
115 };
116
117 static struct vg_loader
118 {
119 /* Shutdown steps */
120 struct loader_free_step
121 {
122 void (*fn_free)(void);
123 }
124 step_buffer[16];
125 u32 step_count, step_action;
126
127 GLuint vao, vbo;
128 }
129 vg_loader;
130
131 VG_STATIC void _vg_loader_init(void)
132 {
133 float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
134 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
135
136 glGenVertexArrays( 1, &vg_loader.vao );
137 glGenBuffers( 1, &vg_loader.vbo );
138 glBindVertexArray( vg_loader.vao );
139 glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
140 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
141 glBindVertexArray( vg_loader.vao );
142 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
143 glEnableVertexAttribArray( 0 );
144
145 VG_CHECK_GL_ERR();
146
147 if( !vg_shader_compile( &_shader_loader ) )
148 vg_fatal_exit_loop( "failed to compile shader" );
149 }
150
151 VG_STATIC void _vg_loader_free(void)
152 {
153 vg_info( "vg_loader_free\n" );
154 glDeleteVertexArrays( 1, &vg_loader.vao );
155 glDeleteBuffers( 1, &vg_loader.vbo );
156
157 for( int i=0; i<vg_loader.step_count; i++ )
158 {
159 struct loader_free_step *step =
160 &vg_loader.step_buffer[vg_loader.step_count -1 -i];
161
162 vg_info( " -> %p\n", step->fn_free );
163 step->fn_free();
164 }
165
166 vg_info( "done\n" );
167 }
168
169 VG_STATIC void _vg_render_log(void)
170 {
171 ui_begin( vg.window_x, vg.window_y );
172 SDL_AtomicLock( &log_print_sl );
173
174 int const fh = 14;
175 int lines_screen_max = ((vg.window_y/fh)-2),
176 lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
177 lines_to_draw = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
178
179 int ptr = vg_log.buffer_line_current;
180
181 vg_uictx.cursor[0] = 0;
182 vg_uictx.cursor[1] = lines_to_draw*fh;
183 vg_uictx.cursor[3] = fh;
184 ui_fill_x();
185
186 for( int i=0; i<lines_to_draw; i ++ ){
187 ptr --;
188
189 if( ptr < 0 )
190 ptr = vg_list_size( vg_log.buffer )-1;
191
192 ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
193 vg_uictx.cursor[1] -= fh;
194 }
195
196 SDL_AtomicUnlock( &log_print_sl );
197
198 ui_resolve();
199 ui_draw( NULL );
200 }
201
202 VG_STATIC void _vg_loader_render(void)
203 {
204 glViewport( 0,0, vg.window_x, vg.window_y );
205 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
206 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
207 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
208
209 #if 0
210 glUseProgram( _shader_loader.id );
211 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
212 glBindVertexArray( vg_loader.vao );
213 glDrawArrays( GL_TRIANGLES, 0, 6 );
214 #endif
215
216 _vg_render_log();
217
218 #if 1
219 glEnable(GL_BLEND);
220 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
221 glBlendEquation(GL_FUNC_ADD);
222
223 glUseProgram( _shader_loader.id );
224 glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
225 float ratio = (float)vg.window_x / (float)vg.window_y;
226 glUniform1f( glGetUniformLocation( _shader_loader.id, "uRatio"), ratio );
227 glBindVertexArray( vg_loader.vao );
228 glDrawArrays( GL_TRIANGLES, 0, 6 );
229 #endif
230 }
231
232
233 VG_STATIC void vg_load_full(void);
234
235 VG_STATIC int _vg_loader_thread(void *pfn)
236 {
237 SDL_AtomicLock( &vg.sl_context );
238 vg.thread_id_loader = SDL_GetThreadID(NULL);
239 VG_SYNC_LOG( "[%d] Loader thread begins\n" );
240 SDL_AtomicUnlock( &vg.sl_context );
241
242 /* Run client loader */
243 void (*call_func)(void) = pfn;
244 call_func();
245
246 SDL_SemPost( vg.sem_loader );
247 vg.thread_id_loader = 0;
248
249 vg_acquire_thread_sync();
250 vg.is_loaded = 1;
251 vg_release_thread_sync();
252
253 return 0;
254 }
255
256 VG_STATIC void vg_loader_start( void(*pfn)(void) )
257 {
258 vg.is_loaded = 0;
259 SDL_SemWait( vg.sem_loader );
260 SDL_CreateThread( _vg_loader_thread, "Loader thread", pfn );
261 }
262
263 /*
264 * Schedule something to be ran now, freed later. Checks in with engine status
265 */
266 VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) )
267 {
268 if( fn_load )
269 fn_load();
270
271 if( fn_free )
272 {
273 struct loader_free_step step;
274 step.fn_free = fn_free;
275
276 if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
277 vg_fatal_exit_loop( "Too many free steps" );
278
279 vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
280 }
281
282 _vg_ensure_engine_running();
283 }
284
285 #endif /* VG_LOADER_H */