Optimisation trick: use GL_TRIANGLE_STRIP instead of GL_TRIANGLES

42 views
Skip to first unread message

sduclos

unread,
Mar 21, 2014, 6:54:50 PM3/21/14
to freetype-gl, Nicolas Rougier
Bonjours M. Rougier,

The trick is to render all text with a single call to
glDrawArrays(GL_TRIANGLE_STRIP, ..),

The simplified code bellow show how to make that happen for a string:

---------------------------------->8--------------------------------------------------
static GArray *_fill_ftglBuf(GArray *ftglBuf, const char *str)
{
int pen_x = 0;
int pen_y = 0;

if (NULL == ftglBuf) {
ftglBuf = g_array_new(FALSE, FALSE, sizeof(_freetype_gl_vertex_t));
} else {
g_array_set_size(ftglBuf, 0);
}

int len = strlen(str);
for (int i=0; i<len; ++i) {
GLfloat x0 = pen_x + glyph->offset_x;
GLfloat y0 = pen_y + glyph->offset_y;
GLfloat x1 = x0 + glyph->width;
GLfloat y1 = y0 - (glyph->height);

GLfloat s0 = glyph->s0;
GLfloat t0 = glyph->t0;
GLfloat s1 = glyph->s1;
GLfloat t1 = glyph->t1;

// GL_TRIANGLE_STRIP
GLfloat z0 = 0.0;
_freetype_gl_vertex_t vertices[4] = {
{x0,y0,z0, s0,t0},
{x0,y1,z0, s0,t1},
{x1,y0,z0, s1,t0},
{x1,y1,z0, s1,t1}
};

// connect glyphs with degenerated triangle (GPU skip those)
if (0 < ftglBuf->len) {
// dup last vertex
ftglBuf = g_array_append_vals(ftglBuf, &vertices[0], 1);
}

// glyph strip
ftglBuf = g_array_append_vals(ftglBuf, &vertices[0], 4);
// dup 3rd vertex
ftglBuf = g_array_append_vals(ftglBuf, &vertices[2], 1);

pen_x += glyph->advance_x;
pen_y += glyph->advance_y;
}

return ftglBuf;
}
---------------------------------->8--------------------------------------------------

The GPU discard degenerated triangle. So the trick is to link glyph strip using
degenerated strip.

Note how vertex in vertices are organize to form a strip.

Also note how degenerated strip are appended to glyph.

Finally there is no core code to change in freetype_gl (texture-atlas,
texture-font
vector, vec123.h)


I use glib GArray to store stuff but anything could do.



Salutation,

Sylvain.

Behdad Esfahbod

unread,
Mar 21, 2014, 7:14:40 PM3/21/14
to freet...@googlegroups.com, Nicolas Rougier
On 14-03-21 03:54 PM, sduclos wrote:
> Bonjours M. Rougier,
>
> The trick is to render all text with a single call to
> glDrawArrays(GL_TRIANGLE_STRIP, ..),

How is this an improvement over GL_TRIANGLES? Both take six vertex entries
per glyph.

behdad
--
behdad
http://behdad.org/

sduclos

unread,
Mar 21, 2014, 7:37:43 PM3/21/14
to freet...@googlegroups.com, Nicolas Rougier
Hi,

One can you fill a whole screen full of text in a single glDraw..
using GL_TRIANGLES!

Sorry I miss that, perhaps I should try to make a demo for rendering a bunch of
glyphs with triangles. Right now the demo illustrate that one glyph is
produce per
glDraw.. call. And that is expensive compare to a single glDraw for
rendering a whole page of text



rgds,

Sylvain..
> --
> You received this message because you are subscribed to the Google Groups
> "freetype-gl" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to freetype-gl...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Behdad Esfahbod

unread,
Mar 21, 2014, 7:47:29 PM3/21/14
to freet...@googlegroups.com, Nicolas Rougier
On 14-03-21 04:37 PM, sduclos wrote:
> Hi,
>
> One can you fill a whole screen full of text in a single glDraw..
> using GL_TRIANGLES!
>
> Sorry I miss that, perhaps I should try to make a demo for rendering a bunch of
> glyphs with triangles. Right now the demo illustrate that one glyph is
> produce per
> glDraw.. call. And that is expensive compare to a single glDraw for
> rendering a whole page of text

Ah I thought freetype-gl already does all in one GL_TRIANGLES. That's what
GLyphy does, and I couldn't find any way to get lower than six entries per glyph.
--
behdad
http://behdad.org/

sduclos

unread,
Mar 21, 2014, 8:05:29 PM3/21/14
to freet...@googlegroups.com, Nicolas Rougier
-- I couldn't find any way to get lower than six entries per glyph.

Use GL_TRIANGLE_FAN and 4 vertexes ..
The layout of 'vertices' is the same.

Behdad Esfahbod

unread,
Mar 21, 2014, 8:10:00 PM3/21/14
to freet...@googlegroups.com, Nicolas Rougier
On 14-03-21 05:05 PM, sduclos wrote:
> -- I couldn't find any way to get lower than six entries per glyph.
>
> Use GL_TRIANGLE_FAN and 4 vertexes ..
> The layout of 'vertices' is the same.

Then you need two extra entries for the degenerate triangles, adds up to six.
Right?
--
behdad
http://behdad.org/

sduclos

unread,
Mar 21, 2014, 8:26:29 PM3/21/14
to freet...@googlegroups.com
right!

that sound like a demo to show how FAN, STRIP, TRIAN differ
Reply all
Reply to author
Forward
0 new messages