ffdd87ad3c9d9803ee9a131f9b84513c25b033f7
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
7 * -----------------------------------------------------------------------------
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) );
18 static struct vg_shader _shader_loader
=
20 .name
= "[vg] loader",
23 /* This is the old background shader */
29 "layout (location=0) in vec2 a_co;"
33 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
43 "uniform float uTime;"
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 );"
52 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
54 "FragColor = vec4(vec3(0.5,0.5,0.5)*grad*fmt1,1.0);"
59 /* This is the new foreground shader */
65 "layout (location=0) in vec2 a_co;"
69 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
79 "uniform float uTime;"
80 "uniform float uRatio;"
83 "float eval_zero( vec2 uv )"
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;"
89 "vec2 vpos = uv + vec2( offset, 0.0 );"
90 "float dist = dot( vpos, vpos );"
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 );"
99 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
101 "vec2 uvx = aUv - vec2( 0.5 );"
105 "float zero = eval_zero( uvx );"
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;"
110 "FragColor = vec4(vec3(fmt1),0.8);"
117 static struct vg_loader
120 struct loader_free_step
122 void (*fn_free
)(void);
125 u32 step_count
, step_action
;
131 VG_STATIC
void _vg_loader_init(void)
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
};
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 );
147 if( !vg_shader_compile( &_shader_loader
) )
148 vg_fatal_exit_loop( "failed to compile shader" );
151 VG_STATIC
void _vg_loader_free(void)
153 vg_info( "vg_loader_free\n" );
154 glDeleteVertexArrays( 1, &vg_loader
.vao
);
155 glDeleteBuffers( 1, &vg_loader
.vbo
);
157 for( int i
=0; i
<vg_loader
.step_count
; i
++ )
159 struct loader_free_step
*step
=
160 &vg_loader
.step_buffer
[vg_loader
.step_count
-1 -i
];
162 vg_info( " -> %p\n", step
->fn_free
);
169 VG_STATIC
void _vg_render_log(void)
171 ui_begin( vg
.window_x
, vg
.window_y
);
172 SDL_AtomicLock( &log_print_sl
);
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
);
179 int ptr
= vg_log
.buffer_line_current
;
181 vg_uictx
.cursor
[0] = 0;
182 vg_uictx
.cursor
[1] = lines_to_draw
*fh
;
183 vg_uictx
.cursor
[3] = fh
;
186 for( int i
=0; i
<lines_to_draw
; i
++ ){
190 ptr
= vg_list_size( vg_log
.buffer
)-1;
192 ui_text( vg_uictx
.cursor
, vg_log
.buffer
[ptr
], 1, 0 );
193 vg_uictx
.cursor
[1] -= fh
;
196 SDL_AtomicUnlock( &log_print_sl
);
202 VG_STATIC
void _vg_loader_render(void)
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
);
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 );
220 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
221 glBlendEquation(GL_FUNC_ADD
);
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 );
233 VG_STATIC
void vg_load_full(void);
235 VG_STATIC
int _vg_loader_thread(void *pfn
)
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
);
242 /* Run client loader */
243 void (*call_func
)(void) = pfn
;
246 SDL_SemPost( vg
.sem_loader
);
247 vg
.thread_id_loader
= 0;
249 vg_acquire_thread_sync();
251 vg_release_thread_sync();
256 VG_STATIC
void vg_loader_start( void(*pfn
)(void) )
259 SDL_SemWait( vg
.sem_loader
);
260 SDL_CreateThread( _vg_loader_thread
, "Loader thread", pfn
);
264 * Schedule something to be ran now, freed later. Checks in with engine status
266 VG_STATIC
void vg_loader_step( void( *fn_load
)(void), void( *fn_free
)(void) )
273 struct loader_free_step step
;
274 step
.fn_free
= fn_free
;
276 if( vg_loader
.step_count
== vg_list_size(vg_loader
.step_buffer
) )
277 vg_fatal_exit_loop( "Too many free steps" );
279 vg_loader
.step_buffer
[ vg_loader
.step_count
++ ] = step
;
282 _vg_ensure_engine_running();
285 #endif /* VG_LOADER_H */