News for hackers. Stuff that matters.
Re:John Carmack: Steps to Avoid Aliasing in VR by nighthawk454 on Thursday July 28, 2016 @06:05PM (#12183549)
Post content:

In almost all of the public VR critiques I have made, I comment about aliasing. Aliasing is the technical term for when a signal is under sampled, and it shows up in many ways across graphics (and audio), but in VR this is seen most characteristically as "shimmering" imagery when you move your head around, even slightly. This doesn't need to happen! When you do everything right, the world feels rock solid as you look around. The photos in the Oculus 360 Photos app are properly filtered -- everything should ideally feel that solid.

In an ideal world, this would be turned into a slick article with before-and-after looping animated GIFs of each point I am making, along with screenshots of which checkboxes you need to hit in Unity to do the various things. Maybe someone in devrel can find the time...

All together in one place now, the formula for avoiding aliasing, from the most important: Generate mip maps for every texture being used, and set GL_LINEAR_MIPMAP_LINEAR filtering. This is easy, but missed by so many titles it makes me cringe.

If you dynamically generate textures, there is a little more excuse for it, but glGenerateMipmap() is not a crazy thing to do every frame if necessary.

Use MSAA for rendering. Not using MSAA should be an immediate fail for any app submission, but I still see some apps with full jaggy edges. We currently default to 4x MSAA on Mali and 2x MSAA on Adreno because there is a modest performance cost going to 4x there. We may want to go 4x everywhere, and it should certainly be the first knob turned to improve quality on a Qualcomm S7, long before considering an increase in eye buffer resolution.

In general, using MSAA means you can't use a deferred rendering engine. You don't want to for performance reasons, anyway. On PC, you can get by throwing lots of resources at it, but not on mobile.

Even 4x MSAA still leaves you with edges that have a bit of crawl, since it only gives you two bits of blending quality. For characters and the environment you just live with it, but for simple things like a floating UI panel, you can usually arrange to use alpha blending to get eight bits of blending quality, which is essentially perfect. Blending requires sorting, and ensuring a 0 alpha border around your images is a little harder than it first appears, since the size of a border gets halved with each mip level you descend. Depending on how shrunk down a piece of imagery may get, you may want to leave an eight or more pixel cleared alpha border around it. To avoid unexpected fringe colors around the graphics, make sure that the colors stay consistent all the way to the edge, even where the alpha channel is 0. It is very common to see blended graphics with a dark fringe around them because the texture went to 0 0 0 0 right at the outline, rather than just cutting it out in the alpha channel. Adding an alpha border also fixes the common problem of not getting CLAMP_TO_EDGE set properly on UI tiles, since if it fades to invisible at the edges, it doesn't matter if you are wrapping half a texel from the other side.

Don't use techniques like alpha testing or anything with a discard in the fragment shader unless you are absolutely sure that the contribution to the frame buffer has reached zero before the pixels are discarded. The best case for the standard cutout uses is to use blending, but if the sorting isn't feasible, using ALPHA_FROM_COVERAGE is a middle ground. Avoid geometry that can alias. The classic case is things like power lines -- you can model a nice drooping catenary of thin little tubes or crossed ribbons, but as it recedes into the distance it will rapidly turn into a scattered, aliasing mess of pixels along the line with only the 1 or 2 bits of blending you get from MSAA. There are techniques for turning such things into blends, but the easiest thing to do is just avoid it in the design phase. Anything very thin should be considered carefully.

Don't try to do accurate dynamic shadows on GearVR. Dynamic shadows are rarely aliasing free and high quality even on AAA PC titles, cutting the resolution by a factor of 16 and using a single sample so it runs reasonably performant on GearVR makes it hopeless. Go back to Quake 3 style and just blend a blurry blob underneath moving things, unless you really, really know what you are doing.

Don't use specular highlights on bump maps. This is hard to get really right even with significant resources. Specular at the geometry level (still calculated in the fragment shader, not the vertex level!) is usually ok, and also a powerful stereoscopic cue. This applies to any graphics calculation done in shaders beyond just sampling a texture -- if it has any frequency component greater than the pixel rate (a clamp is infinite frequency!), there will be aliasing. Think very carefully before doing anything clever in a shader.

Use gamma correct rendering. This is usually talked about in game dev circles as part of "physically based rendering", and it does matter for correct lighting calculations, but it isn't appreciated as widely that it matters for texture sampling and MSAA even when no lighting at all is done. For photos and other continuous tone images this barely matters at all, but for high contrast line art it is still important. The most critical high contrast line art is text. It is hard to ease into this, you need to convert your window, all textures, and any frame buffer objects you use too the correct sRGB formats. Some formats, like 4444, don't have an equivalent sRGB version, so it may involve going to a full 32 bit format.

Avoid unshared vertexes. A model edge that abruptly changes between two surfaces has the same aliasing characteristics as a silhouette at the edge of a model; you only get the MSAA bits of blending. If textures are wrapped completely around a model, and all the vertex normals and other attributes are shared, you only get the edge effects along the "pelting lines" where you have no choice but to have unmatched texture coordinates. It costs geometry, so it isn't always advisable, but even seemingly hard edged things like a cube will look better if you have a small bevel with shared vertexes crossing all the edges, rather than a knife-sharp 90 degree angle. If a surface is using baked lighting, the geometry doesn't need normals, and you are fine wrapping around hard angles as long as the texture coordinates are shared.

Trilinear filtering on textures is not what you ideally want -- it blends a too-aliased sample together with a too-blurry sample to try to limit the negatives of each. High contrast textures, like, say, the floor grates in Doom 3, or the white shutters in Tuscany, can still visibly alias even with trilinear. Reducing the contrast in the image by "prefiltering" it can fix the problem, or you can programmatically add an LOD bias.

Once aliasing is under control, you can start looking at optimizing quality.

Re:John Carmack: Steps to Avoid Aliasing in VR by LeifCarrotson on Thursday July 28, 2016 @08:44PM (#12184362)
Thank you! Reading the title, I was interested. Then I saw the domain and thought "ugh". Then I remembered that Carmack is with Oculus, and Facebook bought Oculus, making it a natural place for him to post, and was conflicted. Conflict resolved!
Re:John Carmack: Steps to Avoid Aliasing in VR by taneq on Thursday July 28, 2016 @09:06PM (#12184439)
It's not a natural place for him to post. He was bought. From what I've seen of the guy, the natural place for him to post would be in a .plan file somewhere.
Re:John Carmack: Steps to Avoid Aliasing in VR by tomphoolery on Thursday July 28, 2016 @09:26PM (#12184506)
There's nothing wrong with posting a technical article on Facebook, especially if you're too busy being a graphical programming genius to set up your own blog somewhere. I don't see it as any different than posting on Medium...besides making a choice as to which company you're going to support? But seriously, who cares?
Re:John Carmack: Steps to Avoid Aliasing in VR by erikpukinskis on Friday July 29, 2016 @12:29AM (#12185033)
The only thing wrong with it is that you generally have to have a Facebook account to read Facebook content, which means you have to give them your real name. It appears that this month Facebook is allowing people to read this particular post, but they aggressively put the sign up wall wherever they thing it will generate the most conversions. You certainly can not see John Carmack's past posts without a Facebook account. Facebook is not the open web.

Technically you can violate the ToS and set up a fake account, but that's a bit of an inconvenience

Re:John Carmack: Steps to Avoid Aliasing in VR by mcpherrinm on Friday July 29, 2016 @01:49AM (#12185207)
Whether or not a post is available to "the public" (signed in or not) is completely the choice of the author, not some mysterious committee at Facebook.
Re:John Carmack: Steps to Avoid Aliasing in VR by rmtew on Friday July 29, 2016 @04:10AM (#12185550)
Have you tried to read a page on facebook without an account? It's intended to be an almost unusable experience.

On loading a page, I get a modal popup obscuring all content insisting I "register" or "login", with a "not now" option. Then when you click "not now", the bottom third of the screen is obscured by a fixed overlay.

It is loosely describable as readable, but in practice, it's a fight to read it. The element has randomly generated id, so can't easily be blocked with ublock or similar.

Re:John Carmack: Steps to Avoid Aliasing in VR by tmsbrg on Friday July 29, 2016 @05:56AM (#12185821)
I agree Facebook is a bad place for placing public content. Note though that it's currently a lot more bearable if you disable JavaScript for Facebook. As someone who doesn't use Facebook at all it's easy for me to keep it disabled with NoScript. Then I just see content, no popups.
Re:John Carmack: Steps to Avoid Aliasing in VR by xxs on Friday July 29, 2016 @04:29AM (#12185595)
indeed: inspect element -> style='display: none' should not be mandatory to read a page. Grease monkey is an option, but still.
Re:John Carmack: Steps to Avoid Aliasing in VR by intoverflow2 on Friday July 29, 2016 @05:55AM (#12185818)
It's coded in react or whatever so it just gets recreated a second or so later.
Re:John Carmack: Steps to Avoid Aliasing in VR by zdkl on Friday July 29, 2016 @05:41AM (#12185779)
And what happens when facebook decides otherwise? Because it seems they already have with their strategy of curating content and information to maximise engagement.
Re:John Carmack: Steps to Avoid Aliasing in VR by Alupis on Thursday July 28, 2016 @09:57PM (#12184599)
> There's nothing wrong with posting a technical article on Facebook, especially if you're too busy being a graphical programming genius to set up your own blog somewhere

Well, he could have posted on the Oculus blog[1] - as the subject matter would have been very relevant.

> I don't see it as any different than posting on Medium

Medium doesn't try to coerce readers into singing up for an account. Nor does it track you across domains. Nor does it smash a lengthy article into 1/4 page width.

> But seriously, who cares?

A lot of folks, myself included. Not only was it difficult to read due to the formatting, but many have strong objections to Facebook in general, causing Carmack's thoughts have less reach.

Facebook is not a blogging platform, it never has been a blogging platform, and it never will be a blogging platform. It's simply not setup to be conducive of blog content. I wager this is the reason Facebook decided to not use Facebook for their own blog posts[2].

[1] https://www.oculus.com/blog/

[2] https://developers.facebook.com/blog

Re:John Carmack: Steps to Avoid Aliasing in VR by andrepd on Thursday July 28, 2016 @09:51PM (#12184571)
That I know of, Medium doesn't have as it's core business the mining and sale of user data with major intrusion and breach of privacy, much less a self-hosted, or even non-self-hosted blog.
Re:John Carmack: Steps to Avoid Aliasing in VR by intoverflow2 on Friday July 29, 2016 @05:55AM (#12185816)
>There's nothing wrong with posting a technical article on Facebook

Not what I think of when I think "technical article" http://i.imgur.com/nmoFIQL.png

Re:John Carmack: Steps to Avoid Aliasing in VR by ralfd on Friday July 29, 2016 @04:04AM (#12185531)
Can you format text (eg different fonts for code snippets) in facebook posts?
Re:John Carmack: Steps to Avoid Aliasing in VR by sbierwagen on Thursday July 28, 2016 @07:40PM (#12184036)
fold --spaces --width 75 | sed -e 's/^/ /'

  In almost all of the public VR critiques I have made, I comment about
  aliasing. Aliasing is the technical term for when a signal is under
  sampled, and it shows up in many ways across graphics (and audio), but in
  VR this is seen most characteristically as "shimmering" imagery when you
  move your head around, even slightly. This doesn't need to happen! When
  you do everything right, the world feels rock solid as you look around.
  The photos in the Oculus 360 Photos app are properly filtered --
  everything should ideally feel that solid.

  In an ideal world, this would be turned into a slick article with
  before-and-after looping animated GIFs of each point I am making, along
  with screenshots of which checkboxes you need to hit in Unity to do the
  various things. Maybe someone in devrel can find the time...

  All together in one place now, the formula for avoiding aliasing, from the
  most important:

  Generate mip maps for every texture being used, and set
  GL_LINEAR_MIPMAP_LINEAR filtering. This is easy, but missed by so many
  titles it makes me cringe.

  If you dynamically generate textures, there is a little more excuse for
  it, but glGenerateMipmap() is not a crazy thing to do every frame if
  necessary.

  Use MSAA for rendering. Not using MSAA should be an immediate fail for any
  app submission, but I still see some apps with full jaggy edges. We
  currently default to 4x MSAA on Mali and 2x MSAA on Adreno because there
  is a modest performance cost going to 4x there. We may want to go 4x
  everywhere, and it should certainly be the first knob turned to improve
  quality on a Qualcomm S7, long before considering an increase in eye
  buffer resolution.

  In general, using MSAA means you can't use a deferred rendering engine.
  You don't want to for performance reasons, anyway. On PC, you can get by
  throwing lots of resources at it, but not on mobile.

  Even 4x MSAA still leaves you with edges that have a bit of crawl, since
  it only gives you two bits of blending quality. For characters and the
  environment you just live with it, but for simple things like a floating
  UI panel, you can usually arrange to use alpha blending to get eight bits
  of blending quality, which is essentially perfect. Blending requires
  sorting, and ensuring a 0 alpha border around your images is a little
  harder than it first appears, since the size of a border gets halved with
  each mip level you descend. Depending on how shrunk down a piece of
  imagery may get, you may want to leave an eight or more pixel cleared
  alpha border around it. To avoid unexpected fringe colors around the
  graphics, make sure that the colors stay consistent all the way to the
  edge, even where the alpha channel is 0. It is very common to see blended
  graphics with a dark fringe around them because the texture went to 0 0 0
  0 right at the outline, rather than just cutting it out in the alpha
  channel. Adding an alpha
  border also fixes the common problem of not getting CLAMP_TO_EDGE set
  properly on UI tiles, since if it fades to invisible at the edges, it
  doesn't matter if you are wrapping half a texel from the other side.

  Don't use techniques like alpha testing or anything with a discard in the
  fragment shader unless you are absolutely sure that the contribution to
  the frame buffer has reached zero before the pixels are discarded. The
  best case for the standard cutout uses is to use blending, but if the
  sorting isn't feasible, using ALPHA_FROM_COVERAGE is a middle ground.

  Avoid geometry that can alias. The classic case is things like power lines
  -- you can model a nice drooping catenary of thin little tubes or crossed
  ribbons, but as it recedes into the distance it will rapidly turn into a
  scattered, aliasing mess of pixels along the line with only the 1 or 2
  bits of blending you get from MSAA. There are techniques for turning such
  things into blends, but the easiest thing to do is just avoid it in the
  design phase. Anything very thin should be considered carefully.

  Don't try to do accurate dynamic shadows on GearVR. Dynamic shadows are
  rarely aliasing free and high quality even on AAA PC titles, cutting the
  resolution by a factor of 16 and using a single sample so it runs
  reasonably performant on GearVR makes it hopeless. Go back to Quake 3
  style and just blend a blurry blob underneath moving things, unless you
  really, really know what you are doing.

  Don't use specular highlights on bump maps. This is hard to get really
  right even with significant resources. Specular at the geometry level
  (still calculated in the fragment shader, not the vertex level!) is
  usually ok, and also a powerful stereoscopic cue. This applies to any
  graphics calculation done in shaders beyond just sampling a texture -- if
  it has any frequency component greater than the pixel rate (a clamp is
  infinite frequency!), there will be aliasing. Think very carefully before
  doing anything clever in a shader.
  
  Use gamma correct rendering. This is usually talked about in game dev
  circles as part of "physically based rendering", and it does matter for
  correct lighting calculations, but it isn't appreciated as widely that it
  matters for texture sampling and MSAA even when no lighting at all is
  done. For photos and other continuous tone images this barely matters at
  all, but for high contrast line art it is still important. The most
  critical high contrast line art is text. It is hard to ease into this, you
  need to convert your window, all textures, and any frame buffer objects
  you use too the correct sRGB formats. Some formats, like 4444, don't have
  an equivalent sRGB version, so it may involve going to a full 32 bit
  format.

  Avoid unshared vertexes. A model edge that abruptly changes between two
  surfaces has the same aliasing characteristics as a silhouette at the edge
  of a model; you only get the MSAA bits of blending. If textures are
  wrapped completely around a model, and all the vertex normals and other
  attributes are shared, you only get the edge effects along the "pelting
  lines" where you have no choice but to have unmatched texture coordinates.
  It costs geometry, so it isn't always advisable, but even seemingly hard
  edged things like a cube will look better if you have a small bevel with
  shared vertexes crossing all the edges, rather than a knife-sharp 90
  degree angle. If a surface is using baked lighting, the geometry doesn't
  need normals, and you are fine wrapping around hard angles as long as the
  texture coordinates are shared.

  Trilinear filtering on textures is not what you ideally want -- it blends
  a too-aliased sample together with a too-blurry sample to try to limit the
  negatives of each. High contrast textures, like, say, the floor grates in
  Doom 3, or the white shutters in Tuscany, can still visibly alias even
  with trilinear. Reducing the contrast in the image by "prefiltering" it
  can fix the problem, or you can programmatically add an LOD bias.

  Once aliasing is under control, you can start looking at optimizing
  quality.
Re:John Carmack: Steps to Avoid Aliasing in VR by vacri on Thursday July 28, 2016 @10:42PM (#12184749)
TIL 'fold', thank you.

I've always been a big fan of including the command you use to get the output you're quoting - people sometimes pick up even subtle tricks or flags from doing this.

Re:John Carmack: Steps to Avoid Aliasing in VR by taeric on Friday July 29, 2016 @12:43AM (#12185060)
Oddly this is one of the tricks in Programming Pearls that I love, but can't get others to want to do.

It doesn't help that json prohibited comments. So, anything that modified json is incapable of adding a breadcrumb of what it did.

Re:John Carmack: Steps to Avoid Aliasing in VR by retox on Thursday July 28, 2016 @07:58PM (#12184128)
When will HN fix the dumb issue with pre text being maxed to 50% width on android. Having to scroll each line means I don't even bother.

Thank you for taking this time to post it though.