├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── examples └── benchmark │ ├── CMakeLists.txt │ └── main.cu └── include └── cuda_noise.cuh /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Windows Store app package directory 170 | AppPackages/ 171 | BundleArtifacts/ 172 | 173 | # Visual Studio cache files 174 | # files ending in .cache can be ignored 175 | *.[Cc]ache 176 | # but keep track of directories ending in .cache 177 | !*.[Cc]ache/ 178 | 179 | # Others 180 | ClientBin/ 181 | [Ss]tyle[Cc]op.* 182 | ~$* 183 | *~ 184 | *.dbmdl 185 | *.dbproj.schemaview 186 | *.pfx 187 | *.publishsettings 188 | node_modules/ 189 | orleans.codegen.cs 190 | 191 | # RIA/Silverlight projects 192 | Generated_Code/ 193 | 194 | # Backup & report files from converting an old project file 195 | # to a newer Visual Studio version. Backup files are not needed, 196 | # because we have git ;-) 197 | _UpgradeReport_Files/ 198 | Backup*/ 199 | UpgradeLog*.XML 200 | UpgradeLog*.htm 201 | 202 | # SQL Server files 203 | *.mdf 204 | *.ldf 205 | 206 | # Business Intelligence projects 207 | *.rdl.data 208 | *.bim.layout 209 | *.bim_*.settings 210 | 211 | # Microsoft Fakes 212 | FakesAssemblies/ 213 | 214 | # GhostDoc plugin setting file 215 | *.GhostDoc.xml 216 | 217 | # Node.js Tools for Visual Studio 218 | .ntvs_analysis.dat 219 | 220 | # Visual Studio 6 build log 221 | *.plg 222 | 223 | # Visual Studio 6 workspace options file 224 | *.opt 225 | 226 | # Visual Studio LightSwitch build output 227 | **/*.HTMLClient/GeneratedArtifacts 228 | **/*.DesktopClient/GeneratedArtifacts 229 | **/*.DesktopClient/ModelManifest.xml 230 | **/*.Server/GeneratedArtifacts 231 | **/*.Server/ModelManifest.xml 232 | _Pvt_Extensions 233 | 234 | # LightSwitch generated files 235 | GeneratedArtifacts/ 236 | ModelManifest.xml 237 | 238 | # Paket dependency manager 239 | .paket/paket.exe 240 | 241 | # FAKE - F# Make 242 | .fake/ 243 | 244 | # Cmake 245 | cmake-* 246 | 247 | # Jetbrains 248 | .idea/ 249 | **/.idea/* 250 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Henrik Lehtinen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cudaNoise 2 | 3 | *Library of common noise functions for CUDA kernels* 4 | 5 | Device functions for use in CUDA kernels which provide tools for generating procedural 3D noise. 6 | Basis noise functions can be combined for fractional Brownian motion, as well as used to perturb 7 | the input vector for other noise functions for turbulence effects. 8 | 9 | ## Basis functions 10 | 11 | ![montage](https://user-images.githubusercontent.com/6199226/202872480-512ab0ef-7210-4eff-8c3c-8179701e1f1e.jpg "Basis functions") 12 | 13 | *Basis functions include: discrete noise, tricubic value noise, perlin gradient noise, simplex noise, spots and worley noise.* 14 | 15 | ## Derived functions 16 | 17 | ![cudanoise](https://user-images.githubusercontent.com/6199226/202872441-bc67fadf-ae3f-44ac-afa1-b65eb60a8562.png "Repeater turbulence") 18 | 19 | *Repeater turbulence of perlin noise functions.* 20 | 21 | ## Usage 22 | 23 | cudaNoise comes as a single-header include library. Simply include cuda_noise.cuh in your CUDA source file, then call the cudaNoise functions from kernel or device functions. 24 | 25 | **NOTE: cudaNoise is designed to be used from inside CUDA kernels and cannot directly be called from host code** 26 | 27 | There is a simple texture viewer included in the /examples directory. 28 | 29 | ## Reference 30 | 31 | ### Basis functions 32 | 33 | #### 3D Checker pattern 34 | 35 | ```cpp 36 | float checker(float3 pos, float scale, int seed) 37 | ``` 38 | 39 | #### 3D Discrete noise 40 | 41 | ```cpp 42 | float discreteNoise(float3 pos, float scale, int seed) 43 | ``` 44 | 45 | #### 3D Linear value noise 46 | 47 | ```cpp 48 | float linearValue(float3 pos, float scale, int seed) 49 | ``` 50 | 51 | #### 3D Cubic value noise 52 | 53 | ```cpp 54 | float cubicValue(float3 pos, float scale, int seed) 55 | ``` 56 | 57 | #### 3D Perlin gradient noise 58 | 59 | ```cpp 60 | float perlinNoise(float3 pos, float scale, int seed) 61 | ``` 62 | 63 | #### 3D Simplex noise 64 | 65 | ```cpp 66 | float simplexNoise(float3 pos, float scale, int seed) 67 | ``` 68 | 69 | #### 3D Worley cellular noise 70 | 71 | ```cpp 72 | float worleyNoise(float3 pos, float scale, int seed, float size, int minNum, int maxNum, float jitter) 73 | ``` 74 | 75 | #### 3D Spots 76 | 77 | ```cpp 78 | float spots(float3 pos, float scale, int seed, float size, int minNum, int maxNum, float jitter, profileShape shape) 79 | ``` 80 | 81 | ## TODO 82 | 83 | - Implement 4D noise functions. 84 | - Optimization work. 85 | - Add more types of cellular functions. 86 | - Cleaning up the interface. 87 | 88 | Contributions are more than welcome! 89 | -------------------------------------------------------------------------------- /examples/benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.17) 2 | project(benchmark CUDA) 3 | 4 | set(CMAKE_CUDA_STANDARD 14) 5 | set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_61 -use_fast_math --ptxas-options=-v,-O3") 6 | message(NVCC Flags: ${CMAKE_CUDA_FLAGS}) 7 | 8 | add_executable(benchmark main.cu) 9 | 10 | set_target_properties( 11 | benchmark 12 | PROPERTIES 13 | CUDA_SEPARABLE_COMPILATION ON) -------------------------------------------------------------------------------- /examples/benchmark/main.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../../include/cuda_noise.cuh" 7 | 8 | __global__ void benchmarkPerlin(unsigned char* outputBuffer, int iterations) 9 | { 10 | int x = threadIdx.x + blockIdx.x * blockDim.x; 11 | int y = threadIdx.y + blockIdx.y * blockDim.y; 12 | long idx = x + y * blockDim.x * gridDim.x; 13 | 14 | float fx = static_cast(x) / (blockDim.x * gridDim.x) * 16.0f; 15 | float fy = static_cast(y) / (blockDim.y * gridDim.y) * 16.0f; 16 | 17 | float3 pos = make_float3(fx, fy, 0.0f); 18 | 19 | float sum = 0.0f; 20 | unsigned int seed = 0x71889283; 21 | for(int i = 0; i < iterations; i++) 22 | { 23 | seed = seed ^ ((i + 91482) * 1778932); 24 | sum += cudaNoise::repeaterPerlin(pos, 1.0f, seed, 32, 2.0f, 0.5f); 25 | } 26 | 27 | outputBuffer[idx] = static_cast((sum / static_cast(iterations)) * 63.0f + 127.0f); 28 | } 29 | 30 | __global__ void benchmarkSimplex(unsigned char* outputBuffer, int iterations) 31 | { 32 | int x = threadIdx.x + blockIdx.x * blockDim.x; 33 | int y = threadIdx.y + blockIdx.y * blockDim.y; 34 | long idx = x + y * blockDim.x * gridDim.x; 35 | 36 | float fx = static_cast(x) / (blockDim.x * gridDim.x) * 16.0f; 37 | float fy = static_cast(y) / (blockDim.y * gridDim.y) * 16.0f; 38 | 39 | float3 pos = make_float3(fx, fy, 0.0f); 40 | 41 | float sum = 0.0f; 42 | unsigned int seed = 0x71889283; 43 | for(int i = 0; i < iterations; i++) 44 | { 45 | seed = seed ^ ((i + 91482) * 1778932); 46 | sum += cudaNoise::repeaterSimplex(pos, 1.0f, seed, 32, 2.0f, 0.5f); 47 | } 48 | 49 | outputBuffer[idx] = static_cast((sum / static_cast(iterations)) * 127.0f + 127.0f); 50 | } 51 | 52 | void writeToDisk(unsigned char* buffer, const std::string& filename, size_t datasize) 53 | { 54 | std::fstream file; 55 | file.open(filename, std::ios::out | std::ios::binary); 56 | file.write(reinterpret_cast(buffer), datasize); 57 | } 58 | 59 | int main() 60 | { 61 | std::cout << "Benchmarking cuda-noise..." << std::endl; 62 | 63 | const size_t DIM = 4096; 64 | const int iterations = 32; 65 | 66 | dim3 blockSize {16, 16}; 67 | dim3 gridSize { static_cast(DIM) / blockSize.x, static_cast(DIM) / blockSize.y }; 68 | 69 | unsigned char* d_outputBuffer; 70 | unsigned char* h_outputBuffer; 71 | 72 | cudaMalloc((void**)&d_outputBuffer, DIM * DIM * sizeof(unsigned char)); 73 | cudaMallocHost((void**)&h_outputBuffer, DIM * DIM * sizeof(unsigned char)); 74 | 75 | { 76 | auto start = std::chrono::system_clock::now(); 77 | benchmarkPerlin<<>>(d_outputBuffer, iterations); 78 | cudaDeviceSynchronize(); 79 | auto end = std::chrono::system_clock::now(); 80 | auto elapsed = std::chrono::duration_cast(end - start); 81 | std::cout << "Perlin noise: " << elapsed.count() << " milliseconds" << std::endl; 82 | } 83 | 84 | cudaMemcpy(h_outputBuffer, d_outputBuffer, DIM * DIM * sizeof(unsigned char), cudaMemcpyDeviceToHost); 85 | writeToDisk(h_outputBuffer, "perlin.data", DIM * DIM * sizeof(unsigned char)); 86 | 87 | { 88 | auto start = std::chrono::system_clock::now(); 89 | benchmarkSimplex<<>>(d_outputBuffer, iterations); 90 | cudaDeviceSynchronize(); 91 | auto end = std::chrono::system_clock::now(); 92 | auto elapsed = std::chrono::duration_cast(end - start); 93 | std::cout << "Simplex noise: " << elapsed.count() << " milliseconds" << std::endl; 94 | } 95 | 96 | cudaMemcpy(h_outputBuffer, d_outputBuffer, DIM * DIM * sizeof(unsigned char), cudaMemcpyDeviceToHost); 97 | writeToDisk(h_outputBuffer, "simplex.data", DIM * DIM * sizeof(unsigned char)); 98 | 99 | cudaFree(d_outputBuffer); 100 | cudaFreeHost(h_outputBuffer); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /include/cuda_noise.cuh: -------------------------------------------------------------------------------- 1 | // cudaNoise 2 | // Library of common 3D noise functions for CUDA kernels 3 | 4 | #pragma once 5 | 6 | #include 7 | 8 | namespace cudaNoise { 9 | 10 | // Basis functions 11 | typedef enum { 12 | BASIS_CHECKER, 13 | BASIS_DISCRETE, 14 | BASIS_LINEARVALUE, 15 | BASIS_FADEDVALUE, 16 | BASIS_CUBICVALUE, 17 | BASIS_PERLIN, 18 | BASIS_SIMPLEX, 19 | BASIS_WORLEY, 20 | BASIS_SPOTS 21 | } basisFunction; 22 | 23 | // Shaping functions 24 | typedef enum { 25 | SHAPE_STEP, 26 | SHAPE_LINEAR, 27 | SHAPE_QUADRATIC 28 | } profileShape; 29 | 30 | // Function blending operators 31 | typedef enum { 32 | OPERATOR_ADD, 33 | OPERATOR_AVG, 34 | OPERATOR_MUL, 35 | OPERATOR_MAX, 36 | OPERATOR_MIN 37 | } repeatOperator; 38 | 39 | #define EPSILON 0.000000001f 40 | 41 | // Utility functions 42 | 43 | // Hashing function (used for fast on-device pseudorandom numbers for randomness in noise) 44 | __device__ unsigned int hash(unsigned int seed) 45 | { 46 | seed = (seed + 0x7ed55d16) + (seed << 12); 47 | seed = (seed ^ 0xc761c23c) ^ (seed >> 19); 48 | seed = (seed + 0x165667b1) + (seed << 5); 49 | seed = (seed + 0xd3a2646c) ^ (seed << 9); 50 | seed = (seed + 0xfd7046c5) + (seed << 3); 51 | seed = (seed ^ 0xb55a4f09) ^ (seed >> 16); 52 | 53 | return seed; 54 | } 55 | 56 | // Returns a random integer between [min, max] 57 | __device__ int randomIntRange(int min, int max, int seed) 58 | { 59 | int base = hash(seed); 60 | base = base % (1 + max - min) + min; 61 | 62 | return base; 63 | } 64 | 65 | // Returns a random float between [0, 1] 66 | __device__ float randomFloat(unsigned int seed) 67 | { 68 | unsigned int noiseVal = hash(seed); 69 | 70 | return ((float)noiseVal / (float)0xffffffff); 71 | } 72 | 73 | // Clamps val between [min, max] 74 | __device__ float clamp(float val, float min, float max) 75 | { 76 | if (val < 0.0f) 77 | return 0.0f; 78 | else if (val > 1.0f) 79 | return 1.0f; 80 | 81 | return val; 82 | } 83 | 84 | // Maps from the signed range [0, 1] to unsigned [-1, 1] 85 | // NOTE: no clamping 86 | __device__ float mapToSigned(float input) 87 | { 88 | return input * 2.0f - 1.0f; 89 | } 90 | 91 | // Maps from the unsigned range [-1, 1] to signed [0, 1] 92 | // NOTE: no clamping 93 | __device__ float mapToUnsigned(float input) 94 | { 95 | return input * 0.5f + 0.5f; 96 | } 97 | 98 | // Maps from the signed range [0, 1] to unsigned [-1, 1] with clamping 99 | __device__ float clampToSigned(float input) 100 | { 101 | return __saturatef(input) * 2.0f - 1.0f; 102 | } 103 | 104 | // Maps from the unsigned range [-1, 1] to signed [0, 1] with clamping 105 | __device__ float clampToUnsigned(float input) 106 | { 107 | return __saturatef(input * 0.5f + 0.5f); 108 | } 109 | 110 | 111 | // Random float for a grid coordinate [-1, 1] 112 | __device__ float randomGrid(int x, int y, int z, int seed = 0) 113 | { 114 | return mapToSigned(randomFloat((unsigned int)(x * 1723.0f + y * 93241.0f + z * 149812.0f + 3824.0f + seed))); 115 | } 116 | 117 | // Random unsigned int for a grid coordinate [0, MAXUINT] 118 | __device__ unsigned int randomIntGrid(float x, float y, float z, float seed = 0.0f) 119 | { 120 | return hash((unsigned int)(x * 1723.0f + y * 93241.0f + z * 149812.0f + 3824 + seed)); 121 | } 122 | 123 | // Random 3D vector as float3 from grid position 124 | __device__ float3 vectorNoise(int x, int y, int z) 125 | { 126 | return make_float3(randomFloat(x * 8231.0f + y * 34612.0f + z * 11836.0f + 19283.0f) * 2.0f - 1.0f, 127 | randomFloat(x * 1171.0f + y * 9234.0f + z * 992903.0f + 1466.0f) * 2.0f - 1.0f, 128 | 0.0f); 129 | } 130 | 131 | // Scale 3D vector by scalar value 132 | __device__ float3 scaleVector(float3 v, float factor) 133 | { 134 | return make_float3(v.x * factor, v.y * factor, v.z * factor); 135 | } 136 | 137 | // Scale 3D vector by nonuniform parameters 138 | __device__ float3 nonuniformScaleVector(float3 v, float xf, float yf, float zf) 139 | { 140 | return make_float3(v.x * xf, v.y * yf, v.z * zf); 141 | } 142 | 143 | 144 | // Adds two 3D vectors 145 | __device__ float3 addVectors(float3 v, float3 w) 146 | { 147 | return make_float3(v.x + w.x, v.y + w.y, v.z + w.z); 148 | } 149 | 150 | // Dot product between two vectors 151 | __device__ float dotProduct(float3 u, float3 v) 152 | { 153 | return (u.x * v.x + u.y * v.y + u.z * v.z); 154 | } 155 | 156 | // Device constants for noise 157 | 158 | __device__ __constant__ float gradMap[16][3] = { { 1.0f, 1.0f, 0.0f },{ -1.0f, 1.0f, 0.0f },{ 1.0f, -1.0f, 0.0f },{ -1.0f, -1.0f, 0.0f }, 159 | { 1.0f, 0.0f, 1.0f },{ -1.0f, 0.0f, 1.0f },{ 1.0f, 0.0f, -1.0f },{ -1.0f, 0.0f, -1.0f }, 160 | { 0.0f, 1.0f, 1.0f },{ 0.0f, -1.0f, 1.0f },{ 0.0f, 1.0f, -1.0f },{ 0.0f, -1.0f, -1.0f }}; 161 | 162 | // Helper functions for noise 163 | 164 | // Linearly interpolate between two float values 165 | __device__ float lerp(float a, float b, float ratio) 166 | { 167 | return a * (1.0f - ratio) + b * ratio; 168 | } 169 | 170 | // 1D cubic interpolation with four points 171 | __device__ float cubic(float p0, float p1, float p2, float p3, float x) 172 | { 173 | return p1 + 0.5f * x * (p2 - p0 + x * (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3 + x * (3.0f * (p1 - p2) + p3 - p0))); 174 | } 175 | 176 | // Fast gradient function for gradient noise 177 | __device__ float grad(int hash, float x, float y, float z) 178 | { 179 | switch (hash & 0xF) 180 | { 181 | case 0x0: return x + y; 182 | case 0x1: return -x + y; 183 | case 0x2: return x - y; 184 | case 0x3: return -x - y; 185 | case 0x4: return x + z; 186 | case 0x5: return -x + z; 187 | case 0x6: return x - z; 188 | case 0x7: return -x - z; 189 | case 0x8: return y + z; 190 | case 0x9: return -y + z; 191 | case 0xA: return y - z; 192 | case 0xB: return -y - z; 193 | case 0xC: return y + x; 194 | case 0xD: return -y + z; 195 | case 0xE: return y - x; 196 | case 0xF: return -y - z; 197 | default: return 0; // never happens 198 | } 199 | } 200 | 201 | // Ken Perlin's fade function for Perlin noise 202 | __device__ float fade(float t) 203 | { 204 | return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); // 6t^5 - 15t^4 + 10t^3 205 | } 206 | 207 | // Dot product using a float[3] and float parameters 208 | // NOTE: could be cleaned up 209 | __device__ float dot(float g[3], float x, float y, float z) { 210 | return g[0] * x + g[1] * y + g[2] * z; 211 | } 212 | 213 | // Random value for simplex noise [0, 255] 214 | __device__ unsigned char calcPerm(int p) 215 | { 216 | return (unsigned char)(hash(p)); 217 | } 218 | 219 | // Random value for simplex noise [0, 11] 220 | __device__ unsigned char calcPerm12(int p) 221 | { 222 | return (unsigned char)(hash(p) % 12); 223 | } 224 | 225 | // Noise functions 226 | 227 | // Simplex noise adapted from Java code by Stefan Gustafson and Peter Eastman 228 | __device__ float simplexNoise(float3 pos, float scale, int seed) 229 | { 230 | float xin = pos.x * scale; 231 | float yin = pos.y * scale; 232 | float zin = pos.z * scale; 233 | 234 | // Skewing and unskewing factors for 3 dimensions 235 | float F3 = 1.0f / 3.0f; 236 | float G3 = 1.0f / 6.0f; 237 | 238 | float n0, n1, n2, n3; // Noise contributions from the four corners 239 | 240 | // Skew the input space to determine which simplex cell we're in 241 | float s = (xin + yin + zin)*F3; // Very nice and simple skew factor for 3D 242 | int i = floorf(xin + s); 243 | int j = floorf(yin + s); 244 | int k = floorf(zin + s); 245 | float t = (i + j + k)*G3; 246 | float X0 = i - t; // Unskew the cell origin back to (x,y,z) space 247 | float Y0 = j - t; 248 | float Z0 = k - t; 249 | float x0 = xin - X0; // The x,y,z distances from the cell origin 250 | float y0 = yin - Y0; 251 | float z0 = zin - Z0; 252 | 253 | // For the 3D case, the simplex shape is a slightly irregular tetrahedron. 254 | // Determine which simplex we are in. 255 | int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords 256 | int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords 257 | if (x0 >= y0) { 258 | if (y0 >= z0) 259 | { 260 | i1 = 1.0f; j1 = 0.0f; k1 = 0.0f; i2 = 1.0f; j2 = 1.0f; k2 = 0.0f; 261 | } // X Y Z order 262 | else if (x0 >= z0) { i1 = 1.0f; j1 = 0.0f; k1 = 0.0f; i2 = 1.0f; j2 = 0.0f; k2 = 1.0f; } // X Z Y order 263 | else { i1 = 0.0f; j1 = 0.0f; k1 = 1.0f; i2 = 1.0f; j2 = 0.0f; k2 = 1.0f; } // Z X Y order 264 | } 265 | else { // x0 rdu) 794 | break; 795 | } 796 | 797 | return acc; 798 | } 799 | 800 | // Generic turbulence function 801 | // Uses a first pass of noise to offset the input vectors for the second pass 802 | __device__ float turbulence(float3 pos, float scaleIn, float scaleOut, int seed, float strength, basisFunction inFunc, basisFunction outFunc) 803 | { 804 | switch (inFunc) 805 | { 806 | case(BASIS_CHECKER): 807 | pos.x += checker(pos, scaleIn, seed ^ 0x34ff8885) * strength; 808 | pos.y += checker(pos, scaleIn, seed ^ 0x2d03cba3) * strength; 809 | pos.z += checker(pos, scaleIn, seed ^ 0x5a76fb1b) * strength; 810 | break; 811 | case(BASIS_LINEARVALUE): 812 | pos.x += linearValue(pos, scaleIn, seed ^ 0x5527fdb8) * strength; 813 | pos.y += linearValue(pos, scaleIn, seed ^ 0x42af1a2e) * strength; 814 | pos.z += linearValue(pos, scaleIn, seed ^ 0x1482ee8c) * strength; 815 | break; 816 | case(BASIS_FADEDVALUE): 817 | pos.x += fadedValue(pos, scaleIn, seed ^ 0x295590fc) * strength; 818 | pos.y += fadedValue(pos, scaleIn, seed ^ 0x30731854) * strength; 819 | pos.z += fadedValue(pos, scaleIn, seed ^ 0x73d2ca4c) * strength; 820 | break; 821 | case(BASIS_CUBICVALUE): 822 | pos.x += cubicValue(pos, scaleIn, seed ^ 0x663a1f09) * strength; 823 | pos.y += cubicValue(pos, scaleIn, seed ^ 0x429bf56b) * strength; 824 | pos.z += cubicValue(pos, scaleIn, seed ^ 0x37fa6fe9) * strength; 825 | break; 826 | case(BASIS_PERLIN): 827 | pos.x += perlinNoise(pos, scaleIn, seed ^ 0x74827384) * strength; 828 | pos.y += perlinNoise(pos, scaleIn, seed ^ 0x10938478) * strength; 829 | pos.z += perlinNoise(pos, scaleIn, seed ^ 0x62723883) * strength; 830 | break; 831 | case(BASIS_SIMPLEX): 832 | pos.x += simplexNoise(pos, scaleIn, seed ^ 0x47829472) * strength; 833 | pos.y += simplexNoise(pos, scaleIn, seed ^ 0x58273829) * strength; 834 | pos.z += simplexNoise(pos, scaleIn, seed ^ 0x10294647) * strength; 835 | break; 836 | case(BASIS_WORLEY): 837 | pos.x += worleyNoise(pos, scaleIn, seed ^ 0x1d96f515, 1.0f, 4, 4, 1.0f) * strength; 838 | pos.y += worleyNoise(pos, scaleIn, seed ^ 0x4df308f0, 1.0f, 4, 4, 1.0f) * strength; 839 | pos.z += worleyNoise(pos, scaleIn, seed ^ 0x2b79442a, 1.0f, 4, 4, 1.0f) * strength; 840 | break; 841 | } 842 | 843 | switch (outFunc) 844 | { 845 | case(BASIS_CHECKER): 846 | return checker(pos, scaleOut, seed); 847 | case(BASIS_LINEARVALUE): 848 | return linearValue(pos, scaleOut, seed); 849 | case(BASIS_FADEDVALUE): 850 | return fadedValue(pos, scaleOut, seed); 851 | case(BASIS_CUBICVALUE): 852 | return cubicValue(pos, scaleOut, seed); 853 | case(BASIS_PERLIN): 854 | return perlinNoise(pos, scaleOut, seed); 855 | case(BASIS_SIMPLEX): 856 | return simplexNoise(pos, scaleIn, seed); 857 | case(BASIS_WORLEY): 858 | return worleyNoise(pos, scaleIn, seed, 1.0f, 4, 4, 1.0f); 859 | } 860 | 861 | return 0.0f; 862 | } 863 | 864 | // Turbulence using repeaters for the first and second pass 865 | __device__ float repeaterTurbulence(float3 pos, float scaleIn, float scaleOut, int seed, float strength, int n, basisFunction basisIn, basisFunction basisOut) 866 | { 867 | pos.x += (repeater(make_float3(pos.x, pos.y, pos.z), scaleIn, seed ^ 0x41728394, n, 2.0f, 0.5f, basisIn)) * strength; 868 | pos.y += (repeater(make_float3(pos.x, pos.y, pos.z), scaleIn, seed ^ 0x72837263, n, 2.0f, 0.5f, basisIn)) * strength; 869 | pos.z += (repeater(make_float3(pos.x, pos.y, pos.z), scaleIn, seed ^ 0x26837363, n, 2.0f, 0.5f, basisIn)) * strength; 870 | 871 | return repeater(pos, scaleOut, seed ^ 0x3f821dab, n, 2.0f, 0.5f, basisOut); 872 | } 873 | 874 | } // namespace 875 | --------------------------------------------------------------------------------