[Bf-blender-cvs] [53d82c3] blender2.8: BLF/OpenGL: draw text with new immediate mode

Mike Erwin noreply at git.blender.org
Tue Oct 11 20:37:28 CEST 2016


Commit: 53d82c3e8d608bc05e40e26950a98d15b16c562c
Author: Mike Erwin
Date:   Tue Oct 11 14:36:16 2016 -0400
Branches: blender2.8
https://developer.blender.org/rB53d82c3e8d608bc05e40e26950a98d15b16c562c

BLF/OpenGL: draw text with new immediate mode

Part of T49043

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

M	source/blender/blenfont/intern/blf.c
M	source/blender/blenfont/intern/blf_glyph.c
M	source/blender/blenfont/intern/blf_internal.h
M	source/blender/gpu/shaders/gpu_shader_text_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_text_vert.glsl

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

diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index d7fcb2c..9bd43f7 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -57,6 +57,7 @@
 
 #ifndef BLF_STANDALONE
 #include "GPU_shader.h"
+#include "GPU_immediate.h"
 #endif
 
 #include "blf_internal_types.h"
@@ -500,10 +501,6 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode)
 	glEnable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-#ifndef BLF_STANDALONE
-	GPU_shader_bind(GPU_shader_get_builtin_shader(GPU_SHADER_TEXT));
-#endif
-
 	/* Save the current matrix mode. */
 	glGetIntegerv(GL_MATRIX_MODE, mode);
 
@@ -522,8 +519,20 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode)
 	if (font->flags & BLF_ROTATION)  /* radians -> degrees */
 		glRotatef(font->angle * (float)(180.0 / M_PI), 0.0f, 0.0f, 1.0f);
 
-	if (font->shadow || font->blur)
-		glGetFloatv(GL_CURRENT_COLOR, font->orig_col);
+	glGetFloatv(GL_CURRENT_COLOR, font->orig_col); /* TODO(merwin): new BLF_color function? */
+
+#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);
+
+	BLI_assert(pos == BLF_POS_ID);
+	BLI_assert(texCoord == BLF_COORD_ID);
+	BLI_assert(color == BLF_COLOR_ID);
+
+	immBindBuiltinProgram(GPU_SHADER_TEXT);
+#endif
 
 	/* always bind the texture for the first glyph */
 	font->tex_bind_state = -1;
@@ -537,7 +546,7 @@ static void blf_draw_gl__end(GLint mode)
 		glMatrixMode(mode);
 
 #ifndef BLF_STANDALONE
-	GPU_shader_unbind();
+	immUnbindProgram();
 #endif
 
 	glDisable(GL_BLEND);
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index aa7d539..2354769 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -56,7 +56,7 @@
 #include "BLF_api.h"
 
 #ifndef BLF_STANDALONE
-#include "GPU_basic_shader.h"
+#include "GPU_immediate.h"
 #endif
 
 #include "blf_internal_types.h"
@@ -182,17 +182,6 @@ static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-
-#ifndef BLF_STANDALONE
-	/* needed since basic shader doesn't support alpha-only textures,
-	 * while we could add support this is only used in a few places
-	 * (an alternative could be to have a simple shader for BLF). */
-	if (GLEW_ARB_texture_swizzle && GPU_basic_shader_use_glsl_get()) {
-		GLint swizzle_mask[] = {GL_ONE, GL_ONE, GL_ONE, GL_ALPHA};
-		glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask);
-	}
-#endif
-
 	glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
 }
 
@@ -328,19 +317,17 @@ void blf_glyph_free(GlyphBLF *g)
 
 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
 {
-	glBegin(GL_QUADS);
-	glTexCoord2f(uv[0][0], uv[0][1]);
-	glVertex2f(dx, y1);
-	
-	glTexCoord2f(uv[0][0], uv[1][1]);
-	glVertex2f(dx, y2);
-	
-	glTexCoord2f(uv[1][0], uv[1][1]);
-	glVertex2f(dx1, y2);
-	
-	glTexCoord2f(uv[1][0], uv[0][1]);
-	glVertex2f(dx1, y1);
-	glEnd();
+	immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]);
+	immVertex2f(BLF_POS_ID, dx, y1);
+
+	immAttrib2f(BLF_COORD_ID, uv[0][0], uv[1][1]);
+	immVertex2f(BLF_POS_ID, dx, y2);
+
+	immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]);
+	immVertex2f(BLF_POS_ID, dx1, y2);
+
+	immAttrib2f(BLF_COORD_ID, uv[1][0], uv[0][1]);
+	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)
@@ -350,7 +337,7 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x
 	                        2 / 60.0f, 5 / 60.0f, 8 / 60.0f, 5 / 60.0f, 2 / 60.0f,
 	                        1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f,
 	                        1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f};
-	
+
 	const float *fp = soft;
 	float color[4];
 	float dx, dy;
@@ -358,16 +345,14 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x
 	color[0] = shadow_col[0];
 	color[1] = shadow_col[1];
 	color[2] = shadow_col[2];
-	
+
 	for (dx = -2; dx < 3; dx++) {
 		for (dy = -2; dy < 3; dy++, fp++) {
 			color[3] = *(fp) * shadow_col[3];
-			glColor4fv(color);
+			immAttrib4fv(BLF_COLOR_ID, color);
 			blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
 		}
 	}
-	
-	glColor4fv(color);
 }
 
 static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
@@ -387,12 +372,10 @@ static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x
 	for (dx = -1; dx < 2; dx++) {
 		for (dy = -1; dy < 2; dy++, fp++) {
 			color[3] = *(fp) * shadow_col[3];
-			glColor4fv(color);
+			immAttrib4fv(BLF_COLOR_ID, color);
 			blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
 		}
 	}
-	
-	glColor4fv(color);
 }
 
 static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
@@ -486,6 +469,18 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
 		glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = g->tex));
 	}
 
+#if 0 /* determine exact count of quads */
+	unsigned quad_ct = 1 + (unsigned)font->blur;
+	if (font->flags & BLF_SHADOW)
+		quad_ct += (unsigned)font->shadow;
+
+	immBegin(GL_QUADS, quad_ct * 4);
+#else
+	immBeginAtMost(GL_QUADS, 40); /* (5 shadow + 5 blur) * 4 verts per quad */
+#endif
+
+	/* TODO: blur & shadow in shader, single quad per glyph */
+
 	if (font->flags & BLF_SHADOW) {
 		rctf rect_ofs;
 		blf_glyph_calc_rect(&rect_ofs, g,
@@ -500,12 +495,9 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
 				blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
 				break;
 			default:
-				glColor4fv(font->shadow_col);
+				immAttrib4fv(BLF_COLOR_ID, font->shadow_col);
 				blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
-				break;
 		}
-
-		glColor4fv(font->orig_col);
 	}
 
 	switch (font->blur) {
@@ -516,9 +508,9 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
 			blf_texture5_draw(font->orig_col, 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);
-			break;
 	}
 
-	return;
+	immEnd();
 }
diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h
index d9d758c..ba17e05 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -37,6 +37,11 @@ struct GlyphBLF;
 struct GlyphCacheBLF;
 struct rctf;
 
+/* vertex attribute IDs (fixed IDs so we don't have to pass them around) */
+#define BLF_POS_ID 0
+#define BLF_COORD_ID 1
+#define BLF_COLOR_ID 2
+
 unsigned int blf_next_p2(unsigned int x);
 unsigned int blf_hash(unsigned int val);
 
diff --git a/source/blender/gpu/shaders/gpu_shader_text_frag.glsl b/source/blender/gpu/shaders/gpu_shader_text_frag.glsl
index c169b31..9283d68 100644
--- a/source/blender/gpu/shaders/gpu_shader_text_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_text_frag.glsl
@@ -1,12 +1,13 @@
 
 #if __VERSION__ == 120
-  flat varying vec4 color;
-  noperspective varying vec2 texcoord;
+  flat varying vec4 color_flat;
+  noperspective varying vec2 texCoord_interp;
   #define fragColor gl_FragColor
 #else
-  flat in vec4 color;
-  noperspective in vec2 texcoord;
+  flat in vec4 color_flat;
+  noperspective in vec2 texCoord_interp;
   out vec4 fragColor;
+  #define texture2D texture
 #endif
 
 uniform sampler2D glyph;
@@ -14,8 +15,8 @@ uniform sampler2D glyph;
 void main()
 {
 	// input color replaces texture color
-	fragColor.rgb = color.rgb;
+	fragColor.rgb = color_flat.rgb;
 
 	// modulate input alpha & texture alpha
-	fragColor.a = color.a * texture2D(glyph, texcoord).a;
+	fragColor.a = color_flat.a * texture2D(glyph, texCoord_interp).a;
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_text_vert.glsl b/source/blender/gpu/shaders/gpu_shader_text_vert.glsl
index bdbffdf..44568f2 100644
--- a/source/blender/gpu/shaders/gpu_shader_text_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_text_vert.glsl
@@ -1,20 +1,24 @@
 
-// TODO(merwin):
-// - uniform color, not per vertex
-// - generic attrib inputs (2D pos, tex coord)
+uniform mat4 ModelViewProjectionMatrix;
 
 #if __VERSION__ == 120
-  flat varying vec4 color;
-  noperspective varying vec2 texcoord;
+  attribute vec2 pos;
+  attribute vec2 texCoord;
+  attribute vec4 color;
+  flat varying vec4 color_flat;
+  noperspective varying vec2 texCoord_interp;
 #else
-  flat out vec4 color;
-  noperspective out vec2 texcoord;
+  in vec2 pos;
+  in vec2 texCoord;
+  in vec4 color;
+  flat out vec4 color_flat;
+  noperspective out vec2 texCoord_interp;
 #endif
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 
-	color = gl_Color;
-	texcoord = gl_MultiTexCoord0.st;
+	color_flat = color;
+	texCoord_interp = texCoord;
 }




More information about the Bf-blender-cvs mailing list