Skip to content

danilw/GPU-sin-hash-stability

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 

Repository files navigation

DO NOT USE SIN HASH


Read Hash Noise in GPU Shaders - there lots of screenshots

Notice on screenshots below - use sin(large_numbers) by itself is broken - not just in hash.


Short:

Sin-hash is not stable on "any scale" and there no way to fix it.

  • Nvidia OpenGL sin-hash
  • will be different on Nvidia DX11/12 sin hash
  • will be different(and broken) on Vulkan
  • will be different(and broken) on AMD/Mac/smartphone/anything else
  • When fract-hash will be stable and exact same everywhere (except rare cases but can be fixed - read below FIX_FRACT_HASH)

img


  • Use fract hash - https://www.shadertoy.com/view/4djSRW
  • Or int hash - but int hash have exact same behavior as fract hash - because source of random is float.
  • But - int and fract hash can still be "broken"

FIX_FRACT_HASH

Fix (not always needed) to inconsistent fract hash: (look screenshots below)

  • use it only when you actually see fract hash is broken for your case - in most cases like noise-use case fract hash is stable
  • this "fix" hit performance of fract-hash and making performance equal to int-hash (without this fix)
  • int hash with this fix will be slower than fract-hash with fix
  • and when fract-hash is broken int hash will also be broken so require same "fix" - image
  • read blog - Hash Noise stability in GPU Shaders
// not always needed, and not always fix
#define FIX_FRACT_HASH 1000.

float hash12(vec2 p)
{
#ifdef FIX_FRACT_HASH
    p = sign(p)*(floor(abs(p))+floor(fract(abs(p))*FIX_FRACT_HASH)/FIX_FRACT_HASH);
#endif
    vec3 p3  = fract(vec3(p.xyx) * .1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

Screenshot, click to open Bug link
sin hash instability read blog - Hash Noise stability in GPU Shaders
BUG Vulkan Nvidia sin hash - sin hash broken in Vulkan on Nvidia, when in OpenGL everything fine.
BUG sin not same on AMD/Nvidia - similar as "sin hash broken" above, but in not-noise shader, in fractal shader sin making different visual image.
BUG mod2pi does not fix sin/cos AMD/Nvidia - same shader as above, uncomment first two lines with define, obvious idea to fix sin inconsistensy just mod(x,2*PI) but it not just "not fix" it change pattern on Nvidia where it was working, on Nvidia pattern on left side with define will be different compare to no define.
--- ---
fract hash instability read blog - Hash Noise stability in GPU Shaders
BUG TEST Rough Seas fract hash - Just test shader for this blog post Hash Noise stability in GPU Shaders, blog mirror.
Related-example of fract-hash instability described in blog post Hash Noise stability in GPU Shaders, blog mirror. Shader link.
One more real-life example of fract-hash instability Shader link.
--- ---

more bugs in

GPU-my-list-of-bugs

About

LOOK SCREENSHOTS - Sin-hash is not stable on "any scale" and there no way to fix it. Use fract hash.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published