Blender V5.3
cycles/kernel/device/gpu/image.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
9#include "util/defines.h"
10#include "util/types_image.h"
11
13
14ccl_device_inline float frac(const float x, ccl_private int *ix)
15{
16 int i = float_to_int(x) - ((x < 0.0f) ? 1 : 0);
17 *ix = i;
18 return x - (float)i;
19}
20
21/* w0, w1, w2, and w3 are the four cubic B-spline basis functions. */
22ccl_device float cubic_w0(const float a)
23{
24 return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f);
25}
26ccl_device float cubic_w1(const float a)
27{
28 return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
29}
30ccl_device float cubic_w2(const float a)
31{
32 return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
33}
34ccl_device float cubic_w3(const float a)
35{
36 return (1.0f / 6.0f) * (a * a * a);
37}
38
39/* g0 and g1 are the two amplitude functions. */
40ccl_device float cubic_g0(const float a)
41{
42 return cubic_w0(a) + cubic_w1(a);
43}
44ccl_device float cubic_g1(const float a)
45{
46 return cubic_w2(a) + cubic_w3(a);
47}
48
49/* h0 and h1 are the two offset functions */
50ccl_device float cubic_h0(const float a)
51{
52 return (cubic_w1(a) / cubic_g0(a)) - 1.0f;
53}
54ccl_device float cubic_h1(const float a)
55{
56 return (cubic_w3(a) / cubic_g1(a)) + 1.0f;
57}
58
59/* Fast bicubic texture lookup using 4 bilinear lookups, adapted from CUDA samples. */
60template<typename T>
61/* Workaround for an apparent compiler bug with CUDA 12.8 and Blackwell.
62 * This appears to be fixed with CUDA 12.9, so this workaround can be removed
63 * when the minimum toolkit version is increased. */
64#if defined(__KERNEL_CUDA__) && (__CUDA_ARCH__ >= 1000)
66#else
68#endif
70{
72
73 const float x = (uv.x * (float)info.width) - 0.5f;
74 const float y = (uv.y * (float)info.height) - 0.5f;
75
76 const float px = floorf(x);
77 const float py = floorf(y);
78 const float fx = x - px;
79 const float fy = y - py;
80
81 const float g0x = cubic_g0(fx);
82 const float g1x = cubic_g1(fx);
83 /* Note +0.5 offset to compensate for CUDA linear filtering convention. */
84 const float x0 = (px + cubic_h0(fx) + 0.5f) * info.inv_width;
85 const float x1 = (px + cubic_h1(fx) + 0.5f) * info.inv_width;
86 const float y0 = (py + cubic_h0(fy) + 0.5f) * info.inv_height;
87 const float y1 = (py + cubic_h1(fy) + 0.5f) * info.inv_height;
88
89 return cubic_g0(fy) * (g0x * ccl_gpu_image_object_read_2D<T>(tex, x0, y0) +
90 g1x * ccl_gpu_image_object_read_2D<T>(tex, x1, y0)) +
91 cubic_g1(fy) * (g0x * ccl_gpu_image_object_read_2D<T>(tex, x0, y1) +
92 g1x * ccl_gpu_image_object_read_2D<T>(tex, x1, y1));
93}
94
96 ccl_private ShaderData *sd,
97 const int image_texture_id,
98 dual2 uv,
99 const float4 missing_rgba)
100{
101 if (image_texture_id == KERNEL_IMAGE_NONE) {
102 return missing_rgba;
103 }
104 const ccl_global KernelImageTexture &tex = kernel_data_fetch(image_textures, image_texture_id);
105 const ccl_global KernelImageInfo *info;
106
107 float2 sample_uv;
108
109 if (tex.tile_descriptor_offset != KERNEL_TILE_LOAD_NONE) {
110 /* Wrapping. */
111 if (!kernel_image_tile_wrap(ExtensionType(tex.extension), uv.val)) {
112 return zero_float4();
113 }
114
115 /* Tile mapping */
116 float2 xy = zero_float2();
117 const KernelTileDescriptor tile_descriptor = kernel_image_tile_map(
118 kg, sd, tex, image_texture_id, uv, xy);
119
120 if (!kernel_tile_descriptor_loaded(tile_descriptor)) {
121 return (tile_descriptor == KERNEL_TILE_LOAD_FAILED) ? missing_rgba : tex.average_color;
122 }
123
124 info = &kernel_data_fetch(image_info, kernel_tile_descriptor_image_info_id(tile_descriptor));
125
126 /* Convert to normalized space again. */
127 sample_uv = make_float2(xy.x * info->inv_width, xy.y * info->inv_height);
128 }
129 else {
130 /* Full image sampling. */
131 if (tex.image_info_id == KERNEL_IMAGE_NONE) {
132 return missing_rgba;
133 }
134
135 info = &kernel_data_fetch(image_info, tex.image_info_id);
136 sample_uv = uv.val;
137 }
138
139 /* float4, byte4, ushort4 and half4 */
140 const int texture_type = info->data_type;
141 if (texture_type == IMAGE_DATA_TYPE_FLOAT4 || texture_type == IMAGE_DATA_TYPE_BYTE4 ||
142 texture_type == IMAGE_DATA_TYPE_HALF4 || texture_type == IMAGE_DATA_TYPE_USHORT4)
143 {
144 if (info->interpolation == INTERPOLATION_CUBIC || info->interpolation == INTERPOLATION_SMART) {
145 return kernel_image_interp_bicubic<float4>(*info, sample_uv);
146 }
147 else {
149 return ccl_gpu_image_object_read_2D<float4>(tex, sample_uv.x, sample_uv.y);
150 }
151 }
152 /* float, byte and half */
153 else {
154 float f;
155
156 if (info->interpolation == INTERPOLATION_CUBIC || info->interpolation == INTERPOLATION_SMART) {
157 f = kernel_image_interp_bicubic<float>(*info, sample_uv);
158 }
159 else {
161 f = ccl_gpu_image_object_read_2D<float>(tex, sample_uv.x, sample_uv.y);
162 }
163
164 return make_float4(f, f, f, 1.0f);
165 }
166}
167
169 ccl_private ShaderData *sd,
170 const int udim_id,
171 dual2 uv,
172 const float4 missing_rgba)
173{
174 const int image_texture_id = kernel_image_udim_map(kg, udim_id, uv.val);
175 if (image_texture_id == KERNEL_IMAGE_NONE) {
176 return missing_rgba;
177 }
178
179 return kernel_image_interp(kg, sd, image_texture_id, uv, missing_rgba);
180}
181
ccl_device float cubic_g0(const float a)
ccl_device_noinline T kernel_image_interp_bicubic(const ccl_global KernelImageInfo &info, const float2 uv)
ccl_device float cubic_h1(const float a)
ccl_device float cubic_w3(const float a)
CCL_NAMESPACE_BEGIN ccl_device_inline float frac(const float x, ccl_private int *ix)
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)
ccl_device float cubic_g1(const float a)
ccl_device float cubic_w2(const float a)
ccl_device float cubic_h0(const float a)
ccl_device float cubic_w0(const float a)
ccl_device float cubic_w1(const float a)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
CUtexObject ccl_gpu_image_object_2D
#define CCL_NAMESPACE_END
ccl_device_forceinline T ccl_gpu_image_object_read_2D(const ccl_gpu_image_object_2D texobj, const float x, const float y)
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 T
#define KERNEL_TILE_LOAD_NONE
#define KERNEL_TILE_LOAD_FAILED
#define KERNEL_IMAGE_NONE
#define floorf
#define ccl_device
#define make_float2
#define make_float4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
dual< float2 > dual2
Definition types_dual.h:105
uint KernelTileDescriptor
@ IMAGE_DATA_TYPE_FLOAT4
Definition types_image.h:36
@ IMAGE_DATA_TYPE_USHORT4
Definition types_image.h:42
@ 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_SMART
Definition types_image.h:29
@ INTERPOLATION_CUBIC
Definition types_image.h:28
ExtensionType
Definition types_image.h:80
ccl_device_inline uint kernel_tile_descriptor_image_info_id(const KernelTileDescriptor tile)