News for hackers. Stuff that matters.
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.