From 68cf8354fc0c0bcfe9584b84e9ecae21980f0042 Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 25 Jan 2023 19:05:23 -0500 Subject: new needs_redraw system --- src/fshader_main.glsl | 35 ----------------------------- src/fshader_post.glsl | 50 +++++++++++++++++++++++++++++++++++++---- src/main.rs | 61 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 88 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/fshader_main.glsl b/src/fshader_main.glsl index 2a6864a..9a4ce52 100644 --- a/src/fshader_main.glsl +++ b/src/fshader_main.glsl @@ -13,14 +13,6 @@ uniform ivec2 u_antialiasing; uniform int u_iterations; uniform float u_aspect_ratio; uniform vec2 u_screen_size; -uniform vec4 u_flash; -uniform int u_flash_icon; - -#define ICON_COPY 1 -#define ICON_PLAY 2 -#define ICON_PAUSE 3 -#define ICON_REWIND 4 -#define ICON_SCREENSHOT 5 %COMMON% %SDF% @@ -71,13 +63,6 @@ vec3 normal(vec3 p) return normalize(sdf_normal); } -bool play_icon(vec2 pos) { - vec2 a = abs(pos); - if (a.x >= 0.5 || a.y >= 0.5) - return false; - return a.y < 0.25 + 0.5 * pos.x; -} - void main() { float min_dist = 10.; vec2 inv_screen_size = 1.0 / u_screen_size; @@ -128,26 +113,6 @@ void main() { } } final_color *= 1.0 / (AA_X * AA_Y); - bool icon = false; - switch (u_flash_icon) { - case 0: break; - case ICON_COPY: - case ICON_SCREENSHOT: - icon = abs(pos.x) > u_aspect_ratio - 0.1 || abs(pos.y) > 0.9; - break; - case ICON_PLAY: - icon = play_icon(pos); - break; - case ICON_REWIND: - icon = play_icon(vec2(-pos.x, pos.y)); - break; - case ICON_PAUSE: - vec2 p = abs(pos); - icon = p.x >= 0.1 && p.x <= 0.4 && p.y <= 0.5; - break; - } - if (icon) - final_color = mix(final_color, u_flash.xyz, u_flash.w); gl_FragColor = vec4(final_color, 1.0); } diff --git a/src/fshader_post.glsl b/src/fshader_post.glsl index 2c26d95..c540f0d 100644 --- a/src/fshader_post.glsl +++ b/src/fshader_post.glsl @@ -5,20 +5,62 @@ uniform float u_aspect_ratio; uniform float u_menu_scale; uniform vec2 u_highlight_button; IN vec2 uv; +uniform vec4 u_flash; +uniform int u_flash_icon; + +#define ICON_COPY 1 +#define ICON_PLAY 2 +#define ICON_PAUSE 3 +#define ICON_REWIND 4 +#define ICON_SCREENSHOT 5 + +bool play_icon(vec2 pos) { + vec2 a = abs(pos); + if (a.x >= 0.5 || a.y >= 0.5) + return false; + return a.y < 0.25 + 0.5 * pos.x; +} + +bool get_icon(vec2 pos) { + bool icon = false; + switch (u_flash_icon) { + case 0: break; + case ICON_COPY: + case ICON_SCREENSHOT: + icon = abs(pos.x) > u_aspect_ratio - 0.1 || abs(pos.y) > 0.9; + break; + case ICON_PLAY: + icon = play_icon(pos); + break; + case ICON_REWIND: + icon = play_icon(vec2(-pos.x, pos.y)); + break; + case ICON_PAUSE: + vec2 p = abs(pos); + icon = p.x >= 0.1 && p.x <= 0.4 && p.y <= 0.5; + break; + } + return icon; +} void main() { + vec3 color = texture(u_main_texture, uv).xyz; + + if (get_icon((uv * 2.0 - 1.0) * vec2(u_aspect_ratio, 1.0))) + color = mix(color, u_flash.xyz, u_flash.w); + // amount to darken screen by when paused float pause_darkening = 0.5; - vec4 color = texture(u_main_texture, uv) * (1.0 - pause_darkening * u_paused); + color *= 1.0 - pause_darkening * u_paused; vec2 menu_uv = (uv * 2.0 - 1.0) * vec2(1.0, -1.0); menu_uv *= 1.0 / u_menu_scale; menu_uv = menu_uv * vec2(u_aspect_ratio, 1.0); menu_uv = 0.5 * menu_uv + 0.5; float menu_pixel = u_paused * texture(u_menu_texture, menu_uv).x; - vec4 menu_color = vec4(1.0); + vec3 menu_color = vec3(1.0); if (uv.y >= u_highlight_button.x && uv.y <= u_highlight_button.y) { - menu_color = vec4(1.0, 0.5, 0.0, 1.0); // highlight button + menu_color = vec3(1.0, 0.5, 0.0); // highlight button } color = mix(color, menu_color, menu_pixel); - gl_FragColor = clamp(color, 0.0, 1.0); + gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0); } diff --git a/src/main.rs b/src/main.rs index c13574a..1a7c726 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ /* @TODO: -- redraw SDF even when paused if settings or window resolution is changed - flash error on bad string (see @TODO(error handling)) - RnToRn functions (& add back in RToR) - also add PerComponent(Box,Box,Box) in R3ToR3 @@ -104,9 +103,16 @@ impl View { fn unpause(&mut self, rewind: bool) { self.time_speed = if rewind { -1.0 } else { 1.0 }; } - - fn pass_time(&mut self, dt: f64) { - self.time += self.time_speed * dt; + + /// returns true if the current time is modified + fn pass_time(&mut self, dt: f64) -> bool { + let dt = self.time_speed * dt; + if dt == 0.0 { + false + } else { + self.time += dt; + true + } } fn yaw_by(&mut self, yaw: f32) { @@ -280,6 +286,9 @@ struct State { fullscreen: bool, esc_menu: bool, quit: bool, + /// set to true when the view/window size/settings/etc. is changed, + /// and we need to redraw the SDF + needs_redraw: bool, frame_time: Instant, programs: Programs, config: sdf::SceneConfig, @@ -408,6 +417,7 @@ impl State { test_array, esc_menu: false, quit: false, + needs_redraw: true, post_array, scene_list, settings, @@ -470,9 +480,10 @@ impl State { self.view = initial_view; } Err(e) => { - eprintln!("Error: {e}") + eprintln!("Error: {e}"); } - }; + } + self.needs_redraw = true; } fn flash(&mut self, icon: Icon) { @@ -539,8 +550,6 @@ impl State { ); window.uniform3x3f("u_rotation", view.rotation().as_slice()); window.uniform3f_slice("u_translation", view.pos.as_slice()); - window.uniform4f_color("u_flash", self.flash); - window.uniform1i("u_flash_icon", self.flash_icon as i32); self.main_array.draw(); } @@ -555,9 +564,11 @@ impl State { window.use_program(&self.programs.post); window.active_texture(0, &self.main_framebuffer_texture); window.active_texture(1, &self.menu_texture); - window.uniform1f("u_paused", if self.esc_menu { 1.0 } else { 0.0 }); window.uniform_texture("u_main_texture", 0); window.uniform_texture("u_menu_texture", 1); + window.uniform1f("u_paused", if self.esc_menu { 1.0 } else { 0.0 }); + window.uniform4f_color("u_flash", self.flash); + window.uniform1i("u_flash_icon", self.flash_icon as i32); window.uniform1f( "u_aspect_ratio", render_resolution.0 as f32 / render_resolution.1 as f32, @@ -639,7 +650,9 @@ impl State { // returns false if we should quit fn frame(&mut self) -> bool { - self.settings.reload_if_modified(); + if self.settings.reload_if_modified() { + self.needs_redraw = true; + } if let Some(max_framerate) = self.settings.get_f32("max-framerate") { if max_framerate > 0.0 { @@ -743,6 +756,7 @@ impl State { .yaw_by(-xrel as f32 * mouse_sensitivity * frame_dt); self.view .pitch_by(-yrel as f32 * mouse_sensitivity * frame_dt); + self.needs_redraw = true; } } MouseButtonDown { button: MouseButton::Left, x, y, .. } => { @@ -757,7 +771,9 @@ impl State { } if !self.esc_menu { - self.view.pass_time(frame_dt.into()); + if self.view.pass_time(frame_dt.into()) { + self.needs_redraw = true; + } } if !self.esc_menu { @@ -809,6 +825,7 @@ impl State { let dt = dt * speed_multiplier; self.view.pause(); self.view.time += f64::from(dt); + self.needs_redraw = true; } let motion = Vec3::new(dx, dy, dz); @@ -817,10 +834,14 @@ impl State { let motion = motion * move_amount; let motion = self.view.rotation() * motion; self.view.pos += motion; + self.needs_redraw = true; + } + + if dl != 0.0 { + let level_set_amount = 1.0 * speed_multiplier; + self.view.level_set += dl * level_set_amount; + self.needs_redraw = true; } - - let level_set_amount = 1.0 * speed_multiplier; - self.view.level_set += dl * level_set_amount; } let render_resolution = self.get_render_resolution(); @@ -842,13 +863,15 @@ impl State { &self.main_framebuffer_texture, ); self.main_framebuffer_size = render_resolution; + self.needs_redraw = true; } - - // if the escape menu is open, stop rendering the SDF. - // the framebuffer contents will stay the same. - // this lowers GPU usage (and increases framerate). - if !self.esc_menu { + + // this needs_redraw check stops the SDF from being rendered when + // the escape menu is open for example (unless window dimensions/settings are changed) + // this reduces GPU usage and gives a higher framerate + if self.needs_redraw { self.render_main(); + self.needs_redraw = false; } self.flash.a = f32::max(self.flash.a - frame_dt * (2.0 - 1.0 * self.flash.a), 0.0); -- cgit v1.2.3