2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
7 * -----------------------------------------------------------------------------
15 #include "vg/vg_shader.h"
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) );
20 static struct vg_shader _shader_loader
=
22 .name
= "[vg] loader",
25 /* This is the new foreground shader */
30 "layout (location=0) in vec2 a_co;"
34 "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
44 "uniform float uTime;"
45 "uniform float uRatio;"
46 "uniform float uOpacity;"
49 "float eval_zero( vec2 uv )"
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;"
55 "vec2 vpos = uv + vec2( offset, 0.0 );"
56 "float dist = dot( vpos, vpos );"
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 );"
64 "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
66 "vec2 uvx = aUv - vec2( 0.5 );"
70 "float zero = eval_zero( uvx );"
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;"
75 "FragColor = vec4(vec3(fmt1),uOpacity);"
80 static struct vg_loader
83 struct loader_free_step
{
84 void (*fn_free
)(void);
87 u32 step_count
, step_action
;
93 static void _vg_loader_init(void)
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
};
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 );
109 if( !vg_shader_compile( &_shader_loader
) )
110 vg_fatal_error( "failed to compile shader" );
113 static void _vg_loader_free(void)
115 vg_info( "vg_loader_free\n" );
116 glDeleteVertexArrays( 1, &vg_loader
.vao
);
117 glDeleteBuffers( 1, &vg_loader
.vbo
);
119 for( int i
=0; i
<vg_loader
.step_count
; i
++ )
121 struct loader_free_step
*step
=
122 &vg_loader
.step_buffer
[vg_loader
.step_count
-1 -i
];
124 vg_info( " -> %p\n", step
->fn_free
);
129 static void _vg_render_log(void)
132 ui_begin( vg
.window_x
, vg
.window_y
);
133 SDL_AtomicLock( &log_print_sl
);
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
);
140 int ptr
= vg_log
.buffer_line_current
;
142 vg_uictx
.cursor
[0] = 0;
143 vg_uictx
.cursor
[1] = lines_to_draw
*fh
;
144 vg_uictx
.cursor
[3] = fh
;
147 for( int i
=0; i
<lines_to_draw
; i
++ ){
151 ptr
= vg_list_size( vg_log
.buffer
)-1;
153 ui_text( vg_uictx
.cursor
, vg_log
.buffer
[ptr
], 1, 0 );
154 vg_uictx
.cursor
[1] -= fh
;
157 SDL_AtomicUnlock( &log_print_sl
);
164 static void _vg_loader_render_ring( f32 opacity
){
166 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
167 glBlendEquation(GL_FUNC_ADD
);
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 );
180 static void _vg_loader_render(void)
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
);
188 vg
.loader_ring
= 1.0f
;
192 static void vg_load_full(void);
194 static int _vg_loader_thread( void *pfn
){
195 if( setjmp( vg
.env_loader_exit
) )
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
);
203 SDL_SemPost( vg
.sem_loader
);
204 vg
.thread_id_loader
= 0;
209 static int vg_loader_availible(void){
210 if( SDL_SemValue( vg
.sem_loader
) ){
211 if( !(vg_async
.start
) )
218 static void vg_loader_start( void(*pfn
)(void *data
), void *data
)
220 SDL_SemWait( vg
.sem_loader
);
222 vg
.thread_data
= data
;
223 SDL_CreateThread( _vg_loader_thread
, "vg: loader", pfn
);
227 * Schedule something to be ran now, freed later. Checks in with engine status
229 static void _vg_loader_step( void( *fn_load
)(void), void( *fn_free
)(void),
232 u64 t0
= SDL_GetPerformanceCounter();
233 vg
.time_hp_last
= vg
.time_hp
;
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
);
243 struct loader_free_step step
;
244 step
.fn_free
= fn_free
;
246 if( vg_loader
.step_count
== vg_list_size(vg_loader
.step_buffer
) )
247 vg_fatal_error( "Too many free steps" );
249 vg_loader
.step_buffer
[ vg_loader
.step_count
++ ] = step
;
252 /* TODO: There was a quit checker here, re-add this? */
255 #define vg_loader_step( FN, FN_FREE )\
256 _vg_loader_step( FN, FN_FREE, #FN )
258 #endif /* VG_LOADER_H */