Hashed Alpha Testing
Chris Wyman
GDC 2017

Traditional

HashedWhy Alpha Test?
• Alpha testing has advantages over alpha blend• Order independent, cheap, for forward or deferred• Extends to MSAA, via alpha-to-coverage

www.gameworks.nvidia.com

2But… Problem• Alpha testing has advantages over alpha blend• Order independent, cheap, for forward or deferred• Extends to MSAA, via alpha-to-coverage• But alpha-tested geom can disappear w / distancewww.gameworks.nvidia.com

3But… Problem• Alpha testing has advantages over alpha blend• Order independent, cheap, for forward or deferred• Extends to MSAA, via alpha-to-coverage• But alpha-tested geom can disappear w / distance• Why? Cannot prefilter binary querieswww.gameworks.nvidia.com

4New Solution: Hashed Alpha• Use stochastic sampling to avoid this problem

www.gameworks.nvidia.com

5New Solution: Hashed Alpha• Use stochastic sampling to avoid this problem• Basic idea is replace standard test:• With this stochastic test:

www.gameworks.nvidia.com

6New Solution: Hashed Alpha• Use stochastic sampling to avoid this problem• Basic idea is replace standard test:• With this stochastic test:• But this flickers like crazy• Want temporal stability, esp. under slight motion• Use stable, procedural noise:www.gameworks.nvidia.com

7What Does this Look Like?

Alpha Test

Hashed Alphawww.gameworks.nvidia.com

Ground Truth
8

Talk Takeaways• Stochasm addresses problems with alpha testing• Converges to ground truth (OIT) with enough random samples

www.gameworks.nvidia.com

9

Talk Takeaways• Stochasm addresses problems with alpha testing• Converges to ground truth (OIT) with enough random samples• Hashing can give noise stable over time

www.gameworks.nvidia.com

10

Talk Takeaways• Stochasm addresses problems with alpha testing• Converges to ground truth (OIT) with enough random samples• Hashing can give noise stable over time

• By constraining hash inputs:• Control noise behavior• Ensure samples remain largely stable between frames

www.gameworks.nvidia.com

11

Talk Takeaways• Stochasm addresses problems with alpha testing• Converges to ground truth (OIT) with enough random samples• Hashing can give noise stable over time• By constraining hash inputs:• Control noise behavior• Ensure samples remain largely stable between frames• Also applies to alpha-to-coverage, screen-door transparency, etc.

www.gameworks.nvidia.com

12

Do Stochastic Alpha Thresholds Work?

www.gameworks.nvidia.com

13

Stochastic Alpha Thresholds Work

Alpha test

1 sample4 samples

16 samples

www.gameworks.nvidia.com

64 samples

OIT withalpha blend
14

Stochastic Alpha Thresholds Work
Traditional Alpha

Alpha-to-Coverage

1/2

1 sample, selected uniformly
on interval [0..1]

1/8

1/8

3/8

3/8

5/8

5/87/8

7/8

N samples, selected uniformly
on interval [0..1]

Alpha test

1 sample4 samples

Stochastic Alpha (aka stochastic transparency)

16 samples

www.gameworks.nvidia.com

N samples, selected randomly
on interval [0..1]

64 samples

OIT withalpha blend
15

Stochastic Alpha Thresholds Work
Traditional Alpha

Alpha-to-Coverage

1/2

1 sample, selected uniformly
on interval [0..1]

Stochastic Alpha (aka stochastic transparency)

1/8

1/8

3/8

3/8

5/8

5/8

7/8

7/8

N samples, selected uniformly
on interval [0..1]

N samples, selected randomly
on interval [0..1]

But stochastic algorithms change each frame, causing severe temporal flickering!
Alpha test

1 sample4 samples

16 samples

www.gameworks.nvidia.com

64 samples

OIT withalpha blend
16

Why Hashing?
• Hashing long known as a way of ‘randomizing’• See “Numerical Recipes” for an example PRNG using DES encryption

www.gameworks.nvidia.com

17Why Hashing?
• Hashing long known as a way of ‘randomizing’• See “Numerical Recipes” for an example PRNG using DES encryption• Good hash function properties:• Determinism given fixed inputi.e., gives same value each frame• Defined range of outputs

i.e., in range [0…1)• Uniformity over output rangei.e., uniform outputs in range [0…1)

www.gameworks.nvidia.com

18

What Does This Mean?
• Consider the following, with good hash function hash( ):float hashSample = hash( myPosition );• Gives noisy hashSample like RNG per sample, but stays fixed between frames

www.gameworks.nvidia.com

19

What Does This Mean?
• Consider the following, with good hash function hash( ):float hashSample = hash( myPosition );• Gives noisy hashSample like RNG per sample, but stays fixed between frames• Important caveat! Tiny camera or object motions change hash sample, ashash( myPosition ) ≠ hash( myPosition + Δ )• So these give different random values → flicker under motion

www.gameworks.nvidia.com

20

What Does “Stability” Look Like?

Frame-to-frame diff

Frame-to-frame diff

Unstable

StableDiff image under motion,
www.gameworks.nvidia.com
uncorrelated samples

21

Achieving Hash Stability• Key: Discretize hash inputs in appropriate coordinate frame•

For small Δ:
hash( floor( myPosition ) ) = hash( floor( myPosition + Δ ) )•Tweaking this, allows us to control the scale of the stable noise, e.g.:hash( floor( myPosition / scale ) * scale )www.gameworks.nvidia.com

22

What Does A Hash Scale Look Like?

1x1 pixel scale

3x3 pixel scalewww.gameworks.nvidia.com

9x9 pixel scale23

Key: Coordinate Choice For Hash Input• Need to discretize in appropriate coordinates• Same geometry should yield same hash, under:• Camera translation or rotation• Object translation or rotation• Ideally object skinning and deformation

www.gameworks.nvidia.com

24

Key: Coordinate Choice For Hash Input.’s work for deformation (and skinning) assuming hashing of pre-deformed coordinates* = being somewhat generous
www.gameworks.nvidia.com

25

Key: Coordinate Choice For Hash Input.’s work for deformation (and skinning) assuming hashing of pre-deformed coordinates* = being somewhat generous
www.gameworks.nvidia.com

26

Key: Coordinate Choice For Hash Input

We selected object coordinates; can discuss why, offline.’s work for deformation (and skinning) assuming hashing of pre-deformed coordinates* = being somewhat generous
www.gameworks.nvidia.com

27

Hashed Alpha Test Codewww.gameworks.nvidia.com

28

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noise

www.gameworks.nvidia.com

29

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noiseUse 2 scales, one smaller & one larger thandesired scale; akin to mipmapping

www.gameworks.nvidia.com

30

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noiseUse 2 scales, one smaller & one larger thandesired scale; akin to mipmapping
Compute hashed noise samples at 2 discrete scales

www.gameworks.nvidia.com

31

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noiseUse 2 scales, one smaller & one larger thandesired scale; akin to mipmapping
Compute hashed noise samples at 2 discrete scales
Linearly interpolate between noise scales

www.gameworks.nvidia.com

32

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noiseUse 2 scales, one smaller & one larger thandesired scale; akin to mipmapping
Compute hashed noise samples at 2 discrete scales
Linearly interpolate between noise scales

Interpolating between two uniform distributions
does not give a new uniform distribution
This math corrects for this non-uniformity

www.gameworks.nvidia.com

33

Hashed Alpha Test CodeCompute ideal scale to get 1-pixel sized noiseUse 2 scales, one smaller & one larger thandesired scale; akin to mipmapping
Compute hashed noise samples at 2 discrete scales
Linearly interpolate between noise scales

Interpolating between two uniform distributions
does not give a new uniform distribution
This math corrects for this non-uniformity

Clamp; alpha threshold of 0 makes no sensewww.gameworks.nvidia.com

34

Hashed Alpha Test CodeSpecific hash not important, if uniform andtakes R3 → [0…1)

We use:www.gameworks.nvidia.com

35

Hashed Alpha Test CodeUser parameter to control size of noise:
1.0 = roughly pixel sized noise
2.0 = roughly 2x2 pixel sized noise

www.gameworks.nvidia.com

36

Results
Alpha test αt = 0.5

Hashed alpha test

Ground truth OIT

0.06 ms @ 10242

0.08 ms @ 10242

1.3 ms @ 10242

www.gameworks.nvidia.comPerformances not optimized; only demonstrative of relative costs

37

Results
Adjust alpha [Castaño 2010]

Hashed alpha test

Ground truth OIT

0.06 ms @ 10242

0.08 ms @ 10242

1.3 ms @ 10242

www.gameworks.nvidia.com

Performances not optimized; only demonstrative of relative costs

38But Not Just Alpha!

www.gameworks.nvidia.com

39

Hashed Sampling Has Other Uses

www.gameworks.nvidia.com

40

Hashed Sampling Has Other Uses

Shows there’s a problem with anisotropic hashing!

www.gameworks.nvidia.com

41

Hashed Sampling Has Other UsesCan be addressed, mostly, by updating hash inputs

www.gameworks.nvidia.com

42

Anisotropic
hashed alphaHashed alpha

Alpha test

Hashing At Grazing Angles

www.gameworks.nvidia.com

43

Results
Alpha test αt = 0.5

Hashed alpha test

www.gameworks.nvidia.com

Ground truth OIT

44

Results
Alpha test αt = 0.5

Hashed alpha test

Ground truth OIT

Might ask:
Do I want noise nearby?
(Alpha test is OK here)

www.gameworks.nvidia.com

45

Results
Adjust alpha [Castaño 2010]

Hashed alpha test
(Faded in with LOD)

www.gameworks.nvidia.com

Ground truth OIT

46

ResultsPer-LOD threshold adjust(Manual, “artist” driven)

Hashed alpha test
(Faded in with LOD)

www.gameworks.nvidia.com

Ground truth OIT

47

Helps With MSAA and A2C

8x alpha-to-coverage

8x hashed alpha-tocoverage
www.gameworks.nvidia.com

Ground truth OIT
48

Temporal Antialiasing
No Temporal Antialiasing
Alpha
Hashed Alpha Ground Truth

www.gameworks.nvidia.com

With Temporal Antialiasing
Alpha
Hashed Alpha Ground Truth

49

Temporal Antialiasing• Might think, “based on stochastic sampling; of course TAA works well!”• But hashed alpha designed for stability under tiny camera motions, e.g. TAA jitterwww.gameworks.nvidia.com

50Temporal Antialiasing• Might think, “based on stochastic sampling; of course TAA works well!”• But hashed alpha designed for stability under tiny camera motions, e.g. TAA jitter• A couple approaches:• Reduce global noise scale to < 1 pixel•

TAA integrates sub pixel noise samples• Jitter offset in hash space;•Hash on objPos+offset[i] rather than objPos for relatively large, uncorrelated offset[i]• Jitter the alpha threshold•Compute αT = hash( objPos ), use thresholds fract( αT + i/N ) for i in [0…N)
www.gameworks.nvidia.com

51

Summary• Introduce new approach for alpha testing:• Alpha threshold cheaply & procedurally selected via a hash function• Gives stable noise; roughly as stable as traditional alpha test

www.gameworks.nvidia.com

52

Summary• Introduce new approach for alpha testing:• Alpha threshold cheaply & procedurally selected via a hash function• Gives stable noise; roughly as stable as traditional alpha test

• Still use one alpha test per pixel, allowing:• Use in both deferred and forward pipelines• Run on older hardware; no new (or recent) features requiredwww.gameworks.nvidia.com

53Summary• Introduce new approach for alpha testing:• Alpha threshold cheaply & procedurally selected via a hash function• Gives stable noise; roughly as stable as traditional alpha test

• Still use one alpha test per pixel, allowing:• Use in both deferred and forward pipelines• Run on older hardware; no new (or recent) features required• Requires nothing in asset pipeline• Directly uses mip-chain’s alpha channels representing pre-filtered visibility

www.gameworks.nvidia.com

54

Email: cwyman@nvidia.com Twitter: @_cwyman_
Paper: http://www.cwyman.org/papers/i3d17_hashedAlpha.pdf

Thanks!

Alpha blend

Hashed alpha test

Alpha test

For Questions:www.gameworks.nvidia.com

55