Blender V5.3
cycles/kernel/util/image.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9#include "util/defines.h"
10#include "util/half.h"
11#include "util/types_image.h"
12
14
15#ifndef __KERNEL_GPU__
16/* Make template functions private so symbols don't conflict between kernels with different
17 * instruction sets. */
18namespace {
19#endif
20
21#define SET_CUBIC_SPLINE_WEIGHTS(u, t) \
22 { \
23 u[0] = (((-1.0f / 6.0f) * t + 0.5f) * t - 0.5f) * t + (1.0f / 6.0f); \
24 u[1] = ((0.5f * t - 1.0f) * t) * t + (2.0f / 3.0f); \
25 u[2] = ((-0.5f * t + 0.5f) * t + 0.5f) * t + (1.0f / 6.0f); \
26 u[3] = (1.0f / 6.0f) * t * t * t; \
27 } \
28 (void)0
29
30template<typename TexT, typename OutT = float4> struct ImageInterpolator {
31
32 static ccl_always_inline OutT zero()
33 {
34 if constexpr (std::is_same_v<OutT, float4>) {
35 return zero_float4();
36 }
37 else {
38 return 0.0f;
39 }
40 }
41
43 {
44 return r;
45 }
46
48 {
49 const float f = 1.0f / 255.0f;
50 return make_float4(r.x * f, r.y * f, r.z * f, r.w * f);
51 }
52
53 static ccl_always_inline float read(const uchar r)
54 {
55 return r * (1.0f / 255.0f);
56 }
57
58 static ccl_always_inline float read(const float r)
59 {
60 return r;
61 }
62
64 {
65 return half4_to_float4_image(r);
66 }
67
68 static ccl_always_inline float read(half r)
69 {
70 return half_to_float_image(r);
71 }
72
73 static ccl_always_inline float read(const uint16_t r)
74 {
75 return r * (1.0f / 65535.0f);
76 }
77
79 {
80 const float f = 1.0f / 65535.0f;
81 return make_float4(r.x * f, r.y * f, r.z * f, r.w * f);
82 }
83
84 /* Read 2D Texture Data
85 * Does not check if data request is in bounds. */
86 static ccl_always_inline OutT
87 read(const ccl_global TexT *data, const int x, int y, const int width, const int /*height*/)
88 {
89 return read(data[int64_t(y) * width + x]);
90 }
91
92 /* Read 2D Texture Data Clip
93 * Returns transparent black if data request is out of bounds. */
94 static ccl_always_inline OutT
95 read_clip(const ccl_global TexT *data, const int x, int y, const int width, const int height)
96 {
97 if (x < 0 || x >= width || y < 0 || y >= height) {
98 return zero();
99 }
100 return read(data[int64_t(y) * width + x]);
101 }
102
103 static ccl_always_inline float frac(const float x, ccl_private int *ix)
104 {
105 int i = float_to_int(x) - ((x < 0.0f) ? 1 : 0);
106 *ix = i;
107 return x - (float)i;
108 }
109
110 static ccl_always_inline int wrap_periodic(int x, const int width)
111 {
112 x %= width;
113 if (x < 0) {
114 x += width;
115 }
116 return x;
117 }
118
119 static ccl_always_inline int wrap_clamp(const int x, const int width)
120 {
121 return clamp(x, 0, width - 1);
122 }
123
124 static ccl_always_inline int wrap_mirror(const int x, const int width)
125 {
126 const int m = abs(x + (x < 0)) % (2 * width);
127 if (m >= width) {
128 return 2 * width - m - 1;
129 }
130 return m;
131 }
132
133 /* ******** 2D interpolation ******** */
134
136 const float x,
137 float y)
138 {
139 const int width = info.width;
140 const int height = info.height;
141 int ix, iy;
142 frac(x, &ix);
143 frac(y, &iy);
144 switch (info.extension) {
145 case EXTENSION_REPEAT:
146 ix = wrap_periodic(ix, width);
147 iy = wrap_periodic(iy, height);
148 break;
149 case EXTENSION_CLIP:
150 /* No samples are inside the clip region. */
151 if (ix < 0 || ix >= width || iy < 0 || iy >= height) {
152 return zero();
153 }
154 break;
155 case EXTENSION_EXTEND:
156 ix = wrap_clamp(ix, width);
157 iy = wrap_clamp(iy, height);
158 break;
159 case EXTENSION_MIRROR:
160 ix = wrap_mirror(ix, width);
161 iy = wrap_mirror(iy, height);
162 break;
163 default:
164 kernel_assert(0);
165 return zero();
166 }
167
168 const ccl_global TexT *data = (const ccl_global TexT *)info.data;
169 return read(data, ix, iy, width, height);
170 }
171
173 const float x,
174 float y)
175 {
176 const int width = info.width;
177 const int height = info.height;
178
179 /* A -0.5 offset is used to center the linear samples around the sample point. */
180 int ix, iy;
181 int nix, niy;
182 const float tx = frac(x - 0.5f, &ix);
183 const float ty = frac(y - 0.5f, &iy);
184 const ccl_global TexT *data = (const ccl_global TexT *)info.data;
185
186 switch (info.extension) {
187 case EXTENSION_REPEAT:
188 ix = wrap_periodic(ix, width);
189 nix = wrap_periodic(ix + 1, width);
190
191 iy = wrap_periodic(iy, height);
192 niy = wrap_periodic(iy + 1, height);
193 break;
194 case EXTENSION_CLIP:
195 /* No linear samples are inside the clip region. */
196 if (ix < -1 || ix >= width || iy < -1 || iy >= height) {
197 return zero();
198 }
199 nix = ix + 1;
200 niy = iy + 1;
201 return (1.0f - ty) * (1.0f - tx) * read_clip(data, ix, iy, width, height) +
202 (1.0f - ty) * tx * read_clip(data, nix, iy, width, height) +
203 ty * (1.0f - tx) * read_clip(data, ix, niy, width, height) +
204 ty * tx * read_clip(data, nix, niy, width, height);
205 case EXTENSION_EXTEND:
206 nix = wrap_clamp(ix + 1, width);
207 ix = wrap_clamp(ix, width);
208 niy = wrap_clamp(iy + 1, height);
209 iy = wrap_clamp(iy, height);
210 break;
211 case EXTENSION_MIRROR:
212 nix = wrap_mirror(ix + 1, width);
213 ix = wrap_mirror(ix, width);
214 niy = wrap_mirror(iy + 1, height);
215 iy = wrap_mirror(iy, height);
216 break;
217 default:
218 kernel_assert(0);
219 return zero();
220 }
221
222 return (1.0f - ty) * (1.0f - tx) * read(data, ix, iy, width, height) +
223 (1.0f - ty) * tx * read(data, nix, iy, width, height) +
224 ty * (1.0f - tx) * read(data, ix, niy, width, height) +
225 ty * tx * read(data, nix, niy, width, height);
226 }
227
229 const float x,
230 float y)
231 {
232 const int width = info.width;
233 const int height = info.height;
234
235 /* A -0.5 offset is used to center the cubic samples around the sample point. */
236 int ix, iy;
237 const float tx = frac(x - 0.5f, &ix);
238 const float ty = frac(y - 0.5f, &iy);
239
240 int pix, piy;
241 int nix, niy;
242 int nnix, nniy;
243
244 switch (info.extension) {
245 case EXTENSION_REPEAT:
246 ix = wrap_periodic(ix, width);
247 pix = wrap_periodic(ix - 1, width);
248 nix = wrap_periodic(ix + 1, width);
249 nnix = wrap_periodic(ix + 2, width);
250
251 iy = wrap_periodic(iy, height);
252 piy = wrap_periodic(iy - 1, height);
253 niy = wrap_periodic(iy + 1, height);
254 nniy = wrap_periodic(iy + 2, height);
255 break;
256 case EXTENSION_CLIP:
257 /* No cubic samples are inside the clip region. */
258 if (ix < -2 || ix > width || iy < -2 || iy > height) {
259 return zero();
260 }
261
262 pix = ix - 1;
263 nix = ix + 1;
264 nnix = ix + 2;
265
266 piy = iy - 1;
267 niy = iy + 1;
268 nniy = iy + 2;
269 break;
270 case EXTENSION_EXTEND:
271 pix = wrap_clamp(ix - 1, width);
272 nix = wrap_clamp(ix + 1, width);
273 nnix = wrap_clamp(ix + 2, width);
274 ix = wrap_clamp(ix, width);
275
276 piy = wrap_clamp(iy - 1, height);
277 niy = wrap_clamp(iy + 1, height);
278 nniy = wrap_clamp(iy + 2, height);
279 iy = wrap_clamp(iy, height);
280 break;
281 case EXTENSION_MIRROR:
282 pix = wrap_mirror(ix - 1, width);
283 nix = wrap_mirror(ix + 1, width);
284 nnix = wrap_mirror(ix + 2, width);
285 ix = wrap_mirror(ix, width);
286
287 piy = wrap_mirror(iy - 1, height);
288 niy = wrap_mirror(iy + 1, height);
289 nniy = wrap_mirror(iy + 2, height);
290 iy = wrap_mirror(iy, height);
291 break;
292 default:
293 kernel_assert(0);
294 return zero();
295 }
296
297 const ccl_global TexT *data = (const ccl_global TexT *)info.data;
298 const int xc[4] = {pix, ix, nix, nnix};
299 const int yc[4] = {piy, iy, niy, nniy};
300 float u[4], v[4];
301
302 /* Some helper macros to keep code size reasonable.
303 * Lets the compiler inline all the matrix multiplications.
304 */
305#define DATA(x, y) (read_clip(data, xc[x], yc[y], width, height))
306#define TERM(col) \
307 (v[col] * \
308 (u[0] * DATA(0, col) + u[1] * DATA(1, col) + u[2] * DATA(2, col) + u[3] * DATA(3, col)))
309
312
313 /* Actual interpolation. */
314 return TERM(0) + TERM(1) + TERM(2) + TERM(3);
315#undef TERM
316#undef DATA
317 }
318
320 const float x,
321 float y)
322 {
323 switch (info.interpolation) {
325 return interp_closest(info, x, y);
327 return interp_linear(info, x, y);
328 default:
329 return interp_cubic(info, x, y);
330 }
331 }
332};
333
334#undef SET_CUBIC_SPLINE_WEIGHTS
335
337 ccl_private ShaderData *sd,
338 const int image_texture_id,
339 dual2 uv,
340 const float4 missing_rgba)
341{
342 if (image_texture_id == KERNEL_IMAGE_NONE) {
343 return missing_rgba;
344 }
345 const ccl_global KernelImageTexture &tex = kernel_data_fetch(image_textures, image_texture_id);
346 const ccl_global KernelImageInfo *info;
347
348 float2 xy = zero_float2();
349
350 if (tex.tile_descriptor_offset != UINT_MAX) {
351 /* Wrapping. */
352 if (!kernel_image_tile_wrap(ExtensionType(tex.extension), uv.val)) {
353 return zero_float4();
354 }
355
356 /* Tile mapping */
357 const KernelTileDescriptor tile_descriptor = kernel_image_tile_map(
358 kg, sd, tex, image_texture_id, uv, xy);
359
360 if (!kernel_tile_descriptor_loaded(tile_descriptor)) {
361 return (tile_descriptor == KERNEL_TILE_LOAD_FAILED) ? missing_rgba : tex.average_color;
362 }
363
364 info = &kernel_data_fetch(image_info, kernel_tile_descriptor_image_info_id(tile_descriptor));
365 }
366 else {
367 /* Full image sampling. */
368 if (tex.image_info_id == KERNEL_IMAGE_NONE) {
369 return missing_rgba;
370 }
371
372 /* Convert to pixel space. */
373 info = &kernel_data_fetch(image_info, tex.image_info_id);
374 xy = make_float2(uv.val.x * info->width, uv.val.y * info->height);
375 }
376
377 if (UNLIKELY(!info->data)) {
378 return zero_float4();
379 }
380
381 switch (info->data_type) {
383 const float f = ImageInterpolator<half, float>::interp(*info, xy.x, xy.y);
384 return make_float4(f, f, f, 1.0f);
385 }
387 const float f = ImageInterpolator<uchar, float>::interp(*info, xy.x, xy.y);
388 return make_float4(f, f, f, 1.0f);
389 }
391 const float f = ImageInterpolator<uint16_t, float>::interp(*info, xy.x, xy.y);
392 return make_float4(f, f, f, 1.0f);
393 }
395 const float f = ImageInterpolator<float, float>::interp(*info, xy.x, xy.y);
396 return make_float4(f, f, f, 1.0f);
397 }
399 return ImageInterpolator<half4>::interp(*info, xy.x, xy.y);
401 return ImageInterpolator<uchar4>::interp(*info, xy.x, xy.y);
403 return ImageInterpolator<ushort4>::interp(*info, xy.x, xy.y);
405 return ImageInterpolator<float4>::interp(*info, xy.x, xy.y);
406 default:
407 kernel_assert(0);
408 return missing_rgba;
409 }
410}
411
413 ccl_private ShaderData *sd,
414 const int udim_id,
415 dual2 uv,
416 const float4 missing_rgba)
417{
418 const int image_texture_id = kernel_image_udim_map(kg, udim_id, uv.val);
419 if (image_texture_id == KERNEL_IMAGE_NONE) {
420 return missing_rgba;
421 }
422
423 return kernel_image_interp(kg, sd, image_texture_id, uv, missing_rgba);
424}
425
426#ifndef __KERNEL_GPU__
427} /* Namespace. */
428#endif
429
#define UNLIKELY(x)
long long int int64_t
Definition half.h:29
#define TERM(col)
#define SET_CUBIC_SPLINE_WEIGHTS(u, t)
#define kernel_assert(cond)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_always_inline
#define ccl_private
#define ccl_global
#define CCL_NAMESPACE_END
#define abs
constexpr T clamp(T, U, U) RET
unsigned int uchar
ccl_device_inline float4 half4_to_float4_image(const half4 h)
Definition half.h:326
ccl_device_inline float half_to_float_image(half h)
Definition half.h:321
#define UINT_MAX
Definition hash_md5.cc:46
ccl_device_forceinline bool kernel_image_tile_wrap(const ExtensionType extension, ccl_private float2 &uv)
Definition image_2d.h:48
ccl_device_forceinline KernelTileDescriptor kernel_image_tile_map(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_global KernelImageTexture &tex, const uint image_texture_id, const dual2 uv, ccl_private float2 &xy)
Definition image_2d.h:76
CCL_NAMESPACE_BEGIN ccl_device_forceinline int kernel_image_udim_map(KernelGlobals kg, const int id, ccl_private float2 &uv)
Definition image_2d.h:17
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:454
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define KERNEL_TILE_LOAD_FAILED
#define KERNEL_IMAGE_NONE
ccl_device float4 kernel_image_interp(KernelGlobals kg, ccl_private ShaderData *sd, const int image_texture_id, dual2 uv, const float4 missing_rgba)
ccl_device_forceinline float4 kernel_image_interp_with_udim(KernelGlobals kg, ccl_private ShaderData *sd, const int udim_id, dual2 uv, const float4 missing_rgba)
#define ccl_device
#define make_float2
#define make_float4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static ccl_always_inline float4 read(half4 r)
static ccl_always_inline float read(const uint16_t r)
static ccl_always_inline float read(half r)
static ccl_always_inline OutT interp_closest(const ccl_global KernelImageInfo &info, const float x, float y)
static ccl_always_inline float4 read(ushort4 r)
static ccl_always_inline OutT interp_cubic(const ccl_global KernelImageInfo &info, const float x, float y)
static ccl_always_inline float4 read(const float4 r)
static ccl_always_inline OutT interp_linear(const ccl_global KernelImageInfo &info, const float x, float y)
static ccl_always_inline float4 read(const uchar4 r)
static ccl_always_inline OutT interp(const ccl_global KernelImageInfo &info, const float x, float y)
static ccl_always_inline int wrap_mirror(const int x, const int width)
static ccl_always_inline OutT read_clip(const ccl_global TexT *data, const int x, int y, const int width, const int height)
static ccl_always_inline int wrap_periodic(int x, const int width)
static ccl_always_inline OutT read(const ccl_global TexT *data, const int x, int y, const int width, const int)
static ccl_always_inline float frac(const float x, ccl_private int *ix)
static ccl_always_inline float read(const uchar r)
static ccl_always_inline float read(const float r)
static ccl_always_inline int wrap_clamp(const int x, const int width)
float x
float y
Definition half.h:53
uchar z
uchar w
uchar x
uchar y
uint16_t w
uint16_t y
uint16_t z
uint16_t x
dual< float2 > dual2
Definition types_dual.h:105
uint KernelTileDescriptor
@ IMAGE_DATA_TYPE_BYTE
Definition types_image.h:40
@ IMAGE_DATA_TYPE_FLOAT
Definition types_image.h:39
@ IMAGE_DATA_TYPE_FLOAT4
Definition types_image.h:36
@ IMAGE_DATA_TYPE_USHORT4
Definition types_image.h:42
@ IMAGE_DATA_TYPE_USHORT
Definition types_image.h:43
@ IMAGE_DATA_TYPE_HALF
Definition types_image.h:41
@ IMAGE_DATA_TYPE_BYTE4
Definition types_image.h:37
@ IMAGE_DATA_TYPE_HALF4
Definition types_image.h:38
ccl_device_inline bool kernel_tile_descriptor_loaded(const KernelTileDescriptor tile)
@ INTERPOLATION_LINEAR
Definition types_image.h:26
@ INTERPOLATION_CLOSEST
Definition types_image.h:27
ExtensionType
Definition types_image.h:80
@ EXTENSION_REPEAT
Definition types_image.h:82
@ EXTENSION_CLIP
Definition types_image.h:86
@ EXTENSION_EXTEND
Definition types_image.h:84
@ EXTENSION_MIRROR
Definition types_image.h:88
ccl_device_inline uint kernel_tile_descriptor_image_info_id(const KernelTileDescriptor tile)