Blender V4.3
cycles/kernel/svm/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
9ccl_device float4 svm_image_texture(KernelGlobals kg, int id, float x, float y, uint flags)
10{
11 if (id == -1) {
12 return make_float4(
14 }
15
16 float4 r = kernel_tex_image_interp(kg, id, x, y);
17 const float alpha = r.w;
18
19 if ((flags & NODE_IMAGE_ALPHA_UNASSOCIATE) && alpha != 1.0f && alpha != 0.0f) {
20 r /= alpha;
21 r.w = alpha;
22 }
23
24 if (flags & NODE_IMAGE_COMPRESS_AS_SRGB) {
26 }
27
28 return r;
29}
30
31/* Remap coordinate from 0..1 box to -1..-1 */
33{
34 return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
35}
36
38 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
39{
40 uint co_offset, out_offset, alpha_offset, flags;
41
42 svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
43
44 float3 co = stack_load_float3(stack, co_offset);
45 float2 tex_co;
46 if (node.w == NODE_IMAGE_PROJ_SPHERE) {
47 co = texco_remap_square(co);
48 tex_co = map_to_sphere(co);
49 }
50 else if (node.w == NODE_IMAGE_PROJ_TUBE) {
51 co = texco_remap_square(co);
52 tex_co = map_to_tube(co);
53 }
54 else {
55 tex_co = make_float2(co.x, co.y);
56 }
57
58 /* TODO(lukas): Consider moving tile information out of the SVM node.
59 * TextureInfo seems a reasonable candidate. */
60 int id = -1;
61 int num_nodes = (int)node.y;
62 if (num_nodes > 0) {
63 /* Remember the offset of the node following the tile nodes. */
64 int next_offset = offset + num_nodes;
65
66 /* Find the tile that the UV lies in. */
67 int tx = (int)tex_co.x;
68 int ty = (int)tex_co.y;
69
70 /* Check that we're within a legitimate tile. */
71 if (tx >= 0 && ty >= 0 && tx < 10) {
72 int tile = 1001 + 10 * ty + tx;
73
74 /* Find the index of the tile. */
75 for (int i = 0; i < num_nodes; i++) {
76 uint4 tile_node = read_node(kg, &offset);
77 if (tile_node.x == tile) {
78 id = tile_node.y;
79 break;
80 }
81 if (tile_node.z == tile) {
82 id = tile_node.w;
83 break;
84 }
85 }
86
87 /* If we found the tile, offset the UVs to be relative to it. */
88 if (id != -1) {
89 tex_co.x -= tx;
90 tex_co.y -= ty;
91 }
92 }
93
94 /* Skip over the remaining nodes. */
95 offset = next_offset;
96 }
97 else {
98 id = -num_nodes;
99 }
100
101 float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, flags);
102
103 if (stack_valid(out_offset))
104 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
105 if (stack_valid(alpha_offset))
106 stack_store_float(stack, alpha_offset, f.w);
107 return offset;
108}
109
112 ccl_private float *stack,
113 uint4 node)
114{
115 /* get object space normal */
116 float3 N = sd->N;
117
118 N = sd->N;
120
121 /* project from direction vector to barycentric coordinates in triangles */
122 float3 signed_N = N;
123
124 N.x = fabsf(N.x);
125 N.y = fabsf(N.y);
126 N.z = fabsf(N.z);
127
128 N /= (N.x + N.y + N.z);
129
130 /* basic idea is to think of this as a triangle, each corner representing
131 * one of the 3 faces of the cube. in the corners we have single textures,
132 * in between we blend between two textures, and in the middle we a blend
133 * between three textures.
134 *
135 * The `Nxyz` values are the barycentric coordinates in an equilateral
136 * triangle, which in case of blending, in the middle has a smaller
137 * equilateral triangle where 3 textures blend. this divides things into
138 * 7 zones, with an if() test for each zone. */
139
140 float3 weight = make_float3(0.0f, 0.0f, 0.0f);
141 float blend = __int_as_float(node.w);
142 float limit = 0.5f * (1.0f + blend);
143
144 /* first test for corners with single texture */
145 if (N.x > limit * (N.x + N.y) && N.x > limit * (N.x + N.z)) {
146 weight.x = 1.0f;
147 }
148 else if (N.y > limit * (N.x + N.y) && N.y > limit * (N.y + N.z)) {
149 weight.y = 1.0f;
150 }
151 else if (N.z > limit * (N.x + N.z) && N.z > limit * (N.y + N.z)) {
152 weight.z = 1.0f;
153 }
154 else if (blend > 0.0f) {
155 /* in case of blending, test for mixes between two textures */
156 if (N.z < (1.0f - limit) * (N.y + N.x)) {
157 weight.x = N.x / (N.x + N.y);
158 weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
159 weight.y = 1.0f - weight.x;
160 }
161 else if (N.x < (1.0f - limit) * (N.y + N.z)) {
162 weight.y = N.y / (N.y + N.z);
163 weight.y = saturatef((weight.y - 0.5f * (1.0f - blend)) / blend);
164 weight.z = 1.0f - weight.y;
165 }
166 else if (N.y < (1.0f - limit) * (N.x + N.z)) {
167 weight.x = N.x / (N.x + N.z);
168 weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
169 weight.z = 1.0f - weight.x;
170 }
171 else {
172 /* last case, we have a mix between three */
173 weight.x = ((2.0f - limit) * N.x + (limit - 1.0f)) / (2.0f * limit - 1.0f);
174 weight.y = ((2.0f - limit) * N.y + (limit - 1.0f)) / (2.0f * limit - 1.0f);
175 weight.z = ((2.0f - limit) * N.z + (limit - 1.0f)) / (2.0f * limit - 1.0f);
176 }
177 }
178 else {
179 /* Desperate mode, no valid choice anyway, fallback to one side. */
180 weight.x = 1.0f;
181 }
182
183 /* now fetch textures */
184 uint co_offset, out_offset, alpha_offset, flags;
185 svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
186
187 float3 co = stack_load_float3(stack, co_offset);
188 uint id = node.y;
189
190 float4 f = zero_float4();
191
192 /* Map so that no textures are flipped, rotation is somewhat arbitrary. */
193 if (weight.x > 0.0f) {
194 float2 uv = make_float2((signed_N.x < 0.0f) ? 1.0f - co.y : co.y, co.z);
195 f += weight.x * svm_image_texture(kg, id, uv.x, uv.y, flags);
196 }
197 if (weight.y > 0.0f) {
198 float2 uv = make_float2((signed_N.y > 0.0f) ? 1.0f - co.x : co.x, co.z);
199 f += weight.y * svm_image_texture(kg, id, uv.x, uv.y, flags);
200 }
201 if (weight.z > 0.0f) {
202 float2 uv = make_float2((signed_N.z > 0.0f) ? 1.0f - co.y : co.y, co.x);
203 f += weight.z * svm_image_texture(kg, id, uv.x, uv.y, flags);
204 }
205
206 if (stack_valid(out_offset))
207 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
208 if (stack_valid(alpha_offset))
209 stack_store_float(stack, alpha_offset, f.w);
210}
211
214 ccl_private float *stack,
215 uint4 node)
216{
217 uint id = node.y;
218 uint co_offset, out_offset, alpha_offset, flags;
219 uint projection = node.w;
220
221 svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
222
223 float3 co = stack_load_float3(stack, co_offset);
224 float2 uv;
225
226 co = safe_normalize(co);
227
228 if (projection == 0)
230 else
232
233 float4 f = svm_image_texture(kg, id, uv.x, uv.y, flags);
234
235 if (stack_valid(out_offset))
236 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
237 if (stack_valid(alpha_offset))
238 stack_store_float(stack, alpha_offset, f.w);
239}
240
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
bool map_to_tube(float *r_u, float *r_v, float x, float y, float z)
unsigned int uint
ccl_device float2 direction_to_mirrorball(float3 dir)
ccl_device float2 direction_to_equirectangular(float3 dir)
ccl_device float4 kernel_tex_image_interp(KernelGlobals kg, int id, float x, float y)
CCL_NAMESPACE_BEGIN ccl_device float4 svm_image_texture(KernelGlobals kg, int id, float x, float y, uint flags)
ccl_device_noinline void svm_node_tex_image_box(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node)
ccl_device_noinline void svm_node_tex_environment(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node)
ccl_device_noinline int svm_node_tex_image(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
ccl_device_inline float3 texco_remap_square(float3 co)
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float4 make_float4(const float x, const float y, const float z, const float w)
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline float2 make_float2(const float x, const float y)
#define __int_as_float(x)
#define fabsf(x)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
ccl_global const KernelWorkTile * tile
ccl_device_inline void object_inverse_normal_transform(KernelGlobals kg, ccl_private const ShaderData *sd, ccl_private float3 *N)
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
@ NODE_IMAGE_COMPRESS_AS_SRGB
@ NODE_IMAGE_ALPHA_UNASSOCIATE
@ NODE_IMAGE_PROJ_SPHERE
@ NODE_IMAGE_PROJ_TUBE
ShaderData
ccl_device_inline float2 safe_normalize(const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:15
#define N
float x
float y
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
uint x
Definition types_uint4.h:15
uint y
Definition types_uint4.h:15
uint z
Definition types_uint4.h:15
uint w
Definition types_uint4.h:15
ccl_device float4 color_srgb_to_linear_v4(float4 c)
Definition util/color.h:327
#define TEX_IMAGE_MISSING_R
#define TEX_IMAGE_MISSING_B
#define TEX_IMAGE_MISSING_A
#define TEX_IMAGE_MISSING_G