Blender  V2.93
svm_image.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint flags)
20 {
21  if (id == -1) {
22  return make_float4(
24  }
25 
26  float4 r = kernel_tex_image_interp(kg, id, x, y);
27  const float alpha = r.w;
28 
29  if ((flags & NODE_IMAGE_ALPHA_UNASSOCIATE) && alpha != 1.0f && alpha != 0.0f) {
30  r /= alpha;
31  r.w = alpha;
32  }
33 
34  if (flags & NODE_IMAGE_COMPRESS_AS_SRGB) {
36  }
37 
38  return r;
39 }
40 
41 /* Remap coordinate from 0..1 box to -1..-1 */
43 {
44  return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
45 }
46 
48  KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
49 {
50  uint co_offset, out_offset, alpha_offset, flags;
51 
52  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
53 
54  float3 co = stack_load_float3(stack, co_offset);
55  float2 tex_co;
56  if (node.w == NODE_IMAGE_PROJ_SPHERE) {
57  co = texco_remap_square(co);
58  tex_co = map_to_sphere(co);
59  }
60  else if (node.w == NODE_IMAGE_PROJ_TUBE) {
61  co = texco_remap_square(co);
62  tex_co = map_to_tube(co);
63  }
64  else {
65  tex_co = make_float2(co.x, co.y);
66  }
67 
68  /* TODO(lukas): Consider moving tile information out of the SVM node.
69  * TextureInfo seems a reasonable candidate. */
70  int id = -1;
71  int num_nodes = (int)node.y;
72  if (num_nodes > 0) {
73  /* Remember the offset of the node following the tile nodes. */
74  int next_offset = (*offset) + num_nodes;
75 
76  /* Find the tile that the UV lies in. */
77  int tx = (int)tex_co.x;
78  int ty = (int)tex_co.y;
79 
80  /* Check that we're within a legitimate tile. */
81  if (tx >= 0 && ty >= 0 && tx < 10) {
82  int tile = 1001 + 10 * ty + tx;
83 
84  /* Find the index of the tile. */
85  for (int i = 0; i < num_nodes; i++) {
86  uint4 tile_node = read_node(kg, offset);
87  if (tile_node.x == tile) {
88  id = tile_node.y;
89  break;
90  }
91  if (tile_node.z == tile) {
92  id = tile_node.w;
93  break;
94  }
95  }
96 
97  /* If we found the tile, offset the UVs to be relative to it. */
98  if (id != -1) {
99  tex_co.x -= tx;
100  tex_co.y -= ty;
101  }
102  }
103 
104  /* Skip over the remaining nodes. */
105  *offset = next_offset;
106  }
107  else {
108  id = -num_nodes;
109  }
110 
111  float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, flags);
112 
113  if (stack_valid(out_offset))
114  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
115  if (stack_valid(alpha_offset))
116  stack_store_float(stack, alpha_offset, f.w);
117 }
118 
119 ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
120 {
121  /* get object space normal */
122  float3 N = sd->N;
123 
124  N = sd->N;
126 
127  /* project from direction vector to barycentric coordinates in triangles */
128  float3 signed_N = N;
129 
130  N.x = fabsf(N.x);
131  N.y = fabsf(N.y);
132  N.z = fabsf(N.z);
133 
134  N /= (N.x + N.y + N.z);
135 
136  /* basic idea is to think of this as a triangle, each corner representing
137  * one of the 3 faces of the cube. in the corners we have single textures,
138  * in between we blend between two textures, and in the middle we a blend
139  * between three textures.
140  *
141  * the Nxyz values are the barycentric coordinates in an equilateral
142  * triangle, which in case of blending, in the middle has a smaller
143  * equilateral triangle where 3 textures blend. this divides things into
144  * 7 zones, with an if() test for each zone */
145 
146  float3 weight = make_float3(0.0f, 0.0f, 0.0f);
147  float blend = __int_as_float(node.w);
148  float limit = 0.5f * (1.0f + blend);
149 
150  /* first test for corners with single texture */
151  if (N.x > limit * (N.x + N.y) && N.x > limit * (N.x + N.z)) {
152  weight.x = 1.0f;
153  }
154  else if (N.y > limit * (N.x + N.y) && N.y > limit * (N.y + N.z)) {
155  weight.y = 1.0f;
156  }
157  else if (N.z > limit * (N.x + N.z) && N.z > limit * (N.y + N.z)) {
158  weight.z = 1.0f;
159  }
160  else if (blend > 0.0f) {
161  /* in case of blending, test for mixes between two textures */
162  if (N.z < (1.0f - limit) * (N.y + N.x)) {
163  weight.x = N.x / (N.x + N.y);
164  weight.x = saturate((weight.x - 0.5f * (1.0f - blend)) / blend);
165  weight.y = 1.0f - weight.x;
166  }
167  else if (N.x < (1.0f - limit) * (N.y + N.z)) {
168  weight.y = N.y / (N.y + N.z);
169  weight.y = saturate((weight.y - 0.5f * (1.0f - blend)) / blend);
170  weight.z = 1.0f - weight.y;
171  }
172  else if (N.y < (1.0f - limit) * (N.x + N.z)) {
173  weight.x = N.x / (N.x + N.z);
174  weight.x = saturate((weight.x - 0.5f * (1.0f - blend)) / blend);
175  weight.z = 1.0f - weight.x;
176  }
177  else {
178  /* last case, we have a mix between three */
179  weight.x = ((2.0f - limit) * N.x + (limit - 1.0f)) / (2.0f * limit - 1.0f);
180  weight.y = ((2.0f - limit) * N.y + (limit - 1.0f)) / (2.0f * limit - 1.0f);
181  weight.z = ((2.0f - limit) * N.z + (limit - 1.0f)) / (2.0f * limit - 1.0f);
182  }
183  }
184  else {
185  /* Desperate mode, no valid choice anyway, fallback to one side.*/
186  weight.x = 1.0f;
187  }
188 
189  /* now fetch textures */
190  uint co_offset, out_offset, alpha_offset, flags;
191  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
192 
193  float3 co = stack_load_float3(stack, co_offset);
194  uint id = node.y;
195 
196  float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
197 
198  /* Map so that no textures are flipped, rotation is somewhat arbitrary. */
199  if (weight.x > 0.0f) {
200  float2 uv = make_float2((signed_N.x < 0.0f) ? 1.0f - co.y : co.y, co.z);
201  f += weight.x * svm_image_texture(kg, id, uv.x, uv.y, flags);
202  }
203  if (weight.y > 0.0f) {
204  float2 uv = make_float2((signed_N.y > 0.0f) ? 1.0f - co.x : co.x, co.z);
205  f += weight.y * svm_image_texture(kg, id, uv.x, uv.y, flags);
206  }
207  if (weight.z > 0.0f) {
208  float2 uv = make_float2((signed_N.z > 0.0f) ? 1.0f - co.y : co.y, co.x);
209  f += weight.z * svm_image_texture(kg, id, uv.x, uv.y, flags);
210  }
211 
212  if (stack_valid(out_offset))
213  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
214  if (stack_valid(alpha_offset))
215  stack_store_float(stack, alpha_offset, f.w);
216 }
217 
219  ShaderData *sd,
220  float *stack,
221  uint4 node)
222 {
223  uint id = node.y;
224  uint co_offset, out_offset, alpha_offset, flags;
225  uint projection = node.w;
226 
227  svm_unpack_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &flags);
228 
229  float3 co = stack_load_float3(stack, co_offset);
230  float2 uv;
231 
232  co = safe_normalize(co);
233 
234  if (projection == 0)
236  else
237  uv = direction_to_mirrorball(co);
238 
239  float4 f = svm_image_texture(kg, id, uv.x, uv.y, flags);
240 
241  if (stack_valid(out_offset))
242  stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
243  if (stack_valid(alpha_offset))
244  stack_store_float(stack, alpha_offset, f.w);
245 }
246 
void map_to_sphere(float *r_u, float *r_v, const float x, const float y, const float z)
Definition: math_geom.c:5236
void map_to_tube(float *r_u, float *r_v, const float x, const float y, const float z)
Definition: math_geom.c:5221
unsigned int uint
Definition: BLI_sys_types.h:83
_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
OperationNode * node
static CCL_NAMESPACE_BEGIN const double alpha
ccl_device_inline void object_inverse_normal_transform(KernelGlobals *kg, const ShaderData *sd, float3 *N)
Definition: geom_object.h:144
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
ccl_device_inline void stack_store_float(float *stack, uint a, float f)
ccl_device_inline bool stack_valid(uint a)
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define make_float2(x, y)
#define make_float4(x, y, z, w)
#define fabsf(x)
#define make_float3(x, y, z)
ccl_device float2 direction_to_mirrorball(float3 dir)
ccl_device float2 direction_to_equirectangular(float3 dir)
ShaderData
ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y)
params N
float z
Definition: sky_float3.h:35
float y
Definition: sky_float3.h:35
float x
Definition: sky_float3.h:35
ccl_device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
Definition: svm_image.h:47
CCL_NAMESPACE_BEGIN ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint flags)
Definition: svm_image.h:19
ccl_device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
Definition: svm_image.h:218
ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
Definition: svm_image.h:119
ccl_device_inline float3 texco_remap_square(float3 co)
Definition: svm_image.h:42
@ NODE_IMAGE_COMPRESS_AS_SRGB
Definition: svm_types.h:485
@ NODE_IMAGE_ALPHA_UNASSOCIATE
Definition: svm_types.h:486
@ NODE_IMAGE_PROJ_SPHERE
Definition: svm_types.h:480
@ NODE_IMAGE_PROJ_TUBE
Definition: svm_types.h:481
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:263
ccl_device_inline float saturate(float a)
Definition: util_math.h:315
ccl_device_inline float __int_as_float(int i)
Definition: util_math.h:212
ccl_device_inline float2 safe_normalize(const float2 &a)
#define TEX_IMAGE_MISSING_R
Definition: util_texture.h:28
#define TEX_IMAGE_MISSING_B
Definition: util_texture.h:30
#define TEX_IMAGE_MISSING_A
Definition: util_texture.h:31
#define TEX_IMAGE_MISSING_G
Definition: util_texture.h:29