Commit: 80d4d713607aafa69a4fde15cc1cdadc0e167abf
Author: Clément Foucault
Date:   Thu Apr 5 18:51:08 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB80d4d713607aafa69a4fde15cc1cdadc0e167abf

UI: Perf: Modify UI_draw_roundbox to use GWN_batch API.

This commit also rename and move a few thing to clean things up.

Major improvment is using one drawcall instead of 8 for UI_draw_roundbox_aa.

===================================================================

M       source/blender/editors/interface/interface_draw.c
M       source/blender/editors/interface/interface_intern.h
M       source/blender/editors/interface/interface_widgets.c

===================================================================

diff --git a/source/blender/editors/interface/interface_draw.c 
b/source/blender/editors/interface/interface_draw.c
index 5e5da60593c..cc5cbba0f05 100644
--- a/source/blender/editors/interface/interface_draw.c
+++ b/source/blender/editors/interface/interface_draw.c
@@ -64,6 +64,7 @@
 /* own include */
 #include "interface_intern.h"
 
+
 static int roundboxtype = UI_CNR_ALL;
 
 void UI_draw_roundbox_corner_set(int type)
@@ -101,12 +102,58 @@ void UI_draw_roundbox_3fvAlpha(bool filled, float minx, 
float miny, float maxx,
        UI_draw_roundbox_4fv(filled, minx, miny, maxx, maxy, rad, colv);
 }
 
+void UI_draw_roundbox_aa(bool filled, float minx, float miny, float maxx, 
float maxy, float rad, const float color[4])
+{
+       uiWidgetBaseParameters widget_params = {
+               .recti.xmin = minx, .recti.ymin = miny,
+               .recti.xmax = maxx, .recti.ymax = maxy,
+               .radi = rad,
+               .round_corners[0] = (roundboxtype & UI_CNR_BOTTOM_LEFT) ? 1.0f 
: 0.0f,
+               .round_corners[1] = (roundboxtype & UI_CNR_BOTTOM_RIGHT) ? 1.0f 
: 0.0f,
+               .round_corners[2] = (roundboxtype & UI_CNR_TOP_RIGHT) ? 1.0f : 
0.0f,
+               .round_corners[3] = (roundboxtype & UI_CNR_TOP_LEFT) ? 1.0f : 
0.0f,
+               .color_inner1[0] = color[0], .color_inner2[0] = color[0],
+               .color_inner1[1] = color[1], .color_inner2[1] = color[1],
+               .color_inner1[2] = color[2], .color_inner2[2] = color[2],
+               .color_inner1[3] = color[3], .color_inner2[3] = color[3],
+       };
+
+       glEnable(GL_BLEND);
+
+       if (filled) {
+               /* plain antialiased filled box */
+               widget_params.color_inner1[3] *= 0.125f;
+               widget_params.color_inner2[3] *= 0.125f;
+
+               /* WATCH: This is assuming the ModelViewProjectionMatrix is 
area pixel space.
+                * If it has been scaled, then it's no longer valid. */
+               Gwn_Batch *batch = ui_batch_roundbox_get(filled, true);
+               GWN_batch_program_set_builtin(batch, GPU_SHADER_2D_WIDGET_BASE);
+               GWN_batch_uniform_4fv_array(batch, "parameters", 11, (float 
*)&widget_params);
+               GWN_batch_draw(batch);
+       }
+       else {
+               /* plain antialiased unfilled box */
+               glEnable(GL_LINE_SMOOTH);
+
+               Gwn_Batch *batch = ui_batch_roundbox_get(filled, false);
+               GWN_batch_program_set_builtin(batch, GPU_SHADER_2D_WIDGET_BASE);
+               GWN_batch_uniform_4fv_array(batch, "parameters", 11, (float 
*)&widget_params);
+               GWN_batch_draw(batch);
+
+               glDisable(GL_LINE_SMOOTH);
+       }
+
+       glDisable(GL_BLEND);
+}
+
 void UI_draw_roundbox_4fv(bool filled, float minx, float miny, float maxx, 
float maxy, float rad, const float col[4])
 {
+#if 0
        float vec[7][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, 
{0.707, 0.293},
                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
        int a;
-       
+
        Gwn_VertFormat *format = immVertexFormat();
        unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 
2, GWN_FETCH_FLOAT);
 
@@ -175,8 +222,29 @@ void UI_draw_roundbox_4fv(bool filled, float minx, float 
miny, float maxx, float
        
        immEnd();
        immUnbindProgram();
+#endif
+
+       uiWidgetBaseParameters widget_params = {
+               .recti.xmin = minx, .recti.ymin = miny,
+               .recti.xmax = maxx, .recti.ymax = maxy,
+               .radi = rad,
+               .round_corners[0] = (roundboxtype & UI_CNR_BOTTOM_LEFT) ? 1.0f 
: 0.0f,
+               .round_corners[1] = (roundboxtype & UI_CNR_BOTTOM_RIGHT) ? 1.0f 
: 0.0f,
+               .round_corners[2] = (roundboxtype & UI_CNR_TOP_RIGHT) ? 1.0f : 
0.0f,
+               .round_corners[3] = (roundboxtype & UI_CNR_TOP_LEFT) ? 1.0f : 
0.0f,
+               .color_inner1[0] = col[0], .color_inner2[0] = col[0],
+               .color_inner1[1] = col[1], .color_inner2[1] = col[1],
+               .color_inner1[2] = col[2], .color_inner2[2] = col[2],
+               .color_inner1[3] = col[3], .color_inner2[3] = col[3],
+       };
+
+       Gwn_Batch *batch = ui_batch_roundbox_get(filled, false);
+       GWN_batch_program_set_builtin(batch, GPU_SHADER_2D_WIDGET_BASE);
+       GWN_batch_uniform_4fv_array(batch, "parameters", 11, (float 
*)&widget_params);
+       GWN_batch_draw(batch);
 }
 
+#if 0
 static void round_box_shade_col(unsigned attrib, const float col1[3], float 
const col2[3], const float fac)
 {
        float col[4] = {
@@ -187,6 +255,7 @@ static void round_box_shade_col(unsigned attrib, const 
float col1[3], float cons
        };
        immAttrib4fv(attrib, col);
 }
+#endif
 
 /* linear horizontal shade within button or in outline */
 /* view2d scrollers use it */
@@ -194,6 +263,7 @@ void UI_draw_roundbox_shade_x(
         bool filled, float minx, float miny, float maxx, float maxy,
         float rad, float shadetop, float shadedown, const float col[4])
 {
+#if 0
        float vec[7][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, 
{0.707, 0.293},
                           {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}};
        const float div = maxy - miny;
@@ -305,6 +375,30 @@ void UI_draw_roundbox_shade_x(
 
        immEnd();
        immUnbindProgram();
+#endif
+
+       uiWidgetBaseParameters widget_params = {
+               .recti.xmin = minx, .recti.ymin = miny,
+               .recti.xmax = maxx, .recti.ymax = maxy,
+               .radi = rad,
+               .round_corners[0] = (roundboxtype & UI_CNR_BOTTOM_LEFT) ? 1.0f 
: 0.0f,
+               .round_corners[1] = (roundboxtype & UI_CNR_BOTTOM_RIGHT) ? 1.0f 
: 0.0f,
+               .round_corners[2] = (roundboxtype & UI_CNR_TOP_RIGHT) ? 1.0f : 
0.0f,
+               .round_corners[3] = (roundboxtype & UI_CNR_TOP_LEFT) ? 1.0f : 
0.0f,
+               .color_inner1[0] = min_ff(1.0f, col[0] + shadetop),
+               .color_inner2[0] = max_ff(0.0f, col[0] + shadedown),
+               .color_inner1[1] = min_ff(1.0f, col[1] + shadetop),
+               .color_inner2[1] = max_ff(0.0f, col[1] + shadedown),
+               .color_inner1[2] = min_ff(1.0f, col[2] + shadetop),
+               .color_inner2[2] = max_ff(0.0f, col[2] + shadedown),
+               .color_inner1[3] = 1.0f,
+               .color_inner2[3] = 1.0f,
+       };
+
+       Gwn_Batch *batch = ui_batch_roundbox_get(filled, false);
+       GWN_batch_program_set_builtin(batch, GPU_SHADER_2D_WIDGET_BASE);
+       GWN_batch_uniform_4fv_array(batch, "parameters", 11, (float 
*)&widget_params);
+       GWN_batch_draw(batch);
 }
 
 #if 0 /* unused */
diff --git a/source/blender/editors/interface/interface_intern.h 
b/source/blender/editors/interface/interface_intern.h
index 8d7f9d47ab5..516dc1f4a29 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -700,6 +700,34 @@ struct wmIMEData *ui_but_ime_data_get(uiBut *but);
 #endif
 
 /* interface_widgets.c */
+
+/* Widget shader parameters, must match the shader layout. */
+typedef struct uiWidgetBaseParameters {
+       rctf recti, rect;
+       float radi, rad;
+       float facxi, facyi;
+       float round_corners[4];
+       float color_inner1[4], color_inner2[4];
+       float color_outline[4], color_emboss[4];
+       float color_tria[4];
+       float tria1_center[2], tria2_center[2];
+       float tria1_size, tria2_size;
+       float shade_dir, clamp;
+} uiWidgetBaseParameters;
+
+enum {
+       ROUNDBOX_TRIA_NONE = 0,
+       ROUNDBOX_TRIA_ARROWS,
+       ROUNDBOX_TRIA_SCROLL,
+       ROUNDBOX_TRIA_MENU,
+       ROUNDBOX_TRIA_CHECK,
+
+       ROUNDBOX_TRIA_MAX, /* don't use */
+};
+
+struct Gwn_Batch *ui_batch_roundbox_get(bool filled, bool antialiased);
+struct Gwn_Batch *ui_batch_roundbox_widget_get(int tria);
+
 void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float 
y3, const float color[4]);
 void ui_draw_anti_roundbox(int mode, float minx, float miny, float maxx, float 
maxy,
                            float rad, bool use_alpha, const float color[4]);
diff --git a/source/blender/editors/interface/interface_widgets.c 
b/source/blender/editors/interface/interface_widgets.c
index 1ef39a1d52a..6c473c0e0c9 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -126,18 +126,7 @@ typedef struct uiWidgetBase {
        uiWidgetTrias tria2;
 
        /* Widget shader parameters, must match the shader layout. */
-       struct {
-               rctf recti, rect;
-               float radi, rad;
-               float facxi, facyi;
-               float round_corners[4];
-               float color_inner1[4], color_inner2[4];
-               float color_outline[4], color_emboss[4];
-               float color_tria[4];
-               float tria1_center[2], tria2_center[2];
-               float tria1_size, tria2_size;
-               float shade_dir, clamp;
-       } uniform_params;
+       uiWidgetBaseParameters uniform_params;
 } uiWidgetBase;
 
 /** uiWidgetType: for time being only for visual appearance,
@@ -231,22 +220,16 @@ static const uint g_shape_preset_hold_action_face[2][3] = 
{{2, 0, 1}, {3, 5, 4}}
  * TODO: find a better place. Maybe it's own file?
  **/
 
-enum {
-       ROUNDBOX_TRIA_NONE = 0,
-       ROUNDBOX_TRIA_ARROWS,
-       ROUNDBOX_TRIA_SCROLL,
-       ROUNDBOX_TRIA_MENU,
-       ROUNDBOX_TRIA_CHECK,
-
-       ROUNDBOX_TRIA_MAX, /* don't use */
-};
-
 /* offset in triavec[] in shader per type */
 static const int tria_ofs[ROUNDBOX_TRIA_MAX] = {0, 0, 6, 22, 28};
 static const int tria_vcount[ROUNDBOX_TRIA_MAX] = {0, 3, 16, 3, 6};
 
 static struct {
-       Gwn_Batch *roundbox[ROUNDBOX_TRIA_MAX];
+       Gwn_Batch *roundbox_widget[ROUNDBOX_TRIA_MAX];
+
+       Gwn_Batch *roundbox_simple;
+       Gwn_Batch *roundbox_simple_aa;
+       Gwn_Batch *roundbox_simple_outline;
 
        Gwn_VertFormat format;
        uint vflag_id;
@@ -318,9 +301,9 @@ static void roundbox_batch_add_tria(Gwn_VertBufRaw 
*vflag_step, int tria, uint32
        }
 }
 
-static Gwn_Batch *ui_batch_roundbox_get(int tria)
+Gwn_Batch *ui_batch_roundbox_widget_get(int tria)
 {
-       if (g_ui_batch_cache.roundbox[tria] == NULL) {
+       if (g_ui_batch_cache.roundbox_widget[tria] == NULL) {
                uint32_t last_data;
                Gwn_VertBufRaw vflag_step;
                Gwn_VertBuf *vbo = 
GWN_vertbuf_create_with_format(vflag_format());
@@ -377,10 +360,80 @@ static Gwn_Batch *ui_batch_roundbox_get(int tria)
                if (tria) {
                        roundbox_batch_add_tria(&vflag_step, tria, last_data);
                }
-               g_ui_batch_cache.roundbox[tria] = 
GWN_batch_create_ex(GWN_PRIM_TRI_STRIP, vbo, NULL, GWN_BATCH_OWNS_VBO);
-               gpu_batch_presets_register(g_ui_batch_cache.roundbox[tria]);
+               g_ui_batch_cache.roundbox_widget[tria] = 
GWN_batch_create_ex(GWN_PRIM_TRI_STRIP, vbo, NULL, GWN_BATCH_OWNS_VBO);
+               
gpu_batch_presets_register(g_ui_batch_cache.roundbox_widget[tria]);
        }
-       return g_ui_batch_cache.roundbox[tria];
+       return g_ui_batch_cache.roundbox_widget[tria];
+}
+
+Gwn_Batch *ui_batch_roundbox_get(bool filled, bool antialiased)
+{
+       Gwn_Batch **batch = NULL;
+
+       if (filled) {
+               if (antialiased)
+                       batch = &g_ui_batch_cache.roundbox_simple_aa;
+               else
+                       batch = &g_ui_batch_cache.roundbox_simple;
+       }
+       else {
+               if (antialiased)
+                       BLI_assert(0); /* Use GL_LINE_SMOOTH instead!!: */
+               else
+                       batch = &g_ui_batch_cache.

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to