Blender  V3.3
cycles/kernel/svm/image.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
8 ccl_device float4 svm_image_texture(KernelGlobals kg, int id, float x, float y, uint flags)
9 {
10  if (id == -1) {
11  return make_float4(
13  }
14 
15  float4 r = kernel_tex_image_interp(kg, id, x, y);
16  const float alpha = r.w;
17 
18  if ((flags & NODE_IMAGE_ALPHA_UNASSOCIATE) && alpha != 1.0f && alpha != 0.0f) {
19  r /= alpha;
20  r.w = alpha;
21  }
22 
23  if (flags & NODE_IMAGE_COMPRESS_AS_SRGB) {
25  }
26 
27  return r;
28 }
29 
30 /* Remap coordinate from 0..1 box to -1..-1 */
32 {
33  return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
34 }
35 
37  KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
38 {
39  uint co_offset, out_offset, alpha_offset, flags;
40 
41  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
42 
43  float3 co = stack_load_float3(stack, co_offset);
44  float2 tex_co;
45  if (node.w == NODE_IMAGE_PROJ_SPHERE) {
46  co = texco_remap_square(co);
47  tex_co = map_to_sphere(co);
48  }
49  else if (node.w == NODE_IMAGE_PROJ_TUBE) {
50  co = texco_remap_square(co);
51  tex_co = map_to_tube(co);
52  }
53  else {
54  tex_co = make_float2(co.x, co.y);
55  }
56 
57  /* TODO(lukas): Consider moving tile information out of the SVM node.
58  * TextureInfo seems a reasonable candidate. */
59  int id = -1;
60  int num_nodes = (int)node.y;
61  if (num_nodes > 0) {
62  /* Remember the offset of the node following the tile nodes. */
63  int next_offset = offset + num_nodes;
64 
65  /* Find the tile that the UV lies in. */
66  int tx = (int)tex_co.x;
67  int ty = (int)tex_co.y;
68 
69  /* Check that we're within a legitimate tile. */
70  if (tx >= 0 && ty >= 0 && tx < 10) {
71  int tile = 1001 + 10 * ty + tx;
72 
73  /* Find the index of the tile. */
74  for (int i = 0; i < num_nodes; i++) {
75  uint4 tile_node = read_node(kg, &offset);
76  if (tile_node.x == tile) {
77  id = tile_node.y;
78  break;
79  }
80  if (tile_node.z == tile) {
81  id = tile_node.w;
82  break;
83  }
84  }
85 
86  /* If we found the tile, offset the UVs to be relative to it. */
87  if (id != -1) {
88  tex_co.x -= tx;
89  tex_co.y -= ty;
90  }
91  }
92 
93  /* Skip over the remaining nodes. */
94  offset = next_offset;
95  }
96  else {
97  id = -num_nodes;
98  }
99 
100  float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, flags);
101 
102  if (stack_valid(out_offset))
103  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
104  if (stack_valid(alpha_offset))
105  stack_store_float(stack, alpha_offset, f.w);
106  return offset;
107 }
108 
111  ccl_private float *stack,
112  uint4 node)
113 {
114  /* get object space normal */
115  float3 N = sd->N;
116 
117  N = sd->N;
119 
120  /* project from direction vector to barycentric coordinates in triangles */
121  float3 signed_N = N;
122 
123  N.x = fabsf(N.x);
124  N.y = fabsf(N.y);
125  N.z = fabsf(N.z);
126 
127  N /= (N.x + N.y + N.z);
128 
129  /* basic idea is to think of this as a triangle, each corner representing
130  * one of the 3 faces of the cube. in the corners we have single textures,
131  * in between we blend between two textures, and in the middle we a blend
132  * between three textures.
133  *
134  * The `Nxyz` values are the barycentric coordinates in an equilateral
135  * triangle, which in case of blending, in the middle has a smaller
136  * equilateral triangle where 3 textures blend. this divides things into
137  * 7 zones, with an if() test for each zone. */
138 
139  float3 weight = make_float3(0.0f, 0.0f, 0.0f);
140  float blend = __int_as_float(node.w);
141  float limit = 0.5f * (1.0f + blend);
142 
143  /* first test for corners with single texture */
144  if (N.x > limit * (N.x + N.y) && N.x > limit * (N.x + N.z)) {
145  weight.x = 1.0f;
146  }
147  else if (N.y > limit * (N.x + N.y) && N.y > limit * (N.y + N.z)) {
148  weight.y = 1.0f;
149  }
150  else if (N.z > limit * (N.x + N.z) && N.z > limit * (N.y + N.z)) {
151  weight.z = 1.0f;
152  }
153  else if (blend > 0.0f) {
154  /* in case of blending, test for mixes between two textures */
155  if (N.z < (1.0f - limit) * (N.y + N.x)) {
156  weight.x = N.x / (N.x + N.y);
157  weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
158  weight.y = 1.0f - weight.x;
159  }
160  else if (N.x < (1.0f - limit) * (N.y + N.z)) {
161  weight.y = N.y / (N.y + N.z);
162  weight.y = saturatef((weight.y - 0.5f * (1.0f - blend)) / blend);
163  weight.z = 1.0f - weight.y;
164  }
165  else if (N.y < (1.0f - limit) * (N.x + N.z)) {
166  weight.x = N.x / (N.x + N.z);
167  weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
168  weight.z = 1.0f - weight.x;
169  }
170  else {
171  /* last case, we have a mix between three */
172  weight.x = ((2.0f - limit) * N.x + (limit - 1.0f)) / (2.0f * limit - 1.0f);
173  weight.y = ((2.0f - limit) * N.y + (limit - 1.0f)) / (2.0f * limit - 1.0f);
174  weight.z = ((2.0f - limit) * N.z + (limit - 1.0f)) / (2.0f * limit - 1.0f);
175  }
176  }
177  else {
178  /* Desperate mode, no valid choice anyway, fallback to one side. */
179  weight.x = 1.0f;
180  }
181 
182  /* now fetch textures */
183  uint co_offset, out_offset, alpha_offset, flags;
184  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
185 
186  float3 co = stack_load_float3(stack, co_offset);
187  uint id = node.y;
188 
189  float4 f = zero_float4();
190 
191  /* Map so that no textures are flipped, rotation is somewhat arbitrary. */
192  if (weight.x > 0.0f) {
193  float2 uv = make_float2((signed_N.x < 0.0f) ? 1.0f - co.y : co.y, co.z);
194  f += weight.x * svm_image_texture(kg, id, uv.x, uv.y, flags);
195  }
196  if (weight.y > 0.0f) {
197  float2 uv = make_float2((signed_N.y > 0.0f) ? 1.0f - co.x : co.x, co.z);
198  f += weight.y * svm_image_texture(kg, id, uv.x, uv.y, flags);
199  }
200  if (weight.z > 0.0f) {
201  float2 uv = make_float2((signed_N.z > 0.0f) ? 1.0f - co.y : co.y, co.x);
202  f += weight.z * svm_image_texture(kg, id, uv.x, uv.y, flags);
203  }
204 
205  if (stack_valid(out_offset))
206  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
207  if (stack_valid(alpha_offset))
208  stack_store_float(stack, alpha_offset, f.w);
209 }
210 
213  ccl_private float *stack,
214  uint4 node)
215 {
216  uint id = node.y;
217  uint co_offset, out_offset, alpha_offset, flags;
218  uint projection = node.w;
219 
220  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
221 
222  float3 co = stack_load_float3(stack, co_offset);
223  float2 uv;
224 
225  co = safe_normalize(co);
226 
227  if (projection == 0)
229  else
230  uv = direction_to_mirrorball(co);
231 
232  float4 f = svm_image_texture(kg, id, uv.x, uv.y, flags);
233 
234  if (stack_valid(out_offset))
235  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
236  if (stack_valid(alpha_offset))
237  stack_store_float(stack, alpha_offset, f.w);
238 }
239 
void map_to_tube(float *r_u, float *r_v, float x, float y, float z)
Definition: math_geom.c:4917
void map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
Definition: math_geom.c:4932
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
float float4[4]
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define ccl_device_noinline
Definition: cuda/compat.h:40
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ccl_device float2 direction_to_mirrorball(float3 dir)
ccl_device float2 direction_to_equirectangular(float3 dir)
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)
OperationNode * node
const KernelGlobalsCPU *ccl_restrict KernelGlobals
ccl_global const KernelWorkTile * tile
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
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
Definition: kernel/types.h:925
ccl_device_inline float2 safe_normalize(const float2 &a)
Definition: math_float2.h:201
ccl_device_inline float4 zero_float4()
Definition: math_float4.h:92
#define N
#define make_float2(x, y)
Definition: metal/compat.h:203
#define make_float4(x, y, z, w)
Definition: metal/compat.h:205
#define fabsf(x)
Definition: metal/compat.h:219
#define make_float3(x, y, z)
Definition: metal/compat.h:204
ccl_device float4 kernel_tex_image_interp(KernelGlobals kg, int id, float x, float y)
float x
Definition: types_float2.h:15
float y
Definition: types_float2.h:15
float z
float y
float x
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
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
ccl_device float4 color_srgb_to_linear_v4(float4 c)
Definition: util/color.h:302
ccl_device_inline float saturatef(float a)
Definition: util/math.h:404
ccl_device_inline float __int_as_float(int i)
Definition: util/math.h:253
#define TEX_IMAGE_MISSING_R
Definition: util/texture.h:12
#define TEX_IMAGE_MISSING_B
Definition: util/texture.h:14
#define TEX_IMAGE_MISSING_A
Definition: util/texture.h:15
#define TEX_IMAGE_MISSING_G
Definition: util/texture.h:13