[Bf-blender-cvs] [1deab69] blender2.8: BLF/OpenGL: more text drawing optimization

Mike Erwin noreply at git.blender.org
Sun Oct 16 08:09:48 CEST 2016


Commit: 1deab69e0a60413b3d3fef41ae74873d1e8b0a53
Author: Mike Erwin
Date:   Sun Oct 16 02:08:16 2016 -0400
Branches: blender2.8
https://developer.blender.org/rB1deab69e0a60413b3d3fef41ae74873d1e8b0a53

BLF/OpenGL: more text drawing optimization

Make color values compact. Set color once per primitive. Use new immSkipAttrib to avoid useless color copies.

All of this should make text drawing less CPU hungry.

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

M	source/blender/blenfont/intern/blf.c
M	source/blender/blenfont/intern/blf_glyph.c
M	source/blender/blenfont/intern/blf_internal_types.h

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

diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index f42057a..45d0368 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -517,13 +517,15 @@ static void blf_draw_gl__start(FontBLF *font)
 	if (font->flags & BLF_ROTATION)  /* radians -> degrees */
 		gpuRotateAxis(font->angle * (float)(180.0 / M_PI), 'Z');
 
-	glGetFloatv(GL_CURRENT_COLOR, font->orig_col); /* TODO(merwin): new BLF_color function? */
+	float temp_color[4];
+	glGetFloatv(GL_CURRENT_COLOR, temp_color); /* TODO(merwin): new BLF_color function? */
+	rgba_float_to_uchar(font->color, temp_color);
 
 #ifndef BLF_STANDALONE
 	VertexFormat *format = immVertexFormat();
 	unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
 	unsigned texCoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
-	unsigned color = add_attrib(format, "color", GL_FLOAT, 4, KEEP_FLOAT);
+	unsigned color = add_attrib(format, "color", GL_UNSIGNED_BYTE, 4, NORMALIZE_INT_TO_FLOAT);
 
 	BLI_assert(pos == BLF_POS_ID);
 	BLI_assert(texCoord == BLF_COORD_ID);
@@ -862,7 +864,7 @@ void BLF_shadow(int fontid, int level, const float rgba[4])
 
 	if (font) {
 		font->shadow = level;
-		copy_v4_v4(font->shadow_col, rgba);
+		rgba_float_to_uchar(font->shadow_color, rgba);
 	}
 }
 
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index 746aa72..b1bab13 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -315,22 +315,26 @@ void blf_glyph_free(GlyphBLF *g)
 	MEM_freeN(g);
 }
 
-static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
+static void blf_texture_draw(const unsigned char color[4], float uv[2][2], float dx, float y1, float dx1, float y2)
 {
 	immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]);
+	immSkipAttrib(BLF_COLOR_ID); /* skip color of most vertices */
 	immVertex2f(BLF_POS_ID, dx, y1);
 
 	immAttrib2f(BLF_COORD_ID, uv[0][0], uv[1][1]);
+	immSkipAttrib(BLF_COLOR_ID);
 	immVertex2f(BLF_POS_ID, dx, y2);
 
 	immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]);
+	immSkipAttrib(BLF_COLOR_ID);
 	immVertex2f(BLF_POS_ID, dx1, y2);
 
 	immAttrib2f(BLF_COORD_ID, uv[1][0], uv[0][1]);
+	immAttrib4ubv(BLF_COLOR_ID, color); /* set color of provoking vertex */
 	immVertex2f(BLF_POS_ID, dx1, y1);
 }
 
-static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
+static void blf_texture5_draw(const unsigned char color_in[4], float uv[2][2], float x1, float y1, float x2, float y2)
 {
 	const float soft[25] = {1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f,
 	                        1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f,
@@ -339,41 +343,43 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x
 	                        1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f};
 
 	const float *fp = soft;
-	float color[4];
+	unsigned char color[4];
 	float dx, dy;
 
-	color[0] = shadow_col[0];
-	color[1] = shadow_col[1];
-	color[2] = shadow_col[2];
+	color[0] = color_in[0];
+	color[1] = color_in[1];
+	color[2] = color_in[2];
+
+	const float alpha_in = (1 / 255.0f) * color_in[3];
 
 	for (dx = -2; dx < 3; dx++) {
 		for (dy = -2; dy < 3; dy++, fp++) {
-			color[3] = *(fp) * shadow_col[3];
-			immAttrib4fv(BLF_COLOR_ID, color);
-			blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
+			color[3] = FTOCHAR(*fp * alpha_in);
+			blf_texture_draw(color, uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
 		}
 	}
 }
 
-static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
+static void blf_texture3_draw(const unsigned char color_in[4], float uv[2][2], float x1, float y1, float x2, float y2)
 {
 	const float soft[9] = {1 / 16.0f, 2 / 16.0f, 1 / 16.0f,
 	                       2 / 16.0f, 4 / 16.0f, 2 / 16.0f,
 	                       1 / 16.0f, 2 / 16.0f, 1 / 16.0f};
 
 	const float *fp = soft;
-	float color[4];
+	unsigned char color[4];
 	float dx, dy;
 
-	color[0] = shadow_col[0];
-	color[1] = shadow_col[1];
-	color[2] = shadow_col[2];
+	color[0] = color_in[0];
+	color[1] = color_in[1];
+	color[2] = color_in[2];
+
+	const float alpha_in = (1 / 255.0f) * color_in[3];
 
 	for (dx = -1; dx < 2; dx++) {
 		for (dy = -1; dy < 2; dy++, fp++) {
-			color[3] = *(fp) * shadow_col[3];
-			immAttrib4fv(BLF_COLOR_ID, color);
-			blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
+			color[3] = FTOCHAR(*fp * alpha_in);
+			blf_texture_draw(color, uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
 		}
 	}
 }
@@ -478,31 +484,28 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
 		                    y + (float)font->shadow_y);
 
 		if (font->shadow == 0) {
-			immAttrib4fv(BLF_COLOR_ID, font->shadow_col);
-			blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
+			blf_texture_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
 		}
 		else if (font->shadow <= 4) {
-			blf_texture3_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
+			blf_texture3_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
 		}
 		else {
-			blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
+			blf_texture5_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
 		}
 	}
 
 #if BLF_BLUR_ENABLE
 	switch (font->blur) {
 		case 3:
-			blf_texture3_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+			blf_texture3_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
 			break;
 		case 5:
-			blf_texture5_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+			blf_texture5_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
 			break;
 		default:
-			immAttrib4fv(BLF_COLOR_ID, font->orig_col);
-			blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+			blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
 	}
 #else
-	immAttrib4fv(BLF_COLOR_ID, font->orig_col);
-	blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+	blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
 #endif
 }
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index 26d7fd3..7cddb46 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -188,10 +188,10 @@ typedef struct FontBLF {
 	int shadow_y;
 
 	/* shadow color. */
-	float shadow_col[4];
+	unsigned char shadow_color[4];
 
-	/* store color here when drawing shadow or blur. */
-	float orig_col[4];
+	/* main text color. */
+	unsigned char color[4];
 
 	/* Multiplied this matrix with the current one before
 	 * draw the text! see blf_draw__start.




More information about the Bf-blender-cvs mailing list