Skip to content

Commit 8c23b8d

Browse files
committed
HLSL translations v1
1 parent 4d627ff commit 8c23b8d

26 files changed

+3017
-0
lines changed

src/hlsl/LICENSE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2022 Domenic Portera
2+
As this contains many translations of existing works, those works' original licenses can be found in their respective subfolders.
3+
4+
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:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
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.

src/hlsl/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Source repo where this HLSL port is maintained](https://github.com/domportera/hlsl-noise)
2+
3+
Ports of [this library](https://github.com/ashima/webgl-noise) to HLSL are also included. [This fork](https://github.com/stegu/webgl-noise) (also by the author of psrdnoise) is your best source of information about these functions.
4+
5+
These have been modified for compatibility with HLSL (of course), removing redundant code and renaming things where necessary so that you may #include to your heart's content without worrying about function redefinition errors. I've even included a "noise-include-all.hlsl" for your convenience if you'd like to play around.
6+
7+
When using this, ensure you are using a compiler that supports macros. If you're not, you may need to edit the `special-math.hlsl` file to convert the `mod` macro into several explicit overloaded functions.

src/hlsl/ashima/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (C) 2011 by Ashima Arts (Simplex noise)
2+
Copyright (C) 2011-2016 by Stefan Gustavson (Classic noise and others)
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

src/hlsl/ashima/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
The wiki for this repository contains more information.
2+
3+
Simplex noise functions are (C) Ashima Arts and Stefan Gustavson
4+
Classic noise functions are (C) Stefan Gustavson
5+
Cellular noise functions are (C) Stefan Gustavson
6+
The "psrdnoise" functions are (C) Stefan Gustavson
7+
8+
Source code for the noise functions is released under the
9+
conditions of the MIT license. See the file LICENSE for details.
10+
11+
The simplex noise functions follow Ken Perlin's original idea,
12+
more clearly explained in Stefan Gustavson's paper
13+
"Simplex noise demystified"
14+
http://www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
15+
but without using any uniform arrays or texture engines.
16+
17+
Many other noise implementations make heavy use of a
18+
texture lookup table and are texture bandwidth limited.
19+
The noise functions in this library, however, are completely
20+
self contained with no dependency on external data.
21+
While not quite as fast as texture-based implementations
22+
on typical current desktop GPUs, they are more scalable to
23+
massive parallelism and much more convenient to use, and
24+
they can make good use of unused ALU resources when run
25+
concurrently with a typical texture-intensive rendering.
26+
27+
2016-05-13: Ashima Arts now seems to be defunct as a company
28+
(their website and email addresses have ceased to function)
29+
so I cloned this repository to:
30+
31+
https://github.com/stegu/webgl-noise/
32+
33+
This site is heavily linked from all over the place on the
34+
Internet, so I (Stefan Gustavson) will keep updating both sites
35+
for the foreseeable future as the (apparently) sole maintainer.

src/hlsl/ashima/cellular2D.hlsl

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Cellular noise ("Worley noise") in 2D in GLSL.
2+
// Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
3+
// This code is released under the conditions of the MIT license.
4+
// See LICENSE file for details.
5+
// https://github.com/stegu/webgl-noise
6+
7+
#include "../common/stegu-math.hlsl"
8+
9+
// Cellular noise, returning F1 and F2 in a float2.
10+
// Standard 3x3 search window for good F1 and F2 values
11+
float2 cellular(float2 P) {
12+
#define K 0.142857142857 // 1/7
13+
#define Ko 0.428571428571 // 3/7
14+
#define jitter2D 1.0 // Less gives more regular pattern
15+
float2 Pi = mod289(floor(P));
16+
float2 Pf = frac(P);
17+
float3 oi = float3(-1.0, 0.0, 1.0);
18+
float3 of = float3(-0.5, 0.5, 1.5);
19+
float3 px = permute(Pi.x + oi);
20+
float3 p = permute(px.x + Pi.y + oi); // p11, p12, p13
21+
float3 ox = frac(p*K) - Ko;
22+
float3 oy = mod7(floor(p*K))*K - Ko;
23+
float3 dx = Pf.x + 0.5 + jitter2D*ox;
24+
float3 dy = Pf.y - of + jitter2D*oy;
25+
float3 d1 = dx * dx + dy * dy; // d11, d12 and d13, squared
26+
p = permute(px.y + Pi.y + oi); // p21, p22, p23
27+
ox = frac(p*K) - Ko;
28+
oy = mod7(floor(p*K))*K - Ko;
29+
dx = Pf.x - 0.5 + jitter2D*ox;
30+
dy = Pf.y - of + jitter2D*oy;
31+
float3 d2 = dx * dx + dy * dy; // d21, d22 and d23, squared
32+
p = permute(px.z + Pi.y + oi); // p31, p32, p33
33+
ox = frac(p*K) - Ko;
34+
oy = mod7(floor(p*K))*K - Ko;
35+
dx = Pf.x - 1.5 + jitter2D*ox;
36+
dy = Pf.y - of + jitter2D*oy;
37+
float3 d3 = dx * dx + dy * dy; // d31, d32 and d33, squared
38+
// Sort out the two smallest distances (F1, F2)
39+
float3 d1a = min(d1, d2);
40+
d2 = max(d1, d2); // Swap to keep candidates for F2
41+
d2 = min(d2, d3); // neither F1 nor F2 are now in d3
42+
d1 = min(d1a, d2); // F1 is now in d1
43+
d2 = max(d1a, d2); // Swap to keep candidates for F2
44+
d1.xy = (d1.x < d1.y) ? d1.xy : d1.yx; // Swap if smaller
45+
d1.xz = (d1.x < d1.z) ? d1.xz : d1.zx; // F1 is in d1.x
46+
d1.yz = min(d1.yz, d2.yz); // F2 is now not in d2.yz
47+
d1.y = min(d1.y, d1.z); // nor in d1.z
48+
d1.y = min(d1.y, d2.x); // F2 is in d1.y, we're done.
49+
return sqrt(d1.xy);
50+
}
51+
52+
// Adapted to HLSL by Dom Portera, published under the MIT license
53+
// https://github.com/domportera/hlsl-noise

src/hlsl/ashima/cellular2x2.hlsl

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Cellular noise ("Worley noise") in 2D in GLSL.
2+
// Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
3+
// This code is released under the conditions of the MIT license.
4+
// See LICENSE file for details.
5+
// https://github.com/stegu/webgl-noise
6+
7+
#include "../common/stegu-math.hlsl"
8+
9+
// Cellular noise, returning F1 and F2 in a float2.
10+
// Speeded up by using 2x2 search window instead of 3x3,
11+
// at the expense of some strong pattern artifacts.
12+
// F2 is often wrong and has sharp discontinuities.
13+
// If you need a smooth F2, use the slower 3x3 version.
14+
// F1 is sometimes wrong, too, but OK for most purposes.
15+
float2 cellular2x2(float2 P) {
16+
#define K 0.142857142857 // 1/7
17+
#define K2_2x2 0.0714285714285 // K/2
18+
#define jitter2x2 0.8 // jitter 1.0 makes F1 wrong more often
19+
float2 Pi = mod289(floor(P));
20+
float2 Pf = frac(P);
21+
float4 Pfx = Pf.x + float4(-0.5, -1.5, -0.5, -1.5);
22+
float4 Pfy = Pf.y + float4(-0.5, -0.5, -1.5, -1.5);
23+
float4 p = permute(Pi.x + float4(0.0, 1.0, 0.0, 1.0));
24+
p = permute(p + Pi.y + float4(0.0, 0.0, 1.0, 1.0));
25+
float4 ox = mod7(p)*K+K2_2x2;
26+
float4 oy = mod7(floor(p*K))*K+K2_2x2;
27+
float4 dx = Pfx + jitter2x2*ox;
28+
float4 dy = Pfy + jitter2x2*oy;
29+
float4 d = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
30+
// Sort out the two smallest distances
31+
#if 0
32+
// Cheat and pick only F1
33+
d.xy = min(d.xy, d.zw);
34+
d.x = min(d.x, d.y);
35+
return float2(sqrt(d.x)); // F1 duplicated, F2 not computed
36+
#else
37+
// Do it right and find both F1 and F2
38+
d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap if smaller
39+
d.xz = (d.x < d.z) ? d.xz : d.zx;
40+
d.xw = (d.x < d.w) ? d.xw : d.wx;
41+
d.y = min(d.y, d.z);
42+
d.y = min(d.y, d.w);
43+
return sqrt(d.xy);
44+
#endif
45+
}
46+
47+
// Adapted to HLSL by Dom Portera, published under the MIT license
48+
// https://github.com/domportera/hlsl-noise

src/hlsl/ashima/cellular2x2x2.hlsl

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Cellular noise ("Worley noise") in 3D in GLSL.
2+
// Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
3+
// This code is released under the conditions of the MIT license.
4+
// See LICENSE file for details.
5+
// https://github.com/stegu/webgl-noise
6+
7+
#include "../common/stegu-math.hlsl"
8+
9+
// Cellular noise, returning F1 and F2 in a float2.
10+
// Speeded up by using 2x2x2 search window instead of 3x3x3,
11+
// at the expense of some pattern artifacts.
12+
// F2 is often wrong and has sharp discontinuities.
13+
// If you need a good F2, use the slower 3x3x3 version.
14+
float2 cellular2x2x2(float3 P) {
15+
#define K 0.142857142857 // 1/7
16+
#define Ko 0.428571428571 // 1/2-K/2
17+
#define K2_2x2x2 0.020408163265306 // 1/(7*7)
18+
#define Kz 0.166666666667 // 1/6
19+
#define Kzo 0.416666666667 // 1/2-1/6*2
20+
#define jitter2x2x2 0.8 // smaller jitter gives less errors in F2
21+
float3 Pi = mod289(floor(P));
22+
float3 Pf = frac(P);
23+
float4 Pfx = Pf.x + float4(0.0, -1.0, 0.0, -1.0);
24+
float4 Pfy = Pf.y + float4(0.0, 0.0, -1.0, -1.0);
25+
float4 p = permute(Pi.x + float4(0.0, 1.0, 0.0, 1.0));
26+
p = permute(p + Pi.y + float4(0.0, 0.0, 1.0, 1.0));
27+
float4 p1 = permute(p + Pi.z); // z+0
28+
float4 p2 = permute(p + Pi.z + float4(1.0, 1.0, 1.0, 1.0)); // z+1
29+
float4 ox1 = frac(p1*K) - Ko;
30+
float4 oy1 = mod7(floor(p1*K))*K - Ko;
31+
float4 oz1 = floor(p1*K2_2x2x2)*Kz - Kzo; // p1 < 289 guaranteed
32+
float4 ox2 = frac(p2*K) - Ko;
33+
float4 oy2 = mod7(floor(p2*K))*K - Ko;
34+
float4 oz2 = floor(p2*K2_2x2x2)*Kz - Kzo;
35+
float4 dx1 = Pfx + jitter2x2x2*ox1;
36+
float4 dy1 = Pfy + jitter2x2x2*oy1;
37+
float4 dz1 = Pf.z + jitter2x2x2*oz1;
38+
float4 dx2 = Pfx + jitter2x2x2*ox2;
39+
float4 dy2 = Pfy + jitter2x2x2*oy2;
40+
float4 dz2 = Pf.z - 1.0 + jitter2x2x2*oz2;
41+
float4 d1 = dx1 * dx1 + dy1 * dy1 + dz1 * dz1; // z+0
42+
float4 d2 = dx2 * dx2 + dy2 * dy2 + dz2 * dz2; // z+1
43+
44+
// Sort out the two smallest distances (F1, F2)
45+
#if 0
46+
// Cheat and sort out only F1
47+
d1 = min(d1, d2);
48+
d1.xy = min(d1.xy, d1.wz);
49+
d1.x = min(d1.x, d1.y);
50+
return float2(sqrt(d1.x));
51+
#else
52+
// Do it right and sort out both F1 and F2
53+
float4 d = min(d1,d2); // F1 is now in d
54+
d2 = max(d1,d2); // Make sure we keep all candidates for F2
55+
d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap smallest to d.x
56+
d.xz = (d.x < d.z) ? d.xz : d.zx;
57+
d.xw = (d.x < d.w) ? d.xw : d.wx; // F1 is now in d.x
58+
d.yzw = min(d.yzw, d2.yzw); // F2 now not in d2.yzw
59+
d.y = min(d.y, d.z); // nor in d.z
60+
d.y = min(d.y, d.w); // nor in d.w
61+
d.y = min(d.y, d2.x); // F2 is now in d.y
62+
return sqrt(d.xy); // F1 and F2
63+
#endif
64+
}
65+
66+
// Adapted to HLSL by Dom Portera, published under the MIT license
67+
// https://github.com/domportera/hlsl-noise

0 commit comments

Comments
 (0)