Blender V5.0
node_hash.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "int_vector_types.h"
6#include "stdcycles.h"
7#include "vector2.h"
8#include "vector4.h"
9
10#define vector3 point
11
12/* Hashing `uint` or `uint[234]` into a float in the range [0, 1].
13 * Based on PCG 2D/3D/4D hash, but with signed integers. */
14
16{
17 int2 v = k * 1664525 + 1013904223;
18 v.x += v.y * 1664525;
19 v.y += v.x * 1664525;
20 v = v ^ (v >> 16);
21 v.x += v.y * 1664525;
22 v.y += v.x * 1664525;
23 vector2 f = int2_to_vec2(v & 0x7FFFFFFF);
24 return f * (1.0 / (float)0x7FFFFFFF);
25}
26
28{
29 int3 v = k * 1664525 + 1013904223;
30 v.x += v.y * v.z;
31 v.y += v.z * v.x;
32 v.z += v.x * v.y;
33 v = v ^ (v >> 16);
34 v.x += v.y * v.z;
35 v.y += v.z * v.x;
36 v.z += v.x * v.y;
37 vector3 f = int3_to_vec3(v & 0x7FFFFFFF);
38 return f * (1.0 / (float)0x7FFFFFFF);
39}
40
42{
43 int4 v = k * 1664525 + 1013904223;
44 v.x += v.y * v.w;
45 v.y += v.z * v.x;
46 v.z += v.x * v.y;
47 v.w += v.y * v.z;
48 v = v ^ (v >> 16);
49 v.x += v.y * v.w;
50 v.y += v.z * v.x;
51 v.z += v.x * v.y;
52 v.w += v.y * v.z;
53 vector4 f = int4_to_vec4(v & 0x7FFFFFFF);
54 return f * (1.0 / (float)0x7FFFFFFF);
55}
56
58{
59 return hash_int3_to_vector3(int3(k.x, k.y, 0));
60}
61
63{
64 vector4 v = hash_int4_to_vector4(k);
65 return color(v.x, v.y, v.z);
66}
67
68/* **** Hash a float or vector[234] into a float [0, 1] **** */
69
70float hash_float_to_float(float k)
71{
72 return hashnoise(k);
73}
74
75float hash_vector2_to_float(vector2 k)
76{
77 return hashnoise(k.x, k.y);
78}
79
81{
82 return hashnoise(k);
83}
84
85float hash_vector4_to_float(vector4 k)
86{
87 return hashnoise(vector3(k.x, k.y, k.z), k.w);
88}
89
90/* **** Hash a vector[234] into a vector[234] [0, 1] **** */
91
92vector2 hash_vector2_to_vector2(vector2 k)
93{
94 return vector2(hash_vector2_to_float(k), hash_vector3_to_float(vector3(k.x, k.y, 1.0)));
95}
96
98{
100 hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
101 hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
102}
103
104vector4 hash_vector4_to_vector4(vector4 k)
105{
106 return vector4(hash_vector4_to_float(k),
107 hash_vector4_to_float(vector4(k.w, k.x, k.y, k.z)),
108 hash_vector4_to_float(vector4(k.z, k.w, k.x, k.y)),
109 hash_vector4_to_float(vector4(k.y, k.z, k.w, k.x)));
110}
111
112/* **** Hash a float or a vec[234] into a color [0, 1] **** */
113
115{
116 return color(hash_float_to_float(k),
117 hash_vector2_to_float(vector2(k, 1.0)),
118 hash_vector2_to_float(vector2(k, 2.0)));
119}
120
121color hash_vector2_to_color(vector2 k)
122{
123 return color(hash_vector2_to_float(k),
124 hash_vector3_to_float(vector3(k.x, k.y, 1.0)),
125 hash_vector3_to_float(vector3(k.x, k.y, 2.0)));
126}
127
129{
130 return color(hash_vector3_to_float(k),
131 hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
132 hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
133}
134
135color hash_vector4_to_color(vector4 k)
136{
137 return color(hash_vector4_to_float(k),
138 hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)),
139 hash_vector4_to_float(vector4(k.w, k.z, k.y, k.x)));
140}
141
142/* **** Hash a float or a vec[234] into a vector3 [0, 1] **** */
143
145{
147 hash_vector2_to_float(vector2(k, 1.0)),
148 hash_vector2_to_float(vector2(k, 2.0)));
149}
150
152{
154 hash_vector3_to_float(vector3(k.x, k.y, 1.0)),
155 hash_vector3_to_float(vector3(k.x, k.y, 2.0)));
156}
157
159{
161 hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)),
162 hash_vector4_to_float(vector4(k.w, k.z, k.y, k.x)));
163}
164
165/* Hashing float or vector[234] into vector2 of components in range [0, 1]. */
166
167vector2 hash_float_to_vector2(float k)
168{
169 return vector2(hash_float_to_float(k), hash_vector2_to_float(vector2(k, 1.0)));
170}
171
173{
174 return vector2(hash_vector3_to_float(vector3(k.x, k.y, k.z)),
175 hash_vector3_to_float(vector3(k.z, k.x, k.y)));
176}
177
178vector2 hash_vector4_to_vector2(vector4 k)
179{
180 return vector2(hash_vector4_to_float(vector4(k.x, k.y, k.z, k.w)),
181 hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)));
182}
183
184#undef vector3
ATTR_WARN_UNUSED_RESULT const BMVert * v
nullptr float
VecBase< int, 3 > int3
vector2 int2_to_vec2(int2 k)
point int3_to_vec3(int3 k)
vector4 int4_to_vec4(int4 k)
#define vector3
color hash_vector3_to_color(vector3 k)
Definition node_hash.h:128
vector3 hash_float_to_vector3(float k)
Definition node_hash.h:144
color hash_int4_to_color(int4 k)
Definition node_hash.h:62
float hash_vector4_to_float(vector4 k)
Definition node_hash.h:85
float hash_vector3_to_float(vector3 k)
Definition node_hash.h:80
vector2 hash_float_to_vector2(float k)
Definition node_hash.h:167
color hash_float_to_color(float k)
Definition node_hash.h:114
vector2 hash_int2_to_vector2(int2 k)
Definition node_hash.h:15
vector3 hash_int3_to_vector3(int3 k)
Definition node_hash.h:27
color hash_vector4_to_color(vector4 k)
Definition node_hash.h:135
float hash_vector2_to_float(vector2 k)
Definition node_hash.h:75
vector2 hash_vector3_to_vector2(vector3 k)
Definition node_hash.h:172
color hash_vector2_to_color(vector2 k)
Definition node_hash.h:121
#define vector3
Definition node_hash.h:10
vector2 hash_vector2_to_vector2(vector2 k)
Definition node_hash.h:92
color hash_int2_to_color(int2 k)
Definition node_hash.h:57
vector4 hash_int4_to_vector4(int4 k)
Definition node_hash.h:41
vector4 hash_vector4_to_vector4(vector4 k)
Definition node_hash.h:104
float hash_float_to_float(float k)
Definition node_hash.h:70
vector3 hash_vector2_to_vector3(vector2 k)
Definition node_hash.h:151
vector3 hash_vector4_to_vector3(vector4 k)
Definition node_hash.h:158
vector3 hash_vector3_to_vector3(vector3 k)
Definition node_hash.h:97
vector2 hash_vector4_to_vector2(vector4 k)
Definition node_hash.h:178