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