Noise Functions in OpenCL
Vanilla OpenCL does not provide any build-in noise functions but thanks to Sony Pictures Imageworks and the SideFX team, we have a modified version of osl noise library avaiable for import.
#import "mtlx_noise_internal.h"Within the file we can find variety of functions for us to use, like:
Perlin Gradient Noise Functions
float mx_perlin_noise_float_2(float2 p, int2 per)
float mx_perlin_noise_float_3(float3 p, int3 per)
float3 mx_perlin_noise_float3_2(float2 p, int2 per)
float3 mx_perlin_noise_float3_3(float3 p, int3 per)
Description: These functions generate Perlin gradient noise for a given point p. The per parameter specifies the periodicity along each axis, allowing for tileable noise. The functions return either a single float or a float3 result, depending on the input dimensions and the specific function used.
Worley Noise Functions
float mx_worley_noise_float_2(float2 p, float jitter, int metric, int2 per)
float mx_worley_noise_float_3(float3 p, float jitter, int metric, int3 per)
float2 mx_worley_noise_float2_2(float2 p, float jitter, int metric, int2 per)
float2 mx_worley_noise_float2_3(float3 p, float jitter, int metric, int3 per)
float3 mx_worley_noise_float3_2(float2 p, float jitter, int metric, int2 per)
float3 mx_worley_noise_float3_3(float3 p, float jitter, int metric, int3 per)
Description: These functions compute Worley noise for a given point p with an added jitter and a specified distance metric (Euclidean, Manhattan, or Chebyshev). The per parameter sets the periodicity for tiling. The functions return either the closest squared distance(s) or vectors depending on the input dimensions and the specific function used.
Utility Hash Functions
uint mx_hash_int1p(int x, int px)
uint mx_hash_int2p(int x, int y, int px, int py)
uint mx_hash_int3p(int x, int y, int z, int px, int py, int pz)
uint mx_hash_int4p(int x, int y, int z, int xx, int px, int py, int pz, int pxx)
Description: These functions generate a hash value for integer coordinates with optional periodic wrapping defined by px, py, pz, and pxx.
Noise Conversion Functions
float mx_cell_noise_float_1(float p, int per)
float mx_cell_noise_float_2(float2 p, int2 per)
float mx_cell_noise_float_3(float3 p, int3 per)
float mx_cell_noise_float_4(float4 p, int4 per)
float3 mx_cell_noise_float3_1(float p, int per)
float3 mx_cell_noise_float3_2(float2 p, int2 per)
float3 mx_cell_noise_float3_3(float3 p, int3 per)
float3 mx_cell_noise_float3_4(float4 p, int4 per)
Description:
These functions generate cellular noise for given input dimensions p and periodicity per. They return either a single float or a float3 value representing the noise value.
Interpolation Functions
Bilinear and Trilinear Interpolation
float mx_bilerp1(float v0, float v1, float v2, float v3, float s, float t)
float3 mx_bilerp3(float3 v0, float3 v1, float3 v2, float3 v3, float s, float t)
float mx_trilerp1(float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float s, float t, float r)
float3 mx_trilerp3(float3 v0, float3 v1, float3 v2, float3 v3, float3 v4, float3 v5, float3 v6, float3 v7, float s, float t, float r)
Description: These functions perform bilinear and trilinear interpolation on float and float3 values, respectively, using the given interpolation parameters s, t, and r.
Gradient Functions
float mx_gradient_float1_2(uint hash, float x, float y)
float mx_gradient_float1_3(uint hash, float x, float y, float z)
float3 mx_gradient_float3_2(uint3 hash, float x, float y)
float3 mx_gradient_float3_3(uint3 hash, float x, float y, float z)
Description: These functions compute gradient values based on the given hash and coordinates. They are used internally in noise functions to determine the contribution of each grid point.
Fractal Noise Functions
float mx_fractal_noise_float(float3 p, int octaves, float lacunarity, float diminish, int3 per)
float3 mx_fractal_noise_float3(float3 p, int octaves, float lacunarity, float diminish, int3 per)
float2 mx_fractal_noise_float2(float3 p, int octaves, float lacunarity, float diminish, int3 per)
float4 mx_fractal_noise_float4(float3 p, int octaves, float lacunarity, float diminish, int3 per)
Description: These functions generate fractal noise by combining multiple octaves of Perlin noise. The octaves, lacunarity, diminish, and per parameters control the frequency, amplitude, and periodicity of the noise.
Miscellaneous Utility Functions
uint mx_rotl32(uint x, int k)
void mx_bjmix(uint* a, uint* b, uint* c)
uint mx_bjfinal(uint a, uint b, uint c)
float mx_bits_to_01(uint bits)
float mx_fade(float t)
Description: These utility functions perform various tasks such as bitwise rotation, mixing and finalizing hash values, converting bits to a float in the range [0, 1], and computing the fade curve for interpolation.
Example of mx_fade() function:
#bind layer uv? float2
#bind layer !&dst float
#include "mtlx_noise_internal.h"
@KERNEL
{
float2 uv = @uv;
if(!@uv.bound){
// Convert @P from range [-1, 1] to [0, 1]
uv = (@P + 1.0f) * 0.5f;
}
// Draw a thin line between (0, 0) and (1, 1)
float line_thickness = 0.01f;
float distance_to_line = fabs(uv.x - uv.y);
// Apply mx_fade to both UV coordinates for a smoother transition
float faded_u = mx_fade(uv.x);
float faded_v = mx_fade(uv.y);
// Combine the faded values to create a result
float distance_to_faded_line = fabs(faded_u - faded_v);
float faded_line_value = distance_to_faded_line < line_thickness ? 1.0f : 0.0f;
// Set the result to the destination layer
@dst.set(faded_line_value);
}
