Created
May 29, 2026 15:16
-
-
Save runevision/6dfd6085d0146150202a201cce942fc8 to your computer and use it in GitHub Desktop.
GPU fixed point noise (HLSL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Fixed point 2D and 3D noise functions: | |
| float Noise(in int2 p, float pRes, float freq) | |
| float Noise(in int3 p, float pRes, float freq) | |
| Fixed point 2D noise function with derivatives: | |
| float3 NoiseD(in int2 p, float pRes, float freq) | |
| - p is the input coordinates in raw integers. | |
| - pRes is the "resolution" of one unit; the integer value | |
| that corresponds to one unit in world space. | |
| - freq is the frequency of the noise in units. | |
| Fixed point rewrites copyright 2026 Rune Skovbo Johansen and original authors, MIT license. | |
| */ | |
| /* | |
| pcg2d, pcg3d, pcg4d functions copyright 2020 Mark Jarzynski & Marc Olano | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| uint2 pcg2d(uint2 v) { | |
| v = v * 1664525u + 1013904223u; | |
| v.x += v.y * 1664525u; | |
| v.y += v.x * 1664525u; | |
| v ^= v>>16; | |
| v.x += v.y * 1664525u; | |
| v.y += v.x * 1664525u; | |
| v ^= v>>16; | |
| return v; | |
| } | |
| uint3 pcg3d(uint3 v) { | |
| v = v * 1664525u + 1013904223u; | |
| v.x += v.y * v.z; | |
| v.y += v.z * v.x; | |
| v.z += v.x * v.y; | |
| v ^= v>>16; | |
| v.x += v.y * v.z; | |
| v.y += v.z * v.x; | |
| v.z += v.x * v.y; | |
| return v; | |
| } | |
| uint4 pcg4d(uint4 v) { | |
| v = v * 1664525u + 1013904223u; | |
| v.x += v.y * v.w; | |
| v.y += v.z * v.x; | |
| v.z += v.x * v.y; | |
| v.w += v.y * v.z; | |
| v ^= v>>16; | |
| v.x += v.y * v.w; | |
| v.y += v.z * v.x; | |
| v.z += v.x * v.y; | |
| v.w += v.y * v.z; | |
| return v; | |
| } | |
| // Convert random uint number to random float in -1 to 1 range. | |
| float ToSNORM(uint u) { | |
| const float remap = 2.0f / float(0xffffffffu); | |
| return float(u) * remap - 1.0f; | |
| } | |
| float2 ToSNORM(uint2 u) { | |
| const float remap = 2.0f / float(0xffffffffu); | |
| return float2(u) * remap - 1.0f; | |
| } | |
| float3 ToSNORM(uint3 u) { | |
| const float remap = 2.0f / float(0xffffffffu); | |
| return float3(u) * remap - 1.0f; | |
| } | |
| float4 ToSNORM(uint4 u) { | |
| const float remap = 2.0f / float(0xffffffffu); | |
| return float4(u) * remap - 1.0f; | |
| } | |
| float2 HashSNORM(in int2 i) { | |
| return ToSNORM(pcg2d(uint2(i))); | |
| } | |
| float3 HashSNORM(in int3 i) { | |
| return ToSNORM(pcg3d(uint3(i))); | |
| } | |
| float4 HashSNORM(in int4 i) { | |
| return ToSNORM(pcg4d(uint4(i))); | |
| } | |
| // Performs integer floor division (rounding down rather than towards zero). | |
| int Div(int x, int divisor) { | |
| return (x - ((divisor - 1) & (x >> 31))) / divisor; | |
| } | |
| int2 Div(int2 x, int divisor) { | |
| return (x - ((divisor - 1) & (x >> 31))) / divisor; | |
| } | |
| int3 Div(int3 x, int divisor) { | |
| return (x - ((divisor - 1) & (x >> 31))) / divisor; | |
| } | |
| int4 Div(int4 x, int divisor) { | |
| return (x - ((divisor - 1) & (x >> 31))) / divisor; | |
| } | |
| void GetFloorAndFrac(in int2 p, float freq, out int2 floor, out float2 frac) { | |
| int customRes = (int)ceil(0.999999f / freq); | |
| floor = Div(p, customRes); | |
| frac = float2(p - floor * customRes) / customRes; | |
| } | |
| void GetFloorAndFrac(in int3 p, float freq, out int3 floor, out float3 frac) { | |
| int customRes = (int)ceil(0.999999f / freq); | |
| floor = Div(p, customRes); | |
| frac = float3(p - floor * customRes) / customRes; | |
| } | |
| // Derived from: https://www.shadertoy.com/view/XdXBRH by iq, license: MIT | |
| // Returns gradient noise (in x) and its derivatives (in yz). | |
| // Derived version without floating point coordinates by runevision, license MIT | |
| float3 NoiseD(in int2 p, float pRes, float freq) { | |
| int2 i; | |
| float2 f; | |
| GetFloorAndFrac(p, freq / pRes, i, f); | |
| float2 u = f * f * f * (f * (f * 6.0f - 15.0f) + 10.0f); | |
| float2 du = 30.0f * f * f * (f * (f - 2.0f) + 1.0f); | |
| float2 ga = HashSNORM(i + int2(0, 0)); | |
| float2 gb = HashSNORM(i + int2(1, 0)); | |
| float2 gc = HashSNORM(i + int2(0, 1)); | |
| float2 gd = HashSNORM(i + int2(1, 1)); | |
| float va = dot(ga, f - float2(0.0f, 0.0f)); | |
| float vb = dot(gb, f - float2(1.0f, 0.0f)); | |
| float vc = dot(gc, f - float2(0.0f, 1.0f)); | |
| float vd = dot(gd, f - float2(1.0f, 1.0f)); | |
| return float3( | |
| // Value: | |
| va + u.x * (vb - va) + u.y * (vc - va) + u.x * u.y * (va - vb - vc + vd), | |
| // Gradient: | |
| ( | |
| ga | |
| + u.x * (gb - ga) | |
| + u.y * (gc - ga) | |
| + u.x * u.y * (ga - gb - gc + gd) | |
| + du * (u.yx * (va - vb - vc + vd) | |
| + float2(vb, vc) - va) | |
| ) * freq | |
| ); | |
| } | |
| // Derived from: https://www.shadertoy.com/view/XdXBRH by iq, license: MIT | |
| // Returns 3D gradient noise. | |
| // Derived version without floating point coordinates by runevision, license MIT | |
| float Noise(in int2 p, float pRes, float freq) { | |
| int2 i; | |
| float2 f; | |
| GetFloorAndFrac(p, freq / pRes, i, f); | |
| float2 u = f * f * f * (f * (f * 6.0f - 15.0f) + 10.0f); | |
| float2 ga = HashSNORM(i + int2(0, 0)); | |
| float2 gb = HashSNORM(i + int2(1, 0)); | |
| float2 gc = HashSNORM(i + int2(0, 1)); | |
| float2 gd = HashSNORM(i + int2(1, 1)); | |
| float va = dot(ga, f - float2(0.0f, 0.0f)); | |
| float vb = dot(gb, f - float2(1.0f, 0.0f)); | |
| float vc = dot(gc, f - float2(0.0f, 1.0f)); | |
| float vd = dot(gd, f - float2(1.0f, 1.0f)); | |
| return va + u.x * (vb - va) + u.y * (vc - va) + u.x * u.y * (va - vb - vc + vd); | |
| } | |
| // Derived from: https://www.shadertoy.com/view/XdXBRH by iq, license: MIT | |
| // Returns 3D gradient noise. | |
| // Derived version without floating point coordinates by runevision, license MIT | |
| float Noise(in int3 p, float pRes, float freq) { | |
| int3 i; | |
| float3 f; | |
| GetFloorAndFrac(p, freq / pRes, i, f); | |
| float3 u = f * f * f * (f * (f * 6.0f - 15.0f) + 10.0f); | |
| float3 ga = HashSNORM(i + int3(0, 0, 0)); | |
| float3 gb = HashSNORM(i + int3(1, 0, 0)); | |
| float3 gc = HashSNORM(i + int3(0, 1, 0)); | |
| float3 gd = HashSNORM(i + int3(1, 1, 0)); | |
| float3 ge = HashSNORM(i + int3(0, 0, 1)); | |
| float3 gf = HashSNORM(i + int3(1, 0, 1)); | |
| float3 gg = HashSNORM(i + int3(0, 1, 1)); | |
| float3 gh = HashSNORM(i + int3(1, 1, 1)); | |
| float va = dot(ga, f - float3(0.0f, 0.0f, 0.0f)); | |
| float vb = dot(gb, f - float3(1.0f, 0.0f, 0.0f)); | |
| float vc = dot(gc, f - float3(0.0f, 1.0f, 0.0f)); | |
| float vd = dot(gd, f - float3(1.0f, 1.0f, 0.0f)); | |
| float ve = dot(ge, f - float3(0.0f, 0.0f, 1.0f)); | |
| float vf = dot(gf, f - float3(1.0f, 0.0f, 1.0f)); | |
| float vg = dot(gg, f - float3(0.0f, 1.0f, 1.0f)); | |
| float vh = dot(gh, f - float3(1.0f, 1.0f, 1.0f)); | |
| return lerp( | |
| lerp(lerp(va, vb, u.x), lerp(vc, vd, u.x), u.y), | |
| lerp(lerp(ve, vf, u.x), lerp(vg, vh, u.x), u.y), u.z); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've written some fixed point noise functions for GPU HLSL (and C# with Unity.Mathematics) in order to be able to generate terrain far from the origin.
In my game, I divide each world unit into 256 and thus have a usable range of +/- 8000000 world units, but the functions support arbitrary unit resolutions.
So I thought I'd post the code here, in case it might be of use to anyone else.
Note, floating point numbers end with 'f' due to the code being generated from shared C# and HLSL code.