Skip to content

Commit

Permalink
Only apply blur on full screen refreshes
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rgb committed Apr 29, 2017
1 parent 9713b64 commit 441d3b3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 83 deletions.
13 changes: 6 additions & 7 deletions src/hardware_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1256,11 +1256,12 @@ gpu_render_canvas(RenderData* render_data, i32 view_x, i32 view_y,
glDisable(GL_DEPTH_TEST);
for ( LayerEffect* e = re->effects; e != NULL; e = e->next ) {
if ( e->enabled == false ) { continue; }
glBindTexture(texture_target, in_texture);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
texture_target, out_texture, 0);

if ( e->type == LayerEffectType_BLUR ) {
if ( (render_data->flags & RenderDataFlags_WITH_BLUR) && e->type == LayerEffectType_BLUR ) {
glBindTexture(texture_target, in_texture);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
texture_target, out_texture, 0);

// Three box filter iterations approximate a Gaussian blur
for (int blur_iter = 0; blur_iter < 3; ++blur_iter) {
// Box filter implementation uses the separable property.
Expand All @@ -1285,10 +1286,8 @@ gpu_render_canvas(RenderData* render_data, i32 view_x, i32 view_y,
glBindTexture(texture_target, in_texture);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
texture_target, out_texture, 0);
layer_post_effects = out_texture;
}

layer_post_effects = out_texture;

}
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
Expand Down
2 changes: 1 addition & 1 deletion src/hardware_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum RenderDataFlags

RenderDataFlags_GUI_VISIBLE = 1<<0,
RenderDataFlags_EXPORTING = 1<<1,
RenderDataFlags_WITH_BLUR = 1<<2,
};

struct Arena;
Expand Down Expand Up @@ -107,7 +108,6 @@ void gpu_clip_strokes_and_update(Arena* arena,

void gpu_reset_render_flags(RenderData* render_data, int flags);


void gpu_render(RenderData* render_data, i32 view_x, i32 view_y, i32 view_width, i32 view_height);
void gpu_render_to_buffer(MiltonState* milton_state, u8* buffer, i32 scale, i32 x, i32 y, i32 w, i32 h, f32 background_alpha);

Expand Down
79 changes: 19 additions & 60 deletions src/milton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,17 +539,9 @@ upload_gui(MiltonState* milton_state)
}

// Returns false if the pan_delta moves the pan vector outside of the canvas.
b32
void
milton_resize_and_pan(MiltonState* milton_state, v2l pan_delta, v2i new_screen_size)
{
b32 pan_ok = true;
if ( (new_screen_size.w > 8000
|| new_screen_size.h > 8000
|| new_screen_size.w <= 0
|| new_screen_size.h <= 0) ) {
return pan_ok;
}

b32 do_realloc = false;
if ( milton_state->max_width <= new_screen_size.w ) {
milton_state->max_width = new_screen_size.w + 256;
Expand Down Expand Up @@ -586,26 +578,12 @@ milton_resize_and_pan(MiltonState* milton_state, v2l pan_delta, v2i new_screen_s
// Add delta to pan vector
v2l pan_center = milton_state->view->pan_center - (pan_delta * milton_state->view->scale);

/*
if ( pan_center.x > milton_state->view->canvas_radius_limit
|| pan_center.x <= -milton_state->view->canvas_radius_limit ) {
pan_center.x = milton_state->view->pan_center.x;
pan_ok = false;
}
if ( pan_center.y > milton_state->view->canvas_radius_limit
|| pan_center.y <= -milton_state->view->canvas_radius_limit ) {
pan_center.y = milton_state->view->pan_center.y;
pan_ok = false;
}
*/
milton_state->view->pan_center = pan_center;

upload_gui(milton_state);
} else {
milton_die_gracefully("Fatal error. Screen size is more than Milton can handle.");
}

return pan_ok;
}

void
Expand Down Expand Up @@ -1033,6 +1011,8 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)

if ( input->flags & MiltonInputFlags_FULL_REFRESH ) {
do_full_redraw = true;
// GUI might have changed layer effect parameters.
render_flags |= RenderDataFlags_WITH_BLUR;
}

if ( input->scale ) {
Expand Down Expand Up @@ -1119,20 +1099,8 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)
push(&milton_state->canvas->stroke_graveyard, stroke);
push(&milton_state->canvas->redo_stack, h);

draw_custom_rectangle = true;
Rect bounds = stroke.bounding_rect;
bounds.top_left = canvas_to_raster(milton_state->view, bounds.top_left);
bounds.bot_right = canvas_to_raster(milton_state->view, bounds.bot_right);

// Enlarge rectangle by one pixel to counter a floating point precision
// problem when canvas_to_raster_gl (in the shader) normalizes to [-1,1]^2
auto* view = milton_state->view;
bounds.top = max(0, bounds.top-1);
bounds.left = max(0, bounds.left-1);
bounds.right = min(view->screen_size.w, bounds.right+1);
bounds.bottom = min(view->screen_size.h, bounds.bottom+1);

custom_rectangle = rect_union(custom_rectangle, bounds);
do_full_redraw = true;
render_flags |= RenderDataFlags_WITH_BLUR;
}
break;
}
Expand All @@ -1151,19 +1119,9 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)
push(&l->strokes, stroke);
push(&milton_state->canvas->history, h);

draw_custom_rectangle = true;
Rect bounds = stroke.bounding_rect;
bounds.top_left = canvas_to_raster(milton_state->view, bounds.top_left);
bounds.bot_right = canvas_to_raster(milton_state->view, bounds.bot_right);
custom_rectangle = rect_union(custom_rectangle, bounds);
for ( LayerEffect* e = l->effects; e != NULL; e = e->next ) {
CanvasView* view = milton_state->view;
switch ( e->type ) {
case LayerEffectType_BLUR: {
custom_rectangle = rect_enlarge(custom_rectangle, 10*(i32)(e->blur.kernel_size*(double)e->blur.original_scale/(double)view->scale));
} break;
}
}
do_full_redraw = true;
render_flags |= RenderDataFlags_WITH_BLUR;


break;
}
Expand Down Expand Up @@ -1209,7 +1167,13 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)
}
else if ( !milton_state->gui->owns_user_input
&& (milton_state->canvas->working_layer->flags & LayerFlags_VISIBLE) ) {
Stroke* ws = &milton_state->working_stroke;
auto prev_num_points = ws->num_points;
milton_stroke_input(milton_state, input);
if ( prev_num_points == 0 && ws->num_points > 0 ) {
// New stroke. Clear screen without blur.
do_full_redraw = true;
}
}
}
}
Expand Down Expand Up @@ -1344,6 +1308,10 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)
}

clear_stroke_redo(milton_state);

// Make sure we show blurred layers when finishing a stroke.
render_flags |= RenderDataFlags_WITH_BLUR;
do_full_redraw = true;
}
}
}
Expand Down Expand Up @@ -1506,11 +1474,6 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)
view_width = custom_rectangle.right - custom_rectangle.left;
view_height = custom_rectangle.bottom - custom_rectangle.top;
VALIDATE_RECT(custom_rectangle);

view_x = 0;
view_y = 0;
view_width = milton_state->view->screen_size.w;
view_height = milton_state->view->screen_size.h;
}
else if ( milton_state->working_stroke.num_points > 0 ) {
Rect bounds = milton_state->working_stroke.bounding_rect;
Expand All @@ -1522,14 +1485,10 @@ milton_update_and_render(MiltonState* milton_state, MiltonInput* input)

view_width = bounds.right - bounds.left;
view_height = bounds.bottom - bounds.top;

view_x = 0;
view_y = 0;
view_width = milton_state->view->screen_size.w;
view_height = milton_state->view->screen_size.h;
}

PROFILE_GRAPH_BEGIN(clipping);

gpu_clip_strokes_and_update(&milton_state->root_arena, milton_state->render_data, milton_state->view,
milton_state->canvas->root_layer, &milton_state->working_stroke,
view_x, view_y, view_width, view_height, clip_flags);
Expand Down
2 changes: 1 addition & 1 deletion src/milton.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ float milton_get_pen_alpha(MiltonState* milton_state);
void milton_set_pen_alpha(MiltonState* milton_state, float alpha);

// Returns false if the pan_delta moves the pan vector outside of the canvas.
b32 milton_resize_and_pan(MiltonState* milton_state, v2l pan_delta, v2i new_screen_size);
void milton_resize_and_pan(MiltonState* milton_state, v2l pan_delta, v2i new_screen_size);


void milton_use_previous_mode(MiltonState* milton_state);
Expand Down
1 change: 1 addition & 0 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct PlatformState
b32 is_middle_button_down;

b32 is_panning;
b32 was_panning;
b32 waiting_for_pan_input; // Start panning from GUI menu.

b32 was_exporting;
Expand Down
22 changes: 8 additions & 14 deletions src/sdl_milton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ panning_update(PlatformState* platform_state)
platform_state->pan_point = platform_state->pan_start; // No huge pan_delta at beginning of pan.
};

platform_state->was_panning = platform_state->is_panning;

// Panning from GUI menu, waiting for input
if ( platform_state->waiting_for_pan_input ) {
if ( platform_state->is_pointer_down ) {
Expand Down Expand Up @@ -973,12 +975,16 @@ milton_main(char* file_to_open)
// Clear pan delta if we are zooming
if ( milton_input.scale != 0 ) {
milton_input.pan_delta = {};
input_flags |= MiltonInputFlags_FAST_DRAW;
input_flags |= MiltonInputFlags_FULL_REFRESH;
}
else if ( platform_state.is_panning ) {
input_flags |= MiltonInputFlags_PANNING;
platform_state.num_point_results = 0;
}
else if ( platform_state.was_panning ) {
// Just finished panning. Refresh the screen.
input_flags |= MiltonInputFlags_FULL_REFRESH;
}

if ( platform_state.num_pressure_results < platform_state.num_point_results ) {
platform_state.num_point_results = platform_state.num_pressure_results;
Expand All @@ -995,19 +1001,7 @@ milton_main(char* file_to_open)
|| pan_delta.y != 0
|| platform_state.width != milton_state->view->screen_size.x
|| platform_state.height != milton_state->view->screen_size.y ) {
b32 pan_ok = milton_resize_and_pan(milton_state, pan_delta, {platform_state.width, platform_state.height});
if ( !pan_ok ) {
// TODO: Turn panning off

// Force a full re-render.
// The hover point gets updated and the renderer does a memcpy on most of the screen.
// TODO: Remove use of MiltonInputFlags_FULL_REFRESH after switching to HW rendering...
input_flags |= MiltonInputFlags_FULL_REFRESH;
milton_state->flags |= MiltonStateFlags_REQUEST_QUALITY_REDRAW;
}
else if ( !platform_state.is_panning && !pan_ok ) {
INVALID_CODE_PATH;
}
milton_resize_and_pan(milton_state, pan_delta, {platform_state.width, platform_state.height});
}
milton_input.pan_delta = pan_delta;

Expand Down

0 comments on commit 441d3b3

Please sign in to comment.