From 34b3d6e2f88c275032cfec5aacd036cb65d20dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Thu, 23 Aug 2018 22:29:29 +0200 Subject: UI: Optimize the area border drawing It is was not really a bottleneck but it was triggering my OCD when 1/3rd of the drawcalls in a normal scene were basically only caused by this. --- source/blender/editors/screen/screen_draw.c | 164 ++++++++++++++++++++-------- 1 file changed, 118 insertions(+), 46 deletions(-) (limited to 'source/blender/editors/screen/screen_draw.c') diff --git a/source/blender/editors/screen/screen_draw.c b/source/blender/editors/screen/screen_draw.c index 10d72d74b22..928ed05f1de 100644 --- a/source/blender/editors/screen/screen_draw.c +++ b/source/blender/editors/screen/screen_draw.c @@ -24,12 +24,14 @@ #include "ED_screen.h" +#include "GPU_batch_presets.h" #include "GPU_framebuffer.h" #include "GPU_immediate.h" #include "GPU_matrix.h" #include "GPU_state.h" #include "BLI_math.h" +#include "BLI_rect.h" #include "WM_api.h" #include "WM_types.h" @@ -214,7 +216,85 @@ static void draw_join_shape(ScrArea *sa, char dir, unsigned int pos) } } -#define CORNER_RESOLUTION 10 +#define CORNER_RESOLUTION 9 + +static void do_vert_pair(GPUVertBuf *vbo, uint pos, uint *vidx, int corner, int i) +{ + float inter[2], exter[2]; + inter[0] = cosf((corner * 2.0f * M_PI / 4.0f) + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f))); + inter[1] = sinf((corner * 2.0f * M_PI / 4.0f) + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f))); + + /* Snap point to edge */ + float div = 1.0f / max_ff(fabsf(inter[0]), fabsf(inter[1])); + mul_v2_v2fl(exter, inter, div); + exter[0] = roundf(exter[0]); + exter[1] = roundf(exter[1]); + + if (i == 0 || i == (CORNER_RESOLUTION - 1)) { + copy_v2_v2(inter, exter); + } + + /* Line width is 20% of the entire corner size. */ + const float line_width = 0.2f; + mul_v2_fl(inter, 1.0f - line_width); + mul_v2_fl(exter, 1.0f + line_width); + + switch (corner) { + case 0: + add_v2_v2(inter, (float[2]){-1.0f, -1.0f}); + add_v2_v2(exter, (float[2]){-1.0f, -1.0f}); + break; + case 1: + add_v2_v2(inter, (float[2]){1.0f, -1.0f}); + add_v2_v2(exter, (float[2]){1.0f, -1.0f}); + break; + case 2: + add_v2_v2(inter, (float[2]){1.0f, 1.0f}); + add_v2_v2(exter, (float[2]){1.0f, 1.0f}); + break; + case 3: + add_v2_v2(inter, (float[2]){-1.0f, 1.0f}); + add_v2_v2(exter, (float[2]){-1.0f, 1.0f}); + break; + } + + GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, inter); + GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, exter); +} + +static GPUBatch *batch_screen_edges_get(int *corner_len) +{ + static GPUBatch *screen_edges_batch = NULL; + + if (screen_edges_batch == NULL) { + GPUVertFormat format = {0}; + uint pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); + GPU_vertbuf_data_alloc(vbo, CORNER_RESOLUTION * 2 * 4 * 8 + 2); + + uint vidx = 0; + /* Note jitter is applied in the shader. */ + for (int jit = 0; jit < 8; ++jit) { + for (int corner = 0; corner < 4; ++corner) { + for (int c = 0; c < CORNER_RESOLUTION; ++c) { + do_vert_pair(vbo, pos, &vidx, corner, c); + } + } + } + /* close the loop */ + do_vert_pair(vbo, pos, &vidx, 0, 0); + + screen_edges_batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO); + gpu_batch_presets_register(screen_edges_batch); + } + + if (corner_len) { + *corner_len = CORNER_RESOLUTION * 2; + } + return screen_edges_batch; +} + static void drawscredge_corner_geometry( int sizex, int sizey, int corner_x, int corner_y, @@ -373,59 +453,47 @@ static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y); } -static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, unsigned int pos) +static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, float edge_thickness) { - int count = 0; - - if (x2 < sizex - 1) count += 2; - if (x1 > 0) count += 2; - if (y2 < sizey - 1) count += 2; - if (y1 > 0) count += 2; - - if (count == 0) { - return; - } - - immBegin(GPU_PRIM_LINES, count); + rctf rect; + BLI_rctf_init(&rect, (float)x1, (float)x2, (float)y1, (float)y2); /* right border area */ - if (x2 < sizex - 1) { - immVertex2f(pos, x2, y1); - immVertex2f(pos, x2, y2); + if (x2 >= sizex - 1) { + rect.xmax += edge_thickness * 0.5f; } /* left border area */ - if (x1 > 0) { /* otherwise it draws the emboss of window over */ - immVertex2f(pos, x1, y1); - immVertex2f(pos, x1, y2); + if (x1 <= 0) { /* otherwise it draws the emboss of window over */ + rect.xmin -= edge_thickness * 0.5f; } /* top border area */ - if (y2 < sizey - 1) { - immVertex2f(pos, x1, y2); - immVertex2f(pos, x2, y2); + if (y2 >= sizey - 1) { + rect.ymax += edge_thickness * 0.5f; } /* bottom border area */ - if (y1 > 0) { - immVertex2f(pos, x1, y1); - immVertex2f(pos, x2, y1); + if (y1 <= 0) { + rect.ymin -= edge_thickness * 0.5f; } - immEnd(); + GPUBatch *batch = batch_screen_edges_get(NULL); + GPU_batch_uniform_4fv(batch, "rect", (float *)&rect); + GPU_batch_draw(batch); } /** * \brief Screen edges drawing. */ -static void drawscredge_area(ScrArea *sa, int sizex, int sizey, unsigned int pos) +static void drawscredge_area(ScrArea *sa, int sizex, int sizey, float edge_thickness) { short x1 = sa->v1->vec.x; short y1 = sa->v1->vec.y; short x2 = sa->v3->vec.x; short y2 = sa->v3->vec.y; - drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, pos); + drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, edge_thickness); } /** @@ -436,34 +504,38 @@ void ED_screen_draw_edges(wmWindow *win) bScreen *screen = WM_window_get_active_screen(win); const int winsize_x = WM_window_pixels_x(win); const int winsize_y = WM_window_pixels_y(win); + float col[4], corner_scale, edge_thickness; + int verts_per_corner = 0; ScrArea *sa; - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - - /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */ - if (U.pixelsize > 1.0f) { - /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */ - GPU_line_width((2.0f * U.pixelsize) - 1); - immUniformThemeColor(TH_EDITOR_OUTLINE); + UI_GetThemeColor4fv(TH_EDITOR_OUTLINE, col); + col[3] = 1.0f / 8.0f; + corner_scale = U.pixelsize * 8.0f; + edge_thickness = corner_scale * 0.21f; - for (sa = screen->areabase.first; sa; sa = sa->next) { - drawscredge_area(sa, winsize_x, winsize_y, pos); - } - } + GPU_blend(true); - GPU_line_width(1); - immUniformThemeColor(TH_EDITOR_OUTLINE); + /* Transparent pass (for AA). */ + GPUBatch *batch = batch_screen_edges_get(&verts_per_corner); + GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_AREA_EDGES); + GPU_batch_uniform_1i(batch, "cornerLen", verts_per_corner); + GPU_batch_uniform_1f(batch, "scale", corner_scale); + GPU_batch_uniform_4fv(batch, "color", col); for (sa = screen->areabase.first; sa; sa = sa->next) { - drawscredge_area(sa, winsize_x, winsize_y, pos); + drawscredge_area(sa, winsize_x, winsize_y, edge_thickness); } - immUnbindProgram(); + GPU_blend(false); + + /* Opaque pass. */ + corner_scale -= 2.0f; + edge_thickness = corner_scale * 0.2f; + GPU_batch_uniform_1f(batch, "scale", corner_scale); for (sa = screen->areabase.first; sa; sa = sa->next) { - drawscredge_corner(sa, winsize_x, winsize_y); + drawscredge_area(sa, winsize_x, winsize_y, edge_thickness); } screen->do_draw = false; -- cgit v1.2.3