From 763bfa8bc576da4cf0016bbe34eda91a7713a9ec Mon Sep 17 00:00:00 2001 From: Hoshinova Date: Mon, 25 Mar 2024 16:58:53 +0100 Subject: [PATCH 1/3] -- Fix 119797 --- intern/cycles/kernel/osl/shaders/node_noise.h | 106 ++++++++++-------- intern/cycles/kernel/svm/noise.h | 29 ++++- source/blender/blenlib/intern/noise.cc | 24 ++++ .../material/gpu_shader_material_noise.glsl | 37 ++++-- 4 files changed, 136 insertions(+), 60 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_noise.h b/intern/cycles/kernel/osl/shaders/node_noise.h index 0f8912d7bf1..35d998c7d0f 100644 --- a/intern/cycles/kernel/osl/shaders/node_noise.h +++ b/intern/cycles/kernel/osl/shaders/node_noise.h @@ -7,76 +7,86 @@ #define vector3 point -float safe_noise(float p) +float safe_noise(float co) { - float f = noise("noise", p); - if (isinf(f)) { - return 0.5; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + float p = fmod(co, 100000.0); + + return noise("noise", p); } -float safe_noise(vector2 p) +float safe_noise(vector2 co) { - float f = noise("noise", p.x, p.y); - if (isinf(f)) { - return 0.5; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); + + return noise("noise", p.x, p.y); } -float safe_noise(vector3 p) +float safe_noise(vector3 co) { - float f = noise("noise", p); - if (isinf(f)) { - return 0.5; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); + + return noise("noise", p); } -float safe_noise(vector4 p) +float safe_noise(vector4 co) { - float f = noise("noise", vector3(p.x, p.y, p.z), p.w); - if (isinf(f)) { - return 0.5; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector4 p = vector4( + fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); + + return noise("noise", vector3(p.x, p.y, p.z), p.w); } -float safe_snoise(float p) +float safe_snoise(float co) { - float f = noise("snoise", p); - if (isinf(f)) { - return 0.0; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + float p = fmod(co, 100000.0); + + return noise("snoise", p); } -float safe_snoise(vector2 p) +float safe_snoise(vector2 co) { - float f = noise("snoise", p.x, p.y); - if (isinf(f)) { - return 0.0; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); + + return noise("snoise", p.x, p.y); } -float safe_snoise(vector3 p) +float safe_snoise(vector3 co) { - float f = noise("snoise", p); - if (isinf(f)) { - return 0.0; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); + + return noise("snoise", p); } -float safe_snoise(vector4 p) +float safe_snoise(vector4 co) { - float f = noise("snoise", vector3(p.x, p.y, p.z), p.w); - if (isinf(f)) { - return 0.0; - } - return f; + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + vector4 p = vector4( + fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); + + return noise("snoise", vector3(p.x, p.y, p.z), p.w); } #define NOISE_FBM(T) \ diff --git a/intern/cycles/kernel/svm/noise.h b/intern/cycles/kernel/svm/noise.h index 4a2d7a474df..6d9e15ddd4c 100644 --- a/intern/cycles/kernel/svm/noise.h +++ b/intern/cycles/kernel/svm/noise.h @@ -684,7 +684,12 @@ ccl_device_inline float noise_scale4(float result) ccl_device_inline float snoise_1d(float p) { - return noise_scale1(ensure_finite(perlin_1d(p))); + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + p = fmodf(p, 100000.0f); + + return noise_scale1(perlin_1d(p)); } ccl_device_inline float noise_1d(float p) @@ -694,7 +699,12 @@ ccl_device_inline float noise_1d(float p) ccl_device_inline float snoise_2d(float2 p) { - return noise_scale2(ensure_finite(perlin_2d(p.x, p.y))); + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + p = make_float2(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f)); + + return noise_scale2(perlin_2d(p.x, p.y)); } ccl_device_inline float noise_2d(float2 p) @@ -704,7 +714,12 @@ ccl_device_inline float noise_2d(float2 p) ccl_device_inline float snoise_3d(float3 p) { - return noise_scale3(ensure_finite(perlin_3d(p.x, p.y, p.z))); + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + p = make_float3(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f)); + + return noise_scale3(perlin_3d(p.x, p.y, p.z)); } ccl_device_inline float noise_3d(float3 p) @@ -714,7 +729,13 @@ ccl_device_inline float noise_3d(float3 p) ccl_device_inline float snoise_4d(float4 p) { - return noise_scale4(ensure_finite(perlin_4d(p.x, p.y, p.z, p.w))); + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + p = make_float4( + fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f), fmodf(p.w, 100000.0f)); + + return noise_scale4(perlin_4d(p.x, p.y, p.z, p.w)); } ccl_device_inline float noise_4d(float4 p) diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc index 1c03f7d7cc8..86a4cbcd768 100644 --- a/source/blender/blenlib/intern/noise.cc +++ b/source/blender/blenlib/intern/noise.cc @@ -490,21 +490,45 @@ BLI_INLINE float perlin_noise(float4 position) float perlin_signed(float position) { + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + position = fmodf(position, 100000.0f); + return perlin_noise(position) * 0.2500f; } float perlin_signed(float2 position) { + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + position = float2(fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f)); + return perlin_noise(position) * 0.6616f; } float perlin_signed(float3 position) { + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + position = float3( + fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f), fmodf(position.z, 100000.0f)); + return perlin_noise(position) * 0.9820f; } float perlin_signed(float4 position) { + /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0f, however at such scales + * this usually shouldn't be noticible. */ + position = float4(fmodf(position.x, 100000.0f), + fmodf(position.y, 100000.0f), + fmodf(position.z, 100000.0f), + fmodf(position.w, 100000.0f)); + return perlin_noise(position) * 0.8344f; } diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl index 7facf282913..9928b9e2104 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl @@ -260,8 +260,12 @@ float noise_scale4(float result) float snoise(float p) { - float r = noise_perlin(p); - return (isinf(r)) ? 0.0 : noise_scale1(r); + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + p = compatible_fmod(p, 100000.0); + + return noise_scale1(noise_perlin(p)); } float noise(float p) @@ -271,8 +275,12 @@ float noise(float p) float snoise(vec2 p) { - float r = noise_perlin(p); - return (isinf(r)) ? 0.0 : noise_scale2(r); + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + p = vec2(compatible_fmod(p.x, 100000.0), compatible_fmod(p.y, 100000.0)); + + return noise_scale2(noise_perlin(p)); } float noise(vec2 p) @@ -282,8 +290,14 @@ float noise(vec2 p) float snoise(vec3 p) { - float r = noise_perlin(p); - return (isinf(r)) ? 0.0 : noise_scale3(r); + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + p = vec3(compatible_fmod(p.x, 100000.0), + compatible_fmod(p.y, 100000.0), + compatible_fmod(p.z, 100000.0)); + + return noise_scale3(noise_perlin(p)); } float noise(vec3 p) @@ -293,8 +307,15 @@ float noise(vec3 p) float snoise(vec4 p) { - float r = noise_perlin(p); - return (isinf(r)) ? 0.0 : noise_scale4(r); + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. This causes discontinuities every 100000.0, however at such scales this + * usually shouldn't be noticible. */ + p = vec4(compatible_fmod(p.x, 100000.0), + compatible_fmod(p.y, 100000.0), + compatible_fmod(p.z, 100000.0), + compatible_fmod(p.w, 100000.0)); + + return noise_scale4(noise_perlin(p)); } float noise(vec4 p) -- 2.30.2 From 8cc71152818726ea690a7d9792d72ecd3e1a2b88 Mon Sep 17 00:00:00 2001 From: Hoshinova Date: Mon, 25 Mar 2024 17:21:23 +0100 Subject: [PATCH 2/3] -- Fix typo. --- intern/cycles/kernel/osl/shaders/node_noise.h | 16 ++++++++-------- intern/cycles/kernel/svm/noise.h | 8 ++++---- source/blender/blenlib/intern/noise.cc | 8 ++++---- .../material/gpu_shader_material_noise.glsl | 8 ++++---- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_noise.h b/intern/cycles/kernel/osl/shaders/node_noise.h index 35d998c7d0f..3ad24eb9918 100644 --- a/intern/cycles/kernel/osl/shaders/node_noise.h +++ b/intern/cycles/kernel/osl/shaders/node_noise.h @@ -11,7 +11,7 @@ float safe_noise(float co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ float p = fmod(co, 100000.0); return noise("noise", p); @@ -21,7 +21,7 @@ float safe_noise(vector2 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); return noise("noise", p.x, p.y); @@ -31,7 +31,7 @@ float safe_noise(vector3 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); return noise("noise", p); @@ -41,7 +41,7 @@ float safe_noise(vector4 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector4 p = vector4( fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); @@ -52,7 +52,7 @@ float safe_snoise(float co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ float p = fmod(co, 100000.0); return noise("snoise", p); @@ -62,7 +62,7 @@ float safe_snoise(vector2 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); return noise("snoise", p.x, p.y); @@ -72,7 +72,7 @@ float safe_snoise(vector3 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); return noise("snoise", p); @@ -82,7 +82,7 @@ float safe_snoise(vector4 co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ vector4 p = vector4( fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); diff --git a/intern/cycles/kernel/svm/noise.h b/intern/cycles/kernel/svm/noise.h index 6d9e15ddd4c..908a812483f 100644 --- a/intern/cycles/kernel/svm/noise.h +++ b/intern/cycles/kernel/svm/noise.h @@ -686,7 +686,7 @@ ccl_device_inline float snoise_1d(float p) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ p = fmodf(p, 100000.0f); return noise_scale1(perlin_1d(p)); @@ -701,7 +701,7 @@ ccl_device_inline float snoise_2d(float2 p) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ p = make_float2(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f)); return noise_scale2(perlin_2d(p.x, p.y)); @@ -716,7 +716,7 @@ ccl_device_inline float snoise_3d(float3 p) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ p = make_float3(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f)); return noise_scale3(perlin_3d(p.x, p.y, p.z)); @@ -731,7 +731,7 @@ ccl_device_inline float snoise_4d(float4 p) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ p = make_float4( fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f), fmodf(p.w, 100000.0f)); diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc index 86a4cbcd768..389ac33e3e5 100644 --- a/source/blender/blenlib/intern/noise.cc +++ b/source/blender/blenlib/intern/noise.cc @@ -492,7 +492,7 @@ float perlin_signed(float position) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ position = fmodf(position, 100000.0f); return perlin_noise(position) * 0.2500f; @@ -502,7 +502,7 @@ float perlin_signed(float2 position) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ position = float2(fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f)); return perlin_noise(position) * 0.6616f; @@ -512,7 +512,7 @@ float perlin_signed(float3 position) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ position = float3( fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f), fmodf(position.z, 100000.0f)); @@ -523,7 +523,7 @@ float perlin_signed(float4 position) { /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticible. */ + * this usually shouldn't be noticeable. */ position = float4(fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f), fmodf(position.z, 100000.0f), diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl index 9928b9e2104..8d897738469 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl @@ -262,7 +262,7 @@ float snoise(float p) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ p = compatible_fmod(p, 100000.0); return noise_scale1(noise_perlin(p)); @@ -277,7 +277,7 @@ float snoise(vec2 p) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ p = vec2(compatible_fmod(p.x, 100000.0), compatible_fmod(p.y, 100000.0)); return noise_scale2(noise_perlin(p)); @@ -292,7 +292,7 @@ float snoise(vec3 p) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ p = vec3(compatible_fmod(p.x, 100000.0), compatible_fmod(p.y, 100000.0), compatible_fmod(p.z, 100000.0)); @@ -309,7 +309,7 @@ float snoise(vec4 p) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticible. */ + * usually shouldn't be noticeable. */ p = vec4(compatible_fmod(p.x, 100000.0), compatible_fmod(p.y, 100000.0), compatible_fmod(p.z, 100000.0), -- 2.30.2 From 880d17e953aef3799e73361357dbf29ab84949a7 Mon Sep 17 00:00:00 2001 From: Hoshinova Date: Wed, 27 Mar 2024 16:06:42 +0100 Subject: [PATCH 3/3] -- Overload fmod. --- intern/cycles/kernel/osl/shaders/node_noise.h | 20 ++++++++----------- intern/cycles/kernel/svm/noise.h | 13 ++++++------ intern/cycles/util/math_float2.h | 5 +++++ intern/cycles/util/math_float3.h | 5 +++++ intern/cycles/util/math_float4.h | 5 +++++ source/blender/blenlib/intern/noise.cc | 17 ++++++---------- .../common/gpu_shader_common_math_utils.glsl | 18 +++++++++++++++++ .../material/gpu_shader_material_noise.glsl | 15 +++++--------- 8 files changed, 58 insertions(+), 40 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_noise.h b/intern/cycles/kernel/osl/shaders/node_noise.h index 3ad24eb9918..21d5f0fd28e 100644 --- a/intern/cycles/kernel/osl/shaders/node_noise.h +++ b/intern/cycles/kernel/osl/shaders/node_noise.h @@ -10,8 +10,7 @@ float safe_noise(float co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point - * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticeable. */ + * representation issues. */ float p = fmod(co, 100000.0); return noise("noise", p); @@ -22,7 +21,7 @@ float safe_noise(vector2 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); + vector2 p = fmod(co, 100000.0); return noise("noise", p.x, p.y); } @@ -32,7 +31,7 @@ float safe_noise(vector3 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); + vector3 p = fmod(co, 100000.0); return noise("noise", p); } @@ -42,8 +41,7 @@ float safe_noise(vector4 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector4 p = vector4( - fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); + vector4 p = fmod(co, 100000.0); return noise("noise", vector3(p.x, p.y, p.z), p.w); } @@ -51,8 +49,7 @@ float safe_noise(vector4 co) float safe_snoise(float co) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point - * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticeable. */ + * representation issues. */ float p = fmod(co, 100000.0); return noise("snoise", p); @@ -63,7 +60,7 @@ float safe_snoise(vector2 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector2 p = vector2(fmod(co.x, 100000.0), fmod(co.y, 100000.0)); + vector2 p = fmod(co, 100000.0); return noise("snoise", p.x, p.y); } @@ -73,7 +70,7 @@ float safe_snoise(vector3 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector3 p = vector3(fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0)); + vector3 p = fmod(co, 100000.0); return noise("snoise", p); } @@ -83,8 +80,7 @@ float safe_snoise(vector4 co) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - vector4 p = vector4( - fmod(co.x, 100000.0), fmod(co.y, 100000.0), fmod(co.z, 100000.0), fmod(co.w, 100000.0)); + vector4 p = fmod(co, 100000.0); return noise("snoise", vector3(p.x, p.y, p.z), p.w); } diff --git a/intern/cycles/kernel/svm/noise.h b/intern/cycles/kernel/svm/noise.h index 908a812483f..cb6355a558a 100644 --- a/intern/cycles/kernel/svm/noise.h +++ b/intern/cycles/kernel/svm/noise.h @@ -684,9 +684,9 @@ ccl_device_inline float noise_scale4(float result) ccl_device_inline float snoise_1d(float p) { - /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point - * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticeable. */ + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. */ + /* The 1D variant of fmod is called fmodf. */ p = fmodf(p, 100000.0f); return noise_scale1(perlin_1d(p)); @@ -702,7 +702,7 @@ ccl_device_inline float snoise_2d(float2 p) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - p = make_float2(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f)); + p = fmod(p, 100000.0f); return noise_scale2(perlin_2d(p.x, p.y)); } @@ -717,7 +717,7 @@ ccl_device_inline float snoise_3d(float3 p) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - p = make_float3(fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f)); + p = fmod(p, 100000.0f); return noise_scale3(perlin_3d(p.x, p.y, p.z)); } @@ -732,8 +732,7 @@ ccl_device_inline float snoise_4d(float4 p) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - p = make_float4( - fmodf(p.x, 100000.0f), fmodf(p.y, 100000.0f), fmodf(p.z, 100000.0f), fmodf(p.w, 100000.0f)); + p = fmod(p, 100000.0f); return noise_scale4(perlin_4d(p.x, p.y, p.z, p.w)); } diff --git a/intern/cycles/util/math_float2.h b/intern/cycles/util/math_float2.h index 95337a082e6..d1aaab3b461 100644 --- a/intern/cycles/util/math_float2.h +++ b/intern/cycles/util/math_float2.h @@ -198,6 +198,11 @@ ccl_device_inline float2 clamp(const float2 a, const float2 mn, const float2 mx) return min(max(a, mn), mx); } +ccl_device_inline float2 fmod(const float2 a, const float b) +{ + return make_float2(fmodf(a.x, b), fmodf(a.y, b)); +} + ccl_device_inline float2 fabs(const float2 a) { return make_float2(fabsf(a.x), fabsf(a.y)); diff --git a/intern/cycles/util/math_float3.h b/intern/cycles/util/math_float3.h index fd3dc0d71aa..81943900ad7 100644 --- a/intern/cycles/util/math_float3.h +++ b/intern/cycles/util/math_float3.h @@ -309,6 +309,11 @@ ccl_device_inline float3 fabs(const float3 a) # endif } +ccl_device_inline float3 fmod(const float3 a, const float b) +{ + return make_float3(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b)); +} + ccl_device_inline float3 sqrt(const float3 a) { # ifdef __KERNEL_SSE__ diff --git a/intern/cycles/util/math_float4.h b/intern/cycles/util/math_float4.h index 369b5fef3c2..9921f718beb 100644 --- a/intern/cycles/util/math_float4.h +++ b/intern/cycles/util/math_float4.h @@ -465,6 +465,11 @@ ccl_device_inline float4 fabs(const float4 a) # endif } +ccl_device_inline float4 fmod(const float4 a, const float b) +{ + return make_float4(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b), fmodf(a.w, b)); +} + ccl_device_inline float4 floor(const float4 a) { # ifdef __KERNEL_SSE__ diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc index 389ac33e3e5..c0403191ad8 100644 --- a/source/blender/blenlib/intern/noise.cc +++ b/source/blender/blenlib/intern/noise.cc @@ -490,10 +490,9 @@ BLI_INLINE float perlin_noise(float4 position) float perlin_signed(float position) { - /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point - * representation issues. This causes discontinuities every 100000.0f, however at such scales - * this usually shouldn't be noticeable. */ - position = fmodf(position, 100000.0f); + /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point + * representation issues. */ + position = math::mod(position, 100000.0f); return perlin_noise(position) * 0.2500f; } @@ -503,7 +502,7 @@ float perlin_signed(float2 position) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - position = float2(fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f)); + position = math::mod(position, 100000.0f); return perlin_noise(position) * 0.6616f; } @@ -513,8 +512,7 @@ float perlin_signed(float3 position) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - position = float3( - fmodf(position.x, 100000.0f), fmodf(position.y, 100000.0f), fmodf(position.z, 100000.0f)); + position = math::mod(position, 100000.0f); return perlin_noise(position) * 0.9820f; } @@ -524,10 +522,7 @@ float perlin_signed(float4 position) /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0f, however at such scales * this usually shouldn't be noticeable. */ - position = float4(fmodf(position.x, 100000.0f), - fmodf(position.y, 100000.0f), - fmodf(position.z, 100000.0f), - fmodf(position.w, 100000.0f)); + position = math::mod(position, 100000.0f); return perlin_noise(position) * 0.8344f; } diff --git a/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl b/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl index f697ef1be2e..8fb15b38e5b 100644 --- a/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl +++ b/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl @@ -24,6 +24,24 @@ float compatible_fmod(float a, float b) return 0.0; } +vec2 compatible_fmod(vec2 a, float b) +{ + return vec2(compatible_fmod(a.x, b), compatible_fmod(a.y, b)); +} + +vec3 compatible_fmod(vec3 a, float b) +{ + return vec3(compatible_fmod(a.x, b), compatible_fmod(a.y, b), compatible_fmod(a.z, b)); +} + +vec4 compatible_fmod(vec4 a, float b) +{ + return vec4(compatible_fmod(a.x, b), + compatible_fmod(a.y, b), + compatible_fmod(a.z, b), + compatible_fmod(a.w, b)); +} + float compatible_pow(float x, float y) { if (y == 0.0) { /* x^0 -> 1, including 0^0 */ diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl index 8d897738469..9cb201c0017 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_noise.glsl @@ -2,6 +2,7 @@ * * SPDX-License-Identifier: GPL-2.0-or-later */ +#pragma BLENDER_REQUIRE(gpu_shader_common_math_utils.glsl) #pragma BLENDER_REQUIRE(gpu_shader_common_hash.glsl) /* clang-format off */ @@ -261,8 +262,7 @@ float noise_scale4(float result) float snoise(float p) { /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point - * representation issues. This causes discontinuities every 100000.0, however at such scales this - * usually shouldn't be noticeable. */ + * representation issues. */ p = compatible_fmod(p, 100000.0); return noise_scale1(noise_perlin(p)); @@ -278,7 +278,7 @@ float snoise(vec2 p) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - p = vec2(compatible_fmod(p.x, 100000.0), compatible_fmod(p.y, 100000.0)); + p = compatible_fmod(p, 100000.0); return noise_scale2(noise_perlin(p)); } @@ -293,9 +293,7 @@ float snoise(vec3 p) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - p = vec3(compatible_fmod(p.x, 100000.0), - compatible_fmod(p.y, 100000.0), - compatible_fmod(p.z, 100000.0)); + p = compatible_fmod(p, 100000.0); return noise_scale3(noise_perlin(p)); } @@ -310,10 +308,7 @@ float snoise(vec4 p) /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point * representation issues. This causes discontinuities every 100000.0, however at such scales this * usually shouldn't be noticeable. */ - p = vec4(compatible_fmod(p.x, 100000.0), - compatible_fmod(p.y, 100000.0), - compatible_fmod(p.z, 100000.0), - compatible_fmod(p.w, 100000.0)); + p = compatible_fmod(p, 100000.0); return noise_scale4(noise_perlin(p)); } -- 2.30.2