2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
7 * -----------------------------------------------------------------------------
15 #include "vg/vg_shader.h"
17 VG_STATIC
void vg_loader_start( void(*pfn
)(void *data
), void *data
);
18 VG_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 VG_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 VG_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 VG_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 VG_STATIC
void _vg_loader_render_ring( float opacity
)
167 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
168 glBlendEquation(GL_FUNC_ADD
);
170 glUseProgram( _shader_loader
.id
);
171 glUniform1f( glGetUniformLocation( _shader_loader
.id
, "uTime" ), vg
.time
);
172 float ratio
= (float)vg
.window_x
/ (float)vg
.window_y
;
173 glUniform1f( glGetUniformLocation( _shader_loader
.id
, "uRatio"), ratio
);
174 glUniform1f( glGetUniformLocation( _shader_loader
.id
, "uOpacity"), opacity
);
175 glBindVertexArray( vg_loader
.vao
);
176 glDrawArrays( GL_TRIANGLES
, 0, 6 );
179 VG_STATIC
void _vg_loader_render(void)
181 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
182 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
183 glClearColor( 0.0f
, 0.0f
, 0.0f
, 1.0f
);
184 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
187 vg
.loader_ring
= 1.0f
;
191 VG_STATIC
void vg_load_full(void);
193 VG_STATIC
int _vg_loader_thread( void *pfn
){
194 if( setjmp( vg
.env_loader_exit
) )
197 /* Run client loader */
198 vg_info( "Starting client loader thread @%p\n", pfn
);
199 void (*call_func
)(void *data
) = pfn
;
200 call_func( vg
.thread_data
);
202 SDL_SemPost( vg
.sem_loader
);
203 vg
.thread_id_loader
= 0;
208 static int vg_loader_availible(void){
209 if( SDL_SemValue( vg
.sem_loader
) ){
210 if( !(vg_async
.start
) )
217 VG_STATIC
void vg_loader_start( void(*pfn
)(void *data
), void *data
)
219 SDL_SemWait( vg
.sem_loader
);
221 vg
.thread_data
= data
;
222 SDL_CreateThread( _vg_loader_thread
, "Loader thread", pfn
);
226 * Schedule something to be ran now, freed later. Checks in with engine status
228 VG_STATIC
void vg_loader_step( void( *fn_load
)(void), void( *fn_free
)(void) )
234 struct loader_free_step step
;
235 step
.fn_free
= fn_free
;
237 if( vg_loader
.step_count
== vg_list_size(vg_loader
.step_buffer
) )
238 vg_fatal_error( "Too many free steps" );
240 vg_loader
.step_buffer
[ vg_loader
.step_count
++ ] = step
;
243 /* TODO: There was a quit checker here, re-add this? */
246 #endif /* VG_LOADER_H */