Blender V5.3
image_3d.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#pragma once
6
7#include "kernel/globals.h"
8#include "kernel/sample/lcg.h"
9
10#include "util/types_image.h"
11
12#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
13# ifdef WITH_NANOVDB
14# include "kernel/util/nanovdb.h"
15# endif
16#endif
17
19
20#ifndef __KERNEL_GPU__
21/* Make template functions private so symbols don't conflict between kernels with different
22 * instruction sets. */
23namespace {
24#endif
25
26#ifdef WITH_NANOVDB
27
28/* Cubic interpolation weights. */
29ccl_device_forceinline void fill_cubic_weights(float3 w[4], float3 t)
30{
31 w[0] = (((-1.0f / 6.0f) * t + 0.5f) * t - 0.5f) * t + (1.0f / 6.0f);
32 w[1] = ((0.5f * t - 1.0f) * t) * t + (2.0f / 3.0f);
33 w[2] = ((-0.5f * t + 0.5f) * t + 0.5f) * t + (1.0f / 6.0f);
34 w[3] = (1.0f / 6.0f) * t * t * t;
35}
36
37/* -------------------------------------------------------------------- */
41ccl_device_inline float3 interp_tricubic_stochastic(const float3 P, ccl_private float3 &rand)
42{
43 const float3 p = floor(P);
44 const float3 t = P - p;
45
46 float3 w[4];
47 fill_cubic_weights(w, t);
48
49 /* For reservoir sampling, always accept the first in the stream. */
50 float3 total_weight = w[0];
51 float3 offset = make_float3(-1.0f);
52
53 for (int j = 1; j < 4; j++) {
54 total_weight += w[j];
55 const float3 thresh = w[j] / total_weight;
56 const auto mask = rand < thresh;
57 offset = select(mask, make_float3(float(j) - 1.0f), offset);
58 rand = select(mask, safe_divide(rand, thresh), safe_divide(rand - thresh, 1.0f - thresh));
59 }
60
61 return p + offset;
62}
63
64ccl_device_inline float3 interp_trilinear_stochastic(const float3 P, const float3 rand)
65{
66 const float3 p = floor(P);
67 const float3 t = P - p;
68 return select(rand < t, p + 1.0f, p);
69}
70
71ccl_device_inline float3 interp_stochastic(const float3 P,
72 ccl_private InterpolationType &interpolation,
73 ccl_private float3 &rand)
74{
75 float3 P_new = P;
76 if (interpolation == INTERPOLATION_CUBIC) {
77 P_new = interp_tricubic_stochastic(P, rand);
78 }
79 else if (interpolation == INTERPOLATION_LINEAR) {
80 P_new = interp_trilinear_stochastic(P, rand);
81 }
82 else {
83 kernel_assert(interpolation == INTERPOLATION_CLOSEST);
84 }
85 interpolation = INTERPOLATION_CLOSEST;
86 return P_new;
87}
89
90template<typename OutT, typename Acc>
91ccl_device OutT kernel_image_interp_trilinear_nanovdb(ccl_private Acc &acc, const float3 P)
92{
93 const float3 floor_P = floor(P);
94 const float3 t = P - floor_P;
95 const int3 index = make_int3(floor_P);
96
97 const int ix = index.x;
98 const int iy = index.y;
99 const int iz = index.z;
100
101 return mix(mix(mix(OutT(acc.getValue(make_int3(ix, iy, iz))),
102 OutT(acc.getValue(make_int3(ix, iy, iz + 1))),
103 t.z),
104 mix(OutT(acc.getValue(make_int3(ix, iy + 1, iz + 1))),
105 OutT(acc.getValue(make_int3(ix, iy + 1, iz))),
106 1.0f - t.z),
107 t.y),
108 mix(mix(OutT(acc.getValue(make_int3(ix + 1, iy + 1, iz))),
109 OutT(acc.getValue(make_int3(ix + 1, iy + 1, iz + 1))),
110 t.z),
111 mix(OutT(acc.getValue(make_int3(ix + 1, iy, iz + 1))),
112 OutT(acc.getValue(make_int3(ix + 1, iy, iz))),
113 1.0f - t.z),
114 1.0f - t.y),
115 t.x);
116}
117
118template<typename OutT, typename Acc>
119ccl_device OutT kernel_image_interp_tricubic_nanovdb(ccl_private Acc &acc, const float3 P)
120{
121# if defined(__KERNEL_HIP__)
122 /* Explicitly unroll for HIP compiler to unroll the loop. Without this the render result is wrong
123 * on a specific platform/compiler combinations. ALso don't rely on the `unroll` hint as it has
124 * a performance impact. See #152126 and discussion/benchmark in !152321. */
125
126 const float3 floor_P = floor(P);
127 const float3 t = P - floor_P;
128 const int3 index = make_int3(floor_P);
129
130 const int xc[4] = {index.x - 1, index.x, index.x + 1, index.x + 2};
131 const int yc[4] = {index.y - 1, index.y, index.y + 1, index.y + 2};
132 const int zc[4] = {index.z - 1, index.z, index.z + 1, index.z + 2};
133
134 float3 weight[4];
135 fill_cubic_weights(weight, t);
136
137# define DATA(x, y, z) (OutT(acc.getValue(make_int3(xc[x], yc[y], zc[z]))))
138# define COL_TERM(col, row) \
139 (weight[col].y * (weight[0].x * DATA(0, col, row) + weight[1].x * DATA(1, col, row) + \
140 weight[2].x * DATA(2, col, row) + weight[3].x * DATA(3, col, row)))
141# define ROW_TERM(row) \
142 (weight[row].z * (COL_TERM(0, row) + COL_TERM(1, row) + COL_TERM(2, row) + COL_TERM(3, row)))
143
144 /* Actual interpolation. */
145 return ROW_TERM(0) + ROW_TERM(1) + ROW_TERM(2) + ROW_TERM(3);
146
147# undef COL_TERM
148# undef ROW_TERM
149# undef DATA
150
151# else
152
153 const float3 floor_P = floor(P);
154 const float3 t = P - floor_P;
155 const int3 index = make_int3(floor_P) - make_int3(1);
156
157 float3 w[4];
158 fill_cubic_weights(w, t);
159
160 OutT result = make_zero<OutT>();
161
162 for (int k = 0; k < 4; k++) {
163 OutT col_term_acc = make_zero<OutT>();
164 for (int j = 0; j < 4; j++) {
165 col_term_acc += w[j].y * (w[0].x * (OutT(acc.getValue(index + make_int3(0, j, k)))) +
166 w[1].x * (OutT(acc.getValue(index + make_int3(1, j, k)))) +
167 w[2].x * (OutT(acc.getValue(index + make_int3(2, j, k)))) +
168 w[3].x * (OutT(acc.getValue(index + make_int3(3, j, k)))));
169 }
170 result += w[k].z * col_term_acc;
171 }
172
173 return result;
174# endif
175}
176
177template<typename OutT, typename T>
178# if defined(__KERNEL_METAL__)
179__attribute__((noinline))
180# else
182# endif
183OutT kernel_image_interp_nanovdb(const ccl_global KernelImageInfo &info,
184 float3 P,
186{
188
190 nanovdb::ReadAccessor<T> acc(grid->tree().root());
191 return OutT(acc.getValue(make_int3(floor(P))));
192 }
193
194 nanovdb::CachedReadAccessor<T> acc(grid->tree().root());
196 return kernel_image_interp_trilinear_nanovdb<OutT>(acc, P);
197 }
198
199 return kernel_image_interp_tricubic_nanovdb<OutT>(acc, P);
200}
201#endif /* WITH_NANOVDB */
202
204 ccl_private ShaderData *sd,
205 const int image_texture_id,
206 float3 P,
208 const bool stochastic)
209{
210#ifdef WITH_NANOVDB
211 const ccl_global KernelImageTexture &tex = kernel_data_fetch(image_textures, image_texture_id);
212 const ccl_global KernelImageInfo &info = kernel_data_fetch(image_info, tex.image_info_id);
213
214 if (tex.use_transform_3d) {
215 P = transform_point(&tex.transform_3d, P);
216 }
217
218 InterpolationType interpolation = (interp == INTERPOLATION_NONE) ?
219 (InterpolationType)info.interpolation :
220 interp;
221
222 if (stochastic) {
223 float3 rand = lcg_step_float3(&sd->lcg_state);
224 P = interp_stochastic(P, interpolation, rand);
225 }
226
227 const ImageDataType data_type = (ImageDataType)info.data_type;
228 if (data_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT) {
229 const float f = kernel_image_interp_nanovdb<float, float>(info, P, interpolation);
230 return make_float4(f, f, f, 1.0f);
231 }
232 if (data_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT3) {
233 const float3 f = kernel_image_interp_nanovdb<float3, packed_float3>(info, P, interpolation);
234 return make_float4(f, 1.0f);
235 }
236 if (data_type == IMAGE_DATA_TYPE_NANOVDB_FLOAT4) {
237 return kernel_image_interp_nanovdb<float4, float4>(info, P, interpolation);
238 }
239 if (data_type == IMAGE_DATA_TYPE_NANOVDB_FPN) {
240 const float f = kernel_image_interp_nanovdb<float, nanovdb::FpN>(info, P, interpolation);
241 return make_float4(f, f, f, 1.0f);
242 }
243 if (data_type == IMAGE_DATA_TYPE_NANOVDB_FP16) {
244 const float f = kernel_image_interp_nanovdb<float, nanovdb::Fp16>(info, P, interpolation);
245 return make_float4(f, f, f, 1.0f);
246 }
247 if (data_type == IMAGE_DATA_TYPE_NANOVDB_EMPTY) {
248 return zero_float4();
249 }
250#else
251 (void)kg;
252 (void)sd;
253 (void)image_texture_id;
254 (void)P;
255 (void)interp;
256 (void)stochastic;
257#endif
258
259 return IMAGE_MISSING_RGBA;
260}
261
262#ifndef __KERNEL_GPU__
263} /* Namespace. */
264#endif
265
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define kernel_assert(cond)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
#define index(i)
#define select(A, B, C)
#define floor
float3 transform_point(float3x3 mat, float3 point)
float safe_divide(float a, float b)
ccl_device float3 lcg_step_float3(T rng)
Definition lcg.h:29
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define IMAGE_MISSING_RGBA
ccl_device float4 kernel_image_interp_3d(KernelGlobals kg, ccl_private ShaderData *sd, const int image_texture_id, float3 P, InterpolationType interp, const bool stochastic)
Definition image_3d.h:203
Grid< NanoTree< BuildT > > NanoGrid
#define mix
#define ccl_device
#define make_float4
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ImageDataType
Definition types_image.h:35
@ IMAGE_DATA_TYPE_NANOVDB_FP16
Definition types_image.h:48
@ IMAGE_DATA_TYPE_NANOVDB_EMPTY
Definition types_image.h:49
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition types_image.h:44
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition types_image.h:45
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT4
Definition types_image.h:46
@ IMAGE_DATA_TYPE_NANOVDB_FPN
Definition types_image.h:47
InterpolationType
Definition types_image.h:24
@ INTERPOLATION_LINEAR
Definition types_image.h:26
@ INTERPOLATION_NONE
Definition types_image.h:25
@ INTERPOLATION_CLOSEST
Definition types_image.h:27
@ INTERPOLATION_CUBIC
Definition types_image.h:28