Blender V5.3
image_2d.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 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/atomic.h"
11#include "util/defines.h"
12#include "util/math_fast.h"
13#include "util/types_image.h"
14
16
18 const int id,
20{
21 if (id >= 0) {
22 return id;
23 }
24
25 const int tx = (int)uv.x;
26 const int ty = (int)uv.y;
27 if (tx < 0 || ty < 0 || tx >= 10) {
28 return KERNEL_IMAGE_NONE;
29 }
30
31 int udim_id = -id - 1;
32 const int num_udims = kernel_data_fetch(image_texture_udims, udim_id++).tile;
33 const int tile = 1001 + 10 * ty + tx;
34
35 for (int i = 0; i < num_udims; i++) {
36 const KernelImageUDIM udim = kernel_data_fetch(image_texture_udims, udim_id++);
37 if (udim.tile == tile) {
38 /* If we found the tile, offset the UVs to be relative to it. */
39 uv.x -= tx;
40 uv.y -= ty;
41 return udim.image_texture_id;
42 }
43 }
44
45 return KERNEL_IMAGE_NONE;
46}
47
50{
51 /* Wrapping. */
52 switch (extension) {
54 uv = uv - floor(uv);
55 return true;
56 case EXTENSION_CLIP:
57 return (uv.x >= 0.0f && uv.x <= 1.0f && uv.y >= 0.0f && uv.y <= 1.0f);
59 uv = clamp(uv, zero_float2(), one_float2());
60 return true;
61 case EXTENSION_MIRROR: {
62 const float2 t = uv * 0.5f;
63 uv = 2.0f * (t - floor(t));
64 uv = select(uv >= one_float2(), 2.0f * one_float2() - uv, uv);
65 return true;
66 }
67 default:
68 break;
69 }
70
71 return false;
72}
73
74/* From UV coordinates in 0..1 range, compute tile and pixel coordinates. */
77 ccl_private ShaderData *sd,
79 const uint image_texture_id,
80 const dual2 uv,
82{
83 /* Find mipmap level. Use squared lengths to avoid two sqrt operations,
84 * compensating with 0.5 factor on the log2. Square as float to avoid
85 * integer overflow. */
86 const float dudxy_sq = len_squared(make_float2(uv.dx.x, uv.dy.x)) * sqr(float(tex.width));
87 const float dvdxy_sq = len_squared(make_float2(uv.dx.y, uv.dy.y)) * sqr(float(tex.height));
88
89 /* Limit max anisotropy ratio, to avoid loading too high mip resolutions
90 * for stretched UV coordinates, which don't really benefit from it anyway. */
91 const float maxdxy_sq = max(dudxy_sq, dvdxy_sq);
92 const float mindxy_sq = min(dudxy_sq, dvdxy_sq);
93 const float inv_aniso_ratio_sq = 1.0f / (16.0f * 16.0f);
94 /* Native log2 is faster on GPU. */
95#ifdef __KERNEL_GPU__
96 float flevel = 0.5f * log2(max(mindxy_sq, maxdxy_sq * inv_aniso_ratio_sq));
97#else
98 float flevel = 0.5f * fast_log2f(max(mindxy_sq, maxdxy_sq * inv_aniso_ratio_sq));
99#endif
100
101 /* Select mipmap level. */
102 if (sd->lcg_state != 0) {
103 /* For rounding instead of flooring. */
104 flevel += 0.5f;
105 /* Randomize mip level, except for some cases like displacement or importance map. */
106 const float transition = 0.5f;
107 flevel += (lcg_step_float(&sd->lcg_state) - 0.5f) * transition;
108 }
109 else {
110 /* When not using stochastic interpolation, round to higher level. */
111 }
112 flevel += kernel_data.image.mip_bias;
113 const int level = clamp(int(flevel), 0, tex.tile_levels - 1);
114
115 /* Compute width of this mipmap level. */
116 const int width = max(1, tex.width >> level);
117 const int height = max(1, tex.height >> level);
118
119 /* Convert coordinates to pixel space.
120 * Flip Y convention for tiles to match tx files. */
121 xy = make_float2(uv.val.x * width, (1.0f - uv.val.y) * height);
122
123 /* Tile mapping */
124 const int ix = clamp((int)xy.x, 0, width - 1);
125 const int iy = clamp((int)xy.y, 0, height - 1);
126 const int tile_size_shift = tex.tile_size_shift;
127 const int tile_size_padded = (1 << tile_size_shift) + KERNEL_IMAGE_TEX_PADDING * 2;
128
129 const int tile_x = ix >> tile_size_shift;
130 const int tile_y = iy >> tile_size_shift;
131 const int tile_offset = kernel_data_fetch(image_texture_tile_descriptors,
132 tex.tile_descriptor_offset + level) +
133 tile_x + tile_y * divide_up_by_shift(width, tile_size_shift);
134
135 KernelTileDescriptor tile_descriptor = kernel_data_fetch(
136 image_texture_tile_descriptors, tex.tile_descriptor_offset + tile_offset);
137
138 const uint access_index = tex.tile_descriptor_offset + tile_offset;
139
140 if (!kernel_tile_descriptor_loaded(tile_descriptor)) {
141#ifdef __KERNEL_GPU__
142 /* For GPU, mark load requested and cancel shader execution. */
143 if (tile_descriptor == KERNEL_TILE_LOAD_NONE) {
144 tile_descriptor = KERNEL_TILE_LOAD_REQUEST;
145 /* Write load request for quick rejection of subsequent reads. */
146 kernel_data_write(image_texture_tile_descriptors,
147 tex.tile_descriptor_offset + tile_offset,
148 tile_descriptor);
149 /* Set access state that will be read back to host. Using a byte
150 * instead of a bitmask avoids the need for atomics. */
152 image_texture_tile_access_state)[access_index] = KERNEL_TILE_ACCESS_REQUESTED;
153 }
154 if (tile_descriptor == KERNEL_TILE_LOAD_REQUEST) {
155 sd->runtime_flag |= SR_CACHE_MISS;
156 }
157 return tile_descriptor;
158#else
159 /* For CPU, load tile immediately. */
160 if (tile_descriptor != KERNEL_TILE_LOAD_FAILED) {
161 KernelTileDescriptor &p_tile_descriptor =
162 kg->image_texture_tile_descriptors.data[access_index];
163 kg->image_load_requested_cpu(image_texture_id,
164 level,
165 tile_x << tile_size_shift,
166 tile_y << tile_size_shift,
167 p_tile_descriptor);
168 tile_descriptor = p_tile_descriptor;
169 }
170 if (!kernel_tile_descriptor_loaded(tile_descriptor)) {
171 return tile_descriptor;
172 }
173#endif
174 }
175
176 /* Mark tile as used for cache eviction tracking. Read before writing as we
177 * expect most of the time this was already written. */
178 if (kernel_data_array(image_texture_tile_access_state)[access_index] != KERNEL_TILE_ACCESS_USED)
179 {
180 kernel_data_array(image_texture_tile_access_state)[access_index] = KERNEL_TILE_ACCESS_USED;
181 }
182
183 /* Remap coordinates into tiled image space. */
184 const int offset = kernel_tile_descriptor_offset(tile_descriptor);
185 xy += make_float2(KERNEL_IMAGE_TEX_PADDING - (tile_x << tile_size_shift) +
186 offset * tile_size_padded,
187 KERNEL_IMAGE_TEX_PADDING - (tile_y << tile_size_shift));
188
189 return tile_descriptor;
190}
191
btScalar max
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_global
#define kernel_data_array(name)
#define kernel_data_write(name, index, value)
#define CCL_NAMESPACE_END
#define log2
#define select(A, B, C)
#define floor
constexpr T clamp(T, U, U) RET
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
const ccl_global KernelWorkTile * tile
@ SR_CACHE_MISS
ccl_device float lcg_step_float(T rng)
Definition lcg.h:22
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
ccl_device float fast_log2f(float x)
Definition math_fast.h:353
ccl_device_inline float2 one_float2()
Definition math_float2.h:18
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
#define KERNEL_TILE_LOAD_NONE
#define KERNEL_TILE_LOAD_FAILED
#define KERNEL_TILE_LOAD_REQUEST
#define KERNEL_IMAGE_TEX_PADDING
#define KERNEL_IMAGE_NONE
#define KERNEL_TILE_ACCESS_USED
#define KERNEL_TILE_ACCESS_REQUESTED
#define sqr
#define make_float2
#define min(a, b)
Definition sort.cc:38
float x
float y
ccl_device_inline size_t divide_up_by_shift(const size_t x, const size_t y)
Definition types_base.h:60
dual< float2 > dual2
Definition types_dual.h:105
uint KernelTileDescriptor
ccl_device_inline bool kernel_tile_descriptor_loaded(const KernelTileDescriptor tile)
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_offset(const KernelTileDescriptor tile)