News for hackers. Stuff that matters.
Re:Show HN: GPU text rendering with vector textures by pcwalton on Friday January 01, 2016 @11:16PM (#10825008)
This is really neat. Though in practice if you are using this kind of thing I'm pretty sure you will want to combine it with atlasing (i.e. construct an atlas of glyphs via render to texture). That's because it's a waste of time to rerasterize glyphs in the FS every frame (which appears to be 200+ lines) instead of caching the results and reducing the per-frame work to a 5-line FS that just blits. In most apps pans are more common than zooms and the same glyphs are used over and over, so this usually ends up being a win. Using atlases also reduces the number of raster operations because the same glyphs tend to appear on the page repeatedly, and by using atlases you rasterize each glyph only once. Even more importantly, though, atlasing gives you the ability to reuse a blitting fragment shader to render other content (images, figures, etc.) as well as glyphs from the atlas in order to reduce state changes.
Re:Show HN: GPU text rendering with vector textures by derefr on Saturday January 02, 2016 @03:42AM (#10825556)
There's also the higher level—the thing web browser rendering engines do, where you generate a texture for each "tile" (single-layer viewport rectangle) of likely-to-be-static text, and carry those around until you have to zoom or the text changes.

I'm not sure if you need the medium-level (font atlasing) if you have both lower-level (bezier-curve atlasing), and higher-level (pre-composited "tiles.")

This is one of the reasons I really like WebGL, actually: for GUI elements, you can usually skip rendering them within the game altogether, instead using plain HTML+CSS controls positioned above the <canvas>, where they'll be treated as separate "tiles" that aren't dirtied by whatever your game is doing. Imagine: relatively-static fully-RGBA-translucent stuff sitting on top of your animating viewport, composited into a final image each frame, "for free."

Re:Show HN: GPU text rendering with vector textures by pcwalton on Saturday January 02, 2016 @09:02AM (#10826204)
> There's also the higher level—the thing web browser rendering engines do, where you generate a texture for each "tile" (single-layer viewport rectangle) of likely-to-be-static text, and carry those around until you have to zoom or the text changes.

I think this is actually a harmful approach in most cases--all it does is reduce the number of vertices and slightly reduces overdraw in exchange for a lot more memory consumption and a lot more draw calls. Overdraw can be reduced in other ways, such as sorting front to back and taking advantage of early Z for opaque content. Web pages and PDFs are so totally not bound on either ROPs or VS, and are very sensitive to draw call count, that caching tiles to minimize overdraw is almost never worth it.

But this is somewhat off topic.

> I'm not sure if you need the medium-level (font atlasing) if you have both lower-level (bezier-curve atlasing), and higher-level (pre-composited "tiles.")

You definitely do. You're sacrificing a lot of performance for the rendering of each tile if you don't do that.

Re:Show HN: GPU text rendering with vector textures by derefr on Saturday January 02, 2016 @05:39PM (#10828342)
> Overdraw

I don't think we're imagining the same things here. Think less '3D landscape with text floating in it for some reason' (e.g. SecondLife), and more '3D landscape with labels applied to it' (e.g. Google Earth). Nothing from the "VR" layer obscures the "labels" layer; the labels float above everything, as if part of the GUI, but following the same scale as the things they label.

> PDFs

I'm actually unsure why a (non-interactive) PDF page, displayed at 100% zoom, can't be baked into a single extremely-high-resolution tile—applied as a texture to a single GL rectangle with plain-old lanzcos downsampling. That'd work for the overwhelming majority of the frames.

You'd only need to re-render the tile when you zoom in enough that it'd look bad; effectively, you could think of the PDF page as coming from a mipmapped texture, but where you only have the current size and will just-in-time rerender when the zoom-factor changes. And then, when the zoom factor makes the PDF not fit on the screen, the "tile" would be the screen size—so you'd re-render sibling tiles when the user moves.

Or, in other words: PDFs can be rendered exactly the way the image tiles on a mapping website are rendered, can't they? Doesn't that approach win over re-rendering all the text from a font-atlas every frame? And if not, why do browser rendering engines use tiles? (This isn't a rhetorical question to make my point; I'm not a graphics dev and I honestly don't see why.)

Re:Show HN: GPU text rendering with vector textures by pcwalton on Saturday January 02, 2016 @06:30PM (#10828565)
> I'm actually unsure why a (non-interactive) PDF page, displayed at 100% zoom, can't be baked into a single extremely-high-resolution tile—applied as a texture to a single GL rectangle with plain-old lanzcos downsampling. That'd work for the overwhelming majority of the frames.

Sure, but that uses a lot of memory, and initial pageload is going to be really slow. Especially when you consider you need to generate mipmaps if it's really high resolution, or else blow out your GPU's L1/L2 cache in FS execution.

> Doesn't that approach win over re-rendering all the text from a font-atlas every frame?

Not significantly, in my experience. Assume, in the simple case, that you're just rendering a bunch of text with no overlap. In that case, the only thing that tiles buy you over rerendering all the text from an atlas is decreased vertex count. You're still touching the same number of pixels in the FS and ROP units either way: you just do so with more vertices in the texture atlas case and fewer vertices in the tiling case. Now consider that creating and maintaining the tiles has costs over maintaining the glyph atlas (which you have to do either way): you have the memory of the tiles, the overhead of creating and switching FBOs, lots of little textures to keep around (which in naive implementations results in tons of state changes), and extra draw calls to render the tiles after rendering the content.

> why do browser rendering engines use tiles?

Mostly because it was an easy way to fit GPU-accelerated panning and zooming into the existing, originally CPU-based, rendering architectures that browser engines used (and still largely use). Tiling was popularized by Mobile Safari on the iPhone in 2007 as an easy way to avoid repainting the entire page on the CPU every frame when the user performed touch gestures. I don't think it's necessarily a globally optimal decision.

Re:Show HN: GPU text rendering with vector textures by panic on Saturday January 02, 2016 @05:49AM (#10825804)
The font atlas is definitely still worth it. For example: your comment, which fits in a single browser tile, is made up of 735 drawn glyphs. There are 50 distinct glyphs used (not counting italics because I'm lazy).

Assuming it takes twice as long to draw a glyph into an atlas but half as long to draw it once it's in the atlas, an atlas that starts completely empty will speed up text drawing for your comment by 66% (735 divided by 50 * 2 + (735 - 50) * 0.5, which is 442.5). And this underestimates the savings by a lot (drawing glyphs from an atlas is much more than two times as fast, and some glyphs are probably in the atlas already).

Re:Show HN: GPU text rendering with vector textures by derefr on Saturday January 02, 2016 @05:12PM (#10828245)
The question is about amortized time-cost, though.

• Most frames won't involve zooming or text-change—and therefore will be handled at "zero" time-cost by re-using the pre-composited tile.

• The frames that do involve text-change (but not zoom) can benefit from font-atlasing.

• The frames that involve discrete zoom, where text goes from one integral font size (represented in the atlas) to another integral font size (also in the atlas) can also benefit from font-atlasing.

• The frames that involve continuous zoom—such as in this demo—can't benefit from font-atlasing, because the frames where the font is e.g. 7.037773pt large can't be served from the atlas.

Keep in mind that the only reason we do discrete zoom-levels in PDF readers, text editors, etc. is that font-atlasing is "the optimization we have", and it calls for discrete zoom levels. When the UX affordance is for continuous zoom—think about pinch-to-zoom in Mobile Safari, or about zooming into Google Earth—font-atlasing doesn't and can't work. But this technique can.

Now, in games in particular, "continuous zoom" is a pretty rare edge-case. GUI elements don't tend to get zoomed into[1].

And "in-VR" elements, like street signs in an FPS, aren't usually designed as a bunch of geometry[2], but rather as a very high-detail flat texture that maybe gets mipmapped from far away. "Zooming" by approaching a street sign doesn't make the fonts on it re-render themselves; there are no fonts on it, they're just baked into an image.

---

[1] Well, maybe; I could see a case for continuous zoom of the fonts—and other GUI geometry—plastered over units in RTS games. "Pan up" with units selected, they stay selected but their unit names+stats get smaller and smaller and eventually get obscured by draw-distance and atmosphere effects, just as if they were actually "there" in the scene instead of part of the GUI. (Makes me think of the windows in the TV show ReBoot.)

[2] This does make me imagine a Blade Runner-esque game full of holographic signs, though. Those could definitely benefit from this approach.)

Re:Show HN: GPU text rendering with vector textures by panic on Saturday January 02, 2016 @05:22PM (#10828277)
Font atlasing can and does work during pinch-to-zoom: that was the point I was trying to make. You have to generate a new atlas each time you paint, but drawing multiple "e" glyphs from an atlas is much cheaper than rasterizing each "e" individually.
Re:Show HN: GPU text rendering with vector textures by derefr on Saturday January 02, 2016 @05:27PM (#10828294)
Huh; I almost don't think of that as font-atlasing. If you know you're continuously zooming—and therefore are going to throw out the atlas at the end of the frame—it's almost more like a "glyphs rendered this frame" cache. Which changes things a bit, because you don't need to preload the entirety of the "font"—which might be a unicode font!—into the atlas, but only what you're rendering.

I have no argument against that kind of font-atlasing as a process; it's fairly more stateless, more like a secret cache inside the black box of the glyph-rendering kernel.

Re:Show HN: GPU text rendering with vector textures by wjd on Saturday January 02, 2016 @07:45PM (#10828870)
Consider that each "e" may be slightly differently aligned to the pixel grid though. So using an atlas is lower quality than drawing each "e" from scratch exactly as it aligns to the screen.
Re:Show HN: GPU text rendering with vector textures by baldajan on Saturday January 02, 2016 @12:00AM (#10825125)
That was my first thought. This is brilliant, but the FS is way overworked, making this less practical and going back to the original solution (because it's fast).

It's a trade off between speed and variability, which for games (and most apps) speed trump variability (especially because of consistent design). But given generating a texture atlas is a "1 time task"; and recompiling the curves in the GPU is per frame in the paper, the next best thing I guess is off loading the glyph atlas generator to the GPU, not CPU, since the GPU maybe faster.

It's really awesome research!

Re:Show HN: GPU text rendering with vector textures by carterehsmith on Saturday January 02, 2016 @12:11AM (#10825155)
Wait. I guess you did not read the article at all? It is all about atlasing.

There are issues with atlas of glyphs; so rather than putting the whole glyphs in atlas, he slices the glyphs in a grid, the grid being defined by the intersection of the underlying Bezier curves. And then he puts the grid pieces in atlas.

Re:Show HN: GPU text rendering with vector textures by pcwalton on Saturday January 02, 2016 @12:28AM (#10825206)
> Wait. I guess you did not read the article at all? It is all about atlasing.

Yes, I understand the technique. Perhaps I should be more clear about my terminology. I'm using "atlas" in the narrow sense of "actual pieces of bitmap data stored in a texture". My point is that this technique should not be a substitute for atlasing in this sense, but could well be combined with it.

Re:Show HN: GPU text rendering with vector textures by CyberDildonics on Saturday January 02, 2016 @08:54AM (#10826181)
I don't know why you are being downvoted, that is what is happening and I am not convinced that straight textures would be better. We already know that SDF textures are better than straight texture atlases anyway. The technique is obviously practical in some respect (since it renders a pdf faster than anything else I've seen) and the quality of the text is very sharp, even close up.

Super sharp lines when zoomed in may not seem like a big deal, but as you increase resolution you will reap the benefits since the same size text will be sharper.

Re:Show HN: GPU text rendering with vector textures by pcwalton on Saturday January 02, 2016 @09:05AM (#10826212)
The FS is something like 200 lines (verified in the WebGL debugger). A blitting shader is about 5. If fragment shading load doesn't matter, then of course you don't need straight textures. But if you want to reduce the load on the GPU and do the efficient thing, without any quality loss, it's better to use atlases.

> We already know that SDF textures are better than straight texture atlases anyway.

Even that depends. SDFs are slow to construct and, because they have a special FS, you have to pay attention to the number of state changes. They do have the advantage (unlike this technique) of having a very simple FS--they can even be used in some fixed function hardware!--making it practical to rerasterize glyphs every frame.

> The technique is obviously practical in some respect (since it renders a pdf faster than anything else I've seen)

That says a lot about how existing PDF renderers are slow and nothing about what is the fastest thing to do on the GPU.

Re:Show HN: GPU text rendering with vector textures by armitron on Saturday January 02, 2016 @09:04AM (#10826208)
> We already know that SDF textures are better than straight texture atlases anyway.

This is isn't true. SDF textures can be "better" for "some" applications (like games that want to display stencils mostly at arbitrary zoom levels without losing a lot of quality) but they're also visibly lower-quality compared to glyphs rendered by freetype at each one of these zoom levels.

So, for an application that simply displays lots of text while infrequently changing the glyph size, SDF textures are not a good solution.

Re:Show HN: GPU text rendering with vector textures by CyberDildonics on Sunday January 03, 2016 @11:37AM (#10831144)
You are essentially saying that an SDF texture isn't as good as a freetype glyph rendered at the resolution it is being displayed at. If you don't have to change the zoom of what you are looking at you don't have to use a texture at all, you can just use the pixels from the freetype vector render directly.
Re:Show HN: GPU text rendering with vector textures by crzwdjk on Sunday January 03, 2016 @02:45PM (#10831986)
This latter bit is exactly what modern 2D display drivers already do, or at least what the NVIDIA binary X driver does. It keeps a glyph cache in video memory and just blits from it to the extent that it can. It's still counting on the software to do the rasterization though, and I don't think there's a way to do the rasterizing on the GPU and then feed that data straight into the glyph cache.