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.
This commit is contained in:
Clément Foucault 2018-04-05 18:51:08 +02:00
parent ba821ad2ad
commit 80d4d71360
3 changed files with 205 additions and 56 deletions

View File

@ -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 */

View File

@ -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]);

View File

@ -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.roundbox_simple_outline;
}
if (*batch == NULL) {
uint32_t last_data;
Gwn_VertBufRaw vflag_step;
Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(vflag_format());
int vcount = WIDGET_SIZE_MAX;
vcount += (filled) ? 2 : 0;
vcount *= (antialiased) ? WIDGET_AA_JITTER : 1;
GWN_vertbuf_data_alloc(vbo, vcount);
GWN_vertbuf_attr_get_raw_data(vbo, g_ui_batch_cache.vflag_id, &vflag_step);
if (filled) {
for (int j = 0; j < WIDGET_AA_JITTER; j++) {
if (!antialiased) {
j = NO_AA;
}
/* restart */
set_roundbox_vertex(&vflag_step, 0, 0, j, true, false, INNER);
for (int c1 = 0, c2 = 3; c1 < 2; c1++, c2--) {
for (int a1 = 0, a2 = WIDGET_CURVE_RESOLU -1; a2 >= 0; a1++, a2--) {
last_data = set_roundbox_vertex(&vflag_step, c1, a1, j, true, false, INNER);
last_data = set_roundbox_vertex(&vflag_step, c2, a2, j, true, false, INNER);
}
}
/* restart */
set_roundbox_vertex_data(&vflag_step, last_data);
if (!antialiased) {
break;
}
}
*batch = GWN_batch_create_ex(GWN_PRIM_TRI_STRIP, vbo, NULL, GWN_BATCH_OWNS_VBO);
}
else {
for (int j = 0; j < WIDGET_AA_JITTER; j++) {
if (!antialiased) {
j = NO_AA;
}
for (int c = 0; c < 4; c++) {
for (int a = 0; a < WIDGET_CURVE_RESOLU; a++) {
set_roundbox_vertex(&vflag_step, c, a, j, true, false, INNER);
}
}
if (!antialiased) {
break;
}
}
*batch = GWN_batch_create_ex(GWN_PRIM_LINE_LOOP, vbo, NULL, GWN_BATCH_OWNS_VBO);
}
gpu_batch_presets_register(*batch);
}
return *batch;
}
#undef INNER
@ -421,32 +474,6 @@ void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y
glDisable(GL_BLEND);
}
/* belongs in interface_draw.c, but needs WIDGET_AA_JITTER from this file */
void UI_draw_roundbox_aa(bool filled, float minx, float miny, float maxx, float maxy, float rad, const float color[4])
{
glEnable(GL_BLEND);
if (filled) {
/* plain antialiased filled box */
const float alpha = color[3] * 0.125f;
for (int j = 0; j < WIDGET_AA_JITTER; j++) {
gpuPushMatrix();
gpuTranslate2fv(jit[j]);
UI_draw_roundbox_3fvAlpha(true, minx, miny, maxx, maxy, rad, color, alpha);
gpuPopMatrix();
}
}
else {
/* plain antialiased unfilled box */
glEnable(GL_LINE_SMOOTH);
UI_draw_roundbox_4fv(false, minx, miny, maxx, maxy, rad, color);
glDisable(GL_LINE_SMOOTH);
}
glDisable(GL_BLEND);
}
static void widget_init(uiWidgetBase *wtb)
{
wtb->totvert = wtb->halfwayvert = 0;
@ -1015,7 +1042,7 @@ static void widgetbase_draw(uiWidgetBase *wtb, uiWidgetColors *wcol)
if (inner_col1[3] || inner_col2[3] || outline_col[3] || emboss_col[3] || tria_col[3]) {
widgetbase_set_uniform_colors_ubv(wtb, inner_col1, inner_col2, outline_col, emboss_col, tria_col);
Gwn_Batch *roundbox_batch = ui_batch_roundbox_get(wtb->tria1.type);
Gwn_Batch *roundbox_batch = ui_batch_roundbox_widget_get(wtb->tria1.type);
draw_widgetbase_batch(roundbox_batch, wtb);
}