[Bf-blender-cvs] [a1167e910a2] master: BLI: Add Cycles compatible Perlin noise

Omar Emara noreply at git.blender.org
Fri Sep 10 14:26:45 CEST 2021


Commit: a1167e910a22077a6738edc3ee8e57ab4d5d62dd
Author: Omar Emara
Date:   Fri Sep 10 14:25:32 2021 +0200
Branches: master
https://developer.blender.org/rBa1167e910a22077a6738edc3ee8e57ab4d5d62dd

BLI: Add Cycles compatible Perlin noise

This patch adds new Perlin noise functions to BLI. The noises are compatible
with the shading texture noises in EEVEE, SVM, and OSL.

The existing Jenkins hash functions couldn't be used because they are not
compatible with the shading implementations and an attempt at adjusting the
implementation will break compatibility in various areas of Blender. So the
simplest approach is to reimplement the relevant hashing functions inside the
noise module itself.

Additionally, this patch also adds a minimal float4 structure to use in the
interface of the noise functions.

Reviewed By: JacquesLucke

Differential Revision: https://developer.blender.org/D12443

===================================================================

A	source/blender/blenlib/BLI_float4.hh
A	source/blender/blenlib/BLI_noise.hh
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/noise.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_float4.hh b/source/blender/blenlib/BLI_float4.hh
new file mode 100644
index 00000000000..b1feee3121b
--- /dev/null
+++ b/source/blender/blenlib/BLI_float4.hh
@@ -0,0 +1,86 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+namespace blender {
+
+struct float4 {
+  float x, y, z, w;
+
+  float4() = default;
+
+  float4(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}, w{ptr[3]}
+  {
+  }
+
+  explicit float4(float value) : x(value), y(value), z(value), w(value)
+  {
+  }
+
+  explicit float4(int value) : x(value), y(value), z(value), w(value)
+  {
+  }
+
+  float4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w)
+  {
+  }
+
+  operator float *()
+  {
+    return &x;
+  }
+
+  operator const float *() const
+  {
+    return &x;
+  }
+
+  float4 &operator+=(const float4 &other)
+  {
+    x += other.x;
+    y += other.y;
+    z += other.z;
+    w += other.w;
+    return *this;
+  }
+
+  friend float4 operator+(const float4 &a, const float4 &b)
+  {
+    return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
+  }
+
+  float4 &operator*=(float factor)
+  {
+    x *= factor;
+    y *= factor;
+    z *= factor;
+    w *= factor;
+    return *this;
+  }
+
+  friend float4 operator*(const float4 &a, float b)
+  {
+    return {a.x * b, a.y * b, a.z * b, a.w * b};
+  }
+
+  friend float4 operator*(float a, const float4 &b)
+  {
+    return b * a;
+  }
+};
+
+}  // namespace blender
diff --git a/source/blender/blenlib/BLI_noise.hh b/source/blender/blenlib/BLI_noise.hh
new file mode 100644
index 00000000000..760ff082d06
--- /dev/null
+++ b/source/blender/blenlib/BLI_noise.hh
@@ -0,0 +1,72 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include "BLI_float2.hh"
+#include "BLI_float3.hh"
+#include "BLI_float4.hh"
+
+namespace blender::noise {
+
+/* Perlin noise in the range [-1, 1]. */
+
+float perlin_signed(float position);
+float perlin_signed(float2 position);
+float perlin_signed(float3 position);
+float perlin_signed(float4 position);
+
+/* Perlin noise in the range [0, 1]. */
+
+float perlin(float position);
+float perlin(float2 position);
+float perlin(float3 position);
+float perlin(float4 position);
+
+/* Fractal perlin noise in the range [0, 1]. */
+
+float perlin_fractal(float position, float octaves, float roughness);
+float perlin_fractal(float2 position, float octaves, float roughness);
+float perlin_fractal(float3 position, float octaves, float roughness);
+float perlin_fractal(float4 position, float octaves, float roughness);
+
+/* Positive distorted fractal perlin noise. */
+
+float perlin_fractal_distorted(float position, float octaves, float roughness, float distortion);
+float perlin_fractal_distorted(float2 position, float octaves, float roughness, float distortion);
+float perlin_fractal_distorted(float3 position, float octaves, float roughness, float distortion);
+float perlin_fractal_distorted(float4 position, float octaves, float roughness, float distortion);
+
+/* Positive distorted fractal perlin noise that outputs a float3. */
+
+float3 perlin_float3_fractal_distorted(float position,
+                                       float octaves,
+                                       float roughness,
+                                       float distortion);
+float3 perlin_float3_fractal_distorted(float2 position,
+                                       float octaves,
+                                       float roughness,
+                                       float distortion);
+float3 perlin_float3_fractal_distorted(float3 position,
+                                       float octaves,
+                                       float roughness,
+                                       float distortion);
+float3 perlin_float3_fractal_distorted(float4 position,
+                                       float octaves,
+                                       float roughness,
+                                       float distortion);
+
+}  // namespace blender::noise
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 24178535068..fc058793d11 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -116,6 +116,7 @@ set(SRC
   intern/mesh_boolean.cc
   intern/mesh_intersect.cc
   intern/noise.c
+  intern/noise.cc
   intern/path_util.c
   intern/polyfill_2d.c
   intern/polyfill_2d_beautify.c
@@ -203,6 +204,7 @@ set(SRC
   BLI_filereader.h
   BLI_float2.hh
   BLI_float3.hh
+  BLI_float4.hh
   BLI_float4x4.hh
   BLI_fnmatch.h
   BLI_function_ref.hh
@@ -264,6 +266,7 @@ set(SRC
   BLI_mpq3.hh
   BLI_multi_value_map.hh
   BLI_noise.h
+  BLI_noise.hh
   BLI_path_util.h
   BLI_polyfill_2d.h
   BLI_polyfill_2d_beautify.h
diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc
new file mode 100644
index 00000000000..c057c12e543
--- /dev/null
+++ b/source/blender/blenlib/intern/noise.cc
@@ -0,0 +1,693 @@
+/*
+ * Adapted from Open Shading Language with this license:
+ *
+ * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
+ * All Rights Reserved.
+ *
+ * Modifications Copyright 2011, Blender Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of Sony Pictures Imageworks nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <cmath>
+#include <cstdint>
+
+#include "BLI_float2.hh"
+#include "BLI_float3.hh"
+#include "BLI_float4.hh"
+#include "BLI_noise.hh"
+#include "BLI_utildefines.h"
+
+namespace blender::noise {
+/* ------------------------------
+ * Jenkins Lookup3 Hash Functions
+ * ------------------------------
+ *
+ * https://burtleburtle.net/bob/c/lookup3.c
+ *
+ */
+
+BLI_INLINE uint32_t hash_bit_rotate(uint32_t x, uint32_t k)
+{
+  return (x << k) | (x >> (32 - k));
+}
+
+BLI_INLINE void hash_bit_mix(uint32_t &a, uint32_t &b, uint32_t &c)
+{
+  a -= c;
+  a ^= hash_bit_rotate(c, 4);
+  c += b;
+  b -= a;
+  b ^= hash_bit_rotate(a, 6);
+  a += c;
+  c -= b;
+  c ^= hash_bit_rotate(b, 8);
+  b += a;
+  a -= c;
+  a ^= hash_bit_rotate(c, 16);
+  c += b;
+  b -= a;
+  b ^= hash_bit_rotate(a, 19);
+  a += c;
+  c -= b;
+  c ^= hash_bit_rotate(b, 4);
+  b += a;
+}
+
+BLI_INLINE void hash_bit_final(uint32_t &a, uint32_t &b, uint32_t &c)
+{
+  c ^= b;
+  c -= hash_bit_rotate(b, 14);
+  a ^= c;
+  a -= hash_bit_rotate(c, 11);
+  b ^= a;
+  b -= hash_bit_rotate(a, 25);
+  c ^= b;
+  c -= hash_bit_rotate(b, 16);
+  a ^= c;
+  a -= hash_bit_rotate(c, 4);
+  b ^= a;
+  b -= hash_bit_rotate(a, 14);
+  c ^= b;
+  c -= hash_bit_rotate(b, 24);
+}
+
+BLI_INLINE uint32_t hash(uint32_t kx)
+{
+  uint32_t a, b, c;
+  a = b = c = 0xdeadbeef + (1 << 2) + 13;
+
+  a += 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list